Skip to content

Commit bc125e3

Browse files
insjangakurtakov
authored andcommitted
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.
1 parent 3d40d5d commit bc125e3

14 files changed

Lines changed: 213 additions & 17 deletions

File tree

‎terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100EmulatorBackend.java‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -429,22 +429,28 @@ private void eraseLine(int line) {
429429
@Override
430430
public void eraseLineToEnd() {
431431
synchronized (fTerminal) {
432-
eraseWhole(toAbsoluteLine(fCursorLine), fCursorColumn, fColumns);
432+
int line = toAbsoluteLine(fCursorLine);
433+
eraseWhole(line, fCursorColumn, fColumns);
434+
// Nothing is left out to the margin, so the line ends where the cursor is.
435+
fTerminal.clearWrappedLine(line);
433436
}
434437
}
435438

436439
/**
437440
* Erasing either half of a wide character erases all of it, in the same
438441
* background as the rest of the range.
442+
*
443+
* @return where the erase ended, past a wide character it took whole
439444
*/
440-
private void eraseWhole(int line, int from, int to) {
445+
private int eraseWhole(int line, int from, int to) {
441446
if (isSecondCell(line, from)) {
442447
from--;
443448
}
444449
if (isSecondCell(line, to)) {
445450
to++;
446451
}
447452
eraseCells(line, from, to);
453+
return to;
448454
}
449455

450456
/**
@@ -518,6 +524,8 @@ public void deleteCharacters(int n) {
518524
fTerminal.setChar(line, col - n, c, style);
519525
}
520526
eraseCells(line, Math.max(fCursorColumn, fColumns - n), fColumns);
527+
// what reached the margin moved off it, so the line ends before it (xterm)
528+
fTerminal.clearWrappedLine(line);
521529
}
522530
}
523531

@@ -884,7 +892,14 @@ public void eraseCharacters(int n) {
884892
return; // nothing is erased, so no wide character may be broken either
885893
}
886894
synchronized (fTerminal) {
887-
eraseWhole(toAbsoluteLine(fCursorLine), fCursorColumn, Math.min(fCursorColumn + n, fColumns));
895+
int line = toAbsoluteLine(fCursorLine);
896+
int end = n >= fColumns - fCursorColumn ? fColumns : fCursorColumn + n;
897+
// a wide character the erase ends in is erased whole, maybe up to the margin
898+
if (eraseWhole(line, fCursorColumn, end) == fColumns) {
899+
// Nothing is left out to the margin, so the line ends before it, as
900+
// erasing to the end of the line says (xterm; Ghostty on any ECH).
901+
fTerminal.clearWrappedLine(line);
902+
}
888903
}
889904
}
890905
}

‎terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/model/SynchronizedTerminalTextData.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,9 @@ synchronized public boolean isWrappedLine(int line) {
157157
synchronized public void setWrappedLine(int line) {
158158
fData.setWrappedLine(line);
159159
}
160+
161+
@Override
162+
synchronized public void clearWrappedLine(int line) {
163+
fData.clearWrappedLine(line);
164+
}
160165
}

‎terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/model/TerminalTextData.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,5 +340,12 @@ public boolean isWrappedLine(int line) {
340340
@Override
341341
public void setWrappedLine(int line) {
342342
fData.setWrappedLine(line);
343+
sendLineChangedToSnapshots(line);
344+
}
345+
346+
@Override
347+
public void clearWrappedLine(int line) {
348+
fData.clearWrappedLine(line);
349+
sendLineChangedToSnapshots(line);
343350
}
344351
}

‎terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/model/TerminalTextDataFastScroll.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,4 +308,10 @@ public void setWrappedLine(int line) {
308308
fData.setWrappedLine(getPositionOfLine(line));
309309
}
310310

311+
@Override
312+
public void clearWrappedLine(int line) {
313+
validateLineParameter(line);
314+
fData.clearWrappedLine(getPositionOfLine(line));
315+
}
316+
311317
}

‎terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/model/TerminalTextDataStore.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,4 +394,9 @@ public boolean isWrappedLine(int line) {
394394
public void setWrappedLine(int line) {
395395
fWrappedLines.set(line);
396396
}
397+
398+
@Override
399+
public void clearWrappedLine(int line) {
400+
fWrappedLines.clear(line);
401+
}
397402
}

‎terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/model/TerminalTextDataWindow.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,4 +267,11 @@ public void setWrappedLine(int line) {
267267
fData.setWrappedLine(line - fWindowStartLine);
268268
}
269269
}
270+
271+
@Override
272+
public void clearWrappedLine(int line) {
273+
if (isInWindow(line)) {
274+
fData.clearWrappedLine(line - fWindowStartLine);
275+
}
276+
}
270277
}

‎terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/textcanvas/TextCanvas.java‎

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ public class TextCanvas extends GridCanvas {
5858
private final ILinelRenderer fCellRenderer;
5959
private boolean fScrollLock;
6060
private Point fDraggingStart;
61+
/** -1 above the top, 1 below the bottom, 0 while the pointer is inside */
62+
private boolean fDragScrolling;
63+
private static final int DRAG_SCROLL_INTERVAL = 60;
6164
private Point fDraggingEnd;
6265
private boolean fHasSelection;
6366
private ResizeListener fResizeListener;
@@ -270,21 +273,13 @@ public void mouseUp(MouseEvent e) {
270273
});
271274
addMouseMoveListener(e -> {
272275
if (fDraggingStart != null) {
273-
Point curr = screenPointToCell(e.x, e.y);
274-
updateHasSelection(e);
275-
switch (fSelMode) {
276-
case WORD:
277-
updateWordSelection(curr);
278-
break;
279-
case LINE:
280-
updateLineSelection(curr);
281-
break;
282-
case DRAG:
283-
default:
284-
setSelection(curr);
285-
break;
276+
// Dragging past an edge keeps the lines beyond it coming into view.
277+
if (dragPast(e.y) != 0 && !fDragScrolling) {
278+
fDragScrolling = true;
279+
getDisplay().timerExec(DRAG_SCROLL_INTERVAL, this::dragScroll);
286280
}
287-
fCellCanvasModel.expandHoverSelectionAt(-1, -1);
281+
updateHasSelection(e);
282+
extendSelection(screenPointToCell(e.x, e.y));
288283
} else if ((e.stateMask & SWT.MODIFIER_MASK) == SWT.MOD1) {
289284
// highlight (underline) word that would be used by MOD1 + mouse click
290285
Point pt = screenPointToCell(e.x, e.y);
@@ -298,6 +293,47 @@ public void mouseUp(MouseEvent e) {
298293
setHorizontalBarVisible(false);
299294
}
300295

296+
/**
297+
* A drag moves the end of the selection to the cell, by characters, words or
298+
* lines as it began: the one way a drag does, whether the pointer moved or the
299+
* view scrolled under it.
300+
*/
301+
private void extendSelection(Point cell) {
302+
switch (fSelMode) {
303+
case WORD:
304+
updateWordSelection(cell);
305+
break;
306+
case LINE:
307+
updateLineSelection(cell);
308+
break;
309+
case DRAG:
310+
default:
311+
setSelection(cell);
312+
break;
313+
}
314+
fCellCanvasModel.expandHoverSelectionAt(-1, -1);
315+
}
316+
317+
/** @return -1 above the canvas, 1 below it, 0 on it */
318+
private int dragPast(int y) {
319+
return y < 0 ? -1 : y >= getClientArea().height ? 1 : 0;
320+
}
321+
322+
private void dragScroll() {
323+
Point p = isDisposed() ? null : toControl(getDisplay().getCursorLocation());
324+
// where the pointer is now, not where the last move left it: a new click may have
325+
// come in between
326+
int past = p == null ? 0 : dragPast(p.y);
327+
if (p == null || fDraggingStart == null || past == 0) {
328+
fDragScrolling = false;
329+
return;
330+
}
331+
scrollYDelta(past * getCellHeight());
332+
extendSelection(screenPointToCell(p.x, p.y));
333+
redraw();
334+
getDisplay().timerExec(DRAG_SCROLL_INTERVAL, this::dragScroll);
335+
}
336+
301337
private static class Range {
302338
final Point start;
303339
final Point end;
@@ -495,6 +531,11 @@ private void calculateGrid() {
495531
} finally {
496532
setRedraw(true);
497533
}
534+
// NO_REDRAW_RESIZE paints only what a resize uncovers. When the grid gets
535+
// narrower, what was drawn in the columns now past its edge stays there.
536+
if (getVirtualBounds().width < virtualBounds.width) {
537+
redraw();
538+
}
498539
}
499540

500541
void scrollToEnd() {

‎terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/textcanvas/TextLineRenderer.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,16 @@ public void drawLine(ITextCanvasModel model, GC gc, int line, int x, int y, int
9797
setupGC(gc, style);
9898
Point start = model.getSelectionStart();
9999
Point end = model.getSelectionEnd();
100+
// Everything the selection covers on this line goes down first. Drawing
101+
// it as the text is drawn leaves out whatever the text does not reach:
102+
// the cells past the last character a program put on the line, and the
103+
// cells where the font draws a glyph narrower than the one it sits in.
104+
int from = Math.max(start.y == line ? start.x : 0, colFirst);
105+
int to = end.y == line ? Math.min(end.x + 1, colLast) : colLast;
106+
if (to > from) {
107+
gc.fillRectangle(x + (from - colFirst) * getCellWidth(), y, (to - from) * getCellWidth(),
108+
getCellHeight());
109+
}
100110
char[] chars = model.getTerminalText().getChars(line);
101111
if (chars != null) {
102112
int offset = 0;

‎terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/model/ITerminalTextData.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,14 @@ public interface ITerminalTextData extends ITerminalTextDataReadOnly {
156156
*/
157157
void setWrappedLine(int line);
158158

159+
/**
160+
* Says the line no longer runs on to the next, undoing
161+
* {@link #setWrappedLine(int)}: what ran on to the margin was erased.
162+
*
163+
* @param line line to unmark, must be &gt;=0 and &lt; {@link #getHeight()}
164+
* @since 1.2
165+
*/
166+
default void clearWrappedLine(int line) {
167+
}
168+
159169
}

‎terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/emulator/VT100EmulatorBackendTest.java‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,4 +1762,54 @@ public void testInsertAndDeleteOnHalfOfWideKeepBackground() {
17621762
assertEquals(GREEN, term.getStyle(line, 0).getBackgroundTerminalColor(), "line " + line);
17631763
}
17641764
}
1765+
1766+
@Test
1767+
public void testWrappedLineMarkComesOff() {
1768+
ITerminalTextData term = makeITerminalTextData();
1769+
IVT100EmulatorBackend vt100 = makeBakend(term);
1770+
term.setMaxHeight(3);
1771+
vt100.setDimensions(3, 5);
1772+
vt100.setCursor(0, 0);
1773+
// a line the terminal folded is marked as running on to the next
1774+
vt100.appendString("abcdefg");
1775+
assertTrue(term.isWrappedLine(0));
1776+
assertFalse(term.isWrappedLine(1));
1777+
// writing over part of it leaves it running on: aXcde is still followed by fg
1778+
vt100.setCursor(0, 1);
1779+
vt100.appendString("X");
1780+
assertTrue(term.isWrappedLine(0));
1781+
// erasing to the end of the line ends it there, as in xterm and Ghostty
1782+
vt100.setCursor(0, 3);
1783+
vt100.eraseLineToEnd();
1784+
assertFalse(term.isWrappedLine(0));
1785+
// and so does erasing characters as far as the margin, but not short of it
1786+
vt100.setCursor(0, 0);
1787+
vt100.appendString("abcdefg");
1788+
vt100.setCursor(0, 1);
1789+
vt100.eraseCharacters(2);
1790+
assertTrue(term.isWrappedLine(0));
1791+
vt100.eraseCharacters(9);
1792+
assertFalse(term.isWrappedLine(0));
1793+
// however large the count
1794+
vt100.setCursor(0, 0);
1795+
vt100.appendString("abcdefg");
1796+
vt100.setCursor(0, 1);
1797+
vt100.eraseCharacters(Integer.MAX_VALUE);
1798+
assertFalse(term.isWrappedLine(0));
1799+
assertEquals('a', term.getChar(0, 0));
1800+
assertEquals(0, term.getChar(0, 1));
1801+
// deleting characters pulls blanks in at the margin, so it ends the line too (xterm)
1802+
vt100.setCursor(0, 0);
1803+
vt100.appendString("abcdefg");
1804+
vt100.setCursor(0, 1);
1805+
vt100.deleteCharacters(1);
1806+
assertFalse(term.isWrappedLine(0));
1807+
// erasing the first cell of a wide character at the margin erases it whole, to the margin
1808+
vt100.setCursor(0, 0);
1809+
vt100.appendString("abc\ud55cfg");
1810+
assertTrue(term.isWrappedLine(0));
1811+
vt100.setCursor(0, 3);
1812+
vt100.eraseCharacters(1);
1813+
assertFalse(term.isWrappedLine(0));
1814+
}
17651815
}

0 commit comments

Comments
 (0)