]>
git.proxmox.com Git - mirror_xterm.js.git/blob - src/xterm.js
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 this.removeListener(type
, on
);
112 return listener
.apply(this, 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 * @alias module:xterm/src/xterm
155 function Terminal(options
) {
158 if (!(this instanceof Terminal
)) {
159 return new Terminal(arguments
[0], arguments
[1], arguments
[2]);
162 self
.cancel
= Terminal
.cancel
;
164 EventEmitter
.call(this);
166 if (typeof options
=== 'number') {
170 handler
: arguments
[2]
174 options
= options
|| {};
177 Object
.keys(Terminal
.defaults
).forEach(function(key
) {
178 if (options
[key
] == null) {
179 options
[key
] = Terminal
.options
[key
];
181 if (Terminal
[key
] !== Terminal
.defaults
[key
]) {
182 options
[key
] = Terminal
[key
];
185 self
[key
] = options
[key
];
188 if (options
.colors
.length
=== 8) {
189 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(8));
190 } else if (options
.colors
.length
=== 16) {
191 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(16));
192 } else if (options
.colors
.length
=== 10) {
193 options
.colors
= options
.colors
.slice(0, -2).concat(
194 Terminal
._colors
.slice(8, -2), options
.colors
.slice(-2));
195 } else if (options
.colors
.length
=== 18) {
196 options
.colors
= options
.colors
.concat(
197 Terminal
._colors
.slice(16, -2), options
.colors
.slice(-2));
199 this.colors
= options
.colors
;
201 this.options
= options
;
203 // this.context = options.context || window;
204 // this.document = options.document || document;
205 this.parent
= options
.body
|| options
.parent
206 || (document
? document
.getElementsByTagName('body')[0] : null);
208 this.cols
= options
.cols
|| options
.geometry
[0];
209 this.rows
= options
.rows
|| options
.geometry
[1];
211 if (options
.handler
) {
212 this.on('data', options
.handler
);
216 * The scroll position of the y cursor, ie. ybase + y = the y position within the entire
222 * The scroll position of the viewport
227 * The cursor's x position after ybase
232 * The cursor's y position after ybase
237 * Used to debounce the refresh function
239 this.isRefreshing
= false;
242 * Whether there is a full terminal refresh queued
245 this.cursorState
= 0;
246 this.cursorHidden
= false;
251 this.scrollBottom
= this.rows
- 1;
254 this.applicationKeypad
= false;
255 this.applicationCursor
= false;
256 this.originMode
= false;
257 this.insertMode
= false;
258 this.wraparoundMode
= true; // defaults: xterm - true, vt100 - false
263 this.gcharset
= null;
265 this.charsets
= [null];
289 this.readable
= true;
290 this.writable
= true;
292 this.defAttr
= (0 << 18) | (257 << 9) | (256 << 0);
293 this.curAttr
= this.defAttr
;
296 this.currentParam
= 0;
300 // leftover surrogate high from previous write invocation
301 this.surrogate_high
= '';
304 * An array of all lines in the entire buffer, including the prompt. The lines are array of
305 * characters which are 2-length arrays where [0] is an attribute and [1] is the character.
310 this.lines
.push(this.blankLine());
317 inherits(Terminal
, EventEmitter
);
321 * back_color_erase feature for xterm.
323 Terminal
.prototype.eraseAttr = function() {
324 // if (this.is('screen')) return this.defAttr;
325 return (this.defAttr
& ~0x1ff) | (this.curAttr
& 0x1ff);
333 Terminal
.tangoColors
= [
354 // Colors 0-15 + 16-255
355 // Much thanks to TooTallNate for writing this.
356 Terminal
.colors
= (function() {
357 var colors
= Terminal
.tangoColors
.slice()
358 , r
= [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff]
363 for (; i
< 216; i
++) {
364 out(r
[(i
/ 36) % 6 | 0], r
[(i
/ 6) % 6 | 0], r
[i
% 6]);
369 for (; i
< 24; i
++) {
374 function out(r
, g
, b
) {
375 colors
.push('#' + hex(r
) + hex(g
) + hex(b
));
380 return c
.length
< 2 ? '0' + c
: c
;
386 Terminal
._colors
= Terminal
.colors
.slice();
388 Terminal
.vcolors
= (function() {
390 , colors
= Terminal
.colors
394 for (; i
< 256; i
++) {
395 color
= parseInt(colors
[i
].substring(1), 16);
397 (color
>> 16) & 0xff,
410 Terminal
.defaults
= {
411 colors
: Terminal
.colors
,
423 // programFeatures: false,
427 Terminal
.options
= {};
429 Terminal
.focus
= null;
431 each(keys(Terminal
.defaults
), function(key
) {
432 Terminal
[key
] = Terminal
.defaults
[key
];
433 Terminal
.options
[key
] = Terminal
.defaults
[key
];
437 * Focus the terminal. Delegates focus handling to the terminal's DOM element.
439 Terminal
.prototype.focus = function() {
440 if (document
.activeElement
=== this.textarea
) {
444 if (this.sendFocus
) {
448 this.element
.classList
.add('focus');
450 this.textarea
.focus();
451 Terminal
.focus
= this;
455 * Binds the desired focus behavior on a given terminal object.
459 Terminal
.bindFocus = function (term
) {
460 on(term
.element
, 'focus', function (ev
) {
461 if (term
.sendFocus
) {
466 Terminal
.focus
= term
;
467 term
.emit('focus', {terminal
: term
});
472 * Blur the terminal. Delegates blur handling to the terminal's DOM element.
474 Terminal
.prototype.blur = function() {
475 if (Terminal
.focus
!== this) {
479 this.element
.classList
.remove('focus');
480 this.cursorState
= 0;
481 this.refresh(this.y
, this.y
);
482 this.textarea
.blur();
483 if (this.sendFocus
) {
484 this.send('\x1b[0]');
486 Terminal
.focus
= null;
490 * Binds the desired blur behavior on a given terminal object.
494 Terminal
.bindBlur = function (term
) {
495 on(term
.element
, 'blur', function (ev
) {
496 if (term
.sendFocus
) {
499 Terminal
.focus
= null;
500 term
.emit('blur', {terminal
: term
});
505 * Initialize default behavior
507 Terminal
.prototype.initGlobal = function() {
508 Terminal
.bindPaste(this);
509 Terminal
.bindKeys(this);
510 Terminal
.bindCopy(this);
514 * Bind to paste event and allow both keyboard and right-click pasting, without having the
515 * contentEditable value set to true.
517 Terminal
.bindPaste = function(term
) {
518 on([term
.textarea
, term
.element
], 'paste', function(ev
) {
519 ev
.stopPropagation();
520 if (ev
.clipboardData
) {
521 var text
= ev
.clipboardData
.getData('text/plain');
523 term
.textarea
.value
= '';
524 return term
.cancel(ev
);
530 * Prepares text copied from terminal selection, to be saved in the clipboard by:
531 * 1. stripping all trailing white spaces
532 * 2. converting all non-breaking spaces to regular spaces
533 * @param {string} text The copied text that needs processing for storing in clipboard
537 Terminal
.prepareCopiedTextForClipboard = function (text
) {
538 var space
= String
.fromCharCode(32),
539 nonBreakingSpace
= String
.fromCharCode(160),
540 allNonBreakingSpaces
= new RegExp(nonBreakingSpace
, 'g'),
541 processedText
= text
.split('\n').map(function (line
) {
543 * Strip all trailing white spaces and convert all non-breaking spaces to regular
546 var processedLine
= line
.replace(/\s+$/g, '').replace(allNonBreakingSpaces
, space
);
548 return processedLine
;
551 return processedText
;
555 * Apply key handling to the terminal
557 Terminal
.bindKeys = function(term
) {
558 on(term
.element
, 'keydown', function(ev
) {
559 if (document
.activeElement
!= this) {
565 on(term
.element
, 'keypress', function(ev
) {
566 if (document
.activeElement
!= this) {
572 on(term
.element
, 'keyup', term
.focus
.bind(term
));
574 on(term
.textarea
, 'keydown', function(ev
) {
578 on(term
.textarea
, 'keypress', function(ev
) {
580 // Truncate the textarea's value, since it is not needed
584 on(term
.textarea
, 'compositionstart', function(ev
) {
585 console
.log('compositionstart', ev
);
586 // TODO: Set a composing flag
588 on(term
.textarea
, 'compositionupdate', function(ev
) {
589 console
.log('compositionupdate', ev
);
590 // TODO: Display text being composed in the UI (needs to be handled in keydown)
592 on(term
.textarea
, 'compositionend', function(ev
) {
593 console
.log('compositionend', ev
);
594 // TODO: Send composed text to the pty
595 // TODO: Remove composing flag
600 * Binds copy functionality to the given terminal.
603 Terminal
.bindCopy = function(term
) {
604 on(term
.element
, 'copy', function(ev
) {
611 * Insert the given row to the terminal or produce a new one
612 * if no row argument is passed. Return the inserted row.
613 * @param {HTMLElement} row (optional) The row to append to the terminal.
615 Terminal
.prototype.insertRow = function (row
) {
616 if (typeof row
!= 'object') {
617 row
= document
.createElement('div');
620 this.rowContainer
.appendChild(row
);
621 this.children
.push(row
);
628 * Opens the terminal within an element.
630 * @param {HTMLElement} parent The element to create the terminal within.
632 Terminal
.prototype.open = function(parent
) {
633 var self
=this, i
=0, div
;
635 this.parent
= parent
|| this.parent
;
638 throw new Error('Terminal requires a parent element.');
642 * Grab global elements
644 this.context
= this.parent
.ownerDocument
.defaultView
;
645 this.document
= this.parent
.ownerDocument
;
646 this.body
= this.document
.getElementsByTagName('body')[0];
651 if (this.context
.navigator
&& this.context
.navigator
.userAgent
) {
652 this.isMSIE
= !!~this.context
.navigator
.userAgent
.indexOf('MSIE');
656 * Find the users platform. We use this to interpret the meta key
657 * and ISO third level shifts.
658 * http://stackoverflow.com/questions/19877924/what-is-the-list-of-possible-values-for-navigator-platform-as-of-today
660 if (this.context
.navigator
&& this.context
.navigator
.platform
) {
661 this.isMac
= contains(
662 this.context
.navigator
.platform
,
663 ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K']
665 this.isIpad
= this.context
.navigator
.platform
=== 'iPad';
666 this.isIphone
= this.context
.navigator
.platform
=== 'iPhone';
667 this.isMSWindows
= contains(
668 this.context
.navigator
.platform
,
669 ['Windows', 'Win16', 'Win32', 'WinCE']
674 * Create main element container
676 this.element
= this.document
.createElement('div');
677 this.element
.classList
.add('terminal');
678 this.element
.classList
.add('xterm');
679 this.element
.classList
.add('xterm-theme-' + this.theme
);
680 this.element
.setAttribute('tabindex', 0);
683 * Create the container that will hold the lines of the terminal and then
684 * produce the lines the lines.
686 this.rowContainer
= document
.createElement('div');
687 this.rowContainer
.classList
.add('xterm-rows');
688 this.element
.appendChild(this.rowContainer
);
692 * Create the container that will hold helpers like the textarea for
693 * capturing DOM Events. Then produce the helpers.
695 this.helperContainer
= document
.createElement('div');
696 this.helperContainer
.classList
.add('xterm-helpers');
697 this.element
.appendChild(this.helperContainer
);
698 this.textarea
= document
.createElement('textarea');
699 this.textarea
.classList
.add('xterm-helper-textarea');
700 this.textarea
.setAttribute('autocorrect', 'off');
701 this.textarea
.setAttribute('autocapitalize', 'off');
702 this.textarea
.setAttribute('spellcheck', 'false');
703 this.textarea
.tabIndex
= 0;
704 this.textarea
.onfocus = function() {
705 self
.emit('focus', {terminal
: self
});
707 this.textarea
.onblur = function() {
708 self
.emit('blur', {terminal
: self
});
710 this.helperContainer
.appendChild(this.textarea
);
712 for (; i
< this.rows
; i
++) {
715 this.parent
.appendChild(this.element
);
718 this.refresh(0, this.rows
- 1);
720 // Initialize global actions that
721 // need to be taken on the document.
724 // Ensure there is a Terminal.focus.
727 on(this.element
, 'mouseup', function() {
728 var selection
= document
.getSelection(),
729 collapsed
= selection
.isCollapsed
,
730 isRange
= typeof collapsed
== 'boolean' ? !collapsed
: selection
.type
== 'Range';
736 // Listen for mouse events and translate
737 // them into terminal mouse protocols.
740 // Figure out whether boldness affects
741 // the character width of monospace fonts.
742 if (Terminal
.brokenBold
== null) {
743 Terminal
.brokenBold
= isBoldBroken(this.document
);
751 * Attempts to load an add-on using CommonJS or RequireJS (whichever is available).
752 * @param {string} addon The name of the addon to load
755 Terminal
.loadAddon = function(addon
, callback
) {
756 if (typeof exports
=== 'object' && typeof module
=== 'object') {
758 return require(__dirname
+ '/../addons/' + addon
);
759 } else if (typeof define
== 'function') {
761 return require(['../addons/' + addon
+ '/' + addon
], callback
);
763 console
.error('Cannot load a module without a CommonJS or RequireJS environment.');
771 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#Mouse%20Tracking
772 * To better understand these
773 * the xterm code is very helpful:
775 * button.c, charproc.c, misc.c
776 * Relevant functions in xterm/button.c:
777 * BtnCode, EmitButtonCode, EditorButton, SendMousePosition
779 Terminal
.prototype.bindMouse = function() {
780 var el
= this.element
, self
= this, pressed
= 32;
781 var wheelEvent
= ('onmousewheel' in this.context
) ? 'mousewheel' : 'DOMMouseScroll';
783 // mouseup, mousedown, mousewheel
784 // left click: ^[[M 3<^[[M#3<
785 // mousewheel up: ^[[M`3>
786 function sendButton(ev
) {
790 // get the xterm-style button
791 button
= getButton(ev
);
793 // get mouse coordinates
797 sendEvent(button
, pos
);
799 switch (ev
.overrideType
|| ev
.type
) {
804 // keep it at the left
805 // button, just in case.
816 // motion example of a left click:
817 // ^[[M 3<^[[M@4<^[[M@5<^[[M@6<^[[M@7<^[[M#7<
818 function sendMove(ev
) {
825 // buttons marked as motions
826 // are incremented by 32
829 sendEvent(button
, pos
);
833 // position to characters
834 function encode(data
, ch
) {
835 if (!self
.utfMouse
) {
836 if (ch
=== 255) return data
.push(0);
837 if (ch
> 127) ch
= 127;
840 if (ch
=== 2047) return data
.push(0);
844 if (ch
> 2047) ch
= 2047;
845 data
.push(0xC0 | (ch
>> 6));
846 data
.push(0x80 | (ch
& 0x3F));
851 // send a mouse event:
852 // regular/utf8: ^[[M Cb Cx Cy
853 // urxvt: ^[[ Cb ; Cx ; Cy M
854 // sgr: ^[[ Cb ; Cx ; Cy M/m
855 // vt300: ^[[ 24(1/3/5)~ [ Cx , Cy ] \r
856 // locator: CSI P e ; P b ; P r ; P c ; P p & w
857 function sendEvent(button
, pos
) {
858 // self.emit('mouse', {
864 if (self
.vt300Mouse
) {
866 // http://www.vt100.net/docs/vt3xx-gp/chapter15.html
870 var data
= '\x1b[24';
871 if (button
=== 0) data
+= '1';
872 else if (button
=== 1) data
+= '3';
873 else if (button
=== 2) data
+= '5';
874 else if (button
=== 3) return;
876 data
+= '~[' + pos
.x
+ ',' + pos
.y
+ ']\r';
881 if (self
.decLocator
) {
886 if (button
=== 0) button
= 2;
887 else if (button
=== 1) button
= 4;
888 else if (button
=== 2) button
= 6;
889 else if (button
=== 3) button
= 3;
893 + (button
=== 3 ? 4 : 0)
904 if (self
.urxvtMouse
) {
909 self
.send('\x1b[' + button
+ ';' + pos
.x
+ ';' + pos
.y
+ 'M');
917 + ((button
& 3) === 3 ? button
& ~3 : button
)
922 + ((button
& 3) === 3 ? 'm' : 'M'));
928 encode(data
, button
);
932 self
.send('\x1b[M' + String
.fromCharCode
.apply(String
, data
));
935 function getButton(ev
) {
948 // 1, and 2 - with 64 added
949 switch (ev
.overrideType
|| ev
.type
) {
951 button
= ev
.button
!= null
958 button
= button
=== 1 ? 0 : button
=== 4 ? 1 : button
;
964 case 'DOMMouseScroll':
965 button
= ev
.detail
< 0
970 button
= ev
.wheelDeltaY
> 0
976 // next three bits are the modifiers:
977 // 4 = shift, 8 = meta, 16 = control
978 shift
= ev
.shiftKey
? 4 : 0;
979 meta
= ev
.metaKey
? 8 : 0;
980 ctrl
= ev
.ctrlKey
? 16 : 0;
981 mod
= shift
| meta
| ctrl
;
984 if (self
.vt200Mouse
) {
987 } else if (!self
.normalMouse
) {
992 button
= (32 + (mod
<< 2)) + button
;
997 // mouse coordinates measured in cols/rows
998 function getCoords(ev
) {
1001 // ignore browsers without pageX for now
1002 if (ev
.pageX
== null) return;
1008 // should probably check offsetParent
1009 // but this is more portable
1010 while (el
&& el
!== self
.document
.documentElement
) {
1013 el
= 'offsetParent' in el
1018 // convert to cols/rows
1019 w
= self
.element
.clientWidth
;
1020 h
= self
.element
.clientHeight
;
1021 x
= Math
.round((x
/ w
) * self
.cols
);
1022 y
= Math
.round((y
/ h
) * self
.rows
);
1024 // be sure to avoid sending
1025 // bad positions to the program
1027 if (x
> self
.cols
) x
= self
.cols
;
1029 if (y
> self
.rows
) y
= self
.rows
;
1031 // xterm sends raw bytes and
1032 // starts at 32 (SP) for each.
1039 type
: (ev
.overrideType
|| ev
.type
) === wheelEvent
1041 : (ev
.overrideType
|| ev
.type
)
1045 on(el
, 'mousedown', function(ev
) {
1046 if (!self
.mouseEvents
) return;
1055 //if (self.vt200Mouse && !self.normalMouse) {
1056 if (self
.vt200Mouse
) {
1057 ev
.overrideType
= 'mouseup';
1059 return self
.cancel(ev
);
1063 if (self
.normalMouse
) on(self
.document
, 'mousemove', sendMove
);
1065 // x10 compatibility mode can't send button releases
1066 if (!self
.x10Mouse
) {
1067 on(self
.document
, 'mouseup', function up(ev
) {
1069 if (self
.normalMouse
) off(self
.document
, 'mousemove', sendMove
);
1070 off(self
.document
, 'mouseup', up
);
1071 return self
.cancel(ev
);
1075 return self
.cancel(ev
);
1078 //if (self.normalMouse) {
1079 // on(self.document, 'mousemove', sendMove);
1082 on(el
, wheelEvent
, function(ev
) {
1083 if (!self
.mouseEvents
) return;
1086 || self
.decLocator
) return;
1088 return self
.cancel(ev
);
1091 // allow mousewheel scrolling in
1092 // the shell for example
1093 on(el
, wheelEvent
, function(ev
) {
1094 if (self
.mouseEvents
) return;
1095 if (self
.applicationKeypad
) return;
1096 if (ev
.type
=== 'DOMMouseScroll') {
1097 self
.scrollDisp(ev
.detail
< 0 ? -1 : 1);
1099 self
.scrollDisp(ev
.wheelDeltaY
> 0 ? -1 : 1);
1101 return self
.cancel(ev
);
1106 * Destroys the terminal.
1108 Terminal
.prototype.destroy = function() {
1109 this.readable
= false;
1110 this.writable
= false;
1112 this.handler = function() {};
1113 this.write = function() {};
1114 if (this.element
.parentNode
) {
1115 this.element
.parentNode
.removeChild(this.element
);
1117 //this.emit('close');
1122 * Flags used to render terminal text properly
1133 * Refreshes (re-renders) terminal content within two rows (inclusive)
1137 * In the screen buffer, each character is stored as a an array with a character
1138 * and a 32-bit integer:
1139 * - First value: a utf-16 character.
1141 * - Next 9 bits: background color (0-511).
1142 * - Next 9 bits: foreground color (0-511).
1143 * - Next 14 bits: a mask for misc. flags:
1150 * @param {number} start The row to start from (between 0 and terminal's height terminal - 1)
1151 * @param {number} end The row to end at (between fromRow and terminal's height terminal - 1)
1152 * @param {boolean} queue Whether the refresh should ran right now or be queued
1154 Terminal
.prototype.refresh = function(start
, end
, queue
) {
1157 // queue defaults to true
1158 queue
= (typeof queue
== 'undefined') ? true : queue
;
1161 * The refresh queue allows refresh to execute only approximately 30 times a second. For
1162 * commands that pass a significant amount of output to the write function, this prevents the
1163 * terminal from maxing out the CPU and making the UI unresponsive. While commands can still
1164 * run beyond what they do on the terminal, it is far better with a debounce in place as
1165 * every single terminal manipulation does not need to be constructed in the DOM.
1167 * A side-effect of this is that it makes ^C to interrupt a process seem more responsive.
1170 // If refresh should be queued, order the refresh and return.
1171 if (this._refreshIsQueued
) {
1172 // If a refresh has already been queued, just order a full refresh next
1173 this._fullRefreshNext
= true;
1175 setTimeout(function () {
1176 self
.refresh(start
, end
, false);
1178 this._refreshIsQueued
= true;
1183 // If refresh should be run right now (not be queued), release the lock
1184 this._refreshIsQueued
= false;
1186 // If multiple refreshes were requested, make a full refresh.
1187 if (this._fullRefreshNext
) {
1189 end
= this.rows
- 1;
1190 this._fullRefreshNext
= false // reset lock
1193 var x
, y
, i
, line
, out
, ch
, ch_width
, width
, data
, attr
, bg
, fg
, flags
, row
, parent
, focused
= document
.activeElement
;
1195 // If this is a big refresh, remove the terminal rows from the DOM for faster calculations
1196 if (end
- start
>= this.rows
/ 2) {
1197 parent
= this.element
.parentNode
;
1199 this.element
.removeChild(this.rowContainer
);
1206 if (end
>= this.rows
.length
) {
1207 this.log('`end` is too large. Most likely a bad CSR.');
1208 end
= this.rows
.length
- 1;
1211 for (; y
<= end
; y
++) {
1212 row
= y
+ this.ydisp
;
1214 line
= this.lines
[row
];
1217 if (this.y
=== y
- (this.ybase
- this.ydisp
)
1219 && !this.cursorHidden
) {
1225 attr
= this.defAttr
;
1228 for (; i
< width
; i
++) {
1231 ch_width
= line
[i
][2];
1235 if (i
=== x
) data
= -1;
1237 if (data
!== attr
) {
1238 if (attr
!== this.defAttr
) {
1241 if (data
!== this.defAttr
) {
1243 out
+= '<span class="reverse-video terminal-cursor';
1244 if (this.cursorBlink
) {
1249 var classNames
= [];
1252 fg
= (data
>> 9) & 0x1ff;
1255 if (flags
& Terminal
.flags
.BOLD
) {
1256 if (!Terminal
.brokenBold
) {
1257 classNames
.push('xterm-bold');
1259 // See: XTerm*boldColors
1260 if (fg
< 8) fg
+= 8;
1263 if (flags
& Terminal
.flags
.UNDERLINE
) {
1264 classNames
.push('xterm-underline');
1267 if (flags
& Terminal
.flags
.BLINK
) {
1268 classNames
.push('xterm-blink');
1272 * If inverse flag is on, then swap the foreground and background variables.
1274 if (flags
& Terminal
.flags
.INVERSE
) {
1275 /* One-line variable swap in JavaScript: http://stackoverflow.com/a/16201730 */
1276 bg = [fg, fg = bg][0];
1277 // Should inverse just be before the
1278 // above boldColors effect instead?
1279 if ((flags & 1) && fg < 8) fg += 8;
1282 if (flags & Terminal.flags.INVISIBLE) {
1283 classNames.push('xterm-hidden');
1287 * Weird situation: Invert flag used black foreground and white background results
1288 * in invalid background color, positioned at the 256 index of the 256 terminal
1289 * color map. Pin the colors manually in such a case.
1291 * Source: https://github.com/sourcelair/xterm.js/issues/57
1293 if (flags
& Terminal
.flags
.INVERSE
) {
1303 classNames
.push('xterm-bg-color-' + bg
);
1307 classNames
.push('xterm-color-' + fg
);
1311 if (classNames
.length
) {
1312 out
+= ' class="' + classNames
.join(' ') + '"';
1341 if (attr
!== this.defAttr
) {
1345 this.children
[y
].innerHTML
= out
;
1349 this.element
.appendChild(this.rowContainer
);
1352 this.emit('refresh', {element
: this.element
, start
: start
, end
: end
});
1356 * Display the cursor element
1358 Terminal
.prototype.showCursor = function() {
1359 if (!this.cursorState
) {
1360 this.cursorState
= 1;
1361 this.refresh(this.y
, this.y
);
1366 * Scroll the terminal
1368 Terminal
.prototype.scroll = function() {
1371 if (++this.ybase
=== this.scrollback
) {
1372 this.ybase
= this.ybase
/ 2 | 0;
1373 this.lines
= this.lines
.slice(-(this.ybase
+ this.rows
) + 1);
1376 this.ydisp
= this.ybase
;
1379 row
= this.ybase
+ this.rows
- 1;
1381 // subtract the bottom scroll region
1382 row
-= this.rows
- 1 - this.scrollBottom
;
1384 if (row
=== this.lines
.length
) {
1385 // potential optimization:
1386 // pushing is faster than splicing
1387 // when they amount to the same
1389 this.lines
.push(this.blankLine());
1392 this.lines
.splice(row
, 0, this.blankLine());
1395 if (this.scrollTop
!== 0) {
1396 if (this.ybase
!== 0) {
1398 this.ydisp
= this.ybase
;
1400 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
1404 this.updateRange(this.scrollTop
);
1405 this.updateRange(this.scrollBottom
);
1409 * Scroll the display of the terminal
1410 * @param {number} disp The number of lines to scroll down (negatives scroll up).
1412 Terminal
.prototype.scrollDisp = function(disp
) {
1415 if (this.ydisp
> this.ybase
) {
1416 this.ydisp
= this.ybase
;
1417 } else if (this.ydisp
< 0) {
1421 this.refresh(0, this.rows
- 1);
1425 * Writes text to the terminal.
1426 * @param {string} text The text to write to the terminal.
1428 Terminal
.prototype.write = function(data
) {
1429 var l
= data
.length
, i
= 0, j
, cs
, ch
, code
, low
, ch_width
, row
;
1431 this.refreshStart
= this.y
;
1432 this.refreshEnd
= this.y
;
1434 if (this.ybase
!== this.ydisp
) {
1435 this.ydisp
= this.ybase
;
1439 // apply leftover surrogate high from last write
1440 if (this.surrogate_high
) {
1441 data
= this.surrogate_high
+ data
;
1442 this.surrogate_high
= '';
1445 for (; i
< l
; i
++) {
1448 // FIXME: higher chars than 0xa0 are not allowed in escape sequences
1449 // --> maybe move to default
1450 code
= data
.charCodeAt(i
);
1451 if (0xD800 <= code
&& code
<= 0xDBFF) {
1452 // we got a surrogate high
1453 // get surrogate low (next 2 bytes)
1454 low
= data
.charCodeAt(i
+1);
1456 // end of data stream, save surrogate high
1457 this.surrogate_high
= ch
;
1460 code
= ((code
- 0xD800) * 0x400) + (low
- 0xDC00) + 0x10000;
1461 ch
+= data
.charAt(i
+1);
1463 // surrogate low - already handled above
1464 if (0xDC00 <= code
&& code
<= 0xDFFF)
1467 switch (this.state
) {
1478 if (this.convertEol
) {
1482 if (this.y
> this.scrollBottom
) {
1502 this.x
= this.nextStop();
1517 this.state
= escaped
;
1522 // calculate print space
1523 // expensive call, therefore we save width in line buffer
1524 ch_width
= wcwidth(code
);
1527 if (this.charset
&& this.charset
[ch
]) {
1528 ch
= this.charset
[ch
];
1531 row
= this.y
+ this.ybase
;
1533 // insert combining char in last cell
1534 // FIXME: needs handling after cursor jumps
1535 if (!ch_width
&& this.x
) {
1537 // dont overflow left
1538 if (this.lines
[row
][this.x
-1]) {
1539 if (!this.lines
[row
][this.x
-1][2]) {
1541 // found empty cell after fullwidth, need to go 2 cells back
1542 if (this.lines
[row
][this.x
-2])
1543 this.lines
[row
][this.x
-2][1] += ch
;
1546 this.lines
[row
][this.x
-1][1] += ch
;
1548 this.updateRange(this.y
);
1553 // goto next line if ch would overflow
1554 // TODO: needs a global min terminal width of 2
1555 if (this.x
+ch_width
-1 >= this.cols
) {
1556 // autowrap - DECAWM
1557 if (this.wraparoundMode
) {
1560 if (this.y
> this.scrollBottom
) {
1565 this.x
= this.cols
-1;
1566 if(ch_width
===2) // FIXME: check for xterm behavior
1570 row
= this.y
+ this.ybase
;
1572 // insert mode: move characters to right
1573 if (this.insertMode
) {
1574 // do this twice for a fullwidth char
1575 for (var moves
=0; moves
<ch_width
; ++moves
) {
1576 // remove last cell, if it's width is 0
1577 // we have to adjust the second last cell as well
1578 var removed
= this.lines
[this.y
+ this.ybase
].pop();
1580 && this.lines
[row
][this.cols
-2]
1581 && this.lines
[row
][this.cols
-2][2]===2)
1582 this.lines
[row
][this.cols
-2] = [this.curAttr
, ' ', 1];
1584 // insert empty cell at cursor
1585 this.lines
[row
].splice(this.x
, 0, [this.curAttr
, ' ', 1]);
1589 this.lines
[row
][this.x
] = [this.curAttr
, ch
, ch_width
];
1591 this.updateRange(this.y
);
1593 // fullwidth char - set next cell width to zero and advance cursor
1595 this.lines
[row
][this.x
] = [this.curAttr
, '', 0];
1604 // ESC [ Control Sequence Introducer ( CSI is 0x9b).
1607 this.currentParam
= 0;
1611 // ESC ] Operating System Command ( OSC is 0x9d).
1614 this.currentParam
= 0;
1618 // ESC P Device Control String ( DCS is 0x90).
1621 this.currentParam
= 0;
1625 // ESC _ Application Program Command ( APC is 0x9f).
1627 this.state
= ignore
;
1630 // ESC ^ Privacy Message ( PM is 0x9e).
1632 this.state
= ignore
;
1635 // ESC c Full Reset (RIS).
1640 // ESC E Next Line ( NEL is 0x85).
1641 // ESC D Index ( IND is 0x84).
1649 // ESC M Reverse Index ( RI is 0x8d).
1651 this.reverseIndex();
1654 // ESC % Select default/utf-8 character set.
1655 // @ = default, G = utf-8
1657 //this.charset = null;
1659 this.setgCharset(0, Terminal
.charsets
.US
);
1660 this.state
= normal
;
1664 // ESC (,),*,+,-,. Designate G0-G2 Character Set.
1665 case '(': // <-- this seems to get all the attention
1691 this.state
= charset
;
1694 // Designate G3 Character Set (VT300).
1695 // A = ISO Latin-1 Supplemental.
1699 this.state
= charset
;
1704 // Single Shift Select of G2 Character Set
1705 // ( SS2 is 0x8e). This affects next character only.
1709 // Single Shift Select of G3 Character Set
1710 // ( SS3 is 0x8f). This affects next character only.
1714 // Invoke the G2 Character Set as GL (LS2).
1719 // Invoke the G3 Character Set as GL (LS3).
1724 // Invoke the G3 Character Set as GR (LS3R).
1729 // Invoke the G2 Character Set as GR (LS2R).
1734 // Invoke the G1 Character Set as GR (LS1R).
1739 // ESC 7 Save Cursor (DECSC).
1742 this.state
= normal
;
1745 // ESC 8 Restore Cursor (DECRC).
1747 this.restoreCursor();
1748 this.state
= normal
;
1751 // ESC # 3 DEC line height/width
1753 this.state
= normal
;
1757 // ESC H Tab Set (HTS is 0x88).
1762 // ESC = Application Keypad (DECPAM).
1764 this.log('Serial port requested application keypad.');
1765 this.applicationKeypad
= true;
1766 this.state
= normal
;
1769 // ESC > Normal Keypad (DECPNM).
1771 this.log('Switching back to normal keypad.');
1772 this.applicationKeypad
= false;
1773 this.state
= normal
;
1777 this.state
= normal
;
1778 this.error('Unknown ESC control: %s.', ch
);
1785 case '0': // DEC Special Character and Line Drawing Set.
1786 cs
= Terminal
.charsets
.SCLD
;
1789 cs
= Terminal
.charsets
.UK
;
1791 case 'B': // United States (USASCII).
1792 cs
= Terminal
.charsets
.US
;
1795 cs
= Terminal
.charsets
.Dutch
;
1797 case 'C': // Finnish
1799 cs
= Terminal
.charsets
.Finnish
;
1802 cs
= Terminal
.charsets
.French
;
1804 case 'Q': // FrenchCanadian
1805 cs
= Terminal
.charsets
.FrenchCanadian
;
1808 cs
= Terminal
.charsets
.German
;
1810 case 'Y': // Italian
1811 cs
= Terminal
.charsets
.Italian
;
1813 case 'E': // NorwegianDanish
1815 cs
= Terminal
.charsets
.NorwegianDanish
;
1817 case 'Z': // Spanish
1818 cs
= Terminal
.charsets
.Spanish
;
1820 case 'H': // Swedish
1822 cs
= Terminal
.charsets
.Swedish
;
1825 cs
= Terminal
.charsets
.Swiss
;
1827 case '/': // ISOLatin (actually /A)
1828 cs
= Terminal
.charsets
.ISOLatin
;
1832 cs
= Terminal
.charsets
.US
;
1835 this.setgCharset(this.gcharset
, cs
);
1836 this.gcharset
= null;
1837 this.state
= normal
;
1843 // Set Text Parameters.
1844 if (ch
=== '\x1b' || ch
=== '\x07') {
1845 if (ch
=== '\x1b') i
++;
1847 this.params
.push(this.currentParam
);
1849 switch (this.params
[0]) {
1853 if (this.params
[1]) {
1854 this.title
= this.params
[1];
1855 this.handleTitle(this.title
);
1863 // change dynamic colors
1875 // change dynamic ui colors
1887 // manipulate selection data
1905 this.currentParam
= 0;
1906 this.state
= normal
;
1908 if (!this.params
.length
) {
1909 if (ch
>= '0' && ch
<= '9') {
1911 this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1912 } else if (ch
=== ';') {
1913 this.params
.push(this.currentParam
);
1914 this.currentParam
= '';
1917 this.currentParam
+= ch
;
1924 if (ch
=== '?' || ch
=== '>' || ch
=== '!') {
1930 if (ch
>= '0' && ch
<= '9') {
1931 this.currentParam
= this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1935 // '$', '"', ' ', '\''
1936 if (ch
=== '$' || ch
=== '"' || ch
=== ' ' || ch
=== '\'') {
1941 this.params
.push(this.currentParam
);
1942 this.currentParam
= 0;
1945 if (ch
=== ';') break;
1947 this.state
= normal
;
1951 // Cursor Up Ps Times (default = 1) (CUU).
1953 this.cursorUp(this.params
);
1957 // Cursor Down Ps Times (default = 1) (CUD).
1959 this.cursorDown(this.params
);
1963 // Cursor Forward Ps Times (default = 1) (CUF).
1965 this.cursorForward(this.params
);
1969 // Cursor Backward Ps Times (default = 1) (CUB).
1971 this.cursorBackward(this.params
);
1975 // Cursor Position [row;column] (default = [1,1]) (CUP).
1977 this.cursorPos(this.params
);
1980 // CSI Ps J Erase in Display (ED).
1982 this.eraseInDisplay(this.params
);
1985 // CSI Ps K Erase in Line (EL).
1987 this.eraseInLine(this.params
);
1990 // CSI Pm m Character Attributes (SGR).
1993 this.charAttributes(this.params
);
1997 // CSI Ps n Device Status Report (DSR).
2000 this.deviceStatus(this.params
);
2009 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
2011 this.insertChars(this.params
);
2015 // Cursor Next Line Ps Times (default = 1) (CNL).
2017 this.cursorNextLine(this.params
);
2021 // Cursor Preceding Line Ps Times (default = 1) (CNL).
2023 this.cursorPrecedingLine(this.params
);
2027 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
2029 this.cursorCharAbsolute(this.params
);
2033 // Insert Ps Line(s) (default = 1) (IL).
2035 this.insertLines(this.params
);
2039 // Delete Ps Line(s) (default = 1) (DL).
2041 this.deleteLines(this.params
);
2045 // Delete Ps Character(s) (default = 1) (DCH).
2047 this.deleteChars(this.params
);
2051 // Erase Ps Character(s) (default = 1) (ECH).
2053 this.eraseChars(this.params
);
2056 // CSI Pm ` Character Position Absolute
2057 // [column] (default = [row,1]) (HPA).
2059 this.charPosAbsolute(this.params
);
2063 // Horizontal Position Relative
2065 this.HPositionRelative(this.params
);
2069 // Send Device Attributes (Primary DA).
2071 // Send Device Attributes (Secondary DA)
2073 this.sendDeviceAttributes(this.params
);
2077 // Line Position Absolute [row] (default = [1,column]) (VPA).
2079 this.linePosAbsolute(this.params
);
2082 // 145 65 e * VPR - Vertical Position Relative
2084 this.VPositionRelative(this.params
);
2088 // Horizontal and Vertical Position [row;column] (default =
2091 this.HVPosition(this.params
);
2094 // CSI Pm h Set Mode (SM).
2095 // CSI ? Pm h - mouse escape codes, cursor escape codes
2097 this.setMode(this.params
);
2100 // CSI Pm l Reset Mode (RM).
2103 this.resetMode(this.params
);
2107 // Set Scrolling Region [top;bottom] (default = full size of win-
2111 this.setScrollRegion(this.params
);
2115 // Save cursor (ANSI.SYS).
2117 this.saveCursor(this.params
);
2121 // Restore cursor (ANSI.SYS).
2123 this.restoreCursor(this.params
);
2131 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
2133 this.cursorForwardTab(this.params
);
2136 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
2138 this.scrollUp(this.params
);
2141 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
2142 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
2145 // if (this.prefix === '>') {
2146 // this.resetTitleModes(this.params);
2149 // if (this.params.length > 2) {
2150 // this.initMouseTracking(this.params);
2153 if (this.params
.length
< 2 && !this.prefix
) {
2154 this.scrollDown(this.params
);
2159 // Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
2161 this.cursorBackwardTab(this.params
);
2164 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
2166 this.repeatPrecedingCharacter(this.params
);
2169 // CSI Ps g Tab Clear (TBC).
2171 this.tabClear(this.params
);
2174 // CSI Pm i Media Copy (MC).
2177 // this.mediaCopy(this.params);
2180 // CSI Pm m Character Attributes (SGR).
2182 // case 'm': // duplicate
2183 // if (this.prefix === '>') {
2184 // this.setResources(this.params);
2186 // this.charAttributes(this.params);
2190 // CSI Ps n Device Status Report (DSR).
2192 // case 'n': // duplicate
2193 // if (this.prefix === '>') {
2194 // this.disableModifiers(this.params);
2196 // this.deviceStatus(this.params);
2200 // CSI > Ps p Set pointer mode.
2201 // CSI ! p Soft terminal reset (DECSTR).
2203 // Request ANSI mode (DECRQM).
2205 // Request DEC private mode (DECRQM).
2208 switch (this.prefix
) {
2210 // this.setPointerMode(this.params);
2213 this.softReset(this.params
);
2216 // if (this.postfix === '$') {
2217 // this.requestPrivateMode(this.params);
2221 // if (this.postfix === '"') {
2222 // this.setConformanceLevel(this.params);
2223 // } else if (this.postfix === '$') {
2224 // this.requestAnsiMode(this.params);
2230 // CSI Ps q Load LEDs (DECLL).
2234 // if (this.postfix === ' ') {
2235 // this.setCursorStyle(this.params);
2238 // if (this.postfix === '"') {
2239 // this.setCharProtectionAttr(this.params);
2242 // this.loadLEDs(this.params);
2246 // Set Scrolling Region [top;bottom] (default = full size of win-
2249 // CSI Pt; Pl; Pb; Pr; Ps$ r
2250 // case 'r': // duplicate
2251 // if (this.prefix === '?') {
2252 // this.restorePrivateValues(this.params);
2253 // } else if (this.postfix === '$') {
2254 // this.setAttrInRectangle(this.params);
2256 // this.setScrollRegion(this.params);
2260 // CSI s Save cursor (ANSI.SYS).
2262 // case 's': // duplicate
2263 // if (this.prefix === '?') {
2264 // this.savePrivateValues(this.params);
2266 // this.saveCursor(this.params);
2270 // CSI Ps ; Ps ; Ps t
2271 // CSI Pt; Pl; Pb; Pr; Ps$ t
2275 // if (this.postfix === '$') {
2276 // this.reverseAttrInRectangle(this.params);
2277 // } else if (this.postfix === ' ') {
2278 // this.setWarningBellVolume(this.params);
2280 // if (this.prefix === '>') {
2281 // this.setTitleModeFeature(this.params);
2283 // this.manipulateWindow(this.params);
2288 // CSI u Restore cursor (ANSI.SYS).
2290 // case 'u': // duplicate
2291 // if (this.postfix === ' ') {
2292 // this.setMarginBellVolume(this.params);
2294 // this.restoreCursor(this.params);
2298 // CSI Pt; Pl; Pb; Pr; Pp; Pt; Pl; Pp$ v
2300 // if (this.postfix === '$') {
2301 // this.copyRectagle(this.params);
2305 // CSI Pt ; Pl ; Pb ; Pr ' w
2307 // if (this.postfix === '\'') {
2308 // this.enableFilterRectangle(this.params);
2312 // CSI Ps x Request Terminal Parameters (DECREQTPARM).
2313 // CSI Ps x Select Attribute Change Extent (DECSACE).
2314 // CSI Pc; Pt; Pl; Pb; Pr$ x
2316 // if (this.postfix === '$') {
2317 // this.fillRectangle(this.params);
2319 // this.requestParameters(this.params);
2320 // //this.__(this.params);
2325 // CSI Pt; Pl; Pb; Pr$ z
2327 // if (this.postfix === '\'') {
2328 // this.enableLocatorReporting(this.params);
2329 // } else if (this.postfix === '$') {
2330 // this.eraseRectangle(this.params);
2335 // CSI Pt; Pl; Pb; Pr$ {
2337 // if (this.postfix === '\'') {
2338 // this.setLocatorEvents(this.params);
2339 // } else if (this.postfix === '$') {
2340 // this.selectiveEraseRectangle(this.params);
2346 // if (this.postfix === '\'') {
2347 // this.requestLocatorPosition(this.params);
2352 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
2354 // if (this.postfix === ' ') {
2355 // this.insertColumns(this.params);
2360 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
2362 // if (this.postfix === ' ') {
2363 // this.deleteColumns(this.params);
2368 this.error('Unknown CSI code: %s.', ch
);
2377 if (ch
=== '\x1b' || ch
=== '\x07') {
2378 if (ch
=== '\x1b') i
++;
2380 switch (this.prefix
) {
2381 // User-Defined Keys (DECUDK).
2385 // Request Status String (DECRQSS).
2386 // test: echo -e '\eP$q"p\e\\'
2388 var pt
= this.currentParam
2405 + (this.scrollTop
+ 1)
2407 + (this.scrollBottom
+ 1)
2417 this.error('Unknown DCS Pt: %s.', pt
);
2422 this.send('\x1bP' + +valid
+ '$r' + pt
+ '\x1b\\');
2425 // Set Termcap/Terminfo Data (xterm, experimental).
2429 // Request Termcap/Terminfo String (xterm, experimental)
2430 // Regular xterm does not even respond to this sequence.
2431 // This can cause a small glitch in vim.
2432 // test: echo -ne '\eP+q6b64\e\\'
2434 var pt
= this.currentParam
2437 this.send('\x1bP' + +valid
+ '+r' + pt
+ '\x1b\\');
2441 this.error('Unknown DCS prefix: %s.', this.prefix
);
2445 this.currentParam
= 0;
2447 this.state
= normal
;
2448 } else if (!this.currentParam
) {
2449 if (!this.prefix
&& ch
!== '$' && ch
!== '+') {
2450 this.currentParam
= ch
;
2451 } else if (this.prefix
.length
=== 2) {
2452 this.currentParam
= ch
;
2457 this.currentParam
+= ch
;
2463 if (ch
=== '\x1b' || ch
=== '\x07') {
2464 if (ch
=== '\x1b') i
++;
2465 this.state
= normal
;
2471 this.updateRange(this.y
);
2472 this.refresh(this.refreshStart
, this.refreshEnd
);
2476 * Writes text to the terminal, followed by a break line character (\n).
2477 * @param {string} text The text to write to the terminal.
2479 Terminal
.prototype.writeln = function(data
) {
2480 this.write(data
+ '\r\n');
2484 * Handle a keydown event
2486 * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
2487 * @param {KeyboardEvent} ev The keydown event to be handled.
2489 Terminal
.prototype.keyDown = function(ev
) {
2490 // TODO: Ignore event if currently composing text
2493 var result
= this.evaluateKeyEscapeSequence(ev
);
2495 if (result
.scrollDisp
) {
2496 this.scrollDisp(result
.scrollDisp
);
2497 return this.cancel(ev
);
2500 if (isThirdLevelShift(this, ev
)) {
2504 if (result
.cancel
) {
2505 // The event is canceled at the end already, is this necessary?
2506 this.cancel(ev
, true);
2513 this.emit('keydown', ev
);
2514 this.emit('key', result
.key
, ev
);
2516 this.handler(result
.key
);
2518 return this.cancel(ev
, true);
2522 * Returns an object that determines how a KeyboardEvent should be handled. The key of the
2523 * returned value is the new key code to pass to the PTY.
2525 * Reference: http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
2526 * @param {KeyboardEvent} ev The keyboard event to be translated to key escape sequence.
2528 Terminal
.prototype.evaluateKeyEscapeSequence = function(ev
) {
2530 // Whether to cancel event propogation (NOTE: this may not be needed since the event is
2531 // canceled at the end of keyDown
2533 // The new key even to emit
2535 // The number of characters to scroll, if this is defined it will cancel the event
2536 scrollDisp
: undefined
2538 switch (ev
.keyCode
) {
2542 result
.key
= '\x08'; // ^H
2545 result
.key
= '\x7f'; // ^?
2550 result
.key
= '\x1b[Z';
2554 result
.cancel
= true;
2559 result
.cancel
= true;
2563 result
.key
= '\x1b';
2564 result
.cancel
= true;
2569 result
.key
= '\x1bb' // Jump a word back
2570 result
.cancel
= true;
2574 result
.key
= '\x1b[5D'; // Jump a word back
2577 if (this.applicationCursor
) {
2578 result
.key
= '\x1bOD'; // SS3 as ^[O for 7-bit
2581 result
.key
= '\x1b[D';
2586 result
.key
= '\x1bf' // Jump a word forward
2587 result
.cancel
= true;
2591 result
.key
= '\x1b[5C'; // Jump a word forward
2594 if (this.applicationCursor
) {
2595 result
.key
= '\x1bOC';
2598 result
.key
= '\x1b[C';
2602 if (this.applicationCursor
) {
2603 result
.key
= '\x1bOA';
2607 result
.scrollDisp
= -1;
2609 result
.key
= '\x1b[A';
2614 if (this.applicationCursor
) {
2615 result
.key
= '\x1bOB';
2619 result
.scrollDisp
= 1;
2621 result
.key
= '\x1b[B';
2626 if (!ev
.shiftKey
&& !ev
.ctrlKey
) {
2627 // <Ctrl> or <Shift> + <Insert> are used to
2628 // copy-paste on some systems.
2629 result
.key
= '\x1b[2~';
2633 case 46: result
.key
= '\x1b[3~'; break;
2636 if (this.applicationKeypad
) {
2637 result
.key
= '\x1bOH';
2640 result
.key
= '\x1bOH';
2644 if (this.applicationKeypad
) {
2645 result
.key
= '\x1bOF';
2648 result
.key
= '\x1bOF';
2653 result
.scrollDisp
= -(this.rows
- 1);
2655 result
.key
= '\x1b[5~';
2661 result
.scrollDisp
= this.rows
- 1;
2663 result
.key
= '\x1b[6~';
2667 case 112: result
.key
= '\x1bOP'; break;
2668 case 113: result
.key
= '\x1bOQ'; break;
2669 case 114: result
.key
= '\x1bOR'; break;
2670 case 115: result
.key
= '\x1bOS'; break;
2671 case 116: result
.key
= '\x1b[15~'; break;
2672 case 117: result
.key
= '\x1b[17~'; break;
2673 case 118: result
.key
= '\x1b[18~'; break;
2674 case 119: result
.key
= '\x1b[19~'; break;
2675 case 120: result
.key
= '\x1b[20~'; break;
2676 case 121: result
.key
= '\x1b[21~'; break;
2677 case 122: result
.key
= '\x1b[23~'; break;
2678 case 123: result
.key
= '\x1b[24~'; break;
2681 if (ev
.ctrlKey
&& !ev
.shiftKey
&& !ev
.altKey
&& !ev
.metaKey
) {
2682 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2683 result
.key
= String
.fromCharCode(ev
.keyCode
- 64);
2684 } else if (ev
.keyCode
=== 32) {
2686 result
.key
= String
.fromCharCode(0);
2687 } else if (ev
.keyCode
>= 51 && ev
.keyCode
<= 55) {
2688 // escape, file sep, group sep, record sep, unit sep
2689 result
.key
= String
.fromCharCode(ev
.keyCode
- 51 + 27);
2690 } else if (ev
.keyCode
=== 56) {
2692 result
.key
= String
.fromCharCode(127);
2693 } else if (ev
.keyCode
=== 219) {
2695 result
.key
= String
.fromCharCode(27);
2696 } else if (ev
.keyCode
=== 221) {
2698 result
.key
= String
.fromCharCode(29);
2700 } else if (!this.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) {
2701 // On Mac this is a third level shift. Use <Esc> instead.
2702 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2703 result
.key
= '\x1b' + String
.fromCharCode(ev
.keyCode
+ 32);
2704 } else if (ev
.keyCode
=== 192) {
2705 result
.key
= '\x1b`';
2706 } else if (ev
.keyCode
>= 48 && ev
.keyCode
<= 57) {
2707 result
.key
= '\x1b' + (ev
.keyCode
- 48);
2716 * Set the G level of the terminal
2719 Terminal
.prototype.setgLevel = function(g
) {
2721 this.charset
= this.charsets
[g
];
2725 * Set the charset for the given G level of the terminal
2729 Terminal
.prototype.setgCharset = function(g
, charset
) {
2730 this.charsets
[g
] = charset
;
2731 if (this.glevel
=== g
) {
2732 this.charset
= charset
;
2737 * Handle a keypress event.
2739 * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
2740 * @param {KeyboardEvent} ev The keypress event to be handled.
2742 Terminal
.prototype.keyPress = function(ev
) {
2749 } else if (ev
.which
== null) {
2751 } else if (ev
.which
!== 0 && ev
.charCode
!== 0) {
2758 (ev
.altKey
|| ev
.ctrlKey
|| ev
.metaKey
) && !isThirdLevelShift(this, ev
)
2763 key
= String
.fromCharCode(key
);
2765 this.emit('keypress', key
, ev
);
2766 this.emit('key', key
, ev
);
2774 * Send data for handling to the terminal
2775 * @param {string} data
2777 Terminal
.prototype.send = function(data
) {
2781 setTimeout(function() {
2782 self
.handler(self
.queue
);
2792 * Note: We could do sweet things with webaudio here
2794 Terminal
.prototype.bell = function() {
2795 if (!this.visualBell
) return;
2797 this.element
.style
.borderColor
= 'white';
2798 setTimeout(function() {
2799 self
.element
.style
.borderColor
= '';
2801 if (this.popOnBell
) this.focus();
2805 * Log the current state to the console.
2807 Terminal
.prototype.log = function() {
2808 if (!this.debug
) return;
2809 if (!this.context
.console
|| !this.context
.console
.log
) return;
2810 var args
= Array
.prototype.slice
.call(arguments
);
2811 this.context
.console
.log
.apply(this.context
.console
, args
);
2815 * Log the current state as error to the console.
2817 Terminal
.prototype.error = function() {
2818 if (!this.debug
) return;
2819 if (!this.context
.console
|| !this.context
.console
.error
) return;
2820 var args
= Array
.prototype.slice
.call(arguments
);
2821 this.context
.console
.error
.apply(this.context
.console
, args
);
2825 * Resizes the terminal.
2827 * @param {number} x The number of columns to resize to.
2828 * @param {number} y The number of rows to resize to.
2830 Terminal
.prototype.resize = function(x
, y
) {
2838 if (x
=== this.cols
&& y
=== this.rows
) {
2848 ch
= [this.defAttr
, ' ', 1]; // does xterm use the default attr?
2849 i
= this.lines
.length
;
2851 while (this.lines
[i
].length
< x
) {
2852 this.lines
[i
].push(ch
);
2856 i
= this.lines
.length
;
2858 while (this.lines
[i
].length
> x
) {
2859 this.lines
[i
].pop();
2872 // y is rows, not this.y
2873 if (this.lines
.length
< y
+ this.ybase
) {
2874 if (this.ybase
> 0 && this.lines
.length
<= this.ybase
+ this.y
+ addToY
+ 1) {
2875 // There is room above the buffer and there are no empty elements below the line,
2879 if (this.ydisp
> 0) {
2880 // Viewport is at the top of the buffer, must increase downwards
2884 // Add a blank line if there is no buffer left at the top to scroll to, or if there
2885 // are blank lines after the cursor
2886 this.lines
.push(this.blankLine());
2889 if (this.children
.length
< y
) {
2895 if (this.lines
.length
> y
+ this.ybase
) {
2896 if (this.lines
.length
> this.ybase
+ this.y
+ 1) {
2897 // The line is a blank line below the cursor, remove it
2900 // The line is the cursor, scroll down
2905 if (this.children
.length
> y
) {
2906 el
= this.children
.shift();
2908 el
.parentNode
.removeChild(el
);
2915 * Make sure that the cursor stays on screen
2929 this.scrollBottom
= y
- 1;
2931 this.refresh(0, this.rows
- 1);
2935 this.emit('resize', {terminal
: this, cols
: x
, rows
: y
});
2939 * Updates the range of rows to refresh
2940 * @param {number} y The number of rows to refresh next.
2942 Terminal
.prototype.updateRange = function(y
) {
2943 if (y
< this.refreshStart
) this.refreshStart
= y
;
2944 if (y
> this.refreshEnd
) this.refreshEnd
= y
;
2945 // if (y > this.refreshEnd) {
2946 // this.refreshEnd = y;
2947 // if (y > this.rows - 1) {
2948 // this.refreshEnd = this.rows - 1;
2954 * Set the range of refreshing to the maximyum value
2956 Terminal
.prototype.maxRange = function() {
2957 this.refreshStart
= 0;
2958 this.refreshEnd
= this.rows
- 1;
2961 Terminal
.prototype.setupStops = function(i
) {
2963 if (!this.tabs
[i
]) {
2964 i
= this.prevStop(i
);
2971 for (; i
< this.cols
; i
+= 8) {
2972 this.tabs
[i
] = true;
2976 Terminal
.prototype.prevStop = function(x
) {
2977 if (x
== null) x
= this.x
;
2978 while (!this.tabs
[--x
] && x
> 0);
2979 return x
>= this.cols
2984 Terminal
.prototype.nextStop = function(x
) {
2985 if (x
== null) x
= this.x
;
2986 while (!this.tabs
[++x
] && x
< this.cols
);
2987 return x
>= this.cols
2992 Terminal
.prototype.eraseRight = function(x
, y
) {
2993 var line
= this.lines
[this.ybase
+ y
]
2994 , ch
= [this.eraseAttr(), ' ', 1]; // xterm
2997 for (; x
< this.cols
; x
++) {
3001 this.updateRange(y
);
3004 Terminal
.prototype.eraseLeft = function(x
, y
) {
3005 var line
= this.lines
[this.ybase
+ y
]
3006 , ch
= [this.eraseAttr(), ' ', 1]; // xterm
3009 while (x
--) line
[x
] = ch
;
3011 this.updateRange(y
);
3014 Terminal
.prototype.eraseLine = function(y
) {
3015 this.eraseRight(0, y
);
3018 Terminal
.prototype.blankLine = function(cur
) {
3023 var ch
= [attr
, ' ', 1] // width defaults to 1 halfwidth character
3027 for (; i
< this.cols
; i
++) {
3034 Terminal
.prototype.ch = function(cur
) {
3036 ? [this.eraseAttr(), ' ', 1]
3037 : [this.defAttr
, ' ', 1];
3040 Terminal
.prototype.is = function(term
) {
3041 var name
= this.termName
;
3042 return (name
+ '').indexOf(term
) === 0;
3045 Terminal
.prototype.handler = function(data
) {
3046 this.emit('data', data
);
3049 Terminal
.prototype.handleTitle = function(title
) {
3050 this.emit('title', title
);
3057 // ESC D Index (IND is 0x84).
3058 Terminal
.prototype.index = function() {
3060 if (this.y
> this.scrollBottom
) {
3064 this.state
= normal
;
3067 // ESC M Reverse Index (RI is 0x8d).
3068 Terminal
.prototype.reverseIndex = function() {
3071 if (this.y
< this.scrollTop
) {
3073 // possibly move the code below to term.reverseScroll();
3074 // test: echo -ne '\e[1;1H\e[44m\eM\e[0m'
3075 // blankLine(true) is xterm/linux behavior
3076 this.lines
.splice(this.y
+ this.ybase
, 0, this.blankLine(true));
3077 j
= this.rows
- 1 - this.scrollBottom
;
3078 this.lines
.splice(this.rows
- 1 + this.ybase
- j
+ 1, 1);
3080 this.updateRange(this.scrollTop
);
3081 this.updateRange(this.scrollBottom
);
3083 this.state
= normal
;
3086 // ESC c Full Reset (RIS).
3087 Terminal
.prototype.reset = function() {
3088 this.options
.rows
= this.rows
;
3089 this.options
.cols
= this.cols
;
3090 Terminal
.call(this, this.options
);
3091 this.refresh(0, this.rows
- 1);
3094 // ESC H Tab Set (HTS is 0x88).
3095 Terminal
.prototype.tabSet = function() {
3096 this.tabs
[this.x
] = true;
3097 this.state
= normal
;
3105 // Cursor Up Ps Times (default = 1) (CUU).
3106 Terminal
.prototype.cursorUp = function(params
) {
3107 var param
= params
[0];
3108 if (param
< 1) param
= 1;
3110 if (this.y
< 0) this.y
= 0;
3114 // Cursor Down Ps Times (default = 1) (CUD).
3115 Terminal
.prototype.cursorDown = function(params
) {
3116 var param
= params
[0];
3117 if (param
< 1) param
= 1;
3119 if (this.y
>= this.rows
) {
3120 this.y
= this.rows
- 1;
3125 // Cursor Forward Ps Times (default = 1) (CUF).
3126 Terminal
.prototype.cursorForward = function(params
) {
3127 var param
= params
[0];
3128 if (param
< 1) param
= 1;
3130 if (this.x
>= this.cols
) {
3131 this.x
= this.cols
- 1;
3136 // Cursor Backward Ps Times (default = 1) (CUB).
3137 Terminal
.prototype.cursorBackward = function(params
) {
3138 var param
= params
[0];
3139 if (param
< 1) param
= 1;
3141 if (this.x
< 0) this.x
= 0;
3145 // Cursor Position [row;column] (default = [1,1]) (CUP).
3146 Terminal
.prototype.cursorPos = function(params
) {
3149 row
= params
[0] - 1;
3151 if (params
.length
>= 2) {
3152 col
= params
[1] - 1;
3159 } else if (row
>= this.rows
) {
3160 row
= this.rows
- 1;
3165 } else if (col
>= this.cols
) {
3166 col
= this.cols
- 1;
3173 // CSI Ps J Erase in Display (ED).
3174 // Ps = 0 -> Erase Below (default).
3175 // Ps = 1 -> Erase Above.
3176 // Ps = 2 -> Erase All.
3177 // Ps = 3 -> Erase Saved Lines (xterm).
3179 // Erase in Display (DECSED).
3180 // Ps = 0 -> Selective Erase Below (default).
3181 // Ps = 1 -> Selective Erase Above.
3182 // Ps = 2 -> Selective Erase All.
3183 Terminal
.prototype.eraseInDisplay = function(params
) {
3185 switch (params
[0]) {
3187 this.eraseRight(this.x
, this.y
);
3189 for (; j
< this.rows
; j
++) {
3194 this.eraseLeft(this.x
, this.y
);
3202 while (j
--) this.eraseLine(j
);
3210 // CSI Ps K Erase in Line (EL).
3211 // Ps = 0 -> Erase to Right (default).
3212 // Ps = 1 -> Erase to Left.
3213 // Ps = 2 -> Erase All.
3215 // Erase in Line (DECSEL).
3216 // Ps = 0 -> Selective Erase to Right (default).
3217 // Ps = 1 -> Selective Erase to Left.
3218 // Ps = 2 -> Selective Erase All.
3219 Terminal
.prototype.eraseInLine = function(params
) {
3220 switch (params
[0]) {
3222 this.eraseRight(this.x
, this.y
);
3225 this.eraseLeft(this.x
, this.y
);
3228 this.eraseLine(this.y
);
3233 // CSI Pm m Character Attributes (SGR).
3234 // Ps = 0 -> Normal (default).
3236 // Ps = 4 -> Underlined.
3237 // Ps = 5 -> Blink (appears as Bold).
3238 // Ps = 7 -> Inverse.
3239 // Ps = 8 -> Invisible, i.e., hidden (VT300).
3240 // Ps = 2 2 -> Normal (neither bold nor faint).
3241 // Ps = 2 4 -> Not underlined.
3242 // Ps = 2 5 -> Steady (not blinking).
3243 // Ps = 2 7 -> Positive (not inverse).
3244 // Ps = 2 8 -> Visible, i.e., not hidden (VT300).
3245 // Ps = 3 0 -> Set foreground color to Black.
3246 // Ps = 3 1 -> Set foreground color to Red.
3247 // Ps = 3 2 -> Set foreground color to Green.
3248 // Ps = 3 3 -> Set foreground color to Yellow.
3249 // Ps = 3 4 -> Set foreground color to Blue.
3250 // Ps = 3 5 -> Set foreground color to Magenta.
3251 // Ps = 3 6 -> Set foreground color to Cyan.
3252 // Ps = 3 7 -> Set foreground color to White.
3253 // Ps = 3 9 -> Set foreground color to default (original).
3254 // Ps = 4 0 -> Set background color to Black.
3255 // Ps = 4 1 -> Set background color to Red.
3256 // Ps = 4 2 -> Set background color to Green.
3257 // Ps = 4 3 -> Set background color to Yellow.
3258 // Ps = 4 4 -> Set background color to Blue.
3259 // Ps = 4 5 -> Set background color to Magenta.
3260 // Ps = 4 6 -> Set background color to Cyan.
3261 // Ps = 4 7 -> Set background color to White.
3262 // Ps = 4 9 -> Set background color to default (original).
3264 // If 16-color support is compiled, the following apply. Assume
3265 // that xterm's resources are set so that the ISO color codes are
3266 // the first 8 of a set of 16. Then the aixterm colors are the
3267 // bright versions of the ISO colors:
3268 // Ps = 9 0 -> Set foreground color to Black.
3269 // Ps = 9 1 -> Set foreground color to Red.
3270 // Ps = 9 2 -> Set foreground color to Green.
3271 // Ps = 9 3 -> Set foreground color to Yellow.
3272 // Ps = 9 4 -> Set foreground color to Blue.
3273 // Ps = 9 5 -> Set foreground color to Magenta.
3274 // Ps = 9 6 -> Set foreground color to Cyan.
3275 // Ps = 9 7 -> Set foreground color to White.
3276 // Ps = 1 0 0 -> Set background color to Black.
3277 // Ps = 1 0 1 -> Set background color to Red.
3278 // Ps = 1 0 2 -> Set background color to Green.
3279 // Ps = 1 0 3 -> Set background color to Yellow.
3280 // Ps = 1 0 4 -> Set background color to Blue.
3281 // Ps = 1 0 5 -> Set background color to Magenta.
3282 // Ps = 1 0 6 -> Set background color to Cyan.
3283 // Ps = 1 0 7 -> Set background color to White.
3285 // If xterm is compiled with the 16-color support disabled, it
3286 // supports the following, from rxvt:
3287 // Ps = 1 0 0 -> Set foreground and background color to
3290 // If 88- or 256-color support is compiled, the following apply.
3291 // Ps = 3 8 ; 5 ; Ps -> Set foreground color to the second
3293 // Ps = 4 8 ; 5 ; Ps -> Set background color to the second
3295 Terminal
.prototype.charAttributes = function(params
) {
3296 // Optimize a single SGR0.
3297 if (params
.length
=== 1 && params
[0] === 0) {
3298 this.curAttr
= this.defAttr
;
3302 var l
= params
.length
3304 , flags
= this.curAttr
>> 18
3305 , fg
= (this.curAttr
>> 9) & 0x1ff
3306 , bg
= this.curAttr
& 0x1ff
3309 for (; i
< l
; i
++) {
3311 if (p
>= 30 && p
<= 37) {
3314 } else if (p
>= 40 && p
<= 47) {
3317 } else if (p
>= 90 && p
<= 97) {
3321 } else if (p
>= 100 && p
<= 107) {
3325 } else if (p
=== 0) {
3327 flags
= this.defAttr
>> 18;
3328 fg
= (this.defAttr
>> 9) & 0x1ff;
3329 bg
= this.defAttr
& 0x1ff;
3333 } else if (p
=== 1) {
3336 } else if (p
=== 4) {
3339 } else if (p
=== 5) {
3342 } else if (p
=== 7) {
3343 // inverse and positive
3344 // test with: echo -e '\e[31m\e[42mhello\e[7mworld\e[27mhi\e[m'
3346 } else if (p
=== 8) {
3349 } else if (p
=== 22) {
3352 } else if (p
=== 24) {
3355 } else if (p
=== 25) {
3358 } else if (p
=== 27) {
3361 } else if (p
=== 28) {
3364 } else if (p
=== 39) {
3366 fg
= (this.defAttr
>> 9) & 0x1ff;
3367 } else if (p
=== 49) {
3369 bg
= this.defAttr
& 0x1ff;
3370 } else if (p
=== 38) {
3372 if (params
[i
+ 1] === 2) {
3376 params
[i
+ 1] & 0xff,
3377 params
[i
+ 2] & 0xff);
3378 if (fg
=== -1) fg
= 0x1ff;
3380 } else if (params
[i
+ 1] === 5) {
3382 p
= params
[i
] & 0xff;
3385 } else if (p
=== 48) {
3387 if (params
[i
+ 1] === 2) {
3391 params
[i
+ 1] & 0xff,
3392 params
[i
+ 2] & 0xff);
3393 if (bg
=== -1) bg
= 0x1ff;
3395 } else if (params
[i
+ 1] === 5) {
3397 p
= params
[i
] & 0xff;
3400 } else if (p
=== 100) {
3402 fg
= (this.defAttr
>> 9) & 0x1ff;
3403 bg
= this.defAttr
& 0x1ff;
3405 this.error('Unknown SGR attribute: %d.', p
);
3409 this.curAttr
= (flags
<< 18) | (fg
<< 9) | bg
;
3412 // CSI Ps n Device Status Report (DSR).
3413 // Ps = 5 -> Status Report. Result (``OK'') is
3415 // Ps = 6 -> Report Cursor Position (CPR) [row;column].
3419 // Device Status Report (DSR, DEC-specific).
3420 // Ps = 6 -> Report Cursor Position (CPR) [row;column] as CSI
3421 // ? r ; c R (assumes page is zero).
3422 // Ps = 1 5 -> Report Printer status as CSI ? 1 0 n (ready).
3423 // or CSI ? 1 1 n (not ready).
3424 // Ps = 2 5 -> Report UDK status as CSI ? 2 0 n (unlocked)
3425 // or CSI ? 2 1 n (locked).
3426 // Ps = 2 6 -> Report Keyboard status as
3427 // CSI ? 2 7 ; 1 ; 0 ; 0 n (North American).
3428 // The last two parameters apply to VT400 & up, and denote key-
3429 // board ready and LK01 respectively.
3430 // Ps = 5 3 -> Report Locator status as
3431 // CSI ? 5 3 n Locator available, if compiled-in, or
3432 // CSI ? 5 0 n No Locator, if not.
3433 Terminal
.prototype.deviceStatus = function(params
) {
3435 switch (params
[0]) {
3438 this.send('\x1b[0n');
3449 } else if (this.prefix
=== '?') {
3450 // modern xterm doesnt seem to
3451 // respond to any of these except ?6, 6, and 5
3452 switch (params
[0]) {
3463 // this.send('\x1b[?11n');
3466 // dont support user defined keys
3467 // this.send('\x1b[?21n');
3470 // north american keyboard
3471 // this.send('\x1b[?27;1;0;0n');
3474 // no dec locator/mouse
3475 // this.send('\x1b[?50n');
3486 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
3487 Terminal
.prototype.insertChars = function(params
) {
3488 var param
, row
, j
, ch
;
3491 if (param
< 1) param
= 1;
3493 row
= this.y
+ this.ybase
;
3495 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3497 while (param
-- && j
< this.cols
) {
3498 this.lines
[row
].splice(j
++, 0, ch
);
3499 this.lines
[row
].pop();
3504 // Cursor Next Line Ps Times (default = 1) (CNL).
3505 // same as CSI Ps B ?
3506 Terminal
.prototype.cursorNextLine = function(params
) {
3507 var param
= params
[0];
3508 if (param
< 1) param
= 1;
3510 if (this.y
>= this.rows
) {
3511 this.y
= this.rows
- 1;
3517 // Cursor Preceding Line Ps Times (default = 1) (CNL).
3519 Terminal
.prototype.cursorPrecedingLine = function(params
) {
3520 var param
= params
[0];
3521 if (param
< 1) param
= 1;
3523 if (this.y
< 0) this.y
= 0;
3528 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
3529 Terminal
.prototype.cursorCharAbsolute = function(params
) {
3530 var param
= params
[0];
3531 if (param
< 1) param
= 1;
3536 // Insert Ps Line(s) (default = 1) (IL).
3537 Terminal
.prototype.insertLines = function(params
) {
3541 if (param
< 1) param
= 1;
3542 row
= this.y
+ this.ybase
;
3544 j
= this.rows
- 1 - this.scrollBottom
;
3545 j
= this.rows
- 1 + this.ybase
- j
+ 1;
3548 // test: echo -e '\e[44m\e[1L\e[0m'
3549 // blankLine(true) - xterm/linux behavior
3550 this.lines
.splice(row
, 0, this.blankLine(true));
3551 this.lines
.splice(j
, 1);
3555 this.updateRange(this.y
);
3556 this.updateRange(this.scrollBottom
);
3560 // Delete Ps Line(s) (default = 1) (DL).
3561 Terminal
.prototype.deleteLines = function(params
) {
3565 if (param
< 1) param
= 1;
3566 row
= this.y
+ this.ybase
;
3568 j
= this.rows
- 1 - this.scrollBottom
;
3569 j
= this.rows
- 1 + this.ybase
- j
;
3572 // test: echo -e '\e[44m\e[1M\e[0m'
3573 // blankLine(true) - xterm/linux behavior
3574 this.lines
.splice(j
+ 1, 0, this.blankLine(true));
3575 this.lines
.splice(row
, 1);
3579 this.updateRange(this.y
);
3580 this.updateRange(this.scrollBottom
);
3584 // Delete Ps Character(s) (default = 1) (DCH).
3585 Terminal
.prototype.deleteChars = function(params
) {
3589 if (param
< 1) param
= 1;
3591 row
= this.y
+ this.ybase
;
3592 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3595 this.lines
[row
].splice(this.x
, 1);
3596 this.lines
[row
].push(ch
);
3601 // Erase Ps Character(s) (default = 1) (ECH).
3602 Terminal
.prototype.eraseChars = function(params
) {
3603 var param
, row
, j
, ch
;
3606 if (param
< 1) param
= 1;
3608 row
= this.y
+ this.ybase
;
3610 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3612 while (param
-- && j
< this.cols
) {
3613 this.lines
[row
][j
++] = ch
;
3617 // CSI Pm ` Character Position Absolute
3618 // [column] (default = [row,1]) (HPA).
3619 Terminal
.prototype.charPosAbsolute = function(params
) {
3620 var param
= params
[0];
3621 if (param
< 1) param
= 1;
3623 if (this.x
>= this.cols
) {
3624 this.x
= this.cols
- 1;
3629 // Horizontal Position Relative
3631 Terminal
.prototype.HPositionRelative = function(params
) {
3632 var param
= params
[0];
3633 if (param
< 1) param
= 1;
3635 if (this.x
>= this.cols
) {
3636 this.x
= this.cols
- 1;
3640 // CSI Ps c Send Device Attributes (Primary DA).
3641 // Ps = 0 or omitted -> request attributes from terminal. The
3642 // response depends on the decTerminalID resource setting.
3643 // -> CSI ? 1 ; 2 c (``VT100 with Advanced Video Option'')
3644 // -> CSI ? 1 ; 0 c (``VT101 with No Options'')
3645 // -> CSI ? 6 c (``VT102'')
3646 // -> CSI ? 6 0 ; 1 ; 2 ; 6 ; 8 ; 9 ; 1 5 ; c (``VT220'')
3647 // The VT100-style response parameters do not mean anything by
3648 // themselves. VT220 parameters do, telling the host what fea-
3649 // tures the terminal supports:
3650 // Ps = 1 -> 132-columns.
3651 // Ps = 2 -> Printer.
3652 // Ps = 6 -> Selective erase.
3653 // Ps = 8 -> User-defined keys.
3654 // Ps = 9 -> National replacement character sets.
3655 // Ps = 1 5 -> Technical characters.
3656 // Ps = 2 2 -> ANSI color, e.g., VT525.
3657 // Ps = 2 9 -> ANSI text locator (i.e., DEC Locator mode).
3659 // Send Device Attributes (Secondary DA).
3660 // Ps = 0 or omitted -> request the terminal's identification
3661 // code. The response depends on the decTerminalID resource set-
3662 // ting. It should apply only to VT220 and up, but xterm extends
3664 // -> CSI > Pp ; Pv ; Pc c
3665 // where Pp denotes the terminal type
3666 // Pp = 0 -> ``VT100''.
3667 // Pp = 1 -> ``VT220''.
3668 // and Pv is the firmware version (for xterm, this was originally
3669 // the XFree86 patch number, starting with 95). In a DEC termi-
3670 // nal, Pc indicates the ROM cartridge registration number and is
3672 // More information:
3673 // xterm/charproc.c - line 2012, for more information.
3674 // vim responds with ^[[?0c or ^[[?1c after the terminal's response (?)
3675 Terminal
.prototype.sendDeviceAttributes = function(params
) {
3676 if (params
[0] > 0) return;
3679 if (this.is('xterm')
3680 || this.is('rxvt-unicode')
3681 || this.is('screen')) {
3682 this.send('\x1b[?1;2c');
3683 } else if (this.is('linux')) {
3684 this.send('\x1b[?6c');
3686 } else if (this.prefix
=== '>') {
3688 // seem to spit this
3689 // out around ~370 times (?).
3690 if (this.is('xterm')) {
3691 this.send('\x1b[>0;276;0c');
3692 } else if (this.is('rxvt-unicode')) {
3693 this.send('\x1b[>85;95;0c');
3694 } else if (this.is('linux')) {
3695 // not supported by linux console.
3696 // linux console echoes parameters.
3697 this.send(params
[0] + 'c');
3698 } else if (this.is('screen')) {
3699 this.send('\x1b[>83;40003;0c');
3705 // Line Position Absolute [row] (default = [1,column]) (VPA).
3706 Terminal
.prototype.linePosAbsolute = function(params
) {
3707 var param
= params
[0];
3708 if (param
< 1) param
= 1;
3710 if (this.y
>= this.rows
) {
3711 this.y
= this.rows
- 1;
3715 // 145 65 e * VPR - Vertical Position Relative
3717 Terminal
.prototype.VPositionRelative = function(params
) {
3718 var param
= params
[0];
3719 if (param
< 1) param
= 1;
3721 if (this.y
>= this.rows
) {
3722 this.y
= this.rows
- 1;
3727 // Horizontal and Vertical Position [row;column] (default =
3729 Terminal
.prototype.HVPosition = function(params
) {
3730 if (params
[0] < 1) params
[0] = 1;
3731 if (params
[1] < 1) params
[1] = 1;
3733 this.y
= params
[0] - 1;
3734 if (this.y
>= this.rows
) {
3735 this.y
= this.rows
- 1;
3738 this.x
= params
[1] - 1;
3739 if (this.x
>= this.cols
) {
3740 this.x
= this.cols
- 1;
3744 // CSI Pm h Set Mode (SM).
3745 // Ps = 2 -> Keyboard Action Mode (AM).
3746 // Ps = 4 -> Insert Mode (IRM).
3747 // Ps = 1 2 -> Send/receive (SRM).
3748 // Ps = 2 0 -> Automatic Newline (LNM).
3750 // DEC Private Mode Set (DECSET).
3751 // Ps = 1 -> Application Cursor Keys (DECCKM).
3752 // Ps = 2 -> Designate USASCII for character sets G0-G3
3753 // (DECANM), and set VT100 mode.
3754 // Ps = 3 -> 132 Column Mode (DECCOLM).
3755 // Ps = 4 -> Smooth (Slow) Scroll (DECSCLM).
3756 // Ps = 5 -> Reverse Video (DECSCNM).
3757 // Ps = 6 -> Origin Mode (DECOM).
3758 // Ps = 7 -> Wraparound Mode (DECAWM).
3759 // Ps = 8 -> Auto-repeat Keys (DECARM).
3760 // Ps = 9 -> Send Mouse X & Y on button press. See the sec-
3761 // tion Mouse Tracking.
3762 // Ps = 1 0 -> Show toolbar (rxvt).
3763 // Ps = 1 2 -> Start Blinking Cursor (att610).
3764 // Ps = 1 8 -> Print form feed (DECPFF).
3765 // Ps = 1 9 -> Set print extent to full screen (DECPEX).
3766 // Ps = 2 5 -> Show Cursor (DECTCEM).
3767 // Ps = 3 0 -> Show scrollbar (rxvt).
3768 // Ps = 3 5 -> Enable font-shifting functions (rxvt).
3769 // Ps = 3 8 -> Enter Tektronix Mode (DECTEK).
3770 // Ps = 4 0 -> Allow 80 -> 132 Mode.
3771 // Ps = 4 1 -> more(1) fix (see curses resource).
3772 // Ps = 4 2 -> Enable Nation Replacement Character sets (DECN-
3774 // Ps = 4 4 -> Turn On Margin Bell.
3775 // Ps = 4 5 -> Reverse-wraparound Mode.
3776 // Ps = 4 6 -> Start Logging. This is normally disabled by a
3777 // compile-time option.
3778 // Ps = 4 7 -> Use Alternate Screen Buffer. (This may be dis-
3779 // abled by the titeInhibit resource).
3780 // Ps = 6 6 -> Application keypad (DECNKM).
3781 // Ps = 6 7 -> Backarrow key sends backspace (DECBKM).
3782 // Ps = 1 0 0 0 -> Send Mouse X & Y on button press and
3783 // release. See the section Mouse Tracking.
3784 // Ps = 1 0 0 1 -> Use Hilite Mouse Tracking.
3785 // Ps = 1 0 0 2 -> Use Cell Motion Mouse Tracking.
3786 // Ps = 1 0 0 3 -> Use All Motion Mouse Tracking.
3787 // Ps = 1 0 0 4 -> Send FocusIn/FocusOut events.
3788 // Ps = 1 0 0 5 -> Enable Extended Mouse Mode.
3789 // Ps = 1 0 1 0 -> Scroll to bottom on tty output (rxvt).
3790 // Ps = 1 0 1 1 -> Scroll to bottom on key press (rxvt).
3791 // Ps = 1 0 3 4 -> Interpret "meta" key, sets eighth bit.
3792 // (enables the eightBitInput resource).
3793 // Ps = 1 0 3 5 -> Enable special modifiers for Alt and Num-
3794 // Lock keys. (This enables the numLock resource).
3795 // Ps = 1 0 3 6 -> Send ESC when Meta modifies a key. (This
3796 // enables the metaSendsEscape resource).
3797 // Ps = 1 0 3 7 -> Send DEL from the editing-keypad Delete
3799 // Ps = 1 0 3 9 -> Send ESC when Alt modifies a key. (This
3800 // enables the altSendsEscape resource).
3801 // Ps = 1 0 4 0 -> Keep selection even if not highlighted.
3802 // (This enables the keepSelection resource).
3803 // Ps = 1 0 4 1 -> Use the CLIPBOARD selection. (This enables
3804 // the selectToClipboard resource).
3805 // Ps = 1 0 4 2 -> Enable Urgency window manager hint when
3806 // Control-G is received. (This enables the bellIsUrgent
3808 // Ps = 1 0 4 3 -> Enable raising of the window when Control-G
3809 // is received. (enables the popOnBell resource).
3810 // Ps = 1 0 4 7 -> Use Alternate Screen Buffer. (This may be
3811 // disabled by the titeInhibit resource).
3812 // Ps = 1 0 4 8 -> Save cursor as in DECSC. (This may be dis-
3813 // abled by the titeInhibit resource).
3814 // Ps = 1 0 4 9 -> Save cursor as in DECSC and use Alternate
3815 // Screen Buffer, clearing it first. (This may be disabled by
3816 // the titeInhibit resource). This combines the effects of the 1
3817 // 0 4 7 and 1 0 4 8 modes. Use this with terminfo-based
3818 // applications rather than the 4 7 mode.
3819 // Ps = 1 0 5 0 -> Set terminfo/termcap function-key mode.
3820 // Ps = 1 0 5 1 -> Set Sun function-key mode.
3821 // Ps = 1 0 5 2 -> Set HP function-key mode.
3822 // Ps = 1 0 5 3 -> Set SCO function-key mode.
3823 // Ps = 1 0 6 0 -> Set legacy keyboard emulation (X11R6).
3824 // Ps = 1 0 6 1 -> Set VT220 keyboard emulation.
3825 // Ps = 2 0 0 4 -> Set bracketed paste mode.
3827 // http://vt100.net/docs/vt220-rm/chapter4.html
3828 Terminal
.prototype.setMode = function(params
) {
3829 if (typeof params
=== 'object') {
3830 var l
= params
.length
3833 for (; i
< l
; i
++) {
3834 this.setMode(params
[i
]);
3843 this.insertMode
= true;
3846 //this.convertEol = true;
3849 } else if (this.prefix
=== '?') {
3852 this.applicationCursor
= true;
3855 this.setgCharset(0, Terminal
.charsets
.US
);
3856 this.setgCharset(1, Terminal
.charsets
.US
);
3857 this.setgCharset(2, Terminal
.charsets
.US
);
3858 this.setgCharset(3, Terminal
.charsets
.US
);
3859 // set VT100 mode here
3861 case 3: // 132 col mode
3862 this.savedCols
= this.cols
;
3863 this.resize(132, this.rows
);
3866 this.originMode
= true;
3869 this.wraparoundMode
= true;
3872 // this.cursorBlink = true;
3875 this.log('Serial port requested application keypad.');
3876 this.applicationKeypad
= true;
3878 case 9: // X10 Mouse
3879 // no release, no motion, no wheel, no modifiers.
3880 case 1000: // vt200 mouse
3882 // no modifiers, except control on the wheel.
3883 case 1002: // button event mouse
3884 case 1003: // any event mouse
3885 // any event - sends motion events,
3886 // even if there is no button held down.
3887 this.x10Mouse
= params
=== 9;
3888 this.vt200Mouse
= params
=== 1000;
3889 this.normalMouse
= params
> 1000;
3890 this.mouseEvents
= true;
3891 this.element
.style
.cursor
= 'default';
3892 this.log('Binding to mouse events.');
3894 case 1004: // send focusin/focusout events
3897 this.sendFocus
= true;
3899 case 1005: // utf8 ext mode mouse
3900 this.utfMouse
= true;
3901 // for wide terminals
3902 // simply encodes large values as utf8 characters
3904 case 1006: // sgr ext mode mouse
3905 this.sgrMouse
= true;
3906 // for wide terminals
3907 // does not add 32 to fields
3908 // press: ^[[<b;x;yM
3909 // release: ^[[<b;x;ym
3911 case 1015: // urxvt ext mode mouse
3912 this.urxvtMouse
= true;
3913 // for wide terminals
3914 // numbers for fields
3916 // motion: ^[[b;x;yT
3918 case 25: // show cursor
3919 this.cursorHidden
= false;
3921 case 1049: // alt screen buffer cursor
3922 //this.saveCursor();
3924 case 47: // alt screen buffer
3925 case 1047: // alt screen buffer
3933 scrollTop
: this.scrollTop
,
3934 scrollBottom
: this.scrollBottom
,
3936 // XXX save charset(s) here?
3937 // charset: this.charset,
3938 // glevel: this.glevel,
3939 // charsets: this.charsets
3942 this.normal
= normal
;
3950 // CSI Pm l Reset Mode (RM).
3951 // Ps = 2 -> Keyboard Action Mode (AM).
3952 // Ps = 4 -> Replace Mode (IRM).
3953 // Ps = 1 2 -> Send/receive (SRM).
3954 // Ps = 2 0 -> Normal Linefeed (LNM).
3956 // DEC Private Mode Reset (DECRST).
3957 // Ps = 1 -> Normal Cursor Keys (DECCKM).
3958 // Ps = 2 -> Designate VT52 mode (DECANM).
3959 // Ps = 3 -> 80 Column Mode (DECCOLM).
3960 // Ps = 4 -> Jump (Fast) Scroll (DECSCLM).
3961 // Ps = 5 -> Normal Video (DECSCNM).
3962 // Ps = 6 -> Normal Cursor Mode (DECOM).
3963 // Ps = 7 -> No Wraparound Mode (DECAWM).
3964 // Ps = 8 -> No Auto-repeat Keys (DECARM).
3965 // Ps = 9 -> Don't send Mouse X & Y on button press.
3966 // Ps = 1 0 -> Hide toolbar (rxvt).
3967 // Ps = 1 2 -> Stop Blinking Cursor (att610).
3968 // Ps = 1 8 -> Don't print form feed (DECPFF).
3969 // Ps = 1 9 -> Limit print to scrolling region (DECPEX).
3970 // Ps = 2 5 -> Hide Cursor (DECTCEM).
3971 // Ps = 3 0 -> Don't show scrollbar (rxvt).
3972 // Ps = 3 5 -> Disable font-shifting functions (rxvt).
3973 // Ps = 4 0 -> Disallow 80 -> 132 Mode.
3974 // Ps = 4 1 -> No more(1) fix (see curses resource).
3975 // Ps = 4 2 -> Disable Nation Replacement Character sets (DEC-
3977 // Ps = 4 4 -> Turn Off Margin Bell.
3978 // Ps = 4 5 -> No Reverse-wraparound Mode.
3979 // Ps = 4 6 -> Stop Logging. (This is normally disabled by a
3980 // compile-time option).
3981 // Ps = 4 7 -> Use Normal Screen Buffer.
3982 // Ps = 6 6 -> Numeric keypad (DECNKM).
3983 // Ps = 6 7 -> Backarrow key sends delete (DECBKM).
3984 // Ps = 1 0 0 0 -> Don't send Mouse X & Y on button press and
3985 // release. See the section Mouse Tracking.
3986 // Ps = 1 0 0 1 -> Don't use Hilite Mouse Tracking.
3987 // Ps = 1 0 0 2 -> Don't use Cell Motion Mouse Tracking.
3988 // Ps = 1 0 0 3 -> Don't use All Motion Mouse Tracking.
3989 // Ps = 1 0 0 4 -> Don't send FocusIn/FocusOut events.
3990 // Ps = 1 0 0 5 -> Disable Extended Mouse Mode.
3991 // Ps = 1 0 1 0 -> Don't scroll to bottom on tty output
3993 // Ps = 1 0 1 1 -> Don't scroll to bottom on key press (rxvt).
3994 // Ps = 1 0 3 4 -> Don't interpret "meta" key. (This disables
3995 // the eightBitInput resource).
3996 // Ps = 1 0 3 5 -> Disable special modifiers for Alt and Num-
3997 // Lock keys. (This disables the numLock resource).
3998 // Ps = 1 0 3 6 -> Don't send ESC when Meta modifies a key.
3999 // (This disables the metaSendsEscape resource).
4000 // Ps = 1 0 3 7 -> Send VT220 Remove from the editing-keypad
4002 // Ps = 1 0 3 9 -> Don't send ESC when Alt modifies a key.
4003 // (This disables the altSendsEscape resource).
4004 // Ps = 1 0 4 0 -> Do not keep selection when not highlighted.
4005 // (This disables the keepSelection resource).
4006 // Ps = 1 0 4 1 -> Use the PRIMARY selection. (This disables
4007 // the selectToClipboard resource).
4008 // Ps = 1 0 4 2 -> Disable Urgency window manager hint when
4009 // Control-G is received. (This disables the bellIsUrgent
4011 // Ps = 1 0 4 3 -> Disable raising of the window when Control-
4012 // G is received. (This disables the popOnBell resource).
4013 // Ps = 1 0 4 7 -> Use Normal Screen Buffer, clearing screen
4014 // first if in the Alternate Screen. (This may be disabled by
4015 // the titeInhibit resource).
4016 // Ps = 1 0 4 8 -> Restore cursor as in DECRC. (This may be
4017 // disabled by the titeInhibit resource).
4018 // Ps = 1 0 4 9 -> Use Normal Screen Buffer and restore cursor
4019 // as in DECRC. (This may be disabled by the titeInhibit
4020 // resource). This combines the effects of the 1 0 4 7 and 1 0
4021 // 4 8 modes. Use this with terminfo-based applications rather
4022 // than the 4 7 mode.
4023 // Ps = 1 0 5 0 -> Reset terminfo/termcap function-key mode.
4024 // Ps = 1 0 5 1 -> Reset Sun function-key mode.
4025 // Ps = 1 0 5 2 -> Reset HP function-key mode.
4026 // Ps = 1 0 5 3 -> Reset SCO function-key mode.
4027 // Ps = 1 0 6 0 -> Reset legacy keyboard emulation (X11R6).
4028 // Ps = 1 0 6 1 -> Reset keyboard emulation to Sun/PC style.
4029 // Ps = 2 0 0 4 -> Reset bracketed paste mode.
4030 Terminal
.prototype.resetMode = function(params
) {
4031 if (typeof params
=== 'object') {
4032 var l
= params
.length
4035 for (; i
< l
; i
++) {
4036 this.resetMode(params
[i
]);
4045 this.insertMode
= false;
4048 //this.convertEol = false;
4051 } else if (this.prefix
=== '?') {
4054 this.applicationCursor
= false;
4057 if (this.cols
=== 132 && this.savedCols
) {
4058 this.resize(this.savedCols
, this.rows
);
4060 delete this.savedCols
;
4063 this.originMode
= false;
4066 this.wraparoundMode
= false;
4069 // this.cursorBlink = false;
4072 this.log('Switching back to normal keypad.');
4073 this.applicationKeypad
= false;
4075 case 9: // X10 Mouse
4076 case 1000: // vt200 mouse
4077 case 1002: // button event mouse
4078 case 1003: // any event mouse
4079 this.x10Mouse
= false;
4080 this.vt200Mouse
= false;
4081 this.normalMouse
= false;
4082 this.mouseEvents
= false;
4083 this.element
.style
.cursor
= '';
4085 case 1004: // send focusin/focusout events
4086 this.sendFocus
= false;
4088 case 1005: // utf8 ext mode mouse
4089 this.utfMouse
= false;
4091 case 1006: // sgr ext mode mouse
4092 this.sgrMouse
= false;
4094 case 1015: // urxvt ext mode mouse
4095 this.urxvtMouse
= false;
4097 case 25: // hide cursor
4098 this.cursorHidden
= true;
4100 case 1049: // alt screen buffer cursor
4102 case 47: // normal screen buffer
4103 case 1047: // normal screen buffer - clearing it first
4105 this.lines
= this.normal
.lines
;
4106 this.ybase
= this.normal
.ybase
;
4107 this.ydisp
= this.normal
.ydisp
;
4108 this.x
= this.normal
.x
;
4109 this.y
= this.normal
.y
;
4110 this.scrollTop
= this.normal
.scrollTop
;
4111 this.scrollBottom
= this.normal
.scrollBottom
;
4112 this.tabs
= this.normal
.tabs
;
4114 // if (params === 1049) {
4115 // this.x = this.savedX;
4116 // this.y = this.savedY;
4118 this.refresh(0, this.rows
- 1);
4127 // Set Scrolling Region [top;bottom] (default = full size of win-
4130 Terminal
.prototype.setScrollRegion = function(params
) {
4131 if (this.prefix
) return;
4132 this.scrollTop
= (params
[0] || 1) - 1;
4133 this.scrollBottom
= (params
[1] || this.rows
) - 1;
4139 // Save cursor (ANSI.SYS).
4140 Terminal
.prototype.saveCursor = function(params
) {
4141 this.savedX
= this.x
;
4142 this.savedY
= this.y
;
4146 // Restore cursor (ANSI.SYS).
4147 Terminal
.prototype.restoreCursor = function(params
) {
4148 this.x
= this.savedX
|| 0;
4149 this.y
= this.savedY
|| 0;
4157 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
4158 Terminal
.prototype.cursorForwardTab = function(params
) {
4159 var param
= params
[0] || 1;
4161 this.x
= this.nextStop();
4165 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
4166 Terminal
.prototype.scrollUp = function(params
) {
4167 var param
= params
[0] || 1;
4169 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
4170 this.lines
.splice(this.ybase
+ this.scrollBottom
, 0, this.blankLine());
4173 this.updateRange(this.scrollTop
);
4174 this.updateRange(this.scrollBottom
);
4177 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
4178 Terminal
.prototype.scrollDown = function(params
) {
4179 var param
= params
[0] || 1;
4181 this.lines
.splice(this.ybase
+ this.scrollBottom
, 1);
4182 this.lines
.splice(this.ybase
+ this.scrollTop
, 0, this.blankLine());
4185 this.updateRange(this.scrollTop
);
4186 this.updateRange(this.scrollBottom
);
4189 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
4190 // Initiate highlight mouse tracking. Parameters are
4191 // [func;startx;starty;firstrow;lastrow]. See the section Mouse
4193 Terminal
.prototype.initMouseTracking = function(params
) {
4194 // Relevant: DECSET 1001
4198 // Reset one or more features of the title modes to the default
4199 // value. Normally, "reset" disables the feature. It is possi-
4200 // ble to disable the ability to reset features by compiling a
4201 // different default for the title modes into xterm.
4202 // Ps = 0 -> Do not set window/icon labels using hexadecimal.
4203 // Ps = 1 -> Do not query window/icon labels using hexadeci-
4205 // Ps = 2 -> Do not set window/icon labels using UTF-8.
4206 // Ps = 3 -> Do not query window/icon labels using UTF-8.
4207 // (See discussion of "Title Modes").
4208 Terminal
.prototype.resetTitleModes = function(params
) {
4212 // CSI Ps Z Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
4213 Terminal
.prototype.cursorBackwardTab = function(params
) {
4214 var param
= params
[0] || 1;
4216 this.x
= this.prevStop();
4220 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
4221 Terminal
.prototype.repeatPrecedingCharacter = function(params
) {
4222 var param
= params
[0] || 1
4223 , line
= this.lines
[this.ybase
+ this.y
]
4224 , ch
= line
[this.x
- 1] || [this.defAttr
, ' ', 1];
4226 while (param
--) line
[this.x
++] = ch
;
4229 // CSI Ps g Tab Clear (TBC).
4230 // Ps = 0 -> Clear Current Column (default).
4231 // Ps = 3 -> Clear All.
4233 // Ps = 2 -> Clear Stops on Line.
4234 // http://vt100.net/annarbor/aaa-ug/section6.html
4235 Terminal
.prototype.tabClear = function(params
) {
4236 var param
= params
[0];
4238 delete this.tabs
[this.x
];
4239 } else if (param
=== 3) {
4244 // CSI Pm i Media Copy (MC).
4245 // Ps = 0 -> Print screen (default).
4246 // Ps = 4 -> Turn off printer controller mode.
4247 // Ps = 5 -> Turn on printer controller mode.
4249 // Media Copy (MC, DEC-specific).
4250 // Ps = 1 -> Print line containing cursor.
4251 // Ps = 4 -> Turn off autoprint mode.
4252 // Ps = 5 -> Turn on autoprint mode.
4253 // Ps = 1 0 -> Print composed display, ignores DECPEX.
4254 // Ps = 1 1 -> Print all pages.
4255 Terminal
.prototype.mediaCopy = function(params
) {
4260 // Set or reset resource-values used by xterm to decide whether
4261 // to construct escape sequences holding information about the
4262 // modifiers pressed with a given key. The first parameter iden-
4263 // tifies the resource to set/reset. The second parameter is the
4264 // value to assign to the resource. If the second parameter is
4265 // omitted, the resource is reset to its initial value.
4266 // Ps = 1 -> modifyCursorKeys.
4267 // Ps = 2 -> modifyFunctionKeys.
4268 // Ps = 4 -> modifyOtherKeys.
4269 // If no parameters are given, all resources are reset to their
4271 Terminal
.prototype.setResources = function(params
) {
4276 // Disable modifiers which may be enabled via the CSI > Ps; Ps m
4277 // sequence. This corresponds to a resource value of "-1", which
4278 // cannot be set with the other sequence. The parameter identi-
4279 // fies the resource to be disabled:
4280 // Ps = 1 -> modifyCursorKeys.
4281 // Ps = 2 -> modifyFunctionKeys.
4282 // Ps = 4 -> modifyOtherKeys.
4283 // If the parameter is omitted, modifyFunctionKeys is disabled.
4284 // When modifyFunctionKeys is disabled, xterm uses the modifier
4285 // keys to make an extended sequence of functions rather than
4286 // adding a parameter to each function key to denote the modi-
4288 Terminal
.prototype.disableModifiers = function(params
) {
4293 // Set resource value pointerMode. This is used by xterm to
4294 // decide whether to hide the pointer cursor as the user types.
4295 // Valid values for the parameter:
4296 // Ps = 0 -> never hide the pointer.
4297 // Ps = 1 -> hide if the mouse tracking mode is not enabled.
4298 // Ps = 2 -> always hide the pointer. If no parameter is
4299 // given, xterm uses the default, which is 1 .
4300 Terminal
.prototype.setPointerMode = function(params
) {
4304 // CSI ! p Soft terminal reset (DECSTR).
4305 // http://vt100.net/docs/vt220-rm/table4-10.html
4306 Terminal
.prototype.softReset = function(params
) {
4307 this.cursorHidden
= false;
4308 this.insertMode
= false;
4309 this.originMode
= false;
4310 this.wraparoundMode
= false; // autowrap
4311 this.applicationKeypad
= false; // ?
4312 this.applicationCursor
= false;
4314 this.scrollBottom
= this.rows
- 1;
4315 this.curAttr
= this.defAttr
;
4316 this.x
= this.y
= 0; // ?
4317 this.charset
= null;
4318 this.glevel
= 0; // ??
4319 this.charsets
= [null]; // ??
4323 // Request ANSI mode (DECRQM). For VT300 and up, reply is
4325 // where Ps is the mode number as in RM, and Pm is the mode
4327 // 0 - not recognized
4330 // 3 - permanently set
4331 // 4 - permanently reset
4332 Terminal
.prototype.requestAnsiMode = function(params
) {
4337 // Request DEC private mode (DECRQM). For VT300 and up, reply is
4339 // where Ps is the mode number as in DECSET, Pm is the mode value
4340 // as in the ANSI DECRQM.
4341 Terminal
.prototype.requestPrivateMode = function(params
) {
4346 // Set conformance level (DECSCL). Valid values for the first
4348 // Ps = 6 1 -> VT100.
4349 // Ps = 6 2 -> VT200.
4350 // Ps = 6 3 -> VT300.
4351 // Valid values for the second parameter:
4352 // Ps = 0 -> 8-bit controls.
4353 // Ps = 1 -> 7-bit controls (always set for VT100).
4354 // Ps = 2 -> 8-bit controls.
4355 Terminal
.prototype.setConformanceLevel = function(params
) {
4359 // CSI Ps q Load LEDs (DECLL).
4360 // Ps = 0 -> Clear all LEDS (default).
4361 // Ps = 1 -> Light Num Lock.
4362 // Ps = 2 -> Light Caps Lock.
4363 // Ps = 3 -> Light Scroll Lock.
4364 // Ps = 2 1 -> Extinguish Num Lock.
4365 // Ps = 2 2 -> Extinguish Caps Lock.
4366 // Ps = 2 3 -> Extinguish Scroll Lock.
4367 Terminal
.prototype.loadLEDs = function(params
) {
4372 // Set cursor style (DECSCUSR, VT520).
4373 // Ps = 0 -> blinking block.
4374 // Ps = 1 -> blinking block (default).
4375 // Ps = 2 -> steady block.
4376 // Ps = 3 -> blinking underline.
4377 // Ps = 4 -> steady underline.
4378 Terminal
.prototype.setCursorStyle = function(params
) {
4383 // Select character protection attribute (DECSCA). Valid values
4384 // for the parameter:
4385 // Ps = 0 -> DECSED and DECSEL can erase (default).
4386 // Ps = 1 -> DECSED and DECSEL cannot erase.
4387 // Ps = 2 -> DECSED and DECSEL can erase.
4388 Terminal
.prototype.setCharProtectionAttr = function(params
) {
4393 // Restore DEC Private Mode Values. The value of Ps previously
4394 // saved is restored. Ps values are the same as for DECSET.
4395 Terminal
.prototype.restorePrivateValues = function(params
) {
4399 // CSI Pt; Pl; Pb; Pr; Ps$ r
4400 // Change Attributes in Rectangular Area (DECCARA), VT400 and up.
4401 // Pt; Pl; Pb; Pr denotes the rectangle.
4402 // Ps denotes the SGR attributes to change: 0, 1, 4, 5, 7.
4403 // NOTE: xterm doesn't enable this code by default.
4404 Terminal
.prototype.setAttrInRectangle = function(params
) {
4414 for (; t
< b
+ 1; t
++) {
4415 line
= this.lines
[this.ybase
+ t
];
4416 for (i
= l
; i
< r
; i
++) {
4417 line
[i
] = [attr
, line
[i
][1]];
4422 this.updateRange(params
[0]);
4423 this.updateRange(params
[2]);
4427 // CSI Pc; Pt; Pl; Pb; Pr$ x
4428 // Fill Rectangular Area (DECFRA), VT420 and up.
4429 // Pc is the character to use.
4430 // Pt; Pl; Pb; Pr denotes the rectangle.
4431 // NOTE: xterm doesn't enable this code by default.
4432 Terminal
.prototype.fillRectangle = function(params
) {
4442 for (; t
< b
+ 1; t
++) {
4443 line
= this.lines
[this.ybase
+ t
];
4444 for (i
= l
; i
< r
; i
++) {
4445 line
[i
] = [line
[i
][0], String
.fromCharCode(ch
)];
4450 this.updateRange(params
[1]);
4451 this.updateRange(params
[3]);
4455 // Enable Locator Reporting (DECELR).
4456 // Valid values for the first parameter:
4457 // Ps = 0 -> Locator disabled (default).
4458 // Ps = 1 -> Locator enabled.
4459 // Ps = 2 -> Locator enabled for one report, then disabled.
4460 // The second parameter specifies the coordinate unit for locator
4462 // Valid values for the second parameter:
4463 // Pu = 0 <- or omitted -> default to character cells.
4464 // Pu = 1 <- device physical pixels.
4465 // Pu = 2 <- character cells.
4466 Terminal
.prototype.enableLocatorReporting = function(params
) {
4467 var val
= params
[0] > 0;
4468 //this.mouseEvents = val;
4469 //this.decLocator = val;
4472 // CSI Pt; Pl; Pb; Pr$ z
4473 // Erase Rectangular Area (DECERA), VT400 and up.
4474 // Pt; Pl; Pb; Pr denotes the rectangle.
4475 // NOTE: xterm doesn't enable this code by default.
4476 Terminal
.prototype.eraseRectangle = function(params
) {
4486 ch
= [this.eraseAttr(), ' ', 1]; // xterm?
4488 for (; t
< b
+ 1; t
++) {
4489 line
= this.lines
[this.ybase
+ t
];
4490 for (i
= l
; i
< r
; i
++) {
4496 this.updateRange(params
[0]);
4497 this.updateRange(params
[2]);
4502 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
4503 // NOTE: xterm doesn't enable this code by default.
4504 Terminal
.prototype.insertColumns = function() {
4505 var param
= params
[0]
4506 , l
= this.ybase
+ this.rows
4507 , ch
= [this.eraseAttr(), ' ', 1] // xterm?
4511 for (i
= this.ybase
; i
< l
; i
++) {
4512 this.lines
[i
].splice(this.x
+ 1, 0, ch
);
4513 this.lines
[i
].pop();
4522 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
4523 // NOTE: xterm doesn't enable this code by default.
4524 Terminal
.prototype.deleteColumns = 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);
4533 this.lines
[i
].push(ch
);
4544 Terminal
.charsets
= {};
4546 // DEC Special Character and Line Drawing Set.
4547 // http://vt100.net/docs/vt102-ug/table5-13.html
4548 // A lot of curses apps use this if they see TERM=xterm.
4549 // testing: echo -e '\e(0a\e(B'
4550 // The xterm output sometimes seems to conflict with the
4551 // reference above. xterm seems in line with the reference
4552 // when running vttest however.
4553 // The table below now uses xterm's output from vttest.
4554 Terminal
.charsets
.SCLD
= { // (0
4555 '`': '\u25c6', // '◆'
4556 'a': '\u2592', // '▒'
4557 'b': '\u0009', // '\t'
4558 'c': '\u000c', // '\f'
4559 'd': '\u000d', // '\r'
4560 'e': '\u000a', // '\n'
4561 'f': '\u00b0', // '°'
4562 'g': '\u00b1', // '±'
4563 'h': '\u2424', // '\u2424' (NL)
4564 'i': '\u000b', // '\v'
4565 'j': '\u2518', // '┘'
4566 'k': '\u2510', // '┐'
4567 'l': '\u250c', // '┌'
4568 'm': '\u2514', // '└'
4569 'n': '\u253c', // '┼'
4570 'o': '\u23ba', // '⎺'
4571 'p': '\u23bb', // '⎻'
4572 'q': '\u2500', // '─'
4573 'r': '\u23bc', // '⎼'
4574 's': '\u23bd', // '⎽'
4575 't': '\u251c', // '├'
4576 'u': '\u2524', // '┤'
4577 'v': '\u2534', // '┴'
4578 'w': '\u252c', // '┬'
4579 'x': '\u2502', // '│'
4580 'y': '\u2264', // '≤'
4581 'z': '\u2265', // '≥'
4582 '{': '\u03c0', // 'π'
4583 '|': '\u2260', // '≠'
4584 '}': '\u00a3', // '£'
4585 '~': '\u00b7' // '·'
4588 Terminal
.charsets
.UK
= null; // (A
4589 Terminal
.charsets
.US
= null; // (B (USASCII)
4590 Terminal
.charsets
.Dutch
= null; // (4
4591 Terminal
.charsets
.Finnish
= null; // (C or (5
4592 Terminal
.charsets
.French
= null; // (R
4593 Terminal
.charsets
.FrenchCanadian
= null; // (Q
4594 Terminal
.charsets
.German
= null; // (K
4595 Terminal
.charsets
.Italian
= null; // (Y
4596 Terminal
.charsets
.NorwegianDanish
= null; // (E or (6
4597 Terminal
.charsets
.Spanish
= null; // (Z
4598 Terminal
.charsets
.Swedish
= null; // (H or (7
4599 Terminal
.charsets
.Swiss
= null; // (=
4600 Terminal
.charsets
.ISOLatin
= null; // /A
4606 function contains(el
, arr
) {
4607 for (var i
= 0; i
< arr
.length
; i
+= 1) {
4608 if (el
=== arr
[i
]) {
4615 function on(el
, type
, handler
, capture
) {
4616 if (!Array
.isArray(el
)) {
4619 el
.forEach(function (element
) {
4620 element
.addEventListener(type
, handler
, capture
|| false);
4624 function off(el
, type
, handler
, capture
) {
4625 el
.removeEventListener(type
, handler
, capture
|| false);
4628 function cancel(ev
, force
) {
4629 if (!this.cancelEvents
&& !force
) {
4632 ev
.preventDefault();
4633 ev
.stopPropagation();
4637 function inherits(child
, parent
) {
4639 this.constructor = child
;
4641 f
.prototype = parent
.prototype;
4642 child
.prototype = new f
;
4645 // if bold is broken, we can't
4646 // use it in the terminal.
4647 function isBoldBroken(document
) {
4648 var body
= document
.getElementsByTagName('body')[0];
4649 var el
= document
.createElement('span');
4650 el
.innerHTML
= 'hello world';
4651 body
.appendChild(el
);
4652 var w1
= el
.scrollWidth
;
4653 el
.style
.fontWeight
= 'bold';
4654 var w2
= el
.scrollWidth
;
4655 body
.removeChild(el
);
4659 var String
= this.String
;
4660 var setTimeout
= this.setTimeout
;
4661 var setInterval
= this.setInterval
;
4663 function indexOf(obj
, el
) {
4666 if (obj
[i
] === el
) return i
;
4671 function isThirdLevelShift(term
, ev
) {
4673 (term
.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) ||
4674 (term
.isMSWindows
&& ev
.altKey
&& ev
.ctrlKey
&& !ev
.metaKey
);
4676 // Don't invoke for arrows, pageDown, home, backspace, etc.
4677 return thirdLevelKey
&& (!ev
.keyCode
|| ev
.keyCode
> 47);
4680 function matchColor(r1
, g1
, b1
) {
4681 var hash
= (r1
<< 16) | (g1
<< 8) | b1
;
4683 if (matchColor
._cache
[hash
] != null) {
4684 return matchColor
._cache
[hash
];
4687 var ldiff
= Infinity
4696 for (; i
< Terminal
.vcolors
.length
; i
++) {
4697 c
= Terminal
.vcolors
[i
];
4702 diff
= matchColor
.distance(r1
, g1
, b1
, r2
, g2
, b2
);
4715 return matchColor
._cache
[hash
] = li
;
4718 matchColor
._cache
= {};
4720 // http://stackoverflow.com/questions/1633828
4721 matchColor
.distance = function(r1
, g1
, b1
, r2
, g2
, b2
) {
4722 return Math
.pow(30 * (r1
- r2
), 2)
4723 + Math
.pow(59 * (g1
- g2
), 2)
4724 + Math
.pow(11 * (b1
- b2
), 2);
4727 function each(obj
, iter
, con
) {
4728 if (obj
.forEach
) return obj
.forEach(iter
, con
);
4729 for (var i
= 0; i
< obj
.length
; i
++) {
4730 iter
.call(con
, obj
[i
], i
, obj
);
4734 function keys(obj
) {
4735 if (Object
.keys
) return Object
.keys(obj
);
4738 if (Object
.prototype.hasOwnProperty
.call(obj
, key
)) {
4745 var wcwidth
= (function(opts
) {
4746 // extracted from https://www.cl.cam.ac.uk/%7Emgk25/ucs/wcwidth.c
4747 // combining characters
4749 [0x0300, 0x036F], [0x0483, 0x0486], [0x0488, 0x0489],
4750 [0x0591, 0x05BD], [0x05BF, 0x05BF], [0x05C1, 0x05C2],
4751 [0x05C4, 0x05C5], [0x05C7, 0x05C7], [0x0600, 0x0603],
4752 [0x0610, 0x0615], [0x064B, 0x065E], [0x0670, 0x0670],
4753 [0x06D6, 0x06E4], [0x06E7, 0x06E8], [0x06EA, 0x06ED],
4754 [0x070F, 0x070F], [0x0711, 0x0711], [0x0730, 0x074A],
4755 [0x07A6, 0x07B0], [0x07EB, 0x07F3], [0x0901, 0x0902],
4756 [0x093C, 0x093C], [0x0941, 0x0948], [0x094D, 0x094D],
4757 [0x0951, 0x0954], [0x0962, 0x0963], [0x0981, 0x0981],
4758 [0x09BC, 0x09BC], [0x09C1, 0x09C4], [0x09CD, 0x09CD],
4759 [0x09E2, 0x09E3], [0x0A01, 0x0A02], [0x0A3C, 0x0A3C],
4760 [0x0A41, 0x0A42], [0x0A47, 0x0A48], [0x0A4B, 0x0A4D],
4761 [0x0A70, 0x0A71], [0x0A81, 0x0A82], [0x0ABC, 0x0ABC],
4762 [0x0AC1, 0x0AC5], [0x0AC7, 0x0AC8], [0x0ACD, 0x0ACD],
4763 [0x0AE2, 0x0AE3], [0x0B01, 0x0B01], [0x0B3C, 0x0B3C],
4764 [0x0B3F, 0x0B3F], [0x0B41, 0x0B43], [0x0B4D, 0x0B4D],
4765 [0x0B56, 0x0B56], [0x0B82, 0x0B82], [0x0BC0, 0x0BC0],
4766 [0x0BCD, 0x0BCD], [0x0C3E, 0x0C40], [0x0C46, 0x0C48],
4767 [0x0C4A, 0x0C4D], [0x0C55, 0x0C56], [0x0CBC, 0x0CBC],
4768 [0x0CBF, 0x0CBF], [0x0CC6, 0x0CC6], [0x0CCC, 0x0CCD],
4769 [0x0CE2, 0x0CE3], [0x0D41, 0x0D43], [0x0D4D, 0x0D4D],
4770 [0x0DCA, 0x0DCA], [0x0DD2, 0x0DD4], [0x0DD6, 0x0DD6],
4771 [0x0E31, 0x0E31], [0x0E34, 0x0E3A], [0x0E47, 0x0E4E],
4772 [0x0EB1, 0x0EB1], [0x0EB4, 0x0EB9], [0x0EBB, 0x0EBC],
4773 [0x0EC8, 0x0ECD], [0x0F18, 0x0F19], [0x0F35, 0x0F35],
4774 [0x0F37, 0x0F37], [0x0F39, 0x0F39], [0x0F71, 0x0F7E],
4775 [0x0F80, 0x0F84], [0x0F86, 0x0F87], [0x0F90, 0x0F97],
4776 [0x0F99, 0x0FBC], [0x0FC6, 0x0FC6], [0x102D, 0x1030],
4777 [0x1032, 0x1032], [0x1036, 0x1037], [0x1039, 0x1039],
4778 [0x1058, 0x1059], [0x1160, 0x11FF], [0x135F, 0x135F],
4779 [0x1712, 0x1714], [0x1732, 0x1734], [0x1752, 0x1753],
4780 [0x1772, 0x1773], [0x17B4, 0x17B5], [0x17B7, 0x17BD],
4781 [0x17C6, 0x17C6], [0x17C9, 0x17D3], [0x17DD, 0x17DD],
4782 [0x180B, 0x180D], [0x18A9, 0x18A9], [0x1920, 0x1922],
4783 [0x1927, 0x1928], [0x1932, 0x1932], [0x1939, 0x193B],
4784 [0x1A17, 0x1A18], [0x1B00, 0x1B03], [0x1B34, 0x1B34],
4785 [0x1B36, 0x1B3A], [0x1B3C, 0x1B3C], [0x1B42, 0x1B42],
4786 [0x1B6B, 0x1B73], [0x1DC0, 0x1DCA], [0x1DFE, 0x1DFF],
4787 [0x200B, 0x200F], [0x202A, 0x202E], [0x2060, 0x2063],
4788 [0x206A, 0x206F], [0x20D0, 0x20EF], [0x302A, 0x302F],
4789 [0x3099, 0x309A], [0xA806, 0xA806], [0xA80B, 0xA80B],
4790 [0xA825, 0xA826], [0xFB1E, 0xFB1E], [0xFE00, 0xFE0F],
4791 [0xFE20, 0xFE23], [0xFEFF, 0xFEFF], [0xFFF9, 0xFFFB],
4792 [0x10A01, 0x10A03], [0x10A05, 0x10A06], [0x10A0C, 0x10A0F],
4793 [0x10A38, 0x10A3A], [0x10A3F, 0x10A3F], [0x1D167, 0x1D169],
4794 [0x1D173, 0x1D182], [0x1D185, 0x1D18B], [0x1D1AA, 0x1D1AD],
4795 [0x1D242, 0x1D244], [0xE0001, 0xE0001], [0xE0020, 0xE007F],
4799 function bisearch(ucs
) {
4801 var max
= COMBINING
.length
- 1;
4803 if (ucs
< COMBINING
[0][0] || ucs
> COMBINING
[max
][1])
4805 while (max
>= min
) {
4806 mid
= Math
.floor((min
+ max
) / 2);
4807 if (ucs
> COMBINING
[mid
][1])
4809 else if (ucs
< COMBINING
[mid
][0])
4816 function wcwidth(ucs
) {
4817 // test for 8-bit control characters
4820 if (ucs
< 32 || (ucs
>= 0x7f && ucs
< 0xa0))
4821 return opts
.control
;
4822 // binary search in table of non-spacing characters
4825 // if we arrive here, ucs is not a combining or C0/C1 control character
4830 ucs
<= 0x115f || // Hangul Jamo init. consonants
4833 (ucs
>= 0x2e80 && ucs
<= 0xa4cf && ucs
!= 0x303f) || // CJK..Yi
4834 (ucs
>= 0xac00 && ucs
<= 0xd7a3) || // Hangul Syllables
4835 (ucs
>= 0xf900 && ucs
<= 0xfaff) || // CJK Compat Ideographs
4836 (ucs
>= 0xfe10 && ucs
<= 0xfe19) || // Vertical forms
4837 (ucs
>= 0xfe30 && ucs
<= 0xfe6f) || // CJK Compat Forms
4838 (ucs
>= 0xff00 && ucs
<= 0xff60) || // Fullwidth Forms
4839 (ucs
>= 0xffe0 && ucs
<= 0xffe6) ||
4840 (ucs
>= 0x20000 && ucs
<= 0x2fffd) ||
4841 (ucs
>= 0x30000 && ucs
<= 0x3fffd)
4846 })({nul
: 0, control
: 0}); // configurable options
4852 Terminal
.EventEmitter
= EventEmitter
;
4853 Terminal
.inherits
= inherits
;
4856 * Adds an event listener to the terminal.
4858 * @param {string} event The name of the event. TODO: Document all event types
4859 * @param {function} callback The function to call when the event is triggered.
4863 Terminal
.cancel
= cancel
;