emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/pdf-tools 47610debf7 6/7: Merge pull request #263 from jak


From: ELPA Syncer
Subject: [nongnu] elpa/pdf-tools 47610debf7 6/7: Merge pull request #263 from jakanakaevangeli/decrypt-no-file
Date: Thu, 14 Mar 2024 07:01:13 -0400 (EDT)

branch: elpa/pdf-tools
commit 47610debf7446318de5f6b40f7d135ace74405da
Merge: f12900eda4 20c692060a
Author: Vedang Manerikar <ved.manerikar@gmail.com>
Commit: GitHub <noreply@github.com>

    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
---
 lisp/pdf-view.el | 27 ++++++++++++++++++++-------
 1 file changed, 20 insertions(+), 7 deletions(-)

diff --git a/lisp/pdf-view.el b/lisp/pdf-view.el
index f2b8922724..bfe5f5b0fb 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 ()



reply via email to

[Prev in Thread] Current Thread [Next in Thread]