(epg--status-NOTATION_NAME): New function.
[elisp/epg.git] / epa.el
diff --git a/epa.el b/epa.el
index 27b0dc0..e3d0824 100644 (file)
--- a/epa.el
+++ b/epa.el
@@ -202,15 +202,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))))
@@ -225,14 +225,14 @@ the separate window."
                (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))))))
@@ -257,7 +257,7 @@ the separate window."
   (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 +273,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
@@ -333,7 +375,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))
@@ -362,8 +404,7 @@ If SECRET is non-nil, list secret keys instead of public keys."
                 (buffer-live-p epa-keys-buffer))
       (setq epa-keys-buffer (generate-new-buffer "*Keys*")))
     (let ((inhibit-read-only t)
-         buffer-read-only
-         point)
+         buffer-read-only)
       (set-buffer epa-keys-buffer)
       (erase-buffer)
       (insert prompt "\n")
@@ -382,26 +423,30 @@ If SECRET is non-nil, list secret keys instead of public keys."
       (insert "\n\n")
       (if names
          (while names
-           (setq point (point))
            (epa-insert-keys context (car names) secret)
-           (goto-char point)
-           (epa-mark)
+           (if (get-text-property (point) 'epa-list-keys)
+               (epa-mark))
            (goto-char (point-max))
            (setq names (cdr names)))
-       (epa-insert-keys context nil secret))
+       (if secret
+           (progn
+             (epa-insert-keys context nil secret)
+             (if (get-text-property (point) 'epa-list-keys)
+                 (epa-mark)))
+         (epa-insert-keys context nil nil)))
       (epa-keys-mode)
       (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))
-       (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)
+       (progn
+         (recursive-edit)
+         (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)
   (let* ((primary-sub-key (car (epg-key-sub-key-list key)))
         (entry (assoc (epg-sub-key-id primary-sub-key)
                       epa-key-buffer-alist))
@@ -470,35 +515,6 @@ 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-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)
   (if epa-popup-info-window
       (progn
@@ -517,16 +533,23 @@ If ARG is non-nil, mark the current line."
        (goto-char (point-min)))
     (message "%s" (epg-verify-result-to-string verify-result))))
 
-(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-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)
@@ -541,6 +564,10 @@ If ARG is non-nil, mark the current line."
                  (file-name-directory default-name)
                  default-name)))
         (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))
@@ -554,6 +581,8 @@ If ARG is non-nil, mark the current line."
   (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))
@@ -583,6 +612,10 @@ If no one is selected, default secret key is used.  "
     (epg-context-set-armor context epa-armor)
     (epg-context-set-textmode context epa-textmode)
     (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))))
@@ -598,6 +631,10 @@ If no one is selected, symmetric encryption will be performed.  ")))
        (context (epg-make-context)))
     (epg-context-set-armor context epa-armor)
     (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))))
@@ -611,6 +648,10 @@ Don't use this command in Lisp programs!"
   (save-excursion
     (let ((context (epg-make-context))
          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")
@@ -655,6 +696,8 @@ 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
                        (buffer-substring start end)
@@ -663,8 +706,9 @@ Don't use this command in Lisp programs!"
        (epa-display-verify-result (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")
@@ -673,20 +717,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))))))
@@ -711,6 +750,10 @@ If no one is selected, default secret key is used.  "
       (epg-context-set-armor context epa-armor)
       (epg-context-set-textmode context epa-textmode)
       (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
@@ -735,6 +778,10 @@ If no one is selected, symmetric encryption will be performed.  ")))
          cipher)
       (epg-context-set-armor context epa-armor)
       (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...")
       (setq cipher (epg-encrypt-string context
                                       (encode-coding-string
@@ -747,9 +794,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
@@ -762,7 +811,9 @@ 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: ")
   (let ((context (epg-make-context)))
     (message "Importing %s..." (file-name-nondirectory file))
@@ -772,9 +823,11 @@ If no one is selected, symmetric encryption will be performed.  ")))
 
 ;;;###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"))
@@ -800,13 +853,19 @@ If no one is selected, symmetric encryption will be performed.  ")))
 ;;;###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")))