Thursday, January 23, 2014

Re: Avoid switching files with ctrl-o

On Thu, January 23, 2014 11:01, Christian Brabandt wrote:
> On Thu, January 23, 2014 10:26, Erik Christiansen wrote:
>> On 23.01.14 10:00, Christian Brabandt wrote:
>>> I have actually also recently just thought about adding a
>>> g<C-O>/g<C-I> command to Vim, that would jump by file steps which
>>> means there would be a built-in way to do what you want.
>>
>> I'm not sure how many keys need to be pressed simultaneously to produce
>> g<C-O>, but following a recent thread, I'm using Marcin Szamotulski's
>> function, on Alt-O & Alt-I:
>>
>> nm <A-o> :call FileJump(v:count1, 'b')<cr>
>> nm <A-i> :call FileJump(v:count1, 'f')<cr>
>>
>> " Alt-O & Alt-I between files, just as ^O & ^I retrace move history:
>> " Thanks to Marcin Szamotulski.
>>
>> fun! FileJump(count, ...)
>> let backward = (a:0 >= 1 ? a:1 : 'b')
>> let ind = (backward == 'b' ? -1 : 1)
>> for x in range(a:count)
>> let file = expand('%:p')
>> while file == expand('%:p')
>> let line = line('.')
>> if ind == 1
>> exe "normal! 1\<C-I>"
>> else
>> exe "normal! 1\<C-O>"
>> endif
>> if line == line('.') && file == expand('%:p')
>> break
>> endif
>> endwhile
>> endfor
>> endfun
>>
>> Since that's a lot shorter, I figure that your version does something
>> beyond the straightforward file hopping which meets all the needs I'm
>> aware that I have. (So what are we missing out on? :-)
>
> It does something slightly different. My version always jumps to the
> first jump in the current file (and won't leave the file), while
> Marcin's version always jumps at least one file backwards or forwards
> (and thus won't solve the issue, the OP noticed).
>
> Now that I think about it, I guess my version also does not fully
> solve the OPs original question, as he wants <C-O> to stop at
> file boundaries. Oh well, I guess, I misunderstood something.

Here is a version, that should do, what the OP wanted:

fu! <sid>JumpInFile(forward)
redir => a |exe ":sil jumps"|redir end
let b = split(a, '\n')[1:]
let idx = match(b, '^>.*')
if idx == -1
return "\<esc>"
endif
let idx = (a:forward ? idx-1 : idx+1)
let i = 0
if get(b, idx, -1) != -1
let entry = matchlist(get(b, idx),
\ '^\s*\(\d\+\)\s\+\(\d\+\)\s\+\(\d\+\)'.
\ '\s\+\(.*\)')
if empty(entry)
return "\<esc>"
endif
if !empty(entry[4])
let line = matchstr(getline(entry[2]+0), '^\s*\zs.\{'.
\ strwidth(entry[4]).'}')
else
let line = getline(entry[2]+0)
endif
if line ==# entry[4]
if a:forward && entry[1] > 0
return (entry[1]) . "\<C-O>"
elseif !a:forward && entry[1] > 0
return (entry[1]-1) . "\<C-I>"
endif
else
return "\<esc>"
endif
endif
return "\<esc>"
endfu

nnoremap <expr> g<C-O> <sid>JumpInFile(1)
nnoremap <expr> g<C-I> <sid>JumpInFile(0)


--
--
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 because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment