xterm: implement RIS (ESC c) and DECSTR (CSI ! p)

ESC c was ignored, so `reset` could not recover a terminal left in the
alternate screen with a hidden cursor, a scroll region or mouse modes.
RIS now resets every mode, the attributes, character sets, tab stops,
margins and saved cursors, returns to the main screen and clears it
(scrollback is kept, as in xterm). DECSTR resets modes, attributes and
margins without touching the text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 23:27:50 +00:00
parent 30b32921fe
commit 31d00e057c
7 changed files with 174 additions and 1 deletions
+13
View File
@@ -338,6 +338,19 @@ class Buffer {
charset.restore();
}
/// Resets the cursor, the saved cursor, the character sets and the
/// margins to their power-on state (for RIS and DECSTR). The contents are
/// left alone.
void resetState() {
_cursorX = 0;
_cursorY = 0;
_savedCursorX = 0;
_savedCursorY = 0;
_savedCursorStyle.reset();
charset.reset();
resetVerticalMargins();
}
/// Sets the vertical scrolling margin to [top] and [bottom].
/// Both values must be between 0 and [viewHeight] - 1.
void setVerticalMargins(int top, int bottom) {
+9
View File
@@ -40,6 +40,15 @@ class Charset {
_savedIndex = _currentIndex;
}
/// Back to G0 selected and no designations (ASCII), as after power-on.
void reset() {
_charsetMap = {};
_currentIndex = 0;
_savedCharsetMap = {};
_savedIndex = 0;
_updateCache();
}
void restore() {
_charsetMap = _savedCharsetMap;
_currentIndex = _savedIndex;
@@ -39,6 +39,13 @@ abstract class EscapeHandler {
void unkownEscape(int char);
/// `ESC c` (RIS): everything back to the power-on state, screen cleared.
void resetTerminal();
/// `CSI ! p` (DECSTR): modes, attributes and margins reset; the screen
/// contents stay.
void softResetTerminal();
/* DCS */
/// `DCS $ q Pt ST` (DECRQSS): report the setting named by [setting], such
+21 -1
View File
@@ -123,6 +123,7 @@ class EscapeParser {
// '+'.charCode: _voidHandler(1), // TODO: G3 (vt220)
'>'.charCode: _escHandleResetAppKeypadMode, // TODO: Normal Keypad
'='.charCode: _escHandleSetAppKeypadMode, // TODO: Application Keypad
'c'.charCode: _escHandleReset,
'P'.charCode: () => _beginControlString(Ascii.P), // DCS
'X'.charCode: () => _beginControlString(Ascii.X), // SOS
'^'.charCode: () => _beginControlString(Ascii.caret), // PM
@@ -194,6 +195,14 @@ class EscapeParser {
}
}
/// `ESC c` Reset to Initial State (RIS)
///
/// https://terminalguide.namepad.de/seq/a_esc_sc/
bool _escHandleReset() {
handler.resetTerminal();
return true;
}
/// `ESC 7` Save Cursor (DECSC)
///
/// https://terminalguide.namepad.de/seq/a_esc_a7/
@@ -315,7 +324,18 @@ class EscapeParser {
/// Dispatches a CSI that carries intermediate bytes.
void _csiHandleWithIntermediates() {
handler.unknownCSI(_csi.finalByte);
final finalByte = _csi.finalByte;
final intermediates = _csi.intermediates;
// `CSI ! p` Soft Terminal Reset (DECSTR)
if (intermediates == Ascii.exclamationMark &&
finalByte == Ascii.p &&
_csi.prefix == null) {
handler.softResetTerminal();
return;
}
handler.unknownCSI(finalByte);
}
/// The last parsed [_Csi]. This is a mutable singletion by design to reduce
+39
View File
@@ -482,6 +482,45 @@ class Terminal with Observable implements TerminalState, EscapeHandler {
// no-op
}
@override
void resetTerminal() {
_resetModes();
_mouseMode = MouseMode.none;
_mouseReportMode = MouseReportMode.normal;
_cursorBlinkMode = false;
_lineFeedMode = false;
_reverseDisplayMode = false;
_reportFocusMode = false;
_altBufferMouseScrollMode = false;
_bracketedPasteMode = false;
_precedingCodepoint = 0;
_tabStops.reset();
_buffer = _mainBuffer;
_altBuffer.clear();
_altBuffer.resetState();
_mainBuffer.resetState();
// The screen is cleared; scrollback above it is kept, as in xterm.
_mainBuffer.eraseDisplay();
}
@override
void softResetTerminal() {
_resetModes();
_buffer.resetState();
}
/// The state shared by RIS and DECSTR.
void _resetModes() {
_cursorStyle.reset();
_insertMode = false;
_originMode = false;
_autoWrapMode = true;
_cursorKeysMode = false;
_appKeypadMode = false;
_cursorVisibleMode = true;
}
/* DCS */
@override
+10
View File
@@ -471,6 +471,16 @@ class _TerminalDebuggerHandler implements EscapeHandler {
onCommand('setCursorUnderline');
}
@override
void resetTerminal() {
onCommand('resetTerminal');
}
@override
void softResetTerminal() {
onCommand('softResetTerminal');
}
@override
void cursorForwardTab(int amount) {
onCommand('cursorForwardTab $amount');