2 * xterm.js: xterm, in the browser
3 * Originally forked from (with the author's permission):
4 * Fabrice Bellard's javascript vt100 for jslinux:
5 * http://bellard.org/jslinux/
6 * Copyright (c) 2011 Fabrice Bellard
7 * The original design remains. The terminal itself
8 * has been extended to include xterm CSI codes, among
13 import { CompositionHelper
} from './CompositionHelper.js';
14 import { EventEmitter
} from './EventEmitter.js';
15 import { Viewport
} from './Viewport.js';
16 import { rightClickHandler
, pasteHandler
, copyHandler
} from './handlers/Clipboard.js';
17 import * as Browser
from './utils/Browser';
18 import * as Keyboard
from './utils/Keyboard';
21 * Terminal Emulation References:
23 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.txt
24 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
25 * http://invisible-island.net/vttest/
26 * http://www.inwap.com/pdp10/ansicode.txt
27 * http://linux.die.net/man/4/console_codes
28 * http://linux.die.net/man/7/urxvt
31 // Let it work inside Node.js for automated testing purposes.
32 var document
= (typeof window
!= 'undefined') ? window
.document
: null;
37 var normal
= 0, escaped
= 1, csi
= 2, osc
= 3, charset
= 4, dcs
= 5, ignore
= 6;
44 * Creates a new `Terminal` object.
46 * @param {object} options An object containing a set of options, the available options are:
47 * - `cursorBlink` (boolean): Whether the terminal cursor blinks
48 * - `cols` (number): The number of columns of the terminal (horizontal size)
49 * - `rows` (number): The number of rows of the terminal (vertical size)
53 * @alias module:xterm/src/xterm
55 function Terminal(options
) {
58 if (!(this instanceof Terminal
)) {
59 return new Terminal(arguments
[0], arguments
[1], arguments
[2]);
62 self
.browser
= Browser
;
63 self
.cancel
= Terminal
.cancel
;
65 EventEmitter
.call(this);
67 if (typeof options
=== 'number') {
75 options
= options
|| {};
78 Object
.keys(Terminal
.defaults
).forEach(function(key
) {
79 if (options
[key
] == null) {
80 options
[key
] = Terminal
.options
[key
];
82 if (Terminal
[key
] !== Terminal
.defaults
[key
]) {
83 options
[key
] = Terminal
[key
];
86 self
[key
] = options
[key
];
89 if (options
.colors
.length
=== 8) {
90 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(8));
91 } else if (options
.colors
.length
=== 16) {
92 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(16));
93 } else if (options
.colors
.length
=== 10) {
94 options
.colors
= options
.colors
.slice(0, -2).concat(
95 Terminal
._colors
.slice(8, -2), options
.colors
.slice(-2));
96 } else if (options
.colors
.length
=== 18) {
97 options
.colors
= options
.colors
.concat(
98 Terminal
._colors
.slice(16, -2), options
.colors
.slice(-2));
100 this.colors
= options
.colors
;
102 this.options
= options
;
104 // this.context = options.context || window;
105 // this.document = options.document || document;
106 this.parent
= options
.body
|| options
.parent
|| (
107 document
? document
.getElementsByTagName('body')[0] : null
110 this.cols
= options
.cols
|| options
.geometry
[0];
111 this.rows
= options
.rows
|| options
.geometry
[1];
112 this.geometry
= [this.cols
, this.rows
];
114 if (options
.handler
) {
115 this.on('data', options
.handler
);
119 * The scroll position of the y cursor, ie. ybase + y = the y position within the entire
125 * The scroll position of the viewport
130 * The cursor's x position after ybase
135 * The cursor's y position after ybase
140 * Used to debounce the refresh function
142 this.isRefreshing
= false;
145 * Whether there is a full terminal refresh queued
148 this.cursorState
= 0;
149 this.cursorHidden
= false;
154 this.scrollBottom
= this.rows
- 1;
155 this.customKeydownHandler
= null;
158 this.applicationKeypad
= false;
159 this.applicationCursor
= false;
160 this.originMode
= false;
161 this.insertMode
= false;
162 this.wraparoundMode
= true; // defaults: xterm - true, vt100 - false
167 this.gcharset
= null;
169 this.charsets
= [null];
193 this.readable
= true;
194 this.writable
= true;
196 this.defAttr
= (0 << 18) | (257 << 9) | (256 << 0);
197 this.curAttr
= this.defAttr
;
200 this.currentParam
= 0;
204 // leftover surrogate high from previous write invocation
205 this.surrogate_high
= '';
208 * An array of all lines in the entire buffer, including the prompt. The lines are array of
209 * characters which are 2-length arrays where [0] is an attribute and [1] is the character.
214 this.lines
.push(this.blankLine());
220 // Store if user went browsing history in scrollback
221 this.userScrolling
= false;
224 inherits(Terminal
, EventEmitter
);
227 * back_color_erase feature for xterm.
229 Terminal
.prototype.eraseAttr = function() {
230 // if (this.is('screen')) return this.defAttr;
231 return (this.defAttr
& ~0x1ff) | (this.curAttr
& 0x1ff);
239 Terminal
.tangoColors
= [
260 // Colors 0-15 + 16-255
261 // Much thanks to TooTallNate for writing this.
262 Terminal
.colors
= (function() {
263 var colors
= Terminal
.tangoColors
.slice()
264 , r
= [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff]
269 for (; i
< 216; i
++) {
270 out(r
[(i
/ 36) % 6 | 0], r
[(i
/ 6) % 6 | 0], r
[i
% 6]);
275 for (; i
< 24; i
++) {
280 function out(r
, g
, b
) {
281 colors
.push('#' + hex(r
) + hex(g
) + hex(b
));
286 return c
.length
< 2 ? '0' + c
: c
;
292 Terminal
._colors
= Terminal
.colors
.slice();
294 Terminal
.vcolors
= (function() {
296 , colors
= Terminal
.colors
300 for (; i
< 256; i
++) {
301 color
= parseInt(colors
[i
].substring(1), 16);
303 (color
>> 16) & 0xff,
316 Terminal
.defaults
= {
317 colors
: Terminal
.colors
,
329 // programFeatures: false,
333 Terminal
.options
= {};
335 Terminal
.focus
= null;
337 each(keys(Terminal
.defaults
), function(key
) {
338 Terminal
[key
] = Terminal
.defaults
[key
];
339 Terminal
.options
[key
] = Terminal
.defaults
[key
];
343 * Focus the terminal. Delegates focus handling to the terminal's DOM element.
345 Terminal
.prototype.focus = function() {
346 return this.textarea
.focus();
350 * Retrieves an option's value from the terminal.
351 * @param {string} key The option key.
353 Terminal
.prototype.getOption = function(key
, value
) {
354 if (!(key
in Terminal
.defaults
)) {
355 throw new Error('No option with key "' + key
+ '"');
358 if (typeof this.options
[key
] !== 'undefined') {
359 return this.options
[key
];
366 * Sets an option on the terminal.
367 * @param {string} key The option key.
368 * @param {string} value The option value.
370 Terminal
.prototype.setOption = function(key
, value
) {
371 if (!(key
in Terminal
.defaults
)) {
372 throw new Error('No option with key "' + key
+ '"');
375 this.options
[key
] = value
;
379 * Binds the desired focus behavior on a given terminal object.
383 Terminal
.bindFocus = function (term
) {
384 on(term
.textarea
, 'focus', function (ev
) {
385 if (term
.sendFocus
) {
388 term
.element
.classList
.add('focus');
390 Terminal
.focus
= term
;
391 term
.emit('focus', {terminal
: term
});
396 * Blur the terminal. Delegates blur handling to the terminal's DOM element.
398 Terminal
.prototype.blur = function() {
399 return this.textarea
.blur();
403 * Binds the desired blur behavior on a given terminal object.
407 Terminal
.bindBlur = function (term
) {
408 on(term
.textarea
, 'blur', function (ev
) {
409 term
.refresh(term
.y
, term
.y
);
410 if (term
.sendFocus
) {
413 term
.element
.classList
.remove('focus');
414 Terminal
.focus
= null;
415 term
.emit('blur', {terminal
: term
});
420 * Initialize default behavior
422 Terminal
.prototype.initGlobal = function() {
425 Terminal
.bindKeys(this);
426 Terminal
.bindFocus(this);
427 Terminal
.bindBlur(this);
429 // Bind clipboard functionality
430 on(this.element
, 'copy', function (ev
) {
431 copyHandler
.call(this, ev
, term
);
433 on(this.textarea
, 'paste', function (ev
) {
434 pasteHandler
.call(this, ev
, term
);
436 on(this.element
, 'paste', function (ev
) {
437 pasteHandler
.call(this, ev
, term
);
440 function rightClickHandlerWrapper (ev
) {
441 rightClickHandler
.call(this, ev
, term
);
444 if (term
.browser
.isFirefox
) {
445 on(this.element
, 'mousedown', function (ev
) {
446 if (ev
.button
== 2) {
447 rightClickHandlerWrapper(ev
);
451 on(this.element
, 'contextmenu', rightClickHandlerWrapper
);
456 * Apply key handling to the terminal
458 Terminal
.bindKeys = function(term
) {
459 on(term
.element
, 'keydown', function(ev
) {
460 if (document
.activeElement
!= this) {
466 on(term
.element
, 'keypress', function(ev
) {
467 if (document
.activeElement
!= this) {
473 on(term
.element
, 'keyup', term
.focus
.bind(term
));
475 on(term
.textarea
, 'keydown', function(ev
) {
479 on(term
.textarea
, 'keypress', function(ev
) {
481 // Truncate the textarea's value, since it is not needed
485 on(term
.textarea
, 'compositionstart', term
.compositionHelper
.compositionstart
.bind(term
.compositionHelper
));
486 on(term
.textarea
, 'compositionupdate', term
.compositionHelper
.compositionupdate
.bind(term
.compositionHelper
));
487 on(term
.textarea
, 'compositionend', term
.compositionHelper
.compositionend
.bind(term
.compositionHelper
));
488 term
.on('refresh', term
.compositionHelper
.updateCompositionElements
.bind(term
.compositionHelper
));
493 * Insert the given row to the terminal or produce a new one
494 * if no row argument is passed. Return the inserted row.
495 * @param {HTMLElement} row (optional) The row to append to the terminal.
497 Terminal
.prototype.insertRow = function (row
) {
498 if (typeof row
!= 'object') {
499 row
= document
.createElement('div');
502 this.rowContainer
.appendChild(row
);
503 this.children
.push(row
);
509 * Opens the terminal within an element.
511 * @param {HTMLElement} parent The element to create the terminal within.
513 Terminal
.prototype.open = function(parent
) {
514 var self
=this, i
=0, div
;
516 this.parent
= parent
|| this.parent
;
519 throw new Error('Terminal requires a parent element.');
522 // Grab global elements
523 this.context
= this.parent
.ownerDocument
.defaultView
;
524 this.document
= this.parent
.ownerDocument
;
525 this.body
= this.document
.getElementsByTagName('body')[0];
527 //Create main element container
528 this.element
= this.document
.createElement('div');
529 this.element
.classList
.add('terminal');
530 this.element
.classList
.add('xterm');
531 this.element
.classList
.add('xterm-theme-' + this.theme
);
533 this.element
.style
.height
534 this.element
.setAttribute('tabindex', 0);
536 this.viewportElement
= document
.createElement('div');
537 this.viewportElement
.classList
.add('xterm-viewport');
538 this.element
.appendChild(this.viewportElement
);
539 this.viewportScrollArea
= document
.createElement('div');
540 this.viewportScrollArea
.classList
.add('xterm-scroll-area');
541 this.viewportElement
.appendChild(this.viewportScrollArea
);
543 // Create the container that will hold the lines of the terminal and then
544 // produce the lines the lines.
545 this.rowContainer
= document
.createElement('div');
546 this.rowContainer
.classList
.add('xterm-rows');
547 this.element
.appendChild(this.rowContainer
);
550 // Create the container that will hold helpers like the textarea for
551 // capturing DOM Events. Then produce the helpers.
552 this.helperContainer
= document
.createElement('div');
553 this.helperContainer
.classList
.add('xterm-helpers');
554 // TODO: This should probably be inserted once it's filled to prevent an additional layout
555 this.element
.appendChild(this.helperContainer
);
556 this.textarea
= document
.createElement('textarea');
557 this.textarea
.classList
.add('xterm-helper-textarea');
558 this.textarea
.setAttribute('autocorrect', 'off');
559 this.textarea
.setAttribute('autocapitalize', 'off');
560 this.textarea
.setAttribute('spellcheck', 'false');
561 this.textarea
.tabIndex
= 0;
562 this.textarea
.addEventListener('focus', function() {
563 self
.emit('focus', {terminal
: self
});
565 this.textarea
.addEventListener('blur', function() {
566 self
.emit('blur', {terminal
: self
});
568 this.helperContainer
.appendChild(this.textarea
);
570 this.compositionView
= document
.createElement('div');
571 this.compositionView
.classList
.add('composition-view');
572 this.compositionHelper
= new CompositionHelper(this.textarea
, this.compositionView
, this);
573 this.helperContainer
.appendChild(this.compositionView
);
575 this.charMeasureElement
= document
.createElement('div');
576 this.charMeasureElement
.classList
.add('xterm-char-measure-element');
577 this.charMeasureElement
.innerHTML
= 'W';
578 this.helperContainer
.appendChild(this.charMeasureElement
);
580 for (; i
< this.rows
; i
++) {
583 this.parent
.appendChild(this.element
);
585 this.viewport
= new Viewport(this, this.viewportElement
, this.viewportScrollArea
, this.charMeasureElement
);
588 this.refresh(0, this.rows
- 1);
590 // Initialize global actions that
591 // need to be taken on the document.
594 // Ensure there is a Terminal.focus.
597 on(this.element
, 'click', function() {
598 var selection
= document
.getSelection(),
599 collapsed
= selection
.isCollapsed
,
600 isRange
= typeof collapsed
== 'boolean' ? !collapsed
: selection
.type
== 'Range';
606 // Listen for mouse events and translate
607 // them into terminal mouse protocols.
610 // Figure out whether boldness affects
611 // the character width of monospace fonts.
612 if (Terminal
.brokenBold
== null) {
613 Terminal
.brokenBold
= isBoldBroken(this.document
);
617 * This event is emitted when terminal has completed opening.
626 * Attempts to load an add-on using CommonJS or RequireJS (whichever is available).
627 * @param {string} addon The name of the addon to load
630 Terminal
.loadAddon = function(addon
, callback
) {
631 if (typeof exports
=== 'object' && typeof module
=== 'object') {
633 return require('./addons/' + addon
+ '/' + addon
);
634 } else if (typeof define
== 'function') {
636 return require(['./addons/' + addon
+ '/' + addon
], callback
);
638 console
.error('Cannot load a module without a CommonJS or RequireJS environment.');
646 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#Mouse%20Tracking
647 * To better understand these
648 * the xterm code is very helpful:
650 * button.c, charproc.c, misc.c
651 * Relevant functions in xterm/button.c:
652 * BtnCode, EmitButtonCode, EditorButton, SendMousePosition
654 Terminal
.prototype.bindMouse = function() {
655 var el
= this.element
, self
= this, pressed
= 32;
657 // mouseup, mousedown, wheel
658 // left click: ^[[M 3<^[[M#3<
660 function sendButton(ev
) {
664 // get the xterm-style button
665 button
= getButton(ev
);
667 // get mouse coordinates
671 sendEvent(button
, pos
);
673 switch (ev
.overrideType
|| ev
.type
) {
678 // keep it at the left
679 // button, just in case.
690 // motion example of a left click:
691 // ^[[M 3<^[[M@4<^[[M@5<^[[M@6<^[[M@7<^[[M#7<
692 function sendMove(ev
) {
699 // buttons marked as motions
700 // are incremented by 32
703 sendEvent(button
, pos
);
707 // position to characters
708 function encode(data
, ch
) {
709 if (!self
.utfMouse
) {
710 if (ch
=== 255) return data
.push(0);
711 if (ch
> 127) ch
= 127;
714 if (ch
=== 2047) return data
.push(0);
718 if (ch
> 2047) ch
= 2047;
719 data
.push(0xC0 | (ch
>> 6));
720 data
.push(0x80 | (ch
& 0x3F));
725 // send a mouse event:
726 // regular/utf8: ^[[M Cb Cx Cy
727 // urxvt: ^[[ Cb ; Cx ; Cy M
728 // sgr: ^[[ Cb ; Cx ; Cy M/m
729 // vt300: ^[[ 24(1/3/5)~ [ Cx , Cy ] \r
730 // locator: CSI P e ; P b ; P r ; P c ; P p & w
731 function sendEvent(button
, pos
) {
732 // self.emit('mouse', {
738 if (self
.vt300Mouse
) {
740 // http://www.vt100.net/docs/vt3xx-gp/chapter15.html
744 var data
= '\x1b[24';
745 if (button
=== 0) data
+= '1';
746 else if (button
=== 1) data
+= '3';
747 else if (button
=== 2) data
+= '5';
748 else if (button
=== 3) return;
750 data
+= '~[' + pos
.x
+ ',' + pos
.y
+ ']\r';
755 if (self
.decLocator
) {
760 if (button
=== 0) button
= 2;
761 else if (button
=== 1) button
= 4;
762 else if (button
=== 2) button
= 6;
763 else if (button
=== 3) button
= 3;
767 + (button
=== 3 ? 4 : 0)
778 if (self
.urxvtMouse
) {
783 self
.send('\x1b[' + button
+ ';' + pos
.x
+ ';' + pos
.y
+ 'M');
791 + (((button
& 3) === 3 ? button
& ~3 : button
) - 32)
796 + ((button
& 3) === 3 ? 'm' : 'M'));
802 encode(data
, button
);
806 self
.send('\x1b[M' + String
.fromCharCode
.apply(String
, data
));
809 function getButton(ev
) {
822 // 1, and 2 - with 64 added
823 switch (ev
.overrideType
|| ev
.type
) {
825 button
= ev
.button
!= null
831 if (self
.browser
.isMSIE
) {
832 button
= button
=== 1 ? 0 : button
=== 4 ? 1 : button
;
838 case 'DOMMouseScroll':
839 button
= ev
.detail
< 0
844 button
= ev
.wheelDeltaY
> 0
850 // next three bits are the modifiers:
851 // 4 = shift, 8 = meta, 16 = control
852 shift
= ev
.shiftKey
? 4 : 0;
853 meta
= ev
.metaKey
? 8 : 0;
854 ctrl
= ev
.ctrlKey
? 16 : 0;
855 mod
= shift
| meta
| ctrl
;
858 if (self
.vt200Mouse
) {
861 } else if (!self
.normalMouse
) {
866 button
= (32 + (mod
<< 2)) + button
;
871 // mouse coordinates measured in cols/rows
872 function getCoords(ev
) {
875 // ignore browsers without pageX for now
876 if (ev
.pageX
== null) return;
882 // should probably check offsetParent
883 // but this is more portable
884 while (el
&& el
!== self
.document
.documentElement
) {
887 el
= 'offsetParent' in el
892 // convert to cols/rows
893 w
= self
.element
.clientWidth
;
894 h
= self
.element
.clientHeight
;
895 x
= Math
.ceil((x
/ w
) * self
.cols
);
896 y
= Math
.ceil((y
/ h
) * self
.rows
);
898 // be sure to avoid sending
899 // bad positions to the program
901 if (x
> self
.cols
) x
= self
.cols
;
903 if (y
> self
.rows
) y
= self
.rows
;
905 // xterm sends raw bytes and
906 // starts at 32 (SP) for each.
917 on(el
, 'mousedown', function(ev
) {
918 if (!self
.mouseEvents
) return;
927 //if (self.vt200Mouse && !self.normalMouse) {
928 if (self
.vt200Mouse
) {
929 ev
.overrideType
= 'mouseup';
931 return self
.cancel(ev
);
935 if (self
.normalMouse
) on(self
.document
, 'mousemove', sendMove
);
937 // x10 compatibility mode can't send button releases
938 if (!self
.x10Mouse
) {
939 on(self
.document
, 'mouseup', function up(ev
) {
941 if (self
.normalMouse
) off(self
.document
, 'mousemove', sendMove
);
942 off(self
.document
, 'mouseup', up
);
943 return self
.cancel(ev
);
947 return self
.cancel(ev
);
950 //if (self.normalMouse) {
951 // on(self.document, 'mousemove', sendMove);
954 on(el
, 'wheel', function(ev
) {
955 if (!self
.mouseEvents
) return;
958 || self
.decLocator
) return;
960 return self
.cancel(ev
);
963 // allow wheel scrolling in
964 // the shell for example
965 on(el
, 'wheel', function(ev
) {
966 if (self
.mouseEvents
) return;
967 self
.viewport
.onWheel(ev
);
968 return self
.cancel(ev
);
973 * Destroys the terminal.
975 Terminal
.prototype.destroy = function() {
976 this.readable
= false;
977 this.writable
= false;
979 this.handler = function() {};
980 this.write = function() {};
981 if (this.element
.parentNode
) {
982 this.element
.parentNode
.removeChild(this.element
);
984 //this.emit('close');
989 * Flags used to render terminal text properly
1000 * Refreshes (re-renders) terminal content within two rows (inclusive)
1004 * In the screen buffer, each character is stored as a an array with a character
1005 * and a 32-bit integer:
1006 * - First value: a utf-16 character.
1008 * - Next 9 bits: background color (0-511).
1009 * - Next 9 bits: foreground color (0-511).
1010 * - Next 14 bits: a mask for misc. flags:
1017 * @param {number} start The row to start from (between 0 and terminal's height terminal - 1)
1018 * @param {number} end The row to end at (between fromRow and terminal's height terminal - 1)
1019 * @param {boolean} queue Whether the refresh should ran right now or be queued
1021 Terminal
.prototype.refresh = function(start
, end
, queue
) {
1024 // queue defaults to true
1025 queue
= (typeof queue
== 'undefined') ? true : queue
;
1028 * The refresh queue allows refresh to execute only approximately 30 times a second. For
1029 * commands that pass a significant amount of output to the write function, this prevents the
1030 * terminal from maxing out the CPU and making the UI unresponsive. While commands can still
1031 * run beyond what they do on the terminal, it is far better with a debounce in place as
1032 * every single terminal manipulation does not need to be constructed in the DOM.
1034 * A side-effect of this is that it makes ^C to interrupt a process seem more responsive.
1037 // If refresh should be queued, order the refresh and return.
1038 if (this._refreshIsQueued
) {
1039 // If a refresh has already been queued, just order a full refresh next
1040 this._fullRefreshNext
= true;
1042 setTimeout(function () {
1043 self
.refresh(start
, end
, false);
1045 this._refreshIsQueued
= true;
1050 // If refresh should be run right now (not be queued), release the lock
1051 this._refreshIsQueued
= false;
1053 // If multiple refreshes were requested, make a full refresh.
1054 if (this._fullRefreshNext
) {
1056 end
= this.rows
- 1;
1057 this._fullRefreshNext
= false // reset lock
1060 var x
, y
, i
, line
, out
, ch
, ch_width
, width
, data
, attr
, bg
, fg
, flags
, row
, parent
, focused
= document
.activeElement
;
1062 // If this is a big refresh, remove the terminal rows from the DOM for faster calculations
1063 if (end
- start
>= this.rows
/ 2) {
1064 parent
= this.element
.parentNode
;
1066 this.element
.removeChild(this.rowContainer
);
1073 if (end
>= this.rows
.length
) {
1074 this.log('`end` is too large. Most likely a bad CSR.');
1075 end
= this.rows
.length
- 1;
1078 for (; y
<= end
; y
++) {
1079 row
= y
+ this.ydisp
;
1081 line
= this.lines
[row
];
1084 if (this.y
=== y
- (this.ybase
- this.ydisp
)
1086 && !this.cursorHidden
) {
1092 attr
= this.defAttr
;
1095 for (; i
< width
; i
++) {
1098 ch_width
= line
[i
][2];
1102 if (i
=== x
) data
= -1;
1104 if (data
!== attr
) {
1105 if (attr
!== this.defAttr
) {
1108 if (data
!== this.defAttr
) {
1110 out
+= '<span class="reverse-video terminal-cursor';
1111 if (this.cursorBlink
) {
1116 var classNames
= [];
1119 fg
= (data
>> 9) & 0x1ff;
1122 if (flags
& Terminal
.flags
.BOLD
) {
1123 if (!Terminal
.brokenBold
) {
1124 classNames
.push('xterm-bold');
1126 // See: XTerm*boldColors
1127 if (fg
< 8) fg
+= 8;
1130 if (flags
& Terminal
.flags
.UNDERLINE
) {
1131 classNames
.push('xterm-underline');
1134 if (flags
& Terminal
.flags
.BLINK
) {
1135 classNames
.push('xterm-blink');
1138 // If inverse flag is on, then swap the foreground and background variables.
1139 if (flags
& Terminal
.flags
.INVERSE
) {
1140 /* One-line variable swap in JavaScript: http://stackoverflow.com/a/16201730 */
1141 bg = [fg, fg = bg][0];
1142 // Should inverse just be before the
1143 // above boldColors effect instead?
1144 if ((flags & 1) && fg < 8) fg += 8;
1147 if (flags & Terminal.flags.INVISIBLE) {
1148 classNames.push('xterm-hidden');
1152 * Weird situation: Invert flag used black foreground and white background results
1153 * in invalid background color, positioned at the 256 index of the 256 terminal
1154 * color map. Pin the colors manually in such a case.
1156 * Source: https://github.com/sourcelair/xterm.js/issues/57
1158 if (flags
& Terminal
.flags
.INVERSE
) {
1168 classNames
.push('xterm-bg-color-' + bg
);
1172 classNames
.push('xterm-color-' + fg
);
1176 if (classNames
.length
) {
1177 out
+= ' class="' + classNames
.join(' ') + '"';
1206 if (attr
!== this.defAttr
) {
1210 this.children
[y
].innerHTML
= out
;
1214 this.element
.appendChild(this.rowContainer
);
1217 this.emit('refresh', {element
: this.element
, start
: start
, end
: end
});
1221 * Display the cursor element
1223 Terminal
.prototype.showCursor = function() {
1224 if (!this.cursorState
) {
1225 this.cursorState
= 1;
1226 this.refresh(this.y
, this.y
);
1231 * Scroll the terminal
1233 Terminal
.prototype.scroll = function() {
1236 if (++this.ybase
=== this.scrollback
) {
1237 this.ybase
= this.ybase
/ 2 | 0;
1238 this.lines
= this.lines
.slice(-(this.ybase
+ this.rows
) + 1);
1241 if (!this.userScrolling
) {
1242 this.ydisp
= this.ybase
;
1246 row
= this.ybase
+ this.rows
- 1;
1248 // subtract the bottom scroll region
1249 row
-= this.rows
- 1 - this.scrollBottom
;
1251 if (row
=== this.lines
.length
) {
1252 // potential optimization:
1253 // pushing is faster than splicing
1254 // when they amount to the same
1256 this.lines
.push(this.blankLine());
1259 this.lines
.splice(row
, 0, this.blankLine());
1262 if (this.scrollTop
!== 0) {
1263 if (this.ybase
!== 0) {
1265 if (!this.userScrolling
) {
1266 this.ydisp
= this.ybase
;
1269 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
1273 this.updateRange(this.scrollTop
);
1274 this.updateRange(this.scrollBottom
);
1277 * This event is emitted whenever the terminal is scrolled.
1278 * The one parameter passed is the new y display position.
1282 this.emit('scroll', this.ydisp
);
1286 * Scroll the display of the terminal
1287 * @param {number} disp The number of lines to scroll down (negatives scroll up).
1288 * @param {boolean} suppressScrollEvent Don't emit the scroll event as scrollDisp. This is used
1289 * to avoid unwanted events being handled by the veiwport when the event was triggered from the
1290 * viewport originally.
1292 Terminal
.prototype.scrollDisp = function(disp
, suppressScrollEvent
) {
1294 this.userScrolling
= true;
1295 } else if (disp
+ this.ydisp
>= this.ybase
) {
1296 this.userScrolling
= false;
1301 if (this.ydisp
> this.ybase
) {
1302 this.ydisp
= this.ybase
;
1303 } else if (this.ydisp
< 0) {
1307 if (!suppressScrollEvent
) {
1308 this.emit('scroll', this.ydisp
);
1311 this.refresh(0, this.rows
- 1);
1315 * Scroll the display of the terminal by a number of pages.
1316 * @param {number} pageCount The number of pages to scroll (negative scrolls up).
1318 Terminal
.prototype.scrollPages = function(pageCount
) {
1319 this.scrollDisp(pageCount
* (this.rows
- 1));
1323 * Scrolls the display of the terminal to the top.
1325 Terminal
.prototype.scrollToTop = function() {
1326 this.scrollDisp(-this.ydisp
);
1330 * Scrolls the display of the terminal to the bottom.
1332 Terminal
.prototype.scrollToBottom = function() {
1333 this.scrollDisp(this.ybase
- this.ydisp
);
1337 * Writes text to the terminal.
1338 * @param {string} text The text to write to the terminal.
1340 Terminal
.prototype.write = function(data
) {
1341 var l
= data
.length
, i
= 0, j
, cs
, ch
, code
, low
, ch_width
, row
;
1343 this.refreshStart
= this.y
;
1344 this.refreshEnd
= this.y
;
1346 // apply leftover surrogate high from last write
1347 if (this.surrogate_high
) {
1348 data
= this.surrogate_high
+ data
;
1349 this.surrogate_high
= '';
1352 for (; i
< l
; i
++) {
1355 // FIXME: higher chars than 0xa0 are not allowed in escape sequences
1356 // --> maybe move to default
1357 code
= data
.charCodeAt(i
);
1358 if (0xD800 <= code
&& code
<= 0xDBFF) {
1359 // we got a surrogate high
1360 // get surrogate low (next 2 bytes)
1361 low
= data
.charCodeAt(i
+1);
1363 // end of data stream, save surrogate high
1364 this.surrogate_high
= ch
;
1367 code
= ((code
- 0xD800) * 0x400) + (low
- 0xDC00) + 0x10000;
1368 ch
+= data
.charAt(i
+1);
1370 // surrogate low - already handled above
1371 if (0xDC00 <= code
&& code
<= 0xDFFF)
1374 switch (this.state
) {
1385 if (this.convertEol
) {
1389 if (this.y
> this.scrollBottom
) {
1409 this.x
= this.nextStop();
1424 this.state
= escaped
;
1429 // calculate print space
1430 // expensive call, therefore we save width in line buffer
1431 ch_width
= wcwidth(code
);
1434 if (this.charset
&& this.charset
[ch
]) {
1435 ch
= this.charset
[ch
];
1438 row
= this.y
+ this.ybase
;
1440 // insert combining char in last cell
1441 // FIXME: needs handling after cursor jumps
1442 if (!ch_width
&& this.x
) {
1444 // dont overflow left
1445 if (this.lines
[row
][this.x
-1]) {
1446 if (!this.lines
[row
][this.x
-1][2]) {
1448 // found empty cell after fullwidth, need to go 2 cells back
1449 if (this.lines
[row
][this.x
-2])
1450 this.lines
[row
][this.x
-2][1] += ch
;
1453 this.lines
[row
][this.x
-1][1] += ch
;
1455 this.updateRange(this.y
);
1460 // goto next line if ch would overflow
1461 // TODO: needs a global min terminal width of 2
1462 if (this.x
+ch_width
-1 >= this.cols
) {
1463 // autowrap - DECAWM
1464 if (this.wraparoundMode
) {
1467 if (this.y
> this.scrollBottom
) {
1472 this.x
= this.cols
-1;
1473 if(ch_width
===2) // FIXME: check for xterm behavior
1477 row
= this.y
+ this.ybase
;
1479 // insert mode: move characters to right
1480 if (this.insertMode
) {
1481 // do this twice for a fullwidth char
1482 for (var moves
=0; moves
<ch_width
; ++moves
) {
1483 // remove last cell, if it's width is 0
1484 // we have to adjust the second last cell as well
1485 var removed
= this.lines
[this.y
+ this.ybase
].pop();
1487 && this.lines
[row
][this.cols
-2]
1488 && this.lines
[row
][this.cols
-2][2]===2)
1489 this.lines
[row
][this.cols
-2] = [this.curAttr
, ' ', 1];
1491 // insert empty cell at cursor
1492 this.lines
[row
].splice(this.x
, 0, [this.curAttr
, ' ', 1]);
1496 this.lines
[row
][this.x
] = [this.curAttr
, ch
, ch_width
];
1498 this.updateRange(this.y
);
1500 // fullwidth char - set next cell width to zero and advance cursor
1502 this.lines
[row
][this.x
] = [this.curAttr
, '', 0];
1511 // ESC [ Control Sequence Introducer ( CSI is 0x9b).
1514 this.currentParam
= 0;
1518 // ESC ] Operating System Command ( OSC is 0x9d).
1521 this.currentParam
= 0;
1525 // ESC P Device Control String ( DCS is 0x90).
1528 this.currentParam
= 0;
1532 // ESC _ Application Program Command ( APC is 0x9f).
1534 this.state
= ignore
;
1537 // ESC ^ Privacy Message ( PM is 0x9e).
1539 this.state
= ignore
;
1542 // ESC c Full Reset (RIS).
1547 // ESC E Next Line ( NEL is 0x85).
1548 // ESC D Index ( IND is 0x84).
1556 // ESC M Reverse Index ( RI is 0x8d).
1558 this.reverseIndex();
1561 // ESC % Select default/utf-8 character set.
1562 // @ = default, G = utf-8
1564 //this.charset = null;
1566 this.setgCharset(0, Terminal
.charsets
.US
);
1567 this.state
= normal
;
1571 // ESC (,),*,+,-,. Designate G0-G2 Character Set.
1572 case '(': // <-- this seems to get all the attention
1598 this.state
= charset
;
1601 // Designate G3 Character Set (VT300).
1602 // A = ISO Latin-1 Supplemental.
1606 this.state
= charset
;
1611 // Single Shift Select of G2 Character Set
1612 // ( SS2 is 0x8e). This affects next character only.
1616 // Single Shift Select of G3 Character Set
1617 // ( SS3 is 0x8f). This affects next character only.
1621 // Invoke the G2 Character Set as GL (LS2).
1626 // Invoke the G3 Character Set as GL (LS3).
1631 // Invoke the G3 Character Set as GR (LS3R).
1636 // Invoke the G2 Character Set as GR (LS2R).
1641 // Invoke the G1 Character Set as GR (LS1R).
1646 // ESC 7 Save Cursor (DECSC).
1649 this.state
= normal
;
1652 // ESC 8 Restore Cursor (DECRC).
1654 this.restoreCursor();
1655 this.state
= normal
;
1658 // ESC # 3 DEC line height/width
1660 this.state
= normal
;
1664 // ESC H Tab Set (HTS is 0x88).
1669 // ESC = Application Keypad (DECKPAM).
1671 this.log('Serial port requested application keypad.');
1672 this.applicationKeypad
= true;
1673 this.viewport
.syncScrollArea();
1674 this.state
= normal
;
1677 // ESC > Normal Keypad (DECKPNM).
1679 this.log('Switching back to normal keypad.');
1680 this.applicationKeypad
= false;
1681 this.viewport
.syncScrollArea();
1682 this.state
= normal
;
1686 this.state
= normal
;
1687 this.error('Unknown ESC control: %s.', ch
);
1694 case '0': // DEC Special Character and Line Drawing Set.
1695 cs
= Terminal
.charsets
.SCLD
;
1698 cs
= Terminal
.charsets
.UK
;
1700 case 'B': // United States (USASCII).
1701 cs
= Terminal
.charsets
.US
;
1704 cs
= Terminal
.charsets
.Dutch
;
1706 case 'C': // Finnish
1708 cs
= Terminal
.charsets
.Finnish
;
1711 cs
= Terminal
.charsets
.French
;
1713 case 'Q': // FrenchCanadian
1714 cs
= Terminal
.charsets
.FrenchCanadian
;
1717 cs
= Terminal
.charsets
.German
;
1719 case 'Y': // Italian
1720 cs
= Terminal
.charsets
.Italian
;
1722 case 'E': // NorwegianDanish
1724 cs
= Terminal
.charsets
.NorwegianDanish
;
1726 case 'Z': // Spanish
1727 cs
= Terminal
.charsets
.Spanish
;
1729 case 'H': // Swedish
1731 cs
= Terminal
.charsets
.Swedish
;
1734 cs
= Terminal
.charsets
.Swiss
;
1736 case '/': // ISOLatin (actually /A)
1737 cs
= Terminal
.charsets
.ISOLatin
;
1741 cs
= Terminal
.charsets
.US
;
1744 this.setgCharset(this.gcharset
, cs
);
1745 this.gcharset
= null;
1746 this.state
= normal
;
1752 // Set Text Parameters.
1753 if (ch
=== '\x1b' || ch
=== '\x07') {
1754 if (ch
=== '\x1b') i
++;
1756 this.params
.push(this.currentParam
);
1758 switch (this.params
[0]) {
1762 if (this.params
[1]) {
1763 this.title
= this.params
[1];
1764 this.handleTitle(this.title
);
1772 // change dynamic colors
1784 // change dynamic ui colors
1796 // manipulate selection data
1814 this.currentParam
= 0;
1815 this.state
= normal
;
1817 if (!this.params
.length
) {
1818 if (ch
>= '0' && ch
<= '9') {
1820 this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1821 } else if (ch
=== ';') {
1822 this.params
.push(this.currentParam
);
1823 this.currentParam
= '';
1826 this.currentParam
+= ch
;
1833 if (ch
=== '?' || ch
=== '>' || ch
=== '!') {
1839 if (ch
>= '0' && ch
<= '9') {
1840 this.currentParam
= this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1844 // '$', '"', ' ', '\''
1845 if (ch
=== '$' || ch
=== '"' || ch
=== ' ' || ch
=== '\'') {
1850 this.params
.push(this.currentParam
);
1851 this.currentParam
= 0;
1854 if (ch
=== ';') break;
1856 this.state
= normal
;
1860 // Cursor Up Ps Times (default = 1) (CUU).
1862 this.cursorUp(this.params
);
1866 // Cursor Down Ps Times (default = 1) (CUD).
1868 this.cursorDown(this.params
);
1872 // Cursor Forward Ps Times (default = 1) (CUF).
1874 this.cursorForward(this.params
);
1878 // Cursor Backward Ps Times (default = 1) (CUB).
1880 this.cursorBackward(this.params
);
1884 // Cursor Position [row;column] (default = [1,1]) (CUP).
1886 this.cursorPos(this.params
);
1889 // CSI Ps J Erase in Display (ED).
1891 this.eraseInDisplay(this.params
);
1894 // CSI Ps K Erase in Line (EL).
1896 this.eraseInLine(this.params
);
1899 // CSI Pm m Character Attributes (SGR).
1902 this.charAttributes(this.params
);
1906 // CSI Ps n Device Status Report (DSR).
1909 this.deviceStatus(this.params
);
1918 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
1920 this.insertChars(this.params
);
1924 // Cursor Next Line Ps Times (default = 1) (CNL).
1926 this.cursorNextLine(this.params
);
1930 // Cursor Preceding Line Ps Times (default = 1) (CNL).
1932 this.cursorPrecedingLine(this.params
);
1936 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
1938 this.cursorCharAbsolute(this.params
);
1942 // Insert Ps Line(s) (default = 1) (IL).
1944 this.insertLines(this.params
);
1948 // Delete Ps Line(s) (default = 1) (DL).
1950 this.deleteLines(this.params
);
1954 // Delete Ps Character(s) (default = 1) (DCH).
1956 this.deleteChars(this.params
);
1960 // Erase Ps Character(s) (default = 1) (ECH).
1962 this.eraseChars(this.params
);
1965 // CSI Pm ` Character Position Absolute
1966 // [column] (default = [row,1]) (HPA).
1968 this.charPosAbsolute(this.params
);
1972 // Horizontal Position Relative
1974 this.HPositionRelative(this.params
);
1978 // Send Device Attributes (Primary DA).
1980 // Send Device Attributes (Secondary DA)
1982 this.sendDeviceAttributes(this.params
);
1986 // Line Position Absolute [row] (default = [1,column]) (VPA).
1988 this.linePosAbsolute(this.params
);
1991 // 145 65 e * VPR - Vertical Position Relative
1993 this.VPositionRelative(this.params
);
1997 // Horizontal and Vertical Position [row;column] (default =
2000 this.HVPosition(this.params
);
2003 // CSI Pm h Set Mode (SM).
2004 // CSI ? Pm h - mouse escape codes, cursor escape codes
2006 this.setMode(this.params
);
2009 // CSI Pm l Reset Mode (RM).
2012 this.resetMode(this.params
);
2016 // Set Scrolling Region [top;bottom] (default = full size of win-
2020 this.setScrollRegion(this.params
);
2024 // Save cursor (ANSI.SYS).
2026 this.saveCursor(this.params
);
2030 // Restore cursor (ANSI.SYS).
2032 this.restoreCursor(this.params
);
2040 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
2042 this.cursorForwardTab(this.params
);
2045 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
2047 this.scrollUp(this.params
);
2050 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
2051 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
2054 // if (this.prefix === '>') {
2055 // this.resetTitleModes(this.params);
2058 // if (this.params.length > 2) {
2059 // this.initMouseTracking(this.params);
2062 if (this.params
.length
< 2 && !this.prefix
) {
2063 this.scrollDown(this.params
);
2068 // Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
2070 this.cursorBackwardTab(this.params
);
2073 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
2075 this.repeatPrecedingCharacter(this.params
);
2078 // CSI Ps g Tab Clear (TBC).
2080 this.tabClear(this.params
);
2083 // CSI Pm i Media Copy (MC).
2086 // this.mediaCopy(this.params);
2089 // CSI Pm m Character Attributes (SGR).
2091 // case 'm': // duplicate
2092 // if (this.prefix === '>') {
2093 // this.setResources(this.params);
2095 // this.charAttributes(this.params);
2099 // CSI Ps n Device Status Report (DSR).
2101 // case 'n': // duplicate
2102 // if (this.prefix === '>') {
2103 // this.disableModifiers(this.params);
2105 // this.deviceStatus(this.params);
2109 // CSI > Ps p Set pointer mode.
2110 // CSI ! p Soft terminal reset (DECSTR).
2112 // Request ANSI mode (DECRQM).
2114 // Request DEC private mode (DECRQM).
2117 switch (this.prefix
) {
2119 // this.setPointerMode(this.params);
2122 this.softReset(this.params
);
2125 // if (this.postfix === '$') {
2126 // this.requestPrivateMode(this.params);
2130 // if (this.postfix === '"') {
2131 // this.setConformanceLevel(this.params);
2132 // } else if (this.postfix === '$') {
2133 // this.requestAnsiMode(this.params);
2139 // CSI Ps q Load LEDs (DECLL).
2143 // if (this.postfix === ' ') {
2144 // this.setCursorStyle(this.params);
2147 // if (this.postfix === '"') {
2148 // this.setCharProtectionAttr(this.params);
2151 // this.loadLEDs(this.params);
2155 // Set Scrolling Region [top;bottom] (default = full size of win-
2158 // CSI Pt; Pl; Pb; Pr; Ps$ r
2159 // case 'r': // duplicate
2160 // if (this.prefix === '?') {
2161 // this.restorePrivateValues(this.params);
2162 // } else if (this.postfix === '$') {
2163 // this.setAttrInRectangle(this.params);
2165 // this.setScrollRegion(this.params);
2169 // CSI s Save cursor (ANSI.SYS).
2171 // case 's': // duplicate
2172 // if (this.prefix === '?') {
2173 // this.savePrivateValues(this.params);
2175 // this.saveCursor(this.params);
2179 // CSI Ps ; Ps ; Ps t
2180 // CSI Pt; Pl; Pb; Pr; Ps$ t
2184 // if (this.postfix === '$') {
2185 // this.reverseAttrInRectangle(this.params);
2186 // } else if (this.postfix === ' ') {
2187 // this.setWarningBellVolume(this.params);
2189 // if (this.prefix === '>') {
2190 // this.setTitleModeFeature(this.params);
2192 // this.manipulateWindow(this.params);
2197 // CSI u Restore cursor (ANSI.SYS).
2199 // case 'u': // duplicate
2200 // if (this.postfix === ' ') {
2201 // this.setMarginBellVolume(this.params);
2203 // this.restoreCursor(this.params);
2207 // CSI Pt; Pl; Pb; Pr; Pp; Pt; Pl; Pp$ v
2209 // if (this.postfix === '$') {
2210 // this.copyRectagle(this.params);
2214 // CSI Pt ; Pl ; Pb ; Pr ' w
2216 // if (this.postfix === '\'') {
2217 // this.enableFilterRectangle(this.params);
2221 // CSI Ps x Request Terminal Parameters (DECREQTPARM).
2222 // CSI Ps x Select Attribute Change Extent (DECSACE).
2223 // CSI Pc; Pt; Pl; Pb; Pr$ x
2225 // if (this.postfix === '$') {
2226 // this.fillRectangle(this.params);
2228 // this.requestParameters(this.params);
2229 // //this.__(this.params);
2234 // CSI Pt; Pl; Pb; Pr$ z
2236 // if (this.postfix === '\'') {
2237 // this.enableLocatorReporting(this.params);
2238 // } else if (this.postfix === '$') {
2239 // this.eraseRectangle(this.params);
2244 // CSI Pt; Pl; Pb; Pr$ {
2246 // if (this.postfix === '\'') {
2247 // this.setLocatorEvents(this.params);
2248 // } else if (this.postfix === '$') {
2249 // this.selectiveEraseRectangle(this.params);
2255 // if (this.postfix === '\'') {
2256 // this.requestLocatorPosition(this.params);
2261 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
2263 // if (this.postfix === ' ') {
2264 // this.insertColumns(this.params);
2269 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
2271 // if (this.postfix === ' ') {
2272 // this.deleteColumns(this.params);
2277 this.error('Unknown CSI code: %s.', ch
);
2286 if (ch
=== '\x1b' || ch
=== '\x07') {
2287 if (ch
=== '\x1b') i
++;
2289 switch (this.prefix
) {
2290 // User-Defined Keys (DECUDK).
2294 // Request Status String (DECRQSS).
2295 // test: echo -e '\eP$q"p\e\\'
2297 var pt
= this.currentParam
2314 + (this.scrollTop
+ 1)
2316 + (this.scrollBottom
+ 1)
2326 this.error('Unknown DCS Pt: %s.', pt
);
2331 this.send('\x1bP' + +valid
+ '$r' + pt
+ '\x1b\\');
2334 // Set Termcap/Terminfo Data (xterm, experimental).
2338 // Request Termcap/Terminfo String (xterm, experimental)
2339 // Regular xterm does not even respond to this sequence.
2340 // This can cause a small glitch in vim.
2341 // test: echo -ne '\eP+q6b64\e\\'
2343 var pt
= this.currentParam
2346 this.send('\x1bP' + +valid
+ '+r' + pt
+ '\x1b\\');
2350 this.error('Unknown DCS prefix: %s.', this.prefix
);
2354 this.currentParam
= 0;
2356 this.state
= normal
;
2357 } else if (!this.currentParam
) {
2358 if (!this.prefix
&& ch
!== '$' && ch
!== '+') {
2359 this.currentParam
= ch
;
2360 } else if (this.prefix
.length
=== 2) {
2361 this.currentParam
= ch
;
2366 this.currentParam
+= ch
;
2372 if (ch
=== '\x1b' || ch
=== '\x07') {
2373 if (ch
=== '\x1b') i
++;
2374 this.state
= normal
;
2380 this.updateRange(this.y
);
2381 this.refresh(this.refreshStart
, this.refreshEnd
);
2385 * Writes text to the terminal, followed by a break line character (\n).
2386 * @param {string} text The text to write to the terminal.
2388 Terminal
.prototype.writeln = function(data
) {
2389 this.write(data
+ '\r\n');
2393 * Attaches a custom keydown handler which is run before keys are processed, giving consumers of
2394 * xterm.js ultimate control as to what keys should be processed by the terminal and what keys
2396 * @param {function} customKeydownHandler The custom KeyboardEvent handler to attach. This is a
2397 * function that takes a KeyboardEvent, allowing consumers to stop propogation and/or prevent
2398 * the default action. The function returns whether the event should be processed by xterm.js.
2400 Terminal
.prototype.attachCustomKeydownHandler = function(customKeydownHandler
) {
2401 this.customKeydownHandler
= customKeydownHandler
;
2405 * Handle a keydown event
2407 * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
2408 * @param {KeyboardEvent} ev The keydown event to be handled.
2410 Terminal
.prototype.keyDown = function(ev
) {
2411 if (this.customKeydownHandler
&& this.customKeydownHandler(ev
) === false) {
2415 if (!this.compositionHelper
.keydown
.bind(this.compositionHelper
)(ev
)) {
2416 if (this.ybase
!== this.ydisp
) {
2417 this.scrollToBottom();
2423 var result
= this.evaluateKeyEscapeSequence(ev
);
2425 if (result
.scrollDisp
) {
2426 this.scrollDisp(result
.scrollDisp
);
2427 return this.cancel(ev
, true);
2430 if (isThirdLevelShift(this, ev
)) {
2434 if (result
.cancel
) {
2435 // The event is canceled at the end already, is this necessary?
2436 this.cancel(ev
, true);
2443 this.emit('keydown', ev
);
2444 this.emit('key', result
.key
, ev
);
2446 this.handler(result
.key
);
2448 return this.cancel(ev
, true);
2452 * Returns an object that determines how a KeyboardEvent should be handled. The key of the
2453 * returned value is the new key code to pass to the PTY.
2455 * Reference: http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
2456 * @param {KeyboardEvent} ev The keyboard event to be translated to key escape sequence.
2458 Terminal
.prototype.evaluateKeyEscapeSequence = function(ev
) {
2460 // Whether to cancel event propogation (NOTE: this may not be needed since the event is
2461 // canceled at the end of keyDown
2463 // The new key even to emit
2465 // The number of characters to scroll, if this is defined it will cancel the event
2466 scrollDisp
: undefined
2468 var modifiers
= ev
.shiftKey
<< 0 | ev
.altKey
<< 1 | ev
.ctrlKey
<< 2 | ev
.metaKey
<< 3;
2469 switch (ev
.keyCode
) {
2473 result
.key
= '\x08'; // ^H
2476 result
.key
= '\x7f'; // ^?
2481 result
.key
= '\x1b[Z';
2485 result
.cancel
= true;
2490 result
.cancel
= true;
2494 result
.key
= '\x1b';
2495 result
.cancel
= true;
2500 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'D';
2501 // HACK: Make Alt + left-arrow behave like Ctrl + left-arrow: move one word backwards
2502 // http://unix.stackexchange.com/a/108106
2503 // macOS uses different escape sequences than linux
2504 if (result
.key
== '\x1b[1;3D') {
2505 result
.key
= (this.browser
.isMac
) ? '\x1bb' : '\x1b[1;5D';
2507 } else if (this.applicationCursor
) {
2508 result
.key
= '\x1bOD';
2510 result
.key
= '\x1b[D';
2516 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'C';
2517 // HACK: Make Alt + right-arrow behave like Ctrl + right-arrow: move one word forward
2518 // http://unix.stackexchange.com/a/108106
2519 // macOS uses different escape sequences than linux
2520 if (result
.key
== '\x1b[1;3C') {
2521 result
.key
= (this.browser
.isMac
) ? '\x1bf' : '\x1b[1;5C';
2523 } else if (this.applicationCursor
) {
2524 result
.key
= '\x1bOC';
2526 result
.key
= '\x1b[C';
2532 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'A';
2533 // HACK: Make Alt + up-arrow behave like Ctrl + up-arrow
2534 // http://unix.stackexchange.com/a/108106
2535 if (result
.key
== '\x1b[1;3A') {
2536 result
.key
= '\x1b[1;5A';
2538 } else if (this.applicationCursor
) {
2539 result
.key
= '\x1bOA';
2541 result
.key
= '\x1b[A';
2547 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'B';
2548 // HACK: Make Alt + down-arrow behave like Ctrl + down-arrow
2549 // http://unix.stackexchange.com/a/108106
2550 if (result
.key
== '\x1b[1;3B') {
2551 result
.key
= '\x1b[1;5B';
2553 } else if (this.applicationCursor
) {
2554 result
.key
= '\x1bOB';
2556 result
.key
= '\x1b[B';
2561 if (!ev
.shiftKey
&& !ev
.ctrlKey
) {
2562 // <Ctrl> or <Shift> + <Insert> are used to
2563 // copy-paste on some systems.
2564 result
.key
= '\x1b[2~';
2570 result
.key
= '\x1b[3;' + (modifiers
+ 1) + '~';
2572 result
.key
= '\x1b[3~';
2578 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'H';
2579 else if (this.applicationCursor
)
2580 result
.key
= '\x1bOH';
2582 result
.key
= '\x1b[H';
2587 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'F';
2588 else if (this.applicationCursor
)
2589 result
.key
= '\x1bOF';
2591 result
.key
= '\x1b[F';
2596 result
.scrollDisp
= -(this.rows
- 1);
2598 result
.key
= '\x1b[5~';
2604 result
.scrollDisp
= this.rows
- 1;
2606 result
.key
= '\x1b[6~';
2612 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'P';
2614 result
.key
= '\x1bOP';
2619 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'Q';
2621 result
.key
= '\x1bOQ';
2626 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'R';
2628 result
.key
= '\x1bOR';
2633 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'S';
2635 result
.key
= '\x1bOS';
2640 result
.key
= '\x1b[15;' + (modifiers
+ 1) + '~';
2642 result
.key
= '\x1b[15~';
2647 result
.key
= '\x1b[17;' + (modifiers
+ 1) + '~';
2649 result
.key
= '\x1b[17~';
2654 result
.key
= '\x1b[18;' + (modifiers
+ 1) + '~';
2656 result
.key
= '\x1b[18~';
2661 result
.key
= '\x1b[19;' + (modifiers
+ 1) + '~';
2663 result
.key
= '\x1b[19~';
2668 result
.key
= '\x1b[20;' + (modifiers
+ 1) + '~';
2670 result
.key
= '\x1b[20~';
2675 result
.key
= '\x1b[21;' + (modifiers
+ 1) + '~';
2677 result
.key
= '\x1b[21~';
2682 result
.key
= '\x1b[23;' + (modifiers
+ 1) + '~';
2684 result
.key
= '\x1b[23~';
2689 result
.key
= '\x1b[24;' + (modifiers
+ 1) + '~';
2691 result
.key
= '\x1b[24~';
2696 if (ev
.ctrlKey
&& !ev
.shiftKey
&& !ev
.altKey
&& !ev
.metaKey
) {
2697 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2698 result
.key
= String
.fromCharCode(ev
.keyCode
- 64);
2699 } else if (ev
.keyCode
=== 32) {
2701 result
.key
= String
.fromCharCode(0);
2702 } else if (ev
.keyCode
>= 51 && ev
.keyCode
<= 55) {
2703 // escape, file sep, group sep, record sep, unit sep
2704 result
.key
= String
.fromCharCode(ev
.keyCode
- 51 + 27);
2705 } else if (ev
.keyCode
=== 56) {
2707 result
.key
= String
.fromCharCode(127);
2708 } else if (ev
.keyCode
=== 219) {
2709 // ^[ - Control Sequence Introducer (CSI)
2710 result
.key
= String
.fromCharCode(27);
2711 } else if (ev
.keyCode
=== 220) {
2712 // ^\ - String Terminator (ST)
2713 result
.key
= String
.fromCharCode(28);
2714 } else if (ev
.keyCode
=== 221) {
2715 // ^] - Operating System Command (OSC)
2716 result
.key
= String
.fromCharCode(29);
2718 } else if (!this.browser
.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) {
2719 // On Mac this is a third level shift. Use <Esc> instead.
2720 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2721 result
.key
= '\x1b' + String
.fromCharCode(ev
.keyCode
+ 32);
2722 } else if (ev
.keyCode
=== 192) {
2723 result
.key
= '\x1b`';
2724 } else if (ev
.keyCode
>= 48 && ev
.keyCode
<= 57) {
2725 result
.key
= '\x1b' + (ev
.keyCode
- 48);
2734 * Set the G level of the terminal
2737 Terminal
.prototype.setgLevel = function(g
) {
2739 this.charset
= this.charsets
[g
];
2743 * Set the charset for the given G level of the terminal
2747 Terminal
.prototype.setgCharset = function(g
, charset
) {
2748 this.charsets
[g
] = charset
;
2749 if (this.glevel
=== g
) {
2750 this.charset
= charset
;
2755 * Handle a keypress event.
2757 * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
2758 * @param {KeyboardEvent} ev The keypress event to be handled.
2760 Terminal
.prototype.keyPress = function(ev
) {
2767 } else if (ev
.which
== null) {
2769 } else if (ev
.which
!== 0 && ev
.charCode
!== 0) {
2776 (ev
.altKey
|| ev
.ctrlKey
|| ev
.metaKey
) && !isThirdLevelShift(this, ev
)
2781 key
= String
.fromCharCode(key
);
2783 this.emit('keypress', key
, ev
);
2784 this.emit('key', key
, ev
);
2792 * Send data for handling to the terminal
2793 * @param {string} data
2795 Terminal
.prototype.send = function(data
) {
2799 setTimeout(function() {
2800 self
.handler(self
.queue
);
2810 * Note: We could do sweet things with webaudio here
2812 Terminal
.prototype.bell = function() {
2813 if (!this.visualBell
) return;
2815 this.element
.style
.borderColor
= 'white';
2816 setTimeout(function() {
2817 self
.element
.style
.borderColor
= '';
2819 if (this.popOnBell
) this.focus();
2823 * Log the current state to the console.
2825 Terminal
.prototype.log = function() {
2826 if (!this.debug
) return;
2827 if (!this.context
.console
|| !this.context
.console
.log
) return;
2828 var args
= Array
.prototype.slice
.call(arguments
);
2829 this.context
.console
.log
.apply(this.context
.console
, args
);
2833 * Log the current state as error to the console.
2835 Terminal
.prototype.error = function() {
2836 if (!this.debug
) return;
2837 if (!this.context
.console
|| !this.context
.console
.error
) return;
2838 var args
= Array
.prototype.slice
.call(arguments
);
2839 this.context
.console
.error
.apply(this.context
.console
, args
);
2843 * Resizes the terminal.
2845 * @param {number} x The number of columns to resize to.
2846 * @param {number} y The number of rows to resize to.
2848 Terminal
.prototype.resize = function(x
, y
) {
2856 if (x
=== this.cols
&& y
=== this.rows
) {
2866 ch
= [this.defAttr
, ' ', 1]; // does xterm use the default attr?
2867 i
= this.lines
.length
;
2869 while (this.lines
[i
].length
< x
) {
2870 this.lines
[i
].push(ch
);
2874 i
= this.lines
.length
;
2876 while (this.lines
[i
].length
> x
) {
2877 this.lines
[i
].pop();
2890 // y is rows, not this.y
2891 if (this.lines
.length
< y
+ this.ybase
) {
2892 if (this.ybase
> 0 && this.lines
.length
<= this.ybase
+ this.y
+ addToY
+ 1) {
2893 // There is room above the buffer and there are no empty elements below the line,
2897 if (this.ydisp
> 0) {
2898 // Viewport is at the top of the buffer, must increase downwards
2902 // Add a blank line if there is no buffer left at the top to scroll to, or if there
2903 // are blank lines after the cursor
2904 this.lines
.push(this.blankLine());
2907 if (this.children
.length
< y
) {
2913 if (this.lines
.length
> y
+ this.ybase
) {
2914 if (this.lines
.length
> this.ybase
+ this.y
+ 1) {
2915 // The line is a blank line below the cursor, remove it
2918 // The line is the cursor, scroll down
2923 if (this.children
.length
> y
) {
2924 el
= this.children
.shift();
2926 el
.parentNode
.removeChild(el
);
2932 // Make sure that the cursor stays on screen
2945 this.scrollBottom
= y
- 1;
2947 this.refresh(0, this.rows
- 1);
2951 this.geometry
= [this.cols
, this.rows
];
2952 this.emit('resize', {terminal
: this, cols
: x
, rows
: y
});
2956 * Updates the range of rows to refresh
2957 * @param {number} y The number of rows to refresh next.
2959 Terminal
.prototype.updateRange = function(y
) {
2960 if (y
< this.refreshStart
) this.refreshStart
= y
;
2961 if (y
> this.refreshEnd
) this.refreshEnd
= y
;
2962 // if (y > this.refreshEnd) {
2963 // this.refreshEnd = y;
2964 // if (y > this.rows - 1) {
2965 // this.refreshEnd = this.rows - 1;
2971 * Set the range of refreshing to the maximum value
2973 Terminal
.prototype.maxRange = function() {
2974 this.refreshStart
= 0;
2975 this.refreshEnd
= this.rows
- 1;
2981 * Setup the tab stops.
2984 Terminal
.prototype.setupStops = function(i
) {
2986 if (!this.tabs
[i
]) {
2987 i
= this.prevStop(i
);
2994 for (; i
< this.cols
; i
+= 8) {
2995 this.tabs
[i
] = true;
3001 * Move the cursor to the previous tab stop from the given position (default is current).
3002 * @param {number} x The position to move the cursor to the previous tab stop.
3004 Terminal
.prototype.prevStop = function(x
) {
3005 if (x
== null) x
= this.x
;
3006 while (!this.tabs
[--x
] && x
> 0);
3007 return x
>= this.cols
3014 * Move the cursor one tab stop forward from the given position (default is current).
3015 * @param {number} x The position to move the cursor one tab stop forward.
3017 Terminal
.prototype.nextStop = function(x
) {
3018 if (x
== null) x
= this.x
;
3019 while (!this.tabs
[++x
] && x
< this.cols
);
3020 return x
>= this.cols
3027 * Erase in the identified line everything from "x" to the end of the line (right).
3028 * @param {number} x The column from which to start erasing to the end of the line.
3029 * @param {number} y The line in which to operate.
3031 Terminal
.prototype.eraseRight = function(x
, y
) {
3032 var line
= this.lines
[this.ybase
+ y
]
3033 , ch
= [this.eraseAttr(), ' ', 1]; // xterm
3036 for (; x
< this.cols
; x
++) {
3040 this.updateRange(y
);
3046 * Erase in the identified line everything from "x" to the start of the line (left).
3047 * @param {number} x The column from which to start erasing to the start of the line.
3048 * @param {number} y The line in which to operate.
3050 Terminal
.prototype.eraseLeft = function(x
, y
) {
3051 var line
= this.lines
[this.ybase
+ y
]
3052 , ch
= [this.eraseAttr(), ' ', 1]; // xterm
3055 while (x
--) line
[x
] = ch
;
3057 this.updateRange(y
);
3061 * Clears the entire buffer, making the prompt line the new first line.
3063 Terminal
.prototype.clear = function() {
3064 if (this.ybase
=== 0 && this.y
=== 0) {
3065 // Don't clear if it's already clear
3068 this.lines
= [this.lines
[this.ybase
+ this.y
]];
3072 for (var i
= 1; i
< this.rows
; i
++) {
3073 this.lines
.push(this.blankLine());
3075 this.refresh(0, this.rows
- 1);
3076 this.emit('scroll', this.ydisp
);
3080 * Erase all content in the given line
3081 * @param {number} y The line to erase all of its contents.
3083 Terminal
.prototype.eraseLine = function(y
) {
3084 this.eraseRight(0, y
);
3089 * Return the data array of a blank line/
3090 * @param {number} cur First bunch of data for each "blank" character.
3092 Terminal
.prototype.blankLine = function(cur
) {
3097 var ch
= [attr
, ' ', 1] // width defaults to 1 halfwidth character
3101 for (; i
< this.cols
; i
++) {
3110 * If cur return the back color xterm feature attribute. Else return defAttr.
3111 * @param {object} cur
3113 Terminal
.prototype.ch = function(cur
) {
3115 ? [this.eraseAttr(), ' ', 1]
3116 : [this.defAttr
, ' ', 1];
3121 * Evaluate if the current erminal is the given argument.
3122 * @param {object} term The terminal to evaluate
3124 Terminal
.prototype.is = function(term
) {
3125 var name
= this.termName
;
3126 return (name
+ '').indexOf(term
) === 0;
3131 * Emit the 'data' event and populate the given data.
3132 * @param {string} data The data to populate in the event.
3134 Terminal
.prototype.handler = function(data
) {
3135 // Input is being sent to the terminal, the terminal should focus the prompt.
3136 if (this.ybase
!== this.ydisp
) {
3137 this.scrollToBottom();
3139 this.emit('data', data
);
3144 * Emit the 'title' event and populate the given title.
3145 * @param {string} title The title to populate in the event.
3147 Terminal
.prototype.handleTitle = function(title
) {
3149 * This event is emitted when the title of the terminal is changed
3150 * from inside the terminal. The parameter is the new title.
3154 this.emit('title', title
);
3163 * ESC D Index (IND is 0x84).
3165 Terminal
.prototype.index = function() {
3167 if (this.y
> this.scrollBottom
) {
3171 this.state
= normal
;
3176 * ESC M Reverse Index (RI is 0x8d).
3178 Terminal
.prototype.reverseIndex = function() {
3181 if (this.y
< this.scrollTop
) {
3183 // possibly move the code below to term.reverseScroll();
3184 // test: echo -ne '\e[1;1H\e[44m\eM\e[0m'
3185 // blankLine(true) is xterm/linux behavior
3186 this.lines
.splice(this.y
+ this.ybase
, 0, this.blankLine(true));
3187 j
= this.rows
- 1 - this.scrollBottom
;
3188 this.lines
.splice(this.rows
- 1 + this.ybase
- j
+ 1, 1);
3190 this.updateRange(this.scrollTop
);
3191 this.updateRange(this.scrollBottom
);
3193 this.state
= normal
;
3198 * ESC c Full Reset (RIS).
3200 Terminal
.prototype.reset = function() {
3201 this.options
.rows
= this.rows
;
3202 this.options
.cols
= this.cols
;
3203 var customKeydownHandler
= this.customKeydownHandler
;
3204 Terminal
.call(this, this.options
);
3205 this.customKeydownHandler
= customKeydownHandler
;
3206 this.refresh(0, this.rows
- 1);
3207 this.viewport
.syncScrollArea();
3212 * ESC H Tab Set (HTS is 0x88).
3214 Terminal
.prototype.tabSet = function() {
3215 this.tabs
[this.x
] = true;
3216 this.state
= normal
;
3226 * Cursor Up Ps Times (default = 1) (CUU).
3228 Terminal
.prototype.cursorUp = function(params
) {
3229 var param
= params
[0];
3230 if (param
< 1) param
= 1;
3232 if (this.y
< 0) this.y
= 0;
3238 * Cursor Down Ps Times (default = 1) (CUD).
3240 Terminal
.prototype.cursorDown = function(params
) {
3241 var param
= params
[0];
3242 if (param
< 1) param
= 1;
3244 if (this.y
>= this.rows
) {
3245 this.y
= this.rows
- 1;
3252 * Cursor Forward Ps Times (default = 1) (CUF).
3254 Terminal
.prototype.cursorForward = function(params
) {
3255 var param
= params
[0];
3256 if (param
< 1) param
= 1;
3258 if (this.x
>= this.cols
) {
3259 this.x
= this.cols
- 1;
3266 * Cursor Backward Ps Times (default = 1) (CUB).
3268 Terminal
.prototype.cursorBackward = function(params
) {
3269 var param
= params
[0];
3270 if (param
< 1) param
= 1;
3272 if (this.x
< 0) this.x
= 0;
3278 * Cursor Position [row;column] (default = [1,1]) (CUP).
3280 Terminal
.prototype.cursorPos = function(params
) {
3283 row
= params
[0] - 1;
3285 if (params
.length
>= 2) {
3286 col
= params
[1] - 1;
3293 } else if (row
>= this.rows
) {
3294 row
= this.rows
- 1;
3299 } else if (col
>= this.cols
) {
3300 col
= this.cols
- 1;
3309 * CSI Ps J Erase in Display (ED).
3310 * Ps = 0 -> Erase Below (default).
3311 * Ps = 1 -> Erase Above.
3312 * Ps = 2 -> Erase All.
3313 * Ps = 3 -> Erase Saved Lines (xterm).
3315 * Erase in Display (DECSED).
3316 * Ps = 0 -> Selective Erase Below (default).
3317 * Ps = 1 -> Selective Erase Above.
3318 * Ps = 2 -> Selective Erase All.
3320 Terminal
.prototype.eraseInDisplay = function(params
) {
3322 switch (params
[0]) {
3324 this.eraseRight(this.x
, this.y
);
3326 for (; j
< this.rows
; j
++) {
3331 this.eraseLeft(this.x
, this.y
);
3339 while (j
--) this.eraseLine(j
);
3349 * CSI Ps K Erase in Line (EL).
3350 * Ps = 0 -> Erase to Right (default).
3351 * Ps = 1 -> Erase to Left.
3352 * Ps = 2 -> Erase All.
3354 * Erase in Line (DECSEL).
3355 * Ps = 0 -> Selective Erase to Right (default).
3356 * Ps = 1 -> Selective Erase to Left.
3357 * Ps = 2 -> Selective Erase All.
3359 Terminal
.prototype.eraseInLine = function(params
) {
3360 switch (params
[0]) {
3362 this.eraseRight(this.x
, this.y
);
3365 this.eraseLeft(this.x
, this.y
);
3368 this.eraseLine(this.y
);
3375 * CSI Pm m Character Attributes (SGR).
3376 * Ps = 0 -> Normal (default).
3378 * Ps = 4 -> Underlined.
3379 * Ps = 5 -> Blink (appears as Bold).
3380 * Ps = 7 -> Inverse.
3381 * Ps = 8 -> Invisible, i.e., hidden (VT300).
3382 * Ps = 2 2 -> Normal (neither bold nor faint).
3383 * Ps = 2 4 -> Not underlined.
3384 * Ps = 2 5 -> Steady (not blinking).
3385 * Ps = 2 7 -> Positive (not inverse).
3386 * Ps = 2 8 -> Visible, i.e., not hidden (VT300).
3387 * Ps = 3 0 -> Set foreground color to Black.
3388 * Ps = 3 1 -> Set foreground color to Red.
3389 * Ps = 3 2 -> Set foreground color to Green.
3390 * Ps = 3 3 -> Set foreground color to Yellow.
3391 * Ps = 3 4 -> Set foreground color to Blue.
3392 * Ps = 3 5 -> Set foreground color to Magenta.
3393 * Ps = 3 6 -> Set foreground color to Cyan.
3394 * Ps = 3 7 -> Set foreground color to White.
3395 * Ps = 3 9 -> Set foreground color to default (original).
3396 * Ps = 4 0 -> Set background color to Black.
3397 * Ps = 4 1 -> Set background color to Red.
3398 * Ps = 4 2 -> Set background color to Green.
3399 * Ps = 4 3 -> Set background color to Yellow.
3400 * Ps = 4 4 -> Set background color to Blue.
3401 * Ps = 4 5 -> Set background color to Magenta.
3402 * Ps = 4 6 -> Set background color to Cyan.
3403 * Ps = 4 7 -> Set background color to White.
3404 * Ps = 4 9 -> Set background color to default (original).
3406 * If 16-color support is compiled, the following apply. Assume
3407 * that xterm's resources are set so that the ISO color codes are
3408 * the first 8 of a set of 16. Then the aixterm colors are the
3409 * bright versions of the ISO colors:
3410 * Ps = 9 0 -> Set foreground color to Black.
3411 * Ps = 9 1 -> Set foreground color to Red.
3412 * Ps = 9 2 -> Set foreground color to Green.
3413 * Ps = 9 3 -> Set foreground color to Yellow.
3414 * Ps = 9 4 -> Set foreground color to Blue.
3415 * Ps = 9 5 -> Set foreground color to Magenta.
3416 * Ps = 9 6 -> Set foreground color to Cyan.
3417 * Ps = 9 7 -> Set foreground color to White.
3418 * Ps = 1 0 0 -> Set background color to Black.
3419 * Ps = 1 0 1 -> Set background color to Red.
3420 * Ps = 1 0 2 -> Set background color to Green.
3421 * Ps = 1 0 3 -> Set background color to Yellow.
3422 * Ps = 1 0 4 -> Set background color to Blue.
3423 * Ps = 1 0 5 -> Set background color to Magenta.
3424 * Ps = 1 0 6 -> Set background color to Cyan.
3425 * Ps = 1 0 7 -> Set background color to White.
3427 * If xterm is compiled with the 16-color support disabled, it
3428 * supports the following, from rxvt:
3429 * Ps = 1 0 0 -> Set foreground and background color to
3432 * If 88- or 256-color support is compiled, the following apply.
3433 * Ps = 3 8 ; 5 ; Ps -> Set foreground color to the second
3435 * Ps = 4 8 ; 5 ; Ps -> Set background color to the second
3438 Terminal
.prototype.charAttributes = function(params
) {
3439 // Optimize a single SGR0.
3440 if (params
.length
=== 1 && params
[0] === 0) {
3441 this.curAttr
= this.defAttr
;
3445 var l
= params
.length
3447 , flags
= this.curAttr
>> 18
3448 , fg
= (this.curAttr
>> 9) & 0x1ff
3449 , bg
= this.curAttr
& 0x1ff
3452 for (; i
< l
; i
++) {
3454 if (p
>= 30 && p
<= 37) {
3457 } else if (p
>= 40 && p
<= 47) {
3460 } else if (p
>= 90 && p
<= 97) {
3464 } else if (p
>= 100 && p
<= 107) {
3468 } else if (p
=== 0) {
3470 flags
= this.defAttr
>> 18;
3471 fg
= (this.defAttr
>> 9) & 0x1ff;
3472 bg
= this.defAttr
& 0x1ff;
3476 } else if (p
=== 1) {
3479 } else if (p
=== 4) {
3482 } else if (p
=== 5) {
3485 } else if (p
=== 7) {
3486 // inverse and positive
3487 // test with: echo -e '\e[31m\e[42mhello\e[7mworld\e[27mhi\e[m'
3489 } else if (p
=== 8) {
3492 } else if (p
=== 22) {
3495 } else if (p
=== 24) {
3498 } else if (p
=== 25) {
3501 } else if (p
=== 27) {
3504 } else if (p
=== 28) {
3507 } else if (p
=== 39) {
3509 fg
= (this.defAttr
>> 9) & 0x1ff;
3510 } else if (p
=== 49) {
3512 bg
= this.defAttr
& 0x1ff;
3513 } else if (p
=== 38) {
3515 if (params
[i
+ 1] === 2) {
3519 params
[i
+ 1] & 0xff,
3520 params
[i
+ 2] & 0xff);
3521 if (fg
=== -1) fg
= 0x1ff;
3523 } else if (params
[i
+ 1] === 5) {
3525 p
= params
[i
] & 0xff;
3528 } else if (p
=== 48) {
3530 if (params
[i
+ 1] === 2) {
3534 params
[i
+ 1] & 0xff,
3535 params
[i
+ 2] & 0xff);
3536 if (bg
=== -1) bg
= 0x1ff;
3538 } else if (params
[i
+ 1] === 5) {
3540 p
= params
[i
] & 0xff;
3543 } else if (p
=== 100) {
3545 fg
= (this.defAttr
>> 9) & 0x1ff;
3546 bg
= this.defAttr
& 0x1ff;
3548 this.error('Unknown SGR attribute: %d.', p
);
3552 this.curAttr
= (flags
<< 18) | (fg
<< 9) | bg
;
3557 * CSI Ps n Device Status Report (DSR).
3558 * Ps = 5 -> Status Report. Result (``OK'') is
3560 * Ps = 6 -> Report Cursor Position (CPR) [row;column].
3564 * Device Status Report (DSR, DEC-specific).
3565 * Ps = 6 -> Report Cursor Position (CPR) [row;column] as CSI
3566 * ? r ; c R (assumes page is zero).
3567 * Ps = 1 5 -> Report Printer status as CSI ? 1 0 n (ready).
3568 * or CSI ? 1 1 n (not ready).
3569 * Ps = 2 5 -> Report UDK status as CSI ? 2 0 n (unlocked)
3570 * or CSI ? 2 1 n (locked).
3571 * Ps = 2 6 -> Report Keyboard status as
3572 * CSI ? 2 7 ; 1 ; 0 ; 0 n (North American).
3573 * The last two parameters apply to VT400 & up, and denote key-
3574 * board ready and LK01 respectively.
3575 * Ps = 5 3 -> Report Locator status as
3576 * CSI ? 5 3 n Locator available, if compiled-in, or
3577 * CSI ? 5 0 n No Locator, if not.
3579 Terminal
.prototype.deviceStatus = function(params
) {
3581 switch (params
[0]) {
3584 this.send('\x1b[0n');
3595 } else if (this.prefix
=== '?') {
3596 // modern xterm doesnt seem to
3597 // respond to any of these except ?6, 6, and 5
3598 switch (params
[0]) {
3609 // this.send('\x1b[?11n');
3612 // dont support user defined keys
3613 // this.send('\x1b[?21n');
3616 // north american keyboard
3617 // this.send('\x1b[?27;1;0;0n');
3620 // no dec locator/mouse
3621 // this.send('\x1b[?50n');
3634 * Insert Ps (Blank) Character(s) (default = 1) (ICH).
3636 Terminal
.prototype.insertChars = function(params
) {
3637 var param
, row
, j
, ch
;
3640 if (param
< 1) param
= 1;
3642 row
= this.y
+ this.ybase
;
3644 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3646 while (param
-- && j
< this.cols
) {
3647 this.lines
[row
].splice(j
++, 0, ch
);
3648 this.lines
[row
].pop();
3654 * Cursor Next Line Ps Times (default = 1) (CNL).
3655 * same as CSI Ps B ?
3657 Terminal
.prototype.cursorNextLine = function(params
) {
3658 var param
= params
[0];
3659 if (param
< 1) param
= 1;
3661 if (this.y
>= this.rows
) {
3662 this.y
= this.rows
- 1;
3670 * Cursor Preceding Line Ps Times (default = 1) (CNL).
3673 Terminal
.prototype.cursorPrecedingLine = function(params
) {
3674 var param
= params
[0];
3675 if (param
< 1) param
= 1;
3677 if (this.y
< 0) this.y
= 0;
3684 * Cursor Character Absolute [column] (default = [row,1]) (CHA).
3686 Terminal
.prototype.cursorCharAbsolute = function(params
) {
3687 var param
= params
[0];
3688 if (param
< 1) param
= 1;
3695 * Insert Ps Line(s) (default = 1) (IL).
3697 Terminal
.prototype.insertLines = function(params
) {
3701 if (param
< 1) param
= 1;
3702 row
= this.y
+ this.ybase
;
3704 j
= this.rows
- 1 - this.scrollBottom
;
3705 j
= this.rows
- 1 + this.ybase
- j
+ 1;
3708 // test: echo -e '\e[44m\e[1L\e[0m'
3709 // blankLine(true) - xterm/linux behavior
3710 this.lines
.splice(row
, 0, this.blankLine(true));
3711 this.lines
.splice(j
, 1);
3715 this.updateRange(this.y
);
3716 this.updateRange(this.scrollBottom
);
3722 * Delete Ps Line(s) (default = 1) (DL).
3724 Terminal
.prototype.deleteLines = function(params
) {
3728 if (param
< 1) param
= 1;
3729 row
= this.y
+ this.ybase
;
3731 j
= this.rows
- 1 - this.scrollBottom
;
3732 j
= this.rows
- 1 + this.ybase
- j
;
3735 // test: echo -e '\e[44m\e[1M\e[0m'
3736 // blankLine(true) - xterm/linux behavior
3737 this.lines
.splice(j
+ 1, 0, this.blankLine(true));
3738 this.lines
.splice(row
, 1);
3742 this.updateRange(this.y
);
3743 this.updateRange(this.scrollBottom
);
3749 * Delete Ps Character(s) (default = 1) (DCH).
3751 Terminal
.prototype.deleteChars = function(params
) {
3755 if (param
< 1) param
= 1;
3757 row
= this.y
+ this.ybase
;
3758 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3761 this.lines
[row
].splice(this.x
, 1);
3762 this.lines
[row
].push(ch
);
3768 * Erase Ps Character(s) (default = 1) (ECH).
3770 Terminal
.prototype.eraseChars = function(params
) {
3771 var param
, row
, j
, ch
;
3774 if (param
< 1) param
= 1;
3776 row
= this.y
+ this.ybase
;
3778 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3780 while (param
-- && j
< this.cols
) {
3781 this.lines
[row
][j
++] = ch
;
3786 * CSI Pm ` Character Position Absolute
3787 * [column] (default = [row,1]) (HPA).
3789 Terminal
.prototype.charPosAbsolute = function(params
) {
3790 var param
= params
[0];
3791 if (param
< 1) param
= 1;
3793 if (this.x
>= this.cols
) {
3794 this.x
= this.cols
- 1;
3801 * Horizontal Position Relative
3804 Terminal
.prototype.HPositionRelative = function(params
) {
3805 var param
= params
[0];
3806 if (param
< 1) param
= 1;
3808 if (this.x
>= this.cols
) {
3809 this.x
= this.cols
- 1;
3815 * CSI Ps c Send Device Attributes (Primary DA).
3816 * Ps = 0 or omitted -> request attributes from terminal. The
3817 * response depends on the decTerminalID resource setting.
3818 * -> CSI ? 1 ; 2 c (``VT100 with Advanced Video Option'')
3819 * -> CSI ? 1 ; 0 c (``VT101 with No Options'')
3820 * -> CSI ? 6 c (``VT102'')
3821 * -> CSI ? 6 0 ; 1 ; 2 ; 6 ; 8 ; 9 ; 1 5 ; c (``VT220'')
3822 * The VT100-style response parameters do not mean anything by
3823 * themselves. VT220 parameters do, telling the host what fea-
3824 * tures the terminal supports:
3825 * Ps = 1 -> 132-columns.
3826 * Ps = 2 -> Printer.
3827 * Ps = 6 -> Selective erase.
3828 * Ps = 8 -> User-defined keys.
3829 * Ps = 9 -> National replacement character sets.
3830 * Ps = 1 5 -> Technical characters.
3831 * Ps = 2 2 -> ANSI color, e.g., VT525.
3832 * Ps = 2 9 -> ANSI text locator (i.e., DEC Locator mode).
3834 * Send Device Attributes (Secondary DA).
3835 * Ps = 0 or omitted -> request the terminal's identification
3836 * code. The response depends on the decTerminalID resource set-
3837 * ting. It should apply only to VT220 and up, but xterm extends
3839 * -> CSI > Pp ; Pv ; Pc c
3840 * where Pp denotes the terminal type
3841 * Pp = 0 -> ``VT100''.
3842 * Pp = 1 -> ``VT220''.
3843 * and Pv is the firmware version (for xterm, this was originally
3844 * the XFree86 patch number, starting with 95). In a DEC termi-
3845 * nal, Pc indicates the ROM cartridge registration number and is
3848 * xterm/charproc.c - line 2012, for more information.
3849 * vim responds with ^[[?0c or ^[[?1c after the terminal's response (?)
3851 Terminal
.prototype.sendDeviceAttributes = function(params
) {
3852 if (params
[0] > 0) return;
3855 if (this.is('xterm')
3856 || this.is('rxvt-unicode')
3857 || this.is('screen')) {
3858 this.send('\x1b[?1;2c');
3859 } else if (this.is('linux')) {
3860 this.send('\x1b[?6c');
3862 } else if (this.prefix
=== '>') {
3864 // seem to spit this
3865 // out around ~370 times (?).
3866 if (this.is('xterm')) {
3867 this.send('\x1b[>0;276;0c');
3868 } else if (this.is('rxvt-unicode')) {
3869 this.send('\x1b[>85;95;0c');
3870 } else if (this.is('linux')) {
3871 // not supported by linux console.
3872 // linux console echoes parameters.
3873 this.send(params
[0] + 'c');
3874 } else if (this.is('screen')) {
3875 this.send('\x1b[>83;40003;0c');
3883 * Line Position Absolute [row] (default = [1,column]) (VPA).
3885 Terminal
.prototype.linePosAbsolute = function(params
) {
3886 var param
= params
[0];
3887 if (param
< 1) param
= 1;
3889 if (this.y
>= this.rows
) {
3890 this.y
= this.rows
- 1;
3896 * 145 65 e * VPR - Vertical Position Relative
3899 Terminal
.prototype.VPositionRelative = function(params
) {
3900 var param
= params
[0];
3901 if (param
< 1) param
= 1;
3903 if (this.y
>= this.rows
) {
3904 this.y
= this.rows
- 1;
3911 * Horizontal and Vertical Position [row;column] (default =
3914 Terminal
.prototype.HVPosition = function(params
) {
3915 if (params
[0] < 1) params
[0] = 1;
3916 if (params
[1] < 1) params
[1] = 1;
3918 this.y
= params
[0] - 1;
3919 if (this.y
>= this.rows
) {
3920 this.y
= this.rows
- 1;
3923 this.x
= params
[1] - 1;
3924 if (this.x
>= this.cols
) {
3925 this.x
= this.cols
- 1;
3931 * CSI Pm h Set Mode (SM).
3932 * Ps = 2 -> Keyboard Action Mode (AM).
3933 * Ps = 4 -> Insert Mode (IRM).
3934 * Ps = 1 2 -> Send/receive (SRM).
3935 * Ps = 2 0 -> Automatic Newline (LNM).
3937 * DEC Private Mode Set (DECSET).
3938 * Ps = 1 -> Application Cursor Keys (DECCKM).
3939 * Ps = 2 -> Designate USASCII for character sets G0-G3
3940 * (DECANM), and set VT100 mode.
3941 * Ps = 3 -> 132 Column Mode (DECCOLM).
3942 * Ps = 4 -> Smooth (Slow) Scroll (DECSCLM).
3943 * Ps = 5 -> Reverse Video (DECSCNM).
3944 * Ps = 6 -> Origin Mode (DECOM).
3945 * Ps = 7 -> Wraparound Mode (DECAWM).
3946 * Ps = 8 -> Auto-repeat Keys (DECARM).
3947 * Ps = 9 -> Send Mouse X & Y on button press. See the sec-
3948 * tion Mouse Tracking.
3949 * Ps = 1 0 -> Show toolbar (rxvt).
3950 * Ps = 1 2 -> Start Blinking Cursor (att610).
3951 * Ps = 1 8 -> Print form feed (DECPFF).
3952 * Ps = 1 9 -> Set print extent to full screen (DECPEX).
3953 * Ps = 2 5 -> Show Cursor (DECTCEM).
3954 * Ps = 3 0 -> Show scrollbar (rxvt).
3955 * Ps = 3 5 -> Enable font-shifting functions (rxvt).
3956 * Ps = 3 8 -> Enter Tektronix Mode (DECTEK).
3957 * Ps = 4 0 -> Allow 80 -> 132 Mode.
3958 * Ps = 4 1 -> more(1) fix (see curses resource).
3959 * Ps = 4 2 -> Enable Nation Replacement Character sets (DECN-
3961 * Ps = 4 4 -> Turn On Margin Bell.
3962 * Ps = 4 5 -> Reverse-wraparound Mode.
3963 * Ps = 4 6 -> Start Logging. This is normally disabled by a
3964 * compile-time option.
3965 * Ps = 4 7 -> Use Alternate Screen Buffer. (This may be dis-
3966 * abled by the titeInhibit resource).
3967 * Ps = 6 6 -> Application keypad (DECNKM).
3968 * Ps = 6 7 -> Backarrow key sends backspace (DECBKM).
3969 * Ps = 1 0 0 0 -> Send Mouse X & Y on button press and
3970 * release. See the section Mouse Tracking.
3971 * Ps = 1 0 0 1 -> Use Hilite Mouse Tracking.
3972 * Ps = 1 0 0 2 -> Use Cell Motion Mouse Tracking.
3973 * Ps = 1 0 0 3 -> Use All Motion Mouse Tracking.
3974 * Ps = 1 0 0 4 -> Send FocusIn/FocusOut events.
3975 * Ps = 1 0 0 5 -> Enable Extended Mouse Mode.
3976 * Ps = 1 0 1 0 -> Scroll to bottom on tty output (rxvt).
3977 * Ps = 1 0 1 1 -> Scroll to bottom on key press (rxvt).
3978 * Ps = 1 0 3 4 -> Interpret "meta" key, sets eighth bit.
3979 * (enables the eightBitInput resource).
3980 * Ps = 1 0 3 5 -> Enable special modifiers for Alt and Num-
3981 * Lock keys. (This enables the numLock resource).
3982 * Ps = 1 0 3 6 -> Send ESC when Meta modifies a key. (This
3983 * enables the metaSendsEscape resource).
3984 * Ps = 1 0 3 7 -> Send DEL from the editing-keypad Delete
3986 * Ps = 1 0 3 9 -> Send ESC when Alt modifies a key. (This
3987 * enables the altSendsEscape resource).
3988 * Ps = 1 0 4 0 -> Keep selection even if not highlighted.
3989 * (This enables the keepSelection resource).
3990 * Ps = 1 0 4 1 -> Use the CLIPBOARD selection. (This enables
3991 * the selectToClipboard resource).
3992 * Ps = 1 0 4 2 -> Enable Urgency window manager hint when
3993 * Control-G is received. (This enables the bellIsUrgent
3995 * Ps = 1 0 4 3 -> Enable raising of the window when Control-G
3996 * is received. (enables the popOnBell resource).
3997 * Ps = 1 0 4 7 -> Use Alternate Screen Buffer. (This may be
3998 * disabled by the titeInhibit resource).
3999 * Ps = 1 0 4 8 -> Save cursor as in DECSC. (This may be dis-
4000 * abled by the titeInhibit resource).
4001 * Ps = 1 0 4 9 -> Save cursor as in DECSC and use Alternate
4002 * Screen Buffer, clearing it first. (This may be disabled by
4003 * the titeInhibit resource). This combines the effects of the 1
4004 * 0 4 7 and 1 0 4 8 modes. Use this with terminfo-based
4005 * applications rather than the 4 7 mode.
4006 * Ps = 1 0 5 0 -> Set terminfo/termcap function-key mode.
4007 * Ps = 1 0 5 1 -> Set Sun function-key mode.
4008 * Ps = 1 0 5 2 -> Set HP function-key mode.
4009 * Ps = 1 0 5 3 -> Set SCO function-key mode.
4010 * Ps = 1 0 6 0 -> Set legacy keyboard emulation (X11R6).
4011 * Ps = 1 0 6 1 -> Set VT220 keyboard emulation.
4012 * Ps = 2 0 0 4 -> Set bracketed paste mode.
4014 * http: *vt100.net/docs/vt220-rm/chapter4.html
4016 Terminal
.prototype.setMode = function(params
) {
4017 if (typeof params
=== 'object') {
4018 var l
= params
.length
4021 for (; i
< l
; i
++) {
4022 this.setMode(params
[i
]);
4031 this.insertMode
= true;
4034 //this.convertEol = true;
4037 } else if (this.prefix
=== '?') {
4040 this.applicationCursor
= true;
4043 this.setgCharset(0, Terminal
.charsets
.US
);
4044 this.setgCharset(1, Terminal
.charsets
.US
);
4045 this.setgCharset(2, Terminal
.charsets
.US
);
4046 this.setgCharset(3, Terminal
.charsets
.US
);
4047 // set VT100 mode here
4049 case 3: // 132 col mode
4050 this.savedCols
= this.cols
;
4051 this.resize(132, this.rows
);
4054 this.originMode
= true;
4057 this.wraparoundMode
= true;
4060 // this.cursorBlink = true;
4063 this.log('Serial port requested application keypad.');
4064 this.applicationKeypad
= true;
4065 this.viewport
.syncScrollArea();
4067 case 9: // X10 Mouse
4068 // no release, no motion, no wheel, no modifiers.
4069 case 1000: // vt200 mouse
4071 // no modifiers, except control on the wheel.
4072 case 1002: // button event mouse
4073 case 1003: // any event mouse
4074 // any event - sends motion events,
4075 // even if there is no button held down.
4076 this.x10Mouse
= params
=== 9;
4077 this.vt200Mouse
= params
=== 1000;
4078 this.normalMouse
= params
> 1000;
4079 this.mouseEvents
= true;
4080 this.element
.style
.cursor
= 'default';
4081 this.log('Binding to mouse events.');
4083 case 1004: // send focusin/focusout events
4086 this.sendFocus
= true;
4088 case 1005: // utf8 ext mode mouse
4089 this.utfMouse
= true;
4090 // for wide terminals
4091 // simply encodes large values as utf8 characters
4093 case 1006: // sgr ext mode mouse
4094 this.sgrMouse
= true;
4095 // for wide terminals
4096 // does not add 32 to fields
4097 // press: ^[[<b;x;yM
4098 // release: ^[[<b;x;ym
4100 case 1015: // urxvt ext mode mouse
4101 this.urxvtMouse
= true;
4102 // for wide terminals
4103 // numbers for fields
4105 // motion: ^[[b;x;yT
4107 case 25: // show cursor
4108 this.cursorHidden
= false;
4110 case 1049: // alt screen buffer cursor
4111 //this.saveCursor();
4113 case 47: // alt screen buffer
4114 case 1047: // alt screen buffer
4122 scrollTop
: this.scrollTop
,
4123 scrollBottom
: this.scrollBottom
,
4125 // XXX save charset(s) here?
4126 // charset: this.charset,
4127 // glevel: this.glevel,
4128 // charsets: this.charsets
4131 this.normal
= normal
;
4140 * CSI Pm l Reset Mode (RM).
4141 * Ps = 2 -> Keyboard Action Mode (AM).
4142 * Ps = 4 -> Replace Mode (IRM).
4143 * Ps = 1 2 -> Send/receive (SRM).
4144 * Ps = 2 0 -> Normal Linefeed (LNM).
4146 * DEC Private Mode Reset (DECRST).
4147 * Ps = 1 -> Normal Cursor Keys (DECCKM).
4148 * Ps = 2 -> Designate VT52 mode (DECANM).
4149 * Ps = 3 -> 80 Column Mode (DECCOLM).
4150 * Ps = 4 -> Jump (Fast) Scroll (DECSCLM).
4151 * Ps = 5 -> Normal Video (DECSCNM).
4152 * Ps = 6 -> Normal Cursor Mode (DECOM).
4153 * Ps = 7 -> No Wraparound Mode (DECAWM).
4154 * Ps = 8 -> No Auto-repeat Keys (DECARM).
4155 * Ps = 9 -> Don't send Mouse X & Y on button press.
4156 * Ps = 1 0 -> Hide toolbar (rxvt).
4157 * Ps = 1 2 -> Stop Blinking Cursor (att610).
4158 * Ps = 1 8 -> Don't print form feed (DECPFF).
4159 * Ps = 1 9 -> Limit print to scrolling region (DECPEX).
4160 * Ps = 2 5 -> Hide Cursor (DECTCEM).
4161 * Ps = 3 0 -> Don't show scrollbar (rxvt).
4162 * Ps = 3 5 -> Disable font-shifting functions (rxvt).
4163 * Ps = 4 0 -> Disallow 80 -> 132 Mode.
4164 * Ps = 4 1 -> No more(1) fix (see curses resource).
4165 * Ps = 4 2 -> Disable Nation Replacement Character sets (DEC-
4167 * Ps = 4 4 -> Turn Off Margin Bell.
4168 * Ps = 4 5 -> No Reverse-wraparound Mode.
4169 * Ps = 4 6 -> Stop Logging. (This is normally disabled by a
4170 * compile-time option).
4171 * Ps = 4 7 -> Use Normal Screen Buffer.
4172 * Ps = 6 6 -> Numeric keypad (DECNKM).
4173 * Ps = 6 7 -> Backarrow key sends delete (DECBKM).
4174 * Ps = 1 0 0 0 -> Don't send Mouse X & Y on button press and
4175 * release. See the section Mouse Tracking.
4176 * Ps = 1 0 0 1 -> Don't use Hilite Mouse Tracking.
4177 * Ps = 1 0 0 2 -> Don't use Cell Motion Mouse Tracking.
4178 * Ps = 1 0 0 3 -> Don't use All Motion Mouse Tracking.
4179 * Ps = 1 0 0 4 -> Don't send FocusIn/FocusOut events.
4180 * Ps = 1 0 0 5 -> Disable Extended Mouse Mode.
4181 * Ps = 1 0 1 0 -> Don't scroll to bottom on tty output
4183 * Ps = 1 0 1 1 -> Don't scroll to bottom on key press (rxvt).
4184 * Ps = 1 0 3 4 -> Don't interpret "meta" key. (This disables
4185 * the eightBitInput resource).
4186 * Ps = 1 0 3 5 -> Disable special modifiers for Alt and Num-
4187 * Lock keys. (This disables the numLock resource).
4188 * Ps = 1 0 3 6 -> Don't send ESC when Meta modifies a key.
4189 * (This disables the metaSendsEscape resource).
4190 * Ps = 1 0 3 7 -> Send VT220 Remove from the editing-keypad
4192 * Ps = 1 0 3 9 -> Don't send ESC when Alt modifies a key.
4193 * (This disables the altSendsEscape resource).
4194 * Ps = 1 0 4 0 -> Do not keep selection when not highlighted.
4195 * (This disables the keepSelection resource).
4196 * Ps = 1 0 4 1 -> Use the PRIMARY selection. (This disables
4197 * the selectToClipboard resource).
4198 * Ps = 1 0 4 2 -> Disable Urgency window manager hint when
4199 * Control-G is received. (This disables the bellIsUrgent
4201 * Ps = 1 0 4 3 -> Disable raising of the window when Control-
4202 * G is received. (This disables the popOnBell resource).
4203 * Ps = 1 0 4 7 -> Use Normal Screen Buffer, clearing screen
4204 * first if in the Alternate Screen. (This may be disabled by
4205 * the titeInhibit resource).
4206 * Ps = 1 0 4 8 -> Restore cursor as in DECRC. (This may be
4207 * disabled by the titeInhibit resource).
4208 * Ps = 1 0 4 9 -> Use Normal Screen Buffer and restore cursor
4209 * as in DECRC. (This may be disabled by the titeInhibit
4210 * resource). This combines the effects of the 1 0 4 7 and 1 0
4211 * 4 8 modes. Use this with terminfo-based applications rather
4212 * than the 4 7 mode.
4213 * Ps = 1 0 5 0 -> Reset terminfo/termcap function-key mode.
4214 * Ps = 1 0 5 1 -> Reset Sun function-key mode.
4215 * Ps = 1 0 5 2 -> Reset HP function-key mode.
4216 * Ps = 1 0 5 3 -> Reset SCO function-key mode.
4217 * Ps = 1 0 6 0 -> Reset legacy keyboard emulation (X11R6).
4218 * Ps = 1 0 6 1 -> Reset keyboard emulation to Sun/PC style.
4219 * Ps = 2 0 0 4 -> Reset bracketed paste mode.
4221 Terminal
.prototype.resetMode = function(params
) {
4222 if (typeof params
=== 'object') {
4223 var l
= params
.length
4226 for (; i
< l
; i
++) {
4227 this.resetMode(params
[i
]);
4236 this.insertMode
= false;
4239 //this.convertEol = false;
4242 } else if (this.prefix
=== '?') {
4245 this.applicationCursor
= false;
4248 if (this.cols
=== 132 && this.savedCols
) {
4249 this.resize(this.savedCols
, this.rows
);
4251 delete this.savedCols
;
4254 this.originMode
= false;
4257 this.wraparoundMode
= false;
4260 // this.cursorBlink = false;
4263 this.log('Switching back to normal keypad.');
4264 this.applicationKeypad
= false;
4265 this.viewport
.syncScrollArea();
4267 case 9: // X10 Mouse
4268 case 1000: // vt200 mouse
4269 case 1002: // button event mouse
4270 case 1003: // any event mouse
4271 this.x10Mouse
= false;
4272 this.vt200Mouse
= false;
4273 this.normalMouse
= false;
4274 this.mouseEvents
= false;
4275 this.element
.style
.cursor
= '';
4277 case 1004: // send focusin/focusout events
4278 this.sendFocus
= false;
4280 case 1005: // utf8 ext mode mouse
4281 this.utfMouse
= false;
4283 case 1006: // sgr ext mode mouse
4284 this.sgrMouse
= false;
4286 case 1015: // urxvt ext mode mouse
4287 this.urxvtMouse
= false;
4289 case 25: // hide cursor
4290 this.cursorHidden
= true;
4292 case 1049: // alt screen buffer cursor
4294 case 47: // normal screen buffer
4295 case 1047: // normal screen buffer - clearing it first
4297 this.lines
= this.normal
.lines
;
4298 this.ybase
= this.normal
.ybase
;
4299 this.ydisp
= this.normal
.ydisp
;
4300 this.x
= this.normal
.x
;
4301 this.y
= this.normal
.y
;
4302 this.scrollTop
= this.normal
.scrollTop
;
4303 this.scrollBottom
= this.normal
.scrollBottom
;
4304 this.tabs
= this.normal
.tabs
;
4306 // if (params === 1049) {
4307 // this.x = this.savedX;
4308 // this.y = this.savedY;
4310 this.refresh(0, this.rows
- 1);
4321 * Set Scrolling Region [top;bottom] (default = full size of win-
4325 Terminal
.prototype.setScrollRegion = function(params
) {
4326 if (this.prefix
) return;
4327 this.scrollTop
= (params
[0] || 1) - 1;
4328 this.scrollBottom
= (params
[1] || this.rows
) - 1;
4336 * Save cursor (ANSI.SYS).
4338 Terminal
.prototype.saveCursor = function(params
) {
4339 this.savedX
= this.x
;
4340 this.savedY
= this.y
;
4346 * Restore cursor (ANSI.SYS).
4348 Terminal
.prototype.restoreCursor = function(params
) {
4349 this.x
= this.savedX
|| 0;
4350 this.y
= this.savedY
|| 0;
4360 * Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
4362 Terminal
.prototype.cursorForwardTab = function(params
) {
4363 var param
= params
[0] || 1;
4365 this.x
= this.nextStop();
4371 * CSI Ps S Scroll up Ps lines (default = 1) (SU).
4373 Terminal
.prototype.scrollUp = function(params
) {
4374 var param
= params
[0] || 1;
4376 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
4377 this.lines
.splice(this.ybase
+ this.scrollBottom
, 0, this.blankLine());
4380 this.updateRange(this.scrollTop
);
4381 this.updateRange(this.scrollBottom
);
4386 * CSI Ps T Scroll down Ps lines (default = 1) (SD).
4388 Terminal
.prototype.scrollDown = function(params
) {
4389 var param
= params
[0] || 1;
4391 this.lines
.splice(this.ybase
+ this.scrollBottom
, 1);
4392 this.lines
.splice(this.ybase
+ this.scrollTop
, 0, this.blankLine());
4395 this.updateRange(this.scrollTop
);
4396 this.updateRange(this.scrollBottom
);
4401 * CSI Ps ; Ps ; Ps ; Ps ; Ps T
4402 * Initiate highlight mouse tracking. Parameters are
4403 * [func;startx;starty;firstrow;lastrow]. See the section Mouse
4406 Terminal
.prototype.initMouseTracking = function(params
) {
4407 // Relevant: DECSET 1001
4413 * Reset one or more features of the title modes to the default
4414 * value. Normally, "reset" disables the feature. It is possi-
4415 * ble to disable the ability to reset features by compiling a
4416 * different default for the title modes into xterm.
4417 * Ps = 0 -> Do not set window/icon labels using hexadecimal.
4418 * Ps = 1 -> Do not query window/icon labels using hexadeci-
4420 * Ps = 2 -> Do not set window/icon labels using UTF-8.
4421 * Ps = 3 -> Do not query window/icon labels using UTF-8.
4422 * (See discussion of "Title Modes").
4424 Terminal
.prototype.resetTitleModes = function(params
) {
4430 * CSI Ps Z Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
4432 Terminal
.prototype.cursorBackwardTab = function(params
) {
4433 var param
= params
[0] || 1;
4435 this.x
= this.prevStop();
4441 * CSI Ps b Repeat the preceding graphic character Ps times (REP).
4443 Terminal
.prototype.repeatPrecedingCharacter = function(params
) {
4444 var param
= params
[0] || 1
4445 , line
= this.lines
[this.ybase
+ this.y
]
4446 , ch
= line
[this.x
- 1] || [this.defAttr
, ' ', 1];
4448 while (param
--) line
[this.x
++] = ch
;
4453 * CSI Ps g Tab Clear (TBC).
4454 * Ps = 0 -> Clear Current Column (default).
4455 * Ps = 3 -> Clear All.
4457 * Ps = 2 -> Clear Stops on Line.
4458 * http://vt100.net/annarbor/aaa-ug/section6.html
4460 Terminal
.prototype.tabClear = function(params
) {
4461 var param
= params
[0];
4463 delete this.tabs
[this.x
];
4464 } else if (param
=== 3) {
4471 * CSI Pm i Media Copy (MC).
4472 * Ps = 0 -> Print screen (default).
4473 * Ps = 4 -> Turn off printer controller mode.
4474 * Ps = 5 -> Turn on printer controller mode.
4476 * Media Copy (MC, DEC-specific).
4477 * Ps = 1 -> Print line containing cursor.
4478 * Ps = 4 -> Turn off autoprint mode.
4479 * Ps = 5 -> Turn on autoprint mode.
4480 * Ps = 1 0 -> Print composed display, ignores DECPEX.
4481 * Ps = 1 1 -> Print all pages.
4483 Terminal
.prototype.mediaCopy = function(params
) {
4490 * Set or reset resource-values used by xterm to decide whether
4491 * to construct escape sequences holding information about the
4492 * modifiers pressed with a given key. The first parameter iden-
4493 * tifies the resource to set/reset. The second parameter is the
4494 * value to assign to the resource. If the second parameter is
4495 * omitted, the resource is reset to its initial value.
4496 * Ps = 1 -> modifyCursorKeys.
4497 * Ps = 2 -> modifyFunctionKeys.
4498 * Ps = 4 -> modifyOtherKeys.
4499 * If no parameters are given, all resources are reset to their
4502 Terminal
.prototype.setResources = function(params
) {
4509 * Disable modifiers which may be enabled via the CSI > Ps; Ps m
4510 * sequence. This corresponds to a resource value of "-1", which
4511 * cannot be set with the other sequence. The parameter identi-
4512 * fies the resource to be disabled:
4513 * Ps = 1 -> modifyCursorKeys.
4514 * Ps = 2 -> modifyFunctionKeys.
4515 * Ps = 4 -> modifyOtherKeys.
4516 * If the parameter is omitted, modifyFunctionKeys is disabled.
4517 * When modifyFunctionKeys is disabled, xterm uses the modifier
4518 * keys to make an extended sequence of functions rather than
4519 * adding a parameter to each function key to denote the modi-
4522 Terminal
.prototype.disableModifiers = function(params
) {
4529 * Set resource value pointerMode. This is used by xterm to
4530 * decide whether to hide the pointer cursor as the user types.
4531 * Valid values for the parameter:
4532 * Ps = 0 -> never hide the pointer.
4533 * Ps = 1 -> hide if the mouse tracking mode is not enabled.
4534 * Ps = 2 -> always hide the pointer. If no parameter is
4535 * given, xterm uses the default, which is 1 .
4537 Terminal
.prototype.setPointerMode = function(params
) {
4543 * CSI ! p Soft terminal reset (DECSTR).
4544 * http://vt100.net/docs/vt220-rm/table4-10.html
4546 Terminal
.prototype.softReset = function(params
) {
4547 this.cursorHidden
= false;
4548 this.insertMode
= false;
4549 this.originMode
= false;
4550 this.wraparoundMode
= false; // autowrap
4551 this.applicationKeypad
= false; // ?
4552 this.viewport
.syncScrollArea();
4553 this.applicationCursor
= false;
4555 this.scrollBottom
= this.rows
- 1;
4556 this.curAttr
= this.defAttr
;
4557 this.x
= this.y
= 0; // ?
4558 this.charset
= null;
4559 this.glevel
= 0; // ??
4560 this.charsets
= [null]; // ??
4566 * Request ANSI mode (DECRQM). For VT300 and up, reply is
4568 * where Ps is the mode number as in RM, and Pm is the mode
4570 * 0 - not recognized
4573 * 3 - permanently set
4574 * 4 - permanently reset
4576 Terminal
.prototype.requestAnsiMode = function(params
) {
4583 * Request DEC private mode (DECRQM). For VT300 and up, reply is
4585 * where Ps is the mode number as in DECSET, Pm is the mode value
4586 * as in the ANSI DECRQM.
4588 Terminal
.prototype.requestPrivateMode = function(params
) {
4595 * Set conformance level (DECSCL). Valid values for the first
4597 * Ps = 6 1 -> VT100.
4598 * Ps = 6 2 -> VT200.
4599 * Ps = 6 3 -> VT300.
4600 * Valid values for the second parameter:
4601 * Ps = 0 -> 8-bit controls.
4602 * Ps = 1 -> 7-bit controls (always set for VT100).
4603 * Ps = 2 -> 8-bit controls.
4605 Terminal
.prototype.setConformanceLevel = function(params
) {
4611 * CSI Ps q Load LEDs (DECLL).
4612 * Ps = 0 -> Clear all LEDS (default).
4613 * Ps = 1 -> Light Num Lock.
4614 * Ps = 2 -> Light Caps Lock.
4615 * Ps = 3 -> Light Scroll Lock.
4616 * Ps = 2 1 -> Extinguish Num Lock.
4617 * Ps = 2 2 -> Extinguish Caps Lock.
4618 * Ps = 2 3 -> Extinguish Scroll Lock.
4620 Terminal
.prototype.loadLEDs = function(params
) {
4627 * Set cursor style (DECSCUSR, VT520).
4628 * Ps = 0 -> blinking block.
4629 * Ps = 1 -> blinking block (default).
4630 * Ps = 2 -> steady block.
4631 * Ps = 3 -> blinking underline.
4632 * Ps = 4 -> steady underline.
4634 Terminal
.prototype.setCursorStyle = function(params
) {
4641 * Select character protection attribute (DECSCA). Valid values
4642 * for the parameter:
4643 * Ps = 0 -> DECSED and DECSEL can erase (default).
4644 * Ps = 1 -> DECSED and DECSEL cannot erase.
4645 * Ps = 2 -> DECSED and DECSEL can erase.
4647 Terminal
.prototype.setCharProtectionAttr = function(params
) {
4654 * Restore DEC Private Mode Values. The value of Ps previously
4655 * saved is restored. Ps values are the same as for DECSET.
4657 Terminal
.prototype.restorePrivateValues = function(params
) {
4663 * CSI Pt; Pl; Pb; Pr; Ps$ r
4664 * Change Attributes in Rectangular Area (DECCARA), VT400 and up.
4665 * Pt; Pl; Pb; Pr denotes the rectangle.
4666 * Ps denotes the SGR attributes to change: 0, 1, 4, 5, 7.
4667 * NOTE: xterm doesn't enable this code by default.
4669 Terminal
.prototype.setAttrInRectangle = function(params
) {
4679 for (; t
< b
+ 1; t
++) {
4680 line
= this.lines
[this.ybase
+ t
];
4681 for (i
= l
; i
< r
; i
++) {
4682 line
[i
] = [attr
, line
[i
][1]];
4687 this.updateRange(params
[0]);
4688 this.updateRange(params
[2]);
4693 * CSI Pc; Pt; Pl; Pb; Pr$ x
4694 * Fill Rectangular Area (DECFRA), VT420 and up.
4695 * Pc is the character to use.
4696 * Pt; Pl; Pb; Pr denotes the rectangle.
4697 * NOTE: xterm doesn't enable this code by default.
4699 Terminal
.prototype.fillRectangle = function(params
) {
4709 for (; t
< b
+ 1; t
++) {
4710 line
= this.lines
[this.ybase
+ t
];
4711 for (i
= l
; i
< r
; i
++) {
4712 line
[i
] = [line
[i
][0], String
.fromCharCode(ch
)];
4717 this.updateRange(params
[1]);
4718 this.updateRange(params
[3]);
4724 * Enable Locator Reporting (DECELR).
4725 * Valid values for the first parameter:
4726 * Ps = 0 -> Locator disabled (default).
4727 * Ps = 1 -> Locator enabled.
4728 * Ps = 2 -> Locator enabled for one report, then disabled.
4729 * The second parameter specifies the coordinate unit for locator
4731 * Valid values for the second parameter:
4732 * Pu = 0 <- or omitted -> default to character cells.
4733 * Pu = 1 <- device physical pixels.
4734 * Pu = 2 <- character cells.
4736 Terminal
.prototype.enableLocatorReporting = function(params
) {
4737 var val
= params
[0] > 0;
4738 //this.mouseEvents = val;
4739 //this.decLocator = val;
4744 * CSI Pt; Pl; Pb; Pr$ z
4745 * Erase Rectangular Area (DECERA), VT400 and up.
4746 * Pt; Pl; Pb; Pr denotes the rectangle.
4747 * NOTE: xterm doesn't enable this code by default.
4749 Terminal
.prototype.eraseRectangle = function(params
) {
4759 ch
= [this.eraseAttr(), ' ', 1]; // xterm?
4761 for (; t
< b
+ 1; t
++) {
4762 line
= this.lines
[this.ybase
+ t
];
4763 for (i
= l
; i
< r
; i
++) {
4769 this.updateRange(params
[0]);
4770 this.updateRange(params
[2]);
4776 * Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
4777 * NOTE: xterm doesn't enable this code by default.
4779 Terminal
.prototype.insertColumns = function() {
4780 var param
= params
[0]
4781 , l
= this.ybase
+ this.rows
4782 , ch
= [this.eraseAttr(), ' ', 1] // xterm?
4786 for (i
= this.ybase
; i
< l
; i
++) {
4787 this.lines
[i
].splice(this.x
+ 1, 0, ch
);
4788 this.lines
[i
].pop();
4798 * Delete P s Column(s) (default = 1) (DECDC), VT420 and up
4799 * NOTE: xterm doesn't enable this code by default.
4801 Terminal
.prototype.deleteColumns = function() {
4802 var param
= params
[0]
4803 , l
= this.ybase
+ this.rows
4804 , ch
= [this.eraseAttr(), ' ', 1] // xterm?
4808 for (i
= this.ybase
; i
< l
; i
++) {
4809 this.lines
[i
].splice(this.x
, 1);
4810 this.lines
[i
].push(ch
);
4821 Terminal
.charsets
= {};
4823 // DEC Special Character and Line Drawing Set.
4824 // http://vt100.net/docs/vt102-ug/table5-13.html
4825 // A lot of curses apps use this if they see TERM=xterm.
4826 // testing: echo -e '\e(0a\e(B'
4827 // The xterm output sometimes seems to conflict with the
4828 // reference above. xterm seems in line with the reference
4829 // when running vttest however.
4830 // The table below now uses xterm's output from vttest.
4831 Terminal
.charsets
.SCLD
= { // (0
4832 '`': '\u25c6', // '◆'
4833 'a': '\u2592', // '▒'
4834 'b': '\u0009', // '\t'
4835 'c': '\u000c', // '\f'
4836 'd': '\u000d', // '\r'
4837 'e': '\u000a', // '\n'
4838 'f': '\u00b0', // '°'
4839 'g': '\u00b1', // '±'
4840 'h': '\u2424', // '\u2424' (NL)
4841 'i': '\u000b', // '\v'
4842 'j': '\u2518', // '┘'
4843 'k': '\u2510', // '┐'
4844 'l': '\u250c', // '┌'
4845 'm': '\u2514', // '└'
4846 'n': '\u253c', // '┼'
4847 'o': '\u23ba', // '⎺'
4848 'p': '\u23bb', // '⎻'
4849 'q': '\u2500', // '─'
4850 'r': '\u23bc', // '⎼'
4851 's': '\u23bd', // '⎽'
4852 't': '\u251c', // '├'
4853 'u': '\u2524', // '┤'
4854 'v': '\u2534', // '┴'
4855 'w': '\u252c', // '┬'
4856 'x': '\u2502', // '│'
4857 'y': '\u2264', // '≤'
4858 'z': '\u2265', // '≥'
4859 '{': '\u03c0', // 'π'
4860 '|': '\u2260', // '≠'
4861 '}': '\u00a3', // '£'
4862 '~': '\u00b7' // '·'
4865 Terminal
.charsets
.UK
= null; // (A
4866 Terminal
.charsets
.US
= null; // (B (USASCII)
4867 Terminal
.charsets
.Dutch
= null; // (4
4868 Terminal
.charsets
.Finnish
= null; // (C or (5
4869 Terminal
.charsets
.French
= null; // (R
4870 Terminal
.charsets
.FrenchCanadian
= null; // (Q
4871 Terminal
.charsets
.German
= null; // (K
4872 Terminal
.charsets
.Italian
= null; // (Y
4873 Terminal
.charsets
.NorwegianDanish
= null; // (E or (6
4874 Terminal
.charsets
.Spanish
= null; // (Z
4875 Terminal
.charsets
.Swedish
= null; // (H or (7
4876 Terminal
.charsets
.Swiss
= null; // (=
4877 Terminal
.charsets
.ISOLatin
= null; // /A
4883 function on(el
, type
, handler
, capture
) {
4884 if (!Array
.isArray(el
)) {
4887 el
.forEach(function (element
) {
4888 element
.addEventListener(type
, handler
, capture
|| false);
4892 function off(el
, type
, handler
, capture
) {
4893 el
.removeEventListener(type
, handler
, capture
|| false);
4896 function cancel(ev
, force
) {
4897 if (!this.cancelEvents
&& !force
) {
4900 ev
.preventDefault();
4901 ev
.stopPropagation();
4905 function inherits(child
, parent
) {
4907 this.constructor = child
;
4909 f
.prototype = parent
.prototype;
4910 child
.prototype = new f
;
4913 // if bold is broken, we can't
4914 // use it in the terminal.
4915 function isBoldBroken(document
) {
4916 var body
= document
.getElementsByTagName('body')[0];
4917 var el
= document
.createElement('span');
4918 el
.innerHTML
= 'hello world';
4919 body
.appendChild(el
);
4920 var w1
= el
.scrollWidth
;
4921 el
.style
.fontWeight
= 'bold';
4922 var w2
= el
.scrollWidth
;
4923 body
.removeChild(el
);
4927 function indexOf(obj
, el
) {
4930 if (obj
[i
] === el
) return i
;
4935 function isThirdLevelShift(term
, ev
) {
4937 (term
.browser
.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) ||
4938 (term
.browser
.isMSWindows
&& ev
.altKey
&& ev
.ctrlKey
&& !ev
.metaKey
);
4940 if (ev
.type
== 'keypress') {
4941 return thirdLevelKey
;
4944 // Don't invoke for arrows, pageDown, home, backspace, etc. (on non-keypress events)
4945 return thirdLevelKey
&& (!ev
.keyCode
|| ev
.keyCode
> 47);
4948 function matchColor(r1
, g1
, b1
) {
4949 var hash
= (r1
<< 16) | (g1
<< 8) | b1
;
4951 if (matchColor
._cache
[hash
] != null) {
4952 return matchColor
._cache
[hash
];
4955 var ldiff
= Infinity
4964 for (; i
< Terminal
.vcolors
.length
; i
++) {
4965 c
= Terminal
.vcolors
[i
];
4970 diff
= matchColor
.distance(r1
, g1
, b1
, r2
, g2
, b2
);
4983 return matchColor
._cache
[hash
] = li
;
4986 matchColor
._cache
= {};
4988 // http://stackoverflow.com/questions/1633828
4989 matchColor
.distance = function(r1
, g1
, b1
, r2
, g2
, b2
) {
4990 return Math
.pow(30 * (r1
- r2
), 2)
4991 + Math
.pow(59 * (g1
- g2
), 2)
4992 + Math
.pow(11 * (b1
- b2
), 2);
4995 function each(obj
, iter
, con
) {
4996 if (obj
.forEach
) return obj
.forEach(iter
, con
);
4997 for (var i
= 0; i
< obj
.length
; i
++) {
4998 iter
.call(con
, obj
[i
], i
, obj
);
5002 function keys(obj
) {
5003 if (Object
.keys
) return Object
.keys(obj
);
5006 if (Object
.prototype.hasOwnProperty
.call(obj
, key
)) {
5013 var wcwidth
= (function(opts
) {
5014 // extracted from https://www.cl.cam.ac.uk/%7Emgk25/ucs/wcwidth.c
5015 // combining characters
5017 [0x0300, 0x036F], [0x0483, 0x0486], [0x0488, 0x0489],
5018 [0x0591, 0x05BD], [0x05BF, 0x05BF], [0x05C1, 0x05C2],
5019 [0x05C4, 0x05C5], [0x05C7, 0x05C7], [0x0600, 0x0603],
5020 [0x0610, 0x0615], [0x064B, 0x065E], [0x0670, 0x0670],
5021 [0x06D6, 0x06E4], [0x06E7, 0x06E8], [0x06EA, 0x06ED],
5022 [0x070F, 0x070F], [0x0711, 0x0711], [0x0730, 0x074A],
5023 [0x07A6, 0x07B0], [0x07EB, 0x07F3], [0x0901, 0x0902],
5024 [0x093C, 0x093C], [0x0941, 0x0948], [0x094D, 0x094D],
5025 [0x0951, 0x0954], [0x0962, 0x0963], [0x0981, 0x0981],
5026 [0x09BC, 0x09BC], [0x09C1, 0x09C4], [0x09CD, 0x09CD],
5027 [0x09E2, 0x09E3], [0x0A01, 0x0A02], [0x0A3C, 0x0A3C],
5028 [0x0A41, 0x0A42], [0x0A47, 0x0A48], [0x0A4B, 0x0A4D],
5029 [0x0A70, 0x0A71], [0x0A81, 0x0A82], [0x0ABC, 0x0ABC],
5030 [0x0AC1, 0x0AC5], [0x0AC7, 0x0AC8], [0x0ACD, 0x0ACD],
5031 [0x0AE2, 0x0AE3], [0x0B01, 0x0B01], [0x0B3C, 0x0B3C],
5032 [0x0B3F, 0x0B3F], [0x0B41, 0x0B43], [0x0B4D, 0x0B4D],
5033 [0x0B56, 0x0B56], [0x0B82, 0x0B82], [0x0BC0, 0x0BC0],
5034 [0x0BCD, 0x0BCD], [0x0C3E, 0x0C40], [0x0C46, 0x0C48],
5035 [0x0C4A, 0x0C4D], [0x0C55, 0x0C56], [0x0CBC, 0x0CBC],
5036 [0x0CBF, 0x0CBF], [0x0CC6, 0x0CC6], [0x0CCC, 0x0CCD],
5037 [0x0CE2, 0x0CE3], [0x0D41, 0x0D43], [0x0D4D, 0x0D4D],
5038 [0x0DCA, 0x0DCA], [0x0DD2, 0x0DD4], [0x0DD6, 0x0DD6],
5039 [0x0E31, 0x0E31], [0x0E34, 0x0E3A], [0x0E47, 0x0E4E],
5040 [0x0EB1, 0x0EB1], [0x0EB4, 0x0EB9], [0x0EBB, 0x0EBC],
5041 [0x0EC8, 0x0ECD], [0x0F18, 0x0F19], [0x0F35, 0x0F35],
5042 [0x0F37, 0x0F37], [0x0F39, 0x0F39], [0x0F71, 0x0F7E],
5043 [0x0F80, 0x0F84], [0x0F86, 0x0F87], [0x0F90, 0x0F97],
5044 [0x0F99, 0x0FBC], [0x0FC6, 0x0FC6], [0x102D, 0x1030],
5045 [0x1032, 0x1032], [0x1036, 0x1037], [0x1039, 0x1039],
5046 [0x1058, 0x1059], [0x1160, 0x11FF], [0x135F, 0x135F],
5047 [0x1712, 0x1714], [0x1732, 0x1734], [0x1752, 0x1753],
5048 [0x1772, 0x1773], [0x17B4, 0x17B5], [0x17B7, 0x17BD],
5049 [0x17C6, 0x17C6], [0x17C9, 0x17D3], [0x17DD, 0x17DD],
5050 [0x180B, 0x180D], [0x18A9, 0x18A9], [0x1920, 0x1922],
5051 [0x1927, 0x1928], [0x1932, 0x1932], [0x1939, 0x193B],
5052 [0x1A17, 0x1A18], [0x1B00, 0x1B03], [0x1B34, 0x1B34],
5053 [0x1B36, 0x1B3A], [0x1B3C, 0x1B3C], [0x1B42, 0x1B42],
5054 [0x1B6B, 0x1B73], [0x1DC0, 0x1DCA], [0x1DFE, 0x1DFF],
5055 [0x200B, 0x200F], [0x202A, 0x202E], [0x2060, 0x2063],
5056 [0x206A, 0x206F], [0x20D0, 0x20EF], [0x302A, 0x302F],
5057 [0x3099, 0x309A], [0xA806, 0xA806], [0xA80B, 0xA80B],
5058 [0xA825, 0xA826], [0xFB1E, 0xFB1E], [0xFE00, 0xFE0F],
5059 [0xFE20, 0xFE23], [0xFEFF, 0xFEFF], [0xFFF9, 0xFFFB],
5060 [0x10A01, 0x10A03], [0x10A05, 0x10A06], [0x10A0C, 0x10A0F],
5061 [0x10A38, 0x10A3A], [0x10A3F, 0x10A3F], [0x1D167, 0x1D169],
5062 [0x1D173, 0x1D182], [0x1D185, 0x1D18B], [0x1D1AA, 0x1D1AD],
5063 [0x1D242, 0x1D244], [0xE0001, 0xE0001], [0xE0020, 0xE007F],
5067 function bisearch(ucs
) {
5069 var max
= COMBINING
.length
- 1;
5071 if (ucs
< COMBINING
[0][0] || ucs
> COMBINING
[max
][1])
5073 while (max
>= min
) {
5074 mid
= Math
.floor((min
+ max
) / 2);
5075 if (ucs
> COMBINING
[mid
][1])
5077 else if (ucs
< COMBINING
[mid
][0])
5084 function wcwidth(ucs
) {
5085 // test for 8-bit control characters
5088 if (ucs
< 32 || (ucs
>= 0x7f && ucs
< 0xa0))
5089 return opts
.control
;
5090 // binary search in table of non-spacing characters
5093 // if we arrive here, ucs is not a combining or C0/C1 control character
5098 ucs
<= 0x115f || // Hangul Jamo init. consonants
5101 (ucs
>= 0x2e80 && ucs
<= 0xa4cf && ucs
!= 0x303f) || // CJK..Yi
5102 (ucs
>= 0xac00 && ucs
<= 0xd7a3) || // Hangul Syllables
5103 (ucs
>= 0xf900 && ucs
<= 0xfaff) || // CJK Compat Ideographs
5104 (ucs
>= 0xfe10 && ucs
<= 0xfe19) || // Vertical forms
5105 (ucs
>= 0xfe30 && ucs
<= 0xfe6f) || // CJK Compat Forms
5106 (ucs
>= 0xff00 && ucs
<= 0xff60) || // Fullwidth Forms
5107 (ucs
>= 0xffe0 && ucs
<= 0xffe6) ||
5108 (ucs
>= 0x20000 && ucs
<= 0x2fffd) ||
5109 (ucs
>= 0x30000 && ucs
<= 0x3fffd)
5114 })({nul
: 0, control
: 0}); // configurable options
5120 Terminal
.EventEmitter
= EventEmitter
;
5121 Terminal
.inherits
= inherits
;
5124 * Adds an event listener to the terminal.
5126 * @param {string} event The name of the event. TODO: Document all event types
5127 * @param {function} callback The function to call when the event is triggered.
5131 Terminal
.cancel
= cancel
;
5133 module
.exports
= Terminal
;