> So maybe one could make vimscript search a variable foo as l:foo, a:foo,
> (maybe also: w:foo, b:foo), s:foo, g:foo, and then throw an undefined
> variable name error if none exists. Or so.
No. I don't want to go back to VB without using Option Explicit ;)
Don't let vim find some value somewhere. This leads to failures not so easy to
spot
But you are right. This might be useful:
Use buffer setting if it exists, if not use global one..
But you should be able to emulate this behaviour using the function exists:
function GetSetting(name)
if exists('b:'.a:name)
exec 'return b:'.a:name
elseif exists('g:'.a:name)
exec 'return g:'.a:name
else
echoe "Please define setting ".a:name
endif
endfunction
perhaps even add a optional parameter for a default value..
I'm using this very often:
function! vl#lib#brief#args#GetOptionalArg( name, default, ...)
if a:0 == 1
let idx = a:1
else
let idx = 1
endif
if type( a:default) != 1
throw "wrong type: default parameter of vl#lib#brief#args#GetOptionalArg
must be a string, use string(value)"
endif
let script = [ "if a:0 >= ". idx
\ , " let ".a:name." = a:".idx
\ , "else"
\ , " let ".a:name." = ".a:default
\ , "endif"
\ ]
return join( script, "\n")
endfunction
function GetSetting(name, ...)
exec vl#lib#brief#args#GetOptionalArg('default', string("option not given"))
if exists('b:'.a:name)
exec 'return b:'.a:name
elseif exists('g:'.a:name)
exec 'return g:'.a:name
else
return default
endif
endfunction
Then you can use
let b = GetSetting('my_name','default value')
or
let b = GetSetting('my_name')
which will set b to "option not given" if neither b:my_name nor g:my_name does
exist
HTH Marc