Skip to content

Commit cad3b24

Browse files
ychinchrisbra
authored andcommitted
patch 9.1.1600: using diff anchors with hidden buffers fails silently
Problem: diff: using diff anchors with hidden buffers fails silently Solution: Give specific error message for diff anchors when using hidden buffers (Yee Cheng Chin). Diff anchors currently will fail to parse if a buffer used for diff'ing is hidden. Previously it would just fail as the code assumes it would not happen normally, but this is actually possible to do if `closeoff` and `hideoff` are not set in diffopt. Git's default diff tool "vimdiff3" also takes advantage of this. This fix this properly would require the `{address}` parser to be smarter about whether a particular address relies on window position or not (e.g. the `'.` address requires an active window, but `'a` or `1234` do not). Since hidden diff buffers seem relatively niche, just provide a better error message / documentation for now. This could be improved later if there's a demand for it. related: #17615 closes: #17904 Signed-off-by: Yee Cheng Chin <ychin.git@gmail.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
1 parent 589aa28 commit cad3b24

7 files changed

Lines changed: 27 additions & 4 deletions

File tree

runtime/doc/options.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*options.txt* For Vim version 9.1. Last change: 2025 Aug 06
1+
*options.txt* For Vim version 9.1. Last change: 2025 Aug 07
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -3016,6 +3016,8 @@ A jump table for the options with a short description can be found at |Q_op|.
30163016
If some of the {address} do not resolve to a line in each buffer (e.g.
30173017
a pattern search that does not match anything), none of the anchors
30183018
will be used.
3019+
*E1562*
3020+
Diff anchors can only be used when there are no hidden diff buffers.
30193021

30203022
*'dex'* *'diffexpr'*
30213023
'diffexpr' 'dex' string (default "")

runtime/doc/tags

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4694,6 +4694,7 @@ E1559 vim9.txt /*E1559*
46944694
E156 sign.txt /*E156*
46954695
E1560 vim9.txt /*E1560*
46964696
E1561 vim9.txt /*E1561*
4697+
E1562 options.txt /*E1562*
46974698
E157 sign.txt /*E157*
46984699
E158 sign.txt /*E158*
46994700
E159 sign.txt /*E159*

src/diff.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2768,8 +2768,15 @@ parse_diffanchors(int check_only, buf_T *buf, linenr_T *anchors, int *num_anchor
27682768
FOR_ALL_WINDOWS(bufwin)
27692769
if (bufwin->w_buffer == buf && bufwin->w_p_diff)
27702770
break;
2771-
if (bufwin == NULL)
2772-
return FAIL; // should not really happen
2771+
if (bufwin == NULL && *dia != NUL)
2772+
{
2773+
// The buffer is hidden. Currently this is not supported due to the
2774+
// edge cases of needing to decide if an address is window-specific
2775+
// or not. We could add more checks in the future so we can detect
2776+
// whether an address relies on curwin to make this more fleixble.
2777+
emsg(_(e_diff_anchors_with_hidden_windows));
2778+
return FAIL;
2779+
}
27732780
}
27742781

27752782
for (i = 0; i < MAX_DIFF_ANCHORS && *dia != NUL; i++)

src/errors.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3778,3 +3778,7 @@ EXTERN char e_not_a_generic_function_str[]
37783778
EXTERN char e_duplicate_type_var_name_str[]
37793779
INIT(= N_("E1561: Duplicate type variable name: %s"));
37803780
#endif
3781+
#if defined(FEAT_DIFF)
3782+
EXTERN char e_diff_anchors_with_hidden_windows[]
3783+
INIT(= N_("E1562: Diff anchors cannot be used with hidden diff windows"));
3784+
#endif

src/po/vim.pot

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/testdir/test_diffmode.vim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3483,6 +3483,10 @@ func Test_diffanchors_invalid()
34833483
call assert_fails('diffupdate', 'E1550:')
34843484
call assert_equal('orig_search_pat', @/)
34853485

3486+
" Hidden buffers are not supported right now
3487+
hide
3488+
call assert_fails('diffupdate', 'E1562:')
3489+
34863490
%bw!
34873491
set diffopt&
34883492
set diffanchors&

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,8 @@ static char *(features[]) =
719719

720720
static int included_patches[] =
721721
{ /* Add new patch number below this line */
722+
/**/
723+
1600,
722724
/**/
723725
1599,
724726
/**/

0 commit comments

Comments
 (0)