I uploaded GetVar a long long time ago to vim.sf.net (initially in 2002, updated for Vim 7 in 2006) that basically is called with the name of a variable and an optional default value. It checks w:, b:, t:, and finally g: before returning the specified default value (default default value is -1).
There is also a VarExists function in there that just returns true/false (1/0) based on whether it was able to find any of those. I use these to override global plugin settings on a per-buffer basis, among other things. Salman. > -----Original Message----- > From: Marc Weber [mailto:[EMAIL PROTECTED] > Sent: Wednesday, April 25, 2007 10:24 AM > To: vim-dev@vim.org > Subject: Re: wish: allow a: in the function def > > > 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 >