]> git.lyx.org Git - lyx.git/blob - src/lyxfind.C
That commit replaces the Buffer::message() signal emissions from within "BufferView_p...
[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 #include "frontends/LyXView.h"
32
33 #include "support/convert.h"
34
35 #include <sstream>
36
37 using lyx::support::lowercase;
38 using lyx::support::uppercase;
39 using lyx::support::split;
40
41 using lyx::pit_type;
42 using lyx::pos_type;
43
44 using std::advance;
45 using std::ostringstream;
46 using std::string;
47
48
49 namespace {
50
51 bool parse_bool(string & howto)
52 {
53         if (howto.empty())
54                 return false;
55         string var;
56         howto = split(howto, var, ' ');
57         return (var == "1");
58 }
59
60
61 class MatchString : public std::binary_function<Paragraph, lyx::pos_type, bool>
62 {
63 public:
64         MatchString(string const & str, bool cs, bool mw)
65                 : str(str), cs(cs), mw(mw)
66         {}
67
68         // returns true if the specified string is at the specified position
69         bool operator()(Paragraph const & par, lyx::pos_type pos) const
70         {
71                 string::size_type const size = str.length();
72                 lyx::pos_type i = 0;
73                 lyx::pos_type const parsize = par.size();
74                 for (i = 0; pos + i < parsize; ++i) {
75                         if (string::size_type(i) >= size)
76                                 break;
77                         if (cs && str[i] != par.getChar(pos + i))
78                                 break;
79                         if (!cs && uppercase(str[i]) != uppercase(par.getChar(pos + i)))
80                                 break;
81                 }
82
83                 if (size != string::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 + lyx::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         string 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 {
110         for (; cur; cur.forwardChar())
111                 if (cur.inTexted() && match(cur.paragraph(), cur.pos()))
112                         return true;
113         return false;
114 }
115
116
117 bool findBackwards(DocIterator & cur, MatchString const & match)
118 {
119         while (cur) {
120                 cur.backwardChar();
121                 if (cur.inTexted() && match(cur.paragraph(), cur.pos()))
122                         return true;
123         }
124         return false;
125 }
126
127
128 bool findChange(DocIterator & cur)
129 {
130         for (; cur; cur.forwardPos())
131                 if (cur.inTexted() && cur.paragraph().lookupChange(cur.pos()).type
132                     != Change::UNCHANGED)
133                         return true;
134         return false;
135 }
136
137
138 bool searchAllowed(BufferView * bv, string const & str)
139 {
140         if (str.empty()) {
141                 Alert::error(_("Search error"), _("Search string is empty"));
142                 return false;
143         }
144         return bv->available();
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                 for (int i = 0; i < rsize + striked; ++i)
191                         cur.forwardChar();
192                 ++num;
193         }
194
195         bv->text()->init(bv);
196         bv->putSelectionAt(doc_iterator_begin(buf.inset()), 0, false);
197         if (num)
198                 buf.markDirty();
199         return num;
200 }
201
202
203 bool stringSelected(BufferView * bv, string const & searchstr,
204                     bool cs, bool mw, bool fw)
205 {
206         // if nothing selected or selection does not equal search
207         // string search and select next occurance and return
208         string const & str1 = searchstr;
209         string const str2 = bv->cursor().selectionAsString(false);
210         if ((cs && str1 != str2) || lowercase(str1) != lowercase(str2)) {
211                 find(bv, searchstr, cs, mw, fw);
212                 return false;
213         }
214
215         return true;
216 }
217
218
219 int replace(BufferView * bv, string const & searchstr,
220             string const & replacestr, bool cs, bool mw, bool fw)
221 {
222         if (!searchAllowed(bv, searchstr) || bv->buffer()->isReadonly())
223                 return 0;
224
225         if (!stringSelected(bv, searchstr, cs, mw, fw))
226                 return 0;
227
228         LCursor & cur = bv->cursor();
229         lyx::cap::replaceSelectionWithString(cur, replacestr, fw);
230         bv->buffer()->markDirty();
231         find(bv, searchstr, cs, mw, fw);
232         bv->update();
233
234         return 1;
235 }
236
237 } // namespace anon
238
239
240 namespace lyx {
241 namespace find {
242
243 string const find2string(string const & search,
244                          bool casesensitive, bool matchword, bool forward)
245 {
246         ostringstream ss;
247         ss << search << '\n'
248            << int(casesensitive) << ' '
249            << int(matchword) << ' '
250            << int(forward);
251         return ss.str();
252 }
253
254
255 string const replace2string(string const & search, string const & replace,
256                             bool casesensitive, bool matchword,
257                             bool all, bool forward)
258 {
259         ostringstream ss;
260         ss << search << '\n'
261            << replace << '\n'
262            << int(casesensitive) << ' '
263            << int(matchword) << ' '
264            << int(all) << ' '
265            << int(forward);
266         return ss.str();
267 }
268
269
270 void find(BufferView * bv, FuncRequest const & ev)
271 {
272         if (!bv || ev.action != LFUN_WORD_FIND)
273                 return;
274
275         //lyxerr << "find called, cmd: " << ev << std::endl;
276
277         // data is of the form
278         // "<search>
279         //  <casesensitive> <matchword> <forward>"
280         string search;
281         string howto = split(ev.argument, search, '\n');
282
283         bool casesensitive = parse_bool(howto);
284         bool matchword     = parse_bool(howto);
285         bool forward       = parse_bool(howto);
286
287         bool const found = ::find(bv, search,
288                                   casesensitive, matchword, forward);
289
290         if (!found)
291                 // emit message signal.
292                 bv->message(_("String not found!"));
293 }
294
295
296 void replace(BufferView * bv, FuncRequest const & ev)
297 {
298         if (!bv || ev.action != LFUN_WORD_REPLACE)
299                 return;
300
301         // data is of the form
302         // "<search>
303         //  <replace>
304         //  <casesensitive> <matchword> <all> <forward>"
305         string search;
306         string replace;
307         string howto = split(ev.argument, search, '\n');
308         howto = split(howto, replace, '\n');
309
310         bool casesensitive = parse_bool(howto);
311         bool matchword     = parse_bool(howto);
312         bool all           = parse_bool(howto);
313         bool forward       = parse_bool(howto);
314
315         Buffer * buf = bv->buffer();
316
317         int const replace_count = all
318                 ? ::replaceAll(bv, search, replace, casesensitive, matchword)
319                 : ::replace(bv, search, replace, casesensitive, matchword, forward);
320
321         if (replace_count == 0) {
322                 // emit message signal.
323                 buf->message(_("String not found!"));
324         } else {
325                 if (replace_count == 1) {
326                         // emit message signal.
327                         buf->message(_("String has been replaced."));
328                 } else {
329                         string str = convert<string>(replace_count);
330                         str += _(" strings have been replaced.");
331                         // emit message signal.
332                         buf->message(str);
333                 }
334         }
335 }
336
337
338 bool findNextChange(BufferView * bv)
339 {
340         if (!bv->available())
341                 return false;
342
343         DocIterator cur = bv->cursor();
344
345         if (!findChange(cur))
346                 return false;
347
348         bv->cursor().setCursor(cur);
349         bv->cursor().resetAnchor();
350
351         Change orig_change = cur.paragraph().lookupChange(cur.pos());
352
353         DocIterator et = doc_iterator_end(cur.inset());
354         for (; cur != et; cur.forwardPosNoDescend()) {
355                 Change change = cur.paragraph().lookupChange(cur.pos());
356                 if (change != orig_change) {
357                         break;
358                 }
359         }
360         // Now put cursor to end of selection:
361         bv->cursor().setCursor(cur);
362         bv->cursor().setSelection();
363         // if we used a lfun like in find/replace, dispatch would do
364         // that for us
365         bv->update();
366
367         return true;
368 }
369
370 } // find namespace
371 } // lyx namespace