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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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.
27 static void completion_check_cache (GCompletion* cmp,
31 g_completion_new (GCompletionFunc func)
35 gcomp = g_new (GCompletion, 1);
45 g_completion_add_items (GCompletion* cmp,
50 g_return_if_fail (cmp != NULL);
51 g_return_if_fail (items != NULL);
53 /* optimize adding to cache? */
56 g_list_free (cmp->cache);
69 cmp->items = g_list_prepend (cmp->items, it->data);
75 g_completion_remove_items (GCompletion* cmp,
80 g_return_if_fail (cmp != NULL);
81 g_return_if_fail (items != NULL);
84 while (cmp->items && it)
86 cmp->items = g_list_remove (cmp->items, it->data);
91 while (cmp->cache && it)
93 cmp->cache = g_list_remove(cmp->cache, it->data);
99 g_completion_clear_items (GCompletion* cmp)
101 g_return_if_fail (cmp != NULL);
103 g_list_free (cmp->items);
105 g_list_free (cmp->cache);
107 g_free (cmp->prefix);
112 completion_check_cache (GCompletion* cmp,
115 register GList* list;
130 len = strlen(cmp->prefix);
132 s = cmp->func ? cmp->func (list->data) : (gchar*) list->data;
134 plen = strlen (postfix);
139 s = cmp->func ? cmp->func (list->data) : (gchar*) list->data;
141 for (i = 0; i < plen; ++i)
143 if (postfix[i] != s[i])
150 *new_prefix = g_new0 (gchar, len + plen + 1);
151 strncpy (*new_prefix, cmp->prefix, len);
152 strncpy (*new_prefix + len, postfix, plen);
156 g_completion_complete (GCompletion* cmp,
164 g_return_val_if_fail (cmp != NULL, NULL);
165 g_return_val_if_fail (prefix != NULL, NULL);
167 len = strlen (prefix);
168 if (cmp->prefix && cmp->cache)
170 plen = strlen (cmp->prefix);
171 if (plen <= len && !strncmp (prefix, cmp->prefix, plen))
178 cmp->func ? cmp->func (list->data) : (gchar*) list->data,
181 list = g_list_remove_link (cmp->cache, list);
182 if (list != cmp->cache)
195 g_list_free (cmp->cache);
198 while (*prefix && list)
200 if (!strncmp (prefix,
201 cmp->func ? cmp->func (list->data) : (gchar*) list->data,
203 cmp->cache = g_list_prepend (cmp->cache, list->data);
209 g_free (cmp->prefix);
213 cmp->prefix = g_strdup (prefix);
214 completion_check_cache (cmp, new_prefix);
216 return *prefix ? cmp->cache : cmp->items;
220 g_completion_free (GCompletion* cmp)
222 g_return_if_fail (cmp != NULL);
224 g_completion_clear_items (cmp);
228 #ifdef TEST_COMPLETION
245 g_warning ("Usage: %s filename prefix1 [prefix2 ...]\n", argv[0]);
249 file = fopen (argv[1], "r");
252 g_warning ("Cannot open %s\n", argv[1]);
256 cmp = g_completion_new (NULL);
257 list = g_list_alloc ();
258 while (fgets (buf, 1024, file))
260 list->data = g_strdup (buf);
261 g_completion_add_items (cmp, list);
265 for (i = 2; i < argc; ++i)
267 printf ("COMPLETING: %s\n", argv[i]);
268 result = g_completion_complete (cmp, argv[i], &longp);
269 g_list_foreach (result, (GFunc) printf, NULL);
270 printf ("LONG MATCH: %s\n", longp);
275 g_list_foreach (cmp->items, (GFunc) g_free, NULL);
276 g_completion_free (cmp);