]> git.lyx.org Git - lyx.git/blob - src/lyxfind.C
cleanup some debug messages
[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.forwardChar())
131                 if (cur.inTexted() && cur.pos() != cur.paragraph().size() &&
132                     cur.paragraph().lookupChange(cur.pos())
133                     != Change::UNCHANGED)
134                         return true;
135         return false;
136 }
137
138
139 bool searchAllowed(BufferView * bv, string const & str)
140 {
141         if (str.empty()) {
142                 Alert::error(_("Search error"), _("Search string is empty"));
143                 return false;
144         }
145         return bv->available();
146 }
147
148
149 bool find(BufferView * bv, string const & searchstr, bool cs, bool mw, bool fw)
150 {
151         if (!searchAllowed(bv, searchstr))
152                 return false;
153
154         DocIterator cur = bv->cursor();
155
156         MatchString const match(searchstr, cs, mw);
157
158         bool found = fw ? findForward(cur, match) : findBackwards(cur, match);
159
160         if (found)
161                 bv->putSelectionAt(cur, searchstr.length(), !fw);
162
163         return found;
164 }
165
166
167 int replaceAll(BufferView * bv,
168                string const & searchstr, string const & replacestr,
169                bool cs, bool mw)
170 {
171         Buffer & buf = *bv->buffer();
172
173         if (!searchAllowed(bv, searchstr) || buf.isReadonly())
174                 return 0;
175
176         recordUndoFullDocument(bv);
177
178         MatchString const match(searchstr, cs, mw);
179         int num = 0;
180
181         int const rsize = replacestr.size();
182         int const ssize = searchstr.size();
183
184         DocIterator cur = doc_iterator_begin(buf.inset());
185         while (findForward(cur, match)) {
186                 lyx::pos_type pos = cur.pos();
187                 LyXFont const font
188                         = cur.paragraph().getFontSettings(buf.params(), pos);
189                 int striked = ssize - cur.paragraph().erase(pos, pos + ssize);
190                 cur.paragraph().insert(pos, replacestr, font);
191                 for (int i = 0; i < rsize + striked; ++i)
192                         cur.forwardChar();
193                 ++num;
194         }
195
196         bv->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 = 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         lyx::cap::replaceSelectionWithString(cur, replacestr);
231         lyx::cap::setSelectionRange(cur, replacestr.length());
232         cur.top() = fw ? cur.selEnd() : cur.selBegin();
233         bv->buffer()->markDirty();
234         find(bv, searchstr, cs, mw, fw);
235         bv->update();
236
237         return 1;
238 }
239
240 } // namespace anon
241
242
243 namespace lyx {
244 namespace find {
245
246 string const find2string(string const & search,
247                          bool casesensitive, bool matchword, bool forward)
248 {
249         ostringstream ss;
250         ss << search << '\n'
251            << int(casesensitive) << ' '
252            << int(matchword) << ' '
253            << int(forward);
254         return ss.str();
255 }
256
257
258 string const replace2string(string const & search, string const & replace,
259                             bool casesensitive, bool matchword,
260                             bool all, bool forward)
261 {
262         ostringstream ss;
263         ss << search << '\n'
264            << replace << '\n'
265            << int(casesensitive) << ' '
266            << int(matchword) << ' '
267            << int(all) << ' '
268            << int(forward);
269         return ss.str();
270 }
271
272
273 void find(BufferView * bv, FuncRequest const & ev)
274 {
275         if (!bv || ev.action != LFUN_WORD_FIND)
276                 return;
277
278         lyxerr << "find called, cmd: " << ev << std::endl;
279
280         // data is of the form
281         // "<search>
282         //  <casesensitive> <matchword> <forward>"
283         string search;
284         string howto = split(ev.argument, search, '\n');
285
286         bool casesensitive = parse_bool(howto);
287         bool matchword     = parse_bool(howto);
288         bool forward       = parse_bool(howto);
289
290         bool const found = ::find(bv, search,
291                                   casesensitive, matchword, forward);
292
293         if (!found)
294                 bv->owner()->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(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         LyXView * lv = bv->owner();
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                 lv->message(_("String not found!"));
325         } else {
326                 if (replace_count == 1) {
327                         lv->message(_("String has been replaced."));
328                 } else {
329                         string str = convert<string>(replace_count);
330                         str += _(" strings have been replaced.");
331                         lv->message(str);
332                 }
333         }
334 }
335
336
337 bool findNextChange(BufferView * bv)
338 {
339         if (!bv->available())
340                 return false;
341
342         DocIterator cur = bv->cursor();
343
344         if (!findChange(cur))
345                 return false;
346
347         Paragraph const & par = cur.paragraph();
348         const pos_type pos = cur.pos();
349
350         Change orig_change = par.lookupChangeFull(pos);
351         const pos_type parsize = par.size();
352         pos_type end = pos;
353
354         for (; end != parsize; ++end) {
355                 Change change = par.lookupChangeFull(end);
356                 if (change != orig_change) {
357                         // slight UI optimisation: for replacements, we get
358                         // text like : _old_new. Consider that as one change.
359                         if (!(orig_change.type == Change::DELETED &&
360                                 change.type == Change::INSERTED))
361                                 break;
362                 }
363         }
364         pos_type length = end - pos;
365         bv->putSelectionAt(cur, length, false);
366         // if we used a lfun like in find/replace, dispatch would do
367         // that for us
368         bv->update();
369
370         return true;
371 }
372
373 } // find namespace
374 } // lyx namespace