Files
doomemacs/modules/emacs/eww/config.el
Cash Prokop-Weaver ed860b2b06 feat(eww): Add jump-to-heading in EWW
1. Add jump-to-heading functionality for EWW buffers
2. Bind within imenu (replaces +eww/jump-to-url-on-page; bound to
`:localleader l`)

The new functions are based on existing `eww--capture-url-on-page` and
`+eww/jump-to-url-on-page`. However, I took a different approach and
used alists to hide the position/coordinates from the `completing-read`.
Also, unlike `+eww/jump-to-url-on-page`, we don't give the user an
option of limiting the scope to a region or visible portion of the
buffer. `+eww/jump-to-heading-on-page` always prompts based on the
entire buffer.

Examples:

1. `<h1>H1</h1>`
    - "H1"
2. `<h1>H1</h1><h2>H2</h2>`
    - "H1"
    - "H1/h2"
3. `<h1>H1</h1><h2>H2</h2><h3>H3</h3>`
    - "H1"
    - "H1/H2"
    - "H1/H2/H3"
4. `<h1>H1-1</h1><h2>H2</h2><h1>H1-2</h1>`
    - "H1-1"
    - "H1-1/H2"
    - "H1-2"

![screenshot on the Emacs Wikipedia entry](https://github.com/user-attachments/assets/c2210f0f-c026-4325-9b1b-c2427ec13cd5)

Gaps in the hierarchy (for example a `<h2>` followed by an `<h5>`) are not
represented in the labels presented to the user. Take the Wikipedia
entry for Emacs (above) as an example. The `<h2>` "Content" is the first
heading on the page, there's no preceeding `<h1>`, so it's shown to the
user as "Content" without any prefix. Examples:

1. `<h2>H2</h2>`
    - "H2"
2. `<h2>H2</h2><h4>H4</h4>`
    - "H2"
    - "H2/H4"
3. `<h2>H2</h2><h4>H4</h4><h5>H5</h5>`
    - "H2"
    - "H2/H4"
    - "H2/H4/H5"
4. `<h2>H2</h2><h1>H1</h1><h5>H5</h5>`
    - "H2"
    - "H1"
    - "H1/H5"

- modules/emacs/eww/autoload.el
  - (eww--capture-url-on-page): Rename to `eww--capture-urls-on-page`
  - (eww--capture-headings-on-page): Add; based on existing `eww--capture-urls-on-page`
  - (+eww/jump-to-heading-on-page): Add; based on existing `+eww/jump-to-url-on-page`
- modules/emacs/eww/config.el
  - (keybind) Bind `+eww/jump-to-heading-on-page` to `<:localleader.>`;
    based on existing org-mode jump-to-heading keybind (`consult-org-heading`)
2025-06-10 21:23:31 +02:00

50 lines
2.0 KiB
EmacsLisp

;;; emacs/eww/config.el -*- lexical-binding: t; -*-
(use-package! eww
:defer t
:config
(map! :map eww-mode-map
[remap text-scale-increase] #'+eww/increase-font-size
[remap text-scale-decrease] #'+eww/decrease-font-size
[remap imenu] #'+eww/jump-to-heading-on-page
[remap quit-window] #'+eww/quit
:ni [C-return] #'+eww/open-in-other-window
:n "yy" #'+eww/copy-current-url
:n "zk" #'text-scale-increase
:n "zj" #'text-scale-decrease
(:localleader
:desc "external browser" "e" #'eww-browse-with-external-browser
:desc "buffers" "b" #'eww-switch-to-buffer
:desc "jump to link" "l" #'+eww/jump-to-url-on-page
(:prefix ("t" . "toggle")
:desc "readable" "r" #'eww-readable
:desc "colors" "c" #'eww-toggle-colors
:desc "fonts" "f" #'eww-toggle-fonts
:desc "images" "i" #'eww-toggle-images)
(:prefix ("y" . "copy")
:desc "copy url" "y" #'+eww/copy-current-url
:desc "copy for Org" "o" #'org-eww-copy-for-org-mode)))
;; HACK: There are packages that use eww to pop up html documentation; we want
;; those to open in a popup, but if the user calls `eww' directly, it should
;; open in the current window.
(defadvice! +eww-open-in-fullscreen-if-interactive-a (fn &rest args)
:around #'eww
(if (called-interactively-p 'any)
(apply fn args)
(let (display-buffer-alist)
(apply fn args))))
;; HACK: Rename the eww buffer to match the open page's title or URL.
(if (boundp 'eww-auto-rename-buffer)
(setq eww-auto-rename-buffer #'+eww-page-title-or-url) ; for >=29.1
;; REVIEW: Remove when we drop 28 support
(add-hook! 'eww-after-render-hook
(defun +eww--rename-buffer-to-page-title-or-url-h (&rest _)
(rename-buffer (+eww-page-title-or-url))))
(advice-add #'eww-back-url :after #'+eww--rename-buffer-to-page-title-or-url-h)
(advice-add #'eww-forward-url :after #'+eww--rename-buffer-to-page-title-or-url-h)))