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"
31 #include "frontends/LyXView.h"
33 #include "support/convert.h"
37 using lyx::support::lowercase;
38 using lyx::support::uppercase;
39 using lyx::support::split;
45 using std::ostringstream;
51 bool parse_bool(string & howto)
56 howto = split(howto, var, ' ');
61 class MatchString : public std::binary_function<Paragraph, lyx::pos_type, bool>
64 MatchString(string const & str, bool cs, bool mw)
65 : str(str), cs(cs), mw(mw)
68 // returns true if the specified string is at the specified position
69 bool operator()(Paragraph const & par, lyx::pos_type pos) const
71 string::size_type const size = str.length();
73 lyx::pos_type const parsize = par.size();
74 for (i = 0; pos + i < parsize; ++i) {
75 if (string::size_type(i) >= size)
77 if (cs && str[i] != par.getChar(pos + i))
79 if (!cs && uppercase(str[i]) != uppercase(par.getChar(pos + i)))
83 if (size != string::size_type(i))
86 // if necessary, check whether string matches word
88 if (pos > 0 && par.isLetter(pos - 1))
90 if (pos + lyx::pos_type(size) < parsize
91 && par.isLetter(pos + size))
103 // match whole words only
108 bool findForward(DocIterator & cur, MatchString const & match)
110 for (; cur; cur.forwardChar())
111 if (cur.inTexted() && match(cur.paragraph(), cur.pos()))
117 bool findBackwards(DocIterator & cur, MatchString const & match)
121 if (cur.inTexted() && match(cur.paragraph(), cur.pos()))
128 bool findChange(DocIterator & cur)
130 for (; cur; cur.forwardPos())
131 if (cur.inTexted() && cur.paragraph().lookupChange(cur.pos())
132 != Change::UNCHANGED)
138 bool searchAllowed(BufferView * bv, string const & str)
141 Alert::error(_("Search error"), _("Search string is empty"));
144 return bv->available();
148 bool find(BufferView * bv, string const & searchstr, bool cs, bool mw, bool fw)
150 if (!searchAllowed(bv, searchstr))
153 DocIterator cur = bv->cursor();
155 MatchString const match(searchstr, cs, mw);
157 bool found = fw ? findForward(cur, match) : findBackwards(cur, match);
160 bv->putSelectionAt(cur, searchstr.length(), !fw);
166 int replaceAll(BufferView * bv,
167 string const & searchstr, string const & replacestr,
170 Buffer & buf = *bv->buffer();
172 if (!searchAllowed(bv, searchstr) || buf.isReadonly())
175 recordUndoFullDocument(bv);
177 MatchString const match(searchstr, cs, mw);
180 int const rsize = replacestr.size();
181 int const ssize = searchstr.size();
183 DocIterator cur = doc_iterator_begin(buf.inset());
184 while (findForward(cur, match)) {
185 lyx::pos_type pos = cur.pos();
187 = cur.paragraph().getFontSettings(buf.params(), pos);
188 int striked = ssize - cur.paragraph().erase(pos, pos + ssize);
189 cur.paragraph().insert(pos, replacestr, font);
190 for (int i = 0; i < rsize + striked; ++i)
195 bv->text()->init(bv);
196 bv->putSelectionAt(doc_iterator_begin(buf.inset()), 0, false);
203 bool stringSelected(BufferView * bv, string const & searchstr,
204 bool cs, bool mw, bool fw)
206 // if nothing selected or selection does not equal search
207 // string search and select next occurance and return
208 string const & str1 = searchstr;
209 string const str2 = bv->cursor().selectionAsString(false);
210 if ((cs && str1 != str2) || lowercase(str1) != lowercase(str2)) {
211 find(bv, searchstr, cs, mw, fw);
219 int replace(BufferView * bv, string const & searchstr,
220 string const & replacestr, bool cs, bool mw, bool fw)
222 if (!searchAllowed(bv, searchstr) || bv->buffer()->isReadonly())
225 if (!stringSelected(bv, searchstr, cs, mw, fw))
228 LCursor & cur = bv->cursor();
229 lyx::cap::replaceSelectionWithString(cur, replacestr);
230 lyx::cap::setSelectionRange(cur, replacestr.length());
231 cur.top() = fw ? cur.selEnd() : cur.selBegin();
232 bv->buffer()->markDirty();
233 find(bv, searchstr, cs, mw, fw);
245 string const find2string(string const & search,
246 bool casesensitive, bool matchword, bool forward)
250 << int(casesensitive) << ' '
251 << int(matchword) << ' '
257 string const replace2string(string const & search, string const & replace,
258 bool casesensitive, bool matchword,
259 bool all, bool forward)
264 << int(casesensitive) << ' '
265 << int(matchword) << ' '
272 void find(BufferView * bv, FuncRequest const & ev)
274 if (!bv || ev.action != LFUN_WORD_FIND)
277 lyxerr << "find called, cmd: " << ev << std::endl;
279 // data is of the form
281 // <casesensitive> <matchword> <forward>"
283 string howto = split(ev.argument, search, '\n');
285 bool casesensitive = parse_bool(howto);
286 bool matchword = parse_bool(howto);
287 bool forward = parse_bool(howto);
289 bool const found = ::find(bv, search,
290 casesensitive, matchword, forward);
293 bv->owner()->message(_("String not found!"));
297 void replace(BufferView * bv, FuncRequest const & ev)
299 if (!bv || ev.action != LFUN_WORD_REPLACE)
302 // data is of the form
305 // <casesensitive> <matchword> <all> <forward>"
308 string howto = split(ev.argument, search, '\n');
309 howto = split(howto, replace, '\n');
311 bool casesensitive = parse_bool(howto);
312 bool matchword = parse_bool(howto);
313 bool all = parse_bool(howto);
314 bool forward = parse_bool(howto);
316 LyXView * lv = bv->owner();
318 int const replace_count = all
319 ? ::replaceAll(bv, search, replace, casesensitive, matchword)
320 : ::replace(bv, search, replace, casesensitive, matchword, forward);
322 if (replace_count == 0) {
323 lv->message(_("String not found!"));
325 if (replace_count == 1) {
326 lv->message(_("String has been replaced."));
328 string str = convert<string>(replace_count);
329 str += _(" strings have been replaced.");
336 bool findNextChange(BufferView * bv)
338 if (!bv->available())
341 DocIterator cur = bv->cursor();
343 if (!findChange(cur))
346 bv->cursor().setCursor(cur);
347 bv->cursor().resetAnchor();
349 Change orig_change = cur.paragraph().lookupChangeFull(cur.pos());
351 DocIterator et = doc_iterator_end(cur.inset());
352 for (; cur != et; cur.forwardPosNoDescend()) {
353 Change change = cur.paragraph().lookupChangeFull(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