52938
|
1 |
/*
|
|
2 |
* Copyright (c) 2002-2018, the original author or authors.
|
|
3 |
*
|
|
4 |
* This software is distributable under the BSD license. See the terms of the
|
|
5 |
* BSD license in the documentation provided with this software.
|
|
6 |
*
|
58903
|
7 |
* https://opensource.org/licenses/BSD-3-Clause
|
52938
|
8 |
*/
|
|
9 |
package jdk.internal.org.jline.keymap;
|
|
10 |
|
|
11 |
import java.io.IOError;
|
|
12 |
import java.io.IOException;
|
|
13 |
import java.util.ArrayDeque;
|
|
14 |
import java.util.Deque;
|
|
15 |
|
|
16 |
import jdk.internal.org.jline.reader.EndOfFileException;
|
|
17 |
import jdk.internal.org.jline.utils.ClosedException;
|
|
18 |
import jdk.internal.org.jline.utils.NonBlockingReader;
|
|
19 |
|
|
20 |
/**
|
|
21 |
* The BindingReader will transform incoming chars into
|
|
22 |
* key bindings
|
|
23 |
*
|
|
24 |
* @author <a href="mailto:gnodet@gmail.com">Guillaume Nodet</a>
|
|
25 |
*/
|
|
26 |
public class BindingReader {
|
|
27 |
|
|
28 |
protected final NonBlockingReader reader;
|
|
29 |
protected final StringBuilder opBuffer = new StringBuilder();
|
|
30 |
protected final Deque<Integer> pushBackChar = new ArrayDeque<>();
|
|
31 |
protected String lastBinding;
|
|
32 |
|
|
33 |
public BindingReader(NonBlockingReader reader) {
|
|
34 |
this.reader = reader;
|
|
35 |
}
|
|
36 |
|
|
37 |
/**
|
|
38 |
* Read from the input stream and decode an operation from the key map.
|
|
39 |
*
|
|
40 |
* The input stream will be read character by character until a matching
|
|
41 |
* binding can be found. Characters that can't possibly be matched to
|
|
42 |
* any binding will be send with the {@link KeyMap#getNomatch()} binding.
|
|
43 |
* Unicode (>= 128) characters will be matched to {@link KeyMap#getUnicode()}.
|
|
44 |
* If the current key sequence is ambiguous, i.e. the sequence is bound but
|
|
45 |
* it's also a prefix to other sequences, then the {@link KeyMap#getAmbiguousTimeout()}
|
|
46 |
* timeout will be used to wait for another incoming character.
|
|
47 |
* If a character comes, the disambiguation will be done. If the timeout elapses
|
|
48 |
* and no character came in, or if the timeout is <= 0, the current bound operation
|
|
49 |
* will be returned.
|
|
50 |
*
|
|
51 |
* @param keys the KeyMap to use for decoding the input stream
|
|
52 |
* @param <T> the type of bindings to be read
|
|
53 |
* @return the decoded binding or <code>null</code> if the end of
|
|
54 |
* stream has been reached
|
|
55 |
*/
|
|
56 |
public <T> T readBinding(KeyMap<T> keys) {
|
|
57 |
return readBinding(keys, null, true);
|
|
58 |
}
|
|
59 |
|
|
60 |
public <T> T readBinding(KeyMap<T> keys, KeyMap<T> local) {
|
|
61 |
return readBinding(keys, local, true);
|
|
62 |
}
|
|
63 |
|
|
64 |
public <T> T readBinding(KeyMap<T> keys, KeyMap<T> local, boolean block) {
|
|
65 |
lastBinding = null;
|
|
66 |
T o = null;
|
|
67 |
int[] remaining = new int[1];
|
|
68 |
boolean hasRead = false;
|
|
69 |
for (;;) {
|
|
70 |
if (local != null) {
|
|
71 |
o = local.getBound(opBuffer, remaining);
|
|
72 |
}
|
|
73 |
if (o == null && (local == null || remaining[0] >= 0)) {
|
|
74 |
o = keys.getBound(opBuffer, remaining);
|
|
75 |
}
|
|
76 |
// We have a binding and additional chars
|
|
77 |
if (o != null) {
|
|
78 |
if (remaining[0] >= 0) {
|
|
79 |
runMacro(opBuffer.substring(opBuffer.length() - remaining[0]));
|
|
80 |
opBuffer.setLength(opBuffer.length() - remaining[0]);
|
|
81 |
}
|
|
82 |
else {
|
|
83 |
long ambiguousTimeout = keys.getAmbiguousTimeout();
|
|
84 |
if (ambiguousTimeout > 0 && peekCharacter(ambiguousTimeout) != NonBlockingReader.READ_EXPIRED) {
|
|
85 |
o = null;
|
|
86 |
}
|
|
87 |
}
|
|
88 |
if (o != null) {
|
|
89 |
lastBinding = opBuffer.toString();
|
|
90 |
opBuffer.setLength(0);
|
|
91 |
return o;
|
|
92 |
}
|
|
93 |
// We don't match anything
|
|
94 |
} else if (remaining[0] > 0) {
|
|
95 |
int cp = opBuffer.codePointAt(0);
|
|
96 |
String rem = opBuffer.substring(Character.charCount(cp));
|
|
97 |
lastBinding = opBuffer.substring(0, Character.charCount(cp));
|
|
98 |
// Unicode character
|
|
99 |
o = (cp >= KeyMap.KEYMAP_LENGTH) ? keys.getUnicode() : keys.getNomatch();
|
|
100 |
opBuffer.setLength(0);
|
|
101 |
opBuffer.append(rem);
|
|
102 |
if (o != null) {
|
|
103 |
return o;
|
|
104 |
}
|
|
105 |
}
|
|
106 |
|
|
107 |
if (!block && hasRead) {
|
|
108 |
break;
|
|
109 |
}
|
|
110 |
int c = readCharacter();
|
|
111 |
if (c == -1) {
|
|
112 |
return null;
|
|
113 |
}
|
|
114 |
opBuffer.appendCodePoint(c);
|
|
115 |
hasRead = true;
|
|
116 |
}
|
|
117 |
return null;
|
|
118 |
}
|
|
119 |
|
|
120 |
/**
|
|
121 |
* Read a codepoint from the terminal.
|
|
122 |
*
|
|
123 |
* @return the character, or -1 if an EOF is received.
|
|
124 |
*/
|
|
125 |
public int readCharacter() {
|
|
126 |
if (!pushBackChar.isEmpty()) {
|
|
127 |
return pushBackChar.pop();
|
|
128 |
}
|
|
129 |
try {
|
|
130 |
int c = NonBlockingReader.READ_EXPIRED;
|
|
131 |
int s = 0;
|
|
132 |
while (c == NonBlockingReader.READ_EXPIRED) {
|
|
133 |
c = reader.read(100L);
|
|
134 |
if (c >= 0 && Character.isHighSurrogate((char) c)) {
|
|
135 |
s = c;
|
|
136 |
c = NonBlockingReader.READ_EXPIRED;
|
|
137 |
}
|
|
138 |
}
|
|
139 |
return s != 0 ? Character.toCodePoint((char) s, (char) c) : c;
|
|
140 |
} catch (ClosedException e) {
|
|
141 |
throw new EndOfFileException(e);
|
|
142 |
} catch (IOException e) {
|
|
143 |
throw new IOError(e);
|
|
144 |
}
|
|
145 |
}
|
|
146 |
|
|
147 |
public int peekCharacter(long timeout) {
|
|
148 |
if (!pushBackChar.isEmpty()) {
|
|
149 |
return pushBackChar.peek();
|
|
150 |
}
|
|
151 |
try {
|
|
152 |
return reader.peek(timeout);
|
|
153 |
} catch (IOException e) {
|
|
154 |
throw new IOError(e);
|
|
155 |
}
|
|
156 |
}
|
|
157 |
|
|
158 |
public void runMacro(String macro) {
|
|
159 |
macro.codePoints().forEachOrdered(pushBackChar::addLast);
|
|
160 |
}
|
|
161 |
|
|
162 |
public String getCurrentBuffer() {
|
|
163 |
return opBuffer.toString();
|
|
164 |
}
|
|
165 |
|
|
166 |
public String getLastBinding() {
|
|
167 |
return lastBinding;
|
|
168 |
}
|
|
169 |
|
|
170 |
}
|