editorconfig / editorconfig-vim

EditorConfig plugin for Vim
http://editorconfig.org
Other
3.13k stars 137 forks source link

Installing hooks when using Vim8 plugin #161

Open bcbnz opened 3 years ago

bcbnz commented 3 years ago

When this plugin is installed as a Vim8 plugin, it is not loaded until after the vimrc has been processed. This means that hooks cannot be installed in vimrc as editorconfig#AddNewHook is not yet defined. If you try the example in the plugin help,

call editorconfig#AddNewHook(function('FiletypeHook'))

then running vim gives you an error

Error detected while processing VIMINIT..script ~/.vimrc:
line  262:
E117: Unknown function: editorconfig#AddNewHook

One option to solve this is to use an autocommand on the VimEnter event which fires when startup is complete:

autocmd VimEnter * call editorconfig#AddNewHook(function('FiletypeHook'))

However, this is run after the initial buffers are loaded, so if you run vim myfile.m then the hook is not called on myfile.m, but it will be called if you subsequently load a new buffer (:e otherfile.m or equivalent).

As per https://stackoverflow.com/questions/56454847/ the suggested method for running commands after plugins are loaded is to create a file in ~/.vim/after/plugin with the code to run, so a file ~/.vim/after/plugin/editorconfig.vim with the contents call editorconfig#AddNewHook(function('FiletypeHook')) works.

k-takata commented 3 years ago

packadd! editorconfig-vim might be used.

cxw42 commented 3 years ago

Glad you were able to find a solution! PRs welcome with documentation updates :) .

bcbnz commented 3 years ago

Another solution: for vim >= 8.1.0729 there is a SourcePost event which is triggered after a script is sourced. This can be used to call the AddNewHook function:

autocmd SourcePost */plugin/editorconfig.vim call editorconfig#AddNewHook(function('FiletypeHook'))

@cxw42 -- I'm happy to work on a PR. I think it would be cleanest if the help had one method that worked for both Vim8 plugins and for external plugin managers. Since the current example presumably works for external managers, I assume that either the separate file in ~/.vim/after/plugin/ or the SourcePost event in vimrc options would work. I would prefer the SourcePost way myself to keep everything in one file, and the commit that added it was almost 2 years ago so it should be available in most builds by now. Any objections?

cxw42 commented 3 years ago

@bcbnz As long as the current version posted for download on www.vim.org has SourcePost, that's fine with me! Thanks!

k-takata commented 3 years ago

Using packadd! is simpler than SourcePost, I think.

bcbnz commented 3 years ago

Yes, if you want the plugin loaded at startup. If you have it as an optional plugin you want to load later, then PackAdd! in the vimrc forces it to be loaded at startup whereas SourcePost works whenever it is loaded.

k-takata commented 3 years ago

Ah, okay. That is useful. But,

then PackAdd! in the vimrc forces it to be loaded at startup

this is also applied to ~/.vim/after/plugin/.