Subject: eww: access bookmarks right from the URI prompt

From: Ivan Shmakov <[email-masked]>
Date: Sat, 07 Mar 2015 19:45:53 +0000

Package:  emacs
Severity: wishlist
Tags: patch

	Please consider the tentative patch MIMEd.

	* lisp/net/eww.el (eww-suggest-uris): Add eww-suggest-bookmarks
	to the default value and :options.  (Bug#???)
	(eww-suggest-bookmarks, eww-remove-annotation): New functions.
	(eww-suggested-uris, eww): Use eww-remove-annotation.

	Somehow, I believe that eww-remove-annotation may be generalized
	into something worthy of subr.el.

	On a related note, can we please make eww-bookmarks a defcustom
	and thus replace eww-write-bookmarks, eww-read-bookmarks with
	customize-save-variable and the regular custom-set-variables
	mechanism, respectively?

-- 
FSF associate member #7257  np. Begin Again — Jami Sieber 3013 B6A0 230E 334A
--- a/lisp/net/eww.el
+++ b/lisp/net/eww.el
@@ -64,7 +64,8 @@ defcustom eww-download-directory "~/Downloads/"
 (defcustom eww-suggest-uris
   '(eww-links-at-point
     url-get-url-at-point
-    eww-current-url)
+    eww-current-url
+    eww-suggest-bookmarks)
   "List of functions called to form the list of default URIs for `eww'.
 Each of the elements is a function returning either a string or a list
 of strings.  The results will be joined into a single list with
@@ -74,7 +75,8 @@ defcustom eww-suggest-uris
   :type 'hook
   :options '(eww-links-at-point
 	     url-get-url-at-point
-	     eww-current-url))
+	     eww-current-url
+	     eww-suggest-bookmarks))
 
 (defcustom eww-bookmarks-directory user-emacs-directory
   "Directory where bookmark files will be stored."
@@ -234,11 +243,30 @@ defun eww-suggested-uris nil
     (dolist (fun eww-suggest-uris)
       (let ((ret (funcall fun)))
 	(dolist (uri (if (stringp ret) (list ret) ret))
-	  (when (and uri (not (intern-soft uri obseen)))
-	    (intern uri obseen)
-	    (push   uri uris)))))
+	  (let ((key (and uri (eww-remove-annotation uri))))
+	    (when (and key (not (intern-soft key obseen)))
+	      (intern key obseen)
+	      (push   uri uris))))))
     (nreverse uris)))
 
+(defun eww-remove-annotation (string &optional start)
+  (unless start
+    (setq start 0))
+  (let ((init (get-text-property start 'annotation string))
+	(chan (next-single-property-change start 'annotation string)))
+    (if (not chan)
+	(if init nil		  ; The whole string is an annotation.
+	  string)
+      (let ((acc  nil)
+	    (from (if init chan start)))
+	(while from
+	  (let ((to (next-single-property-change from 'annotation string)))
+	    (setq acc  (concat acc (substring string from to))
+		  from (and to
+			    (next-single-property-change
+			     to 'annotation string)))))
+	acc))))
+
 ;;;###autoload
 (defun eww (url)
   "Fetch URL and render the page.
@@ -249,7 +277,8 @@ defun eww (url)
 	  (prompt (concat "Enter URL or keywords"
 			  (if uris (format " (default %s)" (car uris)) "")
 			  ": ")))
-     (list (read-string prompt nil nil uris))))
+     (when-let ((uri (read-string prompt nil nil uris)))
+       (list (eww-remove-annotation uri)))))
   (setq url (string-trim url))
   (cond ((string-match-p "\\`file:/" url))
 	;; Don't mangle file: URLs at all.
@@ -549,6 +564,22 @@ defun eww-links-at-point ()
 	(list (get-text-property (point) 'shr-url)
 	      (get-text-property (point) 'image-url))))
 
+(defun eww-suggest-bookmarks ()
+  "Return list of bookmarked URIs, if any.
+The URIs returned may contain arbitrary annotations.  Apply
+`eww-remove-annotation' on elements of the list returned to obtain the
+URIs proper."
+  (mapcar (lambda (elt)
+	    (let ((uri (plist-get elt :url))
+		  (title (plist-get elt :title)))
+	      (when uri
+		(concat uri
+			(propertize " "
+				    'display (concat " (" title ")")
+				    'annotation t
+				    'face 'minibuffer-prompt)))))
+	  eww-bookmarks))
+
 (defun eww-view-source ()
   "View the HTML source code of the current page."
   (interactive)