Sunday, July 15, 2012

Re: vimdiff: displaying the total number of changes

Am 19.06.2012 19:41, schrieb Ben Fritz:
> On Monday, June 18, 2012 4:59:51 AM UTC-5, Jonathan Fudger wrote:
>> Hi everyone,
>>
>> When diffing two files, I have been wondering if is it possible to
>> display the total number of differences between the files (i.e. the
>> number of ]c motions required to get to the end).
>>
>> Ideally I would like a statusline (or simply an echoed message) that
>> says something like "This is difference N of M".
>>
>> I have been vimscripting for quite a few years now, but I have no
>> idea how to do this, so any advice will be gratefully received!
>>
>
> I don't know of any built-in way. You could probably go to the top of
> the file and user :normal! ]c in a while loop, counting the number of
> times you do ]c until the cursor stops moving. There may be a better
> way, but since nobody else has chimed in...

Here are two more ideas, the first is to examine the diff output and
store the number of changes in a variable:
:h 'diffexpr
:h diff-diffexpr

set diffexpr=DiffAndCountChanges()

func! DiffAndCountChanges()
let argmap = {'icase': '-i', 'iwhite': '-b'}
let opt = join(map(split(&diffopt, ","), 'get(argmap, v:val, "")'))
sil exec printf("!diff -a --binary %s %s %s > %s", opt, v:fname_in, v:fname_new, v:fname_out)
let dcmdpat = '^[[:digit:],dca]\+$'
let g:diff_number_of_changes = len(filter(readfile(v:fname_out), 'v:val =~ dcmdpat'))
endfunc


Another option is to make use of
:h diff_hlID()
:h diff_filler()

" return the number of diff hunks
func! DiffNumberOfChanges()
let rhl = map(range(1, line("$")), 'diff_hlID(v:val, 1)')
let hunks1 = strlen(substitute(
\ substitute(join(rhl, "")."0", '[1-9]0', 'x', 'g'), '[^x]', '', 'g'))
let hunks2 = len(filter(map(range(1, line("$")+1), 'diff_filler(v:val)'), 'v:val>=1'))
return hunks1 + hunks2
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:

Post a Comment