Reputation: 4948
I have the following in my .vimrc
which displays a function name based on where the cursor is (it uses a function from taglist.vim)
set statusline=%<%f\ [%{Tlist_Get_Tagname_By_Line()}]\ %{fugitive#statusline()}\ %h%m%r%=%-14.(%l,%c%V%)\ %P
My problem is that if taglist.vim is not installed, Vim complains about it when you open the program, and its super annoying:
E117: Unknown function: Tlist_Get_Tagname_By_Line
Press ENTER or type command to continue
E15: Invalid expression: Tlist_Get_Tagname_By_Line()
Q. How do I only set the statusline if Tlist_Get_Tagname_By_line()
exists?
Upvotes: 0
Views: 372
Reputation: 172658
@romainl's answer is totally correct; I'd like to contribute a concrete solution that has less duplication (of the 'statusline'
definition) or complexity (of the ternary):
If the taglist function does not exist, just define a dummy one that returns an empty string.
What the other answer also doesn't mention is that if you set 'statusline'
in your ~/.vimrc
, plugins haven't been loaded yet. In order to check taglist availablity, you have to explicitly load it first. The ternary expression does not suffer from this, but you pay with increased runtime cost (conditional must be evaluated on every statusline update).
" Try to load plugin now.
:runtime! plugin/taglist.vim
if !exists('*Tlist_Get_Tagname_By_line')
" Dummy stub for when taglist.vim isn't available.
function! Tlist_Get_Tagname_By_line()
return ''
endfunction
endif
Alternatively, you could also define the dummy stub in ~/.vim/after/plugin/taglist.vim
, or define it via :autocmd VimEnter * ...
, but both are not as clear and expressive as the recommended solution above.
Upvotes: 1
Reputation: 196751
My problem is that if taglist.vim is not installed, Vim complains about it when you open the program, and its super annoying:
No, the system reporting an error is never the problem. The problem is what caused the error.
How do I only set the statusline if
Tlist_Get_Tagname_By_line()
exists?
You can test the availability of a function with :help exists()
. So you could…
put your whole statusline definition in a conditional:
if exists('*Tlist_Get_Tagname_By_line')
set statusline=...
endif
or use a ternary expression in your statusline:
...%{exists('*Tlist_Get_Tagname_By_line')?Tlist_Get_Tagname_By_line():''}...
Upvotes: 1