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