]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 8.1.0671: cursor in the wrong column after auto-formatting v8.1.0671
authorBram Moolenaar <Bram@vim.org>
Mon, 31 Dec 2018 22:58:24 +0000 (23:58 +0100)
committerBram Moolenaar <Bram@vim.org>
Mon, 31 Dec 2018 22:58:24 +0000 (23:58 +0100)
Problem:    Cursor in the wrong column after auto-formatting.
Solution:   Check for deleting more spaces than adding. (closes #3748)

src/mark.c
src/misc1.c
src/ops.c
src/proto/mark.pro
src/testdir/test_textformat.vim
src/version.c

index f9e3ce3cf4606dc0de963b8bf327b4e747a25d7e..e3e209c4a8721124923381e2dab1ba96e4783ddc 100644 (file)
@@ -1211,6 +1211,8 @@ mark_adjust_internal(
            posp->lnum += lnum_amount; \
            if (col_amount < 0 && posp->col <= (colnr_T)-col_amount) \
                posp->col = 0; \
+           else if (posp->col < spaces_removed) \
+               posp->col = col_amount + spaces_removed; \
            else \
                posp->col += col_amount; \
        } \
@@ -1220,13 +1222,16 @@ mark_adjust_internal(
  * Adjust marks in line "lnum" at column "mincol" and further: add
  * "lnum_amount" to the line number and add "col_amount" to the column
  * position.
+ * "spaces_removed" is the number of spaces that were removed, matters when the
+ * cursor is inside them.
  */
     void
 mark_col_adjust(
     linenr_T   lnum,
     colnr_T    mincol,
     long       lnum_amount,
-    long       col_amount)
+    long       col_amount,
+    int                spaces_removed)
 {
     int                i;
     int                fnum = curbuf->b_fnum;
index a5527510057bc858904cfdf4f74825ac463bc3a9..00a549d66ca412c65f29458390bfac0434ec0064 100644 (file)
@@ -1705,7 +1705,7 @@ open_line(
                if (flags & OPENLINE_MARKFIX)
                    mark_col_adjust(curwin->w_cursor.lnum,
                                         curwin->w_cursor.col + less_cols_off,
-                                                       1L, (long)-less_cols);
+                                                     1L, (long)-less_cols, 0);
            }
            else
                changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
index 004d5093c213b622a3e1ab9cb78c429bdef64252..58f201f2909c772a3564641fe049e488c768231c 100644 (file)
--- a/src/ops.c
+++ b/src/ops.c
@@ -4707,6 +4707,8 @@ do_join(
      */
     for (t = count - 1; ; --t)
     {
+       int spaces_removed;
+
        cend -= currsize;
        mch_memmove(cend, curr, (size_t)currsize);
        if (spaces[t] > 0)
@@ -4714,8 +4716,13 @@ do_join(
            cend -= spaces[t];
            vim_memset(cend, ' ', (size_t)(spaces[t]));
        }
+
+       // If deleting more spaces than adding, the cursor moves no more than
+       // what is added if it is inside these spaces.
+       spaces_removed = (curr - curr_start) - spaces[t];
+
        mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
-                        (long)(cend - newp + spaces[t] - (curr - curr_start)));
+                        (long)(cend - newp - spaces_removed), spaces_removed);
        if (t == 0)
            break;
        curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
@@ -5225,7 +5232,7 @@ format_lines(
                {
                    (void)del_bytes((long)next_leader_len, FALSE, FALSE);
                    mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
-                                                     (long)-next_leader_len);
+                                                     (long)-next_leader_len, 0);
                } else
 #endif
                    if (second_indent > 0)  /* the "leader" for FO_Q_SECOND */
@@ -5236,7 +5243,7 @@ format_lines(
                    {
                        (void)del_bytes(indent, FALSE, FALSE);
                        mark_col_adjust(curwin->w_cursor.lnum,
-                                              (colnr_T)0, 0L, (long)-indent);
+                                              (colnr_T)0, 0L, (long)-indent, 0);
                    }
                }
                curwin->w_cursor.lnum--;
index 623a0599e169443fc242ad778932a603225bd6b5..150e986c52a594d69f45afc974abcc72c9b0e62f 100644 (file)
@@ -21,7 +21,7 @@ void ex_clearjumps(exarg_T *eap);
 void ex_changes(exarg_T *eap);
 void mark_adjust(linenr_T line1, linenr_T line2, long amount, long amount_after);
 void mark_adjust_nofold(linenr_T line1, linenr_T line2, long amount, long amount_after);
-void mark_col_adjust(linenr_T lnum, colnr_T mincol, long lnum_amount, long col_amount);
+void mark_col_adjust(linenr_T lnum, colnr_T mincol, long lnum_amount, long col_amount, int spaces_removed);
 void cleanup_jumplist(win_T *wp, int loadfiles);
 void copy_jumplist(win_T *from, win_T *to);
 void free_jumplist(win_T *wp);
index 377f761b7437ec15a5299cb8c1f998deee895d1e..0f8e09532b26bfe4897f675e49c19f7e8871d95d 100644 (file)
@@ -450,5 +450,16 @@ func Test_format_undo()
              \ ], getline(1, '$'))
 
   unmap gg
+  set tw&
   enew!
 endfunc
+
+func Test_format_list_auto()
+  new
+  call setline(1, ['1. abc', '2. def', '3.  ghi'])
+  set fo=tan ai bs=2
+  call feedkeys("3G0lli\<BS>\<BS>x\<Esc>", 'tx')
+  call assert_equal('2. defx ghi', getline(2))
+  bwipe!
+  set fo& ai& bs&
+endfunc
index 222624124cde18a31c70f12b3f3b803d62bceb9d..3de44618f907fa8f6b48409bd06d0430dd21b0b0 100644 (file)
@@ -799,6 +799,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    671,
 /**/
     670,
 /**/