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) {
180 bv->toggleSelection();
181 bv->getLyXText()->clearSelection();
182 bv->update(bv->getLyXText(), BufferView::SELECT|BufferView::FITCUR);
192 // returns true if the specified string is at the specified position
193 bool IsStringInText(Paragraph * par, pos_type pos,
194 string const & str, bool const & cs,
200 string::size_type size = str.length();
202 while (((pos + i) < par->size())
203 && (string::size_type(i) < size)
204 && (cs ? (str[i] == par->getChar(pos + i))
205 : (uppercase(str[i]) == uppercase(par->getChar(pos + i)))))
209 if (size == string::size_type(i)) {
210 // if necessary, check whether string matches word
213 if ((pos <= 0 || !IsLetterCharOrDigit(par->getChar(pos - 1)))
214 && (pos + pos_type(size) >= par->size()
215 || !IsLetterCharOrDigit(par->getChar(pos + size)))) {
223 // if the string can be found: return true and set the cursor to
224 // the new position, cs = casesensitive, mw = matchword
225 SearchResult SearchForward(BufferView * bv, LyXText * text, string const & str,
226 bool const & cs, bool const & mw)
228 Paragraph * par = text->cursor.par();
229 pos_type pos = text->cursor.pos();
230 Paragraph * prev_par = par;
231 UpdatableInset * inset;
233 while (par && !IsStringInText(par, pos, str, cs, mw)) {
234 if (par->isInset(pos) &&
235 (inset = (UpdatableInset *)par->getInset(pos)) &&
236 (inset->isTextInset()))
239 text->setCursor(bv, par, pos);
241 if (inset->searchForward(bv, str, cs, mw))
242 return SR_FOUND_NOUPDATE;
243 text = bv->getLyXText();
248 if (pos >= par->size()) {
256 text->setCursor(bv, par, pos);
259 // make sure we end up at the end of the text,
260 // not the start point of the last search
261 text->setCursor(bv, prev_par, prev_par->size());
268 // if the string can be found: return true and set the cursor to
269 // the new position, cs = casesensitive, mw = matchword
270 SearchResult SearchBackward(BufferView * bv, LyXText * text,
272 bool const & cs, bool const & mw)
274 Paragraph * par = text->cursor.par();
275 pos_type pos = text->cursor.pos();
276 Paragraph * prev_par = par;
283 // We skip empty paragraphs (Asger)
285 par = par->previous();
287 pos = par->size() - 1;
288 } while (par && pos < 0);
290 UpdatableInset * inset;
291 if (par && par->isInset(pos) &&
292 (inset = (UpdatableInset *)par->getInset(pos)) &&
293 (inset->isTextInset()))
296 text->setCursor(bv, par, pos);
297 inset->edit(bv, false);
298 if (inset->searchBackward(bv, str, cs, mw))
299 return SR_FOUND_NOUPDATE;
300 text = bv->getLyXText();
302 } while (par && !IsStringInText(par, pos, str, cs, mw));
305 text->setCursor(bv, par, pos);
308 // go to the last part of the unsuccessful search
309 text->setCursor(bv, prev_par, 0);