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 "cursor_slice.h"
20 #include "BufferView.h"
22 #include "iterators.h"
23 #include "funcrequest.h"
26 #include "paragraph.h"
27 #include "PosIterator.h"
30 #include "frontends/Alert.h"
31 #include "frontends/LyXView.h"
33 #include "support/textutils.h"
34 #include "support/tostr.h"
36 #include "support/std_sstream.h"
38 using lyx::support::lowercase;
39 using lyx::support::uppercase;
40 using lyx::support::split;
42 using std::ostringstream;
48 bool parse_bool(string & howto)
53 howto = split(howto, var, ' ');
58 bool find(BufferView * bv,
59 string const & searchstr, bool cs, bool mw, bool fw);
62 int replace(BufferView * bv,
63 string const & searchstr, string const & replacestr,
64 bool cs, bool mw, bool fw);
67 int replaceAll(BufferView * bv,
68 string const & searchstr, string const & replacestr,
72 bool findChange(PosIterator & cur, PosIterator const & end);
80 string const find2string(string const & search,
81 bool casesensitive, bool matchword, bool forward)
85 << int(casesensitive) << ' '
86 << int(matchword) << ' '
93 string const replace2string(string const & search, string const & replace,
94 bool casesensitive, bool matchword,
95 bool all, bool forward)
100 << int(casesensitive) << ' '
101 << int(matchword) << ' '
109 void find(BufferView * bv, FuncRequest const & ev)
111 if (!bv || ev.action != LFUN_WORD_FIND)
114 // data is of the form
116 // <casesensitive> <matchword> <forward>"
118 string howto = split(ev.argument, search, '\n');
120 bool casesensitive = parse_bool(howto);
121 bool matchword = parse_bool(howto);
122 bool forward = parse_bool(howto);
124 bool const found = ::find(bv, search,
125 forward, casesensitive, matchword);
128 bv->owner()->message(_("String not found!"));
132 void replace(BufferView * bv, FuncRequest const & ev)
134 if (!bv || ev.action != LFUN_WORD_REPLACE)
137 // data is of the form
140 // <casesensitive> <matchword> <all> <forward>"
143 string howto = split(ev.argument, search, '\n');
144 howto = split(howto, replace, '\n');
146 bool casesensitive = parse_bool(howto);
147 bool matchword = parse_bool(howto);
148 bool all = parse_bool(howto);
149 bool forward = parse_bool(howto);
151 LyXView * lv = bv->owner();
153 int const replace_count = all ?
154 ::replaceAll(bv, search, replace,
155 casesensitive, matchword) :
156 ::replace(bv, search, replace,
157 casesensitive, matchword, forward);
159 if (replace_count == 0) {
160 lv->message(_("String not found!"));
162 if (replace_count == 1) {
163 lv->message(_("String has been replaced."));
165 string str = tostr(replace_count);
166 str += _(" strings have been replaced.");
173 bool findNextChange(BufferView * bv)
175 if (!bv->available())
178 PosIterator cur = PosIterator(*bv);
179 PosIterator const endit = bv->buffer()->pos_iterator_end();
181 if (!findChange(cur, endit))
184 ParagraphList::iterator pit = cur.pit();
185 pos_type pos = cur.pos();
187 Change orig_change = pit->lookupChangeFull(pos);
188 pos_type parsize = pit->size();
191 for (; end != parsize; ++end) {
192 Change change = pit->lookupChangeFull(end);
193 if (change != orig_change) {
194 // slight UI optimisation: for replacements, we get
195 // text like : _old_new. Consider that as one change.
196 if (!(orig_change.type == Change::DELETED &&
197 change.type == Change::INSERTED))
201 pos_type length = end - pos;
202 bv->putSelectionAt(cur, length, true);
215 MatchString(string const & str, bool cs, bool mw)
216 : str(str), cs(cs), mw(mw)
219 // returns true if the specified string is at the specified position
220 bool operator()(Paragraph const & par, lyx::pos_type pos) const
222 string::size_type const size = str.length();
224 lyx::pos_type const parsize = par.size();
225 while ((pos + i < parsize)
226 && (string::size_type(i) < size)
227 && (cs ? (str[i] == par.getChar(pos + i))
228 : (uppercase(str[i]) == uppercase(par.getChar(pos + i))))) {
232 if (size != string::size_type(i))
235 // if necessary, check whether string matches word
237 if (pos > 0 && IsLetterCharOrDigit(par.getChar(pos - 1)))
239 if (pos + lyx::pos_type(size) < parsize
240 && IsLetterCharOrDigit(par.getChar(pos + size)));
252 // match whole words only
257 bool findForward(PosIterator & cur, PosIterator const & end,
258 MatchString const & match)
260 for (; cur != end; ++cur) {
261 if (match(*cur.pit(), cur.pos()))
268 bool findBackwards(PosIterator & cur, PosIterator const & beg,
269 MatchString const & match)
273 if (match(*cur.pit(), cur.pos()))
280 bool findChange(PosIterator & cur, PosIterator const & end)
282 for (; cur != end; ++cur) {
283 if ((!cur.pit()->size() || !cur.at_end())
284 && cur.pit()->lookupChange(cur.pos()) != Change::UNCHANGED)
291 bool searchAllowed(BufferView * bv, string const & str)
294 Alert::error(_("Search error"), _("Search string is empty"));
297 return bv->available();
301 bool find(BufferView * bv, string const & searchstr, bool cs, bool mw, bool fw)
303 if (!searchAllowed(bv, searchstr))
306 PosIterator cur = PosIterator(*bv);
308 MatchString const match(searchstr, cs, mw);
310 PosIterator const end = bv->buffer()->pos_iterator_end();
311 PosIterator const beg = bv->buffer()->pos_iterator_begin();
313 bool found = fw ? findForward(cur, end, match)
314 : findBackwards(cur, beg, match);
317 bv->putSelectionAt(cur, searchstr.length(), !fw);
323 int replaceAll(BufferView * bv,
324 string const & searchstr, string const & replacestr,
327 Buffer & buf = *bv->buffer();
329 if (!searchAllowed(bv, searchstr) || buf.isReadonly())
332 recordUndo(Undo::ATOMIC, bv->text(), 0, buf.paragraphs().size() - 1);
334 PosIterator cur = buf.pos_iterator_begin();
335 PosIterator const end = buf.pos_iterator_end();
336 MatchString const match(searchstr, cs, mw);
339 int const rsize = replacestr.size();
340 int const ssize = searchstr.size();
342 while (findForward(cur, end, match)) {
343 lyx::pos_type pos = cur.pos();
345 = cur.pit()->getFontSettings(buf.params(), pos);
346 int striked = ssize - cur.pit()->erase(pos, pos + ssize);
347 cur.pit()->insert(pos, replacestr, font);
348 advance(cur, rsize + striked);
352 PosIterator beg = buf.pos_iterator_begin();
353 bv->text()->init(bv);
354 bv->putSelectionAt(beg, 0, false);
361 bool stringSelected(BufferView * bv,
362 string const & searchstr,
363 bool cs, bool mw, bool fw)
365 LyXText * text = bv->getLyXText();
366 // if nothing selected or selection does not equal search
367 // string search and select next occurance and return
368 string const & str1 = searchstr;
369 string const str2 = text->selectionAsString(*bv->buffer(),
371 if ((cs && str1 != str2) || lowercase(str1) != lowercase(str2)) {
372 find(bv, searchstr, cs, mw, fw);
380 int replace(BufferView * bv,
381 string const & searchstr, string const & replacestr,
382 bool cs, bool mw, bool fw)
384 if (!searchAllowed(bv, searchstr) || bv->buffer()->isReadonly())
387 if (!stringSelected(bv, searchstr, cs, mw, fw))
390 LyXText * text = bv->getLyXText();
392 text->replaceSelectionWithString(replacestr);
393 text->setSelectionRange(replacestr.length());
394 bv->cursor() = fw ? bv->selEnd() : bv->selStart();
395 bv->buffer()->markDirty();
396 find(bv, searchstr, cs, mw, fw);