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