Subject: bug#6568: 23.2; easypg opens blank buffer when password is incorrect

From: Daiki Ueno <[email-masked]>
Date: Tue, 06 Jul 2010 17:47:02 +0900

Leo <[email-masked]> writes:

> Assume you have an encrypted file test.gpg and try to open it in emacs.
> At the password prompt, type in an incorrect one. A blank buffer is
> still opened.
>
> The tricky thing is if you save back the contents in that buffer
> (because accidentally you think you have typed in a correct password),
> the original content will be gone.
>
> It seems better for easypg not to open a blank buffer when password is
> incorrect.

Basically agree.  I have worndered for a long time how to implement
this, because error handling in find-file is too complex to control.

One idea is to re-throw the error in find-file-not-found-functions
(patch attached).  I'm not sure if this approach is palatable as other
basic Emacs commands.  Anyway, testing would be much appreciated.

=== modified file 'lisp/epa-file.el'
--- lisp/epa-file.el	2010-01-13 08:35:10 +0000
+++ lisp/epa-file.el	2010-07-06 08:36:24 +0000
@@ -101,6 +101,14 @@
     (insert (epa-file--decode-coding-string string (or coding-system-for-read
 						       'undecided)))))
 
+(defvar epa-file-error nil)
+(defun epa-file--find-file-not-found-function ()
+  (save-window-excursion
+    (let ((error (copy-sequence epa-file-error)))
+      (ignore (kill-buffer))
+      (signal 'file-error
+	      (cons "Opening input file" (cdr error))))))
+
 (defvar last-coding-system-used)
 (defun epa-file-insert-file-contents (file &optional visit beg end replace)
   (barf-if-buffer-read-only)
@@ -126,11 +134,16 @@
 	(progn
 	  (if replace
 	      (goto-char (point-min)))
+	  (add-hook 'find-file-not-found-functions
+		    'epa-file--find-file-not-found-function
+		    nil t)
 	  (condition-case error
 	      (setq string (epg-decrypt-file context local-file nil))
 	    (error
 	     (if (setq entry (assoc file epa-file-passphrase-alist))
 		 (setcdr entry nil))
+	     (make-local-variable 'epa-file-error)
+	     (setq epa-file-error error)
 	     (signal 'file-error
 		     (cons "Opening input file" (cdr error)))))
 	  (make-local-variable 'epa-file-encrypt-to)

Regards,
-- 
Daiki Ueno