unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: "Basil L. Contovounesios" <contovob@tcd.ie>
To: Jean Louis <bugs@gnu.support>, Lars Ingebrigtsen <larsi@gnus.org>
Cc: 44338@debbugs.gnu.org, Nicholas Harrison <nicholasharrison222@gmail.com>
Subject: bug#44338: 27.1; EWW can't download and view pdf
Date: Tue, 03 Nov 2020 23:24:57 +0000	[thread overview]
Message-ID: <87a6vy6n1y.fsf@tcd.ie> (raw)
In-Reply-To: <X52SR4PGXTdD4OR8@protected.rcdrun.com> (Jean Louis's message of "Sat, 31 Oct 2020 19:35:19 +0300")

[-- Attachment #1: Type: text/plain, Size: 475 bytes --]

Jean Louis <bugs@gnu.support> writes:

> When external editor is used, buffer for eww pdf remains there in
> background *eww pdf* and neither l or q key bindings work, it would be
> expected to go back to the previous page from that buffer.

Indeed, popping to an empty *eww pdf* buffer when using an external
viewer is suboptimal.  Even less optimal is that external viewers are
called synchronously, so Emacs cannot be used simultaneously.

How's the attached?

-- 
Basil


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Improve-eww-support-for-externally-viewed-PDFs.patch --]
[-- Type: text/x-diff, Size: 4572 bytes --]

From 4595e0f159fffc93fc5be750478e52db4885d373 Mon Sep 17 00:00:00 2001
From: "Basil L. Contovounesios" <contovob@tcd.ie>
Date: Tue, 3 Nov 2020 22:54:34 +0000
Subject: [PATCH] Improve eww support for externally viewed PDFs

The *eww pdf* buffer is only needed when viewing PDFs within Emacs,
e.g., with doc-view-mode.  External PDF viewers are called with a
temporary file, so the buffer is not needed in that case.  What's
more, mailcap-view-mime erased the buffer and left it in
fundamental-mode until now, so the user was left staring at a
useless, empty buffer.  To make things even worse, external viewers
were invoked synchronously until now, so the user could not browse
the PDF file and use Emacs simultaneously.

* lisp/net/mailcap.el (mailcap--async-shell): New function.
(mailcap-view-mime): Use it to invoke external viewers
asynchronously.  Mention erasure of current buffer in that case in
docstring.  Add a period between the temporary file name and its
extension.

* lisp/net/eww.el (eww-display-pdf): Pop to *eww pdf* buffer only if
it is used for displaying a document; otherwise kill it (bug#44338).
Simplify buffer-substring+insert as insert-buffer-substring.
---
 lisp/net/eww.el     | 21 +++++++++++++--------
 lisp/net/mailcap.el | 30 ++++++++++++++++++++----------
 2 files changed, 33 insertions(+), 18 deletions(-)

diff --git a/lisp/net/eww.el b/lisp/net/eww.el
index ebc75e0e8a..e9763ef6df 100644
--- a/lisp/net/eww.el
+++ b/lisp/net/eww.el
@@ -811,14 +811,19 @@ eww-display-image
 
 (declare-function mailcap-view-mime "mailcap" (type))
 (defun eww-display-pdf ()
-  (let ((data (buffer-substring (point) (point-max))))
-    (pop-to-buffer-same-window (get-buffer-create "*eww pdf*"))
-    (let ((coding-system-for-write 'raw-text)
-	  (inhibit-read-only t))
-      (erase-buffer)
-      (insert data)
-      (mailcap-view-mime "application/pdf")))
-  (goto-char (point-min)))
+  (let ((buf (current-buffer))
+        (pos (point)))
+    (with-current-buffer (get-buffer-create "*eww pdf*")
+      (let ((coding-system-for-write 'raw-text)
+            (inhibit-read-only t))
+        (erase-buffer)
+        (insert-buffer-substring buf pos)
+        (mailcap-view-mime "application/pdf"))
+      (if (zerop (buffer-size))
+          ;; Buffer contents passed to shell command via temporary file.
+          (kill-buffer)
+        (goto-char (point-min))
+        (pop-to-buffer-same-window (current-buffer))))))
 
 (defun eww-setup-buffer ()
   (when (or (plist-get eww-data :url)
diff --git a/lisp/net/mailcap.el b/lisp/net/mailcap.el
index 94cd9e2156..ad715c4b0e 100644
--- a/lisp/net/mailcap.el
+++ b/lisp/net/mailcap.el
@@ -1128,20 +1128,30 @@ mailcap-file-default-commands
             res)))
        (nreverse res)))))
 
+(defun mailcap--async-shell (command file)
+  "Asynchronously call MIME viewer shell COMMAND.
+Replace %s in COMMAND with FILE, as per `mailcap-mime-data'.
+Delete FILE once COMMAND exits."
+  (let ((buf (get-buffer-create " *mailcap shell*")))
+    (async-shell-command (format command file) buf)
+    (add-function :after (process-sentinel (get-buffer-process buf))
+                  (lambda (proc _msg)
+                    (when (memq (process-status proc) '(exit signal))
+                      (delete-file file))))))
+
 (defun mailcap-view-mime (type)
   "View the data in the current buffer that has MIME type TYPE.
-`mailcap--computed-mime-data' determines the method to use."
+The variable `mailcap--computed-mime-data' determines the method
+to use.  If the method is a shell command string, erase the
+current buffer after passing its contents to the shell command."
   (let ((method (mailcap-mime-info type)))
     (if (stringp method)
-        (let ((file (make-temp-file "emacs-mailcap" nil
-                                    (cadr (split-string type "/")))))
-          (unwind-protect
-              (let ((coding-system-for-write 'binary))
-                (write-region (point-min) (point-max) file nil 'silent)
-                (delete-region (point-min) (point-max))
-                (shell-command (format method file)))
-            (when (file-exists-p file)
-              (delete-file file))))
+        (let* ((ext (concat "." (cadr (split-string type "/"))))
+               (file (make-temp-file "emacs-mailcap" nil ext))
+               (coding-system-for-write 'binary))
+          (write-region nil nil file nil 'silent)
+          (delete-region (point-min) (point-max))
+          (mailcap--async-shell method file))
       (funcall method))))
 
 (provide 'mailcap)
-- 
2.28.0


  parent reply	other threads:[~2020-11-03 23:24 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-30 22:20 bug#44338: 27.1; EWW can't download and view pdf Nicholas Harrison
2020-10-31 13:43 ` Basil L. Contovounesios
2020-10-31 16:35   ` Jean Louis
2020-10-31 17:14     ` Nicholas Harrison
2020-10-31 23:16       ` Nicholas Harrison
2020-11-01 14:56         ` Basil L. Contovounesios
2020-11-03 23:24     ` Basil L. Contovounesios [this message]
2020-11-05 15:13       ` Lars Ingebrigtsen
2020-11-05 17:37         ` Basil L. Contovounesios
2020-11-01 14:20   ` Lars Ingebrigtsen
2020-11-01 14:59     ` Basil L. Contovounesios
2020-11-02 14:59       ` Lars Ingebrigtsen
2020-11-02 16:50         ` Basil L. Contovounesios
2020-11-03 14:49           ` Lars Ingebrigtsen
2020-11-03 21:28             ` Basil L. Contovounesios
2020-11-05 15:12               ` Lars Ingebrigtsen
2020-11-05 17:36                 ` Basil L. Contovounesios
2020-11-05 20:40                 ` Basil L. Contovounesios
2020-11-03 23:52   ` Nicholas Harrison
2020-11-04  0:23     ` Basil L. Contovounesios
2020-11-04  1:01       ` Nicholas Harrison
2020-11-04 15:10       ` Eli Zaretskii
2020-11-04  0:25     ` Basil L. Contovounesios
2020-11-04 15:07     ` Eli Zaretskii
2020-11-04 20:01       ` Basil L. Contovounesios
2020-11-04 23:43       ` Nicholas Harrison
2020-11-05 13:42         ` Eli Zaretskii
2020-11-05 13:42         ` Eli Zaretskii
2020-11-05 15:18           ` Nicholas Harrison
2020-11-05 15:49             ` Eli Zaretskii
2020-11-05 17:52               ` Nicholas Harrison
2020-11-05 17:55                 ` Eli Zaretskii
2020-11-05 19:04                   ` Basil L. Contovounesios
2020-11-05 19:20                     ` Eli Zaretskii
2020-11-05 21:17                       ` Basil L. Contovounesios
2020-11-06  5:29                         ` Eli Zaretskii
2020-11-05 20:40                     ` Lars Ingebrigtsen
2020-11-05 21:25                       ` Basil L. Contovounesios
2020-11-06  2:12                         ` Nicholas Harrison

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87a6vy6n1y.fsf@tcd.ie \
    --to=contovob@tcd.ie \
    --cc=44338@debbugs.gnu.org \
    --cc=bugs@gnu.support \
    --cc=larsi@gnus.org \
    --cc=nicholasharrison222@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).