Mercurial > ~mikael > mcabber > hg
comparison mcabber/mcabber/compl.c @ 1668:41c26b7d2890
Install mcabber headers
* Change mcabber headers naming scheme
* Move 'src/' -> 'mcabber/'
* Add missing include <mcabber/config.h>'s
* Create and install clean config.h version in 'include/'
* Move "dirty" config.h version to 'mcabber/'
* Add $(top_srcdir) to compiler include path
* Update modules HOWTO
author | Myhailo Danylenko <isbear@ukrpost.net> |
---|---|
date | Mon, 18 Jan 2010 15:36:19 +0200 |
parents | mcabber/src/compl.c@14690e624e9d |
children | 1342df44c814 |
comparison
equal
deleted
inserted
replaced
1667:8af0e0ad20ad | 1668:41c26b7d2890 |
---|---|
1 /* | |
2 * compl.c -- Completion system | |
3 * | |
4 * Copyright (C) 2005-2009 Mikael Berthe <mikael@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 #include "utf8.h" | |
36 #include "roster.h" | |
37 #include "events.h" | |
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 | |
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 // 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) | |
97 // Set the InputCompl pointer to an allocated compl structure. | |
98 // done_completion() must be called when finished. | |
99 // Returns the number of possible completions. | |
100 guint new_completion(char *prefix, GSList *compl_cat) | |
101 { | |
102 compl *c; | |
103 GSList *sl_cat; | |
104 size_t len = strlen(prefix); | |
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 | |
112 for (sl_cat = compl_cat; sl_cat; sl_cat = g_slist_next(sl_cat)) { | |
113 char *word = sl_cat->data; | |
114 if (!strncasecmp(prefix, word, len)) { | |
115 if (strlen(word) != len) | |
116 c->list = g_slist_append(c->list, g_strdup(word+len)); // TODO sort | |
117 } | |
118 } | |
119 c->next = c->list; | |
120 InputCompl = c; | |
121 return g_slist_length(c->list); | |
122 } | |
123 | |
124 // done_completion(); | |
125 void done_completion(void) | |
126 { | |
127 GSList *clp; | |
128 | |
129 if (!InputCompl) return; | |
130 | |
131 // Free the current completion list | |
132 for (clp = InputCompl->list; clp; clp = g_slist_next(clp)) | |
133 g_free(clp->data); | |
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 { | |
144 if (!InputCompl) return 0; | |
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; | |
153 | |
154 if (!InputCompl) return NULL; | |
155 | |
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); | |
163 if (!utf8_mode) { | |
164 c->len_compl = strlen(r); | |
165 } else { | |
166 char *wc; | |
167 c->len_compl = 0; | |
168 for (wc = r; *wc; wc = next_char(wc)) | |
169 c->len_compl++; | |
170 } | |
171 return r; | |
172 } | |
173 | |
174 | |
175 /* Categories functions */ | |
176 | |
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) | |
180 { | |
181 GSList *sl_cat; | |
182 category *cat; | |
183 char *nword; | |
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 | |
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 | |
201 nword = g_strdup_printf("%s ", word); | |
202 } else { // word is fine | |
203 nword = g_strdup(word); | |
204 } | |
205 | |
206 // TODO Check word does not already exist | |
207 cat->words = g_slist_append(cat->words, nword); // TODO sort | |
208 } | |
209 | |
210 // compl_del_category_word(categ, command) | |
211 // Removes a keyword from category categ in completion list. | |
212 void compl_del_category_word(guint categ, const char *word) | |
213 { | |
214 GSList *sl_cat, *sl_elt; | |
215 category *cat; | |
216 char *nword; | |
217 // Look for category | |
218 for (sl_cat=Categories; sl_cat; sl_cat = g_slist_next(sl_cat)) { | |
219 if (categ == ((category*)sl_cat->data)->flag) | |
220 break; | |
221 } | |
222 if (!sl_cat) return; // Category not found, finished! | |
223 | |
224 cat = (category*)sl_cat->data; | |
225 | |
226 // If word is not space-terminated, we add one trailing space | |
227 for (nword = (char*)word; *nword; nword++) | |
228 ; | |
229 if (nword > word) nword--; | |
230 if (*nword != ' ') { // Add a space | |
231 nword = g_strdup_printf("%s ", word); | |
232 } else { // word is fine | |
233 nword = g_strdup(word); | |
234 } | |
235 | |
236 sl_elt = cat->words; | |
237 while (sl_elt) { | |
238 if (!strcasecmp((char*)sl_elt->data, nword)) { | |
239 g_free(sl_elt->data); | |
240 cat->words = g_slist_delete_link(cat->words, sl_elt); | |
241 break; // Only remove first occurence | |
242 } | |
243 sl_elt = g_slist_next(sl_elt); | |
244 } | |
245 } | |
246 | |
247 // compl_get_category_list() | |
248 // Returns a slist of all words in the categories specified by the given flags | |
249 // Iff this function sets *dynlist to TRUE, then the caller must free the | |
250 // whole list after use. | |
251 GSList *compl_get_category_list(guint cat_flags, guint *dynlist) | |
252 { | |
253 GSList *sl_cat; | |
254 | |
255 *dynlist = FALSE; | |
256 | |
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 | |
267 // Handle dynamic SLists | |
268 *dynlist = TRUE; | |
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 } | |
275 if (cat_flags == COMPL_RESOURCE) { | |
276 return buddy_getresources_locale(NULL); | |
277 } | |
278 if (cat_flags == COMPL_EVENTSID) { | |
279 return evs_geteventslist(TRUE); | |
280 } | |
281 | |
282 *dynlist = FALSE; | |
283 return NULL; | |
284 } | |
285 | |
286 /* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */ |