2 * xterm.js: xterm, in the browser
3 * Originally forked from (with the author's permission):
4 * Fabrice Bellard's javascript vt100 for jslinux:
5 * http://bellard.org/jslinux/
6 * Copyright (c) 2011 Fabrice Bellard
7 * The original design remains. The terminal itself
8 * has been extended to include xterm CSI codes, among
13 import { CompositionHelper
} from './CompositionHelper';
14 import { EventEmitter
} from './EventEmitter';
15 import { Viewport
} from './Viewport';
16 import { rightClickHandler
, pasteHandler
, copyHandler
} from './handlers/Clipboard';
17 import { CircularList
} from './utils/CircularList';
18 import { C0
} from './EscapeSequences';
19 import { InputHandler
} from './InputHandler';
20 import { Parser
} from './Parser';
21 import { Renderer
} from './Renderer';
22 import { CharMeasure
} from './utils/CharMeasure';
23 import * as Browser
from './utils/Browser';
24 import * as Keyboard
from './utils/Keyboard';
25 import { CHARSETS
} from './Charsets';
28 * Terminal Emulation References:
30 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.txt
31 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
32 * http://invisible-island.net/vttest/
33 * http://www.inwap.com/pdp10/ansicode.txt
34 * http://linux.die.net/man/4/console_codes
35 * http://linux.die.net/man/7/urxvt
38 // Let it work inside Node.js for automated testing purposes.
39 var document
= (typeof window
!= 'undefined') ? window
.document
: null;
42 * The amount of write requests to queue before sending an XOFF signal to the
43 * pty process. This number must be small in order for ^C and similar sequences
46 var WRITE_BUFFER_PAUSE_THRESHOLD
= 5;
49 * The number of writes to perform in a single batch before allowing the
50 * renderer to catch up with a 0ms setTimeout.
52 var WRITE_BATCH_SIZE
= 300;
59 * Creates a new `Terminal` object.
61 * @param {object} options An object containing a set of options, the available options are:
62 * - `cursorBlink` (boolean): Whether the terminal cursor blinks
63 * - `cols` (number): The number of columns of the terminal (horizontal size)
64 * - `rows` (number): The number of rows of the terminal (vertical size)
68 * @alias module:xterm/src/xterm
70 function Terminal(options
) {
73 if (!(this instanceof Terminal
)) {
74 return new Terminal(arguments
[0], arguments
[1], arguments
[2]);
77 self
.browser
= Browser
;
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];
127 this.geometry
= [this.cols
, this.rows
];
129 if (options
.handler
) {
130 this.on('data', options
.handler
);
134 * The scroll position of the y cursor, ie. ybase + y = the y position within the entire
140 * The scroll position of the viewport
145 * The cursor's x position after ybase
150 * The cursor's y position after ybase
154 this.cursorState
= 0;
155 this.cursorHidden
= false;
159 this.scrollBottom
= this.rows
- 1;
160 this.customKeydownHandler
= null;
163 this.applicationKeypad
= false;
164 this.applicationCursor
= false;
165 this.originMode
= false;
166 this.insertMode
= false;
167 this.wraparoundMode
= true; // defaults: xterm - true, vt100 - false
172 this.gcharset
= null;
174 this.charsets
= [null];
198 this.readable
= true;
199 this.writable
= true;
201 this.defAttr
= (0 << 18) | (257 << 9) | (256 << 0);
202 this.curAttr
= this.defAttr
;
205 this.currentParam
= 0;
209 this.inputHandler
= new InputHandler(this);
210 this.parser
= new Parser(this.inputHandler
, this);
211 // Reuse renderer if the Terminal is being recreated via a Terminal.reset call.
212 this.renderer
= this.renderer
|| null;
215 this.writeBuffer
= [];
216 this.writeInProgress
= false;
219 * Whether _xterm.js_ sent XOFF in order to catch up with the pty process.
220 * This is a distinct state from writeStopped so that if the user requested
221 * XOFF via ^S that it will not automatically resume when the writeBuffer goes
224 this.xoffSentToCatchUp
= false;
226 /** Whether writing has been stopped as a result of XOFF */
227 this.writeStopped
= false;
229 // leftover surrogate high from previous write invocation
230 this.surrogate_high
= '';
233 * An array of all lines in the entire buffer, including the prompt. The lines are array of
234 * characters which are 2-length arrays where [0] is an attribute and [1] is the character.
236 this.lines
= new CircularList(this.scrollback
);
239 this.lines
.push(this.blankLine());
245 // Store if user went browsing history in scrollback
246 this.userScrolling
= false;
249 inherits(Terminal
, EventEmitter
);
252 * back_color_erase feature for xterm.
254 Terminal
.prototype.eraseAttr = function() {
255 // if (this.is('screen')) return this.defAttr;
256 return (this.defAttr
& ~0x1ff) | (this.curAttr
& 0x1ff);
264 Terminal
.tangoColors
= [
285 // Colors 0-15 + 16-255
286 // Much thanks to TooTallNate for writing this.
287 Terminal
.colors
= (function() {
288 var colors
= Terminal
.tangoColors
.slice()
289 , r
= [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff]
294 for (; i
< 216; i
++) {
295 out(r
[(i
/ 36) % 6 | 0], r
[(i
/ 6) % 6 | 0], r
[i
% 6]);
300 for (; i
< 24; i
++) {
305 function out(r
, g
, b
) {
306 colors
.push('#' + hex(r
) + hex(g
) + hex(b
));
311 return c
.length
< 2 ? '0' + c
: c
;
317 Terminal
._colors
= Terminal
.colors
.slice();
319 Terminal
.vcolors
= (function() {
321 , colors
= Terminal
.colors
325 for (; i
< 256; i
++) {
326 color
= parseInt(colors
[i
].substring(1), 16);
328 (color
>> 16) & 0xff,
341 Terminal
.defaults
= {
342 colors
: Terminal
.colors
,
348 cursorStyle
: 'block',
356 useFlowControl
: false,
358 // programFeatures: false,
362 Terminal
.options
= {};
364 Terminal
.focus
= null;
366 each(keys(Terminal
.defaults
), function(key
) {
367 Terminal
[key
] = Terminal
.defaults
[key
];
368 Terminal
.options
[key
] = Terminal
.defaults
[key
];
372 * Focus the terminal. Delegates focus handling to the terminal's DOM element.
374 Terminal
.prototype.focus = function() {
375 return this.textarea
.focus();
379 * Retrieves an option's value from the terminal.
380 * @param {string} key The option key.
382 Terminal
.prototype.getOption = function(key
, value
) {
383 if (!(key
in Terminal
.defaults
)) {
384 throw new Error('No option with key "' + key
+ '"');
387 if (typeof this.options
[key
] !== 'undefined') {
388 return this.options
[key
];
395 * Sets an option on the terminal.
396 * @param {string} key The option key.
397 * @param {string} value The option value.
399 Terminal
.prototype.setOption = function(key
, value
) {
400 if (!(key
in Terminal
.defaults
)) {
401 throw new Error('No option with key "' + key
+ '"');
405 if (this.options
[key
] !== value
) {
406 if (this.lines
.length
> value
) {
407 const amountToTrim
= this.lines
.length
- value
;
408 const needsRefresh
= (this.ydisp
- amountToTrim
< 0);
409 this.lines
.trimStart(amountToTrim
);
410 this.ybase
= Math
.max(this.ybase
- amountToTrim
, 0);
411 this.ydisp
= Math
.max(this.ydisp
- amountToTrim
, 0);
413 this.refresh(0, this.rows
- 1);
416 this.lines
.maxLength
= value
;
417 this.viewport
.syncScrollArea();
422 this.options
[key
] = value
;
424 case 'cursorBlink': this.element
.classList
.toggle('xterm-cursor-blink', value
); break;
426 // Style 'block' applies with no class
427 this.element
.classList
.toggle(`xterm-cursor-style-underline`, value
=== 'underline');
428 this.element
.classList
.toggle(`xterm-cursor-style-bar`, value
=== 'bar');
430 case 'tabStopWidth': this.setupStops(); break;
435 * Binds the desired focus behavior on a given terminal object.
439 Terminal
.bindFocus = function (term
) {
440 on(term
.textarea
, 'focus', function (ev
) {
441 if (term
.sendFocus
) {
442 term
.send(C0
.ESC
+ '[I');
444 term
.element
.classList
.add('focus');
446 Terminal
.focus
= term
;
447 term
.emit('focus', {terminal
: term
});
452 * Blur the terminal. Delegates blur handling to the terminal's DOM element.
454 Terminal
.prototype.blur = function() {
455 return this.textarea
.blur();
459 * Binds the desired blur behavior on a given terminal object.
463 Terminal
.bindBlur = function (term
) {
464 on(term
.textarea
, 'blur', function (ev
) {
465 term
.refresh(term
.y
, term
.y
);
466 if (term
.sendFocus
) {
467 term
.send(C0
.ESC
+ '[O');
469 term
.element
.classList
.remove('focus');
470 Terminal
.focus
= null;
471 term
.emit('blur', {terminal
: term
});
476 * Initialize default behavior
478 Terminal
.prototype.initGlobal = function() {
481 Terminal
.bindKeys(this);
482 Terminal
.bindFocus(this);
483 Terminal
.bindBlur(this);
485 // Bind clipboard functionality
486 on(this.element
, 'copy', function (ev
) {
487 copyHandler
.call(this, ev
, term
);
489 on(this.textarea
, 'paste', function (ev
) {
490 pasteHandler
.call(this, ev
, term
);
492 on(this.element
, 'paste', function (ev
) {
493 pasteHandler
.call(this, ev
, term
);
496 function rightClickHandlerWrapper (ev
) {
497 rightClickHandler
.call(this, ev
, term
);
500 if (term
.browser
.isFirefox
) {
501 on(this.element
, 'mousedown', function (ev
) {
502 if (ev
.button
== 2) {
503 rightClickHandlerWrapper(ev
);
507 on(this.element
, 'contextmenu', rightClickHandlerWrapper
);
512 * Apply key handling to the terminal
514 Terminal
.bindKeys = function(term
) {
515 on(term
.element
, 'keydown', function(ev
) {
516 if (document
.activeElement
!= this) {
522 on(term
.element
, 'keypress', function(ev
) {
523 if (document
.activeElement
!= this) {
529 on(term
.element
, 'keyup', function(ev
) {
530 if (!wasMondifierKeyOnlyEvent(ev
)) {
535 on(term
.textarea
, 'keydown', function(ev
) {
539 on(term
.textarea
, 'keypress', function(ev
) {
541 // Truncate the textarea's value, since it is not needed
545 on(term
.textarea
, 'compositionstart', term
.compositionHelper
.compositionstart
.bind(term
.compositionHelper
));
546 on(term
.textarea
, 'compositionupdate', term
.compositionHelper
.compositionupdate
.bind(term
.compositionHelper
));
547 on(term
.textarea
, 'compositionend', term
.compositionHelper
.compositionend
.bind(term
.compositionHelper
));
548 term
.on('refresh', term
.compositionHelper
.updateCompositionElements
.bind(term
.compositionHelper
));
553 * Insert the given row to the terminal or produce a new one
554 * if no row argument is passed. Return the inserted row.
555 * @param {HTMLElement} row (optional) The row to append to the terminal.
557 Terminal
.prototype.insertRow = function (row
) {
558 if (typeof row
!= 'object') {
559 row
= document
.createElement('div');
562 this.rowContainer
.appendChild(row
);
563 this.children
.push(row
);
569 * Opens the terminal within an element.
571 * @param {HTMLElement} parent The element to create the terminal within.
573 Terminal
.prototype.open = function(parent
) {
574 var self
=this, i
=0, div
;
576 this.parent
= parent
|| this.parent
;
579 throw new Error('Terminal requires a parent element.');
582 // Grab global elements
583 this.context
= this.parent
.ownerDocument
.defaultView
;
584 this.document
= this.parent
.ownerDocument
;
585 this.body
= this.document
.getElementsByTagName('body')[0];
587 //Create main element container
588 this.element
= this.document
.createElement('div');
589 this.element
.classList
.add('terminal');
590 this.element
.classList
.add('xterm');
591 this.element
.classList
.add('xterm-theme-' + this.theme
);
592 this.element
.classList
.toggle('xterm-cursor-blink', this.options
.cursorBlink
);
594 this.element
.style
.height
595 this.element
.setAttribute('tabindex', 0);
597 this.viewportElement
= document
.createElement('div');
598 this.viewportElement
.classList
.add('xterm-viewport');
599 this.element
.appendChild(this.viewportElement
);
600 this.viewportScrollArea
= document
.createElement('div');
601 this.viewportScrollArea
.classList
.add('xterm-scroll-area');
602 this.viewportElement
.appendChild(this.viewportScrollArea
);
604 // Create the container that will hold the lines of the terminal and then
605 // produce the lines the lines.
606 this.rowContainer
= document
.createElement('div');
607 this.rowContainer
.classList
.add('xterm-rows');
608 this.element
.appendChild(this.rowContainer
);
611 // Create the container that will hold helpers like the textarea for
612 // capturing DOM Events. Then produce the helpers.
613 this.helperContainer
= document
.createElement('div');
614 this.helperContainer
.classList
.add('xterm-helpers');
615 // TODO: This should probably be inserted once it's filled to prevent an additional layout
616 this.element
.appendChild(this.helperContainer
);
617 this.textarea
= document
.createElement('textarea');
618 this.textarea
.classList
.add('xterm-helper-textarea');
619 this.textarea
.setAttribute('autocorrect', 'off');
620 this.textarea
.setAttribute('autocapitalize', 'off');
621 this.textarea
.setAttribute('spellcheck', 'false');
622 this.textarea
.tabIndex
= 0;
623 this.textarea
.addEventListener('focus', function() {
624 self
.emit('focus', {terminal
: self
});
626 this.textarea
.addEventListener('blur', function() {
627 self
.emit('blur', {terminal
: self
});
629 this.helperContainer
.appendChild(this.textarea
);
631 this.compositionView
= document
.createElement('div');
632 this.compositionView
.classList
.add('composition-view');
633 this.compositionHelper
= new CompositionHelper(this.textarea
, this.compositionView
, this);
634 this.helperContainer
.appendChild(this.compositionView
);
636 this.charSizeStyleElement
= document
.createElement('style');
637 this.helperContainer
.appendChild(this.charSizeStyleElement
);
639 for (; i
< this.rows
; i
++) {
642 this.parent
.appendChild(this.element
);
644 this.charMeasure
= new CharMeasure(this.helperContainer
);
645 this.charMeasure
.on('charsizechanged', function () {
646 self
.updateCharSizeCSS();
648 this.charMeasure
.measure();
650 this.viewport
= new Viewport(this, this.viewportElement
, this.viewportScrollArea
, this.charMeasure
);
651 this.renderer
= new Renderer(this);
653 // Setup loop that draws to screen
654 this.refresh(0, this.rows
- 1);
656 // Initialize global actions that
657 // need to be taken on the document.
660 // Ensure there is a Terminal.focus.
663 on(this.element
, 'click', function() {
664 var selection
= document
.getSelection(),
665 collapsed
= selection
.isCollapsed
,
666 isRange
= typeof collapsed
== 'boolean' ? !collapsed
: selection
.type
== 'Range';
672 // Listen for mouse events and translate
673 // them into terminal mouse protocols.
677 * This event is emitted when terminal has completed opening.
686 * Attempts to load an add-on using CommonJS or RequireJS (whichever is available).
687 * @param {string} addon The name of the addon to load
690 Terminal
.loadAddon = function(addon
, callback
) {
691 if (typeof exports
=== 'object' && typeof module
=== 'object') {
693 return require('./addons/' + addon
+ '/' + addon
);
694 } else if (typeof define
== 'function') {
696 return require(['./addons/' + addon
+ '/' + addon
], callback
);
698 console
.error('Cannot load a module without a CommonJS or RequireJS environment.');
704 * Updates the helper CSS class with any changes necessary after the terminal's
705 * character width has been changed.
707 Terminal
.prototype.updateCharSizeCSS = function() {
708 this.charSizeStyleElement
.textContent
= '.xterm-wide-char{width:' + (this.charMeasure
.width
* 2) + 'px;}';
713 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#Mouse%20Tracking
714 * To better understand these
715 * the xterm code is very helpful:
717 * button.c, charproc.c, misc.c
718 * Relevant functions in xterm/button.c:
719 * BtnCode, EmitButtonCode, EditorButton, SendMousePosition
721 Terminal
.prototype.bindMouse = function() {
722 var el
= this.element
, self
= this, pressed
= 32;
724 // mouseup, mousedown, wheel
725 // left click: ^[[M 3<^[[M#3<
727 function sendButton(ev
) {
731 // get the xterm-style button
732 button
= getButton(ev
);
734 // get mouse coordinates
738 sendEvent(button
, pos
);
740 switch (ev
.overrideType
|| ev
.type
) {
745 // keep it at the left
746 // button, just in case.
757 // motion example of a left click:
758 // ^[[M 3<^[[M@4<^[[M@5<^[[M@6<^[[M@7<^[[M#7<
759 function sendMove(ev
) {
766 // buttons marked as motions
767 // are incremented by 32
770 sendEvent(button
, pos
);
774 // position to characters
775 function encode(data
, ch
) {
776 if (!self
.utfMouse
) {
777 if (ch
=== 255) return data
.push(0);
778 if (ch
> 127) ch
= 127;
781 if (ch
=== 2047) return data
.push(0);
785 if (ch
> 2047) ch
= 2047;
786 data
.push(0xC0 | (ch
>> 6));
787 data
.push(0x80 | (ch
& 0x3F));
792 // send a mouse event:
793 // regular/utf8: ^[[M Cb Cx Cy
794 // urxvt: ^[[ Cb ; Cx ; Cy M
795 // sgr: ^[[ Cb ; Cx ; Cy M/m
796 // vt300: ^[[ 24(1/3/5)~ [ Cx , Cy ] \r
797 // locator: CSI P e ; P b ; P r ; P c ; P p & w
798 function sendEvent(button
, pos
) {
799 // self.emit('mouse', {
805 if (self
.vt300Mouse
) {
807 // http://www.vt100.net/docs/vt3xx-gp/chapter15.html
811 var data
= C0
.ESC
+ '[24';
812 if (button
=== 0) data
+= '1';
813 else if (button
=== 1) data
+= '3';
814 else if (button
=== 2) data
+= '5';
815 else if (button
=== 3) return;
817 data
+= '~[' + pos
.x
+ ',' + pos
.y
+ ']\r';
822 if (self
.decLocator
) {
827 if (button
=== 0) button
= 2;
828 else if (button
=== 1) button
= 4;
829 else if (button
=== 2) button
= 6;
830 else if (button
=== 3) button
= 3;
831 self
.send(C0
.ESC
+ '['
834 + (button
=== 3 ? 4 : 0)
845 if (self
.urxvtMouse
) {
850 self
.send(C0
.ESC
+ '[' + button
+ ';' + pos
.x
+ ';' + pos
.y
+ 'M');
857 self
.send(C0
.ESC
+ '[<'
858 + (((button
& 3) === 3 ? button
& ~3 : button
) - 32)
863 + ((button
& 3) === 3 ? 'm' : 'M'));
869 encode(data
, button
);
873 self
.send(C0
.ESC
+ '[M' + String
.fromCharCode
.apply(String
, data
));
876 function getButton(ev
) {
889 // 1, and 2 - with 64 added
890 switch (ev
.overrideType
|| ev
.type
) {
892 button
= ev
.button
!= null
898 if (self
.browser
.isMSIE
) {
899 button
= button
=== 1 ? 0 : button
=== 4 ? 1 : button
;
905 case 'DOMMouseScroll':
906 button
= ev
.detail
< 0
911 button
= ev
.wheelDeltaY
> 0
917 // next three bits are the modifiers:
918 // 4 = shift, 8 = meta, 16 = control
919 shift
= ev
.shiftKey
? 4 : 0;
920 meta
= ev
.metaKey
? 8 : 0;
921 ctrl
= ev
.ctrlKey
? 16 : 0;
922 mod
= shift
| meta
| ctrl
;
925 if (self
.vt200Mouse
) {
928 } else if (!self
.normalMouse
) {
933 button
= (32 + (mod
<< 2)) + button
;
938 // mouse coordinates measured in cols/rows
939 function getCoords(ev
) {
942 // ignore browsers without pageX for now
943 if (ev
.pageX
== null) return;
949 // should probably check offsetParent
950 // but this is more portable
951 while (el
&& el
!== self
.document
.documentElement
) {
954 el
= 'offsetParent' in el
959 // convert to cols/rows
960 w
= self
.element
.clientWidth
;
961 h
= self
.element
.clientHeight
;
962 x
= Math
.ceil((x
/ w
) * self
.cols
);
963 y
= Math
.ceil((y
/ h
) * self
.rows
);
965 // be sure to avoid sending
966 // bad positions to the program
968 if (x
> self
.cols
) x
= self
.cols
;
970 if (y
> self
.rows
) y
= self
.rows
;
972 // xterm sends raw bytes and
973 // starts at 32 (SP) for each.
984 on(el
, 'mousedown', function(ev
) {
985 if (!self
.mouseEvents
) return;
994 //if (self.vt200Mouse && !self.normalMouse) {
995 if (self
.vt200Mouse
) {
996 ev
.overrideType
= 'mouseup';
998 return self
.cancel(ev
);
1002 if (self
.normalMouse
) on(self
.document
, 'mousemove', sendMove
);
1004 // x10 compatibility mode can't send button releases
1005 if (!self
.x10Mouse
) {
1006 on(self
.document
, 'mouseup', function up(ev
) {
1008 if (self
.normalMouse
) off(self
.document
, 'mousemove', sendMove
);
1009 off(self
.document
, 'mouseup', up
);
1010 return self
.cancel(ev
);
1014 return self
.cancel(ev
);
1017 //if (self.normalMouse) {
1018 // on(self.document, 'mousemove', sendMove);
1021 on(el
, 'wheel', function(ev
) {
1022 if (!self
.mouseEvents
) return;
1025 || self
.decLocator
) return;
1027 return self
.cancel(ev
);
1030 // allow wheel scrolling in
1031 // the shell for example
1032 on(el
, 'wheel', function(ev
) {
1033 if (self
.mouseEvents
) return;
1034 self
.viewport
.onWheel(ev
);
1035 return self
.cancel(ev
);
1040 * Destroys the terminal.
1042 Terminal
.prototype.destroy = function() {
1043 this.readable
= false;
1044 this.writable
= false;
1046 this.handler = function() {};
1047 this.write = function() {};
1048 if (this.element
.parentNode
) {
1049 this.element
.parentNode
.removeChild(this.element
);
1051 //this.emit('close');
1055 * Tells the renderer to refresh terminal content between two rows (inclusive) at the next
1057 * @param {number} start The row to start from (between 0 and terminal's height terminal - 1)
1058 * @param {number} end The row to end at (between fromRow and terminal's height terminal - 1)
1060 Terminal
.prototype.refresh = function(start
, end
) {
1061 if (this.renderer
) {
1062 this.renderer
.queueRefresh(start
, end
);
1067 * Display the cursor element
1069 Terminal
.prototype.showCursor = function() {
1070 if (!this.cursorState
) {
1071 this.cursorState
= 1;
1072 this.refresh(this.y
, this.y
);
1077 * Scroll the terminal down 1 row, creating a blank line.
1079 Terminal
.prototype.scroll = function() {
1082 // Make room for the new row in lines
1083 if (this.lines
.length
=== this.lines
.maxLength
) {
1084 this.lines
.trimStart(1);
1086 if (this.ydisp
!== 0) {
1093 // TODO: Why is this done twice?
1094 if (!this.userScrolling
) {
1095 this.ydisp
= this.ybase
;
1099 row
= this.ybase
+ this.rows
- 1;
1101 // subtract the bottom scroll region
1102 row
-= this.rows
- 1 - this.scrollBottom
;
1104 if (row
=== this.lines
.length
) {
1105 // Optimization: pushing is faster than splicing when they amount to the same behavior
1106 this.lines
.push(this.blankLine());
1109 this.lines
.splice(row
, 0, this.blankLine());
1112 if (this.scrollTop
!== 0) {
1113 if (this.ybase
!== 0) {
1115 if (!this.userScrolling
) {
1116 this.ydisp
= this.ybase
;
1119 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
1123 this.updateRange(this.scrollTop
);
1124 this.updateRange(this.scrollBottom
);
1127 * This event is emitted whenever the terminal is scrolled.
1128 * The one parameter passed is the new y display position.
1132 this.emit('scroll', this.ydisp
);
1136 * Scroll the display of the terminal
1137 * @param {number} disp The number of lines to scroll down (negatives scroll up).
1138 * @param {boolean} suppressScrollEvent Don't emit the scroll event as scrollDisp. This is used
1139 * to avoid unwanted events being handled by the veiwport when the event was triggered from the
1140 * viewport originally.
1142 Terminal
.prototype.scrollDisp = function(disp
, suppressScrollEvent
) {
1144 this.userScrolling
= true;
1145 } else if (disp
+ this.ydisp
>= this.ybase
) {
1146 this.userScrolling
= false;
1151 if (this.ydisp
> this.ybase
) {
1152 this.ydisp
= this.ybase
;
1153 } else if (this.ydisp
< 0) {
1157 if (!suppressScrollEvent
) {
1158 this.emit('scroll', this.ydisp
);
1161 this.refresh(0, this.rows
- 1);
1165 * Scroll the display of the terminal by a number of pages.
1166 * @param {number} pageCount The number of pages to scroll (negative scrolls up).
1168 Terminal
.prototype.scrollPages = function(pageCount
) {
1169 this.scrollDisp(pageCount
* (this.rows
- 1));
1173 * Scrolls the display of the terminal to the top.
1175 Terminal
.prototype.scrollToTop = function() {
1176 this.scrollDisp(-this.ydisp
);
1180 * Scrolls the display of the terminal to the bottom.
1182 Terminal
.prototype.scrollToBottom = function() {
1183 this.scrollDisp(this.ybase
- this.ydisp
);
1187 * Writes text to the terminal.
1188 * @param {string} text The text to write to the terminal.
1190 Terminal
.prototype.write = function(data
) {
1191 this.writeBuffer
.push(data
);
1193 // Send XOFF to pause the pty process if the write buffer becomes too large so
1194 // xterm.js can catch up before more data is sent. This is necessary in order
1195 // to keep signals such as ^C responsive.
1196 if (this.options
.useFlowControl
&& !this.xoffSentToCatchUp
&& this.writeBuffer
.length
>= WRITE_BUFFER_PAUSE_THRESHOLD
) {
1197 // XOFF - stop pty pipe
1198 // XON will be triggered by emulator before processing data chunk
1200 this.xoffSentToCatchUp
= true;
1203 if (!this.writeInProgress
&& this.writeBuffer
.length
> 0) {
1204 // Kick off a write which will write all data in sequence recursively
1205 this.writeInProgress
= true;
1206 // Kick off an async innerWrite so more writes can come in while processing data
1208 setTimeout(function () {
1214 Terminal
.prototype.innerWrite = function() {
1215 var writeBatch
= this.writeBuffer
.splice(0, WRITE_BATCH_SIZE
);
1216 while (writeBatch
.length
> 0) {
1217 var data
= writeBatch
.shift();
1218 var l
= data
.length
, i
= 0, j
, cs
, ch
, code
, low
, ch_width
, row
;
1220 // If XOFF was sent in order to catch up with the pty process, resume it if
1221 // the writeBuffer is empty to allow more data to come in.
1222 if (this.xoffSentToCatchUp
&& writeBatch
.length
=== 0 && this.writeBuffer
.length
=== 0) {
1224 this.xoffSentToCatchUp
= false;
1227 this.refreshStart
= this.y
;
1228 this.refreshEnd
= this.y
;
1230 this.parser
.parse(data
);
1232 this.updateRange(this.y
);
1233 this.refresh(this.refreshStart
, this.refreshEnd
);
1235 if (this.writeBuffer
.length
> 0) {
1236 // Allow renderer to catch up before processing the next batch
1238 setTimeout(function () {
1242 this.writeInProgress
= false;
1247 * Writes text to the terminal, followed by a break line character (\n).
1248 * @param {string} text The text to write to the terminal.
1250 Terminal
.prototype.writeln = function(data
) {
1251 this.write(data
+ '\r\n');
1255 * Attaches a custom keydown handler which is run before keys are processed, giving consumers of
1256 * xterm.js ultimate control as to what keys should be processed by the terminal and what keys
1258 * @param {function} customKeydownHandler The custom KeyboardEvent handler to attach. This is a
1259 * function that takes a KeyboardEvent, allowing consumers to stop propogation and/or prevent
1260 * the default action. The function returns whether the event should be processed by xterm.js.
1262 Terminal
.prototype.attachCustomKeydownHandler = function(customKeydownHandler
) {
1263 this.customKeydownHandler
= customKeydownHandler
;
1267 * Handle a keydown event
1269 * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
1270 * @param {KeyboardEvent} ev The keydown event to be handled.
1272 Terminal
.prototype.keyDown = function(ev
) {
1273 if (this.customKeydownHandler
&& this.customKeydownHandler(ev
) === false) {
1277 if (!this.compositionHelper
.keydown
.bind(this.compositionHelper
)(ev
)) {
1278 if (this.ybase
!== this.ydisp
) {
1279 this.scrollToBottom();
1285 var result
= this.evaluateKeyEscapeSequence(ev
);
1287 if (result
.key
=== C0
.DC3
) { // XOFF
1288 this.writeStopped
= true;
1289 } else if (result
.key
=== C0
.DC1
) { // XON
1290 this.writeStopped
= false;
1293 if (result
.scrollDisp
) {
1294 this.scrollDisp(result
.scrollDisp
);
1295 return this.cancel(ev
, true);
1298 if (isThirdLevelShift(this, ev
)) {
1302 if (result
.cancel
) {
1303 // The event is canceled at the end already, is this necessary?
1304 this.cancel(ev
, true);
1311 this.emit('keydown', ev
);
1312 this.emit('key', result
.key
, ev
);
1314 this.handler(result
.key
);
1316 return this.cancel(ev
, true);
1320 * Returns an object that determines how a KeyboardEvent should be handled. The key of the
1321 * returned value is the new key code to pass to the PTY.
1323 * Reference: http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
1324 * @param {KeyboardEvent} ev The keyboard event to be translated to key escape sequence.
1326 Terminal
.prototype.evaluateKeyEscapeSequence = function(ev
) {
1328 // Whether to cancel event propogation (NOTE: this may not be needed since the event is
1329 // canceled at the end of keyDown
1331 // The new key even to emit
1333 // The number of characters to scroll, if this is defined it will cancel the event
1334 scrollDisp
: undefined
1336 var modifiers
= ev
.shiftKey
<< 0 | ev
.altKey
<< 1 | ev
.ctrlKey
<< 2 | ev
.metaKey
<< 3;
1337 switch (ev
.keyCode
) {
1341 result
.key
= C0
.BS
; // ^H
1344 result
.key
= C0
.DEL
; // ^?
1349 result
.key
= C0
.ESC
+ '[Z';
1353 result
.cancel
= true;
1358 result
.cancel
= true;
1362 result
.key
= C0
.ESC
;
1363 result
.cancel
= true;
1368 result
.key
= C0
.ESC
+ '[1;' + (modifiers
+ 1) + 'D';
1369 // HACK: Make Alt + left-arrow behave like Ctrl + left-arrow: move one word backwards
1370 // http://unix.stackexchange.com/a/108106
1371 // macOS uses different escape sequences than linux
1372 if (result
.key
== C0
.ESC
+ '[1;3D') {
1373 result
.key
= (this.browser
.isMac
) ? C0
.ESC
+ 'b' : C0
.ESC
+ '[1;5D';
1375 } else if (this.applicationCursor
) {
1376 result
.key
= C0
.ESC
+ 'OD';
1378 result
.key
= C0
.ESC
+ '[D';
1384 result
.key
= C0
.ESC
+ '[1;' + (modifiers
+ 1) + 'C';
1385 // HACK: Make Alt + right-arrow behave like Ctrl + right-arrow: move one word forward
1386 // http://unix.stackexchange.com/a/108106
1387 // macOS uses different escape sequences than linux
1388 if (result
.key
== C0
.ESC
+ '[1;3C') {
1389 result
.key
= (this.browser
.isMac
) ? C0
.ESC
+ 'f' : C0
.ESC
+ '[1;5C';
1391 } else if (this.applicationCursor
) {
1392 result
.key
= C0
.ESC
+ 'OC';
1394 result
.key
= C0
.ESC
+ '[C';
1400 result
.key
= C0
.ESC
+ '[1;' + (modifiers
+ 1) + 'A';
1401 // HACK: Make Alt + up-arrow behave like Ctrl + up-arrow
1402 // http://unix.stackexchange.com/a/108106
1403 if (result
.key
== C0
.ESC
+ '[1;3A') {
1404 result
.key
= C0
.ESC
+ '[1;5A';
1406 } else if (this.applicationCursor
) {
1407 result
.key
= C0
.ESC
+ 'OA';
1409 result
.key
= C0
.ESC
+ '[A';
1415 result
.key
= C0
.ESC
+ '[1;' + (modifiers
+ 1) + 'B';
1416 // HACK: Make Alt + down-arrow behave like Ctrl + down-arrow
1417 // http://unix.stackexchange.com/a/108106
1418 if (result
.key
== C0
.ESC
+ '[1;3B') {
1419 result
.key
= C0
.ESC
+ '[1;5B';
1421 } else if (this.applicationCursor
) {
1422 result
.key
= C0
.ESC
+ 'OB';
1424 result
.key
= C0
.ESC
+ '[B';
1429 if (!ev
.shiftKey
&& !ev
.ctrlKey
) {
1430 // <Ctrl> or <Shift> + <Insert> are used to
1431 // copy-paste on some systems.
1432 result
.key
= C0
.ESC
+ '[2~';
1438 result
.key
= C0
.ESC
+ '[3;' + (modifiers
+ 1) + '~';
1440 result
.key
= C0
.ESC
+ '[3~';
1446 result
.key
= C0
.ESC
+ '[1;' + (modifiers
+ 1) + 'H';
1447 else if (this.applicationCursor
)
1448 result
.key
= C0
.ESC
+ 'OH';
1450 result
.key
= C0
.ESC
+ '[H';
1455 result
.key
= C0
.ESC
+ '[1;' + (modifiers
+ 1) + 'F';
1456 else if (this.applicationCursor
)
1457 result
.key
= C0
.ESC
+ 'OF';
1459 result
.key
= C0
.ESC
+ '[F';
1464 result
.scrollDisp
= -(this.rows
- 1);
1466 result
.key
= C0
.ESC
+ '[5~';
1472 result
.scrollDisp
= this.rows
- 1;
1474 result
.key
= C0
.ESC
+ '[6~';
1480 result
.key
= C0
.ESC
+ '[1;' + (modifiers
+ 1) + 'P';
1482 result
.key
= C0
.ESC
+ 'OP';
1487 result
.key
= C0
.ESC
+ '[1;' + (modifiers
+ 1) + 'Q';
1489 result
.key
= C0
.ESC
+ 'OQ';
1494 result
.key
= C0
.ESC
+ '[1;' + (modifiers
+ 1) + 'R';
1496 result
.key
= C0
.ESC
+ 'OR';
1501 result
.key
= C0
.ESC
+ '[1;' + (modifiers
+ 1) + 'S';
1503 result
.key
= C0
.ESC
+ 'OS';
1508 result
.key
= C0
.ESC
+ '[15;' + (modifiers
+ 1) + '~';
1510 result
.key
= C0
.ESC
+ '[15~';
1515 result
.key
= C0
.ESC
+ '[17;' + (modifiers
+ 1) + '~';
1517 result
.key
= C0
.ESC
+ '[17~';
1522 result
.key
= C0
.ESC
+ '[18;' + (modifiers
+ 1) + '~';
1524 result
.key
= C0
.ESC
+ '[18~';
1529 result
.key
= C0
.ESC
+ '[19;' + (modifiers
+ 1) + '~';
1531 result
.key
= C0
.ESC
+ '[19~';
1536 result
.key
= C0
.ESC
+ '[20;' + (modifiers
+ 1) + '~';
1538 result
.key
= C0
.ESC
+ '[20~';
1543 result
.key
= C0
.ESC
+ '[21;' + (modifiers
+ 1) + '~';
1545 result
.key
= C0
.ESC
+ '[21~';
1550 result
.key
= C0
.ESC
+ '[23;' + (modifiers
+ 1) + '~';
1552 result
.key
= C0
.ESC
+ '[23~';
1557 result
.key
= C0
.ESC
+ '[24;' + (modifiers
+ 1) + '~';
1559 result
.key
= C0
.ESC
+ '[24~';
1564 if (ev
.ctrlKey
&& !ev
.shiftKey
&& !ev
.altKey
&& !ev
.metaKey
) {
1565 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
1566 result
.key
= String
.fromCharCode(ev
.keyCode
- 64);
1567 } else if (ev
.keyCode
=== 32) {
1569 result
.key
= String
.fromCharCode(0);
1570 } else if (ev
.keyCode
>= 51 && ev
.keyCode
<= 55) {
1571 // escape, file sep, group sep, record sep, unit sep
1572 result
.key
= String
.fromCharCode(ev
.keyCode
- 51 + 27);
1573 } else if (ev
.keyCode
=== 56) {
1575 result
.key
= String
.fromCharCode(127);
1576 } else if (ev
.keyCode
=== 219) {
1577 // ^[ - Control Sequence Introducer (CSI)
1578 result
.key
= String
.fromCharCode(27);
1579 } else if (ev
.keyCode
=== 220) {
1580 // ^\ - String Terminator (ST)
1581 result
.key
= String
.fromCharCode(28);
1582 } else if (ev
.keyCode
=== 221) {
1583 // ^] - Operating System Command (OSC)
1584 result
.key
= String
.fromCharCode(29);
1586 } else if (!this.browser
.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) {
1587 // On Mac this is a third level shift. Use <Esc> instead.
1588 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
1589 result
.key
= C0
.ESC
+ String
.fromCharCode(ev
.keyCode
+ 32);
1590 } else if (ev
.keyCode
=== 192) {
1591 result
.key
= C0
.ESC
+ '`';
1592 } else if (ev
.keyCode
>= 48 && ev
.keyCode
<= 57) {
1593 result
.key
= C0
.ESC
+ (ev
.keyCode
- 48);
1603 * Set the G level of the terminal
1606 Terminal
.prototype.setgLevel = function(g
) {
1608 this.charset
= this.charsets
[g
];
1612 * Set the charset for the given G level of the terminal
1616 Terminal
.prototype.setgCharset = function(g
, charset
) {
1617 this.charsets
[g
] = charset
;
1618 if (this.glevel
=== g
) {
1619 this.charset
= charset
;
1624 * Handle a keypress event.
1626 * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
1627 * @param {KeyboardEvent} ev The keypress event to be handled.
1629 Terminal
.prototype.keyPress = function(ev
) {
1636 } else if (ev
.which
== null) {
1638 } else if (ev
.which
!== 0 && ev
.charCode
!== 0) {
1645 (ev
.altKey
|| ev
.ctrlKey
|| ev
.metaKey
) && !isThirdLevelShift(this, ev
)
1650 key
= String
.fromCharCode(key
);
1652 this.emit('keypress', key
, ev
);
1653 this.emit('key', key
, ev
);
1661 * Send data for handling to the terminal
1662 * @param {string} data
1664 Terminal
.prototype.send = function(data
) {
1668 setTimeout(function() {
1669 self
.handler(self
.queue
);
1679 * Note: We could do sweet things with webaudio here
1681 Terminal
.prototype.bell = function() {
1682 if (!this.visualBell
) return;
1684 this.element
.style
.borderColor
= 'white';
1685 setTimeout(function() {
1686 self
.element
.style
.borderColor
= '';
1688 if (this.popOnBell
) this.focus();
1692 * Log the current state to the console.
1694 Terminal
.prototype.log = function() {
1695 if (!this.debug
) return;
1696 if (!this.context
.console
|| !this.context
.console
.log
) return;
1697 var args
= Array
.prototype.slice
.call(arguments
);
1698 this.context
.console
.log
.apply(this.context
.console
, args
);
1702 * Log the current state as error to the console.
1704 Terminal
.prototype.error = function() {
1705 if (!this.debug
) return;
1706 if (!this.context
.console
|| !this.context
.console
.error
) return;
1707 var args
= Array
.prototype.slice
.call(arguments
);
1708 this.context
.console
.error
.apply(this.context
.console
, args
);
1712 * Resizes the terminal.
1714 * @param {number} x The number of columns to resize to.
1715 * @param {number} y The number of rows to resize to.
1717 Terminal
.prototype.resize = function(x
, y
) {
1725 if (x
=== this.cols
&& y
=== this.rows
) {
1735 ch
= [this.defAttr
, ' ', 1]; // does xterm use the default attr?
1736 i
= this.lines
.length
;
1738 while (this.lines
.get(i
).length
< x
) {
1739 this.lines
.get(i
).push(ch
);
1743 i
= this.lines
.length
;
1745 while (this.lines
.get(i
).length
> x
) {
1746 this.lines
.get(i
).pop();
1751 this.setupStops(this.cols
);
1759 // y is rows, not this.y
1760 if (this.lines
.length
< y
+ this.ybase
) {
1761 if (this.ybase
> 0 && this.lines
.length
<= this.ybase
+ this.y
+ addToY
+ 1) {
1762 // There is room above the buffer and there are no empty elements below the line,
1766 if (this.ydisp
> 0) {
1767 // Viewport is at the top of the buffer, must increase downwards
1771 // Add a blank line if there is no buffer left at the top to scroll to, or if there
1772 // are blank lines after the cursor
1773 this.lines
.push(this.blankLine());
1776 if (this.children
.length
< y
) {
1782 if (this.lines
.length
> y
+ this.ybase
) {
1783 if (this.lines
.length
> this.ybase
+ this.y
+ 1) {
1784 // The line is a blank line below the cursor, remove it
1787 // The line is the cursor, scroll down
1792 if (this.children
.length
> y
) {
1793 el
= this.children
.shift();
1795 el
.parentNode
.removeChild(el
);
1801 // Make sure that the cursor stays on screen
1814 this.scrollBottom
= y
- 1;
1816 this.charMeasure
.measure();
1818 this.refresh(0, this.rows
- 1);
1822 this.geometry
= [this.cols
, this.rows
];
1823 this.emit('resize', {terminal
: this, cols
: x
, rows
: y
});
1827 * Updates the range of rows to refresh
1828 * @param {number} y The number of rows to refresh next.
1830 Terminal
.prototype.updateRange = function(y
) {
1831 if (y
< this.refreshStart
) this.refreshStart
= y
;
1832 if (y
> this.refreshEnd
) this.refreshEnd
= y
;
1833 // if (y > this.refreshEnd) {
1834 // this.refreshEnd = y;
1835 // if (y > this.rows - 1) {
1836 // this.refreshEnd = this.rows - 1;
1842 * Set the range of refreshing to the maximum value
1844 Terminal
.prototype.maxRange = function() {
1845 this.refreshStart
= 0;
1846 this.refreshEnd
= this.rows
- 1;
1852 * Setup the tab stops.
1855 Terminal
.prototype.setupStops = function(i
) {
1857 if (!this.tabs
[i
]) {
1858 i
= this.prevStop(i
);
1865 for (; i
< this.cols
; i
+= this.getOption('tabStopWidth')) {
1866 this.tabs
[i
] = true;
1872 * Move the cursor to the previous tab stop from the given position (default is current).
1873 * @param {number} x The position to move the cursor to the previous tab stop.
1875 Terminal
.prototype.prevStop = function(x
) {
1876 if (x
== null) x
= this.x
;
1877 while (!this.tabs
[--x
] && x
> 0);
1878 return x
>= this.cols
1885 * Move the cursor one tab stop forward from the given position (default is current).
1886 * @param {number} x The position to move the cursor one tab stop forward.
1888 Terminal
.prototype.nextStop = function(x
) {
1889 if (x
== null) x
= this.x
;
1890 while (!this.tabs
[++x
] && x
< this.cols
);
1891 return x
>= this.cols
1898 * Erase in the identified line everything from "x" to the end of the line (right).
1899 * @param {number} x The column from which to start erasing to the end of the line.
1900 * @param {number} y The line in which to operate.
1902 Terminal
.prototype.eraseRight = function(x
, y
) {
1903 var line
= this.lines
.get(this.ybase
+ y
);
1907 var ch
= [this.eraseAttr(), ' ', 1]; // xterm
1908 for (; x
< this.cols
; x
++) {
1911 this.updateRange(y
);
1917 * Erase in the identified line everything from "x" to the start of the line (left).
1918 * @param {number} x The column from which to start erasing to the start of the line.
1919 * @param {number} y The line in which to operate.
1921 Terminal
.prototype.eraseLeft = function(x
, y
) {
1922 var line
= this.lines
.get(this.ybase
+ y
);
1926 var ch
= [this.eraseAttr(), ' ', 1]; // xterm
1931 this.updateRange(y
);
1935 * Clears the entire buffer, making the prompt line the new first line.
1937 Terminal
.prototype.clear = function() {
1938 if (this.ybase
=== 0 && this.y
=== 0) {
1939 // Don't clear if it's already clear
1942 this.lines
.set(0, this.lines
.get(this.ybase
+ this.y
));
1943 this.lines
.length
= 1;
1947 for (var i
= 1; i
< this.rows
; i
++) {
1948 this.lines
.push(this.blankLine());
1950 this.refresh(0, this.rows
- 1);
1951 this.emit('scroll', this.ydisp
);
1955 * Erase all content in the given line
1956 * @param {number} y The line to erase all of its contents.
1958 Terminal
.prototype.eraseLine = function(y
) {
1959 this.eraseRight(0, y
);
1964 * Return the data array of a blank line
1965 * @param {number} cur First bunch of data for each "blank" character.
1967 Terminal
.prototype.blankLine = function(cur
) {
1972 var ch
= [attr
, ' ', 1] // width defaults to 1 halfwidth character
1976 for (; i
< this.cols
; i
++) {
1985 * If cur return the back color xterm feature attribute. Else return defAttr.
1986 * @param {object} cur
1988 Terminal
.prototype.ch = function(cur
) {
1990 ? [this.eraseAttr(), ' ', 1]
1991 : [this.defAttr
, ' ', 1];
1996 * Evaluate if the current erminal is the given argument.
1997 * @param {object} term The terminal to evaluate
1999 Terminal
.prototype.is = function(term
) {
2000 var name
= this.termName
;
2001 return (name
+ '').indexOf(term
) === 0;
2006 * Emit the 'data' event and populate the given data.
2007 * @param {string} data The data to populate in the event.
2009 Terminal
.prototype.handler = function(data
) {
2010 // Prevents all events to pty process if stdin is disabled
2011 if (this.options
.disableStdin
) {
2015 // Input is being sent to the terminal, the terminal should focus the prompt.
2016 if (this.ybase
!== this.ydisp
) {
2017 this.scrollToBottom();
2019 this.emit('data', data
);
2024 * Emit the 'title' event and populate the given title.
2025 * @param {string} title The title to populate in the event.
2027 Terminal
.prototype.handleTitle = function(title
) {
2029 * This event is emitted when the title of the terminal is changed
2030 * from inside the terminal. The parameter is the new title.
2034 this.emit('title', title
);
2043 * ESC D Index (IND is 0x84).
2045 Terminal
.prototype.index = function() {
2047 if (this.y
> this.scrollBottom
) {
2051 // If the end of the line is hit, prevent this action from wrapping around to the next line.
2052 if (this.x
>= this.cols
) {
2059 * ESC M Reverse Index (RI is 0x8d).
2061 * Move the cursor up one row, inserting a new blank line if necessary.
2063 Terminal
.prototype.reverseIndex = function() {
2065 if (this.y
=== this.scrollTop
) {
2066 // possibly move the code below to term.reverseScroll();
2067 // test: echo -ne '\e[1;1H\e[44m\eM\e[0m'
2068 // blankLine(true) is xterm/linux behavior
2069 this.lines
.shiftElements(this.y
+ this.ybase
, this.rows
- 1, 1);
2070 this.lines
.set(this.y
+ this.ybase
, this.blankLine(true));
2071 this.updateRange(this.scrollTop
);
2072 this.updateRange(this.scrollBottom
);
2080 * ESC c Full Reset (RIS).
2082 Terminal
.prototype.reset = function() {
2083 this.options
.rows
= this.rows
;
2084 this.options
.cols
= this.cols
;
2085 var customKeydownHandler
= this.customKeydownHandler
;
2086 Terminal
.call(this, this.options
);
2087 this.customKeydownHandler
= customKeydownHandler
;
2088 this.refresh(0, this.rows
- 1);
2089 this.viewport
.syncScrollArea();
2094 * ESC H Tab Set (HTS is 0x88).
2096 Terminal
.prototype.tabSet = function() {
2097 this.tabs
[this.x
] = true;
2104 function on(el
, type
, handler
, capture
) {
2105 if (!Array
.isArray(el
)) {
2108 el
.forEach(function (element
) {
2109 element
.addEventListener(type
, handler
, capture
|| false);
2113 function off(el
, type
, handler
, capture
) {
2114 el
.removeEventListener(type
, handler
, capture
|| false);
2117 function cancel(ev
, force
) {
2118 if (!this.cancelEvents
&& !force
) {
2121 ev
.preventDefault();
2122 ev
.stopPropagation();
2126 function inherits(child
, parent
) {
2128 this.constructor = child
;
2130 f
.prototype = parent
.prototype;
2131 child
.prototype = new f
;
2134 function indexOf(obj
, el
) {
2137 if (obj
[i
] === el
) return i
;
2142 function isThirdLevelShift(term
, ev
) {
2144 (term
.browser
.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) ||
2145 (term
.browser
.isMSWindows
&& ev
.altKey
&& ev
.ctrlKey
&& !ev
.metaKey
);
2147 if (ev
.type
== 'keypress') {
2148 return thirdLevelKey
;
2151 // Don't invoke for arrows, pageDown, home, backspace, etc. (on non-keypress events)
2152 return thirdLevelKey
&& (!ev
.keyCode
|| ev
.keyCode
> 47);
2155 // Expose to InputHandler (temporary)
2156 Terminal
.prototype.matchColor
= matchColor
;
2158 function matchColor(r1
, g1
, b1
) {
2159 var hash
= (r1
<< 16) | (g1
<< 8) | b1
;
2161 if (matchColor
._cache
[hash
] != null) {
2162 return matchColor
._cache
[hash
];
2165 var ldiff
= Infinity
2174 for (; i
< Terminal
.vcolors
.length
; i
++) {
2175 c
= Terminal
.vcolors
[i
];
2180 diff
= matchColor
.distance(r1
, g1
, b1
, r2
, g2
, b2
);
2193 return matchColor
._cache
[hash
] = li
;
2196 matchColor
._cache
= {};
2198 // http://stackoverflow.com/questions/1633828
2199 matchColor
.distance = function(r1
, g1
, b1
, r2
, g2
, b2
) {
2200 return Math
.pow(30 * (r1
- r2
), 2)
2201 + Math
.pow(59 * (g1
- g2
), 2)
2202 + Math
.pow(11 * (b1
- b2
), 2);
2205 function each(obj
, iter
, con
) {
2206 if (obj
.forEach
) return obj
.forEach(iter
, con
);
2207 for (var i
= 0; i
< obj
.length
; i
++) {
2208 iter
.call(con
, obj
[i
], i
, obj
);
2212 function wasMondifierKeyOnlyEvent(ev
) {
2213 return ev
.keyCode
=== 16 || // Shift
2214 ev
.keyCode
=== 17 || // Ctrl
2215 ev
.keyCode
=== 18; // Alt
2218 function keys(obj
) {
2219 if (Object
.keys
) return Object
.keys(obj
);
2222 if (Object
.prototype.hasOwnProperty
.call(obj
, key
)) {
2233 Terminal
.EventEmitter
= EventEmitter
;
2234 Terminal
.inherits
= inherits
;
2237 * Adds an event listener to the terminal.
2239 * @param {string} event The name of the event. TODO: Document all event types
2240 * @param {function} callback The function to call when the event is triggered.
2244 Terminal
.cancel
= cancel
;
2246 module
.exports
= Terminal
;