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

goto line depending on exit code of filter

Expand Messages
  • bill lam
    I want to use an external program to filter the whole buffer. Upon exit of that filter and output written to current buffer, I want to goto a line position
    Message 1 of 11 , Aug 22, 2009
    • 0 Attachment
      I want to use an external program to filter the whole buffer. Upon
      exit of that filter and output written to current buffer, I want to
      goto a line position depending on exit code:

      0 : goto the current line just before invoking filter
      +ve : error occurred, goto the line indicated by the error code.

      how to write a user command to do it assuming the filter is called
      with :%!foo

      Thanks in advance.

      --
      regards,
      ====================================================
      GPG key 1024D/4434BAB3 2008-08-24
      gpg --keyserver subkeys.pgp.net --recv-keys 4434BAB3

      --~--~---------~--~----~------------~-------~--~----~
      You received this message from the "vim_use" maillist.
      For more information, visit http://www.vim.org/maillist.php
      -~----------~----~----~----~------~----~------~--~---
    • Jürgen Krämer
      Hi, ... have a look at ... This variable holds the result of the last shell command. Regards, Jürgen -- Sometimes I think the surest sign that intelligent
      Message 2 of 11 , Aug 22, 2009
      • 0 Attachment
        Hi,

        bill lam wrote:
        >
        > I want to use an external program to filter the whole buffer. Upon
        > exit of that filter and output written to current buffer, I want to
        > goto a line position depending on exit code:
        >
        > 0 : goto the current line just before invoking filter
        > +ve : error occurred, goto the line indicated by the error code.
        >
        > how to write a user command to do it assuming the filter is called
        > with :%!foo

        have a look at

        :help v:shell_error

        This variable holds the result of the last shell command.

        Regards,
        Jürgen

        --
        Sometimes I think the surest sign that intelligent life exists elsewhere
        in the universe is that none of it has tried to contact us. (Calvin)

        --~--~---------~--~----~------------~-------~--~----~
        You received this message from the "vim_use" maillist.
        For more information, visit http://www.vim.org/maillist.php
        -~----------~----~----~----~------~----~------~--~---
      • bill lam
        ... Thanks for help. Excuse me for being dense. How to write a vimscript command to jump to line number v:shell_error? ... -- regards,
        Message 3 of 11 , Aug 22, 2009
        • 0 Attachment
          On Sat, 22 Aug 2009, Jürgen Krämer wrote:
          > have a look at
          >
          > :help v:shell_error
          >
          > This variable holds the result of the last shell command.

          Thanks for help. Excuse me for being dense. How to write a vimscript
          command to jump to line number v:shell_error?

          :if v:shell_error do.
          : (jump to the error line)
          :else
          : (jump to the previous current line)
          :end


          --
          regards,
          ====================================================
          GPG key 1024D/4434BAB3 2008-08-24
          gpg --keyserver subkeys.pgp.net --recv-keys 4434BAB3

          --~--~---------~--~----~------------~-------~--~----~
          You received this message from the "vim_use" maillist.
          For more information, visit http://www.vim.org/maillist.php
          -~----------~----~----~----~------~----~------~--~---
        • Ben Fritz
          ... I m not sure where you get the error line number from, but once you have it, Vim s if syntax is if...elseif...else...endif and is otherwise just as you
          Message 4 of 11 , Aug 22, 2009
          • 0 Attachment
            On Aug 22, 5:53 am, bill lam <cbill....@...> wrote:
            > On Sat, 22 Aug 2009, Jürgen Krämer wrote:
            > > have a look at
            >
            > >   :help v:shell_error
            >
            > > This variable holds the result of the last shell command.
            >
            > Thanks for help. Excuse me for being dense. How to write a vimscript
            > command to jump to line number v:shell_error?
            >
            > :if v:shell_error do.
            > :  (jump to the error line)
            > :else
            > :  (jump to the previous current line)
            > :end
            >

            I'm not sure where you get the error line number from, but once you
            have it, Vim's if syntax is if...elseif...else...endif and is
            otherwise just as you have written (without the "do").

            You can jump to a line number in variable foo with :exec foo

            This is because :5 will go to line 5, :10 will go to line 10, etc., so
            "exec foo" will execute the value of variable "foo" as an ex command.
            If foo contains the line number, you will jump there.

            You could contain all this in a function with

            function Myfunc
            ...
            endfunction

            and then :call Myfunc() to execute the script.

            Finally, you could create your own custom command or mapping to call
            it for you:

            :command Mycommand call Myfunc()
            :nnoremap <F10> :call Myfunc()<CR>

            It is also possible to call it automatically whenever you do a filter
            with

            autocmd ShellFilterPost * call Myfunc()

            but that would run on EVERY filter, not just this one, so I'm not sure
            I'd recommend that.
            --~--~---------~--~----~------------~-------~--~----~
            You received this message from the "vim_use" maillist.
            For more information, visit http://www.vim.org/maillist.php
            -~----------~----~----~----~------~----~------~--~---
          • Tim Chase
            ... Though it s a bit of a horrible abuse, you can do if v:shell_error 0 | exec v:shell_error | endif which exploits the fact that an ex command of just a
            Message 5 of 11 , Aug 22, 2009
            • 0 Attachment
              >> have a look at
              >>
              >> :help v:shell_error
              >>
              >> This variable holds the result of the last shell command.
              >
              > Thanks for help. Excuse me for being dense. How to write a vimscript
              > command to jump to line number v:shell_error?
              >
              > :if v:shell_error do.
              > : (jump to the error line)
              > :else
              > : (jump to the previous current line)
              > :end

              Though it's a bit of a horrible abuse, you can do

              if v:shell_error > 0 | exec v:shell_error | endif

              which exploits the fact that an ex command of just a number means
              to go to that line. Granted, the range of exit codes may limit
              you to a pretty small range (1-127, IIRC), so you can't jump to a
              line beyond this max. But it's what you asked for :)

              There's no good help target that I could find for the ":<number>"
              syntax -- the closest I could get are:

              :help gg " the item after it is ":[range]" with no tag
              :help cmdline.txt " search for "goto line 3"
              :help cpo-- " has a mention of using it
              :help 'sol' " has a mention of using it

              Hope this helps,

              -tim




              --~--~---------~--~----~------------~-------~--~----~
              You received this message from the "vim_use" maillist.
              For more information, visit http://www.vim.org/maillist.php
              -~----------~----~----~----~------~----~------~--~---
            • bill lam
              ... Thanks Tim and Ben! exec number does the trick. I got a pretty print for my source files, when there is syntax error, I made it exit with an error code
              Message 6 of 11 , Aug 22, 2009
              • 0 Attachment
                On Sat, 22 Aug 2009, Tim Chase wrote:
                > Though it's a bit of a horrible abuse, you can do
                >
                > if v:shell_error > 0 | exec v:shell_error | endif
                >
                > which exploits the fact that an ex command of just a number means
                > to go to that line. Granted, the range of exit codes may limit
                > you to a pretty small range (1-127, IIRC), so you can't jump to a
                > line beyond this max. But it's what you asked for :)

                Thanks Tim and Ben! "exec number" does the trick.
                I got a pretty print for my source files, when there is syntax error,
                I made it exit with an error code equal to that line number. I tried
                and found vim can jump to lines beyond 4000.

                --
                regards,
                ====================================================
                GPG key 1024D/4434BAB3 2008-08-24
                gpg --keyserver subkeys.pgp.net --recv-keys 4434BAB3

                --~--~---------~--~----~------------~-------~--~----~
                You received this message from the "vim_use" maillist.
                For more information, visit http://www.vim.org/maillist.php
                -~----------~----~----~----~------~----~------~--~---
              • Tony Mechelynck
                ... Vim can jump to any line that it can count, the problem is having your external program generate the required number as a return code: IIRC, return codes
                Message 7 of 11 , Aug 31, 2009
                • 0 Attachment
                  On 22/08/09 16:56, bill lam wrote:
                  >
                  > On Sat, 22 Aug 2009, Tim Chase wrote:
                  >> Though it's a bit of a horrible abuse, you can do
                  >>
                  >> if v:shell_error> 0 | exec v:shell_error | endif
                  >>
                  >> which exploits the fact that an ex command of just a number means
                  >> to go to that line. Granted, the range of exit codes may limit
                  >> you to a pretty small range (1-127, IIRC), so you can't jump to a
                  >> line beyond this max. But it's what you asked for :)
                  >
                  > Thanks Tim and Ben! "exec number" does the trick.
                  > I got a pretty print for my source files, when there is syntax error,
                  > I made it exit with an error code equal to that line number. I tried
                  > and found vim can jump to lines beyond 4000.
                  >

                  Vim can jump to any line that it can count, the problem is having your
                  external program generate the required number as a return code: IIRC,
                  return codes are limited to one byte, and those above 127 may have a
                  special meaning (generated by the OS maybe).

                  In bash,

                  foobar
                  bash: foobar: command not found
                  echo $?

                  returns 127, for instance.

                  Depending on what you exactly want, you could also write it as

                  if v:shell_error | exe v:shell_error | endif

                  or

                  if v:xhell_error | exe (v:shell_error % 256) | endif

                  etc.


                  Best regards,
                  Tony.
                  --
                  hundred-and-one symptoms of being an internet addict:
                  117. You are more comfortable typing in html.

                  --~--~---------~--~----~------------~-------~--~----~
                  You received this message from the "vim_use" maillist.
                  For more information, visit http://www.vim.org/maillist.php
                  -~----------~----~----~----~------~----~------~--~---
                • bill lam
                  ... Tony, you are correct. I don t why it seemed to work last time I tested. How can I store content of the current buffer and restore it in case of error?
                  Message 8 of 11 , Sep 1, 2009
                  • 0 Attachment
                    On Tue, 01 Sep 2009, Tony Mechelynck wrote:
                    > external program generate the required number as a return code: IIRC,
                    > return codes are limited to one byte, and those above 127 may have a
                    > special meaning (generated by the OS maybe).

                    Tony, you are correct. I don't why it seemed to work last time I
                    tested.

                    How can I store content of the current buffer and restore it in case
                    of error? like the following pseudo code

                    let tmp=%
                    %!filter
                    if v:xhell_error | set %=tmp | endif

                    --
                    regards,
                    ====================================================
                    GPG key 1024D/4434BAB3 2008-08-24
                    gpg --keyserver subkeys.pgp.net --recv-keys 4434BAB3

                    --~--~---------~--~----~------------~-------~--~----~
                    You received this message from the "vim_use" maillist.
                    For more information, visit http://www.vim.org/maillist.php
                    -~----------~----~----~----~------~----~------~--~---
                  • Matt Wozniski
                    ... Depends on the OS, but on Unix... Return codes are limited to one byte Applications are free to return any number in that range, none of them are
                    Message 9 of 11 , Sep 1, 2009
                    • 0 Attachment
                      On Tue, Sep 1, 2009 at 2:55 AM, Tony Mechelynck wrote:
                      >
                      > Vim can jump to any line that it can count, the problem is having your
                      > external program generate the required number as a return code: IIRC,
                      > return codes are limited to one byte, and those above 127 may have a
                      > special meaning (generated by the OS maybe).

                      Depends on the OS, but on Unix...

                      Return codes are limited to one byte

                      Applications are free to return any number in that range, none of them
                      are "special", but...

                      The operating system often uses return codes between 128 and 255 to
                      indicate death by signal. The solaris machine I'm sitting in front of
                      uses 128 + (signal number) when an app dies of a signal, and Linux
                      uses the same. I could swear that an older version of solaris used
                      256 - (signal number), but I can't confirm that at the moment, so
                      don't quote me on that.

                      The 127 return code from bash isn't OS-level special, it's just a
                      convenience thing that bash does to make it easier to guess when a
                      script failed because it couldn't find a command.

                      ~Matt

                      --~--~---------~--~----~------------~-------~--~----~
                      You received this message from the "vim_use" maillist.
                      For more information, visit http://www.vim.org/maillist.php
                      -~----------~----~----~----~------~----~------~--~---
                    • Tony Mechelynck
                      ... what about (untested) %!filter if v:shell_error undo endif ? If it doesn t work you can always do w! /temp/file %!filter if v:shell_error e! /temp/file
                      Message 10 of 11 , Sep 1, 2009
                      • 0 Attachment
                        On 01/09/09 12:18, bill lam wrote:
                        >
                        > On Tue, 01 Sep 2009, Tony Mechelynck wrote:
                        >> external program generate the required number as a return code: IIRC,
                        >> return codes are limited to one byte, and those above 127 may have a
                        >> special meaning (generated by the OS maybe).
                        >
                        > Tony, you are correct. I don't why it seemed to work last time I
                        > tested.
                        >
                        > How can I store content of the current buffer and restore it in case
                        > of error? like the following pseudo code
                        >
                        > let tmp=%
                        > %!filter
                        > if v:xhell_error | set %=tmp | endif
                        >

                        what about (untested)

                        %!filter
                        if v:shell_error
                        undo
                        endif

                        ?

                        If it doesn't work you can always do

                        w! /temp/file
                        %!filter
                        if v:shell_error
                        e! /temp/file
                        saveas #
                        endif

                        but it isn't very elegant.


                        Best regards,
                        Tony.
                        --
                        President Reagan has noted that there are too many economic pundits and
                        forecasters and has decided on an excess prophets tax.

                        --~--~---------~--~----~------------~-------~--~----~
                        You received this message from the "vim_use" maillist.
                        For more information, visit http://www.vim.org/maillist.php
                        -~----------~----~----~----~------~----~------~--~---
                      • bill lam
                        ... Thanks Tony, it works. -- regards, ==================================================== GPG key 1024D/4434BAB3 2008-08-24 gpg --keyserver subkeys.pgp.net
                        Message 11 of 11 , Sep 2, 2009
                        • 0 Attachment
                          On Wed, 02 Sep 2009, Tony Mechelynck wrote:
                          > what about (untested)
                          >
                          > %!filter
                          > if v:shell_error
                          > undo
                          > endif
                          >
                          > ?
                          >
                          > If it doesn't work you can always do

                          Thanks Tony, it works.

                          --
                          regards,
                          ====================================================
                          GPG key 1024D/4434BAB3 2008-08-24
                          gpg --keyserver subkeys.pgp.net --recv-keys 4434BAB3

                          --~--~---------~--~----~------------~-------~--~----~
                          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.