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

What is a good way to check if a package is loaded? #146

Closed
bzp99 opened this issue May 13, 2022 · 6 comments
Closed

What is a good way to check if a package is loaded? #146

bzp99 opened this issue May 13, 2022 · 6 comments

Comments

@bzp99
Copy link

bzp99 commented May 13, 2022

I have been using constructs like

if &rtp =~ 'gruvbox'
        " do something, eg
        colorscheme gruvbox
endif

But this suddenly stopped working since I just updated neovim and minpac.
I see that &rtp looks like this:

/home/bp99/.config/nvim,/home/bp99/.config/nvim/pack/*/start/*,/etc/xdg/nvim,/home/bp99/.local/share/nvim/site,/usr/local/share/nvim/site,/usr/share/nvim/site,/home/bp9
9/.config/nvim/pack/minpac/start/vim-polyglot,/usr/local/share/nvim/runtime,/usr/local/share/nvim/runtime/pack/dist/opt/matchit,/usr/local/lib/nvim,/home/bp99/.config/n
vim/pack/minpac/start/vim-polyglot/after,/home/bp99/.config/nvim/pack/*/start/*/after,/usr/share/nvim/site/after,/usr/local/share/nvim/site/after,/home/bp99/.local/shar
e/nvim/site/after,/etc/xdg/nvim/after,/home/bp99/.config/nvim/after

So I guess this method is no longer suitable. Is there a recommended way to do this?

@bzp99
Copy link
Author

bzp99 commented May 13, 2022

In the meantime, I found the following solution:

function! IsPluginLoaded(name)
        return !empty(globpath(&rtp, '**' . a:name . '*'))
endfunction

if IsPluginLoaded('gruvbox')
        colorscheme gruvbox
endif

I wish there was a cleaner solution, but I am closing this. Sorry for the noise.

@bzp99 bzp99 closed this as completed May 13, 2022
@bzp99
Copy link
Author

bzp99 commented May 13, 2022

I take it back. What I actually wanted to write as the pattern is '**/*' . a:name . '*' but this causes Neovim to hang on startup. I assume it’s too inefficient to go through all the files like this.

@bzp99 bzp99 reopened this May 13, 2022
@matveyt
Copy link
Contributor

matveyt commented Jul 25, 2022

@bzp99 Normally you should add your colorschemes under opt/ subtree, so they never get into runtimepath (keeping it as short as possible). Then just try to load it and see if it works

function! SetColors(name) abort
    execute 'silent! colorscheme' a:name
    return exists('g:colors_name') && g:colors_name is# a:name
endfunction
...
eval SetColors('gruvbox') || SetColors('peachpuff')

@bzp99
Copy link
Author

bzp99 commented Jul 27, 2022

@matveyt thank you, that is good to know. But what about more generic cases? For example, setting mappings if a plugin is loaded. For example, I currently have

if IsPluginLoaded('cosco')
        nmap <silent> <leader>; <plug>(cosco-commaOrSemicolon)
endif

Is there a better or more efficient way to do this or to implement IsPluginLoaded better? Or would you consider this a good solution?

@matveyt
Copy link
Contributor

matveyt commented Jul 27, 2022

@bzp99 Plugins are normally loaded after vimrc, so it is a bit harder than you probably think. Especially, for a totally generic recommendation.

Speaking of mappings, I suggest not to check anything at all. If you won't have the plugin then the mapping will not do anything, so why bother?

@bzp99
Copy link
Author

bzp99 commented Aug 2, 2022

I see, thank you. I realized there is really no need for the checks I had sought. In all cases, I could either skip the whole check and in some others, I switched to autocmds based on filetype.

@bzp99 bzp99 closed this as completed Aug 2, 2022
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

No branches or pull requests

2 participants