Wednesday, December 29, 2010

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

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

No comments: