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 lyx::frontend::Alert::error(_("Search error"),
141 _("Search string is empty"));
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 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 = lyx::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 lyx::cap::replaceSelectionWithString(cur, replacestr, fw);
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(lyx::to_utf8(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 // emit message signal.
294 bv->message(_("String not found!"));
298 void replace(BufferView * bv, FuncRequest const & ev)
300 if (!bv || ev.action != LFUN_WORD_REPLACE)
303 // data is of the form
306 // <casesensitive> <matchword> <all> <forward>"
309 string howto = split(lyx::to_utf8(ev.argument()), search, '\n');
310 howto = split(howto, replace, '\n');
312 bool casesensitive = parse_bool(howto);
313 bool matchword = parse_bool(howto);
314 bool all = parse_bool(howto);
315 bool forward = parse_bool(howto);
317 Buffer * buf = bv->buffer();
319 int const replace_count = all
320 ? ::replaceAll(bv, search, replace, casesensitive, matchword)
321 : ::replace(bv, search, replace, casesensitive, matchword, forward);
323 if (replace_count == 0) {
324 // emit message signal.
325 buf->message(_("String not found!"));
327 if (replace_count == 1) {
328 // emit message signal.
329 buf->message(_("String has been replaced."));
331 docstring str = convert<docstring>(replace_count);
332 str += _(" strings have been replaced.");
333 // emit message signal.
340 bool findNextChange(BufferView * bv)
345 DocIterator cur = bv->cursor();
347 if (!findChange(cur))
350 bv->cursor().setCursor(cur);
351 bv->cursor().resetAnchor();
353 Change orig_change = cur.paragraph().lookupChange(cur.pos());
355 DocIterator et = doc_iterator_end(cur.inset());
356 for (; cur != et; cur.forwardPosNoDescend()) {
357 Change change = cur.paragraph().lookupChange(cur.pos());
358 if (change != orig_change) {
362 // Now put cursor to end of selection:
363 bv->cursor().setCursor(cur);
364 bv->cursor().setSelection();
365 // if we used a lfun like in find/replace, dispatch would do