10 #include "frontends/Alert.h"
11 #include "support/textutils.h"
12 #include "support/lstrings.h"
13 #include "BufferView.h"
34 /// returns true if the specified string is at the specified position
35 bool IsStringInText(Paragraph * par, pos_type pos,
36 string const & str, bool const & = true,
37 bool const & = false);
39 /// if the string is found: return true and set the cursor to the new position
40 SearchResult SearchForward(BufferView *, LyXText * text, string const & str,
41 bool const & = true, bool const & = false);
43 SearchResult SearchBackward(BufferView *, LyXText * text, string const & str,
44 bool const & = true, bool const & = false);
47 int LyXReplace(BufferView * bv,
48 string const & searchstr, string const & replacestr,
49 bool forward, bool casesens, bool matchwrd, bool replaceall,
52 if (!bv->available() || bv->buffer()->isReadonly())
55 // CutSelection cannot cut a single space, so we have to stop
56 // in order to avoid endless loop :-(
57 if (searchstr.length() == 0
58 || (searchstr.length() == 1 && searchstr[0] == ' '))
60 Alert::alert(_("Sorry!"), _("You cannot replace a single space, "
61 "nor an empty character."));
65 LyXText * text = bv->getLyXText();
67 // now we can start searching for the first
68 // start at top if replaceall
71 text->clearSelection();
72 if (text->inset_owner) {
73 bv->unlockInset(bv->theLockingInset());
77 // override search direction because we search top to bottom
81 // if nothing selected or selection does not equal search string
82 // search and select next occurance and return if no replaceall
87 str2 = text->selectionAsString(bv->buffer(), false);
89 str1 = lowercase(searchstr);
90 str2 = lowercase(text->selectionAsString(bv->buffer(), false));
93 if (!LyXFind(bv, searchstr, fw, false, casesens, matchwrd) ||
101 int replace_count = 0;
104 bv->update(bv->getLyXText(), BufferView::SELECT|BufferView::FITCUR);
105 bv->toggleSelection(false);
106 bv->getLyXText()->replaceSelectionWithString(bv, replacestr);
107 bv->getLyXText()->setSelectionOverString(bv, replacestr);
108 bv->update(bv->getLyXText(), BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
111 found = LyXFind(bv, searchstr, fw, false, casesens, matchwrd);
112 } while (!once && replaceall && found);
117 return replace_count;
120 bool LyXFind(BufferView * bv,
121 string const & searchstr, bool forward,
122 bool frominset, bool casesens, bool matchwrd)
124 if (!bv->available() || searchstr.empty())
127 LyXText * text = bv->getLyXText();
130 bv->update(text, BufferView::SELECT|BufferView::FITCUR);
132 if (text->selection.set())
133 text->cursor = forward ?
134 text->selection.end : text->selection.start;
136 SearchResult result = SR_NOT_FOUND;
138 if (!frominset && bv->theLockingInset()) {
139 bool found = forward ?
140 bv->theLockingInset()->searchForward(bv, searchstr, casesens, matchwrd) :
141 bv->theLockingInset()->searchBackward(bv, searchstr, casesens, matchwrd);
143 result = SR_FOUND_NOUPDATE;
145 text = bv->getLyXText();
146 Paragraph * par = text->cursor.par();
147 pos_type pos = text->cursor.pos();
149 if (pos < par->size() - 1)
156 text->setCursor(bv, par, pos);
160 SearchForward(bv, text, searchstr, casesens, matchwrd) :
161 SearchBackward(bv, text, searchstr, casesens, matchwrd);
166 SearchForward(bv, text, searchstr, casesens, matchwrd) :
167 SearchBackward(bv, text, searchstr, casesens, matchwrd);
171 if (result == SR_FOUND) {
172 // the actual text pointer could have changed!
173 bv->update(bv->getLyXText(), BufferView::SELECT|BufferView::FITCUR);
174 bv->toggleSelection();
175 bv->getLyXText()->clearSelection();
176 bv->getLyXText()->setSelectionOverString(bv, searchstr);
177 bv->toggleSelection(false);
178 bv->update(bv->getLyXText(), BufferView::SELECT|BufferView::FITCUR);
179 } else if (result == SR_NOT_FOUND)
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()))
236 text->setCursor(bv, par, pos);
238 if (inset->searchForward(bv, str, cs, mw))
239 return SR_FOUND_NOUPDATE;
240 text = bv->getLyXText();
245 if (pos >= par->size()) {
253 text->setCursor(bv, par, pos);
256 // make sure we end up at the end of the text,
257 // not the start point of the last search
258 text->setCursor(bv, prev_par, prev_par->size());
265 // if the string can be found: return true and set the cursor to
266 // the new position, cs = casesensitive, mw = matchword
267 SearchResult SearchBackward(BufferView * bv, LyXText * text,
269 bool const & cs, bool const & mw)
271 Paragraph * par = text->cursor.par();
272 pos_type pos = text->cursor.pos();
273 Paragraph * prev_par = par;
280 // We skip empty paragraphs (Asger)
282 par = par->previous();
284 pos = par->size() - 1;
285 } while (par && pos < 0);
287 UpdatableInset * inset;
288 if (par && par->isInset(pos) &&
289 (inset = (UpdatableInset *)par->getInset(pos)) &&
290 (inset->isTextInset()))
293 text->setCursor(bv, par, pos);
294 inset->edit(bv, false);
295 if (inset->searchBackward(bv, str, cs, mw))
296 return SR_FOUND_NOUPDATE;
297 text = bv->getLyXText();
299 } while (par && !IsStringInText(par, pos, str, cs, mw));
302 text->setCursor(bv, par, pos);
305 // go to the last part of the unsuccessful search
306 text->setCursor(bv, prev_par, 0);