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"
36 using lyx::support::lowercase;
37 using lyx::support::uppercase;
38 using lyx::support::split;
44 using std::ostringstream;
50 bool parse_bool(string & howto)
55 howto = split(howto, var, ' ');
60 class MatchString : public std::binary_function<Paragraph, lyx::pos_type, bool>
63 MatchString(string const & str, bool cs, bool mw)
64 : str(str), cs(cs), mw(mw)
67 // returns true if the specified string is at the specified position
68 bool operator()(Paragraph const & par, lyx::pos_type pos) const
70 string::size_type const size = str.length();
72 lyx::pos_type const parsize = par.size();
73 for (i = 0; pos + i < parsize; ++i) {
74 if (string::size_type(i) >= size)
76 if (cs && str[i] != par.getChar(pos + i))
78 if (!cs && uppercase(str[i]) != uppercase(par.getChar(pos + i)))
82 if (size != string::size_type(i))
85 // if necessary, check whether string matches word
87 if (pos > 0 && par.isLetter(pos - 1))
89 if (pos + lyx::pos_type(size) < parsize
90 && par.isLetter(pos + size))
102 // match whole words only
107 bool findForward(DocIterator & cur, MatchString const & match)
109 for (; cur; cur.forwardChar())
110 if (cur.inTexted() && match(cur.paragraph(), cur.pos()))
116 bool findBackwards(DocIterator & cur, MatchString const & match)
120 if (cur.inTexted() && match(cur.paragraph(), cur.pos()))
127 bool findChange(DocIterator & cur)
129 for (; cur; cur.forwardPos())
130 if (cur.inTexted() && cur.paragraph().lookupChange(cur.pos()).type
131 != Change::UNCHANGED)
137 bool searchAllowed(BufferView * bv, string const & str)
140 Alert::error(_("Search error"), _("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 lyx::pos_type pos = cur.pos();
186 = cur.paragraph().getFontSettings(buf.params(), pos);
187 int striked = ssize - cur.paragraph().erase(pos, pos + ssize);
188 cur.paragraph().insert(pos, replacestr, font);
189 for (int i = 0; i < rsize + striked; ++i)
194 bv->buffer()->text().init(bv);
195 bv->putSelectionAt(doc_iterator_begin(buf.inset()), 0, false);
202 bool stringSelected(BufferView * bv, string const & searchstr,
203 bool cs, bool mw, bool fw)
205 // if nothing selected or selection does not equal search
206 // string search and select next occurance and return
207 string const & str1 = searchstr;
208 string const str2 = lyx::to_utf8(bv->cursor().selectionAsString(false));
209 if ((cs && str1 != str2) || lowercase(str1) != lowercase(str2)) {
210 find(bv, searchstr, cs, mw, fw);
218 int replace(BufferView * bv, string const & searchstr,
219 string const & replacestr, bool cs, bool mw, bool fw)
221 if (!searchAllowed(bv, searchstr) || bv->buffer()->isReadonly())
224 if (!stringSelected(bv, searchstr, cs, mw, fw))
227 LCursor & cur = bv->cursor();
228 lyx::cap::replaceSelectionWithString(cur, replacestr, fw);
229 bv->buffer()->markDirty();
230 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(lyx::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(lyx::to_utf8(ev.argument()), search, '\n');
307 howto = split(howto, replace, '\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, replace, casesensitive, matchword)
318 : ::replace(bv, search, replace, 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