]> git.lyx.org Git - lyx.git/blob - src/lyxfind.cpp
Clean-up in CharStyles, externalize detection of undefined styles to
[lyx.git] / src / lyxfind.cpp
1 /**
2  * \file lyxfind.cpp
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 "buffer_funcs.h"
22 #include "BufferView.h"
23 #include "debug.h"
24 #include "FuncRequest.h"
25 #include "gettext.h"
26 #include "Text.h"
27 #include "Paragraph.h"
28 #include "ParIterator.h"
29 #include "Undo.h"
30
31 #include "frontends/alert.h"
32
33 #include "support/convert.h"
34 #include "support/docstream.h"
35
36 namespace lyx {
37
38 using support::compare_no_case;
39 using support::uppercase;
40 using support::split;
41
42 using std::advance;
43
44
45 namespace {
46
47 bool parse_bool(docstring & howto)
48 {
49         if (howto.empty())
50                 return false;
51         docstring var;
52         howto = split(howto, var, ' ');
53         return (var == "1");
54 }
55
56
57 class MatchString : public std::binary_function<Paragraph, pos_type, bool>
58 {
59 public:
60         MatchString(docstring const & str, bool cs, bool mw)
61                 : str(str), cs(cs), mw(mw)
62         {}
63
64         // returns true if the specified string is at the specified position
65         // del specifies whether deleted strings in ct mode will be considered
66         bool operator()(Paragraph const & par, pos_type pos, bool del = true) const
67         {
68                 docstring::size_type const size = str.length();
69                 pos_type i = 0;
70                 pos_type const parsize = par.size();
71                 for (i = 0; pos + i < parsize; ++i) {
72                         if (docstring::size_type(i) >= size)
73                                 break;
74                         if (cs && str[i] != par.getChar(pos + i))
75                                 break;
76                         if (!cs && uppercase(str[i]) != uppercase(par.getChar(pos + i)))
77                                 break;
78                         if (!del && par.isDeleted(pos + i))
79                                 break;
80                 }
81
82                 if (size != docstring::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 + 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         docstring 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                  bool find_del = true)
109 {
110         for (; cur; cur.forwardChar())
111                 if (cur.inTexted() &&
112                     match(cur.paragraph(), cur.pos(), find_del))
113                         return true;
114         return false;
115 }
116
117
118 bool findBackwards(DocIterator & cur, MatchString const & match,
119                  bool find_del = true)
120 {
121         while (cur) {
122                 cur.backwardChar();
123                 if (cur.inTexted() &&
124                     match(cur.paragraph(), cur.pos(), find_del))
125                         return true;
126         }
127         return false;
128 }
129
130
131 bool findChange(DocIterator & cur)
132 {
133         for (; cur; cur.forwardPos())
134                 if (cur.inTexted() && !cur.paragraph().isUnchanged(cur.pos()))
135                         return true;
136         return false;
137 }
138
139
140 bool searchAllowed(BufferView * bv, docstring const & str)
141 {
142         if (str.empty()) {
143                 frontend::Alert::error(_("Search error"),
144                                             _("Search string is empty"));
145                 return false;
146         }
147         return true;
148 }
149
150
151 bool find(BufferView * bv, docstring const & searchstr, bool cs, bool mw, bool fw,
152           bool find_del = true)
153 {
154         if (!searchAllowed(bv, searchstr))
155                 return false;
156
157         DocIterator cur = bv->cursor();
158
159         MatchString const match(searchstr, cs, mw);
160
161         bool found = fw ? findForward(cur, match, find_del) :
162                           findBackwards(cur, match, find_del);
163
164         if (found)
165                 bv->putSelectionAt(cur, searchstr.length(), !fw);
166
167         return found;
168 }
169
170
171 int replaceAll(BufferView * bv,
172                docstring const & searchstr, docstring const & replacestr,
173                bool cs, bool mw)
174 {
175         Buffer & buf = bv->buffer();
176
177         if (!searchAllowed(bv, searchstr) || buf.isReadonly())
178                 return 0;
179
180         recordUndoFullDocument(bv);
181
182         MatchString const match(searchstr, cs, mw);
183         int num = 0;
184
185         int const rsize = replacestr.size();
186         int const ssize = searchstr.size();
187
188         DocIterator cur = doc_iterator_begin(buf.inset());
189         while (findForward(cur, match, false)) {
190                 pos_type pos = cur.pos();
191                 Font const font
192                         = cur.paragraph().getFontSettings(buf.params(), pos);
193                 int striked = ssize - cur.paragraph().eraseChars(pos, pos + ssize,
194                                                             buf.params().trackChanges);
195                 cur.paragraph().insert(pos, replacestr, font,
196                                        Change(buf.params().trackChanges ?
197                                               Change::INSERTED : Change::UNCHANGED));
198                 for (int i = 0; i < rsize + striked; ++i)
199                         cur.forwardChar();
200                 ++num;
201         }
202
203         updateLabels(buf);
204         bv->putSelectionAt(doc_iterator_begin(buf.inset()), 0, false);
205         if (num)
206                 buf.markDirty();
207         return num;
208 }
209
210
211 bool stringSelected(BufferView * bv, docstring const & searchstr,
212                     bool cs, bool mw, bool fw)
213 {
214         // if nothing selected or selection does not equal search
215         // string search and select next occurance and return
216         docstring const & str1 = searchstr;
217         docstring const str2 = bv->cursor().selectionAsString(false);
218         if ((cs && str1 != str2) || compare_no_case(str1, str2) != 0) {
219                 find(bv, searchstr, cs, mw, fw);
220                 return false;
221         }
222
223         return true;
224 }
225
226
227 int replace(BufferView * bv, docstring const & searchstr,
228             docstring const & replacestr, bool cs, bool mw, bool fw)
229 {
230         if (!searchAllowed(bv, searchstr) || bv->buffer().isReadonly())
231                 return 0;
232
233         if (!stringSelected(bv, searchstr, cs, mw, fw))
234                 return 0;
235
236         Cursor & cur = bv->cursor();
237         cap::replaceSelectionWithString(cur, replacestr, fw);
238         bv->buffer().markDirty();
239         find(bv, searchstr, cs, mw, fw, false);
240         bv->update();
241
242         return 1;
243 }
244
245 } // namespace anon
246
247
248 docstring const find2string(docstring const & search,
249                          bool casesensitive, bool matchword, bool forward)
250 {
251         odocstringstream ss;
252         ss << search << '\n'
253            << int(casesensitive) << ' '
254            << int(matchword) << ' '
255            << int(forward);
256         return ss.str();
257 }
258
259
260 docstring const replace2string(docstring const & search, docstring const & replace,
261                             bool casesensitive, bool matchword,
262                             bool all, bool forward)
263 {
264         odocstringstream ss;
265         ss << search << '\n'
266            << replace << '\n'
267            << int(casesensitive) << ' '
268            << int(matchword) << ' '
269            << int(all) << ' '
270            << int(forward);
271         return ss.str();
272 }
273
274
275 void find(BufferView * bv, FuncRequest const & ev)
276 {
277         if (!bv || ev.action != LFUN_WORD_FIND)
278                 return;
279
280         //lyxerr << "find called, cmd: " << ev << std::endl;
281
282         // data is of the form
283         // "<search>
284         //  <casesensitive> <matchword> <forward>"
285         docstring search;
286         docstring howto = split(ev.argument(), search, '\n');
287
288         bool casesensitive = parse_bool(howto);
289         bool matchword     = parse_bool(howto);
290         bool forward       = parse_bool(howto);
291
292         bool const found = find(bv, search,
293                                   casesensitive, matchword, forward);
294
295         if (!found)
296                 // emit message signal.
297                 bv->message(_("String not found!"));
298 }
299
300
301 void replace(BufferView * bv, FuncRequest const & ev, bool has_deleted)
302 {
303         if (!bv || ev.action != LFUN_WORD_REPLACE)
304                 return;
305
306         // data is of the form
307         // "<search>
308         //  <replace>
309         //  <casesensitive> <matchword> <all> <forward>"
310         docstring search;
311         docstring rplc;
312         docstring howto = split(ev.argument(), search, '\n');
313         howto = split(howto, rplc, '\n');
314
315         bool casesensitive = parse_bool(howto);
316         bool matchword     = parse_bool(howto);
317         bool all           = parse_bool(howto);
318         bool forward       = parse_bool(howto);
319
320         if (!has_deleted) {
321                 int const replace_count = all
322                         ? replaceAll(bv, search, rplc, casesensitive, matchword)
323                         : replace(bv, search, rplc, casesensitive, matchword, forward);
324         
325                 Buffer & buf = bv->buffer();
326                 if (replace_count == 0) {
327                         // emit message signal.
328                         buf.message(_("String not found!"));
329                 } else {
330                         if (replace_count == 1) {
331                                 // emit message signal.
332                                 buf.message(_("String has been replaced."));
333                         } else {
334                                 docstring str = convert<docstring>(replace_count);
335                                 str += _(" strings have been replaced.");
336                                 // emit message signal.
337                                 buf.message(str);
338                         }
339                 }
340         } else {
341                 // if we have deleted characters, we do not replace at all, but
342                 // rather search for the next occurence
343                 bool const found = find(bv, search,
344                                         casesensitive, matchword, forward);
345
346                 if (!found)
347                         // emit message signal.
348                         bv->message(_("String not found!"));
349         }
350 }
351
352
353 bool findNextChange(BufferView * bv)
354 {
355         DocIterator cur = bv->cursor();
356
357         if (!findChange(cur))
358                 return false;
359
360         bv->cursor().setCursor(cur);
361         bv->cursor().resetAnchor();
362
363         Change orig_change = cur.paragraph().lookupChange(cur.pos());
364
365         CursorSlice & tip = cur.top();
366         for (; !tip.at_end(); tip.forwardPos()) {
367                 Change change = tip.paragraph().lookupChange(tip.pos());
368                 if (change != orig_change)
369                         break;
370         }
371         // avoid crash (assertion violation) if the imaginary end-of-par
372         // character of the last paragraph of the document is marked as changed
373         if (tip.at_end())
374                 tip.backwardPos();
375
376         // Now put cursor to end of selection:
377         bv->cursor().setCursor(cur);
378         bv->cursor().setSelection();
379
380         return true;
381 }
382
383 } // lyx namespace