1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
38 static void completion_check_cache (GCompletion* cmp,
42 g_completion_new (GCompletionFunc func)
46 gcomp = g_new (GCompletion, 1);
51 gcomp->strncmp_func = strncmp;
57 g_completion_add_items (GCompletion* cmp,
62 g_return_if_fail (cmp != NULL);
64 /* optimize adding to cache? */
67 g_list_free (cmp->cache);
80 cmp->items = g_list_prepend (cmp->items, it->data);
86 g_completion_remove_items (GCompletion* cmp,
91 g_return_if_fail (cmp != NULL);
94 while (cmp->items && it)
96 cmp->items = g_list_remove (cmp->items, it->data);
101 while (cmp->cache && it)
103 cmp->cache = g_list_remove(cmp->cache, it->data);
109 g_completion_clear_items (GCompletion* cmp)
111 g_return_if_fail (cmp != NULL);
113 g_list_free (cmp->items);
115 g_list_free (cmp->cache);
117 g_free (cmp->prefix);
122 completion_check_cache (GCompletion* cmp,
125 register GList* list;
140 len = strlen(cmp->prefix);
142 s = cmp->func ? cmp->func (list->data) : (gchar*) list->data;
144 plen = strlen (postfix);
149 s = cmp->func ? cmp->func (list->data) : (gchar*) list->data;
151 for (i = 0; i < plen; ++i)
153 if (postfix[i] != s[i])
160 *new_prefix = g_new0 (gchar, len + plen + 1);
161 strncpy (*new_prefix, cmp->prefix, len);
162 strncpy (*new_prefix + len, postfix, plen);
166 * g_completion_complete_utf8:
167 * @cmp: the #GCompletion
168 * @prefix: the prefix string, typically used by the user, which is compared
169 * with each of the items
170 * @new_prefix: if non-%NULL, returns the longest prefix which is common to all
171 * items that matched @prefix, or %NULL if no items matched @prefix.
172 * This string should be freed when no longer needed.
174 * Attempts to complete the string @prefix using the #GCompletion target items.
175 * In contrast to g_completion_complete(), this function returns the largest common
176 * prefix that is a valid UTF-8 string, omitting a possible common partial
179 * You should use this function instead of g_completion_complete() if your
180 * items are UTF-8 strings.
182 * Return value: the list of items whose strings begin with @prefix. This should
188 g_completion_complete_utf8 (GCompletion *cmp,
195 list = g_completion_complete (cmp, prefix, new_prefix);
199 p = *new_prefix + strlen (*new_prefix);
200 q = g_utf8_find_prev_char (*new_prefix, p);
202 switch (g_utf8_get_char_validated (q, p - q))
217 g_completion_complete (GCompletion* cmp,
222 gboolean done = FALSE;
225 g_return_val_if_fail (cmp != NULL, NULL);
226 g_return_val_if_fail (prefix != NULL, NULL);
228 len = strlen (prefix);
229 if (cmp->prefix && cmp->cache)
231 plen = strlen (cmp->prefix);
232 if (plen <= len && ! cmp->strncmp_func (prefix, cmp->prefix, plen))
238 GList *next = list->next;
240 if (cmp->strncmp_func (prefix,
241 cmp->func ? cmp->func (list->data) : (gchar*) list->data,
243 cmp->cache = g_list_delete_link (cmp->cache, list);
254 g_list_free (cmp->cache);
257 while (*prefix && list)
259 if (!cmp->strncmp_func (prefix,
260 cmp->func ? cmp->func (list->data) : (gchar*) list->data,
262 cmp->cache = g_list_prepend (cmp->cache, list->data);
268 g_free (cmp->prefix);
272 cmp->prefix = g_strdup (prefix);
273 completion_check_cache (cmp, new_prefix);
275 return *prefix ? cmp->cache : cmp->items;
279 g_completion_free (GCompletion* cmp)
281 g_return_if_fail (cmp != NULL);
283 g_completion_clear_items (cmp);
288 g_completion_set_compare(GCompletion *cmp,
289 GCompletionStrncmpFunc strncmp_func)
291 cmp->strncmp_func = strncmp_func;
294 #ifdef TEST_COMPLETION
311 g_warning ("Usage: %s filename prefix1 [prefix2 ...]\n", argv[0]);
315 file = fopen (argv[1], "r");
318 g_warning ("Cannot open %s\n", argv[1]);
322 cmp = g_completion_new (NULL);
323 list = g_list_alloc ();
324 while (fgets (buf, 1024, file))
326 list->data = g_strdup (buf);
327 g_completion_add_items (cmp, list);
331 for (i = 2; i < argc; ++i)
333 printf ("COMPLETING: %s\n", argv[i]);
334 result = g_completion_complete (cmp, argv[i], &longp);
335 g_list_foreach (result, (GFunc) printf, NULL);
336 printf ("LONG MATCH: %s\n", longp);
341 g_list_foreach (cmp->items, (GFunc) g_free, NULL);
342 g_completion_free (cmp);
349 #define __G_COMPLETION_C__
350 #include "galiasdef.c"