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;
38 // returns true if the specified string is at the specified position
39 bool isStringInText(Paragraph const & par, pos_type pos,
40 string const & str, bool const & cs,
43 string::size_type size = str.length();
45 pos_type parsize = par.size();
46 while (((pos + i) < parsize)
47 && (string::size_type(i) < size)
48 && (cs ? (str[i] == par.getChar(pos + i))
49 : (uppercase(str[i]) == uppercase(par.getChar(pos + i))))) {
53 if (size == string::size_type(i)) {
54 // if necessary, check whether string matches word
57 if ((pos <= 0 || !IsLetterCharOrDigit(par.getChar(pos - 1)))
58 && (pos + pos_type(size) >= parsize
59 || !IsLetterCharOrDigit(par.getChar(pos + size)))) {
67 // if the string can be found: return true and set the cursor to
68 // the new position, cs = casesensitive, mw = matchword
69 SearchResult searchForward(BufferView * bv, LyXText * text, string const & str,
70 bool const & cs, bool const & mw)
72 ParagraphList::iterator pit = text->cursor.par();
73 ParagraphList::iterator pend = text->ownerParagraphs().end();
74 pos_type pos = text->cursor.pos();
75 UpdatableInset * inset;
77 while (pit != pend && !isStringInText(*pit, pos, str, cs, mw)) {
80 && (inset = (UpdatableInset *)pit->getInset(pos))
81 && inset->isTextInset()
82 && inset->searchForward(bv, str, cs, mw))
83 return SR_FOUND_NOUPDATE;
85 if (++pos >= pit->size()) {
92 text->setCursor(pit, pos);
100 // if the string can be found: return true and set the cursor to
101 // the new position, cs = casesensitive, mw = matchword
102 SearchResult searchBackward(BufferView * bv, LyXText * text,
104 bool const & cs, bool const & mw)
106 ParagraphList::iterator pit = text->cursor.par();
107 ParagraphList::iterator pbegin = text->ownerParagraphs().begin();
108 pos_type pos = text->cursor.pos();
110 // skip past a match at the current cursor pos
113 } else if (pit != pbegin) {
121 if (pos < pit->size()) {
122 if (pit->isInset(pos) && pit->getInset(pos)->isTextInset()) {
123 UpdatableInset * inset = (UpdatableInset *)pit->getInset(pos);
124 if (inset->searchBackward(bv, str, cs, mw))
125 return SR_FOUND_NOUPDATE;
128 if (isStringInText(*pit, pos, str, cs, mw)) {
129 text->setCursor(pit, pos);
134 if (pos == 0 && pit == pbegin)
139 } else if (pit != pbegin) {
151 int replace(BufferView * bv,
152 string const & searchstr, string const & replacestr,
153 bool forward, bool casesens, bool matchwrd, bool replaceall,
156 if (!bv->available() || bv->buffer()->isReadonly())
159 // CutSelection cannot cut a single space, so we have to stop
160 // in order to avoid endless loop :-(
161 if (searchstr.length() == 0
162 || (searchstr.length() == 1 && searchstr[0] == ' ')) {
164 #warning BLECH. If we have an LFUN for replace, we can sort of fix this bogosity
166 Alert::error(_("Cannot replace"),
167 _("You cannot replace a single space or "
168 "an empty character."));
172 // now we can start searching for the first
173 // start at top if replaceall
174 LyXText * text = bv->getLyXText();
177 text->clearSelection();
178 bv->unlockInset(bv->theLockingInset());
181 // override search direction because we search top to bottom
185 // if nothing selected or selection does not equal search string
186 // search and select next occurance and return if no replaceall
191 str2 = text->selectionAsString(*bv->buffer(), false);
193 str1 = lowercase(searchstr);
194 str2 = lowercase(text->selectionAsString(*bv->buffer(), false));
197 if (!find(bv, searchstr, fw, casesens, matchwrd) ||
204 int replace_count = 0;
206 text = bv->getLyXText();
207 // We have to do this check only because mathed insets don't
208 // return their own LyXText but the LyXText of it's parent!
209 if (!bv->theLockingInset() ||
210 ((text != bv->text) &&
211 (text->inset_owner == text->inset_owner->getLockingInset()))) {
212 text->replaceSelectionWithString(replacestr);
213 text->setSelectionRange(replacestr.length());
217 found = find(bv, searchstr, fw, casesens, matchwrd);
218 } while (!once && replaceall && found);
220 // FIXME: should be called via an LFUN
221 bv->buffer()->markDirty();
224 return replace_count;
228 bool find(BufferView * bv,
229 string const & searchstr, bool forward,
230 bool casesens, bool matchwrd)
232 if (!bv->available() || searchstr.empty())
235 if (bv->theLockingInset()) {
236 bool found = forward ?
237 bv->theLockingInset()->searchForward(bv, searchstr, casesens, matchwrd) :
238 bv->theLockingInset()->searchBackward(bv, searchstr, casesens, matchwrd);
239 // We found the stuff inside the inset so we don't have to
240 // do anything as the inset did all the update for us!
243 // We now are in the main text but if we did a forward
244 // search we have to put the cursor behind the inset.
246 bv->text->cursorRight(true);
249 // If we arrive here we are in the main text again so we
250 // just start searching from the root LyXText at the position
252 LyXText * text = bv->text;
255 if (text->selection.set())
256 text->cursor = forward ?
257 text->selection.end : text->selection.start;
259 text->clearSelection();
261 SearchResult result = forward ?
262 searchForward(bv, text, searchstr, casesens, matchwrd) :
263 searchBackward(bv, text, searchstr, casesens, matchwrd);
266 // If we found the cursor inside an inset we will get back
267 // SR_FOUND_NOUPDATE and we don't have to do anything as the
268 // inset did it already.
269 if (result == SR_FOUND) {
270 bv->unlockInset(bv->theLockingInset());
271 text->setSelectionRange(searchstr.length());
272 } else if (result == SR_NOT_FOUND) {
273 bv->unlockInset(bv->theLockingInset());
282 SearchResult find(BufferView * bv, LyXText * text,
283 string const & searchstr, bool forward,
284 bool casesens, bool matchwrd)
286 if (text->selection.set())
287 text->cursor = forward ?
288 text->selection.end : text->selection.start;
290 text->clearSelection();
292 SearchResult result = forward ?
293 searchForward(bv, text, searchstr, casesens, matchwrd) :
294 searchBackward(bv, text, searchstr, casesens, matchwrd);
302 SearchResult nextChange(BufferView * bv, LyXText * text, pos_type & length)
304 ParagraphList::iterator pit = text->cursor.par();
305 ParagraphList::iterator pend = text->ownerParagraphs().end();
306 pos_type pos = text->cursor.pos();
308 while (pit != pend) {
309 pos_type parsize = pit->size();
312 if ((!parsize || pos != parsize)
313 && pit->lookupChange(pos) != Change::UNCHANGED)
316 if (pit->isInset(pos) && pit->getInset(pos)->isTextInset()) {
317 UpdatableInset * inset = (UpdatableInset *)pit->getInset(pos);
318 if (inset->nextChange(bv, length))
319 return SR_FOUND_NOUPDATE;
325 if (pos >= parsize) {
334 text->setCursor(pit, pos);
335 Change orig_change = pit->lookupChangeFull(pos);
336 pos_type parsize = pit->size();
339 for (; end != parsize; ++end) {
340 Change change = pit->lookupChangeFull(end);
341 if (change != orig_change) {
342 // slight UI optimisation: for replacements, we get
343 // text like : _old_new. Consider that as one change.
344 if (!(orig_change.type == Change::DELETED &&
345 change.type == Change::INSERTED))
354 SearchResult findNextChange(BufferView * bv, LyXText * text, pos_type & length)
356 if (text->selection.set())
357 text->cursor = text->selection.end;
359 text->clearSelection();
361 return nextChange(bv, text, length);
365 bool findNextChange(BufferView * bv)
367 if (!bv->available())
372 if (bv->theLockingInset()) {
373 bool found = bv->theLockingInset()->nextChange(bv, length);
375 // We found the stuff inside the inset so we don't have to
376 // do anything as the inset did all the update for us!
380 // We now are in the main text but if we did a forward
381 // search we have to put the cursor behind the inset.
382 bv->text->cursorRight(true);
384 // If we arrive here we are in the main text again so we
385 // just start searching from the root LyXText at the position
387 LyXText * text = bv->text;
389 if (text->selection.set())
390 text->cursor = text->selection.end;
392 text->clearSelection();
394 SearchResult result = nextChange(bv, text, length);
398 // If we found the cursor inside an inset we will get back
399 // SR_FOUND_NOUPDATE and we don't have to do anything as the
400 // inset did it already.
401 if (result == SR_FOUND) {
402 bv->unlockInset(bv->theLockingInset());
403 text->setSelectionRange(length);
404 } else if (result == SR_NOT_FOUND) {
405 bv->unlockInset(bv->theLockingInset());