From acaa94af2f9424fa7bdd4be1900ce707a1f551a6 Mon Sep 17 00:00:00 2001 From: Kristoffer Carlsson Date: Thu, 9 Oct 2025 15:23:49 +0200 Subject: [PATCH] enable `ENABLE_VIRTUAL_TERMINAL_INPUT` on Windows to enable bracketed paste when supported --- stdlib/REPL/src/LineEdit.jl | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/stdlib/REPL/src/LineEdit.jl b/stdlib/REPL/src/LineEdit.jl index ba4a8155b13bf..342316d56b4ba 100644 --- a/stdlib/REPL/src/LineEdit.jl +++ b/stdlib/REPL/src/LineEdit.jl @@ -602,6 +602,10 @@ function refresh_multi_line(termbuf::TerminalBuffer, terminal::UnixTerminal, buf if writer isa Base.TTY && !Base.ispty(writer)::Bool _reset_console_mode(writer.handle) end + reader = Terminals.pipe_reader(terminal) + if reader isa Base.TTY && !Base.ispty(reader)::Bool + _enable_virtual_terminal_input(reader.handle) + end end # Write out the prompt string lindent = write_prompt(termbuf, prompt, hascolor(terminal))::Int @@ -1689,12 +1693,16 @@ end if Sys.iswindows() #= Get/SetConsoleMode flags =# +# Output flags const ENABLE_PROCESSED_OUTPUT = UInt32(0x0001) const ENABLE_WRAP_AT_EOL_OUTPUT = UInt32(0x0002) const ENABLE_VIRTUAL_TERMINAL_PROCESSING = UInt32(0x0004) const DISABLE_NEWLINE_AUTO_RETURN = UInt32(0x0008) const ENABLE_LVB_GRID_WORLDWIDE = UInt32(0x0010) +# Input flags +const ENABLE_VIRTUAL_TERMINAL_INPUT = UInt32(0x0200) + #= libuv flags =# const UV_TTY_SUPPORTED = 0 const UV_TTY_UNSUPPORTED = 1 @@ -1716,6 +1724,24 @@ function _reset_console_mode(handle::Ptr{Cvoid}) return nothing end +function _enable_virtual_terminal_input(handle::Ptr{Cvoid}) + # Enable virtual terminal input mode for bracketed paste support on Windows Terminal + # See https://github.com/JuliaLang/julia/issues/53763 + # Query libuv to see whether it expects the console to support virtual terminal sequences + vterm_state = Ref{Cint}() + ccall(:uv_tty_get_vterm_state, Cint, (Ref{Cint},), vterm_state) + + if vterm_state[] == UV_TTY_SUPPORTED + mode = Ref{UInt32}() + if ccall(:GetConsoleMode, stdcall, Int32, (Ptr{Cvoid}, Ref{UInt32}), handle, mode) != 0 + mode[] |= ENABLE_VIRTUAL_TERMINAL_INPUT + ccall(:SetConsoleMode, stdcall, Int32, (Ptr{Cvoid}, UInt32), handle, mode[]) + end + end + + return nothing +end + end # returns the width of the written prompt @@ -2938,6 +2964,13 @@ function prompt!(term::TextTerminal, prompt::ModalInterface, s::MIState = init_s status ∈ (:ok, :ignore) || break end raw!(term, true) + @static if Sys.iswindows() + # Enable virtual terminal input mode for bracketed paste support on Windows Terminal + reader = Terminals.pipe_reader(term) + if reader isa Base.TTY + _enable_virtual_terminal_input(reader.handle) + end + end enable_bracketed_paste(term) try activate(prompt, s, term, term)