2 " Version: $Id: gnupg.vim 3026 2010-01-27 08:18:04Z mbr $
3 " Author: Markus Braun <markus.braun@krawel.de>
4 " Summary: Vim plugin for transparent editing of gpg encrypted files.
5 " Licence: This program is free software; you can redistribute it and/or
6 " modify it under the terms of the GNU General Public License.
7 " See http://www.gnu.org/copyleft/gpl.txt
9 " Section: Documentation {{{1
13 " This script implements transparent editing of gpg encrypted files. The
14 " filename must have a ".gpg", ".pgp" or ".asc" suffix. When opening such
15 " a file the content is decrypted, when opening a new file the script will
16 " ask for the recipients of the encrypted file. The file content will be
17 " encrypted to all recipients before it is written. The script turns off
18 " viminfo and swapfile to increase security.
22 " Copy the gnupg.vim file to the $HOME/.vim/plugin directory.
23 " Refer to ':help add-plugin', ':help add-global-plugin' and ':help
24 " runtimepath' for more details about Vim plugins.
26 " From "man 1 gpg-agent":
29 " You should always add the following lines to your .bashrc or whatever
30 " initialization file is used for all shell invocations:
35 " It is important that this environment variable always reflects the out‐
36 " put of the tty command. For W32 systems this option is not required.
39 " Most distributions provide software to ease handling of gpg and gpg-agent.
40 " Examples are keychain or seahorse.
45 " Opens a scratch buffer to change the list of recipients. Recipients that
46 " are unknown (not in your public key) are highlighted and have
47 " a prepended "!". Closing the buffer makes the changes permanent.
50 " Prints the list of recipients.
53 " Opens a scratch buffer to change the options for encryption (symmetric,
54 " asymmetric, signing). Closing the buffer makes the changes permanent.
55 " WARNING: There is no check of the entered options, so you need to know
59 " Prints the list of options.
64 " If set used as gpg executable, otherwise the system chooses what is run
65 " when "gpg" is called. Defaults to "gpg".
68 " If set to 0 a possible available gpg-agent won't be used. Defaults to 1.
70 " g:GPGPreferSymmetric
71 " If set to 1 symmetric encryption is preferred for new files. Defaults to 0.
74 " If set to 1 armored data is preferred for new files. Defaults to 0.
77 " If set to 1 signed data is preferred for new files. Defaults to 0.
79 " g:GPGDefaultRecipients
80 " If set, these recipients are used as defaults when no other recipient is
81 " defined. This variable is a Vim list. Default is unset.
85 " In some cases gvim can't decryt files
87 " This is caused by the fact that a running gvim has no TTY and thus gpg is
88 " not able to ask for the passphrase by itself. This is a problem for Windows
89 " and Linux versions of gvim and could not be solved unless a "terminal
90 " emulation" is implemented for gvim. To circumvent this you have to use any
91 " combination of gpg-agent and a graphical pinentry program:
94 " you need to provide the passphrase for the needed key to gpg-agent
95 " in a terminal before you open files with gvim which require this key.
98 " you will get a popup window every time you open a file that needs to
101 " - gpgagent and pinentry:
102 " you will get a popup window the first time you open a file that
103 " needs to be decrypted.
107 " - Mathieu Clabaut for inspirations through his vimspell.vim script.
108 " - Richard Bronosky for patch to enable ".pgp" suffix.
109 " - Erik Remmelzwaal for patch to enable windows support and patient beta
111 " - Lars Becker for patch to make gpg2 working.
112 " - Thomas Arendsen Hein for patch to convert encoding of gpg output
113 " - Karl-Heinz Ruskowski for patch to fix unknown recipients and trust model
114 " and patient beta testing.
115 " - Giel van Schijndel for patch to get GPG_TTY dynamically.
116 " - Sebastian Luettich for patch to fix issue with symmetric encryption an set
118 " - Tim Swast for patch to generate signed files
120 " Section: Plugin header {{{1
122 " guard against multiple loads {{{2
123 if (exists("g:loaded_gnupg") || &cp || exists("#BufReadPre#*.\(gpg\|asc\|pgp\)"))
126 let g:loaded_gnupg = "$Revision: 3026 $"
128 " check for correct vim version {{{2
130 echohl ErrorMsg | echo 'plugin gnupg.vim requires Vim version >= 7.0' | echohl None
134 " Section: Autocmd setup {{{1
139 " initialize the internal variables
140 autocmd BufNewFile,BufReadPre,FileReadPre *.\(gpg\|asc\|pgp\) call s:GPGInit()
141 " force the user to edit the recipient list if he opens a new file and public
143 autocmd BufNewFile *.\(gpg\|asc\|pgp\) if (exists("g:GPGPreferSymmetric") && g:GPGPreferSymmetric == 0) | call s:GPGEditRecipients() | endif
145 autocmd BufReadPost,FileReadPost *.\(gpg\|asc\|pgp\) call s:GPGDecrypt()
147 " convert all text to encrypted text before writing
148 autocmd BufWritePre,FileWritePre *.\(gpg\|asc\|pgp\) call s:GPGEncrypt()
149 " undo the encryption so we are back in the normal text, directly
150 " after the file has been written.
151 autocmd BufWritePost,FileWritePost *.\(gpg\|asc\|pgp\) call s:GPGEncryptPost()
153 " cleanup on leaving vim
154 autocmd VimLeave *.\(gpg\|asc\|pgp\) call s:GPGCleanup()
157 " Section: Constants {{{1
159 let s:GPGMagicString = "\t \t"
161 " Section: Highlight setup {{{1
163 highlight default link GPGWarning WarningMsg
164 highlight default link GPGError ErrorMsg
165 highlight default link GPGHighlightUnknownRecipient ErrorMsg
167 " Section: Functions {{{1
169 " Function: s:GPGInit() {{{2
171 " initialize the plugin
174 call s:GPGDebug(3, ">>>>>>>> Entering s:GPGInit()")
176 " first make sure nothing is written to ~/.viminfo while editing
180 " we don't want a swap file, as it writes unencrypted data to disk
183 " check what gpg command to use
184 if (!exists("g:GPGExecutable"))
185 let g:GPGExecutable = "gpg --trust-model always"
188 " check if gpg-agent is allowed
189 if (!exists("g:GPGUseAgent"))
190 let g:GPGUseAgent = 1
193 " check if symmetric encryption is preferred
194 if (!exists("g:GPGPreferSymmetric"))
195 let g:GPGPreferSymmetric = 0
198 " check if armored files are preferred
199 if (!exists("g:GPGPreferArmor"))
200 let g:GPGPreferArmor = 0
203 " check if signed files are preferred
204 if (!exists("g:GPGPreferSign"))
205 let g:GPGPreferSign = 0
208 " start with empty default recipients if none is defined so far
209 if (!exists("g:GPGDefaultRecipients"))
210 let g:GPGDefaultRecipients = []
214 call s:GPGDebug(1, "gnupg.vim ". g:loaded_gnupg)
216 " determine if gnupg can use the gpg-agent
217 if (exists("$GPG_AGENT_INFO") && g:GPGUseAgent == 1)
218 if (!exists("$GPG_TTY") && !has("gui_running"))
219 let $GPG_TTY = system("tty")
223 echom "The GPG_TTY is not set and no TTY could be found using the `tty` command!"
224 echom "gpg-agent might not work."
228 let s:GPGCommand = g:GPGExecutable . " --use-agent"
230 let s:GPGCommand = g:GPGExecutable . " --no-use-agent"
233 " don't use tty in gvim
234 " FIXME find a better way to avoid an error.
235 " with this solution only --use-agent will work
236 if (has("gui_running"))
237 let s:GPGCommand = s:GPGCommand . " --no-tty"
240 " setup shell environment for unix and windows
241 let s:shellredirsave = &shellredir
242 let s:shellsave = &shell
244 " unix specific settings
245 let s:shellredir = ">%s 2>&1"
246 let s:shell = '/bin/sh'
247 let s:stderrredirnull = '2>/dev/null'
248 let s:GPGCommand = "LANG=C LC_ALL=C " . s:GPGCommand
250 " windows specific settings
251 let s:shellredir = '>%s'
253 let s:stderrredirnull = '2>nul'
256 call s:GPGDebug(3, "shellredirsave: " . s:shellredirsave)
257 call s:GPGDebug(3, "shellsave: " . s:shellsave)
259 call s:GPGDebug(3, "shell: " . s:shell)
260 call s:GPGDebug(3, "shellcmdflag: " . &shellcmdflag)
261 call s:GPGDebug(3, "shellxquote: " . &shellxquote)
262 call s:GPGDebug(3, "shellredir: " . s:shellredir)
263 call s:GPGDebug(3, "stderrredirnull: " . s:stderrredirnull)
265 call s:GPGDebug(3, "shell implementation: " . resolve(s:shell))
267 " find the supported algorithms
268 let commandline = s:GPGCommand . " --version"
269 call s:GPGDebug(2, "command: ". commandline)
270 let &shellredir = s:shellredir
272 let output = system(commandline)
273 let &shellredir = s:shellredirsave
274 let &shell = s:shellsave
275 call s:GPGDebug(2, "output: ". output)
277 let s:GPGPubkey = substitute(output, ".*Pubkey: \\(.\\{-}\\)\n.*", "\\1", "")
278 let s:GPGCipher = substitute(output, ".*Cipher: \\(.\\{-}\\)\n.*", "\\1", "")
279 let s:GPGHash = substitute(output, ".*Hash: \\(.\\{-}\\)\n.*", "\\1", "")
280 let s:GPGCompress = substitute(output, ".*Compress.\\{-}: \\(.\\{-}\\)\n.*", "\\1", "")
282 call s:GPGDebug(2, "public key algorithms: " . s:GPGPubkey)
283 call s:GPGDebug(2, "cipher algorithms: " . s:GPGCipher)
284 call s:GPGDebug(2, "hashing algorithms: " . s:GPGHash)
285 call s:GPGDebug(2, "compression algorithms: " . s:GPGCompress)
286 call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGInit()")
289 " Function: s:GPGCleanup() {{{2
291 " cleanup on leaving vim
293 function s:GPGCleanup()
294 call s:GPGDebug(3, ">>>>>>>> Entering s:GPGCleanup()")
300 call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGCleanup()")
303 " Function: s:GPGDecrypt() {{{2
305 " decrypt the buffer and find all recipients of the encrypted file
307 function s:GPGDecrypt()
308 call s:GPGDebug(3, ">>>>>>>> Entering s:GPGDecrypt()")
310 " switch to binary mode to read the encrypted file
313 " get the filename of the current buffer
314 let filename = escape(expand("%:p"), '\"')
316 " clear GPGEncrypted, GPGRecipients and GPGOptions
317 let b:GPGEncrypted = 0
318 let b:GPGRecipients = []
319 let b:GPGOptions = []
321 " find the recipients of the file
322 let commandline = s:GPGCommand . " --verbose --decrypt --list-only --dry-run --batch --no-use-agent --logger-fd 1 \"" . filename . "\""
323 call s:GPGDebug(3, "command: " . commandline)
324 let &shellredir = s:shellredir
326 let output = system(commandline)
327 let &shellredir = s:shellredirsave
328 let &shell = s:shellsave
329 call s:GPGDebug(3, "output: ". output)
331 " check if the file is symmetric/asymmetric encrypted
332 if (match(output, "gpg: encrypted with [[:digit:]]\\+ passphrase") >= 0)
333 " file is symmetric encrypted
334 let b:GPGEncrypted = 1
335 call s:GPGDebug(1, "this file is symmetric encrypted")
337 let b:GPGOptions += ["symmetric"]
339 " find the used cipher algorithm
340 let cipher = substitute(output, ".*gpg: \\([^ ]\\+\\) encrypted data.*", "\\1", "")
341 if (match(s:GPGCipher, "\\<" . cipher . "\\>") >= 0)
342 let b:GPGOptions += ["cipher-algo " . cipher]
343 call s:GPGDebug(1, "cipher-algo is " . cipher)
346 echom "The cipher " . cipher . " is not known by the local gpg command. Using default!"
350 elseif (match(output, "gpg: public key is [[:xdigit:]]\\{8}") >= 0)
351 " file is asymmetric encrypted
352 let b:GPGEncrypted = 1
353 call s:GPGDebug(1, "this file is asymmetric encrypted")
355 let b:GPGOptions += ["encrypt"]
357 " find the used public keys
358 let start = match(output, "gpg: public key is [[:xdigit:]]\\{8}")
360 let start = start + strlen("gpg: public key is ")
361 let recipient = strpart(output, start, 8)
362 call s:GPGDebug(1, "recipient is " . recipient)
363 let name = s:GPGNameToID(recipient)
364 if (strlen(name) > 0)
365 let b:GPGRecipients += [name]
366 call s:GPGDebug(1, "name of recipient is " . name)
368 let b:GPGRecipients += [recipient]
370 echom "The recipient \"" . recipient . "\" is not in your public keyring!"
373 let start = match(output, "gpg: public key is [[:xdigit:]]\\{8}", start)
376 " file is not encrypted
377 let b:GPGEncrypted = 0
378 call s:GPGDebug(1, "this file is not encrypted")
380 echom "File is not encrypted, all GPG functions disabled!"
383 call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGDecrypt()")
387 " check if the message is armored
388 if (match(output, "gpg: armor header") >= 0)
389 call s:GPGDebug(1, "this file is armored")
390 let b:GPGOptions += ["armor"]
393 " finally decrypt the buffer content
394 " since even with the --quiet option passphrase typos will be reported,
395 " we must redirect stderr (using shell temporarily)
396 call s:GPGDebug(1, "decrypting file")
397 let commandline = "'[,']!" . s:GPGCommand . " --quiet --decrypt " . s:stderrredirnull
398 call s:GPGDebug(1, "command: " . commandline)
399 let &shellredir = s:shellredir
402 let &shellredir = s:shellredirsave
403 let &shell = s:shellsave
404 if (v:shell_error) " message could not be decrypted
407 let blackhole = input("Message could not be decrypted! (Press ENTER)")
411 call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGDecrypt()")
415 " turn off binary mode
418 " call the autocommand for the file minus .gpg$
419 execute ":doautocmd BufReadPost " . escape(expand("%:r"), ' *?\"'."'")
420 call s:GPGDebug(2, "called autocommand for " . escape(expand("%:r"), ' *?\"'."'"))
425 call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGDecrypt()")
428 " Function: s:GPGEncrypt() {{{2
430 " encrypts the buffer to all previous recipients
432 function s:GPGEncrypt()
433 call s:GPGDebug(3, ">>>>>>>> Entering s:GPGEncrypt()")
436 let s:GPGWindowView = winsaveview()
437 call s:GPGDebug(2, "saved window view " . string(s:GPGWindowView))
439 " store encoding and switch to a safe one
440 if (&fileencoding != &encoding)
441 let s:GPGEncoding = &encoding
442 let &encoding = &fileencoding
443 call s:GPGDebug(2, "encoding was \"" . s:GPGEncoding . "\", switched to \"" . &encoding . "\"")
445 let s:GPGEncoding = ""
446 call s:GPGDebug(2, "encoding and fileencoding are the same (\"" . &encoding . "\"), not switching")
449 " switch buffer to binary mode
452 " guard for unencrypted files
453 if (!exists("b:GPGEncrypted") || b:GPGEncrypted == 0)
455 let blackhole = input("Message could not be encrypted! File might be empty! (Press ENTER)")
457 call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEncrypt()")
461 " initialize GPGOptions if not happened before
462 if (!exists("b:GPGOptions") || len(b:GPGOptions) == 0)
463 let b:GPGOptions = []
464 if (exists("g:GPGPreferSymmetric") && g:GPGPreferSymmetric == 1)
465 let b:GPGOptions += ["symmetric"]
466 let b:GPGRecipients = []
468 let b:GPGOptions += ["encrypt"]
470 if (exists("g:GPGPreferArmor") && g:GPGPreferArmor == 1)
471 let b:GPGOptions += ["armor"]
473 if (exists("g:GPGPreferSign") && g:GPGPreferSign == 1)
474 let b:GPGOptions += ["sign"]
476 call s:GPGDebug(1, "no options set, so using default options: " . string(b:GPGOptions))
479 " built list of options
481 for option in b:GPGOptions
482 let options = options . " --" . option . " "
485 " check here again if all recipients are available in the keyring
486 let [ recipients, unknownrecipients ] = s:GPGCheckRecipients(b:GPGRecipients)
488 " check if there are unknown recipients and warn
489 if (len(unknownrecipients) > 0)
491 echom "Please use GPGEditRecipients to correct!!"
495 " Let user know whats happend and copy known_recipients back to buffer
496 let dummy = input("Press ENTER to quit")
499 " built list of recipients
500 if (len(recipients) > 0)
501 for gpgid in recipients
502 let options = options . " -r " . gpgid
505 if (match(b:GPGOptions, "encrypt") >= 0)
507 echom "There are no recipients!!"
508 echom "Please use GPGEditRecipients to correct!!"
515 let commandline = "'[,']!" . s:GPGCommand . " --quiet --no-encrypt-to " . options . " " . s:stderrredirnull
516 call s:GPGDebug(1, "command: " . commandline)
517 let &shellredir = s:shellredir
519 silent execute commandline
520 let &shellredir = s:shellredirsave
521 let &shell = s:shellsave
522 if (v:shell_error) " message could not be encrypted
523 " delete content of the buffer to be sure no data is written unencrypted
524 " content will be recovered in GPGEncryptPost()
528 let blackhole = input("Message could not be encrypted! File might be empty! (Press ENTER)")
530 call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEncrypt()")
534 call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEncrypt()")
537 " Function: s:GPGEncryptPost() {{{2
539 " undo changes don by encrypt, after writing
541 function s:GPGEncryptPost()
542 call s:GPGDebug(3, ">>>>>>>> Entering s:GPGEncryptPost()")
544 " guard for unencrypted files
545 if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0)
546 call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEncryptPost()")
550 " undo encryption of buffer content
553 " switch back from binary mode
557 if (s:GPGEncoding != "")
558 let &encoding = s:GPGEncoding
559 call s:GPGDebug(2, "restored encoding \"" . &encoding . "\"")
562 " restore window view
563 call winrestview(s:GPGWindowView)
564 call s:GPGDebug(2, "restored window view" . string(s:GPGWindowView))
569 call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEncryptPost()")
572 " Function: s:GPGViewRecipients() {{{2
574 " echo the recipients
576 function s:GPGViewRecipients()
577 call s:GPGDebug(3, ">>>>>>>> Entering s:GPGViewRecipients()")
579 " guard for unencrypted files
580 if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0)
582 echom "File is not encrypted, all GPG functions disabled!"
584 call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGViewRecipients()")
588 let [ recipients, unknownrecipients ] = s:GPGCheckRecipients(b:GPGRecipients)
590 echo 'This file has following recipients (Unknown recipients have a prepended "!"):'
591 " echo the recipients
592 for name in recipients
593 let name = s:GPGIDToName(name)
597 " echo the unknown recipients
599 for name in unknownrecipients
600 let name = "!" . name
605 " check if there is any known recipient
606 if (len(recipients) == 0)
608 echom 'There are no known recipients!'
612 call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGViewRecipients()")
615 " Function: s:GPGEditRecipients() {{{2
617 " create a scratch buffer with all recipients to add/remove recipients
619 function s:GPGEditRecipients()
620 call s:GPGDebug(3, ">>>>>>>> Entering s:GPGEditRecipients()")
622 " guard for unencrypted files
623 if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0)
625 echom "File is not encrypted, all GPG functions disabled!"
627 call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEditRecipients()")
631 " only do this if it isn't already a GPGRecipients_* buffer
632 if (match(bufname("%"), "^\\(GPGRecipients_\\|GPGOptions_\\)") != 0 && match(bufname("%"), "\.\\(gpg\\|asc\\|pgp\\)$") >= 0)
635 let buffername = bufname("%")
636 let editbuffername = "GPGRecipients_" . buffername
638 " check if this buffer exists
639 if (!bufexists(editbuffername))
640 " create scratch buffer
641 execute 'silent! split ' . escape(editbuffername, ' *?\"'."'")
643 " add a autocommand to regenerate the recipients after a write
644 autocmd BufHidden,BufUnload,BufWriteCmd <buffer> call s:GPGFinishRecipientsBuffer()
646 if (bufwinnr(editbuffername) >= 0)
647 " switch to scratch buffer window
648 execute 'silent! ' . bufwinnr(editbuffername) . "wincmd w"
650 " split scratch buffer window
651 execute 'silent! sbuffer ' . escape(editbuffername, ' *?\"'."'")
653 " add a autocommand to regenerate the recipients after a write
654 autocmd BufHidden,BufUnload,BufWriteCmd <buffer> call s:GPGFinishRecipientsBuffer()
661 " Mark the buffer as a scratch buffer
662 setlocal buftype=acwrite
663 setlocal bufhidden=hide
669 " so we know for which other buffer this edit buffer is
670 let b:GPGCorrespondingTo = buffername
672 " put some comments to the scratch buffer
673 silent put ='GPG: ----------------------------------------------------------------------'
674 silent put ='GPG: Please edit the list of recipients, one recipient per line.'
675 silent put ='GPG: Unknown recipients have a prepended \"!\".'
676 silent put ='GPG: Lines beginning with \"GPG:\" are removed automatically.'
677 silent put ='GPG: Data after recipients between and including \"(\" and \")\" is ignored.'
678 silent put ='GPG: Closing this buffer commits changes.'
679 silent put ='GPG: ----------------------------------------------------------------------'
682 let [ recipients, unknownrecipients ] = s:GPGCheckRecipients(getbufvar(b:GPGCorrespondingTo, "GPGRecipients"))
684 " if there are no known or unknown recipients, use the default ones
685 if (len(recipients) == 0 && len(unknownrecipients) == 0)
686 if (type(g:GPGDefaultRecipients) == type([]))
687 let [ recipients, unknownrecipients ] = s:GPGCheckRecipients(g:GPGDefaultRecipients)
690 echom "g:GPGDefaultRecipients is not a Vim list, please correct this in your vimrc!"
695 " put the recipients in the scratch buffer
696 for name in recipients
697 let name = s:GPGIDToName(name)
701 " put the unknown recipients in the scratch buffer
702 let syntaxPattern = "\\(nonexxistinwordinthisbuffer"
703 for name in unknownrecipients
704 let name = "!" . name
705 let syntaxPattern = syntaxPattern . "\\|" . name
708 let syntaxPattern = syntaxPattern . "\\)"
711 if (has("syntax") && exists("g:syntax_on"))
712 execute 'syntax match GPGUnknownRecipient "' . syntaxPattern . '"'
713 highlight clear GPGUnknownRecipient
714 highlight link GPGUnknownRecipient GPGHighlightUnknownRecipient
716 syntax match GPGComment "^GPG:.*$"
717 execute 'syntax match GPGComment "' . s:GPGMagicString . '.*$"'
718 highlight clear GPGComment
719 highlight link GPGComment Comment
722 " delete the empty first line
725 " jump to the first recipient
730 call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEditRecipients()")
733 " Function: s:GPGFinishRecipientsBuffer() {{{2
735 " create a new recipient list from RecipientsBuffer
737 function s:GPGFinishRecipientsBuffer()
738 call s:GPGDebug(3, ">>>>>>>> Entering s:GPGFinishRecipientsBuffer()")
740 " guard for unencrypted files
741 if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0)
743 echom "File is not encrypted, all GPG functions disabled!"
745 call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGFinishRecipientsBuffer()")
749 " go to buffer before doing work
750 if (bufnr("%") != expand("<abuf>"))
751 " switch to scratch buffer window
752 execute 'silent! ' . bufwinnr(expand("<afile>")) . "wincmd w"
755 " delete the autocommand
759 " get the recipients from the scratch buffer
761 let lines = getline(1,"$")
762 for recipient in lines
763 " delete all text after magic string
764 let recipient = substitute(recipient, s:GPGMagicString . ".*$", "", "")
766 " delete all spaces at beginning and end of the recipient
767 " also delete a '!' at the beginning of the recipient
768 let recipient = substitute(recipient, "^[[:space:]!]*\\(.\\{-}\\)[[:space:]]*$", "\\1", "")
770 " delete comment lines
771 let recipient = substitute(recipient, "^GPG:.*$", "", "")
773 " only do this if the line is not empty
774 if (strlen(recipient) > 0)
775 let gpgid = s:GPGNameToID(recipient)
776 if (strlen(gpgid) > 0)
777 if (match(recipients, gpgid) < 0)
778 let recipients += [gpgid]
781 if (match(recipients, recipient) < 0)
782 let recipients += [recipient]
784 echom "The recipient \"" . recipient . "\" is not in your public keyring!"
791 " write back the new recipient list to the corresponding buffer and mark it
792 " as modified. Buffer is now for sure a encrypted buffer.
793 call setbufvar(b:GPGCorrespondingTo, "GPGRecipients", recipients)
794 call setbufvar(b:GPGCorrespondingTo, "&mod", 1)
795 call setbufvar(b:GPGCorrespondingTo, "GPGEncrypted", 1)
797 " check if there is any known recipient
798 if (len(recipients) == 0)
800 echom 'There are no known recipients!'
804 " reset modified flag
807 call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGFinishRecipientsBuffer()")
810 " Function: s:GPGViewOptions() {{{2
812 " echo the recipients
814 function s:GPGViewOptions()
815 call s:GPGDebug(3, ">>>>>>>> Entering s:GPGViewOptions()")
817 " guard for unencrypted files
818 if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0)
820 echom "File is not encrypted, all GPG functions disabled!"
822 call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGViewOptions()")
826 if (exists("b:GPGOptions"))
827 echo 'This file has following options:'
829 for option in b:GPGOptions
834 call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGViewOptions()")
837 " Function: s:GPGEditOptions() {{{2
839 " create a scratch buffer with all recipients to add/remove recipients
841 function s:GPGEditOptions()
842 call s:GPGDebug(3, ">>>>>>>> Entering s:GPGEditOptions()")
844 " guard for unencrypted files
845 if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0)
847 echom "File is not encrypted, all GPG functions disabled!"
849 call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEditOptions()")
853 " only do this if it isn't already a GPGOptions_* buffer
854 if (match(bufname("%"), "^\\(GPGRecipients_\\|GPGOptions_\\)") != 0 && match(bufname("%"), "\.\\(gpg\\|asc\\|pgp\\)$") >= 0)
857 let buffername = bufname("%")
858 let editbuffername = "GPGOptions_" . buffername
860 " check if this buffer exists
861 if (!bufexists(editbuffername))
862 " create scratch buffer
863 execute 'silent! split ' . escape(editbuffername, ' *?\"'."'")
865 " add a autocommand to regenerate the options after a write
866 autocmd BufHidden,BufUnload,BufWriteCmd <buffer> call s:GPGFinishOptionsBuffer()
868 if (bufwinnr(editbuffername) >= 0)
869 " switch to scratch buffer window
870 execute 'silent! ' . bufwinnr(editbuffername) . "wincmd w"
872 " split scratch buffer window
873 execute 'silent! sbuffer ' . escape(editbuffername, ' *?\"'."'")
875 " add a autocommand to regenerate the options after a write
876 autocmd BufHidden,BufUnload,BufWriteCmd <buffer> call s:GPGFinishOptionsBuffer()
883 " Mark the buffer as a scratch buffer
884 setlocal buftype=nofile
890 " so we know for which other buffer this edit buffer is
891 let b:GPGCorrespondingTo = buffername
893 " put some comments to the scratch buffer
894 silent put ='GPG: ----------------------------------------------------------------------'
895 silent put ='GPG: THERE IS NO CHECK OF THE ENTERED OPTIONS!'
896 silent put ='GPG: YOU NEED TO KNOW WHAT YOU ARE DOING!'
897 silent put ='GPG: IF IN DOUBT, QUICKLY EXIT USING :x OR :bd.'
898 silent put ='GPG: Please edit the list of options, one option per line.'
899 silent put ='GPG: Please refer to the gpg documentation for valid options.'
900 silent put ='GPG: Lines beginning with \"GPG:\" are removed automatically.'
901 silent put ='GPG: Closing this buffer commits changes.'
902 silent put ='GPG: ----------------------------------------------------------------------'
904 " put the options in the scratch buffer
905 let options = getbufvar(b:GPGCorrespondingTo, "GPGOptions")
907 for option in options
911 " delete the empty first line
914 " jump to the first option
918 if (has("syntax") && exists("g:syntax_on"))
919 syntax match GPGComment "^GPG:.*$"
920 highlight clear GPGComment
921 highlight link GPGComment Comment
925 call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEditOptions()")
928 " Function: s:GPGFinishOptionsBuffer() {{{2
930 " create a new option list from OptionsBuffer
932 function s:GPGFinishOptionsBuffer()
933 call s:GPGDebug(3, ">>>>>>>> Entering s:GPGFinishOptionsBuffer()")
935 " guard for unencrypted files
936 if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0)
938 echom "File is not encrypted, all GPG functions disabled!"
940 call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGFinishOptionsBuffer()")
944 " go to buffer before doing work
945 if (bufnr("%") != expand("<abuf>"))
946 " switch to scratch buffer window
947 execute 'silent! ' . bufwinnr(expand("<afile>")) . "wincmd w"
950 " clear options and unknownOptions
952 let unknownOptions = []
954 " delete the autocommand
957 " get the options from the scratch buffer
958 let lines = getline(1, "$")
960 " delete all spaces at beginning and end of the option
961 " also delete a '!' at the beginning of the option
962 let option = substitute(option, "^[[:space:]!]*\\(.\\{-}\\)[[:space:]]*$", "\\1", "")
963 " delete comment lines
964 let option = substitute(option, "^GPG:.*$", "", "")
966 " only do this if the line is not empty
967 if (strlen(option) > 0 && match(options, option) < 0)
968 let options += [option]
972 " write back the new option list to the corresponding buffer and mark it
974 call setbufvar(b:GPGCorrespondingTo, "GPGOptions", options)
975 call setbufvar(b:GPGCorrespondingTo, "&mod", 1)
977 " reset modified flag
980 call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGFinishOptionsBuffer()")
983 " Function: s:GPGCheckRecipients(tocheck) {{{2
985 " check if recipients are known
986 " Returns: two lists recipients and unknownrecipients
988 function s:GPGCheckRecipients(tocheck)
989 call s:GPGDebug(3, ">>>>>>>> Entering s:GPGCheckRecipients()")
992 let unknownrecipients = []
994 if (type(a:tocheck) == type([]))
995 for recipient in a:tocheck
996 let gpgid = s:GPGNameToID(recipient)
997 if (strlen(gpgid) > 0)
998 if (match(recipients, gpgid) < 0)
999 let recipients += [gpgid]
1002 if (match(unknownrecipients, recipient) < 0)
1003 let unknownrecipients += [recipient]
1005 echom "The recipient \"" . recipient . "\" is not in your public keyring!"
1012 call s:GPGDebug(2, "recipients are: " . string(recipients))
1013 call s:GPGDebug(2, "unknown recipients are: " . string(unknownrecipients))
1015 call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGCheckRecipients()")
1016 return [ recipients, unknownrecipients ]
1019 " Function: s:GPGNameToID(name) {{{2
1021 " find GPG key ID corresponding to a name
1022 " Returns: ID for the given name
1024 function s:GPGNameToID(name)
1025 call s:GPGDebug(3, ">>>>>>>> Entering s:GPGNameToID()")
1027 " ask gpg for the id for a name
1028 let commandline = s:GPGCommand . " --quiet --with-colons --fixed-list-mode --list-keys \"" . a:name . "\""
1029 call s:GPGDebug(2, "command: ". commandline)
1030 let &shellredir = s:shellredir
1031 let &shell = s:shell
1032 let output = system(commandline)
1033 let &shellredir = s:shellredirsave
1034 let &shell = s:shellsave
1035 call s:GPGDebug(2, "output: ". output)
1037 " when called with "--with-colons" gpg encodes its output _ALWAYS_ as UTF-8,
1038 " so convert it, if necessary
1039 if (&encoding != "utf-8")
1040 let output = iconv(output, "utf-8", &encoding)
1042 let lines = split(output, "\n")
1044 " parse the output of gpg
1048 let choices = "The name \"" . a:name . "\" is ambiguous. Please select the correct key:\n"
1050 let fields = split(line, ":")
1051 " search for the next uid
1053 if (fields[0] == "uid")
1054 let choices = choices . " " . fields[9] . "\n"
1060 " search for the next pub
1062 if (fields[0] == "pub")
1063 let identity = fields[4]
1064 let gpgids += [identity]
1065 if exists("*strftime")
1066 let choices = choices . counter . ": ID: 0x" . identity . " created at " . strftime("%c", fields[5]) . "\n"
1068 let choices = choices . counter . ": ID: 0x" . identity . "\n"
1070 let counter = counter+1
1077 " counter > 1 means we have more than one results
1080 let choices = choices . "Enter number: "
1081 let answer = input(choices, "0")
1082 while (answer == "")
1083 let answer = input("Enter number: ", "0")
1087 call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGIDToName()")
1088 return get(gpgids, answer, "")
1091 " Function: s:GPGIDToName(identity) {{{2
1093 " find name corresponding to a GPG key ID
1094 " Returns: Name for the given ID
1096 function s:GPGIDToName(identity)
1097 call s:GPGDebug(3, ">>>>>>>> Entering s:GPGIDToName()")
1099 " TODO is the encryption subkey really unique?
1101 " ask gpg for the id for a name
1102 let commandline = s:GPGCommand . " --quiet --with-colons --fixed-list-mode --list-keys " . a:identity
1103 call s:GPGDebug(2, "command: ". commandline)
1104 let &shellredir = s:shellredir
1105 let &shell = s:shell
1106 let output = system(commandline)
1107 let &shellredir = s:shellredirsave
1108 let &shell = s:shellsave
1109 call s:GPGDebug(2, "output: ". output)
1111 " when called with "--with-colons" gpg encodes its output _ALWAYS_ as UTF-8,
1112 " so convert it, if necessary
1113 if (&encoding != "utf-8")
1114 let output = iconv(output, "utf-8", &encoding)
1116 let lines = split(output, "\n")
1118 " parse the output of gpg
1122 let fields = split(line, ":")
1123 if (pubseen == 0) " search for the next pub
1124 if (fields[0] == "pub")
1127 else " search for the next uid
1128 if (fields[0] == "uid")
1130 if exists("*strftime")
1131 let uid = fields[9] . s:GPGMagicString . "(ID: 0x" . a:identity . " created at " . strftime("%c", fields[5]) . ")"
1133 let uid = fields[9] . s:GPGMagicString . "(ID: 0x" . a:identity . ")"
1140 call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGIDToName()")
1144 " Function: s:GPGDebug(level, text) {{{2
1146 " output debug message, if this message has high enough importance
1147 " only define function if GPGDebugLevel set at all
1149 function s:GPGDebug(level, text)
1150 if exists("g:GPGDebugLevel") && g:GPGDebugLevel >= a:level
1151 if exists("g:GPGDebugLog")
1152 execute "redir >> " . g:GPGDebugLog
1153 echom "GnuPG: " . a:text
1156 echom "GnuPG: " . a:text
1161 " Section: Commands {{{1
1163 command! GPGViewRecipients call s:GPGViewRecipients()
1164 command! GPGEditRecipients call s:GPGEditRecipients()
1165 command! GPGViewOptions call s:GPGViewOptions()
1166 command! GPGEditOptions call s:GPGEditOptions()
1168 " Section: Menu {{{1
1171 amenu <silent> Plugin.GnuPG.View\ Recipients :GPGViewRecipients<CR>
1172 amenu <silent> Plugin.GnuPG.Edit\ Recipients :GPGEditRecipients<CR>
1173 amenu <silent> Plugin.GnuPG.View\ Options :GPGViewOptions<CR>
1174 amenu <silent> Plugin.GnuPG.Edit\ Options :GPGEditOptions<CR>
1177 " vim600: set foldmethod=marker foldlevel=0 :