Mercurial > ~mikael > mcabber > hg
annotate mcabber/mcabber/compl.c @ 2223:965e0282c128
Backed out changeset fa516ef22145
Turns out that in this hash the value pointer is the same as the
key pointer, so there's no need to free both the key and the value.
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Fri, 06 Nov 2015 22:31:40 +0100 |
parents | 2f294c2b6778 |
children | f5402d705f67 |
rev | line source |
---|---|
94 | 1 /* |
2 * compl.c -- Completion system | |
393 | 3 * |
2134
fc7a758ebbde
Remove deprecated options comments, update headers
Mikael Berthe <mikael@lilotux.net>
parents:
2107
diff
changeset
|
4 * Copyright (C) 2005-2014 Mikael Berthe <mikael@lilotux.net> |
fc7a758ebbde
Remove deprecated options comments, update headers
Mikael Berthe <mikael@lilotux.net>
parents:
2107
diff
changeset
|
5 * Copyright (C) 2009-2014 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 { | |
2143
2f294c2b6778
Add a backward completion (Oleg)
Mikael Berthe <mikael@lilotux.net>
parents:
2134
diff
changeset
|
44 GList *list; // list of matches |
94 | 45 guint len_prefix; // length of text already typed by the user |
46 guint len_compl; // length of the last completion | |
2143
2f294c2b6778
Add a backward completion (Oleg)
Mikael Berthe <mikael@lilotux.net>
parents:
2134
diff
changeset
|
47 GList *next; // pointer to next completion to try |
94 | 48 } compl; |
49 | |
2042
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
50 typedef GSList *(*compl_handler_t) (void); // XXX userdata? *dynlist? |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
51 |
94 | 52 // Category structure |
53 typedef struct { | |
2042
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
54 guint flags; |
94 | 55 GSList *words; |
2042
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
56 compl_handler_t dynamic; |
94 | 57 } category; |
58 | |
2042
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
59 #define COMPL_CAT_BUILTIN 0x01 |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
60 #define COMPL_CAT_ACTIVE 0x02 |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
61 #define COMPL_CAT_DYNAMIC 0x04 |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
62 #define COMPL_CAT_REVERSE 0x10 |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
63 #define COMPL_CAT_NOSORT 0x20 |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
64 |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
65 #define COMPL_CAT_USERFLAGS 0x30 |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
66 |
94 | 67 static compl *InputCompl; |
2042
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
68 static category *Categories; |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
69 static guint num_categories; |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
70 |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
71 // Dynamic completions callbacks |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
72 static GSList *compl_dyn_group (void) |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
73 { |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
74 return compl_list(ROSTER_TYPE_GROUP); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
75 } |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
76 |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
77 static GSList *compl_dyn_user (void) |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
78 { |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
79 return compl_list(ROSTER_TYPE_USER); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
80 } |
94 | 81 |
2042
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
82 static GSList *compl_dyn_resource (void) |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
83 { |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
84 return buddy_getresources_locale(NULL); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
85 } |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
86 |
2042
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
87 static GSList *compl_dyn_events (void) |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
88 { |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
89 GSList *compl = evs_geteventslist(); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
90 GSList *cel; |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
91 for (cel = compl; cel; cel = cel->next) |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
92 cel->data = g_strdup(cel->data); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
93 compl = g_slist_append(compl, g_strdup("list")); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
94 return compl; |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
95 } |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
96 |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
97 static inline void register_builtin_cat(guint c, compl_handler_t dynamic) { |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
98 Categories[c-1].flags = COMPL_CAT_BUILTIN | COMPL_CAT_ACTIVE; |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
99 Categories[c-1].words = NULL; |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
100 Categories[c-1].dynamic = dynamic; |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
101 if (dynamic != NULL) { |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
102 Categories[c-1].flags |= COMPL_CAT_DYNAMIC; |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
103 } |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
104 } |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
105 |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
106 void compl_init_system(void) |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
107 { |
2042
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
108 num_categories = COMPL_MAX_ID; |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
109 #ifdef MODULES_ENABLE |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
110 num_categories = ((num_categories / 16) + 1) * 16; |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
111 #endif |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
112 Categories = g_new0(category, num_categories); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
113 |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
114 // Builtin completion categories: |
2042
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
115 register_builtin_cat(COMPL_CMD, NULL); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
116 register_builtin_cat(COMPL_JID, compl_dyn_user); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
117 register_builtin_cat(COMPL_URLJID, NULL); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
118 register_builtin_cat(COMPL_NAME, NULL); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
119 register_builtin_cat(COMPL_STATUS, NULL); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
120 register_builtin_cat(COMPL_FILENAME, NULL); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
121 register_builtin_cat(COMPL_ROSTER, NULL); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
122 register_builtin_cat(COMPL_BUFFER, NULL); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
123 register_builtin_cat(COMPL_GROUP, NULL); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
124 register_builtin_cat(COMPL_GROUPNAME, compl_dyn_group); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
125 register_builtin_cat(COMPL_MULTILINE, NULL); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
126 register_builtin_cat(COMPL_ROOM, NULL); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
127 register_builtin_cat(COMPL_RESOURCE, compl_dyn_resource); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
128 register_builtin_cat(COMPL_AUTH, NULL); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
129 register_builtin_cat(COMPL_REQUEST, NULL); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
130 register_builtin_cat(COMPL_EVENTS, NULL); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
131 register_builtin_cat(COMPL_EVENTSID, compl_dyn_events); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
132 register_builtin_cat(COMPL_PGP, NULL); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
133 register_builtin_cat(COMPL_COLOR, NULL); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
134 register_builtin_cat(COMPL_OTR, NULL); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
135 register_builtin_cat(COMPL_OTRPOLICY, NULL); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
136 register_builtin_cat(COMPL_MODULE, NULL); |
2107
1bd9978ed5d0
Added carbon command
Roeland Jago Douma <roeland@famdouma.nl>
parents:
2042
diff
changeset
|
137 register_builtin_cat(COMPL_CARBONS, NULL); |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
138 } |
1607 | 139 |
2042
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
140 #ifdef MODULES_ENABLE |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
141 // compl_new_category(flags) |
1607 | 142 // Reserves id for new completion category. |
2042
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
143 // Flags determine word sorting order. |
1607 | 144 // Returns 0, if no more categories can be allocated. |
2042
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
145 guint compl_new_category(guint flags) |
1607 | 146 { |
2042
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
147 guint i; |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
148 for (i = 0; i < num_categories; i++) |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
149 if (!(Categories[i].flags & COMPL_CAT_ACTIVE)) |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
150 break; |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
151 if (i >= num_categories ) { |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
152 guint j; |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
153 if (num_categories > G_MAXUINT - 16) { |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
154 scr_log_print(LPRINT_LOGNORM, "Warning: Too many " |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
155 "completion categories!"); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
156 return 0; |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
157 } |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
158 num_categories += 16; |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
159 Categories = g_renew(category, Categories, num_categories); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
160 for (j = i+1; j < num_categories; j++) |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
161 Categories[j].flags = 0; |
1607 | 162 } |
2042
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
163 Categories[i].flags = COMPL_CAT_ACTIVE | (flags & COMPL_CAT_USERFLAGS); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
164 Categories[i].words = NULL; |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
165 return i+1; |
1607 | 166 } |
167 | |
1828
8f7d7c05f275
Small coding style update
Mikael Berthe <mikael@lilotux.net>
parents:
1811
diff
changeset
|
168 // compl_del_category(id) |
1607 | 169 // Frees reserved id for category. |
170 // Note, that for now it not validates its input, so, be careful | |
171 // and specify exactly what you get from compl_new_category. | |
2042
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
172 void compl_del_category(guint compl) |
1607 | 173 { |
2042
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
174 GSList *wel; |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
175 |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
176 if (!compl) { |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
177 scr_log_print(LPRINT_DEBUG, "Error: compl_del_category() - " |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
178 "Invalid category (0)."); |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
179 return; |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
180 } |
2042
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
181 |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
182 compl--; |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
183 |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
184 if ((compl >= num_categories) || |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
185 (Categories[compl].flags & COMPL_CAT_BUILTIN)) { |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
186 scr_log_print(LPRINT_DEBUG, "Error: compl_del_category() " |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
187 "Invalid category."); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
188 return; |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
189 } |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
190 |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
191 Categories[compl].flags = 0; |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
192 for (wel = Categories[compl].words; wel; wel = g_slist_next (wel)) |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
193 g_free (wel -> data); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
194 g_slist_free (Categories[compl].words); |
1607 | 195 } |
196 #endif | |
197 | |
1756
e2c084204583
Add (optional) suffix after nick completion in Multi-User Chats
Mikael Berthe <mikael@lilotux.net>
parents:
1729
diff
changeset
|
198 // new_completion(prefix, compl_cat, suffix) |
94 | 199 // . prefix = beginning of the word, typed by the user |
200 // . 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
|
201 // . 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
|
202 // 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
|
203 // done_completion() must be called when finished. |
1228
9a68fe4515dc
Improve MUC nickname completion
Mikael Berthe <mikael@lilotux.net>
parents:
1205
diff
changeset
|
204 // 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
|
205 guint new_completion(const char *prefix, GSList *compl_cat, const gchar *suffix) |
94 | 206 { |
207 compl *c; | |
2042
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
208 guint ret_len = 0; |
94 | 209 GSList *sl_cat; |
1926
c1f9a4d47e05
Add option 'completion_ignore_case'
Mikael Berthe <mikael@lilotux.net>
parents:
1923
diff
changeset
|
210 gint (*cmp)(const char *s1, const char *s2, size_t n); |
735 | 211 size_t len = strlen(prefix); |
94 | 212 |
213 if (InputCompl) { // This should not happen, but hey... | |
2042
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
214 scr_log_print(LPRINT_DEBUG, "Warning: new_completion() - " |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
215 "Previous completion exists!"); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
216 done_completion(); |
94 | 217 } |
218 | |
1926
c1f9a4d47e05
Add option 'completion_ignore_case'
Mikael Berthe <mikael@lilotux.net>
parents:
1923
diff
changeset
|
219 if (settings_opt_get_int("completion_ignore_case")) |
c1f9a4d47e05
Add option 'completion_ignore_case'
Mikael Berthe <mikael@lilotux.net>
parents:
1923
diff
changeset
|
220 cmp = &strncasecmp; |
c1f9a4d47e05
Add option 'completion_ignore_case'
Mikael Berthe <mikael@lilotux.net>
parents:
1923
diff
changeset
|
221 else |
c1f9a4d47e05
Add option 'completion_ignore_case'
Mikael Berthe <mikael@lilotux.net>
parents:
1923
diff
changeset
|
222 cmp = &strncmp; |
c1f9a4d47e05
Add option 'completion_ignore_case'
Mikael Berthe <mikael@lilotux.net>
parents:
1923
diff
changeset
|
223 |
94 | 224 c = g_new0(compl, 1); |
225 // Build the list of matches | |
1228
9a68fe4515dc
Improve MUC nickname completion
Mikael Berthe <mikael@lilotux.net>
parents:
1205
diff
changeset
|
226 for (sl_cat = compl_cat; sl_cat; sl_cat = g_slist_next(sl_cat)) { |
94 | 227 char *word = sl_cat->data; |
1926
c1f9a4d47e05
Add option 'completion_ignore_case'
Mikael Berthe <mikael@lilotux.net>
parents:
1923
diff
changeset
|
228 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
|
229 if (strlen(word) != len) { |
e2c084204583
Add (optional) suffix after nick completion in Multi-User Chats
Mikael Berthe <mikael@lilotux.net>
parents:
1729
diff
changeset
|
230 gchar *compval; |
e2c084204583
Add (optional) suffix after nick completion in Multi-User Chats
Mikael Berthe <mikael@lilotux.net>
parents:
1729
diff
changeset
|
231 if (suffix) |
e2c084204583
Add (optional) suffix after nick completion in Multi-User Chats
Mikael Berthe <mikael@lilotux.net>
parents:
1729
diff
changeset
|
232 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
|
233 else |
e2c084204583
Add (optional) suffix after nick completion in Multi-User Chats
Mikael Berthe <mikael@lilotux.net>
parents:
1729
diff
changeset
|
234 compval = g_strdup(word+len); |
2042
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
235 // for a bit of efficiency, will reverse order afterwards |
2143
2f294c2b6778
Add a backward completion (Oleg)
Mikael Berthe <mikael@lilotux.net>
parents:
2134
diff
changeset
|
236 c->list = g_list_prepend(c->list, compval); |
2042
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
237 ret_len ++; |
1756
e2c084204583
Add (optional) suffix after nick completion in Multi-User Chats
Mikael Berthe <mikael@lilotux.net>
parents:
1729
diff
changeset
|
238 } |
94 | 239 } |
240 } | |
2143
2f294c2b6778
Add a backward completion (Oleg)
Mikael Berthe <mikael@lilotux.net>
parents:
2134
diff
changeset
|
241 c->list = g_list_reverse(c->list); |
2f294c2b6778
Add a backward completion (Oleg)
Mikael Berthe <mikael@lilotux.net>
parents:
2134
diff
changeset
|
242 c->next = NULL; |
94 | 243 InputCompl = c; |
2042
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
244 return ret_len; |
94 | 245 } |
246 | |
247 // done_completion(); | |
248 void done_completion(void) | |
249 { | |
2143
2f294c2b6778
Add a backward completion (Oleg)
Mikael Berthe <mikael@lilotux.net>
parents:
2134
diff
changeset
|
250 GList *clp; |
894
f76b32ff2f14
done_completion(): free all allocated memory
Mikael Berthe <mikael@lilotux.net>
parents:
820
diff
changeset
|
251 |
98 | 252 if (!InputCompl) return; |
253 | |
894
f76b32ff2f14
done_completion(): free all allocated memory
Mikael Berthe <mikael@lilotux.net>
parents:
820
diff
changeset
|
254 // Free the current completion list |
2143
2f294c2b6778
Add a backward completion (Oleg)
Mikael Berthe <mikael@lilotux.net>
parents:
2134
diff
changeset
|
255 for (clp = InputCompl->list; clp; clp = g_list_next(clp)) |
894
f76b32ff2f14
done_completion(): free all allocated memory
Mikael Berthe <mikael@lilotux.net>
parents:
820
diff
changeset
|
256 g_free(clp->data); |
2143
2f294c2b6778
Add a backward completion (Oleg)
Mikael Berthe <mikael@lilotux.net>
parents:
2134
diff
changeset
|
257 g_list_free(InputCompl->list); |
94 | 258 g_free(InputCompl); |
259 InputCompl = NULL; | |
260 } | |
261 | |
262 // cancel_completion() | |
263 // Returns the number of chars to delete to cancel the completion | |
264 guint cancel_completion(void) | |
265 { | |
98 | 266 if (!InputCompl) return 0; |
94 | 267 return InputCompl->len_compl; |
268 } | |
269 | |
270 // Returns pointer to text to insert, NULL if no completion. | |
2143
2f294c2b6778
Add a backward completion (Oleg)
Mikael Berthe <mikael@lilotux.net>
parents:
2134
diff
changeset
|
271 const char *complete(gboolean fwd) |
94 | 272 { |
273 compl* c = InputCompl; | |
274 char *r; | |
98 | 275 |
276 if (!InputCompl) return NULL; | |
277 | |
94 | 278 if (!c->next) { |
2143
2f294c2b6778
Add a backward completion (Oleg)
Mikael Berthe <mikael@lilotux.net>
parents:
2134
diff
changeset
|
279 if (fwd) |
2f294c2b6778
Add a backward completion (Oleg)
Mikael Berthe <mikael@lilotux.net>
parents:
2134
diff
changeset
|
280 c->next = c->list; // back to the beginning |
2f294c2b6778
Add a backward completion (Oleg)
Mikael Berthe <mikael@lilotux.net>
parents:
2134
diff
changeset
|
281 else |
2f294c2b6778
Add a backward completion (Oleg)
Mikael Berthe <mikael@lilotux.net>
parents:
2134
diff
changeset
|
282 c->next = g_list_last(c->list); // back to the ending |
2f294c2b6778
Add a backward completion (Oleg)
Mikael Berthe <mikael@lilotux.net>
parents:
2134
diff
changeset
|
283 } else { |
2f294c2b6778
Add a backward completion (Oleg)
Mikael Berthe <mikael@lilotux.net>
parents:
2134
diff
changeset
|
284 if (fwd) |
2f294c2b6778
Add a backward completion (Oleg)
Mikael Berthe <mikael@lilotux.net>
parents:
2134
diff
changeset
|
285 c->next = g_list_next(c->next); |
2f294c2b6778
Add a backward completion (Oleg)
Mikael Berthe <mikael@lilotux.net>
parents:
2134
diff
changeset
|
286 else |
2f294c2b6778
Add a backward completion (Oleg)
Mikael Berthe <mikael@lilotux.net>
parents:
2134
diff
changeset
|
287 c->next = g_list_previous(c->next); |
2f294c2b6778
Add a backward completion (Oleg)
Mikael Berthe <mikael@lilotux.net>
parents:
2134
diff
changeset
|
288 } |
2f294c2b6778
Add a backward completion (Oleg)
Mikael Berthe <mikael@lilotux.net>
parents:
2134
diff
changeset
|
289 |
2f294c2b6778
Add a backward completion (Oleg)
Mikael Berthe <mikael@lilotux.net>
parents:
2134
diff
changeset
|
290 if (!c->next) { |
2f294c2b6778
Add a backward completion (Oleg)
Mikael Berthe <mikael@lilotux.net>
parents:
2134
diff
changeset
|
291 c->next = NULL; |
94 | 292 c->len_compl = 0; |
293 return NULL; | |
294 } | |
2143
2f294c2b6778
Add a backward completion (Oleg)
Mikael Berthe <mikael@lilotux.net>
parents:
2134
diff
changeset
|
295 |
94 | 296 r = (char*)c->next->data; |
2143
2f294c2b6778
Add a backward completion (Oleg)
Mikael Berthe <mikael@lilotux.net>
parents:
2134
diff
changeset
|
297 |
1240
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
298 if (!utf8_mode) { |
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
299 c->len_compl = strlen(r); |
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
300 } else { |
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
301 char *wc; |
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
302 c->len_compl = 0; |
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
303 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
|
304 c->len_compl++; |
1240
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
305 } |
94 | 306 return r; |
307 } | |
308 | |
309 | |
310 /* Categories functions */ | |
311 | |
2042
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
312 static gint compl_sort_forward(gconstpointer a, gconstpointer b) |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
313 { |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
314 return g_ascii_strcasecmp((const gchar *)a, (const gchar *)b); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
315 } |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
316 |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
317 static gint compl_sort_reverse(gconstpointer a, gconstpointer b) |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
318 { |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
319 return -g_ascii_strcasecmp((const gchar *)a, (const gchar *)b); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
320 } |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
321 |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
322 static gint compl_sort_append(gconstpointer a, gconstpointer b) |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
323 { |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
324 return 1; |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
325 } |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
326 |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
327 static gint compl_sort_prepend(gconstpointer a, gconstpointer b) |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
328 { |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
329 return -1; |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
330 } |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
331 |
95 | 332 // compl_add_category_word(categ, command) |
333 // 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
|
334 void compl_add_category_word(guint categ, const gchar *word) |
94 | 335 { |
121 | 336 char *nword; |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
337 |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
338 if (!categ) { |
2042
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
339 scr_log_print(LPRINT_DEBUG, "Error: compl_add_category_word() - " |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
340 "Invalid category (0)."); |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
341 return; |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
342 } |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
343 |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
344 categ--; |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
345 |
2042
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
346 if ((categ >= num_categories) || |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
347 !(Categories[categ].flags & COMPL_CAT_ACTIVE)) { |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
348 scr_log_print(LPRINT_DEBUG, "Error: compl_add_category_word() - " |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
349 "Category does not exist."); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
350 return; |
94 | 351 } |
352 | |
121 | 353 // If word is not space-terminated, we add one trailing space |
354 for (nword = (char*)word; *nword; nword++) | |
355 ; | |
356 if (nword > word) nword--; | |
357 if (*nword != ' ') { // Add a space | |
1240
a54645448e00
Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents:
1228
diff
changeset
|
358 nword = g_strdup_printf("%s ", word); |
121 | 359 } else { // word is fine |
360 nword = g_strdup(word); | |
361 } | |
362 | |
2042
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
363 if (g_slist_find_custom(Categories[categ].words, nword, |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
364 (GCompareFunc)g_strcmp0) == NULL) { |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
365 guint flags = Categories[categ].flags; |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
366 GCompareFunc comparator = compl_sort_forward; |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
367 if (flags & COMPL_CAT_NOSORT) { |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
368 if (flags & COMPL_CAT_REVERSE) |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
369 comparator = compl_sort_prepend; |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
370 else |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
371 comparator = compl_sort_append; |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
372 } else if (flags & COMPL_CAT_REVERSE) |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
373 comparator = compl_sort_reverse; |
1850
8d7810f529db
Sort items in completion lists
alkino <nicolas.cornu@ensi-bourges.fr>
parents:
1828
diff
changeset
|
374 |
2042
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
375 Categories[categ].words = g_slist_insert_sorted |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
376 (Categories[categ].words, nword, comparator); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
377 } |
94 | 378 } |
379 | |
284
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
380 // compl_del_category_word(categ, command) |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
381 // 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
|
382 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
|
383 { |
2042
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
384 GSList *wel; |
284
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
385 char *nword; |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
386 |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
387 if (!categ) { |
2042
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
388 scr_log_print(LPRINT_DEBUG, "Error: compl_del_category_word() - " |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
389 "Invalid category (0)."); |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
390 return; |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
391 } |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
392 |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
393 categ--; |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
394 |
2042
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
395 if ((categ >= num_categories) || |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
396 !(Categories[categ].flags & COMPL_CAT_ACTIVE)) { |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
397 scr_log_print(LPRINT_DEBUG, "Error: compl_del_category_word() - " |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
398 "Category does not exist."); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
399 return; |
284
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
400 } |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
401 |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
402 // 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
|
403 for (nword = (char*)word; *nword; nword++) |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
404 ; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
405 if (nword > word) nword--; |
2042
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
406 if (*nword != ' ') // Add a space |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
407 word = nword = g_strdup_printf("%s ", word); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
408 else |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
409 nword = NULL; |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
410 |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
411 for (wel = Categories[categ].words; wel; wel = g_slist_next (wel)) { |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
412 if (!strcasecmp((char*)wel->data, word)) { |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
413 g_free(wel->data); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
414 Categories[categ].words = g_slist_delete_link |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
415 (Categories[categ].words, wel); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
416 break; // Only remove first occurence |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
417 } |
284
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
418 } |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
419 |
2042
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
420 g_free (nword); |
284
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
421 } |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
422 |
95 | 423 // compl_get_category_list() |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
424 // 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
|
425 // 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
|
426 // whole list after use. |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
427 GSList *compl_get_category_list(guint categ, guint *dynlist) |
94 | 428 { |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
429 if (!categ) { |
2042
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
430 scr_log_print(LPRINT_DEBUG, "Error: compl_get_category_list() - " |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
431 "Invalid category (0)."); |
1922
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
432 return NULL; |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
433 } |
4ba68ad737bc
Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents:
1852
diff
changeset
|
434 |
2042
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
435 categ --; |
94 | 436 |
2042
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
437 if ((categ > num_categories) || |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
438 !(Categories[categ].flags & COMPL_CAT_ACTIVE)) { |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
439 scr_log_print(LPRINT_DEBUG, "Error: compl_get_category_list() - " |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
440 "Category does not exist."); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
441 return NULL; |
757
ae23c8826efb
Improve completion for the "/event" command
Mikael Berthe <mikael@lilotux.net>
parents:
735
diff
changeset
|
442 } |
225 | 443 |
2042
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
444 if (Categories[categ].flags & COMPL_CAT_DYNAMIC) { |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
445 *dynlist = TRUE; |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
446 return (*Categories[categ].dynamic) (); |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
447 } else { |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
448 *dynlist = FALSE; |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
449 return Categories[categ].words; |
0cb8ea02e472
Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2035
diff
changeset
|
450 } |
94 | 451 } |
452 | |
1811 | 453 /* vim: set expandtab cindent cinoptions=>2\:2(0 sw=2 ts=2: For Vim users... */ |