Skip to content

Ensure window-start and window-hscroll remain unchanged#63

Open
jamescherti wants to merge 1 commit into
ardumont:masterfrom
jamescherti:save-restore-marker-window-start
Open

Ensure window-start and window-hscroll remain unchanged#63
jamescherti wants to merge 1 commit into
ardumont:masterfrom
jamescherti:save-restore-marker-window-start

Conversation

@jamescherti
Copy link
Copy Markdown
Contributor

@jamescherti jamescherti commented Dec 1, 2024

This commit ensures that markdown-toc-generate-toc does not change the window-start or window-hscroll, addressing a common issue that can alter these settings. (I mentioned this issue in this article, and the version of this pull request is better than the cod snippet in the article.)

@jamescherti jamescherti force-pushed the save-restore-marker-window-start branch 3 times, most recently from dc3ca39 to 9878c7b Compare December 1, 2024 05:28
Comment thread markdown-toc.el Outdated
@jamescherti jamescherti force-pushed the save-restore-marker-window-start branch 3 times, most recently from bc07ab7 to 8509575 Compare December 1, 2024 06:50
@jamescherti jamescherti changed the title Ensure window start and hscroll remain unchanged Ensure window-start and window-hscroll remain unchanged Dec 1, 2024
@jamescherti jamescherti force-pushed the save-restore-marker-window-start branch from 8509575 to 0d8f877 Compare December 1, 2024 22:46
@jamescherti jamescherti force-pushed the save-restore-marker-window-start branch 2 times, most recently from c7307d5 to 5effd04 Compare August 22, 2025 17:55
@jamescherti jamescherti force-pushed the save-restore-marker-window-start branch from 5effd04 to 4886d79 Compare March 12, 2026 12:03
@jamescherti
Copy link
Copy Markdown
Contributor Author

jamescherti commented Mar 12, 2026

I've updated the commit and commit message, @jcs090218.

Because markdown-toc modifies the buffer content while it is visible in a window, Emacs often realigns the display to keep the point visible, which frequently causes the window-start to jump. Using save-window-excursion does not fix this issue that this commit fixes.

This is noticeable when the Table of Contents is updated at the top of a file while the user is working further down.

Steps to Reproduce:

  1. Open a long Markdown file (longer than the window height).
  2. Ensure there is an existing TOC at the top, or a placeholder for one.
  3. Scroll down to the middle of the document and place your cursor on a specific line under the table of contents.
  4. Note the visual position of that line (e.g., exactly in the middle of the window).
  5. Ensure the TOC is above the cursor and visible in the current window, then run M-x markdown-toc-generate-toc.

(I recommend reducing the window width and disabling line truncation, as this makes it easier to reproduce the issue.)

@jamescherti jamescherti force-pushed the save-restore-marker-window-start branch 10 times, most recently from e2e17c4 to 64fd324 Compare March 12, 2026 13:16
Generating a table of contents involves modifying the buffer content while it is
visible in a window. When Markdown content is added or removed above the current
cursor position, Emacs often realigns the display to keep the point visible,
which frequently causes the window-start to jump. This is noticeable when the
Table of Contents is generated at the top of a file while the user is working
further down. By capturing the number of visual lines between the cursor and the
top of the window before the TOC generation and restoring that relative offset
afterward, we ensure the visual layout remains stable and prevent the text from
shifting unexpectedly under the user's eyes.

This commit prevents markdown-toc-generate-toc from disrupting the user's scroll
position by preserving the window-start relative to the cursor.
@jamescherti jamescherti force-pushed the save-restore-marker-window-start branch from 64fd324 to 4450c36 Compare March 12, 2026 13:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants