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