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 "buffer_funcs.h"
22 #include "BufferView.h"
24 #include "FuncRequest.h"
27 #include "Paragraph.h"
28 #include "ParIterator.h"
31 #include "frontends/alert.h"
33 #include "support/convert.h"
34 #include "support/docstream.h"
38 using support::compare_no_case;
39 using support::uppercase;
47 bool parse_bool(docstring & howto)
52 howto = split(howto, var, ' ');
57 class MatchString : public std::binary_function<Paragraph, pos_type, bool>
60 MatchString(docstring const & str, bool cs, bool mw)
61 : str(str), cs(cs), mw(mw)
64 // returns true if the specified string is at the specified position
65 bool operator()(Paragraph const & par, pos_type pos) const
67 docstring::size_type const size = str.length();
69 pos_type const parsize = par.size();
70 for (i = 0; pos + i < parsize; ++i) {
71 if (docstring::size_type(i) >= size)
73 if (cs && str[i] != par.getChar(pos + i))
75 if (!cs && uppercase(str[i]) != uppercase(par.getChar(pos + i)))
79 if (size != docstring::size_type(i))
82 // if necessary, check whether string matches word
84 if (pos > 0 && par.isLetter(pos - 1))
86 if (pos + pos_type(size) < parsize
87 && par.isLetter(pos + size))
99 // match whole words only
104 bool findForward(DocIterator & cur, MatchString const & match)
106 for (; cur; cur.forwardChar())
107 if (cur.inTexted() && match(cur.paragraph(), cur.pos()))
113 bool findBackwards(DocIterator & cur, MatchString const & match)
117 if (cur.inTexted() && match(cur.paragraph(), cur.pos()))
124 bool findChange(DocIterator & cur)
126 for (; cur; cur.forwardPos())
127 if (cur.inTexted() && !cur.paragraph().isUnchanged(cur.pos()))
133 bool searchAllowed(BufferView * bv, docstring const & str)
136 frontend::Alert::error(_("Search error"),
137 _("Search string is empty"));
144 bool find(BufferView * bv, docstring const & searchstr, bool cs, bool mw, bool fw)
146 if (!searchAllowed(bv, searchstr))
149 DocIterator cur = bv->cursor();
151 MatchString const match(searchstr, cs, mw);
153 bool found = fw ? findForward(cur, match) : findBackwards(cur, match);
156 bv->putSelectionAt(cur, searchstr.length(), !fw);
162 int replaceAll(BufferView * bv,
163 docstring const & searchstr, docstring const & replacestr,
166 Buffer & buf = *bv->buffer();
168 if (!searchAllowed(bv, searchstr) || buf.isReadonly())
171 recordUndoFullDocument(bv);
173 MatchString const match(searchstr, cs, mw);
176 int const rsize = replacestr.size();
177 int const ssize = searchstr.size();
179 DocIterator cur = doc_iterator_begin(buf.inset());
180 while (findForward(cur, match)) {
181 pos_type pos = cur.pos();
183 = cur.paragraph().getFontSettings(buf.params(), pos);
184 int striked = ssize - cur.paragraph().eraseChars(pos, pos + ssize,
185 buf.params().trackChanges);
186 cur.paragraph().insert(pos, replacestr, font,
187 Change(buf.params().trackChanges ?
188 Change::INSERTED : Change::UNCHANGED));
189 for (int i = 0; i < rsize + striked; ++i)
195 bv->putSelectionAt(doc_iterator_begin(buf.inset()), 0, false);
202 bool stringSelected(BufferView * bv, docstring const & searchstr,
203 bool cs, bool mw, bool fw)
205 // if nothing selected or selection does not equal search
206 // string search and select next occurance and return
207 docstring const & str1 = searchstr;
208 docstring const str2 = bv->cursor().selectionAsString(false);
209 if ((cs && str1 != str2) || compare_no_case(str1, str2) != 0) {
210 find(bv, searchstr, cs, mw, fw);
218 int replace(BufferView * bv, docstring const & searchstr,
219 docstring const & replacestr, bool cs, bool mw, bool fw)
221 if (!searchAllowed(bv, searchstr) || bv->buffer()->isReadonly())
224 if (!stringSelected(bv, searchstr, cs, mw, fw))
227 Cursor & cur = bv->cursor();
228 cap::replaceSelectionWithString(cur, replacestr, fw);
229 bv->buffer()->markDirty();
230 find(bv, searchstr, cs, mw, fw);
239 docstring const find2string(docstring const & search,
240 bool casesensitive, bool matchword, bool forward)
244 << int(casesensitive) << ' '
245 << int(matchword) << ' '
251 docstring const replace2string(docstring const & search, docstring const & replace,
252 bool casesensitive, bool matchword,
253 bool all, bool forward)
258 << int(casesensitive) << ' '
259 << int(matchword) << ' '
266 void find(BufferView * bv, FuncRequest const & ev)
268 if (!bv || ev.action != LFUN_WORD_FIND)
271 //lyxerr << "find called, cmd: " << ev << std::endl;
273 // data is of the form
275 // <casesensitive> <matchword> <forward>"
277 docstring howto = split(ev.argument(), search, '\n');
279 bool casesensitive = parse_bool(howto);
280 bool matchword = parse_bool(howto);
281 bool forward = parse_bool(howto);
283 bool const found = find(bv, search,
284 casesensitive, matchword, forward);
287 // emit message signal.
288 bv->message(_("String not found!"));
292 void replace(BufferView * bv, FuncRequest const & ev)
294 if (!bv || ev.action != LFUN_WORD_REPLACE)
297 // data is of the form
300 // <casesensitive> <matchword> <all> <forward>"
303 docstring howto = split(ev.argument(), search, '\n');
304 howto = split(howto, rplc, '\n');
306 bool casesensitive = parse_bool(howto);
307 bool matchword = parse_bool(howto);
308 bool all = parse_bool(howto);
309 bool forward = parse_bool(howto);
311 Buffer * buf = bv->buffer();
313 int const replace_count = all
314 ? replaceAll(bv, search, rplc, casesensitive, matchword)
315 : replace(bv, search, rplc, casesensitive, matchword, forward);
317 if (replace_count == 0) {
318 // emit message signal.
319 buf->message(_("String not found!"));
321 if (replace_count == 1) {
322 // emit message signal.
323 buf->message(_("String has been replaced."));
325 docstring str = convert<docstring>(replace_count);
326 str += _(" strings have been replaced.");
327 // emit message signal.
334 bool findNextChange(BufferView * bv)
339 DocIterator cur = bv->cursor();
341 if (!findChange(cur))
344 bv->cursor().setCursor(cur);
345 bv->cursor().resetAnchor();
347 Change orig_change = cur.paragraph().lookupChange(cur.pos());
349 DocIterator et = doc_iterator_end(cur.inset());
350 DocIterator ok = cur; // see below
351 for (; cur != et; cur.forwardPosNoDescend()) {
353 Change change = cur.paragraph().lookupChange(cur.pos());
354 if (change != orig_change) {
359 // avoid crash (assertion violation) if the imaginary end-of-par
360 // character of the last paragraph of the document is marked as changed
365 // Now put cursor to end of selection:
366 bv->cursor().setCursor(cur);
367 bv->cursor().setSelection();