Subject: Re: bug#19902: 25.0.50; [PATCH v2] Make eww entry point more info-like

From: Mark Oteiza <[email-masked]>
Date: Thu, 19 Feb 2015 17:55:53 -0500

---
Both `C-u M-x eww RET' and `G' (eww-goto-url) in eww prompt for URL
using `eww-suggest-uris'.

A little argument shuffling had to happen. I think I properly handled
the possible uses of `eww' and `eww-goto-url' interactively and in lisp.

Regarding the bookmark/info binding, the whole inspiration to this
change is to make the eww interface more info-like.  Info is a
good manual browser, and eww benefits from assimilating features from
info to be a good web browser.

Between the familiar M-n, eww's "S" command, and the new behavior of
`M-x eww', eww's flow has improved.  Eww is now a nice tabbed web
browser, where each tab's history doesn't get clobbered every time M-x
eww is invoked, and one doesn't have to switch-buffer or similar to find an
existing eww session.

I also have never felt compelled to cycle through my bookmarks, so I am
not troubled by losing the bookmark bindings.

 lisp/ChangeLog  | 13 +++++++++++++
 lisp/net/eww.el | 47 +++++++++++++++++++++++++++++++++++------------
 2 files changed, 48 insertions(+), 12 deletions(-)

diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index 3d15b40..fa2f252 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,3 +1,16 @@
+2015-02-19  Mark Oteiza  <[email-masked]>
+
+	* net/eww.el (eww): New behavior.  Pops to an existing buffer,
+	otherwise creates a new one.  The buffer name is controlled by
+	numeric prefix.  A non-numeric prefix always prompts for a URL.
+	(eww-goto-url): New function.  Assumes the previous role of `eww',
+	now accepting an optional `buffer' argument
+	(eww-read-string): New function.  Wrapper for `read-string'
+	used by `eww' and `eww-goto-url'.
+	(eww-setup-buffer): Accept `buffer' argument.
+	(eww-mode-map): Replace existing bindings for M-n and M-p with
+	a binding of M-n to `clone-buffer'.
+
 2015-02-19  Dima Kogan  <[email-masked]>
 
 	* autorevert.el (auto-revert-buffers-counter)
diff --git a/lisp/net/eww.el b/lisp/net/eww.el
index b7ee065..37df68e 100644
--- a/lisp/net/eww.el
+++ b/lisp/net/eww.el
@@ -239,17 +239,41 @@ This list can be customized via `eww-suggest-uris'."
 	    (push   uri uris)))))
     (nreverse uris)))
 
+(defun eww-read-string ()
+  (let* ((uris (eww-suggested-uris))
+         (prompt (concat "Enter URL or keywords"
+                         (if uris (format " (default %s)" (car uris)))
+                         ": ")))
+    (read-string prompt nil nil uris)))
+
 ;;;###autoload
-(defun eww (url)
+(defun eww (&optional url buffer)
+  "Enter eww, the Emacs Web Wowser.
+Optional argument URL specifies the target to navigate;
+the default is `eww-search-prefix'.
+
+In interactive use, a non-numeric prefix argument directs
+this command to read URL from the minibuffer.
+
+A numeric prefix argument of N selects an eww buffer named \"*eww*<N>\"."
+  (interactive
+   (let ((name (if (numberp current-prefix-arg)
+                   (format "*eww*<%s>" current-prefix-arg)
+                 "*eww*")))
+     (list (if (or (not (get-buffer name))
+                   (and current-prefix-arg (not (numberp current-prefix-arg))))
+               (eww-read-string))
+           name)))
+  (if (and (get-buffer buffer) (or (not current-prefix-arg) (numberp current-prefix-arg)))
+      (pop-to-buffer-same-window (or buffer "*eww*"))
+    (eww-goto-url url (or buffer "*eww*"))))
+
+(defun eww-goto-url (url &optional buffer)
   "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'."
   (interactive
-   (let* ((uris (eww-suggested-uris))
-	  (prompt (concat "Enter URL or keywords"
-			  (if uris (format " (default %s)" (car uris)) "")
-			  ": ")))
-     (list (read-string prompt nil nil uris))))
+   (list (eww-read-string)))
   (setq url (string-trim url))
   (cond ((string-match-p "\\`file:/" url))
 	;; Don't mangle file: URLs at all.
@@ -278,7 +302,7 @@ word(s) will be searched for via `eww-search-prefix'."
       (when (or (plist-get eww-data :url)
 		(plist-get eww-data :dom))
 	(eww-save-history))
-    (eww-setup-buffer)
+    (eww-setup-buffer buffer)
     (plist-put eww-data :url url)
     (plist-put eww-data :title "")
     (eww-update-header-line-format)
@@ -531,8 +555,8 @@ See the `eww-search-prefix' variable for the search engine used."
       (mailcap-view-mime "application/pdf")))
   (goto-char (point-min)))
 
-(defun eww-setup-buffer ()
-  (switch-to-buffer (get-buffer-create "*eww*"))
+(defun eww-setup-buffer (buffer)
+  (switch-to-buffer (get-buffer-create (or buffer "*eww*")))
   (let ((inhibit-read-only t))
     (remove-overlays)
     (erase-buffer))
@@ -624,7 +648,7 @@ the like."
   (let ((map (make-sparse-keymap)))
     (set-keymap-parent map special-mode-map)
     (define-key map "g" 'eww-reload) ;FIXME: revert-buffer-function instead!
-    (define-key map "G" 'eww)
+    (define-key map "G" 'eww-goto-url)
     (define-key map [?\t] 'shr-next-link)
     (define-key map [?\M-\t] 'shr-previous-link)
     (define-key map [backtab] 'shr-previous-link)
@@ -648,8 +672,7 @@ the like."
 
     (define-key map "b" 'eww-add-bookmark)
     (define-key map "B" 'eww-list-bookmarks)
-    (define-key map [(meta n)] 'eww-next-bookmark)
-    (define-key map [(meta p)] 'eww-previous-bookmark)
+    (define-key map [(meta n)] 'clone-buffer)
 
     (easy-menu-define nil map ""
       '("Eww"
-- 
2.3.0