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.
19 #include "BufferParams.h"
21 #include "CutAndPaste.h"
22 #include "buffer_funcs.h"
23 #include "BufferView.h"
25 #include "support/debug.h"
26 #include "FuncRequest.h"
27 #include "support/gettext.h"
29 #include "Paragraph.h"
30 #include "ParIterator.h"
32 #include "frontends/alert.h"
34 #include "support/convert.h"
35 #include "support/docstream.h"
36 #include "support/lstrings.h"
39 using namespace lyx::support;
45 bool parse_bool(docstring & howto)
50 howto = split(howto, var, ' ');
55 class MatchString : public binary_function<Paragraph, pos_type, bool>
58 MatchString(docstring const & str, bool cs, bool mw)
59 : str(str), cs(cs), mw(mw)
62 // returns true if the specified string is at the specified position
63 // del specifies whether deleted strings in ct mode will be considered
64 bool operator()(Paragraph const & par, pos_type pos, bool del = true) const
66 return par.find(str, cs, mw, pos, del);
74 // match whole words only
79 bool findForward(DocIterator & cur, MatchString const & match,
82 for (; cur; cur.forwardChar())
84 match(cur.paragraph(), cur.pos(), find_del))
90 bool findBackwards(DocIterator & cur, MatchString const & match,
96 match(cur.paragraph(), cur.pos(), find_del))
103 bool findChange(DocIterator & cur)
105 for (; cur; cur.forwardPos())
106 if (cur.inTexted() && !cur.paragraph().isUnchanged(cur.pos()))
112 bool searchAllowed(BufferView * /*bv*/, docstring const & str)
115 frontend::Alert::error(_("Search error"), _("Search string is empty"));
122 bool find(BufferView * bv, docstring const & searchstr,
123 bool cs, bool mw, bool fw, bool find_del = true)
125 if (!searchAllowed(bv, searchstr))
128 DocIterator cur = bv->cursor();
130 MatchString const match(searchstr, cs, mw);
132 bool found = fw ? findForward(cur, match, find_del) :
133 findBackwards(cur, match, find_del);
136 bv->putSelectionAt(cur, searchstr.length(), !fw);
142 int replaceAll(BufferView * bv,
143 docstring const & searchstr, docstring const & replacestr,
146 Buffer & buf = bv->buffer();
148 if (!searchAllowed(bv, searchstr) || buf.isReadonly())
151 bv->cursor().recordUndoFullDocument();
153 MatchString const match(searchstr, cs, mw);
156 int const rsize = replacestr.size();
157 int const ssize = searchstr.size();
159 DocIterator cur = doc_iterator_begin(buf.inset());
160 while (findForward(cur, match, false)) {
161 pos_type pos = cur.pos();
163 = cur.paragraph().getFontSettings(buf.params(), pos);
164 int striked = ssize - cur.paragraph().eraseChars(pos, pos + ssize,
165 buf.params().trackChanges);
166 cur.paragraph().insert(pos, replacestr, font,
167 Change(buf.params().trackChanges ?
168 Change::INSERTED : Change::UNCHANGED));
169 for (int i = 0; i < rsize + striked; ++i)
175 bv->putSelectionAt(doc_iterator_begin(buf.inset()), 0, false);
182 bool stringSelected(BufferView * bv, docstring const & searchstr,
183 bool cs, bool mw, bool fw)
185 // if nothing selected or selection does not equal search
186 // string search and select next occurance and return
187 docstring const & str1 = searchstr;
188 docstring const str2 = bv->cursor().selectionAsString(false);
189 if ((cs && str1 != str2) || compare_no_case(str1, str2) != 0) {
190 find(bv, searchstr, cs, mw, fw);
198 int replace(BufferView * bv, docstring const & searchstr,
199 docstring const & replacestr, bool cs, bool mw, bool fw)
201 if (!searchAllowed(bv, searchstr) || bv->buffer().isReadonly())
204 if (!stringSelected(bv, searchstr, cs, mw, fw))
207 Cursor & cur = bv->cursor();
208 cap::replaceSelectionWithString(cur, replacestr, fw);
209 bv->buffer().markDirty();
210 find(bv, searchstr, cs, mw, fw, false);
211 bv->buffer().updateMacros();
212 bv->processUpdateFlags(Update::Force | Update::FitCursor);
220 docstring const find2string(docstring const & search,
221 bool casesensitive, bool matchword, bool forward)
225 << int(casesensitive) << ' '
226 << int(matchword) << ' '
232 docstring const replace2string(docstring const & search, docstring const & replace,
233 bool casesensitive, bool matchword,
234 bool all, bool forward)
239 << int(casesensitive) << ' '
240 << int(matchword) << ' '
247 void find(BufferView * bv, FuncRequest const & ev)
249 if (!bv || ev.action != LFUN_WORD_FIND)
252 //lyxerr << "find called, cmd: " << ev << endl;
254 // data is of the form
256 // <casesensitive> <matchword> <forward>"
258 docstring howto = split(ev.argument(), search, '\n');
260 bool casesensitive = parse_bool(howto);
261 bool matchword = parse_bool(howto);
262 bool forward = parse_bool(howto);
264 bool const found = find(bv, search,
265 casesensitive, matchword, forward);
268 // emit message signal.
269 bv->message(_("String not found!"));
273 void replace(BufferView * bv, FuncRequest const & ev, bool has_deleted)
275 if (!bv || ev.action != LFUN_WORD_REPLACE)
278 // data is of the form
281 // <casesensitive> <matchword> <all> <forward>"
284 docstring howto = split(ev.argument(), search, '\n');
285 howto = split(howto, rplc, '\n');
287 bool casesensitive = parse_bool(howto);
288 bool matchword = parse_bool(howto);
289 bool all = parse_bool(howto);
290 bool forward = parse_bool(howto);
293 int const replace_count = all
294 ? replaceAll(bv, search, rplc, casesensitive, matchword)
295 : replace(bv, search, rplc, casesensitive, matchword, forward);
297 Buffer & buf = bv->buffer();
298 if (replace_count == 0) {
299 // emit message signal.
300 buf.message(_("String not found!"));
302 if (replace_count == 1) {
303 // emit message signal.
304 buf.message(_("String has been replaced."));
306 docstring str = convert<docstring>(replace_count);
307 str += _(" strings have been replaced.");
308 // emit message signal.
313 // if we have deleted characters, we do not replace at all, but
314 // rather search for the next occurence
315 bool const found = find(bv, search,
316 casesensitive, matchword, forward);
319 // emit message signal.
320 bv->message(_("String not found!"));
325 bool findNextChange(BufferView * bv)
327 DocIterator cur = bv->cursor();
329 if (!findChange(cur))
332 bv->cursor().setCursor(cur);
333 bv->cursor().resetAnchor();
335 Change orig_change = cur.paragraph().lookupChange(cur.pos());
337 CursorSlice & tip = cur.top();
338 for (; !tip.at_end(); tip.forwardPos()) {
339 Change change = tip.paragraph().lookupChange(tip.pos());
340 if (change != orig_change)
343 // avoid crash (assertion violation) if the imaginary end-of-par
344 // character of the last paragraph of the document is marked as changed
348 // Now put cursor to end of selection:
349 bv->cursor().setCursor(cur);
350 bv->cursor().setSelection();