* epa-file.el (epa-write-file): Try to write region to "/" to get
authorueno <ueno>
Mon, 17 Apr 2006 04:57:22 +0000 (04:57 +0000)
committerueno <ueno>
Mon, 17 Apr 2006 04:57:22 +0000 (04:57 +0000)
last-coding-system-used to be set.

ChangeLog
epa-file.el

index b385a4b..4f658ec 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,12 @@
 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.
+
+2006-04-17  Daiki Ueno  <ueno@unixuser.org>
+
        * epa-file.el: New implementation of epf.el.
+
        * epf.el: Abolished.
 
 2006-04-17  Daiki Ueno  <ueno@unixuser.org>
index 093c66e..3d3b757 100644 (file)
 
 (defun epa-find-file ()
   (when (string-match epa-file-name-regexp (buffer-file-name))
-    (if (= (buffer-size) 0)
-       (progn
-         (set-auto-mode)
-         (hack-local-variables)
-         (auto-save-mode nil))
-      (goto-char (point-min))
+    (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))
       (delete-region (point) (point-max))
       (decode-coding-region (point-min) (point-max) 'undecided)
-      (set-buffer-modified-p nil))
+      (set-buffer-modified-p nil)
+      (set-auto-mode)
+      (hack-local-variables)
+      (auto-save-mode nil))
     (make-local-variable 'epa-file)
     (setq epa-file (buffer-file-name))))
 
+(defvar last-coding-system-used)
 (defun epa-write-file ()
   (when epa-file
-    (write-region
-     (epg-encrypt-string
-      (epg-make-context)
-      (buffer-string)
-      (mapcar (lambda (key)
-               (epg-sub-key-id
-                (car (epg-key-sub-key-list key))))
-             (epa-select-keys
+    (let* ((coding-system (if (boundp 'last-coding-system-used)
+                             (condition-case nil
+                                 (write-region (point-min) (point-max) "/")
+                               (error last-coding-system-used))
+                           buffer-file-coding-system))
+          (coding-system-for-write 'binary))
+      (write-region
+       (epg-encrypt-string
+       (epg-make-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.  ")))
-     nil (expand-file-name (buffer-file-name)))
+       nil (expand-file-name (buffer-file-name))))
     (set-buffer-modified-p nil)
     t))