Skip to content

Commit 0fff441

Browse files
committed
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 #5807)
1 parent 8601545 commit 0fff441

3 files changed

Lines changed: 50 additions & 4 deletions

File tree

src/fileio.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,21 @@ readfile(
261261
{
262262
if (apply_autocmds_exarg(EVENT_BUFREADCMD, NULL, sfname,
263263
FALSE, curbuf, eap))
264+
{
265+
int status = OK;
264266
#ifdef FEAT_EVAL
265-
return aborting() ? FAIL : OK;
266-
#else
267-
return OK;
268-
#endif
267+
if (aborting())
268+
status = FAIL;
269+
#endif
270+
// The BufReadCmd code usually uses ":read" to get the text and
271+
// perhaps ":file" to change the buffer name. But we should
272+
// consider this to work like ":edit", thus reset the
273+
// BF_NOTEDITED flag. Then ":write" will work to overwrite the
274+
// same file.
275+
if (status == OK)
276+
curbuf->b_flags &= ~BF_NOTEDITED;
277+
return status;
278+
}
269279
}
270280
else if (apply_autocmds_exarg(EVENT_FILEREADCMD, sfname, sfname,
271281
FALSE, NULL, eap))

src/testdir/test_autocmd.vim

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,6 +1536,40 @@ func Test_Cmd_Autocmds()
15361536
enew!
15371537
endfunc
15381538

1539+
func s:ReadFile()
1540+
setl noswapfile nomodified
1541+
let filename = resolve(expand("<afile>:p"))
1542+
execute 'read' fnameescape(filename)
1543+
1d_
1544+
exe 'file' fnameescape(filename)
1545+
setl buftype=acwrite
1546+
endfunc
1547+
1548+
func s:WriteFile()
1549+
let filename = resolve(expand("<afile>:p"))
1550+
setl buftype=
1551+
noautocmd execute 'write' fnameescape(filename)
1552+
setl buftype=acwrite
1553+
setl nomodified
1554+
endfunc
1555+
1556+
func Test_BufReadCmd()
1557+
autocmd BufReadCmd *.test call s:ReadFile()
1558+
autocmd BufWriteCmd *.test call s:WriteFile()
1559+
1560+
call writefile(['one', 'two', 'three'], 'Xcmd.test')
1561+
edit Xcmd.test
1562+
call assert_match('Xcmd.test" line 1 of 3', execute('file'))
1563+
normal! Gofour
1564+
write
1565+
call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test'))
1566+
1567+
bwipe!
1568+
call delete('Xcmd.test')
1569+
au! BufReadCmd
1570+
au! BufWriteCmd
1571+
endfunc
1572+
15391573
func SetChangeMarks(start, end)
15401574
exe a:start. 'mark ['
15411575
exe a:end. 'mark ]'

src/version.c

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

739739
static int included_patches[] =
740740
{ /* Add new patch number below this line */
741+
/**/
742+
474,
741743
/**/
742744
473,
743745
/**/

0 commit comments

Comments
 (0)