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 "BufferView.h"
21 #include "iterators.h"
24 #include "paragraph.h"
25 #include "PosIterator.h"
28 #include "frontends/Alert.h"
30 #include "insets/insettext.h"
32 #include "support/textutils.h"
34 using lyx::support::lowercase;
35 using lyx::support::uppercase;
36 using bv_funcs::put_selection_at;
49 MatchString(string const & str, bool cs, bool mw)
50 : str(str), cs(cs), mw(mw) {};
51 // returns true if the specified string is at the specified position
52 bool operator()(Paragraph const & par, pos_type pos) const
54 string::size_type size = str.length();
56 pos_type parsize = par.size();
57 while ((pos + i < parsize)
58 && (string::size_type(i) < size)
59 && (cs ? (str[i] == par.getChar(pos + i))
60 : (uppercase(str[i]) == uppercase(par.getChar(pos + i))))) {
63 if (size == string::size_type(i)) {
64 // if necessary, check whether string matches word
67 if ((pos <= 0 || !IsLetterCharOrDigit(par.getChar(pos - 1)))
68 && (pos + pos_type(size) >= parsize
69 || !IsLetterCharOrDigit(par.getChar(pos + size)))) {
95 bool findForward(PosIterator & cur, PosIterator const & end,
98 for (; cur != end && !match(*cur.pit(), cur.pos()); ++cur)
105 bool findBackwards(PosIterator & cur, PosIterator const & beg,
112 if (match(*cur.pit(), cur.pos()))
114 } while (cur != beg);
116 return match(*cur.pit(), cur.pos());
120 bool findChange(PosIterator & cur, PosIterator const & end)
122 for (; cur != end; ++cur) {
123 if ((!cur.pit()->size() || !cur.at_end())
124 && cur.pit()->lookupChange(cur.pos()) != Change::UNCHANGED)
132 bool searchAllowed(BufferView * bv, string const & str)
135 Alert::error(_("Search error"), _("Search string is empty"));
138 if (!bv->available())
147 bool find(BufferView * bv, string const & searchstr,
148 bool cs, bool mw, bool fw)
150 if (!searchAllowed(bv, searchstr))
153 PosIterator cur = PosIterator(*bv);
155 MatchString match(searchstr, cs, mw);
160 PosIterator const end = bv->buffer()->pos_iterator_end();
161 found = findForward(cur, end, match);
163 PosIterator const beg = bv->buffer()->pos_iterator_begin();
164 found = findBackwards(cur, beg, match);
168 put_selection_at(bv, cur, searchstr.length(), !fw);
181 int replaceAll(BufferView * bv,
182 string const & searchstr, string const & replacestr,
185 Buffer & buf = *bv->buffer();
187 if (!searchAllowed(bv, searchstr) || buf.isReadonly())
190 recordUndo(Undo::ATOMIC, bv->text, 0,
191 buf.paragraphs().size() - 1);
193 PosIterator cur = buf.pos_iterator_begin();
194 PosIterator const end = buf.pos_iterator_end();
195 MatchString match(searchstr, cs, mw);
198 int const rsize = replacestr.size();
199 int const ssize = searchstr.size();
200 while (findForward(cur, end, match)) {
201 pos_type pos = cur.pos();
203 = cur.pit()->getFontSettings(buf.params(), pos);
204 int striked = ssize - cur.pit()->erase(pos, pos + ssize);
205 cur.pit()->insert(pos, replacestr, font);
206 advance(cur, rsize + striked);
209 PosIterator beg = buf.pos_iterator_begin();
211 put_selection_at(bv, beg, 0, false);
218 int replace(BufferView * bv,
219 string const & searchstr, string const & replacestr,
220 bool cs, bool mw, bool fw)
222 if (!searchAllowed(bv, searchstr) || bv->buffer()->isReadonly())
226 LyXText * text = bv->getLyXText();
227 // if nothing selected or selection does not equal search
228 // string search and select next occurance and return
229 string const str1 = searchstr;
230 string const str2 = text->selectionAsString(*bv->buffer(),
232 if ((cs && str1 != str2)
233 || lowercase(str1) != lowercase(str2)) {
234 find(bv, searchstr, cs, mw, fw);
239 LyXText * text = bv->getLyXText();
240 // We have to do this check only because mathed insets don't
241 // return their own LyXText but the LyXText of it's parent!
242 if (!bv->theLockingInset() ||
243 ((text != bv->text) &&
244 (text->inset_owner == text->inset_owner->getLockingInset()))) {
245 text->replaceSelectionWithString(replacestr);
246 text->setSelectionRange(replacestr.length());
247 text->cursor = fw ? text->selection.end
248 : text->selection.start;
251 // FIXME: should be called via an LFUN
252 bv->buffer()->markDirty();
254 find(bv, searchstr, cs, mw, fw);
261 bool findNextChange(BufferView * bv)
263 if (!bv->available())
266 PosIterator cur = PosIterator(*bv);
267 PosIterator const endit = bv->buffer()->pos_iterator_end();
269 if (!findChange(cur, endit))
273 ParagraphList::iterator pit = cur.pit();
274 pos_type pos = cur.pos();
276 Change orig_change = pit->lookupChangeFull(pos);
277 pos_type parsize = pit->size();
280 for (; end != parsize; ++end) {
281 Change change = pit->lookupChangeFull(end);
282 if (change != orig_change) {
283 // slight UI optimisation: for replacements, we get
284 // text like : _old_new. Consider that as one change.
285 if (!(orig_change.type == Change::DELETED &&
286 change.type == Change::INSERTED))
290 pos_type length = end - pos;
292 put_selection_at(bv, cur, length, true);