Skip to content

Bracketed Paste Mode is not enabled on Windows #53763

Open
@mkitti

Description

@mkitti

Windows Terminal now supports "bracketed paste mode" when in VT (Virtual Terminal) Mode. This will allow Windows users to paste into the Julia REPL with text beginning with "julia>", which will then be removed from the input. This feature already exists for Linux and macOS users. (It probably works for the one FreeBSD user too).

Here is where the work was done to enable this in Windows:
https://devblogs.microsoft.com/commandline/windows-terminal-preview-1-18-release/
microsoft/terminal#15155

To enable virtual terminal input, use SetConsoleMode with ENABLE_VIRTUAL_TERMINAL_INPUT.

Here is a demonstrating, starting with some setup.

# From REPL.Terminals.enable_bracketed_paste
function enable_bracketed_paste()
    write(stdout, "\x1b[?2004h")
end
# Similar to REPL.LineEdit._console_mode()
function get_input_console_mode()
    hInput = ccall(:GetStdHandle, stdcall, Ptr{Cvoid}, (UInt32,), -10 % UInt32) # STD_INPUT_HANDLE
    dwMode = Ref{UInt32}(0)
    ccall(:GetConsoleMode, stdcall, Int32, (Ref{Cvoid}, Ref{UInt32}), hInput, dwMode)
    return dwMode[]
end
function enable_virtual_terminal_input()
    hInput = ccall(:GetStdHandle, stdcall, Ptr{Cvoid}, (UInt32,), -10 % UInt32)
    dwMode = get_input_console_mode()
    ENABLE_VIRTUAL_TERMINAL_INPUT = 0x200
    ccall(:SetConsoleMode, stdcall, Int32, (Ptr{Cvoid}, UInt32), hInput, dwMode | ENABLE_VIRTUAL_TERMINAL_INPUT)
end

Here's how it can be used.

julia> get_input_console_mode() # Virtual terminal input is not enabled by default on Windows
0x00000007

julia> 5+5 # copied the line before the comment
10

julia> readline() # pasted the line
julia> 5+5
"julia> 5+5"

julia> begin
           enable_bracketed_paste()
           enable_virtual_terminal_input()
           @info "" get_input_console_mode()
           readline() # pasted the same line
       end
┌ Info:get_input_console_mode() = 519
└ @ Main REPL[12]:4
^[[200~julia> 5+5^[[201~
"\e[200~julia> 5+5\e[201~"

julia> get_input_console_mode() # something keeps resetting the input console mode
0x00000007

Something seems to keep resetting the input console mode. The follow calls lead to SetConsoleMode.

  • uv_tty_set_mode calls SetConsoleMode.
  • [jl_uv_flush_close_callback] (
    uv_tty_set_mode((uv_tty_t*)stream, UV_TTY_MODE_NORMAL);
    ) calls uv_tty_set_mode
  • init_stdio_handle calls uv_tty_set_mode
  • jl_tty_set_mode calls uv_tty_set_mode

Metadata

Metadata

Assignees

No one assigned

    Labels

    REPLJulia's REPL (Read Eval Print Loop)system:windowsAffects only Windows

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions