Add edit_add_window() function.
[midnight-commander.git] / src / diffviewer / search.c
blob00573a87fbbf111cb68ac1fa42692d63b1cfa0c1
1 /*
2 Search functions for diffviewer.
4 Copyright (C) 2010, 2011
5 The Free Software Foundation, Inc.
7 Written by:
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/>.
26 #include <config.h>
28 #include <stdio.h>
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"
38 #include "internal.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;
52 gboolean case_sens;
53 gboolean backwards;
54 gboolean whole_words;
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,
62 .case_sens = FALSE,
63 .backwards = FALSE,
64 .whole_words = FALSE,
65 .all_codepages = FALSE,
68 /*** file scope functions ************************************************************************/
69 /* --------------------------------------------------------------------------------------------- */
71 #define DLG_BTN1_text N_("&Cancel")
72 #define DLG_BTN2_text N_("&OK")
74 static void
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 /* --------------------------------------------------------------------------------------------- */
90 static gboolean
91 mcdiffview_dialog_search (WDiff * dview)
93 char *exp = NULL;
95 int qd_result;
97 size_t num_of_types;
98 gchar **list_of_types = mc_search_get_types_strings_array (&num_of_types);
100 QuickWidget search_widgets[] = {
101 /* 0 */
102 QUICK_BUTTON (3, SEARCH_DLG_WIDTH, SEARCH_DLG_HEIGHT - 3, SEARCH_DLG_HEIGHT, DLG_BTN1_text,
103 B_CANCEL, NULL),
104 /* 1 */
105 QUICK_BUTTON (3, SEARCH_DLG_WIDTH, SEARCH_DLG_HEIGHT - 3, SEARCH_DLG_HEIGHT, DLG_BTN2_text,
106 B_ENTER, NULL),
107 /* 2 */
108 #ifdef HAVE_CHARSET
109 QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH, 8, SEARCH_DLG_HEIGHT, N_("&All charsets"),
110 &mcdiffview_search_options.all_codepages),
111 #endif
113 QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH, 7, SEARCH_DLG_HEIGHT, N_("&Whole words"),
114 &mcdiffview_search_options.whole_words),
115 /* 3 */
116 QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH, 6, SEARCH_DLG_HEIGHT, N_("&Backwards"),
117 &mcdiffview_search_options.backwards),
118 /* 4 */
119 QUICK_CHECKBOX (33, SEARCH_DLG_WIDTH, 5, SEARCH_DLG_HEIGHT, N_("Cas&e sensitive"),
120 &mcdiffview_search_options.case_sens),
121 /* 5 */
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),
125 /* 6 */
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,
129 &exp),
130 /* 7 */
131 QUICK_LABEL (3, SEARCH_DLG_WIDTH, 2, SEARCH_DLG_HEIGHT, N_("Enter search string:")),
132 QUICK_END
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'))
149 g_free (exp);
150 return FALSE;
153 #ifdef HAVE_CHARSET
155 GString *tmp = str_convert_to_input (exp);
157 if (tmp)
159 g_free (exp);
160 exp = g_string_free (tmp, FALSE);
163 #endif
165 g_free (dview->search.last_string);
166 dview->search.last_string = exp;
168 return TRUE;
171 /* --------------------------------------------------------------------------------------------- */
173 static gboolean
174 mcdiffview_do_search_backward (WDiff * dview)
176 ssize_t ind;
177 DIFFLN *p;
179 if (dview->search.last_accessed_num_line < 0)
181 dview->search.last_accessed_num_line = -1;
182 return FALSE;
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);
191 if (p->u.len == 0)
192 continue;
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;
198 return TRUE;
201 return FALSE;
204 /* --------------------------------------------------------------------------------------------- */
207 static gboolean
208 mcdiffview_do_search_forward (WDiff * dview)
210 size_t ind;
211 DIFFLN *p;
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;
218 return FALSE;
221 for (ind = (size_t)++ dview->search.last_accessed_num_line; ind < dview->a[dview->ord]->len;
222 ind++)
224 p = (DIFFLN *) & g_array_index (dview->a[dview->ord], DIFFLN, ind);
225 if (p->u.len == 0)
226 continue;
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;
232 return TRUE;
235 return FALSE;
238 /* --------------------------------------------------------------------------------------------- */
240 static void
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);
251 else
253 present_result = mcdiffview_do_search_forward (dview);
256 tty_disable_interrupt_key ();
258 if (!present_result)
260 dview->search.last_found_line = -1;
261 error_dialog (_("Search"), _("Search string not found"));
265 /* --------------------------------------------------------------------------------------------- */
266 /*** public functions ****************************************************************************/
267 /* --------------------------------------------------------------------------------------------- */
269 void
270 dview_search_cmd (WDiff * dview)
272 if (dview->dsrc != DATA_SRC_MEM)
274 error_dialog (_("Search"), _("Search is disabled"));
275 return;
278 if (!mcdiffview_dialog_search (dview))
279 return;
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)
285 return;
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 /* --------------------------------------------------------------------------------------------- */
297 void
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);
304 else
305 mcdiffview_do_search (dview);
308 /* --------------------------------------------------------------------------------------------- */