2 * xterm.js: xterm, in the browser
3 * Originally forked from (with the author's permission):
4 * Fabrice Bellard's javascript vt100 for jslinux:
5 * http://bellard.org/jslinux/
6 * Copyright (c) 2011 Fabrice Bellard
7 * The original design remains. The terminal itself
8 * has been extended to include xterm CSI codes, among
13 import { CompositionHelper
} from './CompositionHelper.js';
14 import { EventEmitter
} from './EventEmitter.js';
15 import { Viewport
} from './Viewport.js';
16 import { rightClickHandler
, pasteHandler
, copyHandler
} from './handlers/Clipboard.js';
17 import { CircularList
} from './utils/CircularList.js';
18 import * as Browser
from './utils/Browser';
19 import * as Keyboard
from './utils/Keyboard';
22 * Terminal Emulation References:
24 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.txt
25 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
26 * http://invisible-island.net/vttest/
27 * http://www.inwap.com/pdp10/ansicode.txt
28 * http://linux.die.net/man/4/console_codes
29 * http://linux.die.net/man/7/urxvt
32 // Let it work inside Node.js for automated testing purposes.
33 var document
= (typeof window
!= 'undefined') ? window
.document
: null;
38 var normal
= 0, escaped
= 1, csi
= 2, osc
= 3, charset
= 4, dcs
= 5, ignore
= 6;
45 * Creates a new `Terminal` object.
47 * @param {object} options An object containing a set of options, the available options are:
48 * - `cursorBlink` (boolean): Whether the terminal cursor blinks
49 * - `cols` (number): The number of columns of the terminal (horizontal size)
50 * - `rows` (number): The number of rows of the terminal (vertical size)
54 * @alias module:xterm/src/xterm
56 function Terminal(options
) {
59 if (!(this instanceof Terminal
)) {
60 return new Terminal(arguments
[0], arguments
[1], arguments
[2]);
63 self
.browser
= Browser
;
64 self
.cancel
= Terminal
.cancel
;
66 EventEmitter
.call(this);
68 if (typeof options
=== 'number') {
76 options
= options
|| {};
79 Object
.keys(Terminal
.defaults
).forEach(function(key
) {
80 if (options
[key
] == null) {
81 options
[key
] = Terminal
.options
[key
];
83 if (Terminal
[key
] !== Terminal
.defaults
[key
]) {
84 options
[key
] = Terminal
[key
];
87 self
[key
] = options
[key
];
90 if (options
.colors
.length
=== 8) {
91 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(8));
92 } else if (options
.colors
.length
=== 16) {
93 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(16));
94 } else if (options
.colors
.length
=== 10) {
95 options
.colors
= options
.colors
.slice(0, -2).concat(
96 Terminal
._colors
.slice(8, -2), options
.colors
.slice(-2));
97 } else if (options
.colors
.length
=== 18) {
98 options
.colors
= options
.colors
.concat(
99 Terminal
._colors
.slice(16, -2), options
.colors
.slice(-2));
101 this.colors
= options
.colors
;
103 this.options
= options
;
105 // this.context = options.context || window;
106 // this.document = options.document || document;
107 this.parent
= options
.body
|| options
.parent
|| (
108 document
? document
.getElementsByTagName('body')[0] : null
111 this.cols
= options
.cols
|| options
.geometry
[0];
112 this.rows
= options
.rows
|| options
.geometry
[1];
113 this.geometry
= [this.cols
, this.rows
];
115 if (options
.handler
) {
116 this.on('data', options
.handler
);
120 * The scroll position of the y cursor, ie. ybase + y = the y position within the entire
126 * The scroll position of the viewport
131 * The cursor's x position after ybase
136 * The cursor's y position after ybase
141 * Used to debounce the refresh function
143 this.isRefreshing
= false;
146 * Whether there is a full terminal refresh queued
149 this.cursorState
= 0;
150 this.cursorHidden
= false;
155 this.scrollBottom
= this.rows
- 1;
156 this.customKeydownHandler
= null;
159 this.applicationKeypad
= false;
160 this.applicationCursor
= false;
161 this.originMode
= false;
162 this.insertMode
= false;
163 this.wraparoundMode
= true; // defaults: xterm - true, vt100 - false
168 this.gcharset
= null;
170 this.charsets
= [null];
194 this.readable
= true;
195 this.writable
= true;
197 this.defAttr
= (0 << 18) | (257 << 9) | (256 << 0);
198 this.curAttr
= this.defAttr
;
201 this.currentParam
= 0;
205 // leftover surrogate high from previous write invocation
206 this.surrogate_high
= '';
209 * An array of all lines in the entire buffer, including the prompt. The lines are array of
210 * characters which are 2-length arrays where [0] is an attribute and [1] is the character.
212 this.lines
= new CircularList(this.scrollback
);
215 this.lines
.push(this.blankLine());
221 // Store if user went browsing history in scrollback
222 this.userScrolling
= false;
225 inherits(Terminal
, EventEmitter
);
228 * back_color_erase feature for xterm.
230 Terminal
.prototype.eraseAttr = function() {
231 // if (this.is('screen')) return this.defAttr;
232 return (this.defAttr
& ~0x1ff) | (this.curAttr
& 0x1ff);
240 Terminal
.tangoColors
= [
261 // Colors 0-15 + 16-255
262 // Much thanks to TooTallNate for writing this.
263 Terminal
.colors
= (function() {
264 var colors
= Terminal
.tangoColors
.slice()
265 , r
= [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff]
270 for (; i
< 216; i
++) {
271 out(r
[(i
/ 36) % 6 | 0], r
[(i
/ 6) % 6 | 0], r
[i
% 6]);
276 for (; i
< 24; i
++) {
281 function out(r
, g
, b
) {
282 colors
.push('#' + hex(r
) + hex(g
) + hex(b
));
287 return c
.length
< 2 ? '0' + c
: c
;
293 Terminal
._colors
= Terminal
.colors
.slice();
295 Terminal
.vcolors
= (function() {
297 , colors
= Terminal
.colors
301 for (; i
< 256; i
++) {
302 color
= parseInt(colors
[i
].substring(1), 16);
304 (color
>> 16) & 0xff,
317 Terminal
.defaults
= {
318 colors
: Terminal
.colors
,
330 // programFeatures: false,
334 Terminal
.options
= {};
336 Terminal
.focus
= null;
338 each(keys(Terminal
.defaults
), function(key
) {
339 Terminal
[key
] = Terminal
.defaults
[key
];
340 Terminal
.options
[key
] = Terminal
.defaults
[key
];
344 * Focus the terminal. Delegates focus handling to the terminal's DOM element.
346 Terminal
.prototype.focus = function() {
347 return this.textarea
.focus();
351 * Retrieves an option's value from the terminal.
352 * @param {string} key The option key.
354 Terminal
.prototype.getOption = function(key
, value
) {
355 if (!(key
in Terminal
.defaults
)) {
356 throw new Error('No option with key "' + key
+ '"');
359 if (typeof this.options
[key
] !== 'undefined') {
360 return this.options
[key
];
367 * Sets an option on the terminal.
368 * @param {string} key The option key.
369 * @param {string} value The option value.
371 Terminal
.prototype.setOption = function(key
, value
) {
372 if (!(key
in Terminal
.defaults
)) {
373 throw new Error('No option with key "' + key
+ '"');
376 this.options
[key
] = value
;
380 * Binds the desired focus behavior on a given terminal object.
384 Terminal
.bindFocus = function (term
) {
385 on(term
.textarea
, 'focus', function (ev
) {
386 if (term
.sendFocus
) {
389 term
.element
.classList
.add('focus');
391 Terminal
.focus
= term
;
392 term
.emit('focus', {terminal
: term
});
397 * Blur the terminal. Delegates blur handling to the terminal's DOM element.
399 Terminal
.prototype.blur = function() {
400 return this.textarea
.blur();
404 * Binds the desired blur behavior on a given terminal object.
408 Terminal
.bindBlur = function (term
) {
409 on(term
.textarea
, 'blur', function (ev
) {
410 term
.refresh(term
.y
, term
.y
);
411 if (term
.sendFocus
) {
414 term
.element
.classList
.remove('focus');
415 Terminal
.focus
= null;
416 term
.emit('blur', {terminal
: term
});
421 * Initialize default behavior
423 Terminal
.prototype.initGlobal = function() {
426 Terminal
.bindKeys(this);
427 Terminal
.bindFocus(this);
428 Terminal
.bindBlur(this);
430 // Bind clipboard functionality
431 on(this.element
, 'copy', function (ev
) {
432 copyHandler
.call(this, ev
, term
);
434 on(this.textarea
, 'paste', function (ev
) {
435 pasteHandler
.call(this, ev
, term
);
438 function rightClickHandlerWrapper (ev
) {
439 rightClickHandler
.call(this, ev
, term
);
442 if (term
.browser
.isFirefox
) {
443 on(this.element
, 'mousedown', function (ev
) {
444 if (ev
.button
== 2) {
445 rightClickHandlerWrapper(ev
);
449 on(this.element
, 'contextmenu', rightClickHandlerWrapper
);
454 * Apply key handling to the terminal
456 Terminal
.bindKeys = function(term
) {
457 on(term
.element
, 'keydown', function(ev
) {
458 if (document
.activeElement
!= this) {
464 on(term
.element
, 'keypress', function(ev
) {
465 if (document
.activeElement
!= this) {
471 on(term
.element
, 'keyup', term
.focus
.bind(term
));
473 on(term
.textarea
, 'keydown', function(ev
) {
477 on(term
.textarea
, 'keypress', function(ev
) {
479 // Truncate the textarea's value, since it is not needed
483 on(term
.textarea
, 'compositionstart', term
.compositionHelper
.compositionstart
.bind(term
.compositionHelper
));
484 on(term
.textarea
, 'compositionupdate', term
.compositionHelper
.compositionupdate
.bind(term
.compositionHelper
));
485 on(term
.textarea
, 'compositionend', term
.compositionHelper
.compositionend
.bind(term
.compositionHelper
));
486 term
.on('refresh', term
.compositionHelper
.updateCompositionElements
.bind(term
.compositionHelper
));
491 * Insert the given row to the terminal or produce a new one
492 * if no row argument is passed. Return the inserted row.
493 * @param {HTMLElement} row (optional) The row to append to the terminal.
495 Terminal
.prototype.insertRow = function (row
) {
496 if (typeof row
!= 'object') {
497 row
= document
.createElement('div');
500 this.rowContainer
.appendChild(row
);
501 this.children
.push(row
);
507 * Opens the terminal within an element.
509 * @param {HTMLElement} parent The element to create the terminal within.
511 Terminal
.prototype.open = function(parent
) {
512 var self
=this, i
=0, div
;
514 this.parent
= parent
|| this.parent
;
517 throw new Error('Terminal requires a parent element.');
520 // Grab global elements
521 this.context
= this.parent
.ownerDocument
.defaultView
;
522 this.document
= this.parent
.ownerDocument
;
523 this.body
= this.document
.getElementsByTagName('body')[0];
525 //Create main element container
526 this.element
= this.document
.createElement('div');
527 this.element
.classList
.add('terminal');
528 this.element
.classList
.add('xterm');
529 this.element
.classList
.add('xterm-theme-' + this.theme
);
531 this.element
.style
.height
532 this.element
.setAttribute('tabindex', 0);
534 this.viewportElement
= document
.createElement('div');
535 this.viewportElement
.classList
.add('xterm-viewport');
536 this.element
.appendChild(this.viewportElement
);
537 this.viewportScrollArea
= document
.createElement('div');
538 this.viewportScrollArea
.classList
.add('xterm-scroll-area');
539 this.viewportElement
.appendChild(this.viewportScrollArea
);
541 // Create the container that will hold the lines of the terminal and then
542 // produce the lines the lines.
543 this.rowContainer
= document
.createElement('div');
544 this.rowContainer
.classList
.add('xterm-rows');
545 this.element
.appendChild(this.rowContainer
);
548 // Create the container that will hold helpers like the textarea for
549 // capturing DOM Events. Then produce the helpers.
550 this.helperContainer
= document
.createElement('div');
551 this.helperContainer
.classList
.add('xterm-helpers');
552 // TODO: This should probably be inserted once it's filled to prevent an additional layout
553 this.element
.appendChild(this.helperContainer
);
554 this.textarea
= document
.createElement('textarea');
555 this.textarea
.classList
.add('xterm-helper-textarea');
556 this.textarea
.setAttribute('autocorrect', 'off');
557 this.textarea
.setAttribute('autocapitalize', 'off');
558 this.textarea
.setAttribute('spellcheck', 'false');
559 this.textarea
.tabIndex
= 0;
560 this.textarea
.addEventListener('focus', function() {
561 self
.emit('focus', {terminal
: self
});
563 this.textarea
.addEventListener('blur', function() {
564 self
.emit('blur', {terminal
: self
});
566 this.helperContainer
.appendChild(this.textarea
);
568 this.compositionView
= document
.createElement('div');
569 this.compositionView
.classList
.add('composition-view');
570 this.compositionHelper
= new CompositionHelper(this.textarea
, this.compositionView
, this);
571 this.helperContainer
.appendChild(this.compositionView
);
573 this.charMeasureElement
= document
.createElement('div');
574 this.charMeasureElement
.classList
.add('xterm-char-measure-element');
575 this.charMeasureElement
.innerHTML
= 'W';
576 this.helperContainer
.appendChild(this.charMeasureElement
);
578 for (; i
< this.rows
; i
++) {
581 this.parent
.appendChild(this.element
);
583 this.viewport
= new Viewport(this, this.viewportElement
, this.viewportScrollArea
, this.charMeasureElement
);
586 this.refresh(0, this.rows
- 1);
588 // Initialize global actions that
589 // need to be taken on the document.
592 // Ensure there is a Terminal.focus.
595 on(this.element
, 'click', function() {
596 var selection
= document
.getSelection(),
597 collapsed
= selection
.isCollapsed
,
598 isRange
= typeof collapsed
== 'boolean' ? !collapsed
: selection
.type
== 'Range';
604 // Listen for mouse events and translate
605 // them into terminal mouse protocols.
608 // Figure out whether boldness affects
609 // the character width of monospace fonts.
610 if (Terminal
.brokenBold
== null) {
611 Terminal
.brokenBold
= isBoldBroken(this.document
);
615 * This event is emitted when terminal has completed opening.
624 * Attempts to load an add-on using CommonJS or RequireJS (whichever is available).
625 * @param {string} addon The name of the addon to load
628 Terminal
.loadAddon = function(addon
, callback
) {
629 if (typeof exports
=== 'object' && typeof module
=== 'object') {
631 return require('./addons/' + addon
+ '/' + addon
);
632 } else if (typeof define
== 'function') {
634 return require(['./addons/' + addon
+ '/' + addon
], callback
);
636 console
.error('Cannot load a module without a CommonJS or RequireJS environment.');
644 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#Mouse%20Tracking
645 * To better understand these
646 * the xterm code is very helpful:
648 * button.c, charproc.c, misc.c
649 * Relevant functions in xterm/button.c:
650 * BtnCode, EmitButtonCode, EditorButton, SendMousePosition
652 Terminal
.prototype.bindMouse = function() {
653 var el
= this.element
, self
= this, pressed
= 32;
655 // mouseup, mousedown, wheel
656 // left click: ^[[M 3<^[[M#3<
658 function sendButton(ev
) {
662 // get the xterm-style button
663 button
= getButton(ev
);
665 // get mouse coordinates
669 sendEvent(button
, pos
);
671 switch (ev
.overrideType
|| ev
.type
) {
676 // keep it at the left
677 // button, just in case.
688 // motion example of a left click:
689 // ^[[M 3<^[[M@4<^[[M@5<^[[M@6<^[[M@7<^[[M#7<
690 function sendMove(ev
) {
697 // buttons marked as motions
698 // are incremented by 32
701 sendEvent(button
, pos
);
705 // position to characters
706 function encode(data
, ch
) {
707 if (!self
.utfMouse
) {
708 if (ch
=== 255) return data
.push(0);
709 if (ch
> 127) ch
= 127;
712 if (ch
=== 2047) return data
.push(0);
716 if (ch
> 2047) ch
= 2047;
717 data
.push(0xC0 | (ch
>> 6));
718 data
.push(0x80 | (ch
& 0x3F));
723 // send a mouse event:
724 // regular/utf8: ^[[M Cb Cx Cy
725 // urxvt: ^[[ Cb ; Cx ; Cy M
726 // sgr: ^[[ Cb ; Cx ; Cy M/m
727 // vt300: ^[[ 24(1/3/5)~ [ Cx , Cy ] \r
728 // locator: CSI P e ; P b ; P r ; P c ; P p & w
729 function sendEvent(button
, pos
) {
730 // self.emit('mouse', {
736 if (self
.vt300Mouse
) {
738 // http://www.vt100.net/docs/vt3xx-gp/chapter15.html
742 var data
= '\x1b[24';
743 if (button
=== 0) data
+= '1';
744 else if (button
=== 1) data
+= '3';
745 else if (button
=== 2) data
+= '5';
746 else if (button
=== 3) return;
748 data
+= '~[' + pos
.x
+ ',' + pos
.y
+ ']\r';
753 if (self
.decLocator
) {
758 if (button
=== 0) button
= 2;
759 else if (button
=== 1) button
= 4;
760 else if (button
=== 2) button
= 6;
761 else if (button
=== 3) button
= 3;
765 + (button
=== 3 ? 4 : 0)
776 if (self
.urxvtMouse
) {
781 self
.send('\x1b[' + button
+ ';' + pos
.x
+ ';' + pos
.y
+ 'M');
789 + (((button
& 3) === 3 ? button
& ~3 : button
) - 32)
794 + ((button
& 3) === 3 ? 'm' : 'M'));
800 encode(data
, button
);
804 self
.send('\x1b[M' + String
.fromCharCode
.apply(String
, data
));
807 function getButton(ev
) {
820 // 1, and 2 - with 64 added
821 switch (ev
.overrideType
|| ev
.type
) {
823 button
= ev
.button
!= null
829 if (self
.browser
.isMSIE
) {
830 button
= button
=== 1 ? 0 : button
=== 4 ? 1 : button
;
836 case 'DOMMouseScroll':
837 button
= ev
.detail
< 0
842 button
= ev
.wheelDeltaY
> 0
848 // next three bits are the modifiers:
849 // 4 = shift, 8 = meta, 16 = control
850 shift
= ev
.shiftKey
? 4 : 0;
851 meta
= ev
.metaKey
? 8 : 0;
852 ctrl
= ev
.ctrlKey
? 16 : 0;
853 mod
= shift
| meta
| ctrl
;
856 if (self
.vt200Mouse
) {
859 } else if (!self
.normalMouse
) {
864 button
= (32 + (mod
<< 2)) + button
;
869 // mouse coordinates measured in cols/rows
870 function getCoords(ev
) {
873 // ignore browsers without pageX for now
874 if (ev
.pageX
== null) return;
880 // should probably check offsetParent
881 // but this is more portable
882 while (el
&& el
!== self
.document
.documentElement
) {
885 el
= 'offsetParent' in el
890 // convert to cols/rows
891 w
= self
.element
.clientWidth
;
892 h
= self
.element
.clientHeight
;
893 x
= Math
.ceil((x
/ w
) * self
.cols
);
894 y
= Math
.ceil((y
/ h
) * self
.rows
);
896 // be sure to avoid sending
897 // bad positions to the program
899 if (x
> self
.cols
) x
= self
.cols
;
901 if (y
> self
.rows
) y
= self
.rows
;
903 // xterm sends raw bytes and
904 // starts at 32 (SP) for each.
915 on(el
, 'mousedown', function(ev
) {
916 if (!self
.mouseEvents
) return;
925 //if (self.vt200Mouse && !self.normalMouse) {
926 if (self
.vt200Mouse
) {
927 ev
.overrideType
= 'mouseup';
929 return self
.cancel(ev
);
933 if (self
.normalMouse
) on(self
.document
, 'mousemove', sendMove
);
935 // x10 compatibility mode can't send button releases
936 if (!self
.x10Mouse
) {
937 on(self
.document
, 'mouseup', function up(ev
) {
939 if (self
.normalMouse
) off(self
.document
, 'mousemove', sendMove
);
940 off(self
.document
, 'mouseup', up
);
941 return self
.cancel(ev
);
945 return self
.cancel(ev
);
948 //if (self.normalMouse) {
949 // on(self.document, 'mousemove', sendMove);
952 on(el
, 'wheel', function(ev
) {
953 if (!self
.mouseEvents
) return;
956 || self
.decLocator
) return;
958 return self
.cancel(ev
);
961 // allow wheel scrolling in
962 // the shell for example
963 on(el
, 'wheel', function(ev
) {
964 if (self
.mouseEvents
) return;
965 self
.viewport
.onWheel(ev
);
966 return self
.cancel(ev
);
971 * Destroys the terminal.
973 Terminal
.prototype.destroy = function() {
974 this.readable
= false;
975 this.writable
= false;
977 this.handler = function() {};
978 this.write = function() {};
979 if (this.element
.parentNode
) {
980 this.element
.parentNode
.removeChild(this.element
);
982 //this.emit('close');
987 * Flags used to render terminal text properly
998 * Refreshes (re-renders) terminal content within two rows (inclusive)
1002 * In the screen buffer, each character is stored as a an array with a character
1003 * and a 32-bit integer:
1004 * - First value: a utf-16 character.
1006 * - Next 9 bits: background color (0-511).
1007 * - Next 9 bits: foreground color (0-511).
1008 * - Next 14 bits: a mask for misc. flags:
1015 * @param {number} start The row to start from (between 0 and terminal's height terminal - 1)
1016 * @param {number} end The row to end at (between fromRow and terminal's height terminal - 1)
1017 * @param {boolean} queue Whether the refresh should ran right now or be queued
1019 Terminal
.prototype.refresh = function(start
, end
, queue
) {
1022 // queue defaults to true
1023 queue
= (typeof queue
== 'undefined') ? true : queue
;
1026 * The refresh queue allows refresh to execute only approximately 30 times a second. For
1027 * commands that pass a significant amount of output to the write function, this prevents the
1028 * terminal from maxing out the CPU and making the UI unresponsive. While commands can still
1029 * run beyond what they do on the terminal, it is far better with a debounce in place as
1030 * every single terminal manipulation does not need to be constructed in the DOM.
1032 * A side-effect of this is that it makes ^C to interrupt a process seem more responsive.
1035 // If refresh should be queued, order the refresh and return.
1036 if (this._refreshIsQueued
) {
1037 // If a refresh has already been queued, just order a full refresh next
1038 this._fullRefreshNext
= true;
1040 setTimeout(function () {
1041 self
.refresh(start
, end
, false);
1043 this._refreshIsQueued
= true;
1048 // If refresh should be run right now (not be queued), release the lock
1049 this._refreshIsQueued
= false;
1051 // If multiple refreshes were requested, make a full refresh.
1052 if (this._fullRefreshNext
) {
1054 end
= this.rows
- 1;
1055 this._fullRefreshNext
= false // reset lock
1058 var x
, y
, i
, line
, out
, ch
, ch_width
, width
, data
, attr
, bg
, fg
, flags
, row
, parent
, focused
= document
.activeElement
;
1060 // If this is a big refresh, remove the terminal rows from the DOM for faster calculations
1061 if (end
- start
>= this.rows
/ 2) {
1062 parent
= this.element
.parentNode
;
1064 this.element
.removeChild(this.rowContainer
);
1071 if (end
>= this.rows
.length
) {
1072 this.log('`end` is too large. Most likely a bad CSR.');
1073 end
= this.rows
.length
- 1;
1076 for (; y
<= end
; y
++) {
1077 row
= y
+ this.ydisp
;
1079 line
= this.lines
.get(row
);
1082 if (this.y
=== y
- (this.ybase
- this.ydisp
)
1084 && !this.cursorHidden
) {
1090 attr
= this.defAttr
;
1093 for (; i
< width
; i
++) {
1096 ch_width
= line
[i
][2];
1100 if (i
=== x
) data
= -1;
1102 if (data
!== attr
) {
1103 if (attr
!== this.defAttr
) {
1106 if (data
!== this.defAttr
) {
1108 out
+= '<span class="reverse-video terminal-cursor';
1109 if (this.cursorBlink
) {
1114 var classNames
= [];
1117 fg
= (data
>> 9) & 0x1ff;
1120 if (flags
& Terminal
.flags
.BOLD
) {
1121 if (!Terminal
.brokenBold
) {
1122 classNames
.push('xterm-bold');
1124 // See: XTerm*boldColors
1125 if (fg
< 8) fg
+= 8;
1128 if (flags
& Terminal
.flags
.UNDERLINE
) {
1129 classNames
.push('xterm-underline');
1132 if (flags
& Terminal
.flags
.BLINK
) {
1133 classNames
.push('xterm-blink');
1136 // If inverse flag is on, then swap the foreground and background variables.
1137 if (flags
& Terminal
.flags
.INVERSE
) {
1138 /* One-line variable swap in JavaScript: http://stackoverflow.com/a/16201730 */
1139 bg = [fg, fg = bg][0];
1140 // Should inverse just be before the
1141 // above boldColors effect instead?
1142 if ((flags & 1) && fg < 8) fg += 8;
1145 if (flags & Terminal.flags.INVISIBLE) {
1146 classNames.push('xterm-hidden');
1150 * Weird situation: Invert flag used black foreground and white background results
1151 * in invalid background color, positioned at the 256 index of the 256 terminal
1152 * color map. Pin the colors manually in such a case.
1154 * Source: https://github.com/sourcelair/xterm.js/issues/57
1156 if (flags
& Terminal
.flags
.INVERSE
) {
1166 classNames
.push('xterm-bg-color-' + bg
);
1170 classNames
.push('xterm-color-' + fg
);
1174 if (classNames
.length
) {
1175 out
+= ' class="' + classNames
.join(' ') + '"';
1204 if (attr
!== this.defAttr
) {
1208 this.children
[y
].innerHTML
= out
;
1212 this.element
.appendChild(this.rowContainer
);
1215 this.emit('refresh', {element
: this.element
, start
: start
, end
: end
});
1219 * Display the cursor element
1221 Terminal
.prototype.showCursor = function() {
1222 if (!this.cursorState
) {
1223 this.cursorState
= 1;
1224 this.refresh(this.y
, this.y
);
1229 * Scroll the terminal down 1 row, creating a blank line.
1231 Terminal
.prototype.scroll = function() {
1234 if (this.lines
.length
< this.lines
.maxLength
) {
1238 if (!this.userScrolling
) {
1239 this.ydisp
= this.ybase
;
1243 row
= this.ybase
+ this.rows
- 1;
1245 // subtract the bottom scroll region
1246 row
-= this.rows
- 1 - this.scrollBottom
;
1248 this.lines
.push(this.blankLine());
1250 if (this.scrollTop
!== 0) {
1251 if (this.ybase
!== 0) {
1253 if (!this.userScrolling
) {
1254 this.ydisp
= this.ybase
;
1257 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
1261 this.updateRange(this.scrollTop
);
1262 this.updateRange(this.scrollBottom
);
1265 * This event is emitted whenever the terminal is scrolled.
1266 * The one parameter passed is the new y display position.
1270 this.emit('scroll', this.ydisp
);
1274 * Scroll the display of the terminal
1275 * @param {number} disp The number of lines to scroll down (negatives scroll up).
1276 * @param {boolean} suppressScrollEvent Don't emit the scroll event as scrollDisp. This is used
1277 * to avoid unwanted events being handled by the veiwport when the event was triggered from the
1278 * viewport originally.
1280 Terminal
.prototype.scrollDisp = function(disp
, suppressScrollEvent
) {
1282 this.userScrolling
= true;
1283 } else if (disp
+ this.ydisp
>= this.ybase
) {
1284 this.userScrolling
= false;
1289 if (this.ydisp
> this.ybase
) {
1290 this.ydisp
= this.ybase
;
1291 } else if (this.ydisp
< 0) {
1295 if (!suppressScrollEvent
) {
1296 this.emit('scroll', this.ydisp
);
1299 this.refresh(0, this.rows
- 1);
1303 * Scroll the display of the terminal by a number of pages.
1304 * @param {number} pageCount The number of pages to scroll (negative scrolls up).
1306 Terminal
.prototype.scrollPages = function(pageCount
) {
1307 this.scrollDisp(pageCount
* (this.rows
- 1));
1311 * Scrolls the display of the terminal to the top.
1313 Terminal
.prototype.scrollToTop = function() {
1314 this.scrollDisp(-this.ydisp
);
1318 * Scrolls the display of the terminal to the bottom.
1320 Terminal
.prototype.scrollToBottom = function() {
1321 this.scrollDisp(this.ybase
- this.ydisp
);
1325 * Writes text to the terminal.
1326 * @param {string} text The text to write to the terminal.
1328 Terminal
.prototype.write = function(data
) {
1329 var l
= data
.length
, i
= 0, j
, cs
, ch
, code
, low
, ch_width
, row
;
1331 this.refreshStart
= this.y
;
1332 this.refreshEnd
= this.y
;
1334 // apply leftover surrogate high from last write
1335 if (this.surrogate_high
) {
1336 data
= this.surrogate_high
+ data
;
1337 this.surrogate_high
= '';
1340 for (; i
< l
; i
++) {
1343 // FIXME: higher chars than 0xa0 are not allowed in escape sequences
1344 // --> maybe move to default
1345 code
= data
.charCodeAt(i
);
1346 if (0xD800 <= code
&& code
<= 0xDBFF) {
1347 // we got a surrogate high
1348 // get surrogate low (next 2 bytes)
1349 low
= data
.charCodeAt(i
+1);
1351 // end of data stream, save surrogate high
1352 this.surrogate_high
= ch
;
1355 code
= ((code
- 0xD800) * 0x400) + (low
- 0xDC00) + 0x10000;
1356 ch
+= data
.charAt(i
+1);
1358 // surrogate low - already handled above
1359 if (0xDC00 <= code
&& code
<= 0xDFFF)
1361 switch (this.state
) {
1372 if (this.convertEol
) {
1376 if (this.y
> this.scrollBottom
) {
1396 this.x
= this.nextStop();
1411 this.state
= escaped
;
1416 // calculate print space
1417 // expensive call, therefore we save width in line buffer
1418 ch_width
= wcwidth(code
);
1421 if (this.charset
&& this.charset
[ch
]) {
1422 ch
= this.charset
[ch
];
1425 row
= this.y
+ this.ybase
;
1427 // insert combining char in last cell
1428 // FIXME: needs handling after cursor jumps
1429 if (!ch_width
&& this.x
) {
1430 // dont overflow left
1431 if (this.lines
.get(row
)[this.x
-1]) {
1432 if (!this.lines
.get(row
)[this.x
-1][2]) {
1434 // found empty cell after fullwidth, need to go 2 cells back
1435 if (this.lines
.get(row
)[this.x
-2])
1436 this.lines
.get(row
)[this.x
-2][1] += ch
;
1439 this.lines
.get(row
)[this.x
-1][1] += ch
;
1441 this.updateRange(this.y
);
1446 // goto next line if ch would overflow
1447 // TODO: needs a global min terminal width of 2
1448 if (this.x
+ch_width
-1 >= this.cols
) {
1449 // autowrap - DECAWM
1450 if (this.wraparoundMode
) {
1453 if (this.y
> this.scrollBottom
) {
1458 this.x
= this.cols
-1;
1459 if(ch_width
===2) // FIXME: check for xterm behavior
1463 row
= this.y
+ this.ybase
;
1465 // insert mode: move characters to right
1466 if (this.insertMode
) {
1467 // do this twice for a fullwidth char
1468 for (var moves
=0; moves
<ch_width
; ++moves
) {
1469 // remove last cell, if it's width is 0
1470 // we have to adjust the second last cell as well
1471 var removed
= this.lines
.get(this.y
+ this.ybase
).pop();
1473 && this.lines
.get(row
)[this.cols
-2]
1474 && this.lines
.get(row
)[this.cols
-2][2]===2)
1475 this.lines
.get(row
)[this.cols
-2] = [this.curAttr
, ' ', 1];
1477 // insert empty cell at cursor
1478 this.lines
.get(row
).splice(this.x
, 0, [this.curAttr
, ' ', 1]);
1482 this.lines
.get(row
)[this.x
] = [this.curAttr
, ch
, ch_width
];
1484 this.updateRange(this.y
);
1486 // fullwidth char - set next cell width to zero and advance cursor
1488 this.lines
.get(row
)[this.x
] = [this.curAttr
, '', 0];
1497 // ESC [ Control Sequence Introducer ( CSI is 0x9b).
1500 this.currentParam
= 0;
1504 // ESC ] Operating System Command ( OSC is 0x9d).
1507 this.currentParam
= 0;
1511 // ESC P Device Control String ( DCS is 0x90).
1514 this.currentParam
= 0;
1518 // ESC _ Application Program Command ( APC is 0x9f).
1520 this.state
= ignore
;
1523 // ESC ^ Privacy Message ( PM is 0x9e).
1525 this.state
= ignore
;
1528 // ESC c Full Reset (RIS).
1533 // ESC E Next Line ( NEL is 0x85).
1534 // ESC D Index ( IND is 0x84).
1542 // ESC M Reverse Index ( RI is 0x8d).
1544 this.reverseIndex();
1547 // ESC % Select default/utf-8 character set.
1548 // @ = default, G = utf-8
1550 //this.charset = null;
1552 this.setgCharset(0, Terminal
.charsets
.US
);
1553 this.state
= normal
;
1557 // ESC (,),*,+,-,. Designate G0-G2 Character Set.
1558 case '(': // <-- this seems to get all the attention
1584 this.state
= charset
;
1587 // Designate G3 Character Set (VT300).
1588 // A = ISO Latin-1 Supplemental.
1592 this.state
= charset
;
1597 // Single Shift Select of G2 Character Set
1598 // ( SS2 is 0x8e). This affects next character only.
1602 // Single Shift Select of G3 Character Set
1603 // ( SS3 is 0x8f). This affects next character only.
1607 // Invoke the G2 Character Set as GL (LS2).
1612 // Invoke the G3 Character Set as GL (LS3).
1617 // Invoke the G3 Character Set as GR (LS3R).
1622 // Invoke the G2 Character Set as GR (LS2R).
1627 // Invoke the G1 Character Set as GR (LS1R).
1632 // ESC 7 Save Cursor (DECSC).
1635 this.state
= normal
;
1638 // ESC 8 Restore Cursor (DECRC).
1640 this.restoreCursor();
1641 this.state
= normal
;
1644 // ESC # 3 DEC line height/width
1646 this.state
= normal
;
1650 // ESC H Tab Set (HTS is 0x88).
1655 // ESC = Application Keypad (DECKPAM).
1657 this.log('Serial port requested application keypad.');
1658 this.applicationKeypad
= true;
1659 this.viewport
.syncScrollArea();
1660 this.state
= normal
;
1663 // ESC > Normal Keypad (DECKPNM).
1665 this.log('Switching back to normal keypad.');
1666 this.applicationKeypad
= false;
1667 this.viewport
.syncScrollArea();
1668 this.state
= normal
;
1672 this.state
= normal
;
1673 this.error('Unknown ESC control: %s.', ch
);
1680 case '0': // DEC Special Character and Line Drawing Set.
1681 cs
= Terminal
.charsets
.SCLD
;
1684 cs
= Terminal
.charsets
.UK
;
1686 case 'B': // United States (USASCII).
1687 cs
= Terminal
.charsets
.US
;
1690 cs
= Terminal
.charsets
.Dutch
;
1692 case 'C': // Finnish
1694 cs
= Terminal
.charsets
.Finnish
;
1697 cs
= Terminal
.charsets
.French
;
1699 case 'Q': // FrenchCanadian
1700 cs
= Terminal
.charsets
.FrenchCanadian
;
1703 cs
= Terminal
.charsets
.German
;
1705 case 'Y': // Italian
1706 cs
= Terminal
.charsets
.Italian
;
1708 case 'E': // NorwegianDanish
1710 cs
= Terminal
.charsets
.NorwegianDanish
;
1712 case 'Z': // Spanish
1713 cs
= Terminal
.charsets
.Spanish
;
1715 case 'H': // Swedish
1717 cs
= Terminal
.charsets
.Swedish
;
1720 cs
= Terminal
.charsets
.Swiss
;
1722 case '/': // ISOLatin (actually /A)
1723 cs
= Terminal
.charsets
.ISOLatin
;
1727 cs
= Terminal
.charsets
.US
;
1730 this.setgCharset(this.gcharset
, cs
);
1731 this.gcharset
= null;
1732 this.state
= normal
;
1738 // Set Text Parameters.
1739 if (ch
=== '\x1b' || ch
=== '\x07') {
1740 if (ch
=== '\x1b') i
++;
1742 this.params
.push(this.currentParam
);
1744 switch (this.params
[0]) {
1748 if (this.params
[1]) {
1749 this.title
= this.params
[1];
1750 this.handleTitle(this.title
);
1758 // change dynamic colors
1770 // change dynamic ui colors
1782 // manipulate selection data
1800 this.currentParam
= 0;
1801 this.state
= normal
;
1803 if (!this.params
.length
) {
1804 if (ch
>= '0' && ch
<= '9') {
1806 this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1807 } else if (ch
=== ';') {
1808 this.params
.push(this.currentParam
);
1809 this.currentParam
= '';
1812 this.currentParam
+= ch
;
1819 if (ch
=== '?' || ch
=== '>' || ch
=== '!') {
1825 if (ch
>= '0' && ch
<= '9') {
1826 this.currentParam
= this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1830 // '$', '"', ' ', '\''
1831 if (ch
=== '$' || ch
=== '"' || ch
=== ' ' || ch
=== '\'') {
1836 this.params
.push(this.currentParam
);
1837 this.currentParam
= 0;
1840 if (ch
=== ';') break;
1842 this.state
= normal
;
1846 // Cursor Up Ps Times (default = 1) (CUU).
1848 this.cursorUp(this.params
);
1852 // Cursor Down Ps Times (default = 1) (CUD).
1854 this.cursorDown(this.params
);
1858 // Cursor Forward Ps Times (default = 1) (CUF).
1860 this.cursorForward(this.params
);
1864 // Cursor Backward Ps Times (default = 1) (CUB).
1866 this.cursorBackward(this.params
);
1870 // Cursor Position [row;column] (default = [1,1]) (CUP).
1872 this.cursorPos(this.params
);
1875 // CSI Ps J Erase in Display (ED).
1877 this.eraseInDisplay(this.params
);
1880 // CSI Ps K Erase in Line (EL).
1882 this.eraseInLine(this.params
);
1885 // CSI Pm m Character Attributes (SGR).
1888 this.charAttributes(this.params
);
1892 // CSI Ps n Device Status Report (DSR).
1895 this.deviceStatus(this.params
);
1904 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
1906 this.insertChars(this.params
);
1910 // Cursor Next Line Ps Times (default = 1) (CNL).
1912 this.cursorNextLine(this.params
);
1916 // Cursor Preceding Line Ps Times (default = 1) (CNL).
1918 this.cursorPrecedingLine(this.params
);
1922 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
1924 this.cursorCharAbsolute(this.params
);
1928 // Insert Ps Line(s) (default = 1) (IL).
1930 this.insertLines(this.params
);
1934 // Delete Ps Line(s) (default = 1) (DL).
1936 this.deleteLines(this.params
);
1940 // Delete Ps Character(s) (default = 1) (DCH).
1942 this.deleteChars(this.params
);
1946 // Erase Ps Character(s) (default = 1) (ECH).
1948 this.eraseChars(this.params
);
1951 // CSI Pm ` Character Position Absolute
1952 // [column] (default = [row,1]) (HPA).
1954 this.charPosAbsolute(this.params
);
1958 // Horizontal Position Relative
1960 this.HPositionRelative(this.params
);
1964 // Send Device Attributes (Primary DA).
1966 // Send Device Attributes (Secondary DA)
1968 this.sendDeviceAttributes(this.params
);
1972 // Line Position Absolute [row] (default = [1,column]) (VPA).
1974 this.linePosAbsolute(this.params
);
1977 // 145 65 e * VPR - Vertical Position Relative
1979 this.VPositionRelative(this.params
);
1983 // Horizontal and Vertical Position [row;column] (default =
1986 this.HVPosition(this.params
);
1989 // CSI Pm h Set Mode (SM).
1990 // CSI ? Pm h - mouse escape codes, cursor escape codes
1992 this.setMode(this.params
);
1995 // CSI Pm l Reset Mode (RM).
1998 this.resetMode(this.params
);
2002 // Set Scrolling Region [top;bottom] (default = full size of win-
2006 this.setScrollRegion(this.params
);
2010 // Save cursor (ANSI.SYS).
2012 this.saveCursor(this.params
);
2016 // Restore cursor (ANSI.SYS).
2018 this.restoreCursor(this.params
);
2026 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
2028 this.cursorForwardTab(this.params
);
2031 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
2033 this.scrollUp(this.params
);
2036 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
2037 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
2040 // if (this.prefix === '>') {
2041 // this.resetTitleModes(this.params);
2044 // if (this.params.length > 2) {
2045 // this.initMouseTracking(this.params);
2048 if (this.params
.length
< 2 && !this.prefix
) {
2049 this.scrollDown(this.params
);
2054 // Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
2056 this.cursorBackwardTab(this.params
);
2059 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
2061 this.repeatPrecedingCharacter(this.params
);
2064 // CSI Ps g Tab Clear (TBC).
2066 this.tabClear(this.params
);
2069 // CSI Pm i Media Copy (MC).
2072 // this.mediaCopy(this.params);
2075 // CSI Pm m Character Attributes (SGR).
2077 // case 'm': // duplicate
2078 // if (this.prefix === '>') {
2079 // this.setResources(this.params);
2081 // this.charAttributes(this.params);
2085 // CSI Ps n Device Status Report (DSR).
2087 // case 'n': // duplicate
2088 // if (this.prefix === '>') {
2089 // this.disableModifiers(this.params);
2091 // this.deviceStatus(this.params);
2095 // CSI > Ps p Set pointer mode.
2096 // CSI ! p Soft terminal reset (DECSTR).
2098 // Request ANSI mode (DECRQM).
2100 // Request DEC private mode (DECRQM).
2103 switch (this.prefix
) {
2105 // this.setPointerMode(this.params);
2108 this.softReset(this.params
);
2111 // if (this.postfix === '$') {
2112 // this.requestPrivateMode(this.params);
2116 // if (this.postfix === '"') {
2117 // this.setConformanceLevel(this.params);
2118 // } else if (this.postfix === '$') {
2119 // this.requestAnsiMode(this.params);
2125 // CSI Ps q Load LEDs (DECLL).
2129 // if (this.postfix === ' ') {
2130 // this.setCursorStyle(this.params);
2133 // if (this.postfix === '"') {
2134 // this.setCharProtectionAttr(this.params);
2137 // this.loadLEDs(this.params);
2141 // Set Scrolling Region [top;bottom] (default = full size of win-
2144 // CSI Pt; Pl; Pb; Pr; Ps$ r
2145 // case 'r': // duplicate
2146 // if (this.prefix === '?') {
2147 // this.restorePrivateValues(this.params);
2148 // } else if (this.postfix === '$') {
2149 // this.setAttrInRectangle(this.params);
2151 // this.setScrollRegion(this.params);
2155 // CSI s Save cursor (ANSI.SYS).
2157 // case 's': // duplicate
2158 // if (this.prefix === '?') {
2159 // this.savePrivateValues(this.params);
2161 // this.saveCursor(this.params);
2165 // CSI Ps ; Ps ; Ps t
2166 // CSI Pt; Pl; Pb; Pr; Ps$ t
2170 // if (this.postfix === '$') {
2171 // this.reverseAttrInRectangle(this.params);
2172 // } else if (this.postfix === ' ') {
2173 // this.setWarningBellVolume(this.params);
2175 // if (this.prefix === '>') {
2176 // this.setTitleModeFeature(this.params);
2178 // this.manipulateWindow(this.params);
2183 // CSI u Restore cursor (ANSI.SYS).
2185 // case 'u': // duplicate
2186 // if (this.postfix === ' ') {
2187 // this.setMarginBellVolume(this.params);
2189 // this.restoreCursor(this.params);
2193 // CSI Pt; Pl; Pb; Pr; Pp; Pt; Pl; Pp$ v
2195 // if (this.postfix === '$') {
2196 // this.copyRectagle(this.params);
2200 // CSI Pt ; Pl ; Pb ; Pr ' w
2202 // if (this.postfix === '\'') {
2203 // this.enableFilterRectangle(this.params);
2207 // CSI Ps x Request Terminal Parameters (DECREQTPARM).
2208 // CSI Ps x Select Attribute Change Extent (DECSACE).
2209 // CSI Pc; Pt; Pl; Pb; Pr$ x
2211 // if (this.postfix === '$') {
2212 // this.fillRectangle(this.params);
2214 // this.requestParameters(this.params);
2215 // //this.__(this.params);
2220 // CSI Pt; Pl; Pb; Pr$ z
2222 // if (this.postfix === '\'') {
2223 // this.enableLocatorReporting(this.params);
2224 // } else if (this.postfix === '$') {
2225 // this.eraseRectangle(this.params);
2230 // CSI Pt; Pl; Pb; Pr$ {
2232 // if (this.postfix === '\'') {
2233 // this.setLocatorEvents(this.params);
2234 // } else if (this.postfix === '$') {
2235 // this.selectiveEraseRectangle(this.params);
2241 // if (this.postfix === '\'') {
2242 // this.requestLocatorPosition(this.params);
2247 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
2249 // if (this.postfix === ' ') {
2250 // this.insertColumns(this.params);
2255 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
2257 // if (this.postfix === ' ') {
2258 // this.deleteColumns(this.params);
2263 this.error('Unknown CSI code: %s.', ch
);
2272 if (ch
=== '\x1b' || ch
=== '\x07') {
2273 if (ch
=== '\x1b') i
++;
2275 switch (this.prefix
) {
2276 // User-Defined Keys (DECUDK).
2280 // Request Status String (DECRQSS).
2281 // test: echo -e '\eP$q"p\e\\'
2283 var pt
= this.currentParam
2300 + (this.scrollTop
+ 1)
2302 + (this.scrollBottom
+ 1)
2312 this.error('Unknown DCS Pt: %s.', pt
);
2317 this.send('\x1bP' + +valid
+ '$r' + pt
+ '\x1b\\');
2320 // Set Termcap/Terminfo Data (xterm, experimental).
2324 // Request Termcap/Terminfo String (xterm, experimental)
2325 // Regular xterm does not even respond to this sequence.
2326 // This can cause a small glitch in vim.
2327 // test: echo -ne '\eP+q6b64\e\\'
2329 var pt
= this.currentParam
2332 this.send('\x1bP' + +valid
+ '+r' + pt
+ '\x1b\\');
2336 this.error('Unknown DCS prefix: %s.', this.prefix
);
2340 this.currentParam
= 0;
2342 this.state
= normal
;
2343 } else if (!this.currentParam
) {
2344 if (!this.prefix
&& ch
!== '$' && ch
!== '+') {
2345 this.currentParam
= ch
;
2346 } else if (this.prefix
.length
=== 2) {
2347 this.currentParam
= ch
;
2352 this.currentParam
+= ch
;
2358 if (ch
=== '\x1b' || ch
=== '\x07') {
2359 if (ch
=== '\x1b') i
++;
2360 this.state
= normal
;
2366 this.updateRange(this.y
);
2367 this.refresh(this.refreshStart
, this.refreshEnd
);
2371 * Writes text to the terminal, followed by a break line character (\n).
2372 * @param {string} text The text to write to the terminal.
2374 Terminal
.prototype.writeln = function(data
) {
2375 this.write(data
+ '\r\n');
2379 * Attaches a custom keydown handler which is run before keys are processed, giving consumers of
2380 * xterm.js ultimate control as to what keys should be processed by the terminal and what keys
2382 * @param {function} customKeydownHandler The custom KeyboardEvent handler to attach. This is a
2383 * function that takes a KeyboardEvent, allowing consumers to stop propogation and/or prevent
2384 * the default action. The function returns whether the event should be processed by xterm.js.
2386 Terminal
.prototype.attachCustomKeydownHandler = function(customKeydownHandler
) {
2387 this.customKeydownHandler
= customKeydownHandler
;
2391 * Handle a keydown event
2393 * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
2394 * @param {KeyboardEvent} ev The keydown event to be handled.
2396 Terminal
.prototype.keyDown = function(ev
) {
2397 if (this.customKeydownHandler
&& this.customKeydownHandler(ev
) === false) {
2401 if (!this.compositionHelper
.keydown
.bind(this.compositionHelper
)(ev
)) {
2402 if (this.ybase
!== this.ydisp
) {
2403 this.scrollToBottom();
2409 var result
= this.evaluateKeyEscapeSequence(ev
);
2411 if (result
.scrollDisp
) {
2412 this.scrollDisp(result
.scrollDisp
);
2413 return this.cancel(ev
, true);
2416 if (isThirdLevelShift(this, ev
)) {
2420 if (result
.cancel
) {
2421 // The event is canceled at the end already, is this necessary?
2422 this.cancel(ev
, true);
2429 this.emit('keydown', ev
);
2430 this.emit('key', result
.key
, ev
);
2432 this.handler(result
.key
);
2434 return this.cancel(ev
, true);
2438 * Returns an object that determines how a KeyboardEvent should be handled. The key of the
2439 * returned value is the new key code to pass to the PTY.
2441 * Reference: http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
2442 * @param {KeyboardEvent} ev The keyboard event to be translated to key escape sequence.
2444 Terminal
.prototype.evaluateKeyEscapeSequence = function(ev
) {
2446 // Whether to cancel event propogation (NOTE: this may not be needed since the event is
2447 // canceled at the end of keyDown
2449 // The new key even to emit
2451 // The number of characters to scroll, if this is defined it will cancel the event
2452 scrollDisp
: undefined
2454 var modifiers
= ev
.shiftKey
<< 0 | ev
.altKey
<< 1 | ev
.ctrlKey
<< 2 | ev
.metaKey
<< 3;
2455 switch (ev
.keyCode
) {
2459 result
.key
= '\x08'; // ^H
2462 result
.key
= '\x7f'; // ^?
2467 result
.key
= '\x1b[Z';
2471 result
.cancel
= true;
2476 result
.cancel
= true;
2480 result
.key
= '\x1b';
2481 result
.cancel
= true;
2486 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'D';
2487 // HACK: Make Alt + left-arrow behave like Ctrl + left-arrow: move one word backwards
2488 // http://unix.stackexchange.com/a/108106
2489 if (result
.key
== '\x1b[1;3D') {
2490 result
.key
= '\x1b[1;5D';
2492 } else if (this.applicationCursor
) {
2493 result
.key
= '\x1bOD';
2495 result
.key
= '\x1b[D';
2501 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'C';
2502 // HACK: Make Alt + right-arrow behave like Ctrl + right-arrow: move one word forward
2503 // http://unix.stackexchange.com/a/108106
2504 if (result
.key
== '\x1b[1;3C') {
2505 result
.key
= '\x1b[1;5C';
2507 } else if (this.applicationCursor
) {
2508 result
.key
= '\x1bOC';
2510 result
.key
= '\x1b[C';
2516 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'A';
2517 // HACK: Make Alt + up-arrow behave like Ctrl + up-arrow
2518 // http://unix.stackexchange.com/a/108106
2519 if (result
.key
== '\x1b[1;3A') {
2520 result
.key
= '\x1b[1;5A';
2522 } else if (this.applicationCursor
) {
2523 result
.key
= '\x1bOA';
2525 result
.key
= '\x1b[A';
2531 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'B';
2532 // HACK: Make Alt + down-arrow behave like Ctrl + down-arrow
2533 // http://unix.stackexchange.com/a/108106
2534 if (result
.key
== '\x1b[1;3B') {
2535 result
.key
= '\x1b[1;5B';
2537 } else if (this.applicationCursor
) {
2538 result
.key
= '\x1bOB';
2540 result
.key
= '\x1b[B';
2545 if (!ev
.shiftKey
&& !ev
.ctrlKey
) {
2546 // <Ctrl> or <Shift> + <Insert> are used to
2547 // copy-paste on some systems.
2548 result
.key
= '\x1b[2~';
2554 result
.key
= '\x1b[3;' + (modifiers
+ 1) + '~';
2556 result
.key
= '\x1b[3~';
2562 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'H';
2563 else if (this.applicationCursor
)
2564 result
.key
= '\x1bOH';
2566 result
.key
= '\x1b[H';
2571 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'F';
2572 else if (this.applicationCursor
)
2573 result
.key
= '\x1bOF';
2575 result
.key
= '\x1b[F';
2580 result
.scrollDisp
= -(this.rows
- 1);
2582 result
.key
= '\x1b[5~';
2588 result
.scrollDisp
= this.rows
- 1;
2590 result
.key
= '\x1b[6~';
2596 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'P';
2598 result
.key
= '\x1bOP';
2603 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'Q';
2605 result
.key
= '\x1bOQ';
2610 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'R';
2612 result
.key
= '\x1bOR';
2617 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'S';
2619 result
.key
= '\x1bOS';
2624 result
.key
= '\x1b[15;' + (modifiers
+ 1) + '~';
2626 result
.key
= '\x1b[15~';
2631 result
.key
= '\x1b[17;' + (modifiers
+ 1) + '~';
2633 result
.key
= '\x1b[17~';
2638 result
.key
= '\x1b[18;' + (modifiers
+ 1) + '~';
2640 result
.key
= '\x1b[18~';
2645 result
.key
= '\x1b[19;' + (modifiers
+ 1) + '~';
2647 result
.key
= '\x1b[19~';
2652 result
.key
= '\x1b[20;' + (modifiers
+ 1) + '~';
2654 result
.key
= '\x1b[20~';
2659 result
.key
= '\x1b[21;' + (modifiers
+ 1) + '~';
2661 result
.key
= '\x1b[21~';
2666 result
.key
= '\x1b[23;' + (modifiers
+ 1) + '~';
2668 result
.key
= '\x1b[23~';
2673 result
.key
= '\x1b[24;' + (modifiers
+ 1) + '~';
2675 result
.key
= '\x1b[24~';
2680 if (ev
.ctrlKey
&& !ev
.shiftKey
&& !ev
.altKey
&& !ev
.metaKey
) {
2681 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2682 result
.key
= String
.fromCharCode(ev
.keyCode
- 64);
2683 } else if (ev
.keyCode
=== 32) {
2685 result
.key
= String
.fromCharCode(0);
2686 } else if (ev
.keyCode
>= 51 && ev
.keyCode
<= 55) {
2687 // escape, file sep, group sep, record sep, unit sep
2688 result
.key
= String
.fromCharCode(ev
.keyCode
- 51 + 27);
2689 } else if (ev
.keyCode
=== 56) {
2691 result
.key
= String
.fromCharCode(127);
2692 } else if (ev
.keyCode
=== 219) {
2693 // ^[ - Control Sequence Introducer (CSI)
2694 result
.key
= String
.fromCharCode(27);
2695 } else if (ev
.keyCode
=== 220) {
2696 // ^\ - String Terminator (ST)
2697 result
.key
= String
.fromCharCode(28);
2698 } else if (ev
.keyCode
=== 221) {
2699 // ^] - Operating System Command (OSC)
2700 result
.key
= String
.fromCharCode(29);
2702 } else if (!this.browser
.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) {
2703 // On Mac this is a third level shift. Use <Esc> instead.
2704 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2705 result
.key
= '\x1b' + String
.fromCharCode(ev
.keyCode
+ 32);
2706 } else if (ev
.keyCode
=== 192) {
2707 result
.key
= '\x1b`';
2708 } else if (ev
.keyCode
>= 48 && ev
.keyCode
<= 57) {
2709 result
.key
= '\x1b' + (ev
.keyCode
- 48);
2718 * Set the G level of the terminal
2721 Terminal
.prototype.setgLevel = function(g
) {
2723 this.charset
= this.charsets
[g
];
2727 * Set the charset for the given G level of the terminal
2731 Terminal
.prototype.setgCharset = function(g
, charset
) {
2732 this.charsets
[g
] = charset
;
2733 if (this.glevel
=== g
) {
2734 this.charset
= charset
;
2739 * Handle a keypress event.
2741 * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
2742 * @param {KeyboardEvent} ev The keypress event to be handled.
2744 Terminal
.prototype.keyPress = function(ev
) {
2751 } else if (ev
.which
== null) {
2753 } else if (ev
.which
!== 0 && ev
.charCode
!== 0) {
2760 (ev
.altKey
|| ev
.ctrlKey
|| ev
.metaKey
) && !isThirdLevelShift(this, ev
)
2765 key
= String
.fromCharCode(key
);
2767 this.emit('keypress', key
, ev
);
2768 this.emit('key', key
, ev
);
2776 * Send data for handling to the terminal
2777 * @param {string} data
2779 Terminal
.prototype.send = function(data
) {
2783 setTimeout(function() {
2784 self
.handler(self
.queue
);
2794 * Note: We could do sweet things with webaudio here
2796 Terminal
.prototype.bell = function() {
2797 if (!this.visualBell
) return;
2799 this.element
.style
.borderColor
= 'white';
2800 setTimeout(function() {
2801 self
.element
.style
.borderColor
= '';
2803 if (this.popOnBell
) this.focus();
2807 * Log the current state to the console.
2809 Terminal
.prototype.log = function() {
2810 if (!this.debug
) return;
2811 if (!this.context
.console
|| !this.context
.console
.log
) return;
2812 var args
= Array
.prototype.slice
.call(arguments
);
2813 this.context
.console
.log
.apply(this.context
.console
, args
);
2817 * Log the current state as error to the console.
2819 Terminal
.prototype.error = function() {
2820 if (!this.debug
) return;
2821 if (!this.context
.console
|| !this.context
.console
.error
) return;
2822 var args
= Array
.prototype.slice
.call(arguments
);
2823 this.context
.console
.error
.apply(this.context
.console
, args
);
2827 * Resizes the terminal.
2829 * @param {number} x The number of columns to resize to.
2830 * @param {number} y The number of rows to resize to.
2832 Terminal
.prototype.resize = function(x
, y
) {
2840 if (x
=== this.cols
&& y
=== this.rows
) {
2850 ch
= [this.defAttr
, ' ', 1]; // does xterm use the default attr?
2851 i
= this.lines
.length
;
2853 while (this.lines
.get(i
).length
< x
) {
2854 this.lines
.get(i
).push(ch
);
2858 i
= this.lines
.length
;
2860 while (this.lines
.get(i
).length
> x
) {
2861 this.lines
.get(i
).pop();
2874 // y is rows, not this.y
2875 if (this.lines
.length
< y
+ this.ybase
) {
2876 if (this.ybase
> 0 && this.lines
.length
<= this.ybase
+ this.y
+ addToY
+ 1) {
2877 // There is room above the buffer and there are no empty elements below the line,
2881 if (this.ydisp
> 0) {
2882 // Viewport is at the top of the buffer, must increase downwards
2886 // Add a blank line if there is no buffer left at the top to scroll to, or if there
2887 // are blank lines after the cursor
2888 this.lines
.push(this.blankLine());
2891 if (this.children
.length
< y
) {
2897 if (this.lines
.length
> y
+ this.ybase
) {
2898 if (this.lines
.length
> this.ybase
+ this.y
+ 1) {
2899 // The line is a blank line below the cursor, remove it
2902 // The line is the cursor, scroll down
2907 if (this.children
.length
> y
) {
2908 el
= this.children
.shift();
2910 el
.parentNode
.removeChild(el
);
2916 // Make sure that the cursor stays on screen
2929 this.scrollBottom
= y
- 1;
2931 this.refresh(0, this.rows
- 1);
2935 this.geometry
= [this.cols
, this.rows
];
2936 this.emit('resize', {terminal
: this, cols
: x
, rows
: y
});
2940 * Updates the range of rows to refresh
2941 * @param {number} y The number of rows to refresh next.
2943 Terminal
.prototype.updateRange = function(y
) {
2944 if (y
< this.refreshStart
) this.refreshStart
= y
;
2945 if (y
> this.refreshEnd
) this.refreshEnd
= y
;
2946 // if (y > this.refreshEnd) {
2947 // this.refreshEnd = y;
2948 // if (y > this.rows - 1) {
2949 // this.refreshEnd = this.rows - 1;
2955 * Set the range of refreshing to the maximum value
2957 Terminal
.prototype.maxRange = function() {
2958 this.refreshStart
= 0;
2959 this.refreshEnd
= this.rows
- 1;
2965 * Setup the tab stops.
2968 Terminal
.prototype.setupStops = function(i
) {
2970 if (!this.tabs
[i
]) {
2971 i
= this.prevStop(i
);
2978 for (; i
< this.cols
; i
+= 8) {
2979 this.tabs
[i
] = true;
2985 * Move the cursor to the previous tab stop from the given position (default is current).
2986 * @param {number} x The position to move the cursor to the previous tab stop.
2988 Terminal
.prototype.prevStop = function(x
) {
2989 if (x
== null) x
= this.x
;
2990 while (!this.tabs
[--x
] && x
> 0);
2991 return x
>= this.cols
2998 * Move the cursor one tab stop forward from the given position (default is current).
2999 * @param {number} x The position to move the cursor one tab stop forward.
3001 Terminal
.prototype.nextStop = function(x
) {
3002 if (x
== null) x
= this.x
;
3003 while (!this.tabs
[++x
] && x
< this.cols
);
3004 return x
>= this.cols
3011 * Erase in the identified line everything from "x" to the end of the line (right).
3012 * @param {number} x The column from which to start erasing to the end of the line.
3013 * @param {number} y The line in which to operate.
3015 Terminal
.prototype.eraseRight = function(x
, y
) {
3016 var line
= this.lines
.get(this.ybase
+ y
)
3017 , ch
= [this.eraseAttr(), ' ', 1]; // xterm
3020 for (; x
< this.cols
; x
++) {
3024 this.updateRange(y
);
3030 * Erase in the identified line everything from "x" to the start of the line (left).
3031 * @param {number} x The column from which to start erasing to the start of the line.
3032 * @param {number} y The line in which to operate.
3034 Terminal
.prototype.eraseLeft = function(x
, y
) {
3035 var line
= this.lines
.get(this.ybase
+ y
)
3036 , ch
= [this.eraseAttr(), ' ', 1]; // xterm
3039 while (x
--) line
[x
] = ch
;
3041 this.updateRange(y
);
3045 * Clears the entire buffer, making the prompt line the new first line.
3047 Terminal
.prototype.clear = function() {
3048 if (this.ybase
=== 0 && this.y
=== 0) {
3049 // Don't clear if it's already clear
3052 this.lines
.set(0, this.lines
.get(this.ybase
+ this.y
));
3053 this.lines
.length
= 1;
3057 for (var i
= 1; i
< this.rows
; i
++) {
3058 this.lines
.push(this.blankLine());
3060 this.refresh(0, this.rows
- 1);
3061 this.emit('scroll', this.ydisp
);
3065 * Erase all content in the given line
3066 * @param {number} y The line to erase all of its contents.
3068 Terminal
.prototype.eraseLine = function(y
) {
3069 this.eraseRight(0, y
);
3074 * Return the data array of a blank line/
3075 * @param {number} cur First bunch of data for each "blank" character.
3077 Terminal
.prototype.blankLine = function(cur
) {
3082 var ch
= [attr
, ' ', 1] // width defaults to 1 halfwidth character
3086 for (; i
< this.cols
; i
++) {
3095 * If cur return the back color xterm feature attribute. Else return defAttr.
3096 * @param {object} cur
3098 Terminal
.prototype.ch = function(cur
) {
3100 ? [this.eraseAttr(), ' ', 1]
3101 : [this.defAttr
, ' ', 1];
3106 * Evaluate if the current erminal is the given argument.
3107 * @param {object} term The terminal to evaluate
3109 Terminal
.prototype.is = function(term
) {
3110 var name
= this.termName
;
3111 return (name
+ '').indexOf(term
) === 0;
3116 * Emit the 'data' event and populate the given data.
3117 * @param {string} data The data to populate in the event.
3119 Terminal
.prototype.handler = function(data
) {
3120 // Input is being sent to the terminal, the terminal should focus the prompt.
3121 if (this.ybase
!== this.ydisp
) {
3122 this.scrollToBottom();
3124 this.emit('data', data
);
3129 * Emit the 'title' event and populate the given title.
3130 * @param {string} title The title to populate in the event.
3132 Terminal
.prototype.handleTitle = function(title
) {
3134 * This event is emitted when the title of the terminal is changed
3135 * from inside the terminal. The parameter is the new title.
3139 this.emit('title', title
);
3148 * ESC D Index (IND is 0x84).
3150 Terminal
.prototype.index = function() {
3152 if (this.y
> this.scrollBottom
) {
3156 this.state
= normal
;
3161 * ESC M Reverse Index (RI is 0x8d).
3163 Terminal
.prototype.reverseIndex = function() {
3166 if (this.y
< this.scrollTop
) {
3168 // possibly move the code below to term.reverseScroll();
3169 // test: echo -ne '\e[1;1H\e[44m\eM\e[0m'
3170 // blankLine(true) is xterm/linux behavior
3171 this.lines
.splice(this.y
+ this.ybase
, 0, this.blankLine(true));
3172 j
= this.rows
- 1 - this.scrollBottom
;
3173 this.lines
.splice(this.rows
- 1 + this.ybase
- j
+ 1, 1);
3175 this.updateRange(this.scrollTop
);
3176 this.updateRange(this.scrollBottom
);
3178 this.state
= normal
;
3183 * ESC c Full Reset (RIS).
3185 Terminal
.prototype.reset = function() {
3186 this.options
.rows
= this.rows
;
3187 this.options
.cols
= this.cols
;
3188 var customKeydownHandler
= this.customKeydownHandler
;
3189 Terminal
.call(this, this.options
);
3190 this.customKeydownHandler
= customKeydownHandler
;
3191 this.refresh(0, this.rows
- 1);
3192 this.viewport
.syncScrollArea();
3197 * ESC H Tab Set (HTS is 0x88).
3199 Terminal
.prototype.tabSet = function() {
3200 this.tabs
[this.x
] = true;
3201 this.state
= normal
;
3211 * Cursor Up Ps Times (default = 1) (CUU).
3213 Terminal
.prototype.cursorUp = function(params
) {
3214 var param
= params
[0];
3215 if (param
< 1) param
= 1;
3217 if (this.y
< 0) this.y
= 0;
3223 * Cursor Down Ps Times (default = 1) (CUD).
3225 Terminal
.prototype.cursorDown = function(params
) {
3226 var param
= params
[0];
3227 if (param
< 1) param
= 1;
3229 if (this.y
>= this.rows
) {
3230 this.y
= this.rows
- 1;
3237 * Cursor Forward Ps Times (default = 1) (CUF).
3239 Terminal
.prototype.cursorForward = function(params
) {
3240 var param
= params
[0];
3241 if (param
< 1) param
= 1;
3243 if (this.x
>= this.cols
) {
3244 this.x
= this.cols
- 1;
3251 * Cursor Backward Ps Times (default = 1) (CUB).
3253 Terminal
.prototype.cursorBackward = function(params
) {
3254 var param
= params
[0];
3255 if (param
< 1) param
= 1;
3257 if (this.x
< 0) this.x
= 0;
3263 * Cursor Position [row;column] (default = [1,1]) (CUP).
3265 Terminal
.prototype.cursorPos = function(params
) {
3268 row
= params
[0] - 1;
3270 if (params
.length
>= 2) {
3271 col
= params
[1] - 1;
3278 } else if (row
>= this.rows
) {
3279 row
= this.rows
- 1;
3284 } else if (col
>= this.cols
) {
3285 col
= this.cols
- 1;
3294 * CSI Ps J Erase in Display (ED).
3295 * Ps = 0 -> Erase Below (default).
3296 * Ps = 1 -> Erase Above.
3297 * Ps = 2 -> Erase All.
3298 * Ps = 3 -> Erase Saved Lines (xterm).
3300 * Erase in Display (DECSED).
3301 * Ps = 0 -> Selective Erase Below (default).
3302 * Ps = 1 -> Selective Erase Above.
3303 * Ps = 2 -> Selective Erase All.
3305 Terminal
.prototype.eraseInDisplay = function(params
) {
3307 switch (params
[0]) {
3309 this.eraseRight(this.x
, this.y
);
3311 for (; j
< this.rows
; j
++) {
3316 this.eraseLeft(this.x
, this.y
);
3324 while (j
--) this.eraseLine(j
);
3334 * CSI Ps K Erase in Line (EL).
3335 * Ps = 0 -> Erase to Right (default).
3336 * Ps = 1 -> Erase to Left.
3337 * Ps = 2 -> Erase All.
3339 * Erase in Line (DECSEL).
3340 * Ps = 0 -> Selective Erase to Right (default).
3341 * Ps = 1 -> Selective Erase to Left.
3342 * Ps = 2 -> Selective Erase All.
3344 Terminal
.prototype.eraseInLine = function(params
) {
3345 switch (params
[0]) {
3347 this.eraseRight(this.x
, this.y
);
3350 this.eraseLeft(this.x
, this.y
);
3353 this.eraseLine(this.y
);
3360 * CSI Pm m Character Attributes (SGR).
3361 * Ps = 0 -> Normal (default).
3363 * Ps = 4 -> Underlined.
3364 * Ps = 5 -> Blink (appears as Bold).
3365 * Ps = 7 -> Inverse.
3366 * Ps = 8 -> Invisible, i.e., hidden (VT300).
3367 * Ps = 2 2 -> Normal (neither bold nor faint).
3368 * Ps = 2 4 -> Not underlined.
3369 * Ps = 2 5 -> Steady (not blinking).
3370 * Ps = 2 7 -> Positive (not inverse).
3371 * Ps = 2 8 -> Visible, i.e., not hidden (VT300).
3372 * Ps = 3 0 -> Set foreground color to Black.
3373 * Ps = 3 1 -> Set foreground color to Red.
3374 * Ps = 3 2 -> Set foreground color to Green.
3375 * Ps = 3 3 -> Set foreground color to Yellow.
3376 * Ps = 3 4 -> Set foreground color to Blue.
3377 * Ps = 3 5 -> Set foreground color to Magenta.
3378 * Ps = 3 6 -> Set foreground color to Cyan.
3379 * Ps = 3 7 -> Set foreground color to White.
3380 * Ps = 3 9 -> Set foreground color to default (original).
3381 * Ps = 4 0 -> Set background color to Black.
3382 * Ps = 4 1 -> Set background color to Red.
3383 * Ps = 4 2 -> Set background color to Green.
3384 * Ps = 4 3 -> Set background color to Yellow.
3385 * Ps = 4 4 -> Set background color to Blue.
3386 * Ps = 4 5 -> Set background color to Magenta.
3387 * Ps = 4 6 -> Set background color to Cyan.
3388 * Ps = 4 7 -> Set background color to White.
3389 * Ps = 4 9 -> Set background color to default (original).
3391 * If 16-color support is compiled, the following apply. Assume
3392 * that xterm's resources are set so that the ISO color codes are
3393 * the first 8 of a set of 16. Then the aixterm colors are the
3394 * bright versions of the ISO colors:
3395 * Ps = 9 0 -> Set foreground color to Black.
3396 * Ps = 9 1 -> Set foreground color to Red.
3397 * Ps = 9 2 -> Set foreground color to Green.
3398 * Ps = 9 3 -> Set foreground color to Yellow.
3399 * Ps = 9 4 -> Set foreground color to Blue.
3400 * Ps = 9 5 -> Set foreground color to Magenta.
3401 * Ps = 9 6 -> Set foreground color to Cyan.
3402 * Ps = 9 7 -> Set foreground color to White.
3403 * Ps = 1 0 0 -> Set background color to Black.
3404 * Ps = 1 0 1 -> Set background color to Red.
3405 * Ps = 1 0 2 -> Set background color to Green.
3406 * Ps = 1 0 3 -> Set background color to Yellow.
3407 * Ps = 1 0 4 -> Set background color to Blue.
3408 * Ps = 1 0 5 -> Set background color to Magenta.
3409 * Ps = 1 0 6 -> Set background color to Cyan.
3410 * Ps = 1 0 7 -> Set background color to White.
3412 * If xterm is compiled with the 16-color support disabled, it
3413 * supports the following, from rxvt:
3414 * Ps = 1 0 0 -> Set foreground and background color to
3417 * If 88- or 256-color support is compiled, the following apply.
3418 * Ps = 3 8 ; 5 ; Ps -> Set foreground color to the second
3420 * Ps = 4 8 ; 5 ; Ps -> Set background color to the second
3423 Terminal
.prototype.charAttributes = function(params
) {
3424 // Optimize a single SGR0.
3425 if (params
.length
=== 1 && params
[0] === 0) {
3426 this.curAttr
= this.defAttr
;
3430 var l
= params
.length
3432 , flags
= this.curAttr
>> 18
3433 , fg
= (this.curAttr
>> 9) & 0x1ff
3434 , bg
= this.curAttr
& 0x1ff
3437 for (; i
< l
; i
++) {
3439 if (p
>= 30 && p
<= 37) {
3442 } else if (p
>= 40 && p
<= 47) {
3445 } else if (p
>= 90 && p
<= 97) {
3449 } else if (p
>= 100 && p
<= 107) {
3453 } else if (p
=== 0) {
3455 flags
= this.defAttr
>> 18;
3456 fg
= (this.defAttr
>> 9) & 0x1ff;
3457 bg
= this.defAttr
& 0x1ff;
3461 } else if (p
=== 1) {
3464 } else if (p
=== 4) {
3467 } else if (p
=== 5) {
3470 } else if (p
=== 7) {
3471 // inverse and positive
3472 // test with: echo -e '\e[31m\e[42mhello\e[7mworld\e[27mhi\e[m'
3474 } else if (p
=== 8) {
3477 } else if (p
=== 22) {
3480 } else if (p
=== 24) {
3483 } else if (p
=== 25) {
3486 } else if (p
=== 27) {
3489 } else if (p
=== 28) {
3492 } else if (p
=== 39) {
3494 fg
= (this.defAttr
>> 9) & 0x1ff;
3495 } else if (p
=== 49) {
3497 bg
= this.defAttr
& 0x1ff;
3498 } else if (p
=== 38) {
3500 if (params
[i
+ 1] === 2) {
3504 params
[i
+ 1] & 0xff,
3505 params
[i
+ 2] & 0xff);
3506 if (fg
=== -1) fg
= 0x1ff;
3508 } else if (params
[i
+ 1] === 5) {
3510 p
= params
[i
] & 0xff;
3513 } else if (p
=== 48) {
3515 if (params
[i
+ 1] === 2) {
3519 params
[i
+ 1] & 0xff,
3520 params
[i
+ 2] & 0xff);
3521 if (bg
=== -1) bg
= 0x1ff;
3523 } else if (params
[i
+ 1] === 5) {
3525 p
= params
[i
] & 0xff;
3528 } else if (p
=== 100) {
3530 fg
= (this.defAttr
>> 9) & 0x1ff;
3531 bg
= this.defAttr
& 0x1ff;
3533 this.error('Unknown SGR attribute: %d.', p
);
3537 this.curAttr
= (flags
<< 18) | (fg
<< 9) | bg
;
3542 * CSI Ps n Device Status Report (DSR).
3543 * Ps = 5 -> Status Report. Result (``OK'') is
3545 * Ps = 6 -> Report Cursor Position (CPR) [row;column].
3549 * Device Status Report (DSR, DEC-specific).
3550 * Ps = 6 -> Report Cursor Position (CPR) [row;column] as CSI
3551 * ? r ; c R (assumes page is zero).
3552 * Ps = 1 5 -> Report Printer status as CSI ? 1 0 n (ready).
3553 * or CSI ? 1 1 n (not ready).
3554 * Ps = 2 5 -> Report UDK status as CSI ? 2 0 n (unlocked)
3555 * or CSI ? 2 1 n (locked).
3556 * Ps = 2 6 -> Report Keyboard status as
3557 * CSI ? 2 7 ; 1 ; 0 ; 0 n (North American).
3558 * The last two parameters apply to VT400 & up, and denote key-
3559 * board ready and LK01 respectively.
3560 * Ps = 5 3 -> Report Locator status as
3561 * CSI ? 5 3 n Locator available, if compiled-in, or
3562 * CSI ? 5 0 n No Locator, if not.
3564 Terminal
.prototype.deviceStatus = function(params
) {
3566 switch (params
[0]) {
3569 this.send('\x1b[0n');
3580 } else if (this.prefix
=== '?') {
3581 // modern xterm doesnt seem to
3582 // respond to any of these except ?6, 6, and 5
3583 switch (params
[0]) {
3594 // this.send('\x1b[?11n');
3597 // dont support user defined keys
3598 // this.send('\x1b[?21n');
3601 // north american keyboard
3602 // this.send('\x1b[?27;1;0;0n');
3605 // no dec locator/mouse
3606 // this.send('\x1b[?50n');
3619 * Insert Ps (Blank) Character(s) (default = 1) (ICH).
3621 Terminal
.prototype.insertChars = function(params
) {
3622 var param
, row
, j
, ch
;
3625 if (param
< 1) param
= 1;
3627 row
= this.y
+ this.ybase
;
3629 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3631 while (param
-- && j
< this.cols
) {
3632 this.lines
.get(row
).splice(j
++, 0, ch
);
3633 this.lines
.get(row
).pop();
3639 * Cursor Next Line Ps Times (default = 1) (CNL).
3640 * same as CSI Ps B ?
3642 Terminal
.prototype.cursorNextLine = function(params
) {
3643 var param
= params
[0];
3644 if (param
< 1) param
= 1;
3646 if (this.y
>= this.rows
) {
3647 this.y
= this.rows
- 1;
3655 * Cursor Preceding Line Ps Times (default = 1) (CNL).
3658 Terminal
.prototype.cursorPrecedingLine = function(params
) {
3659 var param
= params
[0];
3660 if (param
< 1) param
= 1;
3662 if (this.y
< 0) this.y
= 0;
3669 * Cursor Character Absolute [column] (default = [row,1]) (CHA).
3671 Terminal
.prototype.cursorCharAbsolute = function(params
) {
3672 var param
= params
[0];
3673 if (param
< 1) param
= 1;
3680 * Insert Ps Line(s) (default = 1) (IL).
3682 Terminal
.prototype.insertLines = function(params
) {
3686 if (param
< 1) param
= 1;
3687 row
= this.y
+ this.ybase
;
3689 j
= this.rows
- 1 - this.scrollBottom
;
3690 j
= this.rows
- 1 + this.ybase
- j
+ 1;
3693 // test: echo -e '\e[44m\e[1L\e[0m'
3694 // blankLine(true) - xterm/linux behavior
3695 this.lines
.splice(row
, 0, this.blankLine(true));
3696 this.lines
.splice(j
, 1);
3700 this.updateRange(this.y
);
3701 this.updateRange(this.scrollBottom
);
3707 * Delete Ps Line(s) (default = 1) (DL).
3709 Terminal
.prototype.deleteLines = function(params
) {
3713 if (param
< 1) param
= 1;
3714 row
= this.y
+ this.ybase
;
3716 j
= this.rows
- 1 - this.scrollBottom
;
3717 j
= this.rows
- 1 + this.ybase
- j
;
3720 // test: echo -e '\e[44m\e[1M\e[0m'
3721 // blankLine(true) - xterm/linux behavior
3722 this.lines
.splice(j
+ 1, 0, this.blankLine(true));
3723 this.lines
.splice(row
, 1);
3727 this.updateRange(this.y
);
3728 this.updateRange(this.scrollBottom
);
3734 * Delete Ps Character(s) (default = 1) (DCH).
3736 Terminal
.prototype.deleteChars = function(params
) {
3740 if (param
< 1) param
= 1;
3742 row
= this.y
+ this.ybase
;
3743 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3746 this.lines
[row
].splice(this.x
, 1);
3747 this.lines
[row
].push(ch
);
3753 * Erase Ps Character(s) (default = 1) (ECH).
3755 Terminal
.prototype.eraseChars = function(params
) {
3756 var param
, row
, j
, ch
;
3759 if (param
< 1) param
= 1;
3761 row
= this.y
+ this.ybase
;
3763 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3765 while (param
-- && j
< this.cols
) {
3766 this.lines
.get(row
)[j
++] = ch
;
3771 * CSI Pm ` Character Position Absolute
3772 * [column] (default = [row,1]) (HPA).
3774 Terminal
.prototype.charPosAbsolute = function(params
) {
3775 var param
= params
[0];
3776 if (param
< 1) param
= 1;
3778 if (this.x
>= this.cols
) {
3779 this.x
= this.cols
- 1;
3786 * Horizontal Position Relative
3789 Terminal
.prototype.HPositionRelative = function(params
) {
3790 var param
= params
[0];
3791 if (param
< 1) param
= 1;
3793 if (this.x
>= this.cols
) {
3794 this.x
= this.cols
- 1;
3800 * CSI Ps c Send Device Attributes (Primary DA).
3801 * Ps = 0 or omitted -> request attributes from terminal. The
3802 * response depends on the decTerminalID resource setting.
3803 * -> CSI ? 1 ; 2 c (``VT100 with Advanced Video Option'')
3804 * -> CSI ? 1 ; 0 c (``VT101 with No Options'')
3805 * -> CSI ? 6 c (``VT102'')
3806 * -> CSI ? 6 0 ; 1 ; 2 ; 6 ; 8 ; 9 ; 1 5 ; c (``VT220'')
3807 * The VT100-style response parameters do not mean anything by
3808 * themselves. VT220 parameters do, telling the host what fea-
3809 * tures the terminal supports:
3810 * Ps = 1 -> 132-columns.
3811 * Ps = 2 -> Printer.
3812 * Ps = 6 -> Selective erase.
3813 * Ps = 8 -> User-defined keys.
3814 * Ps = 9 -> National replacement character sets.
3815 * Ps = 1 5 -> Technical characters.
3816 * Ps = 2 2 -> ANSI color, e.g., VT525.
3817 * Ps = 2 9 -> ANSI text locator (i.e., DEC Locator mode).
3819 * Send Device Attributes (Secondary DA).
3820 * Ps = 0 or omitted -> request the terminal's identification
3821 * code. The response depends on the decTerminalID resource set-
3822 * ting. It should apply only to VT220 and up, but xterm extends
3824 * -> CSI > Pp ; Pv ; Pc c
3825 * where Pp denotes the terminal type
3826 * Pp = 0 -> ``VT100''.
3827 * Pp = 1 -> ``VT220''.
3828 * and Pv is the firmware version (for xterm, this was originally
3829 * the XFree86 patch number, starting with 95). In a DEC termi-
3830 * nal, Pc indicates the ROM cartridge registration number and is
3833 * xterm/charproc.c - line 2012, for more information.
3834 * vim responds with ^[[?0c or ^[[?1c after the terminal's response (?)
3836 Terminal
.prototype.sendDeviceAttributes = function(params
) {
3837 if (params
[0] > 0) return;
3840 if (this.is('xterm')
3841 || this.is('rxvt-unicode')
3842 || this.is('screen')) {
3843 this.send('\x1b[?1;2c');
3844 } else if (this.is('linux')) {
3845 this.send('\x1b[?6c');
3847 } else if (this.prefix
=== '>') {
3849 // seem to spit this
3850 // out around ~370 times (?).
3851 if (this.is('xterm')) {
3852 this.send('\x1b[>0;276;0c');
3853 } else if (this.is('rxvt-unicode')) {
3854 this.send('\x1b[>85;95;0c');
3855 } else if (this.is('linux')) {
3856 // not supported by linux console.
3857 // linux console echoes parameters.
3858 this.send(params
[0] + 'c');
3859 } else if (this.is('screen')) {
3860 this.send('\x1b[>83;40003;0c');
3868 * Line Position Absolute [row] (default = [1,column]) (VPA).
3870 Terminal
.prototype.linePosAbsolute = function(params
) {
3871 var param
= params
[0];
3872 if (param
< 1) param
= 1;
3874 if (this.y
>= this.rows
) {
3875 this.y
= this.rows
- 1;
3881 * 145 65 e * VPR - Vertical Position Relative
3884 Terminal
.prototype.VPositionRelative = function(params
) {
3885 var param
= params
[0];
3886 if (param
< 1) param
= 1;
3888 if (this.y
>= this.rows
) {
3889 this.y
= this.rows
- 1;
3896 * Horizontal and Vertical Position [row;column] (default =
3899 Terminal
.prototype.HVPosition = function(params
) {
3900 if (params
[0] < 1) params
[0] = 1;
3901 if (params
[1] < 1) params
[1] = 1;
3903 this.y
= params
[0] - 1;
3904 if (this.y
>= this.rows
) {
3905 this.y
= this.rows
- 1;
3908 this.x
= params
[1] - 1;
3909 if (this.x
>= this.cols
) {
3910 this.x
= this.cols
- 1;
3916 * CSI Pm h Set Mode (SM).
3917 * Ps = 2 -> Keyboard Action Mode (AM).
3918 * Ps = 4 -> Insert Mode (IRM).
3919 * Ps = 1 2 -> Send/receive (SRM).
3920 * Ps = 2 0 -> Automatic Newline (LNM).
3922 * DEC Private Mode Set (DECSET).
3923 * Ps = 1 -> Application Cursor Keys (DECCKM).
3924 * Ps = 2 -> Designate USASCII for character sets G0-G3
3925 * (DECANM), and set VT100 mode.
3926 * Ps = 3 -> 132 Column Mode (DECCOLM).
3927 * Ps = 4 -> Smooth (Slow) Scroll (DECSCLM).
3928 * Ps = 5 -> Reverse Video (DECSCNM).
3929 * Ps = 6 -> Origin Mode (DECOM).
3930 * Ps = 7 -> Wraparound Mode (DECAWM).
3931 * Ps = 8 -> Auto-repeat Keys (DECARM).
3932 * Ps = 9 -> Send Mouse X & Y on button press. See the sec-
3933 * tion Mouse Tracking.
3934 * Ps = 1 0 -> Show toolbar (rxvt).
3935 * Ps = 1 2 -> Start Blinking Cursor (att610).
3936 * Ps = 1 8 -> Print form feed (DECPFF).
3937 * Ps = 1 9 -> Set print extent to full screen (DECPEX).
3938 * Ps = 2 5 -> Show Cursor (DECTCEM).
3939 * Ps = 3 0 -> Show scrollbar (rxvt).
3940 * Ps = 3 5 -> Enable font-shifting functions (rxvt).
3941 * Ps = 3 8 -> Enter Tektronix Mode (DECTEK).
3942 * Ps = 4 0 -> Allow 80 -> 132 Mode.
3943 * Ps = 4 1 -> more(1) fix (see curses resource).
3944 * Ps = 4 2 -> Enable Nation Replacement Character sets (DECN-
3946 * Ps = 4 4 -> Turn On Margin Bell.
3947 * Ps = 4 5 -> Reverse-wraparound Mode.
3948 * Ps = 4 6 -> Start Logging. This is normally disabled by a
3949 * compile-time option.
3950 * Ps = 4 7 -> Use Alternate Screen Buffer. (This may be dis-
3951 * abled by the titeInhibit resource).
3952 * Ps = 6 6 -> Application keypad (DECNKM).
3953 * Ps = 6 7 -> Backarrow key sends backspace (DECBKM).
3954 * Ps = 1 0 0 0 -> Send Mouse X & Y on button press and
3955 * release. See the section Mouse Tracking.
3956 * Ps = 1 0 0 1 -> Use Hilite Mouse Tracking.
3957 * Ps = 1 0 0 2 -> Use Cell Motion Mouse Tracking.
3958 * Ps = 1 0 0 3 -> Use All Motion Mouse Tracking.
3959 * Ps = 1 0 0 4 -> Send FocusIn/FocusOut events.
3960 * Ps = 1 0 0 5 -> Enable Extended Mouse Mode.
3961 * Ps = 1 0 1 0 -> Scroll to bottom on tty output (rxvt).
3962 * Ps = 1 0 1 1 -> Scroll to bottom on key press (rxvt).
3963 * Ps = 1 0 3 4 -> Interpret "meta" key, sets eighth bit.
3964 * (enables the eightBitInput resource).
3965 * Ps = 1 0 3 5 -> Enable special modifiers for Alt and Num-
3966 * Lock keys. (This enables the numLock resource).
3967 * Ps = 1 0 3 6 -> Send ESC when Meta modifies a key. (This
3968 * enables the metaSendsEscape resource).
3969 * Ps = 1 0 3 7 -> Send DEL from the editing-keypad Delete
3971 * Ps = 1 0 3 9 -> Send ESC when Alt modifies a key. (This
3972 * enables the altSendsEscape resource).
3973 * Ps = 1 0 4 0 -> Keep selection even if not highlighted.
3974 * (This enables the keepSelection resource).
3975 * Ps = 1 0 4 1 -> Use the CLIPBOARD selection. (This enables
3976 * the selectToClipboard resource).
3977 * Ps = 1 0 4 2 -> Enable Urgency window manager hint when
3978 * Control-G is received. (This enables the bellIsUrgent
3980 * Ps = 1 0 4 3 -> Enable raising of the window when Control-G
3981 * is received. (enables the popOnBell resource).
3982 * Ps = 1 0 4 7 -> Use Alternate Screen Buffer. (This may be
3983 * disabled by the titeInhibit resource).
3984 * Ps = 1 0 4 8 -> Save cursor as in DECSC. (This may be dis-
3985 * abled by the titeInhibit resource).
3986 * Ps = 1 0 4 9 -> Save cursor as in DECSC and use Alternate
3987 * Screen Buffer, clearing it first. (This may be disabled by
3988 * the titeInhibit resource). This combines the effects of the 1
3989 * 0 4 7 and 1 0 4 8 modes. Use this with terminfo-based
3990 * applications rather than the 4 7 mode.
3991 * Ps = 1 0 5 0 -> Set terminfo/termcap function-key mode.
3992 * Ps = 1 0 5 1 -> Set Sun function-key mode.
3993 * Ps = 1 0 5 2 -> Set HP function-key mode.
3994 * Ps = 1 0 5 3 -> Set SCO function-key mode.
3995 * Ps = 1 0 6 0 -> Set legacy keyboard emulation (X11R6).
3996 * Ps = 1 0 6 1 -> Set VT220 keyboard emulation.
3997 * Ps = 2 0 0 4 -> Set bracketed paste mode.
3999 * http: *vt100.net/docs/vt220-rm/chapter4.html
4001 Terminal
.prototype.setMode = function(params
) {
4002 if (typeof params
=== 'object') {
4003 var l
= params
.length
4006 for (; i
< l
; i
++) {
4007 this.setMode(params
[i
]);
4016 this.insertMode
= true;
4019 //this.convertEol = true;
4022 } else if (this.prefix
=== '?') {
4025 this.applicationCursor
= true;
4028 this.setgCharset(0, Terminal
.charsets
.US
);
4029 this.setgCharset(1, Terminal
.charsets
.US
);
4030 this.setgCharset(2, Terminal
.charsets
.US
);
4031 this.setgCharset(3, Terminal
.charsets
.US
);
4032 // set VT100 mode here
4034 case 3: // 132 col mode
4035 this.savedCols
= this.cols
;
4036 this.resize(132, this.rows
);
4039 this.originMode
= true;
4042 this.wraparoundMode
= true;
4045 // this.cursorBlink = true;
4048 this.log('Serial port requested application keypad.');
4049 this.applicationKeypad
= true;
4050 this.viewport
.syncScrollArea();
4052 case 9: // X10 Mouse
4053 // no release, no motion, no wheel, no modifiers.
4054 case 1000: // vt200 mouse
4056 // no modifiers, except control on the wheel.
4057 case 1002: // button event mouse
4058 case 1003: // any event mouse
4059 // any event - sends motion events,
4060 // even if there is no button held down.
4061 this.x10Mouse
= params
=== 9;
4062 this.vt200Mouse
= params
=== 1000;
4063 this.normalMouse
= params
> 1000;
4064 this.mouseEvents
= true;
4065 this.element
.style
.cursor
= 'default';
4066 this.log('Binding to mouse events.');
4068 case 1004: // send focusin/focusout events
4071 this.sendFocus
= true;
4073 case 1005: // utf8 ext mode mouse
4074 this.utfMouse
= true;
4075 // for wide terminals
4076 // simply encodes large values as utf8 characters
4078 case 1006: // sgr ext mode mouse
4079 this.sgrMouse
= true;
4080 // for wide terminals
4081 // does not add 32 to fields
4082 // press: ^[[<b;x;yM
4083 // release: ^[[<b;x;ym
4085 case 1015: // urxvt ext mode mouse
4086 this.urxvtMouse
= true;
4087 // for wide terminals
4088 // numbers for fields
4090 // motion: ^[[b;x;yT
4092 case 25: // show cursor
4093 this.cursorHidden
= false;
4095 case 1049: // alt screen buffer cursor
4096 //this.saveCursor();
4098 case 47: // alt screen buffer
4099 case 1047: // alt screen buffer
4107 scrollTop
: this.scrollTop
,
4108 scrollBottom
: this.scrollBottom
,
4110 // XXX save charset(s) here?
4111 // charset: this.charset,
4112 // glevel: this.glevel,
4113 // charsets: this.charsets
4116 this.normal
= normal
;
4125 * CSI Pm l Reset Mode (RM).
4126 * Ps = 2 -> Keyboard Action Mode (AM).
4127 * Ps = 4 -> Replace Mode (IRM).
4128 * Ps = 1 2 -> Send/receive (SRM).
4129 * Ps = 2 0 -> Normal Linefeed (LNM).
4131 * DEC Private Mode Reset (DECRST).
4132 * Ps = 1 -> Normal Cursor Keys (DECCKM).
4133 * Ps = 2 -> Designate VT52 mode (DECANM).
4134 * Ps = 3 -> 80 Column Mode (DECCOLM).
4135 * Ps = 4 -> Jump (Fast) Scroll (DECSCLM).
4136 * Ps = 5 -> Normal Video (DECSCNM).
4137 * Ps = 6 -> Normal Cursor Mode (DECOM).
4138 * Ps = 7 -> No Wraparound Mode (DECAWM).
4139 * Ps = 8 -> No Auto-repeat Keys (DECARM).
4140 * Ps = 9 -> Don't send Mouse X & Y on button press.
4141 * Ps = 1 0 -> Hide toolbar (rxvt).
4142 * Ps = 1 2 -> Stop Blinking Cursor (att610).
4143 * Ps = 1 8 -> Don't print form feed (DECPFF).
4144 * Ps = 1 9 -> Limit print to scrolling region (DECPEX).
4145 * Ps = 2 5 -> Hide Cursor (DECTCEM).
4146 * Ps = 3 0 -> Don't show scrollbar (rxvt).
4147 * Ps = 3 5 -> Disable font-shifting functions (rxvt).
4148 * Ps = 4 0 -> Disallow 80 -> 132 Mode.
4149 * Ps = 4 1 -> No more(1) fix (see curses resource).
4150 * Ps = 4 2 -> Disable Nation Replacement Character sets (DEC-
4152 * Ps = 4 4 -> Turn Off Margin Bell.
4153 * Ps = 4 5 -> No Reverse-wraparound Mode.
4154 * Ps = 4 6 -> Stop Logging. (This is normally disabled by a
4155 * compile-time option).
4156 * Ps = 4 7 -> Use Normal Screen Buffer.
4157 * Ps = 6 6 -> Numeric keypad (DECNKM).
4158 * Ps = 6 7 -> Backarrow key sends delete (DECBKM).
4159 * Ps = 1 0 0 0 -> Don't send Mouse X & Y on button press and
4160 * release. See the section Mouse Tracking.
4161 * Ps = 1 0 0 1 -> Don't use Hilite Mouse Tracking.
4162 * Ps = 1 0 0 2 -> Don't use Cell Motion Mouse Tracking.
4163 * Ps = 1 0 0 3 -> Don't use All Motion Mouse Tracking.
4164 * Ps = 1 0 0 4 -> Don't send FocusIn/FocusOut events.
4165 * Ps = 1 0 0 5 -> Disable Extended Mouse Mode.
4166 * Ps = 1 0 1 0 -> Don't scroll to bottom on tty output
4168 * Ps = 1 0 1 1 -> Don't scroll to bottom on key press (rxvt).
4169 * Ps = 1 0 3 4 -> Don't interpret "meta" key. (This disables
4170 * the eightBitInput resource).
4171 * Ps = 1 0 3 5 -> Disable special modifiers for Alt and Num-
4172 * Lock keys. (This disables the numLock resource).
4173 * Ps = 1 0 3 6 -> Don't send ESC when Meta modifies a key.
4174 * (This disables the metaSendsEscape resource).
4175 * Ps = 1 0 3 7 -> Send VT220 Remove from the editing-keypad
4177 * Ps = 1 0 3 9 -> Don't send ESC when Alt modifies a key.
4178 * (This disables the altSendsEscape resource).
4179 * Ps = 1 0 4 0 -> Do not keep selection when not highlighted.
4180 * (This disables the keepSelection resource).
4181 * Ps = 1 0 4 1 -> Use the PRIMARY selection. (This disables
4182 * the selectToClipboard resource).
4183 * Ps = 1 0 4 2 -> Disable Urgency window manager hint when
4184 * Control-G is received. (This disables the bellIsUrgent
4186 * Ps = 1 0 4 3 -> Disable raising of the window when Control-
4187 * G is received. (This disables the popOnBell resource).
4188 * Ps = 1 0 4 7 -> Use Normal Screen Buffer, clearing screen
4189 * first if in the Alternate Screen. (This may be disabled by
4190 * the titeInhibit resource).
4191 * Ps = 1 0 4 8 -> Restore cursor as in DECRC. (This may be
4192 * disabled by the titeInhibit resource).
4193 * Ps = 1 0 4 9 -> Use Normal Screen Buffer and restore cursor
4194 * as in DECRC. (This may be disabled by the titeInhibit
4195 * resource). This combines the effects of the 1 0 4 7 and 1 0
4196 * 4 8 modes. Use this with terminfo-based applications rather
4197 * than the 4 7 mode.
4198 * Ps = 1 0 5 0 -> Reset terminfo/termcap function-key mode.
4199 * Ps = 1 0 5 1 -> Reset Sun function-key mode.
4200 * Ps = 1 0 5 2 -> Reset HP function-key mode.
4201 * Ps = 1 0 5 3 -> Reset SCO function-key mode.
4202 * Ps = 1 0 6 0 -> Reset legacy keyboard emulation (X11R6).
4203 * Ps = 1 0 6 1 -> Reset keyboard emulation to Sun/PC style.
4204 * Ps = 2 0 0 4 -> Reset bracketed paste mode.
4206 Terminal
.prototype.resetMode = function(params
) {
4207 if (typeof params
=== 'object') {
4208 var l
= params
.length
4211 for (; i
< l
; i
++) {
4212 this.resetMode(params
[i
]);
4221 this.insertMode
= false;
4224 //this.convertEol = false;
4227 } else if (this.prefix
=== '?') {
4230 this.applicationCursor
= false;
4233 if (this.cols
=== 132 && this.savedCols
) {
4234 this.resize(this.savedCols
, this.rows
);
4236 delete this.savedCols
;
4239 this.originMode
= false;
4242 this.wraparoundMode
= false;
4245 // this.cursorBlink = false;
4248 this.log('Switching back to normal keypad.');
4249 this.applicationKeypad
= false;
4250 this.viewport
.syncScrollArea();
4252 case 9: // X10 Mouse
4253 case 1000: // vt200 mouse
4254 case 1002: // button event mouse
4255 case 1003: // any event mouse
4256 this.x10Mouse
= false;
4257 this.vt200Mouse
= false;
4258 this.normalMouse
= false;
4259 this.mouseEvents
= false;
4260 this.element
.style
.cursor
= '';
4262 case 1004: // send focusin/focusout events
4263 this.sendFocus
= false;
4265 case 1005: // utf8 ext mode mouse
4266 this.utfMouse
= false;
4268 case 1006: // sgr ext mode mouse
4269 this.sgrMouse
= false;
4271 case 1015: // urxvt ext mode mouse
4272 this.urxvtMouse
= false;
4274 case 25: // hide cursor
4275 this.cursorHidden
= true;
4277 case 1049: // alt screen buffer cursor
4279 case 47: // normal screen buffer
4280 case 1047: // normal screen buffer - clearing it first
4282 this.lines
= this.normal
.lines
;
4283 this.ybase
= this.normal
.ybase
;
4284 this.ydisp
= this.normal
.ydisp
;
4285 this.x
= this.normal
.x
;
4286 this.y
= this.normal
.y
;
4287 this.scrollTop
= this.normal
.scrollTop
;
4288 this.scrollBottom
= this.normal
.scrollBottom
;
4289 this.tabs
= this.normal
.tabs
;
4291 // if (params === 1049) {
4292 // this.x = this.savedX;
4293 // this.y = this.savedY;
4295 this.refresh(0, this.rows
- 1);
4306 * Set Scrolling Region [top;bottom] (default = full size of win-
4310 Terminal
.prototype.setScrollRegion = function(params
) {
4311 if (this.prefix
) return;
4312 this.scrollTop
= (params
[0] || 1) - 1;
4313 this.scrollBottom
= (params
[1] || this.rows
) - 1;
4321 * Save cursor (ANSI.SYS).
4323 Terminal
.prototype.saveCursor = function(params
) {
4324 this.savedX
= this.x
;
4325 this.savedY
= this.y
;
4331 * Restore cursor (ANSI.SYS).
4333 Terminal
.prototype.restoreCursor = function(params
) {
4334 this.x
= this.savedX
|| 0;
4335 this.y
= this.savedY
|| 0;
4345 * Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
4347 Terminal
.prototype.cursorForwardTab = function(params
) {
4348 var param
= params
[0] || 1;
4350 this.x
= this.nextStop();
4356 * CSI Ps S Scroll up Ps lines (default = 1) (SU).
4358 Terminal
.prototype.scrollUp = function(params
) {
4359 var param
= params
[0] || 1;
4361 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
4362 this.lines
.splice(this.ybase
+ this.scrollBottom
, 0, this.blankLine());
4365 this.updateRange(this.scrollTop
);
4366 this.updateRange(this.scrollBottom
);
4371 * CSI Ps T Scroll down Ps lines (default = 1) (SD).
4373 Terminal
.prototype.scrollDown = function(params
) {
4374 var param
= params
[0] || 1;
4376 this.lines
.splice(this.ybase
+ this.scrollBottom
, 1);
4377 this.lines
.splice(this.ybase
+ this.scrollTop
, 0, this.blankLine());
4380 this.updateRange(this.scrollTop
);
4381 this.updateRange(this.scrollBottom
);
4386 * CSI Ps ; Ps ; Ps ; Ps ; Ps T
4387 * Initiate highlight mouse tracking. Parameters are
4388 * [func;startx;starty;firstrow;lastrow]. See the section Mouse
4391 Terminal
.prototype.initMouseTracking = function(params
) {
4392 // Relevant: DECSET 1001
4398 * Reset one or more features of the title modes to the default
4399 * value. Normally, "reset" disables the feature. It is possi-
4400 * ble to disable the ability to reset features by compiling a
4401 * different default for the title modes into xterm.
4402 * Ps = 0 -> Do not set window/icon labels using hexadecimal.
4403 * Ps = 1 -> Do not query window/icon labels using hexadeci-
4405 * Ps = 2 -> Do not set window/icon labels using UTF-8.
4406 * Ps = 3 -> Do not query window/icon labels using UTF-8.
4407 * (See discussion of "Title Modes").
4409 Terminal
.prototype.resetTitleModes = function(params
) {
4415 * CSI Ps Z Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
4417 Terminal
.prototype.cursorBackwardTab = function(params
) {
4418 var param
= params
[0] || 1;
4420 this.x
= this.prevStop();
4426 * CSI Ps b Repeat the preceding graphic character Ps times (REP).
4428 Terminal
.prototype.repeatPrecedingCharacter = function(params
) {
4429 var param
= params
[0] || 1
4430 , line
= this.lines
.get(this.ybase
+ this.y
)
4431 , ch
= line
[this.x
- 1] || [this.defAttr
, ' ', 1];
4433 while (param
--) line
[this.x
++] = ch
;
4438 * CSI Ps g Tab Clear (TBC).
4439 * Ps = 0 -> Clear Current Column (default).
4440 * Ps = 3 -> Clear All.
4442 * Ps = 2 -> Clear Stops on Line.
4443 * http://vt100.net/annarbor/aaa-ug/section6.html
4445 Terminal
.prototype.tabClear = function(params
) {
4446 var param
= params
[0];
4448 delete this.tabs
[this.x
];
4449 } else if (param
=== 3) {
4456 * CSI Pm i Media Copy (MC).
4457 * Ps = 0 -> Print screen (default).
4458 * Ps = 4 -> Turn off printer controller mode.
4459 * Ps = 5 -> Turn on printer controller mode.
4461 * Media Copy (MC, DEC-specific).
4462 * Ps = 1 -> Print line containing cursor.
4463 * Ps = 4 -> Turn off autoprint mode.
4464 * Ps = 5 -> Turn on autoprint mode.
4465 * Ps = 1 0 -> Print composed display, ignores DECPEX.
4466 * Ps = 1 1 -> Print all pages.
4468 Terminal
.prototype.mediaCopy = function(params
) {
4475 * Set or reset resource-values used by xterm to decide whether
4476 * to construct escape sequences holding information about the
4477 * modifiers pressed with a given key. The first parameter iden-
4478 * tifies the resource to set/reset. The second parameter is the
4479 * value to assign to the resource. If the second parameter is
4480 * omitted, the resource is reset to its initial value.
4481 * Ps = 1 -> modifyCursorKeys.
4482 * Ps = 2 -> modifyFunctionKeys.
4483 * Ps = 4 -> modifyOtherKeys.
4484 * If no parameters are given, all resources are reset to their
4487 Terminal
.prototype.setResources = function(params
) {
4494 * Disable modifiers which may be enabled via the CSI > Ps; Ps m
4495 * sequence. This corresponds to a resource value of "-1", which
4496 * cannot be set with the other sequence. The parameter identi-
4497 * fies the resource to be disabled:
4498 * Ps = 1 -> modifyCursorKeys.
4499 * Ps = 2 -> modifyFunctionKeys.
4500 * Ps = 4 -> modifyOtherKeys.
4501 * If the parameter is omitted, modifyFunctionKeys is disabled.
4502 * When modifyFunctionKeys is disabled, xterm uses the modifier
4503 * keys to make an extended sequence of functions rather than
4504 * adding a parameter to each function key to denote the modi-
4507 Terminal
.prototype.disableModifiers = function(params
) {
4514 * Set resource value pointerMode. This is used by xterm to
4515 * decide whether to hide the pointer cursor as the user types.
4516 * Valid values for the parameter:
4517 * Ps = 0 -> never hide the pointer.
4518 * Ps = 1 -> hide if the mouse tracking mode is not enabled.
4519 * Ps = 2 -> always hide the pointer. If no parameter is
4520 * given, xterm uses the default, which is 1 .
4522 Terminal
.prototype.setPointerMode = function(params
) {
4528 * CSI ! p Soft terminal reset (DECSTR).
4529 * http://vt100.net/docs/vt220-rm/table4-10.html
4531 Terminal
.prototype.softReset = function(params
) {
4532 this.cursorHidden
= false;
4533 this.insertMode
= false;
4534 this.originMode
= false;
4535 this.wraparoundMode
= false; // autowrap
4536 this.applicationKeypad
= false; // ?
4537 this.viewport
.syncScrollArea();
4538 this.applicationCursor
= false;
4540 this.scrollBottom
= this.rows
- 1;
4541 this.curAttr
= this.defAttr
;
4542 this.x
= this.y
= 0; // ?
4543 this.charset
= null;
4544 this.glevel
= 0; // ??
4545 this.charsets
= [null]; // ??
4551 * Request ANSI mode (DECRQM). For VT300 and up, reply is
4553 * where Ps is the mode number as in RM, and Pm is the mode
4555 * 0 - not recognized
4558 * 3 - permanently set
4559 * 4 - permanently reset
4561 Terminal
.prototype.requestAnsiMode = function(params
) {
4568 * Request DEC private mode (DECRQM). For VT300 and up, reply is
4570 * where Ps is the mode number as in DECSET, Pm is the mode value
4571 * as in the ANSI DECRQM.
4573 Terminal
.prototype.requestPrivateMode = function(params
) {
4580 * Set conformance level (DECSCL). Valid values for the first
4582 * Ps = 6 1 -> VT100.
4583 * Ps = 6 2 -> VT200.
4584 * Ps = 6 3 -> VT300.
4585 * Valid values for the second parameter:
4586 * Ps = 0 -> 8-bit controls.
4587 * Ps = 1 -> 7-bit controls (always set for VT100).
4588 * Ps = 2 -> 8-bit controls.
4590 Terminal
.prototype.setConformanceLevel = function(params
) {
4596 * CSI Ps q Load LEDs (DECLL).
4597 * Ps = 0 -> Clear all LEDS (default).
4598 * Ps = 1 -> Light Num Lock.
4599 * Ps = 2 -> Light Caps Lock.
4600 * Ps = 3 -> Light Scroll Lock.
4601 * Ps = 2 1 -> Extinguish Num Lock.
4602 * Ps = 2 2 -> Extinguish Caps Lock.
4603 * Ps = 2 3 -> Extinguish Scroll Lock.
4605 Terminal
.prototype.loadLEDs = function(params
) {
4612 * Set cursor style (DECSCUSR, VT520).
4613 * Ps = 0 -> blinking block.
4614 * Ps = 1 -> blinking block (default).
4615 * Ps = 2 -> steady block.
4616 * Ps = 3 -> blinking underline.
4617 * Ps = 4 -> steady underline.
4619 Terminal
.prototype.setCursorStyle = function(params
) {
4626 * Select character protection attribute (DECSCA). Valid values
4627 * for the parameter:
4628 * Ps = 0 -> DECSED and DECSEL can erase (default).
4629 * Ps = 1 -> DECSED and DECSEL cannot erase.
4630 * Ps = 2 -> DECSED and DECSEL can erase.
4632 Terminal
.prototype.setCharProtectionAttr = function(params
) {
4639 * Restore DEC Private Mode Values. The value of Ps previously
4640 * saved is restored. Ps values are the same as for DECSET.
4642 Terminal
.prototype.restorePrivateValues = function(params
) {
4648 * CSI Pt; Pl; Pb; Pr; Ps$ r
4649 * Change Attributes in Rectangular Area (DECCARA), VT400 and up.
4650 * Pt; Pl; Pb; Pr denotes the rectangle.
4651 * Ps denotes the SGR attributes to change: 0, 1, 4, 5, 7.
4652 * NOTE: xterm doesn't enable this code by default.
4654 Terminal
.prototype.setAttrInRectangle = function(params
) {
4664 for (; t
< b
+ 1; t
++) {
4665 line
= this.lines
.get(this.ybase
+ t
);
4666 for (i
= l
; i
< r
; i
++) {
4667 line
[i
] = [attr
, line
[i
][1]];
4672 this.updateRange(params
[0]);
4673 this.updateRange(params
[2]);
4678 * CSI Pc; Pt; Pl; Pb; Pr$ x
4679 * Fill Rectangular Area (DECFRA), VT420 and up.
4680 * Pc is the character to use.
4681 * Pt; Pl; Pb; Pr denotes the rectangle.
4682 * NOTE: xterm doesn't enable this code by default.
4684 Terminal
.prototype.fillRectangle = function(params
) {
4694 for (; t
< b
+ 1; t
++) {
4695 line
= this.lines
.get(this.ybase
+ t
);
4696 for (i
= l
; i
< r
; i
++) {
4697 line
[i
] = [line
[i
][0], String
.fromCharCode(ch
)];
4702 this.updateRange(params
[1]);
4703 this.updateRange(params
[3]);
4709 * Enable Locator Reporting (DECELR).
4710 * Valid values for the first parameter:
4711 * Ps = 0 -> Locator disabled (default).
4712 * Ps = 1 -> Locator enabled.
4713 * Ps = 2 -> Locator enabled for one report, then disabled.
4714 * The second parameter specifies the coordinate unit for locator
4716 * Valid values for the second parameter:
4717 * Pu = 0 <- or omitted -> default to character cells.
4718 * Pu = 1 <- device physical pixels.
4719 * Pu = 2 <- character cells.
4721 Terminal
.prototype.enableLocatorReporting = function(params
) {
4722 var val
= params
[0] > 0;
4723 //this.mouseEvents = val;
4724 //this.decLocator = val;
4729 * CSI Pt; Pl; Pb; Pr$ z
4730 * Erase Rectangular Area (DECERA), VT400 and up.
4731 * Pt; Pl; Pb; Pr denotes the rectangle.
4732 * NOTE: xterm doesn't enable this code by default.
4734 Terminal
.prototype.eraseRectangle = function(params
) {
4744 ch
= [this.eraseAttr(), ' ', 1]; // xterm?
4746 for (; t
< b
+ 1; t
++) {
4747 line
= this.lines
.get(this.ybase
+ t
);
4748 for (i
= l
; i
< r
; i
++) {
4754 this.updateRange(params
[0]);
4755 this.updateRange(params
[2]);
4761 * Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
4762 * NOTE: xterm doesn't enable this code by default.
4764 Terminal
.prototype.insertColumns = function() {
4765 var param
= params
[0]
4766 , l
= this.ybase
+ this.rows
4767 , ch
= [this.eraseAttr(), ' ', 1] // xterm?
4771 for (i
= this.ybase
; i
< l
; i
++) {
4772 this.lines
[i
].splice(this.x
+ 1, 0, ch
);
4773 this.lines
[i
].pop();
4783 * Delete P s Column(s) (default = 1) (DECDC), VT420 and up
4784 * NOTE: xterm doesn't enable this code by default.
4786 Terminal
.prototype.deleteColumns = function() {
4787 var param
= params
[0]
4788 , l
= this.ybase
+ this.rows
4789 , ch
= [this.eraseAttr(), ' ', 1] // xterm?
4793 for (i
= this.ybase
; i
< l
; i
++) {
4794 this.lines
[i
].splice(this.x
, 1);
4795 this.lines
[i
].push(ch
);
4806 Terminal
.charsets
= {};
4808 // DEC Special Character and Line Drawing Set.
4809 // http://vt100.net/docs/vt102-ug/table5-13.html
4810 // A lot of curses apps use this if they see TERM=xterm.
4811 // testing: echo -e '\e(0a\e(B'
4812 // The xterm output sometimes seems to conflict with the
4813 // reference above. xterm seems in line with the reference
4814 // when running vttest however.
4815 // The table below now uses xterm's output from vttest.
4816 Terminal
.charsets
.SCLD
= { // (0
4817 '`': '\u25c6', // '◆'
4818 'a': '\u2592', // '▒'
4819 'b': '\u0009', // '\t'
4820 'c': '\u000c', // '\f'
4821 'd': '\u000d', // '\r'
4822 'e': '\u000a', // '\n'
4823 'f': '\u00b0', // '°'
4824 'g': '\u00b1', // '±'
4825 'h': '\u2424', // '\u2424' (NL)
4826 'i': '\u000b', // '\v'
4827 'j': '\u2518', // '┘'
4828 'k': '\u2510', // '┐'
4829 'l': '\u250c', // '┌'
4830 'm': '\u2514', // '└'
4831 'n': '\u253c', // '┼'
4832 'o': '\u23ba', // '⎺'
4833 'p': '\u23bb', // '⎻'
4834 'q': '\u2500', // '─'
4835 'r': '\u23bc', // '⎼'
4836 's': '\u23bd', // '⎽'
4837 't': '\u251c', // '├'
4838 'u': '\u2524', // '┤'
4839 'v': '\u2534', // '┴'
4840 'w': '\u252c', // '┬'
4841 'x': '\u2502', // '│'
4842 'y': '\u2264', // '≤'
4843 'z': '\u2265', // '≥'
4844 '{': '\u03c0', // 'π'
4845 '|': '\u2260', // '≠'
4846 '}': '\u00a3', // '£'
4847 '~': '\u00b7' // '·'
4850 Terminal
.charsets
.UK
= null; // (A
4851 Terminal
.charsets
.US
= null; // (B (USASCII)
4852 Terminal
.charsets
.Dutch
= null; // (4
4853 Terminal
.charsets
.Finnish
= null; // (C or (5
4854 Terminal
.charsets
.French
= null; // (R
4855 Terminal
.charsets
.FrenchCanadian
= null; // (Q
4856 Terminal
.charsets
.German
= null; // (K
4857 Terminal
.charsets
.Italian
= null; // (Y
4858 Terminal
.charsets
.NorwegianDanish
= null; // (E or (6
4859 Terminal
.charsets
.Spanish
= null; // (Z
4860 Terminal
.charsets
.Swedish
= null; // (H or (7
4861 Terminal
.charsets
.Swiss
= null; // (=
4862 Terminal
.charsets
.ISOLatin
= null; // /A
4868 function on(el
, type
, handler
, capture
) {
4869 if (!Array
.isArray(el
)) {
4872 el
.forEach(function (element
) {
4873 element
.addEventListener(type
, handler
, capture
|| false);
4877 function off(el
, type
, handler
, capture
) {
4878 el
.removeEventListener(type
, handler
, capture
|| false);
4881 function cancel(ev
, force
) {
4882 if (!this.cancelEvents
&& !force
) {
4885 ev
.preventDefault();
4886 ev
.stopPropagation();
4890 function inherits(child
, parent
) {
4892 this.constructor = child
;
4894 f
.prototype = parent
.prototype;
4895 child
.prototype = new f
;
4898 // if bold is broken, we can't
4899 // use it in the terminal.
4900 function isBoldBroken(document
) {
4901 var body
= document
.getElementsByTagName('body')[0];
4902 var el
= document
.createElement('span');
4903 el
.innerHTML
= 'hello world';
4904 body
.appendChild(el
);
4905 var w1
= el
.scrollWidth
;
4906 el
.style
.fontWeight
= 'bold';
4907 var w2
= el
.scrollWidth
;
4908 body
.removeChild(el
);
4912 function indexOf(obj
, el
) {
4915 if (obj
[i
] === el
) return i
;
4920 function isThirdLevelShift(term
, ev
) {
4922 (term
.browser
.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) ||
4923 (term
.browser
.isMSWindows
&& ev
.altKey
&& ev
.ctrlKey
&& !ev
.metaKey
);
4925 if (ev
.type
== 'keypress') {
4926 return thirdLevelKey
;
4929 // Don't invoke for arrows, pageDown, home, backspace, etc. (on non-keypress events)
4930 return thirdLevelKey
&& (!ev
.keyCode
|| ev
.keyCode
> 47);
4933 function matchColor(r1
, g1
, b1
) {
4934 var hash
= (r1
<< 16) | (g1
<< 8) | b1
;
4936 if (matchColor
._cache
[hash
] != null) {
4937 return matchColor
._cache
[hash
];
4940 var ldiff
= Infinity
4949 for (; i
< Terminal
.vcolors
.length
; i
++) {
4950 c
= Terminal
.vcolors
[i
];
4955 diff
= matchColor
.distance(r1
, g1
, b1
, r2
, g2
, b2
);
4968 return matchColor
._cache
[hash
] = li
;
4971 matchColor
._cache
= {};
4973 // http://stackoverflow.com/questions/1633828
4974 matchColor
.distance = function(r1
, g1
, b1
, r2
, g2
, b2
) {
4975 return Math
.pow(30 * (r1
- r2
), 2)
4976 + Math
.pow(59 * (g1
- g2
), 2)
4977 + Math
.pow(11 * (b1
- b2
), 2);
4980 function each(obj
, iter
, con
) {
4981 if (obj
.forEach
) return obj
.forEach(iter
, con
);
4982 for (var i
= 0; i
< obj
.length
; i
++) {
4983 iter
.call(con
, obj
[i
], i
, obj
);
4987 function keys(obj
) {
4988 if (Object
.keys
) return Object
.keys(obj
);
4991 if (Object
.prototype.hasOwnProperty
.call(obj
, key
)) {
4998 var wcwidth
= (function(opts
) {
4999 // extracted from https://www.cl.cam.ac.uk/%7Emgk25/ucs/wcwidth.c
5000 // combining characters
5002 [0x0300, 0x036F], [0x0483, 0x0486], [0x0488, 0x0489],
5003 [0x0591, 0x05BD], [0x05BF, 0x05BF], [0x05C1, 0x05C2],
5004 [0x05C4, 0x05C5], [0x05C7, 0x05C7], [0x0600, 0x0603],
5005 [0x0610, 0x0615], [0x064B, 0x065E], [0x0670, 0x0670],
5006 [0x06D6, 0x06E4], [0x06E7, 0x06E8], [0x06EA, 0x06ED],
5007 [0x070F, 0x070F], [0x0711, 0x0711], [0x0730, 0x074A],
5008 [0x07A6, 0x07B0], [0x07EB, 0x07F3], [0x0901, 0x0902],
5009 [0x093C, 0x093C], [0x0941, 0x0948], [0x094D, 0x094D],
5010 [0x0951, 0x0954], [0x0962, 0x0963], [0x0981, 0x0981],
5011 [0x09BC, 0x09BC], [0x09C1, 0x09C4], [0x09CD, 0x09CD],
5012 [0x09E2, 0x09E3], [0x0A01, 0x0A02], [0x0A3C, 0x0A3C],
5013 [0x0A41, 0x0A42], [0x0A47, 0x0A48], [0x0A4B, 0x0A4D],
5014 [0x0A70, 0x0A71], [0x0A81, 0x0A82], [0x0ABC, 0x0ABC],
5015 [0x0AC1, 0x0AC5], [0x0AC7, 0x0AC8], [0x0ACD, 0x0ACD],
5016 [0x0AE2, 0x0AE3], [0x0B01, 0x0B01], [0x0B3C, 0x0B3C],
5017 [0x0B3F, 0x0B3F], [0x0B41, 0x0B43], [0x0B4D, 0x0B4D],
5018 [0x0B56, 0x0B56], [0x0B82, 0x0B82], [0x0BC0, 0x0BC0],
5019 [0x0BCD, 0x0BCD], [0x0C3E, 0x0C40], [0x0C46, 0x0C48],
5020 [0x0C4A, 0x0C4D], [0x0C55, 0x0C56], [0x0CBC, 0x0CBC],
5021 [0x0CBF, 0x0CBF], [0x0CC6, 0x0CC6], [0x0CCC, 0x0CCD],
5022 [0x0CE2, 0x0CE3], [0x0D41, 0x0D43], [0x0D4D, 0x0D4D],
5023 [0x0DCA, 0x0DCA], [0x0DD2, 0x0DD4], [0x0DD6, 0x0DD6],
5024 [0x0E31, 0x0E31], [0x0E34, 0x0E3A], [0x0E47, 0x0E4E],
5025 [0x0EB1, 0x0EB1], [0x0EB4, 0x0EB9], [0x0EBB, 0x0EBC],
5026 [0x0EC8, 0x0ECD], [0x0F18, 0x0F19], [0x0F35, 0x0F35],
5027 [0x0F37, 0x0F37], [0x0F39, 0x0F39], [0x0F71, 0x0F7E],
5028 [0x0F80, 0x0F84], [0x0F86, 0x0F87], [0x0F90, 0x0F97],
5029 [0x0F99, 0x0FBC], [0x0FC6, 0x0FC6], [0x102D, 0x1030],
5030 [0x1032, 0x1032], [0x1036, 0x1037], [0x1039, 0x1039],
5031 [0x1058, 0x1059], [0x1160, 0x11FF], [0x135F, 0x135F],
5032 [0x1712, 0x1714], [0x1732, 0x1734], [0x1752, 0x1753],
5033 [0x1772, 0x1773], [0x17B4, 0x17B5], [0x17B7, 0x17BD],
5034 [0x17C6, 0x17C6], [0x17C9, 0x17D3], [0x17DD, 0x17DD],
5035 [0x180B, 0x180D], [0x18A9, 0x18A9], [0x1920, 0x1922],
5036 [0x1927, 0x1928], [0x1932, 0x1932], [0x1939, 0x193B],
5037 [0x1A17, 0x1A18], [0x1B00, 0x1B03], [0x1B34, 0x1B34],
5038 [0x1B36, 0x1B3A], [0x1B3C, 0x1B3C], [0x1B42, 0x1B42],
5039 [0x1B6B, 0x1B73], [0x1DC0, 0x1DCA], [0x1DFE, 0x1DFF],
5040 [0x200B, 0x200F], [0x202A, 0x202E], [0x2060, 0x2063],
5041 [0x206A, 0x206F], [0x20D0, 0x20EF], [0x302A, 0x302F],
5042 [0x3099, 0x309A], [0xA806, 0xA806], [0xA80B, 0xA80B],
5043 [0xA825, 0xA826], [0xFB1E, 0xFB1E], [0xFE00, 0xFE0F],
5044 [0xFE20, 0xFE23], [0xFEFF, 0xFEFF], [0xFFF9, 0xFFFB],
5045 [0x10A01, 0x10A03], [0x10A05, 0x10A06], [0x10A0C, 0x10A0F],
5046 [0x10A38, 0x10A3A], [0x10A3F, 0x10A3F], [0x1D167, 0x1D169],
5047 [0x1D173, 0x1D182], [0x1D185, 0x1D18B], [0x1D1AA, 0x1D1AD],
5048 [0x1D242, 0x1D244], [0xE0001, 0xE0001], [0xE0020, 0xE007F],
5052 function bisearch(ucs
) {
5054 var max
= COMBINING
.length
- 1;
5056 if (ucs
< COMBINING
[0][0] || ucs
> COMBINING
[max
][1])
5058 while (max
>= min
) {
5059 mid
= Math
.floor((min
+ max
) / 2);
5060 if (ucs
> COMBINING
[mid
][1])
5062 else if (ucs
< COMBINING
[mid
][0])
5069 function wcwidth(ucs
) {
5070 // test for 8-bit control characters
5073 if (ucs
< 32 || (ucs
>= 0x7f && ucs
< 0xa0))
5074 return opts
.control
;
5075 // binary search in table of non-spacing characters
5078 // if we arrive here, ucs is not a combining or C0/C1 control character
5083 ucs
<= 0x115f || // Hangul Jamo init. consonants
5086 (ucs
>= 0x2e80 && ucs
<= 0xa4cf && ucs
!= 0x303f) || // CJK..Yi
5087 (ucs
>= 0xac00 && ucs
<= 0xd7a3) || // Hangul Syllables
5088 (ucs
>= 0xf900 && ucs
<= 0xfaff) || // CJK Compat Ideographs
5089 (ucs
>= 0xfe10 && ucs
<= 0xfe19) || // Vertical forms
5090 (ucs
>= 0xfe30 && ucs
<= 0xfe6f) || // CJK Compat Forms
5091 (ucs
>= 0xff00 && ucs
<= 0xff60) || // Fullwidth Forms
5092 (ucs
>= 0xffe0 && ucs
<= 0xffe6) ||
5093 (ucs
>= 0x20000 && ucs
<= 0x2fffd) ||
5094 (ucs
>= 0x30000 && ucs
<= 0x3fffd)
5099 })({nul
: 0, control
: 0}); // configurable options
5105 Terminal
.EventEmitter
= EventEmitter
;
5106 Terminal
.inherits
= inherits
;
5109 * Adds an event listener to the terminal.
5111 * @param {string} event The name of the event. TODO: Document all event types
5112 * @param {function} callback The function to call when the event is triggered.
5116 Terminal
.cancel
= cancel
;
5118 module
.exports
= Terminal
;