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
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;
132 text = bv->getLyXText();
133 Paragraph * par = text->cursor.par();
134 Paragraph::size_type pos = text->cursor.pos();
136 if (pos < par->size() - 1)
143 text->setCursor(bv, par, pos);
147 SearchForward(bv, text, searchstr, casesens, matchwrd) :
148 SearchBackward(bv, text, searchstr, casesens, matchwrd);
153 SearchForward(bv, text, searchstr, casesens, matchwrd) :
154 SearchBackward(bv, text, searchstr, casesens, matchwrd);
158 if (result == SR_FOUND) {
159 // the actual text pointer could have changed!
160 bv->update(bv->getLyXText(), BufferView::SELECT|BufferView::FITCUR);
161 bv->toggleSelection();
162 bv->getLyXText()->clearSelection();
163 bv->getLyXText()->setSelectionOverString(bv, searchstr);
164 bv->toggleSelection(false);
165 bv->update(bv->getLyXText(), BufferView::SELECT|BufferView::FITCUR);
166 } else if (result == SR_NOT_FOUND)
176 // returns true if the specified string is at the specified position
177 bool IsStringInText(Paragraph * par, Paragraph::size_type pos,
178 string const & str, bool const & cs,
184 string::size_type size = str.length();
185 Paragraph::size_type i = 0;
186 while (((pos + i) < par->size())
187 && (string::size_type(i) < size)
188 && (cs ? (str[i] == par->getChar(pos + i))
189 : (toupper(str[i]) == toupper(par->getChar(pos + i)))))
193 if (size == string::size_type(i)) {
194 // if necessary, check whether string matches word
196 (mw && ((pos <= 0 || !IsLetterCharOrDigit(par->getChar(pos - 1)))
197 && (pos + Paragraph::size_type(size) >= par->size()
198 || !IsLetterCharOrDigit(par->getChar(pos + size))))
208 // if the string can be found: return true and set the cursor to
209 // the new position, cs = casesensitive, mw = matchword
210 SearchResult SearchForward(BufferView * bv, LyXText * text, string const & str,
211 bool const & cs, bool const & mw)
213 Paragraph * par = text->cursor.par();
214 Paragraph::size_type pos = text->cursor.pos();
215 UpdatableInset * inset;
217 while (par && !IsStringInText(par, pos, str, cs, mw)) {
218 if (par->isInset(pos) &&
219 (inset = (UpdatableInset *)par->getInset(pos)) &&
220 (inset->isTextInset()))
223 text->setCursor(bv, par, pos);
225 if (inset->searchForward(bv, str, cs, mw))
226 return SR_FOUND_NOUPDATE;
227 text = bv->getLyXText();
229 if (pos < par->size() - 1)
237 text->setCursor(bv, par, pos);
240 } else if (text->inset_owner) {
241 // test if we're inside an inset if yes unlock the inset
242 // and recall us with the outside LyXText!
243 bv->unlockInset((UpdatableInset *)text->inset_owner);
244 if (!bv->theLockingInset()) {
245 text = bv->getLyXText();
246 par = text->cursor.par();
247 pos = text->cursor.pos();
248 if (pos < par->size() - 1)
256 text->setCursor(bv, par, pos);
257 return SearchForward(bv, text, str, cs, mw);
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 Paragraph::size_type pos = text->cursor.pos();
281 // We skip empty paragraphs (Asger)
283 par = par->previous();
285 pos = par->size() - 1;
286 } while (par && pos < 0);
288 UpdatableInset * inset;
289 if (par && par->isInset(pos) &&
290 (inset = (UpdatableInset *)par->getInset(pos)) &&
291 (inset->isTextInset()))
294 text->setCursor(bv, par, pos);
295 inset->edit(bv, false);
296 if (inset->searchBackward(bv, str, cs, mw))
297 return SR_FOUND_NOUPDATE;
298 text = bv->getLyXText();
300 } while (par && !IsStringInText(par, pos, str, cs, mw));
303 text->setCursor(bv, par, pos);
305 } else if (text->inset_owner) {
306 // test if we're inside an inset if yes unlock the inset
307 // and recall us with the outside LyXText!
308 bv->unlockInset((UpdatableInset *)text->inset_owner);
309 if (!bv->theLockingInset()) {
310 return SearchBackward(bv, bv->getLyXText(), str, cs, mw);