From 520ada35ad5804b01afa84fd258d59ecc567799f Mon Sep 17 00:00:00 2001 From: Michael Heerdegen Date: Wed, 25 Mar 2020 03:55:41 +0100 Subject: [PATCH] WIP: Make standard bookmarks work for eww buffers --- etc/NEWS | 3 +++ lisp/net/eww.el | 58 +++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 54 insertions(+), 7 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index ed4722b27f..3ddfbe3d25 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -403,6 +403,9 @@ The function that is invoked when clicking on or otherwise following a 'mailto:' link in an EWW buffer can now be customized. For more information, see the related entry about 'shr-browse-url' above. +*** Support for bookmark.el. +EWW buffers can now be bookmarked with standard bookmarks. + ** Project *** New user option 'project-vc-merge-submodules'. diff --git a/lisp/net/eww.el b/lisp/net/eww.el index 2a70560ca7..3b1948649d 100644 --- a/lisp/net/eww.el +++ b/lisp/net/eww.el @@ -83,6 +83,13 @@ eww-bookmarks-directory :group 'eww :type 'directory) +(defcustom bookmark-eww-browse-url-function #'eww-browse-url + "Doc..." + :type '(choice + (function-item eww-browse-url :doc "Use eww") + (const :tag "Use value of `browse-url-browser-function'" nil) + function)) + (defcustom eww-desktop-remove-duplicates t "Whether to remove duplicates from the history when saving desktop data. If non-nil, repetitive EWW history entries (comprising of the URI, the @@ -895,6 +902,8 @@ eww-mode (setq-local desktop-save-buffer #'eww-desktop-misc-data) ;; multi-page isearch support (setq-local multi-isearch-next-buffer-function #'eww-isearch-next-buffer) + ;; Emacs bookmarks support + (setq-local bookmark-make-record-function #'eww-bookmark-make-record) (setq truncate-lines t) (buffer-disable-undo) (setq buffer-read-only t)) @@ -1720,6 +1729,11 @@ eww-toggle-colors (defvar eww-bookmarks nil) +(defun eww--bookmark-title (title-string) + (replace-regexp-in-string + "\\` +\\| +\\'" "" + (replace-regexp-in-string "[\n\t\r]" " " title-string))) + (defun eww-add-bookmark () "Bookmark the current page." (interactive) @@ -1728,13 +1742,10 @@ eww-add-bookmark (when (equal (plist-get eww-data :url) (plist-get bookmark :url)) (user-error "Already bookmarked"))) (when (y-or-n-p "Bookmark this page?") - (let ((title (replace-regexp-in-string "[\n\t\r]" " " - (plist-get eww-data :title)))) - (setq title (replace-regexp-in-string "\\` +\\| +\\'" "" title)) - (push (list :url (plist-get eww-data :url) - :title title - :time (current-time-string)) - eww-bookmarks)) + (push (list :url (plist-get eww-data :url) + :title (eww--bookmark-title (plist-get eww-data :title)) + :time (current-time-string)) + eww-bookmarks) (eww-write-bookmarks) (message "Bookmarked %s (%s)" (plist-get eww-data :url) (plist-get eww-data :title)))) @@ -1888,6 +1899,39 @@ eww-bookmark-mode (buffer-disable-undo) (setq truncate-lines t)) +;;; Emacs bookmarks support + +(declare-function bookmark-make-record-default + "bookmark" (&optional no-file no-context posn)) +(declare-function bookmark-prop-get "bookmark" (bookmark prop)) +(declare-function bookmark-default-handler "bookmark" (bmk)) + +(defun eww-bookmark-make-record () + "Create an emacs bookmark record for an eww buffer. +This implements the `bookmark-make-record-function' type (which +see)." + (let ((url (plist-get eww-data :url))) + `(,(plist-get eww-data :title) + ,@(bookmark-make-record-default 'no-file) + (url . ,url) + (defaults . (,(eww--bookmark-title (plist-get eww-data :title)) + ,url)) + (handler . ,#'bookmark-eww-bookmark-jump)))) + +(declare-function bookmark-get-bookmark-record bookmark) +;;;###autoload +(defun bookmark-eww-bookmark-jump (bookmark) + "Bookmark handler for eww buffers." + (let ((browse-url-fun (or bookmark-eww-browse-url-function + browse-url-browser-function))) + (funcall browse-url-fun (bookmark-prop-get bookmark 'url)) + (when (eq browse-url-fun #'eww-browse-url) + ;;FIXME: this doesn't work because eww renders asynchronously: + (bookmark-default-handler + `("" + (buffer . ,(current-buffer)) . + ,(bookmark-get-bookmark-record bookmark)))))) + ;;; History code (defun eww-save-history () -- 2.26.2