X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Flyxfind.cpp;h=4c8846c5eb8c7c0db7b036d6f9de14503381e8ff;hb=fa8dae4201fc25da685a21de0f8968678df8f119;hp=937838d51b7f13a0d888d5ae34f65c3d17a61b1a;hpb=df8e614237e6ca81208eb7b141bdc548455b6528;p=lyx.git diff --git a/src/lyxfind.cpp b/src/lyxfind.cpp index 937838d51b..4c8846c5eb 100644 --- a/src/lyxfind.cpp +++ b/src/lyxfind.cpp @@ -67,7 +67,7 @@ bool parse_bool(docstring & howto) } -class MatchString : public binary_function +class MatchString : public binary_function { public: MatchString(docstring const & str, bool cs, bool mw) @@ -76,7 +76,7 @@ public: // returns true if the specified string is at the specified position // del specifies whether deleted strings in ct mode will be considered - bool operator()(Paragraph const & par, pos_type pos, bool del = true) const + int operator()(Paragraph const & par, pos_type pos, bool del = true) const { return par.find(str, case_sens, whole_words, pos, del); } @@ -91,27 +91,31 @@ private: }; -bool findForward(DocIterator & cur, MatchString const & match, +int findForward(DocIterator & cur, MatchString const & match, bool find_del = true) { for (; cur; cur.forwardChar()) - if (cur.inTexted() && - match(cur.paragraph(), cur.pos(), find_del)) - return true; - return false; + if (cur.inTexted()) { + int len = match(cur.paragraph(), cur.pos(), find_del); + if (len > 0) + return len; + } + return 0; } -bool findBackwards(DocIterator & cur, MatchString const & match, +int findBackwards(DocIterator & cur, MatchString const & match, bool find_del = true) { while (cur) { cur.backwardChar(); - if (cur.inTexted() && - match(cur.paragraph(), cur.pos(), find_del)) - return true; + if (cur.inTexted()) { + int len = match(cur.paragraph(), cur.pos(), find_del); + if (len > 0) + return len; + } } - return false; + return 0; } @@ -152,13 +156,13 @@ bool findOne(BufferView * bv, docstring const & searchstr, MatchString const match(searchstr, case_sens, whole); - bool found = forward ? findForward(cur, match, find_del) : + int match_len = forward ? findForward(cur, match, find_del) : findBackwards(cur, match, find_del); - if (found) - bv->putSelectionAt(cur, searchstr.length(), !forward); + if (match_len > 0) + bv->putSelectionAt(cur, match_len, !forward); - return found; + return match_len > 0; } @@ -181,12 +185,13 @@ int replaceAll(BufferView * bv, Cursor cur(*bv); cur.setCursor(doc_iterator_begin(&buf)); - while (findForward(cur, match, false)) { + int match_len = findForward(cur, match, false); + while (match_len > 0) { // Backup current cursor position and font. pos_type const pos = cur.pos(); Font const font = cur.paragraph().getFontSettings(buf.params(), pos); cur.recordUndo(); - int striked = ssize - cur.paragraph().eraseChars(pos, pos + ssize, + int striked = ssize - cur.paragraph().eraseChars(pos, pos + match_len, buf.params().trackChanges); cur.paragraph().insert(pos, replacestr, font, Change(buf.params().trackChanges ? @@ -194,6 +199,7 @@ int replaceAll(BufferView * bv, for (int i = 0; i < rsize + striked; ++i) cur.forwardChar(); ++num; + match_len = findForward(cur, match, false); } bv->putSelectionAt(doc_iterator_begin(&buf), 0, false); @@ -772,7 +778,12 @@ int MatchStringAdv::findAux(DocIterator const & cur, int len, bool at_begin) con 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); + string t = par_as_string; + while (regex_replace(t, t, "\\\\(emph|textbf|subsubsection|subsection|section|subparagraph|paragraph|part)\\{", "") + || regex_replace(t, t, "^\\$", "") + || regex_replace(t, t, "^\\\\\\[ ", "")) + LYXERR(Debug::FIND, " after removing leading $, \\[ , \\emph{, \\textbf{, etc.: " << t); + size_t pos = str.find(t); if (pos != string::npos) return par_as_string.size(); } @@ -787,11 +798,11 @@ int MatchStringAdv::findAux(DocIterator const & cur, int len, bool at_begin) con match_results 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)) + if (!braces_match(m[0].first, m[0].second, open_braces)) return 0; // Check braces on segments that matched all (.*?) subexpressions. for (size_t i = 1; i < m.size(); ++i) - if (! braces_match(m[i].first, m[i].second)) + 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 @@ -844,7 +855,7 @@ string MatchStringAdv::normalize(docstring const & s) const t.replace(pos, 1, " "); // Remove stale empty \emph{}, \textbf{} and similar blocks from latexify LYXERR(Debug::FIND, "Removing stale empty \\emph{}, \\textbf{}, \\*section{} macros from: " << t); - while (regex_replace(t, t, "\\\\(emph|textbf|subsubsection|subsection|section|subparagraph|paragraph)(\\{\\})+", "")) + while (regex_replace(t, t, "\\\\(emph|textbf|subsubsection|subsection|section|subparagraph|paragraph|part)(\\{\\})+", "")) LYXERR(Debug::FIND, " further removing stale empty \\emph{}, \\textbf{} macros from: " << t); return t; }