Update.
[elisp/epg.git] / epa.el
diff --git a/epa.el b/epa.el
index b6509db..267637d 100644 (file)
--- a/epa.el
+++ b/epa.el
@@ -172,6 +172,7 @@ the separate window."
 (defvar epa-key nil)
 (defvar epa-list-keys-arguments nil)
 (defvar epa-info-buffer nil)
+(defvar epa-last-coding-system-specified nil)
 
 (defvar epa-keys-mode-map
   (let ((keymap (make-sparse-keymap)))
@@ -463,6 +464,26 @@ If SECRET is non-nil, list secret keys instead of public keys."
          (delete-window (get-buffer-window epa-keys-buffer)))
       (kill-buffer epa-keys-buffer))))
 
+(defun epa--format-fingerprint-1 (fingerprint unit-size block-size)
+  (let ((unit 0))
+    (with-temp-buffer
+      (insert fingerprint)
+      (goto-char (point-min))
+      (while (progn
+              (goto-char (+ (point) unit-size))
+              (not (eobp)))
+       (setq unit (1+ unit))
+       (insert (if (= (% unit block-size) 0) "  " " ")))
+      (buffer-string))))
+
+(defun epa--format-fingerprint (fingerprint)
+  (if fingerprint
+      (if (= (length fingerprint) 40)
+         ;; 1234 5678 9ABC DEF0 1234  5678 9ABC DEF0 1234 5678
+         (epa--format-fingerprint-1 fingerprint 4 5)
+       ;; 12 34 56 78 9A BC DE F0  12 34 56 78 9A BC DE F0
+       (epa--format-fingerprint-1 fingerprint 2 8))))
+
 (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)
@@ -526,7 +547,7 @@ If SECRET is non-nil, list secret keys instead of public keys."
                         (epg-sub-key-capability (car pointer))
                         " ")
              "\n\tFingerprint: "
-             (epg-sub-key-fingerprint (car pointer))
+             (epa--format-fingerprint (epg-sub-key-fingerprint (car pointer)))
              "\n")
       (setq pointer (cdr pointer)))
     (goto-char (point-min))
@@ -626,37 +647,45 @@ If SECRET is non-nil, list secret keys instead of public keys."
   "Sign FILE by SIGNERS keys selected."
   (interactive
    (list (expand-file-name (read-file-name "File: "))
-        (epa-select-keys (epg-make-context epa-protocol)
-                         "Select keys for signing.
+        (if current-prefix-arg
+            (epa-select-keys (epg-make-context epa-protocol)
+                             "Select keys for signing.
 If no one is selected, default secret key is used.  "
-                         nil t)
-        (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 "\
+                             nil t))
+        (if current-prefix-arg
+            (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))))))))
+                        (t
+                         (throw 'done nil))))))
+          'clear)))
   (let ((signature (concat file
-                          (if (or epa-armor
-                                  (not (memq mode '(nil t normal detached))))
-                              ".asc"
+                          (if (eq epa-protocol 'OpenPGP)
+                              (if (or epa-armor
+                                      (not (memq mode
+                                                 '(nil t normal detached))))
+                                  ".asc"
+                                (if (memq mode '(t detached))
+                                    ".sig"
+                                  ".gpg"))
                             (if (memq mode '(t detached))
-                                ".sig"
-                              ".gpg"))))
+                                ".p7s"
+                              ".p7m"))))
        (context (epg-make-context epa-protocol)))
     (epg-context-set-armor context epa-armor)
     (epg-context-set-textmode context epa-textmode)
@@ -678,7 +707,9 @@ d - Create a detached signature
         (epa-select-keys (epg-make-context epa-protocol)
                          "Select recipients for encryption.
 If no one is selected, symmetric encryption will be performed.  ")))
-  (let ((cipher (concat file (if epa-armor ".asc" ".gpg")))
+  (let ((cipher (concat file (if (eq epa-protocol 'OpenPGP)
+                                (if epa-armor ".asc" ".gpg")
+                              ".p7m")))
        (context (epg-make-context epa-protocol)))
     (epg-context-set-armor context epa-armor)
     (epg-context-set-textmode context epa-textmode)
@@ -709,7 +740,10 @@ Don't use this command in Lisp programs!"
       (message "Decrypting...done")
       (delete-region start end)
       (goto-char start)
-      (insert (epa--decode-coding-string plain coding-system-for-read))
+      (insert (epa--decode-coding-string plain
+                                        (or coding-system-for-read
+                                            (get-text-property
+                                             start 'epa-coding-system-used))))
       (if (epg-context-result-for context 'verify)
          (epa-display-info (epg-verify-result-to-string
                             (epg-context-result-for context 'verify)))))))
@@ -742,6 +776,11 @@ Don't use this command in Lisp programs!"
          (let ((coding-system-for-read coding-system))
            (epa-decrypt-region start end)))))))
 
+(if (fboundp 'select-safe-coding-system)
+    (defalias 'epa--select-safe-coding-system 'select-safe-coding-system)
+  (defun epa--select-safe-coding-system (from to)
+    buffer-file-coding-system))
+
 ;;;###autoload
 (defun epa-verify-region (start end)
   "Verify the current region between START and END.
@@ -754,7 +793,9 @@ Don't use this command in Lisp programs!"
     (epg-verify-string context
                       (epa--encode-coding-string
                        (buffer-substring start end)
-                       coding-system-for-write))
+                       (or coding-system-for-write
+                           (get-text-property start
+                                              'epa-coding-system-used))))
     (if (epg-context-result-for context 'verify)
        (epa-display-info (epg-verify-result-to-string
                           (epg-context-result-for context 'verify))))))
@@ -790,31 +831,39 @@ Don't use this command in Lisp programs!"
 
 Don't use this command in Lisp programs!"
   (interactive
-   (list (region-beginning) (region-end)
-        (epa-select-keys (epg-make-context epa-protocol)
-                         "Select keys for signing.
+   (progn
+     (setq epa-last-coding-system-specified
+          (or coding-system-for-write
+              (epa--select-safe-coding-system
+               (region-beginning) (region-end))))
+     (list (region-beginning) (region-end)
+          (if current-prefix-arg
+              (epa-select-keys (epg-make-context epa-protocol)
+                               "Select keys for signing.
 If no one is selected, default secret key is used.  "
-                         nil t)
-        (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 "\
+                               nil t))
+          (if current-prefix-arg
+              (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))))))))
+                          (t
+                           (throw 'done nil))))))
+            'clear))))
   (save-excursion
     (let ((context (epg-make-context epa-protocol))
          signature)
@@ -831,11 +880,23 @@ d - Create a detached signature
       (setq signature (epg-sign-string context
                                       (epa--encode-coding-string
                                        (buffer-substring start end)
-                                       coding-system-for-write)
+                                       epa-last-coding-system-specified)
                                       mode))
       (message "Signing...done")
       (delete-region start end)
-      (insert (epa--decode-coding-string signature coding-system-for-read)))))
+      (add-text-properties (point)
+                          (progn
+                            (insert (epa--decode-coding-string
+                                     signature
+                                     (or coding-system-for-read
+                                         epa-last-coding-system-specified)))
+                            (point))
+                          (list 'epa-coding-system-used
+                                epa-last-coding-system-specified
+                                'front-sticky nil
+                                'rear-nonsticky t
+                                'start-open t
+                                'end-open t)))))
 
 ;;;###autoload
 (defun epa-encrypt-region (start end recipients)
@@ -843,10 +904,15 @@ d - Create a detached signature
 
 Don't use this command in Lisp programs!"
   (interactive
-   (list (region-beginning) (region-end)
-        (epa-select-keys (epg-make-context epa-protocol)
-                         "Select recipients for encryption.
-If no one is selected, symmetric encryption will be performed.  ")))
+   (progn
+     (setq epa-last-coding-system-specified
+          (or coding-system-for-write
+              (epa--select-safe-coding-system
+               (region-beginning) (region-end))))
+     (list (region-beginning) (region-end)
+          (epa-select-keys (epg-make-context epa-protocol)
+                           "Select recipients for encryption.
+If no one is selected, symmetric encryption will be performed.  "))))
   (save-excursion
     (let ((context (epg-make-context epa-protocol))
          cipher)
@@ -862,11 +928,20 @@ If no one is selected, symmetric encryption will be performed.  ")))
       (setq cipher (epg-encrypt-string context
                                       (epa--encode-coding-string
                                        (buffer-substring start end)
-                                       coding-system-for-write)
+                                       epa-last-coding-system-specified)
                                       recipients))
       (message "Encrypting...done")
       (delete-region start end)
-      (insert cipher))))
+      (add-text-properties (point)
+                          (progn
+                            (insert cipher)
+                            (point))
+                          (list 'epa-coding-system-used
+                                epa-last-coding-system-specified
+                                'front-sticky nil
+                                'rear-nonsticky t
+                                'start-open t
+                                'end-open t)))))
 
 ;;;###autoload
 (defun epa-delete-keys (keys &optional allow-secret)