From 388950ae94558654fcc9c19bfd7530328f326919 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miha=20Rihtar=C5=A1i=C4=8D?= Date: Fri, 8 Mar 2024 17:18:40 +0100 Subject: Support decryption of non-file-visiting pdf buffers Opening a pdf e-mail attachment from within Emacs can show a pdf-view buffer, where (buffer-file-name) returns nil. --- lisp/pdf-view.el | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/lisp/pdf-view.el b/lisp/pdf-view.el index f2b8922..d45571a 100644 --- a/lisp/pdf-view.el +++ b/lisp/pdf-view.el @@ -466,12 +466,14 @@ PNG images in Emacs buffers." "Read a password, if the document is encrypted and open it." (interactive) (when (pdf-info-encrypted-p) - (let ((prompt (format "Enter password for `%s': " - (abbreviate-file-name - (buffer-file-name)))) - (key (concat "/pdf-tools" (buffer-file-name))) + (let ((fn (buffer-file-name)) + (prompt "Enter password for pdf document: ") (i 3) - password) + key password) + (when fn + (setq prompt (format "Enter password for `%s': " + (abbreviate-file-name fn))) + (setq key (concat "/pdf-tools" fn))) (while (and (> i 0) (pdf-info-encrypted-p)) (setq i (1- i)) @@ -479,7 +481,8 @@ PNG images in Emacs buffers." (setq prompt "Invalid password, try again: ") (ignore-errors (pdf-info-open nil password))) (pdf-info-open nil password) - (password-cache-add key password))) + (when key + (password-cache-add key password)))) nil) (defun pdf-view-buffer-file-name () -- cgit v1.0 From 98bfeb638aaef37c17266accdb3b5a153540d452 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miha=20Rihtar=C5=A1i=C4=8D?= Date: Sat, 9 Mar 2024 11:27:50 +0100 Subject: Don't retry with the same cached password upon decryption failure Password cache may contain a stale password if a pdf file's password is modified. Re-read a new password without cache in this case, rather than retry with the same cached password 3 times and fail. --- lisp/pdf-view.el | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lisp/pdf-view.el b/lisp/pdf-view.el index d45571a..9cfb188 100644 --- a/lisp/pdf-view.el +++ b/lisp/pdf-view.el @@ -470,14 +470,23 @@ PNG images in Emacs buffers." (prompt "Enter password for pdf document: ") (i 3) key password) + (when fn (setq prompt (format "Enter password for `%s': " (abbreviate-file-name fn))) - (setq key (concat "/pdf-tools" fn))) + (setq key (concat "/pdf-tools" fn)) + ;; First, try with a cached password + (when (setq password (password-read-from-cache key)) + (ignore-errors (pdf-info-open nil password)) + (when (pdf-info-encrypted-p) + (password-cache-remove key)))) + (while (and (> i 0) (pdf-info-encrypted-p)) (setq i (1- i)) - (setq password (password-read prompt key)) + ;; Cached password was not present or valid, try reading a new password + ;; without cache. + (setq password (password-read prompt)) (setq prompt "Invalid password, try again: ") (ignore-errors (pdf-info-open nil password))) (pdf-info-open nil password) -- cgit v1.0 From 20c692060a05bfd10bfbedc9b87fd6ab93ab2c23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miha=20Rihtar=C5=A1i=C4=8D?= Date: Sat, 9 Mar 2024 11:27:00 +0100 Subject: Support 'revert-buffer' on encrypted documents --- lisp/pdf-view.el | 1 + 1 file changed, 1 insertion(+) diff --git a/lisp/pdf-view.el b/lisp/pdf-view.el index 9cfb188..bfe5f5b 100644 --- a/lisp/pdf-view.el +++ b/lisp/pdf-view.el @@ -553,6 +553,7 @@ Optional parameters IGNORE-AUTO and NOCONFIRM are defined as in after-revert-hook))) (prog1 (revert-buffer ignore-auto noconfirm 'preserve-modes) + (pdf-view-decrypt-document) (pdf-view-redisplay t)))) (defun pdf-view-close-document () -- cgit v1.0