]>
git.proxmox.com Git - mirror_xterm.js.git/blob - src/xterm.js
69b645117e39f8703ff306a8dc48975ec6000f8d
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') {
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
) {
109 var args
= Array
.prototype.slice
.call(arguments
);
110 this.removeListener(type
, on
);
111 return listener
.apply(this, args
);
113 on
.listener
= listener
;
114 return this.on(type
, on
);
117 EventEmitter
.prototype.emit = function(type
) {
118 if (!this._events
[type
]) return;
120 var args
= Array
.prototype.slice
.call(arguments
, 1)
121 , obj
= this._events
[type
]
126 obj
[i
].apply(this, args
);
130 EventEmitter
.prototype.listeners = function(type
) {
131 return this._events
[type
] = this._events
[type
] || [];
138 var normal
= 0, escaped
= 1, csi
= 2, osc
= 3, charset
= 4, dcs
= 5, ignore
= 6;
144 function Terminal(options
) {
147 if (!(this instanceof Terminal
)) {
148 return new Terminal(arguments
[0], arguments
[1], arguments
[2]);
151 self
.cancel
= Terminal
.cancel
;
153 EventEmitter
.call(this);
155 if (typeof options
=== 'number') {
159 handler
: arguments
[2]
163 options
= options
|| {};
166 Object
.keys(Terminal
.defaults
).forEach(function(key
) {
167 if (options
[key
] == null) {
168 options
[key
] = Terminal
.options
[key
];
170 if (Terminal
[key
] !== Terminal
.defaults
[key
]) {
171 options
[key
] = Terminal
[key
];
174 self
[key
] = options
[key
];
177 if (options
.colors
.length
=== 8) {
178 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(8));
179 } else if (options
.colors
.length
=== 16) {
180 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(16));
181 } else if (options
.colors
.length
=== 10) {
182 options
.colors
= options
.colors
.slice(0, -2).concat(
183 Terminal
._colors
.slice(8, -2), options
.colors
.slice(-2));
184 } else if (options
.colors
.length
=== 18) {
185 options
.colors
= options
.colors
.concat(
186 Terminal
._colors
.slice(16, -2), options
.colors
.slice(-2));
188 this.colors
= options
.colors
;
190 this.options
= options
;
192 // this.context = options.context || window;
193 // this.document = options.document || document;
194 this.parent
= options
.body
|| options
.parent
195 || (document
? document
.getElementsByTagName('body')[0] : null);
197 this.cols
= options
.cols
|| options
.geometry
[0];
198 this.rows
= options
.rows
|| options
.geometry
[1];
200 if (options
.handler
) {
201 this.on('data', options
.handler
);
208 this.cursorState
= 0;
209 this.cursorHidden
= false;
214 this.scrollBottom
= this.rows
- 1;
217 this.applicationKeypad
= false;
218 this.applicationCursor
= false;
219 this.originMode
= false;
220 this.insertMode
= false;
221 this.wraparoundMode
= false;
226 this.gcharset
= null;
228 this.charsets
= [null];
252 this.readable
= true;
253 this.writable
= true;
255 this.defAttr
= (0 << 18) | (257 << 9) | (256 << 0);
256 this.curAttr
= this.defAttr
;
259 this.currentParam
= 0;
266 this.lines
.push(this.blankLine());
273 inherits(Terminal
, EventEmitter
);
275 // back_color_erase feature for xterm.
276 Terminal
.prototype.eraseAttr = function() {
277 // if (this.is('screen')) return this.defAttr;
278 return (this.defAttr
& ~0x1ff) | (this.curAttr
& 0x1ff);
286 Terminal
.tangoColors
= [
307 // Colors 0-15 + 16-255
308 // Much thanks to TooTallNate for writing this.
309 Terminal
.colors
= (function() {
310 var colors
= Terminal
.tangoColors
.slice()
311 , r
= [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff]
316 for (; i
< 216; i
++) {
317 out(r
[(i
/ 36) % 6 | 0], r
[(i
/ 6) % 6 | 0], r
[i
% 6]);
322 for (; i
< 24; i
++) {
327 function out(r
, g
, b
) {
328 colors
.push('#' + hex(r
) + hex(g
) + hex(b
));
333 return c
.length
< 2 ? '0' + c
: c
;
339 Terminal
._colors
= Terminal
.colors
.slice();
341 Terminal
.vcolors
= (function() {
343 , colors
= Terminal
.colors
347 for (; i
< 256; i
++) {
348 color
= parseInt(colors
[i
].substring(1), 16);
350 (color
>> 16) & 0xff,
363 Terminal
.defaults
= {
364 colors
: Terminal
.colors
,
376 // programFeatures: false,
380 Terminal
.options
= {};
382 Terminal
.focus
= null;
384 each(keys(Terminal
.defaults
), function(key
) {
385 Terminal
[key
] = Terminal
.defaults
[key
];
386 Terminal
.options
[key
] = Terminal
.defaults
[key
];
392 Terminal
.prototype.focus = function() {
393 if (document
.activeElement
=== this.textarea
) {
397 if (this.sendFocus
) {
402 this.textarea
.focus();
403 Terminal
.focus
= this;
406 Terminal
.prototype.blur = function() {
407 if (Terminal
.focus
!== this) {
411 this.cursorState
= 0;
412 this.refresh(this.y
, this.y
);
413 this.textarea
.blur();
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);
432 * Bind to paste event
434 Terminal
.bindPaste = function(term
) {
435 on([term
.textarea
, term
.element
], 'paste', function(ev
) {
436 ev
.stopPropagation();
437 if (ev
.clipboardData
) {
438 var text
= ev
.clipboardData
.getData('text/plain');
441 term
.textarea
.value
= '';
442 return term
.cancel(ev
);
448 * Apply key handling to the terminal
450 Terminal
.bindKeys = function(term
) {
451 on(term
.element
, 'keydown', function(ev
) {
452 if (document
.activeElement
!= this) {
458 on(term
.element
, 'keypress', function(ev
) {
459 if (document
.activeElement
!= this) {
465 on(term
.element
, 'keyup', term
.focus
.bind(term
));
467 on(term
.textarea
, 'keydown', function(ev
) {
471 on(term
.textarea
, 'keypress', function(ev
) {
475 * Truncate the textarea's value, since it is not needed
485 Terminal
.bindCopy = function(term
) {
486 on(term
.element
, 'copy', function(ev
) {
493 * Insert the given row to the terminal or produce a new one
494 * if no row argument is passed. Return the inserted row.
496 Terminal
.prototype.insertRow = function (row
) {
497 if (typeof row
!= 'object') {
498 row
= document
.createElement('div');
501 this.rowContainer
.appendChild(row
);
502 this.children
.push(row
);
509 * Open Terminal in the DOM
511 Terminal
.prototype.open = function(parent
) {
512 var self
=this, i
=0, div
;
514 this.parent
= parent
|| this.parent
;
517 throw new Error('Terminal requires a parent element.');
521 * Grab global elements
523 this.context
= this.parent
.ownerDocument
.defaultView
;
524 this.document
= this.parent
.ownerDocument
;
525 this.body
= this.document
.getElementsByTagName('body')[0];
530 if (this.context
.navigator
&& this.context
.navigator
.userAgent
) {
531 this.isMac
= !!~this.context
.navigator
.userAgent
.indexOf('Mac');
532 this.isIpad
= !!~this.context
.navigator
.userAgent
.indexOf('iPad');
533 this.isIphone
= !!~this.context
.navigator
.userAgent
.indexOf('iPhone');
534 this.isMSIE
= !!~this.context
.navigator
.userAgent
.indexOf('MSIE');
538 * Create main element container
540 this.element
= this.document
.createElement('div');
541 this.element
.classList
.add('terminal');
542 this.element
.classList
.add('xterm');
543 this.element
.classList
.add('xterm-theme-' + this.theme
);
544 this.element
.setAttribute('tabindex', 0);
547 * Create the container that will hold the lines of the terminal and then
548 * produce the lines the lines.
550 this.rowContainer
= document
.createElement('div');
551 this.rowContainer
.classList
.add('xterm-rows');
552 this.element
.appendChild(this.rowContainer
);
556 * Create the container that will hold helpers like the textarea for
557 * capturing DOM Events. Then produce the helpers.
559 this.helperContainer
= document
.createElement('div');
560 this.helperContainer
.classList
.add('xterm-helpers');
561 this.element
.appendChild(this.helperContainer
);
562 this.textarea
= document
.createElement('textarea');
563 this.textarea
.classList
.add('xterm-helper-textarea');
564 this.textarea
.setAttribute('autocorrect', 'off');
565 this.textarea
.setAttribute('autocapitalize', 'off');
566 this.textarea
.setAttribute('spellcheck', 'false');
567 this.textarea
.tabIndex
= 0;
568 this.textarea
.onfocus = function() {
569 self
.emit('focus', {terminal
: self
});
571 this.textarea
.onblur = function() {
572 self
.emit('blur', {terminal
: self
});
574 this.helperContainer
.appendChild(this.textarea
);
576 for (; i
< this.rows
; i
++) {
579 this.parent
.appendChild(this.element
);
582 this.refresh(0, this.rows
- 1);
584 // Initialize global actions that
585 // need to be taken on the document.
588 // Ensure there is a Terminal.focus.
591 // Start blinking the cursor.
594 on(this.element
, 'mouseup', function() {
595 var selection
= document
.getSelection(),
596 collapsed
= selection
.isCollapsed
,
597 isRange
= typeof collapsed
== 'boolean' ? !collapsed
: selection
.type
== 'Range';
603 // Listen for mouse events and translate
604 // them into terminal mouse protocols.
607 // Figure out whether boldness affects
608 // the character width of monospace fonts.
609 if (Terminal
.brokenBold
== null) {
610 Terminal
.brokenBold
= isBoldBroken(this.document
);
616 // XTerm mouse events
617 // http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#Mouse%20Tracking
618 // To better understand these
619 // the xterm code is very helpful:
621 // button.c, charproc.c, misc.c
622 // Relevant functions in xterm/button.c:
623 // BtnCode, EmitButtonCode, EditorButton, SendMousePosition
624 Terminal
.prototype.bindMouse = function() {
625 var el
= this.element
629 var wheelEvent
= 'onmousewheel' in this.context
633 // mouseup, mousedown, mousewheel
634 // left click: ^[[M 3<^[[M#3<
635 // mousewheel up: ^[[M`3>
636 function sendButton(ev
) {
640 // get the xterm-style button
641 button
= getButton(ev
);
643 // get mouse coordinates
647 sendEvent(button
, pos
);
654 // keep it at the left
655 // button, just in case.
666 // motion example of a left click:
667 // ^[[M 3<^[[M@4<^[[M@5<^[[M@6<^[[M@7<^[[M#7<
668 function sendMove(ev
) {
675 // buttons marked as motions
676 // are incremented by 32
679 sendEvent(button
, pos
);
683 // position to characters
684 function encode(data
, ch
) {
685 if (!self
.utfMouse
) {
686 if (ch
=== 255) return data
.push(0);
687 if (ch
> 127) ch
= 127;
690 if (ch
=== 2047) return data
.push(0);
694 if (ch
> 2047) ch
= 2047;
695 data
.push(0xC0 | (ch
>> 6));
696 data
.push(0x80 | (ch
& 0x3F));
701 // send a mouse event:
702 // regular/utf8: ^[[M Cb Cx Cy
703 // urxvt: ^[[ Cb ; Cx ; Cy M
704 // sgr: ^[[ Cb ; Cx ; Cy M/m
705 // vt300: ^[[ 24(1/3/5)~ [ Cx , Cy ] \r
706 // locator: CSI P e ; P b ; P r ; P c ; P p & w
707 function sendEvent(button
, pos
) {
708 // self.emit('mouse', {
714 if (self
.vt300Mouse
) {
716 // http://www.vt100.net/docs/vt3xx-gp/chapter15.html
720 var data
= '\x1b[24';
721 if (button
=== 0) data
+= '1';
722 else if (button
=== 1) data
+= '3';
723 else if (button
=== 2) data
+= '5';
724 else if (button
=== 3) return;
726 data
+= '~[' + pos
.x
+ ',' + pos
.y
+ ']\r';
731 if (self
.decLocator
) {
736 if (button
=== 0) button
= 2;
737 else if (button
=== 1) button
= 4;
738 else if (button
=== 2) button
= 6;
739 else if (button
=== 3) button
= 3;
743 + (button
=== 3 ? 4 : 0)
754 if (self
.urxvtMouse
) {
759 self
.send('\x1b[' + button
+ ';' + pos
.x
+ ';' + pos
.y
+ 'M');
767 + ((button
& 3) === 3 ? button
& ~3 : button
)
772 + ((button
& 3) === 3 ? 'm' : 'M'));
778 encode(data
, button
);
782 self
.send('\x1b[M' + String
.fromCharCode
.apply(String
, data
));
785 function getButton(ev
) {
798 // 1, and 2 - with 64 added
801 button
= ev
.button
!= null
808 button
= button
=== 1 ? 0 : button
=== 4 ? 1 : button
;
814 case 'DOMMouseScroll':
815 button
= ev
.detail
< 0
820 button
= ev
.wheelDeltaY
> 0
826 // next three bits are the modifiers:
827 // 4 = shift, 8 = meta, 16 = control
828 shift
= ev
.shiftKey
? 4 : 0;
829 meta
= ev
.metaKey
? 8 : 0;
830 ctrl
= ev
.ctrlKey
? 16 : 0;
831 mod
= shift
| meta
| ctrl
;
834 if (self
.vt200Mouse
) {
837 } else if (!self
.normalMouse
) {
842 button
= (32 + (mod
<< 2)) + button
;
847 // mouse coordinates measured in cols/rows
848 function getCoords(ev
) {
851 // ignore browsers without pageX for now
852 if (ev
.pageX
== null) return;
858 // should probably check offsetParent
859 // but this is more portable
860 while (el
&& el
!== self
.document
.documentElement
) {
863 el
= 'offsetParent' in el
868 // convert to cols/rows
869 w
= self
.element
.clientWidth
;
870 h
= self
.element
.clientHeight
;
871 x
= Math
.round((x
/ w
) * self
.cols
);
872 y
= Math
.round((y
/ h
) * self
.rows
);
874 // be sure to avoid sending
875 // bad positions to the program
877 if (x
> self
.cols
) x
= self
.cols
;
879 if (y
> self
.rows
) y
= self
.rows
;
881 // xterm sends raw bytes and
882 // starts at 32 (SP) for each.
889 type
: ev
.type
=== wheelEvent
895 on(el
, 'mousedown', function(ev
) {
896 if (!self
.mouseEvents
) return;
905 //if (self.vt200Mouse && !self.normalMouse) {
906 if (self
.vt200Mouse
) {
907 sendButton({ __proto__
: ev
, type
: 'mouseup' });
908 return self
.cancel(ev
);
912 if (self
.normalMouse
) on(self
.document
, 'mousemove', sendMove
);
914 // x10 compatibility mode can't send button releases
915 if (!self
.x10Mouse
) {
916 on(self
.document
, 'mouseup', function up(ev
) {
918 if (self
.normalMouse
) off(self
.document
, 'mousemove', sendMove
);
919 off(self
.document
, 'mouseup', up
);
920 return self
.cancel(ev
);
924 return self
.cancel(ev
);
927 //if (self.normalMouse) {
928 // on(self.document, 'mousemove', sendMove);
931 on(el
, wheelEvent
, function(ev
) {
932 if (!self
.mouseEvents
) return;
935 || self
.decLocator
) return;
937 return self
.cancel(ev
);
940 // allow mousewheel scrolling in
941 // the shell for example
942 on(el
, wheelEvent
, function(ev
) {
943 if (self
.mouseEvents
) return;
944 if (self
.applicationKeypad
) return;
945 if (ev
.type
=== 'DOMMouseScroll') {
946 self
.scrollDisp(ev
.detail
< 0 ? -1 : 1);
948 self
.scrollDisp(ev
.wheelDeltaY
> 0 ? -1 : 1);
950 return self
.cancel(ev
);
958 Terminal
.prototype.destroy = function() {
959 this.readable
= false;
960 this.writable
= false;
962 this.handler = function() {};
963 this.write = function() {};
964 if (this.element
.parentNode
) {
965 this.element
.parentNode
.removeChild(this.element
);
967 //this.emit('close');
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;
1040 if (!Terminal
.brokenBold
) {
1041 out
+= ' xterm-bold ';
1043 // See: XTerm*boldColors
1044 if (fg
< 8) fg
+= 8;
1049 out
+= ' xterm-underline ';
1054 out
+= ' xterm-blink ';
1059 bg
= (data
>> 9) & 0x1ff;
1061 // Should inverse just be before the
1062 // above boldColors effect instead?
1063 if ((flags
& 1) && fg
< 8) fg
+= 8;
1068 out
+= ' xterm-hidden ';
1072 out
+= ' xterm-bg-color-' + bg
+ ' ';
1076 out
+= ' xterm-color-' + fg
+ ' ';
1098 if (isWide(ch
)) i
++;
1107 if (attr
!== this.defAttr
) {
1111 this.children
[y
].innerHTML
= out
;
1115 parent
.appendChild(this.element
);
1119 * Return focus to previously focused element
1122 this.emit('refresh', {element
: this.element
, start
: start
, end
: end
});
1125 Terminal
.prototype._cursorBlink = function() {
1126 if (Terminal
.focus
!== this) return;
1127 this.cursorState
^= 1;
1128 this.refresh(this.y
, this.y
);
1131 Terminal
.prototype.showCursor = function() {
1132 if (!this.cursorState
) {
1133 this.cursorState
= 1;
1134 this.refresh(this.y
, this.y
);
1136 // Temporarily disabled:
1137 // this.refreshBlink();
1141 Terminal
.prototype.startBlink = function() {
1142 if (!this.cursorBlink
) return;
1144 this._blinker = function() {
1145 self
._cursorBlink();
1147 this._blink
= setInterval(this._blinker
, 500);
1150 Terminal
.prototype.refreshBlink = function() {
1151 if (!this.cursorBlink
) return;
1152 clearInterval(this._blink
);
1153 this._blink
= setInterval(this._blinker
, 500);
1156 Terminal
.prototype.scroll = function() {
1159 if (++this.ybase
=== this.scrollback
) {
1160 this.ybase
= this.ybase
/ 2 | 0;
1161 this.lines
= this.lines
.slice(-(this.ybase
+ this.rows
) + 1);
1164 this.ydisp
= this.ybase
;
1167 row
= this.ybase
+ this.rows
- 1;
1169 // subtract the bottom scroll region
1170 row
-= this.rows
- 1 - this.scrollBottom
;
1172 if (row
=== this.lines
.length
) {
1173 // potential optimization:
1174 // pushing is faster than splicing
1175 // when they amount to the same
1177 this.lines
.push(this.blankLine());
1180 this.lines
.splice(row
, 0, this.blankLine());
1183 if (this.scrollTop
!== 0) {
1184 if (this.ybase
!== 0) {
1186 this.ydisp
= this.ybase
;
1188 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
1192 this.updateRange(this.scrollTop
);
1193 this.updateRange(this.scrollBottom
);
1196 Terminal
.prototype.scrollDisp = function(disp
) {
1199 if (this.ydisp
> this.ybase
) {
1200 this.ydisp
= this.ybase
;
1201 } else if (this.ydisp
< 0) {
1205 this.refresh(0, this.rows
- 1);
1208 Terminal
.prototype.write = function(data
) {
1209 var l
= data
.length
, i
= 0, j
, cs
, ch
;
1211 this.refreshStart
= this.y
;
1212 this.refreshEnd
= this.y
;
1214 if (this.ybase
!== this.ydisp
) {
1215 this.ydisp
= this.ybase
;
1219 for (; i
< l
; i
++) {
1221 switch (this.state
) {
1232 if (this.convertEol
) {
1236 if (this.y
> this.scrollBottom
) {
1256 this.x
= this.nextStop();
1271 this.state
= escaped
;
1277 if (this.charset
&& this.charset
[ch
]) {
1278 ch
= this.charset
[ch
];
1281 if (this.x
>= this.cols
) {
1284 if (this.y
> this.scrollBottom
) {
1290 this.lines
[this.y
+ this.ybase
][this.x
] = [this.curAttr
, ch
];
1292 this.updateRange(this.y
);
1295 j
= this.y
+ this.ybase
;
1296 if (this.cols
< 2 || this.x
>= this.cols
) {
1297 this.lines
[j
][this.x
- 1] = [this.curAttr
, ' '];
1300 this.lines
[j
][this.x
] = [this.curAttr
, ' '];
1309 // ESC [ Control Sequence Introducer ( CSI is 0x9b).
1312 this.currentParam
= 0;
1316 // ESC ] Operating System Command ( OSC is 0x9d).
1319 this.currentParam
= 0;
1323 // ESC P Device Control String ( DCS is 0x90).
1326 this.currentParam
= 0;
1330 // ESC _ Application Program Command ( APC is 0x9f).
1332 this.state
= ignore
;
1335 // ESC ^ Privacy Message ( PM is 0x9e).
1337 this.state
= ignore
;
1340 // ESC c Full Reset (RIS).
1345 // ESC E Next Line ( NEL is 0x85).
1346 // ESC D Index ( IND is 0x84).
1354 // ESC M Reverse Index ( RI is 0x8d).
1356 this.reverseIndex();
1359 // ESC % Select default/utf-8 character set.
1360 // @ = default, G = utf-8
1362 //this.charset = null;
1364 this.setgCharset(0, Terminal
.charsets
.US
);
1365 this.state
= normal
;
1369 // ESC (,),*,+,-,. Designate G0-G2 Character Set.
1370 case '(': // <-- this seems to get all the attention
1396 this.state
= charset
;
1399 // Designate G3 Character Set (VT300).
1400 // A = ISO Latin-1 Supplemental.
1404 this.state
= charset
;
1409 // Single Shift Select of G2 Character Set
1410 // ( SS2 is 0x8e). This affects next character only.
1414 // Single Shift Select of G3 Character Set
1415 // ( SS3 is 0x8f). This affects next character only.
1419 // Invoke the G2 Character Set as GL (LS2).
1424 // Invoke the G3 Character Set as GL (LS3).
1429 // Invoke the G3 Character Set as GR (LS3R).
1434 // Invoke the G2 Character Set as GR (LS2R).
1439 // Invoke the G1 Character Set as GR (LS1R).
1444 // ESC 7 Save Cursor (DECSC).
1447 this.state
= normal
;
1450 // ESC 8 Restore Cursor (DECRC).
1452 this.restoreCursor();
1453 this.state
= normal
;
1456 // ESC # 3 DEC line height/width
1458 this.state
= normal
;
1462 // ESC H Tab Set (HTS is 0x88).
1467 // ESC = Application Keypad (DECPAM).
1469 this.log('Serial port requested application keypad.');
1470 this.applicationKeypad
= true;
1471 this.state
= normal
;
1474 // ESC > Normal Keypad (DECPNM).
1476 this.log('Switching back to normal keypad.');
1477 this.applicationKeypad
= false;
1478 this.state
= normal
;
1482 this.state
= normal
;
1483 this.error('Unknown ESC control: %s.', ch
);
1490 case '0': // DEC Special Character and Line Drawing Set.
1491 cs
= Terminal
.charsets
.SCLD
;
1494 cs
= Terminal
.charsets
.UK
;
1496 case 'B': // United States (USASCII).
1497 cs
= Terminal
.charsets
.US
;
1500 cs
= Terminal
.charsets
.Dutch
;
1502 case 'C': // Finnish
1504 cs
= Terminal
.charsets
.Finnish
;
1507 cs
= Terminal
.charsets
.French
;
1509 case 'Q': // FrenchCanadian
1510 cs
= Terminal
.charsets
.FrenchCanadian
;
1513 cs
= Terminal
.charsets
.German
;
1515 case 'Y': // Italian
1516 cs
= Terminal
.charsets
.Italian
;
1518 case 'E': // NorwegianDanish
1520 cs
= Terminal
.charsets
.NorwegianDanish
;
1522 case 'Z': // Spanish
1523 cs
= Terminal
.charsets
.Spanish
;
1525 case 'H': // Swedish
1527 cs
= Terminal
.charsets
.Swedish
;
1530 cs
= Terminal
.charsets
.Swiss
;
1532 case '/': // ISOLatin (actually /A)
1533 cs
= Terminal
.charsets
.ISOLatin
;
1537 cs
= Terminal
.charsets
.US
;
1540 this.setgCharset(this.gcharset
, cs
);
1541 this.gcharset
= null;
1542 this.state
= normal
;
1548 // Set Text Parameters.
1549 if (ch
=== '\x1b' || ch
=== '\x07') {
1550 if (ch
=== '\x1b') i
++;
1552 this.params
.push(this.currentParam
);
1554 switch (this.params
[0]) {
1558 if (this.params
[1]) {
1559 this.title
= this.params
[1];
1560 this.handleTitle(this.title
);
1568 // change dynamic colors
1580 // change dynamic ui colors
1592 // manipulate selection data
1610 this.currentParam
= 0;
1611 this.state
= normal
;
1613 if (!this.params
.length
) {
1614 if (ch
>= '0' && ch
<= '9') {
1616 this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1617 } else if (ch
=== ';') {
1618 this.params
.push(this.currentParam
);
1619 this.currentParam
= '';
1622 this.currentParam
+= ch
;
1629 if (ch
=== '?' || ch
=== '>' || ch
=== '!') {
1635 if (ch
>= '0' && ch
<= '9') {
1636 this.currentParam
= this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1640 // '$', '"', ' ', '\''
1641 if (ch
=== '$' || ch
=== '"' || ch
=== ' ' || ch
=== '\'') {
1646 this.params
.push(this.currentParam
);
1647 this.currentParam
= 0;
1650 if (ch
=== ';') break;
1652 this.state
= normal
;
1656 // Cursor Up Ps Times (default = 1) (CUU).
1658 this.cursorUp(this.params
);
1662 // Cursor Down Ps Times (default = 1) (CUD).
1664 this.cursorDown(this.params
);
1668 // Cursor Forward Ps Times (default = 1) (CUF).
1670 this.cursorForward(this.params
);
1674 // Cursor Backward Ps Times (default = 1) (CUB).
1676 this.cursorBackward(this.params
);
1680 // Cursor Position [row;column] (default = [1,1]) (CUP).
1682 this.cursorPos(this.params
);
1685 // CSI Ps J Erase in Display (ED).
1687 this.eraseInDisplay(this.params
);
1690 // CSI Ps K Erase in Line (EL).
1692 this.eraseInLine(this.params
);
1695 // CSI Pm m Character Attributes (SGR).
1698 this.charAttributes(this.params
);
1702 // CSI Ps n Device Status Report (DSR).
1705 this.deviceStatus(this.params
);
1714 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
1716 this.insertChars(this.params
);
1720 // Cursor Next Line Ps Times (default = 1) (CNL).
1722 this.cursorNextLine(this.params
);
1726 // Cursor Preceding Line Ps Times (default = 1) (CNL).
1728 this.cursorPrecedingLine(this.params
);
1732 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
1734 this.cursorCharAbsolute(this.params
);
1738 // Insert Ps Line(s) (default = 1) (IL).
1740 this.insertLines(this.params
);
1744 // Delete Ps Line(s) (default = 1) (DL).
1746 this.deleteLines(this.params
);
1750 // Delete Ps Character(s) (default = 1) (DCH).
1752 this.deleteChars(this.params
);
1756 // Erase Ps Character(s) (default = 1) (ECH).
1758 this.eraseChars(this.params
);
1761 // CSI Pm ` Character Position Absolute
1762 // [column] (default = [row,1]) (HPA).
1764 this.charPosAbsolute(this.params
);
1768 // Horizontal Position Relative
1770 this.HPositionRelative(this.params
);
1774 // Send Device Attributes (Primary DA).
1776 // Send Device Attributes (Secondary DA)
1778 this.sendDeviceAttributes(this.params
);
1782 // Line Position Absolute [row] (default = [1,column]) (VPA).
1784 this.linePosAbsolute(this.params
);
1787 // 145 65 e * VPR - Vertical Position Relative
1789 this.VPositionRelative(this.params
);
1793 // Horizontal and Vertical Position [row;column] (default =
1796 this.HVPosition(this.params
);
1799 // CSI Pm h Set Mode (SM).
1800 // CSI ? Pm h - mouse escape codes, cursor escape codes
1802 this.setMode(this.params
);
1805 // CSI Pm l Reset Mode (RM).
1808 this.resetMode(this.params
);
1812 // Set Scrolling Region [top;bottom] (default = full size of win-
1816 this.setScrollRegion(this.params
);
1820 // Save cursor (ANSI.SYS).
1822 this.saveCursor(this.params
);
1826 // Restore cursor (ANSI.SYS).
1828 this.restoreCursor(this.params
);
1836 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
1838 this.cursorForwardTab(this.params
);
1841 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
1843 this.scrollUp(this.params
);
1846 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
1847 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
1850 // if (this.prefix === '>') {
1851 // this.resetTitleModes(this.params);
1854 // if (this.params.length > 2) {
1855 // this.initMouseTracking(this.params);
1858 if (this.params
.length
< 2 && !this.prefix
) {
1859 this.scrollDown(this.params
);
1864 // Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
1866 this.cursorBackwardTab(this.params
);
1869 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
1871 this.repeatPrecedingCharacter(this.params
);
1874 // CSI Ps g Tab Clear (TBC).
1876 this.tabClear(this.params
);
1879 // CSI Pm i Media Copy (MC).
1882 // this.mediaCopy(this.params);
1885 // CSI Pm m Character Attributes (SGR).
1887 // case 'm': // duplicate
1888 // if (this.prefix === '>') {
1889 // this.setResources(this.params);
1891 // this.charAttributes(this.params);
1895 // CSI Ps n Device Status Report (DSR).
1897 // case 'n': // duplicate
1898 // if (this.prefix === '>') {
1899 // this.disableModifiers(this.params);
1901 // this.deviceStatus(this.params);
1905 // CSI > Ps p Set pointer mode.
1906 // CSI ! p Soft terminal reset (DECSTR).
1908 // Request ANSI mode (DECRQM).
1910 // Request DEC private mode (DECRQM).
1913 switch (this.prefix
) {
1915 // this.setPointerMode(this.params);
1918 this.softReset(this.params
);
1921 // if (this.postfix === '$') {
1922 // this.requestPrivateMode(this.params);
1926 // if (this.postfix === '"') {
1927 // this.setConformanceLevel(this.params);
1928 // } else if (this.postfix === '$') {
1929 // this.requestAnsiMode(this.params);
1935 // CSI Ps q Load LEDs (DECLL).
1939 // if (this.postfix === ' ') {
1940 // this.setCursorStyle(this.params);
1943 // if (this.postfix === '"') {
1944 // this.setCharProtectionAttr(this.params);
1947 // this.loadLEDs(this.params);
1951 // Set Scrolling Region [top;bottom] (default = full size of win-
1954 // CSI Pt; Pl; Pb; Pr; Ps$ r
1955 // case 'r': // duplicate
1956 // if (this.prefix === '?') {
1957 // this.restorePrivateValues(this.params);
1958 // } else if (this.postfix === '$') {
1959 // this.setAttrInRectangle(this.params);
1961 // this.setScrollRegion(this.params);
1965 // CSI s Save cursor (ANSI.SYS).
1967 // case 's': // duplicate
1968 // if (this.prefix === '?') {
1969 // this.savePrivateValues(this.params);
1971 // this.saveCursor(this.params);
1975 // CSI Ps ; Ps ; Ps t
1976 // CSI Pt; Pl; Pb; Pr; Ps$ t
1980 // if (this.postfix === '$') {
1981 // this.reverseAttrInRectangle(this.params);
1982 // } else if (this.postfix === ' ') {
1983 // this.setWarningBellVolume(this.params);
1985 // if (this.prefix === '>') {
1986 // this.setTitleModeFeature(this.params);
1988 // this.manipulateWindow(this.params);
1993 // CSI u Restore cursor (ANSI.SYS).
1995 // case 'u': // duplicate
1996 // if (this.postfix === ' ') {
1997 // this.setMarginBellVolume(this.params);
1999 // this.restoreCursor(this.params);
2003 // CSI Pt; Pl; Pb; Pr; Pp; Pt; Pl; Pp$ v
2005 // if (this.postfix === '$') {
2006 // this.copyRectagle(this.params);
2010 // CSI Pt ; Pl ; Pb ; Pr ' w
2012 // if (this.postfix === '\'') {
2013 // this.enableFilterRectangle(this.params);
2017 // CSI Ps x Request Terminal Parameters (DECREQTPARM).
2018 // CSI Ps x Select Attribute Change Extent (DECSACE).
2019 // CSI Pc; Pt; Pl; Pb; Pr$ x
2021 // if (this.postfix === '$') {
2022 // this.fillRectangle(this.params);
2024 // this.requestParameters(this.params);
2025 // //this.__(this.params);
2030 // CSI Pt; Pl; Pb; Pr$ z
2032 // if (this.postfix === '\'') {
2033 // this.enableLocatorReporting(this.params);
2034 // } else if (this.postfix === '$') {
2035 // this.eraseRectangle(this.params);
2040 // CSI Pt; Pl; Pb; Pr$ {
2042 // if (this.postfix === '\'') {
2043 // this.setLocatorEvents(this.params);
2044 // } else if (this.postfix === '$') {
2045 // this.selectiveEraseRectangle(this.params);
2051 // if (this.postfix === '\'') {
2052 // this.requestLocatorPosition(this.params);
2057 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
2059 // if (this.postfix === ' ') {
2060 // this.insertColumns(this.params);
2065 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
2067 // if (this.postfix === ' ') {
2068 // this.deleteColumns(this.params);
2073 this.error('Unknown CSI code: %s.', ch
);
2082 if (ch
=== '\x1b' || ch
=== '\x07') {
2083 if (ch
=== '\x1b') i
++;
2085 switch (this.prefix
) {
2086 // User-Defined Keys (DECUDK).
2090 // Request Status String (DECRQSS).
2091 // test: echo -e '\eP$q"p\e\\'
2093 var pt
= this.currentParam
2110 + (this.scrollTop
+ 1)
2112 + (this.scrollBottom
+ 1)
2122 this.error('Unknown DCS Pt: %s.', pt
);
2127 this.send('\x1bP' + +valid
+ '$r' + pt
+ '\x1b\\');
2130 // Set Termcap/Terminfo Data (xterm, experimental).
2134 // Request Termcap/Terminfo String (xterm, experimental)
2135 // Regular xterm does not even respond to this sequence.
2136 // This can cause a small glitch in vim.
2137 // test: echo -ne '\eP+q6b64\e\\'
2139 var pt
= this.currentParam
2142 this.send('\x1bP' + +valid
+ '+r' + pt
+ '\x1b\\');
2146 this.error('Unknown DCS prefix: %s.', this.prefix
);
2150 this.currentParam
= 0;
2152 this.state
= normal
;
2153 } else if (!this.currentParam
) {
2154 if (!this.prefix
&& ch
!== '$' && ch
!== '+') {
2155 this.currentParam
= ch
;
2156 } else if (this.prefix
.length
=== 2) {
2157 this.currentParam
= ch
;
2162 this.currentParam
+= ch
;
2168 if (ch
=== '\x1b' || ch
=== '\x07') {
2169 if (ch
=== '\x1b') i
++;
2170 this.state
= normal
;
2176 this.updateRange(this.y
);
2177 this.refresh(this.refreshStart
, this.refreshEnd
);
2180 Terminal
.prototype.writeln = function(data
) {
2181 this.write(data
+ '\r\n');
2185 // https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
2186 Terminal
.prototype.keyDown = function(ev
) {
2187 var self
= this, key
;
2189 switch (ev
.keyCode
) {
2205 this.cancel(ev
, true);
2210 this.cancel(ev
, true);
2215 this.cancel(ev
, true);
2220 this.cancel(ev
, true);
2221 key
= '\x1bb' // Jump a word back
2223 } else if (this.applicationCursor
) {
2224 key
= '\x1bOD'; // SS3 as ^[O for 7-bit
2232 this.cancel(ev
, true);
2233 key
= '\x1bf' // Jump a word forward
2235 } else if (this.applicationCursor
) {
2243 if (this.applicationCursor
) {
2248 this.scrollDisp(-1);
2249 return this.cancel(ev
);
2256 if (this.applicationCursor
) {
2262 return this.cancel(ev
);
2277 if (this.applicationKeypad
) {
2285 if (this.applicationKeypad
) {
2294 this.scrollDisp(-(this.rows
- 1));
2295 return this.cancel(ev
);
2303 this.scrollDisp(this.rows
- 1);
2304 return this.cancel(ev
);
2359 if (ev
.ctrlKey
&& !ev
.shiftKey
&& !ev
.altKey
&& !ev
.metaKey
) {
2360 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2361 key
= String
.fromCharCode(ev
.keyCode
- 64);
2362 } else if (ev
.keyCode
=== 32) {
2364 key
= String
.fromCharCode(0);
2365 } else if (ev
.keyCode
>= 51 && ev
.keyCode
<= 55) {
2366 // escape, file sep, group sep, record sep, unit sep
2367 key
= String
.fromCharCode(ev
.keyCode
- 51 + 27);
2368 } else if (ev
.keyCode
=== 56) {
2370 key
= String
.fromCharCode(127);
2371 } else if (ev
.keyCode
=== 219) {
2373 key
= String
.fromCharCode(27);
2374 } else if (ev
.keyCode
=== 221) {
2376 key
= String
.fromCharCode(29);
2378 } else if ((!this.isMac
&& ev
.altKey
) || (this.isMac
&& ev
.metaKey
)) {
2379 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2380 key
= '\x1b' + String
.fromCharCode(ev
.keyCode
+ 32);
2381 } else if (ev
.keyCode
=== 192) {
2383 } else if (ev
.keyCode
>= 48 && ev
.keyCode
<= 57) {
2384 key
= '\x1b' + (ev
.keyCode
- 48);
2390 if (!key
|| (this.isMac
&& ev
.metaKey
)) {
2394 this.emit('keydown', ev
);
2395 this.emit('key', key
, ev
);
2399 return this.cancel(ev
);
2402 Terminal
.prototype.setgLevel = function(g
) {
2404 this.charset
= this.charsets
[g
];
2407 Terminal
.prototype.setgCharset = function(g
, charset
) {
2408 this.charsets
[g
] = charset
;
2409 if (this.glevel
=== g
) {
2410 this.charset
= charset
;
2414 Terminal
.prototype.keyPress = function(ev
) {
2421 } else if (ev
.which
== null) {
2423 } else if (ev
.which
!== 0 && ev
.charCode
!== 0) {
2429 if (!key
|| ev
.ctrlKey
|| ev
.altKey
|| ev
.metaKey
) {
2433 key
= String
.fromCharCode(key
);
2435 this.emit('keypress', key
, ev
);
2436 this.emit('key', key
, ev
);
2443 Terminal
.prototype.send = function(data
) {
2447 setTimeout(function() {
2448 self
.handler(self
.queue
);
2456 Terminal
.prototype.bell = function() {
2457 if (!this.visualBell
) return;
2459 this.element
.style
.borderColor
= 'white';
2460 setTimeout(function() {
2461 self
.element
.style
.borderColor
= '';
2463 if (this.popOnBell
) this.focus();
2466 Terminal
.prototype.log = function() {
2467 if (!this.debug
) return;
2468 if (!this.context
.console
|| !this.context
.console
.log
) return;
2469 var args
= Array
.prototype.slice
.call(arguments
);
2470 this.context
.console
.log
.apply(this.context
.console
, args
);
2473 Terminal
.prototype.error = function() {
2474 if (!this.debug
) return;
2475 if (!this.context
.console
|| !this.context
.console
.error
) return;
2476 var args
= Array
.prototype.slice
.call(arguments
);
2477 this.context
.console
.error
.apply(this.context
.console
, args
);
2480 Terminal
.prototype.resize = function(x
, y
) {
2493 ch
= [this.defAttr
, ' ']; // does xterm use the default attr?
2494 i
= this.lines
.length
;
2496 while (this.lines
[i
].length
< x
) {
2497 this.lines
[i
].push(ch
);
2501 i
= this.lines
.length
;
2503 while (this.lines
[i
].length
> x
) {
2504 this.lines
[i
].pop();
2516 if (this.lines
.length
< y
+ this.ybase
) {
2517 this.lines
.push(this.blankLine());
2519 if (this.children
.length
< y
) {
2525 if (this.lines
.length
> y
+ this.ybase
) {
2528 if (this.children
.length
> y
) {
2529 el
= this.children
.shift();
2531 el
.parentNode
.removeChild(el
);
2538 * Make sure that the cursor stays on screen
2549 this.scrollBottom
= y
- 1;
2551 this.refresh(0, this.rows
- 1);
2555 this.emit('resize', {terminal
: this, cols
: x
, rows
: y
});
2558 Terminal
.prototype.updateRange = function(y
) {
2559 if (y
< this.refreshStart
) this.refreshStart
= y
;
2560 if (y
> this.refreshEnd
) this.refreshEnd
= y
;
2561 // if (y > this.refreshEnd) {
2562 // this.refreshEnd = y;
2563 // if (y > this.rows - 1) {
2564 // this.refreshEnd = this.rows - 1;
2569 Terminal
.prototype.maxRange = function() {
2570 this.refreshStart
= 0;
2571 this.refreshEnd
= this.rows
- 1;
2574 Terminal
.prototype.setupStops = function(i
) {
2576 if (!this.tabs
[i
]) {
2577 i
= this.prevStop(i
);
2584 for (; i
< this.cols
; i
+= 8) {
2585 this.tabs
[i
] = true;
2589 Terminal
.prototype.prevStop = function(x
) {
2590 if (x
== null) x
= this.x
;
2591 while (!this.tabs
[--x
] && x
> 0);
2592 return x
>= this.cols
2597 Terminal
.prototype.nextStop = function(x
) {
2598 if (x
== null) x
= this.x
;
2599 while (!this.tabs
[++x
] && x
< this.cols
);
2600 return x
>= this.cols
2605 Terminal
.prototype.eraseRight = function(x
, y
) {
2606 var line
= this.lines
[this.ybase
+ y
]
2607 , ch
= [this.eraseAttr(), ' ']; // xterm
2610 for (; x
< this.cols
; x
++) {
2614 this.updateRange(y
);
2617 Terminal
.prototype.eraseLeft = function(x
, y
) {
2618 var line
= this.lines
[this.ybase
+ y
]
2619 , ch
= [this.eraseAttr(), ' ']; // xterm
2622 while (x
--) line
[x
] = ch
;
2624 this.updateRange(y
);
2627 Terminal
.prototype.eraseLine = function(y
) {
2628 this.eraseRight(0, y
);
2631 Terminal
.prototype.blankLine = function(cur
) {
2636 var ch
= [attr
, ' ']
2640 for (; i
< this.cols
; i
++) {
2647 Terminal
.prototype.ch = function(cur
) {
2649 ? [this.eraseAttr(), ' ']
2650 : [this.defAttr
, ' '];
2653 Terminal
.prototype.is = function(term
) {
2654 var name
= this.termName
;
2655 return (name
+ '').indexOf(term
) === 0;
2658 Terminal
.prototype.handler = function(data
) {
2659 this.emit('data', data
);
2662 Terminal
.prototype.handleTitle = function(title
) {
2663 this.emit('title', title
);
2670 // ESC D Index (IND is 0x84).
2671 Terminal
.prototype.index = function() {
2673 if (this.y
> this.scrollBottom
) {
2677 this.state
= normal
;
2680 // ESC M Reverse Index (RI is 0x8d).
2681 Terminal
.prototype.reverseIndex = function() {
2684 if (this.y
< this.scrollTop
) {
2686 // possibly move the code below to term.reverseScroll();
2687 // test: echo -ne '\e[1;1H\e[44m\eM\e[0m'
2688 // blankLine(true) is xterm/linux behavior
2689 this.lines
.splice(this.y
+ this.ybase
, 0, this.blankLine(true));
2690 j
= this.rows
- 1 - this.scrollBottom
;
2691 this.lines
.splice(this.rows
- 1 + this.ybase
- j
+ 1, 1);
2693 this.updateRange(this.scrollTop
);
2694 this.updateRange(this.scrollBottom
);
2696 this.state
= normal
;
2699 // ESC c Full Reset (RIS).
2700 Terminal
.prototype.reset = function() {
2701 this.options
.rows
= this.rows
;
2702 this.options
.cols
= this.cols
;
2703 Terminal
.call(this, this.options
);
2704 this.refresh(0, this.rows
- 1);
2707 // ESC H Tab Set (HTS is 0x88).
2708 Terminal
.prototype.tabSet = function() {
2709 this.tabs
[this.x
] = true;
2710 this.state
= normal
;
2718 // Cursor Up Ps Times (default = 1) (CUU).
2719 Terminal
.prototype.cursorUp = function(params
) {
2720 var param
= params
[0];
2721 if (param
< 1) param
= 1;
2723 if (this.y
< 0) this.y
= 0;
2727 // Cursor Down Ps Times (default = 1) (CUD).
2728 Terminal
.prototype.cursorDown = function(params
) {
2729 var param
= params
[0];
2730 if (param
< 1) param
= 1;
2732 if (this.y
>= this.rows
) {
2733 this.y
= this.rows
- 1;
2738 // Cursor Forward Ps Times (default = 1) (CUF).
2739 Terminal
.prototype.cursorForward = function(params
) {
2740 var param
= params
[0];
2741 if (param
< 1) param
= 1;
2743 if (this.x
>= this.cols
) {
2744 this.x
= this.cols
- 1;
2749 // Cursor Backward Ps Times (default = 1) (CUB).
2750 Terminal
.prototype.cursorBackward = function(params
) {
2751 var param
= params
[0];
2752 if (param
< 1) param
= 1;
2754 if (this.x
< 0) this.x
= 0;
2758 // Cursor Position [row;column] (default = [1,1]) (CUP).
2759 Terminal
.prototype.cursorPos = function(params
) {
2762 row
= params
[0] - 1;
2764 if (params
.length
>= 2) {
2765 col
= params
[1] - 1;
2772 } else if (row
>= this.rows
) {
2773 row
= this.rows
- 1;
2778 } else if (col
>= this.cols
) {
2779 col
= this.cols
- 1;
2786 // CSI Ps J Erase in Display (ED).
2787 // Ps = 0 -> Erase Below (default).
2788 // Ps = 1 -> Erase Above.
2789 // Ps = 2 -> Erase All.
2790 // Ps = 3 -> Erase Saved Lines (xterm).
2792 // Erase in Display (DECSED).
2793 // Ps = 0 -> Selective Erase Below (default).
2794 // Ps = 1 -> Selective Erase Above.
2795 // Ps = 2 -> Selective Erase All.
2796 Terminal
.prototype.eraseInDisplay = function(params
) {
2798 switch (params
[0]) {
2800 this.eraseRight(this.x
, this.y
);
2802 for (; j
< this.rows
; j
++) {
2807 this.eraseLeft(this.x
, this.y
);
2815 while (j
--) this.eraseLine(j
);
2823 // CSI Ps K Erase in Line (EL).
2824 // Ps = 0 -> Erase to Right (default).
2825 // Ps = 1 -> Erase to Left.
2826 // Ps = 2 -> Erase All.
2828 // Erase in Line (DECSEL).
2829 // Ps = 0 -> Selective Erase to Right (default).
2830 // Ps = 1 -> Selective Erase to Left.
2831 // Ps = 2 -> Selective Erase All.
2832 Terminal
.prototype.eraseInLine = function(params
) {
2833 switch (params
[0]) {
2835 this.eraseRight(this.x
, this.y
);
2838 this.eraseLeft(this.x
, this.y
);
2841 this.eraseLine(this.y
);
2846 // CSI Pm m Character Attributes (SGR).
2847 // Ps = 0 -> Normal (default).
2849 // Ps = 4 -> Underlined.
2850 // Ps = 5 -> Blink (appears as Bold).
2851 // Ps = 7 -> Inverse.
2852 // Ps = 8 -> Invisible, i.e., hidden (VT300).
2853 // Ps = 2 2 -> Normal (neither bold nor faint).
2854 // Ps = 2 4 -> Not underlined.
2855 // Ps = 2 5 -> Steady (not blinking).
2856 // Ps = 2 7 -> Positive (not inverse).
2857 // Ps = 2 8 -> Visible, i.e., not hidden (VT300).
2858 // Ps = 3 0 -> Set foreground color to Black.
2859 // Ps = 3 1 -> Set foreground color to Red.
2860 // Ps = 3 2 -> Set foreground color to Green.
2861 // Ps = 3 3 -> Set foreground color to Yellow.
2862 // Ps = 3 4 -> Set foreground color to Blue.
2863 // Ps = 3 5 -> Set foreground color to Magenta.
2864 // Ps = 3 6 -> Set foreground color to Cyan.
2865 // Ps = 3 7 -> Set foreground color to White.
2866 // Ps = 3 9 -> Set foreground color to default (original).
2867 // Ps = 4 0 -> Set background color to Black.
2868 // Ps = 4 1 -> Set background color to Red.
2869 // Ps = 4 2 -> Set background color to Green.
2870 // Ps = 4 3 -> Set background color to Yellow.
2871 // Ps = 4 4 -> Set background color to Blue.
2872 // Ps = 4 5 -> Set background color to Magenta.
2873 // Ps = 4 6 -> Set background color to Cyan.
2874 // Ps = 4 7 -> Set background color to White.
2875 // Ps = 4 9 -> Set background color to default (original).
2877 // If 16-color support is compiled, the following apply. Assume
2878 // that xterm's resources are set so that the ISO color codes are
2879 // the first 8 of a set of 16. Then the aixterm colors are the
2880 // bright versions of the ISO colors:
2881 // Ps = 9 0 -> Set foreground color to Black.
2882 // Ps = 9 1 -> Set foreground color to Red.
2883 // Ps = 9 2 -> Set foreground color to Green.
2884 // Ps = 9 3 -> Set foreground color to Yellow.
2885 // Ps = 9 4 -> Set foreground color to Blue.
2886 // Ps = 9 5 -> Set foreground color to Magenta.
2887 // Ps = 9 6 -> Set foreground color to Cyan.
2888 // Ps = 9 7 -> Set foreground color to White.
2889 // Ps = 1 0 0 -> Set background color to Black.
2890 // Ps = 1 0 1 -> Set background color to Red.
2891 // Ps = 1 0 2 -> Set background color to Green.
2892 // Ps = 1 0 3 -> Set background color to Yellow.
2893 // Ps = 1 0 4 -> Set background color to Blue.
2894 // Ps = 1 0 5 -> Set background color to Magenta.
2895 // Ps = 1 0 6 -> Set background color to Cyan.
2896 // Ps = 1 0 7 -> Set background color to White.
2898 // If xterm is compiled with the 16-color support disabled, it
2899 // supports the following, from rxvt:
2900 // Ps = 1 0 0 -> Set foreground and background color to
2903 // If 88- or 256-color support is compiled, the following apply.
2904 // Ps = 3 8 ; 5 ; Ps -> Set foreground color to the second
2906 // Ps = 4 8 ; 5 ; Ps -> Set background color to the second
2908 Terminal
.prototype.charAttributes = function(params
) {
2909 // Optimize a single SGR0.
2910 if (params
.length
=== 1 && params
[0] === 0) {
2911 this.curAttr
= this.defAttr
;
2915 var l
= params
.length
2917 , flags
= this.curAttr
>> 18
2918 , fg
= (this.curAttr
>> 9) & 0x1ff
2919 , bg
= this.curAttr
& 0x1ff
2922 for (; i
< l
; i
++) {
2924 if (p
>= 30 && p
<= 37) {
2927 } else if (p
>= 40 && p
<= 47) {
2930 } else if (p
>= 90 && p
<= 97) {
2934 } else if (p
>= 100 && p
<= 107) {
2938 } else if (p
=== 0) {
2940 flags
= this.defAttr
>> 18;
2941 fg
= (this.defAttr
>> 9) & 0x1ff;
2942 bg
= this.defAttr
& 0x1ff;
2946 } else if (p
=== 1) {
2949 } else if (p
=== 4) {
2952 } else if (p
=== 5) {
2955 } else if (p
=== 7) {
2956 // inverse and positive
2957 // test with: echo -e '\e[31m\e[42mhello\e[7mworld\e[27mhi\e[m'
2959 } else if (p
=== 8) {
2962 } else if (p
=== 22) {
2965 } else if (p
=== 24) {
2968 } else if (p
=== 25) {
2971 } else if (p
=== 27) {
2974 } else if (p
=== 28) {
2977 } else if (p
=== 39) {
2979 fg
= (this.defAttr
>> 9) & 0x1ff;
2980 } else if (p
=== 49) {
2982 bg
= this.defAttr
& 0x1ff;
2983 } else if (p
=== 38) {
2985 if (params
[i
+ 1] === 2) {
2989 params
[i
+ 1] & 0xff,
2990 params
[i
+ 2] & 0xff);
2991 if (fg
=== -1) fg
= 0x1ff;
2993 } else if (params
[i
+ 1] === 5) {
2995 p
= params
[i
] & 0xff;
2998 } else if (p
=== 48) {
3000 if (params
[i
+ 1] === 2) {
3004 params
[i
+ 1] & 0xff,
3005 params
[i
+ 2] & 0xff);
3006 if (bg
=== -1) bg
= 0x1ff;
3008 } else if (params
[i
+ 1] === 5) {
3010 p
= params
[i
] & 0xff;
3013 } else if (p
=== 100) {
3015 fg
= (this.defAttr
>> 9) & 0x1ff;
3016 bg
= this.defAttr
& 0x1ff;
3018 this.error('Unknown SGR attribute: %d.', p
);
3022 this.curAttr
= (flags
<< 18) | (fg
<< 9) | bg
;
3025 // CSI Ps n Device Status Report (DSR).
3026 // Ps = 5 -> Status Report. Result (``OK'') is
3028 // Ps = 6 -> Report Cursor Position (CPR) [row;column].
3032 // Device Status Report (DSR, DEC-specific).
3033 // Ps = 6 -> Report Cursor Position (CPR) [row;column] as CSI
3034 // ? r ; c R (assumes page is zero).
3035 // Ps = 1 5 -> Report Printer status as CSI ? 1 0 n (ready).
3036 // or CSI ? 1 1 n (not ready).
3037 // Ps = 2 5 -> Report UDK status as CSI ? 2 0 n (unlocked)
3038 // or CSI ? 2 1 n (locked).
3039 // Ps = 2 6 -> Report Keyboard status as
3040 // CSI ? 2 7 ; 1 ; 0 ; 0 n (North American).
3041 // The last two parameters apply to VT400 & up, and denote key-
3042 // board ready and LK01 respectively.
3043 // Ps = 5 3 -> Report Locator status as
3044 // CSI ? 5 3 n Locator available, if compiled-in, or
3045 // CSI ? 5 0 n No Locator, if not.
3046 Terminal
.prototype.deviceStatus = function(params
) {
3048 switch (params
[0]) {
3051 this.send('\x1b[0n');
3062 } else if (this.prefix
=== '?') {
3063 // modern xterm doesnt seem to
3064 // respond to any of these except ?6, 6, and 5
3065 switch (params
[0]) {
3076 // this.send('\x1b[?11n');
3079 // dont support user defined keys
3080 // this.send('\x1b[?21n');
3083 // north american keyboard
3084 // this.send('\x1b[?27;1;0;0n');
3087 // no dec locator/mouse
3088 // this.send('\x1b[?50n');
3099 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
3100 Terminal
.prototype.insertChars = function(params
) {
3101 var param
, row
, j
, ch
;
3104 if (param
< 1) param
= 1;
3106 row
= this.y
+ this.ybase
;
3108 ch
= [this.eraseAttr(), ' ']; // xterm
3110 while (param
-- && j
< this.cols
) {
3111 this.lines
[row
].splice(j
++, 0, ch
);
3112 this.lines
[row
].pop();
3117 // Cursor Next Line Ps Times (default = 1) (CNL).
3118 // same as CSI Ps B ?
3119 Terminal
.prototype.cursorNextLine = function(params
) {
3120 var param
= params
[0];
3121 if (param
< 1) param
= 1;
3123 if (this.y
>= this.rows
) {
3124 this.y
= this.rows
- 1;
3130 // Cursor Preceding Line Ps Times (default = 1) (CNL).
3132 Terminal
.prototype.cursorPrecedingLine = function(params
) {
3133 var param
= params
[0];
3134 if (param
< 1) param
= 1;
3136 if (this.y
< 0) this.y
= 0;
3141 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
3142 Terminal
.prototype.cursorCharAbsolute = function(params
) {
3143 var param
= params
[0];
3144 if (param
< 1) param
= 1;
3149 // Insert Ps Line(s) (default = 1) (IL).
3150 Terminal
.prototype.insertLines = function(params
) {
3154 if (param
< 1) param
= 1;
3155 row
= this.y
+ this.ybase
;
3157 j
= this.rows
- 1 - this.scrollBottom
;
3158 j
= this.rows
- 1 + this.ybase
- j
+ 1;
3161 // test: echo -e '\e[44m\e[1L\e[0m'
3162 // blankLine(true) - xterm/linux behavior
3163 this.lines
.splice(row
, 0, this.blankLine(true));
3164 this.lines
.splice(j
, 1);
3168 this.updateRange(this.y
);
3169 this.updateRange(this.scrollBottom
);
3173 // Delete Ps Line(s) (default = 1) (DL).
3174 Terminal
.prototype.deleteLines = function(params
) {
3178 if (param
< 1) param
= 1;
3179 row
= this.y
+ this.ybase
;
3181 j
= this.rows
- 1 - this.scrollBottom
;
3182 j
= this.rows
- 1 + this.ybase
- j
;
3185 // test: echo -e '\e[44m\e[1M\e[0m'
3186 // blankLine(true) - xterm/linux behavior
3187 this.lines
.splice(j
+ 1, 0, this.blankLine(true));
3188 this.lines
.splice(row
, 1);
3192 this.updateRange(this.y
);
3193 this.updateRange(this.scrollBottom
);
3197 // Delete Ps Character(s) (default = 1) (DCH).
3198 Terminal
.prototype.deleteChars = function(params
) {
3202 if (param
< 1) param
= 1;
3204 row
= this.y
+ this.ybase
;
3205 ch
= [this.eraseAttr(), ' ']; // xterm
3208 this.lines
[row
].splice(this.x
, 1);
3209 this.lines
[row
].push(ch
);
3214 // Erase Ps Character(s) (default = 1) (ECH).
3215 Terminal
.prototype.eraseChars = function(params
) {
3216 var param
, row
, j
, ch
;
3219 if (param
< 1) param
= 1;
3221 row
= this.y
+ this.ybase
;
3223 ch
= [this.eraseAttr(), ' ']; // xterm
3225 while (param
-- && j
< this.cols
) {
3226 this.lines
[row
][j
++] = ch
;
3230 // CSI Pm ` Character Position Absolute
3231 // [column] (default = [row,1]) (HPA).
3232 Terminal
.prototype.charPosAbsolute = function(params
) {
3233 var param
= params
[0];
3234 if (param
< 1) param
= 1;
3236 if (this.x
>= this.cols
) {
3237 this.x
= this.cols
- 1;
3242 // Horizontal Position Relative
3244 Terminal
.prototype.HPositionRelative = function(params
) {
3245 var param
= params
[0];
3246 if (param
< 1) param
= 1;
3248 if (this.x
>= this.cols
) {
3249 this.x
= this.cols
- 1;
3253 // CSI Ps c Send Device Attributes (Primary DA).
3254 // Ps = 0 or omitted -> request attributes from terminal. The
3255 // response depends on the decTerminalID resource setting.
3256 // -> CSI ? 1 ; 2 c (``VT100 with Advanced Video Option'')
3257 // -> CSI ? 1 ; 0 c (``VT101 with No Options'')
3258 // -> CSI ? 6 c (``VT102'')
3259 // -> CSI ? 6 0 ; 1 ; 2 ; 6 ; 8 ; 9 ; 1 5 ; c (``VT220'')
3260 // The VT100-style response parameters do not mean anything by
3261 // themselves. VT220 parameters do, telling the host what fea-
3262 // tures the terminal supports:
3263 // Ps = 1 -> 132-columns.
3264 // Ps = 2 -> Printer.
3265 // Ps = 6 -> Selective erase.
3266 // Ps = 8 -> User-defined keys.
3267 // Ps = 9 -> National replacement character sets.
3268 // Ps = 1 5 -> Technical characters.
3269 // Ps = 2 2 -> ANSI color, e.g., VT525.
3270 // Ps = 2 9 -> ANSI text locator (i.e., DEC Locator mode).
3272 // Send Device Attributes (Secondary DA).
3273 // Ps = 0 or omitted -> request the terminal's identification
3274 // code. The response depends on the decTerminalID resource set-
3275 // ting. It should apply only to VT220 and up, but xterm extends
3277 // -> CSI > Pp ; Pv ; Pc c
3278 // where Pp denotes the terminal type
3279 // Pp = 0 -> ``VT100''.
3280 // Pp = 1 -> ``VT220''.
3281 // and Pv is the firmware version (for xterm, this was originally
3282 // the XFree86 patch number, starting with 95). In a DEC termi-
3283 // nal, Pc indicates the ROM cartridge registration number and is
3285 // More information:
3286 // xterm/charproc.c - line 2012, for more information.
3287 // vim responds with ^[[?0c or ^[[?1c after the terminal's response (?)
3288 Terminal
.prototype.sendDeviceAttributes = function(params
) {
3289 if (params
[0] > 0) return;
3292 if (this.is('xterm')
3293 || this.is('rxvt-unicode')
3294 || this.is('screen')) {
3295 this.send('\x1b[?1;2c');
3296 } else if (this.is('linux')) {
3297 this.send('\x1b[?6c');
3299 } else if (this.prefix
=== '>') {
3301 // seem to spit this
3302 // out around ~370 times (?).
3303 if (this.is('xterm')) {
3304 this.send('\x1b[>0;276;0c');
3305 } else if (this.is('rxvt-unicode')) {
3306 this.send('\x1b[>85;95;0c');
3307 } else if (this.is('linux')) {
3308 // not supported by linux console.
3309 // linux console echoes parameters.
3310 this.send(params
[0] + 'c');
3311 } else if (this.is('screen')) {
3312 this.send('\x1b[>83;40003;0c');
3318 // Line Position Absolute [row] (default = [1,column]) (VPA).
3319 Terminal
.prototype.linePosAbsolute = function(params
) {
3320 var param
= params
[0];
3321 if (param
< 1) param
= 1;
3323 if (this.y
>= this.rows
) {
3324 this.y
= this.rows
- 1;
3328 // 145 65 e * VPR - Vertical Position Relative
3330 Terminal
.prototype.VPositionRelative = function(params
) {
3331 var param
= params
[0];
3332 if (param
< 1) param
= 1;
3334 if (this.y
>= this.rows
) {
3335 this.y
= this.rows
- 1;
3340 // Horizontal and Vertical Position [row;column] (default =
3342 Terminal
.prototype.HVPosition = function(params
) {
3343 if (params
[0] < 1) params
[0] = 1;
3344 if (params
[1] < 1) params
[1] = 1;
3346 this.y
= params
[0] - 1;
3347 if (this.y
>= this.rows
) {
3348 this.y
= this.rows
- 1;
3351 this.x
= params
[1] - 1;
3352 if (this.x
>= this.cols
) {
3353 this.x
= this.cols
- 1;
3357 // CSI Pm h Set Mode (SM).
3358 // Ps = 2 -> Keyboard Action Mode (AM).
3359 // Ps = 4 -> Insert Mode (IRM).
3360 // Ps = 1 2 -> Send/receive (SRM).
3361 // Ps = 2 0 -> Automatic Newline (LNM).
3363 // DEC Private Mode Set (DECSET).
3364 // Ps = 1 -> Application Cursor Keys (DECCKM).
3365 // Ps = 2 -> Designate USASCII for character sets G0-G3
3366 // (DECANM), and set VT100 mode.
3367 // Ps = 3 -> 132 Column Mode (DECCOLM).
3368 // Ps = 4 -> Smooth (Slow) Scroll (DECSCLM).
3369 // Ps = 5 -> Reverse Video (DECSCNM).
3370 // Ps = 6 -> Origin Mode (DECOM).
3371 // Ps = 7 -> Wraparound Mode (DECAWM).
3372 // Ps = 8 -> Auto-repeat Keys (DECARM).
3373 // Ps = 9 -> Send Mouse X & Y on button press. See the sec-
3374 // tion Mouse Tracking.
3375 // Ps = 1 0 -> Show toolbar (rxvt).
3376 // Ps = 1 2 -> Start Blinking Cursor (att610).
3377 // Ps = 1 8 -> Print form feed (DECPFF).
3378 // Ps = 1 9 -> Set print extent to full screen (DECPEX).
3379 // Ps = 2 5 -> Show Cursor (DECTCEM).
3380 // Ps = 3 0 -> Show scrollbar (rxvt).
3381 // Ps = 3 5 -> Enable font-shifting functions (rxvt).
3382 // Ps = 3 8 -> Enter Tektronix Mode (DECTEK).
3383 // Ps = 4 0 -> Allow 80 -> 132 Mode.
3384 // Ps = 4 1 -> more(1) fix (see curses resource).
3385 // Ps = 4 2 -> Enable Nation Replacement Character sets (DECN-
3387 // Ps = 4 4 -> Turn On Margin Bell.
3388 // Ps = 4 5 -> Reverse-wraparound Mode.
3389 // Ps = 4 6 -> Start Logging. This is normally disabled by a
3390 // compile-time option.
3391 // Ps = 4 7 -> Use Alternate Screen Buffer. (This may be dis-
3392 // abled by the titeInhibit resource).
3393 // Ps = 6 6 -> Application keypad (DECNKM).
3394 // Ps = 6 7 -> Backarrow key sends backspace (DECBKM).
3395 // Ps = 1 0 0 0 -> Send Mouse X & Y on button press and
3396 // release. See the section Mouse Tracking.
3397 // Ps = 1 0 0 1 -> Use Hilite Mouse Tracking.
3398 // Ps = 1 0 0 2 -> Use Cell Motion Mouse Tracking.
3399 // Ps = 1 0 0 3 -> Use All Motion Mouse Tracking.
3400 // Ps = 1 0 0 4 -> Send FocusIn/FocusOut events.
3401 // Ps = 1 0 0 5 -> Enable Extended Mouse Mode.
3402 // Ps = 1 0 1 0 -> Scroll to bottom on tty output (rxvt).
3403 // Ps = 1 0 1 1 -> Scroll to bottom on key press (rxvt).
3404 // Ps = 1 0 3 4 -> Interpret "meta" key, sets eighth bit.
3405 // (enables the eightBitInput resource).
3406 // Ps = 1 0 3 5 -> Enable special modifiers for Alt and Num-
3407 // Lock keys. (This enables the numLock resource).
3408 // Ps = 1 0 3 6 -> Send ESC when Meta modifies a key. (This
3409 // enables the metaSendsEscape resource).
3410 // Ps = 1 0 3 7 -> Send DEL from the editing-keypad Delete
3412 // Ps = 1 0 3 9 -> Send ESC when Alt modifies a key. (This
3413 // enables the altSendsEscape resource).
3414 // Ps = 1 0 4 0 -> Keep selection even if not highlighted.
3415 // (This enables the keepSelection resource).
3416 // Ps = 1 0 4 1 -> Use the CLIPBOARD selection. (This enables
3417 // the selectToClipboard resource).
3418 // Ps = 1 0 4 2 -> Enable Urgency window manager hint when
3419 // Control-G is received. (This enables the bellIsUrgent
3421 // Ps = 1 0 4 3 -> Enable raising of the window when Control-G
3422 // is received. (enables the popOnBell resource).
3423 // Ps = 1 0 4 7 -> Use Alternate Screen Buffer. (This may be
3424 // disabled by the titeInhibit resource).
3425 // Ps = 1 0 4 8 -> Save cursor as in DECSC. (This may be dis-
3426 // abled by the titeInhibit resource).
3427 // Ps = 1 0 4 9 -> Save cursor as in DECSC and use Alternate
3428 // Screen Buffer, clearing it first. (This may be disabled by
3429 // the titeInhibit resource). This combines the effects of the 1
3430 // 0 4 7 and 1 0 4 8 modes. Use this with terminfo-based
3431 // applications rather than the 4 7 mode.
3432 // Ps = 1 0 5 0 -> Set terminfo/termcap function-key mode.
3433 // Ps = 1 0 5 1 -> Set Sun function-key mode.
3434 // Ps = 1 0 5 2 -> Set HP function-key mode.
3435 // Ps = 1 0 5 3 -> Set SCO function-key mode.
3436 // Ps = 1 0 6 0 -> Set legacy keyboard emulation (X11R6).
3437 // Ps = 1 0 6 1 -> Set VT220 keyboard emulation.
3438 // Ps = 2 0 0 4 -> Set bracketed paste mode.
3440 // http://vt100.net/docs/vt220-rm/chapter4.html
3441 Terminal
.prototype.setMode = function(params
) {
3442 if (typeof params
=== 'object') {
3443 var l
= params
.length
3446 for (; i
< l
; i
++) {
3447 this.setMode(params
[i
]);
3456 this.insertMode
= true;
3459 //this.convertEol = true;
3462 } else if (this.prefix
=== '?') {
3465 this.applicationCursor
= true;
3468 this.setgCharset(0, Terminal
.charsets
.US
);
3469 this.setgCharset(1, Terminal
.charsets
.US
);
3470 this.setgCharset(2, Terminal
.charsets
.US
);
3471 this.setgCharset(3, Terminal
.charsets
.US
);
3472 // set VT100 mode here
3474 case 3: // 132 col mode
3475 this.savedCols
= this.cols
;
3476 this.resize(132, this.rows
);
3479 this.originMode
= true;
3482 this.wraparoundMode
= true;
3485 // this.cursorBlink = true;
3488 this.log('Serial port requested application keypad.');
3489 this.applicationKeypad
= true;
3491 case 9: // X10 Mouse
3492 // no release, no motion, no wheel, no modifiers.
3493 case 1000: // vt200 mouse
3495 // no modifiers, except control on the wheel.
3496 case 1002: // button event mouse
3497 case 1003: // any event mouse
3498 // any event - sends motion events,
3499 // even if there is no button held down.
3500 this.x10Mouse
= params
=== 9;
3501 this.vt200Mouse
= params
=== 1000;
3502 this.normalMouse
= params
> 1000;
3503 this.mouseEvents
= true;
3504 this.element
.style
.cursor
= 'default';
3505 this.log('Binding to mouse events.');
3507 case 1004: // send focusin/focusout events
3510 this.sendFocus
= true;
3512 case 1005: // utf8 ext mode mouse
3513 this.utfMouse
= true;
3514 // for wide terminals
3515 // simply encodes large values as utf8 characters
3517 case 1006: // sgr ext mode mouse
3518 this.sgrMouse
= true;
3519 // for wide terminals
3520 // does not add 32 to fields
3521 // press: ^[[<b;x;yM
3522 // release: ^[[<b;x;ym
3524 case 1015: // urxvt ext mode mouse
3525 this.urxvtMouse
= true;
3526 // for wide terminals
3527 // numbers for fields
3529 // motion: ^[[b;x;yT
3531 case 25: // show cursor
3532 this.cursorHidden
= false;
3534 case 1049: // alt screen buffer cursor
3535 //this.saveCursor();
3537 case 47: // alt screen buffer
3538 case 1047: // alt screen buffer
3546 scrollTop
: this.scrollTop
,
3547 scrollBottom
: this.scrollBottom
,
3549 // XXX save charset(s) here?
3550 // charset: this.charset,
3551 // glevel: this.glevel,
3552 // charsets: this.charsets
3555 this.normal
= normal
;
3563 // CSI Pm l Reset Mode (RM).
3564 // Ps = 2 -> Keyboard Action Mode (AM).
3565 // Ps = 4 -> Replace Mode (IRM).
3566 // Ps = 1 2 -> Send/receive (SRM).
3567 // Ps = 2 0 -> Normal Linefeed (LNM).
3569 // DEC Private Mode Reset (DECRST).
3570 // Ps = 1 -> Normal Cursor Keys (DECCKM).
3571 // Ps = 2 -> Designate VT52 mode (DECANM).
3572 // Ps = 3 -> 80 Column Mode (DECCOLM).
3573 // Ps = 4 -> Jump (Fast) Scroll (DECSCLM).
3574 // Ps = 5 -> Normal Video (DECSCNM).
3575 // Ps = 6 -> Normal Cursor Mode (DECOM).
3576 // Ps = 7 -> No Wraparound Mode (DECAWM).
3577 // Ps = 8 -> No Auto-repeat Keys (DECARM).
3578 // Ps = 9 -> Don't send Mouse X & Y on button press.
3579 // Ps = 1 0 -> Hide toolbar (rxvt).
3580 // Ps = 1 2 -> Stop Blinking Cursor (att610).
3581 // Ps = 1 8 -> Don't print form feed (DECPFF).
3582 // Ps = 1 9 -> Limit print to scrolling region (DECPEX).
3583 // Ps = 2 5 -> Hide Cursor (DECTCEM).
3584 // Ps = 3 0 -> Don't show scrollbar (rxvt).
3585 // Ps = 3 5 -> Disable font-shifting functions (rxvt).
3586 // Ps = 4 0 -> Disallow 80 -> 132 Mode.
3587 // Ps = 4 1 -> No more(1) fix (see curses resource).
3588 // Ps = 4 2 -> Disable Nation Replacement Character sets (DEC-
3590 // Ps = 4 4 -> Turn Off Margin Bell.
3591 // Ps = 4 5 -> No Reverse-wraparound Mode.
3592 // Ps = 4 6 -> Stop Logging. (This is normally disabled by a
3593 // compile-time option).
3594 // Ps = 4 7 -> Use Normal Screen Buffer.
3595 // Ps = 6 6 -> Numeric keypad (DECNKM).
3596 // Ps = 6 7 -> Backarrow key sends delete (DECBKM).
3597 // Ps = 1 0 0 0 -> Don't send Mouse X & Y on button press and
3598 // release. See the section Mouse Tracking.
3599 // Ps = 1 0 0 1 -> Don't use Hilite Mouse Tracking.
3600 // Ps = 1 0 0 2 -> Don't use Cell Motion Mouse Tracking.
3601 // Ps = 1 0 0 3 -> Don't use All Motion Mouse Tracking.
3602 // Ps = 1 0 0 4 -> Don't send FocusIn/FocusOut events.
3603 // Ps = 1 0 0 5 -> Disable Extended Mouse Mode.
3604 // Ps = 1 0 1 0 -> Don't scroll to bottom on tty output
3606 // Ps = 1 0 1 1 -> Don't scroll to bottom on key press (rxvt).
3607 // Ps = 1 0 3 4 -> Don't interpret "meta" key. (This disables
3608 // the eightBitInput resource).
3609 // Ps = 1 0 3 5 -> Disable special modifiers for Alt and Num-
3610 // Lock keys. (This disables the numLock resource).
3611 // Ps = 1 0 3 6 -> Don't send ESC when Meta modifies a key.
3612 // (This disables the metaSendsEscape resource).
3613 // Ps = 1 0 3 7 -> Send VT220 Remove from the editing-keypad
3615 // Ps = 1 0 3 9 -> Don't send ESC when Alt modifies a key.
3616 // (This disables the altSendsEscape resource).
3617 // Ps = 1 0 4 0 -> Do not keep selection when not highlighted.
3618 // (This disables the keepSelection resource).
3619 // Ps = 1 0 4 1 -> Use the PRIMARY selection. (This disables
3620 // the selectToClipboard resource).
3621 // Ps = 1 0 4 2 -> Disable Urgency window manager hint when
3622 // Control-G is received. (This disables the bellIsUrgent
3624 // Ps = 1 0 4 3 -> Disable raising of the window when Control-
3625 // G is received. (This disables the popOnBell resource).
3626 // Ps = 1 0 4 7 -> Use Normal Screen Buffer, clearing screen
3627 // first if in the Alternate Screen. (This may be disabled by
3628 // the titeInhibit resource).
3629 // Ps = 1 0 4 8 -> Restore cursor as in DECRC. (This may be
3630 // disabled by the titeInhibit resource).
3631 // Ps = 1 0 4 9 -> Use Normal Screen Buffer and restore cursor
3632 // as in DECRC. (This may be disabled by the titeInhibit
3633 // resource). This combines the effects of the 1 0 4 7 and 1 0
3634 // 4 8 modes. Use this with terminfo-based applications rather
3635 // than the 4 7 mode.
3636 // Ps = 1 0 5 0 -> Reset terminfo/termcap function-key mode.
3637 // Ps = 1 0 5 1 -> Reset Sun function-key mode.
3638 // Ps = 1 0 5 2 -> Reset HP function-key mode.
3639 // Ps = 1 0 5 3 -> Reset SCO function-key mode.
3640 // Ps = 1 0 6 0 -> Reset legacy keyboard emulation (X11R6).
3641 // Ps = 1 0 6 1 -> Reset keyboard emulation to Sun/PC style.
3642 // Ps = 2 0 0 4 -> Reset bracketed paste mode.
3643 Terminal
.prototype.resetMode = function(params
) {
3644 if (typeof params
=== 'object') {
3645 var l
= params
.length
3648 for (; i
< l
; i
++) {
3649 this.resetMode(params
[i
]);
3658 this.insertMode
= false;
3661 //this.convertEol = false;
3664 } else if (this.prefix
=== '?') {
3667 this.applicationCursor
= false;
3670 if (this.cols
=== 132 && this.savedCols
) {
3671 this.resize(this.savedCols
, this.rows
);
3673 delete this.savedCols
;
3676 this.originMode
= false;
3679 this.wraparoundMode
= false;
3682 // this.cursorBlink = false;
3685 this.log('Switching back to normal keypad.');
3686 this.applicationKeypad
= false;
3688 case 9: // X10 Mouse
3689 case 1000: // vt200 mouse
3690 case 1002: // button event mouse
3691 case 1003: // any event mouse
3692 this.x10Mouse
= false;
3693 this.vt200Mouse
= false;
3694 this.normalMouse
= false;
3695 this.mouseEvents
= false;
3696 this.element
.style
.cursor
= '';
3698 case 1004: // send focusin/focusout events
3699 this.sendFocus
= false;
3701 case 1005: // utf8 ext mode mouse
3702 this.utfMouse
= false;
3704 case 1006: // sgr ext mode mouse
3705 this.sgrMouse
= false;
3707 case 1015: // urxvt ext mode mouse
3708 this.urxvtMouse
= false;
3710 case 25: // hide cursor
3711 this.cursorHidden
= true;
3713 case 1049: // alt screen buffer cursor
3715 case 47: // normal screen buffer
3716 case 1047: // normal screen buffer - clearing it first
3718 this.lines
= this.normal
.lines
;
3719 this.ybase
= this.normal
.ybase
;
3720 this.ydisp
= this.normal
.ydisp
;
3721 this.x
= this.normal
.x
;
3722 this.y
= this.normal
.y
;
3723 this.scrollTop
= this.normal
.scrollTop
;
3724 this.scrollBottom
= this.normal
.scrollBottom
;
3725 this.tabs
= this.normal
.tabs
;
3727 // if (params === 1049) {
3728 // this.x = this.savedX;
3729 // this.y = this.savedY;
3731 this.refresh(0, this.rows
- 1);
3740 // Set Scrolling Region [top;bottom] (default = full size of win-
3743 Terminal
.prototype.setScrollRegion = function(params
) {
3744 if (this.prefix
) return;
3745 this.scrollTop
= (params
[0] || 1) - 1;
3746 this.scrollBottom
= (params
[1] || this.rows
) - 1;
3752 // Save cursor (ANSI.SYS).
3753 Terminal
.prototype.saveCursor = function(params
) {
3754 this.savedX
= this.x
;
3755 this.savedY
= this.y
;
3759 // Restore cursor (ANSI.SYS).
3760 Terminal
.prototype.restoreCursor = function(params
) {
3761 this.x
= this.savedX
|| 0;
3762 this.y
= this.savedY
|| 0;
3770 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
3771 Terminal
.prototype.cursorForwardTab = function(params
) {
3772 var param
= params
[0] || 1;
3774 this.x
= this.nextStop();
3778 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
3779 Terminal
.prototype.scrollUp = function(params
) {
3780 var param
= params
[0] || 1;
3782 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
3783 this.lines
.splice(this.ybase
+ this.scrollBottom
, 0, this.blankLine());
3786 this.updateRange(this.scrollTop
);
3787 this.updateRange(this.scrollBottom
);
3790 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
3791 Terminal
.prototype.scrollDown = function(params
) {
3792 var param
= params
[0] || 1;
3794 this.lines
.splice(this.ybase
+ this.scrollBottom
, 1);
3795 this.lines
.splice(this.ybase
+ this.scrollTop
, 0, this.blankLine());
3798 this.updateRange(this.scrollTop
);
3799 this.updateRange(this.scrollBottom
);
3802 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
3803 // Initiate highlight mouse tracking. Parameters are
3804 // [func;startx;starty;firstrow;lastrow]. See the section Mouse
3806 Terminal
.prototype.initMouseTracking = function(params
) {
3807 // Relevant: DECSET 1001
3811 // Reset one or more features of the title modes to the default
3812 // value. Normally, "reset" disables the feature. It is possi-
3813 // ble to disable the ability to reset features by compiling a
3814 // different default for the title modes into xterm.
3815 // Ps = 0 -> Do not set window/icon labels using hexadecimal.
3816 // Ps = 1 -> Do not query window/icon labels using hexadeci-
3818 // Ps = 2 -> Do not set window/icon labels using UTF-8.
3819 // Ps = 3 -> Do not query window/icon labels using UTF-8.
3820 // (See discussion of "Title Modes").
3821 Terminal
.prototype.resetTitleModes = function(params
) {
3825 // CSI Ps Z Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
3826 Terminal
.prototype.cursorBackwardTab = function(params
) {
3827 var param
= params
[0] || 1;
3829 this.x
= this.prevStop();
3833 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
3834 Terminal
.prototype.repeatPrecedingCharacter = function(params
) {
3835 var param
= params
[0] || 1
3836 , line
= this.lines
[this.ybase
+ this.y
]
3837 , ch
= line
[this.x
- 1] || [this.defAttr
, ' '];
3839 while (param
--) line
[this.x
++] = ch
;
3842 // CSI Ps g Tab Clear (TBC).
3843 // Ps = 0 -> Clear Current Column (default).
3844 // Ps = 3 -> Clear All.
3846 // Ps = 2 -> Clear Stops on Line.
3847 // http://vt100.net/annarbor/aaa-ug/section6.html
3848 Terminal
.prototype.tabClear = function(params
) {
3849 var param
= params
[0];
3851 delete this.tabs
[this.x
];
3852 } else if (param
=== 3) {
3857 // CSI Pm i Media Copy (MC).
3858 // Ps = 0 -> Print screen (default).
3859 // Ps = 4 -> Turn off printer controller mode.
3860 // Ps = 5 -> Turn on printer controller mode.
3862 // Media Copy (MC, DEC-specific).
3863 // Ps = 1 -> Print line containing cursor.
3864 // Ps = 4 -> Turn off autoprint mode.
3865 // Ps = 5 -> Turn on autoprint mode.
3866 // Ps = 1 0 -> Print composed display, ignores DECPEX.
3867 // Ps = 1 1 -> Print all pages.
3868 Terminal
.prototype.mediaCopy = function(params
) {
3873 // Set or reset resource-values used by xterm to decide whether
3874 // to construct escape sequences holding information about the
3875 // modifiers pressed with a given key. The first parameter iden-
3876 // tifies the resource to set/reset. The second parameter is the
3877 // value to assign to the resource. If the second parameter is
3878 // omitted, the resource is reset to its initial value.
3879 // Ps = 1 -> modifyCursorKeys.
3880 // Ps = 2 -> modifyFunctionKeys.
3881 // Ps = 4 -> modifyOtherKeys.
3882 // If no parameters are given, all resources are reset to their
3884 Terminal
.prototype.setResources = function(params
) {
3889 // Disable modifiers which may be enabled via the CSI > Ps; Ps m
3890 // sequence. This corresponds to a resource value of "-1", which
3891 // cannot be set with the other sequence. The parameter identi-
3892 // fies the resource to be disabled:
3893 // Ps = 1 -> modifyCursorKeys.
3894 // Ps = 2 -> modifyFunctionKeys.
3895 // Ps = 4 -> modifyOtherKeys.
3896 // If the parameter is omitted, modifyFunctionKeys is disabled.
3897 // When modifyFunctionKeys is disabled, xterm uses the modifier
3898 // keys to make an extended sequence of functions rather than
3899 // adding a parameter to each function key to denote the modi-
3901 Terminal
.prototype.disableModifiers = function(params
) {
3906 // Set resource value pointerMode. This is used by xterm to
3907 // decide whether to hide the pointer cursor as the user types.
3908 // Valid values for the parameter:
3909 // Ps = 0 -> never hide the pointer.
3910 // Ps = 1 -> hide if the mouse tracking mode is not enabled.
3911 // Ps = 2 -> always hide the pointer. If no parameter is
3912 // given, xterm uses the default, which is 1 .
3913 Terminal
.prototype.setPointerMode = function(params
) {
3917 // CSI ! p Soft terminal reset (DECSTR).
3918 // http://vt100.net/docs/vt220-rm/table4-10.html
3919 Terminal
.prototype.softReset = function(params
) {
3920 this.cursorHidden
= false;
3921 this.insertMode
= false;
3922 this.originMode
= false;
3923 this.wraparoundMode
= false; // autowrap
3924 this.applicationKeypad
= false; // ?
3925 this.applicationCursor
= false;
3927 this.scrollBottom
= this.rows
- 1;
3928 this.curAttr
= this.defAttr
;
3929 this.x
= this.y
= 0; // ?
3930 this.charset
= null;
3931 this.glevel
= 0; // ??
3932 this.charsets
= [null]; // ??
3936 // Request ANSI mode (DECRQM). For VT300 and up, reply is
3938 // where Ps is the mode number as in RM, and Pm is the mode
3940 // 0 - not recognized
3943 // 3 - permanently set
3944 // 4 - permanently reset
3945 Terminal
.prototype.requestAnsiMode = function(params
) {
3950 // Request DEC private mode (DECRQM). For VT300 and up, reply is
3952 // where Ps is the mode number as in DECSET, Pm is the mode value
3953 // as in the ANSI DECRQM.
3954 Terminal
.prototype.requestPrivateMode = function(params
) {
3959 // Set conformance level (DECSCL). Valid values for the first
3961 // Ps = 6 1 -> VT100.
3962 // Ps = 6 2 -> VT200.
3963 // Ps = 6 3 -> VT300.
3964 // Valid values for the second parameter:
3965 // Ps = 0 -> 8-bit controls.
3966 // Ps = 1 -> 7-bit controls (always set for VT100).
3967 // Ps = 2 -> 8-bit controls.
3968 Terminal
.prototype.setConformanceLevel = function(params
) {
3972 // CSI Ps q Load LEDs (DECLL).
3973 // Ps = 0 -> Clear all LEDS (default).
3974 // Ps = 1 -> Light Num Lock.
3975 // Ps = 2 -> Light Caps Lock.
3976 // Ps = 3 -> Light Scroll Lock.
3977 // Ps = 2 1 -> Extinguish Num Lock.
3978 // Ps = 2 2 -> Extinguish Caps Lock.
3979 // Ps = 2 3 -> Extinguish Scroll Lock.
3980 Terminal
.prototype.loadLEDs = function(params
) {
3985 // Set cursor style (DECSCUSR, VT520).
3986 // Ps = 0 -> blinking block.
3987 // Ps = 1 -> blinking block (default).
3988 // Ps = 2 -> steady block.
3989 // Ps = 3 -> blinking underline.
3990 // Ps = 4 -> steady underline.
3991 Terminal
.prototype.setCursorStyle = function(params
) {
3996 // Select character protection attribute (DECSCA). Valid values
3997 // for the parameter:
3998 // Ps = 0 -> DECSED and DECSEL can erase (default).
3999 // Ps = 1 -> DECSED and DECSEL cannot erase.
4000 // Ps = 2 -> DECSED and DECSEL can erase.
4001 Terminal
.prototype.setCharProtectionAttr = function(params
) {
4006 // Restore DEC Private Mode Values. The value of Ps previously
4007 // saved is restored. Ps values are the same as for DECSET.
4008 Terminal
.prototype.restorePrivateValues = function(params
) {
4012 // CSI Pt; Pl; Pb; Pr; Ps$ r
4013 // Change Attributes in Rectangular Area (DECCARA), VT400 and up.
4014 // Pt; Pl; Pb; Pr denotes the rectangle.
4015 // Ps denotes the SGR attributes to change: 0, 1, 4, 5, 7.
4016 // NOTE: xterm doesn't enable this code by default.
4017 Terminal
.prototype.setAttrInRectangle = function(params
) {
4027 for (; t
< b
+ 1; t
++) {
4028 line
= this.lines
[this.ybase
+ t
];
4029 for (i
= l
; i
< r
; i
++) {
4030 line
[i
] = [attr
, line
[i
][1]];
4035 this.updateRange(params
[0]);
4036 this.updateRange(params
[2]);
4040 // CSI Pc; Pt; Pl; Pb; Pr$ x
4041 // Fill Rectangular Area (DECFRA), VT420 and up.
4042 // Pc is the character to use.
4043 // Pt; Pl; Pb; Pr denotes the rectangle.
4044 // NOTE: xterm doesn't enable this code by default.
4045 Terminal
.prototype.fillRectangle = function(params
) {
4055 for (; t
< b
+ 1; t
++) {
4056 line
= this.lines
[this.ybase
+ t
];
4057 for (i
= l
; i
< r
; i
++) {
4058 line
[i
] = [line
[i
][0], String
.fromCharCode(ch
)];
4063 this.updateRange(params
[1]);
4064 this.updateRange(params
[3]);
4068 // Enable Locator Reporting (DECELR).
4069 // Valid values for the first parameter:
4070 // Ps = 0 -> Locator disabled (default).
4071 // Ps = 1 -> Locator enabled.
4072 // Ps = 2 -> Locator enabled for one report, then disabled.
4073 // The second parameter specifies the coordinate unit for locator
4075 // Valid values for the second parameter:
4076 // Pu = 0 <- or omitted -> default to character cells.
4077 // Pu = 1 <- device physical pixels.
4078 // Pu = 2 <- character cells.
4079 Terminal
.prototype.enableLocatorReporting = function(params
) {
4080 var val
= params
[0] > 0;
4081 //this.mouseEvents = val;
4082 //this.decLocator = val;
4085 // CSI Pt; Pl; Pb; Pr$ z
4086 // Erase Rectangular Area (DECERA), VT400 and up.
4087 // Pt; Pl; Pb; Pr denotes the rectangle.
4088 // NOTE: xterm doesn't enable this code by default.
4089 Terminal
.prototype.eraseRectangle = function(params
) {
4099 ch
= [this.eraseAttr(), ' ']; // xterm?
4101 for (; t
< b
+ 1; t
++) {
4102 line
= this.lines
[this.ybase
+ t
];
4103 for (i
= l
; i
< r
; i
++) {
4109 this.updateRange(params
[0]);
4110 this.updateRange(params
[2]);
4115 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
4116 // NOTE: xterm doesn't enable this code by default.
4117 Terminal
.prototype.insertColumns = function() {
4118 var param
= params
[0]
4119 , l
= this.ybase
+ this.rows
4120 , ch
= [this.eraseAttr(), ' '] // xterm?
4124 for (i
= this.ybase
; i
< l
; i
++) {
4125 this.lines
[i
].splice(this.x
+ 1, 0, ch
);
4126 this.lines
[i
].pop();
4134 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
4135 // NOTE: xterm doesn't enable this code by default.
4136 Terminal
.prototype.deleteColumns = function() {
4137 var param
= params
[0]
4138 , l
= this.ybase
+ this.rows
4139 , ch
= [this.eraseAttr(), ' '] // xterm?
4143 for (i
= this.ybase
; i
< l
; i
++) {
4144 this.lines
[i
].splice(this.x
, 1);
4145 this.lines
[i
].push(ch
);
4153 Terminal
.prototype.copyBuffer = function(lines
) {
4154 var lines
= lines
|| this.lines
4157 for (var y
= 0; y
< lines
.length
; y
++) {
4159 for (var x
= 0; x
< lines
[y
].length
; x
++) {
4160 out
[y
][x
] = [lines
[y
][x
][0], lines
[y
][x
][1]];
4167 Terminal
.prototype.getCopyTextarea = function(text
) {
4168 var textarea
= this._copyTextarea
4169 , document
= this.document
;
4172 textarea
= document
.createElement('textarea');
4173 textarea
.style
.position
= 'absolute';
4174 textarea
.style
.left
= '-32000px';
4175 textarea
.style
.top
= '-32000px';
4176 textarea
.style
.width
= '0px';
4177 textarea
.style
.height
= '0px';
4178 textarea
.style
.opacity
= '0';
4179 textarea
.style
.backgroundColor
= 'transparent';
4180 textarea
.style
.borderStyle
= 'none';
4181 textarea
.style
.outlineStyle
= 'none';
4183 document
.getElementsByTagName('body')[0].appendChild(textarea
);
4185 this._copyTextarea
= textarea
;
4191 // NOTE: Only works for primary selection on X11.
4192 // Non-X11 users should use Ctrl-C instead.
4193 Terminal
.prototype.copyText = function(text
) {
4195 , textarea
= this.getCopyTextarea();
4197 this.emit('copy', text
);
4200 textarea
.textContent
= text
;
4201 textarea
.value
= text
;
4202 textarea
.setSelectionRange(0, text
.length
);
4204 setTimeout(function() {
4205 self
.element
.focus();
4210 Terminal
.prototype.keyPrefix = function(ev
, key
) {
4211 if (key
=== 'k' || key
=== '&') {
4213 } else if (key
=== 'p' || key
=== ']') {
4214 this.emit('request paste');
4215 } else if (key
=== 'c') {
4216 this.emit('request create');
4217 } else if (key
>= '0' && key
<= '9') {
4220 this.emit('request term', key
);
4221 } else if (key
=== 'n') {
4222 this.emit('request term next');
4223 } else if (key
=== 'P') {
4224 this.emit('request term previous');
4225 } else if (key
=== ':') {
4226 this.emit('request command mode');
4230 Terminal
.prototype.keySelect = function(ev
, key
) {
4233 if (key
=== '\x04') { // ctrl-d
4234 var y
= this.ydisp
+ this.y
;
4235 if (this.ydisp
=== this.ybase
) {
4236 // Mimic vim behavior
4237 this.y
= Math
.min(this.y
+ (this.rows
- 1) / 2 | 0, this.rows
- 1);
4238 this.refresh(0, this.rows
- 1);
4240 this.scrollDisp((this.rows
- 1) / 2 | 0);
4245 if (key
=== '\x15') { // ctrl-u
4246 var y
= this.ydisp
+ this.y
;
4247 if (this.ydisp
=== 0) {
4248 // Mimic vim behavior
4249 this.y
= Math
.max(this.y
- (this.rows
- 1) / 2 | 0, 0);
4250 this.refresh(0, this.rows
- 1);
4252 this.scrollDisp(-(this.rows
- 1) / 2 | 0);
4257 if (key
=== '\x06') { // ctrl-f
4258 var y
= this.ydisp
+ this.y
;
4259 this.scrollDisp(this.rows
- 1);
4263 if (key
=== '\x02') { // ctrl-b
4264 var y
= this.ydisp
+ this.y
;
4265 this.scrollDisp(-(this.rows
- 1));
4269 if (key
=== 'k' || key
=== '\x1b[A') {
4270 var y
= this.ydisp
+ this.y
;
4274 this.scrollDisp(-1);
4276 this.refresh(this.y
, this.y
+ 1);
4280 if (key
=== 'j' || key
=== '\x1b[B') {
4281 var y
= this.ydisp
+ this.y
;
4283 if (this.y
>= this.rows
) {
4284 this.y
= this.rows
- 1;
4287 this.refresh(this.y
- 1, this.y
);
4291 if (key
=== 'h' || key
=== '\x1b[D') {
4297 this.refresh(this.y
, this.y
);
4301 if (key
=== 'l' || key
=== '\x1b[C') {
4304 if (this.x
>= this.cols
) {
4305 this.x
= this.cols
- 1;
4307 this.refresh(this.y
, this.y
);
4311 if (key
=== 'w' || key
=== 'W') {
4314 var oyd
= this.ydisp
;
4318 var yb
= this.ydisp
;
4319 var saw_space
= false;
4322 var line
= this.lines
[yb
+ y
];
4323 while (x
< this.cols
) {
4324 if (line
[x
][1] <= ' ') {
4326 } else if (saw_space
) {
4331 if (x
>= this.cols
) x
= this.cols
- 1;
4332 if (x
=== this.cols
- 1 && line
[x
][1] <= ' ') {
4334 if (++y
>= this.rows
) {
4336 if (++yb
> this.ybase
) {
4347 this.x
= x
, this.y
= y
;
4348 this.scrollDisp(-this.ydisp
+ yb
);
4353 if (key
=== 'b' || key
=== 'B') {
4356 var oyd
= this.ydisp
;
4360 var yb
= this.ydisp
;
4363 var line
= this.lines
[yb
+ y
];
4364 var saw_space
= x
> 0 && line
[x
][1] > ' ' && line
[x
- 1][1] > ' ';
4366 if (line
[x
][1] <= ' ') {
4367 if (saw_space
&& (x
+ 1 < this.cols
&& line
[x
+ 1][1] > ' ')) {
4377 if (x
=== 0 && (line
[x
][1] <= ' ' || !saw_space
)) {
4392 this.x
= x
, this.y
= y
;
4393 this.scrollDisp(-this.ydisp
+ yb
);
4398 if (key
=== 'e' || key
=== 'E') {
4401 var yb
= this.ydisp
;
4402 if (x
>= this.cols
) x
--;
4405 var line
= this.lines
[yb
+ y
];
4406 while (x
< this.cols
) {
4407 if (line
[x
][1] <= ' ') {
4413 while (x
< this.cols
) {
4414 if (line
[x
][1] <= ' ') {
4415 if (x
- 1 >= 0 && line
[x
- 1][1] > ' ') {
4422 if (x
>= this.cols
) x
= this.cols
- 1;
4423 if (x
=== this.cols
- 1 && line
[x
][1] <= ' ') {
4425 if (++y
>= this.rows
) {
4427 if (++yb
> this.ybase
) {
4437 this.x
= x
, this.y
= y
;
4438 this.scrollDisp(-this.ydisp
+ yb
);
4443 if (key
=== '^' || key
=== '0') {
4448 } else if (key
=== '^') {
4449 var line
= this.lines
[this.ydisp
+ this.y
];
4451 while (x
< this.cols
) {
4452 if (line
[x
][1] > ' ') {
4457 if (x
>= this.cols
) x
= this.cols
- 1;
4461 this.refresh(this.y
, this.y
);
4467 var line
= this.lines
[this.ydisp
+ this.y
];
4468 var x
= this.cols
- 1;
4474 this.refresh(this.y
, this.y
);
4478 if (key
=== 'g' || key
=== 'G') {
4481 var oyd
= this.ydisp
;
4483 this.x
= 0, this.y
= 0;
4484 this.scrollDisp(-this.ydisp
);
4485 } else if (key
=== 'G') {
4486 this.x
= 0, this.y
= this.rows
- 1;
4487 this.scrollDisp(this.ybase
);
4492 if (key
=== 'H' || key
=== 'M' || key
=== 'L') {
4496 this.x
= 0, this.y
= 0;
4497 } else if (key
=== 'M') {
4498 this.x
= 0, this.y
= this.rows
/ 2 | 0;
4499 } else if (key
=== 'L') {
4500 this.x
= 0, this.y
= this.rows
- 1;
4502 this.refresh(oy
, oy
);
4503 this.refresh(this.y
, this.y
);
4507 if (key
=== '{' || key
=== '}') {
4510 var oyd
= this.ydisp
;
4513 var saw_full
= false;
4515 var first_is_space
= -1;
4516 var y
= this.y
+ (key
=== '{' ? -1 : 1);
4517 var yb
= this.ydisp
;
4525 } else if (key
=== '}') {
4526 if (y
>= this.rows
) {
4528 if (yb
< this.ybase
) yb
++;
4533 line
= this.lines
[yb
+ y
];
4535 for (i
= 0; i
< this.cols
; i
++) {
4536 if (line
[i
][1] > ' ') {
4537 if (first_is_space
=== -1) {
4542 } else if (i
=== this.cols
- 1) {
4543 if (first_is_space
=== -1) {
4545 } else if (first_is_space
=== 0) {
4547 } else if (first_is_space
=== 1) {
4548 if (saw_full
) found
= true;
4563 } else if (key
=== '}') {
4565 if (y
>= this.rows
) {
4567 if (yb
< this.ybase
) yb
++;
4577 } else if (key
=== '}') {
4583 this.x
= 0, this.y
= y
;
4584 this.scrollDisp(-this.ydisp
+ yb
);
4596 Terminal
.charsets
= {};
4598 // DEC Special Character and Line Drawing Set.
4599 // http://vt100.net/docs/vt102-ug/table5-13.html
4600 // A lot of curses apps use this if they see TERM=xterm.
4601 // testing: echo -e '\e(0a\e(B'
4602 // The xterm output sometimes seems to conflict with the
4603 // reference above. xterm seems in line with the reference
4604 // when running vttest however.
4605 // The table below now uses xterm's output from vttest.
4606 Terminal
.charsets
.SCLD
= { // (0
4607 '`': '\u25c6', // '◆'
4608 'a': '\u2592', // '▒'
4609 'b': '\u0009', // '\t'
4610 'c': '\u000c', // '\f'
4611 'd': '\u000d', // '\r'
4612 'e': '\u000a', // '\n'
4613 'f': '\u00b0', // '°'
4614 'g': '\u00b1', // '±'
4615 'h': '\u2424', // '\u2424' (NL)
4616 'i': '\u000b', // '\v'
4617 'j': '\u2518', // '┘'
4618 'k': '\u2510', // '┐'
4619 'l': '\u250c', // '┌'
4620 'm': '\u2514', // '└'
4621 'n': '\u253c', // '┼'
4622 'o': '\u23ba', // '⎺'
4623 'p': '\u23bb', // '⎻'
4624 'q': '\u2500', // '─'
4625 'r': '\u23bc', // '⎼'
4626 's': '\u23bd', // '⎽'
4627 't': '\u251c', // '├'
4628 'u': '\u2524', // '┤'
4629 'v': '\u2534', // '┴'
4630 'w': '\u252c', // '┬'
4631 'x': '\u2502', // '│'
4632 'y': '\u2264', // '≤'
4633 'z': '\u2265', // '≥'
4634 '{': '\u03c0', // 'π'
4635 '|': '\u2260', // '≠'
4636 '}': '\u00a3', // '£'
4637 '~': '\u00b7' // '·'
4640 Terminal
.charsets
.UK
= null; // (A
4641 Terminal
.charsets
.US
= null; // (B (USASCII)
4642 Terminal
.charsets
.Dutch
= null; // (4
4643 Terminal
.charsets
.Finnish
= null; // (C or (5
4644 Terminal
.charsets
.French
= null; // (R
4645 Terminal
.charsets
.FrenchCanadian
= null; // (Q
4646 Terminal
.charsets
.German
= null; // (K
4647 Terminal
.charsets
.Italian
= null; // (Y
4648 Terminal
.charsets
.NorwegianDanish
= null; // (E or (6
4649 Terminal
.charsets
.Spanish
= null; // (Z
4650 Terminal
.charsets
.Swedish
= null; // (H or (7
4651 Terminal
.charsets
.Swiss
= null; // (=
4652 Terminal
.charsets
.ISOLatin
= null; // /A
4658 function on(el
, type
, handler
, capture
) {
4659 if (!Array
.isArray(el
)) {
4662 el
.forEach(function (element
) {
4663 element
.addEventListener(type
, handler
, capture
|| false);
4667 function off(el
, type
, handler
, capture
) {
4668 el
.removeEventListener(type
, handler
, capture
|| false);
4671 function cancel(ev
, force
) {
4672 if (!this.cancelEvents
&& !force
) {
4675 ev
.preventDefault();
4676 ev
.stopPropagation();
4680 function inherits(child
, parent
) {
4682 this.constructor = child
;
4684 f
.prototype = parent
.prototype;
4685 child
.prototype = new f
;
4688 // if bold is broken, we can't
4689 // use it in the terminal.
4690 function isBoldBroken(document
) {
4691 var body
= document
.getElementsByTagName('body')[0];
4692 var el
= document
.createElement('span');
4693 el
.innerHTML
= 'hello world';
4694 body
.appendChild(el
);
4695 var w1
= el
.scrollWidth
;
4696 el
.style
.fontWeight
= 'bold';
4697 var w2
= el
.scrollWidth
;
4698 body
.removeChild(el
);
4702 var String
= this.String
;
4703 var setTimeout
= this.setTimeout
;
4704 var setInterval
= this.setInterval
;
4706 function indexOf(obj
, el
) {
4709 if (obj
[i
] === el
) return i
;
4714 function isWide(ch
) {
4715 if (ch
<= '\uff00') return false;
4716 return (ch
>= '\uff01' && ch
<= '\uffbe')
4717 || (ch
>= '\uffc2' && ch
<= '\uffc7')
4718 || (ch
>= '\uffca' && ch
<= '\uffcf')
4719 || (ch
>= '\uffd2' && ch
<= '\uffd7')
4720 || (ch
>= '\uffda' && ch
<= '\uffdc')
4721 || (ch
>= '\uffe0' && ch
<= '\uffe6')
4722 || (ch
>= '\uffe8' && ch
<= '\uffee');
4725 function matchColor(r1
, g1
, b1
) {
4726 var hash
= (r1
<< 16) | (g1
<< 8) | b1
;
4728 if (matchColor
._cache
[hash
] != null) {
4729 return matchColor
._cache
[hash
];
4732 var ldiff
= Infinity
4741 for (; i
< Terminal
.vcolors
.length
; i
++) {
4742 c
= Terminal
.vcolors
[i
];
4747 diff
= matchColor
.distance(r1
, g1
, b1
, r2
, g2
, b2
);
4760 return matchColor
._cache
[hash
] = li
;
4763 matchColor
._cache
= {};
4765 // http://stackoverflow.com/questions/1633828
4766 matchColor
.distance = function(r1
, g1
, b1
, r2
, g2
, b2
) {
4767 return Math
.pow(30 * (r1
- r2
), 2)
4768 + Math
.pow(59 * (g1
- g2
), 2)
4769 + Math
.pow(11 * (b1
- b2
), 2);
4772 function each(obj
, iter
, con
) {
4773 if (obj
.forEach
) return obj
.forEach(iter
, con
);
4774 for (var i
= 0; i
< obj
.length
; i
++) {
4775 iter
.call(con
, obj
[i
], i
, obj
);
4779 function keys(obj
) {
4780 if (Object
.keys
) return Object
.keys(obj
);
4783 if (Object
.prototype.hasOwnProperty
.call(obj
, key
)) {
4794 Terminal
.EventEmitter
= EventEmitter
;
4795 Terminal
.inherits
= inherits
;
4798 Terminal
.cancel
= cancel
;