comparison mcabber/src/compl.c @ 94:9a4aa2797f02

[/trunk] Changeset 108 by mikael * Add completion functions. (Not integrated to the UI yet)
author mikael
date Tue, 19 Apr 2005 20:09:54 +0000
parents
children 9e6b7897ec37
comparison
equal deleted inserted replaced
93:b3618cb3bf65 94:9a4aa2797f02
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"
35
36 // Completion structure
37 typedef struct {
38 GSList *list; // list of matches
39 guint len_prefix; // length of text already typed by the user
40 guint len_compl; // length of the last completion
41 GSList *next; // pointer to next completion to try
42 } compl;
43
44 // Category structure
45 typedef struct {
46 guint flag;
47 GSList *words;
48 } category;
49
50 static GSList *Categories;
51 static compl *InputCompl;
52
53 // XXX Should not be there (?)
54 // jid_list(type)
55 // Returns a list of jid's. If type is COMPL_URLJID, urls are surrounded with
56 // '<' and '>'.
57 GSList *jid_list(guint type) // bool urlstyle?
58 {
59 }
60
61 // new_completion(prefix, compl_cat)
62 // . prefix = beginning of the word, typed by the user
63 // . compl_cat = pointer to a completion category list (list of *char)
64 // Returns a pointer to an allocated compl structure. This structure should
65 // be freed by the caller when not used anymore.
66 //compl *new_completion(char *prefix, GSList *compl_cat)
67 void new_completion(char *prefix, GSList *compl_cat)
68 {
69 compl *c;
70 GSList *sl_cat;
71 int len = strlen(prefix);
72
73 if (InputCompl) { // This should not happen, but hey...
74 cancel_completion();
75 }
76
77 c = g_new0(compl, 1);
78 // Build the list of matches
79 for (sl_cat=compl_cat; sl_cat; sl_cat = g_slist_next(sl_cat)) {
80 char *word = sl_cat->data;
81 if (!strncmp(prefix, word, len)) {
82 c->list = g_slist_append(c->list, g_strdup(word+len)); // TODO sort
83 }
84 }
85 c->next = c->list;
86 InputCompl = c;
87 }
88
89 // done_completion();
90 void done_completion(void)
91 {
92 // TODO free everything
93 g_slist_free(InputCompl->list);
94 g_free(InputCompl);
95 InputCompl = NULL;
96 }
97
98 // cancel_completion()
99 // Returns the number of chars to delete to cancel the completion
100 //guint cancel_completion(compl *c)
101 guint cancel_completion(void)
102 {
103 return InputCompl->len_compl;
104 }
105
106 // Returns pointer to text to insert, NULL if no completion.
107 const char *complete()
108 {
109 compl* c = InputCompl;
110 char *r;
111 if (!c->next) {
112 c->next = c->list; // back to the beginning
113 c->len_compl = 0;
114 return NULL;
115 }
116 r = (char*)c->next->data;
117 c->next = g_slist_next(c->next);
118 c->len_compl = strlen(r);
119 return r;
120 }
121
122
123 /* Categories functions */
124
125 // compl_add_category_command(categ, command)
126 // Adds command as a possible completion in category categ.
127 void compl_add_category_command(guint categ, char *command)
128 {
129 GSList *sl_cat;
130 category *cat;
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
143 // TODO Check word does not already exist
144 cat->words = g_slist_append(cat->words, g_strdup(command)); // TODO sort
145 }
146
147 GSList *compl_get_category_list(guint cat_flags)
148 {
149 GSList *sl_cat;
150 // Look for category
151 // XXX Actually that's not that simple... cat_flags can be a combination
152 // of several flags!
153 for (sl_cat=Categories; sl_cat; sl_cat = g_slist_next(sl_cat)) {
154 if (cat_flags == ((category*)sl_cat->data)->flag)
155 break;
156 }
157 if (sl_cat) // Category was found, easy...
158 return ((category*)sl_cat->data)->words;
159
160 // TODO handle dynamic SLists :)
161 return NULL;
162 }
163
164
165 /*
166 void test_dump_categories(void)
167 {
168 GSList *sl_cat;
169 // Look for category
170 for (sl_cat=Categories; sl_cat; sl_cat = g_slist_next(sl_cat)) {
171 GSList *sl_words;
172 category *cat = (category*)sl_cat->data;
173 printf("Category: %u\n", cat->flag);
174 for (sl_words=cat->words; sl_words; sl_words = g_slist_next(sl_words)) {
175 char *word = sl_words->data;
176 printf(" Keyword: [%s]\n", word);
177 }
178 }
179 }
180
181 void test_comp(guint cat_flags, char *prefix)
182 {
183 GSList *list = compl_get_category_list(cat_flags);
184 if (list) {
185 const char *cchar;
186 guint back;
187 int i;
188 new_completion(prefix, list);
189 cchar = complete();
190 printf("Complete -> \"%s\"\n", cchar);
191 for (i=0 ; i<5; i++) {
192 back = cancel_completion();
193 printf("Back: %d\n", back);
194 cchar = complete();
195 printf("Complete -> \"%s\"\n", cchar);
196 }
197 back = cancel_completion();
198 printf("Back: %d\n", back);
199 done_completion();
200 }
201 }
202
203 int main()
204 {
205 compl_add_category_command(COMPL_CMD, "add");
206 compl_add_category_command(COMPL_CMD, "clear");
207 compl_add_category_command(COMPL_CMD, "del");
208 compl_add_category_command(COMPL_CMD, "group");
209 compl_add_category_command(COMPL_CMD, "info");
210 compl_add_category_command(COMPL_CMD, "move");
211 compl_add_category_command(COMPL_CMD, "rename");
212 compl_add_category_command(COMPL_CMD, "request_auth");
213 compl_add_category_command(COMPL_CMD, "say");
214 compl_add_category_command(COMPL_CMD, "search");
215 compl_add_category_command(COMPL_CMD, "send_auth");
216 compl_add_category_command(COMPL_CMD, "status");
217 compl_add_category_command(COMPL_STATUS, "online");
218 compl_add_category_command(COMPL_STATUS, "avail");
219 compl_add_category_command(COMPL_STATUS, "invisible");
220 compl_add_category_command(COMPL_STATUS, "free");
221 compl_add_category_command(COMPL_STATUS, "dnd");
222 compl_add_category_command(COMPL_STATUS, "busy");
223 compl_add_category_command(COMPL_STATUS, "notavail");
224 compl_add_category_command(COMPL_STATUS, "away");
225
226 //test_dump_categories();
227
228 test_comp(COMPL_STATUS, "d");
229 test_comp(COMPL_CMD, "s");
230
231 return 0;
232 }
233 */