Skip to content

Commit c369133

Browse files
committed
patch 7.4.1754
Problem: When 'filetype' was set and reloading a buffer which does not cause it to be set, the syntax isn't loaded. (KillTheMule) Solution: Remember whether the FileType event was fired and fire it if not. (Anton Lindqvist, closes #747)
1 parent c020042 commit c369133

3 files changed

Lines changed: 45 additions & 0 deletions

File tree

src/fileio.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,18 @@ static void vim_settempdir(char_u *tempdir);
141141
#endif
142142
#ifdef FEAT_AUTOCMD
143143
static char *e_auchangedbuf = N_("E812: Autocommands changed buffer or buffer name");
144+
#endif
145+
146+
#ifdef FEAT_AUTOCMD
147+
/*
148+
* Set by the apply_autocmds_group function if the given event is equal to
149+
* EVENT_FILETYPE. Used by the readfile function in order to determine if
150+
* EVENT_BUFREADPOST triggered the EVENT_FILETYPE.
151+
*
152+
* Relying on this value requires one to reset it prior calling
153+
* apply_autocmds_group.
154+
*/
155+
static int au_did_filetype INIT(= FALSE);
144156
#endif
145157

146158
void
@@ -305,6 +317,10 @@ readfile(
305317
int using_b_fname;
306318
#endif
307319

320+
#ifdef FEAT_AUTOCMD
321+
au_did_filetype = FALSE; /* reset before triggering any autocommands */
322+
#endif
323+
308324
curbuf->b_no_eol_lnum = 0; /* in case it was set by the previous read */
309325

310326
/*
@@ -2669,8 +2685,17 @@ readfile(
26692685
apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname,
26702686
FALSE, curbuf, eap);
26712687
else if (newfile)
2688+
{
26722689
apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname,
26732690
FALSE, curbuf, eap);
2691+
if (!au_did_filetype && *curbuf->b_p_ft != NUL)
2692+
/*
2693+
* EVENT_FILETYPE was not triggered but the buffer already has a
2694+
* filetype. Trigger EVENT_FILETYPE using the existing filetype.
2695+
*/
2696+
apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
2697+
TRUE, curbuf);
2698+
}
26742699
else
26752700
apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname,
26762701
FALSE, NULL, eap);
@@ -9537,6 +9562,9 @@ apply_autocmds_group(
95379562
if (event == EVENT_BUFWIPEOUT && buf != NULL)
95389563
aubuflocal_remove(buf);
95399564

9565+
if (retval == OK && event == EVENT_FILETYPE)
9566+
au_did_filetype = TRUE;
9567+
95409568
return retval;
95419569
}
95429570

src/testdir/test_syntax.vim

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,18 @@ func Test_syn_iskeyword()
6565

6666
quit!
6767
endfunc
68+
69+
func Test_syntax_after_reload()
70+
split Xsomefile
71+
call setline(1, ['hello', 'there'])
72+
w!
73+
only!
74+
setl filetype=hello
75+
au FileType hello let g:gotit = 1
76+
call assert_false(exists('g:gotit'))
77+
edit other
78+
buf Xsomefile
79+
call assert_equal('hello', &filetype)
80+
call assert_true(exists('g:gotit'))
81+
call delete('Xsomefile')
82+
endfunc

src/version.c

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

749749
static int included_patches[] =
750750
{ /* Add new patch number below this line */
751+
/**/
752+
1754,
751753
/**/
752754
1753,
753755
/**/

0 commit comments

Comments
 (0)