]> git.lyx.org Git - lyx.git/blob - src/lyxfind.C
remove LFUN_TOOLTIPS_TOGGLE and associated Dialog::tooltipsEnabled() method.
[lyx.git] / src / lyxfind.C
1 /**
2  * \file lyxfind.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author John Levon
8  * \author Jürgen Vigna
9  * \author Alfredo Braunstein
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "lyxfind.h"
17
18 #include "buffer.h"
19 #include "cursor.h"
20 #include "CutAndPaste.h"
21 #include "BufferView.h"
22 #include "debug.h"
23 #include "funcrequest.h"
24 #include "gettext.h"
25 #include "lyxtext.h"
26 #include "paragraph.h"
27 #include "pariterator.h"
28 #include "undo.h"
29
30 #include "frontends/Alert.h"
31
32 #include "support/convert.h"
33
34 #include <sstream>
35
36 using lyx::support::lowercase;
37 using lyx::support::uppercase;
38 using lyx::support::split;
39
40 using lyx::pit_type;
41 using lyx::pos_type;
42
43 using std::advance;
44 using std::ostringstream;
45 using std::string;
46
47
48 namespace {
49
50 bool parse_bool(string & howto)
51 {
52         if (howto.empty())
53                 return false;
54         string var;
55         howto = split(howto, var, ' ');
56         return (var == "1");
57 }
58
59
60 class MatchString : public std::binary_function<Paragraph, lyx::pos_type, bool>
61 {
62 public:
63         MatchString(string const & str, bool cs, bool mw)
64                 : str(str), cs(cs), mw(mw)
65         {}
66
67         // returns true if the specified string is at the specified position
68         bool operator()(Paragraph const & par, lyx::pos_type pos) const
69         {
70                 string::size_type const size = str.length();
71                 lyx::pos_type i = 0;
72                 lyx::pos_type const parsize = par.size();
73                 for (i = 0; pos + i < parsize; ++i) {
74                         if (string::size_type(i) >= size)
75                                 break;
76                         if (cs && str[i] != par.getChar(pos + i))
77                                 break;
78                         if (!cs && uppercase(str[i]) != uppercase(par.getChar(pos + i)))
79                                 break;
80                 }
81
82                 if (size != string::size_type(i))
83                         return false;
84
85                 // if necessary, check whether string matches word
86                 if (mw) {
87                         if (pos > 0 && par.isLetter(pos - 1))
88                                 return false;
89                         if (pos + lyx::pos_type(size) < parsize
90                             && par.isLetter(pos + size))
91                                 return false;
92                 }
93
94                 return true;
95         }
96
97 private:
98         // search string
99         string str;
100         // case sensitive
101         bool cs;
102         // match whole words only
103         bool mw;
104 };
105
106
107 bool findForward(DocIterator & cur, MatchString const & match)
108 {
109         for (; cur; cur.forwardChar())
110                 if (cur.inTexted() && match(cur.paragraph(), cur.pos()))
111                         return true;
112         return false;
113 }
114
115
116 bool findBackwards(DocIterator & cur, MatchString const & match)
117 {
118         while (cur) {
119                 cur.backwardChar();
120                 if (cur.inTexted() && match(cur.paragraph(), cur.pos()))
121                         return true;
122         }
123         return false;
124 }
125
126
127 bool findChange(DocIterator & cur)
128 {
129         for (; cur; cur.forwardPos())
130                 if (cur.inTexted() && cur.paragraph().lookupChange(cur.pos()).type
131                     != Change::UNCHANGED)
132                         return true;
133         return false;
134 }
135
136
137 bool searchAllowed(BufferView * bv, string const & str)
138 {
139         if (str.empty()) {
140                 Alert::error(_("Search error"), _("Search string is empty"));
141                 return false;
142         }
143         return bv->buffer();
144 }
145
146
147 bool find(BufferView * bv, string const & searchstr, bool cs, bool mw, bool fw)
148 {
149         if (!searchAllowed(bv, searchstr))
150                 return false;
151
152         DocIterator cur = bv->cursor();
153
154         MatchString const match(searchstr, cs, mw);
155
156         bool found = fw ? findForward(cur, match) : findBackwards(cur, match);
157
158         if (found)
159                 bv->putSelectionAt(cur, searchstr.length(), !fw);
160
161         return found;
162 }
163
164
165 int replaceAll(BufferView * bv,
166                string const & searchstr, string const & replacestr,
167                bool cs, bool mw)
168 {
169         Buffer & buf = *bv->buffer();
170
171         if (!searchAllowed(bv, searchstr) || buf.isReadonly())
172                 return 0;
173
174         recordUndoFullDocument(bv);
175
176         MatchString const match(searchstr, cs, mw);
177         int num = 0;
178
179         int const rsize = replacestr.size();
180         int const ssize = searchstr.size();
181
182         DocIterator cur = doc_iterator_begin(buf.inset());
183         while (findForward(cur, match)) {
184                 lyx::pos_type pos = cur.pos();
185                 LyXFont const font
186                         = cur.paragraph().getFontSettings(buf.params(), pos);
187                 int striked = ssize - cur.paragraph().erase(pos, pos + ssize);
188                 cur.paragraph().insert(pos, replacestr, font);
189                 for (int i = 0; i < rsize + striked; ++i)
190                         cur.forwardChar();
191                 ++num;
192         }
193
194         bv->buffer()->text().init(bv);
195         bv->putSelectionAt(doc_iterator_begin(buf.inset()), 0, false);
196         if (num)
197                 buf.markDirty();
198         return num;
199 }
200
201
202 bool stringSelected(BufferView * bv, string const & searchstr,
203                     bool cs, bool mw, bool fw)
204 {
205         // if nothing selected or selection does not equal search
206         // string search and select next occurance and return
207         string const & str1 = searchstr;
208         string const str2 = lyx::to_utf8(bv->cursor().selectionAsString(false));
209         if ((cs && str1 != str2) || lowercase(str1) != lowercase(str2)) {
210                 find(bv, searchstr, cs, mw, fw);
211                 return false;
212         }
213
214         return true;
215 }
216
217
218 int replace(BufferView * bv, string const & searchstr,
219             string const & replacestr, bool cs, bool mw, bool fw)
220 {
221         if (!searchAllowed(bv, searchstr) || bv->buffer()->isReadonly())
222                 return 0;
223
224         if (!stringSelected(bv, searchstr, cs, mw, fw))
225                 return 0;
226
227         LCursor & cur = bv->cursor();
228         lyx::cap::replaceSelectionWithString(cur, replacestr, fw);
229         bv->buffer()->markDirty();
230         find(bv, searchstr, cs, mw, fw);
231         bv->update();
232
233         return 1;
234 }
235
236 } // namespace anon
237
238
239 namespace lyx {
240 namespace find {
241
242 string const find2string(string const & search,
243                          bool casesensitive, bool matchword, bool forward)
244 {
245         ostringstream ss;
246         ss << search << '\n'
247            << int(casesensitive) << ' '
248            << int(matchword) << ' '
249            << int(forward);
250         return ss.str();
251 }
252
253
254 string const replace2string(string const & search, string const & replace,
255                             bool casesensitive, bool matchword,
256                             bool all, bool forward)
257 {
258         ostringstream ss;
259         ss << search << '\n'
260            << replace << '\n'
261            << int(casesensitive) << ' '
262            << int(matchword) << ' '
263            << int(all) << ' '
264            << int(forward);
265         return ss.str();
266 }
267
268
269 void find(BufferView * bv, FuncRequest const & ev)
270 {
271         if (!bv || ev.action != LFUN_WORD_FIND)
272                 return;
273
274         //lyxerr << "find called, cmd: " << ev << std::endl;
275
276         // data is of the form
277         // "<search>
278         //  <casesensitive> <matchword> <forward>"
279         string search;
280         string howto = split(lyx::to_utf8(ev.argument()), search, '\n');
281
282         bool casesensitive = parse_bool(howto);
283         bool matchword     = parse_bool(howto);
284         bool forward       = parse_bool(howto);
285
286         bool const found = ::find(bv, search,
287                                   casesensitive, matchword, forward);
288
289         if (!found)
290                 // emit message signal.
291                 bv->message(_("String not found!"));
292 }
293
294
295 void replace(BufferView * bv, FuncRequest const & ev)
296 {
297         if (!bv || ev.action != LFUN_WORD_REPLACE)
298                 return;
299
300         // data is of the form
301         // "<search>
302         //  <replace>
303         //  <casesensitive> <matchword> <all> <forward>"
304         string search;
305         string replace;
306         string howto = split(lyx::to_utf8(ev.argument()), search, '\n');
307         howto = split(howto, replace, '\n');
308
309         bool casesensitive = parse_bool(howto);
310         bool matchword     = parse_bool(howto);
311         bool all           = parse_bool(howto);
312         bool forward       = parse_bool(howto);
313
314         Buffer * buf = bv->buffer();
315
316         int const replace_count = all
317                 ? ::replaceAll(bv, search, replace, casesensitive, matchword)
318                 : ::replace(bv, search, replace, casesensitive, matchword, forward);
319
320         if (replace_count == 0) {
321                 // emit message signal.
322                 buf->message(_("String not found!"));
323         } else {
324                 if (replace_count == 1) {
325                         // emit message signal.
326                         buf->message(_("String has been replaced."));
327                 } else {
328                         docstring str = convert<docstring>(replace_count);
329                         str += _(" strings have been replaced.");
330                         // emit message signal.
331                         buf->message(str);
332                 }
333         }
334 }
335
336
337 bool findNextChange(BufferView * bv)
338 {
339         if (!bv->buffer())
340                 return false;
341
342         DocIterator cur = bv->cursor();
343
344         if (!findChange(cur))
345                 return false;
346
347         bv->cursor().setCursor(cur);
348         bv->cursor().resetAnchor();
349
350         Change orig_change = cur.paragraph().lookupChange(cur.pos());
351
352         DocIterator et = doc_iterator_end(cur.inset());
353         for (; cur != et; cur.forwardPosNoDescend()) {
354                 Change change = cur.paragraph().lookupChange(cur.pos());
355                 if (change != orig_change) {
356                         break;
357                 }
358         }
359         // Now put cursor to end of selection:
360         bv->cursor().setCursor(cur);
361         bv->cursor().setSelection();
362         // if we used a lfun like in find/replace, dispatch would do
363         // that for us
364         bv->update();
365
366         return true;
367 }
368
369 } // find namespace
370 } // lyx namespace