Skip to content

vimdiff does not work correctly when using &diffexpr #14437

@rickhowe

Description

@rickhowe

Steps to reproduce

I am trying to use diff mode and compare line-by-line by setting a following simple script to &diffexpr. This script skips an empty line and continues to compare the next line.

set diffexpr=MyDiff()
function! MyDiff()
  let [in, new] = [readfile(v:fname_in), readfile(v:fname_new)]
  let [len_in, len_new] = [len(in), len(new)]
  let out = []
  let [ln_in, ln_new] = [0, 0]
  while ln_in < len_in && ln_new < len_new
    let [text_in, text_new] = [in[ln_in], new[ln_new]]
    let [empty_in, empty_new] = [empty(text_in), empty(text_new)]
    if empty_in == empty_new
      if text_in != text_new
        let out += [(ln_in + 1) . 'c' . (ln_new + 1)]
      endif
      let [ln_in, ln_new] += [1, 1]
    else
      if empty_in | let ln_in += 1 | endif
      if empty_new | let ln_new += 1 | endif
    endif
  endwhile
  call writefile(out, v:fname_out)
endfunction

Case 1:

image
:windo diffthis
image
Both lines should be highlighted as changed lines, but the 2nd one in the left window is highlighted as added line.
In this case, ['1c1', '2c2'] is written to v:fname_out.

Case 2:

image
:windo diffthis
image
Both lines should be highlighted as changed lines, but the 2nd one in the right window is not highlighted.
In this case, ['1c1', '3c2'] is written to v:fname_out.

Expected behaviour

Both lines should be highlighted as changed lines.

Version of Vim

9.1.0271 but same thing happens in 8.2 as well.

Environment

Windows 10

Logs and stack traces

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions