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;
147 if (text->selection.set())
148 text->cursor = forward ?
149 text->selection.end : text->selection.start;
151 bv->toggleSelection();
152 text->clearSelection();
154 SearchResult result = forward ?
155 SearchForward(bv, text, searchstr, casesens, matchwrd) :
156 SearchBackward(bv, text, searchstr, casesens, matchwrd);
159 // If we found the cursor inside an inset we will get back
160 // SR_FOUND_NOUPDATE and we don't have to do anything as the
161 // inset did it already.
162 if (result == SR_FOUND) {
163 bv->unlockInset(bv->theLockingInset());
164 bv->update(text, BufferView::SELECT);
165 text->setSelectionRange(searchstr.length());
166 bv->toggleSelection(false);
167 bv->update(text, BufferView::SELECT);
168 } else if (result == SR_NOT_FOUND) {
169 bv->unlockInset(bv->theLockingInset());
170 bv->update(text, BufferView::SELECT);
180 SearchResult LyXFind(BufferView * bv, LyXText * text,
181 string const & searchstr, bool forward,
182 bool casesens, bool matchwrd)
184 if (text->selection.set())
185 text->cursor = forward ?
186 text->selection.end : text->selection.start;
188 bv->toggleSelection();
189 text->clearSelection();
191 SearchResult result = forward ?
192 SearchForward(bv, text, searchstr, casesens, matchwrd) :
193 SearchBackward(bv, text, searchstr, casesens, matchwrd);
199 // returns true if the specified string is at the specified position
200 bool IsStringInText(Paragraph * par, pos_type pos,
201 string const & str, bool const & cs,
207 string::size_type size = str.length();
209 while (((pos + i) < par->size())
210 && (string::size_type(i) < size)
211 && (cs ? (str[i] == par->getChar(pos + i))
212 : (uppercase(str[i]) == uppercase(par->getChar(pos + i)))))
216 if (size == string::size_type(i)) {
217 // if necessary, check whether string matches word
220 if ((pos <= 0 || !IsLetterCharOrDigit(par->getChar(pos - 1)))
221 && (pos + pos_type(size) >= par->size()
222 || !IsLetterCharOrDigit(par->getChar(pos + size)))) {
230 // if the string can be found: return true and set the cursor to
231 // the new position, cs = casesensitive, mw = matchword
232 SearchResult SearchForward(BufferView * bv, LyXText * text, string const & str,
233 bool const & cs, bool const & mw)
235 Paragraph * par = &*text->cursor.par();
236 pos_type pos = text->cursor.pos();
237 UpdatableInset * inset;
239 while (par && !IsStringInText(par, pos, str, cs, mw)) {
240 if (pos < par->size()
242 && (inset = (UpdatableInset *)par->getInset(pos))
243 && inset->isTextInset()
244 && inset->searchForward(bv, str, cs, mw))
245 return SR_FOUND_NOUPDATE;
247 if (++pos >= par->size()) {
254 text->setCursor(par, pos);
262 // if the string can be found: return true and set the cursor to
263 // the new position, cs = casesensitive, mw = matchword
264 SearchResult SearchBackward(BufferView * bv, LyXText * text,
266 bool const & cs, bool const & mw)
268 Paragraph * par = &*text->cursor.par();
269 pos_type pos = text->cursor.pos();
275 // We skip empty paragraphs (Asger)
277 par = par->previous();
279 pos = par->size() - 1;
280 } while (par && pos < 0);
282 UpdatableInset * inset;
283 if (par && par->isInset(pos)
284 && (inset = (UpdatableInset *)par->getInset(pos))
285 && inset->isTextInset()
286 && inset->searchBackward(bv, str, cs, mw))
287 return SR_FOUND_NOUPDATE;
288 } while (par && !IsStringInText(par, pos, str, cs, mw));
291 text->setCursor(par, pos);
298 SearchResult nextChange(BufferView * bv, LyXText * text, pos_type & length)
300 Paragraph * par = &*text->cursor.par();
301 pos_type pos = text->cursor.pos();
302 Paragraph * prev_par = par;
303 UpdatableInset * inset;
306 if ((!par->size() || pos != par->size())
307 && par->lookupChange(pos) != Change::UNCHANGED)
310 if (par->isInset(pos) &&
311 (inset = (UpdatableInset *)par->getInset(pos)) &&
312 (inset->isTextInset())) {
313 if (inset->nextChange(bv, length))
314 return SR_FOUND_NOUPDATE;
319 if (pos >= par->size()) {
327 text->setCursor(par, pos);
328 Change orig_change = par->lookupChangeFull(pos);
331 for (; end != par->size(); ++end) {
332 Change change = par->lookupChangeFull(end);
333 if (change != orig_change) {
334 // slight UI optimisation: for replacements, we get
335 // text like : _old_new. Consider that as one change.
336 if (!(orig_change.type == Change::DELETED &&
337 change.type == Change::INSERTED))
344 // make sure we end up at the end of the text,
345 // not the start point of the last search
346 text->setCursor(prev_par, prev_par->size());
352 SearchResult findNextChange(BufferView * bv, LyXText * text, pos_type & length)
354 if (text->selection.set())
355 text->cursor = text->selection.end;
357 bv->toggleSelection();
358 text->clearSelection();
360 return nextChange(bv, text, length);
364 bool findNextChange(BufferView * bv)
366 if (!bv->available())
370 bv->update(bv->getLyXText(), BufferView::SELECT);
374 if (bv->theLockingInset()) {
375 bool found = bv->theLockingInset()->nextChange(bv, length);
377 // We found the stuff inside the inset so we don't have to
378 // do anything as the inset did all the update for us!
382 // We now are in the main text but if we did a forward
383 // search we have to put the cursor behind the inset.
384 bv->text->cursorRight(true);
386 // If we arrive here we are in the main text again so we
387 // just start searching from the root LyXText at the position
389 LyXText * text = bv->text;
391 if (text->selection.set())
392 text->cursor = text->selection.end;
394 bv->toggleSelection();
395 text->clearSelection();
397 SearchResult result = nextChange(bv, text, length);
399 lyxerr << "Result is " << result << endl;
403 // If we found the cursor inside an inset we will get back
404 // SR_FOUND_NOUPDATE and we don't have to do anything as the
405 // inset did it already.
406 if (result == SR_FOUND) {
407 bv->unlockInset(bv->theLockingInset());
408 bv->update(text, BufferView::SELECT);
409 text->setSelectionRange(length);
410 bv->toggleSelection(false);
411 bv->update(text, BufferView::SELECT);
412 } else if (result == SR_NOT_FOUND) {
413 bv->unlockInset(bv->theLockingInset());
414 bv->update(text, BufferView::SELECT);
423 } // end lyxfind namespace