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->cursor.par();
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->cursor.par();
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) {
153 int replace(BufferView * bv,
154 string const & searchstr, string const & replacestr,
155 bool forward, bool casesens, bool matchwrd, bool replaceall,
158 if (!bv->available() || bv->buffer()->isReadonly())
161 // CutSelection cannot cut a single space, so we have to stop
162 // in order to avoid endless loop :-(
163 if (searchstr.length() == 0
164 || (searchstr.length() == 1 && searchstr[0] == ' ')) {
166 #warning BLECH. If we have an LFUN for replace, we can sort of fix this bogosity
168 Alert::error(_("Cannot replace"),
169 _("You cannot replace a single space or "
170 "an empty character."));
174 // now we can start searching for the first
175 // start at top if replaceall
176 LyXText * text = bv->getLyXText();
179 text->clearSelection();
180 bv->unlockInset(bv->theLockingInset());
183 // override search direction because we search top to bottom
187 // if nothing selected or selection does not equal search string
188 // search and select next occurance and return if no replaceall
193 str2 = text->selectionAsString(*bv->buffer(), false);
195 str1 = lowercase(searchstr);
196 str2 = lowercase(text->selectionAsString(*bv->buffer(), false));
199 if (!find(bv, searchstr, fw, casesens, matchwrd) ||
206 int replace_count = 0;
208 text = bv->getLyXText();
209 // We have to do this check only because mathed insets don't
210 // return their own LyXText but the LyXText of it's parent!
211 if (!bv->theLockingInset() ||
212 ((text != bv->text) &&
213 (text->inset_owner == text->inset_owner->getLockingInset()))) {
214 text->replaceSelectionWithString(replacestr);
215 text->setSelectionRange(replacestr.length());
219 found = find(bv, searchstr, fw, casesens, matchwrd);
220 } while (!once && replaceall && found);
222 // FIXME: should be called via an LFUN
223 bv->buffer()->markDirty();
226 return replace_count;
230 bool find(BufferView * bv,
231 string const & searchstr, bool forward,
232 bool casesens, bool matchwrd)
234 if (!bv->available() || searchstr.empty())
237 if (bv->theLockingInset()) {
238 bool found = forward ?
239 bv->theLockingInset()->searchForward(bv, searchstr, casesens, matchwrd) :
240 bv->theLockingInset()->searchBackward(bv, searchstr, casesens, matchwrd);
241 // We found the stuff inside the inset so we don't have to
242 // do anything as the inset did all the update for us!
245 // We now are in the main text but if we did a forward
246 // search we have to put the cursor behind the inset.
248 bv->text->cursorRight(true);
251 // If we arrive here we are in the main text again so we
252 // just start searching from the root LyXText at the position
254 LyXText * text = bv->text;
257 if (text->selection.set())
258 text->cursor = forward ?
259 text->selection.end : text->selection.start;
261 text->clearSelection();
263 SearchResult result = forward ?
264 searchForward(bv, text, searchstr, casesens, matchwrd) :
265 searchBackward(bv, text, searchstr, casesens, matchwrd);
268 // If we found the cursor inside an inset we will get back
269 // SR_FOUND_NOUPDATE and we don't have to do anything as the
270 // inset did it already.
271 if (result == SR_FOUND) {
272 bv->unlockInset(bv->theLockingInset());
273 text->setSelectionRange(searchstr.length());
274 } else if (result == SR_NOT_FOUND) {
275 bv->unlockInset(bv->theLockingInset());
284 SearchResult find(BufferView * bv, LyXText * text,
285 string const & searchstr, bool forward,
286 bool casesens, bool matchwrd)
288 if (text->selection.set())
289 text->cursor = forward ?
290 text->selection.end : text->selection.start;
292 text->clearSelection();
294 SearchResult result = forward ?
295 searchForward(bv, text, searchstr, casesens, matchwrd) :
296 searchBackward(bv, text, searchstr, casesens, matchwrd);
304 SearchResult nextChange(BufferView * bv, LyXText * text, pos_type & length)
306 ParagraphList::iterator pit = text->cursor.par();
307 ParagraphList::iterator pend = text->ownerParagraphs().end();
308 pos_type pos = text->cursor.pos();
310 while (pit != pend) {
311 pos_type parsize = pit->size();
314 if ((!parsize || pos != parsize)
315 && pit->lookupChange(pos) != Change::UNCHANGED)
318 if (pit->isInset(pos) && pit->getInset(pos)->isTextInset()) {
319 UpdatableInset * inset = (UpdatableInset *)pit->getInset(pos);
320 if (inset->nextChange(bv, length))
321 return SR_FOUND_NOUPDATE;
327 if (pos >= parsize) {
336 text->setCursor(pit, pos);
337 Change orig_change = pit->lookupChangeFull(pos);
338 pos_type parsize = pit->size();
341 for (; end != parsize; ++end) {
342 Change change = pit->lookupChangeFull(end);
343 if (change != orig_change) {
344 // slight UI optimisation: for replacements, we get
345 // text like : _old_new. Consider that as one change.
346 if (!(orig_change.type == Change::DELETED &&
347 change.type == Change::INSERTED))
356 SearchResult findNextChange(BufferView * bv, LyXText * text, pos_type & length)
358 if (text->selection.set())
359 text->cursor = text->selection.end;
361 text->clearSelection();
363 return nextChange(bv, text, length);
367 bool findNextChange(BufferView * bv)
369 if (!bv->available())
374 if (bv->theLockingInset()) {
375 bool found = bv->theLockingInset()->nextChange(bv, length);
377 // We found the stuff inside the inset so we don't have to
378 // do anything as the inset did all the update for us!
382 // We now are in the main text but if we did a forward
383 // search we have to put the cursor behind the inset.
384 bv->text->cursorRight(true);
386 // If we arrive here we are in the main text again so we
387 // just start searching from the root LyXText at the position
389 LyXText * text = bv->text;
391 if (text->selection.set())
392 text->cursor = text->selection.end;
394 text->clearSelection();
396 SearchResult result = nextChange(bv, text, length);
400 // If we found the cursor inside an inset we will get back
401 // SR_FOUND_NOUPDATE and we don't have to do anything as the
402 // inset did it already.
403 if (result == SR_FOUND) {
404 bv->unlockInset(bv->theLockingInset());
405 text->setSelectionRange(length);
406 } else if (result == SR_NOT_FOUND) {
407 bv->unlockInset(bv->theLockingInset());