From 167ef9a4f3e5cee4ee4e211437f1c94fa32416e3 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Wed, 24 Apr 2002 08:59:12 +0000 Subject: [PATCH 001/115] GnuPG plugin for Vim This script implements transparent editing of gpg encrypted files. The filename must have a ".gpg" suffix. When opening such a file the content is decrypted, when opening a new file the script will ask for the recipients of the encrypted file. The file content will be encrypted to all recipients before it is written. The script turns off viminfo and swapfile to increase security. --- plugin/gnupg.vim | 713 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 713 insertions(+) create mode 100644 plugin/gnupg.vim diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim new file mode 100644 index 0000000..fa47917 --- /dev/null +++ b/plugin/gnupg.vim @@ -0,0 +1,713 @@ +" Name: gnupg.vim +" Version: $Id$ +" Author: Markus Braun +" Summary: Vim plugin for transparent editing of gpg encrypted files. +" Licence: This program is free software; you can redistribute it and/or +" modify it under the terms of the GNU General Public License. +" See http://www.gnu.org/copyleft/gpl.txt +" Section: Documentation {{{1 +" Description: +" +" This script implements transparent editing of gpg encrypted files. The +" filename must have a ".gpg" suffix. When opening such a file the content +" is decrypted, when opening a new file the script will ask for the +" recipients of the encrypted file. The file content will be encrypted to +" all recipients before it is written. The script turns off viminfo and +" swapfile to increase security. +" +" Installation: +" +" Copy the gnupg.vim file to the $HOME/.vim/plugin directory. +" Refer to ':help add-plugin', ':help add-global-plugin' and ':help +" runtimepath' for more details about Vim plugins. +" +" Commands: +" +" :GPGEditRecipients +" Opens a scratch buffer to change the list of recipients. Recipients that +" are unknown (not in your public key) are highlighted and have a +" prepended "!". Closing the buffer with :x or :bd makes the changes +" permanent. +" +" :GPGViewRecipients +" Prints the list of recipients. +" +" :GPGEditOptions +" Opens a scratch buffer to change the options for encryption (symmetric, +" asymmetric, signing). Closing the buffer with :x or :bd makes the +" changes permanent. +" WARNING: There is no check of the entered options, so you need to know +" what you are doing. +" +" :GPGViewRecipients +" Prints the list of options. +" +" Credits: +" Mathieu Clabaut for inspirations through his vimspell.vim script. +" Section: Plugin header {{{1 +if (exists("loaded_gnupg") || &cp || exists("#BufReadPre#*.gpg")) + finish +endi +let loaded_gnupg = 1 + +" Section: Autocmd setup {{{1 +augroup GnuPG +au! + +" First make sure nothing is written to ~/.viminfo while editing +" an encrypted file. +autocmd BufNewFile,BufReadPre,FileReadPre *.gpg set viminfo= +" We don't want a swap file, as it writes unencrypted data to disk +autocmd BufNewFile,BufReadPre,FileReadPre *.gpg set noswapfile +" Initialize the internal variables +autocmd BufNewFile,BufReadPre,FileReadPre *.gpg call s:GPGInit() +" Force the user to edit the recipient list if he opens a new file +autocmd BufNewFile *.gpg call s:GPGEditRecipients() +" Switch to binary mode to read the encrypted file +autocmd BufReadPre,FileReadPre *.gpg set bin +autocmd BufReadPost,FileReadPost *.gpg call s:GPGDecrypt() +" Switch to normal mode for editing +autocmd BufReadPost,FileReadPost *.gpg set nobin +" Call the autocommand for the file minus .gpg$ +autocmd BufReadPost,FileReadPost *.gpg execute ":doautocmd BufReadPost " . expand("%:r") +autocmd BufReadPost,FileReadPost *.gpg execute ":redraw!" + +" Switch to binary mode before encrypt the file +autocmd BufWritePre,FileWritePre *.gpg set bin +" Convert all text to encrypted text before writing +autocmd BufWritePre,FileWritePre *.gpg call s:GPGEncrypt() +" Undo the encryption so we are back in the normal text, directly +" after the file has been written. +autocmd BufWritePost,FileWritePost *.gpg silent u +" Switch back to normal mode for editing +autocmd BufWritePost,FileWritePost *.gpg set nobin +augroup END +" Section: Highlight setup {{{1 +highlight default GPGWarning term=reverse ctermfg=Yellow guifg=Yellow +highlight default GPGError term=reverse ctermfg=Red guifg=Red +highlight default GPGHighlightUnknownRecipient term=reverse ctermfg=Red cterm=underline guifg=Red gui=underline +" Section: Functions {{{1 +" Function: s:GPGInit() {{{2 +" +" initialize the plugin +" +fun s:GPGInit() + " determine if gnupg can use the gpg-agent + if (exists("$GPG_AGENT_INFO")) + let s:gpgcommand="LANG=C gpg --use-agent" + else + let s:gpgcommand="LANG=C gpg --no-use-agent" + endif + + " find the supported algorithms + let shsave=&sh + let &sh='sh' + let output=system(s:gpgcommand . " --version") + let &sh=shsave + + let s:GPGPubkey=substitute(output, ".*Pubkey: \\(.\\{-}\\)\n.*", "\\1", "") + let s:GPGCipher=substitute(output, ".*Cipher: \\(.\\{-}\\)\n.*", "\\1", "") + let s:GPGHash=substitute(output, ".*Hash: \\(.\\{-}\\)\n.*", "\\1", "") + let s:GPGCompress=substitute(output, ".*Compress: \\(.\\{-}\\)\n.*", "\\1", "") +endf + +" Function: s:GPGDecrypt() {{{2 +" +" decrypt the buffer and find all recipients of the encrypted file +" +fun s:GPGDecrypt() + " get the filename of the current buffer + let filename=escape(expand("%:p"), ' *?\"'."'") + + " clear GPGRecipients, GPGUnknownRecipients and GPGOptions + let b:GPGRecipients="" + let b:GPGUnknownRecipients="" + let b:GPGOptions="" + + " find the recipients of the file + let shsave=&sh + let &sh='sh' + let output=system(s:gpgcommand . " --decrypt --dry-run --batch " . filename) + let &sh=shsave + + " check if the file is symmetric/asymmetric encrypted + if (match(output, "gpg: [^ ]\\+ encrypted data") >= 0) + " file is symmetric encrypted + let b:GPGOptions=b:GPGOptions . "symmetric:" + + let cipher=substitute(output, ".*gpg: \\([^ ]\\+\\) encrypted data.*", "\\1", "") + if (match(s:GPGCipher, "\\<" . cipher . "\\>") >= 0) + let b:GPGOptions=b:GPGOptions . "cipher-algo " . cipher . ":" + else + echohl GPGWarning + echo "The cipher " . cipher . " is not known by the local gpg command. Using default!" + echo + echohl None + endi + else + " file is asymmetric encrypted + let b:GPGOptions=b:GPGOptions . "encrypt:" + + let start=match(output, "ID [[:xdigit:]]\\{8}") + while (start >= 0) + let start=start+3 + let recipient=strpart(output, start, 8) + let name=s:GPGNameToID(recipient) + if (strlen(name) > 0) + let b:GPGRecipients=b:GPGRecipients . name . ":" + else + let b:GPGUnknownRecipients=b:GPGUnknownRecipients . recipient . ":" + echohl GPGWarning + echo "The recipient " . recipient . " is not in your public keyring!" + echohl None + end + let start=match(output, "ID [[:xdigit:]]\\{8}", start) + endw + + "echo "GPGRecipients=\"" . b:GPGRecipients . "\"" + endi + + " check if the message is armored + if (stridx(getline(1), "-----BEGIN PGP MESSAGE-----") >= 0) + let b:GPGOptions=b:GPGOptions . "armor:" + endi + + " finally decrypt the buffer content + " since even with the --quiet option passphrase typos will be reported, + " we must redirect stderr (using sh temporarily) + let shsave=&sh + let &sh='sh' + exec "'[,']!" . s:gpgcommand . " --quiet --decrypt 2>/dev/null" + let &sh=shsave + if (v:shell_error) " message could not be decrypted + silent u + echohl GPGError + let asd=input("Message could not be decrypted! (Press ENTER)") + echohl None + bwipeout + return + endi +endf + +" Function: s:GPGEncrypt() {{{2 +" +" encrypts the buffer to all previous recipients +" +fun s:GPGEncrypt() + let options="" + let recipients="" + let field=0 + + " built list of options + if (!exists("b:GPGOptions") || strlen(b:GPGOptions) == 0) + let b:GPGOptions="encrypt:" + endi + let field=0 + let option=s:GetField(b:GPGOptions, ":", field) + while (strlen(option)) + let options=options . " --" . option . " " + let field=field+1 + let option=s:GetField(b:GPGOptions, ":", field) + endw + + " check if there are unknown recipients and warn + if (exists("b:GPGUnknownRecipients") && strlen(b:GPGUnknownRecipients) > 0) + echohl GPGWarning + echo "There are unknown recipients!!" + echo "Please use GPGEditRecipients to correct!!" + echo + echohl None + endi + + " built list of recipients + if (exists("b:GPGRecipients") && strlen(b:GPGRecipients) > 0) + let field=0 + let gpgid=s:GetField(b:GPGRecipients, ":", field) + while (strlen(gpgid)) + let recipients=recipients . " -r " . gpgid + let field=field+1 + let gpgid=s:GetField(b:GPGRecipients, ":", field) + endw + else + if (match(b:GPGOptions, "symmetric:") < 0) + echohl GPGError + echo "There are no recipients!!" + echo "Please use GPGEditRecipients to correct!!" + echo + echohl None + endi + endi + + " encrypt the buffer + let shsave=&sh + let &sh='sh' + silent exec "'[,']!" . s:gpgcommand . " --quiet --no-encrypt-to " . options . recipients . " 2>/dev/null" + let &sh=shsave + if (v:shell_error) " message could not be encrypted + silent u + echohl GPGError + let asd=input("Message could not be encrypted! File might be empty! (Press ENTER)") + echohl None + bwipeout + return + endi + + "redraw! +endf + +" Function: s:GPGViewRecipients() {{{2 +" +" echo the recipients +" +fun s:GPGViewRecipients() + if (exists("b:GPGRecipients")) + echo 'This file has following recipients (Unknown recipients have a prepended "!"):' + " echo the recipients + let field=0 + let name=s:GetField(b:GPGRecipients, ":", field) + while (strlen(name) > 0) + let name=s:GPGIDToName(name) + echo name + + let field=field+1 + let name=s:GetField(b:GPGRecipients, ":", field) + endw + + " put the unknown recipients in the scratch buffer + let field=0 + echohl GPGWarning + let name=s:GetField(b:GPGUnknownRecipients, ":", field) + while (strlen(name) > 0) + let name="!" . name + echo name + + let field=field+1 + let name=s:GetField(b:GPGUnknownRecipients, ":", field) + endw + echohl None + + " check if there is any known recipient + if (strlen(s:GetField(b:GPGRecipients, ":", 0)) == 0) + echohl GPGError + echo 'There are no known recipients!' + echohl None + endi + endi +endf + +" Function: s:GPGEditRecipients() {{{2 +" +" create a scratch buffer with all recipients to add/remove recipients +" +fun s:GPGEditRecipients() + " only do this if it isn't already a GPGRecipients_* buffer + if (match(bufname("%"), "^\\(GPGRecipients_\\|GPGOptions_\\)") != 0 && match(bufname("%"), "\.gpg$") >= 0) + + " save buffer name + let buffername=bufname("%") + let editbuffername="GPGRecipients_" . buffername + + " create scratch buffer + exe 'silent! split ' . editbuffername + + " check if this buffer exists + if (bufexists(editbuffername)) + " empty the buffer + silent normal! 1GdG + endi + + " Mark the buffer as a scratch buffer + setlocal buftype=nofile + setlocal noswapfile + setlocal nowrap + setlocal nobuflisted + setlocal nonumber + + " so we know for which other buffer this edit buffer is + let b:corresponding_to=buffername + + " put some comments to the scratch buffer + silent put ='GPG: ----------------------------------------------------------------------' + silent put ='GPG: Please edit the list of recipients, one recipient per line' + silent put ='GPG: Unknown recipients have a prepended \"!\"' + silent put ='GPG: Lines beginning with \"GPG:\" are removed automatically' + silent put ='GPG: Use :x or :bd to close this buffer' + silent put ='GPG: ----------------------------------------------------------------------' + + " put the recipients in the scratch buffer + let recipients=getbufvar(b:corresponding_to, "GPGRecipients") + let field=0 + + let name=s:GetField(recipients, ":", field) + while (strlen(name) > 0) + let name=s:GPGIDToName(name) + silent put =name + + let field=field+1 + let name=s:GetField(recipients, ":", field) + endw + + " put the unknown recipients in the scratch buffer + let unknownRecipients=getbufvar(b:corresponding_to, "GPGUnknownRecipients") + let field=0 + let syntaxPattern="\\(nonexistingwordinthisbuffer" + + let name=s:GetField(unknownRecipients, ":", field) + while (strlen(name) > 0) + let name="!" . name + let syntaxPattern=syntaxPattern . "\\|" . name + silent put =name + + let field=field+1 + let name=s:GetField(unknownRecipients, ":", field) + endw + + let syntaxPattern=syntaxPattern . "\\)" + + " define highlight + if (has("syntax") && exists("g:syntax_on")) + exec('syntax match GPGUnknownRecipient "' . syntaxPattern . '"') + highlight clear GPGUnknownRecipient + highlight link GPGUnknownRecipient GPGHighlightUnknownRecipient + + syntax match GPGComment "^GPG:.*$" + highlight clear GPGComment + highlight link GPGComment Comment + endi + + " delete the empty first line + silent normal! 1Gdd + + " jump to the first recipient + silent normal! 6G + + " add a autocommand to regenerate the recipients after a write + augroup GPGEditRecipients + augroup END + execute 'au GPGEditRecipients BufHidden ' . editbuffername . ' call s:GPGFinishRecipientsBuffer()' + + endi +endf + +" Function: s:GPGFinishRecipientsBuffer() {{{2 +" +" create a new recipient list from RecipientsBuffer +fun s:GPGFinishRecipientsBuffer() + " clear GPGRecipients and GPGUnknownRecipients + let GPGRecipients="" + let GPGUnknownRecipients="" + + " delete the autocommand + exe "au! GPGEditRecipients * " . bufname("%") + + let currentline=1 + let recipient=getline(currentline) + + " get the recipients from the scratch buffer + while (currentline <= line("$")) + " delete all spaces at beginning and end of the line + " also delete a '!' at the beginning of the line + let recipient=substitute(recipient, "^[[:space:]!]*\\(.\\{-}\\)[[:space:]]*$", "\\1", "") + " delete comment lines + let recipient=substitute(recipient, "^GPG:.*$", "", "") + + " only do this if the line is not empty + if (strlen(recipient) > 0) + let gpgid=s:GPGNameToID(recipient) + if (strlen(gpgid) > 0) + let GPGRecipients=GPGRecipients . gpgid . ":" + else + let GPGUnknownRecipients=GPGUnknownRecipients . recipient . ":" + echohl GPGWarning + echo "The recipient " . recipient . " is not in your public keyring!" + echohl None + end + endi + + let currentline=currentline+1 + let recipient=getline(currentline) + endw + + " write back the new recipient list to the corresponding buffer and mark it + " as modified + call setbufvar(b:corresponding_to, "GPGRecipients", GPGRecipients) + call setbufvar(b:corresponding_to, "GPGUnknownRecipients", GPGUnknownRecipients) + call setbufvar(b:corresponding_to, "&mod", 1) + "echo "GPGRecipients=\"" . getbufvar(b:corresponding_to, "GPGRecipients") . "\"" + + " check if there is any known recipient + if (strlen(s:GetField(GPGRecipients, ":", 0)) == 0) + echohl GPGError + echo 'There are no known recipients!' + echohl None + endi +endf + +" Function: s:GPGViewOptions() {{{2 +" +" echo the recipients +" +fun s:GPGViewOptions() + if (exists("b:GPGOptions")) + echo 'This file has following options:' + " echo the options + let field=0 + let option=s:GetField(b:GPGOptions, ":", field) + while (strlen(option) > 0) + echo option + + let field=field+1 + let option=s:GetField(b:GPGOptions, ":", field) + endw + endi +endf + +" Function: s:GPGEditOptions() {{{2 +" +" create a scratch buffer with all recipients to add/remove recipients +" +fun s:GPGEditOptions() + " only do this if it isn't already a GPGOptions_* buffer + if (match(bufname("%"), "^\\(GPGRecipients_\\|GPGOptions_\\)") != 0 && match(bufname("%"), "\.gpg$") >= 0) + + " save buffer name + let buffername=bufname("%") + let editbuffername="GPGOptions_" . buffername + + " create scratch buffer + exe 'silent! split ' . editbuffername + + " check if this buffer exists + if (bufexists(editbuffername)) + " empty the buffer + silent normal! 1GdG + endi + + " Mark the buffer as a scratch buffer + setlocal buftype=nofile + setlocal noswapfile + setlocal nowrap + setlocal nobuflisted + setlocal nonumber + + " so we know for which other buffer this edit buffer is + let b:corresponding_to=buffername + + " put some comments to the scratch buffer + silent put ='GPG: ----------------------------------------------------------------------' + silent put ='GPG: THERE IS NO CHECK OF THE ENTERED OPTIONS!' + silent put ='GPG: YOU NEED TO KNOW WHAT YOU ARE DOING!' + silent put ='GPG: IF IN DOUBT, QUICKLY EXIT USING :x OR :bd' + silent put ='GPG: Please edit the list of options, one option per line' + silent put ='GPG: Please refer to the gpg documentation for valid options' + silent put ='GPG: Lines beginning with \"GPG:\" are removed automatically' + silent put ='GPG: Use :x or :bd to close this buffer' + silent put ='GPG: ----------------------------------------------------------------------' + + " put the options in the scratch buffer + let options=getbufvar(b:corresponding_to, "GPGOptions") + let field=0 + + let option=s:GetField(options, ":", field) + while (strlen(option) > 0) + silent put =option + + let field=field+1 + let option=s:GetField(options, ":", field) + endw + + " delete the empty first line + silent normal! 1Gdd + + " jump to the first option + silent normal! 6G + + " add a autocommand to regenerate the options after a write + augroup GPGEditOptions + augroup END + execute 'au GPGEditOptions BufHidden ' . editbuffername . ' call s:GPGFinishOptionsBuffer()' + + " define highlight + if (has("syntax") && exists("g:syntax_on")) + syntax match GPGComment "^GPG:.*$" + highlight clear GPGComment + highlight link GPGComment Comment + endi + endi +endf + +" Function: s:GPGFinishOptionsBuffer() {{{2 +" +" create a new option list from OptionsBuffer +fun s:GPGFinishOptionsBuffer() + " clear GPGOptions and GPGUnknownOptions + let GPGOptions="" + let GPGUnknownOptions="" + + " delete the autocommand + exe "au! GPGEditOptions * " . bufname("%") + + let currentline=1 + let option=getline(currentline) + + " get the options from the scratch buffer + while (currentline <= line("$")) + " delete all spaces at beginning and end of the line + " also delete a '!' at the beginning of the line + let option=substitute(option, "^[[:space:]!]*\\(.\\{-}\\)[[:space:]]*$", "\\1", "") + " delete comment lines + let option=substitute(option, "^GPG:.*$", "", "") + + " only do this if the line is not empty + if (strlen(option) > 0) + let GPGOptions=GPGOptions . option . ":" + endi + + let currentline=currentline+1 + let option=getline(currentline) + endw + + " write back the new option list to the corresponding buffer and mark it + " as modified + call setbufvar(b:corresponding_to, "GPGOptions", GPGOptions) + call setbufvar(b:corresponding_to, "&mod", 1) + "echo "GPGOptions=\"" . getbufvar(b:corresponding_to, "GPGOptions") . "\"" + +endf + +" Function: s:GPGNameToID(name) {{{2 +" +" find GPG key ID corresponding to a name +" Returns: ID for the given name +fun s:GPGNameToID(name) + " ask gpg for the id for a name + let shsave=&sh + let &sh='sh' + let output=system(s:gpgcommand . " --quiet --with-colons --fixed-list-mode --list-keys \"" . a:name . "\"") + let &sh=shsave + + " parse the output of gpg + let pub_seen=0 + let uid_seen=0 + let line=0 + let counter=0 + let gpgids="" + let choices="The name \"" . a:name . "\" is ambiguous. Please select the correct key:\n" + let linecontent=s:GetField(output, "\n", line) + while (strlen(linecontent)) + " search for the next uid + if (pub_seen == 1) + if (s:GetField(linecontent, ":", 0) == "uid") + if (uid_seen == 0) + let choices=choices . counter . ": " . s:GetField(linecontent, ":", 9) . "\n" + let counter=counter+1 + let uid_seen=1 + else + let choices=choices . " " . s:GetField(linecontent, ":", 9) . "\n" + endi + else + let uid_seen=0 + let pub_seen=0 + endi + endi + + " search for the next pub + if (pub_seen == 0) + if (s:GetField(linecontent, ":", 0) == "pub") + let gpgids=gpgids . s:GetField(linecontent, ":", 4) . ":" + let pub_seen=1 + endi + endi + + let line=line+1 + let linecontent=s:GetField(output, "\n", line) + endw + + " counter > 1 means we have more than one results + let answer=0 + if (counter > 1) + let choices=choices . "Enter number: " + let answer=input(choices, "0") + while (answer == "") + let answer=input("Enter number: ", "0") + endw + endi + + return s:GetField(gpgids, ":", answer) +endf + +" Function: s:GPGIDToName(identity) {{{2 +" +" find name corresponding to a GPG key ID +" Returns: Name for the given ID +fun s:GPGIDToName(identity) + " TODO is the encryption subkey really unique? + + " ask gpg for the id for a name + let shsave=&sh + let &sh='sh' + let output=system(s:gpgcommand . " --quiet --with-colons --fixed-list-mode --list-keys " . a:identity ) + let &sh=shsave + + " parse the output of gpg + let pub_seen=0 + let finish=0 + let line=0 + let linecontent=s:GetField(output, "\n", line) + while (strlen(linecontent) && !finish) + if (pub_seen == 0) " search for the next pub + if (s:GetField(linecontent, ":", 0) == "pub") + let pub_seen=1 + endi + else " search for the next uid + if (s:GetField(linecontent, ":", 0) == "uid") + let pub_seen=0 + let finish=1 + let uid=s:GetField(linecontent, ":", 9) + endi + endi + + let line=line+1 + let linecontent=s:GetField(output, "\n", line) + endw + + return uid +endf + +" Function: s:GetField(line, separator, field) {{{2 +" +" find field of 'separator' separated string, counting starts with 0 +" Returns: content of the field, if field doesn't exist it returns an empty +" string +fun s:GetField(line, separator, field) + let counter=a:field + let separatorLength=strlen(a:separator) + let start=0 + let end=match(a:line, a:separator) + if (end < 0) + let end=strlen(a:line) + endi + + " search for requested field + while (start < strlen(a:line) && counter > 0) + let counter=counter-separatorLength + let start=end+separatorLength + let end=match(a:line, a:separator, start) + if (end < 0) + let end=strlen(a:line) + endi + endw + + if (start < strlen(a:line)) + return strpart(a:line, start, end-start) + else + return "" + endi +endf +" Section: Command definitions {{{1 +com! GPGViewRecipients call s:GPGViewRecipients() +com! GPGEditRecipients call s:GPGEditRecipients() +com! GPGViewOptions call s:GPGViewOptions() +com! GPGEditOptions call s:GPGEditOptions() + +" vim600: set foldmethod=marker: From a14b9fe5923048cf620d569694ad4ac90a624a48 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Thu, 16 Nov 2006 15:29:55 +0000 Subject: [PATCH 002/115] Introduced variable GPGUseAgent Selectively enable usage of gpg-agent. --- plugin/gnupg.vim | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index fa47917..06ad5ff 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -42,6 +42,10 @@ " :GPGViewRecipients " Prints the list of options. " +" Variables: +" GPGUseAgent +" If set to 1 a possible available gpg-agent is used. Defaults to 0. +" " Credits: " Mathieu Clabaut for inspirations through his vimspell.vim script. " Section: Plugin header {{{1 @@ -92,17 +96,22 @@ highlight default GPGHighlightUnknownRecipient term=reverse ctermfg=Red cterm=un " initialize the plugin " fun s:GPGInit() + " check if gpg-agent is allowed + if (!exists("GPGUseAgent")) + let GPGUseAgent = 0 + endif + " determine if gnupg can use the gpg-agent - if (exists("$GPG_AGENT_INFO")) - let s:gpgcommand="LANG=C gpg --use-agent" + if (exists("$GPG_AGENT_INFO") && GPGUseAgent == 1) + let s:GPGCommand="LANG=C gpg --use-agent" else - let s:gpgcommand="LANG=C gpg --no-use-agent" + let s:GPGCommand="LANG=C gpg --no-use-agent" endif " find the supported algorithms let shsave=&sh let &sh='sh' - let output=system(s:gpgcommand . " --version") + let output=system(s:GPGCommand . " --version") let &sh=shsave let s:GPGPubkey=substitute(output, ".*Pubkey: \\(.\\{-}\\)\n.*", "\\1", "") @@ -127,7 +136,7 @@ fun s:GPGDecrypt() " find the recipients of the file let shsave=&sh let &sh='sh' - let output=system(s:gpgcommand . " --decrypt --dry-run --batch " . filename) + let output=system(s:GPGCommand . " --decrypt --dry-run --batch " . filename) let &sh=shsave " check if the file is symmetric/asymmetric encrypted @@ -177,7 +186,7 @@ fun s:GPGDecrypt() " we must redirect stderr (using sh temporarily) let shsave=&sh let &sh='sh' - exec "'[,']!" . s:gpgcommand . " --quiet --decrypt 2>/dev/null" + exec "'[,']!" . s:GPGCommand . " --quiet --decrypt 2>/dev/null" let &sh=shsave if (v:shell_error) " message could not be decrypted silent u @@ -241,7 +250,7 @@ fun s:GPGEncrypt() " encrypt the buffer let shsave=&sh let &sh='sh' - silent exec "'[,']!" . s:gpgcommand . " --quiet --no-encrypt-to " . options . recipients . " 2>/dev/null" + silent exec "'[,']!" . s:GPGCommand . " --quiet --no-encrypt-to " . options . recipients . " 2>/dev/null" let &sh=shsave if (v:shell_error) " message could not be encrypted silent u @@ -583,7 +592,7 @@ fun s:GPGNameToID(name) " ask gpg for the id for a name let shsave=&sh let &sh='sh' - let output=system(s:gpgcommand . " --quiet --with-colons --fixed-list-mode --list-keys \"" . a:name . "\"") + let output=system(s:GPGCommand . " --quiet --with-colons --fixed-list-mode --list-keys \"" . a:name . "\"") let &sh=shsave " parse the output of gpg @@ -646,7 +655,7 @@ fun s:GPGIDToName(identity) " ask gpg for the id for a name let shsave=&sh let &sh='sh' - let output=system(s:gpgcommand . " --quiet --with-colons --fixed-list-mode --list-keys " . a:identity ) + let output=system(s:GPGCommand . " --quiet --with-colons --fixed-list-mode --list-keys " . a:identity ) let &sh=shsave " parse the output of gpg From d0540c6b4839d43cf711210a8e9f2b8b213516e0 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Thu, 16 Nov 2006 16:12:57 +0000 Subject: [PATCH 003/115] Information about missing environment GPG_TTY. --- plugin/gnupg.vim | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 06ad5ff..f844b5d 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -43,7 +43,7 @@ " Prints the list of options. " " Variables: -" GPGUseAgent +" g:GPGUseAgent " If set to 1 a possible available gpg-agent is used. Defaults to 0. " " Credits: @@ -97,12 +97,18 @@ highlight default GPGHighlightUnknownRecipient term=reverse ctermfg=Red cterm=un " fun s:GPGInit() " check if gpg-agent is allowed - if (!exists("GPGUseAgent")) - let GPGUseAgent = 0 + if (!exists("g:GPGUseAgent")) + let g:GPGUseAgent = 0 endif " determine if gnupg can use the gpg-agent - if (exists("$GPG_AGENT_INFO") && GPGUseAgent == 1) + if (exists("$GPG_AGENT_INFO") && g:GPGUseAgent == 1) + if (!exists("$GPG_TTY")) + echohl GPGError + echo "The GPG_TTY is not set!" + echo "gpg-agent might not work." + echohl None + endif let s:GPGCommand="LANG=C gpg --use-agent" else let s:GPGCommand="LANG=C gpg --no-use-agent" From 8c2d996b57337739f78a02c380918fdd6c92c7bf Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Tue, 21 Nov 2006 08:54:56 +0000 Subject: [PATCH 004/115] Default of variable GPGUseAgent to 1 This way so the default behaviour is not changed compared to previous versions. --- plugin/gnupg.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index f844b5d..51cb93f 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -44,7 +44,7 @@ " " Variables: " g:GPGUseAgent -" If set to 1 a possible available gpg-agent is used. Defaults to 0. +" If set to 0 a possible available gpg-agent won't be used. Defaults to 1. " " Credits: " Mathieu Clabaut for inspirations through his vimspell.vim script. @@ -98,7 +98,7 @@ highlight default GPGHighlightUnknownRecipient term=reverse ctermfg=Red cterm=un fun s:GPGInit() " check if gpg-agent is allowed if (!exists("g:GPGUseAgent")) - let g:GPGUseAgent = 0 + let g:GPGUseAgent = 1 endif " determine if gnupg can use the gpg-agent From d29ae7efa31fdee1fd390427f0a8a85d27cdaea8 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Fri, 8 Dec 2006 08:02:59 +0000 Subject: [PATCH 005/115] Recognize '.pgp' suffix --- plugin/gnupg.vim | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 51cb93f..aeff65f 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -9,8 +9,8 @@ " Description: " " This script implements transparent editing of gpg encrypted files. The -" filename must have a ".gpg" suffix. When opening such a file the content -" is decrypted, when opening a new file the script will ask for the +" filename must have a ".gpg" or ".pgp" suffix. When opening such a file the +" content is decrypted, when opening a new file the script will ask for the " recipients of the encrypted file. The file content will be encrypted to " all recipients before it is written. The script turns off viminfo and " swapfile to increase security. @@ -49,7 +49,7 @@ " Credits: " Mathieu Clabaut for inspirations through his vimspell.vim script. " Section: Plugin header {{{1 -if (exists("loaded_gnupg") || &cp || exists("#BufReadPre#*.gpg")) +if (exists("loaded_gnupg") || &cp || exists("#BufReadPre#*.\(gpg\|pgp\)")) finish endi let loaded_gnupg = 1 @@ -60,31 +60,31 @@ au! " First make sure nothing is written to ~/.viminfo while editing " an encrypted file. -autocmd BufNewFile,BufReadPre,FileReadPre *.gpg set viminfo= +autocmd BufNewFile,BufReadPre,FileReadPre *.\(gpg\|pgp\) set viminfo= " We don't want a swap file, as it writes unencrypted data to disk -autocmd BufNewFile,BufReadPre,FileReadPre *.gpg set noswapfile +autocmd BufNewFile,BufReadPre,FileReadPre *.\(gpg\|pgp\) set noswapfile " Initialize the internal variables -autocmd BufNewFile,BufReadPre,FileReadPre *.gpg call s:GPGInit() +autocmd BufNewFile,BufReadPre,FileReadPre *.\(gpg\|pgp\) call s:GPGInit() " Force the user to edit the recipient list if he opens a new file -autocmd BufNewFile *.gpg call s:GPGEditRecipients() +autocmd BufNewFile *.\(gpg\|pgp\) call s:GPGEditRecipients() " Switch to binary mode to read the encrypted file -autocmd BufReadPre,FileReadPre *.gpg set bin -autocmd BufReadPost,FileReadPost *.gpg call s:GPGDecrypt() +autocmd BufReadPre,FileReadPre *.\(gpg\|pgp\) set bin +autocmd BufReadPost,FileReadPost *.\(gpg\|pgp\) call s:GPGDecrypt() " Switch to normal mode for editing -autocmd BufReadPost,FileReadPost *.gpg set nobin +autocmd BufReadPost,FileReadPost *.\(gpg\|pgp\) set nobin " Call the autocommand for the file minus .gpg$ -autocmd BufReadPost,FileReadPost *.gpg execute ":doautocmd BufReadPost " . expand("%:r") -autocmd BufReadPost,FileReadPost *.gpg execute ":redraw!" +autocmd BufReadPost,FileReadPost *.\(gpg\|pgp\) execute ":doautocmd BufReadPost " . expand("%:r") +autocmd BufReadPost,FileReadPost *.\(gpg\|pgp\) execute ":redraw!" " Switch to binary mode before encrypt the file -autocmd BufWritePre,FileWritePre *.gpg set bin +autocmd BufWritePre,FileWritePre *.\(gpg\|pgp\) set bin " Convert all text to encrypted text before writing -autocmd BufWritePre,FileWritePre *.gpg call s:GPGEncrypt() +autocmd BufWritePre,FileWritePre *.\(gpg\|pgp\) call s:GPGEncrypt() " Undo the encryption so we are back in the normal text, directly " after the file has been written. -autocmd BufWritePost,FileWritePost *.gpg silent u +autocmd BufWritePost,FileWritePost *.\(gpg\|pgp\) silent u " Switch back to normal mode for editing -autocmd BufWritePost,FileWritePost *.gpg set nobin +autocmd BufWritePost,FileWritePost *.\(gpg\|pgp\) set nobin augroup END " Section: Highlight setup {{{1 highlight default GPGWarning term=reverse ctermfg=Yellow guifg=Yellow From bb849bd53e4f5286694da3155faee959e87e7b58 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Fri, 8 Dec 2006 10:27:58 +0000 Subject: [PATCH 006/115] Make gnupg.vim working with cmd under windows. --- plugin/gnupg.vim | 63 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 42 insertions(+), 21 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index aeff65f..6fa605a 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -114,11 +114,27 @@ fun s:GPGInit() let s:GPGCommand="LANG=C gpg --no-use-agent" endif + " setup shell environment for unix and windows + let s:shellredirsave=&shellredir + let s:shellsave=&shell + if (match(&shell,"cmd.exe")) + " windows specific settings + let s:shellredir = '>%s' + let s:shell = &shell + let s:redirnull = '2>nul' + else + " unix specific settings + let s:shellredir = &shellredir + let s:shell = 'sh' + let s:redirnull ='2>/dev/null' + endi + " find the supported algorithms - let shsave=&sh - let &sh='sh' + let &shellredir=s:shellredir + let &shell=s:shell let output=system(s:GPGCommand . " --version") - let &sh=shsave + let &shellredir=s:shellredir + let &shell=s:shellsave let s:GPGPubkey=substitute(output, ".*Pubkey: \\(.\\{-}\\)\n.*", "\\1", "") let s:GPGCipher=substitute(output, ".*Cipher: \\(.\\{-}\\)\n.*", "\\1", "") @@ -140,10 +156,11 @@ fun s:GPGDecrypt() let b:GPGOptions="" " find the recipients of the file - let shsave=&sh - let &sh='sh' + let &shellredir=s:shellredir + let &shell=s:shell let output=system(s:GPGCommand . " --decrypt --dry-run --batch " . filename) - let &sh=shsave + let &shellredir=s:shellredir + let &shell=s:shellsave " check if the file is symmetric/asymmetric encrypted if (match(output, "gpg: [^ ]\\+ encrypted data") >= 0) @@ -189,11 +206,12 @@ fun s:GPGDecrypt() " finally decrypt the buffer content " since even with the --quiet option passphrase typos will be reported, - " we must redirect stderr (using sh temporarily) - let shsave=&sh - let &sh='sh' - exec "'[,']!" . s:GPGCommand . " --quiet --decrypt 2>/dev/null" - let &sh=shsave + " we must redirect stderr (using shell temporarily) + let &shellredir=s:shellredir + let &shell=s:shell + exec "'[,']!" . s:GPGCommand . " --quiet --decrypt " . s:redirnull + let &shellredir=s:shellredir + let &shell=s:shellsave if (v:shell_error) " message could not be decrypted silent u echohl GPGError @@ -254,10 +272,11 @@ fun s:GPGEncrypt() endi " encrypt the buffer - let shsave=&sh - let &sh='sh' - silent exec "'[,']!" . s:GPGCommand . " --quiet --no-encrypt-to " . options . recipients . " 2>/dev/null" - let &sh=shsave + let &shellredir=s:shellredir + let &shell=s:shell + silent exec "'[,']!" . s:GPGCommand . " --quiet --no-encrypt-to " . options . recipients . " " . s:redirnull + let &shellredir=s:shellredir + let &shell=s:shellsave if (v:shell_error) " message could not be encrypted silent u echohl GPGError @@ -596,10 +615,11 @@ endf " Returns: ID for the given name fun s:GPGNameToID(name) " ask gpg for the id for a name - let shsave=&sh - let &sh='sh' + let &shellredir=s:shellredir + let &shell=s:shell let output=system(s:GPGCommand . " --quiet --with-colons --fixed-list-mode --list-keys \"" . a:name . "\"") - let &sh=shsave + let &shellredir=s:shellredir + let &shell=s:shellsave " parse the output of gpg let pub_seen=0 @@ -659,10 +679,11 @@ fun s:GPGIDToName(identity) " TODO is the encryption subkey really unique? " ask gpg for the id for a name - let shsave=&sh - let &sh='sh' + let &shellredir=s:shellredir + let &shell=s:shell let output=system(s:GPGCommand . " --quiet --with-colons --fixed-list-mode --list-keys " . a:identity ) - let &sh=shsave + let &shellredir=s:shellredir + let &shell=s:shellsave " parse the output of gpg let pub_seen=0 From 768dda0d949b488543c283a7571f39dab1c8da72 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Fri, 8 Dec 2006 11:58:26 +0000 Subject: [PATCH 007/115] Forgot one place while adding '.pgp' suffix. --- plugin/gnupg.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 6fa605a..02e6e25 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -335,7 +335,7 @@ endf " fun s:GPGEditRecipients() " only do this if it isn't already a GPGRecipients_* buffer - if (match(bufname("%"), "^\\(GPGRecipients_\\|GPGOptions_\\)") != 0 && match(bufname("%"), "\.gpg$") >= 0) + if (match(bufname("%"), "^\\(GPGRecipients_\\|GPGOptions_\\)") != 0 && match(bufname("%"), "\.\\(gpg\\|pgp\\)$") >= 0) " save buffer name let buffername=bufname("%") @@ -502,7 +502,7 @@ endf " fun s:GPGEditOptions() " only do this if it isn't already a GPGOptions_* buffer - if (match(bufname("%"), "^\\(GPGRecipients_\\|GPGOptions_\\)") != 0 && match(bufname("%"), "\.gpg$") >= 0) + if (match(bufname("%"), "^\\(GPGRecipients_\\|GPGOptions_\\)") != 0 && match(bufname("%"), "\.\\(gpg\\|pgp\\)$") >= 0) " save buffer name let buffername=bufname("%") From bc2f74b4f255a41002e494ec2346789d9ec56210 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Tue, 5 Jul 2011 08:44:14 +0200 Subject: [PATCH 008/115] Check for cmd.com and command.com to check for windows system. --- plugin/gnupg.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 02e6e25..a5c74c0 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -117,7 +117,7 @@ fun s:GPGInit() " setup shell environment for unix and windows let s:shellredirsave=&shellredir let s:shellsave=&shell - if (match(&shell,"cmd.exe")) + if (match(&shell,"\\(cmd\\|command\\).exe") >= 0) " windows specific settings let s:shellredir = '>%s' let s:shell = &shell From 0e6b891b8e24d198ade58999ffb29ccc578da669 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Tue, 5 Jul 2011 08:44:44 +0200 Subject: [PATCH 009/115] Make symmetric encryption working again. --- plugin/gnupg.vim | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index a5c74c0..a51e1d4 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -121,12 +121,14 @@ fun s:GPGInit() " windows specific settings let s:shellredir = '>%s' let s:shell = &shell - let s:redirnull = '2>nul' + let s:stderrredir = '2>&1' + let s:stderrredirnull = '2>nul' else " unix specific settings let s:shellredir = &shellredir let s:shell = 'sh' - let s:redirnull ='2>/dev/null' + let s:stderrredir = '2>&1' + let s:stderrredirnull ='2>/dev/null' endi " find the supported algorithms @@ -158,7 +160,7 @@ fun s:GPGDecrypt() " find the recipients of the file let &shellredir=s:shellredir let &shell=s:shell - let output=system(s:GPGCommand . " --decrypt --dry-run --batch " . filename) + let output=system(s:GPGCommand . " --decrypt --dry-run --batch --no-use-agent " . filename . " " . s:stderrredir) let &shellredir=s:shellredir let &shell=s:shellsave @@ -209,7 +211,7 @@ fun s:GPGDecrypt() " we must redirect stderr (using shell temporarily) let &shellredir=s:shellredir let &shell=s:shell - exec "'[,']!" . s:GPGCommand . " --quiet --decrypt " . s:redirnull + exec "'[,']!" . s:GPGCommand . " --quiet --decrypt " . s:stderrredirnull let &shellredir=s:shellredir let &shell=s:shellsave if (v:shell_error) " message could not be decrypted @@ -274,7 +276,7 @@ fun s:GPGEncrypt() " encrypt the buffer let &shellredir=s:shellredir let &shell=s:shell - silent exec "'[,']!" . s:GPGCommand . " --quiet --no-encrypt-to " . options . recipients . " " . s:redirnull + silent exec "'[,']!" . s:GPGCommand . " --quiet --no-encrypt-to " . options . recipients . " " . s:stderrredirnull let &shellredir=s:shellredir let &shell=s:shellsave if (v:shell_error) " message could not be encrypted From 9f3b48757c0eb365f9c8cf2ba37ee1d1d0bf8405 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Fri, 8 Dec 2006 16:02:33 +0000 Subject: [PATCH 010/115] Added '.asc' suffix. --- plugin/gnupg.vim | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index a51e1d4..b7fe61c 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -43,13 +43,15 @@ " Prints the list of options. " " Variables: +" " g:GPGUseAgent " If set to 0 a possible available gpg-agent won't be used. Defaults to 1. " " Credits: " Mathieu Clabaut for inspirations through his vimspell.vim script. +" Richard Bronosky for patch to enable native windows support " Section: Plugin header {{{1 -if (exists("loaded_gnupg") || &cp || exists("#BufReadPre#*.\(gpg\|pgp\)")) +if (exists("loaded_gnupg") || &cp || exists("#BufReadPre#*.\(gpg\|asc\|pgp\)")) finish endi let loaded_gnupg = 1 @@ -60,31 +62,31 @@ au! " First make sure nothing is written to ~/.viminfo while editing " an encrypted file. -autocmd BufNewFile,BufReadPre,FileReadPre *.\(gpg\|pgp\) set viminfo= +autocmd BufNewFile,BufReadPre,FileReadPre *.\(gpg\|asc\|pgp\) set viminfo= " We don't want a swap file, as it writes unencrypted data to disk -autocmd BufNewFile,BufReadPre,FileReadPre *.\(gpg\|pgp\) set noswapfile +autocmd BufNewFile,BufReadPre,FileReadPre *.\(gpg\|asc\|pgp\) set noswapfile " Initialize the internal variables -autocmd BufNewFile,BufReadPre,FileReadPre *.\(gpg\|pgp\) call s:GPGInit() +autocmd BufNewFile,BufReadPre,FileReadPre *.\(gpg\|asc\|pgp\) call s:GPGInit() " Force the user to edit the recipient list if he opens a new file -autocmd BufNewFile *.\(gpg\|pgp\) call s:GPGEditRecipients() +autocmd BufNewFile *.\(gpg\|asc\|pgp\) call s:GPGEditRecipients() " Switch to binary mode to read the encrypted file -autocmd BufReadPre,FileReadPre *.\(gpg\|pgp\) set bin -autocmd BufReadPost,FileReadPost *.\(gpg\|pgp\) call s:GPGDecrypt() +autocmd BufReadPre,FileReadPre *.\(gpg\|asc\|pgp\) set bin +autocmd BufReadPost,FileReadPost *.\(gpg\|asc\|pgp\) call s:GPGDecrypt() " Switch to normal mode for editing -autocmd BufReadPost,FileReadPost *.\(gpg\|pgp\) set nobin +autocmd BufReadPost,FileReadPost *.\(gpg\|asc\|pgp\) set nobin " Call the autocommand for the file minus .gpg$ -autocmd BufReadPost,FileReadPost *.\(gpg\|pgp\) execute ":doautocmd BufReadPost " . expand("%:r") -autocmd BufReadPost,FileReadPost *.\(gpg\|pgp\) execute ":redraw!" +autocmd BufReadPost,FileReadPost *.\(gpg\|asc\|pgp\) execute ":doautocmd BufReadPost " . expand("%:r") +autocmd BufReadPost,FileReadPost *.\(gpg\|asc\|pgp\) execute ":redraw!" " Switch to binary mode before encrypt the file -autocmd BufWritePre,FileWritePre *.\(gpg\|pgp\) set bin +autocmd BufWritePre,FileWritePre *.\(gpg\|asc\|pgp\) set bin " Convert all text to encrypted text before writing -autocmd BufWritePre,FileWritePre *.\(gpg\|pgp\) call s:GPGEncrypt() +autocmd BufWritePre,FileWritePre *.\(gpg\|asc\|pgp\) call s:GPGEncrypt() " Undo the encryption so we are back in the normal text, directly " after the file has been written. -autocmd BufWritePost,FileWritePost *.\(gpg\|pgp\) silent u +autocmd BufWritePost,FileWritePost *.\(gpg\|asc\|pgp\) silent u " Switch back to normal mode for editing -autocmd BufWritePost,FileWritePost *.\(gpg\|pgp\) set nobin +autocmd BufWritePost,FileWritePost *.\(gpg\|asc\|pgp\) set nobin augroup END " Section: Highlight setup {{{1 highlight default GPGWarning term=reverse ctermfg=Yellow guifg=Yellow @@ -198,7 +200,6 @@ fun s:GPGDecrypt() let start=match(output, "ID [[:xdigit:]]\\{8}", start) endw - "echo "GPGRecipients=\"" . b:GPGRecipients . "\"" endi " check if the message is armored @@ -264,7 +265,7 @@ fun s:GPGEncrypt() let gpgid=s:GetField(b:GPGRecipients, ":", field) endw else - if (match(b:GPGOptions, "symmetric:") < 0) + if (match(b:GPGOptions, "encrypt:") >= 0) echohl GPGError echo "There are no recipients!!" echo "Please use GPGEditRecipients to correct!!" @@ -337,7 +338,7 @@ endf " fun s:GPGEditRecipients() " only do this if it isn't already a GPGRecipients_* buffer - if (match(bufname("%"), "^\\(GPGRecipients_\\|GPGOptions_\\)") != 0 && match(bufname("%"), "\.\\(gpg\\|pgp\\)$") >= 0) + if (match(bufname("%"), "^\\(GPGRecipients_\\|GPGOptions_\\)") != 0 && match(bufname("%"), "\.\\(gpg\\|asc\\|pgp\\)$") >= 0) " save buffer name let buffername=bufname("%") @@ -469,7 +470,6 @@ fun s:GPGFinishRecipientsBuffer() call setbufvar(b:corresponding_to, "GPGRecipients", GPGRecipients) call setbufvar(b:corresponding_to, "GPGUnknownRecipients", GPGUnknownRecipients) call setbufvar(b:corresponding_to, "&mod", 1) - "echo "GPGRecipients=\"" . getbufvar(b:corresponding_to, "GPGRecipients") . "\"" " check if there is any known recipient if (strlen(s:GetField(GPGRecipients, ":", 0)) == 0) @@ -504,7 +504,7 @@ endf " fun s:GPGEditOptions() " only do this if it isn't already a GPGOptions_* buffer - if (match(bufname("%"), "^\\(GPGRecipients_\\|GPGOptions_\\)") != 0 && match(bufname("%"), "\.\\(gpg\\|pgp\\)$") >= 0) + if (match(bufname("%"), "^\\(GPGRecipients_\\|GPGOptions_\\)") != 0 && match(bufname("%"), "\.\\(gpg\\|asc\\|pgp\\)$") >= 0) " save buffer name let buffername=bufname("%") @@ -607,7 +607,6 @@ fun s:GPGFinishOptionsBuffer() " as modified call setbufvar(b:corresponding_to, "GPGOptions", GPGOptions) call setbufvar(b:corresponding_to, "&mod", 1) - "echo "GPGOptions=\"" . getbufvar(b:corresponding_to, "GPGOptions") . "\"" endf From ca35a3b51d851e135cb4636a63c9a9165a0272ef Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Tue, 12 Dec 2006 22:46:03 +0000 Subject: [PATCH 011/115] Documentation and credits update. --- plugin/gnupg.vim | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index b7fe61c..f77c6e7 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -9,11 +9,11 @@ " Description: " " This script implements transparent editing of gpg encrypted files. The -" filename must have a ".gpg" or ".pgp" suffix. When opening such a file the -" content is decrypted, when opening a new file the script will ask for the -" recipients of the encrypted file. The file content will be encrypted to -" all recipients before it is written. The script turns off viminfo and -" swapfile to increase security. +" filename must have a ".gpg", ".pgp" or ".asc" suffix. When opening such +" a file the content is decrypted, when opening a new file the script will +" ask for the recipients of the encrypted file. The file content will be +" encrypted to all recipients before it is written. The script turns off +" viminfo and swapfile to increase security. " " Installation: " @@ -49,7 +49,9 @@ " " Credits: " Mathieu Clabaut for inspirations through his vimspell.vim script. -" Richard Bronosky for patch to enable native windows support +" Richard Bronosky for patch to enable ".pgp" suffix. +" Erik Remmelzwaal for patch to enable windows support. +" " Section: Plugin header {{{1 if (exists("loaded_gnupg") || &cp || exists("#BufReadPre#*.\(gpg\|asc\|pgp\)")) finish From 9a272dbf45a80c0988583c807d5f5cb15bbc83ae Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Wed, 13 Dec 2006 14:58:58 +0000 Subject: [PATCH 012/115] Some fixes for windows. --- plugin/gnupg.vim | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index f77c6e7..46fafad 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -113,9 +113,9 @@ fun s:GPGInit() echo "gpg-agent might not work." echohl None endif - let s:GPGCommand="LANG=C gpg --use-agent" + let s:GPGCommand="gpg --use-agent" else - let s:GPGCommand="LANG=C gpg --no-use-agent" + let s:GPGCommand="gpg --no-use-agent" endif " setup shell environment for unix and windows @@ -125,14 +125,13 @@ fun s:GPGInit() " windows specific settings let s:shellredir = '>%s' let s:shell = &shell - let s:stderrredir = '2>&1' let s:stderrredirnull = '2>nul' else " unix specific settings let s:shellredir = &shellredir let s:shell = 'sh' - let s:stderrredir = '2>&1' let s:stderrredirnull ='2>/dev/null' + let s:GPGCommand="LANG=C " . s:GPGCommand endi " find the supported algorithms @@ -164,7 +163,7 @@ fun s:GPGDecrypt() " find the recipients of the file let &shellredir=s:shellredir let &shell=s:shell - let output=system(s:GPGCommand . " --decrypt --dry-run --batch --no-use-agent " . filename . " " . s:stderrredir) + let output=system(s:GPGCommand . " --decrypt --dry-run --batch --no-use-agent --logger-fd 1 " . filename) let &shellredir=s:shellredir let &shell=s:shellsave @@ -418,7 +417,7 @@ fun s:GPGEditRecipients() silent normal! 1Gdd " jump to the first recipient - silent normal! 6G + silent normal! G " add a autocommand to regenerate the recipients after a write augroup GPGEditRecipients @@ -558,7 +557,7 @@ fun s:GPGEditOptions() silent normal! 1Gdd " jump to the first option - silent normal! 6G + silent normal! G " add a autocommand to regenerate the options after a write augroup GPGEditOptions From 4771cc9025a90c21245edad41567f33138863dd4 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Wed, 13 Dec 2006 15:28:31 +0000 Subject: [PATCH 013/115] Edit unencrypted files with "encryption" suffix as normal files. --- plugin/gnupg.vim | 73 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 70 insertions(+), 3 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 46fafad..e4fc41c 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -155,7 +155,8 @@ fun s:GPGDecrypt() " get the filename of the current buffer let filename=escape(expand("%:p"), ' *?\"'."'") - " clear GPGRecipients, GPGUnknownRecipients and GPGOptions + " clear GPGEncrypted, GPGRecipients, GPGUnknownRecipients and GPGOptions + let b:GPGEncrypted=0 let b:GPGRecipients="" let b:GPGUnknownRecipients="" let b:GPGOptions="" @@ -170,6 +171,8 @@ fun s:GPGDecrypt() " check if the file is symmetric/asymmetric encrypted if (match(output, "gpg: [^ ]\\+ encrypted data") >= 0) " file is symmetric encrypted + let b:GPGEncrypted=1 + let b:GPGOptions=b:GPGOptions . "symmetric:" let cipher=substitute(output, ".*gpg: \\([^ ]\\+\\) encrypted data.*", "\\1", "") @@ -181,8 +184,10 @@ fun s:GPGDecrypt() echo echohl None endi - else + elseif (match(output, "gpg: public key decryption") >= 0) " file is asymmetric encrypted + let b:GPGEncrypted=1 + let b:GPGOptions=b:GPGOptions . "encrypt:" let start=match(output, "ID [[:xdigit:]]\\{8}") @@ -200,7 +205,13 @@ fun s:GPGDecrypt() end let start=match(output, "ID [[:xdigit:]]\\{8}", start) endw - + elseif (match(output, "gpg: no valid OpenPGP data found") >= 0) + " file is not encrypted + let b:GPGEncrypted=0 + echohl GPGWarning + echo "File is not encrypted, all GPG functions disabled!" + echohl None + return endi " check if the message is armored @@ -231,6 +242,14 @@ endf " encrypts the buffer to all previous recipients " fun s:GPGEncrypt() + " guard for unencrypted files + if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) + echohl GPGWarning + echo "File is not encrypted, all GPG functions disabled!" + echohl None + return + endi + let options="" let recipients="" let field=0 @@ -298,6 +317,14 @@ endf " echo the recipients " fun s:GPGViewRecipients() + " guard for unencrypted files + if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) + echohl GPGWarning + echo "File is not encrypted, all GPG functions disabled!" + echohl None + return + endi + if (exists("b:GPGRecipients")) echo 'This file has following recipients (Unknown recipients have a prepended "!"):' " echo the recipients @@ -338,6 +365,14 @@ endf " create a scratch buffer with all recipients to add/remove recipients " fun s:GPGEditRecipients() + " guard for unencrypted files + if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) + echohl GPGWarning + echo "File is not encrypted, all GPG functions disabled!" + echohl None + return + endi + " only do this if it isn't already a GPGRecipients_* buffer if (match(bufname("%"), "^\\(GPGRecipients_\\|GPGOptions_\\)") != 0 && match(bufname("%"), "\.\\(gpg\\|asc\\|pgp\\)$") >= 0) @@ -431,6 +466,14 @@ endf " " create a new recipient list from RecipientsBuffer fun s:GPGFinishRecipientsBuffer() + " guard for unencrypted files + if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) + echohl GPGWarning + echo "File is not encrypted, all GPG functions disabled!" + echohl None + return + endi + " clear GPGRecipients and GPGUnknownRecipients let GPGRecipients="" let GPGUnknownRecipients="" @@ -485,6 +528,14 @@ endf " echo the recipients " fun s:GPGViewOptions() + " guard for unencrypted files + if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) + echohl GPGWarning + echo "File is not encrypted, all GPG functions disabled!" + echohl None + return + endi + if (exists("b:GPGOptions")) echo 'This file has following options:' " echo the options @@ -504,6 +555,14 @@ endf " create a scratch buffer with all recipients to add/remove recipients " fun s:GPGEditOptions() + " guard for unencrypted files + if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) + echohl GPGWarning + echo "File is not encrypted, all GPG functions disabled!" + echohl None + return + endi + " only do this if it isn't already a GPGOptions_* buffer if (match(bufname("%"), "^\\(GPGRecipients_\\|GPGOptions_\\)") != 0 && match(bufname("%"), "\.\\(gpg\\|asc\\|pgp\\)$") >= 0) @@ -577,6 +636,14 @@ endf " " create a new option list from OptionsBuffer fun s:GPGFinishOptionsBuffer() + " guard for unencrypted files + if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) + echohl GPGWarning + echo "File is not encrypted, all GPG functions disabled!" + echohl None + return + endi + " clear GPGOptions and GPGUnknownOptions let GPGOptions="" let GPGUnknownOptions="" From 9a15ce7ef1e18041792b202e713f5c17b5c57fa3 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Wed, 13 Dec 2006 16:04:30 +0000 Subject: [PATCH 014/115] Only undo encryption of buffer content for encrypted files. --- plugin/gnupg.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index e4fc41c..b3c3d4d 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -86,7 +86,7 @@ autocmd BufWritePre,FileWritePre *.\(gpg\|asc\|pgp\) set bin autocmd BufWritePre,FileWritePre *.\(gpg\|asc\|pgp\) call s:GPGEncrypt() " Undo the encryption so we are back in the normal text, directly " after the file has been written. -autocmd BufWritePost,FileWritePost *.\(gpg\|asc\|pgp\) silent u +autocmd BufWritePost,FileWritePost *.\(gpg\|asc\|pgp\) if (exists("b:GPGEncrypted") && b:GPGEncrypted == 1) | silent u | endi " Switch back to normal mode for editing autocmd BufWritePost,FileWritePost *.\(gpg\|asc\|pgp\) set nobin augroup END From ac21a24b3d2640346dec9a3595505ddaa000db86 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Thu, 14 Dec 2006 07:34:09 +0000 Subject: [PATCH 015/115] Better escaping file names with spaces --- plugin/gnupg.vim | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index b3c3d4d..f1ca44e 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -77,7 +77,7 @@ autocmd BufReadPost,FileReadPost *.\(gpg\|asc\|pgp\) call s:GPGDec " Switch to normal mode for editing autocmd BufReadPost,FileReadPost *.\(gpg\|asc\|pgp\) set nobin " Call the autocommand for the file minus .gpg$ -autocmd BufReadPost,FileReadPost *.\(gpg\|asc\|pgp\) execute ":doautocmd BufReadPost " . expand("%:r") +autocmd BufReadPost,FileReadPost *.\(gpg\|asc\|pgp\) execute ":doautocmd BufReadPost " . escape(expand("%:r"), ' *?\"'."'") autocmd BufReadPost,FileReadPost *.\(gpg\|asc\|pgp\) execute ":redraw!" " Switch to binary mode before encrypt the file @@ -153,7 +153,7 @@ endf " fun s:GPGDecrypt() " get the filename of the current buffer - let filename=escape(expand("%:p"), ' *?\"'."'") + let filename=escape(expand("%:p"), '\"') " clear GPGEncrypted, GPGRecipients, GPGUnknownRecipients and GPGOptions let b:GPGEncrypted=0 @@ -164,10 +164,12 @@ fun s:GPGDecrypt() " find the recipients of the file let &shellredir=s:shellredir let &shell=s:shell - let output=system(s:GPGCommand . " --decrypt --dry-run --batch --no-use-agent --logger-fd 1 " . filename) + let output=system(s:GPGCommand . " --decrypt --dry-run --batch --no-use-agent --logger-fd 1 \"" . filename . "\"") let &shellredir=s:shellredir let &shell=s:shellsave + echom ">>>" . s:GPGCommand . " --decrypt --dry-run --batch --no-use-agent --logger-fd 1 \"" . filename . "\"" . "<<<" + echom ">>>" . output . "<<<" " check if the file is symmetric/asymmetric encrypted if (match(output, "gpg: [^ ]\\+ encrypted data") >= 0) " file is symmetric encrypted @@ -377,7 +379,7 @@ fun s:GPGEditRecipients() if (match(bufname("%"), "^\\(GPGRecipients_\\|GPGOptions_\\)") != 0 && match(bufname("%"), "\.\\(gpg\\|asc\\|pgp\\)$") >= 0) " save buffer name - let buffername=bufname("%") + let buffername=escape(bufname("%"), ' *?\"'."'") let editbuffername="GPGRecipients_" . buffername " create scratch buffer @@ -479,7 +481,7 @@ fun s:GPGFinishRecipientsBuffer() let GPGUnknownRecipients="" " delete the autocommand - exe "au! GPGEditRecipients * " . bufname("%") + exe "au! GPGEditRecipients * " . escape(bufname("%"), ' *?\"'."'") let currentline=1 let recipient=getline(currentline) @@ -510,10 +512,11 @@ fun s:GPGFinishRecipientsBuffer() endw " write back the new recipient list to the corresponding buffer and mark it - " as modified + " as modified. Buffer is now for sure a encrypted buffer. call setbufvar(b:corresponding_to, "GPGRecipients", GPGRecipients) call setbufvar(b:corresponding_to, "GPGUnknownRecipients", GPGUnknownRecipients) call setbufvar(b:corresponding_to, "&mod", 1) + call setbufvar(b:corresponding_to, "GPGEncrypted", 1) " check if there is any known recipient if (strlen(s:GetField(GPGRecipients, ":", 0)) == 0) @@ -567,7 +570,7 @@ fun s:GPGEditOptions() if (match(bufname("%"), "^\\(GPGRecipients_\\|GPGOptions_\\)") != 0 && match(bufname("%"), "\.\\(gpg\\|asc\\|pgp\\)$") >= 0) " save buffer name - let buffername=bufname("%") + let buffername=escape(bufname("%"), ' *?\"'."'") let editbuffername="GPGOptions_" . buffername " create scratch buffer @@ -649,7 +652,7 @@ fun s:GPGFinishOptionsBuffer() let GPGUnknownOptions="" " delete the autocommand - exe "au! GPGEditOptions * " . bufname("%") + exe "au! GPGEditOptions * " . escape(bufname("%"), ' *?\"'."'") let currentline=1 let option=getline(currentline) From 621813ef6ed32bb3ff5af4d9a952cff124344064 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Thu, 14 Dec 2006 10:03:19 +0000 Subject: [PATCH 016/115] Make GPGEdit* commands more robust. --- plugin/gnupg.vim | 72 ++++++++++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 33 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index f1ca44e..ed39531 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -25,17 +25,15 @@ " " :GPGEditRecipients " Opens a scratch buffer to change the list of recipients. Recipients that -" are unknown (not in your public key) are highlighted and have a -" prepended "!". Closing the buffer with :x or :bd makes the changes -" permanent. +" are unknown (not in your public key) are highlighted and have +" a prepended "!". Closing the buffer makes the changes permanent. " " :GPGViewRecipients " Prints the list of recipients. " " :GPGEditOptions " Opens a scratch buffer to change the options for encryption (symmetric, -" asymmetric, signing). Closing the buffer with :x or :bd makes the -" changes permanent. +" asymmetric, signing). Closing the buffer makes the changes permanent. " WARNING: There is no check of the entered options, so you need to know " what you are doing. " @@ -168,8 +166,6 @@ fun s:GPGDecrypt() let &shellredir=s:shellredir let &shell=s:shellsave - echom ">>>" . s:GPGCommand . " --decrypt --dry-run --batch --no-use-agent --logger-fd 1 \"" . filename . "\"" . "<<<" - echom ">>>" . output . "<<<" " check if the file is symmetric/asymmetric encrypted if (match(output, "gpg: [^ ]\\+ encrypted data") >= 0) " file is symmetric encrypted @@ -379,14 +375,25 @@ fun s:GPGEditRecipients() if (match(bufname("%"), "^\\(GPGRecipients_\\|GPGOptions_\\)") != 0 && match(bufname("%"), "\.\\(gpg\\|asc\\|pgp\\)$") >= 0) " save buffer name - let buffername=escape(bufname("%"), ' *?\"'."'") + let buffername=bufname("%") let editbuffername="GPGRecipients_" . buffername - " create scratch buffer - exe 'silent! split ' . editbuffername - " check if this buffer exists - if (bufexists(editbuffername)) + if (!bufexists(editbuffername)) + " create scratch buffer + exe 'silent! split ' . escape(editbuffername, ' *?\"'."'") + + " add a autocommand to regenerate the recipients after a write + autocmd BufHidden,BufUnload call s:GPGFinishRecipientsBuffer() + else + if (bufwinnr(editbuffername) >= 0) + " switch to scratch buffer window + exe 'silent! ' . bufwinnr(editbuffername) . "wincmd w" + else + " split scratch buffer window + exe 'silent! sbuffer ' . escape(editbuffername, ' *?\"'."'") + endi + " empty the buffer silent normal! 1GdG endi @@ -406,7 +413,7 @@ fun s:GPGEditRecipients() silent put ='GPG: Please edit the list of recipients, one recipient per line' silent put ='GPG: Unknown recipients have a prepended \"!\"' silent put ='GPG: Lines beginning with \"GPG:\" are removed automatically' - silent put ='GPG: Use :x or :bd to close this buffer' + silent put ='GPG: Closing this buffer commits changes' silent put ='GPG: ----------------------------------------------------------------------' " put the recipients in the scratch buffer @@ -456,11 +463,6 @@ fun s:GPGEditRecipients() " jump to the first recipient silent normal! G - " add a autocommand to regenerate the recipients after a write - augroup GPGEditRecipients - augroup END - execute 'au GPGEditRecipients BufHidden ' . editbuffername . ' call s:GPGFinishRecipientsBuffer()' - endi endf @@ -481,8 +483,7 @@ fun s:GPGFinishRecipientsBuffer() let GPGUnknownRecipients="" " delete the autocommand - exe "au! GPGEditRecipients * " . escape(bufname("%"), ' *?\"'."'") - + autocmd! * let currentline=1 let recipient=getline(currentline) @@ -570,14 +571,25 @@ fun s:GPGEditOptions() if (match(bufname("%"), "^\\(GPGRecipients_\\|GPGOptions_\\)") != 0 && match(bufname("%"), "\.\\(gpg\\|asc\\|pgp\\)$") >= 0) " save buffer name - let buffername=escape(bufname("%"), ' *?\"'."'") + let buffername=bufname("%") let editbuffername="GPGOptions_" . buffername - " create scratch buffer - exe 'silent! split ' . editbuffername - " check if this buffer exists - if (bufexists(editbuffername)) + if (!bufexists(editbuffername)) + " create scratch buffer + exe 'silent! split ' . escape(editbuffername, ' *?\"'."'") + + " add a autocommand to regenerate the options after a write + autocmd BufHidden,BufUnload call s:GPGFinishOptionsBuffer() + else + if (bufwinnr(editbuffername) >= 0) + " switch to scratch buffer window + exe 'silent! ' . bufwinnr(editbuffername) . "wincmd w" + else + " split scratch buffer window + exe 'silent! sbuffer ' . escape(editbuffername, ' *?\"'."'") + endi + " empty the buffer silent normal! 1GdG endi @@ -600,7 +612,7 @@ fun s:GPGEditOptions() silent put ='GPG: Please edit the list of options, one option per line' silent put ='GPG: Please refer to the gpg documentation for valid options' silent put ='GPG: Lines beginning with \"GPG:\" are removed automatically' - silent put ='GPG: Use :x or :bd to close this buffer' + silent put ='GPG: Closing this buffer commits changes' silent put ='GPG: ----------------------------------------------------------------------' " put the options in the scratch buffer @@ -621,11 +633,6 @@ fun s:GPGEditOptions() " jump to the first option silent normal! G - " add a autocommand to regenerate the options after a write - augroup GPGEditOptions - augroup END - execute 'au GPGEditOptions BufHidden ' . editbuffername . ' call s:GPGFinishOptionsBuffer()' - " define highlight if (has("syntax") && exists("g:syntax_on")) syntax match GPGComment "^GPG:.*$" @@ -652,8 +659,7 @@ fun s:GPGFinishOptionsBuffer() let GPGUnknownOptions="" " delete the autocommand - exe "au! GPGEditOptions * " . escape(bufname("%"), ' *?\"'."'") - + autocmd! * let currentline=1 let option=getline(currentline) From 60b7fb1ede66296a85aef325a3afd7ea0df7ca10 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Thu, 14 Dec 2006 10:51:22 +0000 Subject: [PATCH 017/115] Aotocommand rework Make GPGFinish*() more robust, change to window if closed from another. Also install autocmd in GPGEdit*() if we split an existing buffer. --- plugin/gnupg.vim | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index ed39531..f5f0ef4 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -392,6 +392,9 @@ fun s:GPGEditRecipients() else " split scratch buffer window exe 'silent! sbuffer ' . escape(editbuffername, ' *?\"'."'") + + " add a autocommand to regenerate the recipients after a write + autocmd BufHidden,BufUnload call s:GPGFinishRecipientsBuffer() endi " empty the buffer @@ -478,12 +481,19 @@ fun s:GPGFinishRecipientsBuffer() return endi + " go to buffer before doing work + if (bufnr("%") != expand("")) + " switch to scratch buffer window + exe 'silent! ' . bufwinnr(expand("")) . "wincmd w" + endi + " clear GPGRecipients and GPGUnknownRecipients let GPGRecipients="" let GPGUnknownRecipients="" " delete the autocommand autocmd! * + let currentline=1 let recipient=getline(currentline) @@ -588,6 +598,9 @@ fun s:GPGEditOptions() else " split scratch buffer window exe 'silent! sbuffer ' . escape(editbuffername, ' *?\"'."'") + + " add a autocommand to regenerate the options after a write + autocmd BufHidden,BufUnload call s:GPGFinishOptionsBuffer() endi " empty the buffer @@ -654,12 +667,19 @@ fun s:GPGFinishOptionsBuffer() return endi + " go to buffer before doing work + if (bufnr("%") != expand("")) + " switch to scratch buffer window + exe 'silent! ' . bufwinnr(expand("")) . "wincmd w" + endi + " clear GPGOptions and GPGUnknownOptions let GPGOptions="" let GPGUnknownOptions="" " delete the autocommand autocmd! * + let currentline=1 let option=getline(currentline) From ae76dffaedf0673734198bc918a0a5265ce3316a Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Tue, 5 Jul 2011 08:46:55 +0200 Subject: [PATCH 018/115] Correction in documentation --- plugin/gnupg.vim | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index f5f0ef4..a963553 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -37,7 +37,7 @@ " WARNING: There is no check of the entered options, so you need to know " what you are doing. " -" :GPGViewRecipients +" :GPGViewOptions " Prints the list of options. " " Variables: @@ -48,7 +48,8 @@ " Credits: " Mathieu Clabaut for inspirations through his vimspell.vim script. " Richard Bronosky for patch to enable ".pgp" suffix. -" Erik Remmelzwaal for patch to enable windows support. +" Erik Remmelzwaal for patch to enable windows support and patient beta +" testing. " " Section: Plugin header {{{1 if (exists("loaded_gnupg") || &cp || exists("#BufReadPre#*.\(gpg\|asc\|pgp\)")) From fc7904948162be8a1909f844119b6498f1dc8a3c Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Tue, 5 Jul 2011 08:47:10 +0200 Subject: [PATCH 019/115] Bind highlight to standard highlighting. --- plugin/gnupg.vim | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index a963553..cf31f54 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -90,9 +90,9 @@ autocmd BufWritePost,FileWritePost *.\(gpg\|asc\|pgp\) if (exists("b autocmd BufWritePost,FileWritePost *.\(gpg\|asc\|pgp\) set nobin augroup END " Section: Highlight setup {{{1 -highlight default GPGWarning term=reverse ctermfg=Yellow guifg=Yellow -highlight default GPGError term=reverse ctermfg=Red guifg=Red -highlight default GPGHighlightUnknownRecipient term=reverse ctermfg=Red cterm=underline guifg=Red gui=underline +highlight default link GPGWarning WarningMsg +highlight default link GPGError ErrorMsg +highlight default link GPGHighlightUnknownRecipient ErrorMsg " Section: Functions {{{1 " Function: s:GPGInit() {{{2 " From eb2fb277485063efab94f5129660faf4e2f13217 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Sun, 17 Dec 2006 21:20:33 +0000 Subject: [PATCH 020/115] Added variables g:GPGPreferSymmetric and g:GPGPreferArmor. --- plugin/gnupg.vim | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index cf31f54..5dcf9de 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -45,6 +45,12 @@ " g:GPGUseAgent " If set to 0 a possible available gpg-agent won't be used. Defaults to 1. " +" g:GPGPreferSymmetric +" If set to 1 symmetric encryption is preferred for new files. Defaults to 0. +" +" g:GPGPreferArmor +" If set to 1 armored data is preferred for new files. Defaults to 0. +" " Credits: " Mathieu Clabaut for inspirations through his vimspell.vim script. " Richard Bronosky for patch to enable ".pgp" suffix. @@ -68,8 +74,9 @@ autocmd BufNewFile,BufReadPre,FileReadPre *.\(gpg\|asc\|pgp\) set viminfo= autocmd BufNewFile,BufReadPre,FileReadPre *.\(gpg\|asc\|pgp\) set noswapfile " Initialize the internal variables autocmd BufNewFile,BufReadPre,FileReadPre *.\(gpg\|asc\|pgp\) call s:GPGInit() -" Force the user to edit the recipient list if he opens a new file -autocmd BufNewFile *.\(gpg\|asc\|pgp\) call s:GPGEditRecipients() +" Force the user to edit the recipient list if he opens a new file and public +" keys are preferred +autocmd BufNewFile *.\(gpg\|asc\|pgp\) if (exists("g:GPGPreferSymmetric") && g:GPGPreferSymmetric == 0) | call s:GPGEditRecipients() | endi " Switch to binary mode to read the encrypted file autocmd BufReadPre,FileReadPre *.\(gpg\|asc\|pgp\) set bin autocmd BufReadPost,FileReadPost *.\(gpg\|asc\|pgp\) call s:GPGDecrypt() @@ -104,6 +111,16 @@ fun s:GPGInit() let g:GPGUseAgent = 1 endif + " check if symmetric encryption is preferred + if (!exists("g:GPGPreferSymmetric")) + let g:GPGPreferSymmetric = 0 + endif + + " check if armored files are preferred + if (!exists("g:GPGPreferArmor")) + let g:GPGPreferArmor = 0 + endif + " determine if gnupg can use the gpg-agent if (exists("$GPG_AGENT_INFO") && g:GPGUseAgent == 1) if (!exists("$GPG_TTY")) @@ -255,7 +272,14 @@ fun s:GPGEncrypt() " built list of options if (!exists("b:GPGOptions") || strlen(b:GPGOptions) == 0) - let b:GPGOptions="encrypt:" + if (exists("g:GPGPreferSymmetric") && g:GPGPreferSymmetric == 1) + let b:GPGOptions="symmetric:" + else + let b:GPGOptions="encrypt:" + endi + if (exists("g:GPGPreferArmor") && g:GPGPreferArmor == 1) + let b:GPGOptions=b:GPGOptions . "armor:" + endi endi let field=0 let option=s:GetField(b:GPGOptions, ":", field) From f9a81fc1bdc0df8afdff481822633fad6646f21f Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Thu, 28 Dec 2006 13:05:56 +0000 Subject: [PATCH 021/115] Set LC_ALL also, to ensure english messages of gpg. --- plugin/gnupg.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 5dcf9de..48cfa18 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -147,7 +147,7 @@ fun s:GPGInit() let s:shellredir = &shellredir let s:shell = 'sh' let s:stderrredirnull ='2>/dev/null' - let s:GPGCommand="LANG=C " . s:GPGCommand + let s:GPGCommand="LANG=C LC_ALL=C " . s:GPGCommand endi " find the supported algorithms From 63dff1cd4b5869c65a62c7e95ab616e3908f3859 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Mon, 29 Jan 2007 13:38:16 +0000 Subject: [PATCH 022/115] shellredir wasn't restored. Instead of restore it was set again to s:shellredir --- plugin/gnupg.vim | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 48cfa18..785e8bf 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -154,7 +154,7 @@ fun s:GPGInit() let &shellredir=s:shellredir let &shell=s:shell let output=system(s:GPGCommand . " --version") - let &shellredir=s:shellredir + let &shellredir=s:shellredirsave let &shell=s:shellsave let s:GPGPubkey=substitute(output, ".*Pubkey: \\(.\\{-}\\)\n.*", "\\1", "") @@ -181,7 +181,7 @@ fun s:GPGDecrypt() let &shellredir=s:shellredir let &shell=s:shell let output=system(s:GPGCommand . " --decrypt --dry-run --batch --no-use-agent --logger-fd 1 \"" . filename . "\"") - let &shellredir=s:shellredir + let &shellredir=s:shellredirsave let &shell=s:shellsave " check if the file is symmetric/asymmetric encrypted @@ -241,7 +241,7 @@ fun s:GPGDecrypt() let &shellredir=s:shellredir let &shell=s:shell exec "'[,']!" . s:GPGCommand . " --quiet --decrypt " . s:stderrredirnull - let &shellredir=s:shellredir + let &shellredir=s:shellredirsave let &shell=s:shellsave if (v:shell_error) " message could not be decrypted silent u @@ -321,7 +321,7 @@ fun s:GPGEncrypt() let &shellredir=s:shellredir let &shell=s:shell silent exec "'[,']!" . s:GPGCommand . " --quiet --no-encrypt-to " . options . recipients . " " . s:stderrredirnull - let &shellredir=s:shellredir + let &shellredir=s:shellredirsave let &shell=s:shellsave if (v:shell_error) " message could not be encrypted silent u @@ -741,7 +741,7 @@ fun s:GPGNameToID(name) let &shellredir=s:shellredir let &shell=s:shell let output=system(s:GPGCommand . " --quiet --with-colons --fixed-list-mode --list-keys \"" . a:name . "\"") - let &shellredir=s:shellredir + let &shellredir=s:shellredirsave let &shell=s:shellsave " parse the output of gpg @@ -805,7 +805,7 @@ fun s:GPGIDToName(identity) let &shellredir=s:shellredir let &shell=s:shell let output=system(s:GPGCommand . " --quiet --with-colons --fixed-list-mode --list-keys " . a:identity ) - let &shellredir=s:shellredir + let &shellredir=s:shellredirsave let &shell=s:shellsave " parse the output of gpg From f786031299b48c1c015cd29b1436b72e655ca419 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Thu, 1 Mar 2007 09:58:04 +0000 Subject: [PATCH 023/115] Ignore tty for gvim --- plugin/gnupg.vim | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 785e8bf..7814d3b 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -134,6 +134,13 @@ fun s:GPGInit() let s:GPGCommand="gpg --no-use-agent" endif + " don't use tty in gvim + " FIXME find a better way to avoid an error. + " with this solution only --use-agent will work + if has("gui_running") + let s:GPGCommand=s:GPGCommand . " --no-tty" + endif + " setup shell environment for unix and windows let s:shellredirsave=&shellredir let s:shellsave=&shell From bf868fb9a142c4698b4e6bab2033ca80e9fb0dbe Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Thu, 5 Apr 2007 10:50:38 +0000 Subject: [PATCH 024/115] Use revision number for multiple load guard. --- plugin/gnupg.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 7814d3b..d91defc 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -58,10 +58,10 @@ " testing. " " Section: Plugin header {{{1 -if (exists("loaded_gnupg") || &cp || exists("#BufReadPre#*.\(gpg\|asc\|pgp\)")) +if (exists("g:loaded_gnupg") || &cp || exists("#BufReadPre#*.\(gpg\|asc\|pgp\)")) finish endi -let loaded_gnupg = 1 +let g:loaded_gnupg = "$Revision$" " Section: Autocmd setup {{{1 augroup GnuPG From c069e0f21dcf9ca54c127aba3e6d18ab6b760b89 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Tue, 11 Dec 2007 09:43:38 +0000 Subject: [PATCH 025/115] Modified to work with gpg2 correctly. --- plugin/gnupg.vim | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index d91defc..c0a1f71 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -1,11 +1,11 @@ " Name: gnupg.vim -" Version: $Id$ -" Author: Markus Braun -" Summary: Vim plugin for transparent editing of gpg encrypted files. -" Licence: This program is free software; you can redistribute it and/or -" modify it under the terms of the GNU General Public License. -" See http://www.gnu.org/copyleft/gpl.txt -" Section: Documentation {{{1 +" Version: $Id$ +" Author: Markus Braun +" Summary: Vim plugin for transparent editing of gpg encrypted files. +" Licence: This program is free software; you can redistribute it and/or +" modify it under the terms of the GNU General Public License. +" See http://www.gnu.org/copyleft/gpl.txt +" Section: Documentation {{{1 " Description: " " This script implements transparent editing of gpg encrypted files. The @@ -56,6 +56,7 @@ " Richard Bronosky for patch to enable ".pgp" suffix. " Erik Remmelzwaal for patch to enable windows support and patient beta " testing. +" Lars Becker for patch to make gpg2 working. " " Section: Plugin header {{{1 if (exists("g:loaded_gnupg") || &cp || exists("#BufReadPre#*.\(gpg\|asc\|pgp\)")) @@ -187,12 +188,12 @@ fun s:GPGDecrypt() " find the recipients of the file let &shellredir=s:shellredir let &shell=s:shell - let output=system(s:GPGCommand . " --decrypt --dry-run --batch --no-use-agent --logger-fd 1 \"" . filename . "\"") + let output=system(s:GPGCommand . " --verbose --decrypt --dry-run --batch --no-use-agent --passphrase \"ThisIsHopefullyNotThePassphraseOfAnyone\" --logger-fd 1 \"" . filename . "\"") let &shellredir=s:shellredirsave let &shell=s:shellsave " check if the file is symmetric/asymmetric encrypted - if (match(output, "gpg: [^ ]\\+ encrypted data") >= 0) + if (match(output, "gpg: encrypted with [[:digit:]]\\+ passphrase") >= 0) " file is symmetric encrypted let b:GPGEncrypted=1 @@ -207,7 +208,7 @@ fun s:GPGDecrypt() echo echohl None endi - elseif (match(output, "gpg: public key decryption") >= 0) + elseif (match(output, "gpg: public key is [[:xdigit:]]\\{8}") >= 0) " file is asymmetric encrypted let b:GPGEncrypted=1 @@ -228,7 +229,7 @@ fun s:GPGDecrypt() end let start=match(output, "ID [[:xdigit:]]\\{8}", start) endw - elseif (match(output, "gpg: no valid OpenPGP data found") >= 0) + else " file is not encrypted let b:GPGEncrypted=0 echohl GPGWarning From f5e107f596a5360231e32fd165ed70555c52b32e Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Tue, 5 Jul 2011 08:51:02 +0200 Subject: [PATCH 026/115] Added a debug command and some debug messages --- plugin/gnupg.vim | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index c0a1f71..c5dece7 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -122,6 +122,11 @@ fun s:GPGInit() let g:GPGPreferArmor = 0 endif + " check if debugging is turned on + if (!exists("g:GPGDebugLevel")) + let g:GPGDebugLevel = 0 + endif + " determine if gnupg can use the gpg-agent if (exists("$GPG_AGENT_INFO") && g:GPGUseAgent == 1) if (!exists("$GPG_TTY")) @@ -191,17 +196,21 @@ fun s:GPGDecrypt() let output=system(s:GPGCommand . " --verbose --decrypt --dry-run --batch --no-use-agent --passphrase \"ThisIsHopefullyNotThePassphraseOfAnyone\" --logger-fd 1 \"" . filename . "\"") let &shellredir=s:shellredirsave let &shell=s:shellsave + call s:GPGDebug(1, "output of command '" . s:GPGCommand . " --verbose --decrypt --list-only --dry-run --batch --no-use-agent --logger-fd 1 \"" . filename . "\"' is:") + call s:GPGDebug(1, ">>>>> " . output . " <<<<<") " check if the file is symmetric/asymmetric encrypted if (match(output, "gpg: encrypted with [[:digit:]]\\+ passphrase") >= 0) " file is symmetric encrypted let b:GPGEncrypted=1 + call s:GPGDebug(1, "this file is symmetric encrypted") let b:GPGOptions=b:GPGOptions . "symmetric:" let cipher=substitute(output, ".*gpg: \\([^ ]\\+\\) encrypted data.*", "\\1", "") if (match(s:GPGCipher, "\\<" . cipher . "\\>") >= 0) let b:GPGOptions=b:GPGOptions . "cipher-algo " . cipher . ":" + call s:GPGDebug(1, "cipher-algo is " . cipher) else echohl GPGWarning echo "The cipher " . cipher . " is not known by the local gpg command. Using default!" @@ -211,27 +220,31 @@ fun s:GPGDecrypt() elseif (match(output, "gpg: public key is [[:xdigit:]]\\{8}") >= 0) " file is asymmetric encrypted let b:GPGEncrypted=1 + call s:GPGDebug(1, "this file is asymmetric encrypted") let b:GPGOptions=b:GPGOptions . "encrypt:" - let start=match(output, "ID [[:xdigit:]]\\{8}") + let start=match(output, "gpg: public key is [[:xdigit:]]\\{8}") while (start >= 0) - let start=start+3 + let start=start + strlen("gpg: public key is ") let recipient=strpart(output, start, 8) + call s:GPGDebug(1, "recipient is " . recipient) let name=s:GPGNameToID(recipient) if (strlen(name) > 0) let b:GPGRecipients=b:GPGRecipients . name . ":" + call s:GPGDebug(1, "name of recipient is " . name) else let b:GPGUnknownRecipients=b:GPGUnknownRecipients . recipient . ":" echohl GPGWarning echo "The recipient " . recipient . " is not in your public keyring!" echohl None end - let start=match(output, "ID [[:xdigit:]]\\{8}", start) + let start=match(output, "gpg: public key is [[:xdigit:]]\\{8}", start) endw else " file is not encrypted let b:GPGEncrypted=0 + call s:GPGDebug(1, "this file is not encrypted") echohl GPGWarning echo "File is not encrypted, all GPG functions disabled!" echohl None @@ -239,7 +252,8 @@ fun s:GPGDecrypt() endi " check if the message is armored - if (stridx(getline(1), "-----BEGIN PGP MESSAGE-----") >= 0) + if (match(output, "gpg: armor header") >= 0) + call s:GPGDebug(1, "this file is armored") let b:GPGOptions=b:GPGOptions . "armor:" endi @@ -288,6 +302,7 @@ fun s:GPGEncrypt() if (exists("g:GPGPreferArmor") && g:GPGPreferArmor == 1) let b:GPGOptions=b:GPGOptions . "armor:" endi + call s:GPGDebug(1, "no options set, so using default options: " . b:GPGOptions) endi let field=0 let option=s:GetField(b:GPGOptions, ":", field) @@ -304,10 +319,12 @@ fun s:GPGEncrypt() echo "Please use GPGEditRecipients to correct!!" echo echohl None + call s:GPGDebug(1, "unknown recipients are: " . b:GPGUnknownRecipients) endi " built list of recipients if (exists("b:GPGRecipients") && strlen(b:GPGRecipients) > 0) + call s:GPGDebug(1, "recipients are: " . b:GPGRecipients) let field=0 let gpgid=s:GetField(b:GPGRecipients, ":", field) while (strlen(gpgid)) @@ -331,6 +348,7 @@ fun s:GPGEncrypt() silent exec "'[,']!" . s:GPGCommand . " --quiet --no-encrypt-to " . options . recipients . " " . s:stderrredirnull let &shellredir=s:shellredirsave let &shell=s:shellsave + call s:GPGDebug(1, "called gpg command is: " . "'[,']!" . s:GPGCommand . " --quiet --no-encrypt-to " . options . recipients . " " . s:stderrredirnull) if (v:shell_error) " message could not be encrypted silent u echohl GPGError @@ -340,7 +358,7 @@ fun s:GPGEncrypt() return endi - "redraw! + redraw! endf " Function: s:GPGViewRecipients() {{{2 @@ -871,6 +889,16 @@ fun s:GetField(line, separator, field) return "" endi endf + +" Function: s:GPGDebug(level, text) {{{2 +" +" output debug message, if this message has high enough importance +fun s:GPGDebug(level, text) + if (g:GPGDebugLevel >= a:level) + echom a:text + endi +endf + " Section: Command definitions {{{1 com! GPGViewRecipients call s:GPGViewRecipients() com! GPGEditRecipients call s:GPGEditRecipients() From 780d1ed91e30a861d11a29051d84a97307a5d222 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Tue, 5 Jul 2011 08:51:27 +0200 Subject: [PATCH 027/115] Make use of "--list-only" option. --- plugin/gnupg.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index c5dece7..22c27af 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -193,7 +193,7 @@ fun s:GPGDecrypt() " find the recipients of the file let &shellredir=s:shellredir let &shell=s:shell - let output=system(s:GPGCommand . " --verbose --decrypt --dry-run --batch --no-use-agent --passphrase \"ThisIsHopefullyNotThePassphraseOfAnyone\" --logger-fd 1 \"" . filename . "\"") + let output=system(s:GPGCommand . " --verbose --decrypt --list-only --dry-run --batch --no-use-agent --logger-fd 1 \"" . filename . "\"") let &shellredir=s:shellredirsave let &shell=s:shellsave call s:GPGDebug(1, "output of command '" . s:GPGCommand . " --verbose --decrypt --list-only --dry-run --batch --no-use-agent --logger-fd 1 \"" . filename . "\"' is:") From 5aabc38ec2f1c70fff93d7c3698f19fcbdf8b8dc Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Tue, 5 Jul 2011 08:55:50 +0200 Subject: [PATCH 028/115] Restore cursor position after write. --- plugin/gnupg.vim | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 22c27af..aee14ad 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -5,7 +5,7 @@ " Licence: This program is free software; you can redistribute it and/or " modify it under the terms of the GNU General Public License. " See http://www.gnu.org/copyleft/gpl.txt -" Section: Documentation {{{1 +" Section: Documentation {{{1 " Description: " " This script implements transparent editing of gpg encrypted files. The @@ -280,6 +280,13 @@ endf " encrypts the buffer to all previous recipients " fun s:GPGEncrypt() + " save cursor position + let s:GPGCursorPosition = getpos(".") + call s:GPGDebug(2, "saved cursor position " . string(s:GPGCursorPosition)) + + " switch buffer to binary mode + set bin + " guard for unencrypted files if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) echohl GPGWarning @@ -358,6 +365,29 @@ fun s:GPGEncrypt() return endi +endf + +" Function: s:GPGEncryptPost() {{{2 +" +" undo changes don by encrypt, after writing +" +fun s:GPGEncryptPost() + + if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) + return + endi + + " undo encryption of buffer content + silent u + + " switch back from binary mode + set nobin + + " restore cursor position + call setpos('.', s:GPGCursorPosition) + call s:GPGDebug(2, "restored cursor position " . string(s:GPGCursorPosition)) + + " refresh screen redraw! endf From acdf9e096d0a6dc91bfaa499e9c20fe1525b0ec5 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Tue, 5 Jul 2011 08:56:18 +0200 Subject: [PATCH 029/115] Restructured autocommand triggers, merged in functions. --- plugin/gnupg.vim | 56 ++++++++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index aee14ad..eb3ecf4 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -66,47 +66,42 @@ let g:loaded_gnupg = "$Revision$" " Section: Autocmd setup {{{1 augroup GnuPG -au! +autocmd! -" First make sure nothing is written to ~/.viminfo while editing -" an encrypted file. -autocmd BufNewFile,BufReadPre,FileReadPre *.\(gpg\|asc\|pgp\) set viminfo= -" We don't want a swap file, as it writes unencrypted data to disk -autocmd BufNewFile,BufReadPre,FileReadPre *.\(gpg\|asc\|pgp\) set noswapfile -" Initialize the internal variables +" initialize the internal variables autocmd BufNewFile,BufReadPre,FileReadPre *.\(gpg\|asc\|pgp\) call s:GPGInit() -" Force the user to edit the recipient list if he opens a new file and public +" force the user to edit the recipient list if he opens a new file and public " keys are preferred autocmd BufNewFile *.\(gpg\|asc\|pgp\) if (exists("g:GPGPreferSymmetric") && g:GPGPreferSymmetric == 0) | call s:GPGEditRecipients() | endi -" Switch to binary mode to read the encrypted file -autocmd BufReadPre,FileReadPre *.\(gpg\|asc\|pgp\) set bin +" do the decryption autocmd BufReadPost,FileReadPost *.\(gpg\|asc\|pgp\) call s:GPGDecrypt() -" Switch to normal mode for editing -autocmd BufReadPost,FileReadPost *.\(gpg\|asc\|pgp\) set nobin -" Call the autocommand for the file minus .gpg$ -autocmd BufReadPost,FileReadPost *.\(gpg\|asc\|pgp\) execute ":doautocmd BufReadPost " . escape(expand("%:r"), ' *?\"'."'") -autocmd BufReadPost,FileReadPost *.\(gpg\|asc\|pgp\) execute ":redraw!" -" Switch to binary mode before encrypt the file -autocmd BufWritePre,FileWritePre *.\(gpg\|asc\|pgp\) set bin -" Convert all text to encrypted text before writing +" convert all text to encrypted text before writing autocmd BufWritePre,FileWritePre *.\(gpg\|asc\|pgp\) call s:GPGEncrypt() -" Undo the encryption so we are back in the normal text, directly +" undo the encryption so we are back in the normal text, directly " after the file has been written. -autocmd BufWritePost,FileWritePost *.\(gpg\|asc\|pgp\) if (exists("b:GPGEncrypted") && b:GPGEncrypted == 1) | silent u | endi -" Switch back to normal mode for editing -autocmd BufWritePost,FileWritePost *.\(gpg\|asc\|pgp\) set nobin +autocmd BufWritePost,FileWritePost *.\(gpg\|asc\|pgp\) call s:GPGEncryptPost() + augroup END + " Section: Highlight setup {{{1 highlight default link GPGWarning WarningMsg highlight default link GPGError ErrorMsg highlight default link GPGHighlightUnknownRecipient ErrorMsg + " Section: Functions {{{1 " Function: s:GPGInit() {{{2 " " initialize the plugin " fun s:GPGInit() + " first make sure nothing is written to ~/.viminfo while editing + " an encrypted file. + set viminfo= + + " we don't want a swap file, as it writes unencrypted data to disk + set noswapfile + " check if gpg-agent is allowed if (!exists("g:GPGUseAgent")) let g:GPGUseAgent = 1 @@ -181,6 +176,9 @@ endf " decrypt the buffer and find all recipients of the encrypted file " fun s:GPGDecrypt() + " switch to binary mode to read the encrypted file + set bin + " get the filename of the current buffer let filename=escape(expand("%:p"), '\"') @@ -248,6 +246,7 @@ fun s:GPGDecrypt() echohl GPGWarning echo "File is not encrypted, all GPG functions disabled!" echohl None + set nobin return endi @@ -271,8 +270,19 @@ fun s:GPGDecrypt() let asd=input("Message could not be decrypted! (Press ENTER)") echohl None bwipeout + set nobin return endi + + " turn off binary mode + set nobin + + " call the autocommand for the file minus .gpg$ + execute ":doautocmd BufReadPost " . escape(expand("%:r"), ' *?\"'."'") + call s:GPGDebug(2, "called autocommand for " . escape(expand("%:r"), ' *?\"'."'")) + + " refresh screen + redraw! endf " Function: s:GPGEncrypt() {{{2 @@ -935,4 +945,4 @@ com! GPGEditRecipients call s:GPGEditRecipients() com! GPGViewOptions call s:GPGViewOptions() com! GPGEditOptions call s:GPGEditOptions() -" vim600: set foldmethod=marker: +" vim600: foldmethod=marker:foldlevel=0 From 479e79d3be0e84fb96f3613e73135de0f6163fda Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Mon, 21 Jan 2008 17:25:48 +0000 Subject: [PATCH 030/115] Print plugin version in verbose mode --- plugin/gnupg.vim | 3 +++ 1 file changed, 3 insertions(+) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index eb3ecf4..a3d024d 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -95,6 +95,9 @@ highlight default link GPGHighlightUnknownRecipient ErrorMsg " initialize the plugin " fun s:GPGInit() + " print version + call s:GPGDebug(1, "gnupg.vim ". g:loaded_gnupg) + " first make sure nothing is written to ~/.viminfo while editing " an encrypted file. set viminfo= From 874bd187f288fc8d954c1e799e29cd9123dd7402 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Wed, 23 Jan 2008 08:58:02 +0000 Subject: [PATCH 031/115] Made gnupg.vim hopefully encoding safe, needs testing. --- plugin/gnupg.vim | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index a3d024d..3910e47 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -297,6 +297,16 @@ fun s:GPGEncrypt() let s:GPGCursorPosition = getpos(".") call s:GPGDebug(2, "saved cursor position " . string(s:GPGCursorPosition)) + " store encoding and switch to a safe one + if &fileencoding != &encoding + let s:GPGEncoding = &encoding + let &encoding = &fileencoding + call s:GPGDebug(2, "encoding was \"" . s:GPGEncoding . "\", switched to \"" . &encoding . "\"") + else + let s:GPGEncoding = "" + call s:GPGDebug(2, "encoding and fileencoding are the same (\"" . &encoding . "\"), not switching") + endi + " switch buffer to binary mode set bin @@ -396,6 +406,12 @@ fun s:GPGEncryptPost() " switch back from binary mode set nobin + " restore encoding + if s:GPGEncoding != "" + let &encoding = s:GPGEncoding + call s:GPGDebug(2, "restored encoding \"" . &encoding . "\"") + endi + " restore cursor position call setpos('.', s:GPGCursorPosition) call s:GPGDebug(2, "restored cursor position " . string(s:GPGCursorPosition)) From bfdc09a732153817f0b5850093a5a082cd922e46 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Wed, 23 Jan 2008 09:00:51 +0000 Subject: [PATCH 032/115] Print debug message only after debug level had been defined. --- plugin/gnupg.vim | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 3910e47..fe7205b 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -95,9 +95,6 @@ highlight default link GPGHighlightUnknownRecipient ErrorMsg " initialize the plugin " fun s:GPGInit() - " print version - call s:GPGDebug(1, "gnupg.vim ". g:loaded_gnupg) - " first make sure nothing is written to ~/.viminfo while editing " an encrypted file. set viminfo= @@ -124,6 +121,9 @@ fun s:GPGInit() if (!exists("g:GPGDebugLevel")) let g:GPGDebugLevel = 0 endif + + " print version + call s:GPGDebug(1, "gnupg.vim ". g:loaded_gnupg) " determine if gnupg can use the gpg-agent if (exists("$GPG_AGENT_INFO") && g:GPGUseAgent == 1) From fceb8855f9a83362132dc196642446fa7c55c940 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Wed, 23 Jan 2008 09:49:33 +0000 Subject: [PATCH 033/115] Save/restore view of saved window instead of cursor position. --- plugin/gnupg.vim | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index fe7205b..5d66d50 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -293,9 +293,9 @@ endf " encrypts the buffer to all previous recipients " fun s:GPGEncrypt() - " save cursor position - let s:GPGCursorPosition = getpos(".") - call s:GPGDebug(2, "saved cursor position " . string(s:GPGCursorPosition)) + " save window view + let s:GPGWindowView = winsaveview() + call s:GPGDebug(2, "saved window view " . string(s:GPGWindowView)) " store encoding and switch to a safe one if &fileencoding != &encoding @@ -412,9 +412,9 @@ fun s:GPGEncryptPost() call s:GPGDebug(2, "restored encoding \"" . &encoding . "\"") endi - " restore cursor position - call setpos('.', s:GPGCursorPosition) - call s:GPGDebug(2, "restored cursor position " . string(s:GPGCursorPosition)) + " restore window view + call winrestview(s:GPGWindowView) + call s:GPGDebug(2, "restored window view" . string(s:GPGWindowView)) " refresh screen redraw! From ac6ec713e68c45c830371c8669f9df968e26a2af Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Tue, 5 Jul 2011 08:57:30 +0200 Subject: [PATCH 034/115] Added an option to set the gpg executable. --- plugin/gnupg.vim | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 5d66d50..e0b8f51 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -42,6 +42,10 @@ " " Variables: " +" g:GPGExecutable +" If set used as gpg executable, otherwise the system chooses what is run +" when "gpg" is called. Defaults to "gpg". +" " g:GPGUseAgent " If set to 0 a possible available gpg-agent won't be used. Defaults to 1. " @@ -52,11 +56,12 @@ " If set to 1 armored data is preferred for new files. Defaults to 0. " " Credits: -" Mathieu Clabaut for inspirations through his vimspell.vim script. -" Richard Bronosky for patch to enable ".pgp" suffix. -" Erik Remmelzwaal for patch to enable windows support and patient beta +" - Mathieu Clabaut for inspirations through his vimspell.vim script. +" - Richard Bronosky for patch to enable ".pgp" suffix. +" - Erik Remmelzwaal for patch to enable windows support and patient beta " testing. -" Lars Becker for patch to make gpg2 working. +" - Lars Becker for patch to make gpg2 working. +" - Thomas Arendsen Hein for patch to convert encoding of gpg output " " Section: Plugin header {{{1 if (exists("g:loaded_gnupg") || &cp || exists("#BufReadPre#*.\(gpg\|asc\|pgp\)")) @@ -102,6 +107,11 @@ fun s:GPGInit() " we don't want a swap file, as it writes unencrypted data to disk set noswapfile + " check what gpg command to use + if (!exists("g:GPGExecutable")) + let g:GPGExecutable = "gpg" + endif + " check if gpg-agent is allowed if (!exists("g:GPGUseAgent")) let g:GPGUseAgent = 1 @@ -133,9 +143,9 @@ fun s:GPGInit() echo "gpg-agent might not work." echohl None endif - let s:GPGCommand="gpg --use-agent" + let s:GPGCommand=g:GPGExecutable . " --use-agent" else - let s:GPGCommand="gpg --no-use-agent" + let s:GPGCommand=g:GPGExecutable . " --no-use-agent" endif " don't use tty in gvim From b98c3fbd4a37ec366ff35eab4fb41785d63c9407 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Tue, 5 Jul 2011 08:57:47 +0200 Subject: [PATCH 035/115] Convert the output of gpg to the correct encoding. --- plugin/gnupg.vim | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index e0b8f51..bcc94a6 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -839,6 +839,12 @@ fun s:GPGNameToID(name) let &shellredir=s:shellredirsave let &shell=s:shellsave + " when called with "--with-colons" gpg encodes its output _ALWAYS_ as UTF-8, + " so convert it, if necessary + if &encoding != "utf-8" + let output=iconv(output, "utf-8", &encoding) + endi + " parse the output of gpg let pub_seen=0 let uid_seen=0 @@ -903,6 +909,12 @@ fun s:GPGIDToName(identity) let &shellredir=s:shellredirsave let &shell=s:shellsave + " when called with "--with-colons" gpg encodes its output _ALWAYS_ as UTF-8, + " so convert it, if necessary + if &encoding != "utf-8" + let output=iconv(output, "utf-8", &encoding) + endi + " parse the output of gpg let pub_seen=0 let finish=0 From de5ea9f6bd32dcdc5509a7f21b941b4f194a90b5 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Thu, 24 Jan 2008 13:39:05 +0000 Subject: [PATCH 036/115] New commands ":GPGRecipients" and ":GPGOptions" Aliases for ":GPGEditRecipients" and ":GPGEditOptions" for better tab completion. --- plugin/gnupg.vim | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index bcc94a6..3503111 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -23,6 +23,7 @@ " " Commands: " +" :GPGRecipients " :GPGEditRecipients " Opens a scratch buffer to change the list of recipients. Recipients that " are unknown (not in your public key) are highlighted and have @@ -31,6 +32,7 @@ " :GPGViewRecipients " Prints the list of recipients. " +" :GPGOptions " :GPGEditOptions " Opens a scratch buffer to change the options for encryption (symmetric, " asymmetric, signing). Closing the buffer makes the changes permanent. @@ -982,8 +984,10 @@ endf " Section: Command definitions {{{1 com! GPGViewRecipients call s:GPGViewRecipients() +com! GPGRecipients call s:GPGEditRecipients() com! GPGEditRecipients call s:GPGEditRecipients() com! GPGViewOptions call s:GPGViewOptions() +com! GPGOptions call s:GPGEditOptions() com! GPGEditOptions call s:GPGEditOptions() " vim600: foldmethod=marker:foldlevel=0 From 6a32435246e461fc9f9ac22736a9cbefc1444b01 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Thu, 24 Jan 2008 14:01:31 +0000 Subject: [PATCH 037/115] Revert last commit. --- plugin/gnupg.vim | 4 ---- 1 file changed, 4 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 3503111..bcc94a6 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -23,7 +23,6 @@ " " Commands: " -" :GPGRecipients " :GPGEditRecipients " Opens a scratch buffer to change the list of recipients. Recipients that " are unknown (not in your public key) are highlighted and have @@ -32,7 +31,6 @@ " :GPGViewRecipients " Prints the list of recipients. " -" :GPGOptions " :GPGEditOptions " Opens a scratch buffer to change the options for encryption (symmetric, " asymmetric, signing). Closing the buffer makes the changes permanent. @@ -984,10 +982,8 @@ endf " Section: Command definitions {{{1 com! GPGViewRecipients call s:GPGViewRecipients() -com! GPGRecipients call s:GPGEditRecipients() com! GPGEditRecipients call s:GPGEditRecipients() com! GPGViewOptions call s:GPGViewOptions() -com! GPGOptions call s:GPGEditOptions() com! GPGEditOptions call s:GPGEditOptions() " vim600: foldmethod=marker:foldlevel=0 From febf2d4685936289c69372cf3a0ba144ef6b689a Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Mon, 28 Jan 2008 09:21:04 +0000 Subject: [PATCH 038/115] Changed recipient and option buffer handling This allows ':w' to close (besides ':q' and ':x') --- plugin/gnupg.vim | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index bcc94a6..7d076c6 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -504,7 +504,7 @@ fun s:GPGEditRecipients() exe 'silent! split ' . escape(editbuffername, ' *?\"'."'") " add a autocommand to regenerate the recipients after a write - autocmd BufHidden,BufUnload call s:GPGFinishRecipientsBuffer() + autocmd BufHidden,BufUnload,BufWriteCmd call s:GPGFinishRecipientsBuffer() else if (bufwinnr(editbuffername) >= 0) " switch to scratch buffer window @@ -514,7 +514,7 @@ fun s:GPGEditRecipients() exe 'silent! sbuffer ' . escape(editbuffername, ' *?\"'."'") " add a autocommand to regenerate the recipients after a write - autocmd BufHidden,BufUnload call s:GPGFinishRecipientsBuffer() + autocmd BufHidden,BufUnload,BufWriteCmd call s:GPGFinishRecipientsBuffer() endi " empty the buffer @@ -522,7 +522,8 @@ fun s:GPGEditRecipients() endi " Mark the buffer as a scratch buffer - setlocal buftype=nofile + setlocal buftype=acwrite + setlocal bufhidden=hide setlocal noswapfile setlocal nowrap setlocal nobuflisted @@ -655,6 +656,9 @@ fun s:GPGFinishRecipientsBuffer() echo 'There are no known recipients!' echohl None endi + + " reset modified flag + set nomodified endf " Function: s:GPGViewOptions() {{{2 @@ -710,7 +714,7 @@ fun s:GPGEditOptions() exe 'silent! split ' . escape(editbuffername, ' *?\"'."'") " add a autocommand to regenerate the options after a write - autocmd BufHidden,BufUnload call s:GPGFinishOptionsBuffer() + autocmd BufHidden,BufUnload,BufWriteCmd call s:GPGFinishOptionsBuffer() else if (bufwinnr(editbuffername) >= 0) " switch to scratch buffer window @@ -720,7 +724,7 @@ fun s:GPGEditOptions() exe 'silent! sbuffer ' . escape(editbuffername, ' *?\"'."'") " add a autocommand to regenerate the options after a write - autocmd BufHidden,BufUnload call s:GPGFinishOptionsBuffer() + autocmd BufHidden,BufUnload,BufWriteCmd call s:GPGFinishOptionsBuffer() endi " empty the buffer @@ -825,6 +829,8 @@ fun s:GPGFinishOptionsBuffer() call setbufvar(b:corresponding_to, "GPGOptions", GPGOptions) call setbufvar(b:corresponding_to, "&mod", 1) + " reset modified flag + set nomodified endf " Function: s:GPGNameToID(name) {{{2 From 9f4f083367f8e92e03e4b98d0981bf260278507d Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Mon, 28 Jan 2008 09:30:59 +0000 Subject: [PATCH 039/115] Little reindenting. --- plugin/gnupg.vim | 99 ++++++++++++++++++++++++------------------------ 1 file changed, 49 insertions(+), 50 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 7d076c6..f83ee1c 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -71,22 +71,21 @@ let g:loaded_gnupg = "$Revision$" " Section: Autocmd setup {{{1 augroup GnuPG -autocmd! + autocmd! -" initialize the internal variables -autocmd BufNewFile,BufReadPre,FileReadPre *.\(gpg\|asc\|pgp\) call s:GPGInit() -" force the user to edit the recipient list if he opens a new file and public -" keys are preferred -autocmd BufNewFile *.\(gpg\|asc\|pgp\) if (exists("g:GPGPreferSymmetric") && g:GPGPreferSymmetric == 0) | call s:GPGEditRecipients() | endi -" do the decryption -autocmd BufReadPost,FileReadPost *.\(gpg\|asc\|pgp\) call s:GPGDecrypt() - -" convert all text to encrypted text before writing -autocmd BufWritePre,FileWritePre *.\(gpg\|asc\|pgp\) call s:GPGEncrypt() -" undo the encryption so we are back in the normal text, directly -" after the file has been written. -autocmd BufWritePost,FileWritePost *.\(gpg\|asc\|pgp\) call s:GPGEncryptPost() + " initialize the internal variables + autocmd BufNewFile,BufReadPre,FileReadPre *.\(gpg\|asc\|pgp\) call s:GPGInit() + " force the user to edit the recipient list if he opens a new file and public + " keys are preferred + autocmd BufNewFile *.\(gpg\|asc\|pgp\) if (exists("g:GPGPreferSymmetric") && g:GPGPreferSymmetric == 0) | call s:GPGEditRecipients() | endi + " do the decryption + autocmd BufReadPost,FileReadPost *.\(gpg\|asc\|pgp\) call s:GPGDecrypt() + " convert all text to encrypted text before writing + autocmd BufWritePre,FileWritePre *.\(gpg\|asc\|pgp\) call s:GPGEncrypt() + " undo the encryption so we are back in the normal text, directly + " after the file has been written. + autocmd BufWritePost,FileWritePost *.\(gpg\|asc\|pgp\) call s:GPGEncryptPost() augroup END " Section: Highlight setup {{{1 @@ -131,7 +130,7 @@ fun s:GPGInit() if (!exists("g:GPGDebugLevel")) let g:GPGDebugLevel = 0 endif - + " print version call s:GPGDebug(1, "gnupg.vim ". g:loaded_gnupg) @@ -242,13 +241,13 @@ fun s:GPGDecrypt() call s:GPGDebug(1, "recipient is " . recipient) let name=s:GPGNameToID(recipient) if (strlen(name) > 0) - let b:GPGRecipients=b:GPGRecipients . name . ":" + let b:GPGRecipients=b:GPGRecipients . name . ":" call s:GPGDebug(1, "name of recipient is " . name) else - let b:GPGUnknownRecipients=b:GPGUnknownRecipients . recipient . ":" - echohl GPGWarning - echo "The recipient " . recipient . " is not in your public keyring!" - echohl None + let b:GPGUnknownRecipients=b:GPGUnknownRecipients . recipient . ":" + echohl GPGWarning + echo "The recipient " . recipient . " is not in your public keyring!" + echohl None end let start=match(output, "gpg: public key is [[:xdigit:]]\\{8}", start) endw @@ -507,14 +506,14 @@ fun s:GPGEditRecipients() autocmd BufHidden,BufUnload,BufWriteCmd call s:GPGFinishRecipientsBuffer() else if (bufwinnr(editbuffername) >= 0) - " switch to scratch buffer window - exe 'silent! ' . bufwinnr(editbuffername) . "wincmd w" + " switch to scratch buffer window + exe 'silent! ' . bufwinnr(editbuffername) . "wincmd w" else - " split scratch buffer window + " split scratch buffer window exe 'silent! sbuffer ' . escape(editbuffername, ' *?\"'."'") - " add a autocommand to regenerate the recipients after a write - autocmd BufHidden,BufUnload,BufWriteCmd call s:GPGFinishRecipientsBuffer() + " add a autocommand to regenerate the recipients after a write + autocmd BufHidden,BufUnload,BufWriteCmd call s:GPGFinishRecipientsBuffer() endi " empty the buffer @@ -630,12 +629,12 @@ fun s:GPGFinishRecipientsBuffer() if (strlen(recipient) > 0) let gpgid=s:GPGNameToID(recipient) if (strlen(gpgid) > 0) - let GPGRecipients=GPGRecipients . gpgid . ":" + let GPGRecipients=GPGRecipients . gpgid . ":" else - let GPGUnknownRecipients=GPGUnknownRecipients . recipient . ":" - echohl GPGWarning - echo "The recipient " . recipient . " is not in your public keyring!" - echohl None + let GPGUnknownRecipients=GPGUnknownRecipients . recipient . ":" + echohl GPGWarning + echo "The recipient " . recipient . " is not in your public keyring!" + echohl None end endi @@ -717,14 +716,14 @@ fun s:GPGEditOptions() autocmd BufHidden,BufUnload,BufWriteCmd call s:GPGFinishOptionsBuffer() else if (bufwinnr(editbuffername) >= 0) - " switch to scratch buffer window - exe 'silent! ' . bufwinnr(editbuffername) . "wincmd w" + " switch to scratch buffer window + exe 'silent! ' . bufwinnr(editbuffername) . "wincmd w" else - " split scratch buffer window + " split scratch buffer window exe 'silent! sbuffer ' . escape(editbuffername, ' *?\"'."'") - " add a autocommand to regenerate the options after a write - autocmd BufHidden,BufUnload,BufWriteCmd call s:GPGFinishOptionsBuffer() + " add a autocommand to regenerate the options after a write + autocmd BufHidden,BufUnload,BufWriteCmd call s:GPGFinishOptionsBuffer() endi " empty the buffer @@ -863,24 +862,24 @@ fun s:GPGNameToID(name) " search for the next uid if (pub_seen == 1) if (s:GetField(linecontent, ":", 0) == "uid") - if (uid_seen == 0) - let choices=choices . counter . ": " . s:GetField(linecontent, ":", 9) . "\n" - let counter=counter+1 - let uid_seen=1 - else - let choices=choices . " " . s:GetField(linecontent, ":", 9) . "\n" - endi + if (uid_seen == 0) + let choices=choices . counter . ": " . s:GetField(linecontent, ":", 9) . "\n" + let counter=counter+1 + let uid_seen=1 + else + let choices=choices . " " . s:GetField(linecontent, ":", 9) . "\n" + endi else - let uid_seen=0 - let pub_seen=0 + let uid_seen=0 + let pub_seen=0 endi endi " search for the next pub if (pub_seen == 0) if (s:GetField(linecontent, ":", 0) == "pub") - let gpgids=gpgids . s:GetField(linecontent, ":", 4) . ":" - let pub_seen=1 + let gpgids=gpgids . s:GetField(linecontent, ":", 4) . ":" + let pub_seen=1 endi endi @@ -929,13 +928,13 @@ fun s:GPGIDToName(identity) while (strlen(linecontent) && !finish) if (pub_seen == 0) " search for the next pub if (s:GetField(linecontent, ":", 0) == "pub") - let pub_seen=1 + let pub_seen=1 endi else " search for the next uid if (s:GetField(linecontent, ":", 0) == "uid") - let pub_seen=0 - let finish=1 - let uid=s:GetField(linecontent, ":", 9) + let pub_seen=0 + let finish=1 + let uid=s:GetField(linecontent, ":", 9) endi endi From 9cb02c800a3477d71f8d95b7b02fb0e19596f227 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Wed, 13 Feb 2008 06:00:19 +0000 Subject: [PATCH 040/115] Vim version compatibility Wrap winsaveview() and winrestview() with v:version > 700 to make the plugin run under older vim versions. --- plugin/gnupg.vim | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index f83ee1c..43a77b5 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -303,8 +303,10 @@ endf " fun s:GPGEncrypt() " save window view - let s:GPGWindowView = winsaveview() - call s:GPGDebug(2, "saved window view " . string(s:GPGWindowView)) + if v:version >= 700 + let s:GPGWindowView = winsaveview() + call s:GPGDebug(2, "saved window view " . string(s:GPGWindowView)) + endi " store encoding and switch to a safe one if &fileencoding != &encoding @@ -422,8 +424,10 @@ fun s:GPGEncryptPost() endi " restore window view - call winrestview(s:GPGWindowView) - call s:GPGDebug(2, "restored window view" . string(s:GPGWindowView)) + if v:version >= 700 + call winrestview(s:GPGWindowView) + call s:GPGDebug(2, "restored window view" . string(s:GPGWindowView)) + endi " refresh screen redraw! From c7ab1464da57d28e7448b888324931d6881fd277 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Mon, 12 May 2008 09:07:42 +0000 Subject: [PATCH 041/115] Patch from Karl-Heinz Ruskowski. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Here his message: Hallo, uns sind hier zwei Fehler an dem gnupg.vim-script aufgefallen die wir hier erstmal vorsorglich umgangen haben. (Patch hängt an) 1. Wenn wenn Einträge in der in der Empfängerliste waren die einen Trustlevel < 4 hatten frage Gnupg nochmal nach ob denn nun dieser Schlüssel übernommen werden sollte wenn man dies verneinte wurde die Datei dennoch gespeichert, aber unverschlüsselt - was sicher die schlechteste lösung ist. Kleiner auszug aus der Schell darüber: ========================================= pub 1024g/6E4DF128 2006-12-06 Sascha L. Teichmann Haupt-Fingerabdruck = 4F52 9526 6786 0497 4390 676F B2BB CE94 57FC 1337 Unter-Fingerabdruck = AD32 F49A C4AD 7113 CF0D 0303 548A 53F3 6E4D F128 It is NOT certain that the key belongs to the person named in the user ID. If you *really* know what you are doing, you may answer the next question with yes. Use this key anyway? (y/N) --> Vorgabe (also nein) führt zu "Zugang/Zugang.gpg" 0L, 0C geschrieben ========================================= Mittels "--trust-model always" erzwingen wir die zustimung und die Daten bleiben verschlüsselt. 2. Wenn man die Empfänger einer Datei bearbeitet hat und man verlässt den Puffer zur eingabe und nun wird ein eingetragener Schlüssel gelöscht speichert man wieder eine kaputte gpg Datei. Ich habe eine zusätzliche prüfung auf unbekannte Empfänger hinzugefügt und lösche diese Unbekannten Keys vor er verschlüsselung. In beiden fällen war es mir nicht möglich bei einem aufgetretenen Fehler wieder zurück in den Editormodus zu wechseln, was sicher an meinen schlechten Vimscript Kenntnissen liegt. Dies wäre aus meiner Sicht natürlich die beste Lösung. Hoffe ich konnte helfen. Mit freundlichen Grüßen Karl-Heinz Ruskowski -- Karl-Heinz Ruskowski OpenPGP key: FB8DA3BF Intevation GmbH, Osnabrück Amtsgericht Osnabrück, HR B 18998 http://www.intevation.de/ Geschäftsführer: Frank Koormann, Bernhard Reiter, Dr. Jan-Oliver Wagner --- plugin/gnupg.vim | 48 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 43a77b5..80bf4d6 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -108,7 +108,7 @@ fun s:GPGInit() " check what gpg command to use if (!exists("g:GPGExecutable")) - let g:GPGExecutable = "gpg" + let g:GPGExecutable = "gpg --trust-model always" endif " check if gpg-agent is allowed @@ -353,14 +353,56 @@ fun s:GPGEncrypt() let option=s:GetField(b:GPGOptions, ":", field) endw + let GPGUnknownRecipients="" + let field=0 + let cur_recipient="-" + + " Check recipientslist for unknown recipients again + while(strlen(cur_recipient)) + let cur_recipient=s:GetField(b:GPGRecipients, ":", field) + let field=field+1 + + " only do this if the line is not empty + if (strlen(cur_recipient) > 0) + let gpgid=s:GPGNameToID(cur_recipient) + if (strlen(gpgid) <= 0) + let GPGUnknownRecipients=GPGUnknownRecipients . cur_recipient . ":" + echohl GPGWarning + echo "The recipient " . cur_recipient . " is not in your public keyring!" + echohl None + endi + endi + endw + " check if there are unknown recipients and warn - if (exists("b:GPGUnknownRecipients") && strlen(b:GPGUnknownRecipients) > 0) + if(strlen(GPGUnknownRecipients) > 0) echohl GPGWarning echo "There are unknown recipients!!" echo "Please use GPGEditRecipients to correct!!" echo echohl None - call s:GPGDebug(1, "unknown recipients are: " . b:GPGUnknownRecipients) + call s:GPGDebug(1, "unknown recipients are: " . GPGUnknownRecipients) + + " Remove unknown recipients from recipientslist + let unknown_recipients_field=0 + let cur_unknown_recipient="-" + let known_recipients=b:GPGRecipients + while(strlen(cur_unknown_recipient)) + let cur_unknown_recipient=s:GetField(GPGUnknownRecipients, ":", unknown_recipients_field) + + let match_result=match(known_recipients, cur_unknown_recipient.":") + if(match_result > 0 && strlen(cur_unknown_recipient) > 0) + echohl GPGWarning + echo "Removing ". cur_unknown_recipient ." from recipientlist!\n" + echohl None + let Known_Recipients=substitute(known_recipients, cur_unknown_recipient .":", "", "g") + endi + + let unknown_recipients_field=unknown_recipients_field+1 + endw + " Let user know whats happend and copy known_recipients back to buffer + let dummy=input("Press ENTER to quit") + let b:GPGRecipients=known_recipients endi " built list of recipients From cce39d13b2aba0db801340825585b51ab2b3a818 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Mon, 12 May 2008 09:10:33 +0000 Subject: [PATCH 042/115] Give Karl-Heinz Ruskowski his credit. --- plugin/gnupg.vim | 1 + 1 file changed, 1 insertion(+) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 80bf4d6..fbcc0f8 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -62,6 +62,7 @@ " testing. " - Lars Becker for patch to make gpg2 working. " - Thomas Arendsen Hein for patch to convert encoding of gpg output +" - Karl-Heinz Ruskowski for patch to fix unknown recipients and trust model " " Section: Plugin header {{{1 if (exists("g:loaded_gnupg") || &cp || exists("#BufReadPre#*.\(gpg\|asc\|pgp\)")) From 6fdd1667ec0a943d2613bda552ad9f2705cfdb58 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Mon, 12 May 2008 09:18:49 +0000 Subject: [PATCH 043/115] Patch by Giel van Schijndel to get GPG_TTY dynamically: Hi, I'm using your gnupg Vim script [1] and found a minor problem involving GPG_TTY not being set, which is no problem on my system because I use pinentry-gtk to get passphrases with. Secondly it is possible to use whatever tty is current by using the output from the `tty' command. Thus I have made a patch that addresses this problem. See attached. [1] http://www.vim.org/scripts/script.php?script_id=661 -- Giel --- plugin/gnupg.vim | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index fbcc0f8..8916994 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -63,6 +63,7 @@ " - Lars Becker for patch to make gpg2 working. " - Thomas Arendsen Hein for patch to convert encoding of gpg output " - Karl-Heinz Ruskowski for patch to fix unknown recipients and trust model +" - Giel van Schijndel for patch to get GPG_TTY dynamically. " " Section: Plugin header {{{1 if (exists("g:loaded_gnupg") || &cp || exists("#BufReadPre#*.\(gpg\|asc\|pgp\)")) @@ -137,11 +138,15 @@ fun s:GPGInit() " determine if gnupg can use the gpg-agent if (exists("$GPG_AGENT_INFO") && g:GPGUseAgent == 1) - if (!exists("$GPG_TTY")) - echohl GPGError - echo "The GPG_TTY is not set!" - echo "gpg-agent might not work." - echohl None + if (!exists("$GPG_TTY") && !has("gui_running")) + let $GPG_TTY = system("tty") + if (v:shell_error) + let $GPG_TTY = "" + echohl GPGError + echo "The GPG_TTY is not set and no TTY could be found using the `tty` command!" + echo "gpg-agent might not work." + echohl None + endif endif let s:GPGCommand=g:GPGExecutable . " --use-agent" else From 921d99fd8890e646dc88646bef580bdae76d7059 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Mon, 12 May 2008 09:39:55 +0000 Subject: [PATCH 044/115] Use echom instead of plain echo This way warnings and errors are stored in message list. --- plugin/gnupg.vim | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 8916994..b1bc0c8 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -143,8 +143,8 @@ fun s:GPGInit() if (v:shell_error) let $GPG_TTY = "" echohl GPGError - echo "The GPG_TTY is not set and no TTY could be found using the `tty` command!" - echo "gpg-agent might not work." + echom "The GPG_TTY is not set and no TTY could be found using the `tty` command!" + echom "gpg-agent might not work." echohl None endif endif @@ -229,7 +229,7 @@ fun s:GPGDecrypt() call s:GPGDebug(1, "cipher-algo is " . cipher) else echohl GPGWarning - echo "The cipher " . cipher . " is not known by the local gpg command. Using default!" + echom "The cipher " . cipher . " is not known by the local gpg command. Using default!" echo echohl None endi @@ -252,7 +252,7 @@ fun s:GPGDecrypt() else let b:GPGUnknownRecipients=b:GPGUnknownRecipients . recipient . ":" echohl GPGWarning - echo "The recipient " . recipient . " is not in your public keyring!" + echom "The recipient " . recipient . " is not in your public keyring!" echohl None end let start=match(output, "gpg: public key is [[:xdigit:]]\\{8}", start) @@ -262,7 +262,7 @@ fun s:GPGDecrypt() let b:GPGEncrypted=0 call s:GPGDebug(1, "this file is not encrypted") echohl GPGWarning - echo "File is not encrypted, all GPG functions disabled!" + echom "File is not encrypted, all GPG functions disabled!" echohl None set nobin return @@ -330,7 +330,7 @@ fun s:GPGEncrypt() " guard for unencrypted files if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) echohl GPGWarning - echo "File is not encrypted, all GPG functions disabled!" + echom "File is not encrypted, all GPG functions disabled!" echohl None return endi @@ -374,7 +374,7 @@ fun s:GPGEncrypt() if (strlen(gpgid) <= 0) let GPGUnknownRecipients=GPGUnknownRecipients . cur_recipient . ":" echohl GPGWarning - echo "The recipient " . cur_recipient . " is not in your public keyring!" + echom "The recipient " . cur_recipient . " is not in your public keyring!" echohl None endi endi @@ -383,8 +383,8 @@ fun s:GPGEncrypt() " check if there are unknown recipients and warn if(strlen(GPGUnknownRecipients) > 0) echohl GPGWarning - echo "There are unknown recipients!!" - echo "Please use GPGEditRecipients to correct!!" + echom "There are unknown recipients!!" + echom "Please use GPGEditRecipients to correct!!" echo echohl None call s:GPGDebug(1, "unknown recipients are: " . GPGUnknownRecipients) @@ -399,7 +399,7 @@ fun s:GPGEncrypt() let match_result=match(known_recipients, cur_unknown_recipient.":") if(match_result > 0 && strlen(cur_unknown_recipient) > 0) echohl GPGWarning - echo "Removing ". cur_unknown_recipient ." from recipientlist!\n" + echom "Removing ". cur_unknown_recipient ." from recipientlist!\n" echohl None let Known_Recipients=substitute(known_recipients, cur_unknown_recipient .":", "", "g") endi @@ -424,8 +424,8 @@ fun s:GPGEncrypt() else if (match(b:GPGOptions, "encrypt:") >= 0) echohl GPGError - echo "There are no recipients!!" - echo "Please use GPGEditRecipients to correct!!" + echom "There are no recipients!!" + echom "Please use GPGEditRecipients to correct!!" echo echohl None endi @@ -489,7 +489,7 @@ fun s:GPGViewRecipients() " guard for unencrypted files if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) echohl GPGWarning - echo "File is not encrypted, all GPG functions disabled!" + echom "File is not encrypted, all GPG functions disabled!" echohl None return endi @@ -523,7 +523,7 @@ fun s:GPGViewRecipients() " check if there is any known recipient if (strlen(s:GetField(b:GPGRecipients, ":", 0)) == 0) echohl GPGError - echo 'There are no known recipients!' + echom 'There are no known recipients!' echohl None endi endi @@ -537,7 +537,7 @@ fun s:GPGEditRecipients() " guard for unencrypted files if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) echohl GPGWarning - echo "File is not encrypted, all GPG functions disabled!" + echom "File is not encrypted, all GPG functions disabled!" echohl None return endi @@ -648,7 +648,7 @@ fun s:GPGFinishRecipientsBuffer() " guard for unencrypted files if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) echohl GPGWarning - echo "File is not encrypted, all GPG functions disabled!" + echom "File is not encrypted, all GPG functions disabled!" echohl None return endi @@ -685,7 +685,7 @@ fun s:GPGFinishRecipientsBuffer() else let GPGUnknownRecipients=GPGUnknownRecipients . recipient . ":" echohl GPGWarning - echo "The recipient " . recipient . " is not in your public keyring!" + echom "The recipient " . recipient . " is not in your public keyring!" echohl None end endi @@ -704,7 +704,7 @@ fun s:GPGFinishRecipientsBuffer() " check if there is any known recipient if (strlen(s:GetField(GPGRecipients, ":", 0)) == 0) echohl GPGError - echo 'There are no known recipients!' + echom 'There are no known recipients!' echohl None endi @@ -720,7 +720,7 @@ fun s:GPGViewOptions() " guard for unencrypted files if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) echohl GPGWarning - echo "File is not encrypted, all GPG functions disabled!" + echom "File is not encrypted, all GPG functions disabled!" echohl None return endi @@ -747,7 +747,7 @@ fun s:GPGEditOptions() " guard for unencrypted files if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) echohl GPGWarning - echo "File is not encrypted, all GPG functions disabled!" + echom "File is not encrypted, all GPG functions disabled!" echohl None return endi @@ -837,7 +837,7 @@ fun s:GPGFinishOptionsBuffer() " guard for unencrypted files if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) echohl GPGWarning - echo "File is not encrypted, all GPG functions disabled!" + echom "File is not encrypted, all GPG functions disabled!" echohl None return endi From 96f3901aa0de4561a6f13548f710ab1afd38b7a2 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Mon, 28 Jul 2008 21:01:41 +0000 Subject: [PATCH 045/115] Conversion from self implemented "list" to vim builtin lists. --- plugin/gnupg.vim | 265 ++++++++++++++++------------------------------- 1 file changed, 91 insertions(+), 174 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index b1bc0c8..554b706 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -66,9 +66,15 @@ " - Giel van Schijndel for patch to get GPG_TTY dynamically. " " Section: Plugin header {{{1 +if v:version < 700 + echohl ErrorMsg | echo 'plugin gnupg.vim requires Vim version >= 7' | echohl None + finish +endif + if (exists("g:loaded_gnupg") || &cp || exists("#BufReadPre#*.\(gpg\|asc\|pgp\)")) finish endi + let g:loaded_gnupg = "$Revision$" " Section: Autocmd setup {{{1 @@ -202,9 +208,9 @@ fun s:GPGDecrypt() " clear GPGEncrypted, GPGRecipients, GPGUnknownRecipients and GPGOptions let b:GPGEncrypted=0 - let b:GPGRecipients="" - let b:GPGUnknownRecipients="" - let b:GPGOptions="" + let b:GPGRecipients=[] + let b:GPGUnknownRecipients=[] + let b:GPGOptions=[] " find the recipients of the file let &shellredir=s:shellredir @@ -221,11 +227,11 @@ fun s:GPGDecrypt() let b:GPGEncrypted=1 call s:GPGDebug(1, "this file is symmetric encrypted") - let b:GPGOptions=b:GPGOptions . "symmetric:" + let b:GPGOptions+=["symmetric"] let cipher=substitute(output, ".*gpg: \\([^ ]\\+\\) encrypted data.*", "\\1", "") if (match(s:GPGCipher, "\\<" . cipher . "\\>") >= 0) - let b:GPGOptions=b:GPGOptions . "cipher-algo " . cipher . ":" + let b:GPGOptions+=["cipher-algo " . cipher] call s:GPGDebug(1, "cipher-algo is " . cipher) else echohl GPGWarning @@ -238,7 +244,7 @@ fun s:GPGDecrypt() let b:GPGEncrypted=1 call s:GPGDebug(1, "this file is asymmetric encrypted") - let b:GPGOptions=b:GPGOptions . "encrypt:" + let b:GPGOptions+=["encrypt"] let start=match(output, "gpg: public key is [[:xdigit:]]\\{8}") while (start >= 0) @@ -247,10 +253,10 @@ fun s:GPGDecrypt() call s:GPGDebug(1, "recipient is " . recipient) let name=s:GPGNameToID(recipient) if (strlen(name) > 0) - let b:GPGRecipients=b:GPGRecipients . name . ":" + let b:GPGRecipients+=[name] call s:GPGDebug(1, "name of recipient is " . name) else - let b:GPGUnknownRecipients=b:GPGUnknownRecipients . recipient . ":" + let b:GPGUnknownRecipients+=[recipient] echohl GPGWarning echom "The recipient " . recipient . " is not in your public keyring!" echohl None @@ -271,7 +277,7 @@ fun s:GPGDecrypt() " check if the message is armored if (match(output, "gpg: armor header") >= 0) call s:GPGDebug(1, "this file is armored") - let b:GPGOptions=b:GPGOptions . "armor:" + let b:GPGOptions+=["armor"] endi " finally decrypt the buffer content @@ -309,10 +315,8 @@ endf " fun s:GPGEncrypt() " save window view - if v:version >= 700 - let s:GPGWindowView = winsaveview() - call s:GPGDebug(2, "saved window view " . string(s:GPGWindowView)) - endi + let s:GPGWindowView = winsaveview() + call s:GPGDebug(2, "saved window view " . string(s:GPGWindowView)) " store encoding and switch to a safe one if &fileencoding != &encoding @@ -340,89 +344,71 @@ fun s:GPGEncrypt() let field=0 " built list of options - if (!exists("b:GPGOptions") || strlen(b:GPGOptions) == 0) + if (!exists("b:GPGOptions") || len(b:GPGOptions) == 0) + let b:GPGOptions=[] if (exists("g:GPGPreferSymmetric") && g:GPGPreferSymmetric == 1) - let b:GPGOptions="symmetric:" + let b:GPGOptions+=["symmetric"] else - let b:GPGOptions="encrypt:" + let b:GPGOptions+=["encrypt"] endi if (exists("g:GPGPreferArmor") && g:GPGPreferArmor == 1) - let b:GPGOptions=b:GPGOptions . "armor:" + let b:GPGOptions+=["armor"] endi - call s:GPGDebug(1, "no options set, so using default options: " . b:GPGOptions) + call s:GPGDebug(1, "no options set, so using default options: " . string(b:GPGOptions)) endi - let field=0 - let option=s:GetField(b:GPGOptions, ":", field) - while (strlen(option)) + for option in b:GPGOptions let options=options . " --" . option . " " - let field=field+1 - let option=s:GetField(b:GPGOptions, ":", field) - endw + endfor - let GPGUnknownRecipients="" - let field=0 - let cur_recipient="-" + let GPGUnknownRecipients=[] " Check recipientslist for unknown recipients again - while(strlen(cur_recipient)) - let cur_recipient=s:GetField(b:GPGRecipients, ":", field) - let field=field+1 - + for cur_recipient in b:GPGRecipients " only do this if the line is not empty if (strlen(cur_recipient) > 0) let gpgid=s:GPGNameToID(cur_recipient) if (strlen(gpgid) <= 0) - let GPGUnknownRecipients=GPGUnknownRecipients . cur_recipient . ":" + let GPGUnknownRecipients+=[cur_recipient] echohl GPGWarning echom "The recipient " . cur_recipient . " is not in your public keyring!" echohl None endi endi - endw + endfor " check if there are unknown recipients and warn - if(strlen(GPGUnknownRecipients) > 0) + if(len(GPGUnknownRecipients) > 0) echohl GPGWarning echom "There are unknown recipients!!" echom "Please use GPGEditRecipients to correct!!" echo echohl None - call s:GPGDebug(1, "unknown recipients are: " . GPGUnknownRecipients) + call s:GPGDebug(1, "unknown recipients are: " . join(GPGUnknownRecipients, " ")) " Remove unknown recipients from recipientslist - let unknown_recipients_field=0 - let cur_unknown_recipient="-" - let known_recipients=b:GPGRecipients - while(strlen(cur_unknown_recipient)) - let cur_unknown_recipient=s:GetField(GPGUnknownRecipients, ":", unknown_recipients_field) - - let match_result=match(known_recipients, cur_unknown_recipient.":") - if(match_result > 0 && strlen(cur_unknown_recipient) > 0) - echohl GPGWarning - echom "Removing ". cur_unknown_recipient ." from recipientlist!\n" - echohl None - let Known_Recipients=substitute(known_recipients, cur_unknown_recipient .":", "", "g") - endi - - let unknown_recipients_field=unknown_recipients_field+1 + let unknown_recipients=join(GPGUnknownRecipients, " ") + let index=0 + while index < len(b:GPGRecipients) + if match(unknown_recipients, b:GPGRecipients[index]) + echohl GPGWarning + echom "Removing ". b:GPGRecipients[index] ." from recipientlist!\n" + echohl None + call remove(b:GPGRecipients, index) + endi endw - " Let user know whats happend and copy known_recipients back to buffer - let dummy=input("Press ENTER to quit") - let b:GPGRecipients=known_recipients + + " Let user know whats happend and copy known_recipients back to buffer + let dummy=input("Press ENTER to quit") endi " built list of recipients - if (exists("b:GPGRecipients") && strlen(b:GPGRecipients) > 0) - call s:GPGDebug(1, "recipients are: " . b:GPGRecipients) - let field=0 - let gpgid=s:GetField(b:GPGRecipients, ":", field) - while (strlen(gpgid)) + if (exists("b:GPGRecipients") && len(b:GPGRecipients) > 0) + call s:GPGDebug(1, "recipients are: " . join(b:GPGRecipients, " ")) + for gpgid in b:GPGRecipients let recipients=recipients . " -r " . gpgid - let field=field+1 - let gpgid=s:GetField(b:GPGRecipients, ":", field) - endw + endfor else - if (match(b:GPGOptions, "encrypt:") >= 0) + if (match(join(b:GPGOptions, " "), "encrypt") >= 0) echohl GPGError echom "There are no recipients!!" echom "Please use GPGEditRecipients to correct!!" @@ -472,10 +458,8 @@ fun s:GPGEncryptPost() endi " restore window view - if v:version >= 700 - call winrestview(s:GPGWindowView) - call s:GPGDebug(2, "restored window view" . string(s:GPGWindowView)) - endi + call winrestview(s:GPGWindowView) + call s:GPGDebug(2, "restored window view" . string(s:GPGWindowView)) " refresh screen redraw! @@ -497,31 +481,21 @@ fun s:GPGViewRecipients() if (exists("b:GPGRecipients")) echo 'This file has following recipients (Unknown recipients have a prepended "!"):' " echo the recipients - let field=0 - let name=s:GetField(b:GPGRecipients, ":", field) - while (strlen(name) > 0) + for name in b:GPGRecipients let name=s:GPGIDToName(name) echo name - - let field=field+1 - let name=s:GetField(b:GPGRecipients, ":", field) - endw + endfor " put the unknown recipients in the scratch buffer - let field=0 echohl GPGWarning - let name=s:GetField(b:GPGUnknownRecipients, ":", field) - while (strlen(name) > 0) + for name in b:GPGUnknownRecipients let name="!" . name echo name - - let field=field+1 - let name=s:GetField(b:GPGUnknownRecipients, ":", field) - endw + endfor echohl None " check if there is any known recipient - if (strlen(s:GetField(b:GPGRecipients, ":", 0)) == 0) + if (len(b:GPGRecipients) == 0) echohl GPGError echom 'There are no known recipients!' echohl None @@ -593,31 +567,20 @@ fun s:GPGEditRecipients() " put the recipients in the scratch buffer let recipients=getbufvar(b:corresponding_to, "GPGRecipients") - let field=0 - let name=s:GetField(recipients, ":", field) - while (strlen(name) > 0) + for name in recipients let name=s:GPGIDToName(name) silent put =name - - let field=field+1 - let name=s:GetField(recipients, ":", field) - endw + endfor " put the unknown recipients in the scratch buffer let unknownRecipients=getbufvar(b:corresponding_to, "GPGUnknownRecipients") - let field=0 let syntaxPattern="\\(nonexistingwordinthisbuffer" - - let name=s:GetField(unknownRecipients, ":", field) - while (strlen(name) > 0) + for name in unknownRecipients let name="!" . name let syntaxPattern=syntaxPattern . "\\|" . name silent put =name - - let field=field+1 - let name=s:GetField(unknownRecipients, ":", field) - endw + endfor let syntaxPattern=syntaxPattern . "\\)" @@ -660,8 +623,8 @@ fun s:GPGFinishRecipientsBuffer() endi " clear GPGRecipients and GPGUnknownRecipients - let GPGRecipients="" - let GPGUnknownRecipients="" + let GPGRecipients=[] + let GPGUnknownRecipients=[] " delete the autocommand autocmd! * @@ -681,9 +644,9 @@ fun s:GPGFinishRecipientsBuffer() if (strlen(recipient) > 0) let gpgid=s:GPGNameToID(recipient) if (strlen(gpgid) > 0) - let GPGRecipients=GPGRecipients . gpgid . ":" + let GPGRecipients+=[gpgid] else - let GPGUnknownRecipients=GPGUnknownRecipients . recipient . ":" + let GPGUnknownRecipients+=[recipient] echohl GPGWarning echom "The recipient " . recipient . " is not in your public keyring!" echohl None @@ -702,7 +665,7 @@ fun s:GPGFinishRecipientsBuffer() call setbufvar(b:corresponding_to, "GPGEncrypted", 1) " check if there is any known recipient - if (strlen(s:GetField(GPGRecipients, ":", 0)) == 0) + if (len(GPGRecipients) == 0) echohl GPGError echom 'There are no known recipients!' echohl None @@ -728,14 +691,9 @@ fun s:GPGViewOptions() if (exists("b:GPGOptions")) echo 'This file has following options:' " echo the options - let field=0 - let option=s:GetField(b:GPGOptions, ":", field) - while (strlen(option) > 0) + for option in b:GPGOptions echo option - - let field=field+1 - let option=s:GetField(b:GPGOptions, ":", field) - endw + endfor endi endf @@ -805,15 +763,10 @@ fun s:GPGEditOptions() " put the options in the scratch buffer let options=getbufvar(b:corresponding_to, "GPGOptions") - let field=0 - let option=s:GetField(options, ":", field) - while (strlen(option) > 0) + for option in options silent put =option - - let field=field+1 - let option=s:GetField(options, ":", field) - endw + endfor " delete the empty first line silent normal! 1Gdd @@ -849,8 +802,8 @@ fun s:GPGFinishOptionsBuffer() endi " clear GPGOptions and GPGUnknownOptions - let GPGOptions="" - let GPGUnknownOptions="" + let GPGOptions=[] + let GPGUnknownOptions=[] " delete the autocommand autocmd! * @@ -868,7 +821,7 @@ fun s:GPGFinishOptionsBuffer() " only do this if the line is not empty if (strlen(option) > 0) - let GPGOptions=GPGOptions . option . ":" + let GPGOptions+=[option] endi let currentline=currentline+1 @@ -901,25 +854,25 @@ fun s:GPGNameToID(name) if &encoding != "utf-8" let output=iconv(output, "utf-8", &encoding) endi + let lines=split(output, "\n") " parse the output of gpg let pub_seen=0 let uid_seen=0 - let line=0 let counter=0 - let gpgids="" + let gpgids=[] let choices="The name \"" . a:name . "\" is ambiguous. Please select the correct key:\n" - let linecontent=s:GetField(output, "\n", line) - while (strlen(linecontent)) + for line in lines + let fields=split(line, ":") " search for the next uid if (pub_seen == 1) - if (s:GetField(linecontent, ":", 0) == "uid") + if (fields[0] == "uid") if (uid_seen == 0) - let choices=choices . counter . ": " . s:GetField(linecontent, ":", 9) . "\n" + let choices=choices . counter . ": " . fields[9] . "\n" let counter=counter+1 let uid_seen=1 else - let choices=choices . " " . s:GetField(linecontent, ":", 9) . "\n" + let choices=choices . " " . fields[9] . "\n" endi else let uid_seen=0 @@ -929,15 +882,13 @@ fun s:GPGNameToID(name) " search for the next pub if (pub_seen == 0) - if (s:GetField(linecontent, ":", 0) == "pub") - let gpgids=gpgids . s:GetField(linecontent, ":", 4) . ":" + if (fields[0] == "pub") + let gpgids+=[fields[4]] let pub_seen=1 endi endi - let line=line+1 - let linecontent=s:GetField(output, "\n", line) - endw + endfor " counter > 1 means we have more than one results let answer=0 @@ -949,7 +900,7 @@ fun s:GPGNameToID(name) endw endi - return s:GetField(gpgids, ":", answer) + return get(gpgids, answer, "") endf " Function: s:GPGIDToName(identity) {{{2 @@ -971,63 +922,29 @@ fun s:GPGIDToName(identity) if &encoding != "utf-8" let output=iconv(output, "utf-8", &encoding) endi + let lines=split(output, "\n") " parse the output of gpg let pub_seen=0 - let finish=0 - let line=0 - let linecontent=s:GetField(output, "\n", line) - while (strlen(linecontent) && !finish) + let uid="" + for line in lines + let fields=split(line, ":") if (pub_seen == 0) " search for the next pub - if (s:GetField(linecontent, ":", 0) == "pub") + if (fields[0] == "pub") let pub_seen=1 endi else " search for the next uid - if (s:GetField(linecontent, ":", 0) == "uid") + if (fields[0] == "uid") let pub_seen=0 - let finish=1 - let uid=s:GetField(linecontent, ":", 9) + let uid=fields[9] + break endi endi - - let line=line+1 - let linecontent=s:GetField(output, "\n", line) - endw + endfor return uid endf -" Function: s:GetField(line, separator, field) {{{2 -" -" find field of 'separator' separated string, counting starts with 0 -" Returns: content of the field, if field doesn't exist it returns an empty -" string -fun s:GetField(line, separator, field) - let counter=a:field - let separatorLength=strlen(a:separator) - let start=0 - let end=match(a:line, a:separator) - if (end < 0) - let end=strlen(a:line) - endi - - " search for requested field - while (start < strlen(a:line) && counter > 0) - let counter=counter-separatorLength - let start=end+separatorLength - let end=match(a:line, a:separator, start) - if (end < 0) - let end=strlen(a:line) - endi - endw - - if (start < strlen(a:line)) - return strpart(a:line, start, end-start) - else - return "" - endi -endf - " Function: s:GPGDebug(level, text) {{{2 " " output debug message, if this message has high enough importance From 3aa8e8d1dd698c4ec5237069898a8fa1fafd458e Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Tue, 5 Jul 2011 09:35:11 +0200 Subject: [PATCH 046/115] Don't use explicit join() in match() calls. --- plugin/gnupg.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 554b706..be82b7b 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -408,7 +408,7 @@ fun s:GPGEncrypt() let recipients=recipients . " -r " . gpgid endfor else - if (match(join(b:GPGOptions, " "), "encrypt") >= 0) + if (match(b:GPGOptions, "encrypt") >= 0) echohl GPGError echom "There are no recipients!!" echom "Please use GPGEditRecipients to correct!!" From 6c6b8b156c58ee2d083bd35cee1bad46dde9c470 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Tue, 5 Jul 2011 09:35:33 +0200 Subject: [PATCH 047/115] Unique recipient list and option list. --- plugin/gnupg.vim | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index be82b7b..7e0f3ab 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -644,12 +644,16 @@ fun s:GPGFinishRecipientsBuffer() if (strlen(recipient) > 0) let gpgid=s:GPGNameToID(recipient) if (strlen(gpgid) > 0) - let GPGRecipients+=[gpgid] + if (match(GPGRecipients, gpgid) < 0) + let GPGRecipients+=[gpgid] + endi else - let GPGUnknownRecipients+=[recipient] - echohl GPGWarning - echom "The recipient " . recipient . " is not in your public keyring!" - echohl None + if (match(GPGUnknownRecipients, recipient) < 0) + let GPGUnknownRecipients+=[recipient] + echohl GPGWarning + echom "The recipient " . recipient . " is not in your public keyring!" + echohl None + endi end endi @@ -820,7 +824,7 @@ fun s:GPGFinishOptionsBuffer() let option=substitute(option, "^GPG:.*$", "", "") " only do this if the line is not empty - if (strlen(option) > 0) + if (strlen(option) > 0 && match(GPGOptions, option) < 0) let GPGOptions+=[option] endi From f5b4b9a754f5f2e730488d348875d6a6109b660c Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Mon, 28 Jul 2008 21:46:53 +0000 Subject: [PATCH 048/115] Write whole vim keywords endi -> endif endw -> endwhile endf -> endfunction fun -> function com -> command --- plugin/gnupg.vim | 184 +++++++++++++++++++++++------------------------ 1 file changed, 92 insertions(+), 92 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 7e0f3ab..6b49179 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -73,7 +73,7 @@ endif if (exists("g:loaded_gnupg") || &cp || exists("#BufReadPre#*.\(gpg\|asc\|pgp\)")) finish -endi +endif let g:loaded_gnupg = "$Revision$" @@ -85,7 +85,7 @@ augroup GnuPG autocmd BufNewFile,BufReadPre,FileReadPre *.\(gpg\|asc\|pgp\) call s:GPGInit() " force the user to edit the recipient list if he opens a new file and public " keys are preferred - autocmd BufNewFile *.\(gpg\|asc\|pgp\) if (exists("g:GPGPreferSymmetric") && g:GPGPreferSymmetric == 0) | call s:GPGEditRecipients() | endi + autocmd BufNewFile *.\(gpg\|asc\|pgp\) if (exists("g:GPGPreferSymmetric") && g:GPGPreferSymmetric == 0) | call s:GPGEditRecipients() | endif " do the decryption autocmd BufReadPost,FileReadPost *.\(gpg\|asc\|pgp\) call s:GPGDecrypt() @@ -106,7 +106,7 @@ highlight default link GPGHighlightUnknownRecipient ErrorMsg " " initialize the plugin " -fun s:GPGInit() +function s:GPGInit() " first make sure nothing is written to ~/.viminfo while editing " an encrypted file. set viminfo= @@ -180,7 +180,7 @@ fun s:GPGInit() let s:shell = 'sh' let s:stderrredirnull ='2>/dev/null' let s:GPGCommand="LANG=C LC_ALL=C " . s:GPGCommand - endi + endif " find the supported algorithms let &shellredir=s:shellredir @@ -193,13 +193,13 @@ fun s:GPGInit() let s:GPGCipher=substitute(output, ".*Cipher: \\(.\\{-}\\)\n.*", "\\1", "") let s:GPGHash=substitute(output, ".*Hash: \\(.\\{-}\\)\n.*", "\\1", "") let s:GPGCompress=substitute(output, ".*Compress: \\(.\\{-}\\)\n.*", "\\1", "") -endf +endfunction " Function: s:GPGDecrypt() {{{2 " " decrypt the buffer and find all recipients of the encrypted file " -fun s:GPGDecrypt() +function s:GPGDecrypt() " switch to binary mode to read the encrypted file set bin @@ -238,7 +238,7 @@ fun s:GPGDecrypt() echom "The cipher " . cipher . " is not known by the local gpg command. Using default!" echo echohl None - endi + endif elseif (match(output, "gpg: public key is [[:xdigit:]]\\{8}") >= 0) " file is asymmetric encrypted let b:GPGEncrypted=1 @@ -262,7 +262,7 @@ fun s:GPGDecrypt() echohl None end let start=match(output, "gpg: public key is [[:xdigit:]]\\{8}", start) - endw + endwhile else " file is not encrypted let b:GPGEncrypted=0 @@ -272,13 +272,13 @@ fun s:GPGDecrypt() echohl None set nobin return - endi + 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"] - endi + endif " finally decrypt the buffer content " since even with the --quiet option passphrase typos will be reported, @@ -296,7 +296,7 @@ fun s:GPGDecrypt() bwipeout set nobin return - endi + endif " turn off binary mode set nobin @@ -307,13 +307,13 @@ fun s:GPGDecrypt() " refresh screen redraw! -endf +endfunction " Function: s:GPGEncrypt() {{{2 " " encrypts the buffer to all previous recipients " -fun s:GPGEncrypt() +function s:GPGEncrypt() " save window view let s:GPGWindowView = winsaveview() call s:GPGDebug(2, "saved window view " . string(s:GPGWindowView)) @@ -326,7 +326,7 @@ fun s:GPGEncrypt() else let s:GPGEncoding = "" call s:GPGDebug(2, "encoding and fileencoding are the same (\"" . &encoding . "\"), not switching") - endi + endif " switch buffer to binary mode set bin @@ -337,7 +337,7 @@ fun s:GPGEncrypt() echom "File is not encrypted, all GPG functions disabled!" echohl None return - endi + endif let options="" let recipients="" @@ -350,12 +350,12 @@ fun s:GPGEncrypt() let b:GPGOptions+=["symmetric"] else let b:GPGOptions+=["encrypt"] - endi + endif if (exists("g:GPGPreferArmor") && g:GPGPreferArmor == 1) let b:GPGOptions+=["armor"] - endi + endif call s:GPGDebug(1, "no options set, so using default options: " . string(b:GPGOptions)) - endi + endif for option in b:GPGOptions let options=options . " --" . option . " " endfor @@ -372,8 +372,8 @@ fun s:GPGEncrypt() echohl GPGWarning echom "The recipient " . cur_recipient . " is not in your public keyring!" echohl None - endi - endi + endif + endif endfor " check if there are unknown recipients and warn @@ -394,12 +394,12 @@ fun s:GPGEncrypt() echom "Removing ". b:GPGRecipients[index] ." from recipientlist!\n" echohl None call remove(b:GPGRecipients, index) - endi - endw + endif + endwhile " Let user know whats happend and copy known_recipients back to buffer let dummy=input("Press ENTER to quit") - endi + endif " built list of recipients if (exists("b:GPGRecipients") && len(b:GPGRecipients) > 0) @@ -414,8 +414,8 @@ fun s:GPGEncrypt() echom "Please use GPGEditRecipients to correct!!" echo echohl None - endi - endi + endif + endif " encrypt the buffer let &shellredir=s:shellredir @@ -431,19 +431,19 @@ fun s:GPGEncrypt() echohl None bwipeout return - endi + endif -endf +endfunction " Function: s:GPGEncryptPost() {{{2 " " undo changes don by encrypt, after writing " -fun s:GPGEncryptPost() +function s:GPGEncryptPost() if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) return - endi + endif " undo encryption of buffer content silent u @@ -455,7 +455,7 @@ fun s:GPGEncryptPost() if s:GPGEncoding != "" let &encoding = s:GPGEncoding call s:GPGDebug(2, "restored encoding \"" . &encoding . "\"") - endi + endif " restore window view call winrestview(s:GPGWindowView) @@ -463,20 +463,20 @@ fun s:GPGEncryptPost() " refresh screen redraw! -endf +endfunction " Function: s:GPGViewRecipients() {{{2 " " echo the recipients " -fun s:GPGViewRecipients() +function s:GPGViewRecipients() " guard for unencrypted files if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) echohl GPGWarning echom "File is not encrypted, all GPG functions disabled!" echohl None return - endi + endif if (exists("b:GPGRecipients")) echo 'This file has following recipients (Unknown recipients have a prepended "!"):' @@ -499,22 +499,22 @@ fun s:GPGViewRecipients() echohl GPGError echom 'There are no known recipients!' echohl None - endi - endi -endf + endif + endif +endfunction " Function: s:GPGEditRecipients() {{{2 " " create a scratch buffer with all recipients to add/remove recipients " -fun s:GPGEditRecipients() +function s:GPGEditRecipients() " guard for unencrypted files if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) echohl GPGWarning echom "File is not encrypted, all GPG functions disabled!" echohl None return - endi + endif " only do this if it isn't already a GPGRecipients_* buffer if (match(bufname("%"), "^\\(GPGRecipients_\\|GPGOptions_\\)") != 0 && match(bufname("%"), "\.\\(gpg\\|asc\\|pgp\\)$") >= 0) @@ -540,11 +540,11 @@ fun s:GPGEditRecipients() " add a autocommand to regenerate the recipients after a write autocmd BufHidden,BufUnload,BufWriteCmd call s:GPGFinishRecipientsBuffer() - endi + endif " empty the buffer silent normal! 1GdG - endi + endif " Mark the buffer as a scratch buffer setlocal buftype=acwrite @@ -593,7 +593,7 @@ fun s:GPGEditRecipients() syntax match GPGComment "^GPG:.*$" highlight clear GPGComment highlight link GPGComment Comment - endi + endif " delete the empty first line silent normal! 1Gdd @@ -601,26 +601,26 @@ fun s:GPGEditRecipients() " jump to the first recipient silent normal! G - endi -endf + endif +endfunction " Function: s:GPGFinishRecipientsBuffer() {{{2 " " create a new recipient list from RecipientsBuffer -fun s:GPGFinishRecipientsBuffer() +function s:GPGFinishRecipientsBuffer() " guard for unencrypted files if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) echohl GPGWarning echom "File is not encrypted, all GPG functions disabled!" echohl None return - endi + endif " go to buffer before doing work if (bufnr("%") != expand("")) " switch to scratch buffer window exe 'silent! ' . bufwinnr(expand("")) . "wincmd w" - endi + endif " clear GPGRecipients and GPGUnknownRecipients let GPGRecipients=[] @@ -646,20 +646,20 @@ fun s:GPGFinishRecipientsBuffer() if (strlen(gpgid) > 0) if (match(GPGRecipients, gpgid) < 0) let GPGRecipients+=[gpgid] - endi + endif else if (match(GPGUnknownRecipients, recipient) < 0) let GPGUnknownRecipients+=[recipient] echohl GPGWarning echom "The recipient " . recipient . " is not in your public keyring!" echohl None - endi + endif end - endi + endif let currentline=currentline+1 let recipient=getline(currentline) - endw + endwhile " write back the new recipient list to the corresponding buffer and mark it " as modified. Buffer is now for sure a encrypted buffer. @@ -673,24 +673,24 @@ fun s:GPGFinishRecipientsBuffer() echohl GPGError echom 'There are no known recipients!' echohl None - endi + endif " reset modified flag set nomodified -endf +endfunction " Function: s:GPGViewOptions() {{{2 " " echo the recipients " -fun s:GPGViewOptions() +function s:GPGViewOptions() " guard for unencrypted files if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) echohl GPGWarning echom "File is not encrypted, all GPG functions disabled!" echohl None return - endi + endif if (exists("b:GPGOptions")) echo 'This file has following options:' @@ -698,21 +698,21 @@ fun s:GPGViewOptions() for option in b:GPGOptions echo option endfor - endi -endf + endif +endfunction " Function: s:GPGEditOptions() {{{2 " " create a scratch buffer with all recipients to add/remove recipients " -fun s:GPGEditOptions() +function s:GPGEditOptions() " guard for unencrypted files if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) echohl GPGWarning echom "File is not encrypted, all GPG functions disabled!" echohl None return - endi + endif " only do this if it isn't already a GPGOptions_* buffer if (match(bufname("%"), "^\\(GPGRecipients_\\|GPGOptions_\\)") != 0 && match(bufname("%"), "\.\\(gpg\\|asc\\|pgp\\)$") >= 0) @@ -738,11 +738,11 @@ fun s:GPGEditOptions() " add a autocommand to regenerate the options after a write autocmd BufHidden,BufUnload,BufWriteCmd call s:GPGFinishOptionsBuffer() - endi + endif " empty the buffer silent normal! 1GdG - endi + endif " Mark the buffer as a scratch buffer setlocal buftype=nofile @@ -783,27 +783,27 @@ fun s:GPGEditOptions() syntax match GPGComment "^GPG:.*$" highlight clear GPGComment highlight link GPGComment Comment - endi - endi -endf + endif + endif +endfunction " Function: s:GPGFinishOptionsBuffer() {{{2 " " create a new option list from OptionsBuffer -fun s:GPGFinishOptionsBuffer() +function s:GPGFinishOptionsBuffer() " guard for unencrypted files if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) echohl GPGWarning echom "File is not encrypted, all GPG functions disabled!" echohl None return - endi + endif " go to buffer before doing work if (bufnr("%") != expand("")) " switch to scratch buffer window exe 'silent! ' . bufwinnr(expand("")) . "wincmd w" - endi + endif " clear GPGOptions and GPGUnknownOptions let GPGOptions=[] @@ -826,11 +826,11 @@ fun s:GPGFinishOptionsBuffer() " only do this if the line is not empty if (strlen(option) > 0 && match(GPGOptions, option) < 0) let GPGOptions+=[option] - endi + endif let currentline=currentline+1 let option=getline(currentline) - endw + endwhile " write back the new option list to the corresponding buffer and mark it " as modified @@ -839,13 +839,13 @@ fun s:GPGFinishOptionsBuffer() " reset modified flag set nomodified -endf +endfunction " Function: s:GPGNameToID(name) {{{2 " " find GPG key ID corresponding to a name " Returns: ID for the given name -fun s:GPGNameToID(name) +function s:GPGNameToID(name) " ask gpg for the id for a name let &shellredir=s:shellredir let &shell=s:shell @@ -857,7 +857,7 @@ fun s:GPGNameToID(name) " so convert it, if necessary if &encoding != "utf-8" let output=iconv(output, "utf-8", &encoding) - endi + endif let lines=split(output, "\n") " parse the output of gpg @@ -877,20 +877,20 @@ fun s:GPGNameToID(name) let uid_seen=1 else let choices=choices . " " . fields[9] . "\n" - endi + endif else let uid_seen=0 let pub_seen=0 - endi - endi + endif + endif " search for the next pub if (pub_seen == 0) if (fields[0] == "pub") let gpgids+=[fields[4]] let pub_seen=1 - endi - endi + endif + endif endfor @@ -901,17 +901,17 @@ fun s:GPGNameToID(name) let answer=input(choices, "0") while (answer == "") let answer=input("Enter number: ", "0") - endw - endi + endwhile + endif return get(gpgids, answer, "") -endf +endfunction " Function: s:GPGIDToName(identity) {{{2 " " find name corresponding to a GPG key ID " Returns: Name for the given ID -fun s:GPGIDToName(identity) +function s:GPGIDToName(identity) " TODO is the encryption subkey really unique? " ask gpg for the id for a name @@ -925,7 +925,7 @@ fun s:GPGIDToName(identity) " so convert it, if necessary if &encoding != "utf-8" let output=iconv(output, "utf-8", &encoding) - endi + endif let lines=split(output, "\n") " parse the output of gpg @@ -936,32 +936,32 @@ fun s:GPGIDToName(identity) if (pub_seen == 0) " search for the next pub if (fields[0] == "pub") let pub_seen=1 - endi + endif else " search for the next uid if (fields[0] == "uid") let pub_seen=0 let uid=fields[9] break - endi - endi + endif + endif endfor return uid -endf +endfunction " Function: s:GPGDebug(level, text) {{{2 " " output debug message, if this message has high enough importance -fun s:GPGDebug(level, text) +function s:GPGDebug(level, text) if (g:GPGDebugLevel >= a:level) echom a:text - endi -endf + endif +endfunction " Section: Command definitions {{{1 -com! GPGViewRecipients call s:GPGViewRecipients() -com! GPGEditRecipients call s:GPGEditRecipients() -com! GPGViewOptions call s:GPGViewOptions() -com! GPGEditOptions call s:GPGEditOptions() +command! GPGViewRecipients call s:GPGViewRecipients() +command! GPGEditRecipients call s:GPGEditRecipients() +command! GPGViewOptions call s:GPGViewOptions() +command! GPGEditOptions call s:GPGEditOptions() " vim600: foldmethod=marker:foldlevel=0 From b4be3c10e2344379e90f75af3754a32cc06659b7 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Tue, 5 Jul 2011 12:32:24 +0200 Subject: [PATCH 049/115] Add menu for GVim --- plugin/gnupg.vim | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 6b49179..6dbe455 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -963,5 +963,11 @@ command! GPGViewRecipients call s:GPGViewRecipients() command! GPGEditRecipients call s:GPGEditRecipients() command! GPGViewOptions call s:GPGViewOptions() command! GPGEditOptions call s:GPGEditOptions() - +" Section: Menu {{{1 +if has("menu") + amenu Plugin.GnuPG.View\ Recipients :GPGViewRecipients + amenu Plugin.GnuPG.Edit\ Recipients :GPGEditRecipients + amenu Plugin.GnuPG.View\ Options :GPGViewOptions + amenu Plugin.GnuPG.Edit\ Options :GPGEditOptions +endif " vim600: foldmethod=marker:foldlevel=0 From 4340361541f265de03e08737258b5ae448b11896 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Tue, 5 Jul 2011 12:32:37 +0200 Subject: [PATCH 050/115] Recipients and unknownRecipients was not ensured to be a list --- plugin/gnupg.vim | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 6dbe455..01b3405 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -567,6 +567,10 @@ function s:GPGEditRecipients() " put the recipients in the scratch buffer let recipients=getbufvar(b:corresponding_to, "GPGRecipients") + if (type(recipients) != type([])) + unlet recipients + let recipients=[] + endif for name in recipients let name=s:GPGIDToName(name) @@ -575,6 +579,10 @@ function s:GPGEditRecipients() " put the unknown recipients in the scratch buffer let unknownRecipients=getbufvar(b:corresponding_to, "GPGUnknownRecipients") + if (type(unknownRecipients) != type([])) + unlet unknownRecipients + let unknownRecipients=[] + endif let syntaxPattern="\\(nonexistingwordinthisbuffer" for name in unknownRecipients let name="!" . name From 7afa048df7992794dd39e0ab8a42fa0692416cfc Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Tue, 5 Jul 2011 12:45:44 +0200 Subject: [PATCH 051/115] Make use of fnameescape() --- plugin/gnupg.vim | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 01b3405..b3e59d4 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -67,7 +67,7 @@ " " Section: Plugin header {{{1 if v:version < 700 - echohl ErrorMsg | echo 'plugin gnupg.vim requires Vim version >= 7' | echohl None + echohl ErrorMsg | echo 'plugin gnupg.vim requires Vim version >= 7.0' | echohl None finish endif @@ -169,7 +169,7 @@ function s:GPGInit() " setup shell environment for unix and windows let s:shellredirsave=&shellredir let s:shellsave=&shell - if (match(&shell,"\\(cmd\\|command\\).exe") >= 0) + if (match(&shell,"\\(cmd\\|command\\).execute") >= 0) " windows specific settings let s:shellredir = '>%s' let s:shell = &shell @@ -204,7 +204,7 @@ function s:GPGDecrypt() set bin " get the filename of the current buffer - let filename=escape(expand("%:p"), '\"') + let filename=fnameescape(expand("%:p")) " clear GPGEncrypted, GPGRecipients, GPGUnknownRecipients and GPGOptions let b:GPGEncrypted=0 @@ -302,8 +302,8 @@ function s:GPGDecrypt() set nobin " call the autocommand for the file minus .gpg$ - execute ":doautocmd BufReadPost " . escape(expand("%:r"), ' *?\"'."'") - call s:GPGDebug(2, "called autocommand for " . escape(expand("%:r"), ' *?\"'."'")) + execute ":doautocmd BufReadPost " . fnameescape(expand("%:r")) + call s:GPGDebug(2, "called autocommand for " . fnameescape(expand("%:r"))) " refresh screen redraw! @@ -526,17 +526,17 @@ function s:GPGEditRecipients() " check if this buffer exists if (!bufexists(editbuffername)) " create scratch buffer - exe 'silent! split ' . escape(editbuffername, ' *?\"'."'") + execute 'silent! split ' . fnameescape(editbuffername) " add a autocommand to regenerate the recipients after a write autocmd BufHidden,BufUnload,BufWriteCmd call s:GPGFinishRecipientsBuffer() else if (bufwinnr(editbuffername) >= 0) " switch to scratch buffer window - exe 'silent! ' . bufwinnr(editbuffername) . "wincmd w" + execute 'silent! ' . bufwinnr(editbuffername) . "wincmd w" else " split scratch buffer window - exe 'silent! sbuffer ' . escape(editbuffername, ' *?\"'."'") + execute 'silent! sbuffer ' . fnameescape(editbuffername) " add a autocommand to regenerate the recipients after a write autocmd BufHidden,BufUnload,BufWriteCmd call s:GPGFinishRecipientsBuffer() @@ -627,7 +627,7 @@ function s:GPGFinishRecipientsBuffer() " go to buffer before doing work if (bufnr("%") != expand("")) " switch to scratch buffer window - exe 'silent! ' . bufwinnr(expand("")) . "wincmd w" + execute 'silent! ' . bufwinnr(expand("")) . "wincmd w" endif " clear GPGRecipients and GPGUnknownRecipients @@ -732,17 +732,17 @@ function s:GPGEditOptions() " check if this buffer exists if (!bufexists(editbuffername)) " create scratch buffer - exe 'silent! split ' . escape(editbuffername, ' *?\"'."'") + execute 'silent! split ' . fnameescape(editbuffername) " add a autocommand to regenerate the options after a write autocmd BufHidden,BufUnload,BufWriteCmd call s:GPGFinishOptionsBuffer() else if (bufwinnr(editbuffername) >= 0) " switch to scratch buffer window - exe 'silent! ' . bufwinnr(editbuffername) . "wincmd w" + execute 'silent! ' . bufwinnr(editbuffername) . "wincmd w" else " split scratch buffer window - exe 'silent! sbuffer ' . escape(editbuffername, ' *?\"'."'") + execute 'silent! sbuffer ' . fnameescape(editbuffername) " add a autocommand to regenerate the options after a write autocmd BufHidden,BufUnload,BufWriteCmd call s:GPGFinishOptionsBuffer() @@ -810,7 +810,7 @@ function s:GPGFinishOptionsBuffer() " go to buffer before doing work if (bufnr("%") != expand("")) " switch to scratch buffer window - exe 'silent! ' . bufwinnr(expand("")) . "wincmd w" + execute 'silent! ' . bufwinnr(expand("")) . "wincmd w" endif " clear GPGOptions and GPGUnknownOptions From c3f898827aeb60561afec3659b72d372d8cbbb98 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Tue, 5 Jul 2011 12:46:39 +0200 Subject: [PATCH 052/115] Move initialization of variables to the places where they are used. --- plugin/gnupg.vim | 145 ++++++++++++++++++++--------------------------- 1 file changed, 61 insertions(+), 84 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index b3e59d4..9738949 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -229,6 +229,7 @@ function s:GPGDecrypt() let b:GPGOptions+=["symmetric"] + " find the used cipher algorithm let cipher=substitute(output, ".*gpg: \\([^ ]\\+\\) encrypted data.*", "\\1", "") if (match(s:GPGCipher, "\\<" . cipher . "\\>") >= 0) let b:GPGOptions+=["cipher-algo " . cipher] @@ -246,6 +247,7 @@ function s:GPGDecrypt() let b:GPGOptions+=["encrypt"] + " find the used public keys let start=match(output, "gpg: public key is [[:xdigit:]]\\{8}") while (start >= 0) let start=start + strlen("gpg: public key is ") @@ -258,7 +260,7 @@ function s:GPGDecrypt() else let b:GPGUnknownRecipients+=[recipient] echohl GPGWarning - echom "The recipient " . recipient . " is not in your public keyring!" + echom "The recipient \"" . recipient . "\" is not in your public keyring!" echohl None end let start=match(output, "gpg: public key is [[:xdigit:]]\\{8}", start) @@ -291,7 +293,7 @@ function s:GPGDecrypt() if (v:shell_error) " message could not be decrypted silent u echohl GPGError - let asd=input("Message could not be decrypted! (Press ENTER)") + let blackhole=input("Message could not be decrypted! (Press ENTER)") echohl None bwipeout set nobin @@ -339,11 +341,7 @@ function s:GPGEncrypt() return endif - let options="" - let recipients="" - let field=0 - - " built list of options + " initialize GPGOptions if not happened before if (!exists("b:GPGOptions") || len(b:GPGOptions) == 0) let b:GPGOptions=[] if (exists("g:GPGPreferSymmetric") && g:GPGPreferSymmetric == 1) @@ -356,52 +354,35 @@ function s:GPGEncrypt() endif call s:GPGDebug(1, "no options set, so using default options: " . string(b:GPGOptions)) endif + + " built list of options + let options="" for option in b:GPGOptions let options=options . " --" . option . " " endfor - let GPGUnknownRecipients=[] - - " Check recipientslist for unknown recipients again - for cur_recipient in b:GPGRecipients - " only do this if the line is not empty - if (strlen(cur_recipient) > 0) - let gpgid=s:GPGNameToID(cur_recipient) - if (strlen(gpgid) <= 0) - let GPGUnknownRecipients+=[cur_recipient] - echohl GPGWarning - echom "The recipient " . cur_recipient . " is not in your public keyring!" - echohl None - endif - endif + " check recipientslist for unknown recipients again + for recipient in b:GPGUnknownRecipients + echohl GPGWarning + echom "The recipient \"" . recipient . "\" is not in your public keyring!" + echohl None endfor " check if there are unknown recipients and warn - if(len(GPGUnknownRecipients) > 0) + if(len(b:GPGUnknownRecipients) > 0) echohl GPGWarning echom "There are unknown recipients!!" echom "Please use GPGEditRecipients to correct!!" echo echohl None - call s:GPGDebug(1, "unknown recipients are: " . join(GPGUnknownRecipients, " ")) - - " Remove unknown recipients from recipientslist - let unknown_recipients=join(GPGUnknownRecipients, " ") - let index=0 - while index < len(b:GPGRecipients) - if match(unknown_recipients, b:GPGRecipients[index]) - echohl GPGWarning - echom "Removing ". b:GPGRecipients[index] ." from recipientlist!\n" - echohl None - call remove(b:GPGRecipients, index) - endif - endwhile + call s:GPGDebug(1, "unknown recipients are: " . join(b:GPGUnknownRecipients, " ")) " Let user know whats happend and copy known_recipients back to buffer let dummy=input("Press ENTER to quit") endif " built list of recipients + let recipients="" if (exists("b:GPGRecipients") && len(b:GPGRecipients) > 0) call s:GPGDebug(1, "recipients are: " . join(b:GPGRecipients, " ")) for gpgid in b:GPGRecipients @@ -427,7 +408,7 @@ function s:GPGEncrypt() if (v:shell_error) " message could not be encrypted silent u echohl GPGError - let asd=input("Message could not be encrypted! File might be empty! (Press ENTER)") + let blackhole=input("Message could not be encrypted! File might be empty! (Press ENTER)") echohl None bwipeout return @@ -440,7 +421,7 @@ endfunction " undo changes don by encrypt, after writing " function s:GPGEncryptPost() - + " guard for unencrypted files if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) return endif @@ -486,7 +467,7 @@ function s:GPGViewRecipients() echo name endfor - " put the unknown recipients in the scratch buffer + " echo the unknown recipients echohl GPGWarning for name in b:GPGUnknownRecipients let name="!" . name @@ -555,7 +536,7 @@ function s:GPGEditRecipients() setlocal nonumber " so we know for which other buffer this edit buffer is - let b:corresponding_to=buffername + let b:GPGCorrespondingTo=buffername " put some comments to the scratch buffer silent put ='GPG: ----------------------------------------------------------------------' @@ -566,30 +547,28 @@ function s:GPGEditRecipients() silent put ='GPG: ----------------------------------------------------------------------' " put the recipients in the scratch buffer - let recipients=getbufvar(b:corresponding_to, "GPGRecipients") + let recipients=getbufvar(b:GPGCorrespondingTo, "GPGRecipients") if (type(recipients) != type([])) unlet recipients let recipients=[] endif - for name in recipients let name=s:GPGIDToName(name) silent put =name endfor " put the unknown recipients in the scratch buffer - let unknownRecipients=getbufvar(b:corresponding_to, "GPGUnknownRecipients") - if (type(unknownRecipients) != type([])) - unlet unknownRecipients - let unknownRecipients=[] + let unknownrecipients=getbufvar(b:GPGCorrespondingTo, "GPGUnknownRecipients") + if (type(unknownrecipients) != type([])) + unlet unknownrecipients + let unknownrecipients=[] endif - let syntaxPattern="\\(nonexistingwordinthisbuffer" - for name in unknownRecipients + let syntaxPattern="\\(nonexxistinwordinthisbuffer" + for name in unknownrecipients let name="!" . name let syntaxPattern=syntaxPattern . "\\|" . name silent put =name endfor - let syntaxPattern=syntaxPattern . "\\)" " define highlight @@ -630,10 +609,6 @@ function s:GPGFinishRecipientsBuffer() execute 'silent! ' . bufwinnr(expand("")) . "wincmd w" endif - " clear GPGRecipients and GPGUnknownRecipients - let GPGRecipients=[] - let GPGUnknownRecipients=[] - " delete the autocommand autocmd! * @@ -641,6 +616,8 @@ function s:GPGFinishRecipientsBuffer() let recipient=getline(currentline) " get the recipients from the scratch buffer + let recipients=[] + let unknownrecipients=[] while (currentline <= line("$")) " delete all spaces at beginning and end of the line " also delete a '!' at the beginning of the line @@ -652,14 +629,14 @@ function s:GPGFinishRecipientsBuffer() if (strlen(recipient) > 0) let gpgid=s:GPGNameToID(recipient) if (strlen(gpgid) > 0) - if (match(GPGRecipients, gpgid) < 0) - let GPGRecipients+=[gpgid] + if (match(recipients, gpgid) < 0) + let recipients+=[gpgid] endif else - if (match(GPGUnknownRecipients, recipient) < 0) - let GPGUnknownRecipients+=[recipient] + if (match(unknownrecipients, recipient) < 0) + let unknownrecipients+=[recipient] echohl GPGWarning - echom "The recipient " . recipient . " is not in your public keyring!" + echom "The recipient \"" . recipient . "\" is not in your public keyring!" echohl None endif end @@ -671,13 +648,13 @@ function s:GPGFinishRecipientsBuffer() " write back the new recipient list to the corresponding buffer and mark it " as modified. Buffer is now for sure a encrypted buffer. - call setbufvar(b:corresponding_to, "GPGRecipients", GPGRecipients) - call setbufvar(b:corresponding_to, "GPGUnknownRecipients", GPGUnknownRecipients) - call setbufvar(b:corresponding_to, "&mod", 1) - call setbufvar(b:corresponding_to, "GPGEncrypted", 1) + call setbufvar(b:GPGCorrespondingTo, "GPGRecipients", recipients) + call setbufvar(b:GPGCorrespondingTo, "GPGUnknownRecipients", unknownrecipients) + call setbufvar(b:GPGCorrespondingTo, "&mod", 1) + call setbufvar(b:GPGCorrespondingTo, "GPGEncrypted", 1) " check if there is any known recipient - if (len(GPGRecipients) == 0) + if (len(recipients) == 0) echohl GPGError echom 'There are no known recipients!' echohl None @@ -760,7 +737,7 @@ function s:GPGEditOptions() setlocal nonumber " so we know for which other buffer this edit buffer is - let b:corresponding_to=buffername + let b:GPGCorrespondingTo=buffername " put some comments to the scratch buffer silent put ='GPG: ----------------------------------------------------------------------' @@ -774,7 +751,7 @@ function s:GPGEditOptions() silent put ='GPG: ----------------------------------------------------------------------' " put the options in the scratch buffer - let options=getbufvar(b:corresponding_to, "GPGOptions") + let options=getbufvar(b:GPGCorrespondingTo, "GPGOptions") for option in options silent put =option @@ -813,9 +790,9 @@ function s:GPGFinishOptionsBuffer() execute 'silent! ' . bufwinnr(expand("")) . "wincmd w" endif - " clear GPGOptions and GPGUnknownOptions - let GPGOptions=[] - let GPGUnknownOptions=[] + " clear options and unknownOptions + let options=[] + let unknownOptions=[] " delete the autocommand autocmd! * @@ -832,8 +809,8 @@ function s:GPGFinishOptionsBuffer() let option=substitute(option, "^GPG:.*$", "", "") " only do this if the line is not empty - if (strlen(option) > 0 && match(GPGOptions, option) < 0) - let GPGOptions+=[option] + if (strlen(option) > 0 && match(options, option) < 0) + let options+=[option] endif let currentline=currentline+1 @@ -842,8 +819,8 @@ function s:GPGFinishOptionsBuffer() " write back the new option list to the corresponding buffer and mark it " as modified - call setbufvar(b:corresponding_to, "GPGOptions", GPGOptions) - call setbufvar(b:corresponding_to, "&mod", 1) + call setbufvar(b:GPGCorrespondingTo, "GPGOptions", options) + call setbufvar(b:GPGCorrespondingTo, "&mod", 1) " reset modified flag set nomodified @@ -869,34 +846,34 @@ function s:GPGNameToID(name) let lines=split(output, "\n") " parse the output of gpg - let pub_seen=0 - let uid_seen=0 + let pubseen=0 + let uidseen=0 let counter=0 let gpgids=[] let choices="The name \"" . a:name . "\" is ambiguous. Please select the correct key:\n" for line in lines let fields=split(line, ":") " search for the next uid - if (pub_seen == 1) + if (pubseen == 1) if (fields[0] == "uid") - if (uid_seen == 0) + if (uidseen == 0) let choices=choices . counter . ": " . fields[9] . "\n" let counter=counter+1 - let uid_seen=1 + let uidseen=1 else let choices=choices . " " . fields[9] . "\n" endif else - let uid_seen=0 - let pub_seen=0 + let uidseen=0 + let pubseen=0 endif endif " search for the next pub - if (pub_seen == 0) + if (pubseen == 0) if (fields[0] == "pub") let gpgids+=[fields[4]] - let pub_seen=1 + let pubseen=1 endif endif @@ -937,17 +914,17 @@ function s:GPGIDToName(identity) let lines=split(output, "\n") " parse the output of gpg - let pub_seen=0 + let pubseen=0 let uid="" for line in lines let fields=split(line, ":") - if (pub_seen == 0) " search for the next pub + if (pubseen == 0) " search for the next pub if (fields[0] == "pub") - let pub_seen=1 + let pubseen=1 endif else " search for the next uid if (fields[0] == "uid") - let pub_seen=0 + let pubseen=0 let uid=fields[9] break endif From 733c2ce362c5f85554cf41a85e6cd37d76ccf9b5 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Tue, 29 Jul 2008 11:03:08 +0000 Subject: [PATCH 053/115] Yet another use of vim lists --- plugin/gnupg.vim | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 9738949..c22a813 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -612,18 +612,17 @@ function s:GPGFinishRecipientsBuffer() " delete the autocommand autocmd! * - let currentline=1 - let recipient=getline(currentline) " get the recipients from the scratch buffer let recipients=[] let unknownrecipients=[] - while (currentline <= line("$")) + let lines=getline(1,"$") + for line in lines " delete all spaces at beginning and end of the line " also delete a '!' at the beginning of the line - let recipient=substitute(recipient, "^[[:space:]!]*\\(.\\{-}\\)[[:space:]]*$", "\\1", "") + let recipient=substitute(line, "^[[:space:]!]*\\(.\\{-}\\)[[:space:]]*$", "\\1", "") " delete comment lines - let recipient=substitute(recipient, "^GPG:.*$", "", "") + let recipient=substitute(line, "^GPG:.*$", "", "") " only do this if the line is not empty if (strlen(recipient) > 0) @@ -641,10 +640,7 @@ function s:GPGFinishRecipientsBuffer() endif end endif - - let currentline=currentline+1 - let recipient=getline(currentline) - endwhile + endfor " write back the new recipient list to the corresponding buffer and mark it " as modified. Buffer is now for sure a encrypted buffer. @@ -797,25 +793,20 @@ function s:GPGFinishOptionsBuffer() " delete the autocommand autocmd! * - let currentline=1 - let option=getline(currentline) - " get the options from the scratch buffer - while (currentline <= line("$")) + let lines=getline(1, "$") + for line in lines " delete all spaces at beginning and end of the line " also delete a '!' at the beginning of the line - let option=substitute(option, "^[[:space:]!]*\\(.\\{-}\\)[[:space:]]*$", "\\1", "") + let option=substitute(line, "^[[:space:]!]*\\(.\\{-}\\)[[:space:]]*$", "\\1", "") " delete comment lines - let option=substitute(option, "^GPG:.*$", "", "") + let option=substitute(line, "^GPG:.*$", "", "") " only do this if the line is not empty if (strlen(option) > 0 && match(options, option) < 0) let options+=[option] endif - - let currentline=currentline+1 - let option=getline(currentline) - endwhile + endfor " write back the new option list to the corresponding buffer and mark it " as modified From 8998ff3bdef95d09e50343dd914a589595bbd6b2 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Tue, 29 Jul 2008 12:29:28 +0000 Subject: [PATCH 054/115] Did two substitution which overwrote each other --- plugin/gnupg.vim | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index c22a813..e7c5a60 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -617,12 +617,12 @@ function s:GPGFinishRecipientsBuffer() let recipients=[] let unknownrecipients=[] let lines=getline(1,"$") - for line in lines - " delete all spaces at beginning and end of the line - " also delete a '!' at the beginning of the line - let recipient=substitute(line, "^[[:space:]!]*\\(.\\{-}\\)[[:space:]]*$", "\\1", "") + for recipient in lines + " delete all spaces at beginning and end of the recipient + " also delete a '!' at the beginning of the recipient + let recipient=substitute(recipient, "^[[:space:]!]*\\(.\\{-}\\)[[:space:]]*$", "\\1", "") " delete comment lines - let recipient=substitute(line, "^GPG:.*$", "", "") + let recipient=substitute(recipient, "^GPG:.*$", "", "") " only do this if the line is not empty if (strlen(recipient) > 0) @@ -795,12 +795,12 @@ function s:GPGFinishOptionsBuffer() " get the options from the scratch buffer let lines=getline(1, "$") - for line in lines - " delete all spaces at beginning and end of the line - " also delete a '!' at the beginning of the line - let option=substitute(line, "^[[:space:]!]*\\(.\\{-}\\)[[:space:]]*$", "\\1", "") + for option in lines + " delete all spaces at beginning and end of the option + " also delete a '!' at the beginning of the option + let option=substitute(option, "^[[:space:]!]*\\(.\\{-}\\)[[:space:]]*$", "\\1", "") " delete comment lines - let option=substitute(line, "^GPG:.*$", "", "") + let option=substitute(option, "^GPG:.*$", "", "") " only do this if the line is not empty if (strlen(option) > 0 && match(options, option) < 0) From 594ac054a855bed4131f0e1d7c08bd6f12002896 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Wed, 30 Jul 2008 11:55:47 +0000 Subject: [PATCH 055/115] Go back from fnameescape() to escape() fnameescape() is supported only by later patch levels of Vim 7.1 --- plugin/gnupg.vim | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index e7c5a60..6587266 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -204,7 +204,7 @@ function s:GPGDecrypt() set bin " get the filename of the current buffer - let filename=fnameescape(expand("%:p")) + let filename=escape(expand("%:p"), '\"') " clear GPGEncrypted, GPGRecipients, GPGUnknownRecipients and GPGOptions let b:GPGEncrypted=0 @@ -304,8 +304,8 @@ function s:GPGDecrypt() set nobin " call the autocommand for the file minus .gpg$ - execute ":doautocmd BufReadPost " . fnameescape(expand("%:r")) - call s:GPGDebug(2, "called autocommand for " . fnameescape(expand("%:r"))) + execute ":doautocmd BufReadPost " . escape(expand("%:r"), ' *?\"'."'") + call s:GPGDebug(2, "called autocommand for " . escape(expand("%:r"), ' *?\"'."'")) " refresh screen redraw! @@ -507,7 +507,7 @@ function s:GPGEditRecipients() " check if this buffer exists if (!bufexists(editbuffername)) " create scratch buffer - execute 'silent! split ' . fnameescape(editbuffername) + execute 'silent! split ' . escape(editbuffername, ' *?\"'."'") " add a autocommand to regenerate the recipients after a write autocmd BufHidden,BufUnload,BufWriteCmd call s:GPGFinishRecipientsBuffer() @@ -517,7 +517,7 @@ function s:GPGEditRecipients() execute 'silent! ' . bufwinnr(editbuffername) . "wincmd w" else " split scratch buffer window - execute 'silent! sbuffer ' . fnameescape(editbuffername) + execute 'silent! sbuffer ' . escape(editbuffername, ' *?\"'."'") " add a autocommand to regenerate the recipients after a write autocmd BufHidden,BufUnload,BufWriteCmd call s:GPGFinishRecipientsBuffer() @@ -705,7 +705,7 @@ function s:GPGEditOptions() " check if this buffer exists if (!bufexists(editbuffername)) " create scratch buffer - execute 'silent! split ' . fnameescape(editbuffername) + execute 'silent! split ' . escape(editbuffername, ' *?\"'."'") " add a autocommand to regenerate the options after a write autocmd BufHidden,BufUnload,BufWriteCmd call s:GPGFinishOptionsBuffer() @@ -715,7 +715,7 @@ function s:GPGEditOptions() execute 'silent! ' . bufwinnr(editbuffername) . "wincmd w" else " split scratch buffer window - execute 'silent! sbuffer ' . fnameescape(editbuffername) + execute 'silent! sbuffer ' . escape(editbuffername, ' *?\"'."'") " add a autocommand to regenerate the options after a write autocmd BufHidden,BufUnload,BufWriteCmd call s:GPGFinishOptionsBuffer() From 71645fcb3a17eb47d1a89c7860dfef7ee3b52c2d Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Wed, 30 Jul 2008 14:15:02 +0000 Subject: [PATCH 056/115] Check the recipient list again before writing the file Detect a recipient that has been removed between GPGEditRecipients() and GPGEncrypt() --- plugin/gnupg.vim | 45 ++++++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 6587266..e32ad5b 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -361,32 +361,51 @@ function s:GPGEncrypt() let options=options . " --" . option . " " endfor - " check recipientslist for unknown recipients again - for recipient in b:GPGUnknownRecipients - echohl GPGWarning - echom "The recipient \"" . recipient . "\" is not in your public keyring!" - echohl None + " check here again if all recipients are available in the keyring + let recipients = [] + if (exists("b:GPGRecipients") && type(b:GPGRecipients) == type([])) + let recipients += b:GPGRecipients + else + unlet b:GPGRecipients + endif + if (exists("b:GPGUnknownRecipients") && type(b:GPGUnknownRecipients) == type([])) + let recipients += b:GPGUnknownRecipients + else + unlet b:GPGUnknownRecipients + endif + let b:GPGRecipients = [] + let b:GPGUnknownRecipients = [] + for recipient in recipients + let gpgid=s:GPGNameToID(recipient) + if (strlen(gpgid) > 0) + if (match(b:GPGRecipients, gpgid) < 0) + let b:GPGRecipients+=[gpgid] + endif + else + if (match(b:GPGUnknownRecipients, recipient) < 0) + let b:GPGUnknownRecipients+=[recipient] + echohl GPGWarning + echom "The recipient \"" . recipient . "\" is not in your public keyring!" + echohl None + endif + end endfor " check if there are unknown recipients and warn - if(len(b:GPGUnknownRecipients) > 0) + if(exists("b:GPGUnknownRecipients") && len(b:GPGUnknownRecipients) > 0) echohl GPGWarning - echom "There are unknown recipients!!" echom "Please use GPGEditRecipients to correct!!" echo echohl None - call s:GPGDebug(1, "unknown recipients are: " . join(b:GPGUnknownRecipients, " ")) " Let user know whats happend and copy known_recipients back to buffer let dummy=input("Press ENTER to quit") endif " built list of recipients - let recipients="" if (exists("b:GPGRecipients") && len(b:GPGRecipients) > 0) - call s:GPGDebug(1, "recipients are: " . join(b:GPGRecipients, " ")) for gpgid in b:GPGRecipients - let recipients=recipients . " -r " . gpgid + let options=options . " -r " . gpgid endfor else if (match(b:GPGOptions, "encrypt") >= 0) @@ -401,10 +420,10 @@ function s:GPGEncrypt() " encrypt the buffer let &shellredir=s:shellredir let &shell=s:shell - silent exec "'[,']!" . s:GPGCommand . " --quiet --no-encrypt-to " . options . recipients . " " . s:stderrredirnull + silent exec "'[,']!" . s:GPGCommand . " --quiet --no-encrypt-to " . options . " " . s:stderrredirnull let &shellredir=s:shellredirsave let &shell=s:shellsave - call s:GPGDebug(1, "called gpg command is: " . "'[,']!" . s:GPGCommand . " --quiet --no-encrypt-to " . options . recipients . " " . s:stderrredirnull) + call s:GPGDebug(1, "called gpg command is: " . "'[,']!" . s:GPGCommand . " --quiet --no-encrypt-to " . options . " " . s:stderrredirnull) if (v:shell_error) " message could not be encrypted silent u echohl GPGError From eebc52cbe1919f26cdda2d12a9de038cd4acfec8 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Wed, 30 Jul 2008 14:48:53 +0000 Subject: [PATCH 057/115] Get rid of b:GPGUnknownRecipients Introduce s:GPGCheckRecipients() and do _always_ check the recipients before using them. --- plugin/gnupg.vim | 131 +++++++++++++++++++++++------------------------ 1 file changed, 63 insertions(+), 68 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index e32ad5b..878ee2a 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -206,10 +206,9 @@ function s:GPGDecrypt() " get the filename of the current buffer let filename=escape(expand("%:p"), '\"') - " clear GPGEncrypted, GPGRecipients, GPGUnknownRecipients and GPGOptions + " clear GPGEncrypted, GPGRecipients and GPGOptions let b:GPGEncrypted=0 let b:GPGRecipients=[] - let b:GPGUnknownRecipients=[] let b:GPGOptions=[] " find the recipients of the file @@ -258,7 +257,7 @@ function s:GPGDecrypt() let b:GPGRecipients+=[name] call s:GPGDebug(1, "name of recipient is " . name) else - let b:GPGUnknownRecipients+=[recipient] + let b:GPGRecipients+=[recipient] echohl GPGWarning echom "The recipient \"" . recipient . "\" is not in your public keyring!" echohl None @@ -362,37 +361,10 @@ function s:GPGEncrypt() endfor " check here again if all recipients are available in the keyring - let recipients = [] - if (exists("b:GPGRecipients") && type(b:GPGRecipients) == type([])) - let recipients += b:GPGRecipients - else - unlet b:GPGRecipients - endif - if (exists("b:GPGUnknownRecipients") && type(b:GPGUnknownRecipients) == type([])) - let recipients += b:GPGUnknownRecipients - else - unlet b:GPGUnknownRecipients - endif - let b:GPGRecipients = [] - let b:GPGUnknownRecipients = [] - for recipient in recipients - let gpgid=s:GPGNameToID(recipient) - if (strlen(gpgid) > 0) - if (match(b:GPGRecipients, gpgid) < 0) - let b:GPGRecipients+=[gpgid] - endif - else - if (match(b:GPGUnknownRecipients, recipient) < 0) - let b:GPGUnknownRecipients+=[recipient] - echohl GPGWarning - echom "The recipient \"" . recipient . "\" is not in your public keyring!" - echohl None - endif - end - endfor + let [ recipients, unknownrecipients ] = s:GPGCheckRecipients(b:GPGRecipients) " check if there are unknown recipients and warn - if(exists("b:GPGUnknownRecipients") && len(b:GPGUnknownRecipients) > 0) + if(len(unknownrecipients) > 0) echohl GPGWarning echom "Please use GPGEditRecipients to correct!!" echo @@ -403,8 +375,8 @@ function s:GPGEncrypt() endif " built list of recipients - if (exists("b:GPGRecipients") && len(b:GPGRecipients) > 0) - for gpgid in b:GPGRecipients + if (len(recipients) > 0) + for gpgid in recipients let options=options . " -r " . gpgid endfor else @@ -478,28 +450,28 @@ function s:GPGViewRecipients() return endif - if (exists("b:GPGRecipients")) - echo 'This file has following recipients (Unknown recipients have a prepended "!"):' - " echo the recipients - for name in b:GPGRecipients - let name=s:GPGIDToName(name) - echo name - endfor + let [ recipients, unknownrecipients ] = s:GPGCheckRecipients(b:GPGRecipients) - " echo the unknown recipients - echohl GPGWarning - for name in b:GPGUnknownRecipients - let name="!" . name - echo name - endfor + echo 'This file has following recipients (Unknown recipients have a prepended "!"):' + " echo the recipients + for name in recipients + let name=s:GPGIDToName(name) + echo name + endfor + + " echo the unknown recipients + echohl GPGWarning + for name in unknownrecipients + let name="!" . name + echo name + endfor + echohl None + + " check if there is any known recipient + if (len(recipients) == 0) + echohl GPGError + echom 'There are no known recipients!' echohl None - - " check if there is any known recipient - if (len(b:GPGRecipients) == 0) - echohl GPGError - echom 'There are no known recipients!' - echohl None - endif endif endfunction @@ -565,23 +537,16 @@ function s:GPGEditRecipients() silent put ='GPG: Closing this buffer commits changes' silent put ='GPG: ----------------------------------------------------------------------' + " get the recipients + let [ recipients, unknownrecipients ] = s:GPGCheckRecipients(getbufvar(b:GPGCorrespondingTo, "GPGRecipients")) + " put the recipients in the scratch buffer - let recipients=getbufvar(b:GPGCorrespondingTo, "GPGRecipients") - if (type(recipients) != type([])) - unlet recipients - let recipients=[] - endif for name in recipients let name=s:GPGIDToName(name) silent put =name endfor " put the unknown recipients in the scratch buffer - let unknownrecipients=getbufvar(b:GPGCorrespondingTo, "GPGUnknownRecipients") - if (type(unknownrecipients) != type([])) - unlet unknownrecipients - let unknownrecipients=[] - endif let syntaxPattern="\\(nonexxistinwordinthisbuffer" for name in unknownrecipients let name="!" . name @@ -634,7 +599,6 @@ function s:GPGFinishRecipientsBuffer() " get the recipients from the scratch buffer let recipients=[] - let unknownrecipients=[] let lines=getline(1,"$") for recipient in lines " delete all spaces at beginning and end of the recipient @@ -651,8 +615,8 @@ function s:GPGFinishRecipientsBuffer() let recipients+=[gpgid] endif else - if (match(unknownrecipients, recipient) < 0) - let unknownrecipients+=[recipient] + if (match(recipients, recipient) < 0) + let recipients+=[recipient] echohl GPGWarning echom "The recipient \"" . recipient . "\" is not in your public keyring!" echohl None @@ -664,7 +628,6 @@ function s:GPGFinishRecipientsBuffer() " write back the new recipient list to the corresponding buffer and mark it " as modified. Buffer is now for sure a encrypted buffer. call setbufvar(b:GPGCorrespondingTo, "GPGRecipients", recipients) - call setbufvar(b:GPGCorrespondingTo, "GPGUnknownRecipients", unknownrecipients) call setbufvar(b:GPGCorrespondingTo, "&mod", 1) call setbufvar(b:GPGCorrespondingTo, "GPGEncrypted", 1) @@ -836,6 +799,38 @@ function s:GPGFinishOptionsBuffer() set nomodified endfunction +" Function: s:GPGCheckRecipients(tocheck) {{{2 +" +" check if recipients are known +" Returns: two lists recipients and unknownrecipients +function s:GPGCheckRecipients(tocheck) + let recipients=[] + let unknownrecipients=[] + + if (type(a:tocheck) == type([])) + for recipient in a:tocheck + let gpgid=s:GPGNameToID(recipient) + if (strlen(gpgid) > 0) + if (match(recipients, gpgid) < 0) + let recipients+=[gpgid] + endif + else + if (match(unknownrecipients, recipient) < 0) + let unknownrecipients+=[recipient] + echohl GPGWarning + echom "The recipient \"" . recipient . "\" is not in your public keyring!" + echohl None + endif + end + endfor + endif + + call s:GPGDebug(2, "recipients are: " . string(recipients)) + call s:GPGDebug(2, "unknown recipients are: " . string(unknownrecipients)) + + return [ recipients, unknownrecipients ] +endfunction + " Function: s:GPGNameToID(name) {{{2 " " find GPG key ID corresponding to a name From 31a518148b64d6b77828a0cc06e5fe7215141b28 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Thu, 31 Jul 2008 11:43:14 +0000 Subject: [PATCH 058/115] Updated documentation. --- plugin/gnupg.vim | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 878ee2a..e719a1d 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -21,6 +21,19 @@ " Refer to ':help add-plugin', ':help add-global-plugin' and ':help " runtimepath' for more details about Vim plugins. " +" From "man 1 gpg-agent": +" +" ... +" You should always add the following lines to your .bashrc or whatever +" initialization file is used for all shell invocations: +" +" GPG_TTY=‘tty‘ +" export GPG_TTY +" +" It is important that this environment variable always reflects the out‐ +" put of the tty command. For W32 systems this option is not required. +" ... +" " Commands: " " :GPGEditRecipients @@ -63,6 +76,7 @@ " - Lars Becker for patch to make gpg2 working. " - Thomas Arendsen Hein for patch to convert encoding of gpg output " - Karl-Heinz Ruskowski for patch to fix unknown recipients and trust model +" and patient beta testing. " - Giel van Schijndel for patch to get GPG_TTY dynamically. " " Section: Plugin header {{{1 From 9278fb2d332d3d552cda3a57f94924db3c43b12a Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Mon, 4 Aug 2008 06:27:59 +0000 Subject: [PATCH 059/115] Added support for default recipients. --- plugin/gnupg.vim | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index e719a1d..befe6e7 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -68,6 +68,10 @@ " g:GPGPreferArmor " If set to 1 armored data is preferred for new files. Defaults to 0. " +" g:GPGDefaultRecipients +" If set, these recipients are used as defaults when no other recipient is +" defined. This variable is a Vim list. Default is unset. +" " Credits: " - Mathieu Clabaut for inspirations through his vimspell.vim script. " - Richard Bronosky for patch to enable ".pgp" suffix. @@ -554,6 +558,17 @@ function s:GPGEditRecipients() " get the recipients let [ recipients, unknownrecipients ] = s:GPGCheckRecipients(getbufvar(b:GPGCorrespondingTo, "GPGRecipients")) + " if there are no known or unknown recipients, use the default ones + if (exists("g:GPGDefaultRecipients") && len(recipients) == 0 && len(unknownrecipients) == 0) + if (type(g:GPGDefaultRecipients) == type([])) + let [ recipients, unknownrecipients ] = s:GPGCheckRecipients(g:GPGDefaultRecipients) + else + echohl GPGWarning + echom "g:GPGDefaultRecipients is not a Vim list, please correct this in your vimrc!" + echohl None + endif + endif + " put the recipients in the scratch buffer for name in recipients let name=s:GPGIDToName(name) @@ -635,7 +650,7 @@ function s:GPGFinishRecipientsBuffer() echom "The recipient \"" . recipient . "\" is not in your public keyring!" echohl None endif - end + endif endif endfor From a3f589f5e269a73d6a19eca096b259be9ffb04d3 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Tue, 5 Aug 2008 10:51:48 +0000 Subject: [PATCH 060/115] Make sure the variable "g:GPGDefaultRecipients" exists --- plugin/gnupg.vim | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index befe6e7..61660c2 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -152,6 +152,11 @@ function s:GPGInit() let g:GPGPreferArmor = 0 endif + " check if debugging is turned on + if (!exists("g:GPGDefaultRecipients")) + let g:GPGDefaultRecipients = [] + endif + " check if debugging is turned on if (!exists("g:GPGDebugLevel")) let g:GPGDebugLevel = 0 @@ -559,7 +564,7 @@ function s:GPGEditRecipients() let [ recipients, unknownrecipients ] = s:GPGCheckRecipients(getbufvar(b:GPGCorrespondingTo, "GPGRecipients")) " if there are no known or unknown recipients, use the default ones - if (exists("g:GPGDefaultRecipients") && len(recipients) == 0 && len(unknownrecipients) == 0) + if (len(recipients) == 0 && len(unknownrecipients) == 0) if (type(g:GPGDefaultRecipients) == type([])) let [ recipients, unknownrecipients ] = s:GPGCheckRecipients(g:GPGDefaultRecipients) else From c4b203d17c1bca08e92ea04951031f6a0e63482d Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Fri, 15 Aug 2008 09:16:44 +0000 Subject: [PATCH 061/115] Patch from Sebastian Luettich Fix issue with symmetric encryption and set recipients. --- plugin/gnupg.vim | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 61660c2..7b9d3de 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -82,6 +82,8 @@ " - Karl-Heinz Ruskowski for patch to fix unknown recipients and trust model " and patient beta testing. " - Giel van Schijndel for patch to get GPG_TTY dynamically. +" - Sebastian Luettich for patch to fix issue with symmetric encryption an set +" recipients. " " Section: Plugin header {{{1 if v:version < 700 @@ -368,6 +370,7 @@ function s:GPGEncrypt() let b:GPGOptions=[] if (exists("g:GPGPreferSymmetric") && g:GPGPreferSymmetric == 1) let b:GPGOptions+=["symmetric"] + let b:GPGRecipients=[] else let b:GPGOptions+=["encrypt"] endif @@ -387,7 +390,7 @@ function s:GPGEncrypt() let [ recipients, unknownrecipients ] = s:GPGCheckRecipients(b:GPGRecipients) " check if there are unknown recipients and warn - if(len(unknownrecipients) > 0) + if (len(unknownrecipients) > 0) echohl GPGWarning echom "Please use GPGEditRecipients to correct!!" echo From 9425fd6754d1180554efa13812da7b4d54873ba2 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Fri, 15 Aug 2008 09:34:34 +0000 Subject: [PATCH 062/115] Code beautification --- plugin/gnupg.vim | 280 +++++++++++++++++++++++------------------------ 1 file changed, 140 insertions(+), 140 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 7b9d3de..309f487 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -86,7 +86,7 @@ " recipients. " " Section: Plugin header {{{1 -if v:version < 700 +if (v:version < 700) echohl ErrorMsg | echo 'plugin gnupg.vim requires Vim version >= 7.0' | echohl None finish endif @@ -129,7 +129,7 @@ highlight default link GPGHighlightUnknownRecipient ErrorMsg function s:GPGInit() " first make sure nothing is written to ~/.viminfo while editing " an encrypted file. - set viminfo= + set viminfo = " we don't want a swap file, as it writes unencrypted data to disk set noswapfile @@ -179,21 +179,21 @@ function s:GPGInit() echohl None endif endif - let s:GPGCommand=g:GPGExecutable . " --use-agent" + let s:GPGCommand = g:GPGExecutable . " --use-agent" else - let s:GPGCommand=g:GPGExecutable . " --no-use-agent" + let s:GPGCommand = g:GPGExecutable . " --no-use-agent" endif " don't use tty in gvim " FIXME find a better way to avoid an error. " with this solution only --use-agent will work - if has("gui_running") - let s:GPGCommand=s:GPGCommand . " --no-tty" + if (has("gui_running")) + let s:GPGCommand = s:GPGCommand . " --no-tty" endif " setup shell environment for unix and windows - let s:shellredirsave=&shellredir - let s:shellsave=&shell + let s:shellredirsave = &shellredir + let s:shellsave = &shell if (match(&shell,"\\(cmd\\|command\\).execute") >= 0) " windows specific settings let s:shellredir = '>%s' @@ -203,21 +203,21 @@ function s:GPGInit() " unix specific settings let s:shellredir = &shellredir let s:shell = 'sh' - let s:stderrredirnull ='2>/dev/null' - let s:GPGCommand="LANG=C LC_ALL=C " . s:GPGCommand + let s:stderrredirnull = '2>/dev/null' + let s:GPGCommand = "LANG=C LC_ALL=C " . s:GPGCommand endif " find the supported algorithms - let &shellredir=s:shellredir - let &shell=s:shell - let output=system(s:GPGCommand . " --version") - let &shellredir=s:shellredirsave - let &shell=s:shellsave + let &shellredir = s:shellredir + let &shell = s:shell + let output = system(s:GPGCommand . " --version") + let &shellredir = s:shellredirsave + let &shell = s:shellsave - let s:GPGPubkey=substitute(output, ".*Pubkey: \\(.\\{-}\\)\n.*", "\\1", "") - let s:GPGCipher=substitute(output, ".*Cipher: \\(.\\{-}\\)\n.*", "\\1", "") - let s:GPGHash=substitute(output, ".*Hash: \\(.\\{-}\\)\n.*", "\\1", "") - let s:GPGCompress=substitute(output, ".*Compress: \\(.\\{-}\\)\n.*", "\\1", "") + let s:GPGPubkey = substitute(output, ".*Pubkey: \\(.\\{-}\\)\n.*", "\\1", "") + let s:GPGCipher = substitute(output, ".*Cipher: \\(.\\{-}\\)\n.*", "\\1", "") + let s:GPGHash = substitute(output, ".*Hash: \\(.\\{-}\\)\n.*", "\\1", "") + let s:GPGCompress = substitute(output, ".*Compress: \\(.\\{-}\\)\n.*", "\\1", "") endfunction " Function: s:GPGDecrypt() {{{2 @@ -229,34 +229,34 @@ function s:GPGDecrypt() set bin " get the filename of the current buffer - let filename=escape(expand("%:p"), '\"') + let filename = escape(expand("%:p"), '\"') " clear GPGEncrypted, GPGRecipients and GPGOptions - let b:GPGEncrypted=0 - let b:GPGRecipients=[] - let b:GPGOptions=[] + let b:GPGEncrypted = 0 + let b:GPGRecipients = [] + let b:GPGOptions = [] " find the recipients of the file - let &shellredir=s:shellredir - let &shell=s:shell - let output=system(s:GPGCommand . " --verbose --decrypt --list-only --dry-run --batch --no-use-agent --logger-fd 1 \"" . filename . "\"") - let &shellredir=s:shellredirsave - let &shell=s:shellsave + let &shellredir = s:shellredir + let &shell = s:shell + let output = system(s:GPGCommand . " --verbose --decrypt --list-only --dry-run --batch --no-use-agent --logger-fd 1 \"" . filename . "\"") + let &shellredir = s:shellredirsave + let &shell = s:shellsave call s:GPGDebug(1, "output of command '" . s:GPGCommand . " --verbose --decrypt --list-only --dry-run --batch --no-use-agent --logger-fd 1 \"" . filename . "\"' is:") call s:GPGDebug(1, ">>>>> " . output . " <<<<<") " check if the file is symmetric/asymmetric encrypted if (match(output, "gpg: encrypted with [[:digit:]]\\+ passphrase") >= 0) " file is symmetric encrypted - let b:GPGEncrypted=1 + let b:GPGEncrypted = 1 call s:GPGDebug(1, "this file is symmetric encrypted") - let b:GPGOptions+=["symmetric"] + let b:GPGOptions += ["symmetric"] " find the used cipher algorithm - let cipher=substitute(output, ".*gpg: \\([^ ]\\+\\) encrypted data.*", "\\1", "") + let cipher = substitute(output, ".*gpg: \\([^ ]\\+\\) encrypted data.*", "\\1", "") if (match(s:GPGCipher, "\\<" . cipher . "\\>") >= 0) - let b:GPGOptions+=["cipher-algo " . cipher] + let b:GPGOptions += ["cipher-algo " . cipher] call s:GPGDebug(1, "cipher-algo is " . cipher) else echohl GPGWarning @@ -266,32 +266,32 @@ function s:GPGDecrypt() endif elseif (match(output, "gpg: public key is [[:xdigit:]]\\{8}") >= 0) " file is asymmetric encrypted - let b:GPGEncrypted=1 + let b:GPGEncrypted = 1 call s:GPGDebug(1, "this file is asymmetric encrypted") - let b:GPGOptions+=["encrypt"] + let b:GPGOptions += ["encrypt"] " find the used public keys - let start=match(output, "gpg: public key is [[:xdigit:]]\\{8}") + let start = match(output, "gpg: public key is [[:xdigit:]]\\{8}") while (start >= 0) - let start=start + strlen("gpg: public key is ") - let recipient=strpart(output, start, 8) + let start = start + strlen("gpg: public key is ") + let recipient = strpart(output, start, 8) call s:GPGDebug(1, "recipient is " . recipient) - let name=s:GPGNameToID(recipient) + let name = s:GPGNameToID(recipient) if (strlen(name) > 0) - let b:GPGRecipients+=[name] + let b:GPGRecipients += [name] call s:GPGDebug(1, "name of recipient is " . name) else - let b:GPGRecipients+=[recipient] + let b:GPGRecipients += [recipient] echohl GPGWarning echom "The recipient \"" . recipient . "\" is not in your public keyring!" echohl None end - let start=match(output, "gpg: public key is [[:xdigit:]]\\{8}", start) + let start = match(output, "gpg: public key is [[:xdigit:]]\\{8}", start) endwhile else " file is not encrypted - let b:GPGEncrypted=0 + let b:GPGEncrypted = 0 call s:GPGDebug(1, "this file is not encrypted") echohl GPGWarning echom "File is not encrypted, all GPG functions disabled!" @@ -303,21 +303,21 @@ function s:GPGDecrypt() " 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"] + 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) - let &shellredir=s:shellredir - let &shell=s:shell + let &shellredir = s:shellredir + let &shell = s:shell exec "'[,']!" . s:GPGCommand . " --quiet --decrypt " . s:stderrredirnull - let &shellredir=s:shellredirsave - let &shell=s:shellsave + let &shellredir = s:shellredirsave + let &shell = s:shellsave if (v:shell_error) " message could not be decrypted silent u echohl GPGError - let blackhole=input("Message could not be decrypted! (Press ENTER)") + let blackhole = input("Message could not be decrypted! (Press ENTER)") echohl None bwipeout set nobin @@ -345,7 +345,7 @@ function s:GPGEncrypt() call s:GPGDebug(2, "saved window view " . string(s:GPGWindowView)) " store encoding and switch to a safe one - if &fileencoding != &encoding + if (&fileencoding != &encoding) let s:GPGEncoding = &encoding let &encoding = &fileencoding call s:GPGDebug(2, "encoding was \"" . s:GPGEncoding . "\", switched to \"" . &encoding . "\"") @@ -367,23 +367,23 @@ function s:GPGEncrypt() " initialize GPGOptions if not happened before if (!exists("b:GPGOptions") || len(b:GPGOptions) == 0) - let b:GPGOptions=[] + let b:GPGOptions = [] if (exists("g:GPGPreferSymmetric") && g:GPGPreferSymmetric == 1) - let b:GPGOptions+=["symmetric"] - let b:GPGRecipients=[] + let b:GPGOptions += ["symmetric"] + let b:GPGRecipients = [] else - let b:GPGOptions+=["encrypt"] + let b:GPGOptions += ["encrypt"] endif if (exists("g:GPGPreferArmor") && g:GPGPreferArmor == 1) - let b:GPGOptions+=["armor"] + let b:GPGOptions += ["armor"] endif call s:GPGDebug(1, "no options set, so using default options: " . string(b:GPGOptions)) endif " built list of options - let options="" + let options = "" for option in b:GPGOptions - let options=options . " --" . option . " " + let options = options . " --" . option . " " endfor " check here again if all recipients are available in the keyring @@ -397,13 +397,13 @@ function s:GPGEncrypt() echohl None " Let user know whats happend and copy known_recipients back to buffer - let dummy=input("Press ENTER to quit") + let dummy = input("Press ENTER to quit") endif " built list of recipients if (len(recipients) > 0) for gpgid in recipients - let options=options . " -r " . gpgid + let options = options . " -r " . gpgid endfor else if (match(b:GPGOptions, "encrypt") >= 0) @@ -416,16 +416,16 @@ function s:GPGEncrypt() endif " encrypt the buffer - let &shellredir=s:shellredir - let &shell=s:shell + let &shellredir = s:shellredir + let &shell = s:shell silent exec "'[,']!" . s:GPGCommand . " --quiet --no-encrypt-to " . options . " " . s:stderrredirnull - let &shellredir=s:shellredirsave - let &shell=s:shellsave + let &shellredir = s:shellredirsave + let &shell = s:shellsave call s:GPGDebug(1, "called gpg command is: " . "'[,']!" . s:GPGCommand . " --quiet --no-encrypt-to " . options . " " . s:stderrredirnull) if (v:shell_error) " message could not be encrypted silent u echohl GPGError - let blackhole=input("Message could not be encrypted! File might be empty! (Press ENTER)") + let blackhole = input("Message could not be encrypted! File might be empty! (Press ENTER)") echohl None bwipeout return @@ -450,7 +450,7 @@ function s:GPGEncryptPost() set nobin " restore encoding - if s:GPGEncoding != "" + if (s:GPGEncoding != "") let &encoding = s:GPGEncoding call s:GPGDebug(2, "restored encoding \"" . &encoding . "\"") endif @@ -481,14 +481,14 @@ function s:GPGViewRecipients() echo 'This file has following recipients (Unknown recipients have a prepended "!"):' " echo the recipients for name in recipients - let name=s:GPGIDToName(name) + let name = s:GPGIDToName(name) echo name endfor " echo the unknown recipients echohl GPGWarning for name in unknownrecipients - let name="!" . name + let name = "!" . name echo name endfor echohl None @@ -518,8 +518,8 @@ function s:GPGEditRecipients() if (match(bufname("%"), "^\\(GPGRecipients_\\|GPGOptions_\\)") != 0 && match(bufname("%"), "\.\\(gpg\\|asc\\|pgp\\)$") >= 0) " save buffer name - let buffername=bufname("%") - let editbuffername="GPGRecipients_" . buffername + let buffername = bufname("%") + let editbuffername = "GPGRecipients_" . buffername " check if this buffer exists if (!bufexists(editbuffername)) @@ -545,15 +545,15 @@ function s:GPGEditRecipients() endif " Mark the buffer as a scratch buffer - setlocal buftype=acwrite - setlocal bufhidden=hide + setlocal buftype = acwrite + setlocal bufhidden = hide setlocal noswapfile setlocal nowrap setlocal nobuflisted setlocal nonumber " so we know for which other buffer this edit buffer is - let b:GPGCorrespondingTo=buffername + let b:GPGCorrespondingTo = buffername " put some comments to the scratch buffer silent put ='GPG: ----------------------------------------------------------------------' @@ -579,18 +579,18 @@ function s:GPGEditRecipients() " put the recipients in the scratch buffer for name in recipients - let name=s:GPGIDToName(name) + let name = s:GPGIDToName(name) silent put =name endfor " put the unknown recipients in the scratch buffer - let syntaxPattern="\\(nonexxistinwordinthisbuffer" + let syntaxPattern = "\\(nonexxistinwordinthisbuffer" for name in unknownrecipients - let name="!" . name - let syntaxPattern=syntaxPattern . "\\|" . name + let name = "!" . name + let syntaxPattern = syntaxPattern . "\\|" . name silent put =name endfor - let syntaxPattern=syntaxPattern . "\\)" + let syntaxPattern = syntaxPattern . "\\)" " define highlight if (has("syntax") && exists("g:syntax_on")) @@ -635,25 +635,25 @@ function s:GPGFinishRecipientsBuffer() " get the recipients from the scratch buffer - let recipients=[] - let lines=getline(1,"$") + let recipients = [] + let lines = getline(1,"$") for recipient in lines " delete all spaces at beginning and end of the recipient " also delete a '!' at the beginning of the recipient - let recipient=substitute(recipient, "^[[:space:]!]*\\(.\\{-}\\)[[:space:]]*$", "\\1", "") + let recipient = substitute(recipient, "^[[:space:]!]*\\(.\\{-}\\)[[:space:]]*$", "\\1", "") " delete comment lines - let recipient=substitute(recipient, "^GPG:.*$", "", "") + let recipient = substitute(recipient, "^GPG:.*$", "", "") " only do this if the line is not empty if (strlen(recipient) > 0) - let gpgid=s:GPGNameToID(recipient) + let gpgid = s:GPGNameToID(recipient) if (strlen(gpgid) > 0) if (match(recipients, gpgid) < 0) - let recipients+=[gpgid] + let recipients += [gpgid] endif else if (match(recipients, recipient) < 0) - let recipients+=[recipient] + let recipients += [recipient] echohl GPGWarning echom "The recipient \"" . recipient . "\" is not in your public keyring!" echohl None @@ -718,8 +718,8 @@ function s:GPGEditOptions() if (match(bufname("%"), "^\\(GPGRecipients_\\|GPGOptions_\\)") != 0 && match(bufname("%"), "\.\\(gpg\\|asc\\|pgp\\)$") >= 0) " save buffer name - let buffername=bufname("%") - let editbuffername="GPGOptions_" . buffername + let buffername = bufname("%") + let editbuffername = "GPGOptions_" . buffername " check if this buffer exists if (!bufexists(editbuffername)) @@ -745,14 +745,14 @@ function s:GPGEditOptions() endif " Mark the buffer as a scratch buffer - setlocal buftype=nofile + setlocal buftype = nofile setlocal noswapfile setlocal nowrap setlocal nobuflisted setlocal nonumber " so we know for which other buffer this edit buffer is - let b:GPGCorrespondingTo=buffername + let b:GPGCorrespondingTo = buffername " put some comments to the scratch buffer silent put ='GPG: ----------------------------------------------------------------------' @@ -766,7 +766,7 @@ function s:GPGEditOptions() silent put ='GPG: ----------------------------------------------------------------------' " put the options in the scratch buffer - let options=getbufvar(b:GPGCorrespondingTo, "GPGOptions") + let options = getbufvar(b:GPGCorrespondingTo, "GPGOptions") for option in options silent put =option @@ -806,24 +806,24 @@ function s:GPGFinishOptionsBuffer() endif " clear options and unknownOptions - let options=[] - let unknownOptions=[] + let options = [] + let unknownOptions = [] " delete the autocommand autocmd! * " get the options from the scratch buffer - let lines=getline(1, "$") + let lines = getline(1, "$") for option in lines " delete all spaces at beginning and end of the option " also delete a '!' at the beginning of the option - let option=substitute(option, "^[[:space:]!]*\\(.\\{-}\\)[[:space:]]*$", "\\1", "") + let option = substitute(option, "^[[:space:]!]*\\(.\\{-}\\)[[:space:]]*$", "\\1", "") " delete comment lines - let option=substitute(option, "^GPG:.*$", "", "") + let option = substitute(option, "^GPG:.*$", "", "") " only do this if the line is not empty if (strlen(option) > 0 && match(options, option) < 0) - let options+=[option] + let options += [option] endif endfor @@ -841,19 +841,19 @@ endfunction " check if recipients are known " Returns: two lists recipients and unknownrecipients function s:GPGCheckRecipients(tocheck) - let recipients=[] - let unknownrecipients=[] + let recipients = [] + let unknownrecipients = [] if (type(a:tocheck) == type([])) for recipient in a:tocheck - let gpgid=s:GPGNameToID(recipient) + let gpgid = s:GPGNameToID(recipient) if (strlen(gpgid) > 0) if (match(recipients, gpgid) < 0) - let recipients+=[gpgid] + let recipients += [gpgid] endif else if (match(unknownrecipients, recipient) < 0) - let unknownrecipients+=[recipient] + let unknownrecipients += [recipient] echohl GPGWarning echom "The recipient \"" . recipient . "\" is not in your public keyring!" echohl None @@ -874,60 +874,60 @@ endfunction " Returns: ID for the given name function s:GPGNameToID(name) " ask gpg for the id for a name - let &shellredir=s:shellredir - let &shell=s:shell - let output=system(s:GPGCommand . " --quiet --with-colons --fixed-list-mode --list-keys \"" . a:name . "\"") - let &shellredir=s:shellredirsave - let &shell=s:shellsave + let &shellredir = s:shellredir + let &shell = s:shell + let output = system(s:GPGCommand . " --quiet --with-colons --fixed-list-mode --list-keys \"" . a:name . "\"") + let &shellredir = s:shellredirsave + let &shell = s:shellsave " when called with "--with-colons" gpg encodes its output _ALWAYS_ as UTF-8, " so convert it, if necessary - if &encoding != "utf-8" - let output=iconv(output, "utf-8", &encoding) + if (&encoding != "utf-8") + let output = iconv(output, "utf-8", &encoding) endif - let lines=split(output, "\n") + let lines = split(output, "\n") " parse the output of gpg - let pubseen=0 - let uidseen=0 - let counter=0 - let gpgids=[] - let choices="The name \"" . a:name . "\" is ambiguous. Please select the correct key:\n" + let pubseen = 0 + let uidseen = 0 + let counter = 0 + let gpgids = [] + let choices = "The name \"" . a:name . "\" is ambiguous. Please select the correct key:\n" for line in lines - let fields=split(line, ":") + let fields = split(line, ":") " search for the next uid if (pubseen == 1) if (fields[0] == "uid") if (uidseen == 0) - let choices=choices . counter . ": " . fields[9] . "\n" - let counter=counter+1 - let uidseen=1 + let choices = choices . counter . ": " . fields[9] . "\n" + let counter = counter+1 + let uidseen = 1 else - let choices=choices . " " . fields[9] . "\n" + let choices = choices . " " . fields[9] . "\n" endif else - let uidseen=0 - let pubseen=0 + let uidseen = 0 + let pubseen = 0 endif endif " search for the next pub if (pubseen == 0) if (fields[0] == "pub") - let gpgids+=[fields[4]] - let pubseen=1 + let gpgids += [fields[4]] + let pubseen = 1 endif endif endfor " counter > 1 means we have more than one results - let answer=0 + let answer = 0 if (counter > 1) - let choices=choices . "Enter number: " - let answer=input(choices, "0") + let choices = choices . "Enter number: " + let answer = input(choices, "0") while (answer == "") - let answer=input("Enter number: ", "0") + let answer = input("Enter number: ", "0") endwhile endif @@ -942,32 +942,32 @@ function s:GPGIDToName(identity) " TODO is the encryption subkey really unique? " ask gpg for the id for a name - let &shellredir=s:shellredir - let &shell=s:shell - let output=system(s:GPGCommand . " --quiet --with-colons --fixed-list-mode --list-keys " . a:identity ) - let &shellredir=s:shellredirsave - let &shell=s:shellsave + let &shellredir = s:shellredir + let &shell = s:shell + let output = system(s:GPGCommand . " --quiet --with-colons --fixed-list-mode --list-keys " . a:identity ) + let &shellredir = s:shellredirsave + let &shell = s:shellsave " when called with "--with-colons" gpg encodes its output _ALWAYS_ as UTF-8, " so convert it, if necessary - if &encoding != "utf-8" - let output=iconv(output, "utf-8", &encoding) + if (&encoding != "utf-8") + let output = iconv(output, "utf-8", &encoding) endif - let lines=split(output, "\n") + let lines = split(output, "\n") " parse the output of gpg - let pubseen=0 - let uid="" + let pubseen = 0 + let uid = "" for line in lines - let fields=split(line, ":") + let fields = split(line, ":") if (pubseen == 0) " search for the next pub if (fields[0] == "pub") - let pubseen=1 + let pubseen = 1 endif else " search for the next uid if (fields[0] == "uid") - let pubseen=0 - let uid=fields[9] + let pubseen = 0 + let uid = fields[9] break endif endif @@ -991,7 +991,7 @@ command! GPGEditRecipients call s:GPGEditRecipients() command! GPGViewOptions call s:GPGViewOptions() command! GPGEditOptions call s:GPGEditOptions() " Section: Menu {{{1 -if has("menu") +if (has("menu")) amenu Plugin.GnuPG.View\ Recipients :GPGViewRecipients amenu Plugin.GnuPG.Edit\ Recipients :GPGEditRecipients amenu Plugin.GnuPG.View\ Options :GPGViewOptions From 6a0bdcf05c75fba2c466253cca2b0872a5686709 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Fri, 15 Aug 2008 09:53:06 +0000 Subject: [PATCH 063/115] added function to cleanup on leaving vim For now create a new, empty buffer before leaving to wipe out data on console. --- plugin/gnupg.vim | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 309f487..ac6cc41 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -114,6 +114,9 @@ augroup GnuPG " undo the encryption so we are back in the normal text, directly " after the file has been written. autocmd BufWritePost,FileWritePost *.\(gpg\|asc\|pgp\) call s:GPGEncryptPost() + + " cleanup on leaving vim + autocmd VimLeave *.\(gpg\|asc\|pgp\) call s:GPGCleanup() augroup END " Section: Highlight setup {{{1 @@ -220,6 +223,16 @@ function s:GPGInit() let s:GPGCompress = substitute(output, ".*Compress: \\(.\\{-}\\)\n.*", "\\1", "") endfunction +" Function: s:GPGCleanup() {{{2 +" +" cleanup on leaving vim +" +function s:GPGCleanup() + " wipe out screen + new +only + redraw! +endfunction + " Function: s:GPGDecrypt() {{{2 " " decrypt the buffer and find all recipients of the encrypted file From aecfdde11da5989ce93c6243595945fbff0cbc17 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Fri, 15 Aug 2008 12:50:33 +0000 Subject: [PATCH 064/115] Make sure that sensitive data is never written unencrypted. It is better to write an empty buffer to the file instead. --- plugin/gnupg.vim | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index ac6cc41..f65af90 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -132,7 +132,7 @@ highlight default link GPGHighlightUnknownRecipient ErrorMsg function s:GPGInit() " first make sure nothing is written to ~/.viminfo while editing " an encrypted file. - set viminfo = + set viminfo= " we don't want a swap file, as it writes unencrypted data to disk set noswapfile @@ -436,11 +436,13 @@ function s:GPGEncrypt() let &shell = s:shellsave call s:GPGDebug(1, "called gpg command is: " . "'[,']!" . s:GPGCommand . " --quiet --no-encrypt-to " . options . " " . s:stderrredirnull) if (v:shell_error) " message could not be encrypted - silent u + " delete content of the buffer to be sure no data is written unencrypted + " content will be recovered in GPGEncryptPost() + silent normal! 1GdG + echohl GPGError let blackhole = input("Message could not be encrypted! File might be empty! (Press ENTER)") echohl None - bwipeout return endif @@ -558,8 +560,8 @@ function s:GPGEditRecipients() endif " Mark the buffer as a scratch buffer - setlocal buftype = acwrite - setlocal bufhidden = hide + setlocal buftype=acwrite + setlocal bufhidden=hide setlocal noswapfile setlocal nowrap setlocal nobuflisted @@ -758,7 +760,7 @@ function s:GPGEditOptions() endif " Mark the buffer as a scratch buffer - setlocal buftype = nofile + setlocal buftype=nofile setlocal noswapfile setlocal nowrap setlocal nobuflisted From 478aeb919683b2d4cd73a4aabeccc262535ea373 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Sun, 5 Oct 2008 20:41:52 +0000 Subject: [PATCH 065/115] Correctly use backticks for determining the tty --- plugin/gnupg.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index f65af90..55b02ff 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -27,7 +27,7 @@ " You should always add the following lines to your .bashrc or whatever " initialization file is used for all shell invocations: " -" GPG_TTY=‘tty‘ +" GPG_TTY=`tty` " export GPG_TTY " " It is important that this environment variable always reflects the out‐ From 12ac7f9f03370a6e491db885f13bd0d858d558c8 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Wed, 15 Oct 2008 11:43:54 +0000 Subject: [PATCH 066/115] Use has("unix") to determine between windows and unix. --- plugin/gnupg.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 55b02ff..c62e642 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -197,7 +197,7 @@ function s:GPGInit() " setup shell environment for unix and windows let s:shellredirsave = &shellredir let s:shellsave = &shell - if (match(&shell,"\\(cmd\\|command\\).execute") >= 0) + if (has("unix")) " windows specific settings let s:shellredir = '>%s' let s:shell = &shell From 54faa743b20441fce02add1ddc012d655bcbe19f Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Fri, 21 Nov 2008 18:43:36 +0000 Subject: [PATCH 067/115] Correct error in redirection. --- plugin/gnupg.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index c62e642..6a64170 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -201,7 +201,7 @@ function s:GPGInit() " windows specific settings let s:shellredir = '>%s' let s:shell = &shell - let s:stderrredirnull = '2>nul' + let s:stderrredirnull = '2>null' else " unix specific settings let s:shellredir = &shellredir From 666d1e10f5fa797a8ed16036b6853e108465bc23 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Mon, 15 Dec 2008 13:16:44 +0000 Subject: [PATCH 068/115] Unix/windows specific settings done right :-) --- plugin/gnupg.vim | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 6a64170..6d60170 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -198,16 +198,16 @@ function s:GPGInit() let s:shellredirsave = &shellredir let s:shellsave = &shell if (has("unix")) - " windows specific settings - let s:shellredir = '>%s' - let s:shell = &shell - let s:stderrredirnull = '2>null' - else " unix specific settings let s:shellredir = &shellredir let s:shell = 'sh' let s:stderrredirnull = '2>/dev/null' let s:GPGCommand = "LANG=C LC_ALL=C " . s:GPGCommand + else + " windows specific settings + let s:shellredir = '>%s' + let s:shell = &shell + let s:stderrredirnull = '2>nul' endif " find the supported algorithms From b426430ff8b28bf8f524950c9d73818e55f85844 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Wed, 29 Apr 2009 05:19:55 +0000 Subject: [PATCH 069/115] This is just a stupid change, more ore less to update Id and Revision keywords --- plugin/gnupg.vim | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 6d60170..e784dcb 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -1012,4 +1012,5 @@ if (has("menu")) amenu Plugin.GnuPG.View\ Options :GPGViewOptions amenu Plugin.GnuPG.Edit\ Options :GPGEditOptions endif -" vim600: foldmethod=marker:foldlevel=0 + +" vim600: set foldmethod=marker foldlevel=0 : From da53fc98f0a1c78b6c9aa60fdec439426533a9cb Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Wed, 29 Apr 2009 07:34:40 +0000 Subject: [PATCH 070/115] Added section "Known Issues:" to documentation. --- plugin/gnupg.vim | 50 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index e784dcb..0c54460 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -7,7 +7,7 @@ " See http://www.gnu.org/copyleft/gpl.txt " Section: Documentation {{{1 " Description: -" +" " This script implements transparent editing of gpg encrypted files. The " filename must have a ".gpg", ".pgp" or ".asc" suffix. When opening such " a file the content is decrypted, when opening a new file the script will @@ -15,7 +15,7 @@ " encrypted to all recipients before it is written. The script turns off " viminfo and swapfile to increase security. " -" Installation: +" Installation: " " Copy the gnupg.vim file to the $HOME/.vim/plugin directory. " Refer to ':help add-plugin', ':help add-global-plugin' and ':help @@ -72,18 +72,40 @@ " If set, these recipients are used as defaults when no other recipient is " defined. This variable is a Vim list. Default is unset. " +" Known Issues: +" +" gvim can't decryt files + +" This is caused by the fact that a running gvim has no TTY and thus gpg is +" not able to ask for the passphrase by itself. This is a problem for Windows +" and Linux versions of gvim and could not be solved unless a "terminal +" emulation" is implemented for gvim. To circumvent this you have to use any +" combination of gpg-agent and a graphical pinentry program: +" +" - gpg-agent only: +" you need to provide the passphrase for the needed key to gpg-agent +" in a terminal before you open files with gvim which require this key. +" +" - pinentry only: +" you will get a popup window every time you open a file that needs to +" be decrypted. +" +" - gpgagent and pinentry: +" you will get a popup window the first time you open a file that +" needs to be decrypted. +" " Credits: -" - Mathieu Clabaut for inspirations through his vimspell.vim script. -" - Richard Bronosky for patch to enable ".pgp" suffix. -" - Erik Remmelzwaal for patch to enable windows support and patient beta -" testing. -" - Lars Becker for patch to make gpg2 working. -" - Thomas Arendsen Hein for patch to convert encoding of gpg output -" - Karl-Heinz Ruskowski for patch to fix unknown recipients and trust model -" and patient beta testing. -" - Giel van Schijndel for patch to get GPG_TTY dynamically. -" - Sebastian Luettich for patch to fix issue with symmetric encryption an set -" recipients. +" - Mathieu Clabaut for inspirations through his vimspell.vim script. +" - Richard Bronosky for patch to enable ".pgp" suffix. +" - Erik Remmelzwaal for patch to enable windows support and patient beta +" testing. +" - Lars Becker for patch to make gpg2 working. +" - Thomas Arendsen Hein for patch to convert encoding of gpg output +" - Karl-Heinz Ruskowski for patch to fix unknown recipients and trust model +" and patient beta testing. +" - Giel van Schijndel for patch to get GPG_TTY dynamically. +" - Sebastian Luettich for patch to fix issue with symmetric encryption an set +" recipients. " " Section: Plugin header {{{1 if (v:version < 700) @@ -132,7 +154,7 @@ highlight default link GPGHighlightUnknownRecipient ErrorMsg function s:GPGInit() " first make sure nothing is written to ~/.viminfo while editing " an encrypted file. - set viminfo= + set viminfo= " we don't want a swap file, as it writes unencrypted data to disk set noswapfile From a817a3bfa1db0e2bb2c0b1cdbe9ed97c331be08c Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Mon, 11 May 2009 08:47:47 +0000 Subject: [PATCH 071/115] Tim Swast patch for signed files --- plugin/gnupg.vim | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 0c54460..378db93 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -68,6 +68,9 @@ " g:GPGPreferArmor " If set to 1 armored data is preferred for new files. Defaults to 0. " +" g:GPGPreferSign +" If set to 1 signed data is preferred for new files. Defaults to 0. +" " g:GPGDefaultRecipients " If set, these recipients are used as defaults when no other recipient is " defined. This variable is a Vim list. Default is unset. @@ -106,6 +109,7 @@ " - Giel van Schijndel for patch to get GPG_TTY dynamically. " - Sebastian Luettich for patch to fix issue with symmetric encryption an set " recipients. +" - Tim Swast for patch to generate signed files " " Section: Plugin header {{{1 if (v:version < 700) @@ -179,6 +183,11 @@ function s:GPGInit() let g:GPGPreferArmor = 0 endif + " check if signed files are preferred + if (!exists("g:GPGPreferSign")) + let g:GPGPreferSign = 0 + endif + " check if debugging is turned on if (!exists("g:GPGDefaultRecipients")) let g:GPGDefaultRecipients = [] @@ -412,6 +421,9 @@ function s:GPGEncrypt() if (exists("g:GPGPreferArmor") && g:GPGPreferArmor == 1) let b:GPGOptions += ["armor"] endif + if (exists("g:GPGPreferSign") && g:GPGPreferSign == 1) + let b:GPGOptions += ["sign"] + endif call s:GPGDebug(1, "no options set, so using default options: " . string(b:GPGOptions)) endif From 8ae4e4973662f1fea8a9b18f2537e73283624547 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Wed, 27 May 2009 06:54:33 +0000 Subject: [PATCH 072/115] Just another useless change to update revision date. --- plugin/gnupg.vim | 1 + 1 file changed, 1 insertion(+) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 378db93..635c369 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -98,6 +98,7 @@ " needs to be decrypted. " " Credits: +" " - Mathieu Clabaut for inspirations through his vimspell.vim script. " - Richard Bronosky for patch to enable ".pgp" suffix. " - Erik Remmelzwaal for patch to enable windows support and patient beta From b932a58be92de927d3fed3de445fbabc1e4cd1ea Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Wed, 27 May 2009 07:10:20 +0000 Subject: [PATCH 073/115] Another documentation change. --- plugin/gnupg.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 635c369..7282e35 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -77,7 +77,7 @@ " " Known Issues: " -" gvim can't decryt files +" In some cases gvim can't decryt files " This is caused by the fact that a running gvim has no TTY and thus gpg is " not able to ask for the passphrase by itself. This is a problem for Windows From 16412c1b18034b2af0eae64b8138781c0b5cd607 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Mon, 8 Jun 2009 12:32:13 +0000 Subject: [PATCH 074/115] Add more info about a key Show ID and creation time when viewing, editing or choosing a recipient (suggested by Curt Sampson) --- plugin/gnupg.vim | 52 +++++++++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 7282e35..1106587 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -146,6 +146,9 @@ augroup GnuPG autocmd VimLeave *.\(gpg\|asc\|pgp\) call s:GPGCleanup() augroup END +" Section: Constants {{{1 +let s:GPGMagicString = "\t \t" + " Section: Highlight setup {{{1 highlight default link GPGWarning WarningMsg highlight default link GPGError ErrorMsg @@ -607,10 +610,11 @@ function s:GPGEditRecipients() " put some comments to the scratch buffer silent put ='GPG: ----------------------------------------------------------------------' - silent put ='GPG: Please edit the list of recipients, one recipient per line' - silent put ='GPG: Unknown recipients have a prepended \"!\"' - silent put ='GPG: Lines beginning with \"GPG:\" are removed automatically' - silent put ='GPG: Closing this buffer commits changes' + silent put ='GPG: Please edit the list of recipients, one recipient per line.' + silent put ='GPG: Unknown recipients have a prepended \"!\".' + silent put ='GPG: Lines beginning with \"GPG:\" are removed automatically.' + silent put ='GPG: Data after recipients between and including \"(\" and \")\" is ignored.' + silent put ='GPG: Closing this buffer commits changes.' silent put ='GPG: ----------------------------------------------------------------------' " get the recipients @@ -649,6 +653,7 @@ function s:GPGEditRecipients() highlight link GPGUnknownRecipient GPGHighlightUnknownRecipient syntax match GPGComment "^GPG:.*$" + exec 'syntax match GPGComment "' . s:GPGMagicString . '.*$"' highlight clear GPGComment highlight link GPGComment Comment endif @@ -688,9 +693,13 @@ function s:GPGFinishRecipientsBuffer() let recipients = [] let lines = getline(1,"$") for recipient in lines + " delete all text after magic string + let recipient = substitute(recipient, s:GPGMagicString . ".*$", "", "") + " delete all spaces at beginning and end of the recipient " also delete a '!' at the beginning of the recipient let recipient = substitute(recipient, "^[[:space:]!]*\\(.\\{-}\\)[[:space:]]*$", "\\1", "") + " delete comment lines let recipient = substitute(recipient, "^GPG:.*$", "", "") @@ -808,11 +817,11 @@ function s:GPGEditOptions() silent put ='GPG: ----------------------------------------------------------------------' silent put ='GPG: THERE IS NO CHECK OF THE ENTERED OPTIONS!' silent put ='GPG: YOU NEED TO KNOW WHAT YOU ARE DOING!' - silent put ='GPG: IF IN DOUBT, QUICKLY EXIT USING :x OR :bd' - silent put ='GPG: Please edit the list of options, one option per line' - silent put ='GPG: Please refer to the gpg documentation for valid options' - silent put ='GPG: Lines beginning with \"GPG:\" are removed automatically' - silent put ='GPG: Closing this buffer commits changes' + silent put ='GPG: IF IN DOUBT, QUICKLY EXIT USING :x OR :bd.' + silent put ='GPG: Please edit the list of options, one option per line.' + silent put ='GPG: Please refer to the gpg documentation for valid options.' + silent put ='GPG: Lines beginning with \"GPG:\" are removed automatically.' + silent put ='GPG: Closing this buffer commits changes.' silent put ='GPG: ----------------------------------------------------------------------' " put the options in the scratch buffer @@ -939,7 +948,6 @@ function s:GPGNameToID(name) " parse the output of gpg let pubseen = 0 - let uidseen = 0 let counter = 0 let gpgids = [] let choices = "The name \"" . a:name . "\" is ambiguous. Please select the correct key:\n" @@ -948,15 +956,8 @@ function s:GPGNameToID(name) " search for the next uid if (pubseen == 1) if (fields[0] == "uid") - if (uidseen == 0) - let choices = choices . counter . ": " . fields[9] . "\n" - let counter = counter+1 - let uidseen = 1 - else - let choices = choices . " " . fields[9] . "\n" - endif + let choices = choices . " " . fields[9] . "\n" else - let uidseen = 0 let pubseen = 0 endif endif @@ -964,7 +965,14 @@ function s:GPGNameToID(name) " search for the next pub if (pubseen == 0) if (fields[0] == "pub") - let gpgids += [fields[4]] + let identity = fields[4] + let gpgids += [identity] + if exists("*strftime") + let choices = choices . counter . ": ID: 0x" . identity . " created at " . strftime("%c", fields[5]) . "\n" + else + let choices = choices . counter . ": ID: 0x" . identity . "\n" + endif + let counter = counter+1 let pubseen = 1 endif endif @@ -1017,7 +1025,11 @@ function s:GPGIDToName(identity) else " search for the next uid if (fields[0] == "uid") let pubseen = 0 - let uid = fields[9] + if exists("*strftime") + let uid = fields[9] . s:GPGMagicString . "(ID: 0x" . a:identity . " created at " . strftime("%c", fields[5]) . ")" + else + let uid = fields[9] . s:GPGMagicString . "(ID: 0x" . a:identity . ")" + endif break endif endif From b9a0a498d5a9dce9e562b7e31eb284436f53143a Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Fri, 17 Jul 2009 09:02:15 +0000 Subject: [PATCH 075/115] Prefix "GnuPG: " to all debug messages --- plugin/gnupg.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 1106587..adb8ac2 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -192,7 +192,7 @@ function s:GPGInit() let g:GPGPreferSign = 0 endif - " check if debugging is turned on + " start with empty default recipients if none is defined so far if (!exists("g:GPGDefaultRecipients")) let g:GPGDefaultRecipients = [] endif @@ -1043,7 +1043,7 @@ endfunction " output debug message, if this message has high enough importance function s:GPGDebug(level, text) if (g:GPGDebugLevel >= a:level) - echom a:text + echom "GnuPG: " . a:text endif endfunction From 062524c107cf8560dc4f47bf6a6e213cc855b09b Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Thu, 23 Jul 2009 07:02:52 +0000 Subject: [PATCH 076/115] Code beautification --- plugin/gnupg.vim | 57 +++++++++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 20 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index adb8ac2..3fe1ba9 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -1,12 +1,14 @@ -" Name: gnupg.vim -" Version: $Id$ -" Author: Markus Braun -" Summary: Vim plugin for transparent editing of gpg encrypted files. -" Licence: This program is free software; you can redistribute it and/or -" modify it under the terms of the GNU General Public License. -" See http://www.gnu.org/copyleft/gpl.txt +" Name: gnupg.vim +" Version: $Id$ +" Author: Markus Braun +" Summary: Vim plugin for transparent editing of gpg encrypted files. +" Licence: This program is free software; you can redistribute it and/or +" modify it under the terms of the GNU General Public License. +" See http://www.gnu.org/copyleft/gpl.txt +" " Section: Documentation {{{1 -" Description: +" +" Description: {{{2 " " This script implements transparent editing of gpg encrypted files. The " filename must have a ".gpg", ".pgp" or ".asc" suffix. When opening such @@ -15,7 +17,7 @@ " encrypted to all recipients before it is written. The script turns off " viminfo and swapfile to increase security. " -" Installation: +" Installation: {{{2 " " Copy the gnupg.vim file to the $HOME/.vim/plugin directory. " Refer to ':help add-plugin', ':help add-global-plugin' and ':help @@ -34,7 +36,7 @@ " put of the tty command. For W32 systems this option is not required. " ... " -" Commands: +" Commands: {{{2 " " :GPGEditRecipients " Opens a scratch buffer to change the list of recipients. Recipients that @@ -53,7 +55,7 @@ " :GPGViewOptions " Prints the list of options. " -" Variables: +" Variables: {{{2 " " g:GPGExecutable " If set used as gpg executable, otherwise the system chooses what is run @@ -75,7 +77,7 @@ " If set, these recipients are used as defaults when no other recipient is " defined. This variable is a Vim list. Default is unset. " -" Known Issues: +" Known Issues: {{{2 " " In some cases gvim can't decryt files @@ -97,7 +99,7 @@ " you will get a popup window the first time you open a file that " needs to be decrypted. " -" Credits: +" Credits: {{{2 " " - Mathieu Clabaut for inspirations through his vimspell.vim script. " - Richard Bronosky for patch to enable ".pgp" suffix. @@ -113,18 +115,21 @@ " - Tim Swast for patch to generate signed files " " Section: Plugin header {{{1 + +" guard against multiple loads {{{2 +if (exists("g:loaded_gnupg") || &cp || exists("#BufReadPre#*.\(gpg\|asc\|pgp\)")) + finish +endif +let g:loaded_gnupg = "$Revision$" + +" check for correct vim version {{{2 if (v:version < 700) echohl ErrorMsg | echo 'plugin gnupg.vim requires Vim version >= 7.0' | echohl None finish endif -if (exists("g:loaded_gnupg") || &cp || exists("#BufReadPre#*.\(gpg\|asc\|pgp\)")) - finish -endif - -let g:loaded_gnupg = "$Revision$" - " Section: Autocmd setup {{{1 + augroup GnuPG autocmd! @@ -147,14 +152,17 @@ augroup GnuPG augroup END " Section: Constants {{{1 + let s:GPGMagicString = "\t \t" " Section: Highlight setup {{{1 + highlight default link GPGWarning WarningMsg highlight default link GPGError ErrorMsg highlight default link GPGHighlightUnknownRecipient ErrorMsg " Section: Functions {{{1 + " Function: s:GPGInit() {{{2 " " initialize the plugin @@ -670,6 +678,7 @@ endfunction " Function: s:GPGFinishRecipientsBuffer() {{{2 " " create a new recipient list from RecipientsBuffer +" function s:GPGFinishRecipientsBuffer() " guard for unencrypted files if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) @@ -849,6 +858,7 @@ endfunction " Function: s:GPGFinishOptionsBuffer() {{{2 " " create a new option list from OptionsBuffer +" function s:GPGFinishOptionsBuffer() " guard for unencrypted files if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) @@ -899,6 +909,7 @@ endfunction " " check if recipients are known " Returns: two lists recipients and unknownrecipients +" function s:GPGCheckRecipients(tocheck) let recipients = [] let unknownrecipients = [] @@ -931,6 +942,7 @@ endfunction " " find GPG key ID corresponding to a name " Returns: ID for the given name +" function s:GPGNameToID(name) " ask gpg for the id for a name let &shellredir = s:shellredir @@ -996,6 +1008,7 @@ endfunction " " find name corresponding to a GPG key ID " Returns: Name for the given ID +" function s:GPGIDToName(identity) " TODO is the encryption subkey really unique? @@ -1041,18 +1054,22 @@ endfunction " Function: s:GPGDebug(level, text) {{{2 " " output debug message, if this message has high enough importance +" function s:GPGDebug(level, text) if (g:GPGDebugLevel >= a:level) echom "GnuPG: " . a:text endif endfunction -" Section: Command definitions {{{1 +" Section: Commands {{{1 + command! GPGViewRecipients call s:GPGViewRecipients() command! GPGEditRecipients call s:GPGEditRecipients() command! GPGViewOptions call s:GPGViewOptions() command! GPGEditOptions call s:GPGEditOptions() + " Section: Menu {{{1 + if (has("menu")) amenu Plugin.GnuPG.View\ Recipients :GPGViewRecipients amenu Plugin.GnuPG.Edit\ Recipients :GPGEditRecipients From e53b79872ae4ef6717534b20f45a871ea364a086 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Thu, 24 Sep 2009 07:18:49 +0000 Subject: [PATCH 077/115] Show an error if the buffer is not prepared for writing an encrypted file. --- plugin/gnupg.vim | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 3fe1ba9..f9cece0 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -414,9 +414,9 @@ function s:GPGEncrypt() set bin " guard for unencrypted files - if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) - echohl GPGWarning - echom "File is not encrypted, all GPG functions disabled!" + if (!exists("b:GPGEncrypted") || b:GPGEncrypted == 0) + echohl GPGError + let blackhole = input("Message could not be encrypted! File might be empty! (Press ENTER)") echohl None return endif From 616192b2365cce1dd05c01d17c1a4233218cbd14 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Fri, 20 Nov 2009 20:39:49 +0000 Subject: [PATCH 078/115] Debug messages --- plugin/gnupg.vim | 2 ++ 1 file changed, 2 insertions(+) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index f9cece0..97a387d 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -365,6 +365,8 @@ function s:GPGDecrypt() " 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") + call s:GPGDebug(1, "command is '[,']!" . s:GPGCommand . " --quiet --decrypt " . s:stderrredirnull) let &shellredir = s:shellredir let &shell = s:shell exec "'[,']!" . s:GPGCommand . " --quiet --decrypt " . s:stderrredirnull From f2e0a30de819515cd7e843bcdec1741c27121454 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Tue, 19 Jan 2010 09:58:57 +0000 Subject: [PATCH 079/115] Added more debug informations --- plugin/gnupg.vim | 117 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 99 insertions(+), 18 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 97a387d..61fae04 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -168,6 +168,8 @@ highlight default link GPGHighlightUnknownRecipient ErrorMsg " initialize the plugin " function s:GPGInit() + call s:GPGDebug(3, ">>>>>>>> Entering s:GPGInit()") + " first make sure nothing is written to ~/.viminfo while editing " an encrypted file. set viminfo= @@ -205,11 +207,6 @@ function s:GPGInit() let g:GPGDefaultRecipients = [] endif - " check if debugging is turned on - if (!exists("g:GPGDebugLevel")) - let g:GPGDebugLevel = 0 - endif - " print version call s:GPGDebug(1, "gnupg.vim ". g:loaded_gnupg) @@ -254,16 +251,25 @@ function s:GPGInit() endif " find the supported algorithms + let GPGExec = s:GPGCommand . " --version" + call s:GPGDebug(2, "command: ". GPGExec) let &shellredir = s:shellredir let &shell = s:shell - let output = system(s:GPGCommand . " --version") + let output = system(GPGExec) let &shellredir = s:shellredirsave let &shell = s:shellsave + call s:GPGDebug(2, "output: ". output) let s:GPGPubkey = substitute(output, ".*Pubkey: \\(.\\{-}\\)\n.*", "\\1", "") let s:GPGCipher = substitute(output, ".*Cipher: \\(.\\{-}\\)\n.*", "\\1", "") let s:GPGHash = substitute(output, ".*Hash: \\(.\\{-}\\)\n.*", "\\1", "") - let s:GPGCompress = substitute(output, ".*Compress: \\(.\\{-}\\)\n.*", "\\1", "") + let s:GPGCompress = substitute(output, ".*Compress.\\{-}: \\(.\\{-}\\)\n.*", "\\1", "") + + call s:GPGDebug(2, "public key algorithms: " . s:GPGPubkey) + call s:GPGDebug(2, "cipher algorithms: " . s:GPGCipher) + call s:GPGDebug(2, "hashing algorithms: " . s:GPGHash) + call s:GPGDebug(2, "compression algorithms: " . s:GPGCompress) + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGInit()") endfunction " Function: s:GPGCleanup() {{{2 @@ -271,9 +277,13 @@ endfunction " cleanup on leaving vim " function s:GPGCleanup() + call s:GPGDebug(3, ">>>>>>>> Entering s:GPGCleanup()") + " wipe out screen new +only redraw! + + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGCleanup()") endfunction " Function: s:GPGDecrypt() {{{2 @@ -281,6 +291,8 @@ endfunction " decrypt the buffer and find all recipients of the encrypted file " function s:GPGDecrypt() + call s:GPGDebug(3, ">>>>>>>> Entering s:GPGDecrypt()") + " switch to binary mode to read the encrypted file set bin @@ -293,13 +305,14 @@ function s:GPGDecrypt() let b:GPGOptions = [] " find the recipients of the file + let GPGExec = s:GPGCommand . " --verbose --decrypt --list-only --dry-run --batch --no-use-agent --logger-fd 1 \"" . filename . "\"" + call s:GPGDebug(3, "command: " . GPGExec) let &shellredir = s:shellredir let &shell = s:shell - let output = system(s:GPGCommand . " --verbose --decrypt --list-only --dry-run --batch --no-use-agent --logger-fd 1 \"" . filename . "\"") + let output = system(GPGExec) let &shellredir = s:shellredirsave let &shell = s:shellsave - call s:GPGDebug(1, "output of command '" . s:GPGCommand . " --verbose --decrypt --list-only --dry-run --batch --no-use-agent --logger-fd 1 \"" . filename . "\"' is:") - call s:GPGDebug(1, ">>>>> " . output . " <<<<<") + call s:GPGDebug(3, "output: ". output) " check if the file is symmetric/asymmetric encrypted if (match(output, "gpg: encrypted with [[:digit:]]\\+ passphrase") >= 0) @@ -353,6 +366,7 @@ function s:GPGDecrypt() echom "File is not encrypted, all GPG functions disabled!" echohl None set nobin + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGDecrypt()") return endif @@ -366,10 +380,11 @@ function s:GPGDecrypt() " since even with the --quiet option passphrase typos will be reported, " we must redirect stderr (using shell temporarily) call s:GPGDebug(1, "decrypting file") - call s:GPGDebug(1, "command is '[,']!" . s:GPGCommand . " --quiet --decrypt " . s:stderrredirnull) + let GPGExec = "'[,']!" . s:GPGCommand . " --quiet --decrypt " . s:stderrredirnull + call s:GPGDebug(1, "command: " . GPGExec) let &shellredir = s:shellredir let &shell = s:shell - exec "'[,']!" . s:GPGCommand . " --quiet --decrypt " . s:stderrredirnull + exec GPGExec let &shellredir = s:shellredirsave let &shell = s:shellsave if (v:shell_error) " message could not be decrypted @@ -379,6 +394,7 @@ function s:GPGDecrypt() echohl None bwipeout set nobin + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGDecrypt()") return endif @@ -391,6 +407,8 @@ function s:GPGDecrypt() " refresh screen redraw! + + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGDecrypt()") endfunction " Function: s:GPGEncrypt() {{{2 @@ -398,6 +416,8 @@ endfunction " encrypts the buffer to all previous recipients " function s:GPGEncrypt() + call s:GPGDebug(3, ">>>>>>>> Entering s:GPGEncrypt()") + " save window view let s:GPGWindowView = winsaveview() call s:GPGDebug(2, "saved window view " . string(s:GPGWindowView)) @@ -420,6 +440,7 @@ function s:GPGEncrypt() echohl GPGError let blackhole = input("Message could not be encrypted! File might be empty! (Press ENTER)") echohl None + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEncrypt()") return endif @@ -477,12 +498,13 @@ function s:GPGEncrypt() endif " encrypt the buffer + let GPGExec = "'[,']!" . s:GPGCommand . " --quiet --no-encrypt-to " . options . " " . s:stderrredirnull + call s:GPGDebug(1, "command: " . GPGExec) let &shellredir = s:shellredir let &shell = s:shell - silent exec "'[,']!" . s:GPGCommand . " --quiet --no-encrypt-to " . options . " " . s:stderrredirnull + silent exec GPGExec let &shellredir = s:shellredirsave let &shell = s:shellsave - call s:GPGDebug(1, "called gpg command is: " . "'[,']!" . s:GPGCommand . " --quiet --no-encrypt-to " . options . " " . s:stderrredirnull) if (v:shell_error) " message could not be encrypted " delete content of the buffer to be sure no data is written unencrypted " content will be recovered in GPGEncryptPost() @@ -491,9 +513,11 @@ function s:GPGEncrypt() echohl GPGError let blackhole = input("Message could not be encrypted! File might be empty! (Press ENTER)") echohl None + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEncrypt()") return endif + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEncrypt()") endfunction " Function: s:GPGEncryptPost() {{{2 @@ -501,8 +525,11 @@ endfunction " undo changes don by encrypt, after writing " function s:GPGEncryptPost() + call s:GPGDebug(3, ">>>>>>>> Entering s:GPGEncryptPost()") + " guard for unencrypted files if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEncryptPost()") return endif @@ -524,6 +551,8 @@ function s:GPGEncryptPost() " refresh screen redraw! + + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEncryptPost()") endfunction " Function: s:GPGViewRecipients() {{{2 @@ -531,11 +560,14 @@ endfunction " echo the recipients " function s:GPGViewRecipients() + call s:GPGDebug(3, ">>>>>>>> Entering s:GPGViewRecipients()") + " guard for unencrypted files if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) echohl GPGWarning echom "File is not encrypted, all GPG functions disabled!" echohl None + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGViewRecipients()") return endif @@ -562,6 +594,8 @@ function s:GPGViewRecipients() echom 'There are no known recipients!' echohl None endif + + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGViewRecipients()") endfunction " Function: s:GPGEditRecipients() {{{2 @@ -569,11 +603,14 @@ endfunction " create a scratch buffer with all recipients to add/remove recipients " function s:GPGEditRecipients() + call s:GPGDebug(3, ">>>>>>>> Entering s:GPGEditRecipients()") + " guard for unencrypted files if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) echohl GPGWarning echom "File is not encrypted, all GPG functions disabled!" echohl None + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEditRecipients()") return endif @@ -675,6 +712,8 @@ function s:GPGEditRecipients() silent normal! G endif + + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEditRecipients()") endfunction " Function: s:GPGFinishRecipientsBuffer() {{{2 @@ -682,11 +721,14 @@ endfunction " create a new recipient list from RecipientsBuffer " function s:GPGFinishRecipientsBuffer() + call s:GPGDebug(3, ">>>>>>>> Entering s:GPGFinishRecipientsBuffer()") + " guard for unencrypted files if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) echohl GPGWarning echom "File is not encrypted, all GPG functions disabled!" echohl None + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGFinishRecipientsBuffer()") return endif @@ -747,6 +789,8 @@ function s:GPGFinishRecipientsBuffer() " reset modified flag set nomodified + + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGFinishRecipientsBuffer()") endfunction " Function: s:GPGViewOptions() {{{2 @@ -754,11 +798,14 @@ endfunction " echo the recipients " function s:GPGViewOptions() + call s:GPGDebug(3, ">>>>>>>> Entering s:GPGViewOptions()") + " guard for unencrypted files if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) echohl GPGWarning echom "File is not encrypted, all GPG functions disabled!" echohl None + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGViewOptions()") return endif @@ -769,6 +816,8 @@ function s:GPGViewOptions() echo option endfor endif + + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGViewOptions()") endfunction " Function: s:GPGEditOptions() {{{2 @@ -776,11 +825,14 @@ endfunction " create a scratch buffer with all recipients to add/remove recipients " function s:GPGEditOptions() + call s:GPGDebug(3, ">>>>>>>> Entering s:GPGEditOptions()") + " guard for unencrypted files if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) echohl GPGWarning echom "File is not encrypted, all GPG functions disabled!" echohl None + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEditOptions()") return endif @@ -855,6 +907,8 @@ function s:GPGEditOptions() highlight link GPGComment Comment endif endif + + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEditOptions()") endfunction " Function: s:GPGFinishOptionsBuffer() {{{2 @@ -862,11 +916,14 @@ endfunction " create a new option list from OptionsBuffer " function s:GPGFinishOptionsBuffer() + call s:GPGDebug(3, ">>>>>>>> Entering s:GPGFinishOptionsBuffer()") + " guard for unencrypted files if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) echohl GPGWarning echom "File is not encrypted, all GPG functions disabled!" echohl None + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGFinishOptionsBuffer()") return endif @@ -905,6 +962,8 @@ function s:GPGFinishOptionsBuffer() " reset modified flag set nomodified + + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGFinishOptionsBuffer()") endfunction " Function: s:GPGCheckRecipients(tocheck) {{{2 @@ -913,6 +972,8 @@ endfunction " Returns: two lists recipients and unknownrecipients " function s:GPGCheckRecipients(tocheck) + call s:GPGDebug(3, ">>>>>>>> Entering s:GPGCheckRecipients()") + let recipients = [] let unknownrecipients = [] @@ -937,6 +998,7 @@ function s:GPGCheckRecipients(tocheck) call s:GPGDebug(2, "recipients are: " . string(recipients)) call s:GPGDebug(2, "unknown recipients are: " . string(unknownrecipients)) + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGCheckRecipients()") return [ recipients, unknownrecipients ] endfunction @@ -946,12 +1008,17 @@ endfunction " Returns: ID for the given name " function s:GPGNameToID(name) + call s:GPGDebug(3, ">>>>>>>> Entering s:GPGNameToID()") + " ask gpg for the id for a name + let GPGExec = s:GPGCommand . " --quiet --with-colons --fixed-list-mode --list-keys \"" . a:name . "\"" + call s:GPGDebug(2, "command: ". GPGExec) let &shellredir = s:shellredir let &shell = s:shell - let output = system(s:GPGCommand . " --quiet --with-colons --fixed-list-mode --list-keys \"" . a:name . "\"") + let output = system(GPGExec) let &shellredir = s:shellredirsave let &shell = s:shellsave + call s:GPGDebug(2, "output: ". output) " when called with "--with-colons" gpg encodes its output _ALWAYS_ as UTF-8, " so convert it, if necessary @@ -1003,6 +1070,7 @@ function s:GPGNameToID(name) endwhile endif + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGIDToName()") return get(gpgids, answer, "") endfunction @@ -1012,14 +1080,19 @@ endfunction " Returns: Name for the given ID " function s:GPGIDToName(identity) + call s:GPGDebug(3, ">>>>>>>> Entering s:GPGIDToName()") + " TODO is the encryption subkey really unique? " ask gpg for the id for a name + let GPGExec = s:GPGCommand . " --quiet --with-colons --fixed-list-mode --list-keys " . a:identity + call s:GPGDebug(2, "command: ". GPGExec) let &shellredir = s:shellredir let &shell = s:shell - let output = system(s:GPGCommand . " --quiet --with-colons --fixed-list-mode --list-keys " . a:identity ) + let output = system(GPGExec) let &shellredir = s:shellredirsave let &shell = s:shellsave + call s:GPGDebug(2, "output: ". output) " when called with "--with-colons" gpg encodes its output _ALWAYS_ as UTF-8, " so convert it, if necessary @@ -1050,16 +1123,24 @@ function s:GPGIDToName(identity) endif endfor + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGIDToName()") return uid endfunction " Function: s:GPGDebug(level, text) {{{2 " " output debug message, if this message has high enough importance +" only define function if GPGDebugLevel set at all " function s:GPGDebug(level, text) - if (g:GPGDebugLevel >= a:level) - echom "GnuPG: " . a:text + if exists("g:GPGDebugLevel") && g:GPGDebugLevel >= a:level + if exists("g:GPGDebugLog") + exec "redir >> " . g:GPGDebugLog + echom "GnuPG: " . a:text + redir END + else + echom "GnuPG: " . a:text + endif endif endfunction From 6d50997fff50fe34d0e4978afe2abc69d572414b Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Tue, 5 Jul 2011 12:49:45 +0200 Subject: [PATCH 080/115] Use '/bin/sh' instead of 'sh' as shell --- plugin/gnupg.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 61fae04..38cf471 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -240,7 +240,7 @@ function s:GPGInit() if (has("unix")) " unix specific settings let s:shellredir = &shellredir - let s:shell = 'sh' + let s:shell = '/bin/sh' let s:stderrredirnull = '2>/dev/null' let s:GPGCommand = "LANG=C LC_ALL=C " . s:GPGCommand else From d48f7364aa8fef8cea51bc5ab5115977125ed6c6 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Tue, 5 Jul 2011 12:51:12 +0200 Subject: [PATCH 081/115] Renamed GPGExec to commandline --- plugin/gnupg.vim | 53 +++++++++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 38cf471..406beba 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -250,12 +250,23 @@ function s:GPGInit() let s:stderrredirnull = '2>nul' endif + call s:GPGDebug(3, "shellredirsave: " . s:shellredirsave) + call s:GPGDebug(3, "shellsave: " . s:shellsave) + + call s:GPGDebug(3, "shell: " . s:shell) + call s:GPGDebug(3, "shellcmdflag: " . &shellcmdflag) + call s:GPGDebug(3, "shellxquote: " . &shellxquote) + call s:GPGDebug(3, "shellredir: " . s:shellredir) + call s:GPGDebug(3, "stderrredirnull: " . s:stderrredirnull) + + call s:GPGDebug(3, "shell implementation: " . resolve(s:shell)) + " find the supported algorithms - let GPGExec = s:GPGCommand . " --version" - call s:GPGDebug(2, "command: ". GPGExec) + let commandline = s:GPGCommand . " --version" + call s:GPGDebug(2, "command: ". commandline) let &shellredir = s:shellredir let &shell = s:shell - let output = system(GPGExec) + let output = system(commandline) let &shellredir = s:shellredirsave let &shell = s:shellsave call s:GPGDebug(2, "output: ". output) @@ -305,11 +316,11 @@ function s:GPGDecrypt() let b:GPGOptions = [] " find the recipients of the file - let GPGExec = s:GPGCommand . " --verbose --decrypt --list-only --dry-run --batch --no-use-agent --logger-fd 1 \"" . filename . "\"" - call s:GPGDebug(3, "command: " . GPGExec) + let commandline = s:GPGCommand . " --verbose --decrypt --list-only --dry-run --batch --no-use-agent --logger-fd 1 \"" . filename . "\"" + call s:GPGDebug(3, "command: " . commandline) let &shellredir = s:shellredir let &shell = s:shell - let output = system(GPGExec) + let output = system(commandline) let &shellredir = s:shellredirsave let &shell = s:shellsave call s:GPGDebug(3, "output: ". output) @@ -380,11 +391,11 @@ function s:GPGDecrypt() " 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 GPGExec = "'[,']!" . s:GPGCommand . " --quiet --decrypt " . s:stderrredirnull - call s:GPGDebug(1, "command: " . GPGExec) + let commandline = "'[,']!" . s:GPGCommand . " --quiet --decrypt " . s:stderrredirnull + call s:GPGDebug(1, "command: " . commandline) let &shellredir = s:shellredir let &shell = s:shell - exec GPGExec + execute commandline let &shellredir = s:shellredirsave let &shell = s:shellsave if (v:shell_error) " message could not be decrypted @@ -498,11 +509,11 @@ function s:GPGEncrypt() endif " encrypt the buffer - let GPGExec = "'[,']!" . s:GPGCommand . " --quiet --no-encrypt-to " . options . " " . s:stderrredirnull - call s:GPGDebug(1, "command: " . GPGExec) + let commandline = "'[,']!" . s:GPGCommand . " --quiet --no-encrypt-to " . options . " " . s:stderrredirnull + call s:GPGDebug(1, "command: " . commandline) let &shellredir = s:shellredir let &shell = s:shell - silent exec GPGExec + silent execute commandline let &shellredir = s:shellredirsave let &shell = s:shellsave if (v:shell_error) " message could not be encrypted @@ -695,12 +706,12 @@ function s:GPGEditRecipients() " define highlight if (has("syntax") && exists("g:syntax_on")) - exec('syntax match GPGUnknownRecipient "' . syntaxPattern . '"') + execute 'syntax match GPGUnknownRecipient "' . syntaxPattern . '"' highlight clear GPGUnknownRecipient highlight link GPGUnknownRecipient GPGHighlightUnknownRecipient syntax match GPGComment "^GPG:.*$" - exec 'syntax match GPGComment "' . s:GPGMagicString . '.*$"' + execute 'syntax match GPGComment "' . s:GPGMagicString . '.*$"' highlight clear GPGComment highlight link GPGComment Comment endif @@ -1011,11 +1022,11 @@ function s:GPGNameToID(name) call s:GPGDebug(3, ">>>>>>>> Entering s:GPGNameToID()") " ask gpg for the id for a name - let GPGExec = s:GPGCommand . " --quiet --with-colons --fixed-list-mode --list-keys \"" . a:name . "\"" - call s:GPGDebug(2, "command: ". GPGExec) + let commandline = s:GPGCommand . " --quiet --with-colons --fixed-list-mode --list-keys \"" . a:name . "\"" + call s:GPGDebug(2, "command: ". commandline) let &shellredir = s:shellredir let &shell = s:shell - let output = system(GPGExec) + let output = system(commandline) let &shellredir = s:shellredirsave let &shell = s:shellsave call s:GPGDebug(2, "output: ". output) @@ -1085,11 +1096,11 @@ function s:GPGIDToName(identity) " TODO is the encryption subkey really unique? " ask gpg for the id for a name - let GPGExec = s:GPGCommand . " --quiet --with-colons --fixed-list-mode --list-keys " . a:identity - call s:GPGDebug(2, "command: ". GPGExec) + let commandline = s:GPGCommand . " --quiet --with-colons --fixed-list-mode --list-keys " . a:identity + call s:GPGDebug(2, "command: ". commandline) let &shellredir = s:shellredir let &shell = s:shell - let output = system(GPGExec) + let output = system(commandline) let &shellredir = s:shellredirsave let &shell = s:shellsave call s:GPGDebug(2, "output: ". output) @@ -1135,7 +1146,7 @@ endfunction function s:GPGDebug(level, text) if exists("g:GPGDebugLevel") && g:GPGDebugLevel >= a:level if exists("g:GPGDebugLog") - exec "redir >> " . g:GPGDebugLog + execute "redir >> " . g:GPGDebugLog echom "GnuPG: " . a:text redir END else From 468b9fe59c914088802bcecc8aa820d770b29194 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Tue, 19 Jan 2010 17:03:48 +0000 Subject: [PATCH 082/115] Force sh/zsh/bash/dash compatible shellredir setting. --- plugin/gnupg.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 406beba..eaeb1e8 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -239,7 +239,7 @@ function s:GPGInit() let s:shellsave = &shell if (has("unix")) " unix specific settings - let s:shellredir = &shellredir + let s:shellredir = ">%s 2>&1" let s:shell = '/bin/sh' let s:stderrredirnull = '2>/dev/null' let s:GPGCommand = "LANG=C LC_ALL=C " . s:GPGCommand From f93f113717caa915c089a802140a38fcc818f3bf Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Wed, 27 Jan 2010 08:18:04 +0000 Subject: [PATCH 083/115] Added remark about keychain and seahorse --- plugin/gnupg.vim | 3 +++ 1 file changed, 3 insertions(+) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index eaeb1e8..fa12d53 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -36,6 +36,9 @@ " put of the tty command. For W32 systems this option is not required. " ... " +" Most distributions provide software to ease handling of gpg and gpg-agent. +" Examples are keychain or seahorse. +" " Commands: {{{2 " " :GPGEditRecipients From 1e5a796e0b5ec899104f3615d371d98185274683 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Tue, 16 Feb 2010 07:31:53 +0000 Subject: [PATCH 084/115] Patch by James Vega Default to g:GPGPreferArmor = 1 for "*.asc" files if g:GPGPreferArmor is not set by the user. --- plugin/gnupg.vim | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index fa12d53..7e74af1 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -71,7 +71,8 @@ " If set to 1 symmetric encryption is preferred for new files. Defaults to 0. " " g:GPGPreferArmor -" If set to 1 armored data is preferred for new files. Defaults to 0. +" If set to 1 armored data is preferred for new files. Defaults to 0 +" unless a "*.asc" file is being edited. " " g:GPGPreferSign " If set to 1 signed data is preferred for new files. Defaults to 0. @@ -82,7 +83,7 @@ " " Known Issues: {{{2 " -" In some cases gvim can't decryt files +" In some cases gvim can't decrypt files " This is caused by the fact that a running gvim has no TTY and thus gpg is " not able to ask for the passphrase by itself. This is a problem for Windows @@ -197,7 +198,12 @@ function s:GPGInit() " check if armored files are preferred if (!exists("g:GPGPreferArmor")) - let g:GPGPreferArmor = 0 + " .asc files should be armored as that's what the extension is used for + if expand('') =~ '\.asc$' + let g:GPGPreferArmor = 1 + else + let g:GPGPreferArmor = 0 + endif endif " check if signed files are preferred From 82e70cf70a85f53e2672015dff0a9a1b0e2a1410 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Tue, 16 Feb 2010 07:39:06 +0000 Subject: [PATCH 085/115] Patch by James Vega Ignore duplicate recipients if the exactly same recipient is contained in multiple keyrings. --- plugin/gnupg.vim | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 7e74af1..6bef158 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -1051,8 +1051,16 @@ function s:GPGNameToID(name) let pubseen = 0 let counter = 0 let gpgids = [] + let duplicates = {} let choices = "The name \"" . a:name . "\" is ambiguous. Please select the correct key:\n" for line in lines + if !has_key(duplicates, line) + let duplicates[line] = 1 + else + " Exact line has already been seen. Probably multiple keyrings being + " searched with the same data. + continue + endif let fields = split(line, ":") " search for the next uid if (pubseen == 1) @@ -1090,7 +1098,7 @@ function s:GPGNameToID(name) endwhile endif - call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGIDToName()") + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGNameToID()") return get(gpgids, answer, "") endfunction From 05a3e4e1f69aa5b91b16abc6b6502e2abd822b75 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Tue, 16 Feb 2010 07:40:41 +0000 Subject: [PATCH 086/115] Reworked patch by James Vega because I don't like "continue". --- plugin/gnupg.vim | 47 +++++++++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 6bef158..1171b4f 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -1054,35 +1054,34 @@ function s:GPGNameToID(name) let duplicates = {} let choices = "The name \"" . a:name . "\" is ambiguous. Please select the correct key:\n" for line in lines + + " check if this line has already been processed if !has_key(duplicates, line) let duplicates[line] = 1 - else - " Exact line has already been seen. Probably multiple keyrings being - " searched with the same data. - continue - endif - let fields = split(line, ":") - " search for the next uid - if (pubseen == 1) - if (fields[0] == "uid") - let choices = choices . " " . fields[9] . "\n" - else - let pubseen = 0 - endif - endif - " search for the next pub - if (pubseen == 0) - if (fields[0] == "pub") - let identity = fields[4] - let gpgids += [identity] - if exists("*strftime") - let choices = choices . counter . ": ID: 0x" . identity . " created at " . strftime("%c", fields[5]) . "\n" + let fields = split(line, ":") + " search for the next uid + if (pubseen == 1) + if (fields[0] == "uid") + let choices = choices . " " . fields[9] . "\n" else - let choices = choices . counter . ": ID: 0x" . identity . "\n" + let pubseen = 0 + endif + endif + + " search for the next pub + if (pubseen == 0) + if (fields[0] == "pub") + let identity = fields[4] + let gpgids += [identity] + if exists("*strftime") + let choices = choices . counter . ": ID: 0x" . identity . " created at " . strftime("%c", fields[5]) . "\n" + else + let choices = choices . counter . ": ID: 0x" . identity . "\n" + endif + let counter = counter+1 + let pubseen = 1 endif - let counter = counter+1 - let pubseen = 1 endif endif From 9e7c686de56b5fceb01c6d2f405f8217f00d2c01 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Tue, 16 Feb 2010 07:46:46 +0000 Subject: [PATCH 087/115] Patch by James Vega Make use of shellescape() and fnameescape() functions which better escape filenames than previous handmade calls to escape(). --- plugin/gnupg.vim | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 1171b4f..25ffa0a 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -131,6 +131,10 @@ if (v:version < 700) echohl ErrorMsg | echo 'plugin gnupg.vim requires Vim version >= 7.0' | echohl None finish endif +if !(exists("*shellescape") && exists("*fnameescape")) + echohl ErrorMsg | echo 'plugin gnupg.vim requires Vim with the shellescape() and fnameescape() functions' | echohl None + finish +endif " Section: Autocmd setup {{{1 @@ -317,7 +321,7 @@ function s:GPGDecrypt() set bin " get the filename of the current buffer - let filename = escape(expand("%:p"), '\"') + let filename = expand("%:p") " clear GPGEncrypted, GPGRecipients and GPGOptions let b:GPGEncrypted = 0 @@ -325,7 +329,7 @@ function s:GPGDecrypt() let b:GPGOptions = [] " find the recipients of the file - let commandline = s:GPGCommand . " --verbose --decrypt --list-only --dry-run --batch --no-use-agent --logger-fd 1 \"" . filename . "\"" + let commandline = s:GPGCommand . " --verbose --decrypt --list-only --dry-run --batch --no-use-agent --logger-fd 1 " . shellescape(filename) call s:GPGDebug(3, "command: " . commandline) let &shellredir = s:shellredir let &shell = s:shell @@ -422,8 +426,8 @@ function s:GPGDecrypt() set nobin " call the autocommand for the file minus .gpg$ - execute ":doautocmd BufReadPost " . escape(expand("%:r"), ' *?\"'."'") - call s:GPGDebug(2, "called autocommand for " . escape(expand("%:r"), ' *?\"'."'")) + execute ":doautocmd BufReadPost " . fnameescape(expand("%:r")) + call s:GPGDebug(2, "called autocommand for " . fnameescape(expand("%:r"))) " refresh screen redraw! @@ -644,7 +648,7 @@ function s:GPGEditRecipients() " check if this buffer exists if (!bufexists(editbuffername)) " create scratch buffer - execute 'silent! split ' . escape(editbuffername, ' *?\"'."'") + execute 'silent! split ' . fnameescape(editbuffername) " add a autocommand to regenerate the recipients after a write autocmd BufHidden,BufUnload,BufWriteCmd call s:GPGFinishRecipientsBuffer() @@ -654,7 +658,7 @@ function s:GPGEditRecipients() execute 'silent! ' . bufwinnr(editbuffername) . "wincmd w" else " split scratch buffer window - execute 'silent! sbuffer ' . escape(editbuffername, ' *?\"'."'") + execute 'silent! sbuffer ' . fnameescape(editbuffername) " add a autocommand to regenerate the recipients after a write autocmd BufHidden,BufUnload,BufWriteCmd call s:GPGFinishRecipientsBuffer() @@ -708,7 +712,7 @@ function s:GPGEditRecipients() let syntaxPattern = "\\(nonexxistinwordinthisbuffer" for name in unknownrecipients let name = "!" . name - let syntaxPattern = syntaxPattern . "\\|" . name + let syntaxPattern = syntaxPattern . "\\|" . fnameescape(name) silent put =name endfor let syntaxPattern = syntaxPattern . "\\)" @@ -866,7 +870,7 @@ function s:GPGEditOptions() " check if this buffer exists if (!bufexists(editbuffername)) " create scratch buffer - execute 'silent! split ' . escape(editbuffername, ' *?\"'."'") + execute 'silent! split ' . fnameescape(editbuffername) " add a autocommand to regenerate the options after a write autocmd BufHidden,BufUnload,BufWriteCmd call s:GPGFinishOptionsBuffer() @@ -876,7 +880,7 @@ function s:GPGEditOptions() execute 'silent! ' . bufwinnr(editbuffername) . "wincmd w" else " split scratch buffer window - execute 'silent! sbuffer ' . escape(editbuffername, ' *?\"'."'") + execute 'silent! sbuffer ' . fnameescape(editbuffername) " add a autocommand to regenerate the options after a write autocmd BufHidden,BufUnload,BufWriteCmd call s:GPGFinishOptionsBuffer() @@ -1031,7 +1035,7 @@ function s:GPGNameToID(name) call s:GPGDebug(3, ">>>>>>>> Entering s:GPGNameToID()") " ask gpg for the id for a name - let commandline = s:GPGCommand . " --quiet --with-colons --fixed-list-mode --list-keys \"" . a:name . "\"" + let commandline = s:GPGCommand . " --quiet --with-colons --fixed-list-mode --list-keys " . shellescape(a:name) call s:GPGDebug(2, "command: ". commandline) let &shellredir = s:shellredir let &shell = s:shell From efd23b4440e4d86ccf5ad5a8950657965ed6fd38 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Tue, 16 Feb 2010 07:52:11 +0000 Subject: [PATCH 088/115] Patch by Erik Remmelzwaal Make gnupg.vim work better on win32 --- plugin/gnupg.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 25ffa0a..d542472 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -240,10 +240,10 @@ function s:GPGInit() let s:GPGCommand = g:GPGExecutable . " --no-use-agent" endif - " don't use tty in gvim + " don't use tty in gvim except for windows: we get their a tty for free. " FIXME find a better way to avoid an error. " with this solution only --use-agent will work - if (has("gui_running")) + if (has("gui_running") && !has("gui_win32")) let s:GPGCommand = s:GPGCommand . " --no-tty" endif From a8838d0e86109aac467e913c58512ff8887c80b7 Mon Sep 17 00:00:00 2001 From: Markus Braun Date: Tue, 16 Feb 2010 07:56:18 +0000 Subject: [PATCH 089/115] Updated credits. --- plugin/gnupg.vim | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index d542472..f400f02 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -110,13 +110,15 @@ " - Erik Remmelzwaal for patch to enable windows support and patient beta " testing. " - Lars Becker for patch to make gpg2 working. -" - Thomas Arendsen Hein for patch to convert encoding of gpg output +" - Thomas Arendsen Hein for patch to convert encoding of gpg output. " - Karl-Heinz Ruskowski for patch to fix unknown recipients and trust model " and patient beta testing. " - Giel van Schijndel for patch to get GPG_TTY dynamically. " - Sebastian Luettich for patch to fix issue with symmetric encryption an set " recipients. -" - Tim Swast for patch to generate signed files +" - Tim Swast for patch to generate signed files. +" - James Vega for patches for better '*.asc' handling, better filename +" escaping and better handling of multiple keyrings. " " Section: Plugin header {{{1 From bf67f5561afd7189f5c7ec5446c9b1dc2ac6f0ef Mon Sep 17 00:00:00 2001 From: James Vega Date: Thu, 28 Oct 2010 01:30:47 -0400 Subject: [PATCH 090/115] Refactor to use *WriteCmd/*ReadCmd autocommands. With this change, we're able to properly handle errors from shell commands. This means no more overwriting the original file when an incorrect password is entered or some other similar scenario. Also, move the handling of entering recipients to gpg itself instead of mimicking that in Vim itself. Signed-off-by: James Vega --- plugin/gnupg.vim | 150 ++++++++++++++++++----------------------------- 1 file changed, 57 insertions(+), 93 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index f400f02..f2ebba9 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -127,6 +127,7 @@ if (exists("g:loaded_gnupg") || &cp || exists("#BufReadPre#*.\(gpg\|asc\|pgp\)") finish endif let g:loaded_gnupg = "$Revision$" +let s:GPGInitRun = 0 " check for correct vim version {{{2 if (v:version < 700) @@ -143,19 +144,14 @@ endif augroup GnuPG autocmd! - " initialize the internal variables - autocmd BufNewFile,BufReadPre,FileReadPre *.\(gpg\|asc\|pgp\) call s:GPGInit() - " force the user to edit the recipient list if he opens a new file and public - " keys are preferred - autocmd BufNewFile *.\(gpg\|asc\|pgp\) if (exists("g:GPGPreferSymmetric") && g:GPGPreferSymmetric == 0) | call s:GPGEditRecipients() | endif " do the decryption - autocmd BufReadPost,FileReadPost *.\(gpg\|asc\|pgp\) call s:GPGDecrypt() + autocmd BufReadCmd,FileReadCmd *.\(gpg\|asc\|pgp\) call s:GPGInit() + autocmd BufReadCmd,FileReadCmd *.\(gpg\|asc\|pgp\) call s:GPGDecrypt() + autocmd BufReadCmd *.\(gpg\|asc\|pgp\) call s:GPGBufReadPost() " convert all text to encrypted text before writing - autocmd BufWritePre,FileWritePre *.\(gpg\|asc\|pgp\) call s:GPGEncrypt() - " undo the encryption so we are back in the normal text, directly - " after the file has been written. - autocmd BufWritePost,FileWritePost *.\(gpg\|asc\|pgp\) call s:GPGEncryptPost() + autocmd BufWriteCmd,FileWriteCmd *.\(gpg\|asc\|pgp\) call s:GPGInit() + autocmd BufWriteCmd,FileWriteCmd *.\(gpg\|asc\|pgp\) call s:GPGEncrypt() " cleanup on leaving vim autocmd VimLeave *.\(gpg\|asc\|pgp\) call s:GPGCleanup() @@ -178,6 +174,9 @@ highlight default link GPGHighlightUnknownRecipient ErrorMsg " initialize the plugin " function s:GPGInit() + if s:GPGInitRun + return + endif call s:GPGDebug(3, ">>>>>>>> Entering s:GPGInit()") " first make sure nothing is written to ~/.viminfo while editing @@ -185,7 +184,7 @@ function s:GPGInit() set viminfo= " we don't want a swap file, as it writes unencrypted data to disk - set noswapfile + setl noswapfile " check what gpg command to use if (!exists("g:GPGExecutable")) @@ -296,6 +295,7 @@ function s:GPGInit() call s:GPGDebug(2, "hashing algorithms: " . s:GPGHash) call s:GPGDebug(2, "compression algorithms: " . s:GPGCompress) call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGInit()") + let s:GPGInitRun = 1 endfunction " Function: s:GPGCleanup() {{{2 @@ -319,11 +319,13 @@ endfunction function s:GPGDecrypt() call s:GPGDebug(3, ">>>>>>>> Entering s:GPGDecrypt()") - " switch to binary mode to read the encrypted file - set bin - " get the filename of the current buffer - let filename = expand("%:p") + let filename = expand(":p") + + " File doesn't exist yet, so force recipients + if empty(glob(filename)) + return + endif " clear GPGEncrypted, GPGRecipients and GPGOptions let b:GPGEncrypted = 0 @@ -391,7 +393,7 @@ function s:GPGDecrypt() echohl GPGWarning echom "File is not encrypted, all GPG functions disabled!" echohl None - set nobin + silent exe '.r ' . fnameescape(filename) call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGDecrypt()") return endif @@ -406,7 +408,7 @@ function s:GPGDecrypt() " 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 commandline = "'[,']!" . s:GPGCommand . " --quiet --decrypt " . s:stderrredirnull + let commandline = "r !" . s:GPGCommand . ' --quiet --decrypt ' . shellescape(filename, 1) . ' ' . s:stderrredirnull call s:GPGDebug(1, "command: " . commandline) let &shellredir = s:shellredir let &shell = s:shell @@ -414,29 +416,29 @@ function s:GPGDecrypt() let &shellredir = s:shellredirsave let &shell = s:shellsave if (v:shell_error) " message could not be decrypted - silent u echohl GPGError let blackhole = input("Message could not be decrypted! (Press ENTER)") echohl None - bwipeout - set nobin + silent bwipeout! call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGDecrypt()") return endif - " turn off binary mode - set nobin - - " call the autocommand for the file minus .gpg$ - execute ":doautocmd BufReadPost " . fnameescape(expand("%:r")) - call s:GPGDebug(2, "called autocommand for " . fnameescape(expand("%:r"))) - " refresh screen redraw! call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGDecrypt()") endfunction +function s:GPGBufReadPost() + call s:GPGDebug(3, ">>>>>>>> Entering s:GPGBufReadPost()") + silent 1delete + " call the autocommand for the file minus .gpg$ + execute ':doautocmd BufReadPost ' . fnameescape(expand(':r')) + call s:GPGDebug(2, 'called autocommand for ' . fnameescape(expand(':r'))) + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGBufReadPost()") +endfunction + " Function: s:GPGEncrypt() {{{2 " " encrypts the buffer to all previous recipients @@ -444,10 +446,6 @@ endfunction function s:GPGEncrypt() call s:GPGDebug(3, ">>>>>>>> Entering s:GPGEncrypt()") - " save window view - let s:GPGWindowView = winsaveview() - call s:GPGDebug(2, "saved window view " . string(s:GPGWindowView)) - " store encoding and switch to a safe one if (&fileencoding != &encoding) let s:GPGEncoding = &encoding @@ -458,13 +456,10 @@ function s:GPGEncrypt() call s:GPGDebug(2, "encoding and fileencoding are the same (\"" . &encoding . "\"), not switching") endif - " switch buffer to binary mode - set bin - " guard for unencrypted files - if (!exists("b:GPGEncrypted") || b:GPGEncrypted == 0) + if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) echohl GPGError - let blackhole = input("Message could not be encrypted! File might be empty! (Press ENTER)") + let blackhole = input("Message could not be encrypted! (Press ENTER)") echohl None call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEncrypt()") return @@ -494,6 +489,10 @@ function s:GPGEncrypt() let options = options . " --" . option . " " endfor + if (!exists('b:GPGRecipients')) + let b:GPGRecipients = [] + endif + " check here again if all recipients are available in the keyring let [ recipients, unknownrecipients ] = s:GPGCheckRecipients(b:GPGRecipients) @@ -513,57 +512,17 @@ function s:GPGEncrypt() for gpgid in recipients let options = options . " -r " . gpgid endfor - else - if (match(b:GPGOptions, "encrypt") >= 0) - echohl GPGError - echom "There are no recipients!!" - echom "Please use GPGEditRecipients to correct!!" - echo - echohl None - endif endif " encrypt the buffer - let commandline = "'[,']!" . s:GPGCommand . " --quiet --no-encrypt-to " . options . " " . s:stderrredirnull + let destfile = tempname() + let commandline = "'[,']w !" . s:GPGCommand . ' --quiet --no-encrypt-to ' . options . '>' . shellescape(destfile, 1) . ' ' . s:stderrredirnull call s:GPGDebug(1, "command: " . commandline) let &shellredir = s:shellredir let &shell = s:shell silent execute commandline let &shellredir = s:shellredirsave let &shell = s:shellsave - if (v:shell_error) " message could not be encrypted - " delete content of the buffer to be sure no data is written unencrypted - " content will be recovered in GPGEncryptPost() - silent normal! 1GdG - - echohl GPGError - let blackhole = input("Message could not be encrypted! File might be empty! (Press ENTER)") - echohl None - call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEncrypt()") - return - endif - - call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEncrypt()") -endfunction - -" Function: s:GPGEncryptPost() {{{2 -" -" undo changes don by encrypt, after writing -" -function s:GPGEncryptPost() - call s:GPGDebug(3, ">>>>>>>> Entering s:GPGEncryptPost()") - - " guard for unencrypted files - if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) - call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEncryptPost()") - return - endif - - " undo encryption of buffer content - silent u - - " switch back from binary mode - set nobin " restore encoding if (s:GPGEncoding != "") @@ -571,14 +530,19 @@ function s:GPGEncryptPost() call s:GPGDebug(2, "restored encoding \"" . &encoding . "\"") endif - " restore window view - call winrestview(s:GPGWindowView) - call s:GPGDebug(2, "restored window view" . string(s:GPGWindowView)) + if (v:shell_error) " message could not be encrypted + " Command failed, so clean up the tempfile + call delete(destfile) + echohl GPGError + let blackhole = input("Message could not be encrypted! (Press ENTER)") + echohl None + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEncrypt()") + return + endif - " refresh screen - redraw! - - call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEncryptPost()") + call rename(destfile, expand('')) + setl nomodified + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEncrypt()") endfunction " Function: s:GPGViewRecipients() {{{2 @@ -667,7 +631,7 @@ function s:GPGEditRecipients() endif " empty the buffer - silent normal! 1GdG + silent %delete endif " Mark the buffer as a scratch buffer @@ -732,10 +696,10 @@ function s:GPGEditRecipients() endif " delete the empty first line - silent normal! 1Gdd + silent 1delete " jump to the first recipient - silent normal! G + silent $ endif @@ -814,7 +778,7 @@ function s:GPGFinishRecipientsBuffer() endif " reset modified flag - set nomodified + setl nomodified call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGFinishRecipientsBuffer()") endfunction @@ -889,7 +853,7 @@ function s:GPGEditOptions() endif " empty the buffer - silent normal! 1GdG + silent %delete endif " Mark the buffer as a scratch buffer @@ -921,10 +885,10 @@ function s:GPGEditOptions() endfor " delete the empty first line - silent normal! 1Gdd + silent 1delete " jump to the first option - silent normal! G + silent $ " define highlight if (has("syntax") && exists("g:syntax_on")) @@ -987,7 +951,7 @@ function s:GPGFinishOptionsBuffer() call setbufvar(b:GPGCorrespondingTo, "&mod", 1) " reset modified flag - set nomodified + setl nomodified call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGFinishOptionsBuffer()") endfunction From 6e178a6dd1310b44e382bc6680035ffbb5a349d6 Mon Sep 17 00:00:00 2001 From: James Vega Date: Tue, 24 May 2011 01:14:45 -0400 Subject: [PATCH 091/115] Check for our BufReadCmd to see if the plugin has been loaded. Signed-off-by: James Vega --- plugin/gnupg.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index f2ebba9..9b6bcec 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -123,7 +123,7 @@ " Section: Plugin header {{{1 " guard against multiple loads {{{2 -if (exists("g:loaded_gnupg") || &cp || exists("#BufReadPre#*.\(gpg\|asc\|pgp\)")) +if (exists("g:loaded_gnupg") || &cp || exists("#BufReadCmd*.\(gpg\|asc\|pgp\)")) finish endif let g:loaded_gnupg = "$Revision$" From a37434ab5dfc2d515b311cccdbf7af881bbb5272 Mon Sep 17 00:00:00 2001 From: James Vega Date: Tue, 24 May 2011 01:16:05 -0400 Subject: [PATCH 092/115] Bump minimum Vim version to 7.2 The two argument form of shellescape() is now being used, and that was introduced in the pre-releases of 7.2. Signed-off-by: James Vega --- plugin/gnupg.vim | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 9b6bcec..4621ede 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -130,12 +130,8 @@ let g:loaded_gnupg = "$Revision$" let s:GPGInitRun = 0 " check for correct vim version {{{2 -if (v:version < 700) - echohl ErrorMsg | echo 'plugin gnupg.vim requires Vim version >= 7.0' | echohl None - finish -endif -if !(exists("*shellescape") && exists("*fnameescape")) - echohl ErrorMsg | echo 'plugin gnupg.vim requires Vim with the shellescape() and fnameescape() functions' | echohl None +if (v:version < 702) + echohl ErrorMsg | echo 'plugin gnupg.vim requires Vim version >= 7.2' | echohl None finish endif From f23c3c764350efa85c96c64bb76caae907717ce1 Mon Sep 17 00:00:00 2001 From: James Vega Date: Tue, 24 May 2011 01:21:19 -0400 Subject: [PATCH 093/115] Set 'noshelltemp' when executing gnupg This is another step to prevent writing out sensitive information to disk. Typically, when running an external command, Vim writes the command input (the buffer in this case) to a file in a private temp directory. When 'noshelltemp' is set and the system supports it, Vim uses pipes to the child process to handle this instead. Signed-off-by: James Vega --- plugin/gnupg.vim | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 4621ede..3d5b12c 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -247,6 +247,11 @@ function s:GPGInit() " setup shell environment for unix and windows let s:shellredirsave = &shellredir let s:shellsave = &shell + let s:shelltempsave = &shelltemp + " noshelltemp isn't currently supported on Windows, but it doesn't cause any + " errors and this future proofs us against requiring changes if Windows + " gains noshelltemp functionality + let s:shelltemp = 0 if (has("unix")) " unix specific settings let s:shellredir = ">%s 2>&1" @@ -262,6 +267,7 @@ function s:GPGInit() call s:GPGDebug(3, "shellredirsave: " . s:shellredirsave) call s:GPGDebug(3, "shellsave: " . s:shellsave) + call s:GPGDebug(3, "shelltempsave: " . s:shelltempsave) call s:GPGDebug(3, "shell: " . s:shell) call s:GPGDebug(3, "shellcmdflag: " . &shellcmdflag) @@ -333,9 +339,11 @@ function s:GPGDecrypt() call s:GPGDebug(3, "command: " . commandline) let &shellredir = s:shellredir let &shell = s:shell + let &shelltemp = s:shelltemp let output = system(commandline) let &shellredir = s:shellredirsave let &shell = s:shellsave + let &shelltemp = s:shelltempsave call s:GPGDebug(3, "output: ". output) " check if the file is symmetric/asymmetric encrypted @@ -408,9 +416,11 @@ function s:GPGDecrypt() call s:GPGDebug(1, "command: " . commandline) let &shellredir = s:shellredir let &shell = s:shell + let &shelltemp = s:shelltemp execute commandline let &shellredir = s:shellredirsave let &shell = s:shellsave + let &shelltemp = s:shelltempsave if (v:shell_error) " message could not be decrypted echohl GPGError let blackhole = input("Message could not be decrypted! (Press ENTER)") @@ -516,9 +526,11 @@ function s:GPGEncrypt() call s:GPGDebug(1, "command: " . commandline) let &shellredir = s:shellredir let &shell = s:shell + let &shelltemp = s:shelltemp silent execute commandline let &shellredir = s:shellredirsave let &shell = s:shellsave + let &shelltemp = s:shelltempsave " restore encoding if (s:GPGEncoding != "") From 9e70b21d91312dbe80bc08c853d716ea90a7fe1e Mon Sep 17 00:00:00 2001 From: James Vega Date: Tue, 24 May 2011 07:28:02 -0400 Subject: [PATCH 094/115] Update docs to show my takeover of gnupg.vim Signed-off-by: James Vega --- plugin/gnupg.vim | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 3d5b12c..9c04125 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -1,10 +1,13 @@ " Name: gnupg.vim -" Version: $Id$ -" Author: Markus Braun +" Last Change: 2011 May 24 +" Author: James Vega +" Original Author: Markus Braun " Summary: Vim plugin for transparent editing of gpg encrypted files. -" Licence: This program is free software; you can redistribute it and/or -" modify it under the terms of the GNU General Public License. -" See http://www.gnu.org/copyleft/gpl.txt +" License: This program is free software; you can redistribute it and/or +" modify it under the terms of the GNU General Public License +" as published by the Free Software Foundation; either version +" 2 of the License, or (at your option) any later version. +" See http://www.gnu.org/copyleft/gpl-2.0.txt " " Section: Documentation {{{1 " From 32c88feb43bcabd3059089bbbac10d9194983325 Mon Sep 17 00:00:00 2001 From: James Vega Date: Sun, 26 Jun 2011 15:40:34 -0400 Subject: [PATCH 095/115] Disable 'undofile' for the buffer, if the option exists Signed-off-by: James Vega --- plugin/gnupg.vim | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 9c04125..e68e964 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -1,5 +1,5 @@ " Name: gnupg.vim -" Last Change: 2011 May 24 +" Last Change: 2011 June 26 " Author: James Vega " Original Author: Markus Braun " Summary: Vim plugin for transparent editing of gpg encrypted files. @@ -129,7 +129,7 @@ if (exists("g:loaded_gnupg") || &cp || exists("#BufReadCmd*.\(gpg\|asc\|pgp\)")) finish endif -let g:loaded_gnupg = "$Revision$" +let g:loaded_gnupg = '2.0' let s:GPGInitRun = 0 " check for correct vim version {{{2 @@ -173,18 +173,25 @@ highlight default link GPGHighlightUnknownRecipient ErrorMsg " initialize the plugin " function s:GPGInit() + call s:GPGDebug(3, ">>>>>>>> Entering s:GPGInit()") + + " we don't want a swap file, as it writes unencrypted data to disk + setl noswapfile + + " if persistent undo is present, disable it for this buffer + if exists('+undofile') + setl noundofile + endif + + " the rest only has to be run once if s:GPGInitRun return endif - call s:GPGDebug(3, ">>>>>>>> Entering s:GPGInit()") " first make sure nothing is written to ~/.viminfo while editing " an encrypted file. set viminfo= - " we don't want a swap file, as it writes unencrypted data to disk - setl noswapfile - " check what gpg command to use if (!exists("g:GPGExecutable")) let g:GPGExecutable = "gpg --trust-model always" @@ -327,7 +334,7 @@ function s:GPGDecrypt() " get the filename of the current buffer let filename = expand(":p") - " File doesn't exist yet, so force recipients + " File doesn't exist yet, so nothing to decrypt if empty(glob(filename)) return endif From 79a936311dd9b9ecfddc60337185598b980d3368 Mon Sep 17 00:00:00 2001 From: James Vega Date: Mon, 1 Aug 2011 20:44:17 -0400 Subject: [PATCH 096/115] Add g:GPGUsePipes variable to avoid saving unencrypted data to tempfiles Signed-off-by: James Vega --- plugin/gnupg.vim | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index e68e964..10e1ab7 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -1,6 +1,6 @@ " Name: gnupg.vim -" Last Change: 2011 June 26 -" Author: James Vega +" Last Change: 2011 Aug 01 +" Maintainer: James Vega " Original Author: Markus Braun " Summary: Vim plugin for transparent editing of gpg encrypted files. " License: This program is free software; you can redistribute it and/or @@ -84,6 +84,11 @@ " If set, these recipients are used as defaults when no other recipient is " defined. This variable is a Vim list. Default is unset. " +" g:GPGUsePipes +" If set to 1, use pipes instead of temporary files when interacting with +" gnupg. When set to 1, this can cause terminal-based gpg agents to not +" display correctly when prompting for passwords. Defaults to 0. +" " Known Issues: {{{2 " " In some cases gvim can't decrypt files @@ -129,7 +134,7 @@ if (exists("g:loaded_gnupg") || &cp || exists("#BufReadCmd*.\(gpg\|asc\|pgp\)")) finish endif -let g:loaded_gnupg = '2.0' +let g:loaded_gnupg = '2.1' let s:GPGInitRun = 0 " check for correct vim version {{{2 @@ -227,6 +232,11 @@ function s:GPGInit() let g:GPGDefaultRecipients = [] endif + " prefer not to use pipes since it can garble gpg agent display + if (!exists("g:GPGUsePipes")) + let g:GPGUsePipes = 0 + endif + " print version call s:GPGDebug(1, "gnupg.vim ". g:loaded_gnupg) @@ -261,7 +271,7 @@ function s:GPGInit() " noshelltemp isn't currently supported on Windows, but it doesn't cause any " errors and this future proofs us against requiring changes if Windows " gains noshelltemp functionality - let s:shelltemp = 0 + let s:shelltemp = !g:GPGUsePipes if (has("unix")) " unix specific settings let s:shellredir = ">%s 2>&1" From d645eb26cd0f374e4e5e7a15c7784897d6dedcc6 Mon Sep 17 00:00:00 2001 From: James Vega Date: Sat, 13 Aug 2011 18:00:27 -0400 Subject: [PATCH 097/115] Correctly handle the different keyid-format options Signed-off-by: James Vega --- plugin/gnupg.vim | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 10e1ab7..81cda0d 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -1,5 +1,5 @@ " Name: gnupg.vim -" Last Change: 2011 Aug 01 +" Last Change: 2011 Aug 13 " Maintainer: James Vega " Original Author: Markus Braun " Summary: Vim plugin for transparent editing of gpg encrypted files. @@ -134,7 +134,7 @@ if (exists("g:loaded_gnupg") || &cp || exists("#BufReadCmd*.\(gpg\|asc\|pgp\)")) finish endif -let g:loaded_gnupg = '2.1' +let g:loaded_gnupg = '2.2' let s:GPGInitRun = 0 " check for correct vim version {{{2 @@ -366,6 +366,7 @@ function s:GPGDecrypt() let &shelltemp = s:shelltempsave call s:GPGDebug(3, "output: ". output) + let asymmPattern = 'gpg: public key is \%(0x\)\=[[:xdigit:]]\{8,16}' " check if the file is symmetric/asymmetric encrypted if (match(output, "gpg: encrypted with [[:digit:]]\\+ passphrase") >= 0) " file is symmetric encrypted @@ -385,7 +386,7 @@ function s:GPGDecrypt() echo echohl None endif - elseif (match(output, "gpg: public key is [[:xdigit:]]\\{8}") >= 0) + elseif (match(output, asymmPattern) >= 0) " file is asymmetric encrypted let b:GPGEncrypted = 1 call s:GPGDebug(1, "this file is asymmetric encrypted") @@ -393,10 +394,10 @@ function s:GPGDecrypt() let b:GPGOptions += ["encrypt"] " find the used public keys - let start = match(output, "gpg: public key is [[:xdigit:]]\\{8}") + let start = match(output, asymmPattern) while (start >= 0) let start = start + strlen("gpg: public key is ") - let recipient = strpart(output, start, 8) + let recipient = matchstr(output, '[[:xdigit:]]\{8,16}', start) call s:GPGDebug(1, "recipient is " . recipient) let name = s:GPGNameToID(recipient) if (strlen(name) > 0) @@ -408,7 +409,7 @@ function s:GPGDecrypt() echom "The recipient \"" . recipient . "\" is not in your public keyring!" echohl None end - let start = match(output, "gpg: public key is [[:xdigit:]]\\{8}", start) + let start = match(output, asymmPattern, start) endwhile else " file is not encrypted From 904e392ae3e290102acb089f525f6205e9c1b491 Mon Sep 17 00:00:00 2001 From: James Vega Date: Thu, 13 Oct 2011 17:22:09 -0400 Subject: [PATCH 098/115] Initialize b:GPGRecipients with g:GPGDefaultRecipients Signed-off-by: James Vega --- plugin/gnupg.vim | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 81cda0d..0b6c3f2 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -344,15 +344,19 @@ function s:GPGDecrypt() " get the filename of the current buffer let filename = expand(":p") + " clear GPGRecipients and GPGOptions + let b:GPGRecipients = g:GPGDefaultRecipients + let b:GPGOptions = [] + " File doesn't exist yet, so nothing to decrypt if empty(glob(filename)) return endif - " clear GPGEncrypted, GPGRecipients and GPGOptions + " Only let this if the file actually exists, otherwise GPG functionality + " will be disabled when editing a buffer that doesn't yet have a backing + " file let b:GPGEncrypted = 0 - let b:GPGRecipients = [] - let b:GPGOptions = [] " find the recipients of the file let commandline = s:GPGCommand . " --verbose --decrypt --list-only --dry-run --batch --no-use-agent --logger-fd 1 " . shellescape(filename) From 571f46455e35235e701baf34e7781bc233b59300 Mon Sep 17 00:00:00 2001 From: James Vega Date: Tue, 18 Oct 2011 22:00:02 -0400 Subject: [PATCH 099/115] Resolve the filename when saving to follow symlinks. Signed-off-by: James Vega --- plugin/gnupg.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 0b6c3f2..f74ea25 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -1,5 +1,5 @@ " Name: gnupg.vim -" Last Change: 2011 Aug 13 +" Last Change: 2011 Oct 18 " Maintainer: James Vega " Original Author: Markus Braun " Summary: Vim plugin for transparent editing of gpg encrypted files. @@ -573,7 +573,7 @@ function s:GPGEncrypt() return endif - call rename(destfile, expand('')) + call rename(destfile, resolve(expand(''))) setl nomodified call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEncrypt()") endfunction From cd8aa8d0ce7af9a4d8ecd64ca63c3934a5bfb780 Mon Sep 17 00:00:00 2001 From: James McCoy Date: Wed, 23 Nov 2011 15:30:35 -0500 Subject: [PATCH 100/115] Consolidate handling of system()/:execute calls Signed-off-by: James McCoy --- plugin/gnupg.vim | 130 +++++++++++++++++++++++++++-------------------- 1 file changed, 76 insertions(+), 54 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index f74ea25..5cdc18d 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -1,6 +1,6 @@ " Name: gnupg.vim -" Last Change: 2011 Oct 18 -" Maintainer: James Vega +" Last Change: 2011 Nov 23 +" Maintainer: James McCoy " Original Author: Markus Braun " Summary: Vim plugin for transparent editing of gpg encrypted files. " License: This program is free software; you can redistribute it and/or @@ -298,14 +298,7 @@ function s:GPGInit() call s:GPGDebug(3, "shell implementation: " . resolve(s:shell)) " find the supported algorithms - let commandline = s:GPGCommand . " --version" - call s:GPGDebug(2, "command: ". commandline) - let &shellredir = s:shellredir - let &shell = s:shell - let output = system(commandline) - let &shellredir = s:shellredirsave - let &shell = s:shellsave - call s:GPGDebug(2, "output: ". output) + let output = s:GPGSystem({ 'level': 2, 'args': '--version' }) let s:GPGPubkey = substitute(output, ".*Pubkey: \\(.\\{-}\\)\n.*", "\\1", "") let s:GPGCipher = substitute(output, ".*Cipher: \\(.\\{-}\\)\n.*", "\\1", "") @@ -359,16 +352,9 @@ function s:GPGDecrypt() let b:GPGEncrypted = 0 " find the recipients of the file - let commandline = s:GPGCommand . " --verbose --decrypt --list-only --dry-run --batch --no-use-agent --logger-fd 1 " . shellescape(filename) - call s:GPGDebug(3, "command: " . commandline) - let &shellredir = s:shellredir - let &shell = s:shell - let &shelltemp = s:shelltemp - let output = system(commandline) - let &shellredir = s:shellredirsave - let &shell = s:shellsave - let &shelltemp = s:shelltempsave - call s:GPGDebug(3, "output: ". output) + let cmd = { 'level': 3 } + let cmd.args = '--verbose --decrypt --list-only --dry-run --batch --no-use-agent --logger-fd 1 ' . shellescape(filename) + let output = s:GPGSystem(cmd) let asymmPattern = 'gpg: public key is \%(0x\)\=[[:xdigit:]]\{8,16}' " check if the file is symmetric/asymmetric encrypted @@ -437,15 +423,10 @@ function s:GPGDecrypt() " 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 commandline = "r !" . s:GPGCommand . ' --quiet --decrypt ' . shellescape(filename, 1) . ' ' . s:stderrredirnull - call s:GPGDebug(1, "command: " . commandline) - let &shellredir = s:shellredir - let &shell = s:shell - let &shelltemp = s:shelltemp - execute commandline - let &shellredir = s:shellredirsave - let &shell = s:shellsave - let &shelltemp = s:shelltempsave + let cmd = { 'level': 1, 'ex': 'r !' } + let cmd.args = '--quiet --decrypt ' . 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)") @@ -547,15 +528,10 @@ function s:GPGEncrypt() " encrypt the buffer let destfile = tempname() - let commandline = "'[,']w !" . s:GPGCommand . ' --quiet --no-encrypt-to ' . options . '>' . shellescape(destfile, 1) . ' ' . s:stderrredirnull - call s:GPGDebug(1, "command: " . commandline) - let &shellredir = s:shellredir - let &shell = s:shell - let &shelltemp = s:shelltemp - silent execute commandline - let &shellredir = s:shellredirsave - let &shell = s:shellsave - let &shelltemp = s:shelltempsave + let cmd = { 'level': 1, 'ex': "'[,']w !" } + let cmd.args = '--quiet --no-encrypt-to ' . options + let cmd.redirect = '>' . shellescape(destfile, 1) + call s:GPGExecute(cmd) " restore encoding if (s:GPGEncoding != "") @@ -1034,14 +1010,9 @@ function s:GPGNameToID(name) call s:GPGDebug(3, ">>>>>>>> Entering s:GPGNameToID()") " ask gpg for the id for a name - let commandline = s:GPGCommand . " --quiet --with-colons --fixed-list-mode --list-keys " . shellescape(a:name) - call s:GPGDebug(2, "command: ". commandline) - let &shellredir = s:shellredir - let &shell = s:shell - let output = system(commandline) - let &shellredir = s:shellredirsave - let &shell = s:shellsave - call s:GPGDebug(2, "output: ". output) + let cmd = { 'level': 2 } + let cmd.args = '--quiet --with-colons --fixed-list-mode --list-keys ' . shellescape(a:name) + let output = s:GPGSystem(cmd) " when called with "--with-colons" gpg encodes its output _ALWAYS_ as UTF-8, " so convert it, if necessary @@ -1115,14 +1086,9 @@ function s:GPGIDToName(identity) " TODO is the encryption subkey really unique? " ask gpg for the id for a name - let commandline = s:GPGCommand . " --quiet --with-colons --fixed-list-mode --list-keys " . a:identity - call s:GPGDebug(2, "command: ". commandline) - let &shellredir = s:shellredir - let &shell = s:shell - let output = system(commandline) - let &shellredir = s:shellredirsave - let &shell = s:shellsave - call s:GPGDebug(2, "output: ". output) + let cmd = { 'level': 2 } + let cmd.args = '--quiet --with-colons --fixed-list-mode --list-keys ' . a:identity + let output = s:GPGSystem(cmd) " when called with "--with-colons" gpg encodes its output _ALWAYS_ as UTF-8, " so convert it, if necessary @@ -1157,6 +1123,62 @@ function s:GPGIDToName(identity) return uid endfunction +function s:GPGPreCmd() + let &shellredir = s:shellredir + let &shell = s:shell + let &shelltemp = s:shelltemp +endfunction + +function s:GPGPostCmd() + let &shellredir = s:shellredirsave + let &shell = s:shellsave + let &shelltemp = s:shelltempsave +endfunction + +" Function: s:GPGSystem(dict) {{{2 +" +" run g:GPGCommand using system(), logging the commandline and output +" Recognized keys are: +" level - Debug level at which the commandline and output will be logged +" args - Arguments to be given to g:GPGCommand +" +" Returns: command output +" +function s:GPGSystem(dict) + let commandline = printf('%s %s', s:GPGCommand, a:dict.args) + let commandline .= ' ' . s:stderrredirnull + call s:GPGDebug(a:dict.level, "command: ". commandline) + + call s:GPGPreCmd() + let output = system(commandline) + call s:GPGPostCmd() + + call s:GPGDebug(a:dict.level, "output: ". output) + return output +endfunction + +" Function: s:GPGExecute(dict) {{{2 +" +" run g:GPGCommand using :execute, logging the commandline +" Recognized keys are: +" level - Debug level at which the commandline will be logged +" args - Arguments to be given to g:GPGCommand +" ex - Ex command which will be :executed +" redirect - Shell redirect to use, if needed +" +function s:GPGExecute(dict) + let commandline = printf('%s%s %s', a:dict.ex, s:GPGCommand, a:dict.args) + if (has_key(a:dict, 'redirect')) + let commandline .= ' ' . a:dict.redirect + endif + let commandline .= ' ' . s:stderrredirnull + call s:GPGDebug(a:dict.level, "command: " . commandline) + + call s:GPGPreCmd() + execute commandline + call s:GPGPostCmd() +endfunction + " Function: s:GPGDebug(level, text) {{{2 " " output debug message, if this message has high enough importance From b470e4e11ed35452e1d88386cd00686033294f92 Mon Sep 17 00:00:00 2001 From: James McCoy Date: Wed, 23 Nov 2011 15:35:01 -0500 Subject: [PATCH 101/115] Add support for specifying an alternate --homedir Reported-By: Matt Callaway Signed-off-by: James McCoy --- plugin/gnupg.vim | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 5cdc18d..e2e7665 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -134,7 +134,7 @@ if (exists("g:loaded_gnupg") || &cp || exists("#BufReadCmd*.\(gpg\|asc\|pgp\)")) finish endif -let g:loaded_gnupg = '2.2' +let g:loaded_gnupg = '2.3' let s:GPGInitRun = 0 " check for correct vim version {{{2 @@ -237,6 +237,11 @@ function s:GPGInit() let g:GPGUsePipes = 0 endif + " allow alternate gnupg homedir + if (!exists('g:GPGHomedir')) + let g:GPGHomedir = '' + endif + " print version call s:GPGDebug(1, "gnupg.vim ". g:loaded_gnupg) @@ -1146,6 +1151,9 @@ endfunction " function s:GPGSystem(dict) let commandline = printf('%s %s', s:GPGCommand, a:dict.args) + if (!empty(g:GPGHomedir)) + let commandline .= ' --homedir ' . shellescape(g:GPGHomedir) + endif let commandline .= ' ' . s:stderrredirnull call s:GPGDebug(a:dict.level, "command: ". commandline) @@ -1168,6 +1176,9 @@ endfunction " function s:GPGExecute(dict) let commandline = printf('%s%s %s', a:dict.ex, s:GPGCommand, a:dict.args) + if (!empty(g:GPGHomedir)) + let commandline .= ' --homedir ' . shellescape(g:GPGHomedir, 1) + endif if (has_key(a:dict, 'redirect')) let commandline .= ' ' . a:dict.redirect endif From ebc86fca44d2ebf7ba02d22f2c0896b7fb19cbba Mon Sep 17 00:00:00 2001 From: James McCoy Date: Wed, 23 Nov 2011 17:09:42 -0500 Subject: [PATCH 102/115] Document the g:GPGHomedir option Signed-off-by: James McCoy --- plugin/gnupg.vim | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index e2e7665..08ba580 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -89,6 +89,11 @@ " gnupg. When set to 1, this can cause terminal-based gpg agents to not " display correctly when prompting for passwords. Defaults to 0. " +" g:GPGHomedir +" If set, specifies the directory that will be used for GPG's homedir. +" This corresponds to gpg's --homedir option. This variable is a Vim +" string. +" " Known Issues: {{{2 " " In some cases gvim can't decrypt files From f04de4883c005674fad0c1678f8bd1a6a064427d Mon Sep 17 00:00:00 2001 From: Alex Efros Date: Sat, 14 Jan 2012 00:37:31 +0200 Subject: [PATCH 103/115] added doautocmd BufWritePre --- plugin/gnupg.vim | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 08ba580..e621f25 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -159,6 +159,7 @@ augroup GnuPG autocmd BufReadCmd *.\(gpg\|asc\|pgp\) call s:GPGBufReadPost() " convert all text to encrypted text before writing + autocmd BufWriteCmd *.\(gpg\|asc\|pgp\) call s:GPGBufWritePre() autocmd BufWriteCmd,FileWriteCmd *.\(gpg\|asc\|pgp\) call s:GPGInit() autocmd BufWriteCmd,FileWriteCmd *.\(gpg\|asc\|pgp\) call s:GPGEncrypt() @@ -461,6 +462,14 @@ function s:GPGBufReadPost() call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGBufReadPost()") endfunction +function s:GPGBufWritePre() + call s:GPGDebug(3, ">>>>>>>> Entering s:GPGBufWritePre()") + " call the autocommand for the file minus .gpg$ + execute ':doautocmd BufWritePre ' . fnameescape(expand(':r')) + call s:GPGDebug(2, 'called autocommand for ' . fnameescape(expand(':r'))) + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGBufWritePre()") +endfunction + " Function: s:GPGEncrypt() {{{2 " " encrypts the buffer to all previous recipients From 39e54fbd96d35d1650a71e9b45b4ce71015548fc Mon Sep 17 00:00:00 2001 From: James McCoy Date: Wed, 30 May 2012 21:02:40 -0400 Subject: [PATCH 104/115] Clear undo history as the last step of BufReadCmd After opening a file, an immediate "u"/":undo" would remove the entire contents of the buffer. Even worse, this would not set 'modified', so there would be no indication that wasn't the actual initial state of the buffer. Discarding undo history for a buffer requires a few steps, including making a change. Luckily, a change is already being made (deleting the extra line left after ":r !" the decrypted file), so discarding undo history can piggy-back on top of that. Signed-off-by: James McCoy --- plugin/gnupg.vim | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index e621f25..8816e58 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -1,5 +1,5 @@ " Name: gnupg.vim -" Last Change: 2011 Nov 23 +" Last Change: 2012 May 30 " Maintainer: James McCoy " Original Author: Markus Braun " Summary: Vim plugin for transparent editing of gpg encrypted files. @@ -453,15 +453,33 @@ function s:GPGDecrypt() call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGDecrypt()") endfunction +" Function: s:GPGBufReadPost() {{{2 +" +" Handle functionality specific to opening a file for reading rather than +" reading the contents of a file into a buffer +" function s:GPGBufReadPost() call s:GPGDebug(3, ">>>>>>>> Entering s:GPGBufReadPost()") + " In order to make :undo a no-op immediately after the buffer is read, + " we need to do this dance with 'undolevels'. Actually discarding the undo + " history requires performing a change after setting 'undolevels' to -1 and, + " luckily, we have one we need to do (delete the extra line from the :r + " command) + let levels = &undolevels + set undolevels=-1 silent 1delete + let &undolevels = levels " call the autocommand for the file minus .gpg$ execute ':doautocmd BufReadPost ' . fnameescape(expand(':r')) call s:GPGDebug(2, 'called autocommand for ' . fnameescape(expand(':r'))) call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGBufReadPost()") endfunction +" Function: s:GPGBufWritePre() {{{2 +" +" Handle functionality specific to saving an entire buffer to a file rather +" than saving a partial buffer +" function s:GPGBufWritePre() call s:GPGDebug(3, ">>>>>>>> Entering s:GPGBufWritePre()") " call the autocommand for the file minus .gpg$ From 31a84b07303cde7f854c86e53697219ed2a055ef Mon Sep 17 00:00:00 2001 From: James McCoy Date: Wed, 30 May 2012 22:15:46 -0400 Subject: [PATCH 105/115] Indicate whether GPGInit/GPGDecrypt were invoked from BufReadCmd Signed-off-by: James McCoy --- plugin/gnupg.vim | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 8816e58..831861c 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -18,7 +18,7 @@ " a file the content is decrypted, when opening a new file the script will " ask for the recipients of the encrypted file. The file content will be " encrypted to all recipients before it is written. The script turns off -" viminfo and swapfile to increase security. +" viminfo, swapfile, and undofile to increase security. " " Installation: {{{2 " @@ -154,13 +154,15 @@ augroup GnuPG autocmd! " do the decryption - autocmd BufReadCmd,FileReadCmd *.\(gpg\|asc\|pgp\) call s:GPGInit() - autocmd BufReadCmd,FileReadCmd *.\(gpg\|asc\|pgp\) call s:GPGDecrypt() + autocmd BufReadCmd *.\(gpg\|asc\|pgp\) call s:GPGInit(1) + autocmd BufReadCmd *.\(gpg\|asc\|pgp\) call s:GPGDecrypt(1) autocmd BufReadCmd *.\(gpg\|asc\|pgp\) call s:GPGBufReadPost() + autocmd FileReadCmd *.\(gpg\|asc\|pgp\) call s:GPGInit(0) + autocmd FileReadCmd *.\(gpg\|asc\|pgp\) call s:GPGDecrypt(0) " convert all text to encrypted text before writing autocmd BufWriteCmd *.\(gpg\|asc\|pgp\) call s:GPGBufWritePre() - autocmd BufWriteCmd,FileWriteCmd *.\(gpg\|asc\|pgp\) call s:GPGInit() + autocmd BufWriteCmd,FileWriteCmd *.\(gpg\|asc\|pgp\) call s:GPGInit(0) autocmd BufWriteCmd,FileWriteCmd *.\(gpg\|asc\|pgp\) call s:GPGEncrypt() " cleanup on leaving vim @@ -179,12 +181,13 @@ highlight default link GPGHighlightUnknownRecipient ErrorMsg " Section: Functions {{{1 -" Function: s:GPGInit() {{{2 +" Function: s:GPGInit(bufread) {{{2 " " initialize the plugin +" The bufread argument specifies whether this was called due to BufReadCmd " -function s:GPGInit() - call s:GPGDebug(3, ">>>>>>>> Entering s:GPGInit()") +function s:GPGInit(bufread) + call s:GPGDebug(3, printf(">>>>>>>> Entering s:GPGInit(%d)", a:bufread)) " we don't want a swap file, as it writes unencrypted data to disk setl noswapfile @@ -338,12 +341,13 @@ function s:GPGCleanup() call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGCleanup()") endfunction -" Function: s:GPGDecrypt() {{{2 +" Function: s:GPGDecrypt(bufread) {{{2 " " decrypt the buffer and find all recipients of the encrypted file +" The bufread argument specifies whether this was called due to BufReadCmd " -function s:GPGDecrypt() - call s:GPGDebug(3, ">>>>>>>> Entering s:GPGDecrypt()") +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") From 34c24a19e5ed57124c66bc4d826c6847fe4535a3 Mon Sep 17 00:00:00 2001 From: James McCoy Date: Wed, 30 May 2012 22:17:05 -0400 Subject: [PATCH 106/115] Only disable swapfile, undofile, and viminfo when editing a file. Signed-off-by: James McCoy --- plugin/gnupg.vim | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 831861c..8bfda97 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -189,12 +189,22 @@ highlight default link GPGHighlightUnknownRecipient ErrorMsg function s:GPGInit(bufread) call s:GPGDebug(3, printf(">>>>>>>> Entering s:GPGInit(%d)", a:bufread)) - " we don't want a swap file, as it writes unencrypted data to disk - setl noswapfile + " For FileReadCmd, we're reading the contents into another buffer. If that + " buffer is also destined to be encrypted, then these settings will have + " already been set, otherwise don't set them since it limits the + " functionality of the cleartext buffer. + if a:bufread + " we don't want a swap file, as it writes unencrypted data to disk + setl noswapfile - " if persistent undo is present, disable it for this buffer - if exists('+undofile') - setl noundofile + " if persistent undo is present, disable it for this buffer + if exists('+undofile') + setl noundofile + endif + + " first make sure nothing is written to ~/.viminfo while editing + " an encrypted file. + set viminfo= endif " the rest only has to be run once @@ -202,10 +212,6 @@ function s:GPGInit(bufread) return endif - " first make sure nothing is written to ~/.viminfo while editing - " an encrypted file. - set viminfo= - " check what gpg command to use if (!exists("g:GPGExecutable")) let g:GPGExecutable = "gpg --trust-model always" From 181ed597f2c27cea99b5e291b994254d3ef3253f Mon Sep 17 00:00:00 2001 From: James McCoy Date: Wed, 30 May 2012 22:18:01 -0400 Subject: [PATCH 107/115] Suppress "N more lines" message when editing a file Signed-off-by: James McCoy --- plugin/gnupg.vim | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 8bfda97..1e191bd 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -377,6 +377,10 @@ function s:GPGDecrypt(bufread) let cmd.args = '--verbose --decrypt --list-only --dry-run --batch --no-use-agent --logger-fd 1 ' . shellescape(filename) let output = s:GPGSystem(cmd) + " Suppress the "N more lines" message when editing a file, not when reading + " the contents of a file into a buffer + let silent = a:bufread ? 'silent ' : '' + let asymmPattern = 'gpg: public key is \%(0x\)\=[[:xdigit:]]\{8,16}' " check if the file is symmetric/asymmetric encrypted if (match(output, "gpg: encrypted with [[:digit:]]\\+ passphrase") >= 0) @@ -429,7 +433,7 @@ function s:GPGDecrypt(bufread) echohl GPGWarning echom "File is not encrypted, all GPG functions disabled!" echohl None - silent exe '.r ' . fnameescape(filename) + exe printf('%sr %s', silent, fnameescape(filename)) call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGDecrypt()") return endif @@ -444,7 +448,7 @@ function s:GPGDecrypt(bufread) " 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': 'r !' } + let cmd = { 'level': 1, 'ex': silent . 'r !' } let cmd.args = '--quiet --decrypt ' . shellescape(filename, 1) call s:GPGExecute(cmd) From 07fec22b0a37744df4fd522ffb0a8266fd76c6f8 Mon Sep 17 00:00:00 2001 From: James McCoy Date: Wed, 30 May 2012 22:18:34 -0400 Subject: [PATCH 108/115] Don't wipeout the current buffer if FileReadCmd failed Signed-off-by: James McCoy --- plugin/gnupg.vim | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 1e191bd..c564432 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -456,7 +456,11 @@ function s:GPGDecrypt(bufread) echohl GPGError let blackhole = input("Message could not be decrypted! (Press ENTER)") echohl None - silent bwipeout! + " 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 From 53d086cb35724d9e1234f72885d9bd4878d16b25 Mon Sep 17 00:00:00 2001 From: James McCoy Date: Wed, 30 May 2012 23:48:24 -0400 Subject: [PATCH 109/115] Pull the key pattern out to a constant Signed-off-by: James McCoy --- plugin/gnupg.vim | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index c564432..c6430f8 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -172,6 +172,7 @@ augroup END " Section: Constants {{{1 let s:GPGMagicString = "\t \t" +let s:keyPattern = '\%(0x\)\=[[:xdigit:]]\{8,16}' " Section: Highlight setup {{{1 @@ -381,7 +382,7 @@ function s:GPGDecrypt(bufread) " the contents of a file into a buffer let silent = a:bufread ? 'silent ' : '' - let asymmPattern = 'gpg: public key is \%(0x\)\=[[:xdigit:]]\{8,16}' + let asymmPattern = 'gpg: public key is ' . s:keyPattern " check if the file is symmetric/asymmetric encrypted if (match(output, "gpg: encrypted with [[:digit:]]\\+ passphrase") >= 0) " file is symmetric encrypted @@ -412,7 +413,7 @@ function s:GPGDecrypt(bufread) let start = match(output, asymmPattern) while (start >= 0) let start = start + strlen("gpg: public key is ") - let recipient = matchstr(output, '[[:xdigit:]]\{8,16}', start) + let recipient = matchstr(output, s:keyPattern, start) call s:GPGDebug(1, "recipient is " . recipient) let name = s:GPGNameToID(recipient) if (strlen(name) > 0) From 0e8634693306870596c803d2303cf35d0d909289 Mon Sep 17 00:00:00 2001 From: James McCoy Date: Wed, 30 May 2012 23:48:43 -0400 Subject: [PATCH 110/115] GPGFinishRecipientsBuffer: Use the GPG ID, if present, to avoid repeat prompts Whenever GPGFinishRecipientsBuffer is called, the buffer is processed to convert names to IDs. However, this was ignoring any existing ID information in the buffer. This meant that if a person had multiple keys, the user would be prompted to disambiguate the keys after every :GPGEditRecipients session. Parsing the ID out of the recipients buffer avoids the unnecessary prompting. Signed-off-by: James McCoy --- plugin/gnupg.vim | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index c6430f8..3325901 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -796,13 +796,13 @@ function s:GPGFinishRecipientsBuffer() " delete the autocommand autocmd! * - " get the recipients from the scratch buffer let recipients = [] let lines = getline(1,"$") for recipient in lines - " delete all text after magic string - let recipient = substitute(recipient, s:GPGMagicString . ".*$", "", "") + let matches = matchlist(recipient, '^\(.\{-}\)\%(' . s:GPGMagicString . '(ID:\s\+\(' . s:keyPattern . '\)\s\+.*\)\=$') + + let recipient = matches[2] ? matches[2] : matches[1] " delete all spaces at beginning and end of the recipient " also delete a '!' at the beginning of the recipient From 94c0a22b8a012344761559bb94b2de12f588b8d2 Mon Sep 17 00:00:00 2001 From: James McCoy Date: Thu, 31 May 2012 00:31:36 -0400 Subject: [PATCH 111/115] Ignore expired keys. Signed-off-by: James McCoy --- plugin/gnupg.vim | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 3325901..11ee552 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -1,5 +1,5 @@ " Name: gnupg.vim -" Last Change: 2012 May 30 +" Last Change: 2012 May 31 " Maintainer: James McCoy " Original Author: Markus Braun " Summary: Vim plugin for transparent editing of gpg encrypted files. @@ -1090,17 +1090,21 @@ function s:GPGNameToID(name) let duplicates[line] = 1 let fields = split(line, ":") + + " Ignore expired keys + if fields[1] == 'e' + continue + endif + " search for the next uid - if (pubseen == 1) + if pubseen if (fields[0] == "uid") let choices = choices . " " . fields[9] . "\n" else let pubseen = 0 endif - endif - " search for the next pub - if (pubseen == 0) + else if (fields[0] == "pub") let identity = fields[4] let gpgids += [identity] @@ -1158,7 +1162,13 @@ function s:GPGIDToName(identity) let uid = "" for line in lines let fields = split(line, ":") - if (pubseen == 0) " search for the next pub + + " Ignore expired keys + if fields[1] == 'e' + continue + endif + + if !pubseen " search for the next pub if (fields[0] == "pub") let pubseen = 1 endif From d973d05173180a153319efff93237b06a454f081 Mon Sep 17 00:00:00 2001 From: James McCoy Date: Thu, 31 May 2012 00:31:56 -0400 Subject: [PATCH 112/115] Change autocmd check to look for GnuPG group Signed-off-by: James McCoy --- plugin/gnupg.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 11ee552..4e5d3f5 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -136,10 +136,10 @@ " Section: Plugin header {{{1 " guard against multiple loads {{{2 -if (exists("g:loaded_gnupg") || &cp || exists("#BufReadCmd*.\(gpg\|asc\|pgp\)")) +if (exists("g:loaded_gnupg") || &cp || exists("#GnuPG")) finish endif -let g:loaded_gnupg = '2.3' +let g:loaded_gnupg = '2.4' let s:GPGInitRun = 0 " check for correct vim version {{{2 From 5489147a47fb9e0350710efed760fc78b69a1613 Mon Sep 17 00:00:00 2001 From: James McCoy Date: Thu, 31 May 2012 21:17:07 -0400 Subject: [PATCH 113/115] Use "silent doautocmd" to suppress annoying "No matching autocmds" message Signed-off-by: James McCoy --- plugin/gnupg.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 4e5d3f5..c2b172a 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -489,7 +489,7 @@ function s:GPGBufReadPost() silent 1delete let &undolevels = levels " call the autocommand for the file minus .gpg$ - execute ':doautocmd BufReadPost ' . fnameescape(expand(':r')) + silent execute ':doautocmd BufReadPost ' . fnameescape(expand(':r')) call s:GPGDebug(2, 'called autocommand for ' . fnameescape(expand(':r'))) call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGBufReadPost()") endfunction @@ -502,7 +502,7 @@ endfunction function s:GPGBufWritePre() call s:GPGDebug(3, ">>>>>>>> Entering s:GPGBufWritePre()") " call the autocommand for the file minus .gpg$ - execute ':doautocmd BufWritePre ' . fnameescape(expand(':r')) + silent execute ':doautocmd BufWritePre ' . fnameescape(expand(':r')) call s:GPGDebug(2, 'called autocommand for ' . fnameescape(expand(':r'))) call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGBufWritePre()") endfunction From 6006af630b56b4b970fd63919c20420f7af32225 Mon Sep 17 00:00:00 2001 From: Thomas Arendsen Hein Date: Thu, 31 May 2012 21:21:35 -0400 Subject: [PATCH 114/115] Do not echo debug messages when redirecting to a log file Signed-off-by: James McCoy --- plugin/gnupg.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index c2b172a..046100d 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -1260,7 +1260,7 @@ function s:GPGDebug(level, text) if exists("g:GPGDebugLevel") && g:GPGDebugLevel >= a:level if exists("g:GPGDebugLog") execute "redir >> " . g:GPGDebugLog - echom "GnuPG: " . a:text + silent echom "GnuPG: " . a:text redir END else echom "GnuPG: " . a:text From 2844d06498c9802a6b3fc03bbac51929a1549726 Mon Sep 17 00:00:00 2001 From: James McCoy Date: Thu, 31 May 2012 22:27:42 -0400 Subject: [PATCH 115/115] Ignore keys that aren't usable for encryption rather than just expired keys Signed-off-by: James McCoy --- plugin/gnupg.vim | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/plugin/gnupg.vim b/plugin/gnupg.vim index 046100d..97539c5 100644 --- a/plugin/gnupg.vim +++ b/plugin/gnupg.vim @@ -139,7 +139,7 @@ if (exists("g:loaded_gnupg") || &cp || exists("#GnuPG")) finish endif -let g:loaded_gnupg = '2.4' +let g:loaded_gnupg = '2.5' let s:GPGInitRun = 0 " check for correct vim version {{{2 @@ -1091,11 +1091,6 @@ function s:GPGNameToID(name) let fields = split(line, ":") - " Ignore expired keys - if fields[1] == 'e' - continue - endif - " search for the next uid if pubseen if (fields[0] == "uid") @@ -1106,6 +1101,11 @@ function s:GPGNameToID(name) " search for the next pub else if (fields[0] == "pub") + " Ignore keys which are not usable for encryption + if fields[11] !~? 'e' + continue + endif + let identity = fields[4] let gpgids += [identity] if exists("*strftime") @@ -1163,13 +1163,13 @@ function s:GPGIDToName(identity) for line in lines let fields = split(line, ":") - " Ignore expired keys - if fields[1] == 'e' - continue - endif - if !pubseen " search for the next pub if (fields[0] == "pub") + " Ignore keys which are not usable for encryption + if fields[11] !~? 'e' + continue + endif + let pubseen = 1 endif else " search for the next uid