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/textutils.h"
34 #include "support/tostr.h"
36 #include "support/std_sstream.h"
38 using lyx::support::lowercase;
39 using lyx::support::uppercase;
40 using lyx::support::split;
46 using std::ostringstream;
52 bool parse_bool(string & howto)
57 howto = split(howto, var, ' ');
62 class MatchString : public std::binary_function<Paragraph, lyx::pos_type, bool>
65 MatchString(string const & str, bool cs, bool mw)
66 : str(str), cs(cs), mw(mw)
69 // returns true if the specified string is at the specified position
70 bool operator()(Paragraph const & par, lyx::pos_type pos) const
72 string::size_type const size = str.length();
74 lyx::pos_type const parsize = par.size();
75 for (i = 0; pos + i < parsize; ++i) {
76 if (string::size_type(i) >= size)
78 if (cs && str[i] != par.getChar(pos + i))
80 if (!cs && uppercase(str[i]) != uppercase(par.getChar(pos + i)))
84 if (size != string::size_type(i))
87 // if necessary, check whether string matches word
89 if (pos > 0 && IsLetterCharOrDigit(par.getChar(pos - 1)))
91 if (pos + lyx::pos_type(size) < parsize
92 && IsLetterCharOrDigit(par.getChar(pos + size)));
104 // match whole words only
109 bool findForward(DocumentIterator & cur, MatchString const & match)
111 for (; cur.size(); cur.forwardChar())
112 if (match(cur.paragraph(), cur.pos()))
118 bool findBackwards(DocumentIterator & cur, MatchString const & match)
120 for (; cur.size(); cur.backwardChar())
121 if (match(cur.paragraph(), cur.pos()))
127 bool findChange(DocumentIterator & cur)
129 for (; cur.size(); cur.forwardChar())
130 if ((cur.paragraph().empty() || !cur.empty())
131 && cur.paragraph().lookupChange(cur.pos()) != Change::UNCHANGED)
137 bool searchAllowed(BufferView * bv, string const & str)
140 Alert::error(_("Search error"), _("Search string is empty"));
143 return bv->available();
147 bool find(BufferView * bv, string const & searchstr, bool cs, bool mw, bool fw)
149 if (!searchAllowed(bv, searchstr))
152 DocumentIterator 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->cursor());
176 MatchString const match(searchstr, cs, mw);
179 int const rsize = replacestr.size();
180 int const ssize = searchstr.size();
182 DocumentIterator cur = DocumentIterator(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->text()->init(bv);
195 bv->putSelectionAt(DocumentIterator(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 = 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);
229 lyx::cap::setSelectionRange(cur, replacestr.length());
230 cur.top() = fw ? cur.selEnd() : cur.selBegin();
231 bv->buffer()->markDirty();
232 find(bv, searchstr, cs, mw, fw);
244 string const find2string(string const & search,
245 bool casesensitive, bool matchword, bool forward)
249 << int(casesensitive) << ' '
250 << int(matchword) << ' '
256 string const replace2string(string const & search, string const & replace,
257 bool casesensitive, bool matchword,
258 bool all, bool forward)
263 << int(casesensitive) << ' '
264 << int(matchword) << ' '
271 void find(BufferView * bv, FuncRequest const & ev)
273 if (!bv || ev.action != LFUN_WORD_FIND)
276 lyxerr << "find called, cmd: " << ev << std::endl;
278 // data is of the form
280 // <casesensitive> <matchword> <forward>"
282 string howto = split(ev.argument, search, '\n');
284 bool casesensitive = parse_bool(howto);
285 bool matchword = parse_bool(howto);
286 bool forward = parse_bool(howto);
288 bool const found = ::find(bv, search,
289 casesensitive, matchword, forward);
292 bv->owner()->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(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 LyXView * lv = bv->owner();
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 lv->message(_("String not found!"));
324 if (replace_count == 1) {
325 lv->message(_("String has been replaced."));
327 string str = tostr(replace_count);
328 str += _(" strings have been replaced.");
335 bool findNextChange(BufferView * bv)
337 if (!bv->available())
340 DocumentIterator cur = DocumentIterator(bv->buffer()->inset());
342 if (!findChange(cur))
345 Paragraph const & par = cur.paragraph();
346 pos_type pos = cur.pos();
348 Change orig_change = par.lookupChangeFull(pos);
349 pos_type parsize = par.size();
352 for (; end != parsize; ++end) {
353 Change change = par.lookupChangeFull(end);
354 if (change != orig_change) {
355 // slight UI optimisation: for replacements, we get
356 // text like : _old_new. Consider that as one change.
357 if (!(orig_change.type == Change::DELETED &&
358 change.type == Change::INSERTED))
362 pos_type length = end - pos;
363 bv->putSelectionAt(cur, length, true);