Mercurial > ~mikael > mcabber > hg
annotate mcabber/src/compl.c @ 1228:9a68fe4515dc
Improve MUC nickname completion
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Sun, 20 May 2007 22:01:20 +0200 |
parents | 2de8f8ba1f34 |
children | a54645448e00 |
rev | line source |
---|---|
94 | 1 /* |
2 * compl.c -- Completion system | |
393 | 3 * |
1205 | 4 * Copyright (C) 2005-2007 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" | |
225 | 35 #include "roster.h" |
757
ae23c8826efb
Improve completion for the "/event" command
Mikael Berthe <mikael@lilotux.net>
parents:
735
diff
changeset
|
36 #include "events.h" |
94 | 37 |
38 // Completion structure | |
39 typedef struct { | |
40 GSList *list; // list of matches | |
41 guint len_prefix; // length of text already typed by the user | |
42 guint len_compl; // length of the last completion | |
43 GSList *next; // pointer to next completion to try | |
44 } compl; | |
45 | |
46 // Category structure | |
47 typedef struct { | |
48 guint flag; | |
49 GSList *words; | |
50 } category; | |
51 | |
52 static GSList *Categories; | |
53 static compl *InputCompl; | |
54 | |
55 // new_completion(prefix, compl_cat) | |
56 // . prefix = beginning of the word, typed by the user | |
57 // . 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
|
58 // 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
|
59 // done_completion() must be called when finished. |
1228
9a68fe4515dc
Improve MUC nickname completion
Mikael Berthe <mikael@lilotux.net>
parents:
1205
diff
changeset
|
60 // Returns the number of possible completions. |
9a68fe4515dc
Improve MUC nickname completion
Mikael Berthe <mikael@lilotux.net>
parents:
1205
diff
changeset
|
61 guint new_completion(char *prefix, GSList *compl_cat) |
94 | 62 { |
63 compl *c; | |
64 GSList *sl_cat; | |
735 | 65 size_t len = strlen(prefix); |
94 | 66 |
67 if (InputCompl) { // This should not happen, but hey... | |
68 cancel_completion(); | |
69 } | |
70 | |
71 c = g_new0(compl, 1); | |
72 // Build the list of matches | |
1228
9a68fe4515dc
Improve MUC nickname completion
Mikael Berthe <mikael@lilotux.net>
parents:
1205
diff
changeset
|
73 for (sl_cat = compl_cat; sl_cat; sl_cat = g_slist_next(sl_cat)) { |
94 | 74 char *word = sl_cat->data; |
104 | 75 if (!strncasecmp(prefix, word, len)) { |
98 | 76 if (strlen(word) != len) |
77 c->list = g_slist_append(c->list, g_strdup(word+len)); // TODO sort | |
94 | 78 } |
79 } | |
80 c->next = c->list; | |
81 InputCompl = c; | |
1228
9a68fe4515dc
Improve MUC nickname completion
Mikael Berthe <mikael@lilotux.net>
parents:
1205
diff
changeset
|
82 return g_slist_length(c->list); |
94 | 83 } |
84 | |
85 // done_completion(); | |
86 void done_completion(void) | |
87 { | |
894
f76b32ff2f14
done_completion(): free all allocated memory
Mikael Berthe <mikael@lilotux.net>
parents:
820
diff
changeset
|
88 GSList *clp; |
f76b32ff2f14
done_completion(): free all allocated memory
Mikael Berthe <mikael@lilotux.net>
parents:
820
diff
changeset
|
89 |
98 | 90 if (!InputCompl) return; |
91 | |
894
f76b32ff2f14
done_completion(): free all allocated memory
Mikael Berthe <mikael@lilotux.net>
parents:
820
diff
changeset
|
92 // Free the current completion list |
f76b32ff2f14
done_completion(): free all allocated memory
Mikael Berthe <mikael@lilotux.net>
parents:
820
diff
changeset
|
93 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
|
94 g_free(clp->data); |
94 | 95 g_slist_free(InputCompl->list); |
96 g_free(InputCompl); | |
97 InputCompl = NULL; | |
98 } | |
99 | |
100 // cancel_completion() | |
101 // Returns the number of chars to delete to cancel the completion | |
102 //guint cancel_completion(compl *c) | |
103 guint cancel_completion(void) | |
104 { | |
98 | 105 if (!InputCompl) return 0; |
94 | 106 return InputCompl->len_compl; |
107 } | |
108 | |
109 // Returns pointer to text to insert, NULL if no completion. | |
110 const char *complete() | |
111 { | |
112 compl* c = InputCompl; | |
113 char *r; | |
98 | 114 |
115 if (!InputCompl) return NULL; | |
116 | |
94 | 117 if (!c->next) { |
118 c->next = c->list; // back to the beginning | |
119 c->len_compl = 0; | |
120 return NULL; | |
121 } | |
122 r = (char*)c->next->data; | |
123 c->next = g_slist_next(c->next); | |
124 c->len_compl = strlen(r); | |
125 return r; | |
126 } | |
127 | |
128 | |
129 /* Categories functions */ | |
130 | |
95 | 131 // compl_add_category_word(categ, command) |
132 // Adds a keyword as a possible completion in category categ. | |
133 void compl_add_category_word(guint categ, const char *word) | |
94 | 134 { |
135 GSList *sl_cat; | |
136 category *cat; | |
121 | 137 char *nword; |
94 | 138 // Look for category |
139 for (sl_cat=Categories; sl_cat; sl_cat = g_slist_next(sl_cat)) { | |
140 if (categ == ((category*)sl_cat->data)->flag) | |
141 break; | |
142 } | |
143 if (!sl_cat) { // Category not found, let's create it | |
144 cat = g_new0(category, 1); | |
145 cat->flag = categ; | |
146 Categories = g_slist_append(Categories, cat); | |
147 } else | |
148 cat = (category*)sl_cat->data; | |
149 | |
121 | 150 // If word is not space-terminated, we add one trailing space |
151 for (nword = (char*)word; *nword; nword++) | |
152 ; | |
153 if (nword > word) nword--; | |
154 if (*nword != ' ') { // Add a space | |
155 nword = g_new(char, strlen(word)+2); | |
156 strcpy(nword, word); | |
157 strcat(nword, " "); | |
158 } else { // word is fine | |
159 nword = g_strdup(word); | |
160 } | |
161 | |
94 | 162 // TODO Check word does not already exist |
121 | 163 cat->words = g_slist_append(cat->words, nword); // TODO sort |
94 | 164 } |
165 | |
284
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
166 // compl_del_category_word(categ, command) |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
167 // Removes a keyword from category categ in completion list. |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
168 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
|
169 { |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
170 GSList *sl_cat, *sl_elt; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
171 category *cat; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
172 char *nword; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
173 // Look for category |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
174 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
|
175 if (categ == ((category*)sl_cat->data)->flag) |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
176 break; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
177 } |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
178 if (!sl_cat) return; // Category not found, finished! |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
179 |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
180 cat = (category*)sl_cat->data; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
181 |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
182 // 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
|
183 for (nword = (char*)word; *nword; nword++) |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
184 ; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
185 if (nword > word) nword--; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
186 if (*nword != ' ') { // Add a space |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
187 nword = g_new(char, strlen(word)+2); |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
188 strcpy(nword, word); |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
189 strcat(nword, " "); |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
190 } else { // word is fine |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
191 nword = g_strdup(word); |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
192 } |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
193 |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
194 sl_elt = cat->words; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
195 while (sl_elt) { |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
196 if (!strcasecmp((char*)sl_elt->data, nword)) { |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
197 g_free(sl_elt->data); |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
198 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
|
199 break; // Only remove first occurence |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
200 } |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
201 sl_elt = g_slist_next(sl_elt); |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
202 } |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
203 } |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
204 |
95 | 205 // compl_get_category_list() |
206 // 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
|
207 // 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
|
208 // whole list after use. |
b9698c89f46d
Fix memory leak in scr_handle_tab()
Mikael Berthe <mikael@lilotux.net>
parents:
894
diff
changeset
|
209 GSList *compl_get_category_list(guint cat_flags, guint *dynlist) |
94 | 210 { |
211 GSList *sl_cat; | |
1076
b9698c89f46d
Fix memory leak in scr_handle_tab()
Mikael Berthe <mikael@lilotux.net>
parents:
894
diff
changeset
|
212 |
b9698c89f46d
Fix memory leak in scr_handle_tab()
Mikael Berthe <mikael@lilotux.net>
parents:
894
diff
changeset
|
213 *dynlist = FALSE; |
b9698c89f46d
Fix memory leak in scr_handle_tab()
Mikael Berthe <mikael@lilotux.net>
parents:
894
diff
changeset
|
214 |
94 | 215 // Look for category |
216 // XXX Actually that's not that simple... cat_flags can be a combination | |
217 // of several flags! | |
218 for (sl_cat=Categories; sl_cat; sl_cat = g_slist_next(sl_cat)) { | |
219 if (cat_flags == ((category*)sl_cat->data)->flag) | |
220 break; | |
221 } | |
222 if (sl_cat) // Category was found, easy... | |
223 return ((category*)sl_cat->data)->words; | |
224 | |
225 | 225 // Handle dynamic SLists |
1076
b9698c89f46d
Fix memory leak in scr_handle_tab()
Mikael Berthe <mikael@lilotux.net>
parents:
894
diff
changeset
|
226 *dynlist = TRUE; |
225 | 227 if (cat_flags == COMPL_GROUPNAME) { |
228 return compl_list(ROSTER_TYPE_GROUP); | |
229 } | |
230 if (cat_flags == COMPL_JID) { | |
231 return compl_list(ROSTER_TYPE_USER); | |
232 } | |
501
7c1ca00070e8
Add COMPL_RESOURCE (resource completion category)
Mikael Berthe <mikael@lilotux.net>
parents:
393
diff
changeset
|
233 if (cat_flags == COMPL_RESOURCE) { |
792
89ad7b530b3c
MUC: Fix completion for UTF-8 nicknames
Mikael Berthe <mikael@lilotux.net>
parents:
757
diff
changeset
|
234 return buddy_getresources_locale(NULL); |
501
7c1ca00070e8
Add COMPL_RESOURCE (resource completion category)
Mikael Berthe <mikael@lilotux.net>
parents:
393
diff
changeset
|
235 } |
757
ae23c8826efb
Improve completion for the "/event" command
Mikael Berthe <mikael@lilotux.net>
parents:
735
diff
changeset
|
236 if (cat_flags == COMPL_EVENTSID) { |
820
80bd7f49075f
Allow '*' in /event command
Mikael Berthe <mikael@lilotux.net>
parents:
792
diff
changeset
|
237 return evs_geteventslist(TRUE); |
757
ae23c8826efb
Improve completion for the "/event" command
Mikael Berthe <mikael@lilotux.net>
parents:
735
diff
changeset
|
238 } |
225 | 239 |
1076
b9698c89f46d
Fix memory leak in scr_handle_tab()
Mikael Berthe <mikael@lilotux.net>
parents:
894
diff
changeset
|
240 *dynlist = FALSE; |
94 | 241 return NULL; |
242 } | |
243 | |
580 | 244 /* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */ |