From 5d8e26a99d4338dddd5b0d47334df148b7b30024 Mon Sep 17 00:00:00 2001 From: insjang Date: Wed, 2 Sep 2026 22:29:03 +0900 Subject: [PATCH] Terminal: clear the wrap mark on erase, paint a selection whole The mark that says a line runs on to the next (set when the terminal folds a line at the margin) could only be set, never cleared. A program that draws its own screen erases a row before it draws it again, and a row that was folded in one frame is a row of its own in the next; the stale mark then joined unrelated rows when copying. The mark now comes off when what ran on to the margin is erased or moved off it: erasing to the end of the line, as xterm and Ghostty do, erasing characters as far as the margin (Ghostty clears it on any such erase), or deleting characters (xterm). Writing over part of the line leaves it, since the line still runs on. Setting or clearing the mark tells snapshots, so a reader sees it without another change to the line. ITerminalTextData gains clearWrappedLine, a default method. The selection colour was drawn only where text was drawn, leaving the cells past the last character on a line and the gaps under narrow glyphs in the ordinary colours. The selected range on each line is now filled first. The canvas is created with NO_REDRAW_RESIZE, which repaints only what a resize uncovers; when the grid got narrower, what was drawn in the columns now past its edge stayed on screen until something else repainted it. A narrower grid now redraws. And a drag that goes past the top or bottom edge keeps scrolling while the pointer stays there, so a selection can reach beyond the visible page, by characters, words or lines as the drag began. ITerminalTextData.clearWrappedLine is a new default method (@since 1.2), within the 1.2.0 bundle and the 1.1.0 org.eclipse.terminal.model export that master already has. --- .../emulator/VT100EmulatorBackend.java | 21 +++++- .../model/SynchronizedTerminalTextData.java | 5 ++ .../internal/model/TerminalTextData.java | 7 ++ .../model/TerminalTextDataFastScroll.java | 6 ++ .../internal/model/TerminalTextDataStore.java | 5 ++ .../model/TerminalTextDataWindow.java | 7 ++ .../internal/textcanvas/TextCanvas.java | 69 +++++++++++++++---- .../internal/textcanvas/TextLineRenderer.java | 10 +++ .../terminal/model/ITerminalTextData.java | 10 +++ .../emulator/VT100EmulatorBackendTest.java | 50 ++++++++++++++ .../model/AbstractITerminalTextDataTest.java | 3 + .../model/TerminalTextDataSnapshotTest.java | 17 +++++ .../model/TerminalTextDataWindowTest.java | 4 ++ .../textcanvas/TextCanvasModelTest.java | 16 +++++ 14 files changed, 213 insertions(+), 17 deletions(-) diff --git a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100EmulatorBackend.java b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100EmulatorBackend.java index fe9e0b9a2e8..43f8b3d490e 100644 --- a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100EmulatorBackend.java +++ b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100EmulatorBackend.java @@ -429,15 +429,20 @@ private void eraseLine(int line) { @Override public void eraseLineToEnd() { synchronized (fTerminal) { - eraseWhole(toAbsoluteLine(fCursorLine), fCursorColumn, fColumns); + int line = toAbsoluteLine(fCursorLine); + eraseWhole(line, fCursorColumn, fColumns); + // Nothing is left out to the margin, so the line ends where the cursor is. + fTerminal.clearWrappedLine(line); } } /** * Erasing either half of a wide character erases all of it, in the same * background as the rest of the range. + * + * @return where the erase ended, past a wide character it took whole */ - private void eraseWhole(int line, int from, int to) { + private int eraseWhole(int line, int from, int to) { if (isSecondCell(line, from)) { from--; } @@ -445,6 +450,7 @@ private void eraseWhole(int line, int from, int to) { to++; } eraseCells(line, from, to); + return to; } /** @@ -518,6 +524,8 @@ public void deleteCharacters(int n) { fTerminal.setChar(line, col - n, c, style); } eraseCells(line, Math.max(fCursorColumn, fColumns - n), fColumns); + // what reached the margin moved off it, so the line ends before it (xterm) + fTerminal.clearWrappedLine(line); } } @@ -884,7 +892,14 @@ public void eraseCharacters(int n) { return; // nothing is erased, so no wide character may be broken either } synchronized (fTerminal) { - eraseWhole(toAbsoluteLine(fCursorLine), fCursorColumn, Math.min(fCursorColumn + n, fColumns)); + int line = toAbsoluteLine(fCursorLine); + int end = n >= fColumns - fCursorColumn ? fColumns : fCursorColumn + n; + // a wide character the erase ends in is erased whole, maybe up to the margin + if (eraseWhole(line, fCursorColumn, end) == fColumns) { + // Nothing is left out to the margin, so the line ends before it, as + // erasing to the end of the line says (xterm; Ghostty on any ECH). + fTerminal.clearWrappedLine(line); + } } } } diff --git a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/model/SynchronizedTerminalTextData.java b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/model/SynchronizedTerminalTextData.java index a142ef8578a..3de26a78cb4 100644 --- a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/model/SynchronizedTerminalTextData.java +++ b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/model/SynchronizedTerminalTextData.java @@ -157,4 +157,9 @@ synchronized public boolean isWrappedLine(int line) { synchronized public void setWrappedLine(int line) { fData.setWrappedLine(line); } + + @Override + synchronized public void clearWrappedLine(int line) { + fData.clearWrappedLine(line); + } } diff --git a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/model/TerminalTextData.java b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/model/TerminalTextData.java index 78990827baf..b4cae86bc03 100644 --- a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/model/TerminalTextData.java +++ b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/model/TerminalTextData.java @@ -340,5 +340,12 @@ public boolean isWrappedLine(int line) { @Override public void setWrappedLine(int line) { fData.setWrappedLine(line); + sendLineChangedToSnapshots(line); + } + + @Override + public void clearWrappedLine(int line) { + fData.clearWrappedLine(line); + sendLineChangedToSnapshots(line); } } diff --git a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/model/TerminalTextDataFastScroll.java b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/model/TerminalTextDataFastScroll.java index c76fab20863..db68f309069 100644 --- a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/model/TerminalTextDataFastScroll.java +++ b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/model/TerminalTextDataFastScroll.java @@ -308,4 +308,10 @@ public void setWrappedLine(int line) { fData.setWrappedLine(getPositionOfLine(line)); } + @Override + public void clearWrappedLine(int line) { + validateLineParameter(line); + fData.clearWrappedLine(getPositionOfLine(line)); + } + } diff --git a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/model/TerminalTextDataStore.java b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/model/TerminalTextDataStore.java index 04763621546..d0f3ac2b398 100644 --- a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/model/TerminalTextDataStore.java +++ b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/model/TerminalTextDataStore.java @@ -394,4 +394,9 @@ public boolean isWrappedLine(int line) { public void setWrappedLine(int line) { fWrappedLines.set(line); } + + @Override + public void clearWrappedLine(int line) { + fWrappedLines.clear(line); + } } diff --git a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/model/TerminalTextDataWindow.java b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/model/TerminalTextDataWindow.java index 21e343ee6b8..92149232a88 100644 --- a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/model/TerminalTextDataWindow.java +++ b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/model/TerminalTextDataWindow.java @@ -267,4 +267,11 @@ public void setWrappedLine(int line) { fData.setWrappedLine(line - fWindowStartLine); } } + + @Override + public void clearWrappedLine(int line) { + if (isInWindow(line)) { + fData.clearWrappedLine(line - fWindowStartLine); + } + } } diff --git a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/textcanvas/TextCanvas.java b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/textcanvas/TextCanvas.java index a088b3bb6be..11d7487ee77 100644 --- a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/textcanvas/TextCanvas.java +++ b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/textcanvas/TextCanvas.java @@ -58,6 +58,9 @@ public class TextCanvas extends GridCanvas { private final ILinelRenderer fCellRenderer; private boolean fScrollLock; private Point fDraggingStart; + /** -1 above the top, 1 below the bottom, 0 while the pointer is inside */ + private boolean fDragScrolling; + private static final int DRAG_SCROLL_INTERVAL = 60; private Point fDraggingEnd; private boolean fHasSelection; private ResizeListener fResizeListener; @@ -270,21 +273,13 @@ public void mouseUp(MouseEvent e) { }); addMouseMoveListener(e -> { if (fDraggingStart != null) { - Point curr = screenPointToCell(e.x, e.y); - updateHasSelection(e); - switch (fSelMode) { - case WORD: - updateWordSelection(curr); - break; - case LINE: - updateLineSelection(curr); - break; - case DRAG: - default: - setSelection(curr); - break; + // Dragging past an edge keeps the lines beyond it coming into view. + if (dragPast(e.y) != 0 && !fDragScrolling) { + fDragScrolling = true; + getDisplay().timerExec(DRAG_SCROLL_INTERVAL, this::dragScroll); } - fCellCanvasModel.expandHoverSelectionAt(-1, -1); + updateHasSelection(e); + extendSelection(screenPointToCell(e.x, e.y)); } else if ((e.stateMask & SWT.MODIFIER_MASK) == SWT.MOD1) { // highlight (underline) word that would be used by MOD1 + mouse click Point pt = screenPointToCell(e.x, e.y); @@ -298,6 +293,47 @@ public void mouseUp(MouseEvent e) { setHorizontalBarVisible(false); } + /** + * A drag moves the end of the selection to the cell, by characters, words or + * lines as it began: the one way a drag does, whether the pointer moved or the + * view scrolled under it. + */ + private void extendSelection(Point cell) { + switch (fSelMode) { + case WORD: + updateWordSelection(cell); + break; + case LINE: + updateLineSelection(cell); + break; + case DRAG: + default: + setSelection(cell); + break; + } + fCellCanvasModel.expandHoverSelectionAt(-1, -1); + } + + /** @return -1 above the canvas, 1 below it, 0 on it */ + private int dragPast(int y) { + return y < 0 ? -1 : y >= getClientArea().height ? 1 : 0; + } + + private void dragScroll() { + Point p = isDisposed() ? null : toControl(getDisplay().getCursorLocation()); + // where the pointer is now, not where the last move left it: a new click may have + // come in between + int past = p == null ? 0 : dragPast(p.y); + if (p == null || fDraggingStart == null || past == 0) { + fDragScrolling = false; + return; + } + scrollYDelta(past * getCellHeight()); + extendSelection(screenPointToCell(p.x, p.y)); + redraw(); + getDisplay().timerExec(DRAG_SCROLL_INTERVAL, this::dragScroll); + } + private static class Range { final Point start; final Point end; @@ -495,6 +531,11 @@ private void calculateGrid() { } finally { setRedraw(true); } + // NO_REDRAW_RESIZE paints only what a resize uncovers. When the grid gets + // narrower, what was drawn in the columns now past its edge stays there. + if (getVirtualBounds().width < virtualBounds.width) { + redraw(); + } } void scrollToEnd() { diff --git a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/textcanvas/TextLineRenderer.java b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/textcanvas/TextLineRenderer.java index ae515ecfa40..56c185363a1 100644 --- a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/textcanvas/TextLineRenderer.java +++ b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/textcanvas/TextLineRenderer.java @@ -97,6 +97,16 @@ public void drawLine(ITextCanvasModel model, GC gc, int line, int x, int y, int setupGC(gc, style); Point start = model.getSelectionStart(); Point end = model.getSelectionEnd(); + // Everything the selection covers on this line goes down first. Drawing + // it as the text is drawn leaves out whatever the text does not reach: + // the cells past the last character a program put on the line, and the + // cells where the font draws a glyph narrower than the one it sits in. + int from = Math.max(start.y == line ? start.x : 0, colFirst); + int to = end.y == line ? Math.min(end.x + 1, colLast) : colLast; + if (to > from) { + gc.fillRectangle(x + (from - colFirst) * getCellWidth(), y, (to - from) * getCellWidth(), + getCellHeight()); + } char[] chars = model.getTerminalText().getChars(line); if (chars != null) { int offset = 0; diff --git a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/model/ITerminalTextData.java b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/model/ITerminalTextData.java index f972bbbe28b..46f145a370e 100644 --- a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/model/ITerminalTextData.java +++ b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/model/ITerminalTextData.java @@ -156,4 +156,14 @@ public interface ITerminalTextData extends ITerminalTextDataReadOnly { */ void setWrappedLine(int line); + /** + * Says the line no longer runs on to the next, undoing + * {@link #setWrappedLine(int)}: what ran on to the margin was erased. + * + * @param line line to unmark, must be >=0 and < {@link #getHeight()} + * @since 1.2 + */ + default void clearWrappedLine(int line) { + } + } diff --git a/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/emulator/VT100EmulatorBackendTest.java b/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/emulator/VT100EmulatorBackendTest.java index 02a53b13994..7ac103bf774 100644 --- a/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/emulator/VT100EmulatorBackendTest.java +++ b/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/emulator/VT100EmulatorBackendTest.java @@ -1762,4 +1762,54 @@ public void testInsertAndDeleteOnHalfOfWideKeepBackground() { assertEquals(GREEN, term.getStyle(line, 0).getBackgroundTerminalColor(), "line " + line); } } + + @Test + public void testWrappedLineMarkComesOff() { + ITerminalTextData term = makeITerminalTextData(); + IVT100EmulatorBackend vt100 = makeBakend(term); + term.setMaxHeight(3); + vt100.setDimensions(3, 5); + vt100.setCursor(0, 0); + // a line the terminal folded is marked as running on to the next + vt100.appendString("abcdefg"); + assertTrue(term.isWrappedLine(0)); + assertFalse(term.isWrappedLine(1)); + // writing over part of it leaves it running on: aXcde is still followed by fg + vt100.setCursor(0, 1); + vt100.appendString("X"); + assertTrue(term.isWrappedLine(0)); + // erasing to the end of the line ends it there, as in xterm and Ghostty + vt100.setCursor(0, 3); + vt100.eraseLineToEnd(); + assertFalse(term.isWrappedLine(0)); + // and so does erasing characters as far as the margin, but not short of it + vt100.setCursor(0, 0); + vt100.appendString("abcdefg"); + vt100.setCursor(0, 1); + vt100.eraseCharacters(2); + assertTrue(term.isWrappedLine(0)); + vt100.eraseCharacters(9); + assertFalse(term.isWrappedLine(0)); + // however large the count + vt100.setCursor(0, 0); + vt100.appendString("abcdefg"); + vt100.setCursor(0, 1); + vt100.eraseCharacters(Integer.MAX_VALUE); + assertFalse(term.isWrappedLine(0)); + assertEquals('a', term.getChar(0, 0)); + assertEquals(0, term.getChar(0, 1)); + // deleting characters pulls blanks in at the margin, so it ends the line too (xterm) + vt100.setCursor(0, 0); + vt100.appendString("abcdefg"); + vt100.setCursor(0, 1); + vt100.deleteCharacters(1); + assertFalse(term.isWrappedLine(0)); + // erasing the first cell of a wide character at the margin erases it whole, to the margin + vt100.setCursor(0, 0); + vt100.appendString("abc\ud55cfg"); + assertTrue(term.isWrappedLine(0)); + vt100.setCursor(0, 3); + vt100.eraseCharacters(1); + assertFalse(term.isWrappedLine(0)); + } } diff --git a/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/model/AbstractITerminalTextDataTest.java b/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/model/AbstractITerminalTextDataTest.java index f1d3857ba27..457b4ebc943 100644 --- a/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/model/AbstractITerminalTextDataTest.java +++ b/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/model/AbstractITerminalTextDataTest.java @@ -654,5 +654,8 @@ public void testWrappedLines() { assertTrue(term.isWrappedLine(3)); term.cleanLine(0); assertFalse(term.isWrappedLine(0)); + term.setWrappedLine(2); + term.clearWrappedLine(2); + assertFalse(term.isWrappedLine(2)); } } diff --git a/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/model/TerminalTextDataSnapshotTest.java b/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/model/TerminalTextDataSnapshotTest.java index ad9814f5002..7a509ba4829 100644 --- a/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/model/TerminalTextDataSnapshotTest.java +++ b/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/model/TerminalTextDataSnapshotTest.java @@ -1288,4 +1288,21 @@ public void testCopyRefreshesSnapshotOfAnotherSize() { assertEquals(3, snapshot.getWidth()); assertEquals(toMultiLineText(source), toMultiLineText(snapshot)); } + + @Test + public void testWrapMarkChangesReachTheSnapshot() { + ITerminalTextData term = new TerminalTextData(); + term.setDimensions(2, 4); + ITerminalTextDataSnapshot snapshot = term.makeSnapshot(); + snapshot.updateSnapshot(false); + // setting or clearing the mark alone is a change readers have to see + term.setWrappedLine(0); + assertTrue(snapshot.isOutOfDate()); + snapshot.updateSnapshot(false); + assertTrue(snapshot.isWrappedLine(0)); + term.clearWrappedLine(0); + assertTrue(snapshot.isOutOfDate()); + snapshot.updateSnapshot(false); + assertFalse(snapshot.isWrappedLine(0)); + } } diff --git a/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/model/TerminalTextDataWindowTest.java b/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/model/TerminalTextDataWindowTest.java index 163af3be430..b4be30273ad 100644 --- a/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/model/TerminalTextDataWindowTest.java +++ b/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/model/TerminalTextDataWindowTest.java @@ -441,5 +441,9 @@ public void testWrappedLines() { assertTrue(term.isWrappedLine(3)); term.cleanLine(3); assertFalse(term.isWrappedLine(3)); + term.setWrappedLine(3); + term.clearWrappedLine(3); + assertFalse(term.isWrappedLine(3)); + term.clearWrappedLine(0); // outside window, harmless } } diff --git a/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/textcanvas/TextCanvasModelTest.java b/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/textcanvas/TextCanvasModelTest.java index 26648eedeae..ecc792c2fa1 100644 --- a/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/textcanvas/TextCanvasModelTest.java +++ b/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/textcanvas/TextCanvasModelTest.java @@ -70,4 +70,20 @@ public void testSelectionCoversWideCharactersWhole() { assertEquals(new Point(0, 0), model.getSelectionStart()); assertEquals(new Point(3, 0), model.getSelectionEnd()); } + + @Test + public void testCopyKeepsSpacesBeforeAFold() { + TerminalTextData data = new TerminalTextData(); + data.setDimensions(2, 4); + // "foo bar" folded at four columns: the space ends the first row + data.setChars(0, 0, "foo ".toCharArray(), null); + data.setWrappedLine(0); + data.setChars(1, 0, "bar".toCharArray(), null); + ITerminalTextDataSnapshot snapshot = data.makeSnapshot(); + snapshot.updateSnapshot(true); + AbstractTextCanvasModel model = new AbstractTextCanvasModel(snapshot) { + }; + model.setSelection(0, 1, 0, 3); + assertEquals("foo bar", model.getSelectedText()); + } }