Recreate temp directory if it gets deleted

Vim does not check the existence of its temp directory when generating
file names with tempname().  Therefore if, for some reason, the temp
directory gets deleted, Vim will continue to generate paths for file
names inside the directory.  Writes to these file paths will fail
(E482).

See #746, #433, #190, #147.
This commit is contained in:
Andy Stewart
2020-11-25 12:42:00 +00:00
parent 81963946ed
commit dfe55e2b92

View File

@@ -399,7 +399,16 @@ function! s:write_buffer(bufnr, file)
let bufcontents[0]=''.bufcontents[0] let bufcontents[0]=''.bufcontents[0]
endif endif
" The file we are writing to is a temporary file. Sometimes the parent
" directory is deleted outside Vim but, because Vim caches the directory
" name at startup and does not check for its existence subsequently, Vim
" does not realise. This causes E482 errors.
try
call writefile(bufcontents, a:file, 'b') call writefile(bufcontents, a:file, 'b')
catch /E482/
call mkdir(fnamemodify(a:file, ':h'), '', '0700')
call writefile(bufcontents, a:file, 'b')
endtry
endfunction endfunction