]> git.lyx.org Git - lyx.git/blobdiff - src/lyxfind.cpp
Exporter.cpp: revert r34230 because this interferes with Enrico's new LyXVC feature...
[lyx.git] / src / lyxfind.cpp
index 6aade107bb2411469f5352b616f1463b54f726c2..fd44187104fdb9a6d80ed04f18cb54c0875f562b 100644 (file)
@@ -171,6 +171,8 @@ int replaceAll(BufferView * bv,
        if (!searchAllowed(bv, searchstr) || buf.isReadonly())
                return 0;
 
+       DocIterator cur_orig(bv->cursor());
+
        MatchString const match(searchstr, cs, mw);
        int num = 0;
 
@@ -198,6 +200,10 @@ int replaceAll(BufferView * bv,
        bv->putSelectionAt(doc_iterator_begin(&buf), 0, false);
        if (num)
                buf.markDirty();
+
+       cur_orig.fixIfBroken();
+       bv->setCursor(cur_orig);
+
        return num;
 }
 
@@ -279,7 +285,7 @@ docstring const replace2string(docstring const & replace,
 
 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;
@@ -300,7 +306,7 @@ bool find(BufferView * bv, FuncRequest const & ev)
 
 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
@@ -365,7 +371,7 @@ bool findChange(BufferView * bv, bool next)
        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();
        }
 
@@ -980,25 +986,21 @@ int findForwardAdv(DocIterator & cur, MatchStringAdv & match)
 void findMostBackwards(DocIterator & cur, MatchStringAdv const & match, int & len)
 {
        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;
+       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);
 }
 
 
@@ -1007,10 +1009,11 @@ int findBackwardsAdv(DocIterator & cur, MatchStringAdv & match) {
        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;
@@ -1025,22 +1028,21 @@ int findBackwardsAdv(DocIterator & cur, MatchStringAdv & match) {
                                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;
+                                       return len;
                                }
-                               // Prevent infinite loop at begin of document
-                               if (cur == cur_begin || cur == cur_prev_iter)
+                               // Stop if begin of document reached
+                               if (cur == cur_begin)
                                        break;
                                cur_prev_iter = cur;
                                cur.backwardPos();
-                       }
+                       } while (true);
                }
                if (cur == cur_begin)
                        break;