Subject: bug#34374: 27.0.50; Outside an eww buffer, optionally use new buffer when calling eww instead of reusing *eww*

From: Göktuğ Kayaalp <[email-masked]>
Date: Sat, 09 Feb 2019 18:04:12 +0300

On 2019-02-08 08:52 +02, Eli Zaretskii <[email-masked]> wrote:
> Wouldn't it be more convenient if you could invoke eww with a prefix
> argument for that?

Sure, appended a patch, can add docs and news modifications to it if you
like it better.

>> I have manually tested the general use of EWW with this patch applied.
>> But I haven’t found a test suite for EWW; if I missed it, I can run it,
>> or any other suggested testing.
>
> You could start a test suite, although testing eww should ideally work
> even if no network connection is available.

I can try to come up with tests for the feature I’m adding.  EWW works
with file:// links, so generating a few temporary files and working with
them should work alright.  I’m not familiar enough with Emacs TDD and
EWW to try add something more comprehensive though.

>> Subject: [PATCH] Support not reusing *eww* buffer when navigating from a
>>  non-eww one
>
> It is best to reword this header line to be positive instead of
> negative.
>
>> +(defcustom eww-reuse-buffer t
>> +  "Reuse the *eww* buffer when not in an `eww-mode' buffer."
>
> For a boolean option, the first line of the doc string should say
> either
>
>   Non-nil means reuse the *eww* buffer ...
>
> or
>
>   Whether to reuse the *eww* buffer ...
>
> (the former is preferable).

I can apply these to the old patch if you think that approach is better
than the one in the new patch (using a prefix arg), which doesn’t have
these problems.

>> +When the current buffer is not in `eww-mode', if
>> +`eww-reuse-buffer' is non-nil, the *eww* buffer will be reused if
>> +available, otherwise generated; if set to nil instead, a new
>> +buffer will be used in case *eww* is already in use."
>
> Once this feature exists and is used, wouldn't it be better to program
> it so it either reuses the current EWW buffer or creates a new one,
> regardless of whether the current buffer's name is "*eww*"?  IOW,
> should we really hardcode "*eww*" in this feature?

Sure.  The literal "*eww*" appears only in two places, and could be
replaced with a defcustom and/or an extra arg to ‘eww’.

I don’t think it depends on the name of the current buffer as it is,
e.g. M-x eww ... RET M-x rename-buffer somebuf RET C-u M-x eww ... RET
does work, resulting in buffers *eww*, somebuf, *eww*<2>, and
‘eww-list-buffers’ can show a user-friendly list of buffers.  Do you
mean this scenario should result in somebuf<2> instead of *eww*<2>?  I
think that’d be confusing when calling M-x eww from some non-eww buffer,
I’d rather have new names be based on the default buffer name.

>> -     (get-buffer-create "*eww*")))
>> +     (funcall
>> +      (if eww-reuse-buffer #'get-buffer-create #'generate-new-buffer)
>> +      "*eww*")))
>
> Any particular reason to use funcall here, instead of calling the
> functions literally?

Just to not repeat the string literal.  Could definitely be replaced
with a let binding.

>From b498bf557558fb0564cb35dc663378ead5076afd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C4=B0=2E=20G=C3=B6ktu=C4=9F=20Kayaalp?= <[email-masked]>
Date: Sat, 9 Feb 2019 17:41:31 +0300
Subject: [PATCH] * lisp/net/eww.el (eww): With prefix arg, open url in new
 buffer

---
 lisp/net/eww.el | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

diff --git a/lisp/net/eww.el b/lisp/net/eww.el
index 3b7d9d5c2f..65a75f8212 100644
--- a/lisp/net/eww.el
+++ b/lisp/net/eww.el
@@ -247,21 +247,29 @@ eww-suggested-uris
     (nreverse uris)))
 
 ;;;###autoload
-(defun eww (url)
+(defun eww (url &optional arg)
   "Fetch URL and render the page.
 If the input doesn't look like an URL or a domain name, the
-word(s) will be searched for via `eww-search-prefix'."
+word(s) will be searched for via `eww-search-prefix'.
+
+If called with a prefix ARG, use a new buffer instead of reusing
+the default EWW buffer."
   (interactive
    (let* ((uris (eww-suggested-uris))
 	  (prompt (concat "Enter URL or keywords"
 			  (if uris (format " (default %s)" (car uris)) "")
 			  ": ")))
-     (list (read-string prompt nil 'eww-prompt-history uris))))
+     (list (read-string prompt nil 'eww-prompt-history uris)
+           (prefix-numeric-value current-prefix-arg))))
   (setq url (eww--dwim-expand-url url))
   (pop-to-buffer-same-window
-   (if (eq major-mode 'eww-mode)
-       (current-buffer)
-     (get-buffer-create "*eww*")))
+   (cond
+    ((eq arg 4)
+     (generate-new-buffer "*eww*"))
+    ((eq major-mode 'eww-mode)
+     (current-buffer))
+    (t
+     (get-buffer-create "*eww*"))))
   (eww-setup-buffer)
   ;; Check whether the domain only uses "Highly Restricted" Unicode
   ;; IDNA characters.  If not, transform to punycode to indicate that
-- 
2.20.1