Subject: Re: bug#16211: eww should support multiple *eww* buffers

From: Ivan Shmakov <[email-masked]>
Date: Sun, 06 Jul 2014 19:05:11 +0000

>>>>> Ted Zlatanov <[email-masked]> writes:
>>>>> On Sat, 21 Dec 2013 21:51:49 +0000 Ivan Shmakov <[email-masked]> wrote:
>>>>> "TZ" == Ted Zlatanov <[email-masked]> writes:

[…]

 TZ> I don't see a need for complicated variable passing as you
 TZ> described and I omitted, but have no strong opinion about it
 TZ> either.

 IS> Please note that eww-setup-buffer is called from the buffer filled
 IS> by url-retrieve, and /not/ from one of the EWW buffers.  I see no
 IS> obvious way for it to deduce from which buffer the original command
 IS> (say, M-x eww) was called so to get back there.

 TZ> Right, OK.  I will let you and Lars figure it out, but if you can
 TZ> propose a patch to accomplish what you describe, it will probably
 TZ> speed things up.

	Well, I’ve tried to contact assign at gnu dot org and
	copyright-clerk at fsf dot org once again some weeks ago, and
	got no reply again.  I wonder if it has anything to do with the
	way my MXes work (though I clearly see the message being
	accepted by one of the FSF/GNU ones in the logs), or that I’ve
	requested a copyright /disclaimer/ (for I believe that these of
	my changes are non-copyrightable per se anyway.)

	Nevertheless, the patch that I use for quite some time is MIMEd.
	And just in case the changes I suggest /are/ copyrightable,
	here’s the license for them:

To the extent possible under law, the author(s) have dedicated all
copyright and related and neighboring rights to this software to the
public domain worldwide.  This software is distributed without any
warranty.

You should have received a copy of the CC0 Public Domain Dedication
along with this software.  If not, see
<http://creativecommons.org/publicdomain/zero/1.0/>.

	Note though that there’re a few changes not strictly necessary.
	For instance, eww-setup-buffer now uses set-buffer instead of
	switch-to-buffer, so that it’s possible to start eww-reload in
	several EWW buffers in a row, without being switched from one to
	another at some random point.  Also, there’s a new
	eww-buffer-name variable.

-- 
FSF associate member #7257	http://boycottsystemd.org/
diff --git a/lisp/net/eww.el b/lisp/net/eww.el
index 02fc575..738e462 100644
--- a/lisp/net/eww.el
+++ b/lisp/net/eww.el
@@ -57,6 +57,12 @@
   :group 'eww
   :type 'string)
 
+(defcustom eww-buffer-name "*eww*"
+  "Buffer name to use for rendering HTML."
+  :version "24.4"
+  :group 'eww
+  :type 'string)
+
 (defcustom eww-use-external-browser-for-content-type
   "\\`\\(video/\\|audio/\\|application/ogg\\)"
   "Always use external browser for specified content-type."
@@ -125,6 +131,7 @@ See also `eww-form-checkbox-selected-symbol'."
   :group 'eww)
 
 (defvar eww-current-url nil)
+(defvar eww-current-buffer nil)
 (defvar eww-current-dom nil)
 (defvar eww-current-source nil)
 (defvar eww-current-title ""
@@ -169,7 +176,8 @@ word(s) will be searched for via `eww-search-prefix'."
                  (setq url (concat url "/"))))
            (setq url (concat eww-search-prefix
                              (replace-regexp-in-string " " "+" url))))))
-  (url-retrieve url 'eww-render (list url)))
+  (url-retrieve url 'eww-render
+		(list url nil eww-current-buffer)))
 
 ;;;###autoload (defalias 'browse-web 'eww)
 
@@ -182,7 +190,7 @@ word(s) will be searched for via `eww-search-prefix'."
 		    "/")
 	       (expand-file-name file))))
 
-(defun eww-render (status url &optional point)
+(defun eww-render (status url &optional point buffer)
   (let ((redirect (plist-get status :redirect)))
     (when redirect
       (setq url redirect)))
@@ -199,8 +207,10 @@ word(s) will be searched for via `eww-search-prefix'."
 			"utf8"))))
 	 (data-buffer (current-buffer)))
     (unwind-protect
-	(progn
-          (setq eww-current-title "")
+	(let ((eww-current-buffer (or buffer
+				      eww-current-buffer)))
+	  (with-current-buffer eww-current-buffer
+	    (setq eww-current-title ""))
 	  (cond
            ((and eww-use-external-browser-for-content-type
                  (string-match-p eww-use-external-browser-for-content-type
@@ -258,10 +268,11 @@ word(s) will be searched for via `eww-search-prefix'."
 	 (or document
 	     (list
 	      'base (list (cons 'href url))
-	      (libxml-parse-html-region (point) (point-max))))))
-    (setq eww-current-source (buffer-substring (point) (point-max)))
+	      (libxml-parse-html-region (point) (point-max)))))
+	(source (buffer-substring (point) (point-max))))
     (eww-setup-buffer)
-    (setq eww-current-dom document)
+    (setq eww-current-dom document
+	  eww-current-source source)
     (let ((inhibit-read-only t)
 	  (after-change-functions nil)
 	  (shr-width nil)
@@ -381,8 +392,12 @@ word(s) will be searched for via `eww-search-prefix'."
       (shr-put-image data nil))
     (goto-char (point-min))))
 
-(defun eww-setup-buffer ()
-  (switch-to-buffer (get-buffer-create "*eww*"))
+(defun eww-setup-buffer (&optional buffer)
+  (set-buffer
+   (cond ((not buffer)      (or eww-current-buffer
+				(get-buffer-create eww-buffer-name)))
+	 ((bufferp buffer)  buffer)
+	 (t (generate-new-buffer (or buffer eww-buffer-name)))))
   (let ((inhibit-read-only t))
     (remove-overlays)
     (erase-buffer))
@@ -401,7 +416,7 @@ word(s) will be searched for via `eww-search-prefix'."
         (source eww-current-source))
     (with-current-buffer buf
       (delete-region (point-min) (point-max))
-      (insert (or eww-current-source "no source"))
+      (insert (or source "no source"))
       (goto-char (point-min))
       (when (fboundp 'html-mode)
         (html-mode)))
@@ -479,6 +494,7 @@ word(s) will be searched for via `eww-search-prefix'."
   (setq-local eww-current-dom nil)
   (setq-local eww-current-source nil)
   (setq-local eww-current-title "")
+  (setq-local eww-current-buffer (current-buffer))
   (setq-local browse-url-browser-function 'eww-browse-url)
   (setq-local after-change-functions 'eww-process-text-input)
   (setq-local eww-history nil)
@@ -567,7 +583,7 @@ appears in a <link> or <a> tag."
   "Reload the current page."
   (interactive)
   (url-retrieve eww-current-url 'eww-render
-		(list eww-current-url (point))))
+		(list eww-current-url (point) eww-current-buffer)))
 
 ;; Form support.