* epa-file.el (epa-file-passphrase-callback-function): Use
[elisp/epg.git] / epa-file.el
index 5a50295..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)
@@ -152,15 +158,19 @@ May either be a string or a list of strings.")
   (if (and visit (or beg end))
       (error "Attempt to visit less than an entire file"))
   (setq file (expand-file-name file))
-  (let ((local-copy (epa-file-run-real-handler #'file-local-copy (list file)))
-       (context (epg-make-context))
-       string length entry)
+  (let* ((local-copy
+         (condition-case inl
+             (epa-file-run-real-handler #'file-local-copy (list file))
+           (error)))
+        (local-file (or local-copy file))
+        (context (epg-make-context))
+        string length entry)
     (if visit
        (setq buffer-file-name file))
     (epg-context-set-passphrase-callback
      context
      (cons #'epa-file-passphrase-callback-function
-          file))
+          local-file))
     (epg-context-set-progress-callback context
                                       #'epa-progress-callback-function)
     (unwind-protect
@@ -168,7 +178,7 @@ May either be a string or a list of strings.")
          (if replace
              (goto-char (point-min)))
          (condition-case error
-             (setq string (epg-decrypt-file context file nil))
+             (setq string (epg-decrypt-file context local-file nil))
            (error
             (if (setq entry (assoc file epa-file-passphrase-alist))
                 (setcdr entry nil))
@@ -230,7 +240,8 @@ May either be a string or a list of strings.")
                 (epa-file--encode-coding-string (buffer-substring start end)
                                                 coding-system))
               (if (or epa-file-select-keys
-                      (not (local-variable-p 'epa-file-encrypt-to)))
+                      (not (local-variable-p 'epa-file-encrypt-to
+                                             (current-buffer))))
                   (epa-select-keys
                    context
                    "Select recipents for encryption.
@@ -265,7 +276,8 @@ If no one is selected, symmetric encryption will be performed.  "
   (if (and buffer-file-name
           (string-match epa-file-name-regexp buffer-file-name)
           epa-file-inhibit-auto-save)
-      (auto-save-mode 0)))
+      (auto-save-mode 0))
+  (set-buffer-modified-p nil))
 
 (defun epa-file-select-keys ()
   "Select recipients for encryption."
@@ -285,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
@@ -295,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")))