Mercurial > hg
annotate mcabber/src/compl.c @ 308:70914672c8e7 0.6.3
Ready for 0.6.3 release
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Tue, 12 Jul 2005 17:36:06 +0100 |
parents | f879b17ecb8e |
children | f8f3c7493457 |
rev | line source |
---|---|
94 | 1 /* |
2 * compl.c -- Completion system | |
3 * | |
4 * Copyright (C) 2005 Mikael Berthe <bmikael@lists.lilotux.net> | |
5 * | |
6 * This program is free software; you can redistribute it and/or modify | |
7 * it under the terms of the GNU General Public License as published by | |
8 * the Free Software Foundation; either version 2 of the License, or (at | |
9 * your option) any later version. | |
10 * | |
11 * This program is distributed in the hope that it will be useful, but | |
12 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 * General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU General Public License | |
17 * along with this program; if not, write to the Free Software | |
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
19 * USA | |
20 */ | |
21 | |
22 /* Usage, basically: | |
23 * - new_completion(); // 1. Initialization | |
24 * - complete(); // 2. 1st completion | |
25 * - cancel_completion(); // 3a. 2nd completion / cancel previous | |
26 * - complete(); // 3b. 2nd completion / complete | |
27 * ... | |
28 * - done_completion(); // n. finished -- free allocated areas | |
29 * | |
30 */ | |
31 | |
32 #include <string.h> | |
33 | |
34 #include "compl.h" | |
225 | 35 #include "roster.h" |
94 | 36 |
37 // Completion structure | |
38 typedef struct { | |
39 GSList *list; // list of matches | |
40 guint len_prefix; // length of text already typed by the user | |
41 guint len_compl; // length of the last completion | |
42 GSList *next; // pointer to next completion to try | |
43 } compl; | |
44 | |
45 // Category structure | |
46 typedef struct { | |
47 guint flag; | |
48 GSList *words; | |
49 } category; | |
50 | |
51 static GSList *Categories; | |
52 static compl *InputCompl; | |
53 | |
54 // new_completion(prefix, compl_cat) | |
55 // . prefix = beginning of the word, typed by the user | |
56 // . compl_cat = pointer to a completion category list (list of *char) | |
57 // Returns a pointer to an allocated compl structure. This structure should | |
58 // be freed by the caller when not used anymore. | |
59 void new_completion(char *prefix, GSList *compl_cat) | |
60 { | |
61 compl *c; | |
62 GSList *sl_cat; | |
63 int len = strlen(prefix); | |
64 | |
65 if (InputCompl) { // This should not happen, but hey... | |
66 cancel_completion(); | |
67 } | |
68 | |
69 c = g_new0(compl, 1); | |
70 // Build the list of matches | |
71 for (sl_cat=compl_cat; sl_cat; sl_cat = g_slist_next(sl_cat)) { | |
72 char *word = sl_cat->data; | |
104 | 73 if (!strncasecmp(prefix, word, len)) { |
98 | 74 if (strlen(word) != len) |
75 c->list = g_slist_append(c->list, g_strdup(word+len)); // TODO sort | |
94 | 76 } |
77 } | |
78 c->next = c->list; | |
79 InputCompl = c; | |
80 } | |
81 | |
82 // done_completion(); | |
83 void done_completion(void) | |
84 { | |
98 | 85 if (!InputCompl) return; |
86 | |
94 | 87 // TODO free everything |
88 g_slist_free(InputCompl->list); | |
89 g_free(InputCompl); | |
90 InputCompl = NULL; | |
91 } | |
92 | |
93 // cancel_completion() | |
94 // Returns the number of chars to delete to cancel the completion | |
95 //guint cancel_completion(compl *c) | |
96 guint cancel_completion(void) | |
97 { | |
98 | 98 if (!InputCompl) return 0; |
94 | 99 return InputCompl->len_compl; |
100 } | |
101 | |
102 // Returns pointer to text to insert, NULL if no completion. | |
103 const char *complete() | |
104 { | |
105 compl* c = InputCompl; | |
106 char *r; | |
98 | 107 |
108 if (!InputCompl) return NULL; | |
109 | |
94 | 110 if (!c->next) { |
111 c->next = c->list; // back to the beginning | |
112 c->len_compl = 0; | |
113 return NULL; | |
114 } | |
115 r = (char*)c->next->data; | |
116 c->next = g_slist_next(c->next); | |
117 c->len_compl = strlen(r); | |
118 return r; | |
119 } | |
120 | |
121 | |
122 /* Categories functions */ | |
123 | |
95 | 124 // compl_add_category_word(categ, command) |
125 // Adds a keyword as a possible completion in category categ. | |
126 void compl_add_category_word(guint categ, const char *word) | |
94 | 127 { |
128 GSList *sl_cat; | |
129 category *cat; | |
121 | 130 char *nword; |
94 | 131 // Look for category |
132 for (sl_cat=Categories; sl_cat; sl_cat = g_slist_next(sl_cat)) { | |
133 if (categ == ((category*)sl_cat->data)->flag) | |
134 break; | |
135 } | |
136 if (!sl_cat) { // Category not found, let's create it | |
137 cat = g_new0(category, 1); | |
138 cat->flag = categ; | |
139 Categories = g_slist_append(Categories, cat); | |
140 } else | |
141 cat = (category*)sl_cat->data; | |
142 | |
121 | 143 // If word is not space-terminated, we add one trailing space |
144 for (nword = (char*)word; *nword; nword++) | |
145 ; | |
146 if (nword > word) nword--; | |
147 if (*nword != ' ') { // Add a space | |
148 nword = g_new(char, strlen(word)+2); | |
149 strcpy(nword, word); | |
150 strcat(nword, " "); | |
151 } else { // word is fine | |
152 nword = g_strdup(word); | |
153 } | |
154 | |
94 | 155 // TODO Check word does not already exist |
121 | 156 cat->words = g_slist_append(cat->words, nword); // TODO sort |
94 | 157 } |
158 | |
284
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
159 // compl_del_category_word(categ, command) |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
160 // Removes a keyword from category categ in completion list. |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
161 void compl_del_category_word(guint categ, const char *word) |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
162 { |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
163 GSList *sl_cat, *sl_elt; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
164 category *cat; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
165 char *nword; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
166 // Look for category |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
167 for (sl_cat=Categories; sl_cat; sl_cat = g_slist_next(sl_cat)) { |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
168 if (categ == ((category*)sl_cat->data)->flag) |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
169 break; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
170 } |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
171 if (!sl_cat) return; // Category not found, finished! |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
172 |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
173 cat = (category*)sl_cat->data; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
174 |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
175 // 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
|
176 for (nword = (char*)word; *nword; nword++) |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
177 ; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
178 if (nword > word) nword--; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
179 if (*nword != ' ') { // Add a space |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
180 nword = g_new(char, strlen(word)+2); |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
181 strcpy(nword, word); |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
182 strcat(nword, " "); |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
183 } else { // word is fine |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
184 nword = g_strdup(word); |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
185 } |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
186 |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
187 sl_elt = cat->words; |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
188 while (sl_elt) { |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
189 if (!strcasecmp((char*)sl_elt->data, nword)) { |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
190 g_free(sl_elt->data); |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
191 cat->words = g_slist_delete_link(cat->words, sl_elt); |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
192 break; // Only remove first occurence |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
193 } |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
194 sl_elt = g_slist_next(sl_elt); |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
195 } |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
196 } |
f879b17ecb8e
Add compl_del_category_word()
Mikael Berthe <mikael@lilotux.net>
parents:
225
diff
changeset
|
197 |
95 | 198 // compl_get_category_list() |
199 // Returns a slist of all words in the categories specified by the given flags | |
94 | 200 GSList *compl_get_category_list(guint cat_flags) |
201 { | |
202 GSList *sl_cat; | |
203 // Look for category | |
204 // XXX Actually that's not that simple... cat_flags can be a combination | |
205 // of several flags! | |
206 for (sl_cat=Categories; sl_cat; sl_cat = g_slist_next(sl_cat)) { | |
207 if (cat_flags == ((category*)sl_cat->data)->flag) | |
208 break; | |
209 } | |
210 if (sl_cat) // Category was found, easy... | |
211 return ((category*)sl_cat->data)->words; | |
212 | |
225 | 213 // Handle dynamic SLists |
214 if (cat_flags == COMPL_GROUPNAME) { | |
215 return compl_list(ROSTER_TYPE_GROUP); | |
216 } | |
217 if (cat_flags == COMPL_JID) { | |
218 return compl_list(ROSTER_TYPE_USER); | |
219 } | |
220 | |
94 | 221 return NULL; |
222 } | |
223 |