Add EWW functions for URL handling and buffer renaming

This commit introduces three new custom functions for EWW: `eww-rename-buffer-to-title` for renaming buffers based on page titles, `eww-capture-urls-on-page` to capture all links on the current page, `eww-visit-url-on-page` to select and visit links using completion, and `eww-jump-to-url-on-page` to jump to URLs on the page. These enhancements improve the usability and navigation of EWW.
This commit is contained in:
Jeremy Dormitzer 2024-07-23 13:18:09 -04:00
parent f3353bb124
commit 454ffb7d33

View File

@ -92,7 +92,73 @@
(list (completing-read "URL or keywords: " suggestions nil nil current-uri 'eww-prompt-history)
current-prefix-arg))))
(advice-add 'eww :before #'eww-before-advice)
(defun eww-rename-buffer-to-title ()
(rename-buffer (format "*eww: %s*" (plist-get eww-data :title)) t))
(add-hook 'eww-after-render-hook #'eww-rename-buffer-to-title)
;; Copied from https://protesilaos.com/emacs/dotemacs#h:abc20037-7a4f-4555-809a-dc4165c5db6a
(defun eww-capture-urls-on-page (&optional position)
"Capture all the links on the current web page.
Return a list of strings. Strings are in the form LABEL @ URL.
When optional argument POSITION is non-nil, include position info
in the strings too, so strings take the form
LABEL @ URL ~ POSITION."
(let (links match)
(save-excursion
(goto-char (point-max))
;; NOTE 2021-07-25: The first clause in the `or' is meant to
;; address a bug where if a URL is in `point-min' it does not get
;; captured.
(while (setq match (text-property-search-backward 'shr-url))
(let* ((raw-url (prop-match-value match))
(start-point-prop (prop-match-beginning match))
(end-point-prop (prop-match-end match))
(url (when (stringp raw-url)
(propertize raw-url 'face 'link)))
(label (replace-regexp-in-string "\n" " " ; NOTE 2021-07-25: newlines break completion
(buffer-substring-no-properties
start-point-prop end-point-prop)))
(point start-point-prop)
(line (line-number-at-pos point t))
(column (save-excursion (goto-char point) (current-column)))
(coordinates (propertize
(format "%d,%d (%d)" line column point)
'face 'shadow)))
(when url
(if position
(push (format "%-15s ~ %s @ %s"
coordinates label url)
links)
(push (format "%s @ %s"
label url)
links))))))
links))
(defun eww-visit-url-on-page (&optional arg)
"Visit URL from list of links on the page using completion.
With optional prefix ARG (\\[universal-argument]) open URL in a
new EWW buffer."
(interactive "P")
(when (derived-mode-p 'eww-mode)
(let* ((links (eww-capture-urls-on-page))
(selection (completing-read "Browse URL: " links nil t))
(url (replace-regexp-in-string ".*@ " "" selection)))
(eww url (when arg 4)))))
(defun eww-jump-to-url-on-page (&optional arg)
"Jump to URL position on the page using completion."
(interactive "P")
(when (derived-mode-p 'eww-mode)
(let* ((links (eww-capture-urls-on-page t))
(prompt (format "Jump to URL: "))
(selection (completing-read prompt links nil t))
(position (replace-regexp-in-string "^.*(\\([0-9]+\\))[\s\t]+~" "\\1" selection))
(point (string-to-number position)))
(goto-char point))))
:general
(normal eww-mode-map "go" #'eww))
(leader-map "E" #'eww)
(normal eww-mode-map
"go" #'eww
"gJ"#'eww-jump-to-url-on-page
"gV" #'eww-visit-url-on-page))
(provide 'init-built-ins)