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"
|
|
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;
|
104
|
81 if (!strncasecmp(prefix, word, len)) {
|
98
|
82 if (strlen(word) != len)
|
|
83 c->list = g_slist_append(c->list, g_strdup(word+len)); // TODO sort
|
94
|
84 }
|
|
85 }
|
|
86 c->next = c->list;
|
|
87 InputCompl = c;
|
|
88 }
|
|
89
|
|
90 // done_completion();
|
|
91 void done_completion(void)
|
|
92 {
|
98
|
93 if (!InputCompl) return;
|
|
94
|
94
|
95 // TODO free everything
|
|
96 g_slist_free(InputCompl->list);
|
|
97 g_free(InputCompl);
|
|
98 InputCompl = NULL;
|
|
99 }
|
|
100
|
|
101 // cancel_completion()
|
|
102 // Returns the number of chars to delete to cancel the completion
|
|
103 //guint cancel_completion(compl *c)
|
|
104 guint cancel_completion(void)
|
|
105 {
|
98
|
106 if (!InputCompl) return 0;
|
94
|
107 return InputCompl->len_compl;
|
|
108 }
|
|
109
|
|
110 // Returns pointer to text to insert, NULL if no completion.
|
|
111 const char *complete()
|
|
112 {
|
|
113 compl* c = InputCompl;
|
|
114 char *r;
|
98
|
115
|
|
116 if (!InputCompl) return NULL;
|
|
117
|
94
|
118 if (!c->next) {
|
|
119 c->next = c->list; // back to the beginning
|
|
120 c->len_compl = 0;
|
|
121 return NULL;
|
|
122 }
|
|
123 r = (char*)c->next->data;
|
|
124 c->next = g_slist_next(c->next);
|
|
125 c->len_compl = strlen(r);
|
|
126 return r;
|
|
127 }
|
|
128
|
|
129
|
|
130 /* Categories functions */
|
|
131
|
95
|
132 // compl_add_category_word(categ, command)
|
|
133 // Adds a keyword as a possible completion in category categ.
|
|
134 void compl_add_category_word(guint categ, const char *word)
|
94
|
135 {
|
|
136 GSList *sl_cat;
|
|
137 category *cat;
|
121
|
138 char *nword;
|
94
|
139 // Look for category
|
|
140 for (sl_cat=Categories; sl_cat; sl_cat = g_slist_next(sl_cat)) {
|
|
141 if (categ == ((category*)sl_cat->data)->flag)
|
|
142 break;
|
|
143 }
|
|
144 if (!sl_cat) { // Category not found, let's create it
|
|
145 cat = g_new0(category, 1);
|
|
146 cat->flag = categ;
|
|
147 Categories = g_slist_append(Categories, cat);
|
|
148 } else
|
|
149 cat = (category*)sl_cat->data;
|
|
150
|
121
|
151 // If word is not space-terminated, we add one trailing space
|
|
152 for (nword = (char*)word; *nword; nword++)
|
|
153 ;
|
|
154 if (nword > word) nword--;
|
|
155 if (*nword != ' ') { // Add a space
|
|
156 nword = g_new(char, strlen(word)+2);
|
|
157 strcpy(nword, word);
|
|
158 strcat(nword, " ");
|
|
159 } else { // word is fine
|
|
160 nword = g_strdup(word);
|
|
161 }
|
|
162
|
94
|
163 // TODO Check word does not already exist
|
121
|
164 cat->words = g_slist_append(cat->words, nword); // TODO sort
|
94
|
165 }
|
|
166
|
95
|
167 // compl_get_category_list()
|
|
168 // Returns a slist of all words in the categories specified by the given flags
|
94
|
169 GSList *compl_get_category_list(guint cat_flags)
|
|
170 {
|
|
171 GSList *sl_cat;
|
|
172 // Look for category
|
|
173 // XXX Actually that's not that simple... cat_flags can be a combination
|
|
174 // of several flags!
|
|
175 for (sl_cat=Categories; sl_cat; sl_cat = g_slist_next(sl_cat)) {
|
|
176 if (cat_flags == ((category*)sl_cat->data)->flag)
|
|
177 break;
|
|
178 }
|
|
179 if (sl_cat) // Category was found, easy...
|
|
180 return ((category*)sl_cat->data)->words;
|
|
181
|
|
182 // TODO handle dynamic SLists :)
|
|
183 return NULL;
|
|
184 }
|
|
185
|