From 300297d8821ce24f562573b35fe09b0e5c93ba98 Mon Sep 17 00:00:00 2001
From: Sebastian Gniazdowski <sgniazdowski@gmail.com>
Date: Wed, 20 Jan 2021 19:52:55 -0600
Subject: Add an option for completing from all buffers.
---
src/editor/edit.c | 1 +
src/editor/edit.h | 1 +
src/editor/editcmd.c | 46 ++++++++++++++++++++++++++++++++----
src/editor/editcmd_dialogs.c | 9 +++++++
src/setup.c | 1 +
5 files changed, 53 insertions(+), 5 deletions(-)
diff --git a/src/editor/edit.c b/src/editor/edit.c
index edda1f832..bada6619c 100644
a
|
b
|
gboolean option_line_state = FALSE; |
90 | 90 | int option_line_state_width = 0; |
91 | 91 | gboolean option_cursor_after_inserted_block = FALSE; |
92 | 92 | gboolean option_state_full_filename = FALSE; |
| 93 | gboolean option_completion_collect_other_files = TRUE; |
93 | 94 | |
94 | 95 | gboolean enable_show_tabs_tws = TRUE; |
95 | 96 | gboolean option_check_nl_at_eof = FALSE; |
diff --git a/src/editor/edit.h b/src/editor/edit.h
index 6c519e9d3..de5645179 100644
a
|
b
|
extern int option_save_mode; |
47 | 47 | extern gboolean option_save_position; |
48 | 48 | extern gboolean option_syntax_highlighting; |
49 | 49 | extern gboolean option_group_undo; |
| 50 | extern gboolean option_completion_collect_other_files; |
50 | 51 | extern char *option_backup_ext; |
51 | 52 | extern char *option_filesize_threshold; |
52 | 53 | extern char *option_stop_format_chars; |
diff --git a/src/editor/editcmd.c b/src/editor/editcmd.c
index 0d2caa923..e77b8a07f 100644
a
|
b
|
gboolean option_drop_selection_on_copy = TRUE; |
96 | 96 | |
97 | 97 | #define TEMP_BUF_LEN 1024 |
98 | 98 | |
99 | | #define MAX_WORD_COMPLETIONS 100 /* in listbox */ |
| 99 | #define MAX_WORD_COMPLETIONS 150 /* in listbox */ |
100 | 100 | |
101 | 101 | /*** file scope type declarations ****************************************************************/ |
102 | 102 | |
… |
… |
edit_collect_completions_get_current_word (edit_search_status_msg_t * esm, mc_se |
1212 | 1212 | |
1213 | 1213 | static gsize |
1214 | 1214 | edit_collect_completions (WEdit * edit, off_t word_start, gsize word_len, |
1215 | | char *match_expr, GString ** compl, gsize * num) |
| 1215 | char *match_expr, GString ** compl, gsize * num, gsize current_limit) |
1216 | 1216 | { |
1217 | 1217 | gsize len = 0; |
1218 | 1218 | gsize max_len = 0; |
… |
… |
edit_collect_completions (WEdit * edit, off_t word_start, gsize word_len, |
1235 | 1235 | |
1236 | 1236 | entire_file = |
1237 | 1237 | mc_config_get_bool (mc_global.main_config, CONFIG_APP_SECTION, |
1238 | | "editor_wordcompletion_collect_entire_file", 0); |
| 1238 | "editor_wordcompletion_collect_entire_file", 1); |
1239 | 1239 | |
1240 | 1240 | last_byte = entire_file ? edit->buffer.size : word_start; |
1241 | 1241 | |
… |
… |
edit_collect_completions (WEdit * edit, off_t word_start, gsize word_len, |
1302 | 1302 | if (skip != 0) |
1303 | 1303 | continue; |
1304 | 1304 | |
1305 | | if (*num == MAX_WORD_COMPLETIONS) |
| 1305 | if (*num >= current_limit) |
1306 | 1306 | { |
1307 | 1307 | g_string_free (compl[0], TRUE); |
1308 | 1308 | for (i = 1; i < *num; i++) |
… |
… |
edit_complete_word_cmd (WEdit * edit) |
3339 | 3339 | GString *match_expr; |
3340 | 3340 | GString *compl[MAX_WORD_COMPLETIONS]; /* completions */ |
3341 | 3341 | |
| 3342 | WEdit *edit_wid; |
| 3343 | const WGroup *gr; |
| 3344 | GList *w; |
| 3345 | |
3342 | 3346 | /* search start of word to be completed */ |
3343 | 3347 | if (!edit_find_word_start (&edit->buffer, &word_start, &word_len)) |
3344 | 3348 | return; |
… |
… |
edit_complete_word_cmd (WEdit * edit) |
3355 | 3359 | /* start search from begin to end of file */ |
3356 | 3360 | max_len = |
3357 | 3361 | edit_collect_completions (edit, word_start, word_len, match_expr->str, (GString **) & compl, |
3358 | | &num_compl); |
| 3362 | &num_compl, MAX_WORD_COMPLETIONS); |
| 3363 | |
| 3364 | /* |
| 3365 | * Should search also other open files ? This «if» collects them up to MAX_WORD_COMPLETIONS |
| 3366 | * limit. |
| 3367 | */ |
| 3368 | if (option_completion_collect_other_files && num_compl < MAX_WORD_COMPLETIONS) |
| 3369 | { |
| 3370 | /* |
| 3371 | * Process all the remaining files by listing the editor widgets |
| 3372 | * grouped in the main editor dialog. |
| 3373 | */ |
| 3374 | gr = CONST_GROUP (CONST_WIDGET (edit)->owner); |
| 3375 | for (w = gr->widgets; w != NULL; w = g_list_next (w)) |
| 3376 | { |
| 3377 | if (w->data != edit && edit_widget_is_editor (CONST_WIDGET (w->data))) |
| 3378 | { |
| 3379 | gsize max_len_candidate, num_compl_sub = 0; |
| 3380 | edit_wid = (WEdit *) w->data; |
| 3381 | max_len_candidate = |
| 3382 | edit_collect_completions (edit_wid, word_start, word_len, match_expr->str, |
| 3383 | ((GString **) & compl) + num_compl, &num_compl_sub, |
| 3384 | MAX_WORD_COMPLETIONS - num_compl); |
| 3385 | |
| 3386 | if (max_len_candidate > max_len) |
| 3387 | max_len = max_len_candidate; |
| 3388 | |
| 3389 | num_compl += num_compl_sub; |
| 3390 | if (num_compl >= MAX_WORD_COMPLETIONS) |
| 3391 | break; |
| 3392 | } |
| 3393 | } |
| 3394 | } |
3359 | 3395 | |
3360 | 3396 | if (num_compl > 0) |
3361 | 3397 | { |
diff --git a/src/editor/editcmd_dialogs.c b/src/editor/editcmd_dialogs.c
index 8b3634f23..3c65331c2 100644
a
|
b
|
editcmd_dialog_completion_show (const WEdit * edit, int max_len, GString ** comp |
385 | 385 | |
386 | 386 | /* create the listbox */ |
387 | 387 | compl_list = listbox_new (1, 1, compl_dlg_h - 2, compl_dlg_w - 2, FALSE, NULL); |
| 388 | compl_list->allow_duplicates = FALSE; |
388 | 389 | |
389 | 390 | /* add the dialog */ |
390 | 391 | group_add_widget (GROUP (compl_dlg), compl_list); |
… |
… |
editcmd_dialog_completion_show (const WEdit * edit, int max_len, GString ** comp |
394 | 395 | listbox_add_item (compl_list, LISTBOX_APPEND_AT_END, 0, (char *) compl[i]->str, NULL, |
395 | 396 | FALSE); |
396 | 397 | |
| 398 | /* Set widgets size after possible duplicates removal. */ |
| 399 | num_compl = listbox_get_length (compl_list); |
| 400 | if (num_compl < compl_dlg_h - 2) |
| 401 | { |
| 402 | WIDGET (compl_list)->lines = num_compl; |
| 403 | WIDGET (compl_dlg)->lines = num_compl + 2; |
| 404 | } |
| 405 | |
397 | 406 | /* pop up the dialog and apply the chosen completion */ |
398 | 407 | if (dlg_run (compl_dlg) == B_ENTER) |
399 | 408 | { |
diff --git a/src/setup.c b/src/setup.c
index 77c07649d..f2f5eb427 100644
a
|
b
|
static const struct |
357 | 357 | { "editor_show_right_margin", &show_right_margin }, |
358 | 358 | { "editor_group_undo", &option_group_undo }, |
359 | 359 | { "editor_state_full_filename", &option_state_full_filename }, |
| 360 | { "editor_wordcompletion_collect_other_files", &option_completion_collect_other_files }, |
360 | 361 | #endif /* USE_INTERNAL_EDIT */ |
361 | 362 | { "editor_ask_filename_before_edit", &editor_ask_filename_before_edit }, |
362 | 363 | { "nice_rotating_dash", &nice_rotating_dash }, |