1+ import type { TerminalOptionAsAlt } from "@t3tools/contracts" ;
12import { isMacPlatform } from "../../lib/utils" ;
23import { SELECTION_MULTI_CLICK_INTERVAL_MS } from "../../lib/selectionActions" ;
34import { collectWrappedTerminalLinkLine , extractTerminalLinks } from "../../terminal-links" ;
@@ -427,6 +428,44 @@ export function isTerminalAltGraphText(
427428 return event . getModifierState ( "AltGraph" ) && [ ...event . key ] . length === 1 ;
428429}
429430
431+ /** Which Option keys are down, tracked from their own keydown and keyup. */
432+ export interface TerminalHeldOptionKeys {
433+ readonly left : boolean ;
434+ readonly right : boolean ;
435+ }
436+
437+ /**
438+ * A macOS Option press that should type the character the layout composed
439+ * (German Option+L is `@`) through the textarea, as Terminal.app does with
440+ * "Use Option as Meta key" off. Browsers report Option as Alt, never AltGraph,
441+ * so encoding it sends an Alt sequence and the character is lost. Option keys
442+ * set to act as Alt keep that encoding. An Option press that started before
443+ * the terminal had focus has no tracked side and types the character.
444+ * Dead keys (German Option+N for `~`) go to the textarea to compose. Option+Space
445+ * stays encoded, since a non-breaking space in a command line breaks it.
446+ * Option+arrow is not a single character, so word motion is unaffected.
447+ */
448+ export function isTerminalOptionComposedText (
449+ event : Pick < KeyboardEvent , "altKey" | "ctrlKey" | "key" | "metaKey" > ,
450+ optionAsAlt : TerminalOptionAsAlt ,
451+ heldOption : TerminalHeldOptionKeys ,
452+ platform : string ,
453+ ) : boolean {
454+ if ( ! isMacPlatform ( platform ) || ! event . altKey || event . ctrlKey || event . metaKey ) return false ;
455+ if ( event . key === "\u00a0" ) return false ;
456+ if ( event . key !== "Dead" && [ ...event . key ] . length !== 1 ) return false ;
457+ switch ( optionAsAlt ) {
458+ case "off" :
459+ return true ;
460+ case "both" :
461+ return false ;
462+ case "left" :
463+ return ! heldOption . left ;
464+ case "right" :
465+ return ! heldOption . right ;
466+ }
467+ }
468+
430469export function shouldReportTerminalMouse (
431470 tracking : boolean ,
432471 event : Pick < MouseEvent , "ctrlKey" | "metaKey" | "shiftKey" > ,
@@ -547,6 +586,8 @@ export interface GhosttyTerminalSurfaceOptions {
547586 readonly onResize : ( cols : number , rows : number ) => void ;
548587 readonly onSelectionChange : ( ) => void ;
549588 readonly beforeKey : ( event : KeyboardEvent ) => boolean ;
589+ /** Read on every key press. Hosts can supply a getter for the latest value. */
590+ readonly optionAsAlt ?: TerminalOptionAsAlt ;
550591 readonly onLinkActivate : ( text : string , event : MouseEvent ) => void ;
551592 readonly canActivateLink ?: ( text : string ) => boolean ;
552593 /**
@@ -622,6 +663,7 @@ export class GhosttyTerminalSurface {
622663 private selectionMoved = false ;
623664 private composing = false ;
624665 private focused = false ;
666+ private heldOption : TerminalHeldOptionKeys = { left : false , right : false } ;
625667 private resizeNotified = false ;
626668 private canvasConfigured = false ;
627669 private theme : GhosttyTheme ;
@@ -1114,6 +1156,7 @@ export class GhosttyTerminalSurface {
11141156 }
11151157
11161158 private readonly onKeyDown = ( event : KeyboardEvent ) => {
1159+ this . trackHeldOption ( event , true ) ;
11171160 // Presses handled outside the terminal must also swallow their release:
11181161 // beforeKey runs side effects (keybindings, navigation sends), so it cannot
11191162 // be consulted again on keyup, and Kitty report-event-types sessions would
@@ -1128,6 +1171,17 @@ export class GhosttyTerminalSurface {
11281171 this . suppressedKeyCodes . add ( event . code ) ;
11291172 return ;
11301173 }
1174+ if (
1175+ isTerminalOptionComposedText (
1176+ event ,
1177+ this . options . optionAsAlt ?? "off" ,
1178+ this . heldOption ,
1179+ navigator . platform ,
1180+ )
1181+ ) {
1182+ this . suppressedKeyCodes . add ( event . code ) ;
1183+ return ;
1184+ }
11311185 const mac = isMacPlatform ( navigator . platform ) ;
11321186 const primaryModifier = mac ? event . metaKey && ! event . ctrlKey : event . ctrlKey && ! event . metaKey ;
11331187 const selectAllShortcut =
@@ -1259,6 +1313,7 @@ export class GhosttyTerminalSurface {
12591313 } ;
12601314
12611315 private readonly onKeyUp = ( event : KeyboardEvent ) => {
1316+ this . trackHeldOption ( event , false ) ;
12621317 if ( this . suppressedKeyCodes . delete ( event . code ) ) return ;
12631318 if ( isTerminalCompositionKey ( event , this . composing ) ) {
12641319 return ;
@@ -1272,6 +1327,19 @@ export class GhosttyTerminalSurface {
12721327 this . options . onData ( data ) ;
12731328 } ;
12741329
1330+ // Any event without Alt means no Option is down, which also clears a side
1331+ // whose release happened while focus was elsewhere.
1332+ private trackHeldOption ( event : KeyboardEvent , down : boolean ) : void {
1333+ if ( ! event . altKey && event . code !== "AltLeft" && event . code !== "AltRight" ) {
1334+ if ( this . heldOption . left || this . heldOption . right ) {
1335+ this . heldOption = { left : false , right : false } ;
1336+ }
1337+ return ;
1338+ }
1339+ if ( event . code === "AltLeft" ) this . heldOption = { ...this . heldOption , left : down } ;
1340+ if ( event . code === "AltRight" ) this . heldOption = { ...this . heldOption , right : down } ;
1341+ }
1342+
12751343 private readonly onFocus = ( ) => {
12761344 this . focused = true ;
12771345 this . cursorOn = true ;
@@ -1280,6 +1348,7 @@ export class GhosttyTerminalSurface {
12801348
12811349 private readonly onBlur = ( ) => {
12821350 this . focused = false ;
1351+ this . heldOption = { left : false , right : false } ;
12831352 this . refreshHoveredLink ( ) ;
12841353 // Suppressions survive blur deliberately: a shortcut that moves focus (for
12851354 // example terminal-toggle) must still swallow its own keyup if focus comes
0 commit comments