}
-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;
}
bool findNextChange(BufferView * bv)
{
- DocIterator cur = bv->cursor();
+ return findChange(bv, true);
+}
+
+
+bool findPreviousChange(BufferView * bv)
+{
+ return findChange(bv, false);
+}
+
+
+bool findChange(BufferView * bv, bool next)
+{
+ if (bv->cursor().selection()) {
+ // set the cursor at the beginning or at the end of the selection
+ // before searching. Otherwise, the current change will be found.
+ if (next != bv->cursor().top() > bv->cursor().anchor())
+ bv->cursor().setCursorToAnchor();
+ }
- if (!findChange(cur))
+ 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: