I have a relatively simple function I use:

function! GetVar( ... )
  let varName=a:1

  let retVal = exists( "a:2" ) ? a:2 : -1

  if ( exists ( "w:" . varName ) )
    let retVal=w:{varName}
  elseif ( exists ( "b:" . varName ) )
    let retVal=b:{varName}
  elseif ( exists ( "t:" . varName ) )
    let retVal=t:{varName}
  elseif ( exists ( "g:" . varName ) )
    let retVal=g:{varName}
  endif
  return retVal
endfunction

Calling it with something like:

:let test = GetVar( 'expandWindow', 'never' )

First checks to see if w:expandWindow exists (and returns that), then 
b:expandWindow, t:expandWindow and finally g:expandWindow; if none of them 
exist, it returns 'never' (or -1, if there was no second parameter).  I use it 
in plugins to allow both global default values as well as window-, buffer- or 
tab-specific overrides.

This could be easily rewritten as:

:return exists( a:1 ) ? {a:1} : exists( a:2 ) ? a:2 : 0

To do what you're asking.

Of course, an internal version would be much faster, which might be why you 
asked :)

Salman.

> -----Original Message-----
> From: Eggum, DavidX S [mailto:[EMAIL PROTECTED] 
> Sent: Thursday, December 07, 2006 1:22 PM
> To: vim developers list
> Subject: RE: Two minor requests for the TODO list
> 
> 
> > How about adding this:
> > get({string} [, {default}])
> >                                 Get value from variable {string}.  
> > When this variable does not
> >                                 exist, return {default}.  
> Return zero 
> > when {default} is
> >                                 omitted.
> 
> 
> Hmmm.... we could follow the more succinct gmake way of doing 
> things and add a new operator instead (I like this solution better):
> 
> :let {var} ?= {expr}            This is a conditional 
> variable assignment operator,
>                                         it only has an effect 
> if the variable is not yet
>                                         defined. This statement:
> 
>                                          let foo ?= "bar"
> 
>                                 is exactly equivalent to this:
> 
>                                          if !exists("foo")
>                                             let foo = "bar"
>                                          endif
> 
> Regards,
> David
> 
> ---
> "Love is what's in the room with you at Christmas if you stop 
> opening presents and listen."
> 
> Bobby - age 7
> 

Reply via email to