On 5/30/19 1:06 PM, Paul Jolly wrote:
> Hi all,
>
> As a follow up to a recent thread ("Plugins exposing events to other
> plugins":
>
https://groups.google.com/d/msg/vim_dev/uiw6gHft-0g/i8qdfT8YBQAJ)
> I'd like to ask whether there is an existing pattern of configuration
> functions that plugin developers follow?
>
> Some background.
>
> I'm not really a fan of global config variables in .vimrc files, e.g.:
>
> let g:govim_quickfix_auto_diagnotics_disable=1
>
> especially when it comes to channel-based plugins, because they (the
> channel-based plugins) end up having to make a roundtrip call to check
> a global variable value; if this is on a "hot" path it becomes
> expensive. But also because it's too easy to mis-type/set an invalid
> value.
>
> So I'd like to explore a pattern by which the end user calls a config
> function from their .vimrc or whenever they want to change a config
> value. This will have the benefit of pushing the config value to the
> channel-based plugin, but also allowing tighter validation at the time
> of the value being set.
>
> The main problem (aside from my complete lack of real understanding of
> the .vimrc load sequence) is that plugins have not loaded at the time
> .vimrc is processed, and hence it's not possible to call a
> plugin-defined config function directly... so we'd need to handle this
> somehow.
>
> Does anyone have any thoughts on/experience of the above?
>
> Thanks in advance,
>
>
> Paul
>
I don't really see the difference between configuring via global
variable or by calling a function in your vimrc. Your plugin doesn't
need to check vim's global variable every time you use a feature, just
check it once during plugin initialization and set some internal
variable (supposing your plugin is written in go/python/etc). There is
no expectation to the user that modifying `g:govim_whatever` will take
immediate effect after the plugin has already started (if you want you
can provide a :GovimReload or similar). You can also do validation just
once when you are saving the values.
Another option is to use a separate .json or .ini file for configuration
so you don't have to worry about vimrc/plugin sourcing order. But this
is less idiomatic than putting settings in a single g: dictionary.
To address the direct question, a standard is to use autoloaded
functions, `call myplugin#configure()`. This merely requires the plugin
to be in the runtimepath which is handled by :Plug or packadd! A
seperate standard is to use User autocmd which requests the user call
some myplugin#configure() function- if the autocmd is fired the plugin
is definitely loaded and ready to get options set.