3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
9 * \author Alfredo Braunstein
11 * Full author contact details are available in file CREDITS.
19 #include "BufferView.h"
21 #include "iterators.h"
22 #include "funcrequest.h"
25 #include "paragraph.h"
26 #include "PosIterator.h"
29 #include "frontends/Alert.h"
30 #include "frontends/LyXView.h"
32 #include "support/textutils.h"
33 #include "support/tostr.h"
35 #include "support/std_sstream.h"
37 using lyx::support::lowercase;
38 using lyx::support::uppercase;
39 using lyx::support::split;
41 using bv_funcs::put_selection_at;
43 using std::ostringstream;
49 bool parse_bool(string & howto)
54 howto = split(howto, var, ' ');
59 bool find(BufferView * bv,
60 string const & searchstr, bool cs, bool mw, bool fw);
63 int replace(BufferView * bv,
64 string const & searchstr, string const & replacestr,
65 bool cs, bool mw, bool fw);
68 int replaceAll(BufferView * bv,
69 string const & searchstr, string const & replacestr,
73 bool findChange(PosIterator & cur, PosIterator const & end);
81 string const find2string(string const & search,
82 bool casesensitive, bool matchword, bool forward)
86 << int(casesensitive) << ' '
87 << int(matchword) << ' '
94 string const replace2string(string const & search, string const & replace,
95 bool casesensitive, bool matchword,
96 bool all, bool forward)
101 << int(casesensitive) << ' '
102 << int(matchword) << ' '
110 void find(FuncRequest const & ev)
112 if (!ev.view() || ev.action != LFUN_WORD_FIND)
115 // data is of the form
117 // <casesensitive> <matchword> <forward>"
119 string howto = split(ev.argument, search, '\n');
121 bool casesensitive = parse_bool(howto);
122 bool matchword = parse_bool(howto);
123 bool forward = parse_bool(howto);
125 BufferView * bv = ev.view();
126 bool const found = ::find(bv, search,
127 forward, casesensitive, matchword);
130 bv->owner()->message(_("String not found!"));
134 void replace(FuncRequest const & ev)
136 if (!ev.view() || ev.action != LFUN_WORD_REPLACE)
139 // data is of the form
142 // <casesensitive> <matchword> <all> <forward>"
145 string howto = split(ev.argument, search, '\n');
146 howto = split(howto, replace, '\n');
148 bool casesensitive = parse_bool(howto);
149 bool matchword = parse_bool(howto);
150 bool all = parse_bool(howto);
151 bool forward = parse_bool(howto);
153 BufferView * bv = ev.view();
154 LyXView * lv = bv->owner();
156 int const replace_count = all ?
157 ::replaceAll(bv, search, replace,
158 casesensitive, matchword) :
159 ::replace(bv, search, replace,
160 casesensitive, matchword, forward);
162 if (replace_count == 0) {
163 lv->message(_("String not found!"));
165 if (replace_count == 1) {
166 lv->message(_("String has been replaced."));
168 string str = tostr(replace_count);
169 str += _(" strings have been replaced.");
176 bool findNextChange(BufferView * bv)
178 if (!bv->available())
181 PosIterator cur = PosIterator(*bv);
182 PosIterator const endit = bv->buffer()->pos_iterator_end();
184 if (!findChange(cur, endit))
187 ParagraphList::iterator pit = cur.pit();
188 pos_type pos = cur.pos();
190 Change orig_change = pit->lookupChangeFull(pos);
191 pos_type parsize = pit->size();
194 for (; end != parsize; ++end) {
195 Change change = pit->lookupChangeFull(end);
196 if (change != orig_change) {
197 // slight UI optimisation: for replacements, we get
198 // text like : _old_new. Consider that as one change.
199 if (!(orig_change.type == Change::DELETED &&
200 change.type == Change::INSERTED))
204 pos_type length = end - pos;
205 put_selection_at(bv, cur, length, true);
218 MatchString(string const & str, bool cs, bool mw)
219 : str(str), cs(cs), mw(mw)
222 // returns true if the specified string is at the specified position
223 bool operator()(Paragraph const & par, lyx::pos_type pos) const
225 string::size_type const size = str.length();
227 lyx::pos_type const parsize = par.size();
228 while ((pos + i < parsize)
229 && (string::size_type(i) < size)
230 && (cs ? (str[i] == par.getChar(pos + i))
231 : (uppercase(str[i]) == uppercase(par.getChar(pos + i))))) {
235 if (size != string::size_type(i))
238 // if necessary, check whether string matches word
240 if (pos > 0 && IsLetterCharOrDigit(par.getChar(pos - 1)))
242 if (pos + lyx::pos_type(size) < parsize
243 && IsLetterCharOrDigit(par.getChar(pos + size)));
255 // match whole words only
260 bool findForward(PosIterator & cur, PosIterator const & end,
261 MatchString const & match)
263 for (; cur != end; ++cur) {
264 if (match(*cur.pit(), cur.pos()))
271 bool findBackwards(PosIterator & cur, PosIterator const & beg,
272 MatchString const & match)
276 if (match(*cur.pit(), cur.pos()))
283 bool findChange(PosIterator & cur, PosIterator const & end)
285 for (; cur != end; ++cur) {
286 if ((!cur.pit()->size() || !cur.at_end())
287 && cur.pit()->lookupChange(cur.pos()) != Change::UNCHANGED)
294 bool searchAllowed(BufferView * bv, string const & str)
297 Alert::error(_("Search error"), _("Search string is empty"));
300 return bv->available();
304 bool find(BufferView * bv, string const & searchstr, bool cs, bool mw, bool fw)
306 if (!searchAllowed(bv, searchstr))
309 PosIterator cur = PosIterator(*bv);
311 MatchString const match(searchstr, cs, mw);
313 PosIterator const end = bv->buffer()->pos_iterator_end();
314 PosIterator const beg = bv->buffer()->pos_iterator_begin();
316 bool found = fw ? findForward(cur, end, match)
317 : findBackwards(cur, beg, match);
320 put_selection_at(bv, cur, searchstr.length(), !fw);
326 int replaceAll(BufferView * bv,
327 string const & searchstr, string const & replacestr,
330 Buffer & buf = *bv->buffer();
332 if (!searchAllowed(bv, searchstr) || buf.isReadonly())
335 recordUndo(Undo::ATOMIC, bv->text(), 0, buf.paragraphs().size() - 1);
337 PosIterator cur = buf.pos_iterator_begin();
338 PosIterator const end = buf.pos_iterator_end();
339 MatchString const match(searchstr, cs, mw);
342 int const rsize = replacestr.size();
343 int const ssize = searchstr.size();
345 while (findForward(cur, end, match)) {
346 lyx::pos_type pos = cur.pos();
348 = cur.pit()->getFontSettings(buf.params(), pos);
349 int striked = ssize - cur.pit()->erase(pos, pos + ssize);
350 cur.pit()->insert(pos, replacestr, font);
351 advance(cur, rsize + striked);
355 PosIterator beg = buf.pos_iterator_begin();
356 bv->text()->init(bv);
357 put_selection_at(bv, beg, 0, false);
364 bool stringSelected(BufferView * bv,
365 string const & searchstr,
366 bool cs, bool mw, bool fw)
368 LyXText * text = bv->getLyXText();
369 // if nothing selected or selection does not equal search
370 // string search and select next occurance and return
371 string const & str1 = searchstr;
372 string const str2 = text->selectionAsString(*bv->buffer(),
374 if ((cs && str1 != str2) || lowercase(str1) != lowercase(str2)) {
375 find(bv, searchstr, cs, mw, fw);
383 int replace(BufferView * bv,
384 string const & searchstr, string const & replacestr,
385 bool cs, bool mw, bool fw)
387 if (!searchAllowed(bv, searchstr) || bv->buffer()->isReadonly())
390 if (!stringSelected(bv, searchstr, cs, mw, fw))
393 LyXText * text = bv->getLyXText();
395 text->replaceSelectionWithString(replacestr);
396 text->setSelectionRange(replacestr.length());
397 text->cursor = fw ? text->selEnd() : text->selStart();
399 bv->buffer()->markDirty();
400 find(bv, searchstr, cs, mw, fw);