2 * search.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2006-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * Find, Replace, Find in Files dialog related functions.
24 * Note that the basic text find functions are in document.c.
35 #include "encodings.h"
37 #include "msgwindow.h"
39 #include "sciwrappers.h"
47 #include "gtkcompat.h"
53 #include <gdk/gdkkeysyms.h>
57 GEANY_RESPONSE_FIND
= 1,
58 GEANY_RESPONSE_FIND_PREVIOUS
,
59 GEANY_RESPONSE_FIND_IN_FILE
,
60 GEANY_RESPONSE_FIND_IN_SESSION
,
62 GEANY_RESPONSE_REPLACE
,
63 GEANY_RESPONSE_REPLACE_AND_FIND
,
64 GEANY_RESPONSE_REPLACE_IN_SESSION
,
65 GEANY_RESPONSE_REPLACE_IN_FILE
,
66 GEANY_RESPONSE_REPLACE_IN_SEL
78 GeanySearchData search_data
;
79 GeanySearchPrefs search_prefs
;
85 gboolean fif_case_sensitive
;
86 gboolean fif_match_whole_word
;
87 gboolean fif_invert_results
;
88 gboolean fif_recursive
;
89 gboolean fif_use_extra_options
;
90 gchar
*fif_extra_options
;
94 gboolean find_regexp_multiline
;
95 gboolean find_escape_sequences
;
96 gboolean find_case_sensitive
;
97 gboolean find_match_whole_word
;
98 gboolean find_match_word_start
;
99 gboolean find_close_dialog
;
100 gboolean replace_regexp
;
101 gboolean replace_regexp_multiline
;
102 gboolean replace_escape_sequences
;
103 gboolean replace_case_sensitive
;
104 gboolean replace_match_whole_word
;
105 gboolean replace_match_word_start
;
106 gboolean replace_search_backwards
;
107 gboolean replace_close_dialog
;
111 static StashGroup
*fif_prefs
= NULL
;
112 static StashGroup
*find_prefs
= NULL
;
113 static StashGroup
*replace_prefs
= NULL
;
120 gboolean all_expanded
;
121 gint position
[2]; /* x, y */
123 find_dlg
= {NULL
, NULL
, FALSE
, {0, 0}};
128 GtkWidget
*find_entry
;
129 GtkWidget
*replace_entry
;
130 gboolean all_expanded
;
131 gint position
[2]; /* x, y */
133 replace_dlg
= {NULL
, NULL
, NULL
, FALSE
, {0, 0}};
138 GtkWidget
*dir_combo
;
139 GtkWidget
*files_combo
;
140 GtkWidget
*search_combo
;
141 GtkWidget
*encoding_combo
;
142 GtkWidget
*files_mode_combo
;
143 gint position
[2]; /* x, y */
145 fif_dlg
= {NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, {0, 0}};
148 static void search_read_io(GString
*string
, GIOCondition condition
, gpointer data
);
149 static void search_read_io_stderr(GString
*string
, GIOCondition condition
, gpointer data
);
151 static void search_finished(GPid child_pid
, gint status
, gpointer user_data
);
153 static gchar
**search_get_argv(const gchar
**argv_prefix
, const gchar
*dir
);
155 static GRegex
*compile_regex(const gchar
*str
, GeanyFindFlags sflags
);
159 on_find_replace_checkbutton_toggled(GtkToggleButton
*togglebutton
, gpointer user_data
);
162 on_find_dialog_response(GtkDialog
*dialog
, gint response
, gpointer user_data
);
165 on_find_entry_activate(GtkEntry
*entry
, gpointer user_data
);
168 on_find_entry_activate_backward(GtkEntry
*entry
, gpointer user_data
);
171 on_replace_dialog_response(GtkDialog
*dialog
, gint response
, gpointer user_data
);
174 on_replace_find_entry_activate(GtkEntry
*entry
, gpointer user_data
);
177 on_replace_entry_activate(GtkEntry
*entry
, gpointer user_data
);
180 on_find_in_files_dialog_response(GtkDialog
*dialog
, gint response
, gpointer user_data
);
183 search_find_in_files(const gchar
*utf8_search_text
, const gchar
*dir
, const gchar
*opts
,
187 static void init_prefs(void)
191 group
= stash_group_new("search");
192 configuration_add_pref_group(group
, TRUE
);
193 stash_group_add_toggle_button(group
, &search_prefs
.always_wrap
,
194 "pref_search_hide_find_dialog", FALSE
, "check_always_wrap_search");
195 stash_group_add_toggle_button(group
, &search_prefs
.hide_find_dialog
,
196 "pref_search_always_wrap", FALSE
, "check_hide_find_dialog");
197 stash_group_add_toggle_button(group
, &search_prefs
.use_current_file_dir
,
198 "pref_search_current_file_dir", TRUE
, "check_fif_current_dir");
199 stash_group_add_boolean(group
, &find_dlg
.all_expanded
, "find_all_expanded", FALSE
);
200 stash_group_add_boolean(group
, &replace_dlg
.all_expanded
, "replace_all_expanded", FALSE
);
201 /* dialog positions */
202 stash_group_add_integer(group
, &find_dlg
.position
[0], "position_find_x", -1);
203 stash_group_add_integer(group
, &find_dlg
.position
[1], "position_find_y", -1);
204 stash_group_add_integer(group
, &replace_dlg
.position
[0], "position_replace_x", -1);
205 stash_group_add_integer(group
, &replace_dlg
.position
[1], "position_replace_y", -1);
206 stash_group_add_integer(group
, &fif_dlg
.position
[0], "position_fif_x", -1);
207 stash_group_add_integer(group
, &fif_dlg
.position
[1], "position_fif_y", -1);
209 memset(&settings
, '\0', sizeof(settings
));
211 group
= stash_group_new("search");
213 configuration_add_pref_group(group
, FALSE
);
214 stash_group_add_toggle_button(group
, &settings
.fif_regexp
,
215 "fif_regexp", FALSE
, "check_regexp");
216 stash_group_add_toggle_button(group
, &settings
.fif_case_sensitive
,
217 "fif_case_sensitive", TRUE
, "check_case");
218 stash_group_add_toggle_button(group
, &settings
.fif_match_whole_word
,
219 "fif_match_whole_word", FALSE
, "check_wholeword");
220 stash_group_add_toggle_button(group
, &settings
.fif_invert_results
,
221 "fif_invert_results", FALSE
, "check_invert");
222 stash_group_add_toggle_button(group
, &settings
.fif_recursive
,
223 "fif_recursive", FALSE
, "check_recursive");
224 stash_group_add_entry(group
, &settings
.fif_extra_options
,
225 "fif_extra_options", "", "entry_extra");
226 stash_group_add_toggle_button(group
, &settings
.fif_use_extra_options
,
227 "fif_use_extra_options", FALSE
, "check_extra");
228 stash_group_add_entry(group
, &settings
.fif_files
,
229 "fif_files", "", "entry_files");
230 stash_group_add_combo_box(group
, &settings
.fif_files_mode
,
231 "fif_files_mode", FILES_MODE_ALL
, "combo_files_mode");
233 group
= stash_group_new("search");
235 configuration_add_pref_group(group
, FALSE
);
236 stash_group_add_toggle_button(group
, &settings
.find_regexp
,
237 "find_regexp", FALSE
, "check_regexp");
238 stash_group_add_toggle_button(group
, &settings
.find_regexp_multiline
,
239 "find_regexp_multiline", FALSE
, "check_multiline");
240 stash_group_add_toggle_button(group
, &settings
.find_case_sensitive
,
241 "find_case_sensitive", FALSE
, "check_case");
242 stash_group_add_toggle_button(group
, &settings
.find_escape_sequences
,
243 "find_escape_sequences", FALSE
, "check_escape");
244 stash_group_add_toggle_button(group
, &settings
.find_match_whole_word
,
245 "find_match_whole_word", FALSE
, "check_word");
246 stash_group_add_toggle_button(group
, &settings
.find_match_word_start
,
247 "find_match_word_start", FALSE
, "check_wordstart");
248 stash_group_add_toggle_button(group
, &settings
.find_close_dialog
,
249 "find_close_dialog", TRUE
, "check_close");
251 group
= stash_group_new("search");
252 replace_prefs
= group
;
253 configuration_add_pref_group(group
, FALSE
);
254 stash_group_add_toggle_button(group
, &settings
.replace_regexp
,
255 "replace_regexp", FALSE
, "check_regexp");
256 stash_group_add_toggle_button(group
, &settings
.replace_regexp_multiline
,
257 "replace_regexp_multiline", FALSE
, "check_multiline");
258 stash_group_add_toggle_button(group
, &settings
.replace_case_sensitive
,
259 "replace_case_sensitive", FALSE
, "check_case");
260 stash_group_add_toggle_button(group
, &settings
.replace_escape_sequences
,
261 "replace_escape_sequences", FALSE
, "check_escape");
262 stash_group_add_toggle_button(group
, &settings
.replace_match_whole_word
,
263 "replace_match_whole_word", FALSE
, "check_word");
264 stash_group_add_toggle_button(group
, &settings
.replace_match_word_start
,
265 "replace_match_word_start", FALSE
, "check_wordstart");
266 stash_group_add_toggle_button(group
, &settings
.replace_search_backwards
,
267 "replace_search_backwards", FALSE
, "check_back");
268 stash_group_add_toggle_button(group
, &settings
.replace_close_dialog
,
269 "replace_close_dialog", TRUE
, "check_close");
273 void search_init(void)
275 search_data
.text
= NULL
;
276 search_data
.original_text
= NULL
;
281 #define FREE_WIDGET(wid) \
282 if (wid && GTK_IS_WIDGET(wid)) gtk_widget_destroy(wid);
284 void search_finalize(void)
286 FREE_WIDGET(find_dlg
.dialog
);
287 FREE_WIDGET(replace_dlg
.dialog
);
288 FREE_WIDGET(fif_dlg
.dialog
);
289 g_free(search_data
.text
);
290 g_free(search_data
.original_text
);
294 static void on_widget_toggled_set_insensitive(
295 GtkToggleButton
*togglebutton
, gpointer user_data
)
297 gtk_widget_set_sensitive(GTK_WIDGET(user_data
),
298 !gtk_toggle_button_get_active(togglebutton
));
302 static GtkWidget
*add_find_checkboxes(GtkDialog
*dialog
)
304 GtkWidget
*checkbox1
, *checkbox2
, *check_regexp
, *check_back
, *checkbox5
,
305 *checkbox7
, *check_multiline
, *hbox
, *fbox
, *mbox
;
307 check_regexp
= gtk_check_button_new_with_mnemonic(_("_Use regular expressions"));
308 ui_hookup_widget(dialog
, check_regexp
, "check_regexp");
309 gtk_button_set_focus_on_click(GTK_BUTTON(check_regexp
), FALSE
);
310 gtk_widget_set_tooltip_text(check_regexp
, _("Use POSIX-like regular expressions. "
311 "For detailed information about using regular expressions, please read the documentation."));
312 g_signal_connect(check_regexp
, "toggled",
313 G_CALLBACK(on_find_replace_checkbutton_toggled
), dialog
);
315 checkbox7
= gtk_check_button_new_with_mnemonic(_("Use _escape sequences"));
316 ui_hookup_widget(dialog
, checkbox7
, "check_escape");
317 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox7
), FALSE
);
318 gtk_widget_set_tooltip_text(checkbox7
,
319 _("Replace \\\\, \\t, \\n, \\r and \\uXXXX (Unicode characters) with the "
320 "corresponding control characters"));
322 check_multiline
= gtk_check_button_new_with_mnemonic(_("Use multi-_line matching"));
323 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_multiline
), FALSE
);
324 gtk_widget_set_sensitive(check_multiline
, FALSE
);
325 ui_hookup_widget(dialog
, check_multiline
, "check_multiline");
326 gtk_button_set_focus_on_click(GTK_BUTTON(check_multiline
), FALSE
);
327 gtk_widget_set_tooltip_text(check_multiline
, _("Perform regular expression "
328 "matching on the whole buffer at once rather than line by line, allowing "
329 "matches to span multiple lines. In this mode, newline characters are part "
330 "of the input and can be captured as normal characters by the pattern."));
332 /* Search features */
333 fbox
= gtk_vbox_new(FALSE
, 0);
334 gtk_box_pack_start(GTK_BOX(fbox
), check_regexp
, FALSE
, FALSE
, 0);
335 gtk_box_pack_start(GTK_BOX(fbox
), check_multiline
, FALSE
, FALSE
, 0);
336 gtk_box_pack_start(GTK_BOX(fbox
), checkbox7
, FALSE
, FALSE
, 0);
338 if (dialog
!= GTK_DIALOG(find_dlg
.dialog
))
340 check_back
= gtk_check_button_new_with_mnemonic(_("Search _backwards"));
341 ui_hookup_widget(dialog
, check_back
, "check_back");
342 gtk_button_set_focus_on_click(GTK_BUTTON(check_back
), FALSE
);
343 gtk_container_add(GTK_CONTAINER(fbox
), check_back
);
346 checkbox1
= gtk_check_button_new_with_mnemonic(_("C_ase sensitive"));
347 ui_hookup_widget(dialog
, checkbox1
, "check_case");
348 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox1
), FALSE
);
350 checkbox2
= gtk_check_button_new_with_mnemonic(_("Match only a _whole word"));
351 ui_hookup_widget(dialog
, checkbox2
, "check_word");
352 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox2
), FALSE
);
354 checkbox5
= gtk_check_button_new_with_mnemonic(_("Match from s_tart of word"));
355 ui_hookup_widget(dialog
, checkbox5
, "check_wordstart");
356 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox5
), FALSE
);
358 /* disable wordstart when wholeword is checked */
359 g_signal_connect(checkbox2
, "toggled",
360 G_CALLBACK(on_widget_toggled_set_insensitive
), checkbox5
);
362 /* Matching options */
363 mbox
= gtk_vbox_new(FALSE
, 0);
364 gtk_box_pack_start(GTK_BOX(mbox
), checkbox1
, FALSE
, FALSE
, 0);
365 gtk_box_pack_start(GTK_BOX(mbox
), checkbox2
, FALSE
, FALSE
, 0);
366 gtk_box_pack_start(GTK_BOX(mbox
), checkbox5
, FALSE
, FALSE
, 0);
368 hbox
= gtk_hbox_new(TRUE
, 6);
369 gtk_container_add(GTK_CONTAINER(hbox
), fbox
);
370 gtk_container_add(GTK_CONTAINER(hbox
), mbox
);
375 static void send_find_dialog_response(GtkButton
*button
, gpointer user_data
)
377 gtk_dialog_response(GTK_DIALOG(find_dlg
.dialog
), GPOINTER_TO_INT(user_data
));
381 /* store text, clear search flags so we can use Search->Find Next/Previous */
382 static void setup_find_next(const gchar
*text
)
384 g_free(search_data
.text
);
385 g_free(search_data
.original_text
);
386 search_data
.text
= g_strdup(text
);
387 search_data
.original_text
= g_strdup(text
);
388 search_data
.flags
= 0;
389 search_data
.backwards
= FALSE
;
390 search_data
.search_bar
= FALSE
;
394 /* Search for next match of the current "selection".
395 * Optionally for X11 based systems, this will try to use the system-wide
397 * If it doesn't find a suitable search string it will try to use
398 * the current word instead, or just repeat the last search.
399 * Search flags are always zero.
401 void search_find_selection(GeanyDocument
*doc
, gboolean search_backwards
)
405 g_return_if_fail(DOC_VALID(doc
));
408 if (search_prefs
.find_selection_type
== GEANY_FIND_SEL_X
)
410 GtkClipboard
*clipboard
= gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
412 s
= gtk_clipboard_wait_for_text(clipboard
);
413 if (s
&& (strchr(s
,'\n') || strchr(s
, '\r')))
421 if (!s
&& sci_has_selection(doc
->editor
->sci
))
422 s
= sci_get_selection_contents(doc
->editor
->sci
);
424 if (!s
&& search_prefs
.find_selection_type
!= GEANY_FIND_SEL_AGAIN
)
426 /* get the current word */
427 s
= editor_get_default_selection(doc
->editor
, TRUE
, NULL
);
432 setup_find_next(s
); /* allow find next/prev */
434 if (document_find_text(doc
, s
, NULL
, 0, search_backwards
, NULL
, FALSE
, NULL
) > -1)
435 editor_display_current_line(doc
->editor
, 0.3F
);
438 else if (search_prefs
.find_selection_type
== GEANY_FIND_SEL_AGAIN
)
440 /* Repeat last search (in case selection was lost) */
441 search_find_again(search_backwards
);
450 static void on_expander_activated(GtkExpander
*exp
, gpointer data
)
452 gboolean
*setting
= data
;
454 *setting
= gtk_expander_get_expanded(exp
);
458 static void create_find_dialog(void)
460 GtkWidget
*label
, *entry
, *sbox
, *vbox
;
461 GtkWidget
*exp
, *bbox
, *button
, *check_close
;
463 find_dlg
.dialog
= gtk_dialog_new_with_buttons(_("Find"),
464 GTK_WINDOW(main_widgets
.window
), GTK_DIALOG_DESTROY_WITH_PARENT
,
465 GTK_STOCK_CLOSE
, GTK_RESPONSE_CANCEL
, NULL
);
466 vbox
= ui_dialog_vbox_new(GTK_DIALOG(find_dlg
.dialog
));
467 gtk_widget_set_name(find_dlg
.dialog
, "GeanyDialogSearch");
468 gtk_box_set_spacing(GTK_BOX(vbox
), 9);
470 button
= ui_button_new_with_image(GTK_STOCK_GO_BACK
, _("_Previous"));
471 gtk_dialog_add_action_widget(GTK_DIALOG(find_dlg
.dialog
), button
,
472 GEANY_RESPONSE_FIND_PREVIOUS
);
473 ui_hookup_widget(find_dlg
.dialog
, button
, "btn_previous");
475 button
= ui_button_new_with_image(GTK_STOCK_GO_FORWARD
, _("_Next"));
476 gtk_dialog_add_action_widget(GTK_DIALOG(find_dlg
.dialog
), button
,
477 GEANY_RESPONSE_FIND
);
479 label
= gtk_label_new_with_mnemonic(_("_Search for:"));
480 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0.5);
482 entry
= gtk_combo_box_text_new_with_entry();
483 ui_entry_add_clear_icon(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(entry
))));
484 gtk_label_set_mnemonic_widget(GTK_LABEL(label
), entry
);
485 gtk_entry_set_width_chars(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(entry
))), 50);
486 find_dlg
.entry
= gtk_bin_get_child(GTK_BIN(entry
));
488 g_signal_connect(gtk_bin_get_child(GTK_BIN(entry
)), "activate",
489 G_CALLBACK(on_find_entry_activate
), entry
);
490 ui_entry_add_activate_backward_signal(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(entry
))));
491 g_signal_connect(gtk_bin_get_child(GTK_BIN(entry
)), "activate-backward",
492 G_CALLBACK(on_find_entry_activate_backward
), entry
);
493 g_signal_connect(find_dlg
.dialog
, "response",
494 G_CALLBACK(on_find_dialog_response
), entry
);
495 g_signal_connect(find_dlg
.dialog
, "delete-event",
496 G_CALLBACK(gtk_widget_hide_on_delete
), NULL
);
498 sbox
= gtk_hbox_new(FALSE
, 6);
499 gtk_box_pack_start(GTK_BOX(sbox
), label
, FALSE
, FALSE
, 0);
500 gtk_box_pack_start(GTK_BOX(sbox
), entry
, TRUE
, TRUE
, 0);
501 gtk_box_pack_start(GTK_BOX(vbox
), sbox
, TRUE
, FALSE
, 0);
503 gtk_container_add(GTK_CONTAINER(vbox
),
504 add_find_checkboxes(GTK_DIALOG(find_dlg
.dialog
)));
506 /* Now add the multiple match options */
507 exp
= gtk_expander_new_with_mnemonic(_("_Find All"));
508 gtk_expander_set_expanded(GTK_EXPANDER(exp
), find_dlg
.all_expanded
);
509 g_signal_connect_after(exp
, "activate",
510 G_CALLBACK(on_expander_activated
), &find_dlg
.all_expanded
);
512 bbox
= gtk_hbutton_box_new();
514 button
= gtk_button_new_with_mnemonic(_("_Mark"));
515 gtk_widget_set_tooltip_text(button
,
516 _("Mark all matches in the current document"));
517 gtk_container_add(GTK_CONTAINER(bbox
), button
);
518 g_signal_connect(button
, "clicked", G_CALLBACK(send_find_dialog_response
),
519 GINT_TO_POINTER(GEANY_RESPONSE_MARK
));
521 button
= gtk_button_new_with_mnemonic(_("In Sessi_on"));
522 gtk_container_add(GTK_CONTAINER(bbox
), button
);
523 g_signal_connect(button
, "clicked", G_CALLBACK(send_find_dialog_response
),
524 GINT_TO_POINTER(GEANY_RESPONSE_FIND_IN_SESSION
));
526 button
= gtk_button_new_with_mnemonic(_("_In Document"));
527 gtk_container_add(GTK_CONTAINER(bbox
), button
);
528 g_signal_connect(button
, "clicked", G_CALLBACK(send_find_dialog_response
),
529 GINT_TO_POINTER(GEANY_RESPONSE_FIND_IN_FILE
));
531 /* close window checkbox */
532 check_close
= gtk_check_button_new_with_mnemonic(_("Close _dialog"));
533 ui_hookup_widget(find_dlg
.dialog
, check_close
, "check_close");
534 gtk_button_set_focus_on_click(GTK_BUTTON(check_close
), FALSE
);
535 gtk_widget_set_tooltip_text(check_close
,
536 _("Disable this option to keep the dialog open"));
537 gtk_container_add(GTK_CONTAINER(bbox
), check_close
);
538 gtk_button_box_set_child_secondary(GTK_BUTTON_BOX(bbox
), check_close
, TRUE
);
540 ui_hbutton_box_copy_layout(
541 GTK_BUTTON_BOX(gtk_dialog_get_action_area(GTK_DIALOG(find_dlg
.dialog
))),
542 GTK_BUTTON_BOX(bbox
));
543 gtk_container_add(GTK_CONTAINER(exp
), bbox
);
544 gtk_container_add(GTK_CONTAINER(vbox
), exp
);
548 static void set_dialog_position(GtkWidget
*dialog
, gint
*position
)
550 if (position
[0] >= 0)
551 gtk_window_move(GTK_WINDOW(dialog
), position
[0], position
[1]);
555 void search_show_find_dialog(void)
557 GeanyDocument
*doc
= document_get_current();
560 g_return_if_fail(doc
!= NULL
);
562 sel
= editor_get_default_selection(doc
->editor
, search_prefs
.use_current_word
, NULL
);
564 if (find_dlg
.dialog
== NULL
)
566 create_find_dialog();
567 stash_group_display(find_prefs
, find_dlg
.dialog
);
569 gtk_entry_set_text(GTK_ENTRY(find_dlg
.entry
), sel
);
571 set_dialog_position(find_dlg
.dialog
, find_dlg
.position
);
572 gtk_widget_show_all(find_dlg
.dialog
);
576 /* only set selection if the dialog is not already visible */
577 if (! gtk_widget_get_visible(find_dlg
.dialog
) && sel
)
578 gtk_entry_set_text(GTK_ENTRY(find_dlg
.entry
), sel
);
579 gtk_widget_grab_focus(find_dlg
.entry
);
580 set_dialog_position(find_dlg
.dialog
, find_dlg
.position
);
581 gtk_widget_show(find_dlg
.dialog
);
582 if (sel
!= NULL
) /* when we have a selection, reset the entry widget's background colour */
583 ui_set_search_entry_background(find_dlg
.entry
, TRUE
);
584 /* bring the dialog back in the foreground in case it is already open but the focus is away */
585 gtk_window_present(GTK_WINDOW(find_dlg
.dialog
));
592 static void send_replace_dialog_response(GtkButton
*button
, gpointer user_data
)
594 gtk_dialog_response(GTK_DIALOG(replace_dlg
.dialog
), GPOINTER_TO_INT(user_data
));
599 on_widget_key_pressed_set_focus(GtkWidget
*widget
, GdkEventKey
*event
, gpointer user_data
)
601 if (event
->keyval
== GDK_Tab
)
603 gtk_widget_grab_focus(GTK_WIDGET(user_data
));
610 static void create_replace_dialog(void)
612 GtkWidget
*label_find
, *label_replace
, *entry_find
, *entry_replace
,
613 *check_close
, *button
, *rbox
, *fbox
, *vbox
, *exp
, *bbox
;
614 GtkSizeGroup
*label_size
;
616 replace_dlg
.dialog
= gtk_dialog_new_with_buttons(_("Replace"),
617 GTK_WINDOW(main_widgets
.window
), GTK_DIALOG_DESTROY_WITH_PARENT
,
618 GTK_STOCK_CLOSE
, GTK_RESPONSE_CANCEL
, NULL
);
619 vbox
= ui_dialog_vbox_new(GTK_DIALOG(replace_dlg
.dialog
));
620 gtk_box_set_spacing(GTK_BOX(vbox
), 9);
621 gtk_widget_set_name(replace_dlg
.dialog
, "GeanyDialogSearch");
623 button
= gtk_button_new_from_stock(GTK_STOCK_FIND
);
624 gtk_dialog_add_action_widget(GTK_DIALOG(replace_dlg
.dialog
), button
,
625 GEANY_RESPONSE_FIND
);
626 button
= gtk_button_new_with_mnemonic(_("_Replace"));
627 gtk_button_set_image(GTK_BUTTON(button
),
628 gtk_image_new_from_stock(GTK_STOCK_FIND_AND_REPLACE
, GTK_ICON_SIZE_BUTTON
));
629 gtk_dialog_add_action_widget(GTK_DIALOG(replace_dlg
.dialog
), button
,
630 GEANY_RESPONSE_REPLACE
);
631 button
= gtk_button_new_with_mnemonic(_("Replace & Fi_nd"));
632 gtk_button_set_image(GTK_BUTTON(button
),
633 gtk_image_new_from_stock(GTK_STOCK_FIND_AND_REPLACE
, GTK_ICON_SIZE_BUTTON
));
634 gtk_dialog_add_action_widget(GTK_DIALOG(replace_dlg
.dialog
), button
,
635 GEANY_RESPONSE_REPLACE_AND_FIND
);
637 label_find
= gtk_label_new_with_mnemonic(_("_Search for:"));
638 gtk_misc_set_alignment(GTK_MISC(label_find
), 0, 0.5);
640 label_replace
= gtk_label_new_with_mnemonic(_("Replace wit_h:"));
641 gtk_misc_set_alignment(GTK_MISC(label_replace
), 0, 0.5);
643 entry_find
= gtk_combo_box_text_new_with_entry();
644 ui_entry_add_clear_icon(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(entry_find
))));
645 gtk_label_set_mnemonic_widget(GTK_LABEL(label_find
), entry_find
);
646 gtk_entry_set_width_chars(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(entry_find
))), 50);
647 ui_hookup_widget(replace_dlg
.dialog
, entry_find
, "entry_find");
648 replace_dlg
.find_entry
= gtk_bin_get_child(GTK_BIN(entry_find
));
650 entry_replace
= gtk_combo_box_text_new_with_entry();
651 ui_entry_add_clear_icon(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(entry_replace
))));
652 gtk_label_set_mnemonic_widget(GTK_LABEL(label_replace
), entry_replace
);
653 gtk_entry_set_width_chars(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(entry_replace
))), 50);
654 ui_hookup_widget(replace_dlg
.dialog
, entry_replace
, "entry_replace");
655 replace_dlg
.replace_entry
= gtk_bin_get_child(GTK_BIN(entry_replace
));
657 /* tab from find to the replace entry */
658 g_signal_connect(gtk_bin_get_child(GTK_BIN(entry_find
)),
659 "key-press-event", G_CALLBACK(on_widget_key_pressed_set_focus
),
660 gtk_bin_get_child(GTK_BIN(entry_replace
)));
661 g_signal_connect(gtk_bin_get_child(GTK_BIN(entry_find
)), "activate",
662 G_CALLBACK(on_replace_find_entry_activate
), NULL
);
663 g_signal_connect(gtk_bin_get_child(GTK_BIN(entry_replace
)), "activate",
664 G_CALLBACK(on_replace_entry_activate
), NULL
);
665 g_signal_connect(replace_dlg
.dialog
, "response",
666 G_CALLBACK(on_replace_dialog_response
), NULL
);
667 g_signal_connect(replace_dlg
.dialog
, "delete-event",
668 G_CALLBACK(gtk_widget_hide_on_delete
), NULL
);
670 fbox
= gtk_hbox_new(FALSE
, 6);
671 gtk_box_pack_start(GTK_BOX(fbox
), label_find
, FALSE
, FALSE
, 0);
672 gtk_box_pack_start(GTK_BOX(fbox
), entry_find
, TRUE
, TRUE
, 0);
674 rbox
= gtk_hbox_new(FALSE
, 6);
675 gtk_box_pack_start(GTK_BOX(rbox
), label_replace
, FALSE
, FALSE
, 0);
676 gtk_box_pack_start(GTK_BOX(rbox
), entry_replace
, TRUE
, TRUE
, 0);
678 label_size
= gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL
);
679 gtk_size_group_add_widget(label_size
, label_find
);
680 gtk_size_group_add_widget(label_size
, label_replace
);
681 g_object_unref(G_OBJECT(label_size
)); /* auto destroy the size group */
683 gtk_box_pack_start(GTK_BOX(vbox
), fbox
, TRUE
, FALSE
, 0);
684 gtk_box_pack_start(GTK_BOX(vbox
), rbox
, TRUE
, FALSE
, 0);
685 gtk_container_add(GTK_CONTAINER(vbox
),
686 add_find_checkboxes(GTK_DIALOG(replace_dlg
.dialog
)));
688 /* Now add the multiple replace options */
689 exp
= gtk_expander_new_with_mnemonic(_("Re_place All"));
690 gtk_expander_set_expanded(GTK_EXPANDER(exp
), replace_dlg
.all_expanded
);
691 g_signal_connect_after(exp
, "activate",
692 G_CALLBACK(on_expander_activated
), &replace_dlg
.all_expanded
);
694 bbox
= gtk_hbutton_box_new();
696 button
= gtk_button_new_with_mnemonic(_("In Sessi_on"));
697 gtk_container_add(GTK_CONTAINER(bbox
), button
);
698 g_signal_connect(button
, "clicked", G_CALLBACK(send_replace_dialog_response
),
699 GINT_TO_POINTER(GEANY_RESPONSE_REPLACE_IN_SESSION
));
701 button
= gtk_button_new_with_mnemonic(_("_In Document"));
702 gtk_container_add(GTK_CONTAINER(bbox
), button
);
703 g_signal_connect(button
, "clicked", G_CALLBACK(send_replace_dialog_response
),
704 GINT_TO_POINTER(GEANY_RESPONSE_REPLACE_IN_FILE
));
706 button
= gtk_button_new_with_mnemonic(_("In Se_lection"));
707 gtk_widget_set_tooltip_text(button
,
708 _("Replace all matches found in the currently selected text"));
709 gtk_container_add(GTK_CONTAINER(bbox
), button
);
710 g_signal_connect(button
, "clicked", G_CALLBACK(send_replace_dialog_response
),
711 GINT_TO_POINTER(GEANY_RESPONSE_REPLACE_IN_SEL
));
713 /* close window checkbox */
714 check_close
= gtk_check_button_new_with_mnemonic(_("Close _dialog"));
715 ui_hookup_widget(replace_dlg
.dialog
, check_close
, "check_close");
716 gtk_button_set_focus_on_click(GTK_BUTTON(check_close
), FALSE
);
717 gtk_widget_set_tooltip_text(check_close
,
718 _("Disable this option to keep the dialog open"));
719 gtk_container_add(GTK_CONTAINER(bbox
), check_close
);
720 gtk_button_box_set_child_secondary(GTK_BUTTON_BOX(bbox
), check_close
, TRUE
);
722 ui_hbutton_box_copy_layout(
723 GTK_BUTTON_BOX(gtk_dialog_get_action_area(GTK_DIALOG(replace_dlg
.dialog
))),
724 GTK_BUTTON_BOX(bbox
));
725 gtk_container_add(GTK_CONTAINER(exp
), bbox
);
726 gtk_container_add(GTK_CONTAINER(vbox
), exp
);
730 void search_show_replace_dialog(void)
732 GeanyDocument
*doc
= document_get_current();
738 sel
= editor_get_default_selection(doc
->editor
, search_prefs
.use_current_word
, NULL
);
740 if (replace_dlg
.dialog
== NULL
)
742 create_replace_dialog();
743 stash_group_display(replace_prefs
, replace_dlg
.dialog
);
745 gtk_entry_set_text(GTK_ENTRY(replace_dlg
.find_entry
), sel
);
747 set_dialog_position(replace_dlg
.dialog
, replace_dlg
.position
);
748 gtk_widget_show_all(replace_dlg
.dialog
);
752 /* only set selection if the dialog is not already visible */
753 if (! gtk_widget_get_visible(replace_dlg
.dialog
) && sel
)
754 gtk_entry_set_text(GTK_ENTRY(replace_dlg
.find_entry
), sel
);
755 if (sel
!= NULL
) /* when we have a selection, reset the entry widget's background colour */
756 ui_set_search_entry_background(replace_dlg
.find_entry
, TRUE
);
757 gtk_widget_grab_focus(replace_dlg
.find_entry
);
758 set_dialog_position(replace_dlg
.dialog
, replace_dlg
.position
);
759 gtk_widget_show(replace_dlg
.dialog
);
760 /* bring the dialog back in the foreground in case it is already open but the focus is away */
761 gtk_window_present(GTK_WINDOW(replace_dlg
.dialog
));
768 static void on_widget_toggled_set_sensitive(GtkToggleButton
*togglebutton
, gpointer user_data
)
770 /* disable extra option entry when checkbutton not checked */
771 gtk_widget_set_sensitive(GTK_WIDGET(user_data
),
772 gtk_toggle_button_get_active(togglebutton
));
776 static void update_file_patterns(GtkWidget
*mode_combo
, GtkWidget
*fcombo
)
781 entry
= gtk_bin_get_child(GTK_BIN(fcombo
));
783 selection
= gtk_combo_box_get_active(GTK_COMBO_BOX(mode_combo
));
785 if (selection
== FILES_MODE_ALL
)
787 gtk_entry_set_text(GTK_ENTRY(entry
), "");
788 gtk_widget_set_sensitive(fcombo
, FALSE
);
790 else if (selection
== FILES_MODE_CUSTOM
)
792 gtk_widget_set_sensitive(fcombo
, TRUE
);
794 else if (selection
== FILES_MODE_PROJECT
)
796 if (app
->project
&& !EMPTY(app
->project
->file_patterns
))
800 patterns
= g_strjoinv(" ", app
->project
->file_patterns
);
801 gtk_entry_set_text(GTK_ENTRY(entry
), patterns
);
806 gtk_entry_set_text(GTK_ENTRY(entry
), "");
809 gtk_widget_set_sensitive(fcombo
, FALSE
);
814 /* creates the combo to choose which files include in the search */
815 static GtkWidget
*create_fif_file_mode_combo(void)
818 GtkCellRenderer
*renderer
;
823 store
= gtk_list_store_new(2, G_TYPE_STRING
, G_TYPE_BOOLEAN
);
824 gtk_list_store_append(store
, &iter
);
825 gtk_list_store_set(store
, &iter
, 0, _("all"), 1, TRUE
, -1);
826 gtk_list_store_append(store
, &iter
);
827 gtk_list_store_set(store
, &iter
, 0, _("project"), 1, app
->project
!= NULL
, -1);
828 gtk_list_store_append(store
, &iter
);
829 gtk_list_store_set(store
, &iter
, 0, _("custom"), 1, TRUE
, -1);
831 combo
= gtk_combo_box_new_with_model(GTK_TREE_MODEL(store
));
832 g_object_unref(store
);
833 gtk_widget_set_tooltip_text(combo
, _("All: search all files in the directory\n"
834 "Project: use file patterns defined in the project settings\n"
835 "Custom: specify file patterns manually"));
837 renderer
= gtk_cell_renderer_text_new();
838 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo
), renderer
, TRUE
);
839 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo
), renderer
, "text", 0, "sensitive", 1, NULL
);
845 /* updates the sensitivity of the project combo item */
846 static void update_fif_file_mode_combo(void)
848 GtkTreeModel
*model
= gtk_combo_box_get_model(GTK_COMBO_BOX(fif_dlg
.files_mode_combo
));
851 /* "1" refers to the second list entry, project */
852 if (gtk_tree_model_get_iter_from_string(model
, &iter
, "1"))
853 gtk_list_store_set(GTK_LIST_STORE(model
), &iter
, 1, app
->project
!= NULL
, -1);
857 static void create_fif_dialog(void)
859 GtkWidget
*dir_combo
, *combo
, *fcombo
, *e_combo
, *entry
;
860 GtkWidget
*label
, *label1
, *label2
, *label3
, *checkbox1
, *checkbox2
, *check_wholeword
,
861 *check_recursive
, *check_extra
, *entry_extra
, *check_regexp
, *combo_files_mode
;
862 GtkWidget
*dbox
, *sbox
, *lbox
, *rbox
, *hbox
, *vbox
, *ebox
;
863 GtkSizeGroup
*size_group
;
865 fif_dlg
.dialog
= gtk_dialog_new_with_buttons(
866 _("Find in Files"), GTK_WINDOW(main_widgets
.window
), GTK_DIALOG_DESTROY_WITH_PARENT
,
867 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
, NULL
);
868 vbox
= ui_dialog_vbox_new(GTK_DIALOG(fif_dlg
.dialog
));
869 gtk_box_set_spacing(GTK_BOX(vbox
), 9);
870 gtk_widget_set_name(fif_dlg
.dialog
, "GeanyDialogSearch");
872 gtk_dialog_add_button(GTK_DIALOG(fif_dlg
.dialog
), GTK_STOCK_FIND
, GTK_RESPONSE_ACCEPT
);
873 gtk_dialog_set_default_response(GTK_DIALOG(fif_dlg
.dialog
),
874 GTK_RESPONSE_ACCEPT
);
876 label
= gtk_label_new_with_mnemonic(_("_Search for:"));
877 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0.5);
879 combo
= gtk_combo_box_text_new_with_entry();
880 entry
= gtk_bin_get_child(GTK_BIN(combo
));
881 ui_entry_add_clear_icon(GTK_ENTRY(entry
));
882 gtk_label_set_mnemonic_widget(GTK_LABEL(label
), entry
);
883 gtk_entry_set_width_chars(GTK_ENTRY(entry
), 50);
884 gtk_entry_set_activates_default(GTK_ENTRY(entry
), TRUE
);
885 fif_dlg
.search_combo
= combo
;
887 sbox
= gtk_hbox_new(FALSE
, 6);
888 gtk_box_pack_start(GTK_BOX(sbox
), label
, FALSE
, FALSE
, 0);
889 gtk_box_pack_start(GTK_BOX(sbox
), combo
, TRUE
, TRUE
, 0);
891 /* make labels same width */
892 size_group
= gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL
);
893 gtk_size_group_add_widget(size_group
, label
);
895 label3
= gtk_label_new_with_mnemonic(_("Fi_les:"));
896 gtk_misc_set_alignment(GTK_MISC(label3
), 0, 0.5);
898 combo_files_mode
= create_fif_file_mode_combo();
899 gtk_label_set_mnemonic_widget(GTK_LABEL(label3
), combo_files_mode
);
900 ui_hookup_widget(fif_dlg
.dialog
, combo_files_mode
, "combo_files_mode");
901 fif_dlg
.files_mode_combo
= combo_files_mode
;
903 fcombo
= gtk_combo_box_text_new_with_entry();
904 entry
= gtk_bin_get_child(GTK_BIN(fcombo
));
905 ui_entry_add_clear_icon(GTK_ENTRY(entry
));
906 gtk_entry_set_activates_default(GTK_ENTRY(entry
), TRUE
);
907 gtk_widget_set_tooltip_text(entry
, _("File patterns, e.g. *.c *.h"));
908 ui_hookup_widget(fif_dlg
.dialog
, entry
, "entry_files");
909 fif_dlg
.files_combo
= fcombo
;
911 /* update the entry when selection is changed */
912 g_signal_connect(combo_files_mode
, "changed", G_CALLBACK(update_file_patterns
), fcombo
);
914 hbox
= gtk_hbox_new(FALSE
, 6);
915 gtk_box_pack_start(GTK_BOX(hbox
), label3
, FALSE
, FALSE
, 0);
916 gtk_box_pack_start(GTK_BOX(hbox
), combo_files_mode
, FALSE
, FALSE
, 0);
917 gtk_box_pack_start(GTK_BOX(hbox
), fcombo
, TRUE
, TRUE
, 0);
919 label1
= gtk_label_new_with_mnemonic(_("_Directory:"));
920 gtk_misc_set_alignment(GTK_MISC(label1
), 0, 0.5);
922 dir_combo
= gtk_combo_box_text_new_with_entry();
923 entry
= gtk_bin_get_child(GTK_BIN(dir_combo
));
924 ui_entry_add_clear_icon(GTK_ENTRY(entry
));
925 gtk_entry_set_activates_default(GTK_ENTRY(entry
), TRUE
);
926 gtk_label_set_mnemonic_widget(GTK_LABEL(label1
), entry
);
927 gtk_entry_set_width_chars(GTK_ENTRY(entry
), 50);
928 fif_dlg
.dir_combo
= dir_combo
;
930 /* tab from files to the dir entry */
931 g_signal_connect(gtk_bin_get_child(GTK_BIN(fcombo
)), "key-press-event",
932 G_CALLBACK(on_widget_key_pressed_set_focus
), entry
);
934 dbox
= ui_path_box_new(NULL
, GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
,
936 gtk_box_pack_start(GTK_BOX(dbox
), label1
, FALSE
, FALSE
, 0);
938 label2
= gtk_label_new_with_mnemonic(_("E_ncoding:"));
939 gtk_misc_set_alignment(GTK_MISC(label2
), 0, 0.5);
941 e_combo
= ui_create_encodings_combo_box(FALSE
, GEANY_ENCODING_UTF_8
);
942 gtk_label_set_mnemonic_widget(GTK_LABEL(label2
), e_combo
);
943 fif_dlg
.encoding_combo
= e_combo
;
945 ebox
= gtk_hbox_new(FALSE
, 6);
946 gtk_box_pack_start(GTK_BOX(ebox
), label2
, FALSE
, FALSE
, 0);
947 gtk_box_pack_start(GTK_BOX(ebox
), e_combo
, TRUE
, TRUE
, 0);
949 gtk_size_group_add_widget(size_group
, label1
);
950 gtk_size_group_add_widget(size_group
, label2
);
951 gtk_size_group_add_widget(size_group
, label3
);
952 g_object_unref(G_OBJECT(size_group
)); /* auto destroy the size group */
954 gtk_box_pack_start(GTK_BOX(vbox
), sbox
, TRUE
, FALSE
, 0);
955 gtk_box_pack_start(GTK_BOX(vbox
), hbox
, TRUE
, FALSE
, 0);
956 gtk_box_pack_start(GTK_BOX(vbox
), dbox
, TRUE
, FALSE
, 0);
957 gtk_box_pack_start(GTK_BOX(vbox
), ebox
, TRUE
, FALSE
, 0);
959 check_regexp
= gtk_check_button_new_with_mnemonic(_("_Use regular expressions"));
960 ui_hookup_widget(fif_dlg
.dialog
, check_regexp
, "check_regexp");
961 gtk_button_set_focus_on_click(GTK_BUTTON(check_regexp
), FALSE
);
962 gtk_widget_set_tooltip_text(check_regexp
, _("See grep's manual page for more information"));
964 check_recursive
= gtk_check_button_new_with_mnemonic(_("_Recurse in subfolders"));
965 ui_hookup_widget(fif_dlg
.dialog
, check_recursive
, "check_recursive");
966 gtk_button_set_focus_on_click(GTK_BUTTON(check_recursive
), FALSE
);
968 checkbox1
= gtk_check_button_new_with_mnemonic(_("C_ase sensitive"));
969 ui_hookup_widget(fif_dlg
.dialog
, checkbox1
, "check_case");
970 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox1
), FALSE
);
971 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox1
), TRUE
);
973 check_wholeword
= gtk_check_button_new_with_mnemonic(_("Match only a _whole word"));
974 ui_hookup_widget(fif_dlg
.dialog
, check_wholeword
, "check_wholeword");
975 gtk_button_set_focus_on_click(GTK_BUTTON(check_wholeword
), FALSE
);
977 checkbox2
= gtk_check_button_new_with_mnemonic(_("_Invert search results"));
978 ui_hookup_widget(fif_dlg
.dialog
, checkbox2
, "check_invert");
979 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox2
), FALSE
);
980 gtk_widget_set_tooltip_text(checkbox2
,
981 _("Invert the sense of matching, to select non-matching lines"));
983 lbox
= gtk_vbox_new(FALSE
, 0);
984 gtk_container_add(GTK_CONTAINER(lbox
), check_regexp
);
985 gtk_container_add(GTK_CONTAINER(lbox
), checkbox2
);
986 gtk_container_add(GTK_CONTAINER(lbox
), check_recursive
);
988 rbox
= gtk_vbox_new(FALSE
, 0);
989 gtk_container_add(GTK_CONTAINER(rbox
), checkbox1
);
990 gtk_container_add(GTK_CONTAINER(rbox
), check_wholeword
);
991 gtk_container_add(GTK_CONTAINER(rbox
), gtk_label_new(NULL
));
993 hbox
= gtk_hbox_new(FALSE
, 6);
994 gtk_container_add(GTK_CONTAINER(hbox
), lbox
);
995 gtk_container_add(GTK_CONTAINER(hbox
), rbox
);
996 gtk_container_add(GTK_CONTAINER(vbox
), hbox
);
998 check_extra
= gtk_check_button_new_with_mnemonic(_("E_xtra options:"));
999 ui_hookup_widget(fif_dlg
.dialog
, check_extra
, "check_extra");
1000 gtk_button_set_focus_on_click(GTK_BUTTON(check_extra
), FALSE
);
1002 entry_extra
= gtk_entry_new();
1003 ui_entry_add_clear_icon(GTK_ENTRY(entry_extra
));
1004 gtk_entry_set_activates_default(GTK_ENTRY(entry_extra
), TRUE
);
1005 gtk_widget_set_sensitive(entry_extra
, FALSE
);
1006 gtk_widget_set_tooltip_text(entry_extra
, _("Other options to pass to Grep"));
1007 ui_hookup_widget(fif_dlg
.dialog
, entry_extra
, "entry_extra");
1009 /* enable entry_extra when check_extra is checked */
1010 g_signal_connect(check_extra
, "toggled",
1011 G_CALLBACK(on_widget_toggled_set_sensitive
), entry_extra
);
1013 hbox
= gtk_hbox_new(FALSE
, 6);
1014 gtk_box_pack_start(GTK_BOX(hbox
), check_extra
, FALSE
, FALSE
, 0);
1015 gtk_box_pack_start(GTK_BOX(hbox
), entry_extra
, TRUE
, TRUE
, 0);
1016 gtk_container_add(GTK_CONTAINER(vbox
), hbox
);
1018 g_signal_connect(fif_dlg
.dialog
, "response",
1019 G_CALLBACK(on_find_in_files_dialog_response
), NULL
);
1020 g_signal_connect(fif_dlg
.dialog
, "delete-event",
1021 G_CALLBACK(gtk_widget_hide_on_delete
), NULL
);
1026 * Shows the Find in Files dialog.
1028 * @param dir The directory to search in (UTF-8 encoding). May be @c NULL
1029 * to determine it the usual way by using the current document's path.
1031 * @since 0.14, plugin API 53
1034 void search_show_find_in_files_dialog(const gchar
*dir
)
1036 search_show_find_in_files_dialog_full(NULL
, dir
);
1040 void search_show_find_in_files_dialog_full(const gchar
*text
, const gchar
*dir
)
1042 GtkWidget
*entry
; /* for child GtkEntry of a GtkComboBoxEntry */
1043 GeanyDocument
*doc
= document_get_current();
1045 gchar
*cur_dir
= NULL
;
1046 GeanyEncodingIndex enc_idx
= GEANY_ENCODING_UTF_8
;
1048 if (fif_dlg
.dialog
== NULL
)
1050 create_fif_dialog();
1051 gtk_widget_show_all(fif_dlg
.dialog
);
1053 sel
= editor_get_default_selection(doc
->editor
, search_prefs
.use_current_word
, NULL
);
1055 stash_group_display(fif_prefs
, fif_dlg
.dialog
);
1059 /* only set selection if the dialog is not already visible, or has just been created */
1060 if (doc
&& ! sel
&& ! gtk_widget_get_visible(fif_dlg
.dialog
))
1061 sel
= editor_get_default_selection(doc
->editor
, search_prefs
.use_current_word
, NULL
);
1065 entry
= gtk_bin_get_child(GTK_BIN(fif_dlg
.search_combo
));
1067 gtk_entry_set_text(GTK_ENTRY(entry
), text
);
1070 /* add project's base path directory to the dir list, we do this here once
1071 * (in create_fif_dialog() it would fail if a project is opened after dialog creation) */
1072 if (app
->project
!= NULL
&& !EMPTY(app
->project
->base_path
))
1074 ui_combo_box_prepend_text_once(GTK_COMBO_BOX_TEXT(fif_dlg
.dir_combo
),
1075 app
->project
->base_path
);
1078 entry
= gtk_bin_get_child(GTK_BIN(fif_dlg
.dir_combo
));
1080 cur_dir
= g_strdup(dir
); /* custom directory argument passed */
1083 if (search_prefs
.use_current_file_dir
)
1085 static gchar
*last_cur_dir
= NULL
;
1086 static GeanyDocument
*last_doc
= NULL
;
1088 /* Only set the directory entry once for the current document */
1089 cur_dir
= utils_get_current_file_dir_utf8();
1090 if (doc
== last_doc
&& cur_dir
&& utils_str_equal(cur_dir
, last_cur_dir
))
1092 /* in case the user now wants the current directory, add it to history */
1093 ui_combo_box_add_to_history(GTK_COMBO_BOX_TEXT(fif_dlg
.dir_combo
), cur_dir
, 0);
1094 SETPTR(cur_dir
, NULL
);
1097 SETPTR(last_cur_dir
, g_strdup(cur_dir
));
1101 if (!cur_dir
&& EMPTY(gtk_entry_get_text(GTK_ENTRY(entry
))))
1103 /* use default_open_path if no directory could be determined
1104 * (e.g. when no files are open) */
1106 cur_dir
= g_strdup(utils_get_default_dir_utf8());
1108 cur_dir
= g_get_current_dir();
1113 gtk_entry_set_text(GTK_ENTRY(entry
), cur_dir
);
1117 update_fif_file_mode_combo();
1118 update_file_patterns(fif_dlg
.files_mode_combo
, fif_dlg
.files_combo
);
1120 /* set the encoding of the current file */
1122 enc_idx
= encodings_get_idx_from_charset(doc
->encoding
);
1123 ui_encodings_combo_box_set_active_encoding(GTK_COMBO_BOX(fif_dlg
.encoding_combo
), enc_idx
);
1125 /* put the focus to the directory entry if it is empty */
1126 if (utils_str_equal(gtk_entry_get_text(GTK_ENTRY(entry
)), ""))
1127 gtk_widget_grab_focus(fif_dlg
.dir_combo
);
1129 gtk_widget_grab_focus(fif_dlg
.search_combo
);
1131 /* set dialog window position */
1132 set_dialog_position(fif_dlg
.dialog
, fif_dlg
.position
);
1134 gtk_widget_show(fif_dlg
.dialog
);
1135 /* bring the dialog back in the foreground in case it is already open but the focus is away */
1136 gtk_window_present(GTK_WINDOW(fif_dlg
.dialog
));
1141 on_find_replace_checkbutton_toggled(GtkToggleButton
*togglebutton
, gpointer user_data
)
1143 GtkWidget
*dialog
= GTK_WIDGET(user_data
);
1144 GtkToggleButton
*chk_regexp
= GTK_TOGGLE_BUTTON(
1145 ui_lookup_widget(dialog
, "check_regexp"));
1147 if (togglebutton
== chk_regexp
)
1149 gboolean regex_set
= gtk_toggle_button_get_active(chk_regexp
);
1150 GtkWidget
*check_word
= ui_lookup_widget(dialog
, "check_word");
1151 GtkWidget
*check_wordstart
= ui_lookup_widget(dialog
, "check_wordstart");
1152 GtkWidget
*check_escape
= ui_lookup_widget(dialog
, "check_escape");
1153 GtkWidget
*check_multiline
= ui_lookup_widget(dialog
, "check_multiline");
1154 gboolean replace
= (dialog
!= find_dlg
.dialog
);
1155 const char *back_button
[2] = { "btn_previous" , "check_back" };
1157 /* hide options that don't apply to regex searches */
1158 gtk_widget_set_sensitive(check_escape
, ! regex_set
);
1159 gtk_widget_set_sensitive(ui_lookup_widget(dialog
, back_button
[replace
]), ! regex_set
);
1160 gtk_widget_set_sensitive(check_word
, ! regex_set
);
1161 gtk_widget_set_sensitive(check_wordstart
, ! regex_set
);
1162 gtk_widget_set_sensitive(check_multiline
, regex_set
);
1167 static GeanyMatchInfo
*match_info_new(GeanyFindFlags flags
, gint start
, gint end
)
1169 GeanyMatchInfo
*info
= g_slice_alloc(sizeof *info
);
1171 info
->flags
= flags
;
1172 info
->start
= start
;
1174 info
->match_text
= NULL
;
1179 void geany_match_info_free(GeanyMatchInfo
*info
)
1181 g_free(info
->match_text
);
1182 g_slice_free1(sizeof *info
, info
);
1186 /* find all in the given range.
1187 * Returns a list of allocated GeanyMatchInfo, should be freed using:
1189 * foreach_slist(node, matches)
1190 * geany_match_info_free(node->data);
1191 * g_slist_free(matches); */
1192 static GSList
*find_range(ScintillaObject
*sci
, GeanyFindFlags flags
, struct Sci_TextToFind
*ttf
)
1194 GSList
*matches
= NULL
;
1195 GeanyMatchInfo
*info
;
1197 g_return_val_if_fail(sci
!= NULL
&& ttf
->lpstrText
!= NULL
, NULL
);
1198 if (! *ttf
->lpstrText
)
1201 while (search_find_text(sci
, flags
, ttf
, &info
) != -1)
1203 if (ttf
->chrgText
.cpMax
> ttf
->chrg
.cpMax
)
1205 /* found text is partially out of range */
1206 geany_match_info_free(info
);
1210 matches
= g_slist_prepend(matches
, info
);
1211 ttf
->chrg
.cpMin
= ttf
->chrgText
.cpMax
;
1213 /* avoid rematching with empty matches like "(?=[a-z])" or "^$".
1214 * note we cannot assume a match will always be empty or not and then break out, since
1215 * matches like "a?(?=b)" will sometimes be empty and sometimes not */
1216 if (ttf
->chrgText
.cpMax
== ttf
->chrgText
.cpMin
)
1220 return g_slist_reverse(matches
);
1224 /* Clears markers if text is null/empty.
1225 * @return Number of matches marked. */
1226 gint
search_mark_all(GeanyDocument
*doc
, const gchar
*search_text
, GeanyFindFlags flags
)
1229 struct Sci_TextToFind ttf
;
1230 GSList
*match
, *matches
;
1232 g_return_val_if_fail(DOC_VALID(doc
), 0);
1234 /* clear previous search indicators */
1235 editor_indicator_clear(doc
->editor
, GEANY_INDICATOR_SEARCH
);
1237 if (G_UNLIKELY(EMPTY(search_text
)))
1241 ttf
.chrg
.cpMax
= sci_get_length(doc
->editor
->sci
);
1242 ttf
.lpstrText
= (gchar
*)search_text
;
1244 matches
= find_range(doc
->editor
->sci
, flags
, &ttf
);
1245 foreach_slist (match
, matches
)
1247 GeanyMatchInfo
*info
= match
->data
;
1249 if (info
->end
!= info
->start
)
1250 editor_indicator_set_on_range(doc
->editor
, GEANY_INDICATOR_SEARCH
, info
->start
, info
->end
);
1253 geany_match_info_free(info
);
1255 g_slist_free(matches
);
1262 on_find_entry_activate(GtkEntry
*entry
, gpointer user_data
)
1264 on_find_dialog_response(NULL
, GEANY_RESPONSE_FIND
, user_data
);
1269 on_find_entry_activate_backward(GtkEntry
*entry
, gpointer user_data
)
1271 /* can't search backwards with a regexp */
1272 if (search_data
.flags
& GEANY_FIND_REGEXP
)
1275 on_find_dialog_response(NULL
, GEANY_RESPONSE_FIND_PREVIOUS
, user_data
);
1279 static GeanyFindFlags
int_search_flags(gint match_case
, gint whole_word
, gint regexp
, gint multiline
, gint word_start
)
1281 return (match_case
? GEANY_FIND_MATCHCASE
: 0) |
1282 (regexp
? GEANY_FIND_REGEXP
: 0) |
1283 (whole_word
? GEANY_FIND_WHOLEWORD
: 0) |
1284 (multiline
? GEANY_FIND_MULTILINE
: 0) |
1285 /* SCFIND_WORDSTART overrides SCFIND_WHOLEWORD, but we want the opposite */
1286 (word_start
&& !whole_word
? GEANY_FIND_WORDSTART
: 0);
1291 on_find_dialog_response(GtkDialog
*dialog
, gint response
, gpointer user_data
)
1293 gtk_window_get_position(GTK_WINDOW(find_dlg
.dialog
),
1294 &find_dlg
.position
[0], &find_dlg
.position
[1]);
1296 stash_group_update(find_prefs
, find_dlg
.dialog
);
1298 if (response
== GTK_RESPONSE_CANCEL
|| response
== GTK_RESPONSE_DELETE_EVENT
)
1299 gtk_widget_hide(find_dlg
.dialog
);
1302 GeanyDocument
*doc
= document_get_current();
1303 gboolean check_close
= settings
.find_close_dialog
;
1308 search_data
.backwards
= FALSE
;
1309 search_data
.search_bar
= FALSE
;
1311 g_free(search_data
.text
);
1312 g_free(search_data
.original_text
);
1313 search_data
.text
= g_strdup(gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(user_data
)))));
1314 search_data
.original_text
= g_strdup(search_data
.text
);
1315 search_data
.flags
= int_search_flags(settings
.find_case_sensitive
,
1316 settings
.find_match_whole_word
, settings
.find_regexp
, settings
.find_regexp_multiline
,
1317 settings
.find_match_word_start
);
1319 if (EMPTY(search_data
.text
))
1323 gtk_widget_grab_focus(find_dlg
.entry
);
1326 if (search_data
.flags
& GEANY_FIND_REGEXP
)
1328 GRegex
*regex
= compile_regex(search_data
.text
, search_data
.flags
);
1332 g_regex_unref(regex
);
1334 else if (settings
.find_escape_sequences
)
1336 if (! utils_str_replace_escape(search_data
.text
, FALSE
))
1339 ui_combo_box_add_to_history(GTK_COMBO_BOX_TEXT(user_data
), search_data
.original_text
, 0);
1343 case GEANY_RESPONSE_FIND
:
1344 case GEANY_RESPONSE_FIND_PREVIOUS
:
1346 gint result
= document_find_text(doc
, search_data
.text
, search_data
.original_text
, search_data
.flags
,
1347 (response
== GEANY_RESPONSE_FIND_PREVIOUS
), NULL
, TRUE
, GTK_WIDGET(find_dlg
.dialog
));
1348 ui_set_search_entry_background(find_dlg
.entry
, (result
> -1));
1349 check_close
= search_prefs
.hide_find_dialog
;
1352 case GEANY_RESPONSE_FIND_IN_FILE
:
1353 search_find_usage(search_data
.text
, search_data
.original_text
, search_data
.flags
, FALSE
);
1356 case GEANY_RESPONSE_FIND_IN_SESSION
:
1357 search_find_usage(search_data
.text
, search_data
.original_text
, search_data
.flags
, TRUE
);
1360 case GEANY_RESPONSE_MARK
:
1362 gint count
= search_mark_all(doc
, search_data
.text
, search_data
.flags
);
1365 ui_set_statusbar(FALSE
, _("No matches found for \"%s\"."), search_data
.original_text
);
1367 ui_set_statusbar(FALSE
,
1368 ngettext("Found %d match for \"%s\".",
1369 "Found %d matches for \"%s\".", count
),
1370 count
, search_data
.original_text
);
1375 gtk_widget_hide(find_dlg
.dialog
);
1381 on_replace_find_entry_activate(GtkEntry
*entry
, gpointer user_data
)
1383 on_replace_dialog_response(NULL
, GEANY_RESPONSE_FIND
, NULL
);
1388 on_replace_entry_activate(GtkEntry
*entry
, gpointer user_data
)
1390 on_replace_dialog_response(NULL
,
1391 search_prefs
.replace_and_find_by_default
? GEANY_RESPONSE_REPLACE_AND_FIND
: GEANY_RESPONSE_REPLACE
,
1396 static void replace_in_session(GeanyDocument
*doc
,
1397 GeanyFindFlags search_flags_re
, gboolean search_replace_escape_re
,
1398 const gchar
*find
, const gchar
*replace
,
1399 const gchar
*original_find
, const gchar
*original_replace
)
1401 guint n
, page_count
, rep_count
= 0, file_count
= 0;
1403 /* replace in all documents following notebook tab order */
1404 page_count
= gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets
.notebook
));
1405 for (n
= 0; n
< page_count
; n
++)
1407 GeanyDocument
*tmp_doc
= document_get_from_page(n
);
1410 reps
= document_replace_all(tmp_doc
, find
, replace
, original_find
, original_replace
, search_flags_re
);
1415 if (file_count
== 0)
1418 ui_set_statusbar(FALSE
, _("No matches found for \"%s\"."), original_find
);
1421 /* if only one file was changed, don't override that document's status message
1422 * so we don't have to translate 4 messages for ngettext */
1424 ui_set_statusbar(FALSE
, _("Replaced %u matches in %u documents."),
1425 rep_count
, file_count
);
1427 /* show which docs had replacements: */
1428 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow
.notebook
), MSG_STATUS
);
1430 ui_save_buttons_toggle(doc
->changed
); /* update save all */
1435 on_replace_dialog_response(GtkDialog
*dialog
, gint response
, gpointer user_data
)
1437 GeanyDocument
*doc
= document_get_current();
1438 GeanyFindFlags search_flags_re
;
1439 gboolean search_backwards_re
, search_replace_escape_re
;
1440 gchar
*find
, *replace
, *original_find
= NULL
, *original_replace
= NULL
;
1442 gtk_window_get_position(GTK_WINDOW(replace_dlg
.dialog
),
1443 &replace_dlg
.position
[0], &replace_dlg
.position
[1]);
1445 stash_group_update(replace_prefs
, replace_dlg
.dialog
);
1447 if (response
== GTK_RESPONSE_CANCEL
|| response
== GTK_RESPONSE_DELETE_EVENT
)
1449 gtk_widget_hide(replace_dlg
.dialog
);
1453 search_backwards_re
= settings
.replace_search_backwards
;
1454 search_replace_escape_re
= settings
.replace_escape_sequences
;
1455 find
= g_strdup(gtk_entry_get_text(GTK_ENTRY(replace_dlg
.find_entry
)));
1456 replace
= g_strdup(gtk_entry_get_text(GTK_ENTRY(replace_dlg
.replace_entry
)));
1458 search_flags_re
= int_search_flags(settings
.replace_case_sensitive
,
1459 settings
.replace_match_whole_word
, settings
.replace_regexp
,
1460 settings
.replace_regexp_multiline
, settings
.replace_match_word_start
);
1462 if ((response
!= GEANY_RESPONSE_FIND
) && (search_flags_re
& GEANY_FIND_MATCHCASE
)
1463 && (strcmp(find
, replace
) == 0))
1466 original_find
= g_strdup(find
);
1467 original_replace
= g_strdup(replace
);
1469 if (search_flags_re
& GEANY_FIND_REGEXP
)
1471 GRegex
*regex
= compile_regex(find
, search_flags_re
);
1473 g_regex_unref(regex
);
1474 /* find escapes will be handled by GRegex */
1475 if (!regex
|| !utils_str_replace_escape(replace
, TRUE
))
1478 else if (search_replace_escape_re
)
1480 if (! utils_str_replace_escape(find
, FALSE
) ||
1481 ! utils_str_replace_escape(replace
, FALSE
))
1485 ui_combo_box_add_to_history(GTK_COMBO_BOX_TEXT(
1486 gtk_widget_get_parent(replace_dlg
.find_entry
)), original_find
, 0);
1487 ui_combo_box_add_to_history(GTK_COMBO_BOX_TEXT(
1488 gtk_widget_get_parent(replace_dlg
.replace_entry
)), original_replace
, 0);
1492 case GEANY_RESPONSE_REPLACE_AND_FIND
:
1494 gint rep
= document_replace_text(doc
, find
, original_find
, replace
, search_flags_re
,
1495 search_backwards_re
);
1497 document_find_text(doc
, find
, original_find
, search_flags_re
, search_backwards_re
,
1501 case GEANY_RESPONSE_REPLACE
:
1502 document_replace_text(doc
, find
, original_find
, replace
, search_flags_re
,
1503 search_backwards_re
);
1506 case GEANY_RESPONSE_FIND
:
1508 gint result
= document_find_text(doc
, find
, original_find
, search_flags_re
,
1509 search_backwards_re
, NULL
, TRUE
, GTK_WIDGET(dialog
));
1510 ui_set_search_entry_background(replace_dlg
.find_entry
, (result
> -1));
1513 case GEANY_RESPONSE_REPLACE_IN_FILE
:
1514 if (! document_replace_all(doc
, find
, replace
, original_find
, original_replace
, search_flags_re
))
1518 case GEANY_RESPONSE_REPLACE_IN_SESSION
:
1519 replace_in_session(doc
, search_flags_re
, search_replace_escape_re
, find
, replace
, original_find
, original_replace
);
1522 case GEANY_RESPONSE_REPLACE_IN_SEL
:
1523 document_replace_sel(doc
, find
, replace
, original_find
, original_replace
, search_flags_re
);
1528 case GEANY_RESPONSE_REPLACE_IN_SEL
:
1529 case GEANY_RESPONSE_REPLACE_IN_FILE
:
1530 case GEANY_RESPONSE_REPLACE_IN_SESSION
:
1531 if (settings
.replace_close_dialog
)
1532 gtk_widget_hide(replace_dlg
.dialog
);
1536 g_free(original_find
);
1537 g_free(original_replace
);
1542 gtk_widget_grab_focus(replace_dlg
.find_entry
);
1545 g_free(original_find
);
1546 g_free(original_replace
);
1550 static GString
*get_grep_options(void)
1552 GString
*gstr
= g_string_new("-nHI"); /* line numbers, filenames, ignore binaries */
1554 if (settings
.fif_invert_results
)
1555 g_string_append_c(gstr
, 'v');
1556 if (! settings
.fif_case_sensitive
)
1557 g_string_append_c(gstr
, 'i');
1558 if (settings
.fif_match_whole_word
)
1559 g_string_append_c(gstr
, 'w');
1560 if (settings
.fif_recursive
)
1561 g_string_append_c(gstr
, 'r');
1563 if (!settings
.fif_regexp
)
1564 g_string_append_c(gstr
, 'F');
1566 g_string_append_c(gstr
, 'E');
1568 if (settings
.fif_use_extra_options
)
1570 g_strstrip(settings
.fif_extra_options
);
1572 if (*settings
.fif_extra_options
!= 0)
1574 g_string_append_c(gstr
, ' ');
1575 g_string_append(gstr
, settings
.fif_extra_options
);
1578 g_strstrip(settings
.fif_files
);
1579 if (settings
.fif_files_mode
!= FILES_MODE_ALL
&& *settings
.fif_files
)
1583 /* put --include= before each pattern */
1584 tmp
= g_string_new(settings
.fif_files
);
1585 do {} while (utils_string_replace_all(tmp
, " ", " "));
1586 g_string_prepend_c(tmp
, ' ');
1587 utils_string_replace_all(tmp
, " ", " --include=");
1588 g_string_append(gstr
, tmp
->str
);
1589 g_string_free(tmp
, TRUE
);
1596 on_find_in_files_dialog_response(GtkDialog
*dialog
, gint response
,
1597 G_GNUC_UNUSED gpointer user_data
)
1599 gtk_window_get_position(GTK_WINDOW(fif_dlg
.dialog
), &fif_dlg
.position
[0], &fif_dlg
.position
[1]);
1601 stash_group_update(fif_prefs
, fif_dlg
.dialog
);
1603 if (response
== GTK_RESPONSE_ACCEPT
)
1605 GtkWidget
*search_combo
= fif_dlg
.search_combo
;
1606 const gchar
*search_text
=
1607 gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(search_combo
))));
1608 GtkWidget
*dir_combo
= fif_dlg
.dir_combo
;
1609 const gchar
*utf8_dir
=
1610 gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(dir_combo
))));
1611 GeanyEncodingIndex enc_idx
=
1612 ui_encodings_combo_box_get_active_encoding(GTK_COMBO_BOX(fif_dlg
.encoding_combo
));
1614 if (G_UNLIKELY(EMPTY(utf8_dir
)))
1615 ui_set_statusbar(FALSE
, _("Invalid directory for find in files."));
1616 else if (!EMPTY(search_text
))
1618 GString
*opts
= get_grep_options();
1619 const gchar
*enc
= (enc_idx
== GEANY_ENCODING_UTF_8
) ? NULL
:
1620 encodings_get_charset_from_index(enc_idx
);
1622 if (search_find_in_files(search_text
, utf8_dir
, opts
->str
, enc
))
1624 ui_combo_box_add_to_history(GTK_COMBO_BOX_TEXT(search_combo
), search_text
, 0);
1625 ui_combo_box_add_to_history(GTK_COMBO_BOX_TEXT(fif_dlg
.files_combo
), NULL
, 0);
1626 ui_combo_box_add_to_history(GTK_COMBO_BOX_TEXT(dir_combo
), utf8_dir
, 0);
1627 gtk_widget_hide(fif_dlg
.dialog
);
1629 g_string_free(opts
, TRUE
);
1632 ui_set_statusbar(FALSE
, _("No text to find."));
1635 gtk_widget_hide(fif_dlg
.dialog
);
1640 search_find_in_files(const gchar
*utf8_search_text
, const gchar
*utf8_dir
, const gchar
*opts
,
1643 gchar
**argv_prefix
, **argv
;
1644 gchar
*command_grep
;
1645 gchar
*command_line
, *dir
;
1646 gchar
*search_text
= NULL
;
1647 GError
*error
= NULL
;
1648 gboolean ret
= FALSE
;
1649 gssize utf8_text_len
;
1651 if (EMPTY(utf8_search_text
) || ! utf8_dir
) return TRUE
;
1653 command_grep
= g_find_program_in_path(tool_prefs
.grep_cmd
);
1654 if (command_grep
== NULL
)
1655 command_line
= g_strdup_printf("%s %s --", tool_prefs
.grep_cmd
, opts
);
1658 command_line
= g_strdup_printf("\"%s\" %s --", command_grep
, opts
);
1659 g_free(command_grep
);
1662 /* convert the search text in the preferred encoding (if the text is not valid UTF-8. assume
1663 * it is already in the preferred encoding) */
1664 utf8_text_len
= strlen(utf8_search_text
);
1665 if (enc
!= NULL
&& g_utf8_validate(utf8_search_text
, utf8_text_len
, NULL
))
1667 search_text
= g_convert(utf8_search_text
, utf8_text_len
, enc
, "UTF-8", NULL
, NULL
, NULL
);
1669 if (search_text
== NULL
)
1670 search_text
= g_strdup(utf8_search_text
);
1672 argv_prefix
= g_new(gchar
*, 3);
1673 argv_prefix
[0] = search_text
;
1674 dir
= utils_get_locale_from_utf8(utf8_dir
);
1676 /* finally add the arguments(files to be searched) */
1677 if (settings
.fif_recursive
) /* recursive option set */
1679 /* Use '.' so we get relative paths in the output */
1680 argv_prefix
[1] = g_strdup(".");
1681 argv_prefix
[2] = NULL
;
1686 argv_prefix
[1] = NULL
;
1687 argv
= search_get_argv((const gchar
**)argv_prefix
, dir
);
1688 g_strfreev(argv_prefix
);
1690 if (argv
== NULL
) /* no files */
1692 g_free(command_line
);
1697 gtk_list_store_clear(msgwindow
.store_msg
);
1698 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow
.notebook
), MSG_MESSAGE
);
1700 /* we can pass 'enc' without strdup'ing it here because it's a global const string and
1701 * always exits longer than the lifetime of this function */
1702 if (spawn_with_callbacks(dir
, command_line
, argv
, NULL
, 0, NULL
, NULL
, search_read_io
,
1703 (gpointer
) enc
, 0, search_read_io_stderr
, (gpointer
) enc
, 0, search_finished
, NULL
,
1708 ui_progress_bar_start(_("Searching..."));
1709 msgwin_set_messages_dir(dir
);
1710 utf8_str
= g_strdup_printf(_("%s %s -- %s (in directory: %s)"),
1711 tool_prefs
.grep_cmd
, opts
, utf8_search_text
, utf8_dir
);
1712 msgwin_msg_add_string(COLOR_BLUE
, -1, NULL
, utf8_str
);
1718 geany_debug("%s: spawn_with_callbacks() failed: %s", G_STRFUNC
, error
->message
);
1719 ui_set_statusbar(TRUE
, _("Process failed (%s)"), error
->message
);
1720 g_error_free(error
);
1723 utils_free_pointers(2, dir
, command_line
, NULL
);
1729 static gboolean
pattern_list_match(GSList
*patterns
, const gchar
*str
)
1733 foreach_slist(item
, patterns
)
1735 if (g_pattern_match_string(item
->data
, str
))
1742 /* Creates an argument vector of strings, copying argv_prefix[] values for
1743 * the first arguments, then followed by filenames found in dir.
1744 * Returns NULL if no files were found, otherwise returned vector should be fully freed. */
1745 static gchar
**search_get_argv(const gchar
**argv_prefix
, const gchar
*dir
)
1747 guint prefix_len
, list_len
, i
, j
;
1749 GSList
*list
, *item
, *patterns
= NULL
;
1750 GError
*error
= NULL
;
1752 g_return_val_if_fail(dir
!= NULL
, NULL
);
1754 prefix_len
= g_strv_length((gchar
**)argv_prefix
);
1755 list
= utils_get_file_list(dir
, &list_len
, &error
);
1758 ui_set_statusbar(TRUE
, _("Could not open directory (%s)"), error
->message
);
1759 g_error_free(error
);
1765 argv
= g_new(gchar
*, prefix_len
+ list_len
+ 1);
1767 for (i
= 0, j
= 0; i
< prefix_len
; i
++)
1769 if (g_str_has_prefix(argv_prefix
[i
], "--include="))
1771 const gchar
*pat
= &(argv_prefix
[i
][10]); /* the pattern part of the argument */
1773 patterns
= g_slist_prepend(patterns
, g_pattern_spec_new(pat
));
1776 argv
[j
++] = g_strdup(argv_prefix
[i
]);
1783 foreach_slist(item
, list
)
1785 if (pattern_list_match(patterns
, item
->data
))
1786 argv
[j
++] = item
->data
;
1790 foreach_slist(pat
, patterns
)
1791 g_pattern_spec_free(pat
->data
);
1792 g_slist_free(patterns
);
1796 foreach_slist(item
, list
)
1797 argv
[j
++] = item
->data
;
1806 static void read_fif_io(gchar
*msg
, GIOCondition condition
, gchar
*enc
, gint msg_color
)
1808 if (condition
& (G_IO_IN
| G_IO_PRI
))
1810 gchar
*utf8_msg
= NULL
;
1813 /* enc is NULL when encoding is set to UTF-8, so we can skip any conversion */
1816 if (! g_utf8_validate(msg
, -1, NULL
))
1818 utf8_msg
= g_convert(msg
, -1, "UTF-8", enc
, NULL
, NULL
, NULL
);
1820 if (utf8_msg
== NULL
)
1826 msgwin_msg_add_string(msg_color
, -1, NULL
, utf8_msg
);
1828 if (utf8_msg
!= msg
)
1834 static void search_read_io(GString
*string
, GIOCondition condition
, gpointer data
)
1836 return read_fif_io(string
->str
, condition
, data
, COLOR_BLACK
);
1840 static void search_read_io_stderr(GString
*string
, GIOCondition condition
, gpointer data
)
1842 return read_fif_io(string
->str
, condition
, data
, COLOR_DARK_RED
);
1846 static void search_finished(GPid child_pid
, gint status
, gpointer user_data
)
1848 const gchar
*msg
= _("Search failed.");
1850 gint exit_status
= 1;
1852 if (WIFEXITED(status
))
1854 exit_status
= WEXITSTATUS(status
);
1856 else if (WIFSIGNALED(status
))
1859 g_warning("Find in Files: The command failed unexpectedly (signal received).");
1862 gint exit_status
= status
;
1865 switch (exit_status
)
1869 gint count
= gtk_tree_model_iter_n_children(
1870 GTK_TREE_MODEL(msgwindow
.store_msg
), NULL
) - 1;
1871 gchar
*text
= ngettext(
1872 "Search completed with %d match.",
1873 "Search completed with %d matches.", count
);
1875 msgwin_msg_add(COLOR_BLUE
, -1, NULL
, text
, count
);
1876 ui_set_statusbar(FALSE
, text
, count
);
1880 msg
= _("No matches found.");
1882 msgwin_msg_add_string(COLOR_BLUE
, -1, NULL
, msg
);
1883 ui_set_statusbar(FALSE
, "%s", msg
);
1887 ui_progress_bar_stop();
1891 static GRegex
*compile_regex(const gchar
*str
, GeanyFindFlags sflags
)
1894 GError
*error
= NULL
;
1897 if (sflags
& GEANY_FIND_MULTILINE
)
1898 rflags
|= G_REGEX_MULTILINE
;
1899 if (~sflags
& GEANY_FIND_MATCHCASE
)
1900 rflags
|= G_REGEX_CASELESS
;
1901 if (sflags
& (GEANY_FIND_WHOLEWORD
| GEANY_FIND_WORDSTART
))
1903 geany_debug("%s: Unsupported regex flags found!", G_STRFUNC
);
1906 regex
= g_regex_new(str
, rflags
, 0, &error
);
1909 ui_set_statusbar(FALSE
, _("Bad regex: %s"), error
->message
);
1910 g_error_free(error
);
1916 /* groups that don't exist are handled OK as len = end - start = (-1) - (-1) = 0 */
1917 static gchar
*get_regex_match_string(const gchar
*text
, const GeanyMatchInfo
*match
, guint nth
)
1919 const gint start
= match
->matches
[nth
].start
;
1920 const gint end
= match
->matches
[nth
].end
;
1921 return g_strndup(&text
[start
], end
- start
);
1925 static gint
find_regex(ScintillaObject
*sci
, guint pos
, GRegex
*regex
, gboolean multiline
, GeanyMatchInfo
*match
)
1929 guint document_length
;
1933 document_length
= (guint
)sci_get_length(sci
);
1934 if (document_length
<= 0)
1935 return -1; /* skip empty documents */
1937 g_return_val_if_fail(pos
<= document_length
, -1);
1941 /* Warning: any SCI calls will invalidate 'text' after calling SCI_GETCHARACTERPOINTER */
1942 text
= (void*)scintilla_send_message(sci
, SCI_GETCHARACTERPOINTER
, 0, 0);
1943 g_regex_match_full(regex
, text
, -1, pos
, 0, &minfo
, NULL
);
1945 else /* single-line mode, manually match against each line */
1947 gint line
= sci_get_line_from_position(sci
, pos
);
1951 gint start
= sci_get_position_from_line(sci
, line
);
1952 gint end
= sci_get_line_end_position(sci
, line
);
1954 text
= (void*)scintilla_send_message(sci
, SCI_GETRANGEPOINTER
, start
, end
- start
);
1955 if (g_regex_match_full(regex
, text
, end
- start
, pos
- start
, 0, &minfo
, NULL
))
1960 else /* not found, try next line */
1963 if (line
>= sci_get_line_count(sci
))
1965 pos
= sci_get_position_from_line(sci
, line
);
1966 /* don't free last info, it's freed below */
1967 g_match_info_free(minfo
);
1972 /* Warning: minfo will become invalid when 'text' does! */
1973 if (g_match_info_matches(minfo
))
1977 /* copy whole match text and offsets before they become invalid */
1978 SETPTR(match
->match_text
, g_match_info_fetch(minfo
, 0));
1980 foreach_range(i
, G_N_ELEMENTS(match
->matches
))
1982 gint start
= -1, end
= -1;
1984 g_match_info_fetch_pos(minfo
, (gint
)i
, &start
, &end
);
1985 match
->matches
[i
].start
= offset
+ start
;
1986 match
->matches
[i
].end
= offset
+ end
;
1988 match
->start
= match
->matches
[0].start
;
1989 match
->end
= match
->matches
[0].end
;
1992 g_match_info_free(minfo
);
1997 static gint
geany_find_flags_to_sci_flags(GeanyFindFlags flags
)
1999 g_warn_if_fail(! (flags
& GEANY_FIND_REGEXP
) || ! (flags
& GEANY_FIND_MULTILINE
));
2001 return ((flags
& GEANY_FIND_MATCHCASE
) ? SCFIND_MATCHCASE
: 0) |
2002 ((flags
& GEANY_FIND_WHOLEWORD
) ? SCFIND_WHOLEWORD
: 0) |
2003 ((flags
& GEANY_FIND_REGEXP
) ? SCFIND_REGEXP
| SCFIND_POSIX
: 0) |
2004 ((flags
& GEANY_FIND_WORDSTART
) ? SCFIND_WORDSTART
: 0);
2008 gint
search_find_prev(ScintillaObject
*sci
, const gchar
*str
, GeanyFindFlags flags
, GeanyMatchInfo
**match_
)
2012 g_return_val_if_fail(! (flags
& GEANY_FIND_REGEXP
), -1);
2014 ret
= sci_search_prev(sci
, geany_find_flags_to_sci_flags(flags
), str
);
2015 if (ret
!= -1 && match_
)
2016 *match_
= match_info_new(flags
, ret
, ret
+ strlen(str
));
2021 gint
search_find_next(ScintillaObject
*sci
, const gchar
*str
, GeanyFindFlags flags
, GeanyMatchInfo
**match_
)
2023 GeanyMatchInfo
*match
;
2028 if (~flags
& GEANY_FIND_REGEXP
)
2030 ret
= sci_search_next(sci
, geany_find_flags_to_sci_flags(flags
), str
);
2031 if (ret
!= -1 && match_
)
2032 *match_
= match_info_new(flags
, ret
, ret
+ strlen(str
));
2036 regex
= compile_regex(str
, flags
);
2040 match
= match_info_new(flags
, 0, 0);
2042 pos
= sci_get_current_position(sci
);
2043 ret
= find_regex(sci
, pos
, regex
, flags
& GEANY_FIND_MULTILINE
, match
);
2044 /* avoid re-matching the same position in case of empty matches */
2045 if (ret
== pos
&& match
->matches
[0].start
== match
->matches
[0].end
)
2046 ret
= find_regex(sci
, pos
+ 1, regex
, flags
& GEANY_FIND_MULTILINE
, match
);
2048 sci_set_selection(sci
, match
->start
, match
->end
);
2050 if (ret
!= -1 && match_
)
2053 geany_match_info_free(match
);
2055 g_regex_unref(regex
);
2060 gint
search_replace_match(ScintillaObject
*sci
, const GeanyMatchInfo
*match
, const gchar
*replace_text
)
2066 sci_set_target_start(sci
, match
->start
);
2067 sci_set_target_end(sci
, match
->end
);
2069 if (! (match
->flags
& GEANY_FIND_REGEXP
))
2070 return sci_replace_target(sci
, replace_text
, FALSE
);
2072 str
= g_string_new(replace_text
);
2075 gchar
*ptr
= &str
->str
[i
];
2085 /* backslash or unnecessary escape */
2086 if (c
== '\\' || !isdigit(c
))
2088 g_string_erase(str
, i
, 1);
2093 g_string_erase(str
, i
, 2);
2094 /* fix match offsets by subtracting index of whole match start from the string */
2095 grp
= get_regex_match_string(match
->match_text
- match
->matches
[0].start
, match
, c
- '0');
2096 g_string_insert(str
, i
, grp
);
2100 ret
= sci_replace_target(sci
, str
->str
, FALSE
);
2101 g_string_free(str
, TRUE
);
2106 gint
search_find_text(ScintillaObject
*sci
, GeanyFindFlags flags
, struct Sci_TextToFind
*ttf
, GeanyMatchInfo
**match_
)
2108 GeanyMatchInfo
*match
= NULL
;
2112 if (~flags
& GEANY_FIND_REGEXP
)
2114 ret
= sci_find_text(sci
, geany_find_flags_to_sci_flags(flags
), ttf
);
2115 if (ret
!= -1 && match_
)
2116 *match_
= match_info_new(flags
, ttf
->chrgText
.cpMin
, ttf
->chrgText
.cpMax
);
2120 regex
= compile_regex(ttf
->lpstrText
, flags
);
2124 match
= match_info_new(flags
, 0, 0);
2126 ret
= find_regex(sci
, ttf
->chrg
.cpMin
, regex
, flags
& GEANY_FIND_MULTILINE
, match
);
2127 if (ret
>= ttf
->chrg
.cpMax
)
2131 ttf
->chrgText
.cpMin
= match
->start
;
2132 ttf
->chrgText
.cpMax
= match
->end
;
2135 if (ret
!= -1 && match_
)
2138 geany_match_info_free(match
);
2140 g_regex_unref(regex
);
2145 static gint
find_document_usage(GeanyDocument
*doc
, const gchar
*search_text
, GeanyFindFlags flags
)
2147 gchar
*buffer
, *short_file_name
;
2148 struct Sci_TextToFind ttf
;
2150 gint prev_line
= -1;
2151 GSList
*match
, *matches
;
2153 g_return_val_if_fail(DOC_VALID(doc
), 0);
2155 short_file_name
= g_path_get_basename(DOC_FILENAME(doc
));
2158 ttf
.chrg
.cpMax
= sci_get_length(doc
->editor
->sci
);
2159 ttf
.lpstrText
= (gchar
*)search_text
;
2161 matches
= find_range(doc
->editor
->sci
, flags
, &ttf
);
2162 foreach_slist (match
, matches
)
2164 GeanyMatchInfo
*info
= match
->data
;
2165 gint line
= sci_get_line_from_position(doc
->editor
->sci
, info
->start
);
2167 if (line
!= prev_line
)
2169 buffer
= sci_get_line(doc
->editor
->sci
, line
);
2170 msgwin_msg_add(COLOR_BLACK
, line
+ 1, doc
,
2171 "%s:%d: %s", short_file_name
, line
+ 1, g_strstrip(buffer
));
2177 geany_match_info_free(info
);
2179 g_slist_free(matches
);
2180 g_free(short_file_name
);
2185 void search_find_usage(const gchar
*search_text
, const gchar
*original_search_text
,
2186 GeanyFindFlags flags
, gboolean in_session
)
2191 doc
= document_get_current();
2192 g_return_if_fail(doc
!= NULL
);
2194 if (G_UNLIKELY(EMPTY(search_text
)))
2200 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow
.notebook
), MSG_MESSAGE
);
2201 gtk_list_store_clear(msgwindow
.store_msg
);
2204 { /* use current document */
2205 count
= find_document_usage(doc
, search_text
, flags
);
2210 for (i
= 0; i
< documents_array
->len
; i
++)
2212 if (documents
[i
]->is_valid
)
2214 count
+= find_document_usage(documents
[i
], search_text
, flags
);
2219 if (count
== 0) /* no matches were found */
2221 ui_set_statusbar(FALSE
, _("No matches found for \"%s\"."), original_search_text
);
2222 msgwin_msg_add(COLOR_BLUE
, -1, NULL
, _("No matches found for \"%s\"."), original_search_text
);
2226 ui_set_statusbar(FALSE
, ngettext(
2227 "Found %d match for \"%s\".", "Found %d matches for \"%s\".", count
),
2228 count
, original_search_text
);
2229 msgwin_msg_add(COLOR_BLUE
, -1, NULL
, ngettext(
2230 "Found %d match for \"%s\".", "Found %d matches for \"%s\".", count
),
2231 count
, original_search_text
);
2236 /* ttf is updated to include the last match position (ttf->chrg.cpMin) and
2237 * the new search range end (ttf->chrg.cpMax).
2238 * Note: Normally you would call sci_start/end_undo_action() around this call. */
2239 guint
search_replace_range(ScintillaObject
*sci
, struct Sci_TextToFind
*ttf
,
2240 GeanyFindFlags flags
, const gchar
*replace_text
)
2243 gint offset
= 0; /* difference between search pos and replace pos */
2244 GSList
*match
, *matches
;
2246 g_return_val_if_fail(sci
!= NULL
&& ttf
->lpstrText
!= NULL
&& replace_text
!= NULL
, 0);
2247 if (! *ttf
->lpstrText
)
2250 matches
= find_range(sci
, flags
, ttf
);
2251 foreach_slist (match
, matches
)
2253 GeanyMatchInfo
*info
= match
->data
;
2256 info
->start
+= offset
;
2257 info
->end
+= offset
;
2259 replace_len
= search_replace_match(sci
, info
, replace_text
);
2260 offset
+= replace_len
- (info
->end
- info
->start
);
2263 /* on last match, update the last match/new range end */
2266 ttf
->chrg
.cpMin
= info
->start
;
2267 ttf
->chrg
.cpMax
+= offset
;
2270 geany_match_info_free(info
);
2272 g_slist_free(matches
);
2278 void search_find_again(gboolean change_direction
)
2280 GeanyDocument
*doc
= document_get_current();
2282 g_return_if_fail(doc
!= NULL
);
2284 if (search_data
.text
)
2286 gboolean forward
= ! search_data
.backwards
;
2287 gint result
= document_find_text(doc
, search_data
.text
, search_data
.original_text
, search_data
.flags
,
2288 change_direction
? forward
: !forward
, NULL
, FALSE
, NULL
);
2291 editor_display_current_line(doc
->editor
, 0.3F
);
2293 if (search_data
.search_bar
)
2294 ui_set_search_entry_background(
2295 toolbar_get_widget_child_by_name("SearchEntry"), (result
> -1));