diff options
| author | Vedang Manerikar <ved.manerikar@gmail.com> | 2024-03-14 14:42:02 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-14 14:42:02 +0530 |
| commit | 47610debf7446318de5f6b40f7d135ace74405da (patch) | |
| tree | fbf81183e0cc21c2c15e68be53078d2c40e80132 | |
| parent | f12900eda47f45f0e27fd4e8dfa4962a51671d59 (diff) | |
| parent | 20c692060a05bfd10bfbedc9b87fd6ab93ab2c23 (diff) | |
Merge pull request #263 from jakanakaevangeli/decrypt-no-file
Improvements to PDF decryption:
- Support decryption of non-file-visiting pdf buffers, for example, when opening a pdf e-mail attachment from within Emacs
- Don't retry with the same cached password upon decryption failure, to support re-opening a pdf file with a modified password
- Don't error during 'revert-buffer' on encrypted documents
| -rw-r--r-- | lisp/pdf-view.el | 27 |
1 files changed, 20 insertions, 7 deletions
diff --git a/lisp/pdf-view.el b/lisp/pdf-view.el index f2b8922..bfe5f5b 100644 --- a/lisp/pdf-view.el +++ b/lisp/pdf-view.el @@ -466,20 +466,32 @@ 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)) + ;; 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) - (password-cache-add key password))) + (when key + (password-cache-add key password)))) nil) (defun pdf-view-buffer-file-name () @@ -541,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 () |
