This source file includes following definitions.
- edit_collect_completions_get_current_word
- edit_collect_completion_from_one_buffer
- edit_collect_completions
- edit_complete_word_insert_recoded_completion
- edit_completion_string_free
- edit_completion_dialog_show
- edit_complete_word_cmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 #include <config.h>
27
28 #include <ctype.h>
29 #include <string.h>
30
31 #include "lib/global.h"
32 #include "lib/search.h"
33 #include "lib/strutil.h"
34 #ifdef HAVE_CHARSET
35 #include "lib/charsets.h"
36 #endif
37 #include "lib/tty/tty.h"
38 #include "lib/widget.h"
39
40 #include "src/setup.h"
41
42 #include "editwidget.h"
43 #include "edit-impl.h"
44 #include "editsearch.h"
45
46 #include "editcomplete.h"
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72 static GString *
73 edit_collect_completions_get_current_word (edit_search_status_msg_t *esm, mc_search_t *srch,
74 off_t word_start)
75 {
76 WEdit *edit = esm->edit;
77 gsize len = 0;
78 GString *temp = NULL;
79
80 if (mc_search_run (srch, (void *) esm, word_start, edit->buffer.size, &len))
81 {
82 off_t i;
83
84 for (i = 0; i < (off_t) len; i++)
85 {
86 int chr;
87
88 chr = edit_buffer_get_byte (&edit->buffer, word_start + i);
89 if (!isspace (chr))
90 {
91 if (temp == NULL)
92 temp = g_string_sized_new (len);
93
94 g_string_append_c (temp, chr);
95 }
96 }
97 }
98
99 return temp;
100 }
101
102
103
104
105
106
107 static void
108 edit_collect_completion_from_one_buffer (gboolean active_buffer, GQueue **compl,
109 mc_search_t *srch, edit_search_status_msg_t *esm,
110 off_t word_start, gsize word_len, off_t last_byte,
111 GString *current_word, int *max_width)
112 {
113 GString *temp = NULL;
114 gsize len = 0;
115 off_t start = -1;
116
117 while (mc_search_run (srch, (void *) esm, start + 1, last_byte, &len))
118 {
119 gsize i;
120 int width;
121
122 if (temp == NULL)
123 temp = g_string_sized_new (8);
124 else
125 g_string_set_size (temp, 0);
126
127 start = srch->normal_offset;
128
129
130 for (i = 0; i < len; i++)
131 {
132 int ch;
133
134 ch = edit_buffer_get_byte (&esm->edit->buffer, start + i);
135 if (isspace (ch))
136 continue;
137
138
139 if (start + (off_t) i == word_start)
140 break;
141
142 g_string_append_c (temp, ch);
143 }
144
145 if (temp->len == 0)
146 continue;
147
148 if (current_word != NULL && g_string_equal (current_word, temp))
149 continue;
150
151 if (*compl == NULL)
152 *compl = g_queue_new ();
153 else
154 {
155 GList *l;
156
157 for (l = g_queue_peek_head_link (*compl); l != NULL; l = g_list_next (l))
158 {
159 GString *s = (GString *) l->data;
160
161
162 if (strncmp (s->str + word_len, temp->str + word_len,
163 MAX (len, s->len) - word_len) == 0)
164 break;
165 }
166
167 if (l != NULL)
168 {
169
170
171 if (!active_buffer && l != g_queue_peek_tail_link (*compl))
172 {
173
174 g_queue_unlink (*compl, l);
175 g_queue_push_tail_link (*compl, l);
176 }
177
178 continue;
179 }
180 }
181
182 #ifdef HAVE_CHARSET
183 {
184 GString *recoded;
185
186 recoded = str_nconvert_to_display (temp->str, temp->len);
187 if (recoded != NULL)
188 {
189 if (recoded->len != 0)
190 mc_g_string_copy (temp, recoded);
191
192 g_string_free (recoded, TRUE);
193 }
194 }
195 #endif
196
197 if (active_buffer)
198 g_queue_push_tail (*compl, temp);
199 else
200 g_queue_push_head (*compl, temp);
201
202 start += len;
203
204
205 width = str_term_width1 (temp->str);
206 *max_width = MAX (*max_width, width);
207
208 temp = NULL;
209 }
210
211 if (temp != NULL)
212 g_string_free (temp, TRUE);
213 }
214
215
216
217
218
219
220 static GQueue *
221 edit_collect_completions (WEdit *edit, off_t word_start, gsize word_len,
222 const char *match_expr, int *max_width)
223 {
224 GQueue *compl = NULL;
225 mc_search_t *srch;
226 off_t last_byte;
227 GString *current_word;
228 gboolean entire_file, all_files;
229 edit_search_status_msg_t esm;
230
231 #ifdef HAVE_CHARSET
232 srch = mc_search_new (match_expr, cp_source);
233 #else
234 srch = mc_search_new (match_expr, NULL);
235 #endif
236 if (srch == NULL)
237 return NULL;
238
239 entire_file =
240 mc_config_get_bool (mc_global.main_config, CONFIG_APP_SECTION,
241 "editor_wordcompletion_collect_entire_file", FALSE);
242
243 last_byte = entire_file ? edit->buffer.size : word_start;
244
245 srch->search_type = MC_SEARCH_T_REGEX;
246 srch->is_case_sensitive = TRUE;
247 srch->search_fn = edit_search_cmd_callback;
248 srch->update_fn = edit_search_update_callback;
249
250 esm.first = TRUE;
251 esm.edit = edit;
252 esm.offset = entire_file ? 0 : word_start;
253
254 status_msg_init (STATUS_MSG (&esm), _("Collect completions"), 1.0, simple_status_msg_init_cb,
255 edit_search_status_update_cb, NULL);
256
257 current_word = edit_collect_completions_get_current_word (&esm, srch, word_start);
258
259 *max_width = 0;
260
261
262 edit_collect_completion_from_one_buffer (TRUE, &compl, srch, &esm, word_start, word_len,
263 last_byte, current_word, max_width);
264
265
266 all_files =
267 mc_config_get_bool (mc_global.main_config, CONFIG_APP_SECTION,
268 "editor_wordcompletion_collect_all_files", TRUE);
269 if (all_files)
270 {
271 const WGroup *owner = CONST_GROUP (CONST_WIDGET (edit)->owner);
272 gboolean saved_verbose;
273 GList *w;
274
275
276 saved_verbose = verbose;
277 verbose = FALSE;
278
279 for (w = owner->widgets; w != NULL; w = g_list_next (w))
280 {
281 Widget *ww = WIDGET (w->data);
282 WEdit *e;
283
284 if (!edit_widget_is_editor (ww))
285 continue;
286
287 e = EDIT (ww);
288
289 if (e == edit)
290 continue;
291
292
293 word_start = 0;
294 last_byte = e->buffer.size;
295 esm.edit = e;
296 esm.offset = 0;
297
298 edit_collect_completion_from_one_buffer (FALSE, &compl, srch, &esm, word_start,
299 word_len, last_byte, current_word, max_width);
300 }
301
302 verbose = saved_verbose;
303 }
304
305 status_msg_deinit (STATUS_MSG (&esm));
306 mc_search_free (srch);
307 if (current_word != NULL)
308 g_string_free (current_word, TRUE);
309
310 return compl;
311 }
312
313
314
315
316
317
318
319
320
321
322
323 static void
324 edit_complete_word_insert_recoded_completion (WEdit *edit, char *completion, gsize word_len)
325 {
326 #ifdef HAVE_CHARSET
327 GString *temp;
328
329 temp = str_convert_to_input (completion);
330 if (temp != NULL)
331 {
332 for (completion = temp->str + word_len; *completion != '\0'; completion++)
333 edit_insert (edit, *completion);
334 g_string_free (temp, TRUE);
335 }
336 #else
337 for (completion += word_len; *completion != '\0'; completion++)
338 edit_insert (edit, *completion);
339 #endif
340 }
341
342
343
344 static void
345 edit_completion_string_free (gpointer data)
346 {
347 g_string_free ((GString *) data, TRUE);
348 }
349
350
351
352
353
354
355
356 char *
357 edit_completion_dialog_show (const WEdit *edit, GQueue *compl, int max_width)
358 {
359 const WRect *we = &CONST_WIDGET (edit)->rect;
360 int start_x, start_y, offset;
361 char *curr = NULL;
362 WDialog *compl_dlg;
363 WListbox *compl_list;
364 int compl_dlg_h;
365 int compl_dlg_w;
366 GList *i;
367
368
369 compl_dlg_h = g_queue_get_length (compl) + 2;
370 compl_dlg_w = max_width + 4;
371 start_x = we->x + edit->curs_col + edit->start_col + EDIT_TEXT_HORIZONTAL_OFFSET +
372 (edit->fullscreen ? 0 : 1) + edit_options.line_state_width;
373 start_y = we->y + edit->curs_row + EDIT_TEXT_VERTICAL_OFFSET + (edit->fullscreen ? 0 : 1) + 1;
374
375 if (start_x < 0)
376 start_x = 0;
377 if (start_x < we->x + 1)
378 start_x = we->x + 1 + edit_options.line_state_width;
379 if (compl_dlg_w > COLS)
380 compl_dlg_w = COLS;
381 if (compl_dlg_h > LINES - 2)
382 compl_dlg_h = LINES - 2;
383
384 offset = start_x + compl_dlg_w - COLS;
385 if (offset > 0)
386 start_x -= offset;
387 offset = start_y + compl_dlg_h - LINES;
388 if (offset > 0)
389 start_y -= offset;
390
391
392 compl_dlg =
393 dlg_create (TRUE, start_y, start_x, compl_dlg_h, compl_dlg_w, WPOS_KEEP_DEFAULT, TRUE,
394 dialog_colors, NULL, NULL, "[Completion]", NULL);
395
396
397 compl_list = listbox_new (1, 1, compl_dlg_h - 2, compl_dlg_w - 2, FALSE, NULL);
398
399
400 for (i = g_queue_peek_tail_link (compl); i != NULL; i = g_list_previous (i))
401 listbox_add_item (compl_list, LISTBOX_APPEND_AT_END, 0, ((GString *) i->data)->str, NULL,
402 FALSE);
403
404 group_add_widget (GROUP (compl_dlg), compl_list);
405
406
407 if (dlg_run (compl_dlg) == B_ENTER)
408 {
409 listbox_get_current (compl_list, &curr, NULL);
410 curr = g_strdup (curr);
411 }
412
413
414 widget_destroy (WIDGET (compl_dlg));
415
416 return curr;
417 }
418
419
420
421
422
423
424
425
426 void
427 edit_complete_word_cmd (WEdit *edit)
428 {
429 off_t word_start = 0;
430 gsize word_len = 0;
431 GString *match_expr;
432 gsize i;
433 GQueue *compl;
434 int max_width;
435
436
437 if (!edit_buffer_find_word_start (&edit->buffer, &word_start, &word_len))
438 return;
439
440
441
442 match_expr = g_string_new ("(^|\\s+|\\b)");
443 for (i = 0; i < word_len; i++)
444 g_string_append_c (match_expr, edit_buffer_get_byte (&edit->buffer, word_start + i));
445 g_string_append (match_expr,
446 "[^\\s\\.=\\+\\[\\]\\(\\)\\,\\;\\:\\\"\\'\\-\\?\\/\\|\\\\\\{\\}\\*\\&\\^\\%%\\$#@\\!]+");
447
448
449 compl = edit_collect_completions (edit, word_start, word_len, match_expr->str, &max_width);
450
451 g_string_free (match_expr, TRUE);
452
453 if (compl == NULL)
454 return;
455
456 if (g_queue_get_length (compl) == 1)
457 {
458
459
460 GString *curr_compl;
461
462 curr_compl = (GString *) g_queue_peek_head (compl);
463 edit_complete_word_insert_recoded_completion (edit, curr_compl->str, word_len);
464 }
465 else
466 {
467
468
469 char *curr_compl;
470
471
472 curr_compl = edit_completion_dialog_show (edit, compl, max_width);
473 if (curr_compl != NULL)
474 {
475 edit_complete_word_insert_recoded_completion (edit, curr_compl, word_len);
476 g_free (curr_compl);
477 }
478 }
479
480 g_queue_free_full (compl, edit_completion_string_free);
481 }
482
483