]> git.lyx.org Git - lyx.git/blob - src/lyxfind.C
some integer type changes for inset unification
[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 "BufferView.h"
20 #include "debug.h"
21 #include "iterators.h"
22 #include "funcrequest.h"
23 #include "gettext.h"
24 #include "lyxtext.h"
25 #include "paragraph.h"
26 #include "PosIterator.h"
27 #include "undo.h"
28
29 #include "frontends/Alert.h"
30 #include "frontends/LyXView.h"
31
32 #include "support/textutils.h"
33 #include "support/tostr.h"
34
35 #include "support/std_sstream.h"
36
37 using lyx::support::lowercase;
38 using lyx::support::uppercase;
39 using lyx::support::split;
40
41 using bv_funcs::put_selection_at;
42
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(FuncRequest const & ev)
111 {
112         if (!ev.view() || 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         BufferView * bv = ev.view();
126         bool const found = ::find(bv, search,
127                                   forward, casesensitive, matchword);
128
129         if (!found)
130                 bv->owner()->message(_("String not found!"));
131 }
132
133
134 void replace(FuncRequest const & ev)
135 {
136         if (!ev.view() || ev.action != LFUN_WORD_REPLACE)
137                 return;
138
139         // data is of the form
140         // "<search>
141         //  <replace>
142         //  <casesensitive> <matchword> <all> <forward>"
143         string search;
144         string replace;
145         string howto = split(ev.argument, search, '\n');
146         howto = split(howto, replace, '\n');
147
148         bool casesensitive = parse_bool(howto);
149         bool matchword     = parse_bool(howto);
150         bool all           = parse_bool(howto);
151         bool forward       = parse_bool(howto);
152
153         BufferView * bv = ev.view();
154         LyXView * lv = bv->owner();
155
156         int const replace_count = all ?
157                 ::replaceAll(bv, search, replace,
158                              casesensitive, matchword) :
159                 ::replace(bv, search, replace,
160                           casesensitive, matchword, forward);
161  
162         if (replace_count == 0) {
163                 lv->message(_("String not found!"));
164         } else {
165                 if (replace_count == 1) {
166                         lv->message(_("String has been replaced."));
167                 } else {
168                         string str = tostr(replace_count);
169                         str += _(" strings have been replaced.");
170                         lv->message(str);
171                 }
172         }
173 }
174
175
176 bool findNextChange(BufferView * bv)
177 {
178         if (!bv->available())
179                 return false;
180
181         PosIterator cur = PosIterator(*bv);
182         PosIterator const endit = bv->buffer()->pos_iterator_end();
183
184         if (!findChange(cur, endit))
185                 return false;
186         
187         ParagraphList::iterator pit = cur.pit();
188         pos_type pos = cur.pos();
189         
190         Change orig_change = pit->lookupChangeFull(pos);
191         pos_type parsize = pit->size();
192         pos_type end = pos;
193
194         for (; end != parsize; ++end) {
195                 Change change = pit->lookupChangeFull(end);
196                 if (change != orig_change) {
197                         // slight UI optimisation: for replacements, we get
198                         // text like : _old_new. Consider that as one change.
199                         if (!(orig_change.type == Change::DELETED &&
200                                 change.type == Change::INSERTED))
201                                 break;
202                 }
203         }
204         pos_type length = end - pos;
205         put_selection_at(bv, cur, length, true);
206         return true;
207 }
208
209 } // find namespace
210 } // lyx namespace
211
212
213 namespace {
214
215 class MatchString
216 {
217 public:
218         MatchString(string const & str, bool cs, bool mw)
219                 : str(str), cs(cs), mw(mw)
220         {}
221
222         // returns true if the specified string is at the specified position
223         bool operator()(Paragraph const & par, lyx::pos_type pos) const
224         {
225                 string::size_type const size = str.length();
226                 lyx::pos_type i = 0;
227                 lyx::pos_type const parsize = par.size();
228                 while ((pos + i < parsize)
229                        && (string::size_type(i) < size)
230                        && (cs ? (str[i] == par.getChar(pos + i))
231                            : (uppercase(str[i]) == uppercase(par.getChar(pos + i))))) {
232                         ++i;
233                 }
234
235                 if (size != string::size_type(i))
236                         return false;
237
238                 // if necessary, check whether string matches word
239                 if (mw) {
240                         if (pos > 0     && IsLetterCharOrDigit(par.getChar(pos - 1)))
241                                 return false;
242                         if (pos + lyx::pos_type(size) < parsize
243                                         && IsLetterCharOrDigit(par.getChar(pos + size)));
244                                 return false;
245                 }
246
247                 return true;
248         }
249         
250 private:
251         // search string
252         string str;
253         // case sensitive
254         bool cs;
255         // match whole words only
256         bool mw;
257 };
258
259
260 bool findForward(PosIterator & cur, PosIterator const & end,
261                  MatchString const & match)
262 {
263         for (; cur != end; ++cur) {
264                 if (match(*cur.pit(), cur.pos()))
265                         return true;
266         }
267         return false;
268 }
269
270
271 bool findBackwards(PosIterator & cur, PosIterator const & beg,
272                    MatchString const & match)
273 {
274         while (beg != cur) {
275                 --cur;
276                 if (match(*cur.pit(), cur.pos()))
277                         return true;
278         }
279         return false;
280 }
281
282
283 bool findChange(PosIterator & cur, PosIterator const & end)
284 {
285         for (; cur != end; ++cur) {
286                 if ((!cur.pit()->size() || !cur.at_end())
287                     && cur.pit()->lookupChange(cur.pos()) != Change::UNCHANGED)
288                         return true;
289         }
290         return false;
291 }
292
293
294 bool searchAllowed(BufferView * bv, string const & str)
295 {
296         if (str.empty()) {
297                 Alert::error(_("Search error"), _("Search string is empty"));
298                 return false;
299         }
300         return bv->available();
301 }
302
303
304 bool find(BufferView * bv, string const & searchstr, bool cs, bool mw, bool fw)
305 {
306         if (!searchAllowed(bv, searchstr))
307                 return false;
308
309         PosIterator cur = PosIterator(*bv);
310
311         MatchString const match(searchstr, cs, mw);
312
313         PosIterator const end = bv->buffer()->pos_iterator_end();
314         PosIterator const beg = bv->buffer()->pos_iterator_begin();
315
316         bool found = fw ? findForward(cur, end, match)
317                 : findBackwards(cur, beg, match);
318
319         if (found)
320                 put_selection_at(bv, cur, searchstr.length(), !fw);
321
322         return found;
323 }
324
325
326 int replaceAll(BufferView * bv,
327                string const & searchstr, string const & replacestr,
328                bool cs, bool mw)
329 {
330         Buffer & buf = *bv->buffer();
331
332         if (!searchAllowed(bv, searchstr) || buf.isReadonly())
333                 return 0;
334         
335         recordUndo(Undo::ATOMIC, bv->text(), 0, buf.paragraphs().size() - 1);
336         
337         PosIterator cur = buf.pos_iterator_begin();
338         PosIterator const end = buf.pos_iterator_end();
339         MatchString const match(searchstr, cs, mw);
340         int num = 0;
341
342         int const rsize = replacestr.size();
343         int const ssize = searchstr.size();
344
345         while (findForward(cur, end, match)) {
346                 lyx::pos_type pos = cur.pos();
347                 LyXFont const font
348                         = cur.pit()->getFontSettings(buf.params(), pos);
349                 int striked = ssize - cur.pit()->erase(pos, pos + ssize);
350                 cur.pit()->insert(pos, replacestr, font);
351                 advance(cur, rsize + striked);
352                 ++num;
353         }
354
355         PosIterator beg = buf.pos_iterator_begin();
356         bv->text()->init(bv);
357         put_selection_at(bv, beg, 0, false);
358         if (num)
359                 buf.markDirty();
360         return num;
361 }
362
363
364 bool stringSelected(BufferView * bv,
365                     string const & searchstr, 
366                     bool cs, bool mw, bool fw)
367 {
368         LyXText * text = bv->getLyXText();
369         // if nothing selected or selection does not equal search
370         // string search and select next occurance and return
371         string const & str1 = searchstr;
372         string const str2 = text->selectionAsString(*bv->buffer(),
373                                                     false);
374         if ((cs && str1 != str2) || lowercase(str1) != lowercase(str2)) {
375                 find(bv, searchstr, cs, mw, fw);
376                 return false;
377         }
378
379         return true;
380 }
381
382
383 int replace(BufferView * bv,
384             string const & searchstr, string const & replacestr,
385             bool cs, bool mw, bool fw)
386 {
387         if (!searchAllowed(bv, searchstr) || bv->buffer()->isReadonly())
388                 return 0;
389
390         if (!stringSelected(bv, searchstr, cs, mw, fw))
391                 return 0;
392
393         LyXText * text = bv->getLyXText();
394
395         text->replaceSelectionWithString(replacestr);
396         text->setSelectionRange(replacestr.length());
397         text->cursor() = fw ? text->selEnd() : text->selStart();
398
399         bv->buffer()->markDirty();
400         find(bv, searchstr, cs, mw, fw);
401         bv->update();
402         
403         return 1;
404 }
405
406 } //namespace anon