9 #include "frontends/LyXView.h"
10 #include "frontends/Alert.h"
11 #include "support/textutils.h"
12 #include "support/lstrings.h"
13 #include "BufferView.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();
89 bv->update(text, BufferView::SELECT|BufferView::FITCUR);
90 bv->toggleSelection(false);
91 text->replaceSelectionWithString(bv, replacestr);
92 text->setSelectionOverString(bv, replacestr);
93 bv->update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
96 found = LyXFind(bv, searchstr, fw, casesens, matchwrd);
97 } while (!once && replaceall && found);
102 return replace_count;
105 bool LyXFind(BufferView * bv,
106 string const & searchstr, bool forward,
107 bool casesens, bool matchwrd)
109 if (!bv->available() || searchstr.empty())
113 bv->update(bv->getLyXText(), BufferView::SELECT|BufferView::FITCUR);
115 if (bv->theLockingInset()) {
116 bool found = forward ?
117 bv->theLockingInset()->searchForward(bv, searchstr, casesens, matchwrd) :
118 bv->theLockingInset()->searchBackward(bv, searchstr, casesens, matchwrd);
119 // We found the stuff inside the inset so we don't have to
120 // do anything as the inset did all the update for us!
123 // We now are in the main text but if we did a forward
124 // search we have to put the cursor behind the inset.
126 bv->text->cursorRight(bv, true);
129 // If we arrive here we are in the main text again so we
130 // just start searching from the root LyXText at the position
132 LyXText * text = bv->text;
134 if (text->selection.set())
135 text->cursor = forward ?
136 text->selection.end : text->selection.start;
138 bv->toggleSelection();
139 text->clearSelection();
141 SearchResult result = forward ?
142 SearchForward(bv, text, searchstr, casesens, matchwrd) :
143 SearchBackward(bv, text, searchstr, casesens, matchwrd);
146 // If we found the cursor inside an inset we will get back
147 // SR_FOUND_NOUPDATE and we don't have to do anything as the
148 // inset did it already.
149 if (result == SR_FOUND) {
150 bv->unlockInset(bv->theLockingInset());
151 bv->update(text, BufferView::SELECT|BufferView::FITCUR);
152 text->setSelectionOverString(bv, searchstr);
153 bv->toggleSelection(false);
154 bv->update(text, BufferView::SELECT|BufferView::FITCUR);
155 } else if (result == SR_NOT_FOUND) {
156 bv->unlockInset(bv->theLockingInset());
157 bv->update(text, BufferView::SELECT|BufferView::FITCUR);
160 if (result != SR_FOUND_NOUPDATE && bv->focus())
166 SearchResult LyXFind(BufferView * bv, LyXText * text,
167 string const & searchstr, bool forward,
168 bool casesens, bool matchwrd)
170 if (text->selection.set())
171 text->cursor = forward ?
172 text->selection.end : text->selection.start;
174 bv->toggleSelection();
175 text->clearSelection();
177 SearchResult result = forward ?
178 SearchForward(bv, text, searchstr, casesens, matchwrd) :
179 SearchBackward(bv, text, searchstr, casesens, matchwrd);
185 // returns true if the specified string is at the specified position
186 bool IsStringInText(Paragraph * par, pos_type pos,
187 string const & str, bool const & cs,
193 string::size_type size = str.length();
195 while (((pos + i) < par->size())
196 && (string::size_type(i) < size)
197 && (cs ? (str[i] == par->getChar(pos + i))
198 : (uppercase(str[i]) == uppercase(par->getChar(pos + i)))))
202 if (size == string::size_type(i)) {
203 // if necessary, check whether string matches word
206 if ((pos <= 0 || !IsLetterCharOrDigit(par->getChar(pos - 1)))
207 && (pos + pos_type(size) >= par->size()
208 || !IsLetterCharOrDigit(par->getChar(pos + size)))) {
216 // if the string can be found: return true and set the cursor to
217 // the new position, cs = casesensitive, mw = matchword
218 SearchResult SearchForward(BufferView * bv, LyXText * text, string const & str,
219 bool const & cs, bool const & mw)
221 Paragraph * par = text->cursor.par();
222 pos_type pos = text->cursor.pos();
223 Paragraph * prev_par = par;
224 UpdatableInset * inset;
226 while (par && !IsStringInText(par, pos, str, cs, mw)) {
227 if (par->isInset(pos) &&
228 (inset = (UpdatableInset *)par->getInset(pos)) &&
229 (inset->isTextInset()))
233 text->setCursor(bv, par, pos);
236 if (inset->searchForward(bv, str, cs, mw))
237 return SR_FOUND_NOUPDATE;
242 if (pos >= par->size()) {
250 text->setCursor(bv, par, pos);
253 // make sure we end up at the end of the text,
254 // not the start point of the last search
255 text->setCursor(bv, prev_par, prev_par->size());
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();
270 Paragraph * prev_par = par;
277 // We skip empty paragraphs (Asger)
279 par = par->previous();
281 pos = par->size() - 1;
282 } while (par && pos < 0);
284 UpdatableInset * inset;
285 if (par && par->isInset(pos) &&
286 (inset = (UpdatableInset *)par->getInset(pos)) &&
287 (inset->isTextInset()))
291 text->setCursor(bv, par, pos);
292 inset->edit(bv, false);
294 if (inset->searchBackward(bv, str, cs, mw))
295 return SR_FOUND_NOUPDATE;
297 } while (par && !IsStringInText(par, pos, str, cs, mw));
300 text->setCursor(bv, par, pos);
303 // go to the last part of the unsuccessful search
304 text->setCursor(bv, prev_par, 0);
309 } // end lyxfind namespace