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"
21 #include "paragraph.h"
23 #include "frontends/Alert.h"
25 #include "insets/insettext.h"
27 #include "support/textutils.h"
29 using lyx::support::lowercase;
30 using lyx::support::uppercase;
40 // returns true if the specified string is at the specified position
41 bool isStringInText(Paragraph const & par, pos_type pos,
42 string const & str, bool const & cs,
45 string::size_type size = str.length();
47 pos_type parsize = par.size();
48 while ((pos + i < parsize)
49 && (string::size_type(i) < size)
50 && (cs ? (str[i] == par.getChar(pos + i))
51 : (uppercase(str[i]) == uppercase(par.getChar(pos + i))))) {
55 if (size == string::size_type(i)) {
56 // if necessary, check whether string matches word
59 if ((pos <= 0 || !IsLetterCharOrDigit(par.getChar(pos - 1)))
60 && (pos + pos_type(size) >= parsize
61 || !IsLetterCharOrDigit(par.getChar(pos + size)))) {
69 // if the string can be found: return true and set the cursor to
70 // the new position, cs = casesensitive, mw = matchword
71 SearchResult searchForward(BufferView * bv, LyXText * text, string const & str,
72 bool const & cs, bool const & mw)
74 ParagraphList::iterator pit = text->cursorPar();
75 ParagraphList::iterator pend = text->ownerParagraphs().end();
76 pos_type pos = text->cursor.pos();
77 UpdatableInset * inset;
79 while (pit != pend && !isStringInText(*pit, pos, str, cs, mw)) {
82 && (inset = (UpdatableInset *)pit->getInset(pos))
83 && inset->isTextInset()
84 && inset->searchForward(bv, str, cs, mw))
85 return SR_FOUND_NOUPDATE;
87 if (++pos >= pit->size()) {
94 text->setCursor(pit, pos);
102 // if the string can be found: return true and set the cursor to
103 // the new position, cs = casesensitive, mw = matchword
104 SearchResult searchBackward(BufferView * bv, LyXText * text,
106 bool const & cs, bool const & mw)
108 ParagraphList::iterator pit = text->cursorPar();
109 ParagraphList::iterator pbegin = text->ownerParagraphs().begin();
110 pos_type pos = text->cursor.pos();
112 // skip past a match at the current cursor pos
115 } else if (pit != pbegin) {
123 if (pos < pit->size()) {
124 if (pit->isInset(pos) && pit->getInset(pos)->isTextInset()) {
125 UpdatableInset * inset = (UpdatableInset *)pit->getInset(pos);
126 if (inset->searchBackward(bv, str, cs, mw))
127 return SR_FOUND_NOUPDATE;
130 if (isStringInText(*pit, pos, str, cs, mw)) {
131 text->setCursor(pit, pos);
136 if (pos == 0 && pit == pbegin)
141 } else if (pit != pbegin) {
154 int replace(BufferView * bv,
155 string const & searchstr, string const & replacestr,
156 bool forward, bool casesens, bool matchwrd, bool replaceall,
159 if (!bv->available() || bv->buffer()->isReadonly())
162 // CutSelection cannot cut a single space, so we have to stop
163 // in order to avoid endless loop :-(
164 if (searchstr.length() == 0
165 || (searchstr.length() == 1 && searchstr[0] == ' ')) {
167 #warning BLECH. If we have an LFUN for replace, we can sort of fix this bogosity
169 Alert::error(_("Cannot replace"),
170 _("You cannot replace a single space or "
171 "an empty character."));
175 // now we can start searching for the first
176 // start at top if replaceall
177 LyXText * text = bv->getLyXText();
180 text->clearSelection();
181 bv->unlockInset(bv->theLockingInset());
184 // override search direction because we search top to bottom
188 // if nothing selected or selection does not equal search string
189 // search and select next occurance and return if no replaceall
194 str2 = text->selectionAsString(*bv->buffer(), false);
196 str1 = lowercase(searchstr);
197 str2 = lowercase(text->selectionAsString(*bv->buffer(), false));
200 if (!find(bv, searchstr, fw, casesens, matchwrd) ||
207 int replace_count = 0;
209 text = bv->getLyXText();
210 // We have to do this check only because mathed insets don't
211 // return their own LyXText but the LyXText of it's parent!
212 if (!bv->theLockingInset() ||
213 ((text != bv->text) &&
214 (text->inset_owner == text->inset_owner->getLockingInset()))) {
215 text->replaceSelectionWithString(replacestr);
216 text->setSelectionRange(replacestr.length());
220 found = find(bv, searchstr, fw, casesens, matchwrd);
221 } while (!once && replaceall && found);
223 // FIXME: should be called via an LFUN
224 bv->buffer()->markDirty();
227 return replace_count;
231 bool find(BufferView * bv,
232 string const & searchstr, bool forward,
233 bool casesens, bool matchwrd)
235 if (!bv->available() || searchstr.empty())
238 if (bv->theLockingInset()) {
239 bool found = forward ?
240 bv->theLockingInset()->searchForward(bv, searchstr, casesens, matchwrd) :
241 bv->theLockingInset()->searchBackward(bv, searchstr, casesens, matchwrd);
242 // We found the stuff inside the inset so we don't have to
243 // do anything as the inset did all the update for us!
246 // We now are in the main text but if we did a forward
247 // search we have to put the cursor behind the inset.
249 bv->text->cursorRight(true);
252 // If we arrive here we are in the main text again so we
253 // just start searching from the root LyXText at the position
255 LyXText * text = bv->text;
258 if (text->selection.set())
259 text->cursor = forward ?
260 text->selection.end : text->selection.start;
262 text->clearSelection();
264 SearchResult result = forward ?
265 searchForward(bv, text, searchstr, casesens, matchwrd) :
266 searchBackward(bv, text, searchstr, casesens, matchwrd);
269 // If we found the cursor inside an inset we will get back
270 // SR_FOUND_NOUPDATE and we don't have to do anything as the
271 // inset did it already.
272 if (result == SR_FOUND) {
273 bv->unlockInset(bv->theLockingInset());
274 text->setSelectionRange(searchstr.length());
275 } else if (result == SR_NOT_FOUND) {
276 bv->unlockInset(bv->theLockingInset());
285 SearchResult find(BufferView * bv, LyXText * text,
286 string const & searchstr, bool forward,
287 bool casesens, bool matchwrd)
289 if (text->selection.set())
290 text->cursor = forward ?
291 text->selection.end : text->selection.start;
293 text->clearSelection();
295 SearchResult result = forward ?
296 searchForward(bv, text, searchstr, casesens, matchwrd) :
297 searchBackward(bv, text, searchstr, casesens, matchwrd);
305 SearchResult nextChange(BufferView * bv, LyXText * text, pos_type & length)
307 ParagraphList::iterator pit = text->cursorPar();
308 ParagraphList::iterator pend = text->ownerParagraphs().end();
309 pos_type pos = text->cursor.pos();
311 while (pit != pend) {
312 pos_type parsize = pit->size();
315 if ((!parsize || pos != parsize)
316 && pit->lookupChange(pos) != Change::UNCHANGED)
319 if (pit->isInset(pos) && pit->getInset(pos)->isTextInset()) {
320 UpdatableInset * inset = (UpdatableInset *)pit->getInset(pos);
321 if (inset->nextChange(bv, length))
322 return SR_FOUND_NOUPDATE;
328 if (pos >= parsize) {
337 text->setCursor(pit, pos);
338 Change orig_change = pit->lookupChangeFull(pos);
339 pos_type parsize = pit->size();
342 for (; end != parsize; ++end) {
343 Change change = pit->lookupChangeFull(end);
344 if (change != orig_change) {
345 // slight UI optimisation: for replacements, we get
346 // text like : _old_new. Consider that as one change.
347 if (!(orig_change.type == Change::DELETED &&
348 change.type == Change::INSERTED))
357 SearchResult findNextChange(BufferView * bv, LyXText * text, pos_type & length)
359 if (text->selection.set())
360 text->cursor = text->selection.end;
362 text->clearSelection();
364 return nextChange(bv, text, length);
368 bool findNextChange(BufferView * bv)
370 if (!bv->available())
375 if (bv->theLockingInset()) {
376 bool found = bv->theLockingInset()->nextChange(bv, length);
378 // We found the stuff inside the inset so we don't have to
379 // do anything as the inset did all the update for us!
383 // We now are in the main text but if we did a forward
384 // search we have to put the cursor behind the inset.
385 bv->text->cursorRight(true);
387 // If we arrive here we are in the main text again so we
388 // just start searching from the root LyXText at the position
390 LyXText * text = bv->text;
392 if (text->selection.set())
393 text->cursor = text->selection.end;
395 text->clearSelection();
397 SearchResult result = nextChange(bv, text, length);
401 // If we found the cursor inside an inset we will get back
402 // SR_FOUND_NOUPDATE and we don't have to do anything as the
403 // inset did it already.
404 if (result == SR_FOUND) {
405 bv->unlockInset(bv->theLockingInset());
406 text->setSelectionRange(length);
407 } else if (result == SR_NOT_FOUND) {
408 bv->unlockInset(bv->theLockingInset());