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
10 * Full author contact details are available in file CREDITS.
18 #include "BufferView.h"
22 #include "frontends/Alert.h"
24 #include "insets/insettext.h"
26 #include "support/textutils.h"
28 using lyx::support::lowercase;
29 using lyx::support::uppercase;
37 // returns true if the specified string is at the specified position
38 bool isStringInText(Paragraph const & par, pos_type pos,
39 string const & str, bool const & cs,
42 string::size_type size = str.length();
44 pos_type parsize = par.size();
45 while (((pos + i) < parsize)
46 && (string::size_type(i) < size)
47 && (cs ? (str[i] == par.getChar(pos + i))
48 : (uppercase(str[i]) == uppercase(par.getChar(pos + i))))) {
52 if (size == string::size_type(i)) {
53 // if necessary, check whether string matches word
56 if ((pos <= 0 || !IsLetterCharOrDigit(par.getChar(pos - 1)))
57 && (pos + pos_type(size) >= parsize
58 || !IsLetterCharOrDigit(par.getChar(pos + size)))) {
66 // if the string can be found: return true and set the cursor to
67 // the new position, cs = casesensitive, mw = matchword
68 SearchResult searchForward(BufferView * bv, LyXText * text, string const & str,
69 bool const & cs, bool const & mw)
71 ParagraphList::iterator pit = text->cursor.par();
72 ParagraphList::iterator pend = text->ownerParagraphs().end();
73 pos_type pos = text->cursor.pos();
74 UpdatableInset * inset;
76 while (pit != pend && !isStringInText(*pit, pos, str, cs, mw)) {
79 && (inset = (UpdatableInset *)pit->getInset(pos))
80 && inset->isTextInset()
81 && inset->searchForward(bv, str, cs, mw))
82 return SR_FOUND_NOUPDATE;
84 if (++pos >= pit->size()) {
91 text->setCursor(pit, pos);
99 // if the string can be found: return true and set the cursor to
100 // the new position, cs = casesensitive, mw = matchword
101 SearchResult searchBackward(BufferView * bv, LyXText * text,
103 bool const & cs, bool const & mw)
105 ParagraphList::iterator pit = text->cursor.par();
106 ParagraphList::iterator pbegin = text->ownerParagraphs().begin();
107 pos_type pos = text->cursor.pos();
109 // skip past a match at the current cursor pos
112 } else if (pit != pbegin) {
120 if (pos < pit->size()) {
121 if (pit->isInset(pos) && pit->getInset(pos)->isTextInset()) {
122 UpdatableInset * inset = (UpdatableInset *)pit->getInset(pos);
123 if (inset->searchBackward(bv, str, cs, mw))
124 return SR_FOUND_NOUPDATE;
127 if (isStringInText(*pit, pos, str, cs, mw)) {
128 text->setCursor(pit, pos);
133 if (pos == 0 && pit == pbegin)
138 } else if (pit != pbegin) {
150 int replace(BufferView * bv,
151 string const & searchstr, string const & replacestr,
152 bool forward, bool casesens, bool matchwrd, bool replaceall,
155 if (!bv->available() || bv->buffer()->isReadonly())
158 // CutSelection cannot cut a single space, so we have to stop
159 // in order to avoid endless loop :-(
160 if (searchstr.length() == 0
161 || (searchstr.length() == 1 && searchstr[0] == ' ')) {
163 #warning BLECH. If we have an LFUN for replace, we can sort of fix this bogosity
165 Alert::error(_("Cannot replace"),
166 _("You cannot replace a single space or "
167 "an empty character."));
171 // now we can start searching for the first
172 // start at top if replaceall
173 LyXText * text = bv->getLyXText();
176 text->clearSelection();
177 bv->unlockInset(bv->theLockingInset());
180 // override search direction because we search top to bottom
184 // if nothing selected or selection does not equal search string
185 // search and select next occurance and return if no replaceall
190 str2 = text->selectionAsString(*bv->buffer(), false);
192 str1 = lowercase(searchstr);
193 str2 = lowercase(text->selectionAsString(*bv->buffer(), false));
196 if (!find(bv, searchstr, fw, casesens, matchwrd) ||
203 int replace_count = 0;
205 text = bv->getLyXText();
206 // We have to do this check only because mathed insets don't
207 // return their own LyXText but the LyXText of it's parent!
208 if (!bv->theLockingInset() ||
209 ((text != bv->text) &&
210 (text->inset_owner == text->inset_owner->getLockingInset()))) {
211 text->replaceSelectionWithString(replacestr);
212 text->setSelectionRange(replacestr.length());
216 found = find(bv, searchstr, fw, casesens, matchwrd);
217 } while (!once && replaceall && found);
219 // FIXME: should be called via an LFUN
220 bv->buffer()->markDirty();
223 return replace_count;
227 bool find(BufferView * bv,
228 string const & searchstr, bool forward,
229 bool casesens, bool matchwrd)
231 if (!bv->available() || searchstr.empty())
234 if (bv->theLockingInset()) {
235 bool found = forward ?
236 bv->theLockingInset()->searchForward(bv, searchstr, casesens, matchwrd) :
237 bv->theLockingInset()->searchBackward(bv, searchstr, casesens, matchwrd);
238 // We found the stuff inside the inset so we don't have to
239 // do anything as the inset did all the update for us!
242 // We now are in the main text but if we did a forward
243 // search we have to put the cursor behind the inset.
245 bv->text->cursorRight(true);
248 // If we arrive here we are in the main text again so we
249 // just start searching from the root LyXText at the position
251 LyXText * text = bv->text;
254 if (text->selection.set())
255 text->cursor = forward ?
256 text->selection.end : text->selection.start;
258 text->clearSelection();
260 SearchResult result = forward ?
261 searchForward(bv, text, searchstr, casesens, matchwrd) :
262 searchBackward(bv, text, searchstr, casesens, matchwrd);
265 // If we found the cursor inside an inset we will get back
266 // SR_FOUND_NOUPDATE and we don't have to do anything as the
267 // inset did it already.
268 if (result == SR_FOUND) {
269 bv->unlockInset(bv->theLockingInset());
270 text->setSelectionRange(searchstr.length());
271 } else if (result == SR_NOT_FOUND) {
272 bv->unlockInset(bv->theLockingInset());
281 SearchResult find(BufferView * bv, LyXText * text,
282 string const & searchstr, bool forward,
283 bool casesens, bool matchwrd)
285 if (text->selection.set())
286 text->cursor = forward ?
287 text->selection.end : text->selection.start;
289 text->clearSelection();
291 SearchResult result = forward ?
292 searchForward(bv, text, searchstr, casesens, matchwrd) :
293 searchBackward(bv, text, searchstr, casesens, matchwrd);
301 SearchResult nextChange(BufferView * bv, LyXText * text, pos_type & length)
303 ParagraphList::iterator pit = text->cursor.par();
304 ParagraphList::iterator pend = text->ownerParagraphs().end();
305 pos_type pos = text->cursor.pos();
307 while (pit != pend) {
308 pos_type parsize = pit->size();
311 if ((!parsize || pos != parsize)
312 && pit->lookupChange(pos) != Change::UNCHANGED)
315 if (pit->isInset(pos) && pit->getInset(pos)->isTextInset()) {
316 UpdatableInset * inset = (UpdatableInset *)pit->getInset(pos);
317 if (inset->nextChange(bv, length))
318 return SR_FOUND_NOUPDATE;
324 if (pos >= parsize) {
333 text->setCursor(pit, pos);
334 Change orig_change = pit->lookupChangeFull(pos);
335 pos_type parsize = pit->size();
338 for (; end != parsize; ++end) {
339 Change change = pit->lookupChangeFull(end);
340 if (change != orig_change) {
341 // slight UI optimisation: for replacements, we get
342 // text like : _old_new. Consider that as one change.
343 if (!(orig_change.type == Change::DELETED &&
344 change.type == Change::INSERTED))
353 SearchResult findNextChange(BufferView * bv, LyXText * text, pos_type & length)
355 if (text->selection.set())
356 text->cursor = text->selection.end;
358 text->clearSelection();
360 return nextChange(bv, text, length);
364 bool findNextChange(BufferView * bv)
366 if (!bv->available())
371 if (bv->theLockingInset()) {
372 bool found = bv->theLockingInset()->nextChange(bv, length);
374 // We found the stuff inside the inset so we don't have to
375 // do anything as the inset did all the update for us!
379 // We now are in the main text but if we did a forward
380 // search we have to put the cursor behind the inset.
381 bv->text->cursorRight(true);
383 // If we arrive here we are in the main text again so we
384 // just start searching from the root LyXText at the position
386 LyXText * text = bv->text;
388 if (text->selection.set())
389 text->cursor = text->selection.end;
391 text->clearSelection();
393 SearchResult result = nextChange(bv, text, length);
397 // If we found the cursor inside an inset we will get back
398 // SR_FOUND_NOUPDATE and we don't have to do anything as the
399 // inset did it already.
400 if (result == SR_FOUND) {
401 bv->unlockInset(bv->theLockingInset());
402 text->setSelectionRange(length);
403 } else if (result == SR_NOT_FOUND) {
404 bv->unlockInset(bv->theLockingInset());