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 "paragraph.h"
18 #include "frontends/LyXView.h"
19 #include "frontends/Alert.h"
20 #include "support/textutils.h"
21 #include "support/lstrings.h"
22 #include "BufferView.h"
26 #include "insets/insettext.h"
29 using namespace lyx::support;
39 // returns true if the specified string is at the specified position
40 bool isStringInText(Paragraph const & par, pos_type pos,
41 string const & str, bool const & cs,
44 string::size_type size = str.length();
46 pos_type parsize = par.size();
47 while (((pos + i) < parsize)
48 && (string::size_type(i) < size)
49 && (cs ? (str[i] == par.getChar(pos + i))
50 : (uppercase(str[i]) == uppercase(par.getChar(pos + i))))) {
54 if (size == string::size_type(i)) {
55 // if necessary, check whether string matches word
58 if ((pos <= 0 || !IsLetterCharOrDigit(par.getChar(pos - 1)))
59 && (pos + pos_type(size) >= parsize
60 || !IsLetterCharOrDigit(par.getChar(pos + size)))) {
68 // if the string can be found: return true and set the cursor to
69 // the new position, cs = casesensitive, mw = matchword
70 SearchResult searchForward(BufferView * bv, LyXText * text, string const & str,
71 bool const & cs, bool const & mw)
73 ParagraphList::iterator pit = text->cursor.par();
74 ParagraphList::iterator pend = text->ownerParagraphs().end();
75 pos_type pos = text->cursor.pos();
76 UpdatableInset * inset;
78 while (pit != pend && !isStringInText(*pit, pos, str, cs, mw)) {
81 && (inset = (UpdatableInset *)pit->getInset(pos))
82 && inset->isTextInset()
83 && inset->searchForward(bv, str, cs, mw))
84 return SR_FOUND_NOUPDATE;
86 if (++pos >= pit->size()) {
93 text->setCursor(pit, pos);
101 // if the string can be found: return true and set the cursor to
102 // the new position, cs = casesensitive, mw = matchword
103 SearchResult searchBackward(BufferView * bv, LyXText * text,
105 bool const & cs, bool const & mw)
107 ParagraphList::iterator pit = text->cursor.par();
108 ParagraphList::iterator pbegin = text->ownerParagraphs().begin();
109 pos_type pos = text->cursor.pos();
111 // skip past a match at the current cursor pos
114 } else if (pit != pbegin) {
122 if (pos < pit->size()) {
123 if (pit->isInset(pos) && pit->getInset(pos)->isTextInset()) {
124 UpdatableInset * inset = (UpdatableInset *)pit->getInset(pos);
125 if (inset->searchBackward(bv, str, cs, mw))
126 return SR_FOUND_NOUPDATE;
129 if (isStringInText(*pit, pos, str, cs, mw)) {
130 text->setCursor(pit, pos);
135 if (pos == 0 && pit == pbegin)
140 } else if (pit != pbegin) {
152 int replace(BufferView * bv,
153 string const & searchstr, string const & replacestr,
154 bool forward, bool casesens, bool matchwrd, bool replaceall,
157 if (!bv->available() || bv->buffer()->isReadonly())
160 // CutSelection cannot cut a single space, so we have to stop
161 // in order to avoid endless loop :-(
162 if (searchstr.length() == 0
163 || (searchstr.length() == 1 && searchstr[0] == ' ')) {
165 #warning BLECH. If we have an LFUN for replace, we can sort of fix this bogosity
167 Alert::error(_("Cannot replace"),
168 _("You cannot replace a single space or "
169 "an empty character."));
173 // now we can start searching for the first
174 // start at top if replaceall
175 LyXText * text = bv->getLyXText();
178 text->clearSelection();
179 bv->unlockInset(bv->theLockingInset());
182 // override search direction because we search top to bottom
186 // if nothing selected or selection does not equal search string
187 // search and select next occurance and return if no replaceall
192 str2 = text->selectionAsString(*bv->buffer(), false);
194 str1 = lowercase(searchstr);
195 str2 = lowercase(text->selectionAsString(*bv->buffer(), false));
198 if (!find(bv, searchstr, fw, casesens, matchwrd) ||
205 int replace_count = 0;
207 text = bv->getLyXText();
208 // We have to do this check only because mathed insets don't
209 // return their own LyXText but the LyXText of it's parent!
210 if (!bv->theLockingInset() ||
211 ((text != bv->text) &&
212 (text->inset_owner == text->inset_owner->getLockingInset()))) {
213 text->replaceSelectionWithString(replacestr);
214 text->setSelectionRange(replacestr.length());
218 found = find(bv, searchstr, fw, casesens, matchwrd);
219 } while (!once && replaceall && found);
221 // FIXME: should be called via an LFUN
222 bv->buffer()->markDirty();
225 return replace_count;
229 bool find(BufferView * bv,
230 string const & searchstr, bool forward,
231 bool casesens, bool matchwrd)
233 if (!bv->available() || searchstr.empty())
236 if (bv->theLockingInset()) {
237 bool found = forward ?
238 bv->theLockingInset()->searchForward(bv, searchstr, casesens, matchwrd) :
239 bv->theLockingInset()->searchBackward(bv, searchstr, casesens, matchwrd);
240 // We found the stuff inside the inset so we don't have to
241 // do anything as the inset did all the update for us!
244 // We now are in the main text but if we did a forward
245 // search we have to put the cursor behind the inset.
247 bv->text->cursorRight(true);
250 // If we arrive here we are in the main text again so we
251 // just start searching from the root LyXText at the position
253 LyXText * text = bv->text;
256 if (text->selection.set())
257 text->cursor = forward ?
258 text->selection.end : text->selection.start;
260 text->clearSelection();
262 SearchResult result = forward ?
263 searchForward(bv, text, searchstr, casesens, matchwrd) :
264 searchBackward(bv, text, searchstr, casesens, matchwrd);
267 // If we found the cursor inside an inset we will get back
268 // SR_FOUND_NOUPDATE and we don't have to do anything as the
269 // inset did it already.
270 if (result == SR_FOUND) {
271 bv->unlockInset(bv->theLockingInset());
272 text->setSelectionRange(searchstr.length());
273 } else if (result == SR_NOT_FOUND) {
274 bv->unlockInset(bv->theLockingInset());
283 SearchResult find(BufferView * bv, LyXText * text,
284 string const & searchstr, bool forward,
285 bool casesens, bool matchwrd)
287 if (text->selection.set())
288 text->cursor = forward ?
289 text->selection.end : text->selection.start;
291 text->clearSelection();
293 SearchResult result = forward ?
294 searchForward(bv, text, searchstr, casesens, matchwrd) :
295 searchBackward(bv, text, searchstr, casesens, matchwrd);
303 SearchResult nextChange(BufferView * bv, LyXText * text, pos_type & length)
305 ParagraphList::iterator pit = text->cursor.par();
306 ParagraphList::iterator pend = text->ownerParagraphs().end();
307 pos_type pos = text->cursor.pos();
309 while (pit != pend) {
310 pos_type parsize = pit->size();
313 if ((!parsize || pos != parsize)
314 && pit->lookupChange(pos) != Change::UNCHANGED)
317 if (pit->isInset(pos) && pit->getInset(pos)->isTextInset()) {
318 UpdatableInset * inset = (UpdatableInset *)pit->getInset(pos);
319 if (inset->nextChange(bv, length))
320 return SR_FOUND_NOUPDATE;
326 if (pos >= parsize) {
335 text->setCursor(pit, pos);
336 Change orig_change = pit->lookupChangeFull(pos);
337 pos_type parsize = pit->size();
340 for (; end != parsize; ++end) {
341 Change change = pit->lookupChangeFull(end);
342 if (change != orig_change) {
343 // slight UI optimisation: for replacements, we get
344 // text like : _old_new. Consider that as one change.
345 if (!(orig_change.type == Change::DELETED &&
346 change.type == Change::INSERTED))
355 SearchResult findNextChange(BufferView * bv, LyXText * text, pos_type & length)
357 if (text->selection.set())
358 text->cursor = text->selection.end;
360 text->clearSelection();
362 return nextChange(bv, text, length);
366 bool findNextChange(BufferView * bv)
368 if (!bv->available())
373 if (bv->theLockingInset()) {
374 bool found = bv->theLockingInset()->nextChange(bv, length);
376 // We found the stuff inside the inset so we don't have to
377 // do anything as the inset did all the update for us!
381 // We now are in the main text but if we did a forward
382 // search we have to put the cursor behind the inset.
383 bv->text->cursorRight(true);
385 // If we arrive here we are in the main text again so we
386 // just start searching from the root LyXText at the position
388 LyXText * text = bv->text;
390 if (text->selection.set())
391 text->cursor = text->selection.end;
393 text->clearSelection();
395 SearchResult result = nextChange(bv, text, length);
399 // If we found the cursor inside an inset we will get back
400 // SR_FOUND_NOUPDATE and we don't have to do anything as the
401 // inset did it already.
402 if (result == SR_FOUND) {
403 bv->unlockInset(bv->theLockingInset());
404 text->setSelectionRange(length);
405 } else if (result == SR_NOT_FOUND) {
406 bv->unlockInset(bv->theLockingInset());