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/.
39 static void completion_check_cache (GCompletion* cmp,
43 g_completion_new (GCompletionFunc func)
47 gcomp = g_new (GCompletion, 1);
52 gcomp->strncmp_func = strncmp;
58 g_completion_add_items (GCompletion* cmp,
63 g_return_if_fail (cmp != NULL);
65 /* optimize adding to cache? */
68 g_list_free (cmp->cache);
81 cmp->items = g_list_prepend (cmp->items, it->data);
87 g_completion_remove_items (GCompletion* cmp,
92 g_return_if_fail (cmp != NULL);
95 while (cmp->items && it)
97 cmp->items = g_list_remove (cmp->items, it->data);
102 while (cmp->cache && it)
104 cmp->cache = g_list_remove(cmp->cache, it->data);
110 g_completion_clear_items (GCompletion* cmp)
112 g_return_if_fail (cmp != NULL);
114 g_list_free (cmp->items);
116 g_list_free (cmp->cache);
118 g_free (cmp->prefix);
123 completion_check_cache (GCompletion* cmp,
126 register GList* list;
141 len = strlen(cmp->prefix);
143 s = cmp->func ? cmp->func (list->data) : (gchar*) list->data;
145 plen = strlen (postfix);
150 s = cmp->func ? cmp->func (list->data) : (gchar*) list->data;
152 for (i = 0; i < plen; ++i)
154 if (postfix[i] != s[i])
161 *new_prefix = g_new0 (gchar, len + plen + 1);
162 strncpy (*new_prefix, cmp->prefix, len);
163 strncpy (*new_prefix + len, postfix, plen);
167 * g_completion_complete_utf8:
168 * @cmp: the #GCompletion
169 * @prefix: the prefix string, typically used by the user, which is compared
170 * with each of the items
171 * @new_prefix: if non-%NULL, returns the longest prefix which is common to all
172 * items that matched @prefix, or %NULL if no items matched @prefix.
173 * This string should be freed when no longer needed.
175 * Attempts to complete the string @prefix using the #GCompletion target items.
176 * In contrast to g_completion_complete(), this function returns the largest common
177 * prefix that is a valid UTF-8 string, omitting a possible common partial
180 * You should use this function instead of g_completion_complete() if your
181 * items are UTF-8 strings.
183 * Return value: the list of items whose strings begin with @prefix. This should
189 g_completion_complete_utf8 (GCompletion *cmp,
196 list = g_completion_complete (cmp, prefix, new_prefix);
200 p = *new_prefix + strlen (*new_prefix);
201 q = g_utf8_find_prev_char (*new_prefix, p);
203 switch (g_utf8_get_char_validated (q, p - q))
218 g_completion_complete (GCompletion* cmp,
223 gboolean done = FALSE;
226 g_return_val_if_fail (cmp != NULL, NULL);
227 g_return_val_if_fail (prefix != NULL, NULL);
229 len = strlen (prefix);
230 if (cmp->prefix && cmp->cache)
232 plen = strlen (cmp->prefix);
233 if (plen <= len && ! cmp->strncmp_func (prefix, cmp->prefix, plen))
239 GList *next = list->next;
241 if (cmp->strncmp_func (prefix,
242 cmp->func ? cmp->func (list->data) : (gchar*) list->data,
244 cmp->cache = g_list_delete_link (cmp->cache, list);
255 g_list_free (cmp->cache);
258 while (*prefix && list)
260 if (!cmp->strncmp_func (prefix,
261 cmp->func ? cmp->func (list->data) : (gchar*) list->data,
263 cmp->cache = g_list_prepend (cmp->cache, list->data);
269 g_free (cmp->prefix);
273 cmp->prefix = g_strdup (prefix);
274 completion_check_cache (cmp, new_prefix);
276 return *prefix ? cmp->cache : cmp->items;
280 g_completion_free (GCompletion* cmp)
282 g_return_if_fail (cmp != NULL);
284 g_completion_clear_items (cmp);
289 g_completion_set_compare(GCompletion *cmp,
290 GCompletionStrncmpFunc strncmp_func)
292 cmp->strncmp_func = strncmp_func;
295 #ifdef TEST_COMPLETION
312 g_warning ("Usage: %s filename prefix1 [prefix2 ...]\n", argv[0]);
316 file = fopen (argv[1], "r");
319 g_warning ("Cannot open %s\n", argv[1]);
323 cmp = g_completion_new (NULL);
324 list = g_list_alloc ();
325 while (fgets (buf, 1024, file))
327 list->data = g_strdup (buf);
328 g_completion_add_items (cmp, list);
332 for (i = 2; i < argc; ++i)
334 printf ("COMPLETING: %s\n", argv[i]);
335 result = g_completion_complete (cmp, argv[i], &longp);
336 g_list_foreach (result, (GFunc) printf, NULL);
337 printf ("LONG MATCH: %s\n", longp);
342 g_list_foreach (cmp->items, (GFunc) g_free, NULL);
343 g_completion_free (cmp);