Repository: microsoft/terminal (language: cpp)

Issue:
Enable alternate scroll mode by default
I was really pleased to see that proper support for the alt screen buffer has landed in the latest preview release (thanks to https://github.com/microsoft/terminal/pull/12569). Would you consider enabling this behavior by default? I realize that you could just add a `printf "\e[?1007h"` to your shell startup script, but most terminals that I use have this mode enabled by default. [This issue in the `foot` repository](https://codeberg.org/dnkl/foot/pulls/137) summarizes the issue nicely.


Discussion / hints:
I don't think I have much reluctance to enabling this by default. I'll refer to @j4james as our resident VT (and compatibility) expert. It's not like an app that didn't request that mode is going to get anything particularly bad by getting up/down keystrokes. IDK it seems pretty sensible.

We could easily make it an opt-out behavior as well, or opt in if there's more compat issues that I'm not aware of.
Every terminal I've tested that has support for alternate scroll also has it enabled by default, except for XTerm, and even XTerm has an option for that. So yeah, I'm definitely in favour of doing this. And now that the alt buffer doesn't show the scrollback, there's nothing else the scroll wheel can do anyway.

That said, if an application has a mouse tracking mode enabled (e.g. mode 1003) at the same time as alternate scroll, that mouse tracking should take precedence (or at least the most recent mode should take precedence). I don't think we're currently doing that, so it's important that we fix that first.
I'm not sure if I should open a new issue, but I seem to notice that alternate scroll is noticeably slower than normal scrolling (e.g. scrolling in Vim is slower than scrolling the scrollback in the shell). Is this known  / expected ?
That intuitively makes sense to me - if you're using alternate scroll mode, then I'd suspect we send one `^[[A` per wheel click, resulting in one line scrolled. In normal scrollback, we're usually scrolling about four lines per wheel click (depending on various settings)
Right. Windows defaults to scrolling 3 lines per wheel detent, but if we translated a single wheel scroll event into _three up/down events_ we would risk overshooting in cases where the wheel is being used to scroll things that aren't linear text buffers.

This will also manifest in applications that take over full mouse control rather than the partial control offered by alternate scroll mode: vim, for example, will scroll one line when it receives one "button 5" event (wheel).
@zadjii-msft This is pretty commonly configurable:

- Kitty: Configured by the [`wheel_scroll_multiplier` setting](https://sw.kovidgoyal.net/kitty/conf/#opt-kitty.wheel_scroll_multiplier).
- Alacritty: Configured by the `scrolling.multiplier` setting.
- VS Code Terminal: Configured by the `terminal.integrated.mouseWheelScrollSensitivity` setting.

@DHowett What I've usually seen is that alternate scroll mode is consistent with the scrollback behavior (usually more than 1 line per wheel click), and when an application takes over full mouse control, then it's usually 1 line per wheel click (probably because that's easiest to implement). I'm not sure how other terminals handle "overshooting".
Ah dang, you're totally right. We should make this configurable! Thanks!
By the way, how to add `printf "\e[?1007h"` to powershell startup?
If alternate buffer isn't enabled by default what is required to turn it on or make it turn on by default for all shell sessions?
It sounds like in bash you enable this with `printf "\e[?1007h"`. Is it possible to enable it in powershell?
In pwsh (7), I believe a ```write-host `e[?1007h``` will work. The syntax in earlier versions of powershell (`powershell.exe`) is... more annoying

Fix patch:
diff --git a/src/terminal/input/mouseInput.cpp b/src/terminal/input/mouseInput.cpp
index ddcb81b7e9b..d17482216ca 100644
--- a/src/terminal/input/mouseInput.cpp
+++ b/src/terminal/input/mouseInput.cpp
@@ -334,11 +334,6 @@ TerminalInput::OutputType TerminalInput::HandleMouse(const til::point position,
         _mouseInputState.accumulatedDelta = 0;
     }
 
-    if (ShouldSendAlternateScroll(button, delta))
-    {
-        return _makeAlternateScrollOutput(delta);
-    }
-
     if (IsTrackingMouseInput())
     {
         // isHover is only true for WM_MOUSEMOVE events
@@ -392,6 +387,11 @@ TerminalInput::OutputType TerminalInput::HandleMouse(const til::point position,
         }
     }
 
+    if (ShouldSendAlternateScroll(button, delta))
+    {
+        return _makeAlternateScrollOutput(delta);
+    }
+
     return {};
 }
 
diff --git a/src/terminal/input/terminalInput.cpp b/src/terminal/input/terminalInput.cpp
index 855ba74b28a..ae5e94ed22c 100644
--- a/src/terminal/input/terminalInput.cpp
+++ b/src/terminal/input/terminalInput.cpp
@@ -259,7 +259,7 @@ bool TerminalInput::GetInputMode(const Mode mode) const noexcept
 
 void TerminalInput::ResetInputModes() noexcept
 {
-    _inputMode = { Mode::Ansi, Mode::AutoRepeat };
+    _inputMode = { Mode::Ansi, Mode::AutoRepeat, Mode::AlternateScroll };
     _mouseInputState.lastPos = { -1, -1 };
     _mouseInputState.lastButton = 0;
 }
diff --git a/src/terminal/input/terminalInput.hpp b/src/terminal/input/terminalInput.hpp
index 1bb49cca7b2..17cf2a6fef3 100644
--- a/src/terminal/input/terminalInput.hpp
+++ b/src/terminal/input/terminalInput.hpp
@@ -70,7 +70,7 @@ namespace Microsoft::Console::VirtualTerminal
 
         std::optional<WORD> _lastVirtualKeyCode;
 
-        til::enumset<Mode> _inputMode{ Mode::Ansi, Mode::AutoRepeat };
+        til::enumset<Mode> _inputMode{ Mode::Ansi, Mode::AutoRepeat, Mode::AlternateScroll };
         bool _forceDisableWin32InputMode{ false };
 
         [[nodiscard]] OutputType _makeCharOutput(wchar_t ch);


Test patch: