Permalink
Browse files

Support pastes and meta-backspace

  • Loading branch information...
1 parent f9fea12 commit 430a7f1bed77ba1066d7b3999399ae3762226f6b @isaacs isaacs committed with ry Jan 25, 2011
Showing with 28 additions and 5 deletions.
  1. +13 −0 lib/readline.js
  2. +15 −5 lib/tty_posix.js
View
@@ -503,6 +503,7 @@ Interface.prototype._ttyWrite = function(s, key) {
break;
case 'd': // delete forward word
+ case 'delete':
if (this.cursor < this.line.length) {
next_word = this.line
.slice(this.cursor, this.line.length)
@@ -525,6 +526,18 @@ Interface.prototype._ttyWrite = function(s, key) {
this._refreshLine();
}
break;
+
+ case 'backspace': // delete backwards to a word boundary
+ if (this.cursor !== 0) {
+ var leading = this.line.slice(0, this.cursor);
+ var match = leading.match(/\s?((\W+|\w+)\s*)$/);
+ leading = leading.slice(0, leading.length - match[1].length);
+ this.line = leading + this.line.slice(this.cursor, this.line.length);
+ this.cursor = leading.length;
+ this._refreshLine();
+ }
+ break;
+
}
} else {
View
@@ -122,27 +122,31 @@ ReadStream.prototype._emitKey = function(s) {
// tab
key.name = 'tab';
- } else if (s === '\b' || s === '\x7f') {
+ } else if (s === '\b' || s === '\x7f' ||
+ s === '\x1b\x7f' || s === '\x1b\b') {
// backspace or ctrl+h
key.name = 'backspace';
+ key.meta = (s.charAt(0) === '\x1b');
- } else if (s === '\x1b') {
+ } else if (s === '\x1b' || s === '\x1b\x1b') {
// escape key
key.name = 'escape';
+ key.meta = (s.length === 2);
- } else if (s === ' ') {
+ } else if (s === ' ' || s === '\x1b ') {
key.name = 'space';
+ key.meta = (s.length === 2);
} else if (s <= '\x1a') {
// ctrl+letter
key.name = String.fromCharCode(s.charCodeAt(0) + 'a'.charCodeAt(0) - 1);
key.ctrl = true;
- } else if (s >= 'a' && s <= 'z') {
+ } else if (s.length === 1 && s >= 'a' && s <= 'z') {
// lowercase letter
key.name = s;
- } else if (s >= 'A' && s <= 'Z') {
+ } else if (s.length === 1 && s >= 'A' && s <= 'Z') {
// shift+letter
key.name = s.toLowerCase();
key.shift = true;
@@ -254,7 +258,13 @@ ReadStream.prototype._emitKey = function(s) {
/* misc. */
case '[Z': key.name = 'tab'; key.shift = true; break;
+
}
+ } else if (s.length > 1 && s[0] !== '\x1b') {
+ // Got a longer-than-one string of characters.
+ // Probably a paste, since it wasn't a control sequence.
+ Array.prototype.forEach.call(s, this._emitKey, this);
+ return;
}
// Don't emit a key if no name was found

0 comments on commit 430a7f1

Please sign in to comment.