]> git.lyx.org Git - features.git/commitdiff
DEPM: some factorization beween both versions
authorJean-Marc Lasgouttes <lasgouttes@lyx.org>
Tue, 29 Jan 2019 13:38:17 +0000 (14:38 +0100)
committerJean-Marc Lasgouttes <lasgouttes@lyx.org>
Tue, 29 Jan 2019 13:42:13 +0000 (14:42 +0100)
Additionally, correct the cursor by an offset equal to the variation of paragraph size : if change tracking is on, deleting a space may mean that it is just marked as deleted.

Part of bug #11412.

src/BufferView.cpp
src/Text2.cpp

index af372877d8e57ab51ce61d0ed7d1445e48d426d8..7edf9e25cfbb5610644e09ec9a55f28e90b65db1 100644 (file)
@@ -2608,7 +2608,7 @@ bool BufferView::checkDepm(Cursor & cur, Cursor & old)
                return false;
 
        bool need_anchor_change = false;
-       bool changed = d->cursor_.text()->deleteEmptyParagraphMechanism(cur, old,
+       bool changed = Text::deleteEmptyParagraphMechanism(cur, old,
                need_anchor_change);
 
        if (need_anchor_change)
index fe6d1fbcf1305830d31925f4d7eebcda4f75584b..356e4e8dd18bd29742c6458dfce32305a9b176c0 100644 (file)
@@ -779,6 +779,32 @@ bool Text::cursorDownParagraph(Cursor & cur)
        return updated;
 }
 
+namespace {
+
+void deleteSpaces(Paragraph & par, pos_type const from, pos_type to,
+                                 int num_spaces, bool const trackChanges)
+{
+       if (!num_spaces)
+               return;
+
+       // First, delete spaces marked as inserted
+       int pos = from;
+       while (pos < to && num_spaces > 0) {
+               Change const & change = par.lookupChange(pos);
+               if (change.inserted() && change.currentAuthor()) {
+                       par.eraseChar(pos, trackChanges);
+                       --num_spaces;
+                       --to;
+               } else
+                       ++pos;
+       }
+
+       // Then remove remaining spaces
+       par.eraseChars(from, from + num_spaces, trackChanges);
+}
+
+}
+
 
 bool Text::deleteEmptyParagraphMechanism(Cursor & cur,
                Cursor & old, bool & need_anchor_change)
@@ -848,26 +874,14 @@ bool Text::deleteEmptyParagraphMechanism(Cursor & cur,
 
                // Remove spaces and adapt cursor.
                if (num_spaces > 0) {
-                       // delete first spaces marked as inserted
-                       int pos = from;
-                       int ns = num_spaces;
-                       while (pos < to && ns > 0) {
-                               Change const & change = oldpar.lookupChange(pos);
-                               if (change.inserted() && change.currentAuthor()) {
-                                       oldpar.eraseChar(pos, cur.buffer()->params().track_changes);
-                                       --ns;
-                                       --to;
-                               } else
-                                       ++pos;
-                       }
-
-                       // Then remove remaining spaces
-                       oldpar.eraseChars(from, from + ns, cur.buffer()->params().track_changes);
+                       pos_type const oldsize = oldpar.size();
+                       deleteSpaces(oldpar, from, to, num_spaces,
+                               cur.buffer()->params().track_changes);
                        // correct cur position
                        // FIXME: there can be other cursors pointing there, we should update them
                        if (same_par) {
                                if (cur[depth].pos() >= to)
-                                       cur[depth].pos() -= num_spaces;
+                                       cur[depth].pos() -= oldsize - oldpar.size();
                                else if (cur[depth].pos() > from)
                                        cur[depth].pos() = min(from + 1, old.lastpos());
                                need_anchor_change = true;
@@ -952,12 +966,15 @@ void Text::deleteEmptyParagraphMechanism(pit_type first, pit_type last, bool tra
                        // empty? We are done
                        if (from == to)
                                break;
-                       // if inside the line, keep one space
-                       if (from > 0 && to < par.size())
-                               ++from;
-                       // remove the extra spaces
-                       if (from < to)
-                               par.eraseChars(from, to, trackChanges);
+
+                       int num_spaces = to - from;
+
+                       // If we are not at the extremity of the paragraph, keep one space
+                       if (from != to && from > 0 && to < par.size())
+                               --num_spaces;
+
+                       // Remove spaces if needed
+                       deleteSpaces(par, from , to, num_spaces, trackChanges);
                }
 
                // don't delete anything if this is the only remaining paragraph