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

[BUG] “:execute 'if 1 '” works like “:if 1”

Expand Messages
  • ZyX
    Consider the following script: execute if 0 echo Not shown else echo Shown endif . If you source it you find that instead of 3 errors (“missing
    Message 1 of 9 , Mar 20, 2014
      Consider the following script:

      execute 'if 0'
      echo 'Not shown'
      else
      echo 'Shown'
      endif

      . If you source it you find that instead of 3 errors (“missing :endif”, “:else without :if”, “:endif without :if”) and two messages (“Not shown” and “Shown”) you will see one message (“Shown”) and no errors.

      Note that `execute 'if 0'` is explicitly forbidden in help:

      > Note: The executed string may be any command-line, but
      > you cannot start or end a "while", "for" or "if"
      > command. Thus this is illegal:
      > :execute 'while i > 5'
      > :execute 'echo "test" | break'

      --
      --
      You received this message from the "vim_dev" maillist.
      Do not top-post! Type your reply below the text you are replying to.
      For more information, visit http://www.vim.org/maillist.php

      ---
      You received this message because you are subscribed to the Google Groups "vim_dev" group.
      To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
      For more options, visit https://groups.google.com/d/optout.
    • Bram Moolenaar
      ... The docs are wrong, using if is allowed. The other two are not. -- CART DRIVER: Bring out your dead! LARGE MAN: Here s one! CART DRIVER: Ninepence.
      Message 2 of 9 , Mar 22, 2014
        ZyX wrote:

        > Consider the following script:
        >
        > execute 'if 0'
        > echo 'Not shown'
        > else
        > echo 'Shown'
        > endif
        >
        > . If you source it you find that instead of 3 errors (“missing
        > :endif”, “:else without :if”, “:endif without :if”) and two messages
        > (“Not shown” and “Shown”) you will see one message (“Shown”) and no
        > errors.
        >
        > Note that `execute 'if 0'` is explicitly forbidden in help:
        >
        > > Note: The executed string may be any command-line, but
        > > you cannot start or end a "while", "for" or "if"
        > > command. Thus this is illegal:
        > > :execute 'while i > 5'
        > > :execute 'echo "test" | break'

        The docs are wrong, using "if" is allowed. The other two are not.

        --
        CART DRIVER: Bring out your dead!
        LARGE MAN: Here's one!
        CART DRIVER: Ninepence.
        BODY: I'm not dead!
        "Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD

        /// Bram Moolenaar -- Bram@... -- http://www.Moolenaar.net \\\
        /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
        \\\ an exciting new programming language -- http://www.Zimbu.org ///
        \\\ help me help AIDS victims -- http://ICCF-Holland.org ///

        --
        --
        You received this message from the "vim_dev" maillist.
        Do not top-post! Type your reply below the text you are replying to.
        For more information, visit http://www.vim.org/maillist.php

        ---
        You received this message because you are subscribed to the Google Groups "vim_dev" group.
        To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
        For more options, visit https://groups.google.com/d/optout.
      • Marcin Szamotulski
        ... Well, this script works just fine for me: let i = 0 execute while i
        Message 3 of 9 , Mar 22, 2014
          On 15:33 Sat 22 Mar , Bram Moolenaar wrote:
          >
          > ZyX wrote:
          >
          > > Consider the following script:
          > >
          > > execute 'if 0'
          > > echo 'Not shown'
          > > else
          > > echo 'Shown'
          > > endif
          > >
          > > . If you source it you find that instead of 3 errors (“missing
          > > :endif”, “:else without :if”, “:endif without :if”) and two messages
          > > (“Not shown” and “Shown”) you will see one message (“Shown”) and no
          > > errors.
          > >
          > > Note that `execute 'if 0'` is explicitly forbidden in help:
          > >
          > > > Note: The executed string may be any command-line, but
          > > > you cannot start or end a "while", "for" or "if"
          > > > command. Thus this is illegal:
          > > > :execute 'while i > 5'
          > > > :execute 'echo "test" | break'
          >
          > The docs are wrong, using "if" is allowed. The other two are not.

          Well, this script works just fine for me:

          let i = 0
          execute 'while i < 10'
          let i += 1
          echo 1
          endwhile

          also this one works:

          execute 'for i in range(10)'
          echo i
          endfor

          Best regards,
          Marcin Szamotulski

          --
          --
          You received this message from the "vim_dev" maillist.
          Do not top-post! Type your reply below the text you are replying to.
          For more information, visit http://www.vim.org/maillist.php

          ---
          You received this message because you are subscribed to the Google Groups "vim_dev" group.
          To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
          For more options, visit https://groups.google.com/d/optout.
        • Yukihiro Nakadaira
          ... It doesn t work when it is used in nested block. if 0 execute if 1 endif endif = E580: :endif without :if: endif -- Yukihiro Nakadaira -
          Message 4 of 9 , Mar 22, 2014
            On Sat, Mar 22, 2014 at 11:33 PM, Bram Moolenaar <Bram@...> wrote:

            ZyX wrote:

            > Consider the following script:
            >
            >     execute 'if 0'
            >         echo 'Not shown'
            >     else
            >         echo 'Shown'
            >     endif
            >
            > . If you source it you find that instead of 3 errors (“missing
            > :endif”, “:else without :if”, “:endif without :if”) and two messages
            > (“Not shown” and “Shown”) you will see one message (“Shown”) and no
            > errors.
            >
            > Note that `execute 'if 0'` is explicitly forbidden in help:
            >
            > >                     Note: The executed string may be any command-line, but
            > >                     you cannot start or end a "while", "for" or "if"
            > >                     command.  Thus this is illegal:
            > >             :execute 'while i > 5'
            > >             :execute 'echo "test" | break'

            The docs are wrong, using "if" is allowed.  The other two are not.

            It doesn't work when it is used in nested block.

            if 0
              execute 'if 1'
              endif
            endif
            => E580: :endif without :if: endif

            --
            Yukihiro Nakadaira - yukihiro.nakadaira@...

            --
            --
            You received this message from the "vim_dev" maillist.
            Do not top-post! Type your reply below the text you are replying to.
            For more information, visit http://www.vim.org/maillist.php

            ---
            You received this message because you are subscribed to the Google Groups "vim_dev" group.
            To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
            For more options, visit https://groups.google.com/d/optout.
          • ZyX
            ... Why? It makes parsing impossible. E.g. like indicated by @Yukihiro Nakadaira, there is absolutely nothing you can do with this error unless you add hacks
            Message 5 of 9 , Mar 22, 2014
              > The docs are wrong, using "if" is allowed. The other two are not.

              Why? It makes parsing impossible. E.g. like indicated by @Yukihiro Nakadaira, there is absolutely nothing you can do with this error unless you add hacks for some specific cases (e.g. if `execute` is followed by a string literal starting with `if`).

              --
              --
              You received this message from the "vim_dev" maillist.
              Do not top-post! Type your reply below the text you are replying to.
              For more information, visit http://www.vim.org/maillist.php

              ---
              You received this message because you are subscribed to the Google Groups "vim_dev" group.
              To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
              For more options, visit https://groups.google.com/d/optout.
            • Bram Moolenaar
              ... I got confused by the example in the docs. It looks like :break and ... improved after this documentation was written. -- I used to wonder about the
              Message 6 of 9 , Mar 22, 2014
                Marcin Szamotulski wrote:

                > On 15:33 Sat 22 Mar , Bram Moolenaar wrote:
                > >
                > > ZyX wrote:
                > >
                > > > Consider the following script:
                > > >
                > > > execute 'if 0'
                > > > echo 'Not shown'
                > > > else
                > > > echo 'Shown'
                > > > endif
                > > >
                > > > . If you source it you find that instead of 3 errors (“missing
                > > > :endif”, “:else without :if”, “:endif without :if”) and two messages
                > > > (“Not shown” and “Shown”) you will see one message (“Shown”) and no
                > > > errors.
                > > >
                > > > Note that `execute 'if 0'` is explicitly forbidden in help:
                > > >
                > > > > Note: The executed string may be any command-line, but
                > > > > you cannot start or end a "while", "for" or "if"
                > > > > command. Thus this is illegal:
                > > > > :execute 'while i > 5'
                > > > > :execute 'echo "test" | break'
                > >
                > > The docs are wrong, using "if" is allowed. The other two are not.
                >
                > Well, this script works just fine for me:
                >
                > let i = 0
                > execute 'while i < 10'
                > let i += 1
                > echo 1
                > endwhile
                >
                > also this one works:
                >
                > execute 'for i in range(10)'
                > echo i
                > endfor

                I got confused by the example in the docs. It looks like :break and
                :continue don't work in :execute. I suppose the implementation got
                improved after this documentation was written.

                --
                I used to wonder about the meaning of life. But I looked it
                up in the dictionary under "L" and there it was - the meaning
                of life. It was less than I expected. - Dogbert

                /// Bram Moolenaar -- Bram@... -- http://www.Moolenaar.net \\\
                /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
                \\\ an exciting new programming language -- http://www.Zimbu.org ///
                \\\ help me help AIDS victims -- http://ICCF-Holland.org ///

                --
                --
                You received this message from the "vim_dev" maillist.
                Do not top-post! Type your reply below the text you are replying to.
                For more information, visit http://www.vim.org/maillist.php

                ---
                You received this message because you are subscribed to the Google Groups "vim_dev" group.
                To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
                For more options, visit https://groups.google.com/d/optout.
              • Bram Moolenaar
                ... The code is full of exceptions. There is a lot of history, some dating back to the days of Ex. The language wasn t designed to make life for parser
                Message 7 of 9 , Mar 22, 2014
                  ZyX wrote:

                  > > The docs are wrong, using "if" is allowed. The other two are not.
                  >
                  > Why? It makes parsing impossible. E.g. like indicated by @Yukihiro
                  > Nakadaira, there is absolutely nothing you can do with this error
                  > unless you add hacks for some specific cases (e.g. if `execute` is
                  > followed by a string literal starting with `if`).

                  The code is full of exceptions. There is a lot of history, some dating
                  back to the days of Ex. The language wasn't designed to make life for
                  parser writers easier. Some parts of the language weren't designed at
                  all, they just happened. Not breaking existing Vim scripts is the main
                  goal, thus making changes in the language to simplify the implementation
                  will cause headeaches for users.

                  --
                  BODY: I'm not dead!
                  CART DRIVER: 'Ere. He says he's not dead.
                  LARGE MAN: Yes he is.
                  BODY: I'm not!
                  "Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD

                  /// Bram Moolenaar -- Bram@... -- http://www.Moolenaar.net \\\
                  /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
                  \\\ an exciting new programming language -- http://www.Zimbu.org ///
                  \\\ help me help AIDS victims -- http://ICCF-Holland.org ///

                  --
                  --
                  You received this message from the "vim_dev" maillist.
                  Do not top-post! Type your reply below the text you are replying to.
                  For more information, visit http://www.vim.org/maillist.php

                  ---
                  You received this message because you are subscribed to the Google Groups "vim_dev" group.
                  To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
                  For more options, visit https://groups.google.com/d/optout.
                • Nikolay Pavlov
                  ... The problem is that you have a parser (I mean what vim does when skipping) there and it is not correct and may not be correct with this rule in effect.
                  Message 8 of 9 , Mar 22, 2014


                    On Mar 23, 2014 12:19 AM, "Bram Moolenaar" <Bram@...> wrote:
                    >
                    >
                    > ZyX wrote:
                    >
                    > > > The docs are wrong, using "if" is allowed.  The other two are not.
                    > >
                    > > Why? It makes parsing impossible. E.g. like indicated by @Yukihiro
                    > > Nakadaira, there is absolutely nothing you can do with this error
                    > > unless you add hacks for some specific cases (e.g. if `execute` is
                    > > followed by a string literal starting with `if`).
                    >
                    > The code is full of exceptions.  There is a lot of history, some dating
                    > back to the days of Ex.  The language wasn't designed to make life for
                    > parser writers easier.  Some parts of the language weren't designed at
                    > all, they just happened.  Not breaking existing Vim scripts is the main
                    > goal, thus making changes in the language to simplify the implementation
                    > will cause headeaches for users.

                    The problem is that you have a parser (I mean what vim does when skipping) there and it is not correct and may not be correct with this rule in effect. This statement in the help file exists since the first version recorded in mercurial repository and was also found in a 6.0 version archive: ":exe 'if'" was never correct. I just ask to make it not only officially incorrect, but also not working.

                    Though it worked in 6.0 as well (not when skipping, of course).

                    >
                    > --
                    > BODY:        I'm not dead!
                    > CART DRIVER: 'Ere.  He says he's not dead.
                    > LARGE MAN:   Yes he is.
                    > BODY:        I'm not!
                    >                  "Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD
                    >
                    >  /// Bram Moolenaar -- Bram@... -- http://www.Moolenaar.net   \\\
                    > ///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
                    > \\\  an exciting new programming language -- http://www.Zimbu.org        ///
                    >  \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

                    --
                    --
                    You received this message from the "vim_dev" maillist.
                    Do not top-post! Type your reply below the text you are replying to.
                    For more information, visit http://www.vim.org/maillist.php

                    ---
                    You received this message because you are subscribed to the Google Groups "vim_dev" group.
                    To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
                    For more options, visit https://groups.google.com/d/optout.
                  • Bram Moolenaar
                    ... The problem with skipping is that it should not evaluate :execute statements, since that may have side effects. But then the matching endif could be
                    Message 9 of 9 , Mar 23, 2014
                      Nikolay Pavlov wrote:

                      > On Mar 23, 2014 12:19 AM, "Bram Moolenaar" <Bram@...> wrote:
                      > >
                      > >
                      > > ZyX wrote:
                      > >
                      > > > > The docs are wrong, using "if" is allowed. The other two are not.
                      > > >
                      > > > Why? It makes parsing impossible. E.g. like indicated by @Yukihiro
                      > > > Nakadaira, there is absolutely nothing you can do with this error
                      > > > unless you add hacks for some specific cases (e.g. if `execute` is
                      > > > followed by a string literal starting with `if`).
                      > >
                      > > The code is full of exceptions. There is a lot of history, some dating
                      > > back to the days of Ex. The language wasn't designed to make life for
                      > > parser writers easier. Some parts of the language weren't designed at
                      > > all, they just happened. Not breaking existing Vim scripts is the main
                      > > goal, thus making changes in the language to simplify the implementation
                      > > will cause headeaches for users.
                      >
                      > The problem is that you have a parser (I mean what vim does when skipping)
                      > there and it is not correct and may not be correct with this rule in
                      > effect. This statement in the help file exists since the first version
                      > recorded in mercurial repository and was also found in a 6.0 version
                      > archive: ":exe 'if'" was never correct. I just ask to make it not only
                      > officially incorrect, but also not working.

                      The problem with skipping is that it should not evaluate ":execute"
                      statements, since that may have side effects. But then the matching
                      "endif" could be missed. And a nested "if-endif" is not seen. Sounds
                      like a catch 22.

                      Let me update the help text to this:

                      Note: The executed string may be any command-line, but
                      starting or ending "if", "while" and "for" does not
                      always work, because when commands are skipped the
                      ":execute" is not evaluated and Vim loses track of
                      where blocks start and end. Also "break" and
                      "continue" should not be inside ":execute".
                      This does not work, because the ":execute" is not
                      evaluated and Vim does not see the "while", and gives
                      an error for finding an ":endwhile": >
                      :if 0
                      : execute 'while i > 5'
                      : echo "test"
                      : endwhile
                      :endif

                      Generally, it should not be necessary to put "if", "while" and "for"
                      inside an execute, since these commands evaluate an expression
                      themselves.

                      Should we turn these "don't do this" instructions into an error?

                      > Though it worked in 6.0 as well (not when skipping, of course).

                      How about writing tests for these cases? If you are writing a new
                      parser you must have a way to verify it does the right thing.

                      --
                      Friends? I have lots of friends! In fact, I have all episodes ever made.

                      /// Bram Moolenaar -- Bram@... -- http://www.Moolenaar.net \\\
                      /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
                      \\\ an exciting new programming language -- http://www.Zimbu.org ///
                      \\\ help me help AIDS victims -- http://ICCF-Holland.org ///

                      --
                      --
                      You received this message from the "vim_dev" maillist.
                      Do not top-post! Type your reply below the text you are replying to.
                      For more information, visit http://www.vim.org/maillist.php

                      ---
                      You received this message because you are subscribed to the Google Groups "vim_dev" group.
                      To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
                      For more options, visit https://groups.google.com/d/optout.
                    Your message has been successfully submitted and would be delivered to recipients shortly.