1

Here is a scenario:

I would like to load another vimscript file into my script(via :runtime! command), and have access to script scoped (s:) variables and functions that are inside loaded file.

Is it possible (without altering file that is loaded)?

1 Answer 1

3

Unless the script exposes a function for that purpose, I'm afraid that it won't be possible to access script variables.

For instance, all my autoload plugins have a function such as

function! lh#dict#debug(expr) abort
  return eval(a:expr)
endfunction

However, it's perfectly possible to access script function through a very dirty trick.

But, honestly, you'd better ask the script maintainer to open his/her script for your need(s). You need first to obtain the script id (this can be done by parsing the result of :scriptname through :redir command or through the recent execute() function.

" or if your prefer, if you're looking plugin foobar.vim:
let snr = matchstr(matchstr(split(execute('scriptnames'), "\n"), 'foobar.vim'), '^\d\+')

Let's suppose the 42nd script loaded defines the function s:foo(), you can access to it with:

let Foo = function('<SNR>42_foo') " or, use snr variable obtained as explained above instead of 42
echo Foo(42)
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Luc, I will try this later. Not sure if asking maintainer will be possible, as the file I'm loading is .indent/html.vim.
@kam, you mean this is a standard file? The procedure is to contact its maintainer with your patch. He'll then send the file back to Bram.
I've found that '\(^\s*\)\@<=\d\+' works better than '^\d\+' because there may be leading whitespace.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.