* epa-file.el (epa-file-passphrase-callback-function): Use
[elisp/epg.git] / epa-file.el
index a6a800e..551a7a0 100644 (file)
@@ -13,7 +13,7 @@
 
 ;; This program is distributed in the hope that it will be useful,
 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
-;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
@@ -82,6 +82,9 @@ May either be a string or a list of strings.")
 (defvar epa-file-handler
   (cons epa-file-name-regexp 'epa-file-handler))
 
+(defvar epa-file-auto-mode-alist-entry
+  (list epa-file-name-regexp nil 'epa-file))
+
 (defvar epa-file-passphrase-alist nil)
 
 (eval-and-compile
@@ -102,18 +105,21 @@ May either be a string or a list of strings.")
 (defun epa-file-passphrase-callback-function (context key-id file)
   (if (and epa-file-cache-passphrase-for-symmetric-encryption
           (eq key-id 'SYM))
-      (let ((entry (assoc file epa-file-passphrase-alist))
-           passphrase)
-       (or (copy-sequence (cdr entry))
-           (progn
-             (unless entry
-               (setq entry (list file)
-                     epa-file-passphrase-alist (cons entry
-                                                epa-file-passphrase-alist)))
-             (setq passphrase (epa-passphrase-callback-function context
-                                                                key-id nil))
-             (setcdr entry (copy-sequence passphrase))
-             passphrase)))
+      (progn
+       (setq file (file-truename file))
+       (let ((entry (assoc file epa-file-passphrase-alist))
+             passphrase)
+         (or (copy-sequence (cdr entry))
+             (progn
+               (unless entry
+                 (setq entry (list file)
+                       epa-file-passphrase-alist
+                       (cons entry
+                             epa-file-passphrase-alist)))
+               (setq passphrase (epa-passphrase-callback-function context
+                                                                  key-id nil))
+               (setcdr entry (copy-sequence passphrase))
+               passphrase))))
     (epa-passphrase-callback-function context key-id nil)))
 
 (defun epa-file-handler (operation &rest args)
@@ -291,6 +297,7 @@ If no one is selected, symmetric encryption will be performed.  ")))
     (setq file-name-handler-alist
          (cons epa-file-handler file-name-handler-alist))
     (add-hook 'find-file-hooks 'epa-file-find-file-hook)
+    (setq auto-mode-alist (cons epa-file-auto-mode-alist-entry auto-mode-alist))
     (message "`epa-file' enabled")))
 
 ;;;###autoload
@@ -301,6 +308,8 @@ If no one is selected, symmetric encryption will be performed.  ")))
        (setq file-name-handler-alist
              (delq epa-file-handler file-name-handler-alist))
        (remove-hook 'find-file-hooks 'epa-file-find-file-hook)
+       (setq auto-mode-alist (delq epa-file-auto-mode-alist-entry
+                                   auto-mode-alist))
        (message "`epa-file' disabled"))
     (message "`epa-file' already disabled")))