Add imenu for eww

Add support for Imenu integration to EWW mode by defining a custom index function that uses HTML headings from the current buffer as menu items, and then add this hook to eww-mode-hook so that it can be triggered automatically when eww buffers are opened. This change includes code borrowed from 3b46f9c0e0
This commit is contained in:
Jeremy Dormitzer 2024-07-25 16:15:31 -04:00
parent a00ecb3fc0
commit 71d26e5e05

View File

@ -90,6 +90,28 @@ new EWW buffer."
;; Don't try to render SVGs, for some reason they are not rendered correctly
(add-to-list 'shr-external-rendering-functions
'(svg . ignore))
;; https://github.com/alphapapa/unpackaged.el/commit/3b46f9c0e0195d78df8c4ca6e1953b69539e2844
(defun imenu-eww-headings ()
"Return alist of HTML headings in current EWW buffer for Imenu.
Suitable for `imenu-create-index-function'."
(let ((faces '(shr-h1 shr-h2 shr-h3 shr-h4 shr-h5 shr-h6 shr-heading)))
(save-excursion
(save-restriction
(widen)
(goto-char (point-min))
(cl-loop for next-pos = (next-single-property-change (point) 'face)
while next-pos
do (goto-char next-pos)
for face = (get-text-property (point) 'face)
when (cl-typecase face
(list (cl-intersection face faces))
(symbol (member face faces)))
collect (cons (buffer-substring (point-at-bol) (point-at-eol)) (point))
and do (forward-line 1))))))
(add-hook 'eww-mode-hook
(lambda ()
(setq-local imenu-create-index-function #'imenu-eww-headings)))
:general
(leader-map "E" #'eww)
(normal eww-mode-map