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 { C0
} from './EscapeSequences';
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
142 * Used to debounce the refresh function
144 this.isRefreshing
= false;
147 * Whether there is a full terminal refresh queued
150 this.cursorState
= 0;
151 this.cursorHidden
= false;
156 this.scrollBottom
= this.rows
- 1;
157 this.customKeydownHandler
= null;
160 this.applicationKeypad
= false;
161 this.applicationCursor
= false;
162 this.originMode
= false;
163 this.insertMode
= false;
164 this.wraparoundMode
= true; // defaults: xterm - true, vt100 - false
169 this.gcharset
= null;
171 this.charsets
= [null];
195 this.readable
= true;
196 this.writable
= true;
198 this.defAttr
= (0 << 18) | (257 << 9) | (256 << 0);
199 this.curAttr
= this.defAttr
;
202 this.currentParam
= 0;
206 // leftover surrogate high from previous write invocation
207 this.surrogate_high
= '';
210 * An array of all lines in the entire buffer, including the prompt. The lines are array of
211 * characters which are 2-length arrays where [0] is an attribute and [1] is the character.
213 this.lines
= new CircularList(this.scrollback
);
216 this.lines
.push(this.blankLine());
222 // Store if user went browsing history in scrollback
223 this.userScrolling
= false;
226 inherits(Terminal
, EventEmitter
);
229 * back_color_erase feature for xterm.
231 Terminal
.prototype.eraseAttr = function() {
232 // if (this.is('screen')) return this.defAttr;
233 return (this.defAttr
& ~0x1ff) | (this.curAttr
& 0x1ff);
241 Terminal
.tangoColors
= [
262 // Colors 0-15 + 16-255
263 // Much thanks to TooTallNate for writing this.
264 Terminal
.colors
= (function() {
265 var colors
= Terminal
.tangoColors
.slice()
266 , r
= [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff]
271 for (; i
< 216; i
++) {
272 out(r
[(i
/ 36) % 6 | 0], r
[(i
/ 6) % 6 | 0], r
[i
% 6]);
277 for (; i
< 24; i
++) {
282 function out(r
, g
, b
) {
283 colors
.push('#' + hex(r
) + hex(g
) + hex(b
));
288 return c
.length
< 2 ? '0' + c
: c
;
294 Terminal
._colors
= Terminal
.colors
.slice();
296 Terminal
.vcolors
= (function() {
298 , colors
= Terminal
.colors
302 for (; i
< 256; i
++) {
303 color
= parseInt(colors
[i
].substring(1), 16);
305 (color
>> 16) & 0xff,
318 Terminal
.defaults
= {
319 colors
: Terminal
.colors
,
331 // programFeatures: false,
335 Terminal
.options
= {};
337 Terminal
.focus
= null;
339 each(keys(Terminal
.defaults
), function(key
) {
340 Terminal
[key
] = Terminal
.defaults
[key
];
341 Terminal
.options
[key
] = Terminal
.defaults
[key
];
345 * Focus the terminal. Delegates focus handling to the terminal's DOM element.
347 Terminal
.prototype.focus = function() {
348 return this.textarea
.focus();
352 * Retrieves an option's value from the terminal.
353 * @param {string} key The option key.
355 Terminal
.prototype.getOption = function(key
, value
) {
356 if (!(key
in Terminal
.defaults
)) {
357 throw new Error('No option with key "' + key
+ '"');
360 if (typeof this.options
[key
] !== 'undefined') {
361 return this.options
[key
];
368 * Sets an option on the terminal.
369 * @param {string} key The option key.
370 * @param {string} value The option value.
372 Terminal
.prototype.setOption = function(key
, value
) {
373 if (!(key
in Terminal
.defaults
)) {
374 throw new Error('No option with key "' + key
+ '"');
377 this.options
[key
] = value
;
381 * Binds the desired focus behavior on a given terminal object.
385 Terminal
.bindFocus = function (term
) {
386 on(term
.textarea
, 'focus', function (ev
) {
387 if (term
.sendFocus
) {
388 term
.send(C0
.ESC
+ '[I');
390 term
.element
.classList
.add('focus');
392 Terminal
.focus
= term
;
393 term
.emit('focus', {terminal
: term
});
398 * Blur the terminal. Delegates blur handling to the terminal's DOM element.
400 Terminal
.prototype.blur = function() {
401 return this.textarea
.blur();
405 * Binds the desired blur behavior on a given terminal object.
409 Terminal
.bindBlur = function (term
) {
410 on(term
.textarea
, 'blur', function (ev
) {
411 term
.refresh(term
.y
, term
.y
);
412 if (term
.sendFocus
) {
413 term
.send(C0
.ESC
+ '[O');
415 term
.element
.classList
.remove('focus');
416 Terminal
.focus
= null;
417 term
.emit('blur', {terminal
: term
});
422 * Initialize default behavior
424 Terminal
.prototype.initGlobal = function() {
427 Terminal
.bindKeys(this);
428 Terminal
.bindFocus(this);
429 Terminal
.bindBlur(this);
431 // Bind clipboard functionality
432 on(this.element
, 'copy', function (ev
) {
433 copyHandler
.call(this, ev
, term
);
435 on(this.textarea
, 'paste', function (ev
) {
436 pasteHandler
.call(this, ev
, term
);
438 on(this.element
, 'paste', function (ev
) {
439 pasteHandler
.call(this, ev
, term
);
442 function rightClickHandlerWrapper (ev
) {
443 rightClickHandler
.call(this, ev
, term
);
446 if (term
.browser
.isFirefox
) {
447 on(this.element
, 'mousedown', function (ev
) {
448 if (ev
.button
== 2) {
449 rightClickHandlerWrapper(ev
);
453 on(this.element
, 'contextmenu', rightClickHandlerWrapper
);
458 * Apply key handling to the terminal
460 Terminal
.bindKeys = function(term
) {
461 on(term
.element
, 'keydown', function(ev
) {
462 if (document
.activeElement
!= this) {
468 on(term
.element
, 'keypress', function(ev
) {
469 if (document
.activeElement
!= this) {
475 on(term
.element
, 'keyup', term
.focus
.bind(term
));
477 on(term
.textarea
, 'keydown', function(ev
) {
481 on(term
.textarea
, 'keypress', function(ev
) {
483 // Truncate the textarea's value, since it is not needed
487 on(term
.textarea
, 'compositionstart', term
.compositionHelper
.compositionstart
.bind(term
.compositionHelper
));
488 on(term
.textarea
, 'compositionupdate', term
.compositionHelper
.compositionupdate
.bind(term
.compositionHelper
));
489 on(term
.textarea
, 'compositionend', term
.compositionHelper
.compositionend
.bind(term
.compositionHelper
));
490 term
.on('refresh', term
.compositionHelper
.updateCompositionElements
.bind(term
.compositionHelper
));
495 * Insert the given row to the terminal or produce a new one
496 * if no row argument is passed. Return the inserted row.
497 * @param {HTMLElement} row (optional) The row to append to the terminal.
499 Terminal
.prototype.insertRow = function (row
) {
500 if (typeof row
!= 'object') {
501 row
= document
.createElement('div');
504 this.rowContainer
.appendChild(row
);
505 this.children
.push(row
);
511 * Opens the terminal within an element.
513 * @param {HTMLElement} parent The element to create the terminal within.
515 Terminal
.prototype.open = function(parent
) {
516 var self
=this, i
=0, div
;
518 this.parent
= parent
|| this.parent
;
521 throw new Error('Terminal requires a parent element.');
524 // Grab global elements
525 this.context
= this.parent
.ownerDocument
.defaultView
;
526 this.document
= this.parent
.ownerDocument
;
527 this.body
= this.document
.getElementsByTagName('body')[0];
529 //Create main element container
530 this.element
= this.document
.createElement('div');
531 this.element
.classList
.add('terminal');
532 this.element
.classList
.add('xterm');
533 this.element
.classList
.add('xterm-theme-' + this.theme
);
535 this.element
.style
.height
536 this.element
.setAttribute('tabindex', 0);
538 this.viewportElement
= document
.createElement('div');
539 this.viewportElement
.classList
.add('xterm-viewport');
540 this.element
.appendChild(this.viewportElement
);
541 this.viewportScrollArea
= document
.createElement('div');
542 this.viewportScrollArea
.classList
.add('xterm-scroll-area');
543 this.viewportElement
.appendChild(this.viewportScrollArea
);
545 // Create the container that will hold the lines of the terminal and then
546 // produce the lines the lines.
547 this.rowContainer
= document
.createElement('div');
548 this.rowContainer
.classList
.add('xterm-rows');
549 this.element
.appendChild(this.rowContainer
);
552 // Create the container that will hold helpers like the textarea for
553 // capturing DOM Events. Then produce the helpers.
554 this.helperContainer
= document
.createElement('div');
555 this.helperContainer
.classList
.add('xterm-helpers');
556 // TODO: This should probably be inserted once it's filled to prevent an additional layout
557 this.element
.appendChild(this.helperContainer
);
558 this.textarea
= document
.createElement('textarea');
559 this.textarea
.classList
.add('xterm-helper-textarea');
560 this.textarea
.setAttribute('autocorrect', 'off');
561 this.textarea
.setAttribute('autocapitalize', 'off');
562 this.textarea
.setAttribute('spellcheck', 'false');
563 this.textarea
.tabIndex
= 0;
564 this.textarea
.addEventListener('focus', function() {
565 self
.emit('focus', {terminal
: self
});
567 this.textarea
.addEventListener('blur', function() {
568 self
.emit('blur', {terminal
: self
});
570 this.helperContainer
.appendChild(this.textarea
);
572 this.compositionView
= document
.createElement('div');
573 this.compositionView
.classList
.add('composition-view');
574 this.compositionHelper
= new CompositionHelper(this.textarea
, this.compositionView
, this);
575 this.helperContainer
.appendChild(this.compositionView
);
577 this.charMeasureElement
= document
.createElement('div');
578 this.charMeasureElement
.classList
.add('xterm-char-measure-element');
579 this.charMeasureElement
.innerHTML
= 'W';
580 this.helperContainer
.appendChild(this.charMeasureElement
);
582 for (; i
< this.rows
; i
++) {
585 this.parent
.appendChild(this.element
);
587 this.viewport
= new Viewport(this, this.viewportElement
, this.viewportScrollArea
, this.charMeasureElement
);
590 this.refresh(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.');
648 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#Mouse%20Tracking
649 * To better understand these
650 * the xterm code is very helpful:
652 * button.c, charproc.c, misc.c
653 * Relevant functions in xterm/button.c:
654 * BtnCode, EmitButtonCode, EditorButton, SendMousePosition
656 Terminal
.prototype.bindMouse = function() {
657 var el
= this.element
, self
= this, pressed
= 32;
659 // mouseup, mousedown, wheel
660 // left click: ^[[M 3<^[[M#3<
662 function sendButton(ev
) {
666 // get the xterm-style button
667 button
= getButton(ev
);
669 // get mouse coordinates
673 sendEvent(button
, pos
);
675 switch (ev
.overrideType
|| ev
.type
) {
680 // keep it at the left
681 // button, just in case.
692 // motion example of a left click:
693 // ^[[M 3<^[[M@4<^[[M@5<^[[M@6<^[[M@7<^[[M#7<
694 function sendMove(ev
) {
701 // buttons marked as motions
702 // are incremented by 32
705 sendEvent(button
, pos
);
709 // position to characters
710 function encode(data
, ch
) {
711 if (!self
.utfMouse
) {
712 if (ch
=== 255) return data
.push(0);
713 if (ch
> 127) ch
= 127;
716 if (ch
=== 2047) return data
.push(0);
720 if (ch
> 2047) ch
= 2047;
721 data
.push(0xC0 | (ch
>> 6));
722 data
.push(0x80 | (ch
& 0x3F));
727 // send a mouse event:
728 // regular/utf8: ^[[M Cb Cx Cy
729 // urxvt: ^[[ Cb ; Cx ; Cy M
730 // sgr: ^[[ Cb ; Cx ; Cy M/m
731 // vt300: ^[[ 24(1/3/5)~ [ Cx , Cy ] \r
732 // locator: CSI P e ; P b ; P r ; P c ; P p & w
733 function sendEvent(button
, pos
) {
734 // self.emit('mouse', {
740 if (self
.vt300Mouse
) {
742 // http://www.vt100.net/docs/vt3xx-gp/chapter15.html
746 var data
= C0
.ESC
+ '[24';
747 if (button
=== 0) data
+= '1';
748 else if (button
=== 1) data
+= '3';
749 else if (button
=== 2) data
+= '5';
750 else if (button
=== 3) return;
752 data
+= '~[' + pos
.x
+ ',' + pos
.y
+ ']\r';
757 if (self
.decLocator
) {
762 if (button
=== 0) button
= 2;
763 else if (button
=== 1) button
= 4;
764 else if (button
=== 2) button
= 6;
765 else if (button
=== 3) button
= 3;
766 self
.send(C0
.ESC
+ '['
769 + (button
=== 3 ? 4 : 0)
780 if (self
.urxvtMouse
) {
785 self
.send(C0
.ESC
+ '[' + button
+ ';' + pos
.x
+ ';' + pos
.y
+ 'M');
792 self
.send(C0
.ESC
+ '[<'
793 + (((button
& 3) === 3 ? button
& ~3 : button
) - 32)
798 + ((button
& 3) === 3 ? 'm' : 'M'));
804 encode(data
, button
);
808 self
.send(C0
.ESC
+ '[M' + String
.fromCharCode
.apply(String
, data
));
811 function getButton(ev
) {
824 // 1, and 2 - with 64 added
825 switch (ev
.overrideType
|| ev
.type
) {
827 button
= ev
.button
!= null
833 if (self
.browser
.isMSIE
) {
834 button
= button
=== 1 ? 0 : button
=== 4 ? 1 : button
;
840 case 'DOMMouseScroll':
841 button
= ev
.detail
< 0
846 button
= ev
.wheelDeltaY
> 0
852 // next three bits are the modifiers:
853 // 4 = shift, 8 = meta, 16 = control
854 shift
= ev
.shiftKey
? 4 : 0;
855 meta
= ev
.metaKey
? 8 : 0;
856 ctrl
= ev
.ctrlKey
? 16 : 0;
857 mod
= shift
| meta
| ctrl
;
860 if (self
.vt200Mouse
) {
863 } else if (!self
.normalMouse
) {
868 button
= (32 + (mod
<< 2)) + button
;
873 // mouse coordinates measured in cols/rows
874 function getCoords(ev
) {
877 // ignore browsers without pageX for now
878 if (ev
.pageX
== null) return;
884 // should probably check offsetParent
885 // but this is more portable
886 while (el
&& el
!== self
.document
.documentElement
) {
889 el
= 'offsetParent' in el
894 // convert to cols/rows
895 w
= self
.element
.clientWidth
;
896 h
= self
.element
.clientHeight
;
897 x
= Math
.ceil((x
/ w
) * self
.cols
);
898 y
= Math
.ceil((y
/ h
) * self
.rows
);
900 // be sure to avoid sending
901 // bad positions to the program
903 if (x
> self
.cols
) x
= self
.cols
;
905 if (y
> self
.rows
) y
= self
.rows
;
907 // xterm sends raw bytes and
908 // starts at 32 (SP) for each.
919 on(el
, 'mousedown', function(ev
) {
920 if (!self
.mouseEvents
) return;
929 //if (self.vt200Mouse && !self.normalMouse) {
930 if (self
.vt200Mouse
) {
931 ev
.overrideType
= 'mouseup';
933 return self
.cancel(ev
);
937 if (self
.normalMouse
) on(self
.document
, 'mousemove', sendMove
);
939 // x10 compatibility mode can't send button releases
940 if (!self
.x10Mouse
) {
941 on(self
.document
, 'mouseup', function up(ev
) {
943 if (self
.normalMouse
) off(self
.document
, 'mousemove', sendMove
);
944 off(self
.document
, 'mouseup', up
);
945 return self
.cancel(ev
);
949 return self
.cancel(ev
);
952 //if (self.normalMouse) {
953 // on(self.document, 'mousemove', sendMove);
956 on(el
, 'wheel', function(ev
) {
957 if (!self
.mouseEvents
) return;
960 || self
.decLocator
) return;
962 return self
.cancel(ev
);
965 // allow wheel scrolling in
966 // the shell for example
967 on(el
, 'wheel', function(ev
) {
968 if (self
.mouseEvents
) return;
969 self
.viewport
.onWheel(ev
);
970 return self
.cancel(ev
);
975 * Destroys the terminal.
977 Terminal
.prototype.destroy = function() {
978 this.readable
= false;
979 this.writable
= false;
981 this.handler = function() {};
982 this.write = function() {};
983 if (this.element
.parentNode
) {
984 this.element
.parentNode
.removeChild(this.element
);
986 //this.emit('close');
991 * Flags used to render terminal text properly
1002 * Refreshes (re-renders) terminal content within two rows (inclusive)
1006 * In the screen buffer, each character is stored as a an array with a character
1007 * and a 32-bit integer:
1008 * - First value: a utf-16 character.
1010 * - Next 9 bits: background color (0-511).
1011 * - Next 9 bits: foreground color (0-511).
1012 * - Next 14 bits: a mask for misc. flags:
1019 * @param {number} start The row to start from (between 0 and terminal's height terminal - 1)
1020 * @param {number} end The row to end at (between fromRow and terminal's height terminal - 1)
1021 * @param {boolean} queue Whether the refresh should ran right now or be queued
1023 Terminal
.prototype.refresh = function(start
, end
, queue
) {
1026 // queue defaults to true
1027 queue
= (typeof queue
== 'undefined') ? true : queue
;
1030 * The refresh queue allows refresh to execute only approximately 30 times a second. For
1031 * commands that pass a significant amount of output to the write function, this prevents the
1032 * terminal from maxing out the CPU and making the UI unresponsive. While commands can still
1033 * run beyond what they do on the terminal, it is far better with a debounce in place as
1034 * every single terminal manipulation does not need to be constructed in the DOM.
1036 * A side-effect of this is that it makes ^C to interrupt a process seem more responsive.
1039 // If refresh should be queued, order the refresh and return.
1040 if (this._refreshIsQueued
) {
1041 // If a refresh has already been queued, just order a full refresh next
1042 this._fullRefreshNext
= true;
1044 setTimeout(function () {
1045 self
.refresh(start
, end
, false);
1047 this._refreshIsQueued
= true;
1052 // If refresh should be run right now (not be queued), release the lock
1053 this._refreshIsQueued
= false;
1055 // If multiple refreshes were requested, make a full refresh.
1056 if (this._fullRefreshNext
) {
1058 end
= this.rows
- 1;
1059 this._fullRefreshNext
= false // reset lock
1062 var x
, y
, i
, line
, out
, ch
, ch_width
, width
, data
, attr
, bg
, fg
, flags
, row
, parent
, focused
= document
.activeElement
;
1064 // If this is a big refresh, remove the terminal rows from the DOM for faster calculations
1065 if (end
- start
>= this.rows
/ 2) {
1066 parent
= this.element
.parentNode
;
1068 this.element
.removeChild(this.rowContainer
);
1075 if (end
>= this.rows
.length
) {
1076 this.log('`end` is too large. Most likely a bad CSR.');
1077 end
= this.rows
.length
- 1;
1080 for (; y
<= end
; y
++) {
1081 row
= y
+ this.ydisp
;
1083 line
= this.lines
.get(row
);
1086 if (this.y
=== y
- (this.ybase
- this.ydisp
)
1088 && !this.cursorHidden
) {
1094 attr
= this.defAttr
;
1097 for (; i
< width
; i
++) {
1100 ch_width
= line
[i
][2];
1104 if (i
=== x
) data
= -1;
1106 if (data
!== attr
) {
1107 if (attr
!== this.defAttr
) {
1110 if (data
!== this.defAttr
) {
1112 out
+= '<span class="reverse-video terminal-cursor';
1113 if (this.cursorBlink
) {
1118 var classNames
= [];
1121 fg
= (data
>> 9) & 0x1ff;
1124 if (flags
& Terminal
.flags
.BOLD
) {
1125 if (!Terminal
.brokenBold
) {
1126 classNames
.push('xterm-bold');
1128 // See: XTerm*boldColors
1129 if (fg
< 8) fg
+= 8;
1132 if (flags
& Terminal
.flags
.UNDERLINE
) {
1133 classNames
.push('xterm-underline');
1136 if (flags
& Terminal
.flags
.BLINK
) {
1137 classNames
.push('xterm-blink');
1140 // If inverse flag is on, then swap the foreground and background variables.
1141 if (flags
& Terminal
.flags
.INVERSE
) {
1142 /* One-line variable swap in JavaScript: http://stackoverflow.com/a/16201730 */
1143 bg = [fg, fg = bg][0];
1144 // Should inverse just be before the
1145 // above boldColors effect instead?
1146 if ((flags & 1) && fg < 8) fg += 8;
1149 if (flags & Terminal.flags.INVISIBLE) {
1150 classNames.push('xterm-hidden');
1154 * Weird situation: Invert flag used black foreground and white background results
1155 * in invalid background color, positioned at the 256 index of the 256 terminal
1156 * color map. Pin the colors manually in such a case.
1158 * Source: https://github.com/sourcelair/xterm.js/issues/57
1160 if (flags
& Terminal
.flags
.INVERSE
) {
1170 classNames
.push('xterm-bg-color-' + bg
);
1174 classNames
.push('xterm-color-' + fg
);
1178 if (classNames
.length
) {
1179 out
+= ' class="' + classNames
.join(' ') + '"';
1208 if (attr
!== this.defAttr
) {
1212 this.children
[y
].innerHTML
= out
;
1216 this.element
.appendChild(this.rowContainer
);
1219 this.emit('refresh', {element
: this.element
, start
: start
, end
: end
});
1223 * Display the cursor element
1225 Terminal
.prototype.showCursor = function() {
1226 if (!this.cursorState
) {
1227 this.cursorState
= 1;
1228 this.refresh(this.y
, this.y
);
1233 * Scroll the terminal down 1 row, creating a blank line.
1235 Terminal
.prototype.scroll = function() {
1240 // TODO: Why is this done twice?
1241 if (!this.userScrolling
) {
1242 this.ydisp
= this.ybase
;
1246 row
= this.ybase
+ this.rows
- 1;
1248 // subtract the bottom scroll region
1249 row
-= this.rows
- 1 - this.scrollBottom
;
1251 if (row
=== this.lines
.length
) {
1252 // Compensate ybase and ydisp if lines has hit the maximum buffer size
1253 if (this.lines
.length
=== this.lines
.maxLength
) {
1255 if (this.ydisp
!== 0) {
1259 // Optimization: pushing is faster than splicing when they amount to the same behavior
1260 this.lines
.push(this.blankLine());
1263 this.lines
.splice(row
, 0, this.blankLine());
1266 if (this.scrollTop
!== 0) {
1267 if (this.ybase
!== 0) {
1269 if (!this.userScrolling
) {
1270 this.ydisp
= this.ybase
;
1273 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
1277 this.updateRange(this.scrollTop
);
1278 this.updateRange(this.scrollBottom
);
1281 * This event is emitted whenever the terminal is scrolled.
1282 * The one parameter passed is the new y display position.
1286 this.emit('scroll', this.ydisp
);
1290 * Scroll the display of the terminal
1291 * @param {number} disp The number of lines to scroll down (negatives scroll up).
1292 * @param {boolean} suppressScrollEvent Don't emit the scroll event as scrollDisp. This is used
1293 * to avoid unwanted events being handled by the veiwport when the event was triggered from the
1294 * viewport originally.
1296 Terminal
.prototype.scrollDisp = function(disp
, suppressScrollEvent
) {
1298 this.userScrolling
= true;
1299 } else if (disp
+ this.ydisp
>= this.ybase
) {
1300 this.userScrolling
= false;
1305 if (this.ydisp
> this.ybase
) {
1306 this.ydisp
= this.ybase
;
1307 } else if (this.ydisp
< 0) {
1311 if (!suppressScrollEvent
) {
1312 this.emit('scroll', this.ydisp
);
1315 this.refresh(0, this.rows
- 1);
1319 * Scroll the display of the terminal by a number of pages.
1320 * @param {number} pageCount The number of pages to scroll (negative scrolls up).
1322 Terminal
.prototype.scrollPages = function(pageCount
) {
1323 this.scrollDisp(pageCount
* (this.rows
- 1));
1327 * Scrolls the display of the terminal to the top.
1329 Terminal
.prototype.scrollToTop = function() {
1330 this.scrollDisp(-this.ydisp
);
1334 * Scrolls the display of the terminal to the bottom.
1336 Terminal
.prototype.scrollToBottom = function() {
1337 this.scrollDisp(this.ybase
- this.ydisp
);
1341 * Writes text to the terminal.
1342 * @param {string} text The text to write to the terminal.
1344 Terminal
.prototype.write = function(data
) {
1345 var l
= data
.length
, i
= 0, j
, cs
, ch
, code
, low
, ch_width
, row
;
1347 this.refreshStart
= this.y
;
1348 this.refreshEnd
= this.y
;
1350 // apply leftover surrogate high from last write
1351 if (this.surrogate_high
) {
1352 data
= this.surrogate_high
+ data
;
1353 this.surrogate_high
= '';
1356 for (; i
< l
; i
++) {
1359 // FIXME: higher chars than 0xa0 are not allowed in escape sequences
1360 // --> maybe move to default
1361 code
= data
.charCodeAt(i
);
1362 if (0xD800 <= code
&& code
<= 0xDBFF) {
1363 // we got a surrogate high
1364 // get surrogate low (next 2 bytes)
1365 low
= data
.charCodeAt(i
+1);
1367 // end of data stream, save surrogate high
1368 this.surrogate_high
= ch
;
1371 code
= ((code
- 0xD800) * 0x400) + (low
- 0xDC00) + 0x10000;
1372 ch
+= data
.charAt(i
+1);
1374 // surrogate low - already handled above
1375 if (0xDC00 <= code
&& code
<= 0xDFFF)
1377 switch (this.state
) {
1387 if (this.convertEol
) {
1391 if (this.y
> this.scrollBottom
) {
1411 this.x
= this.nextStop();
1426 this.state
= escaped
;
1431 // calculate print space
1432 // expensive call, therefore we save width in line buffer
1433 ch_width
= wcwidth(code
);
1436 if (this.charset
&& this.charset
[ch
]) {
1437 ch
= this.charset
[ch
];
1440 row
= this.y
+ this.ybase
;
1442 // insert combining char in last cell
1443 // FIXME: needs handling after cursor jumps
1444 if (!ch_width
&& this.x
) {
1445 // dont overflow left
1446 if (this.lines
.get(row
)[this.x
-1]) {
1447 if (!this.lines
.get(row
)[this.x
-1][2]) {
1449 // found empty cell after fullwidth, need to go 2 cells back
1450 if (this.lines
.get(row
)[this.x
-2])
1451 this.lines
.get(row
)[this.x
-2][1] += ch
;
1454 this.lines
.get(row
)[this.x
-1][1] += ch
;
1456 this.updateRange(this.y
);
1461 // goto next line if ch would overflow
1462 // TODO: needs a global min terminal width of 2
1463 if (this.x
+ch_width
-1 >= this.cols
) {
1464 // autowrap - DECAWM
1465 if (this.wraparoundMode
) {
1468 if (this.y
> this.scrollBottom
) {
1473 this.x
= this.cols
-1;
1474 if(ch_width
===2) // FIXME: check for xterm behavior
1478 row
= this.y
+ this.ybase
;
1480 // insert mode: move characters to right
1481 if (this.insertMode
) {
1482 // do this twice for a fullwidth char
1483 for (var moves
=0; moves
<ch_width
; ++moves
) {
1484 // remove last cell, if it's width is 0
1485 // we have to adjust the second last cell as well
1486 var removed
= this.lines
.get(this.y
+ this.ybase
).pop();
1488 && this.lines
.get(row
)[this.cols
-2]
1489 && this.lines
.get(row
)[this.cols
-2][2]===2)
1490 this.lines
.get(row
)[this.cols
-2] = [this.curAttr
, ' ', 1];
1492 // insert empty cell at cursor
1493 this.lines
.get(row
).splice(this.x
, 0, [this.curAttr
, ' ', 1]);
1497 this.lines
.get(row
)[this.x
] = [this.curAttr
, ch
, ch_width
];
1499 this.updateRange(this.y
);
1501 // fullwidth char - set next cell width to zero and advance cursor
1503 this.lines
.get(row
)[this.x
] = [this.curAttr
, '', 0];
1512 // ESC [ Control Sequence Introducer ( CSI is 0x9b).
1515 this.currentParam
= 0;
1519 // ESC ] Operating System Command ( OSC is 0x9d).
1522 this.currentParam
= 0;
1526 // ESC P Device Control String ( DCS is 0x90).
1529 this.currentParam
= 0;
1533 // ESC _ Application Program Command ( APC is 0x9f).
1535 this.state
= ignore
;
1538 // ESC ^ Privacy Message ( PM is 0x9e).
1540 this.state
= ignore
;
1543 // ESC c Full Reset (RIS).
1548 // ESC E Next Line ( NEL is 0x85).
1549 // ESC D Index ( IND is 0x84).
1557 // ESC M Reverse Index ( RI is 0x8d).
1559 this.reverseIndex();
1562 // ESC % Select default/utf-8 character set.
1563 // @ = default, G = utf-8
1565 //this.charset = null;
1567 this.setgCharset(0, Terminal
.charsets
.US
);
1568 this.state
= normal
;
1572 // ESC (,),*,+,-,. Designate G0-G2 Character Set.
1573 case '(': // <-- this seems to get all the attention
1599 this.state
= charset
;
1602 // Designate G3 Character Set (VT300).
1603 // A = ISO Latin-1 Supplemental.
1607 this.state
= charset
;
1612 // Single Shift Select of G2 Character Set
1613 // ( SS2 is 0x8e). This affects next character only.
1617 // Single Shift Select of G3 Character Set
1618 // ( SS3 is 0x8f). This affects next character only.
1622 // Invoke the G2 Character Set as GL (LS2).
1627 // Invoke the G3 Character Set as GL (LS3).
1632 // Invoke the G3 Character Set as GR (LS3R).
1637 // Invoke the G2 Character Set as GR (LS2R).
1642 // Invoke the G1 Character Set as GR (LS1R).
1647 // ESC 7 Save Cursor (DECSC).
1650 this.state
= normal
;
1653 // ESC 8 Restore Cursor (DECRC).
1655 this.restoreCursor();
1656 this.state
= normal
;
1659 // ESC # 3 DEC line height/width
1661 this.state
= normal
;
1665 // ESC H Tab Set (HTS is 0x88).
1670 // ESC = Application Keypad (DECKPAM).
1672 this.log('Serial port requested application keypad.');
1673 this.applicationKeypad
= true;
1674 this.viewport
.syncScrollArea();
1675 this.state
= normal
;
1678 // ESC > Normal Keypad (DECKPNM).
1680 this.log('Switching back to normal keypad.');
1681 this.applicationKeypad
= false;
1682 this.viewport
.syncScrollArea();
1683 this.state
= normal
;
1687 this.state
= normal
;
1688 this.error('Unknown ESC control: %s.', ch
);
1695 case '0': // DEC Special Character and Line Drawing Set.
1696 cs
= Terminal
.charsets
.SCLD
;
1699 cs
= Terminal
.charsets
.UK
;
1701 case 'B': // United States (USASCII).
1702 cs
= Terminal
.charsets
.US
;
1705 cs
= Terminal
.charsets
.Dutch
;
1707 case 'C': // Finnish
1709 cs
= Terminal
.charsets
.Finnish
;
1712 cs
= Terminal
.charsets
.French
;
1714 case 'Q': // FrenchCanadian
1715 cs
= Terminal
.charsets
.FrenchCanadian
;
1718 cs
= Terminal
.charsets
.German
;
1720 case 'Y': // Italian
1721 cs
= Terminal
.charsets
.Italian
;
1723 case 'E': // NorwegianDanish
1725 cs
= Terminal
.charsets
.NorwegianDanish
;
1727 case 'Z': // Spanish
1728 cs
= Terminal
.charsets
.Spanish
;
1730 case 'H': // Swedish
1732 cs
= Terminal
.charsets
.Swedish
;
1735 cs
= Terminal
.charsets
.Swiss
;
1737 case '/': // ISOLatin (actually /A)
1738 cs
= Terminal
.charsets
.ISOLatin
;
1742 cs
= Terminal
.charsets
.US
;
1745 this.setgCharset(this.gcharset
, cs
);
1746 this.gcharset
= null;
1747 this.state
= normal
;
1753 // Set Text Parameters.
1754 if (ch
=== C0
.ESC
|| ch
=== C0
.BEL
) {
1755 if (ch
=== C0
.ESC
) i
++;
1757 this.params
.push(this.currentParam
);
1759 switch (this.params
[0]) {
1763 if (this.params
[1]) {
1764 this.title
= this.params
[1];
1765 this.handleTitle(this.title
);
1773 // change dynamic colors
1785 // change dynamic ui colors
1797 // manipulate selection data
1815 this.currentParam
= 0;
1816 this.state
= normal
;
1818 if (!this.params
.length
) {
1819 if (ch
>= '0' && ch
<= '9') {
1821 this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1822 } else if (ch
=== ';') {
1823 this.params
.push(this.currentParam
);
1824 this.currentParam
= '';
1827 this.currentParam
+= ch
;
1834 if (ch
=== '?' || ch
=== '>' || ch
=== '!') {
1840 if (ch
>= '0' && ch
<= '9') {
1841 this.currentParam
= this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1845 // '$', '"', ' ', '\''
1846 if (ch
=== '$' || ch
=== '"' || ch
=== ' ' || ch
=== '\'') {
1851 this.params
.push(this.currentParam
);
1852 this.currentParam
= 0;
1855 if (ch
=== ';') break;
1857 this.state
= normal
;
1861 // Cursor Up Ps Times (default = 1) (CUU).
1863 this.cursorUp(this.params
);
1867 // Cursor Down Ps Times (default = 1) (CUD).
1869 this.cursorDown(this.params
);
1873 // Cursor Forward Ps Times (default = 1) (CUF).
1875 this.cursorForward(this.params
);
1879 // Cursor Backward Ps Times (default = 1) (CUB).
1881 this.cursorBackward(this.params
);
1885 // Cursor Position [row;column] (default = [1,1]) (CUP).
1887 this.cursorPos(this.params
);
1890 // CSI Ps J Erase in Display (ED).
1892 this.eraseInDisplay(this.params
);
1895 // CSI Ps K Erase in Line (EL).
1897 this.eraseInLine(this.params
);
1900 // CSI Pm m Character Attributes (SGR).
1903 this.charAttributes(this.params
);
1907 // CSI Ps n Device Status Report (DSR).
1910 this.deviceStatus(this.params
);
1919 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
1921 this.insertChars(this.params
);
1925 // Cursor Next Line Ps Times (default = 1) (CNL).
1927 this.cursorNextLine(this.params
);
1931 // Cursor Preceding Line Ps Times (default = 1) (CNL).
1933 this.cursorPrecedingLine(this.params
);
1937 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
1939 this.cursorCharAbsolute(this.params
);
1943 // Insert Ps Line(s) (default = 1) (IL).
1945 this.insertLines(this.params
);
1949 // Delete Ps Line(s) (default = 1) (DL).
1951 this.deleteLines(this.params
);
1955 // Delete Ps Character(s) (default = 1) (DCH).
1957 this.deleteChars(this.params
);
1961 // Erase Ps Character(s) (default = 1) (ECH).
1963 this.eraseChars(this.params
);
1966 // CSI Pm ` Character Position Absolute
1967 // [column] (default = [row,1]) (HPA).
1969 this.charPosAbsolute(this.params
);
1973 // Horizontal Position Relative
1975 this.HPositionRelative(this.params
);
1979 // Send Device Attributes (Primary DA).
1981 // Send Device Attributes (Secondary DA)
1983 this.sendDeviceAttributes(this.params
);
1987 // Line Position Absolute [row] (default = [1,column]) (VPA).
1989 this.linePosAbsolute(this.params
);
1992 // 145 65 e * VPR - Vertical Position Relative
1994 this.VPositionRelative(this.params
);
1998 // Horizontal and Vertical Position [row;column] (default =
2001 this.HVPosition(this.params
);
2004 // CSI Pm h Set Mode (SM).
2005 // CSI ? Pm h - mouse escape codes, cursor escape codes
2007 this.setMode(this.params
);
2010 // CSI Pm l Reset Mode (RM).
2013 this.resetMode(this.params
);
2017 // Set Scrolling Region [top;bottom] (default = full size of win-
2021 this.setScrollRegion(this.params
);
2025 // Save cursor (ANSI.SYS).
2027 this.saveCursor(this.params
);
2031 // Restore cursor (ANSI.SYS).
2033 this.restoreCursor(this.params
);
2041 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
2043 this.cursorForwardTab(this.params
);
2046 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
2048 this.scrollUp(this.params
);
2051 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
2052 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
2055 // if (this.prefix === '>') {
2056 // this.resetTitleModes(this.params);
2059 // if (this.params.length > 2) {
2060 // this.initMouseTracking(this.params);
2063 if (this.params
.length
< 2 && !this.prefix
) {
2064 this.scrollDown(this.params
);
2069 // Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
2071 this.cursorBackwardTab(this.params
);
2074 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
2076 this.repeatPrecedingCharacter(this.params
);
2079 // CSI Ps g Tab Clear (TBC).
2081 this.tabClear(this.params
);
2084 // CSI Pm i Media Copy (MC).
2087 // this.mediaCopy(this.params);
2090 // CSI Pm m Character Attributes (SGR).
2092 // case 'm': // duplicate
2093 // if (this.prefix === '>') {
2094 // this.setResources(this.params);
2096 // this.charAttributes(this.params);
2100 // CSI Ps n Device Status Report (DSR).
2102 // case 'n': // duplicate
2103 // if (this.prefix === '>') {
2104 // this.disableModifiers(this.params);
2106 // this.deviceStatus(this.params);
2110 // CSI > Ps p Set pointer mode.
2111 // CSI ! p Soft terminal reset (DECSTR).
2113 // Request ANSI mode (DECRQM).
2115 // Request DEC private mode (DECRQM).
2118 switch (this.prefix
) {
2120 // this.setPointerMode(this.params);
2123 this.softReset(this.params
);
2126 // if (this.postfix === '$') {
2127 // this.requestPrivateMode(this.params);
2131 // if (this.postfix === '"') {
2132 // this.setConformanceLevel(this.params);
2133 // } else if (this.postfix === '$') {
2134 // this.requestAnsiMode(this.params);
2140 // CSI Ps q Load LEDs (DECLL).
2144 // if (this.postfix === ' ') {
2145 // this.setCursorStyle(this.params);
2148 // if (this.postfix === '"') {
2149 // this.setCharProtectionAttr(this.params);
2152 // this.loadLEDs(this.params);
2156 // Set Scrolling Region [top;bottom] (default = full size of win-
2159 // CSI Pt; Pl; Pb; Pr; Ps$ r
2160 // case 'r': // duplicate
2161 // if (this.prefix === '?') {
2162 // this.restorePrivateValues(this.params);
2163 // } else if (this.postfix === '$') {
2164 // this.setAttrInRectangle(this.params);
2166 // this.setScrollRegion(this.params);
2170 // CSI s Save cursor (ANSI.SYS).
2172 // case 's': // duplicate
2173 // if (this.prefix === '?') {
2174 // this.savePrivateValues(this.params);
2176 // this.saveCursor(this.params);
2180 // CSI Ps ; Ps ; Ps t
2181 // CSI Pt; Pl; Pb; Pr; Ps$ t
2185 // if (this.postfix === '$') {
2186 // this.reverseAttrInRectangle(this.params);
2187 // } else if (this.postfix === ' ') {
2188 // this.setWarningBellVolume(this.params);
2190 // if (this.prefix === '>') {
2191 // this.setTitleModeFeature(this.params);
2193 // this.manipulateWindow(this.params);
2198 // CSI u Restore cursor (ANSI.SYS).
2200 // case 'u': // duplicate
2201 // if (this.postfix === ' ') {
2202 // this.setMarginBellVolume(this.params);
2204 // this.restoreCursor(this.params);
2208 // CSI Pt; Pl; Pb; Pr; Pp; Pt; Pl; Pp$ v
2210 // if (this.postfix === '$') {
2211 // this.copyRectagle(this.params);
2215 // CSI Pt ; Pl ; Pb ; Pr ' w
2217 // if (this.postfix === '\'') {
2218 // this.enableFilterRectangle(this.params);
2222 // CSI Ps x Request Terminal Parameters (DECREQTPARM).
2223 // CSI Ps x Select Attribute Change Extent (DECSACE).
2224 // CSI Pc; Pt; Pl; Pb; Pr$ x
2226 // if (this.postfix === '$') {
2227 // this.fillRectangle(this.params);
2229 // this.requestParameters(this.params);
2230 // //this.__(this.params);
2235 // CSI Pt; Pl; Pb; Pr$ z
2237 // if (this.postfix === '\'') {
2238 // this.enableLocatorReporting(this.params);
2239 // } else if (this.postfix === '$') {
2240 // this.eraseRectangle(this.params);
2245 // CSI Pt; Pl; Pb; Pr$ {
2247 // if (this.postfix === '\'') {
2248 // this.setLocatorEvents(this.params);
2249 // } else if (this.postfix === '$') {
2250 // this.selectiveEraseRectangle(this.params);
2256 // if (this.postfix === '\'') {
2257 // this.requestLocatorPosition(this.params);
2262 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
2264 // if (this.postfix === ' ') {
2265 // this.insertColumns(this.params);
2270 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
2272 // if (this.postfix === ' ') {
2273 // this.deleteColumns(this.params);
2278 this.error('Unknown CSI code: %s.', ch
);
2287 if (ch
=== C0
.ESC
|| ch
=== C0
.BEL
) {
2288 if (ch
=== C0
.ESC
) i
++;
2290 switch (this.prefix
) {
2291 // User-Defined Keys (DECUDK).
2295 // Request Status String (DECRQSS).
2296 // test: echo -e '\eP$q"p\e\\'
2298 var pt
= this.currentParam
2315 + (this.scrollTop
+ 1)
2317 + (this.scrollBottom
+ 1)
2327 this.error('Unknown DCS Pt: %s.', pt
);
2332 this.send(C0
.ESC
+ 'P' + +valid
+ '$r' + pt
+ C0
.ESC
+ '\\');
2335 // Set Termcap/Terminfo Data (xterm, experimental).
2339 // Request Termcap/Terminfo String (xterm, experimental)
2340 // Regular xterm does not even respond to this sequence.
2341 // This can cause a small glitch in vim.
2342 // test: echo -ne '\eP+q6b64\e\\'
2344 var pt
= this.currentParam
2347 this.send(C0
.ESC
+ 'P' + +valid
+ '+r' + pt
+ C0
.ESC
+ '\\');
2351 this.error('Unknown DCS prefix: %s.', this.prefix
);
2355 this.currentParam
= 0;
2357 this.state
= normal
;
2358 } else if (!this.currentParam
) {
2359 if (!this.prefix
&& ch
!== '$' && ch
!== '+') {
2360 this.currentParam
= ch
;
2361 } else if (this.prefix
.length
=== 2) {
2362 this.currentParam
= ch
;
2367 this.currentParam
+= ch
;
2373 if (ch
=== C0
.ESC
|| ch
=== C0
.BEL
) {
2374 if (ch
=== C0
.ESC
) i
++;
2375 this.state
= normal
;
2381 this.updateRange(this.y
);
2382 this.refresh(this.refreshStart
, this.refreshEnd
);
2386 * Writes text to the terminal, followed by a break line character (\n).
2387 * @param {string} text The text to write to the terminal.
2389 Terminal
.prototype.writeln = function(data
) {
2390 this.write(data
+ '\r\n');
2394 * Attaches a custom keydown handler which is run before keys are processed, giving consumers of
2395 * xterm.js ultimate control as to what keys should be processed by the terminal and what keys
2397 * @param {function} customKeydownHandler The custom KeyboardEvent handler to attach. This is a
2398 * function that takes a KeyboardEvent, allowing consumers to stop propogation and/or prevent
2399 * the default action. The function returns whether the event should be processed by xterm.js.
2401 Terminal
.prototype.attachCustomKeydownHandler = function(customKeydownHandler
) {
2402 this.customKeydownHandler
= customKeydownHandler
;
2406 * Handle a keydown event
2408 * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
2409 * @param {KeyboardEvent} ev The keydown event to be handled.
2411 Terminal
.prototype.keyDown = function(ev
) {
2412 if (this.customKeydownHandler
&& this.customKeydownHandler(ev
) === false) {
2416 if (!this.compositionHelper
.keydown
.bind(this.compositionHelper
)(ev
)) {
2417 if (this.ybase
!== this.ydisp
) {
2418 this.scrollToBottom();
2424 var result
= this.evaluateKeyEscapeSequence(ev
);
2426 if (result
.scrollDisp
) {
2427 this.scrollDisp(result
.scrollDisp
);
2428 return this.cancel(ev
, true);
2431 if (isThirdLevelShift(this, ev
)) {
2435 if (result
.cancel
) {
2436 // The event is canceled at the end already, is this necessary?
2437 this.cancel(ev
, true);
2444 this.emit('keydown', ev
);
2445 this.emit('key', result
.key
, ev
);
2447 this.handler(result
.key
);
2449 return this.cancel(ev
, true);
2453 * Returns an object that determines how a KeyboardEvent should be handled. The key of the
2454 * returned value is the new key code to pass to the PTY.
2456 * Reference: http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
2457 * @param {KeyboardEvent} ev The keyboard event to be translated to key escape sequence.
2459 Terminal
.prototype.evaluateKeyEscapeSequence = function(ev
) {
2461 // Whether to cancel event propogation (NOTE: this may not be needed since the event is
2462 // canceled at the end of keyDown
2464 // The new key even to emit
2466 // The number of characters to scroll, if this is defined it will cancel the event
2467 scrollDisp
: undefined
2469 var modifiers
= ev
.shiftKey
<< 0 | ev
.altKey
<< 1 | ev
.ctrlKey
<< 2 | ev
.metaKey
<< 3;
2470 switch (ev
.keyCode
) {
2474 result
.key
= C0
.BS
; // ^H
2477 result
.key
= C0
.DEL
; // ^?
2482 result
.key
= C0
.ESC
+ '[Z';
2486 result
.cancel
= true;
2491 result
.cancel
= true;
2495 result
.key
= C0
.ESC
;
2496 result
.cancel
= true;
2501 result
.key
= C0
.ESC
+ '[1;' + (modifiers
+ 1) + 'D';
2502 // HACK: Make Alt + left-arrow behave like Ctrl + left-arrow: move one word backwards
2503 // http://unix.stackexchange.com/a/108106
2504 // macOS uses different escape sequences than linux
2505 if (result
.key
== C0
.ESC
+ '[1;3D') {
2506 result
.key
= (this.browser
.isMac
) ? C0
.ESC
+ 'b' : C0
.ESC
+ '[1;5D';
2508 } else if (this.applicationCursor
) {
2509 result
.key
= C0
.ESC
+ 'OD';
2511 result
.key
= C0
.ESC
+ '[D';
2517 result
.key
= C0
.ESC
+ '[1;' + (modifiers
+ 1) + 'C';
2518 // HACK: Make Alt + right-arrow behave like Ctrl + right-arrow: move one word forward
2519 // http://unix.stackexchange.com/a/108106
2520 // macOS uses different escape sequences than linux
2521 if (result
.key
== C0
.ESC
+ '[1;3C') {
2522 result
.key
= (this.browser
.isMac
) ? C0
.ESC
+ 'f' : C0
.ESC
+ '[1;5C';
2524 } else if (this.applicationCursor
) {
2525 result
.key
= C0
.ESC
+ 'OC';
2527 result
.key
= C0
.ESC
+ '[C';
2533 result
.key
= C0
.ESC
+ '[1;' + (modifiers
+ 1) + 'A';
2534 // HACK: Make Alt + up-arrow behave like Ctrl + up-arrow
2535 // http://unix.stackexchange.com/a/108106
2536 if (result
.key
== C0
.ESC
+ '[1;3A') {
2537 result
.key
= C0
.ESC
+ '[1;5A';
2539 } else if (this.applicationCursor
) {
2540 result
.key
= C0
.ESC
+ 'OA';
2542 result
.key
= C0
.ESC
+ '[A';
2548 result
.key
= C0
.ESC
+ '[1;' + (modifiers
+ 1) + 'B';
2549 // HACK: Make Alt + down-arrow behave like Ctrl + down-arrow
2550 // http://unix.stackexchange.com/a/108106
2551 if (result
.key
== C0
.ESC
+ '[1;3B') {
2552 result
.key
= C0
.ESC
+ '[1;5B';
2554 } else if (this.applicationCursor
) {
2555 result
.key
= C0
.ESC
+ 'OB';
2557 result
.key
= C0
.ESC
+ '[B';
2562 if (!ev
.shiftKey
&& !ev
.ctrlKey
) {
2563 // <Ctrl> or <Shift> + <Insert> are used to
2564 // copy-paste on some systems.
2565 result
.key
= C0
.ESC
+ '[2~';
2571 result
.key
= C0
.ESC
+ '[3;' + (modifiers
+ 1) + '~';
2573 result
.key
= C0
.ESC
+ '[3~';
2579 result
.key
= C0
.ESC
+ '[1;' + (modifiers
+ 1) + 'H';
2580 else if (this.applicationCursor
)
2581 result
.key
= C0
.ESC
+ 'OH';
2583 result
.key
= C0
.ESC
+ '[H';
2588 result
.key
= C0
.ESC
+ '[1;' + (modifiers
+ 1) + 'F';
2589 else if (this.applicationCursor
)
2590 result
.key
= C0
.ESC
+ 'OF';
2592 result
.key
= C0
.ESC
+ '[F';
2597 result
.scrollDisp
= -(this.rows
- 1);
2599 result
.key
= C0
.ESC
+ '[5~';
2605 result
.scrollDisp
= this.rows
- 1;
2607 result
.key
= C0
.ESC
+ '[6~';
2613 result
.key
= C0
.ESC
+ '[1;' + (modifiers
+ 1) + 'P';
2615 result
.key
= C0
.ESC
+ 'OP';
2620 result
.key
= C0
.ESC
+ '[1;' + (modifiers
+ 1) + 'Q';
2622 result
.key
= C0
.ESC
+ 'OQ';
2627 result
.key
= C0
.ESC
+ '[1;' + (modifiers
+ 1) + 'R';
2629 result
.key
= C0
.ESC
+ 'OR';
2634 result
.key
= C0
.ESC
+ '[1;' + (modifiers
+ 1) + 'S';
2636 result
.key
= C0
.ESC
+ 'OS';
2641 result
.key
= C0
.ESC
+ '[15;' + (modifiers
+ 1) + '~';
2643 result
.key
= C0
.ESC
+ '[15~';
2648 result
.key
= C0
.ESC
+ '[17;' + (modifiers
+ 1) + '~';
2650 result
.key
= C0
.ESC
+ '[17~';
2655 result
.key
= C0
.ESC
+ '[18;' + (modifiers
+ 1) + '~';
2657 result
.key
= C0
.ESC
+ '[18~';
2662 result
.key
= C0
.ESC
+ '[19;' + (modifiers
+ 1) + '~';
2664 result
.key
= C0
.ESC
+ '[19~';
2669 result
.key
= C0
.ESC
+ '[20;' + (modifiers
+ 1) + '~';
2671 result
.key
= C0
.ESC
+ '[20~';
2676 result
.key
= C0
.ESC
+ '[21;' + (modifiers
+ 1) + '~';
2678 result
.key
= C0
.ESC
+ '[21~';
2683 result
.key
= C0
.ESC
+ '[23;' + (modifiers
+ 1) + '~';
2685 result
.key
= C0
.ESC
+ '[23~';
2690 result
.key
= C0
.ESC
+ '[24;' + (modifiers
+ 1) + '~';
2692 result
.key
= C0
.ESC
+ '[24~';
2697 if (ev
.ctrlKey
&& !ev
.shiftKey
&& !ev
.altKey
&& !ev
.metaKey
) {
2698 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2699 result
.key
= String
.fromCharCode(ev
.keyCode
- 64);
2700 } else if (ev
.keyCode
=== 32) {
2702 result
.key
= String
.fromCharCode(0);
2703 } else if (ev
.keyCode
>= 51 && ev
.keyCode
<= 55) {
2704 // escape, file sep, group sep, record sep, unit sep
2705 result
.key
= String
.fromCharCode(ev
.keyCode
- 51 + 27);
2706 } else if (ev
.keyCode
=== 56) {
2708 result
.key
= String
.fromCharCode(127);
2709 } else if (ev
.keyCode
=== 219) {
2710 // ^[ - Control Sequence Introducer (CSI)
2711 result
.key
= String
.fromCharCode(27);
2712 } else if (ev
.keyCode
=== 220) {
2713 // ^\ - String Terminator (ST)
2714 result
.key
= String
.fromCharCode(28);
2715 } else if (ev
.keyCode
=== 221) {
2716 // ^] - Operating System Command (OSC)
2717 result
.key
= String
.fromCharCode(29);
2719 } else if (!this.browser
.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) {
2720 // On Mac this is a third level shift. Use <Esc> instead.
2721 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2722 result
.key
= C0
.ESC
+ String
.fromCharCode(ev
.keyCode
+ 32);
2723 } else if (ev
.keyCode
=== 192) {
2724 result
.key
= C0
.ESC
+ '`';
2725 } else if (ev
.keyCode
>= 48 && ev
.keyCode
<= 57) {
2726 result
.key
= C0
.ESC
+ (ev
.keyCode
- 48);
2735 * Set the G level of the terminal
2738 Terminal
.prototype.setgLevel = function(g
) {
2740 this.charset
= this.charsets
[g
];
2744 * Set the charset for the given G level of the terminal
2748 Terminal
.prototype.setgCharset = function(g
, charset
) {
2749 this.charsets
[g
] = charset
;
2750 if (this.glevel
=== g
) {
2751 this.charset
= charset
;
2756 * Handle a keypress event.
2758 * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
2759 * @param {KeyboardEvent} ev The keypress event to be handled.
2761 Terminal
.prototype.keyPress = function(ev
) {
2768 } else if (ev
.which
== null) {
2770 } else if (ev
.which
!== 0 && ev
.charCode
!== 0) {
2777 (ev
.altKey
|| ev
.ctrlKey
|| ev
.metaKey
) && !isThirdLevelShift(this, ev
)
2782 key
= String
.fromCharCode(key
);
2784 this.emit('keypress', key
, ev
);
2785 this.emit('key', key
, ev
);
2793 * Send data for handling to the terminal
2794 * @param {string} data
2796 Terminal
.prototype.send = function(data
) {
2800 setTimeout(function() {
2801 self
.handler(self
.queue
);
2811 * Note: We could do sweet things with webaudio here
2813 Terminal
.prototype.bell = function() {
2814 if (!this.visualBell
) return;
2816 this.element
.style
.borderColor
= 'white';
2817 setTimeout(function() {
2818 self
.element
.style
.borderColor
= '';
2820 if (this.popOnBell
) this.focus();
2824 * Log the current state to the console.
2826 Terminal
.prototype.log = function() {
2827 if (!this.debug
) return;
2828 if (!this.context
.console
|| !this.context
.console
.log
) return;
2829 var args
= Array
.prototype.slice
.call(arguments
);
2830 this.context
.console
.log
.apply(this.context
.console
, args
);
2834 * Log the current state as error to the console.
2836 Terminal
.prototype.error = function() {
2837 if (!this.debug
) return;
2838 if (!this.context
.console
|| !this.context
.console
.error
) return;
2839 var args
= Array
.prototype.slice
.call(arguments
);
2840 this.context
.console
.error
.apply(this.context
.console
, args
);
2844 * Resizes the terminal.
2846 * @param {number} x The number of columns to resize to.
2847 * @param {number} y The number of rows to resize to.
2849 Terminal
.prototype.resize = function(x
, y
) {
2857 if (x
=== this.cols
&& y
=== this.rows
) {
2867 ch
= [this.defAttr
, ' ', 1]; // does xterm use the default attr?
2868 i
= this.lines
.length
;
2870 while (this.lines
.get(i
).length
< x
) {
2871 this.lines
.get(i
).push(ch
);
2875 i
= this.lines
.length
;
2877 while (this.lines
.get(i
).length
> x
) {
2878 this.lines
.get(i
).pop();
2891 // y is rows, not this.y
2892 if (this.lines
.length
< y
+ this.ybase
) {
2893 if (this.ybase
> 0 && this.lines
.length
<= this.ybase
+ this.y
+ addToY
+ 1) {
2894 // There is room above the buffer and there are no empty elements below the line,
2898 if (this.ydisp
> 0) {
2899 // Viewport is at the top of the buffer, must increase downwards
2903 // Add a blank line if there is no buffer left at the top to scroll to, or if there
2904 // are blank lines after the cursor
2905 this.lines
.push(this.blankLine());
2908 if (this.children
.length
< y
) {
2914 if (this.lines
.length
> y
+ this.ybase
) {
2915 if (this.lines
.length
> this.ybase
+ this.y
+ 1) {
2916 // The line is a blank line below the cursor, remove it
2919 // The line is the cursor, scroll down
2924 if (this.children
.length
> y
) {
2925 el
= this.children
.shift();
2927 el
.parentNode
.removeChild(el
);
2933 // Make sure that the cursor stays on screen
2946 this.scrollBottom
= y
- 1;
2948 this.refresh(0, this.rows
- 1);
2952 this.geometry
= [this.cols
, this.rows
];
2953 this.emit('resize', {terminal
: this, cols
: x
, rows
: y
});
2957 * Updates the range of rows to refresh
2958 * @param {number} y The number of rows to refresh next.
2960 Terminal
.prototype.updateRange = function(y
) {
2961 if (y
< this.refreshStart
) this.refreshStart
= y
;
2962 if (y
> this.refreshEnd
) this.refreshEnd
= y
;
2963 // if (y > this.refreshEnd) {
2964 // this.refreshEnd = y;
2965 // if (y > this.rows - 1) {
2966 // this.refreshEnd = this.rows - 1;
2972 * Set the range of refreshing to the maximum value
2974 Terminal
.prototype.maxRange = function() {
2975 this.refreshStart
= 0;
2976 this.refreshEnd
= this.rows
- 1;
2982 * Setup the tab stops.
2985 Terminal
.prototype.setupStops = function(i
) {
2987 if (!this.tabs
[i
]) {
2988 i
= this.prevStop(i
);
2995 for (; i
< this.cols
; i
+= 8) {
2996 this.tabs
[i
] = true;
3002 * Move the cursor to the previous tab stop from the given position (default is current).
3003 * @param {number} x The position to move the cursor to the previous tab stop.
3005 Terminal
.prototype.prevStop = function(x
) {
3006 if (x
== null) x
= this.x
;
3007 while (!this.tabs
[--x
] && x
> 0);
3008 return x
>= this.cols
3015 * Move the cursor one tab stop forward from the given position (default is current).
3016 * @param {number} x The position to move the cursor one tab stop forward.
3018 Terminal
.prototype.nextStop = function(x
) {
3019 if (x
== null) x
= this.x
;
3020 while (!this.tabs
[++x
] && x
< this.cols
);
3021 return x
>= this.cols
3028 * Erase in the identified line everything from "x" to the end of the line (right).
3029 * @param {number} x The column from which to start erasing to the end of the line.
3030 * @param {number} y The line in which to operate.
3032 Terminal
.prototype.eraseRight = function(x
, y
) {
3033 var line
= this.lines
.get(this.ybase
+ y
)
3034 , ch
= [this.eraseAttr(), ' ', 1]; // xterm
3037 for (; x
< this.cols
; x
++) {
3041 this.updateRange(y
);
3047 * Erase in the identified line everything from "x" to the start of the line (left).
3048 * @param {number} x The column from which to start erasing to the start of the line.
3049 * @param {number} y The line in which to operate.
3051 Terminal
.prototype.eraseLeft = function(x
, y
) {
3052 var line
= this.lines
.get(this.ybase
+ y
)
3053 , ch
= [this.eraseAttr(), ' ', 1]; // xterm
3056 while (x
--) line
[x
] = ch
;
3058 this.updateRange(y
);
3062 * Clears the entire buffer, making the prompt line the new first line.
3064 Terminal
.prototype.clear = function() {
3065 if (this.ybase
=== 0 && this.y
=== 0) {
3066 // Don't clear if it's already clear
3069 this.lines
.set(0, this.lines
.get(this.ybase
+ this.y
));
3070 this.lines
.length
= 1;
3074 for (var i
= 1; i
< this.rows
; i
++) {
3075 this.lines
.push(this.blankLine());
3077 this.refresh(0, this.rows
- 1);
3078 this.emit('scroll', this.ydisp
);
3082 * Erase all content in the given line
3083 * @param {number} y The line to erase all of its contents.
3085 Terminal
.prototype.eraseLine = function(y
) {
3086 this.eraseRight(0, y
);
3091 * Return the data array of a blank line
3092 * @param {number} cur First bunch of data for each "blank" character.
3094 Terminal
.prototype.blankLine = function(cur
) {
3099 var ch
= [attr
, ' ', 1] // width defaults to 1 halfwidth character
3103 for (; i
< this.cols
; i
++) {
3112 * If cur return the back color xterm feature attribute. Else return defAttr.
3113 * @param {object} cur
3115 Terminal
.prototype.ch = function(cur
) {
3117 ? [this.eraseAttr(), ' ', 1]
3118 : [this.defAttr
, ' ', 1];
3123 * Evaluate if the current erminal is the given argument.
3124 * @param {object} term The terminal to evaluate
3126 Terminal
.prototype.is = function(term
) {
3127 var name
= this.termName
;
3128 return (name
+ '').indexOf(term
) === 0;
3133 * Emit the 'data' event and populate the given data.
3134 * @param {string} data The data to populate in the event.
3136 Terminal
.prototype.handler = function(data
) {
3137 // Input is being sent to the terminal, the terminal should focus the prompt.
3138 if (this.ybase
!== this.ydisp
) {
3139 this.scrollToBottom();
3141 this.emit('data', data
);
3146 * Emit the 'title' event and populate the given title.
3147 * @param {string} title The title to populate in the event.
3149 Terminal
.prototype.handleTitle = function(title
) {
3151 * This event is emitted when the title of the terminal is changed
3152 * from inside the terminal. The parameter is the new title.
3156 this.emit('title', title
);
3165 * ESC D Index (IND is 0x84).
3167 Terminal
.prototype.index = function() {
3169 if (this.y
> this.scrollBottom
) {
3173 this.state
= normal
;
3178 * ESC M Reverse Index (RI is 0x8d).
3180 * Move the cursor up one row, inserting a new blank line if necessary.
3182 Terminal
.prototype.reverseIndex = function() {
3184 if (this.y
=== this.scrollTop
) {
3185 // possibly move the code below to term.reverseScroll();
3186 // test: echo -ne '\e[1;1H\e[44m\eM\e[0m'
3187 // blankLine(true) is xterm/linux behavior
3188 this.lines
.shiftElements(this.y
+ this.ybase
, this.rows
- 1, 1);
3189 this.lines
.set(this.y
+ this.ybase
, this.blankLine(true));
3190 this.updateRange(this.scrollTop
);
3191 this.updateRange(this.scrollBottom
);
3195 this.state
= normal
;
3200 * ESC c Full Reset (RIS).
3202 Terminal
.prototype.reset = function() {
3203 this.options
.rows
= this.rows
;
3204 this.options
.cols
= this.cols
;
3205 var customKeydownHandler
= this.customKeydownHandler
;
3206 Terminal
.call(this, this.options
);
3207 this.customKeydownHandler
= customKeydownHandler
;
3208 this.refresh(0, this.rows
- 1);
3209 this.viewport
.syncScrollArea();
3214 * ESC H Tab Set (HTS is 0x88).
3216 Terminal
.prototype.tabSet = function() {
3217 this.tabs
[this.x
] = true;
3218 this.state
= normal
;
3228 * Cursor Up Ps Times (default = 1) (CUU).
3230 Terminal
.prototype.cursorUp = function(params
) {
3231 var param
= params
[0];
3232 if (param
< 1) param
= 1;
3234 if (this.y
< 0) this.y
= 0;
3240 * Cursor Down Ps Times (default = 1) (CUD).
3242 Terminal
.prototype.cursorDown = function(params
) {
3243 var param
= params
[0];
3244 if (param
< 1) param
= 1;
3246 if (this.y
>= this.rows
) {
3247 this.y
= this.rows
- 1;
3254 * Cursor Forward Ps Times (default = 1) (CUF).
3256 Terminal
.prototype.cursorForward = function(params
) {
3257 var param
= params
[0];
3258 if (param
< 1) param
= 1;
3260 if (this.x
>= this.cols
) {
3261 this.x
= this.cols
- 1;
3268 * Cursor Backward Ps Times (default = 1) (CUB).
3270 Terminal
.prototype.cursorBackward = function(params
) {
3271 var param
= params
[0];
3272 if (param
< 1) param
= 1;
3274 if (this.x
< 0) this.x
= 0;
3280 * Cursor Position [row;column] (default = [1,1]) (CUP).
3282 Terminal
.prototype.cursorPos = function(params
) {
3285 row
= params
[0] - 1;
3287 if (params
.length
>= 2) {
3288 col
= params
[1] - 1;
3295 } else if (row
>= this.rows
) {
3296 row
= this.rows
- 1;
3301 } else if (col
>= this.cols
) {
3302 col
= this.cols
- 1;
3311 * CSI Ps J Erase in Display (ED).
3312 * Ps = 0 -> Erase Below (default).
3313 * Ps = 1 -> Erase Above.
3314 * Ps = 2 -> Erase All.
3315 * Ps = 3 -> Erase Saved Lines (xterm).
3317 * Erase in Display (DECSED).
3318 * Ps = 0 -> Selective Erase Below (default).
3319 * Ps = 1 -> Selective Erase Above.
3320 * Ps = 2 -> Selective Erase All.
3322 Terminal
.prototype.eraseInDisplay = function(params
) {
3324 switch (params
[0]) {
3326 this.eraseRight(this.x
, this.y
);
3328 for (; j
< this.rows
; j
++) {
3333 this.eraseLeft(this.x
, this.y
);
3341 while (j
--) this.eraseLine(j
);
3351 * CSI Ps K Erase in Line (EL).
3352 * Ps = 0 -> Erase to Right (default).
3353 * Ps = 1 -> Erase to Left.
3354 * Ps = 2 -> Erase All.
3356 * Erase in Line (DECSEL).
3357 * Ps = 0 -> Selective Erase to Right (default).
3358 * Ps = 1 -> Selective Erase to Left.
3359 * Ps = 2 -> Selective Erase All.
3361 Terminal
.prototype.eraseInLine = function(params
) {
3362 switch (params
[0]) {
3364 this.eraseRight(this.x
, this.y
);
3367 this.eraseLeft(this.x
, this.y
);
3370 this.eraseLine(this.y
);
3377 * CSI Pm m Character Attributes (SGR).
3378 * Ps = 0 -> Normal (default).
3380 * Ps = 4 -> Underlined.
3381 * Ps = 5 -> Blink (appears as Bold).
3382 * Ps = 7 -> Inverse.
3383 * Ps = 8 -> Invisible, i.e., hidden (VT300).
3384 * Ps = 2 2 -> Normal (neither bold nor faint).
3385 * Ps = 2 4 -> Not underlined.
3386 * Ps = 2 5 -> Steady (not blinking).
3387 * Ps = 2 7 -> Positive (not inverse).
3388 * Ps = 2 8 -> Visible, i.e., not hidden (VT300).
3389 * Ps = 3 0 -> Set foreground color to Black.
3390 * Ps = 3 1 -> Set foreground color to Red.
3391 * Ps = 3 2 -> Set foreground color to Green.
3392 * Ps = 3 3 -> Set foreground color to Yellow.
3393 * Ps = 3 4 -> Set foreground color to Blue.
3394 * Ps = 3 5 -> Set foreground color to Magenta.
3395 * Ps = 3 6 -> Set foreground color to Cyan.
3396 * Ps = 3 7 -> Set foreground color to White.
3397 * Ps = 3 9 -> Set foreground color to default (original).
3398 * Ps = 4 0 -> Set background color to Black.
3399 * Ps = 4 1 -> Set background color to Red.
3400 * Ps = 4 2 -> Set background color to Green.
3401 * Ps = 4 3 -> Set background color to Yellow.
3402 * Ps = 4 4 -> Set background color to Blue.
3403 * Ps = 4 5 -> Set background color to Magenta.
3404 * Ps = 4 6 -> Set background color to Cyan.
3405 * Ps = 4 7 -> Set background color to White.
3406 * Ps = 4 9 -> Set background color to default (original).
3408 * If 16-color support is compiled, the following apply. Assume
3409 * that xterm's resources are set so that the ISO color codes are
3410 * the first 8 of a set of 16. Then the aixterm colors are the
3411 * bright versions of the ISO colors:
3412 * Ps = 9 0 -> Set foreground color to Black.
3413 * Ps = 9 1 -> Set foreground color to Red.
3414 * Ps = 9 2 -> Set foreground color to Green.
3415 * Ps = 9 3 -> Set foreground color to Yellow.
3416 * Ps = 9 4 -> Set foreground color to Blue.
3417 * Ps = 9 5 -> Set foreground color to Magenta.
3418 * Ps = 9 6 -> Set foreground color to Cyan.
3419 * Ps = 9 7 -> Set foreground color to White.
3420 * Ps = 1 0 0 -> Set background color to Black.
3421 * Ps = 1 0 1 -> Set background color to Red.
3422 * Ps = 1 0 2 -> Set background color to Green.
3423 * Ps = 1 0 3 -> Set background color to Yellow.
3424 * Ps = 1 0 4 -> Set background color to Blue.
3425 * Ps = 1 0 5 -> Set background color to Magenta.
3426 * Ps = 1 0 6 -> Set background color to Cyan.
3427 * Ps = 1 0 7 -> Set background color to White.
3429 * If xterm is compiled with the 16-color support disabled, it
3430 * supports the following, from rxvt:
3431 * Ps = 1 0 0 -> Set foreground and background color to
3434 * If 88- or 256-color support is compiled, the following apply.
3435 * Ps = 3 8 ; 5 ; Ps -> Set foreground color to the second
3437 * Ps = 4 8 ; 5 ; Ps -> Set background color to the second
3440 Terminal
.prototype.charAttributes = function(params
) {
3441 // Optimize a single SGR0.
3442 if (params
.length
=== 1 && params
[0] === 0) {
3443 this.curAttr
= this.defAttr
;
3447 var l
= params
.length
3449 , flags
= this.curAttr
>> 18
3450 , fg
= (this.curAttr
>> 9) & 0x1ff
3451 , bg
= this.curAttr
& 0x1ff
3454 for (; i
< l
; i
++) {
3456 if (p
>= 30 && p
<= 37) {
3459 } else if (p
>= 40 && p
<= 47) {
3462 } else if (p
>= 90 && p
<= 97) {
3466 } else if (p
>= 100 && p
<= 107) {
3470 } else if (p
=== 0) {
3472 flags
= this.defAttr
>> 18;
3473 fg
= (this.defAttr
>> 9) & 0x1ff;
3474 bg
= this.defAttr
& 0x1ff;
3478 } else if (p
=== 1) {
3481 } else if (p
=== 4) {
3484 } else if (p
=== 5) {
3487 } else if (p
=== 7) {
3488 // inverse and positive
3489 // test with: echo -e '\e[31m\e[42mhello\e[7mworld\e[27mhi\e[m'
3491 } else if (p
=== 8) {
3494 } else if (p
=== 22) {
3497 } else if (p
=== 24) {
3500 } else if (p
=== 25) {
3503 } else if (p
=== 27) {
3506 } else if (p
=== 28) {
3509 } else if (p
=== 39) {
3511 fg
= (this.defAttr
>> 9) & 0x1ff;
3512 } else if (p
=== 49) {
3514 bg
= this.defAttr
& 0x1ff;
3515 } else if (p
=== 38) {
3517 if (params
[i
+ 1] === 2) {
3521 params
[i
+ 1] & 0xff,
3522 params
[i
+ 2] & 0xff);
3523 if (fg
=== -1) fg
= 0x1ff;
3525 } else if (params
[i
+ 1] === 5) {
3527 p
= params
[i
] & 0xff;
3530 } else if (p
=== 48) {
3532 if (params
[i
+ 1] === 2) {
3536 params
[i
+ 1] & 0xff,
3537 params
[i
+ 2] & 0xff);
3538 if (bg
=== -1) bg
= 0x1ff;
3540 } else if (params
[i
+ 1] === 5) {
3542 p
= params
[i
] & 0xff;
3545 } else if (p
=== 100) {
3547 fg
= (this.defAttr
>> 9) & 0x1ff;
3548 bg
= this.defAttr
& 0x1ff;
3550 this.error('Unknown SGR attribute: %d.', p
);
3554 this.curAttr
= (flags
<< 18) | (fg
<< 9) | bg
;
3559 * CSI Ps n Device Status Report (DSR).
3560 * Ps = 5 -> Status Report. Result (``OK'') is
3562 * Ps = 6 -> Report Cursor Position (CPR) [row;column].
3566 * Device Status Report (DSR, DEC-specific).
3567 * Ps = 6 -> Report Cursor Position (CPR) [row;column] as CSI
3568 * ? r ; c R (assumes page is zero).
3569 * Ps = 1 5 -> Report Printer status as CSI ? 1 0 n (ready).
3570 * or CSI ? 1 1 n (not ready).
3571 * Ps = 2 5 -> Report UDK status as CSI ? 2 0 n (unlocked)
3572 * or CSI ? 2 1 n (locked).
3573 * Ps = 2 6 -> Report Keyboard status as
3574 * CSI ? 2 7 ; 1 ; 0 ; 0 n (North American).
3575 * The last two parameters apply to VT400 & up, and denote key-
3576 * board ready and LK01 respectively.
3577 * Ps = 5 3 -> Report Locator status as
3578 * CSI ? 5 3 n Locator available, if compiled-in, or
3579 * CSI ? 5 0 n No Locator, if not.
3581 Terminal
.prototype.deviceStatus = function(params
) {
3583 switch (params
[0]) {
3586 this.send(C0
.ESC
+ '[0n');
3590 this.send(C0
.ESC
+ '['
3597 } else if (this.prefix
=== '?') {
3598 // modern xterm doesnt seem to
3599 // respond to any of these except ?6, 6, and 5
3600 switch (params
[0]) {
3603 this.send(C0
.ESC
+ '[?'
3611 // this.send(C0.ESC + '[?11n');
3614 // dont support user defined keys
3615 // this.send(C0.ESC + '[?21n');
3618 // north american keyboard
3619 // this.send(C0.ESC + '[?27;1;0;0n');
3622 // no dec locator/mouse
3623 // this.send(C0.ESC + '[?50n');
3636 * Insert Ps (Blank) Character(s) (default = 1) (ICH).
3638 Terminal
.prototype.insertChars = function(params
) {
3639 var param
, row
, j
, ch
;
3642 if (param
< 1) param
= 1;
3644 row
= this.y
+ this.ybase
;
3646 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3648 while (param
-- && j
< this.cols
) {
3649 this.lines
.get(row
).splice(j
++, 0, ch
);
3650 this.lines
.get(row
).pop();
3656 * Cursor Next Line Ps Times (default = 1) (CNL).
3657 * same as CSI Ps B ?
3659 Terminal
.prototype.cursorNextLine = function(params
) {
3660 var param
= params
[0];
3661 if (param
< 1) param
= 1;
3663 if (this.y
>= this.rows
) {
3664 this.y
= this.rows
- 1;
3672 * Cursor Preceding Line Ps Times (default = 1) (CNL).
3675 Terminal
.prototype.cursorPrecedingLine = function(params
) {
3676 var param
= params
[0];
3677 if (param
< 1) param
= 1;
3679 if (this.y
< 0) this.y
= 0;
3686 * Cursor Character Absolute [column] (default = [row,1]) (CHA).
3688 Terminal
.prototype.cursorCharAbsolute = function(params
) {
3689 var param
= params
[0];
3690 if (param
< 1) param
= 1;
3697 * Insert Ps Line(s) (default = 1) (IL).
3699 Terminal
.prototype.insertLines = function(params
) {
3703 if (param
< 1) param
= 1;
3704 row
= this.y
+ this.ybase
;
3706 j
= this.rows
- 1 - this.scrollBottom
;
3707 j
= this.rows
- 1 + this.ybase
- j
+ 1;
3710 if (this.lines
.length
=== this.lines
.maxLength
) {
3711 // Trim the start of lines to make room for the new line
3712 this.lines
.trimStart(1);
3718 // test: echo -e '\e[44m\e[1L\e[0m'
3719 // blankLine(true) - xterm/linux behavior
3720 this.lines
.splice(row
, 0, this.blankLine(true));
3721 this.lines
.splice(j
, 1);
3725 this.updateRange(this.y
);
3726 this.updateRange(this.scrollBottom
);
3732 * Delete Ps Line(s) (default = 1) (DL).
3734 Terminal
.prototype.deleteLines = function(params
) {
3738 if (param
< 1) param
= 1;
3739 row
= this.y
+ this.ybase
;
3741 j
= this.rows
- 1 - this.scrollBottom
;
3742 j
= this.rows
- 1 + this.ybase
- j
;
3745 if (this.lines
.length
=== this.lines
.maxLength
) {
3746 // Trim the start of lines to make room for the new line
3747 this.lines
.trimStart(1);
3751 // test: echo -e '\e[44m\e[1M\e[0m'
3752 // blankLine(true) - xterm/linux behavior
3753 this.lines
.splice(j
+ 1, 0, this.blankLine(true));
3754 this.lines
.splice(row
, 1);
3758 this.updateRange(this.y
);
3759 this.updateRange(this.scrollBottom
);
3765 * Delete Ps Character(s) (default = 1) (DCH).
3767 Terminal
.prototype.deleteChars = function(params
) {
3771 if (param
< 1) param
= 1;
3773 row
= this.y
+ this.ybase
;
3774 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3777 this.lines
.get(row
).splice(this.x
, 1);
3778 this.lines
.get(row
).push(ch
);
3784 * Erase Ps Character(s) (default = 1) (ECH).
3786 Terminal
.prototype.eraseChars = function(params
) {
3787 var param
, row
, j
, ch
;
3790 if (param
< 1) param
= 1;
3792 row
= this.y
+ this.ybase
;
3794 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3796 while (param
-- && j
< this.cols
) {
3797 this.lines
.get(row
)[j
++] = ch
;
3802 * CSI Pm ` Character Position Absolute
3803 * [column] (default = [row,1]) (HPA).
3805 Terminal
.prototype.charPosAbsolute = function(params
) {
3806 var param
= params
[0];
3807 if (param
< 1) param
= 1;
3809 if (this.x
>= this.cols
) {
3810 this.x
= this.cols
- 1;
3817 * Horizontal Position Relative
3820 Terminal
.prototype.HPositionRelative = function(params
) {
3821 var param
= params
[0];
3822 if (param
< 1) param
= 1;
3824 if (this.x
>= this.cols
) {
3825 this.x
= this.cols
- 1;
3831 * CSI Ps c Send Device Attributes (Primary DA).
3832 * Ps = 0 or omitted -> request attributes from terminal. The
3833 * response depends on the decTerminalID resource setting.
3834 * -> CSI ? 1 ; 2 c (``VT100 with Advanced Video Option'')
3835 * -> CSI ? 1 ; 0 c (``VT101 with No Options'')
3836 * -> CSI ? 6 c (``VT102'')
3837 * -> CSI ? 6 0 ; 1 ; 2 ; 6 ; 8 ; 9 ; 1 5 ; c (``VT220'')
3838 * The VT100-style response parameters do not mean anything by
3839 * themselves. VT220 parameters do, telling the host what fea-
3840 * tures the terminal supports:
3841 * Ps = 1 -> 132-columns.
3842 * Ps = 2 -> Printer.
3843 * Ps = 6 -> Selective erase.
3844 * Ps = 8 -> User-defined keys.
3845 * Ps = 9 -> National replacement character sets.
3846 * Ps = 1 5 -> Technical characters.
3847 * Ps = 2 2 -> ANSI color, e.g., VT525.
3848 * Ps = 2 9 -> ANSI text locator (i.e., DEC Locator mode).
3850 * Send Device Attributes (Secondary DA).
3851 * Ps = 0 or omitted -> request the terminal's identification
3852 * code. The response depends on the decTerminalID resource set-
3853 * ting. It should apply only to VT220 and up, but xterm extends
3855 * -> CSI > Pp ; Pv ; Pc c
3856 * where Pp denotes the terminal type
3857 * Pp = 0 -> ``VT100''.
3858 * Pp = 1 -> ``VT220''.
3859 * and Pv is the firmware version (for xterm, this was originally
3860 * the XFree86 patch number, starting with 95). In a DEC termi-
3861 * nal, Pc indicates the ROM cartridge registration number and is
3864 * xterm/charproc.c - line 2012, for more information.
3865 * vim responds with ^[[?0c or ^[[?1c after the terminal's response (?)
3867 Terminal
.prototype.sendDeviceAttributes = function(params
) {
3868 if (params
[0] > 0) return;
3871 if (this.is('xterm')
3872 || this.is('rxvt-unicode')
3873 || this.is('screen')) {
3874 this.send(C0
.ESC
+ '[?1;2c');
3875 } else if (this.is('linux')) {
3876 this.send(C0
.ESC
+ '[?6c');
3878 } else if (this.prefix
=== '>') {
3880 // seem to spit this
3881 // out around ~370 times (?).
3882 if (this.is('xterm')) {
3883 this.send(C0
.ESC
+ '[>0;276;0c');
3884 } else if (this.is('rxvt-unicode')) {
3885 this.send(C0
.ESC
+ '[>85;95;0c');
3886 } else if (this.is('linux')) {
3887 // not supported by linux console.
3888 // linux console echoes parameters.
3889 this.send(params
[0] + 'c');
3890 } else if (this.is('screen')) {
3891 this.send(C0
.ESC
+ '[>83;40003;0c');
3899 * Line Position Absolute [row] (default = [1,column]) (VPA).
3901 Terminal
.prototype.linePosAbsolute = function(params
) {
3902 var param
= params
[0];
3903 if (param
< 1) param
= 1;
3905 if (this.y
>= this.rows
) {
3906 this.y
= this.rows
- 1;
3912 * 145 65 e * VPR - Vertical Position Relative
3915 Terminal
.prototype.VPositionRelative = function(params
) {
3916 var param
= params
[0];
3917 if (param
< 1) param
= 1;
3919 if (this.y
>= this.rows
) {
3920 this.y
= this.rows
- 1;
3927 * Horizontal and Vertical Position [row;column] (default =
3930 Terminal
.prototype.HVPosition = function(params
) {
3931 if (params
[0] < 1) params
[0] = 1;
3932 if (params
[1] < 1) params
[1] = 1;
3934 this.y
= params
[0] - 1;
3935 if (this.y
>= this.rows
) {
3936 this.y
= this.rows
- 1;
3939 this.x
= params
[1] - 1;
3940 if (this.x
>= this.cols
) {
3941 this.x
= this.cols
- 1;
3947 * CSI Pm h Set Mode (SM).
3948 * Ps = 2 -> Keyboard Action Mode (AM).
3949 * Ps = 4 -> Insert Mode (IRM).
3950 * Ps = 1 2 -> Send/receive (SRM).
3951 * Ps = 2 0 -> Automatic Newline (LNM).
3953 * DEC Private Mode Set (DECSET).
3954 * Ps = 1 -> Application Cursor Keys (DECCKM).
3955 * Ps = 2 -> Designate USASCII for character sets G0-G3
3956 * (DECANM), and set VT100 mode.
3957 * Ps = 3 -> 132 Column Mode (DECCOLM).
3958 * Ps = 4 -> Smooth (Slow) Scroll (DECSCLM).
3959 * Ps = 5 -> Reverse Video (DECSCNM).
3960 * Ps = 6 -> Origin Mode (DECOM).
3961 * Ps = 7 -> Wraparound Mode (DECAWM).
3962 * Ps = 8 -> Auto-repeat Keys (DECARM).
3963 * Ps = 9 -> Send Mouse X & Y on button press. See the sec-
3964 * tion Mouse Tracking.
3965 * Ps = 1 0 -> Show toolbar (rxvt).
3966 * Ps = 1 2 -> Start Blinking Cursor (att610).
3967 * Ps = 1 8 -> Print form feed (DECPFF).
3968 * Ps = 1 9 -> Set print extent to full screen (DECPEX).
3969 * Ps = 2 5 -> Show Cursor (DECTCEM).
3970 * Ps = 3 0 -> Show scrollbar (rxvt).
3971 * Ps = 3 5 -> Enable font-shifting functions (rxvt).
3972 * Ps = 3 8 -> Enter Tektronix Mode (DECTEK).
3973 * Ps = 4 0 -> Allow 80 -> 132 Mode.
3974 * Ps = 4 1 -> more(1) fix (see curses resource).
3975 * Ps = 4 2 -> Enable Nation Replacement Character sets (DECN-
3977 * Ps = 4 4 -> Turn On Margin Bell.
3978 * Ps = 4 5 -> Reverse-wraparound Mode.
3979 * Ps = 4 6 -> Start Logging. This is normally disabled by a
3980 * compile-time option.
3981 * Ps = 4 7 -> Use Alternate Screen Buffer. (This may be dis-
3982 * abled by the titeInhibit resource).
3983 * Ps = 6 6 -> Application keypad (DECNKM).
3984 * Ps = 6 7 -> Backarrow key sends backspace (DECBKM).
3985 * Ps = 1 0 0 0 -> Send Mouse X & Y on button press and
3986 * release. See the section Mouse Tracking.
3987 * Ps = 1 0 0 1 -> Use Hilite Mouse Tracking.
3988 * Ps = 1 0 0 2 -> Use Cell Motion Mouse Tracking.
3989 * Ps = 1 0 0 3 -> Use All Motion Mouse Tracking.
3990 * Ps = 1 0 0 4 -> Send FocusIn/FocusOut events.
3991 * Ps = 1 0 0 5 -> Enable Extended Mouse Mode.
3992 * Ps = 1 0 1 0 -> Scroll to bottom on tty output (rxvt).
3993 * Ps = 1 0 1 1 -> Scroll to bottom on key press (rxvt).
3994 * Ps = 1 0 3 4 -> Interpret "meta" key, sets eighth bit.
3995 * (enables the eightBitInput resource).
3996 * Ps = 1 0 3 5 -> Enable special modifiers for Alt and Num-
3997 * Lock keys. (This enables the numLock resource).
3998 * Ps = 1 0 3 6 -> Send ESC when Meta modifies a key. (This
3999 * enables the metaSendsEscape resource).
4000 * Ps = 1 0 3 7 -> Send DEL from the editing-keypad Delete
4002 * Ps = 1 0 3 9 -> Send ESC when Alt modifies a key. (This
4003 * enables the altSendsEscape resource).
4004 * Ps = 1 0 4 0 -> Keep selection even if not highlighted.
4005 * (This enables the keepSelection resource).
4006 * Ps = 1 0 4 1 -> Use the CLIPBOARD selection. (This enables
4007 * the selectToClipboard resource).
4008 * Ps = 1 0 4 2 -> Enable Urgency window manager hint when
4009 * Control-G is received. (This enables the bellIsUrgent
4011 * Ps = 1 0 4 3 -> Enable raising of the window when Control-G
4012 * is received. (enables the popOnBell resource).
4013 * Ps = 1 0 4 7 -> Use Alternate Screen Buffer. (This may be
4014 * disabled by the titeInhibit resource).
4015 * Ps = 1 0 4 8 -> Save cursor as in DECSC. (This may be dis-
4016 * abled by the titeInhibit resource).
4017 * Ps = 1 0 4 9 -> Save cursor as in DECSC and use Alternate
4018 * Screen Buffer, clearing it first. (This may be disabled by
4019 * the titeInhibit resource). This combines the effects of the 1
4020 * 0 4 7 and 1 0 4 8 modes. Use this with terminfo-based
4021 * applications rather than the 4 7 mode.
4022 * Ps = 1 0 5 0 -> Set terminfo/termcap function-key mode.
4023 * Ps = 1 0 5 1 -> Set Sun function-key mode.
4024 * Ps = 1 0 5 2 -> Set HP function-key mode.
4025 * Ps = 1 0 5 3 -> Set SCO function-key mode.
4026 * Ps = 1 0 6 0 -> Set legacy keyboard emulation (X11R6).
4027 * Ps = 1 0 6 1 -> Set VT220 keyboard emulation.
4028 * Ps = 2 0 0 4 -> Set bracketed paste mode.
4030 * http: *vt100.net/docs/vt220-rm/chapter4.html
4032 Terminal
.prototype.setMode = function(params
) {
4033 if (typeof params
=== 'object') {
4034 var l
= params
.length
4037 for (; i
< l
; i
++) {
4038 this.setMode(params
[i
]);
4047 this.insertMode
= true;
4050 //this.convertEol = true;
4053 } else if (this.prefix
=== '?') {
4056 this.applicationCursor
= true;
4059 this.setgCharset(0, Terminal
.charsets
.US
);
4060 this.setgCharset(1, Terminal
.charsets
.US
);
4061 this.setgCharset(2, Terminal
.charsets
.US
);
4062 this.setgCharset(3, Terminal
.charsets
.US
);
4063 // set VT100 mode here
4065 case 3: // 132 col mode
4066 this.savedCols
= this.cols
;
4067 this.resize(132, this.rows
);
4070 this.originMode
= true;
4073 this.wraparoundMode
= true;
4076 // this.cursorBlink = true;
4079 this.log('Serial port requested application keypad.');
4080 this.applicationKeypad
= true;
4081 this.viewport
.syncScrollArea();
4083 case 9: // X10 Mouse
4084 // no release, no motion, no wheel, no modifiers.
4085 case 1000: // vt200 mouse
4087 // no modifiers, except control on the wheel.
4088 case 1002: // button event mouse
4089 case 1003: // any event mouse
4090 // any event - sends motion events,
4091 // even if there is no button held down.
4092 this.x10Mouse
= params
=== 9;
4093 this.vt200Mouse
= params
=== 1000;
4094 this.normalMouse
= params
> 1000;
4095 this.mouseEvents
= true;
4096 this.element
.style
.cursor
= 'default';
4097 this.log('Binding to mouse events.');
4099 case 1004: // send focusin/focusout events
4102 this.sendFocus
= true;
4104 case 1005: // utf8 ext mode mouse
4105 this.utfMouse
= true;
4106 // for wide terminals
4107 // simply encodes large values as utf8 characters
4109 case 1006: // sgr ext mode mouse
4110 this.sgrMouse
= true;
4111 // for wide terminals
4112 // does not add 32 to fields
4113 // press: ^[[<b;x;yM
4114 // release: ^[[<b;x;ym
4116 case 1015: // urxvt ext mode mouse
4117 this.urxvtMouse
= true;
4118 // for wide terminals
4119 // numbers for fields
4121 // motion: ^[[b;x;yT
4123 case 25: // show cursor
4124 this.cursorHidden
= false;
4126 case 1049: // alt screen buffer cursor
4127 //this.saveCursor();
4129 case 47: // alt screen buffer
4130 case 1047: // alt screen buffer
4138 scrollTop
: this.scrollTop
,
4139 scrollBottom
: this.scrollBottom
,
4141 // XXX save charset(s) here?
4142 // charset: this.charset,
4143 // glevel: this.glevel,
4144 // charsets: this.charsets
4147 this.viewport
.syncScrollArea();
4148 this.normal
= normal
;
4157 * CSI Pm l Reset Mode (RM).
4158 * Ps = 2 -> Keyboard Action Mode (AM).
4159 * Ps = 4 -> Replace Mode (IRM).
4160 * Ps = 1 2 -> Send/receive (SRM).
4161 * Ps = 2 0 -> Normal Linefeed (LNM).
4163 * DEC Private Mode Reset (DECRST).
4164 * Ps = 1 -> Normal Cursor Keys (DECCKM).
4165 * Ps = 2 -> Designate VT52 mode (DECANM).
4166 * Ps = 3 -> 80 Column Mode (DECCOLM).
4167 * Ps = 4 -> Jump (Fast) Scroll (DECSCLM).
4168 * Ps = 5 -> Normal Video (DECSCNM).
4169 * Ps = 6 -> Normal Cursor Mode (DECOM).
4170 * Ps = 7 -> No Wraparound Mode (DECAWM).
4171 * Ps = 8 -> No Auto-repeat Keys (DECARM).
4172 * Ps = 9 -> Don't send Mouse X & Y on button press.
4173 * Ps = 1 0 -> Hide toolbar (rxvt).
4174 * Ps = 1 2 -> Stop Blinking Cursor (att610).
4175 * Ps = 1 8 -> Don't print form feed (DECPFF).
4176 * Ps = 1 9 -> Limit print to scrolling region (DECPEX).
4177 * Ps = 2 5 -> Hide Cursor (DECTCEM).
4178 * Ps = 3 0 -> Don't show scrollbar (rxvt).
4179 * Ps = 3 5 -> Disable font-shifting functions (rxvt).
4180 * Ps = 4 0 -> Disallow 80 -> 132 Mode.
4181 * Ps = 4 1 -> No more(1) fix (see curses resource).
4182 * Ps = 4 2 -> Disable Nation Replacement Character sets (DEC-
4184 * Ps = 4 4 -> Turn Off Margin Bell.
4185 * Ps = 4 5 -> No Reverse-wraparound Mode.
4186 * Ps = 4 6 -> Stop Logging. (This is normally disabled by a
4187 * compile-time option).
4188 * Ps = 4 7 -> Use Normal Screen Buffer.
4189 * Ps = 6 6 -> Numeric keypad (DECNKM).
4190 * Ps = 6 7 -> Backarrow key sends delete (DECBKM).
4191 * Ps = 1 0 0 0 -> Don't send Mouse X & Y on button press and
4192 * release. See the section Mouse Tracking.
4193 * Ps = 1 0 0 1 -> Don't use Hilite Mouse Tracking.
4194 * Ps = 1 0 0 2 -> Don't use Cell Motion Mouse Tracking.
4195 * Ps = 1 0 0 3 -> Don't use All Motion Mouse Tracking.
4196 * Ps = 1 0 0 4 -> Don't send FocusIn/FocusOut events.
4197 * Ps = 1 0 0 5 -> Disable Extended Mouse Mode.
4198 * Ps = 1 0 1 0 -> Don't scroll to bottom on tty output
4200 * Ps = 1 0 1 1 -> Don't scroll to bottom on key press (rxvt).
4201 * Ps = 1 0 3 4 -> Don't interpret "meta" key. (This disables
4202 * the eightBitInput resource).
4203 * Ps = 1 0 3 5 -> Disable special modifiers for Alt and Num-
4204 * Lock keys. (This disables the numLock resource).
4205 * Ps = 1 0 3 6 -> Don't send ESC when Meta modifies a key.
4206 * (This disables the metaSendsEscape resource).
4207 * Ps = 1 0 3 7 -> Send VT220 Remove from the editing-keypad
4209 * Ps = 1 0 3 9 -> Don't send ESC when Alt modifies a key.
4210 * (This disables the altSendsEscape resource).
4211 * Ps = 1 0 4 0 -> Do not keep selection when not highlighted.
4212 * (This disables the keepSelection resource).
4213 * Ps = 1 0 4 1 -> Use the PRIMARY selection. (This disables
4214 * the selectToClipboard resource).
4215 * Ps = 1 0 4 2 -> Disable Urgency window manager hint when
4216 * Control-G is received. (This disables the bellIsUrgent
4218 * Ps = 1 0 4 3 -> Disable raising of the window when Control-
4219 * G is received. (This disables the popOnBell resource).
4220 * Ps = 1 0 4 7 -> Use Normal Screen Buffer, clearing screen
4221 * first if in the Alternate Screen. (This may be disabled by
4222 * the titeInhibit resource).
4223 * Ps = 1 0 4 8 -> Restore cursor as in DECRC. (This may be
4224 * disabled by the titeInhibit resource).
4225 * Ps = 1 0 4 9 -> Use Normal Screen Buffer and restore cursor
4226 * as in DECRC. (This may be disabled by the titeInhibit
4227 * resource). This combines the effects of the 1 0 4 7 and 1 0
4228 * 4 8 modes. Use this with terminfo-based applications rather
4229 * than the 4 7 mode.
4230 * Ps = 1 0 5 0 -> Reset terminfo/termcap function-key mode.
4231 * Ps = 1 0 5 1 -> Reset Sun function-key mode.
4232 * Ps = 1 0 5 2 -> Reset HP function-key mode.
4233 * Ps = 1 0 5 3 -> Reset SCO function-key mode.
4234 * Ps = 1 0 6 0 -> Reset legacy keyboard emulation (X11R6).
4235 * Ps = 1 0 6 1 -> Reset keyboard emulation to Sun/PC style.
4236 * Ps = 2 0 0 4 -> Reset bracketed paste mode.
4238 Terminal
.prototype.resetMode = function(params
) {
4239 if (typeof params
=== 'object') {
4240 var l
= params
.length
4243 for (; i
< l
; i
++) {
4244 this.resetMode(params
[i
]);
4253 this.insertMode
= false;
4256 //this.convertEol = false;
4259 } else if (this.prefix
=== '?') {
4262 this.applicationCursor
= false;
4265 if (this.cols
=== 132 && this.savedCols
) {
4266 this.resize(this.savedCols
, this.rows
);
4268 delete this.savedCols
;
4271 this.originMode
= false;
4274 this.wraparoundMode
= false;
4277 // this.cursorBlink = false;
4280 this.log('Switching back to normal keypad.');
4281 this.applicationKeypad
= false;
4282 this.viewport
.syncScrollArea();
4284 case 9: // X10 Mouse
4285 case 1000: // vt200 mouse
4286 case 1002: // button event mouse
4287 case 1003: // any event mouse
4288 this.x10Mouse
= false;
4289 this.vt200Mouse
= false;
4290 this.normalMouse
= false;
4291 this.mouseEvents
= false;
4292 this.element
.style
.cursor
= '';
4294 case 1004: // send focusin/focusout events
4295 this.sendFocus
= false;
4297 case 1005: // utf8 ext mode mouse
4298 this.utfMouse
= false;
4300 case 1006: // sgr ext mode mouse
4301 this.sgrMouse
= false;
4303 case 1015: // urxvt ext mode mouse
4304 this.urxvtMouse
= false;
4306 case 25: // hide cursor
4307 this.cursorHidden
= true;
4309 case 1049: // alt screen buffer cursor
4311 case 47: // normal screen buffer
4312 case 1047: // normal screen buffer - clearing it first
4314 this.lines
= this.normal
.lines
;
4315 this.ybase
= this.normal
.ybase
;
4316 this.ydisp
= this.normal
.ydisp
;
4317 this.x
= this.normal
.x
;
4318 this.y
= this.normal
.y
;
4319 this.scrollTop
= this.normal
.scrollTop
;
4320 this.scrollBottom
= this.normal
.scrollBottom
;
4321 this.tabs
= this.normal
.tabs
;
4323 // if (params === 1049) {
4324 // this.x = this.savedX;
4325 // this.y = this.savedY;
4327 this.refresh(0, this.rows
- 1);
4328 this.viewport
.syncScrollArea();
4339 * Set Scrolling Region [top;bottom] (default = full size of win-
4343 Terminal
.prototype.setScrollRegion = function(params
) {
4344 if (this.prefix
) return;
4345 this.scrollTop
= (params
[0] || 1) - 1;
4346 this.scrollBottom
= (params
[1] || this.rows
) - 1;
4354 * Save cursor (ANSI.SYS).
4356 Terminal
.prototype.saveCursor = function(params
) {
4357 this.savedX
= this.x
;
4358 this.savedY
= this.y
;
4364 * Restore cursor (ANSI.SYS).
4366 Terminal
.prototype.restoreCursor = function(params
) {
4367 this.x
= this.savedX
|| 0;
4368 this.y
= this.savedY
|| 0;
4378 * Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
4380 Terminal
.prototype.cursorForwardTab = function(params
) {
4381 var param
= params
[0] || 1;
4383 this.x
= this.nextStop();
4389 * CSI Ps S Scroll up Ps lines (default = 1) (SU).
4391 Terminal
.prototype.scrollUp = function(params
) {
4392 var param
= params
[0] || 1;
4394 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
4395 this.lines
.splice(this.ybase
+ this.scrollBottom
, 0, this.blankLine());
4398 this.updateRange(this.scrollTop
);
4399 this.updateRange(this.scrollBottom
);
4404 * CSI Ps T Scroll down Ps lines (default = 1) (SD).
4406 Terminal
.prototype.scrollDown = function(params
) {
4407 var param
= params
[0] || 1;
4409 this.lines
.splice(this.ybase
+ this.scrollBottom
, 1);
4410 this.lines
.splice(this.ybase
+ this.scrollTop
, 0, this.blankLine());
4413 this.updateRange(this.scrollTop
);
4414 this.updateRange(this.scrollBottom
);
4419 * CSI Ps ; Ps ; Ps ; Ps ; Ps T
4420 * Initiate highlight mouse tracking. Parameters are
4421 * [func;startx;starty;firstrow;lastrow]. See the section Mouse
4424 Terminal
.prototype.initMouseTracking = function(params
) {
4425 // Relevant: DECSET 1001
4431 * Reset one or more features of the title modes to the default
4432 * value. Normally, "reset" disables the feature. It is possi-
4433 * ble to disable the ability to reset features by compiling a
4434 * different default for the title modes into xterm.
4435 * Ps = 0 -> Do not set window/icon labels using hexadecimal.
4436 * Ps = 1 -> Do not query window/icon labels using hexadeci-
4438 * Ps = 2 -> Do not set window/icon labels using UTF-8.
4439 * Ps = 3 -> Do not query window/icon labels using UTF-8.
4440 * (See discussion of "Title Modes").
4442 Terminal
.prototype.resetTitleModes = function(params
) {
4448 * CSI Ps Z Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
4450 Terminal
.prototype.cursorBackwardTab = function(params
) {
4451 var param
= params
[0] || 1;
4453 this.x
= this.prevStop();
4459 * CSI Ps b Repeat the preceding graphic character Ps times (REP).
4461 Terminal
.prototype.repeatPrecedingCharacter = function(params
) {
4462 var param
= params
[0] || 1
4463 , line
= this.lines
.get(this.ybase
+ this.y
)
4464 , ch
= line
[this.x
- 1] || [this.defAttr
, ' ', 1];
4466 while (param
--) line
[this.x
++] = ch
;
4471 * CSI Ps g Tab Clear (TBC).
4472 * Ps = 0 -> Clear Current Column (default).
4473 * Ps = 3 -> Clear All.
4475 * Ps = 2 -> Clear Stops on Line.
4476 * http://vt100.net/annarbor/aaa-ug/section6.html
4478 Terminal
.prototype.tabClear = function(params
) {
4479 var param
= params
[0];
4481 delete this.tabs
[this.x
];
4482 } else if (param
=== 3) {
4489 * CSI Pm i Media Copy (MC).
4490 * Ps = 0 -> Print screen (default).
4491 * Ps = 4 -> Turn off printer controller mode.
4492 * Ps = 5 -> Turn on printer controller mode.
4494 * Media Copy (MC, DEC-specific).
4495 * Ps = 1 -> Print line containing cursor.
4496 * Ps = 4 -> Turn off autoprint mode.
4497 * Ps = 5 -> Turn on autoprint mode.
4498 * Ps = 1 0 -> Print composed display, ignores DECPEX.
4499 * Ps = 1 1 -> Print all pages.
4501 Terminal
.prototype.mediaCopy = function(params
) {
4508 * Set or reset resource-values used by xterm to decide whether
4509 * to construct escape sequences holding information about the
4510 * modifiers pressed with a given key. The first parameter iden-
4511 * tifies the resource to set/reset. The second parameter is the
4512 * value to assign to the resource. If the second parameter is
4513 * omitted, the resource is reset to its initial value.
4514 * Ps = 1 -> modifyCursorKeys.
4515 * Ps = 2 -> modifyFunctionKeys.
4516 * Ps = 4 -> modifyOtherKeys.
4517 * If no parameters are given, all resources are reset to their
4520 Terminal
.prototype.setResources = function(params
) {
4527 * Disable modifiers which may be enabled via the CSI > Ps; Ps m
4528 * sequence. This corresponds to a resource value of "-1", which
4529 * cannot be set with the other sequence. The parameter identi-
4530 * fies the resource to be disabled:
4531 * Ps = 1 -> modifyCursorKeys.
4532 * Ps = 2 -> modifyFunctionKeys.
4533 * Ps = 4 -> modifyOtherKeys.
4534 * If the parameter is omitted, modifyFunctionKeys is disabled.
4535 * When modifyFunctionKeys is disabled, xterm uses the modifier
4536 * keys to make an extended sequence of functions rather than
4537 * adding a parameter to each function key to denote the modi-
4540 Terminal
.prototype.disableModifiers = function(params
) {
4547 * Set resource value pointerMode. This is used by xterm to
4548 * decide whether to hide the pointer cursor as the user types.
4549 * Valid values for the parameter:
4550 * Ps = 0 -> never hide the pointer.
4551 * Ps = 1 -> hide if the mouse tracking mode is not enabled.
4552 * Ps = 2 -> always hide the pointer. If no parameter is
4553 * given, xterm uses the default, which is 1 .
4555 Terminal
.prototype.setPointerMode = function(params
) {
4561 * CSI ! p Soft terminal reset (DECSTR).
4562 * http://vt100.net/docs/vt220-rm/table4-10.html
4564 Terminal
.prototype.softReset = function(params
) {
4565 this.cursorHidden
= false;
4566 this.insertMode
= false;
4567 this.originMode
= false;
4568 this.wraparoundMode
= false; // autowrap
4569 this.applicationKeypad
= false; // ?
4570 this.viewport
.syncScrollArea();
4571 this.applicationCursor
= false;
4573 this.scrollBottom
= this.rows
- 1;
4574 this.curAttr
= this.defAttr
;
4575 this.x
= this.y
= 0; // ?
4576 this.charset
= null;
4577 this.glevel
= 0; // ??
4578 this.charsets
= [null]; // ??
4584 * Request ANSI mode (DECRQM). For VT300 and up, reply is
4586 * where Ps is the mode number as in RM, and Pm is the mode
4588 * 0 - not recognized
4591 * 3 - permanently set
4592 * 4 - permanently reset
4594 Terminal
.prototype.requestAnsiMode = function(params
) {
4601 * Request DEC private mode (DECRQM). For VT300 and up, reply is
4603 * where Ps is the mode number as in DECSET, Pm is the mode value
4604 * as in the ANSI DECRQM.
4606 Terminal
.prototype.requestPrivateMode = function(params
) {
4613 * Set conformance level (DECSCL). Valid values for the first
4615 * Ps = 6 1 -> VT100.
4616 * Ps = 6 2 -> VT200.
4617 * Ps = 6 3 -> VT300.
4618 * Valid values for the second parameter:
4619 * Ps = 0 -> 8-bit controls.
4620 * Ps = 1 -> 7-bit controls (always set for VT100).
4621 * Ps = 2 -> 8-bit controls.
4623 Terminal
.prototype.setConformanceLevel = function(params
) {
4629 * CSI Ps q Load LEDs (DECLL).
4630 * Ps = 0 -> Clear all LEDS (default).
4631 * Ps = 1 -> Light Num Lock.
4632 * Ps = 2 -> Light Caps Lock.
4633 * Ps = 3 -> Light Scroll Lock.
4634 * Ps = 2 1 -> Extinguish Num Lock.
4635 * Ps = 2 2 -> Extinguish Caps Lock.
4636 * Ps = 2 3 -> Extinguish Scroll Lock.
4638 Terminal
.prototype.loadLEDs = function(params
) {
4645 * Set cursor style (DECSCUSR, VT520).
4646 * Ps = 0 -> blinking block.
4647 * Ps = 1 -> blinking block (default).
4648 * Ps = 2 -> steady block.
4649 * Ps = 3 -> blinking underline.
4650 * Ps = 4 -> steady underline.
4652 Terminal
.prototype.setCursorStyle = function(params
) {
4659 * Select character protection attribute (DECSCA). Valid values
4660 * for the parameter:
4661 * Ps = 0 -> DECSED and DECSEL can erase (default).
4662 * Ps = 1 -> DECSED and DECSEL cannot erase.
4663 * Ps = 2 -> DECSED and DECSEL can erase.
4665 Terminal
.prototype.setCharProtectionAttr = function(params
) {
4672 * Restore DEC Private Mode Values. The value of Ps previously
4673 * saved is restored. Ps values are the same as for DECSET.
4675 Terminal
.prototype.restorePrivateValues = function(params
) {
4681 * CSI Pt; Pl; Pb; Pr; Ps$ r
4682 * Change Attributes in Rectangular Area (DECCARA), VT400 and up.
4683 * Pt; Pl; Pb; Pr denotes the rectangle.
4684 * Ps denotes the SGR attributes to change: 0, 1, 4, 5, 7.
4685 * NOTE: xterm doesn't enable this code by default.
4687 Terminal
.prototype.setAttrInRectangle = function(params
) {
4697 for (; t
< b
+ 1; t
++) {
4698 line
= this.lines
.get(this.ybase
+ t
);
4699 for (i
= l
; i
< r
; i
++) {
4700 line
[i
] = [attr
, line
[i
][1]];
4705 this.updateRange(params
[0]);
4706 this.updateRange(params
[2]);
4711 * CSI Pc; Pt; Pl; Pb; Pr$ x
4712 * Fill Rectangular Area (DECFRA), VT420 and up.
4713 * Pc is the character to use.
4714 * Pt; Pl; Pb; Pr denotes the rectangle.
4715 * NOTE: xterm doesn't enable this code by default.
4717 Terminal
.prototype.fillRectangle = function(params
) {
4727 for (; t
< b
+ 1; t
++) {
4728 line
= this.lines
.get(this.ybase
+ t
);
4729 for (i
= l
; i
< r
; i
++) {
4730 line
[i
] = [line
[i
][0], String
.fromCharCode(ch
)];
4735 this.updateRange(params
[1]);
4736 this.updateRange(params
[3]);
4742 * Enable Locator Reporting (DECELR).
4743 * Valid values for the first parameter:
4744 * Ps = 0 -> Locator disabled (default).
4745 * Ps = 1 -> Locator enabled.
4746 * Ps = 2 -> Locator enabled for one report, then disabled.
4747 * The second parameter specifies the coordinate unit for locator
4749 * Valid values for the second parameter:
4750 * Pu = 0 <- or omitted -> default to character cells.
4751 * Pu = 1 <- device physical pixels.
4752 * Pu = 2 <- character cells.
4754 Terminal
.prototype.enableLocatorReporting = function(params
) {
4755 var val
= params
[0] > 0;
4756 //this.mouseEvents = val;
4757 //this.decLocator = val;
4762 * CSI Pt; Pl; Pb; Pr$ z
4763 * Erase Rectangular Area (DECERA), VT400 and up.
4764 * Pt; Pl; Pb; Pr denotes the rectangle.
4765 * NOTE: xterm doesn't enable this code by default.
4767 Terminal
.prototype.eraseRectangle = function(params
) {
4777 ch
= [this.eraseAttr(), ' ', 1]; // xterm?
4779 for (; t
< b
+ 1; t
++) {
4780 line
= this.lines
.get(this.ybase
+ t
);
4781 for (i
= l
; i
< r
; i
++) {
4787 this.updateRange(params
[0]);
4788 this.updateRange(params
[2]);
4794 * Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
4795 * NOTE: xterm doesn't enable this code by default.
4797 Terminal
.prototype.insertColumns = function() {
4798 var param
= params
[0]
4799 , l
= this.ybase
+ this.rows
4800 , ch
= [this.eraseAttr(), ' ', 1] // xterm?
4804 for (i
= this.ybase
; i
< l
; i
++) {
4805 this.lines
.get(i
).splice(this.x
+ 1, 0, ch
);
4806 this.lines
.get(i
).pop();
4816 * Delete P s Column(s) (default = 1) (DECDC), VT420 and up
4817 * NOTE: xterm doesn't enable this code by default.
4819 Terminal
.prototype.deleteColumns = function() {
4820 var param
= params
[0]
4821 , l
= this.ybase
+ this.rows
4822 , ch
= [this.eraseAttr(), ' ', 1] // xterm?
4826 for (i
= this.ybase
; i
< l
; i
++) {
4827 this.lines
.get(i
).splice(this.x
, 1);
4828 this.lines
.get(i
).push(ch
);
4839 Terminal
.charsets
= {};
4841 // DEC Special Character and Line Drawing Set.
4842 // http://vt100.net/docs/vt102-ug/table5-13.html
4843 // A lot of curses apps use this if they see TERM=xterm.
4844 // testing: echo -e '\e(0a\e(B'
4845 // The xterm output sometimes seems to conflict with the
4846 // reference above. xterm seems in line with the reference
4847 // when running vttest however.
4848 // The table below now uses xterm's output from vttest.
4849 Terminal
.charsets
.SCLD
= { // (0
4850 '`': '\u25c6', // '◆'
4851 'a': '\u2592', // '▒'
4852 'b': '\u0009', // '\t'
4853 'c': '\u000c', // '\f'
4854 'd': '\u000d', // '\r'
4855 'e': '\u000a', // '\n'
4856 'f': '\u00b0', // '°'
4857 'g': '\u00b1', // '±'
4858 'h': '\u2424', // '\u2424' (NL)
4859 'i': '\u000b', // '\v'
4860 'j': '\u2518', // '┘'
4861 'k': '\u2510', // '┐'
4862 'l': '\u250c', // '┌'
4863 'm': '\u2514', // '└'
4864 'n': '\u253c', // '┼'
4865 'o': '\u23ba', // '⎺'
4866 'p': '\u23bb', // '⎻'
4867 'q': '\u2500', // '─'
4868 'r': '\u23bc', // '⎼'
4869 's': '\u23bd', // '⎽'
4870 't': '\u251c', // '├'
4871 'u': '\u2524', // '┤'
4872 'v': '\u2534', // '┴'
4873 'w': '\u252c', // '┬'
4874 'x': '\u2502', // '│'
4875 'y': '\u2264', // '≤'
4876 'z': '\u2265', // '≥'
4877 '{': '\u03c0', // 'π'
4878 '|': '\u2260', // '≠'
4879 '}': '\u00a3', // '£'
4880 '~': '\u00b7' // '·'
4883 Terminal
.charsets
.UK
= null; // (A
4884 Terminal
.charsets
.US
= null; // (B (USASCII)
4885 Terminal
.charsets
.Dutch
= null; // (4
4886 Terminal
.charsets
.Finnish
= null; // (C or (5
4887 Terminal
.charsets
.French
= null; // (R
4888 Terminal
.charsets
.FrenchCanadian
= null; // (Q
4889 Terminal
.charsets
.German
= null; // (K
4890 Terminal
.charsets
.Italian
= null; // (Y
4891 Terminal
.charsets
.NorwegianDanish
= null; // (E or (6
4892 Terminal
.charsets
.Spanish
= null; // (Z
4893 Terminal
.charsets
.Swedish
= null; // (H or (7
4894 Terminal
.charsets
.Swiss
= null; // (=
4895 Terminal
.charsets
.ISOLatin
= null; // /A
4901 function on(el
, type
, handler
, capture
) {
4902 if (!Array
.isArray(el
)) {
4905 el
.forEach(function (element
) {
4906 element
.addEventListener(type
, handler
, capture
|| false);
4910 function off(el
, type
, handler
, capture
) {
4911 el
.removeEventListener(type
, handler
, capture
|| false);
4914 function cancel(ev
, force
) {
4915 if (!this.cancelEvents
&& !force
) {
4918 ev
.preventDefault();
4919 ev
.stopPropagation();
4923 function inherits(child
, parent
) {
4925 this.constructor = child
;
4927 f
.prototype = parent
.prototype;
4928 child
.prototype = new f
;
4931 // if bold is broken, we can't
4932 // use it in the terminal.
4933 function isBoldBroken(document
) {
4934 var body
= document
.getElementsByTagName('body')[0];
4935 var el
= document
.createElement('span');
4936 el
.innerHTML
= 'hello world';
4937 body
.appendChild(el
);
4938 var w1
= el
.scrollWidth
;
4939 el
.style
.fontWeight
= 'bold';
4940 var w2
= el
.scrollWidth
;
4941 body
.removeChild(el
);
4945 function indexOf(obj
, el
) {
4948 if (obj
[i
] === el
) return i
;
4953 function isThirdLevelShift(term
, ev
) {
4955 (term
.browser
.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) ||
4956 (term
.browser
.isMSWindows
&& ev
.altKey
&& ev
.ctrlKey
&& !ev
.metaKey
);
4958 if (ev
.type
== 'keypress') {
4959 return thirdLevelKey
;
4962 // Don't invoke for arrows, pageDown, home, backspace, etc. (on non-keypress events)
4963 return thirdLevelKey
&& (!ev
.keyCode
|| ev
.keyCode
> 47);
4966 function matchColor(r1
, g1
, b1
) {
4967 var hash
= (r1
<< 16) | (g1
<< 8) | b1
;
4969 if (matchColor
._cache
[hash
] != null) {
4970 return matchColor
._cache
[hash
];
4973 var ldiff
= Infinity
4982 for (; i
< Terminal
.vcolors
.length
; i
++) {
4983 c
= Terminal
.vcolors
[i
];
4988 diff
= matchColor
.distance(r1
, g1
, b1
, r2
, g2
, b2
);
5001 return matchColor
._cache
[hash
] = li
;
5004 matchColor
._cache
= {};
5006 // http://stackoverflow.com/questions/1633828
5007 matchColor
.distance = function(r1
, g1
, b1
, r2
, g2
, b2
) {
5008 return Math
.pow(30 * (r1
- r2
), 2)
5009 + Math
.pow(59 * (g1
- g2
), 2)
5010 + Math
.pow(11 * (b1
- b2
), 2);
5013 function each(obj
, iter
, con
) {
5014 if (obj
.forEach
) return obj
.forEach(iter
, con
);
5015 for (var i
= 0; i
< obj
.length
; i
++) {
5016 iter
.call(con
, obj
[i
], i
, obj
);
5020 function keys(obj
) {
5021 if (Object
.keys
) return Object
.keys(obj
);
5024 if (Object
.prototype.hasOwnProperty
.call(obj
, key
)) {
5031 var wcwidth
= (function(opts
) {
5032 // extracted from https://www.cl.cam.ac.uk/%7Emgk25/ucs/wcwidth.c
5033 // combining characters
5035 [0x0300, 0x036F], [0x0483, 0x0486], [0x0488, 0x0489],
5036 [0x0591, 0x05BD], [0x05BF, 0x05BF], [0x05C1, 0x05C2],
5037 [0x05C4, 0x05C5], [0x05C7, 0x05C7], [0x0600, 0x0603],
5038 [0x0610, 0x0615], [0x064B, 0x065E], [0x0670, 0x0670],
5039 [0x06D6, 0x06E4], [0x06E7, 0x06E8], [0x06EA, 0x06ED],
5040 [0x070F, 0x070F], [0x0711, 0x0711], [0x0730, 0x074A],
5041 [0x07A6, 0x07B0], [0x07EB, 0x07F3], [0x0901, 0x0902],
5042 [0x093C, 0x093C], [0x0941, 0x0948], [0x094D, 0x094D],
5043 [0x0951, 0x0954], [0x0962, 0x0963], [0x0981, 0x0981],
5044 [0x09BC, 0x09BC], [0x09C1, 0x09C4], [0x09CD, 0x09CD],
5045 [0x09E2, 0x09E3], [0x0A01, 0x0A02], [0x0A3C, 0x0A3C],
5046 [0x0A41, 0x0A42], [0x0A47, 0x0A48], [0x0A4B, 0x0A4D],
5047 [0x0A70, 0x0A71], [0x0A81, 0x0A82], [0x0ABC, 0x0ABC],
5048 [0x0AC1, 0x0AC5], [0x0AC7, 0x0AC8], [0x0ACD, 0x0ACD],
5049 [0x0AE2, 0x0AE3], [0x0B01, 0x0B01], [0x0B3C, 0x0B3C],
5050 [0x0B3F, 0x0B3F], [0x0B41, 0x0B43], [0x0B4D, 0x0B4D],
5051 [0x0B56, 0x0B56], [0x0B82, 0x0B82], [0x0BC0, 0x0BC0],
5052 [0x0BCD, 0x0BCD], [0x0C3E, 0x0C40], [0x0C46, 0x0C48],
5053 [0x0C4A, 0x0C4D], [0x0C55, 0x0C56], [0x0CBC, 0x0CBC],
5054 [0x0CBF, 0x0CBF], [0x0CC6, 0x0CC6], [0x0CCC, 0x0CCD],
5055 [0x0CE2, 0x0CE3], [0x0D41, 0x0D43], [0x0D4D, 0x0D4D],
5056 [0x0DCA, 0x0DCA], [0x0DD2, 0x0DD4], [0x0DD6, 0x0DD6],
5057 [0x0E31, 0x0E31], [0x0E34, 0x0E3A], [0x0E47, 0x0E4E],
5058 [0x0EB1, 0x0EB1], [0x0EB4, 0x0EB9], [0x0EBB, 0x0EBC],
5059 [0x0EC8, 0x0ECD], [0x0F18, 0x0F19], [0x0F35, 0x0F35],
5060 [0x0F37, 0x0F37], [0x0F39, 0x0F39], [0x0F71, 0x0F7E],
5061 [0x0F80, 0x0F84], [0x0F86, 0x0F87], [0x0F90, 0x0F97],
5062 [0x0F99, 0x0FBC], [0x0FC6, 0x0FC6], [0x102D, 0x1030],
5063 [0x1032, 0x1032], [0x1036, 0x1037], [0x1039, 0x1039],
5064 [0x1058, 0x1059], [0x1160, 0x11FF], [0x135F, 0x135F],
5065 [0x1712, 0x1714], [0x1732, 0x1734], [0x1752, 0x1753],
5066 [0x1772, 0x1773], [0x17B4, 0x17B5], [0x17B7, 0x17BD],
5067 [0x17C6, 0x17C6], [0x17C9, 0x17D3], [0x17DD, 0x17DD],
5068 [0x180B, 0x180D], [0x18A9, 0x18A9], [0x1920, 0x1922],
5069 [0x1927, 0x1928], [0x1932, 0x1932], [0x1939, 0x193B],
5070 [0x1A17, 0x1A18], [0x1B00, 0x1B03], [0x1B34, 0x1B34],
5071 [0x1B36, 0x1B3A], [0x1B3C, 0x1B3C], [0x1B42, 0x1B42],
5072 [0x1B6B, 0x1B73], [0x1DC0, 0x1DCA], [0x1DFE, 0x1DFF],
5073 [0x200B, 0x200F], [0x202A, 0x202E], [0x2060, 0x2063],
5074 [0x206A, 0x206F], [0x20D0, 0x20EF], [0x302A, 0x302F],
5075 [0x3099, 0x309A], [0xA806, 0xA806], [0xA80B, 0xA80B],
5076 [0xA825, 0xA826], [0xFB1E, 0xFB1E], [0xFE00, 0xFE0F],
5077 [0xFE20, 0xFE23], [0xFEFF, 0xFEFF], [0xFFF9, 0xFFFB],
5078 [0x10A01, 0x10A03], [0x10A05, 0x10A06], [0x10A0C, 0x10A0F],
5079 [0x10A38, 0x10A3A], [0x10A3F, 0x10A3F], [0x1D167, 0x1D169],
5080 [0x1D173, 0x1D182], [0x1D185, 0x1D18B], [0x1D1AA, 0x1D1AD],
5081 [0x1D242, 0x1D244], [0xE0001, 0xE0001], [0xE0020, 0xE007F],
5085 function bisearch(ucs
) {
5087 var max
= COMBINING
.length
- 1;
5089 if (ucs
< COMBINING
[0][0] || ucs
> COMBINING
[max
][1])
5091 while (max
>= min
) {
5092 mid
= Math
.floor((min
+ max
) / 2);
5093 if (ucs
> COMBINING
[mid
][1])
5095 else if (ucs
< COMBINING
[mid
][0])
5102 function wcwidth(ucs
) {
5103 // test for 8-bit control characters
5106 if (ucs
< 32 || (ucs
>= 0x7f && ucs
< 0xa0))
5107 return opts
.control
;
5108 // binary search in table of non-spacing characters
5111 // if we arrive here, ucs is not a combining or C0/C1 control character
5116 ucs
<= 0x115f || // Hangul Jamo init. consonants
5119 (ucs
>= 0x2e80 && ucs
<= 0xa4cf && ucs
!= 0x303f) || // CJK..Yi
5120 (ucs
>= 0xac00 && ucs
<= 0xd7a3) || // Hangul Syllables
5121 (ucs
>= 0xf900 && ucs
<= 0xfaff) || // CJK Compat Ideographs
5122 (ucs
>= 0xfe10 && ucs
<= 0xfe19) || // Vertical forms
5123 (ucs
>= 0xfe30 && ucs
<= 0xfe6f) || // CJK Compat Forms
5124 (ucs
>= 0xff00 && ucs
<= 0xff60) || // Fullwidth Forms
5125 (ucs
>= 0xffe0 && ucs
<= 0xffe6) ||
5126 (ucs
>= 0x20000 && ucs
<= 0x2fffd) ||
5127 (ucs
>= 0x30000 && ucs
<= 0x3fffd)
5132 })({nul
: 0, control
: 0}); // configurable options
5138 Terminal
.EventEmitter
= EventEmitter
;
5139 Terminal
.inherits
= inherits
;
5142 * Adds an event listener to the terminal.
5144 * @param {string} event The name of the event. TODO: Document all event types
5145 * @param {function} callback The function to call when the event is triggered.
5149 Terminal
.cancel
= cancel
;
5151 module
.exports
= Terminal
;