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 "BufferView.h"
23 #include "funcrequest.h"
26 #include "paragraph.h"
27 #include "pariterator.h"
30 #include "frontends/Alert.h"
32 #include "support/convert.h"
38 using support::lowercase;
39 using support::uppercase;
43 using std::ostringstream;
49 bool parse_bool(string & howto)
54 howto = split(howto, var, ' ');
59 class MatchString : public std::binary_function<Paragraph, pos_type, bool>
62 MatchString(string const & str, bool cs, bool mw)
63 : str(str), cs(cs), mw(mw)
66 // returns true if the specified string is at the specified position
67 bool operator()(Paragraph const & par, pos_type pos) const
69 string::size_type const size = str.length();
71 pos_type const parsize = par.size();
72 for (i = 0; pos + i < parsize; ++i) {
73 if (string::size_type(i) >= size)
75 if (cs && str[i] != par.getChar(pos + i))
77 if (!cs && uppercase(str[i]) != uppercase(par.getChar(pos + i)))
81 if (size != string::size_type(i))
84 // if necessary, check whether string matches word
86 if (pos > 0 && par.isLetter(pos - 1))
88 if (pos + pos_type(size) < parsize
89 && par.isLetter(pos + size))
101 // match whole words only
106 bool findForward(DocIterator & cur, MatchString const & match)
108 for (; cur; cur.forwardChar())
109 if (cur.inTexted() && match(cur.paragraph(), cur.pos()))
115 bool findBackwards(DocIterator & cur, MatchString const & match)
119 if (cur.inTexted() && match(cur.paragraph(), cur.pos()))
126 bool findChange(DocIterator & cur)
128 for (; cur; cur.forwardPos())
129 if (cur.inTexted() && !cur.paragraph().isUnchanged(cur.pos()))
135 bool searchAllowed(BufferView * bv, string const & str)
138 frontend::Alert::error(_("Search error"),
139 _("Search string is empty"));
146 bool find(BufferView * bv, string const & searchstr, bool cs, bool mw, bool fw)
148 if (!searchAllowed(bv, searchstr))
151 DocIterator cur = bv->cursor();
153 MatchString const match(searchstr, cs, mw);
155 bool found = fw ? findForward(cur, match) : findBackwards(cur, match);
158 bv->putSelectionAt(cur, searchstr.length(), !fw);
164 int replaceAll(BufferView * bv,
165 string const & searchstr, string const & replacestr,
168 Buffer & buf = *bv->buffer();
170 if (!searchAllowed(bv, searchstr) || buf.isReadonly())
173 recordUndoFullDocument(bv);
175 MatchString const match(searchstr, cs, mw);
178 int const rsize = replacestr.size();
179 int const ssize = searchstr.size();
181 DocIterator cur = doc_iterator_begin(buf.inset());
182 while (findForward(cur, match)) {
183 pos_type pos = cur.pos();
185 = cur.paragraph().getFontSettings(buf.params(), pos);
186 int striked = ssize - cur.paragraph().eraseChars(pos, pos + ssize,
187 buf.params().trackChanges);
188 cur.paragraph().insert(pos, from_utf8(replacestr), font,
189 Change(buf.params().trackChanges ?
190 Change::INSERTED : Change::UNCHANGED));
191 for (int i = 0; i < rsize + striked; ++i)
196 bv->buffer()->text().init(bv);
197 bv->putSelectionAt(doc_iterator_begin(buf.inset()), 0, false);
204 bool stringSelected(BufferView * bv, string const & searchstr,
205 bool cs, bool mw, bool fw)
207 // if nothing selected or selection does not equal search
208 // string search and select next occurance and return
209 string const & str1 = searchstr;
210 string const str2 = to_utf8(bv->cursor().selectionAsString(false));
211 if ((cs && str1 != str2) || lowercase(str1) != lowercase(str2)) {
212 find(bv, searchstr, cs, mw, fw);
220 int replace(BufferView * bv, string const & searchstr,
221 string const & replacestr, bool cs, bool mw, bool fw)
223 if (!searchAllowed(bv, searchstr) || bv->buffer()->isReadonly())
226 if (!stringSelected(bv, searchstr, cs, mw, fw))
229 LCursor & cur = bv->cursor();
230 cap::replaceSelectionWithString(cur, replacestr, fw);
231 bv->buffer()->markDirty();
232 find(bv, searchstr, cs, mw, fw);
241 string const find2string(string const & search,
242 bool casesensitive, bool matchword, bool forward)
246 << int(casesensitive) << ' '
247 << int(matchword) << ' '
253 string const replace2string(string const & search, string const & replace,
254 bool casesensitive, bool matchword,
255 bool all, bool forward)
260 << int(casesensitive) << ' '
261 << int(matchword) << ' '
268 void find(BufferView * bv, FuncRequest const & ev)
270 if (!bv || ev.action != LFUN_WORD_FIND)
273 //lyxerr << "find called, cmd: " << ev << std::endl;
275 // data is of the form
277 // <casesensitive> <matchword> <forward>"
279 string howto = split(to_utf8(ev.argument()), search, '\n');
281 bool casesensitive = parse_bool(howto);
282 bool matchword = parse_bool(howto);
283 bool forward = parse_bool(howto);
285 bool const found = find(bv, search,
286 casesensitive, matchword, forward);
289 // emit message signal.
290 bv->message(_("String not found!"));
294 void replace(BufferView * bv, FuncRequest const & ev)
296 if (!bv || ev.action != LFUN_WORD_REPLACE)
299 // data is of the form
302 // <casesensitive> <matchword> <all> <forward>"
305 string howto = split(to_utf8(ev.argument()), search, '\n');
306 howto = split(howto, rplc, '\n');
308 bool casesensitive = parse_bool(howto);
309 bool matchword = parse_bool(howto);
310 bool all = parse_bool(howto);
311 bool forward = parse_bool(howto);
313 Buffer * buf = bv->buffer();
315 int const replace_count = all
316 ? replaceAll(bv, search, rplc, casesensitive, matchword)
317 : replace(bv, search, rplc, casesensitive, matchword, forward);
319 if (replace_count == 0) {
320 // emit message signal.
321 buf->message(_("String not found!"));
323 if (replace_count == 1) {
324 // emit message signal.
325 buf->message(_("String has been replaced."));
327 docstring str = convert<docstring>(replace_count);
328 str += _(" strings have been replaced.");
329 // emit message signal.
336 bool findNextChange(BufferView * bv)
341 DocIterator cur = bv->cursor();
343 if (!findChange(cur))
346 bv->cursor().setCursor(cur);
347 bv->cursor().resetAnchor();
349 Change orig_change = cur.paragraph().lookupChange(cur.pos());
351 DocIterator et = doc_iterator_end(cur.inset());
352 for (; cur != et; cur.forwardPosNoDescend()) {
353 Change change = cur.paragraph().lookupChange(cur.pos());
354 if (change != orig_change) {
358 // Now put cursor to end of selection:
359 bv->cursor().setCursor(cur);
360 bv->cursor().setSelection();
361 // if we used a lfun like in find/replace, dispatch would do