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 { CharMeasure
} from './utils/CharMeasure.js';
19 import * as Browser
from './utils/Browser';
20 import * as Keyboard
from './utils/Keyboard';
23 * Terminal Emulation References:
25 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.txt
26 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
27 * http://invisible-island.net/vttest/
28 * http://www.inwap.com/pdp10/ansicode.txt
29 * http://linux.die.net/man/4/console_codes
30 * http://linux.die.net/man/7/urxvt
33 // Let it work inside Node.js for automated testing purposes.
34 var document
= (typeof window
!= 'undefined') ? window
.document
: null;
39 var normal
= 0, escaped
= 1, csi
= 2, osc
= 3, charset
= 4, dcs
= 5, ignore
= 6;
46 * Creates a new `Terminal` object.
48 * @param {object} options An object containing a set of options, the available options are:
49 * - `cursorBlink` (boolean): Whether the terminal cursor blinks
50 * - `cols` (number): The number of columns of the terminal (horizontal size)
51 * - `rows` (number): The number of rows of the terminal (vertical size)
55 * @alias module:xterm/src/xterm
57 function Terminal(options
) {
60 if (!(this instanceof Terminal
)) {
61 return new Terminal(arguments
[0], arguments
[1], arguments
[2]);
64 self
.browser
= Browser
;
65 self
.cancel
= Terminal
.cancel
;
67 EventEmitter
.call(this);
69 if (typeof options
=== 'number') {
77 options
= options
|| {};
80 Object
.keys(Terminal
.defaults
).forEach(function(key
) {
81 if (options
[key
] == null) {
82 options
[key
] = Terminal
.options
[key
];
84 if (Terminal
[key
] !== Terminal
.defaults
[key
]) {
85 options
[key
] = Terminal
[key
];
88 self
[key
] = options
[key
];
91 if (options
.colors
.length
=== 8) {
92 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(8));
93 } else if (options
.colors
.length
=== 16) {
94 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(16));
95 } else if (options
.colors
.length
=== 10) {
96 options
.colors
= options
.colors
.slice(0, -2).concat(
97 Terminal
._colors
.slice(8, -2), options
.colors
.slice(-2));
98 } else if (options
.colors
.length
=== 18) {
99 options
.colors
= options
.colors
.concat(
100 Terminal
._colors
.slice(16, -2), options
.colors
.slice(-2));
102 this.colors
= options
.colors
;
104 this.options
= options
;
106 // this.context = options.context || window;
107 // this.document = options.document || document;
108 this.parent
= options
.body
|| options
.parent
|| (
109 document
? document
.getElementsByTagName('body')[0] : null
112 this.cols
= options
.cols
|| options
.geometry
[0];
113 this.rows
= options
.rows
|| options
.geometry
[1];
114 this.geometry
= [this.cols
, this.rows
];
116 if (options
.handler
) {
117 this.on('data', options
.handler
);
121 * The scroll position of the y cursor, ie. ybase + y = the y position within the entire
127 * The scroll position of the viewport
132 * The cursor's x position after ybase
137 * The cursor's y position after ybase
141 /** A queue of the rows to be refreshed */
142 this.refreshRowsQueue
= [];
144 this.cursorState
= 0;
145 this.cursorHidden
= false;
150 this.scrollBottom
= this.rows
- 1;
151 this.customKeydownHandler
= null;
154 this.applicationKeypad
= false;
155 this.applicationCursor
= false;
156 this.originMode
= false;
157 this.insertMode
= false;
158 this.wraparoundMode
= true; // defaults: xterm - true, vt100 - false
163 this.gcharset
= null;
165 this.charsets
= [null];
189 this.readable
= true;
190 this.writable
= true;
192 this.defAttr
= (0 << 18) | (257 << 9) | (256 << 0);
193 this.curAttr
= this.defAttr
;
196 this.currentParam
= 0;
200 // leftover surrogate high from previous write invocation
201 this.surrogate_high
= '';
204 * An array of all lines in the entire buffer, including the prompt. The lines are array of
205 * characters which are 2-length arrays where [0] is an attribute and [1] is the character.
207 this.lines
= new CircularList(this.scrollback
);
210 this.lines
.push(this.blankLine());
216 // Store if user went browsing history in scrollback
217 this.userScrolling
= false;
220 inherits(Terminal
, EventEmitter
);
223 * back_color_erase feature for xterm.
225 Terminal
.prototype.eraseAttr = function() {
226 // if (this.is('screen')) return this.defAttr;
227 return (this.defAttr
& ~0x1ff) | (this.curAttr
& 0x1ff);
235 Terminal
.tangoColors
= [
256 // Colors 0-15 + 16-255
257 // Much thanks to TooTallNate for writing this.
258 Terminal
.colors
= (function() {
259 var colors
= Terminal
.tangoColors
.slice()
260 , r
= [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff]
265 for (; i
< 216; i
++) {
266 out(r
[(i
/ 36) % 6 | 0], r
[(i
/ 6) % 6 | 0], r
[i
% 6]);
271 for (; i
< 24; i
++) {
276 function out(r
, g
, b
) {
277 colors
.push('#' + hex(r
) + hex(g
) + hex(b
));
282 return c
.length
< 2 ? '0' + c
: c
;
288 Terminal
._colors
= Terminal
.colors
.slice();
290 Terminal
.vcolors
= (function() {
292 , colors
= Terminal
.colors
296 for (; i
< 256; i
++) {
297 color
= parseInt(colors
[i
].substring(1), 16);
299 (color
>> 16) & 0xff,
312 Terminal
.defaults
= {
313 colors
: Terminal
.colors
,
326 // programFeatures: false,
330 Terminal
.options
= {};
332 Terminal
.focus
= null;
334 each(keys(Terminal
.defaults
), function(key
) {
335 Terminal
[key
] = Terminal
.defaults
[key
];
336 Terminal
.options
[key
] = Terminal
.defaults
[key
];
340 * Focus the terminal. Delegates focus handling to the terminal's DOM element.
342 Terminal
.prototype.focus = function() {
343 return this.textarea
.focus();
347 * Retrieves an option's value from the terminal.
348 * @param {string} key The option key.
350 Terminal
.prototype.getOption = function(key
, value
) {
351 if (!(key
in Terminal
.defaults
)) {
352 throw new Error('No option with key "' + key
+ '"');
355 if (typeof this.options
[key
] !== 'undefined') {
356 return this.options
[key
];
363 * Sets an option on the terminal.
364 * @param {string} key The option key.
365 * @param {string} value The option value.
367 Terminal
.prototype.setOption = function(key
, value
) {
368 if (!(key
in Terminal
.defaults
)) {
369 throw new Error('No option with key "' + key
+ '"');
372 this.options
[key
] = value
;
376 * Binds the desired focus behavior on a given terminal object.
380 Terminal
.bindFocus = function (term
) {
381 on(term
.textarea
, 'focus', function (ev
) {
382 if (term
.sendFocus
) {
385 term
.element
.classList
.add('focus');
387 Terminal
.focus
= term
;
388 term
.emit('focus', {terminal
: term
});
393 * Blur the terminal. Delegates blur handling to the terminal's DOM element.
395 Terminal
.prototype.blur = function() {
396 return this.textarea
.blur();
400 * Binds the desired blur behavior on a given terminal object.
404 Terminal
.bindBlur = function (term
) {
405 on(term
.textarea
, 'blur', function (ev
) {
406 term
.queueRefresh(term
.y
, term
.y
);
407 if (term
.sendFocus
) {
410 term
.element
.classList
.remove('focus');
411 Terminal
.focus
= null;
412 term
.emit('blur', {terminal
: term
});
417 * Initialize default behavior
419 Terminal
.prototype.initGlobal = function() {
422 Terminal
.bindKeys(this);
423 Terminal
.bindFocus(this);
424 Terminal
.bindBlur(this);
426 // Bind clipboard functionality
427 on(this.element
, 'copy', function (ev
) {
428 copyHandler
.call(this, ev
, term
);
430 on(this.textarea
, 'paste', function (ev
) {
431 pasteHandler
.call(this, ev
, term
);
433 on(this.element
, 'paste', function (ev
) {
434 pasteHandler
.call(this, ev
, term
);
437 function rightClickHandlerWrapper (ev
) {
438 rightClickHandler
.call(this, ev
, term
);
441 if (term
.browser
.isFirefox
) {
442 on(this.element
, 'mousedown', function (ev
) {
443 if (ev
.button
== 2) {
444 rightClickHandlerWrapper(ev
);
448 on(this.element
, 'contextmenu', rightClickHandlerWrapper
);
453 * Apply key handling to the terminal
455 Terminal
.bindKeys = function(term
) {
456 on(term
.element
, 'keydown', function(ev
) {
457 if (document
.activeElement
!= this) {
463 on(term
.element
, 'keypress', function(ev
) {
464 if (document
.activeElement
!= this) {
470 on(term
.element
, 'keyup', term
.focus
.bind(term
));
472 on(term
.textarea
, 'keydown', function(ev
) {
476 on(term
.textarea
, 'keypress', function(ev
) {
478 // Truncate the textarea's value, since it is not needed
482 on(term
.textarea
, 'compositionstart', term
.compositionHelper
.compositionstart
.bind(term
.compositionHelper
));
483 on(term
.textarea
, 'compositionupdate', term
.compositionHelper
.compositionupdate
.bind(term
.compositionHelper
));
484 on(term
.textarea
, 'compositionend', term
.compositionHelper
.compositionend
.bind(term
.compositionHelper
));
485 term
.on('refresh', term
.compositionHelper
.updateCompositionElements
.bind(term
.compositionHelper
));
490 * Insert the given row to the terminal or produce a new one
491 * if no row argument is passed. Return the inserted row.
492 * @param {HTMLElement} row (optional) The row to append to the terminal.
494 Terminal
.prototype.insertRow = function (row
) {
495 if (typeof row
!= 'object') {
496 row
= document
.createElement('div');
499 this.rowContainer
.appendChild(row
);
500 this.children
.push(row
);
506 * Opens the terminal within an element.
508 * @param {HTMLElement} parent The element to create the terminal within.
510 Terminal
.prototype.open = function(parent
) {
511 var self
=this, i
=0, div
;
513 this.parent
= parent
|| this.parent
;
516 throw new Error('Terminal requires a parent element.');
519 // Grab global elements
520 this.context
= this.parent
.ownerDocument
.defaultView
;
521 this.document
= this.parent
.ownerDocument
;
522 this.body
= this.document
.getElementsByTagName('body')[0];
524 //Create main element container
525 this.element
= this.document
.createElement('div');
526 this.element
.classList
.add('terminal');
527 this.element
.classList
.add('xterm');
528 this.element
.classList
.add('xterm-theme-' + this.theme
);
530 this.element
.style
.height
531 this.element
.setAttribute('tabindex', 0);
533 this.viewportElement
= document
.createElement('div');
534 this.viewportElement
.classList
.add('xterm-viewport');
535 this.element
.appendChild(this.viewportElement
);
536 this.viewportScrollArea
= document
.createElement('div');
537 this.viewportScrollArea
.classList
.add('xterm-scroll-area');
538 this.viewportElement
.appendChild(this.viewportScrollArea
);
540 // Create the container that will hold the lines of the terminal and then
541 // produce the lines the lines.
542 this.rowContainer
= document
.createElement('div');
543 this.rowContainer
.classList
.add('xterm-rows');
544 this.element
.appendChild(this.rowContainer
);
547 // Create the container that will hold helpers like the textarea for
548 // capturing DOM Events. Then produce the helpers.
549 this.helperContainer
= document
.createElement('div');
550 this.helperContainer
.classList
.add('xterm-helpers');
551 // TODO: This should probably be inserted once it's filled to prevent an additional layout
552 this.element
.appendChild(this.helperContainer
);
553 this.textarea
= document
.createElement('textarea');
554 this.textarea
.classList
.add('xterm-helper-textarea');
555 this.textarea
.setAttribute('autocorrect', 'off');
556 this.textarea
.setAttribute('autocapitalize', 'off');
557 this.textarea
.setAttribute('spellcheck', 'false');
558 this.textarea
.tabIndex
= 0;
559 this.textarea
.addEventListener('focus', function() {
560 self
.emit('focus', {terminal
: self
});
562 this.textarea
.addEventListener('blur', function() {
563 self
.emit('blur', {terminal
: self
});
565 this.helperContainer
.appendChild(this.textarea
);
567 this.compositionView
= document
.createElement('div');
568 this.compositionView
.classList
.add('composition-view');
569 this.compositionHelper
= new CompositionHelper(this.textarea
, this.compositionView
, this);
570 this.helperContainer
.appendChild(this.compositionView
);
572 this.charSizeStyleElement
= document
.createElement('style');
573 this.helperContainer
.appendChild(this.charSizeStyleElement
);
575 for (; i
< this.rows
; i
++) {
578 this.parent
.appendChild(this.element
);
580 this.charMeasure
= new CharMeasure(this.rowContainer
);
581 this.charMeasure
.on('charsizechanged', function () {
582 self
.updateCharSizeCSS();
584 this.charMeasure
.measure();
586 this.viewport
= new Viewport(this, this.viewportElement
, this.viewportScrollArea
, this.charMeasure
);
588 // Setup loop that draws to screen
589 this.queueRefresh(0, this.rows
- 1);
592 // Initialize global actions that
593 // need to be taken on the document.
596 // Ensure there is a Terminal.focus.
599 on(this.element
, 'click', function() {
600 var selection
= document
.getSelection(),
601 collapsed
= selection
.isCollapsed
,
602 isRange
= typeof collapsed
== 'boolean' ? !collapsed
: selection
.type
== 'Range';
608 // Listen for mouse events and translate
609 // them into terminal mouse protocols.
612 // Figure out whether boldness affects
613 // the character width of monospace fonts.
614 if (Terminal
.brokenBold
== null) {
615 Terminal
.brokenBold
= isBoldBroken(this.document
);
619 * This event is emitted when terminal has completed opening.
628 * Attempts to load an add-on using CommonJS or RequireJS (whichever is available).
629 * @param {string} addon The name of the addon to load
632 Terminal
.loadAddon = function(addon
, callback
) {
633 if (typeof exports
=== 'object' && typeof module
=== 'object') {
635 return require('./addons/' + addon
+ '/' + addon
);
636 } else if (typeof define
== 'function') {
638 return require(['./addons/' + addon
+ '/' + addon
], callback
);
640 console
.error('Cannot load a module without a CommonJS or RequireJS environment.');
646 * Updates the helper CSS class with any changes necessary after the terminal's
647 * character width has been changed.
649 Terminal
.prototype.updateCharSizeCSS = function() {
650 this.charSizeStyleElement
.textContent
= '.xterm-wide-char{width:' + (this.charMeasure
.width
* 2) + 'px;}';
655 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#Mouse%20Tracking
656 * To better understand these
657 * the xterm code is very helpful:
659 * button.c, charproc.c, misc.c
660 * Relevant functions in xterm/button.c:
661 * BtnCode, EmitButtonCode, EditorButton, SendMousePosition
663 Terminal
.prototype.bindMouse = function() {
664 var el
= this.element
, self
= this, pressed
= 32;
666 // mouseup, mousedown, wheel
667 // left click: ^[[M 3<^[[M#3<
669 function sendButton(ev
) {
673 // get the xterm-style button
674 button
= getButton(ev
);
676 // get mouse coordinates
680 sendEvent(button
, pos
);
682 switch (ev
.overrideType
|| ev
.type
) {
687 // keep it at the left
688 // button, just in case.
699 // motion example of a left click:
700 // ^[[M 3<^[[M@4<^[[M@5<^[[M@6<^[[M@7<^[[M#7<
701 function sendMove(ev
) {
708 // buttons marked as motions
709 // are incremented by 32
712 sendEvent(button
, pos
);
716 // position to characters
717 function encode(data
, ch
) {
718 if (!self
.utfMouse
) {
719 if (ch
=== 255) return data
.push(0);
720 if (ch
> 127) ch
= 127;
723 if (ch
=== 2047) return data
.push(0);
727 if (ch
> 2047) ch
= 2047;
728 data
.push(0xC0 | (ch
>> 6));
729 data
.push(0x80 | (ch
& 0x3F));
734 // send a mouse event:
735 // regular/utf8: ^[[M Cb Cx Cy
736 // urxvt: ^[[ Cb ; Cx ; Cy M
737 // sgr: ^[[ Cb ; Cx ; Cy M/m
738 // vt300: ^[[ 24(1/3/5)~ [ Cx , Cy ] \r
739 // locator: CSI P e ; P b ; P r ; P c ; P p & w
740 function sendEvent(button
, pos
) {
741 // self.emit('mouse', {
747 if (self
.vt300Mouse
) {
749 // http://www.vt100.net/docs/vt3xx-gp/chapter15.html
753 var data
= '\x1b[24';
754 if (button
=== 0) data
+= '1';
755 else if (button
=== 1) data
+= '3';
756 else if (button
=== 2) data
+= '5';
757 else if (button
=== 3) return;
759 data
+= '~[' + pos
.x
+ ',' + pos
.y
+ ']\r';
764 if (self
.decLocator
) {
769 if (button
=== 0) button
= 2;
770 else if (button
=== 1) button
= 4;
771 else if (button
=== 2) button
= 6;
772 else if (button
=== 3) button
= 3;
776 + (button
=== 3 ? 4 : 0)
787 if (self
.urxvtMouse
) {
792 self
.send('\x1b[' + button
+ ';' + pos
.x
+ ';' + pos
.y
+ 'M');
800 + (((button
& 3) === 3 ? button
& ~3 : button
) - 32)
805 + ((button
& 3) === 3 ? 'm' : 'M'));
811 encode(data
, button
);
815 self
.send('\x1b[M' + String
.fromCharCode
.apply(String
, data
));
818 function getButton(ev
) {
831 // 1, and 2 - with 64 added
832 switch (ev
.overrideType
|| ev
.type
) {
834 button
= ev
.button
!= null
840 if (self
.browser
.isMSIE
) {
841 button
= button
=== 1 ? 0 : button
=== 4 ? 1 : button
;
847 case 'DOMMouseScroll':
848 button
= ev
.detail
< 0
853 button
= ev
.wheelDeltaY
> 0
859 // next three bits are the modifiers:
860 // 4 = shift, 8 = meta, 16 = control
861 shift
= ev
.shiftKey
? 4 : 0;
862 meta
= ev
.metaKey
? 8 : 0;
863 ctrl
= ev
.ctrlKey
? 16 : 0;
864 mod
= shift
| meta
| ctrl
;
867 if (self
.vt200Mouse
) {
870 } else if (!self
.normalMouse
) {
875 button
= (32 + (mod
<< 2)) + button
;
880 // mouse coordinates measured in cols/rows
881 function getCoords(ev
) {
884 // ignore browsers without pageX for now
885 if (ev
.pageX
== null) return;
891 // should probably check offsetParent
892 // but this is more portable
893 while (el
&& el
!== self
.document
.documentElement
) {
896 el
= 'offsetParent' in el
901 // convert to cols/rows
902 w
= self
.element
.clientWidth
;
903 h
= self
.element
.clientHeight
;
904 x
= Math
.ceil((x
/ w
) * self
.cols
);
905 y
= Math
.ceil((y
/ h
) * self
.rows
);
907 // be sure to avoid sending
908 // bad positions to the program
910 if (x
> self
.cols
) x
= self
.cols
;
912 if (y
> self
.rows
) y
= self
.rows
;
914 // xterm sends raw bytes and
915 // starts at 32 (SP) for each.
926 on(el
, 'mousedown', function(ev
) {
927 if (!self
.mouseEvents
) return;
936 //if (self.vt200Mouse && !self.normalMouse) {
937 if (self
.vt200Mouse
) {
938 ev
.overrideType
= 'mouseup';
940 return self
.cancel(ev
);
944 if (self
.normalMouse
) on(self
.document
, 'mousemove', sendMove
);
946 // x10 compatibility mode can't send button releases
947 if (!self
.x10Mouse
) {
948 on(self
.document
, 'mouseup', function up(ev
) {
950 if (self
.normalMouse
) off(self
.document
, 'mousemove', sendMove
);
951 off(self
.document
, 'mouseup', up
);
952 return self
.cancel(ev
);
956 return self
.cancel(ev
);
959 //if (self.normalMouse) {
960 // on(self.document, 'mousemove', sendMove);
963 on(el
, 'wheel', function(ev
) {
964 if (!self
.mouseEvents
) return;
967 || self
.decLocator
) return;
969 return self
.cancel(ev
);
972 // allow wheel scrolling in
973 // the shell for example
974 on(el
, 'wheel', function(ev
) {
975 if (self
.mouseEvents
) return;
976 self
.viewport
.onWheel(ev
);
977 return self
.cancel(ev
);
982 * Destroys the terminal.
984 Terminal
.prototype.destroy = function() {
985 this.readable
= false;
986 this.writable
= false;
988 this.handler = function() {};
989 this.write = function() {};
990 if (this.element
.parentNode
) {
991 this.element
.parentNode
.removeChild(this.element
);
993 //this.emit('close');
998 * Flags used to render terminal text properly
1009 * Queues a refresh between two rows (inclusive), to be done on next animation
1011 * @param {number} start The start row.
1012 * @param {number} end The end row.
1014 Terminal
.prototype.queueRefresh = function(start
, end
) {
1015 this.refreshRowsQueue
.push({ start
: start
, end
: end
});
1019 * Performs the refresh loop callback, calling refresh only if a refresh is
1020 * necessary before queueing up the next one.
1022 Terminal
.prototype.refreshLoop = function() {
1023 // Don't refresh if there were no row changes
1024 if (this.refreshRowsQueue
.length
> 0) {
1027 if (this.refreshRowsQueue
.length
> 4) {
1028 // Just do a full refresh when 5+ refreshes are queued
1030 end
= this.rows
- 1;
1032 // Get start and end rows that need refreshing
1033 start
= this.refreshRowsQueue
[0].start
;
1034 end
= this.refreshRowsQueue
[0].end
;
1035 for (var i
= 1; i
< this.refreshRowsQueue
.length
; i
++) {
1036 if (this.refreshRowsQueue
[i
].start
< start
) {
1037 start
= this.refreshRowsQueue
[i
].start
;
1039 if (this.refreshRowsQueue
[i
].end
> end
) {
1040 end
= this.refreshRowsQueue
[i
].end
;
1044 this.refreshRowsQueue
= [];
1045 this.refresh(start
, end
);
1047 window
.requestAnimationFrame(this.refreshLoop
.bind(this));
1051 * Refreshes (re-renders) terminal content within two rows (inclusive)
1055 * In the screen buffer, each character is stored as a an array with a character
1056 * and a 32-bit integer:
1057 * - First value: a utf-16 character.
1059 * - Next 9 bits: background color (0-511).
1060 * - Next 9 bits: foreground color (0-511).
1061 * - Next 14 bits: a mask for misc. flags:
1068 * @param {number} start The row to start from (between 0 and terminal's height terminal - 1)
1069 * @param {number} end The row to end at (between fromRow and terminal's height terminal - 1)
1071 Terminal
.prototype.refresh = function(start
, end
) {
1074 var x
, y
, i
, line
, out
, ch
, ch_width
, width
, data
, attr
, bg
, fg
, flags
, row
, parent
, focused
= document
.activeElement
;
1076 // If this is a big refresh, remove the terminal rows from the DOM for faster calculations
1077 if (end
- start
>= this.rows
/ 2) {
1078 parent
= this.element
.parentNode
;
1080 this.element
.removeChild(this.rowContainer
);
1087 if (end
>= this.rows
.length
) {
1088 this.log('`end` is too large. Most likely a bad CSR.');
1089 end
= this.rows
.length
- 1;
1092 for (; y
<= end
; y
++) {
1093 row
= y
+ this.ydisp
;
1095 line
= this.lines
.get(row
);
1098 if (this.y
=== y
- (this.ybase
- this.ydisp
)
1100 && !this.cursorHidden
) {
1106 attr
= this.defAttr
;
1109 for (; i
< width
; i
++) {
1112 ch_width
= line
[i
][2];
1116 if (i
=== x
) data
= -1;
1118 if (data
!== attr
) {
1119 if (attr
!== this.defAttr
) {
1122 if (data
!== this.defAttr
) {
1124 out
+= '<span class="reverse-video terminal-cursor';
1125 if (this.cursorBlink
) {
1130 var classNames
= [];
1133 fg
= (data
>> 9) & 0x1ff;
1136 if (flags
& Terminal
.flags
.BOLD
) {
1137 if (!Terminal
.brokenBold
) {
1138 classNames
.push('xterm-bold');
1140 // See: XTerm*boldColors
1141 if (fg
< 8) fg
+= 8;
1144 if (flags
& Terminal
.flags
.UNDERLINE
) {
1145 classNames
.push('xterm-underline');
1148 if (flags
& Terminal
.flags
.BLINK
) {
1149 classNames
.push('xterm-blink');
1152 // If inverse flag is on, then swap the foreground and background variables.
1153 if (flags
& Terminal
.flags
.INVERSE
) {
1154 /* One-line variable swap in JavaScript: http://stackoverflow.com/a/16201730 */
1155 bg = [fg, fg = bg][0];
1156 // Should inverse just be before the
1157 // above boldColors effect instead?
1158 if ((flags & 1) && fg < 8) fg += 8;
1161 if (flags & Terminal.flags.INVISIBLE) {
1162 classNames.push('xterm-hidden');
1166 * Weird situation: Invert flag used black foreground and white background results
1167 * in invalid background color, positioned at the 256 index of the 256 terminal
1168 * color map. Pin the colors manually in such a case.
1170 * Source: https://github.com/sourcelair/xterm.js/issues/57
1172 if (flags
& Terminal
.flags
.INVERSE
) {
1182 classNames
.push('xterm-bg-color-' + bg
);
1186 classNames
.push('xterm-color-' + fg
);
1190 if (classNames
.length
) {
1191 out
+= ' class="' + classNames
.join(' ') + '"';
1198 if (ch_width
=== 2) {
1199 out
+= '<span class="xterm-wide-char">';
1219 if (ch_width
=== 2) {
1226 if (attr
!== this.defAttr
) {
1230 this.children
[y
].innerHTML
= out
;
1234 this.element
.appendChild(this.rowContainer
);
1237 this.emit('refresh', {element
: this.element
, start
: start
, end
: end
});
1241 * Display the cursor element
1243 Terminal
.prototype.showCursor = function() {
1244 if (!this.cursorState
) {
1245 this.cursorState
= 1;
1246 this.queueRefresh(this.y
, this.y
);
1251 * Scroll the terminal down 1 row, creating a blank line.
1253 Terminal
.prototype.scroll = function() {
1256 // Make room for the new row in lines
1257 if (this.lines
.length
=== this.lines
.maxLength
) {
1258 this.lines
.trimStart(1);
1260 if (this.ydisp
!== 0) {
1267 // TODO: Why is this done twice?
1268 if (!this.userScrolling
) {
1269 this.ydisp
= this.ybase
;
1273 row
= this.ybase
+ this.rows
- 1;
1275 // subtract the bottom scroll region
1276 row
-= this.rows
- 1 - this.scrollBottom
;
1278 if (row
=== this.lines
.length
) {
1279 // Optimization: pushing is faster than splicing when they amount to the same behavior
1280 this.lines
.push(this.blankLine());
1283 this.lines
.splice(row
, 0, this.blankLine());
1286 if (this.scrollTop
!== 0) {
1287 if (this.ybase
!== 0) {
1289 if (!this.userScrolling
) {
1290 this.ydisp
= this.ybase
;
1293 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
1297 this.updateRange(this.scrollTop
);
1298 this.updateRange(this.scrollBottom
);
1301 * This event is emitted whenever the terminal is scrolled.
1302 * The one parameter passed is the new y display position.
1306 this.emit('scroll', this.ydisp
);
1310 * Scroll the display of the terminal
1311 * @param {number} disp The number of lines to scroll down (negatives scroll up).
1312 * @param {boolean} suppressScrollEvent Don't emit the scroll event as scrollDisp. This is used
1313 * to avoid unwanted events being handled by the veiwport when the event was triggered from the
1314 * viewport originally.
1316 Terminal
.prototype.scrollDisp = function(disp
, suppressScrollEvent
) {
1318 this.userScrolling
= true;
1319 } else if (disp
+ this.ydisp
>= this.ybase
) {
1320 this.userScrolling
= false;
1325 if (this.ydisp
> this.ybase
) {
1326 this.ydisp
= this.ybase
;
1327 } else if (this.ydisp
< 0) {
1331 if (!suppressScrollEvent
) {
1332 this.emit('scroll', this.ydisp
);
1335 this.queueRefresh(0, this.rows
- 1);
1339 * Scroll the display of the terminal by a number of pages.
1340 * @param {number} pageCount The number of pages to scroll (negative scrolls up).
1342 Terminal
.prototype.scrollPages = function(pageCount
) {
1343 this.scrollDisp(pageCount
* (this.rows
- 1));
1347 * Scrolls the display of the terminal to the top.
1349 Terminal
.prototype.scrollToTop = function() {
1350 this.scrollDisp(-this.ydisp
);
1354 * Scrolls the display of the terminal to the bottom.
1356 Terminal
.prototype.scrollToBottom = function() {
1357 this.scrollDisp(this.ybase
- this.ydisp
);
1361 * Writes text to the terminal.
1362 * @param {string} text The text to write to the terminal.
1364 Terminal
.prototype.write = function(data
) {
1365 var l
= data
.length
, i
= 0, j
, cs
, ch
, code
, low
, ch_width
, row
;
1367 this.refreshStart
= this.y
;
1368 this.refreshEnd
= this.y
;
1370 // apply leftover surrogate high from last write
1371 if (this.surrogate_high
) {
1372 data
= this.surrogate_high
+ data
;
1373 this.surrogate_high
= '';
1376 for (; i
< l
; i
++) {
1379 // FIXME: higher chars than 0xa0 are not allowed in escape sequences
1380 // --> maybe move to default
1381 code
= data
.charCodeAt(i
);
1382 if (0xD800 <= code
&& code
<= 0xDBFF) {
1383 // we got a surrogate high
1384 // get surrogate low (next 2 bytes)
1385 low
= data
.charCodeAt(i
+1);
1387 // end of data stream, save surrogate high
1388 this.surrogate_high
= ch
;
1391 code
= ((code
- 0xD800) * 0x400) + (low
- 0xDC00) + 0x10000;
1392 ch
+= data
.charAt(i
+1);
1394 // surrogate low - already handled above
1395 if (0xDC00 <= code
&& code
<= 0xDFFF)
1397 switch (this.state
) {
1408 if (this.convertEol
) {
1412 if (this.y
> this.scrollBottom
) {
1432 this.x
= this.nextStop();
1447 this.state
= escaped
;
1452 // calculate print space
1453 // expensive call, therefore we save width in line buffer
1454 ch_width
= wcwidth(code
);
1457 if (this.charset
&& this.charset
[ch
]) {
1458 ch
= this.charset
[ch
];
1461 row
= this.y
+ this.ybase
;
1463 // insert combining char in last cell
1464 // FIXME: needs handling after cursor jumps
1465 if (!ch_width
&& this.x
) {
1466 // dont overflow left
1467 if (this.lines
.get(row
)[this.x
-1]) {
1468 if (!this.lines
.get(row
)[this.x
-1][2]) {
1470 // found empty cell after fullwidth, need to go 2 cells back
1471 if (this.lines
.get(row
)[this.x
-2])
1472 this.lines
.get(row
)[this.x
-2][1] += ch
;
1475 this.lines
.get(row
)[this.x
-1][1] += ch
;
1477 this.updateRange(this.y
);
1482 // goto next line if ch would overflow
1483 // TODO: needs a global min terminal width of 2
1484 if (this.x
+ch_width
-1 >= this.cols
) {
1485 // autowrap - DECAWM
1486 if (this.wraparoundMode
) {
1489 if (this.y
> this.scrollBottom
) {
1494 this.x
= this.cols
-1;
1495 if(ch_width
===2) // FIXME: check for xterm behavior
1499 row
= this.y
+ this.ybase
;
1501 // insert mode: move characters to right
1502 if (this.insertMode
) {
1503 // do this twice for a fullwidth char
1504 for (var moves
=0; moves
<ch_width
; ++moves
) {
1505 // remove last cell, if it's width is 0
1506 // we have to adjust the second last cell as well
1507 var removed
= this.lines
.get(this.y
+ this.ybase
).pop();
1509 && this.lines
.get(row
)[this.cols
-2]
1510 && this.lines
.get(row
)[this.cols
-2][2]===2)
1511 this.lines
.get(row
)[this.cols
-2] = [this.curAttr
, ' ', 1];
1513 // insert empty cell at cursor
1514 this.lines
.get(row
).splice(this.x
, 0, [this.curAttr
, ' ', 1]);
1518 this.lines
.get(row
)[this.x
] = [this.curAttr
, ch
, ch_width
];
1520 this.updateRange(this.y
);
1522 // fullwidth char - set next cell width to zero and advance cursor
1524 this.lines
.get(row
)[this.x
] = [this.curAttr
, '', 0];
1533 // ESC [ Control Sequence Introducer ( CSI is 0x9b).
1536 this.currentParam
= 0;
1540 // ESC ] Operating System Command ( OSC is 0x9d).
1543 this.currentParam
= 0;
1547 // ESC P Device Control String ( DCS is 0x90).
1550 this.currentParam
= 0;
1554 // ESC _ Application Program Command ( APC is 0x9f).
1556 this.state
= ignore
;
1559 // ESC ^ Privacy Message ( PM is 0x9e).
1561 this.state
= ignore
;
1564 // ESC c Full Reset (RIS).
1569 // ESC E Next Line ( NEL is 0x85).
1570 // ESC D Index ( IND is 0x84).
1578 // ESC M Reverse Index ( RI is 0x8d).
1580 this.reverseIndex();
1583 // ESC % Select default/utf-8 character set.
1584 // @ = default, G = utf-8
1586 //this.charset = null;
1588 this.setgCharset(0, Terminal
.charsets
.US
);
1589 this.state
= normal
;
1593 // ESC (,),*,+,-,. Designate G0-G2 Character Set.
1594 case '(': // <-- this seems to get all the attention
1620 this.state
= charset
;
1623 // Designate G3 Character Set (VT300).
1624 // A = ISO Latin-1 Supplemental.
1628 this.state
= charset
;
1633 // Single Shift Select of G2 Character Set
1634 // ( SS2 is 0x8e). This affects next character only.
1638 // Single Shift Select of G3 Character Set
1639 // ( SS3 is 0x8f). This affects next character only.
1643 // Invoke the G2 Character Set as GL (LS2).
1648 // Invoke the G3 Character Set as GL (LS3).
1653 // Invoke the G3 Character Set as GR (LS3R).
1658 // Invoke the G2 Character Set as GR (LS2R).
1663 // Invoke the G1 Character Set as GR (LS1R).
1668 // ESC 7 Save Cursor (DECSC).
1671 this.state
= normal
;
1674 // ESC 8 Restore Cursor (DECRC).
1676 this.restoreCursor();
1677 this.state
= normal
;
1680 // ESC # 3 DEC line height/width
1682 this.state
= normal
;
1686 // ESC H Tab Set (HTS is 0x88).
1691 // ESC = Application Keypad (DECKPAM).
1693 this.log('Serial port requested application keypad.');
1694 this.applicationKeypad
= true;
1695 this.viewport
.syncScrollArea();
1696 this.state
= normal
;
1699 // ESC > Normal Keypad (DECKPNM).
1701 this.log('Switching back to normal keypad.');
1702 this.applicationKeypad
= false;
1703 this.viewport
.syncScrollArea();
1704 this.state
= normal
;
1708 this.state
= normal
;
1709 this.error('Unknown ESC control: %s.', ch
);
1716 case '0': // DEC Special Character and Line Drawing Set.
1717 cs
= Terminal
.charsets
.SCLD
;
1720 cs
= Terminal
.charsets
.UK
;
1722 case 'B': // United States (USASCII).
1723 cs
= Terminal
.charsets
.US
;
1726 cs
= Terminal
.charsets
.Dutch
;
1728 case 'C': // Finnish
1730 cs
= Terminal
.charsets
.Finnish
;
1733 cs
= Terminal
.charsets
.French
;
1735 case 'Q': // FrenchCanadian
1736 cs
= Terminal
.charsets
.FrenchCanadian
;
1739 cs
= Terminal
.charsets
.German
;
1741 case 'Y': // Italian
1742 cs
= Terminal
.charsets
.Italian
;
1744 case 'E': // NorwegianDanish
1746 cs
= Terminal
.charsets
.NorwegianDanish
;
1748 case 'Z': // Spanish
1749 cs
= Terminal
.charsets
.Spanish
;
1751 case 'H': // Swedish
1753 cs
= Terminal
.charsets
.Swedish
;
1756 cs
= Terminal
.charsets
.Swiss
;
1758 case '/': // ISOLatin (actually /A)
1759 cs
= Terminal
.charsets
.ISOLatin
;
1763 cs
= Terminal
.charsets
.US
;
1766 this.setgCharset(this.gcharset
, cs
);
1767 this.gcharset
= null;
1768 this.state
= normal
;
1774 // Set Text Parameters.
1775 if (ch
=== '\x1b' || ch
=== '\x07') {
1776 if (ch
=== '\x1b') i
++;
1778 this.params
.push(this.currentParam
);
1780 switch (this.params
[0]) {
1784 if (this.params
[1]) {
1785 this.title
= this.params
[1];
1786 this.handleTitle(this.title
);
1794 // change dynamic colors
1806 // change dynamic ui colors
1818 // manipulate selection data
1836 this.currentParam
= 0;
1837 this.state
= normal
;
1839 if (!this.params
.length
) {
1840 if (ch
>= '0' && ch
<= '9') {
1842 this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1843 } else if (ch
=== ';') {
1844 this.params
.push(this.currentParam
);
1845 this.currentParam
= '';
1848 this.currentParam
+= ch
;
1855 if (ch
=== '?' || ch
=== '>' || ch
=== '!') {
1861 if (ch
>= '0' && ch
<= '9') {
1862 this.currentParam
= this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1866 // '$', '"', ' ', '\''
1867 if (ch
=== '$' || ch
=== '"' || ch
=== ' ' || ch
=== '\'') {
1872 this.params
.push(this.currentParam
);
1873 this.currentParam
= 0;
1876 if (ch
=== ';') break;
1878 this.state
= normal
;
1882 // Cursor Up Ps Times (default = 1) (CUU).
1884 this.cursorUp(this.params
);
1888 // Cursor Down Ps Times (default = 1) (CUD).
1890 this.cursorDown(this.params
);
1894 // Cursor Forward Ps Times (default = 1) (CUF).
1896 this.cursorForward(this.params
);
1900 // Cursor Backward Ps Times (default = 1) (CUB).
1902 this.cursorBackward(this.params
);
1906 // Cursor Position [row;column] (default = [1,1]) (CUP).
1908 this.cursorPos(this.params
);
1911 // CSI Ps J Erase in Display (ED).
1913 this.eraseInDisplay(this.params
);
1916 // CSI Ps K Erase in Line (EL).
1918 this.eraseInLine(this.params
);
1921 // CSI Pm m Character Attributes (SGR).
1924 this.charAttributes(this.params
);
1928 // CSI Ps n Device Status Report (DSR).
1931 this.deviceStatus(this.params
);
1940 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
1942 this.insertChars(this.params
);
1946 // Cursor Next Line Ps Times (default = 1) (CNL).
1948 this.cursorNextLine(this.params
);
1952 // Cursor Preceding Line Ps Times (default = 1) (CNL).
1954 this.cursorPrecedingLine(this.params
);
1958 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
1960 this.cursorCharAbsolute(this.params
);
1964 // Insert Ps Line(s) (default = 1) (IL).
1966 this.insertLines(this.params
);
1970 // Delete Ps Line(s) (default = 1) (DL).
1972 this.deleteLines(this.params
);
1976 // Delete Ps Character(s) (default = 1) (DCH).
1978 this.deleteChars(this.params
);
1982 // Erase Ps Character(s) (default = 1) (ECH).
1984 this.eraseChars(this.params
);
1987 // CSI Pm ` Character Position Absolute
1988 // [column] (default = [row,1]) (HPA).
1990 this.charPosAbsolute(this.params
);
1994 // Horizontal Position Relative
1996 this.HPositionRelative(this.params
);
2000 // Send Device Attributes (Primary DA).
2002 // Send Device Attributes (Secondary DA)
2004 this.sendDeviceAttributes(this.params
);
2008 // Line Position Absolute [row] (default = [1,column]) (VPA).
2010 this.linePosAbsolute(this.params
);
2013 // 145 65 e * VPR - Vertical Position Relative
2015 this.VPositionRelative(this.params
);
2019 // Horizontal and Vertical Position [row;column] (default =
2022 this.HVPosition(this.params
);
2025 // CSI Pm h Set Mode (SM).
2026 // CSI ? Pm h - mouse escape codes, cursor escape codes
2028 this.setMode(this.params
);
2031 // CSI Pm l Reset Mode (RM).
2034 this.resetMode(this.params
);
2038 // Set Scrolling Region [top;bottom] (default = full size of win-
2042 this.setScrollRegion(this.params
);
2046 // Save cursor (ANSI.SYS).
2048 this.saveCursor(this.params
);
2052 // Restore cursor (ANSI.SYS).
2054 this.restoreCursor(this.params
);
2062 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
2064 this.cursorForwardTab(this.params
);
2067 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
2069 this.scrollUp(this.params
);
2072 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
2073 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
2076 // if (this.prefix === '>') {
2077 // this.resetTitleModes(this.params);
2080 // if (this.params.length > 2) {
2081 // this.initMouseTracking(this.params);
2084 if (this.params
.length
< 2 && !this.prefix
) {
2085 this.scrollDown(this.params
);
2090 // Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
2092 this.cursorBackwardTab(this.params
);
2095 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
2097 this.repeatPrecedingCharacter(this.params
);
2100 // CSI Ps g Tab Clear (TBC).
2102 this.tabClear(this.params
);
2105 // CSI Pm i Media Copy (MC).
2108 // this.mediaCopy(this.params);
2111 // CSI Pm m Character Attributes (SGR).
2113 // case 'm': // duplicate
2114 // if (this.prefix === '>') {
2115 // this.setResources(this.params);
2117 // this.charAttributes(this.params);
2121 // CSI Ps n Device Status Report (DSR).
2123 // case 'n': // duplicate
2124 // if (this.prefix === '>') {
2125 // this.disableModifiers(this.params);
2127 // this.deviceStatus(this.params);
2131 // CSI > Ps p Set pointer mode.
2132 // CSI ! p Soft terminal reset (DECSTR).
2134 // Request ANSI mode (DECRQM).
2136 // Request DEC private mode (DECRQM).
2139 switch (this.prefix
) {
2141 // this.setPointerMode(this.params);
2144 this.softReset(this.params
);
2147 // if (this.postfix === '$') {
2148 // this.requestPrivateMode(this.params);
2152 // if (this.postfix === '"') {
2153 // this.setConformanceLevel(this.params);
2154 // } else if (this.postfix === '$') {
2155 // this.requestAnsiMode(this.params);
2161 // CSI Ps q Load LEDs (DECLL).
2165 // if (this.postfix === ' ') {
2166 // this.setCursorStyle(this.params);
2169 // if (this.postfix === '"') {
2170 // this.setCharProtectionAttr(this.params);
2173 // this.loadLEDs(this.params);
2177 // Set Scrolling Region [top;bottom] (default = full size of win-
2180 // CSI Pt; Pl; Pb; Pr; Ps$ r
2181 // case 'r': // duplicate
2182 // if (this.prefix === '?') {
2183 // this.restorePrivateValues(this.params);
2184 // } else if (this.postfix === '$') {
2185 // this.setAttrInRectangle(this.params);
2187 // this.setScrollRegion(this.params);
2191 // CSI s Save cursor (ANSI.SYS).
2193 // case 's': // duplicate
2194 // if (this.prefix === '?') {
2195 // this.savePrivateValues(this.params);
2197 // this.saveCursor(this.params);
2201 // CSI Ps ; Ps ; Ps t
2202 // CSI Pt; Pl; Pb; Pr; Ps$ t
2206 // if (this.postfix === '$') {
2207 // this.reverseAttrInRectangle(this.params);
2208 // } else if (this.postfix === ' ') {
2209 // this.setWarningBellVolume(this.params);
2211 // if (this.prefix === '>') {
2212 // this.setTitleModeFeature(this.params);
2214 // this.manipulateWindow(this.params);
2219 // CSI u Restore cursor (ANSI.SYS).
2221 // case 'u': // duplicate
2222 // if (this.postfix === ' ') {
2223 // this.setMarginBellVolume(this.params);
2225 // this.restoreCursor(this.params);
2229 // CSI Pt; Pl; Pb; Pr; Pp; Pt; Pl; Pp$ v
2231 // if (this.postfix === '$') {
2232 // this.copyRectagle(this.params);
2236 // CSI Pt ; Pl ; Pb ; Pr ' w
2238 // if (this.postfix === '\'') {
2239 // this.enableFilterRectangle(this.params);
2243 // CSI Ps x Request Terminal Parameters (DECREQTPARM).
2244 // CSI Ps x Select Attribute Change Extent (DECSACE).
2245 // CSI Pc; Pt; Pl; Pb; Pr$ x
2247 // if (this.postfix === '$') {
2248 // this.fillRectangle(this.params);
2250 // this.requestParameters(this.params);
2251 // //this.__(this.params);
2256 // CSI Pt; Pl; Pb; Pr$ z
2258 // if (this.postfix === '\'') {
2259 // this.enableLocatorReporting(this.params);
2260 // } else if (this.postfix === '$') {
2261 // this.eraseRectangle(this.params);
2266 // CSI Pt; Pl; Pb; Pr$ {
2268 // if (this.postfix === '\'') {
2269 // this.setLocatorEvents(this.params);
2270 // } else if (this.postfix === '$') {
2271 // this.selectiveEraseRectangle(this.params);
2277 // if (this.postfix === '\'') {
2278 // this.requestLocatorPosition(this.params);
2283 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
2285 // if (this.postfix === ' ') {
2286 // this.insertColumns(this.params);
2291 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
2293 // if (this.postfix === ' ') {
2294 // this.deleteColumns(this.params);
2299 this.error('Unknown CSI code: %s.', ch
);
2308 if (ch
=== '\x1b' || ch
=== '\x07') {
2309 if (ch
=== '\x1b') i
++;
2311 switch (this.prefix
) {
2312 // User-Defined Keys (DECUDK).
2316 // Request Status String (DECRQSS).
2317 // test: echo -e '\eP$q"p\e\\'
2319 var pt
= this.currentParam
2336 + (this.scrollTop
+ 1)
2338 + (this.scrollBottom
+ 1)
2348 this.error('Unknown DCS Pt: %s.', pt
);
2353 this.send('\x1bP' + +valid
+ '$r' + pt
+ '\x1b\\');
2356 // Set Termcap/Terminfo Data (xterm, experimental).
2360 // Request Termcap/Terminfo String (xterm, experimental)
2361 // Regular xterm does not even respond to this sequence.
2362 // This can cause a small glitch in vim.
2363 // test: echo -ne '\eP+q6b64\e\\'
2365 var pt
= this.currentParam
2368 this.send('\x1bP' + +valid
+ '+r' + pt
+ '\x1b\\');
2372 this.error('Unknown DCS prefix: %s.', this.prefix
);
2376 this.currentParam
= 0;
2378 this.state
= normal
;
2379 } else if (!this.currentParam
) {
2380 if (!this.prefix
&& ch
!== '$' && ch
!== '+') {
2381 this.currentParam
= ch
;
2382 } else if (this.prefix
.length
=== 2) {
2383 this.currentParam
= ch
;
2388 this.currentParam
+= ch
;
2394 if (ch
=== '\x1b' || ch
=== '\x07') {
2395 if (ch
=== '\x1b') i
++;
2396 this.state
= normal
;
2402 this.updateRange(this.y
);
2403 this.queueRefresh(this.refreshStart
, this.refreshEnd
);
2407 * Writes text to the terminal, followed by a break line character (\n).
2408 * @param {string} text The text to write to the terminal.
2410 Terminal
.prototype.writeln = function(data
) {
2411 this.write(data
+ '\r\n');
2415 * Attaches a custom keydown handler which is run before keys are processed, giving consumers of
2416 * xterm.js ultimate control as to what keys should be processed by the terminal and what keys
2418 * @param {function} customKeydownHandler The custom KeyboardEvent handler to attach. This is a
2419 * function that takes a KeyboardEvent, allowing consumers to stop propogation and/or prevent
2420 * the default action. The function returns whether the event should be processed by xterm.js.
2422 Terminal
.prototype.attachCustomKeydownHandler = function(customKeydownHandler
) {
2423 this.customKeydownHandler
= customKeydownHandler
;
2427 * Handle a keydown event
2429 * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
2430 * @param {KeyboardEvent} ev The keydown event to be handled.
2432 Terminal
.prototype.keyDown = function(ev
) {
2433 if (this.customKeydownHandler
&& this.customKeydownHandler(ev
) === false) {
2437 if (!this.compositionHelper
.keydown
.bind(this.compositionHelper
)(ev
)) {
2438 if (this.ybase
!== this.ydisp
) {
2439 this.scrollToBottom();
2445 var result
= this.evaluateKeyEscapeSequence(ev
);
2447 if (result
.scrollDisp
) {
2448 this.scrollDisp(result
.scrollDisp
);
2449 return this.cancel(ev
, true);
2452 if (isThirdLevelShift(this, ev
)) {
2456 if (result
.cancel
) {
2457 // The event is canceled at the end already, is this necessary?
2458 this.cancel(ev
, true);
2465 this.emit('keydown', ev
);
2466 this.emit('key', result
.key
, ev
);
2468 this.handler(result
.key
);
2470 return this.cancel(ev
, true);
2474 * Returns an object that determines how a KeyboardEvent should be handled. The key of the
2475 * returned value is the new key code to pass to the PTY.
2477 * Reference: http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
2478 * @param {KeyboardEvent} ev The keyboard event to be translated to key escape sequence.
2480 Terminal
.prototype.evaluateKeyEscapeSequence = function(ev
) {
2482 // Whether to cancel event propogation (NOTE: this may not be needed since the event is
2483 // canceled at the end of keyDown
2485 // The new key even to emit
2487 // The number of characters to scroll, if this is defined it will cancel the event
2488 scrollDisp
: undefined
2490 var modifiers
= ev
.shiftKey
<< 0 | ev
.altKey
<< 1 | ev
.ctrlKey
<< 2 | ev
.metaKey
<< 3;
2491 switch (ev
.keyCode
) {
2495 result
.key
= '\x08'; // ^H
2498 result
.key
= '\x7f'; // ^?
2503 result
.key
= '\x1b[Z';
2507 result
.cancel
= true;
2512 result
.cancel
= true;
2516 result
.key
= '\x1b';
2517 result
.cancel
= true;
2522 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'D';
2523 // HACK: Make Alt + left-arrow behave like Ctrl + left-arrow: move one word backwards
2524 // http://unix.stackexchange.com/a/108106
2525 // macOS uses different escape sequences than linux
2526 if (result
.key
== '\x1b[1;3D') {
2527 result
.key
= (this.browser
.isMac
) ? '\x1bb' : '\x1b[1;5D';
2529 } else if (this.applicationCursor
) {
2530 result
.key
= '\x1bOD';
2532 result
.key
= '\x1b[D';
2538 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'C';
2539 // HACK: Make Alt + right-arrow behave like Ctrl + right-arrow: move one word forward
2540 // http://unix.stackexchange.com/a/108106
2541 // macOS uses different escape sequences than linux
2542 if (result
.key
== '\x1b[1;3C') {
2543 result
.key
= (this.browser
.isMac
) ? '\x1bf' : '\x1b[1;5C';
2545 } else if (this.applicationCursor
) {
2546 result
.key
= '\x1bOC';
2548 result
.key
= '\x1b[C';
2554 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'A';
2555 // HACK: Make Alt + up-arrow behave like Ctrl + up-arrow
2556 // http://unix.stackexchange.com/a/108106
2557 if (result
.key
== '\x1b[1;3A') {
2558 result
.key
= '\x1b[1;5A';
2560 } else if (this.applicationCursor
) {
2561 result
.key
= '\x1bOA';
2563 result
.key
= '\x1b[A';
2569 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'B';
2570 // HACK: Make Alt + down-arrow behave like Ctrl + down-arrow
2571 // http://unix.stackexchange.com/a/108106
2572 if (result
.key
== '\x1b[1;3B') {
2573 result
.key
= '\x1b[1;5B';
2575 } else if (this.applicationCursor
) {
2576 result
.key
= '\x1bOB';
2578 result
.key
= '\x1b[B';
2583 if (!ev
.shiftKey
&& !ev
.ctrlKey
) {
2584 // <Ctrl> or <Shift> + <Insert> are used to
2585 // copy-paste on some systems.
2586 result
.key
= '\x1b[2~';
2592 result
.key
= '\x1b[3;' + (modifiers
+ 1) + '~';
2594 result
.key
= '\x1b[3~';
2600 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'H';
2601 else if (this.applicationCursor
)
2602 result
.key
= '\x1bOH';
2604 result
.key
= '\x1b[H';
2609 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'F';
2610 else if (this.applicationCursor
)
2611 result
.key
= '\x1bOF';
2613 result
.key
= '\x1b[F';
2618 result
.scrollDisp
= -(this.rows
- 1);
2620 result
.key
= '\x1b[5~';
2626 result
.scrollDisp
= this.rows
- 1;
2628 result
.key
= '\x1b[6~';
2634 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'P';
2636 result
.key
= '\x1bOP';
2641 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'Q';
2643 result
.key
= '\x1bOQ';
2648 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'R';
2650 result
.key
= '\x1bOR';
2655 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'S';
2657 result
.key
= '\x1bOS';
2662 result
.key
= '\x1b[15;' + (modifiers
+ 1) + '~';
2664 result
.key
= '\x1b[15~';
2669 result
.key
= '\x1b[17;' + (modifiers
+ 1) + '~';
2671 result
.key
= '\x1b[17~';
2676 result
.key
= '\x1b[18;' + (modifiers
+ 1) + '~';
2678 result
.key
= '\x1b[18~';
2683 result
.key
= '\x1b[19;' + (modifiers
+ 1) + '~';
2685 result
.key
= '\x1b[19~';
2690 result
.key
= '\x1b[20;' + (modifiers
+ 1) + '~';
2692 result
.key
= '\x1b[20~';
2697 result
.key
= '\x1b[21;' + (modifiers
+ 1) + '~';
2699 result
.key
= '\x1b[21~';
2704 result
.key
= '\x1b[23;' + (modifiers
+ 1) + '~';
2706 result
.key
= '\x1b[23~';
2711 result
.key
= '\x1b[24;' + (modifiers
+ 1) + '~';
2713 result
.key
= '\x1b[24~';
2718 if (ev
.ctrlKey
&& !ev
.shiftKey
&& !ev
.altKey
&& !ev
.metaKey
) {
2719 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2720 result
.key
= String
.fromCharCode(ev
.keyCode
- 64);
2721 } else if (ev
.keyCode
=== 32) {
2723 result
.key
= String
.fromCharCode(0);
2724 } else if (ev
.keyCode
>= 51 && ev
.keyCode
<= 55) {
2725 // escape, file sep, group sep, record sep, unit sep
2726 result
.key
= String
.fromCharCode(ev
.keyCode
- 51 + 27);
2727 } else if (ev
.keyCode
=== 56) {
2729 result
.key
= String
.fromCharCode(127);
2730 } else if (ev
.keyCode
=== 219) {
2731 // ^[ - Control Sequence Introducer (CSI)
2732 result
.key
= String
.fromCharCode(27);
2733 } else if (ev
.keyCode
=== 220) {
2734 // ^\ - String Terminator (ST)
2735 result
.key
= String
.fromCharCode(28);
2736 } else if (ev
.keyCode
=== 221) {
2737 // ^] - Operating System Command (OSC)
2738 result
.key
= String
.fromCharCode(29);
2740 } else if (!this.browser
.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) {
2741 // On Mac this is a third level shift. Use <Esc> instead.
2742 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2743 result
.key
= '\x1b' + String
.fromCharCode(ev
.keyCode
+ 32);
2744 } else if (ev
.keyCode
=== 192) {
2745 result
.key
= '\x1b`';
2746 } else if (ev
.keyCode
>= 48 && ev
.keyCode
<= 57) {
2747 result
.key
= '\x1b' + (ev
.keyCode
- 48);
2756 * Set the G level of the terminal
2759 Terminal
.prototype.setgLevel = function(g
) {
2761 this.charset
= this.charsets
[g
];
2765 * Set the charset for the given G level of the terminal
2769 Terminal
.prototype.setgCharset = function(g
, charset
) {
2770 this.charsets
[g
] = charset
;
2771 if (this.glevel
=== g
) {
2772 this.charset
= charset
;
2777 * Handle a keypress event.
2779 * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
2780 * @param {KeyboardEvent} ev The keypress event to be handled.
2782 Terminal
.prototype.keyPress = function(ev
) {
2789 } else if (ev
.which
== null) {
2791 } else if (ev
.which
!== 0 && ev
.charCode
!== 0) {
2798 (ev
.altKey
|| ev
.ctrlKey
|| ev
.metaKey
) && !isThirdLevelShift(this, ev
)
2803 key
= String
.fromCharCode(key
);
2805 this.emit('keypress', key
, ev
);
2806 this.emit('key', key
, ev
);
2814 * Send data for handling to the terminal
2815 * @param {string} data
2817 Terminal
.prototype.send = function(data
) {
2821 setTimeout(function() {
2822 self
.handler(self
.queue
);
2832 * Note: We could do sweet things with webaudio here
2834 Terminal
.prototype.bell = function() {
2835 if (!this.visualBell
) return;
2837 this.element
.style
.borderColor
= 'white';
2838 setTimeout(function() {
2839 self
.element
.style
.borderColor
= '';
2841 if (this.popOnBell
) this.focus();
2845 * Log the current state to the console.
2847 Terminal
.prototype.log = function() {
2848 if (!this.debug
) return;
2849 if (!this.context
.console
|| !this.context
.console
.log
) return;
2850 var args
= Array
.prototype.slice
.call(arguments
);
2851 this.context
.console
.log
.apply(this.context
.console
, args
);
2855 * Log the current state as error to the console.
2857 Terminal
.prototype.error = function() {
2858 if (!this.debug
) return;
2859 if (!this.context
.console
|| !this.context
.console
.error
) return;
2860 var args
= Array
.prototype.slice
.call(arguments
);
2861 this.context
.console
.error
.apply(this.context
.console
, args
);
2865 * Resizes the terminal.
2867 * @param {number} x The number of columns to resize to.
2868 * @param {number} y The number of rows to resize to.
2870 Terminal
.prototype.resize = function(x
, y
) {
2878 if (x
=== this.cols
&& y
=== this.rows
) {
2888 ch
= [this.defAttr
, ' ', 1]; // does xterm use the default attr?
2889 i
= this.lines
.length
;
2891 while (this.lines
.get(i
).length
< x
) {
2892 this.lines
.get(i
).push(ch
);
2896 i
= this.lines
.length
;
2898 while (this.lines
.get(i
).length
> x
) {
2899 this.lines
.get(i
).pop();
2912 // y is rows, not this.y
2913 if (this.lines
.length
< y
+ this.ybase
) {
2914 if (this.ybase
> 0 && this.lines
.length
<= this.ybase
+ this.y
+ addToY
+ 1) {
2915 // There is room above the buffer and there are no empty elements below the line,
2919 if (this.ydisp
> 0) {
2920 // Viewport is at the top of the buffer, must increase downwards
2924 // Add a blank line if there is no buffer left at the top to scroll to, or if there
2925 // are blank lines after the cursor
2926 this.lines
.push(this.blankLine());
2929 if (this.children
.length
< y
) {
2935 if (this.lines
.length
> y
+ this.ybase
) {
2936 if (this.lines
.length
> this.ybase
+ this.y
+ 1) {
2937 // The line is a blank line below the cursor, remove it
2940 // The line is the cursor, scroll down
2945 if (this.children
.length
> y
) {
2946 el
= this.children
.shift();
2948 el
.parentNode
.removeChild(el
);
2954 // Make sure that the cursor stays on screen
2967 this.scrollBottom
= y
- 1;
2969 this.charMeasure
.measure();
2971 this.queueRefresh(0, this.rows
- 1);
2975 this.geometry
= [this.cols
, this.rows
];
2976 this.emit('resize', {terminal
: this, cols
: x
, rows
: y
});
2980 * Updates the range of rows to refresh
2981 * @param {number} y The number of rows to refresh next.
2983 Terminal
.prototype.updateRange = function(y
) {
2984 if (y
< this.refreshStart
) this.refreshStart
= y
;
2985 if (y
> this.refreshEnd
) this.refreshEnd
= y
;
2986 // if (y > this.refreshEnd) {
2987 // this.refreshEnd = y;
2988 // if (y > this.rows - 1) {
2989 // this.refreshEnd = this.rows - 1;
2995 * Set the range of refreshing to the maximum value
2997 Terminal
.prototype.maxRange = function() {
2998 this.refreshStart
= 0;
2999 this.refreshEnd
= this.rows
- 1;
3005 * Setup the tab stops.
3008 Terminal
.prototype.setupStops = function(i
) {
3010 if (!this.tabs
[i
]) {
3011 i
= this.prevStop(i
);
3018 for (; i
< this.cols
; i
+= 8) {
3019 this.tabs
[i
] = true;
3025 * Move the cursor to the previous tab stop from the given position (default is current).
3026 * @param {number} x The position to move the cursor to the previous tab stop.
3028 Terminal
.prototype.prevStop = function(x
) {
3029 if (x
== null) x
= this.x
;
3030 while (!this.tabs
[--x
] && x
> 0);
3031 return x
>= this.cols
3038 * Move the cursor one tab stop forward from the given position (default is current).
3039 * @param {number} x The position to move the cursor one tab stop forward.
3041 Terminal
.prototype.nextStop = function(x
) {
3042 if (x
== null) x
= this.x
;
3043 while (!this.tabs
[++x
] && x
< this.cols
);
3044 return x
>= this.cols
3051 * Erase in the identified line everything from "x" to the end of the line (right).
3052 * @param {number} x The column from which to start erasing to the end of the line.
3053 * @param {number} y The line in which to operate.
3055 Terminal
.prototype.eraseRight = function(x
, y
) {
3056 var line
= this.lines
.get(this.ybase
+ y
)
3057 , ch
= [this.eraseAttr(), ' ', 1]; // xterm
3060 for (; x
< this.cols
; x
++) {
3064 this.updateRange(y
);
3070 * Erase in the identified line everything from "x" to the start of the line (left).
3071 * @param {number} x The column from which to start erasing to the start of the line.
3072 * @param {number} y The line in which to operate.
3074 Terminal
.prototype.eraseLeft = function(x
, y
) {
3075 var line
= this.lines
.get(this.ybase
+ y
)
3076 , ch
= [this.eraseAttr(), ' ', 1]; // xterm
3079 while (x
--) line
[x
] = ch
;
3081 this.updateRange(y
);
3085 * Clears the entire buffer, making the prompt line the new first line.
3087 Terminal
.prototype.clear = function() {
3088 if (this.ybase
=== 0 && this.y
=== 0) {
3089 // Don't clear if it's already clear
3092 this.lines
.set(0, this.lines
.get(this.ybase
+ this.y
));
3093 this.lines
.length
= 1;
3097 for (var i
= 1; i
< this.rows
; i
++) {
3098 this.lines
.push(this.blankLine());
3100 this.queueRefresh(0, this.rows
- 1);
3101 this.emit('scroll', this.ydisp
);
3105 * Erase all content in the given line
3106 * @param {number} y The line to erase all of its contents.
3108 Terminal
.prototype.eraseLine = function(y
) {
3109 this.eraseRight(0, y
);
3114 * Return the data array of a blank line
3115 * @param {number} cur First bunch of data for each "blank" character.
3117 Terminal
.prototype.blankLine = function(cur
) {
3122 var ch
= [attr
, ' ', 1] // width defaults to 1 halfwidth character
3126 for (; i
< this.cols
; i
++) {
3135 * If cur return the back color xterm feature attribute. Else return defAttr.
3136 * @param {object} cur
3138 Terminal
.prototype.ch = function(cur
) {
3140 ? [this.eraseAttr(), ' ', 1]
3141 : [this.defAttr
, ' ', 1];
3146 * Evaluate if the current erminal is the given argument.
3147 * @param {object} term The terminal to evaluate
3149 Terminal
.prototype.is = function(term
) {
3150 var name
= this.termName
;
3151 return (name
+ '').indexOf(term
) === 0;
3156 * Emit the 'data' event and populate the given data.
3157 * @param {string} data The data to populate in the event.
3159 Terminal
.prototype.handler = function(data
) {
3160 // Prevents all events to pty process if stdin is disabled
3161 if (this.options
.disableStdin
) {
3165 // Input is being sent to the terminal, the terminal should focus the prompt.
3166 if (this.ybase
!== this.ydisp
) {
3167 this.scrollToBottom();
3169 this.emit('data', data
);
3174 * Emit the 'title' event and populate the given title.
3175 * @param {string} title The title to populate in the event.
3177 Terminal
.prototype.handleTitle = function(title
) {
3179 * This event is emitted when the title of the terminal is changed
3180 * from inside the terminal. The parameter is the new title.
3184 this.emit('title', title
);
3193 * ESC D Index (IND is 0x84).
3195 Terminal
.prototype.index = function() {
3197 if (this.y
> this.scrollBottom
) {
3201 this.state
= normal
;
3206 * ESC M Reverse Index (RI is 0x8d).
3208 * Move the cursor up one row, inserting a new blank line if necessary.
3210 Terminal
.prototype.reverseIndex = function() {
3212 if (this.y
=== this.scrollTop
) {
3213 // possibly move the code below to term.reverseScroll();
3214 // test: echo -ne '\e[1;1H\e[44m\eM\e[0m'
3215 // blankLine(true) is xterm/linux behavior
3216 this.lines
.shiftElements(this.y
+ this.ybase
, this.rows
- 1, 1);
3217 this.lines
.set(this.y
+ this.ybase
, this.blankLine(true));
3218 this.updateRange(this.scrollTop
);
3219 this.updateRange(this.scrollBottom
);
3223 this.state
= normal
;
3228 * ESC c Full Reset (RIS).
3230 Terminal
.prototype.reset = function() {
3231 this.options
.rows
= this.rows
;
3232 this.options
.cols
= this.cols
;
3233 var customKeydownHandler
= this.customKeydownHandler
;
3234 Terminal
.call(this, this.options
);
3235 this.customKeydownHandler
= customKeydownHandler
;
3236 this.queueRefresh(0, this.rows
- 1);
3237 this.viewport
.syncScrollArea();
3242 * ESC H Tab Set (HTS is 0x88).
3244 Terminal
.prototype.tabSet = function() {
3245 this.tabs
[this.x
] = true;
3246 this.state
= normal
;
3256 * Cursor Up Ps Times (default = 1) (CUU).
3258 Terminal
.prototype.cursorUp = function(params
) {
3259 var param
= params
[0];
3260 if (param
< 1) param
= 1;
3262 if (this.y
< 0) this.y
= 0;
3268 * Cursor Down Ps Times (default = 1) (CUD).
3270 Terminal
.prototype.cursorDown = function(params
) {
3271 var param
= params
[0];
3272 if (param
< 1) param
= 1;
3274 if (this.y
>= this.rows
) {
3275 this.y
= this.rows
- 1;
3282 * Cursor Forward Ps Times (default = 1) (CUF).
3284 Terminal
.prototype.cursorForward = function(params
) {
3285 var param
= params
[0];
3286 if (param
< 1) param
= 1;
3288 if (this.x
>= this.cols
) {
3289 this.x
= this.cols
- 1;
3296 * Cursor Backward Ps Times (default = 1) (CUB).
3298 Terminal
.prototype.cursorBackward = function(params
) {
3299 var param
= params
[0];
3300 if (param
< 1) param
= 1;
3302 if (this.x
< 0) this.x
= 0;
3308 * Cursor Position [row;column] (default = [1,1]) (CUP).
3310 Terminal
.prototype.cursorPos = function(params
) {
3313 row
= params
[0] - 1;
3315 if (params
.length
>= 2) {
3316 col
= params
[1] - 1;
3323 } else if (row
>= this.rows
) {
3324 row
= this.rows
- 1;
3329 } else if (col
>= this.cols
) {
3330 col
= this.cols
- 1;
3339 * CSI Ps J Erase in Display (ED).
3340 * Ps = 0 -> Erase Below (default).
3341 * Ps = 1 -> Erase Above.
3342 * Ps = 2 -> Erase All.
3343 * Ps = 3 -> Erase Saved Lines (xterm).
3345 * Erase in Display (DECSED).
3346 * Ps = 0 -> Selective Erase Below (default).
3347 * Ps = 1 -> Selective Erase Above.
3348 * Ps = 2 -> Selective Erase All.
3350 Terminal
.prototype.eraseInDisplay = function(params
) {
3352 switch (params
[0]) {
3354 this.eraseRight(this.x
, this.y
);
3356 for (; j
< this.rows
; j
++) {
3361 this.eraseLeft(this.x
, this.y
);
3369 while (j
--) this.eraseLine(j
);
3379 * CSI Ps K Erase in Line (EL).
3380 * Ps = 0 -> Erase to Right (default).
3381 * Ps = 1 -> Erase to Left.
3382 * Ps = 2 -> Erase All.
3384 * Erase in Line (DECSEL).
3385 * Ps = 0 -> Selective Erase to Right (default).
3386 * Ps = 1 -> Selective Erase to Left.
3387 * Ps = 2 -> Selective Erase All.
3389 Terminal
.prototype.eraseInLine = function(params
) {
3390 switch (params
[0]) {
3392 this.eraseRight(this.x
, this.y
);
3395 this.eraseLeft(this.x
, this.y
);
3398 this.eraseLine(this.y
);
3405 * CSI Pm m Character Attributes (SGR).
3406 * Ps = 0 -> Normal (default).
3408 * Ps = 4 -> Underlined.
3409 * Ps = 5 -> Blink (appears as Bold).
3410 * Ps = 7 -> Inverse.
3411 * Ps = 8 -> Invisible, i.e., hidden (VT300).
3412 * Ps = 2 2 -> Normal (neither bold nor faint).
3413 * Ps = 2 4 -> Not underlined.
3414 * Ps = 2 5 -> Steady (not blinking).
3415 * Ps = 2 7 -> Positive (not inverse).
3416 * Ps = 2 8 -> Visible, i.e., not hidden (VT300).
3417 * Ps = 3 0 -> Set foreground color to Black.
3418 * Ps = 3 1 -> Set foreground color to Red.
3419 * Ps = 3 2 -> Set foreground color to Green.
3420 * Ps = 3 3 -> Set foreground color to Yellow.
3421 * Ps = 3 4 -> Set foreground color to Blue.
3422 * Ps = 3 5 -> Set foreground color to Magenta.
3423 * Ps = 3 6 -> Set foreground color to Cyan.
3424 * Ps = 3 7 -> Set foreground color to White.
3425 * Ps = 3 9 -> Set foreground color to default (original).
3426 * Ps = 4 0 -> Set background color to Black.
3427 * Ps = 4 1 -> Set background color to Red.
3428 * Ps = 4 2 -> Set background color to Green.
3429 * Ps = 4 3 -> Set background color to Yellow.
3430 * Ps = 4 4 -> Set background color to Blue.
3431 * Ps = 4 5 -> Set background color to Magenta.
3432 * Ps = 4 6 -> Set background color to Cyan.
3433 * Ps = 4 7 -> Set background color to White.
3434 * Ps = 4 9 -> Set background color to default (original).
3436 * If 16-color support is compiled, the following apply. Assume
3437 * that xterm's resources are set so that the ISO color codes are
3438 * the first 8 of a set of 16. Then the aixterm colors are the
3439 * bright versions of the ISO colors:
3440 * Ps = 9 0 -> Set foreground color to Black.
3441 * Ps = 9 1 -> Set foreground color to Red.
3442 * Ps = 9 2 -> Set foreground color to Green.
3443 * Ps = 9 3 -> Set foreground color to Yellow.
3444 * Ps = 9 4 -> Set foreground color to Blue.
3445 * Ps = 9 5 -> Set foreground color to Magenta.
3446 * Ps = 9 6 -> Set foreground color to Cyan.
3447 * Ps = 9 7 -> Set foreground color to White.
3448 * Ps = 1 0 0 -> Set background color to Black.
3449 * Ps = 1 0 1 -> Set background color to Red.
3450 * Ps = 1 0 2 -> Set background color to Green.
3451 * Ps = 1 0 3 -> Set background color to Yellow.
3452 * Ps = 1 0 4 -> Set background color to Blue.
3453 * Ps = 1 0 5 -> Set background color to Magenta.
3454 * Ps = 1 0 6 -> Set background color to Cyan.
3455 * Ps = 1 0 7 -> Set background color to White.
3457 * If xterm is compiled with the 16-color support disabled, it
3458 * supports the following, from rxvt:
3459 * Ps = 1 0 0 -> Set foreground and background color to
3462 * If 88- or 256-color support is compiled, the following apply.
3463 * Ps = 3 8 ; 5 ; Ps -> Set foreground color to the second
3465 * Ps = 4 8 ; 5 ; Ps -> Set background color to the second
3468 Terminal
.prototype.charAttributes = function(params
) {
3469 // Optimize a single SGR0.
3470 if (params
.length
=== 1 && params
[0] === 0) {
3471 this.curAttr
= this.defAttr
;
3475 var l
= params
.length
3477 , flags
= this.curAttr
>> 18
3478 , fg
= (this.curAttr
>> 9) & 0x1ff
3479 , bg
= this.curAttr
& 0x1ff
3482 for (; i
< l
; i
++) {
3484 if (p
>= 30 && p
<= 37) {
3487 } else if (p
>= 40 && p
<= 47) {
3490 } else if (p
>= 90 && p
<= 97) {
3494 } else if (p
>= 100 && p
<= 107) {
3498 } else if (p
=== 0) {
3500 flags
= this.defAttr
>> 18;
3501 fg
= (this.defAttr
>> 9) & 0x1ff;
3502 bg
= this.defAttr
& 0x1ff;
3506 } else if (p
=== 1) {
3509 } else if (p
=== 4) {
3512 } else if (p
=== 5) {
3515 } else if (p
=== 7) {
3516 // inverse and positive
3517 // test with: echo -e '\e[31m\e[42mhello\e[7mworld\e[27mhi\e[m'
3519 } else if (p
=== 8) {
3522 } else if (p
=== 22) {
3525 } else if (p
=== 24) {
3528 } else if (p
=== 25) {
3531 } else if (p
=== 27) {
3534 } else if (p
=== 28) {
3537 } else if (p
=== 39) {
3539 fg
= (this.defAttr
>> 9) & 0x1ff;
3540 } else if (p
=== 49) {
3542 bg
= this.defAttr
& 0x1ff;
3543 } else if (p
=== 38) {
3545 if (params
[i
+ 1] === 2) {
3549 params
[i
+ 1] & 0xff,
3550 params
[i
+ 2] & 0xff);
3551 if (fg
=== -1) fg
= 0x1ff;
3553 } else if (params
[i
+ 1] === 5) {
3555 p
= params
[i
] & 0xff;
3558 } else if (p
=== 48) {
3560 if (params
[i
+ 1] === 2) {
3564 params
[i
+ 1] & 0xff,
3565 params
[i
+ 2] & 0xff);
3566 if (bg
=== -1) bg
= 0x1ff;
3568 } else if (params
[i
+ 1] === 5) {
3570 p
= params
[i
] & 0xff;
3573 } else if (p
=== 100) {
3575 fg
= (this.defAttr
>> 9) & 0x1ff;
3576 bg
= this.defAttr
& 0x1ff;
3578 this.error('Unknown SGR attribute: %d.', p
);
3582 this.curAttr
= (flags
<< 18) | (fg
<< 9) | bg
;
3587 * CSI Ps n Device Status Report (DSR).
3588 * Ps = 5 -> Status Report. Result (``OK'') is
3590 * Ps = 6 -> Report Cursor Position (CPR) [row;column].
3594 * Device Status Report (DSR, DEC-specific).
3595 * Ps = 6 -> Report Cursor Position (CPR) [row;column] as CSI
3596 * ? r ; c R (assumes page is zero).
3597 * Ps = 1 5 -> Report Printer status as CSI ? 1 0 n (ready).
3598 * or CSI ? 1 1 n (not ready).
3599 * Ps = 2 5 -> Report UDK status as CSI ? 2 0 n (unlocked)
3600 * or CSI ? 2 1 n (locked).
3601 * Ps = 2 6 -> Report Keyboard status as
3602 * CSI ? 2 7 ; 1 ; 0 ; 0 n (North American).
3603 * The last two parameters apply to VT400 & up, and denote key-
3604 * board ready and LK01 respectively.
3605 * Ps = 5 3 -> Report Locator status as
3606 * CSI ? 5 3 n Locator available, if compiled-in, or
3607 * CSI ? 5 0 n No Locator, if not.
3609 Terminal
.prototype.deviceStatus = function(params
) {
3611 switch (params
[0]) {
3614 this.send('\x1b[0n');
3625 } else if (this.prefix
=== '?') {
3626 // modern xterm doesnt seem to
3627 // respond to any of these except ?6, 6, and 5
3628 switch (params
[0]) {
3639 // this.send('\x1b[?11n');
3642 // dont support user defined keys
3643 // this.send('\x1b[?21n');
3646 // north american keyboard
3647 // this.send('\x1b[?27;1;0;0n');
3650 // no dec locator/mouse
3651 // this.send('\x1b[?50n');
3664 * Insert Ps (Blank) Character(s) (default = 1) (ICH).
3666 Terminal
.prototype.insertChars = function(params
) {
3667 var param
, row
, j
, ch
;
3670 if (param
< 1) param
= 1;
3672 row
= this.y
+ this.ybase
;
3674 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3676 while (param
-- && j
< this.cols
) {
3677 this.lines
.get(row
).splice(j
++, 0, ch
);
3678 this.lines
.get(row
).pop();
3684 * Cursor Next Line Ps Times (default = 1) (CNL).
3685 * same as CSI Ps B ?
3687 Terminal
.prototype.cursorNextLine = function(params
) {
3688 var param
= params
[0];
3689 if (param
< 1) param
= 1;
3691 if (this.y
>= this.rows
) {
3692 this.y
= this.rows
- 1;
3700 * Cursor Preceding Line Ps Times (default = 1) (CNL).
3703 Terminal
.prototype.cursorPrecedingLine = function(params
) {
3704 var param
= params
[0];
3705 if (param
< 1) param
= 1;
3707 if (this.y
< 0) this.y
= 0;
3714 * Cursor Character Absolute [column] (default = [row,1]) (CHA).
3716 Terminal
.prototype.cursorCharAbsolute = function(params
) {
3717 var param
= params
[0];
3718 if (param
< 1) param
= 1;
3725 * Insert Ps Line(s) (default = 1) (IL).
3727 Terminal
.prototype.insertLines = function(params
) {
3731 if (param
< 1) param
= 1;
3732 row
= this.y
+ this.ybase
;
3734 j
= this.rows
- 1 - this.scrollBottom
;
3735 j
= this.rows
- 1 + this.ybase
- j
+ 1;
3738 if (this.lines
.length
=== this.lines
.maxLength
) {
3739 // Trim the start of lines to make room for the new line
3740 this.lines
.trimStart(1);
3746 // test: echo -e '\e[44m\e[1L\e[0m'
3747 // blankLine(true) - xterm/linux behavior
3748 this.lines
.splice(row
, 0, this.blankLine(true));
3749 this.lines
.splice(j
, 1);
3753 this.updateRange(this.y
);
3754 this.updateRange(this.scrollBottom
);
3760 * Delete Ps Line(s) (default = 1) (DL).
3762 Terminal
.prototype.deleteLines = function(params
) {
3766 if (param
< 1) param
= 1;
3767 row
= this.y
+ this.ybase
;
3769 j
= this.rows
- 1 - this.scrollBottom
;
3770 j
= this.rows
- 1 + this.ybase
- j
;
3773 if (this.lines
.length
=== this.lines
.maxLength
) {
3774 // Trim the start of lines to make room for the new line
3775 this.lines
.trimStart(1);
3779 // test: echo -e '\e[44m\e[1M\e[0m'
3780 // blankLine(true) - xterm/linux behavior
3781 this.lines
.splice(j
+ 1, 0, this.blankLine(true));
3782 this.lines
.splice(row
, 1);
3786 this.updateRange(this.y
);
3787 this.updateRange(this.scrollBottom
);
3793 * Delete Ps Character(s) (default = 1) (DCH).
3795 Terminal
.prototype.deleteChars = function(params
) {
3799 if (param
< 1) param
= 1;
3801 row
= this.y
+ this.ybase
;
3802 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3805 this.lines
.get(row
).splice(this.x
, 1);
3806 this.lines
.get(row
).push(ch
);
3812 * Erase Ps Character(s) (default = 1) (ECH).
3814 Terminal
.prototype.eraseChars = function(params
) {
3815 var param
, row
, j
, ch
;
3818 if (param
< 1) param
= 1;
3820 row
= this.y
+ this.ybase
;
3822 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3824 while (param
-- && j
< this.cols
) {
3825 this.lines
.get(row
)[j
++] = ch
;
3830 * CSI Pm ` Character Position Absolute
3831 * [column] (default = [row,1]) (HPA).
3833 Terminal
.prototype.charPosAbsolute = function(params
) {
3834 var param
= params
[0];
3835 if (param
< 1) param
= 1;
3837 if (this.x
>= this.cols
) {
3838 this.x
= this.cols
- 1;
3845 * Horizontal Position Relative
3848 Terminal
.prototype.HPositionRelative = function(params
) {
3849 var param
= params
[0];
3850 if (param
< 1) param
= 1;
3852 if (this.x
>= this.cols
) {
3853 this.x
= this.cols
- 1;
3859 * CSI Ps c Send Device Attributes (Primary DA).
3860 * Ps = 0 or omitted -> request attributes from terminal. The
3861 * response depends on the decTerminalID resource setting.
3862 * -> CSI ? 1 ; 2 c (``VT100 with Advanced Video Option'')
3863 * -> CSI ? 1 ; 0 c (``VT101 with No Options'')
3864 * -> CSI ? 6 c (``VT102'')
3865 * -> CSI ? 6 0 ; 1 ; 2 ; 6 ; 8 ; 9 ; 1 5 ; c (``VT220'')
3866 * The VT100-style response parameters do not mean anything by
3867 * themselves. VT220 parameters do, telling the host what fea-
3868 * tures the terminal supports:
3869 * Ps = 1 -> 132-columns.
3870 * Ps = 2 -> Printer.
3871 * Ps = 6 -> Selective erase.
3872 * Ps = 8 -> User-defined keys.
3873 * Ps = 9 -> National replacement character sets.
3874 * Ps = 1 5 -> Technical characters.
3875 * Ps = 2 2 -> ANSI color, e.g., VT525.
3876 * Ps = 2 9 -> ANSI text locator (i.e., DEC Locator mode).
3878 * Send Device Attributes (Secondary DA).
3879 * Ps = 0 or omitted -> request the terminal's identification
3880 * code. The response depends on the decTerminalID resource set-
3881 * ting. It should apply only to VT220 and up, but xterm extends
3883 * -> CSI > Pp ; Pv ; Pc c
3884 * where Pp denotes the terminal type
3885 * Pp = 0 -> ``VT100''.
3886 * Pp = 1 -> ``VT220''.
3887 * and Pv is the firmware version (for xterm, this was originally
3888 * the XFree86 patch number, starting with 95). In a DEC termi-
3889 * nal, Pc indicates the ROM cartridge registration number and is
3892 * xterm/charproc.c - line 2012, for more information.
3893 * vim responds with ^[[?0c or ^[[?1c after the terminal's response (?)
3895 Terminal
.prototype.sendDeviceAttributes = function(params
) {
3896 if (params
[0] > 0) return;
3899 if (this.is('xterm')
3900 || this.is('rxvt-unicode')
3901 || this.is('screen')) {
3902 this.send('\x1b[?1;2c');
3903 } else if (this.is('linux')) {
3904 this.send('\x1b[?6c');
3906 } else if (this.prefix
=== '>') {
3908 // seem to spit this
3909 // out around ~370 times (?).
3910 if (this.is('xterm')) {
3911 this.send('\x1b[>0;276;0c');
3912 } else if (this.is('rxvt-unicode')) {
3913 this.send('\x1b[>85;95;0c');
3914 } else if (this.is('linux')) {
3915 // not supported by linux console.
3916 // linux console echoes parameters.
3917 this.send(params
[0] + 'c');
3918 } else if (this.is('screen')) {
3919 this.send('\x1b[>83;40003;0c');
3927 * Line Position Absolute [row] (default = [1,column]) (VPA).
3929 Terminal
.prototype.linePosAbsolute = function(params
) {
3930 var param
= params
[0];
3931 if (param
< 1) param
= 1;
3933 if (this.y
>= this.rows
) {
3934 this.y
= this.rows
- 1;
3940 * 145 65 e * VPR - Vertical Position Relative
3943 Terminal
.prototype.VPositionRelative = function(params
) {
3944 var param
= params
[0];
3945 if (param
< 1) param
= 1;
3947 if (this.y
>= this.rows
) {
3948 this.y
= this.rows
- 1;
3955 * Horizontal and Vertical Position [row;column] (default =
3958 Terminal
.prototype.HVPosition = function(params
) {
3959 if (params
[0] < 1) params
[0] = 1;
3960 if (params
[1] < 1) params
[1] = 1;
3962 this.y
= params
[0] - 1;
3963 if (this.y
>= this.rows
) {
3964 this.y
= this.rows
- 1;
3967 this.x
= params
[1] - 1;
3968 if (this.x
>= this.cols
) {
3969 this.x
= this.cols
- 1;
3975 * CSI Pm h Set Mode (SM).
3976 * Ps = 2 -> Keyboard Action Mode (AM).
3977 * Ps = 4 -> Insert Mode (IRM).
3978 * Ps = 1 2 -> Send/receive (SRM).
3979 * Ps = 2 0 -> Automatic Newline (LNM).
3981 * DEC Private Mode Set (DECSET).
3982 * Ps = 1 -> Application Cursor Keys (DECCKM).
3983 * Ps = 2 -> Designate USASCII for character sets G0-G3
3984 * (DECANM), and set VT100 mode.
3985 * Ps = 3 -> 132 Column Mode (DECCOLM).
3986 * Ps = 4 -> Smooth (Slow) Scroll (DECSCLM).
3987 * Ps = 5 -> Reverse Video (DECSCNM).
3988 * Ps = 6 -> Origin Mode (DECOM).
3989 * Ps = 7 -> Wraparound Mode (DECAWM).
3990 * Ps = 8 -> Auto-repeat Keys (DECARM).
3991 * Ps = 9 -> Send Mouse X & Y on button press. See the sec-
3992 * tion Mouse Tracking.
3993 * Ps = 1 0 -> Show toolbar (rxvt).
3994 * Ps = 1 2 -> Start Blinking Cursor (att610).
3995 * Ps = 1 8 -> Print form feed (DECPFF).
3996 * Ps = 1 9 -> Set print extent to full screen (DECPEX).
3997 * Ps = 2 5 -> Show Cursor (DECTCEM).
3998 * Ps = 3 0 -> Show scrollbar (rxvt).
3999 * Ps = 3 5 -> Enable font-shifting functions (rxvt).
4000 * Ps = 3 8 -> Enter Tektronix Mode (DECTEK).
4001 * Ps = 4 0 -> Allow 80 -> 132 Mode.
4002 * Ps = 4 1 -> more(1) fix (see curses resource).
4003 * Ps = 4 2 -> Enable Nation Replacement Character sets (DECN-
4005 * Ps = 4 4 -> Turn On Margin Bell.
4006 * Ps = 4 5 -> Reverse-wraparound Mode.
4007 * Ps = 4 6 -> Start Logging. This is normally disabled by a
4008 * compile-time option.
4009 * Ps = 4 7 -> Use Alternate Screen Buffer. (This may be dis-
4010 * abled by the titeInhibit resource).
4011 * Ps = 6 6 -> Application keypad (DECNKM).
4012 * Ps = 6 7 -> Backarrow key sends backspace (DECBKM).
4013 * Ps = 1 0 0 0 -> Send Mouse X & Y on button press and
4014 * release. See the section Mouse Tracking.
4015 * Ps = 1 0 0 1 -> Use Hilite Mouse Tracking.
4016 * Ps = 1 0 0 2 -> Use Cell Motion Mouse Tracking.
4017 * Ps = 1 0 0 3 -> Use All Motion Mouse Tracking.
4018 * Ps = 1 0 0 4 -> Send FocusIn/FocusOut events.
4019 * Ps = 1 0 0 5 -> Enable Extended Mouse Mode.
4020 * Ps = 1 0 1 0 -> Scroll to bottom on tty output (rxvt).
4021 * Ps = 1 0 1 1 -> Scroll to bottom on key press (rxvt).
4022 * Ps = 1 0 3 4 -> Interpret "meta" key, sets eighth bit.
4023 * (enables the eightBitInput resource).
4024 * Ps = 1 0 3 5 -> Enable special modifiers for Alt and Num-
4025 * Lock keys. (This enables the numLock resource).
4026 * Ps = 1 0 3 6 -> Send ESC when Meta modifies a key. (This
4027 * enables the metaSendsEscape resource).
4028 * Ps = 1 0 3 7 -> Send DEL from the editing-keypad Delete
4030 * Ps = 1 0 3 9 -> Send ESC when Alt modifies a key. (This
4031 * enables the altSendsEscape resource).
4032 * Ps = 1 0 4 0 -> Keep selection even if not highlighted.
4033 * (This enables the keepSelection resource).
4034 * Ps = 1 0 4 1 -> Use the CLIPBOARD selection. (This enables
4035 * the selectToClipboard resource).
4036 * Ps = 1 0 4 2 -> Enable Urgency window manager hint when
4037 * Control-G is received. (This enables the bellIsUrgent
4039 * Ps = 1 0 4 3 -> Enable raising of the window when Control-G
4040 * is received. (enables the popOnBell resource).
4041 * Ps = 1 0 4 7 -> Use Alternate Screen Buffer. (This may be
4042 * disabled by the titeInhibit resource).
4043 * Ps = 1 0 4 8 -> Save cursor as in DECSC. (This may be dis-
4044 * abled by the titeInhibit resource).
4045 * Ps = 1 0 4 9 -> Save cursor as in DECSC and use Alternate
4046 * Screen Buffer, clearing it first. (This may be disabled by
4047 * the titeInhibit resource). This combines the effects of the 1
4048 * 0 4 7 and 1 0 4 8 modes. Use this with terminfo-based
4049 * applications rather than the 4 7 mode.
4050 * Ps = 1 0 5 0 -> Set terminfo/termcap function-key mode.
4051 * Ps = 1 0 5 1 -> Set Sun function-key mode.
4052 * Ps = 1 0 5 2 -> Set HP function-key mode.
4053 * Ps = 1 0 5 3 -> Set SCO function-key mode.
4054 * Ps = 1 0 6 0 -> Set legacy keyboard emulation (X11R6).
4055 * Ps = 1 0 6 1 -> Set VT220 keyboard emulation.
4056 * Ps = 2 0 0 4 -> Set bracketed paste mode.
4058 * http: *vt100.net/docs/vt220-rm/chapter4.html
4060 Terminal
.prototype.setMode = function(params
) {
4061 if (typeof params
=== 'object') {
4062 var l
= params
.length
4065 for (; i
< l
; i
++) {
4066 this.setMode(params
[i
]);
4075 this.insertMode
= true;
4078 //this.convertEol = true;
4081 } else if (this.prefix
=== '?') {
4084 this.applicationCursor
= true;
4087 this.setgCharset(0, Terminal
.charsets
.US
);
4088 this.setgCharset(1, Terminal
.charsets
.US
);
4089 this.setgCharset(2, Terminal
.charsets
.US
);
4090 this.setgCharset(3, Terminal
.charsets
.US
);
4091 // set VT100 mode here
4093 case 3: // 132 col mode
4094 this.savedCols
= this.cols
;
4095 this.resize(132, this.rows
);
4098 this.originMode
= true;
4101 this.wraparoundMode
= true;
4104 // this.cursorBlink = true;
4107 this.log('Serial port requested application keypad.');
4108 this.applicationKeypad
= true;
4109 this.viewport
.syncScrollArea();
4111 case 9: // X10 Mouse
4112 // no release, no motion, no wheel, no modifiers.
4113 case 1000: // vt200 mouse
4115 // no modifiers, except control on the wheel.
4116 case 1002: // button event mouse
4117 case 1003: // any event mouse
4118 // any event - sends motion events,
4119 // even if there is no button held down.
4120 this.x10Mouse
= params
=== 9;
4121 this.vt200Mouse
= params
=== 1000;
4122 this.normalMouse
= params
> 1000;
4123 this.mouseEvents
= true;
4124 this.element
.style
.cursor
= 'default';
4125 this.log('Binding to mouse events.');
4127 case 1004: // send focusin/focusout events
4130 this.sendFocus
= true;
4132 case 1005: // utf8 ext mode mouse
4133 this.utfMouse
= true;
4134 // for wide terminals
4135 // simply encodes large values as utf8 characters
4137 case 1006: // sgr ext mode mouse
4138 this.sgrMouse
= true;
4139 // for wide terminals
4140 // does not add 32 to fields
4141 // press: ^[[<b;x;yM
4142 // release: ^[[<b;x;ym
4144 case 1015: // urxvt ext mode mouse
4145 this.urxvtMouse
= true;
4146 // for wide terminals
4147 // numbers for fields
4149 // motion: ^[[b;x;yT
4151 case 25: // show cursor
4152 this.cursorHidden
= false;
4154 case 1049: // alt screen buffer cursor
4155 //this.saveCursor();
4157 case 47: // alt screen buffer
4158 case 1047: // alt screen buffer
4166 scrollTop
: this.scrollTop
,
4167 scrollBottom
: this.scrollBottom
,
4169 // XXX save charset(s) here?
4170 // charset: this.charset,
4171 // glevel: this.glevel,
4172 // charsets: this.charsets
4175 this.viewport
.syncScrollArea();
4176 this.normal
= normal
;
4185 * CSI Pm l Reset Mode (RM).
4186 * Ps = 2 -> Keyboard Action Mode (AM).
4187 * Ps = 4 -> Replace Mode (IRM).
4188 * Ps = 1 2 -> Send/receive (SRM).
4189 * Ps = 2 0 -> Normal Linefeed (LNM).
4191 * DEC Private Mode Reset (DECRST).
4192 * Ps = 1 -> Normal Cursor Keys (DECCKM).
4193 * Ps = 2 -> Designate VT52 mode (DECANM).
4194 * Ps = 3 -> 80 Column Mode (DECCOLM).
4195 * Ps = 4 -> Jump (Fast) Scroll (DECSCLM).
4196 * Ps = 5 -> Normal Video (DECSCNM).
4197 * Ps = 6 -> Normal Cursor Mode (DECOM).
4198 * Ps = 7 -> No Wraparound Mode (DECAWM).
4199 * Ps = 8 -> No Auto-repeat Keys (DECARM).
4200 * Ps = 9 -> Don't send Mouse X & Y on button press.
4201 * Ps = 1 0 -> Hide toolbar (rxvt).
4202 * Ps = 1 2 -> Stop Blinking Cursor (att610).
4203 * Ps = 1 8 -> Don't print form feed (DECPFF).
4204 * Ps = 1 9 -> Limit print to scrolling region (DECPEX).
4205 * Ps = 2 5 -> Hide Cursor (DECTCEM).
4206 * Ps = 3 0 -> Don't show scrollbar (rxvt).
4207 * Ps = 3 5 -> Disable font-shifting functions (rxvt).
4208 * Ps = 4 0 -> Disallow 80 -> 132 Mode.
4209 * Ps = 4 1 -> No more(1) fix (see curses resource).
4210 * Ps = 4 2 -> Disable Nation Replacement Character sets (DEC-
4212 * Ps = 4 4 -> Turn Off Margin Bell.
4213 * Ps = 4 5 -> No Reverse-wraparound Mode.
4214 * Ps = 4 6 -> Stop Logging. (This is normally disabled by a
4215 * compile-time option).
4216 * Ps = 4 7 -> Use Normal Screen Buffer.
4217 * Ps = 6 6 -> Numeric keypad (DECNKM).
4218 * Ps = 6 7 -> Backarrow key sends delete (DECBKM).
4219 * Ps = 1 0 0 0 -> Don't send Mouse X & Y on button press and
4220 * release. See the section Mouse Tracking.
4221 * Ps = 1 0 0 1 -> Don't use Hilite Mouse Tracking.
4222 * Ps = 1 0 0 2 -> Don't use Cell Motion Mouse Tracking.
4223 * Ps = 1 0 0 3 -> Don't use All Motion Mouse Tracking.
4224 * Ps = 1 0 0 4 -> Don't send FocusIn/FocusOut events.
4225 * Ps = 1 0 0 5 -> Disable Extended Mouse Mode.
4226 * Ps = 1 0 1 0 -> Don't scroll to bottom on tty output
4228 * Ps = 1 0 1 1 -> Don't scroll to bottom on key press (rxvt).
4229 * Ps = 1 0 3 4 -> Don't interpret "meta" key. (This disables
4230 * the eightBitInput resource).
4231 * Ps = 1 0 3 5 -> Disable special modifiers for Alt and Num-
4232 * Lock keys. (This disables the numLock resource).
4233 * Ps = 1 0 3 6 -> Don't send ESC when Meta modifies a key.
4234 * (This disables the metaSendsEscape resource).
4235 * Ps = 1 0 3 7 -> Send VT220 Remove from the editing-keypad
4237 * Ps = 1 0 3 9 -> Don't send ESC when Alt modifies a key.
4238 * (This disables the altSendsEscape resource).
4239 * Ps = 1 0 4 0 -> Do not keep selection when not highlighted.
4240 * (This disables the keepSelection resource).
4241 * Ps = 1 0 4 1 -> Use the PRIMARY selection. (This disables
4242 * the selectToClipboard resource).
4243 * Ps = 1 0 4 2 -> Disable Urgency window manager hint when
4244 * Control-G is received. (This disables the bellIsUrgent
4246 * Ps = 1 0 4 3 -> Disable raising of the window when Control-
4247 * G is received. (This disables the popOnBell resource).
4248 * Ps = 1 0 4 7 -> Use Normal Screen Buffer, clearing screen
4249 * first if in the Alternate Screen. (This may be disabled by
4250 * the titeInhibit resource).
4251 * Ps = 1 0 4 8 -> Restore cursor as in DECRC. (This may be
4252 * disabled by the titeInhibit resource).
4253 * Ps = 1 0 4 9 -> Use Normal Screen Buffer and restore cursor
4254 * as in DECRC. (This may be disabled by the titeInhibit
4255 * resource). This combines the effects of the 1 0 4 7 and 1 0
4256 * 4 8 modes. Use this with terminfo-based applications rather
4257 * than the 4 7 mode.
4258 * Ps = 1 0 5 0 -> Reset terminfo/termcap function-key mode.
4259 * Ps = 1 0 5 1 -> Reset Sun function-key mode.
4260 * Ps = 1 0 5 2 -> Reset HP function-key mode.
4261 * Ps = 1 0 5 3 -> Reset SCO function-key mode.
4262 * Ps = 1 0 6 0 -> Reset legacy keyboard emulation (X11R6).
4263 * Ps = 1 0 6 1 -> Reset keyboard emulation to Sun/PC style.
4264 * Ps = 2 0 0 4 -> Reset bracketed paste mode.
4266 Terminal
.prototype.resetMode = function(params
) {
4267 if (typeof params
=== 'object') {
4268 var l
= params
.length
4271 for (; i
< l
; i
++) {
4272 this.resetMode(params
[i
]);
4281 this.insertMode
= false;
4284 //this.convertEol = false;
4287 } else if (this.prefix
=== '?') {
4290 this.applicationCursor
= false;
4293 if (this.cols
=== 132 && this.savedCols
) {
4294 this.resize(this.savedCols
, this.rows
);
4296 delete this.savedCols
;
4299 this.originMode
= false;
4302 this.wraparoundMode
= false;
4305 // this.cursorBlink = false;
4308 this.log('Switching back to normal keypad.');
4309 this.applicationKeypad
= false;
4310 this.viewport
.syncScrollArea();
4312 case 9: // X10 Mouse
4313 case 1000: // vt200 mouse
4314 case 1002: // button event mouse
4315 case 1003: // any event mouse
4316 this.x10Mouse
= false;
4317 this.vt200Mouse
= false;
4318 this.normalMouse
= false;
4319 this.mouseEvents
= false;
4320 this.element
.style
.cursor
= '';
4322 case 1004: // send focusin/focusout events
4323 this.sendFocus
= false;
4325 case 1005: // utf8 ext mode mouse
4326 this.utfMouse
= false;
4328 case 1006: // sgr ext mode mouse
4329 this.sgrMouse
= false;
4331 case 1015: // urxvt ext mode mouse
4332 this.urxvtMouse
= false;
4334 case 25: // hide cursor
4335 this.cursorHidden
= true;
4337 case 1049: // alt screen buffer cursor
4339 case 47: // normal screen buffer
4340 case 1047: // normal screen buffer - clearing it first
4342 this.lines
= this.normal
.lines
;
4343 this.ybase
= this.normal
.ybase
;
4344 this.ydisp
= this.normal
.ydisp
;
4345 this.x
= this.normal
.x
;
4346 this.y
= this.normal
.y
;
4347 this.scrollTop
= this.normal
.scrollTop
;
4348 this.scrollBottom
= this.normal
.scrollBottom
;
4349 this.tabs
= this.normal
.tabs
;
4351 // if (params === 1049) {
4352 // this.x = this.savedX;
4353 // this.y = this.savedY;
4355 this.queueRefresh(0, this.rows
- 1);
4356 this.viewport
.syncScrollArea();
4367 * Set Scrolling Region [top;bottom] (default = full size of win-
4371 Terminal
.prototype.setScrollRegion = function(params
) {
4372 if (this.prefix
) return;
4373 this.scrollTop
= (params
[0] || 1) - 1;
4374 this.scrollBottom
= (params
[1] || this.rows
) - 1;
4382 * Save cursor (ANSI.SYS).
4384 Terminal
.prototype.saveCursor = function(params
) {
4385 this.savedX
= this.x
;
4386 this.savedY
= this.y
;
4392 * Restore cursor (ANSI.SYS).
4394 Terminal
.prototype.restoreCursor = function(params
) {
4395 this.x
= this.savedX
|| 0;
4396 this.y
= this.savedY
|| 0;
4406 * Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
4408 Terminal
.prototype.cursorForwardTab = function(params
) {
4409 var param
= params
[0] || 1;
4411 this.x
= this.nextStop();
4417 * CSI Ps S Scroll up Ps lines (default = 1) (SU).
4419 Terminal
.prototype.scrollUp = function(params
) {
4420 var param
= params
[0] || 1;
4422 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
4423 this.lines
.splice(this.ybase
+ this.scrollBottom
, 0, this.blankLine());
4426 this.updateRange(this.scrollTop
);
4427 this.updateRange(this.scrollBottom
);
4432 * CSI Ps T Scroll down Ps lines (default = 1) (SD).
4434 Terminal
.prototype.scrollDown = function(params
) {
4435 var param
= params
[0] || 1;
4437 this.lines
.splice(this.ybase
+ this.scrollBottom
, 1);
4438 this.lines
.splice(this.ybase
+ this.scrollTop
, 0, this.blankLine());
4441 this.updateRange(this.scrollTop
);
4442 this.updateRange(this.scrollBottom
);
4447 * CSI Ps ; Ps ; Ps ; Ps ; Ps T
4448 * Initiate highlight mouse tracking. Parameters are
4449 * [func;startx;starty;firstrow;lastrow]. See the section Mouse
4452 Terminal
.prototype.initMouseTracking = function(params
) {
4453 // Relevant: DECSET 1001
4459 * Reset one or more features of the title modes to the default
4460 * value. Normally, "reset" disables the feature. It is possi-
4461 * ble to disable the ability to reset features by compiling a
4462 * different default for the title modes into xterm.
4463 * Ps = 0 -> Do not set window/icon labels using hexadecimal.
4464 * Ps = 1 -> Do not query window/icon labels using hexadeci-
4466 * Ps = 2 -> Do not set window/icon labels using UTF-8.
4467 * Ps = 3 -> Do not query window/icon labels using UTF-8.
4468 * (See discussion of "Title Modes").
4470 Terminal
.prototype.resetTitleModes = function(params
) {
4476 * CSI Ps Z Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
4478 Terminal
.prototype.cursorBackwardTab = function(params
) {
4479 var param
= params
[0] || 1;
4481 this.x
= this.prevStop();
4487 * CSI Ps b Repeat the preceding graphic character Ps times (REP).
4489 Terminal
.prototype.repeatPrecedingCharacter = function(params
) {
4490 var param
= params
[0] || 1
4491 , line
= this.lines
.get(this.ybase
+ this.y
)
4492 , ch
= line
[this.x
- 1] || [this.defAttr
, ' ', 1];
4494 while (param
--) line
[this.x
++] = ch
;
4499 * CSI Ps g Tab Clear (TBC).
4500 * Ps = 0 -> Clear Current Column (default).
4501 * Ps = 3 -> Clear All.
4503 * Ps = 2 -> Clear Stops on Line.
4504 * http://vt100.net/annarbor/aaa-ug/section6.html
4506 Terminal
.prototype.tabClear = function(params
) {
4507 var param
= params
[0];
4509 delete this.tabs
[this.x
];
4510 } else if (param
=== 3) {
4517 * CSI Pm i Media Copy (MC).
4518 * Ps = 0 -> Print screen (default).
4519 * Ps = 4 -> Turn off printer controller mode.
4520 * Ps = 5 -> Turn on printer controller mode.
4522 * Media Copy (MC, DEC-specific).
4523 * Ps = 1 -> Print line containing cursor.
4524 * Ps = 4 -> Turn off autoprint mode.
4525 * Ps = 5 -> Turn on autoprint mode.
4526 * Ps = 1 0 -> Print composed display, ignores DECPEX.
4527 * Ps = 1 1 -> Print all pages.
4529 Terminal
.prototype.mediaCopy = function(params
) {
4536 * Set or reset resource-values used by xterm to decide whether
4537 * to construct escape sequences holding information about the
4538 * modifiers pressed with a given key. The first parameter iden-
4539 * tifies the resource to set/reset. The second parameter is the
4540 * value to assign to the resource. If the second parameter is
4541 * omitted, the resource is reset to its initial value.
4542 * Ps = 1 -> modifyCursorKeys.
4543 * Ps = 2 -> modifyFunctionKeys.
4544 * Ps = 4 -> modifyOtherKeys.
4545 * If no parameters are given, all resources are reset to their
4548 Terminal
.prototype.setResources = function(params
) {
4555 * Disable modifiers which may be enabled via the CSI > Ps; Ps m
4556 * sequence. This corresponds to a resource value of "-1", which
4557 * cannot be set with the other sequence. The parameter identi-
4558 * fies the resource to be disabled:
4559 * Ps = 1 -> modifyCursorKeys.
4560 * Ps = 2 -> modifyFunctionKeys.
4561 * Ps = 4 -> modifyOtherKeys.
4562 * If the parameter is omitted, modifyFunctionKeys is disabled.
4563 * When modifyFunctionKeys is disabled, xterm uses the modifier
4564 * keys to make an extended sequence of functions rather than
4565 * adding a parameter to each function key to denote the modi-
4568 Terminal
.prototype.disableModifiers = function(params
) {
4575 * Set resource value pointerMode. This is used by xterm to
4576 * decide whether to hide the pointer cursor as the user types.
4577 * Valid values for the parameter:
4578 * Ps = 0 -> never hide the pointer.
4579 * Ps = 1 -> hide if the mouse tracking mode is not enabled.
4580 * Ps = 2 -> always hide the pointer. If no parameter is
4581 * given, xterm uses the default, which is 1 .
4583 Terminal
.prototype.setPointerMode = function(params
) {
4589 * CSI ! p Soft terminal reset (DECSTR).
4590 * http://vt100.net/docs/vt220-rm/table4-10.html
4592 Terminal
.prototype.softReset = function(params
) {
4593 this.cursorHidden
= false;
4594 this.insertMode
= false;
4595 this.originMode
= false;
4596 this.wraparoundMode
= false; // autowrap
4597 this.applicationKeypad
= false; // ?
4598 this.viewport
.syncScrollArea();
4599 this.applicationCursor
= false;
4601 this.scrollBottom
= this.rows
- 1;
4602 this.curAttr
= this.defAttr
;
4603 this.x
= this.y
= 0; // ?
4604 this.charset
= null;
4605 this.glevel
= 0; // ??
4606 this.charsets
= [null]; // ??
4612 * Request ANSI mode (DECRQM). For VT300 and up, reply is
4614 * where Ps is the mode number as in RM, and Pm is the mode
4616 * 0 - not recognized
4619 * 3 - permanently set
4620 * 4 - permanently reset
4622 Terminal
.prototype.requestAnsiMode = function(params
) {
4629 * Request DEC private mode (DECRQM). For VT300 and up, reply is
4631 * where Ps is the mode number as in DECSET, Pm is the mode value
4632 * as in the ANSI DECRQM.
4634 Terminal
.prototype.requestPrivateMode = function(params
) {
4641 * Set conformance level (DECSCL). Valid values for the first
4643 * Ps = 6 1 -> VT100.
4644 * Ps = 6 2 -> VT200.
4645 * Ps = 6 3 -> VT300.
4646 * Valid values for the second parameter:
4647 * Ps = 0 -> 8-bit controls.
4648 * Ps = 1 -> 7-bit controls (always set for VT100).
4649 * Ps = 2 -> 8-bit controls.
4651 Terminal
.prototype.setConformanceLevel = function(params
) {
4657 * CSI Ps q Load LEDs (DECLL).
4658 * Ps = 0 -> Clear all LEDS (default).
4659 * Ps = 1 -> Light Num Lock.
4660 * Ps = 2 -> Light Caps Lock.
4661 * Ps = 3 -> Light Scroll Lock.
4662 * Ps = 2 1 -> Extinguish Num Lock.
4663 * Ps = 2 2 -> Extinguish Caps Lock.
4664 * Ps = 2 3 -> Extinguish Scroll Lock.
4666 Terminal
.prototype.loadLEDs = function(params
) {
4673 * Set cursor style (DECSCUSR, VT520).
4674 * Ps = 0 -> blinking block.
4675 * Ps = 1 -> blinking block (default).
4676 * Ps = 2 -> steady block.
4677 * Ps = 3 -> blinking underline.
4678 * Ps = 4 -> steady underline.
4680 Terminal
.prototype.setCursorStyle = function(params
) {
4687 * Select character protection attribute (DECSCA). Valid values
4688 * for the parameter:
4689 * Ps = 0 -> DECSED and DECSEL can erase (default).
4690 * Ps = 1 -> DECSED and DECSEL cannot erase.
4691 * Ps = 2 -> DECSED and DECSEL can erase.
4693 Terminal
.prototype.setCharProtectionAttr = function(params
) {
4700 * Restore DEC Private Mode Values. The value of Ps previously
4701 * saved is restored. Ps values are the same as for DECSET.
4703 Terminal
.prototype.restorePrivateValues = function(params
) {
4709 * CSI Pt; Pl; Pb; Pr; Ps$ r
4710 * Change Attributes in Rectangular Area (DECCARA), VT400 and up.
4711 * Pt; Pl; Pb; Pr denotes the rectangle.
4712 * Ps denotes the SGR attributes to change: 0, 1, 4, 5, 7.
4713 * NOTE: xterm doesn't enable this code by default.
4715 Terminal
.prototype.setAttrInRectangle = function(params
) {
4725 for (; t
< b
+ 1; t
++) {
4726 line
= this.lines
.get(this.ybase
+ t
);
4727 for (i
= l
; i
< r
; i
++) {
4728 line
[i
] = [attr
, line
[i
][1]];
4733 this.updateRange(params
[0]);
4734 this.updateRange(params
[2]);
4739 * CSI Pc; Pt; Pl; Pb; Pr$ x
4740 * Fill Rectangular Area (DECFRA), VT420 and up.
4741 * Pc is the character to use.
4742 * Pt; Pl; Pb; Pr denotes the rectangle.
4743 * NOTE: xterm doesn't enable this code by default.
4745 Terminal
.prototype.fillRectangle = function(params
) {
4755 for (; t
< b
+ 1; t
++) {
4756 line
= this.lines
.get(this.ybase
+ t
);
4757 for (i
= l
; i
< r
; i
++) {
4758 line
[i
] = [line
[i
][0], String
.fromCharCode(ch
)];
4763 this.updateRange(params
[1]);
4764 this.updateRange(params
[3]);
4770 * Enable Locator Reporting (DECELR).
4771 * Valid values for the first parameter:
4772 * Ps = 0 -> Locator disabled (default).
4773 * Ps = 1 -> Locator enabled.
4774 * Ps = 2 -> Locator enabled for one report, then disabled.
4775 * The second parameter specifies the coordinate unit for locator
4777 * Valid values for the second parameter:
4778 * Pu = 0 <- or omitted -> default to character cells.
4779 * Pu = 1 <- device physical pixels.
4780 * Pu = 2 <- character cells.
4782 Terminal
.prototype.enableLocatorReporting = function(params
) {
4783 var val
= params
[0] > 0;
4784 //this.mouseEvents = val;
4785 //this.decLocator = val;
4790 * CSI Pt; Pl; Pb; Pr$ z
4791 * Erase Rectangular Area (DECERA), VT400 and up.
4792 * Pt; Pl; Pb; Pr denotes the rectangle.
4793 * NOTE: xterm doesn't enable this code by default.
4795 Terminal
.prototype.eraseRectangle = function(params
) {
4805 ch
= [this.eraseAttr(), ' ', 1]; // xterm?
4807 for (; t
< b
+ 1; t
++) {
4808 line
= this.lines
.get(this.ybase
+ t
);
4809 for (i
= l
; i
< r
; i
++) {
4815 this.updateRange(params
[0]);
4816 this.updateRange(params
[2]);
4822 * Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
4823 * NOTE: xterm doesn't enable this code by default.
4825 Terminal
.prototype.insertColumns = function() {
4826 var param
= params
[0]
4827 , l
= this.ybase
+ this.rows
4828 , ch
= [this.eraseAttr(), ' ', 1] // xterm?
4832 for (i
= this.ybase
; i
< l
; i
++) {
4833 this.lines
.get(i
).splice(this.x
+ 1, 0, ch
);
4834 this.lines
.get(i
).pop();
4844 * Delete P s Column(s) (default = 1) (DECDC), VT420 and up
4845 * NOTE: xterm doesn't enable this code by default.
4847 Terminal
.prototype.deleteColumns = function() {
4848 var param
= params
[0]
4849 , l
= this.ybase
+ this.rows
4850 , ch
= [this.eraseAttr(), ' ', 1] // xterm?
4854 for (i
= this.ybase
; i
< l
; i
++) {
4855 this.lines
.get(i
).splice(this.x
, 1);
4856 this.lines
.get(i
).push(ch
);
4867 Terminal
.charsets
= {};
4869 // DEC Special Character and Line Drawing Set.
4870 // http://vt100.net/docs/vt102-ug/table5-13.html
4871 // A lot of curses apps use this if they see TERM=xterm.
4872 // testing: echo -e '\e(0a\e(B'
4873 // The xterm output sometimes seems to conflict with the
4874 // reference above. xterm seems in line with the reference
4875 // when running vttest however.
4876 // The table below now uses xterm's output from vttest.
4877 Terminal
.charsets
.SCLD
= { // (0
4878 '`': '\u25c6', // '◆'
4879 'a': '\u2592', // '▒'
4880 'b': '\u0009', // '\t'
4881 'c': '\u000c', // '\f'
4882 'd': '\u000d', // '\r'
4883 'e': '\u000a', // '\n'
4884 'f': '\u00b0', // '°'
4885 'g': '\u00b1', // '±'
4886 'h': '\u2424', // '\u2424' (NL)
4887 'i': '\u000b', // '\v'
4888 'j': '\u2518', // '┘'
4889 'k': '\u2510', // '┐'
4890 'l': '\u250c', // '┌'
4891 'm': '\u2514', // '└'
4892 'n': '\u253c', // '┼'
4893 'o': '\u23ba', // '⎺'
4894 'p': '\u23bb', // '⎻'
4895 'q': '\u2500', // '─'
4896 'r': '\u23bc', // '⎼'
4897 's': '\u23bd', // '⎽'
4898 't': '\u251c', // '├'
4899 'u': '\u2524', // '┤'
4900 'v': '\u2534', // '┴'
4901 'w': '\u252c', // '┬'
4902 'x': '\u2502', // '│'
4903 'y': '\u2264', // '≤'
4904 'z': '\u2265', // '≥'
4905 '{': '\u03c0', // 'π'
4906 '|': '\u2260', // '≠'
4907 '}': '\u00a3', // '£'
4908 '~': '\u00b7' // '·'
4911 Terminal
.charsets
.UK
= null; // (A
4912 Terminal
.charsets
.US
= null; // (B (USASCII)
4913 Terminal
.charsets
.Dutch
= null; // (4
4914 Terminal
.charsets
.Finnish
= null; // (C or (5
4915 Terminal
.charsets
.French
= null; // (R
4916 Terminal
.charsets
.FrenchCanadian
= null; // (Q
4917 Terminal
.charsets
.German
= null; // (K
4918 Terminal
.charsets
.Italian
= null; // (Y
4919 Terminal
.charsets
.NorwegianDanish
= null; // (E or (6
4920 Terminal
.charsets
.Spanish
= null; // (Z
4921 Terminal
.charsets
.Swedish
= null; // (H or (7
4922 Terminal
.charsets
.Swiss
= null; // (=
4923 Terminal
.charsets
.ISOLatin
= null; // /A
4929 function on(el
, type
, handler
, capture
) {
4930 if (!Array
.isArray(el
)) {
4933 el
.forEach(function (element
) {
4934 element
.addEventListener(type
, handler
, capture
|| false);
4938 function off(el
, type
, handler
, capture
) {
4939 el
.removeEventListener(type
, handler
, capture
|| false);
4942 function cancel(ev
, force
) {
4943 if (!this.cancelEvents
&& !force
) {
4946 ev
.preventDefault();
4947 ev
.stopPropagation();
4951 function inherits(child
, parent
) {
4953 this.constructor = child
;
4955 f
.prototype = parent
.prototype;
4956 child
.prototype = new f
;
4959 // if bold is broken, we can't
4960 // use it in the terminal.
4961 function isBoldBroken(document
) {
4962 var body
= document
.getElementsByTagName('body')[0];
4963 var el
= document
.createElement('span');
4964 el
.innerHTML
= 'hello world';
4965 body
.appendChild(el
);
4966 var w1
= el
.scrollWidth
;
4967 el
.style
.fontWeight
= 'bold';
4968 var w2
= el
.scrollWidth
;
4969 body
.removeChild(el
);
4973 function indexOf(obj
, el
) {
4976 if (obj
[i
] === el
) return i
;
4981 function isThirdLevelShift(term
, ev
) {
4983 (term
.browser
.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) ||
4984 (term
.browser
.isMSWindows
&& ev
.altKey
&& ev
.ctrlKey
&& !ev
.metaKey
);
4986 if (ev
.type
== 'keypress') {
4987 return thirdLevelKey
;
4990 // Don't invoke for arrows, pageDown, home, backspace, etc. (on non-keypress events)
4991 return thirdLevelKey
&& (!ev
.keyCode
|| ev
.keyCode
> 47);
4994 function matchColor(r1
, g1
, b1
) {
4995 var hash
= (r1
<< 16) | (g1
<< 8) | b1
;
4997 if (matchColor
._cache
[hash
] != null) {
4998 return matchColor
._cache
[hash
];
5001 var ldiff
= Infinity
5010 for (; i
< Terminal
.vcolors
.length
; i
++) {
5011 c
= Terminal
.vcolors
[i
];
5016 diff
= matchColor
.distance(r1
, g1
, b1
, r2
, g2
, b2
);
5029 return matchColor
._cache
[hash
] = li
;
5032 matchColor
._cache
= {};
5034 // http://stackoverflow.com/questions/1633828
5035 matchColor
.distance = function(r1
, g1
, b1
, r2
, g2
, b2
) {
5036 return Math
.pow(30 * (r1
- r2
), 2)
5037 + Math
.pow(59 * (g1
- g2
), 2)
5038 + Math
.pow(11 * (b1
- b2
), 2);
5041 function each(obj
, iter
, con
) {
5042 if (obj
.forEach
) return obj
.forEach(iter
, con
);
5043 for (var i
= 0; i
< obj
.length
; i
++) {
5044 iter
.call(con
, obj
[i
], i
, obj
);
5048 function keys(obj
) {
5049 if (Object
.keys
) return Object
.keys(obj
);
5052 if (Object
.prototype.hasOwnProperty
.call(obj
, key
)) {
5059 var wcwidth
= (function(opts
) {
5060 // extracted from https://www.cl.cam.ac.uk/%7Emgk25/ucs/wcwidth.c
5061 // combining characters
5063 [0x0300, 0x036F], [0x0483, 0x0486], [0x0488, 0x0489],
5064 [0x0591, 0x05BD], [0x05BF, 0x05BF], [0x05C1, 0x05C2],
5065 [0x05C4, 0x05C5], [0x05C7, 0x05C7], [0x0600, 0x0603],
5066 [0x0610, 0x0615], [0x064B, 0x065E], [0x0670, 0x0670],
5067 [0x06D6, 0x06E4], [0x06E7, 0x06E8], [0x06EA, 0x06ED],
5068 [0x070F, 0x070F], [0x0711, 0x0711], [0x0730, 0x074A],
5069 [0x07A6, 0x07B0], [0x07EB, 0x07F3], [0x0901, 0x0902],
5070 [0x093C, 0x093C], [0x0941, 0x0948], [0x094D, 0x094D],
5071 [0x0951, 0x0954], [0x0962, 0x0963], [0x0981, 0x0981],
5072 [0x09BC, 0x09BC], [0x09C1, 0x09C4], [0x09CD, 0x09CD],
5073 [0x09E2, 0x09E3], [0x0A01, 0x0A02], [0x0A3C, 0x0A3C],
5074 [0x0A41, 0x0A42], [0x0A47, 0x0A48], [0x0A4B, 0x0A4D],
5075 [0x0A70, 0x0A71], [0x0A81, 0x0A82], [0x0ABC, 0x0ABC],
5076 [0x0AC1, 0x0AC5], [0x0AC7, 0x0AC8], [0x0ACD, 0x0ACD],
5077 [0x0AE2, 0x0AE3], [0x0B01, 0x0B01], [0x0B3C, 0x0B3C],
5078 [0x0B3F, 0x0B3F], [0x0B41, 0x0B43], [0x0B4D, 0x0B4D],
5079 [0x0B56, 0x0B56], [0x0B82, 0x0B82], [0x0BC0, 0x0BC0],
5080 [0x0BCD, 0x0BCD], [0x0C3E, 0x0C40], [0x0C46, 0x0C48],
5081 [0x0C4A, 0x0C4D], [0x0C55, 0x0C56], [0x0CBC, 0x0CBC],
5082 [0x0CBF, 0x0CBF], [0x0CC6, 0x0CC6], [0x0CCC, 0x0CCD],
5083 [0x0CE2, 0x0CE3], [0x0D41, 0x0D43], [0x0D4D, 0x0D4D],
5084 [0x0DCA, 0x0DCA], [0x0DD2, 0x0DD4], [0x0DD6, 0x0DD6],
5085 [0x0E31, 0x0E31], [0x0E34, 0x0E3A], [0x0E47, 0x0E4E],
5086 [0x0EB1, 0x0EB1], [0x0EB4, 0x0EB9], [0x0EBB, 0x0EBC],
5087 [0x0EC8, 0x0ECD], [0x0F18, 0x0F19], [0x0F35, 0x0F35],
5088 [0x0F37, 0x0F37], [0x0F39, 0x0F39], [0x0F71, 0x0F7E],
5089 [0x0F80, 0x0F84], [0x0F86, 0x0F87], [0x0F90, 0x0F97],
5090 [0x0F99, 0x0FBC], [0x0FC6, 0x0FC6], [0x102D, 0x1030],
5091 [0x1032, 0x1032], [0x1036, 0x1037], [0x1039, 0x1039],
5092 [0x1058, 0x1059], [0x1160, 0x11FF], [0x135F, 0x135F],
5093 [0x1712, 0x1714], [0x1732, 0x1734], [0x1752, 0x1753],
5094 [0x1772, 0x1773], [0x17B4, 0x17B5], [0x17B7, 0x17BD],
5095 [0x17C6, 0x17C6], [0x17C9, 0x17D3], [0x17DD, 0x17DD],
5096 [0x180B, 0x180D], [0x18A9, 0x18A9], [0x1920, 0x1922],
5097 [0x1927, 0x1928], [0x1932, 0x1932], [0x1939, 0x193B],
5098 [0x1A17, 0x1A18], [0x1B00, 0x1B03], [0x1B34, 0x1B34],
5099 [0x1B36, 0x1B3A], [0x1B3C, 0x1B3C], [0x1B42, 0x1B42],
5100 [0x1B6B, 0x1B73], [0x1DC0, 0x1DCA], [0x1DFE, 0x1DFF],
5101 [0x200B, 0x200F], [0x202A, 0x202E], [0x2060, 0x2063],
5102 [0x206A, 0x206F], [0x20D0, 0x20EF], [0x302A, 0x302F],
5103 [0x3099, 0x309A], [0xA806, 0xA806], [0xA80B, 0xA80B],
5104 [0xA825, 0xA826], [0xFB1E, 0xFB1E], [0xFE00, 0xFE0F],
5105 [0xFE20, 0xFE23], [0xFEFF, 0xFEFF], [0xFFF9, 0xFFFB],
5106 [0x10A01, 0x10A03], [0x10A05, 0x10A06], [0x10A0C, 0x10A0F],
5107 [0x10A38, 0x10A3A], [0x10A3F, 0x10A3F], [0x1D167, 0x1D169],
5108 [0x1D173, 0x1D182], [0x1D185, 0x1D18B], [0x1D1AA, 0x1D1AD],
5109 [0x1D242, 0x1D244], [0xE0001, 0xE0001], [0xE0020, 0xE007F],
5113 function bisearch(ucs
) {
5115 var max
= COMBINING
.length
- 1;
5117 if (ucs
< COMBINING
[0][0] || ucs
> COMBINING
[max
][1])
5119 while (max
>= min
) {
5120 mid
= Math
.floor((min
+ max
) / 2);
5121 if (ucs
> COMBINING
[mid
][1])
5123 else if (ucs
< COMBINING
[mid
][0])
5130 function wcwidth(ucs
) {
5131 // test for 8-bit control characters
5134 if (ucs
< 32 || (ucs
>= 0x7f && ucs
< 0xa0))
5135 return opts
.control
;
5136 // binary search in table of non-spacing characters
5139 // if we arrive here, ucs is not a combining or C0/C1 control character
5144 ucs
<= 0x115f || // Hangul Jamo init. consonants
5147 (ucs
>= 0x2e80 && ucs
<= 0xa4cf && ucs
!= 0x303f) || // CJK..Yi
5148 (ucs
>= 0xac00 && ucs
<= 0xd7a3) || // Hangul Syllables
5149 (ucs
>= 0xf900 && ucs
<= 0xfaff) || // CJK Compat Ideographs
5150 (ucs
>= 0xfe10 && ucs
<= 0xfe19) || // Vertical forms
5151 (ucs
>= 0xfe30 && ucs
<= 0xfe6f) || // CJK Compat Forms
5152 (ucs
>= 0xff00 && ucs
<= 0xff60) || // Fullwidth Forms
5153 (ucs
>= 0xffe0 && ucs
<= 0xffe6) ||
5154 (ucs
>= 0x20000 && ucs
<= 0x2fffd) ||
5155 (ucs
>= 0x30000 && ucs
<= 0x3fffd)
5160 })({nul
: 0, control
: 0}); // configurable options
5166 Terminal
.EventEmitter
= EventEmitter
;
5167 Terminal
.inherits
= inherits
;
5170 * Adds an event listener to the terminal.
5172 * @param {string} event The name of the event. TODO: Document all event types
5173 * @param {function} callback The function to call when the event is triggered.
5177 Terminal
.cancel
= cancel
;
5179 module
.exports
= Terminal
;