From bd33a4337da84f2081eef40d4d029b2247797d28 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 11 Oct 2017 22:25:21 +0200 Subject: [PATCH 1/5] Do not trigger filetypeindent/filetypeplugin autocmds by default This is not necessary if `filetype plugin indent on` was not used before `plug#end()`, since then the `FileType` autocmds from there will come after vim-plug's. This will issue a warning, and makes handling of this conditional. This could use `filetype plugin/indent off` to work around this (similar to the `filetype off` being used), but `runtime/indoff.vim` and `runtime/ftplugof.vim` will only empty the augroups, and not remove them. Fixing the user's config is the best solution anyway, so I think a warning is good. --- plug.vim | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/plug.vim b/plug.vim index 9ebcf533..065624bc 100644 --- a/plug.vim +++ b/plug.vim @@ -109,6 +109,8 @@ let s:TYPE = { \ } let s:loaded = get(s:, 'loaded', {}) let s:triggers = get(s:, 'triggers', {}) +let s:need_filetypeplugin_au = 0 +let s:need_filetypeindent_au = 0 function! plug#begin(...) if a:0 > 0 @@ -209,6 +211,21 @@ function! plug#end() if exists('g:did_load_filetypes') filetype off endif + + let warn = [] + if exists('g:did_load_ftplugin') + let warn += ['plugin'] + let s:need_filetypeindent_au = 1 + endif + if exists('g:did_indent_on') + let warn += ['indent'] + let s:need_filetypeplugin_au = 1 + endif + if !empty(warn) + redraw + call s:warn('echom', printf('[vim-plug] "filetype %s on" should not be used manually with vim-plug, please remove it from your vimrc.', join(warn))) + endif + for name in g:plugs_order if !has_key(g:plugs, name) continue @@ -502,8 +519,17 @@ function! s:lod_ft(pat, names) let syn = 'syntax/'.a:pat.'.vim' call s:lod(a:names, ['plugin', 'after/plugin'], syn, 'after/'.syn) execute 'autocmd! PlugLOD FileType' a:pat - call s:doautocmd('filetypeplugin', 'FileType') - call s:doautocmd('filetypeindent', 'FileType') + + " Executing this is only necessary if "filetype plugin indent on" was used + " before plug#end, and can be skipped when Vim has not entered always. + if v:vim_did_enter + if s:need_filetypeplugin_au + call s:doautocmd('filetypeplugin', 'FileType') + endif + if s:need_filetypeindent_au + call s:doautocmd('filetypeindent', 'FileType') + endif + endif endfunction function! s:lod_cmd(cmd, bang, l1, l2, args, names) From fa165cd592527ed0db425e9e672bc38babb0ba4b Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Mon, 15 Jan 2018 00:11:16 +0100 Subject: [PATCH 2/5] handle missing v:vim_did_enter / use s:vim_did_enter --- plug.vim | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/plug.vim b/plug.vim index 065624bc..8d4ade35 100644 --- a/plug.vim +++ b/plug.vim @@ -298,7 +298,15 @@ function! plug#end() if has('syntax') && !exists('g:syntax_on') syntax enable end + + " NOTE: v:vim_did_enter might not exist with older Vims, and handling it + " manually can be used in tests. + let s:vim_did_enter = 0 + augroup PlugLOD + autocmd VimEnter * let s:vim_did_enter = 1 + augroup END else + let s:vim_did_enter = 1 call s:reload_plugins() endif endfunction @@ -522,7 +530,7 @@ function! s:lod_ft(pat, names) " Executing this is only necessary if "filetype plugin indent on" was used " before plug#end, and can be skipped when Vim has not entered always. - if v:vim_did_enter + if s:vim_did_enter if s:need_filetypeplugin_au call s:doautocmd('filetypeplugin', 'FileType') endif From 66e48daac394ec6453f3ff53324f39e1641f0805 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Mon, 15 Jan 2018 04:06:52 +0100 Subject: [PATCH 3/5] adjust test "Filetype-based on-demand loading" --- test/workflow.vader | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/workflow.vader b/test/workflow.vader index b0a43a17..7f0c13d4 100644 --- a/test/workflow.vader +++ b/test/workflow.vader @@ -1117,7 +1117,7 @@ Execute (Filetype-based on-demand loading): AssertEqual ['xxx/ftdetect', 'xxx/after/ftdetect'], g:xxx setf xxx - AssertEqual ['xxx/ftdetect', 'xxx/after/ftdetect', 'xxx/plugin', 'xxx/after/plugin', 'xxx/syntax', 'xxx/after/syntax', 'xxx/ftplugin', 'xxx/after/ftplugin', 'xxx/indent', 'xxx/after/indent'], g:xxx + AssertEqual ['xxx/ftdetect', 'xxx/after/ftdetect', 'xxx/plugin', 'xxx/after/plugin', 'xxx/syntax', 'xxx/after/syntax'], g:xxx " syntax/xxx.vim and after/syntax/xxx.vim should not be loaded (#410) setf yyy From a78b28a318036ec4af323982ceda95e1c5f3c670 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Mon, 15 Jan 2018 04:20:07 +0100 Subject: [PATCH 4/5] tests: trigger VimEnter for #112 test --- test/regressions.vader | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/regressions.vader b/test/regressions.vader index dd2a6a2e..aa0a3a06 100644 --- a/test/regressions.vader +++ b/test/regressions.vader @@ -5,6 +5,10 @@ Execute (#112 On-demand loading should not suppress messages from ftplugin): Plug '$PLUG_FIXTURES/ftplugin-msg', { 'for': 'c' } call plug#end() + " Trigger VimEnter (simulate Vim being started), so that s:lod handles + " filetypeindent/filetypeplugin." + doautocmd VimEnter + redir => out tabnew a.c redir END From 00d3f0ada65c0ce84263f9beb5d8daae3989aa7d Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 17 Apr 2018 09:57:15 +0200 Subject: [PATCH 5/5] Queue autocommands for VimEnter This also uses `s:dobufread` for the `BufEnter` event (which enables `` for it). Triggering `BufRead` during startup of Vim should be avoided, because it will also trigger `FileType` events, which can have unexpected side effects. Ref: https://github.com/vim/vim/issues/2810 --- plug.vim | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/plug.vim b/plug.vim index 8d4ade35..2b696209 100644 --- a/plug.vim +++ b/plug.vim @@ -111,6 +111,7 @@ let s:loaded = get(s:, 'loaded', {}) let s:triggers = get(s:, 'triggers', {}) let s:need_filetypeplugin_au = 0 let s:need_filetypeindent_au = 0 +let s:autocmd_queue_for_vimenter = [] function! plug#begin(...) if a:0 > 0 @@ -302,8 +303,14 @@ function! plug#end() " NOTE: v:vim_did_enter might not exist with older Vims, and handling it " manually can be used in tests. let s:vim_did_enter = 0 + function! s:plug_on_vimenter() + let s:vim_did_enter = 1 + for event in s:autocmd_queue_for_vimenter + call s:doautocmd(event) + endfor + endfunction augroup PlugLOD - autocmd VimEnter * let s:vim_did_enter = 1 + autocmd VimEnter * call s:plug_on_vimenter() augroup END else let s:vim_did_enter = 1 @@ -444,6 +451,12 @@ function! s:reorg_rtp() endfunction function! s:doautocmd(...) + if !s:vim_did_enter + if index(s:autocmd_queue_for_vimenter, a:000) == -1 + call add(s:autocmd_queue_for_vimenter, a:000) + endif + return + endif if exists('#'.join(a:000, '#')) execute 'doautocmd' ((v:version > 703 || has('patch442')) ? '' : '') join(a:000) endif @@ -454,9 +467,7 @@ function! s:dobufread(names) let path = s:rtp(g:plugs[name]).'/**' for dir in ['ftdetect', 'ftplugin'] if len(finddir(dir, path)) - if exists('#BufRead') - doautocmd BufRead - endif + call s:doautocmd('BufRead') return endif endfor