]> git.lyx.org Git - lyx.git/blob - src/lyxfind.C
use more unicode in math
[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 namespace lyx {
37
38 using support::lowercase;
39 using support::uppercase;
40 using support::split;
41
42 using std::advance;
43 using std::ostringstream;
44 using std::string;
45
46
47 namespace {
48
49 bool parse_bool(string & howto)
50 {
51         if (howto.empty())
52                 return false;
53         string var;
54         howto = split(howto, var, ' ');
55         return (var == "1");
56 }
57
58
59 class MatchString : public std::binary_function<Paragraph, pos_type, bool>
60 {
61 public:
62         MatchString(string const & str, bool cs, bool mw)
63                 : str(str), cs(cs), mw(mw)
64         {}
65
66         // returns true if the specified string is at the specified position
67         bool operator()(Paragraph const & par, pos_type pos) const
68         {
69                 string::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 (string::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                 }
80
81                 if (size != string::size_type(i))
82                         return false;
83
84                 // if necessary, check whether string matches word
85                 if (mw) {
86                         if (pos > 0 && par.isLetter(pos - 1))
87                                 return false;
88                         if (pos + pos_type(size) < parsize
89                             && par.isLetter(pos + size))
90                                 return false;
91                 }
92
93                 return true;
94         }
95
96 private:
97         // search string
98         string str;
99         // case sensitive
100         bool cs;
101         // match whole words only
102         bool mw;
103 };
104
105
106 bool findForward(DocIterator & cur, MatchString const & match)
107 {
108         for (; cur; cur.forwardChar())
109                 if (cur.inTexted() && match(cur.paragraph(), cur.pos()))
110                         return true;
111         return false;
112 }
113
114
115 bool findBackwards(DocIterator & cur, MatchString const & match)
116 {
117         while (cur) {
118                 cur.backwardChar();
119                 if (cur.inTexted() && match(cur.paragraph(), cur.pos()))
120                         return true;
121         }
122         return false;
123 }
124
125
126 bool findChange(DocIterator & cur)
127 {
128         for (; cur; cur.forwardPos())
129                 if (cur.inTexted() &&
130                     cur.paragraph().lookupChange(cur.pos()).type != Change::UNCHANGED)
131                         return true;
132         return false;
133 }
134
135
136 bool searchAllowed(BufferView * bv, string const & str)
137 {
138         if (str.empty()) {
139                 frontend::Alert::error(_("Search error"),
140                                             _("Search string is empty"));
141                 return false;
142         }
143         return bv->buffer();
144 }
145
146
147 bool find(BufferView * bv, string const & searchstr, bool cs, bool mw, bool fw)
148 {
149         if (!searchAllowed(bv, searchstr))
150                 return false;
151
152         DocIterator cur = bv->cursor();
153
154         MatchString const match(searchstr, cs, mw);
155
156         bool found = fw ? findForward(cur, match) : findBackwards(cur, match);
157
158         if (found)
159                 bv->putSelectionAt(cur, searchstr.length(), !fw);
160
161         return found;
162 }
163
164
165 int replaceAll(BufferView * bv,
166                string const & searchstr, string const & replacestr,
167                bool cs, bool mw)
168 {
169         Buffer & buf = *bv->buffer();
170
171         if (!searchAllowed(bv, searchstr) || buf.isReadonly())
172                 return 0;
173
174         recordUndoFullDocument(bv);
175
176         MatchString const match(searchstr, cs, mw);
177         int num = 0;
178
179         int const rsize = replacestr.size();
180         int const ssize = searchstr.size();
181
182         DocIterator cur = doc_iterator_begin(buf.inset());
183         while (findForward(cur, match)) {
184                 pos_type pos = cur.pos();
185                 LyXFont const font
186                         = cur.paragraph().getFontSettings(buf.params(), pos);
187                 int striked = ssize - cur.paragraph().erase(pos, pos + ssize,
188                                                             buf.params().trackChanges);
189                 cur.paragraph().insert(pos, from_utf8(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 = 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         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 string const find2string(string const & search,
243                          bool casesensitive, bool matchword, bool forward)
244 {
245         ostringstream ss;
246         ss << search << '\n'
247            << int(casesensitive) << ' '
248            << int(matchword) << ' '
249            << int(forward);
250         return ss.str();
251 }
252
253
254 string const replace2string(string const & search, string const & replace,
255                             bool casesensitive, bool matchword,
256                             bool all, bool forward)
257 {
258         ostringstream ss;
259         ss << search << '\n'
260            << replace << '\n'
261            << int(casesensitive) << ' '
262            << int(matchword) << ' '
263            << int(all) << ' '
264            << int(forward);
265         return ss.str();
266 }
267
268
269 void find(BufferView * bv, FuncRequest const & ev)
270 {
271         if (!bv || ev.action != LFUN_WORD_FIND)
272                 return;
273
274         //lyxerr << "find called, cmd: " << ev << std::endl;
275
276         // data is of the form
277         // "<search>
278         //  <casesensitive> <matchword> <forward>"
279         string search;
280         string howto = split(to_utf8(ev.argument()), search, '\n');
281
282         bool casesensitive = parse_bool(howto);
283         bool matchword     = parse_bool(howto);
284         bool forward       = parse_bool(howto);
285
286         bool const found = find(bv, search,
287                                   casesensitive, matchword, forward);
288
289         if (!found)
290                 // emit message signal.
291                 bv->message(_("String not found!"));
292 }
293
294
295 void replace(BufferView * bv, FuncRequest const & ev)
296 {
297         if (!bv || ev.action != LFUN_WORD_REPLACE)
298                 return;
299
300         // data is of the form
301         // "<search>
302         //  <replace>
303         //  <casesensitive> <matchword> <all> <forward>"
304         string search;
305         string rplc;
306         string howto = split(to_utf8(ev.argument()), search, '\n');
307         howto = split(howto, rplc, '\n');
308
309         bool casesensitive = parse_bool(howto);
310         bool matchword     = parse_bool(howto);
311         bool all           = parse_bool(howto);
312         bool forward       = parse_bool(howto);
313
314         Buffer * buf = bv->buffer();
315
316         int const replace_count = all
317                 ? replaceAll(bv, search, rplc, casesensitive, matchword)
318                 : replace(bv, search, rplc, casesensitive, matchword, forward);
319
320         if (replace_count == 0) {
321                 // emit message signal.
322                 buf->message(_("String not found!"));
323         } else {
324                 if (replace_count == 1) {
325                         // emit message signal.
326                         buf->message(_("String has been replaced."));
327                 } else {
328                         docstring str = convert<docstring>(replace_count);
329                         str += _(" strings have been replaced.");
330                         // emit message signal.
331                         buf->message(str);
332                 }
333         }
334 }
335
336
337 bool findNextChange(BufferView * bv)
338 {
339         if (!bv->buffer())
340                 return false;
341
342         DocIterator cur = bv->cursor();
343
344         if (!findChange(cur))
345                 return false;
346
347         bv->cursor().setCursor(cur);
348         bv->cursor().resetAnchor();
349
350         Change orig_change = cur.paragraph().lookupChange(cur.pos());
351
352         DocIterator et = doc_iterator_end(cur.inset());
353         for (; cur != et; cur.forwardPosNoDescend()) {
354                 Change change = cur.paragraph().lookupChange(cur.pos());
355                 if (change != orig_change) {
356                         break;
357                 }
358         }
359         // Now put cursor to end of selection:
360         bv->cursor().setCursor(cur);
361         bv->cursor().setSelection();
362         // if we used a lfun like in find/replace, dispatch would do
363         // that for us
364         bv->update();
365
366         return true;
367 }
368
369 } // lyx namespace