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.
23 static void completion_check_cache (GCompletion* cmp,
27 g_completion_new (GCompletionFunc func)
31 gcomp = g_new (GCompletion, 1);
41 g_completion_add_items (GCompletion* cmp,
46 g_return_if_fail (cmp != NULL);
47 g_return_if_fail (items != NULL);
49 /* optimize adding to cache? */
52 g_list_free (cmp->cache);
65 cmp->items = g_list_prepend (cmp->items, it->data);
71 g_completion_remove_items (GCompletion* cmp,
76 g_return_if_fail (cmp != NULL);
77 g_return_if_fail (items != NULL);
80 while (cmp->items && it)
82 cmp->items = g_list_remove (cmp->items, it->data);
87 while (cmp->cache && it)
89 cmp->cache = g_list_remove(cmp->cache, it->data);
95 g_completion_clear_items (GCompletion* cmp)
97 g_return_if_fail (cmp != NULL);
99 g_list_free (cmp->items);
101 g_list_free (cmp->cache);
103 g_free (cmp->prefix);
108 completion_check_cache (GCompletion* cmp,
111 register GList* list;
126 len = strlen(cmp->prefix);
128 s = cmp->func ? cmp->func (list->data) : (gchar*) list->data;
130 plen = strlen (postfix);
135 s = cmp->func ? cmp->func (list->data) : (gchar*) list->data;
137 for (i = 0; i < plen; ++i)
139 if (postfix[i] != s[i])
146 *new_prefix = g_new0 (gchar, len + plen + 1);
147 strncpy (*new_prefix, cmp->prefix, len);
148 strncpy (*new_prefix + len, postfix, plen);
152 g_completion_complete (GCompletion* cmp,
160 g_return_val_if_fail (cmp != NULL, NULL);
161 g_return_val_if_fail (prefix != NULL, NULL);
163 len = strlen (prefix);
164 if (cmp->prefix && cmp->cache)
166 plen = strlen (cmp->prefix);
167 if (plen <= len && !strncmp (prefix, cmp->prefix, plen))
174 cmp->func ? cmp->func (list->data) : (gchar*) list->data,
177 list = g_list_remove_link (cmp->cache, list);
178 if (list != cmp->cache)
191 g_list_free (cmp->cache);
194 while (*prefix && list)
196 if (!strncmp (prefix,
197 cmp->func ? cmp->func (list->data) : (gchar*) list->data,
199 cmp->cache = g_list_prepend (cmp->cache, list->data);
205 g_free (cmp->prefix);
209 cmp->prefix = g_strdup (prefix);
210 completion_check_cache (cmp, new_prefix);
212 return *prefix ? cmp->cache : cmp->items;
216 g_completion_free (GCompletion* cmp)
218 g_return_if_fail (cmp != NULL);
220 g_completion_clear_items (cmp);
224 #ifdef TEST_COMPLETION
241 g_warning ("Usage: %s filename prefix1 [prefix2 ...]\n", argv[0]);
245 file = fopen (argv[1], "r");
248 g_warning ("Cannot open %s\n", argv[1]);
252 cmp = g_completion_new (NULL);
253 list = g_list_alloc ();
254 while (fgets (buf, 1024, file))
256 list->data = g_strdup (buf);
257 g_completion_add_items (cmp, list);
261 for (i = 2; i < argc; ++i)
263 printf ("COMPLETING: %s\n", argv[i]);
264 result = g_completion_complete (cmp, argv[i], &longp);
265 g_list_foreach (result, (GFunc) printf, NULL);
266 printf ("LONG MATCH: %s\n", longp);
271 g_list_foreach (cmp->items, (GFunc) g_free, NULL);
272 g_completion_free (cmp);