2 Search functions for diffviewer.
4 Copyright (C) 2010, 2011, 2012
5 The Free Software Foundation, Inc.
8 Slava Zanko <slavazanko@gmail.com>, 2010.
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/>.
31 #include "lib/global.h"
32 #include "lib/strutil.h"
33 #include "lib/tty/key.h"
34 #include "lib/widget.h"
36 #include "lib/charsets.h"
39 #include "src/history.h"
43 /*** global variables ****************************************************************************/
45 /*** file scope macro definitions ****************************************************************/
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 /* --------------------------------------------------------------------------------------------- */
69 /*** file scope functions ************************************************************************/
70 /* --------------------------------------------------------------------------------------------- */
73 mcdiffview_dialog_search (WDiff
* dview
)
78 gchar
**list_of_types
;
80 list_of_types
= mc_search_get_types_strings_array (&num_of_types
);
83 quick_widget_t quick_widgets
[] = {
85 QUICK2_LABELED_INPUT (N_("Enter search string:"), input_label_above
,
86 INPUT_LAST_TEXT
, 0, MC_HISTORY_SHARED_SEARCH
, &exp
, NULL
),
87 QUICK2_SEPARATOR (TRUE
),
89 QUICK2_RADIO (num_of_types
, (const char **) list_of_types
,
90 (int *) &mcdiffview_search_options
.type
, NULL
),
92 QUICK2_CHECKBOX (N_("Cas&e sensitive"), &mcdiffview_search_options
.case_sens
, NULL
),
93 QUICK2_CHECKBOX (N_("&Backwards"), &mcdiffview_search_options
.backwards
, NULL
),
94 QUICK2_CHECKBOX (N_("&Whole words"), &mcdiffview_search_options
.whole_words
, NULL
),
96 QUICK2_CHECKBOX (N_("&All charsets"), &mcdiffview_search_options
.all_codepages
,
100 QUICK2_START_BUTTONS (TRUE
, TRUE
),
101 QUICK2_BUTTON (N_("&OK"), B_ENTER
, NULL
, NULL
),
102 QUICK2_BUTTON (N_("&Cancel"), B_CANCEL
, NULL
, NULL
),
107 quick_dialog_t qdlg
= {
109 N_("Search"), "[Input Line Keys]",
110 quick_widgets
, NULL
, NULL
113 qd_result
= quick2_dialog (&qdlg
);
116 g_strfreev (list_of_types
);
118 if ((qd_result
== B_CANCEL
) || (exp
== NULL
) || (exp
[0] == '\0'))
128 tmp
= str_convert_to_input (exp
);
132 exp
= g_string_free (tmp
, FALSE
);
137 g_free (dview
->search
.last_string
);
138 dview
->search
.last_string
= exp
;
143 /* --------------------------------------------------------------------------------------------- */
146 mcdiffview_do_search_backward (WDiff
* dview
)
151 if (dview
->search
.last_accessed_num_line
< 0)
153 dview
->search
.last_accessed_num_line
= -1;
157 if ((size_t) dview
->search
.last_accessed_num_line
>= dview
->a
[dview
->ord
]->len
)
158 dview
->search
.last_accessed_num_line
= (ssize_t
) dview
->a
[dview
->ord
]->len
;
160 for (ind
= --dview
->search
.last_accessed_num_line
; ind
>= 0; ind
--)
162 p
= (DIFFLN
*) & g_array_index (dview
->a
[dview
->ord
], DIFFLN
, (size_t) ind
);
166 if (mc_search_run (dview
->search
.handle
, p
->p
, 0, p
->u
.len
, NULL
))
168 dview
->skip_rows
= dview
->search
.last_found_line
=
169 dview
->search
.last_accessed_num_line
= ind
;
176 /* --------------------------------------------------------------------------------------------- */
180 mcdiffview_do_search_forward (WDiff
* dview
)
185 if (dview
->search
.last_accessed_num_line
< 0)
186 dview
->search
.last_accessed_num_line
= -1;
187 else if ((size_t) dview
->search
.last_accessed_num_line
>= dview
->a
[dview
->ord
]->len
)
189 dview
->search
.last_accessed_num_line
= (ssize_t
) dview
->a
[dview
->ord
]->len
;
193 for (ind
= (size_t)++ dview
->search
.last_accessed_num_line
; ind
< dview
->a
[dview
->ord
]->len
;
196 p
= (DIFFLN
*) & g_array_index (dview
->a
[dview
->ord
], DIFFLN
, ind
);
200 if (mc_search_run (dview
->search
.handle
, p
->p
, 0, p
->u
.len
, NULL
))
202 dview
->skip_rows
= dview
->search
.last_found_line
=
203 dview
->search
.last_accessed_num_line
= (ssize_t
) ind
;
210 /* --------------------------------------------------------------------------------------------- */
213 mcdiffview_do_search (WDiff
* dview
)
215 gboolean present_result
= FALSE
;
217 tty_enable_interrupt_key ();
219 if (mcdiffview_search_options
.backwards
)
221 present_result
= mcdiffview_do_search_backward (dview
);
225 present_result
= mcdiffview_do_search_forward (dview
);
228 tty_disable_interrupt_key ();
232 dview
->search
.last_found_line
= -1;
233 error_dialog (_("Search"), _("Search string not found"));
237 /* --------------------------------------------------------------------------------------------- */
238 /*** public functions ****************************************************************************/
239 /* --------------------------------------------------------------------------------------------- */
242 dview_search_cmd (WDiff
* dview
)
244 if (dview
->dsrc
!= DATA_SRC_MEM
)
246 error_dialog (_("Search"), _("Search is disabled"));
250 if (!mcdiffview_dialog_search (dview
))
253 mc_search_free (dview
->search
.handle
);
254 dview
->search
.handle
= mc_search_new (dview
->search
.last_string
, -1);
256 if (dview
->search
.handle
== NULL
)
259 dview
->search
.handle
->search_type
= mcdiffview_search_options
.type
;
260 dview
->search
.handle
->is_all_charsets
= mcdiffview_search_options
.all_codepages
;
261 dview
->search
.handle
->is_case_sensitive
= mcdiffview_search_options
.case_sens
;
262 dview
->search
.handle
->whole_words
= mcdiffview_search_options
.whole_words
;
264 mcdiffview_do_search (dview
);
267 /* --------------------------------------------------------------------------------------------- */
270 dview_continue_search_cmd (WDiff
* dview
)
272 if (dview
->dsrc
!= DATA_SRC_MEM
)
273 error_dialog (_("Search"), _("Search is disabled"));
274 else if (dview
->search
.handle
== NULL
)
275 dview_search_cmd (dview
);
277 mcdiffview_do_search (dview
);
280 /* --------------------------------------------------------------------------------------------- */