]> git.lyx.org Git - lyx.git/blob - src/lyxfind.C
Add support for the jurabib package (www.jurabib.org), a package for elegant BibTeX...
[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                 for (i = 0; pos + i < parsize; ++i) {
227                         if (string::size_type(i) >= size)
228                                 break;
229                   if (cs && str[i] != par.getChar(pos + i))
230                                 break;
231                         if (!cs && uppercase(str[i]) != uppercase(par.getChar(pos + i)))
232                                 break;
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()->empty() || !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                 bv->putSelectionAt(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         recordUndoFullDocument(bv->cursor());
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         bv->putSelectionAt(beg, 0, false);
358         if (num)
359                 buf.markDirty();
360         return num;
361 }
362
363
364 bool stringSelected(BufferView * bv, string const & searchstr,
365                     bool cs, bool mw, bool fw)
366 {
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 = bv->cursor().selectionAsString(false);
371         if ((cs && str1 != str2) || lowercase(str1) != lowercase(str2)) {
372                 find(bv, searchstr, cs, mw, fw);
373                 return false;
374         }
375
376         return true;
377 }
378
379
380 int replace(BufferView * bv, string const & searchstr,
381       string const & replacestr, bool cs, bool mw, bool fw)
382 {
383         if (!searchAllowed(bv, searchstr) || bv->buffer()->isReadonly())
384                 return 0;
385
386         if (!stringSelected(bv, searchstr, cs, mw, fw))
387                 return 0;
388
389         LyXText * text = bv->getLyXText();
390
391         text->replaceSelectionWithString(bv->cursor(), replacestr);
392         text->setSelectionRange(bv->cursor(), replacestr.length());
393         bv->cursor().top() = fw ? bv->cursor().selEnd() : bv->cursor().selBegin();
394         bv->buffer()->markDirty();
395         find(bv, searchstr, cs, mw, fw);
396         bv->update();
397
398         return 1;
399 }
400
401 } //namespace anon