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"
26 #include "FuncRequest.h"
29 #include "Paragraph.h"
30 #include "ParIterator.h"
32 #include "frontends/alert.h"
34 #include "support/convert.h"
35 #include "support/docstream.h"
39 using support::compare_no_case;
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 // del specifies whether deleted strings in ct mode will be considered
67 bool operator()(Paragraph const & par, pos_type pos, bool del = true) const
69 docstring::size_type const size = str.length();
71 pos_type const parsize = par.size();
72 for (i = 0; pos + i < parsize; ++i) {
73 if (docstring::size_type(i) >= size)
75 if (cs && str[i] != par.getChar(pos + i))
77 if (!cs && uppercase(str[i]) != uppercase(par.getChar(pos + i)))
79 if (!del && par.isDeleted(pos + i))
83 if (size != docstring::size_type(i))
86 // if necessary, check whether string matches word
88 if (pos > 0 && par.isLetter(pos - 1))
90 if (pos + pos_type(size) < parsize
91 && par.isLetter(pos + size))
103 // match whole words only
108 bool findForward(DocIterator & cur, MatchString const & match,
109 bool find_del = true)
111 for (; cur; cur.forwardChar())
112 if (cur.inTexted() &&
113 match(cur.paragraph(), cur.pos(), find_del))
119 bool findBackwards(DocIterator & cur, MatchString const & match,
120 bool find_del = true)
124 if (cur.inTexted() &&
125 match(cur.paragraph(), cur.pos(), find_del))
132 bool findChange(DocIterator & cur)
134 for (; cur; cur.forwardPos())
135 if (cur.inTexted() && !cur.paragraph().isUnchanged(cur.pos()))
141 bool searchAllowed(BufferView * bv, docstring const & str)
144 frontend::Alert::error(_("Search error"),
145 _("Search string is empty"));
152 bool find(BufferView * bv, docstring const & searchstr, bool cs, bool mw, bool fw,
153 bool find_del = true)
155 if (!searchAllowed(bv, searchstr))
158 DocIterator cur = bv->cursor();
160 MatchString const match(searchstr, cs, mw);
162 bool found = fw ? findForward(cur, match, find_del) :
163 findBackwards(cur, match, find_del);
166 bv->putSelectionAt(cur, searchstr.length(), !fw);
172 int replaceAll(BufferView * bv,
173 docstring const & searchstr, docstring const & replacestr,
176 Buffer & buf = bv->buffer();
178 if (!searchAllowed(bv, searchstr) || buf.isReadonly())
181 bv->cursor().recordUndoFullDocument();
183 MatchString const match(searchstr, cs, mw);
186 int const rsize = replacestr.size();
187 int const ssize = searchstr.size();
189 DocIterator cur = doc_iterator_begin(buf.inset());
190 while (findForward(cur, match, false)) {
191 pos_type pos = cur.pos();
193 = cur.paragraph().getFontSettings(buf.params(), pos);
194 int striked = ssize - cur.paragraph().eraseChars(pos, pos + ssize,
195 buf.params().trackChanges);
196 cur.paragraph().insert(pos, replacestr, font,
197 Change(buf.params().trackChanges ?
198 Change::INSERTED : Change::UNCHANGED));
199 for (int i = 0; i < rsize + striked; ++i)
205 bv->putSelectionAt(doc_iterator_begin(buf.inset()), 0, false);
212 bool stringSelected(BufferView * bv, docstring const & searchstr,
213 bool cs, bool mw, bool fw)
215 // if nothing selected or selection does not equal search
216 // string search and select next occurance and return
217 docstring const & str1 = searchstr;
218 docstring const str2 = bv->cursor().selectionAsString(false);
219 if ((cs && str1 != str2) || compare_no_case(str1, str2) != 0) {
220 find(bv, searchstr, cs, mw, fw);
228 int replace(BufferView * bv, docstring const & searchstr,
229 docstring const & replacestr, bool cs, bool mw, bool fw)
231 if (!searchAllowed(bv, searchstr) || bv->buffer().isReadonly())
234 if (!stringSelected(bv, searchstr, cs, mw, fw))
237 Cursor & cur = bv->cursor();
238 cap::replaceSelectionWithString(cur, replacestr, fw);
239 bv->buffer().markDirty();
240 find(bv, searchstr, cs, mw, fw, false);
241 bv->processUpdateFlags(Update::Force | Update::FitCursor);
249 docstring const find2string(docstring const & search,
250 bool casesensitive, bool matchword, bool forward)
254 << int(casesensitive) << ' '
255 << int(matchword) << ' '
261 docstring const replace2string(docstring const & search, docstring const & replace,
262 bool casesensitive, bool matchword,
263 bool all, bool forward)
268 << int(casesensitive) << ' '
269 << int(matchword) << ' '
276 void find(BufferView * bv, FuncRequest const & ev)
278 if (!bv || ev.action != LFUN_WORD_FIND)
281 //lyxerr << "find called, cmd: " << ev << std::endl;
283 // data is of the form
285 // <casesensitive> <matchword> <forward>"
287 docstring howto = split(ev.argument(), search, '\n');
289 bool casesensitive = parse_bool(howto);
290 bool matchword = parse_bool(howto);
291 bool forward = parse_bool(howto);
293 bool const found = find(bv, search,
294 casesensitive, matchword, forward);
297 // emit message signal.
298 bv->message(_("String not found!"));
302 void replace(BufferView * bv, FuncRequest const & ev, bool has_deleted)
304 if (!bv || ev.action != LFUN_WORD_REPLACE)
307 // data is of the form
310 // <casesensitive> <matchword> <all> <forward>"
313 docstring howto = split(ev.argument(), search, '\n');
314 howto = split(howto, rplc, '\n');
316 bool casesensitive = parse_bool(howto);
317 bool matchword = parse_bool(howto);
318 bool all = parse_bool(howto);
319 bool forward = parse_bool(howto);
322 int const replace_count = all
323 ? replaceAll(bv, search, rplc, casesensitive, matchword)
324 : replace(bv, search, rplc, casesensitive, matchword, forward);
326 Buffer & buf = bv->buffer();
327 if (replace_count == 0) {
328 // emit message signal.
329 buf.message(_("String not found!"));
331 if (replace_count == 1) {
332 // emit message signal.
333 buf.message(_("String has been replaced."));
335 docstring str = convert<docstring>(replace_count);
336 str += _(" strings have been replaced.");
337 // emit message signal.
342 // if we have deleted characters, we do not replace at all, but
343 // rather search for the next occurence
344 bool const found = find(bv, search,
345 casesensitive, matchword, forward);
348 // emit message signal.
349 bv->message(_("String not found!"));
354 bool findNextChange(BufferView * bv)
356 DocIterator cur = bv->cursor();
358 if (!findChange(cur))
361 bv->cursor().setCursor(cur);
362 bv->cursor().resetAnchor();
364 Change orig_change = cur.paragraph().lookupChange(cur.pos());
366 CursorSlice & tip = cur.top();
367 for (; !tip.at_end(); tip.forwardPos()) {
368 Change change = tip.paragraph().lookupChange(tip.pos());
369 if (change != orig_change)
372 // avoid crash (assertion violation) if the imaginary end-of-par
373 // character of the last paragraph of the document is marked as changed
377 // Now put cursor to end of selection:
378 bv->cursor().setCursor(cur);
379 bv->cursor().setSelection();