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

1st attempt at an autocmd - name:line, name:line:col

Expand Messages
  • Dr. David Alan Gilbert
    Hi, Below is my first attempt at writing an autocmd (and for that matter a vim script) and I d appreciate any feedback (e.g. no you fool, don t do it like
    Message 1 of 10 , Dec 29, 2010
      Hi,
      Below is my first attempt at writing an autocmd (and for that
      matter a vim script) and I'd appreciate any feedback (e.g.
      'no you fool, don't do it like that, do it like this....', or
      I wouldn't be surprised to be told someone has already done it).

      This lets you do:

      vi file:20

      and it will load 'file' and jump to line 20, or

      vi file:20:30

      and it will load 'file' and jump to line 20, column 30
      It will also accept a : at the end, e.g.

      vi file:20:30:

      Really, it's for where you have the output of a compiler or the like
      (sparse in my case) but for one reason or another want to open it in
      a separate vim.

      Dave

      {<>}{<>}{<>}{<>}{<>}{<>}{<>}{<>}{<>}{<>}{<>}{<>}{<>}{<>}{<>}{<>}{<>}{<>}{<>}

      function s:FileandLinenumber(rawname)
      " Allow a : at the end, as from sparse
      if a:rawname =~ ':$'
      let tmpname=substitute(a:rawname, ':$', "", "")
      else
      let tmpname=a:rawname
      endif

      " Note the match in the autocmd is glob so can have junk after the number
      if tmpname =~ '.*:[0-9][0-9]*$'
      let filename=substitute(tmpname, ':[0-9][0-9]*$', "", "")
      let lastnum=substitute(tmpname, '.*:', "", "")

      " We allow name:line:col, so filename might actually still be name:line
      " at this point
      if filename =~ '.*:[0-9][0-9]*$'
      " OK so this is name:line:col
      let colnum=lastnum
      let linenum=substitute(filename, '.*:', "", "")
      let filename=substitute(filename, ':[0-9][0-9]*$', "", "")
      else
      let colnum=-1
      let linenum=lastnum
      endif
      exe "e ".fnameescape(filename)
      exe linenum
      if (colnum != -1)
      exe "normal " . colnum . "|"
      endif
      endif
      endfunction

      augroup davesfileandnumber
      autocmd davesfileandnumber BufNewFile *:* nested call s:FileandLinenumber( expand("<afile>") )

      {<>}{<>}{<>}{<>}{<>}{<>}{<>}{<>}{<>}{<>}{<>}{<>}{<>}{<>}{<>}{<>}{<>}{<>}{<>}

      --
      -----Open up your eyes, open up your mind, open up your code -------
      / Dr. David Alan Gilbert | Running GNU/Linux | Happy \
      \ gro.gilbert @ treblig.org | | In Hex /
      \ _________________________|_____ http://www.treblig.org |_______/

      --
      You received this message from the "vim_use" 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
    • Christian Brabandt
      Hi Dr.! ... I haven t looked at your script, but I d like to let you know, that there already exists the file:line plugin:
      Message 2 of 10 , Dec 30, 2010
        Hi Dr.!

        On Mi, 29 Dez 2010, Dr. David Alan Gilbert wrote:

        > Below is my first attempt at writing an autocmd (and for that
        > matter a vim script) and I'd appreciate any feedback (e.g.
        > 'no you fool, don't do it like that, do it like this....', or
        > I wouldn't be surprised to be told someone has already done it).

        I haven't looked at your script, but I'd like to let you know, that
        there already exists the file:line plugin:
        http://www.vim.org/scripts/script.php?script_id=2184

        which seems to do, what you want.

        regards,
        Christian

        --
        You received this message from the "vim_use" 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
      • Dr. David Alan Gilbert
        ... Hi Christian, Thanks for your response. ... That plugin is interesting; it only does file:line while mine does file:line:col and also allows a trailing :
        Message 3 of 10 , Dec 30, 2010
          * Christian Brabandt wrote:
          > Hi Dr.!

          Hi Christian,
          Thanks for your response.

          > On Mi, 29 Dez 2010, Dr. David Alan Gilbert wrote:
          >
          > > Below is my first attempt at writing an autocmd (and for that
          > > matter a vim script) and I'd appreciate any feedback (e.g.
          > > 'no you fool, don't do it like that, do it like this....', or
          > > I wouldn't be surprised to be told someone has already done it).
          >
          > I haven't looked at your script, but I'd like to let you know, that
          > there already exists the file:line plugin:
          > http://www.vim.org/scripts/script.php?script_id=2184
          >
          > which seems to do, what you want.

          That plugin is interesting; it only does file:line while mine does
          file:line:col and also allows a trailing : , however it does
          clean a few things up - I can see from the history the author has
          found a few things that need to be fixed that I also didn't think
          of.
          Victor also uses matchlist which looks like a much saner way of doing things
          than the multiple substitute() calls that I use to slice up the
          filename.

          Thanks!

          Dave
          --
          -----Open up your eyes, open up your mind, open up your code -------
          / Dr. David Alan Gilbert | Running GNU/Linux | Happy \
          \ gro.gilbert @ treblig.org | | In Hex /
          \ _________________________|_____ http://www.treblig.org |_______/

          --
          You received this message from the "vim_use" 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
        • Dr. David Alan Gilbert
          Hi Victor, (and vim list cc d) I posted a script similar to your file:line script to vim@vim.org and Christian Brabandt was good enough to point me at your
          Message 4 of 10 , Jan 2, 2011
            Hi Victor, (and vim list cc'd)
            I posted a script similar to your file:line script to vim@...
            and Christian Brabandt was good enough to point me at your file:line plugin
            here:
            http://www.vim.org/scripts/script.php?script_id=2184

            My script also deals with file:line: and file:line:column and file:line:column:
            but the original version wasn't as tidy as yours and hadn't taken into account
            things like folding and deleting the original buffer like yours had.

            So please find below a modified version of your script that adds the file:line:column
            varient and allows a : at the end.

            ------------------------------------------------------------------------------
            function! s:gotoline()
            let file = bufname("%")

            if file =~ ':$'
            let file = substitute(file, ':$', "", "")
            endif

            " Lets try with name:line:column
            let names = matchlist( file, '\(.*\):\(\d\+\):\(\d\+\)')
            if len(names) == 0
            " OK, lets try just name:line
            let names = matchlist( file, '\(.*\):\(\d\+\)')
            endif


            if len(names) != 0 && filereadable(names[1])
            let l:bufn = bufnr("%")
            exec ":e " . names[1]
            exec ":" . names[2]
            exec ":bdelete " . l:bufn
            if foldlevel(names[2]) > 0
            exec ":foldopen!"
            endif

            if (names[3])
            exe "normal " . names[3] . "|"
            endif
            endif

            endfunction

            autocmd! BufNewFile *:* nested call s:gotoline()

            ------------------------------------------------------------------------------
            --
            -----Open up your eyes, open up your mind, open up your code -------
            / Dr. David Alan Gilbert | Running GNU/Linux | Happy \
            \ gro.gilbert @ treblig.org | | In Hex /
            \ _________________________|_____ http://www.treblig.org |_______/

            --
            You received this message from the "vim_use" 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
          • Yegappan Lakshmanan
            Hi, ... The above code using matchlist() can be replaced by the following: let names = split(file, : ) - Yegappan ... -- You received this message from the
            Message 5 of 10 , Jan 2, 2011
              Hi,

              >
              >        " Lets try with name:line:column
              >        let names =  matchlist( file, '\(.*\):\(\d\+\):\(\d\+\)')
              >        if len(names) == 0
              >                " OK, lets try just name:line
              >                let names =  matchlist( file, '\(.*\):\(\d\+\)')
              >        endif
              >

              The above code using matchlist() can be replaced by the following:

              let names = split(file, ':')

              - Yegappan


              On Sun, Jan 2, 2011 at 4:40 PM, Dr. David Alan Gilbert <dave@...> wrote:
              > Hi Victor, (and vim list cc'd)
              >  I posted a script similar to your file:line script to vim@...
              > and Christian Brabandt was good enough to point me at your file:line plugin
              > here:
              >  http://www.vim.org/scripts/script.php?script_id=2184
              >
              > My script also deals with file:line: and file:line:column and file:line:column:
              > but the original version wasn't as tidy as yours and hadn't taken into account
              > things like folding and deleting the original buffer like yours had.
              >
              > So please find below a modified version of your script that adds the file:line:column
              > varient and allows a : at the end.
              >
              > ------------------------------------------------------------------------------
              > function! s:gotoline()
              >        let file = bufname("%")
              >
              >        if file =~ ':$'
              >                let file = substitute(file, ':$', "", "")
              >        endif
              >
              >        " Lets try with name:line:column
              >        let names =  matchlist( file, '\(.*\):\(\d\+\):\(\d\+\)')
              >        if len(names) == 0
              >                " OK, lets try just name:line
              >                let names =  matchlist( file, '\(.*\):\(\d\+\)')
              >        endif
              >
              >        if len(names) != 0 && filereadable(names[1])
              >                let l:bufn = bufnr("%")
              >                exec ":e " . names[1]
              >                exec ":" . names[2]
              >                exec ":bdelete " . l:bufn
              >                if foldlevel(names[2]) > 0
              >                        exec ":foldopen!"
              >                endif
              >
              >                if (names[3])
              >                        exe "normal " . names[3] . "|"
              >                endif
              >        endif
              >
              > endfunction
              >
              > autocmd! BufNewFile *:* nested call s:gotoline()
              >
              > ------------------------------------------------------------------------------
              > --
              >  -----Open up your eyes, open up your mind, open up your code -------
              > / Dr. David Alan Gilbert    |       Running GNU/Linux       | Happy  \
              > \ gro.gilbert @ treblig.org |                               | In Hex /
              >  \ _________________________|_____ http://www.treblig.org   |_______/
              >
              > --
              > You received this message from the "vim_use" 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 from the "vim_use" 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
            • Christian Brabandt
              Hi ... It probably doesn t matter in this case, but in general you should always use :normal! regards, Christian -- You received this message from the
              Message 6 of 10 , Jan 3, 2011
                Hi

                On Mo, 03 Jan 2011, Dr. David Alan Gilbert wrote:
                > if (names[3])
                > exe "normal " . names[3] . "|"
                > endif

                It probably doesn't matter in this case, but in general you should
                always use :normal!

                regards,
                Christian

                --
                You received this message from the "vim_use" 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
              • Dr. David Alan Gilbert
                ... Dave -- ... / Dr. David Alan Gilbert | Running GNU/Linux | Happy gro.gilbert @ treblig.org | | In Hex /
                Message 7 of 10 , Jan 3, 2011
                  * Yegappan Lakshmanan (yegappanl@...) wrote:
                  > Hi,
                  >
                  > >
                  > >        " Lets try with name:line:column
                  > >        let names =  matchlist( file, '\(.*\):\(\d\+\):\(\d\+\)')
                  > >        if len(names) == 0
                  > >                " OK, lets try just name:line
                  > >                let names =  matchlist( file, '\(.*\):\(\d\+\)')
                  > >        endif
                  > >
                  >
                  > The above code using matchlist() can be replaced by the following:
                  >
                  > let names = split(file, ':')

                  I think that would break where the filename contained a :

                  > - Yegappan

                  Dave
                  --
                  -----Open up your eyes, open up your mind, open up your code -------
                  / Dr. David Alan Gilbert | Running GNU/Linux | Happy \
                  \ gro.gilbert @ treblig.org | | In Hex /
                  \ _________________________|_____ http://www.treblig.org |_______/

                  --
                  You received this message from the "vim_use" 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
                • Dr. David Alan Gilbert
                  ... Why - what does that do for me? Dave -- ... / Dr. David Alan Gilbert | Running GNU/Linux | Happy gro.gilbert @ treblig.org |
                  Message 8 of 10 , Jan 3, 2011
                    * Christian Brabandt (cblists@...) wrote:
                    > Hi
                    >
                    > On Mo, 03 Jan 2011, Dr. David Alan Gilbert wrote:
                    > > if (names[3])
                    > > exe "normal " . names[3] . "|"
                    > > endif
                    >
                    > It probably doesn't matter in this case, but in general you should
                    > always use :normal!

                    Why - what does that do for me?

                    Dave
                    --
                    -----Open up your eyes, open up your mind, open up your code -------
                    / Dr. David Alan Gilbert | Running GNU/Linux | Happy \
                    \ gro.gilbert @ treblig.org | | In Hex /
                    \ _________________________|_____ http://www.treblig.org |_______/

                    --
                    You received this message from the "vim_use" 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
                  • ZyX
                    Reply to message «Re: Extended version of file:line script», sent 17:02:09 03 January 2011, Monday ... For you - nothing, at least now. It just makes your
                    Message 9 of 10 , Jan 3, 2011
                      Reply to message «Re: Extended version of file:line script»,
                      sent 17:02:09 03 January 2011, Monday
                      by Dr. David Alan Gilbert:


                      > * Christian Brabandt (cblists@...) wrote:
                      > > Hi
                      > >
                      > > On Mo, 03 Jan 2011, Dr. David Alan Gilbert wrote:
                      > > > if (names[3])
                      > > >
                      > > > exe "normal " . names[3] . "|"
                      > > >
                      > > > endif
                      > >
                      > > It probably doesn't matter in this case, but in general you should
                      > > always use :normal!
                      >
                      > Why - what does that do for me?
                      For you - nothing, at least now. It just makes your script independent of user
                      mappings: pretend that user has remapped `|' to `gg' for some reason. In this
                      case ``normal 10|'' will bring you to 10th line, but ``normal! 10|'' will work
                      as expected.
                    • Dr. David Alan Gilbert
                      ... Thanks! Dave -- ... / Dr. David Alan Gilbert | Running GNU/Linux | Happy gro.gilbert @ treblig.org | | In
                      Message 10 of 10 , Jan 3, 2011
                        * ZyX (zyx.vim@...) wrote:
                        > Reply to message «Re: Extended version of file:line script»,
                        > sent 17:02:09 03 January 2011, Monday
                        > by Dr. David Alan Gilbert:
                        >
                        > > Why - what does that do for me?
                        > For you - nothing, at least now. It just makes your script independent of user
                        > mappings: pretend that user has remapped `|' to `gg' for some reason. In this
                        > case ``normal 10|'' will bring you to 10th line, but ``normal! 10|'' will work
                        > as expected.

                        Thanks!

                        Dave


                        --
                        -----Open up your eyes, open up your mind, open up your code -------
                        / Dr. David Alan Gilbert | Running GNU/Linux | Happy \
                        \ gro.gilbert @ treblig.org | | In Hex /
                        \ _________________________|_____ http://www.treblig.org |_______/

                        --
                        You received this message from the "vim_use" 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
                      Your message has been successfully submitted and would be delivered to recipients shortly.