]>
git.proxmox.com Git - mirror_xterm.js.git/blob - src/xterm.js
2 * xterm.js: xterm, in the browser
3 * Copyright (c) 2014, sourceLair Limited (www.sourcelair.com (MIT License)
4 * Copyright (c) 2012-2013, Christopher Jeffrey (MIT License)
5 * https://github.com/chjj/term.js
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * Originally forked from (with the author's permission):
26 * Fabrice Bellard's javascript vt100 for jslinux:
27 * http://bellard.org/jslinux/
28 * Copyright (c) 2011 Fabrice Bellard
29 * The original design remains. The terminal itself
30 * has been extended to include xterm CSI codes, among
35 if (typeof exports
=== 'object' && typeof module
=== 'object') {
37 * CommonJS environment
39 module
.exports
= xterm
.call(this);
40 } else if (typeof define
== 'function') {
42 * Require.js is available
44 define([], xterm
.bind(window
));
47 * Plain browser environment
49 this.Xterm
= xterm
.call(this);
50 this.Terminal
= this.Xterm
; /* Backwards compatibility with term.js */
54 * Terminal Emulation References:
56 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.txt
57 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
58 * http://invisible-island.net/vttest/
59 * http://www.inwap.com/pdp10/ansicode.txt
60 * http://linux.die.net/man/4/console_codes
61 * http://linux.die.net/man/7/urxvt
70 var window
= this, document
= this.document
;
76 function EventEmitter() {
77 this._events
= this._events
|| {};
80 EventEmitter
.prototype.addListener = function(type
, listener
) {
81 this._events
[type
] = this._events
[type
] || [];
82 this._events
[type
].push(listener
);
85 EventEmitter
.prototype.on
= EventEmitter
.prototype.addListener
;
87 EventEmitter
.prototype.removeListener = function(type
, listener
) {
88 if (!this._events
[type
]) return;
90 var obj
= this._events
[type
]
94 if (obj
[i
] === listener
|| obj
[i
].listener
=== listener
) {
101 EventEmitter
.prototype.off
= EventEmitter
.prototype.removeListener
;
103 EventEmitter
.prototype.removeAllListeners = function(type
) {
104 if (this._events
[type
]) delete this._events
[type
];
107 EventEmitter
.prototype.once = function(type
, listener
) {
110 var args
= Array
.prototype.slice
.call(arguments
);
111 self
.removeListener(type
, on
);
112 return listener
.apply(self
, args
);
114 on
.listener
= listener
;
115 return this.on(type
, on
);
118 EventEmitter
.prototype.emit = function(type
) {
119 if (!this._events
[type
]) return;
121 var args
= Array
.prototype.slice
.call(arguments
, 1)
122 , obj
= this._events
[type
]
127 obj
[i
].apply(this, args
);
131 EventEmitter
.prototype.listeners = function(type
) {
132 return this._events
[type
] = this._events
[type
] || [];
139 var normal
= 0, escaped
= 1, csi
= 2, osc
= 3, charset
= 4, dcs
= 5, ignore
= 6;
145 function Terminal(options
) {
148 if (!(this instanceof Terminal
)) {
149 return new Terminal(arguments
[0], arguments
[1], arguments
[2]);
152 self
.cancel
= Terminal
.cancel
;
154 EventEmitter
.call(this);
156 if (typeof options
=== 'number') {
160 handler
: arguments
[2]
164 options
= options
|| {};
167 Object
.keys(Terminal
.defaults
).forEach(function(key
) {
168 if (options
[key
] == null) {
169 options
[key
] = Terminal
.options
[key
];
171 if (Terminal
[key
] !== Terminal
.defaults
[key
]) {
172 options
[key
] = Terminal
[key
];
175 self
[key
] = options
[key
];
178 if (options
.colors
.length
=== 8) {
179 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(8));
180 } else if (options
.colors
.length
=== 16) {
181 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(16));
182 } else if (options
.colors
.length
=== 10) {
183 options
.colors
= options
.colors
.slice(0, -2).concat(
184 Terminal
._colors
.slice(8, -2), options
.colors
.slice(-2));
185 } else if (options
.colors
.length
=== 18) {
186 options
.colors
= options
.colors
.concat(
187 Terminal
._colors
.slice(16, -2), options
.colors
.slice(-2));
189 this.colors
= options
.colors
;
191 this.options
= options
;
193 // this.context = options.context || window;
194 // this.document = options.document || document;
195 this.parent
= options
.body
|| options
.parent
196 || (document
? document
.getElementsByTagName('body')[0] : null);
198 this.cols
= options
.cols
|| options
.geometry
[0];
199 this.rows
= options
.rows
|| options
.geometry
[1];
201 if (options
.handler
) {
202 this.on('data', options
.handler
);
209 this.cursorState
= 0;
210 this.cursorHidden
= false;
215 this.scrollBottom
= this.rows
- 1;
218 this.applicationKeypad
= false;
219 this.applicationCursor
= false;
220 this.originMode
= false;
221 this.insertMode
= false;
222 this.wraparoundMode
= false;
227 this.gcharset
= null;
229 this.charsets
= [null];
253 this.readable
= true;
254 this.writable
= true;
256 this.defAttr
= (0 << 18) | (257 << 9) | (256 << 0);
257 this.curAttr
= this.defAttr
;
260 this.currentParam
= 0;
267 this.lines
.push(this.blankLine());
274 inherits(Terminal
, EventEmitter
);
276 // back_color_erase feature for xterm.
277 Terminal
.prototype.eraseAttr = function() {
278 // if (this.is('screen')) return this.defAttr;
279 return (this.defAttr
& ~0x1ff) | (this.curAttr
& 0x1ff);
287 Terminal
.tangoColors
= [
308 // Colors 0-15 + 16-255
309 // Much thanks to TooTallNate for writing this.
310 Terminal
.colors
= (function() {
311 var colors
= Terminal
.tangoColors
.slice()
312 , r
= [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff]
317 for (; i
< 216; i
++) {
318 out(r
[(i
/ 36) % 6 | 0], r
[(i
/ 6) % 6 | 0], r
[i
% 6]);
323 for (; i
< 24; i
++) {
328 function out(r
, g
, b
) {
329 colors
.push('#' + hex(r
) + hex(g
) + hex(b
));
334 return c
.length
< 2 ? '0' + c
: c
;
340 Terminal
._colors
= Terminal
.colors
.slice();
342 Terminal
.vcolors
= (function() {
344 , colors
= Terminal
.colors
348 for (; i
< 256; i
++) {
349 color
= parseInt(colors
[i
].substring(1), 16);
351 (color
>> 16) & 0xff,
364 Terminal
.defaults
= {
365 colors
: Terminal
.colors
,
377 // programFeatures: false,
381 Terminal
.options
= {};
383 Terminal
.focus
= null;
385 each(keys(Terminal
.defaults
), function(key
) {
386 Terminal
[key
] = Terminal
.defaults
[key
];
387 Terminal
.options
[key
] = Terminal
.defaults
[key
];
393 Terminal
.prototype.focus = function() {
394 if (document
.activeElement
=== this.element
) {
398 if (this.sendFocus
) {
403 this.element
.focus();
406 Terminal
.prototype.blur = function() {
407 if (Terminal
.focus
!== this) {
411 this.cursorState
= 0;
412 this.refresh(this.y
, this.y
);
415 if (this.sendFocus
) {
418 Terminal
.focus
= null;
422 * Initialize default behavior
424 Terminal
.prototype.initGlobal = function() {
425 Terminal
.bindKeys(this);
426 Terminal
.bindPaste(this);
427 Terminal
.bindCopy(this);
428 Terminal
.bindCut(this);
429 Terminal
.bindDrop(this);
433 * Clears all selected text, inside the terminal.
435 Terminal
.prototype.clearSelection = function() {
436 var selectionBaseNode
= window
.getSelection().baseNode
;
438 if (selectionBaseNode
&& (this.element
.contains(selectionBaseNode
.parentElement
))) {
439 window
.getSelection().removeAllRanges();
444 * This function temporarily enables (leases) the contentEditable value of the terminal, which
445 * should be set back to false within 5 seconds at most.
447 Terminal
.prototype.leaseContentEditable = function (ms
, callback
) {
450 term
.element
.contentEditable
= true;
451 setTimeout(function () {
452 term
.element
.contentEditable
= false;
453 if (typeof callback
== 'function') {
460 * Bind to paste event and allow both keyboard and right-click pasting, without having the
461 * contentEditable value set to true.
463 Terminal
.bindPaste = function(term
) {
464 on(term
.element
, 'paste', function(ev
) {
465 if (ev
.clipboardData
) {
466 var text
= ev
.clipboardData
.getData('text/plain');
467 term
.emit('paste', text
, ev
);
470 * Cancel the paste event, or else things will be pasted twice:
471 * 1. by the terminal handler
472 * 2. by the browser, because of the contentEditable value being true
474 term
.cancel(ev
, true);
477 * After the paste event is completed, always set the contentEditable value to false.
479 term
.element
.contentEditable
= false;
484 * Hack pasting with keyboard, in order to make it work without contentEditable.
485 * When a user types Ctrl + Shift + V or Cmd + V on a Mac, lease the contentEditable value
488 on(term
.element
, 'keydown', function (ev
) {
490 * If on a Mac, lease the contentEditable value temporarily, when the user presses
491 * the Cmd button, in order to cope with some sync issues on Safari.
493 if (term
.isMac
&& ev
.keyCode
== 91) {
494 term
.leaseContentEditable(1000);
497 if (ev
.keyCode
== 86) { // keyCode 96 corresponds to "v"
498 if (term
.isMac
&& ev
.metaKey
) {
499 term
.leaseContentEditable();
505 * Hack pasting with right-click in order to allow right-click paste, by leasing the
506 * contentEditable value as true.
508 on(term
.element
, 'contextmenu', function (ev
) {
509 term
.leaseContentEditable();
515 * Apply key handling to the terminal
517 Terminal
.bindKeys = function(term
) {
518 on(term
.element
, 'keydown', function(ev
) {
522 on(term
.element
, 'keypress', function(ev
) {
529 * Bind copy event. Stript trailing whitespaces from selection.
531 Terminal
.bindCopy = function(term
) {
532 on(term
.element
, 'copy', function(ev
) {
533 var selectedText
= window
.getSelection().toString(),
534 copiedText
= selectedText
.split('\n').map(function (element
) {
535 return element
.replace(/\s+$/g, '');
537 ev
.clipboardData
.setData('text/plain', copiedText
);
543 * Cancel the cut event completely
545 Terminal
.bindCut = function(term
) {
546 on(term
.element
, 'cut', function (ev
) {
552 Terminal
.bindDrop = function (term
) {
554 * Do not perform the "drop" event. Altering the contents of the
555 * terminal with drag n drop is unwanted behavior.
557 on(term
.element
, 'drop', function (ev
) {
558 term
.cancel(ev
, true);
563 Terminal
.click = function (term
) {
565 * Do not perform the "drop" event. Altering the contents of the
566 * terminal with drag n drop is unwanted behavior.
568 on(term
.element
, 'click', function (ev
) {
569 term
.cancel(ev
, true);
575 * Insert the given row to the terminal or produce a new one
576 * if no row argument is passed. Return the inserted row.
578 Terminal
.prototype.insertRow = function (row
) {
579 if (typeof row
!= 'object') {
580 row
= document
.createElement('div');
583 this.rowContainer
.appendChild(row
);
584 this.children
.push(row
);
591 * Open Terminal in the DOM
593 Terminal
.prototype.open = function(parent
) {
594 var self
=this, i
=0, div
;
596 this.parent
= parent
|| this.parent
;
599 throw new Error('Terminal requires a parent element.');
603 * Grab global elements
605 this.context
= this.parent
.ownerDocument
.defaultView
;
606 this.document
= this.parent
.ownerDocument
;
607 this.body
= this.document
.getElementsByTagName('body')[0];
612 if (this.context
.navigator
&& this.context
.navigator
.userAgent
) {
613 this.isMac
= !!~this.context
.navigator
.userAgent
.indexOf('Mac');
614 this.isIpad
= !!~this.context
.navigator
.userAgent
.indexOf('iPad');
615 this.isIphone
= !!~this.context
.navigator
.userAgent
.indexOf('iPhone');
616 this.isMSIE
= !!~this.context
.navigator
.userAgent
.indexOf('MSIE');
620 * Create main element container
622 this.element
= this.document
.createElement('div');
623 this.element
.classList
.add('terminal');
624 this.element
.classList
.add('xterm');
625 this.element
.classList
.add('xterm-theme-' + this.theme
);
626 this.element
.setAttribute('tabindex', 0);
627 this.element
.spellcheck
= 'false';
630 * Create the container that will hold the lines of the terminal and then
631 * produce the lines the lines.
633 this.rowContainer
= document
.createElement('div');
634 this.rowContainer
.classList
.add('xterm-rows');
635 this.element
.appendChild(this.rowContainer
);
638 for (; i
< this.rows
; i
++) {
641 this.parent
.appendChild(this.element
);
644 this.refresh(0, this.rows
- 1);
646 // Initialize global actions that
647 // need to be taken on the document.
650 // Ensure there is a Terminal.focus.
653 // Start blinking the cursor.
656 on(this.element
, 'mouseup', function() {
657 var selection
= document
.getSelection(),
658 collapsed
= selection
.isCollapsed
,
659 isRange
= typeof collapsed
== 'boolean' ? !collapsed
: selection
.type
== 'Range';
665 // Listen for mouse events and translate
666 // them into terminal mouse protocols.
669 // Figure out whether boldness affects
670 // the character width of monospace fonts.
671 if (Terminal
.brokenBold
== null) {
672 Terminal
.brokenBold
= isBoldBroken(this.document
);
678 // XTerm mouse events
679 // http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#Mouse%20Tracking
680 // To better understand these
681 // the xterm code is very helpful:
683 // button.c, charproc.c, misc.c
684 // Relevant functions in xterm/button.c:
685 // BtnCode, EmitButtonCode, EditorButton, SendMousePosition
686 Terminal
.prototype.bindMouse = function() {
687 var el
= this.element
691 var wheelEvent
= 'onmousewheel' in this.context
695 // mouseup, mousedown, mousewheel
696 // left click: ^[[M 3<^[[M#3<
697 // mousewheel up: ^[[M`3>
698 function sendButton(ev
) {
702 // get the xterm-style button
703 button
= getButton(ev
);
705 // get mouse coordinates
709 sendEvent(button
, pos
);
716 // keep it at the left
717 // button, just in case.
728 // motion example of a left click:
729 // ^[[M 3<^[[M@4<^[[M@5<^[[M@6<^[[M@7<^[[M#7<
730 function sendMove(ev
) {
737 // buttons marked as motions
738 // are incremented by 32
741 sendEvent(button
, pos
);
745 // position to characters
746 function encode(data
, ch
) {
747 if (!self
.utfMouse
) {
748 if (ch
=== 255) return data
.push(0);
749 if (ch
> 127) ch
= 127;
752 if (ch
=== 2047) return data
.push(0);
756 if (ch
> 2047) ch
= 2047;
757 data
.push(0xC0 | (ch
>> 6));
758 data
.push(0x80 | (ch
& 0x3F));
763 // send a mouse event:
764 // regular/utf8: ^[[M Cb Cx Cy
765 // urxvt: ^[[ Cb ; Cx ; Cy M
766 // sgr: ^[[ Cb ; Cx ; Cy M/m
767 // vt300: ^[[ 24(1/3/5)~ [ Cx , Cy ] \r
768 // locator: CSI P e ; P b ; P r ; P c ; P p & w
769 function sendEvent(button
, pos
) {
770 // self.emit('mouse', {
776 if (self
.vt300Mouse
) {
778 // http://www.vt100.net/docs/vt3xx-gp/chapter15.html
782 var data
= '\x1b[24';
783 if (button
=== 0) data
+= '1';
784 else if (button
=== 1) data
+= '3';
785 else if (button
=== 2) data
+= '5';
786 else if (button
=== 3) return;
788 data
+= '~[' + pos
.x
+ ',' + pos
.y
+ ']\r';
793 if (self
.decLocator
) {
798 if (button
=== 0) button
= 2;
799 else if (button
=== 1) button
= 4;
800 else if (button
=== 2) button
= 6;
801 else if (button
=== 3) button
= 3;
805 + (button
=== 3 ? 4 : 0)
816 if (self
.urxvtMouse
) {
821 self
.send('\x1b[' + button
+ ';' + pos
.x
+ ';' + pos
.y
+ 'M');
829 + ((button
& 3) === 3 ? button
& ~3 : button
)
834 + ((button
& 3) === 3 ? 'm' : 'M'));
840 encode(data
, button
);
844 self
.send('\x1b[M' + String
.fromCharCode
.apply(String
, data
));
847 function getButton(ev
) {
860 // 1, and 2 - with 64 added
863 button
= ev
.button
!= null
870 button
= button
=== 1 ? 0 : button
=== 4 ? 1 : button
;
876 case 'DOMMouseScroll':
877 button
= ev
.detail
< 0
882 button
= ev
.wheelDeltaY
> 0
888 // next three bits are the modifiers:
889 // 4 = shift, 8 = meta, 16 = control
890 shift
= ev
.shiftKey
? 4 : 0;
891 meta
= ev
.metaKey
? 8 : 0;
892 ctrl
= ev
.ctrlKey
? 16 : 0;
893 mod
= shift
| meta
| ctrl
;
896 if (self
.vt200Mouse
) {
899 } else if (!self
.normalMouse
) {
904 button
= (32 + (mod
<< 2)) + button
;
909 // mouse coordinates measured in cols/rows
910 function getCoords(ev
) {
913 // ignore browsers without pageX for now
914 if (ev
.pageX
== null) return;
920 // should probably check offsetParent
921 // but this is more portable
922 while (el
&& el
!== self
.document
.documentElement
) {
925 el
= 'offsetParent' in el
930 // convert to cols/rows
931 w
= self
.element
.clientWidth
;
932 h
= self
.element
.clientHeight
;
933 x
= Math
.round((x
/ w
) * self
.cols
);
934 y
= Math
.round((y
/ h
) * self
.rows
);
936 // be sure to avoid sending
937 // bad positions to the program
939 if (x
> self
.cols
) x
= self
.cols
;
941 if (y
> self
.rows
) y
= self
.rows
;
943 // xterm sends raw bytes and
944 // starts at 32 (SP) for each.
951 type
: ev
.type
=== wheelEvent
957 on(el
, 'mousedown', function(ev
) {
958 if (!self
.mouseEvents
) return;
967 //if (self.vt200Mouse && !self.normalMouse) {
968 if (self
.vt200Mouse
) {
969 sendButton({ __proto__
: ev
, type
: 'mouseup' });
970 return self
.cancel(ev
);
974 if (self
.normalMouse
) on(self
.document
, 'mousemove', sendMove
);
976 // x10 compatibility mode can't send button releases
977 if (!self
.x10Mouse
) {
978 on(self
.document
, 'mouseup', function up(ev
) {
980 if (self
.normalMouse
) off(self
.document
, 'mousemove', sendMove
);
981 off(self
.document
, 'mouseup', up
);
982 return self
.cancel(ev
);
986 return self
.cancel(ev
);
989 //if (self.normalMouse) {
990 // on(self.document, 'mousemove', sendMove);
993 on(el
, wheelEvent
, function(ev
) {
994 if (!self
.mouseEvents
) return;
997 || self
.decLocator
) return;
999 return self
.cancel(ev
);
1002 // allow mousewheel scrolling in
1003 // the shell for example
1004 on(el
, wheelEvent
, function(ev
) {
1005 if (self
.mouseEvents
) return;
1006 if (self
.applicationKeypad
) return;
1007 if (ev
.type
=== 'DOMMouseScroll') {
1008 self
.scrollDisp(ev
.detail
< 0 ? -1 : 1);
1010 self
.scrollDisp(ev
.wheelDeltaY
> 0 ? -1 : 1);
1012 return self
.cancel(ev
);
1020 Terminal
.prototype.destroy = function() {
1021 this.readable
= false;
1022 this.writable
= false;
1024 this.handler = function() {};
1025 this.write = function() {};
1026 if (this.element
.parentNode
) {
1027 this.element
.parentNode
.removeChild(this.element
);
1029 //this.emit('close');
1034 * Flags used to render terminal text properly
1047 * In the screen buffer, each character
1048 * is stored as a an array with a character
1049 * and a 32-bit integer.
1050 * First value: a utf-16 character.
1052 * Next 9 bits: background color (0-511).
1053 * Next 9 bits: foreground color (0-511).
1054 * Next 14 bits: a mask for misc. flags:
1055 * 1=bold, 2=underline, 4=blink, 8=inverse, 16=invisible
1057 Terminal
.prototype.refresh = function(start
, end
) {
1058 var x
, y
, i
, line
, out
, ch
, width
, data
, attr
, bg
, fg
, flags
, row
, parent
, focused
= document
.activeElement
;
1060 if (end
- start
>= this.rows
/ 2) {
1061 parent
= this.element
.parentNode
;
1062 if (parent
) parent
.removeChild(this.element
);
1068 if (end
>= this.lines
.length
) {
1069 this.log('`end` is too large. Most likely a bad CSR.');
1070 end
= this.lines
.length
- 1;
1073 for (; y
<= end
; y
++) {
1074 row
= y
+ this.ydisp
;
1076 line
= this.lines
[row
];
1081 && (this.ydisp
=== this.ybase
)
1082 && !this.cursorHidden
) {
1088 attr
= this.defAttr
;
1091 for (; i
< width
; i
++) {
1095 if (i
=== x
) data
= -1;
1097 if (data
!== attr
) {
1098 if (attr
!== this.defAttr
) {
1101 if (data
!== this.defAttr
) {
1103 out
+= '<span class="reverse-video terminal-cursor">';
1105 out
+= '<span class="';
1108 fg
= (data
>> 9) & 0x1ff;
1111 if (flags
& Terminal
.flags
.BOLD
) {
1112 if (!Terminal
.brokenBold
) {
1113 out
+= ' xterm-bold ';
1115 // See: XTerm*boldColors
1116 if (fg
< 8) fg
+= 8;
1119 if (flags
& Terminal
.flags
.UNDERLINE
) {
1120 out
+= ' xterm-underline ';
1123 if (flags
& Terminal
.flags
.BLINK
) {
1124 out
+= ' xterm-blink ';
1128 * If inverse flag is on, then swap the foreground and background variables.
1130 if (flags
& Terminal
.flags
.INVERSE
) {
1131 /* One-line variable swap in JavaScript: http://stackoverflow.com/a/16201730 */
1132 bg = [fg, fg = bg][0];
1133 // Should inverse just be before the
1134 // above boldColors effect instead?
1135 if ((flags & 1) && fg < 8) fg += 8;
1138 if (flags & Terminal.flags.INVISIBLE) {
1139 out += ' xterm-hidden ';
1143 * Weird situation: Invert flag used black foreground and white background results
1144 * in invalid background color, positioned at the 256 index of the 256 terminal
1145 * color map. Pin the colors manually in such a case.
1147 * Source: https://github.com/sourcelair/xterm.js/issues/57
1149 if (flags
& Terminal
.flags
.INVERSE
) {
1159 out
+= ' xterm-bg-color-' + bg
+ ' ';
1163 out
+= ' xterm-color-' + fg
+ ' ';
1185 if (isWide(ch
)) i
++;
1194 if (attr
!== this.defAttr
) {
1198 this.children
[y
].innerHTML
= out
;
1202 parent
.appendChild(this.element
);
1206 * Return focus to previously focused element
1209 this.emit('refresh', {element
: this.element
, start
: start
, end
: end
});
1212 Terminal
.prototype._cursorBlink = function() {
1213 if (Terminal
.focus
!== this) return;
1214 this.cursorState
^= 1;
1215 this.refresh(this.y
, this.y
);
1218 Terminal
.prototype.showCursor = function() {
1219 if (!this.cursorState
) {
1220 this.cursorState
= 1;
1221 this.refresh(this.y
, this.y
);
1223 // Temporarily disabled:
1224 // this.refreshBlink();
1228 Terminal
.prototype.startBlink = function() {
1229 if (!this.cursorBlink
) return;
1231 this._blinker = function() {
1232 self
._cursorBlink();
1234 this._blink
= setInterval(this._blinker
, 500);
1237 Terminal
.prototype.refreshBlink = function() {
1238 if (!this.cursorBlink
) return;
1239 clearInterval(this._blink
);
1240 this._blink
= setInterval(this._blinker
, 500);
1243 Terminal
.prototype.scroll = function() {
1246 if (++this.ybase
=== this.scrollback
) {
1247 this.ybase
= this.ybase
/ 2 | 0;
1248 this.lines
= this.lines
.slice(-(this.ybase
+ this.rows
) + 1);
1251 this.ydisp
= this.ybase
;
1254 row
= this.ybase
+ this.rows
- 1;
1256 // subtract the bottom scroll region
1257 row
-= this.rows
- 1 - this.scrollBottom
;
1259 if (row
=== this.lines
.length
) {
1260 // potential optimization:
1261 // pushing is faster than splicing
1262 // when they amount to the same
1264 this.lines
.push(this.blankLine());
1267 this.lines
.splice(row
, 0, this.blankLine());
1270 if (this.scrollTop
!== 0) {
1271 if (this.ybase
!== 0) {
1273 this.ydisp
= this.ybase
;
1275 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
1279 this.updateRange(this.scrollTop
);
1280 this.updateRange(this.scrollBottom
);
1283 Terminal
.prototype.scrollDisp = function(disp
) {
1286 if (this.ydisp
> this.ybase
) {
1287 this.ydisp
= this.ybase
;
1288 } else if (this.ydisp
< 0) {
1292 this.refresh(0, this.rows
- 1);
1295 Terminal
.prototype.write = function(data
) {
1296 var l
= data
.length
, i
= 0, j
, cs
, ch
;
1298 this.refreshStart
= this.y
;
1299 this.refreshEnd
= this.y
;
1301 if (this.ybase
!== this.ydisp
) {
1302 this.ydisp
= this.ybase
;
1306 for (; i
< l
; i
++) {
1308 switch (this.state
) {
1319 if (this.convertEol
) {
1323 if (this.y
> this.scrollBottom
) {
1343 this.x
= this.nextStop();
1358 this.state
= escaped
;
1364 if (this.charset
&& this.charset
[ch
]) {
1365 ch
= this.charset
[ch
];
1368 if (this.x
>= this.cols
) {
1371 if (this.y
> this.scrollBottom
) {
1377 this.lines
[this.y
+ this.ybase
][this.x
] = [this.curAttr
, ch
];
1379 this.updateRange(this.y
);
1382 j
= this.y
+ this.ybase
;
1383 if (this.cols
< 2 || this.x
>= this.cols
) {
1384 this.lines
[j
][this.x
- 1] = [this.curAttr
, ' '];
1387 this.lines
[j
][this.x
] = [this.curAttr
, ' '];
1396 // ESC [ Control Sequence Introducer ( CSI is 0x9b).
1399 this.currentParam
= 0;
1403 // ESC ] Operating System Command ( OSC is 0x9d).
1406 this.currentParam
= 0;
1410 // ESC P Device Control String ( DCS is 0x90).
1413 this.currentParam
= 0;
1417 // ESC _ Application Program Command ( APC is 0x9f).
1419 this.state
= ignore
;
1422 // ESC ^ Privacy Message ( PM is 0x9e).
1424 this.state
= ignore
;
1427 // ESC c Full Reset (RIS).
1432 // ESC E Next Line ( NEL is 0x85).
1433 // ESC D Index ( IND is 0x84).
1441 // ESC M Reverse Index ( RI is 0x8d).
1443 this.reverseIndex();
1446 // ESC % Select default/utf-8 character set.
1447 // @ = default, G = utf-8
1449 //this.charset = null;
1451 this.setgCharset(0, Terminal
.charsets
.US
);
1452 this.state
= normal
;
1456 // ESC (,),*,+,-,. Designate G0-G2 Character Set.
1457 case '(': // <-- this seems to get all the attention
1483 this.state
= charset
;
1486 // Designate G3 Character Set (VT300).
1487 // A = ISO Latin-1 Supplemental.
1491 this.state
= charset
;
1496 // Single Shift Select of G2 Character Set
1497 // ( SS2 is 0x8e). This affects next character only.
1501 // Single Shift Select of G3 Character Set
1502 // ( SS3 is 0x8f). This affects next character only.
1506 // Invoke the G2 Character Set as GL (LS2).
1511 // Invoke the G3 Character Set as GL (LS3).
1516 // Invoke the G3 Character Set as GR (LS3R).
1521 // Invoke the G2 Character Set as GR (LS2R).
1526 // Invoke the G1 Character Set as GR (LS1R).
1531 // ESC 7 Save Cursor (DECSC).
1534 this.state
= normal
;
1537 // ESC 8 Restore Cursor (DECRC).
1539 this.restoreCursor();
1540 this.state
= normal
;
1543 // ESC # 3 DEC line height/width
1545 this.state
= normal
;
1549 // ESC H Tab Set (HTS is 0x88).
1554 // ESC = Application Keypad (DECPAM).
1556 this.log('Serial port requested application keypad.');
1557 this.applicationKeypad
= true;
1558 this.state
= normal
;
1561 // ESC > Normal Keypad (DECPNM).
1563 this.log('Switching back to normal keypad.');
1564 this.applicationKeypad
= false;
1565 this.state
= normal
;
1569 this.state
= normal
;
1570 this.error('Unknown ESC control: %s.', ch
);
1577 case '0': // DEC Special Character and Line Drawing Set.
1578 cs
= Terminal
.charsets
.SCLD
;
1581 cs
= Terminal
.charsets
.UK
;
1583 case 'B': // United States (USASCII).
1584 cs
= Terminal
.charsets
.US
;
1587 cs
= Terminal
.charsets
.Dutch
;
1589 case 'C': // Finnish
1591 cs
= Terminal
.charsets
.Finnish
;
1594 cs
= Terminal
.charsets
.French
;
1596 case 'Q': // FrenchCanadian
1597 cs
= Terminal
.charsets
.FrenchCanadian
;
1600 cs
= Terminal
.charsets
.German
;
1602 case 'Y': // Italian
1603 cs
= Terminal
.charsets
.Italian
;
1605 case 'E': // NorwegianDanish
1607 cs
= Terminal
.charsets
.NorwegianDanish
;
1609 case 'Z': // Spanish
1610 cs
= Terminal
.charsets
.Spanish
;
1612 case 'H': // Swedish
1614 cs
= Terminal
.charsets
.Swedish
;
1617 cs
= Terminal
.charsets
.Swiss
;
1619 case '/': // ISOLatin (actually /A)
1620 cs
= Terminal
.charsets
.ISOLatin
;
1624 cs
= Terminal
.charsets
.US
;
1627 this.setgCharset(this.gcharset
, cs
);
1628 this.gcharset
= null;
1629 this.state
= normal
;
1635 // Set Text Parameters.
1636 if (ch
=== '\x1b' || ch
=== '\x07') {
1637 if (ch
=== '\x1b') i
++;
1639 this.params
.push(this.currentParam
);
1641 switch (this.params
[0]) {
1645 if (this.params
[1]) {
1646 this.title
= this.params
[1];
1647 this.handleTitle(this.title
);
1655 // change dynamic colors
1667 // change dynamic ui colors
1679 // manipulate selection data
1697 this.currentParam
= 0;
1698 this.state
= normal
;
1700 if (!this.params
.length
) {
1701 if (ch
>= '0' && ch
<= '9') {
1703 this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1704 } else if (ch
=== ';') {
1705 this.params
.push(this.currentParam
);
1706 this.currentParam
= '';
1709 this.currentParam
+= ch
;
1716 if (ch
=== '?' || ch
=== '>' || ch
=== '!') {
1722 if (ch
>= '0' && ch
<= '9') {
1723 this.currentParam
= this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1727 // '$', '"', ' ', '\''
1728 if (ch
=== '$' || ch
=== '"' || ch
=== ' ' || ch
=== '\'') {
1733 this.params
.push(this.currentParam
);
1734 this.currentParam
= 0;
1737 if (ch
=== ';') break;
1739 this.state
= normal
;
1743 // Cursor Up Ps Times (default = 1) (CUU).
1745 this.cursorUp(this.params
);
1749 // Cursor Down Ps Times (default = 1) (CUD).
1751 this.cursorDown(this.params
);
1755 // Cursor Forward Ps Times (default = 1) (CUF).
1757 this.cursorForward(this.params
);
1761 // Cursor Backward Ps Times (default = 1) (CUB).
1763 this.cursorBackward(this.params
);
1767 // Cursor Position [row;column] (default = [1,1]) (CUP).
1769 this.cursorPos(this.params
);
1772 // CSI Ps J Erase in Display (ED).
1774 this.eraseInDisplay(this.params
);
1777 // CSI Ps K Erase in Line (EL).
1779 this.eraseInLine(this.params
);
1782 // CSI Pm m Character Attributes (SGR).
1785 this.charAttributes(this.params
);
1789 // CSI Ps n Device Status Report (DSR).
1792 this.deviceStatus(this.params
);
1801 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
1803 this.insertChars(this.params
);
1807 // Cursor Next Line Ps Times (default = 1) (CNL).
1809 this.cursorNextLine(this.params
);
1813 // Cursor Preceding Line Ps Times (default = 1) (CNL).
1815 this.cursorPrecedingLine(this.params
);
1819 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
1821 this.cursorCharAbsolute(this.params
);
1825 // Insert Ps Line(s) (default = 1) (IL).
1827 this.insertLines(this.params
);
1831 // Delete Ps Line(s) (default = 1) (DL).
1833 this.deleteLines(this.params
);
1837 // Delete Ps Character(s) (default = 1) (DCH).
1839 this.deleteChars(this.params
);
1843 // Erase Ps Character(s) (default = 1) (ECH).
1845 this.eraseChars(this.params
);
1848 // CSI Pm ` Character Position Absolute
1849 // [column] (default = [row,1]) (HPA).
1851 this.charPosAbsolute(this.params
);
1855 // Horizontal Position Relative
1857 this.HPositionRelative(this.params
);
1861 // Send Device Attributes (Primary DA).
1863 // Send Device Attributes (Secondary DA)
1865 this.sendDeviceAttributes(this.params
);
1869 // Line Position Absolute [row] (default = [1,column]) (VPA).
1871 this.linePosAbsolute(this.params
);
1874 // 145 65 e * VPR - Vertical Position Relative
1876 this.VPositionRelative(this.params
);
1880 // Horizontal and Vertical Position [row;column] (default =
1883 this.HVPosition(this.params
);
1886 // CSI Pm h Set Mode (SM).
1887 // CSI ? Pm h - mouse escape codes, cursor escape codes
1889 this.setMode(this.params
);
1892 // CSI Pm l Reset Mode (RM).
1895 this.resetMode(this.params
);
1899 // Set Scrolling Region [top;bottom] (default = full size of win-
1903 this.setScrollRegion(this.params
);
1907 // Save cursor (ANSI.SYS).
1909 this.saveCursor(this.params
);
1913 // Restore cursor (ANSI.SYS).
1915 this.restoreCursor(this.params
);
1923 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
1925 this.cursorForwardTab(this.params
);
1928 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
1930 this.scrollUp(this.params
);
1933 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
1934 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
1937 // if (this.prefix === '>') {
1938 // this.resetTitleModes(this.params);
1941 // if (this.params.length > 2) {
1942 // this.initMouseTracking(this.params);
1945 if (this.params
.length
< 2 && !this.prefix
) {
1946 this.scrollDown(this.params
);
1951 // Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
1953 this.cursorBackwardTab(this.params
);
1956 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
1958 this.repeatPrecedingCharacter(this.params
);
1961 // CSI Ps g Tab Clear (TBC).
1963 this.tabClear(this.params
);
1966 // CSI Pm i Media Copy (MC).
1969 // this.mediaCopy(this.params);
1972 // CSI Pm m Character Attributes (SGR).
1974 // case 'm': // duplicate
1975 // if (this.prefix === '>') {
1976 // this.setResources(this.params);
1978 // this.charAttributes(this.params);
1982 // CSI Ps n Device Status Report (DSR).
1984 // case 'n': // duplicate
1985 // if (this.prefix === '>') {
1986 // this.disableModifiers(this.params);
1988 // this.deviceStatus(this.params);
1992 // CSI > Ps p Set pointer mode.
1993 // CSI ! p Soft terminal reset (DECSTR).
1995 // Request ANSI mode (DECRQM).
1997 // Request DEC private mode (DECRQM).
2000 switch (this.prefix
) {
2002 // this.setPointerMode(this.params);
2005 this.softReset(this.params
);
2008 // if (this.postfix === '$') {
2009 // this.requestPrivateMode(this.params);
2013 // if (this.postfix === '"') {
2014 // this.setConformanceLevel(this.params);
2015 // } else if (this.postfix === '$') {
2016 // this.requestAnsiMode(this.params);
2022 // CSI Ps q Load LEDs (DECLL).
2026 // if (this.postfix === ' ') {
2027 // this.setCursorStyle(this.params);
2030 // if (this.postfix === '"') {
2031 // this.setCharProtectionAttr(this.params);
2034 // this.loadLEDs(this.params);
2038 // Set Scrolling Region [top;bottom] (default = full size of win-
2041 // CSI Pt; Pl; Pb; Pr; Ps$ r
2042 // case 'r': // duplicate
2043 // if (this.prefix === '?') {
2044 // this.restorePrivateValues(this.params);
2045 // } else if (this.postfix === '$') {
2046 // this.setAttrInRectangle(this.params);
2048 // this.setScrollRegion(this.params);
2052 // CSI s Save cursor (ANSI.SYS).
2054 // case 's': // duplicate
2055 // if (this.prefix === '?') {
2056 // this.savePrivateValues(this.params);
2058 // this.saveCursor(this.params);
2062 // CSI Ps ; Ps ; Ps t
2063 // CSI Pt; Pl; Pb; Pr; Ps$ t
2067 // if (this.postfix === '$') {
2068 // this.reverseAttrInRectangle(this.params);
2069 // } else if (this.postfix === ' ') {
2070 // this.setWarningBellVolume(this.params);
2072 // if (this.prefix === '>') {
2073 // this.setTitleModeFeature(this.params);
2075 // this.manipulateWindow(this.params);
2080 // CSI u Restore cursor (ANSI.SYS).
2082 // case 'u': // duplicate
2083 // if (this.postfix === ' ') {
2084 // this.setMarginBellVolume(this.params);
2086 // this.restoreCursor(this.params);
2090 // CSI Pt; Pl; Pb; Pr; Pp; Pt; Pl; Pp$ v
2092 // if (this.postfix === '$') {
2093 // this.copyRectagle(this.params);
2097 // CSI Pt ; Pl ; Pb ; Pr ' w
2099 // if (this.postfix === '\'') {
2100 // this.enableFilterRectangle(this.params);
2104 // CSI Ps x Request Terminal Parameters (DECREQTPARM).
2105 // CSI Ps x Select Attribute Change Extent (DECSACE).
2106 // CSI Pc; Pt; Pl; Pb; Pr$ x
2108 // if (this.postfix === '$') {
2109 // this.fillRectangle(this.params);
2111 // this.requestParameters(this.params);
2112 // //this.__(this.params);
2117 // CSI Pt; Pl; Pb; Pr$ z
2119 // if (this.postfix === '\'') {
2120 // this.enableLocatorReporting(this.params);
2121 // } else if (this.postfix === '$') {
2122 // this.eraseRectangle(this.params);
2127 // CSI Pt; Pl; Pb; Pr$ {
2129 // if (this.postfix === '\'') {
2130 // this.setLocatorEvents(this.params);
2131 // } else if (this.postfix === '$') {
2132 // this.selectiveEraseRectangle(this.params);
2138 // if (this.postfix === '\'') {
2139 // this.requestLocatorPosition(this.params);
2144 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
2146 // if (this.postfix === ' ') {
2147 // this.insertColumns(this.params);
2152 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
2154 // if (this.postfix === ' ') {
2155 // this.deleteColumns(this.params);
2160 this.error('Unknown CSI code: %s.', ch
);
2169 if (ch
=== '\x1b' || ch
=== '\x07') {
2170 if (ch
=== '\x1b') i
++;
2172 switch (this.prefix
) {
2173 // User-Defined Keys (DECUDK).
2177 // Request Status String (DECRQSS).
2178 // test: echo -e '\eP$q"p\e\\'
2180 var pt
= this.currentParam
2197 + (this.scrollTop
+ 1)
2199 + (this.scrollBottom
+ 1)
2209 this.error('Unknown DCS Pt: %s.', pt
);
2214 this.send('\x1bP' + +valid
+ '$r' + pt
+ '\x1b\\');
2217 // Set Termcap/Terminfo Data (xterm, experimental).
2221 // Request Termcap/Terminfo String (xterm, experimental)
2222 // Regular xterm does not even respond to this sequence.
2223 // This can cause a small glitch in vim.
2224 // test: echo -ne '\eP+q6b64\e\\'
2226 var pt
= this.currentParam
2229 this.send('\x1bP' + +valid
+ '+r' + pt
+ '\x1b\\');
2233 this.error('Unknown DCS prefix: %s.', this.prefix
);
2237 this.currentParam
= 0;
2239 this.state
= normal
;
2240 } else if (!this.currentParam
) {
2241 if (!this.prefix
&& ch
!== '$' && ch
!== '+') {
2242 this.currentParam
= ch
;
2243 } else if (this.prefix
.length
=== 2) {
2244 this.currentParam
= ch
;
2249 this.currentParam
+= ch
;
2255 if (ch
=== '\x1b' || ch
=== '\x07') {
2256 if (ch
=== '\x1b') i
++;
2257 this.state
= normal
;
2263 this.updateRange(this.y
);
2264 this.refresh(this.refreshStart
, this.refreshEnd
);
2267 Terminal
.prototype.writeln = function(data
) {
2268 this.write(data
+ '\r\n');
2272 // https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
2273 Terminal
.prototype.keyDown = function(ev
) {
2274 var self
= this, key
;
2276 switch (ev
.keyCode
) {
2292 this.cancel(ev
, true);
2297 this.cancel(ev
, true);
2302 this.cancel(ev
, true);
2307 this.cancel(ev
, true);
2308 key
= '\x1bb' // Jump a word back
2310 } else if (this.applicationCursor
) {
2311 key
= '\x1bOD'; // SS3 as ^[O for 7-bit
2319 this.cancel(ev
, true);
2320 key
= '\x1bf' // Jump a word forward
2322 } else if (this.applicationCursor
) {
2330 if (this.applicationCursor
) {
2335 this.scrollDisp(-1);
2336 return this.cancel(ev
);
2343 if (this.applicationCursor
) {
2349 return this.cancel(ev
);
2364 if (this.applicationKeypad
) {
2372 if (this.applicationKeypad
) {
2381 this.scrollDisp(-(this.rows
- 1));
2382 return this.cancel(ev
);
2390 this.scrollDisp(this.rows
- 1);
2391 return this.cancel(ev
);
2446 if (ev
.ctrlKey
&& !ev
.shiftKey
&& !ev
.altKey
&& !ev
.metaKey
) {
2447 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2448 key
= String
.fromCharCode(ev
.keyCode
- 64);
2449 } else if (ev
.keyCode
=== 32) {
2451 key
= String
.fromCharCode(0);
2452 } else if (ev
.keyCode
>= 51 && ev
.keyCode
<= 55) {
2453 // escape, file sep, group sep, record sep, unit sep
2454 key
= String
.fromCharCode(ev
.keyCode
- 51 + 27);
2455 } else if (ev
.keyCode
=== 56) {
2457 key
= String
.fromCharCode(127);
2458 } else if (ev
.keyCode
=== 219) {
2460 key
= String
.fromCharCode(27);
2461 } else if (ev
.keyCode
=== 221) {
2463 key
= String
.fromCharCode(29);
2465 } else if ((!this.isMac
&& ev
.altKey
) || (this.isMac
&& ev
.metaKey
)) {
2466 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2467 key
= '\x1b' + String
.fromCharCode(ev
.keyCode
+ 32);
2468 } else if (ev
.keyCode
=== 192) {
2470 } else if (ev
.keyCode
>= 48 && ev
.keyCode
<= 57) {
2471 key
= '\x1b' + (ev
.keyCode
- 48);
2477 if (!key
|| (this.isMac
&& ev
.metaKey
)) {
2481 this.emit('keydown', ev
);
2482 this.emit('key', key
, ev
);
2486 return this.cancel(ev
, true);
2489 Terminal
.prototype.setgLevel = function(g
) {
2491 this.charset
= this.charsets
[g
];
2494 Terminal
.prototype.setgCharset = function(g
, charset
) {
2495 this.charsets
[g
] = charset
;
2496 if (this.glevel
=== g
) {
2497 this.charset
= charset
;
2501 Terminal
.prototype.keyPress = function(ev
) {
2506 } else if (ev
.which
== null) {
2508 } else if (ev
.which
!== 0 && ev
.charCode
!== 0) {
2514 if (!key
|| ev
.ctrlKey
|| ev
.altKey
|| ev
.metaKey
) {
2518 key
= String
.fromCharCode(key
);
2521 * When a key is pressed and a character is sent to the terminal, then clear any text
2522 * selected in the terminal.
2525 this.clearSelection();
2528 this.emit('keypress', key
, ev
);
2529 this.emit('key', key
, ev
);
2533 this.cancel(ev
, true);
2538 Terminal
.prototype.send = function(data
) {
2542 setTimeout(function() {
2543 self
.handler(self
.queue
);
2551 Terminal
.prototype.bell = function() {
2552 if (!this.visualBell
) return;
2554 this.element
.style
.borderColor
= 'white';
2555 setTimeout(function() {
2556 self
.element
.style
.borderColor
= '';
2558 if (this.popOnBell
) this.focus();
2561 Terminal
.prototype.log = function() {
2562 if (!this.debug
) return;
2563 if (!this.context
.console
|| !this.context
.console
.log
) return;
2564 var args
= Array
.prototype.slice
.call(arguments
);
2565 this.context
.console
.log
.apply(this.context
.console
, args
);
2568 Terminal
.prototype.error = function() {
2569 if (!this.debug
) return;
2570 if (!this.context
.console
|| !this.context
.console
.error
) return;
2571 var args
= Array
.prototype.slice
.call(arguments
);
2572 this.context
.console
.error
.apply(this.context
.console
, args
);
2575 Terminal
.prototype.resize = function(x
, y
) {
2588 ch
= [this.defAttr
, ' ']; // does xterm use the default attr?
2589 i
= this.lines
.length
;
2591 while (this.lines
[i
].length
< x
) {
2592 this.lines
[i
].push(ch
);
2596 i
= this.lines
.length
;
2598 while (this.lines
[i
].length
> x
) {
2599 this.lines
[i
].pop();
2611 if (this.lines
.length
< y
+ this.ybase
) {
2612 this.lines
.push(this.blankLine());
2614 if (this.children
.length
< y
) {
2620 if (this.lines
.length
> y
+ this.ybase
) {
2623 if (this.children
.length
> y
) {
2624 el
= this.children
.shift();
2626 el
.parentNode
.removeChild(el
);
2633 * Make sure that the cursor stays on screen
2644 this.scrollBottom
= y
- 1;
2646 this.refresh(0, this.rows
- 1);
2650 this.emit('resize', {terminal
: this, cols
: x
, rows
: y
});
2653 Terminal
.prototype.updateRange = function(y
) {
2654 if (y
< this.refreshStart
) this.refreshStart
= y
;
2655 if (y
> this.refreshEnd
) this.refreshEnd
= y
;
2656 // if (y > this.refreshEnd) {
2657 // this.refreshEnd = y;
2658 // if (y > this.rows - 1) {
2659 // this.refreshEnd = this.rows - 1;
2664 Terminal
.prototype.maxRange = function() {
2665 this.refreshStart
= 0;
2666 this.refreshEnd
= this.rows
- 1;
2669 Terminal
.prototype.setupStops = function(i
) {
2671 if (!this.tabs
[i
]) {
2672 i
= this.prevStop(i
);
2679 for (; i
< this.cols
; i
+= 8) {
2680 this.tabs
[i
] = true;
2684 Terminal
.prototype.prevStop = function(x
) {
2685 if (x
== null) x
= this.x
;
2686 while (!this.tabs
[--x
] && x
> 0);
2687 return x
>= this.cols
2692 Terminal
.prototype.nextStop = function(x
) {
2693 if (x
== null) x
= this.x
;
2694 while (!this.tabs
[++x
] && x
< this.cols
);
2695 return x
>= this.cols
2700 Terminal
.prototype.eraseRight = function(x
, y
) {
2701 var line
= this.lines
[this.ybase
+ y
]
2702 , ch
= [this.eraseAttr(), ' ']; // xterm
2705 for (; x
< this.cols
; x
++) {
2709 this.updateRange(y
);
2712 Terminal
.prototype.eraseLeft = function(x
, y
) {
2713 var line
= this.lines
[this.ybase
+ y
]
2714 , ch
= [this.eraseAttr(), ' ']; // xterm
2717 while (x
--) line
[x
] = ch
;
2719 this.updateRange(y
);
2722 Terminal
.prototype.eraseLine = function(y
) {
2723 this.eraseRight(0, y
);
2726 Terminal
.prototype.blankLine = function(cur
) {
2731 var ch
= [attr
, ' ']
2735 for (; i
< this.cols
; i
++) {
2742 Terminal
.prototype.ch = function(cur
) {
2744 ? [this.eraseAttr(), ' ']
2745 : [this.defAttr
, ' '];
2748 Terminal
.prototype.is = function(term
) {
2749 var name
= this.termName
;
2750 return (name
+ '').indexOf(term
) === 0;
2753 Terminal
.prototype.handler = function(data
) {
2754 this.emit('data', data
);
2757 Terminal
.prototype.handleTitle = function(title
) {
2758 this.emit('title', title
);
2765 // ESC D Index (IND is 0x84).
2766 Terminal
.prototype.index = function() {
2768 if (this.y
> this.scrollBottom
) {
2772 this.state
= normal
;
2775 // ESC M Reverse Index (RI is 0x8d).
2776 Terminal
.prototype.reverseIndex = function() {
2779 if (this.y
< this.scrollTop
) {
2781 // possibly move the code below to term.reverseScroll();
2782 // test: echo -ne '\e[1;1H\e[44m\eM\e[0m'
2783 // blankLine(true) is xterm/linux behavior
2784 this.lines
.splice(this.y
+ this.ybase
, 0, this.blankLine(true));
2785 j
= this.rows
- 1 - this.scrollBottom
;
2786 this.lines
.splice(this.rows
- 1 + this.ybase
- j
+ 1, 1);
2788 this.updateRange(this.scrollTop
);
2789 this.updateRange(this.scrollBottom
);
2791 this.state
= normal
;
2794 // ESC c Full Reset (RIS).
2795 Terminal
.prototype.reset = function() {
2796 this.options
.rows
= this.rows
;
2797 this.options
.cols
= this.cols
;
2798 Terminal
.call(this, this.options
);
2799 this.refresh(0, this.rows
- 1);
2802 // ESC H Tab Set (HTS is 0x88).
2803 Terminal
.prototype.tabSet = function() {
2804 this.tabs
[this.x
] = true;
2805 this.state
= normal
;
2813 // Cursor Up Ps Times (default = 1) (CUU).
2814 Terminal
.prototype.cursorUp = function(params
) {
2815 var param
= params
[0];
2816 if (param
< 1) param
= 1;
2818 if (this.y
< 0) this.y
= 0;
2822 // Cursor Down Ps Times (default = 1) (CUD).
2823 Terminal
.prototype.cursorDown = function(params
) {
2824 var param
= params
[0];
2825 if (param
< 1) param
= 1;
2827 if (this.y
>= this.rows
) {
2828 this.y
= this.rows
- 1;
2833 // Cursor Forward Ps Times (default = 1) (CUF).
2834 Terminal
.prototype.cursorForward = function(params
) {
2835 var param
= params
[0];
2836 if (param
< 1) param
= 1;
2838 if (this.x
>= this.cols
) {
2839 this.x
= this.cols
- 1;
2844 // Cursor Backward Ps Times (default = 1) (CUB).
2845 Terminal
.prototype.cursorBackward = function(params
) {
2846 var param
= params
[0];
2847 if (param
< 1) param
= 1;
2849 if (this.x
< 0) this.x
= 0;
2853 // Cursor Position [row;column] (default = [1,1]) (CUP).
2854 Terminal
.prototype.cursorPos = function(params
) {
2857 row
= params
[0] - 1;
2859 if (params
.length
>= 2) {
2860 col
= params
[1] - 1;
2867 } else if (row
>= this.rows
) {
2868 row
= this.rows
- 1;
2873 } else if (col
>= this.cols
) {
2874 col
= this.cols
- 1;
2881 // CSI Ps J Erase in Display (ED).
2882 // Ps = 0 -> Erase Below (default).
2883 // Ps = 1 -> Erase Above.
2884 // Ps = 2 -> Erase All.
2885 // Ps = 3 -> Erase Saved Lines (xterm).
2887 // Erase in Display (DECSED).
2888 // Ps = 0 -> Selective Erase Below (default).
2889 // Ps = 1 -> Selective Erase Above.
2890 // Ps = 2 -> Selective Erase All.
2891 Terminal
.prototype.eraseInDisplay = function(params
) {
2893 switch (params
[0]) {
2895 this.eraseRight(this.x
, this.y
);
2897 for (; j
< this.rows
; j
++) {
2902 this.eraseLeft(this.x
, this.y
);
2910 while (j
--) this.eraseLine(j
);
2918 // CSI Ps K Erase in Line (EL).
2919 // Ps = 0 -> Erase to Right (default).
2920 // Ps = 1 -> Erase to Left.
2921 // Ps = 2 -> Erase All.
2923 // Erase in Line (DECSEL).
2924 // Ps = 0 -> Selective Erase to Right (default).
2925 // Ps = 1 -> Selective Erase to Left.
2926 // Ps = 2 -> Selective Erase All.
2927 Terminal
.prototype.eraseInLine = function(params
) {
2928 switch (params
[0]) {
2930 this.eraseRight(this.x
, this.y
);
2933 this.eraseLeft(this.x
, this.y
);
2936 this.eraseLine(this.y
);
2941 // CSI Pm m Character Attributes (SGR).
2942 // Ps = 0 -> Normal (default).
2944 // Ps = 4 -> Underlined.
2945 // Ps = 5 -> Blink (appears as Bold).
2946 // Ps = 7 -> Inverse.
2947 // Ps = 8 -> Invisible, i.e., hidden (VT300).
2948 // Ps = 2 2 -> Normal (neither bold nor faint).
2949 // Ps = 2 4 -> Not underlined.
2950 // Ps = 2 5 -> Steady (not blinking).
2951 // Ps = 2 7 -> Positive (not inverse).
2952 // Ps = 2 8 -> Visible, i.e., not hidden (VT300).
2953 // Ps = 3 0 -> Set foreground color to Black.
2954 // Ps = 3 1 -> Set foreground color to Red.
2955 // Ps = 3 2 -> Set foreground color to Green.
2956 // Ps = 3 3 -> Set foreground color to Yellow.
2957 // Ps = 3 4 -> Set foreground color to Blue.
2958 // Ps = 3 5 -> Set foreground color to Magenta.
2959 // Ps = 3 6 -> Set foreground color to Cyan.
2960 // Ps = 3 7 -> Set foreground color to White.
2961 // Ps = 3 9 -> Set foreground color to default (original).
2962 // Ps = 4 0 -> Set background color to Black.
2963 // Ps = 4 1 -> Set background color to Red.
2964 // Ps = 4 2 -> Set background color to Green.
2965 // Ps = 4 3 -> Set background color to Yellow.
2966 // Ps = 4 4 -> Set background color to Blue.
2967 // Ps = 4 5 -> Set background color to Magenta.
2968 // Ps = 4 6 -> Set background color to Cyan.
2969 // Ps = 4 7 -> Set background color to White.
2970 // Ps = 4 9 -> Set background color to default (original).
2972 // If 16-color support is compiled, the following apply. Assume
2973 // that xterm's resources are set so that the ISO color codes are
2974 // the first 8 of a set of 16. Then the aixterm colors are the
2975 // bright versions of the ISO colors:
2976 // Ps = 9 0 -> Set foreground color to Black.
2977 // Ps = 9 1 -> Set foreground color to Red.
2978 // Ps = 9 2 -> Set foreground color to Green.
2979 // Ps = 9 3 -> Set foreground color to Yellow.
2980 // Ps = 9 4 -> Set foreground color to Blue.
2981 // Ps = 9 5 -> Set foreground color to Magenta.
2982 // Ps = 9 6 -> Set foreground color to Cyan.
2983 // Ps = 9 7 -> Set foreground color to White.
2984 // Ps = 1 0 0 -> Set background color to Black.
2985 // Ps = 1 0 1 -> Set background color to Red.
2986 // Ps = 1 0 2 -> Set background color to Green.
2987 // Ps = 1 0 3 -> Set background color to Yellow.
2988 // Ps = 1 0 4 -> Set background color to Blue.
2989 // Ps = 1 0 5 -> Set background color to Magenta.
2990 // Ps = 1 0 6 -> Set background color to Cyan.
2991 // Ps = 1 0 7 -> Set background color to White.
2993 // If xterm is compiled with the 16-color support disabled, it
2994 // supports the following, from rxvt:
2995 // Ps = 1 0 0 -> Set foreground and background color to
2998 // If 88- or 256-color support is compiled, the following apply.
2999 // Ps = 3 8 ; 5 ; Ps -> Set foreground color to the second
3001 // Ps = 4 8 ; 5 ; Ps -> Set background color to the second
3003 Terminal
.prototype.charAttributes = function(params
) {
3004 // Optimize a single SGR0.
3005 if (params
.length
=== 1 && params
[0] === 0) {
3006 this.curAttr
= this.defAttr
;
3010 var l
= params
.length
3012 , flags
= this.curAttr
>> 18
3013 , fg
= (this.curAttr
>> 9) & 0x1ff
3014 , bg
= this.curAttr
& 0x1ff
3017 for (; i
< l
; i
++) {
3019 if (p
>= 30 && p
<= 37) {
3022 } else if (p
>= 40 && p
<= 47) {
3025 } else if (p
>= 90 && p
<= 97) {
3029 } else if (p
>= 100 && p
<= 107) {
3033 } else if (p
=== 0) {
3035 flags
= this.defAttr
>> 18;
3036 fg
= (this.defAttr
>> 9) & 0x1ff;
3037 bg
= this.defAttr
& 0x1ff;
3041 } else if (p
=== 1) {
3044 } else if (p
=== 4) {
3047 } else if (p
=== 5) {
3050 } else if (p
=== 7) {
3051 // inverse and positive
3052 // test with: echo -e '\e[31m\e[42mhello\e[7mworld\e[27mhi\e[m'
3054 } else if (p
=== 8) {
3057 } else if (p
=== 22) {
3060 } else if (p
=== 24) {
3063 } else if (p
=== 25) {
3066 } else if (p
=== 27) {
3069 } else if (p
=== 28) {
3072 } else if (p
=== 39) {
3074 fg
= (this.defAttr
>> 9) & 0x1ff;
3075 } else if (p
=== 49) {
3077 bg
= this.defAttr
& 0x1ff;
3078 } else if (p
=== 38) {
3080 if (params
[i
+ 1] === 2) {
3084 params
[i
+ 1] & 0xff,
3085 params
[i
+ 2] & 0xff);
3086 if (fg
=== -1) fg
= 0x1ff;
3088 } else if (params
[i
+ 1] === 5) {
3090 p
= params
[i
] & 0xff;
3093 } else if (p
=== 48) {
3095 if (params
[i
+ 1] === 2) {
3099 params
[i
+ 1] & 0xff,
3100 params
[i
+ 2] & 0xff);
3101 if (bg
=== -1) bg
= 0x1ff;
3103 } else if (params
[i
+ 1] === 5) {
3105 p
= params
[i
] & 0xff;
3108 } else if (p
=== 100) {
3110 fg
= (this.defAttr
>> 9) & 0x1ff;
3111 bg
= this.defAttr
& 0x1ff;
3113 this.error('Unknown SGR attribute: %d.', p
);
3117 this.curAttr
= (flags
<< 18) | (fg
<< 9) | bg
;
3120 // CSI Ps n Device Status Report (DSR).
3121 // Ps = 5 -> Status Report. Result (``OK'') is
3123 // Ps = 6 -> Report Cursor Position (CPR) [row;column].
3127 // Device Status Report (DSR, DEC-specific).
3128 // Ps = 6 -> Report Cursor Position (CPR) [row;column] as CSI
3129 // ? r ; c R (assumes page is zero).
3130 // Ps = 1 5 -> Report Printer status as CSI ? 1 0 n (ready).
3131 // or CSI ? 1 1 n (not ready).
3132 // Ps = 2 5 -> Report UDK status as CSI ? 2 0 n (unlocked)
3133 // or CSI ? 2 1 n (locked).
3134 // Ps = 2 6 -> Report Keyboard status as
3135 // CSI ? 2 7 ; 1 ; 0 ; 0 n (North American).
3136 // The last two parameters apply to VT400 & up, and denote key-
3137 // board ready and LK01 respectively.
3138 // Ps = 5 3 -> Report Locator status as
3139 // CSI ? 5 3 n Locator available, if compiled-in, or
3140 // CSI ? 5 0 n No Locator, if not.
3141 Terminal
.prototype.deviceStatus = function(params
) {
3143 switch (params
[0]) {
3146 this.send('\x1b[0n');
3157 } else if (this.prefix
=== '?') {
3158 // modern xterm doesnt seem to
3159 // respond to any of these except ?6, 6, and 5
3160 switch (params
[0]) {
3171 // this.send('\x1b[?11n');
3174 // dont support user defined keys
3175 // this.send('\x1b[?21n');
3178 // north american keyboard
3179 // this.send('\x1b[?27;1;0;0n');
3182 // no dec locator/mouse
3183 // this.send('\x1b[?50n');
3194 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
3195 Terminal
.prototype.insertChars = function(params
) {
3196 var param
, row
, j
, ch
;
3199 if (param
< 1) param
= 1;
3201 row
= this.y
+ this.ybase
;
3203 ch
= [this.eraseAttr(), ' ']; // xterm
3205 while (param
-- && j
< this.cols
) {
3206 this.lines
[row
].splice(j
++, 0, ch
);
3207 this.lines
[row
].pop();
3212 // Cursor Next Line Ps Times (default = 1) (CNL).
3213 // same as CSI Ps B ?
3214 Terminal
.prototype.cursorNextLine = function(params
) {
3215 var param
= params
[0];
3216 if (param
< 1) param
= 1;
3218 if (this.y
>= this.rows
) {
3219 this.y
= this.rows
- 1;
3225 // Cursor Preceding Line Ps Times (default = 1) (CNL).
3227 Terminal
.prototype.cursorPrecedingLine = function(params
) {
3228 var param
= params
[0];
3229 if (param
< 1) param
= 1;
3231 if (this.y
< 0) this.y
= 0;
3236 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
3237 Terminal
.prototype.cursorCharAbsolute = function(params
) {
3238 var param
= params
[0];
3239 if (param
< 1) param
= 1;
3244 // Insert Ps Line(s) (default = 1) (IL).
3245 Terminal
.prototype.insertLines = function(params
) {
3249 if (param
< 1) param
= 1;
3250 row
= this.y
+ this.ybase
;
3252 j
= this.rows
- 1 - this.scrollBottom
;
3253 j
= this.rows
- 1 + this.ybase
- j
+ 1;
3256 // test: echo -e '\e[44m\e[1L\e[0m'
3257 // blankLine(true) - xterm/linux behavior
3258 this.lines
.splice(row
, 0, this.blankLine(true));
3259 this.lines
.splice(j
, 1);
3263 this.updateRange(this.y
);
3264 this.updateRange(this.scrollBottom
);
3268 // Delete Ps Line(s) (default = 1) (DL).
3269 Terminal
.prototype.deleteLines = function(params
) {
3273 if (param
< 1) param
= 1;
3274 row
= this.y
+ this.ybase
;
3276 j
= this.rows
- 1 - this.scrollBottom
;
3277 j
= this.rows
- 1 + this.ybase
- j
;
3280 // test: echo -e '\e[44m\e[1M\e[0m'
3281 // blankLine(true) - xterm/linux behavior
3282 this.lines
.splice(j
+ 1, 0, this.blankLine(true));
3283 this.lines
.splice(row
, 1);
3287 this.updateRange(this.y
);
3288 this.updateRange(this.scrollBottom
);
3292 // Delete Ps Character(s) (default = 1) (DCH).
3293 Terminal
.prototype.deleteChars = function(params
) {
3297 if (param
< 1) param
= 1;
3299 row
= this.y
+ this.ybase
;
3300 ch
= [this.eraseAttr(), ' ']; // xterm
3303 this.lines
[row
].splice(this.x
, 1);
3304 this.lines
[row
].push(ch
);
3309 // Erase Ps Character(s) (default = 1) (ECH).
3310 Terminal
.prototype.eraseChars = function(params
) {
3311 var param
, row
, j
, ch
;
3314 if (param
< 1) param
= 1;
3316 row
= this.y
+ this.ybase
;
3318 ch
= [this.eraseAttr(), ' ']; // xterm
3320 while (param
-- && j
< this.cols
) {
3321 this.lines
[row
][j
++] = ch
;
3325 // CSI Pm ` Character Position Absolute
3326 // [column] (default = [row,1]) (HPA).
3327 Terminal
.prototype.charPosAbsolute = function(params
) {
3328 var param
= params
[0];
3329 if (param
< 1) param
= 1;
3331 if (this.x
>= this.cols
) {
3332 this.x
= this.cols
- 1;
3337 // Horizontal Position Relative
3339 Terminal
.prototype.HPositionRelative = function(params
) {
3340 var param
= params
[0];
3341 if (param
< 1) param
= 1;
3343 if (this.x
>= this.cols
) {
3344 this.x
= this.cols
- 1;
3348 // CSI Ps c Send Device Attributes (Primary DA).
3349 // Ps = 0 or omitted -> request attributes from terminal. The
3350 // response depends on the decTerminalID resource setting.
3351 // -> CSI ? 1 ; 2 c (``VT100 with Advanced Video Option'')
3352 // -> CSI ? 1 ; 0 c (``VT101 with No Options'')
3353 // -> CSI ? 6 c (``VT102'')
3354 // -> CSI ? 6 0 ; 1 ; 2 ; 6 ; 8 ; 9 ; 1 5 ; c (``VT220'')
3355 // The VT100-style response parameters do not mean anything by
3356 // themselves. VT220 parameters do, telling the host what fea-
3357 // tures the terminal supports:
3358 // Ps = 1 -> 132-columns.
3359 // Ps = 2 -> Printer.
3360 // Ps = 6 -> Selective erase.
3361 // Ps = 8 -> User-defined keys.
3362 // Ps = 9 -> National replacement character sets.
3363 // Ps = 1 5 -> Technical characters.
3364 // Ps = 2 2 -> ANSI color, e.g., VT525.
3365 // Ps = 2 9 -> ANSI text locator (i.e., DEC Locator mode).
3367 // Send Device Attributes (Secondary DA).
3368 // Ps = 0 or omitted -> request the terminal's identification
3369 // code. The response depends on the decTerminalID resource set-
3370 // ting. It should apply only to VT220 and up, but xterm extends
3372 // -> CSI > Pp ; Pv ; Pc c
3373 // where Pp denotes the terminal type
3374 // Pp = 0 -> ``VT100''.
3375 // Pp = 1 -> ``VT220''.
3376 // and Pv is the firmware version (for xterm, this was originally
3377 // the XFree86 patch number, starting with 95). In a DEC termi-
3378 // nal, Pc indicates the ROM cartridge registration number and is
3380 // More information:
3381 // xterm/charproc.c - line 2012, for more information.
3382 // vim responds with ^[[?0c or ^[[?1c after the terminal's response (?)
3383 Terminal
.prototype.sendDeviceAttributes = function(params
) {
3384 if (params
[0] > 0) return;
3387 if (this.is('xterm')
3388 || this.is('rxvt-unicode')
3389 || this.is('screen')) {
3390 this.send('\x1b[?1;2c');
3391 } else if (this.is('linux')) {
3392 this.send('\x1b[?6c');
3394 } else if (this.prefix
=== '>') {
3396 // seem to spit this
3397 // out around ~370 times (?).
3398 if (this.is('xterm')) {
3399 this.send('\x1b[>0;276;0c');
3400 } else if (this.is('rxvt-unicode')) {
3401 this.send('\x1b[>85;95;0c');
3402 } else if (this.is('linux')) {
3403 // not supported by linux console.
3404 // linux console echoes parameters.
3405 this.send(params
[0] + 'c');
3406 } else if (this.is('screen')) {
3407 this.send('\x1b[>83;40003;0c');
3413 // Line Position Absolute [row] (default = [1,column]) (VPA).
3414 Terminal
.prototype.linePosAbsolute = function(params
) {
3415 var param
= params
[0];
3416 if (param
< 1) param
= 1;
3418 if (this.y
>= this.rows
) {
3419 this.y
= this.rows
- 1;
3423 // 145 65 e * VPR - Vertical Position Relative
3425 Terminal
.prototype.VPositionRelative = function(params
) {
3426 var param
= params
[0];
3427 if (param
< 1) param
= 1;
3429 if (this.y
>= this.rows
) {
3430 this.y
= this.rows
- 1;
3435 // Horizontal and Vertical Position [row;column] (default =
3437 Terminal
.prototype.HVPosition = function(params
) {
3438 if (params
[0] < 1) params
[0] = 1;
3439 if (params
[1] < 1) params
[1] = 1;
3441 this.y
= params
[0] - 1;
3442 if (this.y
>= this.rows
) {
3443 this.y
= this.rows
- 1;
3446 this.x
= params
[1] - 1;
3447 if (this.x
>= this.cols
) {
3448 this.x
= this.cols
- 1;
3452 // CSI Pm h Set Mode (SM).
3453 // Ps = 2 -> Keyboard Action Mode (AM).
3454 // Ps = 4 -> Insert Mode (IRM).
3455 // Ps = 1 2 -> Send/receive (SRM).
3456 // Ps = 2 0 -> Automatic Newline (LNM).
3458 // DEC Private Mode Set (DECSET).
3459 // Ps = 1 -> Application Cursor Keys (DECCKM).
3460 // Ps = 2 -> Designate USASCII for character sets G0-G3
3461 // (DECANM), and set VT100 mode.
3462 // Ps = 3 -> 132 Column Mode (DECCOLM).
3463 // Ps = 4 -> Smooth (Slow) Scroll (DECSCLM).
3464 // Ps = 5 -> Reverse Video (DECSCNM).
3465 // Ps = 6 -> Origin Mode (DECOM).
3466 // Ps = 7 -> Wraparound Mode (DECAWM).
3467 // Ps = 8 -> Auto-repeat Keys (DECARM).
3468 // Ps = 9 -> Send Mouse X & Y on button press. See the sec-
3469 // tion Mouse Tracking.
3470 // Ps = 1 0 -> Show toolbar (rxvt).
3471 // Ps = 1 2 -> Start Blinking Cursor (att610).
3472 // Ps = 1 8 -> Print form feed (DECPFF).
3473 // Ps = 1 9 -> Set print extent to full screen (DECPEX).
3474 // Ps = 2 5 -> Show Cursor (DECTCEM).
3475 // Ps = 3 0 -> Show scrollbar (rxvt).
3476 // Ps = 3 5 -> Enable font-shifting functions (rxvt).
3477 // Ps = 3 8 -> Enter Tektronix Mode (DECTEK).
3478 // Ps = 4 0 -> Allow 80 -> 132 Mode.
3479 // Ps = 4 1 -> more(1) fix (see curses resource).
3480 // Ps = 4 2 -> Enable Nation Replacement Character sets (DECN-
3482 // Ps = 4 4 -> Turn On Margin Bell.
3483 // Ps = 4 5 -> Reverse-wraparound Mode.
3484 // Ps = 4 6 -> Start Logging. This is normally disabled by a
3485 // compile-time option.
3486 // Ps = 4 7 -> Use Alternate Screen Buffer. (This may be dis-
3487 // abled by the titeInhibit resource).
3488 // Ps = 6 6 -> Application keypad (DECNKM).
3489 // Ps = 6 7 -> Backarrow key sends backspace (DECBKM).
3490 // Ps = 1 0 0 0 -> Send Mouse X & Y on button press and
3491 // release. See the section Mouse Tracking.
3492 // Ps = 1 0 0 1 -> Use Hilite Mouse Tracking.
3493 // Ps = 1 0 0 2 -> Use Cell Motion Mouse Tracking.
3494 // Ps = 1 0 0 3 -> Use All Motion Mouse Tracking.
3495 // Ps = 1 0 0 4 -> Send FocusIn/FocusOut events.
3496 // Ps = 1 0 0 5 -> Enable Extended Mouse Mode.
3497 // Ps = 1 0 1 0 -> Scroll to bottom on tty output (rxvt).
3498 // Ps = 1 0 1 1 -> Scroll to bottom on key press (rxvt).
3499 // Ps = 1 0 3 4 -> Interpret "meta" key, sets eighth bit.
3500 // (enables the eightBitInput resource).
3501 // Ps = 1 0 3 5 -> Enable special modifiers for Alt and Num-
3502 // Lock keys. (This enables the numLock resource).
3503 // Ps = 1 0 3 6 -> Send ESC when Meta modifies a key. (This
3504 // enables the metaSendsEscape resource).
3505 // Ps = 1 0 3 7 -> Send DEL from the editing-keypad Delete
3507 // Ps = 1 0 3 9 -> Send ESC when Alt modifies a key. (This
3508 // enables the altSendsEscape resource).
3509 // Ps = 1 0 4 0 -> Keep selection even if not highlighted.
3510 // (This enables the keepSelection resource).
3511 // Ps = 1 0 4 1 -> Use the CLIPBOARD selection. (This enables
3512 // the selectToClipboard resource).
3513 // Ps = 1 0 4 2 -> Enable Urgency window manager hint when
3514 // Control-G is received. (This enables the bellIsUrgent
3516 // Ps = 1 0 4 3 -> Enable raising of the window when Control-G
3517 // is received. (enables the popOnBell resource).
3518 // Ps = 1 0 4 7 -> Use Alternate Screen Buffer. (This may be
3519 // disabled by the titeInhibit resource).
3520 // Ps = 1 0 4 8 -> Save cursor as in DECSC. (This may be dis-
3521 // abled by the titeInhibit resource).
3522 // Ps = 1 0 4 9 -> Save cursor as in DECSC and use Alternate
3523 // Screen Buffer, clearing it first. (This may be disabled by
3524 // the titeInhibit resource). This combines the effects of the 1
3525 // 0 4 7 and 1 0 4 8 modes. Use this with terminfo-based
3526 // applications rather than the 4 7 mode.
3527 // Ps = 1 0 5 0 -> Set terminfo/termcap function-key mode.
3528 // Ps = 1 0 5 1 -> Set Sun function-key mode.
3529 // Ps = 1 0 5 2 -> Set HP function-key mode.
3530 // Ps = 1 0 5 3 -> Set SCO function-key mode.
3531 // Ps = 1 0 6 0 -> Set legacy keyboard emulation (X11R6).
3532 // Ps = 1 0 6 1 -> Set VT220 keyboard emulation.
3533 // Ps = 2 0 0 4 -> Set bracketed paste mode.
3535 // http://vt100.net/docs/vt220-rm/chapter4.html
3536 Terminal
.prototype.setMode = function(params
) {
3537 if (typeof params
=== 'object') {
3538 var l
= params
.length
3541 for (; i
< l
; i
++) {
3542 this.setMode(params
[i
]);
3551 this.insertMode
= true;
3554 //this.convertEol = true;
3557 } else if (this.prefix
=== '?') {
3560 this.applicationCursor
= true;
3563 this.setgCharset(0, Terminal
.charsets
.US
);
3564 this.setgCharset(1, Terminal
.charsets
.US
);
3565 this.setgCharset(2, Terminal
.charsets
.US
);
3566 this.setgCharset(3, Terminal
.charsets
.US
);
3567 // set VT100 mode here
3569 case 3: // 132 col mode
3570 this.savedCols
= this.cols
;
3571 this.resize(132, this.rows
);
3574 this.originMode
= true;
3577 this.wraparoundMode
= true;
3580 // this.cursorBlink = true;
3583 this.log('Serial port requested application keypad.');
3584 this.applicationKeypad
= true;
3586 case 9: // X10 Mouse
3587 // no release, no motion, no wheel, no modifiers.
3588 case 1000: // vt200 mouse
3590 // no modifiers, except control on the wheel.
3591 case 1002: // button event mouse
3592 case 1003: // any event mouse
3593 // any event - sends motion events,
3594 // even if there is no button held down.
3595 this.x10Mouse
= params
=== 9;
3596 this.vt200Mouse
= params
=== 1000;
3597 this.normalMouse
= params
> 1000;
3598 this.mouseEvents
= true;
3599 this.element
.style
.cursor
= 'default';
3600 this.log('Binding to mouse events.');
3602 case 1004: // send focusin/focusout events
3605 this.sendFocus
= true;
3607 case 1005: // utf8 ext mode mouse
3608 this.utfMouse
= true;
3609 // for wide terminals
3610 // simply encodes large values as utf8 characters
3612 case 1006: // sgr ext mode mouse
3613 this.sgrMouse
= true;
3614 // for wide terminals
3615 // does not add 32 to fields
3616 // press: ^[[<b;x;yM
3617 // release: ^[[<b;x;ym
3619 case 1015: // urxvt ext mode mouse
3620 this.urxvtMouse
= true;
3621 // for wide terminals
3622 // numbers for fields
3624 // motion: ^[[b;x;yT
3626 case 25: // show cursor
3627 this.cursorHidden
= false;
3629 case 1049: // alt screen buffer cursor
3630 //this.saveCursor();
3632 case 47: // alt screen buffer
3633 case 1047: // alt screen buffer
3641 scrollTop
: this.scrollTop
,
3642 scrollBottom
: this.scrollBottom
,
3644 // XXX save charset(s) here?
3645 // charset: this.charset,
3646 // glevel: this.glevel,
3647 // charsets: this.charsets
3650 this.normal
= normal
;
3658 // CSI Pm l Reset Mode (RM).
3659 // Ps = 2 -> Keyboard Action Mode (AM).
3660 // Ps = 4 -> Replace Mode (IRM).
3661 // Ps = 1 2 -> Send/receive (SRM).
3662 // Ps = 2 0 -> Normal Linefeed (LNM).
3664 // DEC Private Mode Reset (DECRST).
3665 // Ps = 1 -> Normal Cursor Keys (DECCKM).
3666 // Ps = 2 -> Designate VT52 mode (DECANM).
3667 // Ps = 3 -> 80 Column Mode (DECCOLM).
3668 // Ps = 4 -> Jump (Fast) Scroll (DECSCLM).
3669 // Ps = 5 -> Normal Video (DECSCNM).
3670 // Ps = 6 -> Normal Cursor Mode (DECOM).
3671 // Ps = 7 -> No Wraparound Mode (DECAWM).
3672 // Ps = 8 -> No Auto-repeat Keys (DECARM).
3673 // Ps = 9 -> Don't send Mouse X & Y on button press.
3674 // Ps = 1 0 -> Hide toolbar (rxvt).
3675 // Ps = 1 2 -> Stop Blinking Cursor (att610).
3676 // Ps = 1 8 -> Don't print form feed (DECPFF).
3677 // Ps = 1 9 -> Limit print to scrolling region (DECPEX).
3678 // Ps = 2 5 -> Hide Cursor (DECTCEM).
3679 // Ps = 3 0 -> Don't show scrollbar (rxvt).
3680 // Ps = 3 5 -> Disable font-shifting functions (rxvt).
3681 // Ps = 4 0 -> Disallow 80 -> 132 Mode.
3682 // Ps = 4 1 -> No more(1) fix (see curses resource).
3683 // Ps = 4 2 -> Disable Nation Replacement Character sets (DEC-
3685 // Ps = 4 4 -> Turn Off Margin Bell.
3686 // Ps = 4 5 -> No Reverse-wraparound Mode.
3687 // Ps = 4 6 -> Stop Logging. (This is normally disabled by a
3688 // compile-time option).
3689 // Ps = 4 7 -> Use Normal Screen Buffer.
3690 // Ps = 6 6 -> Numeric keypad (DECNKM).
3691 // Ps = 6 7 -> Backarrow key sends delete (DECBKM).
3692 // Ps = 1 0 0 0 -> Don't send Mouse X & Y on button press and
3693 // release. See the section Mouse Tracking.
3694 // Ps = 1 0 0 1 -> Don't use Hilite Mouse Tracking.
3695 // Ps = 1 0 0 2 -> Don't use Cell Motion Mouse Tracking.
3696 // Ps = 1 0 0 3 -> Don't use All Motion Mouse Tracking.
3697 // Ps = 1 0 0 4 -> Don't send FocusIn/FocusOut events.
3698 // Ps = 1 0 0 5 -> Disable Extended Mouse Mode.
3699 // Ps = 1 0 1 0 -> Don't scroll to bottom on tty output
3701 // Ps = 1 0 1 1 -> Don't scroll to bottom on key press (rxvt).
3702 // Ps = 1 0 3 4 -> Don't interpret "meta" key. (This disables
3703 // the eightBitInput resource).
3704 // Ps = 1 0 3 5 -> Disable special modifiers for Alt and Num-
3705 // Lock keys. (This disables the numLock resource).
3706 // Ps = 1 0 3 6 -> Don't send ESC when Meta modifies a key.
3707 // (This disables the metaSendsEscape resource).
3708 // Ps = 1 0 3 7 -> Send VT220 Remove from the editing-keypad
3710 // Ps = 1 0 3 9 -> Don't send ESC when Alt modifies a key.
3711 // (This disables the altSendsEscape resource).
3712 // Ps = 1 0 4 0 -> Do not keep selection when not highlighted.
3713 // (This disables the keepSelection resource).
3714 // Ps = 1 0 4 1 -> Use the PRIMARY selection. (This disables
3715 // the selectToClipboard resource).
3716 // Ps = 1 0 4 2 -> Disable Urgency window manager hint when
3717 // Control-G is received. (This disables the bellIsUrgent
3719 // Ps = 1 0 4 3 -> Disable raising of the window when Control-
3720 // G is received. (This disables the popOnBell resource).
3721 // Ps = 1 0 4 7 -> Use Normal Screen Buffer, clearing screen
3722 // first if in the Alternate Screen. (This may be disabled by
3723 // the titeInhibit resource).
3724 // Ps = 1 0 4 8 -> Restore cursor as in DECRC. (This may be
3725 // disabled by the titeInhibit resource).
3726 // Ps = 1 0 4 9 -> Use Normal Screen Buffer and restore cursor
3727 // as in DECRC. (This may be disabled by the titeInhibit
3728 // resource). This combines the effects of the 1 0 4 7 and 1 0
3729 // 4 8 modes. Use this with terminfo-based applications rather
3730 // than the 4 7 mode.
3731 // Ps = 1 0 5 0 -> Reset terminfo/termcap function-key mode.
3732 // Ps = 1 0 5 1 -> Reset Sun function-key mode.
3733 // Ps = 1 0 5 2 -> Reset HP function-key mode.
3734 // Ps = 1 0 5 3 -> Reset SCO function-key mode.
3735 // Ps = 1 0 6 0 -> Reset legacy keyboard emulation (X11R6).
3736 // Ps = 1 0 6 1 -> Reset keyboard emulation to Sun/PC style.
3737 // Ps = 2 0 0 4 -> Reset bracketed paste mode.
3738 Terminal
.prototype.resetMode = function(params
) {
3739 if (typeof params
=== 'object') {
3740 var l
= params
.length
3743 for (; i
< l
; i
++) {
3744 this.resetMode(params
[i
]);
3753 this.insertMode
= false;
3756 //this.convertEol = false;
3759 } else if (this.prefix
=== '?') {
3762 this.applicationCursor
= false;
3765 if (this.cols
=== 132 && this.savedCols
) {
3766 this.resize(this.savedCols
, this.rows
);
3768 delete this.savedCols
;
3771 this.originMode
= false;
3774 this.wraparoundMode
= false;
3777 // this.cursorBlink = false;
3780 this.log('Switching back to normal keypad.');
3781 this.applicationKeypad
= false;
3783 case 9: // X10 Mouse
3784 case 1000: // vt200 mouse
3785 case 1002: // button event mouse
3786 case 1003: // any event mouse
3787 this.x10Mouse
= false;
3788 this.vt200Mouse
= false;
3789 this.normalMouse
= false;
3790 this.mouseEvents
= false;
3791 this.element
.style
.cursor
= '';
3793 case 1004: // send focusin/focusout events
3794 this.sendFocus
= false;
3796 case 1005: // utf8 ext mode mouse
3797 this.utfMouse
= false;
3799 case 1006: // sgr ext mode mouse
3800 this.sgrMouse
= false;
3802 case 1015: // urxvt ext mode mouse
3803 this.urxvtMouse
= false;
3805 case 25: // hide cursor
3806 this.cursorHidden
= true;
3808 case 1049: // alt screen buffer cursor
3810 case 47: // normal screen buffer
3811 case 1047: // normal screen buffer - clearing it first
3813 this.lines
= this.normal
.lines
;
3814 this.ybase
= this.normal
.ybase
;
3815 this.ydisp
= this.normal
.ydisp
;
3816 this.x
= this.normal
.x
;
3817 this.y
= this.normal
.y
;
3818 this.scrollTop
= this.normal
.scrollTop
;
3819 this.scrollBottom
= this.normal
.scrollBottom
;
3820 this.tabs
= this.normal
.tabs
;
3822 // if (params === 1049) {
3823 // this.x = this.savedX;
3824 // this.y = this.savedY;
3826 this.refresh(0, this.rows
- 1);
3835 // Set Scrolling Region [top;bottom] (default = full size of win-
3838 Terminal
.prototype.setScrollRegion = function(params
) {
3839 if (this.prefix
) return;
3840 this.scrollTop
= (params
[0] || 1) - 1;
3841 this.scrollBottom
= (params
[1] || this.rows
) - 1;
3847 // Save cursor (ANSI.SYS).
3848 Terminal
.prototype.saveCursor = function(params
) {
3849 this.savedX
= this.x
;
3850 this.savedY
= this.y
;
3854 // Restore cursor (ANSI.SYS).
3855 Terminal
.prototype.restoreCursor = function(params
) {
3856 this.x
= this.savedX
|| 0;
3857 this.y
= this.savedY
|| 0;
3865 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
3866 Terminal
.prototype.cursorForwardTab = function(params
) {
3867 var param
= params
[0] || 1;
3869 this.x
= this.nextStop();
3873 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
3874 Terminal
.prototype.scrollUp = function(params
) {
3875 var param
= params
[0] || 1;
3877 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
3878 this.lines
.splice(this.ybase
+ this.scrollBottom
, 0, this.blankLine());
3881 this.updateRange(this.scrollTop
);
3882 this.updateRange(this.scrollBottom
);
3885 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
3886 Terminal
.prototype.scrollDown = function(params
) {
3887 var param
= params
[0] || 1;
3889 this.lines
.splice(this.ybase
+ this.scrollBottom
, 1);
3890 this.lines
.splice(this.ybase
+ this.scrollTop
, 0, this.blankLine());
3893 this.updateRange(this.scrollTop
);
3894 this.updateRange(this.scrollBottom
);
3897 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
3898 // Initiate highlight mouse tracking. Parameters are
3899 // [func;startx;starty;firstrow;lastrow]. See the section Mouse
3901 Terminal
.prototype.initMouseTracking = function(params
) {
3902 // Relevant: DECSET 1001
3906 // Reset one or more features of the title modes to the default
3907 // value. Normally, "reset" disables the feature. It is possi-
3908 // ble to disable the ability to reset features by compiling a
3909 // different default for the title modes into xterm.
3910 // Ps = 0 -> Do not set window/icon labels using hexadecimal.
3911 // Ps = 1 -> Do not query window/icon labels using hexadeci-
3913 // Ps = 2 -> Do not set window/icon labels using UTF-8.
3914 // Ps = 3 -> Do not query window/icon labels using UTF-8.
3915 // (See discussion of "Title Modes").
3916 Terminal
.prototype.resetTitleModes = function(params
) {
3920 // CSI Ps Z Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
3921 Terminal
.prototype.cursorBackwardTab = function(params
) {
3922 var param
= params
[0] || 1;
3924 this.x
= this.prevStop();
3928 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
3929 Terminal
.prototype.repeatPrecedingCharacter = function(params
) {
3930 var param
= params
[0] || 1
3931 , line
= this.lines
[this.ybase
+ this.y
]
3932 , ch
= line
[this.x
- 1] || [this.defAttr
, ' '];
3934 while (param
--) line
[this.x
++] = ch
;
3937 // CSI Ps g Tab Clear (TBC).
3938 // Ps = 0 -> Clear Current Column (default).
3939 // Ps = 3 -> Clear All.
3941 // Ps = 2 -> Clear Stops on Line.
3942 // http://vt100.net/annarbor/aaa-ug/section6.html
3943 Terminal
.prototype.tabClear = function(params
) {
3944 var param
= params
[0];
3946 delete this.tabs
[this.x
];
3947 } else if (param
=== 3) {
3952 // CSI Pm i Media Copy (MC).
3953 // Ps = 0 -> Print screen (default).
3954 // Ps = 4 -> Turn off printer controller mode.
3955 // Ps = 5 -> Turn on printer controller mode.
3957 // Media Copy (MC, DEC-specific).
3958 // Ps = 1 -> Print line containing cursor.
3959 // Ps = 4 -> Turn off autoprint mode.
3960 // Ps = 5 -> Turn on autoprint mode.
3961 // Ps = 1 0 -> Print composed display, ignores DECPEX.
3962 // Ps = 1 1 -> Print all pages.
3963 Terminal
.prototype.mediaCopy = function(params
) {
3968 // Set or reset resource-values used by xterm to decide whether
3969 // to construct escape sequences holding information about the
3970 // modifiers pressed with a given key. The first parameter iden-
3971 // tifies the resource to set/reset. The second parameter is the
3972 // value to assign to the resource. If the second parameter is
3973 // omitted, the resource is reset to its initial value.
3974 // Ps = 1 -> modifyCursorKeys.
3975 // Ps = 2 -> modifyFunctionKeys.
3976 // Ps = 4 -> modifyOtherKeys.
3977 // If no parameters are given, all resources are reset to their
3979 Terminal
.prototype.setResources = function(params
) {
3984 // Disable modifiers which may be enabled via the CSI > Ps; Ps m
3985 // sequence. This corresponds to a resource value of "-1", which
3986 // cannot be set with the other sequence. The parameter identi-
3987 // fies the resource to be disabled:
3988 // Ps = 1 -> modifyCursorKeys.
3989 // Ps = 2 -> modifyFunctionKeys.
3990 // Ps = 4 -> modifyOtherKeys.
3991 // If the parameter is omitted, modifyFunctionKeys is disabled.
3992 // When modifyFunctionKeys is disabled, xterm uses the modifier
3993 // keys to make an extended sequence of functions rather than
3994 // adding a parameter to each function key to denote the modi-
3996 Terminal
.prototype.disableModifiers = function(params
) {
4001 // Set resource value pointerMode. This is used by xterm to
4002 // decide whether to hide the pointer cursor as the user types.
4003 // Valid values for the parameter:
4004 // Ps = 0 -> never hide the pointer.
4005 // Ps = 1 -> hide if the mouse tracking mode is not enabled.
4006 // Ps = 2 -> always hide the pointer. If no parameter is
4007 // given, xterm uses the default, which is 1 .
4008 Terminal
.prototype.setPointerMode = function(params
) {
4012 // CSI ! p Soft terminal reset (DECSTR).
4013 // http://vt100.net/docs/vt220-rm/table4-10.html
4014 Terminal
.prototype.softReset = function(params
) {
4015 this.cursorHidden
= false;
4016 this.insertMode
= false;
4017 this.originMode
= false;
4018 this.wraparoundMode
= false; // autowrap
4019 this.applicationKeypad
= false; // ?
4020 this.applicationCursor
= false;
4022 this.scrollBottom
= this.rows
- 1;
4023 this.curAttr
= this.defAttr
;
4024 this.x
= this.y
= 0; // ?
4025 this.charset
= null;
4026 this.glevel
= 0; // ??
4027 this.charsets
= [null]; // ??
4031 // Request ANSI mode (DECRQM). For VT300 and up, reply is
4033 // where Ps is the mode number as in RM, and Pm is the mode
4035 // 0 - not recognized
4038 // 3 - permanently set
4039 // 4 - permanently reset
4040 Terminal
.prototype.requestAnsiMode = function(params
) {
4045 // Request DEC private mode (DECRQM). For VT300 and up, reply is
4047 // where Ps is the mode number as in DECSET, Pm is the mode value
4048 // as in the ANSI DECRQM.
4049 Terminal
.prototype.requestPrivateMode = function(params
) {
4054 // Set conformance level (DECSCL). Valid values for the first
4056 // Ps = 6 1 -> VT100.
4057 // Ps = 6 2 -> VT200.
4058 // Ps = 6 3 -> VT300.
4059 // Valid values for the second parameter:
4060 // Ps = 0 -> 8-bit controls.
4061 // Ps = 1 -> 7-bit controls (always set for VT100).
4062 // Ps = 2 -> 8-bit controls.
4063 Terminal
.prototype.setConformanceLevel = function(params
) {
4067 // CSI Ps q Load LEDs (DECLL).
4068 // Ps = 0 -> Clear all LEDS (default).
4069 // Ps = 1 -> Light Num Lock.
4070 // Ps = 2 -> Light Caps Lock.
4071 // Ps = 3 -> Light Scroll Lock.
4072 // Ps = 2 1 -> Extinguish Num Lock.
4073 // Ps = 2 2 -> Extinguish Caps Lock.
4074 // Ps = 2 3 -> Extinguish Scroll Lock.
4075 Terminal
.prototype.loadLEDs = function(params
) {
4080 // Set cursor style (DECSCUSR, VT520).
4081 // Ps = 0 -> blinking block.
4082 // Ps = 1 -> blinking block (default).
4083 // Ps = 2 -> steady block.
4084 // Ps = 3 -> blinking underline.
4085 // Ps = 4 -> steady underline.
4086 Terminal
.prototype.setCursorStyle = function(params
) {
4091 // Select character protection attribute (DECSCA). Valid values
4092 // for the parameter:
4093 // Ps = 0 -> DECSED and DECSEL can erase (default).
4094 // Ps = 1 -> DECSED and DECSEL cannot erase.
4095 // Ps = 2 -> DECSED and DECSEL can erase.
4096 Terminal
.prototype.setCharProtectionAttr = function(params
) {
4101 // Restore DEC Private Mode Values. The value of Ps previously
4102 // saved is restored. Ps values are the same as for DECSET.
4103 Terminal
.prototype.restorePrivateValues = function(params
) {
4107 // CSI Pt; Pl; Pb; Pr; Ps$ r
4108 // Change Attributes in Rectangular Area (DECCARA), VT400 and up.
4109 // Pt; Pl; Pb; Pr denotes the rectangle.
4110 // Ps denotes the SGR attributes to change: 0, 1, 4, 5, 7.
4111 // NOTE: xterm doesn't enable this code by default.
4112 Terminal
.prototype.setAttrInRectangle = function(params
) {
4122 for (; t
< b
+ 1; t
++) {
4123 line
= this.lines
[this.ybase
+ t
];
4124 for (i
= l
; i
< r
; i
++) {
4125 line
[i
] = [attr
, line
[i
][1]];
4130 this.updateRange(params
[0]);
4131 this.updateRange(params
[2]);
4135 // CSI Pc; Pt; Pl; Pb; Pr$ x
4136 // Fill Rectangular Area (DECFRA), VT420 and up.
4137 // Pc is the character to use.
4138 // Pt; Pl; Pb; Pr denotes the rectangle.
4139 // NOTE: xterm doesn't enable this code by default.
4140 Terminal
.prototype.fillRectangle = function(params
) {
4150 for (; t
< b
+ 1; t
++) {
4151 line
= this.lines
[this.ybase
+ t
];
4152 for (i
= l
; i
< r
; i
++) {
4153 line
[i
] = [line
[i
][0], String
.fromCharCode(ch
)];
4158 this.updateRange(params
[1]);
4159 this.updateRange(params
[3]);
4163 // Enable Locator Reporting (DECELR).
4164 // Valid values for the first parameter:
4165 // Ps = 0 -> Locator disabled (default).
4166 // Ps = 1 -> Locator enabled.
4167 // Ps = 2 -> Locator enabled for one report, then disabled.
4168 // The second parameter specifies the coordinate unit for locator
4170 // Valid values for the second parameter:
4171 // Pu = 0 <- or omitted -> default to character cells.
4172 // Pu = 1 <- device physical pixels.
4173 // Pu = 2 <- character cells.
4174 Terminal
.prototype.enableLocatorReporting = function(params
) {
4175 var val
= params
[0] > 0;
4176 //this.mouseEvents = val;
4177 //this.decLocator = val;
4180 // CSI Pt; Pl; Pb; Pr$ z
4181 // Erase Rectangular Area (DECERA), VT400 and up.
4182 // Pt; Pl; Pb; Pr denotes the rectangle.
4183 // NOTE: xterm doesn't enable this code by default.
4184 Terminal
.prototype.eraseRectangle = function(params
) {
4194 ch
= [this.eraseAttr(), ' ']; // xterm?
4196 for (; t
< b
+ 1; t
++) {
4197 line
= this.lines
[this.ybase
+ t
];
4198 for (i
= l
; i
< r
; i
++) {
4204 this.updateRange(params
[0]);
4205 this.updateRange(params
[2]);
4210 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
4211 // NOTE: xterm doesn't enable this code by default.
4212 Terminal
.prototype.insertColumns = function() {
4213 var param
= params
[0]
4214 , l
= this.ybase
+ this.rows
4215 , ch
= [this.eraseAttr(), ' '] // xterm?
4219 for (i
= this.ybase
; i
< l
; i
++) {
4220 this.lines
[i
].splice(this.x
+ 1, 0, ch
);
4221 this.lines
[i
].pop();
4230 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
4231 // NOTE: xterm doesn't enable this code by default.
4232 Terminal
.prototype.deleteColumns = function() {
4233 var param
= params
[0]
4234 , l
= this.ybase
+ this.rows
4235 , ch
= [this.eraseAttr(), ' '] // xterm?
4239 for (i
= this.ybase
; i
< l
; i
++) {
4240 this.lines
[i
].splice(this.x
, 1);
4241 this.lines
[i
].push(ch
);
4252 Terminal
.charsets
= {};
4254 // DEC Special Character and Line Drawing Set.
4255 // http://vt100.net/docs/vt102-ug/table5-13.html
4256 // A lot of curses apps use this if they see TERM=xterm.
4257 // testing: echo -e '\e(0a\e(B'
4258 // The xterm output sometimes seems to conflict with the
4259 // reference above. xterm seems in line with the reference
4260 // when running vttest however.
4261 // The table below now uses xterm's output from vttest.
4262 Terminal
.charsets
.SCLD
= { // (0
4263 '`': '\u25c6', // '◆'
4264 'a': '\u2592', // '▒'
4265 'b': '\u0009', // '\t'
4266 'c': '\u000c', // '\f'
4267 'd': '\u000d', // '\r'
4268 'e': '\u000a', // '\n'
4269 'f': '\u00b0', // '°'
4270 'g': '\u00b1', // '±'
4271 'h': '\u2424', // '\u2424' (NL)
4272 'i': '\u000b', // '\v'
4273 'j': '\u2518', // '┘'
4274 'k': '\u2510', // '┐'
4275 'l': '\u250c', // '┌'
4276 'm': '\u2514', // '└'
4277 'n': '\u253c', // '┼'
4278 'o': '\u23ba', // '⎺'
4279 'p': '\u23bb', // '⎻'
4280 'q': '\u2500', // '─'
4281 'r': '\u23bc', // '⎼'
4282 's': '\u23bd', // '⎽'
4283 't': '\u251c', // '├'
4284 'u': '\u2524', // '┤'
4285 'v': '\u2534', // '┴'
4286 'w': '\u252c', // '┬'
4287 'x': '\u2502', // '│'
4288 'y': '\u2264', // '≤'
4289 'z': '\u2265', // '≥'
4290 '{': '\u03c0', // 'π'
4291 '|': '\u2260', // '≠'
4292 '}': '\u00a3', // '£'
4293 '~': '\u00b7' // '·'
4296 Terminal
.charsets
.UK
= null; // (A
4297 Terminal
.charsets
.US
= null; // (B (USASCII)
4298 Terminal
.charsets
.Dutch
= null; // (4
4299 Terminal
.charsets
.Finnish
= null; // (C or (5
4300 Terminal
.charsets
.French
= null; // (R
4301 Terminal
.charsets
.FrenchCanadian
= null; // (Q
4302 Terminal
.charsets
.German
= null; // (K
4303 Terminal
.charsets
.Italian
= null; // (Y
4304 Terminal
.charsets
.NorwegianDanish
= null; // (E or (6
4305 Terminal
.charsets
.Spanish
= null; // (Z
4306 Terminal
.charsets
.Swedish
= null; // (H or (7
4307 Terminal
.charsets
.Swiss
= null; // (=
4308 Terminal
.charsets
.ISOLatin
= null; // /A
4314 function on(el
, type
, handler
, capture
) {
4315 if (!Array
.isArray(el
)) {
4318 el
.forEach(function (element
) {
4319 element
.addEventListener(type
, handler
, capture
|| false);
4323 function off(el
, type
, handler
, capture
) {
4324 el
.removeEventListener(type
, handler
, capture
|| false);
4327 function cancel(ev
, force
) {
4328 if (!this.cancelEvents
&& !force
) {
4331 ev
.preventDefault();
4332 ev
.stopPropagation();
4336 function inherits(child
, parent
) {
4338 this.constructor = child
;
4340 f
.prototype = parent
.prototype;
4341 child
.prototype = new f
;
4344 // if bold is broken, we can't
4345 // use it in the terminal.
4346 function isBoldBroken(document
) {
4347 var body
= document
.getElementsByTagName('body')[0];
4348 var el
= document
.createElement('span');
4349 el
.innerHTML
= 'hello world';
4350 body
.appendChild(el
);
4351 var w1
= el
.scrollWidth
;
4352 el
.style
.fontWeight
= 'bold';
4353 var w2
= el
.scrollWidth
;
4354 body
.removeChild(el
);
4358 var String
= this.String
;
4359 var setTimeout
= this.setTimeout
;
4360 var setInterval
= this.setInterval
;
4362 function indexOf(obj
, el
) {
4365 if (obj
[i
] === el
) return i
;
4370 function isWide(ch
) {
4371 if (ch
<= '\uff00') return false;
4372 return (ch
>= '\uff01' && ch
<= '\uffbe')
4373 || (ch
>= '\uffc2' && ch
<= '\uffc7')
4374 || (ch
>= '\uffca' && ch
<= '\uffcf')
4375 || (ch
>= '\uffd2' && ch
<= '\uffd7')
4376 || (ch
>= '\uffda' && ch
<= '\uffdc')
4377 || (ch
>= '\uffe0' && ch
<= '\uffe6')
4378 || (ch
>= '\uffe8' && ch
<= '\uffee');
4381 function matchColor(r1
, g1
, b1
) {
4382 var hash
= (r1
<< 16) | (g1
<< 8) | b1
;
4384 if (matchColor
._cache
[hash
] != null) {
4385 return matchColor
._cache
[hash
];
4388 var ldiff
= Infinity
4397 for (; i
< Terminal
.vcolors
.length
; i
++) {
4398 c
= Terminal
.vcolors
[i
];
4403 diff
= matchColor
.distance(r1
, g1
, b1
, r2
, g2
, b2
);
4416 return matchColor
._cache
[hash
] = li
;
4419 matchColor
._cache
= {};
4421 // http://stackoverflow.com/questions/1633828
4422 matchColor
.distance = function(r1
, g1
, b1
, r2
, g2
, b2
) {
4423 return Math
.pow(30 * (r1
- r2
), 2)
4424 + Math
.pow(59 * (g1
- g2
), 2)
4425 + Math
.pow(11 * (b1
- b2
), 2);
4428 function each(obj
, iter
, con
) {
4429 if (obj
.forEach
) return obj
.forEach(iter
, con
);
4430 for (var i
= 0; i
< obj
.length
; i
++) {
4431 iter
.call(con
, obj
[i
], i
, obj
);
4435 function keys(obj
) {
4436 if (Object
.keys
) return Object
.keys(obj
);
4439 if (Object
.prototype.hasOwnProperty
.call(obj
, key
)) {
4450 Terminal
.EventEmitter
= EventEmitter
;
4451 Terminal
.inherits
= inherits
;
4454 Terminal
.cancel
= cancel
;