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 "support/textutils.h"
32 using lyx::support::lowercase;
33 using lyx::support::uppercase;
34 using bv_funcs::put_selection_at;
47 MatchString(string const & str, bool cs, bool mw)
48 : 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))))) {
64 if (size != string::size_type(i))
67 // if necessary, check whether string matches word
69 if (pos > 0 && IsLetterCharOrDigit(par.getChar(pos - 1)))
71 if (pos + pos_type(size) < parsize
72 && IsLetterCharOrDigit(par.getChar(pos + size)));
84 // match whole words only
89 bool findForward(PosIterator & cur, PosIterator const & end,
90 MatchString const & match)
92 for (; cur != end; ++cur) {
93 if (match(*cur.pit(), cur.pos()))
100 bool findBackwards(PosIterator & cur, PosIterator const & beg,
101 MatchString const & match)
105 if (match(*cur.pit(), cur.pos()))
112 bool findChange(PosIterator & cur, PosIterator const & end)
114 for (; cur != end; ++cur) {
115 if ((!cur.pit()->size() || !cur.at_end())
116 && cur.pit()->lookupChange(cur.pos()) != Change::UNCHANGED)
123 bool searchAllowed(BufferView * bv, string const & str)
126 Alert::error(_("Search error"), _("Search string is empty"));
129 return bv->available();
136 bool find(BufferView * bv, string const & searchstr, bool cs, bool mw, bool fw)
138 if (!searchAllowed(bv, searchstr))
141 PosIterator cur = PosIterator(*bv);
143 MatchString const match(searchstr, cs, mw);
145 PosIterator const end = bv->buffer()->pos_iterator_end();
146 PosIterator const beg = bv->buffer()->pos_iterator_begin();
148 bool found = fw ? findForward(cur, end, match)
149 : findBackwards(cur, beg, match);
152 put_selection_at(bv, cur, searchstr.length(), !fw);
158 int replaceAll(BufferView * bv,
159 string const & searchstr, string const & replacestr,
162 Buffer & buf = *bv->buffer();
164 if (!searchAllowed(bv, searchstr) || buf.isReadonly())
167 recordUndo(Undo::ATOMIC, bv->text(), 0, buf.paragraphs().size() - 1);
169 PosIterator cur = buf.pos_iterator_begin();
170 PosIterator const end = buf.pos_iterator_end();
171 MatchString const match(searchstr, cs, mw);
174 int const rsize = replacestr.size();
175 int const ssize = searchstr.size();
177 while (findForward(cur, end, match)) {
178 pos_type pos = cur.pos();
180 = cur.pit()->getFontSettings(buf.params(), pos);
181 int striked = ssize - cur.pit()->erase(pos, pos + ssize);
182 cur.pit()->insert(pos, replacestr, font);
183 advance(cur, rsize + striked);
187 PosIterator beg = buf.pos_iterator_begin();
188 bv->text()->init(bv);
189 put_selection_at(bv, beg, 0, false);
198 bool stringSelected(BufferView * bv,
199 string const & searchstr,
200 bool cs, bool mw, bool fw)
202 LyXText * text = bv->getLyXText();
203 // if nothing selected or selection does not equal search
204 // string search and select next occurance and return
205 string const & str1 = searchstr;
206 string const str2 = text->selectionAsString(*bv->buffer(),
208 if ((cs && str1 != str2) || lowercase(str1) != lowercase(str2)) {
209 find(bv, searchstr, cs, mw, fw);
219 int replace(BufferView * bv,
220 string const & searchstr, string const & replacestr,
221 bool cs, bool mw, bool fw)
223 if (!searchAllowed(bv, searchstr) || bv->buffer()->isReadonly())
226 if (!stringSelected(bv, searchstr, cs, mw, fw))
229 LyXText * text = bv->getLyXText();
231 text->replaceSelectionWithString(replacestr);
232 text->setSelectionRange(replacestr.length());
233 text->cursor = fw ? text->selEnd() : text->selStart();
235 bv->buffer()->markDirty();
236 find(bv, searchstr, cs, mw, fw);
243 bool findNextChange(BufferView * bv)
245 if (!bv->available())
248 PosIterator cur = PosIterator(*bv);
249 PosIterator const endit = bv->buffer()->pos_iterator_end();
251 if (!findChange(cur, endit))
254 ParagraphList::iterator pit = cur.pit();
255 pos_type pos = cur.pos();
257 Change orig_change = pit->lookupChangeFull(pos);
258 pos_type parsize = pit->size();
261 for (; end != parsize; ++end) {
262 Change change = pit->lookupChangeFull(end);
263 if (change != orig_change) {
264 // slight UI optimisation: for replacements, we get
265 // text like : _old_new. Consider that as one change.
266 if (!(orig_change.type == Change::DELETED &&
267 change.type == Change::INSERTED))
271 pos_type length = end - pos;
272 put_selection_at(bv, cur, length, true);