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