#include "support/lassert.h"
#include "support/lstrings.h"
-#include <boost/regex.hpp>
+#include "support/regex.h"
#include <boost/next_prior.hpp>
using namespace std;
if (!searchAllowed(bv, searchstr) || buf.isReadonly())
return 0;
+ DocIterator cur_orig(bv->cursor());
+
MatchString const match(searchstr, cs, mw);
int num = 0;
++num;
}
- buf.updateLabels();
bv->putSelectionAt(doc_iterator_begin(&buf), 0, false);
if (num)
buf.markDirty();
+
+ cur_orig.fixIfBroken();
+ bv->setCursor(cur_orig);
+
return num;
}
bool cs, bool mw, bool fw)
{
// if nothing selected and searched string is empty, this
- // means that we want to search current word at cursor position.
- if (!bv->cursor().selection() && searchstr.empty()) {
+ // means that we want to search current word at cursor position,
+ // but only if we are in texted() mode.
+ if (!bv->cursor().selection() && searchstr.empty()
+ && bv->cursor().inTexted()) {
bv->cursor().innerText()->selectWord(bv->cursor(), WHOLE_WORD);
searchstr = bv->cursor().selectionAsString(false);
return true;
bool find(BufferView * bv, FuncRequest const & ev)
{
- if (!bv || ev.action != LFUN_WORD_FIND)
+ if (!bv || ev.action() != LFUN_WORD_FIND)
return false;
//lyxerr << "find called, cmd: " << ev << endl;
void replace(BufferView * bv, FuncRequest const & ev, bool has_deleted)
{
- if (!bv || ev.action != LFUN_WORD_REPLACE)
+ if (!bv || ev.action() != LFUN_WORD_REPLACE)
return;
// data is of the form
if (bv->cursor().selection()) {
// set the cursor at the beginning or at the end of the selection
// before searching. Otherwise, the current change will be found.
- if (next != (bv->cursor().top() > bv->cursor().anchor()))
+ if (next != (bv->cursor().top() > bv->cursor().normalAnchor()))
bv->cursor().setCursorToAnchor();
}
return s;
}
-/// Wrapper for boost::regex_replace with simpler interface
+/// Wrapper for lyx::regex_replace with simpler interface
bool regex_replace(string const & s, string & t, string const & searchstr,
string const & replacestr)
{
- boost::regex e(searchstr);
+ lyx::regex e(searchstr);
ostringstream oss;
ostream_iterator<char, char> it(oss);
- boost::regex_replace(it, s.begin(), s.end(), e, replacestr);
+ lyx::regex_replace(it, s.begin(), s.end(), e, replacestr);
// tolerate t and s be references to the same variable
bool rv = (s != oss.str());
t = oss.str();
// normalized string to search
string par_as_string;
// regular expression to use for searching
- boost::regex regexp;
+ lyx::regex regexp;
// same as regexp, but prefixed with a ".*"
- boost::regex regexp2;
+ lyx::regex regexp2;
// unmatched open braces in the search string/regexp
int open_braces;
// number of (.*?) subexpressions added at end of search regexp for closing
LYXERR(Debug::FIND, "Close .*? : " << close_wildcards);
LYXERR(Debug::FIND, "Replaced text (to be used as regex): " << par_as_string);
// If entered regexp must match at begin of searched string buffer
- regexp = boost::regex(string("\\`") + par_as_string);
+ regexp = lyx::regex(string("\\`") + par_as_string);
// If entered regexp may match wherever in searched string buffer
- regexp2 = boost::regex(string("\\`.*") + par_as_string);
+ regexp2 = lyx::regex(string("\\`.*") + par_as_string);
}
}
} else {
// Try all possible regexp matches,
//until one that verifies the braces match test is found
- boost::regex const *p_regexp = at_begin ? ®exp : ®exp2;
- boost::sregex_iterator re_it(str.begin(), str.end(), *p_regexp);
- boost::sregex_iterator re_it_end;
+ regex const *p_regexp = at_begin ? ®exp : ®exp2;
+ sregex_iterator re_it(str.begin(), str.end(), *p_regexp);
+ sregex_iterator re_it_end;
for (; re_it != re_it_end; ++re_it) {
- boost::match_results<string::const_iterator> const & m = *re_it;
+ match_results<string::const_iterator> const & m = *re_it;
// Check braces on the segment that matched the entire regexp expression,
// plus the last subexpression, if a (.*?) was inserted in the constructor.
if (! braces_match(m[0].first, m[0].second, open_braces))
/// Find the most backward consecutive match within same paragraph while searching backwards.
-void findMostBackwards(DocIterator & cur, MatchStringAdv const & match, int & len)
+int findMostBackwards(DocIterator & cur, MatchStringAdv const & match)
{
DocIterator cur_begin = doc_iterator_begin(cur.buffer());
- len = findAdvFinalize(cur, match);
- if (cur != cur_begin) {
- Inset & inset = cur.inset();
- int old_len;
- DocIterator old_cur;
- DocIterator dit2;
- do {
- old_cur = cur;
- old_len = len;
- cur.backwardPos();
- LYXERR(Debug::FIND, "findMostBackwards(): old_cur="
- << old_cur << ", old_len=" << len << ", cur=" << cur);
- dit2 = cur;
- } while (cur != cur_begin && &cur.inset() == &inset && match(cur)
- && (len = findAdvFinalize(dit2, match)) > old_len);
- cur = old_cur;
- len = old_len;
+ DocIterator tmp_cur = cur;
+ int len = findAdvFinalize(tmp_cur, match);
+ Inset & inset = cur.inset();
+ for (; cur != cur_begin; cur.backwardPos()) {
+ LYXERR(Debug::FIND, "findMostBackwards(): cur=" << cur);
+ DocIterator new_cur = cur;
+ new_cur.backwardPos();
+ if (new_cur == cur || &new_cur.inset() != &inset || !match(new_cur))
+ break;
+ int new_len = findAdvFinalize(new_cur, match);
+ if (new_len == len)
+ break;
+ len = new_len;
}
- LYXERR(Debug::FIND, "findMostBackwards(): cur=" << cur);
+ LYXERR(Debug::FIND, "findMostBackwards(): exiting with cur=" << cur);
+ return len;
}
if (! cur)
return 0;
// Backup of original position
- DocIterator cur_orig(cur);
DocIterator cur_begin = doc_iterator_begin(cur.buffer());
if (cur == cur_begin)
return 0;
+ cur.backwardPos();
+ DocIterator cur_orig(cur);
bool found_match;
bool pit_changed = false;
found_match = false;
cur.pos() = cur_orig.pos();
LYXERR(Debug::FIND, "findBackAdv2: cur: " << cur);
DocIterator cur_prev_iter;
- while (true) {
+ do {
found_match = match(cur);
LYXERR(Debug::FIND, "findBackAdv3: found_match="
<< found_match << ", cur: " << cur);
- if (found_match) {
- int len;
- findMostBackwards(cur, match, len);
- if (cur < cur_orig)
- return len;
- }
- // Prevent infinite loop at begin of document
- if (cur == cur_begin || cur == cur_prev_iter)
+ if (found_match)
+ return findMostBackwards(cur, match);
+
+ // Stop if begin of document reached
+ if (cur == cur_begin)
break;
cur_prev_iter = cur;
cur.backwardPos();
- }
+ } while (true);
}
if (cur == cur_begin)
break;
return;
DocIterator sel_beg = cur.selectionBegin();
DocIterator sel_end = cur.selectionEnd();
- LASSERT(&sel_beg.inset() == &sel_end.inset(), /**/);
+ if (&sel_beg.inset() != &sel_end.inset()
+ || sel_beg.pit() != sel_end.pit())
+ return;
int sel_len = sel_end.pos() - sel_beg.pos();
- LYXERR(Debug::FIND, "sel_beg: " << sel_beg << ", sel_end: " << sel_end << ", sel_len: " << sel_len << endl);
+ LYXERR(Debug::FIND, "sel_beg: " << sel_beg << ", sel_end: " << sel_end
+ << ", sel_len: " << sel_len << endl);
if (sel_len == 0)
return;
LASSERT(sel_len > 0, /**/);
else
match_len = findBackwardsAdv(cur, matchAdv);
} catch (...) {
- // This may only be raised by boost::regex()
+ // This may only be raised by lyx::regex()
bv->message(_("Invalid regular expression!"));
return false;
}