k-takata / minpac

A minimal package manager for Vim 8+ (and Neovim)
835 stars 30 forks source link

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

Closed bzp99 closed 2 years ago

bzp99 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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.

matveyt commented 2 years ago

@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 commented 2 years ago

@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 commented 2 years ago

@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 commented 2 years ago

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.