diff --git a/src/lyxfind.cpp b/src/lyxfind.cpp index 0b6bc6509f..40e503e5f9 100644 --- a/src/lyxfind.cpp +++ b/src/lyxfind.cpp @@ -115,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; } @@ -332,21 +340,47 @@ void replace(BufferView * bv, FuncRequest const & ev, bool has_deleted) bool findNextChange(BufferView * bv) { - DocIterator cur = bv->cursor(); + return findChange(bv, true); +} - if (!findChange(cur)) + +bool findPreviousChange(BufferView * bv) +{ + return findChange(bv, false); +} + + +bool findChange(BufferView * bv, bool next) +{ + DocIterator cur = bv->cursor(); + 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: diff --git a/src/lyxfind.h b/src/lyxfind.h index 7da80af65c..9dc17df86b 100644 --- a/src/lyxfind.h +++ b/src/lyxfind.h @@ -66,6 +66,13 @@ void replace(BufferView * bv, FuncRequest const &, bool has_deleted = false); /// find the next change in the buffer bool findNextChange(BufferView * bv); +/// find the previous change in the buffer +bool findPreviousChange(BufferView * bv); + +/// find the change in the buffer +/// \param next true to find the next change, otherwise the previous +bool findChange(BufferView * bv, bool next); + // Hopefully, nobody will ever replace with something like this #define LYX_FR_NULL_STRING "__LYX__F&R__NULL__STRING__"