From ae1b31965ee222580cfe2f46b638318b771a530f Mon Sep 17 00:00:00 2001 From: James McCoy Date: Sun, 22 Oct 2017 23:16:28 -0400 Subject: [PATCH 1/3] Ensure buftype=acwrite is always set for the buffer The user may be a) editing an existing encrypted file, b) creating a new encrypted file, c) editing an unnamed buffer and saving it as an encrypted file. In all cases, 'buftype' should be set correctly once we know the buffer is intended to be encrypted. Signed-off-by: James McCoy --- plugin/gnupg.vim | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 4be9130..eb36375 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -1,5 +1,5 @@ " Name: gnupg.vim -" Last Change: 2017 May 31 +" Last Change: 2017 Oct 22 " Maintainer: James McCoy " Original Author: Markus Braun " Summary: Vim plugin for transparent editing of gpg encrypted files. @@ -478,6 +478,8 @@ function s:GPGDecrypt(bufread) silent execute ':doautocmd BufNewFile ' . fnameescape(autocmd_filename) call s:GPGDebug(2, 'called BufNewFile autocommand for ' . autocmd_filename) + set buftype=acwrite + " This is a new file, so force the user to edit the recipient list if " they open a new file and public keys are preferred if (g:GPGPreferSymmetric == 0) @@ -752,6 +754,7 @@ function s:GPGEncrypt() if auType == 'BufWrite' setl nomodified + setl buftype=acwrite let &readonly = filereadable(filename) && filewritable(filename) == 0 endif From d3453145c5f0a087b445902461c964ffb32aac08 Mon Sep 17 00:00:00 2001 From: James McCoy Date: Sun, 22 Oct 2017 23:18:00 -0400 Subject: [PATCH 2/3] Set the buffer name to the resolved, absolute path If buftype=acwrite, Vim doesn't adjust relative buffer names when changing directory. Since 5103285d, encrypted buffers have set buftype=acwrite and therefore also risked having incorrect buffer names (e.g., if 'autochdir' is set). Now that the buffer name is always fully resolved, this should no longer be a problem. Closes jamessan/vim-gnupg#81 Signed-off-by: James McCoy --- plugin/gnupg.vim | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index eb36375..568b637 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -455,7 +455,7 @@ function s:GPGDecrypt(bufread) call s:GPGDebug(3, printf(">>>>>>>> Entering s:GPGDecrypt(%d)", a:bufread)) " get the filename of the current buffer - let filename = expand(":p") + let filename = resolve(expand(":p")) " clear GPGRecipients and GPGOptions if type(g:GPGDefaultRecipients) == type([]) @@ -469,7 +469,7 @@ function s:GPGDecrypt(bufread) let b:GPGOptions = [] " file name minus extension - let autocmd_filename = expand(':r') + let autocmd_filename = fnamemodify(filename, ':r') " File doesn't exist yet, so nothing to decrypt if !filereadable(filename) @@ -479,6 +479,10 @@ function s:GPGDecrypt(bufread) call s:GPGDebug(2, 'called BufNewFile autocommand for ' . autocmd_filename) set buftype=acwrite + " Remove the buffer name ... + silent 0file + " ... so we can force it to be absolute + exe 'silent file' filename " This is a new file, so force the user to edit the recipient list if " they open a new file and public keys are preferred @@ -605,6 +609,10 @@ function s:GPGDecrypt(bufread) endif " Ensure the buffer is only saved by using our BufWriteCmd set buftype=acwrite + " Always set the buffer name to the absolute path, otherwise Vim won't + " track the correct buffer name when changing directories (due to + " buftype=acwrite). + exe 'file' filename else execute silent 'read' fnameescape(filename) endif @@ -663,7 +671,7 @@ function s:GPGEncrypt() endif " file name minus extension - let autocmd_filename = expand(':r') + let autocmd_filename = expand(':p:r') silent exe ':doautocmd '. auType .'Pre '. fnameescape(autocmd_filename) call s:GPGDebug(2, 'called '. auType .'Pre autocommand for ' . autocmd_filename) @@ -677,7 +685,7 @@ function s:GPGEncrypt() return endif - let filename = resolve(expand('')) + let filename = resolve(expand(':p')) " initialize GPGOptions if not happened before if (!exists("b:GPGOptions") || empty(b:GPGOptions)) let b:GPGOptions = [] @@ -941,7 +949,7 @@ function s:GPGFinishRecipientsBuffer() " go to buffer before doing work if (bufnr("%") != expand("")) " switch to scratch buffer window - execute 'silent! ' . bufwinnr(expand("")) . "wincmd w" + execute 'silent! ' . bufwinnr(expand(":p")) . "wincmd w" endif " delete the autocommand @@ -1124,7 +1132,7 @@ function s:GPGFinishOptionsBuffer() " go to buffer before doing work if (bufnr("%") != expand("")) " switch to scratch buffer window - execute 'silent! ' . bufwinnr(expand("")) . "wincmd w" + execute 'silent! ' . bufwinnr(expand(":p")) . "wincmd w" endif " clear options and unknownOptions From c411e61aeb0efc232d89f81481520c1f5fe8355e Mon Sep 17 00:00:00 2001 From: James McCoy Date: Sun, 22 Oct 2017 23:21:58 -0400 Subject: [PATCH 3/3] On save, only unset 'modified' for the current filename If the buffer is being saved to a different filename, then this buffer should still be modified as its backing file is still different than the buffer. Signed-off-by: James McCoy --- plugin/gnupg.vim | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 568b637..ae3005b 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -761,7 +761,9 @@ function s:GPGEncrypt() endif if auType == 'BufWrite' - setl nomodified + if expand('%:p') == filename + setl nomodified + endif setl buftype=acwrite let &readonly = filereadable(filename) && filewritable(filename) == 0 endif