Rearrange functions.
[elisp/epg.git] / epa.el
diff --git a/epa.el b/epa.el
index 27b0dc0..cc87f52 100644 (file)
--- a/epa.el
+++ b/epa.el
@@ -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
@@ -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,24 +423,28 @@ 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))))
+       (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)))
@@ -473,32 +518,6 @@ If SECRET is non-nil, list secret keys instead of public keys."
 (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 +536,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 +567,8 @@ 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)
     (message "Decrypting %s..." (file-name-nondirectory file))
     (epg-decrypt-file context file plain)
     (message "Decrypting %s...done" (file-name-nondirectory file))
@@ -583,6 +611,8 @@ 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)
     (message "Signing %s..." (file-name-nondirectory file))
     (epg-sign-file context file signature mode)
     (message "Signing %s...done" (file-name-nondirectory file))))
@@ -598,6 +628,8 @@ 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)
     (message "Encrypting %s..." (file-name-nondirectory file))
     (epg-encrypt-file context file recipients cipher)
     (message "Encrypting %s...done" (file-name-nondirectory file))))
@@ -611,6 +643,8 @@ 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)
       (message "Decrypting...")
       (setq plain (epg-decrypt-string context (buffer-substring start end)))
       (message "Decrypting...done")
@@ -711,6 +745,8 @@ 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)
       (message "Signing...")
       (setq signature (epg-sign-string context
                                       (encode-coding-string
@@ -735,6 +771,8 @@ 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)
       (message "Encrypting...")
       (setq cipher (epg-encrypt-string context
                                       (encode-coding-string
@@ -807,6 +845,8 @@ If LOCAL is non-nil, the signature is marked as non exportable."
        (error "No keys selected"))
      (list keys current-prefix-arg)))
   (let ((context (epg-make-context)))
+    (epg-context-set-passphrase-callback context
+                                        #'epa-passphrase-callback-function)
     (message "Signing keys...")
     (epg-sign-keys context keys local)
     (message "Signing keys...done")))