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 Alert::alert(_("Sorry!"), _("You cannot replace a single space, "
48 "nor an empty character."));
52 // now we can start searching for the first
53 // start at top if replaceall
54 LyXText * text = bv->getLyXText();
57 text->clearSelection();
58 bv->unlockInset(bv->theLockingInset());
61 // override search direction because we search top to bottom
65 // if nothing selected or selection does not equal search string
66 // search and select next occurance and return if no replaceall
71 str2 = text->selectionAsString(bv->buffer(), false);
73 str1 = lowercase(searchstr);
74 str2 = lowercase(text->selectionAsString(bv->buffer(), false));
77 if (!LyXFind(bv, searchstr, fw, casesens, matchwrd) ||
85 int replace_count = 0;
87 text = bv->getLyXText();
88 // We have to do this check only because mathed insets don't
89 // return their own LyXText but the LyXText of it's parent!
90 if (!bv->theLockingInset() ||
91 ((text != bv->text) &&
92 (text->inset_owner == text->inset_owner->getLockingInset())))
95 bv->update(text, BufferView::SELECT|BufferView::FITCUR);
96 bv->toggleSelection(false);
97 text->replaceSelectionWithString(replacestr);
98 text->setSelectionRange(replacestr.length());
99 bv->update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
103 found = LyXFind(bv, searchstr, fw, casesens, matchwrd);
104 } while (!once && replaceall && found);
106 return replace_count;
110 bool LyXFind(BufferView * bv,
111 string const & searchstr, bool forward,
112 bool casesens, bool matchwrd)
114 if (!bv->available() || searchstr.empty())
118 bv->update(bv->getLyXText(), BufferView::SELECT|BufferView::FITCUR);
120 if (bv->theLockingInset()) {
121 bool found = forward ?
122 bv->theLockingInset()->searchForward(bv, searchstr, casesens, matchwrd) :
123 bv->theLockingInset()->searchBackward(bv, searchstr, casesens, matchwrd);
124 // We found the stuff inside the inset so we don't have to
125 // do anything as the inset did all the update for us!
128 // We now are in the main text but if we did a forward
129 // search we have to put the cursor behind the inset.
131 bv->text->cursorRight(true);
134 // If we arrive here we are in the main text again so we
135 // just start searching from the root LyXText at the position
137 LyXText * text = bv->text;
139 if (text->selection.set())
140 text->cursor = forward ?
141 text->selection.end : text->selection.start;
143 bv->toggleSelection();
144 text->clearSelection();
146 SearchResult result = forward ?
147 SearchForward(bv, text, searchstr, casesens, matchwrd) :
148 SearchBackward(bv, text, searchstr, casesens, matchwrd);
151 // If we found the cursor inside an inset we will get back
152 // SR_FOUND_NOUPDATE and we don't have to do anything as the
153 // inset did it already.
154 if (result == SR_FOUND) {
155 bv->unlockInset(bv->theLockingInset());
156 bv->update(text, BufferView::SELECT|BufferView::FITCUR);
157 text->setSelectionRange(searchstr.length());
158 bv->toggleSelection(false);
159 bv->update(text, BufferView::SELECT|BufferView::FITCUR);
160 } else if (result == SR_NOT_FOUND) {
161 bv->unlockInset(bv->theLockingInset());
162 bv->update(text, BufferView::SELECT|BufferView::FITCUR);
170 SearchResult LyXFind(BufferView * bv, LyXText * text,
171 string const & searchstr, bool forward,
172 bool casesens, bool matchwrd)
174 if (text->selection.set())
175 text->cursor = forward ?
176 text->selection.end : text->selection.start;
178 bv->toggleSelection();
179 text->clearSelection();
181 SearchResult result = forward ?
182 SearchForward(bv, text, searchstr, casesens, matchwrd) :
183 SearchBackward(bv, text, searchstr, casesens, matchwrd);
189 // returns true if the specified string is at the specified position
190 bool IsStringInText(Paragraph * par, pos_type pos,
191 string const & str, bool const & cs,
197 string::size_type size = str.length();
199 while (((pos + i) < par->size())
200 && (string::size_type(i) < size)
201 && (cs ? (str[i] == par->getChar(pos + i))
202 : (uppercase(str[i]) == uppercase(par->getChar(pos + i)))))
206 if (size == string::size_type(i)) {
207 // if necessary, check whether string matches word
210 if ((pos <= 0 || !IsLetterCharOrDigit(par->getChar(pos - 1)))
211 && (pos + pos_type(size) >= par->size()
212 || !IsLetterCharOrDigit(par->getChar(pos + size)))) {
220 // if the string can be found: return true and set the cursor to
221 // the new position, cs = casesensitive, mw = matchword
222 SearchResult SearchForward(BufferView * bv, LyXText * text, string const & str,
223 bool const & cs, bool const & mw)
225 Paragraph * par = text->cursor.par();
226 pos_type pos = text->cursor.pos();
227 Paragraph * prev_par = par;
228 UpdatableInset * inset;
230 while (par && !IsStringInText(par, pos, str, cs, mw)) {
231 if (par->isInset(pos) &&
232 (inset = (UpdatableInset *)par->getInset(pos)) &&
233 (inset->isTextInset()))
237 text->setCursor(bv, par, pos);
240 if (inset->searchForward(bv, str, cs, mw))
241 return SR_FOUND_NOUPDATE;
246 if (pos >= par->size()) {
254 text->setCursor(par, pos);
257 // make sure we end up at the end of the text,
258 // not the start point of the last search
259 text->setCursor(prev_par, prev_par->size());
266 // if the string can be found: return true and set the cursor to
267 // the new position, cs = casesensitive, mw = matchword
268 SearchResult SearchBackward(BufferView * bv, LyXText * text,
270 bool const & cs, bool const & mw)
272 Paragraph * par = text->cursor.par();
273 pos_type pos = text->cursor.pos();
274 Paragraph * prev_par = par;
281 // We skip empty paragraphs (Asger)
283 par = par->previous();
285 pos = par->size() - 1;
286 } while (par && pos < 0);
288 UpdatableInset * inset;
289 if (par && par->isInset(pos) &&
290 (inset = (UpdatableInset *)par->getInset(pos)) &&
291 (inset->isTextInset()))
295 text->setCursor(bv, par, pos);
296 inset->edit(bv, false);
298 if (inset->searchBackward(bv, str, cs, mw))
299 return SR_FOUND_NOUPDATE;
301 } while (par && !IsStringInText(par, pos, str, cs, mw));
304 text->setCursor(par, pos);
307 // go to the last part of the unsuccessful search
308 text->setCursor(prev_par, 0);
314 SearchResult nextChange(BufferView * bv, LyXText * text, pos_type & length)
316 Paragraph * par = text->cursor.par();
317 pos_type pos = text->cursor.pos();
318 Paragraph * prev_par = par;
319 UpdatableInset * inset;
322 if ((!par->size() || pos != par->size())
323 && par->lookupChange(pos) != Change::UNCHANGED)
326 if (par->isInset(pos) &&
327 (inset = (UpdatableInset *)par->getInset(pos)) &&
328 (inset->isTextInset())) {
329 if (inset->nextChange(bv, length))
330 return SR_FOUND_NOUPDATE;
335 if (pos >= par->size()) {
343 text->setCursor(par, pos);
344 Change orig_change = par->lookupChangeFull(pos);
347 for (; end != par->size(); ++end) {
348 Change change = par->lookupChangeFull(end);
349 if (change != orig_change) {
350 // slight UI optimisation: for replacements, we get
351 // text like : _old_new. Consider that as one change.
352 if (!(orig_change.type == Change::DELETED &&
353 change.type == Change::INSERTED))
360 // make sure we end up at the end of the text,
361 // not the start point of the last search
362 text->setCursor(prev_par, prev_par->size());
368 SearchResult findNextChange(BufferView * bv, LyXText * text, pos_type & length)
370 if (text->selection.set())
371 text->cursor = text->selection.end;
373 bv->toggleSelection();
374 text->clearSelection();
376 return nextChange(bv, text, length);
380 bool findNextChange(BufferView * bv)
382 if (!bv->available())
386 bv->update(bv->getLyXText(), BufferView::SELECT | BufferView::FITCUR);
390 if (bv->theLockingInset()) {
391 bool found = bv->theLockingInset()->nextChange(bv, length);
393 // We found the stuff inside the inset so we don't have to
394 // do anything as the inset did all the update for us!
398 // We now are in the main text but if we did a forward
399 // search we have to put the cursor behind the inset.
400 bv->text->cursorRight(true);
402 // If we arrive here we are in the main text again so we
403 // just start searching from the root LyXText at the position
405 LyXText * text = bv->text;
407 if (text->selection.set())
408 text->cursor = text->selection.end;
410 bv->toggleSelection();
411 text->clearSelection();
413 SearchResult result = nextChange(bv, text, length);
415 lyxerr << "Result is " << result << endl;
419 // If we found the cursor inside an inset we will get back
420 // SR_FOUND_NOUPDATE and we don't have to do anything as the
421 // inset did it already.
422 if (result == SR_FOUND) {
423 bv->unlockInset(bv->theLockingInset());
424 bv->update(text, BufferView::SELECT|BufferView::FITCUR);
425 text->setSelectionRange(length);
426 bv->toggleSelection(false);
427 bv->update(text, BufferView::SELECT|BufferView::FITCUR);
428 } else if (result == SR_NOT_FOUND) {
429 bv->unlockInset(bv->theLockingInset());
430 bv->update(text, BufferView::SELECT|BufferView::FITCUR);
437 } // end lyxfind namespace