* epg.el (epg-export-keys-to-file): Fixed a typo.
[elisp/epg.git] / epa.el
diff --git a/epa.el b/epa.el
index c95f0e1..c59706b 100644 (file)
--- a/epa.el
+++ b/epa.el
@@ -42,7 +42,7 @@
   :type 'boolean
   :group 'epa)
 
-(defcustom epa-popup-info-window nil
+(defcustom epa-popup-info-window t
   "If non-nil, status information from epa commands is displayed on
 the separate window."
   :type 'boolean
@@ -174,7 +174,6 @@ the separate window."
     (define-key keymap "d" 'epa-decrypt-file)
     (define-key keymap "v" 'epa-verify-file)
     (define-key keymap "s" 'epa-sign-file)
-    (define-key keymap "S" 'epa-sign-keys)
     (define-key keymap "e" 'epa-encrypt-file)
     (define-key keymap "r" 'epa-delete-keys)
     (define-key keymap "i" 'epa-import-keys)
@@ -202,15 +201,15 @@ the separate window."
 (define-widget 'epa-key 'push-button
   "Button for representing a epg-key object."
   :format "%[%v%]"
-  :button-face-get 'epa-key-widget-button-face-get
-  :value-create 'epa-key-widget-value-create
-  :action 'epa-key-widget-action
-  :help-echo 'epa-key-widget-help-echo)
+  :button-face-get 'epa--key-widget-button-face-get
+  :value-create 'epa--key-widget-value-create
+  :action 'epa--key-widget-action
+  :help-echo 'epa--key-widget-help-echo)
 
-(defun epa-key-widget-action (widget &optional event)
-  (epa-show-key (widget-get widget :value)))
+(defun epa--key-widget-action (widget &optional event)
+  (epa--show-key (widget-get widget :value)))
 
-(defun epa-key-widget-value-create (widget)
+(defun epa--key-widget-value-create (widget)
   (let* ((key (widget-get widget :value))
         (primary-sub-key (car (epg-key-sub-key-list key)))
         (primary-user-id (car (epg-key-user-id-list key))))
@@ -221,22 +220,32 @@ the separate window."
                      ? ))
            (epg-sub-key-id primary-sub-key)
            " "
-           (if (stringp (epg-user-id-string primary-user-id))
-               (epg-user-id-string primary-user-id)
-             (epg-decode-dn (epg-user-id-string primary-user-id))))))
+           (if primary-user-id
+               (if (stringp (epg-user-id-string primary-user-id))
+                   (epg-user-id-string primary-user-id)
+                 (epg-decode-dn (epg-user-id-string primary-user-id)))
+             ""))))
 
-(defun epa-key-widget-button-face-get (widget)
+(defun epa--key-widget-button-face-get (widget)
   (let ((validity (epg-sub-key-validity (car (epg-key-sub-key-list
                                              (widget-get widget :value))))))
     (if validity
        (cdr (assq validity epa-validity-face-alist))
       'default)))
 
-(defun epa-key-widget-help-echo (widget)
+(defun epa--key-widget-help-echo (widget)
   (format "Show %s"
          (epg-sub-key-id (car (epg-key-sub-key-list
                                (widget-get widget :value))))))
 
+(if (fboundp 'encode-coding-string)
+    (defalias 'epa--encode-coding-string 'encode-coding-string)
+  (defalias 'epa--encode-coding-string 'identity))
+
+(if (fboundp 'decode-coding-string)
+    (defalias 'epa--decode-coding-string 'decode-coding-string)
+  (defalias 'epa--decode-coding-string 'identity))
+
 (defun epa-keys-mode ()
   "Major mode for `epa-list-keys'."
   (kill-all-local-variables)
@@ -246,18 +255,16 @@ the separate window."
        truncate-lines t
        buffer-read-only t)
   (use-local-map epa-keys-mode-map)
-  (set-keymap-parent (current-local-map) widget-keymap)
   (make-local-variable 'font-lock-defaults)
   (setq font-lock-defaults '(epa-font-lock-keywords t))
   ;; In XEmacs, auto-initialization of font-lock is not effective
   ;; if buffer-file-name is not set.
   (font-lock-set-defaults)
-  (widget-setup)
   (make-local-variable 'epa-exit-buffer-function)
   (run-hooks 'epa-keys-mode-hook))
 
 (defun epa-key-mode ()
-  "Major mode for `epa-show-key'."
+  "Major mode for a key description."
   (kill-all-local-variables)
   (buffer-disable-undo)
   (setq major-mode 'epa-key-mode
@@ -273,6 +280,48 @@ the separate window."
   (make-local-variable 'epa-exit-buffer-function)
   (run-hooks 'epa-key-mode-hook))
 
+(defun epa-info-mode ()
+  "Major mode for `epa-info-buffer'."
+  (kill-all-local-variables)
+  (buffer-disable-undo)
+  (setq major-mode 'epa-info-mode
+       mode-name "Info"
+       truncate-lines t
+       buffer-read-only t)
+  (use-local-map epa-info-mode-map)
+  (run-hooks 'epa-info-mode-hook))
+
+(defun epa-mark (&optional arg)
+  "Mark the current line.
+If ARG is non-nil, unmark the current line."
+  (interactive "P")
+  (let ((inhibit-read-only t)
+       buffer-read-only
+       properties)
+    (beginning-of-line)
+    (setq properties (text-properties-at (point)))
+    (delete-char 1)
+    (insert (if arg " " "*"))
+    (set-text-properties (1- (point)) (point) properties)
+    (forward-line)))
+
+(defun epa-unmark (&optional arg)
+  "Unmark the current line.
+If ARG is non-nil, mark the current line."
+  (interactive "P")
+  (epa-mark (not arg)))
+
+(defun epa-toggle-mark ()
+  "Toggle the mark the current line."
+  (interactive)
+  (epa-mark (eq (char-after (save-excursion (beginning-of-line) (point))) ?*)))
+
+(defun epa-exit-buffer ()
+  "Exit the current buffer.
+`epa-exit-buffer-function' is called if it is set."
+  (interactive)
+  (funcall epa-exit-buffer-function))
+
 ;;;###autoload
 (defun epa-list-keys (&optional name mode protocol)
   (interactive
@@ -301,14 +350,16 @@ the separate window."
                     (or (next-single-property-change point 'epa-list-keys)
                         (point-max)))
       (goto-char point))
-    (epa-insert-keys context name mode)
-    (epa-keys-mode))
+    (epa--insert-keys context name mode)
+    (epa-keys-mode)
+    (widget-setup)
+    (set-keymap-parent (current-local-map) widget-keymap))
   (make-local-variable 'epa-list-keys-arguments)
   (setq epa-list-keys-arguments (list name mode protocol))
   (goto-char (point-min))
   (pop-to-buffer (current-buffer)))
 
-(defun epa-insert-keys (context name mode)
+(defun epa--insert-keys (context name mode)
   (save-excursion
     (save-restriction
       (narrow-to-region (point) (point))
@@ -333,7 +384,7 @@ the separate window."
                                 'start-open t
                                 'end-open t)))))
 
-(defun epa-marked-keys ()
+(defun epa--marked-keys ()
   (or (save-excursion
        (set-buffer epa-keys-buffer)
        (goto-char (point-min))
@@ -381,30 +432,32 @@ If SECRET is non-nil, list secret keys instead of public keys."
       (insert "\n\n")
       (if names
          (while names
-           (epa-insert-keys context (car names) secret)
+           (epa--insert-keys context (car names) secret)
            (if (get-text-property (point) 'epa-list-keys)
                (epa-mark))
            (goto-char (point-max))
            (setq names (cdr names)))
        (if secret
            (progn
-             (epa-insert-keys context nil secret)
+             (epa--insert-keys context nil secret)
              (if (get-text-property (point) 'epa-list-keys)
                  (epa-mark)))
-         (epa-insert-keys context nil nil)))
+         (epa--insert-keys context nil nil)))
       (epa-keys-mode)
+      (widget-setup)
+      (set-keymap-parent (current-local-map) widget-keymap)
       (setq epa-exit-buffer-function #'abort-recursive-edit)
       (goto-char (point-min))
       (pop-to-buffer (current-buffer)))
     (unwind-protect
        (progn
          (recursive-edit)
-         (epa-marked-keys))
+         (epa--marked-keys))
       (if (get-buffer-window epa-keys-buffer)
          (delete-window (get-buffer-window epa-keys-buffer)))
       (kill-buffer epa-keys-buffer))))
 
-(defun epa-show-key (key)
+(defun epa--show-key (key)
   (let* ((primary-sub-key (car (epg-key-sub-key-list key)))
         (entry (assoc (epg-sub-key-id primary-sub-key)
                       epa-key-buffer-alist))
@@ -424,17 +477,18 @@ If SECRET is non-nil, list secret keys instead of public keys."
     (erase-buffer)
     (setq pointer (epg-key-user-id-list key))
     (while pointer
-      (insert " "
-             (if (epg-user-id-validity (car pointer))
-                 (char-to-string
-                  (car (rassq (epg-user-id-validity (car pointer))
-                              epg-key-validity-alist)))
-               " ")
-             " "
-             (if (stringp (epg-user-id-string (car pointer)))
-                 (epg-user-id-string (car pointer))
-               (epg-decode-dn (epg-user-id-string (car pointer))))
-             "\n")
+      (if (car pointer)
+         (insert " "
+                 (if (epg-user-id-validity (car pointer))
+                     (char-to-string
+                      (car (rassq (epg-user-id-validity (car pointer))
+                                  epg-key-validity-alist)))
+                   " ")
+                 " "
+                 (if (stringp (epg-user-id-string (car pointer)))
+                     (epg-user-id-string (car pointer))
+                   (epg-decode-dn (epg-user-id-string (car pointer))))
+                 "\n"))
       (setq pointer (cdr pointer)))
     (setq pointer (epg-key-sub-key-list key))
     (while pointer
@@ -473,43 +527,9 @@ If SECRET is non-nil, list secret keys instead of public keys."
     (pop-to-buffer (current-buffer))
     (epa-key-mode)))
 
-(defun epa-show-key-notify (widget &rest ignore)
-  (epa-show-key (widget-get widget :value)))
-
-(defun epa-mark (&optional arg)
-  "Mark the current line.
-If ARG is non-nil, unmark the current line."
-  (interactive "P")
-  (let ((inhibit-read-only t)
-       buffer-read-only
-       properties)
-    (beginning-of-line)
-    (setq properties (text-properties-at (point)))
-    (delete-char 1)
-    (insert (if arg " " "*"))
-    (set-text-properties (1- (point)) (point) properties)
-    (forward-line)))
-
-(defun epa-unmark (&optional arg)
-  "Unmark the current line.
-If ARG is non-nil, mark the current line."
-  (interactive "P")
-  (epa-mark (not arg)))
-
-(defun epa-toggle-mark ()
-  "Toggle the mark the current line."
-  (interactive)
-  (epa-mark (eq (char-after (save-excursion (beginning-of-line) (point))) ?*)))
-
-(defun epa-exit-buffer ()
-  "Exit the current buffer.
-`epa-exit-buffer-function' is called if it is set."
-  (interactive)
-  (funcall epa-exit-buffer-function))
-
-(defun epa-display-verify-result (verify-result)
+(defun epa-display-info (info)
   (if epa-popup-info-window
-      (progn
+      (save-selected-window
        (unless epa-info-buffer
          (setq epa-info-buffer (generate-new-buffer "*Info*")))
        (save-excursion
@@ -517,29 +537,46 @@ If ARG is non-nil, mark the current line."
          (let ((inhibit-read-only t)
                buffer-read-only)
            (erase-buffer)
-           (insert (epg-verify-result-to-string verify-result)))
-         (epa-info-mode))
-       (pop-to-buffer epa-info-buffer)
-       (if (> (window-height) epa-info-window-height)
-           (shrink-window (- (window-height) epa-info-window-height)))
-       (goto-char (point-min)))
-    (message "%s" (epg-verify-result-to-string verify-result))))
+           (insert info))
+         (epa-info-mode)
+         (goto-char (point-min)))
+       (if (> (window-height)
+              epa-info-window-height)
+           (set-window-buffer (split-window nil (- (window-height)
+                                                   epa-info-window-height))
+                              epa-info-buffer)
+         (pop-to-buffer epa-info-buffer)
+         (if (> (window-height) epa-info-window-height)
+             (shrink-window (- (window-height) epa-info-window-height)))))
+    (message "%s" info)))
 
-(defun epa-info-mode ()
-  "Major mode for `epa-info-buffer'."
-  (kill-all-local-variables)
-  (buffer-disable-undo)
-  (setq major-mode 'epa-info-mode
-       mode-name "Info"
-       truncate-lines t
-       buffer-read-only t)
-  (use-local-map epa-info-mode-map)
-  (run-hooks 'epa-info-mode-hook))
+(defun epa-display-verify-result (verify-result)
+  (epa-display-info (epg-verify-result-to-string verify-result)))
+(make-obsolete 'epa-display-verify-result 'epa-display-info)
+
+(defun epa-passphrase-callback-function (context key-id handback)
+  (if (eq key-id 'SYM)
+      (read-passwd "Passphrase for symmetric encryption: "
+                  (eq (epg-context-operation context) 'encrypt))
+    (read-passwd
+     (if (eq key-id 'PIN)
+       "Passphrase for PIN: "
+       (let ((entry (assoc key-id epg-user-id-alist)))
+        (if entry
+            (format "Passphrase for %s %s: " key-id (cdr entry))
+          (format "Passphrase for %s: " key-id)))))))
+
+(defun epa-progress-callback-function (context what char current total
+                                              handback)
+  (message "%s: %d%% (%d/%d)" what
+          (if (> total 0) (floor (* (/ current (float total)) 100)) 0)
+          current total))
 
 ;;;###autoload
 (defun epa-decrypt-file (file)
   "Decrypt FILE."
   (interactive "fFile: ")
+  (setq file (expand-file-name file))
   (let* ((default-name (file-name-sans-extension file))
         (plain (expand-file-name
                 (read-file-name
@@ -551,24 +588,32 @@ If ARG is non-nil, mark the current line."
         (context (epg-make-context)))
     (epg-context-set-passphrase-callback context
                                         #'epa-passphrase-callback-function)
+    (epg-context-set-progress-callback context
+                                      #'epa-progress-callback-function)
     (message "Decrypting %s..." (file-name-nondirectory file))
     (epg-decrypt-file context file plain)
-    (message "Decrypting %s...done" (file-name-nondirectory file))
+    (message "Decrypting %s...wrote %s" (file-name-nondirectory file)
+            (file-name-nondirectory plain))
     (if (epg-context-result-for context 'verify)
-       (epa-display-verify-result (epg-context-result-for context 'verify)))))
+       (epa-display-info (epg-verify-result-to-string
+                          (epg-context-result-for context 'verify))))))
 
 ;;;###autoload
 (defun epa-verify-file (file)
   "Verify FILE."
   (interactive "fFile: ")
+  (setq file (expand-file-name file))
   (let* ((context (epg-make-context))
         (plain (if (equal (file-name-extension file) "sig")
                    (file-name-sans-extension file))))
+    (epg-context-set-progress-callback context
+                                      #'epa-progress-callback-function)
     (message "Verifying %s..." (file-name-nondirectory file))
     (epg-verify-file context file plain)
     (message "Verifying %s...done" (file-name-nondirectory file))
     (if (epg-context-result-for context 'verify)
-       (epa-display-verify-result (epg-context-result-for context 'verify)))))
+       (epa-display-info (epg-verify-result-to-string
+                          (epg-context-result-for context 'verify))))))
 
 ;;;###autoload
 (defun epa-sign-file (file signers mode)
@@ -578,10 +623,26 @@ If ARG is non-nil, mark the current line."
         (epa-select-keys (epg-make-context) "Select keys for signing.
 If no one is selected, default secret key is used.  "
                          nil t)
-        (if (y-or-n-p "Make a detached signature? ")
-            'detached
-          (if (y-or-n-p "Make a cleartext signature? ")
-              'clear))))
+        (catch 'done
+          (while t
+            (message "Signature type (n,c,d,?) ")
+            (let ((c (read-char)))
+              (cond ((eq c ?c)
+                     (throw 'done 'clear))
+                    ((eq c ?d)
+                     (throw 'done 'detached))
+                    ((eq c ??)
+                     (with-output-to-temp-buffer "*Help*"
+                       (save-excursion
+                         (set-buffer standard-output)
+                         (insert "\
+n - Create a normal signature
+c - Create a cleartext signature
+d - Create a detached signature
+? - Show this help
+"))))
+                    (t
+                     (throw 'done nil))))))))
   (let ((signature (concat file
                           (if (or epa-armor
                                   (not (memq mode '(nil t normal detached))))
@@ -595,9 +656,12 @@ If no one is selected, default secret key is used.  "
     (epg-context-set-signers context signers)
     (epg-context-set-passphrase-callback context
                                         #'epa-passphrase-callback-function)
+    (epg-context-set-progress-callback context
+                                      #'epa-progress-callback-function)
     (message "Signing %s..." (file-name-nondirectory file))
     (epg-sign-file context file signature mode)
-    (message "Signing %s...done" (file-name-nondirectory file))))
+    (message "Signing %s...wrote %s" (file-name-nondirectory file)
+            (file-name-nondirectory signature))))
 
 ;;;###autoload
 (defun epa-encrypt-file (file recipients)
@@ -612,9 +676,12 @@ If no one is selected, symmetric encryption will be performed.  ")))
     (epg-context-set-textmode context epa-textmode)
     (epg-context-set-passphrase-callback context
                                         #'epa-passphrase-callback-function)
+    (epg-context-set-progress-callback context
+                                      #'epa-progress-callback-function)
     (message "Encrypting %s..." (file-name-nondirectory file))
     (epg-encrypt-file context file recipients cipher)
-    (message "Encrypting %s...done" (file-name-nondirectory file))))
+    (message "Encrypting %s...wrote %s" (file-name-nondirectory file)
+            (file-name-nondirectory cipher))))
 
 ;;;###autoload
 (defun epa-decrypt-region (start end)
@@ -627,14 +694,17 @@ Don't use this command in Lisp programs!"
          plain)
       (epg-context-set-passphrase-callback context
                                           #'epa-passphrase-callback-function)
+      (epg-context-set-progress-callback context
+                                        #'epa-progress-callback-function)
       (message "Decrypting...")
       (setq plain (epg-decrypt-string context (buffer-substring start end)))
       (message "Decrypting...done")
       (delete-region start end)
       (goto-char start)
-      (insert (decode-coding-string plain coding-system-for-read))
+      (insert (epa--decode-coding-string plain coding-system-for-read))
       (if (epg-context-result-for context 'verify)
-         (epa-display-verify-result (epg-context-result-for context 'verify))))))
+         (epa-display-info (epg-verify-result-to-string
+                            (epg-context-result-for context 'verify)))))))
 
 ;;;###autoload
 (defun epa-decrypt-armor-in-region (start end)
@@ -671,16 +741,20 @@ Don't use this command in Lisp programs!"
 Don't use this command in Lisp programs!"
   (interactive "r")
   (let ((context (epg-make-context)))
+    (epg-context-set-progress-callback context
+                                      #'epa-progress-callback-function)
     (epg-verify-string context
-                      (encode-coding-string
+                      (epa--encode-coding-string
                        (buffer-substring start end)
                        coding-system-for-write))
     (if (epg-context-result-for context 'verify)
-       (epa-display-verify-result (epg-context-result-for context 'verify)))))
+       (epa-display-info (epg-verify-result-to-string
+                          (epg-context-result-for context 'verify))))))
 
 ;;;###autoload
-(defun epa-verify-armor-in-region (start end)
-  "Verify OpenPGP armors in the current region between START and END.
+(defun epa-verify-cleartext-in-region (start end)
+  "Verify OpenPGP cleartext signed messages in the current region
+between START and END.
 
 Don't use this command in Lisp programs!"
   (interactive "r")
@@ -689,20 +763,15 @@ Don't use this command in Lisp programs!"
       (narrow-to-region start end)
       (goto-char start)
       (let (armor-start armor-end)
-       (while (re-search-forward "-----BEGIN PGP\\( SIGNED\\)? MESSAGE-----$"
+       (while (re-search-forward "-----BEGIN PGP SIGNED MESSAGE-----$"
                                  nil t)
          (setq armor-start (match-beginning 0))
-         (if (match-beginning 1)       ;cleartext signed message
-             (progn
-               (unless (re-search-forward "^-----BEGIN PGP SIGNATURE-----$"
+         (unless (re-search-forward "^-----BEGIN PGP SIGNATURE-----$"
                                           nil t)
-                 (error "Invalid cleartext signed message"))
-               (setq armor-end (re-search-forward
-                                "^-----END PGP SIGNATURE-----$"
-                                nil t)))
-           (setq armor-end (re-search-forward
-                            "^-----END PGP MESSAGE-----$"
-                            nil t)))
+           (error "Invalid cleartext signed message"))
+         (setq armor-end (re-search-forward
+                          "^-----END PGP SIGNATURE-----$"
+                          nil t))
          (unless armor-end
            (error "No armor tail"))
          (epa-verify-region armor-start armor-end))))))
@@ -717,27 +786,47 @@ Don't use this command in Lisp programs!"
         (epa-select-keys (epg-make-context) "Select keys for signing.
 If no one is selected, default secret key is used.  "
                          nil t)
-        (if (y-or-n-p "Make a detached signature? ")
-            'detached
-          (if (y-or-n-p "Make a cleartext signature? ")
-              'clear))))
+        (catch 'done
+          (while t
+            (message "Signature type (n,c,d,?) ")
+            (let ((c (read-char)))
+              (cond ((eq c ?c)
+                     (throw 'done 'clear))
+                    ((eq c ?d)
+                     (throw 'done 'detached))
+                    ((eq c ??)
+                     (with-output-to-temp-buffer "*Help*"
+                       (save-excursion
+                         (set-buffer standard-output)
+                         (insert "\
+n - Create a normal signature
+c - Create a cleartext signature
+d - Create a detached signature
+? - Show this help
+"))))
+                    (t
+                     (throw 'done nil))))))))
   (save-excursion
     (let ((context (epg-make-context))
          signature)
-      (epg-context-set-armor context epa-armor)
-      (epg-context-set-textmode context epa-textmode)
+      ;;(epg-context-set-armor context epa-armor)
+      (epg-context-set-armor context t)
+      ;;(epg-context-set-textmode context epa-textmode)
+      (epg-context-set-textmode context t)
       (epg-context-set-signers context signers)
       (epg-context-set-passphrase-callback context
                                           #'epa-passphrase-callback-function)
+      (epg-context-set-progress-callback context
+                                        #'epa-progress-callback-function)
       (message "Signing...")
       (setq signature (epg-sign-string context
-                                      (encode-coding-string
+                                      (epa--encode-coding-string
                                        (buffer-substring start end)
                                        coding-system-for-write)
                                       mode))
       (message "Signing...done")
       (delete-region start end)
-      (insert (decode-coding-string signature coding-system-for-read)))))
+      (insert (epa--decode-coding-string signature coding-system-for-read)))))
 
 ;;;###autoload
 (defun epa-encrypt-region (start end recipients)
@@ -751,13 +840,17 @@ If no one is selected, symmetric encryption will be performed.  ")))
   (save-excursion
     (let ((context (epg-make-context))
          cipher)
-      (epg-context-set-armor context epa-armor)
-      (epg-context-set-textmode context epa-textmode)
+      ;;(epg-context-set-armor context epa-armor)
+      (epg-context-set-armor context t)
+      ;;(epg-context-set-textmode context epa-textmode)
+      (epg-context-set-textmode context t)
       (epg-context-set-passphrase-callback context
                                           #'epa-passphrase-callback-function)
+      (epg-context-set-progress-callback context
+                                        #'epa-progress-callback-function)
       (message "Encrypting...")
       (setq cipher (epg-encrypt-string context
-                                      (encode-coding-string
+                                      (epa--encode-coding-string
                                        (buffer-substring start end)
                                        coding-system-for-write)
                                       recipients))
@@ -767,9 +860,11 @@ If no one is selected, symmetric encryption will be performed.  ")))
 
 ;;;###autoload
 (defun epa-delete-keys (keys &optional allow-secret)
-  "Delete selected KEYS."
+  "Delete selected KEYS.
+
+Don't use this command in Lisp programs!"
   (interactive
-   (let ((keys (epa-marked-keys)))
+   (let ((keys (epa--marked-keys)))
      (unless keys
        (error "No keys selected"))
      (list keys
@@ -782,19 +877,32 @@ If no one is selected, symmetric encryption will be performed.  ")))
 
 ;;;###autoload
 (defun epa-import-keys (file)
-  "Import keys from FILE."
+  "Import keys from FILE.
+
+Don't use this command in Lisp programs!"
   (interactive "fFile: ")
+  (setq file (expand-file-name file))
   (let ((context (epg-make-context)))
     (message "Importing %s..." (file-name-nondirectory file))
-    (epg-import-keys-from-file context (expand-file-name file))
-    (message "Importing %s...done" (file-name-nondirectory file))
-    (apply #'epa-list-keys epa-list-keys-arguments)))
+    (condition-case nil
+       (progn
+         (epg-import-keys-from-file context file)
+         (message "Importing %s...done" (file-name-nondirectory file)))
+      (error
+       (message "Importing %s...failed" (file-name-nondirectory file))))
+    (if (epg-context-result-for context 'import)
+       (epa-display-info (epg-import-result-to-string
+                          (epg-context-result-for context 'import))))
+    (if (eq major-mode 'epa-keys-mode)
+       (apply #'epa-list-keys epa-list-keys-arguments))))
 
 ;;;###autoload
 (defun epa-export-keys (keys file)
-  "Export selected KEYS to FILE."
+  "Export selected KEYS to FILE.
+
+Don't use this command in Lisp programs!"
   (interactive
-   (let ((keys (epa-marked-keys))
+   (let ((keys (epa--marked-keys))
         default-name)
      (unless keys
        (error "No keys selected"))
@@ -818,38 +926,37 @@ If no one is selected, symmetric encryption will be performed.  ")))
     (message "Exporting to %s...done" (file-name-nondirectory file))))
 
 ;;;###autoload
+(defun epa-insert-keys (keys)
+  "Insert selected KEYS after the point.
+
+Don't use this command in Lisp programs!"
+  (interactive
+   (list (epa-select-keys (epg-make-context) "Select keys to export.  ")))
+  (let ((context (epg-make-context)))
+    ;;(epg-context-set-armor context epa-armor)
+    (epg-context-set-armor context t)
+    (insert (epg-export-keys-to-string context keys))))
+
+;;;###autoload
 (defun epa-sign-keys (keys &optional local)
   "Sign selected KEYS.
-If LOCAL is non-nil, the signature is marked as non exportable."
+If a prefix-arg is specified, the signature is marked as non exportable.
+
+Don't use this command in Lisp programs!"
   (interactive
-   (let ((keys (epa-marked-keys)))
+   (let ((keys (epa--marked-keys)))
      (unless keys
        (error "No keys selected"))
      (list keys current-prefix-arg)))
   (let ((context (epg-make-context)))
     (epg-context-set-passphrase-callback context
                                         #'epa-passphrase-callback-function)
+    (epg-context-set-progress-callback context
+                                      #'epa-progress-callback-function)
     (message "Signing keys...")
     (epg-sign-keys context keys local)
     (message "Signing keys...done")))
-
-(defun epa-passphrase-callback-function (context key-id handback)
-  (if (eq key-id 'SYM)
-      (read-passwd "Passphrase for symmetric encryption: "
-                  (eq (epg-context-operation context) 'encrypt))
-    (read-passwd
-     (if (eq key-id 'PIN)
-       "Passphrase for PIN: "
-       (let ((entry (assoc key-id epg-user-id-alist)))
-        (if entry
-            (format "Passphrase for %s %s: " key-id (cdr entry))
-          (format "Passphrase for %s: " key-id)))))))
-
-(defun epa-progress-callback-function (context what char current total
-                                              handback)
-  (message "%s: %d%% (%d/%d)" what
-          (if (> total 0) (floor (* (/ current (float total)) 100)) 0)
-          current total))
+(make-obsolete 'epa-sign-keys "Do not use.")
 
 (provide 'epa)