]> git.lyx.org Git - lyx.git/blobdiff - src/lyxfind.cpp
Use convert instead of a stream to read LFUN_REPEAT count
[lyx.git] / src / lyxfind.cpp
index 4d01547aa281a3f84a5a4bb730504ac8b41b2b90..441cbf18b3c0cf0396ce56d60b3944e9345e1748 100644 (file)
@@ -39,6 +39,7 @@
 #include "mathed/InsetMath.h"
 #include "mathed/InsetMathGrid.h"
 #include "mathed/InsetMathHull.h"
+#include "mathed/MathData.h"
 #include "mathed/MathStream.h"
 #include "mathed/MathSupport.h"
 
@@ -417,21 +418,57 @@ bool lyxreplace(BufferView * bv,
 }
 
 
-bool findNextChange(DocIterator & cur)
+bool findNextChange(BufferView * bv, Cursor & cur, bool const check_wrap)
 {
        for (; cur; cur.forwardPos())
                if (cur.inTexted() && cur.paragraph().isChanged(cur.pos()))
                        return true;
+
+       if (check_wrap) {
+               DocIterator cur_orig(bv->cursor());
+               docstring q = _("End of file reached while searching forward.\n"
+                         "Continue searching from the beginning?");
+               int wrap_answer = frontend::Alert::prompt(_("Wrap search?"),
+                       q, 0, 1, _("&Yes"), _("&No"));
+               if (wrap_answer == 0) {
+                       bv->cursor().clear();
+                       bv->cursor().push_back(CursorSlice(bv->buffer().inset()));
+                       bv->clearSelection();
+                       cur.setCursor(bv->cursor().selectionBegin());
+                       if (findNextChange(bv, cur, false))
+                               return true;
+               }
+               bv->cursor().setCursor(cur_orig);
+       }
+
        return false;
 }
 
 
-bool findPreviousChange(DocIterator & cur)
+bool findPreviousChange(BufferView * bv, Cursor & cur, bool const check_wrap)
 {
        for (cur.backwardPos(); cur; cur.backwardPos()) {
                if (cur.inTexted() && cur.paragraph().isChanged(cur.pos()))
                        return true;
        }
+
+       if (check_wrap) {
+               DocIterator cur_orig(bv->cursor());
+               docstring q = _("Beginning of file reached while searching backward.\n"
+                         "Continue searching from the end?");
+               int wrap_answer = frontend::Alert::prompt(_("Wrap search?"),
+                       q, 0, 1, _("&Yes"), _("&No"));
+               if (wrap_answer == 0) {
+                       bv->cursor().setCursor(doc_iterator_end(&bv->buffer()));
+                       bv->cursor().backwardPos();
+                       bv->clearSelection();
+                       cur.setCursor(bv->cursor().selectionBegin());
+                       if (findPreviousChange(bv, cur, false))
+                               return true;
+               }
+               bv->cursor().setCursor(cur_orig);
+       }
+
        return false;
 }
 
@@ -476,7 +513,7 @@ bool findChange(BufferView * bv, bool forward)
        Cursor cur(*bv);
        cur.setCursor(forward ? bv->cursor().selectionEnd()
                      : bv->cursor().selectionBegin());
-       forward ? findNextChange(cur) : findPreviousChange(cur);
+       forward ? findNextChange(bv, cur, true) : findPreviousChange(bv, cur, true);
        return selectChange(cur, forward);
 }
 
@@ -914,47 +951,53 @@ int MatchStringAdv::findAux(DocIterator const & cur, int len, bool at_begin) con
        if (at_begin &&
                (opt.restr == FindAndReplaceOptions::R_ONLY_MATHS && !cur.inMathed()) )
                return 0;
+
        docstring docstr = stringifyFromForSearch(opt, cur, len);
-       LYXERR(Debug::FIND, "Matching against     '" << lyx::to_utf8(docstr) << "'");
        string str = normalize(docstr, true);
+       LYXERR(Debug::FIND, "Matching against     '" << lyx::to_utf8(docstr) << "'");
        LYXERR(Debug::FIND, "After normalization: '" << str << "'");
-       if (! use_regexp) {
-               LYXERR(Debug::FIND, "Searching in normal mode: par_as_string='" << par_as_string << "', str='" << str << "'");
-               LYXERR(Debug::FIND, "Searching in normal mode: lead_as_string='" << lead_as_string << "', par_as_string_nolead='" << par_as_string_nolead << "'");
-               if (at_begin) {
-                       LYXERR(Debug::FIND, "size=" << par_as_string.size() << ", substr='" << str.substr(0, par_as_string.size()) << "'");
-                       if (str.substr(0, par_as_string.size()) == par_as_string)
-                               return par_as_string.size();
-               } else {
-                       size_t pos = str.find(par_as_string_nolead);
-                       if (pos != string::npos)
-                               return par_as_string.size();
-               }
-       } else {
+
+       if (use_regexp) {
                LYXERR(Debug::FIND, "Searching in regexp mode: at_begin=" << at_begin);
-               // Try all possible regexp matches,
-               //until one that verifies the braces match test is found
-               regex const *p_regexp = at_begin ? &regexp : &regexp2;
-               sregex_iterator re_it(str.begin(), str.end(), *p_regexp);
-               sregex_iterator re_it_end;
-               for (; re_it != re_it_end; ++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))
-                               return 0;
-                       // Check braces on segments that matched all (.*?) subexpressions,
-                       // except the last "padding" one inserted by lyx.
-                       for (size_t i = 1; i < m.size() - 1; ++i)
-                               if (!braces_match(m[i].first, m[i].second))
-                                       return false;
-                       // Exclude from the returned match length any length
-                       // due to close wildcards added at end of regexp
-                       if (close_wildcards == 0)
-                               return m[0].second - m[0].first;
-                       else
-                               return m[m.size() - close_wildcards].first - m[0].first;
-               }
+               regex const & p_regexp = at_begin ? regexp : regexp2;
+               sregex_iterator re_it(str.begin(), str.end(), p_regexp);
+               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))
+                       return 0;
+       
+               // Check braces on segments that matched all (.*?) subexpressions,
+               // except the last "padding" one inserted by lyx.
+               for (size_t i = 1; i < m.size() - 1; ++i)
+                       if (!braces_match(m[i].first, m[i].second))
+                               return false;
+       
+               // Exclude from the returned match length any length
+               // due to close wildcards added at end of regexp
+               if (close_wildcards == 0)
+                       return m[0].second - m[0].first;
+
+               return m[m.size() - close_wildcards].first - m[0].first;
+       }
+
+       // else !use_regexp: but all code paths above return
+       LYXERR(Debug::FIND, "Searching in normal mode: par_as_string='"
+                                << par_as_string << "', str='" << str << "'");
+       LYXERR(Debug::FIND, "Searching in normal mode: lead_as_string='"
+                                << lead_as_string << "', par_as_string_nolead='"
+                                << par_as_string_nolead << "'");
+
+       if (at_begin) {
+               LYXERR(Debug::FIND, "size=" << par_as_string.size()
+                                        << ", substr='" << str.substr(0, par_as_string.size()) << "'");
+               if (str.substr(0, par_as_string.size()) == par_as_string)
+                       return par_as_string.size();
+       } else {
+               size_t pos = str.find(par_as_string_nolead);
+               if (pos != string::npos)
+                       return par_as_string.size();
        }
        return 0;
 }
@@ -1073,7 +1116,6 @@ docstring latexifyFromCursor(DocIterator const & cur, int len)
        LYXERR(Debug::FIND, "  with cur.lastpost=" << cur.lastpos() << ", cur.lastrow="
               << cur.lastrow() << ", cur.lastcol=" << cur.lastcol());
        Buffer const & buf = *cur.buffer();
-       LBUFERR(buf.params().isLatex());
 
        odocstringstream ods;
        otexstream os(ods);
@@ -1434,8 +1476,9 @@ static void findAdvReplace(BufferView * bv, FindAndReplaceOptions const & opt, M
                LYXERR(Debug::FIND, "Latexified replace_buffer: '" << repl_latex << "'");
                string s;
                // false positive from coverity
-               // coverity[CHECKED_RETURN]
+               // coverity[check_return]
                regex_replace(to_utf8(repl_latex), s, "\\$(.*)\\$", "$1");
+               // coverity[check_return]
                regex_replace(s, s, "\\\\\\[(.*)\\\\\\]", "$1");
                repl_latex = from_utf8(s);
                LYXERR(Debug::FIND, "Replacing by insert()ing latex: '" << repl_latex << "' cur=" << cur << " with depth=" << cur.depth());