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 |
95
|
159 // compl_get_category_list() |
|
160 // Returns a slist of all words in the categories specified by the given flags |
94
|
161 GSList *compl_get_category_list(guint cat_flags) |
|
162 { |
|
163 GSList *sl_cat; |
|
164 // Look for category |
|
165 // XXX Actually that's not that simple... cat_flags can be a combination |
|
166 // of several flags! |
|
167 for (sl_cat=Categories; sl_cat; sl_cat = g_slist_next(sl_cat)) { |
|
168 if (cat_flags == ((category*)sl_cat->data)->flag) |
|
169 break; |
|
170 } |
|
171 if (sl_cat) // Category was found, easy... |
|
172 return ((category*)sl_cat->data)->words; |
|
173 |
225
|
174 // Handle dynamic SLists |
|
175 if (cat_flags == COMPL_GROUPNAME) { |
|
176 return compl_list(ROSTER_TYPE_GROUP); |
|
177 } |
|
178 if (cat_flags == COMPL_JID) { |
|
179 return compl_list(ROSTER_TYPE_USER); |
|
180 } |
|
181 |
94
|
182 return NULL; |
|
183 } |
|
184 |