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 lyxerr << "find called, cmd: " << ev << std::endl;
117 // data is of the form
119 // <casesensitive> <matchword> <forward>"
121 string howto = split(ev.argument, search, '\n');
123 bool casesensitive = parse_bool(howto);
124 bool matchword = parse_bool(howto);
125 bool forward = parse_bool(howto);
127 bool const found = ::find(bv, search,
128 casesensitive, matchword, forward);
131 bv->owner()->message(_("String not found!"));
135 void replace(BufferView * bv, FuncRequest const & ev)
137 if (!bv || ev.action != LFUN_WORD_REPLACE)
140 // data is of the form
143 // <casesensitive> <matchword> <all> <forward>"
146 string howto = split(ev.argument, search, '\n');
147 howto = split(howto, replace, '\n');
149 bool casesensitive = parse_bool(howto);
150 bool matchword = parse_bool(howto);
151 bool all = parse_bool(howto);
152 bool forward = parse_bool(howto);
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 bv->putSelectionAt(cur, length, true);
215 class MatchString : public std::binary_function<Paragraph, lyx::pos_type, bool>
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 for (i = 0; pos + i < parsize; ++i) {
229 if (string::size_type(i) >= size)
231 if (cs && str[i] != par.getChar(pos + i))
233 if (!cs && uppercase(str[i]) != uppercase(par.getChar(pos + i)))
237 if (size != string::size_type(i))
240 // if necessary, check whether string matches word
242 if (pos > 0 && IsLetterCharOrDigit(par.getChar(pos - 1)))
244 if (pos + lyx::pos_type(size) < parsize
245 && IsLetterCharOrDigit(par.getChar(pos + size)));
257 // match whole words only
262 bool findForward(PosIterator & cur, PosIterator const & end,
263 MatchString const & match)
265 for (; cur != end; ++cur) {
266 if (match(*cur.pit(), cur.pos()))
273 bool findBackwards(PosIterator & cur, PosIterator const & beg,
274 MatchString const & match)
278 if (match(*cur.pit(), cur.pos()))
285 bool findChange(PosIterator & cur, PosIterator const & end)
287 for (; cur != end; ++cur) {
288 if ((cur.pit()->empty() || !cur.at_end())
289 && cur.pit()->lookupChange(cur.pos()) != Change::UNCHANGED)
296 bool searchAllowed(BufferView * bv, string const & str)
299 Alert::error(_("Search error"), _("Search string is empty"));
302 return bv->available();
306 bool find(BufferView * bv, string const & searchstr, bool cs, bool mw, bool fw)
308 if (!searchAllowed(bv, searchstr))
311 PosIterator cur = PosIterator(*bv);
313 MatchString const match(searchstr, cs, mw);
315 PosIterator const end = bv->buffer()->pos_iterator_end();
316 PosIterator const beg = bv->buffer()->pos_iterator_begin();
318 bool found = fw ? findForward(cur, end, match)
319 : findBackwards(cur, beg, match);
322 bv->putSelectionAt(cur, searchstr.length(), !fw);
328 int replaceAll(BufferView * bv,
329 string const & searchstr, string const & replacestr,
332 Buffer & buf = *bv->buffer();
334 if (!searchAllowed(bv, searchstr) || buf.isReadonly())
337 recordUndoFullDocument(bv->cursor());
339 PosIterator cur = buf.pos_iterator_begin();
340 PosIterator const end = buf.pos_iterator_end();
341 MatchString const match(searchstr, cs, mw);
344 int const rsize = replacestr.size();
345 int const ssize = searchstr.size();
347 while (findForward(cur, end, match)) {
348 lyx::pos_type pos = cur.pos();
350 = cur.pit()->getFontSettings(buf.params(), pos);
351 int striked = ssize - cur.pit()->erase(pos, pos + ssize);
352 cur.pit()->insert(pos, replacestr, font);
353 advance(cur, rsize + striked);
357 PosIterator beg = buf.pos_iterator_begin();
358 bv->text()->init(bv);
359 bv->putSelectionAt(beg, 0, false);
366 bool stringSelected(BufferView * bv, string const & searchstr,
367 bool cs, bool mw, bool fw)
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 = bv->cursor().selectionAsString(false);
373 if ((cs && str1 != str2) || lowercase(str1) != lowercase(str2)) {
374 find(bv, searchstr, cs, mw, fw);
382 int replace(BufferView * bv, string const & searchstr,
383 string const & replacestr, 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(bv->cursor(), replacestr);
394 text->setSelectionRange(bv->cursor(), replacestr.length());
395 bv->cursor().top() = fw ? bv->cursor().selEnd() : bv->cursor().selBegin();
396 bv->buffer()->markDirty();
397 find(bv, searchstr, cs, mw, fw);