Skip to content

Commit

Permalink
Support bracketing paste, fixes #142
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Jun 28, 2017
1 parent 2a7fa6e commit c722074
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
8 changes: 6 additions & 2 deletions reader/src/main/java/org/jline/reader/LineReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ public interface LineReader {
String YANK_POP = "yank-pop";
String MOUSE = "mouse";

String BEGIN_PASTE = "begin-paste";

//
// KeyMap names
//
Expand Down Expand Up @@ -362,7 +364,8 @@ enum Option {
AUTO_REMOVE_SLASH(true),
INSERT_TAB(true),
MOUSE,
DISABLE_HIGHLIGHTER;
DISABLE_HIGHLIGHTER,
BRACKETED_PASTE(true);

private final boolean def;

Expand All @@ -382,7 +385,8 @@ public boolean isDef() {
enum RegionType {
NONE,
CHAR,
LINE
LINE,
PASTE
}

/**
Expand Down
41 changes: 41 additions & 0 deletions reader/src/main/java/org/jline/reader/impl/LineReaderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ public class LineReaderImpl implements LineReader, Flushable

private static final int MIN_ROWS = 3;

public static final String BRACKETED_PASTE_ON = "\033[?2004h";
public static final String BRACKETED_PASTE_OFF = "\033[?2004l";
public static final String BRACKETED_PASTE_BEGIN = "\033[200~";
public static final String BRACKETED_PASTE_END = "\033[201~";

/**
* Possible states in which the current readline operation may be in.
*/
Expand Down Expand Up @@ -491,6 +496,8 @@ public String readLine(String prompt, String rightPrompt, Character mask, String
callWidget(FRESH_LINE);
if (isSet(Option.MOUSE))
terminal.trackMouse(Terminal.MouseTracking.Normal);
if (isSet(Option.BRACKETED_PASTE))
terminal.writer().write(BRACKETED_PASTE_ON);
} else {
// For dumb terminals, we need to make sure that CR are ignored
Attributes attr = new Attributes(originalAttributes);
Expand Down Expand Up @@ -530,6 +537,10 @@ public String readLine(String prompt, String rightPrompt, Character mask, String
count = ((repeatCount == 0) ? 1 : repeatCount) * mult;
// Reset undo/redo flag
isUndo = false;
// Reset region after a paste
if (regionActive == RegionType.PASTE) {
regionActive = RegionType.NONE;
}

// Get executable widget
Buffer copy = buf.copy();
Expand Down Expand Up @@ -2229,6 +2240,8 @@ protected void cleanup() {
println();
terminal.puts(Capability.keypad_local);
terminal.trackMouse(Terminal.MouseTracking.Off);
if (isSet(Option.BRACKETED_PASTE))
terminal.writer().write(BRACKETED_PASTE_OFF);
flush();
}
history.moveToEnd();
Expand Down Expand Up @@ -3285,6 +3298,7 @@ protected Map<String, Widget> builtinWidgets() {
widgets.put(YANK, this::yank);
widgets.put(YANK_POP, this::yankPop);
widgets.put(MOUSE, this::mouse);
widgets.put(BEGIN_PASTE, this::beginPaste);
return widgets;
}

Expand Down Expand Up @@ -4867,6 +4881,32 @@ public boolean mouse() {
return true;
}

public boolean beginPaste() {
final Object SELF_INSERT = new Object();
final Object END_PASTE = new Object();
KeyMap<Object> keyMap = new KeyMap<>();
keyMap.setUnicode(SELF_INSERT);
keyMap.setNomatch(SELF_INSERT);
keyMap.setAmbiguousTimeout(0);
keyMap.bind(END_PASTE, BRACKETED_PASTE_END);
StringBuilder sb = new StringBuilder();
while (true) {
Object b = bindingReader.readBinding(keyMap);
if (b == END_PASTE) {
break;
}
String s = getLastBinding();
if ("\r".equals(s)) {
s = "\n";
}
sb.append(s);
}
regionActive = RegionType.PASTE;
regionMark = getBuffer().cursor();
getBuffer().write(sb);
return true;
}

/**
* Clean the used display
*/
Expand Down Expand Up @@ -5297,6 +5337,7 @@ private void bindArrowKeys(KeyMap<Binding> map) {
bind(map, KILL_WHOLE_LINE, key(Capability.key_dl));
bind(map, OVERWRITE_MODE, key(Capability.key_ic));
bind(map, MOUSE, key(Capability.key_mouse));
bind(map, BEGIN_PASTE, BRACKETED_PASTE_BEGIN);
}

/**
Expand Down

0 comments on commit c722074

Please sign in to comment.