Skip to content

Commit 8c64a36

Browse files
committed
patch 8.0.1633: a TextChanged autocmd triggers when it is defined
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 #2742)
1 parent 47015b8 commit 8c64a36

3 files changed

Lines changed: 30 additions & 0 deletions

File tree

src/buffer.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,13 @@ open_buffer(
291291
unchanged(curbuf, FALSE);
292292
save_file_ff(curbuf); /* keep this fileformat */
293293

294+
/* Set last_changedtick to avoid triggering a TextChanged autocommand right
295+
* after it was added. */
296+
curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
297+
#ifdef FEAT_INS_EXPAND
298+
curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
299+
#endif
300+
294301
/* require "!" to overwrite the file, because it wasn't read completely */
295302
#ifdef FEAT_EVAL
296303
if (aborting())

src/testdir/test_autocmd.vim

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
" Tests for autocommands
22

3+
source shared.vim
4+
35
func! s:cleanup_buffers() abort
46
for bnr in range(1, bufnr('$'))
57
if bufloaded(bnr) && bufnr('%') != bnr
@@ -1304,3 +1306,22 @@ func Test_ChangedP()
13041306

13051307
bw!
13061308
endfunc
1309+
1310+
func Test_Changed_FirstTime()
1311+
if !has('terminal') || has('gui_running')
1312+
return
1313+
endif
1314+
" Prepare file for TextChanged event.
1315+
call writefile([''], 'Xchanged.txt')
1316+
let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
1317+
call assert_equal('running', term_getstatus(buf))
1318+
" It's only adding autocmd, so that no event occurs.
1319+
call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
1320+
call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
1321+
call WaitFor({-> term_getstatus(buf) == 'finished'})
1322+
call assert_equal([''], readfile('Xchanged.txt'))
1323+
1324+
" clean up
1325+
call delete('Xchanged.txt')
1326+
bwipe!
1327+
endfunc

src/version.c

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

767767
static int included_patches[] =
768768
{ /* Add new patch number below this line */
769+
/**/
770+
1633,
769771
/**/
770772
1632,
771773
/**/

0 commit comments

Comments
 (0)