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 { CircularList
} from './utils/CircularList.js';
18 import * as Browser
from './utils/Browser';
19 import * as Keyboard
from './utils/Keyboard';
22 * Terminal Emulation References:
24 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.txt
25 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
26 * http://invisible-island.net/vttest/
27 * http://www.inwap.com/pdp10/ansicode.txt
28 * http://linux.die.net/man/4/console_codes
29 * http://linux.die.net/man/7/urxvt
32 // Let it work inside Node.js for automated testing purposes.
33 var document
= (typeof window
!= 'undefined') ? window
.document
: null;
38 var normal
= 0, escaped
= 1, csi
= 2, osc
= 3, charset
= 4, dcs
= 5, ignore
= 6;
45 * Creates a new `Terminal` object.
47 * @param {object} options An object containing a set of options, the available options are:
48 * - `cursorBlink` (boolean): Whether the terminal cursor blinks
49 * - `cols` (number): The number of columns of the terminal (horizontal size)
50 * - `rows` (number): The number of rows of the terminal (vertical size)
54 * @alias module:xterm/src/xterm
56 function Terminal(options
) {
59 if (!(this instanceof Terminal
)) {
60 return new Terminal(arguments
[0], arguments
[1], arguments
[2]);
63 self
.browser
= Browser
;
64 self
.cancel
= Terminal
.cancel
;
66 EventEmitter
.call(this);
68 if (typeof options
=== 'number') {
76 options
= options
|| {};
79 Object
.keys(Terminal
.defaults
).forEach(function(key
) {
80 if (options
[key
] == null) {
81 options
[key
] = Terminal
.options
[key
];
83 if (Terminal
[key
] !== Terminal
.defaults
[key
]) {
84 options
[key
] = Terminal
[key
];
87 self
[key
] = options
[key
];
90 if (options
.colors
.length
=== 8) {
91 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(8));
92 } else if (options
.colors
.length
=== 16) {
93 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(16));
94 } else if (options
.colors
.length
=== 10) {
95 options
.colors
= options
.colors
.slice(0, -2).concat(
96 Terminal
._colors
.slice(8, -2), options
.colors
.slice(-2));
97 } else if (options
.colors
.length
=== 18) {
98 options
.colors
= options
.colors
.concat(
99 Terminal
._colors
.slice(16, -2), options
.colors
.slice(-2));
101 this.colors
= options
.colors
;
103 this.options
= options
;
105 // this.context = options.context || window;
106 // this.document = options.document || document;
107 this.parent
= options
.body
|| options
.parent
|| (
108 document
? document
.getElementsByTagName('body')[0] : null
111 this.cols
= options
.cols
|| options
.geometry
[0];
112 this.rows
= options
.rows
|| options
.geometry
[1];
113 this.geometry
= [this.cols
, this.rows
];
115 if (options
.handler
) {
116 this.on('data', options
.handler
);
120 * The scroll position of the y cursor, ie. ybase + y = the y position within the entire
126 * The scroll position of the viewport
131 * The cursor's x position after ybase
136 * The cursor's y position after ybase
141 * Used to debounce the refresh function
143 this.isRefreshing
= false;
146 * Whether there is a full terminal refresh queued
149 this.cursorState
= 0;
150 this.cursorHidden
= false;
155 this.scrollBottom
= this.rows
- 1;
156 this.customKeydownHandler
= null;
159 this.applicationKeypad
= false;
160 this.applicationCursor
= false;
161 this.originMode
= false;
162 this.insertMode
= false;
163 this.wraparoundMode
= true; // defaults: xterm - true, vt100 - false
168 this.gcharset
= null;
170 this.charsets
= [null];
194 this.readable
= true;
195 this.writable
= true;
197 this.defAttr
= (0 << 18) | (257 << 9) | (256 << 0);
198 this.curAttr
= this.defAttr
;
201 this.currentParam
= 0;
205 // leftover surrogate high from previous write invocation
206 this.surrogate_high
= '';
209 * An array of all lines in the entire buffer, including the prompt. The lines are array of
210 * characters which are 2-length arrays where [0] is an attribute and [1] is the character.
212 this.lines
= new CircularList(this.scrollback
);
215 this.lines
.push(this.blankLine());
221 // Store if user went browsing history in scrollback
222 this.userScrolling
= false;
225 inherits(Terminal
, EventEmitter
);
228 * back_color_erase feature for xterm.
230 Terminal
.prototype.eraseAttr = function() {
231 // if (this.is('screen')) return this.defAttr;
232 return (this.defAttr
& ~0x1ff) | (this.curAttr
& 0x1ff);
240 Terminal
.tangoColors
= [
261 // Colors 0-15 + 16-255
262 // Much thanks to TooTallNate for writing this.
263 Terminal
.colors
= (function() {
264 var colors
= Terminal
.tangoColors
.slice()
265 , r
= [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff]
270 for (; i
< 216; i
++) {
271 out(r
[(i
/ 36) % 6 | 0], r
[(i
/ 6) % 6 | 0], r
[i
% 6]);
276 for (; i
< 24; i
++) {
281 function out(r
, g
, b
) {
282 colors
.push('#' + hex(r
) + hex(g
) + hex(b
));
287 return c
.length
< 2 ? '0' + c
: c
;
293 Terminal
._colors
= Terminal
.colors
.slice();
295 Terminal
.vcolors
= (function() {
297 , colors
= Terminal
.colors
301 for (; i
< 256; i
++) {
302 color
= parseInt(colors
[i
].substring(1), 16);
304 (color
>> 16) & 0xff,
317 Terminal
.defaults
= {
318 colors
: Terminal
.colors
,
330 // programFeatures: false,
334 Terminal
.options
= {};
336 Terminal
.focus
= null;
338 each(keys(Terminal
.defaults
), function(key
) {
339 Terminal
[key
] = Terminal
.defaults
[key
];
340 Terminal
.options
[key
] = Terminal
.defaults
[key
];
344 * Focus the terminal. Delegates focus handling to the terminal's DOM element.
346 Terminal
.prototype.focus = function() {
347 return this.textarea
.focus();
351 * Retrieves an option's value from the terminal.
352 * @param {string} key The option key.
354 Terminal
.prototype.getOption = function(key
, value
) {
355 if (!(key
in Terminal
.defaults
)) {
356 throw new Error('No option with key "' + key
+ '"');
359 if (typeof this.options
[key
] !== 'undefined') {
360 return this.options
[key
];
367 * Sets an option on the terminal.
368 * @param {string} key The option key.
369 * @param {string} value The option value.
371 Terminal
.prototype.setOption = function(key
, value
) {
372 if (!(key
in Terminal
.defaults
)) {
373 throw new Error('No option with key "' + key
+ '"');
376 this.options
[key
] = value
;
380 * Binds the desired focus behavior on a given terminal object.
384 Terminal
.bindFocus = function (term
) {
385 on(term
.textarea
, 'focus', function (ev
) {
386 if (term
.sendFocus
) {
389 term
.element
.classList
.add('focus');
391 Terminal
.focus
= term
;
392 term
.emit('focus', {terminal
: term
});
397 * Blur the terminal. Delegates blur handling to the terminal's DOM element.
399 Terminal
.prototype.blur = function() {
400 return this.textarea
.blur();
404 * Binds the desired blur behavior on a given terminal object.
408 Terminal
.bindBlur = function (term
) {
409 on(term
.textarea
, 'blur', function (ev
) {
410 term
.refresh(term
.y
, term
.y
);
411 if (term
.sendFocus
) {
414 term
.element
.classList
.remove('focus');
415 Terminal
.focus
= null;
416 term
.emit('blur', {terminal
: term
});
421 * Initialize default behavior
423 Terminal
.prototype.initGlobal = function() {
426 Terminal
.bindKeys(this);
427 Terminal
.bindFocus(this);
428 Terminal
.bindBlur(this);
430 // Bind clipboard functionality
431 on(this.element
, 'copy', function (ev
) {
432 copyHandler
.call(this, ev
, term
);
434 on(this.textarea
, 'paste', function (ev
) {
435 pasteHandler
.call(this, ev
, term
);
437 on(this.element
, 'paste', function (ev
) {
438 pasteHandler
.call(this, ev
, term
);
441 function rightClickHandlerWrapper (ev
) {
442 rightClickHandler
.call(this, ev
, term
);
445 if (term
.browser
.isFirefox
) {
446 on(this.element
, 'mousedown', function (ev
) {
447 if (ev
.button
== 2) {
448 rightClickHandlerWrapper(ev
);
452 on(this.element
, 'contextmenu', rightClickHandlerWrapper
);
457 * Apply key handling to the terminal
459 Terminal
.bindKeys = function(term
) {
460 on(term
.element
, 'keydown', function(ev
) {
461 if (document
.activeElement
!= this) {
467 on(term
.element
, 'keypress', function(ev
) {
468 if (document
.activeElement
!= this) {
474 on(term
.element
, 'keyup', term
.focus
.bind(term
));
476 on(term
.textarea
, 'keydown', function(ev
) {
480 on(term
.textarea
, 'keypress', function(ev
) {
482 // Truncate the textarea's value, since it is not needed
486 on(term
.textarea
, 'compositionstart', term
.compositionHelper
.compositionstart
.bind(term
.compositionHelper
));
487 on(term
.textarea
, 'compositionupdate', term
.compositionHelper
.compositionupdate
.bind(term
.compositionHelper
));
488 on(term
.textarea
, 'compositionend', term
.compositionHelper
.compositionend
.bind(term
.compositionHelper
));
489 term
.on('refresh', term
.compositionHelper
.updateCompositionElements
.bind(term
.compositionHelper
));
494 * Insert the given row to the terminal or produce a new one
495 * if no row argument is passed. Return the inserted row.
496 * @param {HTMLElement} row (optional) The row to append to the terminal.
498 Terminal
.prototype.insertRow = function (row
) {
499 if (typeof row
!= 'object') {
500 row
= document
.createElement('div');
503 this.rowContainer
.appendChild(row
);
504 this.children
.push(row
);
510 * Opens the terminal within an element.
512 * @param {HTMLElement} parent The element to create the terminal within.
514 Terminal
.prototype.open = function(parent
) {
515 var self
=this, i
=0, div
;
517 this.parent
= parent
|| this.parent
;
520 throw new Error('Terminal requires a parent element.');
523 // Grab global elements
524 this.context
= this.parent
.ownerDocument
.defaultView
;
525 this.document
= this.parent
.ownerDocument
;
526 this.body
= this.document
.getElementsByTagName('body')[0];
528 //Create main element container
529 this.element
= this.document
.createElement('div');
530 this.element
.classList
.add('terminal');
531 this.element
.classList
.add('xterm');
532 this.element
.classList
.add('xterm-theme-' + this.theme
);
534 this.element
.style
.height
535 this.element
.setAttribute('tabindex', 0);
537 this.viewportElement
= document
.createElement('div');
538 this.viewportElement
.classList
.add('xterm-viewport');
539 this.element
.appendChild(this.viewportElement
);
540 this.viewportScrollArea
= document
.createElement('div');
541 this.viewportScrollArea
.classList
.add('xterm-scroll-area');
542 this.viewportElement
.appendChild(this.viewportScrollArea
);
544 // Create the container that will hold the lines of the terminal and then
545 // produce the lines the lines.
546 this.rowContainer
= document
.createElement('div');
547 this.rowContainer
.classList
.add('xterm-rows');
548 this.element
.appendChild(this.rowContainer
);
551 // Create the container that will hold helpers like the textarea for
552 // capturing DOM Events. Then produce the helpers.
553 this.helperContainer
= document
.createElement('div');
554 this.helperContainer
.classList
.add('xterm-helpers');
555 // TODO: This should probably be inserted once it's filled to prevent an additional layout
556 this.element
.appendChild(this.helperContainer
);
557 this.textarea
= document
.createElement('textarea');
558 this.textarea
.classList
.add('xterm-helper-textarea');
559 this.textarea
.setAttribute('autocorrect', 'off');
560 this.textarea
.setAttribute('autocapitalize', 'off');
561 this.textarea
.setAttribute('spellcheck', 'false');
562 this.textarea
.tabIndex
= 0;
563 this.textarea
.addEventListener('focus', function() {
564 self
.emit('focus', {terminal
: self
});
566 this.textarea
.addEventListener('blur', function() {
567 self
.emit('blur', {terminal
: self
});
569 this.helperContainer
.appendChild(this.textarea
);
571 this.compositionView
= document
.createElement('div');
572 this.compositionView
.classList
.add('composition-view');
573 this.compositionHelper
= new CompositionHelper(this.textarea
, this.compositionView
, this);
574 this.helperContainer
.appendChild(this.compositionView
);
576 this.charMeasureElement
= document
.createElement('div');
577 this.charMeasureElement
.classList
.add('xterm-char-measure-element');
578 this.charMeasureElement
.innerHTML
= 'W';
579 this.helperContainer
.appendChild(this.charMeasureElement
);
581 for (; i
< this.rows
; i
++) {
584 this.parent
.appendChild(this.element
);
586 this.viewport
= new Viewport(this, this.viewportElement
, this.viewportScrollArea
, this.charMeasureElement
);
589 this.refresh(0, this.rows
- 1);
591 // Initialize global actions that
592 // need to be taken on the document.
595 // Ensure there is a Terminal.focus.
598 on(this.element
, 'click', function() {
599 var selection
= document
.getSelection(),
600 collapsed
= selection
.isCollapsed
,
601 isRange
= typeof collapsed
== 'boolean' ? !collapsed
: selection
.type
== 'Range';
607 // Listen for mouse events and translate
608 // them into terminal mouse protocols.
611 // Figure out whether boldness affects
612 // the character width of monospace fonts.
613 if (Terminal
.brokenBold
== null) {
614 Terminal
.brokenBold
= isBoldBroken(this.document
);
618 * This event is emitted when terminal has completed opening.
627 * Attempts to load an add-on using CommonJS or RequireJS (whichever is available).
628 * @param {string} addon The name of the addon to load
631 Terminal
.loadAddon = function(addon
, callback
) {
632 if (typeof exports
=== 'object' && typeof module
=== 'object') {
634 return require('./addons/' + addon
+ '/' + addon
);
635 } else if (typeof define
== 'function') {
637 return require(['./addons/' + addon
+ '/' + addon
], callback
);
639 console
.error('Cannot load a module without a CommonJS or RequireJS environment.');
647 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#Mouse%20Tracking
648 * To better understand these
649 * the xterm code is very helpful:
651 * button.c, charproc.c, misc.c
652 * Relevant functions in xterm/button.c:
653 * BtnCode, EmitButtonCode, EditorButton, SendMousePosition
655 Terminal
.prototype.bindMouse = function() {
656 var el
= this.element
, self
= this, pressed
= 32;
658 // mouseup, mousedown, wheel
659 // left click: ^[[M 3<^[[M#3<
661 function sendButton(ev
) {
665 // get the xterm-style button
666 button
= getButton(ev
);
668 // get mouse coordinates
672 sendEvent(button
, pos
);
674 switch (ev
.overrideType
|| ev
.type
) {
679 // keep it at the left
680 // button, just in case.
691 // motion example of a left click:
692 // ^[[M 3<^[[M@4<^[[M@5<^[[M@6<^[[M@7<^[[M#7<
693 function sendMove(ev
) {
700 // buttons marked as motions
701 // are incremented by 32
704 sendEvent(button
, pos
);
708 // position to characters
709 function encode(data
, ch
) {
710 if (!self
.utfMouse
) {
711 if (ch
=== 255) return data
.push(0);
712 if (ch
> 127) ch
= 127;
715 if (ch
=== 2047) return data
.push(0);
719 if (ch
> 2047) ch
= 2047;
720 data
.push(0xC0 | (ch
>> 6));
721 data
.push(0x80 | (ch
& 0x3F));
726 // send a mouse event:
727 // regular/utf8: ^[[M Cb Cx Cy
728 // urxvt: ^[[ Cb ; Cx ; Cy M
729 // sgr: ^[[ Cb ; Cx ; Cy M/m
730 // vt300: ^[[ 24(1/3/5)~ [ Cx , Cy ] \r
731 // locator: CSI P e ; P b ; P r ; P c ; P p & w
732 function sendEvent(button
, pos
) {
733 // self.emit('mouse', {
739 if (self
.vt300Mouse
) {
741 // http://www.vt100.net/docs/vt3xx-gp/chapter15.html
745 var data
= '\x1b[24';
746 if (button
=== 0) data
+= '1';
747 else if (button
=== 1) data
+= '3';
748 else if (button
=== 2) data
+= '5';
749 else if (button
=== 3) return;
751 data
+= '~[' + pos
.x
+ ',' + pos
.y
+ ']\r';
756 if (self
.decLocator
) {
761 if (button
=== 0) button
= 2;
762 else if (button
=== 1) button
= 4;
763 else if (button
=== 2) button
= 6;
764 else if (button
=== 3) button
= 3;
768 + (button
=== 3 ? 4 : 0)
779 if (self
.urxvtMouse
) {
784 self
.send('\x1b[' + button
+ ';' + pos
.x
+ ';' + pos
.y
+ 'M');
792 + (((button
& 3) === 3 ? button
& ~3 : button
) - 32)
797 + ((button
& 3) === 3 ? 'm' : 'M'));
803 encode(data
, button
);
807 self
.send('\x1b[M' + String
.fromCharCode
.apply(String
, data
));
810 function getButton(ev
) {
823 // 1, and 2 - with 64 added
824 switch (ev
.overrideType
|| ev
.type
) {
826 button
= ev
.button
!= null
832 if (self
.browser
.isMSIE
) {
833 button
= button
=== 1 ? 0 : button
=== 4 ? 1 : button
;
839 case 'DOMMouseScroll':
840 button
= ev
.detail
< 0
845 button
= ev
.wheelDeltaY
> 0
851 // next three bits are the modifiers:
852 // 4 = shift, 8 = meta, 16 = control
853 shift
= ev
.shiftKey
? 4 : 0;
854 meta
= ev
.metaKey
? 8 : 0;
855 ctrl
= ev
.ctrlKey
? 16 : 0;
856 mod
= shift
| meta
| ctrl
;
859 if (self
.vt200Mouse
) {
862 } else if (!self
.normalMouse
) {
867 button
= (32 + (mod
<< 2)) + button
;
872 // mouse coordinates measured in cols/rows
873 function getCoords(ev
) {
876 // ignore browsers without pageX for now
877 if (ev
.pageX
== null) return;
883 // should probably check offsetParent
884 // but this is more portable
885 while (el
&& el
!== self
.document
.documentElement
) {
888 el
= 'offsetParent' in el
893 // convert to cols/rows
894 w
= self
.element
.clientWidth
;
895 h
= self
.element
.clientHeight
;
896 x
= Math
.ceil((x
/ w
) * self
.cols
);
897 y
= Math
.ceil((y
/ h
) * self
.rows
);
899 // be sure to avoid sending
900 // bad positions to the program
902 if (x
> self
.cols
) x
= self
.cols
;
904 if (y
> self
.rows
) y
= self
.rows
;
906 // xterm sends raw bytes and
907 // starts at 32 (SP) for each.
918 on(el
, 'mousedown', function(ev
) {
919 if (!self
.mouseEvents
) return;
928 //if (self.vt200Mouse && !self.normalMouse) {
929 if (self
.vt200Mouse
) {
930 ev
.overrideType
= 'mouseup';
932 return self
.cancel(ev
);
936 if (self
.normalMouse
) on(self
.document
, 'mousemove', sendMove
);
938 // x10 compatibility mode can't send button releases
939 if (!self
.x10Mouse
) {
940 on(self
.document
, 'mouseup', function up(ev
) {
942 if (self
.normalMouse
) off(self
.document
, 'mousemove', sendMove
);
943 off(self
.document
, 'mouseup', up
);
944 return self
.cancel(ev
);
948 return self
.cancel(ev
);
951 //if (self.normalMouse) {
952 // on(self.document, 'mousemove', sendMove);
955 on(el
, 'wheel', function(ev
) {
956 if (!self
.mouseEvents
) return;
959 || self
.decLocator
) return;
961 return self
.cancel(ev
);
964 // allow wheel scrolling in
965 // the shell for example
966 on(el
, 'wheel', function(ev
) {
967 if (self
.mouseEvents
) return;
968 self
.viewport
.onWheel(ev
);
969 return self
.cancel(ev
);
974 * Destroys the terminal.
976 Terminal
.prototype.destroy = function() {
977 this.readable
= false;
978 this.writable
= false;
980 this.handler = function() {};
981 this.write = function() {};
982 if (this.element
.parentNode
) {
983 this.element
.parentNode
.removeChild(this.element
);
985 //this.emit('close');
990 * Flags used to render terminal text properly
1001 * Refreshes (re-renders) terminal content within two rows (inclusive)
1005 * In the screen buffer, each character is stored as a an array with a character
1006 * and a 32-bit integer:
1007 * - First value: a utf-16 character.
1009 * - Next 9 bits: background color (0-511).
1010 * - Next 9 bits: foreground color (0-511).
1011 * - Next 14 bits: a mask for misc. flags:
1018 * @param {number} start The row to start from (between 0 and terminal's height terminal - 1)
1019 * @param {number} end The row to end at (between fromRow and terminal's height terminal - 1)
1020 * @param {boolean} queue Whether the refresh should ran right now or be queued
1022 Terminal
.prototype.refresh = function(start
, end
, queue
) {
1025 // queue defaults to true
1026 queue
= (typeof queue
== 'undefined') ? true : queue
;
1029 * The refresh queue allows refresh to execute only approximately 30 times a second. For
1030 * commands that pass a significant amount of output to the write function, this prevents the
1031 * terminal from maxing out the CPU and making the UI unresponsive. While commands can still
1032 * run beyond what they do on the terminal, it is far better with a debounce in place as
1033 * every single terminal manipulation does not need to be constructed in the DOM.
1035 * A side-effect of this is that it makes ^C to interrupt a process seem more responsive.
1038 // If refresh should be queued, order the refresh and return.
1039 if (this._refreshIsQueued
) {
1040 // If a refresh has already been queued, just order a full refresh next
1041 this._fullRefreshNext
= true;
1043 setTimeout(function () {
1044 self
.refresh(start
, end
, false);
1046 this._refreshIsQueued
= true;
1051 // If refresh should be run right now (not be queued), release the lock
1052 this._refreshIsQueued
= false;
1054 // If multiple refreshes were requested, make a full refresh.
1055 if (this._fullRefreshNext
) {
1057 end
= this.rows
- 1;
1058 this._fullRefreshNext
= false // reset lock
1061 var x
, y
, i
, line
, out
, ch
, ch_width
, width
, data
, attr
, bg
, fg
, flags
, row
, parent
, focused
= document
.activeElement
;
1063 // If this is a big refresh, remove the terminal rows from the DOM for faster calculations
1064 if (end
- start
>= this.rows
/ 2) {
1065 parent
= this.element
.parentNode
;
1067 this.element
.removeChild(this.rowContainer
);
1074 if (end
>= this.rows
.length
) {
1075 this.log('`end` is too large. Most likely a bad CSR.');
1076 end
= this.rows
.length
- 1;
1079 for (; y
<= end
; y
++) {
1080 row
= y
+ this.ydisp
;
1082 line
= this.lines
.get(row
);
1085 if (this.y
=== y
- (this.ybase
- this.ydisp
)
1087 && !this.cursorHidden
) {
1093 attr
= this.defAttr
;
1096 for (; i
< width
; i
++) {
1099 ch_width
= line
[i
][2];
1103 if (i
=== x
) data
= -1;
1105 if (data
!== attr
) {
1106 if (attr
!== this.defAttr
) {
1109 if (data
!== this.defAttr
) {
1111 out
+= '<span class="reverse-video terminal-cursor';
1112 if (this.cursorBlink
) {
1117 var classNames
= [];
1120 fg
= (data
>> 9) & 0x1ff;
1123 if (flags
& Terminal
.flags
.BOLD
) {
1124 if (!Terminal
.brokenBold
) {
1125 classNames
.push('xterm-bold');
1127 // See: XTerm*boldColors
1128 if (fg
< 8) fg
+= 8;
1131 if (flags
& Terminal
.flags
.UNDERLINE
) {
1132 classNames
.push('xterm-underline');
1135 if (flags
& Terminal
.flags
.BLINK
) {
1136 classNames
.push('xterm-blink');
1139 // If inverse flag is on, then swap the foreground and background variables.
1140 if (flags
& Terminal
.flags
.INVERSE
) {
1141 /* One-line variable swap in JavaScript: http://stackoverflow.com/a/16201730 */
1142 bg = [fg, fg = bg][0];
1143 // Should inverse just be before the
1144 // above boldColors effect instead?
1145 if ((flags & 1) && fg < 8) fg += 8;
1148 if (flags & Terminal.flags.INVISIBLE) {
1149 classNames.push('xterm-hidden');
1153 * Weird situation: Invert flag used black foreground and white background results
1154 * in invalid background color, positioned at the 256 index of the 256 terminal
1155 * color map. Pin the colors manually in such a case.
1157 * Source: https://github.com/sourcelair/xterm.js/issues/57
1159 if (flags
& Terminal
.flags
.INVERSE
) {
1169 classNames
.push('xterm-bg-color-' + bg
);
1173 classNames
.push('xterm-color-' + fg
);
1177 if (classNames
.length
) {
1178 out
+= ' class="' + classNames
.join(' ') + '"';
1207 if (attr
!== this.defAttr
) {
1211 this.children
[y
].innerHTML
= out
;
1215 this.element
.appendChild(this.rowContainer
);
1218 this.emit('refresh', {element
: this.element
, start
: start
, end
: end
});
1222 * Display the cursor element
1224 Terminal
.prototype.showCursor = function() {
1225 if (!this.cursorState
) {
1226 this.cursorState
= 1;
1227 this.refresh(this.y
, this.y
);
1232 * Scroll the terminal down 1 row, creating a blank line.
1234 Terminal
.prototype.scroll = function() {
1239 // TODO: Why is this done twice?
1240 if (!this.userScrolling
) {
1241 this.ydisp
= this.ybase
;
1245 row
= this.ybase
+ this.rows
- 1;
1247 // subtract the bottom scroll region
1248 row
-= this.rows
- 1 - this.scrollBottom
;
1250 if (row
=== this.lines
.length
) {
1251 // Compensate ybase and ydisp if lines has hit the maximum buffer size
1252 if (this.lines
.length
=== this.lines
.maxLength
) {
1256 // Optimization: pushing is faster than splicing when they amount to the same behavior
1257 this.lines
.push(this.blankLine());
1260 this.lines
.splice(row
, 0, this.blankLine());
1263 if (this.scrollTop
!== 0) {
1264 if (this.ybase
!== 0) {
1266 if (!this.userScrolling
) {
1267 this.ydisp
= this.ybase
;
1270 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
1274 this.updateRange(this.scrollTop
);
1275 this.updateRange(this.scrollBottom
);
1278 * This event is emitted whenever the terminal is scrolled.
1279 * The one parameter passed is the new y display position.
1283 this.emit('scroll', this.ydisp
);
1287 * Scroll the display of the terminal
1288 * @param {number} disp The number of lines to scroll down (negatives scroll up).
1289 * @param {boolean} suppressScrollEvent Don't emit the scroll event as scrollDisp. This is used
1290 * to avoid unwanted events being handled by the veiwport when the event was triggered from the
1291 * viewport originally.
1293 Terminal
.prototype.scrollDisp = function(disp
, suppressScrollEvent
) {
1295 this.userScrolling
= true;
1296 } else if (disp
+ this.ydisp
>= this.ybase
) {
1297 this.userScrolling
= false;
1302 if (this.ydisp
> this.ybase
) {
1303 this.ydisp
= this.ybase
;
1304 } else if (this.ydisp
< 0) {
1308 if (!suppressScrollEvent
) {
1309 this.emit('scroll', this.ydisp
);
1312 this.refresh(0, this.rows
- 1);
1316 * Scroll the display of the terminal by a number of pages.
1317 * @param {number} pageCount The number of pages to scroll (negative scrolls up).
1319 Terminal
.prototype.scrollPages = function(pageCount
) {
1320 this.scrollDisp(pageCount
* (this.rows
- 1));
1324 * Scrolls the display of the terminal to the top.
1326 Terminal
.prototype.scrollToTop = function() {
1327 this.scrollDisp(-this.ydisp
);
1331 * Scrolls the display of the terminal to the bottom.
1333 Terminal
.prototype.scrollToBottom = function() {
1334 this.scrollDisp(this.ybase
- this.ydisp
);
1338 * Writes text to the terminal.
1339 * @param {string} text The text to write to the terminal.
1341 Terminal
.prototype.write = function(data
) {
1342 var l
= data
.length
, i
= 0, j
, cs
, ch
, code
, low
, ch_width
, row
;
1344 this.refreshStart
= this.y
;
1345 this.refreshEnd
= this.y
;
1347 // apply leftover surrogate high from last write
1348 if (this.surrogate_high
) {
1349 data
= this.surrogate_high
+ data
;
1350 this.surrogate_high
= '';
1353 for (; i
< l
; i
++) {
1356 // FIXME: higher chars than 0xa0 are not allowed in escape sequences
1357 // --> maybe move to default
1358 code
= data
.charCodeAt(i
);
1359 if (0xD800 <= code
&& code
<= 0xDBFF) {
1360 // we got a surrogate high
1361 // get surrogate low (next 2 bytes)
1362 low
= data
.charCodeAt(i
+1);
1364 // end of data stream, save surrogate high
1365 this.surrogate_high
= ch
;
1368 code
= ((code
- 0xD800) * 0x400) + (low
- 0xDC00) + 0x10000;
1369 ch
+= data
.charAt(i
+1);
1371 // surrogate low - already handled above
1372 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
) {
1443 // dont overflow left
1444 if (this.lines
.get(row
)[this.x
-1]) {
1445 if (!this.lines
.get(row
)[this.x
-1][2]) {
1447 // found empty cell after fullwidth, need to go 2 cells back
1448 if (this.lines
.get(row
)[this.x
-2])
1449 this.lines
.get(row
)[this.x
-2][1] += ch
;
1452 this.lines
.get(row
)[this.x
-1][1] += ch
;
1454 this.updateRange(this.y
);
1459 // goto next line if ch would overflow
1460 // TODO: needs a global min terminal width of 2
1461 if (this.x
+ch_width
-1 >= this.cols
) {
1462 // autowrap - DECAWM
1463 if (this.wraparoundMode
) {
1466 if (this.y
> this.scrollBottom
) {
1471 this.x
= this.cols
-1;
1472 if(ch_width
===2) // FIXME: check for xterm behavior
1476 row
= this.y
+ this.ybase
;
1478 // insert mode: move characters to right
1479 if (this.insertMode
) {
1480 // do this twice for a fullwidth char
1481 for (var moves
=0; moves
<ch_width
; ++moves
) {
1482 // remove last cell, if it's width is 0
1483 // we have to adjust the second last cell as well
1484 var removed
= this.lines
.get(this.y
+ this.ybase
).pop();
1486 && this.lines
.get(row
)[this.cols
-2]
1487 && this.lines
.get(row
)[this.cols
-2][2]===2)
1488 this.lines
.get(row
)[this.cols
-2] = [this.curAttr
, ' ', 1];
1490 // insert empty cell at cursor
1491 this.lines
.get(row
).splice(this.x
, 0, [this.curAttr
, ' ', 1]);
1495 this.lines
.get(row
)[this.x
] = [this.curAttr
, ch
, ch_width
];
1497 this.updateRange(this.y
);
1499 // fullwidth char - set next cell width to zero and advance cursor
1501 this.lines
.get(row
)[this.x
] = [this.curAttr
, '', 0];
1510 // ESC [ Control Sequence Introducer ( CSI is 0x9b).
1513 this.currentParam
= 0;
1517 // ESC ] Operating System Command ( OSC is 0x9d).
1520 this.currentParam
= 0;
1524 // ESC P Device Control String ( DCS is 0x90).
1527 this.currentParam
= 0;
1531 // ESC _ Application Program Command ( APC is 0x9f).
1533 this.state
= ignore
;
1536 // ESC ^ Privacy Message ( PM is 0x9e).
1538 this.state
= ignore
;
1541 // ESC c Full Reset (RIS).
1546 // ESC E Next Line ( NEL is 0x85).
1547 // ESC D Index ( IND is 0x84).
1555 // ESC M Reverse Index ( RI is 0x8d).
1557 this.reverseIndex();
1560 // ESC % Select default/utf-8 character set.
1561 // @ = default, G = utf-8
1563 //this.charset = null;
1565 this.setgCharset(0, Terminal
.charsets
.US
);
1566 this.state
= normal
;
1570 // ESC (,),*,+,-,. Designate G0-G2 Character Set.
1571 case '(': // <-- this seems to get all the attention
1597 this.state
= charset
;
1600 // Designate G3 Character Set (VT300).
1601 // A = ISO Latin-1 Supplemental.
1605 this.state
= charset
;
1610 // Single Shift Select of G2 Character Set
1611 // ( SS2 is 0x8e). This affects next character only.
1615 // Single Shift Select of G3 Character Set
1616 // ( SS3 is 0x8f). This affects next character only.
1620 // Invoke the G2 Character Set as GL (LS2).
1625 // Invoke the G3 Character Set as GL (LS3).
1630 // Invoke the G3 Character Set as GR (LS3R).
1635 // Invoke the G2 Character Set as GR (LS2R).
1640 // Invoke the G1 Character Set as GR (LS1R).
1645 // ESC 7 Save Cursor (DECSC).
1648 this.state
= normal
;
1651 // ESC 8 Restore Cursor (DECRC).
1653 this.restoreCursor();
1654 this.state
= normal
;
1657 // ESC # 3 DEC line height/width
1659 this.state
= normal
;
1663 // ESC H Tab Set (HTS is 0x88).
1668 // ESC = Application Keypad (DECKPAM).
1670 this.log('Serial port requested application keypad.');
1671 this.applicationKeypad
= true;
1672 this.viewport
.syncScrollArea();
1673 this.state
= normal
;
1676 // ESC > Normal Keypad (DECKPNM).
1678 this.log('Switching back to normal keypad.');
1679 this.applicationKeypad
= false;
1680 this.viewport
.syncScrollArea();
1681 this.state
= normal
;
1685 this.state
= normal
;
1686 this.error('Unknown ESC control: %s.', ch
);
1693 case '0': // DEC Special Character and Line Drawing Set.
1694 cs
= Terminal
.charsets
.SCLD
;
1697 cs
= Terminal
.charsets
.UK
;
1699 case 'B': // United States (USASCII).
1700 cs
= Terminal
.charsets
.US
;
1703 cs
= Terminal
.charsets
.Dutch
;
1705 case 'C': // Finnish
1707 cs
= Terminal
.charsets
.Finnish
;
1710 cs
= Terminal
.charsets
.French
;
1712 case 'Q': // FrenchCanadian
1713 cs
= Terminal
.charsets
.FrenchCanadian
;
1716 cs
= Terminal
.charsets
.German
;
1718 case 'Y': // Italian
1719 cs
= Terminal
.charsets
.Italian
;
1721 case 'E': // NorwegianDanish
1723 cs
= Terminal
.charsets
.NorwegianDanish
;
1725 case 'Z': // Spanish
1726 cs
= Terminal
.charsets
.Spanish
;
1728 case 'H': // Swedish
1730 cs
= Terminal
.charsets
.Swedish
;
1733 cs
= Terminal
.charsets
.Swiss
;
1735 case '/': // ISOLatin (actually /A)
1736 cs
= Terminal
.charsets
.ISOLatin
;
1740 cs
= Terminal
.charsets
.US
;
1743 this.setgCharset(this.gcharset
, cs
);
1744 this.gcharset
= null;
1745 this.state
= normal
;
1751 // Set Text Parameters.
1752 if (ch
=== '\x1b' || ch
=== '\x07') {
1753 if (ch
=== '\x1b') i
++;
1755 this.params
.push(this.currentParam
);
1757 switch (this.params
[0]) {
1761 if (this.params
[1]) {
1762 this.title
= this.params
[1];
1763 this.handleTitle(this.title
);
1771 // change dynamic colors
1783 // change dynamic ui colors
1795 // manipulate selection data
1813 this.currentParam
= 0;
1814 this.state
= normal
;
1816 if (!this.params
.length
) {
1817 if (ch
>= '0' && ch
<= '9') {
1819 this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1820 } else if (ch
=== ';') {
1821 this.params
.push(this.currentParam
);
1822 this.currentParam
= '';
1825 this.currentParam
+= ch
;
1832 if (ch
=== '?' || ch
=== '>' || ch
=== '!') {
1838 if (ch
>= '0' && ch
<= '9') {
1839 this.currentParam
= this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1843 // '$', '"', ' ', '\''
1844 if (ch
=== '$' || ch
=== '"' || ch
=== ' ' || ch
=== '\'') {
1849 this.params
.push(this.currentParam
);
1850 this.currentParam
= 0;
1853 if (ch
=== ';') break;
1855 this.state
= normal
;
1859 // Cursor Up Ps Times (default = 1) (CUU).
1861 this.cursorUp(this.params
);
1865 // Cursor Down Ps Times (default = 1) (CUD).
1867 this.cursorDown(this.params
);
1871 // Cursor Forward Ps Times (default = 1) (CUF).
1873 this.cursorForward(this.params
);
1877 // Cursor Backward Ps Times (default = 1) (CUB).
1879 this.cursorBackward(this.params
);
1883 // Cursor Position [row;column] (default = [1,1]) (CUP).
1885 this.cursorPos(this.params
);
1888 // CSI Ps J Erase in Display (ED).
1890 this.eraseInDisplay(this.params
);
1893 // CSI Ps K Erase in Line (EL).
1895 this.eraseInLine(this.params
);
1898 // CSI Pm m Character Attributes (SGR).
1901 this.charAttributes(this.params
);
1905 // CSI Ps n Device Status Report (DSR).
1908 this.deviceStatus(this.params
);
1917 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
1919 this.insertChars(this.params
);
1923 // Cursor Next Line Ps Times (default = 1) (CNL).
1925 this.cursorNextLine(this.params
);
1929 // Cursor Preceding Line Ps Times (default = 1) (CNL).
1931 this.cursorPrecedingLine(this.params
);
1935 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
1937 this.cursorCharAbsolute(this.params
);
1941 // Insert Ps Line(s) (default = 1) (IL).
1943 this.insertLines(this.params
);
1947 // Delete Ps Line(s) (default = 1) (DL).
1949 this.deleteLines(this.params
);
1953 // Delete Ps Character(s) (default = 1) (DCH).
1955 this.deleteChars(this.params
);
1959 // Erase Ps Character(s) (default = 1) (ECH).
1961 this.eraseChars(this.params
);
1964 // CSI Pm ` Character Position Absolute
1965 // [column] (default = [row,1]) (HPA).
1967 this.charPosAbsolute(this.params
);
1971 // Horizontal Position Relative
1973 this.HPositionRelative(this.params
);
1977 // Send Device Attributes (Primary DA).
1979 // Send Device Attributes (Secondary DA)
1981 this.sendDeviceAttributes(this.params
);
1985 // Line Position Absolute [row] (default = [1,column]) (VPA).
1987 this.linePosAbsolute(this.params
);
1990 // 145 65 e * VPR - Vertical Position Relative
1992 this.VPositionRelative(this.params
);
1996 // Horizontal and Vertical Position [row;column] (default =
1999 this.HVPosition(this.params
);
2002 // CSI Pm h Set Mode (SM).
2003 // CSI ? Pm h - mouse escape codes, cursor escape codes
2005 this.setMode(this.params
);
2008 // CSI Pm l Reset Mode (RM).
2011 this.resetMode(this.params
);
2015 // Set Scrolling Region [top;bottom] (default = full size of win-
2019 this.setScrollRegion(this.params
);
2023 // Save cursor (ANSI.SYS).
2025 this.saveCursor(this.params
);
2029 // Restore cursor (ANSI.SYS).
2031 this.restoreCursor(this.params
);
2039 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
2041 this.cursorForwardTab(this.params
);
2044 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
2046 this.scrollUp(this.params
);
2049 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
2050 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
2053 // if (this.prefix === '>') {
2054 // this.resetTitleModes(this.params);
2057 // if (this.params.length > 2) {
2058 // this.initMouseTracking(this.params);
2061 if (this.params
.length
< 2 && !this.prefix
) {
2062 this.scrollDown(this.params
);
2067 // Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
2069 this.cursorBackwardTab(this.params
);
2072 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
2074 this.repeatPrecedingCharacter(this.params
);
2077 // CSI Ps g Tab Clear (TBC).
2079 this.tabClear(this.params
);
2082 // CSI Pm i Media Copy (MC).
2085 // this.mediaCopy(this.params);
2088 // CSI Pm m Character Attributes (SGR).
2090 // case 'm': // duplicate
2091 // if (this.prefix === '>') {
2092 // this.setResources(this.params);
2094 // this.charAttributes(this.params);
2098 // CSI Ps n Device Status Report (DSR).
2100 // case 'n': // duplicate
2101 // if (this.prefix === '>') {
2102 // this.disableModifiers(this.params);
2104 // this.deviceStatus(this.params);
2108 // CSI > Ps p Set pointer mode.
2109 // CSI ! p Soft terminal reset (DECSTR).
2111 // Request ANSI mode (DECRQM).
2113 // Request DEC private mode (DECRQM).
2116 switch (this.prefix
) {
2118 // this.setPointerMode(this.params);
2121 this.softReset(this.params
);
2124 // if (this.postfix === '$') {
2125 // this.requestPrivateMode(this.params);
2129 // if (this.postfix === '"') {
2130 // this.setConformanceLevel(this.params);
2131 // } else if (this.postfix === '$') {
2132 // this.requestAnsiMode(this.params);
2138 // CSI Ps q Load LEDs (DECLL).
2142 // if (this.postfix === ' ') {
2143 // this.setCursorStyle(this.params);
2146 // if (this.postfix === '"') {
2147 // this.setCharProtectionAttr(this.params);
2150 // this.loadLEDs(this.params);
2154 // Set Scrolling Region [top;bottom] (default = full size of win-
2157 // CSI Pt; Pl; Pb; Pr; Ps$ r
2158 // case 'r': // duplicate
2159 // if (this.prefix === '?') {
2160 // this.restorePrivateValues(this.params);
2161 // } else if (this.postfix === '$') {
2162 // this.setAttrInRectangle(this.params);
2164 // this.setScrollRegion(this.params);
2168 // CSI s Save cursor (ANSI.SYS).
2170 // case 's': // duplicate
2171 // if (this.prefix === '?') {
2172 // this.savePrivateValues(this.params);
2174 // this.saveCursor(this.params);
2178 // CSI Ps ; Ps ; Ps t
2179 // CSI Pt; Pl; Pb; Pr; Ps$ t
2183 // if (this.postfix === '$') {
2184 // this.reverseAttrInRectangle(this.params);
2185 // } else if (this.postfix === ' ') {
2186 // this.setWarningBellVolume(this.params);
2188 // if (this.prefix === '>') {
2189 // this.setTitleModeFeature(this.params);
2191 // this.manipulateWindow(this.params);
2196 // CSI u Restore cursor (ANSI.SYS).
2198 // case 'u': // duplicate
2199 // if (this.postfix === ' ') {
2200 // this.setMarginBellVolume(this.params);
2202 // this.restoreCursor(this.params);
2206 // CSI Pt; Pl; Pb; Pr; Pp; Pt; Pl; Pp$ v
2208 // if (this.postfix === '$') {
2209 // this.copyRectagle(this.params);
2213 // CSI Pt ; Pl ; Pb ; Pr ' w
2215 // if (this.postfix === '\'') {
2216 // this.enableFilterRectangle(this.params);
2220 // CSI Ps x Request Terminal Parameters (DECREQTPARM).
2221 // CSI Ps x Select Attribute Change Extent (DECSACE).
2222 // CSI Pc; Pt; Pl; Pb; Pr$ x
2224 // if (this.postfix === '$') {
2225 // this.fillRectangle(this.params);
2227 // this.requestParameters(this.params);
2228 // //this.__(this.params);
2233 // CSI Pt; Pl; Pb; Pr$ z
2235 // if (this.postfix === '\'') {
2236 // this.enableLocatorReporting(this.params);
2237 // } else if (this.postfix === '$') {
2238 // this.eraseRectangle(this.params);
2243 // CSI Pt; Pl; Pb; Pr$ {
2245 // if (this.postfix === '\'') {
2246 // this.setLocatorEvents(this.params);
2247 // } else if (this.postfix === '$') {
2248 // this.selectiveEraseRectangle(this.params);
2254 // if (this.postfix === '\'') {
2255 // this.requestLocatorPosition(this.params);
2260 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
2262 // if (this.postfix === ' ') {
2263 // this.insertColumns(this.params);
2268 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
2270 // if (this.postfix === ' ') {
2271 // this.deleteColumns(this.params);
2276 this.error('Unknown CSI code: %s.', ch
);
2285 if (ch
=== '\x1b' || ch
=== '\x07') {
2286 if (ch
=== '\x1b') i
++;
2288 switch (this.prefix
) {
2289 // User-Defined Keys (DECUDK).
2293 // Request Status String (DECRQSS).
2294 // test: echo -e '\eP$q"p\e\\'
2296 var pt
= this.currentParam
2313 + (this.scrollTop
+ 1)
2315 + (this.scrollBottom
+ 1)
2325 this.error('Unknown DCS Pt: %s.', pt
);
2330 this.send('\x1bP' + +valid
+ '$r' + pt
+ '\x1b\\');
2333 // Set Termcap/Terminfo Data (xterm, experimental).
2337 // Request Termcap/Terminfo String (xterm, experimental)
2338 // Regular xterm does not even respond to this sequence.
2339 // This can cause a small glitch in vim.
2340 // test: echo -ne '\eP+q6b64\e\\'
2342 var pt
= this.currentParam
2345 this.send('\x1bP' + +valid
+ '+r' + pt
+ '\x1b\\');
2349 this.error('Unknown DCS prefix: %s.', this.prefix
);
2353 this.currentParam
= 0;
2355 this.state
= normal
;
2356 } else if (!this.currentParam
) {
2357 if (!this.prefix
&& ch
!== '$' && ch
!== '+') {
2358 this.currentParam
= ch
;
2359 } else if (this.prefix
.length
=== 2) {
2360 this.currentParam
= ch
;
2365 this.currentParam
+= ch
;
2371 if (ch
=== '\x1b' || ch
=== '\x07') {
2372 if (ch
=== '\x1b') i
++;
2373 this.state
= normal
;
2379 this.updateRange(this.y
);
2380 this.refresh(this.refreshStart
, this.refreshEnd
);
2384 * Writes text to the terminal, followed by a break line character (\n).
2385 * @param {string} text The text to write to the terminal.
2387 Terminal
.prototype.writeln = function(data
) {
2388 this.write(data
+ '\r\n');
2392 * Attaches a custom keydown handler which is run before keys are processed, giving consumers of
2393 * xterm.js ultimate control as to what keys should be processed by the terminal and what keys
2395 * @param {function} customKeydownHandler The custom KeyboardEvent handler to attach. This is a
2396 * function that takes a KeyboardEvent, allowing consumers to stop propogation and/or prevent
2397 * the default action. The function returns whether the event should be processed by xterm.js.
2399 Terminal
.prototype.attachCustomKeydownHandler = function(customKeydownHandler
) {
2400 this.customKeydownHandler
= customKeydownHandler
;
2404 * Handle a keydown event
2406 * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
2407 * @param {KeyboardEvent} ev The keydown event to be handled.
2409 Terminal
.prototype.keyDown = function(ev
) {
2410 if (this.customKeydownHandler
&& this.customKeydownHandler(ev
) === false) {
2414 if (!this.compositionHelper
.keydown
.bind(this.compositionHelper
)(ev
)) {
2415 if (this.ybase
!== this.ydisp
) {
2416 this.scrollToBottom();
2422 var result
= this.evaluateKeyEscapeSequence(ev
);
2424 if (result
.scrollDisp
) {
2425 this.scrollDisp(result
.scrollDisp
);
2426 return this.cancel(ev
, true);
2429 if (isThirdLevelShift(this, ev
)) {
2433 if (result
.cancel
) {
2434 // The event is canceled at the end already, is this necessary?
2435 this.cancel(ev
, true);
2442 this.emit('keydown', ev
);
2443 this.emit('key', result
.key
, ev
);
2445 this.handler(result
.key
);
2447 return this.cancel(ev
, true);
2451 * Returns an object that determines how a KeyboardEvent should be handled. The key of the
2452 * returned value is the new key code to pass to the PTY.
2454 * Reference: http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
2455 * @param {KeyboardEvent} ev The keyboard event to be translated to key escape sequence.
2457 Terminal
.prototype.evaluateKeyEscapeSequence = function(ev
) {
2459 // Whether to cancel event propogation (NOTE: this may not be needed since the event is
2460 // canceled at the end of keyDown
2462 // The new key even to emit
2464 // The number of characters to scroll, if this is defined it will cancel the event
2465 scrollDisp
: undefined
2467 var modifiers
= ev
.shiftKey
<< 0 | ev
.altKey
<< 1 | ev
.ctrlKey
<< 2 | ev
.metaKey
<< 3;
2468 switch (ev
.keyCode
) {
2472 result
.key
= '\x08'; // ^H
2475 result
.key
= '\x7f'; // ^?
2480 result
.key
= '\x1b[Z';
2484 result
.cancel
= true;
2489 result
.cancel
= true;
2493 result
.key
= '\x1b';
2494 result
.cancel
= true;
2499 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'D';
2500 // HACK: Make Alt + left-arrow behave like Ctrl + left-arrow: move one word backwards
2501 // http://unix.stackexchange.com/a/108106
2502 // macOS uses different escape sequences than linux
2503 if (result
.key
== '\x1b[1;3D') {
2504 result
.key
= (this.browser
.isMac
) ? '\x1bb' : '\x1b[1;5D';
2506 } else if (this.applicationCursor
) {
2507 result
.key
= '\x1bOD';
2509 result
.key
= '\x1b[D';
2515 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'C';
2516 // HACK: Make Alt + right-arrow behave like Ctrl + right-arrow: move one word forward
2517 // http://unix.stackexchange.com/a/108106
2518 // macOS uses different escape sequences than linux
2519 if (result
.key
== '\x1b[1;3C') {
2520 result
.key
= (this.browser
.isMac
) ? '\x1bf' : '\x1b[1;5C';
2522 } else if (this.applicationCursor
) {
2523 result
.key
= '\x1bOC';
2525 result
.key
= '\x1b[C';
2531 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'A';
2532 // HACK: Make Alt + up-arrow behave like Ctrl + up-arrow
2533 // http://unix.stackexchange.com/a/108106
2534 if (result
.key
== '\x1b[1;3A') {
2535 result
.key
= '\x1b[1;5A';
2537 } else if (this.applicationCursor
) {
2538 result
.key
= '\x1bOA';
2540 result
.key
= '\x1b[A';
2546 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'B';
2547 // HACK: Make Alt + down-arrow behave like Ctrl + down-arrow
2548 // http://unix.stackexchange.com/a/108106
2549 if (result
.key
== '\x1b[1;3B') {
2550 result
.key
= '\x1b[1;5B';
2552 } else if (this.applicationCursor
) {
2553 result
.key
= '\x1bOB';
2555 result
.key
= '\x1b[B';
2560 if (!ev
.shiftKey
&& !ev
.ctrlKey
) {
2561 // <Ctrl> or <Shift> + <Insert> are used to
2562 // copy-paste on some systems.
2563 result
.key
= '\x1b[2~';
2569 result
.key
= '\x1b[3;' + (modifiers
+ 1) + '~';
2571 result
.key
= '\x1b[3~';
2577 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'H';
2578 else if (this.applicationCursor
)
2579 result
.key
= '\x1bOH';
2581 result
.key
= '\x1b[H';
2586 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'F';
2587 else if (this.applicationCursor
)
2588 result
.key
= '\x1bOF';
2590 result
.key
= '\x1b[F';
2595 result
.scrollDisp
= -(this.rows
- 1);
2597 result
.key
= '\x1b[5~';
2603 result
.scrollDisp
= this.rows
- 1;
2605 result
.key
= '\x1b[6~';
2611 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'P';
2613 result
.key
= '\x1bOP';
2618 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'Q';
2620 result
.key
= '\x1bOQ';
2625 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'R';
2627 result
.key
= '\x1bOR';
2632 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'S';
2634 result
.key
= '\x1bOS';
2639 result
.key
= '\x1b[15;' + (modifiers
+ 1) + '~';
2641 result
.key
= '\x1b[15~';
2646 result
.key
= '\x1b[17;' + (modifiers
+ 1) + '~';
2648 result
.key
= '\x1b[17~';
2653 result
.key
= '\x1b[18;' + (modifiers
+ 1) + '~';
2655 result
.key
= '\x1b[18~';
2660 result
.key
= '\x1b[19;' + (modifiers
+ 1) + '~';
2662 result
.key
= '\x1b[19~';
2667 result
.key
= '\x1b[20;' + (modifiers
+ 1) + '~';
2669 result
.key
= '\x1b[20~';
2674 result
.key
= '\x1b[21;' + (modifiers
+ 1) + '~';
2676 result
.key
= '\x1b[21~';
2681 result
.key
= '\x1b[23;' + (modifiers
+ 1) + '~';
2683 result
.key
= '\x1b[23~';
2688 result
.key
= '\x1b[24;' + (modifiers
+ 1) + '~';
2690 result
.key
= '\x1b[24~';
2695 if (ev
.ctrlKey
&& !ev
.shiftKey
&& !ev
.altKey
&& !ev
.metaKey
) {
2696 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2697 result
.key
= String
.fromCharCode(ev
.keyCode
- 64);
2698 } else if (ev
.keyCode
=== 32) {
2700 result
.key
= String
.fromCharCode(0);
2701 } else if (ev
.keyCode
>= 51 && ev
.keyCode
<= 55) {
2702 // escape, file sep, group sep, record sep, unit sep
2703 result
.key
= String
.fromCharCode(ev
.keyCode
- 51 + 27);
2704 } else if (ev
.keyCode
=== 56) {
2706 result
.key
= String
.fromCharCode(127);
2707 } else if (ev
.keyCode
=== 219) {
2708 // ^[ - Control Sequence Introducer (CSI)
2709 result
.key
= String
.fromCharCode(27);
2710 } else if (ev
.keyCode
=== 220) {
2711 // ^\ - String Terminator (ST)
2712 result
.key
= String
.fromCharCode(28);
2713 } else if (ev
.keyCode
=== 221) {
2714 // ^] - Operating System Command (OSC)
2715 result
.key
= String
.fromCharCode(29);
2717 } else if (!this.browser
.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) {
2718 // On Mac this is a third level shift. Use <Esc> instead.
2719 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2720 result
.key
= '\x1b' + String
.fromCharCode(ev
.keyCode
+ 32);
2721 } else if (ev
.keyCode
=== 192) {
2722 result
.key
= '\x1b`';
2723 } else if (ev
.keyCode
>= 48 && ev
.keyCode
<= 57) {
2724 result
.key
= '\x1b' + (ev
.keyCode
- 48);
2733 * Set the G level of the terminal
2736 Terminal
.prototype.setgLevel = function(g
) {
2738 this.charset
= this.charsets
[g
];
2742 * Set the charset for the given G level of the terminal
2746 Terminal
.prototype.setgCharset = function(g
, charset
) {
2747 this.charsets
[g
] = charset
;
2748 if (this.glevel
=== g
) {
2749 this.charset
= charset
;
2754 * Handle a keypress event.
2756 * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
2757 * @param {KeyboardEvent} ev The keypress event to be handled.
2759 Terminal
.prototype.keyPress = function(ev
) {
2766 } else if (ev
.which
== null) {
2768 } else if (ev
.which
!== 0 && ev
.charCode
!== 0) {
2775 (ev
.altKey
|| ev
.ctrlKey
|| ev
.metaKey
) && !isThirdLevelShift(this, ev
)
2780 key
= String
.fromCharCode(key
);
2782 this.emit('keypress', key
, ev
);
2783 this.emit('key', key
, ev
);
2791 * Send data for handling to the terminal
2792 * @param {string} data
2794 Terminal
.prototype.send = function(data
) {
2798 setTimeout(function() {
2799 self
.handler(self
.queue
);
2809 * Note: We could do sweet things with webaudio here
2811 Terminal
.prototype.bell = function() {
2812 if (!this.visualBell
) return;
2814 this.element
.style
.borderColor
= 'white';
2815 setTimeout(function() {
2816 self
.element
.style
.borderColor
= '';
2818 if (this.popOnBell
) this.focus();
2822 * Log the current state to the console.
2824 Terminal
.prototype.log = function() {
2825 if (!this.debug
) return;
2826 if (!this.context
.console
|| !this.context
.console
.log
) return;
2827 var args
= Array
.prototype.slice
.call(arguments
);
2828 this.context
.console
.log
.apply(this.context
.console
, args
);
2832 * Log the current state as error to the console.
2834 Terminal
.prototype.error = function() {
2835 if (!this.debug
) return;
2836 if (!this.context
.console
|| !this.context
.console
.error
) return;
2837 var args
= Array
.prototype.slice
.call(arguments
);
2838 this.context
.console
.error
.apply(this.context
.console
, args
);
2842 * Resizes the terminal.
2844 * @param {number} x The number of columns to resize to.
2845 * @param {number} y The number of rows to resize to.
2847 Terminal
.prototype.resize = function(x
, y
) {
2855 if (x
=== this.cols
&& y
=== this.rows
) {
2865 ch
= [this.defAttr
, ' ', 1]; // does xterm use the default attr?
2866 i
= this.lines
.length
;
2868 while (this.lines
.get(i
).length
< x
) {
2869 this.lines
.get(i
).push(ch
);
2873 i
= this.lines
.length
;
2875 while (this.lines
.get(i
).length
> x
) {
2876 this.lines
.get(i
).pop();
2889 // y is rows, not this.y
2890 if (this.lines
.length
< y
+ this.ybase
) {
2891 if (this.ybase
> 0 && this.lines
.length
<= this.ybase
+ this.y
+ addToY
+ 1) {
2892 // There is room above the buffer and there are no empty elements below the line,
2896 if (this.ydisp
> 0) {
2897 // Viewport is at the top of the buffer, must increase downwards
2901 // Add a blank line if there is no buffer left at the top to scroll to, or if there
2902 // are blank lines after the cursor
2903 this.lines
.push(this.blankLine());
2906 if (this.children
.length
< y
) {
2912 if (this.lines
.length
> y
+ this.ybase
) {
2913 if (this.lines
.length
> this.ybase
+ this.y
+ 1) {
2914 // The line is a blank line below the cursor, remove it
2917 // The line is the cursor, scroll down
2922 if (this.children
.length
> y
) {
2923 el
= this.children
.shift();
2925 el
.parentNode
.removeChild(el
);
2931 // Make sure that the cursor stays on screen
2944 this.scrollBottom
= y
- 1;
2946 this.refresh(0, this.rows
- 1);
2950 this.geometry
= [this.cols
, this.rows
];
2951 this.emit('resize', {terminal
: this, cols
: x
, rows
: y
});
2955 * Updates the range of rows to refresh
2956 * @param {number} y The number of rows to refresh next.
2958 Terminal
.prototype.updateRange = function(y
) {
2959 if (y
< this.refreshStart
) this.refreshStart
= y
;
2960 if (y
> this.refreshEnd
) this.refreshEnd
= y
;
2961 // if (y > this.refreshEnd) {
2962 // this.refreshEnd = y;
2963 // if (y > this.rows - 1) {
2964 // this.refreshEnd = this.rows - 1;
2970 * Set the range of refreshing to the maximum value
2972 Terminal
.prototype.maxRange = function() {
2973 this.refreshStart
= 0;
2974 this.refreshEnd
= this.rows
- 1;
2980 * Setup the tab stops.
2983 Terminal
.prototype.setupStops = function(i
) {
2985 if (!this.tabs
[i
]) {
2986 i
= this.prevStop(i
);
2993 for (; i
< this.cols
; i
+= 8) {
2994 this.tabs
[i
] = true;
3000 * Move the cursor to the previous tab stop from the given position (default is current).
3001 * @param {number} x The position to move the cursor to the previous tab stop.
3003 Terminal
.prototype.prevStop = function(x
) {
3004 if (x
== null) x
= this.x
;
3005 while (!this.tabs
[--x
] && x
> 0);
3006 return x
>= this.cols
3013 * Move the cursor one tab stop forward from the given position (default is current).
3014 * @param {number} x The position to move the cursor one tab stop forward.
3016 Terminal
.prototype.nextStop = function(x
) {
3017 if (x
== null) x
= this.x
;
3018 while (!this.tabs
[++x
] && x
< this.cols
);
3019 return x
>= this.cols
3026 * Erase in the identified line everything from "x" to the end of the line (right).
3027 * @param {number} x The column from which to start erasing to the end of the line.
3028 * @param {number} y The line in which to operate.
3030 Terminal
.prototype.eraseRight = function(x
, y
) {
3031 var line
= this.lines
.get(this.ybase
+ y
)
3032 , ch
= [this.eraseAttr(), ' ', 1]; // xterm
3035 for (; x
< this.cols
; x
++) {
3039 this.updateRange(y
);
3045 * Erase in the identified line everything from "x" to the start of the line (left).
3046 * @param {number} x The column from which to start erasing to the start of the line.
3047 * @param {number} y The line in which to operate.
3049 Terminal
.prototype.eraseLeft = function(x
, y
) {
3050 var line
= this.lines
.get(this.ybase
+ y
)
3051 , ch
= [this.eraseAttr(), ' ', 1]; // xterm
3054 while (x
--) line
[x
] = ch
;
3056 this.updateRange(y
);
3060 * Clears the entire buffer, making the prompt line the new first line.
3062 Terminal
.prototype.clear = function() {
3063 if (this.ybase
=== 0 && this.y
=== 0) {
3064 // Don't clear if it's already clear
3067 this.lines
.set(0, this.lines
.get(this.ybase
+ this.y
));
3068 this.lines
.length
= 1;
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 * Move the cursor up one row, inserting a new blank line if necessary.
3180 Terminal
.prototype.reverseIndex = function() {
3182 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
.shiftElements(this.y
+ this.ybase
, this.rows
- 1, 1);
3187 this.lines
.set(this.y
+ this.ybase
, this.blankLine(true));
3188 this.updateRange(this.scrollTop
);
3189 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
.get(row
).splice(j
++, 0, ch
);
3648 this.lines
.get(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 if (this.lines
.length
=== this.lines
.maxLength
) {
3709 // Trim the start of lines to make room for the new line
3710 this.lines
.trimStart(1);
3716 // test: echo -e '\e[44m\e[1L\e[0m'
3717 // blankLine(true) - xterm/linux behavior
3718 this.lines
.splice(row
, 0, this.blankLine(true));
3719 this.lines
.splice(j
, 1);
3723 this.updateRange(this.y
);
3724 this.updateRange(this.scrollBottom
);
3730 * Delete Ps Line(s) (default = 1) (DL).
3732 Terminal
.prototype.deleteLines = function(params
) {
3736 if (param
< 1) param
= 1;
3737 row
= this.y
+ this.ybase
;
3739 j
= this.rows
- 1 - this.scrollBottom
;
3740 j
= this.rows
- 1 + this.ybase
- j
;
3743 if (this.lines
.length
=== this.lines
.maxLength
) {
3744 // Trim the start of lines to make room for the new line
3745 this.lines
.trimStart(1);
3749 // test: echo -e '\e[44m\e[1M\e[0m'
3750 // blankLine(true) - xterm/linux behavior
3751 this.lines
.splice(j
+ 1, 0, this.blankLine(true));
3752 this.lines
.splice(row
, 1);
3756 this.updateRange(this.y
);
3757 this.updateRange(this.scrollBottom
);
3763 * Delete Ps Character(s) (default = 1) (DCH).
3765 Terminal
.prototype.deleteChars = function(params
) {
3769 if (param
< 1) param
= 1;
3771 row
= this.y
+ this.ybase
;
3772 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3775 this.lines
.get(row
).splice(this.x
, 1);
3776 this.lines
.get(row
).push(ch
);
3782 * Erase Ps Character(s) (default = 1) (ECH).
3784 Terminal
.prototype.eraseChars = function(params
) {
3785 var param
, row
, j
, ch
;
3788 if (param
< 1) param
= 1;
3790 row
= this.y
+ this.ybase
;
3792 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3794 while (param
-- && j
< this.cols
) {
3795 this.lines
.get(row
)[j
++] = ch
;
3800 * CSI Pm ` Character Position Absolute
3801 * [column] (default = [row,1]) (HPA).
3803 Terminal
.prototype.charPosAbsolute = function(params
) {
3804 var param
= params
[0];
3805 if (param
< 1) param
= 1;
3807 if (this.x
>= this.cols
) {
3808 this.x
= this.cols
- 1;
3815 * Horizontal Position Relative
3818 Terminal
.prototype.HPositionRelative = function(params
) {
3819 var param
= params
[0];
3820 if (param
< 1) param
= 1;
3822 if (this.x
>= this.cols
) {
3823 this.x
= this.cols
- 1;
3829 * CSI Ps c Send Device Attributes (Primary DA).
3830 * Ps = 0 or omitted -> request attributes from terminal. The
3831 * response depends on the decTerminalID resource setting.
3832 * -> CSI ? 1 ; 2 c (``VT100 with Advanced Video Option'')
3833 * -> CSI ? 1 ; 0 c (``VT101 with No Options'')
3834 * -> CSI ? 6 c (``VT102'')
3835 * -> CSI ? 6 0 ; 1 ; 2 ; 6 ; 8 ; 9 ; 1 5 ; c (``VT220'')
3836 * The VT100-style response parameters do not mean anything by
3837 * themselves. VT220 parameters do, telling the host what fea-
3838 * tures the terminal supports:
3839 * Ps = 1 -> 132-columns.
3840 * Ps = 2 -> Printer.
3841 * Ps = 6 -> Selective erase.
3842 * Ps = 8 -> User-defined keys.
3843 * Ps = 9 -> National replacement character sets.
3844 * Ps = 1 5 -> Technical characters.
3845 * Ps = 2 2 -> ANSI color, e.g., VT525.
3846 * Ps = 2 9 -> ANSI text locator (i.e., DEC Locator mode).
3848 * Send Device Attributes (Secondary DA).
3849 * Ps = 0 or omitted -> request the terminal's identification
3850 * code. The response depends on the decTerminalID resource set-
3851 * ting. It should apply only to VT220 and up, but xterm extends
3853 * -> CSI > Pp ; Pv ; Pc c
3854 * where Pp denotes the terminal type
3855 * Pp = 0 -> ``VT100''.
3856 * Pp = 1 -> ``VT220''.
3857 * and Pv is the firmware version (for xterm, this was originally
3858 * the XFree86 patch number, starting with 95). In a DEC termi-
3859 * nal, Pc indicates the ROM cartridge registration number and is
3862 * xterm/charproc.c - line 2012, for more information.
3863 * vim responds with ^[[?0c or ^[[?1c after the terminal's response (?)
3865 Terminal
.prototype.sendDeviceAttributes = function(params
) {
3866 if (params
[0] > 0) return;
3869 if (this.is('xterm')
3870 || this.is('rxvt-unicode')
3871 || this.is('screen')) {
3872 this.send('\x1b[?1;2c');
3873 } else if (this.is('linux')) {
3874 this.send('\x1b[?6c');
3876 } else if (this.prefix
=== '>') {
3878 // seem to spit this
3879 // out around ~370 times (?).
3880 if (this.is('xterm')) {
3881 this.send('\x1b[>0;276;0c');
3882 } else if (this.is('rxvt-unicode')) {
3883 this.send('\x1b[>85;95;0c');
3884 } else if (this.is('linux')) {
3885 // not supported by linux console.
3886 // linux console echoes parameters.
3887 this.send(params
[0] + 'c');
3888 } else if (this.is('screen')) {
3889 this.send('\x1b[>83;40003;0c');
3897 * Line Position Absolute [row] (default = [1,column]) (VPA).
3899 Terminal
.prototype.linePosAbsolute = 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;
3910 * 145 65 e * VPR - Vertical Position Relative
3913 Terminal
.prototype.VPositionRelative = function(params
) {
3914 var param
= params
[0];
3915 if (param
< 1) param
= 1;
3917 if (this.y
>= this.rows
) {
3918 this.y
= this.rows
- 1;
3925 * Horizontal and Vertical Position [row;column] (default =
3928 Terminal
.prototype.HVPosition = function(params
) {
3929 if (params
[0] < 1) params
[0] = 1;
3930 if (params
[1] < 1) params
[1] = 1;
3932 this.y
= params
[0] - 1;
3933 if (this.y
>= this.rows
) {
3934 this.y
= this.rows
- 1;
3937 this.x
= params
[1] - 1;
3938 if (this.x
>= this.cols
) {
3939 this.x
= this.cols
- 1;
3945 * CSI Pm h Set Mode (SM).
3946 * Ps = 2 -> Keyboard Action Mode (AM).
3947 * Ps = 4 -> Insert Mode (IRM).
3948 * Ps = 1 2 -> Send/receive (SRM).
3949 * Ps = 2 0 -> Automatic Newline (LNM).
3951 * DEC Private Mode Set (DECSET).
3952 * Ps = 1 -> Application Cursor Keys (DECCKM).
3953 * Ps = 2 -> Designate USASCII for character sets G0-G3
3954 * (DECANM), and set VT100 mode.
3955 * Ps = 3 -> 132 Column Mode (DECCOLM).
3956 * Ps = 4 -> Smooth (Slow) Scroll (DECSCLM).
3957 * Ps = 5 -> Reverse Video (DECSCNM).
3958 * Ps = 6 -> Origin Mode (DECOM).
3959 * Ps = 7 -> Wraparound Mode (DECAWM).
3960 * Ps = 8 -> Auto-repeat Keys (DECARM).
3961 * Ps = 9 -> Send Mouse X & Y on button press. See the sec-
3962 * tion Mouse Tracking.
3963 * Ps = 1 0 -> Show toolbar (rxvt).
3964 * Ps = 1 2 -> Start Blinking Cursor (att610).
3965 * Ps = 1 8 -> Print form feed (DECPFF).
3966 * Ps = 1 9 -> Set print extent to full screen (DECPEX).
3967 * Ps = 2 5 -> Show Cursor (DECTCEM).
3968 * Ps = 3 0 -> Show scrollbar (rxvt).
3969 * Ps = 3 5 -> Enable font-shifting functions (rxvt).
3970 * Ps = 3 8 -> Enter Tektronix Mode (DECTEK).
3971 * Ps = 4 0 -> Allow 80 -> 132 Mode.
3972 * Ps = 4 1 -> more(1) fix (see curses resource).
3973 * Ps = 4 2 -> Enable Nation Replacement Character sets (DECN-
3975 * Ps = 4 4 -> Turn On Margin Bell.
3976 * Ps = 4 5 -> Reverse-wraparound Mode.
3977 * Ps = 4 6 -> Start Logging. This is normally disabled by a
3978 * compile-time option.
3979 * Ps = 4 7 -> Use Alternate Screen Buffer. (This may be dis-
3980 * abled by the titeInhibit resource).
3981 * Ps = 6 6 -> Application keypad (DECNKM).
3982 * Ps = 6 7 -> Backarrow key sends backspace (DECBKM).
3983 * Ps = 1 0 0 0 -> Send Mouse X & Y on button press and
3984 * release. See the section Mouse Tracking.
3985 * Ps = 1 0 0 1 -> Use Hilite Mouse Tracking.
3986 * Ps = 1 0 0 2 -> Use Cell Motion Mouse Tracking.
3987 * Ps = 1 0 0 3 -> Use All Motion Mouse Tracking.
3988 * Ps = 1 0 0 4 -> Send FocusIn/FocusOut events.
3989 * Ps = 1 0 0 5 -> Enable Extended Mouse Mode.
3990 * Ps = 1 0 1 0 -> Scroll to bottom on tty output (rxvt).
3991 * Ps = 1 0 1 1 -> Scroll to bottom on key press (rxvt).
3992 * Ps = 1 0 3 4 -> Interpret "meta" key, sets eighth bit.
3993 * (enables the eightBitInput resource).
3994 * Ps = 1 0 3 5 -> Enable special modifiers for Alt and Num-
3995 * Lock keys. (This enables the numLock resource).
3996 * Ps = 1 0 3 6 -> Send ESC when Meta modifies a key. (This
3997 * enables the metaSendsEscape resource).
3998 * Ps = 1 0 3 7 -> Send DEL from the editing-keypad Delete
4000 * Ps = 1 0 3 9 -> Send ESC when Alt modifies a key. (This
4001 * enables the altSendsEscape resource).
4002 * Ps = 1 0 4 0 -> Keep selection even if not highlighted.
4003 * (This enables the keepSelection resource).
4004 * Ps = 1 0 4 1 -> Use the CLIPBOARD selection. (This enables
4005 * the selectToClipboard resource).
4006 * Ps = 1 0 4 2 -> Enable Urgency window manager hint when
4007 * Control-G is received. (This enables the bellIsUrgent
4009 * Ps = 1 0 4 3 -> Enable raising of the window when Control-G
4010 * is received. (enables the popOnBell resource).
4011 * Ps = 1 0 4 7 -> Use Alternate Screen Buffer. (This may be
4012 * disabled by the titeInhibit resource).
4013 * Ps = 1 0 4 8 -> Save cursor as in DECSC. (This may be dis-
4014 * abled by the titeInhibit resource).
4015 * Ps = 1 0 4 9 -> Save cursor as in DECSC and use Alternate
4016 * Screen Buffer, clearing it first. (This may be disabled by
4017 * the titeInhibit resource). This combines the effects of the 1
4018 * 0 4 7 and 1 0 4 8 modes. Use this with terminfo-based
4019 * applications rather than the 4 7 mode.
4020 * Ps = 1 0 5 0 -> Set terminfo/termcap function-key mode.
4021 * Ps = 1 0 5 1 -> Set Sun function-key mode.
4022 * Ps = 1 0 5 2 -> Set HP function-key mode.
4023 * Ps = 1 0 5 3 -> Set SCO function-key mode.
4024 * Ps = 1 0 6 0 -> Set legacy keyboard emulation (X11R6).
4025 * Ps = 1 0 6 1 -> Set VT220 keyboard emulation.
4026 * Ps = 2 0 0 4 -> Set bracketed paste mode.
4028 * http: *vt100.net/docs/vt220-rm/chapter4.html
4030 Terminal
.prototype.setMode = function(params
) {
4031 if (typeof params
=== 'object') {
4032 var l
= params
.length
4035 for (; i
< l
; i
++) {
4036 this.setMode(params
[i
]);
4045 this.insertMode
= true;
4048 //this.convertEol = true;
4051 } else if (this.prefix
=== '?') {
4054 this.applicationCursor
= true;
4057 this.setgCharset(0, Terminal
.charsets
.US
);
4058 this.setgCharset(1, Terminal
.charsets
.US
);
4059 this.setgCharset(2, Terminal
.charsets
.US
);
4060 this.setgCharset(3, Terminal
.charsets
.US
);
4061 // set VT100 mode here
4063 case 3: // 132 col mode
4064 this.savedCols
= this.cols
;
4065 this.resize(132, this.rows
);
4068 this.originMode
= true;
4071 this.wraparoundMode
= true;
4074 // this.cursorBlink = true;
4077 this.log('Serial port requested application keypad.');
4078 this.applicationKeypad
= true;
4079 this.viewport
.syncScrollArea();
4081 case 9: // X10 Mouse
4082 // no release, no motion, no wheel, no modifiers.
4083 case 1000: // vt200 mouse
4085 // no modifiers, except control on the wheel.
4086 case 1002: // button event mouse
4087 case 1003: // any event mouse
4088 // any event - sends motion events,
4089 // even if there is no button held down.
4090 this.x10Mouse
= params
=== 9;
4091 this.vt200Mouse
= params
=== 1000;
4092 this.normalMouse
= params
> 1000;
4093 this.mouseEvents
= true;
4094 this.element
.style
.cursor
= 'default';
4095 this.log('Binding to mouse events.');
4097 case 1004: // send focusin/focusout events
4100 this.sendFocus
= true;
4102 case 1005: // utf8 ext mode mouse
4103 this.utfMouse
= true;
4104 // for wide terminals
4105 // simply encodes large values as utf8 characters
4107 case 1006: // sgr ext mode mouse
4108 this.sgrMouse
= true;
4109 // for wide terminals
4110 // does not add 32 to fields
4111 // press: ^[[<b;x;yM
4112 // release: ^[[<b;x;ym
4114 case 1015: // urxvt ext mode mouse
4115 this.urxvtMouse
= true;
4116 // for wide terminals
4117 // numbers for fields
4119 // motion: ^[[b;x;yT
4121 case 25: // show cursor
4122 this.cursorHidden
= false;
4124 case 1049: // alt screen buffer cursor
4125 //this.saveCursor();
4127 case 47: // alt screen buffer
4128 case 1047: // alt screen buffer
4136 scrollTop
: this.scrollTop
,
4137 scrollBottom
: this.scrollBottom
,
4139 // XXX save charset(s) here?
4140 // charset: this.charset,
4141 // glevel: this.glevel,
4142 // charsets: this.charsets
4145 this.normal
= normal
;
4154 * CSI Pm l Reset Mode (RM).
4155 * Ps = 2 -> Keyboard Action Mode (AM).
4156 * Ps = 4 -> Replace Mode (IRM).
4157 * Ps = 1 2 -> Send/receive (SRM).
4158 * Ps = 2 0 -> Normal Linefeed (LNM).
4160 * DEC Private Mode Reset (DECRST).
4161 * Ps = 1 -> Normal Cursor Keys (DECCKM).
4162 * Ps = 2 -> Designate VT52 mode (DECANM).
4163 * Ps = 3 -> 80 Column Mode (DECCOLM).
4164 * Ps = 4 -> Jump (Fast) Scroll (DECSCLM).
4165 * Ps = 5 -> Normal Video (DECSCNM).
4166 * Ps = 6 -> Normal Cursor Mode (DECOM).
4167 * Ps = 7 -> No Wraparound Mode (DECAWM).
4168 * Ps = 8 -> No Auto-repeat Keys (DECARM).
4169 * Ps = 9 -> Don't send Mouse X & Y on button press.
4170 * Ps = 1 0 -> Hide toolbar (rxvt).
4171 * Ps = 1 2 -> Stop Blinking Cursor (att610).
4172 * Ps = 1 8 -> Don't print form feed (DECPFF).
4173 * Ps = 1 9 -> Limit print to scrolling region (DECPEX).
4174 * Ps = 2 5 -> Hide Cursor (DECTCEM).
4175 * Ps = 3 0 -> Don't show scrollbar (rxvt).
4176 * Ps = 3 5 -> Disable font-shifting functions (rxvt).
4177 * Ps = 4 0 -> Disallow 80 -> 132 Mode.
4178 * Ps = 4 1 -> No more(1) fix (see curses resource).
4179 * Ps = 4 2 -> Disable Nation Replacement Character sets (DEC-
4181 * Ps = 4 4 -> Turn Off Margin Bell.
4182 * Ps = 4 5 -> No Reverse-wraparound Mode.
4183 * Ps = 4 6 -> Stop Logging. (This is normally disabled by a
4184 * compile-time option).
4185 * Ps = 4 7 -> Use Normal Screen Buffer.
4186 * Ps = 6 6 -> Numeric keypad (DECNKM).
4187 * Ps = 6 7 -> Backarrow key sends delete (DECBKM).
4188 * Ps = 1 0 0 0 -> Don't send Mouse X & Y on button press and
4189 * release. See the section Mouse Tracking.
4190 * Ps = 1 0 0 1 -> Don't use Hilite Mouse Tracking.
4191 * Ps = 1 0 0 2 -> Don't use Cell Motion Mouse Tracking.
4192 * Ps = 1 0 0 3 -> Don't use All Motion Mouse Tracking.
4193 * Ps = 1 0 0 4 -> Don't send FocusIn/FocusOut events.
4194 * Ps = 1 0 0 5 -> Disable Extended Mouse Mode.
4195 * Ps = 1 0 1 0 -> Don't scroll to bottom on tty output
4197 * Ps = 1 0 1 1 -> Don't scroll to bottom on key press (rxvt).
4198 * Ps = 1 0 3 4 -> Don't interpret "meta" key. (This disables
4199 * the eightBitInput resource).
4200 * Ps = 1 0 3 5 -> Disable special modifiers for Alt and Num-
4201 * Lock keys. (This disables the numLock resource).
4202 * Ps = 1 0 3 6 -> Don't send ESC when Meta modifies a key.
4203 * (This disables the metaSendsEscape resource).
4204 * Ps = 1 0 3 7 -> Send VT220 Remove from the editing-keypad
4206 * Ps = 1 0 3 9 -> Don't send ESC when Alt modifies a key.
4207 * (This disables the altSendsEscape resource).
4208 * Ps = 1 0 4 0 -> Do not keep selection when not highlighted.
4209 * (This disables the keepSelection resource).
4210 * Ps = 1 0 4 1 -> Use the PRIMARY selection. (This disables
4211 * the selectToClipboard resource).
4212 * Ps = 1 0 4 2 -> Disable Urgency window manager hint when
4213 * Control-G is received. (This disables the bellIsUrgent
4215 * Ps = 1 0 4 3 -> Disable raising of the window when Control-
4216 * G is received. (This disables the popOnBell resource).
4217 * Ps = 1 0 4 7 -> Use Normal Screen Buffer, clearing screen
4218 * first if in the Alternate Screen. (This may be disabled by
4219 * the titeInhibit resource).
4220 * Ps = 1 0 4 8 -> Restore cursor as in DECRC. (This may be
4221 * disabled by the titeInhibit resource).
4222 * Ps = 1 0 4 9 -> Use Normal Screen Buffer and restore cursor
4223 * as in DECRC. (This may be disabled by the titeInhibit
4224 * resource). This combines the effects of the 1 0 4 7 and 1 0
4225 * 4 8 modes. Use this with terminfo-based applications rather
4226 * than the 4 7 mode.
4227 * Ps = 1 0 5 0 -> Reset terminfo/termcap function-key mode.
4228 * Ps = 1 0 5 1 -> Reset Sun function-key mode.
4229 * Ps = 1 0 5 2 -> Reset HP function-key mode.
4230 * Ps = 1 0 5 3 -> Reset SCO function-key mode.
4231 * Ps = 1 0 6 0 -> Reset legacy keyboard emulation (X11R6).
4232 * Ps = 1 0 6 1 -> Reset keyboard emulation to Sun/PC style.
4233 * Ps = 2 0 0 4 -> Reset bracketed paste mode.
4235 Terminal
.prototype.resetMode = function(params
) {
4236 if (typeof params
=== 'object') {
4237 var l
= params
.length
4240 for (; i
< l
; i
++) {
4241 this.resetMode(params
[i
]);
4250 this.insertMode
= false;
4253 //this.convertEol = false;
4256 } else if (this.prefix
=== '?') {
4259 this.applicationCursor
= false;
4262 if (this.cols
=== 132 && this.savedCols
) {
4263 this.resize(this.savedCols
, this.rows
);
4265 delete this.savedCols
;
4268 this.originMode
= false;
4271 this.wraparoundMode
= false;
4274 // this.cursorBlink = false;
4277 this.log('Switching back to normal keypad.');
4278 this.applicationKeypad
= false;
4279 this.viewport
.syncScrollArea();
4281 case 9: // X10 Mouse
4282 case 1000: // vt200 mouse
4283 case 1002: // button event mouse
4284 case 1003: // any event mouse
4285 this.x10Mouse
= false;
4286 this.vt200Mouse
= false;
4287 this.normalMouse
= false;
4288 this.mouseEvents
= false;
4289 this.element
.style
.cursor
= '';
4291 case 1004: // send focusin/focusout events
4292 this.sendFocus
= false;
4294 case 1005: // utf8 ext mode mouse
4295 this.utfMouse
= false;
4297 case 1006: // sgr ext mode mouse
4298 this.sgrMouse
= false;
4300 case 1015: // urxvt ext mode mouse
4301 this.urxvtMouse
= false;
4303 case 25: // hide cursor
4304 this.cursorHidden
= true;
4306 case 1049: // alt screen buffer cursor
4308 case 47: // normal screen buffer
4309 case 1047: // normal screen buffer - clearing it first
4311 this.lines
= this.normal
.lines
;
4312 this.ybase
= this.normal
.ybase
;
4313 this.ydisp
= this.normal
.ydisp
;
4314 this.x
= this.normal
.x
;
4315 this.y
= this.normal
.y
;
4316 this.scrollTop
= this.normal
.scrollTop
;
4317 this.scrollBottom
= this.normal
.scrollBottom
;
4318 this.tabs
= this.normal
.tabs
;
4320 // if (params === 1049) {
4321 // this.x = this.savedX;
4322 // this.y = this.savedY;
4324 this.refresh(0, this.rows
- 1);
4335 * Set Scrolling Region [top;bottom] (default = full size of win-
4339 Terminal
.prototype.setScrollRegion = function(params
) {
4340 if (this.prefix
) return;
4341 this.scrollTop
= (params
[0] || 1) - 1;
4342 this.scrollBottom
= (params
[1] || this.rows
) - 1;
4350 * Save cursor (ANSI.SYS).
4352 Terminal
.prototype.saveCursor = function(params
) {
4353 this.savedX
= this.x
;
4354 this.savedY
= this.y
;
4360 * Restore cursor (ANSI.SYS).
4362 Terminal
.prototype.restoreCursor = function(params
) {
4363 this.x
= this.savedX
|| 0;
4364 this.y
= this.savedY
|| 0;
4374 * Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
4376 Terminal
.prototype.cursorForwardTab = function(params
) {
4377 var param
= params
[0] || 1;
4379 this.x
= this.nextStop();
4385 * CSI Ps S Scroll up Ps lines (default = 1) (SU).
4387 Terminal
.prototype.scrollUp = function(params
) {
4388 var param
= params
[0] || 1;
4390 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
4391 this.lines
.splice(this.ybase
+ this.scrollBottom
, 0, this.blankLine());
4394 this.updateRange(this.scrollTop
);
4395 this.updateRange(this.scrollBottom
);
4400 * CSI Ps T Scroll down Ps lines (default = 1) (SD).
4402 Terminal
.prototype.scrollDown = function(params
) {
4403 var param
= params
[0] || 1;
4405 this.lines
.splice(this.ybase
+ this.scrollBottom
, 1);
4406 this.lines
.splice(this.ybase
+ this.scrollTop
, 0, this.blankLine());
4409 this.updateRange(this.scrollTop
);
4410 this.updateRange(this.scrollBottom
);
4415 * CSI Ps ; Ps ; Ps ; Ps ; Ps T
4416 * Initiate highlight mouse tracking. Parameters are
4417 * [func;startx;starty;firstrow;lastrow]. See the section Mouse
4420 Terminal
.prototype.initMouseTracking = function(params
) {
4421 // Relevant: DECSET 1001
4427 * Reset one or more features of the title modes to the default
4428 * value. Normally, "reset" disables the feature. It is possi-
4429 * ble to disable the ability to reset features by compiling a
4430 * different default for the title modes into xterm.
4431 * Ps = 0 -> Do not set window/icon labels using hexadecimal.
4432 * Ps = 1 -> Do not query window/icon labels using hexadeci-
4434 * Ps = 2 -> Do not set window/icon labels using UTF-8.
4435 * Ps = 3 -> Do not query window/icon labels using UTF-8.
4436 * (See discussion of "Title Modes").
4438 Terminal
.prototype.resetTitleModes = function(params
) {
4444 * CSI Ps Z Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
4446 Terminal
.prototype.cursorBackwardTab = function(params
) {
4447 var param
= params
[0] || 1;
4449 this.x
= this.prevStop();
4455 * CSI Ps b Repeat the preceding graphic character Ps times (REP).
4457 Terminal
.prototype.repeatPrecedingCharacter = function(params
) {
4458 var param
= params
[0] || 1
4459 , line
= this.lines
.get(this.ybase
+ this.y
)
4460 , ch
= line
[this.x
- 1] || [this.defAttr
, ' ', 1];
4462 while (param
--) line
[this.x
++] = ch
;
4467 * CSI Ps g Tab Clear (TBC).
4468 * Ps = 0 -> Clear Current Column (default).
4469 * Ps = 3 -> Clear All.
4471 * Ps = 2 -> Clear Stops on Line.
4472 * http://vt100.net/annarbor/aaa-ug/section6.html
4474 Terminal
.prototype.tabClear = function(params
) {
4475 var param
= params
[0];
4477 delete this.tabs
[this.x
];
4478 } else if (param
=== 3) {
4485 * CSI Pm i Media Copy (MC).
4486 * Ps = 0 -> Print screen (default).
4487 * Ps = 4 -> Turn off printer controller mode.
4488 * Ps = 5 -> Turn on printer controller mode.
4490 * Media Copy (MC, DEC-specific).
4491 * Ps = 1 -> Print line containing cursor.
4492 * Ps = 4 -> Turn off autoprint mode.
4493 * Ps = 5 -> Turn on autoprint mode.
4494 * Ps = 1 0 -> Print composed display, ignores DECPEX.
4495 * Ps = 1 1 -> Print all pages.
4497 Terminal
.prototype.mediaCopy = function(params
) {
4504 * Set or reset resource-values used by xterm to decide whether
4505 * to construct escape sequences holding information about the
4506 * modifiers pressed with a given key. The first parameter iden-
4507 * tifies the resource to set/reset. The second parameter is the
4508 * value to assign to the resource. If the second parameter is
4509 * omitted, the resource is reset to its initial value.
4510 * Ps = 1 -> modifyCursorKeys.
4511 * Ps = 2 -> modifyFunctionKeys.
4512 * Ps = 4 -> modifyOtherKeys.
4513 * If no parameters are given, all resources are reset to their
4516 Terminal
.prototype.setResources = function(params
) {
4523 * Disable modifiers which may be enabled via the CSI > Ps; Ps m
4524 * sequence. This corresponds to a resource value of "-1", which
4525 * cannot be set with the other sequence. The parameter identi-
4526 * fies the resource to be disabled:
4527 * Ps = 1 -> modifyCursorKeys.
4528 * Ps = 2 -> modifyFunctionKeys.
4529 * Ps = 4 -> modifyOtherKeys.
4530 * If the parameter is omitted, modifyFunctionKeys is disabled.
4531 * When modifyFunctionKeys is disabled, xterm uses the modifier
4532 * keys to make an extended sequence of functions rather than
4533 * adding a parameter to each function key to denote the modi-
4536 Terminal
.prototype.disableModifiers = function(params
) {
4543 * Set resource value pointerMode. This is used by xterm to
4544 * decide whether to hide the pointer cursor as the user types.
4545 * Valid values for the parameter:
4546 * Ps = 0 -> never hide the pointer.
4547 * Ps = 1 -> hide if the mouse tracking mode is not enabled.
4548 * Ps = 2 -> always hide the pointer. If no parameter is
4549 * given, xterm uses the default, which is 1 .
4551 Terminal
.prototype.setPointerMode = function(params
) {
4557 * CSI ! p Soft terminal reset (DECSTR).
4558 * http://vt100.net/docs/vt220-rm/table4-10.html
4560 Terminal
.prototype.softReset = function(params
) {
4561 this.cursorHidden
= false;
4562 this.insertMode
= false;
4563 this.originMode
= false;
4564 this.wraparoundMode
= false; // autowrap
4565 this.applicationKeypad
= false; // ?
4566 this.viewport
.syncScrollArea();
4567 this.applicationCursor
= false;
4569 this.scrollBottom
= this.rows
- 1;
4570 this.curAttr
= this.defAttr
;
4571 this.x
= this.y
= 0; // ?
4572 this.charset
= null;
4573 this.glevel
= 0; // ??
4574 this.charsets
= [null]; // ??
4580 * Request ANSI mode (DECRQM). For VT300 and up, reply is
4582 * where Ps is the mode number as in RM, and Pm is the mode
4584 * 0 - not recognized
4587 * 3 - permanently set
4588 * 4 - permanently reset
4590 Terminal
.prototype.requestAnsiMode = function(params
) {
4597 * Request DEC private mode (DECRQM). For VT300 and up, reply is
4599 * where Ps is the mode number as in DECSET, Pm is the mode value
4600 * as in the ANSI DECRQM.
4602 Terminal
.prototype.requestPrivateMode = function(params
) {
4609 * Set conformance level (DECSCL). Valid values for the first
4611 * Ps = 6 1 -> VT100.
4612 * Ps = 6 2 -> VT200.
4613 * Ps = 6 3 -> VT300.
4614 * Valid values for the second parameter:
4615 * Ps = 0 -> 8-bit controls.
4616 * Ps = 1 -> 7-bit controls (always set for VT100).
4617 * Ps = 2 -> 8-bit controls.
4619 Terminal
.prototype.setConformanceLevel = function(params
) {
4625 * CSI Ps q Load LEDs (DECLL).
4626 * Ps = 0 -> Clear all LEDS (default).
4627 * Ps = 1 -> Light Num Lock.
4628 * Ps = 2 -> Light Caps Lock.
4629 * Ps = 3 -> Light Scroll Lock.
4630 * Ps = 2 1 -> Extinguish Num Lock.
4631 * Ps = 2 2 -> Extinguish Caps Lock.
4632 * Ps = 2 3 -> Extinguish Scroll Lock.
4634 Terminal
.prototype.loadLEDs = function(params
) {
4641 * Set cursor style (DECSCUSR, VT520).
4642 * Ps = 0 -> blinking block.
4643 * Ps = 1 -> blinking block (default).
4644 * Ps = 2 -> steady block.
4645 * Ps = 3 -> blinking underline.
4646 * Ps = 4 -> steady underline.
4648 Terminal
.prototype.setCursorStyle = function(params
) {
4655 * Select character protection attribute (DECSCA). Valid values
4656 * for the parameter:
4657 * Ps = 0 -> DECSED and DECSEL can erase (default).
4658 * Ps = 1 -> DECSED and DECSEL cannot erase.
4659 * Ps = 2 -> DECSED and DECSEL can erase.
4661 Terminal
.prototype.setCharProtectionAttr = function(params
) {
4668 * Restore DEC Private Mode Values. The value of Ps previously
4669 * saved is restored. Ps values are the same as for DECSET.
4671 Terminal
.prototype.restorePrivateValues = function(params
) {
4677 * CSI Pt; Pl; Pb; Pr; Ps$ r
4678 * Change Attributes in Rectangular Area (DECCARA), VT400 and up.
4679 * Pt; Pl; Pb; Pr denotes the rectangle.
4680 * Ps denotes the SGR attributes to change: 0, 1, 4, 5, 7.
4681 * NOTE: xterm doesn't enable this code by default.
4683 Terminal
.prototype.setAttrInRectangle = function(params
) {
4693 for (; t
< b
+ 1; t
++) {
4694 line
= this.lines
.get(this.ybase
+ t
);
4695 for (i
= l
; i
< r
; i
++) {
4696 line
[i
] = [attr
, line
[i
][1]];
4701 this.updateRange(params
[0]);
4702 this.updateRange(params
[2]);
4707 * CSI Pc; Pt; Pl; Pb; Pr$ x
4708 * Fill Rectangular Area (DECFRA), VT420 and up.
4709 * Pc is the character to use.
4710 * Pt; Pl; Pb; Pr denotes the rectangle.
4711 * NOTE: xterm doesn't enable this code by default.
4713 Terminal
.prototype.fillRectangle = function(params
) {
4723 for (; t
< b
+ 1; t
++) {
4724 line
= this.lines
.get(this.ybase
+ t
);
4725 for (i
= l
; i
< r
; i
++) {
4726 line
[i
] = [line
[i
][0], String
.fromCharCode(ch
)];
4731 this.updateRange(params
[1]);
4732 this.updateRange(params
[3]);
4738 * Enable Locator Reporting (DECELR).
4739 * Valid values for the first parameter:
4740 * Ps = 0 -> Locator disabled (default).
4741 * Ps = 1 -> Locator enabled.
4742 * Ps = 2 -> Locator enabled for one report, then disabled.
4743 * The second parameter specifies the coordinate unit for locator
4745 * Valid values for the second parameter:
4746 * Pu = 0 <- or omitted -> default to character cells.
4747 * Pu = 1 <- device physical pixels.
4748 * Pu = 2 <- character cells.
4750 Terminal
.prototype.enableLocatorReporting = function(params
) {
4751 var val
= params
[0] > 0;
4752 //this.mouseEvents = val;
4753 //this.decLocator = val;
4758 * CSI Pt; Pl; Pb; Pr$ z
4759 * Erase Rectangular Area (DECERA), VT400 and up.
4760 * Pt; Pl; Pb; Pr denotes the rectangle.
4761 * NOTE: xterm doesn't enable this code by default.
4763 Terminal
.prototype.eraseRectangle = function(params
) {
4773 ch
= [this.eraseAttr(), ' ', 1]; // xterm?
4775 for (; t
< b
+ 1; t
++) {
4776 line
= this.lines
.get(this.ybase
+ t
);
4777 for (i
= l
; i
< r
; i
++) {
4783 this.updateRange(params
[0]);
4784 this.updateRange(params
[2]);
4790 * Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
4791 * NOTE: xterm doesn't enable this code by default.
4793 Terminal
.prototype.insertColumns = function() {
4794 var param
= params
[0]
4795 , l
= this.ybase
+ this.rows
4796 , ch
= [this.eraseAttr(), ' ', 1] // xterm?
4800 for (i
= this.ybase
; i
< l
; i
++) {
4801 this.lines
.get(i
).splice(this.x
+ 1, 0, ch
);
4802 this.lines
.get(i
).pop();
4812 * Delete P s Column(s) (default = 1) (DECDC), VT420 and up
4813 * NOTE: xterm doesn't enable this code by default.
4815 Terminal
.prototype.deleteColumns = function() {
4816 var param
= params
[0]
4817 , l
= this.ybase
+ this.rows
4818 , ch
= [this.eraseAttr(), ' ', 1] // xterm?
4822 for (i
= this.ybase
; i
< l
; i
++) {
4823 this.lines
.get(i
).splice(this.x
, 1);
4824 this.lines
.get(i
).push(ch
);
4835 Terminal
.charsets
= {};
4837 // DEC Special Character and Line Drawing Set.
4838 // http://vt100.net/docs/vt102-ug/table5-13.html
4839 // A lot of curses apps use this if they see TERM=xterm.
4840 // testing: echo -e '\e(0a\e(B'
4841 // The xterm output sometimes seems to conflict with the
4842 // reference above. xterm seems in line with the reference
4843 // when running vttest however.
4844 // The table below now uses xterm's output from vttest.
4845 Terminal
.charsets
.SCLD
= { // (0
4846 '`': '\u25c6', // '◆'
4847 'a': '\u2592', // '▒'
4848 'b': '\u0009', // '\t'
4849 'c': '\u000c', // '\f'
4850 'd': '\u000d', // '\r'
4851 'e': '\u000a', // '\n'
4852 'f': '\u00b0', // '°'
4853 'g': '\u00b1', // '±'
4854 'h': '\u2424', // '\u2424' (NL)
4855 'i': '\u000b', // '\v'
4856 'j': '\u2518', // '┘'
4857 'k': '\u2510', // '┐'
4858 'l': '\u250c', // '┌'
4859 'm': '\u2514', // '└'
4860 'n': '\u253c', // '┼'
4861 'o': '\u23ba', // '⎺'
4862 'p': '\u23bb', // '⎻'
4863 'q': '\u2500', // '─'
4864 'r': '\u23bc', // '⎼'
4865 's': '\u23bd', // '⎽'
4866 't': '\u251c', // '├'
4867 'u': '\u2524', // '┤'
4868 'v': '\u2534', // '┴'
4869 'w': '\u252c', // '┬'
4870 'x': '\u2502', // '│'
4871 'y': '\u2264', // '≤'
4872 'z': '\u2265', // '≥'
4873 '{': '\u03c0', // 'π'
4874 '|': '\u2260', // '≠'
4875 '}': '\u00a3', // '£'
4876 '~': '\u00b7' // '·'
4879 Terminal
.charsets
.UK
= null; // (A
4880 Terminal
.charsets
.US
= null; // (B (USASCII)
4881 Terminal
.charsets
.Dutch
= null; // (4
4882 Terminal
.charsets
.Finnish
= null; // (C or (5
4883 Terminal
.charsets
.French
= null; // (R
4884 Terminal
.charsets
.FrenchCanadian
= null; // (Q
4885 Terminal
.charsets
.German
= null; // (K
4886 Terminal
.charsets
.Italian
= null; // (Y
4887 Terminal
.charsets
.NorwegianDanish
= null; // (E or (6
4888 Terminal
.charsets
.Spanish
= null; // (Z
4889 Terminal
.charsets
.Swedish
= null; // (H or (7
4890 Terminal
.charsets
.Swiss
= null; // (=
4891 Terminal
.charsets
.ISOLatin
= null; // /A
4897 function on(el
, type
, handler
, capture
) {
4898 if (!Array
.isArray(el
)) {
4901 el
.forEach(function (element
) {
4902 element
.addEventListener(type
, handler
, capture
|| false);
4906 function off(el
, type
, handler
, capture
) {
4907 el
.removeEventListener(type
, handler
, capture
|| false);
4910 function cancel(ev
, force
) {
4911 if (!this.cancelEvents
&& !force
) {
4914 ev
.preventDefault();
4915 ev
.stopPropagation();
4919 function inherits(child
, parent
) {
4921 this.constructor = child
;
4923 f
.prototype = parent
.prototype;
4924 child
.prototype = new f
;
4927 // if bold is broken, we can't
4928 // use it in the terminal.
4929 function isBoldBroken(document
) {
4930 var body
= document
.getElementsByTagName('body')[0];
4931 var el
= document
.createElement('span');
4932 el
.innerHTML
= 'hello world';
4933 body
.appendChild(el
);
4934 var w1
= el
.scrollWidth
;
4935 el
.style
.fontWeight
= 'bold';
4936 var w2
= el
.scrollWidth
;
4937 body
.removeChild(el
);
4941 function indexOf(obj
, el
) {
4944 if (obj
[i
] === el
) return i
;
4949 function isThirdLevelShift(term
, ev
) {
4951 (term
.browser
.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) ||
4952 (term
.browser
.isMSWindows
&& ev
.altKey
&& ev
.ctrlKey
&& !ev
.metaKey
);
4954 if (ev
.type
== 'keypress') {
4955 return thirdLevelKey
;
4958 // Don't invoke for arrows, pageDown, home, backspace, etc. (on non-keypress events)
4959 return thirdLevelKey
&& (!ev
.keyCode
|| ev
.keyCode
> 47);
4962 function matchColor(r1
, g1
, b1
) {
4963 var hash
= (r1
<< 16) | (g1
<< 8) | b1
;
4965 if (matchColor
._cache
[hash
] != null) {
4966 return matchColor
._cache
[hash
];
4969 var ldiff
= Infinity
4978 for (; i
< Terminal
.vcolors
.length
; i
++) {
4979 c
= Terminal
.vcolors
[i
];
4984 diff
= matchColor
.distance(r1
, g1
, b1
, r2
, g2
, b2
);
4997 return matchColor
._cache
[hash
] = li
;
5000 matchColor
._cache
= {};
5002 // http://stackoverflow.com/questions/1633828
5003 matchColor
.distance = function(r1
, g1
, b1
, r2
, g2
, b2
) {
5004 return Math
.pow(30 * (r1
- r2
), 2)
5005 + Math
.pow(59 * (g1
- g2
), 2)
5006 + Math
.pow(11 * (b1
- b2
), 2);
5009 function each(obj
, iter
, con
) {
5010 if (obj
.forEach
) return obj
.forEach(iter
, con
);
5011 for (var i
= 0; i
< obj
.length
; i
++) {
5012 iter
.call(con
, obj
[i
], i
, obj
);
5016 function keys(obj
) {
5017 if (Object
.keys
) return Object
.keys(obj
);
5020 if (Object
.prototype.hasOwnProperty
.call(obj
, key
)) {
5027 var wcwidth
= (function(opts
) {
5028 // extracted from https://www.cl.cam.ac.uk/%7Emgk25/ucs/wcwidth.c
5029 // combining characters
5031 [0x0300, 0x036F], [0x0483, 0x0486], [0x0488, 0x0489],
5032 [0x0591, 0x05BD], [0x05BF, 0x05BF], [0x05C1, 0x05C2],
5033 [0x05C4, 0x05C5], [0x05C7, 0x05C7], [0x0600, 0x0603],
5034 [0x0610, 0x0615], [0x064B, 0x065E], [0x0670, 0x0670],
5035 [0x06D6, 0x06E4], [0x06E7, 0x06E8], [0x06EA, 0x06ED],
5036 [0x070F, 0x070F], [0x0711, 0x0711], [0x0730, 0x074A],
5037 [0x07A6, 0x07B0], [0x07EB, 0x07F3], [0x0901, 0x0902],
5038 [0x093C, 0x093C], [0x0941, 0x0948], [0x094D, 0x094D],
5039 [0x0951, 0x0954], [0x0962, 0x0963], [0x0981, 0x0981],
5040 [0x09BC, 0x09BC], [0x09C1, 0x09C4], [0x09CD, 0x09CD],
5041 [0x09E2, 0x09E3], [0x0A01, 0x0A02], [0x0A3C, 0x0A3C],
5042 [0x0A41, 0x0A42], [0x0A47, 0x0A48], [0x0A4B, 0x0A4D],
5043 [0x0A70, 0x0A71], [0x0A81, 0x0A82], [0x0ABC, 0x0ABC],
5044 [0x0AC1, 0x0AC5], [0x0AC7, 0x0AC8], [0x0ACD, 0x0ACD],
5045 [0x0AE2, 0x0AE3], [0x0B01, 0x0B01], [0x0B3C, 0x0B3C],
5046 [0x0B3F, 0x0B3F], [0x0B41, 0x0B43], [0x0B4D, 0x0B4D],
5047 [0x0B56, 0x0B56], [0x0B82, 0x0B82], [0x0BC0, 0x0BC0],
5048 [0x0BCD, 0x0BCD], [0x0C3E, 0x0C40], [0x0C46, 0x0C48],
5049 [0x0C4A, 0x0C4D], [0x0C55, 0x0C56], [0x0CBC, 0x0CBC],
5050 [0x0CBF, 0x0CBF], [0x0CC6, 0x0CC6], [0x0CCC, 0x0CCD],
5051 [0x0CE2, 0x0CE3], [0x0D41, 0x0D43], [0x0D4D, 0x0D4D],
5052 [0x0DCA, 0x0DCA], [0x0DD2, 0x0DD4], [0x0DD6, 0x0DD6],
5053 [0x0E31, 0x0E31], [0x0E34, 0x0E3A], [0x0E47, 0x0E4E],
5054 [0x0EB1, 0x0EB1], [0x0EB4, 0x0EB9], [0x0EBB, 0x0EBC],
5055 [0x0EC8, 0x0ECD], [0x0F18, 0x0F19], [0x0F35, 0x0F35],
5056 [0x0F37, 0x0F37], [0x0F39, 0x0F39], [0x0F71, 0x0F7E],
5057 [0x0F80, 0x0F84], [0x0F86, 0x0F87], [0x0F90, 0x0F97],
5058 [0x0F99, 0x0FBC], [0x0FC6, 0x0FC6], [0x102D, 0x1030],
5059 [0x1032, 0x1032], [0x1036, 0x1037], [0x1039, 0x1039],
5060 [0x1058, 0x1059], [0x1160, 0x11FF], [0x135F, 0x135F],
5061 [0x1712, 0x1714], [0x1732, 0x1734], [0x1752, 0x1753],
5062 [0x1772, 0x1773], [0x17B4, 0x17B5], [0x17B7, 0x17BD],
5063 [0x17C6, 0x17C6], [0x17C9, 0x17D3], [0x17DD, 0x17DD],
5064 [0x180B, 0x180D], [0x18A9, 0x18A9], [0x1920, 0x1922],
5065 [0x1927, 0x1928], [0x1932, 0x1932], [0x1939, 0x193B],
5066 [0x1A17, 0x1A18], [0x1B00, 0x1B03], [0x1B34, 0x1B34],
5067 [0x1B36, 0x1B3A], [0x1B3C, 0x1B3C], [0x1B42, 0x1B42],
5068 [0x1B6B, 0x1B73], [0x1DC0, 0x1DCA], [0x1DFE, 0x1DFF],
5069 [0x200B, 0x200F], [0x202A, 0x202E], [0x2060, 0x2063],
5070 [0x206A, 0x206F], [0x20D0, 0x20EF], [0x302A, 0x302F],
5071 [0x3099, 0x309A], [0xA806, 0xA806], [0xA80B, 0xA80B],
5072 [0xA825, 0xA826], [0xFB1E, 0xFB1E], [0xFE00, 0xFE0F],
5073 [0xFE20, 0xFE23], [0xFEFF, 0xFEFF], [0xFFF9, 0xFFFB],
5074 [0x10A01, 0x10A03], [0x10A05, 0x10A06], [0x10A0C, 0x10A0F],
5075 [0x10A38, 0x10A3A], [0x10A3F, 0x10A3F], [0x1D167, 0x1D169],
5076 [0x1D173, 0x1D182], [0x1D185, 0x1D18B], [0x1D1AA, 0x1D1AD],
5077 [0x1D242, 0x1D244], [0xE0001, 0xE0001], [0xE0020, 0xE007F],
5081 function bisearch(ucs
) {
5083 var max
= COMBINING
.length
- 1;
5085 if (ucs
< COMBINING
[0][0] || ucs
> COMBINING
[max
][1])
5087 while (max
>= min
) {
5088 mid
= Math
.floor((min
+ max
) / 2);
5089 if (ucs
> COMBINING
[mid
][1])
5091 else if (ucs
< COMBINING
[mid
][0])
5098 function wcwidth(ucs
) {
5099 // test for 8-bit control characters
5102 if (ucs
< 32 || (ucs
>= 0x7f && ucs
< 0xa0))
5103 return opts
.control
;
5104 // binary search in table of non-spacing characters
5107 // if we arrive here, ucs is not a combining or C0/C1 control character
5112 ucs
<= 0x115f || // Hangul Jamo init. consonants
5115 (ucs
>= 0x2e80 && ucs
<= 0xa4cf && ucs
!= 0x303f) || // CJK..Yi
5116 (ucs
>= 0xac00 && ucs
<= 0xd7a3) || // Hangul Syllables
5117 (ucs
>= 0xf900 && ucs
<= 0xfaff) || // CJK Compat Ideographs
5118 (ucs
>= 0xfe10 && ucs
<= 0xfe19) || // Vertical forms
5119 (ucs
>= 0xfe30 && ucs
<= 0xfe6f) || // CJK Compat Forms
5120 (ucs
>= 0xff00 && ucs
<= 0xff60) || // Fullwidth Forms
5121 (ucs
>= 0xffe0 && ucs
<= 0xffe6) ||
5122 (ucs
>= 0x20000 && ucs
<= 0x2fffd) ||
5123 (ucs
>= 0x30000 && ucs
<= 0x3fffd)
5128 })({nul
: 0, control
: 0}); // configurable options
5134 Terminal
.EventEmitter
= EventEmitter
;
5135 Terminal
.inherits
= inherits
;
5138 * Adds an event listener to the terminal.
5140 * @param {string} event The name of the event. TODO: Document all event types
5141 * @param {function} callback The function to call when the event is triggered.
5145 Terminal
.cancel
= cancel
;
5147 module
.exports
= Terminal
;