6 #include "frontends/LyXView.h"
7 #include "frontends/Alert.h"
8 #include "support/textutils.h"
9 #include "support/lstrings.h"
10 #include "BufferView.h"
14 #include "insets/insettext.h"
22 /// returns true if the specified string is at the specified position
23 bool IsStringInText(Paragraph * par, pos_type pos,
24 string const & str, bool const & = true,
25 bool const & = false);
27 /// if the string is found: return true and set the cursor to the new position
28 SearchResult SearchForward(BufferView *, LyXText * text, string const & str,
29 bool const & = true, bool const & = false);
31 SearchResult SearchBackward(BufferView *, LyXText * text, string const & str,
32 bool const & = true, bool const & = false);
34 int LyXReplace(BufferView * bv,
35 string const & searchstr, string const & replacestr,
36 bool forward, bool casesens, bool matchwrd, bool replaceall,
39 if (!bv->available() || bv->buffer()->isReadonly())
42 // CutSelection cannot cut a single space, so we have to stop
43 // in order to avoid endless loop :-(
44 if (searchstr.length() == 0
45 || (searchstr.length() == 1 && searchstr[0] == ' ')) {
47 #warning BLECH. If we have an LFUN for replace, we can sort of fix this bogosity
49 Alert::error(_("Cannot replace"),
50 _("You cannot replace a single space or "
51 "an empty character."));
55 // now we can start searching for the first
56 // start at top if replaceall
57 LyXText * text = bv->getLyXText();
60 text->clearSelection();
61 bv->unlockInset(bv->theLockingInset());
64 // override search direction because we search top to bottom
68 // if nothing selected or selection does not equal search string
69 // search and select next occurance and return if no replaceall
74 str2 = text->selectionAsString(bv->buffer(), false);
76 str1 = lowercase(searchstr);
77 str2 = lowercase(text->selectionAsString(bv->buffer(), false));
80 if (!LyXFind(bv, searchstr, fw, casesens, matchwrd) ||
87 int replace_count = 0;
89 text = bv->getLyXText();
90 // We have to do this check only because mathed insets don't
91 // return their own LyXText but the LyXText of it's parent!
92 if (!bv->theLockingInset() ||
93 ((text != bv->text) &&
94 (text->inset_owner == text->inset_owner->getLockingInset()))) {
95 bv->update(text, BufferView::SELECT);
96 bv->toggleSelection(false);
97 text->replaceSelectionWithString(replacestr);
98 text->setSelectionRange(replacestr.length());
99 bv->update(text, BufferView::SELECT);
103 found = LyXFind(bv, searchstr, fw, casesens, matchwrd);
104 } while (!once && replaceall && found);
106 // FIXME: should be called via an LFUN
107 bv->buffer()->markDirty();
110 return replace_count;
114 bool LyXFind(BufferView * bv,
115 string const & searchstr, bool forward,
116 bool casesens, bool matchwrd)
118 if (!bv->available() || searchstr.empty())
121 bv->update(bv->getLyXText(), BufferView::SELECT);
123 if (bv->theLockingInset()) {
124 bool found = forward ?
125 bv->theLockingInset()->searchForward(bv, searchstr, casesens, matchwrd) :
126 bv->theLockingInset()->searchBackward(bv, searchstr, casesens, matchwrd);
127 // We found the stuff inside the inset so we don't have to
128 // do anything as the inset did all the update for us!
131 // We now are in the main text but if we did a forward
132 // search we have to put the cursor behind the inset.
134 bv->text->cursorRight(true);
137 // If we arrive here we are in the main text again so we
138 // just start searching from the root LyXText at the position
140 LyXText * text = bv->text;
143 if (text->selection.set())
144 text->cursor = forward ?
145 text->selection.end : text->selection.start;
147 bv->toggleSelection();
148 text->clearSelection();
150 SearchResult result = forward ?
151 SearchForward(bv, text, searchstr, casesens, matchwrd) :
152 SearchBackward(bv, text, searchstr, casesens, matchwrd);
155 // If we found the cursor inside an inset we will get back
156 // SR_FOUND_NOUPDATE and we don't have to do anything as the
157 // inset did it already.
158 if (result == SR_FOUND) {
159 bv->unlockInset(bv->theLockingInset());
160 bv->update(text, BufferView::SELECT);
161 text->setSelectionRange(searchstr.length());
162 bv->toggleSelection(false);
163 bv->update(text, BufferView::SELECT);
164 } else if (result == SR_NOT_FOUND) {
165 bv->unlockInset(bv->theLockingInset());
166 bv->update(text, BufferView::SELECT);
176 SearchResult LyXFind(BufferView * bv, LyXText * text,
177 string const & searchstr, bool forward,
178 bool casesens, bool matchwrd)
180 if (text->selection.set())
181 text->cursor = forward ?
182 text->selection.end : text->selection.start;
184 bv->toggleSelection();
185 text->clearSelection();
187 SearchResult result = forward ?
188 SearchForward(bv, text, searchstr, casesens, matchwrd) :
189 SearchBackward(bv, text, searchstr, casesens, matchwrd);
195 // returns true if the specified string is at the specified position
196 bool IsStringInText(Paragraph const & par, pos_type pos,
197 string const & str, bool const & cs,
200 string::size_type size = str.length();
202 pos_type parsize = par.size();
203 while (((pos + i) < parsize)
204 && (string::size_type(i) < size)
205 && (cs ? (str[i] == par.getChar(pos + i))
206 : (uppercase(str[i]) == uppercase(par.getChar(pos + i))))) {
210 if (size == string::size_type(i)) {
211 // if necessary, check whether string matches word
214 if ((pos <= 0 || !IsLetterCharOrDigit(par.getChar(pos - 1)))
215 && (pos + pos_type(size) >= parsize
216 || !IsLetterCharOrDigit(par.getChar(pos + size)))) {
224 // if the string can be found: return true and set the cursor to
225 // the new position, cs = casesensitive, mw = matchword
226 SearchResult SearchForward(BufferView * bv, LyXText * text, string const & str,
227 bool const & cs, bool const & mw)
229 ParagraphList::iterator pit = text->cursor.par();
230 ParagraphList::iterator pend = text->ownerParagraphs().end();
231 pos_type pos = text->cursor.pos();
232 UpdatableInset * inset;
234 while (pit != pend && !IsStringInText(*pit, pos, str, cs, mw)) {
235 if (pos < pit->size()
237 && (inset = (UpdatableInset *)pit->getInset(pos))
238 && inset->isTextInset()
239 && inset->searchForward(bv, str, cs, mw))
240 return SR_FOUND_NOUPDATE;
242 if (++pos >= pit->size()) {
249 text->setCursor(pit, pos);
257 // if the string can be found: return true and set the cursor to
258 // the new position, cs = casesensitive, mw = matchword
259 SearchResult SearchBackward(BufferView * bv, LyXText * text,
261 bool const & cs, bool const & mw)
263 ParagraphList::iterator pit = text->cursor.par();
264 ParagraphList::iterator pbegin = text->ownerParagraphs().begin();
265 pos_type pos = text->cursor.pos();
267 // skip past a match at the current cursor pos
270 } else if (pit != pbegin) {
278 if (pos < pit->size()) {
279 if (pit->isInset(pos) && pit->getInset(pos)->isTextInset()) {
280 UpdatableInset * inset = (UpdatableInset *)pit->getInset(pos);
281 if (inset->searchBackward(bv, str, cs, mw))
282 return SR_FOUND_NOUPDATE;
285 if (IsStringInText(*pit, pos, str, cs, mw)) {
286 text->setCursor(pit, pos);
291 if (pos == 0 && pit == pbegin)
296 } else if (pit != pbegin) {
306 SearchResult nextChange(BufferView * bv, LyXText * text, pos_type & length)
308 ParagraphList::iterator pit = text->cursor.par();
309 ParagraphList::iterator pend = text->ownerParagraphs().end();
310 pos_type pos = text->cursor.pos();
312 while (pit != pend) {
313 pos_type parsize = pit->size();
316 if ((!parsize || pos != parsize)
317 && pit->lookupChange(pos) != Change::UNCHANGED)
320 if (pit->isInset(pos) && pit->getInset(pos)->isTextInset()) {
321 UpdatableInset * inset = (UpdatableInset *)pit->getInset(pos);
322 if (inset->nextChange(bv, length))
323 return SR_FOUND_NOUPDATE;
329 if (pos >= parsize) {
338 text->setCursor(pit, pos);
339 Change orig_change = pit->lookupChangeFull(pos);
340 pos_type parsize = pit->size();
343 for (; end != parsize; ++end) {
344 Change change = pit->lookupChangeFull(end);
345 if (change != orig_change) {
346 // slight UI optimisation: for replacements, we get
347 // text like : _old_new. Consider that as one change.
348 if (!(orig_change.type == Change::DELETED &&
349 change.type == Change::INSERTED))
358 SearchResult findNextChange(BufferView * bv, LyXText * text, pos_type & length)
360 if (text->selection.set())
361 text->cursor = text->selection.end;
363 bv->toggleSelection();
364 text->clearSelection();
366 return nextChange(bv, text, length);
370 bool findNextChange(BufferView * bv)
372 if (!bv->available())
375 bv->update(bv->getLyXText(), BufferView::SELECT);
379 if (bv->theLockingInset()) {
380 bool found = bv->theLockingInset()->nextChange(bv, length);
382 // We found the stuff inside the inset so we don't have to
383 // do anything as the inset did all the update for us!
387 // We now are in the main text but if we did a forward
388 // search we have to put the cursor behind the inset.
389 bv->text->cursorRight(true);
391 // If we arrive here we are in the main text again so we
392 // just start searching from the root LyXText at the position
394 LyXText * text = bv->text;
396 if (text->selection.set())
397 text->cursor = text->selection.end;
399 bv->toggleSelection();
400 text->clearSelection();
402 SearchResult result = nextChange(bv, text, length);
406 // If we found the cursor inside an inset we will get back
407 // SR_FOUND_NOUPDATE and we don't have to do anything as the
408 // inset did it already.
409 if (result == SR_FOUND) {
410 bv->unlockInset(bv->theLockingInset());
411 bv->update(text, BufferView::SELECT);
412 text->setSelectionRange(length);
413 bv->toggleSelection(false);
414 bv->update(text, BufferView::SELECT);
415 } else if (result == SR_NOT_FOUND) {
416 bv->unlockInset(bv->theLockingInset());
417 bv->update(text, BufferView::SELECT);
426 } // end lyxfind namespace