]>
git.proxmox.com Git - mirror_xterm.js.git/blob - src/xterm.js
f506e8e14aee823b539500284547119ac74f3573
2 * xterm.js: xterm, in the browser
3 * Copyright (c) 2014, sourceLair Limited (www.sourcelair.com (MIT License)
4 * Copyright (c) 2012-2013, Christopher Jeffrey (MIT License)
5 * https://github.com/chjj/term.js
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * Originally forked from (with the author's permission):
26 * Fabrice Bellard's javascript vt100 for jslinux:
27 * http://bellard.org/jslinux/
28 * Copyright (c) 2011 Fabrice Bellard
29 * The original design remains. The terminal itself
30 * has been extended to include xterm CSI codes, among
35 if (typeof exports
=== 'object' && typeof module
=== 'object') {
37 * CommonJS environment
39 module
.exports
= xterm
.call(this);
40 } else if (typeof define
== 'function') {
42 * Require.js is available
44 define([], xterm
.bind(window
));
47 * Plain browser environment
49 this.Xterm
= xterm
.call(this);
50 this.Terminal
= this.Xterm
; /* Backwards compatibility with term.js */
54 * Terminal Emulation References:
56 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.txt
57 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
58 * http://invisible-island.net/vttest/
59 * http://www.inwap.com/pdp10/ansicode.txt
60 * http://linux.die.net/man/4/console_codes
61 * http://linux.die.net/man/7/urxvt
70 var window
= this, document
= this.document
;
76 function EventEmitter() {
77 this._events
= this._events
|| {};
80 EventEmitter
.prototype.addListener = function(type
, listener
) {
81 this._events
[type
] = this._events
[type
] || [];
82 this._events
[type
].push(listener
);
85 EventEmitter
.prototype.on
= EventEmitter
.prototype.addListener
;
87 EventEmitter
.prototype.removeListener = function(type
, listener
) {
88 if (!this._events
[type
]) return;
90 var obj
= this._events
[type
]
94 if (obj
[i
] === listener
|| obj
[i
].listener
=== listener
) {
101 EventEmitter
.prototype.off
= EventEmitter
.prototype.removeListener
;
103 EventEmitter
.prototype.removeAllListeners = function(type
) {
104 if (this._events
[type
]) delete this._events
[type
];
107 EventEmitter
.prototype.once = function(type
, listener
) {
110 var args
= Array
.prototype.slice
.call(arguments
);
111 self
.removeListener(type
, on
);
112 return listener
.apply(self
, args
);
114 on
.listener
= listener
;
115 return this.on(type
, on
);
118 EventEmitter
.prototype.emit = function(type
) {
119 if (!this._events
[type
]) return;
121 var args
= Array
.prototype.slice
.call(arguments
, 1)
122 , obj
= this._events
[type
]
127 obj
[i
].apply(this, args
);
131 EventEmitter
.prototype.listeners = function(type
) {
132 return this._events
[type
] = this._events
[type
] || [];
139 var normal
= 0, escaped
= 1, csi
= 2, osc
= 3, charset
= 4, dcs
= 5, ignore
= 6;
146 * Creates a new `Terminal` object.
148 * @param {object} options An object containing a set of options, the available options are:
149 * - cursorBlink (boolean): Whether the terminal cursor blinks
153 function Terminal(options
) {
156 if (!(this instanceof Terminal
)) {
157 return new Terminal(arguments
[0], arguments
[1], arguments
[2]);
160 self
.cancel
= Terminal
.cancel
;
162 EventEmitter
.call(this);
164 if (typeof options
=== 'number') {
168 handler
: arguments
[2]
172 options
= options
|| {};
175 Object
.keys(Terminal
.defaults
).forEach(function(key
) {
176 if (options
[key
] == null) {
177 options
[key
] = Terminal
.options
[key
];
179 if (Terminal
[key
] !== Terminal
.defaults
[key
]) {
180 options
[key
] = Terminal
[key
];
183 self
[key
] = options
[key
];
186 if (options
.colors
.length
=== 8) {
187 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(8));
188 } else if (options
.colors
.length
=== 16) {
189 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(16));
190 } else if (options
.colors
.length
=== 10) {
191 options
.colors
= options
.colors
.slice(0, -2).concat(
192 Terminal
._colors
.slice(8, -2), options
.colors
.slice(-2));
193 } else if (options
.colors
.length
=== 18) {
194 options
.colors
= options
.colors
.concat(
195 Terminal
._colors
.slice(16, -2), options
.colors
.slice(-2));
197 this.colors
= options
.colors
;
199 this.options
= options
;
201 // this.context = options.context || window;
202 // this.document = options.document || document;
203 this.parent
= options
.body
|| options
.parent
204 || (document
? document
.getElementsByTagName('body')[0] : null);
206 this.cols
= options
.cols
|| options
.geometry
[0];
207 this.rows
= options
.rows
|| options
.geometry
[1];
209 if (options
.handler
) {
210 this.on('data', options
.handler
);
214 * The scroll position of the y cursor, ie. ybase + y = the y position within the entire
220 * The scroll position of the viewport
225 * The cursor's x position after ybase
230 * The cursor's y position after ybase
235 * Used to debounce the refresh function
237 this.isRefreshing
= false;
240 * Whether there is a full terminal refresh queued
243 this.cursorState
= 0;
244 this.cursorHidden
= false;
249 this.scrollBottom
= this.rows
- 1;
250 this.customKeydownHandler
= null;
253 this.applicationKeypad
= false;
254 this.applicationCursor
= false;
255 this.originMode
= false;
256 this.insertMode
= false;
257 this.wraparoundMode
= true; // defaults: xterm - true, vt100 - false
262 this.gcharset
= null;
264 this.charsets
= [null];
288 this.readable
= true;
289 this.writable
= true;
291 this.defAttr
= (0 << 18) | (257 << 9) | (256 << 0);
292 this.curAttr
= this.defAttr
;
295 this.currentParam
= 0;
299 // leftover surrogate high from previous write invocation
300 this.surrogate_high
= '';
303 * An array of all lines in the entire buffer, including the prompt. The lines are array of
304 * characters which are 2-length arrays where [0] is an attribute and [1] is the character.
309 this.lines
.push(this.blankLine());
316 inherits(Terminal
, EventEmitter
);
318 // back_color_erase feature for xterm.
319 Terminal
.prototype.eraseAttr = function() {
320 // if (this.is('screen')) return this.defAttr;
321 return (this.defAttr
& ~0x1ff) | (this.curAttr
& 0x1ff);
329 Terminal
.tangoColors
= [
350 // Colors 0-15 + 16-255
351 // Much thanks to TooTallNate for writing this.
352 Terminal
.colors
= (function() {
353 var colors
= Terminal
.tangoColors
.slice()
354 , r
= [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff]
359 for (; i
< 216; i
++) {
360 out(r
[(i
/ 36) % 6 | 0], r
[(i
/ 6) % 6 | 0], r
[i
% 6]);
365 for (; i
< 24; i
++) {
370 function out(r
, g
, b
) {
371 colors
.push('#' + hex(r
) + hex(g
) + hex(b
));
376 return c
.length
< 2 ? '0' + c
: c
;
382 Terminal
._colors
= Terminal
.colors
.slice();
384 Terminal
.vcolors
= (function() {
386 , colors
= Terminal
.colors
390 for (; i
< 256; i
++) {
391 color
= parseInt(colors
[i
].substring(1), 16);
393 (color
>> 16) & 0xff,
406 Terminal
.defaults
= {
407 colors
: Terminal
.colors
,
419 // programFeatures: false,
423 Terminal
.options
= {};
425 Terminal
.focus
= null;
427 each(keys(Terminal
.defaults
), function(key
) {
428 Terminal
[key
] = Terminal
.defaults
[key
];
429 Terminal
.options
[key
] = Terminal
.defaults
[key
];
433 * Focus the terminal. Delegates focus handling to the terminal's DOM element.
437 Terminal
.prototype.focus = function() {
438 return this.element
.focus();
442 * Binds the desired focus behavior on a given terminal object.
446 Terminal
.bindFocus = function (term
) {
447 on(term
.element
, 'focus', function (ev
) {
448 if (term
.sendFocus
) {
453 Terminal
.focus
= term
;
454 term
.emit('focus', {terminal
: term
});
459 * Blur the terminal. Delegates blur handling to the terminal's DOM element.
463 Terminal
.prototype.blur = function() {
464 return this.element
.blur();
468 * Binds the desired blur behavior on a given terminal object.
472 Terminal
.bindBlur = function (term
) {
473 on(term
.element
, 'blur', function (ev
) {
474 if (term
.sendFocus
) {
477 Terminal
.focus
= null;
478 term
.emit('blur', {terminal
: term
});
483 * Initialize default behavior
485 Terminal
.prototype.initGlobal = function() {
486 Terminal
.bindKeys(this);
487 Terminal
.bindPaste(this);
488 Terminal
.bindCopy(this);
489 Terminal
.bindCut(this);
490 Terminal
.bindDrop(this);
491 Terminal
.bindFocus(this);
492 Terminal
.bindBlur(this);
496 * Clears all selected text, inside the terminal.
498 Terminal
.prototype.clearSelection = function() {
499 var selectionBaseNode
= window
.getSelection().baseNode
;
501 if (selectionBaseNode
&& (this.element
.contains(selectionBaseNode
.parentElement
))) {
502 window
.getSelection().removeAllRanges();
507 * This function temporarily enables (leases) the contentEditable value of the terminal, which
508 * should be set back to false within 5 seconds at most.
510 Terminal
.prototype.leaseContentEditable = function (ms
, callback
) {
513 term
.element
.contentEditable
= true;
516 * Blur and re-focus instantly. This is due to a weird focus state on Chrome, when setting
517 * contentEditable to true on a focused element.
522 setTimeout(function () {
523 term
.element
.contentEditable
= false;
524 if (typeof callback
== 'function') {
531 * Bind to paste event and allow both keyboard and right-click pasting, without having the
532 * contentEditable value set to true.
534 Terminal
.bindPaste = function(term
) {
535 on(term
.element
, 'paste', function(ev
) {
536 if (ev
.clipboardData
) {
537 var text
= ev
.clipboardData
.getData('text/plain');
538 term
.emit('paste', text
, ev
);
541 * Cancel the paste event, or else things will be pasted twice:
542 * 1. by the terminal handler
543 * 2. by the browser, because of the contentEditable value being true
545 term
.cancel(ev
, true);
548 * After the paste event is completed, always set the contentEditable value to false.
550 term
.element
.contentEditable
= false;
555 * Hack pasting with keyboard, in order to make it work without contentEditable.
556 * When a user types Ctrl + Shift + V or Shift + Insert on a non Mac or Cmd + V on a Mac,
557 * lease the contentEditable value as true.
559 on(term
.element
, 'keydown', function (ev
) {
560 var isEditable
= term
.element
.contentEditable
=== "true";
563 * If on a Mac, lease the contentEditable value temporarily, when the user presses
564 * the Cmd button, in a keydown event order to paste frictionlessly.
566 if (term
.isMac
&& ev
.metaKey
&& !isEditable
) {
567 term
.leaseContentEditable(5000);
570 if (!term
.isMac
&& !isEditable
) {
571 if ((ev
.keyCode
== 45 && ev
.shiftKey
&& !ev
.ctrlKey
) || // Shift + Insert
572 (ev
.keyCode
== 86 && ev
.shiftKey
&& ev
.ctrlKey
)) { // Ctrl + Shict + V
573 term
.leaseContentEditable();
579 * Hack pasting with right-click in order to allow right-click paste, by leasing the
580 * contentEditable value as true.
582 on(term
.element
, 'contextmenu', function (ev
) {
583 term
.leaseContentEditable();
589 * Apply key handling to the terminal
591 Terminal
.bindKeys = function(term
) {
592 on(term
.element
, 'keydown', function(ev
) {
596 on(term
.element
, 'keypress', function(ev
) {
602 * Prepares text copied from terminal selection, to be saved in the clipboard by:
603 * 1. stripping all trailing white spaces
604 * 2. converting all non-breaking spaces to regular spaces
605 * @param {string} text The copied text that needs processing for storing in clipboard
609 Terminal
.prepareCopiedTextForClipboard = function (text
) {
610 var space
= String
.fromCharCode(32),
611 nonBreakingSpace
= String
.fromCharCode(160),
612 allNonBreakingSpaces
= new RegExp(nonBreakingSpace
, 'g'),
613 processedText
= text
.split('\n').map(function (line
) {
615 * Strip all trailing white spaces and convert all non-breaking spaces to regular
618 var processedLine
= line
.replace(/\s+$/g, '').replace(allNonBreakingSpaces
, space
);
620 return processedLine
;
623 return processedText
;
627 * Binds copy functionality to the given terminal.
630 Terminal
.bindCopy = function(term
) {
631 on(term
.element
, 'copy', function(ev
) {
632 var copiedText
= window
.getSelection().toString(),
633 text
= Terminal
.prepareCopiedTextForClipboard(copiedText
);
635 ev
.clipboardData
.setData('text/plain', text
);
641 * Cancel the cut event completely
643 Terminal
.bindCut = function(term
) {
644 on(term
.element
, 'cut', function (ev
) {
650 Terminal
.bindDrop = function (term
) {
652 * Do not perform the "drop" event. Altering the contents of the
653 * terminal with drag n drop is unwanted behavior.
655 on(term
.element
, 'drop', function (ev
) {
656 term
.cancel(ev
, true);
661 Terminal
.click = function (term
) {
663 * Do not perform the "drop" event. Altering the contents of the
664 * terminal with drag n drop is unwanted behavior.
666 on(term
.element
, 'click', function (ev
) {
667 term
.cancel(ev
, true);
673 * Insert the given row to the terminal or produce a new one
674 * if no row argument is passed. Return the inserted row.
676 Terminal
.prototype.insertRow = function (row
) {
677 if (typeof row
!= 'object') {
678 row
= document
.createElement('div');
681 this.rowContainer
.appendChild(row
);
682 this.children
.push(row
);
689 * Opens the terminal within an element.
691 * @param {HTMLElement} parent The element to create the terminal within.
695 Terminal
.prototype.open = function(parent
) {
696 var self
=this, i
=0, div
;
698 this.parent
= parent
|| this.parent
;
701 throw new Error('Terminal requires a parent element.');
705 * Grab global elements
707 this.context
= this.parent
.ownerDocument
.defaultView
;
708 this.document
= this.parent
.ownerDocument
;
709 this.body
= this.document
.getElementsByTagName('body')[0];
714 if (this.context
.navigator
&& this.context
.navigator
.userAgent
) {
715 this.isMSIE
= !!~this.context
.navigator
.userAgent
.indexOf('MSIE');
719 * Find the users platform. We use this to interpret the meta key
720 * and ISO third level shifts.
721 * http://stackoverflow.com/questions/19877924/what-is-the-list-of-possible-values-for-navigator-platform-as-of-today
723 if (this.context
.navigator
&& this.context
.navigator
.platform
) {
724 this.isMac
= contains(
725 this.context
.navigator
.platform
,
726 ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K']
728 this.isIpad
= this.context
.navigator
.platform
=== 'iPad';
729 this.isIphone
= this.context
.navigator
.platform
=== 'iPhone';
730 this.isMSWindows
= contains(
731 this.context
.navigator
.platform
,
732 ['Windows', 'Win16', 'Win32', 'WinCE']
737 * Create main element container
739 this.element
= this.document
.createElement('div');
740 this.element
.classList
.add('terminal');
741 this.element
.classList
.add('xterm');
742 this.element
.classList
.add('xterm-theme-' + this.theme
);
743 this.element
.setAttribute('tabindex', 0);
744 this.element
.spellcheck
= false;
747 * Create the container that will hold the lines of the terminal and then
748 * produce the lines the lines.
750 this.rowContainer
= document
.createElement('div');
751 this.rowContainer
.classList
.add('xterm-rows');
752 this.element
.appendChild(this.rowContainer
);
755 for (; i
< this.rows
; i
++) {
758 this.parent
.appendChild(this.element
);
761 this.refresh(0, this.rows
- 1);
763 // Initialize global actions that
764 // need to be taken on the document.
767 // Ensure there is a Terminal.focus.
770 on(this.element
, 'mouseup', function() {
771 var selection
= document
.getSelection(),
772 collapsed
= selection
.isCollapsed
,
773 isRange
= typeof collapsed
== 'boolean' ? !collapsed
: selection
.type
== 'Range';
779 // Listen for mouse events and translate
780 // them into terminal mouse protocols.
783 // Figure out whether boldness affects
784 // the character width of monospace fonts.
785 if (Terminal
.brokenBold
== null) {
786 Terminal
.brokenBold
= isBoldBroken(this.document
);
794 * Attempts to load an add-on using CommonJS or RequireJS (whichever is available).
795 * @param {string} addon The name of the addon to load
798 Terminal
.loadAddon = function(addon
, callback
) {
799 if (typeof exports
=== 'object' && typeof module
=== 'object') {
801 return require(__dirname
+ '/../addons/' + addon
);
802 } else if (typeof define
== 'function') {
804 return require(['../addons/' + addon
+ '/' + addon
], callback
);
806 console
.error('Cannot load a module without a CommonJS or RequireJS environment.');
812 // XTerm mouse events
813 // http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#Mouse%20Tracking
814 // To better understand these
815 // the xterm code is very helpful:
817 // button.c, charproc.c, misc.c
818 // Relevant functions in xterm/button.c:
819 // BtnCode, EmitButtonCode, EditorButton, SendMousePosition
820 Terminal
.prototype.bindMouse = function() {
821 var el
= this.element
825 var wheelEvent
= 'onmousewheel' in this.context
829 // mouseup, mousedown, mousewheel
830 // left click: ^[[M 3<^[[M#3<
831 // mousewheel up: ^[[M`3>
832 function sendButton(ev
) {
836 // get the xterm-style button
837 button
= getButton(ev
);
839 // get mouse coordinates
843 sendEvent(button
, pos
);
845 switch (ev
.overrideType
|| ev
.type
) {
850 // keep it at the left
851 // button, just in case.
862 // motion example of a left click:
863 // ^[[M 3<^[[M@4<^[[M@5<^[[M@6<^[[M@7<^[[M#7<
864 function sendMove(ev
) {
871 // buttons marked as motions
872 // are incremented by 32
875 sendEvent(button
, pos
);
879 // position to characters
880 function encode(data
, ch
) {
881 if (!self
.utfMouse
) {
882 if (ch
=== 255) return data
.push(0);
883 if (ch
> 127) ch
= 127;
886 if (ch
=== 2047) return data
.push(0);
890 if (ch
> 2047) ch
= 2047;
891 data
.push(0xC0 | (ch
>> 6));
892 data
.push(0x80 | (ch
& 0x3F));
897 // send a mouse event:
898 // regular/utf8: ^[[M Cb Cx Cy
899 // urxvt: ^[[ Cb ; Cx ; Cy M
900 // sgr: ^[[ Cb ; Cx ; Cy M/m
901 // vt300: ^[[ 24(1/3/5)~ [ Cx , Cy ] \r
902 // locator: CSI P e ; P b ; P r ; P c ; P p & w
903 function sendEvent(button
, pos
) {
904 // self.emit('mouse', {
910 if (self
.vt300Mouse
) {
912 // http://www.vt100.net/docs/vt3xx-gp/chapter15.html
916 var data
= '\x1b[24';
917 if (button
=== 0) data
+= '1';
918 else if (button
=== 1) data
+= '3';
919 else if (button
=== 2) data
+= '5';
920 else if (button
=== 3) return;
922 data
+= '~[' + pos
.x
+ ',' + pos
.y
+ ']\r';
927 if (self
.decLocator
) {
932 if (button
=== 0) button
= 2;
933 else if (button
=== 1) button
= 4;
934 else if (button
=== 2) button
= 6;
935 else if (button
=== 3) button
= 3;
939 + (button
=== 3 ? 4 : 0)
950 if (self
.urxvtMouse
) {
955 self
.send('\x1b[' + button
+ ';' + pos
.x
+ ';' + pos
.y
+ 'M');
963 + ((button
& 3) === 3 ? button
& ~3 : button
)
968 + ((button
& 3) === 3 ? 'm' : 'M'));
974 encode(data
, button
);
978 self
.send('\x1b[M' + String
.fromCharCode
.apply(String
, data
));
981 function getButton(ev
) {
994 // 1, and 2 - with 64 added
995 switch (ev
.overrideType
|| ev
.type
) {
997 button
= ev
.button
!= null
1004 button
= button
=== 1 ? 0 : button
=== 4 ? 1 : button
;
1010 case 'DOMMouseScroll':
1011 button
= ev
.detail
< 0
1016 button
= ev
.wheelDeltaY
> 0
1022 // next three bits are the modifiers:
1023 // 4 = shift, 8 = meta, 16 = control
1024 shift
= ev
.shiftKey
? 4 : 0;
1025 meta
= ev
.metaKey
? 8 : 0;
1026 ctrl
= ev
.ctrlKey
? 16 : 0;
1027 mod
= shift
| meta
| ctrl
;
1030 if (self
.vt200Mouse
) {
1033 } else if (!self
.normalMouse
) {
1038 button
= (32 + (mod
<< 2)) + button
;
1043 // mouse coordinates measured in cols/rows
1044 function getCoords(ev
) {
1047 // ignore browsers without pageX for now
1048 if (ev
.pageX
== null) return;
1054 // should probably check offsetParent
1055 // but this is more portable
1056 while (el
&& el
!== self
.document
.documentElement
) {
1059 el
= 'offsetParent' in el
1064 // convert to cols/rows
1065 w
= self
.element
.clientWidth
;
1066 h
= self
.element
.clientHeight
;
1067 x
= Math
.round((x
/ w
) * self
.cols
);
1068 y
= Math
.round((y
/ h
) * self
.rows
);
1070 // be sure to avoid sending
1071 // bad positions to the program
1073 if (x
> self
.cols
) x
= self
.cols
;
1075 if (y
> self
.rows
) y
= self
.rows
;
1077 // xterm sends raw bytes and
1078 // starts at 32 (SP) for each.
1085 type
: (ev
.overrideType
|| ev
.type
) === wheelEvent
1087 : (ev
.overrideType
|| ev
.type
)
1091 on(el
, 'mousedown', function(ev
) {
1092 if (!self
.mouseEvents
) return;
1101 //if (self.vt200Mouse && !self.normalMouse) {
1102 if (self
.vt200Mouse
) {
1103 ev
.overrideType
= 'mouseup';
1105 return self
.cancel(ev
);
1109 if (self
.normalMouse
) on(self
.document
, 'mousemove', sendMove
);
1111 // x10 compatibility mode can't send button releases
1112 if (!self
.x10Mouse
) {
1113 on(self
.document
, 'mouseup', function up(ev
) {
1115 if (self
.normalMouse
) off(self
.document
, 'mousemove', sendMove
);
1116 off(self
.document
, 'mouseup', up
);
1117 return self
.cancel(ev
);
1121 return self
.cancel(ev
);
1124 //if (self.normalMouse) {
1125 // on(self.document, 'mousemove', sendMove);
1128 on(el
, wheelEvent
, function(ev
) {
1129 if (!self
.mouseEvents
) return;
1132 || self
.decLocator
) return;
1134 return self
.cancel(ev
);
1137 // allow mousewheel scrolling in
1138 // the shell for example
1139 on(el
, wheelEvent
, function(ev
) {
1140 if (self
.mouseEvents
) return;
1141 if (self
.applicationKeypad
) return;
1142 if (ev
.type
=== 'DOMMouseScroll') {
1143 self
.scrollDisp(ev
.detail
< 0 ? -1 : 1);
1145 self
.scrollDisp(ev
.wheelDeltaY
> 0 ? -1 : 1);
1147 return self
.cancel(ev
);
1152 * Destroys the terminal.
1156 Terminal
.prototype.destroy = function() {
1157 this.readable
= false;
1158 this.writable
= false;
1160 this.handler = function() {};
1161 this.write = function() {};
1162 if (this.element
.parentNode
) {
1163 this.element
.parentNode
.removeChild(this.element
);
1165 //this.emit('close');
1170 * Flags used to render terminal text properly
1183 * In the screen buffer, each character
1184 * is stored as a an array with a character
1185 * and a 32-bit integer.
1186 * First value: a utf-16 character.
1188 * Next 9 bits: background color (0-511).
1189 * Next 9 bits: foreground color (0-511).
1190 * Next 14 bits: a mask for misc. flags:
1191 * 1=bold, 2=underline, 4=blink, 8=inverse, 16=invisible
1195 * Refreshes terminal content within two rows (inclusive).
1197 * @param {number} start The row to start from (between 0 and terminal's height terminal - 1)
1198 * @param {number} end The row to end at (between fromRow and terminal's height terminal - 1)
1199 * @param {boolean} queue Whether the refresh should ran right now or be queued
1203 Terminal
.prototype.refresh = function(start
, end
, queue
) {
1206 // queue defaults to true
1207 queue
= (typeof queue
== 'undefined') ? true : queue
;
1210 * The refresh queue allows refresh to execute only approximately 30 times a second. For
1211 * commands that pass a significant amount of output to the write function, this prevents the
1212 * terminal from maxing out the CPU and making the UI unresponsive. While commands can still
1213 * run beyond what they do on the terminal, it is far better with a debounce in place as
1214 * every single terminal manipulation does not need to be constructed in the DOM.
1216 * A side-effect of this is that it makes ^C to interrupt a process seem more responsive.
1219 // If refresh should be queued, order the refresh and return.
1220 if (this._refreshIsQueued
) {
1221 // If a refresh has already been queued, just order a full refresh next
1222 this._fullRefreshNext
= true;
1224 setTimeout(function () {
1225 self
.refresh(start
, end
, false);
1227 this._refreshIsQueued
= true;
1232 // If refresh should be run right now (not be queued), release the lock
1233 this._refreshIsQueued
= false;
1235 // If multiple refreshes were requested, make a full refresh.
1236 if (this._fullRefreshNext
) {
1238 end
= this.rows
- 1;
1239 this._fullRefreshNext
= false // reset lock
1242 var x
, y
, i
, line
, out
, ch
, ch_width
, width
, data
, attr
, bg
, fg
, flags
, row
, parent
, focused
= document
.activeElement
;
1244 // If this is a big refresh, remove the terminal rows from the DOM for faster calculations
1245 if (end
- start
>= this.rows
/ 2) {
1246 parent
= this.element
.parentNode
;
1248 this.element
.removeChild(this.rowContainer
);
1255 if (end
>= this.rows
.length
) {
1256 this.log('`end` is too large. Most likely a bad CSR.');
1257 end
= this.rows
.length
- 1;
1260 for (; y
<= end
; y
++) {
1261 row
= y
+ this.ydisp
;
1263 line
= this.lines
[row
];
1266 if (this.y
=== y
- (this.ybase
- this.ydisp
)
1268 && !this.cursorHidden
) {
1274 attr
= this.defAttr
;
1277 for (; i
< width
; i
++) {
1280 ch_width
= line
[i
][2];
1284 if (i
=== x
) data
= -1;
1286 if (data
!== attr
) {
1287 if (attr
!== this.defAttr
) {
1290 if (data
!== this.defAttr
) {
1292 out
+= '<span class="reverse-video terminal-cursor';
1293 if (this.cursorBlink
) {
1298 var classNames
= [];
1301 fg
= (data
>> 9) & 0x1ff;
1304 if (flags
& Terminal
.flags
.BOLD
) {
1305 if (!Terminal
.brokenBold
) {
1306 classNames
.push('xterm-bold');
1308 // See: XTerm*boldColors
1309 if (fg
< 8) fg
+= 8;
1312 if (flags
& Terminal
.flags
.UNDERLINE
) {
1313 classNames
.push('xterm-underline');
1316 if (flags
& Terminal
.flags
.BLINK
) {
1317 classNames
.push('xterm-blink');
1321 * If inverse flag is on, then swap the foreground and background variables.
1323 if (flags
& Terminal
.flags
.INVERSE
) {
1324 /* One-line variable swap in JavaScript: http://stackoverflow.com/a/16201730 */
1325 bg = [fg, fg = bg][0];
1326 // Should inverse just be before the
1327 // above boldColors effect instead?
1328 if ((flags & 1) && fg < 8) fg += 8;
1331 if (flags & Terminal.flags.INVISIBLE) {
1332 classNames.push('xterm-hidden');
1336 * Weird situation: Invert flag used black foreground and white background results
1337 * in invalid background color, positioned at the 256 index of the 256 terminal
1338 * color map. Pin the colors manually in such a case.
1340 * Source: https://github.com/sourcelair/xterm.js/issues/57
1342 if (flags
& Terminal
.flags
.INVERSE
) {
1352 classNames
.push('xterm-bg-color-' + bg
);
1356 classNames
.push('xterm-color-' + fg
);
1360 if (classNames
.length
) {
1361 out
+= ' class="' + classNames
.join(' ') + '"';
1390 if (attr
!== this.defAttr
) {
1394 this.children
[y
].innerHTML
= out
;
1398 this.element
.appendChild(this.rowContainer
);
1401 this.emit('refresh', {element
: this.element
, start
: start
, end
: end
});
1404 Terminal
.prototype.showCursor = function() {
1405 if (!this.cursorState
) {
1406 this.cursorState
= 1;
1407 this.refresh(this.y
, this.y
);
1411 Terminal
.prototype.scroll = function() {
1414 if (++this.ybase
=== this.scrollback
) {
1415 this.ybase
= this.ybase
/ 2 | 0;
1416 this.lines
= this.lines
.slice(-(this.ybase
+ this.rows
) + 1);
1419 this.ydisp
= this.ybase
;
1422 row
= this.ybase
+ this.rows
- 1;
1424 // subtract the bottom scroll region
1425 row
-= this.rows
- 1 - this.scrollBottom
;
1427 if (row
=== this.lines
.length
) {
1428 // potential optimization:
1429 // pushing is faster than splicing
1430 // when they amount to the same
1432 this.lines
.push(this.blankLine());
1435 this.lines
.splice(row
, 0, this.blankLine());
1438 if (this.scrollTop
!== 0) {
1439 if (this.ybase
!== 0) {
1441 this.ydisp
= this.ybase
;
1443 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
1447 this.updateRange(this.scrollTop
);
1448 this.updateRange(this.scrollBottom
);
1451 Terminal
.prototype.scrollDisp = function(disp
) {
1454 if (this.ydisp
> this.ybase
) {
1455 this.ydisp
= this.ybase
;
1456 } else if (this.ydisp
< 0) {
1460 this.refresh(0, this.rows
- 1);
1464 * Writes text to the terminal.
1466 * @param {string} text The text to write to the terminal.
1470 Terminal
.prototype.write = function(data
) {
1471 var l
= data
.length
, i
= 0, j
, cs
, ch
, code
, low
, ch_width
, row
;
1473 this.refreshStart
= this.y
;
1474 this.refreshEnd
= this.y
;
1476 if (this.ybase
!== this.ydisp
) {
1477 this.ydisp
= this.ybase
;
1481 // apply leftover surrogate high from last write
1482 if (this.surrogate_high
) {
1483 data
= this.surrogate_high
+ data
;
1484 this.surrogate_high
= '';
1487 for (; i
< l
; i
++) {
1490 // FIXME: higher chars than 0xa0 are not allowed in escape sequences
1491 // --> maybe move to default
1492 code
= data
.charCodeAt(i
);
1493 if (0xD800 <= code
&& code
<= 0xDBFF) {
1494 // we got a surrogate high
1495 // get surrogate low (next 2 bytes)
1496 low
= data
.charCodeAt(i
+1);
1498 // end of data stream, save surrogate high
1499 this.surrogate_high
= ch
;
1502 code
= ((code
- 0xD800) * 0x400) + (low
- 0xDC00) + 0x10000;
1503 ch
+= data
.charAt(i
+1);
1505 // surrogate low - already handled above
1506 if (0xDC00 <= code
&& code
<= 0xDFFF)
1509 switch (this.state
) {
1520 if (this.convertEol
) {
1524 if (this.y
> this.scrollBottom
) {
1544 this.x
= this.nextStop();
1559 this.state
= escaped
;
1564 // calculate print space
1565 // expensive call, therefore we save width in line buffer
1566 ch_width
= wcwidth(code
);
1569 if (this.charset
&& this.charset
[ch
]) {
1570 ch
= this.charset
[ch
];
1573 row
= this.y
+ this.ybase
;
1575 // insert combining char in last cell
1576 // FIXME: needs handling after cursor jumps
1577 if (!ch_width
&& this.x
) {
1579 // dont overflow left
1580 if (this.lines
[row
][this.x
-1]) {
1581 if (!this.lines
[row
][this.x
-1][2]) {
1583 // found empty cell after fullwidth, need to go 2 cells back
1584 if (this.lines
[row
][this.x
-2])
1585 this.lines
[row
][this.x
-2][1] += ch
;
1588 this.lines
[row
][this.x
-1][1] += ch
;
1590 this.updateRange(this.y
);
1595 // goto next line if ch would overflow
1596 // TODO: needs a global min terminal width of 2
1597 if (this.x
+ch_width
-1 >= this.cols
) {
1598 // autowrap - DECAWM
1599 if (this.wraparoundMode
) {
1602 if (this.y
> this.scrollBottom
) {
1607 this.x
= this.cols
-1;
1608 if(ch_width
===2) // FIXME: check for xterm behavior
1612 row
= this.y
+ this.ybase
;
1614 // insert mode: move characters to right
1615 if (this.insertMode
) {
1616 // do this twice for a fullwidth char
1617 for (var moves
=0; moves
<ch_width
; ++moves
) {
1618 // remove last cell, if it's width is 0
1619 // we have to adjust the second last cell as well
1620 var removed
= this.lines
[this.y
+ this.ybase
].pop();
1622 && this.lines
[row
][this.cols
-2]
1623 && this.lines
[row
][this.cols
-2][2]===2)
1624 this.lines
[row
][this.cols
-2] = [this.curAttr
, ' ', 1];
1626 // insert empty cell at cursor
1627 this.lines
[row
].splice(this.x
, 0, [this.curAttr
, ' ', 1]);
1631 this.lines
[row
][this.x
] = [this.curAttr
, ch
, ch_width
];
1633 this.updateRange(this.y
);
1635 // fullwidth char - set next cell width to zero and advance cursor
1637 this.lines
[row
][this.x
] = [this.curAttr
, '', 0];
1646 // ESC [ Control Sequence Introducer ( CSI is 0x9b).
1649 this.currentParam
= 0;
1653 // ESC ] Operating System Command ( OSC is 0x9d).
1656 this.currentParam
= 0;
1660 // ESC P Device Control String ( DCS is 0x90).
1663 this.currentParam
= 0;
1667 // ESC _ Application Program Command ( APC is 0x9f).
1669 this.state
= ignore
;
1672 // ESC ^ Privacy Message ( PM is 0x9e).
1674 this.state
= ignore
;
1677 // ESC c Full Reset (RIS).
1682 // ESC E Next Line ( NEL is 0x85).
1683 // ESC D Index ( IND is 0x84).
1691 // ESC M Reverse Index ( RI is 0x8d).
1693 this.reverseIndex();
1696 // ESC % Select default/utf-8 character set.
1697 // @ = default, G = utf-8
1699 //this.charset = null;
1701 this.setgCharset(0, Terminal
.charsets
.US
);
1702 this.state
= normal
;
1706 // ESC (,),*,+,-,. Designate G0-G2 Character Set.
1707 case '(': // <-- this seems to get all the attention
1733 this.state
= charset
;
1736 // Designate G3 Character Set (VT300).
1737 // A = ISO Latin-1 Supplemental.
1741 this.state
= charset
;
1746 // Single Shift Select of G2 Character Set
1747 // ( SS2 is 0x8e). This affects next character only.
1751 // Single Shift Select of G3 Character Set
1752 // ( SS3 is 0x8f). This affects next character only.
1756 // Invoke the G2 Character Set as GL (LS2).
1761 // Invoke the G3 Character Set as GL (LS3).
1766 // Invoke the G3 Character Set as GR (LS3R).
1771 // Invoke the G2 Character Set as GR (LS2R).
1776 // Invoke the G1 Character Set as GR (LS1R).
1781 // ESC 7 Save Cursor (DECSC).
1784 this.state
= normal
;
1787 // ESC 8 Restore Cursor (DECRC).
1789 this.restoreCursor();
1790 this.state
= normal
;
1793 // ESC # 3 DEC line height/width
1795 this.state
= normal
;
1799 // ESC H Tab Set (HTS is 0x88).
1804 // ESC = Application Keypad (DECPAM).
1806 this.log('Serial port requested application keypad.');
1807 this.applicationKeypad
= true;
1808 this.state
= normal
;
1811 // ESC > Normal Keypad (DECPNM).
1813 this.log('Switching back to normal keypad.');
1814 this.applicationKeypad
= false;
1815 this.state
= normal
;
1819 this.state
= normal
;
1820 this.error('Unknown ESC control: %s.', ch
);
1827 case '0': // DEC Special Character and Line Drawing Set.
1828 cs
= Terminal
.charsets
.SCLD
;
1831 cs
= Terminal
.charsets
.UK
;
1833 case 'B': // United States (USASCII).
1834 cs
= Terminal
.charsets
.US
;
1837 cs
= Terminal
.charsets
.Dutch
;
1839 case 'C': // Finnish
1841 cs
= Terminal
.charsets
.Finnish
;
1844 cs
= Terminal
.charsets
.French
;
1846 case 'Q': // FrenchCanadian
1847 cs
= Terminal
.charsets
.FrenchCanadian
;
1850 cs
= Terminal
.charsets
.German
;
1852 case 'Y': // Italian
1853 cs
= Terminal
.charsets
.Italian
;
1855 case 'E': // NorwegianDanish
1857 cs
= Terminal
.charsets
.NorwegianDanish
;
1859 case 'Z': // Spanish
1860 cs
= Terminal
.charsets
.Spanish
;
1862 case 'H': // Swedish
1864 cs
= Terminal
.charsets
.Swedish
;
1867 cs
= Terminal
.charsets
.Swiss
;
1869 case '/': // ISOLatin (actually /A)
1870 cs
= Terminal
.charsets
.ISOLatin
;
1874 cs
= Terminal
.charsets
.US
;
1877 this.setgCharset(this.gcharset
, cs
);
1878 this.gcharset
= null;
1879 this.state
= normal
;
1885 // Set Text Parameters.
1886 if (ch
=== '\x1b' || ch
=== '\x07') {
1887 if (ch
=== '\x1b') i
++;
1889 this.params
.push(this.currentParam
);
1891 switch (this.params
[0]) {
1895 if (this.params
[1]) {
1896 this.title
= this.params
[1];
1897 this.handleTitle(this.title
);
1905 // change dynamic colors
1917 // change dynamic ui colors
1929 // manipulate selection data
1947 this.currentParam
= 0;
1948 this.state
= normal
;
1950 if (!this.params
.length
) {
1951 if (ch
>= '0' && ch
<= '9') {
1953 this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1954 } else if (ch
=== ';') {
1955 this.params
.push(this.currentParam
);
1956 this.currentParam
= '';
1959 this.currentParam
+= ch
;
1966 if (ch
=== '?' || ch
=== '>' || ch
=== '!') {
1972 if (ch
>= '0' && ch
<= '9') {
1973 this.currentParam
= this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1977 // '$', '"', ' ', '\''
1978 if (ch
=== '$' || ch
=== '"' || ch
=== ' ' || ch
=== '\'') {
1983 this.params
.push(this.currentParam
);
1984 this.currentParam
= 0;
1987 if (ch
=== ';') break;
1989 this.state
= normal
;
1993 // Cursor Up Ps Times (default = 1) (CUU).
1995 this.cursorUp(this.params
);
1999 // Cursor Down Ps Times (default = 1) (CUD).
2001 this.cursorDown(this.params
);
2005 // Cursor Forward Ps Times (default = 1) (CUF).
2007 this.cursorForward(this.params
);
2011 // Cursor Backward Ps Times (default = 1) (CUB).
2013 this.cursorBackward(this.params
);
2017 // Cursor Position [row;column] (default = [1,1]) (CUP).
2019 this.cursorPos(this.params
);
2022 // CSI Ps J Erase in Display (ED).
2024 this.eraseInDisplay(this.params
);
2027 // CSI Ps K Erase in Line (EL).
2029 this.eraseInLine(this.params
);
2032 // CSI Pm m Character Attributes (SGR).
2035 this.charAttributes(this.params
);
2039 // CSI Ps n Device Status Report (DSR).
2042 this.deviceStatus(this.params
);
2051 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
2053 this.insertChars(this.params
);
2057 // Cursor Next Line Ps Times (default = 1) (CNL).
2059 this.cursorNextLine(this.params
);
2063 // Cursor Preceding Line Ps Times (default = 1) (CNL).
2065 this.cursorPrecedingLine(this.params
);
2069 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
2071 this.cursorCharAbsolute(this.params
);
2075 // Insert Ps Line(s) (default = 1) (IL).
2077 this.insertLines(this.params
);
2081 // Delete Ps Line(s) (default = 1) (DL).
2083 this.deleteLines(this.params
);
2087 // Delete Ps Character(s) (default = 1) (DCH).
2089 this.deleteChars(this.params
);
2093 // Erase Ps Character(s) (default = 1) (ECH).
2095 this.eraseChars(this.params
);
2098 // CSI Pm ` Character Position Absolute
2099 // [column] (default = [row,1]) (HPA).
2101 this.charPosAbsolute(this.params
);
2105 // Horizontal Position Relative
2107 this.HPositionRelative(this.params
);
2111 // Send Device Attributes (Primary DA).
2113 // Send Device Attributes (Secondary DA)
2115 this.sendDeviceAttributes(this.params
);
2119 // Line Position Absolute [row] (default = [1,column]) (VPA).
2121 this.linePosAbsolute(this.params
);
2124 // 145 65 e * VPR - Vertical Position Relative
2126 this.VPositionRelative(this.params
);
2130 // Horizontal and Vertical Position [row;column] (default =
2133 this.HVPosition(this.params
);
2136 // CSI Pm h Set Mode (SM).
2137 // CSI ? Pm h - mouse escape codes, cursor escape codes
2139 this.setMode(this.params
);
2142 // CSI Pm l Reset Mode (RM).
2145 this.resetMode(this.params
);
2149 // Set Scrolling Region [top;bottom] (default = full size of win-
2153 this.setScrollRegion(this.params
);
2157 // Save cursor (ANSI.SYS).
2159 this.saveCursor(this.params
);
2163 // Restore cursor (ANSI.SYS).
2165 this.restoreCursor(this.params
);
2173 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
2175 this.cursorForwardTab(this.params
);
2178 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
2180 this.scrollUp(this.params
);
2183 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
2184 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
2187 // if (this.prefix === '>') {
2188 // this.resetTitleModes(this.params);
2191 // if (this.params.length > 2) {
2192 // this.initMouseTracking(this.params);
2195 if (this.params
.length
< 2 && !this.prefix
) {
2196 this.scrollDown(this.params
);
2201 // Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
2203 this.cursorBackwardTab(this.params
);
2206 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
2208 this.repeatPrecedingCharacter(this.params
);
2211 // CSI Ps g Tab Clear (TBC).
2213 this.tabClear(this.params
);
2216 // CSI Pm i Media Copy (MC).
2219 // this.mediaCopy(this.params);
2222 // CSI Pm m Character Attributes (SGR).
2224 // case 'm': // duplicate
2225 // if (this.prefix === '>') {
2226 // this.setResources(this.params);
2228 // this.charAttributes(this.params);
2232 // CSI Ps n Device Status Report (DSR).
2234 // case 'n': // duplicate
2235 // if (this.prefix === '>') {
2236 // this.disableModifiers(this.params);
2238 // this.deviceStatus(this.params);
2242 // CSI > Ps p Set pointer mode.
2243 // CSI ! p Soft terminal reset (DECSTR).
2245 // Request ANSI mode (DECRQM).
2247 // Request DEC private mode (DECRQM).
2250 switch (this.prefix
) {
2252 // this.setPointerMode(this.params);
2255 this.softReset(this.params
);
2258 // if (this.postfix === '$') {
2259 // this.requestPrivateMode(this.params);
2263 // if (this.postfix === '"') {
2264 // this.setConformanceLevel(this.params);
2265 // } else if (this.postfix === '$') {
2266 // this.requestAnsiMode(this.params);
2272 // CSI Ps q Load LEDs (DECLL).
2276 // if (this.postfix === ' ') {
2277 // this.setCursorStyle(this.params);
2280 // if (this.postfix === '"') {
2281 // this.setCharProtectionAttr(this.params);
2284 // this.loadLEDs(this.params);
2288 // Set Scrolling Region [top;bottom] (default = full size of win-
2291 // CSI Pt; Pl; Pb; Pr; Ps$ r
2292 // case 'r': // duplicate
2293 // if (this.prefix === '?') {
2294 // this.restorePrivateValues(this.params);
2295 // } else if (this.postfix === '$') {
2296 // this.setAttrInRectangle(this.params);
2298 // this.setScrollRegion(this.params);
2302 // CSI s Save cursor (ANSI.SYS).
2304 // case 's': // duplicate
2305 // if (this.prefix === '?') {
2306 // this.savePrivateValues(this.params);
2308 // this.saveCursor(this.params);
2312 // CSI Ps ; Ps ; Ps t
2313 // CSI Pt; Pl; Pb; Pr; Ps$ t
2317 // if (this.postfix === '$') {
2318 // this.reverseAttrInRectangle(this.params);
2319 // } else if (this.postfix === ' ') {
2320 // this.setWarningBellVolume(this.params);
2322 // if (this.prefix === '>') {
2323 // this.setTitleModeFeature(this.params);
2325 // this.manipulateWindow(this.params);
2330 // CSI u Restore cursor (ANSI.SYS).
2332 // case 'u': // duplicate
2333 // if (this.postfix === ' ') {
2334 // this.setMarginBellVolume(this.params);
2336 // this.restoreCursor(this.params);
2340 // CSI Pt; Pl; Pb; Pr; Pp; Pt; Pl; Pp$ v
2342 // if (this.postfix === '$') {
2343 // this.copyRectagle(this.params);
2347 // CSI Pt ; Pl ; Pb ; Pr ' w
2349 // if (this.postfix === '\'') {
2350 // this.enableFilterRectangle(this.params);
2354 // CSI Ps x Request Terminal Parameters (DECREQTPARM).
2355 // CSI Ps x Select Attribute Change Extent (DECSACE).
2356 // CSI Pc; Pt; Pl; Pb; Pr$ x
2358 // if (this.postfix === '$') {
2359 // this.fillRectangle(this.params);
2361 // this.requestParameters(this.params);
2362 // //this.__(this.params);
2367 // CSI Pt; Pl; Pb; Pr$ z
2369 // if (this.postfix === '\'') {
2370 // this.enableLocatorReporting(this.params);
2371 // } else if (this.postfix === '$') {
2372 // this.eraseRectangle(this.params);
2377 // CSI Pt; Pl; Pb; Pr$ {
2379 // if (this.postfix === '\'') {
2380 // this.setLocatorEvents(this.params);
2381 // } else if (this.postfix === '$') {
2382 // this.selectiveEraseRectangle(this.params);
2388 // if (this.postfix === '\'') {
2389 // this.requestLocatorPosition(this.params);
2394 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
2396 // if (this.postfix === ' ') {
2397 // this.insertColumns(this.params);
2402 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
2404 // if (this.postfix === ' ') {
2405 // this.deleteColumns(this.params);
2410 this.error('Unknown CSI code: %s.', ch
);
2419 if (ch
=== '\x1b' || ch
=== '\x07') {
2420 if (ch
=== '\x1b') i
++;
2422 switch (this.prefix
) {
2423 // User-Defined Keys (DECUDK).
2427 // Request Status String (DECRQSS).
2428 // test: echo -e '\eP$q"p\e\\'
2430 var pt
= this.currentParam
2447 + (this.scrollTop
+ 1)
2449 + (this.scrollBottom
+ 1)
2459 this.error('Unknown DCS Pt: %s.', pt
);
2464 this.send('\x1bP' + +valid
+ '$r' + pt
+ '\x1b\\');
2467 // Set Termcap/Terminfo Data (xterm, experimental).
2471 // Request Termcap/Terminfo String (xterm, experimental)
2472 // Regular xterm does not even respond to this sequence.
2473 // This can cause a small glitch in vim.
2474 // test: echo -ne '\eP+q6b64\e\\'
2476 var pt
= this.currentParam
2479 this.send('\x1bP' + +valid
+ '+r' + pt
+ '\x1b\\');
2483 this.error('Unknown DCS prefix: %s.', this.prefix
);
2487 this.currentParam
= 0;
2489 this.state
= normal
;
2490 } else if (!this.currentParam
) {
2491 if (!this.prefix
&& ch
!== '$' && ch
!== '+') {
2492 this.currentParam
= ch
;
2493 } else if (this.prefix
.length
=== 2) {
2494 this.currentParam
= ch
;
2499 this.currentParam
+= ch
;
2505 if (ch
=== '\x1b' || ch
=== '\x07') {
2506 if (ch
=== '\x1b') i
++;
2507 this.state
= normal
;
2513 this.updateRange(this.y
);
2514 this.refresh(this.refreshStart
, this.refreshEnd
);
2517 Terminal
.prototype.writeln = function(data
) {
2518 this.write(data
+ '\r\n');
2522 * Attaches a custom keydown handler which is run before keys are processed, giving consumers of
2523 * xterm.js ultimate control as to what keys should be processed by the terminal and what keys
2525 * @param {function} customKeydownHandler The custom KeyboardEvent handler to attach. This is a
2526 * function that takes a KeyboardEvent, allowing consumers to stop propogation and/or prevent
2527 * the default action. The function returns whether the event should be processed by xterm.js.
2529 Terminal
.prototype.attachCustomKeydownHandler = function(customKeydownHandler
) {
2530 this.customKeydownHandler
= customKeydownHandler
;
2534 // https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
2535 Terminal
.prototype.keyDown = function(ev
) {
2536 if (this.customKeydownHandler
&& !this.customKeydownHandler(ev
)) {
2540 var result
= this.evaluateKeyEscapeSequence(ev
);
2542 if (result
.scrollDisp
) {
2543 this.scrollDisp(result
.scrollDisp
);
2544 return this.cancel(ev
);
2547 if (isThirdLevelShift(this, ev
)) {
2551 if (result
.cancel
) {
2552 // The event is canceled at the end already, is this necessary?
2553 this.cancel(ev
, true);
2560 this.emit('keydown', ev
);
2561 this.emit('key', result
.key
, ev
);
2563 this.handler(result
.key
);
2565 return this.cancel(ev
, true);
2569 * Returns an object that determines how a KeyboardEvent should be handled. The key of the
2570 * returned value is the new key code to pass to the PTY.
2572 * Reference: http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
2574 Terminal
.prototype.evaluateKeyEscapeSequence = function(ev
) {
2576 // Whether to cancel event propogation (NOTE: this may not be needed since the event is
2577 // canceled at the end of keyDown
2579 // The new key even to emit
2581 // The number of characters to scroll, if this is defined it will cancel the event
2582 scrollDisp
: undefined
2584 switch (ev
.keyCode
) {
2588 result
.key
= '\x08'; // ^H
2591 result
.key
= '\x7f'; // ^?
2596 result
.key
= '\x1b[Z';
2600 result
.cancel
= true;
2605 result
.cancel
= true;
2609 result
.key
= '\x1b';
2610 result
.cancel
= true;
2615 result
.key
= '\x1bb' // Jump a word back
2616 result
.cancel
= true;
2620 result
.key
= '\x1b[5D'; // Jump a word back
2623 if (this.applicationCursor
) {
2624 result
.key
= '\x1bOD'; // SS3 as ^[O for 7-bit
2627 result
.key
= '\x1b[D';
2632 result
.key
= '\x1bf' // Jump a word forward
2633 result
.cancel
= true;
2637 result
.key
= '\x1b[5C'; // Jump a word forward
2640 if (this.applicationCursor
) {
2641 result
.key
= '\x1bOC';
2644 result
.key
= '\x1b[C';
2648 if (this.applicationCursor
) {
2649 result
.key
= '\x1bOA';
2653 result
.scrollDisp
= -1;
2655 result
.key
= '\x1b[A';
2660 if (this.applicationCursor
) {
2661 result
.key
= '\x1bOB';
2665 result
.scrollDisp
= 1;
2667 result
.key
= '\x1b[B';
2672 if (!ev
.shiftKey
&& !ev
.ctrlKey
) {
2673 // <Ctrl> or <Shift> + <Insert> are used to
2674 // copy-paste on some systems.
2675 result
.key
= '\x1b[2~';
2679 case 46: result
.key
= '\x1b[3~'; break;
2682 if (this.applicationKeypad
) {
2683 result
.key
= '\x1bOH';
2686 result
.key
= '\x1bOH';
2690 if (this.applicationKeypad
) {
2691 result
.key
= '\x1bOF';
2694 result
.key
= '\x1bOF';
2699 result
.scrollDisp
= -(this.rows
- 1);
2701 result
.key
= '\x1b[5~';
2707 result
.scrollDisp
= this.rows
- 1;
2709 result
.key
= '\x1b[6~';
2713 case 112: result
.key
= '\x1bOP'; break;
2714 case 113: result
.key
= '\x1bOQ'; break;
2715 case 114: result
.key
= '\x1bOR'; break;
2716 case 115: result
.key
= '\x1bOS'; break;
2717 case 116: result
.key
= '\x1b[15~'; break;
2718 case 117: result
.key
= '\x1b[17~'; break;
2719 case 118: result
.key
= '\x1b[18~'; break;
2720 case 119: result
.key
= '\x1b[19~'; break;
2721 case 120: result
.key
= '\x1b[20~'; break;
2722 case 121: result
.key
= '\x1b[21~'; break;
2723 case 122: result
.key
= '\x1b[23~'; break;
2724 case 123: result
.key
= '\x1b[24~'; break;
2727 if (ev
.ctrlKey
&& !ev
.shiftKey
&& !ev
.altKey
&& !ev
.metaKey
) {
2728 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2729 result
.key
= String
.fromCharCode(ev
.keyCode
- 64);
2730 } else if (ev
.keyCode
=== 32) {
2732 result
.key
= String
.fromCharCode(0);
2733 } else if (ev
.keyCode
>= 51 && ev
.keyCode
<= 55) {
2734 // escape, file sep, group sep, record sep, unit sep
2735 result
.key
= String
.fromCharCode(ev
.keyCode
- 51 + 27);
2736 } else if (ev
.keyCode
=== 56) {
2738 result
.key
= String
.fromCharCode(127);
2739 } else if (ev
.keyCode
=== 219) {
2741 result
.key
= String
.fromCharCode(27);
2742 } else if (ev
.keyCode
=== 221) {
2744 result
.key
= String
.fromCharCode(29);
2746 } else if (!this.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) {
2747 // On Mac this is a third level shift. Use <Esc> instead.
2748 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2749 result
.key
= '\x1b' + String
.fromCharCode(ev
.keyCode
+ 32);
2750 } else if (ev
.keyCode
=== 192) {
2751 result
.key
= '\x1b`';
2752 } else if (ev
.keyCode
>= 48 && ev
.keyCode
<= 57) {
2753 result
.key
= '\x1b' + (ev
.keyCode
- 48);
2761 Terminal
.prototype.setgLevel = function(g
) {
2763 this.charset
= this.charsets
[g
];
2766 Terminal
.prototype.setgCharset = function(g
, charset
) {
2767 this.charsets
[g
] = charset
;
2768 if (this.glevel
=== g
) {
2769 this.charset
= charset
;
2773 Terminal
.prototype.keyPress = function(ev
) {
2778 } else if (ev
.which
== null) {
2780 } else if (ev
.which
!== 0 && ev
.charCode
!== 0) {
2787 (ev
.altKey
|| ev
.ctrlKey
|| ev
.metaKey
) && !isThirdLevelShift(this, ev
)
2792 key
= String
.fromCharCode(key
);
2795 * When a key is pressed and a character is sent to the terminal, then clear any text
2796 * selected in the terminal.
2799 this.clearSelection();
2802 this.emit('keypress', key
, ev
);
2803 this.emit('key', key
, ev
);
2807 this.cancel(ev
, true);
2812 Terminal
.prototype.send = function(data
) {
2816 setTimeout(function() {
2817 self
.handler(self
.queue
);
2825 Terminal
.prototype.bell = function() {
2826 if (!this.visualBell
) return;
2828 this.element
.style
.borderColor
= 'white';
2829 setTimeout(function() {
2830 self
.element
.style
.borderColor
= '';
2832 if (this.popOnBell
) this.focus();
2835 Terminal
.prototype.log = function() {
2836 if (!this.debug
) return;
2837 if (!this.context
.console
|| !this.context
.console
.log
) return;
2838 var args
= Array
.prototype.slice
.call(arguments
);
2839 this.context
.console
.log
.apply(this.context
.console
, args
);
2842 Terminal
.prototype.error = function() {
2843 if (!this.debug
) return;
2844 if (!this.context
.console
|| !this.context
.console
.error
) return;
2845 var args
= Array
.prototype.slice
.call(arguments
);
2846 this.context
.console
.error
.apply(this.context
.console
, args
);
2850 * Resizes the terminal.
2852 * @param {number} x The number of columns to resize to.
2853 * @param {number} y The number of rows to resize to.
2857 Terminal
.prototype.resize = function(x
, y
) {
2865 if (x
=== this.cols
&& y
=== this.rows
) {
2875 ch
= [this.defAttr
, ' ', 1]; // does xterm use the default attr?
2876 i
= this.lines
.length
;
2878 while (this.lines
[i
].length
< x
) {
2879 this.lines
[i
].push(ch
);
2883 i
= this.lines
.length
;
2885 while (this.lines
[i
].length
> x
) {
2886 this.lines
[i
].pop();
2899 // y is rows, not this.y
2900 if (this.lines
.length
< y
+ this.ybase
) {
2901 if (this.ybase
> 0 && this.lines
.length
<= this.ybase
+ this.y
+ addToY
+ 1) {
2902 // There is room above the buffer and there are no empty elements below the line,
2906 if (this.ydisp
> 0) {
2907 // Viewport is at the top of the buffer, must increase downwards
2911 // Add a blank line if there is no buffer left at the top to scroll to, or if there
2912 // are blank lines after the cursor
2913 this.lines
.push(this.blankLine());
2916 if (this.children
.length
< y
) {
2922 if (this.lines
.length
> y
+ this.ybase
) {
2923 if (this.lines
.length
> this.ybase
+ this.y
+ 1) {
2924 // The line is a blank line below the cursor, remove it
2927 // The line is the cursor, scroll down
2932 if (this.children
.length
> y
) {
2933 el
= this.children
.shift();
2935 el
.parentNode
.removeChild(el
);
2942 * Make sure that the cursor stays on screen
2956 this.scrollBottom
= y
- 1;
2958 this.refresh(0, this.rows
- 1);
2962 this.emit('resize', {terminal
: this, cols
: x
, rows
: y
});
2965 Terminal
.prototype.updateRange = function(y
) {
2966 if (y
< this.refreshStart
) this.refreshStart
= y
;
2967 if (y
> this.refreshEnd
) this.refreshEnd
= y
;
2968 // if (y > this.refreshEnd) {
2969 // this.refreshEnd = y;
2970 // if (y > this.rows - 1) {
2971 // this.refreshEnd = this.rows - 1;
2976 Terminal
.prototype.maxRange = function() {
2977 this.refreshStart
= 0;
2978 this.refreshEnd
= this.rows
- 1;
2981 Terminal
.prototype.setupStops = function(i
) {
2983 if (!this.tabs
[i
]) {
2984 i
= this.prevStop(i
);
2991 for (; i
< this.cols
; i
+= 8) {
2992 this.tabs
[i
] = true;
2996 Terminal
.prototype.prevStop = function(x
) {
2997 if (x
== null) x
= this.x
;
2998 while (!this.tabs
[--x
] && x
> 0);
2999 return x
>= this.cols
3004 Terminal
.prototype.nextStop = function(x
) {
3005 if (x
== null) x
= this.x
;
3006 while (!this.tabs
[++x
] && x
< this.cols
);
3007 return x
>= this.cols
3012 Terminal
.prototype.eraseRight = function(x
, y
) {
3013 var line
= this.lines
[this.ybase
+ y
]
3014 , ch
= [this.eraseAttr(), ' ', 1]; // xterm
3017 for (; x
< this.cols
; x
++) {
3021 this.updateRange(y
);
3024 Terminal
.prototype.eraseLeft = function(x
, y
) {
3025 var line
= this.lines
[this.ybase
+ y
]
3026 , ch
= [this.eraseAttr(), ' ', 1]; // xterm
3029 while (x
--) line
[x
] = ch
;
3031 this.updateRange(y
);
3034 Terminal
.prototype.eraseLine = function(y
) {
3035 this.eraseRight(0, y
);
3038 Terminal
.prototype.blankLine = function(cur
) {
3043 var ch
= [attr
, ' ', 1] // width defaults to 1 halfwidth character
3047 for (; i
< this.cols
; i
++) {
3054 Terminal
.prototype.ch = function(cur
) {
3056 ? [this.eraseAttr(), ' ', 1]
3057 : [this.defAttr
, ' ', 1];
3060 Terminal
.prototype.is = function(term
) {
3061 var name
= this.termName
;
3062 return (name
+ '').indexOf(term
) === 0;
3065 Terminal
.prototype.handler = function(data
) {
3066 this.emit('data', data
);
3069 Terminal
.prototype.handleTitle = function(title
) {
3070 this.emit('title', title
);
3077 // ESC D Index (IND is 0x84).
3078 Terminal
.prototype.index = function() {
3080 if (this.y
> this.scrollBottom
) {
3084 this.state
= normal
;
3087 // ESC M Reverse Index (RI is 0x8d).
3088 Terminal
.prototype.reverseIndex = function() {
3091 if (this.y
< this.scrollTop
) {
3093 // possibly move the code below to term.reverseScroll();
3094 // test: echo -ne '\e[1;1H\e[44m\eM\e[0m'
3095 // blankLine(true) is xterm/linux behavior
3096 this.lines
.splice(this.y
+ this.ybase
, 0, this.blankLine(true));
3097 j
= this.rows
- 1 - this.scrollBottom
;
3098 this.lines
.splice(this.rows
- 1 + this.ybase
- j
+ 1, 1);
3100 this.updateRange(this.scrollTop
);
3101 this.updateRange(this.scrollBottom
);
3103 this.state
= normal
;
3106 // ESC c Full Reset (RIS).
3107 Terminal
.prototype.reset = function() {
3108 this.options
.rows
= this.rows
;
3109 this.options
.cols
= this.cols
;
3110 Terminal
.call(this, this.options
);
3111 this.refresh(0, this.rows
- 1);
3114 // ESC H Tab Set (HTS is 0x88).
3115 Terminal
.prototype.tabSet = function() {
3116 this.tabs
[this.x
] = true;
3117 this.state
= normal
;
3125 // Cursor Up Ps Times (default = 1) (CUU).
3126 Terminal
.prototype.cursorUp = function(params
) {
3127 var param
= params
[0];
3128 if (param
< 1) param
= 1;
3130 if (this.y
< 0) this.y
= 0;
3134 // Cursor Down Ps Times (default = 1) (CUD).
3135 Terminal
.prototype.cursorDown = function(params
) {
3136 var param
= params
[0];
3137 if (param
< 1) param
= 1;
3139 if (this.y
>= this.rows
) {
3140 this.y
= this.rows
- 1;
3145 // Cursor Forward Ps Times (default = 1) (CUF).
3146 Terminal
.prototype.cursorForward = function(params
) {
3147 var param
= params
[0];
3148 if (param
< 1) param
= 1;
3150 if (this.x
>= this.cols
) {
3151 this.x
= this.cols
- 1;
3156 // Cursor Backward Ps Times (default = 1) (CUB).
3157 Terminal
.prototype.cursorBackward = function(params
) {
3158 var param
= params
[0];
3159 if (param
< 1) param
= 1;
3161 if (this.x
< 0) this.x
= 0;
3165 // Cursor Position [row;column] (default = [1,1]) (CUP).
3166 Terminal
.prototype.cursorPos = function(params
) {
3169 row
= params
[0] - 1;
3171 if (params
.length
>= 2) {
3172 col
= params
[1] - 1;
3179 } else if (row
>= this.rows
) {
3180 row
= this.rows
- 1;
3185 } else if (col
>= this.cols
) {
3186 col
= this.cols
- 1;
3193 // CSI Ps J Erase in Display (ED).
3194 // Ps = 0 -> Erase Below (default).
3195 // Ps = 1 -> Erase Above.
3196 // Ps = 2 -> Erase All.
3197 // Ps = 3 -> Erase Saved Lines (xterm).
3199 // Erase in Display (DECSED).
3200 // Ps = 0 -> Selective Erase Below (default).
3201 // Ps = 1 -> Selective Erase Above.
3202 // Ps = 2 -> Selective Erase All.
3203 Terminal
.prototype.eraseInDisplay = function(params
) {
3205 switch (params
[0]) {
3207 this.eraseRight(this.x
, this.y
);
3209 for (; j
< this.rows
; j
++) {
3214 this.eraseLeft(this.x
, this.y
);
3222 while (j
--) this.eraseLine(j
);
3230 // CSI Ps K Erase in Line (EL).
3231 // Ps = 0 -> Erase to Right (default).
3232 // Ps = 1 -> Erase to Left.
3233 // Ps = 2 -> Erase All.
3235 // Erase in Line (DECSEL).
3236 // Ps = 0 -> Selective Erase to Right (default).
3237 // Ps = 1 -> Selective Erase to Left.
3238 // Ps = 2 -> Selective Erase All.
3239 Terminal
.prototype.eraseInLine = function(params
) {
3240 switch (params
[0]) {
3242 this.eraseRight(this.x
, this.y
);
3245 this.eraseLeft(this.x
, this.y
);
3248 this.eraseLine(this.y
);
3253 // CSI Pm m Character Attributes (SGR).
3254 // Ps = 0 -> Normal (default).
3256 // Ps = 4 -> Underlined.
3257 // Ps = 5 -> Blink (appears as Bold).
3258 // Ps = 7 -> Inverse.
3259 // Ps = 8 -> Invisible, i.e., hidden (VT300).
3260 // Ps = 2 2 -> Normal (neither bold nor faint).
3261 // Ps = 2 4 -> Not underlined.
3262 // Ps = 2 5 -> Steady (not blinking).
3263 // Ps = 2 7 -> Positive (not inverse).
3264 // Ps = 2 8 -> Visible, i.e., not hidden (VT300).
3265 // Ps = 3 0 -> Set foreground color to Black.
3266 // Ps = 3 1 -> Set foreground color to Red.
3267 // Ps = 3 2 -> Set foreground color to Green.
3268 // Ps = 3 3 -> Set foreground color to Yellow.
3269 // Ps = 3 4 -> Set foreground color to Blue.
3270 // Ps = 3 5 -> Set foreground color to Magenta.
3271 // Ps = 3 6 -> Set foreground color to Cyan.
3272 // Ps = 3 7 -> Set foreground color to White.
3273 // Ps = 3 9 -> Set foreground color to default (original).
3274 // Ps = 4 0 -> Set background color to Black.
3275 // Ps = 4 1 -> Set background color to Red.
3276 // Ps = 4 2 -> Set background color to Green.
3277 // Ps = 4 3 -> Set background color to Yellow.
3278 // Ps = 4 4 -> Set background color to Blue.
3279 // Ps = 4 5 -> Set background color to Magenta.
3280 // Ps = 4 6 -> Set background color to Cyan.
3281 // Ps = 4 7 -> Set background color to White.
3282 // Ps = 4 9 -> Set background color to default (original).
3284 // If 16-color support is compiled, the following apply. Assume
3285 // that xterm's resources are set so that the ISO color codes are
3286 // the first 8 of a set of 16. Then the aixterm colors are the
3287 // bright versions of the ISO colors:
3288 // Ps = 9 0 -> Set foreground color to Black.
3289 // Ps = 9 1 -> Set foreground color to Red.
3290 // Ps = 9 2 -> Set foreground color to Green.
3291 // Ps = 9 3 -> Set foreground color to Yellow.
3292 // Ps = 9 4 -> Set foreground color to Blue.
3293 // Ps = 9 5 -> Set foreground color to Magenta.
3294 // Ps = 9 6 -> Set foreground color to Cyan.
3295 // Ps = 9 7 -> Set foreground color to White.
3296 // Ps = 1 0 0 -> Set background color to Black.
3297 // Ps = 1 0 1 -> Set background color to Red.
3298 // Ps = 1 0 2 -> Set background color to Green.
3299 // Ps = 1 0 3 -> Set background color to Yellow.
3300 // Ps = 1 0 4 -> Set background color to Blue.
3301 // Ps = 1 0 5 -> Set background color to Magenta.
3302 // Ps = 1 0 6 -> Set background color to Cyan.
3303 // Ps = 1 0 7 -> Set background color to White.
3305 // If xterm is compiled with the 16-color support disabled, it
3306 // supports the following, from rxvt:
3307 // Ps = 1 0 0 -> Set foreground and background color to
3310 // If 88- or 256-color support is compiled, the following apply.
3311 // Ps = 3 8 ; 5 ; Ps -> Set foreground color to the second
3313 // Ps = 4 8 ; 5 ; Ps -> Set background color to the second
3315 Terminal
.prototype.charAttributes = function(params
) {
3316 // Optimize a single SGR0.
3317 if (params
.length
=== 1 && params
[0] === 0) {
3318 this.curAttr
= this.defAttr
;
3322 var l
= params
.length
3324 , flags
= this.curAttr
>> 18
3325 , fg
= (this.curAttr
>> 9) & 0x1ff
3326 , bg
= this.curAttr
& 0x1ff
3329 for (; i
< l
; i
++) {
3331 if (p
>= 30 && p
<= 37) {
3334 } else if (p
>= 40 && p
<= 47) {
3337 } else if (p
>= 90 && p
<= 97) {
3341 } else if (p
>= 100 && p
<= 107) {
3345 } else if (p
=== 0) {
3347 flags
= this.defAttr
>> 18;
3348 fg
= (this.defAttr
>> 9) & 0x1ff;
3349 bg
= this.defAttr
& 0x1ff;
3353 } else if (p
=== 1) {
3356 } else if (p
=== 4) {
3359 } else if (p
=== 5) {
3362 } else if (p
=== 7) {
3363 // inverse and positive
3364 // test with: echo -e '\e[31m\e[42mhello\e[7mworld\e[27mhi\e[m'
3366 } else if (p
=== 8) {
3369 } else if (p
=== 22) {
3372 } else if (p
=== 24) {
3375 } else if (p
=== 25) {
3378 } else if (p
=== 27) {
3381 } else if (p
=== 28) {
3384 } else if (p
=== 39) {
3386 fg
= (this.defAttr
>> 9) & 0x1ff;
3387 } else if (p
=== 49) {
3389 bg
= this.defAttr
& 0x1ff;
3390 } else if (p
=== 38) {
3392 if (params
[i
+ 1] === 2) {
3396 params
[i
+ 1] & 0xff,
3397 params
[i
+ 2] & 0xff);
3398 if (fg
=== -1) fg
= 0x1ff;
3400 } else if (params
[i
+ 1] === 5) {
3402 p
= params
[i
] & 0xff;
3405 } else if (p
=== 48) {
3407 if (params
[i
+ 1] === 2) {
3411 params
[i
+ 1] & 0xff,
3412 params
[i
+ 2] & 0xff);
3413 if (bg
=== -1) bg
= 0x1ff;
3415 } else if (params
[i
+ 1] === 5) {
3417 p
= params
[i
] & 0xff;
3420 } else if (p
=== 100) {
3422 fg
= (this.defAttr
>> 9) & 0x1ff;
3423 bg
= this.defAttr
& 0x1ff;
3425 this.error('Unknown SGR attribute: %d.', p
);
3429 this.curAttr
= (flags
<< 18) | (fg
<< 9) | bg
;
3432 // CSI Ps n Device Status Report (DSR).
3433 // Ps = 5 -> Status Report. Result (``OK'') is
3435 // Ps = 6 -> Report Cursor Position (CPR) [row;column].
3439 // Device Status Report (DSR, DEC-specific).
3440 // Ps = 6 -> Report Cursor Position (CPR) [row;column] as CSI
3441 // ? r ; c R (assumes page is zero).
3442 // Ps = 1 5 -> Report Printer status as CSI ? 1 0 n (ready).
3443 // or CSI ? 1 1 n (not ready).
3444 // Ps = 2 5 -> Report UDK status as CSI ? 2 0 n (unlocked)
3445 // or CSI ? 2 1 n (locked).
3446 // Ps = 2 6 -> Report Keyboard status as
3447 // CSI ? 2 7 ; 1 ; 0 ; 0 n (North American).
3448 // The last two parameters apply to VT400 & up, and denote key-
3449 // board ready and LK01 respectively.
3450 // Ps = 5 3 -> Report Locator status as
3451 // CSI ? 5 3 n Locator available, if compiled-in, or
3452 // CSI ? 5 0 n No Locator, if not.
3453 Terminal
.prototype.deviceStatus = function(params
) {
3455 switch (params
[0]) {
3458 this.send('\x1b[0n');
3469 } else if (this.prefix
=== '?') {
3470 // modern xterm doesnt seem to
3471 // respond to any of these except ?6, 6, and 5
3472 switch (params
[0]) {
3483 // this.send('\x1b[?11n');
3486 // dont support user defined keys
3487 // this.send('\x1b[?21n');
3490 // north american keyboard
3491 // this.send('\x1b[?27;1;0;0n');
3494 // no dec locator/mouse
3495 // this.send('\x1b[?50n');
3506 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
3507 Terminal
.prototype.insertChars = function(params
) {
3508 var param
, row
, j
, ch
;
3511 if (param
< 1) param
= 1;
3513 row
= this.y
+ this.ybase
;
3515 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3517 while (param
-- && j
< this.cols
) {
3518 this.lines
[row
].splice(j
++, 0, ch
);
3519 this.lines
[row
].pop();
3524 // Cursor Next Line Ps Times (default = 1) (CNL).
3525 // same as CSI Ps B ?
3526 Terminal
.prototype.cursorNextLine = function(params
) {
3527 var param
= params
[0];
3528 if (param
< 1) param
= 1;
3530 if (this.y
>= this.rows
) {
3531 this.y
= this.rows
- 1;
3537 // Cursor Preceding Line Ps Times (default = 1) (CNL).
3539 Terminal
.prototype.cursorPrecedingLine = function(params
) {
3540 var param
= params
[0];
3541 if (param
< 1) param
= 1;
3543 if (this.y
< 0) this.y
= 0;
3548 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
3549 Terminal
.prototype.cursorCharAbsolute = function(params
) {
3550 var param
= params
[0];
3551 if (param
< 1) param
= 1;
3556 // Insert Ps Line(s) (default = 1) (IL).
3557 Terminal
.prototype.insertLines = function(params
) {
3561 if (param
< 1) param
= 1;
3562 row
= this.y
+ this.ybase
;
3564 j
= this.rows
- 1 - this.scrollBottom
;
3565 j
= this.rows
- 1 + this.ybase
- j
+ 1;
3568 // test: echo -e '\e[44m\e[1L\e[0m'
3569 // blankLine(true) - xterm/linux behavior
3570 this.lines
.splice(row
, 0, this.blankLine(true));
3571 this.lines
.splice(j
, 1);
3575 this.updateRange(this.y
);
3576 this.updateRange(this.scrollBottom
);
3580 // Delete Ps Line(s) (default = 1) (DL).
3581 Terminal
.prototype.deleteLines = function(params
) {
3585 if (param
< 1) param
= 1;
3586 row
= this.y
+ this.ybase
;
3588 j
= this.rows
- 1 - this.scrollBottom
;
3589 j
= this.rows
- 1 + this.ybase
- j
;
3592 // test: echo -e '\e[44m\e[1M\e[0m'
3593 // blankLine(true) - xterm/linux behavior
3594 this.lines
.splice(j
+ 1, 0, this.blankLine(true));
3595 this.lines
.splice(row
, 1);
3599 this.updateRange(this.y
);
3600 this.updateRange(this.scrollBottom
);
3604 // Delete Ps Character(s) (default = 1) (DCH).
3605 Terminal
.prototype.deleteChars = function(params
) {
3609 if (param
< 1) param
= 1;
3611 row
= this.y
+ this.ybase
;
3612 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3615 this.lines
[row
].splice(this.x
, 1);
3616 this.lines
[row
].push(ch
);
3621 // Erase Ps Character(s) (default = 1) (ECH).
3622 Terminal
.prototype.eraseChars = function(params
) {
3623 var param
, row
, j
, ch
;
3626 if (param
< 1) param
= 1;
3628 row
= this.y
+ this.ybase
;
3630 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3632 while (param
-- && j
< this.cols
) {
3633 this.lines
[row
][j
++] = ch
;
3637 // CSI Pm ` Character Position Absolute
3638 // [column] (default = [row,1]) (HPA).
3639 Terminal
.prototype.charPosAbsolute = function(params
) {
3640 var param
= params
[0];
3641 if (param
< 1) param
= 1;
3643 if (this.x
>= this.cols
) {
3644 this.x
= this.cols
- 1;
3649 // Horizontal Position Relative
3651 Terminal
.prototype.HPositionRelative = function(params
) {
3652 var param
= params
[0];
3653 if (param
< 1) param
= 1;
3655 if (this.x
>= this.cols
) {
3656 this.x
= this.cols
- 1;
3660 // CSI Ps c Send Device Attributes (Primary DA).
3661 // Ps = 0 or omitted -> request attributes from terminal. The
3662 // response depends on the decTerminalID resource setting.
3663 // -> CSI ? 1 ; 2 c (``VT100 with Advanced Video Option'')
3664 // -> CSI ? 1 ; 0 c (``VT101 with No Options'')
3665 // -> CSI ? 6 c (``VT102'')
3666 // -> CSI ? 6 0 ; 1 ; 2 ; 6 ; 8 ; 9 ; 1 5 ; c (``VT220'')
3667 // The VT100-style response parameters do not mean anything by
3668 // themselves. VT220 parameters do, telling the host what fea-
3669 // tures the terminal supports:
3670 // Ps = 1 -> 132-columns.
3671 // Ps = 2 -> Printer.
3672 // Ps = 6 -> Selective erase.
3673 // Ps = 8 -> User-defined keys.
3674 // Ps = 9 -> National replacement character sets.
3675 // Ps = 1 5 -> Technical characters.
3676 // Ps = 2 2 -> ANSI color, e.g., VT525.
3677 // Ps = 2 9 -> ANSI text locator (i.e., DEC Locator mode).
3679 // Send Device Attributes (Secondary DA).
3680 // Ps = 0 or omitted -> request the terminal's identification
3681 // code. The response depends on the decTerminalID resource set-
3682 // ting. It should apply only to VT220 and up, but xterm extends
3684 // -> CSI > Pp ; Pv ; Pc c
3685 // where Pp denotes the terminal type
3686 // Pp = 0 -> ``VT100''.
3687 // Pp = 1 -> ``VT220''.
3688 // and Pv is the firmware version (for xterm, this was originally
3689 // the XFree86 patch number, starting with 95). In a DEC termi-
3690 // nal, Pc indicates the ROM cartridge registration number and is
3692 // More information:
3693 // xterm/charproc.c - line 2012, for more information.
3694 // vim responds with ^[[?0c or ^[[?1c after the terminal's response (?)
3695 Terminal
.prototype.sendDeviceAttributes = function(params
) {
3696 if (params
[0] > 0) return;
3699 if (this.is('xterm')
3700 || this.is('rxvt-unicode')
3701 || this.is('screen')) {
3702 this.send('\x1b[?1;2c');
3703 } else if (this.is('linux')) {
3704 this.send('\x1b[?6c');
3706 } else if (this.prefix
=== '>') {
3708 // seem to spit this
3709 // out around ~370 times (?).
3710 if (this.is('xterm')) {
3711 this.send('\x1b[>0;276;0c');
3712 } else if (this.is('rxvt-unicode')) {
3713 this.send('\x1b[>85;95;0c');
3714 } else if (this.is('linux')) {
3715 // not supported by linux console.
3716 // linux console echoes parameters.
3717 this.send(params
[0] + 'c');
3718 } else if (this.is('screen')) {
3719 this.send('\x1b[>83;40003;0c');
3725 // Line Position Absolute [row] (default = [1,column]) (VPA).
3726 Terminal
.prototype.linePosAbsolute = function(params
) {
3727 var param
= params
[0];
3728 if (param
< 1) param
= 1;
3730 if (this.y
>= this.rows
) {
3731 this.y
= this.rows
- 1;
3735 // 145 65 e * VPR - Vertical Position Relative
3737 Terminal
.prototype.VPositionRelative = function(params
) {
3738 var param
= params
[0];
3739 if (param
< 1) param
= 1;
3741 if (this.y
>= this.rows
) {
3742 this.y
= this.rows
- 1;
3747 // Horizontal and Vertical Position [row;column] (default =
3749 Terminal
.prototype.HVPosition = function(params
) {
3750 if (params
[0] < 1) params
[0] = 1;
3751 if (params
[1] < 1) params
[1] = 1;
3753 this.y
= params
[0] - 1;
3754 if (this.y
>= this.rows
) {
3755 this.y
= this.rows
- 1;
3758 this.x
= params
[1] - 1;
3759 if (this.x
>= this.cols
) {
3760 this.x
= this.cols
- 1;
3764 // CSI Pm h Set Mode (SM).
3765 // Ps = 2 -> Keyboard Action Mode (AM).
3766 // Ps = 4 -> Insert Mode (IRM).
3767 // Ps = 1 2 -> Send/receive (SRM).
3768 // Ps = 2 0 -> Automatic Newline (LNM).
3770 // DEC Private Mode Set (DECSET).
3771 // Ps = 1 -> Application Cursor Keys (DECCKM).
3772 // Ps = 2 -> Designate USASCII for character sets G0-G3
3773 // (DECANM), and set VT100 mode.
3774 // Ps = 3 -> 132 Column Mode (DECCOLM).
3775 // Ps = 4 -> Smooth (Slow) Scroll (DECSCLM).
3776 // Ps = 5 -> Reverse Video (DECSCNM).
3777 // Ps = 6 -> Origin Mode (DECOM).
3778 // Ps = 7 -> Wraparound Mode (DECAWM).
3779 // Ps = 8 -> Auto-repeat Keys (DECARM).
3780 // Ps = 9 -> Send Mouse X & Y on button press. See the sec-
3781 // tion Mouse Tracking.
3782 // Ps = 1 0 -> Show toolbar (rxvt).
3783 // Ps = 1 2 -> Start Blinking Cursor (att610).
3784 // Ps = 1 8 -> Print form feed (DECPFF).
3785 // Ps = 1 9 -> Set print extent to full screen (DECPEX).
3786 // Ps = 2 5 -> Show Cursor (DECTCEM).
3787 // Ps = 3 0 -> Show scrollbar (rxvt).
3788 // Ps = 3 5 -> Enable font-shifting functions (rxvt).
3789 // Ps = 3 8 -> Enter Tektronix Mode (DECTEK).
3790 // Ps = 4 0 -> Allow 80 -> 132 Mode.
3791 // Ps = 4 1 -> more(1) fix (see curses resource).
3792 // Ps = 4 2 -> Enable Nation Replacement Character sets (DECN-
3794 // Ps = 4 4 -> Turn On Margin Bell.
3795 // Ps = 4 5 -> Reverse-wraparound Mode.
3796 // Ps = 4 6 -> Start Logging. This is normally disabled by a
3797 // compile-time option.
3798 // Ps = 4 7 -> Use Alternate Screen Buffer. (This may be dis-
3799 // abled by the titeInhibit resource).
3800 // Ps = 6 6 -> Application keypad (DECNKM).
3801 // Ps = 6 7 -> Backarrow key sends backspace (DECBKM).
3802 // Ps = 1 0 0 0 -> Send Mouse X & Y on button press and
3803 // release. See the section Mouse Tracking.
3804 // Ps = 1 0 0 1 -> Use Hilite Mouse Tracking.
3805 // Ps = 1 0 0 2 -> Use Cell Motion Mouse Tracking.
3806 // Ps = 1 0 0 3 -> Use All Motion Mouse Tracking.
3807 // Ps = 1 0 0 4 -> Send FocusIn/FocusOut events.
3808 // Ps = 1 0 0 5 -> Enable Extended Mouse Mode.
3809 // Ps = 1 0 1 0 -> Scroll to bottom on tty output (rxvt).
3810 // Ps = 1 0 1 1 -> Scroll to bottom on key press (rxvt).
3811 // Ps = 1 0 3 4 -> Interpret "meta" key, sets eighth bit.
3812 // (enables the eightBitInput resource).
3813 // Ps = 1 0 3 5 -> Enable special modifiers for Alt and Num-
3814 // Lock keys. (This enables the numLock resource).
3815 // Ps = 1 0 3 6 -> Send ESC when Meta modifies a key. (This
3816 // enables the metaSendsEscape resource).
3817 // Ps = 1 0 3 7 -> Send DEL from the editing-keypad Delete
3819 // Ps = 1 0 3 9 -> Send ESC when Alt modifies a key. (This
3820 // enables the altSendsEscape resource).
3821 // Ps = 1 0 4 0 -> Keep selection even if not highlighted.
3822 // (This enables the keepSelection resource).
3823 // Ps = 1 0 4 1 -> Use the CLIPBOARD selection. (This enables
3824 // the selectToClipboard resource).
3825 // Ps = 1 0 4 2 -> Enable Urgency window manager hint when
3826 // Control-G is received. (This enables the bellIsUrgent
3828 // Ps = 1 0 4 3 -> Enable raising of the window when Control-G
3829 // is received. (enables the popOnBell resource).
3830 // Ps = 1 0 4 7 -> Use Alternate Screen Buffer. (This may be
3831 // disabled by the titeInhibit resource).
3832 // Ps = 1 0 4 8 -> Save cursor as in DECSC. (This may be dis-
3833 // abled by the titeInhibit resource).
3834 // Ps = 1 0 4 9 -> Save cursor as in DECSC and use Alternate
3835 // Screen Buffer, clearing it first. (This may be disabled by
3836 // the titeInhibit resource). This combines the effects of the 1
3837 // 0 4 7 and 1 0 4 8 modes. Use this with terminfo-based
3838 // applications rather than the 4 7 mode.
3839 // Ps = 1 0 5 0 -> Set terminfo/termcap function-key mode.
3840 // Ps = 1 0 5 1 -> Set Sun function-key mode.
3841 // Ps = 1 0 5 2 -> Set HP function-key mode.
3842 // Ps = 1 0 5 3 -> Set SCO function-key mode.
3843 // Ps = 1 0 6 0 -> Set legacy keyboard emulation (X11R6).
3844 // Ps = 1 0 6 1 -> Set VT220 keyboard emulation.
3845 // Ps = 2 0 0 4 -> Set bracketed paste mode.
3847 // http://vt100.net/docs/vt220-rm/chapter4.html
3848 Terminal
.prototype.setMode = function(params
) {
3849 if (typeof params
=== 'object') {
3850 var l
= params
.length
3853 for (; i
< l
; i
++) {
3854 this.setMode(params
[i
]);
3863 this.insertMode
= true;
3866 //this.convertEol = true;
3869 } else if (this.prefix
=== '?') {
3872 this.applicationCursor
= true;
3875 this.setgCharset(0, Terminal
.charsets
.US
);
3876 this.setgCharset(1, Terminal
.charsets
.US
);
3877 this.setgCharset(2, Terminal
.charsets
.US
);
3878 this.setgCharset(3, Terminal
.charsets
.US
);
3879 // set VT100 mode here
3881 case 3: // 132 col mode
3882 this.savedCols
= this.cols
;
3883 this.resize(132, this.rows
);
3886 this.originMode
= true;
3889 this.wraparoundMode
= true;
3892 // this.cursorBlink = true;
3895 this.log('Serial port requested application keypad.');
3896 this.applicationKeypad
= true;
3898 case 9: // X10 Mouse
3899 // no release, no motion, no wheel, no modifiers.
3900 case 1000: // vt200 mouse
3902 // no modifiers, except control on the wheel.
3903 case 1002: // button event mouse
3904 case 1003: // any event mouse
3905 // any event - sends motion events,
3906 // even if there is no button held down.
3907 this.x10Mouse
= params
=== 9;
3908 this.vt200Mouse
= params
=== 1000;
3909 this.normalMouse
= params
> 1000;
3910 this.mouseEvents
= true;
3911 this.element
.style
.cursor
= 'default';
3912 this.log('Binding to mouse events.');
3914 case 1004: // send focusin/focusout events
3917 this.sendFocus
= true;
3919 case 1005: // utf8 ext mode mouse
3920 this.utfMouse
= true;
3921 // for wide terminals
3922 // simply encodes large values as utf8 characters
3924 case 1006: // sgr ext mode mouse
3925 this.sgrMouse
= true;
3926 // for wide terminals
3927 // does not add 32 to fields
3928 // press: ^[[<b;x;yM
3929 // release: ^[[<b;x;ym
3931 case 1015: // urxvt ext mode mouse
3932 this.urxvtMouse
= true;
3933 // for wide terminals
3934 // numbers for fields
3936 // motion: ^[[b;x;yT
3938 case 25: // show cursor
3939 this.cursorHidden
= false;
3941 case 1049: // alt screen buffer cursor
3942 //this.saveCursor();
3944 case 47: // alt screen buffer
3945 case 1047: // alt screen buffer
3953 scrollTop
: this.scrollTop
,
3954 scrollBottom
: this.scrollBottom
,
3956 // XXX save charset(s) here?
3957 // charset: this.charset,
3958 // glevel: this.glevel,
3959 // charsets: this.charsets
3962 this.normal
= normal
;
3970 // CSI Pm l Reset Mode (RM).
3971 // Ps = 2 -> Keyboard Action Mode (AM).
3972 // Ps = 4 -> Replace Mode (IRM).
3973 // Ps = 1 2 -> Send/receive (SRM).
3974 // Ps = 2 0 -> Normal Linefeed (LNM).
3976 // DEC Private Mode Reset (DECRST).
3977 // Ps = 1 -> Normal Cursor Keys (DECCKM).
3978 // Ps = 2 -> Designate VT52 mode (DECANM).
3979 // Ps = 3 -> 80 Column Mode (DECCOLM).
3980 // Ps = 4 -> Jump (Fast) Scroll (DECSCLM).
3981 // Ps = 5 -> Normal Video (DECSCNM).
3982 // Ps = 6 -> Normal Cursor Mode (DECOM).
3983 // Ps = 7 -> No Wraparound Mode (DECAWM).
3984 // Ps = 8 -> No Auto-repeat Keys (DECARM).
3985 // Ps = 9 -> Don't send Mouse X & Y on button press.
3986 // Ps = 1 0 -> Hide toolbar (rxvt).
3987 // Ps = 1 2 -> Stop Blinking Cursor (att610).
3988 // Ps = 1 8 -> Don't print form feed (DECPFF).
3989 // Ps = 1 9 -> Limit print to scrolling region (DECPEX).
3990 // Ps = 2 5 -> Hide Cursor (DECTCEM).
3991 // Ps = 3 0 -> Don't show scrollbar (rxvt).
3992 // Ps = 3 5 -> Disable font-shifting functions (rxvt).
3993 // Ps = 4 0 -> Disallow 80 -> 132 Mode.
3994 // Ps = 4 1 -> No more(1) fix (see curses resource).
3995 // Ps = 4 2 -> Disable Nation Replacement Character sets (DEC-
3997 // Ps = 4 4 -> Turn Off Margin Bell.
3998 // Ps = 4 5 -> No Reverse-wraparound Mode.
3999 // Ps = 4 6 -> Stop Logging. (This is normally disabled by a
4000 // compile-time option).
4001 // Ps = 4 7 -> Use Normal Screen Buffer.
4002 // Ps = 6 6 -> Numeric keypad (DECNKM).
4003 // Ps = 6 7 -> Backarrow key sends delete (DECBKM).
4004 // Ps = 1 0 0 0 -> Don't send Mouse X & Y on button press and
4005 // release. See the section Mouse Tracking.
4006 // Ps = 1 0 0 1 -> Don't use Hilite Mouse Tracking.
4007 // Ps = 1 0 0 2 -> Don't use Cell Motion Mouse Tracking.
4008 // Ps = 1 0 0 3 -> Don't use All Motion Mouse Tracking.
4009 // Ps = 1 0 0 4 -> Don't send FocusIn/FocusOut events.
4010 // Ps = 1 0 0 5 -> Disable Extended Mouse Mode.
4011 // Ps = 1 0 1 0 -> Don't scroll to bottom on tty output
4013 // Ps = 1 0 1 1 -> Don't scroll to bottom on key press (rxvt).
4014 // Ps = 1 0 3 4 -> Don't interpret "meta" key. (This disables
4015 // the eightBitInput resource).
4016 // Ps = 1 0 3 5 -> Disable special modifiers for Alt and Num-
4017 // Lock keys. (This disables the numLock resource).
4018 // Ps = 1 0 3 6 -> Don't send ESC when Meta modifies a key.
4019 // (This disables the metaSendsEscape resource).
4020 // Ps = 1 0 3 7 -> Send VT220 Remove from the editing-keypad
4022 // Ps = 1 0 3 9 -> Don't send ESC when Alt modifies a key.
4023 // (This disables the altSendsEscape resource).
4024 // Ps = 1 0 4 0 -> Do not keep selection when not highlighted.
4025 // (This disables the keepSelection resource).
4026 // Ps = 1 0 4 1 -> Use the PRIMARY selection. (This disables
4027 // the selectToClipboard resource).
4028 // Ps = 1 0 4 2 -> Disable Urgency window manager hint when
4029 // Control-G is received. (This disables the bellIsUrgent
4031 // Ps = 1 0 4 3 -> Disable raising of the window when Control-
4032 // G is received. (This disables the popOnBell resource).
4033 // Ps = 1 0 4 7 -> Use Normal Screen Buffer, clearing screen
4034 // first if in the Alternate Screen. (This may be disabled by
4035 // the titeInhibit resource).
4036 // Ps = 1 0 4 8 -> Restore cursor as in DECRC. (This may be
4037 // disabled by the titeInhibit resource).
4038 // Ps = 1 0 4 9 -> Use Normal Screen Buffer and restore cursor
4039 // as in DECRC. (This may be disabled by the titeInhibit
4040 // resource). This combines the effects of the 1 0 4 7 and 1 0
4041 // 4 8 modes. Use this with terminfo-based applications rather
4042 // than the 4 7 mode.
4043 // Ps = 1 0 5 0 -> Reset terminfo/termcap function-key mode.
4044 // Ps = 1 0 5 1 -> Reset Sun function-key mode.
4045 // Ps = 1 0 5 2 -> Reset HP function-key mode.
4046 // Ps = 1 0 5 3 -> Reset SCO function-key mode.
4047 // Ps = 1 0 6 0 -> Reset legacy keyboard emulation (X11R6).
4048 // Ps = 1 0 6 1 -> Reset keyboard emulation to Sun/PC style.
4049 // Ps = 2 0 0 4 -> Reset bracketed paste mode.
4050 Terminal
.prototype.resetMode = function(params
) {
4051 if (typeof params
=== 'object') {
4052 var l
= params
.length
4055 for (; i
< l
; i
++) {
4056 this.resetMode(params
[i
]);
4065 this.insertMode
= false;
4068 //this.convertEol = false;
4071 } else if (this.prefix
=== '?') {
4074 this.applicationCursor
= false;
4077 if (this.cols
=== 132 && this.savedCols
) {
4078 this.resize(this.savedCols
, this.rows
);
4080 delete this.savedCols
;
4083 this.originMode
= false;
4086 this.wraparoundMode
= false;
4089 // this.cursorBlink = false;
4092 this.log('Switching back to normal keypad.');
4093 this.applicationKeypad
= false;
4095 case 9: // X10 Mouse
4096 case 1000: // vt200 mouse
4097 case 1002: // button event mouse
4098 case 1003: // any event mouse
4099 this.x10Mouse
= false;
4100 this.vt200Mouse
= false;
4101 this.normalMouse
= false;
4102 this.mouseEvents
= false;
4103 this.element
.style
.cursor
= '';
4105 case 1004: // send focusin/focusout events
4106 this.sendFocus
= false;
4108 case 1005: // utf8 ext mode mouse
4109 this.utfMouse
= false;
4111 case 1006: // sgr ext mode mouse
4112 this.sgrMouse
= false;
4114 case 1015: // urxvt ext mode mouse
4115 this.urxvtMouse
= false;
4117 case 25: // hide cursor
4118 this.cursorHidden
= true;
4120 case 1049: // alt screen buffer cursor
4122 case 47: // normal screen buffer
4123 case 1047: // normal screen buffer - clearing it first
4125 this.lines
= this.normal
.lines
;
4126 this.ybase
= this.normal
.ybase
;
4127 this.ydisp
= this.normal
.ydisp
;
4128 this.x
= this.normal
.x
;
4129 this.y
= this.normal
.y
;
4130 this.scrollTop
= this.normal
.scrollTop
;
4131 this.scrollBottom
= this.normal
.scrollBottom
;
4132 this.tabs
= this.normal
.tabs
;
4134 // if (params === 1049) {
4135 // this.x = this.savedX;
4136 // this.y = this.savedY;
4138 this.refresh(0, this.rows
- 1);
4147 // Set Scrolling Region [top;bottom] (default = full size of win-
4150 Terminal
.prototype.setScrollRegion = function(params
) {
4151 if (this.prefix
) return;
4152 this.scrollTop
= (params
[0] || 1) - 1;
4153 this.scrollBottom
= (params
[1] || this.rows
) - 1;
4159 // Save cursor (ANSI.SYS).
4160 Terminal
.prototype.saveCursor = function(params
) {
4161 this.savedX
= this.x
;
4162 this.savedY
= this.y
;
4166 // Restore cursor (ANSI.SYS).
4167 Terminal
.prototype.restoreCursor = function(params
) {
4168 this.x
= this.savedX
|| 0;
4169 this.y
= this.savedY
|| 0;
4177 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
4178 Terminal
.prototype.cursorForwardTab = function(params
) {
4179 var param
= params
[0] || 1;
4181 this.x
= this.nextStop();
4185 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
4186 Terminal
.prototype.scrollUp = function(params
) {
4187 var param
= params
[0] || 1;
4189 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
4190 this.lines
.splice(this.ybase
+ this.scrollBottom
, 0, this.blankLine());
4193 this.updateRange(this.scrollTop
);
4194 this.updateRange(this.scrollBottom
);
4197 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
4198 Terminal
.prototype.scrollDown = function(params
) {
4199 var param
= params
[0] || 1;
4201 this.lines
.splice(this.ybase
+ this.scrollBottom
, 1);
4202 this.lines
.splice(this.ybase
+ this.scrollTop
, 0, this.blankLine());
4205 this.updateRange(this.scrollTop
);
4206 this.updateRange(this.scrollBottom
);
4209 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
4210 // Initiate highlight mouse tracking. Parameters are
4211 // [func;startx;starty;firstrow;lastrow]. See the section Mouse
4213 Terminal
.prototype.initMouseTracking = function(params
) {
4214 // Relevant: DECSET 1001
4218 // Reset one or more features of the title modes to the default
4219 // value. Normally, "reset" disables the feature. It is possi-
4220 // ble to disable the ability to reset features by compiling a
4221 // different default for the title modes into xterm.
4222 // Ps = 0 -> Do not set window/icon labels using hexadecimal.
4223 // Ps = 1 -> Do not query window/icon labels using hexadeci-
4225 // Ps = 2 -> Do not set window/icon labels using UTF-8.
4226 // Ps = 3 -> Do not query window/icon labels using UTF-8.
4227 // (See discussion of "Title Modes").
4228 Terminal
.prototype.resetTitleModes = function(params
) {
4232 // CSI Ps Z Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
4233 Terminal
.prototype.cursorBackwardTab = function(params
) {
4234 var param
= params
[0] || 1;
4236 this.x
= this.prevStop();
4240 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
4241 Terminal
.prototype.repeatPrecedingCharacter = function(params
) {
4242 var param
= params
[0] || 1
4243 , line
= this.lines
[this.ybase
+ this.y
]
4244 , ch
= line
[this.x
- 1] || [this.defAttr
, ' ', 1];
4246 while (param
--) line
[this.x
++] = ch
;
4249 // CSI Ps g Tab Clear (TBC).
4250 // Ps = 0 -> Clear Current Column (default).
4251 // Ps = 3 -> Clear All.
4253 // Ps = 2 -> Clear Stops on Line.
4254 // http://vt100.net/annarbor/aaa-ug/section6.html
4255 Terminal
.prototype.tabClear = function(params
) {
4256 var param
= params
[0];
4258 delete this.tabs
[this.x
];
4259 } else if (param
=== 3) {
4264 // CSI Pm i Media Copy (MC).
4265 // Ps = 0 -> Print screen (default).
4266 // Ps = 4 -> Turn off printer controller mode.
4267 // Ps = 5 -> Turn on printer controller mode.
4269 // Media Copy (MC, DEC-specific).
4270 // Ps = 1 -> Print line containing cursor.
4271 // Ps = 4 -> Turn off autoprint mode.
4272 // Ps = 5 -> Turn on autoprint mode.
4273 // Ps = 1 0 -> Print composed display, ignores DECPEX.
4274 // Ps = 1 1 -> Print all pages.
4275 Terminal
.prototype.mediaCopy = function(params
) {
4280 // Set or reset resource-values used by xterm to decide whether
4281 // to construct escape sequences holding information about the
4282 // modifiers pressed with a given key. The first parameter iden-
4283 // tifies the resource to set/reset. The second parameter is the
4284 // value to assign to the resource. If the second parameter is
4285 // omitted, the resource is reset to its initial value.
4286 // Ps = 1 -> modifyCursorKeys.
4287 // Ps = 2 -> modifyFunctionKeys.
4288 // Ps = 4 -> modifyOtherKeys.
4289 // If no parameters are given, all resources are reset to their
4291 Terminal
.prototype.setResources = function(params
) {
4296 // Disable modifiers which may be enabled via the CSI > Ps; Ps m
4297 // sequence. This corresponds to a resource value of "-1", which
4298 // cannot be set with the other sequence. The parameter identi-
4299 // fies the resource to be disabled:
4300 // Ps = 1 -> modifyCursorKeys.
4301 // Ps = 2 -> modifyFunctionKeys.
4302 // Ps = 4 -> modifyOtherKeys.
4303 // If the parameter is omitted, modifyFunctionKeys is disabled.
4304 // When modifyFunctionKeys is disabled, xterm uses the modifier
4305 // keys to make an extended sequence of functions rather than
4306 // adding a parameter to each function key to denote the modi-
4308 Terminal
.prototype.disableModifiers = function(params
) {
4313 // Set resource value pointerMode. This is used by xterm to
4314 // decide whether to hide the pointer cursor as the user types.
4315 // Valid values for the parameter:
4316 // Ps = 0 -> never hide the pointer.
4317 // Ps = 1 -> hide if the mouse tracking mode is not enabled.
4318 // Ps = 2 -> always hide the pointer. If no parameter is
4319 // given, xterm uses the default, which is 1 .
4320 Terminal
.prototype.setPointerMode = function(params
) {
4324 // CSI ! p Soft terminal reset (DECSTR).
4325 // http://vt100.net/docs/vt220-rm/table4-10.html
4326 Terminal
.prototype.softReset = function(params
) {
4327 this.cursorHidden
= false;
4328 this.insertMode
= false;
4329 this.originMode
= false;
4330 this.wraparoundMode
= false; // autowrap
4331 this.applicationKeypad
= false; // ?
4332 this.applicationCursor
= false;
4334 this.scrollBottom
= this.rows
- 1;
4335 this.curAttr
= this.defAttr
;
4336 this.x
= this.y
= 0; // ?
4337 this.charset
= null;
4338 this.glevel
= 0; // ??
4339 this.charsets
= [null]; // ??
4343 // Request ANSI mode (DECRQM). For VT300 and up, reply is
4345 // where Ps is the mode number as in RM, and Pm is the mode
4347 // 0 - not recognized
4350 // 3 - permanently set
4351 // 4 - permanently reset
4352 Terminal
.prototype.requestAnsiMode = function(params
) {
4357 // Request DEC private mode (DECRQM). For VT300 and up, reply is
4359 // where Ps is the mode number as in DECSET, Pm is the mode value
4360 // as in the ANSI DECRQM.
4361 Terminal
.prototype.requestPrivateMode = function(params
) {
4366 // Set conformance level (DECSCL). Valid values for the first
4368 // Ps = 6 1 -> VT100.
4369 // Ps = 6 2 -> VT200.
4370 // Ps = 6 3 -> VT300.
4371 // Valid values for the second parameter:
4372 // Ps = 0 -> 8-bit controls.
4373 // Ps = 1 -> 7-bit controls (always set for VT100).
4374 // Ps = 2 -> 8-bit controls.
4375 Terminal
.prototype.setConformanceLevel = function(params
) {
4379 // CSI Ps q Load LEDs (DECLL).
4380 // Ps = 0 -> Clear all LEDS (default).
4381 // Ps = 1 -> Light Num Lock.
4382 // Ps = 2 -> Light Caps Lock.
4383 // Ps = 3 -> Light Scroll Lock.
4384 // Ps = 2 1 -> Extinguish Num Lock.
4385 // Ps = 2 2 -> Extinguish Caps Lock.
4386 // Ps = 2 3 -> Extinguish Scroll Lock.
4387 Terminal
.prototype.loadLEDs = function(params
) {
4392 // Set cursor style (DECSCUSR, VT520).
4393 // Ps = 0 -> blinking block.
4394 // Ps = 1 -> blinking block (default).
4395 // Ps = 2 -> steady block.
4396 // Ps = 3 -> blinking underline.
4397 // Ps = 4 -> steady underline.
4398 Terminal
.prototype.setCursorStyle = function(params
) {
4403 // Select character protection attribute (DECSCA). Valid values
4404 // for the parameter:
4405 // Ps = 0 -> DECSED and DECSEL can erase (default).
4406 // Ps = 1 -> DECSED and DECSEL cannot erase.
4407 // Ps = 2 -> DECSED and DECSEL can erase.
4408 Terminal
.prototype.setCharProtectionAttr = function(params
) {
4413 // Restore DEC Private Mode Values. The value of Ps previously
4414 // saved is restored. Ps values are the same as for DECSET.
4415 Terminal
.prototype.restorePrivateValues = function(params
) {
4419 // CSI Pt; Pl; Pb; Pr; Ps$ r
4420 // Change Attributes in Rectangular Area (DECCARA), VT400 and up.
4421 // Pt; Pl; Pb; Pr denotes the rectangle.
4422 // Ps denotes the SGR attributes to change: 0, 1, 4, 5, 7.
4423 // NOTE: xterm doesn't enable this code by default.
4424 Terminal
.prototype.setAttrInRectangle = function(params
) {
4434 for (; t
< b
+ 1; t
++) {
4435 line
= this.lines
[this.ybase
+ t
];
4436 for (i
= l
; i
< r
; i
++) {
4437 line
[i
] = [attr
, line
[i
][1]];
4442 this.updateRange(params
[0]);
4443 this.updateRange(params
[2]);
4447 // CSI Pc; Pt; Pl; Pb; Pr$ x
4448 // Fill Rectangular Area (DECFRA), VT420 and up.
4449 // Pc is the character to use.
4450 // Pt; Pl; Pb; Pr denotes the rectangle.
4451 // NOTE: xterm doesn't enable this code by default.
4452 Terminal
.prototype.fillRectangle = function(params
) {
4462 for (; t
< b
+ 1; t
++) {
4463 line
= this.lines
[this.ybase
+ t
];
4464 for (i
= l
; i
< r
; i
++) {
4465 line
[i
] = [line
[i
][0], String
.fromCharCode(ch
)];
4470 this.updateRange(params
[1]);
4471 this.updateRange(params
[3]);
4475 // Enable Locator Reporting (DECELR).
4476 // Valid values for the first parameter:
4477 // Ps = 0 -> Locator disabled (default).
4478 // Ps = 1 -> Locator enabled.
4479 // Ps = 2 -> Locator enabled for one report, then disabled.
4480 // The second parameter specifies the coordinate unit for locator
4482 // Valid values for the second parameter:
4483 // Pu = 0 <- or omitted -> default to character cells.
4484 // Pu = 1 <- device physical pixels.
4485 // Pu = 2 <- character cells.
4486 Terminal
.prototype.enableLocatorReporting = function(params
) {
4487 var val
= params
[0] > 0;
4488 //this.mouseEvents = val;
4489 //this.decLocator = val;
4492 // CSI Pt; Pl; Pb; Pr$ z
4493 // Erase Rectangular Area (DECERA), VT400 and up.
4494 // Pt; Pl; Pb; Pr denotes the rectangle.
4495 // NOTE: xterm doesn't enable this code by default.
4496 Terminal
.prototype.eraseRectangle = function(params
) {
4506 ch
= [this.eraseAttr(), ' ', 1]; // xterm?
4508 for (; t
< b
+ 1; t
++) {
4509 line
= this.lines
[this.ybase
+ t
];
4510 for (i
= l
; i
< r
; i
++) {
4516 this.updateRange(params
[0]);
4517 this.updateRange(params
[2]);
4522 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
4523 // NOTE: xterm doesn't enable this code by default.
4524 Terminal
.prototype.insertColumns = function() {
4525 var param
= params
[0]
4526 , l
= this.ybase
+ this.rows
4527 , ch
= [this.eraseAttr(), ' ', 1] // xterm?
4531 for (i
= this.ybase
; i
< l
; i
++) {
4532 this.lines
[i
].splice(this.x
+ 1, 0, ch
);
4533 this.lines
[i
].pop();
4542 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
4543 // NOTE: xterm doesn't enable this code by default.
4544 Terminal
.prototype.deleteColumns = function() {
4545 var param
= params
[0]
4546 , l
= this.ybase
+ this.rows
4547 , ch
= [this.eraseAttr(), ' ', 1] // xterm?
4551 for (i
= this.ybase
; i
< l
; i
++) {
4552 this.lines
[i
].splice(this.x
, 1);
4553 this.lines
[i
].push(ch
);
4564 Terminal
.charsets
= {};
4566 // DEC Special Character and Line Drawing Set.
4567 // http://vt100.net/docs/vt102-ug/table5-13.html
4568 // A lot of curses apps use this if they see TERM=xterm.
4569 // testing: echo -e '\e(0a\e(B'
4570 // The xterm output sometimes seems to conflict with the
4571 // reference above. xterm seems in line with the reference
4572 // when running vttest however.
4573 // The table below now uses xterm's output from vttest.
4574 Terminal
.charsets
.SCLD
= { // (0
4575 '`': '\u25c6', // '◆'
4576 'a': '\u2592', // '▒'
4577 'b': '\u0009', // '\t'
4578 'c': '\u000c', // '\f'
4579 'd': '\u000d', // '\r'
4580 'e': '\u000a', // '\n'
4581 'f': '\u00b0', // '°'
4582 'g': '\u00b1', // '±'
4583 'h': '\u2424', // '\u2424' (NL)
4584 'i': '\u000b', // '\v'
4585 'j': '\u2518', // '┘'
4586 'k': '\u2510', // '┐'
4587 'l': '\u250c', // '┌'
4588 'm': '\u2514', // '└'
4589 'n': '\u253c', // '┼'
4590 'o': '\u23ba', // '⎺'
4591 'p': '\u23bb', // '⎻'
4592 'q': '\u2500', // '─'
4593 'r': '\u23bc', // '⎼'
4594 's': '\u23bd', // '⎽'
4595 't': '\u251c', // '├'
4596 'u': '\u2524', // '┤'
4597 'v': '\u2534', // '┴'
4598 'w': '\u252c', // '┬'
4599 'x': '\u2502', // '│'
4600 'y': '\u2264', // '≤'
4601 'z': '\u2265', // '≥'
4602 '{': '\u03c0', // 'π'
4603 '|': '\u2260', // '≠'
4604 '}': '\u00a3', // '£'
4605 '~': '\u00b7' // '·'
4608 Terminal
.charsets
.UK
= null; // (A
4609 Terminal
.charsets
.US
= null; // (B (USASCII)
4610 Terminal
.charsets
.Dutch
= null; // (4
4611 Terminal
.charsets
.Finnish
= null; // (C or (5
4612 Terminal
.charsets
.French
= null; // (R
4613 Terminal
.charsets
.FrenchCanadian
= null; // (Q
4614 Terminal
.charsets
.German
= null; // (K
4615 Terminal
.charsets
.Italian
= null; // (Y
4616 Terminal
.charsets
.NorwegianDanish
= null; // (E or (6
4617 Terminal
.charsets
.Spanish
= null; // (Z
4618 Terminal
.charsets
.Swedish
= null; // (H or (7
4619 Terminal
.charsets
.Swiss
= null; // (=
4620 Terminal
.charsets
.ISOLatin
= null; // /A
4626 function contains(el
, arr
) {
4627 for (var i
= 0; i
< arr
.length
; i
+= 1) {
4628 if (el
=== arr
[i
]) {
4635 function on(el
, type
, handler
, capture
) {
4636 if (!Array
.isArray(el
)) {
4639 el
.forEach(function (element
) {
4640 element
.addEventListener(type
, handler
, capture
|| false);
4644 function off(el
, type
, handler
, capture
) {
4645 el
.removeEventListener(type
, handler
, capture
|| false);
4648 function cancel(ev
, force
) {
4649 if (!this.cancelEvents
&& !force
) {
4652 ev
.preventDefault();
4653 ev
.stopPropagation();
4657 function inherits(child
, parent
) {
4659 this.constructor = child
;
4661 f
.prototype = parent
.prototype;
4662 child
.prototype = new f
;
4665 // if bold is broken, we can't
4666 // use it in the terminal.
4667 function isBoldBroken(document
) {
4668 var body
= document
.getElementsByTagName('body')[0];
4669 var el
= document
.createElement('span');
4670 el
.innerHTML
= 'hello world';
4671 body
.appendChild(el
);
4672 var w1
= el
.scrollWidth
;
4673 el
.style
.fontWeight
= 'bold';
4674 var w2
= el
.scrollWidth
;
4675 body
.removeChild(el
);
4679 var String
= this.String
;
4680 var setTimeout
= this.setTimeout
;
4681 var setInterval
= this.setInterval
;
4683 function indexOf(obj
, el
) {
4686 if (obj
[i
] === el
) return i
;
4691 function isThirdLevelShift(term
, ev
) {
4693 (term
.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) ||
4694 (term
.isMSWindows
&& ev
.altKey
&& ev
.ctrlKey
&& !ev
.metaKey
);
4696 // Don't invoke for arrows, pageDown, home, backspace, etc.
4697 return thirdLevelKey
&& (!ev
.keyCode
|| ev
.keyCode
> 47);
4700 function matchColor(r1
, g1
, b1
) {
4701 var hash
= (r1
<< 16) | (g1
<< 8) | b1
;
4703 if (matchColor
._cache
[hash
] != null) {
4704 return matchColor
._cache
[hash
];
4707 var ldiff
= Infinity
4716 for (; i
< Terminal
.vcolors
.length
; i
++) {
4717 c
= Terminal
.vcolors
[i
];
4722 diff
= matchColor
.distance(r1
, g1
, b1
, r2
, g2
, b2
);
4735 return matchColor
._cache
[hash
] = li
;
4738 matchColor
._cache
= {};
4740 // http://stackoverflow.com/questions/1633828
4741 matchColor
.distance = function(r1
, g1
, b1
, r2
, g2
, b2
) {
4742 return Math
.pow(30 * (r1
- r2
), 2)
4743 + Math
.pow(59 * (g1
- g2
), 2)
4744 + Math
.pow(11 * (b1
- b2
), 2);
4747 function each(obj
, iter
, con
) {
4748 if (obj
.forEach
) return obj
.forEach(iter
, con
);
4749 for (var i
= 0; i
< obj
.length
; i
++) {
4750 iter
.call(con
, obj
[i
], i
, obj
);
4754 function keys(obj
) {
4755 if (Object
.keys
) return Object
.keys(obj
);
4758 if (Object
.prototype.hasOwnProperty
.call(obj
, key
)) {
4765 var wcwidth
= (function(opts
) {
4766 // extracted from https://www.cl.cam.ac.uk/%7Emgk25/ucs/wcwidth.c
4767 // combining characters
4769 [0x0300, 0x036F], [0x0483, 0x0486], [0x0488, 0x0489],
4770 [0x0591, 0x05BD], [0x05BF, 0x05BF], [0x05C1, 0x05C2],
4771 [0x05C4, 0x05C5], [0x05C7, 0x05C7], [0x0600, 0x0603],
4772 [0x0610, 0x0615], [0x064B, 0x065E], [0x0670, 0x0670],
4773 [0x06D6, 0x06E4], [0x06E7, 0x06E8], [0x06EA, 0x06ED],
4774 [0x070F, 0x070F], [0x0711, 0x0711], [0x0730, 0x074A],
4775 [0x07A6, 0x07B0], [0x07EB, 0x07F3], [0x0901, 0x0902],
4776 [0x093C, 0x093C], [0x0941, 0x0948], [0x094D, 0x094D],
4777 [0x0951, 0x0954], [0x0962, 0x0963], [0x0981, 0x0981],
4778 [0x09BC, 0x09BC], [0x09C1, 0x09C4], [0x09CD, 0x09CD],
4779 [0x09E2, 0x09E3], [0x0A01, 0x0A02], [0x0A3C, 0x0A3C],
4780 [0x0A41, 0x0A42], [0x0A47, 0x0A48], [0x0A4B, 0x0A4D],
4781 [0x0A70, 0x0A71], [0x0A81, 0x0A82], [0x0ABC, 0x0ABC],
4782 [0x0AC1, 0x0AC5], [0x0AC7, 0x0AC8], [0x0ACD, 0x0ACD],
4783 [0x0AE2, 0x0AE3], [0x0B01, 0x0B01], [0x0B3C, 0x0B3C],
4784 [0x0B3F, 0x0B3F], [0x0B41, 0x0B43], [0x0B4D, 0x0B4D],
4785 [0x0B56, 0x0B56], [0x0B82, 0x0B82], [0x0BC0, 0x0BC0],
4786 [0x0BCD, 0x0BCD], [0x0C3E, 0x0C40], [0x0C46, 0x0C48],
4787 [0x0C4A, 0x0C4D], [0x0C55, 0x0C56], [0x0CBC, 0x0CBC],
4788 [0x0CBF, 0x0CBF], [0x0CC6, 0x0CC6], [0x0CCC, 0x0CCD],
4789 [0x0CE2, 0x0CE3], [0x0D41, 0x0D43], [0x0D4D, 0x0D4D],
4790 [0x0DCA, 0x0DCA], [0x0DD2, 0x0DD4], [0x0DD6, 0x0DD6],
4791 [0x0E31, 0x0E31], [0x0E34, 0x0E3A], [0x0E47, 0x0E4E],
4792 [0x0EB1, 0x0EB1], [0x0EB4, 0x0EB9], [0x0EBB, 0x0EBC],
4793 [0x0EC8, 0x0ECD], [0x0F18, 0x0F19], [0x0F35, 0x0F35],
4794 [0x0F37, 0x0F37], [0x0F39, 0x0F39], [0x0F71, 0x0F7E],
4795 [0x0F80, 0x0F84], [0x0F86, 0x0F87], [0x0F90, 0x0F97],
4796 [0x0F99, 0x0FBC], [0x0FC6, 0x0FC6], [0x102D, 0x1030],
4797 [0x1032, 0x1032], [0x1036, 0x1037], [0x1039, 0x1039],
4798 [0x1058, 0x1059], [0x1160, 0x11FF], [0x135F, 0x135F],
4799 [0x1712, 0x1714], [0x1732, 0x1734], [0x1752, 0x1753],
4800 [0x1772, 0x1773], [0x17B4, 0x17B5], [0x17B7, 0x17BD],
4801 [0x17C6, 0x17C6], [0x17C9, 0x17D3], [0x17DD, 0x17DD],
4802 [0x180B, 0x180D], [0x18A9, 0x18A9], [0x1920, 0x1922],
4803 [0x1927, 0x1928], [0x1932, 0x1932], [0x1939, 0x193B],
4804 [0x1A17, 0x1A18], [0x1B00, 0x1B03], [0x1B34, 0x1B34],
4805 [0x1B36, 0x1B3A], [0x1B3C, 0x1B3C], [0x1B42, 0x1B42],
4806 [0x1B6B, 0x1B73], [0x1DC0, 0x1DCA], [0x1DFE, 0x1DFF],
4807 [0x200B, 0x200F], [0x202A, 0x202E], [0x2060, 0x2063],
4808 [0x206A, 0x206F], [0x20D0, 0x20EF], [0x302A, 0x302F],
4809 [0x3099, 0x309A], [0xA806, 0xA806], [0xA80B, 0xA80B],
4810 [0xA825, 0xA826], [0xFB1E, 0xFB1E], [0xFE00, 0xFE0F],
4811 [0xFE20, 0xFE23], [0xFEFF, 0xFEFF], [0xFFF9, 0xFFFB],
4812 [0x10A01, 0x10A03], [0x10A05, 0x10A06], [0x10A0C, 0x10A0F],
4813 [0x10A38, 0x10A3A], [0x10A3F, 0x10A3F], [0x1D167, 0x1D169],
4814 [0x1D173, 0x1D182], [0x1D185, 0x1D18B], [0x1D1AA, 0x1D1AD],
4815 [0x1D242, 0x1D244], [0xE0001, 0xE0001], [0xE0020, 0xE007F],
4819 function bisearch(ucs
) {
4821 var max
= COMBINING
.length
- 1;
4823 if (ucs
< COMBINING
[0][0] || ucs
> COMBINING
[max
][1])
4825 while (max
>= min
) {
4826 mid
= Math
.floor((min
+ max
) / 2);
4827 if (ucs
> COMBINING
[mid
][1])
4829 else if (ucs
< COMBINING
[mid
][0])
4836 function wcwidth(ucs
) {
4837 // test for 8-bit control characters
4840 if (ucs
< 32 || (ucs
>= 0x7f && ucs
< 0xa0))
4841 return opts
.control
;
4842 // binary search in table of non-spacing characters
4845 // if we arrive here, ucs is not a combining or C0/C1 control character
4850 ucs
<= 0x115f || // Hangul Jamo init. consonants
4853 (ucs
>= 0x2e80 && ucs
<= 0xa4cf && ucs
!= 0x303f) || // CJK..Yi
4854 (ucs
>= 0xac00 && ucs
<= 0xd7a3) || // Hangul Syllables
4855 (ucs
>= 0xf900 && ucs
<= 0xfaff) || // CJK Compat Ideographs
4856 (ucs
>= 0xfe10 && ucs
<= 0xfe19) || // Vertical forms
4857 (ucs
>= 0xfe30 && ucs
<= 0xfe6f) || // CJK Compat Forms
4858 (ucs
>= 0xff00 && ucs
<= 0xff60) || // Fullwidth Forms
4859 (ucs
>= 0xffe0 && ucs
<= 0xffe6) ||
4860 (ucs
>= 0x20000 && ucs
<= 0x2fffd) ||
4861 (ucs
>= 0x30000 && ucs
<= 0x3fffd)
4866 })({nul
: 0, control
: 0}); // configurable options
4872 Terminal
.EventEmitter
= EventEmitter
;
4873 Terminal
.inherits
= inherits
;
4876 * Adds an event listener to the terminal.
4878 * @param {string} event The name of the event. TODO: Document all event types
4879 * @param {function} callback The function to call when the event is triggered.
4885 Terminal
.cancel
= cancel
;