Extend QUICK_INPUT and QUICK_LABELED_INPUT macros for getting completion flags via...
[midnight-commander.git] / src / editor / editcmd_dialogs.c
blob17f2a8bd130800ab6434304fc709a692aff6c1f5
1 /*
2 Editor dialogs for high level editing commands
4 Copyright (C) 2009, 2011, 2012
5 The Free Software Foundation, Inc.
7 Written by:
8 Slava Zanko <slavazanko@gmail.com>, 2009.
9 Andrew Borodin, <aborodin@vmail.ru>, 2012.
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include <config.h>
29 #include "lib/global.h"
30 #include "lib/tty/tty.h"
31 #include "lib/skin.h" /* INPUT_COLOR */
32 #include "lib/tty/key.h"
33 #include "lib/search.h"
34 #include "lib/strutil.h"
35 #include "lib/widget.h"
36 #ifdef HAVE_CHARSET
37 #include "lib/charsets.h"
38 #endif
40 #include "src/history.h"
42 #include "src/editor/editwidget.h"
43 #include "src/editor/etags.h"
44 #include "src/editor/editcmd_dialogs.h"
46 #ifdef HAVE_ASPELL
47 #include "src/editor/spell.h"
48 #endif
50 /*** global variables ****************************************************************************/
52 edit_search_options_t edit_search_options = {
53 .type = MC_SEARCH_T_NORMAL,
54 .case_sens = FALSE,
55 .backwards = FALSE,
56 .only_in_selection = FALSE,
57 .whole_words = FALSE,
58 .all_codepages = FALSE
61 /*** file scope macro definitions ****************************************************************/
63 /*** file scope type declarations ****************************************************************/
65 /*** file scope variables ************************************************************************/
67 /*** file scope functions ************************************************************************/
68 /* --------------------------------------------------------------------------------------------- */
70 static cb_ret_t
71 editcmd_dialog_raw_key_query_cb (Widget * w, Widget * sender, widget_msg_t msg, int parm,
72 void *data)
74 WDialog *h = DIALOG (w);
76 switch (msg)
78 case MSG_KEY:
79 h->ret_value = parm;
80 dlg_stop (h);
81 return MSG_HANDLED;
82 default:
83 return dlg_default_callback (w, sender, msg, parm, data);
87 /* --------------------------------------------------------------------------------------------- */
88 /*** public functions ****************************************************************************/
89 /* --------------------------------------------------------------------------------------------- */
91 gboolean
92 editcmd_dialog_search_show (WEdit * edit)
94 char *search_text;
95 size_t num_of_types;
96 gchar **list_of_types;
97 int dialog_result;
99 list_of_types = mc_search_get_types_strings_array (&num_of_types);
102 quick_widget_t quick_widgets[] = {
103 /* *INDENT-OFF* */
104 QUICK_LABELED_INPUT (N_("Enter search string:"), input_label_above, INPUT_LAST_TEXT,
105 MC_HISTORY_SHARED_SEARCH, &search_text, NULL, FALSE, FALSE,
106 INPUT_COMPLETE_DEFAULT),
107 QUICK_SEPARATOR (TRUE),
108 QUICK_START_COLUMNS,
109 QUICK_RADIO (num_of_types, (const char **) list_of_types,
110 (int *) &edit_search_options.type, NULL),
111 QUICK_NEXT_COLUMN,
112 QUICK_CHECKBOX (N_("Cas&e sensitive"), &edit_search_options.case_sens, NULL),
113 QUICK_CHECKBOX (N_("&Backwards"), &edit_search_options.backwards, NULL),
114 QUICK_CHECKBOX (N_("In se&lection"), &edit_search_options.only_in_selection, NULL),
115 QUICK_CHECKBOX (N_("&Whole words"), &edit_search_options.whole_words, NULL),
116 #ifdef HAVE_CHARSET
117 QUICK_CHECKBOX (N_("&All charsets"), &edit_search_options.all_codepages, NULL),
118 #endif
119 QUICK_STOP_COLUMNS,
120 QUICK_START_BUTTONS (TRUE, TRUE),
121 QUICK_BUTTON (N_("&OK"), B_ENTER, NULL, NULL),
122 QUICK_BUTTON (N_("&Find all"), B_USER, NULL, NULL),
123 QUICK_BUTTON (N_("&Cancel"), B_CANCEL, NULL, NULL),
124 QUICK_END
125 /* *INDENT-ON* */
128 quick_dialog_t qdlg = {
129 -1, -1, 58,
130 N_("Search"), "[Input Line Keys]",
131 quick_widgets, NULL, NULL
134 dialog_result = quick_dialog (&qdlg);
137 g_strfreev (list_of_types);
139 if ((dialog_result == B_CANCEL) || (search_text == NULL) || (search_text[0] == '\0'))
141 g_free (search_text);
142 return FALSE;
145 if (dialog_result == B_USER)
146 search_create_bookmark = TRUE;
148 #ifdef HAVE_CHARSET
150 GString *tmp;
152 tmp = str_convert_to_input (search_text);
153 if (tmp != NULL)
155 g_free (search_text);
156 search_text = g_string_free (tmp, FALSE);
159 #endif
161 g_free (edit->last_search_string);
162 edit->last_search_string = search_text;
163 mc_search_free (edit->search);
165 edit->search = mc_search_new (edit->last_search_string, -1);
166 if (edit->search != NULL)
168 edit->search->search_type = edit_search_options.type;
169 edit->search->is_all_charsets = edit_search_options.all_codepages;
170 edit->search->is_case_sensitive = edit_search_options.case_sens;
171 edit->search->whole_words = edit_search_options.whole_words;
172 edit->search->search_fn = edit_search_cmd_callback;
175 return (edit->search != NULL);
178 /* --------------------------------------------------------------------------------------------- */
180 void
181 editcmd_dialog_replace_show (WEdit * edit, const char *search_default, const char *replace_default,
182 /*@out@ */ char **search_text, /*@out@ */ char **replace_text)
184 size_t num_of_types;
185 gchar **list_of_types;
187 if ((search_default == NULL) || (*search_default == '\0'))
188 search_default = INPUT_LAST_TEXT;
190 list_of_types = mc_search_get_types_strings_array (&num_of_types);
193 quick_widget_t quick_widgets[] = {
194 /* *INDENT-OFF* */
195 QUICK_LABELED_INPUT (N_("Enter search string:"), input_label_above, search_default,
196 MC_HISTORY_SHARED_SEARCH, search_text, NULL, FALSE, FALSE,
197 INPUT_COMPLETE_DEFAULT),
198 QUICK_LABELED_INPUT (N_("Enter replacement string:"), input_label_above, replace_default,
199 "replace", replace_text, NULL, FALSE, FALSE, INPUT_COMPLETE_DEFAULT),
200 QUICK_SEPARATOR (TRUE),
201 QUICK_START_COLUMNS,
202 QUICK_RADIO (num_of_types, (const char **) list_of_types,
203 (int *) &edit_search_options.type, NULL),
204 QUICK_NEXT_COLUMN,
205 QUICK_CHECKBOX (N_("Cas&e sensitive"), &edit_search_options.case_sens, NULL),
206 QUICK_CHECKBOX (N_("&Backwards"), &edit_search_options.backwards, NULL),
207 QUICK_CHECKBOX (N_("In se&lection"), &edit_search_options.only_in_selection, NULL),
208 QUICK_CHECKBOX (N_("&Whole words"), &edit_search_options.whole_words, NULL),
209 #ifdef HAVE_CHARSET
210 QUICK_CHECKBOX (N_("&All charsets"), &edit_search_options.all_codepages, NULL),
211 #endif
212 QUICK_STOP_COLUMNS,
213 QUICK_BUTTONS_OK_CANCEL,
214 QUICK_END
215 /* *INDENT-ON* */
218 quick_dialog_t qdlg = {
219 -1, -1, 58,
220 N_("Replace"), "[Input Line Keys]",
221 quick_widgets, NULL, NULL
224 if (quick_dialog (&qdlg) != B_CANCEL)
225 edit->replace_mode = 0;
226 else
228 *replace_text = NULL;
229 *search_text = NULL;
233 g_strfreev (list_of_types);
236 /* --------------------------------------------------------------------------------------------- */
239 editcmd_dialog_replace_prompt_show (WEdit * edit, char *from_text, char *to_text, int xpos,
240 int ypos)
242 Widget *w = WIDGET (edit);
244 /* dialog size */
245 int dlg_height = 10;
246 int dlg_width;
248 char tmp[BUF_MEDIUM];
249 char *repl_from, *repl_to;
250 int retval;
252 if (xpos == -1)
253 xpos = w->x + option_line_state_width + 1;
254 if (ypos == -1)
255 ypos = w->y + w->lines / 2;
256 /* Sometimes menu can hide replaced text. I don't like it */
257 if ((edit->curs_row >= ypos - 1) && (edit->curs_row <= ypos + dlg_height - 1))
258 ypos -= dlg_height;
260 dlg_width = WIDGET (w->owner)->cols - xpos - 1;
262 g_snprintf (tmp, sizeof (tmp), "\"%s\"", from_text);
263 repl_from = g_strdup (str_trunc (tmp, dlg_width - 7));
265 g_snprintf (tmp, sizeof (tmp), "\"%s\"", to_text);
266 repl_to = g_strdup (str_trunc (tmp, dlg_width - 7));
269 quick_widget_t quick_widgets[] = {
270 /* *INDENT-OFF* */
271 QUICK_LABEL (repl_from, NULL),
272 QUICK_LABEL (N_("Replace with:"), NULL),
273 QUICK_LABEL (repl_to, NULL),
274 QUICK_START_BUTTONS (TRUE, TRUE),
275 QUICK_BUTTON (N_("&Replace"), B_ENTER, NULL, NULL),
276 QUICK_BUTTON (N_("A&ll"), B_REPLACE_ALL, NULL, NULL),
277 QUICK_BUTTON (N_("&Skip"), B_SKIP_REPLACE, NULL, NULL),
278 QUICK_BUTTON (N_("&Cancel"), B_CANCEL, NULL, NULL),
279 QUICK_END
280 /* *INDENT-ON* */
283 quick_dialog_t qdlg = {
284 ypos, xpos, -1,
285 N_("Confirm replace"), NULL,
286 quick_widgets, NULL, NULL
289 retval = quick_dialog (&qdlg);
292 g_free (repl_from);
293 g_free (repl_to);
295 return retval;
298 /* --------------------------------------------------------------------------------------------- */
299 /* gets a raw key from the keyboard. Passing cancel = 1 draws
300 a cancel button thus allowing c-c etc. Alternatively, cancel = 0
301 will return the next key pressed. ctrl-a (=B_CANCEL), ctrl-g, ctrl-c,
302 and Esc are cannot returned */
305 editcmd_dialog_raw_key_query (const char *heading, const char *query, gboolean cancel)
307 int w, wq;
308 int y = 2;
309 WDialog *raw_dlg;
311 w = str_term_width1 (heading) + 6;
312 wq = str_term_width1 (query);
313 w = max (w, wq + 3 * 2 + 1 + 2);
315 raw_dlg =
316 create_dlg (TRUE, 0, 0, cancel ? 7 : 5, w, dialog_colors, editcmd_dialog_raw_key_query_cb,
317 NULL, NULL, heading, DLG_CENTER | DLG_TRYUP | DLG_WANT_TAB);
319 add_widget (raw_dlg, label_new (y, 3, query));
320 add_widget (raw_dlg, input_new (y++, 3 + wq + 1, input_get_default_colors (),
321 w - (6 + wq + 1), "", 0, INPUT_COMPLETE_DEFAULT));
322 if (cancel)
324 add_widget (raw_dlg, hline_new (y++, -1, -1));
325 /* Button w/o hotkey to allow use any key as raw or macro one */
326 add_widget_autopos (raw_dlg, button_new (y, 1, B_CANCEL, NORMAL_BUTTON, _("Cancel"), NULL),
327 WPOS_KEEP_TOP | WPOS_CENTER_HORZ, NULL);
330 w = run_dlg (raw_dlg);
331 destroy_dlg (raw_dlg);
333 return (cancel && (w == ESC_CHAR || w == B_CANCEL)) ? 0 : w;
336 /* --------------------------------------------------------------------------------------------- */
337 /* let the user select its preferred completion */
339 void
340 editcmd_dialog_completion_show (WEdit * edit, int max_len, int word_len,
341 struct selection *compl, int num_compl)
344 int start_x, start_y, offset, i;
345 char *curr = NULL;
346 WDialog *compl_dlg;
347 WListbox *compl_list;
348 int compl_dlg_h; /* completion dialog height */
349 int compl_dlg_w; /* completion dialog width */
351 /* calculate the dialog metrics */
352 compl_dlg_h = num_compl + 2;
353 compl_dlg_w = max_len + 4;
354 start_x = edit->curs_col + edit->start_col - (compl_dlg_w / 2) +
355 EDIT_TEXT_HORIZONTAL_OFFSET + (edit->fullscreen ? 0 : 1) + option_line_state_width;
356 start_y = edit->curs_row + EDIT_TEXT_VERTICAL_OFFSET + (edit->fullscreen ? 0 : 1) + 1;
358 if (start_x < 0)
359 start_x = 0;
360 if (compl_dlg_w > COLS)
361 compl_dlg_w = COLS;
362 if (compl_dlg_h > LINES - 2)
363 compl_dlg_h = LINES - 2;
365 offset = start_x + compl_dlg_w - COLS;
366 if (offset > 0)
367 start_x -= offset;
368 offset = start_y + compl_dlg_h - LINES;
369 if (offset > 0)
370 start_y -= (offset + 1);
372 /* create the dialog */
373 compl_dlg =
374 create_dlg (TRUE, start_y, start_x, compl_dlg_h, compl_dlg_w,
375 dialog_colors, NULL, NULL, "[Completion]", NULL, DLG_COMPACT);
377 /* create the listbox */
378 compl_list = listbox_new (1, 1, compl_dlg_h - 2, compl_dlg_w - 2, FALSE, NULL);
380 /* add the dialog */
381 add_widget (compl_dlg, compl_list);
383 /* fill the listbox with the completions */
384 for (i = num_compl - 1; i >= 0; i--) /* reverse order */
385 listbox_add_item (compl_list, LISTBOX_APPEND_AT_END, 0, (char *) compl[i].text, NULL);
387 /* pop up the dialog and apply the choosen completion */
388 if (run_dlg (compl_dlg) == B_ENTER)
390 listbox_get_current (compl_list, &curr, NULL);
391 if (curr)
393 #ifdef HAVE_CHARSET
394 GString *temp, *temp2;
395 temp = g_string_new ("");
396 for (curr += word_len; *curr; curr++)
397 g_string_append_c (temp, *curr);
399 temp2 = str_convert_to_input (temp->str);
401 if (temp2 && temp2->len)
403 g_string_free (temp, TRUE);
404 temp = temp2;
406 else
407 g_string_free (temp2, TRUE);
408 for (curr = temp->str; *curr; curr++)
409 edit_insert (edit, *curr);
410 g_string_free (temp, TRUE);
411 #else
412 for (curr += word_len; *curr; curr++)
413 edit_insert (edit, *curr);
414 #endif
418 /* destroy dialog before return */
419 destroy_dlg (compl_dlg);
422 /* --------------------------------------------------------------------------------------------- */
423 /* let the user select where function definition */
425 void
426 editcmd_dialog_select_definition_show (WEdit * edit, char *match_expr, int max_len, int word_len,
427 etags_hash_t * def_hash, int num_lines)
430 int start_x, start_y, offset, i;
431 char *curr = NULL;
432 etags_hash_t *curr_def = NULL;
433 WDialog *def_dlg;
434 WListbox *def_list;
435 int def_dlg_h; /* dialog height */
436 int def_dlg_w; /* dialog width */
437 char *label_def = NULL;
439 (void) word_len;
440 /* calculate the dialog metrics */
441 def_dlg_h = num_lines + 2;
442 def_dlg_w = max_len + 4;
443 start_x = edit->curs_col + edit->start_col - (def_dlg_w / 2) +
444 EDIT_TEXT_HORIZONTAL_OFFSET + (edit->fullscreen ? 0 : 1) + option_line_state_width;
445 start_y = edit->curs_row + EDIT_TEXT_VERTICAL_OFFSET + (edit->fullscreen ? 0 : 1) + 1;
447 if (start_x < 0)
448 start_x = 0;
449 if (def_dlg_w > COLS)
450 def_dlg_w = COLS;
451 if (def_dlg_h > LINES - 2)
452 def_dlg_h = LINES - 2;
454 offset = start_x + def_dlg_w - COLS;
455 if (offset > 0)
456 start_x -= offset;
457 offset = start_y + def_dlg_h - LINES;
458 if (offset > 0)
459 start_y -= (offset + 1);
461 /* create the dialog */
462 def_dlg = create_dlg (TRUE, start_y, start_x, def_dlg_h, def_dlg_w,
463 dialog_colors, NULL, NULL, "[Definitions]", match_expr, DLG_COMPACT);
465 /* create the listbox */
466 def_list = listbox_new (1, 1, def_dlg_h - 2, def_dlg_w - 2, FALSE, NULL);
468 /* add the dialog */
469 add_widget (def_dlg, def_list);
471 /* fill the listbox with the completions */
472 for (i = 0; i < num_lines; i++)
474 label_def =
475 g_strdup_printf ("%s -> %s:%ld", def_hash[i].short_define, def_hash[i].filename,
476 def_hash[i].line);
477 listbox_add_item (def_list, LISTBOX_APPEND_AT_END, 0, label_def, &def_hash[i]);
478 g_free (label_def);
481 /* pop up the dialog and apply the choosen completion */
482 if (run_dlg (def_dlg) == B_ENTER)
484 char *tmp_curr_def = (char *) curr_def;
485 int do_moveto = 0;
487 listbox_get_current (def_list, &curr, (void **) &tmp_curr_def);
488 curr_def = (etags_hash_t *) tmp_curr_def;
489 if (edit->modified)
491 if (!edit_query_dialog2
492 (_("Warning"),
493 _("Current text was modified without a file save.\n"
494 "Continue discards these changes."), _("C&ontinue"), _("&Cancel")))
496 edit->force |= REDRAW_COMPLETELY;
497 do_moveto = 1;
500 else
502 do_moveto = 1;
505 if (curr && do_moveto)
507 if (edit_stack_iterator + 1 < MAX_HISTORY_MOVETO)
509 vfs_path_free (edit_history_moveto[edit_stack_iterator].filename_vpath);
510 if (edit->dir_vpath != NULL)
512 edit_history_moveto[edit_stack_iterator].filename_vpath =
513 vfs_path_append_vpath_new (edit->dir_vpath, edit->filename_vpath, NULL);
515 else
517 edit_history_moveto[edit_stack_iterator].filename_vpath =
518 vfs_path_clone (edit->filename_vpath);
520 edit_history_moveto[edit_stack_iterator].line = edit->start_line +
521 edit->curs_row + 1;
522 edit_stack_iterator++;
523 vfs_path_free (edit_history_moveto[edit_stack_iterator].filename_vpath);
524 edit_history_moveto[edit_stack_iterator].filename_vpath =
525 vfs_path_from_str ((char *) curr_def->fullpath);
526 edit_history_moveto[edit_stack_iterator].line = curr_def->line;
527 edit_reload_line (edit, edit_history_moveto[edit_stack_iterator].filename_vpath,
528 edit_history_moveto[edit_stack_iterator].line);
533 /* clear definition hash */
534 for (i = 0; i < MAX_DEFINITIONS; i++)
536 g_free (def_hash[i].filename);
539 /* destroy dialog before return */
540 destroy_dlg (def_dlg);
543 /* --------------------------------------------------------------------------------------------- */