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;
34 // returns true if the specified string is at the specified position
35 bool isStringInText(Paragraph const & par, pos_type pos,
36 string const & str, bool const & cs,
39 string::size_type size = str.length();
41 pos_type parsize = par.size();
42 while (((pos + i) < parsize)
43 && (string::size_type(i) < size)
44 && (cs ? (str[i] == par.getChar(pos + i))
45 : (uppercase(str[i]) == uppercase(par.getChar(pos + i))))) {
49 if (size == string::size_type(i)) {
50 // if necessary, check whether string matches word
53 if ((pos <= 0 || !IsLetterCharOrDigit(par.getChar(pos - 1)))
54 && (pos + pos_type(size) >= parsize
55 || !IsLetterCharOrDigit(par.getChar(pos + size)))) {
63 // if the string can be found: return true and set the cursor to
64 // the new position, cs = casesensitive, mw = matchword
65 SearchResult searchForward(BufferView * bv, LyXText * text, string const & str,
66 bool const & cs, bool const & mw)
68 ParagraphList::iterator pit = text->cursor.par();
69 ParagraphList::iterator pend = text->ownerParagraphs().end();
70 pos_type pos = text->cursor.pos();
71 UpdatableInset * inset;
73 while (pit != pend && !isStringInText(*pit, pos, str, cs, mw)) {
76 && (inset = (UpdatableInset *)pit->getInset(pos))
77 && inset->isTextInset()
78 && inset->searchForward(bv, str, cs, mw))
79 return SR_FOUND_NOUPDATE;
81 if (++pos >= pit->size()) {
88 text->setCursor(pit, pos);
96 // if the string can be found: return true and set the cursor to
97 // the new position, cs = casesensitive, mw = matchword
98 SearchResult searchBackward(BufferView * bv, LyXText * text,
100 bool const & cs, bool const & mw)
102 ParagraphList::iterator pit = text->cursor.par();
103 ParagraphList::iterator pbegin = text->ownerParagraphs().begin();
104 pos_type pos = text->cursor.pos();
106 // skip past a match at the current cursor pos
109 } else if (pit != pbegin) {
117 if (pos < pit->size()) {
118 if (pit->isInset(pos) && pit->getInset(pos)->isTextInset()) {
119 UpdatableInset * inset = (UpdatableInset *)pit->getInset(pos);
120 if (inset->searchBackward(bv, str, cs, mw))
121 return SR_FOUND_NOUPDATE;
124 if (isStringInText(*pit, pos, str, cs, mw)) {
125 text->setCursor(pit, pos);
130 if (pos == 0 && pit == pbegin)
135 } else if (pit != pbegin) {
147 int replace(BufferView * bv,
148 string const & searchstr, string const & replacestr,
149 bool forward, bool casesens, bool matchwrd, bool replaceall,
152 if (!bv->available() || bv->buffer()->isReadonly())
155 // CutSelection cannot cut a single space, so we have to stop
156 // in order to avoid endless loop :-(
157 if (searchstr.length() == 0
158 || (searchstr.length() == 1 && searchstr[0] == ' ')) {
160 #warning BLECH. If we have an LFUN for replace, we can sort of fix this bogosity
162 Alert::error(_("Cannot replace"),
163 _("You cannot replace a single space or "
164 "an empty character."));
168 // now we can start searching for the first
169 // start at top if replaceall
170 LyXText * text = bv->getLyXText();
173 text->clearSelection();
174 bv->unlockInset(bv->theLockingInset());
177 // override search direction because we search top to bottom
181 // if nothing selected or selection does not equal search string
182 // search and select next occurance and return if no replaceall
187 str2 = text->selectionAsString(*bv->buffer(), false);
189 str1 = lowercase(searchstr);
190 str2 = lowercase(text->selectionAsString(*bv->buffer(), false));
193 if (!find(bv, searchstr, fw, casesens, matchwrd) ||
200 int replace_count = 0;
202 text = bv->getLyXText();
203 // We have to do this check only because mathed insets don't
204 // return their own LyXText but the LyXText of it's parent!
205 if (!bv->theLockingInset() ||
206 ((text != bv->text) &&
207 (text->inset_owner == text->inset_owner->getLockingInset()))) {
208 text->replaceSelectionWithString(replacestr);
209 text->setSelectionRange(replacestr.length());
213 found = find(bv, searchstr, fw, casesens, matchwrd);
214 } while (!once && replaceall && found);
216 // FIXME: should be called via an LFUN
217 bv->buffer()->markDirty();
220 return replace_count;
224 bool find(BufferView * bv,
225 string const & searchstr, bool forward,
226 bool casesens, bool matchwrd)
228 if (!bv->available() || searchstr.empty())
231 if (bv->theLockingInset()) {
232 bool found = forward ?
233 bv->theLockingInset()->searchForward(bv, searchstr, casesens, matchwrd) :
234 bv->theLockingInset()->searchBackward(bv, searchstr, casesens, matchwrd);
235 // We found the stuff inside the inset so we don't have to
236 // do anything as the inset did all the update for us!
239 // We now are in the main text but if we did a forward
240 // search we have to put the cursor behind the inset.
242 bv->text->cursorRight(true);
245 // If we arrive here we are in the main text again so we
246 // just start searching from the root LyXText at the position
248 LyXText * text = bv->text;
251 if (text->selection.set())
252 text->cursor = forward ?
253 text->selection.end : text->selection.start;
255 text->clearSelection();
257 SearchResult result = forward ?
258 searchForward(bv, text, searchstr, casesens, matchwrd) :
259 searchBackward(bv, text, searchstr, casesens, matchwrd);
262 // If we found the cursor inside an inset we will get back
263 // SR_FOUND_NOUPDATE and we don't have to do anything as the
264 // inset did it already.
265 if (result == SR_FOUND) {
266 bv->unlockInset(bv->theLockingInset());
267 text->setSelectionRange(searchstr.length());
268 } else if (result == SR_NOT_FOUND) {
269 bv->unlockInset(bv->theLockingInset());
278 SearchResult find(BufferView * bv, LyXText * text,
279 string const & searchstr, bool forward,
280 bool casesens, bool matchwrd)
282 if (text->selection.set())
283 text->cursor = forward ?
284 text->selection.end : text->selection.start;
286 text->clearSelection();
288 SearchResult result = forward ?
289 searchForward(bv, text, searchstr, casesens, matchwrd) :
290 searchBackward(bv, text, searchstr, casesens, matchwrd);
298 SearchResult nextChange(BufferView * bv, LyXText * text, pos_type & length)
300 ParagraphList::iterator pit = text->cursor.par();
301 ParagraphList::iterator pend = text->ownerParagraphs().end();
302 pos_type pos = text->cursor.pos();
304 while (pit != pend) {
305 pos_type parsize = pit->size();
308 if ((!parsize || pos != parsize)
309 && pit->lookupChange(pos) != Change::UNCHANGED)
312 if (pit->isInset(pos) && pit->getInset(pos)->isTextInset()) {
313 UpdatableInset * inset = (UpdatableInset *)pit->getInset(pos);
314 if (inset->nextChange(bv, length))
315 return SR_FOUND_NOUPDATE;
321 if (pos >= parsize) {
330 text->setCursor(pit, pos);
331 Change orig_change = pit->lookupChangeFull(pos);
332 pos_type parsize = pit->size();
335 for (; end != parsize; ++end) {
336 Change change = pit->lookupChangeFull(end);
337 if (change != orig_change) {
338 // slight UI optimisation: for replacements, we get
339 // text like : _old_new. Consider that as one change.
340 if (!(orig_change.type == Change::DELETED &&
341 change.type == Change::INSERTED))
350 SearchResult findNextChange(BufferView * bv, LyXText * text, pos_type & length)
352 if (text->selection.set())
353 text->cursor = text->selection.end;
355 text->clearSelection();
357 return nextChange(bv, text, length);
361 bool findNextChange(BufferView * bv)
363 if (!bv->available())
368 if (bv->theLockingInset()) {
369 bool found = bv->theLockingInset()->nextChange(bv, length);
371 // We found the stuff inside the inset so we don't have to
372 // do anything as the inset did all the update for us!
376 // We now are in the main text but if we did a forward
377 // search we have to put the cursor behind the inset.
378 bv->text->cursorRight(true);
380 // If we arrive here we are in the main text again so we
381 // just start searching from the root LyXText at the position
383 LyXText * text = bv->text;
385 if (text->selection.set())
386 text->cursor = text->selection.end;
388 text->clearSelection();
390 SearchResult result = nextChange(bv, text, length);
394 // If we found the cursor inside an inset we will get back
395 // SR_FOUND_NOUPDATE and we don't have to do anything as the
396 // inset did it already.
397 if (result == SR_FOUND) {
398 bv->unlockInset(bv->theLockingInset());
399 text->setSelectionRange(length);
400 } else if (result == SR_NOT_FOUND) {
401 bv->unlockInset(bv->theLockingInset());