Skip to content

Bracketed Paste Mode is not enabled on Windows #53763

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
mkitti opened this issue Mar 17, 2024 · 5 comments
Open

Bracketed Paste Mode is not enabled on Windows #53763

mkitti opened this issue Mar 17, 2024 · 5 comments
Labels
REPL Julia's REPL (Read Eval Print Loop) system:windows Affects only Windows

Comments

@mkitti
Copy link
Contributor

mkitti commented Mar 17, 2024

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
@Keno
Copy link
Member

Keno commented Apr 8, 2025

@topolarity should this have been fixed by #57132?

@mkitti

This comment has been minimized.

@Keno

This comment has been minimized.

@mkitti
Copy link
Contributor Author

mkitti commented Apr 8, 2025

I can test nightly later

@digital-carver
Copy link
Contributor

I tested the nightly from a few weeks ago (well after #57132's merging) on Windows Terminal(Version: 1.22.11141.0), and unfortunately it hasn't been fixed. That PR adds ENABLE_VIRTUAL_TERMINAL_PROCESSING which is for controlling the output processing, whereas this one needs ENABLE_VIRTUAL_TERMINAL_INPUT as mentioned above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
REPL Julia's REPL (Read Eval Print Loop) system:windows Affects only Windows
Projects
None yet
Development

No branches or pull requests

4 participants