vim-gnupg/plugin/gnupg.vim
Victor Koronen a51eb77269 Move documentation into README and doc/
* Moves general documentation, such as the description and installation
  instructions, to a new README file at the root of the repo.
* Moves detailed documentation, such at command descriptions, into a text file
  under doc/ that can be indexed and searched through using Vim's `:help` command.
2014-01-25 15:26:39 +01:00

1193 lines
38 KiB
VimL

" gnupg.vim - Vim plugin for transparent editing of gpg encrypted files.
" Maintainer: James McCoy <vega.james@gmail.com>
" Original Author: Markus Braun <markus.braun@krawel.de>
"
" Section: Plugin header {{{1
" guard against multiple loads {{{2
if (exists("g:loaded_gnupg") || &cp || exists("#GnuPG"))
finish
endif
let g:loaded_gnupg = '2.5'
let s:GPGInitRun = 0
" check for correct vim version {{{2
if (v:version < 702)
echohl ErrorMsg | echo 'plugin gnupg.vim requires Vim version >= 7.2' | echohl None
finish
endif
" Section: Autocmd setup {{{1
if (!exists("g:GPGFilePattern"))
let g:GPGFilePattern = '*.\(gpg\|asc\|pgp\)'
endif
augroup GnuPG
autocmd!
" do the decryption
exe "autocmd BufReadCmd " . g:GPGFilePattern . " call s:GPGInit(1) |" .
\ " call s:GPGDecrypt(1) |" .
\ " call s:GPGBufReadPost()"
exe "autocmd FileReadCmd " . g:GPGFilePattern . " call s:GPGInit(0) |" .
\ " call s:GPGDecrypt(0)"
" convert all text to encrypted text before writing
" We check for GPGCorrespondingTo to avoid triggering on writes in GPG Options/Recipient windows
exe "autocmd BufWriteCmd " . g:GPGFilePattern . " if !exists('b:GPGCorrespondingTo') |" .
\ " call s:GPGBufWritePre() |" .
\ " endif"
exe "autocmd BufWriteCmd,FileWriteCmd " . g:GPGFilePattern . " if !exists('b:GPGCorrespondingTo') |" .
\ " call s:GPGInit(0) |" .
\ " call s:GPGEncrypt() |" .
\ " endif"
" cleanup on leaving vim
exe "autocmd VimLeave " . g:GPGFilePattern . " call s:GPGCleanup()"
augroup END
" Section: Constants {{{1
let s:GPGMagicString = "\t \t"
let s:keyPattern = '\%(0x\)\=[[:xdigit:]]\{8,16}'
" 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(bufread) {{{2
"
" initialize the plugin
" The bufread argument specifies whether this was called due to BufReadCmd
"
function s:GPGInit(bufread)
call s:GPGDebug(3, printf(">>>>>>>> Entering s:GPGInit(%d)", a:bufread))
" 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
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
if s:GPGInitRun
return
endif
" check what gpg command to use
if (!exists("g:GPGExecutable"))
let g:GPGExecutable = "gpg --trust-model always"
endif
" check if gpg-agent is allowed
if (!exists("g:GPGUseAgent"))
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"))
" .asc files should be armored as that's what the extension is used for
if expand('<afile>') =~ '\.asc$'
let g:GPGPreferArmor = 1
else
let g:GPGPreferArmor = 0
endif
endif
" check if signed files are preferred
if (!exists("g:GPGPreferSign"))
let g:GPGPreferSign = 0
endif
" start with empty default recipients if none is defined so far
if (!exists("g:GPGDefaultRecipients"))
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
" allow alternate gnupg homedir
if (!exists('g:GPGHomedir'))
let g:GPGHomedir = ''
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)
if (!exists("$GPG_TTY") && !has("gui_running"))
" Need to determine the associated tty by running a command in the
" shell. We can't use system() here because that doesn't run in a shell
" connected to a tty, so it's rather useless.
"
" Save/restore &modified so the buffer isn't incorrectly marked as
" modified just by detecting the correct tty value.
" Do the &undolevels dance so the :read and :delete don't get added into
" the undo tree, as the user needn't be aware of these.
let [mod, levels] = [&l:modified, &undolevels]
set undolevels=-1
silent read !tty
let $GPG_TTY = getline('.')
silent delete
let [&l:modified, &undolevels] = [mod, levels]
" redraw is needed since we're using silent to run !tty, c.f. :help :!
redraw!
if (v:shell_error)
let $GPG_TTY = ""
echohl GPGWarning
echom "$GPG_TTY is not set and the `tty` command failed! gpg-agent might not work."
echohl None
endif
endif
let s:GPGCommand = g:GPGExecutable . " --use-agent"
else
let s:GPGCommand = g:GPGExecutable . " --no-use-agent"
endif
" 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") && !has("gui_win32"))
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: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 = !g:GPGUsePipes
if (has("unix"))
" unix specific settings
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
else
" windows specific settings
let s:shellredir = '>%s'
let s:shell = &shell
let s:stderrredirnull = '2>nul'
endif
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)
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 output = s:GPGSystem({ 'level': 2, 'args': '--version' })
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", "")
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()")
let s:GPGInitRun = 1
endfunction
" Function: s:GPGCleanup() {{{2
"
" 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(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(bufread)
call s:GPGDebug(3, printf(">>>>>>>> Entering s:GPGDecrypt(%d)", a:bufread))
" get the filename of the current buffer
let filename = expand("<afile>:p")
" clear GPGRecipients and GPGOptions
let b:GPGRecipients = copy(g:GPGDefaultRecipients)
let b:GPGOptions = []
" File doesn't exist yet, so nothing to decrypt
if !filereadable(filename)
" Allow the user to define actions for GnuPG buffers
silent doautocmd User GnuPG
" call the autocommand for the file minus .gpg$
silent execute ':doautocmd BufNewFile ' . fnameescape(expand('<afile>:r'))
call s:GPGDebug(2, 'called BufNewFile autocommand for ' . fnameescape(expand('<afile>:r')))
" This is a new file, so force the user to edit the recipient list if
" they open a new file and public keys are preferred
if (exists("g:GPGPreferSymmetric") && g:GPGPreferSymmetric == 0)
call s:GPGEditRecipients()
endif
return
endif
" 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
" find the recipients of the file
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)
" 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 ' . s:keyPattern
" 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 += ["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]
call s:GPGDebug(1, "cipher-algo is " . cipher)
else
echohl GPGWarning
echom "The cipher " . cipher . " is not known by the local gpg command. Using default!"
echo
echohl None
endif
elseif (match(output, asymmPattern) >= 0)
" file is asymmetric encrypted
let b:GPGEncrypted = 1
call s:GPGDebug(1, "this file is asymmetric encrypted")
let b:GPGOptions += ["encrypt"]
" find the used public keys
let start = match(output, asymmPattern)
while (start >= 0)
let start = start + strlen("gpg: public key is ")
let recipient = matchstr(output, s:keyPattern, start)
call s:GPGDebug(1, "recipient is " . recipient)
let name = s:GPGNameToID(recipient)
if !empty(name)
let b:GPGRecipients += [name]
call s:GPGDebug(1, "name of recipient is " . name)
else
let b:GPGRecipients += [recipient]
echohl GPGWarning
echom "The recipient \"" . recipient . "\" is not in your public keyring!"
echohl None
end
let start = match(output, asymmPattern, start)
endwhile
else
" file is not encrypted
let b:GPGEncrypted = 0
call s:GPGDebug(1, "this file is not encrypted")
echohl GPGWarning
echom "File is not encrypted, all GPG functions disabled!"
echohl None
exe printf('%sr %s', silent, fnameescape(filename))
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGDecrypt()")
return
endif
" check if the message is armored
if (match(output, "gpg: armor header") >= 0)
call s:GPGDebug(1, "this file is armored")
let b:GPGOptions += ["armor"]
endif
" finally decrypt the buffer content
" since even with the --quiet option passphrase typos will be reported,
" we must redirect stderr (using shell temporarily)
call s:GPGDebug(1, "decrypting file")
let cmd = { 'level': 1, 'ex': silent . '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)")
echohl None
" Only wipeout the buffer if we were creating one to start with.
" FileReadCmd just reads the content into the existing buffer
if a:bufread
silent bwipeout!
endif
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGDecrypt()")
return
endif
" refresh screen
redraw!
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
" Allow the user to define actions for GnuPG buffers
silent doautocmd User GnuPG
" call the autocommand for the file minus .gpg$
silent execute ':doautocmd BufReadPost ' . fnameescape(expand('<afile>:r'))
call s:GPGDebug(2, 'called BufReadPost autocommand for ' . fnameescape(expand('<afile>: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$
silent execute ':doautocmd BufWritePre ' . fnameescape(expand('<afile>:r'))
call s:GPGDebug(2, 'called autocommand for ' . fnameescape(expand('<afile>:r')))
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGBufWritePre()")
endfunction
" Function: s:GPGEncrypt() {{{2
"
" encrypts the buffer to all previous recipients
"
function s:GPGEncrypt()
call s:GPGDebug(3, ">>>>>>>> Entering s:GPGEncrypt()")
" 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")
endif
" guard for unencrypted files
if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0)
echohl GPGError
let blackhole = input("Message could not be encrypted! (Press ENTER)")
echohl None
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEncrypt()")
return
endif
" initialize GPGOptions if not happened before
if (!exists("b:GPGOptions") || empty(b:GPGOptions))
let b:GPGOptions = []
if (exists("g:GPGPreferSymmetric") && g:GPGPreferSymmetric == 1)
let b:GPGOptions += ["symmetric"]
let b:GPGRecipients = []
else
let b:GPGOptions += ["encrypt"]
endif
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
" built list of options
let options = ""
for option in b:GPGOptions
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 = s:GPGCheckRecipients(b:GPGRecipients)
" check if there are unknown recipients and warn
if !empty(recipients.unknown)
echohl GPGWarning
echom "Please use GPGEditRecipients to correct!!"
echo
echohl None
" 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 options .= ' ' . join(map(recipients.valid, '"-r ".v:val'), ' ')
" encrypt the buffer
let destfile = tempname()
let cmd = { 'level': 1, 'ex': "'[,']w !" }
let cmd.args = '--quiet --no-encrypt-to ' . options
let cmd.redirect = '>' . shellescape(destfile, 1)
silent call s:GPGExecute(cmd)
" restore encoding
if (s:GPGEncoding != "")
let &encoding = s:GPGEncoding
call s:GPGDebug(2, "restored encoding \"" . &encoding . "\"")
endif
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
call rename(destfile, resolve(expand('<afile>')))
setl nomodified
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEncrypt()")
endfunction
" Function: s:GPGViewRecipients() {{{2
"
" 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
let recipients = s:GPGCheckRecipients(b:GPGRecipients)
echo 'This file has following recipients (Unknown recipients have a prepended "!"):'
" echo the recipients
for name in recipients.valid
let name = s:GPGIDToName(name)
echo name
endfor
" echo the unknown recipients
echohl GPGWarning
for name in recipients.unknown
let name = "!" . name
echo name
endfor
echohl None
" check if there is any known recipient
if empty(recipients.valid)
echohl GPGError
echom 'There are no known recipients!'
echohl None
endif
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGViewRecipients()")
endfunction
" Function: s:GPGEditRecipients() {{{2
"
" 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
" only do this if it isn't already a GPGRecipients_* buffer
if (match(bufname("%"), "^\\(GPGRecipients_\\|GPGOptions_\\)") != 0 && match(bufname("%"), "\.\\(gpg\\|asc\\|pgp\\)$") >= 0)
" save buffer name
let buffername = bufname("%")
let editbuffername = "GPGRecipients_" . buffername
" check if this buffer exists
if (!bufexists(editbuffername))
" create scratch buffer
execute 'silent! split ' . fnameescape(editbuffername)
" add a autocommand to regenerate the recipients after a write
autocmd BufHidden,BufUnload,BufWriteCmd <buffer> call s:GPGFinishRecipientsBuffer()
else
if (bufwinnr(editbuffername) >= 0)
" switch to scratch buffer window
execute 'silent! ' . bufwinnr(editbuffername) . "wincmd w"
else
" split scratch buffer window
execute 'silent! sbuffer ' . fnameescape(editbuffername)
" add a autocommand to regenerate the recipients after a write
autocmd BufHidden,BufUnload,BufWriteCmd <buffer> call s:GPGFinishRecipientsBuffer()
endif
" empty the buffer
silent %delete
endif
" Mark the buffer as a scratch buffer
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
" 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: Data after recipients between and including \"(\" and \")\" is ignored.'
silent put ='GPG: Closing this buffer commits changes.'
silent put ='GPG: ----------------------------------------------------------------------'
" get the recipients
let recipients = s:GPGCheckRecipients(getbufvar(b:GPGCorrespondingTo, "GPGRecipients"))
" if there are no known or unknown recipients, use the default ones
if (empty(recipients.valid) && empty(recipients.unknown))
if (type(g:GPGDefaultRecipients) == type([]))
let recipients = 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.valid
let name = s:GPGIDToName(name)
silent put =name
endfor
" put the unknown recipients in the scratch buffer
let syntaxPattern = ''
if !empty(recipients.unknown)
let flaggedNames = map(recipients.unknown, '"!".v:val')
call append('$', flaggedNames)
let syntaxPattern = '\(' . join(flaggedNames, '\|') . '\)'
endif
" define highlight
if (has("syntax") && exists("g:syntax_on"))
highlight clear GPGUnknownRecipient
if !empty(syntaxPattern)
execute 'syntax match GPGUnknownRecipient "' . syntaxPattern . '"'
highlight link GPGUnknownRecipient GPGHighlightUnknownRecipient
endif
syntax match GPGComment "^GPG:.*$"
execute 'syntax match GPGComment "' . s:GPGMagicString . '.*$"'
highlight clear GPGComment
highlight link GPGComment Comment
endif
" delete the empty first line
silent 1delete
" jump to the first recipient
silent $
endif
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEditRecipients()")
endfunction
" Function: s:GPGFinishRecipientsBuffer() {{{2
"
" 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
" go to buffer before doing work
if (bufnr("%") != expand("<abuf>"))
" switch to scratch buffer window
execute 'silent! ' . bufwinnr(expand("<afile>")) . "wincmd w"
endif
" delete the autocommand
autocmd! * <buffer>
" get the recipients from the scratch buffer
let recipients = []
let lines = getline(1,"$")
for recipient in lines
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
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 !empty(recipient)
let gpgid = s:GPGNameToID(recipient)
if !empty(gpgid)
if (match(recipients, gpgid) < 0)
let recipients += [gpgid]
endif
else
if (match(recipients, recipient) < 0)
let recipients += [recipient]
echohl GPGWarning
echom "The recipient \"" . recipient . "\" is not in your public keyring!"
echohl None
endif
endif
endif
endfor
" write back the new recipient list to the corresponding buffer and mark it
" as modified. Buffer is now for sure an encrypted buffer.
call setbufvar(b:GPGCorrespondingTo, "GPGRecipients", recipients)
call setbufvar(b:GPGCorrespondingTo, "&mod", 1)
call setbufvar(b:GPGCorrespondingTo, "GPGEncrypted", 1)
" check if there is any known recipient
if empty(recipients)
echohl GPGError
echom 'There are no known recipients!'
echohl None
endif
" reset modified flag
setl nomodified
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGFinishRecipientsBuffer()")
endfunction
" Function: s:GPGViewOptions() {{{2
"
" 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
if (exists("b:GPGOptions"))
echo 'This file has following options:'
" echo the options
for option in b:GPGOptions
echo option
endfor
endif
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGViewOptions()")
endfunction
" Function: s:GPGEditOptions() {{{2
"
" 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
" only do this if it isn't already a GPGOptions_* buffer
if (match(bufname("%"), "^\\(GPGRecipients_\\|GPGOptions_\\)") != 0 && match(bufname("%"), "\.\\(gpg\\|asc\\|pgp\\)$") >= 0)
" save buffer name
let buffername = bufname("%")
let editbuffername = "GPGOptions_" . buffername
" check if this buffer exists
if (!bufexists(editbuffername))
" create scratch buffer
execute 'silent! split ' . fnameescape(editbuffername)
" add a autocommand to regenerate the options after a write
autocmd BufHidden,BufUnload,BufWriteCmd <buffer> call s:GPGFinishOptionsBuffer()
else
if (bufwinnr(editbuffername) >= 0)
" switch to scratch buffer window
execute 'silent! ' . bufwinnr(editbuffername) . "wincmd w"
else
" split scratch buffer window
execute 'silent! sbuffer ' . fnameescape(editbuffername)
" add a autocommand to regenerate the options after a write
autocmd BufHidden,BufUnload,BufWriteCmd <buffer> call s:GPGFinishOptionsBuffer()
endif
" empty the buffer
silent %delete
endif
" 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:GPGCorrespondingTo = 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: Closing this buffer commits changes.'
silent put ='GPG: ----------------------------------------------------------------------'
" put the options in the scratch buffer
let options = getbufvar(b:GPGCorrespondingTo, "GPGOptions")
for option in options
silent put =option
endfor
" delete the empty first line
silent 1delete
" jump to the first option
silent $
" define highlight
if (has("syntax") && exists("g:syntax_on"))
syntax match GPGComment "^GPG:.*$"
highlight clear GPGComment
highlight link GPGComment Comment
endif
endif
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEditOptions()")
endfunction
" Function: s:GPGFinishOptionsBuffer() {{{2
"
" 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
" go to buffer before doing work
if (bufnr("%") != expand("<abuf>"))
" switch to scratch buffer window
execute 'silent! ' . bufwinnr(expand("<afile>")) . "wincmd w"
endif
" clear options and unknownOptions
let options = []
let unknownOptions = []
" delete the autocommand
autocmd! * <buffer>
" get the options from the scratch buffer
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", "")
" delete comment lines
let option = substitute(option, "^GPG:.*$", "", "")
" only do this if the line is not empty
if (!empty(option) && match(options, option) < 0)
let options += [option]
endif
endfor
" write back the new option list to the corresponding buffer and mark it
" as modified
call setbufvar(b:GPGCorrespondingTo, "GPGOptions", options)
call setbufvar(b:GPGCorrespondingTo, "&mod", 1)
" reset modified flag
setl nomodified
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGFinishOptionsBuffer()")
endfunction
" Function: s:GPGCheckRecipients(tocheck) {{{2
"
" check if recipients are known
" Returns: dictionary of recipients, {'valid': [], 'unknown': []}
"
function s:GPGCheckRecipients(tocheck)
call s:GPGDebug(3, ">>>>>>>> Entering s:GPGCheckRecipients()")
let recipients = {'valid': [], 'unknown': []}
if (type(a:tocheck) == type([]))
for recipient in a:tocheck
let gpgid = s:GPGNameToID(recipient)
if !empty(gpgid)
if (match(recipients.valid, gpgid) < 0)
call add(recipients.valid, gpgid)
endif
else
if (match(recipients.unknown, recipient) < 0)
call add(recipients.unknown, 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.valid))
call s:GPGDebug(2, "unknown recipients are: " . string(recipients.unknown))
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGCheckRecipients()")
return recipients
endfunction
" Function: s:GPGNameToID(name) {{{2
"
" find GPG key ID corresponding to a name
" 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 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
if (&encoding != "utf-8")
let output = iconv(output, "utf-8", &encoding)
endif
let lines = split(output, "\n")
" parse the output of gpg
let pubseen = 0
let counter = 0
let gpgids = []
let seen_keys = {}
let skip_key = 0
let has_strftime = exists('*strftime')
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 pub
if (fields[0] == "pub")
" check if this key has already been processed
if has_key(seen_keys, fields[4])
let skip_key = 1
continue
endif
let skip_key = 0
let seen_keys[fields[4]] = 1
" Ignore keys which are not usable for encryption
if fields[11] !~? 'e'
continue
endif
let identity = fields[4]
let gpgids += [identity]
if has_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
" search for the next uid
elseif (!skip_key && fields[0] == "uid")
let choices = choices . " " . fields[9] . "\n"
endif
endfor
" 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")
endwhile
endif
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGNameToID()")
return get(gpgids, answer, "")
endfunction
" Function: s:GPGIDToName(identity) {{{2
"
" find name corresponding to a GPG key ID
" 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 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
if (&encoding != "utf-8")
let output = iconv(output, "utf-8", &encoding)
endif
let lines = split(output, "\n")
" parse the output of gpg
let pubseen = 0
let uid = ""
for line in lines
let fields = split(line, ":")
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
if (fields[0] == "uid")
let pubseen = 0
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
endfor
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGIDToName()")
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)
if (!empty(g:GPGHomedir))
let commandline .= ' --homedir ' . shellescape(g:GPGHomedir)
endif
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, "rc: ". v:shell_error)
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 (!empty(g:GPGHomedir))
let commandline .= ' --homedir ' . shellescape(g:GPGHomedir, 1)
endif
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()
call s:GPGDebug(a:dict.level, "rc: ". v:shell_error)
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 exists("g:GPGDebugLevel") && g:GPGDebugLevel >= a:level
if exists("g:GPGDebugLog")
execute "redir >> " . g:GPGDebugLog
silent echom "GnuPG: " . a:text
redir END
else
echom "GnuPG: " . a:text
endif
endif
endfunction
" 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 <silent> Plugin.GnuPG.View\ Recipients :GPGViewRecipients<CR>
amenu <silent> Plugin.GnuPG.Edit\ Recipients :GPGEditRecipients<CR>
amenu <silent> Plugin.GnuPG.View\ Options :GPGViewOptions<CR>
amenu <silent> Plugin.GnuPG.Edit\ Options :GPGEditOptions<CR>
endif
" vim600: set foldmethod=marker foldlevel=0 :