* gnus-art.el (gnus-mime-set-charset-parameters): Add new argument
[bpt/emacs.git] / lisp / epa.el
index ea1c4aa..0f8f252 100644 (file)
@@ -6,10 +6,10 @@
 
 ;; This file is part of GNU Emacs.
 
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; GNU Emacs is free software: you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 3, or (at your option)
-;; any later version.
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -17,9 +17,7 @@
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-;; Boston, MA 02110-1301, USA.
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
 
 ;;; Code:
 
@@ -201,7 +199,8 @@ You should bind this variable with `let', but do not set it globally.")
 (defvar epa-last-coding-system-specified nil)
 
 (defvar epa-key-list-mode-map
-  (let ((keymap (make-sparse-keymap)))
+  (let ((keymap (make-sparse-keymap))
+       (menu-map (make-sparse-keymap)))
     (define-key keymap "m" 'epa-mark-key)
     (define-key keymap "u" 'epa-unmark-key)
     (define-key keymap "d" 'epa-decrypt-file)
@@ -217,6 +216,36 @@ You should bind this variable with `let', but do not set it globally.")
     (define-key keymap " " 'scroll-up)
     (define-key keymap [delete] 'scroll-down)
     (define-key keymap "q" 'epa-exit-buffer)
+    (define-key keymap [menu-bar epa-key-list-mode] (cons "Keys" menu-map))
+    (define-key menu-map [epa-key-list-unmark-key]
+      '(menu-item "Unmark Key" epa-unmark-key
+                 :help "Unmark a key"))
+    (define-key menu-map [epa-key-list-mark-key]
+      '(menu-item "Mark Key" epa-mark-key
+                 :help "Mark a key"))
+    (define-key menu-map [separator-epa-file] '(menu-item "--"))
+    (define-key menu-map [epa-verify-file]
+      '(menu-item "Verify File..." epa-verify-file
+                 :help "Verify FILE"))
+    (define-key menu-map [epa-sign-file]
+      '(menu-item "Sign File..." epa-sign-file
+                 :help "Sign FILE by SIGNERS keys selected"))
+    (define-key menu-map [epa-decrypt-file]
+      '(menu-item "Decrypt File..." epa-decrypt-file
+                 :help "Decrypt FILE"))
+    (define-key menu-map [epa-encrypt-file]
+      '(menu-item "Encrypt File.." epa-encrypt-file
+                 :help "Encrypt FILE for RECIPIENTS"))
+    (define-key menu-map [separator-epa-key-list] '(menu-item "--"))
+    (define-key menu-map [epa-key-list-delete-keys]
+      '(menu-item "Delete keys" epa-delete-keys
+                 :help "Delete Marked Keys"))
+    (define-key menu-map [epa-key-list-import-keys]
+      '(menu-item "Import Keys" epa-import-keys
+                 :help "Import keys from a file"))
+    (define-key menu-map [epa-key-list-export-keys]
+      '(menu-item "Export Keys" epa-export-keys
+                 :help "Export marked keys to a file"))
     keymap))
 
 (defvar epa-key-mode-map
@@ -240,7 +269,8 @@ You should bind this variable with `let', but do not set it globally.")
   :help-echo 'epa--key-widget-help-echo)
 
 (defun epa--key-widget-action (widget &optional event)
-  (epa--show-key (widget-get widget :value)))
+  (save-selected-window
+    (epa--show-key (widget-get widget :value))))
 
 (defun epa--key-widget-value-create (widget)
   (let* ((key (widget-get widget :value))
@@ -298,7 +328,7 @@ You should bind this variable with `let', but do not set it globally.")
   (make-local-variable 'epa-exit-buffer-function)
   (make-local-variable 'revert-buffer-function)
   (setq revert-buffer-function 'epa--key-list-revert-buffer)
-  (run-hooks 'epa-key-list-mode-hook))
+  (run-mode-hooks 'epa-key-list-mode-hook))
 
 (defun epa-key-mode ()
   "Major mode for a key description."
@@ -315,7 +345,7 @@ You should bind this variable with `let', but do not set it globally.")
   ;; if buffer-file-name is not set.
   (font-lock-set-defaults)
   (make-local-variable 'epa-exit-buffer-function)
-  (run-hooks 'epa-key-mode-hook))
+  (run-mode-hooks 'epa-key-mode-hook))
 
 (defun epa-info-mode ()
   "Major mode for `epa-info-buffer'."
@@ -326,7 +356,7 @@ You should bind this variable with `let', but do not set it globally.")
        truncate-lines t
        buffer-read-only t)
   (use-local-map epa-info-mode-map)
-  (run-hooks 'epa-info-mode-hook))
+  (run-mode-hooks 'epa-info-mode-hook))
 
 (defun epa-mark-key (&optional arg)
   "Mark a key on the current line.
@@ -372,7 +402,7 @@ If ARG is non-nil, mark the key."
                                     'end-open t))
          (widget-create 'epa-key :value (car keys))
          (insert "\n")
-         (setq keys (cdr keys))))      
+         (setq keys (cdr keys))))
       (add-text-properties (point-min) (point-max)
                           (list 'epa-list-keys t
                                 'front-sticky nil
@@ -498,9 +528,7 @@ NAMES is a list of strings to be matched with keys.  If it is nil, all
 the keys are listed.
 If SECRET is non-nil, list secret keys instead of public keys."
   (let ((keys (epg-list-keys context names secret)))
-    (if (> (length keys) 1)
-       (epa--select-keys prompt keys)
-      keys)))
+    (epa--select-keys prompt keys)))
 
 (defun epa--show-key (key)
   (let* ((primary-sub-key (car (epg-key-sub-key-list key)))
@@ -767,7 +795,20 @@ If no one is selected, symmetric encryption will be performed.  ")))
 (defun epa-decrypt-region (start end)
   "Decrypt the current region between START and END.
 
-Don't use this command in Lisp programs!"
+Don't use this command in Lisp programs!
+Since this function operates on regions, it does some tricks such
+as coding-system detection and unibyte/multibyte conversion.  If
+you are sure how the data in the region should be treated, you
+should consider using the string based counterpart
+`epg-decrypt-string', or the file based counterpart
+`epg-decrypt-file' instead.
+
+For example:
+
+\(let ((context (epg-make-context 'OpenPGP)))
+  (decode-coding-string
+    (epg-decrypt-string context (buffer-substring start end))
+    'utf-8))"
   (interactive "r")
   (save-excursion
     (let ((context (epg-make-context epa-protocol))
@@ -814,7 +855,8 @@ Don't use this command in Lisp programs!"
 (defun epa-decrypt-armor-in-region (start end)
   "Decrypt OpenPGP armors in the current region between START and END.
 
-Don't use this command in Lisp programs!"
+Don't use this command in Lisp programs!
+See the reason described in the `epa-decrypt-region' documentation."
   (interactive "r")
   (save-excursion
     (save-restriction
@@ -840,7 +882,20 @@ Don't use this command in Lisp programs!"
 (defun epa-verify-region (start end)
   "Verify the current region between START and END.
 
-Don't use this command in Lisp programs!"
+Don't use this command in Lisp programs!
+Since this function operates on regions, it does some tricks such
+as coding-system detection and unibyte/multibyte conversion.  If
+you are sure how the data in the region should be treated, you
+should consider using the string based counterpart
+`epg-verify-string', or the file based counterpart
+`epg-verify-file' instead.
+
+For example:
+
+\(let ((context (epg-make-context 'OpenPGP)))
+  (decode-coding-string
+    (epg-verify-string context (buffer-substring start end))
+    'utf-8))"
   (interactive "r")
   (let ((context (epg-make-context epa-protocol))
        plain)
@@ -879,7 +934,8 @@ Don't use this command in Lisp programs!"
   "Verify OpenPGP cleartext signed messages in the current region
 between START and END.
 
-Don't use this command in Lisp programs!"
+Don't use this command in Lisp programs!
+See the reason described in the `epa-verify-region' documentation."
   (interactive "r")
   (save-excursion
     (save-restriction
@@ -909,7 +965,19 @@ Don't use this command in Lisp programs!"
 (defun epa-sign-region (start end signers mode)
   "Sign the current region between START and END by SIGNERS keys selected.
 
-Don't use this command in Lisp programs!"
+Don't use this command in Lisp programs!
+Since this function operates on regions, it does some tricks such
+as coding-system detection and unibyte/multibyte conversion.  If
+you are sure how the data should be treated, you should consider
+using the string based counterpart `epg-sign-string', or the file
+based counterpart `epg-sign-file' instead.
+
+For example:
+
+\(let ((context (epg-make-context 'OpenPGP)))
+  (epg-sign-string
+    context
+    (encode-coding-string (buffer-substring start end) 'utf-8)))"
   (interactive
    (let ((verbose current-prefix-arg))
      (setq epa-last-coding-system-specified
@@ -977,7 +1045,20 @@ Uses the `derived-mode-parent' property of the symbol to trace backwards."
 (defun epa-encrypt-region (start end recipients sign signers)
   "Encrypt the current region between START and END for RECIPIENTS.
 
-Don't use this command in Lisp programs!"
+Don't use this command in Lisp programs!
+Since this function operates on regions, it does some tricks such
+as coding-system detection and unibyte/multibyte conversion.  If
+you are sure how the data should be treated, you should consider
+using the string based counterpart `epg-encrypt-string', or the
+file based counterpart `epg-encrypt-file' instead.
+
+For example:
+
+\(let ((context (epg-make-context 'OpenPGP)))
+  (epg-encrypt-string
+    context
+    (encode-coding-string (buffer-substring start end) 'utf-8)
+    nil))"
   (interactive
    (let ((verbose current-prefix-arg)
         (context (epg-make-context epa-protocol))
@@ -1032,9 +1113,7 @@ If no one is selected, symmetric encryption will be performed.  ")
 
 ;;;###autoload
 (defun epa-delete-keys (keys &optional allow-secret)
-  "Delete selected KEYS.
-
-Don't use this command in Lisp programs!"
+  "Delete selected KEYS."
   (interactive
    (let ((keys (epa--marked-keys)))
      (unless keys
@@ -1045,13 +1124,11 @@ Don't use this command in Lisp programs!"
     (message "Deleting...")
     (epg-delete-keys context keys allow-secret)
     (message "Deleting...done")
-    (apply #'epa-list-keys epa-list-keys-arguments)))
+    (apply #'epa--list-keys epa-list-keys-arguments)))
 
 ;;;###autoload
 (defun epa-import-keys (file)
-  "Import keys from FILE.
-
-Don't use this command in Lisp programs!"
+  "Import keys from FILE."
   (interactive "fFile: ")
   (setq file (expand-file-name file))
   (let ((context (epg-make-context epa-protocol)))
@@ -1066,13 +1143,11 @@ Don't use this command in Lisp programs!"
        (epa-display-info (epg-import-result-to-string
                           (epg-context-result-for context 'import))))
     (if (eq major-mode 'epa-key-list-mode)
-       (apply #'epa-list-keys epa-list-keys-arguments))))
+       (apply #'epa--list-keys epa-list-keys-arguments))))
 
 ;;;###autoload
 (defun epa-import-keys-region (start end)
-  "Import keys from the region.
-
-Don't use this command in Lisp programs!"
+  "Import keys from the region."
   (interactive "r")
   (let ((context (epg-make-context epa-protocol)))
     (message "Importing...")
@@ -1089,9 +1164,7 @@ Don't use this command in Lisp programs!"
 ;;;###autoload
 (defun epa-import-armor-in-region (start end)
   "Import keys in the OpenPGP armor format in the current region
-between START and END.
-
-Don't use this command in Lisp programs!"
+between START and END."
   (interactive "r")
   (save-excursion
     (save-restriction
@@ -1111,9 +1184,7 @@ Don't use this command in Lisp programs!"
 
 ;;;###autoload
 (defun epa-export-keys (keys file)
-  "Export selected KEYS to FILE.
-
-Don't use this command in Lisp programs!"
+  "Export selected KEYS to FILE."
   (interactive
    (let ((keys (epa--marked-keys))
         default-name)
@@ -1140,9 +1211,7 @@ Don't use this command in Lisp programs!"
 
 ;;;###autoload
 (defun epa-insert-keys (keys)
-  "Insert selected KEYS after the point.
-
-Don't use this command in Lisp programs!"
+  "Insert selected KEYS after the point."
   (interactive
    (list (epa-select-keys (epg-make-context epa-protocol)
                          "Select keys to export.  ")))