Mercurial > ~mikael > mcabber > hg
annotate mcabber/mcabber/compl.c @ 1923:924f4552996c
Add a category module to complete the /module subcommands
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Wed, 10 Nov 2010 14:08:49 +0100 |
parents | 4ba68ad737bc |
children | c1f9a4d47e05 |
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); |
1923
924f4552996c
Add a category module to complete the /module subcommands
Mikael Berthe <mikael@lilotux.net>
parents:
1922
diff
changeset
|
89 register_builtin_cat(COMPL_MODULE); |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
90 } |
1607 | 91 |
92 // compl_new_category() | |
93 // Reserves id for new completion category. | |
94 // Returns 0, if no more categories can be allocated. | |
95 // Note, that user should not make any assumptions about id nature, | |
96 // as it is likely to change in future. | |
1828
8f7d7c05f275
Small coding style update
Mikael Berthe <mikael@lilotux.net>
parents:
1811
diff
changeset
|
97 guint compl_new_category(void) |
1607 | 98 { |
99 guint i = 0; | |
100 while ((registered_cats >> i) & 1) | |
101 i++; | |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
102 if (i >= 8 * sizeof (registered_cats)) |
1607 | 103 return 0; |
104 else { | |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
105 guint64 id = 1 << i; |
1607 | 106 registered_cats |= id; |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
107 return i+1; |
1607 | 108 } |
109 } | |
110 | |
1828
8f7d7c05f275
Small coding style update
Mikael Berthe <mikael@lilotux.net>
parents:
1811
diff
changeset
|
111 // compl_del_category(id) |
1607 | 112 // Frees reserved id for category. |
113 // Note, that for now it not validates its input, so, be careful | |
114 // 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
|
115 void compl_del_category(guint id) |
1607 | 116 { |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
117 if (!id) { |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
118 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
|
119 "Invalid category."); |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
120 return; |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
121 } |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
122 id--; |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
123 registered_cats &= ~(1<<id); |
1607 | 124 } |
125 #endif | |
126 | |
1756
e2c084204583
Add (optional) suffix after nick completion in Multi-User Chats
Mikael Berthe <mikael@lilotux.net>
parents:
1729
diff
changeset
|
127 // new_completion(prefix, compl_cat, suffix) |
94 | 128 // . prefix = beginning of the word, typed by the user |
129 // . 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
|
130 // . 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
|
131 // 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
|
132 // done_completion() must be called when finished. |
1228
9a68fe4515dc
Improve MUC nickname completion
Mikael Berthe <mikael@lilotux.net>
parents:
1205
diff
changeset
|
133 // 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
|
134 guint new_completion(const char *prefix, GSList *compl_cat, const gchar *suffix) |
94 | 135 { |
136 compl *c; | |
137 GSList *sl_cat; | |
735 | 138 size_t len = strlen(prefix); |
94 | 139 |
140 if (InputCompl) { // This should not happen, but hey... | |
141 cancel_completion(); | |
142 } | |
143 | |
144 c = g_new0(compl, 1); | |
145 // Build the list of matches | |
1228
9a68fe4515dc
Improve MUC nickname completion
Mikael Berthe <mikael@lilotux.net>
parents:
1205
diff
changeset
|
146 for (sl_cat = compl_cat; sl_cat; sl_cat = g_slist_next(sl_cat)) { |
94 | 147 char *word = sl_cat->data; |
104 | 148 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
|
149 if (strlen(word) != len) { |
e2c084204583
Add (optional) suffix after nick completion in Multi-User Chats
Mikael Berthe <mikael@lilotux.net>
parents:
1729
diff
changeset
|
150 gchar *compval; |
e2c084204583
Add (optional) suffix after nick completion in Multi-User Chats
Mikael Berthe <mikael@lilotux.net>
parents:
1729
diff
changeset
|
151 if (suffix) |
e2c084204583
Add (optional) suffix after nick completion in Multi-User Chats
Mikael Berthe <mikael@lilotux.net>
parents:
1729
diff
changeset
|
152 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
|
153 else |
e2c084204583
Add (optional) suffix after nick completion in Multi-User Chats
Mikael Berthe <mikael@lilotux.net>
parents:
1729
diff
changeset
|
154 compval = g_strdup(word+len); |
1852 | 155 c->list = g_slist_insert_sorted(c->list, compval, |
156 (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
|
157 } |
94 | 158 } |
159 } | |
160 c->next = c->list; | |
161 InputCompl = c; | |
1228
9a68fe4515dc
Improve MUC nickname completion
Mikael Berthe <mikael@lilotux.net>
parents:
1205
diff
changeset
|
162 return g_slist_length(c->list); |
94 | 163 } |
164 | |
165 // done_completion(); | |
166 void done_completion(void) | |
167 { | |
894
f76b32ff2f14
done_completion(): free all allocated memory
Mikael Berthe <mikael@lilotux.net>
parents:
820
diff
changeset
|
168 GSList *clp; |
f76b32ff2f14
done_completion(): free all allocated memory
Mikael Berthe <mikael@lilotux.net>
parents:
820
diff
changeset
|
169 |
98 | 170 if (!InputCompl) return; |
171 | |
894
f76b32ff2f14
done_completion(): free all allocated memory
Mikael Berthe <mikael@lilotux.net>
parents:
820
diff
changeset
|
172 // Free the current completion list |
f76b32ff2f14
done_completion(): free all allocated memory
Mikael Berthe <mikael@lilotux.net>
parents:
820
diff
changeset
|
173 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
|
174 g_free(clp->data); |
94 | 175 g_slist_free(InputCompl->list); |
176 g_free(InputCompl); | |
177 InputCompl = NULL; | |
178 } | |
179 | |
180 // cancel_completion() | |
181 // Returns the number of chars to delete to cancel the completion | |
182 guint cancel_completion(void) | |
183 { | |
98 | 184 if (!InputCompl) return 0; |
94 | 185 return InputCompl->len_compl; |
186 } | |
187 | |
188 // Returns pointer to text to insert, NULL if no completion. | |
189 const char *complete() | |
190 { | |
191 compl* c = InputCompl; | |
192 char *r; | |
98 | 193 |
194 if (!InputCompl) return NULL; | |
195 | |
94 | 196 if (!c->next) { |
197 c->next = c->list; // back to the beginning | |
198 c->len_compl = 0; | |
199 return NULL; | |
200 } | |
201 r = (char*)c->next->data; | |
202 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
|
203 if (!utf8_mode) { |
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
204 c->len_compl = strlen(r); |
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
205 } else { |
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
206 char *wc; |
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
207 c->len_compl = 0; |
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
208 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
|
209 c->len_compl++; |
1240
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
210 } |
94 | 211 return r; |
212 } | |
213 | |
214 | |
215 /* Categories functions */ | |
216 | |
95 | 217 // compl_add_category_word(categ, command) |
218 // 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
|
219 void compl_add_category_word(guint categ, const gchar *word) |
94 | 220 { |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
221 guint64 catv; |
94 | 222 GSList *sl_cat; |
223 category *cat; | |
121 | 224 char *nword; |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
225 |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
226 if (!categ) { |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
227 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
|
228 "Invalid category."); |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
229 return; |
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 |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
232 categ--; |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
233 catv = 1UL << categ; |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
234 |
94 | 235 // Look for category |
236 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
|
237 if (catv == ((category*)sl_cat->data)->flag) |
94 | 238 break; |
239 } | |
240 if (!sl_cat) { // Category not found, let's create it | |
241 cat = g_new0(category, 1); | |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
242 cat->flag = catv; |
94 | 243 Categories = g_slist_append(Categories, cat); |
244 } else | |
245 cat = (category*)sl_cat->data; | |
246 | |
121 | 247 // If word is not space-terminated, we add one trailing space |
248 for (nword = (char*)word; *nword; nword++) | |
249 ; | |
250 if (nword > word) nword--; | |
251 if (*nword != ' ') { // Add a space | |
1240
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
252 nword = g_strdup_printf("%s ", word); |
121 | 253 } else { // word is fine |
254 nword = g_strdup(word); | |
255 } | |
256 | |
1852 | 257 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
|
258 return; |
8d7810f529db
Sort items in completion lists
alkino <nicolas.cornu@ensi-bourges.fr>
parents:
1828
diff
changeset
|
259 |
1852 | 260 cat->words = g_slist_insert_sorted(cat->words, nword, |
261 (GCompareFunc)g_ascii_strcasecmp); | |
94 | 262 } |
263 | |
284
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
264 // compl_del_category_word(categ, command) |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
265 // 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
|
266 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
|
267 { |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
268 guint64 catv; |
284
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
269 GSList *sl_cat, *sl_elt; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
270 category *cat; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
271 char *nword; |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
272 |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
273 if (!categ) { |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
274 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
|
275 "Invalid category."); |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
276 return; |
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 |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
279 categ--; |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
280 catv = 1UL << categ; |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
281 |
284
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
282 // Look for category |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
283 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
|
284 if (catv == ((category*)sl_cat->data)->flag) |
284
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
285 break; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
286 } |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
287 if (!sl_cat) return; // Category not found, finished! |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
288 |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
289 cat = (category*)sl_cat->data; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
290 |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
291 // 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
|
292 for (nword = (char*)word; *nword; nword++) |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
293 ; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
294 if (nword > word) nword--; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
295 if (*nword != ' ') { // Add a space |
1240
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
296 nword = g_strdup_printf("%s ", word); |
284
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
297 } else { // word is fine |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
298 nword = g_strdup(word); |
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 |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
301 sl_elt = cat->words; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
302 while (sl_elt) { |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
303 if (!strcasecmp((char*)sl_elt->data, nword)) { |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
304 g_free(sl_elt->data); |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
305 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
|
306 break; // Only remove first occurence |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
307 } |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
308 sl_elt = g_slist_next(sl_elt); |
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 } |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
311 |
95 | 312 // compl_get_category_list() |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
313 // 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
|
314 // 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
|
315 // whole list after use. |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
316 GSList *compl_get_category_list(guint categ, guint *dynlist) |
94 | 317 { |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
318 guint64 cat_flags; |
94 | 319 GSList *sl_cat; |
1076
b9698c89f46d
Fix memory leak in scr_handle_tab()
Mikael Berthe <mikael@lilotux.net>
parents:
894
diff
changeset
|
320 |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
321 if (!categ) { |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
322 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
|
323 "Invalid category."); |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
324 return NULL; |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
325 } |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
326 |
1076
b9698c89f46d
Fix memory leak in scr_handle_tab()
Mikael Berthe <mikael@lilotux.net>
parents:
894
diff
changeset
|
327 *dynlist = FALSE; |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
328 cat_flags = 1UL << (categ - 1); |
1076
b9698c89f46d
Fix memory leak in scr_handle_tab()
Mikael Berthe <mikael@lilotux.net>
parents:
894
diff
changeset
|
329 |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
330 // Look for the category |
94 | 331 for (sl_cat=Categories; sl_cat; sl_cat = g_slist_next(sl_cat)) { |
332 if (cat_flags == ((category*)sl_cat->data)->flag) | |
333 break; | |
334 } | |
335 if (sl_cat) // Category was found, easy... | |
336 return ((category*)sl_cat->data)->words; | |
337 | |
225 | 338 // Handle dynamic SLists |
1076
b9698c89f46d
Fix memory leak in scr_handle_tab()
Mikael Berthe <mikael@lilotux.net>
parents:
894
diff
changeset
|
339 *dynlist = TRUE; |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
340 if (categ == COMPL_GROUPNAME) { |
225 | 341 return compl_list(ROSTER_TYPE_GROUP); |
342 } | |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
343 if (categ == COMPL_JID) { |
225 | 344 return compl_list(ROSTER_TYPE_USER); |
345 } | |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
346 if (categ == COMPL_RESOURCE) { |
792
89ad7b530b3c
MUC: Fix completion for UTF-8 nicknames
Mikael Berthe <mikael@lilotux.net>
parents:
757
diff
changeset
|
347 return buddy_getresources_locale(NULL); |
501
7c1ca00070e8
Add COMPL_RESOURCE (resource completion category)
Mikael Berthe <mikael@lilotux.net>
parents:
393
diff
changeset
|
348 } |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
349 if (categ == COMPL_EVENTSID) { |
1685
1342df44c814
Improved events interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1668
diff
changeset
|
350 GSList *compl = evs_geteventslist(); |
1342df44c814
Improved events interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1668
diff
changeset
|
351 GSList *cel; |
1342df44c814
Improved events interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1668
diff
changeset
|
352 for (cel = compl; cel; cel = cel->next) |
1342df44c814
Improved events interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1668
diff
changeset
|
353 cel->data = g_strdup(cel->data); |
1342df44c814
Improved events interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1668
diff
changeset
|
354 compl = g_slist_append(compl, g_strdup("list")); |
1342df44c814
Improved events interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1668
diff
changeset
|
355 return compl; |
757
ae23c8826efb
Improve completion for the "/event" command
Mikael Berthe <mikael@lilotux.net>
parents:
735
diff
changeset
|
356 } |
225 | 357 |
1076
b9698c89f46d
Fix memory leak in scr_handle_tab()
Mikael Berthe <mikael@lilotux.net>
parents:
894
diff
changeset
|
358 *dynlist = FALSE; |
94 | 359 return NULL; |
360 } | |
361 | |
1811 | 362 /* vim: set expandtab cindent cinoptions=>2\:2(0 sw=2 ts=2: For Vim users... */ |