]>
git.proxmox.com Git - mirror_xterm.js.git/blob - src/xterm.js
11e9855e7314817f486d04e22df7ef265bb20a68
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
;
75 var wcswidth
= (function() {
76 var _WIDTH_COMBINING
= [
77 [0x0300, 0x036F], [0x0483, 0x0486], [0x0488, 0x0489],
78 [0x0591, 0x05BD], [0x05BF, 0x05BF], [0x05C1, 0x05C2],
79 [0x05C4, 0x05C5], [0x05C7, 0x05C7], [0x0600, 0x0603],
80 [0x0610, 0x0615], [0x064B, 0x065E], [0x0670, 0x0670],
81 [0x06D6, 0x06E4], [0x06E7, 0x06E8], [0x06EA, 0x06ED],
82 [0x070F, 0x070F], [0x0711, 0x0711], [0x0730, 0x074A],
83 [0x07A6, 0x07B0], [0x07EB, 0x07F3], [0x0901, 0x0902],
84 [0x093C, 0x093C], [0x0941, 0x0948], [0x094D, 0x094D],
85 [0x0951, 0x0954], [0x0962, 0x0963], [0x0981, 0x0981],
86 [0x09BC, 0x09BC], [0x09C1, 0x09C4], [0x09CD, 0x09CD],
87 [0x09E2, 0x09E3], [0x0A01, 0x0A02], [0x0A3C, 0x0A3C],
88 [0x0A41, 0x0A42], [0x0A47, 0x0A48], [0x0A4B, 0x0A4D],
89 [0x0A70, 0x0A71], [0x0A81, 0x0A82], [0x0ABC, 0x0ABC],
90 [0x0AC1, 0x0AC5], [0x0AC7, 0x0AC8], [0x0ACD, 0x0ACD],
91 [0x0AE2, 0x0AE3], [0x0B01, 0x0B01], [0x0B3C, 0x0B3C],
92 [0x0B3F, 0x0B3F], [0x0B41, 0x0B43], [0x0B4D, 0x0B4D],
93 [0x0B56, 0x0B56], [0x0B82, 0x0B82], [0x0BC0, 0x0BC0],
94 [0x0BCD, 0x0BCD], [0x0C3E, 0x0C40], [0x0C46, 0x0C48],
95 [0x0C4A, 0x0C4D], [0x0C55, 0x0C56], [0x0CBC, 0x0CBC],
96 [0x0CBF, 0x0CBF], [0x0CC6, 0x0CC6], [0x0CCC, 0x0CCD],
97 [0x0CE2, 0x0CE3], [0x0D41, 0x0D43], [0x0D4D, 0x0D4D],
98 [0x0DCA, 0x0DCA], [0x0DD2, 0x0DD4], [0x0DD6, 0x0DD6],
99 [0x0E31, 0x0E31], [0x0E34, 0x0E3A], [0x0E47, 0x0E4E],
100 [0x0EB1, 0x0EB1], [0x0EB4, 0x0EB9], [0x0EBB, 0x0EBC],
101 [0x0EC8, 0x0ECD], [0x0F18, 0x0F19], [0x0F35, 0x0F35],
102 [0x0F37, 0x0F37], [0x0F39, 0x0F39], [0x0F71, 0x0F7E],
103 [0x0F80, 0x0F84], [0x0F86, 0x0F87], [0x0F90, 0x0F97],
104 [0x0F99, 0x0FBC], [0x0FC6, 0x0FC6], [0x102D, 0x1030],
105 [0x1032, 0x1032], [0x1036, 0x1037], [0x1039, 0x1039],
106 [0x1058, 0x1059], [0x1160, 0x11FF], [0x135F, 0x135F],
107 [0x1712, 0x1714], [0x1732, 0x1734], [0x1752, 0x1753],
108 [0x1772, 0x1773], [0x17B4, 0x17B5], [0x17B7, 0x17BD],
109 [0x17C6, 0x17C6], [0x17C9, 0x17D3], [0x17DD, 0x17DD],
110 [0x180B, 0x180D], [0x18A9, 0x18A9], [0x1920, 0x1922],
111 [0x1927, 0x1928], [0x1932, 0x1932], [0x1939, 0x193B],
112 [0x1A17, 0x1A18], [0x1B00, 0x1B03], [0x1B34, 0x1B34],
113 [0x1B36, 0x1B3A], [0x1B3C, 0x1B3C], [0x1B42, 0x1B42],
114 [0x1B6B, 0x1B73], [0x1DC0, 0x1DCA], [0x1DFE, 0x1DFF],
115 [0x200B, 0x200F], [0x202A, 0x202E], [0x2060, 0x2063],
116 [0x206A, 0x206F], [0x20D0, 0x20EF], [0x302A, 0x302F],
117 [0x3099, 0x309A], [0xA806, 0xA806], [0xA80B, 0xA80B],
118 [0xA825, 0xA826], [0xFB1E, 0xFB1E], [0xFE00, 0xFE0F],
119 [0xFE20, 0xFE23], [0xFEFF, 0xFEFF], [0xFFF9, 0xFFFB],
120 [0x10A01, 0x10A03], [0x10A05, 0x10A06], [0x10A0C, 0x10A0F],
121 [0x10A38, 0x10A3A], [0x10A3F, 0x10A3F], [0x1D167, 0x1D169],
122 [0x1D173, 0x1D182], [0x1D185, 0x1D18B], [0x1D1AA, 0x1D1AD],
123 [0x1D242, 0x1D244], [0xE0001, 0xE0001], [0xE0020, 0xE007F],
126 function _width_bisearch(ucs
) {
128 var max
= _WIDTH_COMBINING
.length
- 1;
130 if (ucs
< _WIDTH_COMBINING
[0][0] || ucs
> _WIDTH_COMBINING
[max
][1])
133 mid
= Math
.floor((min
+ max
) / 2);
134 if (ucs
> _WIDTH_COMBINING
[mid
][1])
136 else if (ucs
< _WIDTH_COMBINING
[mid
][0])
143 function _width_wcwidth(ucs
, opts
) {
144 // test for 8-bit control characters
147 if (ucs
< 32 || (ucs
>= 0x7f && ucs
< 0xa0))
149 // binary search in table of non-spacing characters
150 if (_width_bisearch(ucs
))
152 // if we arrive here, ucs is not a combining or C0/C1 control character
155 (ucs
<= 0x115f || // Hangul Jamo init. consonants
156 ucs
== 0x2329 || ucs
== 0x232a ||
157 (ucs
>= 0x2e80 && ucs
<= 0xa4cf &&
158 ucs
!= 0x303f) || // CJK ... Yi
159 (ucs
>= 0xac00 && ucs
<= 0xd7a3) || // Hangul Syllables
160 (ucs
>= 0xf900 && ucs
<= 0xfaff) || // CJK Compatibility Ideographs
161 (ucs
>= 0xfe10 && ucs
<= 0xfe19) || // Vertical forms
162 (ucs
>= 0xfe30 && ucs
<= 0xfe6f) || // CJK Compatibility Forms
163 (ucs
>= 0xff00 && ucs
<= 0xff60) || // Fullwidth Forms
164 (ucs
>= 0xffe0 && ucs
<= 0xffe6) ||
165 (ucs
>= 0x20000 && ucs
<= 0x2fffd) ||
166 (ucs
>= 0x30000 && ucs
<= 0x3fffd)));
169 var _WIDTH_DEFAULTS
= {nul
: 0, control
: 0};
171 function wcswidth(str
, opts
) {
172 opts
= _WIDTH_DEFAULTS
; // FIXME
173 if (typeof str
!== 'string')
174 return _width_wcwidth(str
, opts
);
176 for (var i
= 0; i
< str
.length
; i
++) {
177 var n
= _width_wcwidth(str
.charCodeAt(i
), opts
);
192 function EventEmitter() {
193 this._events
= this._events
|| {};
196 EventEmitter
.prototype.addListener = function(type
, listener
) {
197 this._events
[type
] = this._events
[type
] || [];
198 this._events
[type
].push(listener
);
201 EventEmitter
.prototype.on
= EventEmitter
.prototype.addListener
;
203 EventEmitter
.prototype.removeListener = function(type
, listener
) {
204 if (!this._events
[type
]) return;
206 var obj
= this._events
[type
]
210 if (obj
[i
] === listener
|| obj
[i
].listener
=== listener
) {
217 EventEmitter
.prototype.off
= EventEmitter
.prototype.removeListener
;
219 EventEmitter
.prototype.removeAllListeners = function(type
) {
220 if (this._events
[type
]) delete this._events
[type
];
223 EventEmitter
.prototype.once = function(type
, listener
) {
226 var args
= Array
.prototype.slice
.call(arguments
);
227 self
.removeListener(type
, on
);
228 return listener
.apply(self
, args
);
230 on
.listener
= listener
;
231 return this.on(type
, on
);
234 EventEmitter
.prototype.emit = function(type
) {
235 if (!this._events
[type
]) return;
237 var args
= Array
.prototype.slice
.call(arguments
, 1)
238 , obj
= this._events
[type
]
243 obj
[i
].apply(this, args
);
247 EventEmitter
.prototype.listeners = function(type
) {
248 return this._events
[type
] = this._events
[type
] || [];
255 var normal
= 0, escaped
= 1, csi
= 2, osc
= 3, charset
= 4, dcs
= 5, ignore
= 6;
262 * Creates a new `Terminal` object.
264 * @param {object} options An object containing a set of options, the available options are:
265 * - cursorBlink (boolean): Whether the terminal cursor blinks
269 function Terminal(options
) {
272 if (!(this instanceof Terminal
)) {
273 return new Terminal(arguments
[0], arguments
[1], arguments
[2]);
276 self
.cancel
= Terminal
.cancel
;
278 EventEmitter
.call(this);
280 if (typeof options
=== 'number') {
284 handler
: arguments
[2]
288 options
= options
|| {};
291 Object
.keys(Terminal
.defaults
).forEach(function(key
) {
292 if (options
[key
] == null) {
293 options
[key
] = Terminal
.options
[key
];
295 if (Terminal
[key
] !== Terminal
.defaults
[key
]) {
296 options
[key
] = Terminal
[key
];
299 self
[key
] = options
[key
];
302 if (options
.colors
.length
=== 8) {
303 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(8));
304 } else if (options
.colors
.length
=== 16) {
305 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(16));
306 } else if (options
.colors
.length
=== 10) {
307 options
.colors
= options
.colors
.slice(0, -2).concat(
308 Terminal
._colors
.slice(8, -2), options
.colors
.slice(-2));
309 } else if (options
.colors
.length
=== 18) {
310 options
.colors
= options
.colors
.concat(
311 Terminal
._colors
.slice(16, -2), options
.colors
.slice(-2));
313 this.colors
= options
.colors
;
315 this.options
= options
;
317 // this.context = options.context || window;
318 // this.document = options.document || document;
319 this.parent
= options
.body
|| options
.parent
320 || (document
? document
.getElementsByTagName('body')[0] : null);
322 this.cols
= options
.cols
|| options
.geometry
[0];
323 this.rows
= options
.rows
|| options
.geometry
[1];
325 if (options
.handler
) {
326 this.on('data', options
.handler
);
330 * The scroll position of the y cursor, ie. ybase + y = the y position within the entire
335 * The scroll position of the viewport
340 * The cursor's x position after ybase
345 * The cursor's y position after ybase
350 * Used to debounce the refresh function
352 this.isRefreshing
= false;
355 * Whether there is a full terminal refresh queued
358 this.cursorState
= 0;
359 this.cursorHidden
= false;
364 this.scrollBottom
= this.rows
- 1;
367 this.applicationKeypad
= false;
368 this.applicationCursor
= false;
369 this.originMode
= false;
370 this.insertMode
= false;
371 this.wraparoundMode
= false;
376 this.gcharset
= null;
378 this.charsets
= [null];
402 this.readable
= true;
403 this.writable
= true;
405 this.defAttr
= (0 << 18) | (257 << 9) | (256 << 0);
406 this.curAttr
= this.defAttr
;
409 this.currentParam
= 0;
413 // left over left part of surrogate from previous write invocation
414 this.surrogate_left
= '';
417 * An array of all lines in the entire buffer, including the prompt. The lines are array of
418 * characters which are 2-length arrays where [0] is an attribute and [1] is the character.
423 this.lines
.push(this.blankLine());
430 inherits(Terminal
, EventEmitter
);
432 // back_color_erase feature for xterm.
433 Terminal
.prototype.eraseAttr = function() {
434 // if (this.is('screen')) return this.defAttr;
435 return (this.defAttr
& ~0x1ff) | (this.curAttr
& 0x1ff);
443 Terminal
.tangoColors
= [
464 // Colors 0-15 + 16-255
465 // Much thanks to TooTallNate for writing this.
466 Terminal
.colors
= (function() {
467 var colors
= Terminal
.tangoColors
.slice()
468 , r
= [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff]
473 for (; i
< 216; i
++) {
474 out(r
[(i
/ 36) % 6 | 0], r
[(i
/ 6) % 6 | 0], r
[i
% 6]);
479 for (; i
< 24; i
++) {
484 function out(r
, g
, b
) {
485 colors
.push('#' + hex(r
) + hex(g
) + hex(b
));
490 return c
.length
< 2 ? '0' + c
: c
;
496 Terminal
._colors
= Terminal
.colors
.slice();
498 Terminal
.vcolors
= (function() {
500 , colors
= Terminal
.colors
504 for (; i
< 256; i
++) {
505 color
= parseInt(colors
[i
].substring(1), 16);
507 (color
>> 16) & 0xff,
520 Terminal
.defaults
= {
521 colors
: Terminal
.colors
,
533 // programFeatures: false,
537 Terminal
.options
= {};
539 Terminal
.focus
= null;
541 each(keys(Terminal
.defaults
), function(key
) {
542 Terminal
[key
] = Terminal
.defaults
[key
];
543 Terminal
.options
[key
] = Terminal
.defaults
[key
];
547 * Focus the terminal. Delegates focus handling to the terminal's DOM element.
551 Terminal
.prototype.focus = function() {
552 return this.element
.focus();
556 * Binds the desired focus behavior on a given terminal object.
560 Terminal
.bindFocus = function (term
) {
561 on(term
.element
, 'focus', function (ev
) {
562 if (term
.sendFocus
) {
567 Terminal
.focus
= term
;
568 term
.emit('focus', {terminal
: term
});
573 * Blur the terminal. Delegates blur handling to the terminal's DOM element.
577 Terminal
.prototype.blur = function() {
578 return this.element
.blur();
582 * Binds the desired blur behavior on a given terminal object.
586 Terminal
.bindBlur = function (term
) {
587 on(term
.element
, 'blur', function (ev
) {
588 if (term
.sendFocus
) {
591 Terminal
.focus
= null;
592 term
.emit('blur', {terminal
: term
});
597 * Initialize default behavior
599 Terminal
.prototype.initGlobal = function() {
600 Terminal
.bindKeys(this);
601 Terminal
.bindPaste(this);
602 Terminal
.bindCopy(this);
603 Terminal
.bindCut(this);
604 Terminal
.bindDrop(this);
605 Terminal
.bindFocus(this);
606 Terminal
.bindBlur(this);
610 * Clears all selected text, inside the terminal.
612 Terminal
.prototype.clearSelection = function() {
613 var selectionBaseNode
= window
.getSelection().baseNode
;
615 if (selectionBaseNode
&& (this.element
.contains(selectionBaseNode
.parentElement
))) {
616 window
.getSelection().removeAllRanges();
621 * This function temporarily enables (leases) the contentEditable value of the terminal, which
622 * should be set back to false within 5 seconds at most.
624 Terminal
.prototype.leaseContentEditable = function (ms
, callback
) {
627 term
.element
.contentEditable
= true;
630 * Blur and re-focus instantly. This is due to a weird focus state on Chrome, when setting
631 * contentEditable to true on a focused element.
636 setTimeout(function () {
637 term
.element
.contentEditable
= false;
638 if (typeof callback
== 'function') {
645 * Bind to paste event and allow both keyboard and right-click pasting, without having the
646 * contentEditable value set to true.
648 Terminal
.bindPaste = function(term
) {
649 on(term
.element
, 'paste', function(ev
) {
650 if (ev
.clipboardData
) {
651 var text
= ev
.clipboardData
.getData('text/plain');
652 term
.emit('paste', text
, ev
);
655 * Cancel the paste event, or else things will be pasted twice:
656 * 1. by the terminal handler
657 * 2. by the browser, because of the contentEditable value being true
659 term
.cancel(ev
, true);
662 * After the paste event is completed, always set the contentEditable value to false.
664 term
.element
.contentEditable
= false;
669 * Hack pasting with keyboard, in order to make it work without contentEditable.
670 * When a user types Ctrl + Shift + V or Cmd + V on a Mac, lease the contentEditable value
673 on(term
.element
, 'keydown', function (ev
) {
674 var isEditable
= term
.element
.contentEditable
=== "true";
677 * If on a Mac, lease the contentEditable value temporarily, when the user presses
678 * the Cmd button, in a keydown event order to paste frictionlessly.
680 if (term
.isMac
&& ev
.metaKey
&& !isEditable
) {
681 term
.leaseContentEditable(5000);
684 if (!term
.isMac
&& ev
.keyCode
== 45 && ev
.shiftKey
&& !ev
.ctrlKey
&& !isEditable
) {
685 // Shift + Insert pastes on windows and many linuxes
686 term
.leaseContentEditable();
691 * Hack pasting with right-click in order to allow right-click paste, by leasing the
692 * contentEditable value as true.
694 on(term
.element
, 'contextmenu', function (ev
) {
695 term
.leaseContentEditable();
701 * Apply key handling to the terminal
703 Terminal
.bindKeys = function(term
) {
704 on(term
.element
, 'keydown', function(ev
) {
708 on(term
.element
, 'keypress', function(ev
) {
714 * Prepares text copied from terminal selection, to be saved in the clipboard by:
715 * 1. stripping all trailing white spaces
716 * 2. converting all non-breaking spaces to regular spaces
717 * @param {string} text The copied text that needs processing for storing in clipboard
721 Terminal
.prepareCopiedTextForClipboard = function (text
) {
722 var space
= String
.fromCharCode(32),
723 nonBreakingSpace
= String
.fromCharCode(160),
724 allNonBreakingSpaces
= new RegExp(nonBreakingSpace
, 'g'),
725 processedText
= text
.split('\n').map(function (line
) {
727 * Strip all trailing white spaces and convert all non-breaking spaces to regular
730 var processedLine
= line
.replace(/\s+$/g, '').replace(allNonBreakingSpaces
, space
);
732 return processedLine
;
735 return processedText
;
739 * Binds copy functionality to the given terminal.
742 Terminal
.bindCopy = function(term
) {
743 on(term
.element
, 'copy', function(ev
) {
744 var copiedText
= window
.getSelection().toString(),
745 text
= Terminal
.prepareCopiedTextForClipboard(copiedText
);
747 ev
.clipboardData
.setData('text/plain', text
);
753 * Cancel the cut event completely
755 Terminal
.bindCut = function(term
) {
756 on(term
.element
, 'cut', function (ev
) {
762 Terminal
.bindDrop = function (term
) {
764 * Do not perform the "drop" event. Altering the contents of the
765 * terminal with drag n drop is unwanted behavior.
767 on(term
.element
, 'drop', function (ev
) {
768 term
.cancel(ev
, true);
773 Terminal
.click = function (term
) {
775 * Do not perform the "drop" event. Altering the contents of the
776 * terminal with drag n drop is unwanted behavior.
778 on(term
.element
, 'click', function (ev
) {
779 term
.cancel(ev
, true);
785 * Insert the given row to the terminal or produce a new one
786 * if no row argument is passed. Return the inserted row.
788 Terminal
.prototype.insertRow = function (row
) {
789 if (typeof row
!= 'object') {
790 row
= document
.createElement('div');
793 this.rowContainer
.appendChild(row
);
794 this.children
.push(row
);
801 * Opens the terminal within an element.
803 * @param {HTMLElement} parent The element to create the terminal within.
807 Terminal
.prototype.open = function(parent
) {
808 var self
=this, i
=0, div
;
810 this.parent
= parent
|| this.parent
;
813 throw new Error('Terminal requires a parent element.');
817 * Grab global elements
819 this.context
= this.parent
.ownerDocument
.defaultView
;
820 this.document
= this.parent
.ownerDocument
;
821 this.body
= this.document
.getElementsByTagName('body')[0];
826 if (this.context
.navigator
&& this.context
.navigator
.userAgent
) {
827 this.isMSIE
= !!~this.context
.navigator
.userAgent
.indexOf('MSIE');
831 * Find the users platform. We use this to interpret the meta key
832 * and ISO third level shifts.
833 * http://stackoverflow.com/questions/19877924/what-is-the-list-of-possible-values-for-navigator-platform-as-of-today
835 if (this.context
.navigator
&& this.context
.navigator
.platform
) {
836 this.isMac
= contains(
837 this.context
.navigator
.platform
,
838 ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K']
840 this.isIpad
= this.context
.navigator
.platform
=== 'iPad';
841 this.isIphone
= this.context
.navigator
.platform
=== 'iPhone';
842 this.isMSWindows
= contains(
843 this.context
.navigator
.platform
,
844 ['Windows', 'Win16', 'Win32', 'WinCE']
849 * Create main element container
851 this.element
= this.document
.createElement('div');
852 this.element
.classList
.add('terminal');
853 this.element
.classList
.add('xterm');
854 this.element
.classList
.add('xterm-theme-' + this.theme
);
855 this.element
.setAttribute('tabindex', 0);
856 this.element
.spellcheck
= false;
859 * Create the container that will hold the lines of the terminal and then
860 * produce the lines the lines.
862 this.rowContainer
= document
.createElement('div');
863 this.rowContainer
.classList
.add('xterm-rows');
864 this.element
.appendChild(this.rowContainer
);
867 for (; i
< this.rows
; i
++) {
870 this.parent
.appendChild(this.element
);
873 this.refresh(0, this.rows
- 1);
875 // Initialize global actions that
876 // need to be taken on the document.
879 // Ensure there is a Terminal.focus.
882 on(this.element
, 'mouseup', function() {
883 var selection
= document
.getSelection(),
884 collapsed
= selection
.isCollapsed
,
885 isRange
= typeof collapsed
== 'boolean' ? !collapsed
: selection
.type
== 'Range';
891 // Listen for mouse events and translate
892 // them into terminal mouse protocols.
895 // Figure out whether boldness affects
896 // the character width of monospace fonts.
897 if (Terminal
.brokenBold
== null) {
898 Terminal
.brokenBold
= isBoldBroken(this.document
);
906 * Attempts to load an add-on using CommonJS or RequireJS (whichever is available).
907 * @param {string} addon The name of the addon to load
910 Terminal
.loadAddon = function(addon
, callback
) {
911 if (typeof exports
=== 'object' && typeof module
=== 'object') {
913 return require(__dirname
+ '/../addons/' + addon
);
914 } else if (typeof define
== 'function') {
916 return require(['../addons/' + addon
+ '/' + addon
], callback
);
918 console
.error('Cannot load a module without a CommonJS or RequireJS environment.');
924 // XTerm mouse events
925 // http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#Mouse%20Tracking
926 // To better understand these
927 // the xterm code is very helpful:
929 // button.c, charproc.c, misc.c
930 // Relevant functions in xterm/button.c:
931 // BtnCode, EmitButtonCode, EditorButton, SendMousePosition
932 Terminal
.prototype.bindMouse = function() {
933 var el
= this.element
937 var wheelEvent
= 'onmousewheel' in this.context
941 // mouseup, mousedown, mousewheel
942 // left click: ^[[M 3<^[[M#3<
943 // mousewheel up: ^[[M`3>
944 function sendButton(ev
) {
948 // get the xterm-style button
949 button
= getButton(ev
);
951 // get mouse coordinates
955 sendEvent(button
, pos
);
962 // keep it at the left
963 // button, just in case.
974 // motion example of a left click:
975 // ^[[M 3<^[[M@4<^[[M@5<^[[M@6<^[[M@7<^[[M#7<
976 function sendMove(ev
) {
983 // buttons marked as motions
984 // are incremented by 32
987 sendEvent(button
, pos
);
991 // position to characters
992 function encode(data
, ch
) {
993 if (!self
.utfMouse
) {
994 if (ch
=== 255) return data
.push(0);
995 if (ch
> 127) ch
= 127;
998 if (ch
=== 2047) return data
.push(0);
1002 if (ch
> 2047) ch
= 2047;
1003 data
.push(0xC0 | (ch
>> 6));
1004 data
.push(0x80 | (ch
& 0x3F));
1009 // send a mouse event:
1010 // regular/utf8: ^[[M Cb Cx Cy
1011 // urxvt: ^[[ Cb ; Cx ; Cy M
1012 // sgr: ^[[ Cb ; Cx ; Cy M/m
1013 // vt300: ^[[ 24(1/3/5)~ [ Cx , Cy ] \r
1014 // locator: CSI P e ; P b ; P r ; P c ; P p & w
1015 function sendEvent(button
, pos
) {
1016 // self.emit('mouse', {
1022 if (self
.vt300Mouse
) {
1024 // http://www.vt100.net/docs/vt3xx-gp/chapter15.html
1028 var data
= '\x1b[24';
1029 if (button
=== 0) data
+= '1';
1030 else if (button
=== 1) data
+= '3';
1031 else if (button
=== 2) data
+= '5';
1032 else if (button
=== 3) return;
1034 data
+= '~[' + pos
.x
+ ',' + pos
.y
+ ']\r';
1039 if (self
.decLocator
) {
1044 if (button
=== 0) button
= 2;
1045 else if (button
=== 1) button
= 4;
1046 else if (button
=== 2) button
= 6;
1047 else if (button
=== 3) button
= 3;
1051 + (button
=== 3 ? 4 : 0)
1062 if (self
.urxvtMouse
) {
1067 self
.send('\x1b[' + button
+ ';' + pos
.x
+ ';' + pos
.y
+ 'M');
1071 if (self
.sgrMouse
) {
1075 + ((button
& 3) === 3 ? button
& ~3 : button
)
1080 + ((button
& 3) === 3 ? 'm' : 'M'));
1086 encode(data
, button
);
1087 encode(data
, pos
.x
);
1088 encode(data
, pos
.y
);
1090 self
.send('\x1b[M' + String
.fromCharCode
.apply(String
, data
));
1093 function getButton(ev
) {
1106 // 1, and 2 - with 64 added
1109 button
= ev
.button
!= null
1116 button
= button
=== 1 ? 0 : button
=== 4 ? 1 : button
;
1122 case 'DOMMouseScroll':
1123 button
= ev
.detail
< 0
1128 button
= ev
.wheelDeltaY
> 0
1134 // next three bits are the modifiers:
1135 // 4 = shift, 8 = meta, 16 = control
1136 shift
= ev
.shiftKey
? 4 : 0;
1137 meta
= ev
.metaKey
? 8 : 0;
1138 ctrl
= ev
.ctrlKey
? 16 : 0;
1139 mod
= shift
| meta
| ctrl
;
1142 if (self
.vt200Mouse
) {
1145 } else if (!self
.normalMouse
) {
1150 button
= (32 + (mod
<< 2)) + button
;
1155 // mouse coordinates measured in cols/rows
1156 function getCoords(ev
) {
1159 // ignore browsers without pageX for now
1160 if (ev
.pageX
== null) return;
1166 // should probably check offsetParent
1167 // but this is more portable
1168 while (el
&& el
!== self
.document
.documentElement
) {
1171 el
= 'offsetParent' in el
1176 // convert to cols/rows
1177 w
= self
.element
.clientWidth
;
1178 h
= self
.element
.clientHeight
;
1179 x
= Math
.round((x
/ w
) * self
.cols
);
1180 y
= Math
.round((y
/ h
) * self
.rows
);
1182 // be sure to avoid sending
1183 // bad positions to the program
1185 if (x
> self
.cols
) x
= self
.cols
;
1187 if (y
> self
.rows
) y
= self
.rows
;
1189 // xterm sends raw bytes and
1190 // starts at 32 (SP) for each.
1197 type
: ev
.type
=== wheelEvent
1203 on(el
, 'mousedown', function(ev
) {
1204 if (!self
.mouseEvents
) return;
1213 //if (self.vt200Mouse && !self.normalMouse) {
1214 if (self
.vt200Mouse
) {
1215 sendButton({ __proto__
: ev
, type
: 'mouseup' });
1216 return self
.cancel(ev
);
1220 if (self
.normalMouse
) on(self
.document
, 'mousemove', sendMove
);
1222 // x10 compatibility mode can't send button releases
1223 if (!self
.x10Mouse
) {
1224 on(self
.document
, 'mouseup', function up(ev
) {
1226 if (self
.normalMouse
) off(self
.document
, 'mousemove', sendMove
);
1227 off(self
.document
, 'mouseup', up
);
1228 return self
.cancel(ev
);
1232 return self
.cancel(ev
);
1235 //if (self.normalMouse) {
1236 // on(self.document, 'mousemove', sendMove);
1239 on(el
, wheelEvent
, function(ev
) {
1240 if (!self
.mouseEvents
) return;
1243 || self
.decLocator
) return;
1245 return self
.cancel(ev
);
1248 // allow mousewheel scrolling in
1249 // the shell for example
1250 on(el
, wheelEvent
, function(ev
) {
1251 if (self
.mouseEvents
) return;
1252 if (self
.applicationKeypad
) return;
1253 if (ev
.type
=== 'DOMMouseScroll') {
1254 self
.scrollDisp(ev
.detail
< 0 ? -1 : 1);
1256 self
.scrollDisp(ev
.wheelDeltaY
> 0 ? -1 : 1);
1258 return self
.cancel(ev
);
1263 * Destroys the terminal.
1267 Terminal
.prototype.destroy = function() {
1268 this.readable
= false;
1269 this.writable
= false;
1271 this.handler = function() {};
1272 this.write = function() {};
1273 if (this.element
.parentNode
) {
1274 this.element
.parentNode
.removeChild(this.element
);
1276 //this.emit('close');
1281 * Flags used to render terminal text properly
1294 * In the screen buffer, each character
1295 * is stored as a an array with a character
1296 * and a 32-bit integer.
1297 * First value: a utf-16 character.
1299 * Next 9 bits: background color (0-511).
1300 * Next 9 bits: foreground color (0-511).
1301 * Next 14 bits: a mask for misc. flags:
1302 * 1=bold, 2=underline, 4=blink, 8=inverse, 16=invisible
1306 * Refreshes terminal content within two rows (inclusive).
1308 * @param {number} start The row to start from (between 0 and terminal's height terminal - 1)
1309 * @param {number} end The row to end at (between fromRow and terminal's height terminal - 1)
1310 * @param {boolean} queue Whether the refresh should ran right now or be queued
1314 Terminal
.prototype.refresh = function(start
, end
, queue
) {
1317 // queue defaults to true
1318 queue
= (typeof queue
== 'undefined') ? true : queue
;
1321 * The refresh queue allows refresh to execute only approximately 30 times a second. For
1322 * commands that pass a significant amount of output to the write function, this prevents the
1323 * terminal from maxing out the CPU and making the UI unresponsive. While commands can still
1324 * run beyond what they do on the terminal, it is far better with a debounce in place as
1325 * every single terminal manipulation does not need to be constructed in the DOM.
1327 * A side-effect of this is that it makes ^C to interrupt a process seem more responsive.
1330 // If refresh should be queued, order the refresh and return.
1331 if (this._refreshIsQueued
) {
1332 // If a refresh has already been queued, just order a full refresh next
1333 this._fullRefreshNext
= true;
1335 setTimeout(function () {
1336 self
.refresh(start
, end
, false);
1338 this._refreshIsQueued
= true;
1343 // If refresh should be run right now (not be queued), release the lock
1344 this._refreshIsQueued
= false;
1346 // If multiple refreshes were requested, make a full refresh.
1347 if (this._fullRefreshNext
) {
1349 end
= this.rows
- 1;
1350 this._fullRefreshNext
= false // reset lock
1353 var x
, y
, i
, line
, out
, ch
, ch_width
, width
, data
, attr
, bg
, fg
, flags
, row
, parent
, focused
= document
.activeElement
;
1355 // If this is a big refresh, remove the terminal rows from the DOM for faster calculations
1356 if (end
- start
>= this.rows
/ 2) {
1357 parent
= this.element
.parentNode
;
1359 this.element
.removeChild(this.rowContainer
);
1366 if (end
>= this.rows
.length
) {
1367 this.log('`end` is too large. Most likely a bad CSR.');
1368 end
= this.rows
.length
- 1;
1371 for (; y
<= end
; y
++) {
1372 row
= y
+ this.ydisp
;
1374 line
= this.lines
[row
];
1377 if (this.y
=== y
- (this.ybase
- this.ydisp
)
1379 && !this.cursorHidden
) {
1385 attr
= this.defAttr
;
1388 for (; i
< width
; i
++) {
1391 ch_width
= line
[i
][2] || 1;
1395 if (i
=== x
) data
= -1;
1397 if (data
!== attr
) {
1398 if (attr
!== this.defAttr
) {
1401 if (data
!== this.defAttr
) {
1403 out
+= '<span class="reverse-video terminal-cursor';
1404 if (this.cursorBlink
) {
1409 var classNames
= [];
1412 fg
= (data
>> 9) & 0x1ff;
1415 if (flags
& Terminal
.flags
.BOLD
) {
1416 if (!Terminal
.brokenBold
) {
1417 classNames
.push('xterm-bold');
1419 // See: XTerm*boldColors
1420 if (fg
< 8) fg
+= 8;
1423 if (flags
& Terminal
.flags
.UNDERLINE
) {
1424 classNames
.push('xterm-underline');
1427 if (flags
& Terminal
.flags
.BLINK
) {
1428 classNames
.push('xterm-blink');
1432 * If inverse flag is on, then swap the foreground and background variables.
1434 if (flags
& Terminal
.flags
.INVERSE
) {
1435 /* One-line variable swap in JavaScript: http://stackoverflow.com/a/16201730 */
1436 bg = [fg, fg = bg][0];
1437 // Should inverse just be before the
1438 // above boldColors effect instead?
1439 if ((flags & 1) && fg < 8) fg += 8;
1442 if (flags & Terminal.flags.INVISIBLE) {
1443 classNames.push('xterm-hidden');
1447 * Weird situation: Invert flag used black foreground and white background results
1448 * in invalid background color, positioned at the 256 index of the 256 terminal
1449 * color map. Pin the colors manually in such a case.
1451 * Source: https://github.com/sourcelair/xterm.js/issues/57
1453 if (flags
& Terminal
.flags
.INVERSE
) {
1463 classNames
.push('xterm-bg-color-' + bg
);
1467 classNames
.push('xterm-color-' + fg
);
1471 if (classNames
.length
) {
1472 out
+= ' class="' + classNames
.join(' ') + '"';
1493 if (ch_width
==2) i
++;
1502 if (attr
!== this.defAttr
) {
1506 this.children
[y
].innerHTML
= out
;
1510 this.element
.appendChild(this.rowContainer
);
1513 this.emit('refresh', {element
: this.element
, start
: start
, end
: end
});
1516 Terminal
.prototype.showCursor = function() {
1517 if (!this.cursorState
) {
1518 this.cursorState
= 1;
1519 this.refresh(this.y
, this.y
);
1523 Terminal
.prototype.scroll = function() {
1526 if (++this.ybase
=== this.scrollback
) {
1527 this.ybase
= this.ybase
/ 2 | 0;
1528 this.lines
= this.lines
.slice(-(this.ybase
+ this.rows
) + 1);
1531 this.ydisp
= this.ybase
;
1534 row
= this.ybase
+ this.rows
- 1;
1536 // subtract the bottom scroll region
1537 row
-= this.rows
- 1 - this.scrollBottom
;
1539 if (row
=== this.lines
.length
) {
1540 // potential optimization:
1541 // pushing is faster than splicing
1542 // when they amount to the same
1544 this.lines
.push(this.blankLine());
1547 this.lines
.splice(row
, 0, this.blankLine());
1550 if (this.scrollTop
!== 0) {
1551 if (this.ybase
!== 0) {
1553 this.ydisp
= this.ybase
;
1555 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
1559 this.updateRange(this.scrollTop
);
1560 this.updateRange(this.scrollBottom
);
1563 Terminal
.prototype.scrollDisp = function(disp
) {
1566 if (this.ydisp
> this.ybase
) {
1567 this.ydisp
= this.ybase
;
1568 } else if (this.ydisp
< 0) {
1572 this.refresh(0, this.rows
- 1);
1576 * Writes text to the terminal.
1578 * @param {string} text The text to write to the terminal.
1582 Terminal
.prototype.write = function(data
) {
1583 var l
= data
.length
, i
= 0, j
, cs
, ch
, code
, low
, ch_width
;
1585 this.refreshStart
= this.y
;
1586 this.refreshEnd
= this.y
;
1588 if (this.ybase
!== this.ydisp
) {
1589 this.ydisp
= this.ybase
;
1593 // apply leftover surrogate left from last write
1594 if (this.surrogate_left
) {
1595 data
= this.surrogate_left
+ data
;
1596 this.surrogate_left
= '';
1599 for (; i
< l
; i
++) {
1602 code
= data
.charCodeAt(i
);
1604 if (0xD800 <= code
&& code
<= 0xDBFF) {
1605 low
= data
.charCodeAt(i
+1);
1606 if (low
!== low
) { // end of data stream, we need to save to first part of surrogate
1607 this.surrogate_left
= ch
;
1610 code
= ((code
- 0xD800) * 0x400) + (low
- 0xDC00) + 0x10000;
1611 ch
+= data
.charAt(i
+1);
1613 // surrogate low - already handled above
1614 if (0xDC00 <= code
&& code
<= 0xDFFF)
1617 ch_width
= wcswidth(code
);
1620 switch (this.state
) {
1631 if (this.convertEol
) {
1635 if (this.y
> this.scrollBottom
) {
1655 this.x
= this.nextStop();
1670 this.state
= escaped
;
1676 if (this.charset
&& this.charset
[ch
]) {
1677 ch
= this.charset
[ch
];
1680 // combining into last cell - fixing #72 and 'café'
1681 if (!ch_width
&& this.x
) {
1682 this.lines
[this.y
+ this.ybase
][this.x
-1][1] += ch
;
1683 this.updateRange(this.y
);
1687 if (this.x
+ch_width
-1 >= this.cols
) { // just goto next line if ch would overflow
1690 if (this.y
> this.scrollBottom
) {
1696 this.lines
[this.y
+ this.ybase
][this.x
] = [this.curAttr
, ch
, ch_width
];
1698 this.updateRange(this.y
);
1700 if (ch_width
==2) { // set next cell to zero
1701 this.lines
[this.y
+ this.ybase
][this.x
] = [this.curAttr
, '', 0];
1710 // ESC [ Control Sequence Introducer ( CSI is 0x9b).
1713 this.currentParam
= 0;
1717 // ESC ] Operating System Command ( OSC is 0x9d).
1720 this.currentParam
= 0;
1724 // ESC P Device Control String ( DCS is 0x90).
1727 this.currentParam
= 0;
1731 // ESC _ Application Program Command ( APC is 0x9f).
1733 this.state
= ignore
;
1736 // ESC ^ Privacy Message ( PM is 0x9e).
1738 this.state
= ignore
;
1741 // ESC c Full Reset (RIS).
1746 // ESC E Next Line ( NEL is 0x85).
1747 // ESC D Index ( IND is 0x84).
1755 // ESC M Reverse Index ( RI is 0x8d).
1757 this.reverseIndex();
1760 // ESC % Select default/utf-8 character set.
1761 // @ = default, G = utf-8
1763 //this.charset = null;
1765 this.setgCharset(0, Terminal
.charsets
.US
);
1766 this.state
= normal
;
1770 // ESC (,),*,+,-,. Designate G0-G2 Character Set.
1771 case '(': // <-- this seems to get all the attention
1797 this.state
= charset
;
1800 // Designate G3 Character Set (VT300).
1801 // A = ISO Latin-1 Supplemental.
1805 this.state
= charset
;
1810 // Single Shift Select of G2 Character Set
1811 // ( SS2 is 0x8e). This affects next character only.
1815 // Single Shift Select of G3 Character Set
1816 // ( SS3 is 0x8f). This affects next character only.
1820 // Invoke the G2 Character Set as GL (LS2).
1825 // Invoke the G3 Character Set as GL (LS3).
1830 // Invoke the G3 Character Set as GR (LS3R).
1835 // Invoke the G2 Character Set as GR (LS2R).
1840 // Invoke the G1 Character Set as GR (LS1R).
1845 // ESC 7 Save Cursor (DECSC).
1848 this.state
= normal
;
1851 // ESC 8 Restore Cursor (DECRC).
1853 this.restoreCursor();
1854 this.state
= normal
;
1857 // ESC # 3 DEC line height/width
1859 this.state
= normal
;
1863 // ESC H Tab Set (HTS is 0x88).
1868 // ESC = Application Keypad (DECPAM).
1870 this.log('Serial port requested application keypad.');
1871 this.applicationKeypad
= true;
1872 this.state
= normal
;
1875 // ESC > Normal Keypad (DECPNM).
1877 this.log('Switching back to normal keypad.');
1878 this.applicationKeypad
= false;
1879 this.state
= normal
;
1883 this.state
= normal
;
1884 this.error('Unknown ESC control: %s.', ch
);
1891 case '0': // DEC Special Character and Line Drawing Set.
1892 cs
= Terminal
.charsets
.SCLD
;
1895 cs
= Terminal
.charsets
.UK
;
1897 case 'B': // United States (USASCII).
1898 cs
= Terminal
.charsets
.US
;
1901 cs
= Terminal
.charsets
.Dutch
;
1903 case 'C': // Finnish
1905 cs
= Terminal
.charsets
.Finnish
;
1908 cs
= Terminal
.charsets
.French
;
1910 case 'Q': // FrenchCanadian
1911 cs
= Terminal
.charsets
.FrenchCanadian
;
1914 cs
= Terminal
.charsets
.German
;
1916 case 'Y': // Italian
1917 cs
= Terminal
.charsets
.Italian
;
1919 case 'E': // NorwegianDanish
1921 cs
= Terminal
.charsets
.NorwegianDanish
;
1923 case 'Z': // Spanish
1924 cs
= Terminal
.charsets
.Spanish
;
1926 case 'H': // Swedish
1928 cs
= Terminal
.charsets
.Swedish
;
1931 cs
= Terminal
.charsets
.Swiss
;
1933 case '/': // ISOLatin (actually /A)
1934 cs
= Terminal
.charsets
.ISOLatin
;
1938 cs
= Terminal
.charsets
.US
;
1941 this.setgCharset(this.gcharset
, cs
);
1942 this.gcharset
= null;
1943 this.state
= normal
;
1949 // Set Text Parameters.
1950 if (ch
=== '\x1b' || ch
=== '\x07') {
1951 if (ch
=== '\x1b') i
++;
1953 this.params
.push(this.currentParam
);
1955 switch (this.params
[0]) {
1959 if (this.params
[1]) {
1960 this.title
= this.params
[1];
1961 this.handleTitle(this.title
);
1969 // change dynamic colors
1981 // change dynamic ui colors
1993 // manipulate selection data
2011 this.currentParam
= 0;
2012 this.state
= normal
;
2014 if (!this.params
.length
) {
2015 if (ch
>= '0' && ch
<= '9') {
2017 this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
2018 } else if (ch
=== ';') {
2019 this.params
.push(this.currentParam
);
2020 this.currentParam
= '';
2023 this.currentParam
+= ch
;
2030 if (ch
=== '?' || ch
=== '>' || ch
=== '!') {
2036 if (ch
>= '0' && ch
<= '9') {
2037 this.currentParam
= this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
2041 // '$', '"', ' ', '\''
2042 if (ch
=== '$' || ch
=== '"' || ch
=== ' ' || ch
=== '\'') {
2047 this.params
.push(this.currentParam
);
2048 this.currentParam
= 0;
2051 if (ch
=== ';') break;
2053 this.state
= normal
;
2057 // Cursor Up Ps Times (default = 1) (CUU).
2059 this.cursorUp(this.params
);
2063 // Cursor Down Ps Times (default = 1) (CUD).
2065 this.cursorDown(this.params
);
2069 // Cursor Forward Ps Times (default = 1) (CUF).
2071 this.cursorForward(this.params
);
2075 // Cursor Backward Ps Times (default = 1) (CUB).
2077 this.cursorBackward(this.params
);
2081 // Cursor Position [row;column] (default = [1,1]) (CUP).
2083 this.cursorPos(this.params
);
2086 // CSI Ps J Erase in Display (ED).
2088 this.eraseInDisplay(this.params
);
2091 // CSI Ps K Erase in Line (EL).
2093 this.eraseInLine(this.params
);
2096 // CSI Pm m Character Attributes (SGR).
2099 this.charAttributes(this.params
);
2103 // CSI Ps n Device Status Report (DSR).
2106 this.deviceStatus(this.params
);
2115 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
2117 this.insertChars(this.params
);
2121 // Cursor Next Line Ps Times (default = 1) (CNL).
2123 this.cursorNextLine(this.params
);
2127 // Cursor Preceding Line Ps Times (default = 1) (CNL).
2129 this.cursorPrecedingLine(this.params
);
2133 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
2135 this.cursorCharAbsolute(this.params
);
2139 // Insert Ps Line(s) (default = 1) (IL).
2141 this.insertLines(this.params
);
2145 // Delete Ps Line(s) (default = 1) (DL).
2147 this.deleteLines(this.params
);
2151 // Delete Ps Character(s) (default = 1) (DCH).
2153 this.deleteChars(this.params
);
2157 // Erase Ps Character(s) (default = 1) (ECH).
2159 this.eraseChars(this.params
);
2162 // CSI Pm ` Character Position Absolute
2163 // [column] (default = [row,1]) (HPA).
2165 this.charPosAbsolute(this.params
);
2169 // Horizontal Position Relative
2171 this.HPositionRelative(this.params
);
2175 // Send Device Attributes (Primary DA).
2177 // Send Device Attributes (Secondary DA)
2179 this.sendDeviceAttributes(this.params
);
2183 // Line Position Absolute [row] (default = [1,column]) (VPA).
2185 this.linePosAbsolute(this.params
);
2188 // 145 65 e * VPR - Vertical Position Relative
2190 this.VPositionRelative(this.params
);
2194 // Horizontal and Vertical Position [row;column] (default =
2197 this.HVPosition(this.params
);
2200 // CSI Pm h Set Mode (SM).
2201 // CSI ? Pm h - mouse escape codes, cursor escape codes
2203 this.setMode(this.params
);
2206 // CSI Pm l Reset Mode (RM).
2209 this.resetMode(this.params
);
2213 // Set Scrolling Region [top;bottom] (default = full size of win-
2217 this.setScrollRegion(this.params
);
2221 // Save cursor (ANSI.SYS).
2223 this.saveCursor(this.params
);
2227 // Restore cursor (ANSI.SYS).
2229 this.restoreCursor(this.params
);
2237 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
2239 this.cursorForwardTab(this.params
);
2242 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
2244 this.scrollUp(this.params
);
2247 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
2248 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
2251 // if (this.prefix === '>') {
2252 // this.resetTitleModes(this.params);
2255 // if (this.params.length > 2) {
2256 // this.initMouseTracking(this.params);
2259 if (this.params
.length
< 2 && !this.prefix
) {
2260 this.scrollDown(this.params
);
2265 // Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
2267 this.cursorBackwardTab(this.params
);
2270 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
2272 this.repeatPrecedingCharacter(this.params
);
2275 // CSI Ps g Tab Clear (TBC).
2277 this.tabClear(this.params
);
2280 // CSI Pm i Media Copy (MC).
2283 // this.mediaCopy(this.params);
2286 // CSI Pm m Character Attributes (SGR).
2288 // case 'm': // duplicate
2289 // if (this.prefix === '>') {
2290 // this.setResources(this.params);
2292 // this.charAttributes(this.params);
2296 // CSI Ps n Device Status Report (DSR).
2298 // case 'n': // duplicate
2299 // if (this.prefix === '>') {
2300 // this.disableModifiers(this.params);
2302 // this.deviceStatus(this.params);
2306 // CSI > Ps p Set pointer mode.
2307 // CSI ! p Soft terminal reset (DECSTR).
2309 // Request ANSI mode (DECRQM).
2311 // Request DEC private mode (DECRQM).
2314 switch (this.prefix
) {
2316 // this.setPointerMode(this.params);
2319 this.softReset(this.params
);
2322 // if (this.postfix === '$') {
2323 // this.requestPrivateMode(this.params);
2327 // if (this.postfix === '"') {
2328 // this.setConformanceLevel(this.params);
2329 // } else if (this.postfix === '$') {
2330 // this.requestAnsiMode(this.params);
2336 // CSI Ps q Load LEDs (DECLL).
2340 // if (this.postfix === ' ') {
2341 // this.setCursorStyle(this.params);
2344 // if (this.postfix === '"') {
2345 // this.setCharProtectionAttr(this.params);
2348 // this.loadLEDs(this.params);
2352 // Set Scrolling Region [top;bottom] (default = full size of win-
2355 // CSI Pt; Pl; Pb; Pr; Ps$ r
2356 // case 'r': // duplicate
2357 // if (this.prefix === '?') {
2358 // this.restorePrivateValues(this.params);
2359 // } else if (this.postfix === '$') {
2360 // this.setAttrInRectangle(this.params);
2362 // this.setScrollRegion(this.params);
2366 // CSI s Save cursor (ANSI.SYS).
2368 // case 's': // duplicate
2369 // if (this.prefix === '?') {
2370 // this.savePrivateValues(this.params);
2372 // this.saveCursor(this.params);
2376 // CSI Ps ; Ps ; Ps t
2377 // CSI Pt; Pl; Pb; Pr; Ps$ t
2381 // if (this.postfix === '$') {
2382 // this.reverseAttrInRectangle(this.params);
2383 // } else if (this.postfix === ' ') {
2384 // this.setWarningBellVolume(this.params);
2386 // if (this.prefix === '>') {
2387 // this.setTitleModeFeature(this.params);
2389 // this.manipulateWindow(this.params);
2394 // CSI u Restore cursor (ANSI.SYS).
2396 // case 'u': // duplicate
2397 // if (this.postfix === ' ') {
2398 // this.setMarginBellVolume(this.params);
2400 // this.restoreCursor(this.params);
2404 // CSI Pt; Pl; Pb; Pr; Pp; Pt; Pl; Pp$ v
2406 // if (this.postfix === '$') {
2407 // this.copyRectagle(this.params);
2411 // CSI Pt ; Pl ; Pb ; Pr ' w
2413 // if (this.postfix === '\'') {
2414 // this.enableFilterRectangle(this.params);
2418 // CSI Ps x Request Terminal Parameters (DECREQTPARM).
2419 // CSI Ps x Select Attribute Change Extent (DECSACE).
2420 // CSI Pc; Pt; Pl; Pb; Pr$ x
2422 // if (this.postfix === '$') {
2423 // this.fillRectangle(this.params);
2425 // this.requestParameters(this.params);
2426 // //this.__(this.params);
2431 // CSI Pt; Pl; Pb; Pr$ z
2433 // if (this.postfix === '\'') {
2434 // this.enableLocatorReporting(this.params);
2435 // } else if (this.postfix === '$') {
2436 // this.eraseRectangle(this.params);
2441 // CSI Pt; Pl; Pb; Pr$ {
2443 // if (this.postfix === '\'') {
2444 // this.setLocatorEvents(this.params);
2445 // } else if (this.postfix === '$') {
2446 // this.selectiveEraseRectangle(this.params);
2452 // if (this.postfix === '\'') {
2453 // this.requestLocatorPosition(this.params);
2458 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
2460 // if (this.postfix === ' ') {
2461 // this.insertColumns(this.params);
2466 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
2468 // if (this.postfix === ' ') {
2469 // this.deleteColumns(this.params);
2474 this.error('Unknown CSI code: %s.', ch
);
2483 if (ch
=== '\x1b' || ch
=== '\x07') {
2484 if (ch
=== '\x1b') i
++;
2486 switch (this.prefix
) {
2487 // User-Defined Keys (DECUDK).
2491 // Request Status String (DECRQSS).
2492 // test: echo -e '\eP$q"p\e\\'
2494 var pt
= this.currentParam
2511 + (this.scrollTop
+ 1)
2513 + (this.scrollBottom
+ 1)
2523 this.error('Unknown DCS Pt: %s.', pt
);
2528 this.send('\x1bP' + +valid
+ '$r' + pt
+ '\x1b\\');
2531 // Set Termcap/Terminfo Data (xterm, experimental).
2535 // Request Termcap/Terminfo String (xterm, experimental)
2536 // Regular xterm does not even respond to this sequence.
2537 // This can cause a small glitch in vim.
2538 // test: echo -ne '\eP+q6b64\e\\'
2540 var pt
= this.currentParam
2543 this.send('\x1bP' + +valid
+ '+r' + pt
+ '\x1b\\');
2547 this.error('Unknown DCS prefix: %s.', this.prefix
);
2551 this.currentParam
= 0;
2553 this.state
= normal
;
2554 } else if (!this.currentParam
) {
2555 if (!this.prefix
&& ch
!== '$' && ch
!== '+') {
2556 this.currentParam
= ch
;
2557 } else if (this.prefix
.length
=== 2) {
2558 this.currentParam
= ch
;
2563 this.currentParam
+= ch
;
2569 if (ch
=== '\x1b' || ch
=== '\x07') {
2570 if (ch
=== '\x1b') i
++;
2571 this.state
= normal
;
2577 this.updateRange(this.y
);
2578 this.refresh(this.refreshStart
, this.refreshEnd
);
2581 Terminal
.prototype.writeln = function(data
) {
2582 this.write(data
+ '\r\n');
2586 // https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
2587 Terminal
.prototype.keyDown = function(ev
) {
2589 var result
= this.evaluateKeyEscapeSequence(ev
);
2591 if (result
.scrollDisp
) {
2592 this.scrollDisp(result
.scrollDisp
);
2593 return this.cancel(ev
);
2596 if (isThirdLevelShift(this, ev
)) {
2600 if (result
.cancel
) {
2601 // The event is canceled at the end already, is this necessary?
2602 this.cancel(ev
, true);
2609 this.emit('keydown', ev
);
2610 this.emit('key', result
.key
, ev
);
2612 this.handler(result
.key
);
2614 return this.cancel(ev
, true);
2618 * Returns an object that determines how a KeyboardEvent should be handled. The key of the
2619 * returned value is the new key code to pass to the PTY.
2621 * Reference: http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
2623 Terminal
.prototype.evaluateKeyEscapeSequence = function(ev
) {
2625 // Whether to cancel event propogation (NOTE: this may not be needed since the event is
2626 // canceled at the end of keyDown
2628 // The new key even to emit
2630 // The number of characters to scroll, if this is defined it will cancel the event
2631 scrollDisp
: undefined
2633 switch (ev
.keyCode
) {
2637 result
.key
= '\x08'; // ^H
2640 result
.key
= '\x7f'; // ^?
2645 result
.key
= '\x1b[Z';
2649 result
.cancel
= true;
2654 result
.cancel
= true;
2658 result
.key
= '\x1b';
2659 result
.cancel
= true;
2664 result
.key
= '\x1bb' // Jump a word back
2665 result
.cancel
= true;
2669 result
.key
= '\x1b[5D'; // Jump a word back
2672 if (this.applicationCursor
) {
2673 result
.key
= '\x1bOD'; // SS3 as ^[O for 7-bit
2676 result
.key
= '\x1b[D';
2681 result
.key
= '\x1bf' // Jump a word forward
2682 result
.cancel
= true;
2686 result
.key
= '\x1b[5C'; // Jump a word forward
2689 if (this.applicationCursor
) {
2690 result
.key
= '\x1bOC';
2693 result
.key
= '\x1b[C';
2697 if (this.applicationCursor
) {
2698 result
.key
= '\x1bOA';
2702 result
.scrollDisp
= -1;
2704 result
.key
= '\x1b[A';
2709 if (this.applicationCursor
) {
2710 result
.key
= '\x1bOB';
2714 result
.scrollDisp
= 1;
2716 result
.key
= '\x1b[B';
2721 if (!ev
.shiftKey
&& !ev
.ctrlKey
) {
2722 // <Ctrl> or <Shift> + <Insert> are used to
2723 // copy-paste on some systems.
2724 result
.key
= '\x1b[2~';
2728 case 46: result
.key
= '\x1b[3~'; break;
2731 if (this.applicationKeypad
) {
2732 result
.key
= '\x1bOH';
2735 result
.key
= '\x1bOH';
2739 if (this.applicationKeypad
) {
2740 result
.key
= '\x1bOF';
2743 result
.key
= '\x1bOF';
2748 result
.scrollDisp
= -(this.rows
- 1);
2750 result
.key
= '\x1b[5~';
2756 result
.scrollDisp
= this.rows
- 1;
2758 result
.key
= '\x1b[6~';
2762 case 112: result
.key
= '\x1bOP'; break;
2763 case 113: result
.key
= '\x1bOQ'; break;
2764 case 114: result
.key
= '\x1bOR'; break;
2765 case 115: result
.key
= '\x1bOS'; break;
2766 case 116: result
.key
= '\x1b[15~'; break;
2767 case 117: result
.key
= '\x1b[17~'; break;
2768 case 118: result
.key
= '\x1b[18~'; break;
2769 case 119: result
.key
= '\x1b[19~'; break;
2770 case 120: result
.key
= '\x1b[20~'; break;
2771 case 121: result
.key
= '\x1b[21~'; break;
2772 case 122: result
.key
= '\x1b[23~'; break;
2773 case 123: result
.key
= '\x1b[24~'; break;
2776 if (ev
.ctrlKey
&& !ev
.shiftKey
&& !ev
.altKey
&& !ev
.metaKey
) {
2777 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2778 result
.key
= String
.fromCharCode(ev
.keyCode
- 64);
2779 } else if (ev
.keyCode
=== 32) {
2781 result
.key
= String
.fromCharCode(0);
2782 } else if (ev
.keyCode
>= 51 && ev
.keyCode
<= 55) {
2783 // escape, file sep, group sep, record sep, unit sep
2784 result
.key
= String
.fromCharCode(ev
.keyCode
- 51 + 27);
2785 } else if (ev
.keyCode
=== 56) {
2787 result
.key
= String
.fromCharCode(127);
2788 } else if (ev
.keyCode
=== 219) {
2790 result
.key
= String
.fromCharCode(27);
2791 } else if (ev
.keyCode
=== 221) {
2793 result
.key
= String
.fromCharCode(29);
2795 } else if (!this.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) {
2796 // On Mac this is a third level shift. Use <Esc> instead.
2797 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2798 result
.key
= '\x1b' + String
.fromCharCode(ev
.keyCode
+ 32);
2799 } else if (ev
.keyCode
=== 192) {
2800 result
.key
= '\x1b`';
2801 } else if (ev
.keyCode
>= 48 && ev
.keyCode
<= 57) {
2802 result
.key
= '\x1b' + (ev
.keyCode
- 48);
2810 Terminal
.prototype.setgLevel = function(g
) {
2812 this.charset
= this.charsets
[g
];
2815 Terminal
.prototype.setgCharset = function(g
, charset
) {
2816 this.charsets
[g
] = charset
;
2817 if (this.glevel
=== g
) {
2818 this.charset
= charset
;
2822 Terminal
.prototype.keyPress = function(ev
) {
2827 } else if (ev
.which
== null) {
2829 } else if (ev
.which
!== 0 && ev
.charCode
!== 0) {
2836 (ev
.altKey
|| ev
.ctrlKey
|| ev
.metaKey
) && !isThirdLevelShift(this, ev
)
2841 key
= String
.fromCharCode(key
);
2844 * When a key is pressed and a character is sent to the terminal, then clear any text
2845 * selected in the terminal.
2848 this.clearSelection();
2851 this.emit('keypress', key
, ev
);
2852 this.emit('key', key
, ev
);
2856 this.cancel(ev
, true);
2861 Terminal
.prototype.send = function(data
) {
2865 setTimeout(function() {
2866 self
.handler(self
.queue
);
2874 Terminal
.prototype.bell = function() {
2875 if (!this.visualBell
) return;
2877 this.element
.style
.borderColor
= 'white';
2878 setTimeout(function() {
2879 self
.element
.style
.borderColor
= '';
2881 if (this.popOnBell
) this.focus();
2884 Terminal
.prototype.log = function() {
2885 if (!this.debug
) return;
2886 if (!this.context
.console
|| !this.context
.console
.log
) return;
2887 var args
= Array
.prototype.slice
.call(arguments
);
2888 this.context
.console
.log
.apply(this.context
.console
, args
);
2891 Terminal
.prototype.error = function() {
2892 if (!this.debug
) return;
2893 if (!this.context
.console
|| !this.context
.console
.error
) return;
2894 var args
= Array
.prototype.slice
.call(arguments
);
2895 this.context
.console
.error
.apply(this.context
.console
, args
);
2899 * Resizes the terminal.
2901 * @param {number} x The number of columns to resize to.
2902 * @param {number} y The number of rows to resize to.
2906 Terminal
.prototype.resize = function(x
, y
) {
2914 if (x
=== this.cols
&& y
=== this.rows
) {
2924 ch
= [this.defAttr
, ' ']; // does xterm use the default attr?
2925 i
= this.lines
.length
;
2927 while (this.lines
[i
].length
< x
) {
2928 this.lines
[i
].push(ch
);
2932 i
= this.lines
.length
;
2934 while (this.lines
[i
].length
> x
) {
2935 this.lines
[i
].pop();
2948 // y is rows, not this.y
2949 if (this.lines
.length
< y
+ this.ybase
) {
2950 if (this.ybase
> 0 && this.lines
.length
<= this.ybase
+ this.y
+ addToY
+ 1) {
2951 // There is room above the buffer and there are no empty elements below the line,
2957 // Add a blank line if there is no buffer left at the top to scroll to, or if there
2958 // are blank lines after the cursor
2959 this.lines
.push(this.blankLine());
2962 if (this.children
.length
< y
) {
2968 if (this.lines
.length
> y
+ this.ybase
) {
2969 if (this.lines
.length
> this.ybase
+ this.y
+ 1) {
2970 // The line is a blank line below the cursor, remove it
2973 // The line is the cursor, scroll down
2978 if (this.children
.length
> y
) {
2979 el
= this.children
.shift();
2981 el
.parentNode
.removeChild(el
);
2988 * Make sure that the cursor stays on screen
3002 this.scrollBottom
= y
- 1;
3004 this.refresh(0, this.rows
- 1);
3008 this.emit('resize', {terminal
: this, cols
: x
, rows
: y
});
3011 Terminal
.prototype.updateRange = function(y
) {
3012 if (y
< this.refreshStart
) this.refreshStart
= y
;
3013 if (y
> this.refreshEnd
) this.refreshEnd
= y
;
3014 // if (y > this.refreshEnd) {
3015 // this.refreshEnd = y;
3016 // if (y > this.rows - 1) {
3017 // this.refreshEnd = this.rows - 1;
3022 Terminal
.prototype.maxRange = function() {
3023 this.refreshStart
= 0;
3024 this.refreshEnd
= this.rows
- 1;
3027 Terminal
.prototype.setupStops = function(i
) {
3029 if (!this.tabs
[i
]) {
3030 i
= this.prevStop(i
);
3037 for (; i
< this.cols
; i
+= 8) {
3038 this.tabs
[i
] = true;
3042 Terminal
.prototype.prevStop = function(x
) {
3043 if (x
== null) x
= this.x
;
3044 while (!this.tabs
[--x
] && x
> 0);
3045 return x
>= this.cols
3050 Terminal
.prototype.nextStop = function(x
) {
3051 if (x
== null) x
= this.x
;
3052 while (!this.tabs
[++x
] && x
< this.cols
);
3053 return x
>= this.cols
3058 Terminal
.prototype.eraseRight = function(x
, y
) {
3059 var line
= this.lines
[this.ybase
+ y
]
3060 , ch
= [this.eraseAttr(), ' ', 1]; // xterm
3063 for (; x
< this.cols
; x
++) {
3067 this.updateRange(y
);
3070 Terminal
.prototype.eraseLeft = function(x
, y
) {
3071 var line
= this.lines
[this.ybase
+ y
]
3072 , ch
= [this.eraseAttr(), ' ', 1]; // xterm
3075 while (x
--) line
[x
] = ch
;
3077 this.updateRange(y
);
3080 Terminal
.prototype.eraseLine = function(y
) {
3081 this.eraseRight(0, y
);
3084 Terminal
.prototype.blankLine = function(cur
) {
3089 var ch
= [attr
, ' ', 1] // width defaults to 1 halfwidth character
3093 for (; i
< this.cols
; i
++) {
3100 Terminal
.prototype.ch = function(cur
) {
3102 ? [this.eraseAttr(), ' ', 1]
3103 : [this.defAttr
, ' ', 1];
3106 Terminal
.prototype.is = function(term
) {
3107 var name
= this.termName
;
3108 return (name
+ '').indexOf(term
) === 0;
3111 Terminal
.prototype.handler = function(data
) {
3112 this.emit('data', data
);
3115 Terminal
.prototype.handleTitle = function(title
) {
3116 this.emit('title', title
);
3123 // ESC D Index (IND is 0x84).
3124 Terminal
.prototype.index = function() {
3126 if (this.y
> this.scrollBottom
) {
3130 this.state
= normal
;
3133 // ESC M Reverse Index (RI is 0x8d).
3134 Terminal
.prototype.reverseIndex = function() {
3137 if (this.y
< this.scrollTop
) {
3139 // possibly move the code below to term.reverseScroll();
3140 // test: echo -ne '\e[1;1H\e[44m\eM\e[0m'
3141 // blankLine(true) is xterm/linux behavior
3142 this.lines
.splice(this.y
+ this.ybase
, 0, this.blankLine(true));
3143 j
= this.rows
- 1 - this.scrollBottom
;
3144 this.lines
.splice(this.rows
- 1 + this.ybase
- j
+ 1, 1);
3146 this.updateRange(this.scrollTop
);
3147 this.updateRange(this.scrollBottom
);
3149 this.state
= normal
;
3152 // ESC c Full Reset (RIS).
3153 Terminal
.prototype.reset = function() {
3154 this.options
.rows
= this.rows
;
3155 this.options
.cols
= this.cols
;
3156 Terminal
.call(this, this.options
);
3157 this.refresh(0, this.rows
- 1);
3160 // ESC H Tab Set (HTS is 0x88).
3161 Terminal
.prototype.tabSet = function() {
3162 this.tabs
[this.x
] = true;
3163 this.state
= normal
;
3171 // Cursor Up Ps Times (default = 1) (CUU).
3172 Terminal
.prototype.cursorUp = function(params
) {
3173 var param
= params
[0];
3174 if (param
< 1) param
= 1;
3176 if (this.y
< 0) this.y
= 0;
3180 // Cursor Down Ps Times (default = 1) (CUD).
3181 Terminal
.prototype.cursorDown = function(params
) {
3182 var param
= params
[0];
3183 if (param
< 1) param
= 1;
3185 if (this.y
>= this.rows
) {
3186 this.y
= this.rows
- 1;
3191 // Cursor Forward Ps Times (default = 1) (CUF).
3192 Terminal
.prototype.cursorForward = function(params
) {
3193 var param
= params
[0];
3194 if (param
< 1) param
= 1;
3196 if (this.x
>= this.cols
) {
3197 this.x
= this.cols
- 1;
3202 // Cursor Backward Ps Times (default = 1) (CUB).
3203 Terminal
.prototype.cursorBackward = function(params
) {
3204 var param
= params
[0];
3205 if (param
< 1) param
= 1;
3207 if (this.x
< 0) this.x
= 0;
3211 // Cursor Position [row;column] (default = [1,1]) (CUP).
3212 Terminal
.prototype.cursorPos = function(params
) {
3215 row
= params
[0] - 1;
3217 if (params
.length
>= 2) {
3218 col
= params
[1] - 1;
3225 } else if (row
>= this.rows
) {
3226 row
= this.rows
- 1;
3231 } else if (col
>= this.cols
) {
3232 col
= this.cols
- 1;
3239 // CSI Ps J Erase in Display (ED).
3240 // Ps = 0 -> Erase Below (default).
3241 // Ps = 1 -> Erase Above.
3242 // Ps = 2 -> Erase All.
3243 // Ps = 3 -> Erase Saved Lines (xterm).
3245 // Erase in Display (DECSED).
3246 // Ps = 0 -> Selective Erase Below (default).
3247 // Ps = 1 -> Selective Erase Above.
3248 // Ps = 2 -> Selective Erase All.
3249 Terminal
.prototype.eraseInDisplay = function(params
) {
3251 switch (params
[0]) {
3253 this.eraseRight(this.x
, this.y
);
3255 for (; j
< this.rows
; j
++) {
3260 this.eraseLeft(this.x
, this.y
);
3268 while (j
--) this.eraseLine(j
);
3276 // CSI Ps K Erase in Line (EL).
3277 // Ps = 0 -> Erase to Right (default).
3278 // Ps = 1 -> Erase to Left.
3279 // Ps = 2 -> Erase All.
3281 // Erase in Line (DECSEL).
3282 // Ps = 0 -> Selective Erase to Right (default).
3283 // Ps = 1 -> Selective Erase to Left.
3284 // Ps = 2 -> Selective Erase All.
3285 Terminal
.prototype.eraseInLine = function(params
) {
3286 switch (params
[0]) {
3288 this.eraseRight(this.x
, this.y
);
3291 this.eraseLeft(this.x
, this.y
);
3294 this.eraseLine(this.y
);
3299 // CSI Pm m Character Attributes (SGR).
3300 // Ps = 0 -> Normal (default).
3302 // Ps = 4 -> Underlined.
3303 // Ps = 5 -> Blink (appears as Bold).
3304 // Ps = 7 -> Inverse.
3305 // Ps = 8 -> Invisible, i.e., hidden (VT300).
3306 // Ps = 2 2 -> Normal (neither bold nor faint).
3307 // Ps = 2 4 -> Not underlined.
3308 // Ps = 2 5 -> Steady (not blinking).
3309 // Ps = 2 7 -> Positive (not inverse).
3310 // Ps = 2 8 -> Visible, i.e., not hidden (VT300).
3311 // Ps = 3 0 -> Set foreground color to Black.
3312 // Ps = 3 1 -> Set foreground color to Red.
3313 // Ps = 3 2 -> Set foreground color to Green.
3314 // Ps = 3 3 -> Set foreground color to Yellow.
3315 // Ps = 3 4 -> Set foreground color to Blue.
3316 // Ps = 3 5 -> Set foreground color to Magenta.
3317 // Ps = 3 6 -> Set foreground color to Cyan.
3318 // Ps = 3 7 -> Set foreground color to White.
3319 // Ps = 3 9 -> Set foreground color to default (original).
3320 // Ps = 4 0 -> Set background color to Black.
3321 // Ps = 4 1 -> Set background color to Red.
3322 // Ps = 4 2 -> Set background color to Green.
3323 // Ps = 4 3 -> Set background color to Yellow.
3324 // Ps = 4 4 -> Set background color to Blue.
3325 // Ps = 4 5 -> Set background color to Magenta.
3326 // Ps = 4 6 -> Set background color to Cyan.
3327 // Ps = 4 7 -> Set background color to White.
3328 // Ps = 4 9 -> Set background color to default (original).
3330 // If 16-color support is compiled, the following apply. Assume
3331 // that xterm's resources are set so that the ISO color codes are
3332 // the first 8 of a set of 16. Then the aixterm colors are the
3333 // bright versions of the ISO colors:
3334 // Ps = 9 0 -> Set foreground color to Black.
3335 // Ps = 9 1 -> Set foreground color to Red.
3336 // Ps = 9 2 -> Set foreground color to Green.
3337 // Ps = 9 3 -> Set foreground color to Yellow.
3338 // Ps = 9 4 -> Set foreground color to Blue.
3339 // Ps = 9 5 -> Set foreground color to Magenta.
3340 // Ps = 9 6 -> Set foreground color to Cyan.
3341 // Ps = 9 7 -> Set foreground color to White.
3342 // Ps = 1 0 0 -> Set background color to Black.
3343 // Ps = 1 0 1 -> Set background color to Red.
3344 // Ps = 1 0 2 -> Set background color to Green.
3345 // Ps = 1 0 3 -> Set background color to Yellow.
3346 // Ps = 1 0 4 -> Set background color to Blue.
3347 // Ps = 1 0 5 -> Set background color to Magenta.
3348 // Ps = 1 0 6 -> Set background color to Cyan.
3349 // Ps = 1 0 7 -> Set background color to White.
3351 // If xterm is compiled with the 16-color support disabled, it
3352 // supports the following, from rxvt:
3353 // Ps = 1 0 0 -> Set foreground and background color to
3356 // If 88- or 256-color support is compiled, the following apply.
3357 // Ps = 3 8 ; 5 ; Ps -> Set foreground color to the second
3359 // Ps = 4 8 ; 5 ; Ps -> Set background color to the second
3361 Terminal
.prototype.charAttributes = function(params
) {
3362 // Optimize a single SGR0.
3363 if (params
.length
=== 1 && params
[0] === 0) {
3364 this.curAttr
= this.defAttr
;
3368 var l
= params
.length
3370 , flags
= this.curAttr
>> 18
3371 , fg
= (this.curAttr
>> 9) & 0x1ff
3372 , bg
= this.curAttr
& 0x1ff
3375 for (; i
< l
; i
++) {
3377 if (p
>= 30 && p
<= 37) {
3380 } else if (p
>= 40 && p
<= 47) {
3383 } else if (p
>= 90 && p
<= 97) {
3387 } else if (p
>= 100 && p
<= 107) {
3391 } else if (p
=== 0) {
3393 flags
= this.defAttr
>> 18;
3394 fg
= (this.defAttr
>> 9) & 0x1ff;
3395 bg
= this.defAttr
& 0x1ff;
3399 } else if (p
=== 1) {
3402 } else if (p
=== 4) {
3405 } else if (p
=== 5) {
3408 } else if (p
=== 7) {
3409 // inverse and positive
3410 // test with: echo -e '\e[31m\e[42mhello\e[7mworld\e[27mhi\e[m'
3412 } else if (p
=== 8) {
3415 } else if (p
=== 22) {
3418 } else if (p
=== 24) {
3421 } else if (p
=== 25) {
3424 } else if (p
=== 27) {
3427 } else if (p
=== 28) {
3430 } else if (p
=== 39) {
3432 fg
= (this.defAttr
>> 9) & 0x1ff;
3433 } else if (p
=== 49) {
3435 bg
= this.defAttr
& 0x1ff;
3436 } else if (p
=== 38) {
3438 if (params
[i
+ 1] === 2) {
3442 params
[i
+ 1] & 0xff,
3443 params
[i
+ 2] & 0xff);
3444 if (fg
=== -1) fg
= 0x1ff;
3446 } else if (params
[i
+ 1] === 5) {
3448 p
= params
[i
] & 0xff;
3451 } else if (p
=== 48) {
3453 if (params
[i
+ 1] === 2) {
3457 params
[i
+ 1] & 0xff,
3458 params
[i
+ 2] & 0xff);
3459 if (bg
=== -1) bg
= 0x1ff;
3461 } else if (params
[i
+ 1] === 5) {
3463 p
= params
[i
] & 0xff;
3466 } else if (p
=== 100) {
3468 fg
= (this.defAttr
>> 9) & 0x1ff;
3469 bg
= this.defAttr
& 0x1ff;
3471 this.error('Unknown SGR attribute: %d.', p
);
3475 this.curAttr
= (flags
<< 18) | (fg
<< 9) | bg
;
3478 // CSI Ps n Device Status Report (DSR).
3479 // Ps = 5 -> Status Report. Result (``OK'') is
3481 // Ps = 6 -> Report Cursor Position (CPR) [row;column].
3485 // Device Status Report (DSR, DEC-specific).
3486 // Ps = 6 -> Report Cursor Position (CPR) [row;column] as CSI
3487 // ? r ; c R (assumes page is zero).
3488 // Ps = 1 5 -> Report Printer status as CSI ? 1 0 n (ready).
3489 // or CSI ? 1 1 n (not ready).
3490 // Ps = 2 5 -> Report UDK status as CSI ? 2 0 n (unlocked)
3491 // or CSI ? 2 1 n (locked).
3492 // Ps = 2 6 -> Report Keyboard status as
3493 // CSI ? 2 7 ; 1 ; 0 ; 0 n (North American).
3494 // The last two parameters apply to VT400 & up, and denote key-
3495 // board ready and LK01 respectively.
3496 // Ps = 5 3 -> Report Locator status as
3497 // CSI ? 5 3 n Locator available, if compiled-in, or
3498 // CSI ? 5 0 n No Locator, if not.
3499 Terminal
.prototype.deviceStatus = function(params
) {
3501 switch (params
[0]) {
3504 this.send('\x1b[0n');
3515 } else if (this.prefix
=== '?') {
3516 // modern xterm doesnt seem to
3517 // respond to any of these except ?6, 6, and 5
3518 switch (params
[0]) {
3529 // this.send('\x1b[?11n');
3532 // dont support user defined keys
3533 // this.send('\x1b[?21n');
3536 // north american keyboard
3537 // this.send('\x1b[?27;1;0;0n');
3540 // no dec locator/mouse
3541 // this.send('\x1b[?50n');
3552 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
3553 Terminal
.prototype.insertChars = function(params
) {
3554 var param
, row
, j
, ch
;
3557 if (param
< 1) param
= 1;
3559 row
= this.y
+ this.ybase
;
3561 ch
= [this.eraseAttr(), ' ']; // xterm
3563 while (param
-- && j
< this.cols
) {
3564 this.lines
[row
].splice(j
++, 0, ch
);
3565 this.lines
[row
].pop();
3570 // Cursor Next Line Ps Times (default = 1) (CNL).
3571 // same as CSI Ps B ?
3572 Terminal
.prototype.cursorNextLine = function(params
) {
3573 var param
= params
[0];
3574 if (param
< 1) param
= 1;
3576 if (this.y
>= this.rows
) {
3577 this.y
= this.rows
- 1;
3583 // Cursor Preceding Line Ps Times (default = 1) (CNL).
3585 Terminal
.prototype.cursorPrecedingLine = function(params
) {
3586 var param
= params
[0];
3587 if (param
< 1) param
= 1;
3589 if (this.y
< 0) this.y
= 0;
3594 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
3595 Terminal
.prototype.cursorCharAbsolute = function(params
) {
3596 var param
= params
[0];
3597 if (param
< 1) param
= 1;
3602 // Insert Ps Line(s) (default = 1) (IL).
3603 Terminal
.prototype.insertLines = function(params
) {
3607 if (param
< 1) param
= 1;
3608 row
= this.y
+ this.ybase
;
3610 j
= this.rows
- 1 - this.scrollBottom
;
3611 j
= this.rows
- 1 + this.ybase
- j
+ 1;
3614 // test: echo -e '\e[44m\e[1L\e[0m'
3615 // blankLine(true) - xterm/linux behavior
3616 this.lines
.splice(row
, 0, this.blankLine(true));
3617 this.lines
.splice(j
, 1);
3621 this.updateRange(this.y
);
3622 this.updateRange(this.scrollBottom
);
3626 // Delete Ps Line(s) (default = 1) (DL).
3627 Terminal
.prototype.deleteLines = function(params
) {
3631 if (param
< 1) param
= 1;
3632 row
= this.y
+ this.ybase
;
3634 j
= this.rows
- 1 - this.scrollBottom
;
3635 j
= this.rows
- 1 + this.ybase
- j
;
3638 // test: echo -e '\e[44m\e[1M\e[0m'
3639 // blankLine(true) - xterm/linux behavior
3640 this.lines
.splice(j
+ 1, 0, this.blankLine(true));
3641 this.lines
.splice(row
, 1);
3645 this.updateRange(this.y
);
3646 this.updateRange(this.scrollBottom
);
3650 // Delete Ps Character(s) (default = 1) (DCH).
3651 Terminal
.prototype.deleteChars = function(params
) {
3655 if (param
< 1) param
= 1;
3657 row
= this.y
+ this.ybase
;
3658 ch
= [this.eraseAttr(), ' ']; // xterm
3661 this.lines
[row
].splice(this.x
, 1);
3662 this.lines
[row
].push(ch
);
3667 // Erase Ps Character(s) (default = 1) (ECH).
3668 Terminal
.prototype.eraseChars = function(params
) {
3669 var param
, row
, j
, ch
;
3672 if (param
< 1) param
= 1;
3674 row
= this.y
+ this.ybase
;
3676 ch
= [this.eraseAttr(), ' ']; // xterm
3678 while (param
-- && j
< this.cols
) {
3679 this.lines
[row
][j
++] = ch
;
3683 // CSI Pm ` Character Position Absolute
3684 // [column] (default = [row,1]) (HPA).
3685 Terminal
.prototype.charPosAbsolute = function(params
) {
3686 var param
= params
[0];
3687 if (param
< 1) param
= 1;
3689 if (this.x
>= this.cols
) {
3690 this.x
= this.cols
- 1;
3695 // Horizontal Position Relative
3697 Terminal
.prototype.HPositionRelative = function(params
) {
3698 var param
= params
[0];
3699 if (param
< 1) param
= 1;
3701 if (this.x
>= this.cols
) {
3702 this.x
= this.cols
- 1;
3706 // CSI Ps c Send Device Attributes (Primary DA).
3707 // Ps = 0 or omitted -> request attributes from terminal. The
3708 // response depends on the decTerminalID resource setting.
3709 // -> CSI ? 1 ; 2 c (``VT100 with Advanced Video Option'')
3710 // -> CSI ? 1 ; 0 c (``VT101 with No Options'')
3711 // -> CSI ? 6 c (``VT102'')
3712 // -> CSI ? 6 0 ; 1 ; 2 ; 6 ; 8 ; 9 ; 1 5 ; c (``VT220'')
3713 // The VT100-style response parameters do not mean anything by
3714 // themselves. VT220 parameters do, telling the host what fea-
3715 // tures the terminal supports:
3716 // Ps = 1 -> 132-columns.
3717 // Ps = 2 -> Printer.
3718 // Ps = 6 -> Selective erase.
3719 // Ps = 8 -> User-defined keys.
3720 // Ps = 9 -> National replacement character sets.
3721 // Ps = 1 5 -> Technical characters.
3722 // Ps = 2 2 -> ANSI color, e.g., VT525.
3723 // Ps = 2 9 -> ANSI text locator (i.e., DEC Locator mode).
3725 // Send Device Attributes (Secondary DA).
3726 // Ps = 0 or omitted -> request the terminal's identification
3727 // code. The response depends on the decTerminalID resource set-
3728 // ting. It should apply only to VT220 and up, but xterm extends
3730 // -> CSI > Pp ; Pv ; Pc c
3731 // where Pp denotes the terminal type
3732 // Pp = 0 -> ``VT100''.
3733 // Pp = 1 -> ``VT220''.
3734 // and Pv is the firmware version (for xterm, this was originally
3735 // the XFree86 patch number, starting with 95). In a DEC termi-
3736 // nal, Pc indicates the ROM cartridge registration number and is
3738 // More information:
3739 // xterm/charproc.c - line 2012, for more information.
3740 // vim responds with ^[[?0c or ^[[?1c after the terminal's response (?)
3741 Terminal
.prototype.sendDeviceAttributes = function(params
) {
3742 if (params
[0] > 0) return;
3745 if (this.is('xterm')
3746 || this.is('rxvt-unicode')
3747 || this.is('screen')) {
3748 this.send('\x1b[?1;2c');
3749 } else if (this.is('linux')) {
3750 this.send('\x1b[?6c');
3752 } else if (this.prefix
=== '>') {
3754 // seem to spit this
3755 // out around ~370 times (?).
3756 if (this.is('xterm')) {
3757 this.send('\x1b[>0;276;0c');
3758 } else if (this.is('rxvt-unicode')) {
3759 this.send('\x1b[>85;95;0c');
3760 } else if (this.is('linux')) {
3761 // not supported by linux console.
3762 // linux console echoes parameters.
3763 this.send(params
[0] + 'c');
3764 } else if (this.is('screen')) {
3765 this.send('\x1b[>83;40003;0c');
3771 // Line Position Absolute [row] (default = [1,column]) (VPA).
3772 Terminal
.prototype.linePosAbsolute = function(params
) {
3773 var param
= params
[0];
3774 if (param
< 1) param
= 1;
3776 if (this.y
>= this.rows
) {
3777 this.y
= this.rows
- 1;
3781 // 145 65 e * VPR - Vertical Position Relative
3783 Terminal
.prototype.VPositionRelative = function(params
) {
3784 var param
= params
[0];
3785 if (param
< 1) param
= 1;
3787 if (this.y
>= this.rows
) {
3788 this.y
= this.rows
- 1;
3793 // Horizontal and Vertical Position [row;column] (default =
3795 Terminal
.prototype.HVPosition = function(params
) {
3796 if (params
[0] < 1) params
[0] = 1;
3797 if (params
[1] < 1) params
[1] = 1;
3799 this.y
= params
[0] - 1;
3800 if (this.y
>= this.rows
) {
3801 this.y
= this.rows
- 1;
3804 this.x
= params
[1] - 1;
3805 if (this.x
>= this.cols
) {
3806 this.x
= this.cols
- 1;
3810 // CSI Pm h Set Mode (SM).
3811 // Ps = 2 -> Keyboard Action Mode (AM).
3812 // Ps = 4 -> Insert Mode (IRM).
3813 // Ps = 1 2 -> Send/receive (SRM).
3814 // Ps = 2 0 -> Automatic Newline (LNM).
3816 // DEC Private Mode Set (DECSET).
3817 // Ps = 1 -> Application Cursor Keys (DECCKM).
3818 // Ps = 2 -> Designate USASCII for character sets G0-G3
3819 // (DECANM), and set VT100 mode.
3820 // Ps = 3 -> 132 Column Mode (DECCOLM).
3821 // Ps = 4 -> Smooth (Slow) Scroll (DECSCLM).
3822 // Ps = 5 -> Reverse Video (DECSCNM).
3823 // Ps = 6 -> Origin Mode (DECOM).
3824 // Ps = 7 -> Wraparound Mode (DECAWM).
3825 // Ps = 8 -> Auto-repeat Keys (DECARM).
3826 // Ps = 9 -> Send Mouse X & Y on button press. See the sec-
3827 // tion Mouse Tracking.
3828 // Ps = 1 0 -> Show toolbar (rxvt).
3829 // Ps = 1 2 -> Start Blinking Cursor (att610).
3830 // Ps = 1 8 -> Print form feed (DECPFF).
3831 // Ps = 1 9 -> Set print extent to full screen (DECPEX).
3832 // Ps = 2 5 -> Show Cursor (DECTCEM).
3833 // Ps = 3 0 -> Show scrollbar (rxvt).
3834 // Ps = 3 5 -> Enable font-shifting functions (rxvt).
3835 // Ps = 3 8 -> Enter Tektronix Mode (DECTEK).
3836 // Ps = 4 0 -> Allow 80 -> 132 Mode.
3837 // Ps = 4 1 -> more(1) fix (see curses resource).
3838 // Ps = 4 2 -> Enable Nation Replacement Character sets (DECN-
3840 // Ps = 4 4 -> Turn On Margin Bell.
3841 // Ps = 4 5 -> Reverse-wraparound Mode.
3842 // Ps = 4 6 -> Start Logging. This is normally disabled by a
3843 // compile-time option.
3844 // Ps = 4 7 -> Use Alternate Screen Buffer. (This may be dis-
3845 // abled by the titeInhibit resource).
3846 // Ps = 6 6 -> Application keypad (DECNKM).
3847 // Ps = 6 7 -> Backarrow key sends backspace (DECBKM).
3848 // Ps = 1 0 0 0 -> Send Mouse X & Y on button press and
3849 // release. See the section Mouse Tracking.
3850 // Ps = 1 0 0 1 -> Use Hilite Mouse Tracking.
3851 // Ps = 1 0 0 2 -> Use Cell Motion Mouse Tracking.
3852 // Ps = 1 0 0 3 -> Use All Motion Mouse Tracking.
3853 // Ps = 1 0 0 4 -> Send FocusIn/FocusOut events.
3854 // Ps = 1 0 0 5 -> Enable Extended Mouse Mode.
3855 // Ps = 1 0 1 0 -> Scroll to bottom on tty output (rxvt).
3856 // Ps = 1 0 1 1 -> Scroll to bottom on key press (rxvt).
3857 // Ps = 1 0 3 4 -> Interpret "meta" key, sets eighth bit.
3858 // (enables the eightBitInput resource).
3859 // Ps = 1 0 3 5 -> Enable special modifiers for Alt and Num-
3860 // Lock keys. (This enables the numLock resource).
3861 // Ps = 1 0 3 6 -> Send ESC when Meta modifies a key. (This
3862 // enables the metaSendsEscape resource).
3863 // Ps = 1 0 3 7 -> Send DEL from the editing-keypad Delete
3865 // Ps = 1 0 3 9 -> Send ESC when Alt modifies a key. (This
3866 // enables the altSendsEscape resource).
3867 // Ps = 1 0 4 0 -> Keep selection even if not highlighted.
3868 // (This enables the keepSelection resource).
3869 // Ps = 1 0 4 1 -> Use the CLIPBOARD selection. (This enables
3870 // the selectToClipboard resource).
3871 // Ps = 1 0 4 2 -> Enable Urgency window manager hint when
3872 // Control-G is received. (This enables the bellIsUrgent
3874 // Ps = 1 0 4 3 -> Enable raising of the window when Control-G
3875 // is received. (enables the popOnBell resource).
3876 // Ps = 1 0 4 7 -> Use Alternate Screen Buffer. (This may be
3877 // disabled by the titeInhibit resource).
3878 // Ps = 1 0 4 8 -> Save cursor as in DECSC. (This may be dis-
3879 // abled by the titeInhibit resource).
3880 // Ps = 1 0 4 9 -> Save cursor as in DECSC and use Alternate
3881 // Screen Buffer, clearing it first. (This may be disabled by
3882 // the titeInhibit resource). This combines the effects of the 1
3883 // 0 4 7 and 1 0 4 8 modes. Use this with terminfo-based
3884 // applications rather than the 4 7 mode.
3885 // Ps = 1 0 5 0 -> Set terminfo/termcap function-key mode.
3886 // Ps = 1 0 5 1 -> Set Sun function-key mode.
3887 // Ps = 1 0 5 2 -> Set HP function-key mode.
3888 // Ps = 1 0 5 3 -> Set SCO function-key mode.
3889 // Ps = 1 0 6 0 -> Set legacy keyboard emulation (X11R6).
3890 // Ps = 1 0 6 1 -> Set VT220 keyboard emulation.
3891 // Ps = 2 0 0 4 -> Set bracketed paste mode.
3893 // http://vt100.net/docs/vt220-rm/chapter4.html
3894 Terminal
.prototype.setMode = function(params
) {
3895 if (typeof params
=== 'object') {
3896 var l
= params
.length
3899 for (; i
< l
; i
++) {
3900 this.setMode(params
[i
]);
3909 this.insertMode
= true;
3912 //this.convertEol = true;
3915 } else if (this.prefix
=== '?') {
3918 this.applicationCursor
= true;
3921 this.setgCharset(0, Terminal
.charsets
.US
);
3922 this.setgCharset(1, Terminal
.charsets
.US
);
3923 this.setgCharset(2, Terminal
.charsets
.US
);
3924 this.setgCharset(3, Terminal
.charsets
.US
);
3925 // set VT100 mode here
3927 case 3: // 132 col mode
3928 this.savedCols
= this.cols
;
3929 this.resize(132, this.rows
);
3932 this.originMode
= true;
3935 this.wraparoundMode
= true;
3938 // this.cursorBlink = true;
3941 this.log('Serial port requested application keypad.');
3942 this.applicationKeypad
= true;
3944 case 9: // X10 Mouse
3945 // no release, no motion, no wheel, no modifiers.
3946 case 1000: // vt200 mouse
3948 // no modifiers, except control on the wheel.
3949 case 1002: // button event mouse
3950 case 1003: // any event mouse
3951 // any event - sends motion events,
3952 // even if there is no button held down.
3953 this.x10Mouse
= params
=== 9;
3954 this.vt200Mouse
= params
=== 1000;
3955 this.normalMouse
= params
> 1000;
3956 this.mouseEvents
= true;
3957 this.element
.style
.cursor
= 'default';
3958 this.log('Binding to mouse events.');
3960 case 1004: // send focusin/focusout events
3963 this.sendFocus
= true;
3965 case 1005: // utf8 ext mode mouse
3966 this.utfMouse
= true;
3967 // for wide terminals
3968 // simply encodes large values as utf8 characters
3970 case 1006: // sgr ext mode mouse
3971 this.sgrMouse
= true;
3972 // for wide terminals
3973 // does not add 32 to fields
3974 // press: ^[[<b;x;yM
3975 // release: ^[[<b;x;ym
3977 case 1015: // urxvt ext mode mouse
3978 this.urxvtMouse
= true;
3979 // for wide terminals
3980 // numbers for fields
3982 // motion: ^[[b;x;yT
3984 case 25: // show cursor
3985 this.cursorHidden
= false;
3987 case 1049: // alt screen buffer cursor
3988 //this.saveCursor();
3990 case 47: // alt screen buffer
3991 case 1047: // alt screen buffer
3999 scrollTop
: this.scrollTop
,
4000 scrollBottom
: this.scrollBottom
,
4002 // XXX save charset(s) here?
4003 // charset: this.charset,
4004 // glevel: this.glevel,
4005 // charsets: this.charsets
4008 this.normal
= normal
;
4016 // CSI Pm l Reset Mode (RM).
4017 // Ps = 2 -> Keyboard Action Mode (AM).
4018 // Ps = 4 -> Replace Mode (IRM).
4019 // Ps = 1 2 -> Send/receive (SRM).
4020 // Ps = 2 0 -> Normal Linefeed (LNM).
4022 // DEC Private Mode Reset (DECRST).
4023 // Ps = 1 -> Normal Cursor Keys (DECCKM).
4024 // Ps = 2 -> Designate VT52 mode (DECANM).
4025 // Ps = 3 -> 80 Column Mode (DECCOLM).
4026 // Ps = 4 -> Jump (Fast) Scroll (DECSCLM).
4027 // Ps = 5 -> Normal Video (DECSCNM).
4028 // Ps = 6 -> Normal Cursor Mode (DECOM).
4029 // Ps = 7 -> No Wraparound Mode (DECAWM).
4030 // Ps = 8 -> No Auto-repeat Keys (DECARM).
4031 // Ps = 9 -> Don't send Mouse X & Y on button press.
4032 // Ps = 1 0 -> Hide toolbar (rxvt).
4033 // Ps = 1 2 -> Stop Blinking Cursor (att610).
4034 // Ps = 1 8 -> Don't print form feed (DECPFF).
4035 // Ps = 1 9 -> Limit print to scrolling region (DECPEX).
4036 // Ps = 2 5 -> Hide Cursor (DECTCEM).
4037 // Ps = 3 0 -> Don't show scrollbar (rxvt).
4038 // Ps = 3 5 -> Disable font-shifting functions (rxvt).
4039 // Ps = 4 0 -> Disallow 80 -> 132 Mode.
4040 // Ps = 4 1 -> No more(1) fix (see curses resource).
4041 // Ps = 4 2 -> Disable Nation Replacement Character sets (DEC-
4043 // Ps = 4 4 -> Turn Off Margin Bell.
4044 // Ps = 4 5 -> No Reverse-wraparound Mode.
4045 // Ps = 4 6 -> Stop Logging. (This is normally disabled by a
4046 // compile-time option).
4047 // Ps = 4 7 -> Use Normal Screen Buffer.
4048 // Ps = 6 6 -> Numeric keypad (DECNKM).
4049 // Ps = 6 7 -> Backarrow key sends delete (DECBKM).
4050 // Ps = 1 0 0 0 -> Don't send Mouse X & Y on button press and
4051 // release. See the section Mouse Tracking.
4052 // Ps = 1 0 0 1 -> Don't use Hilite Mouse Tracking.
4053 // Ps = 1 0 0 2 -> Don't use Cell Motion Mouse Tracking.
4054 // Ps = 1 0 0 3 -> Don't use All Motion Mouse Tracking.
4055 // Ps = 1 0 0 4 -> Don't send FocusIn/FocusOut events.
4056 // Ps = 1 0 0 5 -> Disable Extended Mouse Mode.
4057 // Ps = 1 0 1 0 -> Don't scroll to bottom on tty output
4059 // Ps = 1 0 1 1 -> Don't scroll to bottom on key press (rxvt).
4060 // Ps = 1 0 3 4 -> Don't interpret "meta" key. (This disables
4061 // the eightBitInput resource).
4062 // Ps = 1 0 3 5 -> Disable special modifiers for Alt and Num-
4063 // Lock keys. (This disables the numLock resource).
4064 // Ps = 1 0 3 6 -> Don't send ESC when Meta modifies a key.
4065 // (This disables the metaSendsEscape resource).
4066 // Ps = 1 0 3 7 -> Send VT220 Remove from the editing-keypad
4068 // Ps = 1 0 3 9 -> Don't send ESC when Alt modifies a key.
4069 // (This disables the altSendsEscape resource).
4070 // Ps = 1 0 4 0 -> Do not keep selection when not highlighted.
4071 // (This disables the keepSelection resource).
4072 // Ps = 1 0 4 1 -> Use the PRIMARY selection. (This disables
4073 // the selectToClipboard resource).
4074 // Ps = 1 0 4 2 -> Disable Urgency window manager hint when
4075 // Control-G is received. (This disables the bellIsUrgent
4077 // Ps = 1 0 4 3 -> Disable raising of the window when Control-
4078 // G is received. (This disables the popOnBell resource).
4079 // Ps = 1 0 4 7 -> Use Normal Screen Buffer, clearing screen
4080 // first if in the Alternate Screen. (This may be disabled by
4081 // the titeInhibit resource).
4082 // Ps = 1 0 4 8 -> Restore cursor as in DECRC. (This may be
4083 // disabled by the titeInhibit resource).
4084 // Ps = 1 0 4 9 -> Use Normal Screen Buffer and restore cursor
4085 // as in DECRC. (This may be disabled by the titeInhibit
4086 // resource). This combines the effects of the 1 0 4 7 and 1 0
4087 // 4 8 modes. Use this with terminfo-based applications rather
4088 // than the 4 7 mode.
4089 // Ps = 1 0 5 0 -> Reset terminfo/termcap function-key mode.
4090 // Ps = 1 0 5 1 -> Reset Sun function-key mode.
4091 // Ps = 1 0 5 2 -> Reset HP function-key mode.
4092 // Ps = 1 0 5 3 -> Reset SCO function-key mode.
4093 // Ps = 1 0 6 0 -> Reset legacy keyboard emulation (X11R6).
4094 // Ps = 1 0 6 1 -> Reset keyboard emulation to Sun/PC style.
4095 // Ps = 2 0 0 4 -> Reset bracketed paste mode.
4096 Terminal
.prototype.resetMode = function(params
) {
4097 if (typeof params
=== 'object') {
4098 var l
= params
.length
4101 for (; i
< l
; i
++) {
4102 this.resetMode(params
[i
]);
4111 this.insertMode
= false;
4114 //this.convertEol = false;
4117 } else if (this.prefix
=== '?') {
4120 this.applicationCursor
= false;
4123 if (this.cols
=== 132 && this.savedCols
) {
4124 this.resize(this.savedCols
, this.rows
);
4126 delete this.savedCols
;
4129 this.originMode
= false;
4132 this.wraparoundMode
= false;
4135 // this.cursorBlink = false;
4138 this.log('Switching back to normal keypad.');
4139 this.applicationKeypad
= false;
4141 case 9: // X10 Mouse
4142 case 1000: // vt200 mouse
4143 case 1002: // button event mouse
4144 case 1003: // any event mouse
4145 this.x10Mouse
= false;
4146 this.vt200Mouse
= false;
4147 this.normalMouse
= false;
4148 this.mouseEvents
= false;
4149 this.element
.style
.cursor
= '';
4151 case 1004: // send focusin/focusout events
4152 this.sendFocus
= false;
4154 case 1005: // utf8 ext mode mouse
4155 this.utfMouse
= false;
4157 case 1006: // sgr ext mode mouse
4158 this.sgrMouse
= false;
4160 case 1015: // urxvt ext mode mouse
4161 this.urxvtMouse
= false;
4163 case 25: // hide cursor
4164 this.cursorHidden
= true;
4166 case 1049: // alt screen buffer cursor
4168 case 47: // normal screen buffer
4169 case 1047: // normal screen buffer - clearing it first
4171 this.lines
= this.normal
.lines
;
4172 this.ybase
= this.normal
.ybase
;
4173 this.ydisp
= this.normal
.ydisp
;
4174 this.x
= this.normal
.x
;
4175 this.y
= this.normal
.y
;
4176 this.scrollTop
= this.normal
.scrollTop
;
4177 this.scrollBottom
= this.normal
.scrollBottom
;
4178 this.tabs
= this.normal
.tabs
;
4180 // if (params === 1049) {
4181 // this.x = this.savedX;
4182 // this.y = this.savedY;
4184 this.refresh(0, this.rows
- 1);
4193 // Set Scrolling Region [top;bottom] (default = full size of win-
4196 Terminal
.prototype.setScrollRegion = function(params
) {
4197 if (this.prefix
) return;
4198 this.scrollTop
= (params
[0] || 1) - 1;
4199 this.scrollBottom
= (params
[1] || this.rows
) - 1;
4205 // Save cursor (ANSI.SYS).
4206 Terminal
.prototype.saveCursor = function(params
) {
4207 this.savedX
= this.x
;
4208 this.savedY
= this.y
;
4212 // Restore cursor (ANSI.SYS).
4213 Terminal
.prototype.restoreCursor = function(params
) {
4214 this.x
= this.savedX
|| 0;
4215 this.y
= this.savedY
|| 0;
4223 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
4224 Terminal
.prototype.cursorForwardTab = function(params
) {
4225 var param
= params
[0] || 1;
4227 this.x
= this.nextStop();
4231 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
4232 Terminal
.prototype.scrollUp = function(params
) {
4233 var param
= params
[0] || 1;
4235 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
4236 this.lines
.splice(this.ybase
+ this.scrollBottom
, 0, this.blankLine());
4239 this.updateRange(this.scrollTop
);
4240 this.updateRange(this.scrollBottom
);
4243 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
4244 Terminal
.prototype.scrollDown = function(params
) {
4245 var param
= params
[0] || 1;
4247 this.lines
.splice(this.ybase
+ this.scrollBottom
, 1);
4248 this.lines
.splice(this.ybase
+ this.scrollTop
, 0, this.blankLine());
4251 this.updateRange(this.scrollTop
);
4252 this.updateRange(this.scrollBottom
);
4255 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
4256 // Initiate highlight mouse tracking. Parameters are
4257 // [func;startx;starty;firstrow;lastrow]. See the section Mouse
4259 Terminal
.prototype.initMouseTracking = function(params
) {
4260 // Relevant: DECSET 1001
4264 // Reset one or more features of the title modes to the default
4265 // value. Normally, "reset" disables the feature. It is possi-
4266 // ble to disable the ability to reset features by compiling a
4267 // different default for the title modes into xterm.
4268 // Ps = 0 -> Do not set window/icon labels using hexadecimal.
4269 // Ps = 1 -> Do not query window/icon labels using hexadeci-
4271 // Ps = 2 -> Do not set window/icon labels using UTF-8.
4272 // Ps = 3 -> Do not query window/icon labels using UTF-8.
4273 // (See discussion of "Title Modes").
4274 Terminal
.prototype.resetTitleModes = function(params
) {
4278 // CSI Ps Z Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
4279 Terminal
.prototype.cursorBackwardTab = function(params
) {
4280 var param
= params
[0] || 1;
4282 this.x
= this.prevStop();
4286 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
4287 Terminal
.prototype.repeatPrecedingCharacter = function(params
) {
4288 var param
= params
[0] || 1
4289 , line
= this.lines
[this.ybase
+ this.y
]
4290 , ch
= line
[this.x
- 1] || [this.defAttr
, ' '];
4292 while (param
--) line
[this.x
++] = ch
;
4295 // CSI Ps g Tab Clear (TBC).
4296 // Ps = 0 -> Clear Current Column (default).
4297 // Ps = 3 -> Clear All.
4299 // Ps = 2 -> Clear Stops on Line.
4300 // http://vt100.net/annarbor/aaa-ug/section6.html
4301 Terminal
.prototype.tabClear = function(params
) {
4302 var param
= params
[0];
4304 delete this.tabs
[this.x
];
4305 } else if (param
=== 3) {
4310 // CSI Pm i Media Copy (MC).
4311 // Ps = 0 -> Print screen (default).
4312 // Ps = 4 -> Turn off printer controller mode.
4313 // Ps = 5 -> Turn on printer controller mode.
4315 // Media Copy (MC, DEC-specific).
4316 // Ps = 1 -> Print line containing cursor.
4317 // Ps = 4 -> Turn off autoprint mode.
4318 // Ps = 5 -> Turn on autoprint mode.
4319 // Ps = 1 0 -> Print composed display, ignores DECPEX.
4320 // Ps = 1 1 -> Print all pages.
4321 Terminal
.prototype.mediaCopy = function(params
) {
4326 // Set or reset resource-values used by xterm to decide whether
4327 // to construct escape sequences holding information about the
4328 // modifiers pressed with a given key. The first parameter iden-
4329 // tifies the resource to set/reset. The second parameter is the
4330 // value to assign to the resource. If the second parameter is
4331 // omitted, the resource is reset to its initial value.
4332 // Ps = 1 -> modifyCursorKeys.
4333 // Ps = 2 -> modifyFunctionKeys.
4334 // Ps = 4 -> modifyOtherKeys.
4335 // If no parameters are given, all resources are reset to their
4337 Terminal
.prototype.setResources = function(params
) {
4342 // Disable modifiers which may be enabled via the CSI > Ps; Ps m
4343 // sequence. This corresponds to a resource value of "-1", which
4344 // cannot be set with the other sequence. The parameter identi-
4345 // fies the resource to be disabled:
4346 // Ps = 1 -> modifyCursorKeys.
4347 // Ps = 2 -> modifyFunctionKeys.
4348 // Ps = 4 -> modifyOtherKeys.
4349 // If the parameter is omitted, modifyFunctionKeys is disabled.
4350 // When modifyFunctionKeys is disabled, xterm uses the modifier
4351 // keys to make an extended sequence of functions rather than
4352 // adding a parameter to each function key to denote the modi-
4354 Terminal
.prototype.disableModifiers = function(params
) {
4359 // Set resource value pointerMode. This is used by xterm to
4360 // decide whether to hide the pointer cursor as the user types.
4361 // Valid values for the parameter:
4362 // Ps = 0 -> never hide the pointer.
4363 // Ps = 1 -> hide if the mouse tracking mode is not enabled.
4364 // Ps = 2 -> always hide the pointer. If no parameter is
4365 // given, xterm uses the default, which is 1 .
4366 Terminal
.prototype.setPointerMode = function(params
) {
4370 // CSI ! p Soft terminal reset (DECSTR).
4371 // http://vt100.net/docs/vt220-rm/table4-10.html
4372 Terminal
.prototype.softReset = function(params
) {
4373 this.cursorHidden
= false;
4374 this.insertMode
= false;
4375 this.originMode
= false;
4376 this.wraparoundMode
= false; // autowrap
4377 this.applicationKeypad
= false; // ?
4378 this.applicationCursor
= false;
4380 this.scrollBottom
= this.rows
- 1;
4381 this.curAttr
= this.defAttr
;
4382 this.x
= this.y
= 0; // ?
4383 this.charset
= null;
4384 this.glevel
= 0; // ??
4385 this.charsets
= [null]; // ??
4389 // Request ANSI mode (DECRQM). For VT300 and up, reply is
4391 // where Ps is the mode number as in RM, and Pm is the mode
4393 // 0 - not recognized
4396 // 3 - permanently set
4397 // 4 - permanently reset
4398 Terminal
.prototype.requestAnsiMode = function(params
) {
4403 // Request DEC private mode (DECRQM). For VT300 and up, reply is
4405 // where Ps is the mode number as in DECSET, Pm is the mode value
4406 // as in the ANSI DECRQM.
4407 Terminal
.prototype.requestPrivateMode = function(params
) {
4412 // Set conformance level (DECSCL). Valid values for the first
4414 // Ps = 6 1 -> VT100.
4415 // Ps = 6 2 -> VT200.
4416 // Ps = 6 3 -> VT300.
4417 // Valid values for the second parameter:
4418 // Ps = 0 -> 8-bit controls.
4419 // Ps = 1 -> 7-bit controls (always set for VT100).
4420 // Ps = 2 -> 8-bit controls.
4421 Terminal
.prototype.setConformanceLevel = function(params
) {
4425 // CSI Ps q Load LEDs (DECLL).
4426 // Ps = 0 -> Clear all LEDS (default).
4427 // Ps = 1 -> Light Num Lock.
4428 // Ps = 2 -> Light Caps Lock.
4429 // Ps = 3 -> Light Scroll Lock.
4430 // Ps = 2 1 -> Extinguish Num Lock.
4431 // Ps = 2 2 -> Extinguish Caps Lock.
4432 // Ps = 2 3 -> Extinguish Scroll Lock.
4433 Terminal
.prototype.loadLEDs = function(params
) {
4438 // Set cursor style (DECSCUSR, VT520).
4439 // Ps = 0 -> blinking block.
4440 // Ps = 1 -> blinking block (default).
4441 // Ps = 2 -> steady block.
4442 // Ps = 3 -> blinking underline.
4443 // Ps = 4 -> steady underline.
4444 Terminal
.prototype.setCursorStyle = function(params
) {
4449 // Select character protection attribute (DECSCA). Valid values
4450 // for the parameter:
4451 // Ps = 0 -> DECSED and DECSEL can erase (default).
4452 // Ps = 1 -> DECSED and DECSEL cannot erase.
4453 // Ps = 2 -> DECSED and DECSEL can erase.
4454 Terminal
.prototype.setCharProtectionAttr = function(params
) {
4459 // Restore DEC Private Mode Values. The value of Ps previously
4460 // saved is restored. Ps values are the same as for DECSET.
4461 Terminal
.prototype.restorePrivateValues = function(params
) {
4465 // CSI Pt; Pl; Pb; Pr; Ps$ r
4466 // Change Attributes in Rectangular Area (DECCARA), VT400 and up.
4467 // Pt; Pl; Pb; Pr denotes the rectangle.
4468 // Ps denotes the SGR attributes to change: 0, 1, 4, 5, 7.
4469 // NOTE: xterm doesn't enable this code by default.
4470 Terminal
.prototype.setAttrInRectangle = function(params
) {
4480 for (; t
< b
+ 1; t
++) {
4481 line
= this.lines
[this.ybase
+ t
];
4482 for (i
= l
; i
< r
; i
++) {
4483 line
[i
] = [attr
, line
[i
][1]];
4488 this.updateRange(params
[0]);
4489 this.updateRange(params
[2]);
4493 // CSI Pc; Pt; Pl; Pb; Pr$ x
4494 // Fill Rectangular Area (DECFRA), VT420 and up.
4495 // Pc is the character to use.
4496 // Pt; Pl; Pb; Pr denotes the rectangle.
4497 // NOTE: xterm doesn't enable this code by default.
4498 Terminal
.prototype.fillRectangle = function(params
) {
4508 for (; t
< b
+ 1; t
++) {
4509 line
= this.lines
[this.ybase
+ t
];
4510 for (i
= l
; i
< r
; i
++) {
4511 line
[i
] = [line
[i
][0], String
.fromCharCode(ch
)];
4516 this.updateRange(params
[1]);
4517 this.updateRange(params
[3]);
4521 // Enable Locator Reporting (DECELR).
4522 // Valid values for the first parameter:
4523 // Ps = 0 -> Locator disabled (default).
4524 // Ps = 1 -> Locator enabled.
4525 // Ps = 2 -> Locator enabled for one report, then disabled.
4526 // The second parameter specifies the coordinate unit for locator
4528 // Valid values for the second parameter:
4529 // Pu = 0 <- or omitted -> default to character cells.
4530 // Pu = 1 <- device physical pixels.
4531 // Pu = 2 <- character cells.
4532 Terminal
.prototype.enableLocatorReporting = function(params
) {
4533 var val
= params
[0] > 0;
4534 //this.mouseEvents = val;
4535 //this.decLocator = val;
4538 // CSI Pt; Pl; Pb; Pr$ z
4539 // Erase Rectangular Area (DECERA), VT400 and up.
4540 // Pt; Pl; Pb; Pr denotes the rectangle.
4541 // NOTE: xterm doesn't enable this code by default.
4542 Terminal
.prototype.eraseRectangle = function(params
) {
4552 ch
= [this.eraseAttr(), ' ']; // xterm?
4554 for (; t
< b
+ 1; t
++) {
4555 line
= this.lines
[this.ybase
+ t
];
4556 for (i
= l
; i
< r
; i
++) {
4562 this.updateRange(params
[0]);
4563 this.updateRange(params
[2]);
4568 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
4569 // NOTE: xterm doesn't enable this code by default.
4570 Terminal
.prototype.insertColumns = function() {
4571 var param
= params
[0]
4572 , l
= this.ybase
+ this.rows
4573 , ch
= [this.eraseAttr(), ' '] // xterm?
4577 for (i
= this.ybase
; i
< l
; i
++) {
4578 this.lines
[i
].splice(this.x
+ 1, 0, ch
);
4579 this.lines
[i
].pop();
4588 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
4589 // NOTE: xterm doesn't enable this code by default.
4590 Terminal
.prototype.deleteColumns = function() {
4591 var param
= params
[0]
4592 , l
= this.ybase
+ this.rows
4593 , ch
= [this.eraseAttr(), ' '] // xterm?
4597 for (i
= this.ybase
; i
< l
; i
++) {
4598 this.lines
[i
].splice(this.x
, 1);
4599 this.lines
[i
].push(ch
);
4610 Terminal
.charsets
= {};
4612 // DEC Special Character and Line Drawing Set.
4613 // http://vt100.net/docs/vt102-ug/table5-13.html
4614 // A lot of curses apps use this if they see TERM=xterm.
4615 // testing: echo -e '\e(0a\e(B'
4616 // The xterm output sometimes seems to conflict with the
4617 // reference above. xterm seems in line with the reference
4618 // when running vttest however.
4619 // The table below now uses xterm's output from vttest.
4620 Terminal
.charsets
.SCLD
= { // (0
4621 '`': '\u25c6', // '◆'
4622 'a': '\u2592', // '▒'
4623 'b': '\u0009', // '\t'
4624 'c': '\u000c', // '\f'
4625 'd': '\u000d', // '\r'
4626 'e': '\u000a', // '\n'
4627 'f': '\u00b0', // '°'
4628 'g': '\u00b1', // '±'
4629 'h': '\u2424', // '\u2424' (NL)
4630 'i': '\u000b', // '\v'
4631 'j': '\u2518', // '┘'
4632 'k': '\u2510', // '┐'
4633 'l': '\u250c', // '┌'
4634 'm': '\u2514', // '└'
4635 'n': '\u253c', // '┼'
4636 'o': '\u23ba', // '⎺'
4637 'p': '\u23bb', // '⎻'
4638 'q': '\u2500', // '─'
4639 'r': '\u23bc', // '⎼'
4640 's': '\u23bd', // '⎽'
4641 't': '\u251c', // '├'
4642 'u': '\u2524', // '┤'
4643 'v': '\u2534', // '┴'
4644 'w': '\u252c', // '┬'
4645 'x': '\u2502', // '│'
4646 'y': '\u2264', // '≤'
4647 'z': '\u2265', // '≥'
4648 '{': '\u03c0', // 'π'
4649 '|': '\u2260', // '≠'
4650 '}': '\u00a3', // '£'
4651 '~': '\u00b7' // '·'
4654 Terminal
.charsets
.UK
= null; // (A
4655 Terminal
.charsets
.US
= null; // (B (USASCII)
4656 Terminal
.charsets
.Dutch
= null; // (4
4657 Terminal
.charsets
.Finnish
= null; // (C or (5
4658 Terminal
.charsets
.French
= null; // (R
4659 Terminal
.charsets
.FrenchCanadian
= null; // (Q
4660 Terminal
.charsets
.German
= null; // (K
4661 Terminal
.charsets
.Italian
= null; // (Y
4662 Terminal
.charsets
.NorwegianDanish
= null; // (E or (6
4663 Terminal
.charsets
.Spanish
= null; // (Z
4664 Terminal
.charsets
.Swedish
= null; // (H or (7
4665 Terminal
.charsets
.Swiss
= null; // (=
4666 Terminal
.charsets
.ISOLatin
= null; // /A
4672 function contains(el
, arr
) {
4673 for (var i
= 0; i
< arr
.length
; i
+= 1) {
4674 if (el
=== arr
[i
]) {
4681 function on(el
, type
, handler
, capture
) {
4682 if (!Array
.isArray(el
)) {
4685 el
.forEach(function (element
) {
4686 element
.addEventListener(type
, handler
, capture
|| false);
4690 function off(el
, type
, handler
, capture
) {
4691 el
.removeEventListener(type
, handler
, capture
|| false);
4694 function cancel(ev
, force
) {
4695 if (!this.cancelEvents
&& !force
) {
4698 ev
.preventDefault();
4699 ev
.stopPropagation();
4703 function inherits(child
, parent
) {
4705 this.constructor = child
;
4707 f
.prototype = parent
.prototype;
4708 child
.prototype = new f
;
4711 // if bold is broken, we can't
4712 // use it in the terminal.
4713 function isBoldBroken(document
) {
4714 var body
= document
.getElementsByTagName('body')[0];
4715 var el
= document
.createElement('span');
4716 el
.innerHTML
= 'hello world';
4717 body
.appendChild(el
);
4718 var w1
= el
.scrollWidth
;
4719 el
.style
.fontWeight
= 'bold';
4720 var w2
= el
.scrollWidth
;
4721 body
.removeChild(el
);
4725 var String
= this.String
;
4726 var setTimeout
= this.setTimeout
;
4727 var setInterval
= this.setInterval
;
4729 function indexOf(obj
, el
) {
4732 if (obj
[i
] === el
) return i
;
4737 function isThirdLevelShift(term
, ev
) {
4739 (term
.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) ||
4740 (term
.isMSWindows
&& ev
.altKey
&& ev
.ctrlKey
&& !ev
.metaKey
);
4742 // Don't invoke for arrows, pageDown, home, backspace, etc.
4743 return thirdLevelKey
&& (!ev
.keyCode
|| ev
.keyCode
> 47);
4746 function isWide(ch
) {
4747 if (ch
<= '\uff00') return false;
4748 return (ch
>= '\uff01' && ch
<= '\uffbe')
4749 || (ch
>= '\uffc2' && ch
<= '\uffc7')
4750 || (ch
>= '\uffca' && ch
<= '\uffcf')
4751 || (ch
>= '\uffd2' && ch
<= '\uffd7')
4752 || (ch
>= '\uffda' && ch
<= '\uffdc')
4753 || (ch
>= '\uffe0' && ch
<= '\uffe6')
4754 || (ch
>= '\uffe8' && ch
<= '\uffee');
4757 function matchColor(r1
, g1
, b1
) {
4758 var hash
= (r1
<< 16) | (g1
<< 8) | b1
;
4760 if (matchColor
._cache
[hash
] != null) {
4761 return matchColor
._cache
[hash
];
4764 var ldiff
= Infinity
4773 for (; i
< Terminal
.vcolors
.length
; i
++) {
4774 c
= Terminal
.vcolors
[i
];
4779 diff
= matchColor
.distance(r1
, g1
, b1
, r2
, g2
, b2
);
4792 return matchColor
._cache
[hash
] = li
;
4795 matchColor
._cache
= {};
4797 // http://stackoverflow.com/questions/1633828
4798 matchColor
.distance = function(r1
, g1
, b1
, r2
, g2
, b2
) {
4799 return Math
.pow(30 * (r1
- r2
), 2)
4800 + Math
.pow(59 * (g1
- g2
), 2)
4801 + Math
.pow(11 * (b1
- b2
), 2);
4804 function each(obj
, iter
, con
) {
4805 if (obj
.forEach
) return obj
.forEach(iter
, con
);
4806 for (var i
= 0; i
< obj
.length
; i
++) {
4807 iter
.call(con
, obj
[i
], i
, obj
);
4811 function keys(obj
) {
4812 if (Object
.keys
) return Object
.keys(obj
);
4815 if (Object
.prototype.hasOwnProperty
.call(obj
, key
)) {
4826 Terminal
.EventEmitter
= EventEmitter
;
4827 Terminal
.inherits
= inherits
;
4830 * Adds an event listener to the terminal.
4832 * @param {string} event The name of the event. TODO: Document all event types
4833 * @param {function} callback The function to call when the event is triggered.
4839 Terminal
.cancel
= cancel
;