Mercurial > ~mikael > mcabber > hg
annotate mcabber/mcabber/compl.c @ 1922:4ba68ad737bc
Increase the number of available categories for completions
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Wed, 10 Nov 2010 14:08:24 +0100 |
parents | 057b514b1f12 |
children | 924f4552996c |
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> |
1828
8f7d7c05f275
Small coding style update
Mikael Berthe <mikael@lilotux.net>
parents:
1811
diff
changeset
|
5 * Copyright (C) 2009,2010 Myhailo Danylenko <isbear@ukrpost.net> |
94 | 6 * |
7 * This program is free software; you can redistribute it and/or modify | |
8 * it under the terms of the GNU General Public License as published by | |
9 * the Free Software Foundation; either version 2 of the License, or (at | |
10 * your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU General Public License | |
18 * along with this program; if not, write to the Free Software | |
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
20 * USA | |
21 */ | |
22 | |
23 /* Usage, basically: | |
24 * - new_completion(); // 1. Initialization | |
25 * - complete(); // 2. 1st completion | |
26 * - cancel_completion(); // 3a. 2nd completion / cancel previous | |
27 * - complete(); // 3b. 2nd completion / complete | |
28 * ... | |
29 * - done_completion(); // n. finished -- free allocated areas | |
30 * | |
31 */ | |
32 | |
33 #include <string.h> | |
34 | |
35 #include "compl.h" | |
1240
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
36 #include "utf8.h" |
225 | 37 #include "roster.h" |
757
ae23c8826efb
Improve completion for the "/event" command
Mikael Berthe <mikael@lilotux.net>
parents:
735
diff
changeset
|
38 #include "events.h" |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
39 #include "logprint.h" |
94 | 40 |
41 // Completion structure | |
42 typedef struct { | |
43 GSList *list; // list of matches | |
44 guint len_prefix; // length of text already typed by the user | |
45 guint len_compl; // length of the last completion | |
46 GSList *next; // pointer to next completion to try | |
47 } compl; | |
48 | |
49 // Category structure | |
50 typedef struct { | |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
51 guint64 flag; |
94 | 52 GSList *words; |
53 } category; | |
54 | |
55 static GSList *Categories; | |
56 static compl *InputCompl; | |
57 | |
1607 | 58 #ifdef MODULES_ENABLE |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
59 static guint64 registered_cats; |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
60 |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
61 static inline void register_builtin_cat(guint c) { |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
62 registered_cats |= 1UL << (c-1); |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
63 } |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
64 |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
65 void compl_init_system(void) |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
66 { |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
67 // Builtin completion categories: |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
68 register_builtin_cat(COMPL_CMD); |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
69 register_builtin_cat(COMPL_JID); |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
70 register_builtin_cat(COMPL_URLJID); |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
71 register_builtin_cat(COMPL_NAME); |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
72 register_builtin_cat(COMPL_STATUS); |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
73 register_builtin_cat(COMPL_FILENAME); |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
74 register_builtin_cat(COMPL_ROSTER); |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
75 register_builtin_cat(COMPL_BUFFER); |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
76 register_builtin_cat(COMPL_GROUP); |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
77 register_builtin_cat(COMPL_GROUPNAME); |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
78 register_builtin_cat(COMPL_MULTILINE); |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
79 register_builtin_cat(COMPL_ROOM); |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
80 register_builtin_cat(COMPL_RESOURCE); |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
81 register_builtin_cat(COMPL_AUTH); |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
82 register_builtin_cat(COMPL_REQUEST); |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
83 register_builtin_cat(COMPL_EVENTS); |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
84 register_builtin_cat(COMPL_EVENTSID); |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
85 register_builtin_cat(COMPL_PGP); |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
86 register_builtin_cat(COMPL_COLOR); |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
87 register_builtin_cat(COMPL_OTR); |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
88 register_builtin_cat(COMPL_OTRPOLICY); |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
89 } |
1607 | 90 |
91 // compl_new_category() | |
92 // Reserves id for new completion category. | |
93 // Returns 0, if no more categories can be allocated. | |
94 // Note, that user should not make any assumptions about id nature, | |
95 // as it is likely to change in future. | |
1828
8f7d7c05f275
Small coding style update
Mikael Berthe <mikael@lilotux.net>
parents:
1811
diff
changeset
|
96 guint compl_new_category(void) |
1607 | 97 { |
98 guint i = 0; | |
99 while ((registered_cats >> i) & 1) | |
100 i++; | |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
101 if (i >= 8 * sizeof (registered_cats)) |
1607 | 102 return 0; |
103 else { | |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
104 guint64 id = 1 << i; |
1607 | 105 registered_cats |= id; |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
106 return i+1; |
1607 | 107 } |
108 } | |
109 | |
1828
8f7d7c05f275
Small coding style update
Mikael Berthe <mikael@lilotux.net>
parents:
1811
diff
changeset
|
110 // compl_del_category(id) |
1607 | 111 // Frees reserved id for category. |
112 // Note, that for now it not validates its input, so, be careful | |
113 // and specify exactly what you get from compl_new_category. | |
1828
8f7d7c05f275
Small coding style update
Mikael Berthe <mikael@lilotux.net>
parents:
1811
diff
changeset
|
114 void compl_del_category(guint id) |
1607 | 115 { |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
116 if (!id) { |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
117 scr_log_print(LPRINT_LOGNORM, "Error: compl_del_category() - " |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
118 "Invalid category."); |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
119 return; |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
120 } |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
121 id--; |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
122 registered_cats &= ~(1<<id); |
1607 | 123 } |
124 #endif | |
125 | |
1756
e2c084204583
Add (optional) suffix after nick completion in Multi-User Chats
Mikael Berthe <mikael@lilotux.net>
parents:
1729
diff
changeset
|
126 // new_completion(prefix, compl_cat, suffix) |
94 | 127 // . prefix = beginning of the word, typed by the user |
128 // . compl_cat = pointer to a completion category list (list of *char) | |
1756
e2c084204583
Add (optional) suffix after nick completion in Multi-User Chats
Mikael Berthe <mikael@lilotux.net>
parents:
1729
diff
changeset
|
129 // . suffix = string to append to all completion possibilities (i.e. ":") |
894
f76b32ff2f14
done_completion(): free all allocated memory
Mikael Berthe <mikael@lilotux.net>
parents:
820
diff
changeset
|
130 // 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
|
131 // done_completion() must be called when finished. |
1228
9a68fe4515dc
Improve MUC nickname completion
Mikael Berthe <mikael@lilotux.net>
parents:
1205
diff
changeset
|
132 // Returns the number of possible completions. |
1756
e2c084204583
Add (optional) suffix after nick completion in Multi-User Chats
Mikael Berthe <mikael@lilotux.net>
parents:
1729
diff
changeset
|
133 guint new_completion(const char *prefix, GSList *compl_cat, const gchar *suffix) |
94 | 134 { |
135 compl *c; | |
136 GSList *sl_cat; | |
735 | 137 size_t len = strlen(prefix); |
94 | 138 |
139 if (InputCompl) { // This should not happen, but hey... | |
140 cancel_completion(); | |
141 } | |
142 | |
143 c = g_new0(compl, 1); | |
144 // Build the list of matches | |
1228
9a68fe4515dc
Improve MUC nickname completion
Mikael Berthe <mikael@lilotux.net>
parents:
1205
diff
changeset
|
145 for (sl_cat = compl_cat; sl_cat; sl_cat = g_slist_next(sl_cat)) { |
94 | 146 char *word = sl_cat->data; |
104 | 147 if (!strncasecmp(prefix, word, len)) { |
1756
e2c084204583
Add (optional) suffix after nick completion in Multi-User Chats
Mikael Berthe <mikael@lilotux.net>
parents:
1729
diff
changeset
|
148 if (strlen(word) != len) { |
e2c084204583
Add (optional) suffix after nick completion in Multi-User Chats
Mikael Berthe <mikael@lilotux.net>
parents:
1729
diff
changeset
|
149 gchar *compval; |
e2c084204583
Add (optional) suffix after nick completion in Multi-User Chats
Mikael Berthe <mikael@lilotux.net>
parents:
1729
diff
changeset
|
150 if (suffix) |
e2c084204583
Add (optional) suffix after nick completion in Multi-User Chats
Mikael Berthe <mikael@lilotux.net>
parents:
1729
diff
changeset
|
151 compval = g_strdup_printf("%s%s", word+len, suffix); |
e2c084204583
Add (optional) suffix after nick completion in Multi-User Chats
Mikael Berthe <mikael@lilotux.net>
parents:
1729
diff
changeset
|
152 else |
e2c084204583
Add (optional) suffix after nick completion in Multi-User Chats
Mikael Berthe <mikael@lilotux.net>
parents:
1729
diff
changeset
|
153 compval = g_strdup(word+len); |
1852 | 154 c->list = g_slist_insert_sorted(c->list, compval, |
155 (GCompareFunc)g_ascii_strcasecmp); | |
1756
e2c084204583
Add (optional) suffix after nick completion in Multi-User Chats
Mikael Berthe <mikael@lilotux.net>
parents:
1729
diff
changeset
|
156 } |
94 | 157 } |
158 } | |
159 c->next = c->list; | |
160 InputCompl = c; | |
1228
9a68fe4515dc
Improve MUC nickname completion
Mikael Berthe <mikael@lilotux.net>
parents:
1205
diff
changeset
|
161 return g_slist_length(c->list); |
94 | 162 } |
163 | |
164 // done_completion(); | |
165 void done_completion(void) | |
166 { | |
894
f76b32ff2f14
done_completion(): free all allocated memory
Mikael Berthe <mikael@lilotux.net>
parents:
820
diff
changeset
|
167 GSList *clp; |
f76b32ff2f14
done_completion(): free all allocated memory
Mikael Berthe <mikael@lilotux.net>
parents:
820
diff
changeset
|
168 |
98 | 169 if (!InputCompl) return; |
170 | |
894
f76b32ff2f14
done_completion(): free all allocated memory
Mikael Berthe <mikael@lilotux.net>
parents:
820
diff
changeset
|
171 // Free the current completion list |
f76b32ff2f14
done_completion(): free all allocated memory
Mikael Berthe <mikael@lilotux.net>
parents:
820
diff
changeset
|
172 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
|
173 g_free(clp->data); |
94 | 174 g_slist_free(InputCompl->list); |
175 g_free(InputCompl); | |
176 InputCompl = NULL; | |
177 } | |
178 | |
179 // cancel_completion() | |
180 // Returns the number of chars to delete to cancel the completion | |
181 guint cancel_completion(void) | |
182 { | |
98 | 183 if (!InputCompl) return 0; |
94 | 184 return InputCompl->len_compl; |
185 } | |
186 | |
187 // Returns pointer to text to insert, NULL if no completion. | |
188 const char *complete() | |
189 { | |
190 compl* c = InputCompl; | |
191 char *r; | |
98 | 192 |
193 if (!InputCompl) return NULL; | |
194 | |
94 | 195 if (!c->next) { |
196 c->next = c->list; // back to the beginning | |
197 c->len_compl = 0; | |
198 return NULL; | |
199 } | |
200 r = (char*)c->next->data; | |
201 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
|
202 if (!utf8_mode) { |
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
203 c->len_compl = strlen(r); |
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
204 } else { |
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
205 char *wc; |
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
206 c->len_compl = 0; |
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
207 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
|
208 c->len_compl++; |
1240
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
209 } |
94 | 210 return r; |
211 } | |
212 | |
213 | |
214 /* Categories functions */ | |
215 | |
95 | 216 // compl_add_category_word(categ, command) |
217 // Adds a keyword as a possible completion in category categ. | |
1756
e2c084204583
Add (optional) suffix after nick completion in Multi-User Chats
Mikael Berthe <mikael@lilotux.net>
parents:
1729
diff
changeset
|
218 void compl_add_category_word(guint categ, const gchar *word) |
94 | 219 { |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
220 guint64 catv; |
94 | 221 GSList *sl_cat; |
222 category *cat; | |
121 | 223 char *nword; |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
224 |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
225 if (!categ) { |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
226 scr_log_print(LPRINT_LOGNORM, "Error: compl_add_category_word() - " |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
227 "Invalid category."); |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
228 return; |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
229 } |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
230 |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
231 categ--; |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
232 catv = 1UL << categ; |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
233 |
94 | 234 // Look for category |
235 for (sl_cat=Categories; sl_cat; sl_cat = g_slist_next(sl_cat)) { | |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
236 if (catv == ((category*)sl_cat->data)->flag) |
94 | 237 break; |
238 } | |
239 if (!sl_cat) { // Category not found, let's create it | |
240 cat = g_new0(category, 1); | |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
241 cat->flag = catv; |
94 | 242 Categories = g_slist_append(Categories, cat); |
243 } else | |
244 cat = (category*)sl_cat->data; | |
245 | |
121 | 246 // If word is not space-terminated, we add one trailing space |
247 for (nword = (char*)word; *nword; nword++) | |
248 ; | |
249 if (nword > word) nword--; | |
250 if (*nword != ' ') { // Add a space | |
1240
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
251 nword = g_strdup_printf("%s ", word); |
121 | 252 } else { // word is fine |
253 nword = g_strdup(word); | |
254 } | |
255 | |
1852 | 256 if (g_slist_find_custom(cat->words, nword, (GCompareFunc)g_strcmp0) != NULL) |
1850
8d7810f529db
Sort items in completion lists
alkino <nicolas.cornu@ensi-bourges.fr>
parents:
1828
diff
changeset
|
257 return; |
8d7810f529db
Sort items in completion lists
alkino <nicolas.cornu@ensi-bourges.fr>
parents:
1828
diff
changeset
|
258 |
1852 | 259 cat->words = g_slist_insert_sorted(cat->words, nword, |
260 (GCompareFunc)g_ascii_strcasecmp); | |
94 | 261 } |
262 | |
284
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
263 // compl_del_category_word(categ, command) |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
264 // Removes a keyword from category categ in completion list. |
1756
e2c084204583
Add (optional) suffix after nick completion in Multi-User Chats
Mikael Berthe <mikael@lilotux.net>
parents:
1729
diff
changeset
|
265 void compl_del_category_word(guint categ, const gchar *word) |
284
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
266 { |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
267 guint64 catv; |
284
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
268 GSList *sl_cat, *sl_elt; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
269 category *cat; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
270 char *nword; |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
271 |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
272 if (!categ) { |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
273 scr_log_print(LPRINT_LOGNORM, "Error: compl_del_category_word() - " |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
274 "Invalid category."); |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
275 return; |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
276 } |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
277 |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
278 categ--; |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
279 catv = 1UL << categ; |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
280 |
284
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
281 // Look for category |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
282 for (sl_cat=Categories; sl_cat; sl_cat = g_slist_next(sl_cat)) { |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
283 if (catv == ((category*)sl_cat->data)->flag) |
284
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
284 break; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
285 } |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
286 if (!sl_cat) return; // Category not found, finished! |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
287 |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
288 cat = (category*)sl_cat->data; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
289 |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
290 // 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
|
291 for (nword = (char*)word; *nword; nword++) |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
292 ; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
293 if (nword > word) nword--; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
294 if (*nword != ' ') { // Add a space |
1240
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
295 nword = g_strdup_printf("%s ", word); |
284
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
296 } else { // word is fine |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
297 nword = g_strdup(word); |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
298 } |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
299 |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
300 sl_elt = cat->words; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
301 while (sl_elt) { |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
302 if (!strcasecmp((char*)sl_elt->data, nword)) { |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
303 g_free(sl_elt->data); |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
304 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
|
305 break; // Only remove first occurence |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
306 } |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
307 sl_elt = g_slist_next(sl_elt); |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
308 } |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
309 } |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
310 |
95 | 311 // compl_get_category_list() |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
312 // Returns a slist of all words in the specified categorie. |
1076
b9698c89f46d
Fix memory leak in scr_handle_tab()
Mikael Berthe <mikael@lilotux.net>
parents:
894
diff
changeset
|
313 // 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
|
314 // whole list after use. |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
315 GSList *compl_get_category_list(guint categ, guint *dynlist) |
94 | 316 { |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
317 guint64 cat_flags; |
94 | 318 GSList *sl_cat; |
1076
b9698c89f46d
Fix memory leak in scr_handle_tab()
Mikael Berthe <mikael@lilotux.net>
parents:
894
diff
changeset
|
319 |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
320 if (!categ) { |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
321 scr_log_print(LPRINT_LOGNORM, "Error: compl_get_category_list() - " |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
322 "Invalid category."); |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
323 return NULL; |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
324 } |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
325 |
1076
b9698c89f46d
Fix memory leak in scr_handle_tab()
Mikael Berthe <mikael@lilotux.net>
parents:
894
diff
changeset
|
326 *dynlist = FALSE; |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
327 cat_flags = 1UL << (categ - 1); |
1076
b9698c89f46d
Fix memory leak in scr_handle_tab()
Mikael Berthe <mikael@lilotux.net>
parents:
894
diff
changeset
|
328 |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
329 // Look for the category |
94 | 330 for (sl_cat=Categories; sl_cat; sl_cat = g_slist_next(sl_cat)) { |
331 if (cat_flags == ((category*)sl_cat->data)->flag) | |
332 break; | |
333 } | |
334 if (sl_cat) // Category was found, easy... | |
335 return ((category*)sl_cat->data)->words; | |
336 | |
225 | 337 // Handle dynamic SLists |
1076
b9698c89f46d
Fix memory leak in scr_handle_tab()
Mikael Berthe <mikael@lilotux.net>
parents:
894
diff
changeset
|
338 *dynlist = TRUE; |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
339 if (categ == COMPL_GROUPNAME) { |
225 | 340 return compl_list(ROSTER_TYPE_GROUP); |
341 } | |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
342 if (categ == COMPL_JID) { |
225 | 343 return compl_list(ROSTER_TYPE_USER); |
344 } | |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
345 if (categ == COMPL_RESOURCE) { |
792
89ad7b530b3c
MUC: Fix completion for UTF-8 nicknames
Mikael Berthe <mikael@lilotux.net>
parents:
757
diff
changeset
|
346 return buddy_getresources_locale(NULL); |
501
7c1ca00070e8
Add COMPL_RESOURCE (resource completion category)
Mikael Berthe <mikael@lilotux.net>
parents:
393
diff
changeset
|
347 } |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
348 if (categ == COMPL_EVENTSID) { |
1685
1342df44c814
Improved events interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1668
diff
changeset
|
349 GSList *compl = evs_geteventslist(); |
1342df44c814
Improved events interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1668
diff
changeset
|
350 GSList *cel; |
1342df44c814
Improved events interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1668
diff
changeset
|
351 for (cel = compl; cel; cel = cel->next) |
1342df44c814
Improved events interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1668
diff
changeset
|
352 cel->data = g_strdup(cel->data); |
1342df44c814
Improved events interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1668
diff
changeset
|
353 compl = g_slist_append(compl, g_strdup("list")); |
1342df44c814
Improved events interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1668
diff
changeset
|
354 return compl; |
757
ae23c8826efb
Improve completion for the "/event" command
Mikael Berthe <mikael@lilotux.net>
parents:
735
diff
changeset
|
355 } |
225 | 356 |
1076
b9698c89f46d
Fix memory leak in scr_handle_tab()
Mikael Berthe <mikael@lilotux.net>
parents:
894
diff
changeset
|
357 *dynlist = FALSE; |
94 | 358 return NULL; |
359 } | |
360 | |
1811 | 361 /* vim: set expandtab cindent cinoptions=>2\:2(0 sw=2 ts=2: For Vim users... */ |