* epa-file.el (epa-passphrase): New variable.
authorueno <ueno>
Mon, 17 Apr 2006 05:22:59 +0000 (05:22 +0000)
committerueno <ueno>
Mon, 17 Apr 2006 05:22:59 +0000 (05:22 +0000)
(epa-file-passphrase-callback-function): New function.
(epa-find-file): Reset buffer-undo-list; set
buffer-file-coding-system to last-coding-system-used after
decoding.

ChangeLog
epa-file.el

index 4f658ec..1a7b25c 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2006-04-17  Daiki Ueno  <ueno@unixuser.org>
 
+       * epa-file.el (epa-passphrase): New variable.
+       (epa-file-passphrase-callback-function): New function.
+       (epa-find-file): Reset buffer-undo-list; set
+       buffer-file-coding-system to last-coding-system-used after
+       decoding.
+
+2006-04-17  Daiki Ueno  <ueno@unixuser.org>
+
        * epa-file.el (epa-write-file): Try to write region to "/" to get
        last-coding-system-used to be set.
 
index 3d3b757..202ccdf 100644 (file)
   :group 'epa-file)
   
 (defvar epa-file nil)
+(defvar epa-passphrase nil)
 
+(defun epa-file-passphrase-callback-function (key-id buffer)
+  (save-excursion
+    (set-buffer buffer)
+    (if (and (eq key-id 'SYM)
+            epa-file)
+       (or epa-passphrase
+           (let ((passphrase (epg-passphrase-callback-function
+                              key-id buffer)))
+             (setq epa-passphrase (copy-sequence passphrase))
+             passphrase))
+      (epg-passphrase-callback-function key-id buffer))))
+
+(defvar last-coding-system-used)
 (defun epa-find-file ()
   (when (string-match epa-file-name-regexp (buffer-file-name))
     (when (file-exists-p (expand-file-name (buffer-file-name)))
       (if (fboundp 'set-buffer-multibyte)
          (set-buffer-multibyte t))
       (goto-char (point-min))
-      (insert (epg-decrypt-file (epg-make-context)
-                               (expand-file-name (buffer-file-name))
-                               nil))
+      (let ((context (epg-make-context)))
+       (epg-context-set-passphrase-callback
+        context
+        (cons #'epa-file-passphrase-callback-function
+              (current-buffer)))
+       (insert (epg-decrypt-file context
+                                 (expand-file-name (buffer-file-name))
+                                 nil)))
       (delete-region (point) (point-max))
       (decode-coding-region (point-min) (point-max) 'undecided)
-      (set-buffer-modified-p nil)
+      (if (boundp 'last-coding-system-used)
+         (set-buffer-file-coding-system last-coding-system-used))
       (set-auto-mode)
       (hack-local-variables)
-      (auto-save-mode nil))
+      (auto-save-mode nil)
+      (set-buffer-modified-p nil)
+      (setq buffer-undo-list nil))
     (make-local-variable 'epa-file)
-    (setq epa-file (buffer-file-name))))
+    (setq epa-file (buffer-file-name))
+    (make-local-variable 'epa-passphrase)))
 
-(defvar last-coding-system-used)
 (defun epa-write-file ()
   (when epa-file
     (let* ((coding-system (if (boundp 'last-coding-system-used)
                                  (write-region (point-min) (point-max) "/")
                                (error last-coding-system-used))
                            buffer-file-coding-system))
-          (coding-system-for-write 'binary))
+          (coding-system-for-write 'binary)
+          (context (epg-make-context)))
+      (epg-context-set-passphrase-callback
+       context
+       (cons #'epa-file-passphrase-callback-function
+            (current-buffer)))
       (write-region
        (epg-encrypt-string
-       (epg-make-context)
+       context
        (encode-coding-string (buffer-string) coding-system)
        (mapcar (lambda (key)
-                 (epg-sub-key-id
-                  (car (epg-key-sub-key-list key))))
-               (epa-select-keys
-                "Select recipents for encryption.
-If no one is selected, symmetric encryption will be performed.  ")))
+                 (epg-sub-key-id (car (epg-key-sub-key-list key))))
+               (unless epa-passphrase
+                 (epa-select-keys
+                  "Select recipents for encryption.
+If no one is selected, symmetric encryption will be performed.  "))))
        nil (expand-file-name (buffer-file-name))))
+    (set-visited-file-modtime)
     (set-buffer-modified-p nil)
     t))