10 #include "lyx_gui_misc.h"
11 #include "support/textutils.h"
12 #include "support/lstrings.h"
13 #include "BufferView.h"
30 /// returns true if the specified string is at the specified position
31 bool IsStringInText(Paragraph * par, Paragraph::size_type pos,
32 string const & str, bool const & = true,
33 bool const & = false);
35 /// if the string is found: return true and set the cursor to the new position
36 SearchResult SearchForward(BufferView *, LyXText * text, string const & str,
37 bool const & = true, bool const & = false);
39 SearchResult SearchBackward(BufferView *, LyXText * text, string const & str,
40 bool const & = true, bool const & = false);
43 int LyXReplace(BufferView * bv,
44 string const & searchstr, string const & replacestr,
45 bool forward, bool casesens, bool matchwrd, bool replaceall,
48 if (!bv->available() || bv->buffer()->isReadonly())
51 // CutSelection cannot cut a single space, so we have to stop
52 // in order to avoid endless loop :-(
53 if (searchstr.length() == 0
54 || (searchstr.length() == 1 && searchstr[0] == ' '))
56 WriteAlert(_("Sorry!"), _("You cannot replace a single space, "
57 "nor an empty character."));
61 LyXText * text = bv->getLyXText();
63 // now we can start searching for the first
64 // start at top if replaceall
67 text->clearSelection(bv);
68 if (text->inset_owner) {
69 bv->unlockInset(bv->theLockingInset());
73 // override search direction because we search top to bottom
77 // if nothing selected or selection does not equal search string
78 // search and select next occurance and return if no replaceall
79 if (searchstr!=text->selectionAsString(bv->buffer())) {
80 if (!LyXFind(bv, searchstr, fw, false, casesens, matchwrd) ||
88 int replace_count = 0;
91 bv->update(bv->getLyXText(), BufferView::SELECT|BufferView::FITCUR);
92 bv->toggleSelection(false);
93 bv->getLyXText()->replaceSelectionWithString(bv, replacestr);
94 bv->getLyXText()->setSelectionOverString(bv, replacestr);
95 bv->update(bv->getLyXText(), BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
98 found = LyXFind(bv, searchstr, fw, false, casesens, matchwrd);
99 } while (!once && replaceall && found);
104 return replace_count;
107 bool LyXFind(BufferView * bv,
108 string const & searchstr, bool forward,
109 bool frominset, bool casesens, bool matchwrd)
111 if (!bv->available() || searchstr.empty())
114 LyXText * text = bv->getLyXText();
117 bv->update(text, BufferView::SELECT|BufferView::FITCUR);
119 if (text->selection.set())
120 text->cursor = forward ?
121 text->selection.end : text->selection.start;
123 SearchResult result = SR_NOT_FOUND;
125 if (!frominset && bv->theLockingInset()) {
126 bool found = forward ?
127 bv->theLockingInset()->searchForward(bv, searchstr, casesens, matchwrd) :
128 bv->theLockingInset()->searchBackward(bv, searchstr, casesens, matchwrd);
130 result = SR_FOUND_NOUPDATE;
133 SearchForward(bv, text, searchstr, casesens, matchwrd) :
134 SearchBackward(bv, text, searchstr, casesens, matchwrd);
138 if (result == SR_FOUND) {
139 // the actual text pointer could have changed!
140 bv->update(bv->getLyXText(), BufferView::SELECT|BufferView::FITCUR);
141 bv->toggleSelection();
142 bv->getLyXText()->clearSelection(bv);
143 bv->getLyXText()->setSelectionOverString(bv, searchstr);
144 bv->toggleSelection(false);
145 bv->update(bv->getLyXText(), BufferView::SELECT|BufferView::FITCUR);
146 } else if (result == SR_NOT_FOUND)
156 // returns true if the specified string is at the specified position
157 bool IsStringInText(Paragraph * par, Paragraph::size_type pos,
158 string const & str, bool const & cs,
164 string::size_type size = str.length();
165 Paragraph::size_type i = 0;
166 while (((pos + i) < par->size())
167 && (string::size_type(i) < size)
168 && (cs ? (str[i] == par->getChar(pos + i))
169 : (toupper(str[i]) == toupper(par->getChar(pos + i)))))
173 if (size == string::size_type(i)) {
174 // if necessary, check whether string matches word
176 (mw && ((pos <= 0 || !IsLetterCharOrDigit(par->getChar(pos - 1)))
177 && (pos + Paragraph::size_type(size) >= par->size()
178 || !IsLetterCharOrDigit(par->getChar(pos + size))))
188 // if the string can be found: return true and set the cursor to
189 // the new position, cs = casesensitive, mw = matchword
190 SearchResult SearchForward(BufferView * bv, LyXText * text, string const & str,
191 bool const & cs, bool const & mw)
193 Paragraph * par = text->cursor.par();
194 Paragraph::size_type pos = text->cursor.pos();
195 UpdatableInset * inset;
197 while (par && !IsStringInText(par, pos, str, cs, mw)) {
198 if (par->isInset(pos) &&
199 (inset = (UpdatableInset *)par->getInset(pos)) &&
200 (inset->isTextInset()))
203 text->setCursor(bv, par, pos);
205 if (inset->searchForward(bv, str, cs, mw))
206 return SR_FOUND_NOUPDATE;
207 text = bv->getLyXText();
209 if (pos < par->size() - 1)
217 text->setCursor(bv, par, pos);
219 } else if (text->inset_owner) {
220 // test if we're inside an inset if yes unlock the inset
221 // and recall us with the outside LyXText!
222 bv->unlockInset((UpdatableInset *)text->inset_owner);
223 text = bv->getLyXText();
224 par = text->cursor.par();
225 pos = text->cursor.pos();
226 if (pos < par->size() - 1)
234 text->setCursor(bv, par, pos);
235 return SearchForward(bv, text, str, cs, mw);
242 // if the string can be found: return true and set the cursor to
243 // the new position, cs = casesensitive, mw = matchword
244 SearchResult SearchBackward(BufferView * bv, LyXText * text,
246 bool const & cs, bool const & mw)
248 Paragraph * par = text->cursor.par();
249 Paragraph::size_type pos = text->cursor.pos();
255 // We skip empty paragraphs (Asger)
257 par = par->previous();
259 pos = par->size() - 1;
260 } while (par && pos < 0);
262 UpdatableInset * inset;
263 if (par && par->isInset(pos) &&
264 (inset = (UpdatableInset *)par->getInset(pos)) &&
265 (inset->isTextInset()))
268 text->setCursor(bv, par, pos);
269 inset->edit(bv, false);
270 if (inset->searchBackward(bv, str, cs, mw))
271 return SR_FOUND_NOUPDATE;
272 text = bv->getLyXText();
274 } while (par && !IsStringInText(par, pos, str, cs, mw));
277 text->setCursor(bv, par, pos);
279 } else if (text->inset_owner) {
280 // test if we're inside an inset if yes unlock the inset
281 // and recall us with the outside LyXText!
282 bv->unlockInset((UpdatableInset *)text->inset_owner);
283 return SearchBackward(bv, bv->getLyXText(), str, cs, mw);