Repository navigation
libghostty proposal: OSC dispatch effect for the embedder API #12477
Unanswered
dakra
asked this question in
Feature Requests, Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Hi,
I'm writing ghostel, a terminal in Emacs using the libghostty C-API.
I have a few feature requests for the libghostty API and this one would bring the most benefit.
I went over this proposal a few times with the AI, so the following text is reviewed by me, but written by AI.
Hope this is ok. I was not sure how lengthy it should be and/or how much implementation details it should contain.
Please say if I should keep future feature requests shorter or do something else.
Anyway, thanks a lot for ghostty and here's the feature request:
Proposal: OSC dispatch effect for the embedder API
Summary
Extend the effects API merged in #11814
with a single new option,
GHOSTTY_TERMINAL_OPT_OSC_DISPATCH, that fires theexisting
GhosttyOscCommandvalue back to the embedder once per successfullyparsed OSC during
ghostty_terminal_vt_write. No new payload type — embedders usethe
ghostty_osc_command_type/ghostty_osc_command_dataaccessors alreadydefined in
include/ghostty/vt/osc.h. This subsumes the open vouch for OSC 133in discussion #11603, the closed vouch in issue #12086,
and the per-sequence-callback direction sketched in discussion #11348
(which mitchellh acknowledged but where only the fixed-effect set landed).
Current state
The effects table in
include/ghostty/vt/terminal.hcovers a hard-coded list ofevents:
WRITE_PTY,BELL,TITLE_CHANGED,ENQUIRY,XTVERSION,SIZE,COLOR_SCHEME,DEVICE_ATTRIBUTES. Per the file's own header comment:The OSC parser already produces a rich command type —
GhosttyOscCommandTypein
include/ghostty/vt/osc.henumerates 22 commands includingSEMANTIC_PROMPT,CLIPBOARD_CONTENTS,REPORT_PWD,COLOR_OPERATION,HYPERLINK_START,HYPERLINK_END,SHOW_DESKTOP_NOTIFICATION,CONEMU_PROGRESS_REPORT,KITTY_COLOR_PROTOCOL,MOUSE_SHAPE, etc. Theinternal terminal already builds these commands while parsing
vt_writeinput. Everything except the few in the effects table is then either applied to
internal state or discarded; the embedder never sees it.
Three prior threads asked for slices of this:
ghostty_terminal_callback_writer_new, a per-sequence callback writer.mitchellh: "This will help me with the roadmap." The fixed effects landed; the
per-sequence variant did not.
"Semantic OSC exposed to C API" vouch, focused on OSC 133. Stalled.
for an OSC 133 zone-text accessor; adjacent.
Proposal
Add one option:
with the matching function-pointer typedef mirroring the existing effect
typedefs in
terminal.h:The exact slot number is illustrative — actual placement is the maintainer's call.
Ordering: pre-state-mutation, not post
The callback must fire before the terminal applies the OSC's own state
update. Concretely: for
OSC 4 ; <i> ; ?(palette query), the embedder may wantto write a color reply via the already-installed
WRITE_PTYcallback. If thesame
vt_writecall also processed aCSI 6ncursor-position query, theterminal will produce that DSR via
WRITE_PTYlater in the same call. Programsthat use termenv-style probing (Charm's
lipgloss, anything that does an"ask-and-read-once") read only the first response chunk and treat the rest as
input. If the color reply doesn't go out first, theme detection breaks.
ghostel hits this today and has documented the workaround at
src/module.zig:149-155:If the dispatch fires post-state-mutation, ghostel (and any embedder with the
same need) still has to maintain the pre-
vt_writepre-scan. Pre-state-mutationordering retires both passes.
Why the GhosttyOscCommand handle, not a flat struct
GhosttyOscCommandis already opaque, already hascommand_type/command_dataaccessors, already handles strings via the parser-owned-bufferpattern, and already has a
GHOSTTY_OSC_COMMAND_INVALIDdiscriminant. Inventinga parallel
GhosttyOscEventstruct duplicates all of that and means every newOSC the parser learns about needs a second wire shape. Reusing the existing
handle keeps the surface area to one option + one typedef.
What this lets embedders retire
Every embedder that needs more than the eight built-in effects today re-runs
its own OSC parser over the PTY stream. With
OSC_DISPATCH:SEMANTIC_PROMPTevents directly.CLIPBOARD_CONTENTSevents with no base64 re-parsing in the embedder.HYPERLINK_START/HYPERLINK_END.COLOR_OPERATIONevents; the embedder writes the reply via the existingWRITE_PTYcallback, with correct ordering guaranteed by the pre-state-mutation firing point above.REPORT_PWDevents.A concrete worked example follows.
Use case
ghostelis an Emacs frontend embeddinglibghostty-vt. It needs every category of OSC the effects API doesn't already
cover: OSC 7 cwd, OSC 51 (a ghostel-specific whitelisted Elisp-eval extension),
OSC 52 clipboard, OSC 133 semantic prompt marks, OSC 9 + 9;4 + 9;9 (iTerm2
notifications, ConEmu progress, ConEmu cwd), OSC 777 rxvt notify, and OSC
4/10/11 color queries.
To get them, ghostel runs a hand-rolled second OSC parser over every PTY
chunk libghostty has already parsed:
vt_write:extractOscColorQueriesatsrc/module.zig:529-627,~100 lines for OSC 4/10/11 query replies. Placement is pre-write specifically
to satisfy the ordering constraint described above.
vt_write:dispatchPostWriteOscs+ theOscIteratorhelper + thedispatchOsc9sub-code disambiguator atsrc/module.zig:204-373,~170 lines.
The iterator's own doc comment lists two known bugs: OSC sequences split
across two
process-filterdeliveries (introducer in chunk A, terminator inchunk B) are silently dropped, and 8-bit C1 forms (
\x9dfor OSC,\x9cfor ST) aren't matched at all. libghostty's stateful parser handles both
cases correctly — that's exactly the win from delegating. The OSC 9 sub-code
disambiguator (ConEmu progress vs iTerm2 notification vs ConEmu cwd) also
duplicates work libghostty already does:
GHOSTTY_OSC_COMMAND_CONEMU_PROGRESS_REPORTandGHOSTTY_OSC_COMMAND_SHOW_DESKTOP_NOTIFICATIONare already distinct commandtypes in
include/ghostty/vt/osc.h.With
OSC_DISPATCHin place, the entire second-parser layer retires:command_typelookup.is parsed twice today (once by libghostty, once by ghostel).
The same shape applies to any embedder that needs OSC 7 cwd tracking, OSC 52
clipboard, OSC 133 prompt zones, or color-query replies — roughly every
shell-integration-aware terminal frontend.
Acceptance criteria (what "done" looks like to me)
GHOSTTY_TERMINAL_OPT_OSC_DISPATCHenumerator and matchingGhosttyTerminalOscDispatchFntypedef ininclude/ghostty/vt/terminal.h,documented in the same style as the existing effects.
mutation, so embedder-emitted replies via
WRITE_PTYprecedeterminal-emitted replies in the same
vt_writecall.GhosttyOscCommandhandle is borrowed for the duration of the call;embedder must extract data before returning. Document this lifetime in the
callback's doc comment, matching the borrowed-string convention used elsewhere.
vt_writefrominside the callback).
example/mirroringexample/c-vt-effects/showingan OSC 7 (cwd) and OSC 133 (prompt) handler, and one color-query reply
routed through
WRITE_PTYto demonstrate the ordering guarantee.Happy to test against ghostel's branch and report back as soon as a draft lands.
All reactions