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 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) ||
88 int replace_count = 0;
90 text = bv->getLyXText();
91 // We have to do this check only because mathed insets don't
92 // return their own LyXText but the LyXText of it's parent!
93 if (!bv->theLockingInset() ||
94 ((text != bv->text) &&
95 (text->inset_owner == text->inset_owner->getLockingInset())))
98 bv->update(text, BufferView::SELECT);
99 bv->toggleSelection(false);
100 text->replaceSelectionWithString(replacestr);
101 text->setSelectionRange(replacestr.length());
102 bv->update(text, BufferView::SELECT);
106 found = LyXFind(bv, searchstr, fw, casesens, matchwrd);
107 } while (!once && replaceall && found);
109 // FIXME: should be called via an LFUN
110 bv->buffer()->markDirty();
113 return replace_count;
117 bool LyXFind(BufferView * bv,
118 string const & searchstr, bool forward,
119 bool casesens, bool matchwrd)
121 if (!bv->available() || searchstr.empty())
125 bv->update(bv->getLyXText(), BufferView::SELECT);
127 if (bv->theLockingInset()) {
128 bool found = forward ?
129 bv->theLockingInset()->searchForward(bv, searchstr, casesens, matchwrd) :
130 bv->theLockingInset()->searchBackward(bv, searchstr, casesens, matchwrd);
131 // We found the stuff inside the inset so we don't have to
132 // do anything as the inset did all the update for us!
135 // We now are in the main text but if we did a forward
136 // search we have to put the cursor behind the inset.
138 bv->text->cursorRight(true);
141 // If we arrive here we are in the main text again so we
142 // just start searching from the root LyXText at the position
144 LyXText * text = bv->text;
146 if (text->selection.set())
147 text->cursor = forward ?
148 text->selection.end : text->selection.start;
150 bv->toggleSelection();
151 text->clearSelection();
153 SearchResult result = forward ?
154 SearchForward(bv, text, searchstr, casesens, matchwrd) :
155 SearchBackward(bv, text, searchstr, casesens, matchwrd);
158 // If we found the cursor inside an inset we will get back
159 // SR_FOUND_NOUPDATE and we don't have to do anything as the
160 // inset did it already.
161 if (result == SR_FOUND) {
162 bv->unlockInset(bv->theLockingInset());
163 bv->update(text, BufferView::SELECT);
164 text->setSelectionRange(searchstr.length());
165 bv->toggleSelection(false);
166 bv->update(text, BufferView::SELECT);
167 } else if (result == SR_NOT_FOUND) {
168 bv->unlockInset(bv->theLockingInset());
169 bv->update(text, BufferView::SELECT);
179 SearchResult LyXFind(BufferView * bv, LyXText * text,
180 string const & searchstr, bool forward,
181 bool casesens, bool matchwrd)
183 if (text->selection.set())
184 text->cursor = forward ?
185 text->selection.end : text->selection.start;
187 bv->toggleSelection();
188 text->clearSelection();
190 SearchResult result = forward ?
191 SearchForward(bv, text, searchstr, casesens, matchwrd) :
192 SearchBackward(bv, text, searchstr, casesens, matchwrd);
198 // returns true if the specified string is at the specified position
199 bool IsStringInText(Paragraph * par, pos_type pos,
200 string const & str, bool const & cs,
206 string::size_type size = str.length();
208 while (((pos + i) < par->size())
209 && (string::size_type(i) < size)
210 && (cs ? (str[i] == par->getChar(pos + i))
211 : (uppercase(str[i]) == uppercase(par->getChar(pos + i)))))
215 if (size == string::size_type(i)) {
216 // if necessary, check whether string matches word
219 if ((pos <= 0 || !IsLetterCharOrDigit(par->getChar(pos - 1)))
220 && (pos + pos_type(size) >= par->size()
221 || !IsLetterCharOrDigit(par->getChar(pos + size)))) {
229 // if the string can be found: return true and set the cursor to
230 // the new position, cs = casesensitive, mw = matchword
231 SearchResult SearchForward(BufferView * bv, LyXText * text, string const & str,
232 bool const & cs, bool const & mw)
234 Paragraph * par = text->cursor.par();
235 pos_type pos = text->cursor.pos();
236 Paragraph * prev_par = par;
237 UpdatableInset * inset;
239 while (par && !IsStringInText(par, pos, str, cs, mw)) {
240 if (par->isInset(pos) &&
241 (inset = (UpdatableInset *)par->getInset(pos)) &&
242 (inset->isTextInset()))
246 text->setCursor(bv, par, pos);
249 if (inset->searchForward(bv, str, cs, mw))
250 return SR_FOUND_NOUPDATE;
255 if (pos >= par->size()) {
263 text->setCursor(par, pos);
266 // make sure we end up at the end of the text,
267 // not the start point of the last search
268 text->setCursor(prev_par, prev_par->size());
275 // if the string can be found: return true and set the cursor to
276 // the new position, cs = casesensitive, mw = matchword
277 SearchResult SearchBackward(BufferView * bv, LyXText * text,
279 bool const & cs, bool const & mw)
281 Paragraph * par = text->cursor.par();
282 pos_type pos = text->cursor.pos();
283 Paragraph * prev_par = par;
290 // We skip empty paragraphs (Asger)
292 par = par->previous();
294 pos = par->size() - 1;
295 } while (par && pos < 0);
297 UpdatableInset * inset;
298 if (par && par->isInset(pos) &&
299 (inset = (UpdatableInset *)par->getInset(pos)) &&
300 (inset->isTextInset()))
304 text->setCursor(bv, par, pos);
305 inset->edit(bv, false);
307 if (inset->searchBackward(bv, str, cs, mw))
308 return SR_FOUND_NOUPDATE;
310 } while (par && !IsStringInText(par, pos, str, cs, mw));
313 text->setCursor(par, pos);
316 // go to the last part of the unsuccessful search
317 text->setCursor(prev_par, 0);
323 SearchResult nextChange(BufferView * bv, LyXText * text, pos_type & length)
325 Paragraph * par = text->cursor.par();
326 pos_type pos = text->cursor.pos();
327 Paragraph * prev_par = par;
328 UpdatableInset * inset;
331 if ((!par->size() || pos != par->size())
332 && par->lookupChange(pos) != Change::UNCHANGED)
335 if (par->isInset(pos) &&
336 (inset = (UpdatableInset *)par->getInset(pos)) &&
337 (inset->isTextInset())) {
338 if (inset->nextChange(bv, length))
339 return SR_FOUND_NOUPDATE;
344 if (pos >= par->size()) {
352 text->setCursor(par, pos);
353 Change orig_change = par->lookupChangeFull(pos);
356 for (; end != par->size(); ++end) {
357 Change change = par->lookupChangeFull(end);
358 if (change != orig_change) {
359 // slight UI optimisation: for replacements, we get
360 // text like : _old_new. Consider that as one change.
361 if (!(orig_change.type == Change::DELETED &&
362 change.type == Change::INSERTED))
369 // make sure we end up at the end of the text,
370 // not the start point of the last search
371 text->setCursor(prev_par, prev_par->size());
377 SearchResult findNextChange(BufferView * bv, LyXText * text, pos_type & length)
379 if (text->selection.set())
380 text->cursor = text->selection.end;
382 bv->toggleSelection();
383 text->clearSelection();
385 return nextChange(bv, text, length);
389 bool findNextChange(BufferView * bv)
391 if (!bv->available())
395 bv->update(bv->getLyXText(), BufferView::SELECT);
399 if (bv->theLockingInset()) {
400 bool found = bv->theLockingInset()->nextChange(bv, length);
402 // We found the stuff inside the inset so we don't have to
403 // do anything as the inset did all the update for us!
407 // We now are in the main text but if we did a forward
408 // search we have to put the cursor behind the inset.
409 bv->text->cursorRight(true);
411 // If we arrive here we are in the main text again so we
412 // just start searching from the root LyXText at the position
414 LyXText * text = bv->text;
416 if (text->selection.set())
417 text->cursor = text->selection.end;
419 bv->toggleSelection();
420 text->clearSelection();
422 SearchResult result = nextChange(bv, text, length);
424 lyxerr << "Result is " << result << endl;
428 // If we found the cursor inside an inset we will get back
429 // SR_FOUND_NOUPDATE and we don't have to do anything as the
430 // inset did it already.
431 if (result == SR_FOUND) {
432 bv->unlockInset(bv->theLockingInset());
433 bv->update(text, BufferView::SELECT);
434 text->setSelectionRange(length);
435 bv->toggleSelection(false);
436 bv->update(text, BufferView::SELECT);
437 } else if (result == SR_NOT_FOUND) {
438 bv->unlockInset(bv->theLockingInset());
439 bv->update(text, BufferView::SELECT);
448 } // end lyxfind namespace