Saturday, June 19, 2010

Re: diff and synchronisation

Am 18.06.2010 15:04, schrieb Luc Hermitte:
> Hello,
>
> I was wondering if there was a way to know the set of matching lines
> when in diff mode (after a ]c)
>
> After a ]c (/[c), I've first tried to go from one window to the other,
> but then the cursor is not on the same lines in the two (or more)
> synched windows.
>
> I've quickly discarded any solution where I could directly play with
> "ln = line('.') ; focus on next window ; l2 = getline(ln)" as the
> diffed buffers won't have the same number of lines.
>
> Then, I've noticed that both windows have their first line
> synchronized. I though at first I could directly use " ln = winline()
> ; ^Ww ; exe 'normal '.ln.'H'". Unfortunately, winline() counts the
> fake lines than are introduced by diff-mode, while H works on actual
> lines, ignoring the fake ones.
>
> So, is there a solution other than iteratively moving down until both
> winline() results are equal ?

No idea, but: I've once struggled with the same issue ...
This code somehow works for me since 2008 ;-)


" vimrc
augroup anwoku
au WinEnter * if &diff| exec "call vimrc#DiffCursor(s:diffcursor)"| endif
au WinLeave * let s:diffcursor = &diff ? eval("vimrc#DiffCursor()") : []
" eval() and exec prevent loading vimrc.vim too early
" ^ not sure if this is needed any more [2010 Jun 19]
augroup End

" autoload/vimrc.vim
func! vimrc#DiffCursor(...) "{{{
" added 12-10-2008
" when moving between diff windows, keep the relative cursor position in
" the window
if a:0 == 0
" get cursor position in the diff window being left
" get number of "virtual" filler lines for visible lines below and
" above the cursor. Problem in the top "real" line: filler lines
" above it may exceed the window boundary!
if line("w0") < line(".")
" let nfiller = eval(join(map(range(line("w0")+1,line(".")),
" 'diff_filler(v:val)'),'+'))
let nfiller = 0
let lnum = line("w0")+1
while lnum <= line(".")
let nfiller += diff_filler(lnum)
let fcelnum = foldclosedend(lnum)
let lnum = fcelnum<0 ? lnum+1 : fcelnum+1
endwhile
else
let nfiller = 0
endif
if diff_filler(line("w0")) >= 1
return [-1, line(".")-line("w0") + nfiller, col(".")]
else
let addID = hlID("DiffAdd")
let lnum = line("w0")
while lnum < line("w$") && diff_hlID(lnum, 1) == addID
let lnum += 1
endwhile
return [lnum-line("w0"), line(".")-line("w0") + nfiller, col(".")]
endif
elseif !empty(a:1)
" set cursor position in the diff window just entered
let nvirtlines = a:1[1]
if a:1[0] == -1
let addID = hlID("DiffAdd")
let lnum = line("w0")
while lnum < line("w$") && diff_hlID(lnum, 1) == addID
let lnum += 1
endwhile
elseif diff_filler(line("w0")) >= 1
" window starts with filler lines
let nvirtlines -= a:1[0]
let lnum = line("w0")
else
let lnum = line("w0")
endif
while nvirtlines > 0
let lnum += 1
let fcelnum = foldclosedend(lnum)
if fcelnum < 0
let nvirtlines -= 1 + diff_filler(lnum)
else
let nvirtlines -= 1 + fcelnum - lnum
let lnum = fcelnum
endif
endwhile
call cursor(lnum, a:1[2])
endif
endfunc "}}}

--
Andy

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