Skip to content

Commit a3a7d10

Browse files
zeertzjqchrisbra
authored andcommitted
patch 9.1.1221: Wrong cursor pos when leaving Insert mode just after 'autoindent'
Problem: Wrong cursor position and '^' mark when leaving Insert mode just after 'autoindent' and cursor on last char of line. Solution: Don't move cursor to NUL when it wasn't moved to the left (zeertzjq). fixes: #15581 related: neovim/neovim#30165 neovim/neovim#32943 closes: #16922 Signed-off-by: zeertzjq <zeertzjq@outlook.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
1 parent 18a6853 commit a3a7d10

3 files changed

Lines changed: 62 additions & 1 deletion

File tree

src/edit.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2512,6 +2512,7 @@ stop_insert(
25122512
&& end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
25132513
{
25142514
pos_T tpos = curwin->w_cursor;
2515+
colnr_T prev_col = end_insert_pos->col;
25152516

25162517
curwin->w_cursor = *end_insert_pos;
25172518
check_cursor_col(); // make sure it is not past the line
@@ -2527,7 +2528,7 @@ stop_insert(
25272528
}
25282529
if (curwin->w_cursor.lnum != tpos.lnum)
25292530
curwin->w_cursor = tpos;
2530-
else
2531+
else if (curwin->w_cursor.col < prev_col)
25312532
{
25322533
// reset tpos, could have been invalidated in the loop above
25332534
tpos = curwin->w_cursor;

src/testdir/test_edit.vim

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,64 @@ func Test_autoindent_remove_indent()
460460
call delete('Xarifile')
461461
endfunc
462462

463+
func Test_edit_esc_after_CR_autoindent()
464+
new
465+
setlocal autoindent
466+
autocmd InsertLeavePre * let g:prev_cursor = getpos('.')
467+
468+
call setline(1, 'foobar')
469+
exe "normal! $hi\<CR>\<Esc>"
470+
call assert_equal(['foob', 'ar'], getline(1, '$'))
471+
call assert_equal([0, 2, 1, 0], getpos('.'))
472+
call assert_equal([0, 2, 1, 0], getpos("'^"))
473+
call assert_equal([0, 2, 1, 0], g:prev_cursor)
474+
%d
475+
476+
call setline(1, 'foobar')
477+
exe "normal! $i\<CR>\<Esc>"
478+
call assert_equal(['fooba', 'r'], getline(1, '$'))
479+
call assert_equal([0, 2, 1, 0], getpos('.'))
480+
call assert_equal([0, 2, 1, 0], getpos("'^"))
481+
call assert_equal([0, 2, 1, 0], g:prev_cursor)
482+
%d
483+
484+
call setline(1, 'foobar')
485+
exe "normal! A\<CR>\<Esc>"
486+
call assert_equal(['foobar', ''], getline(1, '$'))
487+
call assert_equal([0, 2, 1, 0], getpos('.'))
488+
call assert_equal([0, 2, 1, 0], getpos("'^"))
489+
call assert_equal([0, 2, 1, 0], g:prev_cursor)
490+
%d
491+
492+
call setline(1, ' foobar')
493+
exe "normal! $hi\<CR>\<Esc>"
494+
call assert_equal([' foob', ' ar'], getline(1, '$'))
495+
call assert_equal([0, 2, 2, 0], getpos('.'))
496+
call assert_equal([0, 2, 3, 0], getpos("'^"))
497+
call assert_equal([0, 2, 3, 0], g:prev_cursor)
498+
%d
499+
500+
call setline(1, ' foobar')
501+
exe "normal! $i\<CR>\<Esc>"
502+
call assert_equal([' fooba', ' r'], getline(1, '$'))
503+
call assert_equal([0, 2, 2, 0], getpos('.'))
504+
call assert_equal([0, 2, 3, 0], getpos("'^"))
505+
call assert_equal([0, 2, 3, 0], g:prev_cursor)
506+
%d
507+
508+
call setline(1, ' foobar')
509+
exe "normal! A\<CR>\<Esc>"
510+
call assert_equal([' foobar', ''], getline(1, '$'))
511+
call assert_equal([0, 2, 1, 0], getpos('.'))
512+
call assert_equal([0, 2, 1, 0], getpos("'^"))
513+
call assert_equal([0, 2, 1, 0], g:prev_cursor)
514+
%d
515+
516+
autocmd! InsertLeavePre
517+
unlet g:prev_cursor
518+
bwipe!
519+
endfunc
520+
463521
func Test_edit_CR()
464522
" Test for <CR> in insert mode
465523
" basically only in quickfix mode it's tested, the rest

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,8 @@ static char *(features[]) =
704704

705705
static int included_patches[] =
706706
{ /* Add new patch number below this line */
707+
/**/
708+
1221,
707709
/**/
708710
1220,
709711
/**/

0 commit comments

Comments
 (0)