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