GPGDecrypt: Handle (un)encrypted files consistently

In the refactor to use the *Cmd events, an unencrypted file was simply
:read into the buffer.  This incorrectly caused an extra blank line to
be added to the top of the buffer.

The handling of encrypted files properly handles this, as well as
triggering BufRead(Pre,Post) events.

Instead of returning early for unencrypted files, go through the same
steps as for encrypted files (minus the encryption specific steps), thus
preserving the content of the original file.
This commit is contained in:
James McCoy 2016-12-17 22:05:17 -05:00
parent be052843ef
commit d279115bcd
No known key found for this signature in database
GPG Key ID: DFE691AE331BA3DB

View File

@ -562,19 +562,18 @@ function s:GPGDecrypt(bufread)
echohl GPGWarning
echom "File is not encrypted, all GPG functions disabled!"
echohl None
exe printf('%sr %s', silent, fnameescape(filename))
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGDecrypt()")
return
endif
let bufname = b:GPGEncrypted ? autocmd_filename : fnameescape(filename)
if a:bufread
silent execute ':doautocmd BufReadPre ' . autocmd_filename
call s:GPGDebug(2, 'called BufReadPre autocommand for ' . autocmd_filename)
silent execute ':doautocmd BufReadPre ' . bufname
call s:GPGDebug(2, 'called BufReadPre autocommand for ' . bufname)
else
silent execute ':doautocmd FileReadPre ' . autocmd_filename
call s:GPGDebug(2, 'called FileReadPre autocommand for ' . autocmd_filename)
silent execute ':doautocmd FileReadPre ' . bufname
call s:GPGDebug(2, 'called FileReadPre autocommand for ' . bufname)
endif
if b:GPGEncrypted
" check if the message is armored
if (match(output, "gpg: armor header") >= 0)
call s:GPGDebug(1, "this file is armored")
@ -601,6 +600,9 @@ function s:GPGDecrypt(bufread)
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGDecrypt()")
return
endif
else
execute silent 'read' fnameescape(filename)
endif
if a:bufread
" In order to make :undo a no-op immediately after the buffer is read,
@ -621,19 +623,21 @@ function s:GPGDecrypt(bufread)
" - permissions don't allow writing
let &readonly = &readonly || (filereadable(filename) && filewritable(filename) == 0)
" call the autocommand for the file minus .gpg$
silent execute ':doautocmd BufReadPost ' . autocmd_filename
call s:GPGDebug(2, 'called BufReadPost autocommand for ' . autocmd_filename)
silent execute ':doautocmd BufReadPost ' . bufname
call s:GPGDebug(2, 'called BufReadPost autocommand for ' . bufname)
else
" call the autocommand for the file minus .gpg$
silent execute ':doautocmd FileReadPost ' . autocmd_filename
call s:GPGDebug(2, 'called FileReadPost autocommand for ' . autocmd_filename)
silent execute ':doautocmd FileReadPost ' . bufname
call s:GPGDebug(2, 'called FileReadPost autocommand for ' . bufname)
endif
if b:GPGEncrypted
" Allow the user to define actions for GnuPG buffers
silent doautocmd User GnuPG
" refresh screen
redraw!
endif
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGDecrypt()")
endfunction