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