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, buf.paragraphs().size() - 1);
164 PosIterator cur = buf.pos_iterator_begin();
165 PosIterator const end = buf.pos_iterator_end();
166 MatchString const match(searchstr, cs, mw);
169 int const rsize = replacestr.size();
170 int const ssize = searchstr.size();
172 while (findForward(cur, end, match)) {
173 pos_type pos = cur.pos();
175 = cur.pit()->getFontSettings(buf.params(), pos);
176 int striked = ssize - cur.pit()->erase(pos, pos + ssize);
177 cur.pit()->insert(pos, replacestr, font);
178 advance(cur, rsize + striked);
182 PosIterator beg = buf.pos_iterator_begin();
183 bv->text()->init(bv);
184 put_selection_at(bv, beg, 0, false);
193 bool stringSelected(BufferView * bv,
194 string const & searchstr,
195 bool cs, bool mw, bool fw)
197 LyXText * text = bv->getLyXText();
198 // if nothing selected or selection does not equal search
199 // string search and select next occurance and return
200 string const & str1 = searchstr;
201 string const str2 = text->selectionAsString(*bv->buffer(),
203 if ((cs && str1 != str2) || lowercase(str1) != lowercase(str2)) {
204 find(bv, searchstr, cs, mw, fw);
214 int replace(BufferView * bv,
215 string const & searchstr, string const & replacestr,
216 bool cs, bool mw, bool fw)
218 if (!searchAllowed(bv, searchstr) || bv->buffer()->isReadonly())
221 if (!stringSelected(bv, searchstr, cs, mw, fw))
224 LyXText * text = bv->getLyXText();
226 text->replaceSelectionWithString(replacestr);
227 text->setSelectionRange(replacestr.length());
228 text->cursor = fw ? text->selection.end : text->selection.start;
230 bv->buffer()->markDirty();
231 find(bv, searchstr, cs, mw, fw);
238 bool findNextChange(BufferView * bv)
240 if (!bv->available())
243 PosIterator cur = PosIterator(*bv);
244 PosIterator const endit = bv->buffer()->pos_iterator_end();
246 if (!findChange(cur, endit))
249 ParagraphList::iterator pit = cur.pit();
250 pos_type pos = cur.pos();
252 Change orig_change = pit->lookupChangeFull(pos);
253 pos_type parsize = pit->size();
256 for (; end != parsize; ++end) {
257 Change change = pit->lookupChangeFull(end);
258 if (change != orig_change) {
259 // slight UI optimisation: for replacements, we get
260 // text like : _old_new. Consider that as one change.
261 if (!(orig_change.type == Change::DELETED &&
262 change.type == Change::INSERTED))
266 pos_type length = end - pos;
267 put_selection_at(bv, cur, length, true);