From bcc6b7604d87129e03ee92905e40752c0cfc7c61 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 26 Jun 2022 16:39:48 -0700 Subject: [PATCH] fix paste outputting "v" --- TODO.md | 3 --- src/Window.zig | 19 +++++++++++++++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/TODO.md b/TODO.md index 081b441b5..d6a397497 100644 --- a/TODO.md +++ b/TODO.md @@ -1,12 +1,9 @@ Bugs: -* Paste puts a "v" at the end of the string - Performance: * libuv allocates on every read, we should use a read buffer pool * update cells should only update the changed cells -* terminal data structure is bad! Correctness: diff --git a/src/Window.zig b/src/Window.zig index da40db340..f2966bcf3 100644 --- a/src/Window.zig +++ b/src/Window.zig @@ -93,6 +93,12 @@ bg_a: f32, /// Bracketed paste mode bracketed_paste: bool = false, +/// Set to true for a single GLFW key/char callback cycle to cause the +/// char callback to ignore. GLFW seems to always do key followed by char +/// callbacks so we abuse that here. This is to solve an issue where commands +/// like such as "control-v" will write a "v" even if they're intercepted. +ignore_char: bool = false, + /// Create a new window. This allocates and returns a pointer because we /// need a stable pointer for user data callbacks. Therefore, a stack-only /// initialization is not currently possible. @@ -368,6 +374,12 @@ fn charCallback(window: glfw.Window, codepoint: u21) void { const win = window.getUserPointer(Window) orelse return; + // Ignore if requested. See field docs for more information. + if (win.ignore_char) { + win.ignore_char = false; + return; + } + // Write the character to the pty win.queueWrite(&[1]u8{@intCast(u8, codepoint)}) catch unreachable; } @@ -386,14 +398,17 @@ fn keyCallback( // Paste if (action == .press and mods.super and key == .v) { + const win = window.getUserPointer(Window) orelse return; + + // Ignore this character for writing + win.ignore_char = true; + const data = glfw.getClipboardString() catch |err| { log.warn("error reading clipboard: {}", .{err}); return; }; if (data.len > 0) { - const win = window.getUserPointer(Window) orelse return; - if (win.bracketed_paste) win.queueWrite("\x1B[200~") catch unreachable; win.queueWrite(data) catch |err| log.warn("error pasting clipboard: {}", .{err});