]> git.lyx.org Git - lyx.git/blobdiff - src/lyxfind.cpp
Sanitize cursors after a buffer has been reloaded
[lyx.git] / src / lyxfind.cpp
index 2ddedcb4818bf21d37ae1c406fd7de9326990cd5..6e3b324bfa66be867f2bee4d4292bfd6b2ee3422 100644 (file)
@@ -289,9 +289,11 @@ bool findOne(BufferView * bv, docstring const & searchstr,
             bool find_del, bool check_wrap, bool const auto_wrap,
             bool instant, bool onlysel)
 {
+       bool const had_selection = bv->cursor().selection();
+
        // Clean up previous selections with empty searchstr on instant
        if (searchstr.empty() && instant) {
-               if (bv->cursor().selection()) {
+               if (had_selection) {
                        bv->setCursor(bv->cursor().selectionBegin());
                        bv->clearSelection();
                }
@@ -301,9 +303,10 @@ bool findOne(BufferView * bv, docstring const & searchstr,
        if (!searchAllowed(searchstr))
                return false;
 
-       DocIterator const endcur = forward ? bv->cursor().selectionEnd() : bv->cursor().selectionBegin();
+       DocIterator const startcur = bv->cursor().selectionBegin();
+       DocIterator const endcur = bv->cursor().selectionEnd();
 
-       if (onlysel && bv->cursor().selection()) {
+       if (onlysel && had_selection) {
                docstring const matchstring = bv->cursor().selectionAsString(false);
                docstring const lcmatchsting = support::lowercase(matchstring);
                if (matchstring == searchstr || (!case_sens && lcmatchsting == lowercase(searchstr))) {
@@ -329,11 +332,11 @@ bool findOne(BufferView * bv, docstring const & searchstr,
 
        int match_len = forward
                ? findForward(cur, endcur, match, find_del, onlysel)
-               : findBackwards(cur, endcur, match, find_del, onlysel);
+               : findBackwards(cur, startcur, match, find_del, onlysel);
 
        if (match_len > 0)
                bv->putSelectionAt(cur, match_len, !forward);
-       else if (onlysel && bv->cursor().selection()) {
+       else if (onlysel && had_selection) {
                docstring q = _("The search string was not found within the selection.\n"
                                "Continue search outside?");
                int search_answer = frontend::Alert::prompt(_("Search outside selection?"),
@@ -347,7 +350,6 @@ bool findOne(BufferView * bv, docstring const & searchstr,
                return false;
        }
        else if (check_wrap) {
-               DocIterator cur_orig(bv->cursor());
                bool wrap = auto_wrap;
                if (!auto_wrap) {
                        docstring q;
@@ -380,7 +382,13 @@ bool findOne(BufferView * bv, docstring const & searchstr,
                                    find_del, false, false, false, false))
                                return true;
                }
-               bv->cursor().setCursor(cur_orig);
+               bv->setCursor(startcur);
+
+               // restore original selection
+               if (had_selection) {
+                       bv->cursor().resetAnchor();
+                       bv->setSelection(startcur, endcur);
+               }
                return false;
        }
 
@@ -456,7 +464,7 @@ int replaceAll(BufferView * bv,
        if (had_selection) {
                endcur.fixIfBroken();
                bv->cursor().resetAnchor();
-               bv->setCursorSelectionTo(endcur);
+               bv->setSelection(startcur, endcur);
        }
 
        return num;
@@ -831,13 +839,47 @@ string string2regex(string in)
                        // normal blanks
                        blanks++;
                }
-               else if ((tempx[i] == '\302' && tempx[i+1] == '\240')
-                       || (tempx[i] == '\342' && tempx[i+1] == '\200')) {
-                       // protected space
-                       // thin space
+               else if (tempx[i] == '\302' && tempx[i+1] == '\240') {
+                       // Normal Space
                        blanks++;
                        i++;
                }
+               else if (tempx[i] == '\342') {
+                       if (tempx[i+1] == '\200') {
+                               if ((tempx[i+2] == '\257')
+                                  || (tempx[i+2] == '\203')
+                                  || (tempx[i+2] == '\202')) {
+                                       // Non-breaking Thin (1/6 em)
+                                       // Quad(1 em), (Double quad counts as 2 blanks)
+                                       // Half Quad
+                                       blanks++;
+                                       i += 2;
+                               }
+                               else if (tempx[i+2] == '\213') {
+                                       // Ignoring parts of Medium and Thick
+                                       i += 2;
+                                       continue;
+                               }
+                               else if ((tempx[i+2] == '\204') || (tempx[i+2] == '\205')) {
+                                       // Thick
+                                       // Medium
+                                       blanks++;
+                                       i += 2;
+                               }
+                       }
+                       else if (tempx[i+1] == '\201') {
+                               if (tempx[i+2] == '\240') {
+                                       // Ignoring parts of half quad
+                                       i += 2;
+                                       continue;
+                               }
+                       }
+                       else if ((tempx[i+1] == '\220') && (tempx[i+2] == '\243')) {
+                               // Visible space
+                               blanks++;
+                               i += 2;
+                       }
+               }
                else {
                        if (blanks > 0) {
                                temp += getRegexSpaceCount(blanks);
@@ -913,6 +955,9 @@ string correctRegex(string t, bool withformat)
                        }
                        else if (sub.str(4) == "mathcircumflex")
                                replace = "^";
+                       else if ((sub.str(4) == "negthinspace") || (sub.str(4) == "negmedspace") || (sub.str(4) == "negthickspace")) {
+                               replace = accents[sub.str(4)+"{}"];
+                       }
                        else if (backslashed) {
                                backslashed = false;
                                if (withformat) {
@@ -1123,10 +1168,10 @@ private:
        // par_as_string after removal of lead_as_string
        string par_as_string_nolead;
        // unmatched open braces in the search string/regexp
-       int open_braces;
+       int open_braces = 0;
        // number of (.*?) subexpressions added at end of search regexp for closing
        // environments, math mode, styles, etc...
-       int close_wildcards;
+       int close_wildcards = 0;
 public:
        // Are we searching with regular expressions ?
        bool use_regexp = false;
@@ -1962,11 +2007,11 @@ static void buildAccentsMap()
        accents["guillemotleft"] = "«";
        accents["hairspace"]     = getutf8(0xf0000);    // select from free unicode plane 15
        accents["thinspace"]     = getutf8(0xf0002);    // and used _only_ by findadv
-       accents["negthinspace"]  = getutf8(0xf0003);    // to omit backslashed latex macros
+       accents["negthinspace{}"]= getutf8(0xf0003);    // to omit backslashed latex macros
        accents["medspace"]      = getutf8(0xf0004);    // See https://en.wikipedia.org/wiki/Private_Use_Areas
-       accents["negmedspace"]   = getutf8(0xf0005);
+       accents["negmedspace{}"] = getutf8(0xf0005);
        accents["thickspace"]    = getutf8(0xf0006);
-       accents["negthickspace"] = getutf8(0xf0007);
+       accents["negthickspace{}"]= getutf8(0xf0007);
        accents["lyx"]           = getutf8(0xf0010);    // Used logos
        accents["LyX"]           = getutf8(0xf0010);
        accents["tex"]           = getutf8(0xf0011);
@@ -2056,7 +2101,7 @@ void Intervall::removeAccents()
        if (accents.empty())
                buildAccentsMap();
        static regex const accre("\\\\("
-                                "([\\S]|[A-Za-z]+)\\{[^\\\\\\{\\}]+\\}"
+                                "([\\S]|[A-Za-z]+)\\{[^\\\\\\{\\}]*\\}"
                                 "|([\\S]|[A-Za-z]+)\\{\\\\[ij](math)?\\}"
                                 "|("
                                 "(backslash ([lL]y[xX]|[tT]e[xX]|[lL]a[tT]e[xX]e?|lyxarrow))"
@@ -3665,8 +3710,6 @@ MatchStringAdv::MatchStringAdv(lyx::Buffer & buf, FindAndReplaceOptions & opt)
        }
        // When using regexp, braces are hacked already by escape_for_regex()
        par_as_string = convertLF2Space(ds, opt.ignoreformat);
-       open_braces = 0;
-       close_wildcards = 0;
 
        size_t lead_size = 0;
        // correct the language settings