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"
32 #include "frontends/Selection.h"
34 #include "support/convert.h"
35 #include "support/docstream.h"
39 using support::lowercase;
40 using support::uppercase;
48 bool parse_bool(docstring & howto)
53 howto = split(howto, var, ' ');
58 class MatchString : public std::binary_function<Paragraph, pos_type, bool>
61 MatchString(docstring const & str, bool cs, bool mw)
62 : str(str), cs(cs), mw(mw)
65 // returns true if the specified string is at the specified position
66 bool operator()(Paragraph const & par, pos_type pos) 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)))
80 if (size != docstring::size_type(i))
83 // if necessary, check whether string matches word
85 if (pos > 0 && par.isLetter(pos - 1))
87 if (pos + pos_type(size) < parsize
88 && par.isLetter(pos + size))
100 // match whole words only
105 bool findForward(DocIterator & cur, MatchString const & match)
107 for (; cur; cur.forwardChar())
108 if (cur.inTexted() && match(cur.paragraph(), cur.pos()))
114 bool findBackwards(DocIterator & cur, MatchString const & match)
118 if (cur.inTexted() && match(cur.paragraph(), cur.pos()))
125 bool findChange(DocIterator & cur)
127 for (; cur; cur.forwardPos())
128 if (cur.inTexted() && !cur.paragraph().isUnchanged(cur.pos()))
134 bool searchAllowed(BufferView * bv, docstring const & str)
137 frontend::Alert::error(_("Search error"),
138 _("Search string is empty"));
145 bool find(BufferView * bv, docstring const & searchstr, bool cs, bool mw, bool fw)
147 if (!searchAllowed(bv, searchstr))
150 DocIterator cur = bv->cursor();
152 MatchString const match(searchstr, cs, mw);
154 bool found = fw ? findForward(cur, match) : findBackwards(cur, match);
157 bv->putSelectionAt(cur, searchstr.length(), !fw);
163 int replaceAll(BufferView * bv,
164 docstring const & searchstr, docstring const & replacestr,
167 Buffer & buf = *bv->buffer();
169 if (!searchAllowed(bv, searchstr) || buf.isReadonly())
172 recordUndoFullDocument(bv);
174 MatchString const match(searchstr, cs, mw);
177 int const rsize = replacestr.size();
178 int const ssize = searchstr.size();
180 DocIterator cur = doc_iterator_begin(buf.inset());
181 while (findForward(cur, match)) {
182 pos_type pos = cur.pos();
184 = cur.paragraph().getFontSettings(buf.params(), pos);
185 int striked = ssize - cur.paragraph().eraseChars(pos, pos + ssize,
186 buf.params().trackChanges);
187 cur.paragraph().insert(pos, replacestr, font,
188 Change(buf.params().trackChanges ?
189 Change::INSERTED : Change::UNCHANGED));
190 for (int i = 0; i < rsize + striked; ++i)
196 bv->putSelectionAt(doc_iterator_begin(buf.inset()), 0, false);
203 bool stringSelected(BufferView * bv, docstring const & searchstr,
204 bool cs, bool mw, bool fw)
206 // if nothing selected or selection does not equal search
207 // string search and select next occurance and return
208 docstring const & str1 = searchstr;
209 docstring const str2 = bv->cursor().selectionAsString(false);
210 if ((cs && str1 != str2) || lowercase(str1) != lowercase(str2)) {
211 find(bv, searchstr, cs, mw, fw);
219 int replace(BufferView * bv, docstring const & searchstr,
220 docstring const & replacestr, bool cs, bool mw, bool fw)
222 if (!searchAllowed(bv, searchstr) || bv->buffer()->isReadonly())
225 if (!stringSelected(bv, searchstr, cs, mw, fw))
228 LCursor & cur = bv->cursor();
229 cap::replaceSelectionWithString(cur, replacestr, fw);
230 bv->buffer()->markDirty();
231 find(bv, searchstr, cs, mw, fw);
240 docstring const find2string(docstring const & search,
241 bool casesensitive, bool matchword, bool forward)
245 << int(casesensitive) << ' '
246 << int(matchword) << ' '
252 docstring const replace2string(docstring const & search, docstring const & replace,
253 bool casesensitive, bool matchword,
254 bool all, bool forward)
259 << int(casesensitive) << ' '
260 << int(matchword) << ' '
267 void find(BufferView * bv, FuncRequest const & ev)
269 if (!bv || ev.action != LFUN_WORD_FIND)
272 //lyxerr << "find called, cmd: " << ev << std::endl;
274 // data is of the form
276 // <casesensitive> <matchword> <forward>"
278 docstring howto = split(ev.argument(), search, '\n');
280 bool casesensitive = parse_bool(howto);
281 bool matchword = parse_bool(howto);
282 bool forward = parse_bool(howto);
284 bool const found = find(bv, search,
285 casesensitive, matchword, forward);
288 // emit message signal.
289 bv->message(_("String not found!"));
293 void replace(BufferView * bv, FuncRequest const & ev)
295 if (!bv || ev.action != LFUN_WORD_REPLACE)
298 // data is of the form
301 // <casesensitive> <matchword> <all> <forward>"
304 docstring howto = split(ev.argument(), search, '\n');
305 howto = split(howto, rplc, '\n');
307 bool casesensitive = parse_bool(howto);
308 bool matchword = parse_bool(howto);
309 bool all = parse_bool(howto);
310 bool forward = parse_bool(howto);
312 Buffer * buf = bv->buffer();
314 int const replace_count = all
315 ? replaceAll(bv, search, rplc, casesensitive, matchword)
316 : replace(bv, search, rplc, casesensitive, matchword, forward);
318 if (replace_count == 0) {
319 // emit message signal.
320 buf->message(_("String not found!"));
322 if (replace_count == 1) {
323 // emit message signal.
324 buf->message(_("String has been replaced."));
326 docstring str = convert<docstring>(replace_count);
327 str += _(" strings have been replaced.");
328 // emit message signal.
335 bool findNextChange(BufferView * bv)
340 DocIterator cur = bv->cursor();
342 if (!findChange(cur))
345 bv->cursor().setCursor(cur);
346 bv->cursor().resetAnchor();
348 Change orig_change = cur.paragraph().lookupChange(cur.pos());
350 DocIterator et = doc_iterator_end(cur.inset());
351 for (; cur != et; cur.forwardPosNoDescend()) {
352 Change change = cur.paragraph().lookupChange(cur.pos());
353 if (change != orig_change) {
357 // Now put cursor to end of selection:
358 bv->cursor().setCursor(cur);
359 bv->cursor().setSelection();
360 theSelection().haveSelection(bv->cursor().selection());
361 // if we used a lfun like in find/replace, dispatch would do