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