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 for (int i = 0; i < rsize + striked; ++i)
195 bv->buffer()->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 = lyx::to_utf8(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, fw);
230 bv->buffer()->markDirty();
231 find(bv, searchstr, cs, mw, fw);
243 string const find2string(string const & search,
244 bool casesensitive, bool matchword, bool forward)
248 << int(casesensitive) << ' '
249 << int(matchword) << ' '
255 string const replace2string(string const & search, string const & replace,
256 bool casesensitive, bool matchword,
257 bool all, bool forward)
262 << int(casesensitive) << ' '
263 << int(matchword) << ' '
270 void find(BufferView * bv, FuncRequest const & ev)
272 if (!bv || ev.action != LFUN_WORD_FIND)
275 //lyxerr << "find called, cmd: " << ev << std::endl;
277 // data is of the form
279 // <casesensitive> <matchword> <forward>"
281 string howto = split(lyx::to_utf8(ev.argument()), search, '\n');
283 bool casesensitive = parse_bool(howto);
284 bool matchword = parse_bool(howto);
285 bool forward = parse_bool(howto);
287 bool const found = ::find(bv, search,
288 casesensitive, matchword, forward);
291 // emit message signal.
292 bv->message(_("String not found!"));
296 void replace(BufferView * bv, FuncRequest const & ev)
298 if (!bv || ev.action != LFUN_WORD_REPLACE)
301 // data is of the form
304 // <casesensitive> <matchword> <all> <forward>"
307 string howto = split(lyx::to_utf8(ev.argument()), search, '\n');
308 howto = split(howto, replace, '\n');
310 bool casesensitive = parse_bool(howto);
311 bool matchword = parse_bool(howto);
312 bool all = parse_bool(howto);
313 bool forward = parse_bool(howto);
315 Buffer * buf = bv->buffer();
317 int const replace_count = all
318 ? ::replaceAll(bv, search, replace, casesensitive, matchword)
319 : ::replace(bv, search, replace, casesensitive, matchword, forward);
321 if (replace_count == 0) {
322 // emit message signal.
323 buf->message(_("String not found!"));
325 if (replace_count == 1) {
326 // emit message signal.
327 buf->message(_("String has been replaced."));
329 docstring str = convert<docstring>(replace_count);
330 str += _(" strings have been replaced.");
331 // emit message signal.
338 bool findNextChange(BufferView * bv)
343 DocIterator cur = bv->cursor();
345 if (!findChange(cur))
348 bv->cursor().setCursor(cur);
349 bv->cursor().resetAnchor();
351 Change orig_change = cur.paragraph().lookupChange(cur.pos());
353 DocIterator et = doc_iterator_end(cur.inset());
354 for (; cur != et; cur.forwardPosNoDescend()) {
355 Change change = cur.paragraph().lookupChange(cur.pos());
356 if (change != orig_change) {
360 // Now put cursor to end of selection:
361 bv->cursor().setCursor(cur);
362 bv->cursor().setSelection();
363 // if we used a lfun like in find/replace, dispatch would do