infobars: Use wrap labels to avoid cropped infobars
[geany-mirror.git] / src / search.c
blobae939969dc906a273e6466a01058f691ec9e5845
1 /*
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.
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include "search.h"
33 #include "app.h"
34 #include "document.h"
35 #include "encodings.h"
36 #include "keyfile.h"
37 #include "msgwindow.h"
38 #include "prefs.h"
39 #include "sciwrappers.h"
40 #include "stash.h"
41 #include "support.h"
42 #include "toolbar.h"
43 #include "ui_utils.h"
44 #include "utils.h"
46 #include "gtkcompat.h"
48 #include <unistd.h>
49 #include <string.h>
50 #include <ctype.h>
52 #ifdef G_OS_UNIX
53 # include <sys/types.h>
54 # include <sys/wait.h>
55 #endif
57 #include <gdk/gdkkeysyms.h>
59 enum
61 GEANY_RESPONSE_FIND = 1,
62 GEANY_RESPONSE_FIND_PREVIOUS,
63 GEANY_RESPONSE_FIND_IN_FILE,
64 GEANY_RESPONSE_FIND_IN_SESSION,
65 GEANY_RESPONSE_MARK,
66 GEANY_RESPONSE_REPLACE,
67 GEANY_RESPONSE_REPLACE_AND_FIND,
68 GEANY_RESPONSE_REPLACE_IN_SESSION,
69 GEANY_RESPONSE_REPLACE_IN_FILE,
70 GEANY_RESPONSE_REPLACE_IN_SEL
74 enum
76 FILES_MODE_ALL,
77 FILES_MODE_PROJECT,
78 FILES_MODE_CUSTOM
82 GeanySearchData search_data;
83 GeanySearchPrefs search_prefs;
86 static struct
88 gboolean fif_regexp;
89 gboolean fif_case_sensitive;
90 gboolean fif_match_whole_word;
91 gboolean fif_invert_results;
92 gboolean fif_recursive;
93 gboolean fif_use_extra_options;
94 gchar *fif_extra_options;
95 gint fif_files_mode;
96 gchar *fif_files;
97 gboolean find_regexp;
98 gboolean find_escape_sequences;
99 gboolean find_case_sensitive;
100 gboolean find_match_whole_word;
101 gboolean find_match_word_start;
102 gboolean find_close_dialog;
103 gboolean replace_regexp;
104 gboolean replace_escape_sequences;
105 gboolean replace_case_sensitive;
106 gboolean replace_match_whole_word;
107 gboolean replace_match_word_start;
108 gboolean replace_search_backwards;
109 gboolean replace_close_dialog;
111 settings;
113 static StashGroup *fif_prefs = NULL;
114 static StashGroup *find_prefs = NULL;
115 static StashGroup *replace_prefs = NULL;
118 static struct
120 GtkWidget *dialog;
121 GtkWidget *entry;
122 gboolean all_expanded;
123 gint position[2]; /* x, y */
125 find_dlg = {NULL, NULL, FALSE, {0, 0}};
127 static struct
129 GtkWidget *dialog;
130 GtkWidget *find_entry;
131 GtkWidget *replace_entry;
132 gboolean all_expanded;
133 gint position[2]; /* x, y */
135 replace_dlg = {NULL, NULL, NULL, FALSE, {0, 0}};
137 static struct
139 GtkWidget *dialog;
140 GtkWidget *dir_combo;
141 GtkWidget *files_combo;
142 GtkWidget *search_combo;
143 GtkWidget *encoding_combo;
144 GtkWidget *files_mode_combo;
145 gint position[2]; /* x, y */
147 fif_dlg = {NULL, NULL, NULL, NULL, NULL, NULL, {0, 0}};
150 static gboolean search_read_io(GIOChannel *source, GIOCondition condition, gpointer data);
151 static gboolean search_read_io_stderr(GIOChannel *source, GIOCondition condition, gpointer data);
153 static void search_close_pid(GPid child_pid, gint status, gpointer user_data);
155 static gchar **search_get_argv(const gchar **argv_prefix, const gchar *dir);
157 static GRegex *compile_regex(const gchar *str, gint sflags);
160 static void
161 on_find_replace_checkbutton_toggled(GtkToggleButton *togglebutton, gpointer user_data);
163 static void
164 on_find_dialog_response(GtkDialog *dialog, gint response, gpointer user_data);
166 static void
167 on_find_entry_activate(GtkEntry *entry, gpointer user_data);
169 static void
170 on_find_entry_activate_backward(GtkEntry *entry, gpointer user_data);
172 static void
173 on_replace_dialog_response(GtkDialog *dialog, gint response, gpointer user_data);
175 static void
176 on_replace_find_entry_activate(GtkEntry *entry, gpointer user_data);
178 static void
179 on_replace_entry_activate(GtkEntry *entry, gpointer user_data);
181 static void
182 on_find_in_files_dialog_response(GtkDialog *dialog, gint response, gpointer user_data);
184 static gboolean
185 search_find_in_files(const gchar *utf8_search_text, const gchar *dir, const gchar *opts,
186 const gchar *enc);
189 static void init_prefs(void)
191 StashGroup *group;
193 group = stash_group_new("search");
194 configuration_add_pref_group(group, TRUE);
195 stash_group_add_toggle_button(group, &search_prefs.always_wrap,
196 "pref_search_hide_find_dialog", FALSE, "check_always_wrap_search");
197 stash_group_add_toggle_button(group, &search_prefs.hide_find_dialog,
198 "pref_search_always_wrap", FALSE, "check_hide_find_dialog");
199 stash_group_add_toggle_button(group, &search_prefs.use_current_file_dir,
200 "pref_search_current_file_dir", TRUE, "check_fif_current_dir");
201 stash_group_add_boolean(group, &find_dlg.all_expanded, "find_all_expanded", FALSE);
202 stash_group_add_boolean(group, &replace_dlg.all_expanded, "replace_all_expanded", FALSE);
203 /* dialog positions */
204 stash_group_add_integer(group, &find_dlg.position[0], "position_find_x", -1);
205 stash_group_add_integer(group, &find_dlg.position[1], "position_find_y", -1);
206 stash_group_add_integer(group, &replace_dlg.position[0], "position_replace_x", -1);
207 stash_group_add_integer(group, &replace_dlg.position[1], "position_replace_y", -1);
208 stash_group_add_integer(group, &fif_dlg.position[0], "position_fif_x", -1);
209 stash_group_add_integer(group, &fif_dlg.position[1], "position_fif_y", -1);
211 memset(&settings, '\0', sizeof(settings));
213 group = stash_group_new("search");
214 fif_prefs = group;
215 configuration_add_pref_group(group, FALSE);
216 stash_group_add_toggle_button(group, &settings.fif_regexp,
217 "fif_regexp", FALSE, "check_regexp");
218 stash_group_add_toggle_button(group, &settings.fif_case_sensitive,
219 "fif_case_sensitive", TRUE, "check_case");
220 stash_group_add_toggle_button(group, &settings.fif_match_whole_word,
221 "fif_match_whole_word", FALSE, "check_wholeword");
222 stash_group_add_toggle_button(group, &settings.fif_invert_results,
223 "fif_invert_results", FALSE, "check_invert");
224 stash_group_add_toggle_button(group, &settings.fif_recursive,
225 "fif_recursive", FALSE, "check_recursive");
226 stash_group_add_entry(group, &settings.fif_extra_options,
227 "fif_extra_options", "", "entry_extra");
228 stash_group_add_toggle_button(group, &settings.fif_use_extra_options,
229 "fif_use_extra_options", FALSE, "check_extra");
230 stash_group_add_entry(group, &settings.fif_files,
231 "fif_files", "", "entry_files");
232 stash_group_add_combo_box(group, &settings.fif_files_mode,
233 "fif_files_mode", FILES_MODE_ALL, "combo_files_mode");
235 group = stash_group_new("search");
236 find_prefs = group;
237 configuration_add_pref_group(group, FALSE);
238 stash_group_add_toggle_button(group, &settings.find_regexp,
239 "find_regexp", FALSE, "check_regexp");
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_case_sensitive,
257 "replace_case_sensitive", FALSE, "check_case");
258 stash_group_add_toggle_button(group, &settings.replace_escape_sequences,
259 "replace_escape_sequences", FALSE, "check_escape");
260 stash_group_add_toggle_button(group, &settings.replace_match_whole_word,
261 "replace_match_whole_word", FALSE, "check_word");
262 stash_group_add_toggle_button(group, &settings.replace_match_word_start,
263 "replace_match_word_start", FALSE, "check_wordstart");
264 stash_group_add_toggle_button(group, &settings.replace_search_backwards,
265 "replace_search_backwards", FALSE, "check_back");
266 stash_group_add_toggle_button(group, &settings.replace_close_dialog,
267 "replace_close_dialog", TRUE, "check_close");
271 void search_init(void)
273 search_data.text = NULL;
274 search_data.original_text = NULL;
275 init_prefs();
279 #define FREE_WIDGET(wid) \
280 if (wid && GTK_IS_WIDGET(wid)) gtk_widget_destroy(wid);
282 void search_finalize(void)
284 FREE_WIDGET(find_dlg.dialog);
285 FREE_WIDGET(replace_dlg.dialog);
286 FREE_WIDGET(fif_dlg.dialog);
287 g_free(search_data.text);
288 g_free(search_data.original_text);
292 static void on_widget_toggled_set_insensitive(
293 GtkToggleButton *togglebutton, gpointer user_data)
295 gtk_widget_set_sensitive(GTK_WIDGET(user_data),
296 !gtk_toggle_button_get_active(togglebutton));
300 static GtkWidget *add_find_checkboxes(GtkDialog *dialog)
302 GtkWidget *checkbox1, *checkbox2, *check_regexp, *check_back, *checkbox5,
303 *checkbox7, *hbox, *fbox, *mbox;
305 check_regexp = gtk_check_button_new_with_mnemonic(_("_Use regular expressions"));
306 ui_hookup_widget(dialog, check_regexp, "check_regexp");
307 gtk_button_set_focus_on_click(GTK_BUTTON(check_regexp), FALSE);
308 gtk_widget_set_tooltip_text(check_regexp, _("Use POSIX-like regular expressions. "
309 "For detailed information about using regular expressions, please read the documentation."));
310 g_signal_connect(check_regexp, "toggled",
311 G_CALLBACK(on_find_replace_checkbutton_toggled), dialog);
313 if (dialog != GTK_DIALOG(find_dlg.dialog))
315 check_back = gtk_check_button_new_with_mnemonic(_("Search _backwards"));
316 ui_hookup_widget(dialog, check_back, "check_back");
317 gtk_button_set_focus_on_click(GTK_BUTTON(check_back), FALSE);
319 else
320 { /* align the two checkboxes at the top of the hbox */
321 GtkSizeGroup *label_size;
322 check_back = gtk_label_new(NULL);
323 label_size = gtk_size_group_new(GTK_SIZE_GROUP_VERTICAL);
324 gtk_size_group_add_widget(GTK_SIZE_GROUP(label_size), check_back);
325 gtk_size_group_add_widget(GTK_SIZE_GROUP(label_size), check_regexp);
326 g_object_unref(label_size);
328 checkbox7 = gtk_check_button_new_with_mnemonic(_("Use _escape sequences"));
329 ui_hookup_widget(dialog, checkbox7, "check_escape");
330 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox7), FALSE);
331 gtk_widget_set_tooltip_text(checkbox7,
332 _("Replace \\\\, \\t, \\n, \\r and \\uXXXX (Unicode chararacters) with the "
333 "corresponding control characters"));
335 /* Search features */
336 fbox = gtk_vbox_new(FALSE, 0);
337 gtk_container_add(GTK_CONTAINER(fbox), check_regexp);
338 gtk_container_add(GTK_CONTAINER(fbox), checkbox7);
339 gtk_container_add(GTK_CONTAINER(fbox), check_back);
341 checkbox1 = gtk_check_button_new_with_mnemonic(_("C_ase sensitive"));
342 ui_hookup_widget(dialog, checkbox1, "check_case");
343 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox1), FALSE);
345 checkbox2 = gtk_check_button_new_with_mnemonic(_("Match only a _whole word"));
346 ui_hookup_widget(dialog, checkbox2, "check_word");
347 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox2), FALSE);
349 checkbox5 = gtk_check_button_new_with_mnemonic(_("Match from s_tart of word"));
350 ui_hookup_widget(dialog, checkbox5, "check_wordstart");
351 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox5), FALSE);
353 /* disable wordstart when wholeword is checked */
354 g_signal_connect(checkbox2, "toggled",
355 G_CALLBACK(on_widget_toggled_set_insensitive), checkbox5);
357 /* Matching options */
358 mbox = gtk_vbox_new(FALSE, 0);
359 gtk_container_add(GTK_CONTAINER(mbox), checkbox1);
360 gtk_container_add(GTK_CONTAINER(mbox), checkbox2);
361 gtk_container_add(GTK_CONTAINER(mbox), checkbox5);
363 hbox = gtk_hbox_new(TRUE, 6);
364 gtk_container_add(GTK_CONTAINER(hbox), fbox);
365 gtk_container_add(GTK_CONTAINER(hbox), mbox);
366 return hbox;
370 static void send_find_dialog_response(GtkButton *button, gpointer user_data)
372 gtk_dialog_response(GTK_DIALOG(find_dlg.dialog), GPOINTER_TO_INT(user_data));
376 /* store text, clear search flags so we can use Search->Find Next/Previous */
377 static void setup_find_next(const gchar *text)
379 g_free(search_data.text);
380 g_free(search_data.original_text);
381 search_data.text = g_strdup(text);
382 search_data.original_text = g_strdup(text);
383 search_data.flags = 0;
384 search_data.backwards = FALSE;
385 search_data.search_bar = FALSE;
389 /* Search for next match of the current "selection".
390 * Optionally for X11 based systems, this will try to use the system-wide
391 * x-selection first.
392 * If it doesn't find a suitable search string it will try to use
393 * the current word instead, or just repeat the last search.
394 * Search flags are always zero.
396 void search_find_selection(GeanyDocument *doc, gboolean search_backwards)
398 gchar *s = NULL;
400 g_return_if_fail(DOC_VALID(doc));
402 #ifdef G_OS_UNIX
403 if (search_prefs.find_selection_type == GEANY_FIND_SEL_X)
405 GtkClipboard *clipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
407 s = gtk_clipboard_wait_for_text(clipboard);
408 if (s && (strchr(s,'\n') || strchr(s, '\r')))
410 g_free(s);
411 s = NULL;
414 #endif
416 if (!s && sci_has_selection(doc->editor->sci))
417 s = sci_get_selection_contents(doc->editor->sci);
419 if (!s && search_prefs.find_selection_type != GEANY_FIND_SEL_AGAIN)
421 /* get the current word */
422 s = editor_get_default_selection(doc->editor, TRUE, NULL);
425 if (s)
427 setup_find_next(s); /* allow find next/prev */
429 if (document_find_text(doc, s, NULL, 0, search_backwards, NULL, FALSE, NULL) > -1)
430 editor_display_current_line(doc->editor, 0.3F);
431 g_free(s);
433 else if (search_prefs.find_selection_type == GEANY_FIND_SEL_AGAIN)
435 /* Repeat last search (in case selection was lost) */
436 search_find_again(search_backwards);
438 else
440 utils_beep();
445 static void on_expander_activated(GtkExpander *exp, gpointer data)
447 gboolean *setting = data;
449 *setting = gtk_expander_get_expanded(exp);
453 static void create_find_dialog(void)
455 GtkWidget *label, *entry, *sbox, *vbox;
456 GtkWidget *exp, *bbox, *button, *check_close;
458 find_dlg.dialog = gtk_dialog_new_with_buttons(_("Find"),
459 GTK_WINDOW(main_widgets.window), GTK_DIALOG_DESTROY_WITH_PARENT,
460 GTK_STOCK_CLOSE, GTK_RESPONSE_CANCEL, NULL);
461 vbox = ui_dialog_vbox_new(GTK_DIALOG(find_dlg.dialog));
462 gtk_widget_set_name(find_dlg.dialog, "GeanyDialogSearch");
463 gtk_box_set_spacing(GTK_BOX(vbox), 9);
465 button = ui_button_new_with_image(GTK_STOCK_GO_BACK, _("_Previous"));
466 gtk_dialog_add_action_widget(GTK_DIALOG(find_dlg.dialog), button,
467 GEANY_RESPONSE_FIND_PREVIOUS);
468 ui_hookup_widget(find_dlg.dialog, button, "btn_previous");
470 button = ui_button_new_with_image(GTK_STOCK_GO_FORWARD, _("_Next"));
471 gtk_dialog_add_action_widget(GTK_DIALOG(find_dlg.dialog), button,
472 GEANY_RESPONSE_FIND);
474 label = gtk_label_new_with_mnemonic(_("_Search for:"));
475 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
477 entry = gtk_combo_box_text_new_with_entry();
478 ui_entry_add_clear_icon(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(entry))));
479 gtk_label_set_mnemonic_widget(GTK_LABEL(label), entry);
480 gtk_entry_set_width_chars(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(entry))), 50);
481 find_dlg.entry = gtk_bin_get_child(GTK_BIN(entry));
483 g_signal_connect(gtk_bin_get_child(GTK_BIN(entry)), "activate",
484 G_CALLBACK(on_find_entry_activate), entry);
485 ui_entry_add_activate_backward_signal(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(entry))));
486 g_signal_connect(gtk_bin_get_child(GTK_BIN(entry)), "activate-backward",
487 G_CALLBACK(on_find_entry_activate_backward), entry);
488 g_signal_connect(find_dlg.dialog, "response",
489 G_CALLBACK(on_find_dialog_response), entry);
490 g_signal_connect(find_dlg.dialog, "delete-event",
491 G_CALLBACK(gtk_widget_hide_on_delete), NULL);
493 sbox = gtk_hbox_new(FALSE, 6);
494 gtk_box_pack_start(GTK_BOX(sbox), label, FALSE, FALSE, 0);
495 gtk_box_pack_start(GTK_BOX(sbox), entry, TRUE, TRUE, 0);
496 gtk_box_pack_start(GTK_BOX(vbox), sbox, TRUE, FALSE, 0);
498 gtk_container_add(GTK_CONTAINER(vbox),
499 add_find_checkboxes(GTK_DIALOG(find_dlg.dialog)));
501 /* Now add the multiple match options */
502 exp = gtk_expander_new_with_mnemonic(_("_Find All"));
503 gtk_expander_set_expanded(GTK_EXPANDER(exp), find_dlg.all_expanded);
504 g_signal_connect_after(exp, "activate",
505 G_CALLBACK(on_expander_activated), &find_dlg.all_expanded);
507 bbox = gtk_hbutton_box_new();
509 button = gtk_button_new_with_mnemonic(_("_Mark"));
510 gtk_widget_set_tooltip_text(button,
511 _("Mark all matches in the current document"));
512 gtk_container_add(GTK_CONTAINER(bbox), button);
513 g_signal_connect(button, "clicked", G_CALLBACK(send_find_dialog_response),
514 GINT_TO_POINTER(GEANY_RESPONSE_MARK));
516 button = gtk_button_new_with_mnemonic(_("In Sessi_on"));
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_FIND_IN_SESSION));
521 button = gtk_button_new_with_mnemonic(_("_In Document"));
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_FILE));
526 /* close window checkbox */
527 check_close = gtk_check_button_new_with_mnemonic(_("Close _dialog"));
528 ui_hookup_widget(find_dlg.dialog, check_close, "check_close");
529 gtk_button_set_focus_on_click(GTK_BUTTON(check_close), FALSE);
530 gtk_widget_set_tooltip_text(check_close,
531 _("Disable this option to keep the dialog open"));
532 gtk_container_add(GTK_CONTAINER(bbox), check_close);
533 gtk_button_box_set_child_secondary(GTK_BUTTON_BOX(bbox), check_close, TRUE);
535 ui_hbutton_box_copy_layout(
536 GTK_BUTTON_BOX(gtk_dialog_get_action_area(GTK_DIALOG(find_dlg.dialog))),
537 GTK_BUTTON_BOX(bbox));
538 gtk_container_add(GTK_CONTAINER(exp), bbox);
539 gtk_container_add(GTK_CONTAINER(vbox), exp);
543 static void set_dialog_position(GtkWidget *dialog, gint *position)
545 if (position[0] >= 0)
546 gtk_window_move(GTK_WINDOW(dialog), position[0], position[1]);
550 void search_show_find_dialog(void)
552 GeanyDocument *doc = document_get_current();
553 gchar *sel = NULL;
555 g_return_if_fail(doc != NULL);
557 sel = editor_get_default_selection(doc->editor, search_prefs.use_current_word, NULL);
559 if (find_dlg.dialog == NULL)
561 create_find_dialog();
562 stash_group_display(find_prefs, find_dlg.dialog);
563 if (sel)
564 gtk_entry_set_text(GTK_ENTRY(find_dlg.entry), sel);
566 set_dialog_position(find_dlg.dialog, find_dlg.position);
567 gtk_widget_show_all(find_dlg.dialog);
569 else
571 /* only set selection if the dialog is not already visible */
572 if (! gtk_widget_get_visible(find_dlg.dialog) && sel)
573 gtk_entry_set_text(GTK_ENTRY(find_dlg.entry), sel);
574 gtk_widget_grab_focus(find_dlg.entry);
575 set_dialog_position(find_dlg.dialog, find_dlg.position);
576 gtk_widget_show(find_dlg.dialog);
577 if (sel != NULL) /* when we have a selection, reset the entry widget's background colour */
578 ui_set_search_entry_background(find_dlg.entry, TRUE);
579 /* bring the dialog back in the foreground in case it is already open but the focus is away */
580 gtk_window_present(GTK_WINDOW(find_dlg.dialog));
583 g_free(sel);
587 static void send_replace_dialog_response(GtkButton *button, gpointer user_data)
589 gtk_dialog_response(GTK_DIALOG(replace_dlg.dialog), GPOINTER_TO_INT(user_data));
593 static gboolean
594 on_widget_key_pressed_set_focus(GtkWidget *widget, GdkEventKey *event, gpointer user_data)
596 if (event->keyval == GDK_Tab)
598 gtk_widget_grab_focus(GTK_WIDGET(user_data));
599 return TRUE;
601 return FALSE;
605 static void create_replace_dialog(void)
607 GtkWidget *label_find, *label_replace, *entry_find, *entry_replace,
608 *check_close, *button, *rbox, *fbox, *vbox, *exp, *bbox;
609 GtkSizeGroup *label_size;
611 replace_dlg.dialog = gtk_dialog_new_with_buttons(_("Replace"),
612 GTK_WINDOW(main_widgets.window), GTK_DIALOG_DESTROY_WITH_PARENT,
613 GTK_STOCK_CLOSE, GTK_RESPONSE_CANCEL, NULL);
614 vbox = ui_dialog_vbox_new(GTK_DIALOG(replace_dlg.dialog));
615 gtk_box_set_spacing(GTK_BOX(vbox), 9);
616 gtk_widget_set_name(replace_dlg.dialog, "GeanyDialogSearch");
618 button = gtk_button_new_from_stock(GTK_STOCK_FIND);
619 gtk_dialog_add_action_widget(GTK_DIALOG(replace_dlg.dialog), button,
620 GEANY_RESPONSE_FIND);
621 button = gtk_button_new_with_mnemonic(_("_Replace"));
622 gtk_button_set_image(GTK_BUTTON(button),
623 gtk_image_new_from_stock(GTK_STOCK_FIND_AND_REPLACE, GTK_ICON_SIZE_BUTTON));
624 gtk_dialog_add_action_widget(GTK_DIALOG(replace_dlg.dialog), button,
625 GEANY_RESPONSE_REPLACE);
626 button = gtk_button_new_with_mnemonic(_("Replace & Fi_nd"));
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_AND_FIND);
632 label_find = gtk_label_new_with_mnemonic(_("_Search for:"));
633 gtk_misc_set_alignment(GTK_MISC(label_find), 0, 0.5);
635 label_replace = gtk_label_new_with_mnemonic(_("Replace wit_h:"));
636 gtk_misc_set_alignment(GTK_MISC(label_replace), 0, 0.5);
638 entry_find = gtk_combo_box_text_new_with_entry();
639 ui_entry_add_clear_icon(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(entry_find))));
640 gtk_label_set_mnemonic_widget(GTK_LABEL(label_find), entry_find);
641 gtk_entry_set_width_chars(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(entry_find))), 50);
642 ui_hookup_widget(replace_dlg.dialog, entry_find, "entry_find");
643 replace_dlg.find_entry = gtk_bin_get_child(GTK_BIN(entry_find));
645 entry_replace = gtk_combo_box_text_new_with_entry();
646 ui_entry_add_clear_icon(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(entry_replace))));
647 gtk_label_set_mnemonic_widget(GTK_LABEL(label_replace), entry_replace);
648 gtk_entry_set_width_chars(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(entry_replace))), 50);
649 ui_hookup_widget(replace_dlg.dialog, entry_replace, "entry_replace");
650 replace_dlg.replace_entry = gtk_bin_get_child(GTK_BIN(entry_replace));
652 /* tab from find to the replace entry */
653 g_signal_connect(gtk_bin_get_child(GTK_BIN(entry_find)),
654 "key-press-event", G_CALLBACK(on_widget_key_pressed_set_focus),
655 gtk_bin_get_child(GTK_BIN(entry_replace)));
656 g_signal_connect(gtk_bin_get_child(GTK_BIN(entry_find)), "activate",
657 G_CALLBACK(on_replace_find_entry_activate), NULL);
658 g_signal_connect(gtk_bin_get_child(GTK_BIN(entry_replace)), "activate",
659 G_CALLBACK(on_replace_entry_activate), NULL);
660 g_signal_connect(replace_dlg.dialog, "response",
661 G_CALLBACK(on_replace_dialog_response), NULL);
662 g_signal_connect(replace_dlg.dialog, "delete-event",
663 G_CALLBACK(gtk_widget_hide_on_delete), NULL);
665 fbox = gtk_hbox_new(FALSE, 6);
666 gtk_box_pack_start(GTK_BOX(fbox), label_find, FALSE, FALSE, 0);
667 gtk_box_pack_start(GTK_BOX(fbox), entry_find, TRUE, TRUE, 0);
669 rbox = gtk_hbox_new(FALSE, 6);
670 gtk_box_pack_start(GTK_BOX(rbox), label_replace, FALSE, FALSE, 0);
671 gtk_box_pack_start(GTK_BOX(rbox), entry_replace, TRUE, TRUE, 0);
673 label_size = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
674 gtk_size_group_add_widget(label_size, label_find);
675 gtk_size_group_add_widget(label_size, label_replace);
676 g_object_unref(G_OBJECT(label_size)); /* auto destroy the size group */
678 gtk_box_pack_start(GTK_BOX(vbox), fbox, TRUE, FALSE, 0);
679 gtk_box_pack_start(GTK_BOX(vbox), rbox, TRUE, FALSE, 0);
680 gtk_container_add(GTK_CONTAINER(vbox),
681 add_find_checkboxes(GTK_DIALOG(replace_dlg.dialog)));
683 /* Now add the multiple replace options */
684 exp = gtk_expander_new_with_mnemonic(_("Re_place All"));
685 gtk_expander_set_expanded(GTK_EXPANDER(exp), replace_dlg.all_expanded);
686 g_signal_connect_after(exp, "activate",
687 G_CALLBACK(on_expander_activated), &replace_dlg.all_expanded);
689 bbox = gtk_hbutton_box_new();
691 button = gtk_button_new_with_mnemonic(_("In Sessi_on"));
692 gtk_container_add(GTK_CONTAINER(bbox), button);
693 g_signal_connect(button, "clicked", G_CALLBACK(send_replace_dialog_response),
694 GINT_TO_POINTER(GEANY_RESPONSE_REPLACE_IN_SESSION));
696 button = gtk_button_new_with_mnemonic(_("_In Document"));
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_FILE));
701 button = gtk_button_new_with_mnemonic(_("In Se_lection"));
702 gtk_widget_set_tooltip_text(button,
703 _("Replace all matches found in the currently selected text"));
704 gtk_container_add(GTK_CONTAINER(bbox), button);
705 g_signal_connect(button, "clicked", G_CALLBACK(send_replace_dialog_response),
706 GINT_TO_POINTER(GEANY_RESPONSE_REPLACE_IN_SEL));
708 /* close window checkbox */
709 check_close = gtk_check_button_new_with_mnemonic(_("Close _dialog"));
710 ui_hookup_widget(replace_dlg.dialog, check_close, "check_close");
711 gtk_button_set_focus_on_click(GTK_BUTTON(check_close), FALSE);
712 gtk_widget_set_tooltip_text(check_close,
713 _("Disable this option to keep the dialog open"));
714 gtk_container_add(GTK_CONTAINER(bbox), check_close);
715 gtk_button_box_set_child_secondary(GTK_BUTTON_BOX(bbox), check_close, TRUE);
717 ui_hbutton_box_copy_layout(
718 GTK_BUTTON_BOX(gtk_dialog_get_action_area(GTK_DIALOG(replace_dlg.dialog))),
719 GTK_BUTTON_BOX(bbox));
720 gtk_container_add(GTK_CONTAINER(exp), bbox);
721 gtk_container_add(GTK_CONTAINER(vbox), exp);
725 void search_show_replace_dialog(void)
727 GeanyDocument *doc = document_get_current();
728 gchar *sel = NULL;
730 if (doc == NULL)
731 return;
733 sel = editor_get_default_selection(doc->editor, search_prefs.use_current_word, NULL);
735 if (replace_dlg.dialog == NULL)
737 create_replace_dialog();
738 stash_group_display(replace_prefs, replace_dlg.dialog);
739 if (sel)
740 gtk_entry_set_text(GTK_ENTRY(replace_dlg.find_entry), sel);
742 set_dialog_position(replace_dlg.dialog, replace_dlg.position);
743 gtk_widget_show_all(replace_dlg.dialog);
745 else
747 /* only set selection if the dialog is not already visible */
748 if (! gtk_widget_get_visible(replace_dlg.dialog) && sel)
749 gtk_entry_set_text(GTK_ENTRY(replace_dlg.find_entry), sel);
750 if (sel != NULL) /* when we have a selection, reset the entry widget's background colour */
751 ui_set_search_entry_background(replace_dlg.find_entry, TRUE);
752 gtk_widget_grab_focus(replace_dlg.find_entry);
753 set_dialog_position(replace_dlg.dialog, replace_dlg.position);
754 gtk_widget_show(replace_dlg.dialog);
755 /* bring the dialog back in the foreground in case it is already open but the focus is away */
756 gtk_window_present(GTK_WINDOW(replace_dlg.dialog));
759 g_free(sel);
763 static void on_widget_toggled_set_sensitive(GtkToggleButton *togglebutton, gpointer user_data)
765 /* disable extra option entry when checkbutton not checked */
766 gtk_widget_set_sensitive(GTK_WIDGET(user_data),
767 gtk_toggle_button_get_active(togglebutton));
771 static void update_file_patterns(GtkWidget *mode_combo, GtkWidget *fcombo)
773 gint selection;
774 GtkWidget *entry;
776 entry = gtk_bin_get_child(GTK_BIN(fcombo));
778 selection = gtk_combo_box_get_active(GTK_COMBO_BOX(mode_combo));
780 if (selection == FILES_MODE_ALL)
782 gtk_entry_set_text(GTK_ENTRY(entry), "");
783 gtk_widget_set_sensitive(fcombo, FALSE);
785 else if (selection == FILES_MODE_CUSTOM)
787 gtk_widget_set_sensitive(fcombo, TRUE);
789 else if (selection == FILES_MODE_PROJECT)
791 if (app->project && !EMPTY(app->project->file_patterns))
793 gchar *patterns;
795 patterns = g_strjoinv(" ", app->project->file_patterns);
796 gtk_entry_set_text(GTK_ENTRY(entry), patterns);
797 g_free(patterns);
799 else
801 gtk_entry_set_text(GTK_ENTRY(entry), "");
804 gtk_widget_set_sensitive(fcombo, FALSE);
809 /* creates the combo to choose which files include in the search */
810 static GtkWidget *create_fif_file_mode_combo(void)
812 GtkWidget *combo;
813 GtkCellRenderer *renderer;
814 GtkListStore *store;
815 GtkTreeIter iter;
817 /* text/sensitive */
818 store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_BOOLEAN);
819 gtk_list_store_append(store, &iter);
820 gtk_list_store_set(store, &iter, 0, _("all"), 1, TRUE, -1);
821 gtk_list_store_append(store, &iter);
822 gtk_list_store_set(store, &iter, 0, _("project"), 1, app->project != NULL, -1);
823 gtk_list_store_append(store, &iter);
824 gtk_list_store_set(store, &iter, 0, _("custom"), 1, TRUE, -1);
826 combo = gtk_combo_box_new_with_model(GTK_TREE_MODEL(store));
827 g_object_unref(store);
828 gtk_widget_set_tooltip_text(combo, _("All: search all files in the directory\n"
829 "Project: use file patterns defined in the project settings\n"
830 "Custom: specify file patterns manually"));
832 renderer = gtk_cell_renderer_text_new();
833 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), renderer, TRUE);
834 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo), renderer, "text", 0, "sensitive", 1, NULL);
836 return combo;
840 /* updates the sensitivity of the project combo item */
841 static void update_fif_file_mode_combo(void)
843 GtkTreeModel *model = gtk_combo_box_get_model(GTK_COMBO_BOX(fif_dlg.files_mode_combo));
844 GtkTreeIter iter;
846 /* "1" refers to the second list entry, project */
847 if (gtk_tree_model_get_iter_from_string(model, &iter, "1"))
848 gtk_list_store_set(GTK_LIST_STORE(model), &iter, 1, app->project != NULL, -1);
852 static void create_fif_dialog(void)
854 GtkWidget *dir_combo, *combo, *fcombo, *e_combo, *entry;
855 GtkWidget *label, *label1, *label2, *label3, *checkbox1, *checkbox2, *check_wholeword,
856 *check_recursive, *check_extra, *entry_extra, *check_regexp, *combo_files_mode;
857 GtkWidget *dbox, *sbox, *lbox, *rbox, *hbox, *vbox, *ebox;
858 GtkSizeGroup *size_group;
860 fif_dlg.dialog = gtk_dialog_new_with_buttons(
861 _("Find in Files"), GTK_WINDOW(main_widgets.window), GTK_DIALOG_DESTROY_WITH_PARENT,
862 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, NULL);
863 vbox = ui_dialog_vbox_new(GTK_DIALOG(fif_dlg.dialog));
864 gtk_box_set_spacing(GTK_BOX(vbox), 9);
865 gtk_widget_set_name(fif_dlg.dialog, "GeanyDialogSearch");
867 gtk_dialog_add_button(GTK_DIALOG(fif_dlg.dialog), GTK_STOCK_FIND, GTK_RESPONSE_ACCEPT);
868 gtk_dialog_set_default_response(GTK_DIALOG(fif_dlg.dialog),
869 GTK_RESPONSE_ACCEPT);
871 label = gtk_label_new_with_mnemonic(_("_Search for:"));
872 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
874 combo = gtk_combo_box_text_new_with_entry();
875 entry = gtk_bin_get_child(GTK_BIN(combo));
876 ui_entry_add_clear_icon(GTK_ENTRY(entry));
877 gtk_label_set_mnemonic_widget(GTK_LABEL(label), entry);
878 gtk_entry_set_width_chars(GTK_ENTRY(entry), 50);
879 gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
880 fif_dlg.search_combo = combo;
882 sbox = gtk_hbox_new(FALSE, 6);
883 gtk_box_pack_start(GTK_BOX(sbox), label, FALSE, FALSE, 0);
884 gtk_box_pack_start(GTK_BOX(sbox), combo, TRUE, TRUE, 0);
886 /* make labels same width */
887 size_group = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
888 gtk_size_group_add_widget(size_group, label);
890 label3 = gtk_label_new_with_mnemonic(_("Fi_les:"));
891 gtk_misc_set_alignment(GTK_MISC(label3), 0, 0.5);
893 combo_files_mode = create_fif_file_mode_combo();
894 gtk_label_set_mnemonic_widget(GTK_LABEL(label3), combo_files_mode);
895 ui_hookup_widget(fif_dlg.dialog, combo_files_mode, "combo_files_mode");
896 fif_dlg.files_mode_combo = combo_files_mode;
898 fcombo = gtk_combo_box_text_new_with_entry();
899 entry = gtk_bin_get_child(GTK_BIN(fcombo));
900 ui_entry_add_clear_icon(GTK_ENTRY(entry));
901 gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
902 gtk_widget_set_tooltip_text(entry, _("File patterns, e.g. *.c *.h"));
903 ui_hookup_widget(fif_dlg.dialog, entry, "entry_files");
904 fif_dlg.files_combo = fcombo;
906 /* update the entry when selection is changed */
907 g_signal_connect(combo_files_mode, "changed", G_CALLBACK(update_file_patterns), fcombo);
909 hbox = gtk_hbox_new(FALSE, 6);
910 gtk_box_pack_start(GTK_BOX(hbox), label3, FALSE, FALSE, 0);
911 gtk_box_pack_start(GTK_BOX(hbox), combo_files_mode, FALSE, FALSE, 0);
912 gtk_box_pack_start(GTK_BOX(hbox), fcombo, TRUE, TRUE, 0);
914 label1 = gtk_label_new_with_mnemonic(_("_Directory:"));
915 gtk_misc_set_alignment(GTK_MISC(label1), 0, 0.5);
917 dir_combo = gtk_combo_box_text_new_with_entry();
918 entry = gtk_bin_get_child(GTK_BIN(dir_combo));
919 ui_entry_add_clear_icon(GTK_ENTRY(entry));
920 gtk_label_set_mnemonic_widget(GTK_LABEL(label1), entry);
921 gtk_entry_set_width_chars(GTK_ENTRY(entry), 50);
922 fif_dlg.dir_combo = dir_combo;
924 /* tab from files to the dir entry */
925 g_signal_connect(gtk_bin_get_child(GTK_BIN(fcombo)), "key-press-event",
926 G_CALLBACK(on_widget_key_pressed_set_focus), entry);
928 dbox = ui_path_box_new(NULL, GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
929 GTK_ENTRY(entry));
930 gtk_box_pack_start(GTK_BOX(dbox), label1, FALSE, FALSE, 0);
932 label2 = gtk_label_new_with_mnemonic(_("E_ncoding:"));
933 gtk_misc_set_alignment(GTK_MISC(label2), 0, 0.5);
935 e_combo = ui_create_encodings_combo_box(FALSE, GEANY_ENCODING_UTF_8);
936 gtk_label_set_mnemonic_widget(GTK_LABEL(label2), e_combo);
937 fif_dlg.encoding_combo = e_combo;
939 ebox = gtk_hbox_new(FALSE, 6);
940 gtk_box_pack_start(GTK_BOX(ebox), label2, FALSE, FALSE, 0);
941 gtk_box_pack_start(GTK_BOX(ebox), e_combo, TRUE, TRUE, 0);
943 gtk_size_group_add_widget(size_group, label1);
944 gtk_size_group_add_widget(size_group, label2);
945 gtk_size_group_add_widget(size_group, label3);
946 g_object_unref(G_OBJECT(size_group)); /* auto destroy the size group */
948 gtk_box_pack_start(GTK_BOX(vbox), sbox, TRUE, FALSE, 0);
949 gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, FALSE, 0);
950 gtk_box_pack_start(GTK_BOX(vbox), dbox, TRUE, FALSE, 0);
951 gtk_box_pack_start(GTK_BOX(vbox), ebox, TRUE, FALSE, 0);
953 check_regexp = gtk_check_button_new_with_mnemonic(_("_Use regular expressions"));
954 ui_hookup_widget(fif_dlg.dialog, check_regexp, "check_regexp");
955 gtk_button_set_focus_on_click(GTK_BUTTON(check_regexp), FALSE);
956 gtk_widget_set_tooltip_text(check_regexp, _("See grep's manual page for more information"));
958 check_recursive = gtk_check_button_new_with_mnemonic(_("_Recurse in subfolders"));
959 ui_hookup_widget(fif_dlg.dialog, check_recursive, "check_recursive");
960 gtk_button_set_focus_on_click(GTK_BUTTON(check_recursive), FALSE);
962 checkbox1 = gtk_check_button_new_with_mnemonic(_("C_ase sensitive"));
963 ui_hookup_widget(fif_dlg.dialog, checkbox1, "check_case");
964 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox1), FALSE);
965 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox1), TRUE);
967 check_wholeword = gtk_check_button_new_with_mnemonic(_("Match only a _whole word"));
968 ui_hookup_widget(fif_dlg.dialog, check_wholeword, "check_wholeword");
969 gtk_button_set_focus_on_click(GTK_BUTTON(check_wholeword), FALSE);
971 checkbox2 = gtk_check_button_new_with_mnemonic(_("_Invert search results"));
972 ui_hookup_widget(fif_dlg.dialog, checkbox2, "check_invert");
973 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox2), FALSE);
974 gtk_widget_set_tooltip_text(checkbox2,
975 _("Invert the sense of matching, to select non-matching lines"));
977 lbox = gtk_vbox_new(FALSE, 0);
978 gtk_container_add(GTK_CONTAINER(lbox), check_regexp);
979 gtk_container_add(GTK_CONTAINER(lbox), checkbox2);
980 gtk_container_add(GTK_CONTAINER(lbox), check_recursive);
982 rbox = gtk_vbox_new(FALSE, 0);
983 gtk_container_add(GTK_CONTAINER(rbox), checkbox1);
984 gtk_container_add(GTK_CONTAINER(rbox), check_wholeword);
985 gtk_container_add(GTK_CONTAINER(rbox), gtk_label_new(NULL));
987 hbox = gtk_hbox_new(FALSE, 6);
988 gtk_container_add(GTK_CONTAINER(hbox), lbox);
989 gtk_container_add(GTK_CONTAINER(hbox), rbox);
990 gtk_container_add(GTK_CONTAINER(vbox), hbox);
992 check_extra = gtk_check_button_new_with_mnemonic(_("E_xtra options:"));
993 ui_hookup_widget(fif_dlg.dialog, check_extra, "check_extra");
994 gtk_button_set_focus_on_click(GTK_BUTTON(check_extra), FALSE);
996 entry_extra = gtk_entry_new();
997 ui_entry_add_clear_icon(GTK_ENTRY(entry_extra));
998 gtk_widget_set_sensitive(entry_extra, FALSE);
999 gtk_widget_set_tooltip_text(entry_extra, _("Other options to pass to Grep"));
1000 ui_hookup_widget(fif_dlg.dialog, entry_extra, "entry_extra");
1002 /* enable entry_extra when check_extra is checked */
1003 g_signal_connect(check_extra, "toggled",
1004 G_CALLBACK(on_widget_toggled_set_sensitive), entry_extra);
1006 hbox = gtk_hbox_new(FALSE, 6);
1007 gtk_box_pack_start(GTK_BOX(hbox), check_extra, FALSE, FALSE, 0);
1008 gtk_box_pack_start(GTK_BOX(hbox), entry_extra, TRUE, TRUE, 0);
1009 gtk_container_add(GTK_CONTAINER(vbox), hbox);
1011 g_signal_connect(fif_dlg.dialog, "response",
1012 G_CALLBACK(on_find_in_files_dialog_response), NULL);
1013 g_signal_connect(fif_dlg.dialog, "delete-event",
1014 G_CALLBACK(gtk_widget_hide_on_delete), NULL);
1019 * Shows the Find in Files dialog.
1021 * @param dir The directory to search in (UTF-8 encoding). May be @c NULL
1022 * to determine it the usual way by using the current document's path.
1024 * @since 0.14, plugin API 53
1026 void search_show_find_in_files_dialog(const gchar *dir)
1028 search_show_find_in_files_dialog_full(NULL, dir);
1032 void search_show_find_in_files_dialog_full(const gchar *text, const gchar *dir)
1034 GtkWidget *entry; /* for child GtkEntry of a GtkComboBoxEntry */
1035 GeanyDocument *doc = document_get_current();
1036 gchar *sel = NULL;
1037 gchar *cur_dir = NULL;
1038 GeanyEncodingIndex enc_idx = GEANY_ENCODING_UTF_8;
1040 if (fif_dlg.dialog == NULL)
1042 create_fif_dialog();
1043 gtk_widget_show_all(fif_dlg.dialog);
1044 if (doc && !text)
1045 sel = editor_get_default_selection(doc->editor, search_prefs.use_current_word, NULL);
1047 stash_group_display(fif_prefs, fif_dlg.dialog);
1049 if (!text)
1051 /* only set selection if the dialog is not already visible, or has just been created */
1052 if (doc && ! sel && ! gtk_widget_get_visible(fif_dlg.dialog))
1053 sel = editor_get_default_selection(doc->editor, search_prefs.use_current_word, NULL);
1055 text = sel;
1057 entry = gtk_bin_get_child(GTK_BIN(fif_dlg.search_combo));
1058 if (text)
1059 gtk_entry_set_text(GTK_ENTRY(entry), text);
1060 g_free(sel);
1062 /* add project's base path directory to the dir list, we do this here once
1063 * (in create_fif_dialog() it would fail if a project is opened after dialog creation) */
1064 if (app->project != NULL && !EMPTY(app->project->base_path))
1066 ui_combo_box_prepend_text_once(GTK_COMBO_BOX_TEXT(fif_dlg.dir_combo),
1067 app->project->base_path);
1070 entry = gtk_bin_get_child(GTK_BIN(fif_dlg.dir_combo));
1071 if (!EMPTY(dir))
1072 cur_dir = g_strdup(dir); /* custom directory argument passed */
1073 else
1075 if (search_prefs.use_current_file_dir)
1077 static gchar *last_cur_dir = NULL;
1078 static GeanyDocument *last_doc = NULL;
1080 /* Only set the directory entry once for the current document */
1081 cur_dir = utils_get_current_file_dir_utf8();
1082 if (doc == last_doc && cur_dir && utils_str_equal(cur_dir, last_cur_dir))
1084 /* in case the user now wants the current directory, add it to history */
1085 ui_combo_box_add_to_history(GTK_COMBO_BOX_TEXT(fif_dlg.dir_combo), cur_dir, 0);
1086 SETPTR(cur_dir, NULL);
1088 else
1089 SETPTR(last_cur_dir, g_strdup(cur_dir));
1091 last_doc = doc;
1093 if (!cur_dir && EMPTY(gtk_entry_get_text(GTK_ENTRY(entry))))
1095 /* use default_open_path if no directory could be determined
1096 * (e.g. when no files are open) */
1097 if (!cur_dir)
1098 cur_dir = g_strdup(utils_get_default_dir_utf8());
1099 if (!cur_dir)
1100 cur_dir = g_get_current_dir();
1103 if (cur_dir)
1105 gtk_entry_set_text(GTK_ENTRY(entry), cur_dir);
1106 g_free(cur_dir);
1109 update_fif_file_mode_combo();
1110 update_file_patterns(fif_dlg.files_mode_combo, fif_dlg.files_combo);
1112 /* set the encoding of the current file */
1113 if (doc != NULL)
1114 enc_idx = encodings_get_idx_from_charset(doc->encoding);
1115 ui_encodings_combo_box_set_active_encoding(GTK_COMBO_BOX(fif_dlg.encoding_combo), enc_idx);
1117 /* put the focus to the directory entry if it is empty */
1118 if (utils_str_equal(gtk_entry_get_text(GTK_ENTRY(entry)), ""))
1119 gtk_widget_grab_focus(fif_dlg.dir_combo);
1120 else
1121 gtk_widget_grab_focus(fif_dlg.search_combo);
1123 /* set dialog window position */
1124 set_dialog_position(fif_dlg.dialog, fif_dlg.position);
1126 gtk_widget_show(fif_dlg.dialog);
1127 /* bring the dialog back in the foreground in case it is already open but the focus is away */
1128 gtk_window_present(GTK_WINDOW(fif_dlg.dialog));
1132 static void
1133 on_find_replace_checkbutton_toggled(GtkToggleButton *togglebutton, gpointer user_data)
1135 GtkWidget *dialog = GTK_WIDGET(user_data);
1136 GtkToggleButton *chk_regexp = GTK_TOGGLE_BUTTON(
1137 ui_lookup_widget(dialog, "check_regexp"));
1139 if (togglebutton == chk_regexp)
1141 gboolean regex_set = gtk_toggle_button_get_active(chk_regexp);
1142 GtkWidget *check_word = ui_lookup_widget(dialog, "check_word");
1143 GtkWidget *check_wordstart = ui_lookup_widget(dialog, "check_wordstart");
1144 GtkWidget *check_escape = ui_lookup_widget(dialog, "check_escape");
1145 gboolean replace = (dialog != find_dlg.dialog);
1146 const char *back_button[2] = { "btn_previous" , "check_back" };
1148 /* hide options that don't apply to regex searches */
1149 gtk_widget_set_sensitive(check_escape, ! regex_set);
1150 gtk_widget_set_sensitive(ui_lookup_widget(dialog, back_button[replace]), ! regex_set);
1151 gtk_widget_set_sensitive(check_word, ! regex_set);
1152 gtk_widget_set_sensitive(check_wordstart, ! regex_set);
1157 static GeanyMatchInfo *match_info_new(gint flags, gint start, gint end)
1159 GeanyMatchInfo *info = g_slice_alloc(sizeof *info);
1161 info->flags = flags;
1162 info->start = start;
1163 info->end = end;
1164 info->match_text = NULL;
1166 return info;
1169 void geany_match_info_free(GeanyMatchInfo *info)
1171 g_free(info->match_text);
1172 g_slice_free1(sizeof *info, info);
1176 /* find all in the given range.
1177 * Returns a list of allocated GeanyMatchInfo, should be freed using:
1179 * foreach_slist(node, matches)
1180 * geany_match_info_free(node->data);
1181 * g_slist_free(matches); */
1182 static GSList *find_range(ScintillaObject *sci, gint flags, struct Sci_TextToFind *ttf)
1184 GSList *matches = NULL;
1185 GeanyMatchInfo *info;
1187 g_return_val_if_fail(sci != NULL && ttf->lpstrText != NULL, NULL);
1188 if (! *ttf->lpstrText)
1189 return NULL;
1191 while (search_find_text(sci, flags, ttf, &info) != -1)
1193 if (ttf->chrgText.cpMax > ttf->chrg.cpMax)
1195 /* found text is partially out of range */
1196 geany_match_info_free(info);
1197 break;
1200 matches = g_slist_prepend(matches, info);
1201 ttf->chrg.cpMin = ttf->chrgText.cpMax;
1203 /* avoid rematching with empty matches like "(?=[a-z])" or "^$".
1204 * note we cannot assume a match will always be empty or not and then break out, since
1205 * matches like "a?(?=b)" will sometimes be empty and sometimes not */
1206 if (ttf->chrgText.cpMax == ttf->chrgText.cpMin)
1207 ttf->chrg.cpMin ++;
1210 return g_slist_reverse(matches);
1214 /* Clears markers if text is null/empty.
1215 * @return Number of matches marked. */
1216 gint search_mark_all(GeanyDocument *doc, const gchar *search_text, gint flags)
1218 gint count = 0;
1219 struct Sci_TextToFind ttf;
1220 GSList *match, *matches;
1222 g_return_val_if_fail(DOC_VALID(doc), 0);
1224 /* clear previous search indicators */
1225 editor_indicator_clear(doc->editor, GEANY_INDICATOR_SEARCH);
1227 if (G_UNLIKELY(EMPTY(search_text)))
1228 return 0;
1230 ttf.chrg.cpMin = 0;
1231 ttf.chrg.cpMax = sci_get_length(doc->editor->sci);
1232 ttf.lpstrText = (gchar *)search_text;
1234 matches = find_range(doc->editor->sci, flags, &ttf);
1235 foreach_slist (match, matches)
1237 GeanyMatchInfo *info = match->data;
1239 if (info->end != info->start)
1240 editor_indicator_set_on_range(doc->editor, GEANY_INDICATOR_SEARCH, info->start, info->end);
1241 count++;
1243 geany_match_info_free(info);
1245 g_slist_free(matches);
1247 return count;
1251 static void
1252 on_find_entry_activate(GtkEntry *entry, gpointer user_data)
1254 on_find_dialog_response(NULL, GEANY_RESPONSE_FIND, user_data);
1258 static void
1259 on_find_entry_activate_backward(GtkEntry *entry, gpointer user_data)
1261 /* can't search backwards with a regexp */
1262 if (search_data.flags & SCFIND_REGEXP)
1263 utils_beep();
1264 else
1265 on_find_dialog_response(NULL, GEANY_RESPONSE_FIND_PREVIOUS, user_data);
1269 static gboolean int_search_flags(gint match_case, gint whole_word, gint regexp, gint word_start)
1271 return (match_case ? SCFIND_MATCHCASE : 0) |
1272 (regexp ? SCFIND_REGEXP | SCFIND_POSIX : 0) |
1273 (whole_word ? SCFIND_WHOLEWORD : 0) |
1274 /* SCFIND_WORDSTART overrides SCFIND_WHOLEWORD, but we want the opposite */
1275 (word_start && !whole_word ? SCFIND_WORDSTART : 0);
1279 static void
1280 on_find_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
1282 gtk_window_get_position(GTK_WINDOW(find_dlg.dialog),
1283 &find_dlg.position[0], &find_dlg.position[1]);
1285 stash_group_update(find_prefs, find_dlg.dialog);
1287 if (response == GTK_RESPONSE_CANCEL || response == GTK_RESPONSE_DELETE_EVENT)
1288 gtk_widget_hide(find_dlg.dialog);
1289 else
1291 GeanyDocument *doc = document_get_current();
1292 gboolean check_close = settings.find_close_dialog;
1294 if (doc == NULL)
1295 return;
1297 search_data.backwards = FALSE;
1298 search_data.search_bar = FALSE;
1300 g_free(search_data.text);
1301 g_free(search_data.original_text);
1302 search_data.text = g_strdup(gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(user_data)))));
1303 search_data.original_text = g_strdup(search_data.text);
1304 search_data.flags = int_search_flags(settings.find_case_sensitive,
1305 settings.find_match_whole_word, settings.find_regexp, settings.find_match_word_start);
1307 if (EMPTY(search_data.text))
1309 fail:
1310 utils_beep();
1311 gtk_widget_grab_focus(find_dlg.entry);
1312 return;
1314 if (search_data.flags & SCFIND_REGEXP)
1316 GRegex *regex = compile_regex(search_data.text, search_data.flags);
1317 if (!regex)
1318 goto fail;
1319 else
1320 g_regex_unref(regex);
1322 else if (settings.find_escape_sequences)
1324 if (! utils_str_replace_escape(search_data.text, FALSE))
1325 goto fail;
1327 ui_combo_box_add_to_history(GTK_COMBO_BOX_TEXT(user_data), search_data.original_text, 0);
1329 switch (response)
1331 case GEANY_RESPONSE_FIND:
1332 case GEANY_RESPONSE_FIND_PREVIOUS:
1334 gint result = document_find_text(doc, search_data.text, search_data.original_text, search_data.flags,
1335 (response == GEANY_RESPONSE_FIND_PREVIOUS), NULL, TRUE, GTK_WIDGET(find_dlg.dialog));
1336 ui_set_search_entry_background(find_dlg.entry, (result > -1));
1337 check_close = search_prefs.hide_find_dialog;
1338 break;
1340 case GEANY_RESPONSE_FIND_IN_FILE:
1341 search_find_usage(search_data.text, search_data.original_text, search_data.flags, FALSE);
1342 break;
1344 case GEANY_RESPONSE_FIND_IN_SESSION:
1345 search_find_usage(search_data.text, search_data.original_text, search_data.flags, TRUE);
1346 break;
1348 case GEANY_RESPONSE_MARK:
1350 gint count = search_mark_all(doc, search_data.text, search_data.flags);
1352 if (count == 0)
1353 ui_set_statusbar(FALSE, _("No matches found for \"%s\"."), search_data.original_text);
1354 else
1355 ui_set_statusbar(FALSE,
1356 ngettext("Found %d match for \"%s\".",
1357 "Found %d matches for \"%s\".", count),
1358 count, search_data.original_text);
1360 break;
1362 if (check_close)
1363 gtk_widget_hide(find_dlg.dialog);
1368 static void
1369 on_replace_find_entry_activate(GtkEntry *entry, gpointer user_data)
1371 on_replace_dialog_response(NULL, GEANY_RESPONSE_FIND, NULL);
1375 static void
1376 on_replace_entry_activate(GtkEntry *entry, gpointer user_data)
1378 on_replace_dialog_response(NULL, GEANY_RESPONSE_REPLACE, NULL);
1382 static void replace_in_session(GeanyDocument *doc,
1383 gint search_flags_re, gboolean search_replace_escape_re,
1384 const gchar *find, const gchar *replace,
1385 const gchar *original_find, const gchar *original_replace)
1387 guint n, page_count, rep_count = 0, file_count = 0;
1389 /* replace in all documents following notebook tab order */
1390 page_count = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
1391 for (n = 0; n < page_count; n++)
1393 GeanyDocument *tmp_doc = document_get_from_page(n);
1394 gint reps = 0;
1396 reps = document_replace_all(tmp_doc, find, replace, original_find, original_replace, search_flags_re);
1397 rep_count += reps;
1398 if (reps)
1399 file_count++;
1401 if (file_count == 0)
1403 utils_beep();
1404 ui_set_statusbar(FALSE, _("No matches found for \"%s\"."), original_find);
1405 return;
1407 /* if only one file was changed, don't override that document's status message
1408 * so we don't have to translate 4 messages for ngettext */
1409 if (file_count > 1)
1410 ui_set_statusbar(FALSE, _("Replaced %u matches in %u documents."),
1411 rep_count, file_count);
1413 /* show which docs had replacements: */
1414 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_STATUS);
1416 ui_save_buttons_toggle(doc->changed); /* update save all */
1420 static void
1421 on_replace_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
1423 GeanyDocument *doc = document_get_current();
1424 gint search_flags_re;
1425 gboolean search_backwards_re, search_replace_escape_re;
1426 gchar *find, *replace, *original_find = NULL, *original_replace = NULL;
1428 gtk_window_get_position(GTK_WINDOW(replace_dlg.dialog),
1429 &replace_dlg.position[0], &replace_dlg.position[1]);
1431 stash_group_update(replace_prefs, replace_dlg.dialog);
1433 if (response == GTK_RESPONSE_CANCEL || response == GTK_RESPONSE_DELETE_EVENT)
1435 gtk_widget_hide(replace_dlg.dialog);
1436 return;
1439 search_backwards_re = settings.replace_search_backwards;
1440 search_replace_escape_re = settings.replace_escape_sequences;
1441 find = g_strdup(gtk_entry_get_text(GTK_ENTRY(replace_dlg.find_entry)));
1442 replace = g_strdup(gtk_entry_get_text(GTK_ENTRY(replace_dlg.replace_entry)));
1444 search_flags_re = int_search_flags(settings.replace_case_sensitive,
1445 settings.replace_match_whole_word, settings.replace_regexp,
1446 settings.replace_match_word_start);
1448 if ((response != GEANY_RESPONSE_FIND) && (search_flags_re & SCFIND_MATCHCASE)
1449 && (strcmp(find, replace) == 0))
1450 goto fail;
1452 original_find = g_strdup(find);
1453 original_replace = g_strdup(replace);
1455 if (search_flags_re & SCFIND_REGEXP)
1457 GRegex *regex = compile_regex(find, search_flags_re);
1458 if (regex)
1459 g_regex_unref(regex);
1460 /* find escapes will be handled by GRegex */
1461 if (!regex || !utils_str_replace_escape(replace, TRUE))
1462 goto fail;
1464 else if (search_replace_escape_re)
1466 if (! utils_str_replace_escape(find, FALSE) ||
1467 ! utils_str_replace_escape(replace, FALSE))
1468 goto fail;
1471 ui_combo_box_add_to_history(GTK_COMBO_BOX_TEXT(
1472 gtk_widget_get_parent(replace_dlg.find_entry)), original_find, 0);
1473 ui_combo_box_add_to_history(GTK_COMBO_BOX_TEXT(
1474 gtk_widget_get_parent(replace_dlg.replace_entry)), original_replace, 0);
1476 switch (response)
1478 case GEANY_RESPONSE_REPLACE_AND_FIND:
1480 gint rep = document_replace_text(doc, find, original_find, replace, search_flags_re,
1481 search_backwards_re);
1482 if (rep != -1)
1483 document_find_text(doc, find, original_find, search_flags_re, search_backwards_re,
1484 NULL, TRUE, NULL);
1485 break;
1487 case GEANY_RESPONSE_REPLACE:
1488 document_replace_text(doc, find, original_find, replace, search_flags_re,
1489 search_backwards_re);
1490 break;
1492 case GEANY_RESPONSE_FIND:
1494 gint result = document_find_text(doc, find, original_find, search_flags_re,
1495 search_backwards_re, NULL, TRUE, GTK_WIDGET(dialog));
1496 ui_set_search_entry_background(replace_dlg.find_entry, (result > -1));
1497 break;
1499 case GEANY_RESPONSE_REPLACE_IN_FILE:
1500 if (! document_replace_all(doc, find, replace, original_find, original_replace, search_flags_re))
1501 utils_beep();
1502 break;
1504 case GEANY_RESPONSE_REPLACE_IN_SESSION:
1505 replace_in_session(doc, search_flags_re, search_replace_escape_re, find, replace, original_find, original_replace);
1506 break;
1508 case GEANY_RESPONSE_REPLACE_IN_SEL:
1509 document_replace_sel(doc, find, replace, original_find, original_replace, search_flags_re);
1510 break;
1512 switch (response)
1514 case GEANY_RESPONSE_REPLACE_IN_SEL:
1515 case GEANY_RESPONSE_REPLACE_IN_FILE:
1516 case GEANY_RESPONSE_REPLACE_IN_SESSION:
1517 if (settings.replace_close_dialog)
1518 gtk_widget_hide(replace_dlg.dialog);
1520 g_free(find);
1521 g_free(replace);
1522 g_free(original_find);
1523 g_free(original_replace);
1524 return;
1526 fail:
1527 utils_beep();
1528 gtk_widget_grab_focus(replace_dlg.find_entry);
1529 g_free(find);
1530 g_free(replace);
1531 g_free(original_find);
1532 g_free(original_replace);
1536 static GString *get_grep_options(void)
1538 GString *gstr = g_string_new("-nHI"); /* line numbers, filenames, ignore binaries */
1540 if (settings.fif_invert_results)
1541 g_string_append_c(gstr, 'v');
1542 if (! settings.fif_case_sensitive)
1543 g_string_append_c(gstr, 'i');
1544 if (settings.fif_match_whole_word)
1545 g_string_append_c(gstr, 'w');
1546 if (settings.fif_recursive)
1547 g_string_append_c(gstr, 'r');
1549 if (!settings.fif_regexp)
1550 g_string_append_c(gstr, 'F');
1551 else
1552 g_string_append_c(gstr, 'E');
1554 if (settings.fif_use_extra_options)
1556 g_strstrip(settings.fif_extra_options);
1558 if (*settings.fif_extra_options != 0)
1560 g_string_append_c(gstr, ' ');
1561 g_string_append(gstr, settings.fif_extra_options);
1564 g_strstrip(settings.fif_files);
1565 if (settings.fif_files_mode != FILES_MODE_ALL && *settings.fif_files)
1567 GString *tmp;
1569 /* put --include= before each pattern */
1570 tmp = g_string_new(settings.fif_files);
1571 do {} while (utils_string_replace_all(tmp, " ", " "));
1572 g_string_prepend_c(tmp, ' ');
1573 utils_string_replace_all(tmp, " ", " --include=");
1574 g_string_append(gstr, tmp->str);
1575 g_string_free(tmp, TRUE);
1577 return gstr;
1581 static void
1582 on_find_in_files_dialog_response(GtkDialog *dialog, gint response,
1583 G_GNUC_UNUSED gpointer user_data)
1585 gtk_window_get_position(GTK_WINDOW(fif_dlg.dialog), &fif_dlg.position[0], &fif_dlg.position[1]);
1587 stash_group_update(fif_prefs, fif_dlg.dialog);
1589 if (response == GTK_RESPONSE_ACCEPT)
1591 GtkWidget *search_combo = fif_dlg.search_combo;
1592 const gchar *search_text =
1593 gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(search_combo))));
1594 GtkWidget *dir_combo = fif_dlg.dir_combo;
1595 const gchar *utf8_dir =
1596 gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(dir_combo))));
1597 GeanyEncodingIndex enc_idx =
1598 ui_encodings_combo_box_get_active_encoding(GTK_COMBO_BOX(fif_dlg.encoding_combo));
1600 if (G_UNLIKELY(EMPTY(utf8_dir)))
1601 ui_set_statusbar(FALSE, _("Invalid directory for find in files."));
1602 else if (!EMPTY(search_text))
1604 gchar *locale_dir;
1605 GString *opts = get_grep_options();
1606 const gchar *enc = (enc_idx == GEANY_ENCODING_UTF_8) ? NULL :
1607 encodings_get_charset_from_index(enc_idx);
1609 locale_dir = utils_get_locale_from_utf8(utf8_dir);
1611 if (search_find_in_files(search_text, locale_dir, opts->str, enc))
1613 ui_combo_box_add_to_history(GTK_COMBO_BOX_TEXT(search_combo), search_text, 0);
1614 ui_combo_box_add_to_history(GTK_COMBO_BOX_TEXT(fif_dlg.files_combo), NULL, 0);
1615 ui_combo_box_add_to_history(GTK_COMBO_BOX_TEXT(dir_combo), utf8_dir, 0);
1616 gtk_widget_hide(fif_dlg.dialog);
1618 g_free(locale_dir);
1619 g_string_free(opts, TRUE);
1621 else
1622 ui_set_statusbar(FALSE, _("No text to find."));
1624 else
1625 gtk_widget_hide(fif_dlg.dialog);
1629 static gboolean
1630 search_find_in_files(const gchar *utf8_search_text, const gchar *dir, const gchar *opts,
1631 const gchar *enc)
1633 gchar **argv_prefix, **argv, **opts_argv;
1634 gchar *command_grep;
1635 gchar *search_text = NULL;
1636 gint opts_argv_len, i;
1637 GPid child_pid;
1638 gint stdout_fd;
1639 gint stderr_fd;
1640 GError *error = NULL;
1641 gboolean ret = FALSE;
1642 gssize utf8_text_len;
1644 if (EMPTY(utf8_search_text) || ! dir) return TRUE;
1646 command_grep = g_find_program_in_path(tool_prefs.grep_cmd);
1647 if (command_grep == NULL)
1649 ui_set_statusbar(TRUE, _("Cannot execute grep tool '%s';"
1650 " check the path setting in Preferences."), tool_prefs.grep_cmd);
1651 return FALSE;
1654 if (! g_shell_parse_argv(opts, &opts_argv_len, &opts_argv, &error))
1656 ui_set_statusbar(TRUE, _("Cannot parse extra options: %s"), error->message);
1657 g_error_free(error);
1658 g_free(command_grep);
1659 return FALSE;
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 /* set grep command and options */
1673 argv_prefix = g_new0(gchar*, 1 + opts_argv_len + 3 + 1); /* last +1 for recursive arg */
1675 argv_prefix[0] = command_grep;
1676 for (i = 0; i < opts_argv_len; i++)
1678 argv_prefix[i + 1] = g_strdup(opts_argv[i]);
1680 g_strfreev(opts_argv);
1682 i++; /* correct for tool_prefs.grep_cmd */
1683 argv_prefix[i++] = g_strdup("--");
1684 argv_prefix[i++] = search_text;
1686 /* finally add the arguments(files to be searched) */
1687 if (strstr(argv_prefix[1], "r")) /* recursive option set */
1689 /* Use '.' so we get relative paths in the output */
1690 argv_prefix[i++] = g_strdup(".");
1691 argv_prefix[i++] = NULL;
1692 argv = argv_prefix;
1694 else
1696 argv_prefix[i++] = NULL;
1697 argv = search_get_argv((const gchar**)argv_prefix, dir);
1698 g_strfreev(argv_prefix);
1701 if (argv == NULL) /* no files */
1703 return FALSE;
1706 gtk_list_store_clear(msgwindow.store_msg);
1707 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_MESSAGE);
1709 if (! g_spawn_async_with_pipes(dir, (gchar**)argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD,
1710 NULL, NULL, &child_pid,
1711 NULL, &stdout_fd, &stderr_fd, &error))
1713 geany_debug("%s: g_spawn_async_with_pipes() failed: %s", G_STRFUNC, error->message);
1714 ui_set_statusbar(TRUE, _("Process failed (%s)"), error->message);
1715 g_error_free(error);
1716 ret = FALSE;
1718 else
1720 gchar *str, *utf8_str;
1722 ui_progress_bar_start(_("Searching..."));
1724 msgwin_set_messages_dir(dir);
1725 /* we can pass 'enc' without strdup'ing it here because it's a global const string and
1726 * always exits longer than the lifetime of this function */
1727 utils_set_up_io_channel(stdout_fd, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
1728 TRUE, search_read_io, (gpointer) enc);
1729 utils_set_up_io_channel(stderr_fd, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
1730 TRUE, search_read_io_stderr, (gpointer) enc);
1731 g_child_watch_add(child_pid, search_close_pid, NULL);
1733 str = g_strdup_printf(_("%s %s -- %s (in directory: %s)"),
1734 tool_prefs.grep_cmd, opts, utf8_search_text, dir);
1735 utf8_str = utils_get_utf8_from_locale(str);
1736 msgwin_msg_add_string(COLOR_BLUE, -1, NULL, utf8_str);
1737 utils_free_pointers(2, str, utf8_str, NULL);
1738 ret = TRUE;
1740 g_strfreev(argv);
1741 return ret;
1745 static gboolean pattern_list_match(GSList *patterns, const gchar *str)
1747 GSList *item;
1749 foreach_slist(item, patterns)
1751 if (g_pattern_match_string(item->data, str))
1752 return TRUE;
1754 return FALSE;
1758 /* Creates an argument vector of strings, copying argv_prefix[] values for
1759 * the first arguments, then followed by filenames found in dir.
1760 * Returns NULL if no files were found, otherwise returned vector should be fully freed. */
1761 static gchar **search_get_argv(const gchar **argv_prefix, const gchar *dir)
1763 guint prefix_len, list_len, i, j;
1764 gchar **argv;
1765 GSList *list, *item, *patterns = NULL;
1766 GError *error = NULL;
1768 g_return_val_if_fail(dir != NULL, NULL);
1770 prefix_len = g_strv_length((gchar**)argv_prefix);
1771 list = utils_get_file_list(dir, &list_len, &error);
1772 if (error)
1774 ui_set_statusbar(TRUE, _("Could not open directory (%s)"), error->message);
1775 g_error_free(error);
1776 return NULL;
1778 if (list == NULL)
1779 return NULL;
1781 argv = g_new(gchar*, prefix_len + list_len + 1);
1783 for (i = 0, j = 0; i < prefix_len; i++)
1785 if (g_str_has_prefix(argv_prefix[i], "--include="))
1787 const gchar *pat = &(argv_prefix[i][10]); /* the pattern part of the argument */
1789 patterns = g_slist_prepend(patterns, g_pattern_spec_new(pat));
1791 else
1792 argv[j++] = g_strdup(argv_prefix[i]);
1795 if (patterns)
1797 GSList *pat;
1799 foreach_slist(item, list)
1801 if (pattern_list_match(patterns, item->data))
1802 argv[j++] = item->data;
1803 else
1804 g_free(item->data);
1806 foreach_slist(pat, patterns)
1807 g_pattern_spec_free(pat->data);
1808 g_slist_free(patterns);
1810 else
1812 foreach_slist(item, list)
1813 argv[j++] = item->data;
1816 argv[j] = NULL;
1817 g_slist_free(list);
1818 return argv;
1822 static gboolean read_fif_io(GIOChannel *source, GIOCondition condition, gchar *enc, gint msg_color)
1824 if (condition & (G_IO_IN | G_IO_PRI))
1826 gchar *msg, *utf8_msg;
1828 while (g_io_channel_read_line(source, &msg, NULL, NULL, NULL) && msg)
1830 utf8_msg = NULL;
1832 g_strstrip(msg);
1833 /* enc is NULL when encoding is set to UTF-8, so we can skip any conversion */
1834 if (enc != NULL)
1836 if (! g_utf8_validate(msg, -1, NULL))
1838 utf8_msg = g_convert(msg, -1, "UTF-8", enc, NULL, NULL, NULL);
1840 if (utf8_msg == NULL)
1841 utf8_msg = msg;
1843 else
1844 utf8_msg = msg;
1846 msgwin_msg_add_string(msg_color, -1, NULL, utf8_msg);
1848 if (utf8_msg != msg)
1849 g_free(utf8_msg);
1850 g_free(msg);
1853 if (condition & (G_IO_ERR | G_IO_HUP | G_IO_NVAL))
1854 return FALSE;
1856 return TRUE;
1860 static gboolean search_read_io(GIOChannel *source, GIOCondition condition, gpointer data)
1862 return read_fif_io(source, condition, data, COLOR_BLACK);
1866 static gboolean search_read_io_stderr(GIOChannel *source, GIOCondition condition, gpointer data)
1868 return read_fif_io(source, condition, data, COLOR_DARK_RED);
1872 static void search_close_pid(GPid child_pid, gint status, gpointer user_data)
1874 const gchar *msg = _("Search failed.");
1875 #ifdef G_OS_UNIX
1876 gint exit_status = 1;
1878 if (WIFEXITED(status))
1880 exit_status = WEXITSTATUS(status);
1882 else if (WIFSIGNALED(status))
1884 exit_status = -1;
1885 g_warning("Find in Files: The command failed unexpectedly (signal received).");
1887 #else
1888 gint exit_status = status;
1889 #endif
1891 switch (exit_status)
1893 case 0:
1895 gint count = gtk_tree_model_iter_n_children(
1896 GTK_TREE_MODEL(msgwindow.store_msg), NULL) - 1;
1897 gchar *text = ngettext(
1898 "Search completed with %d match.",
1899 "Search completed with %d matches.", count);
1901 msgwin_msg_add(COLOR_BLUE, -1, NULL, text, count);
1902 ui_set_statusbar(FALSE, text, count);
1903 break;
1905 case 1:
1906 msg = _("No matches found.");
1907 default:
1908 msgwin_msg_add_string(COLOR_BLUE, -1, NULL, msg);
1909 ui_set_statusbar(FALSE, "%s", msg);
1910 break;
1912 utils_beep();
1913 g_spawn_close_pid(child_pid);
1914 ui_progress_bar_stop();
1918 static GRegex *compile_regex(const gchar *str, gint sflags)
1920 GRegex *regex;
1921 GError *error = NULL;
1922 gint rflags = G_REGEX_MULTILINE;
1924 if (~sflags & SCFIND_MATCHCASE)
1925 rflags |= G_REGEX_CASELESS;
1926 if (sflags & (SCFIND_WHOLEWORD | SCFIND_WORDSTART))
1928 geany_debug("%s: Unsupported regex flags found!", G_STRFUNC);
1931 regex = g_regex_new(str, rflags, 0, &error);
1932 if (!regex)
1934 ui_set_statusbar(FALSE, _("Bad regex: %s"), error->message);
1935 g_error_free(error);
1937 return regex;
1941 /* groups that don't exist are handled OK as len = end - start = (-1) - (-1) = 0 */
1942 static gchar *get_regex_match_string(const gchar *text, const GeanyMatchInfo *match, guint nth)
1944 const gint start = match->matches[nth].start;
1945 const gint end = match->matches[nth].end;
1946 return g_strndup(&text[start], end - start);
1950 static gint find_regex(ScintillaObject *sci, guint pos, GRegex *regex, GeanyMatchInfo *match)
1952 const gchar *text;
1953 GMatchInfo *minfo;
1954 gint ret = -1;
1956 g_return_val_if_fail(pos <= (guint)sci_get_length(sci), -1);
1958 /* Warning: any SCI calls will invalidate 'text' after calling SCI_GETCHARACTERPOINTER */
1959 text = (void*)scintilla_send_message(sci, SCI_GETCHARACTERPOINTER, 0, 0);
1961 /* Warning: minfo will become invalid when 'text' does! */
1962 if (g_regex_match_full(regex, text, -1, pos, 0, &minfo, NULL))
1964 guint i;
1966 /* copy whole match text and offsets before they become invalid */
1967 SETPTR(match->match_text, g_match_info_fetch(minfo, 0));
1969 foreach_range(i, G_N_ELEMENTS(match->matches))
1971 gint start = -1, end = -1;
1973 g_match_info_fetch_pos(minfo, (gint)i, &start, &end);
1974 match->matches[i].start = start;
1975 match->matches[i].end = end;
1977 match->start = match->matches[0].start;
1978 match->end = match->matches[0].end;
1979 ret = match->start;
1981 g_match_info_free(minfo);
1982 return ret;
1986 gint search_find_prev(ScintillaObject *sci, const gchar *str, gint flags, GeanyMatchInfo **match_)
1988 gint ret;
1990 g_return_val_if_fail(! (flags & SCFIND_REGEXP), -1);
1992 ret = sci_search_prev(sci, flags, str);
1993 if (ret != -1 && match_)
1994 *match_ = match_info_new(flags, ret, ret + strlen(str));
1995 return ret;
1999 gint search_find_next(ScintillaObject *sci, const gchar *str, gint flags, GeanyMatchInfo **match_)
2001 GeanyMatchInfo *match;
2002 GRegex *regex;
2003 gint ret = -1;
2004 gint pos;
2006 if (~flags & SCFIND_REGEXP)
2008 ret = sci_search_next(sci, flags, str);
2009 if (ret != -1 && match_)
2010 *match_ = match_info_new(flags, ret, ret + strlen(str));
2011 return ret;
2014 regex = compile_regex(str, flags);
2015 if (!regex)
2016 return -1;
2018 match = match_info_new(flags, 0, 0);
2020 pos = sci_get_current_position(sci);
2021 ret = find_regex(sci, pos, regex, match);
2022 /* avoid re-matching the same position in case of empty matches */
2023 if (ret == pos && match->matches[0].start == match->matches[0].end)
2024 ret = find_regex(sci, pos + 1, regex, match);
2025 if (ret >= 0)
2026 sci_set_selection(sci, match->start, match->end);
2028 if (ret != -1 && match_)
2029 *match_ = match;
2030 else
2031 geany_match_info_free(match);
2033 g_regex_unref(regex);
2034 return ret;
2038 gint search_replace_match(ScintillaObject *sci, const GeanyMatchInfo *match, const gchar *replace_text)
2040 GString *str;
2041 gint ret = 0;
2042 gint i = 0;
2044 sci_set_target_start(sci, match->start);
2045 sci_set_target_end(sci, match->end);
2047 if (! (match->flags & SCFIND_REGEXP))
2048 return sci_replace_target(sci, replace_text, FALSE);
2050 str = g_string_new(replace_text);
2051 while (str->str[i])
2053 gchar *ptr = &str->str[i];
2054 gchar *grp;
2055 gchar c;
2057 if (ptr[0] != '\\')
2059 i++;
2060 continue;
2062 c = ptr[1];
2063 /* backslash or unnecessary escape */
2064 if (c == '\\' || !isdigit(c))
2066 g_string_erase(str, i, 1);
2067 i++;
2068 continue;
2070 /* digit escape */
2071 g_string_erase(str, i, 2);
2072 /* fix match offsets by subtracting index of whole match start from the string */
2073 grp = get_regex_match_string(match->match_text - match->matches[0].start, match, c - '0');
2074 g_string_insert(str, i, grp);
2075 i += strlen(grp);
2076 g_free(grp);
2078 ret = sci_replace_target(sci, str->str, FALSE);
2079 g_string_free(str, TRUE);
2080 return ret;
2084 gint search_find_text(ScintillaObject *sci, gint flags, struct Sci_TextToFind *ttf, GeanyMatchInfo **match_)
2086 GeanyMatchInfo *match = NULL;
2087 GRegex *regex;
2088 gint ret;
2090 if (~flags & SCFIND_REGEXP)
2092 ret = sci_find_text(sci, flags, ttf);
2093 if (ret != -1 && match_)
2094 *match_ = match_info_new(flags, ttf->chrgText.cpMin, ttf->chrgText.cpMax);
2095 return ret;
2098 regex = compile_regex(ttf->lpstrText, flags);
2099 if (!regex)
2100 return -1;
2102 match = match_info_new(flags, 0, 0);
2104 ret = find_regex(sci, ttf->chrg.cpMin, regex, match);
2105 if (ret >= ttf->chrg.cpMax)
2106 ret = -1;
2107 else if (ret >= 0)
2109 ttf->chrgText.cpMin = match->start;
2110 ttf->chrgText.cpMax = match->end;
2113 if (ret != -1 && match_)
2114 *match_ = match;
2115 else
2116 geany_match_info_free(match);
2118 g_regex_unref(regex);
2119 return ret;
2123 static gint find_document_usage(GeanyDocument *doc, const gchar *search_text, gint flags)
2125 gchar *buffer, *short_file_name;
2126 struct Sci_TextToFind ttf;
2127 gint count = 0;
2128 gint prev_line = -1;
2129 GSList *match, *matches;
2131 g_return_val_if_fail(DOC_VALID(doc), 0);
2133 short_file_name = g_path_get_basename(DOC_FILENAME(doc));
2135 ttf.chrg.cpMin = 0;
2136 ttf.chrg.cpMax = sci_get_length(doc->editor->sci);
2137 ttf.lpstrText = (gchar *)search_text;
2139 matches = find_range(doc->editor->sci, flags, &ttf);
2140 foreach_slist (match, matches)
2142 GeanyMatchInfo *info = match->data;
2143 gint line = sci_get_line_from_position(doc->editor->sci, info->start);
2145 if (line != prev_line)
2147 buffer = sci_get_line(doc->editor->sci, line);
2148 msgwin_msg_add(COLOR_BLACK, line + 1, doc,
2149 "%s:%d: %s", short_file_name, line + 1, g_strstrip(buffer));
2150 g_free(buffer);
2151 prev_line = line;
2153 count++;
2155 geany_match_info_free(info);
2157 g_slist_free(matches);
2158 g_free(short_file_name);
2159 return count;
2163 void search_find_usage(const gchar *search_text, const gchar *original_search_text,
2164 gint flags, gboolean in_session)
2166 GeanyDocument *doc;
2167 gint count = 0;
2169 doc = document_get_current();
2170 g_return_if_fail(doc != NULL);
2172 if (G_UNLIKELY(EMPTY(search_text)))
2174 utils_beep();
2175 return;
2178 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_MESSAGE);
2179 gtk_list_store_clear(msgwindow.store_msg);
2181 if (! in_session)
2182 { /* use current document */
2183 count = find_document_usage(doc, search_text, flags);
2185 else
2187 guint i;
2188 for (i = 0; i < documents_array->len; i++)
2190 if (documents[i]->is_valid)
2192 count += find_document_usage(documents[i], search_text, flags);
2197 if (count == 0) /* no matches were found */
2199 ui_set_statusbar(FALSE, _("No matches found for \"%s\"."), original_search_text);
2200 msgwin_msg_add(COLOR_BLUE, -1, NULL, _("No matches found for \"%s\"."), original_search_text);
2202 else
2204 ui_set_statusbar(FALSE, ngettext(
2205 "Found %d match for \"%s\".", "Found %d matches for \"%s\".", count),
2206 count, original_search_text);
2207 msgwin_msg_add(COLOR_BLUE, -1, NULL, ngettext(
2208 "Found %d match for \"%s\".", "Found %d matches for \"%s\".", count),
2209 count, original_search_text);
2214 /* ttf is updated to include the last match position (ttf->chrg.cpMin) and
2215 * the new search range end (ttf->chrg.cpMax).
2216 * Note: Normally you would call sci_start/end_undo_action() around this call. */
2217 guint search_replace_range(ScintillaObject *sci, struct Sci_TextToFind *ttf,
2218 gint flags, const gchar *replace_text)
2220 gint count = 0;
2221 gint offset = 0; /* difference between search pos and replace pos */
2222 GSList *match, *matches;
2224 g_return_val_if_fail(sci != NULL && ttf->lpstrText != NULL && replace_text != NULL, 0);
2225 if (! *ttf->lpstrText)
2226 return 0;
2228 matches = find_range(sci, flags, ttf);
2229 foreach_slist (match, matches)
2231 GeanyMatchInfo *info = match->data;
2232 gint replace_len;
2234 info->start += offset;
2235 info->end += offset;
2237 replace_len = search_replace_match(sci, info, replace_text);
2238 offset += replace_len - (info->end - info->start);
2239 count ++;
2241 /* on last match, update the last match/new range end */
2242 if (! match->next)
2244 ttf->chrg.cpMin = info->start;
2245 ttf->chrg.cpMax += offset;
2248 geany_match_info_free(info);
2250 g_slist_free(matches);
2252 return count;
2256 void search_find_again(gboolean change_direction)
2258 GeanyDocument *doc = document_get_current();
2260 g_return_if_fail(doc != NULL);
2262 if (search_data.text)
2264 gboolean forward = ! search_data.backwards;
2265 gint result = document_find_text(doc, search_data.text, search_data.original_text, search_data.flags,
2266 change_direction ? forward : !forward, NULL, FALSE, NULL);
2268 if (result > -1)
2269 editor_display_current_line(doc->editor, 0.3F);
2271 if (search_data.search_bar)
2272 ui_set_search_entry_background(
2273 toolbar_get_widget_child_by_name("SearchEntry"), (result > -1));