changeset 94:2b81c74b6dda

Nevermind, those all require 7.2.
author Steve Huston <huston@srhuston.net>
date Tue, 30 Jul 2013 17:27:27 -0400
parents ab30008c9e93
children 3470659ff1ed
files .vim/plugin/gnupg.vim
diffstat 1 files changed, 142 insertions(+), 154 deletions(-) [+]
line wrap: on
line diff
--- a/.vim/plugin/gnupg.vim	Tue Jul 30 17:22:49 2013 -0400
+++ b/.vim/plugin/gnupg.vim	Tue Jul 30 17:27:27 2013 -0400
@@ -1,13 +1,10 @@
 " Name:    gnupg.vim
-" Last Change: 2011 Aug 13
-" Maintainer:  James Vega <vega.james@gmail.com>
-" Original Author:  Markus Braun <markus.braun@krawel.de>
+" Version: $Id: gnupg.vim 3026 2010-01-27 08:18:04Z mbr $
+" Author:  Markus Braun <markus.braun@krawel.de>
 " Summary: Vim plugin for transparent editing of gpg encrypted files.
-" 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
+" 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
 "
@@ -74,8 +71,7 @@
 "     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
-"     unless a "*.asc" file is being edited.
+"     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.
@@ -84,14 +80,9 @@
 "     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
+"   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
@@ -118,28 +109,25 @@
 "   - 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.
-"   - James Vega for patches for better '*.asc' handling, better filename
-"     escaping and better handling of multiple keyrings.
+"   - Tim Swast for patch to generate signed files
 "
 " 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("#BufReadPre#*.\(gpg\|asc\|pgp\)"))
   finish
 endif
-let g:loaded_gnupg = '2.2'
-let s:GPGInitRun = 0
+let g:loaded_gnupg = "$Revision: 3026 $"
 
 " check for correct vim version {{{2
-if (v:version < 702)
-  echohl ErrorMsg | echo 'plugin gnupg.vim requires Vim version >= 7.2' | echohl None
+if (v:version < 700)
+  echohl ErrorMsg | echo 'plugin gnupg.vim requires Vim version >= 7.0' | echohl None
   finish
 endif
 
@@ -148,14 +136,19 @@
 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 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()
+  autocmd BufReadPost,FileReadPost               *.\(gpg\|asc\|pgp\) call s:GPGDecrypt()
 
   " convert all text to encrypted text before writing
-  autocmd BufWriteCmd,FileWriteCmd               *.\(gpg\|asc\|pgp\) call s:GPGInit()
-  autocmd BufWriteCmd,FileWriteCmd               *.\(gpg\|asc\|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\|asc\|pgp\) call s:GPGEncryptPost()
 
   " cleanup on leaving vim
   autocmd VimLeave                               *.\(gpg\|asc\|pgp\) call s:GPGCleanup()
@@ -180,23 +173,13 @@
 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
-
   " 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 what gpg command to use
   if (!exists("g:GPGExecutable"))
     let g:GPGExecutable = "gpg --trust-model always"
@@ -214,12 +197,7 @@
 
   " 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
+    let g:GPGPreferArmor = 0
   endif
 
   " check if signed files are preferred
@@ -232,11 +210,6 @@
     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)
 
@@ -257,21 +230,16 @@
     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.
+  " 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") && !has("gui_win32"))
+  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: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"
@@ -287,7 +255,6 @@
 
   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)
@@ -317,7 +284,6 @@
   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
@@ -341,13 +307,11 @@
 function s:GPGDecrypt()
   call s:GPGDebug(3, ">>>>>>>> Entering s:GPGDecrypt()")
 
-  " get the filename of the current buffer
-  let filename = expand("<afile>:p")
+  " switch to binary mode to read the encrypted file
+  set bin
 
-  " File doesn't exist yet, so nothing to decrypt
-  if empty(glob(filename))
-    return
-  endif
+  " get the filename of the current buffer
+  let filename = escape(expand("%:p"), '\"')
 
   " clear GPGEncrypted, GPGRecipients and GPGOptions
   let b:GPGEncrypted = 0
@@ -355,18 +319,15 @@
   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)
+  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 &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 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
@@ -386,7 +347,7 @@
       echo
       echohl None
     endif
-  elseif (match(output, asymmPattern) >= 0)
+  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")
@@ -394,10 +355,10 @@
     let b:GPGOptions += ["encrypt"]
 
     " find the used public keys
-    let start = match(output, asymmPattern)
+    let start = match(output, "gpg: public key is [[:xdigit:]]\\{8}")
     while (start >= 0)
       let start = start + strlen("gpg: public key is ")
-      let recipient = matchstr(output, '[[:xdigit:]]\{8,16}', start)
+      let recipient = strpart(output, start, 8)
       call s:GPGDebug(1, "recipient is " . recipient)
       let name = s:GPGNameToID(recipient)
       if (strlen(name) > 0)
@@ -409,7 +370,7 @@
         echom "The recipient \"" . recipient . "\" is not in your public keyring!"
         echohl None
       end
-      let start = match(output, asymmPattern, start)
+      let start = match(output, "gpg: public key is [[:xdigit:]]\\{8}", start)
     endwhile
   else
     " file is not encrypted
@@ -418,7 +379,7 @@
     echohl GPGWarning
     echom "File is not encrypted, all GPG functions disabled!"
     echohl None
-    silent exe '.r ' . fnameescape(filename)
+    set nobin
     call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGDecrypt()")
     return
   endif
@@ -433,39 +394,37 @@
   " 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
+  let commandline = "'[,']!" . s:GPGCommand . " --quiet --decrypt " . 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
   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
-    silent bwipeout!
+    bwipeout
+    set nobin
     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 " . escape(expand("%:r"), ' *?\"'."'")
+  call s:GPGDebug(2, "called autocommand for " . escape(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('<afile>:r'))
-  call s:GPGDebug(2, 'called autocommand for ' . fnameescape(expand('<afile>:r')))
-  call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGBufReadPost()")
-endfunction
-
 " Function: s:GPGEncrypt() {{{2
 "
 " encrypts the buffer to all previous recipients
@@ -473,6 +432,10 @@
 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
@@ -483,10 +446,13 @@
     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! (Press ENTER)")
+    let blackhole = input("Message could not be encrypted! File might be empty! (Press ENTER)")
     echohl None
     call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEncrypt()")
     return
@@ -516,10 +482,6 @@
     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)
 
@@ -539,19 +501,57 @@
     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 destfile = tempname()
-  let commandline = "'[,']w !" . s:GPGCommand . ' --quiet --no-encrypt-to ' . options . '>' . shellescape(destfile, 1) . ' ' . s:stderrredirnull
+  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
-  let &shelltemp = s:shelltemp
   silent execute commandline
   let &shellredir = s:shellredirsave
   let &shell = s:shellsave
-  let &shelltemp = s:shelltempsave
+  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 != "")
@@ -559,19 +559,14 @@
     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
+  " restore window view
+  call winrestview(s:GPGWindowView)
+  call s:GPGDebug(2, "restored window view" . string(s:GPGWindowView))
 
-  call rename(destfile, expand('<afile>'))
-  setl nomodified
-  call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEncrypt()")
+  " refresh screen
+  redraw!
+
+  call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEncryptPost()")
 endfunction
 
 " Function: s:GPGViewRecipients() {{{2
@@ -643,7 +638,7 @@
     " 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 <buffer> call s:GPGFinishRecipientsBuffer()
@@ -653,14 +648,14 @@
         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 <buffer> call s:GPGFinishRecipientsBuffer()
       endif
 
       " empty the buffer
-      silent %delete
+      silent normal! 1GdG
     endif
 
     " Mark the buffer as a scratch buffer
@@ -707,7 +702,7 @@
     let syntaxPattern = "\\(nonexxistinwordinthisbuffer"
     for name in unknownrecipients
       let name = "!" . name
-      let syntaxPattern = syntaxPattern . "\\|" . fnameescape(name)
+      let syntaxPattern = syntaxPattern . "\\|" . name
       silent put =name
     endfor
     let syntaxPattern = syntaxPattern . "\\)"
@@ -725,10 +720,10 @@
     endif
 
     " delete the empty first line
-    silent 1delete
+    silent normal! 1Gdd
 
     " jump to the first recipient
-    silent $
+    silent normal! G
 
   endif
 
@@ -807,7 +802,7 @@
   endif
 
   " reset modified flag
-  setl nomodified
+  set nomodified
 
   call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGFinishRecipientsBuffer()")
 endfunction
@@ -865,7 +860,7 @@
     " 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 <buffer> call s:GPGFinishOptionsBuffer()
@@ -875,14 +870,14 @@
         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 <buffer> call s:GPGFinishOptionsBuffer()
       endif
 
       " empty the buffer
-      silent %delete
+      silent normal! 1GdG
     endif
 
     " Mark the buffer as a scratch buffer
@@ -914,10 +909,10 @@
     endfor
 
     " delete the empty first line
-    silent 1delete
+    silent normal! 1Gdd
 
     " jump to the first option
-    silent $
+    silent normal! G
 
     " define highlight
     if (has("syntax") && exists("g:syntax_on"))
@@ -980,7 +975,7 @@
   call setbufvar(b:GPGCorrespondingTo, "&mod", 1)
 
   " reset modified flag
-  setl nomodified
+  set nomodified
 
   call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGFinishOptionsBuffer()")
 endfunction
@@ -1030,7 +1025,7 @@
   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)
+  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
@@ -1050,37 +1045,30 @@
   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
-
-    " check if this line has already been processed
-    if !has_key(duplicates, line)
-      let duplicates[line] = 1
-
-      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
+    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"
-          else
-            let choices = choices . counter . ": ID: 0x" . identity . "\n"
-          endif
-          let counter = counter+1
-          let pubseen = 1
+    " 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
     endif
 
@@ -1096,7 +1084,7 @@
     endwhile
   endif
 
-  call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGNameToID()")
+  call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGIDToName()")
   return get(gpgids, answer, "")
 endfunction