Mercurial > ~mikael > mcabber > hg
annotate mcabber/mcabber/compl.c @ 1729:e6e89b1d7831
Minor style and header updates
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Sun, 28 Feb 2010 15:19:27 +0100 |
parents | 1342df44c814 |
children | e2c084204583 |
rev | line source |
---|---|
94 | 1 /* |
2 * compl.c -- Completion system | |
393 | 3 * |
1729
e6e89b1d7831
Minor style and header updates
Mikael Berthe <mikael@lilotux.net>
parents:
1685
diff
changeset
|
4 * Copyright (C) 2005-2010 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(void) | |
142 { | |
98 | 143 if (!InputCompl) return 0; |
94 | 144 return InputCompl->len_compl; |
145 } | |
146 | |
147 // Returns pointer to text to insert, NULL if no completion. | |
148 const char *complete() | |
149 { | |
150 compl* c = InputCompl; | |
151 char *r; | |
98 | 152 |
153 if (!InputCompl) return NULL; | |
154 | |
94 | 155 if (!c->next) { |
156 c->next = c->list; // back to the beginning | |
157 c->len_compl = 0; | |
158 return NULL; | |
159 } | |
160 r = (char*)c->next->data; | |
161 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
|
162 if (!utf8_mode) { |
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
163 c->len_compl = strlen(r); |
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
164 } else { |
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
165 char *wc; |
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
166 c->len_compl = 0; |
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
167 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
|
168 c->len_compl++; |
1240
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
169 } |
94 | 170 return r; |
171 } | |
172 | |
173 | |
174 /* Categories functions */ | |
175 | |
95 | 176 // compl_add_category_word(categ, command) |
177 // Adds a keyword as a possible completion in category categ. | |
178 void compl_add_category_word(guint categ, const char *word) | |
94 | 179 { |
180 GSList *sl_cat; | |
181 category *cat; | |
121 | 182 char *nword; |
94 | 183 // Look for category |
184 for (sl_cat=Categories; sl_cat; sl_cat = g_slist_next(sl_cat)) { | |
185 if (categ == ((category*)sl_cat->data)->flag) | |
186 break; | |
187 } | |
188 if (!sl_cat) { // Category not found, let's create it | |
189 cat = g_new0(category, 1); | |
190 cat->flag = categ; | |
191 Categories = g_slist_append(Categories, cat); | |
192 } else | |
193 cat = (category*)sl_cat->data; | |
194 | |
121 | 195 // If word is not space-terminated, we add one trailing space |
196 for (nword = (char*)word; *nword; nword++) | |
197 ; | |
198 if (nword > word) nword--; | |
199 if (*nword != ' ') { // Add a space | |
1240
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
200 nword = g_strdup_printf("%s ", word); |
121 | 201 } else { // word is fine |
202 nword = g_strdup(word); | |
203 } | |
204 | |
94 | 205 // TODO Check word does not already exist |
121 | 206 cat->words = g_slist_append(cat->words, nword); // TODO sort |
94 | 207 } |
208 | |
284
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
209 // compl_del_category_word(categ, command) |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
210 // Removes a keyword from category categ in completion list. |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
211 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
|
212 { |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
213 GSList *sl_cat, *sl_elt; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
214 category *cat; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
215 char *nword; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
216 // Look for category |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
217 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
|
218 if (categ == ((category*)sl_cat->data)->flag) |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
219 break; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
220 } |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
221 if (!sl_cat) return; // Category not found, finished! |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
222 |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
223 cat = (category*)sl_cat->data; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
224 |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
225 // 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
|
226 for (nword = (char*)word; *nword; nword++) |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
227 ; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
228 if (nword > word) nword--; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
229 if (*nword != ' ') { // Add a space |
1240
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
230 nword = g_strdup_printf("%s ", word); |
284
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
231 } else { // word is fine |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
232 nword = g_strdup(word); |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
233 } |
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 sl_elt = cat->words; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
236 while (sl_elt) { |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
237 if (!strcasecmp((char*)sl_elt->data, nword)) { |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
238 g_free(sl_elt->data); |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
239 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
|
240 break; // Only remove first occurence |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
241 } |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
242 sl_elt = g_slist_next(sl_elt); |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
243 } |
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 |
95 | 246 // compl_get_category_list() |
247 // 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
|
248 // 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
|
249 // whole list after use. |
b9698c89f46d
Fix memory leak in scr_handle_tab()
Mikael Berthe <mikael@lilotux.net>
parents:
894
diff
changeset
|
250 GSList *compl_get_category_list(guint cat_flags, guint *dynlist) |
94 | 251 { |
252 GSList *sl_cat; | |
1076
b9698c89f46d
Fix memory leak in scr_handle_tab()
Mikael Berthe <mikael@lilotux.net>
parents:
894
diff
changeset
|
253 |
b9698c89f46d
Fix memory leak in scr_handle_tab()
Mikael Berthe <mikael@lilotux.net>
parents:
894
diff
changeset
|
254 *dynlist = FALSE; |
b9698c89f46d
Fix memory leak in scr_handle_tab()
Mikael Berthe <mikael@lilotux.net>
parents:
894
diff
changeset
|
255 |
94 | 256 // Look for category |
257 // XXX Actually that's not that simple... cat_flags can be a combination | |
258 // of several flags! | |
259 for (sl_cat=Categories; sl_cat; sl_cat = g_slist_next(sl_cat)) { | |
260 if (cat_flags == ((category*)sl_cat->data)->flag) | |
261 break; | |
262 } | |
263 if (sl_cat) // Category was found, easy... | |
264 return ((category*)sl_cat->data)->words; | |
265 | |
225 | 266 // Handle dynamic SLists |
1076
b9698c89f46d
Fix memory leak in scr_handle_tab()
Mikael Berthe <mikael@lilotux.net>
parents:
894
diff
changeset
|
267 *dynlist = TRUE; |
225 | 268 if (cat_flags == COMPL_GROUPNAME) { |
269 return compl_list(ROSTER_TYPE_GROUP); | |
270 } | |
271 if (cat_flags == COMPL_JID) { | |
272 return compl_list(ROSTER_TYPE_USER); | |
273 } | |
501
7c1ca00070e8
Add COMPL_RESOURCE (resource completion category)
Mikael Berthe <mikael@lilotux.net>
parents:
393
diff
changeset
|
274 if (cat_flags == COMPL_RESOURCE) { |
792
89ad7b530b3c
MUC: Fix completion for UTF-8 nicknames
Mikael Berthe <mikael@lilotux.net>
parents:
757
diff
changeset
|
275 return buddy_getresources_locale(NULL); |
501
7c1ca00070e8
Add COMPL_RESOURCE (resource completion category)
Mikael Berthe <mikael@lilotux.net>
parents:
393
diff
changeset
|
276 } |
757
ae23c8826efb
Improve completion for the "/event" command
Mikael Berthe <mikael@lilotux.net>
parents:
735
diff
changeset
|
277 if (cat_flags == COMPL_EVENTSID) { |
1685
1342df44c814
Improved events interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1668
diff
changeset
|
278 GSList *compl = evs_geteventslist(); |
1342df44c814
Improved events interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1668
diff
changeset
|
279 GSList *cel; |
1342df44c814
Improved events interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1668
diff
changeset
|
280 for (cel = compl; cel; cel = cel->next) |
1342df44c814
Improved events interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1668
diff
changeset
|
281 cel->data = g_strdup(cel->data); |
1342df44c814
Improved events interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1668
diff
changeset
|
282 compl = g_slist_append(compl, g_strdup("list")); |
1342df44c814
Improved events interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1668
diff
changeset
|
283 return compl; |
757
ae23c8826efb
Improve completion for the "/event" command
Mikael Berthe <mikael@lilotux.net>
parents:
735
diff
changeset
|
284 } |
225 | 285 |
1076
b9698c89f46d
Fix memory leak in scr_handle_tab()
Mikael Berthe <mikael@lilotux.net>
parents:
894
diff
changeset
|
286 *dynlist = FALSE; |
94 | 287 return NULL; |
288 } | |
289 | |
580 | 290 /* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */ |