Loading ...
Sorry, an error occurred while loading the content.

RE: Two minor requests for the TODO list

Expand Messages
  • Eggum, DavidX S
    ... 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
    Message 1 of 5 , Dec 7, 2006
    • 0 Attachment
      > 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
    • Halim, Salman
      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
      Message 2 of 5 , Dec 7, 2006
      • 0 Attachment
        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:davidx.s.eggum@...]
        > 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
        >
      • Eggum, DavidX S
        ... assignment operator, ... is not yet ... Along those lines, why not define a conditional attribute that would apply to functions? ... ... When a function by
        Message 3 of 5 , Dec 7, 2006
        • 0 Attachment
          > :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

          Along those lines, why not define a conditional attribute that would
          apply to functions?

          :fu[nction][!?] {name}([arguments]) [range] [abort] [dict]
          ...

          When a function by this name already exists and
          [!] or
          [?] is not used an error message is given. When [!]
          is
          used, an existing function is silently replaced; when
          [?] is used, the function is ignored.

          The same goes for :command[?]
        Your message has been successfully submitted and would be delivered to recipients shortly.