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.
17 #include "frontends/Alert.h"
18 #include "support/textutils.h"
19 #include "BufferView.h"
22 #include "insets/insettext.h"
24 using namespace lyx::support;
32 // returns true if the specified string is at the specified position
33 bool isStringInText(Paragraph const & par, pos_type pos,
34 string const & str, bool const & cs,
37 string::size_type size = str.length();
39 pos_type parsize = par.size();
40 while (((pos + i) < parsize)
41 && (string::size_type(i) < size)
42 && (cs ? (str[i] == par.getChar(pos + i))
43 : (uppercase(str[i]) == uppercase(par.getChar(pos + i))))) {
47 if (size == string::size_type(i)) {
48 // if necessary, check whether string matches word
51 if ((pos <= 0 || !IsLetterCharOrDigit(par.getChar(pos - 1)))
52 && (pos + pos_type(size) >= parsize
53 || !IsLetterCharOrDigit(par.getChar(pos + size)))) {
61 // if the string can be found: return true and set the cursor to
62 // the new position, cs = casesensitive, mw = matchword
63 SearchResult searchForward(BufferView * bv, LyXText * text, string const & str,
64 bool const & cs, bool const & mw)
66 ParagraphList::iterator pit = text->cursor.par();
67 ParagraphList::iterator pend = text->ownerParagraphs().end();
68 pos_type pos = text->cursor.pos();
69 UpdatableInset * inset;
71 while (pit != pend && !isStringInText(*pit, pos, str, cs, mw)) {
74 && (inset = (UpdatableInset *)pit->getInset(pos))
75 && inset->isTextInset()
76 && inset->searchForward(bv, str, cs, mw))
77 return SR_FOUND_NOUPDATE;
79 if (++pos >= pit->size()) {
86 text->setCursor(pit, pos);
94 // if the string can be found: return true and set the cursor to
95 // the new position, cs = casesensitive, mw = matchword
96 SearchResult searchBackward(BufferView * bv, LyXText * text,
98 bool const & cs, bool const & mw)
100 ParagraphList::iterator pit = text->cursor.par();
101 ParagraphList::iterator pbegin = text->ownerParagraphs().begin();
102 pos_type pos = text->cursor.pos();
104 // skip past a match at the current cursor pos
107 } else if (pit != pbegin) {
115 if (pos < pit->size()) {
116 if (pit->isInset(pos) && pit->getInset(pos)->isTextInset()) {
117 UpdatableInset * inset = (UpdatableInset *)pit->getInset(pos);
118 if (inset->searchBackward(bv, str, cs, mw))
119 return SR_FOUND_NOUPDATE;
122 if (isStringInText(*pit, pos, str, cs, mw)) {
123 text->setCursor(pit, pos);
128 if (pos == 0 && pit == pbegin)
133 } else if (pit != pbegin) {
145 int replace(BufferView * bv,
146 string const & searchstr, string const & replacestr,
147 bool forward, bool casesens, bool matchwrd, bool replaceall,
150 if (!bv->available() || bv->buffer()->isReadonly())
153 // CutSelection cannot cut a single space, so we have to stop
154 // in order to avoid endless loop :-(
155 if (searchstr.length() == 0
156 || (searchstr.length() == 1 && searchstr[0] == ' ')) {
158 #warning BLECH. If we have an LFUN for replace, we can sort of fix this bogosity
160 Alert::error(_("Cannot replace"),
161 _("You cannot replace a single space or "
162 "an empty character."));
166 // now we can start searching for the first
167 // start at top if replaceall
168 LyXText * text = bv->getLyXText();
171 text->clearSelection();
172 bv->unlockInset(bv->theLockingInset());
175 // override search direction because we search top to bottom
179 // if nothing selected or selection does not equal search string
180 // search and select next occurance and return if no replaceall
185 str2 = text->selectionAsString(*bv->buffer(), false);
187 str1 = lowercase(searchstr);
188 str2 = lowercase(text->selectionAsString(*bv->buffer(), false));
191 if (!find(bv, searchstr, fw, casesens, matchwrd) ||
198 int replace_count = 0;
200 text = bv->getLyXText();
201 // We have to do this check only because mathed insets don't
202 // return their own LyXText but the LyXText of it's parent!
203 if (!bv->theLockingInset() ||
204 ((text != bv->text) &&
205 (text->inset_owner == text->inset_owner->getLockingInset()))) {
206 text->replaceSelectionWithString(replacestr);
207 text->setSelectionRange(replacestr.length());
211 found = find(bv, searchstr, fw, casesens, matchwrd);
212 } while (!once && replaceall && found);
214 // FIXME: should be called via an LFUN
215 bv->buffer()->markDirty();
218 return replace_count;
222 bool find(BufferView * bv,
223 string const & searchstr, bool forward,
224 bool casesens, bool matchwrd)
226 if (!bv->available() || searchstr.empty())
229 if (bv->theLockingInset()) {
230 bool found = forward ?
231 bv->theLockingInset()->searchForward(bv, searchstr, casesens, matchwrd) :
232 bv->theLockingInset()->searchBackward(bv, searchstr, casesens, matchwrd);
233 // We found the stuff inside the inset so we don't have to
234 // do anything as the inset did all the update for us!
237 // We now are in the main text but if we did a forward
238 // search we have to put the cursor behind the inset.
240 bv->text->cursorRight(true);
243 // If we arrive here we are in the main text again so we
244 // just start searching from the root LyXText at the position
246 LyXText * text = bv->text;
249 if (text->selection.set())
250 text->cursor = forward ?
251 text->selection.end : text->selection.start;
253 text->clearSelection();
255 SearchResult result = forward ?
256 searchForward(bv, text, searchstr, casesens, matchwrd) :
257 searchBackward(bv, text, searchstr, casesens, matchwrd);
260 // If we found the cursor inside an inset we will get back
261 // SR_FOUND_NOUPDATE and we don't have to do anything as the
262 // inset did it already.
263 if (result == SR_FOUND) {
264 bv->unlockInset(bv->theLockingInset());
265 text->setSelectionRange(searchstr.length());
266 } else if (result == SR_NOT_FOUND) {
267 bv->unlockInset(bv->theLockingInset());
276 SearchResult find(BufferView * bv, LyXText * text,
277 string const & searchstr, bool forward,
278 bool casesens, bool matchwrd)
280 if (text->selection.set())
281 text->cursor = forward ?
282 text->selection.end : text->selection.start;
284 text->clearSelection();
286 SearchResult result = forward ?
287 searchForward(bv, text, searchstr, casesens, matchwrd) :
288 searchBackward(bv, text, searchstr, casesens, matchwrd);
296 SearchResult nextChange(BufferView * bv, LyXText * text, pos_type & length)
298 ParagraphList::iterator pit = text->cursor.par();
299 ParagraphList::iterator pend = text->ownerParagraphs().end();
300 pos_type pos = text->cursor.pos();
302 while (pit != pend) {
303 pos_type parsize = pit->size();
306 if ((!parsize || pos != parsize)
307 && pit->lookupChange(pos) != Change::UNCHANGED)
310 if (pit->isInset(pos) && pit->getInset(pos)->isTextInset()) {
311 UpdatableInset * inset = (UpdatableInset *)pit->getInset(pos);
312 if (inset->nextChange(bv, length))
313 return SR_FOUND_NOUPDATE;
319 if (pos >= parsize) {
328 text->setCursor(pit, pos);
329 Change orig_change = pit->lookupChangeFull(pos);
330 pos_type parsize = pit->size();
333 for (; end != parsize; ++end) {
334 Change change = pit->lookupChangeFull(end);
335 if (change != orig_change) {
336 // slight UI optimisation: for replacements, we get
337 // text like : _old_new. Consider that as one change.
338 if (!(orig_change.type == Change::DELETED &&
339 change.type == Change::INSERTED))
348 SearchResult findNextChange(BufferView * bv, LyXText * text, pos_type & length)
350 if (text->selection.set())
351 text->cursor = text->selection.end;
353 text->clearSelection();
355 return nextChange(bv, text, length);
359 bool findNextChange(BufferView * bv)
361 if (!bv->available())
366 if (bv->theLockingInset()) {
367 bool found = bv->theLockingInset()->nextChange(bv, length);
369 // We found the stuff inside the inset so we don't have to
370 // do anything as the inset did all the update for us!
374 // We now are in the main text but if we did a forward
375 // search we have to put the cursor behind the inset.
376 bv->text->cursorRight(true);
378 // If we arrive here we are in the main text again so we
379 // just start searching from the root LyXText at the position
381 LyXText * text = bv->text;
383 if (text->selection.set())
384 text->cursor = text->selection.end;
386 text->clearSelection();
388 SearchResult result = nextChange(bv, text, length);
392 // If we found the cursor inside an inset we will get back
393 // SR_FOUND_NOUPDATE and we don't have to do anything as the
394 // inset did it already.
395 if (result == SR_FOUND) {
396 bv->unlockInset(bv->theLockingInset());
397 text->setSelectionRange(length);
398 } else if (result == SR_NOT_FOUND) {
399 bv->unlockInset(bv->theLockingInset());