forked from geany/geany
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.c
2272 lines (1876 loc) · 69.4 KB
/
search.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* search.c - this file is part of Geany, a fast and lightweight IDE
*
* Copyright 2006-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
* Copyright 2006-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/*
* Find, Replace, Find in Files dialog related functions.
* Note that the basic text find functions are in document.c.
*/
#include <gdk/gdkkeysyms.h>
#include "geany.h"
#include "search.h"
#include "prefs.h"
#include "support.h"
#include "utils.h"
#include "document.h"
#include "msgwindow.h"
#include "sciwrappers.h"
#include "ui_utils.h"
#include "editor.h"
#include "encodings.h"
#include "project.h"
#include "keyfile.h"
#include "stash.h"
#include "toolbar.h"
#include "gtkcompat.h"
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#ifdef G_OS_UNIX
# include <sys/types.h>
# include <sys/wait.h>
#endif
enum
{
GEANY_RESPONSE_FIND = 1,
GEANY_RESPONSE_FIND_PREVIOUS,
GEANY_RESPONSE_FIND_IN_FILE,
GEANY_RESPONSE_FIND_IN_SESSION,
GEANY_RESPONSE_MARK,
GEANY_RESPONSE_REPLACE,
GEANY_RESPONSE_REPLACE_AND_FIND,
GEANY_RESPONSE_REPLACE_IN_SESSION,
GEANY_RESPONSE_REPLACE_IN_FILE,
GEANY_RESPONSE_REPLACE_IN_SEL
};
enum
{
FILES_MODE_ALL,
FILES_MODE_PROJECT,
FILES_MODE_CUSTOM
};
GeanySearchData search_data;
GeanySearchPrefs search_prefs;
static struct
{
gboolean fif_regexp;
gboolean fif_case_sensitive;
gboolean fif_match_whole_word;
gboolean fif_invert_results;
gboolean fif_recursive;
gboolean fif_use_extra_options;
gchar *fif_extra_options;
gint fif_files_mode;
gchar *fif_files;
gboolean find_regexp;
gboolean find_escape_sequences;
gboolean find_case_sensitive;
gboolean find_match_whole_word;
gboolean find_match_word_start;
gboolean find_close_dialog;
gboolean replace_regexp;
gboolean replace_escape_sequences;
gboolean replace_case_sensitive;
gboolean replace_match_whole_word;
gboolean replace_match_word_start;
gboolean replace_search_backwards;
gboolean replace_close_dialog;
}
settings;
static StashGroup *fif_prefs = NULL;
static StashGroup *find_prefs = NULL;
static StashGroup *replace_prefs = NULL;
static struct
{
GtkWidget *dialog;
GtkWidget *entry;
gboolean all_expanded;
gint position[2]; /* x, y */
}
find_dlg = {NULL, NULL, FALSE, {0, 0}};
static struct
{
GtkWidget *dialog;
GtkWidget *find_entry;
GtkWidget *replace_entry;
gboolean all_expanded;
gint position[2]; /* x, y */
}
replace_dlg = {NULL, NULL, NULL, FALSE, {0, 0}};
static struct
{
GtkWidget *dialog;
GtkWidget *dir_combo;
GtkWidget *files_combo;
GtkWidget *search_combo;
GtkWidget *encoding_combo;
GtkWidget *files_mode_combo;
gint position[2]; /* x, y */
}
fif_dlg = {NULL, NULL, NULL, NULL, NULL, NULL, {0, 0}};
static gboolean search_read_io(GIOChannel *source, GIOCondition condition, gpointer data);
static gboolean search_read_io_stderr(GIOChannel *source, GIOCondition condition, gpointer data);
static void search_close_pid(GPid child_pid, gint status, gpointer user_data);
static gchar **search_get_argv(const gchar **argv_prefix, const gchar *dir);
static GRegex *compile_regex(const gchar *str, gint sflags);
static void
on_find_replace_checkbutton_toggled(GtkToggleButton *togglebutton, gpointer user_data);
static void
on_find_dialog_response(GtkDialog *dialog, gint response, gpointer user_data);
static void
on_find_entry_activate(GtkEntry *entry, gpointer user_data);
static void
on_find_entry_activate_backward(GtkEntry *entry, gpointer user_data);
static void
on_replace_dialog_response(GtkDialog *dialog, gint response, gpointer user_data);
static void
on_replace_entry_activate(GtkEntry *entry, gpointer user_data);
static void
on_find_in_files_dialog_response(GtkDialog *dialog, gint response, gpointer user_data);
static gboolean
search_find_in_files(const gchar *utf8_search_text, const gchar *dir, const gchar *opts,
const gchar *enc);
static void init_prefs(void)
{
StashGroup *group;
group = stash_group_new("search");
configuration_add_pref_group(group, TRUE);
stash_group_add_toggle_button(group, &search_prefs.always_wrap,
"pref_search_hide_find_dialog", FALSE, "check_always_wrap_search");
stash_group_add_toggle_button(group, &search_prefs.hide_find_dialog,
"pref_search_always_wrap", FALSE, "check_hide_find_dialog");
stash_group_add_toggle_button(group, &search_prefs.use_current_file_dir,
"pref_search_current_file_dir", TRUE, "check_fif_current_dir");
stash_group_add_boolean(group, &find_dlg.all_expanded, "find_all_expanded", FALSE);
stash_group_add_boolean(group, &replace_dlg.all_expanded, "replace_all_expanded", FALSE);
/* dialog positions */
stash_group_add_integer(group, &find_dlg.position[0], "position_find_x", -1);
stash_group_add_integer(group, &find_dlg.position[1], "position_find_y", -1);
stash_group_add_integer(group, &replace_dlg.position[0], "position_replace_x", -1);
stash_group_add_integer(group, &replace_dlg.position[1], "position_replace_y", -1);
stash_group_add_integer(group, &fif_dlg.position[0], "position_fif_x", -1);
stash_group_add_integer(group, &fif_dlg.position[1], "position_fif_y", -1);
memset(&settings, '\0', sizeof(settings));
group = stash_group_new("search");
fif_prefs = group;
configuration_add_pref_group(group, FALSE);
stash_group_add_toggle_button(group, &settings.fif_regexp,
"fif_regexp", FALSE, "check_regexp");
stash_group_add_toggle_button(group, &settings.fif_case_sensitive,
"fif_case_sensitive", TRUE, "check_case");
stash_group_add_toggle_button(group, &settings.fif_match_whole_word,
"fif_match_whole_word", FALSE, "check_wholeword");
stash_group_add_toggle_button(group, &settings.fif_invert_results,
"fif_invert_results", FALSE, "check_invert");
stash_group_add_toggle_button(group, &settings.fif_recursive,
"fif_recursive", FALSE, "check_recursive");
stash_group_add_entry(group, &settings.fif_extra_options,
"fif_extra_options", "", "entry_extra");
stash_group_add_toggle_button(group, &settings.fif_use_extra_options,
"fif_use_extra_options", FALSE, "check_extra");
stash_group_add_entry(group, &settings.fif_files,
"fif_files", "", "entry_files");
stash_group_add_combo_box(group, &settings.fif_files_mode,
"fif_files_mode", FILES_MODE_ALL, "combo_files_mode");
group = stash_group_new("search");
find_prefs = group;
configuration_add_pref_group(group, FALSE);
stash_group_add_toggle_button(group, &settings.find_regexp,
"find_regexp", FALSE, "check_regexp");
stash_group_add_toggle_button(group, &settings.find_case_sensitive,
"find_case_sensitive", FALSE, "check_case");
stash_group_add_toggle_button(group, &settings.find_escape_sequences,
"find_escape_sequences", FALSE, "check_escape");
stash_group_add_toggle_button(group, &settings.find_match_whole_word,
"find_match_whole_word", FALSE, "check_word");
stash_group_add_toggle_button(group, &settings.find_match_word_start,
"find_match_word_start", FALSE, "check_wordstart");
stash_group_add_toggle_button(group, &settings.find_close_dialog,
"find_close_dialog", TRUE, "check_close");
group = stash_group_new("search");
replace_prefs = group;
configuration_add_pref_group(group, FALSE);
stash_group_add_toggle_button(group, &settings.replace_regexp,
"replace_regexp", FALSE, "check_regexp");
stash_group_add_toggle_button(group, &settings.replace_case_sensitive,
"replace_case_sensitive", FALSE, "check_case");
stash_group_add_toggle_button(group, &settings.replace_escape_sequences,
"replace_escape_sequences", FALSE, "check_escape");
stash_group_add_toggle_button(group, &settings.replace_match_whole_word,
"replace_match_whole_word", FALSE, "check_word");
stash_group_add_toggle_button(group, &settings.replace_match_word_start,
"replace_match_word_start", FALSE, "check_wordstart");
stash_group_add_toggle_button(group, &settings.replace_search_backwards,
"replace_search_backwards", FALSE, "check_back");
stash_group_add_toggle_button(group, &settings.replace_close_dialog,
"replace_close_dialog", TRUE, "check_close");
}
void search_init(void)
{
search_data.text = NULL;
search_data.original_text = NULL;
init_prefs();
}
#define FREE_WIDGET(wid) \
if (wid && GTK_IS_WIDGET(wid)) gtk_widget_destroy(wid);
void search_finalize(void)
{
FREE_WIDGET(find_dlg.dialog);
FREE_WIDGET(replace_dlg.dialog);
FREE_WIDGET(fif_dlg.dialog);
g_free(search_data.text);
g_free(search_data.original_text);
}
static void on_widget_toggled_set_insensitive(
GtkToggleButton *togglebutton, gpointer user_data)
{
gtk_widget_set_sensitive(GTK_WIDGET(user_data),
!gtk_toggle_button_get_active(togglebutton));
}
static GtkWidget *add_find_checkboxes(GtkDialog *dialog)
{
GtkWidget *checkbox1, *checkbox2, *check_regexp, *check_back, *checkbox5,
*checkbox7, *hbox, *fbox, *mbox;
check_regexp = gtk_check_button_new_with_mnemonic(_("_Use regular expressions"));
ui_hookup_widget(dialog, check_regexp, "check_regexp");
gtk_button_set_focus_on_click(GTK_BUTTON(check_regexp), FALSE);
gtk_widget_set_tooltip_text(check_regexp, _("Use POSIX-like regular expressions. "
"For detailed information about using regular expressions, please read the documentation."));
g_signal_connect(check_regexp, "toggled",
G_CALLBACK(on_find_replace_checkbutton_toggled), dialog);
if (dialog != GTK_DIALOG(find_dlg.dialog))
{
check_back = gtk_check_button_new_with_mnemonic(_("Search _backwards"));
ui_hookup_widget(dialog, check_back, "check_back");
gtk_button_set_focus_on_click(GTK_BUTTON(check_back), FALSE);
}
else
{ /* align the two checkboxes at the top of the hbox */
GtkSizeGroup *label_size;
check_back = gtk_label_new(NULL);
label_size = gtk_size_group_new(GTK_SIZE_GROUP_VERTICAL);
gtk_size_group_add_widget(GTK_SIZE_GROUP(label_size), check_back);
gtk_size_group_add_widget(GTK_SIZE_GROUP(label_size), check_regexp);
g_object_unref(label_size);
}
checkbox7 = gtk_check_button_new_with_mnemonic(_("Use _escape sequences"));
ui_hookup_widget(dialog, checkbox7, "check_escape");
gtk_button_set_focus_on_click(GTK_BUTTON(checkbox7), FALSE);
gtk_widget_set_tooltip_text(checkbox7,
_("Replace \\\\, \\t, \\n, \\r and \\uXXXX (Unicode chararacters) with the "
"corresponding control characters"));
/* Search features */
fbox = gtk_vbox_new(FALSE, 0);
gtk_container_add(GTK_CONTAINER(fbox), check_regexp);
gtk_container_add(GTK_CONTAINER(fbox), checkbox7);
gtk_container_add(GTK_CONTAINER(fbox), check_back);
checkbox1 = gtk_check_button_new_with_mnemonic(_("C_ase sensitive"));
ui_hookup_widget(dialog, checkbox1, "check_case");
gtk_button_set_focus_on_click(GTK_BUTTON(checkbox1), FALSE);
checkbox2 = gtk_check_button_new_with_mnemonic(_("Match only a _whole word"));
ui_hookup_widget(dialog, checkbox2, "check_word");
gtk_button_set_focus_on_click(GTK_BUTTON(checkbox2), FALSE);
checkbox5 = gtk_check_button_new_with_mnemonic(_("Match from s_tart of word"));
ui_hookup_widget(dialog, checkbox5, "check_wordstart");
gtk_button_set_focus_on_click(GTK_BUTTON(checkbox5), FALSE);
/* disable wordstart when wholeword is checked */
g_signal_connect(checkbox2, "toggled",
G_CALLBACK(on_widget_toggled_set_insensitive), checkbox5);
/* Matching options */
mbox = gtk_vbox_new(FALSE, 0);
gtk_container_add(GTK_CONTAINER(mbox), checkbox1);
gtk_container_add(GTK_CONTAINER(mbox), checkbox2);
gtk_container_add(GTK_CONTAINER(mbox), checkbox5);
hbox = gtk_hbox_new(TRUE, 6);
gtk_container_add(GTK_CONTAINER(hbox), fbox);
gtk_container_add(GTK_CONTAINER(hbox), mbox);
return hbox;
}
static void send_find_dialog_response(GtkButton *button, gpointer user_data)
{
gtk_dialog_response(GTK_DIALOG(find_dlg.dialog), GPOINTER_TO_INT(user_data));
}
/* store text, clear search flags so we can use Search->Find Next/Previous */
static void setup_find_next(const gchar *text)
{
g_free(search_data.text);
g_free(search_data.original_text);
search_data.text = g_strdup(text);
search_data.original_text = g_strdup(text);
search_data.flags = 0;
search_data.backwards = FALSE;
search_data.search_bar = FALSE;
}
/* Search for next match of the current "selection".
* Optionally for X11 based systems, this will try to use the system-wide
* x-selection first.
* If it doesn't find a suitable search string it will try to use
* the current word instead, or just repeat the last search.
* Search flags are always zero.
*/
void search_find_selection(GeanyDocument *doc, gboolean search_backwards)
{
gchar *s = NULL;
g_return_if_fail(doc != NULL);
#ifdef G_OS_UNIX
if (search_prefs.find_selection_type == GEANY_FIND_SEL_X)
{
GtkClipboard *clipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
s = gtk_clipboard_wait_for_text(clipboard);
if (s && (strchr(s,'\n') || strchr(s, '\r')))
{
g_free(s);
s = NULL;
};
}
#endif
if (!s && sci_has_selection(doc->editor->sci))
s = sci_get_selection_contents(doc->editor->sci);
if (!s && search_prefs.find_selection_type != GEANY_FIND_SEL_AGAIN)
{
/* get the current word */
s = editor_get_default_selection(doc->editor, TRUE, NULL);
}
if (s)
{
setup_find_next(s); /* allow find next/prev */
if (document_find_text(doc, s, NULL, 0, search_backwards, NULL, FALSE, NULL) > -1)
editor_display_current_line(doc->editor, 0.3F);
g_free(s);
}
else if (search_prefs.find_selection_type == GEANY_FIND_SEL_AGAIN)
{
/* Repeat last search (in case selection was lost) */
search_find_again(search_backwards);
}
else
{
utils_beep();
}
}
static void on_expander_activated(GtkExpander *exp, gpointer data)
{
gboolean *setting = data;
*setting = gtk_expander_get_expanded(exp);
}
static void create_find_dialog(void)
{
GtkWidget *label, *entry, *sbox, *vbox;
GtkWidget *exp, *bbox, *button, *check_close;
find_dlg.dialog = gtk_dialog_new_with_buttons(_("Find"),
GTK_WINDOW(main_widgets.window), GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_STOCK_CLOSE, GTK_RESPONSE_CANCEL, NULL);
vbox = ui_dialog_vbox_new(GTK_DIALOG(find_dlg.dialog));
gtk_widget_set_name(find_dlg.dialog, "GeanyDialogSearch");
gtk_box_set_spacing(GTK_BOX(vbox), 9);
button = ui_button_new_with_image(GTK_STOCK_GO_BACK, _("_Previous"));
gtk_dialog_add_action_widget(GTK_DIALOG(find_dlg.dialog), button,
GEANY_RESPONSE_FIND_PREVIOUS);
ui_hookup_widget(find_dlg.dialog, button, "btn_previous");
button = ui_button_new_with_image(GTK_STOCK_GO_FORWARD, _("_Next"));
gtk_dialog_add_action_widget(GTK_DIALOG(find_dlg.dialog), button,
GEANY_RESPONSE_FIND);
label = gtk_label_new_with_mnemonic(_("_Search for:"));
gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
entry = gtk_combo_box_text_new_with_entry();
ui_entry_add_clear_icon(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(entry))));
gtk_label_set_mnemonic_widget(GTK_LABEL(label), entry);
gtk_entry_set_width_chars(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(entry))), 50);
find_dlg.entry = gtk_bin_get_child(GTK_BIN(entry));
ui_hookup_widget(find_dlg.dialog, entry, "entry");
g_signal_connect(gtk_bin_get_child(GTK_BIN(entry)), "activate",
G_CALLBACK(on_find_entry_activate), NULL);
ui_entry_add_activate_backward_signal(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(entry))));
g_signal_connect(gtk_bin_get_child(GTK_BIN(entry)), "activate-backward",
G_CALLBACK(on_find_entry_activate_backward), NULL);
g_signal_connect(find_dlg.dialog, "response",
G_CALLBACK(on_find_dialog_response), entry);
g_signal_connect(find_dlg.dialog, "delete-event",
G_CALLBACK(gtk_widget_hide_on_delete), NULL);
sbox = gtk_hbox_new(FALSE, 6);
gtk_box_pack_start(GTK_BOX(sbox), label, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(sbox), entry, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(vbox), sbox, TRUE, FALSE, 0);
gtk_container_add(GTK_CONTAINER(vbox),
add_find_checkboxes(GTK_DIALOG(find_dlg.dialog)));
/* Now add the multiple match options */
exp = gtk_expander_new_with_mnemonic(_("_Find All"));
gtk_expander_set_expanded(GTK_EXPANDER(exp), find_dlg.all_expanded);
g_signal_connect_after(exp, "activate",
G_CALLBACK(on_expander_activated), &find_dlg.all_expanded);
bbox = gtk_hbutton_box_new();
button = gtk_button_new_with_mnemonic(_("_Mark"));
gtk_widget_set_tooltip_text(button,
_("Mark all matches in the current document"));
gtk_container_add(GTK_CONTAINER(bbox), button);
g_signal_connect(button, "clicked", G_CALLBACK(send_find_dialog_response),
GINT_TO_POINTER(GEANY_RESPONSE_MARK));
button = gtk_button_new_with_mnemonic(_("In Sessi_on"));
gtk_container_add(GTK_CONTAINER(bbox), button);
g_signal_connect(button, "clicked", G_CALLBACK(send_find_dialog_response),
GINT_TO_POINTER(GEANY_RESPONSE_FIND_IN_SESSION));
button = gtk_button_new_with_mnemonic(_("_In Document"));
gtk_container_add(GTK_CONTAINER(bbox), button);
g_signal_connect(button, "clicked", G_CALLBACK(send_find_dialog_response),
GINT_TO_POINTER(GEANY_RESPONSE_FIND_IN_FILE));
/* close window checkbox */
check_close = gtk_check_button_new_with_mnemonic(_("Close _dialog"));
ui_hookup_widget(find_dlg.dialog, check_close, "check_close");
gtk_button_set_focus_on_click(GTK_BUTTON(check_close), FALSE);
gtk_widget_set_tooltip_text(check_close,
_("Disable this option to keep the dialog open"));
gtk_container_add(GTK_CONTAINER(bbox), check_close);
gtk_button_box_set_child_secondary(GTK_BUTTON_BOX(bbox), check_close, TRUE);
ui_hbutton_box_copy_layout(
GTK_BUTTON_BOX(gtk_dialog_get_action_area(GTK_DIALOG(find_dlg.dialog))),
GTK_BUTTON_BOX(bbox));
gtk_container_add(GTK_CONTAINER(exp), bbox);
gtk_container_add(GTK_CONTAINER(vbox), exp);
}
static void set_dialog_position(GtkWidget *dialog, gint *position)
{
if (position[0] >= 0)
gtk_window_move(GTK_WINDOW(dialog), position[0], position[1]);
}
void search_show_find_dialog(void)
{
GeanyDocument *doc = document_get_current();
gchar *sel = NULL;
g_return_if_fail(doc != NULL);
sel = editor_get_default_selection(doc->editor, search_prefs.use_current_word, NULL);
if (find_dlg.dialog == NULL)
{
create_find_dialog();
stash_group_display(find_prefs, find_dlg.dialog);
if (sel)
gtk_entry_set_text(GTK_ENTRY(find_dlg.entry), sel);
set_dialog_position(find_dlg.dialog, find_dlg.position);
gtk_widget_show_all(find_dlg.dialog);
}
else
{
/* only set selection if the dialog is not already visible */
if (! gtk_widget_get_visible(find_dlg.dialog) && sel)
gtk_entry_set_text(GTK_ENTRY(find_dlg.entry), sel);
gtk_widget_grab_focus(find_dlg.entry);
set_dialog_position(find_dlg.dialog, find_dlg.position);
gtk_widget_show(find_dlg.dialog);
if (sel != NULL) /* when we have a selection, reset the entry widget's background colour */
ui_set_search_entry_background(find_dlg.entry, TRUE);
/* bring the dialog back in the foreground in case it is already open but the focus is away */
gtk_window_present(GTK_WINDOW(find_dlg.dialog));
}
g_free(sel);
}
static void send_replace_dialog_response(GtkButton *button, gpointer user_data)
{
gtk_dialog_response(GTK_DIALOG(replace_dlg.dialog), GPOINTER_TO_INT(user_data));
}
static gboolean
on_widget_key_pressed_set_focus(GtkWidget *widget, GdkEventKey *event, gpointer user_data)
{
if (event->keyval == GDK_Tab)
{
gtk_widget_grab_focus(GTK_WIDGET(user_data));
return TRUE;
}
return FALSE;
}
static void create_replace_dialog(void)
{
GtkWidget *label_find, *label_replace, *entry_find, *entry_replace,
*check_close, *button, *rbox, *fbox, *vbox, *exp, *bbox;
GtkSizeGroup *label_size;
replace_dlg.dialog = gtk_dialog_new_with_buttons(_("Replace"),
GTK_WINDOW(main_widgets.window), GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_STOCK_CLOSE, GTK_RESPONSE_CANCEL, NULL);
vbox = ui_dialog_vbox_new(GTK_DIALOG(replace_dlg.dialog));
gtk_box_set_spacing(GTK_BOX(vbox), 9);
gtk_widget_set_name(replace_dlg.dialog, "GeanyDialogSearch");
button = gtk_button_new_from_stock(GTK_STOCK_FIND);
gtk_dialog_add_action_widget(GTK_DIALOG(replace_dlg.dialog), button,
GEANY_RESPONSE_FIND);
button = gtk_button_new_with_mnemonic(_("_Replace"));
gtk_button_set_image(GTK_BUTTON(button),
gtk_image_new_from_stock(GTK_STOCK_FIND_AND_REPLACE, GTK_ICON_SIZE_BUTTON));
gtk_dialog_add_action_widget(GTK_DIALOG(replace_dlg.dialog), button,
GEANY_RESPONSE_REPLACE);
button = gtk_button_new_with_mnemonic(_("Replace & Fi_nd"));
gtk_button_set_image(GTK_BUTTON(button),
gtk_image_new_from_stock(GTK_STOCK_FIND_AND_REPLACE, GTK_ICON_SIZE_BUTTON));
gtk_dialog_add_action_widget(GTK_DIALOG(replace_dlg.dialog), button,
GEANY_RESPONSE_REPLACE_AND_FIND);
label_find = gtk_label_new_with_mnemonic(_("_Search for:"));
gtk_misc_set_alignment(GTK_MISC(label_find), 0, 0.5);
label_replace = gtk_label_new_with_mnemonic(_("Replace wit_h:"));
gtk_misc_set_alignment(GTK_MISC(label_replace), 0, 0.5);
entry_find = gtk_combo_box_text_new_with_entry();
ui_entry_add_clear_icon(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(entry_find))));
gtk_label_set_mnemonic_widget(GTK_LABEL(label_find), entry_find);
gtk_entry_set_width_chars(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(entry_find))), 50);
ui_hookup_widget(replace_dlg.dialog, entry_find, "entry_find");
replace_dlg.find_entry = gtk_bin_get_child(GTK_BIN(entry_find));
entry_replace = gtk_combo_box_text_new_with_entry();
ui_entry_add_clear_icon(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(entry_replace))));
gtk_label_set_mnemonic_widget(GTK_LABEL(label_replace), entry_replace);
gtk_entry_set_width_chars(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(entry_replace))), 50);
ui_hookup_widget(replace_dlg.dialog, entry_replace, "entry_replace");
replace_dlg.replace_entry = gtk_bin_get_child(GTK_BIN(entry_replace));
/* tab from find to the replace entry */
g_signal_connect(gtk_bin_get_child(GTK_BIN(entry_find)),
"key-press-event", G_CALLBACK(on_widget_key_pressed_set_focus),
gtk_bin_get_child(GTK_BIN(entry_replace)));
g_signal_connect(gtk_bin_get_child(GTK_BIN(entry_replace)), "activate",
G_CALLBACK(on_replace_entry_activate), NULL);
g_signal_connect(replace_dlg.dialog, "response",
G_CALLBACK(on_replace_dialog_response), entry_replace);
g_signal_connect(replace_dlg.dialog, "delete-event",
G_CALLBACK(gtk_widget_hide_on_delete), NULL);
fbox = gtk_hbox_new(FALSE, 6);
gtk_box_pack_start(GTK_BOX(fbox), label_find, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(fbox), entry_find, TRUE, TRUE, 0);
rbox = gtk_hbox_new(FALSE, 6);
gtk_box_pack_start(GTK_BOX(rbox), label_replace, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(rbox), entry_replace, TRUE, TRUE, 0);
label_size = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
gtk_size_group_add_widget(label_size, label_find);
gtk_size_group_add_widget(label_size, label_replace);
g_object_unref(G_OBJECT(label_size)); /* auto destroy the size group */
gtk_box_pack_start(GTK_BOX(vbox), fbox, TRUE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(vbox), rbox, TRUE, FALSE, 0);
gtk_container_add(GTK_CONTAINER(vbox),
add_find_checkboxes(GTK_DIALOG(replace_dlg.dialog)));
/* Now add the multiple replace options */
exp = gtk_expander_new_with_mnemonic(_("Re_place All"));
gtk_expander_set_expanded(GTK_EXPANDER(exp), replace_dlg.all_expanded);
g_signal_connect_after(exp, "activate",
G_CALLBACK(on_expander_activated), &replace_dlg.all_expanded);
bbox = gtk_hbutton_box_new();
button = gtk_button_new_with_mnemonic(_("In Sessi_on"));
gtk_container_add(GTK_CONTAINER(bbox), button);
g_signal_connect(button, "clicked", G_CALLBACK(send_replace_dialog_response),
GINT_TO_POINTER(GEANY_RESPONSE_REPLACE_IN_SESSION));
button = gtk_button_new_with_mnemonic(_("_In Document"));
gtk_container_add(GTK_CONTAINER(bbox), button);
g_signal_connect(button, "clicked", G_CALLBACK(send_replace_dialog_response),
GINT_TO_POINTER(GEANY_RESPONSE_REPLACE_IN_FILE));
button = gtk_button_new_with_mnemonic(_("In Se_lection"));
gtk_widget_set_tooltip_text(button,
_("Replace all matches found in the currently selected text"));
gtk_container_add(GTK_CONTAINER(bbox), button);
g_signal_connect(button, "clicked", G_CALLBACK(send_replace_dialog_response),
GINT_TO_POINTER(GEANY_RESPONSE_REPLACE_IN_SEL));
/* close window checkbox */
check_close = gtk_check_button_new_with_mnemonic(_("Close _dialog"));
ui_hookup_widget(replace_dlg.dialog, check_close, "check_close");
gtk_button_set_focus_on_click(GTK_BUTTON(check_close), FALSE);
gtk_widget_set_tooltip_text(check_close,
_("Disable this option to keep the dialog open"));
gtk_container_add(GTK_CONTAINER(bbox), check_close);
gtk_button_box_set_child_secondary(GTK_BUTTON_BOX(bbox), check_close, TRUE);
ui_hbutton_box_copy_layout(
GTK_BUTTON_BOX(gtk_dialog_get_action_area(GTK_DIALOG(replace_dlg.dialog))),
GTK_BUTTON_BOX(bbox));
gtk_container_add(GTK_CONTAINER(exp), bbox);
gtk_container_add(GTK_CONTAINER(vbox), exp);
}
void search_show_replace_dialog(void)
{
GeanyDocument *doc = document_get_current();
gchar *sel = NULL;
if (doc == NULL)
return;
sel = editor_get_default_selection(doc->editor, search_prefs.use_current_word, NULL);
if (replace_dlg.dialog == NULL)
{
create_replace_dialog();
stash_group_display(replace_prefs, replace_dlg.dialog);
if (sel)
gtk_entry_set_text(GTK_ENTRY(replace_dlg.find_entry), sel);
set_dialog_position(replace_dlg.dialog, replace_dlg.position);
gtk_widget_show_all(replace_dlg.dialog);
}
else
{
/* only set selection if the dialog is not already visible */
if (! gtk_widget_get_visible(replace_dlg.dialog) && sel)
gtk_entry_set_text(GTK_ENTRY(replace_dlg.find_entry), sel);
if (sel != NULL) /* when we have a selection, reset the entry widget's background colour */
ui_set_search_entry_background(replace_dlg.find_entry, TRUE);
gtk_widget_grab_focus(replace_dlg.find_entry);
set_dialog_position(replace_dlg.dialog, replace_dlg.position);
gtk_widget_show(replace_dlg.dialog);
/* bring the dialog back in the foreground in case it is already open but the focus is away */
gtk_window_present(GTK_WINDOW(replace_dlg.dialog));
}
g_free(sel);
}
static void on_widget_toggled_set_sensitive(GtkToggleButton *togglebutton, gpointer user_data)
{
/* disable extra option entry when checkbutton not checked */
gtk_widget_set_sensitive(GTK_WIDGET(user_data),
gtk_toggle_button_get_active(togglebutton));
}
static void update_file_patterns(GtkWidget *mode_combo, GtkWidget *fcombo)
{
gint selection;
GtkWidget *entry;
entry = gtk_bin_get_child(GTK_BIN(fcombo));
selection = gtk_combo_box_get_active(GTK_COMBO_BOX(mode_combo));
if (selection == FILES_MODE_ALL)
{
gtk_entry_set_text(GTK_ENTRY(entry), "");
gtk_widget_set_sensitive(fcombo, FALSE);
}
else if (selection == FILES_MODE_CUSTOM)
{
gtk_widget_set_sensitive(fcombo, TRUE);
}
else if (selection == FILES_MODE_PROJECT)
{
if (app->project && !EMPTY(app->project->file_patterns))
{
gchar *patterns;
patterns = g_strjoinv(" ", app->project->file_patterns);
gtk_entry_set_text(GTK_ENTRY(entry), patterns);
g_free(patterns);
}
else
{
gtk_entry_set_text(GTK_ENTRY(entry), "");
}
gtk_widget_set_sensitive(fcombo, FALSE);
}
}
/* creates the combo to choose which files include in the search */
static GtkWidget *create_fif_file_mode_combo(void)
{
GtkWidget *combo;
GtkCellRenderer *renderer;
GtkListStore *store;
GtkTreeIter iter;
/* text/sensitive */
store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_BOOLEAN);
gtk_list_store_append(store, &iter);
gtk_list_store_set(store, &iter, 0, _("all"), 1, TRUE, -1);
gtk_list_store_append(store, &iter);
gtk_list_store_set(store, &iter, 0, _("project"), 1, app->project != NULL, -1);
gtk_list_store_append(store, &iter);
gtk_list_store_set(store, &iter, 0, _("custom"), 1, TRUE, -1);
combo = gtk_combo_box_new_with_model(GTK_TREE_MODEL(store));
g_object_unref(store);
gtk_widget_set_tooltip_text(combo, _("All: search all files in the directory\n"
"Project: use file patterns defined in the project settings\n"
"Custom: specify file patterns manually"));
renderer = gtk_cell_renderer_text_new();
gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), renderer, TRUE);
gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo), renderer, "text", 0, "sensitive", 1, NULL);
return combo;
}
/* updates the sensitivity of the project combo item */
static void update_fif_file_mode_combo(void)
{
GtkTreeModel *model = gtk_combo_box_get_model(GTK_COMBO_BOX(fif_dlg.files_mode_combo));
GtkTreeIter iter;
/* "1" refers to the second list entry, project */
if (gtk_tree_model_get_iter_from_string(model, &iter, "1"))
gtk_list_store_set(GTK_LIST_STORE(model), &iter, 1, app->project != NULL, -1);
}
static void create_fif_dialog(void)
{
GtkWidget *dir_combo, *combo, *fcombo, *e_combo, *entry;
GtkWidget *label, *label1, *label2, *label3, *checkbox1, *checkbox2, *check_wholeword,
*check_recursive, *check_extra, *entry_extra, *check_regexp, *combo_files_mode;
GtkWidget *dbox, *sbox, *lbox, *rbox, *hbox, *vbox, *ebox;
GtkSizeGroup *size_group;
gchar *encoding_string;
guint i;
fif_dlg.dialog = gtk_dialog_new_with_buttons(
_("Find in Files"), GTK_WINDOW(main_widgets.window), GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, NULL);
vbox = ui_dialog_vbox_new(GTK_DIALOG(fif_dlg.dialog));
gtk_box_set_spacing(GTK_BOX(vbox), 9);
gtk_widget_set_name(fif_dlg.dialog, "GeanyDialogSearch");
gtk_dialog_add_button(GTK_DIALOG(fif_dlg.dialog), GTK_STOCK_FIND, GTK_RESPONSE_ACCEPT);
gtk_dialog_set_default_response(GTK_DIALOG(fif_dlg.dialog),
GTK_RESPONSE_ACCEPT);
label = gtk_label_new_with_mnemonic(_("_Search for:"));
gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
combo = gtk_combo_box_text_new_with_entry();
entry = gtk_bin_get_child(GTK_BIN(combo));
ui_entry_add_clear_icon(GTK_ENTRY(entry));
gtk_label_set_mnemonic_widget(GTK_LABEL(label), entry);
gtk_entry_set_width_chars(GTK_ENTRY(entry), 50);
gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
fif_dlg.search_combo = combo;
sbox = gtk_hbox_new(FALSE, 6);
gtk_box_pack_start(GTK_BOX(sbox), label, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(sbox), combo, TRUE, TRUE, 0);
/* make labels same width */
size_group = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
gtk_size_group_add_widget(size_group, label);
label3 = gtk_label_new_with_mnemonic(_("Fi_les:"));
gtk_misc_set_alignment(GTK_MISC(label3), 0, 0.5);
combo_files_mode = create_fif_file_mode_combo();
gtk_label_set_mnemonic_widget(GTK_LABEL(label3), combo_files_mode);
ui_hookup_widget(fif_dlg.dialog, combo_files_mode, "combo_files_mode");
fif_dlg.files_mode_combo = combo_files_mode;
fcombo = gtk_combo_box_text_new_with_entry();
entry = gtk_bin_get_child(GTK_BIN(fcombo));
ui_entry_add_clear_icon(GTK_ENTRY(entry));
gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
gtk_widget_set_tooltip_text(entry, _("File patterns, e.g. *.c *.h"));
ui_hookup_widget(fif_dlg.dialog, entry, "entry_files");
fif_dlg.files_combo = fcombo;
/* update the entry when selection is changed */
g_signal_connect(combo_files_mode, "changed", G_CALLBACK(update_file_patterns), fcombo);
hbox = gtk_hbox_new(FALSE, 6);
gtk_box_pack_start(GTK_BOX(hbox), label3, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(hbox), combo_files_mode, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(hbox), fcombo, TRUE, TRUE, 0);
label1 = gtk_label_new_with_mnemonic(_("_Directory:"));
gtk_misc_set_alignment(GTK_MISC(label1), 0, 0.5);
dir_combo = gtk_combo_box_text_new_with_entry();
entry = gtk_bin_get_child(GTK_BIN(dir_combo));
ui_entry_add_clear_icon(GTK_ENTRY(entry));
gtk_label_set_mnemonic_widget(GTK_LABEL(label1), entry);
gtk_entry_set_width_chars(GTK_ENTRY(entry), 50);
fif_dlg.dir_combo = dir_combo;
/* tab from files to the dir entry */
g_signal_connect(gtk_bin_get_child(GTK_BIN(fcombo)), "key-press-event",
G_CALLBACK(on_widget_key_pressed_set_focus), entry);
dbox = ui_path_box_new(NULL, GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
GTK_ENTRY(entry));
gtk_box_pack_start(GTK_BOX(dbox), label1, FALSE, FALSE, 0);
label2 = gtk_label_new_with_mnemonic(_("E_ncoding:"));
gtk_misc_set_alignment(GTK_MISC(label2), 0, 0.5);
e_combo = gtk_combo_box_text_new();
for (i = 0; i < GEANY_ENCODINGS_MAX; i++)
{
encoding_string = encodings_to_string(&encodings[i]);
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(e_combo), encoding_string);
g_free(encoding_string);
}
gtk_combo_box_set_wrap_width(GTK_COMBO_BOX(e_combo), 3);
gtk_label_set_mnemonic_widget(GTK_LABEL(label2), e_combo);
fif_dlg.encoding_combo = e_combo;
ebox = gtk_hbox_new(FALSE, 6);
gtk_box_pack_start(GTK_BOX(ebox), label2, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(ebox), e_combo, TRUE, TRUE, 0);
gtk_size_group_add_widget(size_group, label1);
gtk_size_group_add_widget(size_group, label2);
gtk_size_group_add_widget(size_group, label3);
g_object_unref(G_OBJECT(size_group)); /* auto destroy the size group */
gtk_box_pack_start(GTK_BOX(vbox), sbox, TRUE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(vbox), dbox, TRUE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(vbox), ebox, TRUE, FALSE, 0);
check_regexp = gtk_check_button_new_with_mnemonic(_("_Use regular expressions"));
ui_hookup_widget(fif_dlg.dialog, check_regexp, "check_regexp");
gtk_button_set_focus_on_click(GTK_BUTTON(check_regexp), FALSE);
gtk_widget_set_tooltip_text(check_regexp, _("See grep's manual page for more information"));
check_recursive = gtk_check_button_new_with_mnemonic(_("_Recurse in subfolders"));
ui_hookup_widget(fif_dlg.dialog, check_recursive, "check_recursive");
gtk_button_set_focus_on_click(GTK_BUTTON(check_recursive), FALSE);
checkbox1 = gtk_check_button_new_with_mnemonic(_("C_ase sensitive"));
ui_hookup_widget(fif_dlg.dialog, checkbox1, "check_case");
gtk_button_set_focus_on_click(GTK_BUTTON(checkbox1), FALSE);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox1), TRUE);
check_wholeword = gtk_check_button_new_with_mnemonic(_("Match only a _whole word"));
ui_hookup_widget(fif_dlg.dialog, check_wholeword, "check_wholeword");
gtk_button_set_focus_on_click(GTK_BUTTON(check_wholeword), FALSE);
checkbox2 = gtk_check_button_new_with_mnemonic(_("_Invert search results"));
ui_hookup_widget(fif_dlg.dialog, checkbox2, "check_invert");
gtk_button_set_focus_on_click(GTK_BUTTON(checkbox2), FALSE);
gtk_widget_set_tooltip_text(checkbox2,
_("Invert the sense of matching, to select non-matching lines"));
lbox = gtk_vbox_new(FALSE, 0);
gtk_container_add(GTK_CONTAINER(lbox), check_regexp);
gtk_container_add(GTK_CONTAINER(lbox), checkbox2);
gtk_container_add(GTK_CONTAINER(lbox), check_recursive);
rbox = gtk_vbox_new(FALSE, 0);
gtk_container_add(GTK_CONTAINER(rbox), checkbox1);
gtk_container_add(GTK_CONTAINER(rbox), check_wholeword);
gtk_container_add(GTK_CONTAINER(rbox), gtk_label_new(NULL));
hbox = gtk_hbox_new(FALSE, 6);
gtk_container_add(GTK_CONTAINER(hbox), lbox);
gtk_container_add(GTK_CONTAINER(hbox), rbox);
gtk_container_add(GTK_CONTAINER(vbox), hbox);
check_extra = gtk_check_button_new_with_mnemonic(_("E_xtra options:"));
ui_hookup_widget(fif_dlg.dialog, check_extra, "check_extra");
gtk_button_set_focus_on_click(GTK_BUTTON(check_extra), FALSE);
entry_extra = gtk_entry_new();
ui_entry_add_clear_icon(GTK_ENTRY(entry_extra));
gtk_widget_set_sensitive(entry_extra, FALSE);