Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,47 @@ reads that marker.
`xterm-256color`, and a recording with no frame takes the terminal's
size at `record` rather than 80x24. A test that bounded `timestamp` by
the export should bound it by the call to `record`. (#850)
- `ICH` (`CSI @`) with the cursor on the second half of a wide character
erases the character before it inserts, as xterm's `InsertChar` does:
`漢x`, `CSI 2 G`, `CSI @` shows three blank columns and then `x`.
termlens kept the character whole and inserted the blank after it,
`漢 x`. A test that expected the character after such an insert should
expect blanks in its place. (#822)
- The scroll region and origin mode move the cursor as xterm's do.
`DECSTBM` homes the cursor to row 0 outside origin mode, where it went
to the top margin, and a region whose bottom is not below its top is
ignored, where it removed the region. `CUD`, `CNL`, `CUU` and `CPL`
from outside the region stop at its near margin, where they went on to
the screen's edge. `RI` above the region moves the cursor up and
scrolls nothing, where at row 0 it scrolled the region down. And `VPA`
under origin mode counts from the top margin and stops at the bottom
one, where it counted from row 0. A test that expected the old
positions should expect xterm's: a program that writes a header with
`CSI 3;5 r` then text now draws it at the top-left. (#821)
- A switch of screens keeps the cursor, the scroll region and origin
mode, as xterm's does, which keeps one of each for both screens.
`CSI ? 1049 h` and `CSI ? 47 h` left the cursor at the top-left of the
alternate screen with no region and origin mode off, and `CSI ? 47 l`
put it back where the main screen last had it; `CSI ? 1049 l` still
restores the cursor `CSI ? 1049 h` saved. Every child enters through
`smcup`, which is `CSI ? 1049 h`, so a test that asserted the cursor at
0,0 right after start-up should expect it where the program left it on
the main screen, or have the program position it first. (#820)
- At a pending wrap, the motions and edits xterm starts from the cursor's
column start from the last column and cancel the wrap, as xterm's do.
After a character in the last column, `CUU`, `CUD` and `VPA` leave the
cursor on the last column of the row they reach, `CUB` and `BS` count
left from it, `ICH` and `DCH` act on it, and a line feed (`LF`, `VT`,
`FF`, `IND`) and `RI` move from it, so on five columns `abcde`
`CSI D` `X` is `abcXe` and `abcde` LF `X` puts the `X` under the `e`.
`HT` and `CHT` there keep the wrap, and so does a resize to a screen no
wider, so the next character starts the next row. termlens kept the
cursor past the margin: the motions left the wrap pending, `CUB` and
`BS` landed a column right of xterm's, `ICH` and `DCH` changed nothing,
and the tab motions and a resize cancelled the wrap. A test that
expected the next character on the following row after one of the
motions or edits, or over the last column after a tab or a resize,
should expect xterm's place. (#814)

### Fixed

Expand Down
159 changes: 143 additions & 16 deletions crates/termlens/src/emu/seq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,68 @@ pub(crate) enum TabOp {
Back(u16),
}

/// A cursor motion or a character edit the emulator carries out in place
/// of vt100, which starts it from somewhere xterm does not. Whether it
/// differs depends on the cursor, which lives in the grid, so the tracker
/// names the operation and its count and the emulator resolves it.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Motion {
/// `ICH` (`CSI Ps @`): insert `n` blank cells at the cursor. xterm
/// erases a wide character the cursor splits first (#822).
InsertChars(u16),
/// `CUU` (`CSI Ps A`): up `n` rows. xterm stops at the top margin
/// unless the cursor starts above it (`CursorUp`, cursor.c 271);
/// vt100 stops there only from inside the region (#821).
Up(u16),
/// `CUD` (`CSI Ps B`): down `n` rows, stopping at the bottom margin
/// unless the cursor starts below it (`CursorDown`, cursor.c 251).
Down(u16),
/// `CNL` (`CSI Ps E`): [`Down`](Self::Down) and a carriage return
/// (`CursorNextLine`, cursor.c 527).
NextLine(u16),
/// `CPL` (`CSI Ps F`): [`Up`](Self::Up) and a carriage return.
PrevLine(u16),
/// `VPA` (`CSI Ps d`): to row `n`, counted from one. xterm counts it
/// from the top margin under origin mode and clamps it to the bottom
/// one (`CASE_VPA` through `CursorSet`, charproc.c 3952); vt100 counts
/// from the top of the screen whatever the mode (#821).
Row(u16),
/// `RI` (`ESC M`). xterm scrolls only at the top margin, and above
/// the region it is `CursorUp` (`RevIndex`, cursor.c 319); vt100
/// scrolled the region from row 0 above it (#821).
ReverseIndex,
/// `DECSTBM` (`CSI Pt ; Pb r`). xterm sets a region only when its
/// bottom is below its top, ignores the sequence otherwise, and homes
/// the cursor to row 0 outside origin mode (`CASE_DECSTBM`, charproc.c
/// 4773). vt100 took an invalid region for none and homed to the top
/// margin (#821). `valid` is xterm's test, which the tracker has
/// already applied to the margins it holds.
Margins { valid: bool },
/// `DCH` (`CSI Ps P`): delete `n` cells at the cursor, which xterm
/// starts from the last column at a pending wrap (`DeleteChar`, util.c
/// 1987; #814).
DeleteChars(u16),
/// `CUB` (`CSI Ps D`): left `n` columns, which xterm counts from the
/// last column at a pending wrap (`CursorBack`, cursor.c; #814).
Back(u16),
/// `LF`, `VT` or `FF` in ground: xterm's `CASE_VMOT`, `xtermIndex`,
/// which resets a pending wrap through `CursorDown` (charproc.c 3738).
/// vt100 kept the wrap pending on the next row (#814).
LineFeed,
/// `BS` in ground: `CursorBack(1)` (charproc.c 3707), from the last
/// column at a pending wrap (#814).
Backspace,
}

impl Motion {
/// Whether this is a C0 control, which the emulator acts on only when
/// a wrap may be pending: the common line feed after a carriage
/// return then costs nothing.
pub(crate) fn is_control(self) -> bool {
matches!(self, Self::LineFeed | Self::Backspace)
}
}

/// Which glyph set a G0–G3 designation currently names.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Charset {
Expand Down Expand Up @@ -329,10 +391,13 @@ struct SavedCharsets {
/// The margins and origin mode are kept for the cursor position report,
/// which counts rows from the top margin under origin mode (#689), and for
/// `IL` and `DL`, which move the cursor to the left margin only inside the
/// region (#686). They follow vt100's rules rather than xterm's wherever
/// the two differ, since a report is read back as a `CUP` that vt100
/// carries out, and vt100 decides whether a line moves: a region per grid,
/// `1049` clearing the alternate grid's, a resize clearing both.
/// region (#686). They must be the ones vt100 holds, since a report is
/// read back as a `CUP` that vt100 carries out, and vt100 decides whether
/// a line moves. Where vt100's rules differ from xterm's, the emulator
/// holds vt100 to xterm's, and these follow them: `DECSTBM` ignores a
/// region whose bottom is not below its top (#821), a switch of screens
/// carries the margins and origin mode across, as xterm keeps one set of
/// each (#820), and a resize clears both screens'.
/// The charset slot is xterm's `screen->sc[screen->whichBuf]`, one per
/// screen, so a restore on the main screen never reads what the alternate
/// screen saved (#685).
Expand Down Expand Up @@ -509,6 +574,11 @@ pub(crate) enum SeqEvent {
/// The emulator takes the escape from vt100 and feeds both parsers the
/// line feed, as it draws `REP`'s characters (#816).
Index { next_line: bool },
/// A [`Motion`] completed, which the emulator carries out from the
/// cursor xterm would start it from: the bytes before the final byte
/// are fed, the cursor read, and the final byte either fed as it is or
/// replaced by the motion written out in moves vt100 gets right.
Motion(Motion),
}

/// A terminal query the application issued. The tracker classifies;
Expand Down Expand Up @@ -1265,6 +1335,12 @@ impl SeqTracker {
self.frame().bottom.unwrap_or(self.rows - 1)
}

/// The top margin of the screen on display, 0-based: the region's
/// first row, or 0 with no region set.
pub(crate) fn top_margin(&self) -> u16 {
self.frame().top
}

/// Whether origin mode (`DECOM`) is set on the screen on display.
pub(crate) fn origin_mode(&self) -> bool {
self.frame().origin
Expand Down Expand Up @@ -1313,11 +1389,13 @@ impl SeqTracker {
frame.origin = frame.saved_origin;
}

/// `DECSTBM` (`CSI Pt ; Pb r`), read as vt100 reads it: an omitted or
/// zero margin is the screen's edge, a bottom past the screen is its
/// last row, and a region that does not have its top above its bottom
/// is no region at all.
fn set_margins(&mut self) {
/// `DECSTBM` (`CSI Pt ; Pb r`), read as xterm reads it (`CASE_DECSTBM`,
/// charproc.c 4773): an omitted or zero margin is the screen's edge, a
/// bottom past the screen is its last row, and a region that does not
/// have its top above its bottom is ignored, leaving the region as it
/// was. vt100 took that one for no region at all, and the emulator now
/// keeps it from vt100 (#821). Returns whether the region was set.
fn set_margins(&mut self) -> bool {
let given = |param: u32, default: u16| match u16::try_from(param).unwrap_or(u16::MAX) {
0 => default,
value => value,
Expand All @@ -1333,17 +1411,24 @@ impl SeqTracker {
given(self.csi_second_param, self.rows)
};
let (top, bottom) = (top - 1, (bottom - 1).min(self.rows - 1));
if top >= bottom {
return false;
}
let frame = self.frame_mut();
(frame.top, frame.bottom) = if top < bottom {
(top, Some(bottom))
} else {
(0, None)
};
(frame.top, frame.bottom) = (top, Some(bottom));
true
}

/// Set or reset one of the modes that move a [`ScreenFrame`], in vt100's
/// way.
/// way, but for the margins and origin mode, which go with a switch of
/// screens: xterm keeps one set of each, beside the cursor, for both
/// (`SwitchBufs`, charproc.c 9564, moves neither), and the emulator
/// carries vt100's across to match (#820).
fn set_frame_mode(&mut self, mode: u16, set: bool) {
let carried = {
let frame = self.frame();
(frame.top, frame.bottom, frame.origin)
};
match (mode, set) {
(6, _) => self.frame_mut().origin = set,
(47, _) => self.alternate = set,
Expand All @@ -1366,10 +1451,25 @@ impl SeqTracker {
}
(1049, false) => {
self.alternate = false;
let frame = self.frame_mut();
(frame.top, frame.bottom, frame.origin) = carried;
self.restore_cursor();
return;
}
_ => {}
}
if matches!(mode, 47 | 1049) {
let frame = self.frame_mut();
(frame.top, frame.bottom, frame.origin) = carried;
}
}

/// Whether the CSI just completed named `1049`, whose reset restores
/// the cursor saved on the main screen and whose set clears a pending
/// wrap with the screen, where `47` moves the cursor in neither
/// direction (charproc.c 7732 and 7759).
pub(crate) fn named_1049(&self) -> bool {
self.csi_frame_modes[..usize::from(self.csi_frame_mode_count)].contains(&1049)
}

/// Carry out a [`TabOp`] at cursor column `col`, returning the column the
Expand Down Expand Up @@ -2026,7 +2126,10 @@ impl SeqTracker {
// of 2026, whose Begin returns from a list that names one of these
// too.
match (self.csi_prefix, b) {
(0, b'r') => self.set_margins(),
(0, b'r') => {
let valid = self.set_margins();
return SeqEvent::Motion(Motion::Margins { valid });
}
// SCOSC / SCORC. With a parameter `CSI s` is DECSLRM in xterm
// and `CSI u` belongs to kitty's keyboard protocol.
(0, b's') if params_empty => self.save_charsets(),
Expand Down Expand Up @@ -2168,6 +2271,16 @@ impl SeqTracker {
}
b'I' => return SeqEvent::Tabs(TabOp::Forward(count)),
b'Z' => return SeqEvent::Tabs(TabOp::Back(count)),
// ICH and the vertical motions by their first parameter,
// as vt100 and xterm read them.
b'@' => return SeqEvent::Motion(Motion::InsertChars(count)),
b'A' => return SeqEvent::Motion(Motion::Up(count)),
b'B' => return SeqEvent::Motion(Motion::Down(count)),
b'E' => return SeqEvent::Motion(Motion::NextLine(count)),
b'F' => return SeqEvent::Motion(Motion::PrevLine(count)),
b'd' => return SeqEvent::Motion(Motion::Row(count)),
b'D' => return SeqEvent::Motion(Motion::Back(count)),
b'P' => return SeqEvent::Motion(Motion::DeleteChars(count)),
// ED 3, erase the saved lines. vt100 reads the first
// parameter alone, and so does this.
b'J' if ps == 3 => return SeqEvent::ScrollbackErased,
Expand Down Expand Up @@ -2858,6 +2971,14 @@ impl SeqTracker {
// application set a stop of its own — a table drawn
// half from each.
HT => event = SeqEvent::Tabs(TabOp::Forward(1)),
// The controls that cancel a pending wrap from the
// last column in xterm (#814).
0x0a..=0x0c if self.utf8_remaining == 0 => {
event = SeqEvent::Motion(Motion::LineFeed);
}
0x08 if self.utf8_remaining == 0 => {
event = SeqEvent::Motion(Motion::Backspace);
}
_ => {}
}
// Anything that is not a C0 control or DEL. Named once
Expand Down Expand Up @@ -3035,6 +3156,12 @@ impl SeqTracker {
};
State::Ground
}
// RI, which the emulator carries out where it differs from
// vt100's (#821).
b'M' => {
event = SeqEvent::Motion(Motion::ReverseIndex);
State::Ground
}
// DECID, the VT100's DA1 (#833).
b'Z' => {
event = SeqEvent::Query(Query::Decid);
Expand Down
Loading
Loading