Skip to content

Commit

Permalink
patch 8.0.1633: a TextChanged autocmd triggers when it is defined
Browse files Browse the repository at this point in the history
Problem:    A TextChanged autocmd triggers when it is defined after creating a
            buffer.
Solution:   Set b_last_changedtick when opening a buffer. (Hirohito Highlight,
            closes vim#2742)
  • Loading branch information
brammool committed Mar 23, 2018
1 parent 47015b8 commit 8c64a36
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,13 @@ open_buffer(
unchanged(curbuf, FALSE);
save_file_ff(curbuf); /* keep this fileformat */

/* Set last_changedtick to avoid triggering a TextChanged autocommand right
* after it was added. */
curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
#ifdef FEAT_INS_EXPAND
curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
#endif

/* require "!" to overwrite the file, because it wasn't read completely */
#ifdef FEAT_EVAL
if (aborting())
Expand Down
21 changes: 21 additions & 0 deletions src/testdir/test_autocmd.vim
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
" Tests for autocommands

source shared.vim

func! s:cleanup_buffers() abort
for bnr in range(1, bufnr('$'))
if bufloaded(bnr) && bufnr('%') != bnr
Expand Down Expand Up @@ -1304,3 +1306,22 @@ func Test_ChangedP()

bw!
endfunc

func Test_Changed_FirstTime()
if !has('terminal') || has('gui_running')
return
endif
" Prepare file for TextChanged event.
call writefile([''], 'Xchanged.txt')
let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
call assert_equal('running', term_getstatus(buf))
" It's only adding autocmd, so that no event occurs.
call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
call WaitFor({-> term_getstatus(buf) == 'finished'})
call assert_equal([''], readfile('Xchanged.txt'))

" clean up
call delete('Xchanged.txt')
bwipe!
endfunc
2 changes: 2 additions & 0 deletions src/version.c
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,8 @@ static char *(features[]) =

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

0 comments on commit 8c64a36

Please sign in to comment.