Skip to content

Commit

Permalink
patch 9.0.2031: TextChangedI may be triggered by non-insert mode change
Browse files Browse the repository at this point in the history
Problem:  `TextChangedI` can trigger on entering Insert mode if there
          was previously a change not in Insert mode.
Solution: Make it trigger only when text is actually changed in Insert
          mode.

closes: #13265
closes: #13338

Signed-off-by: Christian Brabandt <cb@256bit.org>
Co-authored-by: Evgeni Chasnovski <evgeni.chasnovski@gmail.com>
  • Loading branch information
echasnovski authored and chrisbra committed Oct 15, 2023
1 parent 47510f3 commit d7ae263
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/edit.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ edit(
return FALSE;
}
ins_compl_clear(); // clear stuff for CTRL-X mode
// Reset Changedtick_i, so that TextChangedI will only be triggered for stuff
// from insert mode
curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);

/*
* Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
Expand Down Expand Up @@ -840,6 +843,7 @@ edit(
if (cmdchar != 'r' && cmdchar != 'v' && c != Ctrl_C)
ins_apply_autocmds(EVENT_INSERTLEAVE);
did_cursorhold = FALSE;
curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
return (c == Ctrl_O);
}
continue;
Expand Down
23 changes: 17 additions & 6 deletions src/testdir/test_autocmd.vim
Original file line number Diff line number Diff line change
Expand Up @@ -2566,27 +2566,28 @@ func Test_ChangedP()
call cursor(3, 1)
let g:autocmd = ''
call feedkeys("o\<esc>", 'tnix')
call assert_equal('I', g:autocmd)
" `TextChangedI` triggers only if text is actually changed in Insert mode
call assert_equal('', g:autocmd)

let g:autocmd = ''
call feedkeys("Sf", 'tnix')
call assert_equal('II', g:autocmd)
call assert_equal('I', g:autocmd)

let g:autocmd = ''
call feedkeys("Sf\<C-N>", 'tnix')
call assert_equal('IIP', g:autocmd)
call assert_equal('IP', g:autocmd)

let g:autocmd = ''
call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
call assert_equal('IIPP', g:autocmd)
call assert_equal('IPP', g:autocmd)

let g:autocmd = ''
call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
call assert_equal('IIPPP', g:autocmd)
call assert_equal('IPPP', g:autocmd)

let g:autocmd = ''
call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
call assert_equal('IIPPPP', g:autocmd)
call assert_equal('IPPPP', g:autocmd)

call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
" TODO: how should it handle completeopt=noinsert,noselect?
Expand Down Expand Up @@ -3610,6 +3611,16 @@ func Test_Changed_ChangedI()
" call assert_equal('N4', g:autocmd_n)
call assert_equal('I3', g:autocmd_i)

" TextChangedI should only trigger if change was done in Insert mode
let g:autocmd_i = ''
call feedkeys("yypi\<esc>", 'tnix')
call assert_equal('', g:autocmd_i)

" TextChanged should only trigger if change was done in Normal mode
let g:autocmd_n = ''
call feedkeys("ibar\<esc>", 'tnix')
call assert_equal('', g:autocmd_n)

" CleanUp
call test_override("char_avail", 0)
au! TextChanged <buffer>
Expand Down
2 changes: 2 additions & 0 deletions src/version.c
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,8 @@ static char *(features[]) =

static int included_patches[] =
{ /* Add new patch number below this line */
/**/
2031,
/**/
2030,
/**/
Expand Down

0 comments on commit d7ae263

Please sign in to comment.