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