Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Export-Package: org.eclipse.terminal.connector;version="1.0.100";
org.eclipse.swt.widgets,
org.eclipse.terminal.control",
org.eclipse.terminal.connector.provider;version="1.0.100";uses:="org.eclipse.terminal.connector",
org.eclipse.terminal.control;version="1.0.100";
org.eclipse.terminal.control;version="1.1.0";
uses:="org.eclipse.swt.dnd,
org.eclipse.swt.graphics,
org.eclipse.swt.widgets,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,21 @@ public interface ITerminalViewControl {
*/
public boolean pasteString(String string);

/**
* Pastes text as the user pastes it, unlike {@link #pasteString(String)}, which
* writes it as it is, for callers that run commands with it: each line break goes
* as the Enter key sends it, and the text is marked as pasted when the program
* asked for bracketed paste.
*
* @param text the text to paste
* @return whether it was sent, which it is not while the terminal is not
* connected
* @since 1.2
*/
default boolean paste(String text) {
return pasteString(text);
}

boolean isConnected();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,19 @@ public interface ITerminalControlForText {
*/
void enableApplicationCursorKeys(boolean enable);

/**
* A program that asks for bracketed paste (CSI ?2004) wants pasted text marked
* as such, so that it can take it as text rather than as something typed.
*/
default void enableBracketedPaste(boolean enable) {
}

/**
* Shows or hides the cursor at the program's request (DEC mode 25).
*
* @param show whether the cursor is to be drawn
*/
default void showCursor(boolean show) {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.io.IOException;
import java.io.Reader;
import java.util.Arrays;
import java.util.function.IntConsumer;

import org.eclipse.core.runtime.Platform;
import org.eclipse.swt.events.ControlEvent;
Expand Down Expand Up @@ -521,6 +522,17 @@ private void resetTerminal() {
text.setScrollRegion(-1, -1);
text.setInsertMode(false);
terminal.enableApplicationCursorKeys(false);
resetProgramModes();
}

/**
* The modes a program asked for end with a full reset and with its connection: a
* program that hid the cursor and was cut off must not leave the next shell
* without one.
*/
private void resetProgramModes() {
terminal.showCursor(true);
terminal.enableBracketedPaste(false);
}

/**
Expand Down Expand Up @@ -1302,13 +1314,33 @@ private void processAnsiCommand_X() {
text.eraseCharacters(getAnsiParameter(0));
}

/**
* Runs the action on each mode of a set or reset. Only the first 16 parameters are
* stored, while every ';' is counted: the ones past them are dropped.
*/
private void forEachMode(IntConsumer action) {
for (int i = 0; i <= nextAnsiParameter && i < ansiParameters.length; i++) {
if (ansiParameters[i].length() > 0) { // an empty one names no mode
action.accept(getAnsiParameter(i));
}
}
}

private void processDecPrivateCommand_h() {
int param = getAnsiParameter(0);
// A program may set several modes at once: ncurses turns the mouse on with CSI ? 1006 ; 1000 h.
forEachMode(this::setDecPrivateMode);
}

private void setDecPrivateMode(int param) {
switch (param) {
case 1:
// Enable Application Cursor Keys (DECCKM)
terminal.enableApplicationCursorKeys(true);
break;
case 25:
// Show cursor (DECTCEM).
terminal.showCursor(true);
break;
case 47:
case 1047:
// Use Alternate Screen Buffer, the cursor staying where it is.
Expand All @@ -1324,19 +1356,30 @@ private void processDecPrivateCommand_h() {
// Save cursor, as ESC 7 does.
saveCursor();
break;
case 2004:
// Bracketed paste: pasted text is wrapped so a program can tell it from typing.
terminal.enableBracketedPaste(true);
break;
default:
Logger.log("Unsupported command parameter: CSI ?" + param + 'h'); //$NON-NLS-1$
break;
}
}

private void processDecPrivateCommand_l() {
int param = getAnsiParameter(0);
forEachMode(this::resetDecPrivateMode);
}

private void resetDecPrivateMode(int param) {
switch (param) {
case 1:
// Enable Normal Cursor Keys (DECCKM)
terminal.enableApplicationCursorKeys(false);
break;
case 25:
// Hide cursor (DECTCEM).
terminal.showCursor(false);
break;
case 47:
case 1047:
// Use Normal Screen Buffer, putting back what was on it, the cursor
Expand All @@ -1354,6 +1397,9 @@ private void processDecPrivateCommand_l() {
// Restore cursor, as ESC 8 does.
restoreCursor();
break;
case 2004:
terminal.enableBracketedPaste(false);
break;
default:
Logger.log("Unsupported command parameter: CSI ?" + param + 'l'); //$NON-NLS-1$
break;
Expand Down Expand Up @@ -1627,6 +1673,7 @@ public void resetState() {
text.setStyle(text.getDefaultStyle());
text.setScrollRegion(-1, -1);
text.setInsertMode(false);
resetProgramModes();
}

// public OutputStream getOutputStream() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,23 @@ public class VT100TerminalControl implements ITerminalControlForText, ITerminalC
*/
private final IPropertyChangeListener fPreferenceListener = this::updatePreferences;
private final IPropertyChangeListener fFontListener = this::updateFont;
// set by the reader job, read by the display thread when pasting
private volatile boolean fBracketedPaste;
// asked for by the program (DECTCEM); kept on the control, as the canvas is new after a move.
// Written with the model it is copied to under fCursorLock, so a model made during a move
// cannot miss a hide or show that the reader job makes meanwhile
private boolean fCursorHidden;
private final Object fCursorLock = new Object();
private static final String PASTE_START = "\u001b[200~"; //$NON-NLS-1$
private static final String PASTE_END = "\u001b[201~"; //$NON-NLS-1$

/**
* Is protected by synchronize on this
*/
volatile private Job fJob;

private PollingTextCanvasModel fPollingTextCanvasModel;
// replaced on the display thread when the tab moves, read by the reader job in showCursor
private volatile PollingTextCanvasModel fPollingTextCanvasModel;

/**
* In some circumstances (e.g PowerShell on Windows) the backspace
Expand Down Expand Up @@ -294,7 +304,7 @@ public void paste() {
private void paste(int clipboardType) {
TextTransfer textTransfer = TextTransfer.getInstance();
String strText = (String) fClipboard.getContents(textTransfer, clipboardType);
pasteString(strText);
paste(strText);
}

/**
Expand All @@ -312,6 +322,18 @@ public boolean pasteString(String strText) {
return true;
}

@Override
public boolean paste(String text) {
if (!isConnected() || text == null || text.isEmpty()) {
return false; // nothing to paste: no empty brackets either
}
// a connector that echoes locally (a stream, telnet without remote echo) gets the
// text as it always did: InputStreamMonitor converts the line breaks of a stream
ITerminalConnector connector = getTerminalConnector();
sendString(connector != null && connector.isLocalEcho() ? text : pasteText(text, fBracketedPaste));
return true;
}

@Override
public void selectAll() {
getCtlText().selectAll();
Expand Down Expand Up @@ -610,6 +632,30 @@ public static String modifiedSpecialKey(int keyCode, int modifierKeys) {
return modifier == 1 || key == null ? null : "\u001b[" + key.number() + ';' + modifier + key.last(); //$NON-NLS-1$
}

/**
* Pasted text as it goes to the program: each line break as the Enter key sends
* it, a CR, as Windows Terminal (CR LF to CR) and tmux (LF to CR) paste. A
* Windows clipboard's CR LF would reach the shell as two breaks, and readline
* then draws two rows for every line: a paste taller than the screen pushes the
* prompt off it, and the redraw on the next key lands where the prompt no
* longer is. With bracketed paste on, the text is marked as pasted.
*/
public static String pasteText(String text, boolean bracketedPaste) {
String lines = text.replace("\r\n", "\r").replace('\n', '\r'); //$NON-NLS-1$ //$NON-NLS-2$
Comment thread
insjang marked this conversation as resolved.
Resolved
return bracketedPaste ? bracketed(lines) : lines;
}

/**
* Marks text as pasted, so that a program takes the newlines in it as part of
* the text rather than as the user pressing return on each line. The text cannot
* end the paste early: every ESC and C1 CSI goes, as xterm does, so no end marker
* can be left over or put together from the pieces around one taken out
* ("ESC[20ESC[201~1~").
*/
private static String bracketed(String text) {
return PASTE_START + text.replace("\u001b", "").replace("\u009b", "") + PASTE_END; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
}

protected void sendChar(char chKey, boolean altKeyPressed) {
try {
int byteToSend = chKey;
Expand Down Expand Up @@ -691,8 +737,9 @@ public void setupTerminal(Composite parent) {
fClipboard.dispose();
fPollingTextCanvasModel.stopPolling();
}
// set on this terminal alone (Inverted colors in its menu): kept across a move
// set on this terminal alone (Inverted colors and Scroll Lock in its menu): kept across a move
boolean inverted = fCtlText != null && fCtlText.isInvertedColors();
boolean scrollLock = fCtlText != null && fCtlText.isScrollLock();
if (fWndParent != null && !fWndParent.isDisposed()) {
// terminal widget gets a new parent (DnD)
fWndParent.dispose();
Expand All @@ -715,12 +762,15 @@ public void setupTerminal(Composite parent) {
onTerminalColorsChanged();
onTerminalFontChanged();
setInvertedColors(inverted);
setScrollLock(scrollLock);
}
JFaceResources.getFontRegistry().addListener(fFontListener);
setupHelp(fWndParent, TerminalPlugin.HELP_VIEW);

if (!wasDisposed) {
fState = oldState;
// the new canvas starts with the cursor off: a connected terminal's cursor blinks
fCtlText.setCursorEnabled(isConnected());
}
}

Expand Down Expand Up @@ -816,7 +866,11 @@ protected void setupControls(Composite parent) {
ITerminalTextDataSnapshot snapshot = fTerminalModel.makeSnapshot();
// TODO how to get the initial size correctly!
snapshot.updateSnapshot(false);
fPollingTextCanvasModel = new PollingTextCanvasModel(snapshot);
synchronized (fCursorLock) {
PollingTextCanvasModel model = new PollingTextCanvasModel(snapshot);
model.setCursorHidden(fCursorHidden);
fPollingTextCanvasModel = model;
}
fCtlText = new TextCanvas(fWndParent, fPollingTextCanvasModel, SWT.NONE,
new TextLineRenderer(() -> fCtlText, fPollingTextCanvasModel));

Expand Down Expand Up @@ -1361,6 +1415,11 @@ public void setState(TerminalState state) {
});
}

@Override
public void enableBracketedPaste(boolean enable) {
fBracketedPaste = enable;
}

/**
* @param runnable run in display thread
*/
Expand Down Expand Up @@ -1480,6 +1539,21 @@ public String getHoverSelection() {
return fCtlText.getHoverSelection();
}

/** @return whether the cursor is drawn when on, not hidden by the program (DECTCEM) */
public boolean isCursorShown() {
return !fPollingTextCanvasModel.isCursorHidden();
}

@Override
public void showCursor(boolean show) {
synchronized (fCursorLock) {
fCursorHidden = !show;
if (fPollingTextCanvasModel != null) {
fPollingTextCanvasModel.setCursorHidden(!show);
}
}
}

@Override
public void updateTerminalDimensions() {
getTerminalText().adjustTerminalDimensions();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ abstract public class AbstractTextCanvasModel implements ITextCanvasModel {
private boolean fShowCursor;
private long fCursorTime;
private boolean fCursorIsEnabled;
// set by the emulator thread, read when painting
private volatile boolean fCursorHidden;
private final ITerminalTextDataSnapshot fSnapshot;
private int fLines;

Expand Down Expand Up @@ -150,7 +152,17 @@ public int getCursorLine() {

@Override
public boolean isCursorOn() {
return fShowCursor && fCursorIsEnabled;
return fShowCursor && fCursorIsEnabled && !fCursorHidden;
}

/** @return whether the program hid the cursor, see {@link #setCursorHidden(boolean)} */
public boolean isCursorHidden() {
return fCursorHidden;
}

/** A program hides the cursor while it draws, or for good, with DEC mode 25. */
public void setCursorHidden(boolean hidden) {
fCursorHidden = hidden;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Require-Bundle: org.eclipse.core.expressions;bundle-version="[3.9.0,4.0.0)",
org.eclipse.ui.editors;bundle-version="[3.20.0,4.0.0)";resolution:=optional,
org.eclipse.text;bundle-version="[3.14.0,4.0.0)";resolution:=optional,
org.eclipse.terminal.view.core;bundle-version="[1.0.0,2.0.0)",
org.eclipse.terminal.control;bundle-version="[1.0.0,2.0.0)"
org.eclipse.terminal.control;bundle-version="[1.2.0,2.0.0)"
Bundle-RequiredExecutionEnvironment: JavaSE-21
Bundle-ActivationPolicy: lazy
Bundle-Localization: plugin
Expand All @@ -36,7 +36,7 @@ Export-Package: org.eclipse.terminal.view.ui;version="1.0.100",
Automatic-Module-Name: org.eclipse.terminal.view.ui
Import-Package: org.eclipse.terminal.connector;version="[1.0.0,2.0.0)",
org.eclipse.terminal.connector.provider;version="[1.0.0,2.0.0)",
org.eclipse.terminal.control;version="[1.0.0,2.0.0)",
org.eclipse.terminal.control;version="[1.1.0,2.0.0)",
org.eclipse.terminal.model;version="[1.0.0,2.0.0)"
Service-Component: OSGI-INF/org.eclipse.terminal.view.ui.internal.ConsoleManager.xml,
OSGI-INF/org.eclipse.terminal.view.ui.internal.LauncherDelegateManager.xml,
Expand Down
Loading
Loading