]> git.lyx.org Git - lyx.git/blob - src/lyxfind.C
the pariterator stuff
[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(DocumentIterator & cur, MatchString const & match)
110 {
111         for (; cur.size(); cur.forwardChar())
112                 if (match(cur.paragraph(), cur.pos()))
113                         return true;
114         return false;
115 }
116
117
118 bool findBackwards(DocumentIterator & cur, MatchString const & match)
119 {
120         for (; cur.size(); cur.backwardChar())
121                 if (match(cur.paragraph(), cur.pos()))
122                         return true;
123         return false;
124 }
125
126
127 bool findChange(DocumentIterator & cur)
128 {
129         for (; cur.size(); cur.forwardChar())
130                 if ((cur.paragraph().empty() || !cur.empty())
131                     && cur.paragraph().lookupChange(cur.pos()) != 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->available();
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         DocumentIterator 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->cursor());
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         DocumentIterator cur = DocumentIterator(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->text()->init(bv);
195         bv->putSelectionAt(DocumentIterator(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 = 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);
229         lyx::cap::setSelectionRange(cur, replacestr.length());
230         cur.top() = fw ? cur.selEnd() : cur.selBegin();
231         bv->buffer()->markDirty();
232         find(bv, searchstr, cs, mw, fw);
233         bv->update();
234
235         return 1;
236 }
237
238 } // namespace anon
239
240
241 namespace lyx {
242 namespace find {
243
244 string const find2string(string const & search,
245                          bool casesensitive, bool matchword, bool forward)
246 {
247         ostringstream ss;
248         ss << search << '\n'
249            << int(casesensitive) << ' '
250            << int(matchword) << ' '
251            << int(forward);
252         return ss.str();
253 }
254
255
256 string const replace2string(string const & search, string const & replace,
257                             bool casesensitive, bool matchword,
258                             bool all, bool forward)
259 {
260         ostringstream ss;
261         ss << search << '\n'
262            << replace << '\n'
263            << int(casesensitive) << ' '
264            << int(matchword) << ' '
265            << int(all) << ' '
266            << int(forward);
267         return ss.str();
268 }
269
270
271 void find(BufferView * bv, FuncRequest const & ev)
272 {
273         if (!bv || ev.action != LFUN_WORD_FIND)
274                 return;
275
276         lyxerr << "find called, cmd: " << ev << std::endl;
277
278         // data is of the form
279         // "<search>
280         //  <casesensitive> <matchword> <forward>"
281         string search;
282         string howto = split(ev.argument, search, '\n');
283
284         bool casesensitive = parse_bool(howto);
285         bool matchword     = parse_bool(howto);
286         bool forward       = parse_bool(howto);
287
288         bool const found = ::find(bv, search,
289                                   casesensitive, matchword, forward);
290
291         if (!found)
292                 bv->owner()->message(_("String not found!"));
293 }
294
295
296 void replace(BufferView * bv, FuncRequest const & ev)
297 {
298         if (!bv || ev.action != LFUN_WORD_REPLACE)
299                 return;
300
301         // data is of the form
302         // "<search>
303         //  <replace>
304         //  <casesensitive> <matchword> <all> <forward>"
305         string search;
306         string replace;
307         string howto = split(ev.argument, search, '\n');
308         howto = split(howto, replace, '\n');
309
310         bool casesensitive = parse_bool(howto);
311         bool matchword     = parse_bool(howto);
312         bool all           = parse_bool(howto);
313         bool forward       = parse_bool(howto);
314
315         LyXView * lv = bv->owner();
316
317         int const replace_count = all
318                 ? ::replaceAll(bv, search, replace, casesensitive, matchword)
319                 : ::replace(bv, search, replace, casesensitive, matchword, forward);
320
321         if (replace_count == 0) {
322                 lv->message(_("String not found!"));
323         } else {
324                 if (replace_count == 1) {
325                         lv->message(_("String has been replaced."));
326                 } else {
327                         string str = tostr(replace_count);
328                         str += _(" strings have been replaced.");
329                         lv->message(str);
330                 }
331         }
332 }
333
334
335 bool findNextChange(BufferView * bv)
336 {
337         if (!bv->available())
338                 return false;
339
340         DocumentIterator cur = DocumentIterator(bv->buffer()->inset());
341
342         if (!findChange(cur))
343                 return false;
344
345         Paragraph const & par = cur.paragraph();
346         pos_type pos = cur.pos();
347
348         Change orig_change = par.lookupChangeFull(pos);
349         pos_type parsize = par.size();
350         pos_type end = pos;
351
352         for (; end != parsize; ++end) {
353                 Change change = par.lookupChangeFull(end);
354                 if (change != orig_change) {
355                         // slight UI optimisation: for replacements, we get
356                         // text like : _old_new. Consider that as one change.
357                         if (!(orig_change.type == Change::DELETED &&
358                                 change.type == Change::INSERTED))
359                                 break;
360                 }
361         }
362         pos_type length = end - pos;
363         bv->putSelectionAt(cur, length, true);
364         return true;
365 }
366
367 } // find namespace
368 } // lyx namespace
369
370