]> granicus.if.org Git - vim/commitdiff
patch 8.2.0474: cannot use :write when using a plugin with BufWriteCmd v8.2.0474
authorBram Moolenaar <Bram@vim.org>
Sun, 29 Mar 2020 14:06:29 +0000 (16:06 +0200)
committerBram Moolenaar <Bram@vim.org>
Sun, 29 Mar 2020 14:06:29 +0000 (16:06 +0200)
Problem:    Cannot use :write when using a plugin with BufWriteCmd.
Solution:   Reset BF_NOTEDITED after BufWriteCmd. (closes #5807)

src/fileio.c
src/testdir/test_autocmd.vim
src/version.c

index 6e66a3e862cdf0d9fa322774f4c80a81e391a3df..f05abe68c99accfa08b4bdba8538fbc27cf8f52c 100644 (file)
@@ -261,11 +261,21 @@ readfile(
        {
            if (apply_autocmds_exarg(EVENT_BUFREADCMD, NULL, sfname,
                                                          FALSE, curbuf, eap))
+           {
+               int status = OK;
 #ifdef FEAT_EVAL
-               return aborting() ? FAIL : OK;
-#else
-               return OK;
-#endif
+               if (aborting())
+                   status = FAIL;
+#endif
+               // The BufReadCmd code usually uses ":read" to get the text and
+               // perhaps ":file" to change the buffer name. But we should
+               // consider this to work like ":edit", thus reset the
+               // BF_NOTEDITED flag.  Then ":write" will work to overwrite the
+               // same file.
+               if (status == OK)
+                   curbuf->b_flags &= ~BF_NOTEDITED;
+               return status;
+           }
        }
        else if (apply_autocmds_exarg(EVENT_FILEREADCMD, sfname, sfname,
                                                            FALSE, NULL, eap))
index 44f9465f0cf3a9b2dd22e767765c090adf290994..ad2dc2b0ebb00f8a7874dc3a98750f8d4b29f2b5 100644 (file)
@@ -1536,6 +1536,40 @@ func Test_Cmd_Autocmds()
   enew!
 endfunc
 
+func s:ReadFile()
+  setl noswapfile nomodified
+  let filename = resolve(expand("<afile>:p"))
+  execute 'read' fnameescape(filename)
+  1d_
+  exe 'file' fnameescape(filename)
+  setl buftype=acwrite
+endfunc
+
+func s:WriteFile()
+  let filename = resolve(expand("<afile>:p"))
+  setl buftype=
+  noautocmd execute 'write' fnameescape(filename)
+  setl buftype=acwrite
+  setl nomodified
+endfunc
+
+func Test_BufReadCmd()
+  autocmd BufReadCmd *.test call s:ReadFile()
+  autocmd BufWriteCmd *.test call s:WriteFile()
+
+  call writefile(['one', 'two', 'three'], 'Xcmd.test')
+  edit Xcmd.test
+  call assert_match('Xcmd.test" line 1 of 3', execute('file'))
+  normal! Gofour
+  write
+  call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test'))
+
+  bwipe!
+  call delete('Xcmd.test')
+  au! BufReadCmd
+  au! BufWriteCmd
+endfunc
+
 func SetChangeMarks(start, end)
   exe a:start. 'mark ['
   exe a:end. 'mark ]'
index 3282033587068ec9c53bf212e9941d5e862adc45..d06b4b7771a27db97db7422d5fb4b1b592a03a73 100644 (file)
@@ -738,6 +738,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    474,
 /**/
     473,
 /**/