Skip to content

Defer the repair of foreign deletions to after the command - #726

Merged
dakra merged 1 commit into
mainfrom
fix/deferred-deletion-repair
Oct 12, 2026
Merged

dakra merged 1 commit into
mainfrom
fix/deferred-deletion-repair

Conversation

@dakra

@dakra dakra commented Oct 10, 2026

Copy link
Copy Markdown
Owner

Fixes #724.

Problem

In live terminal-input modes (semi-char, char) the buffer is writable so that foreign insertions (emoji-insert, insert-char, yank from a package, …) can be caught in after-change-functions and forwarded to the PTY. A foreign deletion of rendered text was repaired inside that same hook by a synchronous full redraw plus a goto-char to the terminal cursor.

A command that deletes in a loop until a text condition holds never terminates under that scheme: every delete-char is undone before the loop re-checks. delete-indentation hangs Emacs, and so does anything shaped like it.

Change

The deletion branch of the hook no longer redraws. It sets a buffer-local flag, marks a redraw pending, and installs a one-shot buffer-local post-command-hook member. That member removes itself and runs one forced redraw after the command ends. ghostel--redraw-now renders a full frame while the flag is set and, after a successful render, clears it and realigns point to the terminal cursor.

  • Insertions made while the repair is pending are left in place and vanish with the full redraw instead of being forwarded. delete-indentation deletes a newline and then fixup-whitespace inserts a space; on main that space reached the shell as a keystroke.
  • A replacement (non-zero old length with new text) is a deletion for this purpose, as before.
  • A deletion made outside a command, from a timer or another package's process filter, is repaired on the buffer's next redisplay through the existing ghostel--pending-redraw path.
  • Reinitialising a reused buffer clears the flag.

Nothing is installed in the steady state: the renderer, the output filter and the text properties on rendered text are unchanged, so redraw time and scrollback memory are identical to main. The after-change hook tests one more buffer-local variable per foreign change, and the redraw path one more when choosing full versus incremental.

Visible behaviour

A looping deletion command finishes, then the terminal contents come back and point returns to the cursor. There is no error message. The mode stays semi-char, so the deferred point-leave check does not switch to copy mode.

Known gaps

  • A command that deletes a rendered prefix and then inserts a replacement in a live mode, an expansion or completion command run via M-x, now sends nothing to the PTY; on main the inserted text was forwarded after the repair.
  • A hidden buffer is repaired when it is next displayed, not immediately; insertions into it meanwhile are dropped with the repair.

Alternative that was rejected

#725 (kept as a draft for reference) made rendered text read-only with rear-nonsticky text properties so deletions signal text-read-only. Benchmarks there showed 5 to 8 percent slower redraws of heavily styled output and 43 percent more live memory on styled scrollback, because Emacs copies the property list into every face-run interval. A single category property halved the memory but was slower still. This PR has no rendering cost.

Tests

test/ghostel-insert-forward-test.el: the repair tests assert the deferral and the one-shot hook; new tests cover dropped insertions while pending, delete-indentation finishing, repair on redisplay without a command, and the native path restoring text and realigning point. make -j8 all passes.

Verified live under elate with bash: M-x delete-indentation as a key-driven command returns with the buffer restored, point on the cursor and the mode unchanged; an explicit deletion loop to the buffer start finishes; programmatic kill-region fills the kill ring and the text comes back; echo hi inserted into the buffer still reaches the shell.

Looping deletion commands such as delete-indentation hung Emacs in live
terminal-input modes: the after-change hook repaired every deletion with
a synchronous full redraw, so the command never ran out of text to
delete.

A deletion now only flags the buffer and installs a one-shot
post-command-hook member.  That member runs one forced redraw after the
command ends; the redraw is full while the flag is set and realigns
point to the terminal cursor.  Insertions made while the repair is
pending are dropped with it instead of being forwarded to the PTY.
A deletion made outside a command is repaired on the next redisplay.

Fix #724
@dakra
dakra force-pushed the fix/deferred-deletion-repair branch from 6f5036a to df8a540 Compare October 12, 2026 11:39
@dakra
dakra merged commit df8a540 into main Oct 12, 2026
27 checks passed
@dakra
dakra deleted the fix/deferred-deletion-repair branch October 12, 2026 11:42

This branch was previously deployed

1 inactive deployment
github-pages — df8a5407 Deployed Oct 12, 2026 by dakra via deploy #217
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.

Deletion repair in after-change hangs Emacs on looping commands (e.g. delete-indentation)

1 participant