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

How to check if an option is a boolean option?

Expand Messages
  • thomas
    Hi, Does somebody on this list have an idea how one could find out whether an option is a boolean option? I tried the obvious solution (exists( &noXX )) but
    Message 1 of 8 , Jan 5, 2008
    • 0 Attachment
      Hi,

      Does somebody on this list have an idea how one could find out whether
      an option is a boolean option? I tried the obvious solution
      (exists('&noXX')) but this doesn't work. Boolean options are set by
      "set foo" and "set nofoo". It's not possible to do "set foo=1" or "set
      foo=0". Is there a way to tell whether I have to use "set nofoo"?

      Another question: is "let &foo='bar'" supposed to work precisely like
      "set foo=bar" or are there minor differences intended? I wasn't able
      to find a note on this in the help.

      Regards,
      Thomas.

      --~--~---------~--~----~------------~-------~--~----~
      You received this message from the "vim_use" maillist.
      For more information, visit http://www.vim.org/maillist.php
      -~----------~----~----~----~------~----~------~--~---
    • Tony Mechelynck
      ... The obvious solution to know the type of an option is to check the help: :help option (with the single quotes but not the double ones) will tell you
      Message 2 of 8 , Jan 5, 2008
      • 0 Attachment
        thomas wrote:
        > Hi,
        >
        > Does somebody on this list have an idea how one could find out whether
        > an option is a boolean option? I tried the obvious solution
        > (exists('&noXX')) but this doesn't work. Boolean options are set by
        > "set foo" and "set nofoo". It's not possible to do "set foo=1" or "set
        > foo=0". Is there a way to tell whether I have to use "set nofoo"?
        >
        > Another question: is "let &foo='bar'" supposed to work precisely like
        > "set foo=bar" or are there minor differences intended? I wasn't able
        > to find a note on this in the help.
        >
        > Regards,
        > Thomas.

        The obvious solution to know the type of an option is to check the help:
        ":help 'option'" (with the single quotes but not the double ones) will tell
        you whether the option in question is A String, a Number (= integer) or a Boolean.

        ":let &option = value" works like ":set option=value" except:
        - it works the same for a boolean (values 0 or 1) or an integer
        - IIUC, when setting a Number option to a String value or vice-versa,
        automatic conversion will happen. (I haven't checked this though.)

        ":let &option = <expression>" is used mostly to restore a saved option:

        let s:save_option = &option
        ...
        ... do your stuff
        ...
        let &option = s:save_option

        It can also be used in cases such as:

        (for use with keymaps)

        map <F8> :let &imi = !&imi<CR>

        (for use with Input Method)

        map <F9> :let &imi = 2 * (!&imi)<CR>




        Best regards,
        Tony.
        --
        In Blythe, California, a city ordinance declares that a person must own
        at least two cows before he can wear cowboy boots in public.

        --~--~---------~--~----~------------~-------~--~----~
        You received this message from the "vim_use" maillist.
        For more information, visit http://www.vim.org/maillist.php
        -~----------~----~----~----~------~----~------~--~---
      • A.Politz
        ... How about this. func! IsBooleanOption( opt ) try exec set inv .a:opt. inv .a:opt catch return 0 endtry return 1 endfun -ap -- Ich hab geträumt, der
        Message 3 of 8 , Jan 5, 2008
        • 0 Attachment
          thomas wrote:

          >Hi,
          >
          >Does somebody on this list have an idea how one could find out whether
          >an option is a boolean option? I tried the obvious solution
          >(exists('&noXX')) but this doesn't work. Boolean options are set by
          >"set foo" and "set nofoo". It's not possible to do "set foo=1" or "set
          >foo=0". Is there a way to tell whether I have to use "set nofoo"?
          >
          >
          How about this.


          func! IsBooleanOption( opt )
          try
          exec 'set inv'.a:opt.' inv'.a:opt
          catch
          return 0
          endtry
          return 1
          endfun


          -ap


          --
          Ich hab geträumt, der Krieg wär vorbei.


          --~--~---------~--~----~------------~-------~--~----~
          You received this message from the "vim_use" maillist.
          For more information, visit http://www.vim.org/maillist.php
          -~----------~----~----~----~------~----~------~--~---
        • thomas
          ... Could be tricky when done programmatically. Although one could check the help tags maybe. But the official position is that let &foo and set foo are
          Message 4 of 8 , Jan 5, 2008
          • 0 Attachment
            > The obvious solution to know the type of an option is to check the help

            Could be tricky when done programmatically. Although one could check
            the help tags maybe.

            But the official position is that "let &foo" and "set foo" are 100%
            equivalent? I came to think there are minor differences in the
            behaviour when deleting a buffer and loading a new buffer. Hm.

            --~--~---------~--~----~------------~-------~--~----~
            You received this message from the "vim_use" maillist.
            For more information, visit http://www.vim.org/maillist.php
            -~----------~----~----~----~------~----~------~--~---
          • thomas
            ... This could work. Thanks a lot. --~--~---------~--~----~------------~-------~--~----~ You received this message from the vim_use maillist. For more
            Message 5 of 8 , Jan 5, 2008
            • 0 Attachment
              > try
              > exec 'set inv'.a:opt.' inv'.a:opt
              > catch
              > return 0
              > endtry
              > return 1

              This could work. Thanks a lot.

              --~--~---------~--~----~------------~-------~--~----~
              You received this message from the "vim_use" maillist.
              For more information, visit http://www.vim.org/maillist.php
              -~----------~----~----~----~------~----~------~--~---
            • Tony Mechelynck
              ... Programmatically, most often you know which option you know which option you re dealing which. When you don t, there s the try-catch method shown by A.
              Message 6 of 8 , Jan 5, 2008
              • 0 Attachment
                thomas wrote:
                >> The obvious solution to know the type of an option is to check the help
                >
                > Could be tricky when done programmatically. Although one could check
                > the help tags maybe.

                Programmatically, most often you know which option you know which option
                you're dealing which. When you don't, there's the try-catch method shown by A.
                Politz.

                >
                > But the official position is that "let &foo" and "set foo" are 100%
                > equivalent? I came to think there are minor differences in the
                > behaviour when deleting a buffer and loading a new buffer. Hm.

                For the "official position", read ":help :let-option" slowly and throughly;
                and go on through the various flavours of that command, until *:let-unpack*
                excluded.


                Best regards,
                Tony.
                --
                If Patrick Henry thought that taxation without representation was bad,
                he should see how bad it is with representation.

                --~--~---------~--~----~------------~-------~--~----~
                You received this message from the "vim_use" maillist.
                For more information, visit http://www.vim.org/maillist.php
                -~----------~----~----~----~------~----~------~--~---
              • Andy Wokula
                ... -- Andy --~--~---------~--~----~------------~-------~--~----~ You received this message from the vim_use maillist. For more information, visit
                Message 7 of 8 , Jan 6, 2008
                • 0 Attachment
                  thomas schrieb:
                  > Hi,
                  >
                  > Does somebody on this list have an idea how one could find out whether
                  > an option is a boolean option? I tried the obvious solution
                  > (exists('&noXX')) but this doesn't work. Boolean options are set by
                  > "set foo" and "set nofoo". It's not possible to do "set foo=1" or "set
                  > foo=0". Is there a way to tell whether I have to use "set nofoo"?
                  >
                  > Another question: is "let &foo='bar'" supposed to work precisely like
                  > "set foo=bar" or are there minor differences intended? I wasn't able
                  > to find a note on this in the help.
                  >
                  > Regards,
                  > Thomas.

                  You can use (with boolean option 'foo'):
                  :let &foo=1
                  :let &foo=0

                  --
                  Andy

                  --~--~---------~--~----~------------~-------~--~----~
                  You received this message from the "vim_use" maillist.
                  For more information, visit http://www.vim.org/maillist.php
                  -~----------~----~----~----~------~----~------~--~---
                • thomas
                  ... That s what I m using now. The reason why I asked the question was because I had the slight impression that this behaves a little bit different from using
                  Message 8 of 8 , Jan 6, 2008
                  • 0 Attachment
                    > :let &foo=1
                    > :let &foo=0

                    That's what I'm using now. The reason why I asked the question was
                    because I had the slight impression that this behaves a little bit
                    different from using :set foo. So my question basically was if
                    somebody else had the same impression. The answer seems to be no.

                    The context of my question is a function that saves and restores
                    options listed in an array/list.

                    Thanks for your responses.

                    --~--~---------~--~----~------------~-------~--~----~
                    You received this message from the "vim_use" maillist.
                    For more information, visit http://www.vim.org/maillist.php
                    -~----------~----~----~----~------~----~------~--~---
                  Your message has been successfully submitted and would be delivered to recipients shortly.