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();
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
83 str2 = text->selectionAsString(bv->buffer());
85 str1 = lowercase(searchstr);
86 str2 = lowercase(text->selectionAsString(bv->buffer()));
89 if (!LyXFind(bv, searchstr, fw, false, casesens, matchwrd) ||
97 int replace_count = 0;
100 bv->update(bv->getLyXText(), BufferView::SELECT|BufferView::FITCUR);
101 bv->toggleSelection(false);
102 bv->getLyXText()->replaceSelectionWithString(bv, replacestr);
103 bv->getLyXText()->setSelectionOverString(bv, replacestr);
104 bv->update(bv->getLyXText(), BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
107 found = LyXFind(bv, searchstr, fw, false, casesens, matchwrd);
108 } while (!once && replaceall && found);
113 return replace_count;
116 bool LyXFind(BufferView * bv,
117 string const & searchstr, bool forward,
118 bool frominset, bool casesens, bool matchwrd)
120 if (!bv->available() || searchstr.empty())
123 LyXText * text = bv->getLyXText();
126 bv->update(text, BufferView::SELECT|BufferView::FITCUR);
128 if (text->selection.set())
129 text->cursor = forward ?
130 text->selection.end : text->selection.start;
132 SearchResult result = SR_NOT_FOUND;
134 if (!frominset && bv->theLockingInset()) {
135 bool found = forward ?
136 bv->theLockingInset()->searchForward(bv, searchstr, casesens, matchwrd) :
137 bv->theLockingInset()->searchBackward(bv, searchstr, casesens, matchwrd);
139 result = SR_FOUND_NOUPDATE;
141 text = bv->getLyXText();
142 Paragraph * par = text->cursor.par();
143 Paragraph::size_type pos = text->cursor.pos();
145 if (pos < par->size() - 1)
152 text->setCursor(bv, par, pos);
156 SearchForward(bv, text, searchstr, casesens, matchwrd) :
157 SearchBackward(bv, text, searchstr, casesens, matchwrd);
162 SearchForward(bv, text, searchstr, casesens, matchwrd) :
163 SearchBackward(bv, text, searchstr, casesens, matchwrd);
167 if (result == SR_FOUND) {
168 // the actual text pointer could have changed!
169 bv->update(bv->getLyXText(), BufferView::SELECT|BufferView::FITCUR);
170 bv->toggleSelection();
171 bv->getLyXText()->clearSelection();
172 bv->getLyXText()->setSelectionOverString(bv, searchstr);
173 bv->toggleSelection(false);
174 bv->update(bv->getLyXText(), BufferView::SELECT|BufferView::FITCUR);
175 } else if (result == SR_NOT_FOUND)
185 // returns true if the specified string is at the specified position
186 bool IsStringInText(Paragraph * par, Paragraph::size_type pos,
187 string const & str, bool const & cs,
193 string::size_type size = str.length();
194 Paragraph::size_type i = 0;
195 while (((pos + i) < par->size())
196 && (string::size_type(i) < size)
197 && (cs ? (str[i] == par->getChar(pos + i))
198 : (toupper(str[i]) == toupper(par->getChar(pos + i)))))
202 if (size == string::size_type(i)) {
203 // if necessary, check whether string matches word
205 (mw && ((pos <= 0 || !IsLetterCharOrDigit(par->getChar(pos - 1)))
206 && (pos + Paragraph::size_type(size) >= par->size()
207 || !IsLetterCharOrDigit(par->getChar(pos + size))))
217 // if the string can be found: return true and set the cursor to
218 // the new position, cs = casesensitive, mw = matchword
219 SearchResult SearchForward(BufferView * bv, LyXText * text, string const & str,
220 bool const & cs, bool const & mw)
222 Paragraph * par = text->cursor.par();
223 Paragraph::size_type pos = text->cursor.pos();
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()))
232 text->setCursor(bv, par, pos);
234 if (inset->searchForward(bv, str, cs, mw))
235 return SR_FOUND_NOUPDATE;
236 text = bv->getLyXText();
238 if (pos < par->size() - 1)
246 text->setCursor(bv, par, pos);
249 } else if (text->inset_owner) {
250 // test if we're inside an inset if yes unlock the inset
251 // and recall us with the outside LyXText!
252 bv->unlockInset((UpdatableInset *)text->inset_owner);
253 if (!bv->theLockingInset()) {
254 text = bv->getLyXText();
255 par = text->cursor.par();
256 pos = text->cursor.pos();
257 if (pos < par->size() - 1)
265 text->setCursor(bv, par, pos);
266 return SearchForward(bv, text, str, cs, mw);
277 // if the string can be found: return true and set the cursor to
278 // the new position, cs = casesensitive, mw = matchword
279 SearchResult SearchBackward(BufferView * bv, LyXText * text,
281 bool const & cs, bool const & mw)
283 Paragraph * par = text->cursor.par();
284 Paragraph::size_type pos = text->cursor.pos();
290 // We skip empty paragraphs (Asger)
292 par = par->previous();
294 pos = par->size() - 1;
295 } while (par && pos < 0);
297 UpdatableInset * inset;
298 if (par && par->isInset(pos) &&
299 (inset = (UpdatableInset *)par->getInset(pos)) &&
300 (inset->isTextInset()))
303 text->setCursor(bv, par, pos);
304 inset->edit(bv, false);
305 if (inset->searchBackward(bv, str, cs, mw))
306 return SR_FOUND_NOUPDATE;
307 text = bv->getLyXText();
309 } while (par && !IsStringInText(par, pos, str, cs, mw));
312 text->setCursor(bv, par, pos);
316 else if (text->inset_owner) {
317 // test if we're inside an inset if yes unlock the inset
318 // and recall us with the outside LyXText!
319 bv->unlockInset((UpdatableInset *)text->inset_owner);
320 if (!bv->theLockingInset()) {
321 return SearchBackward(bv, bv->getLyXText(), str, cs, mw);