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.
19 #include "cursor_slice.h"
20 #include "BufferView.h"
22 #include "iterators.h"
23 #include "funcrequest.h"
26 #include "paragraph.h"
27 #include "PosIterator.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;
42 using std::ostringstream;
48 bool parse_bool(string & howto)
53 howto = split(howto, var, ' ');
58 bool find(BufferView * bv,
59 string const & searchstr, bool cs, bool mw, bool fw);
62 int replace(BufferView * bv,
63 string const & searchstr, string const & replacestr,
64 bool cs, bool mw, bool fw);
67 int replaceAll(BufferView * bv,
68 string const & searchstr, string const & replacestr,
72 bool findChange(PosIterator & cur, PosIterator const & end);
80 string const find2string(string const & search,
81 bool casesensitive, bool matchword, bool forward)
85 << int(casesensitive) << ' '
86 << int(matchword) << ' '
93 string const replace2string(string const & search, string const & replace,
94 bool casesensitive, bool matchword,
95 bool all, bool forward)
100 << int(casesensitive) << ' '
101 << int(matchword) << ' '
109 void find(FuncRequest const & ev)
111 if (!ev.view() || ev.action != LFUN_WORD_FIND)
114 // data is of the form
116 // <casesensitive> <matchword> <forward>"
118 string howto = split(ev.argument, search, '\n');
120 bool casesensitive = parse_bool(howto);
121 bool matchword = parse_bool(howto);
122 bool forward = parse_bool(howto);
124 BufferView * bv = ev.view();
125 bool const found = ::find(bv, search,
126 forward, casesensitive, matchword);
129 bv->owner()->message(_("String not found!"));
133 void replace(FuncRequest const & ev)
135 if (!ev.view() || ev.action != LFUN_WORD_REPLACE)
138 // data is of the form
141 // <casesensitive> <matchword> <all> <forward>"
144 string howto = split(ev.argument, search, '\n');
145 howto = split(howto, replace, '\n');
147 bool casesensitive = parse_bool(howto);
148 bool matchword = parse_bool(howto);
149 bool all = parse_bool(howto);
150 bool forward = parse_bool(howto);
152 BufferView * bv = ev.view();
153 LyXView * lv = bv->owner();
155 int const replace_count = all ?
156 ::replaceAll(bv, search, replace,
157 casesensitive, matchword) :
158 ::replace(bv, search, replace,
159 casesensitive, matchword, forward);
161 if (replace_count == 0) {
162 lv->message(_("String not found!"));
164 if (replace_count == 1) {
165 lv->message(_("String has been replaced."));
167 string str = tostr(replace_count);
168 str += _(" strings have been replaced.");
175 bool findNextChange(BufferView * bv)
177 if (!bv->available())
180 PosIterator cur = PosIterator(*bv);
181 PosIterator const endit = bv->buffer()->pos_iterator_end();
183 if (!findChange(cur, endit))
186 ParagraphList::iterator pit = cur.pit();
187 pos_type pos = cur.pos();
189 Change orig_change = pit->lookupChangeFull(pos);
190 pos_type parsize = pit->size();
193 for (; end != parsize; ++end) {
194 Change change = pit->lookupChangeFull(end);
195 if (change != orig_change) {
196 // slight UI optimisation: for replacements, we get
197 // text like : _old_new. Consider that as one change.
198 if (!(orig_change.type == Change::DELETED &&
199 change.type == Change::INSERTED))
203 pos_type length = end - pos;
204 bv->putSelectionAt(cur, length, true);
217 MatchString(string const & str, bool cs, bool mw)
218 : str(str), cs(cs), mw(mw)
221 // returns true if the specified string is at the specified position
222 bool operator()(Paragraph const & par, lyx::pos_type pos) const
224 string::size_type const size = str.length();
226 lyx::pos_type const parsize = par.size();
227 while ((pos + i < parsize)
228 && (string::size_type(i) < size)
229 && (cs ? (str[i] == par.getChar(pos + i))
230 : (uppercase(str[i]) == uppercase(par.getChar(pos + i))))) {
234 if (size != string::size_type(i))
237 // if necessary, check whether string matches word
239 if (pos > 0 && IsLetterCharOrDigit(par.getChar(pos - 1)))
241 if (pos + lyx::pos_type(size) < parsize
242 && IsLetterCharOrDigit(par.getChar(pos + size)));
254 // match whole words only
259 bool findForward(PosIterator & cur, PosIterator const & end,
260 MatchString const & match)
262 for (; cur != end; ++cur) {
263 if (match(*cur.pit(), cur.pos()))
270 bool findBackwards(PosIterator & cur, PosIterator const & beg,
271 MatchString const & match)
275 if (match(*cur.pit(), cur.pos()))
282 bool findChange(PosIterator & cur, PosIterator const & end)
284 for (; cur != end; ++cur) {
285 if ((!cur.pit()->size() || !cur.at_end())
286 && cur.pit()->lookupChange(cur.pos()) != Change::UNCHANGED)
293 bool searchAllowed(BufferView * bv, string const & str)
296 Alert::error(_("Search error"), _("Search string is empty"));
299 return bv->available();
303 bool find(BufferView * bv, string const & searchstr, bool cs, bool mw, bool fw)
305 if (!searchAllowed(bv, searchstr))
308 PosIterator cur = PosIterator(*bv);
310 MatchString const match(searchstr, cs, mw);
312 PosIterator const end = bv->buffer()->pos_iterator_end();
313 PosIterator const beg = bv->buffer()->pos_iterator_begin();
315 bool found = fw ? findForward(cur, end, match)
316 : findBackwards(cur, beg, match);
319 bv->putSelectionAt(cur, searchstr.length(), !fw);
325 int replaceAll(BufferView * bv,
326 string const & searchstr, string const & replacestr,
329 Buffer & buf = *bv->buffer();
331 if (!searchAllowed(bv, searchstr) || buf.isReadonly())
334 recordUndo(Undo::ATOMIC, bv->text(), 0, buf.paragraphs().size() - 1);
336 PosIterator cur = buf.pos_iterator_begin();
337 PosIterator const end = buf.pos_iterator_end();
338 MatchString const match(searchstr, cs, mw);
341 int const rsize = replacestr.size();
342 int const ssize = searchstr.size();
344 while (findForward(cur, end, match)) {
345 lyx::pos_type pos = cur.pos();
347 = cur.pit()->getFontSettings(buf.params(), pos);
348 int striked = ssize - cur.pit()->erase(pos, pos + ssize);
349 cur.pit()->insert(pos, replacestr, font);
350 advance(cur, rsize + striked);
354 PosIterator beg = buf.pos_iterator_begin();
355 bv->text()->init(bv);
356 bv->putSelectionAt(beg, 0, false);
363 bool stringSelected(BufferView * bv,
364 string const & searchstr,
365 bool cs, bool mw, bool fw)
367 LyXText * text = bv->getLyXText();
368 // if nothing selected or selection does not equal search
369 // string search and select next occurance and return
370 string const & str1 = searchstr;
371 string const str2 = text->selectionAsString(*bv->buffer(),
373 if ((cs && str1 != str2) || lowercase(str1) != lowercase(str2)) {
374 find(bv, searchstr, cs, mw, fw);
382 int replace(BufferView * bv,
383 string const & searchstr, string const & replacestr,
384 bool cs, bool mw, bool fw)
386 if (!searchAllowed(bv, searchstr) || bv->buffer()->isReadonly())
389 if (!stringSelected(bv, searchstr, cs, mw, fw))
392 LyXText * text = bv->getLyXText();
394 text->replaceSelectionWithString(replacestr);
395 text->setSelectionRange(replacestr.length());
396 bv->cursor() = fw ? bv->selEnd() : bv->selStart();
397 bv->buffer()->markDirty();
398 find(bv, searchstr, cs, mw, fw);