2 Search functions for diffviewer.
4 Copyright (C) 2010, 2011
5 The Free Software Foundation, Inc.
8 Slava Zanko <slavazanko@gmail.com>, 2010.
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
30 #include "lib/global.h"
31 #include "lib/strutil.h"
32 #include "lib/tty/key.h"
33 #include "lib/widget.h"
34 #include "lib/charsets.h"
36 #include "src/history.h"
40 /*** global variables ****************************************************************************/
42 /*** file scope macro definitions ****************************************************************/
44 #define SEARCH_DLG_WIDTH 58
45 #define SEARCH_DLG_HEIGHT 13
47 /*** file scope type declarations ****************************************************************/
49 typedef struct mcdiffview_search_options_struct
51 mc_search_type_t type
;
55 gboolean all_codepages
;
56 } mcdiffview_search_options_t
;
58 /*** file scope variables ************************************************************************/
60 static mcdiffview_search_options_t mcdiffview_search_options
= {
61 .type
= MC_SEARCH_T_NORMAL
,
65 .all_codepages
= FALSE
,
68 /*** file scope functions ************************************************************************/
69 /* --------------------------------------------------------------------------------------------- */
71 #define DLG_BTN1_text N_("&Cancel")
72 #define DLG_BTN2_text N_("&OK")
75 mcdiffview_dialog_fix_buttons_positions (QuickDialog
* dlg
)
77 size_t str_cancel_len
, str_ok_len
;
78 size_t first_start_position
;
80 str_cancel_len
= str_term_width1 (_(DLG_BTN1_text
)) + 4;
81 str_ok_len
= str_term_width1 (_(DLG_BTN2_text
)) + 6;
83 first_start_position
= (SEARCH_DLG_WIDTH
- str_cancel_len
- str_ok_len
- 1) / 2;
84 dlg
->widgets
[1].relative_x
= first_start_position
;
85 dlg
->widgets
[0].relative_x
= first_start_position
+ str_ok_len
+ 1;
88 /* --------------------------------------------------------------------------------------------- */
91 mcdiffview_dialog_search (WDiff
* dview
)
98 gchar
**list_of_types
= mc_search_get_types_strings_array (&num_of_types
);
100 QuickWidget search_widgets
[] = {
102 QUICK_BUTTON (3, SEARCH_DLG_WIDTH
, SEARCH_DLG_HEIGHT
- 3, SEARCH_DLG_HEIGHT
, DLG_BTN1_text
,
105 QUICK_BUTTON (3, SEARCH_DLG_WIDTH
, SEARCH_DLG_HEIGHT
- 3, SEARCH_DLG_HEIGHT
, DLG_BTN2_text
,
109 QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH
, 8, SEARCH_DLG_HEIGHT
, N_("&All charsets"),
110 &mcdiffview_search_options
.all_codepages
),
113 QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH
, 7, SEARCH_DLG_HEIGHT
, N_("&Whole words"),
114 &mcdiffview_search_options
.whole_words
),
116 QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH
, 6, SEARCH_DLG_HEIGHT
, N_("&Backwards"),
117 &mcdiffview_search_options
.backwards
),
119 QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH
, 5, SEARCH_DLG_HEIGHT
, N_("Cas&e sensitive"),
120 &mcdiffview_search_options
.case_sens
),
122 QUICK_RADIO (3, SEARCH_DLG_WIDTH
, 5, SEARCH_DLG_HEIGHT
,
123 num_of_types
, (const char **) list_of_types
,
124 (int *) &mcdiffview_search_options
.type
),
126 QUICK_INPUT (3, SEARCH_DLG_WIDTH
, 3, SEARCH_DLG_HEIGHT
,
127 INPUT_LAST_TEXT
, SEARCH_DLG_WIDTH
- 6, 0,
128 MC_HISTORY_SHARED_SEARCH
,
131 QUICK_LABEL (3, SEARCH_DLG_WIDTH
, 2, SEARCH_DLG_HEIGHT
, N_("Enter search string:")),
135 QuickDialog search_input
= {
136 SEARCH_DLG_WIDTH
, SEARCH_DLG_HEIGHT
, -1, -1,
137 N_("Search"), "[Input Line Keys]",
138 search_widgets
, NULL
, NULL
, FALSE
141 mcdiffview_dialog_fix_buttons_positions (&search_input
);
143 qd_result
= quick_dialog (&search_input
);
144 g_strfreev (list_of_types
);
147 if ((qd_result
== B_CANCEL
) || (exp
== NULL
) || (exp
[0] == '\0'))
155 GString
*tmp
= str_convert_to_input (exp
);
160 exp
= g_string_free (tmp
, FALSE
);
165 g_free (dview
->search
.last_string
);
166 dview
->search
.last_string
= exp
;
171 /* --------------------------------------------------------------------------------------------- */
174 mcdiffview_do_search_backward (WDiff
* dview
)
179 if (dview
->search
.last_accessed_num_line
< 0)
181 dview
->search
.last_accessed_num_line
= -1;
185 if ((size_t) dview
->search
.last_accessed_num_line
>= dview
->a
[dview
->ord
]->len
)
186 dview
->search
.last_accessed_num_line
= (ssize_t
) dview
->a
[dview
->ord
]->len
;
188 for (ind
= --dview
->search
.last_accessed_num_line
; ind
>= 0; ind
--)
190 p
= (DIFFLN
*) & g_array_index (dview
->a
[dview
->ord
], DIFFLN
, (size_t) ind
);
194 if (mc_search_run (dview
->search
.handle
, p
->p
, 0, p
->u
.len
, NULL
))
196 dview
->skip_rows
= dview
->search
.last_found_line
=
197 dview
->search
.last_accessed_num_line
= ind
;
204 /* --------------------------------------------------------------------------------------------- */
208 mcdiffview_do_search_forward (WDiff
* dview
)
213 if (dview
->search
.last_accessed_num_line
< 0)
214 dview
->search
.last_accessed_num_line
= -1;
215 else if ((size_t) dview
->search
.last_accessed_num_line
>= dview
->a
[dview
->ord
]->len
)
217 dview
->search
.last_accessed_num_line
= (ssize_t
) dview
->a
[dview
->ord
]->len
;
221 for (ind
= (size_t)++ dview
->search
.last_accessed_num_line
; ind
< dview
->a
[dview
->ord
]->len
;
224 p
= (DIFFLN
*) & g_array_index (dview
->a
[dview
->ord
], DIFFLN
, ind
);
228 if (mc_search_run (dview
->search
.handle
, p
->p
, 0, p
->u
.len
, NULL
))
230 dview
->skip_rows
= dview
->search
.last_found_line
=
231 dview
->search
.last_accessed_num_line
= (ssize_t
) ind
;
238 /* --------------------------------------------------------------------------------------------- */
241 mcdiffview_do_search (WDiff
* dview
)
243 gboolean present_result
= FALSE
;
245 tty_enable_interrupt_key ();
247 if (mcdiffview_search_options
.backwards
)
249 present_result
= mcdiffview_do_search_backward (dview
);
253 present_result
= mcdiffview_do_search_forward (dview
);
256 tty_disable_interrupt_key ();
260 dview
->search
.last_found_line
= -1;
261 error_dialog (_("Search"), _("Search string not found"));
265 /* --------------------------------------------------------------------------------------------- */
266 /*** public functions ****************************************************************************/
267 /* --------------------------------------------------------------------------------------------- */
270 dview_search_cmd (WDiff
* dview
)
272 if (dview
->dsrc
!= DATA_SRC_MEM
)
274 error_dialog (_("Search"), _("Search is disabled"));
278 if (!mcdiffview_dialog_search (dview
))
281 mc_search_free (dview
->search
.handle
);
282 dview
->search
.handle
= mc_search_new (dview
->search
.last_string
, -1);
284 if (dview
->search
.handle
== NULL
)
287 dview
->search
.handle
->search_type
= mcdiffview_search_options
.type
;
288 dview
->search
.handle
->is_all_charsets
= mcdiffview_search_options
.all_codepages
;
289 dview
->search
.handle
->is_case_sensitive
= mcdiffview_search_options
.case_sens
;
290 dview
->search
.handle
->whole_words
= mcdiffview_search_options
.whole_words
;
292 mcdiffview_do_search (dview
);
295 /* --------------------------------------------------------------------------------------------- */
298 dview_continue_search_cmd (WDiff
* dview
)
300 if (dview
->dsrc
!= DATA_SRC_MEM
)
301 error_dialog (_("Search"), _("Search is disabled"));
302 else if (dview
->search
.handle
== NULL
)
303 dview_search_cmd (dview
);
305 mcdiffview_do_search (dview
);
308 /* --------------------------------------------------------------------------------------------- */