Tomalak
Tomalak

Reputation: 338326

vim executing diff command twice

I found a peculiar behavior with my vim: It executes every diff command twice and I'm not sure why.

(I'm running this on Windows, I'm suspecting this has something to do with it.) As per the answer, it hasn't.

So I have this in my vimrc:

set diffexpr=MyDiff()

function! DoubleQuoteCond(s)
  return a:s =~ ' ' ? '"' . a:s . '"' : a:s
endfunction

function! MyDiff()
   if &shell =~ '\<cmd'
    let prog = DoubleQuoteCond($VIMRUNTIME . '\diff')
  else
    let prog = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff')
  endif

  let opt = '-a --binary'
  if &diffopt =~ 'icase' | let opt .= ' -i' | endif
  if &diffopt =~ 'iwhite' | let opt .= ' -b' | endif

  let file1 = DoubleQuoteCond(v:fname_in)
  let file2 = DoubleQuoteCond(v:fname_new)
  let file3 = DoubleQuoteCond(v:fname_out)

  let cmdline = join([prog, opt, file1, file2, '>', file3], ' ')

  " no silent execute for debugging reasons
  execute '!' . cmdline 
endfunction

Pretty simple function that builds a cmdline like this one:

"C:\Program Files\Vim\vim\diff" -a --binary C:\Temp\VIoCC20.tmp C:\Temp\VInCC21.tmp > C:\Temp\VIdCC22.tmp

Now this happens:

When I set diffexpr to '' the diff shell command is executed only once.

Where does the second execution come from? What can I do to prevent it (other than not using a custom diffexpr, of course)?

Upvotes: 2

Views: 395

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172688

When you set 'diffexpr'

Vim will do a test if the diff output looks alright.

(This is from :help E810.)

You cannot avoid this. However, you can check for the test, as Vim passes constant dummy file contents ("line1" and "line2", respectively), and short-circuit the external command execution in this case, like this:

if
\   getfsize(v:fname_in) <= 6 &&
\   getfsize(v:fname_new) <= 6 &&
\   readfile(v:fname_in, 0, 1)[0] ==# 'line1' &&
\   readfile(v:fname_new, 0, 1)[0] ==# 'line2'
    return
endif

Upvotes: 2

Related Questions