94
|
1 /*
|
|
2 * compl.c -- Completion system
|
|
3 *
|
|
4 * Copyright (C) 2005 Mikael Berthe <bmikael@lists.lilotux.net>
|
|
5 *
|
|
6 * This program is free software; you can redistribute it and/or modify
|
|
7 * it under the terms of the GNU General Public License as published by
|
|
8 * the Free Software Foundation; either version 2 of the License, or (at
|
|
9 * your option) any later version.
|
|
10 *
|
|
11 * This program is distributed in the hope that it will be useful, but
|
|
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
14 * General Public License for more details.
|
|
15 *
|
|
16 * You should have received a copy of the GNU General Public License
|
|
17 * along with this program; if not, write to the Free Software
|
|
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
19 * USA
|
|
20 */
|
|
21
|
|
22 /* Usage, basically:
|
|
23 * - new_completion(); // 1. Initialization
|
|
24 * - complete(); // 2. 1st completion
|
|
25 * - cancel_completion(); // 3a. 2nd completion / cancel previous
|
|
26 * - complete(); // 3b. 2nd completion / complete
|
|
27 * ...
|
|
28 * - done_completion(); // n. finished -- free allocated areas
|
|
29 *
|
|
30 */
|
|
31
|
|
32 #include <string.h>
|
|
33
|
|
34 #include "compl.h"
|
|
35
|
|
36 // Completion structure
|
|
37 typedef struct {
|
|
38 GSList *list; // list of matches
|
|
39 guint len_prefix; // length of text already typed by the user
|
|
40 guint len_compl; // length of the last completion
|
|
41 GSList *next; // pointer to next completion to try
|
|
42 } compl;
|
|
43
|
|
44 // Category structure
|
|
45 typedef struct {
|
|
46 guint flag;
|
|
47 GSList *words;
|
|
48 } category;
|
|
49
|
|
50 static GSList *Categories;
|
|
51 static compl *InputCompl;
|
|
52
|
|
53 // XXX Should not be there (?)
|
|
54 // jid_list(type)
|
|
55 // Returns a list of jid's. If type is COMPL_URLJID, urls are surrounded with
|
|
56 // '<' and '>'.
|
|
57 GSList *jid_list(guint type) // bool urlstyle?
|
|
58 {
|
|
59 }
|
|
60
|
|
61 // new_completion(prefix, compl_cat)
|
|
62 // . prefix = beginning of the word, typed by the user
|
|
63 // . compl_cat = pointer to a completion category list (list of *char)
|
|
64 // Returns a pointer to an allocated compl structure. This structure should
|
|
65 // be freed by the caller when not used anymore.
|
|
66 //compl *new_completion(char *prefix, GSList *compl_cat)
|
|
67 void new_completion(char *prefix, GSList *compl_cat)
|
|
68 {
|
|
69 compl *c;
|
|
70 GSList *sl_cat;
|
|
71 int len = strlen(prefix);
|
|
72
|
|
73 if (InputCompl) { // This should not happen, but hey...
|
|
74 cancel_completion();
|
|
75 }
|
|
76
|
|
77 c = g_new0(compl, 1);
|
|
78 // Build the list of matches
|
|
79 for (sl_cat=compl_cat; sl_cat; sl_cat = g_slist_next(sl_cat)) {
|
|
80 char *word = sl_cat->data;
|
|
81 if (!strncmp(prefix, word, len)) {
|
|
82 c->list = g_slist_append(c->list, g_strdup(word+len)); // TODO sort
|
|
83 }
|
|
84 }
|
|
85 c->next = c->list;
|
|
86 InputCompl = c;
|
|
87 }
|
|
88
|
|
89 // done_completion();
|
|
90 void done_completion(void)
|
|
91 {
|
|
92 // TODO free everything
|
|
93 g_slist_free(InputCompl->list);
|
|
94 g_free(InputCompl);
|
|
95 InputCompl = NULL;
|
|
96 }
|
|
97
|
|
98 // cancel_completion()
|
|
99 // Returns the number of chars to delete to cancel the completion
|
|
100 //guint cancel_completion(compl *c)
|
|
101 guint cancel_completion(void)
|
|
102 {
|
|
103 return InputCompl->len_compl;
|
|
104 }
|
|
105
|
|
106 // Returns pointer to text to insert, NULL if no completion.
|
|
107 const char *complete()
|
|
108 {
|
|
109 compl* c = InputCompl;
|
|
110 char *r;
|
|
111 if (!c->next) {
|
|
112 c->next = c->list; // back to the beginning
|
|
113 c->len_compl = 0;
|
|
114 return NULL;
|
|
115 }
|
|
116 r = (char*)c->next->data;
|
|
117 c->next = g_slist_next(c->next);
|
|
118 c->len_compl = strlen(r);
|
|
119 return r;
|
|
120 }
|
|
121
|
|
122
|
|
123 /* Categories functions */
|
|
124
|
95
|
125 // compl_add_category_word(categ, command)
|
|
126 // Adds a keyword as a possible completion in category categ.
|
|
127 void compl_add_category_word(guint categ, const char *word)
|
94
|
128 {
|
|
129 GSList *sl_cat;
|
|
130 category *cat;
|
|
131 // Look for category
|
|
132 for (sl_cat=Categories; sl_cat; sl_cat = g_slist_next(sl_cat)) {
|
|
133 if (categ == ((category*)sl_cat->data)->flag)
|
|
134 break;
|
|
135 }
|
|
136 if (!sl_cat) { // Category not found, let's create it
|
|
137 cat = g_new0(category, 1);
|
|
138 cat->flag = categ;
|
|
139 Categories = g_slist_append(Categories, cat);
|
|
140 } else
|
|
141 cat = (category*)sl_cat->data;
|
|
142
|
|
143 // TODO Check word does not already exist
|
95
|
144 cat->words = g_slist_append(cat->words, g_strdup(word)); // TODO sort
|
94
|
145 }
|
|
146
|
95
|
147 // compl_get_category_list()
|
|
148 // Returns a slist of all words in the categories specified by the given flags
|
94
|
149 GSList *compl_get_category_list(guint cat_flags)
|
|
150 {
|
|
151 GSList *sl_cat;
|
|
152 // Look for category
|
|
153 // XXX Actually that's not that simple... cat_flags can be a combination
|
|
154 // of several flags!
|
|
155 for (sl_cat=Categories; sl_cat; sl_cat = g_slist_next(sl_cat)) {
|
|
156 if (cat_flags == ((category*)sl_cat->data)->flag)
|
|
157 break;
|
|
158 }
|
|
159 if (sl_cat) // Category was found, easy...
|
|
160 return ((category*)sl_cat->data)->words;
|
|
161
|
|
162 // TODO handle dynamic SLists :)
|
|
163 return NULL;
|
|
164 }
|
|
165
|
|
166
|
|
167 /*
|
|
168 void test_dump_categories(void)
|
|
169 {
|
|
170 GSList *sl_cat;
|
|
171 // Look for category
|
|
172 for (sl_cat=Categories; sl_cat; sl_cat = g_slist_next(sl_cat)) {
|
|
173 GSList *sl_words;
|
|
174 category *cat = (category*)sl_cat->data;
|
|
175 printf("Category: %u\n", cat->flag);
|
|
176 for (sl_words=cat->words; sl_words; sl_words = g_slist_next(sl_words)) {
|
|
177 char *word = sl_words->data;
|
|
178 printf(" Keyword: [%s]\n", word);
|
|
179 }
|
|
180 }
|
|
181 }
|
|
182
|
|
183 void test_comp(guint cat_flags, char *prefix)
|
|
184 {
|
|
185 GSList *list = compl_get_category_list(cat_flags);
|
|
186 if (list) {
|
|
187 const char *cchar;
|
|
188 guint back;
|
|
189 int i;
|
|
190 new_completion(prefix, list);
|
|
191 cchar = complete();
|
|
192 printf("Complete -> \"%s\"\n", cchar);
|
|
193 for (i=0 ; i<5; i++) {
|
|
194 back = cancel_completion();
|
|
195 printf("Back: %d\n", back);
|
|
196 cchar = complete();
|
|
197 printf("Complete -> \"%s\"\n", cchar);
|
|
198 }
|
|
199 back = cancel_completion();
|
|
200 printf("Back: %d\n", back);
|
|
201 done_completion();
|
|
202 }
|
|
203 }
|
|
204 */
|