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 const 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,
84 MatchString const & match)
86 for (; cur != end; ++cur) {
87 if (match(*cur.pit(), cur.pos()))
94 bool findBackwards(PosIterator & cur, PosIterator const & beg,
95 MatchString const & match)
99 if (match(*cur.pit(), cur.pos()))
106 bool findChange(PosIterator & cur, PosIterator const & end)
108 for (; cur != end; ++cur) {
109 if ((!cur.pit()->size() || !cur.at_end())
110 && cur.pit()->lookupChange(cur.pos()) != Change::UNCHANGED)
117 bool searchAllowed(BufferView * bv, string const & str)
120 Alert::error(_("Search error"), _("Search string is empty"));
123 return bv->available();
130 bool find(BufferView * bv, string const & searchstr,
131 bool cs, bool mw, bool fw)
133 if (!searchAllowed(bv, searchstr))
136 PosIterator cur = PosIterator(*bv);
138 MatchString const match(searchstr, cs, mw);
140 PosIterator const end = bv->buffer()->pos_iterator_end();
141 PosIterator const beg = bv->buffer()->pos_iterator_begin();
143 bool found = fw ? findForward(cur, end, match)
144 : findBackwards(cur, beg, match);
147 put_selection_at(bv, cur, searchstr.length(), !fw);
153 int replaceAll(BufferView * bv,
154 string const & searchstr, string const & replacestr,
157 Buffer & buf = *bv->buffer();
159 if (!searchAllowed(bv, searchstr) || buf.isReadonly())
162 recordUndo(Undo::ATOMIC, bv->text, 0,
163 buf.paragraphs().size() - 1);
165 PosIterator cur = buf.pos_iterator_begin();
166 PosIterator const end = buf.pos_iterator_end();
167 MatchString const match(searchstr, cs, mw);
170 int const rsize = replacestr.size();
171 int const ssize = searchstr.size();
173 while (findForward(cur, end, match)) {
174 pos_type pos = cur.pos();
176 = cur.pit()->getFontSettings(buf.params(), pos);
177 int striked = ssize - cur.pit()->erase(pos, pos + ssize);
178 cur.pit()->insert(pos, replacestr, font);
179 advance(cur, rsize + striked);
183 PosIterator beg = buf.pos_iterator_begin();
185 put_selection_at(bv, beg, 0, false);
194 bool stringSelected(BufferView * bv,
195 string const & searchstr,
196 bool cs, bool mw, bool fw)
198 LyXText * text = bv->getLyXText();
199 // if nothing selected or selection does not equal search
200 // string search and select next occurance and return
201 string const & str1 = searchstr;
202 string const str2 = text->selectionAsString(*bv->buffer(),
204 if ((cs && str1 != str2)
205 || lowercase(str1) != lowercase(str2)) {
206 find(bv, searchstr, cs, mw, fw);
216 int replace(BufferView * bv,
217 string const & searchstr, string const & replacestr,
218 bool cs, bool mw, bool fw)
220 if (!searchAllowed(bv, searchstr) || bv->buffer()->isReadonly())
223 if (!stringSelected(bv, searchstr, cs, mw, fw))
226 LyXText * text = bv->getLyXText();
228 text->replaceSelectionWithString(replacestr);
229 text->setSelectionRange(replacestr.length());
230 text->cursor = fw ? text->selection.end : 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))
251 ParagraphList::iterator pit = cur.pit();
252 pos_type pos = cur.pos();
254 Change orig_change = pit->lookupChangeFull(pos);
255 pos_type parsize = pit->size();
258 for (; end != parsize; ++end) {
259 Change change = pit->lookupChangeFull(end);
260 if (change != orig_change) {
261 // slight UI optimisation: for replacements, we get
262 // text like : _old_new. Consider that as one change.
263 if (!(orig_change.type == Change::DELETED &&
264 change.type == Change::INSERTED))
268 pos_type length = end - pos;
270 put_selection_at(bv, cur, length, true);