]>
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();
404 this.emit('focus', {terminal
: this});
407 Terminal
.prototype.blur = function() {
408 if (Terminal
.focus
!== this) {
412 this.cursorState
= 0;
413 this.refresh(this.y
, this.y
);
416 if (this.sendFocus
) {
419 Terminal
.focus
= null;
420 this.emit('blur', {terminal
: this});
424 * Initialize default behavior
426 Terminal
.prototype.initGlobal = function() {
427 Terminal
.bindKeys(this);
428 Terminal
.bindPaste(this);
429 Terminal
.bindCopy(this);
430 Terminal
.bindCut(this);
431 Terminal
.bindDrop(this);
435 * Clears all selected text, inside the terminal.
437 Terminal
.prototype.clearSelection = function() {
438 var selectionBaseNode
= window
.getSelection().baseNode
;
440 if (selectionBaseNode
&& (this.element
.contains(selectionBaseNode
.parentElement
))) {
441 window
.getSelection().removeAllRanges();
446 * This function temporarily enables (leases) the contentEditable value of the terminal, which
447 * should be set back to false within 5 seconds at most.
449 Terminal
.prototype.leaseContentEditable = function (ms
, callback
) {
452 term
.element
.contentEditable
= true;
453 setTimeout(function () {
454 term
.element
.contentEditable
= false;
455 if (typeof callback
== 'function') {
462 * Bind to paste event and allow both keyboard and right-click pasting, without having the
463 * contentEditable value set to true.
465 Terminal
.bindPaste = function(term
) {
466 on(term
.element
, 'paste', function(ev
) {
467 if (ev
.clipboardData
) {
468 var text
= ev
.clipboardData
.getData('text/plain');
469 term
.emit('paste', text
, ev
);
472 * Cancel the paste event, or else things will be pasted twice:
473 * 1. by the terminal handler
474 * 2. by the browser, because of the contentEditable value being true
476 term
.cancel(ev
, true);
479 * After the paste event is completed, always set the contentEditable value to false.
481 term
.element
.contentEditable
= false;
486 * Hack pasting with keyboard, in order to make it work without contentEditable.
487 * When a user types Ctrl + Shift + V or Cmd + V on a Mac, lease the contentEditable value
490 on(term
.element
, 'keydown', function (ev
) {
492 * If on a Mac, lease the contentEditable value temporarily, when the user presses
493 * the Cmd button, in order to cope with some sync issues on Safari.
495 if (term
.isMac
&& ev
.keyCode
== 91) {
496 term
.leaseContentEditable(1000);
499 if (ev
.keyCode
== 86) { // keyCode 96 corresponds to "v"
500 if (term
.isMac
&& ev
.metaKey
) {
501 term
.leaseContentEditable();
507 * Hack pasting with right-click in order to allow right-click paste, by leasing the
508 * contentEditable value as true.
510 on(term
.element
, 'contextmenu', function (ev
) {
511 term
.leaseContentEditable();
517 * Apply key handling to the terminal
519 Terminal
.bindKeys = function(term
) {
520 on(term
.element
, 'keydown', function(ev
) {
524 on(term
.element
, 'keypress', function(ev
) {
531 * Bind copy event. Stript trailing whitespaces from selection.
533 Terminal
.bindCopy = function(term
) {
534 on(term
.element
, 'copy', function(ev
) {
535 var selectedText
= window
.getSelection().toString(),
536 copiedText
= selectedText
.split('\n').map(function (element
) {
537 return element
.replace(/\s+$/g, '');
539 ev
.clipboardData
.setData('text/plain', copiedText
);
545 * Cancel the cut event completely
547 Terminal
.bindCut = function(term
) {
548 on(term
.element
, 'cut', function (ev
) {
554 Terminal
.bindDrop = function (term
) {
556 * Do not perform the "drop" event. Altering the contents of the
557 * terminal with drag n drop is unwanted behavior.
559 on(term
.element
, 'drop', function (ev
) {
560 term
.cancel(ev
, true);
565 Terminal
.click = function (term
) {
567 * Do not perform the "drop" event. Altering the contents of the
568 * terminal with drag n drop is unwanted behavior.
570 on(term
.element
, 'click', function (ev
) {
571 term
.cancel(ev
, true);
577 * Insert the given row to the terminal or produce a new one
578 * if no row argument is passed. Return the inserted row.
580 Terminal
.prototype.insertRow = function (row
) {
581 if (typeof row
!= 'object') {
582 row
= document
.createElement('div');
585 this.rowContainer
.appendChild(row
);
586 this.children
.push(row
);
593 * Open Terminal in the DOM
595 Terminal
.prototype.open = function(parent
) {
596 var self
=this, i
=0, div
;
598 this.parent
= parent
|| this.parent
;
601 throw new Error('Terminal requires a parent element.');
605 * Grab global elements
607 this.context
= this.parent
.ownerDocument
.defaultView
;
608 this.document
= this.parent
.ownerDocument
;
609 this.body
= this.document
.getElementsByTagName('body')[0];
614 if (this.context
.navigator
&& this.context
.navigator
.userAgent
) {
615 this.isMac
= !!~this.context
.navigator
.userAgent
.indexOf('Mac');
616 this.isIpad
= !!~this.context
.navigator
.userAgent
.indexOf('iPad');
617 this.isIphone
= !!~this.context
.navigator
.userAgent
.indexOf('iPhone');
618 this.isMSIE
= !!~this.context
.navigator
.userAgent
.indexOf('MSIE');
622 * Create main element container
624 this.element
= this.document
.createElement('div');
625 this.element
.classList
.add('terminal');
626 this.element
.classList
.add('xterm');
627 this.element
.classList
.add('xterm-theme-' + this.theme
);
628 this.element
.setAttribute('tabindex', 0);
629 this.element
.spellcheck
= 'false';
632 * Create the container that will hold the lines of the terminal and then
633 * produce the lines the lines.
635 this.rowContainer
= document
.createElement('div');
636 this.rowContainer
.classList
.add('xterm-rows');
637 this.element
.appendChild(this.rowContainer
);
640 for (; i
< this.rows
; i
++) {
643 this.parent
.appendChild(this.element
);
646 this.refresh(0, this.rows
- 1);
648 // Initialize global actions that
649 // need to be taken on the document.
652 // Ensure there is a Terminal.focus.
655 // Start blinking the cursor.
658 on(this.element
, 'mouseup', function() {
659 var selection
= document
.getSelection(),
660 collapsed
= selection
.isCollapsed
,
661 isRange
= typeof collapsed
== 'boolean' ? !collapsed
: selection
.type
== 'Range';
667 // Listen for mouse events and translate
668 // them into terminal mouse protocols.
671 // Figure out whether boldness affects
672 // the character width of monospace fonts.
673 if (Terminal
.brokenBold
== null) {
674 Terminal
.brokenBold
= isBoldBroken(this.document
);
680 // XTerm mouse events
681 // http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#Mouse%20Tracking
682 // To better understand these
683 // the xterm code is very helpful:
685 // button.c, charproc.c, misc.c
686 // Relevant functions in xterm/button.c:
687 // BtnCode, EmitButtonCode, EditorButton, SendMousePosition
688 Terminal
.prototype.bindMouse = function() {
689 var el
= this.element
693 var wheelEvent
= 'onmousewheel' in this.context
697 // mouseup, mousedown, mousewheel
698 // left click: ^[[M 3<^[[M#3<
699 // mousewheel up: ^[[M`3>
700 function sendButton(ev
) {
704 // get the xterm-style button
705 button
= getButton(ev
);
707 // get mouse coordinates
711 sendEvent(button
, pos
);
718 // keep it at the left
719 // button, just in case.
730 // motion example of a left click:
731 // ^[[M 3<^[[M@4<^[[M@5<^[[M@6<^[[M@7<^[[M#7<
732 function sendMove(ev
) {
739 // buttons marked as motions
740 // are incremented by 32
743 sendEvent(button
, pos
);
747 // position to characters
748 function encode(data
, ch
) {
749 if (!self
.utfMouse
) {
750 if (ch
=== 255) return data
.push(0);
751 if (ch
> 127) ch
= 127;
754 if (ch
=== 2047) return data
.push(0);
758 if (ch
> 2047) ch
= 2047;
759 data
.push(0xC0 | (ch
>> 6));
760 data
.push(0x80 | (ch
& 0x3F));
765 // send a mouse event:
766 // regular/utf8: ^[[M Cb Cx Cy
767 // urxvt: ^[[ Cb ; Cx ; Cy M
768 // sgr: ^[[ Cb ; Cx ; Cy M/m
769 // vt300: ^[[ 24(1/3/5)~ [ Cx , Cy ] \r
770 // locator: CSI P e ; P b ; P r ; P c ; P p & w
771 function sendEvent(button
, pos
) {
772 // self.emit('mouse', {
778 if (self
.vt300Mouse
) {
780 // http://www.vt100.net/docs/vt3xx-gp/chapter15.html
784 var data
= '\x1b[24';
785 if (button
=== 0) data
+= '1';
786 else if (button
=== 1) data
+= '3';
787 else if (button
=== 2) data
+= '5';
788 else if (button
=== 3) return;
790 data
+= '~[' + pos
.x
+ ',' + pos
.y
+ ']\r';
795 if (self
.decLocator
) {
800 if (button
=== 0) button
= 2;
801 else if (button
=== 1) button
= 4;
802 else if (button
=== 2) button
= 6;
803 else if (button
=== 3) button
= 3;
807 + (button
=== 3 ? 4 : 0)
818 if (self
.urxvtMouse
) {
823 self
.send('\x1b[' + button
+ ';' + pos
.x
+ ';' + pos
.y
+ 'M');
831 + ((button
& 3) === 3 ? button
& ~3 : button
)
836 + ((button
& 3) === 3 ? 'm' : 'M'));
842 encode(data
, button
);
846 self
.send('\x1b[M' + String
.fromCharCode
.apply(String
, data
));
849 function getButton(ev
) {
862 // 1, and 2 - with 64 added
865 button
= ev
.button
!= null
872 button
= button
=== 1 ? 0 : button
=== 4 ? 1 : button
;
878 case 'DOMMouseScroll':
879 button
= ev
.detail
< 0
884 button
= ev
.wheelDeltaY
> 0
890 // next three bits are the modifiers:
891 // 4 = shift, 8 = meta, 16 = control
892 shift
= ev
.shiftKey
? 4 : 0;
893 meta
= ev
.metaKey
? 8 : 0;
894 ctrl
= ev
.ctrlKey
? 16 : 0;
895 mod
= shift
| meta
| ctrl
;
898 if (self
.vt200Mouse
) {
901 } else if (!self
.normalMouse
) {
906 button
= (32 + (mod
<< 2)) + button
;
911 // mouse coordinates measured in cols/rows
912 function getCoords(ev
) {
915 // ignore browsers without pageX for now
916 if (ev
.pageX
== null) return;
922 // should probably check offsetParent
923 // but this is more portable
924 while (el
&& el
!== self
.document
.documentElement
) {
927 el
= 'offsetParent' in el
932 // convert to cols/rows
933 w
= self
.element
.clientWidth
;
934 h
= self
.element
.clientHeight
;
935 x
= Math
.round((x
/ w
) * self
.cols
);
936 y
= Math
.round((y
/ h
) * self
.rows
);
938 // be sure to avoid sending
939 // bad positions to the program
941 if (x
> self
.cols
) x
= self
.cols
;
943 if (y
> self
.rows
) y
= self
.rows
;
945 // xterm sends raw bytes and
946 // starts at 32 (SP) for each.
953 type
: ev
.type
=== wheelEvent
959 on(el
, 'mousedown', function(ev
) {
960 if (!self
.mouseEvents
) return;
969 //if (self.vt200Mouse && !self.normalMouse) {
970 if (self
.vt200Mouse
) {
971 sendButton({ __proto__
: ev
, type
: 'mouseup' });
972 return self
.cancel(ev
);
976 if (self
.normalMouse
) on(self
.document
, 'mousemove', sendMove
);
978 // x10 compatibility mode can't send button releases
979 if (!self
.x10Mouse
) {
980 on(self
.document
, 'mouseup', function up(ev
) {
982 if (self
.normalMouse
) off(self
.document
, 'mousemove', sendMove
);
983 off(self
.document
, 'mouseup', up
);
984 return self
.cancel(ev
);
988 return self
.cancel(ev
);
991 //if (self.normalMouse) {
992 // on(self.document, 'mousemove', sendMove);
995 on(el
, wheelEvent
, function(ev
) {
996 if (!self
.mouseEvents
) return;
999 || self
.decLocator
) return;
1001 return self
.cancel(ev
);
1004 // allow mousewheel scrolling in
1005 // the shell for example
1006 on(el
, wheelEvent
, function(ev
) {
1007 if (self
.mouseEvents
) return;
1008 if (self
.applicationKeypad
) return;
1009 if (ev
.type
=== 'DOMMouseScroll') {
1010 self
.scrollDisp(ev
.detail
< 0 ? -1 : 1);
1012 self
.scrollDisp(ev
.wheelDeltaY
> 0 ? -1 : 1);
1014 return self
.cancel(ev
);
1022 Terminal
.prototype.destroy = function() {
1023 this.readable
= false;
1024 this.writable
= false;
1026 this.handler = function() {};
1027 this.write = function() {};
1028 if (this.element
.parentNode
) {
1029 this.element
.parentNode
.removeChild(this.element
);
1031 //this.emit('close');
1036 * Flags used to render terminal text properly
1049 * In the screen buffer, each character
1050 * is stored as a an array with a character
1051 * and a 32-bit integer.
1052 * First value: a utf-16 character.
1054 * Next 9 bits: background color (0-511).
1055 * Next 9 bits: foreground color (0-511).
1056 * Next 14 bits: a mask for misc. flags:
1057 * 1=bold, 2=underline, 4=blink, 8=inverse, 16=invisible
1059 Terminal
.prototype.refresh = function(start
, end
) {
1060 var x
, y
, i
, line
, out
, ch
, width
, data
, attr
, bg
, fg
, flags
, row
, parent
, focused
= document
.activeElement
;
1062 if (end
- start
>= this.rows
/ 2) {
1063 parent
= this.element
.parentNode
;
1064 if (parent
) parent
.removeChild(this.element
);
1070 if (end
>= this.lines
.length
) {
1071 this.log('`end` is too large. Most likely a bad CSR.');
1072 end
= this.lines
.length
- 1;
1075 for (; y
<= end
; y
++) {
1076 row
= y
+ this.ydisp
;
1078 line
= this.lines
[row
];
1083 && (this.ydisp
=== this.ybase
)
1084 && !this.cursorHidden
) {
1090 attr
= this.defAttr
;
1093 for (; i
< width
; i
++) {
1097 if (i
=== x
) data
= -1;
1099 if (data
!== attr
) {
1100 if (attr
!== this.defAttr
) {
1103 if (data
!== this.defAttr
) {
1105 out
+= '<span class="reverse-video terminal-cursor">';
1107 var classNames
= [];
1110 fg
= (data
>> 9) & 0x1ff;
1113 if (flags
& Terminal
.flags
.BOLD
) {
1114 if (!Terminal
.brokenBold
) {
1115 classNames
.push('xterm-bold');
1117 // See: XTerm*boldColors
1118 if (fg
< 8) fg
+= 8;
1121 if (flags
& Terminal
.flags
.UNDERLINE
) {
1122 classNames
.push('xterm-underline');
1125 if (flags
& Terminal
.flags
.BLINK
) {
1126 classNames
.push('xterm-blink');
1130 * If inverse flag is on, then swap the foreground and background variables.
1132 if (flags
& Terminal
.flags
.INVERSE
) {
1133 /* One-line variable swap in JavaScript: http://stackoverflow.com/a/16201730 */
1134 bg = [fg, fg = bg][0];
1135 // Should inverse just be before the
1136 // above boldColors effect instead?
1137 if ((flags & 1) && fg < 8) fg += 8;
1140 if (flags & Terminal.flags.INVISIBLE) {
1141 classNames.push('xterm-hidden');
1145 * Weird situation: Invert flag used black foreground and white background results
1146 * in invalid background color, positioned at the 256 index of the 256 terminal
1147 * color map. Pin the colors manually in such a case.
1149 * Source: https://github.com/sourcelair/xterm.js/issues/57
1151 if (flags
& Terminal
.flags
.INVERSE
) {
1161 classNames
.push('xterm-bg-color-' + bg
);
1165 classNames
.push('xterm-color-' + fg
);
1169 if (classNames
.length
) {
1170 out
+= ' class="' + classNames
.join(' ') + '"';
1191 if (isWide(ch
)) i
++;
1200 if (attr
!== this.defAttr
) {
1204 this.children
[y
].innerHTML
= out
;
1208 parent
.appendChild(this.element
);
1212 * Return focus to previously focused element
1215 this.emit('refresh', {element
: this.element
, start
: start
, end
: end
});
1218 Terminal
.prototype._cursorBlink = function() {
1219 if (Terminal
.focus
!== this) return;
1220 this.cursorState
^= 1;
1221 this.refresh(this.y
, this.y
);
1224 Terminal
.prototype.showCursor = function() {
1225 if (!this.cursorState
) {
1226 this.cursorState
= 1;
1227 this.refresh(this.y
, this.y
);
1229 // Temporarily disabled:
1230 // this.refreshBlink();
1234 Terminal
.prototype.startBlink = function() {
1235 if (!this.cursorBlink
) return;
1237 this._blinker = function() {
1238 self
._cursorBlink();
1240 this._blink
= setInterval(this._blinker
, 500);
1243 Terminal
.prototype.refreshBlink = function() {
1244 if (!this.cursorBlink
) return;
1245 clearInterval(this._blink
);
1246 this._blink
= setInterval(this._blinker
, 500);
1249 Terminal
.prototype.scroll = function() {
1252 if (++this.ybase
=== this.scrollback
) {
1253 this.ybase
= this.ybase
/ 2 | 0;
1254 this.lines
= this.lines
.slice(-(this.ybase
+ this.rows
) + 1);
1257 this.ydisp
= this.ybase
;
1260 row
= this.ybase
+ this.rows
- 1;
1262 // subtract the bottom scroll region
1263 row
-= this.rows
- 1 - this.scrollBottom
;
1265 if (row
=== this.lines
.length
) {
1266 // potential optimization:
1267 // pushing is faster than splicing
1268 // when they amount to the same
1270 this.lines
.push(this.blankLine());
1273 this.lines
.splice(row
, 0, this.blankLine());
1276 if (this.scrollTop
!== 0) {
1277 if (this.ybase
!== 0) {
1279 this.ydisp
= this.ybase
;
1281 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
1285 this.updateRange(this.scrollTop
);
1286 this.updateRange(this.scrollBottom
);
1289 Terminal
.prototype.scrollDisp = function(disp
) {
1292 if (this.ydisp
> this.ybase
) {
1293 this.ydisp
= this.ybase
;
1294 } else if (this.ydisp
< 0) {
1298 this.refresh(0, this.rows
- 1);
1301 Terminal
.prototype.write = function(data
) {
1302 var l
= data
.length
, i
= 0, j
, cs
, ch
;
1304 this.refreshStart
= this.y
;
1305 this.refreshEnd
= this.y
;
1307 if (this.ybase
!== this.ydisp
) {
1308 this.ydisp
= this.ybase
;
1312 for (; i
< l
; i
++) {
1314 switch (this.state
) {
1325 if (this.convertEol
) {
1329 if (this.y
> this.scrollBottom
) {
1349 this.x
= this.nextStop();
1364 this.state
= escaped
;
1370 if (this.charset
&& this.charset
[ch
]) {
1371 ch
= this.charset
[ch
];
1374 if (this.x
>= this.cols
) {
1377 if (this.y
> this.scrollBottom
) {
1383 this.lines
[this.y
+ this.ybase
][this.x
] = [this.curAttr
, ch
];
1385 this.updateRange(this.y
);
1388 j
= this.y
+ this.ybase
;
1389 if (this.cols
< 2 || this.x
>= this.cols
) {
1390 this.lines
[j
][this.x
- 1] = [this.curAttr
, ' '];
1393 this.lines
[j
][this.x
] = [this.curAttr
, ' '];
1402 // ESC [ Control Sequence Introducer ( CSI is 0x9b).
1405 this.currentParam
= 0;
1409 // ESC ] Operating System Command ( OSC is 0x9d).
1412 this.currentParam
= 0;
1416 // ESC P Device Control String ( DCS is 0x90).
1419 this.currentParam
= 0;
1423 // ESC _ Application Program Command ( APC is 0x9f).
1425 this.state
= ignore
;
1428 // ESC ^ Privacy Message ( PM is 0x9e).
1430 this.state
= ignore
;
1433 // ESC c Full Reset (RIS).
1438 // ESC E Next Line ( NEL is 0x85).
1439 // ESC D Index ( IND is 0x84).
1447 // ESC M Reverse Index ( RI is 0x8d).
1449 this.reverseIndex();
1452 // ESC % Select default/utf-8 character set.
1453 // @ = default, G = utf-8
1455 //this.charset = null;
1457 this.setgCharset(0, Terminal
.charsets
.US
);
1458 this.state
= normal
;
1462 // ESC (,),*,+,-,. Designate G0-G2 Character Set.
1463 case '(': // <-- this seems to get all the attention
1489 this.state
= charset
;
1492 // Designate G3 Character Set (VT300).
1493 // A = ISO Latin-1 Supplemental.
1497 this.state
= charset
;
1502 // Single Shift Select of G2 Character Set
1503 // ( SS2 is 0x8e). This affects next character only.
1507 // Single Shift Select of G3 Character Set
1508 // ( SS3 is 0x8f). This affects next character only.
1512 // Invoke the G2 Character Set as GL (LS2).
1517 // Invoke the G3 Character Set as GL (LS3).
1522 // Invoke the G3 Character Set as GR (LS3R).
1527 // Invoke the G2 Character Set as GR (LS2R).
1532 // Invoke the G1 Character Set as GR (LS1R).
1537 // ESC 7 Save Cursor (DECSC).
1540 this.state
= normal
;
1543 // ESC 8 Restore Cursor (DECRC).
1545 this.restoreCursor();
1546 this.state
= normal
;
1549 // ESC # 3 DEC line height/width
1551 this.state
= normal
;
1555 // ESC H Tab Set (HTS is 0x88).
1560 // ESC = Application Keypad (DECPAM).
1562 this.log('Serial port requested application keypad.');
1563 this.applicationKeypad
= true;
1564 this.state
= normal
;
1567 // ESC > Normal Keypad (DECPNM).
1569 this.log('Switching back to normal keypad.');
1570 this.applicationKeypad
= false;
1571 this.state
= normal
;
1575 this.state
= normal
;
1576 this.error('Unknown ESC control: %s.', ch
);
1583 case '0': // DEC Special Character and Line Drawing Set.
1584 cs
= Terminal
.charsets
.SCLD
;
1587 cs
= Terminal
.charsets
.UK
;
1589 case 'B': // United States (USASCII).
1590 cs
= Terminal
.charsets
.US
;
1593 cs
= Terminal
.charsets
.Dutch
;
1595 case 'C': // Finnish
1597 cs
= Terminal
.charsets
.Finnish
;
1600 cs
= Terminal
.charsets
.French
;
1602 case 'Q': // FrenchCanadian
1603 cs
= Terminal
.charsets
.FrenchCanadian
;
1606 cs
= Terminal
.charsets
.German
;
1608 case 'Y': // Italian
1609 cs
= Terminal
.charsets
.Italian
;
1611 case 'E': // NorwegianDanish
1613 cs
= Terminal
.charsets
.NorwegianDanish
;
1615 case 'Z': // Spanish
1616 cs
= Terminal
.charsets
.Spanish
;
1618 case 'H': // Swedish
1620 cs
= Terminal
.charsets
.Swedish
;
1623 cs
= Terminal
.charsets
.Swiss
;
1625 case '/': // ISOLatin (actually /A)
1626 cs
= Terminal
.charsets
.ISOLatin
;
1630 cs
= Terminal
.charsets
.US
;
1633 this.setgCharset(this.gcharset
, cs
);
1634 this.gcharset
= null;
1635 this.state
= normal
;
1641 // Set Text Parameters.
1642 if (ch
=== '\x1b' || ch
=== '\x07') {
1643 if (ch
=== '\x1b') i
++;
1645 this.params
.push(this.currentParam
);
1647 switch (this.params
[0]) {
1651 if (this.params
[1]) {
1652 this.title
= this.params
[1];
1653 this.handleTitle(this.title
);
1661 // change dynamic colors
1673 // change dynamic ui colors
1685 // manipulate selection data
1703 this.currentParam
= 0;
1704 this.state
= normal
;
1706 if (!this.params
.length
) {
1707 if (ch
>= '0' && ch
<= '9') {
1709 this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1710 } else if (ch
=== ';') {
1711 this.params
.push(this.currentParam
);
1712 this.currentParam
= '';
1715 this.currentParam
+= ch
;
1722 if (ch
=== '?' || ch
=== '>' || ch
=== '!') {
1728 if (ch
>= '0' && ch
<= '9') {
1729 this.currentParam
= this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1733 // '$', '"', ' ', '\''
1734 if (ch
=== '$' || ch
=== '"' || ch
=== ' ' || ch
=== '\'') {
1739 this.params
.push(this.currentParam
);
1740 this.currentParam
= 0;
1743 if (ch
=== ';') break;
1745 this.state
= normal
;
1749 // Cursor Up Ps Times (default = 1) (CUU).
1751 this.cursorUp(this.params
);
1755 // Cursor Down Ps Times (default = 1) (CUD).
1757 this.cursorDown(this.params
);
1761 // Cursor Forward Ps Times (default = 1) (CUF).
1763 this.cursorForward(this.params
);
1767 // Cursor Backward Ps Times (default = 1) (CUB).
1769 this.cursorBackward(this.params
);
1773 // Cursor Position [row;column] (default = [1,1]) (CUP).
1775 this.cursorPos(this.params
);
1778 // CSI Ps J Erase in Display (ED).
1780 this.eraseInDisplay(this.params
);
1783 // CSI Ps K Erase in Line (EL).
1785 this.eraseInLine(this.params
);
1788 // CSI Pm m Character Attributes (SGR).
1791 this.charAttributes(this.params
);
1795 // CSI Ps n Device Status Report (DSR).
1798 this.deviceStatus(this.params
);
1807 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
1809 this.insertChars(this.params
);
1813 // Cursor Next Line Ps Times (default = 1) (CNL).
1815 this.cursorNextLine(this.params
);
1819 // Cursor Preceding Line Ps Times (default = 1) (CNL).
1821 this.cursorPrecedingLine(this.params
);
1825 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
1827 this.cursorCharAbsolute(this.params
);
1831 // Insert Ps Line(s) (default = 1) (IL).
1833 this.insertLines(this.params
);
1837 // Delete Ps Line(s) (default = 1) (DL).
1839 this.deleteLines(this.params
);
1843 // Delete Ps Character(s) (default = 1) (DCH).
1845 this.deleteChars(this.params
);
1849 // Erase Ps Character(s) (default = 1) (ECH).
1851 this.eraseChars(this.params
);
1854 // CSI Pm ` Character Position Absolute
1855 // [column] (default = [row,1]) (HPA).
1857 this.charPosAbsolute(this.params
);
1861 // Horizontal Position Relative
1863 this.HPositionRelative(this.params
);
1867 // Send Device Attributes (Primary DA).
1869 // Send Device Attributes (Secondary DA)
1871 this.sendDeviceAttributes(this.params
);
1875 // Line Position Absolute [row] (default = [1,column]) (VPA).
1877 this.linePosAbsolute(this.params
);
1880 // 145 65 e * VPR - Vertical Position Relative
1882 this.VPositionRelative(this.params
);
1886 // Horizontal and Vertical Position [row;column] (default =
1889 this.HVPosition(this.params
);
1892 // CSI Pm h Set Mode (SM).
1893 // CSI ? Pm h - mouse escape codes, cursor escape codes
1895 this.setMode(this.params
);
1898 // CSI Pm l Reset Mode (RM).
1901 this.resetMode(this.params
);
1905 // Set Scrolling Region [top;bottom] (default = full size of win-
1909 this.setScrollRegion(this.params
);
1913 // Save cursor (ANSI.SYS).
1915 this.saveCursor(this.params
);
1919 // Restore cursor (ANSI.SYS).
1921 this.restoreCursor(this.params
);
1929 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
1931 this.cursorForwardTab(this.params
);
1934 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
1936 this.scrollUp(this.params
);
1939 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
1940 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
1943 // if (this.prefix === '>') {
1944 // this.resetTitleModes(this.params);
1947 // if (this.params.length > 2) {
1948 // this.initMouseTracking(this.params);
1951 if (this.params
.length
< 2 && !this.prefix
) {
1952 this.scrollDown(this.params
);
1957 // Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
1959 this.cursorBackwardTab(this.params
);
1962 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
1964 this.repeatPrecedingCharacter(this.params
);
1967 // CSI Ps g Tab Clear (TBC).
1969 this.tabClear(this.params
);
1972 // CSI Pm i Media Copy (MC).
1975 // this.mediaCopy(this.params);
1978 // CSI Pm m Character Attributes (SGR).
1980 // case 'm': // duplicate
1981 // if (this.prefix === '>') {
1982 // this.setResources(this.params);
1984 // this.charAttributes(this.params);
1988 // CSI Ps n Device Status Report (DSR).
1990 // case 'n': // duplicate
1991 // if (this.prefix === '>') {
1992 // this.disableModifiers(this.params);
1994 // this.deviceStatus(this.params);
1998 // CSI > Ps p Set pointer mode.
1999 // CSI ! p Soft terminal reset (DECSTR).
2001 // Request ANSI mode (DECRQM).
2003 // Request DEC private mode (DECRQM).
2006 switch (this.prefix
) {
2008 // this.setPointerMode(this.params);
2011 this.softReset(this.params
);
2014 // if (this.postfix === '$') {
2015 // this.requestPrivateMode(this.params);
2019 // if (this.postfix === '"') {
2020 // this.setConformanceLevel(this.params);
2021 // } else if (this.postfix === '$') {
2022 // this.requestAnsiMode(this.params);
2028 // CSI Ps q Load LEDs (DECLL).
2032 // if (this.postfix === ' ') {
2033 // this.setCursorStyle(this.params);
2036 // if (this.postfix === '"') {
2037 // this.setCharProtectionAttr(this.params);
2040 // this.loadLEDs(this.params);
2044 // Set Scrolling Region [top;bottom] (default = full size of win-
2047 // CSI Pt; Pl; Pb; Pr; Ps$ r
2048 // case 'r': // duplicate
2049 // if (this.prefix === '?') {
2050 // this.restorePrivateValues(this.params);
2051 // } else if (this.postfix === '$') {
2052 // this.setAttrInRectangle(this.params);
2054 // this.setScrollRegion(this.params);
2058 // CSI s Save cursor (ANSI.SYS).
2060 // case 's': // duplicate
2061 // if (this.prefix === '?') {
2062 // this.savePrivateValues(this.params);
2064 // this.saveCursor(this.params);
2068 // CSI Ps ; Ps ; Ps t
2069 // CSI Pt; Pl; Pb; Pr; Ps$ t
2073 // if (this.postfix === '$') {
2074 // this.reverseAttrInRectangle(this.params);
2075 // } else if (this.postfix === ' ') {
2076 // this.setWarningBellVolume(this.params);
2078 // if (this.prefix === '>') {
2079 // this.setTitleModeFeature(this.params);
2081 // this.manipulateWindow(this.params);
2086 // CSI u Restore cursor (ANSI.SYS).
2088 // case 'u': // duplicate
2089 // if (this.postfix === ' ') {
2090 // this.setMarginBellVolume(this.params);
2092 // this.restoreCursor(this.params);
2096 // CSI Pt; Pl; Pb; Pr; Pp; Pt; Pl; Pp$ v
2098 // if (this.postfix === '$') {
2099 // this.copyRectagle(this.params);
2103 // CSI Pt ; Pl ; Pb ; Pr ' w
2105 // if (this.postfix === '\'') {
2106 // this.enableFilterRectangle(this.params);
2110 // CSI Ps x Request Terminal Parameters (DECREQTPARM).
2111 // CSI Ps x Select Attribute Change Extent (DECSACE).
2112 // CSI Pc; Pt; Pl; Pb; Pr$ x
2114 // if (this.postfix === '$') {
2115 // this.fillRectangle(this.params);
2117 // this.requestParameters(this.params);
2118 // //this.__(this.params);
2123 // CSI Pt; Pl; Pb; Pr$ z
2125 // if (this.postfix === '\'') {
2126 // this.enableLocatorReporting(this.params);
2127 // } else if (this.postfix === '$') {
2128 // this.eraseRectangle(this.params);
2133 // CSI Pt; Pl; Pb; Pr$ {
2135 // if (this.postfix === '\'') {
2136 // this.setLocatorEvents(this.params);
2137 // } else if (this.postfix === '$') {
2138 // this.selectiveEraseRectangle(this.params);
2144 // if (this.postfix === '\'') {
2145 // this.requestLocatorPosition(this.params);
2150 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
2152 // if (this.postfix === ' ') {
2153 // this.insertColumns(this.params);
2158 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
2160 // if (this.postfix === ' ') {
2161 // this.deleteColumns(this.params);
2166 this.error('Unknown CSI code: %s.', ch
);
2175 if (ch
=== '\x1b' || ch
=== '\x07') {
2176 if (ch
=== '\x1b') i
++;
2178 switch (this.prefix
) {
2179 // User-Defined Keys (DECUDK).
2183 // Request Status String (DECRQSS).
2184 // test: echo -e '\eP$q"p\e\\'
2186 var pt
= this.currentParam
2203 + (this.scrollTop
+ 1)
2205 + (this.scrollBottom
+ 1)
2215 this.error('Unknown DCS Pt: %s.', pt
);
2220 this.send('\x1bP' + +valid
+ '$r' + pt
+ '\x1b\\');
2223 // Set Termcap/Terminfo Data (xterm, experimental).
2227 // Request Termcap/Terminfo String (xterm, experimental)
2228 // Regular xterm does not even respond to this sequence.
2229 // This can cause a small glitch in vim.
2230 // test: echo -ne '\eP+q6b64\e\\'
2232 var pt
= this.currentParam
2235 this.send('\x1bP' + +valid
+ '+r' + pt
+ '\x1b\\');
2239 this.error('Unknown DCS prefix: %s.', this.prefix
);
2243 this.currentParam
= 0;
2245 this.state
= normal
;
2246 } else if (!this.currentParam
) {
2247 if (!this.prefix
&& ch
!== '$' && ch
!== '+') {
2248 this.currentParam
= ch
;
2249 } else if (this.prefix
.length
=== 2) {
2250 this.currentParam
= ch
;
2255 this.currentParam
+= ch
;
2261 if (ch
=== '\x1b' || ch
=== '\x07') {
2262 if (ch
=== '\x1b') i
++;
2263 this.state
= normal
;
2269 this.updateRange(this.y
);
2270 this.refresh(this.refreshStart
, this.refreshEnd
);
2273 Terminal
.prototype.writeln = function(data
) {
2274 this.write(data
+ '\r\n');
2278 // https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
2279 Terminal
.prototype.keyDown = function(ev
) {
2280 var self
= this, key
;
2282 switch (ev
.keyCode
) {
2298 this.cancel(ev
, true);
2303 this.cancel(ev
, true);
2308 this.cancel(ev
, true);
2313 this.cancel(ev
, true);
2314 key
= '\x1bb' // Jump a word back
2316 } else if (this.applicationCursor
) {
2317 key
= '\x1bOD'; // SS3 as ^[O for 7-bit
2325 this.cancel(ev
, true);
2326 key
= '\x1bf' // Jump a word forward
2328 } else if (this.applicationCursor
) {
2336 if (this.applicationCursor
) {
2341 this.scrollDisp(-1);
2342 return this.cancel(ev
);
2349 if (this.applicationCursor
) {
2355 return this.cancel(ev
);
2370 if (this.applicationKeypad
) {
2378 if (this.applicationKeypad
) {
2387 this.scrollDisp(-(this.rows
- 1));
2388 return this.cancel(ev
);
2396 this.scrollDisp(this.rows
- 1);
2397 return this.cancel(ev
);
2452 if (ev
.ctrlKey
&& !ev
.shiftKey
&& !ev
.altKey
&& !ev
.metaKey
) {
2453 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2454 key
= String
.fromCharCode(ev
.keyCode
- 64);
2455 } else if (ev
.keyCode
=== 32) {
2457 key
= String
.fromCharCode(0);
2458 } else if (ev
.keyCode
>= 51 && ev
.keyCode
<= 55) {
2459 // escape, file sep, group sep, record sep, unit sep
2460 key
= String
.fromCharCode(ev
.keyCode
- 51 + 27);
2461 } else if (ev
.keyCode
=== 56) {
2463 key
= String
.fromCharCode(127);
2464 } else if (ev
.keyCode
=== 219) {
2466 key
= String
.fromCharCode(27);
2467 } else if (ev
.keyCode
=== 221) {
2469 key
= String
.fromCharCode(29);
2471 } else if ((!this.isMac
&& ev
.altKey
) || (this.isMac
&& ev
.metaKey
)) {
2472 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2473 key
= '\x1b' + String
.fromCharCode(ev
.keyCode
+ 32);
2474 } else if (ev
.keyCode
=== 192) {
2476 } else if (ev
.keyCode
>= 48 && ev
.keyCode
<= 57) {
2477 key
= '\x1b' + (ev
.keyCode
- 48);
2483 if (!key
|| (this.isMac
&& ev
.metaKey
)) {
2487 this.emit('keydown', ev
);
2488 this.emit('key', key
, ev
);
2492 return this.cancel(ev
, true);
2495 Terminal
.prototype.setgLevel = function(g
) {
2497 this.charset
= this.charsets
[g
];
2500 Terminal
.prototype.setgCharset = function(g
, charset
) {
2501 this.charsets
[g
] = charset
;
2502 if (this.glevel
=== g
) {
2503 this.charset
= charset
;
2507 Terminal
.prototype.keyPress = function(ev
) {
2512 } else if (ev
.which
== null) {
2514 } else if (ev
.which
!== 0 && ev
.charCode
!== 0) {
2520 if (!key
|| ev
.ctrlKey
|| ev
.altKey
|| ev
.metaKey
) {
2524 key
= String
.fromCharCode(key
);
2527 * When a key is pressed and a character is sent to the terminal, then clear any text
2528 * selected in the terminal.
2531 this.clearSelection();
2534 this.emit('keypress', key
, ev
);
2535 this.emit('key', key
, ev
);
2539 this.cancel(ev
, true);
2544 Terminal
.prototype.send = function(data
) {
2548 setTimeout(function() {
2549 self
.handler(self
.queue
);
2557 Terminal
.prototype.bell = function() {
2558 if (!this.visualBell
) return;
2560 this.element
.style
.borderColor
= 'white';
2561 setTimeout(function() {
2562 self
.element
.style
.borderColor
= '';
2564 if (this.popOnBell
) this.focus();
2567 Terminal
.prototype.log = function() {
2568 if (!this.debug
) return;
2569 if (!this.context
.console
|| !this.context
.console
.log
) return;
2570 var args
= Array
.prototype.slice
.call(arguments
);
2571 this.context
.console
.log
.apply(this.context
.console
, args
);
2574 Terminal
.prototype.error = function() {
2575 if (!this.debug
) return;
2576 if (!this.context
.console
|| !this.context
.console
.error
) return;
2577 var args
= Array
.prototype.slice
.call(arguments
);
2578 this.context
.console
.error
.apply(this.context
.console
, args
);
2581 Terminal
.prototype.resize = function(x
, y
) {
2594 ch
= [this.defAttr
, ' ']; // does xterm use the default attr?
2595 i
= this.lines
.length
;
2597 while (this.lines
[i
].length
< x
) {
2598 this.lines
[i
].push(ch
);
2602 i
= this.lines
.length
;
2604 while (this.lines
[i
].length
> x
) {
2605 this.lines
[i
].pop();
2617 if (this.lines
.length
< y
+ this.ybase
) {
2618 this.lines
.push(this.blankLine());
2620 if (this.children
.length
< y
) {
2626 if (this.lines
.length
> y
+ this.ybase
) {
2629 if (this.children
.length
> y
) {
2630 el
= this.children
.shift();
2632 el
.parentNode
.removeChild(el
);
2639 * Make sure that the cursor stays on screen
2650 this.scrollBottom
= y
- 1;
2652 this.refresh(0, this.rows
- 1);
2656 this.emit('resize', {terminal
: this, cols
: x
, rows
: y
});
2659 Terminal
.prototype.updateRange = function(y
) {
2660 if (y
< this.refreshStart
) this.refreshStart
= y
;
2661 if (y
> this.refreshEnd
) this.refreshEnd
= y
;
2662 // if (y > this.refreshEnd) {
2663 // this.refreshEnd = y;
2664 // if (y > this.rows - 1) {
2665 // this.refreshEnd = this.rows - 1;
2670 Terminal
.prototype.maxRange = function() {
2671 this.refreshStart
= 0;
2672 this.refreshEnd
= this.rows
- 1;
2675 Terminal
.prototype.setupStops = function(i
) {
2677 if (!this.tabs
[i
]) {
2678 i
= this.prevStop(i
);
2685 for (; i
< this.cols
; i
+= 8) {
2686 this.tabs
[i
] = true;
2690 Terminal
.prototype.prevStop = function(x
) {
2691 if (x
== null) x
= this.x
;
2692 while (!this.tabs
[--x
] && x
> 0);
2693 return x
>= this.cols
2698 Terminal
.prototype.nextStop = function(x
) {
2699 if (x
== null) x
= this.x
;
2700 while (!this.tabs
[++x
] && x
< this.cols
);
2701 return x
>= this.cols
2706 Terminal
.prototype.eraseRight = function(x
, y
) {
2707 var line
= this.lines
[this.ybase
+ y
]
2708 , ch
= [this.eraseAttr(), ' ']; // xterm
2711 for (; x
< this.cols
; x
++) {
2715 this.updateRange(y
);
2718 Terminal
.prototype.eraseLeft = function(x
, y
) {
2719 var line
= this.lines
[this.ybase
+ y
]
2720 , ch
= [this.eraseAttr(), ' ']; // xterm
2723 while (x
--) line
[x
] = ch
;
2725 this.updateRange(y
);
2728 Terminal
.prototype.eraseLine = function(y
) {
2729 this.eraseRight(0, y
);
2732 Terminal
.prototype.blankLine = function(cur
) {
2737 var ch
= [attr
, ' ']
2741 for (; i
< this.cols
; i
++) {
2748 Terminal
.prototype.ch = function(cur
) {
2750 ? [this.eraseAttr(), ' ']
2751 : [this.defAttr
, ' '];
2754 Terminal
.prototype.is = function(term
) {
2755 var name
= this.termName
;
2756 return (name
+ '').indexOf(term
) === 0;
2759 Terminal
.prototype.handler = function(data
) {
2760 this.emit('data', data
);
2763 Terminal
.prototype.handleTitle = function(title
) {
2764 this.emit('title', title
);
2771 // ESC D Index (IND is 0x84).
2772 Terminal
.prototype.index = function() {
2774 if (this.y
> this.scrollBottom
) {
2778 this.state
= normal
;
2781 // ESC M Reverse Index (RI is 0x8d).
2782 Terminal
.prototype.reverseIndex = function() {
2785 if (this.y
< this.scrollTop
) {
2787 // possibly move the code below to term.reverseScroll();
2788 // test: echo -ne '\e[1;1H\e[44m\eM\e[0m'
2789 // blankLine(true) is xterm/linux behavior
2790 this.lines
.splice(this.y
+ this.ybase
, 0, this.blankLine(true));
2791 j
= this.rows
- 1 - this.scrollBottom
;
2792 this.lines
.splice(this.rows
- 1 + this.ybase
- j
+ 1, 1);
2794 this.updateRange(this.scrollTop
);
2795 this.updateRange(this.scrollBottom
);
2797 this.state
= normal
;
2800 // ESC c Full Reset (RIS).
2801 Terminal
.prototype.reset = function() {
2802 this.options
.rows
= this.rows
;
2803 this.options
.cols
= this.cols
;
2804 Terminal
.call(this, this.options
);
2805 this.refresh(0, this.rows
- 1);
2808 // ESC H Tab Set (HTS is 0x88).
2809 Terminal
.prototype.tabSet = function() {
2810 this.tabs
[this.x
] = true;
2811 this.state
= normal
;
2819 // Cursor Up Ps Times (default = 1) (CUU).
2820 Terminal
.prototype.cursorUp = function(params
) {
2821 var param
= params
[0];
2822 if (param
< 1) param
= 1;
2824 if (this.y
< 0) this.y
= 0;
2828 // Cursor Down Ps Times (default = 1) (CUD).
2829 Terminal
.prototype.cursorDown = function(params
) {
2830 var param
= params
[0];
2831 if (param
< 1) param
= 1;
2833 if (this.y
>= this.rows
) {
2834 this.y
= this.rows
- 1;
2839 // Cursor Forward Ps Times (default = 1) (CUF).
2840 Terminal
.prototype.cursorForward = function(params
) {
2841 var param
= params
[0];
2842 if (param
< 1) param
= 1;
2844 if (this.x
>= this.cols
) {
2845 this.x
= this.cols
- 1;
2850 // Cursor Backward Ps Times (default = 1) (CUB).
2851 Terminal
.prototype.cursorBackward = function(params
) {
2852 var param
= params
[0];
2853 if (param
< 1) param
= 1;
2855 if (this.x
< 0) this.x
= 0;
2859 // Cursor Position [row;column] (default = [1,1]) (CUP).
2860 Terminal
.prototype.cursorPos = function(params
) {
2863 row
= params
[0] - 1;
2865 if (params
.length
>= 2) {
2866 col
= params
[1] - 1;
2873 } else if (row
>= this.rows
) {
2874 row
= this.rows
- 1;
2879 } else if (col
>= this.cols
) {
2880 col
= this.cols
- 1;
2887 // CSI Ps J Erase in Display (ED).
2888 // Ps = 0 -> Erase Below (default).
2889 // Ps = 1 -> Erase Above.
2890 // Ps = 2 -> Erase All.
2891 // Ps = 3 -> Erase Saved Lines (xterm).
2893 // Erase in Display (DECSED).
2894 // Ps = 0 -> Selective Erase Below (default).
2895 // Ps = 1 -> Selective Erase Above.
2896 // Ps = 2 -> Selective Erase All.
2897 Terminal
.prototype.eraseInDisplay = function(params
) {
2899 switch (params
[0]) {
2901 this.eraseRight(this.x
, this.y
);
2903 for (; j
< this.rows
; j
++) {
2908 this.eraseLeft(this.x
, this.y
);
2916 while (j
--) this.eraseLine(j
);
2924 // CSI Ps K Erase in Line (EL).
2925 // Ps = 0 -> Erase to Right (default).
2926 // Ps = 1 -> Erase to Left.
2927 // Ps = 2 -> Erase All.
2929 // Erase in Line (DECSEL).
2930 // Ps = 0 -> Selective Erase to Right (default).
2931 // Ps = 1 -> Selective Erase to Left.
2932 // Ps = 2 -> Selective Erase All.
2933 Terminal
.prototype.eraseInLine = function(params
) {
2934 switch (params
[0]) {
2936 this.eraseRight(this.x
, this.y
);
2939 this.eraseLeft(this.x
, this.y
);
2942 this.eraseLine(this.y
);
2947 // CSI Pm m Character Attributes (SGR).
2948 // Ps = 0 -> Normal (default).
2950 // Ps = 4 -> Underlined.
2951 // Ps = 5 -> Blink (appears as Bold).
2952 // Ps = 7 -> Inverse.
2953 // Ps = 8 -> Invisible, i.e., hidden (VT300).
2954 // Ps = 2 2 -> Normal (neither bold nor faint).
2955 // Ps = 2 4 -> Not underlined.
2956 // Ps = 2 5 -> Steady (not blinking).
2957 // Ps = 2 7 -> Positive (not inverse).
2958 // Ps = 2 8 -> Visible, i.e., not hidden (VT300).
2959 // Ps = 3 0 -> Set foreground color to Black.
2960 // Ps = 3 1 -> Set foreground color to Red.
2961 // Ps = 3 2 -> Set foreground color to Green.
2962 // Ps = 3 3 -> Set foreground color to Yellow.
2963 // Ps = 3 4 -> Set foreground color to Blue.
2964 // Ps = 3 5 -> Set foreground color to Magenta.
2965 // Ps = 3 6 -> Set foreground color to Cyan.
2966 // Ps = 3 7 -> Set foreground color to White.
2967 // Ps = 3 9 -> Set foreground color to default (original).
2968 // Ps = 4 0 -> Set background color to Black.
2969 // Ps = 4 1 -> Set background color to Red.
2970 // Ps = 4 2 -> Set background color to Green.
2971 // Ps = 4 3 -> Set background color to Yellow.
2972 // Ps = 4 4 -> Set background color to Blue.
2973 // Ps = 4 5 -> Set background color to Magenta.
2974 // Ps = 4 6 -> Set background color to Cyan.
2975 // Ps = 4 7 -> Set background color to White.
2976 // Ps = 4 9 -> Set background color to default (original).
2978 // If 16-color support is compiled, the following apply. Assume
2979 // that xterm's resources are set so that the ISO color codes are
2980 // the first 8 of a set of 16. Then the aixterm colors are the
2981 // bright versions of the ISO colors:
2982 // Ps = 9 0 -> Set foreground color to Black.
2983 // Ps = 9 1 -> Set foreground color to Red.
2984 // Ps = 9 2 -> Set foreground color to Green.
2985 // Ps = 9 3 -> Set foreground color to Yellow.
2986 // Ps = 9 4 -> Set foreground color to Blue.
2987 // Ps = 9 5 -> Set foreground color to Magenta.
2988 // Ps = 9 6 -> Set foreground color to Cyan.
2989 // Ps = 9 7 -> Set foreground color to White.
2990 // Ps = 1 0 0 -> Set background color to Black.
2991 // Ps = 1 0 1 -> Set background color to Red.
2992 // Ps = 1 0 2 -> Set background color to Green.
2993 // Ps = 1 0 3 -> Set background color to Yellow.
2994 // Ps = 1 0 4 -> Set background color to Blue.
2995 // Ps = 1 0 5 -> Set background color to Magenta.
2996 // Ps = 1 0 6 -> Set background color to Cyan.
2997 // Ps = 1 0 7 -> Set background color to White.
2999 // If xterm is compiled with the 16-color support disabled, it
3000 // supports the following, from rxvt:
3001 // Ps = 1 0 0 -> Set foreground and background color to
3004 // If 88- or 256-color support is compiled, the following apply.
3005 // Ps = 3 8 ; 5 ; Ps -> Set foreground color to the second
3007 // Ps = 4 8 ; 5 ; Ps -> Set background color to the second
3009 Terminal
.prototype.charAttributes = function(params
) {
3010 // Optimize a single SGR0.
3011 if (params
.length
=== 1 && params
[0] === 0) {
3012 this.curAttr
= this.defAttr
;
3016 var l
= params
.length
3018 , flags
= this.curAttr
>> 18
3019 , fg
= (this.curAttr
>> 9) & 0x1ff
3020 , bg
= this.curAttr
& 0x1ff
3023 for (; i
< l
; i
++) {
3025 if (p
>= 30 && p
<= 37) {
3028 } else if (p
>= 40 && p
<= 47) {
3031 } else if (p
>= 90 && p
<= 97) {
3035 } else if (p
>= 100 && p
<= 107) {
3039 } else if (p
=== 0) {
3041 flags
= this.defAttr
>> 18;
3042 fg
= (this.defAttr
>> 9) & 0x1ff;
3043 bg
= this.defAttr
& 0x1ff;
3047 } else if (p
=== 1) {
3050 } else if (p
=== 4) {
3053 } else if (p
=== 5) {
3056 } else if (p
=== 7) {
3057 // inverse and positive
3058 // test with: echo -e '\e[31m\e[42mhello\e[7mworld\e[27mhi\e[m'
3060 } else if (p
=== 8) {
3063 } else if (p
=== 22) {
3066 } else if (p
=== 24) {
3069 } else if (p
=== 25) {
3072 } else if (p
=== 27) {
3075 } else if (p
=== 28) {
3078 } else if (p
=== 39) {
3080 fg
= (this.defAttr
>> 9) & 0x1ff;
3081 } else if (p
=== 49) {
3083 bg
= this.defAttr
& 0x1ff;
3084 } else if (p
=== 38) {
3086 if (params
[i
+ 1] === 2) {
3090 params
[i
+ 1] & 0xff,
3091 params
[i
+ 2] & 0xff);
3092 if (fg
=== -1) fg
= 0x1ff;
3094 } else if (params
[i
+ 1] === 5) {
3096 p
= params
[i
] & 0xff;
3099 } else if (p
=== 48) {
3101 if (params
[i
+ 1] === 2) {
3105 params
[i
+ 1] & 0xff,
3106 params
[i
+ 2] & 0xff);
3107 if (bg
=== -1) bg
= 0x1ff;
3109 } else if (params
[i
+ 1] === 5) {
3111 p
= params
[i
] & 0xff;
3114 } else if (p
=== 100) {
3116 fg
= (this.defAttr
>> 9) & 0x1ff;
3117 bg
= this.defAttr
& 0x1ff;
3119 this.error('Unknown SGR attribute: %d.', p
);
3123 this.curAttr
= (flags
<< 18) | (fg
<< 9) | bg
;
3126 // CSI Ps n Device Status Report (DSR).
3127 // Ps = 5 -> Status Report. Result (``OK'') is
3129 // Ps = 6 -> Report Cursor Position (CPR) [row;column].
3133 // Device Status Report (DSR, DEC-specific).
3134 // Ps = 6 -> Report Cursor Position (CPR) [row;column] as CSI
3135 // ? r ; c R (assumes page is zero).
3136 // Ps = 1 5 -> Report Printer status as CSI ? 1 0 n (ready).
3137 // or CSI ? 1 1 n (not ready).
3138 // Ps = 2 5 -> Report UDK status as CSI ? 2 0 n (unlocked)
3139 // or CSI ? 2 1 n (locked).
3140 // Ps = 2 6 -> Report Keyboard status as
3141 // CSI ? 2 7 ; 1 ; 0 ; 0 n (North American).
3142 // The last two parameters apply to VT400 & up, and denote key-
3143 // board ready and LK01 respectively.
3144 // Ps = 5 3 -> Report Locator status as
3145 // CSI ? 5 3 n Locator available, if compiled-in, or
3146 // CSI ? 5 0 n No Locator, if not.
3147 Terminal
.prototype.deviceStatus = function(params
) {
3149 switch (params
[0]) {
3152 this.send('\x1b[0n');
3163 } else if (this.prefix
=== '?') {
3164 // modern xterm doesnt seem to
3165 // respond to any of these except ?6, 6, and 5
3166 switch (params
[0]) {
3177 // this.send('\x1b[?11n');
3180 // dont support user defined keys
3181 // this.send('\x1b[?21n');
3184 // north american keyboard
3185 // this.send('\x1b[?27;1;0;0n');
3188 // no dec locator/mouse
3189 // this.send('\x1b[?50n');
3200 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
3201 Terminal
.prototype.insertChars = function(params
) {
3202 var param
, row
, j
, ch
;
3205 if (param
< 1) param
= 1;
3207 row
= this.y
+ this.ybase
;
3209 ch
= [this.eraseAttr(), ' ']; // xterm
3211 while (param
-- && j
< this.cols
) {
3212 this.lines
[row
].splice(j
++, 0, ch
);
3213 this.lines
[row
].pop();
3218 // Cursor Next Line Ps Times (default = 1) (CNL).
3219 // same as CSI Ps B ?
3220 Terminal
.prototype.cursorNextLine = function(params
) {
3221 var param
= params
[0];
3222 if (param
< 1) param
= 1;
3224 if (this.y
>= this.rows
) {
3225 this.y
= this.rows
- 1;
3231 // Cursor Preceding Line Ps Times (default = 1) (CNL).
3233 Terminal
.prototype.cursorPrecedingLine = function(params
) {
3234 var param
= params
[0];
3235 if (param
< 1) param
= 1;
3237 if (this.y
< 0) this.y
= 0;
3242 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
3243 Terminal
.prototype.cursorCharAbsolute = function(params
) {
3244 var param
= params
[0];
3245 if (param
< 1) param
= 1;
3250 // Insert Ps Line(s) (default = 1) (IL).
3251 Terminal
.prototype.insertLines = function(params
) {
3255 if (param
< 1) param
= 1;
3256 row
= this.y
+ this.ybase
;
3258 j
= this.rows
- 1 - this.scrollBottom
;
3259 j
= this.rows
- 1 + this.ybase
- j
+ 1;
3262 // test: echo -e '\e[44m\e[1L\e[0m'
3263 // blankLine(true) - xterm/linux behavior
3264 this.lines
.splice(row
, 0, this.blankLine(true));
3265 this.lines
.splice(j
, 1);
3269 this.updateRange(this.y
);
3270 this.updateRange(this.scrollBottom
);
3274 // Delete Ps Line(s) (default = 1) (DL).
3275 Terminal
.prototype.deleteLines = function(params
) {
3279 if (param
< 1) param
= 1;
3280 row
= this.y
+ this.ybase
;
3282 j
= this.rows
- 1 - this.scrollBottom
;
3283 j
= this.rows
- 1 + this.ybase
- j
;
3286 // test: echo -e '\e[44m\e[1M\e[0m'
3287 // blankLine(true) - xterm/linux behavior
3288 this.lines
.splice(j
+ 1, 0, this.blankLine(true));
3289 this.lines
.splice(row
, 1);
3293 this.updateRange(this.y
);
3294 this.updateRange(this.scrollBottom
);
3298 // Delete Ps Character(s) (default = 1) (DCH).
3299 Terminal
.prototype.deleteChars = function(params
) {
3303 if (param
< 1) param
= 1;
3305 row
= this.y
+ this.ybase
;
3306 ch
= [this.eraseAttr(), ' ']; // xterm
3309 this.lines
[row
].splice(this.x
, 1);
3310 this.lines
[row
].push(ch
);
3315 // Erase Ps Character(s) (default = 1) (ECH).
3316 Terminal
.prototype.eraseChars = function(params
) {
3317 var param
, row
, j
, ch
;
3320 if (param
< 1) param
= 1;
3322 row
= this.y
+ this.ybase
;
3324 ch
= [this.eraseAttr(), ' ']; // xterm
3326 while (param
-- && j
< this.cols
) {
3327 this.lines
[row
][j
++] = ch
;
3331 // CSI Pm ` Character Position Absolute
3332 // [column] (default = [row,1]) (HPA).
3333 Terminal
.prototype.charPosAbsolute = function(params
) {
3334 var param
= params
[0];
3335 if (param
< 1) param
= 1;
3337 if (this.x
>= this.cols
) {
3338 this.x
= this.cols
- 1;
3343 // Horizontal Position Relative
3345 Terminal
.prototype.HPositionRelative = function(params
) {
3346 var param
= params
[0];
3347 if (param
< 1) param
= 1;
3349 if (this.x
>= this.cols
) {
3350 this.x
= this.cols
- 1;
3354 // CSI Ps c Send Device Attributes (Primary DA).
3355 // Ps = 0 or omitted -> request attributes from terminal. The
3356 // response depends on the decTerminalID resource setting.
3357 // -> CSI ? 1 ; 2 c (``VT100 with Advanced Video Option'')
3358 // -> CSI ? 1 ; 0 c (``VT101 with No Options'')
3359 // -> CSI ? 6 c (``VT102'')
3360 // -> CSI ? 6 0 ; 1 ; 2 ; 6 ; 8 ; 9 ; 1 5 ; c (``VT220'')
3361 // The VT100-style response parameters do not mean anything by
3362 // themselves. VT220 parameters do, telling the host what fea-
3363 // tures the terminal supports:
3364 // Ps = 1 -> 132-columns.
3365 // Ps = 2 -> Printer.
3366 // Ps = 6 -> Selective erase.
3367 // Ps = 8 -> User-defined keys.
3368 // Ps = 9 -> National replacement character sets.
3369 // Ps = 1 5 -> Technical characters.
3370 // Ps = 2 2 -> ANSI color, e.g., VT525.
3371 // Ps = 2 9 -> ANSI text locator (i.e., DEC Locator mode).
3373 // Send Device Attributes (Secondary DA).
3374 // Ps = 0 or omitted -> request the terminal's identification
3375 // code. The response depends on the decTerminalID resource set-
3376 // ting. It should apply only to VT220 and up, but xterm extends
3378 // -> CSI > Pp ; Pv ; Pc c
3379 // where Pp denotes the terminal type
3380 // Pp = 0 -> ``VT100''.
3381 // Pp = 1 -> ``VT220''.
3382 // and Pv is the firmware version (for xterm, this was originally
3383 // the XFree86 patch number, starting with 95). In a DEC termi-
3384 // nal, Pc indicates the ROM cartridge registration number and is
3386 // More information:
3387 // xterm/charproc.c - line 2012, for more information.
3388 // vim responds with ^[[?0c or ^[[?1c after the terminal's response (?)
3389 Terminal
.prototype.sendDeviceAttributes = function(params
) {
3390 if (params
[0] > 0) return;
3393 if (this.is('xterm')
3394 || this.is('rxvt-unicode')
3395 || this.is('screen')) {
3396 this.send('\x1b[?1;2c');
3397 } else if (this.is('linux')) {
3398 this.send('\x1b[?6c');
3400 } else if (this.prefix
=== '>') {
3402 // seem to spit this
3403 // out around ~370 times (?).
3404 if (this.is('xterm')) {
3405 this.send('\x1b[>0;276;0c');
3406 } else if (this.is('rxvt-unicode')) {
3407 this.send('\x1b[>85;95;0c');
3408 } else if (this.is('linux')) {
3409 // not supported by linux console.
3410 // linux console echoes parameters.
3411 this.send(params
[0] + 'c');
3412 } else if (this.is('screen')) {
3413 this.send('\x1b[>83;40003;0c');
3419 // Line Position Absolute [row] (default = [1,column]) (VPA).
3420 Terminal
.prototype.linePosAbsolute = function(params
) {
3421 var param
= params
[0];
3422 if (param
< 1) param
= 1;
3424 if (this.y
>= this.rows
) {
3425 this.y
= this.rows
- 1;
3429 // 145 65 e * VPR - Vertical Position Relative
3431 Terminal
.prototype.VPositionRelative = function(params
) {
3432 var param
= params
[0];
3433 if (param
< 1) param
= 1;
3435 if (this.y
>= this.rows
) {
3436 this.y
= this.rows
- 1;
3441 // Horizontal and Vertical Position [row;column] (default =
3443 Terminal
.prototype.HVPosition = function(params
) {
3444 if (params
[0] < 1) params
[0] = 1;
3445 if (params
[1] < 1) params
[1] = 1;
3447 this.y
= params
[0] - 1;
3448 if (this.y
>= this.rows
) {
3449 this.y
= this.rows
- 1;
3452 this.x
= params
[1] - 1;
3453 if (this.x
>= this.cols
) {
3454 this.x
= this.cols
- 1;
3458 // CSI Pm h Set Mode (SM).
3459 // Ps = 2 -> Keyboard Action Mode (AM).
3460 // Ps = 4 -> Insert Mode (IRM).
3461 // Ps = 1 2 -> Send/receive (SRM).
3462 // Ps = 2 0 -> Automatic Newline (LNM).
3464 // DEC Private Mode Set (DECSET).
3465 // Ps = 1 -> Application Cursor Keys (DECCKM).
3466 // Ps = 2 -> Designate USASCII for character sets G0-G3
3467 // (DECANM), and set VT100 mode.
3468 // Ps = 3 -> 132 Column Mode (DECCOLM).
3469 // Ps = 4 -> Smooth (Slow) Scroll (DECSCLM).
3470 // Ps = 5 -> Reverse Video (DECSCNM).
3471 // Ps = 6 -> Origin Mode (DECOM).
3472 // Ps = 7 -> Wraparound Mode (DECAWM).
3473 // Ps = 8 -> Auto-repeat Keys (DECARM).
3474 // Ps = 9 -> Send Mouse X & Y on button press. See the sec-
3475 // tion Mouse Tracking.
3476 // Ps = 1 0 -> Show toolbar (rxvt).
3477 // Ps = 1 2 -> Start Blinking Cursor (att610).
3478 // Ps = 1 8 -> Print form feed (DECPFF).
3479 // Ps = 1 9 -> Set print extent to full screen (DECPEX).
3480 // Ps = 2 5 -> Show Cursor (DECTCEM).
3481 // Ps = 3 0 -> Show scrollbar (rxvt).
3482 // Ps = 3 5 -> Enable font-shifting functions (rxvt).
3483 // Ps = 3 8 -> Enter Tektronix Mode (DECTEK).
3484 // Ps = 4 0 -> Allow 80 -> 132 Mode.
3485 // Ps = 4 1 -> more(1) fix (see curses resource).
3486 // Ps = 4 2 -> Enable Nation Replacement Character sets (DECN-
3488 // Ps = 4 4 -> Turn On Margin Bell.
3489 // Ps = 4 5 -> Reverse-wraparound Mode.
3490 // Ps = 4 6 -> Start Logging. This is normally disabled by a
3491 // compile-time option.
3492 // Ps = 4 7 -> Use Alternate Screen Buffer. (This may be dis-
3493 // abled by the titeInhibit resource).
3494 // Ps = 6 6 -> Application keypad (DECNKM).
3495 // Ps = 6 7 -> Backarrow key sends backspace (DECBKM).
3496 // Ps = 1 0 0 0 -> Send Mouse X & Y on button press and
3497 // release. See the section Mouse Tracking.
3498 // Ps = 1 0 0 1 -> Use Hilite Mouse Tracking.
3499 // Ps = 1 0 0 2 -> Use Cell Motion Mouse Tracking.
3500 // Ps = 1 0 0 3 -> Use All Motion Mouse Tracking.
3501 // Ps = 1 0 0 4 -> Send FocusIn/FocusOut events.
3502 // Ps = 1 0 0 5 -> Enable Extended Mouse Mode.
3503 // Ps = 1 0 1 0 -> Scroll to bottom on tty output (rxvt).
3504 // Ps = 1 0 1 1 -> Scroll to bottom on key press (rxvt).
3505 // Ps = 1 0 3 4 -> Interpret "meta" key, sets eighth bit.
3506 // (enables the eightBitInput resource).
3507 // Ps = 1 0 3 5 -> Enable special modifiers for Alt and Num-
3508 // Lock keys. (This enables the numLock resource).
3509 // Ps = 1 0 3 6 -> Send ESC when Meta modifies a key. (This
3510 // enables the metaSendsEscape resource).
3511 // Ps = 1 0 3 7 -> Send DEL from the editing-keypad Delete
3513 // Ps = 1 0 3 9 -> Send ESC when Alt modifies a key. (This
3514 // enables the altSendsEscape resource).
3515 // Ps = 1 0 4 0 -> Keep selection even if not highlighted.
3516 // (This enables the keepSelection resource).
3517 // Ps = 1 0 4 1 -> Use the CLIPBOARD selection. (This enables
3518 // the selectToClipboard resource).
3519 // Ps = 1 0 4 2 -> Enable Urgency window manager hint when
3520 // Control-G is received. (This enables the bellIsUrgent
3522 // Ps = 1 0 4 3 -> Enable raising of the window when Control-G
3523 // is received. (enables the popOnBell resource).
3524 // Ps = 1 0 4 7 -> Use Alternate Screen Buffer. (This may be
3525 // disabled by the titeInhibit resource).
3526 // Ps = 1 0 4 8 -> Save cursor as in DECSC. (This may be dis-
3527 // abled by the titeInhibit resource).
3528 // Ps = 1 0 4 9 -> Save cursor as in DECSC and use Alternate
3529 // Screen Buffer, clearing it first. (This may be disabled by
3530 // the titeInhibit resource). This combines the effects of the 1
3531 // 0 4 7 and 1 0 4 8 modes. Use this with terminfo-based
3532 // applications rather than the 4 7 mode.
3533 // Ps = 1 0 5 0 -> Set terminfo/termcap function-key mode.
3534 // Ps = 1 0 5 1 -> Set Sun function-key mode.
3535 // Ps = 1 0 5 2 -> Set HP function-key mode.
3536 // Ps = 1 0 5 3 -> Set SCO function-key mode.
3537 // Ps = 1 0 6 0 -> Set legacy keyboard emulation (X11R6).
3538 // Ps = 1 0 6 1 -> Set VT220 keyboard emulation.
3539 // Ps = 2 0 0 4 -> Set bracketed paste mode.
3541 // http://vt100.net/docs/vt220-rm/chapter4.html
3542 Terminal
.prototype.setMode = function(params
) {
3543 if (typeof params
=== 'object') {
3544 var l
= params
.length
3547 for (; i
< l
; i
++) {
3548 this.setMode(params
[i
]);
3557 this.insertMode
= true;
3560 //this.convertEol = true;
3563 } else if (this.prefix
=== '?') {
3566 this.applicationCursor
= true;
3569 this.setgCharset(0, Terminal
.charsets
.US
);
3570 this.setgCharset(1, Terminal
.charsets
.US
);
3571 this.setgCharset(2, Terminal
.charsets
.US
);
3572 this.setgCharset(3, Terminal
.charsets
.US
);
3573 // set VT100 mode here
3575 case 3: // 132 col mode
3576 this.savedCols
= this.cols
;
3577 this.resize(132, this.rows
);
3580 this.originMode
= true;
3583 this.wraparoundMode
= true;
3586 // this.cursorBlink = true;
3589 this.log('Serial port requested application keypad.');
3590 this.applicationKeypad
= true;
3592 case 9: // X10 Mouse
3593 // no release, no motion, no wheel, no modifiers.
3594 case 1000: // vt200 mouse
3596 // no modifiers, except control on the wheel.
3597 case 1002: // button event mouse
3598 case 1003: // any event mouse
3599 // any event - sends motion events,
3600 // even if there is no button held down.
3601 this.x10Mouse
= params
=== 9;
3602 this.vt200Mouse
= params
=== 1000;
3603 this.normalMouse
= params
> 1000;
3604 this.mouseEvents
= true;
3605 this.element
.style
.cursor
= 'default';
3606 this.log('Binding to mouse events.');
3608 case 1004: // send focusin/focusout events
3611 this.sendFocus
= true;
3613 case 1005: // utf8 ext mode mouse
3614 this.utfMouse
= true;
3615 // for wide terminals
3616 // simply encodes large values as utf8 characters
3618 case 1006: // sgr ext mode mouse
3619 this.sgrMouse
= true;
3620 // for wide terminals
3621 // does not add 32 to fields
3622 // press: ^[[<b;x;yM
3623 // release: ^[[<b;x;ym
3625 case 1015: // urxvt ext mode mouse
3626 this.urxvtMouse
= true;
3627 // for wide terminals
3628 // numbers for fields
3630 // motion: ^[[b;x;yT
3632 case 25: // show cursor
3633 this.cursorHidden
= false;
3635 case 1049: // alt screen buffer cursor
3636 //this.saveCursor();
3638 case 47: // alt screen buffer
3639 case 1047: // alt screen buffer
3647 scrollTop
: this.scrollTop
,
3648 scrollBottom
: this.scrollBottom
,
3650 // XXX save charset(s) here?
3651 // charset: this.charset,
3652 // glevel: this.glevel,
3653 // charsets: this.charsets
3656 this.normal
= normal
;
3664 // CSI Pm l Reset Mode (RM).
3665 // Ps = 2 -> Keyboard Action Mode (AM).
3666 // Ps = 4 -> Replace Mode (IRM).
3667 // Ps = 1 2 -> Send/receive (SRM).
3668 // Ps = 2 0 -> Normal Linefeed (LNM).
3670 // DEC Private Mode Reset (DECRST).
3671 // Ps = 1 -> Normal Cursor Keys (DECCKM).
3672 // Ps = 2 -> Designate VT52 mode (DECANM).
3673 // Ps = 3 -> 80 Column Mode (DECCOLM).
3674 // Ps = 4 -> Jump (Fast) Scroll (DECSCLM).
3675 // Ps = 5 -> Normal Video (DECSCNM).
3676 // Ps = 6 -> Normal Cursor Mode (DECOM).
3677 // Ps = 7 -> No Wraparound Mode (DECAWM).
3678 // Ps = 8 -> No Auto-repeat Keys (DECARM).
3679 // Ps = 9 -> Don't send Mouse X & Y on button press.
3680 // Ps = 1 0 -> Hide toolbar (rxvt).
3681 // Ps = 1 2 -> Stop Blinking Cursor (att610).
3682 // Ps = 1 8 -> Don't print form feed (DECPFF).
3683 // Ps = 1 9 -> Limit print to scrolling region (DECPEX).
3684 // Ps = 2 5 -> Hide Cursor (DECTCEM).
3685 // Ps = 3 0 -> Don't show scrollbar (rxvt).
3686 // Ps = 3 5 -> Disable font-shifting functions (rxvt).
3687 // Ps = 4 0 -> Disallow 80 -> 132 Mode.
3688 // Ps = 4 1 -> No more(1) fix (see curses resource).
3689 // Ps = 4 2 -> Disable Nation Replacement Character sets (DEC-
3691 // Ps = 4 4 -> Turn Off Margin Bell.
3692 // Ps = 4 5 -> No Reverse-wraparound Mode.
3693 // Ps = 4 6 -> Stop Logging. (This is normally disabled by a
3694 // compile-time option).
3695 // Ps = 4 7 -> Use Normal Screen Buffer.
3696 // Ps = 6 6 -> Numeric keypad (DECNKM).
3697 // Ps = 6 7 -> Backarrow key sends delete (DECBKM).
3698 // Ps = 1 0 0 0 -> Don't send Mouse X & Y on button press and
3699 // release. See the section Mouse Tracking.
3700 // Ps = 1 0 0 1 -> Don't use Hilite Mouse Tracking.
3701 // Ps = 1 0 0 2 -> Don't use Cell Motion Mouse Tracking.
3702 // Ps = 1 0 0 3 -> Don't use All Motion Mouse Tracking.
3703 // Ps = 1 0 0 4 -> Don't send FocusIn/FocusOut events.
3704 // Ps = 1 0 0 5 -> Disable Extended Mouse Mode.
3705 // Ps = 1 0 1 0 -> Don't scroll to bottom on tty output
3707 // Ps = 1 0 1 1 -> Don't scroll to bottom on key press (rxvt).
3708 // Ps = 1 0 3 4 -> Don't interpret "meta" key. (This disables
3709 // the eightBitInput resource).
3710 // Ps = 1 0 3 5 -> Disable special modifiers for Alt and Num-
3711 // Lock keys. (This disables the numLock resource).
3712 // Ps = 1 0 3 6 -> Don't send ESC when Meta modifies a key.
3713 // (This disables the metaSendsEscape resource).
3714 // Ps = 1 0 3 7 -> Send VT220 Remove from the editing-keypad
3716 // Ps = 1 0 3 9 -> Don't send ESC when Alt modifies a key.
3717 // (This disables the altSendsEscape resource).
3718 // Ps = 1 0 4 0 -> Do not keep selection when not highlighted.
3719 // (This disables the keepSelection resource).
3720 // Ps = 1 0 4 1 -> Use the PRIMARY selection. (This disables
3721 // the selectToClipboard resource).
3722 // Ps = 1 0 4 2 -> Disable Urgency window manager hint when
3723 // Control-G is received. (This disables the bellIsUrgent
3725 // Ps = 1 0 4 3 -> Disable raising of the window when Control-
3726 // G is received. (This disables the popOnBell resource).
3727 // Ps = 1 0 4 7 -> Use Normal Screen Buffer, clearing screen
3728 // first if in the Alternate Screen. (This may be disabled by
3729 // the titeInhibit resource).
3730 // Ps = 1 0 4 8 -> Restore cursor as in DECRC. (This may be
3731 // disabled by the titeInhibit resource).
3732 // Ps = 1 0 4 9 -> Use Normal Screen Buffer and restore cursor
3733 // as in DECRC. (This may be disabled by the titeInhibit
3734 // resource). This combines the effects of the 1 0 4 7 and 1 0
3735 // 4 8 modes. Use this with terminfo-based applications rather
3736 // than the 4 7 mode.
3737 // Ps = 1 0 5 0 -> Reset terminfo/termcap function-key mode.
3738 // Ps = 1 0 5 1 -> Reset Sun function-key mode.
3739 // Ps = 1 0 5 2 -> Reset HP function-key mode.
3740 // Ps = 1 0 5 3 -> Reset SCO function-key mode.
3741 // Ps = 1 0 6 0 -> Reset legacy keyboard emulation (X11R6).
3742 // Ps = 1 0 6 1 -> Reset keyboard emulation to Sun/PC style.
3743 // Ps = 2 0 0 4 -> Reset bracketed paste mode.
3744 Terminal
.prototype.resetMode = function(params
) {
3745 if (typeof params
=== 'object') {
3746 var l
= params
.length
3749 for (; i
< l
; i
++) {
3750 this.resetMode(params
[i
]);
3759 this.insertMode
= false;
3762 //this.convertEol = false;
3765 } else if (this.prefix
=== '?') {
3768 this.applicationCursor
= false;
3771 if (this.cols
=== 132 && this.savedCols
) {
3772 this.resize(this.savedCols
, this.rows
);
3774 delete this.savedCols
;
3777 this.originMode
= false;
3780 this.wraparoundMode
= false;
3783 // this.cursorBlink = false;
3786 this.log('Switching back to normal keypad.');
3787 this.applicationKeypad
= false;
3789 case 9: // X10 Mouse
3790 case 1000: // vt200 mouse
3791 case 1002: // button event mouse
3792 case 1003: // any event mouse
3793 this.x10Mouse
= false;
3794 this.vt200Mouse
= false;
3795 this.normalMouse
= false;
3796 this.mouseEvents
= false;
3797 this.element
.style
.cursor
= '';
3799 case 1004: // send focusin/focusout events
3800 this.sendFocus
= false;
3802 case 1005: // utf8 ext mode mouse
3803 this.utfMouse
= false;
3805 case 1006: // sgr ext mode mouse
3806 this.sgrMouse
= false;
3808 case 1015: // urxvt ext mode mouse
3809 this.urxvtMouse
= false;
3811 case 25: // hide cursor
3812 this.cursorHidden
= true;
3814 case 1049: // alt screen buffer cursor
3816 case 47: // normal screen buffer
3817 case 1047: // normal screen buffer - clearing it first
3819 this.lines
= this.normal
.lines
;
3820 this.ybase
= this.normal
.ybase
;
3821 this.ydisp
= this.normal
.ydisp
;
3822 this.x
= this.normal
.x
;
3823 this.y
= this.normal
.y
;
3824 this.scrollTop
= this.normal
.scrollTop
;
3825 this.scrollBottom
= this.normal
.scrollBottom
;
3826 this.tabs
= this.normal
.tabs
;
3828 // if (params === 1049) {
3829 // this.x = this.savedX;
3830 // this.y = this.savedY;
3832 this.refresh(0, this.rows
- 1);
3841 // Set Scrolling Region [top;bottom] (default = full size of win-
3844 Terminal
.prototype.setScrollRegion = function(params
) {
3845 if (this.prefix
) return;
3846 this.scrollTop
= (params
[0] || 1) - 1;
3847 this.scrollBottom
= (params
[1] || this.rows
) - 1;
3853 // Save cursor (ANSI.SYS).
3854 Terminal
.prototype.saveCursor = function(params
) {
3855 this.savedX
= this.x
;
3856 this.savedY
= this.y
;
3860 // Restore cursor (ANSI.SYS).
3861 Terminal
.prototype.restoreCursor = function(params
) {
3862 this.x
= this.savedX
|| 0;
3863 this.y
= this.savedY
|| 0;
3871 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
3872 Terminal
.prototype.cursorForwardTab = function(params
) {
3873 var param
= params
[0] || 1;
3875 this.x
= this.nextStop();
3879 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
3880 Terminal
.prototype.scrollUp = function(params
) {
3881 var param
= params
[0] || 1;
3883 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
3884 this.lines
.splice(this.ybase
+ this.scrollBottom
, 0, this.blankLine());
3887 this.updateRange(this.scrollTop
);
3888 this.updateRange(this.scrollBottom
);
3891 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
3892 Terminal
.prototype.scrollDown = function(params
) {
3893 var param
= params
[0] || 1;
3895 this.lines
.splice(this.ybase
+ this.scrollBottom
, 1);
3896 this.lines
.splice(this.ybase
+ this.scrollTop
, 0, this.blankLine());
3899 this.updateRange(this.scrollTop
);
3900 this.updateRange(this.scrollBottom
);
3903 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
3904 // Initiate highlight mouse tracking. Parameters are
3905 // [func;startx;starty;firstrow;lastrow]. See the section Mouse
3907 Terminal
.prototype.initMouseTracking = function(params
) {
3908 // Relevant: DECSET 1001
3912 // Reset one or more features of the title modes to the default
3913 // value. Normally, "reset" disables the feature. It is possi-
3914 // ble to disable the ability to reset features by compiling a
3915 // different default for the title modes into xterm.
3916 // Ps = 0 -> Do not set window/icon labels using hexadecimal.
3917 // Ps = 1 -> Do not query window/icon labels using hexadeci-
3919 // Ps = 2 -> Do not set window/icon labels using UTF-8.
3920 // Ps = 3 -> Do not query window/icon labels using UTF-8.
3921 // (See discussion of "Title Modes").
3922 Terminal
.prototype.resetTitleModes = function(params
) {
3926 // CSI Ps Z Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
3927 Terminal
.prototype.cursorBackwardTab = function(params
) {
3928 var param
= params
[0] || 1;
3930 this.x
= this.prevStop();
3934 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
3935 Terminal
.prototype.repeatPrecedingCharacter = function(params
) {
3936 var param
= params
[0] || 1
3937 , line
= this.lines
[this.ybase
+ this.y
]
3938 , ch
= line
[this.x
- 1] || [this.defAttr
, ' '];
3940 while (param
--) line
[this.x
++] = ch
;
3943 // CSI Ps g Tab Clear (TBC).
3944 // Ps = 0 -> Clear Current Column (default).
3945 // Ps = 3 -> Clear All.
3947 // Ps = 2 -> Clear Stops on Line.
3948 // http://vt100.net/annarbor/aaa-ug/section6.html
3949 Terminal
.prototype.tabClear = function(params
) {
3950 var param
= params
[0];
3952 delete this.tabs
[this.x
];
3953 } else if (param
=== 3) {
3958 // CSI Pm i Media Copy (MC).
3959 // Ps = 0 -> Print screen (default).
3960 // Ps = 4 -> Turn off printer controller mode.
3961 // Ps = 5 -> Turn on printer controller mode.
3963 // Media Copy (MC, DEC-specific).
3964 // Ps = 1 -> Print line containing cursor.
3965 // Ps = 4 -> Turn off autoprint mode.
3966 // Ps = 5 -> Turn on autoprint mode.
3967 // Ps = 1 0 -> Print composed display, ignores DECPEX.
3968 // Ps = 1 1 -> Print all pages.
3969 Terminal
.prototype.mediaCopy = function(params
) {
3974 // Set or reset resource-values used by xterm to decide whether
3975 // to construct escape sequences holding information about the
3976 // modifiers pressed with a given key. The first parameter iden-
3977 // tifies the resource to set/reset. The second parameter is the
3978 // value to assign to the resource. If the second parameter is
3979 // omitted, the resource is reset to its initial value.
3980 // Ps = 1 -> modifyCursorKeys.
3981 // Ps = 2 -> modifyFunctionKeys.
3982 // Ps = 4 -> modifyOtherKeys.
3983 // If no parameters are given, all resources are reset to their
3985 Terminal
.prototype.setResources = function(params
) {
3990 // Disable modifiers which may be enabled via the CSI > Ps; Ps m
3991 // sequence. This corresponds to a resource value of "-1", which
3992 // cannot be set with the other sequence. The parameter identi-
3993 // fies the resource to be disabled:
3994 // Ps = 1 -> modifyCursorKeys.
3995 // Ps = 2 -> modifyFunctionKeys.
3996 // Ps = 4 -> modifyOtherKeys.
3997 // If the parameter is omitted, modifyFunctionKeys is disabled.
3998 // When modifyFunctionKeys is disabled, xterm uses the modifier
3999 // keys to make an extended sequence of functions rather than
4000 // adding a parameter to each function key to denote the modi-
4002 Terminal
.prototype.disableModifiers = function(params
) {
4007 // Set resource value pointerMode. This is used by xterm to
4008 // decide whether to hide the pointer cursor as the user types.
4009 // Valid values for the parameter:
4010 // Ps = 0 -> never hide the pointer.
4011 // Ps = 1 -> hide if the mouse tracking mode is not enabled.
4012 // Ps = 2 -> always hide the pointer. If no parameter is
4013 // given, xterm uses the default, which is 1 .
4014 Terminal
.prototype.setPointerMode = function(params
) {
4018 // CSI ! p Soft terminal reset (DECSTR).
4019 // http://vt100.net/docs/vt220-rm/table4-10.html
4020 Terminal
.prototype.softReset = function(params
) {
4021 this.cursorHidden
= false;
4022 this.insertMode
= false;
4023 this.originMode
= false;
4024 this.wraparoundMode
= false; // autowrap
4025 this.applicationKeypad
= false; // ?
4026 this.applicationCursor
= false;
4028 this.scrollBottom
= this.rows
- 1;
4029 this.curAttr
= this.defAttr
;
4030 this.x
= this.y
= 0; // ?
4031 this.charset
= null;
4032 this.glevel
= 0; // ??
4033 this.charsets
= [null]; // ??
4037 // Request ANSI mode (DECRQM). For VT300 and up, reply is
4039 // where Ps is the mode number as in RM, and Pm is the mode
4041 // 0 - not recognized
4044 // 3 - permanently set
4045 // 4 - permanently reset
4046 Terminal
.prototype.requestAnsiMode = function(params
) {
4051 // Request DEC private mode (DECRQM). For VT300 and up, reply is
4053 // where Ps is the mode number as in DECSET, Pm is the mode value
4054 // as in the ANSI DECRQM.
4055 Terminal
.prototype.requestPrivateMode = function(params
) {
4060 // Set conformance level (DECSCL). Valid values for the first
4062 // Ps = 6 1 -> VT100.
4063 // Ps = 6 2 -> VT200.
4064 // Ps = 6 3 -> VT300.
4065 // Valid values for the second parameter:
4066 // Ps = 0 -> 8-bit controls.
4067 // Ps = 1 -> 7-bit controls (always set for VT100).
4068 // Ps = 2 -> 8-bit controls.
4069 Terminal
.prototype.setConformanceLevel = function(params
) {
4073 // CSI Ps q Load LEDs (DECLL).
4074 // Ps = 0 -> Clear all LEDS (default).
4075 // Ps = 1 -> Light Num Lock.
4076 // Ps = 2 -> Light Caps Lock.
4077 // Ps = 3 -> Light Scroll Lock.
4078 // Ps = 2 1 -> Extinguish Num Lock.
4079 // Ps = 2 2 -> Extinguish Caps Lock.
4080 // Ps = 2 3 -> Extinguish Scroll Lock.
4081 Terminal
.prototype.loadLEDs = function(params
) {
4086 // Set cursor style (DECSCUSR, VT520).
4087 // Ps = 0 -> blinking block.
4088 // Ps = 1 -> blinking block (default).
4089 // Ps = 2 -> steady block.
4090 // Ps = 3 -> blinking underline.
4091 // Ps = 4 -> steady underline.
4092 Terminal
.prototype.setCursorStyle = function(params
) {
4097 // Select character protection attribute (DECSCA). Valid values
4098 // for the parameter:
4099 // Ps = 0 -> DECSED and DECSEL can erase (default).
4100 // Ps = 1 -> DECSED and DECSEL cannot erase.
4101 // Ps = 2 -> DECSED and DECSEL can erase.
4102 Terminal
.prototype.setCharProtectionAttr = function(params
) {
4107 // Restore DEC Private Mode Values. The value of Ps previously
4108 // saved is restored. Ps values are the same as for DECSET.
4109 Terminal
.prototype.restorePrivateValues = function(params
) {
4113 // CSI Pt; Pl; Pb; Pr; Ps$ r
4114 // Change Attributes in Rectangular Area (DECCARA), VT400 and up.
4115 // Pt; Pl; Pb; Pr denotes the rectangle.
4116 // Ps denotes the SGR attributes to change: 0, 1, 4, 5, 7.
4117 // NOTE: xterm doesn't enable this code by default.
4118 Terminal
.prototype.setAttrInRectangle = function(params
) {
4128 for (; t
< b
+ 1; t
++) {
4129 line
= this.lines
[this.ybase
+ t
];
4130 for (i
= l
; i
< r
; i
++) {
4131 line
[i
] = [attr
, line
[i
][1]];
4136 this.updateRange(params
[0]);
4137 this.updateRange(params
[2]);
4141 // CSI Pc; Pt; Pl; Pb; Pr$ x
4142 // Fill Rectangular Area (DECFRA), VT420 and up.
4143 // Pc is the character to use.
4144 // Pt; Pl; Pb; Pr denotes the rectangle.
4145 // NOTE: xterm doesn't enable this code by default.
4146 Terminal
.prototype.fillRectangle = function(params
) {
4156 for (; t
< b
+ 1; t
++) {
4157 line
= this.lines
[this.ybase
+ t
];
4158 for (i
= l
; i
< r
; i
++) {
4159 line
[i
] = [line
[i
][0], String
.fromCharCode(ch
)];
4164 this.updateRange(params
[1]);
4165 this.updateRange(params
[3]);
4169 // Enable Locator Reporting (DECELR).
4170 // Valid values for the first parameter:
4171 // Ps = 0 -> Locator disabled (default).
4172 // Ps = 1 -> Locator enabled.
4173 // Ps = 2 -> Locator enabled for one report, then disabled.
4174 // The second parameter specifies the coordinate unit for locator
4176 // Valid values for the second parameter:
4177 // Pu = 0 <- or omitted -> default to character cells.
4178 // Pu = 1 <- device physical pixels.
4179 // Pu = 2 <- character cells.
4180 Terminal
.prototype.enableLocatorReporting = function(params
) {
4181 var val
= params
[0] > 0;
4182 //this.mouseEvents = val;
4183 //this.decLocator = val;
4186 // CSI Pt; Pl; Pb; Pr$ z
4187 // Erase Rectangular Area (DECERA), VT400 and up.
4188 // Pt; Pl; Pb; Pr denotes the rectangle.
4189 // NOTE: xterm doesn't enable this code by default.
4190 Terminal
.prototype.eraseRectangle = function(params
) {
4200 ch
= [this.eraseAttr(), ' ']; // xterm?
4202 for (; t
< b
+ 1; t
++) {
4203 line
= this.lines
[this.ybase
+ t
];
4204 for (i
= l
; i
< r
; i
++) {
4210 this.updateRange(params
[0]);
4211 this.updateRange(params
[2]);
4216 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
4217 // NOTE: xterm doesn't enable this code by default.
4218 Terminal
.prototype.insertColumns = function() {
4219 var param
= params
[0]
4220 , l
= this.ybase
+ this.rows
4221 , ch
= [this.eraseAttr(), ' '] // xterm?
4225 for (i
= this.ybase
; i
< l
; i
++) {
4226 this.lines
[i
].splice(this.x
+ 1, 0, ch
);
4227 this.lines
[i
].pop();
4236 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
4237 // NOTE: xterm doesn't enable this code by default.
4238 Terminal
.prototype.deleteColumns = function() {
4239 var param
= params
[0]
4240 , l
= this.ybase
+ this.rows
4241 , ch
= [this.eraseAttr(), ' '] // xterm?
4245 for (i
= this.ybase
; i
< l
; i
++) {
4246 this.lines
[i
].splice(this.x
, 1);
4247 this.lines
[i
].push(ch
);
4258 Terminal
.charsets
= {};
4260 // DEC Special Character and Line Drawing Set.
4261 // http://vt100.net/docs/vt102-ug/table5-13.html
4262 // A lot of curses apps use this if they see TERM=xterm.
4263 // testing: echo -e '\e(0a\e(B'
4264 // The xterm output sometimes seems to conflict with the
4265 // reference above. xterm seems in line with the reference
4266 // when running vttest however.
4267 // The table below now uses xterm's output from vttest.
4268 Terminal
.charsets
.SCLD
= { // (0
4269 '`': '\u25c6', // '◆'
4270 'a': '\u2592', // '▒'
4271 'b': '\u0009', // '\t'
4272 'c': '\u000c', // '\f'
4273 'd': '\u000d', // '\r'
4274 'e': '\u000a', // '\n'
4275 'f': '\u00b0', // '°'
4276 'g': '\u00b1', // '±'
4277 'h': '\u2424', // '\u2424' (NL)
4278 'i': '\u000b', // '\v'
4279 'j': '\u2518', // '┘'
4280 'k': '\u2510', // '┐'
4281 'l': '\u250c', // '┌'
4282 'm': '\u2514', // '└'
4283 'n': '\u253c', // '┼'
4284 'o': '\u23ba', // '⎺'
4285 'p': '\u23bb', // '⎻'
4286 'q': '\u2500', // '─'
4287 'r': '\u23bc', // '⎼'
4288 's': '\u23bd', // '⎽'
4289 't': '\u251c', // '├'
4290 'u': '\u2524', // '┤'
4291 'v': '\u2534', // '┴'
4292 'w': '\u252c', // '┬'
4293 'x': '\u2502', // '│'
4294 'y': '\u2264', // '≤'
4295 'z': '\u2265', // '≥'
4296 '{': '\u03c0', // 'π'
4297 '|': '\u2260', // '≠'
4298 '}': '\u00a3', // '£'
4299 '~': '\u00b7' // '·'
4302 Terminal
.charsets
.UK
= null; // (A
4303 Terminal
.charsets
.US
= null; // (B (USASCII)
4304 Terminal
.charsets
.Dutch
= null; // (4
4305 Terminal
.charsets
.Finnish
= null; // (C or (5
4306 Terminal
.charsets
.French
= null; // (R
4307 Terminal
.charsets
.FrenchCanadian
= null; // (Q
4308 Terminal
.charsets
.German
= null; // (K
4309 Terminal
.charsets
.Italian
= null; // (Y
4310 Terminal
.charsets
.NorwegianDanish
= null; // (E or (6
4311 Terminal
.charsets
.Spanish
= null; // (Z
4312 Terminal
.charsets
.Swedish
= null; // (H or (7
4313 Terminal
.charsets
.Swiss
= null; // (=
4314 Terminal
.charsets
.ISOLatin
= null; // /A
4320 function on(el
, type
, handler
, capture
) {
4321 if (!Array
.isArray(el
)) {
4324 el
.forEach(function (element
) {
4325 element
.addEventListener(type
, handler
, capture
|| false);
4329 function off(el
, type
, handler
, capture
) {
4330 el
.removeEventListener(type
, handler
, capture
|| false);
4333 function cancel(ev
, force
) {
4334 if (!this.cancelEvents
&& !force
) {
4337 ev
.preventDefault();
4338 ev
.stopPropagation();
4342 function inherits(child
, parent
) {
4344 this.constructor = child
;
4346 f
.prototype = parent
.prototype;
4347 child
.prototype = new f
;
4350 // if bold is broken, we can't
4351 // use it in the terminal.
4352 function isBoldBroken(document
) {
4353 var body
= document
.getElementsByTagName('body')[0];
4354 var el
= document
.createElement('span');
4355 el
.innerHTML
= 'hello world';
4356 body
.appendChild(el
);
4357 var w1
= el
.scrollWidth
;
4358 el
.style
.fontWeight
= 'bold';
4359 var w2
= el
.scrollWidth
;
4360 body
.removeChild(el
);
4364 var String
= this.String
;
4365 var setTimeout
= this.setTimeout
;
4366 var setInterval
= this.setInterval
;
4368 function indexOf(obj
, el
) {
4371 if (obj
[i
] === el
) return i
;
4376 function isWide(ch
) {
4377 if (ch
<= '\uff00') return false;
4378 return (ch
>= '\uff01' && ch
<= '\uffbe')
4379 || (ch
>= '\uffc2' && ch
<= '\uffc7')
4380 || (ch
>= '\uffca' && ch
<= '\uffcf')
4381 || (ch
>= '\uffd2' && ch
<= '\uffd7')
4382 || (ch
>= '\uffda' && ch
<= '\uffdc')
4383 || (ch
>= '\uffe0' && ch
<= '\uffe6')
4384 || (ch
>= '\uffe8' && ch
<= '\uffee');
4387 function matchColor(r1
, g1
, b1
) {
4388 var hash
= (r1
<< 16) | (g1
<< 8) | b1
;
4390 if (matchColor
._cache
[hash
] != null) {
4391 return matchColor
._cache
[hash
];
4394 var ldiff
= Infinity
4403 for (; i
< Terminal
.vcolors
.length
; i
++) {
4404 c
= Terminal
.vcolors
[i
];
4409 diff
= matchColor
.distance(r1
, g1
, b1
, r2
, g2
, b2
);
4422 return matchColor
._cache
[hash
] = li
;
4425 matchColor
._cache
= {};
4427 // http://stackoverflow.com/questions/1633828
4428 matchColor
.distance = function(r1
, g1
, b1
, r2
, g2
, b2
) {
4429 return Math
.pow(30 * (r1
- r2
), 2)
4430 + Math
.pow(59 * (g1
- g2
), 2)
4431 + Math
.pow(11 * (b1
- b2
), 2);
4434 function each(obj
, iter
, con
) {
4435 if (obj
.forEach
) return obj
.forEach(iter
, con
);
4436 for (var i
= 0; i
< obj
.length
; i
++) {
4437 iter
.call(con
, obj
[i
], i
, obj
);
4441 function keys(obj
) {
4442 if (Object
.keys
) return Object
.keys(obj
);
4445 if (Object
.prototype.hasOwnProperty
.call(obj
, key
)) {
4456 Terminal
.EventEmitter
= EventEmitter
;
4457 Terminal
.inherits
= inherits
;
4460 Terminal
.cancel
= cancel
;