]> git.lyx.org Git - lyx.git/blobdiff - src/lyxfind.cpp
LogUi.ui: string redundancy.
[lyx.git] / src / lyxfind.cpp
index 0b6bc6509f3ac00482d3ea402dcb61c4fa12ec97..5c26645075e128791bd5ec1f4681f57996bb3a7e 100644 (file)
@@ -115,11 +115,19 @@ bool findBackwards(DocIterator & cur, MatchString const & match,
 }
 
 
-bool findChange(DocIterator & cur)
+bool findChange(DocIterator & cur, bool next)
 {
-       for (; cur; cur.forwardPos())
-               if (cur.inTexted() && !cur.paragraph().isUnchanged(cur.pos()))
+       if (!next)
+               cur.backwardPos();
+       for (; cur; next ? cur.forwardPos() : cur.backwardPos())
+               if (cur.inTexted() && !cur.paragraph().isUnchanged(cur.pos())) {
+                       if (!next)
+                               // if we search backwards, take a step forward
+                               // to correctly set the anchor
+                               cur.forwardPos();
                        return true;
+               }
+
        return false;
 }
 
@@ -194,9 +202,17 @@ int replaceAll(BufferView * bv,
 }
 
 
-bool stringSelected(BufferView * bv, docstring const & searchstr,
+bool stringSelected(BufferView * bv, docstring & searchstr,
                    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()) {
+               bv->cursor().innerText()->selectWord(bv->cursor(), WHOLE_WORD);
+               searchstr = bv->cursor().selectionAsString(false);
+               return true;
+       }
+
        // if nothing selected or selection does not equal search
        // string search and select next occurance and return
        docstring const & str1 = searchstr;
@@ -210,13 +226,13 @@ bool stringSelected(BufferView * bv, docstring const & searchstr,
 }
 
 
-int replace(BufferView * bv, docstring const & searchstr,
+int replace(BufferView * bv, docstring & searchstr,
            docstring const & replacestr, bool cs, bool mw, bool fw)
 {
-       if (!searchAllowed(bv, searchstr) || bv->buffer().isReadonly())
+       if (!stringSelected(bv, searchstr, cs, mw, fw))
                return 0;
 
-       if (!stringSelected(bv, searchstr, cs, mw, fw))
+       if (!searchAllowed(bv, searchstr) || bv->buffer().isReadonly())
                return 0;
 
        Cursor & cur = bv->cursor();
@@ -244,13 +260,13 @@ docstring const find2string(docstring const & search,
 }
 
 
-docstring const replace2string(docstring const & search, docstring const & replace,
-                           bool casesensitive, bool matchword,
-                           bool all, bool forward)
+docstring const replace2string(docstring const & replace,
+       docstring const & search, bool casesensitive, bool matchword,
+       bool all, bool forward)
 {
        odocstringstream ss;
-       ss << search << '\n'
-          << replace << '\n'
+       ss << replace << '\n'
+          << search << '\n'
           << int(casesensitive) << ' '
           << int(matchword) << ' '
           << int(all) << ' '
@@ -291,8 +307,8 @@ void replace(BufferView * bv, FuncRequest const & ev, bool has_deleted)
        //  <casesensitive> <matchword> <all> <forward>"
        docstring search;
        docstring rplc;
-       docstring howto = split(ev.argument(), search, '\n');
-       howto = split(howto, rplc, '\n');
+       docstring howto = split(ev.argument(), rplc, '\n');
+       howto = split(howto, search, '\n');
 
        bool casesensitive = parse_bool(howto);
        bool matchword     = parse_bool(howto);
@@ -303,7 +319,7 @@ void replace(BufferView * bv, FuncRequest const & ev, bool has_deleted)
                int const replace_count = all
                        ? replaceAll(bv, search, rplc, casesensitive, matchword)
                        : replace(bv, search, rplc, casesensitive, matchword, forward);
-       
+
                Buffer & buf = bv->buffer();
                if (replace_count == 0) {
                        // emit message signal.
@@ -332,27 +348,80 @@ void replace(BufferView * bv, FuncRequest const & ev, bool has_deleted)
 
 bool findNextChange(BufferView * bv)
 {
+       return findChange(bv, true);
+}
+
+
+bool findPreviousChange(BufferView * bv)
+{
+       return findChange(bv, false);
+}
+
+
+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()))
+                       bv->cursor().setCursorToAnchor();
+       }
+
        DocIterator cur = bv->cursor();
 
-       if (!findChange(cur))
+       // Are we within a change ? Then first search forward (backward),
+       // clear the selection and search the other way around (see the end
+       // of this function). This will avoid changes to be selected half.
+       bool search_both_sides = false;
+       if (cur.pos() > 1) {
+               Change change_next_pos
+                       = cur.paragraph().lookupChange(cur.pos());
+               Change change_prev_pos
+                       = cur.paragraph().lookupChange(cur.pos() - 1);
+               if (change_next_pos.isSimilarTo(change_prev_pos))
+                       search_both_sides = true;
+       }
+
+       if (!findChange(cur, next))
                return false;
 
        bv->cursor().setCursor(cur);
        bv->cursor().resetAnchor();
 
+       if (!next)
+               // take a step into the change
+               cur.backwardPos();
+
        Change orig_change = cur.paragraph().lookupChange(cur.pos());
 
        CursorSlice & tip = cur.top();
-       for (; !tip.at_end(); tip.forwardPos()) {
-               Change change = tip.paragraph().lookupChange(tip.pos());
-               if (change != orig_change)
-                       break;
+       if (next) {
+               for (; !tip.at_end(); tip.forwardPos()) {
+                       Change change = tip.paragraph().lookupChange(tip.pos());
+                       if (change != orig_change)
+                               break;
+               }
+       } else {
+               for (; !tip.at_begin();) {
+                       tip.backwardPos();
+                       Change change = tip.paragraph().lookupChange(tip.pos());
+                       if (change != orig_change) {
+                               // take a step forward to correctly set the selection
+                               tip.forwardPos();
+                               break;
+                       }
+               }
        }
 
        // Now put cursor to end of selection:
        bv->cursor().setCursor(cur);
        bv->cursor().setSelection();
 
+       if (search_both_sides) {
+               bv->cursor().setSelection(false);
+               findChange(bv, !next);
+       }
+
        return true;
 }
 
@@ -541,7 +610,7 @@ public:
         ** @param at_begin
         **     If set, then match is searched only against beginning of text starting at cur.
         **     If unset, then match is searched anywhere in text starting at cur.
-        ** 
+        **
         ** @return
         ** The length of the matching text, or zero if no match was found.
         **/