Mercurial > hg
annotate mcabber/src/compl.c @ 1617:9ca672ee884f
Fix previous commit :)
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Sun, 11 Oct 2009 22:47:12 +0200 |
parents | 14690e624e9d |
children |
rev | line source |
---|---|
94 | 1 /* |
2 * compl.c -- Completion system | |
393 | 3 * |
1599 | 4 * Copyright (C) 2005-2009 Mikael Berthe <mikael@lilotux.net> |
94 | 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" | |
1240
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
35 #include "utf8.h" |
225 | 36 #include "roster.h" |
757
ae23c8826efb
Improve completion for the "/event" command
Mikael Berthe <mikael@lilotux.net>
parents:
735
diff
changeset
|
37 #include "events.h" |
94 | 38 |
39 // Completion structure | |
40 typedef struct { | |
41 GSList *list; // list of matches | |
42 guint len_prefix; // length of text already typed by the user | |
43 guint len_compl; // length of the last completion | |
44 GSList *next; // pointer to next completion to try | |
45 } compl; | |
46 | |
47 // Category structure | |
48 typedef struct { | |
49 guint flag; | |
50 GSList *words; | |
51 } category; | |
52 | |
53 static GSList *Categories; | |
54 static compl *InputCompl; | |
55 | |
1607 | 56 #ifdef MODULES_ENABLE |
57 guint registered_cats = COMPL_CMD|COMPL_JID|COMPL_URLJID|COMPL_NAME| \ | |
58 COMPL_STATUS|COMPL_FILENAME|COMPL_ROSTER|COMPL_BUFFER| \ | |
59 COMPL_GROUP|COMPL_GROUPNAME|COMPL_MULTILINE|COMPL_ROOM| \ | |
60 COMPL_RESOURCE|COMPL_AUTH|COMPL_REQUEST|COMPL_EVENTS| \ | |
61 COMPL_EVENTSID|COMPL_PGP|COMPL_COLOR| \ | |
62 COMPL_OTR|COMPL_OTRPOLICY| \ | |
63 0; | |
64 | |
65 // compl_new_category() | |
66 // Reserves id for new completion category. | |
67 // Returns 0, if no more categories can be allocated. | |
68 // Note, that user should not make any assumptions about id nature, | |
69 // as it is likely to change in future. | |
70 guint compl_new_category (void) | |
71 { | |
72 guint i = 0; | |
73 while ((registered_cats >> i) & 1) | |
74 i++; | |
75 if (i >= sizeof (guint)*8) | |
76 return 0; | |
77 else { | |
78 guint id = 1 << i; | |
79 registered_cats |= id; | |
80 return id; | |
81 } | |
82 } | |
83 | |
84 // compl_del_category (id) | |
85 // Frees reserved id for category. | |
86 // Note, that for now it not validates its input, so, be careful | |
87 // and specify exactly what you get from compl_new_category. | |
88 void compl_del_category (guint id) | |
89 { | |
90 registered_cats &= ~id; | |
91 } | |
92 #endif | |
93 | |
94 | 94 // new_completion(prefix, compl_cat) |
95 // . prefix = beginning of the word, typed by the user | |
96 // . compl_cat = pointer to a completion category list (list of *char) | |
894
f76b32ff2f14
done_completion(): free all allocated memory
Mikael Berthe <mikael@lilotux.net>
parents:
820
diff
changeset
|
97 // Set the InputCompl pointer to an allocated compl structure. |
f76b32ff2f14
done_completion(): free all allocated memory
Mikael Berthe <mikael@lilotux.net>
parents:
820
diff
changeset
|
98 // done_completion() must be called when finished. |
1228
9a68fe4515dc
Improve MUC nickname completion
Mikael Berthe <mikael@lilotux.net>
parents:
1205
diff
changeset
|
99 // Returns the number of possible completions. |
9a68fe4515dc
Improve MUC nickname completion
Mikael Berthe <mikael@lilotux.net>
parents:
1205
diff
changeset
|
100 guint new_completion(char *prefix, GSList *compl_cat) |
94 | 101 { |
102 compl *c; | |
103 GSList *sl_cat; | |
735 | 104 size_t len = strlen(prefix); |
94 | 105 |
106 if (InputCompl) { // This should not happen, but hey... | |
107 cancel_completion(); | |
108 } | |
109 | |
110 c = g_new0(compl, 1); | |
111 // Build the list of matches | |
1228
9a68fe4515dc
Improve MUC nickname completion
Mikael Berthe <mikael@lilotux.net>
parents:
1205
diff
changeset
|
112 for (sl_cat = compl_cat; sl_cat; sl_cat = g_slist_next(sl_cat)) { |
94 | 113 char *word = sl_cat->data; |
104 | 114 if (!strncasecmp(prefix, word, len)) { |
98 | 115 if (strlen(word) != len) |
116 c->list = g_slist_append(c->list, g_strdup(word+len)); // TODO sort | |
94 | 117 } |
118 } | |
119 c->next = c->list; | |
120 InputCompl = c; | |
1228
9a68fe4515dc
Improve MUC nickname completion
Mikael Berthe <mikael@lilotux.net>
parents:
1205
diff
changeset
|
121 return g_slist_length(c->list); |
94 | 122 } |
123 | |
124 // done_completion(); | |
125 void done_completion(void) | |
126 { | |
894
f76b32ff2f14
done_completion(): free all allocated memory
Mikael Berthe <mikael@lilotux.net>
parents:
820
diff
changeset
|
127 GSList *clp; |
f76b32ff2f14
done_completion(): free all allocated memory
Mikael Berthe <mikael@lilotux.net>
parents:
820
diff
changeset
|
128 |
98 | 129 if (!InputCompl) return; |
130 | |
894
f76b32ff2f14
done_completion(): free all allocated memory
Mikael Berthe <mikael@lilotux.net>
parents:
820
diff
changeset
|
131 // Free the current completion list |
f76b32ff2f14
done_completion(): free all allocated memory
Mikael Berthe <mikael@lilotux.net>
parents:
820
diff
changeset
|
132 for (clp = InputCompl->list; clp; clp = g_slist_next(clp)) |
f76b32ff2f14
done_completion(): free all allocated memory
Mikael Berthe <mikael@lilotux.net>
parents:
820
diff
changeset
|
133 g_free(clp->data); |
94 | 134 g_slist_free(InputCompl->list); |
135 g_free(InputCompl); | |
136 InputCompl = NULL; | |
137 } | |
138 | |
139 // cancel_completion() | |
140 // Returns the number of chars to delete to cancel the completion | |
141 //guint cancel_completion(compl *c) | |
142 guint cancel_completion(void) | |
143 { | |
98 | 144 if (!InputCompl) return 0; |
94 | 145 return InputCompl->len_compl; |
146 } | |
147 | |
148 // Returns pointer to text to insert, NULL if no completion. | |
149 const char *complete() | |
150 { | |
151 compl* c = InputCompl; | |
152 char *r; | |
98 | 153 |
154 if (!InputCompl) return NULL; | |
155 | |
94 | 156 if (!c->next) { |
157 c->next = c->list; // back to the beginning | |
158 c->len_compl = 0; | |
159 return NULL; | |
160 } | |
161 r = (char*)c->next->data; | |
162 c->next = g_slist_next(c->next); | |
1240
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
163 if (!utf8_mode) { |
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
164 c->len_compl = strlen(r); |
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
165 } else { |
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
166 char *wc; |
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
167 c->len_compl = 0; |
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
168 for (wc = r; *wc; wc = next_char(wc)) |
1569
21c553e4bfb9
Fix completion with wide characters (e.g. Japanese characters)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1414
diff
changeset
|
169 c->len_compl++; |
1240
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
170 } |
94 | 171 return r; |
172 } | |
173 | |
174 | |
175 /* Categories functions */ | |
176 | |
95 | 177 // compl_add_category_word(categ, command) |
178 // Adds a keyword as a possible completion in category categ. | |
179 void compl_add_category_word(guint categ, const char *word) | |
94 | 180 { |
181 GSList *sl_cat; | |
182 category *cat; | |
121 | 183 char *nword; |
94 | 184 // Look for category |
185 for (sl_cat=Categories; sl_cat; sl_cat = g_slist_next(sl_cat)) { | |
186 if (categ == ((category*)sl_cat->data)->flag) | |
187 break; | |
188 } | |
189 if (!sl_cat) { // Category not found, let's create it | |
190 cat = g_new0(category, 1); | |
191 cat->flag = categ; | |
192 Categories = g_slist_append(Categories, cat); | |
193 } else | |
194 cat = (category*)sl_cat->data; | |
195 | |
121 | 196 // If word is not space-terminated, we add one trailing space |
197 for (nword = (char*)word; *nword; nword++) | |
198 ; | |
199 if (nword > word) nword--; | |
200 if (*nword != ' ') { // Add a space | |
1240
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
201 nword = g_strdup_printf("%s ", word); |
121 | 202 } else { // word is fine |
203 nword = g_strdup(word); | |
204 } | |
205 | |
94 | 206 // TODO Check word does not already exist |
121 | 207 cat->words = g_slist_append(cat->words, nword); // TODO sort |
94 | 208 } |
209 | |
284
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
210 // compl_del_category_word(categ, command) |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
211 // Removes a keyword from category categ in completion list. |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
212 void compl_del_category_word(guint categ, const char *word) |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
213 { |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
214 GSList *sl_cat, *sl_elt; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
215 category *cat; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
216 char *nword; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
217 // Look for category |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
218 for (sl_cat=Categories; sl_cat; sl_cat = g_slist_next(sl_cat)) { |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
219 if (categ == ((category*)sl_cat->data)->flag) |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
220 break; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
221 } |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
222 if (!sl_cat) return; // Category not found, finished! |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
223 |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
224 cat = (category*)sl_cat->data; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
225 |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
226 // If word is not space-terminated, we add one trailing space |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
227 for (nword = (char*)word; *nword; nword++) |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
228 ; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
229 if (nword > word) nword--; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
230 if (*nword != ' ') { // Add a space |
1240
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
231 nword = g_strdup_printf("%s ", word); |
284
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
232 } else { // word is fine |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
233 nword = g_strdup(word); |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
234 } |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
235 |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
236 sl_elt = cat->words; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
237 while (sl_elt) { |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
238 if (!strcasecmp((char*)sl_elt->data, nword)) { |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
239 g_free(sl_elt->data); |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
240 cat->words = g_slist_delete_link(cat->words, sl_elt); |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
241 break; // Only remove first occurence |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
242 } |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
243 sl_elt = g_slist_next(sl_elt); |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
244 } |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
245 } |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
246 |
95 | 247 // compl_get_category_list() |
248 // Returns a slist of all words in the categories specified by the given flags | |
1076
b9698c89f46d
Fix memory leak in scr_handle_tab()
Mikael Berthe <mikael@lilotux.net>
parents:
894
diff
changeset
|
249 // Iff this function sets *dynlist to TRUE, then the caller must free the |
b9698c89f46d
Fix memory leak in scr_handle_tab()
Mikael Berthe <mikael@lilotux.net>
parents:
894
diff
changeset
|
250 // whole list after use. |
b9698c89f46d
Fix memory leak in scr_handle_tab()
Mikael Berthe <mikael@lilotux.net>
parents:
894
diff
changeset
|
251 GSList *compl_get_category_list(guint cat_flags, guint *dynlist) |
94 | 252 { |
253 GSList *sl_cat; | |
1076
b9698c89f46d
Fix memory leak in scr_handle_tab()
Mikael Berthe <mikael@lilotux.net>
parents:
894
diff
changeset
|
254 |
b9698c89f46d
Fix memory leak in scr_handle_tab()
Mikael Berthe <mikael@lilotux.net>
parents:
894
diff
changeset
|
255 *dynlist = FALSE; |
b9698c89f46d
Fix memory leak in scr_handle_tab()
Mikael Berthe <mikael@lilotux.net>
parents:
894
diff
changeset
|
256 |
94 | 257 // Look for category |
258 // XXX Actually that's not that simple... cat_flags can be a combination | |
259 // of several flags! | |
260 for (sl_cat=Categories; sl_cat; sl_cat = g_slist_next(sl_cat)) { | |
261 if (cat_flags == ((category*)sl_cat->data)->flag) | |
262 break; | |
263 } | |
264 if (sl_cat) // Category was found, easy... | |
265 return ((category*)sl_cat->data)->words; | |
266 | |
225 | 267 // Handle dynamic SLists |
1076
b9698c89f46d
Fix memory leak in scr_handle_tab()
Mikael Berthe <mikael@lilotux.net>
parents:
894
diff
changeset
|
268 *dynlist = TRUE; |
225 | 269 if (cat_flags == COMPL_GROUPNAME) { |
270 return compl_list(ROSTER_TYPE_GROUP); | |
271 } | |
272 if (cat_flags == COMPL_JID) { | |
273 return compl_list(ROSTER_TYPE_USER); | |
274 } | |
501
7c1ca00070e8
Add COMPL_RESOURCE (resource completion category)
Mikael Berthe <mikael@lilotux.net>
parents:
393
diff
changeset
|
275 if (cat_flags == COMPL_RESOURCE) { |
792
89ad7b530b3c
MUC: Fix completion for UTF-8 nicknames
Mikael Berthe <mikael@lilotux.net>
parents:
757
diff
changeset
|
276 return buddy_getresources_locale(NULL); |
501
7c1ca00070e8
Add COMPL_RESOURCE (resource completion category)
Mikael Berthe <mikael@lilotux.net>
parents:
393
diff
changeset
|
277 } |
757
ae23c8826efb
Improve completion for the "/event" command
Mikael Berthe <mikael@lilotux.net>
parents:
735
diff
changeset
|
278 if (cat_flags == COMPL_EVENTSID) { |
820
80bd7f49075f
Allow '*' in /event command
Mikael Berthe <mikael@lilotux.net>
parents:
792
diff
changeset
|
279 return evs_geteventslist(TRUE); |
757
ae23c8826efb
Improve completion for the "/event" command
Mikael Berthe <mikael@lilotux.net>
parents:
735
diff
changeset
|
280 } |
225 | 281 |
1076
b9698c89f46d
Fix memory leak in scr_handle_tab()
Mikael Berthe <mikael@lilotux.net>
parents:
894
diff
changeset
|
282 *dynlist = FALSE; |
94 | 283 return NULL; |
284 } | |
285 | |
580 | 286 /* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */ |