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"
17 using namespace lyx::support;
27 // returns true if the specified string is at the specified position
28 bool isStringInText(Paragraph const & par, pos_type pos,
29 string const & str, bool const & cs,
32 string::size_type size = str.length();
34 pos_type parsize = par.size();
35 while (((pos + i) < parsize)
36 && (string::size_type(i) < size)
37 && (cs ? (str[i] == par.getChar(pos + i))
38 : (uppercase(str[i]) == uppercase(par.getChar(pos + i))))) {
42 if (size == string::size_type(i)) {
43 // if necessary, check whether string matches word
46 if ((pos <= 0 || !IsLetterCharOrDigit(par.getChar(pos - 1)))
47 && (pos + pos_type(size) >= parsize
48 || !IsLetterCharOrDigit(par.getChar(pos + size)))) {
56 // if the string can be found: return true and set the cursor to
57 // the new position, cs = casesensitive, mw = matchword
58 SearchResult searchForward(BufferView * bv, LyXText * text, string const & str,
59 bool const & cs, bool const & mw)
61 ParagraphList::iterator pit = text->cursor.par();
62 ParagraphList::iterator pend = text->ownerParagraphs().end();
63 pos_type pos = text->cursor.pos();
64 UpdatableInset * inset;
66 while (pit != pend && !isStringInText(*pit, pos, str, cs, mw)) {
69 && (inset = (UpdatableInset *)pit->getInset(pos))
70 && inset->isTextInset()
71 && inset->searchForward(bv, str, cs, mw))
72 return SR_FOUND_NOUPDATE;
74 if (++pos >= pit->size()) {
81 text->setCursor(pit, pos);
89 // if the string can be found: return true and set the cursor to
90 // the new position, cs = casesensitive, mw = matchword
91 SearchResult searchBackward(BufferView * bv, LyXText * text,
93 bool const & cs, bool const & mw)
95 ParagraphList::iterator pit = text->cursor.par();
96 ParagraphList::iterator pbegin = text->ownerParagraphs().begin();
97 pos_type pos = text->cursor.pos();
99 // skip past a match at the current cursor pos
102 } else if (pit != pbegin) {
110 if (pos < pit->size()) {
111 if (pit->isInset(pos) && pit->getInset(pos)->isTextInset()) {
112 UpdatableInset * inset = (UpdatableInset *)pit->getInset(pos);
113 if (inset->searchBackward(bv, str, cs, mw))
114 return SR_FOUND_NOUPDATE;
117 if (isStringInText(*pit, pos, str, cs, mw)) {
118 text->setCursor(pit, pos);
123 if (pos == 0 && pit == pbegin)
128 } else if (pit != pbegin) {
140 int replace(BufferView * bv,
141 string const & searchstr, string const & replacestr,
142 bool forward, bool casesens, bool matchwrd, bool replaceall,
145 if (!bv->available() || bv->buffer()->isReadonly())
148 // CutSelection cannot cut a single space, so we have to stop
149 // in order to avoid endless loop :-(
150 if (searchstr.length() == 0
151 || (searchstr.length() == 1 && searchstr[0] == ' ')) {
153 #warning BLECH. If we have an LFUN for replace, we can sort of fix this bogosity
155 Alert::error(_("Cannot replace"),
156 _("You cannot replace a single space or "
157 "an empty character."));
161 // now we can start searching for the first
162 // start at top if replaceall
163 LyXText * text = bv->getLyXText();
166 text->clearSelection();
167 bv->unlockInset(bv->theLockingInset());
170 // override search direction because we search top to bottom
174 // if nothing selected or selection does not equal search string
175 // search and select next occurance and return if no replaceall
180 str2 = text->selectionAsString(bv->buffer(), false);
182 str1 = lowercase(searchstr);
183 str2 = lowercase(text->selectionAsString(bv->buffer(), false));
186 if (!find(bv, searchstr, fw, casesens, matchwrd) ||
193 int replace_count = 0;
195 text = bv->getLyXText();
196 // We have to do this check only because mathed insets don't
197 // return their own LyXText but the LyXText of it's parent!
198 if (!bv->theLockingInset() ||
199 ((text != bv->text) &&
200 (text->inset_owner == text->inset_owner->getLockingInset()))) {
201 bv->toggleSelection(false);
202 text->replaceSelectionWithString(replacestr);
203 text->setSelectionRange(replacestr.length());
207 found = find(bv, searchstr, fw, casesens, matchwrd);
208 } while (!once && replaceall && found);
210 // FIXME: should be called via an LFUN
211 bv->buffer()->markDirty();
215 return replace_count;
219 bool find(BufferView * bv,
220 string const & searchstr, bool forward,
221 bool casesens, bool matchwrd)
223 if (!bv->available() || searchstr.empty())
226 if (bv->theLockingInset()) {
227 bool found = forward ?
228 bv->theLockingInset()->searchForward(bv, searchstr, casesens, matchwrd) :
229 bv->theLockingInset()->searchBackward(bv, searchstr, casesens, matchwrd);
230 // We found the stuff inside the inset so we don't have to
231 // do anything as the inset did all the update for us!
234 // We now are in the main text but if we did a forward
235 // search we have to put the cursor behind the inset.
237 bv->text->cursorRight(true);
240 // If we arrive here we are in the main text again so we
241 // just start searching from the root LyXText at the position
243 LyXText * text = bv->text;
246 if (text->selection.set())
247 text->cursor = forward ?
248 text->selection.end : text->selection.start;
250 bv->toggleSelection();
251 text->clearSelection();
253 SearchResult result = forward ?
254 searchForward(bv, text, searchstr, casesens, matchwrd) :
255 searchBackward(bv, text, searchstr, casesens, matchwrd);
258 // If we found the cursor inside an inset we will get back
259 // SR_FOUND_NOUPDATE and we don't have to do anything as the
260 // inset did it already.
261 if (result == SR_FOUND) {
262 bv->unlockInset(bv->theLockingInset());
263 text->setSelectionRange(searchstr.length());
264 bv->toggleSelection(false);
265 } else if (result == SR_NOT_FOUND) {
266 bv->unlockInset(bv->theLockingInset());
276 SearchResult find(BufferView * bv, LyXText * text,
277 string const & searchstr, bool forward,
278 bool casesens, bool matchwrd)
280 if (text->selection.set())
281 text->cursor = forward ?
282 text->selection.end : text->selection.start;
284 bv->toggleSelection();
285 text->clearSelection();
287 SearchResult result = forward ?
288 searchForward(bv, text, searchstr, casesens, matchwrd) :
289 searchBackward(bv, text, searchstr, casesens, matchwrd);
297 SearchResult nextChange(BufferView * bv, LyXText * text, pos_type & length)
299 ParagraphList::iterator pit = text->cursor.par();
300 ParagraphList::iterator pend = text->ownerParagraphs().end();
301 pos_type pos = text->cursor.pos();
303 while (pit != pend) {
304 pos_type parsize = pit->size();
307 if ((!parsize || pos != parsize)
308 && pit->lookupChange(pos) != Change::UNCHANGED)
311 if (pit->isInset(pos) && pit->getInset(pos)->isTextInset()) {
312 UpdatableInset * inset = (UpdatableInset *)pit->getInset(pos);
313 if (inset->nextChange(bv, length))
314 return SR_FOUND_NOUPDATE;
320 if (pos >= parsize) {
329 text->setCursor(pit, pos);
330 Change orig_change = pit->lookupChangeFull(pos);
331 pos_type parsize = pit->size();
334 for (; end != parsize; ++end) {
335 Change change = pit->lookupChangeFull(end);
336 if (change != orig_change) {
337 // slight UI optimisation: for replacements, we get
338 // text like : _old_new. Consider that as one change.
339 if (!(orig_change.type == Change::DELETED &&
340 change.type == Change::INSERTED))
349 SearchResult findNextChange(BufferView * bv, LyXText * text, pos_type & length)
351 if (text->selection.set())
352 text->cursor = text->selection.end;
354 bv->toggleSelection();
355 text->clearSelection();
357 return nextChange(bv, text, length);
361 bool findNextChange(BufferView * bv)
363 if (!bv->available())
368 if (bv->theLockingInset()) {
369 bool found = bv->theLockingInset()->nextChange(bv, length);
371 // We found the stuff inside the inset so we don't have to
372 // do anything as the inset did all the update for us!
376 // We now are in the main text but if we did a forward
377 // search we have to put the cursor behind the inset.
378 bv->text->cursorRight(true);
380 // If we arrive here we are in the main text again so we
381 // just start searching from the root LyXText at the position
383 LyXText * text = bv->text;
385 if (text->selection.set())
386 text->cursor = text->selection.end;
388 bv->toggleSelection();
389 text->clearSelection();
391 SearchResult result = nextChange(bv, text, length);
395 // If we found the cursor inside an inset we will get back
396 // SR_FOUND_NOUPDATE and we don't have to do anything as the
397 // inset did it already.
398 if (result == SR_FOUND) {
399 bv->unlockInset(bv->theLockingInset());
400 text->setSelectionRange(length);
401 bv->toggleSelection(false);
402 } else if (result == SR_NOT_FOUND) {
403 bv->unlockInset(bv->theLockingInset());