1 /* Bluefish HTML Editor
2  * snr3.h - search and replace
3  *
4  * Copyright (C) 2011-2017 Olivier Sessink
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 #ifndef __SNR3_H_
20 #define __SNR3_H_
21 
22 #include "async_queue.h"
23 
24 typedef enum {
25 	snr3type_string,
26 	snr3type_pcre
27 } Tsnr3type;
28 
29 typedef enum {
30 	snr3scope_doc,
31 	snr3scope_cursor,
32 	snr3scope_selection,
33 	snr3scope_alldocs,
34 	snr3scope_files
35 } Tsnr3scope;
36 
37 typedef enum {
38 	snr3replace_string,
39 	snr3replace_upper,
40 	snr3replace_lower
41 } Tsnr3replace;
42 
43 typedef struct {
44 	gpointer doc;
45 	gint32 so;
46 	gint32 eo;
47 } Tsnr3result;
48 
49 #define S3RESULT(var)  ((Tsnr3result *)var)
50 
51 typedef struct {
52 	Tbfwin *bfwin;
53 	gpointer dialog; /* NULL for simple search or external search */
54 	gchar *query;
55 	gchar *queryreal; /* with characters escaped and such */
56 	GRegex *regex;
57 	gchar *replace; /* enabled if not NULL */
58 	gchar *replacereal; /* with characters escaped and such */
59 	gboolean replaceall; /* set to TRUE bluefish will immediately (while searching) do the replace */
60 	gboolean findall; /* only used when find all in files is used */
61 	Tsnr3type type;
62 	Tsnr3replace replacetype;
63 	Tsnr3scope scope;
64 	GFile *basedir; /* when replace in files */
65 	gchar *filepattern;
66 	gint recursion_level;
67 	gboolean is_case_sens;
68 /*	gboolean overlapping;*/
69 	gboolean escape_chars;
70 	gboolean dotmatchall;
71 	gboolean select_matches;
72 	gboolean bookmark_matches;
73 	gboolean showinoutputbox;
74 	gboolean ignorebackupfiles;
75 
76 	/* the resultss of a search run */
77 	gboolean in_replace; /* TRUE if the code is in a replace, so the doc_insert and doc_delete signals do not need to do anything */
78 	guint files_resultcount; /*  the number of elements in files */
79 	GQueue results; /* all results, except for search/replace in files */
80 	GList *current; /* current result, used in replace, or when pressing next or previous */
81 	guint resultnumdoc; /* the number of unique documents in the resultset */
82 	guint searchednumdoc; /* the number of documents searched */
83 
84 	guint unre_action_id;
85 
86 	/* following entries are used during the search run */
87 	Tdocument *curdoc; /* the current document */
88 	gchar *curbuf; /* the current buffer */
89 	gint curoffset; /* when running replace all, the difference between the offset in curbuf and the offset in the text widget */
90 	guint curposition; /* the position in curbuf to continue the next search run, used if the first
91 							search run took longer than our maximum-allowed-gui-block-time */
92 	guint so; /* area to search in */
93 	guint eo; /* see so */
94 	void (*callback) (gpointer data);	/* to be called when the search has finished */
95 	guint idle_id;
96 	guint changed_idle_id;
97 	Tasyncqueue idlequeue;
98 	Tasyncqueue threadqueue;
99 	volatile gint runcount;
100 	volatile gint cancelled;
101 	gpointer findfiles; /* a pointer for the return value of findfiles() so we can cancel it */
102 } Tsnr3run;
103 
104 #define S3RUN(var)  ((Tsnr3run *)var)
105 
106 typedef struct {
107 	Tasyncqueue queue;
108 	guint refcount;
109 	volatile gint cancelled;
110 	Tsnr3run *s3run;
111 	void (*callback) (void *);
112 	gpointer findfiles;
113 } Tfilesworker;
114 
115 typedef struct {
116 	gsize pos;
117 	guint line;
118 } Tlineinbuffer;
119 
120 
121 
122 enum {
123 	SNR_RESPONSE_FIND = 0,
124 	SNR_RESPONSE_BACK,
125 	SNR_RESPONSE_REPLACE,
126 	SNR_RESPONSE_REPLACE_ALL,
127 	SNR_RESPONSE_BOOKMARK_ALL,
128 	SNR_RESPONSE_FIND_ALL,
129 };
130 
131 typedef struct {
132 	GtkWidget *dialog;
133 	GtkWidget *expander;
134 	GtkWidget *search;
135 	GtkWidget *searchfeedback;
136 	GtkWidget *replace;
137 	GtkWidget *scope;
138 	GtkWidget *basedir;
139 	GtkWidget *basedirL;
140 	GtkWidget *basedirB;
141 	GtkWidget *filepattern;
142 	GtkWidget *filepatternL;
143 	GtkWidget *recursion_level;
144 	GtkWidget *recursion_levelL;
145 	GtkWidget *countlabel;
146 	GtkWidget *warninglabel;
147 	GtkWidget *searchType;
148 	GtkWidget *replaceType;
149 	GtkWidget *replaceTypeL;
150 	GtkWidget *overlappingMatches;
151 	GtkWidget *matchCase;
152 	GtkWidget *showinoutputbox;
153 	GtkWidget *ignorebackupfiles;
154 	GtkWidget *escapeChars;
155 	GtkWidget *dotmatchall;
156 	GtkWidget *select_match;
157 	GtkWidget *bookmarks;
158 	GtkWidget *findButton;
159 	GtkWidget *backButton;
160 	GtkWidget *findAllButton;
161 	GtkWidget *replaceButton;
162 	GtkWidget *replaceAllButton;
163 	GtkWidget *bookmarkButton;
164 	Tbfwin *bfwin;
165 	Tsnr3run *s3run;
166 } TSNRWin;
167 
168 
169 typedef struct {
170 	gpointer nothing;
171 } Tsnr3config;
172 
173 #define MAX_CONTINUOUS_SEARCH_INTERVAL 0.2
174 
175 void snr3_run(Tsnr3run *s3run, TSNRWin *snrwin, Tdocument *doc, void (*callback)(void *)); /* called from snr3_files.c */
176 void snr3_run_in_doc(Tsnr3run *s3run, Tdocument *doc, gint so, gint eo, gboolean update);
177 void snr3_run_go(Tsnr3run *s3run, gboolean forward);
178 void snr3run_unrun(Tsnr3run *s3run);
179 gpointer simple_search_run(Tbfwin *bfwin, const gchar *string, Tsnr3type type
180 		, gboolean casesens, gboolean dotmatchall, gboolean unescape);
181 void simple_search_next(Tbfwin *bfwin);
182 void simple_search_cancel_free(void *data);
183 void snr3_advanced_dialog(Tbfwin * bfwin, const gchar *searchstring);
184 void snr3_advanced_dialog_files(Tbfwin * bfwin, const gchar *curi);
185 
186 void snr3_run_extern_replace(Tdocument * doc, const gchar * search_pattern, Tsnr3scope scope,
187 							 Tsnr3type type, gboolean is_case_sens, const gchar * replace_pattern,
188 							 gboolean unescape, gboolean dotmatchall);
189 
190 #endif /* #define __SNR3_H_ */
191