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
34 import { CompositionHelper
} from './CompositionHelper.js';
35 import { EventEmitter
} from './EventEmitter.js';
36 import { Viewport
} from './Viewport.js';
39 * Terminal Emulation References:
41 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.txt
42 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
43 * http://invisible-island.net/vttest/
44 * http://www.inwap.com/pdp10/ansicode.txt
45 * http://linux.die.net/man/4/console_codes
46 * http://linux.die.net/man/7/urxvt
49 // Let it work inside Node.js for automated testing purposes.
50 var document
= (typeof window
!= 'undefined') ? window
.document
: null;
55 var normal
= 0, escaped
= 1, csi
= 2, osc
= 3, charset
= 4, dcs
= 5, ignore
= 6;
62 * Creates a new `Terminal` object.
64 * @param {object} options An object containing a set of options, the available options are:
65 * - cursorBlink (boolean): Whether the terminal cursor blinks
69 * @alias module:xterm/src/xterm
71 function Terminal(options
) {
74 if (!(this instanceof Terminal
)) {
75 return new Terminal(arguments
[0], arguments
[1], arguments
[2]);
78 self
.cancel
= Terminal
.cancel
;
80 EventEmitter
.call(this);
82 if (typeof options
=== 'number') {
90 options
= options
|| {};
93 Object
.keys(Terminal
.defaults
).forEach(function(key
) {
94 if (options
[key
] == null) {
95 options
[key
] = Terminal
.options
[key
];
97 if (Terminal
[key
] !== Terminal
.defaults
[key
]) {
98 options
[key
] = Terminal
[key
];
101 self
[key
] = options
[key
];
104 if (options
.colors
.length
=== 8) {
105 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(8));
106 } else if (options
.colors
.length
=== 16) {
107 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(16));
108 } else if (options
.colors
.length
=== 10) {
109 options
.colors
= options
.colors
.slice(0, -2).concat(
110 Terminal
._colors
.slice(8, -2), options
.colors
.slice(-2));
111 } else if (options
.colors
.length
=== 18) {
112 options
.colors
= options
.colors
.concat(
113 Terminal
._colors
.slice(16, -2), options
.colors
.slice(-2));
115 this.colors
= options
.colors
;
117 this.options
= options
;
119 // this.context = options.context || window;
120 // this.document = options.document || document;
121 this.parent
= options
.body
|| options
.parent
|| (
122 document
? document
.getElementsByTagName('body')[0] : null
125 this.cols
= options
.cols
|| options
.geometry
[0];
126 this.rows
= options
.rows
|| options
.geometry
[1];
128 if (options
.handler
) {
129 this.on('data', options
.handler
);
133 * The scroll position of the y cursor, ie. ybase + y = the y position within the entire
139 * The scroll position of the viewport
144 * The cursor's x position after ybase
149 * The cursor's y position after ybase
154 * Used to debounce the refresh function
156 this.isRefreshing
= false;
159 * Whether there is a full terminal refresh queued
162 this.cursorState
= 0;
163 this.cursorHidden
= false;
168 this.scrollBottom
= this.rows
- 1;
169 this.customKeydownHandler
= null;
172 this.applicationKeypad
= false;
173 this.applicationCursor
= false;
174 this.originMode
= false;
175 this.insertMode
= false;
176 this.wraparoundMode
= true; // defaults: xterm - true, vt100 - false
181 this.gcharset
= null;
183 this.charsets
= [null];
207 this.readable
= true;
208 this.writable
= true;
210 this.defAttr
= (0 << 18) | (257 << 9) | (256 << 0);
211 this.curAttr
= this.defAttr
;
214 this.currentParam
= 0;
218 // leftover surrogate high from previous write invocation
219 this.surrogate_high
= '';
222 * An array of all lines in the entire buffer, including the prompt. The lines are array of
223 * characters which are 2-length arrays where [0] is an attribute and [1] is the character.
228 this.lines
.push(this.blankLine());
235 inherits(Terminal
, EventEmitter
);
238 * back_color_erase feature for xterm.
240 Terminal
.prototype.eraseAttr = function() {
241 // if (this.is('screen')) return this.defAttr;
242 return (this.defAttr
& ~0x1ff) | (this.curAttr
& 0x1ff);
250 Terminal
.tangoColors
= [
271 // Colors 0-15 + 16-255
272 // Much thanks to TooTallNate for writing this.
273 Terminal
.colors
= (function() {
274 var colors
= Terminal
.tangoColors
.slice()
275 , r
= [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff]
280 for (; i
< 216; i
++) {
281 out(r
[(i
/ 36) % 6 | 0], r
[(i
/ 6) % 6 | 0], r
[i
% 6]);
286 for (; i
< 24; i
++) {
291 function out(r
, g
, b
) {
292 colors
.push('#' + hex(r
) + hex(g
) + hex(b
));
297 return c
.length
< 2 ? '0' + c
: c
;
303 Terminal
._colors
= Terminal
.colors
.slice();
305 Terminal
.vcolors
= (function() {
307 , colors
= Terminal
.colors
311 for (; i
< 256; i
++) {
312 color
= parseInt(colors
[i
].substring(1), 16);
314 (color
>> 16) & 0xff,
327 Terminal
.defaults
= {
328 colors
: Terminal
.colors
,
340 // programFeatures: false,
344 Terminal
.options
= {};
346 Terminal
.focus
= null;
348 each(keys(Terminal
.defaults
), function(key
) {
349 Terminal
[key
] = Terminal
.defaults
[key
];
350 Terminal
.options
[key
] = Terminal
.defaults
[key
];
354 * Focus the terminal. Delegates focus handling to the terminal's DOM element.
356 Terminal
.prototype.focus = function() {
357 return this.textarea
.focus();
361 * Binds the desired focus behavior on a given terminal object.
365 Terminal
.bindFocus = function (term
) {
366 on(term
.textarea
, 'focus', function (ev
) {
367 if (term
.sendFocus
) {
370 term
.element
.classList
.add('focus');
372 Terminal
.focus
= term
;
373 term
.emit('focus', {terminal
: term
});
378 * Blur the terminal. Delegates blur handling to the terminal's DOM element.
380 Terminal
.prototype.blur = function() {
381 return this.textarea
.blur();
385 * Binds the desired blur behavior on a given terminal object.
389 Terminal
.bindBlur = function (term
) {
390 on(term
.textarea
, 'blur', function (ev
) {
391 term
.refresh(term
.y
, term
.y
);
392 if (term
.sendFocus
) {
395 term
.element
.classList
.remove('focus');
396 Terminal
.focus
= null;
397 term
.emit('blur', {terminal
: term
});
402 * Initialize default behavior
404 Terminal
.prototype.initGlobal = function() {
405 Terminal
.bindPaste(this);
406 Terminal
.bindKeys(this);
407 Terminal
.bindCopy(this);
408 Terminal
.bindFocus(this);
409 Terminal
.bindBlur(this);
413 * Bind to paste event and allow both keyboard and right-click pasting, without having the
414 * contentEditable value set to true.
416 Terminal
.bindPaste = function(term
) {
417 on([term
.textarea
, term
.element
], 'paste', function(ev
) {
418 ev
.stopPropagation();
419 if (ev
.clipboardData
) {
420 var text
= ev
.clipboardData
.getData('text/plain');
422 term
.textarea
.value
= '';
423 return term
.cancel(ev
);
429 * Prepares text copied from terminal selection, to be saved in the clipboard by:
430 * 1. stripping all trailing white spaces
431 * 2. converting all non-breaking spaces to regular spaces
432 * @param {string} text The copied text that needs processing for storing in clipboard
436 Terminal
.prepareCopiedTextForClipboard = function (text
) {
437 var space
= String
.fromCharCode(32),
438 nonBreakingSpace
= String
.fromCharCode(160),
439 allNonBreakingSpaces
= new RegExp(nonBreakingSpace
, 'g'),
440 processedText
= text
.split('\n').map(function (line
) {
442 * Strip all trailing white spaces and convert all non-breaking spaces to regular
445 var processedLine
= line
.replace(/\s+$/g, '').replace(allNonBreakingSpaces
, space
);
447 return processedLine
;
450 return processedText
;
454 * Apply key handling to the terminal
456 Terminal
.bindKeys = function(term
) {
457 on(term
.element
, 'keydown', function(ev
) {
458 if (document
.activeElement
!= this) {
464 on(term
.element
, 'keypress', function(ev
) {
465 if (document
.activeElement
!= this) {
471 on(term
.element
, 'keyup', term
.focus
.bind(term
));
473 on(term
.textarea
, 'keydown', function(ev
) {
477 on(term
.textarea
, 'keypress', function(ev
) {
479 // Truncate the textarea's value, since it is not needed
483 on(term
.textarea
, 'compositionstart', term
.compositionHelper
.compositionstart
.bind(term
.compositionHelper
));
484 on(term
.textarea
, 'compositionupdate', term
.compositionHelper
.compositionupdate
.bind(term
.compositionHelper
));
485 on(term
.textarea
, 'compositionend', term
.compositionHelper
.compositionend
.bind(term
.compositionHelper
));
486 term
.on('refresh', term
.compositionHelper
.updateCompositionElements
.bind(term
.compositionHelper
));
490 * Binds copy functionality to the given terminal.
493 Terminal
.bindCopy = function(term
) {
494 on(term
.element
, 'copy', function(ev
) {
501 * Insert the given row to the terminal or produce a new one
502 * if no row argument is passed. Return the inserted row.
503 * @param {HTMLElement} row (optional) The row to append to the terminal.
505 Terminal
.prototype.insertRow = function (row
) {
506 if (typeof row
!= 'object') {
507 row
= document
.createElement('div');
510 this.rowContainer
.appendChild(row
);
511 this.children
.push(row
);
517 * Opens the terminal within an element.
519 * @param {HTMLElement} parent The element to create the terminal within.
521 Terminal
.prototype.open = function(parent
) {
522 var self
=this, i
=0, div
;
524 this.parent
= parent
|| this.parent
;
527 throw new Error('Terminal requires a parent element.');
530 // Grab global elements
531 this.context
= this.parent
.ownerDocument
.defaultView
;
532 this.document
= this.parent
.ownerDocument
;
533 this.body
= this.document
.getElementsByTagName('body')[0];
536 if (this.context
.navigator
&& this.context
.navigator
.userAgent
) {
537 this.isMSIE
= !!~this.context
.navigator
.userAgent
.indexOf('MSIE');
540 // Find the users platform. We use this to interpret the meta key
541 // and ISO third level shifts.
542 // http://stackoverflow.com/q/19877924/577598
543 if (this.context
.navigator
&& this.context
.navigator
.platform
) {
544 this.isMac
= contains(
545 this.context
.navigator
.platform
,
546 ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K']
548 this.isIpad
= this.context
.navigator
.platform
=== 'iPad';
549 this.isIphone
= this.context
.navigator
.platform
=== 'iPhone';
550 this.isMSWindows
= contains(
551 this.context
.navigator
.platform
,
552 ['Windows', 'Win16', 'Win32', 'WinCE']
556 //Create main element container
557 this.element
= this.document
.createElement('div');
558 this.element
.classList
.add('terminal');
559 this.element
.classList
.add('xterm');
560 this.element
.classList
.add('xterm-theme-' + this.theme
);
562 this.element
.style
.height
563 this.element
.setAttribute('tabindex', 0);
565 this.viewportElement
= document
.createElement('div');
566 this.viewportElement
.classList
.add('xterm-viewport');
567 this.element
.appendChild(this.viewportElement
);
568 this.viewportScrollArea
= document
.createElement('div');
569 this.viewportScrollArea
.classList
.add('xterm-scroll-area');
570 this.viewportElement
.appendChild(this.viewportScrollArea
);
572 // Create the container that will hold the lines of the terminal and then
573 // produce the lines the lines.
574 this.rowContainer
= document
.createElement('div');
575 this.rowContainer
.classList
.add('xterm-rows');
576 this.element
.appendChild(this.rowContainer
);
579 // Create the container that will hold helpers like the textarea for
580 // capturing DOM Events. Then produce the helpers.
581 this.helperContainer
= document
.createElement('div');
582 this.helperContainer
.classList
.add('xterm-helpers');
583 // TODO: This should probably be inserted once it's filled to prevent an additional layout
584 this.element
.appendChild(this.helperContainer
);
585 this.textarea
= document
.createElement('textarea');
586 this.textarea
.classList
.add('xterm-helper-textarea');
587 this.textarea
.setAttribute('autocorrect', 'off');
588 this.textarea
.setAttribute('autocapitalize', 'off');
589 this.textarea
.setAttribute('spellcheck', 'false');
590 this.textarea
.tabIndex
= 0;
591 this.textarea
.addEventListener('focus', function() {
592 self
.emit('focus', {terminal
: self
});
594 this.textarea
.addEventListener('blur', function() {
595 self
.emit('blur', {terminal
: self
});
597 this.helperContainer
.appendChild(this.textarea
);
599 this.compositionView
= document
.createElement('div');
600 this.compositionView
.classList
.add('composition-view');
601 this.compositionHelper
= new CompositionHelper(this.textarea
, this.compositionView
, this);
602 this.helperContainer
.appendChild(this.compositionView
);
604 this.charMeasureElement
= document
.createElement('div');
605 this.charMeasureElement
.classList
.add('xterm-char-measure-element');
606 this.charMeasureElement
.innerHTML
= 'W';
607 this.helperContainer
.appendChild(this.charMeasureElement
);
609 for (; i
< this.rows
; i
++) {
612 this.parent
.appendChild(this.element
);
614 this.viewport
= new Viewport(this, this.viewportElement
, this.viewportScrollArea
, this.charMeasureElement
);
617 this.refresh(0, this.rows
- 1);
619 // Initialize global actions that
620 // need to be taken on the document.
623 // Ensure there is a Terminal.focus.
626 on(this.element
, 'mouseup', function() {
627 var selection
= document
.getSelection(),
628 collapsed
= selection
.isCollapsed
,
629 isRange
= typeof collapsed
== 'boolean' ? !collapsed
: selection
.type
== 'Range';
635 // Listen for mouse events and translate
636 // them into terminal mouse protocols.
639 // Figure out whether boldness affects
640 // the character width of monospace fonts.
641 if (Terminal
.brokenBold
== null) {
642 Terminal
.brokenBold
= isBoldBroken(this.document
);
650 * Attempts to load an add-on using CommonJS or RequireJS (whichever is available).
651 * @param {string} addon The name of the addon to load
654 Terminal
.loadAddon = function(addon
, callback
) {
655 if (typeof exports
=== 'object' && typeof module
=== 'object') {
657 return require(__dirname
+ '/../addons/' + addon
);
658 } else if (typeof define
== 'function') {
660 return require(['../addons/' + addon
+ '/' + addon
], callback
);
662 console
.error('Cannot load a module without a CommonJS or RequireJS environment.');
670 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#Mouse%20Tracking
671 * To better understand these
672 * the xterm code is very helpful:
674 * button.c, charproc.c, misc.c
675 * Relevant functions in xterm/button.c:
676 * BtnCode, EmitButtonCode, EditorButton, SendMousePosition
678 Terminal
.prototype.bindMouse = function() {
679 var el
= this.element
, self
= this, pressed
= 32;
681 // mouseup, mousedown, wheel
682 // left click: ^[[M 3<^[[M#3<
684 function sendButton(ev
) {
688 // get the xterm-style button
689 button
= getButton(ev
);
691 // get mouse coordinates
695 sendEvent(button
, pos
);
697 switch (ev
.overrideType
|| ev
.type
) {
702 // keep it at the left
703 // button, just in case.
714 // motion example of a left click:
715 // ^[[M 3<^[[M@4<^[[M@5<^[[M@6<^[[M@7<^[[M#7<
716 function sendMove(ev
) {
723 // buttons marked as motions
724 // are incremented by 32
727 sendEvent(button
, pos
);
731 // position to characters
732 function encode(data
, ch
) {
733 if (!self
.utfMouse
) {
734 if (ch
=== 255) return data
.push(0);
735 if (ch
> 127) ch
= 127;
738 if (ch
=== 2047) return data
.push(0);
742 if (ch
> 2047) ch
= 2047;
743 data
.push(0xC0 | (ch
>> 6));
744 data
.push(0x80 | (ch
& 0x3F));
749 // send a mouse event:
750 // regular/utf8: ^[[M Cb Cx Cy
751 // urxvt: ^[[ Cb ; Cx ; Cy M
752 // sgr: ^[[ Cb ; Cx ; Cy M/m
753 // vt300: ^[[ 24(1/3/5)~ [ Cx , Cy ] \r
754 // locator: CSI P e ; P b ; P r ; P c ; P p & w
755 function sendEvent(button
, pos
) {
756 // self.emit('mouse', {
762 if (self
.vt300Mouse
) {
764 // http://www.vt100.net/docs/vt3xx-gp/chapter15.html
768 var data
= '\x1b[24';
769 if (button
=== 0) data
+= '1';
770 else if (button
=== 1) data
+= '3';
771 else if (button
=== 2) data
+= '5';
772 else if (button
=== 3) return;
774 data
+= '~[' + pos
.x
+ ',' + pos
.y
+ ']\r';
779 if (self
.decLocator
) {
784 if (button
=== 0) button
= 2;
785 else if (button
=== 1) button
= 4;
786 else if (button
=== 2) button
= 6;
787 else if (button
=== 3) button
= 3;
791 + (button
=== 3 ? 4 : 0)
802 if (self
.urxvtMouse
) {
807 self
.send('\x1b[' + button
+ ';' + pos
.x
+ ';' + pos
.y
+ 'M');
815 + ((button
& 3) === 3 ? button
& ~3 : button
)
820 + ((button
& 3) === 3 ? 'm' : 'M'));
826 encode(data
, button
);
830 self
.send('\x1b[M' + String
.fromCharCode
.apply(String
, data
));
833 function getButton(ev
) {
846 // 1, and 2 - with 64 added
847 switch (ev
.overrideType
|| ev
.type
) {
849 button
= ev
.button
!= null
856 button
= button
=== 1 ? 0 : button
=== 4 ? 1 : button
;
862 case 'DOMMouseScroll':
863 button
= ev
.detail
< 0
868 button
= ev
.wheelDeltaY
> 0
874 // next three bits are the modifiers:
875 // 4 = shift, 8 = meta, 16 = control
876 shift
= ev
.shiftKey
? 4 : 0;
877 meta
= ev
.metaKey
? 8 : 0;
878 ctrl
= ev
.ctrlKey
? 16 : 0;
879 mod
= shift
| meta
| ctrl
;
882 if (self
.vt200Mouse
) {
885 } else if (!self
.normalMouse
) {
890 button
= (32 + (mod
<< 2)) + button
;
895 // mouse coordinates measured in cols/rows
896 function getCoords(ev
) {
899 // ignore browsers without pageX for now
900 if (ev
.pageX
== null) return;
906 // should probably check offsetParent
907 // but this is more portable
908 while (el
&& el
!== self
.document
.documentElement
) {
911 el
= 'offsetParent' in el
916 // convert to cols/rows
917 w
= self
.element
.clientWidth
;
918 h
= self
.element
.clientHeight
;
919 x
= Math
.ceil((x
/ w
) * self
.cols
);
920 y
= Math
.ceil((y
/ h
) * self
.rows
);
922 // be sure to avoid sending
923 // bad positions to the program
925 if (x
> self
.cols
) x
= self
.cols
;
927 if (y
> self
.rows
) y
= self
.rows
;
929 // xterm sends raw bytes and
930 // starts at 32 (SP) for each.
941 on(el
, 'mousedown', function(ev
) {
942 if (!self
.mouseEvents
) return;
951 //if (self.vt200Mouse && !self.normalMouse) {
952 if (self
.vt200Mouse
) {
953 ev
.overrideType
= 'mouseup';
955 return self
.cancel(ev
);
959 if (self
.normalMouse
) on(self
.document
, 'mousemove', sendMove
);
961 // x10 compatibility mode can't send button releases
962 if (!self
.x10Mouse
) {
963 on(self
.document
, 'mouseup', function up(ev
) {
965 if (self
.normalMouse
) off(self
.document
, 'mousemove', sendMove
);
966 off(self
.document
, 'mouseup', up
);
967 return self
.cancel(ev
);
971 return self
.cancel(ev
);
974 //if (self.normalMouse) {
975 // on(self.document, 'mousemove', sendMove);
978 on(el
, 'wheel', function(ev
) {
979 if (!self
.mouseEvents
) return;
982 || self
.decLocator
) return;
984 return self
.cancel(ev
);
987 // allow wheel scrolling in
988 // the shell for example
989 on(el
, 'wheel', function(ev
) {
990 if (self
.mouseEvents
) return;
991 if (self
.applicationKeypad
) return;
992 self
.viewport
.onWheel(ev
);
993 return self
.cancel(ev
);
998 * Destroys the terminal.
1000 Terminal
.prototype.destroy = function() {
1001 this.readable
= false;
1002 this.writable
= false;
1004 this.handler = function() {};
1005 this.write = function() {};
1006 if (this.element
.parentNode
) {
1007 this.element
.parentNode
.removeChild(this.element
);
1009 //this.emit('close');
1014 * Flags used to render terminal text properly
1025 * Refreshes (re-renders) terminal content within two rows (inclusive)
1029 * In the screen buffer, each character is stored as a an array with a character
1030 * and a 32-bit integer:
1031 * - First value: a utf-16 character.
1033 * - Next 9 bits: background color (0-511).
1034 * - Next 9 bits: foreground color (0-511).
1035 * - Next 14 bits: a mask for misc. flags:
1042 * @param {number} start The row to start from (between 0 and terminal's height terminal - 1)
1043 * @param {number} end The row to end at (between fromRow and terminal's height terminal - 1)
1044 * @param {boolean} queue Whether the refresh should ran right now or be queued
1046 Terminal
.prototype.refresh = function(start
, end
, queue
) {
1049 // queue defaults to true
1050 queue
= (typeof queue
== 'undefined') ? true : queue
;
1053 * The refresh queue allows refresh to execute only approximately 30 times a second. For
1054 * commands that pass a significant amount of output to the write function, this prevents the
1055 * terminal from maxing out the CPU and making the UI unresponsive. While commands can still
1056 * run beyond what they do on the terminal, it is far better with a debounce in place as
1057 * every single terminal manipulation does not need to be constructed in the DOM.
1059 * A side-effect of this is that it makes ^C to interrupt a process seem more responsive.
1062 // If refresh should be queued, order the refresh and return.
1063 if (this._refreshIsQueued
) {
1064 // If a refresh has already been queued, just order a full refresh next
1065 this._fullRefreshNext
= true;
1067 setTimeout(function () {
1068 self
.refresh(start
, end
, false);
1070 this._refreshIsQueued
= true;
1075 // If refresh should be run right now (not be queued), release the lock
1076 this._refreshIsQueued
= false;
1078 // If multiple refreshes were requested, make a full refresh.
1079 if (this._fullRefreshNext
) {
1081 end
= this.rows
- 1;
1082 this._fullRefreshNext
= false // reset lock
1085 var x
, y
, i
, line
, out
, ch
, ch_width
, width
, data
, attr
, bg
, fg
, flags
, row
, parent
, focused
= document
.activeElement
;
1087 // If this is a big refresh, remove the terminal rows from the DOM for faster calculations
1088 if (end
- start
>= this.rows
/ 2) {
1089 parent
= this.element
.parentNode
;
1091 this.element
.removeChild(this.rowContainer
);
1098 if (end
>= this.rows
.length
) {
1099 this.log('`end` is too large. Most likely a bad CSR.');
1100 end
= this.rows
.length
- 1;
1103 for (; y
<= end
; y
++) {
1104 row
= y
+ this.ydisp
;
1106 line
= this.lines
[row
];
1109 if (this.y
=== y
- (this.ybase
- this.ydisp
)
1111 && !this.cursorHidden
) {
1117 attr
= this.defAttr
;
1120 for (; i
< width
; i
++) {
1123 ch_width
= line
[i
][2];
1127 if (i
=== x
) data
= -1;
1129 if (data
!== attr
) {
1130 if (attr
!== this.defAttr
) {
1133 if (data
!== this.defAttr
) {
1135 out
+= '<span class="reverse-video terminal-cursor';
1136 if (this.cursorBlink
) {
1141 var classNames
= [];
1144 fg
= (data
>> 9) & 0x1ff;
1147 if (flags
& Terminal
.flags
.BOLD
) {
1148 if (!Terminal
.brokenBold
) {
1149 classNames
.push('xterm-bold');
1151 // See: XTerm*boldColors
1152 if (fg
< 8) fg
+= 8;
1155 if (flags
& Terminal
.flags
.UNDERLINE
) {
1156 classNames
.push('xterm-underline');
1159 if (flags
& Terminal
.flags
.BLINK
) {
1160 classNames
.push('xterm-blink');
1163 // If inverse flag is on, then swap the foreground and background variables.
1164 if (flags
& Terminal
.flags
.INVERSE
) {
1165 /* One-line variable swap in JavaScript: http://stackoverflow.com/a/16201730 */
1166 bg = [fg, fg = bg][0];
1167 // Should inverse just be before the
1168 // above boldColors effect instead?
1169 if ((flags & 1) && fg < 8) fg += 8;
1172 if (flags & Terminal.flags.INVISIBLE) {
1173 classNames.push('xterm-hidden');
1177 * Weird situation: Invert flag used black foreground and white background results
1178 * in invalid background color, positioned at the 256 index of the 256 terminal
1179 * color map. Pin the colors manually in such a case.
1181 * Source: https://github.com/sourcelair/xterm.js/issues/57
1183 if (flags
& Terminal
.flags
.INVERSE
) {
1193 classNames
.push('xterm-bg-color-' + bg
);
1197 classNames
.push('xterm-color-' + fg
);
1201 if (classNames
.length
) {
1202 out
+= ' class="' + classNames
.join(' ') + '"';
1231 if (attr
!== this.defAttr
) {
1235 this.children
[y
].innerHTML
= out
;
1239 this.element
.appendChild(this.rowContainer
);
1242 this.emit('refresh', {element
: this.element
, start
: start
, end
: end
});
1246 * Display the cursor element
1248 Terminal
.prototype.showCursor = function() {
1249 if (!this.cursorState
) {
1250 this.cursorState
= 1;
1251 this.refresh(this.y
, this.y
);
1256 * Scroll the terminal
1258 Terminal
.prototype.scroll = function() {
1261 if (++this.ybase
=== this.scrollback
) {
1262 this.ybase
= this.ybase
/ 2 | 0;
1263 this.lines
= this.lines
.slice(-(this.ybase
+ this.rows
) + 1);
1266 this.ydisp
= this.ybase
;
1269 row
= this.ybase
+ this.rows
- 1;
1271 // subtract the bottom scroll region
1272 row
-= this.rows
- 1 - this.scrollBottom
;
1274 if (row
=== this.lines
.length
) {
1275 // potential optimization:
1276 // pushing is faster than splicing
1277 // when they amount to the same
1279 this.lines
.push(this.blankLine());
1282 this.lines
.splice(row
, 0, this.blankLine());
1285 if (this.scrollTop
!== 0) {
1286 if (this.ybase
!== 0) {
1288 this.ydisp
= this.ybase
;
1290 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
1294 this.updateRange(this.scrollTop
);
1295 this.updateRange(this.scrollBottom
);
1297 this.emit('scroll', this.ydisp
);
1301 * Scroll the display of the terminal
1302 * @param {number} disp The number of lines to scroll down (negatives scroll up).
1303 * @param {boolean} suppressScrollEvent Don't emit the scroll event as scrollDisp. This is used
1304 * to avoid unwanted events being handled by the veiwport when the event was triggered from the
1305 * viewport originally.
1307 Terminal
.prototype.scrollDisp = function(disp
, suppressScrollEvent
) {
1310 if (this.ydisp
> this.ybase
) {
1311 this.ydisp
= this.ybase
;
1312 } else if (this.ydisp
< 0) {
1316 if (!suppressScrollEvent
) {
1317 this.emit('scroll', this.ydisp
);
1320 this.refresh(0, this.rows
- 1);
1324 * Writes text to the terminal.
1325 * @param {string} text The text to write to the terminal.
1327 Terminal
.prototype.write = function(data
) {
1328 var l
= data
.length
, i
= 0, j
, cs
, ch
, code
, low
, ch_width
, row
;
1330 this.refreshStart
= this.y
;
1331 this.refreshEnd
= this.y
;
1333 if (this.ybase
!== this.ydisp
) {
1334 this.ydisp
= this.ybase
;
1335 this.emit('scroll', this.ydisp
);
1339 // apply leftover surrogate high from last write
1340 if (this.surrogate_high
) {
1341 data
= this.surrogate_high
+ data
;
1342 this.surrogate_high
= '';
1345 for (; i
< l
; i
++) {
1348 // FIXME: higher chars than 0xa0 are not allowed in escape sequences
1349 // --> maybe move to default
1350 code
= data
.charCodeAt(i
);
1351 if (0xD800 <= code
&& code
<= 0xDBFF) {
1352 // we got a surrogate high
1353 // get surrogate low (next 2 bytes)
1354 low
= data
.charCodeAt(i
+1);
1356 // end of data stream, save surrogate high
1357 this.surrogate_high
= ch
;
1360 code
= ((code
- 0xD800) * 0x400) + (low
- 0xDC00) + 0x10000;
1361 ch
+= data
.charAt(i
+1);
1363 // surrogate low - already handled above
1364 if (0xDC00 <= code
&& code
<= 0xDFFF)
1367 switch (this.state
) {
1378 if (this.convertEol
) {
1382 if (this.y
> this.scrollBottom
) {
1402 this.x
= this.nextStop();
1417 this.state
= escaped
;
1422 // calculate print space
1423 // expensive call, therefore we save width in line buffer
1424 ch_width
= wcwidth(code
);
1427 if (this.charset
&& this.charset
[ch
]) {
1428 ch
= this.charset
[ch
];
1431 row
= this.y
+ this.ybase
;
1433 // insert combining char in last cell
1434 // FIXME: needs handling after cursor jumps
1435 if (!ch_width
&& this.x
) {
1437 // dont overflow left
1438 if (this.lines
[row
][this.x
-1]) {
1439 if (!this.lines
[row
][this.x
-1][2]) {
1441 // found empty cell after fullwidth, need to go 2 cells back
1442 if (this.lines
[row
][this.x
-2])
1443 this.lines
[row
][this.x
-2][1] += ch
;
1446 this.lines
[row
][this.x
-1][1] += ch
;
1448 this.updateRange(this.y
);
1453 // goto next line if ch would overflow
1454 // TODO: needs a global min terminal width of 2
1455 if (this.x
+ch_width
-1 >= this.cols
) {
1456 // autowrap - DECAWM
1457 if (this.wraparoundMode
) {
1460 if (this.y
> this.scrollBottom
) {
1465 this.x
= this.cols
-1;
1466 if(ch_width
===2) // FIXME: check for xterm behavior
1470 row
= this.y
+ this.ybase
;
1472 // insert mode: move characters to right
1473 if (this.insertMode
) {
1474 // do this twice for a fullwidth char
1475 for (var moves
=0; moves
<ch_width
; ++moves
) {
1476 // remove last cell, if it's width is 0
1477 // we have to adjust the second last cell as well
1478 var removed
= this.lines
[this.y
+ this.ybase
].pop();
1480 && this.lines
[row
][this.cols
-2]
1481 && this.lines
[row
][this.cols
-2][2]===2)
1482 this.lines
[row
][this.cols
-2] = [this.curAttr
, ' ', 1];
1484 // insert empty cell at cursor
1485 this.lines
[row
].splice(this.x
, 0, [this.curAttr
, ' ', 1]);
1489 this.lines
[row
][this.x
] = [this.curAttr
, ch
, ch_width
];
1491 this.updateRange(this.y
);
1493 // fullwidth char - set next cell width to zero and advance cursor
1495 this.lines
[row
][this.x
] = [this.curAttr
, '', 0];
1504 // ESC [ Control Sequence Introducer ( CSI is 0x9b).
1507 this.currentParam
= 0;
1511 // ESC ] Operating System Command ( OSC is 0x9d).
1514 this.currentParam
= 0;
1518 // ESC P Device Control String ( DCS is 0x90).
1521 this.currentParam
= 0;
1525 // ESC _ Application Program Command ( APC is 0x9f).
1527 this.state
= ignore
;
1530 // ESC ^ Privacy Message ( PM is 0x9e).
1532 this.state
= ignore
;
1535 // ESC c Full Reset (RIS).
1540 // ESC E Next Line ( NEL is 0x85).
1541 // ESC D Index ( IND is 0x84).
1549 // ESC M Reverse Index ( RI is 0x8d).
1551 this.reverseIndex();
1554 // ESC % Select default/utf-8 character set.
1555 // @ = default, G = utf-8
1557 //this.charset = null;
1559 this.setgCharset(0, Terminal
.charsets
.US
);
1560 this.state
= normal
;
1564 // ESC (,),*,+,-,. Designate G0-G2 Character Set.
1565 case '(': // <-- this seems to get all the attention
1591 this.state
= charset
;
1594 // Designate G3 Character Set (VT300).
1595 // A = ISO Latin-1 Supplemental.
1599 this.state
= charset
;
1604 // Single Shift Select of G2 Character Set
1605 // ( SS2 is 0x8e). This affects next character only.
1609 // Single Shift Select of G3 Character Set
1610 // ( SS3 is 0x8f). This affects next character only.
1614 // Invoke the G2 Character Set as GL (LS2).
1619 // Invoke the G3 Character Set as GL (LS3).
1624 // Invoke the G3 Character Set as GR (LS3R).
1629 // Invoke the G2 Character Set as GR (LS2R).
1634 // Invoke the G1 Character Set as GR (LS1R).
1639 // ESC 7 Save Cursor (DECSC).
1642 this.state
= normal
;
1645 // ESC 8 Restore Cursor (DECRC).
1647 this.restoreCursor();
1648 this.state
= normal
;
1651 // ESC # 3 DEC line height/width
1653 this.state
= normal
;
1657 // ESC H Tab Set (HTS is 0x88).
1662 // ESC = Application Keypad (DECKPAM).
1664 this.log('Serial port requested application keypad.');
1665 this.applicationKeypad
= true;
1666 this.viewport
.setApplicationMode(true);
1667 this.state
= normal
;
1670 // ESC > Normal Keypad (DECKPNM).
1672 this.log('Switching back to normal keypad.');
1673 this.applicationKeypad
= false;
1674 this.viewport
.setApplicationMode(false);
1675 this.state
= normal
;
1679 this.state
= normal
;
1680 this.error('Unknown ESC control: %s.', ch
);
1687 case '0': // DEC Special Character and Line Drawing Set.
1688 cs
= Terminal
.charsets
.SCLD
;
1691 cs
= Terminal
.charsets
.UK
;
1693 case 'B': // United States (USASCII).
1694 cs
= Terminal
.charsets
.US
;
1697 cs
= Terminal
.charsets
.Dutch
;
1699 case 'C': // Finnish
1701 cs
= Terminal
.charsets
.Finnish
;
1704 cs
= Terminal
.charsets
.French
;
1706 case 'Q': // FrenchCanadian
1707 cs
= Terminal
.charsets
.FrenchCanadian
;
1710 cs
= Terminal
.charsets
.German
;
1712 case 'Y': // Italian
1713 cs
= Terminal
.charsets
.Italian
;
1715 case 'E': // NorwegianDanish
1717 cs
= Terminal
.charsets
.NorwegianDanish
;
1719 case 'Z': // Spanish
1720 cs
= Terminal
.charsets
.Spanish
;
1722 case 'H': // Swedish
1724 cs
= Terminal
.charsets
.Swedish
;
1727 cs
= Terminal
.charsets
.Swiss
;
1729 case '/': // ISOLatin (actually /A)
1730 cs
= Terminal
.charsets
.ISOLatin
;
1734 cs
= Terminal
.charsets
.US
;
1737 this.setgCharset(this.gcharset
, cs
);
1738 this.gcharset
= null;
1739 this.state
= normal
;
1745 // Set Text Parameters.
1746 if (ch
=== '\x1b' || ch
=== '\x07') {
1747 if (ch
=== '\x1b') i
++;
1749 this.params
.push(this.currentParam
);
1751 switch (this.params
[0]) {
1755 if (this.params
[1]) {
1756 this.title
= this.params
[1];
1757 this.handleTitle(this.title
);
1765 // change dynamic colors
1777 // change dynamic ui colors
1789 // manipulate selection data
1807 this.currentParam
= 0;
1808 this.state
= normal
;
1810 if (!this.params
.length
) {
1811 if (ch
>= '0' && ch
<= '9') {
1813 this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1814 } else if (ch
=== ';') {
1815 this.params
.push(this.currentParam
);
1816 this.currentParam
= '';
1819 this.currentParam
+= ch
;
1826 if (ch
=== '?' || ch
=== '>' || ch
=== '!') {
1832 if (ch
>= '0' && ch
<= '9') {
1833 this.currentParam
= this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1837 // '$', '"', ' ', '\''
1838 if (ch
=== '$' || ch
=== '"' || ch
=== ' ' || ch
=== '\'') {
1843 this.params
.push(this.currentParam
);
1844 this.currentParam
= 0;
1847 if (ch
=== ';') break;
1849 this.state
= normal
;
1853 // Cursor Up Ps Times (default = 1) (CUU).
1855 this.cursorUp(this.params
);
1859 // Cursor Down Ps Times (default = 1) (CUD).
1861 this.cursorDown(this.params
);
1865 // Cursor Forward Ps Times (default = 1) (CUF).
1867 this.cursorForward(this.params
);
1871 // Cursor Backward Ps Times (default = 1) (CUB).
1873 this.cursorBackward(this.params
);
1877 // Cursor Position [row;column] (default = [1,1]) (CUP).
1879 this.cursorPos(this.params
);
1882 // CSI Ps J Erase in Display (ED).
1884 this.eraseInDisplay(this.params
);
1887 // CSI Ps K Erase in Line (EL).
1889 this.eraseInLine(this.params
);
1892 // CSI Pm m Character Attributes (SGR).
1895 this.charAttributes(this.params
);
1899 // CSI Ps n Device Status Report (DSR).
1902 this.deviceStatus(this.params
);
1911 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
1913 this.insertChars(this.params
);
1917 // Cursor Next Line Ps Times (default = 1) (CNL).
1919 this.cursorNextLine(this.params
);
1923 // Cursor Preceding Line Ps Times (default = 1) (CNL).
1925 this.cursorPrecedingLine(this.params
);
1929 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
1931 this.cursorCharAbsolute(this.params
);
1935 // Insert Ps Line(s) (default = 1) (IL).
1937 this.insertLines(this.params
);
1941 // Delete Ps Line(s) (default = 1) (DL).
1943 this.deleteLines(this.params
);
1947 // Delete Ps Character(s) (default = 1) (DCH).
1949 this.deleteChars(this.params
);
1953 // Erase Ps Character(s) (default = 1) (ECH).
1955 this.eraseChars(this.params
);
1958 // CSI Pm ` Character Position Absolute
1959 // [column] (default = [row,1]) (HPA).
1961 this.charPosAbsolute(this.params
);
1965 // Horizontal Position Relative
1967 this.HPositionRelative(this.params
);
1971 // Send Device Attributes (Primary DA).
1973 // Send Device Attributes (Secondary DA)
1975 this.sendDeviceAttributes(this.params
);
1979 // Line Position Absolute [row] (default = [1,column]) (VPA).
1981 this.linePosAbsolute(this.params
);
1984 // 145 65 e * VPR - Vertical Position Relative
1986 this.VPositionRelative(this.params
);
1990 // Horizontal and Vertical Position [row;column] (default =
1993 this.HVPosition(this.params
);
1996 // CSI Pm h Set Mode (SM).
1997 // CSI ? Pm h - mouse escape codes, cursor escape codes
1999 this.setMode(this.params
);
2002 // CSI Pm l Reset Mode (RM).
2005 this.resetMode(this.params
);
2009 // Set Scrolling Region [top;bottom] (default = full size of win-
2013 this.setScrollRegion(this.params
);
2017 // Save cursor (ANSI.SYS).
2019 this.saveCursor(this.params
);
2023 // Restore cursor (ANSI.SYS).
2025 this.restoreCursor(this.params
);
2033 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
2035 this.cursorForwardTab(this.params
);
2038 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
2040 this.scrollUp(this.params
);
2043 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
2044 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
2047 // if (this.prefix === '>') {
2048 // this.resetTitleModes(this.params);
2051 // if (this.params.length > 2) {
2052 // this.initMouseTracking(this.params);
2055 if (this.params
.length
< 2 && !this.prefix
) {
2056 this.scrollDown(this.params
);
2061 // Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
2063 this.cursorBackwardTab(this.params
);
2066 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
2068 this.repeatPrecedingCharacter(this.params
);
2071 // CSI Ps g Tab Clear (TBC).
2073 this.tabClear(this.params
);
2076 // CSI Pm i Media Copy (MC).
2079 // this.mediaCopy(this.params);
2082 // CSI Pm m Character Attributes (SGR).
2084 // case 'm': // duplicate
2085 // if (this.prefix === '>') {
2086 // this.setResources(this.params);
2088 // this.charAttributes(this.params);
2092 // CSI Ps n Device Status Report (DSR).
2094 // case 'n': // duplicate
2095 // if (this.prefix === '>') {
2096 // this.disableModifiers(this.params);
2098 // this.deviceStatus(this.params);
2102 // CSI > Ps p Set pointer mode.
2103 // CSI ! p Soft terminal reset (DECSTR).
2105 // Request ANSI mode (DECRQM).
2107 // Request DEC private mode (DECRQM).
2110 switch (this.prefix
) {
2112 // this.setPointerMode(this.params);
2115 this.softReset(this.params
);
2118 // if (this.postfix === '$') {
2119 // this.requestPrivateMode(this.params);
2123 // if (this.postfix === '"') {
2124 // this.setConformanceLevel(this.params);
2125 // } else if (this.postfix === '$') {
2126 // this.requestAnsiMode(this.params);
2132 // CSI Ps q Load LEDs (DECLL).
2136 // if (this.postfix === ' ') {
2137 // this.setCursorStyle(this.params);
2140 // if (this.postfix === '"') {
2141 // this.setCharProtectionAttr(this.params);
2144 // this.loadLEDs(this.params);
2148 // Set Scrolling Region [top;bottom] (default = full size of win-
2151 // CSI Pt; Pl; Pb; Pr; Ps$ r
2152 // case 'r': // duplicate
2153 // if (this.prefix === '?') {
2154 // this.restorePrivateValues(this.params);
2155 // } else if (this.postfix === '$') {
2156 // this.setAttrInRectangle(this.params);
2158 // this.setScrollRegion(this.params);
2162 // CSI s Save cursor (ANSI.SYS).
2164 // case 's': // duplicate
2165 // if (this.prefix === '?') {
2166 // this.savePrivateValues(this.params);
2168 // this.saveCursor(this.params);
2172 // CSI Ps ; Ps ; Ps t
2173 // CSI Pt; Pl; Pb; Pr; Ps$ t
2177 // if (this.postfix === '$') {
2178 // this.reverseAttrInRectangle(this.params);
2179 // } else if (this.postfix === ' ') {
2180 // this.setWarningBellVolume(this.params);
2182 // if (this.prefix === '>') {
2183 // this.setTitleModeFeature(this.params);
2185 // this.manipulateWindow(this.params);
2190 // CSI u Restore cursor (ANSI.SYS).
2192 // case 'u': // duplicate
2193 // if (this.postfix === ' ') {
2194 // this.setMarginBellVolume(this.params);
2196 // this.restoreCursor(this.params);
2200 // CSI Pt; Pl; Pb; Pr; Pp; Pt; Pl; Pp$ v
2202 // if (this.postfix === '$') {
2203 // this.copyRectagle(this.params);
2207 // CSI Pt ; Pl ; Pb ; Pr ' w
2209 // if (this.postfix === '\'') {
2210 // this.enableFilterRectangle(this.params);
2214 // CSI Ps x Request Terminal Parameters (DECREQTPARM).
2215 // CSI Ps x Select Attribute Change Extent (DECSACE).
2216 // CSI Pc; Pt; Pl; Pb; Pr$ x
2218 // if (this.postfix === '$') {
2219 // this.fillRectangle(this.params);
2221 // this.requestParameters(this.params);
2222 // //this.__(this.params);
2227 // CSI Pt; Pl; Pb; Pr$ z
2229 // if (this.postfix === '\'') {
2230 // this.enableLocatorReporting(this.params);
2231 // } else if (this.postfix === '$') {
2232 // this.eraseRectangle(this.params);
2237 // CSI Pt; Pl; Pb; Pr$ {
2239 // if (this.postfix === '\'') {
2240 // this.setLocatorEvents(this.params);
2241 // } else if (this.postfix === '$') {
2242 // this.selectiveEraseRectangle(this.params);
2248 // if (this.postfix === '\'') {
2249 // this.requestLocatorPosition(this.params);
2254 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
2256 // if (this.postfix === ' ') {
2257 // this.insertColumns(this.params);
2262 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
2264 // if (this.postfix === ' ') {
2265 // this.deleteColumns(this.params);
2270 this.error('Unknown CSI code: %s.', ch
);
2279 if (ch
=== '\x1b' || ch
=== '\x07') {
2280 if (ch
=== '\x1b') i
++;
2282 switch (this.prefix
) {
2283 // User-Defined Keys (DECUDK).
2287 // Request Status String (DECRQSS).
2288 // test: echo -e '\eP$q"p\e\\'
2290 var pt
= this.currentParam
2307 + (this.scrollTop
+ 1)
2309 + (this.scrollBottom
+ 1)
2319 this.error('Unknown DCS Pt: %s.', pt
);
2324 this.send('\x1bP' + +valid
+ '$r' + pt
+ '\x1b\\');
2327 // Set Termcap/Terminfo Data (xterm, experimental).
2331 // Request Termcap/Terminfo String (xterm, experimental)
2332 // Regular xterm does not even respond to this sequence.
2333 // This can cause a small glitch in vim.
2334 // test: echo -ne '\eP+q6b64\e\\'
2336 var pt
= this.currentParam
2339 this.send('\x1bP' + +valid
+ '+r' + pt
+ '\x1b\\');
2343 this.error('Unknown DCS prefix: %s.', this.prefix
);
2347 this.currentParam
= 0;
2349 this.state
= normal
;
2350 } else if (!this.currentParam
) {
2351 if (!this.prefix
&& ch
!== '$' && ch
!== '+') {
2352 this.currentParam
= ch
;
2353 } else if (this.prefix
.length
=== 2) {
2354 this.currentParam
= ch
;
2359 this.currentParam
+= ch
;
2365 if (ch
=== '\x1b' || ch
=== '\x07') {
2366 if (ch
=== '\x1b') i
++;
2367 this.state
= normal
;
2373 this.updateRange(this.y
);
2374 this.refresh(this.refreshStart
, this.refreshEnd
);
2378 * Writes text to the terminal, followed by a break line character (\n).
2379 * @param {string} text The text to write to the terminal.
2381 Terminal
.prototype.writeln = function(data
) {
2382 this.write(data
+ '\r\n');
2386 * Attaches a custom keydown handler which is run before keys are processed, giving consumers of
2387 * xterm.js ultimate control as to what keys should be processed by the terminal and what keys
2389 * @param {function} customKeydownHandler The custom KeyboardEvent handler to attach. This is a
2390 * function that takes a KeyboardEvent, allowing consumers to stop propogation and/or prevent
2391 * the default action. The function returns whether the event should be processed by xterm.js.
2393 Terminal
.prototype.attachCustomKeydownHandler = function(customKeydownHandler
) {
2394 this.customKeydownHandler
= customKeydownHandler
;
2398 * Handle a keydown event
2400 * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
2401 * @param {KeyboardEvent} ev The keydown event to be handled.
2403 Terminal
.prototype.keyDown = function(ev
) {
2404 if (this.customKeydownHandler
&& this.customKeydownHandler(ev
) === false) {
2408 if (!this.compositionHelper
.keydown
.bind(this.compositionHelper
)(ev
)) {
2413 var result
= this.evaluateKeyEscapeSequence(ev
);
2415 if (result
.scrollDisp
) {
2416 this.scrollDisp(result
.scrollDisp
);
2417 return this.cancel(ev
);
2420 if (isThirdLevelShift(this, ev
)) {
2424 if (result
.cancel
) {
2425 // The event is canceled at the end already, is this necessary?
2426 this.cancel(ev
, true);
2433 this.emit('keydown', ev
);
2434 this.emit('key', result
.key
, ev
);
2436 this.handler(result
.key
);
2438 return this.cancel(ev
, true);
2442 * Returns an object that determines how a KeyboardEvent should be handled. The key of the
2443 * returned value is the new key code to pass to the PTY.
2445 * Reference: http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
2446 * @param {KeyboardEvent} ev The keyboard event to be translated to key escape sequence.
2448 Terminal
.prototype.evaluateKeyEscapeSequence = function(ev
) {
2450 // Whether to cancel event propogation (NOTE: this may not be needed since the event is
2451 // canceled at the end of keyDown
2453 // The new key even to emit
2455 // The number of characters to scroll, if this is defined it will cancel the event
2456 scrollDisp
: undefined
2458 var modifiers
= ev
.shiftKey
<< 0 | ev
.altKey
<< 1 | ev
.ctrlKey
<< 2 | ev
.metaKey
<< 3;
2459 switch (ev
.keyCode
) {
2463 result
.key
= '\x08'; // ^H
2466 result
.key
= '\x7f'; // ^?
2471 result
.key
= '\x1b[Z';
2475 result
.cancel
= true;
2480 result
.cancel
= true;
2484 result
.key
= '\x1b';
2485 result
.cancel
= true;
2490 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'D';
2491 // HACK: Make Alt + left-arrow behave like Ctrl + left-arrow: move one word backwards
2492 // http://unix.stackexchange.com/a/108106
2493 if (result
.key
== '\x1b[1;3D') {
2494 result
.key
= '\x1b[1;5D';
2496 } else if (this.applicationCursor
) {
2497 result
.key
= '\x1bOD';
2499 result
.key
= '\x1b[D';
2505 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'C';
2506 // HACK: Make Alt + right-arrow behave like Ctrl + right-arrow: move one word forward
2507 // http://unix.stackexchange.com/a/108106
2508 if (result
.key
== '\x1b[1;3C') {
2509 result
.key
= '\x1b[1;5C';
2511 } else if (this.applicationCursor
) {
2512 result
.key
= '\x1bOC';
2514 result
.key
= '\x1b[C';
2520 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'A';
2521 // HACK: Make Alt + up-arrow behave like Ctrl + up-arrow
2522 // http://unix.stackexchange.com/a/108106
2523 if (result
.key
== '\x1b[1;3A') {
2524 result
.key
= '\x1b[1;5A';
2526 } else if (this.applicationCursor
) {
2527 result
.key
= '\x1bOA';
2529 result
.key
= '\x1b[A';
2535 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'B';
2536 // HACK: Make Alt + down-arrow behave like Ctrl + down-arrow
2537 // http://unix.stackexchange.com/a/108106
2538 if (result
.key
== '\x1b[1;3B') {
2539 result
.key
= '\x1b[1;5B';
2541 } else if (this.applicationCursor
) {
2542 result
.key
= '\x1bOB';
2544 result
.key
= '\x1b[B';
2549 if (!ev
.shiftKey
&& !ev
.ctrlKey
) {
2550 // <Ctrl> or <Shift> + <Insert> are used to
2551 // copy-paste on some systems.
2552 result
.key
= '\x1b[2~';
2558 result
.key
= '\x1b[3;' + (modifiers
+ 1) + '~';
2560 result
.key
= '\x1b[3~';
2566 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'H';
2567 else if (this.applicationCursor
)
2568 result
.key
= '\x1bOH';
2570 result
.key
= '\x1b[H';
2575 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'F';
2576 else if (this.applicationCursor
)
2577 result
.key
= '\x1bOF';
2579 result
.key
= '\x1b[F';
2584 result
.scrollDisp
= -(this.rows
- 1);
2586 result
.key
= '\x1b[5~';
2592 result
.scrollDisp
= this.rows
- 1;
2594 result
.key
= '\x1b[6~';
2600 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'P';
2602 result
.key
= '\x1bOP';
2607 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'Q';
2609 result
.key
= '\x1bOQ';
2614 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'R';
2616 result
.key
= '\x1bOR';
2621 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'S';
2623 result
.key
= '\x1bOS';
2628 result
.key
= '\x1b[15;' + (modifiers
+ 1) + '~';
2630 result
.key
= '\x1b[15~';
2635 result
.key
= '\x1b[17;' + (modifiers
+ 1) + '~';
2637 result
.key
= '\x1b[17~';
2642 result
.key
= '\x1b[18;' + (modifiers
+ 1) + '~';
2644 result
.key
= '\x1b[18~';
2649 result
.key
= '\x1b[19;' + (modifiers
+ 1) + '~';
2651 result
.key
= '\x1b[19~';
2656 result
.key
= '\x1b[20;' + (modifiers
+ 1) + '~';
2658 result
.key
= '\x1b[20~';
2663 result
.key
= '\x1b[21;' + (modifiers
+ 1) + '~';
2665 result
.key
= '\x1b[21~';
2670 result
.key
= '\x1b[23;' + (modifiers
+ 1) + '~';
2672 result
.key
= '\x1b[23~';
2677 result
.key
= '\x1b[24;' + (modifiers
+ 1) + '~';
2679 result
.key
= '\x1b[24~';
2684 if (ev
.ctrlKey
&& !ev
.shiftKey
&& !ev
.altKey
&& !ev
.metaKey
) {
2685 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2686 result
.key
= String
.fromCharCode(ev
.keyCode
- 64);
2687 } else if (ev
.keyCode
=== 32) {
2689 result
.key
= String
.fromCharCode(0);
2690 } else if (ev
.keyCode
>= 51 && ev
.keyCode
<= 55) {
2691 // escape, file sep, group sep, record sep, unit sep
2692 result
.key
= String
.fromCharCode(ev
.keyCode
- 51 + 27);
2693 } else if (ev
.keyCode
=== 56) {
2695 result
.key
= String
.fromCharCode(127);
2696 } else if (ev
.keyCode
=== 219) {
2698 result
.key
= String
.fromCharCode(27);
2699 } else if (ev
.keyCode
=== 221) {
2701 result
.key
= String
.fromCharCode(29);
2703 } else if (!this.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) {
2704 // On Mac this is a third level shift. Use <Esc> instead.
2705 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2706 result
.key
= '\x1b' + String
.fromCharCode(ev
.keyCode
+ 32);
2707 } else if (ev
.keyCode
=== 192) {
2708 result
.key
= '\x1b`';
2709 } else if (ev
.keyCode
>= 48 && ev
.keyCode
<= 57) {
2710 result
.key
= '\x1b' + (ev
.keyCode
- 48);
2719 * Set the G level of the terminal
2722 Terminal
.prototype.setgLevel = function(g
) {
2724 this.charset
= this.charsets
[g
];
2728 * Set the charset for the given G level of the terminal
2732 Terminal
.prototype.setgCharset = function(g
, charset
) {
2733 this.charsets
[g
] = charset
;
2734 if (this.glevel
=== g
) {
2735 this.charset
= charset
;
2740 * Handle a keypress event.
2742 * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
2743 * @param {KeyboardEvent} ev The keypress event to be handled.
2745 Terminal
.prototype.keyPress = function(ev
) {
2752 } else if (ev
.which
== null) {
2754 } else if (ev
.which
!== 0 && ev
.charCode
!== 0) {
2761 (ev
.altKey
|| ev
.ctrlKey
|| ev
.metaKey
) && !isThirdLevelShift(this, ev
)
2766 key
= String
.fromCharCode(key
);
2768 this.emit('keypress', key
, ev
);
2769 this.emit('key', key
, ev
);
2777 * Send data for handling to the terminal
2778 * @param {string} data
2780 Terminal
.prototype.send = function(data
) {
2784 setTimeout(function() {
2785 self
.handler(self
.queue
);
2795 * Note: We could do sweet things with webaudio here
2797 Terminal
.prototype.bell = function() {
2798 if (!this.visualBell
) return;
2800 this.element
.style
.borderColor
= 'white';
2801 setTimeout(function() {
2802 self
.element
.style
.borderColor
= '';
2804 if (this.popOnBell
) this.focus();
2808 * Log the current state to the console.
2810 Terminal
.prototype.log = function() {
2811 if (!this.debug
) return;
2812 if (!this.context
.console
|| !this.context
.console
.log
) return;
2813 var args
= Array
.prototype.slice
.call(arguments
);
2814 this.context
.console
.log
.apply(this.context
.console
, args
);
2818 * Log the current state as error to the console.
2820 Terminal
.prototype.error = function() {
2821 if (!this.debug
) return;
2822 if (!this.context
.console
|| !this.context
.console
.error
) return;
2823 var args
= Array
.prototype.slice
.call(arguments
);
2824 this.context
.console
.error
.apply(this.context
.console
, args
);
2828 * Resizes the terminal.
2830 * @param {number} x The number of columns to resize to.
2831 * @param {number} y The number of rows to resize to.
2833 Terminal
.prototype.resize = function(x
, y
) {
2841 if (x
=== this.cols
&& y
=== this.rows
) {
2851 ch
= [this.defAttr
, ' ', 1]; // does xterm use the default attr?
2852 i
= this.lines
.length
;
2854 while (this.lines
[i
].length
< x
) {
2855 this.lines
[i
].push(ch
);
2859 i
= this.lines
.length
;
2861 while (this.lines
[i
].length
> x
) {
2862 this.lines
[i
].pop();
2875 // y is rows, not this.y
2876 if (this.lines
.length
< y
+ this.ybase
) {
2877 if (this.ybase
> 0 && this.lines
.length
<= this.ybase
+ this.y
+ addToY
+ 1) {
2878 // There is room above the buffer and there are no empty elements below the line,
2882 if (this.ydisp
> 0) {
2883 // Viewport is at the top of the buffer, must increase downwards
2887 // Add a blank line if there is no buffer left at the top to scroll to, or if there
2888 // are blank lines after the cursor
2889 this.lines
.push(this.blankLine());
2892 if (this.children
.length
< y
) {
2898 if (this.lines
.length
> y
+ this.ybase
) {
2899 if (this.lines
.length
> this.ybase
+ this.y
+ 1) {
2900 // The line is a blank line below the cursor, remove it
2903 // The line is the cursor, scroll down
2908 if (this.children
.length
> y
) {
2909 el
= this.children
.shift();
2911 el
.parentNode
.removeChild(el
);
2917 // Make sure that the cursor stays on screen
2930 this.scrollBottom
= y
- 1;
2932 this.refresh(0, this.rows
- 1);
2936 this.emit('resize', {terminal
: this, cols
: x
, rows
: y
});
2940 * Updates the range of rows to refresh
2941 * @param {number} y The number of rows to refresh next.
2943 Terminal
.prototype.updateRange = function(y
) {
2944 if (y
< this.refreshStart
) this.refreshStart
= y
;
2945 if (y
> this.refreshEnd
) this.refreshEnd
= y
;
2946 // if (y > this.refreshEnd) {
2947 // this.refreshEnd = y;
2948 // if (y > this.rows - 1) {
2949 // this.refreshEnd = this.rows - 1;
2955 * Set the range of refreshing to the maximyum value
2957 Terminal
.prototype.maxRange = function() {
2958 this.refreshStart
= 0;
2959 this.refreshEnd
= this.rows
- 1;
2965 * Setup the tab stops.
2968 Terminal
.prototype.setupStops = function(i
) {
2970 if (!this.tabs
[i
]) {
2971 i
= this.prevStop(i
);
2978 for (; i
< this.cols
; i
+= 8) {
2979 this.tabs
[i
] = true;
2985 * Move the cursor to the previous tab stop from the given position (default is current).
2986 * @param {number} x The position to move the cursor to the previous tab stop.
2988 Terminal
.prototype.prevStop = function(x
) {
2989 if (x
== null) x
= this.x
;
2990 while (!this.tabs
[--x
] && x
> 0);
2991 return x
>= this.cols
2998 * Move the cursor one tab stop forward from the given position (default is current).
2999 * @param {number} x The position to move the cursor one tab stop forward.
3001 Terminal
.prototype.nextStop = function(x
) {
3002 if (x
== null) x
= this.x
;
3003 while (!this.tabs
[++x
] && x
< this.cols
);
3004 return x
>= this.cols
3011 * Erase in the identified line everything from "x" to the end of the line (right).
3012 * @param {number} x The column from which to start erasing to the end of the line.
3013 * @param {number} y The line in which to operate.
3015 Terminal
.prototype.eraseRight = function(x
, y
) {
3016 var line
= this.lines
[this.ybase
+ y
]
3017 , ch
= [this.eraseAttr(), ' ', 1]; // xterm
3020 for (; x
< this.cols
; x
++) {
3024 this.updateRange(y
);
3030 * Erase in the identified line everything from "x" to the start of the line (left).
3031 * @param {number} x The column from which to start erasing to the start of the line.
3032 * @param {number} y The line in which to operate.
3034 Terminal
.prototype.eraseLeft = function(x
, y
) {
3035 var line
= this.lines
[this.ybase
+ y
]
3036 , ch
= [this.eraseAttr(), ' ', 1]; // xterm
3039 while (x
--) line
[x
] = ch
;
3041 this.updateRange(y
);
3046 * Erase all content in the given line
3047 * @param {number} y The line to erase all of its contents.
3049 Terminal
.prototype.eraseLine = function(y
) {
3050 this.eraseRight(0, y
);
3055 * Return the data array of a blank line/
3056 * @param {number} cur First bunch of data for each "blank" character.
3058 Terminal
.prototype.blankLine = function(cur
) {
3063 var ch
= [attr
, ' ', 1] // width defaults to 1 halfwidth character
3067 for (; i
< this.cols
; i
++) {
3076 * If cur return the back color xterm feature attribute. Else return defAttr.
3077 * @param {object} cur
3079 Terminal
.prototype.ch = function(cur
) {
3081 ? [this.eraseAttr(), ' ', 1]
3082 : [this.defAttr
, ' ', 1];
3087 * Evaluate if the current erminal is the given argument.
3088 * @param {object} term The terminal to evaluate
3090 Terminal
.prototype.is = function(term
) {
3091 var name
= this.termName
;
3092 return (name
+ '').indexOf(term
) === 0;
3097 * Emit the 'data' event and populate the given data.
3098 * @param {string} data The data to populate in the event.
3100 Terminal
.prototype.handler = function(data
) {
3101 this.emit('data', data
);
3106 * Emit the 'title' event and populate the given title.
3107 * @param {string} title The title to populate in the event.
3109 Terminal
.prototype.handleTitle = function(title
) {
3110 this.emit('title', title
);
3119 * ESC D Index (IND is 0x84).
3121 Terminal
.prototype.index = function() {
3123 if (this.y
> this.scrollBottom
) {
3127 this.state
= normal
;
3132 * 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
;
3154 * ESC c Full Reset (RIS).
3156 Terminal
.prototype.reset = function() {
3157 this.options
.rows
= this.rows
;
3158 this.options
.cols
= this.cols
;
3159 var customKeydownHandler
= this.customKeydownHandler
;
3160 Terminal
.call(this, this.options
);
3161 this.customKeydownHandler
= customKeydownHandler
;
3162 this.refresh(0, this.rows
- 1);
3167 * ESC H Tab Set (HTS is 0x88).
3169 Terminal
.prototype.tabSet = function() {
3170 this.tabs
[this.x
] = true;
3171 this.state
= normal
;
3181 * Cursor Up Ps Times (default = 1) (CUU).
3183 Terminal
.prototype.cursorUp = function(params
) {
3184 var param
= params
[0];
3185 if (param
< 1) param
= 1;
3187 if (this.y
< 0) this.y
= 0;
3193 * Cursor Down Ps Times (default = 1) (CUD).
3195 Terminal
.prototype.cursorDown = function(params
) {
3196 var param
= params
[0];
3197 if (param
< 1) param
= 1;
3199 if (this.y
>= this.rows
) {
3200 this.y
= this.rows
- 1;
3207 * Cursor Forward Ps Times (default = 1) (CUF).
3209 Terminal
.prototype.cursorForward = function(params
) {
3210 var param
= params
[0];
3211 if (param
< 1) param
= 1;
3213 if (this.x
>= this.cols
) {
3214 this.x
= this.cols
- 1;
3221 * Cursor Backward Ps Times (default = 1) (CUB).
3223 Terminal
.prototype.cursorBackward = function(params
) {
3224 var param
= params
[0];
3225 if (param
< 1) param
= 1;
3227 if (this.x
< 0) this.x
= 0;
3233 * Cursor Position [row;column] (default = [1,1]) (CUP).
3235 Terminal
.prototype.cursorPos = function(params
) {
3238 row
= params
[0] - 1;
3240 if (params
.length
>= 2) {
3241 col
= params
[1] - 1;
3248 } else if (row
>= this.rows
) {
3249 row
= this.rows
- 1;
3254 } else if (col
>= this.cols
) {
3255 col
= this.cols
- 1;
3264 * CSI Ps J Erase in Display (ED).
3265 * Ps = 0 -> Erase Below (default).
3266 * Ps = 1 -> Erase Above.
3267 * Ps = 2 -> Erase All.
3268 * Ps = 3 -> Erase Saved Lines (xterm).
3270 * Erase in Display (DECSED).
3271 * Ps = 0 -> Selective Erase Below (default).
3272 * Ps = 1 -> Selective Erase Above.
3273 * Ps = 2 -> Selective Erase All.
3275 Terminal
.prototype.eraseInDisplay = function(params
) {
3277 switch (params
[0]) {
3279 this.eraseRight(this.x
, this.y
);
3281 for (; j
< this.rows
; j
++) {
3286 this.eraseLeft(this.x
, this.y
);
3294 while (j
--) this.eraseLine(j
);
3304 * CSI Ps K Erase in Line (EL).
3305 * Ps = 0 -> Erase to Right (default).
3306 * Ps = 1 -> Erase to Left.
3307 * Ps = 2 -> Erase All.
3309 * Erase in Line (DECSEL).
3310 * Ps = 0 -> Selective Erase to Right (default).
3311 * Ps = 1 -> Selective Erase to Left.
3312 * Ps = 2 -> Selective Erase All.
3314 Terminal
.prototype.eraseInLine = function(params
) {
3315 switch (params
[0]) {
3317 this.eraseRight(this.x
, this.y
);
3320 this.eraseLeft(this.x
, this.y
);
3323 this.eraseLine(this.y
);
3330 * CSI Pm m Character Attributes (SGR).
3331 * Ps = 0 -> Normal (default).
3333 * Ps = 4 -> Underlined.
3334 * Ps = 5 -> Blink (appears as Bold).
3335 * Ps = 7 -> Inverse.
3336 * Ps = 8 -> Invisible, i.e., hidden (VT300).
3337 * Ps = 2 2 -> Normal (neither bold nor faint).
3338 * Ps = 2 4 -> Not underlined.
3339 * Ps = 2 5 -> Steady (not blinking).
3340 * Ps = 2 7 -> Positive (not inverse).
3341 * Ps = 2 8 -> Visible, i.e., not hidden (VT300).
3342 * Ps = 3 0 -> Set foreground color to Black.
3343 * Ps = 3 1 -> Set foreground color to Red.
3344 * Ps = 3 2 -> Set foreground color to Green.
3345 * Ps = 3 3 -> Set foreground color to Yellow.
3346 * Ps = 3 4 -> Set foreground color to Blue.
3347 * Ps = 3 5 -> Set foreground color to Magenta.
3348 * Ps = 3 6 -> Set foreground color to Cyan.
3349 * Ps = 3 7 -> Set foreground color to White.
3350 * Ps = 3 9 -> Set foreground color to default (original).
3351 * Ps = 4 0 -> Set background color to Black.
3352 * Ps = 4 1 -> Set background color to Red.
3353 * Ps = 4 2 -> Set background color to Green.
3354 * Ps = 4 3 -> Set background color to Yellow.
3355 * Ps = 4 4 -> Set background color to Blue.
3356 * Ps = 4 5 -> Set background color to Magenta.
3357 * Ps = 4 6 -> Set background color to Cyan.
3358 * Ps = 4 7 -> Set background color to White.
3359 * Ps = 4 9 -> Set background color to default (original).
3361 * If 16-color support is compiled, the following apply. Assume
3362 * that xterm's resources are set so that the ISO color codes are
3363 * the first 8 of a set of 16. Then the aixterm colors are the
3364 * bright versions of the ISO colors:
3365 * Ps = 9 0 -> Set foreground color to Black.
3366 * Ps = 9 1 -> Set foreground color to Red.
3367 * Ps = 9 2 -> Set foreground color to Green.
3368 * Ps = 9 3 -> Set foreground color to Yellow.
3369 * Ps = 9 4 -> Set foreground color to Blue.
3370 * Ps = 9 5 -> Set foreground color to Magenta.
3371 * Ps = 9 6 -> Set foreground color to Cyan.
3372 * Ps = 9 7 -> Set foreground color to White.
3373 * Ps = 1 0 0 -> Set background color to Black.
3374 * Ps = 1 0 1 -> Set background color to Red.
3375 * Ps = 1 0 2 -> Set background color to Green.
3376 * Ps = 1 0 3 -> Set background color to Yellow.
3377 * Ps = 1 0 4 -> Set background color to Blue.
3378 * Ps = 1 0 5 -> Set background color to Magenta.
3379 * Ps = 1 0 6 -> Set background color to Cyan.
3380 * Ps = 1 0 7 -> Set background color to White.
3382 * If xterm is compiled with the 16-color support disabled, it
3383 * supports the following, from rxvt:
3384 * Ps = 1 0 0 -> Set foreground and background color to
3387 * If 88- or 256-color support is compiled, the following apply.
3388 * Ps = 3 8 ; 5 ; Ps -> Set foreground color to the second
3390 * Ps = 4 8 ; 5 ; Ps -> Set background color to the second
3393 Terminal
.prototype.charAttributes = function(params
) {
3394 // Optimize a single SGR0.
3395 if (params
.length
=== 1 && params
[0] === 0) {
3396 this.curAttr
= this.defAttr
;
3400 var l
= params
.length
3402 , flags
= this.curAttr
>> 18
3403 , fg
= (this.curAttr
>> 9) & 0x1ff
3404 , bg
= this.curAttr
& 0x1ff
3407 for (; i
< l
; i
++) {
3409 if (p
>= 30 && p
<= 37) {
3412 } else if (p
>= 40 && p
<= 47) {
3415 } else if (p
>= 90 && p
<= 97) {
3419 } else if (p
>= 100 && p
<= 107) {
3423 } else if (p
=== 0) {
3425 flags
= this.defAttr
>> 18;
3426 fg
= (this.defAttr
>> 9) & 0x1ff;
3427 bg
= this.defAttr
& 0x1ff;
3431 } else if (p
=== 1) {
3434 } else if (p
=== 4) {
3437 } else if (p
=== 5) {
3440 } else if (p
=== 7) {
3441 // inverse and positive
3442 // test with: echo -e '\e[31m\e[42mhello\e[7mworld\e[27mhi\e[m'
3444 } else if (p
=== 8) {
3447 } else if (p
=== 22) {
3450 } else if (p
=== 24) {
3453 } else if (p
=== 25) {
3456 } else if (p
=== 27) {
3459 } else if (p
=== 28) {
3462 } else if (p
=== 39) {
3464 fg
= (this.defAttr
>> 9) & 0x1ff;
3465 } else if (p
=== 49) {
3467 bg
= this.defAttr
& 0x1ff;
3468 } else if (p
=== 38) {
3470 if (params
[i
+ 1] === 2) {
3474 params
[i
+ 1] & 0xff,
3475 params
[i
+ 2] & 0xff);
3476 if (fg
=== -1) fg
= 0x1ff;
3478 } else if (params
[i
+ 1] === 5) {
3480 p
= params
[i
] & 0xff;
3483 } else if (p
=== 48) {
3485 if (params
[i
+ 1] === 2) {
3489 params
[i
+ 1] & 0xff,
3490 params
[i
+ 2] & 0xff);
3491 if (bg
=== -1) bg
= 0x1ff;
3493 } else if (params
[i
+ 1] === 5) {
3495 p
= params
[i
] & 0xff;
3498 } else if (p
=== 100) {
3500 fg
= (this.defAttr
>> 9) & 0x1ff;
3501 bg
= this.defAttr
& 0x1ff;
3503 this.error('Unknown SGR attribute: %d.', p
);
3507 this.curAttr
= (flags
<< 18) | (fg
<< 9) | bg
;
3512 * CSI Ps n Device Status Report (DSR).
3513 * Ps = 5 -> Status Report. Result (``OK'') is
3515 * Ps = 6 -> Report Cursor Position (CPR) [row;column].
3519 * Device Status Report (DSR, DEC-specific).
3520 * Ps = 6 -> Report Cursor Position (CPR) [row;column] as CSI
3521 * ? r ; c R (assumes page is zero).
3522 * Ps = 1 5 -> Report Printer status as CSI ? 1 0 n (ready).
3523 * or CSI ? 1 1 n (not ready).
3524 * Ps = 2 5 -> Report UDK status as CSI ? 2 0 n (unlocked)
3525 * or CSI ? 2 1 n (locked).
3526 * Ps = 2 6 -> Report Keyboard status as
3527 * CSI ? 2 7 ; 1 ; 0 ; 0 n (North American).
3528 * The last two parameters apply to VT400 & up, and denote key-
3529 * board ready and LK01 respectively.
3530 * Ps = 5 3 -> Report Locator status as
3531 * CSI ? 5 3 n Locator available, if compiled-in, or
3532 * CSI ? 5 0 n No Locator, if not.
3534 Terminal
.prototype.deviceStatus = function(params
) {
3536 switch (params
[0]) {
3539 this.send('\x1b[0n');
3550 } else if (this.prefix
=== '?') {
3551 // modern xterm doesnt seem to
3552 // respond to any of these except ?6, 6, and 5
3553 switch (params
[0]) {
3564 // this.send('\x1b[?11n');
3567 // dont support user defined keys
3568 // this.send('\x1b[?21n');
3571 // north american keyboard
3572 // this.send('\x1b[?27;1;0;0n');
3575 // no dec locator/mouse
3576 // this.send('\x1b[?50n');
3589 * Insert Ps (Blank) Character(s) (default = 1) (ICH).
3591 Terminal
.prototype.insertChars = function(params
) {
3592 var param
, row
, j
, ch
;
3595 if (param
< 1) param
= 1;
3597 row
= this.y
+ this.ybase
;
3599 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3601 while (param
-- && j
< this.cols
) {
3602 this.lines
[row
].splice(j
++, 0, ch
);
3603 this.lines
[row
].pop();
3609 * Cursor Next Line Ps Times (default = 1) (CNL).
3610 * same as CSI Ps B ?
3612 Terminal
.prototype.cursorNextLine = function(params
) {
3613 var param
= params
[0];
3614 if (param
< 1) param
= 1;
3616 if (this.y
>= this.rows
) {
3617 this.y
= this.rows
- 1;
3625 * Cursor Preceding Line Ps Times (default = 1) (CNL).
3628 Terminal
.prototype.cursorPrecedingLine = function(params
) {
3629 var param
= params
[0];
3630 if (param
< 1) param
= 1;
3632 if (this.y
< 0) this.y
= 0;
3639 * Cursor Character Absolute [column] (default = [row,1]) (CHA).
3641 Terminal
.prototype.cursorCharAbsolute = function(params
) {
3642 var param
= params
[0];
3643 if (param
< 1) param
= 1;
3650 * Insert Ps Line(s) (default = 1) (IL).
3652 Terminal
.prototype.insertLines = function(params
) {
3656 if (param
< 1) param
= 1;
3657 row
= this.y
+ this.ybase
;
3659 j
= this.rows
- 1 - this.scrollBottom
;
3660 j
= this.rows
- 1 + this.ybase
- j
+ 1;
3663 // test: echo -e '\e[44m\e[1L\e[0m'
3664 // blankLine(true) - xterm/linux behavior
3665 this.lines
.splice(row
, 0, this.blankLine(true));
3666 this.lines
.splice(j
, 1);
3670 this.updateRange(this.y
);
3671 this.updateRange(this.scrollBottom
);
3677 * Delete Ps Line(s) (default = 1) (DL).
3679 Terminal
.prototype.deleteLines = function(params
) {
3683 if (param
< 1) param
= 1;
3684 row
= this.y
+ this.ybase
;
3686 j
= this.rows
- 1 - this.scrollBottom
;
3687 j
= this.rows
- 1 + this.ybase
- j
;
3690 // test: echo -e '\e[44m\e[1M\e[0m'
3691 // blankLine(true) - xterm/linux behavior
3692 this.lines
.splice(j
+ 1, 0, this.blankLine(true));
3693 this.lines
.splice(row
, 1);
3697 this.updateRange(this.y
);
3698 this.updateRange(this.scrollBottom
);
3704 * Delete Ps Character(s) (default = 1) (DCH).
3706 Terminal
.prototype.deleteChars = function(params
) {
3710 if (param
< 1) param
= 1;
3712 row
= this.y
+ this.ybase
;
3713 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3716 this.lines
[row
].splice(this.x
, 1);
3717 this.lines
[row
].push(ch
);
3723 * Erase Ps Character(s) (default = 1) (ECH).
3725 Terminal
.prototype.eraseChars = function(params
) {
3726 var param
, row
, j
, ch
;
3729 if (param
< 1) param
= 1;
3731 row
= this.y
+ this.ybase
;
3733 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3735 while (param
-- && j
< this.cols
) {
3736 this.lines
[row
][j
++] = ch
;
3741 * CSI Pm ` Character Position Absolute
3742 * [column] (default = [row,1]) (HPA).
3744 Terminal
.prototype.charPosAbsolute = function(params
) {
3745 var param
= params
[0];
3746 if (param
< 1) param
= 1;
3748 if (this.x
>= this.cols
) {
3749 this.x
= this.cols
- 1;
3756 * Horizontal Position Relative
3759 Terminal
.prototype.HPositionRelative = function(params
) {
3760 var param
= params
[0];
3761 if (param
< 1) param
= 1;
3763 if (this.x
>= this.cols
) {
3764 this.x
= this.cols
- 1;
3770 * CSI Ps c Send Device Attributes (Primary DA).
3771 * Ps = 0 or omitted -> request attributes from terminal. The
3772 * response depends on the decTerminalID resource setting.
3773 * -> CSI ? 1 ; 2 c (``VT100 with Advanced Video Option'')
3774 * -> CSI ? 1 ; 0 c (``VT101 with No Options'')
3775 * -> CSI ? 6 c (``VT102'')
3776 * -> CSI ? 6 0 ; 1 ; 2 ; 6 ; 8 ; 9 ; 1 5 ; c (``VT220'')
3777 * The VT100-style response parameters do not mean anything by
3778 * themselves. VT220 parameters do, telling the host what fea-
3779 * tures the terminal supports:
3780 * Ps = 1 -> 132-columns.
3781 * Ps = 2 -> Printer.
3782 * Ps = 6 -> Selective erase.
3783 * Ps = 8 -> User-defined keys.
3784 * Ps = 9 -> National replacement character sets.
3785 * Ps = 1 5 -> Technical characters.
3786 * Ps = 2 2 -> ANSI color, e.g., VT525.
3787 * Ps = 2 9 -> ANSI text locator (i.e., DEC Locator mode).
3789 * Send Device Attributes (Secondary DA).
3790 * Ps = 0 or omitted -> request the terminal's identification
3791 * code. The response depends on the decTerminalID resource set-
3792 * ting. It should apply only to VT220 and up, but xterm extends
3794 * -> CSI > Pp ; Pv ; Pc c
3795 * where Pp denotes the terminal type
3796 * Pp = 0 -> ``VT100''.
3797 * Pp = 1 -> ``VT220''.
3798 * and Pv is the firmware version (for xterm, this was originally
3799 * the XFree86 patch number, starting with 95). In a DEC termi-
3800 * nal, Pc indicates the ROM cartridge registration number and is
3803 * xterm/charproc.c - line 2012, for more information.
3804 * vim responds with ^[[?0c or ^[[?1c after the terminal's response (?)
3806 Terminal
.prototype.sendDeviceAttributes = function(params
) {
3807 if (params
[0] > 0) return;
3810 if (this.is('xterm')
3811 || this.is('rxvt-unicode')
3812 || this.is('screen')) {
3813 this.send('\x1b[?1;2c');
3814 } else if (this.is('linux')) {
3815 this.send('\x1b[?6c');
3817 } else if (this.prefix
=== '>') {
3819 // seem to spit this
3820 // out around ~370 times (?).
3821 if (this.is('xterm')) {
3822 this.send('\x1b[>0;276;0c');
3823 } else if (this.is('rxvt-unicode')) {
3824 this.send('\x1b[>85;95;0c');
3825 } else if (this.is('linux')) {
3826 // not supported by linux console.
3827 // linux console echoes parameters.
3828 this.send(params
[0] + 'c');
3829 } else if (this.is('screen')) {
3830 this.send('\x1b[>83;40003;0c');
3838 * Line Position Absolute [row] (default = [1,column]) (VPA).
3840 Terminal
.prototype.linePosAbsolute = function(params
) {
3841 var param
= params
[0];
3842 if (param
< 1) param
= 1;
3844 if (this.y
>= this.rows
) {
3845 this.y
= this.rows
- 1;
3851 * 145 65 e * VPR - Vertical Position Relative
3854 Terminal
.prototype.VPositionRelative = function(params
) {
3855 var param
= params
[0];
3856 if (param
< 1) param
= 1;
3858 if (this.y
>= this.rows
) {
3859 this.y
= this.rows
- 1;
3866 * Horizontal and Vertical Position [row;column] (default =
3869 Terminal
.prototype.HVPosition = function(params
) {
3870 if (params
[0] < 1) params
[0] = 1;
3871 if (params
[1] < 1) params
[1] = 1;
3873 this.y
= params
[0] - 1;
3874 if (this.y
>= this.rows
) {
3875 this.y
= this.rows
- 1;
3878 this.x
= params
[1] - 1;
3879 if (this.x
>= this.cols
) {
3880 this.x
= this.cols
- 1;
3886 * CSI Pm h Set Mode (SM).
3887 * Ps = 2 -> Keyboard Action Mode (AM).
3888 * Ps = 4 -> Insert Mode (IRM).
3889 * Ps = 1 2 -> Send/receive (SRM).
3890 * Ps = 2 0 -> Automatic Newline (LNM).
3892 * DEC Private Mode Set (DECSET).
3893 * Ps = 1 -> Application Cursor Keys (DECCKM).
3894 * Ps = 2 -> Designate USASCII for character sets G0-G3
3895 * (DECANM), and set VT100 mode.
3896 * Ps = 3 -> 132 Column Mode (DECCOLM).
3897 * Ps = 4 -> Smooth (Slow) Scroll (DECSCLM).
3898 * Ps = 5 -> Reverse Video (DECSCNM).
3899 * Ps = 6 -> Origin Mode (DECOM).
3900 * Ps = 7 -> Wraparound Mode (DECAWM).
3901 * Ps = 8 -> Auto-repeat Keys (DECARM).
3902 * Ps = 9 -> Send Mouse X & Y on button press. See the sec-
3903 * tion Mouse Tracking.
3904 * Ps = 1 0 -> Show toolbar (rxvt).
3905 * Ps = 1 2 -> Start Blinking Cursor (att610).
3906 * Ps = 1 8 -> Print form feed (DECPFF).
3907 * Ps = 1 9 -> Set print extent to full screen (DECPEX).
3908 * Ps = 2 5 -> Show Cursor (DECTCEM).
3909 * Ps = 3 0 -> Show scrollbar (rxvt).
3910 * Ps = 3 5 -> Enable font-shifting functions (rxvt).
3911 * Ps = 3 8 -> Enter Tektronix Mode (DECTEK).
3912 * Ps = 4 0 -> Allow 80 -> 132 Mode.
3913 * Ps = 4 1 -> more(1) fix (see curses resource).
3914 * Ps = 4 2 -> Enable Nation Replacement Character sets (DECN-
3916 * Ps = 4 4 -> Turn On Margin Bell.
3917 * Ps = 4 5 -> Reverse-wraparound Mode.
3918 * Ps = 4 6 -> Start Logging. This is normally disabled by a
3919 * compile-time option.
3920 * Ps = 4 7 -> Use Alternate Screen Buffer. (This may be dis-
3921 * abled by the titeInhibit resource).
3922 * Ps = 6 6 -> Application keypad (DECNKM).
3923 * Ps = 6 7 -> Backarrow key sends backspace (DECBKM).
3924 * Ps = 1 0 0 0 -> Send Mouse X & Y on button press and
3925 * release. See the section Mouse Tracking.
3926 * Ps = 1 0 0 1 -> Use Hilite Mouse Tracking.
3927 * Ps = 1 0 0 2 -> Use Cell Motion Mouse Tracking.
3928 * Ps = 1 0 0 3 -> Use All Motion Mouse Tracking.
3929 * Ps = 1 0 0 4 -> Send FocusIn/FocusOut events.
3930 * Ps = 1 0 0 5 -> Enable Extended Mouse Mode.
3931 * Ps = 1 0 1 0 -> Scroll to bottom on tty output (rxvt).
3932 * Ps = 1 0 1 1 -> Scroll to bottom on key press (rxvt).
3933 * Ps = 1 0 3 4 -> Interpret "meta" key, sets eighth bit.
3934 * (enables the eightBitInput resource).
3935 * Ps = 1 0 3 5 -> Enable special modifiers for Alt and Num-
3936 * Lock keys. (This enables the numLock resource).
3937 * Ps = 1 0 3 6 -> Send ESC when Meta modifies a key. (This
3938 * enables the metaSendsEscape resource).
3939 * Ps = 1 0 3 7 -> Send DEL from the editing-keypad Delete
3941 * Ps = 1 0 3 9 -> Send ESC when Alt modifies a key. (This
3942 * enables the altSendsEscape resource).
3943 * Ps = 1 0 4 0 -> Keep selection even if not highlighted.
3944 * (This enables the keepSelection resource).
3945 * Ps = 1 0 4 1 -> Use the CLIPBOARD selection. (This enables
3946 * the selectToClipboard resource).
3947 * Ps = 1 0 4 2 -> Enable Urgency window manager hint when
3948 * Control-G is received. (This enables the bellIsUrgent
3950 * Ps = 1 0 4 3 -> Enable raising of the window when Control-G
3951 * is received. (enables the popOnBell resource).
3952 * Ps = 1 0 4 7 -> Use Alternate Screen Buffer. (This may be
3953 * disabled by the titeInhibit resource).
3954 * Ps = 1 0 4 8 -> Save cursor as in DECSC. (This may be dis-
3955 * abled by the titeInhibit resource).
3956 * Ps = 1 0 4 9 -> Save cursor as in DECSC and use Alternate
3957 * Screen Buffer, clearing it first. (This may be disabled by
3958 * the titeInhibit resource). This combines the effects of the 1
3959 * 0 4 7 and 1 0 4 8 modes. Use this with terminfo-based
3960 * applications rather than the 4 7 mode.
3961 * Ps = 1 0 5 0 -> Set terminfo/termcap function-key mode.
3962 * Ps = 1 0 5 1 -> Set Sun function-key mode.
3963 * Ps = 1 0 5 2 -> Set HP function-key mode.
3964 * Ps = 1 0 5 3 -> Set SCO function-key mode.
3965 * Ps = 1 0 6 0 -> Set legacy keyboard emulation (X11R6).
3966 * Ps = 1 0 6 1 -> Set VT220 keyboard emulation.
3967 * Ps = 2 0 0 4 -> Set bracketed paste mode.
3969 * http: *vt100.net/docs/vt220-rm/chapter4.html
3971 Terminal
.prototype.setMode = function(params
) {
3972 if (typeof params
=== 'object') {
3973 var l
= params
.length
3976 for (; i
< l
; i
++) {
3977 this.setMode(params
[i
]);
3986 this.insertMode
= true;
3989 //this.convertEol = true;
3992 } else if (this.prefix
=== '?') {
3995 this.applicationCursor
= true;
3998 this.setgCharset(0, Terminal
.charsets
.US
);
3999 this.setgCharset(1, Terminal
.charsets
.US
);
4000 this.setgCharset(2, Terminal
.charsets
.US
);
4001 this.setgCharset(3, Terminal
.charsets
.US
);
4002 // set VT100 mode here
4004 case 3: // 132 col mode
4005 this.savedCols
= this.cols
;
4006 this.resize(132, this.rows
);
4009 this.originMode
= true;
4012 this.wraparoundMode
= true;
4015 // this.cursorBlink = true;
4018 this.log('Serial port requested application keypad.');
4019 this.applicationKeypad
= true;
4020 this.viewport
.setApplicationMode(true);
4022 case 9: // X10 Mouse
4023 // no release, no motion, no wheel, no modifiers.
4024 case 1000: // vt200 mouse
4026 // no modifiers, except control on the wheel.
4027 case 1002: // button event mouse
4028 case 1003: // any event mouse
4029 // any event - sends motion events,
4030 // even if there is no button held down.
4031 this.x10Mouse
= params
=== 9;
4032 this.vt200Mouse
= params
=== 1000;
4033 this.normalMouse
= params
> 1000;
4034 this.mouseEvents
= true;
4035 this.element
.style
.cursor
= 'default';
4036 this.log('Binding to mouse events.');
4038 case 1004: // send focusin/focusout events
4041 this.sendFocus
= true;
4043 case 1005: // utf8 ext mode mouse
4044 this.utfMouse
= true;
4045 // for wide terminals
4046 // simply encodes large values as utf8 characters
4048 case 1006: // sgr ext mode mouse
4049 this.sgrMouse
= true;
4050 // for wide terminals
4051 // does not add 32 to fields
4052 // press: ^[[<b;x;yM
4053 // release: ^[[<b;x;ym
4055 case 1015: // urxvt ext mode mouse
4056 this.urxvtMouse
= true;
4057 // for wide terminals
4058 // numbers for fields
4060 // motion: ^[[b;x;yT
4062 case 25: // show cursor
4063 this.cursorHidden
= false;
4065 case 1049: // alt screen buffer cursor
4066 //this.saveCursor();
4068 case 47: // alt screen buffer
4069 case 1047: // alt screen buffer
4077 scrollTop
: this.scrollTop
,
4078 scrollBottom
: this.scrollBottom
,
4080 // XXX save charset(s) here?
4081 // charset: this.charset,
4082 // glevel: this.glevel,
4083 // charsets: this.charsets
4086 this.normal
= normal
;
4095 * CSI Pm l Reset Mode (RM).
4096 * Ps = 2 -> Keyboard Action Mode (AM).
4097 * Ps = 4 -> Replace Mode (IRM).
4098 * Ps = 1 2 -> Send/receive (SRM).
4099 * Ps = 2 0 -> Normal Linefeed (LNM).
4101 * DEC Private Mode Reset (DECRST).
4102 * Ps = 1 -> Normal Cursor Keys (DECCKM).
4103 * Ps = 2 -> Designate VT52 mode (DECANM).
4104 * Ps = 3 -> 80 Column Mode (DECCOLM).
4105 * Ps = 4 -> Jump (Fast) Scroll (DECSCLM).
4106 * Ps = 5 -> Normal Video (DECSCNM).
4107 * Ps = 6 -> Normal Cursor Mode (DECOM).
4108 * Ps = 7 -> No Wraparound Mode (DECAWM).
4109 * Ps = 8 -> No Auto-repeat Keys (DECARM).
4110 * Ps = 9 -> Don't send Mouse X & Y on button press.
4111 * Ps = 1 0 -> Hide toolbar (rxvt).
4112 * Ps = 1 2 -> Stop Blinking Cursor (att610).
4113 * Ps = 1 8 -> Don't print form feed (DECPFF).
4114 * Ps = 1 9 -> Limit print to scrolling region (DECPEX).
4115 * Ps = 2 5 -> Hide Cursor (DECTCEM).
4116 * Ps = 3 0 -> Don't show scrollbar (rxvt).
4117 * Ps = 3 5 -> Disable font-shifting functions (rxvt).
4118 * Ps = 4 0 -> Disallow 80 -> 132 Mode.
4119 * Ps = 4 1 -> No more(1) fix (see curses resource).
4120 * Ps = 4 2 -> Disable Nation Replacement Character sets (DEC-
4122 * Ps = 4 4 -> Turn Off Margin Bell.
4123 * Ps = 4 5 -> No Reverse-wraparound Mode.
4124 * Ps = 4 6 -> Stop Logging. (This is normally disabled by a
4125 * compile-time option).
4126 * Ps = 4 7 -> Use Normal Screen Buffer.
4127 * Ps = 6 6 -> Numeric keypad (DECNKM).
4128 * Ps = 6 7 -> Backarrow key sends delete (DECBKM).
4129 * Ps = 1 0 0 0 -> Don't send Mouse X & Y on button press and
4130 * release. See the section Mouse Tracking.
4131 * Ps = 1 0 0 1 -> Don't use Hilite Mouse Tracking.
4132 * Ps = 1 0 0 2 -> Don't use Cell Motion Mouse Tracking.
4133 * Ps = 1 0 0 3 -> Don't use All Motion Mouse Tracking.
4134 * Ps = 1 0 0 4 -> Don't send FocusIn/FocusOut events.
4135 * Ps = 1 0 0 5 -> Disable Extended Mouse Mode.
4136 * Ps = 1 0 1 0 -> Don't scroll to bottom on tty output
4138 * Ps = 1 0 1 1 -> Don't scroll to bottom on key press (rxvt).
4139 * Ps = 1 0 3 4 -> Don't interpret "meta" key. (This disables
4140 * the eightBitInput resource).
4141 * Ps = 1 0 3 5 -> Disable special modifiers for Alt and Num-
4142 * Lock keys. (This disables the numLock resource).
4143 * Ps = 1 0 3 6 -> Don't send ESC when Meta modifies a key.
4144 * (This disables the metaSendsEscape resource).
4145 * Ps = 1 0 3 7 -> Send VT220 Remove from the editing-keypad
4147 * Ps = 1 0 3 9 -> Don't send ESC when Alt modifies a key.
4148 * (This disables the altSendsEscape resource).
4149 * Ps = 1 0 4 0 -> Do not keep selection when not highlighted.
4150 * (This disables the keepSelection resource).
4151 * Ps = 1 0 4 1 -> Use the PRIMARY selection. (This disables
4152 * the selectToClipboard resource).
4153 * Ps = 1 0 4 2 -> Disable Urgency window manager hint when
4154 * Control-G is received. (This disables the bellIsUrgent
4156 * Ps = 1 0 4 3 -> Disable raising of the window when Control-
4157 * G is received. (This disables the popOnBell resource).
4158 * Ps = 1 0 4 7 -> Use Normal Screen Buffer, clearing screen
4159 * first if in the Alternate Screen. (This may be disabled by
4160 * the titeInhibit resource).
4161 * Ps = 1 0 4 8 -> Restore cursor as in DECRC. (This may be
4162 * disabled by the titeInhibit resource).
4163 * Ps = 1 0 4 9 -> Use Normal Screen Buffer and restore cursor
4164 * as in DECRC. (This may be disabled by the titeInhibit
4165 * resource). This combines the effects of the 1 0 4 7 and 1 0
4166 * 4 8 modes. Use this with terminfo-based applications rather
4167 * than the 4 7 mode.
4168 * Ps = 1 0 5 0 -> Reset terminfo/termcap function-key mode.
4169 * Ps = 1 0 5 1 -> Reset Sun function-key mode.
4170 * Ps = 1 0 5 2 -> Reset HP function-key mode.
4171 * Ps = 1 0 5 3 -> Reset SCO function-key mode.
4172 * Ps = 1 0 6 0 -> Reset legacy keyboard emulation (X11R6).
4173 * Ps = 1 0 6 1 -> Reset keyboard emulation to Sun/PC style.
4174 * Ps = 2 0 0 4 -> Reset bracketed paste mode.
4176 Terminal
.prototype.resetMode = function(params
) {
4177 if (typeof params
=== 'object') {
4178 var l
= params
.length
4181 for (; i
< l
; i
++) {
4182 this.resetMode(params
[i
]);
4191 this.insertMode
= false;
4194 //this.convertEol = false;
4197 } else if (this.prefix
=== '?') {
4200 this.applicationCursor
= false;
4203 if (this.cols
=== 132 && this.savedCols
) {
4204 this.resize(this.savedCols
, this.rows
);
4206 delete this.savedCols
;
4209 this.originMode
= false;
4212 this.wraparoundMode
= false;
4215 // this.cursorBlink = false;
4218 this.log('Switching back to normal keypad.');
4219 this.applicationKeypad
= false;
4220 this.viewport
.setApplicationMode(false);
4222 case 9: // X10 Mouse
4223 case 1000: // vt200 mouse
4224 case 1002: // button event mouse
4225 case 1003: // any event mouse
4226 this.x10Mouse
= false;
4227 this.vt200Mouse
= false;
4228 this.normalMouse
= false;
4229 this.mouseEvents
= false;
4230 this.element
.style
.cursor
= '';
4232 case 1004: // send focusin/focusout events
4233 this.sendFocus
= false;
4235 case 1005: // utf8 ext mode mouse
4236 this.utfMouse
= false;
4238 case 1006: // sgr ext mode mouse
4239 this.sgrMouse
= false;
4241 case 1015: // urxvt ext mode mouse
4242 this.urxvtMouse
= false;
4244 case 25: // hide cursor
4245 this.cursorHidden
= true;
4247 case 1049: // alt screen buffer cursor
4249 case 47: // normal screen buffer
4250 case 1047: // normal screen buffer - clearing it first
4252 this.lines
= this.normal
.lines
;
4253 this.ybase
= this.normal
.ybase
;
4254 this.ydisp
= this.normal
.ydisp
;
4255 this.x
= this.normal
.x
;
4256 this.y
= this.normal
.y
;
4257 this.scrollTop
= this.normal
.scrollTop
;
4258 this.scrollBottom
= this.normal
.scrollBottom
;
4259 this.tabs
= this.normal
.tabs
;
4261 // if (params === 1049) {
4262 // this.x = this.savedX;
4263 // this.y = this.savedY;
4265 this.refresh(0, this.rows
- 1);
4276 * Set Scrolling Region [top;bottom] (default = full size of win-
4280 Terminal
.prototype.setScrollRegion = function(params
) {
4281 if (this.prefix
) return;
4282 this.scrollTop
= (params
[0] || 1) - 1;
4283 this.scrollBottom
= (params
[1] || this.rows
) - 1;
4291 * Save cursor (ANSI.SYS).
4293 Terminal
.prototype.saveCursor = function(params
) {
4294 this.savedX
= this.x
;
4295 this.savedY
= this.y
;
4301 * Restore cursor (ANSI.SYS).
4303 Terminal
.prototype.restoreCursor = function(params
) {
4304 this.x
= this.savedX
|| 0;
4305 this.y
= this.savedY
|| 0;
4315 * Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
4317 Terminal
.prototype.cursorForwardTab = function(params
) {
4318 var param
= params
[0] || 1;
4320 this.x
= this.nextStop();
4326 * CSI Ps S Scroll up Ps lines (default = 1) (SU).
4328 Terminal
.prototype.scrollUp = function(params
) {
4329 var param
= params
[0] || 1;
4331 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
4332 this.lines
.splice(this.ybase
+ this.scrollBottom
, 0, this.blankLine());
4335 this.updateRange(this.scrollTop
);
4336 this.updateRange(this.scrollBottom
);
4341 * CSI Ps T Scroll down Ps lines (default = 1) (SD).
4343 Terminal
.prototype.scrollDown = function(params
) {
4344 var param
= params
[0] || 1;
4346 this.lines
.splice(this.ybase
+ this.scrollBottom
, 1);
4347 this.lines
.splice(this.ybase
+ this.scrollTop
, 0, this.blankLine());
4350 this.updateRange(this.scrollTop
);
4351 this.updateRange(this.scrollBottom
);
4356 * CSI Ps ; Ps ; Ps ; Ps ; Ps T
4357 * Initiate highlight mouse tracking. Parameters are
4358 * [func;startx;starty;firstrow;lastrow]. See the section Mouse
4361 Terminal
.prototype.initMouseTracking = function(params
) {
4362 // Relevant: DECSET 1001
4368 * Reset one or more features of the title modes to the default
4369 * value. Normally, "reset" disables the feature. It is possi-
4370 * ble to disable the ability to reset features by compiling a
4371 * different default for the title modes into xterm.
4372 * Ps = 0 -> Do not set window/icon labels using hexadecimal.
4373 * Ps = 1 -> Do not query window/icon labels using hexadeci-
4375 * Ps = 2 -> Do not set window/icon labels using UTF-8.
4376 * Ps = 3 -> Do not query window/icon labels using UTF-8.
4377 * (See discussion of "Title Modes").
4379 Terminal
.prototype.resetTitleModes = function(params
) {
4385 * CSI Ps Z Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
4387 Terminal
.prototype.cursorBackwardTab = function(params
) {
4388 var param
= params
[0] || 1;
4390 this.x
= this.prevStop();
4396 * CSI Ps b Repeat the preceding graphic character Ps times (REP).
4398 Terminal
.prototype.repeatPrecedingCharacter = function(params
) {
4399 var param
= params
[0] || 1
4400 , line
= this.lines
[this.ybase
+ this.y
]
4401 , ch
= line
[this.x
- 1] || [this.defAttr
, ' ', 1];
4403 while (param
--) line
[this.x
++] = ch
;
4408 * CSI Ps g Tab Clear (TBC).
4409 * Ps = 0 -> Clear Current Column (default).
4410 * Ps = 3 -> Clear All.
4412 * Ps = 2 -> Clear Stops on Line.
4413 * http://vt100.net/annarbor/aaa-ug/section6.html
4415 Terminal
.prototype.tabClear = function(params
) {
4416 var param
= params
[0];
4418 delete this.tabs
[this.x
];
4419 } else if (param
=== 3) {
4426 * CSI Pm i Media Copy (MC).
4427 * Ps = 0 -> Print screen (default).
4428 * Ps = 4 -> Turn off printer controller mode.
4429 * Ps = 5 -> Turn on printer controller mode.
4431 * Media Copy (MC, DEC-specific).
4432 * Ps = 1 -> Print line containing cursor.
4433 * Ps = 4 -> Turn off autoprint mode.
4434 * Ps = 5 -> Turn on autoprint mode.
4435 * Ps = 1 0 -> Print composed display, ignores DECPEX.
4436 * Ps = 1 1 -> Print all pages.
4438 Terminal
.prototype.mediaCopy = function(params
) {
4445 * Set or reset resource-values used by xterm to decide whether
4446 * to construct escape sequences holding information about the
4447 * modifiers pressed with a given key. The first parameter iden-
4448 * tifies the resource to set/reset. The second parameter is the
4449 * value to assign to the resource. If the second parameter is
4450 * omitted, the resource is reset to its initial value.
4451 * Ps = 1 -> modifyCursorKeys.
4452 * Ps = 2 -> modifyFunctionKeys.
4453 * Ps = 4 -> modifyOtherKeys.
4454 * If no parameters are given, all resources are reset to their
4457 Terminal
.prototype.setResources = function(params
) {
4464 * Disable modifiers which may be enabled via the CSI > Ps; Ps m
4465 * sequence. This corresponds to a resource value of "-1", which
4466 * cannot be set with the other sequence. The parameter identi-
4467 * fies the resource to be disabled:
4468 * Ps = 1 -> modifyCursorKeys.
4469 * Ps = 2 -> modifyFunctionKeys.
4470 * Ps = 4 -> modifyOtherKeys.
4471 * If the parameter is omitted, modifyFunctionKeys is disabled.
4472 * When modifyFunctionKeys is disabled, xterm uses the modifier
4473 * keys to make an extended sequence of functions rather than
4474 * adding a parameter to each function key to denote the modi-
4477 Terminal
.prototype.disableModifiers = function(params
) {
4484 * Set resource value pointerMode. This is used by xterm to
4485 * decide whether to hide the pointer cursor as the user types.
4486 * Valid values for the parameter:
4487 * Ps = 0 -> never hide the pointer.
4488 * Ps = 1 -> hide if the mouse tracking mode is not enabled.
4489 * Ps = 2 -> always hide the pointer. If no parameter is
4490 * given, xterm uses the default, which is 1 .
4492 Terminal
.prototype.setPointerMode = function(params
) {
4498 * CSI ! p Soft terminal reset (DECSTR).
4499 * http://vt100.net/docs/vt220-rm/table4-10.html
4501 Terminal
.prototype.softReset = function(params
) {
4502 this.cursorHidden
= false;
4503 this.insertMode
= false;
4504 this.originMode
= false;
4505 this.wraparoundMode
= false; // autowrap
4506 this.applicationKeypad
= false; // ?
4507 this.viewport
.setApplicationMode(false);
4508 this.applicationCursor
= false;
4510 this.scrollBottom
= this.rows
- 1;
4511 this.curAttr
= this.defAttr
;
4512 this.x
= this.y
= 0; // ?
4513 this.charset
= null;
4514 this.glevel
= 0; // ??
4515 this.charsets
= [null]; // ??
4521 * Request ANSI mode (DECRQM). For VT300 and up, reply is
4523 * where Ps is the mode number as in RM, and Pm is the mode
4525 * 0 - not recognized
4528 * 3 - permanently set
4529 * 4 - permanently reset
4531 Terminal
.prototype.requestAnsiMode = function(params
) {
4538 * Request DEC private mode (DECRQM). For VT300 and up, reply is
4540 * where Ps is the mode number as in DECSET, Pm is the mode value
4541 * as in the ANSI DECRQM.
4543 Terminal
.prototype.requestPrivateMode = function(params
) {
4550 * Set conformance level (DECSCL). Valid values for the first
4552 * Ps = 6 1 -> VT100.
4553 * Ps = 6 2 -> VT200.
4554 * Ps = 6 3 -> VT300.
4555 * Valid values for the second parameter:
4556 * Ps = 0 -> 8-bit controls.
4557 * Ps = 1 -> 7-bit controls (always set for VT100).
4558 * Ps = 2 -> 8-bit controls.
4560 Terminal
.prototype.setConformanceLevel = function(params
) {
4566 * CSI Ps q Load LEDs (DECLL).
4567 * Ps = 0 -> Clear all LEDS (default).
4568 * Ps = 1 -> Light Num Lock.
4569 * Ps = 2 -> Light Caps Lock.
4570 * Ps = 3 -> Light Scroll Lock.
4571 * Ps = 2 1 -> Extinguish Num Lock.
4572 * Ps = 2 2 -> Extinguish Caps Lock.
4573 * Ps = 2 3 -> Extinguish Scroll Lock.
4575 Terminal
.prototype.loadLEDs = function(params
) {
4582 * Set cursor style (DECSCUSR, VT520).
4583 * Ps = 0 -> blinking block.
4584 * Ps = 1 -> blinking block (default).
4585 * Ps = 2 -> steady block.
4586 * Ps = 3 -> blinking underline.
4587 * Ps = 4 -> steady underline.
4589 Terminal
.prototype.setCursorStyle = function(params
) {
4596 * Select character protection attribute (DECSCA). Valid values
4597 * for the parameter:
4598 * Ps = 0 -> DECSED and DECSEL can erase (default).
4599 * Ps = 1 -> DECSED and DECSEL cannot erase.
4600 * Ps = 2 -> DECSED and DECSEL can erase.
4602 Terminal
.prototype.setCharProtectionAttr = function(params
) {
4609 * Restore DEC Private Mode Values. The value of Ps previously
4610 * saved is restored. Ps values are the same as for DECSET.
4612 Terminal
.prototype.restorePrivateValues = function(params
) {
4618 * CSI Pt; Pl; Pb; Pr; Ps$ r
4619 * Change Attributes in Rectangular Area (DECCARA), VT400 and up.
4620 * Pt; Pl; Pb; Pr denotes the rectangle.
4621 * Ps denotes the SGR attributes to change: 0, 1, 4, 5, 7.
4622 * NOTE: xterm doesn't enable this code by default.
4624 Terminal
.prototype.setAttrInRectangle = function(params
) {
4634 for (; t
< b
+ 1; t
++) {
4635 line
= this.lines
[this.ybase
+ t
];
4636 for (i
= l
; i
< r
; i
++) {
4637 line
[i
] = [attr
, line
[i
][1]];
4642 this.updateRange(params
[0]);
4643 this.updateRange(params
[2]);
4648 * CSI Pc; Pt; Pl; Pb; Pr$ x
4649 * Fill Rectangular Area (DECFRA), VT420 and up.
4650 * Pc is the character to use.
4651 * Pt; Pl; Pb; Pr denotes the rectangle.
4652 * NOTE: xterm doesn't enable this code by default.
4654 Terminal
.prototype.fillRectangle = function(params
) {
4664 for (; t
< b
+ 1; t
++) {
4665 line
= this.lines
[this.ybase
+ t
];
4666 for (i
= l
; i
< r
; i
++) {
4667 line
[i
] = [line
[i
][0], String
.fromCharCode(ch
)];
4672 this.updateRange(params
[1]);
4673 this.updateRange(params
[3]);
4679 * Enable Locator Reporting (DECELR).
4680 * Valid values for the first parameter:
4681 * Ps = 0 -> Locator disabled (default).
4682 * Ps = 1 -> Locator enabled.
4683 * Ps = 2 -> Locator enabled for one report, then disabled.
4684 * The second parameter specifies the coordinate unit for locator
4686 * Valid values for the second parameter:
4687 * Pu = 0 <- or omitted -> default to character cells.
4688 * Pu = 1 <- device physical pixels.
4689 * Pu = 2 <- character cells.
4691 Terminal
.prototype.enableLocatorReporting = function(params
) {
4692 var val
= params
[0] > 0;
4693 //this.mouseEvents = val;
4694 //this.decLocator = val;
4699 * CSI Pt; Pl; Pb; Pr$ z
4700 * Erase Rectangular Area (DECERA), VT400 and up.
4701 * Pt; Pl; Pb; Pr denotes the rectangle.
4702 * NOTE: xterm doesn't enable this code by default.
4704 Terminal
.prototype.eraseRectangle = function(params
) {
4714 ch
= [this.eraseAttr(), ' ', 1]; // xterm?
4716 for (; t
< b
+ 1; t
++) {
4717 line
= this.lines
[this.ybase
+ t
];
4718 for (i
= l
; i
< r
; i
++) {
4724 this.updateRange(params
[0]);
4725 this.updateRange(params
[2]);
4731 * Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
4732 * NOTE: xterm doesn't enable this code by default.
4734 Terminal
.prototype.insertColumns = function() {
4735 var param
= params
[0]
4736 , l
= this.ybase
+ this.rows
4737 , ch
= [this.eraseAttr(), ' ', 1] // xterm?
4741 for (i
= this.ybase
; i
< l
; i
++) {
4742 this.lines
[i
].splice(this.x
+ 1, 0, ch
);
4743 this.lines
[i
].pop();
4753 * Delete P s Column(s) (default = 1) (DECDC), VT420 and up
4754 * NOTE: xterm doesn't enable this code by default.
4756 Terminal
.prototype.deleteColumns = function() {
4757 var param
= params
[0]
4758 , l
= this.ybase
+ this.rows
4759 , ch
= [this.eraseAttr(), ' ', 1] // xterm?
4763 for (i
= this.ybase
; i
< l
; i
++) {
4764 this.lines
[i
].splice(this.x
, 1);
4765 this.lines
[i
].push(ch
);
4776 Terminal
.charsets
= {};
4778 // DEC Special Character and Line Drawing Set.
4779 // http://vt100.net/docs/vt102-ug/table5-13.html
4780 // A lot of curses apps use this if they see TERM=xterm.
4781 // testing: echo -e '\e(0a\e(B'
4782 // The xterm output sometimes seems to conflict with the
4783 // reference above. xterm seems in line with the reference
4784 // when running vttest however.
4785 // The table below now uses xterm's output from vttest.
4786 Terminal
.charsets
.SCLD
= { // (0
4787 '`': '\u25c6', // '◆'
4788 'a': '\u2592', // '▒'
4789 'b': '\u0009', // '\t'
4790 'c': '\u000c', // '\f'
4791 'd': '\u000d', // '\r'
4792 'e': '\u000a', // '\n'
4793 'f': '\u00b0', // '°'
4794 'g': '\u00b1', // '±'
4795 'h': '\u2424', // '\u2424' (NL)
4796 'i': '\u000b', // '\v'
4797 'j': '\u2518', // '┘'
4798 'k': '\u2510', // '┐'
4799 'l': '\u250c', // '┌'
4800 'm': '\u2514', // '└'
4801 'n': '\u253c', // '┼'
4802 'o': '\u23ba', // '⎺'
4803 'p': '\u23bb', // '⎻'
4804 'q': '\u2500', // '─'
4805 'r': '\u23bc', // '⎼'
4806 's': '\u23bd', // '⎽'
4807 't': '\u251c', // '├'
4808 'u': '\u2524', // '┤'
4809 'v': '\u2534', // '┴'
4810 'w': '\u252c', // '┬'
4811 'x': '\u2502', // '│'
4812 'y': '\u2264', // '≤'
4813 'z': '\u2265', // '≥'
4814 '{': '\u03c0', // 'π'
4815 '|': '\u2260', // '≠'
4816 '}': '\u00a3', // '£'
4817 '~': '\u00b7' // '·'
4820 Terminal
.charsets
.UK
= null; // (A
4821 Terminal
.charsets
.US
= null; // (B (USASCII)
4822 Terminal
.charsets
.Dutch
= null; // (4
4823 Terminal
.charsets
.Finnish
= null; // (C or (5
4824 Terminal
.charsets
.French
= null; // (R
4825 Terminal
.charsets
.FrenchCanadian
= null; // (Q
4826 Terminal
.charsets
.German
= null; // (K
4827 Terminal
.charsets
.Italian
= null; // (Y
4828 Terminal
.charsets
.NorwegianDanish
= null; // (E or (6
4829 Terminal
.charsets
.Spanish
= null; // (Z
4830 Terminal
.charsets
.Swedish
= null; // (H or (7
4831 Terminal
.charsets
.Swiss
= null; // (=
4832 Terminal
.charsets
.ISOLatin
= null; // /A
4838 function contains(el
, arr
) {
4839 for (var i
= 0; i
< arr
.length
; i
+= 1) {
4840 if (el
=== arr
[i
]) {
4847 function on(el
, type
, handler
, capture
) {
4848 if (!Array
.isArray(el
)) {
4851 el
.forEach(function (element
) {
4852 element
.addEventListener(type
, handler
, capture
|| false);
4856 function off(el
, type
, handler
, capture
) {
4857 el
.removeEventListener(type
, handler
, capture
|| false);
4860 function cancel(ev
, force
) {
4861 if (!this.cancelEvents
&& !force
) {
4864 ev
.preventDefault();
4865 ev
.stopPropagation();
4869 function inherits(child
, parent
) {
4871 this.constructor = child
;
4873 f
.prototype = parent
.prototype;
4874 child
.prototype = new f
;
4877 // if bold is broken, we can't
4878 // use it in the terminal.
4879 function isBoldBroken(document
) {
4880 var body
= document
.getElementsByTagName('body')[0];
4881 var el
= document
.createElement('span');
4882 el
.innerHTML
= 'hello world';
4883 body
.appendChild(el
);
4884 var w1
= el
.scrollWidth
;
4885 el
.style
.fontWeight
= 'bold';
4886 var w2
= el
.scrollWidth
;
4887 body
.removeChild(el
);
4891 function indexOf(obj
, el
) {
4894 if (obj
[i
] === el
) return i
;
4899 function isThirdLevelShift(term
, ev
) {
4901 (term
.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) ||
4902 (term
.isMSWindows
&& ev
.altKey
&& ev
.ctrlKey
&& !ev
.metaKey
);
4904 if (ev
.type
== 'keypress') {
4905 return thirdLevelKey
;
4908 // Don't invoke for arrows, pageDown, home, backspace, etc. (on non-keypress events)
4909 return thirdLevelKey
&& (!ev
.keyCode
|| ev
.keyCode
> 47);
4912 function matchColor(r1
, g1
, b1
) {
4913 var hash
= (r1
<< 16) | (g1
<< 8) | b1
;
4915 if (matchColor
._cache
[hash
] != null) {
4916 return matchColor
._cache
[hash
];
4919 var ldiff
= Infinity
4928 for (; i
< Terminal
.vcolors
.length
; i
++) {
4929 c
= Terminal
.vcolors
[i
];
4934 diff
= matchColor
.distance(r1
, g1
, b1
, r2
, g2
, b2
);
4947 return matchColor
._cache
[hash
] = li
;
4950 matchColor
._cache
= {};
4952 // http://stackoverflow.com/questions/1633828
4953 matchColor
.distance = function(r1
, g1
, b1
, r2
, g2
, b2
) {
4954 return Math
.pow(30 * (r1
- r2
), 2)
4955 + Math
.pow(59 * (g1
- g2
), 2)
4956 + Math
.pow(11 * (b1
- b2
), 2);
4959 function each(obj
, iter
, con
) {
4960 if (obj
.forEach
) return obj
.forEach(iter
, con
);
4961 for (var i
= 0; i
< obj
.length
; i
++) {
4962 iter
.call(con
, obj
[i
], i
, obj
);
4966 function keys(obj
) {
4967 if (Object
.keys
) return Object
.keys(obj
);
4970 if (Object
.prototype.hasOwnProperty
.call(obj
, key
)) {
4977 var wcwidth
= (function(opts
) {
4978 // extracted from https://www.cl.cam.ac.uk/%7Emgk25/ucs/wcwidth.c
4979 // combining characters
4981 [0x0300, 0x036F], [0x0483, 0x0486], [0x0488, 0x0489],
4982 [0x0591, 0x05BD], [0x05BF, 0x05BF], [0x05C1, 0x05C2],
4983 [0x05C4, 0x05C5], [0x05C7, 0x05C7], [0x0600, 0x0603],
4984 [0x0610, 0x0615], [0x064B, 0x065E], [0x0670, 0x0670],
4985 [0x06D6, 0x06E4], [0x06E7, 0x06E8], [0x06EA, 0x06ED],
4986 [0x070F, 0x070F], [0x0711, 0x0711], [0x0730, 0x074A],
4987 [0x07A6, 0x07B0], [0x07EB, 0x07F3], [0x0901, 0x0902],
4988 [0x093C, 0x093C], [0x0941, 0x0948], [0x094D, 0x094D],
4989 [0x0951, 0x0954], [0x0962, 0x0963], [0x0981, 0x0981],
4990 [0x09BC, 0x09BC], [0x09C1, 0x09C4], [0x09CD, 0x09CD],
4991 [0x09E2, 0x09E3], [0x0A01, 0x0A02], [0x0A3C, 0x0A3C],
4992 [0x0A41, 0x0A42], [0x0A47, 0x0A48], [0x0A4B, 0x0A4D],
4993 [0x0A70, 0x0A71], [0x0A81, 0x0A82], [0x0ABC, 0x0ABC],
4994 [0x0AC1, 0x0AC5], [0x0AC7, 0x0AC8], [0x0ACD, 0x0ACD],
4995 [0x0AE2, 0x0AE3], [0x0B01, 0x0B01], [0x0B3C, 0x0B3C],
4996 [0x0B3F, 0x0B3F], [0x0B41, 0x0B43], [0x0B4D, 0x0B4D],
4997 [0x0B56, 0x0B56], [0x0B82, 0x0B82], [0x0BC0, 0x0BC0],
4998 [0x0BCD, 0x0BCD], [0x0C3E, 0x0C40], [0x0C46, 0x0C48],
4999 [0x0C4A, 0x0C4D], [0x0C55, 0x0C56], [0x0CBC, 0x0CBC],
5000 [0x0CBF, 0x0CBF], [0x0CC6, 0x0CC6], [0x0CCC, 0x0CCD],
5001 [0x0CE2, 0x0CE3], [0x0D41, 0x0D43], [0x0D4D, 0x0D4D],
5002 [0x0DCA, 0x0DCA], [0x0DD2, 0x0DD4], [0x0DD6, 0x0DD6],
5003 [0x0E31, 0x0E31], [0x0E34, 0x0E3A], [0x0E47, 0x0E4E],
5004 [0x0EB1, 0x0EB1], [0x0EB4, 0x0EB9], [0x0EBB, 0x0EBC],
5005 [0x0EC8, 0x0ECD], [0x0F18, 0x0F19], [0x0F35, 0x0F35],
5006 [0x0F37, 0x0F37], [0x0F39, 0x0F39], [0x0F71, 0x0F7E],
5007 [0x0F80, 0x0F84], [0x0F86, 0x0F87], [0x0F90, 0x0F97],
5008 [0x0F99, 0x0FBC], [0x0FC6, 0x0FC6], [0x102D, 0x1030],
5009 [0x1032, 0x1032], [0x1036, 0x1037], [0x1039, 0x1039],
5010 [0x1058, 0x1059], [0x1160, 0x11FF], [0x135F, 0x135F],
5011 [0x1712, 0x1714], [0x1732, 0x1734], [0x1752, 0x1753],
5012 [0x1772, 0x1773], [0x17B4, 0x17B5], [0x17B7, 0x17BD],
5013 [0x17C6, 0x17C6], [0x17C9, 0x17D3], [0x17DD, 0x17DD],
5014 [0x180B, 0x180D], [0x18A9, 0x18A9], [0x1920, 0x1922],
5015 [0x1927, 0x1928], [0x1932, 0x1932], [0x1939, 0x193B],
5016 [0x1A17, 0x1A18], [0x1B00, 0x1B03], [0x1B34, 0x1B34],
5017 [0x1B36, 0x1B3A], [0x1B3C, 0x1B3C], [0x1B42, 0x1B42],
5018 [0x1B6B, 0x1B73], [0x1DC0, 0x1DCA], [0x1DFE, 0x1DFF],
5019 [0x200B, 0x200F], [0x202A, 0x202E], [0x2060, 0x2063],
5020 [0x206A, 0x206F], [0x20D0, 0x20EF], [0x302A, 0x302F],
5021 [0x3099, 0x309A], [0xA806, 0xA806], [0xA80B, 0xA80B],
5022 [0xA825, 0xA826], [0xFB1E, 0xFB1E], [0xFE00, 0xFE0F],
5023 [0xFE20, 0xFE23], [0xFEFF, 0xFEFF], [0xFFF9, 0xFFFB],
5024 [0x10A01, 0x10A03], [0x10A05, 0x10A06], [0x10A0C, 0x10A0F],
5025 [0x10A38, 0x10A3A], [0x10A3F, 0x10A3F], [0x1D167, 0x1D169],
5026 [0x1D173, 0x1D182], [0x1D185, 0x1D18B], [0x1D1AA, 0x1D1AD],
5027 [0x1D242, 0x1D244], [0xE0001, 0xE0001], [0xE0020, 0xE007F],
5031 function bisearch(ucs
) {
5033 var max
= COMBINING
.length
- 1;
5035 if (ucs
< COMBINING
[0][0] || ucs
> COMBINING
[max
][1])
5037 while (max
>= min
) {
5038 mid
= Math
.floor((min
+ max
) / 2);
5039 if (ucs
> COMBINING
[mid
][1])
5041 else if (ucs
< COMBINING
[mid
][0])
5048 function wcwidth(ucs
) {
5049 // test for 8-bit control characters
5052 if (ucs
< 32 || (ucs
>= 0x7f && ucs
< 0xa0))
5053 return opts
.control
;
5054 // binary search in table of non-spacing characters
5057 // if we arrive here, ucs is not a combining or C0/C1 control character
5062 ucs
<= 0x115f || // Hangul Jamo init. consonants
5065 (ucs
>= 0x2e80 && ucs
<= 0xa4cf && ucs
!= 0x303f) || // CJK..Yi
5066 (ucs
>= 0xac00 && ucs
<= 0xd7a3) || // Hangul Syllables
5067 (ucs
>= 0xf900 && ucs
<= 0xfaff) || // CJK Compat Ideographs
5068 (ucs
>= 0xfe10 && ucs
<= 0xfe19) || // Vertical forms
5069 (ucs
>= 0xfe30 && ucs
<= 0xfe6f) || // CJK Compat Forms
5070 (ucs
>= 0xff00 && ucs
<= 0xff60) || // Fullwidth Forms
5071 (ucs
>= 0xffe0 && ucs
<= 0xffe6) ||
5072 (ucs
>= 0x20000 && ucs
<= 0x2fffd) ||
5073 (ucs
>= 0x30000 && ucs
<= 0x3fffd)
5078 })({nul
: 0, control
: 0}); // configurable options
5084 Terminal
.EventEmitter
= EventEmitter
;
5085 Terminal
.CompositionHelper
= CompositionHelper
;
5086 Terminal
.Viewport
= Viewport
;
5087 Terminal
.inherits
= inherits
;
5090 * Adds an event listener to the terminal.
5092 * @param {string} event The name of the event. TODO: Document all event types
5093 * @param {function} callback The function to call when the event is triggered.
5097 Terminal
.cancel
= cancel
;
5099 module
.exports
= Terminal
;