Revert to non-binary writefile() for writing buffer.

The previous commit switched use of writefile() to binary mode so that
we could prevent a newline being added to a completely empty buffer.
Evidently, however, binary mode has side effects (see #567) so this
commit returns to non-binary mode - with a simpler fix for completely
empty files.

Unfortunately this implementation does not work for noeol files - see
the failing test - because writefile() does not take account of
'nofixeol' (unlike :write).  This is suboptimal but acceptable because
noeol files are not be encountered often.

See #567.
This commit is contained in:
Andy Stewart
2018-12-15 14:55:14 +00:00
parent 5c636b128e
commit 1d422b9f98
2 changed files with 5 additions and 9 deletions

View File

@@ -375,14 +375,10 @@ function! s:write_buffer(bufnr, file)
let bufcontents = getbufline(a:bufnr, 1, '$') let bufcontents = getbufline(a:bufnr, 1, '$')
if bufcontents == [''] && line2byte(1) == -1 if bufcontents == [''] && line2byte(1) == -1
" Special case: empty buffer; do not write an empty line in this case. " Special case: completely empty buffer.
" A nearly empty buffer of only a newline has line2byte(1) == 1. " A nearly empty buffer of only a newline has line2byte(1) == 1.
else call writefile([], a:file)
if getbufvar(a:bufnr, '&endofline') return
\ || (!getbufvar(a:bufnr, '&binary')
\ && (!exists('+fixendofline') || getbufvar(a:bufnr, '&fixendofline')))
call add(bufcontents, '')
endif
endif endif
if getbufvar(a:bufnr, '&fileformat') ==# 'dos' if getbufvar(a:bufnr, '&fileformat') ==# 'dos'
@@ -398,7 +394,7 @@ function! s:write_buffer(bufnr, file)
let bufcontents[0]=''.bufcontents[0] let bufcontents[0]=''.bufcontents[0]
endif endif
call writefile(bufcontents, a:file, 'b') call writefile(bufcontents, a:file)
endfunction endfunction

View File

@@ -669,7 +669,7 @@ function Test_empty_file()
" 1 line file without newline " 1 line file without newline
" Vim will force a newline unless we tell it not to. " Vim will force a newline unless we tell it not to.
call system('echo -n "a" > oneline.txt && git add oneline.txt') call system('echo -n a > oneline.txt && git add oneline.txt')
set noeol nofixeol set noeol nofixeol
edit! oneline.txt edit! oneline.txt