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 // del specifies whether deleted strings in ct mode will be considered
66 bool operator()(Paragraph const & par, pos_type pos, bool del = true) const
68 docstring::size_type const size = str.length();
70 pos_type const parsize = par.size();
71 for (i = 0; pos + i < parsize; ++i) {
72 if (docstring::size_type(i) >= size)
74 if (cs && str[i] != par.getChar(pos + i))
76 if (!cs && uppercase(str[i]) != uppercase(par.getChar(pos + i)))
78 if (!del && par.isDeleted(pos + i))
82 if (size != docstring::size_type(i))
85 // if necessary, check whether string matches word
87 if (pos > 0 && par.isLetter(pos - 1))
89 if (pos + pos_type(size) < parsize
90 && par.isLetter(pos + size))
102 // match whole words only
107 bool findForward(DocIterator & cur, MatchString const & match,
108 bool find_del = true)
110 for (; cur; cur.forwardChar())
111 if (cur.inTexted() &&
112 match(cur.paragraph(), cur.pos(), find_del))
118 bool findBackwards(DocIterator & cur, MatchString const & match,
119 bool find_del = true)
123 if (cur.inTexted() &&
124 match(cur.paragraph(), cur.pos(), find_del))
131 bool findChange(DocIterator & cur)
133 for (; cur; cur.forwardPos())
134 if (cur.inTexted() && !cur.paragraph().isUnchanged(cur.pos()))
140 bool searchAllowed(BufferView * bv, docstring const & str)
143 frontend::Alert::error(_("Search error"),
144 _("Search string is empty"));
151 bool find(BufferView * bv, docstring const & searchstr, bool cs, bool mw, bool fw,
152 bool find_del = true)
154 if (!searchAllowed(bv, searchstr))
157 DocIterator cur = bv->cursor();
159 MatchString const match(searchstr, cs, mw);
161 bool found = fw ? findForward(cur, match, find_del) :
162 findBackwards(cur, match, find_del);
165 bv->putSelectionAt(cur, searchstr.length(), !fw);
171 int replaceAll(BufferView * bv,
172 docstring const & searchstr, docstring const & replacestr,
175 Buffer & buf = *bv->buffer();
177 if (!searchAllowed(bv, searchstr) || buf.isReadonly())
180 recordUndoFullDocument(bv);
182 MatchString const match(searchstr, cs, mw);
185 int const rsize = replacestr.size();
186 int const ssize = searchstr.size();
188 DocIterator cur = doc_iterator_begin(buf.inset());
189 while (findForward(cur, match, false)) {
190 pos_type pos = cur.pos();
192 = cur.paragraph().getFontSettings(buf.params(), pos);
193 int striked = ssize - cur.paragraph().eraseChars(pos, pos + ssize,
194 buf.params().trackChanges);
195 cur.paragraph().insert(pos, replacestr, font,
196 Change(buf.params().trackChanges ?
197 Change::INSERTED : Change::UNCHANGED));
198 for (int i = 0; i < rsize + striked; ++i)
204 bv->putSelectionAt(doc_iterator_begin(buf.inset()), 0, false);
211 bool stringSelected(BufferView * bv, docstring const & searchstr,
212 bool cs, bool mw, bool fw)
214 // if nothing selected or selection does not equal search
215 // string search and select next occurance and return
216 docstring const & str1 = searchstr;
217 docstring const str2 = bv->cursor().selectionAsString(false);
218 if ((cs && str1 != str2) || compare_no_case(str1, str2) != 0) {
219 find(bv, searchstr, cs, mw, fw);
227 int replace(BufferView * bv, docstring const & searchstr,
228 docstring const & replacestr, bool cs, bool mw, bool fw)
230 if (!searchAllowed(bv, searchstr) || bv->buffer()->isReadonly())
233 if (!stringSelected(bv, searchstr, cs, mw, fw))
236 Cursor & cur = bv->cursor();
237 cap::replaceSelectionWithString(cur, replacestr, fw);
238 bv->buffer()->markDirty();
239 find(bv, searchstr, cs, mw, fw, false);
248 docstring const find2string(docstring const & search,
249 bool casesensitive, bool matchword, bool forward)
253 << int(casesensitive) << ' '
254 << int(matchword) << ' '
260 docstring const replace2string(docstring const & search, docstring const & replace,
261 bool casesensitive, bool matchword,
262 bool all, bool forward)
267 << int(casesensitive) << ' '
268 << int(matchword) << ' '
275 void find(BufferView * bv, FuncRequest const & ev)
277 if (!bv || ev.action != LFUN_WORD_FIND)
280 //lyxerr << "find called, cmd: " << ev << std::endl;
282 // data is of the form
284 // <casesensitive> <matchword> <forward>"
286 docstring howto = split(ev.argument(), search, '\n');
288 bool casesensitive = parse_bool(howto);
289 bool matchword = parse_bool(howto);
290 bool forward = parse_bool(howto);
292 bool const found = find(bv, search,
293 casesensitive, matchword, forward);
296 // emit message signal.
297 bv->message(_("String not found!"));
301 void replace(BufferView * bv, FuncRequest const & ev)
303 if (!bv || ev.action != LFUN_WORD_REPLACE)
306 // data is of the form
309 // <casesensitive> <matchword> <all> <forward>"
312 docstring howto = split(ev.argument(), search, '\n');
313 howto = split(howto, rplc, '\n');
315 bool casesensitive = parse_bool(howto);
316 bool matchword = parse_bool(howto);
317 bool all = parse_bool(howto);
318 bool forward = parse_bool(howto);
320 Buffer * buf = bv->buffer();
322 int const replace_count = all
323 ? replaceAll(bv, search, rplc, casesensitive, matchword)
324 : replace(bv, search, rplc, casesensitive, matchword, forward);
326 if (replace_count == 0) {
327 // emit message signal.
328 buf->message(_("String not found!"));
330 if (replace_count == 1) {
331 // emit message signal.
332 buf->message(_("String has been replaced."));
334 docstring str = convert<docstring>(replace_count);
335 str += _(" strings have been replaced.");
336 // emit message signal.
343 bool findNextChange(BufferView * bv)
348 DocIterator cur = bv->cursor();
350 if (!findChange(cur))
353 bv->cursor().setCursor(cur);
354 bv->cursor().resetAnchor();
356 Change orig_change = cur.paragraph().lookupChange(cur.pos());
358 DocIterator et = doc_iterator_end(cur.inset());
359 DocIterator ok = cur; // see below
360 for (; cur != et; cur.forwardPosNoDescend()) {
362 Change change = cur.paragraph().lookupChange(cur.pos());
363 if (change != orig_change) {
368 // avoid crash (assertion violation) if the imaginary end-of-par
369 // character of the last paragraph of the document is marked as changed
374 // Now put cursor to end of selection:
375 bv->cursor().setCursor(cur);
376 bv->cursor().setSelection();