# HG changeset patch # User Bram Moolenaar # Date 1585491303 -7200 # Node ID c32b295af9c505b46d3fb68b73a1301c8779fcbc # Parent b56266f40a71d84c78c95617c58a4cde2a08001e patch 8.2.0474: cannot use :write when using a plugin with BufWriteCmd Commit: https://github.com/vim/vim/commit/0fff44152d06e6b662ad4bef172af07a041d2f3f Author: Bram Moolenaar Date: Sun Mar 29 16:06:29 2020 +0200 patch 8.2.0474: cannot use :write when using a plugin with BufWriteCmd Problem: Cannot use :write when using a plugin with BufWriteCmd. Solution: Reset BF_NOTEDITED after BufWriteCmd. (closes https://github.com/vim/vim/issues/5807) diff --git a/src/fileio.c b/src/fileio.c --- a/src/fileio.c +++ b/src/fileio.c @@ -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; + 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)) diff --git a/src/testdir/test_autocmd.vim b/src/testdir/test_autocmd.vim --- a/src/testdir/test_autocmd.vim +++ b/src/testdir/test_autocmd.vim @@ -1536,6 +1536,40 @@ func Test_Cmd_Autocmds() enew! endfunc +func s:ReadFile() + setl noswapfile nomodified + let filename = resolve(expand(":p")) + execute 'read' fnameescape(filename) + 1d_ + exe 'file' fnameescape(filename) + setl buftype=acwrite +endfunc + +func s:WriteFile() + let filename = resolve(expand(":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 ]' diff --git a/src/version.c b/src/version.c --- a/src/version.c +++ b/src/version.c @@ -739,6 +739,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 474, +/**/ 473, /**/ 472,