From 71d26e5e0585dede466f0b9247c87a294a05c915 Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Thu, 25 Jul 2024 16:15:31 -0400 Subject: [PATCH] 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 https://github.com/alphapapa/unpackaged.el/commit/3b46f9c0e0195d78df8c4ca6e1953b69539e2844 --- emacs/.emacs.d/config/init-eww.el | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/emacs/.emacs.d/config/init-eww.el b/emacs/.emacs.d/config/init-eww.el index ccc4f9d..cc4c12e 100644 --- a/emacs/.emacs.d/config/init-eww.el +++ b/emacs/.emacs.d/config/init-eww.el @@ -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