4

I wonder if there is an elegant way to determine if a specific file exists in/under one of the entries of &rtp.

Currently, I use the following construct:

let s:found_ = 0
for s:dir_ in split(&rtp, ',')
  let s:file_ = s:dir_ . '/autoload/FooBarBaz.vim'
  if filereadable(s:file_)
     let s:found_ = 1
     break
  endif
endfor

if s:found_
   call FooBarBaz#func()
endif

I am under the impression that vim could offer a function or something that does it more elegantly, such as exists('*FooBarBaz#func') which unfortunately didn't work as I intended.

1 Answer 1

11

You're looking for globpath(). If it returns a non empty string, then there is at least a match.

if !empty((globpath(&rtp, '/autoload/FooBarBaz.vim'))

You may also want to check filereadable() if you suspect you may not be able to open the file. BTW, I'd have used filter() in your solution (I hate debugging vim loops) -> if !empty(filter(split(&rtp,','), 'filereable(v:val."autoload/FooBarBaz.vim")')

But in that case, I often simply run a:

runtime autoload/FooBarBaz.vim
if !exists('*FooBarBaz#func')

(See my convoluted lh#dev#option#call() which searches the first function that matches prefix#{filetype}#postfix(args) among a list of filetypes.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.