]> git.lyx.org Git - lyx.git/blob - src/lyxfind.C
f6bad4a97c9594aa9747bec2bffd4c5a1a0d6582
[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
32 #include "support/convert.h"
33
34 #include <sstream>
35
36 using lyx::support::lowercase;
37 using lyx::support::uppercase;
38 using lyx::support::split;
39
40 using lyx::pit_type;
41 using lyx::pos_type;
42
43 using std::advance;
44 using std::ostringstream;
45 using std::string;
46
47
48 namespace {
49
50 bool parse_bool(string & howto)
51 {
52         if (howto.empty())
53                 return false;
54         string var;
55         howto = split(howto, var, ' ');
56         return (var == "1");
57 }
58
59
60 class MatchString : public std::binary_function<Paragraph, lyx::pos_type, bool>
61 {
62 public:
63         MatchString(string const & str, bool cs, bool mw)
64                 : str(str), cs(cs), mw(mw)
65         {}
66
67         // returns true if the specified string is at the specified position
68         bool operator()(Paragraph const & par, lyx::pos_type pos) const
69         {
70                 string::size_type const size = str.length();
71                 lyx::pos_type i = 0;
72                 lyx::pos_type const parsize = par.size();
73                 for (i = 0; pos + i < parsize; ++i) {
74                         if (string::size_type(i) >= size)
75                                 break;
76                         if (cs && str[i] != par.getChar(pos + i))
77                                 break;
78                         if (!cs && uppercase(str[i]) != uppercase(par.getChar(pos + i)))
79                                 break;
80                 }
81
82                 if (size != string::size_type(i))
83                         return false;
84
85                 // if necessary, check whether string matches word
86                 if (mw) {
87                         if (pos > 0 && par.isLetter(pos - 1))
88                                 return false;
89                         if (pos + lyx::pos_type(size) < parsize
90                             && par.isLetter(pos + size))
91                                 return false;
92                 }
93
94                 return true;
95         }
96
97 private:
98         // search string
99         string str;
100         // case sensitive
101         bool cs;
102         // match whole words only
103         bool mw;
104 };
105
106
107 bool findForward(DocIterator & cur, MatchString const & match)
108 {
109         for (; cur; cur.forwardChar())
110                 if (cur.inTexted() && match(cur.paragraph(), cur.pos()))
111                         return true;
112         return false;
113 }
114
115
116 bool findBackwards(DocIterator & cur, MatchString const & match)
117 {
118         while (cur) {
119                 cur.backwardChar();
120                 if (cur.inTexted() && match(cur.paragraph(), cur.pos()))
121                         return true;
122         }
123         return false;
124 }
125
126
127 bool findChange(DocIterator & cur)
128 {
129         for (; cur; cur.forwardPos())
130                 if (cur.inTexted() &&
131                     cur.paragraph().lookupChange(cur.pos()).type != 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                 lyx::frontend::Alert::error(_("Search error"),
141                                             _("Search string is empty"));
142                 return false;
143         }
144         return bv->buffer();
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);
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                                                             buf.params().trackChanges);
190                 cur.paragraph().insert(pos, replacestr, font,
191                                        Change(buf.params().trackChanges ?
192                                               Change::INSERTED : Change::UNCHANGED));
193                 for (int i = 0; i < rsize + striked; ++i)
194                         cur.forwardChar();
195                 ++num;
196         }
197
198         bv->buffer()->text().init(bv);
199         bv->putSelectionAt(doc_iterator_begin(buf.inset()), 0, false);
200         if (num)
201                 buf.markDirty();
202         return num;
203 }
204
205
206 bool stringSelected(BufferView * bv, string const & searchstr,
207                     bool cs, bool mw, bool fw)
208 {
209         // if nothing selected or selection does not equal search
210         // string search and select next occurance and return
211         string const & str1 = searchstr;
212         string const str2 = lyx::to_utf8(bv->cursor().selectionAsString(false));
213         if ((cs && str1 != str2) || lowercase(str1) != lowercase(str2)) {
214                 find(bv, searchstr, cs, mw, fw);
215                 return false;
216         }
217
218         return true;
219 }
220
221
222 int replace(BufferView * bv, string const & searchstr,
223             string const & replacestr, bool cs, bool mw, bool fw)
224 {
225         if (!searchAllowed(bv, searchstr) || bv->buffer()->isReadonly())
226                 return 0;
227
228         if (!stringSelected(bv, searchstr, cs, mw, fw))
229                 return 0;
230
231         LCursor & cur = bv->cursor();
232         lyx::cap::replaceSelectionWithString(cur, replacestr, fw);
233         bv->buffer()->markDirty();
234         find(bv, searchstr, cs, mw, fw);
235         bv->update();
236
237         return 1;
238 }
239
240 } // namespace anon
241
242
243 namespace lyx {
244 namespace find {
245
246 string const find2string(string const & search,
247                          bool casesensitive, bool matchword, bool forward)
248 {
249         ostringstream ss;
250         ss << search << '\n'
251            << int(casesensitive) << ' '
252            << int(matchword) << ' '
253            << int(forward);
254         return ss.str();
255 }
256
257
258 string const replace2string(string const & search, string const & replace,
259                             bool casesensitive, bool matchword,
260                             bool all, bool forward)
261 {
262         ostringstream ss;
263         ss << search << '\n'
264            << replace << '\n'
265            << int(casesensitive) << ' '
266            << int(matchword) << ' '
267            << int(all) << ' '
268            << int(forward);
269         return ss.str();
270 }
271
272
273 void find(BufferView * bv, FuncRequest const & ev)
274 {
275         if (!bv || ev.action != LFUN_WORD_FIND)
276                 return;
277
278         //lyxerr << "find called, cmd: " << ev << std::endl;
279
280         // data is of the form
281         // "<search>
282         //  <casesensitive> <matchword> <forward>"
283         string search;
284         string howto = split(lyx::to_utf8(ev.argument()), search, '\n');
285
286         bool casesensitive = parse_bool(howto);
287         bool matchword     = parse_bool(howto);
288         bool forward       = parse_bool(howto);
289
290         bool const found = ::find(bv, search,
291                                   casesensitive, matchword, forward);
292
293         if (!found)
294                 // emit message signal.
295                 bv->message(_("String not found!"));
296 }
297
298
299 void replace(BufferView * bv, FuncRequest const & ev)
300 {
301         if (!bv || ev.action != LFUN_WORD_REPLACE)
302                 return;
303
304         // data is of the form
305         // "<search>
306         //  <replace>
307         //  <casesensitive> <matchword> <all> <forward>"
308         string search;
309         string replace;
310         string howto = split(lyx::to_utf8(ev.argument()), search, '\n');
311         howto = split(howto, replace, '\n');
312
313         bool casesensitive = parse_bool(howto);
314         bool matchword     = parse_bool(howto);
315         bool all           = parse_bool(howto);
316         bool forward       = parse_bool(howto);
317
318         Buffer * buf = bv->buffer();
319
320         int const replace_count = all
321                 ? ::replaceAll(bv, search, replace, casesensitive, matchword)
322                 : ::replace(bv, search, replace, casesensitive, matchword, forward);
323
324         if (replace_count == 0) {
325                 // emit message signal.
326                 buf->message(_("String not found!"));
327         } else {
328                 if (replace_count == 1) {
329                         // emit message signal.
330                         buf->message(_("String has been replaced."));
331                 } else {
332                         docstring str = convert<docstring>(replace_count);
333                         str += _(" strings have been replaced.");
334                         // emit message signal.
335                         buf->message(str);
336                 }
337         }
338 }
339
340
341 bool findNextChange(BufferView * bv)
342 {
343         if (!bv->buffer())
344                 return false;
345
346         DocIterator cur = bv->cursor();
347
348         if (!findChange(cur))
349                 return false;
350
351         bv->cursor().setCursor(cur);
352         bv->cursor().resetAnchor();
353
354         Change orig_change = cur.paragraph().lookupChange(cur.pos());
355
356         DocIterator et = doc_iterator_end(cur.inset());
357         for (; cur != et; cur.forwardPosNoDescend()) {
358                 Change change = cur.paragraph().lookupChange(cur.pos());
359                 if (change != orig_change) {
360                         break;
361                 }
362         }
363         // Now put cursor to end of selection:
364         bv->cursor().setCursor(cur);
365         bv->cursor().setSelection();
366         // if we used a lfun like in find/replace, dispatch would do
367         // that for us
368         bv->update();
369
370         return true;
371 }
372
373 } // find namespace
374 } // lyx namespace