annotate mcabber/mcabber/compl.c @ 2107:1bd9978ed5d0

Added carbon command Also added help commands for carbons Added auto completion for carbons command
author Roeland Jago Douma <roeland@famdouma.nl>
date Tue, 15 Oct 2013 18:55:18 +0200
parents 0cb8ea02e472
children fc7a758ebbde
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
94
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
1 /*
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
2 * compl.c -- Completion system
393
f8f3c7493457 Whitespace cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 284
diff changeset
3 *
1729
e6e89b1d7831 Minor style and header updates
Mikael Berthe <mikael@lilotux.net>
parents: 1685
diff changeset
4 * Copyright (C) 2005-2010 Mikael Berthe <mikael@lilotux.net>
2042
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
5 * Copyright (C) 2009-2012 Myhailo Danylenko <isbear@ukrpost.net>
94
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
6 *
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
7 * This program is free software; you can redistribute it and/or modify
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
8 * it under the terms of the GNU General Public License as published by
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
9 * the Free Software Foundation; either version 2 of the License, or (at
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
10 * your option) any later version.
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
11 *
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
15 * General Public License for more details.
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
16 *
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
17 * You should have received a copy of the GNU General Public License
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
18 * along with this program; if not, write to the Free Software
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
20 * USA
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
21 */
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
22
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
23 /* Usage, basically:
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
24 * - new_completion(); // 1. Initialization
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
25 * - complete(); // 2. 1st completion
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
26 * - cancel_completion(); // 3a. 2nd completion / cancel previous
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
27 * - complete(); // 3b. 2nd completion / complete
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
28 * ...
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
29 * - done_completion(); // n. finished -- free allocated areas
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
30 *
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
31 */
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
32
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
33 #include <string.h>
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
34
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
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
d5ae42cbe1fa [/trunk] Changeset 237 by mikael
mikael
parents: 121
diff changeset
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
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
41
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
42 // Completion structure
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
43 typedef struct {
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
44 GSList *list; // list of matches
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
45 guint len_prefix; // length of text already typed by the user
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
46 guint len_compl; // length of the last completion
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
47 GSList *next; // pointer to next completion to try
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
48 } compl;
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
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
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
52 // Category structure
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
53 typedef struct {
2042
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
54 guint flags;
94
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
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
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
57 } category;
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
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
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
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
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
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
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
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
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
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
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
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
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
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
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
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
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
166 }
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
167
1828
8f7d7c05f275 Small coding style update
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
168 // compl_del_category(id)
1607
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
169 // Frees reserved id for category.
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
170 // Note, that for now it not validates its input, so, be careful
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
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
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
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
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
195 }
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
196 #endif
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1599
diff changeset
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
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
199 // . prefix = beginning of the word, typed by the user
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
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
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
206 {
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
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
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
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
2f027806cd48 Some code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
211 size_t len = strlen(prefix);
94
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
212
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
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
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
217 }
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
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
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
224 c = g_new0(compl, 1);
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
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
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
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
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
236 c->list = g_slist_prepend(c->list, compval);
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
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
239 }
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
240 }
2042
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
241 c->next = c->list = g_slist_reverse (c->list);
94
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
242 InputCompl = c;
2042
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
243 return ret_len;
94
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
244 }
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
245
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
246 // done_completion();
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
247 void done_completion(void)
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
248 {
894
f76b32ff2f14 done_completion(): free all allocated memory
Mikael Berthe <mikael@lilotux.net>
parents: 820
diff changeset
249 GSList *clp;
f76b32ff2f14 done_completion(): free all allocated memory
Mikael Berthe <mikael@lilotux.net>
parents: 820
diff changeset
250
98
f20831f7d349 [/trunk] Changeset 112 by mikael
mikael
parents: 95
diff changeset
251 if (!InputCompl) return;
f20831f7d349 [/trunk] Changeset 112 by mikael
mikael
parents: 95
diff changeset
252
894
f76b32ff2f14 done_completion(): free all allocated memory
Mikael Berthe <mikael@lilotux.net>
parents: 820
diff changeset
253 // Free the current completion list
f76b32ff2f14 done_completion(): free all allocated memory
Mikael Berthe <mikael@lilotux.net>
parents: 820
diff changeset
254 for (clp = InputCompl->list; clp; clp = g_slist_next(clp))
f76b32ff2f14 done_completion(): free all allocated memory
Mikael Berthe <mikael@lilotux.net>
parents: 820
diff changeset
255 g_free(clp->data);
94
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
256 g_slist_free(InputCompl->list);
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
257 g_free(InputCompl);
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
258 InputCompl = NULL;
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
259 }
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
260
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
261 // cancel_completion()
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
262 // Returns the number of chars to delete to cancel the completion
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
263 guint cancel_completion(void)
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
264 {
98
f20831f7d349 [/trunk] Changeset 112 by mikael
mikael
parents: 95
diff changeset
265 if (!InputCompl) return 0;
94
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
266 return InputCompl->len_compl;
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
267 }
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
268
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
269 // Returns pointer to text to insert, NULL if no completion.
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
270 const char *complete()
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
271 {
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
272 compl* c = InputCompl;
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
273 char *r;
98
f20831f7d349 [/trunk] Changeset 112 by mikael
mikael
parents: 95
diff changeset
274
f20831f7d349 [/trunk] Changeset 112 by mikael
mikael
parents: 95
diff changeset
275 if (!InputCompl) return NULL;
f20831f7d349 [/trunk] Changeset 112 by mikael
mikael
parents: 95
diff changeset
276
94
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
277 if (!c->next) {
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
278 c->next = c->list; // back to the beginning
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
279 c->len_compl = 0;
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
280 return NULL;
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
281 }
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
282 r = (char*)c->next->data;
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
283 c->next = g_slist_next(c->next);
1240
a54645448e00 Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents: 1228
diff changeset
284 if (!utf8_mode) {
a54645448e00 Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents: 1228
diff changeset
285 c->len_compl = strlen(r);
a54645448e00 Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents: 1228
diff changeset
286 } else {
a54645448e00 Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents: 1228
diff changeset
287 char *wc;
a54645448e00 Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents: 1228
diff changeset
288 c->len_compl = 0;
a54645448e00 Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents: 1228
diff changeset
289 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
290 c->len_compl++;
1240
a54645448e00 Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents: 1228
diff changeset
291 }
94
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
292 return r;
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
293 }
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
294
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
295
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
296 /* Categories functions */
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
297
2042
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
298 static gint compl_sort_forward(gconstpointer a, gconstpointer b)
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
299 {
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
300 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
301 }
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
302
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
303 static gint compl_sort_reverse(gconstpointer a, gconstpointer b)
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
304 {
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
305 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
306 }
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
307
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
308 static gint compl_sort_append(gconstpointer a, gconstpointer b)
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
309 {
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
310 return 1;
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
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
313 static gint compl_sort_prepend(gconstpointer a, gconstpointer b)
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 return -1;
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
95
9e6b7897ec37 [/trunk] Changeset 109 by mikael
mikael
parents: 94
diff changeset
318 // compl_add_category_word(categ, command)
9e6b7897ec37 [/trunk] Changeset 109 by mikael
mikael
parents: 94
diff changeset
319 // 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
320 void compl_add_category_word(guint categ, const gchar *word)
94
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
321 {
121
94b251102069 [/trunk] Changeset 134 by mikael
mikael
parents: 104
diff changeset
322 char *nword;
1922
4ba68ad737bc Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents: 1852
diff changeset
323
4ba68ad737bc Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents: 1852
diff changeset
324 if (!categ) {
2042
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
325 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
326 "Invalid category (0).");
1922
4ba68ad737bc Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents: 1852
diff changeset
327 return;
4ba68ad737bc Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents: 1852
diff changeset
328 }
4ba68ad737bc Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents: 1852
diff changeset
329
4ba68ad737bc Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents: 1852
diff changeset
330 categ--;
4ba68ad737bc Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents: 1852
diff changeset
331
2042
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
332 if ((categ >= num_categories) ||
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
333 !(Categories[categ].flags & COMPL_CAT_ACTIVE)) {
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
334 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
335 "Category does not exist.");
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
336 return;
94
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
337 }
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
338
121
94b251102069 [/trunk] Changeset 134 by mikael
mikael
parents: 104
diff changeset
339 // If word is not space-terminated, we add one trailing space
94b251102069 [/trunk] Changeset 134 by mikael
mikael
parents: 104
diff changeset
340 for (nword = (char*)word; *nword; nword++)
94b251102069 [/trunk] Changeset 134 by mikael
mikael
parents: 104
diff changeset
341 ;
94b251102069 [/trunk] Changeset 134 by mikael
mikael
parents: 104
diff changeset
342 if (nword > word) nword--;
94b251102069 [/trunk] Changeset 134 by mikael
mikael
parents: 104
diff changeset
343 if (*nword != ' ') { // Add a space
1240
a54645448e00 Fix completion of strings with multibyte chars
Mikael Berthe <mikael@lilotux.net>
parents: 1228
diff changeset
344 nword = g_strdup_printf("%s ", word);
121
94b251102069 [/trunk] Changeset 134 by mikael
mikael
parents: 104
diff changeset
345 } else { // word is fine
94b251102069 [/trunk] Changeset 134 by mikael
mikael
parents: 104
diff changeset
346 nword = g_strdup(word);
94b251102069 [/trunk] Changeset 134 by mikael
mikael
parents: 104
diff changeset
347 }
94b251102069 [/trunk] Changeset 134 by mikael
mikael
parents: 104
diff changeset
348
2042
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
349 if (g_slist_find_custom(Categories[categ].words, nword,
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
350 (GCompareFunc)g_strcmp0) == NULL) {
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
351 guint flags = Categories[categ].flags;
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
352 GCompareFunc comparator = compl_sort_forward;
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
353 if (flags & COMPL_CAT_NOSORT) {
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
354 if (flags & COMPL_CAT_REVERSE)
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
355 comparator = compl_sort_prepend;
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
356 else
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
357 comparator = compl_sort_append;
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
358 } else if (flags & COMPL_CAT_REVERSE)
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
359 comparator = compl_sort_reverse;
1850
8d7810f529db Sort items in completion lists
alkino <nicolas.cornu@ensi-bourges.fr>
parents: 1828
diff changeset
360
2042
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
361 Categories[categ].words = g_slist_insert_sorted
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
362 (Categories[categ].words, nword, comparator);
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
363 }
94
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
364 }
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
365
284
f879b17ecb8e Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents: 225
diff changeset
366 // compl_del_category_word(categ, command)
f879b17ecb8e Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents: 225
diff changeset
367 // 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
368 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
369 {
2042
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
370 GSList *wel;
284
f879b17ecb8e Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents: 225
diff changeset
371 char *nword;
1922
4ba68ad737bc Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents: 1852
diff changeset
372
4ba68ad737bc Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents: 1852
diff changeset
373 if (!categ) {
2042
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
374 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
375 "Invalid category (0).");
1922
4ba68ad737bc Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents: 1852
diff changeset
376 return;
4ba68ad737bc Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents: 1852
diff changeset
377 }
4ba68ad737bc Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents: 1852
diff changeset
378
4ba68ad737bc Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents: 1852
diff changeset
379 categ--;
4ba68ad737bc Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents: 1852
diff changeset
380
2042
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
381 if ((categ >= num_categories) ||
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
382 !(Categories[categ].flags & COMPL_CAT_ACTIVE)) {
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
383 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
384 "Category does not exist.");
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
385 return;
284
f879b17ecb8e Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents: 225
diff changeset
386 }
f879b17ecb8e Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents: 225
diff changeset
387
f879b17ecb8e Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents: 225
diff changeset
388 // 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
389 for (nword = (char*)word; *nword; nword++)
f879b17ecb8e Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents: 225
diff changeset
390 ;
f879b17ecb8e Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents: 225
diff changeset
391 if (nword > word) nword--;
2042
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
392 if (*nword != ' ') // Add a space
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
393 word = nword = g_strdup_printf("%s ", word);
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
394 else
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
395 nword = NULL;
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
396
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
397 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
398 if (!strcasecmp((char*)wel->data, word)) {
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
399 g_free(wel->data);
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
400 Categories[categ].words = g_slist_delete_link
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
401 (Categories[categ].words, wel);
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
402 break; // Only remove first occurence
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
403 }
284
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
2042
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
406 g_free (nword);
284
f879b17ecb8e Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents: 225
diff changeset
407 }
f879b17ecb8e Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents: 225
diff changeset
408
95
9e6b7897ec37 [/trunk] Changeset 109 by mikael
mikael
parents: 94
diff changeset
409 // compl_get_category_list()
1922
4ba68ad737bc Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents: 1852
diff changeset
410 // 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
411 // 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
412 // whole list after use.
1922
4ba68ad737bc Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents: 1852
diff changeset
413 GSList *compl_get_category_list(guint categ, guint *dynlist)
94
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
414 {
1922
4ba68ad737bc Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents: 1852
diff changeset
415 if (!categ) {
2042
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
416 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
417 "Invalid category (0).");
1922
4ba68ad737bc Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents: 1852
diff changeset
418 return NULL;
4ba68ad737bc Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents: 1852
diff changeset
419 }
4ba68ad737bc Increase the number of available categories for completions
Mikael Berthe <mikael@lilotux.net>
parents: 1852
diff changeset
420
2042
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
421 categ --;
94
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
422
2042
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
423 if ((categ > num_categories) ||
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
424 !(Categories[categ].flags & COMPL_CAT_ACTIVE)) {
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
425 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
426 "Category does not exist.");
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
427 return NULL;
757
ae23c8826efb Improve completion for the "/event" command
Mikael Berthe <mikael@lilotux.net>
parents: 735
diff changeset
428 }
225
d5ae42cbe1fa [/trunk] Changeset 237 by mikael
mikael
parents: 121
diff changeset
429
2042
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
430 if (Categories[categ].flags & COMPL_CAT_DYNAMIC) {
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
431 *dynlist = TRUE;
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
432 return (*Categories[categ].dynamic) ();
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
433 } else {
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
434 *dynlist = FALSE;
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
435 return Categories[categ].words;
0cb8ea02e472 Make completion sorting order configurable
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2035
diff changeset
436 }
94
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
437 }
9a4aa2797f02 [/trunk] Changeset 108 by mikael
mikael
parents:
diff changeset
438
1811
e6d355e50d7a Update Vim modelines
Mikael Berthe <mikael@lilotux.net>
parents: 1756
diff changeset
439 /* vim: set expandtab cindent cinoptions=>2\:2(0 sw=2 ts=2: For Vim users... */