From aeb1aefe743440c6f0000e740a89fc4076a66bad Mon Sep 17 00:00:00 2001 From: JSDurand Date: Thu, 16 Sep 2021 16:41:54 +0800 Subject: eww-conf: Use a giant hack * eww-conf.el (durand-eww-bookmark-jump): Now we don't have to copy the definition of eww. This is much more future-compatible. Instead we use a giant hack that re-defines the built-in function pop-to-buffer, and then restores its definition later, by loading the window.el file. This may be viewed as even uglier. Haha. --- eww-conf.el | 97 ++++++++++++++++++++++++++----------------------------------- 1 file changed, 41 insertions(+), 56 deletions(-) (limited to 'eww-conf.el') diff --git a/eww-conf.el b/eww-conf.el index ad6cbfd..2be4c9b 100644 --- a/eww-conf.el +++ b/eww-conf.el @@ -473,62 +473,47 @@ Otherwise, fetch URL and afterwards try to restore the point." ;; problems if we want to open the bookmark in another window. (cond (reuse-p - (bookmark-default-handler - (list "" (cons 'buffer buffer) front rear))) - ;; eww will pop the buffer, so we manually do the jobs: the - ;; codes are adapted from the codes for `eww'. - - ;; the buffer will be renamed afterwards, so it does not - ;; matter here - ((let ((temp-buffer (generate-new-buffer "*eww*"))) - (setq buffer temp-buffer) - (set-buffer temp-buffer) - (with-current-buffer temp-buffer - (eww-setup-buffer) - ;; Check whether the domain only uses "Highly - ;; Restricted" Unicode IDNA characters. If not, - ;; transform to punycode to indicate that there may be - ;; funny business going on. - (let ((parsed (url-generic-parse-url location))) - (when (url-host parsed) - (unless (puny-highly-restrictive-domain-p - (url-host parsed)) - (setf (url-host parsed) - (puny-encode-domain (url-host parsed))))) - ;; When the URL is on the form "http://a/../../../g", - ;; chop off all the leading "/.."s. - (when (url-filename parsed) - (while (string-match - "\\`/[.][.]/" (url-filename parsed)) - (setf (url-filename parsed) - (substring (url-filename parsed) 3)))) - (setq location (url-recreate-url parsed))) - (plist-put eww-data :url location) - (plist-put eww-data :title "") - (eww-update-header-line-format) - (let ((inhibit-read-only t)) - (insert (format "Loading %s..." location)) - (goto-char (point-min))) - (let ((url-mime-accept-string - eww-accept-content-types)) - (eww-retrieve - location #'eww-render - (list location nil (current-buffer))) - ;; Now the buffer is setup, we shall try to restore - ;; the point. But the buffer might not be ready yet, - ;; so we use the render hook for this purpose. - (add-hook - 'eww-after-render-hook - (defun durand-eww-bookmark-restore-point-hook () - "Restore the point after render." - (remove-hook - 'eww-after-render-hook - #'durand-eww-bookmark-restore-point-hook - t) - (bookmark-default-handler - (list - "" (cons 'buffer buffer) front rear))) - nil t)))))))) + (set-buffer reuse-p) + ;; we may use the default handler to restore the position here + (with-current-buffer reuse-p + (goto-char (cdr position)) + (cond + ((search-forward (cdr front) nil t) + (goto-char (match-beginning 0)))) + (cond + ((search-forward (cdr rear) nil t) + (goto-char (match-end 0)))))) + (t + ;; HACK, GIANT HACK! + + ;; I think I don't need to explain how hacky it is to define + ;; a built-in function here. Haha. + (defun pop-to-buffer (x &rest _args) (set-buffer x)) + (eww location 4) + ;; after the `set-buffer' in `eww', the current buffer is + ;; the buffer we want + (setq buffer (current-buffer)) + ;; restore the definition of pop-to-buffer... + (load "window.el" nil t) + ;; add a hook to restore the position + + ;; make sure each hook function is unique, so that different + ;; hooks don't interfere with each other. + (let ((function-symbol + (intern + (format + "eww-render-hook-%s" + (bookmark-name-from-full-record + (bookmark-get-bookmark bookmark)))))) + (fset function-symbol + (lambda () + (remove-hook + 'eww-after-render-hook function-symbol) + (bookmark-default-handler + (list + "" (cons 'buffer buffer) + front rear position)))) + (add-hook 'eww-after-render-hook function-symbol)))))) ((user-error "Cannot jump to this bookmark"))))) (provide 'eww-conf) -- cgit v1.2.3-18-g5258