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,44 +562,46 @@ 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
" check if the message is armored
if (match(output, "gpg: armor header") >= 0)
call s:GPGDebug(1, "this file is armored")
let b:GPGOptions += ["armor"]
endif
" finally decrypt the buffer content
" since even with the --quiet option passphrase typos will be reported,
" we must redirect stderr (using shell temporarily)
call s:GPGDebug(1, "decrypting file")
let cmd = { 'level': 1, 'ex': silent . 'read ++edit !' }
let cmd.args = '--quiet --decrypt ' . s:shellescape(filename, 1)
call s:GPGExecute(cmd)
if (v:shell_error) " message could not be decrypted
echohl GPGError
let blackhole = input("Message could not be decrypted! (Press ENTER)")
echohl None
" Only wipeout the buffer if we were creating one to start with.
" FileReadCmd just reads the content into the existing buffer
if a:bufread
silent bwipeout!
if b:GPGEncrypted
" check if the message is armored
if (match(output, "gpg: armor header") >= 0)
call s:GPGDebug(1, "this file is armored")
let b:GPGOptions += ["armor"]
endif
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGDecrypt()")
return
" finally decrypt the buffer content
" since even with the --quiet option passphrase typos will be reported,
" we must redirect stderr (using shell temporarily)
call s:GPGDebug(1, "decrypting file")
let cmd = { 'level': 1, 'ex': silent . 'read ++edit !' }
let cmd.args = '--quiet --decrypt ' . s:shellescape(filename, 1)
call s:GPGExecute(cmd)
if (v:shell_error) " message could not be decrypted
echohl GPGError
let blackhole = input("Message could not be decrypted! (Press ENTER)")
echohl None
" Only wipeout the buffer if we were creating one to start with.
" FileReadCmd just reads the content into the existing buffer
if a:bufread
silent bwipeout!
endif
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGDecrypt()")
return
endif
else
execute silent 'read' fnameescape(filename)
endif
if a:bufread
@ -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
" Allow the user to define actions for GnuPG buffers
silent doautocmd User GnuPG
if b:GPGEncrypted
" Allow the user to define actions for GnuPG buffers
silent doautocmd User GnuPG
" refresh screen
redraw!
" refresh screen
redraw!
endif
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGDecrypt()")
endfunction