Skip to content

Deferred loading on autocmd (#event[#pattern]) #548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -121,16 +121,16 @@ Reload .vimrc and `:PlugInstall` to install plugins.

### `Plug` options

| Option | Description |
| ----------------------- | ------------------------------------------------ |
| `branch`/`tag`/`commit` | Branch/tag/commit of the repository to use |
| `rtp` | Subdirectory that contains Vim plugin |
| `dir` | Custom directory for the plugin |
| `as` | Use different name for the plugin |
| `do` | Post-update hook (string or funcref) |
| `on` | On-demand loading: Commands or `<Plug>`-mappings |
| `for` | On-demand loading: File types |
| `frozen` | Do not update unless explicitly specified |
| Option | Description |
| ----------------------- | ----------------------------------------------------------- |
| `branch`/`tag`/`commit` | Branch/tag/commit of the repository to use |
| `rtp` | Subdirectory that contains Vim plugin |
| `dir` | Custom directory for the plugin |
| `as` | Use different name for the plugin |
| `do` | Post-update hook (string or funcref) |
| `on` | On-demand loading: Commands, `<Plug>`-mappings, or #autocmds |
| `for` | On-demand loading: File types |
| `frozen` | Do not update unless explicitly specified |

### Global options

@@ -180,6 +180,9 @@ Plug 'tpope/vim-fireplace', { 'for': 'clojure' }
" Multiple file types
Plug 'kovisoft/paredit', { 'for': ['clojure', 'scheme'] }

" On autocmd
Plug 'SirVer/ultisnips', { 'on': '#InsertEnter' }

" On-demand loading on both conditions
Plug 'junegunn/vader.vim', { 'on': 'Vader', 'for': 'vader' }

21 changes: 17 additions & 4 deletions plug.vim
Original file line number Diff line number Diff line change
@@ -229,9 +229,16 @@ function! plug#end()
call s:assoc(lod.cmd, cmd, name)
endif
call add(s:triggers[name].cmd, cmd)
elseif cmd[0] == '#' && exists('##'.split(cmd, '#')[0])
let tokens = split(cmd, '#')
let group = 'Plug/'.name
execute 'augroup' group
autocmd!
execute 'autocmd' tokens[0] get(tokens, 1, '*') printf('call s:lod_autocmd(%s)', string(name))
execute 'augroup END'
else
call s:err('Invalid `on` option: '.cmd.
\ '. Should start with an uppercase letter or `<Plug>`.')
\ '. Should start with an uppercase letter, `<Plug>`, or `#`.')
endif
endfor
endif
@@ -443,9 +450,7 @@ function! plug#load(...)
let s = len(unknowns) > 1 ? 's' : ''
return s:err(printf('Unknown plugin%s: %s', s, join(unknowns, ', ')))
end
for name in a:000
call s:lod([name], ['ftdetect', 'after/ftdetect', 'plugin', 'after/plugin'])
endfor
call s:lod(a:000, ['ftdetect', 'after/ftdetect', 'plugin', 'after/plugin'])
call s:dobufread(a:000)
return 1
endfunction
@@ -526,6 +531,14 @@ function! s:lod_map(map, names, with_prefix, prefix)
call feedkeys(substitute(a:map, '^<Plug>', "\<Plug>", '') . extra)
endfunction

function! s:lod_autocmd(name)
call s:lod([a:name], ['ftdetect', 'after/ftdetect', 'plugin', 'after/plugin'])
call s:dobufread([a:name])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be problematic.
I found this PR when looking for a better way to handle custom InsertEnter handling.

I am using the following:

    Plug 'Shougo/deoplete.nvim', {'on': []}
    …
    augroup vimplug_load_on_insertmode
      autocmd!
      autocmd InsertEnter * call plug#load(
            \ 'cursorcross.vim',
            \ 'delimitMate',
            \ 'deoplete.nvim',
            \ ) | call deoplete#enable()
            \ | autocmd! vimplug_load_on_insertmode
    augroup END

And here the call to deoplete#enable() adds a BufEnter autocmd that gets triggered then.
It is a bit weird, since plug#load() should be finished already, before deoplete#enable() then sets up the BufEnter autocmd?! (I have also put it into a separate function to make sure | does not cause it).

Maybe the BufEnter gets queued after the InsertEnter?!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dobufread happens for ftplugin …/.vim/plugged/jedi-vim//**, which is triggered through another handler:

    function s:my_insertenter_python()
      if &ft ==# 'python'
        call plug#load(
            \ 'deoplete-jedi',
            \ 'jedi-vim',
            \ )
        autocmd! vimplug_load_on_insertmode_python
      endif
    endfunction
    augroup vimplug_load_on_insertmode_python
      autocmd!
      autocmd InsertEnter * call s:my_insertenter_python()
    augroup END

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, this explains the issue: there are two calls to plug#load in that setup.

let group = 'Plug/'.a:name
execute 'autocmd!' group
execute 'augroup!' group
endfunction

function! plug#(repo, ...)
if a:0 > 1
return s:err('Invalid number of arguments (1..2)')