Merge pull request #1166 from eht16/fix_template_error_message
[geany-mirror.git] / src / search.c
blobb6dab7dd587aa5bd552e5220b2c20c67efcbf5d7
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 "encodingsprivate.h"
37 #include "keyfile.h"
38 #include "msgwindow.h"
39 #include "prefs.h"
40 #include "sciwrappers.h"
41 #include "spawn.h"
42 #include "stash.h"
43 #include "support.h"
44 #include "toolbar.h"
45 #include "ui_utils.h"
46 #include "utils.h"
48 #include "gtkcompat.h"
50 #include <unistd.h>
51 #include <string.h>
52 #include <ctype.h>
54 #include <gdk/gdkkeysyms.h>
56 enum
58 GEANY_RESPONSE_FIND = 1,
59 GEANY_RESPONSE_FIND_PREVIOUS,
60 GEANY_RESPONSE_FIND_IN_FILE,
61 GEANY_RESPONSE_FIND_IN_SESSION,
62 GEANY_RESPONSE_MARK,
63 GEANY_RESPONSE_REPLACE,
64 GEANY_RESPONSE_REPLACE_AND_FIND,
65 GEANY_RESPONSE_REPLACE_IN_SESSION,
66 GEANY_RESPONSE_REPLACE_IN_FILE,
67 GEANY_RESPONSE_REPLACE_IN_SEL
71 enum
73 FILES_MODE_ALL,
74 FILES_MODE_PROJECT,
75 FILES_MODE_CUSTOM
79 GeanySearchData search_data;
80 GeanySearchPrefs search_prefs;
83 static struct
85 gboolean fif_regexp;
86 gboolean fif_case_sensitive;
87 gboolean fif_match_whole_word;
88 gboolean fif_invert_results;
89 gboolean fif_recursive;
90 gboolean fif_use_extra_options;
91 gchar *fif_extra_options;
92 gint fif_files_mode;
93 gchar *fif_files;
94 gboolean find_regexp;
95 gboolean find_regexp_multiline;
96 gboolean find_escape_sequences;
97 gboolean find_case_sensitive;
98 gboolean find_match_whole_word;
99 gboolean find_match_word_start;
100 gboolean find_close_dialog;
101 gboolean replace_regexp;
102 gboolean replace_regexp_multiline;
103 gboolean replace_escape_sequences;
104 gboolean replace_case_sensitive;
105 gboolean replace_match_whole_word;
106 gboolean replace_match_word_start;
107 gboolean replace_search_backwards;
108 gboolean replace_close_dialog;
110 settings;
112 static StashGroup *fif_prefs = NULL;
113 static StashGroup *find_prefs = NULL;
114 static StashGroup *replace_prefs = NULL;
117 static struct
119 GtkWidget *dialog;
120 GtkWidget *entry;
121 gboolean all_expanded;
122 gint position[2]; /* x, y */
124 find_dlg = {NULL, NULL, FALSE, {0, 0}};
126 static struct
128 GtkWidget *dialog;
129 GtkWidget *find_entry;
130 GtkWidget *replace_entry;
131 gboolean all_expanded;
132 gint position[2]; /* x, y */
134 replace_dlg = {NULL, NULL, NULL, FALSE, {0, 0}};
136 static struct
138 GtkWidget *dialog;
139 GtkWidget *dir_combo;
140 GtkWidget *files_combo;
141 GtkWidget *search_combo;
142 GtkWidget *encoding_combo;
143 GtkWidget *files_mode_combo;
144 gint position[2]; /* x, y */
146 fif_dlg = {NULL, NULL, NULL, NULL, NULL, NULL, {0, 0}};
149 static void search_read_io(GString *string, GIOCondition condition, gpointer data);
150 static void search_read_io_stderr(GString *string, GIOCondition condition, gpointer data);
152 static void search_finished(GPid child_pid, gint status, gpointer user_data);
154 static gchar **search_get_argv(const gchar **argv_prefix, const gchar *dir);
156 static GRegex *compile_regex(const gchar *str, GeanyFindFlags sflags);
159 static void
160 on_find_replace_checkbutton_toggled(GtkToggleButton *togglebutton, gpointer user_data);
162 static void
163 on_find_dialog_response(GtkDialog *dialog, gint response, gpointer user_data);
165 static void
166 on_find_entry_activate(GtkEntry *entry, gpointer user_data);
168 static void
169 on_find_entry_activate_backward(GtkEntry *entry, gpointer user_data);
171 static void
172 on_replace_dialog_response(GtkDialog *dialog, gint response, gpointer user_data);
174 static void
175 on_replace_find_entry_activate(GtkEntry *entry, gpointer user_data);
177 static void
178 on_replace_entry_activate(GtkEntry *entry, gpointer user_data);
180 static void
181 on_find_in_files_dialog_response(GtkDialog *dialog, gint response, gpointer user_data);
183 static gboolean
184 search_find_in_files(const gchar *utf8_search_text, const gchar *dir, const gchar *opts,
185 const gchar *enc);
188 static void init_prefs(void)
190 StashGroup *group;
192 group = stash_group_new("search");
193 configuration_add_pref_group(group, TRUE);
194 stash_group_add_toggle_button(group, &search_prefs.always_wrap,
195 "pref_search_hide_find_dialog", FALSE, "check_always_wrap_search");
196 stash_group_add_toggle_button(group, &search_prefs.hide_find_dialog,
197 "pref_search_always_wrap", FALSE, "check_hide_find_dialog");
198 stash_group_add_toggle_button(group, &search_prefs.use_current_file_dir,
199 "pref_search_current_file_dir", TRUE, "check_fif_current_dir");
200 stash_group_add_boolean(group, &find_dlg.all_expanded, "find_all_expanded", FALSE);
201 stash_group_add_boolean(group, &replace_dlg.all_expanded, "replace_all_expanded", FALSE);
202 /* dialog positions */
203 stash_group_add_integer(group, &find_dlg.position[0], "position_find_x", -1);
204 stash_group_add_integer(group, &find_dlg.position[1], "position_find_y", -1);
205 stash_group_add_integer(group, &replace_dlg.position[0], "position_replace_x", -1);
206 stash_group_add_integer(group, &replace_dlg.position[1], "position_replace_y", -1);
207 stash_group_add_integer(group, &fif_dlg.position[0], "position_fif_x", -1);
208 stash_group_add_integer(group, &fif_dlg.position[1], "position_fif_y", -1);
210 memset(&settings, '\0', sizeof(settings));
212 group = stash_group_new("search");
213 fif_prefs = group;
214 configuration_add_pref_group(group, FALSE);
215 stash_group_add_toggle_button(group, &settings.fif_regexp,
216 "fif_regexp", FALSE, "check_regexp");
217 stash_group_add_toggle_button(group, &settings.fif_case_sensitive,
218 "fif_case_sensitive", TRUE, "check_case");
219 stash_group_add_toggle_button(group, &settings.fif_match_whole_word,
220 "fif_match_whole_word", FALSE, "check_wholeword");
221 stash_group_add_toggle_button(group, &settings.fif_invert_results,
222 "fif_invert_results", FALSE, "check_invert");
223 stash_group_add_toggle_button(group, &settings.fif_recursive,
224 "fif_recursive", FALSE, "check_recursive");
225 stash_group_add_entry(group, &settings.fif_extra_options,
226 "fif_extra_options", "", "entry_extra");
227 stash_group_add_toggle_button(group, &settings.fif_use_extra_options,
228 "fif_use_extra_options", FALSE, "check_extra");
229 stash_group_add_entry(group, &settings.fif_files,
230 "fif_files", "", "entry_files");
231 stash_group_add_combo_box(group, &settings.fif_files_mode,
232 "fif_files_mode", FILES_MODE_ALL, "combo_files_mode");
234 group = stash_group_new("search");
235 find_prefs = group;
236 configuration_add_pref_group(group, FALSE);
237 stash_group_add_toggle_button(group, &settings.find_regexp,
238 "find_regexp", FALSE, "check_regexp");
239 stash_group_add_toggle_button(group, &settings.find_regexp_multiline,
240 "find_regexp_multiline", FALSE, "check_multiline");
241 stash_group_add_toggle_button(group, &settings.find_case_sensitive,
242 "find_case_sensitive", FALSE, "check_case");
243 stash_group_add_toggle_button(group, &settings.find_escape_sequences,
244 "find_escape_sequences", FALSE, "check_escape");
245 stash_group_add_toggle_button(group, &settings.find_match_whole_word,
246 "find_match_whole_word", FALSE, "check_word");
247 stash_group_add_toggle_button(group, &settings.find_match_word_start,
248 "find_match_word_start", FALSE, "check_wordstart");
249 stash_group_add_toggle_button(group, &settings.find_close_dialog,
250 "find_close_dialog", TRUE, "check_close");
252 group = stash_group_new("search");
253 replace_prefs = group;
254 configuration_add_pref_group(group, FALSE);
255 stash_group_add_toggle_button(group, &settings.replace_regexp,
256 "replace_regexp", FALSE, "check_regexp");
257 stash_group_add_toggle_button(group, &settings.replace_regexp_multiline,
258 "replace_regexp_multiline", FALSE, "check_multiline");
259 stash_group_add_toggle_button(group, &settings.replace_case_sensitive,
260 "replace_case_sensitive", FALSE, "check_case");
261 stash_group_add_toggle_button(group, &settings.replace_escape_sequences,
262 "replace_escape_sequences", FALSE, "check_escape");
263 stash_group_add_toggle_button(group, &settings.replace_match_whole_word,
264 "replace_match_whole_word", FALSE, "check_word");
265 stash_group_add_toggle_button(group, &settings.replace_match_word_start,
266 "replace_match_word_start", FALSE, "check_wordstart");
267 stash_group_add_toggle_button(group, &settings.replace_search_backwards,
268 "replace_search_backwards", FALSE, "check_back");
269 stash_group_add_toggle_button(group, &settings.replace_close_dialog,
270 "replace_close_dialog", TRUE, "check_close");
274 void search_init(void)
276 search_data.text = NULL;
277 search_data.original_text = NULL;
278 init_prefs();
282 #define FREE_WIDGET(wid) \
283 if (wid && GTK_IS_WIDGET(wid)) gtk_widget_destroy(wid);
285 void search_finalize(void)
287 FREE_WIDGET(find_dlg.dialog);
288 FREE_WIDGET(replace_dlg.dialog);
289 FREE_WIDGET(fif_dlg.dialog);
290 g_free(search_data.text);
291 g_free(search_data.original_text);
295 static void on_widget_toggled_set_insensitive(
296 GtkToggleButton *togglebutton, gpointer user_data)
298 gtk_widget_set_sensitive(GTK_WIDGET(user_data),
299 !gtk_toggle_button_get_active(togglebutton));
303 static GtkWidget *add_find_checkboxes(GtkDialog *dialog)
305 GtkWidget *checkbox1, *checkbox2, *check_regexp, *checkbox5,
306 *checkbox7, *check_multiline, *hbox, *fbox, *mbox;
308 check_regexp = gtk_check_button_new_with_mnemonic(_("_Use regular expressions"));
309 ui_hookup_widget(dialog, check_regexp, "check_regexp");
310 gtk_button_set_focus_on_click(GTK_BUTTON(check_regexp), FALSE);
311 gtk_widget_set_tooltip_text(check_regexp, _("Use Perl-like regular expressions. "
312 "For detailed information about using regular expressions, please refer to the manual."));
313 g_signal_connect(check_regexp, "toggled",
314 G_CALLBACK(on_find_replace_checkbutton_toggled), dialog);
316 checkbox7 = gtk_check_button_new_with_mnemonic(_("Use _escape sequences"));
317 ui_hookup_widget(dialog, checkbox7, "check_escape");
318 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox7), FALSE);
319 gtk_widget_set_tooltip_text(checkbox7,
320 _("Replace \\\\, \\t, \\n, \\r and \\uXXXX (Unicode characters) with the "
321 "corresponding control characters"));
323 check_multiline = gtk_check_button_new_with_mnemonic(_("Use multi-line matchin_g"));
324 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_multiline), FALSE);
325 gtk_widget_set_sensitive(check_multiline, FALSE);
326 ui_hookup_widget(dialog, check_multiline, "check_multiline");
327 gtk_button_set_focus_on_click(GTK_BUTTON(check_multiline), FALSE);
328 gtk_widget_set_tooltip_text(check_multiline, _("Perform regular expression "
329 "matching on the whole buffer at once rather than line by line, allowing "
330 "matches to span multiple lines. In this mode, newline characters are part "
331 "of the input and can be captured as normal characters by the pattern."));
333 /* Search features */
334 fbox = gtk_vbox_new(FALSE, 0);
335 gtk_box_pack_start(GTK_BOX(fbox), check_regexp, FALSE, FALSE, 0);
336 gtk_box_pack_start(GTK_BOX(fbox), check_multiline, FALSE, FALSE, 0);
337 gtk_box_pack_start(GTK_BOX(fbox), checkbox7, FALSE, FALSE, 0);
339 if (dialog != GTK_DIALOG(find_dlg.dialog))
341 GtkWidget *check_back = gtk_check_button_new_with_mnemonic(_("Search _backwards"));
342 ui_hookup_widget(dialog, check_back, "check_back");
343 gtk_button_set_focus_on_click(GTK_BUTTON(check_back), FALSE);
344 gtk_container_add(GTK_CONTAINER(fbox), check_back);
347 checkbox1 = gtk_check_button_new_with_mnemonic(_("C_ase sensitive"));
348 ui_hookup_widget(dialog, checkbox1, "check_case");
349 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox1), FALSE);
351 checkbox2 = gtk_check_button_new_with_mnemonic(_("Match only a _whole word"));
352 ui_hookup_widget(dialog, checkbox2, "check_word");
353 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox2), FALSE);
355 checkbox5 = gtk_check_button_new_with_mnemonic(_("Match from s_tart of word"));
356 ui_hookup_widget(dialog, checkbox5, "check_wordstart");
357 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox5), FALSE);
359 /* disable wordstart when wholeword is checked */
360 g_signal_connect(checkbox2, "toggled",
361 G_CALLBACK(on_widget_toggled_set_insensitive), checkbox5);
363 /* Matching options */
364 mbox = gtk_vbox_new(FALSE, 0);
365 gtk_box_pack_start(GTK_BOX(mbox), checkbox1, FALSE, FALSE, 0);
366 gtk_box_pack_start(GTK_BOX(mbox), checkbox2, FALSE, FALSE, 0);
367 gtk_box_pack_start(GTK_BOX(mbox), checkbox5, FALSE, FALSE, 0);
369 hbox = gtk_hbox_new(TRUE, 6);
370 gtk_container_add(GTK_CONTAINER(hbox), fbox);
371 gtk_container_add(GTK_CONTAINER(hbox), mbox);
372 return hbox;
376 static void send_find_dialog_response(GtkButton *button, gpointer user_data)
378 gtk_dialog_response(GTK_DIALOG(find_dlg.dialog), GPOINTER_TO_INT(user_data));
382 /* store text, clear search flags so we can use Search->Find Next/Previous */
383 static void setup_find_next(const gchar *text)
385 g_free(search_data.text);
386 g_free(search_data.original_text);
387 search_data.text = g_strdup(text);
388 search_data.original_text = g_strdup(text);
389 search_data.flags = 0;
390 search_data.backwards = FALSE;
391 search_data.search_bar = FALSE;
395 /* Search for next match of the current "selection".
396 * Optionally for X11 based systems, this will try to use the system-wide
397 * x-selection first.
398 * If it doesn't find a suitable search string it will try to use
399 * the current word instead, or just repeat the last search.
400 * Search flags are always zero.
402 void search_find_selection(GeanyDocument *doc, gboolean search_backwards)
404 gchar *s = NULL;
406 g_return_if_fail(DOC_VALID(doc));
408 #ifdef G_OS_UNIX
409 if (search_prefs.find_selection_type == GEANY_FIND_SEL_X)
411 GtkClipboard *clipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
413 s = gtk_clipboard_wait_for_text(clipboard);
414 if (s && (strchr(s,'\n') || strchr(s, '\r')))
416 g_free(s);
417 s = NULL;
420 #endif
422 if (!s && sci_has_selection(doc->editor->sci))
423 s = sci_get_selection_contents(doc->editor->sci);
425 if (!s && search_prefs.find_selection_type != GEANY_FIND_SEL_AGAIN)
427 /* get the current word */
428 s = editor_get_default_selection(doc->editor, TRUE, NULL);
431 if (s)
433 setup_find_next(s); /* allow find next/prev */
435 if (document_find_text(doc, s, NULL, 0, search_backwards, NULL, FALSE, NULL) > -1)
436 editor_display_current_line(doc->editor, 0.3F);
437 g_free(s);
439 else if (search_prefs.find_selection_type == GEANY_FIND_SEL_AGAIN)
441 /* Repeat last search (in case selection was lost) */
442 search_find_again(search_backwards);
444 else
446 utils_beep();
451 static void on_expander_activated(GtkExpander *exp, gpointer data)
453 gboolean *setting = data;
455 *setting = gtk_expander_get_expanded(exp);
459 static void create_find_dialog(void)
461 GtkWidget *label, *entry, *sbox, *vbox;
462 GtkWidget *exp, *bbox, *button, *check_close;
464 find_dlg.dialog = gtk_dialog_new_with_buttons(_("Find"),
465 GTK_WINDOW(main_widgets.window), GTK_DIALOG_DESTROY_WITH_PARENT,
466 GTK_STOCK_CLOSE, GTK_RESPONSE_CANCEL, NULL);
467 vbox = ui_dialog_vbox_new(GTK_DIALOG(find_dlg.dialog));
468 gtk_widget_set_name(find_dlg.dialog, "GeanyDialogSearch");
469 gtk_box_set_spacing(GTK_BOX(vbox), 9);
471 button = ui_button_new_with_image(GTK_STOCK_GO_BACK, _("_Previous"));
472 gtk_dialog_add_action_widget(GTK_DIALOG(find_dlg.dialog), button,
473 GEANY_RESPONSE_FIND_PREVIOUS);
474 ui_hookup_widget(find_dlg.dialog, button, "btn_previous");
476 button = ui_button_new_with_image(GTK_STOCK_GO_FORWARD, _("_Next"));
477 gtk_dialog_add_action_widget(GTK_DIALOG(find_dlg.dialog), button,
478 GEANY_RESPONSE_FIND);
480 label = gtk_label_new_with_mnemonic(_("_Search for:"));
481 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
483 entry = gtk_combo_box_text_new_with_entry();
484 ui_entry_add_clear_icon(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(entry))));
485 gtk_label_set_mnemonic_widget(GTK_LABEL(label), entry);
486 gtk_entry_set_width_chars(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(entry))), 50);
487 find_dlg.entry = gtk_bin_get_child(GTK_BIN(entry));
489 g_signal_connect(gtk_bin_get_child(GTK_BIN(entry)), "activate",
490 G_CALLBACK(on_find_entry_activate), entry);
491 ui_entry_add_activate_backward_signal(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(entry))));
492 g_signal_connect(gtk_bin_get_child(GTK_BIN(entry)), "activate-backward",
493 G_CALLBACK(on_find_entry_activate_backward), entry);
494 g_signal_connect(find_dlg.dialog, "response",
495 G_CALLBACK(on_find_dialog_response), entry);
496 g_signal_connect(find_dlg.dialog, "delete-event",
497 G_CALLBACK(gtk_widget_hide_on_delete), NULL);
499 sbox = gtk_hbox_new(FALSE, 6);
500 gtk_box_pack_start(GTK_BOX(sbox), label, FALSE, FALSE, 0);
501 gtk_box_pack_start(GTK_BOX(sbox), entry, TRUE, TRUE, 0);
502 gtk_box_pack_start(GTK_BOX(vbox), sbox, TRUE, FALSE, 0);
504 gtk_container_add(GTK_CONTAINER(vbox),
505 add_find_checkboxes(GTK_DIALOG(find_dlg.dialog)));
507 /* Now add the multiple match options */
508 exp = gtk_expander_new_with_mnemonic(_("_Find All"));
509 gtk_expander_set_expanded(GTK_EXPANDER(exp), find_dlg.all_expanded);
510 g_signal_connect_after(exp, "activate",
511 G_CALLBACK(on_expander_activated), &find_dlg.all_expanded);
513 bbox = gtk_hbutton_box_new();
515 button = gtk_button_new_with_mnemonic(_("_Mark"));
516 gtk_widget_set_tooltip_text(button,
517 _("Mark all matches in the current document"));
518 gtk_container_add(GTK_CONTAINER(bbox), button);
519 g_signal_connect(button, "clicked", G_CALLBACK(send_find_dialog_response),
520 GINT_TO_POINTER(GEANY_RESPONSE_MARK));
522 button = gtk_button_new_with_mnemonic(_("In Sessi_on"));
523 gtk_container_add(GTK_CONTAINER(bbox), button);
524 g_signal_connect(button, "clicked", G_CALLBACK(send_find_dialog_response),
525 GINT_TO_POINTER(GEANY_RESPONSE_FIND_IN_SESSION));
527 button = gtk_button_new_with_mnemonic(_("_In Document"));
528 gtk_container_add(GTK_CONTAINER(bbox), button);
529 g_signal_connect(button, "clicked", G_CALLBACK(send_find_dialog_response),
530 GINT_TO_POINTER(GEANY_RESPONSE_FIND_IN_FILE));
532 /* close window checkbox */
533 check_close = gtk_check_button_new_with_mnemonic(_("Close _dialog"));
534 ui_hookup_widget(find_dlg.dialog, check_close, "check_close");
535 gtk_button_set_focus_on_click(GTK_BUTTON(check_close), FALSE);
536 gtk_widget_set_tooltip_text(check_close,
537 _("Disable this option to keep the dialog open"));
538 gtk_container_add(GTK_CONTAINER(bbox), check_close);
539 gtk_button_box_set_child_secondary(GTK_BUTTON_BOX(bbox), check_close, TRUE);
541 ui_hbutton_box_copy_layout(
542 GTK_BUTTON_BOX(gtk_dialog_get_action_area(GTK_DIALOG(find_dlg.dialog))),
543 GTK_BUTTON_BOX(bbox));
544 gtk_container_add(GTK_CONTAINER(exp), bbox);
545 gtk_container_add(GTK_CONTAINER(vbox), exp);
549 static void set_dialog_position(GtkWidget *dialog, gint *position)
551 if (position[0] >= 0)
552 gtk_window_move(GTK_WINDOW(dialog), position[0], position[1]);
556 void search_show_find_dialog(void)
558 GeanyDocument *doc = document_get_current();
559 gchar *sel = NULL;
561 g_return_if_fail(doc != NULL);
563 sel = editor_get_default_selection(doc->editor, search_prefs.use_current_word, NULL);
565 if (find_dlg.dialog == NULL)
567 create_find_dialog();
568 stash_group_display(find_prefs, find_dlg.dialog);
569 if (sel)
570 gtk_entry_set_text(GTK_ENTRY(find_dlg.entry), sel);
572 set_dialog_position(find_dlg.dialog, find_dlg.position);
573 gtk_widget_show_all(find_dlg.dialog);
575 else
577 /* only set selection if the dialog is not already visible */
578 if (! gtk_widget_get_visible(find_dlg.dialog) && sel)
579 gtk_entry_set_text(GTK_ENTRY(find_dlg.entry), sel);
580 gtk_widget_grab_focus(find_dlg.entry);
581 set_dialog_position(find_dlg.dialog, find_dlg.position);
582 gtk_widget_show(find_dlg.dialog);
583 if (sel != NULL) /* when we have a selection, reset the entry widget's background colour */
584 ui_set_search_entry_background(find_dlg.entry, TRUE);
585 /* bring the dialog back in the foreground in case it is already open but the focus is away */
586 gtk_window_present(GTK_WINDOW(find_dlg.dialog));
589 g_free(sel);
593 static void send_replace_dialog_response(GtkButton *button, gpointer user_data)
595 gtk_dialog_response(GTK_DIALOG(replace_dlg.dialog), GPOINTER_TO_INT(user_data));
599 static gboolean
600 on_widget_key_pressed_set_focus(GtkWidget *widget, GdkEventKey *event, gpointer user_data)
602 if (event->keyval == GDK_Tab)
604 gtk_widget_grab_focus(GTK_WIDGET(user_data));
605 return TRUE;
607 return FALSE;
611 static void create_replace_dialog(void)
613 GtkWidget *label_find, *label_replace, *entry_find, *entry_replace,
614 *check_close, *button, *rbox, *fbox, *vbox, *exp, *bbox;
615 GtkSizeGroup *label_size;
617 replace_dlg.dialog = gtk_dialog_new_with_buttons(_("Replace"),
618 GTK_WINDOW(main_widgets.window), GTK_DIALOG_DESTROY_WITH_PARENT,
619 GTK_STOCK_CLOSE, GTK_RESPONSE_CANCEL, NULL);
620 vbox = ui_dialog_vbox_new(GTK_DIALOG(replace_dlg.dialog));
621 gtk_box_set_spacing(GTK_BOX(vbox), 9);
622 gtk_widget_set_name(replace_dlg.dialog, "GeanyDialogSearch");
624 button = gtk_button_new_from_stock(GTK_STOCK_FIND);
625 gtk_dialog_add_action_widget(GTK_DIALOG(replace_dlg.dialog), button,
626 GEANY_RESPONSE_FIND);
627 button = gtk_button_new_with_mnemonic(_("_Replace"));
628 gtk_button_set_image(GTK_BUTTON(button),
629 gtk_image_new_from_stock(GTK_STOCK_FIND_AND_REPLACE, GTK_ICON_SIZE_BUTTON));
630 gtk_dialog_add_action_widget(GTK_DIALOG(replace_dlg.dialog), button,
631 GEANY_RESPONSE_REPLACE);
632 button = gtk_button_new_with_mnemonic(_("Replace & Fi_nd"));
633 gtk_button_set_image(GTK_BUTTON(button),
634 gtk_image_new_from_stock(GTK_STOCK_FIND_AND_REPLACE, GTK_ICON_SIZE_BUTTON));
635 gtk_dialog_add_action_widget(GTK_DIALOG(replace_dlg.dialog), button,
636 GEANY_RESPONSE_REPLACE_AND_FIND);
638 label_find = gtk_label_new_with_mnemonic(_("_Search for:"));
639 gtk_misc_set_alignment(GTK_MISC(label_find), 0, 0.5);
641 label_replace = gtk_label_new_with_mnemonic(_("Replace wit_h:"));
642 gtk_misc_set_alignment(GTK_MISC(label_replace), 0, 0.5);
644 entry_find = gtk_combo_box_text_new_with_entry();
645 ui_entry_add_clear_icon(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(entry_find))));
646 gtk_label_set_mnemonic_widget(GTK_LABEL(label_find), entry_find);
647 gtk_entry_set_width_chars(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(entry_find))), 50);
648 ui_hookup_widget(replace_dlg.dialog, entry_find, "entry_find");
649 replace_dlg.find_entry = gtk_bin_get_child(GTK_BIN(entry_find));
651 entry_replace = gtk_combo_box_text_new_with_entry();
652 ui_entry_add_clear_icon(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(entry_replace))));
653 gtk_label_set_mnemonic_widget(GTK_LABEL(label_replace), entry_replace);
654 gtk_entry_set_width_chars(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(entry_replace))), 50);
655 ui_hookup_widget(replace_dlg.dialog, entry_replace, "entry_replace");
656 replace_dlg.replace_entry = gtk_bin_get_child(GTK_BIN(entry_replace));
658 /* tab from find to the replace entry */
659 g_signal_connect(gtk_bin_get_child(GTK_BIN(entry_find)),
660 "key-press-event", G_CALLBACK(on_widget_key_pressed_set_focus),
661 gtk_bin_get_child(GTK_BIN(entry_replace)));
662 g_signal_connect(gtk_bin_get_child(GTK_BIN(entry_find)), "activate",
663 G_CALLBACK(on_replace_find_entry_activate), NULL);
664 g_signal_connect(gtk_bin_get_child(GTK_BIN(entry_replace)), "activate",
665 G_CALLBACK(on_replace_entry_activate), NULL);
666 g_signal_connect(replace_dlg.dialog, "response",
667 G_CALLBACK(on_replace_dialog_response), NULL);
668 g_signal_connect(replace_dlg.dialog, "delete-event",
669 G_CALLBACK(gtk_widget_hide_on_delete), NULL);
671 fbox = gtk_hbox_new(FALSE, 6);
672 gtk_box_pack_start(GTK_BOX(fbox), label_find, FALSE, FALSE, 0);
673 gtk_box_pack_start(GTK_BOX(fbox), entry_find, TRUE, TRUE, 0);
675 rbox = gtk_hbox_new(FALSE, 6);
676 gtk_box_pack_start(GTK_BOX(rbox), label_replace, FALSE, FALSE, 0);
677 gtk_box_pack_start(GTK_BOX(rbox), entry_replace, TRUE, TRUE, 0);
679 label_size = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
680 gtk_size_group_add_widget(label_size, label_find);
681 gtk_size_group_add_widget(label_size, label_replace);
682 g_object_unref(G_OBJECT(label_size)); /* auto destroy the size group */
684 gtk_box_pack_start(GTK_BOX(vbox), fbox, TRUE, FALSE, 0);
685 gtk_box_pack_start(GTK_BOX(vbox), rbox, TRUE, FALSE, 0);
686 gtk_container_add(GTK_CONTAINER(vbox),
687 add_find_checkboxes(GTK_DIALOG(replace_dlg.dialog)));
689 /* Now add the multiple replace options */
690 exp = gtk_expander_new_with_mnemonic(_("Re_place All"));
691 gtk_expander_set_expanded(GTK_EXPANDER(exp), replace_dlg.all_expanded);
692 g_signal_connect_after(exp, "activate",
693 G_CALLBACK(on_expander_activated), &replace_dlg.all_expanded);
695 bbox = gtk_hbutton_box_new();
697 button = gtk_button_new_with_mnemonic(_("In Sessi_on"));
698 gtk_container_add(GTK_CONTAINER(bbox), button);
699 g_signal_connect(button, "clicked", G_CALLBACK(send_replace_dialog_response),
700 GINT_TO_POINTER(GEANY_RESPONSE_REPLACE_IN_SESSION));
702 button = gtk_button_new_with_mnemonic(_("_In Document"));
703 gtk_container_add(GTK_CONTAINER(bbox), button);
704 g_signal_connect(button, "clicked", G_CALLBACK(send_replace_dialog_response),
705 GINT_TO_POINTER(GEANY_RESPONSE_REPLACE_IN_FILE));
707 button = gtk_button_new_with_mnemonic(_("In Se_lection"));
708 gtk_widget_set_tooltip_text(button,
709 _("Replace all matches found in the currently selected text"));
710 gtk_container_add(GTK_CONTAINER(bbox), button);
711 g_signal_connect(button, "clicked", G_CALLBACK(send_replace_dialog_response),
712 GINT_TO_POINTER(GEANY_RESPONSE_REPLACE_IN_SEL));
714 /* close window checkbox */
715 check_close = gtk_check_button_new_with_mnemonic(_("Close _dialog"));
716 ui_hookup_widget(replace_dlg.dialog, check_close, "check_close");
717 gtk_button_set_focus_on_click(GTK_BUTTON(check_close), FALSE);
718 gtk_widget_set_tooltip_text(check_close,
719 _("Disable this option to keep the dialog open"));
720 gtk_container_add(GTK_CONTAINER(bbox), check_close);
721 gtk_button_box_set_child_secondary(GTK_BUTTON_BOX(bbox), check_close, TRUE);
723 ui_hbutton_box_copy_layout(
724 GTK_BUTTON_BOX(gtk_dialog_get_action_area(GTK_DIALOG(replace_dlg.dialog))),
725 GTK_BUTTON_BOX(bbox));
726 gtk_container_add(GTK_CONTAINER(exp), bbox);
727 gtk_container_add(GTK_CONTAINER(vbox), exp);
731 void search_show_replace_dialog(void)
733 GeanyDocument *doc = document_get_current();
734 gchar *sel = NULL;
736 if (doc == NULL)
737 return;
739 sel = editor_get_default_selection(doc->editor, search_prefs.use_current_word, NULL);
741 if (replace_dlg.dialog == NULL)
743 create_replace_dialog();
744 stash_group_display(replace_prefs, replace_dlg.dialog);
745 if (sel)
746 gtk_entry_set_text(GTK_ENTRY(replace_dlg.find_entry), sel);
748 set_dialog_position(replace_dlg.dialog, replace_dlg.position);
749 gtk_widget_show_all(replace_dlg.dialog);
751 else
753 /* only set selection if the dialog is not already visible */
754 if (! gtk_widget_get_visible(replace_dlg.dialog) && sel)
755 gtk_entry_set_text(GTK_ENTRY(replace_dlg.find_entry), sel);
756 if (sel != NULL) /* when we have a selection, reset the entry widget's background colour */
757 ui_set_search_entry_background(replace_dlg.find_entry, TRUE);
758 gtk_widget_grab_focus(replace_dlg.find_entry);
759 set_dialog_position(replace_dlg.dialog, replace_dlg.position);
760 gtk_widget_show(replace_dlg.dialog);
761 /* bring the dialog back in the foreground in case it is already open but the focus is away */
762 gtk_window_present(GTK_WINDOW(replace_dlg.dialog));
765 g_free(sel);
769 static void on_widget_toggled_set_sensitive(GtkToggleButton *togglebutton, gpointer user_data)
771 /* disable extra option entry when checkbutton not checked */
772 gtk_widget_set_sensitive(GTK_WIDGET(user_data),
773 gtk_toggle_button_get_active(togglebutton));
777 static void update_file_patterns(GtkWidget *mode_combo, GtkWidget *fcombo)
779 gint selection;
780 GtkWidget *entry;
782 entry = gtk_bin_get_child(GTK_BIN(fcombo));
784 selection = gtk_combo_box_get_active(GTK_COMBO_BOX(mode_combo));
786 if (selection == FILES_MODE_ALL)
788 gtk_entry_set_text(GTK_ENTRY(entry), "");
789 gtk_widget_set_sensitive(fcombo, FALSE);
791 else if (selection == FILES_MODE_CUSTOM)
793 gtk_widget_set_sensitive(fcombo, TRUE);
795 else if (selection == FILES_MODE_PROJECT)
797 if (app->project && !EMPTY(app->project->file_patterns))
799 gchar *patterns;
801 patterns = g_strjoinv(" ", app->project->file_patterns);
802 gtk_entry_set_text(GTK_ENTRY(entry), patterns);
803 g_free(patterns);
805 else
807 gtk_entry_set_text(GTK_ENTRY(entry), "");
810 gtk_widget_set_sensitive(fcombo, FALSE);
815 /* creates the combo to choose which files include in the search */
816 static GtkWidget *create_fif_file_mode_combo(void)
818 GtkWidget *combo;
819 GtkCellRenderer *renderer;
820 GtkListStore *store;
821 GtkTreeIter iter;
823 /* text/sensitive */
824 store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_BOOLEAN);
825 gtk_list_store_append(store, &iter);
826 gtk_list_store_set(store, &iter, 0, _("all"), 1, TRUE, -1);
827 gtk_list_store_append(store, &iter);
828 gtk_list_store_set(store, &iter, 0, _("project"), 1, app->project != NULL, -1);
829 gtk_list_store_append(store, &iter);
830 gtk_list_store_set(store, &iter, 0, _("custom"), 1, TRUE, -1);
832 combo = gtk_combo_box_new_with_model(GTK_TREE_MODEL(store));
833 g_object_unref(store);
834 gtk_widget_set_tooltip_text(combo, _("All: search all files in the directory\n"
835 "Project: use file patterns defined in the project settings\n"
836 "Custom: specify file patterns manually"));
838 renderer = gtk_cell_renderer_text_new();
839 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), renderer, TRUE);
840 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo), renderer, "text", 0, "sensitive", 1, NULL);
842 return combo;
846 /* updates the sensitivity of the project combo item */
847 static void update_fif_file_mode_combo(void)
849 GtkTreeModel *model = gtk_combo_box_get_model(GTK_COMBO_BOX(fif_dlg.files_mode_combo));
850 GtkTreeIter iter;
852 /* "1" refers to the second list entry, project */
853 if (gtk_tree_model_get_iter_from_string(model, &iter, "1"))
854 gtk_list_store_set(GTK_LIST_STORE(model), &iter, 1, app->project != NULL, -1);
858 static void create_fif_dialog(void)
860 GtkWidget *dir_combo, *combo, *fcombo, *e_combo, *entry;
861 GtkWidget *label, *label1, *label2, *label3, *checkbox1, *checkbox2, *check_wholeword,
862 *check_recursive, *check_extra, *entry_extra, *check_regexp, *combo_files_mode;
863 GtkWidget *dbox, *sbox, *lbox, *rbox, *hbox, *vbox, *ebox;
864 GtkSizeGroup *size_group;
866 fif_dlg.dialog = gtk_dialog_new_with_buttons(
867 _("Find in Files"), GTK_WINDOW(main_widgets.window), GTK_DIALOG_DESTROY_WITH_PARENT,
868 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, NULL);
869 vbox = ui_dialog_vbox_new(GTK_DIALOG(fif_dlg.dialog));
870 gtk_box_set_spacing(GTK_BOX(vbox), 9);
871 gtk_widget_set_name(fif_dlg.dialog, "GeanyDialogSearch");
873 gtk_dialog_add_button(GTK_DIALOG(fif_dlg.dialog), GTK_STOCK_FIND, GTK_RESPONSE_ACCEPT);
874 gtk_dialog_set_default_response(GTK_DIALOG(fif_dlg.dialog),
875 GTK_RESPONSE_ACCEPT);
877 label = gtk_label_new_with_mnemonic(_("_Search for:"));
878 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
880 combo = gtk_combo_box_text_new_with_entry();
881 entry = gtk_bin_get_child(GTK_BIN(combo));
882 ui_entry_add_clear_icon(GTK_ENTRY(entry));
883 gtk_label_set_mnemonic_widget(GTK_LABEL(label), entry);
884 gtk_entry_set_width_chars(GTK_ENTRY(entry), 50);
885 gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
886 fif_dlg.search_combo = combo;
888 sbox = gtk_hbox_new(FALSE, 6);
889 gtk_box_pack_start(GTK_BOX(sbox), label, FALSE, FALSE, 0);
890 gtk_box_pack_start(GTK_BOX(sbox), combo, TRUE, TRUE, 0);
892 /* make labels same width */
893 size_group = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
894 gtk_size_group_add_widget(size_group, label);
896 label3 = gtk_label_new_with_mnemonic(_("Fi_les:"));
897 gtk_misc_set_alignment(GTK_MISC(label3), 0, 0.5);
899 combo_files_mode = create_fif_file_mode_combo();
900 gtk_label_set_mnemonic_widget(GTK_LABEL(label3), combo_files_mode);
901 ui_hookup_widget(fif_dlg.dialog, combo_files_mode, "combo_files_mode");
902 fif_dlg.files_mode_combo = combo_files_mode;
904 fcombo = gtk_combo_box_text_new_with_entry();
905 entry = gtk_bin_get_child(GTK_BIN(fcombo));
906 ui_entry_add_clear_icon(GTK_ENTRY(entry));
907 gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
908 gtk_widget_set_tooltip_text(entry, _("File patterns, e.g. *.c *.h"));
909 ui_hookup_widget(fif_dlg.dialog, entry, "entry_files");
910 fif_dlg.files_combo = fcombo;
912 /* update the entry when selection is changed */
913 g_signal_connect(combo_files_mode, "changed", G_CALLBACK(update_file_patterns), fcombo);
915 hbox = gtk_hbox_new(FALSE, 6);
916 gtk_box_pack_start(GTK_BOX(hbox), label3, FALSE, FALSE, 0);
917 gtk_box_pack_start(GTK_BOX(hbox), combo_files_mode, FALSE, FALSE, 0);
918 gtk_box_pack_start(GTK_BOX(hbox), fcombo, TRUE, TRUE, 0);
920 label1 = gtk_label_new_with_mnemonic(_("_Directory:"));
921 gtk_misc_set_alignment(GTK_MISC(label1), 0, 0.5);
923 dir_combo = gtk_combo_box_text_new_with_entry();
924 entry = gtk_bin_get_child(GTK_BIN(dir_combo));
925 ui_entry_add_clear_icon(GTK_ENTRY(entry));
926 gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
927 gtk_label_set_mnemonic_widget(GTK_LABEL(label1), entry);
928 gtk_entry_set_width_chars(GTK_ENTRY(entry), 50);
929 fif_dlg.dir_combo = dir_combo;
931 /* tab from files to the dir entry */
932 g_signal_connect(gtk_bin_get_child(GTK_BIN(fcombo)), "key-press-event",
933 G_CALLBACK(on_widget_key_pressed_set_focus), entry);
935 dbox = ui_path_box_new(NULL, GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
936 GTK_ENTRY(entry));
937 gtk_box_pack_start(GTK_BOX(dbox), label1, FALSE, FALSE, 0);
939 label2 = gtk_label_new_with_mnemonic(_("E_ncoding:"));
940 gtk_misc_set_alignment(GTK_MISC(label2), 0, 0.5);
942 e_combo = ui_create_encodings_combo_box(FALSE, GEANY_ENCODING_UTF_8);
943 gtk_label_set_mnemonic_widget(GTK_LABEL(label2), e_combo);
944 fif_dlg.encoding_combo = e_combo;
946 ebox = gtk_hbox_new(FALSE, 6);
947 gtk_box_pack_start(GTK_BOX(ebox), label2, FALSE, FALSE, 0);
948 gtk_box_pack_start(GTK_BOX(ebox), e_combo, TRUE, TRUE, 0);
950 gtk_size_group_add_widget(size_group, label1);
951 gtk_size_group_add_widget(size_group, label2);
952 gtk_size_group_add_widget(size_group, label3);
953 g_object_unref(G_OBJECT(size_group)); /* auto destroy the size group */
955 gtk_box_pack_start(GTK_BOX(vbox), sbox, TRUE, FALSE, 0);
956 gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, FALSE, 0);
957 gtk_box_pack_start(GTK_BOX(vbox), dbox, TRUE, FALSE, 0);
958 gtk_box_pack_start(GTK_BOX(vbox), ebox, TRUE, FALSE, 0);
960 check_regexp = gtk_check_button_new_with_mnemonic(_("_Use regular expressions"));
961 ui_hookup_widget(fif_dlg.dialog, check_regexp, "check_regexp");
962 gtk_button_set_focus_on_click(GTK_BUTTON(check_regexp), FALSE);
963 gtk_widget_set_tooltip_text(check_regexp, _("See grep's manual page for more information"));
965 check_recursive = gtk_check_button_new_with_mnemonic(_("_Recurse in subfolders"));
966 ui_hookup_widget(fif_dlg.dialog, check_recursive, "check_recursive");
967 gtk_button_set_focus_on_click(GTK_BUTTON(check_recursive), FALSE);
969 checkbox1 = gtk_check_button_new_with_mnemonic(_("C_ase sensitive"));
970 ui_hookup_widget(fif_dlg.dialog, checkbox1, "check_case");
971 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox1), FALSE);
972 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox1), TRUE);
974 check_wholeword = gtk_check_button_new_with_mnemonic(_("Match only a _whole word"));
975 ui_hookup_widget(fif_dlg.dialog, check_wholeword, "check_wholeword");
976 gtk_button_set_focus_on_click(GTK_BUTTON(check_wholeword), FALSE);
978 checkbox2 = gtk_check_button_new_with_mnemonic(_("_Invert search results"));
979 ui_hookup_widget(fif_dlg.dialog, checkbox2, "check_invert");
980 gtk_button_set_focus_on_click(GTK_BUTTON(checkbox2), FALSE);
981 gtk_widget_set_tooltip_text(checkbox2,
982 _("Invert the sense of matching, to select non-matching lines"));
984 lbox = gtk_vbox_new(FALSE, 0);
985 gtk_container_add(GTK_CONTAINER(lbox), check_regexp);
986 gtk_container_add(GTK_CONTAINER(lbox), checkbox2);
987 gtk_container_add(GTK_CONTAINER(lbox), check_recursive);
989 rbox = gtk_vbox_new(FALSE, 0);
990 gtk_container_add(GTK_CONTAINER(rbox), checkbox1);
991 gtk_container_add(GTK_CONTAINER(rbox), check_wholeword);
992 gtk_container_add(GTK_CONTAINER(rbox), gtk_label_new(NULL));
994 hbox = gtk_hbox_new(FALSE, 6);
995 gtk_container_add(GTK_CONTAINER(hbox), lbox);
996 gtk_container_add(GTK_CONTAINER(hbox), rbox);
997 gtk_container_add(GTK_CONTAINER(vbox), hbox);
999 check_extra = gtk_check_button_new_with_mnemonic(_("E_xtra options:"));
1000 ui_hookup_widget(fif_dlg.dialog, check_extra, "check_extra");
1001 gtk_button_set_focus_on_click(GTK_BUTTON(check_extra), FALSE);
1003 entry_extra = gtk_entry_new();
1004 ui_entry_add_clear_icon(GTK_ENTRY(entry_extra));
1005 gtk_entry_set_activates_default(GTK_ENTRY(entry_extra), TRUE);
1006 gtk_widget_set_sensitive(entry_extra, FALSE);
1007 gtk_widget_set_tooltip_text(entry_extra, _("Other options to pass to Grep"));
1008 ui_hookup_widget(fif_dlg.dialog, entry_extra, "entry_extra");
1010 /* enable entry_extra when check_extra is checked */
1011 g_signal_connect(check_extra, "toggled",
1012 G_CALLBACK(on_widget_toggled_set_sensitive), entry_extra);
1014 hbox = gtk_hbox_new(FALSE, 6);
1015 gtk_box_pack_start(GTK_BOX(hbox), check_extra, FALSE, FALSE, 0);
1016 gtk_box_pack_start(GTK_BOX(hbox), entry_extra, TRUE, TRUE, 0);
1017 gtk_container_add(GTK_CONTAINER(vbox), hbox);
1019 g_signal_connect(fif_dlg.dialog, "response",
1020 G_CALLBACK(on_find_in_files_dialog_response), NULL);
1021 g_signal_connect(fif_dlg.dialog, "delete-event",
1022 G_CALLBACK(gtk_widget_hide_on_delete), NULL);
1027 * Shows the Find in Files dialog.
1029 * @param dir The directory to search in (UTF-8 encoding). May be @c NULL
1030 * to determine it the usual way by using the current document's path.
1032 * @since 0.14, plugin API 53
1034 GEANY_API_SYMBOL
1035 void search_show_find_in_files_dialog(const gchar *dir)
1037 search_show_find_in_files_dialog_full(NULL, dir);
1041 void search_show_find_in_files_dialog_full(const gchar *text, const gchar *dir)
1043 GtkWidget *entry; /* for child GtkEntry of a GtkComboBoxEntry */
1044 GeanyDocument *doc = document_get_current();
1045 gchar *sel = NULL;
1046 gchar *cur_dir = NULL;
1047 GeanyEncodingIndex enc_idx = GEANY_ENCODING_UTF_8;
1049 if (fif_dlg.dialog == NULL)
1051 create_fif_dialog();
1052 gtk_widget_show_all(fif_dlg.dialog);
1053 if (doc && !text)
1054 sel = editor_get_default_selection(doc->editor, search_prefs.use_current_word, NULL);
1056 stash_group_display(fif_prefs, fif_dlg.dialog);
1058 if (!text)
1060 /* only set selection if the dialog is not already visible, or has just been created */
1061 if (doc && ! sel && ! gtk_widget_get_visible(fif_dlg.dialog))
1062 sel = editor_get_default_selection(doc->editor, search_prefs.use_current_word, NULL);
1064 text = sel;
1066 entry = gtk_bin_get_child(GTK_BIN(fif_dlg.search_combo));
1067 if (text)
1068 gtk_entry_set_text(GTK_ENTRY(entry), text);
1069 g_free(sel);
1071 /* add project's base path directory to the dir list, we do this here once
1072 * (in create_fif_dialog() it would fail if a project is opened after dialog creation) */
1073 if (app->project != NULL && !EMPTY(app->project->base_path))
1075 ui_combo_box_prepend_text_once(GTK_COMBO_BOX_TEXT(fif_dlg.dir_combo),
1076 app->project->base_path);
1079 entry = gtk_bin_get_child(GTK_BIN(fif_dlg.dir_combo));
1080 if (!EMPTY(dir))
1081 cur_dir = g_strdup(dir); /* custom directory argument passed */
1082 else
1084 if (search_prefs.use_current_file_dir)
1086 static gchar *last_cur_dir = NULL;
1087 static GeanyDocument *last_doc = NULL;
1089 /* Only set the directory entry once for the current document */
1090 cur_dir = utils_get_current_file_dir_utf8();
1091 if (doc == last_doc && cur_dir && utils_str_equal(cur_dir, last_cur_dir))
1093 /* in case the user now wants the current directory, add it to history */
1094 ui_combo_box_add_to_history(GTK_COMBO_BOX_TEXT(fif_dlg.dir_combo), cur_dir, 0);
1095 SETPTR(cur_dir, NULL);
1097 else
1098 SETPTR(last_cur_dir, g_strdup(cur_dir));
1100 last_doc = doc;
1102 if (!cur_dir && EMPTY(gtk_entry_get_text(GTK_ENTRY(entry))))
1104 /* use default_open_path if no directory could be determined
1105 * (e.g. when no files are open) */
1106 if (!cur_dir)
1107 cur_dir = g_strdup(utils_get_default_dir_utf8());
1108 if (!cur_dir)
1109 cur_dir = g_get_current_dir();
1112 if (cur_dir)
1114 gtk_entry_set_text(GTK_ENTRY(entry), cur_dir);
1115 g_free(cur_dir);
1118 update_fif_file_mode_combo();
1119 update_file_patterns(fif_dlg.files_mode_combo, fif_dlg.files_combo);
1121 /* set the encoding of the current file */
1122 if (doc != NULL)
1123 enc_idx = encodings_get_idx_from_charset(doc->encoding);
1124 ui_encodings_combo_box_set_active_encoding(GTK_COMBO_BOX(fif_dlg.encoding_combo), enc_idx);
1126 /* put the focus to the directory entry if it is empty */
1127 if (utils_str_equal(gtk_entry_get_text(GTK_ENTRY(entry)), ""))
1128 gtk_widget_grab_focus(fif_dlg.dir_combo);
1129 else
1130 gtk_widget_grab_focus(fif_dlg.search_combo);
1132 /* set dialog window position */
1133 set_dialog_position(fif_dlg.dialog, fif_dlg.position);
1135 gtk_widget_show(fif_dlg.dialog);
1136 /* bring the dialog back in the foreground in case it is already open but the focus is away */
1137 gtk_window_present(GTK_WINDOW(fif_dlg.dialog));
1141 static void
1142 on_find_replace_checkbutton_toggled(GtkToggleButton *togglebutton, gpointer user_data)
1144 GtkWidget *dialog = GTK_WIDGET(user_data);
1145 GtkToggleButton *chk_regexp = GTK_TOGGLE_BUTTON(
1146 ui_lookup_widget(dialog, "check_regexp"));
1148 if (togglebutton == chk_regexp)
1150 gboolean regex_set = gtk_toggle_button_get_active(chk_regexp);
1151 GtkWidget *check_word = ui_lookup_widget(dialog, "check_word");
1152 GtkWidget *check_wordstart = ui_lookup_widget(dialog, "check_wordstart");
1153 GtkWidget *check_escape = ui_lookup_widget(dialog, "check_escape");
1154 GtkWidget *check_multiline = ui_lookup_widget(dialog, "check_multiline");
1155 gboolean replace = (dialog != find_dlg.dialog);
1156 const char *back_button[2] = { "btn_previous" , "check_back" };
1158 /* hide options that don't apply to regex searches */
1159 gtk_widget_set_sensitive(check_escape, ! regex_set);
1160 gtk_widget_set_sensitive(ui_lookup_widget(dialog, back_button[replace]), ! regex_set);
1161 gtk_widget_set_sensitive(check_word, ! regex_set);
1162 gtk_widget_set_sensitive(check_wordstart, ! regex_set);
1163 gtk_widget_set_sensitive(check_multiline, regex_set);
1168 static GeanyMatchInfo *match_info_new(GeanyFindFlags flags, gint start, gint end)
1170 GeanyMatchInfo *info = g_slice_alloc(sizeof *info);
1172 info->flags = flags;
1173 info->start = start;
1174 info->end = end;
1175 info->match_text = NULL;
1177 return info;
1180 void geany_match_info_free(GeanyMatchInfo *info)
1182 g_free(info->match_text);
1183 g_slice_free1(sizeof *info, info);
1187 /* find all in the given range.
1188 * Returns a list of allocated GeanyMatchInfo, should be freed using:
1190 * foreach_slist(node, matches)
1191 * geany_match_info_free(node->data);
1192 * g_slist_free(matches); */
1193 static GSList *find_range(ScintillaObject *sci, GeanyFindFlags flags, struct Sci_TextToFind *ttf)
1195 GSList *matches = NULL;
1196 GeanyMatchInfo *info;
1198 g_return_val_if_fail(sci != NULL && ttf->lpstrText != NULL, NULL);
1199 if (! *ttf->lpstrText)
1200 return NULL;
1202 while (search_find_text(sci, flags, ttf, &info) != -1)
1204 if (ttf->chrgText.cpMax > ttf->chrg.cpMax)
1206 /* found text is partially out of range */
1207 geany_match_info_free(info);
1208 break;
1211 matches = g_slist_prepend(matches, info);
1212 ttf->chrg.cpMin = ttf->chrgText.cpMax;
1214 /* avoid rematching with empty matches like "(?=[a-z])" or "^$".
1215 * note we cannot assume a match will always be empty or not and then break out, since
1216 * matches like "a?(?=b)" will sometimes be empty and sometimes not */
1217 if (ttf->chrgText.cpMax == ttf->chrgText.cpMin)
1218 ttf->chrg.cpMin ++;
1221 return g_slist_reverse(matches);
1225 /* Clears markers if text is null/empty.
1226 * @return Number of matches marked. */
1227 gint search_mark_all(GeanyDocument *doc, const gchar *search_text, GeanyFindFlags flags)
1229 gint count = 0;
1230 struct Sci_TextToFind ttf;
1231 GSList *match, *matches;
1233 g_return_val_if_fail(DOC_VALID(doc), 0);
1235 /* clear previous search indicators */
1236 editor_indicator_clear(doc->editor, GEANY_INDICATOR_SEARCH);
1238 if (G_UNLIKELY(EMPTY(search_text)))
1239 return 0;
1241 ttf.chrg.cpMin = 0;
1242 ttf.chrg.cpMax = sci_get_length(doc->editor->sci);
1243 ttf.lpstrText = (gchar *)search_text;
1245 matches = find_range(doc->editor->sci, flags, &ttf);
1246 foreach_slist (match, matches)
1248 GeanyMatchInfo *info = match->data;
1250 if (info->end != info->start)
1251 editor_indicator_set_on_range(doc->editor, GEANY_INDICATOR_SEARCH, info->start, info->end);
1252 count++;
1254 geany_match_info_free(info);
1256 g_slist_free(matches);
1258 return count;
1262 static void
1263 on_find_entry_activate(GtkEntry *entry, gpointer user_data)
1265 on_find_dialog_response(NULL, GEANY_RESPONSE_FIND, user_data);
1269 static void
1270 on_find_entry_activate_backward(GtkEntry *entry, gpointer user_data)
1272 /* can't search backwards with a regexp */
1273 if (search_data.flags & GEANY_FIND_REGEXP)
1274 utils_beep();
1275 else
1276 on_find_dialog_response(NULL, GEANY_RESPONSE_FIND_PREVIOUS, user_data);
1280 static GeanyFindFlags int_search_flags(gint match_case, gint whole_word, gint regexp, gint multiline, gint word_start)
1282 return (match_case ? GEANY_FIND_MATCHCASE : 0) |
1283 (regexp ? GEANY_FIND_REGEXP : 0) |
1284 (whole_word ? GEANY_FIND_WHOLEWORD : 0) |
1285 (multiline ? GEANY_FIND_MULTILINE : 0) |
1286 /* SCFIND_WORDSTART overrides SCFIND_WHOLEWORD, but we want the opposite */
1287 (word_start && !whole_word ? GEANY_FIND_WORDSTART : 0);
1291 static void
1292 on_find_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
1294 gtk_window_get_position(GTK_WINDOW(find_dlg.dialog),
1295 &find_dlg.position[0], &find_dlg.position[1]);
1297 stash_group_update(find_prefs, find_dlg.dialog);
1299 if (response == GTK_RESPONSE_CANCEL || response == GTK_RESPONSE_DELETE_EVENT)
1300 gtk_widget_hide(find_dlg.dialog);
1301 else
1303 GeanyDocument *doc = document_get_current();
1304 gboolean check_close = settings.find_close_dialog;
1306 if (doc == NULL)
1307 return;
1309 search_data.backwards = FALSE;
1310 search_data.search_bar = FALSE;
1312 g_free(search_data.text);
1313 g_free(search_data.original_text);
1314 search_data.text = g_strdup(gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(user_data)))));
1315 search_data.original_text = g_strdup(search_data.text);
1316 search_data.flags = int_search_flags(settings.find_case_sensitive,
1317 settings.find_match_whole_word, settings.find_regexp, settings.find_regexp_multiline,
1318 settings.find_match_word_start);
1320 if (EMPTY(search_data.text))
1322 fail:
1323 utils_beep();
1324 gtk_widget_grab_focus(find_dlg.entry);
1325 return;
1327 if (search_data.flags & GEANY_FIND_REGEXP)
1329 GRegex *regex = compile_regex(search_data.text, search_data.flags);
1330 if (!regex)
1331 goto fail;
1332 else
1333 g_regex_unref(regex);
1335 else if (settings.find_escape_sequences)
1337 if (! utils_str_replace_escape(search_data.text, FALSE))
1338 goto fail;
1340 ui_combo_box_add_to_history(GTK_COMBO_BOX_TEXT(user_data), search_data.original_text, 0);
1342 switch (response)
1344 case GEANY_RESPONSE_FIND:
1345 case GEANY_RESPONSE_FIND_PREVIOUS:
1347 gint result = document_find_text(doc, search_data.text, search_data.original_text, search_data.flags,
1348 (response == GEANY_RESPONSE_FIND_PREVIOUS), NULL, TRUE, GTK_WIDGET(find_dlg.dialog));
1349 ui_set_search_entry_background(find_dlg.entry, (result > -1));
1350 check_close = search_prefs.hide_find_dialog;
1351 break;
1353 case GEANY_RESPONSE_FIND_IN_FILE:
1354 search_find_usage(search_data.text, search_data.original_text, search_data.flags, FALSE);
1355 break;
1357 case GEANY_RESPONSE_FIND_IN_SESSION:
1358 search_find_usage(search_data.text, search_data.original_text, search_data.flags, TRUE);
1359 break;
1361 case GEANY_RESPONSE_MARK:
1363 gint count = search_mark_all(doc, search_data.text, search_data.flags);
1365 if (count == 0)
1366 ui_set_statusbar(FALSE, _("No matches found for \"%s\"."), search_data.original_text);
1367 else
1368 ui_set_statusbar(FALSE,
1369 ngettext("Found %d match for \"%s\".",
1370 "Found %d matches for \"%s\".", count),
1371 count, search_data.original_text);
1373 break;
1375 if (check_close)
1376 gtk_widget_hide(find_dlg.dialog);
1381 static void
1382 on_replace_find_entry_activate(GtkEntry *entry, gpointer user_data)
1384 on_replace_dialog_response(NULL, GEANY_RESPONSE_FIND, NULL);
1388 static void
1389 on_replace_entry_activate(GtkEntry *entry, gpointer user_data)
1391 on_replace_dialog_response(NULL,
1392 search_prefs.replace_and_find_by_default ? GEANY_RESPONSE_REPLACE_AND_FIND : GEANY_RESPONSE_REPLACE,
1393 NULL);
1397 static void replace_in_session(GeanyDocument *doc,
1398 GeanyFindFlags search_flags_re, gboolean search_replace_escape_re,
1399 const gchar *find, const gchar *replace,
1400 const gchar *original_find, const gchar *original_replace)
1402 guint n, page_count, rep_count = 0, file_count = 0;
1404 /* replace in all documents following notebook tab order */
1405 page_count = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
1406 for (n = 0; n < page_count; n++)
1408 GeanyDocument *tmp_doc = document_get_from_page(n);
1409 gint reps = 0;
1411 reps = document_replace_all(tmp_doc, find, replace, original_find, original_replace, search_flags_re);
1412 rep_count += reps;
1413 if (reps)
1414 file_count++;
1416 if (file_count == 0)
1418 utils_beep();
1419 ui_set_statusbar(FALSE, _("No matches found for \"%s\"."), original_find);
1420 return;
1422 /* if only one file was changed, don't override that document's status message
1423 * so we don't have to translate 4 messages for ngettext */
1424 if (file_count > 1)
1425 ui_set_statusbar(FALSE, _("Replaced %u matches in %u documents."),
1426 rep_count, file_count);
1428 /* show which docs had replacements: */
1429 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_STATUS);
1431 ui_save_buttons_toggle(doc->changed); /* update save all */
1435 static void
1436 on_replace_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
1438 GeanyDocument *doc = document_get_current();
1439 GeanyFindFlags search_flags_re;
1440 gboolean search_backwards_re, search_replace_escape_re;
1441 gchar *find, *replace, *original_find = NULL, *original_replace = NULL;
1443 gtk_window_get_position(GTK_WINDOW(replace_dlg.dialog),
1444 &replace_dlg.position[0], &replace_dlg.position[1]);
1446 stash_group_update(replace_prefs, replace_dlg.dialog);
1448 if (response == GTK_RESPONSE_CANCEL || response == GTK_RESPONSE_DELETE_EVENT)
1450 gtk_widget_hide(replace_dlg.dialog);
1451 return;
1454 search_backwards_re = settings.replace_search_backwards;
1455 search_replace_escape_re = settings.replace_escape_sequences;
1456 find = g_strdup(gtk_entry_get_text(GTK_ENTRY(replace_dlg.find_entry)));
1457 replace = g_strdup(gtk_entry_get_text(GTK_ENTRY(replace_dlg.replace_entry)));
1459 search_flags_re = int_search_flags(settings.replace_case_sensitive,
1460 settings.replace_match_whole_word, settings.replace_regexp,
1461 settings.replace_regexp_multiline, settings.replace_match_word_start);
1463 if ((response != GEANY_RESPONSE_FIND) && (search_flags_re & GEANY_FIND_MATCHCASE)
1464 && (strcmp(find, replace) == 0))
1465 goto fail;
1467 original_find = g_strdup(find);
1468 original_replace = g_strdup(replace);
1470 if (search_flags_re & GEANY_FIND_REGEXP)
1472 GRegex *regex = compile_regex(find, search_flags_re);
1473 if (regex)
1474 g_regex_unref(regex);
1475 /* find escapes will be handled by GRegex */
1476 if (!regex || !utils_str_replace_escape(replace, TRUE))
1477 goto fail;
1479 else if (search_replace_escape_re)
1481 if (! utils_str_replace_escape(find, FALSE) ||
1482 ! utils_str_replace_escape(replace, FALSE))
1483 goto fail;
1486 ui_combo_box_add_to_history(GTK_COMBO_BOX_TEXT(
1487 gtk_widget_get_parent(replace_dlg.find_entry)), original_find, 0);
1488 ui_combo_box_add_to_history(GTK_COMBO_BOX_TEXT(
1489 gtk_widget_get_parent(replace_dlg.replace_entry)), original_replace, 0);
1491 switch (response)
1493 case GEANY_RESPONSE_REPLACE_AND_FIND:
1495 gint rep = document_replace_text(doc, find, original_find, replace, search_flags_re,
1496 search_backwards_re);
1497 if (rep != -1)
1498 document_find_text(doc, find, original_find, search_flags_re, search_backwards_re,
1499 NULL, TRUE, NULL);
1500 break;
1502 case GEANY_RESPONSE_REPLACE:
1503 document_replace_text(doc, find, original_find, replace, search_flags_re,
1504 search_backwards_re);
1505 break;
1507 case GEANY_RESPONSE_FIND:
1509 gint result = document_find_text(doc, find, original_find, search_flags_re,
1510 search_backwards_re, NULL, TRUE, GTK_WIDGET(dialog));
1511 ui_set_search_entry_background(replace_dlg.find_entry, (result > -1));
1512 break;
1514 case GEANY_RESPONSE_REPLACE_IN_FILE:
1515 if (! document_replace_all(doc, find, replace, original_find, original_replace, search_flags_re))
1516 utils_beep();
1517 break;
1519 case GEANY_RESPONSE_REPLACE_IN_SESSION:
1520 replace_in_session(doc, search_flags_re, search_replace_escape_re, find, replace, original_find, original_replace);
1521 break;
1523 case GEANY_RESPONSE_REPLACE_IN_SEL:
1524 document_replace_sel(doc, find, replace, original_find, original_replace, search_flags_re);
1525 break;
1527 switch (response)
1529 case GEANY_RESPONSE_REPLACE_IN_SEL:
1530 case GEANY_RESPONSE_REPLACE_IN_FILE:
1531 case GEANY_RESPONSE_REPLACE_IN_SESSION:
1532 if (settings.replace_close_dialog)
1533 gtk_widget_hide(replace_dlg.dialog);
1535 g_free(find);
1536 g_free(replace);
1537 g_free(original_find);
1538 g_free(original_replace);
1539 return;
1541 fail:
1542 utils_beep();
1543 gtk_widget_grab_focus(replace_dlg.find_entry);
1544 g_free(find);
1545 g_free(replace);
1546 g_free(original_find);
1547 g_free(original_replace);
1551 static GString *get_grep_options(void)
1553 GString *gstr = g_string_new("-nHI"); /* line numbers, filenames, ignore binaries */
1555 if (settings.fif_invert_results)
1556 g_string_append_c(gstr, 'v');
1557 if (! settings.fif_case_sensitive)
1558 g_string_append_c(gstr, 'i');
1559 if (settings.fif_match_whole_word)
1560 g_string_append_c(gstr, 'w');
1561 if (settings.fif_recursive)
1562 g_string_append_c(gstr, 'r');
1564 if (!settings.fif_regexp)
1565 g_string_append_c(gstr, 'F');
1566 else
1567 g_string_append_c(gstr, 'E');
1569 if (settings.fif_use_extra_options)
1571 g_strstrip(settings.fif_extra_options);
1573 if (*settings.fif_extra_options != 0)
1575 g_string_append_c(gstr, ' ');
1576 g_string_append(gstr, settings.fif_extra_options);
1579 g_strstrip(settings.fif_files);
1580 if (settings.fif_files_mode != FILES_MODE_ALL && *settings.fif_files)
1582 GString *tmp;
1584 /* put --include= before each pattern */
1585 tmp = g_string_new(settings.fif_files);
1586 do {} while (utils_string_replace_all(tmp, " ", " "));
1587 g_string_prepend_c(tmp, ' ');
1588 utils_string_replace_all(tmp, " ", " --include=");
1589 g_string_append(gstr, tmp->str);
1590 g_string_free(tmp, TRUE);
1592 return gstr;
1596 static void
1597 on_find_in_files_dialog_response(GtkDialog *dialog, gint response,
1598 G_GNUC_UNUSED gpointer user_data)
1600 gtk_window_get_position(GTK_WINDOW(fif_dlg.dialog), &fif_dlg.position[0], &fif_dlg.position[1]);
1602 stash_group_update(fif_prefs, fif_dlg.dialog);
1604 if (response == GTK_RESPONSE_ACCEPT)
1606 GtkWidget *search_combo = fif_dlg.search_combo;
1607 const gchar *search_text =
1608 gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(search_combo))));
1609 GtkWidget *dir_combo = fif_dlg.dir_combo;
1610 const gchar *utf8_dir =
1611 gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(dir_combo))));
1612 GeanyEncodingIndex enc_idx =
1613 ui_encodings_combo_box_get_active_encoding(GTK_COMBO_BOX(fif_dlg.encoding_combo));
1615 if (G_UNLIKELY(EMPTY(utf8_dir)))
1616 ui_set_statusbar(FALSE, _("Invalid directory for find in files."));
1617 else if (!EMPTY(search_text))
1619 GString *opts = get_grep_options();
1620 const gchar *enc = (enc_idx == GEANY_ENCODING_UTF_8) ? NULL :
1621 encodings_get_charset_from_index(enc_idx);
1623 if (search_find_in_files(search_text, utf8_dir, opts->str, enc))
1625 ui_combo_box_add_to_history(GTK_COMBO_BOX_TEXT(search_combo), search_text, 0);
1626 ui_combo_box_add_to_history(GTK_COMBO_BOX_TEXT(fif_dlg.files_combo), NULL, 0);
1627 ui_combo_box_add_to_history(GTK_COMBO_BOX_TEXT(dir_combo), utf8_dir, 0);
1628 gtk_widget_hide(fif_dlg.dialog);
1630 g_string_free(opts, TRUE);
1632 else
1633 ui_set_statusbar(FALSE, _("No text to find."));
1635 else
1636 gtk_widget_hide(fif_dlg.dialog);
1640 static gboolean
1641 search_find_in_files(const gchar *utf8_search_text, const gchar *utf8_dir, const gchar *opts,
1642 const gchar *enc)
1644 gchar **argv_prefix, **argv;
1645 gchar *command_grep;
1646 gchar *command_line, *dir;
1647 gchar *search_text = NULL;
1648 GError *error = NULL;
1649 gboolean ret = FALSE;
1650 gssize utf8_text_len;
1652 if (EMPTY(utf8_search_text) || ! utf8_dir) return TRUE;
1654 command_grep = g_find_program_in_path(tool_prefs.grep_cmd);
1655 if (command_grep == NULL)
1656 command_line = g_strdup_printf("%s %s --", tool_prefs.grep_cmd, opts);
1657 else
1659 command_line = g_strdup_printf("\"%s\" %s --", command_grep, opts);
1660 g_free(command_grep);
1663 /* convert the search text in the preferred encoding (if the text is not valid UTF-8. assume
1664 * it is already in the preferred encoding) */
1665 utf8_text_len = strlen(utf8_search_text);
1666 if (enc != NULL && g_utf8_validate(utf8_search_text, utf8_text_len, NULL))
1668 search_text = g_convert(utf8_search_text, utf8_text_len, enc, "UTF-8", NULL, NULL, NULL);
1670 if (search_text == NULL)
1671 search_text = g_strdup(utf8_search_text);
1673 argv_prefix = g_new(gchar*, 3);
1674 argv_prefix[0] = search_text;
1675 dir = utils_get_locale_from_utf8(utf8_dir);
1677 /* finally add the arguments(files to be searched) */
1678 if (settings.fif_recursive) /* recursive option set */
1680 /* Use '.' so we get relative paths in the output */
1681 argv_prefix[1] = g_strdup(".");
1682 argv_prefix[2] = NULL;
1683 argv = argv_prefix;
1685 else
1687 argv_prefix[1] = NULL;
1688 argv = search_get_argv((const gchar**)argv_prefix, dir);
1689 g_strfreev(argv_prefix);
1691 if (argv == NULL) /* no files */
1693 g_free(command_line);
1694 return FALSE;
1698 gtk_list_store_clear(msgwindow.store_msg);
1699 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_MESSAGE);
1701 /* we can pass 'enc' without strdup'ing it here because it's a global const string and
1702 * always exits longer than the lifetime of this function */
1703 if (spawn_with_callbacks(dir, command_line, argv, NULL, 0, NULL, NULL, search_read_io,
1704 (gpointer) enc, 0, search_read_io_stderr, (gpointer) enc, 0, search_finished, NULL,
1705 NULL, &error))
1707 gchar *utf8_str;
1709 ui_progress_bar_start(_("Searching..."));
1710 msgwin_set_messages_dir(dir);
1711 utf8_str = g_strdup_printf(_("%s %s -- %s (in directory: %s)"),
1712 tool_prefs.grep_cmd, opts, utf8_search_text, utf8_dir);
1713 msgwin_msg_add_string(COLOR_BLUE, -1, NULL, utf8_str);
1714 g_free(utf8_str);
1715 ret = TRUE;
1717 else
1719 ui_set_statusbar(TRUE, _("Cannot execute grep tool \"%s\": %s. "
1720 "Check the path setting in Preferences."), tool_prefs.grep_cmd, error->message);
1721 g_error_free(error);
1724 utils_free_pointers(2, dir, command_line, NULL);
1725 g_strfreev(argv);
1726 return ret;
1730 static gboolean pattern_list_match(GSList *patterns, const gchar *str)
1732 GSList *item;
1734 foreach_slist(item, patterns)
1736 if (g_pattern_match_string(item->data, str))
1737 return TRUE;
1739 return FALSE;
1743 /* Creates an argument vector of strings, copying argv_prefix[] values for
1744 * the first arguments, then followed by filenames found in dir.
1745 * Returns NULL if no files were found, otherwise returned vector should be fully freed. */
1746 static gchar **search_get_argv(const gchar **argv_prefix, const gchar *dir)
1748 guint prefix_len, list_len, i, j;
1749 gchar **argv;
1750 GSList *list, *item, *patterns = NULL;
1751 GError *error = NULL;
1753 g_return_val_if_fail(dir != NULL, NULL);
1755 prefix_len = g_strv_length((gchar**)argv_prefix);
1756 list = utils_get_file_list(dir, &list_len, &error);
1757 if (error)
1759 ui_set_statusbar(TRUE, _("Could not open directory (%s)"), error->message);
1760 g_error_free(error);
1761 return NULL;
1763 if (list == NULL)
1764 return NULL;
1766 argv = g_new(gchar*, prefix_len + list_len + 1);
1768 for (i = 0, j = 0; i < prefix_len; i++)
1770 if (g_str_has_prefix(argv_prefix[i], "--include="))
1772 const gchar *pat = &(argv_prefix[i][10]); /* the pattern part of the argument */
1774 patterns = g_slist_prepend(patterns, g_pattern_spec_new(pat));
1776 else
1777 argv[j++] = g_strdup(argv_prefix[i]);
1780 if (patterns)
1782 GSList *pat;
1784 foreach_slist(item, list)
1786 if (pattern_list_match(patterns, item->data))
1787 argv[j++] = item->data;
1788 else
1789 g_free(item->data);
1791 foreach_slist(pat, patterns)
1792 g_pattern_spec_free(pat->data);
1793 g_slist_free(patterns);
1795 else
1797 foreach_slist(item, list)
1798 argv[j++] = item->data;
1801 argv[j] = NULL;
1802 g_slist_free(list);
1803 return argv;
1807 static void read_fif_io(gchar *msg, GIOCondition condition, gchar *enc, gint msg_color)
1809 if (condition & (G_IO_IN | G_IO_PRI))
1811 gchar *utf8_msg = NULL;
1813 g_strstrip(msg);
1814 /* enc is NULL when encoding is set to UTF-8, so we can skip any conversion */
1815 if (enc != NULL)
1817 if (! g_utf8_validate(msg, -1, NULL))
1819 utf8_msg = g_convert(msg, -1, "UTF-8", enc, NULL, NULL, NULL);
1821 if (utf8_msg == NULL)
1822 utf8_msg = msg;
1824 else
1825 utf8_msg = msg;
1827 msgwin_msg_add_string(msg_color, -1, NULL, utf8_msg);
1829 if (utf8_msg != msg)
1830 g_free(utf8_msg);
1835 static void search_read_io(GString *string, GIOCondition condition, gpointer data)
1837 read_fif_io(string->str, condition, data, COLOR_BLACK);
1841 static void search_read_io_stderr(GString *string, GIOCondition condition, gpointer data)
1843 read_fif_io(string->str, condition, data, COLOR_DARK_RED);
1847 static void search_finished(GPid child_pid, gint status, gpointer user_data)
1849 const gchar *msg = _("Search failed.");
1850 gint exit_status;
1852 if (SPAWN_WIFEXITED(status))
1854 exit_status = SPAWN_WEXITSTATUS(status);
1856 else if (SPAWN_WIFSIGNALED(status))
1858 exit_status = -1;
1859 g_warning("Find in Files: The command failed unexpectedly (signal received).");
1861 else
1863 exit_status = 1;
1866 switch (exit_status)
1868 case 0:
1870 gint count = gtk_tree_model_iter_n_children(
1871 GTK_TREE_MODEL(msgwindow.store_msg), NULL) - 1;
1872 gchar *text = ngettext(
1873 "Search completed with %d match.",
1874 "Search completed with %d matches.", count);
1876 msgwin_msg_add(COLOR_BLUE, -1, NULL, text, count);
1877 ui_set_statusbar(FALSE, text, count);
1878 break;
1880 case 1:
1881 msg = _("No matches found.");
1882 default:
1883 msgwin_msg_add_string(COLOR_BLUE, -1, NULL, msg);
1884 ui_set_statusbar(FALSE, "%s", msg);
1885 break;
1887 utils_beep();
1888 ui_progress_bar_stop();
1892 static GRegex *compile_regex(const gchar *str, GeanyFindFlags sflags)
1894 GRegex *regex;
1895 GError *error = NULL;
1896 gint rflags = 0;
1898 if (sflags & GEANY_FIND_MULTILINE)
1899 rflags |= G_REGEX_MULTILINE;
1900 if (~sflags & GEANY_FIND_MATCHCASE)
1901 rflags |= G_REGEX_CASELESS;
1902 if (sflags & (GEANY_FIND_WHOLEWORD | GEANY_FIND_WORDSTART))
1904 geany_debug("%s: Unsupported regex flags found!", G_STRFUNC);
1907 regex = g_regex_new(str, rflags, 0, &error);
1908 if (!regex)
1910 ui_set_statusbar(FALSE, _("Bad regex: %s"), error->message);
1911 g_error_free(error);
1913 return regex;
1917 /* groups that don't exist are handled OK as len = end - start = (-1) - (-1) = 0 */
1918 static gchar *get_regex_match_string(const gchar *text, const GeanyMatchInfo *match, guint nth)
1920 const gint start = match->matches[nth].start;
1921 const gint end = match->matches[nth].end;
1922 return g_strndup(&text[start], end - start);
1926 static gint find_regex(ScintillaObject *sci, guint pos, GRegex *regex, gboolean multiline, GeanyMatchInfo *match)
1928 const gchar *text;
1929 GMatchInfo *minfo;
1930 guint document_length;
1931 gint ret = -1;
1932 gint offset = 0;
1934 document_length = (guint)sci_get_length(sci);
1935 if (document_length <= 0)
1936 return -1; /* skip empty documents */
1938 g_return_val_if_fail(pos <= document_length, -1);
1940 if (multiline)
1942 /* Warning: any SCI calls will invalidate 'text' after calling SCI_GETCHARACTERPOINTER */
1943 text = (void*)scintilla_send_message(sci, SCI_GETCHARACTERPOINTER, 0, 0);
1944 g_regex_match_full(regex, text, -1, pos, 0, &minfo, NULL);
1946 else /* single-line mode, manually match against each line */
1948 gint line = sci_get_line_from_position(sci, pos);
1950 for (;;)
1952 gint start = sci_get_position_from_line(sci, line);
1953 gint end = sci_get_line_end_position(sci, line);
1955 text = (void*)scintilla_send_message(sci, SCI_GETRANGEPOINTER, start, end - start);
1956 if (g_regex_match_full(regex, text, end - start, pos - start, 0, &minfo, NULL))
1958 offset = start;
1959 break;
1961 else /* not found, try next line */
1963 line ++;
1964 if (line >= sci_get_line_count(sci))
1965 break;
1966 pos = sci_get_position_from_line(sci, line);
1967 /* don't free last info, it's freed below */
1968 g_match_info_free(minfo);
1973 /* Warning: minfo will become invalid when 'text' does! */
1974 if (g_match_info_matches(minfo))
1976 guint i;
1978 /* copy whole match text and offsets before they become invalid */
1979 SETPTR(match->match_text, g_match_info_fetch(minfo, 0));
1981 foreach_range(i, G_N_ELEMENTS(match->matches))
1983 gint start = -1, end = -1;
1985 g_match_info_fetch_pos(minfo, (gint)i, &start, &end);
1986 match->matches[i].start = offset + start;
1987 match->matches[i].end = offset + end;
1989 match->start = match->matches[0].start;
1990 match->end = match->matches[0].end;
1991 ret = match->start;
1993 g_match_info_free(minfo);
1994 return ret;
1998 static gint geany_find_flags_to_sci_flags(GeanyFindFlags flags)
2000 g_warn_if_fail(! (flags & GEANY_FIND_REGEXP) || ! (flags & GEANY_FIND_MULTILINE));
2002 return ((flags & GEANY_FIND_MATCHCASE) ? SCFIND_MATCHCASE : 0) |
2003 ((flags & GEANY_FIND_WHOLEWORD) ? SCFIND_WHOLEWORD : 0) |
2004 ((flags & GEANY_FIND_REGEXP) ? SCFIND_REGEXP | SCFIND_POSIX : 0) |
2005 ((flags & GEANY_FIND_WORDSTART) ? SCFIND_WORDSTART : 0);
2009 gint search_find_prev(ScintillaObject *sci, const gchar *str, GeanyFindFlags flags, GeanyMatchInfo **match_)
2011 gint ret;
2013 g_return_val_if_fail(! (flags & GEANY_FIND_REGEXP), -1);
2015 ret = sci_search_prev(sci, geany_find_flags_to_sci_flags(flags), str);
2016 if (ret != -1 && match_)
2017 *match_ = match_info_new(flags, ret, ret + strlen(str));
2018 return ret;
2022 gint search_find_next(ScintillaObject *sci, const gchar *str, GeanyFindFlags flags, GeanyMatchInfo **match_)
2024 GeanyMatchInfo *match;
2025 GRegex *regex;
2026 gint ret = -1;
2027 gint pos;
2029 if (~flags & GEANY_FIND_REGEXP)
2031 ret = sci_search_next(sci, geany_find_flags_to_sci_flags(flags), str);
2032 if (ret != -1 && match_)
2033 *match_ = match_info_new(flags, ret, ret + strlen(str));
2034 return ret;
2037 regex = compile_regex(str, flags);
2038 if (!regex)
2039 return -1;
2041 match = match_info_new(flags, 0, 0);
2043 pos = sci_get_current_position(sci);
2044 ret = find_regex(sci, pos, regex, flags & GEANY_FIND_MULTILINE, match);
2045 /* avoid re-matching the same position in case of empty matches */
2046 if (ret == pos && match->matches[0].start == match->matches[0].end)
2047 ret = find_regex(sci, pos + 1, regex, flags & GEANY_FIND_MULTILINE, match);
2048 if (ret >= 0)
2049 sci_set_selection(sci, match->start, match->end);
2051 if (ret != -1 && match_)
2052 *match_ = match;
2053 else
2054 geany_match_info_free(match);
2056 g_regex_unref(regex);
2057 return ret;
2061 gint search_replace_match(ScintillaObject *sci, const GeanyMatchInfo *match, const gchar *replace_text)
2063 GString *str;
2064 gint ret = 0;
2065 gint i = 0;
2067 sci_set_target_start(sci, match->start);
2068 sci_set_target_end(sci, match->end);
2070 if (! (match->flags & GEANY_FIND_REGEXP))
2071 return sci_replace_target(sci, replace_text, FALSE);
2073 str = g_string_new(replace_text);
2074 while (str->str[i])
2076 gchar *ptr = &str->str[i];
2077 gchar *grp;
2078 gchar c;
2080 if (ptr[0] != '\\')
2082 i++;
2083 continue;
2085 c = ptr[1];
2086 /* backslash or unnecessary escape */
2087 if (c == '\\' || !isdigit(c))
2089 g_string_erase(str, i, 1);
2090 i++;
2091 continue;
2093 /* digit escape */
2094 g_string_erase(str, i, 2);
2095 /* fix match offsets by subtracting index of whole match start from the string */
2096 grp = get_regex_match_string(match->match_text - match->matches[0].start, match, c - '0');
2097 g_string_insert(str, i, grp);
2098 i += strlen(grp);
2099 g_free(grp);
2101 ret = sci_replace_target(sci, str->str, FALSE);
2102 g_string_free(str, TRUE);
2103 return ret;
2107 gint search_find_text(ScintillaObject *sci, GeanyFindFlags flags, struct Sci_TextToFind *ttf, GeanyMatchInfo **match_)
2109 GeanyMatchInfo *match = NULL;
2110 GRegex *regex;
2111 gint ret;
2113 if (~flags & GEANY_FIND_REGEXP)
2115 ret = sci_find_text(sci, geany_find_flags_to_sci_flags(flags), ttf);
2116 if (ret != -1 && match_)
2117 *match_ = match_info_new(flags, ttf->chrgText.cpMin, ttf->chrgText.cpMax);
2118 return ret;
2121 regex = compile_regex(ttf->lpstrText, flags);
2122 if (!regex)
2123 return -1;
2125 match = match_info_new(flags, 0, 0);
2127 ret = find_regex(sci, ttf->chrg.cpMin, regex, flags & GEANY_FIND_MULTILINE, match);
2128 if (ret >= ttf->chrg.cpMax)
2129 ret = -1;
2130 else if (ret >= 0)
2132 ttf->chrgText.cpMin = match->start;
2133 ttf->chrgText.cpMax = match->end;
2136 if (ret != -1 && match_)
2137 *match_ = match;
2138 else
2139 geany_match_info_free(match);
2141 g_regex_unref(regex);
2142 return ret;
2146 static gint find_document_usage(GeanyDocument *doc, const gchar *search_text, GeanyFindFlags flags)
2148 gchar *buffer, *short_file_name;
2149 struct Sci_TextToFind ttf;
2150 gint count = 0;
2151 gint prev_line = -1;
2152 GSList *match, *matches;
2154 g_return_val_if_fail(DOC_VALID(doc), 0);
2156 short_file_name = g_path_get_basename(DOC_FILENAME(doc));
2158 ttf.chrg.cpMin = 0;
2159 ttf.chrg.cpMax = sci_get_length(doc->editor->sci);
2160 ttf.lpstrText = (gchar *)search_text;
2162 matches = find_range(doc->editor->sci, flags, &ttf);
2163 foreach_slist (match, matches)
2165 GeanyMatchInfo *info = match->data;
2166 gint line = sci_get_line_from_position(doc->editor->sci, info->start);
2168 if (line != prev_line)
2170 buffer = sci_get_line(doc->editor->sci, line);
2171 msgwin_msg_add(COLOR_BLACK, line + 1, doc,
2172 "%s:%d: %s", short_file_name, line + 1, g_strstrip(buffer));
2173 g_free(buffer);
2174 prev_line = line;
2176 count++;
2178 geany_match_info_free(info);
2180 g_slist_free(matches);
2181 g_free(short_file_name);
2182 return count;
2186 void search_find_usage(const gchar *search_text, const gchar *original_search_text,
2187 GeanyFindFlags flags, gboolean in_session)
2189 GeanyDocument *doc;
2190 gint count = 0;
2192 doc = document_get_current();
2193 g_return_if_fail(doc != NULL);
2195 if (G_UNLIKELY(EMPTY(search_text)))
2197 utils_beep();
2198 return;
2201 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_MESSAGE);
2202 gtk_list_store_clear(msgwindow.store_msg);
2204 if (! in_session)
2205 { /* use current document */
2206 count = find_document_usage(doc, search_text, flags);
2208 else
2210 guint i;
2211 for (i = 0; i < documents_array->len; i++)
2213 if (documents[i]->is_valid)
2215 count += find_document_usage(documents[i], search_text, flags);
2220 if (count == 0) /* no matches were found */
2222 ui_set_statusbar(FALSE, _("No matches found for \"%s\"."), original_search_text);
2223 msgwin_msg_add(COLOR_BLUE, -1, NULL, _("No matches found for \"%s\"."), original_search_text);
2225 else
2227 ui_set_statusbar(FALSE, ngettext(
2228 "Found %d match for \"%s\".", "Found %d matches for \"%s\".", count),
2229 count, original_search_text);
2230 msgwin_msg_add(COLOR_BLUE, -1, NULL, ngettext(
2231 "Found %d match for \"%s\".", "Found %d matches for \"%s\".", count),
2232 count, original_search_text);
2237 /* ttf is updated to include the last match position (ttf->chrg.cpMin) and
2238 * the new search range end (ttf->chrg.cpMax).
2239 * Note: Normally you would call sci_start/end_undo_action() around this call. */
2240 guint search_replace_range(ScintillaObject *sci, struct Sci_TextToFind *ttf,
2241 GeanyFindFlags flags, const gchar *replace_text)
2243 gint count = 0;
2244 gint offset = 0; /* difference between search pos and replace pos */
2245 GSList *match, *matches;
2247 g_return_val_if_fail(sci != NULL && ttf->lpstrText != NULL && replace_text != NULL, 0);
2248 if (! *ttf->lpstrText)
2249 return 0;
2251 matches = find_range(sci, flags, ttf);
2252 foreach_slist (match, matches)
2254 GeanyMatchInfo *info = match->data;
2255 gint replace_len;
2257 info->start += offset;
2258 info->end += offset;
2260 replace_len = search_replace_match(sci, info, replace_text);
2261 offset += replace_len - (info->end - info->start);
2262 count ++;
2264 /* on last match, update the last match/new range end */
2265 if (! match->next)
2267 ttf->chrg.cpMin = info->start;
2268 ttf->chrg.cpMax += offset;
2271 geany_match_info_free(info);
2273 g_slist_free(matches);
2275 return count;
2279 void search_find_again(gboolean change_direction)
2281 GeanyDocument *doc = document_get_current();
2283 g_return_if_fail(doc != NULL);
2285 if (search_data.text)
2287 gboolean forward = ! search_data.backwards;
2288 gint result = document_find_text(doc, search_data.text, search_data.original_text, search_data.flags,
2289 change_direction ? forward : !forward, NULL, FALSE, NULL);
2291 if (result > -1)
2292 editor_display_current_line(doc->editor, 0.3F);
2294 if (search_data.search_bar)
2295 ui_set_search_entry_background(
2296 toolbar_get_widget_child_by_name("SearchEntry"), (result > -1));