Skip to content
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

Installing hooks when using Vim8 plugin #161

Open
bcbnz opened this issue Dec 9, 2020 · 7 comments · May be fixed by #162
Open

Installing hooks when using Vim8 plugin #161

bcbnz opened this issue Dec 9, 2020 · 7 comments · May be fixed by #162

Comments

@bcbnz
Copy link

bcbnz commented Dec 9, 2020

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
Copy link
Contributor

k-takata commented Dec 9, 2020

packadd! editorconfig-vim might be used.

@cxw42
Copy link
Member

cxw42 commented Dec 9, 2020

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

@bcbnz
Copy link
Author

bcbnz commented Dec 10, 2020

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
Copy link
Member

cxw42 commented Dec 10, 2020

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

bcbnz added a commit to bcbnz/editorconfig-vim that referenced this issue Dec 10, 2020
Packages are loaded after vimrc is parsed, so we can't add the hook immediately.
Suggest using either an autocommand or a file in an after-directory to add the
hook instead.

Fixes: editorconfig#161
@k-takata
Copy link
Contributor

Using packadd! is simpler than SourcePost, I think.

@bcbnz
Copy link
Author

bcbnz commented Dec 11, 2020

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
Copy link
Contributor

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/.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants