Subject: [PATCH] Make key selection method configurable in EPA.
From: Aleksandr Vityazev <[email-masked]>
To: [email-masked]
Date: Wed, 14 Feb 2024 22:47:08 +0300
List-Id: <debbugs-submit.debbugs.gnu.org>

Tags: patch

Hi,

Currently in epa.el it is possible to select keys only through a
separate buffer, I think adding the option to select via minibuffer
would be a nice addition. The current behavior is set to default. Any
comments?

From 95e277514bc7b92858b92dacca66683dd31e6da2 Mon Sep 17 00:00:00 2001
Message-ID: <9[email-masked]>
From: Aleksandr Vityazev <[email-masked]>
Date: Wed, 14 Feb 2024 22:43:04 +0300
Subject: [PATCH] Make key selection method configurable in EPA.

* lisp/epa.el (epa-keys-select-method): New defcustom.
(epa--select-keys-in-minibuffer): New function.
(epa-select-keys): Use new option and function.
* etc/NEWS: Document it.
---
 etc/NEWS    |  8 ++++++++
 lisp/epa.el | 30 +++++++++++++++++++++++++++++-
 2 files changed, 37 insertions(+), 1 deletion(-)

diff --git a/etc/NEWS b/etc/NEWS
index e6b1d424499..6cd7c82b69f 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1352,6 +1352,14 @@ characters, such as ½ (U+00BD VULGAR FRACTION ONE HALF), are also
 recognized as rational fractions.  They have been since 2004, but it
 looks like it was never mentioned in the NEWS, or even the manual.

+** EasyPG
+
+---
+*** New user option 'epa-keys-select-method'
+This allows the user to customize the key selection method, a minibuffer
+or buffer option is available, which is set by default and was used in
+all earlier versions.
+
 
 * New Modes and Packages in Emacs 30.1

diff --git a/lisp/epa.el b/lisp/epa.el
index 53da3bf6cce..f78866c6eef 100644
--- a/lisp/epa.el
+++ b/lisp/epa.el
@@ -73,6 +73,18 @@ epa-mail-aliases
   :group 'epa
   :version "24.4")

+(defcustom epa-keys-select-method 'buffer
+  "Method used to select keys.
+It can have two values: buffer or minibuffer.
+Can have two values: buffer or minibuffer.  In the first case, the keys
+can be selected via a pop-up buffer. In the second case, the keys
+can be selected via a minibuffer and `completing-read-multiple' will be
+used."
+  :type '(choice (const :tag "Read keys from buffer" buffer)
+		 (const :tag "Read keys from minibuffer" minibuffer))
+  :group 'epa
+  :version "30.1")
+
 ;;; Faces

 (defgroup epa-faces nil
@@ -450,6 +462,19 @@ epa--select-keys
 	    (epa--marked-keys))
         (kill-buffer epa-keys-buffer)))))

+(defun epa--select-keys-in-minibuffer (prompt keys)
+  (let* ((keys-alist
+          (seq-map
+           (lambda (key)
+             (cons (substring-no-properties
+                    (epa--button-key-text key))
+                   key))
+           keys))
+         (selected-keys (completing-read-multiple prompt keys-alist)))
+    (seq-map
+     (lambda (key) (cdr (assoc key keys-alist)))
+     selected-keys)))
+
 ;;;###autoload
 (defun epa-select-keys (context prompt &optional names secret)
   "Display a user's keyring and ask him to select keys.
@@ -459,7 +484,10 @@ epa-select-keys
 the keys are listed.
 If SECRET is non-nil, list secret keys instead of public keys."
   (let ((keys (epg-list-keys context names secret)))
-    (epa--select-keys prompt keys)))
+    (pcase epa-keys-select-method
+      ('buffer (epa--select-keys prompt keys))
+      ('minibuffer (epa--select-keys-in-minibuffer prompt keys))
+      (_ (error "Wrong method for key selection is specified")))))

 ;;;; Key Details

--
2.41.0

-- 
Best regards,
Aleksandr Vityazev