]> git.lyx.org Git - lyx.git/blobdiff - src/lyxfind.cpp
Take into account file system case sensitivity when checking whether
[lyx.git] / src / lyxfind.cpp
index 9b9b855d349c4ee7ba1071aedc81d273542bb672..e3c237bf904d58f4c1da38ff7a37d9518b131ada 100644 (file)
@@ -30,6 +30,8 @@
 #include "ParIterator.h"
 #include "TexRow.h"
 #include "Text.h"
+#include "FuncRequest.h"
+#include "LyXFunc.h"
 
 #include "mathed/InsetMath.h"
 #include "mathed/InsetMathGrid.h"
@@ -113,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;
 }
 
@@ -301,7 +311,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.
@@ -330,27 +340,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;
 }
 
@@ -531,7 +594,7 @@ bool braces_match(string::const_iterator const & beg,
  **/
 class MatchStringAdv {
 public:
-       MatchStringAdv(lyx::Buffer const & buf, FindAdvOptions const & opt);
+       MatchStringAdv(lyx::Buffer const & buf, FindAndReplaceOptions const & opt);
 
        /** Tests if text starting at the supplied position matches with the one provided to the MatchStringAdv
         ** constructor as opt.search, under the opt.* options settings.
@@ -539,7 +602,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.
         **/
@@ -549,7 +612,7 @@ public:
        /// buffer
        lyx::Buffer const & buf;
        /// options
-       FindAdvOptions const & opt;
+       FindAndReplaceOptions const & opt;
 
 private:
        /** Normalize a stringified or latexified LyX paragraph.
@@ -580,7 +643,7 @@ private:
 };
 
 
-MatchStringAdv::MatchStringAdv(lyx::Buffer const & buf, FindAdvOptions const & opt)
+MatchStringAdv::MatchStringAdv(lyx::Buffer const & buf, FindAndReplaceOptions const & opt)
   : buf(buf), opt(opt)
 {
        par_as_string = normalize(opt.search);
@@ -943,7 +1006,7 @@ int findBackwardsAdv(DocIterator & cur, MatchStringAdv const & match) {
 } // anonym namespace
 
 
-docstring stringifyFromForSearch(FindAdvOptions const & opt,
+docstring stringifyFromForSearch(FindAndReplaceOptions const & opt,
        DocIterator const & cur, int len)
 {
        if (!opt.ignoreformat)
@@ -953,17 +1016,17 @@ docstring stringifyFromForSearch(FindAdvOptions const & opt,
 }
 
 
-lyx::FindAdvOptions::FindAdvOptions(docstring const & search, bool casesensitive,
+lyx::FindAndReplaceOptions::FindAndReplaceOptions(docstring const & search, bool casesensitive,
        bool matchword, bool forward, bool expandmacros, bool ignoreformat,
-       bool regexp)
+       bool regexp, docstring const & replace)
        : search(search), casesensitive(casesensitive), matchword(matchword),
        forward(forward), expandmacros(expandmacros), ignoreformat(ignoreformat),
-       regexp(regexp)
+       regexp(regexp), replace(replace)
 {
 }
 
 /// Perform a FindAdv operation.
-bool findAdv(BufferView * bv, FindAdvOptions const & opt)
+bool findAdv(BufferView * bv, FindAndReplaceOptions const & opt)
 {
        DocIterator cur = bv->cursor();
        int match_len = 0;
@@ -997,7 +1060,9 @@ bool findAdv(BufferView * bv, FindAdvOptions const & opt)
        LYXERR(Debug::DEBUG, "Putting selection at " << cur << " with len: " << match_len);
        bv->putSelectionAt(cur, match_len, ! opt.forward);
        bv->message(_("Match found!"));
-       //bv->update();
+       if (opt.replace != docstring(from_utf8(LYX_FR_NULL_STRING))) {
+               dispatch(FuncRequest(LFUN_SELF_INSERT, opt.replace));
+       }
 
        return true;
 }
@@ -1008,14 +1073,14 @@ void findAdv(BufferView * bv, FuncRequest const & ev)
        if (!bv || ev.action != LFUN_WORD_FINDADV)
                return;
 
-       FindAdvOptions opt;
+       FindAndReplaceOptions opt;
        istringstream iss(to_utf8(ev.argument()));
        iss >> opt;
        findAdv(bv, opt);
 }
 
 
-ostringstream & operator<<(ostringstream & os, lyx::FindAdvOptions const & opt)
+ostringstream & operator<<(ostringstream & os, lyx::FindAndReplaceOptions const & opt)
 {
        os << to_utf8(opt.search) << "\nEOSS\n"
           << opt.casesensitive << ' '
@@ -1023,14 +1088,15 @@ ostringstream & operator<<(ostringstream & os, lyx::FindAdvOptions const & opt)
           << opt.forward << ' '
           << opt.expandmacros << ' '
           << opt.ignoreformat << ' '
-          << opt.regexp;
+          << opt.regexp << ' '
+          << to_utf8(opt.replace) << "\nEOSS\n";
 
        LYXERR(Debug::DEBUG, "built: " << os.str());
 
        return os;
 }
 
-istringstream & operator>>(istringstream & is, lyx::FindAdvOptions & opt)
+istringstream & operator>>(istringstream & is, lyx::FindAndReplaceOptions & opt)
 {
        LYXERR(Debug::DEBUG, "parsing");
        string s;
@@ -1047,8 +1113,21 @@ istringstream & operator>>(istringstream & is, lyx::FindAdvOptions & opt)
        LYXERR(Debug::DEBUG, "searching for: '" << s << "'");
        opt.search = from_utf8(s);
        is >> opt.casesensitive >> opt.matchword >> opt.forward >> opt.expandmacros >> opt.ignoreformat >> opt.regexp;
+       is.get();       // Waste space before replace string
+       s = "";
+       getline(is, line);
+       while (line != "EOSS") {
+               if (! s.empty())
+                               s = s + "\n";
+               s = s + line;
+               if (is.eof())   // Tolerate malformed request
+                               break;
+               getline(is, line);
+       }
        LYXERR(Debug::DEBUG, "parsed: " << opt.casesensitive << ' ' << opt.matchword << ' ' << opt.forward << ' '
                   << opt.expandmacros << ' ' << opt.ignoreformat << ' ' << opt.regexp);
+       LYXERR(Debug::DEBUG, "replacing with: '" << s << "'");
+       opt.replace = from_utf8(s);
        return is;
 }