Skip to content

Commit 8380800

Browse files
jcubicRafaelGSS
authored andcommitted
readline: add paste bracket mode
The paste bracket mode allows REPL to have auto-indentation that is handled differently when the user copy-pastes the code from the clipboard and the code already has an indentation. PR-URL: #47150 Fixes: #45213 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Kohei Ueno <kohei.ueno119@gmail.com>
1 parent 2fcb855 commit 8380800

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

lib/internal/readline/utils.js

+18-4
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,10 @@ function* emitKeys(stream) {
148148
*
149149
* - `;5` part is optional, e.g. it could be `\x1b[24~`
150150
* - first part can contain one or two digits
151+
* - there is also special case when there can be 3 digits
152+
* but without modifier. They are the case of paste bracket mode
151153
*
152-
* So the generic regexp is like /^\d\d?(;\d)?[~^$]$/
154+
* So the generic regexp is like /^(?:\d\d?(;\d)?[~^$]|\d{3}~)$/
153155
*
154156
*
155157
* 2. `\x1b[1;5H` should be parsed as { code: '[H', modifier: 5 }
@@ -170,6 +172,10 @@ function* emitKeys(stream) {
170172

171173
if (ch >= '0' && ch <= '9') {
172174
s += (ch = yield);
175+
176+
if (ch >= '0' && ch <= '9') {
177+
s += (ch = yield);
178+
}
173179
}
174180
}
175181

@@ -189,9 +195,13 @@ function* emitKeys(stream) {
189195
const cmd = StringPrototypeSlice(s, cmdStart);
190196
let match;
191197

192-
if ((match = RegExpPrototypeExec(/^(\d\d?)(;(\d))?([~^$])$/, cmd))) {
193-
code += match[1] + match[4];
194-
modifier = (match[3] || 1) - 1;
198+
if ((match = RegExpPrototypeExec(/^(?:(\d\d?)(?:;(\d))?([~^$])|(\d{3}~))$/, cmd))) {
199+
if (match[4]) {
200+
code += match[4];
201+
} else {
202+
code += match[1] + match[3];
203+
modifier = (match[2] || 1) - 1;
204+
}
195205
} else if (
196206
(match = RegExpPrototypeExec(/^((\d;)?(\d))?([A-Za-z])$/, cmd))
197207
) {
@@ -228,6 +238,10 @@ function* emitKeys(stream) {
228238
case '[13~': key.name = 'f3'; break;
229239
case '[14~': key.name = 'f4'; break;
230240

241+
/* paste bracket mode */
242+
case '[200~': key.name = 'paste-start'; break;
243+
case '[201~': key.name = 'paste-end'; break;
244+
231245
/* from Cygwin and used in libuv */
232246
case '[[A': key.name = 'f1'; break;
233247
case '[[B': key.name = 'f2'; break;

0 commit comments

Comments
 (0)