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
1231 Terminal
.prototype.scroll = function() {
1234 if (++this.ybase
=== this.scrollback
) {
1235 this.ybase
= this.ybase
/ 2;
1236 // TODO: Rely on the circular list instead of cutting it in half
1237 this.lines
.removeItemsFromStart(this.ybase
+ this.rows
- 1);
1240 if (!this.userScrolling
) {
1241 this.ydisp
= this.ybase
;
1245 row
= this.ybase
+ this.rows
- 1;
1247 // subtract the bottom scroll region
1248 row
-= this.rows
- 1 - this.scrollBottom
;
1250 if (row
=== this.lines
.length
) {
1251 // potential optimization:
1252 // pushing is faster than splicing
1253 // when they amount to the same
1255 this.lines
.push(this.blankLine());
1258 this.lines
.splice(row
, 0, this.blankLine());
1261 if (this.scrollTop
!== 0) {
1262 if (this.ybase
!== 0) {
1264 if (!this.userScrolling
) {
1265 this.ydisp
= this.ybase
;
1268 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
1272 this.updateRange(this.scrollTop
);
1273 this.updateRange(this.scrollBottom
);
1276 * This event is emitted whenever the terminal is scrolled.
1277 * The one parameter passed is the new y display position.
1281 this.emit('scroll', this.ydisp
);
1285 * Scroll the display of the terminal
1286 * @param {number} disp The number of lines to scroll down (negatives scroll up).
1287 * @param {boolean} suppressScrollEvent Don't emit the scroll event as scrollDisp. This is used
1288 * to avoid unwanted events being handled by the veiwport when the event was triggered from the
1289 * viewport originally.
1291 Terminal
.prototype.scrollDisp = function(disp
, suppressScrollEvent
) {
1293 this.userScrolling
= true;
1294 } else if (disp
+ this.ydisp
>= this.ybase
) {
1295 this.userScrolling
= false;
1300 if (this.ydisp
> this.ybase
) {
1301 this.ydisp
= this.ybase
;
1302 } else if (this.ydisp
< 0) {
1306 if (!suppressScrollEvent
) {
1307 this.emit('scroll', this.ydisp
);
1310 this.refresh(0, this.rows
- 1);
1314 * Scroll the display of the terminal by a number of pages.
1315 * @param {number} pageCount The number of pages to scroll (negative scrolls up).
1317 Terminal
.prototype.scrollPages = function(pageCount
) {
1318 this.scrollDisp(pageCount
* (this.rows
- 1));
1322 * Scrolls the display of the terminal to the top.
1324 Terminal
.prototype.scrollToTop = function() {
1325 this.scrollDisp(-this.ydisp
);
1329 * Scrolls the display of the terminal to the bottom.
1331 Terminal
.prototype.scrollToBottom = function() {
1332 this.scrollDisp(this.ybase
- this.ydisp
);
1336 * Writes text to the terminal.
1337 * @param {string} text The text to write to the terminal.
1339 Terminal
.prototype.write = function(data
) {
1340 var l
= data
.length
, i
= 0, j
, cs
, ch
, code
, low
, ch_width
, row
;
1342 this.refreshStart
= this.y
;
1343 this.refreshEnd
= this.y
;
1345 // apply leftover surrogate high from last write
1346 if (this.surrogate_high
) {
1347 data
= this.surrogate_high
+ data
;
1348 this.surrogate_high
= '';
1351 for (; i
< l
; i
++) {
1354 // FIXME: higher chars than 0xa0 are not allowed in escape sequences
1355 // --> maybe move to default
1356 code
= data
.charCodeAt(i
);
1357 if (0xD800 <= code
&& code
<= 0xDBFF) {
1358 // we got a surrogate high
1359 // get surrogate low (next 2 bytes)
1360 low
= data
.charCodeAt(i
+1);
1362 // end of data stream, save surrogate high
1363 this.surrogate_high
= ch
;
1366 code
= ((code
- 0xD800) * 0x400) + (low
- 0xDC00) + 0x10000;
1367 ch
+= data
.charAt(i
+1);
1369 // surrogate low - already handled above
1370 if (0xDC00 <= code
&& code
<= 0xDFFF)
1372 switch (this.state
) {
1383 if (this.convertEol
) {
1387 if (this.y
> this.scrollBottom
) {
1407 this.x
= this.nextStop();
1422 this.state
= escaped
;
1427 // calculate print space
1428 // expensive call, therefore we save width in line buffer
1429 ch_width
= wcwidth(code
);
1432 if (this.charset
&& this.charset
[ch
]) {
1433 ch
= this.charset
[ch
];
1436 row
= this.y
+ this.ybase
;
1438 // insert combining char in last cell
1439 // FIXME: needs handling after cursor jumps
1440 if (!ch_width
&& this.x
) {
1441 // dont overflow left
1442 if (this.lines
.get(row
)[this.x
-1]) {
1443 if (!this.lines
.get(row
)[this.x
-1][2]) {
1445 // found empty cell after fullwidth, need to go 2 cells back
1446 if (this.lines
.get(row
)[this.x
-2])
1447 this.lines
.get(row
)[this.x
-2][1] += ch
;
1450 this.lines
.get(row
)[this.x
-1][1] += ch
;
1452 this.updateRange(this.y
);
1457 // goto next line if ch would overflow
1458 // TODO: needs a global min terminal width of 2
1459 if (this.x
+ch_width
-1 >= this.cols
) {
1460 // autowrap - DECAWM
1461 if (this.wraparoundMode
) {
1464 if (this.y
> this.scrollBottom
) {
1469 this.x
= this.cols
-1;
1470 if(ch_width
===2) // FIXME: check for xterm behavior
1474 row
= this.y
+ this.ybase
;
1476 // insert mode: move characters to right
1477 if (this.insertMode
) {
1478 // do this twice for a fullwidth char
1479 for (var moves
=0; moves
<ch_width
; ++moves
) {
1480 // remove last cell, if it's width is 0
1481 // we have to adjust the second last cell as well
1482 var removed
= this.lines
.get(this.y
+ this.ybase
).pop();
1484 && this.lines
.get(row
)[this.cols
-2]
1485 && this.lines
.get(row
)[this.cols
-2][2]===2)
1486 this.lines
.get(row
)[this.cols
-2] = [this.curAttr
, ' ', 1];
1488 // insert empty cell at cursor
1489 this.lines
.get(row
).splice(this.x
, 0, [this.curAttr
, ' ', 1]);
1493 this.lines
.get(row
)[this.x
] = [this.curAttr
, ch
, ch_width
];
1495 this.updateRange(this.y
);
1497 // fullwidth char - set next cell width to zero and advance cursor
1499 this.lines
.get(row
)[this.x
] = [this.curAttr
, '', 0];
1508 // ESC [ Control Sequence Introducer ( CSI is 0x9b).
1511 this.currentParam
= 0;
1515 // ESC ] Operating System Command ( OSC is 0x9d).
1518 this.currentParam
= 0;
1522 // ESC P Device Control String ( DCS is 0x90).
1525 this.currentParam
= 0;
1529 // ESC _ Application Program Command ( APC is 0x9f).
1531 this.state
= ignore
;
1534 // ESC ^ Privacy Message ( PM is 0x9e).
1536 this.state
= ignore
;
1539 // ESC c Full Reset (RIS).
1544 // ESC E Next Line ( NEL is 0x85).
1545 // ESC D Index ( IND is 0x84).
1553 // ESC M Reverse Index ( RI is 0x8d).
1555 this.reverseIndex();
1558 // ESC % Select default/utf-8 character set.
1559 // @ = default, G = utf-8
1561 //this.charset = null;
1563 this.setgCharset(0, Terminal
.charsets
.US
);
1564 this.state
= normal
;
1568 // ESC (,),*,+,-,. Designate G0-G2 Character Set.
1569 case '(': // <-- this seems to get all the attention
1595 this.state
= charset
;
1598 // Designate G3 Character Set (VT300).
1599 // A = ISO Latin-1 Supplemental.
1603 this.state
= charset
;
1608 // Single Shift Select of G2 Character Set
1609 // ( SS2 is 0x8e). This affects next character only.
1613 // Single Shift Select of G3 Character Set
1614 // ( SS3 is 0x8f). This affects next character only.
1618 // Invoke the G2 Character Set as GL (LS2).
1623 // Invoke the G3 Character Set as GL (LS3).
1628 // Invoke the G3 Character Set as GR (LS3R).
1633 // Invoke the G2 Character Set as GR (LS2R).
1638 // Invoke the G1 Character Set as GR (LS1R).
1643 // ESC 7 Save Cursor (DECSC).
1646 this.state
= normal
;
1649 // ESC 8 Restore Cursor (DECRC).
1651 this.restoreCursor();
1652 this.state
= normal
;
1655 // ESC # 3 DEC line height/width
1657 this.state
= normal
;
1661 // ESC H Tab Set (HTS is 0x88).
1666 // ESC = Application Keypad (DECKPAM).
1668 this.log('Serial port requested application keypad.');
1669 this.applicationKeypad
= true;
1670 this.viewport
.syncScrollArea();
1671 this.state
= normal
;
1674 // ESC > Normal Keypad (DECKPNM).
1676 this.log('Switching back to normal keypad.');
1677 this.applicationKeypad
= false;
1678 this.viewport
.syncScrollArea();
1679 this.state
= normal
;
1683 this.state
= normal
;
1684 this.error('Unknown ESC control: %s.', ch
);
1691 case '0': // DEC Special Character and Line Drawing Set.
1692 cs
= Terminal
.charsets
.SCLD
;
1695 cs
= Terminal
.charsets
.UK
;
1697 case 'B': // United States (USASCII).
1698 cs
= Terminal
.charsets
.US
;
1701 cs
= Terminal
.charsets
.Dutch
;
1703 case 'C': // Finnish
1705 cs
= Terminal
.charsets
.Finnish
;
1708 cs
= Terminal
.charsets
.French
;
1710 case 'Q': // FrenchCanadian
1711 cs
= Terminal
.charsets
.FrenchCanadian
;
1714 cs
= Terminal
.charsets
.German
;
1716 case 'Y': // Italian
1717 cs
= Terminal
.charsets
.Italian
;
1719 case 'E': // NorwegianDanish
1721 cs
= Terminal
.charsets
.NorwegianDanish
;
1723 case 'Z': // Spanish
1724 cs
= Terminal
.charsets
.Spanish
;
1726 case 'H': // Swedish
1728 cs
= Terminal
.charsets
.Swedish
;
1731 cs
= Terminal
.charsets
.Swiss
;
1733 case '/': // ISOLatin (actually /A)
1734 cs
= Terminal
.charsets
.ISOLatin
;
1738 cs
= Terminal
.charsets
.US
;
1741 this.setgCharset(this.gcharset
, cs
);
1742 this.gcharset
= null;
1743 this.state
= normal
;
1749 // Set Text Parameters.
1750 if (ch
=== '\x1b' || ch
=== '\x07') {
1751 if (ch
=== '\x1b') i
++;
1753 this.params
.push(this.currentParam
);
1755 switch (this.params
[0]) {
1759 if (this.params
[1]) {
1760 this.title
= this.params
[1];
1761 this.handleTitle(this.title
);
1769 // change dynamic colors
1781 // change dynamic ui colors
1793 // manipulate selection data
1811 this.currentParam
= 0;
1812 this.state
= normal
;
1814 if (!this.params
.length
) {
1815 if (ch
>= '0' && ch
<= '9') {
1817 this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1818 } else if (ch
=== ';') {
1819 this.params
.push(this.currentParam
);
1820 this.currentParam
= '';
1823 this.currentParam
+= ch
;
1830 if (ch
=== '?' || ch
=== '>' || ch
=== '!') {
1836 if (ch
>= '0' && ch
<= '9') {
1837 this.currentParam
= this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1841 // '$', '"', ' ', '\''
1842 if (ch
=== '$' || ch
=== '"' || ch
=== ' ' || ch
=== '\'') {
1847 this.params
.push(this.currentParam
);
1848 this.currentParam
= 0;
1851 if (ch
=== ';') break;
1853 this.state
= normal
;
1857 // Cursor Up Ps Times (default = 1) (CUU).
1859 this.cursorUp(this.params
);
1863 // Cursor Down Ps Times (default = 1) (CUD).
1865 this.cursorDown(this.params
);
1869 // Cursor Forward Ps Times (default = 1) (CUF).
1871 this.cursorForward(this.params
);
1875 // Cursor Backward Ps Times (default = 1) (CUB).
1877 this.cursorBackward(this.params
);
1881 // Cursor Position [row;column] (default = [1,1]) (CUP).
1883 this.cursorPos(this.params
);
1886 // CSI Ps J Erase in Display (ED).
1888 this.eraseInDisplay(this.params
);
1891 // CSI Ps K Erase in Line (EL).
1893 this.eraseInLine(this.params
);
1896 // CSI Pm m Character Attributes (SGR).
1899 this.charAttributes(this.params
);
1903 // CSI Ps n Device Status Report (DSR).
1906 this.deviceStatus(this.params
);
1915 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
1917 this.insertChars(this.params
);
1921 // Cursor Next Line Ps Times (default = 1) (CNL).
1923 this.cursorNextLine(this.params
);
1927 // Cursor Preceding Line Ps Times (default = 1) (CNL).
1929 this.cursorPrecedingLine(this.params
);
1933 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
1935 this.cursorCharAbsolute(this.params
);
1939 // Insert Ps Line(s) (default = 1) (IL).
1941 this.insertLines(this.params
);
1945 // Delete Ps Line(s) (default = 1) (DL).
1947 this.deleteLines(this.params
);
1951 // Delete Ps Character(s) (default = 1) (DCH).
1953 this.deleteChars(this.params
);
1957 // Erase Ps Character(s) (default = 1) (ECH).
1959 this.eraseChars(this.params
);
1962 // CSI Pm ` Character Position Absolute
1963 // [column] (default = [row,1]) (HPA).
1965 this.charPosAbsolute(this.params
);
1969 // Horizontal Position Relative
1971 this.HPositionRelative(this.params
);
1975 // Send Device Attributes (Primary DA).
1977 // Send Device Attributes (Secondary DA)
1979 this.sendDeviceAttributes(this.params
);
1983 // Line Position Absolute [row] (default = [1,column]) (VPA).
1985 this.linePosAbsolute(this.params
);
1988 // 145 65 e * VPR - Vertical Position Relative
1990 this.VPositionRelative(this.params
);
1994 // Horizontal and Vertical Position [row;column] (default =
1997 this.HVPosition(this.params
);
2000 // CSI Pm h Set Mode (SM).
2001 // CSI ? Pm h - mouse escape codes, cursor escape codes
2003 this.setMode(this.params
);
2006 // CSI Pm l Reset Mode (RM).
2009 this.resetMode(this.params
);
2013 // Set Scrolling Region [top;bottom] (default = full size of win-
2017 this.setScrollRegion(this.params
);
2021 // Save cursor (ANSI.SYS).
2023 this.saveCursor(this.params
);
2027 // Restore cursor (ANSI.SYS).
2029 this.restoreCursor(this.params
);
2037 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
2039 this.cursorForwardTab(this.params
);
2042 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
2044 this.scrollUp(this.params
);
2047 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
2048 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
2051 // if (this.prefix === '>') {
2052 // this.resetTitleModes(this.params);
2055 // if (this.params.length > 2) {
2056 // this.initMouseTracking(this.params);
2059 if (this.params
.length
< 2 && !this.prefix
) {
2060 this.scrollDown(this.params
);
2065 // Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
2067 this.cursorBackwardTab(this.params
);
2070 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
2072 this.repeatPrecedingCharacter(this.params
);
2075 // CSI Ps g Tab Clear (TBC).
2077 this.tabClear(this.params
);
2080 // CSI Pm i Media Copy (MC).
2083 // this.mediaCopy(this.params);
2086 // CSI Pm m Character Attributes (SGR).
2088 // case 'm': // duplicate
2089 // if (this.prefix === '>') {
2090 // this.setResources(this.params);
2092 // this.charAttributes(this.params);
2096 // CSI Ps n Device Status Report (DSR).
2098 // case 'n': // duplicate
2099 // if (this.prefix === '>') {
2100 // this.disableModifiers(this.params);
2102 // this.deviceStatus(this.params);
2106 // CSI > Ps p Set pointer mode.
2107 // CSI ! p Soft terminal reset (DECSTR).
2109 // Request ANSI mode (DECRQM).
2111 // Request DEC private mode (DECRQM).
2114 switch (this.prefix
) {
2116 // this.setPointerMode(this.params);
2119 this.softReset(this.params
);
2122 // if (this.postfix === '$') {
2123 // this.requestPrivateMode(this.params);
2127 // if (this.postfix === '"') {
2128 // this.setConformanceLevel(this.params);
2129 // } else if (this.postfix === '$') {
2130 // this.requestAnsiMode(this.params);
2136 // CSI Ps q Load LEDs (DECLL).
2140 // if (this.postfix === ' ') {
2141 // this.setCursorStyle(this.params);
2144 // if (this.postfix === '"') {
2145 // this.setCharProtectionAttr(this.params);
2148 // this.loadLEDs(this.params);
2152 // Set Scrolling Region [top;bottom] (default = full size of win-
2155 // CSI Pt; Pl; Pb; Pr; Ps$ r
2156 // case 'r': // duplicate
2157 // if (this.prefix === '?') {
2158 // this.restorePrivateValues(this.params);
2159 // } else if (this.postfix === '$') {
2160 // this.setAttrInRectangle(this.params);
2162 // this.setScrollRegion(this.params);
2166 // CSI s Save cursor (ANSI.SYS).
2168 // case 's': // duplicate
2169 // if (this.prefix === '?') {
2170 // this.savePrivateValues(this.params);
2172 // this.saveCursor(this.params);
2176 // CSI Ps ; Ps ; Ps t
2177 // CSI Pt; Pl; Pb; Pr; Ps$ t
2181 // if (this.postfix === '$') {
2182 // this.reverseAttrInRectangle(this.params);
2183 // } else if (this.postfix === ' ') {
2184 // this.setWarningBellVolume(this.params);
2186 // if (this.prefix === '>') {
2187 // this.setTitleModeFeature(this.params);
2189 // this.manipulateWindow(this.params);
2194 // CSI u Restore cursor (ANSI.SYS).
2196 // case 'u': // duplicate
2197 // if (this.postfix === ' ') {
2198 // this.setMarginBellVolume(this.params);
2200 // this.restoreCursor(this.params);
2204 // CSI Pt; Pl; Pb; Pr; Pp; Pt; Pl; Pp$ v
2206 // if (this.postfix === '$') {
2207 // this.copyRectagle(this.params);
2211 // CSI Pt ; Pl ; Pb ; Pr ' w
2213 // if (this.postfix === '\'') {
2214 // this.enableFilterRectangle(this.params);
2218 // CSI Ps x Request Terminal Parameters (DECREQTPARM).
2219 // CSI Ps x Select Attribute Change Extent (DECSACE).
2220 // CSI Pc; Pt; Pl; Pb; Pr$ x
2222 // if (this.postfix === '$') {
2223 // this.fillRectangle(this.params);
2225 // this.requestParameters(this.params);
2226 // //this.__(this.params);
2231 // CSI Pt; Pl; Pb; Pr$ z
2233 // if (this.postfix === '\'') {
2234 // this.enableLocatorReporting(this.params);
2235 // } else if (this.postfix === '$') {
2236 // this.eraseRectangle(this.params);
2241 // CSI Pt; Pl; Pb; Pr$ {
2243 // if (this.postfix === '\'') {
2244 // this.setLocatorEvents(this.params);
2245 // } else if (this.postfix === '$') {
2246 // this.selectiveEraseRectangle(this.params);
2252 // if (this.postfix === '\'') {
2253 // this.requestLocatorPosition(this.params);
2258 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
2260 // if (this.postfix === ' ') {
2261 // this.insertColumns(this.params);
2266 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
2268 // if (this.postfix === ' ') {
2269 // this.deleteColumns(this.params);
2274 this.error('Unknown CSI code: %s.', ch
);
2283 if (ch
=== '\x1b' || ch
=== '\x07') {
2284 if (ch
=== '\x1b') i
++;
2286 switch (this.prefix
) {
2287 // User-Defined Keys (DECUDK).
2291 // Request Status String (DECRQSS).
2292 // test: echo -e '\eP$q"p\e\\'
2294 var pt
= this.currentParam
2311 + (this.scrollTop
+ 1)
2313 + (this.scrollBottom
+ 1)
2323 this.error('Unknown DCS Pt: %s.', pt
);
2328 this.send('\x1bP' + +valid
+ '$r' + pt
+ '\x1b\\');
2331 // Set Termcap/Terminfo Data (xterm, experimental).
2335 // Request Termcap/Terminfo String (xterm, experimental)
2336 // Regular xterm does not even respond to this sequence.
2337 // This can cause a small glitch in vim.
2338 // test: echo -ne '\eP+q6b64\e\\'
2340 var pt
= this.currentParam
2343 this.send('\x1bP' + +valid
+ '+r' + pt
+ '\x1b\\');
2347 this.error('Unknown DCS prefix: %s.', this.prefix
);
2351 this.currentParam
= 0;
2353 this.state
= normal
;
2354 } else if (!this.currentParam
) {
2355 if (!this.prefix
&& ch
!== '$' && ch
!== '+') {
2356 this.currentParam
= ch
;
2357 } else if (this.prefix
.length
=== 2) {
2358 this.currentParam
= ch
;
2363 this.currentParam
+= ch
;
2369 if (ch
=== '\x1b' || ch
=== '\x07') {
2370 if (ch
=== '\x1b') i
++;
2371 this.state
= normal
;
2377 this.updateRange(this.y
);
2378 this.refresh(this.refreshStart
, this.refreshEnd
);
2382 * Writes text to the terminal, followed by a break line character (\n).
2383 * @param {string} text The text to write to the terminal.
2385 Terminal
.prototype.writeln = function(data
) {
2386 this.write(data
+ '\r\n');
2390 * Attaches a custom keydown handler which is run before keys are processed, giving consumers of
2391 * xterm.js ultimate control as to what keys should be processed by the terminal and what keys
2393 * @param {function} customKeydownHandler The custom KeyboardEvent handler to attach. This is a
2394 * function that takes a KeyboardEvent, allowing consumers to stop propogation and/or prevent
2395 * the default action. The function returns whether the event should be processed by xterm.js.
2397 Terminal
.prototype.attachCustomKeydownHandler = function(customKeydownHandler
) {
2398 this.customKeydownHandler
= customKeydownHandler
;
2402 * Handle a keydown event
2404 * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
2405 * @param {KeyboardEvent} ev The keydown event to be handled.
2407 Terminal
.prototype.keyDown = function(ev
) {
2408 if (this.customKeydownHandler
&& this.customKeydownHandler(ev
) === false) {
2412 if (!this.compositionHelper
.keydown
.bind(this.compositionHelper
)(ev
)) {
2413 if (this.ybase
!== this.ydisp
) {
2414 this.scrollToBottom();
2420 var result
= this.evaluateKeyEscapeSequence(ev
);
2422 if (result
.scrollDisp
) {
2423 this.scrollDisp(result
.scrollDisp
);
2424 return this.cancel(ev
, true);
2427 if (isThirdLevelShift(this, ev
)) {
2431 if (result
.cancel
) {
2432 // The event is canceled at the end already, is this necessary?
2433 this.cancel(ev
, true);
2440 this.emit('keydown', ev
);
2441 this.emit('key', result
.key
, ev
);
2443 this.handler(result
.key
);
2445 return this.cancel(ev
, true);
2449 * Returns an object that determines how a KeyboardEvent should be handled. The key of the
2450 * returned value is the new key code to pass to the PTY.
2452 * Reference: http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
2453 * @param {KeyboardEvent} ev The keyboard event to be translated to key escape sequence.
2455 Terminal
.prototype.evaluateKeyEscapeSequence = function(ev
) {
2457 // Whether to cancel event propogation (NOTE: this may not be needed since the event is
2458 // canceled at the end of keyDown
2460 // The new key even to emit
2462 // The number of characters to scroll, if this is defined it will cancel the event
2463 scrollDisp
: undefined
2465 var modifiers
= ev
.shiftKey
<< 0 | ev
.altKey
<< 1 | ev
.ctrlKey
<< 2 | ev
.metaKey
<< 3;
2466 switch (ev
.keyCode
) {
2470 result
.key
= '\x08'; // ^H
2473 result
.key
= '\x7f'; // ^?
2478 result
.key
= '\x1b[Z';
2482 result
.cancel
= true;
2487 result
.cancel
= true;
2491 result
.key
= '\x1b';
2492 result
.cancel
= true;
2497 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'D';
2498 // HACK: Make Alt + left-arrow behave like Ctrl + left-arrow: move one word backwards
2499 // http://unix.stackexchange.com/a/108106
2500 if (result
.key
== '\x1b[1;3D') {
2501 result
.key
= '\x1b[1;5D';
2503 } else if (this.applicationCursor
) {
2504 result
.key
= '\x1bOD';
2506 result
.key
= '\x1b[D';
2512 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'C';
2513 // HACK: Make Alt + right-arrow behave like Ctrl + right-arrow: move one word forward
2514 // http://unix.stackexchange.com/a/108106
2515 if (result
.key
== '\x1b[1;3C') {
2516 result
.key
= '\x1b[1;5C';
2518 } else if (this.applicationCursor
) {
2519 result
.key
= '\x1bOC';
2521 result
.key
= '\x1b[C';
2527 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'A';
2528 // HACK: Make Alt + up-arrow behave like Ctrl + up-arrow
2529 // http://unix.stackexchange.com/a/108106
2530 if (result
.key
== '\x1b[1;3A') {
2531 result
.key
= '\x1b[1;5A';
2533 } else if (this.applicationCursor
) {
2534 result
.key
= '\x1bOA';
2536 result
.key
= '\x1b[A';
2542 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'B';
2543 // HACK: Make Alt + down-arrow behave like Ctrl + down-arrow
2544 // http://unix.stackexchange.com/a/108106
2545 if (result
.key
== '\x1b[1;3B') {
2546 result
.key
= '\x1b[1;5B';
2548 } else if (this.applicationCursor
) {
2549 result
.key
= '\x1bOB';
2551 result
.key
= '\x1b[B';
2556 if (!ev
.shiftKey
&& !ev
.ctrlKey
) {
2557 // <Ctrl> or <Shift> + <Insert> are used to
2558 // copy-paste on some systems.
2559 result
.key
= '\x1b[2~';
2565 result
.key
= '\x1b[3;' + (modifiers
+ 1) + '~';
2567 result
.key
= '\x1b[3~';
2573 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'H';
2574 else if (this.applicationCursor
)
2575 result
.key
= '\x1bOH';
2577 result
.key
= '\x1b[H';
2582 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'F';
2583 else if (this.applicationCursor
)
2584 result
.key
= '\x1bOF';
2586 result
.key
= '\x1b[F';
2591 result
.scrollDisp
= -(this.rows
- 1);
2593 result
.key
= '\x1b[5~';
2599 result
.scrollDisp
= this.rows
- 1;
2601 result
.key
= '\x1b[6~';
2607 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'P';
2609 result
.key
= '\x1bOP';
2614 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'Q';
2616 result
.key
= '\x1bOQ';
2621 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'R';
2623 result
.key
= '\x1bOR';
2628 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'S';
2630 result
.key
= '\x1bOS';
2635 result
.key
= '\x1b[15;' + (modifiers
+ 1) + '~';
2637 result
.key
= '\x1b[15~';
2642 result
.key
= '\x1b[17;' + (modifiers
+ 1) + '~';
2644 result
.key
= '\x1b[17~';
2649 result
.key
= '\x1b[18;' + (modifiers
+ 1) + '~';
2651 result
.key
= '\x1b[18~';
2656 result
.key
= '\x1b[19;' + (modifiers
+ 1) + '~';
2658 result
.key
= '\x1b[19~';
2663 result
.key
= '\x1b[20;' + (modifiers
+ 1) + '~';
2665 result
.key
= '\x1b[20~';
2670 result
.key
= '\x1b[21;' + (modifiers
+ 1) + '~';
2672 result
.key
= '\x1b[21~';
2677 result
.key
= '\x1b[23;' + (modifiers
+ 1) + '~';
2679 result
.key
= '\x1b[23~';
2684 result
.key
= '\x1b[24;' + (modifiers
+ 1) + '~';
2686 result
.key
= '\x1b[24~';
2691 if (ev
.ctrlKey
&& !ev
.shiftKey
&& !ev
.altKey
&& !ev
.metaKey
) {
2692 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2693 result
.key
= String
.fromCharCode(ev
.keyCode
- 64);
2694 } else if (ev
.keyCode
=== 32) {
2696 result
.key
= String
.fromCharCode(0);
2697 } else if (ev
.keyCode
>= 51 && ev
.keyCode
<= 55) {
2698 // escape, file sep, group sep, record sep, unit sep
2699 result
.key
= String
.fromCharCode(ev
.keyCode
- 51 + 27);
2700 } else if (ev
.keyCode
=== 56) {
2702 result
.key
= String
.fromCharCode(127);
2703 } else if (ev
.keyCode
=== 219) {
2704 // ^[ - Control Sequence Introducer (CSI)
2705 result
.key
= String
.fromCharCode(27);
2706 } else if (ev
.keyCode
=== 220) {
2707 // ^\ - String Terminator (ST)
2708 result
.key
= String
.fromCharCode(28);
2709 } else if (ev
.keyCode
=== 221) {
2710 // ^] - Operating System Command (OSC)
2711 result
.key
= String
.fromCharCode(29);
2713 } else if (!this.browser
.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) {
2714 // On Mac this is a third level shift. Use <Esc> instead.
2715 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2716 result
.key
= '\x1b' + String
.fromCharCode(ev
.keyCode
+ 32);
2717 } else if (ev
.keyCode
=== 192) {
2718 result
.key
= '\x1b`';
2719 } else if (ev
.keyCode
>= 48 && ev
.keyCode
<= 57) {
2720 result
.key
= '\x1b' + (ev
.keyCode
- 48);
2729 * Set the G level of the terminal
2732 Terminal
.prototype.setgLevel = function(g
) {
2734 this.charset
= this.charsets
[g
];
2738 * Set the charset for the given G level of the terminal
2742 Terminal
.prototype.setgCharset = function(g
, charset
) {
2743 this.charsets
[g
] = charset
;
2744 if (this.glevel
=== g
) {
2745 this.charset
= charset
;
2750 * Handle a keypress event.
2752 * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
2753 * @param {KeyboardEvent} ev The keypress event to be handled.
2755 Terminal
.prototype.keyPress = function(ev
) {
2762 } else if (ev
.which
== null) {
2764 } else if (ev
.which
!== 0 && ev
.charCode
!== 0) {
2771 (ev
.altKey
|| ev
.ctrlKey
|| ev
.metaKey
) && !isThirdLevelShift(this, ev
)
2776 key
= String
.fromCharCode(key
);
2778 this.emit('keypress', key
, ev
);
2779 this.emit('key', key
, ev
);
2787 * Send data for handling to the terminal
2788 * @param {string} data
2790 Terminal
.prototype.send = function(data
) {
2794 setTimeout(function() {
2795 self
.handler(self
.queue
);
2805 * Note: We could do sweet things with webaudio here
2807 Terminal
.prototype.bell = function() {
2808 if (!this.visualBell
) return;
2810 this.element
.style
.borderColor
= 'white';
2811 setTimeout(function() {
2812 self
.element
.style
.borderColor
= '';
2814 if (this.popOnBell
) this.focus();
2818 * Log the current state to the console.
2820 Terminal
.prototype.log = function() {
2821 if (!this.debug
) return;
2822 if (!this.context
.console
|| !this.context
.console
.log
) return;
2823 var args
= Array
.prototype.slice
.call(arguments
);
2824 this.context
.console
.log
.apply(this.context
.console
, args
);
2828 * Log the current state as error to the console.
2830 Terminal
.prototype.error = function() {
2831 if (!this.debug
) return;
2832 if (!this.context
.console
|| !this.context
.console
.error
) return;
2833 var args
= Array
.prototype.slice
.call(arguments
);
2834 this.context
.console
.error
.apply(this.context
.console
, args
);
2838 * Resizes the terminal.
2840 * @param {number} x The number of columns to resize to.
2841 * @param {number} y The number of rows to resize to.
2843 Terminal
.prototype.resize = function(x
, y
) {
2851 if (x
=== this.cols
&& y
=== this.rows
) {
2861 ch
= [this.defAttr
, ' ', 1]; // does xterm use the default attr?
2862 i
= this.lines
.length
;
2864 while (this.lines
.get(i
).length
< x
) {
2865 this.lines
.get(i
).push(ch
);
2869 i
= this.lines
.length
;
2871 while (this.lines
.get(i
).length
> x
) {
2872 this.lines
.get(i
).pop();
2885 // y is rows, not this.y
2886 if (this.lines
.length
< y
+ this.ybase
) {
2887 if (this.ybase
> 0 && this.lines
.length
<= this.ybase
+ this.y
+ addToY
+ 1) {
2888 // There is room above the buffer and there are no empty elements below the line,
2892 if (this.ydisp
> 0) {
2893 // Viewport is at the top of the buffer, must increase downwards
2897 // Add a blank line if there is no buffer left at the top to scroll to, or if there
2898 // are blank lines after the cursor
2899 this.lines
.push(this.blankLine());
2902 if (this.children
.length
< y
) {
2908 if (this.lines
.length
> y
+ this.ybase
) {
2909 if (this.lines
.length
> this.ybase
+ this.y
+ 1) {
2910 // The line is a blank line below the cursor, remove it
2913 // The line is the cursor, scroll down
2918 if (this.children
.length
> y
) {
2919 el
= this.children
.shift();
2921 el
.parentNode
.removeChild(el
);
2927 // Make sure that the cursor stays on screen
2940 this.scrollBottom
= y
- 1;
2942 this.refresh(0, this.rows
- 1);
2946 this.geometry
= [this.cols
, this.rows
];
2947 this.emit('resize', {terminal
: this, cols
: x
, rows
: y
});
2951 * Updates the range of rows to refresh
2952 * @param {number} y The number of rows to refresh next.
2954 Terminal
.prototype.updateRange = function(y
) {
2955 if (y
< this.refreshStart
) this.refreshStart
= y
;
2956 if (y
> this.refreshEnd
) this.refreshEnd
= y
;
2957 // if (y > this.refreshEnd) {
2958 // this.refreshEnd = y;
2959 // if (y > this.rows - 1) {
2960 // this.refreshEnd = this.rows - 1;
2966 * Set the range of refreshing to the maximum value
2968 Terminal
.prototype.maxRange = function() {
2969 this.refreshStart
= 0;
2970 this.refreshEnd
= this.rows
- 1;
2976 * Setup the tab stops.
2979 Terminal
.prototype.setupStops = function(i
) {
2981 if (!this.tabs
[i
]) {
2982 i
= this.prevStop(i
);
2989 for (; i
< this.cols
; i
+= 8) {
2990 this.tabs
[i
] = true;
2996 * Move the cursor to the previous tab stop from the given position (default is current).
2997 * @param {number} x The position to move the cursor to the previous tab stop.
2999 Terminal
.prototype.prevStop = function(x
) {
3000 if (x
== null) x
= this.x
;
3001 while (!this.tabs
[--x
] && x
> 0);
3002 return x
>= this.cols
3009 * Move the cursor one tab stop forward from the given position (default is current).
3010 * @param {number} x The position to move the cursor one tab stop forward.
3012 Terminal
.prototype.nextStop = function(x
) {
3013 if (x
== null) x
= this.x
;
3014 while (!this.tabs
[++x
] && x
< this.cols
);
3015 return x
>= this.cols
3022 * Erase in the identified line everything from "x" to the end of the line (right).
3023 * @param {number} x The column from which to start erasing to the end of the line.
3024 * @param {number} y The line in which to operate.
3026 Terminal
.prototype.eraseRight = function(x
, y
) {
3027 var line
= this.lines
.get(this.ybase
+ y
)
3028 , ch
= [this.eraseAttr(), ' ', 1]; // xterm
3031 for (; x
< this.cols
; x
++) {
3035 this.updateRange(y
);
3041 * Erase in the identified line everything from "x" to the start of the line (left).
3042 * @param {number} x The column from which to start erasing to the start of the line.
3043 * @param {number} y The line in which to operate.
3045 Terminal
.prototype.eraseLeft = function(x
, y
) {
3046 var line
= this.lines
.get(this.ybase
+ y
)
3047 , ch
= [this.eraseAttr(), ' ', 1]; // xterm
3050 while (x
--) line
[x
] = ch
;
3052 this.updateRange(y
);
3056 * Clears the entire buffer, making the prompt line the new first line.
3058 Terminal
.prototype.clear = function() {
3059 if (this.ybase
=== 0 && this.y
=== 0) {
3060 // Don't clear if it's already clear
3063 this.lines
.set(0, this.lines
.get(this.ybase
+ this.y
));
3064 this.lines
.length
= 1;
3068 for (var i
= 1; i
< this.rows
; i
++) {
3069 this.lines
.push(this.blankLine());
3071 this.refresh(0, this.rows
- 1);
3072 this.emit('scroll', this.ydisp
);
3076 * Erase all content in the given line
3077 * @param {number} y The line to erase all of its contents.
3079 Terminal
.prototype.eraseLine = function(y
) {
3080 this.eraseRight(0, y
);
3085 * Return the data array of a blank line/
3086 * @param {number} cur First bunch of data for each "blank" character.
3088 Terminal
.prototype.blankLine = function(cur
) {
3093 var ch
= [attr
, ' ', 1] // width defaults to 1 halfwidth character
3097 for (; i
< this.cols
; i
++) {
3106 * If cur return the back color xterm feature attribute. Else return defAttr.
3107 * @param {object} cur
3109 Terminal
.prototype.ch = function(cur
) {
3111 ? [this.eraseAttr(), ' ', 1]
3112 : [this.defAttr
, ' ', 1];
3117 * Evaluate if the current erminal is the given argument.
3118 * @param {object} term The terminal to evaluate
3120 Terminal
.prototype.is = function(term
) {
3121 var name
= this.termName
;
3122 return (name
+ '').indexOf(term
) === 0;
3127 * Emit the 'data' event and populate the given data.
3128 * @param {string} data The data to populate in the event.
3130 Terminal
.prototype.handler = function(data
) {
3131 // Input is being sent to the terminal, the terminal should focus the prompt.
3132 if (this.ybase
!== this.ydisp
) {
3133 this.scrollToBottom();
3135 this.emit('data', data
);
3140 * Emit the 'title' event and populate the given title.
3141 * @param {string} title The title to populate in the event.
3143 Terminal
.prototype.handleTitle = function(title
) {
3145 * This event is emitted when the title of the terminal is changed
3146 * from inside the terminal. The parameter is the new title.
3150 this.emit('title', title
);
3159 * ESC D Index (IND is 0x84).
3161 Terminal
.prototype.index = function() {
3163 if (this.y
> this.scrollBottom
) {
3167 this.state
= normal
;
3172 * ESC M Reverse Index (RI is 0x8d).
3174 Terminal
.prototype.reverseIndex = function() {
3177 if (this.y
< this.scrollTop
) {
3179 // possibly move the code below to term.reverseScroll();
3180 // test: echo -ne '\e[1;1H\e[44m\eM\e[0m'
3181 // blankLine(true) is xterm/linux behavior
3182 this.lines
.splice(this.y
+ this.ybase
, 0, this.blankLine(true));
3183 j
= this.rows
- 1 - this.scrollBottom
;
3184 this.lines
.splice(this.rows
- 1 + this.ybase
- j
+ 1, 1);
3186 this.updateRange(this.scrollTop
);
3187 this.updateRange(this.scrollBottom
);
3189 this.state
= normal
;
3194 * ESC c Full Reset (RIS).
3196 Terminal
.prototype.reset = function() {
3197 this.options
.rows
= this.rows
;
3198 this.options
.cols
= this.cols
;
3199 var customKeydownHandler
= this.customKeydownHandler
;
3200 Terminal
.call(this, this.options
);
3201 this.customKeydownHandler
= customKeydownHandler
;
3202 this.refresh(0, this.rows
- 1);
3203 this.viewport
.syncScrollArea();
3208 * ESC H Tab Set (HTS is 0x88).
3210 Terminal
.prototype.tabSet = function() {
3211 this.tabs
[this.x
] = true;
3212 this.state
= normal
;
3222 * Cursor Up Ps Times (default = 1) (CUU).
3224 Terminal
.prototype.cursorUp = function(params
) {
3225 var param
= params
[0];
3226 if (param
< 1) param
= 1;
3228 if (this.y
< 0) this.y
= 0;
3234 * Cursor Down Ps Times (default = 1) (CUD).
3236 Terminal
.prototype.cursorDown = function(params
) {
3237 var param
= params
[0];
3238 if (param
< 1) param
= 1;
3240 if (this.y
>= this.rows
) {
3241 this.y
= this.rows
- 1;
3248 * Cursor Forward Ps Times (default = 1) (CUF).
3250 Terminal
.prototype.cursorForward = function(params
) {
3251 var param
= params
[0];
3252 if (param
< 1) param
= 1;
3254 if (this.x
>= this.cols
) {
3255 this.x
= this.cols
- 1;
3262 * Cursor Backward Ps Times (default = 1) (CUB).
3264 Terminal
.prototype.cursorBackward = function(params
) {
3265 var param
= params
[0];
3266 if (param
< 1) param
= 1;
3268 if (this.x
< 0) this.x
= 0;
3274 * Cursor Position [row;column] (default = [1,1]) (CUP).
3276 Terminal
.prototype.cursorPos = function(params
) {
3279 row
= params
[0] - 1;
3281 if (params
.length
>= 2) {
3282 col
= params
[1] - 1;
3289 } else if (row
>= this.rows
) {
3290 row
= this.rows
- 1;
3295 } else if (col
>= this.cols
) {
3296 col
= this.cols
- 1;
3305 * CSI Ps J Erase in Display (ED).
3306 * Ps = 0 -> Erase Below (default).
3307 * Ps = 1 -> Erase Above.
3308 * Ps = 2 -> Erase All.
3309 * Ps = 3 -> Erase Saved Lines (xterm).
3311 * Erase in Display (DECSED).
3312 * Ps = 0 -> Selective Erase Below (default).
3313 * Ps = 1 -> Selective Erase Above.
3314 * Ps = 2 -> Selective Erase All.
3316 Terminal
.prototype.eraseInDisplay = function(params
) {
3318 switch (params
[0]) {
3320 this.eraseRight(this.x
, this.y
);
3322 for (; j
< this.rows
; j
++) {
3327 this.eraseLeft(this.x
, this.y
);
3335 while (j
--) this.eraseLine(j
);
3345 * CSI Ps K Erase in Line (EL).
3346 * Ps = 0 -> Erase to Right (default).
3347 * Ps = 1 -> Erase to Left.
3348 * Ps = 2 -> Erase All.
3350 * Erase in Line (DECSEL).
3351 * Ps = 0 -> Selective Erase to Right (default).
3352 * Ps = 1 -> Selective Erase to Left.
3353 * Ps = 2 -> Selective Erase All.
3355 Terminal
.prototype.eraseInLine = function(params
) {
3356 switch (params
[0]) {
3358 this.eraseRight(this.x
, this.y
);
3361 this.eraseLeft(this.x
, this.y
);
3364 this.eraseLine(this.y
);
3371 * CSI Pm m Character Attributes (SGR).
3372 * Ps = 0 -> Normal (default).
3374 * Ps = 4 -> Underlined.
3375 * Ps = 5 -> Blink (appears as Bold).
3376 * Ps = 7 -> Inverse.
3377 * Ps = 8 -> Invisible, i.e., hidden (VT300).
3378 * Ps = 2 2 -> Normal (neither bold nor faint).
3379 * Ps = 2 4 -> Not underlined.
3380 * Ps = 2 5 -> Steady (not blinking).
3381 * Ps = 2 7 -> Positive (not inverse).
3382 * Ps = 2 8 -> Visible, i.e., not hidden (VT300).
3383 * Ps = 3 0 -> Set foreground color to Black.
3384 * Ps = 3 1 -> Set foreground color to Red.
3385 * Ps = 3 2 -> Set foreground color to Green.
3386 * Ps = 3 3 -> Set foreground color to Yellow.
3387 * Ps = 3 4 -> Set foreground color to Blue.
3388 * Ps = 3 5 -> Set foreground color to Magenta.
3389 * Ps = 3 6 -> Set foreground color to Cyan.
3390 * Ps = 3 7 -> Set foreground color to White.
3391 * Ps = 3 9 -> Set foreground color to default (original).
3392 * Ps = 4 0 -> Set background color to Black.
3393 * Ps = 4 1 -> Set background color to Red.
3394 * Ps = 4 2 -> Set background color to Green.
3395 * Ps = 4 3 -> Set background color to Yellow.
3396 * Ps = 4 4 -> Set background color to Blue.
3397 * Ps = 4 5 -> Set background color to Magenta.
3398 * Ps = 4 6 -> Set background color to Cyan.
3399 * Ps = 4 7 -> Set background color to White.
3400 * Ps = 4 9 -> Set background color to default (original).
3402 * If 16-color support is compiled, the following apply. Assume
3403 * that xterm's resources are set so that the ISO color codes are
3404 * the first 8 of a set of 16. Then the aixterm colors are the
3405 * bright versions of the ISO colors:
3406 * Ps = 9 0 -> Set foreground color to Black.
3407 * Ps = 9 1 -> Set foreground color to Red.
3408 * Ps = 9 2 -> Set foreground color to Green.
3409 * Ps = 9 3 -> Set foreground color to Yellow.
3410 * Ps = 9 4 -> Set foreground color to Blue.
3411 * Ps = 9 5 -> Set foreground color to Magenta.
3412 * Ps = 9 6 -> Set foreground color to Cyan.
3413 * Ps = 9 7 -> Set foreground color to White.
3414 * Ps = 1 0 0 -> Set background color to Black.
3415 * Ps = 1 0 1 -> Set background color to Red.
3416 * Ps = 1 0 2 -> Set background color to Green.
3417 * Ps = 1 0 3 -> Set background color to Yellow.
3418 * Ps = 1 0 4 -> Set background color to Blue.
3419 * Ps = 1 0 5 -> Set background color to Magenta.
3420 * Ps = 1 0 6 -> Set background color to Cyan.
3421 * Ps = 1 0 7 -> Set background color to White.
3423 * If xterm is compiled with the 16-color support disabled, it
3424 * supports the following, from rxvt:
3425 * Ps = 1 0 0 -> Set foreground and background color to
3428 * If 88- or 256-color support is compiled, the following apply.
3429 * Ps = 3 8 ; 5 ; Ps -> Set foreground color to the second
3431 * Ps = 4 8 ; 5 ; Ps -> Set background color to the second
3434 Terminal
.prototype.charAttributes = function(params
) {
3435 // Optimize a single SGR0.
3436 if (params
.length
=== 1 && params
[0] === 0) {
3437 this.curAttr
= this.defAttr
;
3441 var l
= params
.length
3443 , flags
= this.curAttr
>> 18
3444 , fg
= (this.curAttr
>> 9) & 0x1ff
3445 , bg
= this.curAttr
& 0x1ff
3448 for (; i
< l
; i
++) {
3450 if (p
>= 30 && p
<= 37) {
3453 } else if (p
>= 40 && p
<= 47) {
3456 } else if (p
>= 90 && p
<= 97) {
3460 } else if (p
>= 100 && p
<= 107) {
3464 } else if (p
=== 0) {
3466 flags
= this.defAttr
>> 18;
3467 fg
= (this.defAttr
>> 9) & 0x1ff;
3468 bg
= this.defAttr
& 0x1ff;
3472 } else if (p
=== 1) {
3475 } else if (p
=== 4) {
3478 } else if (p
=== 5) {
3481 } else if (p
=== 7) {
3482 // inverse and positive
3483 // test with: echo -e '\e[31m\e[42mhello\e[7mworld\e[27mhi\e[m'
3485 } else if (p
=== 8) {
3488 } else if (p
=== 22) {
3491 } else if (p
=== 24) {
3494 } else if (p
=== 25) {
3497 } else if (p
=== 27) {
3500 } else if (p
=== 28) {
3503 } else if (p
=== 39) {
3505 fg
= (this.defAttr
>> 9) & 0x1ff;
3506 } else if (p
=== 49) {
3508 bg
= this.defAttr
& 0x1ff;
3509 } else if (p
=== 38) {
3511 if (params
[i
+ 1] === 2) {
3515 params
[i
+ 1] & 0xff,
3516 params
[i
+ 2] & 0xff);
3517 if (fg
=== -1) fg
= 0x1ff;
3519 } else if (params
[i
+ 1] === 5) {
3521 p
= params
[i
] & 0xff;
3524 } else if (p
=== 48) {
3526 if (params
[i
+ 1] === 2) {
3530 params
[i
+ 1] & 0xff,
3531 params
[i
+ 2] & 0xff);
3532 if (bg
=== -1) bg
= 0x1ff;
3534 } else if (params
[i
+ 1] === 5) {
3536 p
= params
[i
] & 0xff;
3539 } else if (p
=== 100) {
3541 fg
= (this.defAttr
>> 9) & 0x1ff;
3542 bg
= this.defAttr
& 0x1ff;
3544 this.error('Unknown SGR attribute: %d.', p
);
3548 this.curAttr
= (flags
<< 18) | (fg
<< 9) | bg
;
3553 * CSI Ps n Device Status Report (DSR).
3554 * Ps = 5 -> Status Report. Result (``OK'') is
3556 * Ps = 6 -> Report Cursor Position (CPR) [row;column].
3560 * Device Status Report (DSR, DEC-specific).
3561 * Ps = 6 -> Report Cursor Position (CPR) [row;column] as CSI
3562 * ? r ; c R (assumes page is zero).
3563 * Ps = 1 5 -> Report Printer status as CSI ? 1 0 n (ready).
3564 * or CSI ? 1 1 n (not ready).
3565 * Ps = 2 5 -> Report UDK status as CSI ? 2 0 n (unlocked)
3566 * or CSI ? 2 1 n (locked).
3567 * Ps = 2 6 -> Report Keyboard status as
3568 * CSI ? 2 7 ; 1 ; 0 ; 0 n (North American).
3569 * The last two parameters apply to VT400 & up, and denote key-
3570 * board ready and LK01 respectively.
3571 * Ps = 5 3 -> Report Locator status as
3572 * CSI ? 5 3 n Locator available, if compiled-in, or
3573 * CSI ? 5 0 n No Locator, if not.
3575 Terminal
.prototype.deviceStatus = function(params
) {
3577 switch (params
[0]) {
3580 this.send('\x1b[0n');
3591 } else if (this.prefix
=== '?') {
3592 // modern xterm doesnt seem to
3593 // respond to any of these except ?6, 6, and 5
3594 switch (params
[0]) {
3605 // this.send('\x1b[?11n');
3608 // dont support user defined keys
3609 // this.send('\x1b[?21n');
3612 // north american keyboard
3613 // this.send('\x1b[?27;1;0;0n');
3616 // no dec locator/mouse
3617 // this.send('\x1b[?50n');
3630 * Insert Ps (Blank) Character(s) (default = 1) (ICH).
3632 Terminal
.prototype.insertChars = function(params
) {
3633 var param
, row
, j
, ch
;
3636 if (param
< 1) param
= 1;
3638 row
= this.y
+ this.ybase
;
3640 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3642 while (param
-- && j
< this.cols
) {
3643 this.lines
.get(row
).splice(j
++, 0, ch
);
3644 this.lines
.get(row
).pop();
3650 * Cursor Next Line Ps Times (default = 1) (CNL).
3651 * same as CSI Ps B ?
3653 Terminal
.prototype.cursorNextLine = function(params
) {
3654 var param
= params
[0];
3655 if (param
< 1) param
= 1;
3657 if (this.y
>= this.rows
) {
3658 this.y
= this.rows
- 1;
3666 * Cursor Preceding Line Ps Times (default = 1) (CNL).
3669 Terminal
.prototype.cursorPrecedingLine = function(params
) {
3670 var param
= params
[0];
3671 if (param
< 1) param
= 1;
3673 if (this.y
< 0) this.y
= 0;
3680 * Cursor Character Absolute [column] (default = [row,1]) (CHA).
3682 Terminal
.prototype.cursorCharAbsolute = function(params
) {
3683 var param
= params
[0];
3684 if (param
< 1) param
= 1;
3691 * Insert Ps Line(s) (default = 1) (IL).
3693 Terminal
.prototype.insertLines = function(params
) {
3697 if (param
< 1) param
= 1;
3698 row
= this.y
+ this.ybase
;
3700 j
= this.rows
- 1 - this.scrollBottom
;
3701 j
= this.rows
- 1 + this.ybase
- j
+ 1;
3704 // test: echo -e '\e[44m\e[1L\e[0m'
3705 // blankLine(true) - xterm/linux behavior
3706 this.lines
.splice(row
, 0, this.blankLine(true));
3707 this.lines
.splice(j
, 1);
3711 this.updateRange(this.y
);
3712 this.updateRange(this.scrollBottom
);
3718 * Delete Ps Line(s) (default = 1) (DL).
3720 Terminal
.prototype.deleteLines = function(params
) {
3724 if (param
< 1) param
= 1;
3725 row
= this.y
+ this.ybase
;
3727 j
= this.rows
- 1 - this.scrollBottom
;
3728 j
= this.rows
- 1 + this.ybase
- j
;
3731 // test: echo -e '\e[44m\e[1M\e[0m'
3732 // blankLine(true) - xterm/linux behavior
3733 this.lines
.splice(j
+ 1, 0, this.blankLine(true));
3734 this.lines
.splice(row
, 1);
3738 this.updateRange(this.y
);
3739 this.updateRange(this.scrollBottom
);
3745 * Delete Ps Character(s) (default = 1) (DCH).
3747 Terminal
.prototype.deleteChars = function(params
) {
3751 if (param
< 1) param
= 1;
3753 row
= this.y
+ this.ybase
;
3754 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3757 this.lines
[row
].splice(this.x
, 1);
3758 this.lines
[row
].push(ch
);
3764 * Erase Ps Character(s) (default = 1) (ECH).
3766 Terminal
.prototype.eraseChars = function(params
) {
3767 var param
, row
, j
, ch
;
3770 if (param
< 1) param
= 1;
3772 row
= this.y
+ this.ybase
;
3774 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3776 while (param
-- && j
< this.cols
) {
3777 this.lines
.get(row
)[j
++] = ch
;
3782 * CSI Pm ` Character Position Absolute
3783 * [column] (default = [row,1]) (HPA).
3785 Terminal
.prototype.charPosAbsolute = function(params
) {
3786 var param
= params
[0];
3787 if (param
< 1) param
= 1;
3789 if (this.x
>= this.cols
) {
3790 this.x
= this.cols
- 1;
3797 * Horizontal Position Relative
3800 Terminal
.prototype.HPositionRelative = function(params
) {
3801 var param
= params
[0];
3802 if (param
< 1) param
= 1;
3804 if (this.x
>= this.cols
) {
3805 this.x
= this.cols
- 1;
3811 * CSI Ps c Send Device Attributes (Primary DA).
3812 * Ps = 0 or omitted -> request attributes from terminal. The
3813 * response depends on the decTerminalID resource setting.
3814 * -> CSI ? 1 ; 2 c (``VT100 with Advanced Video Option'')
3815 * -> CSI ? 1 ; 0 c (``VT101 with No Options'')
3816 * -> CSI ? 6 c (``VT102'')
3817 * -> CSI ? 6 0 ; 1 ; 2 ; 6 ; 8 ; 9 ; 1 5 ; c (``VT220'')
3818 * The VT100-style response parameters do not mean anything by
3819 * themselves. VT220 parameters do, telling the host what fea-
3820 * tures the terminal supports:
3821 * Ps = 1 -> 132-columns.
3822 * Ps = 2 -> Printer.
3823 * Ps = 6 -> Selective erase.
3824 * Ps = 8 -> User-defined keys.
3825 * Ps = 9 -> National replacement character sets.
3826 * Ps = 1 5 -> Technical characters.
3827 * Ps = 2 2 -> ANSI color, e.g., VT525.
3828 * Ps = 2 9 -> ANSI text locator (i.e., DEC Locator mode).
3830 * Send Device Attributes (Secondary DA).
3831 * Ps = 0 or omitted -> request the terminal's identification
3832 * code. The response depends on the decTerminalID resource set-
3833 * ting. It should apply only to VT220 and up, but xterm extends
3835 * -> CSI > Pp ; Pv ; Pc c
3836 * where Pp denotes the terminal type
3837 * Pp = 0 -> ``VT100''.
3838 * Pp = 1 -> ``VT220''.
3839 * and Pv is the firmware version (for xterm, this was originally
3840 * the XFree86 patch number, starting with 95). In a DEC termi-
3841 * nal, Pc indicates the ROM cartridge registration number and is
3844 * xterm/charproc.c - line 2012, for more information.
3845 * vim responds with ^[[?0c or ^[[?1c after the terminal's response (?)
3847 Terminal
.prototype.sendDeviceAttributes = function(params
) {
3848 if (params
[0] > 0) return;
3851 if (this.is('xterm')
3852 || this.is('rxvt-unicode')
3853 || this.is('screen')) {
3854 this.send('\x1b[?1;2c');
3855 } else if (this.is('linux')) {
3856 this.send('\x1b[?6c');
3858 } else if (this.prefix
=== '>') {
3860 // seem to spit this
3861 // out around ~370 times (?).
3862 if (this.is('xterm')) {
3863 this.send('\x1b[>0;276;0c');
3864 } else if (this.is('rxvt-unicode')) {
3865 this.send('\x1b[>85;95;0c');
3866 } else if (this.is('linux')) {
3867 // not supported by linux console.
3868 // linux console echoes parameters.
3869 this.send(params
[0] + 'c');
3870 } else if (this.is('screen')) {
3871 this.send('\x1b[>83;40003;0c');
3879 * Line Position Absolute [row] (default = [1,column]) (VPA).
3881 Terminal
.prototype.linePosAbsolute = function(params
) {
3882 var param
= params
[0];
3883 if (param
< 1) param
= 1;
3885 if (this.y
>= this.rows
) {
3886 this.y
= this.rows
- 1;
3892 * 145 65 e * VPR - Vertical Position Relative
3895 Terminal
.prototype.VPositionRelative = function(params
) {
3896 var param
= params
[0];
3897 if (param
< 1) param
= 1;
3899 if (this.y
>= this.rows
) {
3900 this.y
= this.rows
- 1;
3907 * Horizontal and Vertical Position [row;column] (default =
3910 Terminal
.prototype.HVPosition = function(params
) {
3911 if (params
[0] < 1) params
[0] = 1;
3912 if (params
[1] < 1) params
[1] = 1;
3914 this.y
= params
[0] - 1;
3915 if (this.y
>= this.rows
) {
3916 this.y
= this.rows
- 1;
3919 this.x
= params
[1] - 1;
3920 if (this.x
>= this.cols
) {
3921 this.x
= this.cols
- 1;
3927 * CSI Pm h Set Mode (SM).
3928 * Ps = 2 -> Keyboard Action Mode (AM).
3929 * Ps = 4 -> Insert Mode (IRM).
3930 * Ps = 1 2 -> Send/receive (SRM).
3931 * Ps = 2 0 -> Automatic Newline (LNM).
3933 * DEC Private Mode Set (DECSET).
3934 * Ps = 1 -> Application Cursor Keys (DECCKM).
3935 * Ps = 2 -> Designate USASCII for character sets G0-G3
3936 * (DECANM), and set VT100 mode.
3937 * Ps = 3 -> 132 Column Mode (DECCOLM).
3938 * Ps = 4 -> Smooth (Slow) Scroll (DECSCLM).
3939 * Ps = 5 -> Reverse Video (DECSCNM).
3940 * Ps = 6 -> Origin Mode (DECOM).
3941 * Ps = 7 -> Wraparound Mode (DECAWM).
3942 * Ps = 8 -> Auto-repeat Keys (DECARM).
3943 * Ps = 9 -> Send Mouse X & Y on button press. See the sec-
3944 * tion Mouse Tracking.
3945 * Ps = 1 0 -> Show toolbar (rxvt).
3946 * Ps = 1 2 -> Start Blinking Cursor (att610).
3947 * Ps = 1 8 -> Print form feed (DECPFF).
3948 * Ps = 1 9 -> Set print extent to full screen (DECPEX).
3949 * Ps = 2 5 -> Show Cursor (DECTCEM).
3950 * Ps = 3 0 -> Show scrollbar (rxvt).
3951 * Ps = 3 5 -> Enable font-shifting functions (rxvt).
3952 * Ps = 3 8 -> Enter Tektronix Mode (DECTEK).
3953 * Ps = 4 0 -> Allow 80 -> 132 Mode.
3954 * Ps = 4 1 -> more(1) fix (see curses resource).
3955 * Ps = 4 2 -> Enable Nation Replacement Character sets (DECN-
3957 * Ps = 4 4 -> Turn On Margin Bell.
3958 * Ps = 4 5 -> Reverse-wraparound Mode.
3959 * Ps = 4 6 -> Start Logging. This is normally disabled by a
3960 * compile-time option.
3961 * Ps = 4 7 -> Use Alternate Screen Buffer. (This may be dis-
3962 * abled by the titeInhibit resource).
3963 * Ps = 6 6 -> Application keypad (DECNKM).
3964 * Ps = 6 7 -> Backarrow key sends backspace (DECBKM).
3965 * Ps = 1 0 0 0 -> Send Mouse X & Y on button press and
3966 * release. See the section Mouse Tracking.
3967 * Ps = 1 0 0 1 -> Use Hilite Mouse Tracking.
3968 * Ps = 1 0 0 2 -> Use Cell Motion Mouse Tracking.
3969 * Ps = 1 0 0 3 -> Use All Motion Mouse Tracking.
3970 * Ps = 1 0 0 4 -> Send FocusIn/FocusOut events.
3971 * Ps = 1 0 0 5 -> Enable Extended Mouse Mode.
3972 * Ps = 1 0 1 0 -> Scroll to bottom on tty output (rxvt).
3973 * Ps = 1 0 1 1 -> Scroll to bottom on key press (rxvt).
3974 * Ps = 1 0 3 4 -> Interpret "meta" key, sets eighth bit.
3975 * (enables the eightBitInput resource).
3976 * Ps = 1 0 3 5 -> Enable special modifiers for Alt and Num-
3977 * Lock keys. (This enables the numLock resource).
3978 * Ps = 1 0 3 6 -> Send ESC when Meta modifies a key. (This
3979 * enables the metaSendsEscape resource).
3980 * Ps = 1 0 3 7 -> Send DEL from the editing-keypad Delete
3982 * Ps = 1 0 3 9 -> Send ESC when Alt modifies a key. (This
3983 * enables the altSendsEscape resource).
3984 * Ps = 1 0 4 0 -> Keep selection even if not highlighted.
3985 * (This enables the keepSelection resource).
3986 * Ps = 1 0 4 1 -> Use the CLIPBOARD selection. (This enables
3987 * the selectToClipboard resource).
3988 * Ps = 1 0 4 2 -> Enable Urgency window manager hint when
3989 * Control-G is received. (This enables the bellIsUrgent
3991 * Ps = 1 0 4 3 -> Enable raising of the window when Control-G
3992 * is received. (enables the popOnBell resource).
3993 * Ps = 1 0 4 7 -> Use Alternate Screen Buffer. (This may be
3994 * disabled by the titeInhibit resource).
3995 * Ps = 1 0 4 8 -> Save cursor as in DECSC. (This may be dis-
3996 * abled by the titeInhibit resource).
3997 * Ps = 1 0 4 9 -> Save cursor as in DECSC and use Alternate
3998 * Screen Buffer, clearing it first. (This may be disabled by
3999 * the titeInhibit resource). This combines the effects of the 1
4000 * 0 4 7 and 1 0 4 8 modes. Use this with terminfo-based
4001 * applications rather than the 4 7 mode.
4002 * Ps = 1 0 5 0 -> Set terminfo/termcap function-key mode.
4003 * Ps = 1 0 5 1 -> Set Sun function-key mode.
4004 * Ps = 1 0 5 2 -> Set HP function-key mode.
4005 * Ps = 1 0 5 3 -> Set SCO function-key mode.
4006 * Ps = 1 0 6 0 -> Set legacy keyboard emulation (X11R6).
4007 * Ps = 1 0 6 1 -> Set VT220 keyboard emulation.
4008 * Ps = 2 0 0 4 -> Set bracketed paste mode.
4010 * http: *vt100.net/docs/vt220-rm/chapter4.html
4012 Terminal
.prototype.setMode = function(params
) {
4013 if (typeof params
=== 'object') {
4014 var l
= params
.length
4017 for (; i
< l
; i
++) {
4018 this.setMode(params
[i
]);
4027 this.insertMode
= true;
4030 //this.convertEol = true;
4033 } else if (this.prefix
=== '?') {
4036 this.applicationCursor
= true;
4039 this.setgCharset(0, Terminal
.charsets
.US
);
4040 this.setgCharset(1, Terminal
.charsets
.US
);
4041 this.setgCharset(2, Terminal
.charsets
.US
);
4042 this.setgCharset(3, Terminal
.charsets
.US
);
4043 // set VT100 mode here
4045 case 3: // 132 col mode
4046 this.savedCols
= this.cols
;
4047 this.resize(132, this.rows
);
4050 this.originMode
= true;
4053 this.wraparoundMode
= true;
4056 // this.cursorBlink = true;
4059 this.log('Serial port requested application keypad.');
4060 this.applicationKeypad
= true;
4061 this.viewport
.syncScrollArea();
4063 case 9: // X10 Mouse
4064 // no release, no motion, no wheel, no modifiers.
4065 case 1000: // vt200 mouse
4067 // no modifiers, except control on the wheel.
4068 case 1002: // button event mouse
4069 case 1003: // any event mouse
4070 // any event - sends motion events,
4071 // even if there is no button held down.
4072 this.x10Mouse
= params
=== 9;
4073 this.vt200Mouse
= params
=== 1000;
4074 this.normalMouse
= params
> 1000;
4075 this.mouseEvents
= true;
4076 this.element
.style
.cursor
= 'default';
4077 this.log('Binding to mouse events.');
4079 case 1004: // send focusin/focusout events
4082 this.sendFocus
= true;
4084 case 1005: // utf8 ext mode mouse
4085 this.utfMouse
= true;
4086 // for wide terminals
4087 // simply encodes large values as utf8 characters
4089 case 1006: // sgr ext mode mouse
4090 this.sgrMouse
= true;
4091 // for wide terminals
4092 // does not add 32 to fields
4093 // press: ^[[<b;x;yM
4094 // release: ^[[<b;x;ym
4096 case 1015: // urxvt ext mode mouse
4097 this.urxvtMouse
= true;
4098 // for wide terminals
4099 // numbers for fields
4101 // motion: ^[[b;x;yT
4103 case 25: // show cursor
4104 this.cursorHidden
= false;
4106 case 1049: // alt screen buffer cursor
4107 //this.saveCursor();
4109 case 47: // alt screen buffer
4110 case 1047: // alt screen buffer
4118 scrollTop
: this.scrollTop
,
4119 scrollBottom
: this.scrollBottom
,
4121 // XXX save charset(s) here?
4122 // charset: this.charset,
4123 // glevel: this.glevel,
4124 // charsets: this.charsets
4127 this.normal
= normal
;
4136 * CSI Pm l Reset Mode (RM).
4137 * Ps = 2 -> Keyboard Action Mode (AM).
4138 * Ps = 4 -> Replace Mode (IRM).
4139 * Ps = 1 2 -> Send/receive (SRM).
4140 * Ps = 2 0 -> Normal Linefeed (LNM).
4142 * DEC Private Mode Reset (DECRST).
4143 * Ps = 1 -> Normal Cursor Keys (DECCKM).
4144 * Ps = 2 -> Designate VT52 mode (DECANM).
4145 * Ps = 3 -> 80 Column Mode (DECCOLM).
4146 * Ps = 4 -> Jump (Fast) Scroll (DECSCLM).
4147 * Ps = 5 -> Normal Video (DECSCNM).
4148 * Ps = 6 -> Normal Cursor Mode (DECOM).
4149 * Ps = 7 -> No Wraparound Mode (DECAWM).
4150 * Ps = 8 -> No Auto-repeat Keys (DECARM).
4151 * Ps = 9 -> Don't send Mouse X & Y on button press.
4152 * Ps = 1 0 -> Hide toolbar (rxvt).
4153 * Ps = 1 2 -> Stop Blinking Cursor (att610).
4154 * Ps = 1 8 -> Don't print form feed (DECPFF).
4155 * Ps = 1 9 -> Limit print to scrolling region (DECPEX).
4156 * Ps = 2 5 -> Hide Cursor (DECTCEM).
4157 * Ps = 3 0 -> Don't show scrollbar (rxvt).
4158 * Ps = 3 5 -> Disable font-shifting functions (rxvt).
4159 * Ps = 4 0 -> Disallow 80 -> 132 Mode.
4160 * Ps = 4 1 -> No more(1) fix (see curses resource).
4161 * Ps = 4 2 -> Disable Nation Replacement Character sets (DEC-
4163 * Ps = 4 4 -> Turn Off Margin Bell.
4164 * Ps = 4 5 -> No Reverse-wraparound Mode.
4165 * Ps = 4 6 -> Stop Logging. (This is normally disabled by a
4166 * compile-time option).
4167 * Ps = 4 7 -> Use Normal Screen Buffer.
4168 * Ps = 6 6 -> Numeric keypad (DECNKM).
4169 * Ps = 6 7 -> Backarrow key sends delete (DECBKM).
4170 * Ps = 1 0 0 0 -> Don't send Mouse X & Y on button press and
4171 * release. See the section Mouse Tracking.
4172 * Ps = 1 0 0 1 -> Don't use Hilite Mouse Tracking.
4173 * Ps = 1 0 0 2 -> Don't use Cell Motion Mouse Tracking.
4174 * Ps = 1 0 0 3 -> Don't use All Motion Mouse Tracking.
4175 * Ps = 1 0 0 4 -> Don't send FocusIn/FocusOut events.
4176 * Ps = 1 0 0 5 -> Disable Extended Mouse Mode.
4177 * Ps = 1 0 1 0 -> Don't scroll to bottom on tty output
4179 * Ps = 1 0 1 1 -> Don't scroll to bottom on key press (rxvt).
4180 * Ps = 1 0 3 4 -> Don't interpret "meta" key. (This disables
4181 * the eightBitInput resource).
4182 * Ps = 1 0 3 5 -> Disable special modifiers for Alt and Num-
4183 * Lock keys. (This disables the numLock resource).
4184 * Ps = 1 0 3 6 -> Don't send ESC when Meta modifies a key.
4185 * (This disables the metaSendsEscape resource).
4186 * Ps = 1 0 3 7 -> Send VT220 Remove from the editing-keypad
4188 * Ps = 1 0 3 9 -> Don't send ESC when Alt modifies a key.
4189 * (This disables the altSendsEscape resource).
4190 * Ps = 1 0 4 0 -> Do not keep selection when not highlighted.
4191 * (This disables the keepSelection resource).
4192 * Ps = 1 0 4 1 -> Use the PRIMARY selection. (This disables
4193 * the selectToClipboard resource).
4194 * Ps = 1 0 4 2 -> Disable Urgency window manager hint when
4195 * Control-G is received. (This disables the bellIsUrgent
4197 * Ps = 1 0 4 3 -> Disable raising of the window when Control-
4198 * G is received. (This disables the popOnBell resource).
4199 * Ps = 1 0 4 7 -> Use Normal Screen Buffer, clearing screen
4200 * first if in the Alternate Screen. (This may be disabled by
4201 * the titeInhibit resource).
4202 * Ps = 1 0 4 8 -> Restore cursor as in DECRC. (This may be
4203 * disabled by the titeInhibit resource).
4204 * Ps = 1 0 4 9 -> Use Normal Screen Buffer and restore cursor
4205 * as in DECRC. (This may be disabled by the titeInhibit
4206 * resource). This combines the effects of the 1 0 4 7 and 1 0
4207 * 4 8 modes. Use this with terminfo-based applications rather
4208 * than the 4 7 mode.
4209 * Ps = 1 0 5 0 -> Reset terminfo/termcap function-key mode.
4210 * Ps = 1 0 5 1 -> Reset Sun function-key mode.
4211 * Ps = 1 0 5 2 -> Reset HP function-key mode.
4212 * Ps = 1 0 5 3 -> Reset SCO function-key mode.
4213 * Ps = 1 0 6 0 -> Reset legacy keyboard emulation (X11R6).
4214 * Ps = 1 0 6 1 -> Reset keyboard emulation to Sun/PC style.
4215 * Ps = 2 0 0 4 -> Reset bracketed paste mode.
4217 Terminal
.prototype.resetMode = function(params
) {
4218 if (typeof params
=== 'object') {
4219 var l
= params
.length
4222 for (; i
< l
; i
++) {
4223 this.resetMode(params
[i
]);
4232 this.insertMode
= false;
4235 //this.convertEol = false;
4238 } else if (this.prefix
=== '?') {
4241 this.applicationCursor
= false;
4244 if (this.cols
=== 132 && this.savedCols
) {
4245 this.resize(this.savedCols
, this.rows
);
4247 delete this.savedCols
;
4250 this.originMode
= false;
4253 this.wraparoundMode
= false;
4256 // this.cursorBlink = false;
4259 this.log('Switching back to normal keypad.');
4260 this.applicationKeypad
= false;
4261 this.viewport
.syncScrollArea();
4263 case 9: // X10 Mouse
4264 case 1000: // vt200 mouse
4265 case 1002: // button event mouse
4266 case 1003: // any event mouse
4267 this.x10Mouse
= false;
4268 this.vt200Mouse
= false;
4269 this.normalMouse
= false;
4270 this.mouseEvents
= false;
4271 this.element
.style
.cursor
= '';
4273 case 1004: // send focusin/focusout events
4274 this.sendFocus
= false;
4276 case 1005: // utf8 ext mode mouse
4277 this.utfMouse
= false;
4279 case 1006: // sgr ext mode mouse
4280 this.sgrMouse
= false;
4282 case 1015: // urxvt ext mode mouse
4283 this.urxvtMouse
= false;
4285 case 25: // hide cursor
4286 this.cursorHidden
= true;
4288 case 1049: // alt screen buffer cursor
4290 case 47: // normal screen buffer
4291 case 1047: // normal screen buffer - clearing it first
4293 this.lines
= this.normal
.lines
;
4294 this.ybase
= this.normal
.ybase
;
4295 this.ydisp
= this.normal
.ydisp
;
4296 this.x
= this.normal
.x
;
4297 this.y
= this.normal
.y
;
4298 this.scrollTop
= this.normal
.scrollTop
;
4299 this.scrollBottom
= this.normal
.scrollBottom
;
4300 this.tabs
= this.normal
.tabs
;
4302 // if (params === 1049) {
4303 // this.x = this.savedX;
4304 // this.y = this.savedY;
4306 this.refresh(0, this.rows
- 1);
4317 * Set Scrolling Region [top;bottom] (default = full size of win-
4321 Terminal
.prototype.setScrollRegion = function(params
) {
4322 if (this.prefix
) return;
4323 this.scrollTop
= (params
[0] || 1) - 1;
4324 this.scrollBottom
= (params
[1] || this.rows
) - 1;
4332 * Save cursor (ANSI.SYS).
4334 Terminal
.prototype.saveCursor = function(params
) {
4335 this.savedX
= this.x
;
4336 this.savedY
= this.y
;
4342 * Restore cursor (ANSI.SYS).
4344 Terminal
.prototype.restoreCursor = function(params
) {
4345 this.x
= this.savedX
|| 0;
4346 this.y
= this.savedY
|| 0;
4356 * Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
4358 Terminal
.prototype.cursorForwardTab = function(params
) {
4359 var param
= params
[0] || 1;
4361 this.x
= this.nextStop();
4367 * CSI Ps S Scroll up Ps lines (default = 1) (SU).
4369 Terminal
.prototype.scrollUp = function(params
) {
4370 var param
= params
[0] || 1;
4372 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
4373 this.lines
.splice(this.ybase
+ this.scrollBottom
, 0, this.blankLine());
4376 this.updateRange(this.scrollTop
);
4377 this.updateRange(this.scrollBottom
);
4382 * CSI Ps T Scroll down Ps lines (default = 1) (SD).
4384 Terminal
.prototype.scrollDown = function(params
) {
4385 var param
= params
[0] || 1;
4387 this.lines
.splice(this.ybase
+ this.scrollBottom
, 1);
4388 this.lines
.splice(this.ybase
+ this.scrollTop
, 0, this.blankLine());
4391 this.updateRange(this.scrollTop
);
4392 this.updateRange(this.scrollBottom
);
4397 * CSI Ps ; Ps ; Ps ; Ps ; Ps T
4398 * Initiate highlight mouse tracking. Parameters are
4399 * [func;startx;starty;firstrow;lastrow]. See the section Mouse
4402 Terminal
.prototype.initMouseTracking = function(params
) {
4403 // Relevant: DECSET 1001
4409 * Reset one or more features of the title modes to the default
4410 * value. Normally, "reset" disables the feature. It is possi-
4411 * ble to disable the ability to reset features by compiling a
4412 * different default for the title modes into xterm.
4413 * Ps = 0 -> Do not set window/icon labels using hexadecimal.
4414 * Ps = 1 -> Do not query window/icon labels using hexadeci-
4416 * Ps = 2 -> Do not set window/icon labels using UTF-8.
4417 * Ps = 3 -> Do not query window/icon labels using UTF-8.
4418 * (See discussion of "Title Modes").
4420 Terminal
.prototype.resetTitleModes = function(params
) {
4426 * CSI Ps Z Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
4428 Terminal
.prototype.cursorBackwardTab = function(params
) {
4429 var param
= params
[0] || 1;
4431 this.x
= this.prevStop();
4437 * CSI Ps b Repeat the preceding graphic character Ps times (REP).
4439 Terminal
.prototype.repeatPrecedingCharacter = function(params
) {
4440 var param
= params
[0] || 1
4441 , line
= this.lines
.get(this.ybase
+ this.y
)
4442 , ch
= line
[this.x
- 1] || [this.defAttr
, ' ', 1];
4444 while (param
--) line
[this.x
++] = ch
;
4449 * CSI Ps g Tab Clear (TBC).
4450 * Ps = 0 -> Clear Current Column (default).
4451 * Ps = 3 -> Clear All.
4453 * Ps = 2 -> Clear Stops on Line.
4454 * http://vt100.net/annarbor/aaa-ug/section6.html
4456 Terminal
.prototype.tabClear = function(params
) {
4457 var param
= params
[0];
4459 delete this.tabs
[this.x
];
4460 } else if (param
=== 3) {
4467 * CSI Pm i Media Copy (MC).
4468 * Ps = 0 -> Print screen (default).
4469 * Ps = 4 -> Turn off printer controller mode.
4470 * Ps = 5 -> Turn on printer controller mode.
4472 * Media Copy (MC, DEC-specific).
4473 * Ps = 1 -> Print line containing cursor.
4474 * Ps = 4 -> Turn off autoprint mode.
4475 * Ps = 5 -> Turn on autoprint mode.
4476 * Ps = 1 0 -> Print composed display, ignores DECPEX.
4477 * Ps = 1 1 -> Print all pages.
4479 Terminal
.prototype.mediaCopy = function(params
) {
4486 * Set or reset resource-values used by xterm to decide whether
4487 * to construct escape sequences holding information about the
4488 * modifiers pressed with a given key. The first parameter iden-
4489 * tifies the resource to set/reset. The second parameter is the
4490 * value to assign to the resource. If the second parameter is
4491 * omitted, the resource is reset to its initial value.
4492 * Ps = 1 -> modifyCursorKeys.
4493 * Ps = 2 -> modifyFunctionKeys.
4494 * Ps = 4 -> modifyOtherKeys.
4495 * If no parameters are given, all resources are reset to their
4498 Terminal
.prototype.setResources = function(params
) {
4505 * Disable modifiers which may be enabled via the CSI > Ps; Ps m
4506 * sequence. This corresponds to a resource value of "-1", which
4507 * cannot be set with the other sequence. The parameter identi-
4508 * fies the resource to be disabled:
4509 * Ps = 1 -> modifyCursorKeys.
4510 * Ps = 2 -> modifyFunctionKeys.
4511 * Ps = 4 -> modifyOtherKeys.
4512 * If the parameter is omitted, modifyFunctionKeys is disabled.
4513 * When modifyFunctionKeys is disabled, xterm uses the modifier
4514 * keys to make an extended sequence of functions rather than
4515 * adding a parameter to each function key to denote the modi-
4518 Terminal
.prototype.disableModifiers = function(params
) {
4525 * Set resource value pointerMode. This is used by xterm to
4526 * decide whether to hide the pointer cursor as the user types.
4527 * Valid values for the parameter:
4528 * Ps = 0 -> never hide the pointer.
4529 * Ps = 1 -> hide if the mouse tracking mode is not enabled.
4530 * Ps = 2 -> always hide the pointer. If no parameter is
4531 * given, xterm uses the default, which is 1 .
4533 Terminal
.prototype.setPointerMode = function(params
) {
4539 * CSI ! p Soft terminal reset (DECSTR).
4540 * http://vt100.net/docs/vt220-rm/table4-10.html
4542 Terminal
.prototype.softReset = function(params
) {
4543 this.cursorHidden
= false;
4544 this.insertMode
= false;
4545 this.originMode
= false;
4546 this.wraparoundMode
= false; // autowrap
4547 this.applicationKeypad
= false; // ?
4548 this.viewport
.syncScrollArea();
4549 this.applicationCursor
= false;
4551 this.scrollBottom
= this.rows
- 1;
4552 this.curAttr
= this.defAttr
;
4553 this.x
= this.y
= 0; // ?
4554 this.charset
= null;
4555 this.glevel
= 0; // ??
4556 this.charsets
= [null]; // ??
4562 * Request ANSI mode (DECRQM). For VT300 and up, reply is
4564 * where Ps is the mode number as in RM, and Pm is the mode
4566 * 0 - not recognized
4569 * 3 - permanently set
4570 * 4 - permanently reset
4572 Terminal
.prototype.requestAnsiMode = function(params
) {
4579 * Request DEC private mode (DECRQM). For VT300 and up, reply is
4581 * where Ps is the mode number as in DECSET, Pm is the mode value
4582 * as in the ANSI DECRQM.
4584 Terminal
.prototype.requestPrivateMode = function(params
) {
4591 * Set conformance level (DECSCL). Valid values for the first
4593 * Ps = 6 1 -> VT100.
4594 * Ps = 6 2 -> VT200.
4595 * Ps = 6 3 -> VT300.
4596 * Valid values for the second parameter:
4597 * Ps = 0 -> 8-bit controls.
4598 * Ps = 1 -> 7-bit controls (always set for VT100).
4599 * Ps = 2 -> 8-bit controls.
4601 Terminal
.prototype.setConformanceLevel = function(params
) {
4607 * CSI Ps q Load LEDs (DECLL).
4608 * Ps = 0 -> Clear all LEDS (default).
4609 * Ps = 1 -> Light Num Lock.
4610 * Ps = 2 -> Light Caps Lock.
4611 * Ps = 3 -> Light Scroll Lock.
4612 * Ps = 2 1 -> Extinguish Num Lock.
4613 * Ps = 2 2 -> Extinguish Caps Lock.
4614 * Ps = 2 3 -> Extinguish Scroll Lock.
4616 Terminal
.prototype.loadLEDs = function(params
) {
4623 * Set cursor style (DECSCUSR, VT520).
4624 * Ps = 0 -> blinking block.
4625 * Ps = 1 -> blinking block (default).
4626 * Ps = 2 -> steady block.
4627 * Ps = 3 -> blinking underline.
4628 * Ps = 4 -> steady underline.
4630 Terminal
.prototype.setCursorStyle = function(params
) {
4637 * Select character protection attribute (DECSCA). Valid values
4638 * for the parameter:
4639 * Ps = 0 -> DECSED and DECSEL can erase (default).
4640 * Ps = 1 -> DECSED and DECSEL cannot erase.
4641 * Ps = 2 -> DECSED and DECSEL can erase.
4643 Terminal
.prototype.setCharProtectionAttr = function(params
) {
4650 * Restore DEC Private Mode Values. The value of Ps previously
4651 * saved is restored. Ps values are the same as for DECSET.
4653 Terminal
.prototype.restorePrivateValues = function(params
) {
4659 * CSI Pt; Pl; Pb; Pr; Ps$ r
4660 * Change Attributes in Rectangular Area (DECCARA), VT400 and up.
4661 * Pt; Pl; Pb; Pr denotes the rectangle.
4662 * Ps denotes the SGR attributes to change: 0, 1, 4, 5, 7.
4663 * NOTE: xterm doesn't enable this code by default.
4665 Terminal
.prototype.setAttrInRectangle = function(params
) {
4675 for (; t
< b
+ 1; t
++) {
4676 line
= this.lines
.get(this.ybase
+ t
);
4677 for (i
= l
; i
< r
; i
++) {
4678 line
[i
] = [attr
, line
[i
][1]];
4683 this.updateRange(params
[0]);
4684 this.updateRange(params
[2]);
4689 * CSI Pc; Pt; Pl; Pb; Pr$ x
4690 * Fill Rectangular Area (DECFRA), VT420 and up.
4691 * Pc is the character to use.
4692 * Pt; Pl; Pb; Pr denotes the rectangle.
4693 * NOTE: xterm doesn't enable this code by default.
4695 Terminal
.prototype.fillRectangle = function(params
) {
4705 for (; t
< b
+ 1; t
++) {
4706 line
= this.lines
.get(this.ybase
+ t
);
4707 for (i
= l
; i
< r
; i
++) {
4708 line
[i
] = [line
[i
][0], String
.fromCharCode(ch
)];
4713 this.updateRange(params
[1]);
4714 this.updateRange(params
[3]);
4720 * Enable Locator Reporting (DECELR).
4721 * Valid values for the first parameter:
4722 * Ps = 0 -> Locator disabled (default).
4723 * Ps = 1 -> Locator enabled.
4724 * Ps = 2 -> Locator enabled for one report, then disabled.
4725 * The second parameter specifies the coordinate unit for locator
4727 * Valid values for the second parameter:
4728 * Pu = 0 <- or omitted -> default to character cells.
4729 * Pu = 1 <- device physical pixels.
4730 * Pu = 2 <- character cells.
4732 Terminal
.prototype.enableLocatorReporting = function(params
) {
4733 var val
= params
[0] > 0;
4734 //this.mouseEvents = val;
4735 //this.decLocator = val;
4740 * CSI Pt; Pl; Pb; Pr$ z
4741 * Erase Rectangular Area (DECERA), VT400 and up.
4742 * Pt; Pl; Pb; Pr denotes the rectangle.
4743 * NOTE: xterm doesn't enable this code by default.
4745 Terminal
.prototype.eraseRectangle = function(params
) {
4755 ch
= [this.eraseAttr(), ' ', 1]; // xterm?
4757 for (; t
< b
+ 1; t
++) {
4758 line
= this.lines
.get(this.ybase
+ t
);
4759 for (i
= l
; i
< r
; i
++) {
4765 this.updateRange(params
[0]);
4766 this.updateRange(params
[2]);
4772 * Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
4773 * NOTE: xterm doesn't enable this code by default.
4775 Terminal
.prototype.insertColumns = function() {
4776 var param
= params
[0]
4777 , l
= this.ybase
+ this.rows
4778 , ch
= [this.eraseAttr(), ' ', 1] // xterm?
4782 for (i
= this.ybase
; i
< l
; i
++) {
4783 this.lines
[i
].splice(this.x
+ 1, 0, ch
);
4784 this.lines
[i
].pop();
4794 * Delete P s Column(s) (default = 1) (DECDC), VT420 and up
4795 * NOTE: xterm doesn't enable this code by default.
4797 Terminal
.prototype.deleteColumns = function() {
4798 var param
= params
[0]
4799 , l
= this.ybase
+ this.rows
4800 , ch
= [this.eraseAttr(), ' ', 1] // xterm?
4804 for (i
= this.ybase
; i
< l
; i
++) {
4805 this.lines
[i
].splice(this.x
, 1);
4806 this.lines
[i
].push(ch
);
4817 Terminal
.charsets
= {};
4819 // DEC Special Character and Line Drawing Set.
4820 // http://vt100.net/docs/vt102-ug/table5-13.html
4821 // A lot of curses apps use this if they see TERM=xterm.
4822 // testing: echo -e '\e(0a\e(B'
4823 // The xterm output sometimes seems to conflict with the
4824 // reference above. xterm seems in line with the reference
4825 // when running vttest however.
4826 // The table below now uses xterm's output from vttest.
4827 Terminal
.charsets
.SCLD
= { // (0
4828 '`': '\u25c6', // '◆'
4829 'a': '\u2592', // '▒'
4830 'b': '\u0009', // '\t'
4831 'c': '\u000c', // '\f'
4832 'd': '\u000d', // '\r'
4833 'e': '\u000a', // '\n'
4834 'f': '\u00b0', // '°'
4835 'g': '\u00b1', // '±'
4836 'h': '\u2424', // '\u2424' (NL)
4837 'i': '\u000b', // '\v'
4838 'j': '\u2518', // '┘'
4839 'k': '\u2510', // '┐'
4840 'l': '\u250c', // '┌'
4841 'm': '\u2514', // '└'
4842 'n': '\u253c', // '┼'
4843 'o': '\u23ba', // '⎺'
4844 'p': '\u23bb', // '⎻'
4845 'q': '\u2500', // '─'
4846 'r': '\u23bc', // '⎼'
4847 's': '\u23bd', // '⎽'
4848 't': '\u251c', // '├'
4849 'u': '\u2524', // '┤'
4850 'v': '\u2534', // '┴'
4851 'w': '\u252c', // '┬'
4852 'x': '\u2502', // '│'
4853 'y': '\u2264', // '≤'
4854 'z': '\u2265', // '≥'
4855 '{': '\u03c0', // 'π'
4856 '|': '\u2260', // '≠'
4857 '}': '\u00a3', // '£'
4858 '~': '\u00b7' // '·'
4861 Terminal
.charsets
.UK
= null; // (A
4862 Terminal
.charsets
.US
= null; // (B (USASCII)
4863 Terminal
.charsets
.Dutch
= null; // (4
4864 Terminal
.charsets
.Finnish
= null; // (C or (5
4865 Terminal
.charsets
.French
= null; // (R
4866 Terminal
.charsets
.FrenchCanadian
= null; // (Q
4867 Terminal
.charsets
.German
= null; // (K
4868 Terminal
.charsets
.Italian
= null; // (Y
4869 Terminal
.charsets
.NorwegianDanish
= null; // (E or (6
4870 Terminal
.charsets
.Spanish
= null; // (Z
4871 Terminal
.charsets
.Swedish
= null; // (H or (7
4872 Terminal
.charsets
.Swiss
= null; // (=
4873 Terminal
.charsets
.ISOLatin
= null; // /A
4879 function on(el
, type
, handler
, capture
) {
4880 if (!Array
.isArray(el
)) {
4883 el
.forEach(function (element
) {
4884 element
.addEventListener(type
, handler
, capture
|| false);
4888 function off(el
, type
, handler
, capture
) {
4889 el
.removeEventListener(type
, handler
, capture
|| false);
4892 function cancel(ev
, force
) {
4893 if (!this.cancelEvents
&& !force
) {
4896 ev
.preventDefault();
4897 ev
.stopPropagation();
4901 function inherits(child
, parent
) {
4903 this.constructor = child
;
4905 f
.prototype = parent
.prototype;
4906 child
.prototype = new f
;
4909 // if bold is broken, we can't
4910 // use it in the terminal.
4911 function isBoldBroken(document
) {
4912 var body
= document
.getElementsByTagName('body')[0];
4913 var el
= document
.createElement('span');
4914 el
.innerHTML
= 'hello world';
4915 body
.appendChild(el
);
4916 var w1
= el
.scrollWidth
;
4917 el
.style
.fontWeight
= 'bold';
4918 var w2
= el
.scrollWidth
;
4919 body
.removeChild(el
);
4923 function indexOf(obj
, el
) {
4926 if (obj
[i
] === el
) return i
;
4931 function isThirdLevelShift(term
, ev
) {
4933 (term
.browser
.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) ||
4934 (term
.browser
.isMSWindows
&& ev
.altKey
&& ev
.ctrlKey
&& !ev
.metaKey
);
4936 if (ev
.type
== 'keypress') {
4937 return thirdLevelKey
;
4940 // Don't invoke for arrows, pageDown, home, backspace, etc. (on non-keypress events)
4941 return thirdLevelKey
&& (!ev
.keyCode
|| ev
.keyCode
> 47);
4944 function matchColor(r1
, g1
, b1
) {
4945 var hash
= (r1
<< 16) | (g1
<< 8) | b1
;
4947 if (matchColor
._cache
[hash
] != null) {
4948 return matchColor
._cache
[hash
];
4951 var ldiff
= Infinity
4960 for (; i
< Terminal
.vcolors
.length
; i
++) {
4961 c
= Terminal
.vcolors
[i
];
4966 diff
= matchColor
.distance(r1
, g1
, b1
, r2
, g2
, b2
);
4979 return matchColor
._cache
[hash
] = li
;
4982 matchColor
._cache
= {};
4984 // http://stackoverflow.com/questions/1633828
4985 matchColor
.distance = function(r1
, g1
, b1
, r2
, g2
, b2
) {
4986 return Math
.pow(30 * (r1
- r2
), 2)
4987 + Math
.pow(59 * (g1
- g2
), 2)
4988 + Math
.pow(11 * (b1
- b2
), 2);
4991 function each(obj
, iter
, con
) {
4992 if (obj
.forEach
) return obj
.forEach(iter
, con
);
4993 for (var i
= 0; i
< obj
.length
; i
++) {
4994 iter
.call(con
, obj
[i
], i
, obj
);
4998 function keys(obj
) {
4999 if (Object
.keys
) return Object
.keys(obj
);
5002 if (Object
.prototype.hasOwnProperty
.call(obj
, key
)) {
5009 var wcwidth
= (function(opts
) {
5010 // extracted from https://www.cl.cam.ac.uk/%7Emgk25/ucs/wcwidth.c
5011 // combining characters
5013 [0x0300, 0x036F], [0x0483, 0x0486], [0x0488, 0x0489],
5014 [0x0591, 0x05BD], [0x05BF, 0x05BF], [0x05C1, 0x05C2],
5015 [0x05C4, 0x05C5], [0x05C7, 0x05C7], [0x0600, 0x0603],
5016 [0x0610, 0x0615], [0x064B, 0x065E], [0x0670, 0x0670],
5017 [0x06D6, 0x06E4], [0x06E7, 0x06E8], [0x06EA, 0x06ED],
5018 [0x070F, 0x070F], [0x0711, 0x0711], [0x0730, 0x074A],
5019 [0x07A6, 0x07B0], [0x07EB, 0x07F3], [0x0901, 0x0902],
5020 [0x093C, 0x093C], [0x0941, 0x0948], [0x094D, 0x094D],
5021 [0x0951, 0x0954], [0x0962, 0x0963], [0x0981, 0x0981],
5022 [0x09BC, 0x09BC], [0x09C1, 0x09C4], [0x09CD, 0x09CD],
5023 [0x09E2, 0x09E3], [0x0A01, 0x0A02], [0x0A3C, 0x0A3C],
5024 [0x0A41, 0x0A42], [0x0A47, 0x0A48], [0x0A4B, 0x0A4D],
5025 [0x0A70, 0x0A71], [0x0A81, 0x0A82], [0x0ABC, 0x0ABC],
5026 [0x0AC1, 0x0AC5], [0x0AC7, 0x0AC8], [0x0ACD, 0x0ACD],
5027 [0x0AE2, 0x0AE3], [0x0B01, 0x0B01], [0x0B3C, 0x0B3C],
5028 [0x0B3F, 0x0B3F], [0x0B41, 0x0B43], [0x0B4D, 0x0B4D],
5029 [0x0B56, 0x0B56], [0x0B82, 0x0B82], [0x0BC0, 0x0BC0],
5030 [0x0BCD, 0x0BCD], [0x0C3E, 0x0C40], [0x0C46, 0x0C48],
5031 [0x0C4A, 0x0C4D], [0x0C55, 0x0C56], [0x0CBC, 0x0CBC],
5032 [0x0CBF, 0x0CBF], [0x0CC6, 0x0CC6], [0x0CCC, 0x0CCD],
5033 [0x0CE2, 0x0CE3], [0x0D41, 0x0D43], [0x0D4D, 0x0D4D],
5034 [0x0DCA, 0x0DCA], [0x0DD2, 0x0DD4], [0x0DD6, 0x0DD6],
5035 [0x0E31, 0x0E31], [0x0E34, 0x0E3A], [0x0E47, 0x0E4E],
5036 [0x0EB1, 0x0EB1], [0x0EB4, 0x0EB9], [0x0EBB, 0x0EBC],
5037 [0x0EC8, 0x0ECD], [0x0F18, 0x0F19], [0x0F35, 0x0F35],
5038 [0x0F37, 0x0F37], [0x0F39, 0x0F39], [0x0F71, 0x0F7E],
5039 [0x0F80, 0x0F84], [0x0F86, 0x0F87], [0x0F90, 0x0F97],
5040 [0x0F99, 0x0FBC], [0x0FC6, 0x0FC6], [0x102D, 0x1030],
5041 [0x1032, 0x1032], [0x1036, 0x1037], [0x1039, 0x1039],
5042 [0x1058, 0x1059], [0x1160, 0x11FF], [0x135F, 0x135F],
5043 [0x1712, 0x1714], [0x1732, 0x1734], [0x1752, 0x1753],
5044 [0x1772, 0x1773], [0x17B4, 0x17B5], [0x17B7, 0x17BD],
5045 [0x17C6, 0x17C6], [0x17C9, 0x17D3], [0x17DD, 0x17DD],
5046 [0x180B, 0x180D], [0x18A9, 0x18A9], [0x1920, 0x1922],
5047 [0x1927, 0x1928], [0x1932, 0x1932], [0x1939, 0x193B],
5048 [0x1A17, 0x1A18], [0x1B00, 0x1B03], [0x1B34, 0x1B34],
5049 [0x1B36, 0x1B3A], [0x1B3C, 0x1B3C], [0x1B42, 0x1B42],
5050 [0x1B6B, 0x1B73], [0x1DC0, 0x1DCA], [0x1DFE, 0x1DFF],
5051 [0x200B, 0x200F], [0x202A, 0x202E], [0x2060, 0x2063],
5052 [0x206A, 0x206F], [0x20D0, 0x20EF], [0x302A, 0x302F],
5053 [0x3099, 0x309A], [0xA806, 0xA806], [0xA80B, 0xA80B],
5054 [0xA825, 0xA826], [0xFB1E, 0xFB1E], [0xFE00, 0xFE0F],
5055 [0xFE20, 0xFE23], [0xFEFF, 0xFEFF], [0xFFF9, 0xFFFB],
5056 [0x10A01, 0x10A03], [0x10A05, 0x10A06], [0x10A0C, 0x10A0F],
5057 [0x10A38, 0x10A3A], [0x10A3F, 0x10A3F], [0x1D167, 0x1D169],
5058 [0x1D173, 0x1D182], [0x1D185, 0x1D18B], [0x1D1AA, 0x1D1AD],
5059 [0x1D242, 0x1D244], [0xE0001, 0xE0001], [0xE0020, 0xE007F],
5063 function bisearch(ucs
) {
5065 var max
= COMBINING
.length
- 1;
5067 if (ucs
< COMBINING
[0][0] || ucs
> COMBINING
[max
][1])
5069 while (max
>= min
) {
5070 mid
= Math
.floor((min
+ max
) / 2);
5071 if (ucs
> COMBINING
[mid
][1])
5073 else if (ucs
< COMBINING
[mid
][0])
5080 function wcwidth(ucs
) {
5081 // test for 8-bit control characters
5084 if (ucs
< 32 || (ucs
>= 0x7f && ucs
< 0xa0))
5085 return opts
.control
;
5086 // binary search in table of non-spacing characters
5089 // if we arrive here, ucs is not a combining or C0/C1 control character
5094 ucs
<= 0x115f || // Hangul Jamo init. consonants
5097 (ucs
>= 0x2e80 && ucs
<= 0xa4cf && ucs
!= 0x303f) || // CJK..Yi
5098 (ucs
>= 0xac00 && ucs
<= 0xd7a3) || // Hangul Syllables
5099 (ucs
>= 0xf900 && ucs
<= 0xfaff) || // CJK Compat Ideographs
5100 (ucs
>= 0xfe10 && ucs
<= 0xfe19) || // Vertical forms
5101 (ucs
>= 0xfe30 && ucs
<= 0xfe6f) || // CJK Compat Forms
5102 (ucs
>= 0xff00 && ucs
<= 0xff60) || // Fullwidth Forms
5103 (ucs
>= 0xffe0 && ucs
<= 0xffe6) ||
5104 (ucs
>= 0x20000 && ucs
<= 0x2fffd) ||
5105 (ucs
>= 0x30000 && ucs
<= 0x3fffd)
5110 })({nul
: 0, control
: 0}); // configurable options
5116 Terminal
.EventEmitter
= EventEmitter
;
5117 Terminal
.inherits
= inherits
;
5120 * Adds an event listener to the terminal.
5122 * @param {string} event The name of the event. TODO: Document all event types
5123 * @param {function} callback The function to call when the event is triggered.
5127 Terminal
.cancel
= cancel
;
5129 module
.exports
= Terminal
;