Create "loaded" variable
Contributing guidelines
- [X] I have read CONTRIBUTING.md
- [X] I have read CODE_OF_CONDUCT.md
Module(s)
All
Description
It would be helpful to have a "loaded" variable, similar to other vim plugins such as https://github.com/glepnir/galaxyline.nvim/blob/main/plugin/galaxyline.vim. This recommendation is also further outlined in https://vimways.org/2019/writing-vim-plugin/ and https://vi.stackexchange.com/questions/26132/why-do-plugins-have-to-check-if-they-are-already-loaded.
I make use of these variables to optionally enable certain settings depending on if a plugin is available or not.
The recommended way to check if module 'mini.xxx' is "active" is with _G.MiniXxx ~= nil Lua snippet. For example, the 'mini.surround' check is _G.MiniSurround ~= nil.
Some clarification. Every module of this plugin is an independent Lua module which should be set up explicitly via require('mini.xxx').setup() (except some cases which only provide Lua functions). Every setup() call creates a global variable _G.MiniXxx (that is by design and always be like that). This is also the way 'mini.nvim' itself uses in rare occasions when it needs such a check (like in mini.starter).
Does this solve your problem?
That is close to what I want, but since that variable is only set when the plugin is required, it didn't quite fix it.
Essentially I wanted to add a check that only called "require('mini.surround').setup()" if it could detect that mini.nvim was installed. The global variable "g:loaded_*" allows you to check for that. I think it is an old vimscript-ism, and isn't necessary anymore with lua.
I ended up writing a checker for lua modules instead using something similar to https://stackoverflow.com/questions/15429236/how-to-check-if-a-module-exists-in-lua, which fixes my issue.
Thanks for your quick reply!
Oh, I see.
The current standard approach to this kind of problem is via using pcall():
local ok, surround = pcall(require, 'mini.surround')
if ok then surround.setup() end