Wednesday, May 18, 2011

Re: 'commandline' vs ':e' for jump to line (FEAT. REQ)

On 18/05/11 9:27 PM, Tim Chase wrote:
> On 05/18/2011 03:32 AM, Ben Schmidt wrote:
>> Here's a simple solution for .vimrc that might work:
>>
>> command! -nargs=1 -bang Edit call FileAndLine("e","<bang>",<f-args>)
>> command! -nargs=1 -bang Split call FileAndLine("sp","<bang>",<f-args>)
>> function! FileAndLine(cmd,bang,arg)
>> let file=matchstr(a:arg,'.\{-}\ze:')
>> let line=matchstr(a:arg,':\zs\d\+')
>> exec a:cmd.a:bang." +".line." ".fnameescape(file)
>> endfunction
>>
>> Then :Edit abc.vim:12 will turn into :e +12 abc.vim and :Split the same,
>> but into :sp not :e. Passes on a ! too.
>>
>> Not tested much, and may have nasty edge cases, but should be a good
>> start.
>
> Edge-cases that stick out to me involve non-digit text after the colon such as
> "C:\path\to\file.txt", "resident_evil:afterlife.mov" or "rockets:321blastoff.swf"
> This could be remedied by tightening the regexps:
>
> let file=matchstr(a:arg,'^.*\ze:\d\+$')
> let line=matchstr(a:arg,'^.*:\zs\d\+')

Indeed. I was, perhaps foolishly, thinking about an entire error
message, so avoiding matching too much with greedy matches, e.g.
something like

foo.c:25: error: whatever

But it's probably the user's responsibility to copy/paste a bit more
accurately than that. Still...if I had to use copy/paste at all, rather
than Vim's quickfix, if I could, I'd probably just be lazy and triple
click or something to grab a whole line and slam it in!

And it seems unliekly to me that gcc would find an error in
"resident_evil:afterlife.mov" or "rockets:321blastoff.swf". :-)

> And there are failure conditions if the filename doesn't contain a colon, so you
> might put in a check something like
>
> if file==''
> let file=a:arg
> endif
> if line==''
> let line=1
> endif
>
> before issuing the :exec call.

Thanks for the second pair of eyes! Those are definitely good checks.

Ben.

--
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: