commit 484128d05b95a014b4511bff891bf90c17f69d72
Author: Jean-Marc Lasgouttes <lasgout...@lyx.org>
Date:   Tue Jan 29 10:55:12 2019 +0100

    Fix position of cursor when DEPM is used in change tracking mode
    
    When change tracking is active, it may happen that spaces are marked
    as deleted instead of being removed. Therefore it is better to measure
    the change of size of the paragraph to know how to offset the cursor.
    
    Moreover, it the cursor was strictly after the start of the sequence
    of spaces, we ensure that it is still the case.
    
    This commit is _not_ a backport from master, which has diverged.
    
    Part of bug #11412/
---
 src/Text2.cpp |   10 +++++++---
 1 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/src/Text2.cpp b/src/Text2.cpp
index 34fe1ca..e73d1cc 100644
--- a/src/Text2.cpp
+++ b/src/Text2.cpp
@@ -789,7 +789,7 @@ namespace {
 // fix the cursor `cur' after characters has been deleted at `where'
 // position. Called by deleteEmptyParagraphMechanism
 void fixCursorAfterDelete(CursorSlice & cur, CursorSlice const & where,
-                                                 pos_type from, pos_type to)
+                          pos_type from, int num_chars)
 {
        // Do nothing if cursor is not in the paragraph where the
        // deletion occurred,
@@ -798,7 +798,7 @@ void fixCursorAfterDelete(CursorSlice & cur, CursorSlice 
const & where,
 
        // If cursor position is after the deletion place update it
        if (cur.pos() > from)
-               cur.pos() = max(from, cur.pos() - (to - from));
+               cur.pos() = max(from + 1, cur.pos() - num_chars);
 
        // Check also if we don't want to set the cursor on a spot behind the
        // pagragraph because we erased the last character.
@@ -874,6 +874,10 @@ bool Text::deleteEmptyParagraphMechanism(Cursor & cur,
 
                // Remove spaces and adapt cursor.
                if (from < to) {
+                       // we need to remember what the size was because
+                       // eraseChars might mark spaces as deleted instead of
+                       // removing them.
+                       int const oldsize = oldpar.size();
                        oldpar.eraseChars(from, to, 
cur.buffer()->params().track_changes);
 // FIXME: This will not work anymore when we have multiple views of the same 
buffer
 // In this case, we will have to correct also the cursors held by
@@ -881,7 +885,7 @@ bool Text::deleteEmptyParagraphMechanism(Cursor & cur,
 // automated way in CursorSlice code. (JMarc 26/09/2001)
                        // correct all cursor parts
                        if (same_par) {
-                               fixCursorAfterDelete(cur[depth], old.top(), 
from, to);
+                               fixCursorAfterDelete(cur[depth], old.top(), 
from, oldsize - oldpar.size());
                                need_anchor_change = true;
                        }
                        return true;

Reply via email to