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);
63 g_return_if_fail (items != 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);
93 g_return_if_fail (items != NULL);
96 while (cmp->items && it)
98 cmp->items = g_list_remove (cmp->items, it->data);
103 while (cmp->cache && it)
105 cmp->cache = g_list_remove(cmp->cache, it->data);
111 g_completion_clear_items (GCompletion* cmp)
113 g_return_if_fail (cmp != NULL);
115 g_list_free (cmp->items);
117 g_list_free (cmp->cache);
119 g_free (cmp->prefix);
124 completion_check_cache (GCompletion* cmp,
127 register GList* list;
142 len = strlen(cmp->prefix);
144 s = cmp->func ? cmp->func (list->data) : (gchar*) list->data;
146 plen = strlen (postfix);
151 s = cmp->func ? cmp->func (list->data) : (gchar*) list->data;
153 for (i = 0; i < plen; ++i)
155 if (postfix[i] != s[i])
162 *new_prefix = g_new0 (gchar, len + plen + 1);
163 strncpy (*new_prefix, cmp->prefix, len);
164 strncpy (*new_prefix + len, postfix, plen);
168 * g_completion_complete_utf8:
169 * @cmp: the #GCompletion
170 * @prefix: the prefix string, typically used by the user, which is compared
171 * with each of the items
172 * @new_prefix: if non-%NULL, returns the longest prefix which is common to all
173 * items that matched @prefix, or %NULL if no items matched @prefix.
174 * This string should be freed when no longer needed.
176 * Attempts to complete the string @prefix using the #GCompletion target items.
177 * In contrast to g_completion_complete(), this function returns the largest common
178 * prefix that is a valid UTF-8 string, omitting a possible common partial
181 * You should use this function instead of g_completion_complete() if your
182 * items are UTF-8 strings.
184 * Return value: the list of items whose strings begin with @prefix. This should
190 g_completion_complete_utf8 (GCompletion *cmp,
197 list = g_completion_complete (cmp, prefix, new_prefix);
201 p = *new_prefix + strlen (*new_prefix);
202 q = g_utf8_find_prev_char (*new_prefix, p);
204 switch (g_utf8_get_char_validated (q, p - q))
219 g_completion_complete (GCompletion* cmp,
224 gboolean done = FALSE;
227 g_return_val_if_fail (cmp != NULL, NULL);
228 g_return_val_if_fail (prefix != NULL, NULL);
230 len = strlen (prefix);
231 if (cmp->prefix && cmp->cache)
233 plen = strlen (cmp->prefix);
234 if (plen <= len && ! cmp->strncmp_func (prefix, cmp->prefix, plen))
240 GList *next = list->next;
242 if (cmp->strncmp_func (prefix,
243 cmp->func ? cmp->func (list->data) : (gchar*) list->data,
245 cmp->cache = g_list_delete_link (cmp->cache, list);
256 g_list_free (cmp->cache);
259 while (*prefix && list)
261 if (!cmp->strncmp_func (prefix,
262 cmp->func ? cmp->func (list->data) : (gchar*) list->data,
264 cmp->cache = g_list_prepend (cmp->cache, list->data);
270 g_free (cmp->prefix);
274 cmp->prefix = g_strdup (prefix);
275 completion_check_cache (cmp, new_prefix);
277 return *prefix ? cmp->cache : cmp->items;
281 g_completion_free (GCompletion* cmp)
283 g_return_if_fail (cmp != NULL);
285 g_completion_clear_items (cmp);
290 g_completion_set_compare(GCompletion *cmp,
291 GCompletionStrncmpFunc strncmp_func)
293 cmp->strncmp_func = strncmp_func;
296 #ifdef TEST_COMPLETION
313 g_warning ("Usage: %s filename prefix1 [prefix2 ...]\n", argv[0]);
317 file = fopen (argv[1], "r");
320 g_warning ("Cannot open %s\n", argv[1]);
324 cmp = g_completion_new (NULL);
325 list = g_list_alloc ();
326 while (fgets (buf, 1024, file))
328 list->data = g_strdup (buf);
329 g_completion_add_items (cmp, list);
333 for (i = 2; i < argc; ++i)
335 printf ("COMPLETING: %s\n", argv[i]);
336 result = g_completion_complete (cmp, argv[i], &longp);
337 g_list_foreach (result, (GFunc) printf, NULL);
338 printf ("LONG MATCH: %s\n", longp);
343 g_list_foreach (cmp->items, (GFunc) g_free, NULL);
344 g_completion_free (cmp);