]> git.lyx.org Git - lyx.git/blob - src/lyxfind.C
cmake: don't forget command line options
[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() && cur.paragraph().lookupChange(cur.pos()).type
131                     != 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                 cur.paragraph().insert(pos, replacestr, font,
190                                        Change(buf.params().trackChanges ?
191                                               Change::INSERTED : Change::UNCHANGED));
192                 for (int i = 0; i < rsize + striked; ++i)
193                         cur.forwardChar();
194                 ++num;
195         }
196
197         bv->buffer()->text().init(bv);
198         bv->putSelectionAt(doc_iterator_begin(buf.inset()), 0, false);
199         if (num)
200                 buf.markDirty();
201         return num;
202 }
203
204
205 bool stringSelected(BufferView * bv, string const & searchstr,
206                     bool cs, bool mw, bool fw)
207 {
208         // if nothing selected or selection does not equal search
209         // string search and select next occurance and return
210         string const & str1 = searchstr;
211         string const str2 = lyx::to_utf8(bv->cursor().selectionAsString(false));
212         if ((cs && str1 != str2) || lowercase(str1) != lowercase(str2)) {
213                 find(bv, searchstr, cs, mw, fw);
214                 return false;
215         }
216
217         return true;
218 }
219
220
221 int replace(BufferView * bv, string const & searchstr,
222             string const & replacestr, bool cs, bool mw, bool fw)
223 {
224         if (!searchAllowed(bv, searchstr) || bv->buffer()->isReadonly())
225                 return 0;
226
227         if (!stringSelected(bv, searchstr, cs, mw, fw))
228                 return 0;
229
230         LCursor & cur = bv->cursor();
231         lyx::cap::replaceSelectionWithString(cur, replacestr, fw);
232         bv->buffer()->markDirty();
233         find(bv, searchstr, cs, mw, fw);
234         bv->update();
235
236         return 1;
237 }
238
239 } // namespace anon
240
241
242 namespace lyx {
243 namespace find {
244
245 string const find2string(string const & search,
246                          bool casesensitive, bool matchword, bool forward)
247 {
248         ostringstream ss;
249         ss << search << '\n'
250            << int(casesensitive) << ' '
251            << int(matchword) << ' '
252            << int(forward);
253         return ss.str();
254 }
255
256
257 string const replace2string(string const & search, string const & replace,
258                             bool casesensitive, bool matchword,
259                             bool all, bool forward)
260 {
261         ostringstream ss;
262         ss << search << '\n'
263            << replace << '\n'
264            << int(casesensitive) << ' '
265            << int(matchword) << ' '
266            << int(all) << ' '
267            << int(forward);
268         return ss.str();
269 }
270
271
272 void find(BufferView * bv, FuncRequest const & ev)
273 {
274         if (!bv || ev.action != LFUN_WORD_FIND)
275                 return;
276
277         //lyxerr << "find called, cmd: " << ev << std::endl;
278
279         // data is of the form
280         // "<search>
281         //  <casesensitive> <matchword> <forward>"
282         string search;
283         string howto = split(lyx::to_utf8(ev.argument()), search, '\n');
284
285         bool casesensitive = parse_bool(howto);
286         bool matchword     = parse_bool(howto);
287         bool forward       = parse_bool(howto);
288
289         bool const found = ::find(bv, search,
290                                   casesensitive, matchword, forward);
291
292         if (!found)
293                 // emit message signal.
294                 bv->message(_("String not found!"));
295 }
296
297
298 void replace(BufferView * bv, FuncRequest const & ev)
299 {
300         if (!bv || ev.action != LFUN_WORD_REPLACE)
301                 return;
302
303         // data is of the form
304         // "<search>
305         //  <replace>
306         //  <casesensitive> <matchword> <all> <forward>"
307         string search;
308         string replace;
309         string howto = split(lyx::to_utf8(ev.argument()), search, '\n');
310         howto = split(howto, replace, '\n');
311
312         bool casesensitive = parse_bool(howto);
313         bool matchword     = parse_bool(howto);
314         bool all           = parse_bool(howto);
315         bool forward       = parse_bool(howto);
316
317         Buffer * buf = bv->buffer();
318
319         int const replace_count = all
320                 ? ::replaceAll(bv, search, replace, casesensitive, matchword)
321                 : ::replace(bv, search, replace, casesensitive, matchword, forward);
322
323         if (replace_count == 0) {
324                 // emit message signal.
325                 buf->message(_("String not found!"));
326         } else {
327                 if (replace_count == 1) {
328                         // emit message signal.
329                         buf->message(_("String has been replaced."));
330                 } else {
331                         docstring str = convert<docstring>(replace_count);
332                         str += _(" strings have been replaced.");
333                         // emit message signal.
334                         buf->message(str);
335                 }
336         }
337 }
338
339
340 bool findNextChange(BufferView * bv)
341 {
342         if (!bv->buffer())
343                 return false;
344
345         DocIterator cur = bv->cursor();
346
347         if (!findChange(cur))
348                 return false;
349
350         bv->cursor().setCursor(cur);
351         bv->cursor().resetAnchor();
352
353         Change orig_change = cur.paragraph().lookupChange(cur.pos());
354
355         DocIterator et = doc_iterator_end(cur.inset());
356         for (; cur != et; cur.forwardPosNoDescend()) {
357                 Change change = cur.paragraph().lookupChange(cur.pos());
358                 if (change != orig_change) {
359                         break;
360                 }
361         }
362         // Now put cursor to end of selection:
363         bv->cursor().setCursor(cur);
364         bv->cursor().setSelection();
365         // if we used a lfun like in find/replace, dispatch would do
366         // that for us
367         bv->update();
368
369         return true;
370 }
371
372 } // find namespace
373 } // lyx namespace