From 63aa191e7a78244d2783befa900c90582ff3fb32 Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Tue, 16 Feb 2010 23:01:53 +0300 Subject: [PATCH] Ticket #1572: retain search/replace options in editor. Retain search/replace options in editor across editing session. Type accuracy. Signed-off-by: Andrew Borodin --- src/editor/edit-impl.h | 16 +++++++++++++- src/editor/edit-widget.h | 10 ++------- src/editor/editcmd.c | 39 +++++++++++++++++----------------- src/editor/editcmd_dialogs.c | 50 +++++++++++++++++++++++++++++++------------- 4 files changed, 73 insertions(+), 42 deletions(-) diff --git a/src/editor/edit-impl.h b/src/editor/edit-impl.h index 70bfe336..00e65d69 100644 --- a/src/editor/edit-impl.h +++ b/src/editor/edit-impl.h @@ -32,7 +32,9 @@ #include -#include "src/dialog.h" /* cb_ret_t */ +#include "lib/search.h" /* mc_search_type_t */ + +#include "src/dialog.h" /* cb_ret_t */ #include "src/keybind.h" /* global_keymap_t */ #include "src/editor/edit.h" @@ -117,6 +119,16 @@ #define MAX_HISTORY_MOVETO 50 #define LINE_STATE_WIDTH 8 +/* search/replace options */ +typedef struct edit_search_options_t { + mc_search_type_t type; + gboolean case_sens; + gboolean backwards; + gboolean only_in_selection; + gboolean whole_words; + gboolean all_codepages; +} edit_search_options_t; + typedef struct edit_stack_type { long line; char *filename; @@ -299,6 +311,8 @@ void edit_execute_cmd (WEdit *edit, unsigned long command, int char_for_insertio #endif #endif +extern edit_search_options_t edit_search_options; + extern int edit_stack_iterator; extern edit_stack_type edit_history_moveto [MAX_HISTORY_MOVETO]; diff --git a/src/editor/edit-widget.h b/src/editor/edit-widget.h index 0520a32a..493d54ff 100644 --- a/src/editor/edit-widget.h +++ b/src/editor/edit-widget.h @@ -48,16 +48,10 @@ struct WEdit { /* UTF8 */ char charbuf[4 + 1]; int charpoint; - /* search variables */ - mc_search_t *search; - unsigned int search_type; + /* search handler */ + mc_search_t *search; int replace_mode; - int replace_backwards; - int replace_case; - int only_in_selection; - int whole_words; - int all_codepages; long search_start; /* First character to start searching from */ int found_len; /* Length of found string or 0 if none was found */ diff --git a/src/editor/editcmd.c b/src/editor/editcmd.c index eb11f917..4c8354a1 100644 --- a/src/editor/editcmd.c +++ b/src/editor/editcmd.c @@ -1432,14 +1432,14 @@ editcmd_find (WEdit *edit, gsize *len) long end_mark = edit->last_byte; int mark_res = 0; - if (edit->only_in_selection) { + if (edit_search_options.only_in_selection) { mark_res = eval_marks(edit, &start_mark, &end_mark); if (mark_res != 0) { edit->search->error = MC_SEARCH_E_NOTFOUND; edit->search->error_str = g_strdup(_(" Search string not found ")); return FALSE; } - if (edit->replace_backwards) { + if (edit_search_options.backwards) { if (search_start > end_mark || search_start <= start_mark) { search_start = end_mark; } @@ -1449,10 +1449,10 @@ editcmd_find (WEdit *edit, gsize *len) } } } else { - if (edit->replace_backwards) + if (edit_search_options.backwards) end_mark = max(1, edit->curs1) - 1; } - if (edit->replace_backwards) { + if (edit_search_options.backwards) { search_end = end_mark; while ((int) search_start >= start_mark) { if (search_end > search_start + edit->search->original_len @@ -1574,19 +1574,19 @@ edit_replace_cmd (WEdit *edit, int again) edit->search_start = edit->curs1; return; } - edit->search->search_type = edit->search_type; - edit->search->is_all_charsets = edit->all_codepages; - edit->search->is_case_sentitive = edit->replace_case; - edit->search->whole_words = edit->whole_words; + edit->search->search_type = edit_search_options.type; + edit->search->is_all_charsets = edit_search_options.all_codepages; + edit->search->is_case_sentitive = edit_search_options.case_sens; + edit->search->whole_words = edit_search_options.whole_words; edit->search->search_fn = edit_search_cmd_callback; } if (edit->found_len && edit->search_start == edit->found_start + 1 - && edit->replace_backwards) + && edit_search_options.backwards) edit->search_start--; if (edit->found_len && edit->search_start == edit->found_start - 1 - && !edit->replace_backwards) + && !edit_search_options.backwards) edit->search_start++; do { @@ -1670,7 +1670,7 @@ edit_replace_cmd (WEdit *edit, int again) edit->found_len = i; } /* so that we don't find the same string again */ - if (edit->replace_backwards) { + if (edit_search_options.backwards) { last_search = edit->search_start; edit->search_start--; } else { @@ -1776,30 +1776,31 @@ void edit_search_cmd (WEdit * edit, int again) edit->search_start = edit->curs1; return; } - edit->search->search_type = edit->search_type; - edit->search->is_all_charsets = edit->all_codepages; - edit->search->is_case_sentitive = edit->replace_case; - edit->search->whole_words = edit->whole_words; + edit->search->search_type = edit_search_options.type; + edit->search->is_all_charsets = edit_search_options.all_codepages; + edit->search->is_case_sentitive = edit_search_options.case_sens; + edit->search->whole_words = edit_search_options.whole_words; edit->search->search_fn = edit_search_cmd_callback; } if (search_create_bookmark) { edit_search_cmd_search_create_bookmark(edit); } else { - if (edit->found_len && edit->search_start == edit->found_start + 1 && edit->replace_backwards) + if (edit->found_len && edit->search_start == edit->found_start + 1 + && edit_search_options.backwards) edit->search_start--; - if (edit->found_len && edit->search_start == edit->found_start - 1 && !edit->replace_backwards) + if (edit->found_len && edit->search_start == edit->found_start - 1 + && !edit_search_options.backwards) edit->search_start++; - if (editcmd_find(edit, &len)) { edit->found_start = edit->search_start = edit->search->normal_offset; edit->found_len = len; edit->over_col = 0; edit_cursor_move (edit, edit->search_start - edit->curs1); edit_scroll_screen_over_cursor (edit); - if (edit->replace_backwards) + if (edit_search_options.backwards) edit->search_start--; else edit->search_start++; diff --git a/src/editor/editcmd_dialogs.c b/src/editor/editcmd_dialogs.c index d30ea949..7851e58c 100644 --- a/src/editor/editcmd_dialogs.c +++ b/src/editor/editcmd_dialogs.c @@ -47,6 +47,16 @@ /*** global variables **************************************************/ +edit_search_options_t edit_search_options = +{ + .type = MC_SEARCH_T_NORMAL, + .case_sens = FALSE, + .backwards = FALSE, + .only_in_selection = FALSE, + .whole_words = FALSE, + .all_codepages = FALSE +}; + /*** file scope macro definitions **************************************/ #define SEARCH_DLG_WIDTH 58 @@ -61,7 +71,6 @@ /*** file scope variables **********************************************/ - /*** file scope functions **********************************************/ static cb_ret_t @@ -97,14 +106,20 @@ editcmd_dialog_replace_show (WEdit * edit, const char *search_default, const cha /* 0 */ QUICK_BUTTON (6, 10, 13, REPLACE_DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL), /* 1 */ QUICK_BUTTON (2, 10, 13, REPLACE_DLG_HEIGHT, N_("&OK"), B_ENTER, NULL), #ifdef HAVE_CHARSET - /* 2 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH, 11, REPLACE_DLG_HEIGHT, N_("All charsets"), &edit->all_codepages), + /* 2 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH, 11, REPLACE_DLG_HEIGHT, N_("All charsets"), + &edit_search_options.all_codepages), #endif - /* 3 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH, 10, REPLACE_DLG_HEIGHT, N_("&Whole words"), &edit->whole_words), - /* 4 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH, 9, REPLACE_DLG_HEIGHT, N_("In se&lection"), &edit->only_in_selection), - /* 5 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH, 8, REPLACE_DLG_HEIGHT, N_("&Backwards"), &edit->replace_backwards), - /* 6 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH, 7, REPLACE_DLG_HEIGHT, N_("case &Sensitive"), &edit->replace_case), + /* 3 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH, 10, REPLACE_DLG_HEIGHT, N_("&Whole words"), + &edit_search_options.whole_words), + /* 4 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH, 9, REPLACE_DLG_HEIGHT, N_("In se&lection"), + &edit_search_options.only_in_selection), + /* 5 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH, 8, REPLACE_DLG_HEIGHT, N_("&Backwards"), + &edit_search_options.backwards), + /* 6 */ QUICK_CHECKBOX (33, REPLACE_DLG_WIDTH, 7, REPLACE_DLG_HEIGHT, N_("case &Sensitive"), + &edit_search_options.case_sens), /* 7 */ QUICK_RADIO (3, REPLACE_DLG_WIDTH, 7, REPLACE_DLG_HEIGHT, - num_of_types, (const char **) list_of_types, (int *) &edit->search_type), + num_of_types, (const char **) list_of_types, + (int *) &edit_search_options.type), /* 8 */ QUICK_LABEL (2, REPLACE_DLG_WIDTH, 4, REPLACE_DLG_HEIGHT, N_(" Enter replacement string:")), /* 9 */ QUICK_INPUT (3, REPLACE_DLG_WIDTH, 5, REPLACE_DLG_HEIGHT, replace_default, REPLACE_DLG_WIDTH - 6, 0, "replace", replace_text), @@ -157,22 +172,29 @@ editcmd_dialog_search_show (WEdit * edit, char **search_text) QUICK_BUTTON (2, 10, 11, SEARCH_DLG_HEIGHT, N_("&OK"), B_ENTER, NULL), #ifdef HAVE_CHARSET /* 3 */ - QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH, 9, SEARCH_DLG_HEIGHT, N_("All charsets"), &edit->all_codepages), + QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH, 9, SEARCH_DLG_HEIGHT, N_("All charsets"), + &edit_search_options.all_codepages), #endif /* 4 */ - QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH, 8, SEARCH_DLG_HEIGHT, N_("&Whole words"), &edit->whole_words), + QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH, 8, SEARCH_DLG_HEIGHT, N_("&Whole words"), + &edit_search_options.whole_words), /* 5 */ - QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH, 7, SEARCH_DLG_HEIGHT, N_("In se&lection"), &edit->only_in_selection), + QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH, 7, SEARCH_DLG_HEIGHT, N_("In se&lection"), + &edit_search_options.only_in_selection), /* 6 */ - QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH, 6, SEARCH_DLG_HEIGHT, N_("&Backwards"), &edit->replace_backwards), + QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH, 6, SEARCH_DLG_HEIGHT, N_("&Backwards"), + &edit_search_options.backwards), /* 7 */ - QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH, 5, SEARCH_DLG_HEIGHT, N_("case &Sensitive"), &edit->replace_case), + QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH, 5, SEARCH_DLG_HEIGHT, N_("case &Sensitive"), + &edit_search_options.case_sens), /* 8 */ QUICK_RADIO ( 3, SEARCH_DLG_WIDTH, 5, SEARCH_DLG_HEIGHT, - num_of_types, (const char **) list_of_types, (int *) &edit->search_type), + num_of_types, (const char **) list_of_types, + (int *) &edit_search_options.type), /* 9 */ QUICK_INPUT (3, SEARCH_DLG_WIDTH, 3, SEARCH_DLG_HEIGHT, - *search_text, SEARCH_DLG_WIDTH - 6, 0, MC_HISTORY_SHARED_SEARCH, search_text), + *search_text, SEARCH_DLG_WIDTH - 6, 0, + MC_HISTORY_SHARED_SEARCH, search_text), /* 10 */ QUICK_LABEL (2, SEARCH_DLG_WIDTH, 2, SEARCH_DLG_HEIGHT, N_(" Enter search string:")), QUICK_END -- 2.11.4.GIT