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 "CutAndPaste.h"
21 #include "BufferView.h"
23 #include "funcrequest.h"
26 #include "paragraph.h"
27 #include "pariterator.h"
30 #include "frontends/Alert.h"
31 #include "frontends/LyXView.h"
33 #include "support/tostr.h"
37 using lyx::support::lowercase;
38 using lyx::support::uppercase;
39 using lyx::support::split;
45 using std::ostringstream;
51 bool parse_bool(string & howto)
56 howto = split(howto, var, ' ');
61 class MatchString : public std::binary_function<Paragraph, lyx::pos_type, bool>
64 MatchString(string const & str, bool cs, bool mw)
65 : str(str), cs(cs), mw(mw)
68 // returns true if the specified string is at the specified position
69 bool operator()(Paragraph const & par, lyx::pos_type pos) const
71 string::size_type const size = str.length();
73 lyx::pos_type const parsize = par.size();
74 for (i = 0; pos + i < parsize; ++i) {
75 if (string::size_type(i) >= size)
77 if (cs && str[i] != par.getChar(pos + i))
79 if (!cs && uppercase(str[i]) != uppercase(par.getChar(pos + i)))
83 if (size != string::size_type(i))
86 // if necessary, check whether string matches word
88 if (pos > 0 && par.isWord(pos - 1))
90 if (pos + lyx::pos_type(size) < parsize
91 && par.isWord(pos + size));
103 // match whole words only
108 bool findForward(DocIterator & cur, MatchString const & match)
110 for (; cur; cur.forwardChar())
111 if (cur.inTexted() && match(cur.paragraph(), cur.pos()))
117 bool findBackwards(DocIterator & cur, MatchString const & match)
121 if (cur.inTexted() && match(cur.paragraph(), cur.pos()))
128 bool findChange(DocIterator & cur)
130 for (; cur; cur.forwardChar())
131 if (cur.inTexted() && !cur.paragraph().empty() &&
132 cur.paragraph().lookupChange(cur.pos())
133 != Change::UNCHANGED)
139 bool searchAllowed(BufferView * bv, string const & str)
142 Alert::error(_("Search error"), _("Search string is empty"));
145 return bv->available();
149 bool find(BufferView * bv, string const & searchstr, bool cs, bool mw, bool fw)
151 if (!searchAllowed(bv, searchstr))
154 DocIterator cur = bv->cursor();
156 MatchString const match(searchstr, cs, mw);
158 bool found = fw ? findForward(cur, match) : findBackwards(cur, match);
161 bv->putSelectionAt(cur, searchstr.length(), !fw);
167 int replaceAll(BufferView * bv,
168 string const & searchstr, string const & replacestr,
171 Buffer & buf = *bv->buffer();
173 if (!searchAllowed(bv, searchstr) || buf.isReadonly())
176 recordUndoFullDocument(bv->cursor());
178 MatchString const match(searchstr, cs, mw);
181 int const rsize = replacestr.size();
182 int const ssize = searchstr.size();
184 DocIterator cur = doc_iterator_begin(buf.inset());
185 while (findForward(cur, match)) {
186 lyx::pos_type pos = cur.pos();
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)
196 bv->text()->init(bv);
197 bv->putSelectionAt(doc_iterator_begin(buf.inset()), 0, false);
204 bool stringSelected(BufferView * bv, string const & searchstr,
205 bool cs, bool mw, bool fw)
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);
220 int replace(BufferView * bv, string const & searchstr,
221 string const & replacestr, bool cs, bool mw, bool fw)
223 if (!searchAllowed(bv, searchstr) || bv->buffer()->isReadonly())
226 if (!stringSelected(bv, searchstr, cs, mw, fw))
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);
246 string const find2string(string const & search,
247 bool casesensitive, bool matchword, bool forward)
251 << int(casesensitive) << ' '
252 << int(matchword) << ' '
258 string const replace2string(string const & search, string const & replace,
259 bool casesensitive, bool matchword,
260 bool all, bool forward)
265 << int(casesensitive) << ' '
266 << int(matchword) << ' '
273 void find(BufferView * bv, FuncRequest const & ev)
275 if (!bv || ev.action != LFUN_WORD_FIND)
278 lyxerr << "find called, cmd: " << ev << std::endl;
280 // data is of the form
282 // <casesensitive> <matchword> <forward>"
284 string howto = split(ev.argument, search, '\n');
286 bool casesensitive = parse_bool(howto);
287 bool matchword = parse_bool(howto);
288 bool forward = parse_bool(howto);
290 bool const found = ::find(bv, search,
291 casesensitive, matchword, forward);
294 bv->owner()->message(_("String not found!"));
298 void replace(BufferView * bv, FuncRequest const & ev)
300 if (!bv || ev.action != LFUN_WORD_REPLACE)
303 // data is of the form
306 // <casesensitive> <matchword> <all> <forward>"
309 string howto = split(ev.argument, search, '\n');
310 howto = split(howto, replace, '\n');
312 bool casesensitive = parse_bool(howto);
313 bool matchword = parse_bool(howto);
314 bool all = parse_bool(howto);
315 bool forward = parse_bool(howto);
317 LyXView * lv = bv->owner();
319 int const replace_count = all
320 ? ::replaceAll(bv, search, replace, casesensitive, matchword)
321 : ::replace(bv, search, replace, casesensitive, matchword, forward);
323 if (replace_count == 0) {
324 lv->message(_("String not found!"));
326 if (replace_count == 1) {
327 lv->message(_("String has been replaced."));
329 string str = tostr(replace_count);
330 str += _(" strings have been replaced.");
337 bool findNextChange(BufferView * bv)
339 if (!bv->available())
342 DocIterator cur = DocIterator(bv->cursor());
344 if (!findChange(cur))
347 Paragraph const & par = cur.paragraph();
348 const pos_type pos = cur.pos();
350 Change orig_change = par.lookupChangeFull(pos);
351 const pos_type parsize = par.size();
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))
364 pos_type length = end - pos;
365 bv->putSelectionAt(cur, length, false);