diff --git a/runtime/doc/options.txt b/runtime/doc/options.txt index 81726ca46b..cdec599a74 100644 --- a/runtime/doc/options.txt +++ b/runtime/doc/options.txt @@ -1960,6 +1960,9 @@ A jump table for the options with a short description can be found at |Q_op|. vertical Start diff mode with vertical splits (unless explicitly specified otherwise). + hiddenoff Do not use diff mode for a buffer when it + becomes hidden. + foldcolumn:{n} Set the 'foldcolumn' option to {n} when starting diff mode. Without this 2 is used. diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c index 71e04ec0fb..d4e21d26f6 100644 --- a/src/nvim/buffer.c +++ b/src/nvim/buffer.c @@ -509,6 +509,10 @@ void close_buffer(win_T *win, buf_T *buf, int action, int abort_if_last) if (buf->b_nwindows > 0) --buf->b_nwindows; + if (diffopt_hiddenoff() && !unload_buf && buf->b_nwindows == 0) { + diff_buf_delete(buf); // Clear 'diff' for hidden buffer. + } + /* Return when a window is displaying the buffer or when it's not * unloaded. */ if (buf->b_nwindows > 0 || !unload_buf) diff --git a/src/nvim/diff.c b/src/nvim/diff.c index e7a176d336..878971a35c 100644 --- a/src/nvim/diff.c +++ b/src/nvim/diff.c @@ -44,6 +44,7 @@ static int diff_busy = FALSE; // ex_diffgetput() is busy #define DIFF_IWHITE 4 // ignore change in white space #define DIFF_HORIZONTAL 8 // horizontal splits #define DIFF_VERTICAL 16 // vertical splits +#define DIFF_HIDDEN_OFF 32 // diffoff when hidden static int diff_flags = DIFF_FILLER; #define LBUFLEN 50 // length of line in diff file @@ -1838,6 +1839,9 @@ int diffopt_changed(void) } else if ((STRNCMP(p, "foldcolumn:", 11) == 0) && ascii_isdigit(p[11])) { p += 11; diff_foldcolumn_new = getdigits_int(&p); + } else if (STRNCMP(p, "hiddenoff", 9) == 0) { + p += 9; + diff_flags_new |= DIFF_HIDDEN_OFF; } if ((*p != ',') && (*p != NUL)) { @@ -1880,6 +1884,12 @@ bool diffopt_horizontal(void) return (diff_flags & DIFF_HORIZONTAL) != 0; } +// Return true if 'diffopt' contains "hiddenoff". +bool diffopt_hiddenoff(void) +{ + return (diff_flags & DIFF_HIDDEN_OFF) != 0; +} + /// Find the difference within a changed line. /// /// @param wp window whose current buffer to check diff --git a/src/nvim/testdir/test_diffmode.vim b/src/nvim/testdir/test_diffmode.vim index f53fe438fc..90fd34d93e 100644 --- a/src/nvim/testdir/test_diffmode.vim +++ b/src/nvim/testdir/test_diffmode.vim @@ -375,6 +375,29 @@ func Test_diffopt_vertical() %bwipe endfunc +func Test_diffopt_hiddenoff() + set diffopt=filler,foldcolumn:0,hiddenoff + e! one + call setline(1, ['Two', 'Three']) + redraw + let normattr = screenattr(1, 1) + diffthis + botright vert new two + call setline(1, ['One', 'Four']) + diffthis + redraw + call assert_notequal(normattr, screenattr(1, 1)) + set hidden + close + redraw + " should not diffing with hidden buffer two while 'hiddenoff' is enabled + call assert_equal(normattr, screenattr(1, 1)) + + bwipe! + bwipe! + set hidden& diffopt& +endfunc + func Test_diffoff_hidden() set diffopt=filler,foldcolumn:0 e! one