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.
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;
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(BufferView * bv, FuncRequest const & ev)
112 if (!bv || 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 bool const found = ::find(bv, search,
126 casesensitive, matchword, forward);
129 bv->owner()->message(_("String not found!"));
133 void replace(BufferView * bv, FuncRequest const & ev)
135 if (!bv || ev.action != LFUN_WORD_REPLACE)
138 // data is of the form
141 // <casesensitive> <matchword> <all> <forward>"
144 string howto = split(ev.argument, search, '\n');
145 howto = split(howto, replace, '\n');
147 bool casesensitive = parse_bool(howto);
148 bool matchword = parse_bool(howto);
149 bool all = parse_bool(howto);
150 bool forward = parse_bool(howto);
152 LyXView * lv = bv->owner();
154 int const replace_count = all ?
155 ::replaceAll(bv, search, replace,
156 casesensitive, matchword) :
157 ::replace(bv, search, replace,
158 casesensitive, matchword, forward);
160 if (replace_count == 0) {
161 lv->message(_("String not found!"));
163 if (replace_count == 1) {
164 lv->message(_("String has been replaced."));
166 string str = tostr(replace_count);
167 str += _(" strings have been replaced.");
174 bool findNextChange(BufferView * bv)
176 if (!bv->available())
179 PosIterator cur = PosIterator(*bv);
180 PosIterator const endit = bv->buffer()->pos_iterator_end();
182 if (!findChange(cur, endit))
185 ParagraphList::iterator pit = cur.pit();
186 pos_type pos = cur.pos();
188 Change orig_change = pit->lookupChangeFull(pos);
189 pos_type parsize = pit->size();
192 for (; end != parsize; ++end) {
193 Change change = pit->lookupChangeFull(end);
194 if (change != orig_change) {
195 // slight UI optimisation: for replacements, we get
196 // text like : _old_new. Consider that as one change.
197 if (!(orig_change.type == Change::DELETED &&
198 change.type == Change::INSERTED))
202 pos_type length = end - pos;
203 bv->putSelectionAt(cur, length, true);
213 class MatchString : public std::binary_function<Paragraph, lyx::pos_type, bool>
216 MatchString(string const & str, bool cs, bool mw)
217 : str(str), cs(cs), mw(mw)
220 // returns true if the specified string is at the specified position
221 bool operator()(Paragraph const & par, lyx::pos_type pos) const
223 string::size_type const size = str.length();
225 lyx::pos_type const parsize = par.size();
226 while ((pos + i < parsize)
227 && (string::size_type(i) < size)
228 && (cs ? (str[i] == par.getChar(pos + i))
229 : (uppercase(str[i]) == uppercase(par.getChar(pos + i))))) {
233 if (size != string::size_type(i))
236 // if necessary, check whether string matches word
238 if (pos > 0 && IsLetterCharOrDigit(par.getChar(pos - 1)))
240 if (pos + lyx::pos_type(size) < parsize
241 && IsLetterCharOrDigit(par.getChar(pos + size)));
253 // match whole words only
258 bool findForward(PosIterator & cur, PosIterator const & end,
259 MatchString const & match)
261 for (; cur != end; ++cur) {
262 if (match(*cur.pit(), cur.pos()))
269 bool findBackwards(PosIterator & cur, PosIterator const & beg,
270 MatchString const & match)
274 if (match(*cur.pit(), cur.pos()))
281 bool findChange(PosIterator & cur, PosIterator const & end)
283 for (; cur != end; ++cur) {
284 if ((cur.pit()->empty() || !cur.at_end())
285 && cur.pit()->lookupChange(cur.pos()) != Change::UNCHANGED)
292 bool searchAllowed(BufferView * bv, string const & str)
295 Alert::error(_("Search error"), _("Search string is empty"));
298 return bv->available();
302 bool find(BufferView * bv, string const & searchstr, bool cs, bool mw, bool fw)
304 if (!searchAllowed(bv, searchstr))
307 PosIterator cur = PosIterator(*bv);
309 MatchString const match(searchstr, cs, mw);
311 PosIterator const end = bv->buffer()->pos_iterator_end();
312 PosIterator const beg = bv->buffer()->pos_iterator_begin();
314 bool found = fw ? findForward(cur, end, match)
315 : findBackwards(cur, beg, match);
318 bv->putSelectionAt(cur, searchstr.length(), !fw);
324 int replaceAll(BufferView * bv,
325 string const & searchstr, string const & replacestr,
328 Buffer & buf = *bv->buffer();
330 if (!searchAllowed(bv, searchstr) || buf.isReadonly())
333 recordUndoFullDocument(bv->cursor());
335 PosIterator cur = buf.pos_iterator_begin();
336 PosIterator const end = buf.pos_iterator_end();
337 MatchString const match(searchstr, cs, mw);
340 int const rsize = replacestr.size();
341 int const ssize = searchstr.size();
343 while (findForward(cur, end, match)) {
344 lyx::pos_type pos = cur.pos();
346 = cur.pit()->getFontSettings(buf.params(), pos);
347 int striked = ssize - cur.pit()->erase(pos, pos + ssize);
348 cur.pit()->insert(pos, replacestr, font);
349 advance(cur, rsize + striked);
353 PosIterator beg = buf.pos_iterator_begin();
354 bv->text()->init(bv);
355 bv->putSelectionAt(beg, 0, false);
362 bool stringSelected(BufferView * bv,
363 string const & searchstr,
364 bool cs, bool mw, bool fw)
366 LyXText * text = bv->getLyXText();
367 // if nothing selected or selection does not equal search
368 // string search and select next occurance and return
369 string const & str1 = searchstr;
370 string const str2 = text->selectionAsString(*bv->buffer(),
372 if ((cs && str1 != str2) || lowercase(str1) != lowercase(str2)) {
373 find(bv, searchstr, cs, mw, fw);
381 int replace(BufferView * bv,
382 string const & searchstr, string const & replacestr,
383 bool cs, bool mw, bool fw)
385 if (!searchAllowed(bv, searchstr) || bv->buffer()->isReadonly())
388 if (!stringSelected(bv, searchstr, cs, mw, fw))
391 LyXText * text = bv->getLyXText();
393 text->replaceSelectionWithString(replacestr);
394 text->setSelectionRange(replacestr.length());
395 bv->cursor().current() = fw ? bv->cursor().selEnd() : bv->cursor().selBegin();
396 bv->buffer()->markDirty();
397 find(bv, searchstr, cs, mw, fw);