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() &&
130 cur.paragraph().lookupChange(cur.pos()).type != Change::UNCHANGED)
136 bool searchAllowed(BufferView * bv, string const & str)
139 frontend::Alert::error(_("Search error"),
140 _("Search string is empty"));
147 bool find(BufferView * bv, string const & searchstr, bool cs, bool mw, bool fw)
149 if (!searchAllowed(bv, searchstr))
152 DocIterator cur = bv->cursor();
154 MatchString const match(searchstr, cs, mw);
156 bool found = fw ? findForward(cur, match) : findBackwards(cur, match);
159 bv->putSelectionAt(cur, searchstr.length(), !fw);
165 int replaceAll(BufferView * bv,
166 string const & searchstr, string const & replacestr,
169 Buffer & buf = *bv->buffer();
171 if (!searchAllowed(bv, searchstr) || buf.isReadonly())
174 recordUndoFullDocument(bv);
176 MatchString const match(searchstr, cs, mw);
179 int const rsize = replacestr.size();
180 int const ssize = searchstr.size();
182 DocIterator cur = doc_iterator_begin(buf.inset());
183 while (findForward(cur, match)) {
184 pos_type pos = cur.pos();
186 = cur.paragraph().getFontSettings(buf.params(), pos);
187 int striked = ssize - cur.paragraph().eraseChars(pos, pos + ssize,
188 buf.params().trackChanges);
189 cur.paragraph().insert(pos, from_utf8(replacestr), font,
190 Change(buf.params().trackChanges ?
191 Change::INSERTED : Change::UNCHANGED));
192 for (int i = 0; i < rsize + striked; ++i)
197 bv->buffer()->text().init(bv);
198 bv->putSelectionAt(doc_iterator_begin(buf.inset()), 0, false);
205 bool stringSelected(BufferView * bv, string const & searchstr,
206 bool cs, bool mw, bool fw)
208 // if nothing selected or selection does not equal search
209 // string search and select next occurance and return
210 string const & str1 = searchstr;
211 string const str2 = to_utf8(bv->cursor().selectionAsString(false));
212 if ((cs && str1 != str2) || lowercase(str1) != lowercase(str2)) {
213 find(bv, searchstr, cs, mw, fw);
221 int replace(BufferView * bv, string const & searchstr,
222 string const & replacestr, bool cs, bool mw, bool fw)
224 if (!searchAllowed(bv, searchstr) || bv->buffer()->isReadonly())
227 if (!stringSelected(bv, searchstr, cs, mw, fw))
230 LCursor & cur = bv->cursor();
231 cap::replaceSelectionWithString(cur, replacestr, fw);
232 bv->buffer()->markDirty();
233 find(bv, searchstr, cs, mw, fw);
242 string const find2string(string const & search,
243 bool casesensitive, bool matchword, bool forward)
247 << int(casesensitive) << ' '
248 << int(matchword) << ' '
254 string const replace2string(string const & search, string const & replace,
255 bool casesensitive, bool matchword,
256 bool all, bool forward)
261 << int(casesensitive) << ' '
262 << int(matchword) << ' '
269 void find(BufferView * bv, FuncRequest const & ev)
271 if (!bv || ev.action != LFUN_WORD_FIND)
274 //lyxerr << "find called, cmd: " << ev << std::endl;
276 // data is of the form
278 // <casesensitive> <matchword> <forward>"
280 string howto = split(to_utf8(ev.argument()), search, '\n');
282 bool casesensitive = parse_bool(howto);
283 bool matchword = parse_bool(howto);
284 bool forward = parse_bool(howto);
286 bool const found = find(bv, search,
287 casesensitive, matchword, forward);
290 // emit message signal.
291 bv->message(_("String not found!"));
295 void replace(BufferView * bv, FuncRequest const & ev)
297 if (!bv || ev.action != LFUN_WORD_REPLACE)
300 // data is of the form
303 // <casesensitive> <matchword> <all> <forward>"
306 string howto = split(to_utf8(ev.argument()), search, '\n');
307 howto = split(howto, rplc, '\n');
309 bool casesensitive = parse_bool(howto);
310 bool matchword = parse_bool(howto);
311 bool all = parse_bool(howto);
312 bool forward = parse_bool(howto);
314 Buffer * buf = bv->buffer();
316 int const replace_count = all
317 ? replaceAll(bv, search, rplc, casesensitive, matchword)
318 : replace(bv, search, rplc, casesensitive, matchword, forward);
320 if (replace_count == 0) {
321 // emit message signal.
322 buf->message(_("String not found!"));
324 if (replace_count == 1) {
325 // emit message signal.
326 buf->message(_("String has been replaced."));
328 docstring str = convert<docstring>(replace_count);
329 str += _(" strings have been replaced.");
330 // emit message signal.
337 bool findNextChange(BufferView * bv)
342 DocIterator cur = bv->cursor();
344 if (!findChange(cur))
347 bv->cursor().setCursor(cur);
348 bv->cursor().resetAnchor();
350 Change orig_change = cur.paragraph().lookupChange(cur.pos());
352 DocIterator et = doc_iterator_end(cur.inset());
353 for (; cur != et; cur.forwardPosNoDescend()) {
354 Change change = cur.paragraph().lookupChange(cur.pos());
355 if (change != orig_change) {
359 // Now put cursor to end of selection:
360 bv->cursor().setCursor(cur);
361 bv->cursor().setSelection();
362 // if we used a lfun like in find/replace, dispatch would do