]>
git.proxmox.com Git - mirror_xterm.js.git/blob - src/xterm.js
2 * xterm.js: xterm, in the browser
3 * Copyright (c) 2014, sourceLair Limited (www.sourcelair.com (MIT License)
4 * Copyright (c) 2012-2013, Christopher Jeffrey (MIT License)
5 * https://github.com/chjj/term.js
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * Originally forked from (with the author's permission):
26 * Fabrice Bellard's javascript vt100 for jslinux:
27 * http://bellard.org/jslinux/
28 * Copyright (c) 2011 Fabrice Bellard
29 * The original design remains. The terminal itself
30 * has been extended to include xterm CSI codes, among
35 if (typeof exports
=== 'object' && typeof module
=== 'object') {
37 * CommonJS environment
39 module
.exports
= xterm
.call(this);
40 } else if (typeof define
== 'function') {
42 * Require.js is available
44 define([], xterm
.bind(window
));
47 * Plain browser environment
49 this.Xterm
= xterm
.call(this);
50 this.Terminal
= this.Xterm
; /* Backwards compatibility with term.js */
54 * Terminal Emulation References:
56 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.txt
57 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
58 * http://invisible-island.net/vttest/
59 * http://www.inwap.com/pdp10/ansicode.txt
60 * http://linux.die.net/man/4/console_codes
61 * http://linux.die.net/man/7/urxvt
70 var window
= this, document
= this.document
;
76 function EventEmitter() {
77 this._events
= this._events
|| {};
80 EventEmitter
.prototype.addListener = function(type
, listener
) {
81 this._events
[type
] = this._events
[type
] || [];
82 this._events
[type
].push(listener
);
85 EventEmitter
.prototype.on
= EventEmitter
.prototype.addListener
;
87 EventEmitter
.prototype.removeListener = function(type
, listener
) {
88 if (!this._events
[type
]) return;
90 var obj
= this._events
[type
]
94 if (obj
[i
] === listener
|| obj
[i
].listener
=== listener
) {
101 EventEmitter
.prototype.off
= EventEmitter
.prototype.removeListener
;
103 EventEmitter
.prototype.removeAllListeners = function(type
) {
104 if (this._events
[type
]) delete this._events
[type
];
107 EventEmitter
.prototype.once = function(type
, listener
) {
110 var args
= Array
.prototype.slice
.call(arguments
);
111 self
.removeListener(type
, on
);
112 return listener
.apply(self
, args
);
114 on
.listener
= listener
;
115 return this.on(type
, on
);
118 EventEmitter
.prototype.emit = function(type
) {
119 if (!this._events
[type
]) return;
121 var args
= Array
.prototype.slice
.call(arguments
, 1)
122 , obj
= this._events
[type
]
127 obj
[i
].apply(this, args
);
131 EventEmitter
.prototype.listeners = function(type
) {
132 return this._events
[type
] = this._events
[type
] || [];
139 var normal
= 0, escaped
= 1, csi
= 2, osc
= 3, charset
= 4, dcs
= 5, ignore
= 6;
145 function Terminal(options
) {
148 if (!(this instanceof Terminal
)) {
149 return new Terminal(arguments
[0], arguments
[1], arguments
[2]);
152 self
.cancel
= Terminal
.cancel
;
154 EventEmitter
.call(this);
156 if (typeof options
=== 'number') {
160 handler
: arguments
[2]
164 options
= options
|| {};
167 Object
.keys(Terminal
.defaults
).forEach(function(key
) {
168 if (options
[key
] == null) {
169 options
[key
] = Terminal
.options
[key
];
171 if (Terminal
[key
] !== Terminal
.defaults
[key
]) {
172 options
[key
] = Terminal
[key
];
175 self
[key
] = options
[key
];
178 if (options
.colors
.length
=== 8) {
179 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(8));
180 } else if (options
.colors
.length
=== 16) {
181 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(16));
182 } else if (options
.colors
.length
=== 10) {
183 options
.colors
= options
.colors
.slice(0, -2).concat(
184 Terminal
._colors
.slice(8, -2), options
.colors
.slice(-2));
185 } else if (options
.colors
.length
=== 18) {
186 options
.colors
= options
.colors
.concat(
187 Terminal
._colors
.slice(16, -2), options
.colors
.slice(-2));
189 this.colors
= options
.colors
;
191 this.options
= options
;
193 // this.context = options.context || window;
194 // this.document = options.document || document;
195 this.parent
= options
.body
|| options
.parent
196 || (document
? document
.getElementsByTagName('body')[0] : null);
198 this.cols
= options
.cols
|| options
.geometry
[0];
199 this.rows
= options
.rows
|| options
.geometry
[1];
201 if (options
.handler
) {
202 this.on('data', options
.handler
);
209 this.cursorState
= 0;
210 this.cursorHidden
= false;
215 this.scrollBottom
= this.rows
- 1;
218 this.applicationKeypad
= false;
219 this.applicationCursor
= false;
220 this.originMode
= false;
221 this.insertMode
= false;
222 this.wraparoundMode
= false;
227 this.gcharset
= null;
229 this.charsets
= [null];
253 this.readable
= true;
254 this.writable
= true;
256 this.defAttr
= (0 << 18) | (257 << 9) | (256 << 0);
257 this.curAttr
= this.defAttr
;
260 this.currentParam
= 0;
267 this.lines
.push(this.blankLine());
274 inherits(Terminal
, EventEmitter
);
276 // back_color_erase feature for xterm.
277 Terminal
.prototype.eraseAttr = function() {
278 // if (this.is('screen')) return this.defAttr;
279 return (this.defAttr
& ~0x1ff) | (this.curAttr
& 0x1ff);
287 Terminal
.tangoColors
= [
308 // Colors 0-15 + 16-255
309 // Much thanks to TooTallNate for writing this.
310 Terminal
.colors
= (function() {
311 var colors
= Terminal
.tangoColors
.slice()
312 , r
= [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff]
317 for (; i
< 216; i
++) {
318 out(r
[(i
/ 36) % 6 | 0], r
[(i
/ 6) % 6 | 0], r
[i
% 6]);
323 for (; i
< 24; i
++) {
328 function out(r
, g
, b
) {
329 colors
.push('#' + hex(r
) + hex(g
) + hex(b
));
334 return c
.length
< 2 ? '0' + c
: c
;
340 Terminal
._colors
= Terminal
.colors
.slice();
342 Terminal
.vcolors
= (function() {
344 , colors
= Terminal
.colors
348 for (; i
< 256; i
++) {
349 color
= parseInt(colors
[i
].substring(1), 16);
351 (color
>> 16) & 0xff,
364 Terminal
.defaults
= {
365 colors
: Terminal
.colors
,
377 // programFeatures: false,
381 Terminal
.options
= {};
383 Terminal
.focus
= null;
385 each(keys(Terminal
.defaults
), function(key
) {
386 Terminal
[key
] = Terminal
.defaults
[key
];
387 Terminal
.options
[key
] = Terminal
.defaults
[key
];
393 Terminal
.prototype.focus = function() {
394 if (document
.activeElement
=== this.element
) {
398 if (this.sendFocus
) {
403 this.element
.focus();
406 Terminal
.prototype.blur = function() {
407 if (Terminal
.focus
!== this) {
411 this.cursorState
= 0;
412 this.refresh(this.y
, this.y
);
415 if (this.sendFocus
) {
418 Terminal
.focus
= null;
422 * Initialize default behavior
425 Terminal
.prototype.initGlobal = function() {
426 Terminal
.bindPaste(this);
427 Terminal
.bindKeys(this);
428 Terminal
.bindCopy(this);
429 Terminal
.bindDrop(this);
433 * Bind to paste event
435 Terminal
.bindPaste = function(term
) {
436 on(term
.element
, 'paste', function(ev
) {
437 if (ev
.clipboardData
) {
438 var text
= ev
.clipboardData
.getData('text/plain');
446 * Apply key handling to the terminal
448 Terminal
.bindKeys = function(term
) {
449 on(term
.element
, 'keydown', function(ev
) {
451 * Clear all selections if the key pressed was not among
452 * Shift, Alt, Cmd, Ctrl.
454 var selection
= window
.getSelection();
455 if (selection
.toString() != '') {
456 if ([16, 17, 18, 91].indexOf(ev
.keyCode
) == -1) {
457 selection
.removeAllRanges();
463 on(term
.element
, 'keypress', function(ev
) {
472 Terminal
.bindCopy = function(term
) {
473 on(term
.element
, 'copy', function(ev
) {
479 Terminal
.bindDrop = function (term
) {
481 * Do not perform the "drop" event. Altering the contents of the
482 * terminal with drag n drop is unwanted behavior.
484 on(term
.element
, 'drop', function (ev
) {
485 term
.cancel(ev
, true);
490 Terminal
.click = function (term
) {
492 * Do not perform the "drop" event. Altering the contents of the
493 * terminal with drag n drop is unwanted behavior.
495 on(term
.element
, 'click', function (ev
) {
496 term
.cancel(ev
, true);
502 * Insert the given row to the terminal or produce a new one
503 * if no row argument is passed. Return the inserted row.
505 Terminal
.prototype.insertRow = function (row
) {
506 if (typeof row
!= 'object') {
507 row
= document
.createElement('div');
510 this.rowContainer
.appendChild(row
);
511 this.children
.push(row
);
518 * Open Terminal in the DOM
520 Terminal
.prototype.open = function(parent
) {
521 var self
=this, i
=0, div
;
523 this.parent
= parent
|| this.parent
;
526 throw new Error('Terminal requires a parent element.');
530 * Grab global elements
532 this.context
= this.parent
.ownerDocument
.defaultView
;
533 this.document
= this.parent
.ownerDocument
;
534 this.body
= this.document
.getElementsByTagName('body')[0];
539 if (this.context
.navigator
&& this.context
.navigator
.userAgent
) {
540 this.isMac
= !!~this.context
.navigator
.userAgent
.indexOf('Mac');
541 this.isIpad
= !!~this.context
.navigator
.userAgent
.indexOf('iPad');
542 this.isIphone
= !!~this.context
.navigator
.userAgent
.indexOf('iPhone');
543 this.isMSIE
= !!~this.context
.navigator
.userAgent
.indexOf('MSIE');
547 * Create main element container
549 this.element
= this.document
.createElement('div');
550 this.element
.classList
.add('terminal');
551 this.element
.classList
.add('xterm');
552 this.element
.classList
.add('xterm-theme-' + this.theme
);
553 this.element
.setAttribute('tabindex', 0);
554 this.element
.spellcheck
= 'false';
557 * Create the container that will hold the lines of the terminal and then
558 * produce the lines the lines.
560 this.rowContainer
= document
.createElement('div');
561 this.rowContainer
.classList
.add('xterm-rows');
562 this.element
.appendChild(this.rowContainer
);
565 for (; i
< this.rows
; i
++) {
568 this.parent
.appendChild(this.element
);
571 this.refresh(0, this.rows
- 1);
573 // Initialize global actions that
574 // need to be taken on the document.
577 // Ensure there is a Terminal.focus.
580 // Start blinking the cursor.
583 on(this.element
, 'mouseup', function() {
584 var selection
= document
.getSelection(),
585 collapsed
= selection
.isCollapsed
,
586 isRange
= typeof collapsed
== 'boolean' ? !collapsed
: selection
.type
== 'Range';
592 // Listen for mouse events and translate
593 // them into terminal mouse protocols.
596 // Figure out whether boldness affects
597 // the character width of monospace fonts.
598 if (Terminal
.brokenBold
== null) {
599 Terminal
.brokenBold
= isBoldBroken(this.document
);
605 // XTerm mouse events
606 // http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#Mouse%20Tracking
607 // To better understand these
608 // the xterm code is very helpful:
610 // button.c, charproc.c, misc.c
611 // Relevant functions in xterm/button.c:
612 // BtnCode, EmitButtonCode, EditorButton, SendMousePosition
613 Terminal
.prototype.bindMouse = function() {
614 var el
= this.element
618 var wheelEvent
= 'onmousewheel' in this.context
622 // mouseup, mousedown, mousewheel
623 // left click: ^[[M 3<^[[M#3<
624 // mousewheel up: ^[[M`3>
625 function sendButton(ev
) {
629 // get the xterm-style button
630 button
= getButton(ev
);
632 // get mouse coordinates
636 sendEvent(button
, pos
);
643 // keep it at the left
644 // button, just in case.
655 // motion example of a left click:
656 // ^[[M 3<^[[M@4<^[[M@5<^[[M@6<^[[M@7<^[[M#7<
657 function sendMove(ev
) {
664 // buttons marked as motions
665 // are incremented by 32
668 sendEvent(button
, pos
);
672 // position to characters
673 function encode(data
, ch
) {
674 if (!self
.utfMouse
) {
675 if (ch
=== 255) return data
.push(0);
676 if (ch
> 127) ch
= 127;
679 if (ch
=== 2047) return data
.push(0);
683 if (ch
> 2047) ch
= 2047;
684 data
.push(0xC0 | (ch
>> 6));
685 data
.push(0x80 | (ch
& 0x3F));
690 // send a mouse event:
691 // regular/utf8: ^[[M Cb Cx Cy
692 // urxvt: ^[[ Cb ; Cx ; Cy M
693 // sgr: ^[[ Cb ; Cx ; Cy M/m
694 // vt300: ^[[ 24(1/3/5)~ [ Cx , Cy ] \r
695 // locator: CSI P e ; P b ; P r ; P c ; P p & w
696 function sendEvent(button
, pos
) {
697 // self.emit('mouse', {
703 if (self
.vt300Mouse
) {
705 // http://www.vt100.net/docs/vt3xx-gp/chapter15.html
709 var data
= '\x1b[24';
710 if (button
=== 0) data
+= '1';
711 else if (button
=== 1) data
+= '3';
712 else if (button
=== 2) data
+= '5';
713 else if (button
=== 3) return;
715 data
+= '~[' + pos
.x
+ ',' + pos
.y
+ ']\r';
720 if (self
.decLocator
) {
725 if (button
=== 0) button
= 2;
726 else if (button
=== 1) button
= 4;
727 else if (button
=== 2) button
= 6;
728 else if (button
=== 3) button
= 3;
732 + (button
=== 3 ? 4 : 0)
743 if (self
.urxvtMouse
) {
748 self
.send('\x1b[' + button
+ ';' + pos
.x
+ ';' + pos
.y
+ 'M');
756 + ((button
& 3) === 3 ? button
& ~3 : button
)
761 + ((button
& 3) === 3 ? 'm' : 'M'));
767 encode(data
, button
);
771 self
.send('\x1b[M' + String
.fromCharCode
.apply(String
, data
));
774 function getButton(ev
) {
787 // 1, and 2 - with 64 added
790 button
= ev
.button
!= null
797 button
= button
=== 1 ? 0 : button
=== 4 ? 1 : button
;
803 case 'DOMMouseScroll':
804 button
= ev
.detail
< 0
809 button
= ev
.wheelDeltaY
> 0
815 // next three bits are the modifiers:
816 // 4 = shift, 8 = meta, 16 = control
817 shift
= ev
.shiftKey
? 4 : 0;
818 meta
= ev
.metaKey
? 8 : 0;
819 ctrl
= ev
.ctrlKey
? 16 : 0;
820 mod
= shift
| meta
| ctrl
;
823 if (self
.vt200Mouse
) {
826 } else if (!self
.normalMouse
) {
831 button
= (32 + (mod
<< 2)) + button
;
836 // mouse coordinates measured in cols/rows
837 function getCoords(ev
) {
840 // ignore browsers without pageX for now
841 if (ev
.pageX
== null) return;
847 // should probably check offsetParent
848 // but this is more portable
849 while (el
&& el
!== self
.document
.documentElement
) {
852 el
= 'offsetParent' in el
857 // convert to cols/rows
858 w
= self
.element
.clientWidth
;
859 h
= self
.element
.clientHeight
;
860 x
= Math
.round((x
/ w
) * self
.cols
);
861 y
= Math
.round((y
/ h
) * self
.rows
);
863 // be sure to avoid sending
864 // bad positions to the program
866 if (x
> self
.cols
) x
= self
.cols
;
868 if (y
> self
.rows
) y
= self
.rows
;
870 // xterm sends raw bytes and
871 // starts at 32 (SP) for each.
878 type
: ev
.type
=== wheelEvent
884 on(el
, 'mousedown', function(ev
) {
885 if (!self
.mouseEvents
) return;
894 //if (self.vt200Mouse && !self.normalMouse) {
895 if (self
.vt200Mouse
) {
896 sendButton({ __proto__
: ev
, type
: 'mouseup' });
897 return self
.cancel(ev
);
901 if (self
.normalMouse
) on(self
.document
, 'mousemove', sendMove
);
903 // x10 compatibility mode can't send button releases
904 if (!self
.x10Mouse
) {
905 on(self
.document
, 'mouseup', function up(ev
) {
907 if (self
.normalMouse
) off(self
.document
, 'mousemove', sendMove
);
908 off(self
.document
, 'mouseup', up
);
909 return self
.cancel(ev
);
913 return self
.cancel(ev
);
916 //if (self.normalMouse) {
917 // on(self.document, 'mousemove', sendMove);
920 on(el
, wheelEvent
, function(ev
) {
921 if (!self
.mouseEvents
) return;
924 || self
.decLocator
) return;
926 return self
.cancel(ev
);
929 // allow mousewheel scrolling in
930 // the shell for example
931 on(el
, wheelEvent
, function(ev
) {
932 if (self
.mouseEvents
) return;
933 if (self
.applicationKeypad
) return;
934 if (ev
.type
=== 'DOMMouseScroll') {
935 self
.scrollDisp(ev
.detail
< 0 ? -1 : 1);
937 self
.scrollDisp(ev
.wheelDeltaY
> 0 ? -1 : 1);
939 return self
.cancel(ev
);
947 Terminal
.prototype.destroy = function() {
948 this.readable
= false;
949 this.writable
= false;
951 this.handler = function() {};
952 this.write = function() {};
953 if (this.element
.parentNode
) {
954 this.element
.parentNode
.removeChild(this.element
);
956 //this.emit('close');
961 * Flags used to render terminal text properly
974 * In the screen buffer, each character
975 * is stored as a an array with a character
976 * and a 32-bit integer.
977 * First value: a utf-16 character.
979 * Next 9 bits: background color (0-511).
980 * Next 9 bits: foreground color (0-511).
981 * Next 14 bits: a mask for misc. flags:
982 * 1=bold, 2=underline, 4=blink, 8=inverse, 16=invisible
984 Terminal
.prototype.refresh = function(start
, end
) {
985 var x
, y
, i
, line
, out
, ch
, width
, data
, attr
, bg
, fg
, flags
, row
, parent
, focused
= document
.activeElement
;
987 if (end
- start
>= this.rows
/ 2) {
988 parent
= this.element
.parentNode
;
989 if (parent
) parent
.removeChild(this.element
);
995 if (end
>= this.lines
.length
) {
996 this.log('`end` is too large. Most likely a bad CSR.');
997 end
= this.lines
.length
- 1;
1000 for (; y
<= end
; y
++) {
1001 row
= y
+ this.ydisp
;
1003 line
= this.lines
[row
];
1008 && (this.ydisp
=== this.ybase
)
1009 && !this.cursorHidden
) {
1015 attr
= this.defAttr
;
1018 for (; i
< width
; i
++) {
1022 if (i
=== x
) data
= -1;
1024 if (data
!== attr
) {
1025 if (attr
!== this.defAttr
) {
1028 if (data
!== this.defAttr
) {
1030 out
+= '<span class="reverse-video terminal-cursor">';
1032 out
+= '<span class="';
1035 fg
= (data
>> 9) & 0x1ff;
1038 if (flags
& Terminal
.flags
.BOLD
) {
1039 if (!Terminal
.brokenBold
) {
1040 out
+= ' xterm-bold ';
1042 // See: XTerm*boldColors
1043 if (fg
< 8) fg
+= 8;
1046 if (flags
& Terminal
.flags
.UNDERLINE
) {
1047 out
+= ' xterm-underline ';
1050 if (flags
& Terminal
.flags
.BLINK
) {
1051 out
+= ' xterm-blink ';
1055 * If inverse flag is on, then swap the foreground and background variables.
1057 if (flags
& Terminal
.flags
.INVERSE
) {
1058 /* One-line variable swap in JavaScript: http://stackoverflow.com/a/16201730 */
1059 bg = [fg, fg = bg][0];
1060 // Should inverse just be before the
1061 // above boldColors effect instead?
1062 if ((flags & 1) && fg < 8) fg += 8;
1065 if (flags & Terminal.flags.INVISIBLE) {
1066 out += ' xterm-hidden ';
1070 * Weird situation: Invert flag used black foreground and white background results
1071 * in invalid background color, positioned at the 256 index of the 256 terminal
1072 * color map. Pin the colors manually in such a case.
1074 * Source: https://github.com/sourcelair/xterm.js/issues/57
1076 if (flags
& Terminal
.flags
.INVERSE
) {
1086 out
+= ' xterm-bg-color-' + bg
+ ' ';
1090 out
+= ' xterm-color-' + fg
+ ' ';
1112 if (isWide(ch
)) i
++;
1121 if (attr
!== this.defAttr
) {
1125 this.children
[y
].innerHTML
= out
;
1129 parent
.appendChild(this.element
);
1133 * Return focus to previously focused element
1136 this.emit('refresh', {element
: this.element
, start
: start
, end
: end
});
1139 Terminal
.prototype._cursorBlink = function() {
1140 if (Terminal
.focus
!== this) return;
1141 this.cursorState
^= 1;
1142 this.refresh(this.y
, this.y
);
1145 Terminal
.prototype.showCursor = function() {
1146 if (!this.cursorState
) {
1147 this.cursorState
= 1;
1148 this.refresh(this.y
, this.y
);
1150 // Temporarily disabled:
1151 // this.refreshBlink();
1155 Terminal
.prototype.startBlink = function() {
1156 if (!this.cursorBlink
) return;
1158 this._blinker = function() {
1159 self
._cursorBlink();
1161 this._blink
= setInterval(this._blinker
, 500);
1164 Terminal
.prototype.refreshBlink = function() {
1165 if (!this.cursorBlink
) return;
1166 clearInterval(this._blink
);
1167 this._blink
= setInterval(this._blinker
, 500);
1170 Terminal
.prototype.scroll = function() {
1173 if (++this.ybase
=== this.scrollback
) {
1174 this.ybase
= this.ybase
/ 2 | 0;
1175 this.lines
= this.lines
.slice(-(this.ybase
+ this.rows
) + 1);
1178 this.ydisp
= this.ybase
;
1181 row
= this.ybase
+ this.rows
- 1;
1183 // subtract the bottom scroll region
1184 row
-= this.rows
- 1 - this.scrollBottom
;
1186 if (row
=== this.lines
.length
) {
1187 // potential optimization:
1188 // pushing is faster than splicing
1189 // when they amount to the same
1191 this.lines
.push(this.blankLine());
1194 this.lines
.splice(row
, 0, this.blankLine());
1197 if (this.scrollTop
!== 0) {
1198 if (this.ybase
!== 0) {
1200 this.ydisp
= this.ybase
;
1202 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
1206 this.updateRange(this.scrollTop
);
1207 this.updateRange(this.scrollBottom
);
1210 Terminal
.prototype.scrollDisp = function(disp
) {
1213 if (this.ydisp
> this.ybase
) {
1214 this.ydisp
= this.ybase
;
1215 } else if (this.ydisp
< 0) {
1219 this.refresh(0, this.rows
- 1);
1222 Terminal
.prototype.write = function(data
) {
1223 var l
= data
.length
, i
= 0, j
, cs
, ch
;
1225 this.refreshStart
= this.y
;
1226 this.refreshEnd
= this.y
;
1228 if (this.ybase
!== this.ydisp
) {
1229 this.ydisp
= this.ybase
;
1233 for (; i
< l
; i
++) {
1235 switch (this.state
) {
1246 if (this.convertEol
) {
1250 if (this.y
> this.scrollBottom
) {
1270 this.x
= this.nextStop();
1285 this.state
= escaped
;
1291 if (this.charset
&& this.charset
[ch
]) {
1292 ch
= this.charset
[ch
];
1295 if (this.x
>= this.cols
) {
1298 if (this.y
> this.scrollBottom
) {
1304 this.lines
[this.y
+ this.ybase
][this.x
] = [this.curAttr
, ch
];
1306 this.updateRange(this.y
);
1309 j
= this.y
+ this.ybase
;
1310 if (this.cols
< 2 || this.x
>= this.cols
) {
1311 this.lines
[j
][this.x
- 1] = [this.curAttr
, ' '];
1314 this.lines
[j
][this.x
] = [this.curAttr
, ' '];
1323 // ESC [ Control Sequence Introducer ( CSI is 0x9b).
1326 this.currentParam
= 0;
1330 // ESC ] Operating System Command ( OSC is 0x9d).
1333 this.currentParam
= 0;
1337 // ESC P Device Control String ( DCS is 0x90).
1340 this.currentParam
= 0;
1344 // ESC _ Application Program Command ( APC is 0x9f).
1346 this.state
= ignore
;
1349 // ESC ^ Privacy Message ( PM is 0x9e).
1351 this.state
= ignore
;
1354 // ESC c Full Reset (RIS).
1359 // ESC E Next Line ( NEL is 0x85).
1360 // ESC D Index ( IND is 0x84).
1368 // ESC M Reverse Index ( RI is 0x8d).
1370 this.reverseIndex();
1373 // ESC % Select default/utf-8 character set.
1374 // @ = default, G = utf-8
1376 //this.charset = null;
1378 this.setgCharset(0, Terminal
.charsets
.US
);
1379 this.state
= normal
;
1383 // ESC (,),*,+,-,. Designate G0-G2 Character Set.
1384 case '(': // <-- this seems to get all the attention
1410 this.state
= charset
;
1413 // Designate G3 Character Set (VT300).
1414 // A = ISO Latin-1 Supplemental.
1418 this.state
= charset
;
1423 // Single Shift Select of G2 Character Set
1424 // ( SS2 is 0x8e). This affects next character only.
1428 // Single Shift Select of G3 Character Set
1429 // ( SS3 is 0x8f). This affects next character only.
1433 // Invoke the G2 Character Set as GL (LS2).
1438 // Invoke the G3 Character Set as GL (LS3).
1443 // Invoke the G3 Character Set as GR (LS3R).
1448 // Invoke the G2 Character Set as GR (LS2R).
1453 // Invoke the G1 Character Set as GR (LS1R).
1458 // ESC 7 Save Cursor (DECSC).
1461 this.state
= normal
;
1464 // ESC 8 Restore Cursor (DECRC).
1466 this.restoreCursor();
1467 this.state
= normal
;
1470 // ESC # 3 DEC line height/width
1472 this.state
= normal
;
1476 // ESC H Tab Set (HTS is 0x88).
1481 // ESC = Application Keypad (DECPAM).
1483 this.log('Serial port requested application keypad.');
1484 this.applicationKeypad
= true;
1485 this.state
= normal
;
1488 // ESC > Normal Keypad (DECPNM).
1490 this.log('Switching back to normal keypad.');
1491 this.applicationKeypad
= false;
1492 this.state
= normal
;
1496 this.state
= normal
;
1497 this.error('Unknown ESC control: %s.', ch
);
1504 case '0': // DEC Special Character and Line Drawing Set.
1505 cs
= Terminal
.charsets
.SCLD
;
1508 cs
= Terminal
.charsets
.UK
;
1510 case 'B': // United States (USASCII).
1511 cs
= Terminal
.charsets
.US
;
1514 cs
= Terminal
.charsets
.Dutch
;
1516 case 'C': // Finnish
1518 cs
= Terminal
.charsets
.Finnish
;
1521 cs
= Terminal
.charsets
.French
;
1523 case 'Q': // FrenchCanadian
1524 cs
= Terminal
.charsets
.FrenchCanadian
;
1527 cs
= Terminal
.charsets
.German
;
1529 case 'Y': // Italian
1530 cs
= Terminal
.charsets
.Italian
;
1532 case 'E': // NorwegianDanish
1534 cs
= Terminal
.charsets
.NorwegianDanish
;
1536 case 'Z': // Spanish
1537 cs
= Terminal
.charsets
.Spanish
;
1539 case 'H': // Swedish
1541 cs
= Terminal
.charsets
.Swedish
;
1544 cs
= Terminal
.charsets
.Swiss
;
1546 case '/': // ISOLatin (actually /A)
1547 cs
= Terminal
.charsets
.ISOLatin
;
1551 cs
= Terminal
.charsets
.US
;
1554 this.setgCharset(this.gcharset
, cs
);
1555 this.gcharset
= null;
1556 this.state
= normal
;
1562 // Set Text Parameters.
1563 if (ch
=== '\x1b' || ch
=== '\x07') {
1564 if (ch
=== '\x1b') i
++;
1566 this.params
.push(this.currentParam
);
1568 switch (this.params
[0]) {
1572 if (this.params
[1]) {
1573 this.title
= this.params
[1];
1574 this.handleTitle(this.title
);
1582 // change dynamic colors
1594 // change dynamic ui colors
1606 // manipulate selection data
1624 this.currentParam
= 0;
1625 this.state
= normal
;
1627 if (!this.params
.length
) {
1628 if (ch
>= '0' && ch
<= '9') {
1630 this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1631 } else if (ch
=== ';') {
1632 this.params
.push(this.currentParam
);
1633 this.currentParam
= '';
1636 this.currentParam
+= ch
;
1643 if (ch
=== '?' || ch
=== '>' || ch
=== '!') {
1649 if (ch
>= '0' && ch
<= '9') {
1650 this.currentParam
= this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1654 // '$', '"', ' ', '\''
1655 if (ch
=== '$' || ch
=== '"' || ch
=== ' ' || ch
=== '\'') {
1660 this.params
.push(this.currentParam
);
1661 this.currentParam
= 0;
1664 if (ch
=== ';') break;
1666 this.state
= normal
;
1670 // Cursor Up Ps Times (default = 1) (CUU).
1672 this.cursorUp(this.params
);
1676 // Cursor Down Ps Times (default = 1) (CUD).
1678 this.cursorDown(this.params
);
1682 // Cursor Forward Ps Times (default = 1) (CUF).
1684 this.cursorForward(this.params
);
1688 // Cursor Backward Ps Times (default = 1) (CUB).
1690 this.cursorBackward(this.params
);
1694 // Cursor Position [row;column] (default = [1,1]) (CUP).
1696 this.cursorPos(this.params
);
1699 // CSI Ps J Erase in Display (ED).
1701 this.eraseInDisplay(this.params
);
1704 // CSI Ps K Erase in Line (EL).
1706 this.eraseInLine(this.params
);
1709 // CSI Pm m Character Attributes (SGR).
1712 this.charAttributes(this.params
);
1716 // CSI Ps n Device Status Report (DSR).
1719 this.deviceStatus(this.params
);
1728 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
1730 this.insertChars(this.params
);
1734 // Cursor Next Line Ps Times (default = 1) (CNL).
1736 this.cursorNextLine(this.params
);
1740 // Cursor Preceding Line Ps Times (default = 1) (CNL).
1742 this.cursorPrecedingLine(this.params
);
1746 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
1748 this.cursorCharAbsolute(this.params
);
1752 // Insert Ps Line(s) (default = 1) (IL).
1754 this.insertLines(this.params
);
1758 // Delete Ps Line(s) (default = 1) (DL).
1760 this.deleteLines(this.params
);
1764 // Delete Ps Character(s) (default = 1) (DCH).
1766 this.deleteChars(this.params
);
1770 // Erase Ps Character(s) (default = 1) (ECH).
1772 this.eraseChars(this.params
);
1775 // CSI Pm ` Character Position Absolute
1776 // [column] (default = [row,1]) (HPA).
1778 this.charPosAbsolute(this.params
);
1782 // Horizontal Position Relative
1784 this.HPositionRelative(this.params
);
1788 // Send Device Attributes (Primary DA).
1790 // Send Device Attributes (Secondary DA)
1792 this.sendDeviceAttributes(this.params
);
1796 // Line Position Absolute [row] (default = [1,column]) (VPA).
1798 this.linePosAbsolute(this.params
);
1801 // 145 65 e * VPR - Vertical Position Relative
1803 this.VPositionRelative(this.params
);
1807 // Horizontal and Vertical Position [row;column] (default =
1810 this.HVPosition(this.params
);
1813 // CSI Pm h Set Mode (SM).
1814 // CSI ? Pm h - mouse escape codes, cursor escape codes
1816 this.setMode(this.params
);
1819 // CSI Pm l Reset Mode (RM).
1822 this.resetMode(this.params
);
1826 // Set Scrolling Region [top;bottom] (default = full size of win-
1830 this.setScrollRegion(this.params
);
1834 // Save cursor (ANSI.SYS).
1836 this.saveCursor(this.params
);
1840 // Restore cursor (ANSI.SYS).
1842 this.restoreCursor(this.params
);
1850 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
1852 this.cursorForwardTab(this.params
);
1855 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
1857 this.scrollUp(this.params
);
1860 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
1861 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
1864 // if (this.prefix === '>') {
1865 // this.resetTitleModes(this.params);
1868 // if (this.params.length > 2) {
1869 // this.initMouseTracking(this.params);
1872 if (this.params
.length
< 2 && !this.prefix
) {
1873 this.scrollDown(this.params
);
1878 // Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
1880 this.cursorBackwardTab(this.params
);
1883 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
1885 this.repeatPrecedingCharacter(this.params
);
1888 // CSI Ps g Tab Clear (TBC).
1890 this.tabClear(this.params
);
1893 // CSI Pm i Media Copy (MC).
1896 // this.mediaCopy(this.params);
1899 // CSI Pm m Character Attributes (SGR).
1901 // case 'm': // duplicate
1902 // if (this.prefix === '>') {
1903 // this.setResources(this.params);
1905 // this.charAttributes(this.params);
1909 // CSI Ps n Device Status Report (DSR).
1911 // case 'n': // duplicate
1912 // if (this.prefix === '>') {
1913 // this.disableModifiers(this.params);
1915 // this.deviceStatus(this.params);
1919 // CSI > Ps p Set pointer mode.
1920 // CSI ! p Soft terminal reset (DECSTR).
1922 // Request ANSI mode (DECRQM).
1924 // Request DEC private mode (DECRQM).
1927 switch (this.prefix
) {
1929 // this.setPointerMode(this.params);
1932 this.softReset(this.params
);
1935 // if (this.postfix === '$') {
1936 // this.requestPrivateMode(this.params);
1940 // if (this.postfix === '"') {
1941 // this.setConformanceLevel(this.params);
1942 // } else if (this.postfix === '$') {
1943 // this.requestAnsiMode(this.params);
1949 // CSI Ps q Load LEDs (DECLL).
1953 // if (this.postfix === ' ') {
1954 // this.setCursorStyle(this.params);
1957 // if (this.postfix === '"') {
1958 // this.setCharProtectionAttr(this.params);
1961 // this.loadLEDs(this.params);
1965 // Set Scrolling Region [top;bottom] (default = full size of win-
1968 // CSI Pt; Pl; Pb; Pr; Ps$ r
1969 // case 'r': // duplicate
1970 // if (this.prefix === '?') {
1971 // this.restorePrivateValues(this.params);
1972 // } else if (this.postfix === '$') {
1973 // this.setAttrInRectangle(this.params);
1975 // this.setScrollRegion(this.params);
1979 // CSI s Save cursor (ANSI.SYS).
1981 // case 's': // duplicate
1982 // if (this.prefix === '?') {
1983 // this.savePrivateValues(this.params);
1985 // this.saveCursor(this.params);
1989 // CSI Ps ; Ps ; Ps t
1990 // CSI Pt; Pl; Pb; Pr; Ps$ t
1994 // if (this.postfix === '$') {
1995 // this.reverseAttrInRectangle(this.params);
1996 // } else if (this.postfix === ' ') {
1997 // this.setWarningBellVolume(this.params);
1999 // if (this.prefix === '>') {
2000 // this.setTitleModeFeature(this.params);
2002 // this.manipulateWindow(this.params);
2007 // CSI u Restore cursor (ANSI.SYS).
2009 // case 'u': // duplicate
2010 // if (this.postfix === ' ') {
2011 // this.setMarginBellVolume(this.params);
2013 // this.restoreCursor(this.params);
2017 // CSI Pt; Pl; Pb; Pr; Pp; Pt; Pl; Pp$ v
2019 // if (this.postfix === '$') {
2020 // this.copyRectagle(this.params);
2024 // CSI Pt ; Pl ; Pb ; Pr ' w
2026 // if (this.postfix === '\'') {
2027 // this.enableFilterRectangle(this.params);
2031 // CSI Ps x Request Terminal Parameters (DECREQTPARM).
2032 // CSI Ps x Select Attribute Change Extent (DECSACE).
2033 // CSI Pc; Pt; Pl; Pb; Pr$ x
2035 // if (this.postfix === '$') {
2036 // this.fillRectangle(this.params);
2038 // this.requestParameters(this.params);
2039 // //this.__(this.params);
2044 // CSI Pt; Pl; Pb; Pr$ z
2046 // if (this.postfix === '\'') {
2047 // this.enableLocatorReporting(this.params);
2048 // } else if (this.postfix === '$') {
2049 // this.eraseRectangle(this.params);
2054 // CSI Pt; Pl; Pb; Pr$ {
2056 // if (this.postfix === '\'') {
2057 // this.setLocatorEvents(this.params);
2058 // } else if (this.postfix === '$') {
2059 // this.selectiveEraseRectangle(this.params);
2065 // if (this.postfix === '\'') {
2066 // this.requestLocatorPosition(this.params);
2071 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
2073 // if (this.postfix === ' ') {
2074 // this.insertColumns(this.params);
2079 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
2081 // if (this.postfix === ' ') {
2082 // this.deleteColumns(this.params);
2087 this.error('Unknown CSI code: %s.', ch
);
2096 if (ch
=== '\x1b' || ch
=== '\x07') {
2097 if (ch
=== '\x1b') i
++;
2099 switch (this.prefix
) {
2100 // User-Defined Keys (DECUDK).
2104 // Request Status String (DECRQSS).
2105 // test: echo -e '\eP$q"p\e\\'
2107 var pt
= this.currentParam
2124 + (this.scrollTop
+ 1)
2126 + (this.scrollBottom
+ 1)
2136 this.error('Unknown DCS Pt: %s.', pt
);
2141 this.send('\x1bP' + +valid
+ '$r' + pt
+ '\x1b\\');
2144 // Set Termcap/Terminfo Data (xterm, experimental).
2148 // Request Termcap/Terminfo String (xterm, experimental)
2149 // Regular xterm does not even respond to this sequence.
2150 // This can cause a small glitch in vim.
2151 // test: echo -ne '\eP+q6b64\e\\'
2153 var pt
= this.currentParam
2156 this.send('\x1bP' + +valid
+ '+r' + pt
+ '\x1b\\');
2160 this.error('Unknown DCS prefix: %s.', this.prefix
);
2164 this.currentParam
= 0;
2166 this.state
= normal
;
2167 } else if (!this.currentParam
) {
2168 if (!this.prefix
&& ch
!== '$' && ch
!== '+') {
2169 this.currentParam
= ch
;
2170 } else if (this.prefix
.length
=== 2) {
2171 this.currentParam
= ch
;
2176 this.currentParam
+= ch
;
2182 if (ch
=== '\x1b' || ch
=== '\x07') {
2183 if (ch
=== '\x1b') i
++;
2184 this.state
= normal
;
2190 this.updateRange(this.y
);
2191 this.refresh(this.refreshStart
, this.refreshEnd
);
2194 Terminal
.prototype.writeln = function(data
) {
2195 this.write(data
+ '\r\n');
2199 // https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
2200 Terminal
.prototype.keyDown = function(ev
) {
2201 var self
= this, key
;
2203 switch (ev
.keyCode
) {
2219 this.cancel(ev
, true);
2224 this.cancel(ev
, true);
2229 this.cancel(ev
, true);
2234 this.cancel(ev
, true);
2235 key
= '\x1bb' // Jump a word back
2237 } else if (this.applicationCursor
) {
2238 key
= '\x1bOD'; // SS3 as ^[O for 7-bit
2246 this.cancel(ev
, true);
2247 key
= '\x1bf' // Jump a word forward
2249 } else if (this.applicationCursor
) {
2257 if (this.applicationCursor
) {
2262 this.scrollDisp(-1);
2263 return this.cancel(ev
);
2270 if (this.applicationCursor
) {
2276 return this.cancel(ev
);
2291 if (this.applicationKeypad
) {
2299 if (this.applicationKeypad
) {
2308 this.scrollDisp(-(this.rows
- 1));
2309 return this.cancel(ev
);
2317 this.scrollDisp(this.rows
- 1);
2318 return this.cancel(ev
);
2373 if (ev
.ctrlKey
&& !ev
.shiftKey
&& !ev
.altKey
&& !ev
.metaKey
) {
2374 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2375 key
= String
.fromCharCode(ev
.keyCode
- 64);
2376 } else if (ev
.keyCode
=== 32) {
2378 key
= String
.fromCharCode(0);
2379 } else if (ev
.keyCode
>= 51 && ev
.keyCode
<= 55) {
2380 // escape, file sep, group sep, record sep, unit sep
2381 key
= String
.fromCharCode(ev
.keyCode
- 51 + 27);
2382 } else if (ev
.keyCode
=== 56) {
2384 key
= String
.fromCharCode(127);
2385 } else if (ev
.keyCode
=== 219) {
2387 key
= String
.fromCharCode(27);
2388 } else if (ev
.keyCode
=== 221) {
2390 key
= String
.fromCharCode(29);
2392 } else if ((!this.isMac
&& ev
.altKey
) || (this.isMac
&& ev
.metaKey
)) {
2393 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2394 key
= '\x1b' + String
.fromCharCode(ev
.keyCode
+ 32);
2395 } else if (ev
.keyCode
=== 192) {
2397 } else if (ev
.keyCode
>= 48 && ev
.keyCode
<= 57) {
2398 key
= '\x1b' + (ev
.keyCode
- 48);
2404 if (!key
|| (this.isMac
&& ev
.metaKey
)) {
2408 this.emit('keydown', ev
);
2409 this.emit('key', key
, ev
);
2413 return this.cancel(ev
, true);
2416 Terminal
.prototype.setgLevel = function(g
) {
2418 this.charset
= this.charsets
[g
];
2421 Terminal
.prototype.setgCharset = function(g
, charset
) {
2422 this.charsets
[g
] = charset
;
2423 if (this.glevel
=== g
) {
2424 this.charset
= charset
;
2428 Terminal
.prototype.keyPress = function(ev
) {
2433 } else if (ev
.which
== null) {
2435 } else if (ev
.which
!== 0 && ev
.charCode
!== 0) {
2441 if (!key
|| ev
.ctrlKey
|| ev
.altKey
|| ev
.metaKey
) {
2445 key
= String
.fromCharCode(key
);
2447 this.emit('keypress', key
, ev
);
2448 this.emit('key', key
, ev
);
2452 this.cancel(ev
, true);
2457 Terminal
.prototype.send = function(data
) {
2461 setTimeout(function() {
2462 self
.handler(self
.queue
);
2470 Terminal
.prototype.bell = function() {
2471 if (!this.visualBell
) return;
2473 this.element
.style
.borderColor
= 'white';
2474 setTimeout(function() {
2475 self
.element
.style
.borderColor
= '';
2477 if (this.popOnBell
) this.focus();
2480 Terminal
.prototype.log = function() {
2481 if (!this.debug
) return;
2482 if (!this.context
.console
|| !this.context
.console
.log
) return;
2483 var args
= Array
.prototype.slice
.call(arguments
);
2484 this.context
.console
.log
.apply(this.context
.console
, args
);
2487 Terminal
.prototype.error = function() {
2488 if (!this.debug
) return;
2489 if (!this.context
.console
|| !this.context
.console
.error
) return;
2490 var args
= Array
.prototype.slice
.call(arguments
);
2491 this.context
.console
.error
.apply(this.context
.console
, args
);
2494 Terminal
.prototype.resize = function(x
, y
) {
2507 ch
= [this.defAttr
, ' ']; // does xterm use the default attr?
2508 i
= this.lines
.length
;
2510 while (this.lines
[i
].length
< x
) {
2511 this.lines
[i
].push(ch
);
2515 i
= this.lines
.length
;
2517 while (this.lines
[i
].length
> x
) {
2518 this.lines
[i
].pop();
2530 if (this.lines
.length
< y
+ this.ybase
) {
2531 this.lines
.push(this.blankLine());
2533 if (this.children
.length
< y
) {
2539 if (this.lines
.length
> y
+ this.ybase
) {
2542 if (this.children
.length
> y
) {
2543 el
= this.children
.shift();
2545 el
.parentNode
.removeChild(el
);
2552 * Make sure that the cursor stays on screen
2563 this.scrollBottom
= y
- 1;
2565 this.refresh(0, this.rows
- 1);
2569 this.emit('resize', {terminal
: this, cols
: x
, rows
: y
});
2572 Terminal
.prototype.updateRange = function(y
) {
2573 if (y
< this.refreshStart
) this.refreshStart
= y
;
2574 if (y
> this.refreshEnd
) this.refreshEnd
= y
;
2575 // if (y > this.refreshEnd) {
2576 // this.refreshEnd = y;
2577 // if (y > this.rows - 1) {
2578 // this.refreshEnd = this.rows - 1;
2583 Terminal
.prototype.maxRange = function() {
2584 this.refreshStart
= 0;
2585 this.refreshEnd
= this.rows
- 1;
2588 Terminal
.prototype.setupStops = function(i
) {
2590 if (!this.tabs
[i
]) {
2591 i
= this.prevStop(i
);
2598 for (; i
< this.cols
; i
+= 8) {
2599 this.tabs
[i
] = true;
2603 Terminal
.prototype.prevStop = function(x
) {
2604 if (x
== null) x
= this.x
;
2605 while (!this.tabs
[--x
] && x
> 0);
2606 return x
>= this.cols
2611 Terminal
.prototype.nextStop = function(x
) {
2612 if (x
== null) x
= this.x
;
2613 while (!this.tabs
[++x
] && x
< this.cols
);
2614 return x
>= this.cols
2619 Terminal
.prototype.eraseRight = function(x
, y
) {
2620 var line
= this.lines
[this.ybase
+ y
]
2621 , ch
= [this.eraseAttr(), ' ']; // xterm
2624 for (; x
< this.cols
; x
++) {
2628 this.updateRange(y
);
2631 Terminal
.prototype.eraseLeft = function(x
, y
) {
2632 var line
= this.lines
[this.ybase
+ y
]
2633 , ch
= [this.eraseAttr(), ' ']; // xterm
2636 while (x
--) line
[x
] = ch
;
2638 this.updateRange(y
);
2641 Terminal
.prototype.eraseLine = function(y
) {
2642 this.eraseRight(0, y
);
2645 Terminal
.prototype.blankLine = function(cur
) {
2650 var ch
= [attr
, ' ']
2654 for (; i
< this.cols
; i
++) {
2661 Terminal
.prototype.ch = function(cur
) {
2663 ? [this.eraseAttr(), ' ']
2664 : [this.defAttr
, ' '];
2667 Terminal
.prototype.is = function(term
) {
2668 var name
= this.termName
;
2669 return (name
+ '').indexOf(term
) === 0;
2672 Terminal
.prototype.handler = function(data
) {
2673 this.emit('data', data
);
2676 Terminal
.prototype.handleTitle = function(title
) {
2677 this.emit('title', title
);
2684 // ESC D Index (IND is 0x84).
2685 Terminal
.prototype.index = function() {
2687 if (this.y
> this.scrollBottom
) {
2691 this.state
= normal
;
2694 // ESC M Reverse Index (RI is 0x8d).
2695 Terminal
.prototype.reverseIndex = function() {
2698 if (this.y
< this.scrollTop
) {
2700 // possibly move the code below to term.reverseScroll();
2701 // test: echo -ne '\e[1;1H\e[44m\eM\e[0m'
2702 // blankLine(true) is xterm/linux behavior
2703 this.lines
.splice(this.y
+ this.ybase
, 0, this.blankLine(true));
2704 j
= this.rows
- 1 - this.scrollBottom
;
2705 this.lines
.splice(this.rows
- 1 + this.ybase
- j
+ 1, 1);
2707 this.updateRange(this.scrollTop
);
2708 this.updateRange(this.scrollBottom
);
2710 this.state
= normal
;
2713 // ESC c Full Reset (RIS).
2714 Terminal
.prototype.reset = function() {
2715 this.options
.rows
= this.rows
;
2716 this.options
.cols
= this.cols
;
2717 Terminal
.call(this, this.options
);
2718 this.refresh(0, this.rows
- 1);
2721 // ESC H Tab Set (HTS is 0x88).
2722 Terminal
.prototype.tabSet = function() {
2723 this.tabs
[this.x
] = true;
2724 this.state
= normal
;
2732 // Cursor Up Ps Times (default = 1) (CUU).
2733 Terminal
.prototype.cursorUp = function(params
) {
2734 var param
= params
[0];
2735 if (param
< 1) param
= 1;
2737 if (this.y
< 0) this.y
= 0;
2741 // Cursor Down Ps Times (default = 1) (CUD).
2742 Terminal
.prototype.cursorDown = function(params
) {
2743 var param
= params
[0];
2744 if (param
< 1) param
= 1;
2746 if (this.y
>= this.rows
) {
2747 this.y
= this.rows
- 1;
2752 // Cursor Forward Ps Times (default = 1) (CUF).
2753 Terminal
.prototype.cursorForward = function(params
) {
2754 var param
= params
[0];
2755 if (param
< 1) param
= 1;
2757 if (this.x
>= this.cols
) {
2758 this.x
= this.cols
- 1;
2763 // Cursor Backward Ps Times (default = 1) (CUB).
2764 Terminal
.prototype.cursorBackward = function(params
) {
2765 var param
= params
[0];
2766 if (param
< 1) param
= 1;
2768 if (this.x
< 0) this.x
= 0;
2772 // Cursor Position [row;column] (default = [1,1]) (CUP).
2773 Terminal
.prototype.cursorPos = function(params
) {
2776 row
= params
[0] - 1;
2778 if (params
.length
>= 2) {
2779 col
= params
[1] - 1;
2786 } else if (row
>= this.rows
) {
2787 row
= this.rows
- 1;
2792 } else if (col
>= this.cols
) {
2793 col
= this.cols
- 1;
2800 // CSI Ps J Erase in Display (ED).
2801 // Ps = 0 -> Erase Below (default).
2802 // Ps = 1 -> Erase Above.
2803 // Ps = 2 -> Erase All.
2804 // Ps = 3 -> Erase Saved Lines (xterm).
2806 // Erase in Display (DECSED).
2807 // Ps = 0 -> Selective Erase Below (default).
2808 // Ps = 1 -> Selective Erase Above.
2809 // Ps = 2 -> Selective Erase All.
2810 Terminal
.prototype.eraseInDisplay = function(params
) {
2812 switch (params
[0]) {
2814 this.eraseRight(this.x
, this.y
);
2816 for (; j
< this.rows
; j
++) {
2821 this.eraseLeft(this.x
, this.y
);
2829 while (j
--) this.eraseLine(j
);
2837 // CSI Ps K Erase in Line (EL).
2838 // Ps = 0 -> Erase to Right (default).
2839 // Ps = 1 -> Erase to Left.
2840 // Ps = 2 -> Erase All.
2842 // Erase in Line (DECSEL).
2843 // Ps = 0 -> Selective Erase to Right (default).
2844 // Ps = 1 -> Selective Erase to Left.
2845 // Ps = 2 -> Selective Erase All.
2846 Terminal
.prototype.eraseInLine = function(params
) {
2847 switch (params
[0]) {
2849 this.eraseRight(this.x
, this.y
);
2852 this.eraseLeft(this.x
, this.y
);
2855 this.eraseLine(this.y
);
2860 // CSI Pm m Character Attributes (SGR).
2861 // Ps = 0 -> Normal (default).
2863 // Ps = 4 -> Underlined.
2864 // Ps = 5 -> Blink (appears as Bold).
2865 // Ps = 7 -> Inverse.
2866 // Ps = 8 -> Invisible, i.e., hidden (VT300).
2867 // Ps = 2 2 -> Normal (neither bold nor faint).
2868 // Ps = 2 4 -> Not underlined.
2869 // Ps = 2 5 -> Steady (not blinking).
2870 // Ps = 2 7 -> Positive (not inverse).
2871 // Ps = 2 8 -> Visible, i.e., not hidden (VT300).
2872 // Ps = 3 0 -> Set foreground color to Black.
2873 // Ps = 3 1 -> Set foreground color to Red.
2874 // Ps = 3 2 -> Set foreground color to Green.
2875 // Ps = 3 3 -> Set foreground color to Yellow.
2876 // Ps = 3 4 -> Set foreground color to Blue.
2877 // Ps = 3 5 -> Set foreground color to Magenta.
2878 // Ps = 3 6 -> Set foreground color to Cyan.
2879 // Ps = 3 7 -> Set foreground color to White.
2880 // Ps = 3 9 -> Set foreground color to default (original).
2881 // Ps = 4 0 -> Set background color to Black.
2882 // Ps = 4 1 -> Set background color to Red.
2883 // Ps = 4 2 -> Set background color to Green.
2884 // Ps = 4 3 -> Set background color to Yellow.
2885 // Ps = 4 4 -> Set background color to Blue.
2886 // Ps = 4 5 -> Set background color to Magenta.
2887 // Ps = 4 6 -> Set background color to Cyan.
2888 // Ps = 4 7 -> Set background color to White.
2889 // Ps = 4 9 -> Set background color to default (original).
2891 // If 16-color support is compiled, the following apply. Assume
2892 // that xterm's resources are set so that the ISO color codes are
2893 // the first 8 of a set of 16. Then the aixterm colors are the
2894 // bright versions of the ISO colors:
2895 // Ps = 9 0 -> Set foreground color to Black.
2896 // Ps = 9 1 -> Set foreground color to Red.
2897 // Ps = 9 2 -> Set foreground color to Green.
2898 // Ps = 9 3 -> Set foreground color to Yellow.
2899 // Ps = 9 4 -> Set foreground color to Blue.
2900 // Ps = 9 5 -> Set foreground color to Magenta.
2901 // Ps = 9 6 -> Set foreground color to Cyan.
2902 // Ps = 9 7 -> Set foreground color to White.
2903 // Ps = 1 0 0 -> Set background color to Black.
2904 // Ps = 1 0 1 -> Set background color to Red.
2905 // Ps = 1 0 2 -> Set background color to Green.
2906 // Ps = 1 0 3 -> Set background color to Yellow.
2907 // Ps = 1 0 4 -> Set background color to Blue.
2908 // Ps = 1 0 5 -> Set background color to Magenta.
2909 // Ps = 1 0 6 -> Set background color to Cyan.
2910 // Ps = 1 0 7 -> Set background color to White.
2912 // If xterm is compiled with the 16-color support disabled, it
2913 // supports the following, from rxvt:
2914 // Ps = 1 0 0 -> Set foreground and background color to
2917 // If 88- or 256-color support is compiled, the following apply.
2918 // Ps = 3 8 ; 5 ; Ps -> Set foreground color to the second
2920 // Ps = 4 8 ; 5 ; Ps -> Set background color to the second
2922 Terminal
.prototype.charAttributes = function(params
) {
2923 // Optimize a single SGR0.
2924 if (params
.length
=== 1 && params
[0] === 0) {
2925 this.curAttr
= this.defAttr
;
2929 var l
= params
.length
2931 , flags
= this.curAttr
>> 18
2932 , fg
= (this.curAttr
>> 9) & 0x1ff
2933 , bg
= this.curAttr
& 0x1ff
2936 for (; i
< l
; i
++) {
2938 if (p
>= 30 && p
<= 37) {
2941 } else if (p
>= 40 && p
<= 47) {
2944 } else if (p
>= 90 && p
<= 97) {
2948 } else if (p
>= 100 && p
<= 107) {
2952 } else if (p
=== 0) {
2954 flags
= this.defAttr
>> 18;
2955 fg
= (this.defAttr
>> 9) & 0x1ff;
2956 bg
= this.defAttr
& 0x1ff;
2960 } else if (p
=== 1) {
2963 } else if (p
=== 4) {
2966 } else if (p
=== 5) {
2969 } else if (p
=== 7) {
2970 // inverse and positive
2971 // test with: echo -e '\e[31m\e[42mhello\e[7mworld\e[27mhi\e[m'
2973 } else if (p
=== 8) {
2976 } else if (p
=== 22) {
2979 } else if (p
=== 24) {
2982 } else if (p
=== 25) {
2985 } else if (p
=== 27) {
2988 } else if (p
=== 28) {
2991 } else if (p
=== 39) {
2993 fg
= (this.defAttr
>> 9) & 0x1ff;
2994 } else if (p
=== 49) {
2996 bg
= this.defAttr
& 0x1ff;
2997 } else if (p
=== 38) {
2999 if (params
[i
+ 1] === 2) {
3003 params
[i
+ 1] & 0xff,
3004 params
[i
+ 2] & 0xff);
3005 if (fg
=== -1) fg
= 0x1ff;
3007 } else if (params
[i
+ 1] === 5) {
3009 p
= params
[i
] & 0xff;
3012 } else if (p
=== 48) {
3014 if (params
[i
+ 1] === 2) {
3018 params
[i
+ 1] & 0xff,
3019 params
[i
+ 2] & 0xff);
3020 if (bg
=== -1) bg
= 0x1ff;
3022 } else if (params
[i
+ 1] === 5) {
3024 p
= params
[i
] & 0xff;
3027 } else if (p
=== 100) {
3029 fg
= (this.defAttr
>> 9) & 0x1ff;
3030 bg
= this.defAttr
& 0x1ff;
3032 this.error('Unknown SGR attribute: %d.', p
);
3036 this.curAttr
= (flags
<< 18) | (fg
<< 9) | bg
;
3039 // CSI Ps n Device Status Report (DSR).
3040 // Ps = 5 -> Status Report. Result (``OK'') is
3042 // Ps = 6 -> Report Cursor Position (CPR) [row;column].
3046 // Device Status Report (DSR, DEC-specific).
3047 // Ps = 6 -> Report Cursor Position (CPR) [row;column] as CSI
3048 // ? r ; c R (assumes page is zero).
3049 // Ps = 1 5 -> Report Printer status as CSI ? 1 0 n (ready).
3050 // or CSI ? 1 1 n (not ready).
3051 // Ps = 2 5 -> Report UDK status as CSI ? 2 0 n (unlocked)
3052 // or CSI ? 2 1 n (locked).
3053 // Ps = 2 6 -> Report Keyboard status as
3054 // CSI ? 2 7 ; 1 ; 0 ; 0 n (North American).
3055 // The last two parameters apply to VT400 & up, and denote key-
3056 // board ready and LK01 respectively.
3057 // Ps = 5 3 -> Report Locator status as
3058 // CSI ? 5 3 n Locator available, if compiled-in, or
3059 // CSI ? 5 0 n No Locator, if not.
3060 Terminal
.prototype.deviceStatus = function(params
) {
3062 switch (params
[0]) {
3065 this.send('\x1b[0n');
3076 } else if (this.prefix
=== '?') {
3077 // modern xterm doesnt seem to
3078 // respond to any of these except ?6, 6, and 5
3079 switch (params
[0]) {
3090 // this.send('\x1b[?11n');
3093 // dont support user defined keys
3094 // this.send('\x1b[?21n');
3097 // north american keyboard
3098 // this.send('\x1b[?27;1;0;0n');
3101 // no dec locator/mouse
3102 // this.send('\x1b[?50n');
3113 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
3114 Terminal
.prototype.insertChars = function(params
) {
3115 var param
, row
, j
, ch
;
3118 if (param
< 1) param
= 1;
3120 row
= this.y
+ this.ybase
;
3122 ch
= [this.eraseAttr(), ' ']; // xterm
3124 while (param
-- && j
< this.cols
) {
3125 this.lines
[row
].splice(j
++, 0, ch
);
3126 this.lines
[row
].pop();
3131 // Cursor Next Line Ps Times (default = 1) (CNL).
3132 // same as CSI Ps B ?
3133 Terminal
.prototype.cursorNextLine = function(params
) {
3134 var param
= params
[0];
3135 if (param
< 1) param
= 1;
3137 if (this.y
>= this.rows
) {
3138 this.y
= this.rows
- 1;
3144 // Cursor Preceding Line Ps Times (default = 1) (CNL).
3146 Terminal
.prototype.cursorPrecedingLine = function(params
) {
3147 var param
= params
[0];
3148 if (param
< 1) param
= 1;
3150 if (this.y
< 0) this.y
= 0;
3155 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
3156 Terminal
.prototype.cursorCharAbsolute = function(params
) {
3157 var param
= params
[0];
3158 if (param
< 1) param
= 1;
3163 // Insert Ps Line(s) (default = 1) (IL).
3164 Terminal
.prototype.insertLines = function(params
) {
3168 if (param
< 1) param
= 1;
3169 row
= this.y
+ this.ybase
;
3171 j
= this.rows
- 1 - this.scrollBottom
;
3172 j
= this.rows
- 1 + this.ybase
- j
+ 1;
3175 // test: echo -e '\e[44m\e[1L\e[0m'
3176 // blankLine(true) - xterm/linux behavior
3177 this.lines
.splice(row
, 0, this.blankLine(true));
3178 this.lines
.splice(j
, 1);
3182 this.updateRange(this.y
);
3183 this.updateRange(this.scrollBottom
);
3187 // Delete Ps Line(s) (default = 1) (DL).
3188 Terminal
.prototype.deleteLines = function(params
) {
3192 if (param
< 1) param
= 1;
3193 row
= this.y
+ this.ybase
;
3195 j
= this.rows
- 1 - this.scrollBottom
;
3196 j
= this.rows
- 1 + this.ybase
- j
;
3199 // test: echo -e '\e[44m\e[1M\e[0m'
3200 // blankLine(true) - xterm/linux behavior
3201 this.lines
.splice(j
+ 1, 0, this.blankLine(true));
3202 this.lines
.splice(row
, 1);
3206 this.updateRange(this.y
);
3207 this.updateRange(this.scrollBottom
);
3211 // Delete Ps Character(s) (default = 1) (DCH).
3212 Terminal
.prototype.deleteChars = function(params
) {
3216 if (param
< 1) param
= 1;
3218 row
= this.y
+ this.ybase
;
3219 ch
= [this.eraseAttr(), ' ']; // xterm
3222 this.lines
[row
].splice(this.x
, 1);
3223 this.lines
[row
].push(ch
);
3228 // Erase Ps Character(s) (default = 1) (ECH).
3229 Terminal
.prototype.eraseChars = function(params
) {
3230 var param
, row
, j
, ch
;
3233 if (param
< 1) param
= 1;
3235 row
= this.y
+ this.ybase
;
3237 ch
= [this.eraseAttr(), ' ']; // xterm
3239 while (param
-- && j
< this.cols
) {
3240 this.lines
[row
][j
++] = ch
;
3244 // CSI Pm ` Character Position Absolute
3245 // [column] (default = [row,1]) (HPA).
3246 Terminal
.prototype.charPosAbsolute = function(params
) {
3247 var param
= params
[0];
3248 if (param
< 1) param
= 1;
3250 if (this.x
>= this.cols
) {
3251 this.x
= this.cols
- 1;
3256 // Horizontal Position Relative
3258 Terminal
.prototype.HPositionRelative = function(params
) {
3259 var param
= params
[0];
3260 if (param
< 1) param
= 1;
3262 if (this.x
>= this.cols
) {
3263 this.x
= this.cols
- 1;
3267 // CSI Ps c Send Device Attributes (Primary DA).
3268 // Ps = 0 or omitted -> request attributes from terminal. The
3269 // response depends on the decTerminalID resource setting.
3270 // -> CSI ? 1 ; 2 c (``VT100 with Advanced Video Option'')
3271 // -> CSI ? 1 ; 0 c (``VT101 with No Options'')
3272 // -> CSI ? 6 c (``VT102'')
3273 // -> CSI ? 6 0 ; 1 ; 2 ; 6 ; 8 ; 9 ; 1 5 ; c (``VT220'')
3274 // The VT100-style response parameters do not mean anything by
3275 // themselves. VT220 parameters do, telling the host what fea-
3276 // tures the terminal supports:
3277 // Ps = 1 -> 132-columns.
3278 // Ps = 2 -> Printer.
3279 // Ps = 6 -> Selective erase.
3280 // Ps = 8 -> User-defined keys.
3281 // Ps = 9 -> National replacement character sets.
3282 // Ps = 1 5 -> Technical characters.
3283 // Ps = 2 2 -> ANSI color, e.g., VT525.
3284 // Ps = 2 9 -> ANSI text locator (i.e., DEC Locator mode).
3286 // Send Device Attributes (Secondary DA).
3287 // Ps = 0 or omitted -> request the terminal's identification
3288 // code. The response depends on the decTerminalID resource set-
3289 // ting. It should apply only to VT220 and up, but xterm extends
3291 // -> CSI > Pp ; Pv ; Pc c
3292 // where Pp denotes the terminal type
3293 // Pp = 0 -> ``VT100''.
3294 // Pp = 1 -> ``VT220''.
3295 // and Pv is the firmware version (for xterm, this was originally
3296 // the XFree86 patch number, starting with 95). In a DEC termi-
3297 // nal, Pc indicates the ROM cartridge registration number and is
3299 // More information:
3300 // xterm/charproc.c - line 2012, for more information.
3301 // vim responds with ^[[?0c or ^[[?1c after the terminal's response (?)
3302 Terminal
.prototype.sendDeviceAttributes = function(params
) {
3303 if (params
[0] > 0) return;
3306 if (this.is('xterm')
3307 || this.is('rxvt-unicode')
3308 || this.is('screen')) {
3309 this.send('\x1b[?1;2c');
3310 } else if (this.is('linux')) {
3311 this.send('\x1b[?6c');
3313 } else if (this.prefix
=== '>') {
3315 // seem to spit this
3316 // out around ~370 times (?).
3317 if (this.is('xterm')) {
3318 this.send('\x1b[>0;276;0c');
3319 } else if (this.is('rxvt-unicode')) {
3320 this.send('\x1b[>85;95;0c');
3321 } else if (this.is('linux')) {
3322 // not supported by linux console.
3323 // linux console echoes parameters.
3324 this.send(params
[0] + 'c');
3325 } else if (this.is('screen')) {
3326 this.send('\x1b[>83;40003;0c');
3332 // Line Position Absolute [row] (default = [1,column]) (VPA).
3333 Terminal
.prototype.linePosAbsolute = function(params
) {
3334 var param
= params
[0];
3335 if (param
< 1) param
= 1;
3337 if (this.y
>= this.rows
) {
3338 this.y
= this.rows
- 1;
3342 // 145 65 e * VPR - Vertical Position Relative
3344 Terminal
.prototype.VPositionRelative = function(params
) {
3345 var param
= params
[0];
3346 if (param
< 1) param
= 1;
3348 if (this.y
>= this.rows
) {
3349 this.y
= this.rows
- 1;
3354 // Horizontal and Vertical Position [row;column] (default =
3356 Terminal
.prototype.HVPosition = function(params
) {
3357 if (params
[0] < 1) params
[0] = 1;
3358 if (params
[1] < 1) params
[1] = 1;
3360 this.y
= params
[0] - 1;
3361 if (this.y
>= this.rows
) {
3362 this.y
= this.rows
- 1;
3365 this.x
= params
[1] - 1;
3366 if (this.x
>= this.cols
) {
3367 this.x
= this.cols
- 1;
3371 // CSI Pm h Set Mode (SM).
3372 // Ps = 2 -> Keyboard Action Mode (AM).
3373 // Ps = 4 -> Insert Mode (IRM).
3374 // Ps = 1 2 -> Send/receive (SRM).
3375 // Ps = 2 0 -> Automatic Newline (LNM).
3377 // DEC Private Mode Set (DECSET).
3378 // Ps = 1 -> Application Cursor Keys (DECCKM).
3379 // Ps = 2 -> Designate USASCII for character sets G0-G3
3380 // (DECANM), and set VT100 mode.
3381 // Ps = 3 -> 132 Column Mode (DECCOLM).
3382 // Ps = 4 -> Smooth (Slow) Scroll (DECSCLM).
3383 // Ps = 5 -> Reverse Video (DECSCNM).
3384 // Ps = 6 -> Origin Mode (DECOM).
3385 // Ps = 7 -> Wraparound Mode (DECAWM).
3386 // Ps = 8 -> Auto-repeat Keys (DECARM).
3387 // Ps = 9 -> Send Mouse X & Y on button press. See the sec-
3388 // tion Mouse Tracking.
3389 // Ps = 1 0 -> Show toolbar (rxvt).
3390 // Ps = 1 2 -> Start Blinking Cursor (att610).
3391 // Ps = 1 8 -> Print form feed (DECPFF).
3392 // Ps = 1 9 -> Set print extent to full screen (DECPEX).
3393 // Ps = 2 5 -> Show Cursor (DECTCEM).
3394 // Ps = 3 0 -> Show scrollbar (rxvt).
3395 // Ps = 3 5 -> Enable font-shifting functions (rxvt).
3396 // Ps = 3 8 -> Enter Tektronix Mode (DECTEK).
3397 // Ps = 4 0 -> Allow 80 -> 132 Mode.
3398 // Ps = 4 1 -> more(1) fix (see curses resource).
3399 // Ps = 4 2 -> Enable Nation Replacement Character sets (DECN-
3401 // Ps = 4 4 -> Turn On Margin Bell.
3402 // Ps = 4 5 -> Reverse-wraparound Mode.
3403 // Ps = 4 6 -> Start Logging. This is normally disabled by a
3404 // compile-time option.
3405 // Ps = 4 7 -> Use Alternate Screen Buffer. (This may be dis-
3406 // abled by the titeInhibit resource).
3407 // Ps = 6 6 -> Application keypad (DECNKM).
3408 // Ps = 6 7 -> Backarrow key sends backspace (DECBKM).
3409 // Ps = 1 0 0 0 -> Send Mouse X & Y on button press and
3410 // release. See the section Mouse Tracking.
3411 // Ps = 1 0 0 1 -> Use Hilite Mouse Tracking.
3412 // Ps = 1 0 0 2 -> Use Cell Motion Mouse Tracking.
3413 // Ps = 1 0 0 3 -> Use All Motion Mouse Tracking.
3414 // Ps = 1 0 0 4 -> Send FocusIn/FocusOut events.
3415 // Ps = 1 0 0 5 -> Enable Extended Mouse Mode.
3416 // Ps = 1 0 1 0 -> Scroll to bottom on tty output (rxvt).
3417 // Ps = 1 0 1 1 -> Scroll to bottom on key press (rxvt).
3418 // Ps = 1 0 3 4 -> Interpret "meta" key, sets eighth bit.
3419 // (enables the eightBitInput resource).
3420 // Ps = 1 0 3 5 -> Enable special modifiers for Alt and Num-
3421 // Lock keys. (This enables the numLock resource).
3422 // Ps = 1 0 3 6 -> Send ESC when Meta modifies a key. (This
3423 // enables the metaSendsEscape resource).
3424 // Ps = 1 0 3 7 -> Send DEL from the editing-keypad Delete
3426 // Ps = 1 0 3 9 -> Send ESC when Alt modifies a key. (This
3427 // enables the altSendsEscape resource).
3428 // Ps = 1 0 4 0 -> Keep selection even if not highlighted.
3429 // (This enables the keepSelection resource).
3430 // Ps = 1 0 4 1 -> Use the CLIPBOARD selection. (This enables
3431 // the selectToClipboard resource).
3432 // Ps = 1 0 4 2 -> Enable Urgency window manager hint when
3433 // Control-G is received. (This enables the bellIsUrgent
3435 // Ps = 1 0 4 3 -> Enable raising of the window when Control-G
3436 // is received. (enables the popOnBell resource).
3437 // Ps = 1 0 4 7 -> Use Alternate Screen Buffer. (This may be
3438 // disabled by the titeInhibit resource).
3439 // Ps = 1 0 4 8 -> Save cursor as in DECSC. (This may be dis-
3440 // abled by the titeInhibit resource).
3441 // Ps = 1 0 4 9 -> Save cursor as in DECSC and use Alternate
3442 // Screen Buffer, clearing it first. (This may be disabled by
3443 // the titeInhibit resource). This combines the effects of the 1
3444 // 0 4 7 and 1 0 4 8 modes. Use this with terminfo-based
3445 // applications rather than the 4 7 mode.
3446 // Ps = 1 0 5 0 -> Set terminfo/termcap function-key mode.
3447 // Ps = 1 0 5 1 -> Set Sun function-key mode.
3448 // Ps = 1 0 5 2 -> Set HP function-key mode.
3449 // Ps = 1 0 5 3 -> Set SCO function-key mode.
3450 // Ps = 1 0 6 0 -> Set legacy keyboard emulation (X11R6).
3451 // Ps = 1 0 6 1 -> Set VT220 keyboard emulation.
3452 // Ps = 2 0 0 4 -> Set bracketed paste mode.
3454 // http://vt100.net/docs/vt220-rm/chapter4.html
3455 Terminal
.prototype.setMode = function(params
) {
3456 if (typeof params
=== 'object') {
3457 var l
= params
.length
3460 for (; i
< l
; i
++) {
3461 this.setMode(params
[i
]);
3470 this.insertMode
= true;
3473 //this.convertEol = true;
3476 } else if (this.prefix
=== '?') {
3479 this.applicationCursor
= true;
3482 this.setgCharset(0, Terminal
.charsets
.US
);
3483 this.setgCharset(1, Terminal
.charsets
.US
);
3484 this.setgCharset(2, Terminal
.charsets
.US
);
3485 this.setgCharset(3, Terminal
.charsets
.US
);
3486 // set VT100 mode here
3488 case 3: // 132 col mode
3489 this.savedCols
= this.cols
;
3490 this.resize(132, this.rows
);
3493 this.originMode
= true;
3496 this.wraparoundMode
= true;
3499 // this.cursorBlink = true;
3502 this.log('Serial port requested application keypad.');
3503 this.applicationKeypad
= true;
3505 case 9: // X10 Mouse
3506 // no release, no motion, no wheel, no modifiers.
3507 case 1000: // vt200 mouse
3509 // no modifiers, except control on the wheel.
3510 case 1002: // button event mouse
3511 case 1003: // any event mouse
3512 // any event - sends motion events,
3513 // even if there is no button held down.
3514 this.x10Mouse
= params
=== 9;
3515 this.vt200Mouse
= params
=== 1000;
3516 this.normalMouse
= params
> 1000;
3517 this.mouseEvents
= true;
3518 this.element
.style
.cursor
= 'default';
3519 this.log('Binding to mouse events.');
3521 case 1004: // send focusin/focusout events
3524 this.sendFocus
= true;
3526 case 1005: // utf8 ext mode mouse
3527 this.utfMouse
= true;
3528 // for wide terminals
3529 // simply encodes large values as utf8 characters
3531 case 1006: // sgr ext mode mouse
3532 this.sgrMouse
= true;
3533 // for wide terminals
3534 // does not add 32 to fields
3535 // press: ^[[<b;x;yM
3536 // release: ^[[<b;x;ym
3538 case 1015: // urxvt ext mode mouse
3539 this.urxvtMouse
= true;
3540 // for wide terminals
3541 // numbers for fields
3543 // motion: ^[[b;x;yT
3545 case 25: // show cursor
3546 this.cursorHidden
= false;
3548 case 1049: // alt screen buffer cursor
3549 //this.saveCursor();
3551 case 47: // alt screen buffer
3552 case 1047: // alt screen buffer
3560 scrollTop
: this.scrollTop
,
3561 scrollBottom
: this.scrollBottom
,
3563 // XXX save charset(s) here?
3564 // charset: this.charset,
3565 // glevel: this.glevel,
3566 // charsets: this.charsets
3569 this.normal
= normal
;
3577 // CSI Pm l Reset Mode (RM).
3578 // Ps = 2 -> Keyboard Action Mode (AM).
3579 // Ps = 4 -> Replace Mode (IRM).
3580 // Ps = 1 2 -> Send/receive (SRM).
3581 // Ps = 2 0 -> Normal Linefeed (LNM).
3583 // DEC Private Mode Reset (DECRST).
3584 // Ps = 1 -> Normal Cursor Keys (DECCKM).
3585 // Ps = 2 -> Designate VT52 mode (DECANM).
3586 // Ps = 3 -> 80 Column Mode (DECCOLM).
3587 // Ps = 4 -> Jump (Fast) Scroll (DECSCLM).
3588 // Ps = 5 -> Normal Video (DECSCNM).
3589 // Ps = 6 -> Normal Cursor Mode (DECOM).
3590 // Ps = 7 -> No Wraparound Mode (DECAWM).
3591 // Ps = 8 -> No Auto-repeat Keys (DECARM).
3592 // Ps = 9 -> Don't send Mouse X & Y on button press.
3593 // Ps = 1 0 -> Hide toolbar (rxvt).
3594 // Ps = 1 2 -> Stop Blinking Cursor (att610).
3595 // Ps = 1 8 -> Don't print form feed (DECPFF).
3596 // Ps = 1 9 -> Limit print to scrolling region (DECPEX).
3597 // Ps = 2 5 -> Hide Cursor (DECTCEM).
3598 // Ps = 3 0 -> Don't show scrollbar (rxvt).
3599 // Ps = 3 5 -> Disable font-shifting functions (rxvt).
3600 // Ps = 4 0 -> Disallow 80 -> 132 Mode.
3601 // Ps = 4 1 -> No more(1) fix (see curses resource).
3602 // Ps = 4 2 -> Disable Nation Replacement Character sets (DEC-
3604 // Ps = 4 4 -> Turn Off Margin Bell.
3605 // Ps = 4 5 -> No Reverse-wraparound Mode.
3606 // Ps = 4 6 -> Stop Logging. (This is normally disabled by a
3607 // compile-time option).
3608 // Ps = 4 7 -> Use Normal Screen Buffer.
3609 // Ps = 6 6 -> Numeric keypad (DECNKM).
3610 // Ps = 6 7 -> Backarrow key sends delete (DECBKM).
3611 // Ps = 1 0 0 0 -> Don't send Mouse X & Y on button press and
3612 // release. See the section Mouse Tracking.
3613 // Ps = 1 0 0 1 -> Don't use Hilite Mouse Tracking.
3614 // Ps = 1 0 0 2 -> Don't use Cell Motion Mouse Tracking.
3615 // Ps = 1 0 0 3 -> Don't use All Motion Mouse Tracking.
3616 // Ps = 1 0 0 4 -> Don't send FocusIn/FocusOut events.
3617 // Ps = 1 0 0 5 -> Disable Extended Mouse Mode.
3618 // Ps = 1 0 1 0 -> Don't scroll to bottom on tty output
3620 // Ps = 1 0 1 1 -> Don't scroll to bottom on key press (rxvt).
3621 // Ps = 1 0 3 4 -> Don't interpret "meta" key. (This disables
3622 // the eightBitInput resource).
3623 // Ps = 1 0 3 5 -> Disable special modifiers for Alt and Num-
3624 // Lock keys. (This disables the numLock resource).
3625 // Ps = 1 0 3 6 -> Don't send ESC when Meta modifies a key.
3626 // (This disables the metaSendsEscape resource).
3627 // Ps = 1 0 3 7 -> Send VT220 Remove from the editing-keypad
3629 // Ps = 1 0 3 9 -> Don't send ESC when Alt modifies a key.
3630 // (This disables the altSendsEscape resource).
3631 // Ps = 1 0 4 0 -> Do not keep selection when not highlighted.
3632 // (This disables the keepSelection resource).
3633 // Ps = 1 0 4 1 -> Use the PRIMARY selection. (This disables
3634 // the selectToClipboard resource).
3635 // Ps = 1 0 4 2 -> Disable Urgency window manager hint when
3636 // Control-G is received. (This disables the bellIsUrgent
3638 // Ps = 1 0 4 3 -> Disable raising of the window when Control-
3639 // G is received. (This disables the popOnBell resource).
3640 // Ps = 1 0 4 7 -> Use Normal Screen Buffer, clearing screen
3641 // first if in the Alternate Screen. (This may be disabled by
3642 // the titeInhibit resource).
3643 // Ps = 1 0 4 8 -> Restore cursor as in DECRC. (This may be
3644 // disabled by the titeInhibit resource).
3645 // Ps = 1 0 4 9 -> Use Normal Screen Buffer and restore cursor
3646 // as in DECRC. (This may be disabled by the titeInhibit
3647 // resource). This combines the effects of the 1 0 4 7 and 1 0
3648 // 4 8 modes. Use this with terminfo-based applications rather
3649 // than the 4 7 mode.
3650 // Ps = 1 0 5 0 -> Reset terminfo/termcap function-key mode.
3651 // Ps = 1 0 5 1 -> Reset Sun function-key mode.
3652 // Ps = 1 0 5 2 -> Reset HP function-key mode.
3653 // Ps = 1 0 5 3 -> Reset SCO function-key mode.
3654 // Ps = 1 0 6 0 -> Reset legacy keyboard emulation (X11R6).
3655 // Ps = 1 0 6 1 -> Reset keyboard emulation to Sun/PC style.
3656 // Ps = 2 0 0 4 -> Reset bracketed paste mode.
3657 Terminal
.prototype.resetMode = function(params
) {
3658 if (typeof params
=== 'object') {
3659 var l
= params
.length
3662 for (; i
< l
; i
++) {
3663 this.resetMode(params
[i
]);
3672 this.insertMode
= false;
3675 //this.convertEol = false;
3678 } else if (this.prefix
=== '?') {
3681 this.applicationCursor
= false;
3684 if (this.cols
=== 132 && this.savedCols
) {
3685 this.resize(this.savedCols
, this.rows
);
3687 delete this.savedCols
;
3690 this.originMode
= false;
3693 this.wraparoundMode
= false;
3696 // this.cursorBlink = false;
3699 this.log('Switching back to normal keypad.');
3700 this.applicationKeypad
= false;
3702 case 9: // X10 Mouse
3703 case 1000: // vt200 mouse
3704 case 1002: // button event mouse
3705 case 1003: // any event mouse
3706 this.x10Mouse
= false;
3707 this.vt200Mouse
= false;
3708 this.normalMouse
= false;
3709 this.mouseEvents
= false;
3710 this.element
.style
.cursor
= '';
3712 case 1004: // send focusin/focusout events
3713 this.sendFocus
= false;
3715 case 1005: // utf8 ext mode mouse
3716 this.utfMouse
= false;
3718 case 1006: // sgr ext mode mouse
3719 this.sgrMouse
= false;
3721 case 1015: // urxvt ext mode mouse
3722 this.urxvtMouse
= false;
3724 case 25: // hide cursor
3725 this.cursorHidden
= true;
3727 case 1049: // alt screen buffer cursor
3729 case 47: // normal screen buffer
3730 case 1047: // normal screen buffer - clearing it first
3732 this.lines
= this.normal
.lines
;
3733 this.ybase
= this.normal
.ybase
;
3734 this.ydisp
= this.normal
.ydisp
;
3735 this.x
= this.normal
.x
;
3736 this.y
= this.normal
.y
;
3737 this.scrollTop
= this.normal
.scrollTop
;
3738 this.scrollBottom
= this.normal
.scrollBottom
;
3739 this.tabs
= this.normal
.tabs
;
3741 // if (params === 1049) {
3742 // this.x = this.savedX;
3743 // this.y = this.savedY;
3745 this.refresh(0, this.rows
- 1);
3754 // Set Scrolling Region [top;bottom] (default = full size of win-
3757 Terminal
.prototype.setScrollRegion = function(params
) {
3758 if (this.prefix
) return;
3759 this.scrollTop
= (params
[0] || 1) - 1;
3760 this.scrollBottom
= (params
[1] || this.rows
) - 1;
3766 // Save cursor (ANSI.SYS).
3767 Terminal
.prototype.saveCursor = function(params
) {
3768 this.savedX
= this.x
;
3769 this.savedY
= this.y
;
3773 // Restore cursor (ANSI.SYS).
3774 Terminal
.prototype.restoreCursor = function(params
) {
3775 this.x
= this.savedX
|| 0;
3776 this.y
= this.savedY
|| 0;
3784 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
3785 Terminal
.prototype.cursorForwardTab = function(params
) {
3786 var param
= params
[0] || 1;
3788 this.x
= this.nextStop();
3792 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
3793 Terminal
.prototype.scrollUp = function(params
) {
3794 var param
= params
[0] || 1;
3796 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
3797 this.lines
.splice(this.ybase
+ this.scrollBottom
, 0, this.blankLine());
3800 this.updateRange(this.scrollTop
);
3801 this.updateRange(this.scrollBottom
);
3804 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
3805 Terminal
.prototype.scrollDown = function(params
) {
3806 var param
= params
[0] || 1;
3808 this.lines
.splice(this.ybase
+ this.scrollBottom
, 1);
3809 this.lines
.splice(this.ybase
+ this.scrollTop
, 0, this.blankLine());
3812 this.updateRange(this.scrollTop
);
3813 this.updateRange(this.scrollBottom
);
3816 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
3817 // Initiate highlight mouse tracking. Parameters are
3818 // [func;startx;starty;firstrow;lastrow]. See the section Mouse
3820 Terminal
.prototype.initMouseTracking = function(params
) {
3821 // Relevant: DECSET 1001
3825 // Reset one or more features of the title modes to the default
3826 // value. Normally, "reset" disables the feature. It is possi-
3827 // ble to disable the ability to reset features by compiling a
3828 // different default for the title modes into xterm.
3829 // Ps = 0 -> Do not set window/icon labels using hexadecimal.
3830 // Ps = 1 -> Do not query window/icon labels using hexadeci-
3832 // Ps = 2 -> Do not set window/icon labels using UTF-8.
3833 // Ps = 3 -> Do not query window/icon labels using UTF-8.
3834 // (See discussion of "Title Modes").
3835 Terminal
.prototype.resetTitleModes = function(params
) {
3839 // CSI Ps Z Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
3840 Terminal
.prototype.cursorBackwardTab = function(params
) {
3841 var param
= params
[0] || 1;
3843 this.x
= this.prevStop();
3847 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
3848 Terminal
.prototype.repeatPrecedingCharacter = function(params
) {
3849 var param
= params
[0] || 1
3850 , line
= this.lines
[this.ybase
+ this.y
]
3851 , ch
= line
[this.x
- 1] || [this.defAttr
, ' '];
3853 while (param
--) line
[this.x
++] = ch
;
3856 // CSI Ps g Tab Clear (TBC).
3857 // Ps = 0 -> Clear Current Column (default).
3858 // Ps = 3 -> Clear All.
3860 // Ps = 2 -> Clear Stops on Line.
3861 // http://vt100.net/annarbor/aaa-ug/section6.html
3862 Terminal
.prototype.tabClear = function(params
) {
3863 var param
= params
[0];
3865 delete this.tabs
[this.x
];
3866 } else if (param
=== 3) {
3871 // CSI Pm i Media Copy (MC).
3872 // Ps = 0 -> Print screen (default).
3873 // Ps = 4 -> Turn off printer controller mode.
3874 // Ps = 5 -> Turn on printer controller mode.
3876 // Media Copy (MC, DEC-specific).
3877 // Ps = 1 -> Print line containing cursor.
3878 // Ps = 4 -> Turn off autoprint mode.
3879 // Ps = 5 -> Turn on autoprint mode.
3880 // Ps = 1 0 -> Print composed display, ignores DECPEX.
3881 // Ps = 1 1 -> Print all pages.
3882 Terminal
.prototype.mediaCopy = function(params
) {
3887 // Set or reset resource-values used by xterm to decide whether
3888 // to construct escape sequences holding information about the
3889 // modifiers pressed with a given key. The first parameter iden-
3890 // tifies the resource to set/reset. The second parameter is the
3891 // value to assign to the resource. If the second parameter is
3892 // omitted, the resource is reset to its initial value.
3893 // Ps = 1 -> modifyCursorKeys.
3894 // Ps = 2 -> modifyFunctionKeys.
3895 // Ps = 4 -> modifyOtherKeys.
3896 // If no parameters are given, all resources are reset to their
3898 Terminal
.prototype.setResources = function(params
) {
3903 // Disable modifiers which may be enabled via the CSI > Ps; Ps m
3904 // sequence. This corresponds to a resource value of "-1", which
3905 // cannot be set with the other sequence. The parameter identi-
3906 // fies the resource to be disabled:
3907 // Ps = 1 -> modifyCursorKeys.
3908 // Ps = 2 -> modifyFunctionKeys.
3909 // Ps = 4 -> modifyOtherKeys.
3910 // If the parameter is omitted, modifyFunctionKeys is disabled.
3911 // When modifyFunctionKeys is disabled, xterm uses the modifier
3912 // keys to make an extended sequence of functions rather than
3913 // adding a parameter to each function key to denote the modi-
3915 Terminal
.prototype.disableModifiers = function(params
) {
3920 // Set resource value pointerMode. This is used by xterm to
3921 // decide whether to hide the pointer cursor as the user types.
3922 // Valid values for the parameter:
3923 // Ps = 0 -> never hide the pointer.
3924 // Ps = 1 -> hide if the mouse tracking mode is not enabled.
3925 // Ps = 2 -> always hide the pointer. If no parameter is
3926 // given, xterm uses the default, which is 1 .
3927 Terminal
.prototype.setPointerMode = function(params
) {
3931 // CSI ! p Soft terminal reset (DECSTR).
3932 // http://vt100.net/docs/vt220-rm/table4-10.html
3933 Terminal
.prototype.softReset = function(params
) {
3934 this.cursorHidden
= false;
3935 this.insertMode
= false;
3936 this.originMode
= false;
3937 this.wraparoundMode
= false; // autowrap
3938 this.applicationKeypad
= false; // ?
3939 this.applicationCursor
= false;
3941 this.scrollBottom
= this.rows
- 1;
3942 this.curAttr
= this.defAttr
;
3943 this.x
= this.y
= 0; // ?
3944 this.charset
= null;
3945 this.glevel
= 0; // ??
3946 this.charsets
= [null]; // ??
3950 // Request ANSI mode (DECRQM). For VT300 and up, reply is
3952 // where Ps is the mode number as in RM, and Pm is the mode
3954 // 0 - not recognized
3957 // 3 - permanently set
3958 // 4 - permanently reset
3959 Terminal
.prototype.requestAnsiMode = function(params
) {
3964 // Request DEC private mode (DECRQM). For VT300 and up, reply is
3966 // where Ps is the mode number as in DECSET, Pm is the mode value
3967 // as in the ANSI DECRQM.
3968 Terminal
.prototype.requestPrivateMode = function(params
) {
3973 // Set conformance level (DECSCL). Valid values for the first
3975 // Ps = 6 1 -> VT100.
3976 // Ps = 6 2 -> VT200.
3977 // Ps = 6 3 -> VT300.
3978 // Valid values for the second parameter:
3979 // Ps = 0 -> 8-bit controls.
3980 // Ps = 1 -> 7-bit controls (always set for VT100).
3981 // Ps = 2 -> 8-bit controls.
3982 Terminal
.prototype.setConformanceLevel = function(params
) {
3986 // CSI Ps q Load LEDs (DECLL).
3987 // Ps = 0 -> Clear all LEDS (default).
3988 // Ps = 1 -> Light Num Lock.
3989 // Ps = 2 -> Light Caps Lock.
3990 // Ps = 3 -> Light Scroll Lock.
3991 // Ps = 2 1 -> Extinguish Num Lock.
3992 // Ps = 2 2 -> Extinguish Caps Lock.
3993 // Ps = 2 3 -> Extinguish Scroll Lock.
3994 Terminal
.prototype.loadLEDs = function(params
) {
3999 // Set cursor style (DECSCUSR, VT520).
4000 // Ps = 0 -> blinking block.
4001 // Ps = 1 -> blinking block (default).
4002 // Ps = 2 -> steady block.
4003 // Ps = 3 -> blinking underline.
4004 // Ps = 4 -> steady underline.
4005 Terminal
.prototype.setCursorStyle = function(params
) {
4010 // Select character protection attribute (DECSCA). Valid values
4011 // for the parameter:
4012 // Ps = 0 -> DECSED and DECSEL can erase (default).
4013 // Ps = 1 -> DECSED and DECSEL cannot erase.
4014 // Ps = 2 -> DECSED and DECSEL can erase.
4015 Terminal
.prototype.setCharProtectionAttr = function(params
) {
4020 // Restore DEC Private Mode Values. The value of Ps previously
4021 // saved is restored. Ps values are the same as for DECSET.
4022 Terminal
.prototype.restorePrivateValues = function(params
) {
4026 // CSI Pt; Pl; Pb; Pr; Ps$ r
4027 // Change Attributes in Rectangular Area (DECCARA), VT400 and up.
4028 // Pt; Pl; Pb; Pr denotes the rectangle.
4029 // Ps denotes the SGR attributes to change: 0, 1, 4, 5, 7.
4030 // NOTE: xterm doesn't enable this code by default.
4031 Terminal
.prototype.setAttrInRectangle = function(params
) {
4041 for (; t
< b
+ 1; t
++) {
4042 line
= this.lines
[this.ybase
+ t
];
4043 for (i
= l
; i
< r
; i
++) {
4044 line
[i
] = [attr
, line
[i
][1]];
4049 this.updateRange(params
[0]);
4050 this.updateRange(params
[2]);
4054 // CSI Pc; Pt; Pl; Pb; Pr$ x
4055 // Fill Rectangular Area (DECFRA), VT420 and up.
4056 // Pc is the character to use.
4057 // Pt; Pl; Pb; Pr denotes the rectangle.
4058 // NOTE: xterm doesn't enable this code by default.
4059 Terminal
.prototype.fillRectangle = function(params
) {
4069 for (; t
< b
+ 1; t
++) {
4070 line
= this.lines
[this.ybase
+ t
];
4071 for (i
= l
; i
< r
; i
++) {
4072 line
[i
] = [line
[i
][0], String
.fromCharCode(ch
)];
4077 this.updateRange(params
[1]);
4078 this.updateRange(params
[3]);
4082 // Enable Locator Reporting (DECELR).
4083 // Valid values for the first parameter:
4084 // Ps = 0 -> Locator disabled (default).
4085 // Ps = 1 -> Locator enabled.
4086 // Ps = 2 -> Locator enabled for one report, then disabled.
4087 // The second parameter specifies the coordinate unit for locator
4089 // Valid values for the second parameter:
4090 // Pu = 0 <- or omitted -> default to character cells.
4091 // Pu = 1 <- device physical pixels.
4092 // Pu = 2 <- character cells.
4093 Terminal
.prototype.enableLocatorReporting = function(params
) {
4094 var val
= params
[0] > 0;
4095 //this.mouseEvents = val;
4096 //this.decLocator = val;
4099 // CSI Pt; Pl; Pb; Pr$ z
4100 // Erase Rectangular Area (DECERA), VT400 and up.
4101 // Pt; Pl; Pb; Pr denotes the rectangle.
4102 // NOTE: xterm doesn't enable this code by default.
4103 Terminal
.prototype.eraseRectangle = function(params
) {
4113 ch
= [this.eraseAttr(), ' ']; // xterm?
4115 for (; t
< b
+ 1; t
++) {
4116 line
= this.lines
[this.ybase
+ t
];
4117 for (i
= l
; i
< r
; i
++) {
4123 this.updateRange(params
[0]);
4124 this.updateRange(params
[2]);
4129 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
4130 // NOTE: xterm doesn't enable this code by default.
4131 Terminal
.prototype.insertColumns = function() {
4132 var param
= params
[0]
4133 , l
= this.ybase
+ this.rows
4134 , ch
= [this.eraseAttr(), ' '] // xterm?
4138 for (i
= this.ybase
; i
< l
; i
++) {
4139 this.lines
[i
].splice(this.x
+ 1, 0, ch
);
4140 this.lines
[i
].pop();
4149 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
4150 // NOTE: xterm doesn't enable this code by default.
4151 Terminal
.prototype.deleteColumns = function() {
4152 var param
= params
[0]
4153 , l
= this.ybase
+ this.rows
4154 , ch
= [this.eraseAttr(), ' '] // xterm?
4158 for (i
= this.ybase
; i
< l
; i
++) {
4159 this.lines
[i
].splice(this.x
, 1);
4160 this.lines
[i
].push(ch
);
4167 Terminal
.prototype.keyPrefix = function(ev
, key
) {
4168 if (key
=== 'k' || key
=== '&') {
4170 } else if (key
=== 'p' || key
=== ']') {
4171 this.emit('request paste');
4172 } else if (key
=== 'c') {
4173 this.emit('request create');
4174 } else if (key
>= '0' && key
<= '9') {
4177 this.emit('request term', key
);
4178 } else if (key
=== 'n') {
4179 this.emit('request term next');
4180 } else if (key
=== 'P') {
4181 this.emit('request term previous');
4182 } else if (key
=== ':') {
4183 this.emit('request command mode');
4187 Terminal
.prototype.keySelect = function(ev
, key
) {
4190 if (key
=== '\x04') { // ctrl-d
4191 var y
= this.ydisp
+ this.y
;
4192 if (this.ydisp
=== this.ybase
) {
4193 // Mimic vim behavior
4194 this.y
= Math
.min(this.y
+ (this.rows
- 1) / 2 | 0, this.rows
- 1);
4195 this.refresh(0, this.rows
- 1);
4197 this.scrollDisp((this.rows
- 1) / 2 | 0);
4202 if (key
=== '\x15') { // ctrl-u
4203 var y
= this.ydisp
+ this.y
;
4204 if (this.ydisp
=== 0) {
4205 // Mimic vim behavior
4206 this.y
= Math
.max(this.y
- (this.rows
- 1) / 2 | 0, 0);
4207 this.refresh(0, this.rows
- 1);
4209 this.scrollDisp(-(this.rows
- 1) / 2 | 0);
4214 if (key
=== '\x06') { // ctrl-f
4215 var y
= this.ydisp
+ this.y
;
4216 this.scrollDisp(this.rows
- 1);
4220 if (key
=== '\x02') { // ctrl-b
4221 var y
= this.ydisp
+ this.y
;
4222 this.scrollDisp(-(this.rows
- 1));
4226 if (key
=== 'k' || key
=== '\x1b[A') {
4227 var y
= this.ydisp
+ this.y
;
4231 this.scrollDisp(-1);
4233 this.refresh(this.y
, this.y
+ 1);
4237 if (key
=== 'j' || key
=== '\x1b[B') {
4238 var y
= this.ydisp
+ this.y
;
4240 if (this.y
>= this.rows
) {
4241 this.y
= this.rows
- 1;
4244 this.refresh(this.y
- 1, this.y
);
4248 if (key
=== 'h' || key
=== '\x1b[D') {
4254 this.refresh(this.y
, this.y
);
4258 if (key
=== 'l' || key
=== '\x1b[C') {
4261 if (this.x
>= this.cols
) {
4262 this.x
= this.cols
- 1;
4264 this.refresh(this.y
, this.y
);
4268 if (key
=== 'w' || key
=== 'W') {
4271 var oyd
= this.ydisp
;
4275 var yb
= this.ydisp
;
4276 var saw_space
= false;
4279 var line
= this.lines
[yb
+ y
];
4280 while (x
< this.cols
) {
4281 if (line
[x
][1] <= ' ') {
4283 } else if (saw_space
) {
4288 if (x
>= this.cols
) x
= this.cols
- 1;
4289 if (x
=== this.cols
- 1 && line
[x
][1] <= ' ') {
4291 if (++y
>= this.rows
) {
4293 if (++yb
> this.ybase
) {
4304 this.x
= x
, this.y
= y
;
4305 this.scrollDisp(-this.ydisp
+ yb
);
4310 if (key
=== 'b' || key
=== 'B') {
4313 var oyd
= this.ydisp
;
4317 var yb
= this.ydisp
;
4320 var line
= this.lines
[yb
+ y
];
4321 var saw_space
= x
> 0 && line
[x
][1] > ' ' && line
[x
- 1][1] > ' ';
4323 if (line
[x
][1] <= ' ') {
4324 if (saw_space
&& (x
+ 1 < this.cols
&& line
[x
+ 1][1] > ' ')) {
4334 if (x
=== 0 && (line
[x
][1] <= ' ' || !saw_space
)) {
4349 this.x
= x
, this.y
= y
;
4350 this.scrollDisp(-this.ydisp
+ yb
);
4355 if (key
=== 'e' || key
=== 'E') {
4358 var yb
= this.ydisp
;
4359 if (x
>= this.cols
) x
--;
4362 var line
= this.lines
[yb
+ y
];
4363 while (x
< this.cols
) {
4364 if (line
[x
][1] <= ' ') {
4370 while (x
< this.cols
) {
4371 if (line
[x
][1] <= ' ') {
4372 if (x
- 1 >= 0 && line
[x
- 1][1] > ' ') {
4379 if (x
>= this.cols
) x
= this.cols
- 1;
4380 if (x
=== this.cols
- 1 && line
[x
][1] <= ' ') {
4382 if (++y
>= this.rows
) {
4384 if (++yb
> this.ybase
) {
4394 this.x
= x
, this.y
= y
;
4395 this.scrollDisp(-this.ydisp
+ yb
);
4400 if (key
=== '^' || key
=== '0') {
4405 } else if (key
=== '^') {
4406 var line
= this.lines
[this.ydisp
+ this.y
];
4408 while (x
< this.cols
) {
4409 if (line
[x
][1] > ' ') {
4414 if (x
>= this.cols
) x
= this.cols
- 1;
4418 this.refresh(this.y
, this.y
);
4424 var line
= this.lines
[this.ydisp
+ this.y
];
4425 var x
= this.cols
- 1;
4431 this.refresh(this.y
, this.y
);
4435 if (key
=== 'g' || key
=== 'G') {
4438 var oyd
= this.ydisp
;
4440 this.x
= 0, this.y
= 0;
4441 this.scrollDisp(-this.ydisp
);
4442 } else if (key
=== 'G') {
4443 this.x
= 0, this.y
= this.rows
- 1;
4444 this.scrollDisp(this.ybase
);
4449 if (key
=== 'H' || key
=== 'M' || key
=== 'L') {
4453 this.x
= 0, this.y
= 0;
4454 } else if (key
=== 'M') {
4455 this.x
= 0, this.y
= this.rows
/ 2 | 0;
4456 } else if (key
=== 'L') {
4457 this.x
= 0, this.y
= this.rows
- 1;
4459 this.refresh(oy
, oy
);
4460 this.refresh(this.y
, this.y
);
4464 if (key
=== '{' || key
=== '}') {
4467 var oyd
= this.ydisp
;
4470 var saw_full
= false;
4472 var first_is_space
= -1;
4473 var y
= this.y
+ (key
=== '{' ? -1 : 1);
4474 var yb
= this.ydisp
;
4482 } else if (key
=== '}') {
4483 if (y
>= this.rows
) {
4485 if (yb
< this.ybase
) yb
++;
4490 line
= this.lines
[yb
+ y
];
4492 for (i
= 0; i
< this.cols
; i
++) {
4493 if (line
[i
][1] > ' ') {
4494 if (first_is_space
=== -1) {
4499 } else if (i
=== this.cols
- 1) {
4500 if (first_is_space
=== -1) {
4502 } else if (first_is_space
=== 0) {
4504 } else if (first_is_space
=== 1) {
4505 if (saw_full
) found
= true;
4520 } else if (key
=== '}') {
4522 if (y
>= this.rows
) {
4524 if (yb
< this.ybase
) yb
++;
4534 } else if (key
=== '}') {
4540 this.x
= 0, this.y
= y
;
4541 this.scrollDisp(-this.ydisp
+ yb
);
4553 Terminal
.charsets
= {};
4555 // DEC Special Character and Line Drawing Set.
4556 // http://vt100.net/docs/vt102-ug/table5-13.html
4557 // A lot of curses apps use this if they see TERM=xterm.
4558 // testing: echo -e '\e(0a\e(B'
4559 // The xterm output sometimes seems to conflict with the
4560 // reference above. xterm seems in line with the reference
4561 // when running vttest however.
4562 // The table below now uses xterm's output from vttest.
4563 Terminal
.charsets
.SCLD
= { // (0
4564 '`': '\u25c6', // '◆'
4565 'a': '\u2592', // '▒'
4566 'b': '\u0009', // '\t'
4567 'c': '\u000c', // '\f'
4568 'd': '\u000d', // '\r'
4569 'e': '\u000a', // '\n'
4570 'f': '\u00b0', // '°'
4571 'g': '\u00b1', // '±'
4572 'h': '\u2424', // '\u2424' (NL)
4573 'i': '\u000b', // '\v'
4574 'j': '\u2518', // '┘'
4575 'k': '\u2510', // '┐'
4576 'l': '\u250c', // '┌'
4577 'm': '\u2514', // '└'
4578 'n': '\u253c', // '┼'
4579 'o': '\u23ba', // '⎺'
4580 'p': '\u23bb', // '⎻'
4581 'q': '\u2500', // '─'
4582 'r': '\u23bc', // '⎼'
4583 's': '\u23bd', // '⎽'
4584 't': '\u251c', // '├'
4585 'u': '\u2524', // '┤'
4586 'v': '\u2534', // '┴'
4587 'w': '\u252c', // '┬'
4588 'x': '\u2502', // '│'
4589 'y': '\u2264', // '≤'
4590 'z': '\u2265', // '≥'
4591 '{': '\u03c0', // 'π'
4592 '|': '\u2260', // '≠'
4593 '}': '\u00a3', // '£'
4594 '~': '\u00b7' // '·'
4597 Terminal
.charsets
.UK
= null; // (A
4598 Terminal
.charsets
.US
= null; // (B (USASCII)
4599 Terminal
.charsets
.Dutch
= null; // (4
4600 Terminal
.charsets
.Finnish
= null; // (C or (5
4601 Terminal
.charsets
.French
= null; // (R
4602 Terminal
.charsets
.FrenchCanadian
= null; // (Q
4603 Terminal
.charsets
.German
= null; // (K
4604 Terminal
.charsets
.Italian
= null; // (Y
4605 Terminal
.charsets
.NorwegianDanish
= null; // (E or (6
4606 Terminal
.charsets
.Spanish
= null; // (Z
4607 Terminal
.charsets
.Swedish
= null; // (H or (7
4608 Terminal
.charsets
.Swiss
= null; // (=
4609 Terminal
.charsets
.ISOLatin
= null; // /A
4615 function on(el
, type
, handler
, capture
) {
4616 if (!Array
.isArray(el
)) {
4619 el
.forEach(function (element
) {
4620 element
.addEventListener(type
, handler
, capture
|| false);
4624 function off(el
, type
, handler
, capture
) {
4625 el
.removeEventListener(type
, handler
, capture
|| false);
4628 function cancel(ev
, force
) {
4629 if (!this.cancelEvents
&& !force
) {
4632 ev
.preventDefault();
4633 ev
.stopPropagation();
4637 function inherits(child
, parent
) {
4639 this.constructor = child
;
4641 f
.prototype = parent
.prototype;
4642 child
.prototype = new f
;
4645 // if bold is broken, we can't
4646 // use it in the terminal.
4647 function isBoldBroken(document
) {
4648 var body
= document
.getElementsByTagName('body')[0];
4649 var el
= document
.createElement('span');
4650 el
.innerHTML
= 'hello world';
4651 body
.appendChild(el
);
4652 var w1
= el
.scrollWidth
;
4653 el
.style
.fontWeight
= 'bold';
4654 var w2
= el
.scrollWidth
;
4655 body
.removeChild(el
);
4659 var String
= this.String
;
4660 var setTimeout
= this.setTimeout
;
4661 var setInterval
= this.setInterval
;
4663 function indexOf(obj
, el
) {
4666 if (obj
[i
] === el
) return i
;
4671 function isWide(ch
) {
4672 if (ch
<= '\uff00') return false;
4673 return (ch
>= '\uff01' && ch
<= '\uffbe')
4674 || (ch
>= '\uffc2' && ch
<= '\uffc7')
4675 || (ch
>= '\uffca' && ch
<= '\uffcf')
4676 || (ch
>= '\uffd2' && ch
<= '\uffd7')
4677 || (ch
>= '\uffda' && ch
<= '\uffdc')
4678 || (ch
>= '\uffe0' && ch
<= '\uffe6')
4679 || (ch
>= '\uffe8' && ch
<= '\uffee');
4682 function matchColor(r1
, g1
, b1
) {
4683 var hash
= (r1
<< 16) | (g1
<< 8) | b1
;
4685 if (matchColor
._cache
[hash
] != null) {
4686 return matchColor
._cache
[hash
];
4689 var ldiff
= Infinity
4698 for (; i
< Terminal
.vcolors
.length
; i
++) {
4699 c
= Terminal
.vcolors
[i
];
4704 diff
= matchColor
.distance(r1
, g1
, b1
, r2
, g2
, b2
);
4717 return matchColor
._cache
[hash
] = li
;
4720 matchColor
._cache
= {};
4722 // http://stackoverflow.com/questions/1633828
4723 matchColor
.distance = function(r1
, g1
, b1
, r2
, g2
, b2
) {
4724 return Math
.pow(30 * (r1
- r2
), 2)
4725 + Math
.pow(59 * (g1
- g2
), 2)
4726 + Math
.pow(11 * (b1
- b2
), 2);
4729 function each(obj
, iter
, con
) {
4730 if (obj
.forEach
) return obj
.forEach(iter
, con
);
4731 for (var i
= 0; i
< obj
.length
; i
++) {
4732 iter
.call(con
, obj
[i
], i
, obj
);
4736 function keys(obj
) {
4737 if (Object
.keys
) return Object
.keys(obj
);
4740 if (Object
.prototype.hasOwnProperty
.call(obj
, key
)) {
4751 Terminal
.EventEmitter
= EventEmitter
;
4752 Terminal
.inherits
= inherits
;
4755 Terminal
.cancel
= cancel
;