Skip to content

Commit e209445

Browse files
committed
Make bracketed paste conditional on terminal mode 2004
Expose ghostel--mode-enabled to query DEC private modes from Elisp. Use it to only wrap paste text with bracketed paste sequences when the terminal has mode 2004 enabled, instead of always sending them unconditionally. Also consolidate text drop handling through ghostel--paste-text.
1 parent b0d143c commit e209445

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

ghostel.el

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -541,11 +541,18 @@ Returns the sequence string, or nil for unknown keys."
541541
(defvar-local ghostel--yank-index 0
542542
"Current kill ring index for `ghostel-yank-pop'.")
543543

544+
(defun ghostel--bracketed-paste-p ()
545+
"Return non-nil if the terminal has bracketed paste mode (2004) enabled."
546+
(and ghostel--terminal
547+
(ghostel--mode-enabled ghostel--terminal 2004)))
548+
544549
(defun ghostel--paste-text (text)
545-
"Send TEXT to the terminal using bracketed paste."
550+
"Send TEXT to the terminal, using bracketed paste if the terminal wants it."
546551
(when (and text ghostel--process (process-live-p ghostel--process))
547552
(process-send-string ghostel--process
548-
(concat "\e[200~" text "\e[201~"))))
553+
(if (ghostel--bracketed-paste-p)
554+
(concat "\e[200~" text "\e[201~")
555+
text))))
549556

550557
(defun ghostel-paste ()
551558
"Paste text from the Emacs kill ring into the terminal.
@@ -599,8 +606,7 @@ pasted using bracketed paste."
599606
(ghostel--send-key (shell-quote-argument path))))
600607
;; Text drop
601608
((stringp payload)
602-
(process-send-string ghostel--process
603-
(concat "\e[200~" payload "\e[201~")))
609+
(ghostel--paste-text payload))
604610
;; dnd-protocol-alist style: list of files
605611
((and (listp payload) (cl-every #'stringp payload))
606612
(ghostel--send-key

src/module.zig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export fn emacs_module_init(runtime: *c.struct_emacs_runtime) callconv(.c) c_int
3737
env.bindFunction("ghostel--mouse-event", 6, 6, &fnMouseEvent, "Send a mouse event to the terminal.\n\n(ghostel--mouse-event TERM ACTION BUTTON ROW COL MODS)");
3838
env.bindFunction("ghostel--focus-event", 2, 2, &fnFocusEvent, "Send a focus event to the terminal.\n\n(ghostel--focus-event TERM GAINED)");
3939
env.bindFunction("ghostel--set-palette", 2, 2, &fnSetPalette, "Set the ANSI color palette.\n\n(ghostel--set-palette TERM COLORS-STRING)");
40+
env.bindFunction("ghostel--mode-enabled", 2, 2, &fnModeEnabled, "Return t if terminal DEC private MODE is enabled.\n\n(ghostel--mode-enabled TERM MODE)");
4041
env.bindFunction("ghostel--debug-state", 1, 1, &fnDebugState, "Return debug info about terminal/render state.\n\n(ghostel--debug-state TERM)");
4142
env.bindFunction("ghostel--debug-feed", 2, 2, &fnDebugFeed, "Feed STR to terminal and return first row + cursor.\n\n(ghostel--debug-feed TERM STR)");
4243

@@ -363,6 +364,15 @@ fn fnFocusEvent(raw_env: ?*c.emacs_env, _: isize, args: [*c]c.emacs_value, _: ?*
363364
return env.t();
364365
}
365366

367+
/// (ghostel--mode-enabled TERM MODE)
368+
/// Return t if terminal DEC private MODE is enabled, nil otherwise.
369+
fn fnModeEnabled(raw_env: ?*c.emacs_env, _: isize, args: [*c]c.emacs_value, _: ?*anyopaque) callconv(.c) c.emacs_value {
370+
const env = emacs.Env.init(raw_env.?);
371+
const term = env.getUserPtr(Terminal, args[0]) orelse return env.nil();
372+
const mode: gt.c.GhosttyMode = @intCast(env.extractInteger(args[1]));
373+
return if (term.isModeEnabled(mode)) env.t() else env.nil();
374+
}
375+
366376
/// (ghostel--set-palette TERM COLORS-STRING)
367377
/// Set the 16 ANSI colors from a concatenated hex string like "#000000#aa0000...".
368378
/// The remaining 240 palette entries are taken from the terminal's current palette.

0 commit comments

Comments
 (0)