From b8bd2e6ebab03baf2672067067a599df69a278c0 Mon Sep 17 00:00:00 2001
From: Bram Moolenaar <Bram@vim.org>
Date: Sat, 21 Aug 2021 17:13:14 +0200
Subject: [PATCH] patch 8.2.3363: when :edit reuses the current buffer the
 alternate file is set

Problem:    When :edit reuses the current buffer the alternate file is set to
            the same buffer.
Solution:   Only set the alternate file when not reusing the buffer.
            (closes #8783)
---
 src/ex_cmds.c                     | 6 ++++++
 src/testdir/test_cmdline.vim      | 2 +-
 src/testdir/test_undo.vim         | 2 +-
 src/testdir/test_vim9_builtin.vim | 2 +-
 src/testdir/test_vim9_script.vim  | 2 +-
 src/version.c                     | 2 ++
 6 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/src/ex_cmds.c b/src/ex_cmds.c
index d30db91952..021296d38c 100644
--- a/src/ex_cmds.c
+++ b/src/ex_cmds.c
@@ -2648,6 +2648,8 @@ do_ecmd(
      */
     if (other_file)
     {
+	int prev_alt_fnum = curwin->w_alt_fnum;
+
 	if (!(flags & (ECMD_ADDBUF | ECMD_ALTBUF)))
 	{
 	    if ((cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
@@ -2691,6 +2693,10 @@ do_ecmd(
 	}
 	if (buf == NULL)
 	    goto theend;
+	if (curwin->w_alt_fnum == buf->b_fnum && prev_alt_fnum != 0)
+	    // reusing the buffer, keep the old alternate file
+	    curwin->w_alt_fnum = prev_alt_fnum;
+
 	if (buf->b_ml.ml_mfp == NULL)		// no memfile yet
 	{
 	    oldbuf = FALSE;
diff --git a/src/testdir/test_cmdline.vim b/src/testdir/test_cmdline.vim
index d04bb4c8cb..bd2ca03b60 100644
--- a/src/testdir/test_cmdline.vim
+++ b/src/testdir/test_cmdline.vim
@@ -1365,7 +1365,7 @@ endfunc
 " Test for expanding special keywords in cmdline
 func Test_cmdline_expand_special()
   %bwipe!
-  call assert_fails('e #', 'E499:')
+  call assert_fails('e #', 'E194:')
   call assert_fails('e <afile>', 'E495:')
   call assert_fails('e <abuf>', 'E496:')
   call assert_fails('e <amatch>', 'E497:')
diff --git a/src/testdir/test_undo.vim b/src/testdir/test_undo.vim
index 2621412fa0..bfcc538d93 100644
--- a/src/testdir/test_undo.vim
+++ b/src/testdir/test_undo.vim
@@ -582,7 +582,7 @@ func Test_undofile_2()
 
   " add 10 lines, delete 6 lines, undo 3
   set undofile
-  call setbufline(0, 1, ['one', 'two', 'three', 'four', 'five', 'six',
+  call setbufline('%', 1, ['one', 'two', 'three', 'four', 'five', 'six',
 	      \ 'seven', 'eight', 'nine', 'ten'])
   set undolevels=100
   normal 3Gdd
diff --git a/src/testdir/test_vim9_builtin.vim b/src/testdir/test_vim9_builtin.vim
index 00d021fe19..434f477488 100644
--- a/src/testdir/test_vim9_builtin.vim
+++ b/src/testdir/test_vim9_builtin.vim
@@ -1268,7 +1268,7 @@ enddef
 def Test_getbufline()
   e SomeFile
   var buf = bufnr()
-  e #
+  sp Otherfile
   var lines = ['aaa', 'bbb', 'ccc']
   setbufline(buf, 1, lines)
   getbufline('#', 1, '$')->assert_equal(lines)
diff --git a/src/testdir/test_vim9_script.vim b/src/testdir/test_vim9_script.vim
index ffea2acb27..fd93c3a8b8 100644
--- a/src/testdir/test_vim9_script.vim
+++ b/src/testdir/test_vim9_script.vim
@@ -3495,7 +3495,7 @@ def Test_vim9_comment_gui()
   CheckScriptFailure([
       'vim9script',
       'gui -f#comment'
-      ], 'E499:')
+      ], 'E194:')
 enddef
 
 def Test_vim9_comment_not_compiled()
diff --git a/src/version.c b/src/version.c
index dfe949d9d0..25e0d82ffe 100644
--- a/src/version.c
+++ b/src/version.c
@@ -755,6 +755,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    3363,
 /**/
     3362,
 /**/
-- 
GitLab