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)))) {
83 bool findForward(PosIterator & cur, PosIterator const & end,
86 for (; cur != end && !match(*cur.pit(), cur.pos()); ++cur)
93 bool findBackwards(PosIterator & cur, PosIterator const & beg,
100 if (match(*cur.pit(), cur.pos()))
102 } while (cur != beg);
104 return match(*cur.pit(), cur.pos());
108 bool findChange(PosIterator & cur, PosIterator const & end)
110 for (; cur != end; ++cur) {
111 if ((!cur.pit()->size() || !cur.at_end())
112 && cur.pit()->lookupChange(cur.pos()) != Change::UNCHANGED)
120 bool searchAllowed(BufferView * bv, string const & str)
123 Alert::error(_("Search error"), _("Search string is empty"));
126 return bv->available();
133 bool find(BufferView * bv, string const & searchstr,
134 bool cs, bool mw, bool fw)
136 if (!searchAllowed(bv, searchstr))
139 PosIterator cur = PosIterator(*bv);
141 MatchString match(searchstr, cs, mw);
146 PosIterator const end = bv->buffer()->pos_iterator_end();
147 found = findForward(cur, end, match);
149 PosIterator const beg = bv->buffer()->pos_iterator_begin();
150 found = findBackwards(cur, beg, match);
154 put_selection_at(bv, cur, searchstr.length(), !fw);
160 int replaceAll(BufferView * bv,
161 string const & searchstr, string const & replacestr,
164 Buffer & buf = *bv->buffer();
166 if (!searchAllowed(bv, searchstr) || buf.isReadonly())
169 recordUndo(Undo::ATOMIC, bv->text, 0,
170 buf.paragraphs().size() - 1);
172 PosIterator cur = buf.pos_iterator_begin();
173 PosIterator const end = buf.pos_iterator_end();
174 MatchString match(searchstr, cs, mw);
177 int const rsize = replacestr.size();
178 int const ssize = searchstr.size();
179 while (findForward(cur, end, match)) {
180 pos_type pos = cur.pos();
182 = cur.pit()->getFontSettings(buf.params(), pos);
183 int striked = ssize - cur.pit()->erase(pos, pos + ssize);
184 cur.pit()->insert(pos, replacestr, font);
185 advance(cur, rsize + striked);
188 PosIterator beg = buf.pos_iterator_begin();
190 put_selection_at(bv, beg, 0, false);
197 int replace(BufferView * bv,
198 string const & searchstr, string const & replacestr,
199 bool cs, bool mw, bool fw)
201 if (!searchAllowed(bv, searchstr) || bv->buffer()->isReadonly())
205 LyXText * text = bv->getLyXText();
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 = text->selectionAsString(*bv->buffer(),
211 if ((cs && str1 != str2)
212 || lowercase(str1) != lowercase(str2)) {
213 find(bv, searchstr, cs, mw, fw);
219 LyXText * text = bv->getLyXText();
220 // We have to do this check only because mathed insets don't
221 // return their own LyXText but the LyXText of it's parent!
222 if (!bv->innerInset() ||
223 ((text != bv->text) &&
224 (text->inset_owner == text->inset_owner->getLockingInset()))) {
225 text->replaceSelectionWithString(replacestr);
226 text->setSelectionRange(replacestr.length());
227 text->cursor = fw ? text->selection.end
228 : text->selection.start;
232 bv->buffer()->markDirty();
233 find(bv, searchstr, cs, mw, fw);
240 bool findNextChange(BufferView * bv)
242 if (!bv->available())
245 PosIterator cur = PosIterator(*bv);
246 PosIterator const endit = bv->buffer()->pos_iterator_end();
248 if (!findChange(cur, endit))
252 ParagraphList::iterator pit = cur.pit();
253 pos_type pos = cur.pos();
255 Change orig_change = pit->lookupChangeFull(pos);
256 pos_type parsize = pit->size();
259 for (; end != parsize; ++end) {
260 Change change = pit->lookupChangeFull(end);
261 if (change != orig_change) {
262 // slight UI optimisation: for replacements, we get
263 // text like : _old_new. Consider that as one change.
264 if (!(orig_change.type == Change::DELETED &&
265 change.type == Change::INSERTED))
269 pos_type length = end - pos;
271 put_selection_at(bv, cur, length, true);