Skip to content

Commit 1814183

Browse files
committed
patch 8.0.0677: setting 'filetype' may switch buffers
Problem: Setting 'filetype' internally may cause the current buffer and window to change unexpectedly. Solution: Set curbuf_lock. (closes #1734)
1 parent 182a17b commit 1814183

5 files changed

Lines changed: 32 additions & 1 deletion

File tree

src/ex_cmds.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6835,7 +6835,11 @@ fix_help_buffer(void)
68356835
#ifdef FEAT_AUTOCMD
68366836
/* Set filetype to "help" if still needed. */
68376837
if (STRCMP(curbuf->b_p_ft, "help") != 0)
6838+
{
6839+
++curbuf_lock;
68386840
set_option_value((char_u *)"ft", 0L, (char_u *)"help", OPT_LOCAL);
6841+
--curbuf_lock;
6842+
}
68396843
#endif
68406844

68416845
#ifdef FEAT_SYN_HL

src/ex_getln.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6878,6 +6878,8 @@ open_cmdwin(void)
68786878
# ifdef FEAT_AUTOCMD
68796879
/* Do execute autocommands for setting the filetype (load syntax). */
68806880
unblock_autocmds();
6881+
/* But don't allow switching to another buffer. */
6882+
++curbuf_lock;
68816883
# endif
68826884

68836885
/* Showing the prompt may have set need_wait_return, reset it. */
@@ -6893,6 +6895,9 @@ open_cmdwin(void)
68936895
}
68946896
set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
68956897
}
6898+
# ifdef FEAT_AUTOCMD
6899+
--curbuf_lock;
6900+
# endif
68966901

68976902
/* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
68986903
* sets 'textwidth' to 78). */

src/quickfix.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3425,6 +3425,9 @@ qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
34253425
/* Set the 'filetype' to "qf" each time after filling the buffer.
34263426
* This resembles reading a file into a buffer, it's more logical when
34273427
* using autocommands. */
3428+
#ifdef FEAT_AUTOCMD
3429+
++curbuf_lock;
3430+
#endif
34283431
set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL);
34293432
curbuf->b_p_ma = FALSE;
34303433

@@ -3435,6 +3438,7 @@ qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last)
34353438
apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL,
34363439
FALSE, curbuf);
34373440
keep_filetype = FALSE;
3441+
--curbuf_lock;
34383442
#endif
34393443
/* make sure it will be redrawn */
34403444
redraw_curbuf_later(NOT_VALID);

src/testdir/test_quickfix.vim

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2190,7 +2190,7 @@ endfunc
21902190
func Test_cclose_from_copen()
21912191
augroup QF_Test
21922192
au!
2193-
au FileType qf :cclose
2193+
au FileType qf :call assert_fails(':cclose', 'E788')
21942194
augroup END
21952195
copen
21962196
augroup QF_Test
@@ -2227,3 +2227,19 @@ func Test_Qf_Size()
22272227
call XsizeTests('c')
22282228
call XsizeTests('l')
22292229
endfunc
2230+
2231+
func Test_cclose_in_autocmd()
2232+
" Problem is only triggered if "starting" is zero, so that the OptionsSet
2233+
" event will be triggered.
2234+
call test_override('starting', 1)
2235+
augroup QF_Test
2236+
au!
2237+
au FileType qf :call assert_fails(':cclose', 'E788')
2238+
augroup END
2239+
copen
2240+
augroup QF_Test
2241+
au!
2242+
augroup END
2243+
augroup! QF_Test
2244+
call test_override('starting', 0)
2245+
endfunc

src/version.c

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

765765
static int included_patches[] =
766766
{ /* Add new patch number below this line */
767+
/**/
768+
677,
767769
/**/
768770
676,
769771
/**/

0 commit comments

Comments
 (0)