]> git.lyx.org Git - lyx.git/blob - src/lyxfind.C
hopefully fix tex2lyx linking.
[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() && !cur.paragraph().isUnchanged(cur.pos()))
130                         return true;
131         return false;
132 }
133
134
135 bool searchAllowed(BufferView * bv, string const & str)
136 {
137         if (str.empty()) {
138                 frontend::Alert::error(_("Search error"),
139                                             _("Search string is empty"));
140                 return false;
141         }
142         return bv->buffer();
143 }
144
145
146 bool find(BufferView * bv, string const & searchstr, bool cs, bool mw, bool fw)
147 {
148         if (!searchAllowed(bv, searchstr))
149                 return false;
150
151         DocIterator cur = bv->cursor();
152
153         MatchString const match(searchstr, cs, mw);
154
155         bool found = fw ? findForward(cur, match) : findBackwards(cur, match);
156
157         if (found)
158                 bv->putSelectionAt(cur, searchstr.length(), !fw);
159
160         return found;
161 }
162
163
164 int replaceAll(BufferView * bv,
165                string const & searchstr, string const & replacestr,
166                bool cs, bool mw)
167 {
168         Buffer & buf = *bv->buffer();
169
170         if (!searchAllowed(bv, searchstr) || buf.isReadonly())
171                 return 0;
172
173         recordUndoFullDocument(bv);
174
175         MatchString const match(searchstr, cs, mw);
176         int num = 0;
177
178         int const rsize = replacestr.size();
179         int const ssize = searchstr.size();
180
181         DocIterator cur = doc_iterator_begin(buf.inset());
182         while (findForward(cur, match)) {
183                 pos_type pos = cur.pos();
184                 LyXFont const font
185                         = cur.paragraph().getFontSettings(buf.params(), pos);
186                 int striked = ssize - cur.paragraph().eraseChars(pos, pos + ssize,
187                                                             buf.params().trackChanges);
188                 cur.paragraph().insert(pos, from_utf8(replacestr), font,
189                                        Change(buf.params().trackChanges ?
190                                               Change::INSERTED : Change::UNCHANGED));
191                 for (int i = 0; i < rsize + striked; ++i)
192                         cur.forwardChar();
193                 ++num;
194         }
195
196         bv->buffer()->text().init(bv);
197         bv->putSelectionAt(doc_iterator_begin(buf.inset()), 0, false);
198         if (num)
199                 buf.markDirty();
200         return num;
201 }
202
203
204 bool stringSelected(BufferView * bv, string const & searchstr,
205                     bool cs, bool mw, bool fw)
206 {
207         // if nothing selected or selection does not equal search
208         // string search and select next occurance and return
209         string const & str1 = searchstr;
210         string const str2 = to_utf8(bv->cursor().selectionAsString(false));
211         if ((cs && str1 != str2) || lowercase(str1) != lowercase(str2)) {
212                 find(bv, searchstr, cs, mw, fw);
213                 return false;
214         }
215
216         return true;
217 }
218
219
220 int replace(BufferView * bv, string const & searchstr,
221             string const & replacestr, bool cs, bool mw, bool fw)
222 {
223         if (!searchAllowed(bv, searchstr) || bv->buffer()->isReadonly())
224                 return 0;
225
226         if (!stringSelected(bv, searchstr, cs, mw, fw))
227                 return 0;
228
229         LCursor & cur = bv->cursor();
230         cap::replaceSelectionWithString(cur, replacestr, fw);
231         bv->buffer()->markDirty();
232         find(bv, searchstr, cs, mw, fw);
233         bv->update();
234
235         return 1;
236 }
237
238 } // namespace anon
239
240
241 string const find2string(string const & search,
242                          bool casesensitive, bool matchword, bool forward)
243 {
244         ostringstream ss;
245         ss << search << '\n'
246            << int(casesensitive) << ' '
247            << int(matchword) << ' '
248            << int(forward);
249         return ss.str();
250 }
251
252
253 string const replace2string(string const & search, string const & replace,
254                             bool casesensitive, bool matchword,
255                             bool all, bool forward)
256 {
257         ostringstream ss;
258         ss << search << '\n'
259            << replace << '\n'
260            << int(casesensitive) << ' '
261            << int(matchword) << ' '
262            << int(all) << ' '
263            << int(forward);
264         return ss.str();
265 }
266
267
268 void find(BufferView * bv, FuncRequest const & ev)
269 {
270         if (!bv || ev.action != LFUN_WORD_FIND)
271                 return;
272
273         //lyxerr << "find called, cmd: " << ev << std::endl;
274
275         // data is of the form
276         // "<search>
277         //  <casesensitive> <matchword> <forward>"
278         string search;
279         string howto = split(to_utf8(ev.argument()), search, '\n');
280
281         bool casesensitive = parse_bool(howto);
282         bool matchword     = parse_bool(howto);
283         bool forward       = parse_bool(howto);
284
285         bool const found = find(bv, search,
286                                   casesensitive, matchword, forward);
287
288         if (!found)
289                 // emit message signal.
290                 bv->message(_("String not found!"));
291 }
292
293
294 void replace(BufferView * bv, FuncRequest const & ev)
295 {
296         if (!bv || ev.action != LFUN_WORD_REPLACE)
297                 return;
298
299         // data is of the form
300         // "<search>
301         //  <replace>
302         //  <casesensitive> <matchword> <all> <forward>"
303         string search;
304         string rplc;
305         string howto = split(to_utf8(ev.argument()), search, '\n');
306         howto = split(howto, rplc, '\n');
307
308         bool casesensitive = parse_bool(howto);
309         bool matchword     = parse_bool(howto);
310         bool all           = parse_bool(howto);
311         bool forward       = parse_bool(howto);
312
313         Buffer * buf = bv->buffer();
314
315         int const replace_count = all
316                 ? replaceAll(bv, search, rplc, casesensitive, matchword)
317                 : replace(bv, search, rplc, casesensitive, matchword, forward);
318
319         if (replace_count == 0) {
320                 // emit message signal.
321                 buf->message(_("String not found!"));
322         } else {
323                 if (replace_count == 1) {
324                         // emit message signal.
325                         buf->message(_("String has been replaced."));
326                 } else {
327                         docstring str = convert<docstring>(replace_count);
328                         str += _(" strings have been replaced.");
329                         // emit message signal.
330                         buf->message(str);
331                 }
332         }
333 }
334
335
336 bool findNextChange(BufferView * bv)
337 {
338         if (!bv->buffer())
339                 return false;
340
341         DocIterator cur = bv->cursor();
342
343         if (!findChange(cur))
344                 return false;
345
346         bv->cursor().setCursor(cur);
347         bv->cursor().resetAnchor();
348
349         Change orig_change = cur.paragraph().lookupChange(cur.pos());
350
351         DocIterator et = doc_iterator_end(cur.inset());
352         for (; cur != et; cur.forwardPosNoDescend()) {
353                 Change change = cur.paragraph().lookupChange(cur.pos());
354                 if (change != orig_change) {
355                         break;
356                 }
357         }
358         // Now put cursor to end of selection:
359         bv->cursor().setCursor(cur);
360         bv->cursor().setSelection();
361         // if we used a lfun like in find/replace, dispatch would do
362         // that for us
363         bv->update();
364
365         return true;
366 }
367
368 } // lyx namespace