From 469b43c791339b047112e39a078a3daf800c0699 Mon Sep 17 00:00:00 2001 From: Kornel Benko Date: Fri, 22 Jan 2021 17:03:54 +0100 Subject: [PATCH] FindAdv: Do not search in deleted text. --- src/lyxfind.cpp | 88 ++++++++++++++++++++------------------------ src/output_latex.cpp | 8 ++-- 2 files changed, 43 insertions(+), 53 deletions(-) diff --git a/src/lyxfind.cpp b/src/lyxfind.cpp index 053f19ab71..be529ca19a 100644 --- a/src/lyxfind.cpp +++ b/src/lyxfind.cpp @@ -3622,10 +3622,10 @@ int findForwardAdv(DocIterator & cur, MatchStringAdv & match) else { // match_len > 0 // Try to find the begin of searched string int increment; - int firstInvalid = 100000; + int firstInvalid = cur.lastpos() - cur.pos(); { int incrmatch = (mres.match_prefix + mres.pos - mres.leadsize + 1)*3/4; - int incrcur = (cur.lastpos() - cur.pos() + 1 )*3/4; + int incrcur = (firstInvalid + 1 )*3/4; if (incrcur < incrmatch) increment = incrcur; else @@ -3636,54 +3636,44 @@ int findForwardAdv(DocIterator & cur, MatchStringAdv & match) LYXERR(Debug::FIND, "Set increment to " << increment); while (increment > 0) { DocIterator old_cur = cur; - size_t skipping = cur.depth(); - for (int i = 0; i < increment && cur; i++) { - cur.forwardPos(); - while (cur && cur.depth() > skipping) { - cur.pos() = cur.lastpos(); - cur.forwardPos(); - } - } - if (! cur || (cur.pit() > old_cur.pit())) { - // Are we outside of the paragraph? - // This can happen if moving past some UTF8-encoded chars - cur = old_cur; + if (cur.pos() + increment >= cur.lastpos()) { increment /= 2; + continue; } - else { - MatchResult mres2 = match(cur, -1, false); - displayMres(mres2, "findForwardAdv loop", cur) - switch (interpretMatch(mres, mres2)) { + cur.pos() = cur.pos() + increment; + MatchResult mres2 = match(cur, -1, false); + displayMres(mres2, "findForwardAdv loop", cur) + switch (interpretMatch(mres, mres2)) { case MatchResult::newIsTooFar: - // behind the expected match - firstInvalid = increment; - cur = old_cur; - increment /= 2; - break; + // behind the expected match + firstInvalid = increment; + cur = old_cur; + increment /= 2; + break; case MatchResult::newIsBetter: - // not reached yet, but cur.pos()+increment is bettert - mres = mres2; - firstInvalid -= increment; - if (increment > firstInvalid*3/4) - increment = firstInvalid*3/4; - if ((mres2.pos == mres2.leadsize) && (increment >= mres2.match_prefix)) { - if (increment >= mres2.match_prefix) - increment = (mres2.match_prefix+1)*3/4; - } - break; + // not reached yet, but cur.pos()+increment is bettert + mres = mres2; + firstInvalid -= increment; + if (increment > firstInvalid*3/4) + increment = firstInvalid*3/4; + if ((mres2.pos == mres2.leadsize) && (increment >= mres2.match_prefix)) { + if (increment >= mres2.match_prefix) + increment = (mres2.match_prefix+1)*3/4; + } + break; default: - // Todo@ - // Handle not like MatchResult::newIsTooFar - LYXERR0( "Probably too far: Increment = " << increment << " match_prefix = " << mres.match_prefix); - firstInvalid--; - increment = increment*3/4; - cur = old_cur; - break; - } + // Todo@ + // Handle not like MatchResult::newIsTooFar + LYXERR0( "Probably too far: Increment = " << increment << " match_prefix = " << mres.match_prefix); + firstInvalid--; + increment = increment*3/4; + cur = old_cur; + break; } } if (mres.match_len > 0) { if (mres.match_prefix + mres.pos - mres.leadsize > 0) { + // The match seems to indicate some deeper level repeat = true; orig_cur = cur; orig_mres = mres; @@ -3692,7 +3682,7 @@ int findForwardAdv(DocIterator & cur, MatchStringAdv & match) } } else if (repeat) { - // seems to never be reached. + // should never be reached. cur = orig_cur; mres = orig_mres; } @@ -3700,15 +3690,15 @@ int findForwardAdv(DocIterator & cur, MatchStringAdv & match) LYXERR(Debug::FIND, "Finalizing 1"); MatchResult found_match = findAdvFinalize(cur, match, mres); if (found_match.match_len > 0) { - LASSERT(found_match.pos_len > 0, /**/); - match.FillResults(found_match); - return found_match.pos_len; + LASSERT(found_match.pos_len > 0, /**/); + match.FillResults(found_match); + return found_match.pos_len; } else { - // try next possible match - cur.forwardPos(); - repeat = false; - continue; + // try next possible match + cur.forwardPos(); + repeat = false; + continue; } } } diff --git a/src/output_latex.cpp b/src/output_latex.cpp index fe50442064..2f5d964a47 100644 --- a/src/output_latex.cpp +++ b/src/output_latex.cpp @@ -444,7 +444,7 @@ void TeXEnvironment(Buffer const & buf, Text const & text, if ((par->layout() != nextpar->layout() || par->params().depth() == nextpar->params().depth() || par->params().leftIndent() == nextpar->params().leftIndent()) - && !runparams.for_search && !cpar.empty() + && !cpar.empty() && cpar.isDeleted(0, cpar.size()) && !buf.params().output_changes) { if (!buf.params().output_changes && !cpar.parEndChange().deleted()) os << '\n' << '\n'; @@ -759,7 +759,7 @@ void TeXOnePar(Buffer const & buf, // Do not output empty commands if the whole paragraph has // been deleted with ct and changes are not output. - if (!runparams_in.for_search && style.latextype != LATEX_ENVIRONMENT + if (style.latextype != LATEX_ENVIRONMENT && !par.empty() && par.isDeleted(0, par.size()) && !bparams.output_changes) return; @@ -1665,7 +1665,7 @@ void latexParagraphs(Buffer const & buf, if ((par->layout() != nextpar->layout() || par->params().depth() == nextpar->params().depth() || par->params().leftIndent() == nextpar->params().leftIndent()) - && !runparams.for_search && !cpar.empty() + && !cpar.empty() && cpar.isDeleted(0, cpar.size()) && !bparams.output_changes) { if (!cpar.parEndChange().deleted()) os << '\n' << '\n'; @@ -1674,7 +1674,7 @@ void latexParagraphs(Buffer const & buf, } else { // This is the last par Paragraph const & cpar = paragraphs.at(pit); - if (!runparams.for_search && !cpar.empty() + if (!cpar.empty() && cpar.isDeleted(0, cpar.size()) && !bparams.output_changes) { if (!cpar.parEndChange().deleted()) os << '\n' << '\n';