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.js';
14 import { EventEmitter
} from './EventEmitter.js';
15 import { Viewport
} from './Viewport.js';
16 import { rightClickHandler
, pasteHandler
, copyHandler
} from './handlers/Clipboard.js';
17 import { CircularList
} from './utils/CircularList.js';
18 import * as Browser
from './utils/Browser';
19 import * as Keyboard
from './utils/Keyboard';
22 * Terminal Emulation References:
24 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.txt
25 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
26 * http://invisible-island.net/vttest/
27 * http://www.inwap.com/pdp10/ansicode.txt
28 * http://linux.die.net/man/4/console_codes
29 * http://linux.die.net/man/7/urxvt
32 // Let it work inside Node.js for automated testing purposes.
33 var document
= (typeof window
!= 'undefined') ? window
.document
: null;
38 var normal
= 0, escaped
= 1, csi
= 2, osc
= 3, charset
= 4, dcs
= 5, ignore
= 6;
41 * The amount of write requests to queue before sending an XOFF signal to the
42 * pty process. This number must be small in order for ^C and similar sequences
45 var WRITE_BUFFER_PAUSE_THRESHOLD
= 0;
48 * The maximum number of refresh frames to skip when the write buffer is non-
51 var MAX_REFRESH_FRAME_SKIP
= 6;
58 * Creates a new `Terminal` object.
60 * @param {object} options An object containing a set of options, the available options are:
61 * - `cursorBlink` (boolean): Whether the terminal cursor blinks
62 * - `cols` (number): The number of columns of the terminal (horizontal size)
63 * - `rows` (number): The number of rows of the terminal (vertical size)
67 * @alias module:xterm/src/xterm
69 function Terminal(options
) {
72 if (!(this instanceof Terminal
)) {
73 return new Terminal(arguments
[0], arguments
[1], arguments
[2]);
76 self
.browser
= Browser
;
77 self
.cancel
= Terminal
.cancel
;
79 EventEmitter
.call(this);
81 if (typeof options
=== 'number') {
89 options
= options
|| {};
92 Object
.keys(Terminal
.defaults
).forEach(function(key
) {
93 if (options
[key
] == null) {
94 options
[key
] = Terminal
.options
[key
];
96 if (Terminal
[key
] !== Terminal
.defaults
[key
]) {
97 options
[key
] = Terminal
[key
];
100 self
[key
] = options
[key
];
103 if (options
.colors
.length
=== 8) {
104 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(8));
105 } else if (options
.colors
.length
=== 16) {
106 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(16));
107 } else if (options
.colors
.length
=== 10) {
108 options
.colors
= options
.colors
.slice(0, -2).concat(
109 Terminal
._colors
.slice(8, -2), options
.colors
.slice(-2));
110 } else if (options
.colors
.length
=== 18) {
111 options
.colors
= options
.colors
.concat(
112 Terminal
._colors
.slice(16, -2), options
.colors
.slice(-2));
114 this.colors
= options
.colors
;
116 this.options
= options
;
118 // this.context = options.context || window;
119 // this.document = options.document || document;
120 this.parent
= options
.body
|| options
.parent
|| (
121 document
? document
.getElementsByTagName('body')[0] : null
124 this.cols
= options
.cols
|| options
.geometry
[0];
125 this.rows
= options
.rows
|| options
.geometry
[1];
126 this.geometry
= [this.cols
, this.rows
];
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
153 /** A queue of the rows to be refreshed */
154 this.refreshRowsQueue
= [];
156 this.cursorState
= 0;
157 this.cursorHidden
= false;
162 this.scrollBottom
= this.rows
- 1;
163 this.customKeydownHandler
= null;
166 this.applicationKeypad
= false;
167 this.applicationCursor
= false;
168 this.originMode
= false;
169 this.insertMode
= false;
170 this.wraparoundMode
= true; // defaults: xterm - true, vt100 - false
175 this.gcharset
= null;
177 this.charsets
= [null];
201 this.readable
= true;
202 this.writable
= true;
204 this.defAttr
= (0 << 18) | (257 << 9) | (256 << 0);
205 this.curAttr
= this.defAttr
;
208 this.currentParam
= 0;
213 this.writeBuffer
= [];
214 this.writeInProgress
= false;
215 this.refreshFramesSkipped
= 0;
218 * Whether _xterm.js_ sent XOFF in order to catch up with the pty process.
219 * This is a distinct state from writeStopped so that if the user requested
220 * XOFF via ^S that it will not automatically resume when the writeBuffer goes
223 this.xoffSentToCatchUp
= false;
225 /** Whether writing has been stopped as a result of XOFF */
226 this.writeStopped
= false;
228 // leftover surrogate high from previous write invocation
229 this.surrogate_high
= '';
232 * An array of all lines in the entire buffer, including the prompt. The lines are array of
233 * characters which are 2-length arrays where [0] is an attribute and [1] is the character.
235 this.lines
= new CircularList(this.scrollback
);
238 this.lines
.push(this.blankLine());
244 // Store if user went browsing history in scrollback
245 this.userScrolling
= false;
248 inherits(Terminal
, EventEmitter
);
251 * back_color_erase feature for xterm.
253 Terminal
.prototype.eraseAttr = function() {
254 // if (this.is('screen')) return this.defAttr;
255 return (this.defAttr
& ~0x1ff) | (this.curAttr
& 0x1ff);
263 Terminal
.tangoColors
= [
284 // Colors 0-15 + 16-255
285 // Much thanks to TooTallNate for writing this.
286 Terminal
.colors
= (function() {
287 var colors
= Terminal
.tangoColors
.slice()
288 , r
= [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff]
293 for (; i
< 216; i
++) {
294 out(r
[(i
/ 36) % 6 | 0], r
[(i
/ 6) % 6 | 0], r
[i
% 6]);
299 for (; i
< 24; i
++) {
304 function out(r
, g
, b
) {
305 colors
.push('#' + hex(r
) + hex(g
) + hex(b
));
310 return c
.length
< 2 ? '0' + c
: c
;
316 Terminal
._colors
= Terminal
.colors
.slice();
318 Terminal
.vcolors
= (function() {
320 , colors
= Terminal
.colors
324 for (; i
< 256; i
++) {
325 color
= parseInt(colors
[i
].substring(1), 16);
327 (color
>> 16) & 0xff,
340 Terminal
.defaults
= {
341 colors
: Terminal
.colors
,
353 // programFeatures: false,
357 Terminal
.options
= {};
359 Terminal
.focus
= null;
361 each(keys(Terminal
.defaults
), function(key
) {
362 Terminal
[key
] = Terminal
.defaults
[key
];
363 Terminal
.options
[key
] = Terminal
.defaults
[key
];
367 * Focus the terminal. Delegates focus handling to the terminal's DOM element.
369 Terminal
.prototype.focus = function() {
370 return this.textarea
.focus();
374 * Retrieves an option's value from the terminal.
375 * @param {string} key The option key.
377 Terminal
.prototype.getOption = function(key
, value
) {
378 if (!(key
in Terminal
.defaults
)) {
379 throw new Error('No option with key "' + key
+ '"');
382 if (typeof this.options
[key
] !== 'undefined') {
383 return this.options
[key
];
390 * Sets an option on the terminal.
391 * @param {string} key The option key.
392 * @param {string} value The option value.
394 Terminal
.prototype.setOption = function(key
, value
) {
395 if (!(key
in Terminal
.defaults
)) {
396 throw new Error('No option with key "' + key
+ '"');
399 this.options
[key
] = value
;
403 * Binds the desired focus behavior on a given terminal object.
407 Terminal
.bindFocus = function (term
) {
408 on(term
.textarea
, 'focus', function (ev
) {
409 if (term
.sendFocus
) {
412 term
.element
.classList
.add('focus');
414 Terminal
.focus
= term
;
415 term
.emit('focus', {terminal
: term
});
420 * Blur the terminal. Delegates blur handling to the terminal's DOM element.
422 Terminal
.prototype.blur = function() {
423 return this.textarea
.blur();
427 * Binds the desired blur behavior on a given terminal object.
431 Terminal
.bindBlur = function (term
) {
432 on(term
.textarea
, 'blur', function (ev
) {
433 term
.queueRefresh(term
.y
, term
.y
);
434 if (term
.sendFocus
) {
437 term
.element
.classList
.remove('focus');
438 Terminal
.focus
= null;
439 term
.emit('blur', {terminal
: term
});
444 * Initialize default behavior
446 Terminal
.prototype.initGlobal = function() {
449 Terminal
.bindKeys(this);
450 Terminal
.bindFocus(this);
451 Terminal
.bindBlur(this);
453 // Bind clipboard functionality
454 on(this.element
, 'copy', function (ev
) {
455 copyHandler
.call(this, ev
, term
);
457 on(this.textarea
, 'paste', function (ev
) {
458 pasteHandler
.call(this, ev
, term
);
460 on(this.element
, 'paste', function (ev
) {
461 pasteHandler
.call(this, ev
, term
);
464 function rightClickHandlerWrapper (ev
) {
465 rightClickHandler
.call(this, ev
, term
);
468 if (term
.browser
.isFirefox
) {
469 on(this.element
, 'mousedown', function (ev
) {
470 if (ev
.button
== 2) {
471 rightClickHandlerWrapper(ev
);
475 on(this.element
, 'contextmenu', rightClickHandlerWrapper
);
480 * Apply key handling to the terminal
482 Terminal
.bindKeys = function(term
) {
483 on(term
.element
, 'keydown', function(ev
) {
484 if (document
.activeElement
!= this) {
490 on(term
.element
, 'keypress', function(ev
) {
491 if (document
.activeElement
!= this) {
497 on(term
.element
, 'keyup', term
.focus
.bind(term
));
499 on(term
.textarea
, 'keydown', function(ev
) {
503 on(term
.textarea
, 'keypress', function(ev
) {
505 // Truncate the textarea's value, since it is not needed
509 on(term
.textarea
, 'compositionstart', term
.compositionHelper
.compositionstart
.bind(term
.compositionHelper
));
510 on(term
.textarea
, 'compositionupdate', term
.compositionHelper
.compositionupdate
.bind(term
.compositionHelper
));
511 on(term
.textarea
, 'compositionend', term
.compositionHelper
.compositionend
.bind(term
.compositionHelper
));
512 term
.on('refresh', term
.compositionHelper
.updateCompositionElements
.bind(term
.compositionHelper
));
517 * Insert the given row to the terminal or produce a new one
518 * if no row argument is passed. Return the inserted row.
519 * @param {HTMLElement} row (optional) The row to append to the terminal.
521 Terminal
.prototype.insertRow = function (row
) {
522 if (typeof row
!= 'object') {
523 row
= document
.createElement('div');
526 this.rowContainer
.appendChild(row
);
527 this.children
.push(row
);
533 * Opens the terminal within an element.
535 * @param {HTMLElement} parent The element to create the terminal within.
537 Terminal
.prototype.open = function(parent
) {
538 var self
=this, i
=0, div
;
540 this.parent
= parent
|| this.parent
;
543 throw new Error('Terminal requires a parent element.');
546 // Grab global elements
547 this.context
= this.parent
.ownerDocument
.defaultView
;
548 this.document
= this.parent
.ownerDocument
;
549 this.body
= this.document
.getElementsByTagName('body')[0];
551 //Create main element container
552 this.element
= this.document
.createElement('div');
553 this.element
.classList
.add('terminal');
554 this.element
.classList
.add('xterm');
555 this.element
.classList
.add('xterm-theme-' + this.theme
);
557 this.element
.style
.height
558 this.element
.setAttribute('tabindex', 0);
560 this.viewportElement
= document
.createElement('div');
561 this.viewportElement
.classList
.add('xterm-viewport');
562 this.element
.appendChild(this.viewportElement
);
563 this.viewportScrollArea
= document
.createElement('div');
564 this.viewportScrollArea
.classList
.add('xterm-scroll-area');
565 this.viewportElement
.appendChild(this.viewportScrollArea
);
567 // Create the container that will hold the lines of the terminal and then
568 // produce the lines the lines.
569 this.rowContainer
= document
.createElement('div');
570 this.rowContainer
.classList
.add('xterm-rows');
571 this.element
.appendChild(this.rowContainer
);
574 // Create the container that will hold helpers like the textarea for
575 // capturing DOM Events. Then produce the helpers.
576 this.helperContainer
= document
.createElement('div');
577 this.helperContainer
.classList
.add('xterm-helpers');
578 // TODO: This should probably be inserted once it's filled to prevent an additional layout
579 this.element
.appendChild(this.helperContainer
);
580 this.textarea
= document
.createElement('textarea');
581 this.textarea
.classList
.add('xterm-helper-textarea');
582 this.textarea
.setAttribute('autocorrect', 'off');
583 this.textarea
.setAttribute('autocapitalize', 'off');
584 this.textarea
.setAttribute('spellcheck', 'false');
585 this.textarea
.tabIndex
= 0;
586 this.textarea
.addEventListener('focus', function() {
587 self
.emit('focus', {terminal
: self
});
589 this.textarea
.addEventListener('blur', function() {
590 self
.emit('blur', {terminal
: self
});
592 this.helperContainer
.appendChild(this.textarea
);
594 this.compositionView
= document
.createElement('div');
595 this.compositionView
.classList
.add('composition-view');
596 this.compositionHelper
= new CompositionHelper(this.textarea
, this.compositionView
, this);
597 this.helperContainer
.appendChild(this.compositionView
);
599 this.charMeasureElement
= document
.createElement('div');
600 this.charMeasureElement
.classList
.add('xterm-char-measure-element');
601 this.charMeasureElement
.innerHTML
= 'W';
602 this.helperContainer
.appendChild(this.charMeasureElement
);
604 for (; i
< this.rows
; i
++) {
607 this.parent
.appendChild(this.element
);
609 this.viewport
= new Viewport(this, this.viewportElement
, this.viewportScrollArea
, this.charMeasureElement
);
611 // Setup loop that draws to screen
612 this.queueRefresh(0, this.rows
- 1);
615 // Initialize global actions that
616 // need to be taken on the document.
619 // Ensure there is a Terminal.focus.
622 on(this.element
, 'click', function() {
623 var selection
= document
.getSelection(),
624 collapsed
= selection
.isCollapsed
,
625 isRange
= typeof collapsed
== 'boolean' ? !collapsed
: selection
.type
== 'Range';
631 // Listen for mouse events and translate
632 // them into terminal mouse protocols.
635 // Figure out whether boldness affects
636 // the character width of monospace fonts.
637 if (Terminal
.brokenBold
== null) {
638 Terminal
.brokenBold
= isBoldBroken(this.document
);
642 * This event is emitted when terminal has completed opening.
651 * Attempts to load an add-on using CommonJS or RequireJS (whichever is available).
652 * @param {string} addon The name of the addon to load
655 Terminal
.loadAddon = function(addon
, callback
) {
656 if (typeof exports
=== 'object' && typeof module
=== 'object') {
658 return require('./addons/' + addon
+ '/' + addon
);
659 } else if (typeof define
== 'function') {
661 return require(['./addons/' + addon
+ '/' + addon
], callback
);
663 console
.error('Cannot load a module without a CommonJS or RequireJS environment.');
671 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#Mouse%20Tracking
672 * To better understand these
673 * the xterm code is very helpful:
675 * button.c, charproc.c, misc.c
676 * Relevant functions in xterm/button.c:
677 * BtnCode, EmitButtonCode, EditorButton, SendMousePosition
679 Terminal
.prototype.bindMouse = function() {
680 var el
= this.element
, self
= this, pressed
= 32;
682 // mouseup, mousedown, wheel
683 // left click: ^[[M 3<^[[M#3<
685 function sendButton(ev
) {
689 // get the xterm-style button
690 button
= getButton(ev
);
692 // get mouse coordinates
696 sendEvent(button
, pos
);
698 switch (ev
.overrideType
|| ev
.type
) {
703 // keep it at the left
704 // button, just in case.
715 // motion example of a left click:
716 // ^[[M 3<^[[M@4<^[[M@5<^[[M@6<^[[M@7<^[[M#7<
717 function sendMove(ev
) {
724 // buttons marked as motions
725 // are incremented by 32
728 sendEvent(button
, pos
);
732 // position to characters
733 function encode(data
, ch
) {
734 if (!self
.utfMouse
) {
735 if (ch
=== 255) return data
.push(0);
736 if (ch
> 127) ch
= 127;
739 if (ch
=== 2047) return data
.push(0);
743 if (ch
> 2047) ch
= 2047;
744 data
.push(0xC0 | (ch
>> 6));
745 data
.push(0x80 | (ch
& 0x3F));
750 // send a mouse event:
751 // regular/utf8: ^[[M Cb Cx Cy
752 // urxvt: ^[[ Cb ; Cx ; Cy M
753 // sgr: ^[[ Cb ; Cx ; Cy M/m
754 // vt300: ^[[ 24(1/3/5)~ [ Cx , Cy ] \r
755 // locator: CSI P e ; P b ; P r ; P c ; P p & w
756 function sendEvent(button
, pos
) {
757 // self.emit('mouse', {
763 if (self
.vt300Mouse
) {
765 // http://www.vt100.net/docs/vt3xx-gp/chapter15.html
769 var data
= '\x1b[24';
770 if (button
=== 0) data
+= '1';
771 else if (button
=== 1) data
+= '3';
772 else if (button
=== 2) data
+= '5';
773 else if (button
=== 3) return;
775 data
+= '~[' + pos
.x
+ ',' + pos
.y
+ ']\r';
780 if (self
.decLocator
) {
785 if (button
=== 0) button
= 2;
786 else if (button
=== 1) button
= 4;
787 else if (button
=== 2) button
= 6;
788 else if (button
=== 3) button
= 3;
792 + (button
=== 3 ? 4 : 0)
803 if (self
.urxvtMouse
) {
808 self
.send('\x1b[' + button
+ ';' + pos
.x
+ ';' + pos
.y
+ 'M');
816 + (((button
& 3) === 3 ? button
& ~3 : button
) - 32)
821 + ((button
& 3) === 3 ? 'm' : 'M'));
827 encode(data
, button
);
831 self
.send('\x1b[M' + String
.fromCharCode
.apply(String
, data
));
834 function getButton(ev
) {
847 // 1, and 2 - with 64 added
848 switch (ev
.overrideType
|| ev
.type
) {
850 button
= ev
.button
!= null
856 if (self
.browser
.isMSIE
) {
857 button
= button
=== 1 ? 0 : button
=== 4 ? 1 : button
;
863 case 'DOMMouseScroll':
864 button
= ev
.detail
< 0
869 button
= ev
.wheelDeltaY
> 0
875 // next three bits are the modifiers:
876 // 4 = shift, 8 = meta, 16 = control
877 shift
= ev
.shiftKey
? 4 : 0;
878 meta
= ev
.metaKey
? 8 : 0;
879 ctrl
= ev
.ctrlKey
? 16 : 0;
880 mod
= shift
| meta
| ctrl
;
883 if (self
.vt200Mouse
) {
886 } else if (!self
.normalMouse
) {
891 button
= (32 + (mod
<< 2)) + button
;
896 // mouse coordinates measured in cols/rows
897 function getCoords(ev
) {
900 // ignore browsers without pageX for now
901 if (ev
.pageX
== null) return;
907 // should probably check offsetParent
908 // but this is more portable
909 while (el
&& el
!== self
.document
.documentElement
) {
912 el
= 'offsetParent' in el
917 // convert to cols/rows
918 w
= self
.element
.clientWidth
;
919 h
= self
.element
.clientHeight
;
920 x
= Math
.ceil((x
/ w
) * self
.cols
);
921 y
= Math
.ceil((y
/ h
) * self
.rows
);
923 // be sure to avoid sending
924 // bad positions to the program
926 if (x
> self
.cols
) x
= self
.cols
;
928 if (y
> self
.rows
) y
= self
.rows
;
930 // xterm sends raw bytes and
931 // starts at 32 (SP) for each.
942 on(el
, 'mousedown', function(ev
) {
943 if (!self
.mouseEvents
) return;
952 //if (self.vt200Mouse && !self.normalMouse) {
953 if (self
.vt200Mouse
) {
954 ev
.overrideType
= 'mouseup';
956 return self
.cancel(ev
);
960 if (self
.normalMouse
) on(self
.document
, 'mousemove', sendMove
);
962 // x10 compatibility mode can't send button releases
963 if (!self
.x10Mouse
) {
964 on(self
.document
, 'mouseup', function up(ev
) {
966 if (self
.normalMouse
) off(self
.document
, 'mousemove', sendMove
);
967 off(self
.document
, 'mouseup', up
);
968 return self
.cancel(ev
);
972 return self
.cancel(ev
);
975 //if (self.normalMouse) {
976 // on(self.document, 'mousemove', sendMove);
979 on(el
, 'wheel', function(ev
) {
980 if (!self
.mouseEvents
) return;
983 || self
.decLocator
) return;
985 return self
.cancel(ev
);
988 // allow wheel scrolling in
989 // the shell for example
990 on(el
, 'wheel', function(ev
) {
991 if (self
.mouseEvents
) 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 * Queues a refresh between two rows (inclusive), to be done on next animation
1027 * @param {number} start The start row.
1028 * @param {number} end The end row.
1030 Terminal
.prototype.queueRefresh = function(start
, end
) {
1031 this.refreshRowsQueue
.push({ start
: start
, end
: end
});
1035 * Performs the refresh loop callback, calling refresh only if a refresh is
1036 * necessary before queueing up the next one.
1038 Terminal
.prototype.refreshLoop = function() {
1039 // Don't refresh if there were no row changes
1040 if (this.refreshRowsQueue
.length
> 0) {
1041 // Skip MAX_REFRESH_FRAME_SKIP frames if the writeBuffer is non-empty as it
1042 // will need to be immediately refreshed anyway. This saves a lot of
1043 // rendering time as the viewport DOM does not need to be refreshed, no
1044 // scroll events, no layouts, etc.
1045 var skipFrame
= this.writeBuffer
.length
> 0 && this.refreshFramesSkipped
++ <= MAX_REFRESH_FRAME_SKIP
;
1048 this.refreshFramesSkipped
= 0;
1051 if (this.refreshRowsQueue
.length
> 4) {
1052 // Just do a full refresh when 5+ refreshes are queued
1054 end
= this.rows
- 1;
1056 // Get start and end rows that need refreshing
1057 start
= this.refreshRowsQueue
[0].start
;
1058 end
= this.refreshRowsQueue
[0].end
;
1059 for (var i
= 1; i
< this.refreshRowsQueue
.length
; i
++) {
1060 if (this.refreshRowsQueue
[i
].start
< start
) {
1061 start
= this.refreshRowsQueue
[i
].start
;
1063 if (this.refreshRowsQueue
[i
].end
> end
) {
1064 end
= this.refreshRowsQueue
[i
].end
;
1068 this.refreshRowsQueue
= [];
1069 this.refresh(start
, end
);
1072 window
.requestAnimationFrame(this.refreshLoop
.bind(this));
1076 * Refreshes (re-renders) terminal content within two rows (inclusive)
1080 * In the screen buffer, each character is stored as a an array with a character
1081 * and a 32-bit integer:
1082 * - First value: a utf-16 character.
1084 * - Next 9 bits: background color (0-511).
1085 * - Next 9 bits: foreground color (0-511).
1086 * - Next 14 bits: a mask for misc. flags:
1093 * @param {number} start The row to start from (between 0 and terminal's height terminal - 1)
1094 * @param {number} end The row to end at (between fromRow and terminal's height terminal - 1)
1096 Terminal
.prototype.refresh = function(start
, end
) {
1099 var x
, y
, i
, line
, out
, ch
, ch_width
, width
, data
, attr
, bg
, fg
, flags
, row
, parent
, focused
= document
.activeElement
;
1101 // If this is a big refresh, remove the terminal rows from the DOM for faster calculations
1102 if (end
- start
>= this.rows
/ 2) {
1103 parent
= this.element
.parentNode
;
1105 this.element
.removeChild(this.rowContainer
);
1112 if (end
>= this.rows
.length
) {
1113 this.log('`end` is too large. Most likely a bad CSR.');
1114 end
= this.rows
.length
- 1;
1117 for (; y
<= end
; y
++) {
1118 row
= y
+ this.ydisp
;
1120 line
= this.lines
.get(row
);
1123 if (this.y
=== y
- (this.ybase
- this.ydisp
)
1125 && !this.cursorHidden
) {
1131 attr
= this.defAttr
;
1134 for (; i
< width
; i
++) {
1137 ch_width
= line
[i
][2];
1141 if (i
=== x
) data
= -1;
1143 if (data
!== attr
) {
1144 if (attr
!== this.defAttr
) {
1147 if (data
!== this.defAttr
) {
1149 out
+= '<span class="reverse-video terminal-cursor';
1150 if (this.cursorBlink
) {
1155 var classNames
= [];
1158 fg
= (data
>> 9) & 0x1ff;
1161 if (flags
& Terminal
.flags
.BOLD
) {
1162 if (!Terminal
.brokenBold
) {
1163 classNames
.push('xterm-bold');
1165 // See: XTerm*boldColors
1166 if (fg
< 8) fg
+= 8;
1169 if (flags
& Terminal
.flags
.UNDERLINE
) {
1170 classNames
.push('xterm-underline');
1173 if (flags
& Terminal
.flags
.BLINK
) {
1174 classNames
.push('xterm-blink');
1177 // If inverse flag is on, then swap the foreground and background variables.
1178 if (flags
& Terminal
.flags
.INVERSE
) {
1179 /* One-line variable swap in JavaScript: http://stackoverflow.com/a/16201730 */
1180 bg = [fg, fg = bg][0];
1181 // Should inverse just be before the
1182 // above boldColors effect instead?
1183 if ((flags & 1) && fg < 8) fg += 8;
1186 if (flags & Terminal.flags.INVISIBLE) {
1187 classNames.push('xterm-hidden');
1191 * Weird situation: Invert flag used black foreground and white background results
1192 * in invalid background color, positioned at the 256 index of the 256 terminal
1193 * color map. Pin the colors manually in such a case.
1195 * Source: https://github.com/sourcelair/xterm.js/issues/57
1197 if (flags
& Terminal
.flags
.INVERSE
) {
1207 classNames
.push('xterm-bg-color-' + bg
);
1211 classNames
.push('xterm-color-' + fg
);
1215 if (classNames
.length
) {
1216 out
+= ' class="' + classNames
.join(' ') + '"';
1245 if (attr
!== this.defAttr
) {
1249 this.children
[y
].innerHTML
= out
;
1253 this.element
.appendChild(this.rowContainer
);
1256 this.emit('refresh', {element
: this.element
, start
: start
, end
: end
});
1260 * Display the cursor element
1262 Terminal
.prototype.showCursor = function() {
1263 if (!this.cursorState
) {
1264 this.cursorState
= 1;
1265 this.queueRefresh(this.y
, this.y
);
1270 * Scroll the terminal down 1 row, creating a blank line.
1272 Terminal
.prototype.scroll = function() {
1277 // TODO: Why is this done twice?
1278 if (!this.userScrolling
) {
1279 this.ydisp
= this.ybase
;
1283 row
= this.ybase
+ this.rows
- 1;
1285 // subtract the bottom scroll region
1286 row
-= this.rows
- 1 - this.scrollBottom
;
1288 if (row
=== this.lines
.length
) {
1289 // Compensate ybase and ydisp if lines has hit the maximum buffer size
1290 if (this.lines
.length
=== this.lines
.maxLength
) {
1292 if (this.ydisp
!== 0) {
1296 // Optimization: pushing is faster than splicing when they amount to the same behavior
1297 this.lines
.push(this.blankLine());
1300 this.lines
.splice(row
, 0, this.blankLine());
1303 if (this.scrollTop
!== 0) {
1304 if (this.ybase
!== 0) {
1306 if (!this.userScrolling
) {
1307 this.ydisp
= this.ybase
;
1310 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
1314 this.updateRange(this.scrollTop
);
1315 this.updateRange(this.scrollBottom
);
1318 * This event is emitted whenever the terminal is scrolled.
1319 * The one parameter passed is the new y display position.
1323 this.emit('scroll', this.ydisp
);
1327 * Scroll the display of the terminal
1328 * @param {number} disp The number of lines to scroll down (negatives scroll up).
1329 * @param {boolean} suppressScrollEvent Don't emit the scroll event as scrollDisp. This is used
1330 * to avoid unwanted events being handled by the veiwport when the event was triggered from the
1331 * viewport originally.
1333 Terminal
.prototype.scrollDisp = function(disp
, suppressScrollEvent
) {
1335 this.userScrolling
= true;
1336 } else if (disp
+ this.ydisp
>= this.ybase
) {
1337 this.userScrolling
= false;
1342 if (this.ydisp
> this.ybase
) {
1343 this.ydisp
= this.ybase
;
1344 } else if (this.ydisp
< 0) {
1348 if (!suppressScrollEvent
) {
1349 this.emit('scroll', this.ydisp
);
1352 this.queueRefresh(0, this.rows
- 1);
1356 * Scroll the display of the terminal by a number of pages.
1357 * @param {number} pageCount The number of pages to scroll (negative scrolls up).
1359 Terminal
.prototype.scrollPages = function(pageCount
) {
1360 this.scrollDisp(pageCount
* (this.rows
- 1));
1364 * Scrolls the display of the terminal to the top.
1366 Terminal
.prototype.scrollToTop = function() {
1367 this.scrollDisp(-this.ydisp
);
1371 * Scrolls the display of the terminal to the bottom.
1373 Terminal
.prototype.scrollToBottom = function() {
1374 this.scrollDisp(this.ybase
- this.ydisp
);
1378 * Writes text to the terminal.
1379 * @param {string} text The text to write to the terminal.
1381 Terminal
.prototype.write = function(data
) {
1382 this.writeBuffer
.push(data
);
1384 // Send XOFF to pause the pty process if the write buffer becomes too large so
1385 // xterm.js can catch up before more data is sent. This is necessary in order
1386 // to keep signals such as ^C responsive.
1387 if (!this.xoffSentToCatchUp
&& this.writeBuffer
.length
> WRITE_BUFFER_PAUSE_THRESHOLD
) {
1388 // XOFF - stop pty pipe
1389 // XON will be triggered by emulator before processing data chunk
1391 this.xoffSentToCatchUp
= true;
1394 if (!this.writeInProgress
&& this.writeBuffer
.length
> 0) {
1395 // Kick off a write which will write all data in sequence recursively
1396 this.writeInProgress
= true;
1397 // Kick off an async innerWrite so more writes can come in while processing data
1399 setTimeout(function () {
1400 self
.innerWrite(self
.writeBuffer
.shift());
1405 Terminal
.prototype.innerWrite = function(data
) {
1406 var l
= data
.length
, i
= 0, j
, cs
, ch
, code
, low
, ch_width
, row
;
1408 // If XOFF was sent in order to catch up with the pty process, resume it if
1409 // the writeBuffer is empty to allow more data to come in.
1410 if (this.xoffSentToCatchUp
&& this.writeBuffer
.length
=== 0) {
1412 this.xoffSentToCatchUp
= false;
1415 this.refreshStart
= this.y
;
1416 this.refreshEnd
= this.y
;
1418 // apply leftover surrogate high from last write
1419 if (this.surrogate_high
) {
1420 data
= this.surrogate_high
+ data
;
1421 this.surrogate_high
= '';
1424 for (; i
< l
; i
++) {
1427 // FIXME: higher chars than 0xa0 are not allowed in escape sequences
1428 // --> maybe move to default
1429 code
= data
.charCodeAt(i
);
1430 if (0xD800 <= code
&& code
<= 0xDBFF) {
1431 // we got a surrogate high
1432 // get surrogate low (next 2 bytes)
1433 low
= data
.charCodeAt(i
+1);
1435 // end of data stream, save surrogate high
1436 this.surrogate_high
= ch
;
1439 code
= ((code
- 0xD800) * 0x400) + (low
- 0xDC00) + 0x10000;
1440 ch
+= data
.charAt(i
+1);
1442 // surrogate low - already handled above
1443 if (0xDC00 <= code
&& code
<= 0xDFFF)
1445 switch (this.state
) {
1456 if (this.convertEol
) {
1460 if (this.y
> this.scrollBottom
) {
1480 this.x
= this.nextStop();
1495 this.state
= escaped
;
1500 // calculate print space
1501 // expensive call, therefore we save width in line buffer
1502 ch_width
= wcwidth(code
);
1505 if (this.charset
&& this.charset
[ch
]) {
1506 ch
= this.charset
[ch
];
1509 row
= this.y
+ this.ybase
;
1511 // insert combining char in last cell
1512 // FIXME: needs handling after cursor jumps
1513 if (!ch_width
&& this.x
) {
1514 // dont overflow left
1515 if (this.lines
.get(row
)[this.x
-1]) {
1516 if (!this.lines
.get(row
)[this.x
-1][2]) {
1518 // found empty cell after fullwidth, need to go 2 cells back
1519 if (this.lines
.get(row
)[this.x
-2])
1520 this.lines
.get(row
)[this.x
-2][1] += ch
;
1523 this.lines
.get(row
)[this.x
-1][1] += ch
;
1525 this.updateRange(this.y
);
1530 // goto next line if ch would overflow
1531 // TODO: needs a global min terminal width of 2
1532 if (this.x
+ch_width
-1 >= this.cols
) {
1533 // autowrap - DECAWM
1534 if (this.wraparoundMode
) {
1537 if (this.y
> this.scrollBottom
) {
1542 this.x
= this.cols
-1;
1543 if(ch_width
===2) // FIXME: check for xterm behavior
1547 row
= this.y
+ this.ybase
;
1549 // insert mode: move characters to right
1550 if (this.insertMode
) {
1551 // do this twice for a fullwidth char
1552 for (var moves
=0; moves
<ch_width
; ++moves
) {
1553 // remove last cell, if it's width is 0
1554 // we have to adjust the second last cell as well
1555 var removed
= this.lines
.get(this.y
+ this.ybase
).pop();
1557 && this.lines
.get(row
)[this.cols
-2]
1558 && this.lines
.get(row
)[this.cols
-2][2]===2)
1559 this.lines
.get(row
)[this.cols
-2] = [this.curAttr
, ' ', 1];
1561 // insert empty cell at cursor
1562 this.lines
.get(row
).splice(this.x
, 0, [this.curAttr
, ' ', 1]);
1566 this.lines
.get(row
)[this.x
] = [this.curAttr
, ch
, ch_width
];
1568 this.updateRange(this.y
);
1570 // fullwidth char - set next cell width to zero and advance cursor
1572 this.lines
.get(row
)[this.x
] = [this.curAttr
, '', 0];
1581 // ESC [ Control Sequence Introducer ( CSI is 0x9b).
1584 this.currentParam
= 0;
1588 // ESC ] Operating System Command ( OSC is 0x9d).
1591 this.currentParam
= 0;
1595 // ESC P Device Control String ( DCS is 0x90).
1598 this.currentParam
= 0;
1602 // ESC _ Application Program Command ( APC is 0x9f).
1604 this.state
= ignore
;
1607 // ESC ^ Privacy Message ( PM is 0x9e).
1609 this.state
= ignore
;
1612 // ESC c Full Reset (RIS).
1617 // ESC E Next Line ( NEL is 0x85).
1618 // ESC D Index ( IND is 0x84).
1626 // ESC M Reverse Index ( RI is 0x8d).
1628 this.reverseIndex();
1631 // ESC % Select default/utf-8 character set.
1632 // @ = default, G = utf-8
1634 //this.charset = null;
1636 this.setgCharset(0, Terminal
.charsets
.US
);
1637 this.state
= normal
;
1641 // ESC (,),*,+,-,. Designate G0-G2 Character Set.
1642 case '(': // <-- this seems to get all the attention
1668 this.state
= charset
;
1671 // Designate G3 Character Set (VT300).
1672 // A = ISO Latin-1 Supplemental.
1676 this.state
= charset
;
1681 // Single Shift Select of G2 Character Set
1682 // ( SS2 is 0x8e). This affects next character only.
1686 // Single Shift Select of G3 Character Set
1687 // ( SS3 is 0x8f). This affects next character only.
1691 // Invoke the G2 Character Set as GL (LS2).
1696 // Invoke the G3 Character Set as GL (LS3).
1701 // Invoke the G3 Character Set as GR (LS3R).
1706 // Invoke the G2 Character Set as GR (LS2R).
1711 // Invoke the G1 Character Set as GR (LS1R).
1716 // ESC 7 Save Cursor (DECSC).
1719 this.state
= normal
;
1722 // ESC 8 Restore Cursor (DECRC).
1724 this.restoreCursor();
1725 this.state
= normal
;
1728 // ESC # 3 DEC line height/width
1730 this.state
= normal
;
1734 // ESC H Tab Set (HTS is 0x88).
1739 // ESC = Application Keypad (DECKPAM).
1741 this.log('Serial port requested application keypad.');
1742 this.applicationKeypad
= true;
1743 this.viewport
.syncScrollArea();
1744 this.state
= normal
;
1747 // ESC > Normal Keypad (DECKPNM).
1749 this.log('Switching back to normal keypad.');
1750 this.applicationKeypad
= false;
1751 this.viewport
.syncScrollArea();
1752 this.state
= normal
;
1756 this.state
= normal
;
1757 this.error('Unknown ESC control: %s.', ch
);
1764 case '0': // DEC Special Character and Line Drawing Set.
1765 cs
= Terminal
.charsets
.SCLD
;
1768 cs
= Terminal
.charsets
.UK
;
1770 case 'B': // United States (USASCII).
1771 cs
= Terminal
.charsets
.US
;
1774 cs
= Terminal
.charsets
.Dutch
;
1776 case 'C': // Finnish
1778 cs
= Terminal
.charsets
.Finnish
;
1781 cs
= Terminal
.charsets
.French
;
1783 case 'Q': // FrenchCanadian
1784 cs
= Terminal
.charsets
.FrenchCanadian
;
1787 cs
= Terminal
.charsets
.German
;
1789 case 'Y': // Italian
1790 cs
= Terminal
.charsets
.Italian
;
1792 case 'E': // NorwegianDanish
1794 cs
= Terminal
.charsets
.NorwegianDanish
;
1796 case 'Z': // Spanish
1797 cs
= Terminal
.charsets
.Spanish
;
1799 case 'H': // Swedish
1801 cs
= Terminal
.charsets
.Swedish
;
1804 cs
= Terminal
.charsets
.Swiss
;
1806 case '/': // ISOLatin (actually /A)
1807 cs
= Terminal
.charsets
.ISOLatin
;
1811 cs
= Terminal
.charsets
.US
;
1814 this.setgCharset(this.gcharset
, cs
);
1815 this.gcharset
= null;
1816 this.state
= normal
;
1822 // Set Text Parameters.
1823 if (ch
=== '\x1b' || ch
=== '\x07') {
1824 if (ch
=== '\x1b') i
++;
1826 this.params
.push(this.currentParam
);
1828 switch (this.params
[0]) {
1832 if (this.params
[1]) {
1833 this.title
= this.params
[1];
1834 this.handleTitle(this.title
);
1842 // change dynamic colors
1854 // change dynamic ui colors
1866 // manipulate selection data
1884 this.currentParam
= 0;
1885 this.state
= normal
;
1887 if (!this.params
.length
) {
1888 if (ch
>= '0' && ch
<= '9') {
1890 this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1891 } else if (ch
=== ';') {
1892 this.params
.push(this.currentParam
);
1893 this.currentParam
= '';
1896 this.currentParam
+= ch
;
1903 if (ch
=== '?' || ch
=== '>' || ch
=== '!') {
1909 if (ch
>= '0' && ch
<= '9') {
1910 this.currentParam
= this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1914 // '$', '"', ' ', '\''
1915 if (ch
=== '$' || ch
=== '"' || ch
=== ' ' || ch
=== '\'') {
1920 this.params
.push(this.currentParam
);
1921 this.currentParam
= 0;
1924 if (ch
=== ';') break;
1926 this.state
= normal
;
1930 // Cursor Up Ps Times (default = 1) (CUU).
1932 this.cursorUp(this.params
);
1936 // Cursor Down Ps Times (default = 1) (CUD).
1938 this.cursorDown(this.params
);
1942 // Cursor Forward Ps Times (default = 1) (CUF).
1944 this.cursorForward(this.params
);
1948 // Cursor Backward Ps Times (default = 1) (CUB).
1950 this.cursorBackward(this.params
);
1954 // Cursor Position [row;column] (default = [1,1]) (CUP).
1956 this.cursorPos(this.params
);
1959 // CSI Ps J Erase in Display (ED).
1961 this.eraseInDisplay(this.params
);
1964 // CSI Ps K Erase in Line (EL).
1966 this.eraseInLine(this.params
);
1969 // CSI Pm m Character Attributes (SGR).
1972 this.charAttributes(this.params
);
1976 // CSI Ps n Device Status Report (DSR).
1979 this.deviceStatus(this.params
);
1988 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
1990 this.insertChars(this.params
);
1994 // Cursor Next Line Ps Times (default = 1) (CNL).
1996 this.cursorNextLine(this.params
);
2000 // Cursor Preceding Line Ps Times (default = 1) (CNL).
2002 this.cursorPrecedingLine(this.params
);
2006 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
2008 this.cursorCharAbsolute(this.params
);
2012 // Insert Ps Line(s) (default = 1) (IL).
2014 this.insertLines(this.params
);
2018 // Delete Ps Line(s) (default = 1) (DL).
2020 this.deleteLines(this.params
);
2024 // Delete Ps Character(s) (default = 1) (DCH).
2026 this.deleteChars(this.params
);
2030 // Erase Ps Character(s) (default = 1) (ECH).
2032 this.eraseChars(this.params
);
2035 // CSI Pm ` Character Position Absolute
2036 // [column] (default = [row,1]) (HPA).
2038 this.charPosAbsolute(this.params
);
2042 // Horizontal Position Relative
2044 this.HPositionRelative(this.params
);
2048 // Send Device Attributes (Primary DA).
2050 // Send Device Attributes (Secondary DA)
2052 this.sendDeviceAttributes(this.params
);
2056 // Line Position Absolute [row] (default = [1,column]) (VPA).
2058 this.linePosAbsolute(this.params
);
2061 // 145 65 e * VPR - Vertical Position Relative
2063 this.VPositionRelative(this.params
);
2067 // Horizontal and Vertical Position [row;column] (default =
2070 this.HVPosition(this.params
);
2073 // CSI Pm h Set Mode (SM).
2074 // CSI ? Pm h - mouse escape codes, cursor escape codes
2076 this.setMode(this.params
);
2079 // CSI Pm l Reset Mode (RM).
2082 this.resetMode(this.params
);
2086 // Set Scrolling Region [top;bottom] (default = full size of win-
2090 this.setScrollRegion(this.params
);
2094 // Save cursor (ANSI.SYS).
2096 this.saveCursor(this.params
);
2100 // Restore cursor (ANSI.SYS).
2102 this.restoreCursor(this.params
);
2110 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
2112 this.cursorForwardTab(this.params
);
2115 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
2117 this.scrollUp(this.params
);
2120 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
2121 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
2124 // if (this.prefix === '>') {
2125 // this.resetTitleModes(this.params);
2128 // if (this.params.length > 2) {
2129 // this.initMouseTracking(this.params);
2132 if (this.params
.length
< 2 && !this.prefix
) {
2133 this.scrollDown(this.params
);
2138 // Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
2140 this.cursorBackwardTab(this.params
);
2143 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
2145 this.repeatPrecedingCharacter(this.params
);
2148 // CSI Ps g Tab Clear (TBC).
2150 this.tabClear(this.params
);
2153 // CSI Pm i Media Copy (MC).
2156 // this.mediaCopy(this.params);
2159 // CSI Pm m Character Attributes (SGR).
2161 // case 'm': // duplicate
2162 // if (this.prefix === '>') {
2163 // this.setResources(this.params);
2165 // this.charAttributes(this.params);
2169 // CSI Ps n Device Status Report (DSR).
2171 // case 'n': // duplicate
2172 // if (this.prefix === '>') {
2173 // this.disableModifiers(this.params);
2175 // this.deviceStatus(this.params);
2179 // CSI > Ps p Set pointer mode.
2180 // CSI ! p Soft terminal reset (DECSTR).
2182 // Request ANSI mode (DECRQM).
2184 // Request DEC private mode (DECRQM).
2187 switch (this.prefix
) {
2189 // this.setPointerMode(this.params);
2192 this.softReset(this.params
);
2195 // if (this.postfix === '$') {
2196 // this.requestPrivateMode(this.params);
2200 // if (this.postfix === '"') {
2201 // this.setConformanceLevel(this.params);
2202 // } else if (this.postfix === '$') {
2203 // this.requestAnsiMode(this.params);
2209 // CSI Ps q Load LEDs (DECLL).
2213 // if (this.postfix === ' ') {
2214 // this.setCursorStyle(this.params);
2217 // if (this.postfix === '"') {
2218 // this.setCharProtectionAttr(this.params);
2221 // this.loadLEDs(this.params);
2225 // Set Scrolling Region [top;bottom] (default = full size of win-
2228 // CSI Pt; Pl; Pb; Pr; Ps$ r
2229 // case 'r': // duplicate
2230 // if (this.prefix === '?') {
2231 // this.restorePrivateValues(this.params);
2232 // } else if (this.postfix === '$') {
2233 // this.setAttrInRectangle(this.params);
2235 // this.setScrollRegion(this.params);
2239 // CSI s Save cursor (ANSI.SYS).
2241 // case 's': // duplicate
2242 // if (this.prefix === '?') {
2243 // this.savePrivateValues(this.params);
2245 // this.saveCursor(this.params);
2249 // CSI Ps ; Ps ; Ps t
2250 // CSI Pt; Pl; Pb; Pr; Ps$ t
2254 // if (this.postfix === '$') {
2255 // this.reverseAttrInRectangle(this.params);
2256 // } else if (this.postfix === ' ') {
2257 // this.setWarningBellVolume(this.params);
2259 // if (this.prefix === '>') {
2260 // this.setTitleModeFeature(this.params);
2262 // this.manipulateWindow(this.params);
2267 // CSI u Restore cursor (ANSI.SYS).
2269 // case 'u': // duplicate
2270 // if (this.postfix === ' ') {
2271 // this.setMarginBellVolume(this.params);
2273 // this.restoreCursor(this.params);
2277 // CSI Pt; Pl; Pb; Pr; Pp; Pt; Pl; Pp$ v
2279 // if (this.postfix === '$') {
2280 // this.copyRectagle(this.params);
2284 // CSI Pt ; Pl ; Pb ; Pr ' w
2286 // if (this.postfix === '\'') {
2287 // this.enableFilterRectangle(this.params);
2291 // CSI Ps x Request Terminal Parameters (DECREQTPARM).
2292 // CSI Ps x Select Attribute Change Extent (DECSACE).
2293 // CSI Pc; Pt; Pl; Pb; Pr$ x
2295 // if (this.postfix === '$') {
2296 // this.fillRectangle(this.params);
2298 // this.requestParameters(this.params);
2299 // //this.__(this.params);
2304 // CSI Pt; Pl; Pb; Pr$ z
2306 // if (this.postfix === '\'') {
2307 // this.enableLocatorReporting(this.params);
2308 // } else if (this.postfix === '$') {
2309 // this.eraseRectangle(this.params);
2314 // CSI Pt; Pl; Pb; Pr$ {
2316 // if (this.postfix === '\'') {
2317 // this.setLocatorEvents(this.params);
2318 // } else if (this.postfix === '$') {
2319 // this.selectiveEraseRectangle(this.params);
2325 // if (this.postfix === '\'') {
2326 // this.requestLocatorPosition(this.params);
2331 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
2333 // if (this.postfix === ' ') {
2334 // this.insertColumns(this.params);
2339 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
2341 // if (this.postfix === ' ') {
2342 // this.deleteColumns(this.params);
2347 this.error('Unknown CSI code: %s.', ch
);
2356 if (ch
=== '\x1b' || ch
=== '\x07') {
2357 if (ch
=== '\x1b') i
++;
2359 switch (this.prefix
) {
2360 // User-Defined Keys (DECUDK).
2364 // Request Status String (DECRQSS).
2365 // test: echo -e '\eP$q"p\e\\'
2367 var pt
= this.currentParam
2384 + (this.scrollTop
+ 1)
2386 + (this.scrollBottom
+ 1)
2396 this.error('Unknown DCS Pt: %s.', pt
);
2401 this.send('\x1bP' + +valid
+ '$r' + pt
+ '\x1b\\');
2404 // Set Termcap/Terminfo Data (xterm, experimental).
2408 // Request Termcap/Terminfo String (xterm, experimental)
2409 // Regular xterm does not even respond to this sequence.
2410 // This can cause a small glitch in vim.
2411 // test: echo -ne '\eP+q6b64\e\\'
2413 var pt
= this.currentParam
2416 this.send('\x1bP' + +valid
+ '+r' + pt
+ '\x1b\\');
2420 this.error('Unknown DCS prefix: %s.', this.prefix
);
2424 this.currentParam
= 0;
2426 this.state
= normal
;
2427 } else if (!this.currentParam
) {
2428 if (!this.prefix
&& ch
!== '$' && ch
!== '+') {
2429 this.currentParam
= ch
;
2430 } else if (this.prefix
.length
=== 2) {
2431 this.currentParam
= ch
;
2436 this.currentParam
+= ch
;
2442 if (ch
=== '\x1b' || ch
=== '\x07') {
2443 if (ch
=== '\x1b') i
++;
2444 this.state
= normal
;
2450 this.updateRange(this.y
);
2451 this.queueRefresh(this.refreshStart
, this.refreshEnd
);
2453 if (this.writeBuffer
.length
> 0) {
2456 // TODO: async makes this too slow, need to change to iterative to prevent potential stack overflow
2458 // Start a new async innerWrite to prevent a stack overflow
2459 //setTimeout(function () {
2460 self
.innerWrite(self
.writeBuffer
.shift());
2463 this.writeInProgress
= false;
2468 * Writes text to the terminal, followed by a break line character (\n).
2469 * @param {string} text The text to write to the terminal.
2471 Terminal
.prototype.writeln = function(data
) {
2472 this.write(data
+ '\r\n');
2476 * Attaches a custom keydown handler which is run before keys are processed, giving consumers of
2477 * xterm.js ultimate control as to what keys should be processed by the terminal and what keys
2479 * @param {function} customKeydownHandler The custom KeyboardEvent handler to attach. This is a
2480 * function that takes a KeyboardEvent, allowing consumers to stop propogation and/or prevent
2481 * the default action. The function returns whether the event should be processed by xterm.js.
2483 Terminal
.prototype.attachCustomKeydownHandler = function(customKeydownHandler
) {
2484 this.customKeydownHandler
= customKeydownHandler
;
2488 * Handle a keydown event
2490 * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
2491 * @param {KeyboardEvent} ev The keydown event to be handled.
2493 Terminal
.prototype.keyDown = function(ev
) {
2494 if (this.customKeydownHandler
&& this.customKeydownHandler(ev
) === false) {
2498 if (!this.compositionHelper
.keydown
.bind(this.compositionHelper
)(ev
)) {
2499 if (this.ybase
!== this.ydisp
) {
2500 this.scrollToBottom();
2506 var result
= this.evaluateKeyEscapeSequence(ev
);
2508 if (result
.key
=== '\x13') { // XOFF
2509 this.writeStopped
= true;
2510 } else if (result
.key
=== '\x11') { // XON
2511 this.writeStopped
= false;
2514 if (result
.scrollDisp
) {
2515 this.scrollDisp(result
.scrollDisp
);
2516 return this.cancel(ev
, true);
2519 if (isThirdLevelShift(this, ev
)) {
2523 if (result
.cancel
) {
2524 // The event is canceled at the end already, is this necessary?
2525 this.cancel(ev
, true);
2532 this.emit('keydown', ev
);
2533 this.emit('key', result
.key
, ev
);
2535 this.handler(result
.key
);
2537 return this.cancel(ev
, true);
2541 * Returns an object that determines how a KeyboardEvent should be handled. The key of the
2542 * returned value is the new key code to pass to the PTY.
2544 * Reference: http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
2545 * @param {KeyboardEvent} ev The keyboard event to be translated to key escape sequence.
2547 Terminal
.prototype.evaluateKeyEscapeSequence = function(ev
) {
2549 // Whether to cancel event propogation (NOTE: this may not be needed since the event is
2550 // canceled at the end of keyDown
2552 // The new key even to emit
2554 // The number of characters to scroll, if this is defined it will cancel the event
2555 scrollDisp
: undefined
2557 var modifiers
= ev
.shiftKey
<< 0 | ev
.altKey
<< 1 | ev
.ctrlKey
<< 2 | ev
.metaKey
<< 3;
2558 switch (ev
.keyCode
) {
2562 result
.key
= '\x08'; // ^H
2565 result
.key
= '\x7f'; // ^?
2570 result
.key
= '\x1b[Z';
2574 result
.cancel
= true;
2579 result
.cancel
= true;
2583 result
.key
= '\x1b';
2584 result
.cancel
= true;
2589 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'D';
2590 // HACK: Make Alt + left-arrow behave like Ctrl + left-arrow: move one word backwards
2591 // http://unix.stackexchange.com/a/108106
2592 // macOS uses different escape sequences than linux
2593 if (result
.key
== '\x1b[1;3D') {
2594 result
.key
= (this.browser
.isMac
) ? '\x1bb' : '\x1b[1;5D';
2596 } else if (this.applicationCursor
) {
2597 result
.key
= '\x1bOD';
2599 result
.key
= '\x1b[D';
2605 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'C';
2606 // HACK: Make Alt + right-arrow behave like Ctrl + right-arrow: move one word forward
2607 // http://unix.stackexchange.com/a/108106
2608 // macOS uses different escape sequences than linux
2609 if (result
.key
== '\x1b[1;3C') {
2610 result
.key
= (this.browser
.isMac
) ? '\x1bf' : '\x1b[1;5C';
2612 } else if (this.applicationCursor
) {
2613 result
.key
= '\x1bOC';
2615 result
.key
= '\x1b[C';
2621 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'A';
2622 // HACK: Make Alt + up-arrow behave like Ctrl + up-arrow
2623 // http://unix.stackexchange.com/a/108106
2624 if (result
.key
== '\x1b[1;3A') {
2625 result
.key
= '\x1b[1;5A';
2627 } else if (this.applicationCursor
) {
2628 result
.key
= '\x1bOA';
2630 result
.key
= '\x1b[A';
2636 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'B';
2637 // HACK: Make Alt + down-arrow behave like Ctrl + down-arrow
2638 // http://unix.stackexchange.com/a/108106
2639 if (result
.key
== '\x1b[1;3B') {
2640 result
.key
= '\x1b[1;5B';
2642 } else if (this.applicationCursor
) {
2643 result
.key
= '\x1bOB';
2645 result
.key
= '\x1b[B';
2650 if (!ev
.shiftKey
&& !ev
.ctrlKey
) {
2651 // <Ctrl> or <Shift> + <Insert> are used to
2652 // copy-paste on some systems.
2653 result
.key
= '\x1b[2~';
2659 result
.key
= '\x1b[3;' + (modifiers
+ 1) + '~';
2661 result
.key
= '\x1b[3~';
2667 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'H';
2668 else if (this.applicationCursor
)
2669 result
.key
= '\x1bOH';
2671 result
.key
= '\x1b[H';
2676 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'F';
2677 else if (this.applicationCursor
)
2678 result
.key
= '\x1bOF';
2680 result
.key
= '\x1b[F';
2685 result
.scrollDisp
= -(this.rows
- 1);
2687 result
.key
= '\x1b[5~';
2693 result
.scrollDisp
= this.rows
- 1;
2695 result
.key
= '\x1b[6~';
2701 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'P';
2703 result
.key
= '\x1bOP';
2708 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'Q';
2710 result
.key
= '\x1bOQ';
2715 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'R';
2717 result
.key
= '\x1bOR';
2722 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'S';
2724 result
.key
= '\x1bOS';
2729 result
.key
= '\x1b[15;' + (modifiers
+ 1) + '~';
2731 result
.key
= '\x1b[15~';
2736 result
.key
= '\x1b[17;' + (modifiers
+ 1) + '~';
2738 result
.key
= '\x1b[17~';
2743 result
.key
= '\x1b[18;' + (modifiers
+ 1) + '~';
2745 result
.key
= '\x1b[18~';
2750 result
.key
= '\x1b[19;' + (modifiers
+ 1) + '~';
2752 result
.key
= '\x1b[19~';
2757 result
.key
= '\x1b[20;' + (modifiers
+ 1) + '~';
2759 result
.key
= '\x1b[20~';
2764 result
.key
= '\x1b[21;' + (modifiers
+ 1) + '~';
2766 result
.key
= '\x1b[21~';
2771 result
.key
= '\x1b[23;' + (modifiers
+ 1) + '~';
2773 result
.key
= '\x1b[23~';
2778 result
.key
= '\x1b[24;' + (modifiers
+ 1) + '~';
2780 result
.key
= '\x1b[24~';
2785 if (ev
.ctrlKey
&& !ev
.shiftKey
&& !ev
.altKey
&& !ev
.metaKey
) {
2786 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2787 result
.key
= String
.fromCharCode(ev
.keyCode
- 64);
2788 } else if (ev
.keyCode
=== 32) {
2790 result
.key
= String
.fromCharCode(0);
2791 } else if (ev
.keyCode
>= 51 && ev
.keyCode
<= 55) {
2792 // escape, file sep, group sep, record sep, unit sep
2793 result
.key
= String
.fromCharCode(ev
.keyCode
- 51 + 27);
2794 } else if (ev
.keyCode
=== 56) {
2796 result
.key
= String
.fromCharCode(127);
2797 } else if (ev
.keyCode
=== 219) {
2798 // ^[ - Control Sequence Introducer (CSI)
2799 result
.key
= String
.fromCharCode(27);
2800 } else if (ev
.keyCode
=== 220) {
2801 // ^\ - String Terminator (ST)
2802 result
.key
= String
.fromCharCode(28);
2803 } else if (ev
.keyCode
=== 221) {
2804 // ^] - Operating System Command (OSC)
2805 result
.key
= String
.fromCharCode(29);
2807 } else if (!this.browser
.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) {
2808 // On Mac this is a third level shift. Use <Esc> instead.
2809 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2810 result
.key
= '\x1b' + String
.fromCharCode(ev
.keyCode
+ 32);
2811 } else if (ev
.keyCode
=== 192) {
2812 result
.key
= '\x1b`';
2813 } else if (ev
.keyCode
>= 48 && ev
.keyCode
<= 57) {
2814 result
.key
= '\x1b' + (ev
.keyCode
- 48);
2824 * Set the G level of the terminal
2827 Terminal
.prototype.setgLevel = function(g
) {
2829 this.charset
= this.charsets
[g
];
2833 * Set the charset for the given G level of the terminal
2837 Terminal
.prototype.setgCharset = function(g
, charset
) {
2838 this.charsets
[g
] = charset
;
2839 if (this.glevel
=== g
) {
2840 this.charset
= charset
;
2845 * Handle a keypress event.
2847 * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
2848 * @param {KeyboardEvent} ev The keypress event to be handled.
2850 Terminal
.prototype.keyPress = function(ev
) {
2857 } else if (ev
.which
== null) {
2859 } else if (ev
.which
!== 0 && ev
.charCode
!== 0) {
2866 (ev
.altKey
|| ev
.ctrlKey
|| ev
.metaKey
) && !isThirdLevelShift(this, ev
)
2871 key
= String
.fromCharCode(key
);
2873 this.emit('keypress', key
, ev
);
2874 this.emit('key', key
, ev
);
2882 * Send data for handling to the terminal
2883 * @param {string} data
2885 Terminal
.prototype.send = function(data
) {
2889 setTimeout(function() {
2890 self
.handler(self
.queue
);
2900 * Note: We could do sweet things with webaudio here
2902 Terminal
.prototype.bell = function() {
2903 if (!this.visualBell
) return;
2905 this.element
.style
.borderColor
= 'white';
2906 setTimeout(function() {
2907 self
.element
.style
.borderColor
= '';
2909 if (this.popOnBell
) this.focus();
2913 * Log the current state to the console.
2915 Terminal
.prototype.log = function() {
2916 if (!this.debug
) return;
2917 if (!this.context
.console
|| !this.context
.console
.log
) return;
2918 var args
= Array
.prototype.slice
.call(arguments
);
2919 this.context
.console
.log
.apply(this.context
.console
, args
);
2923 * Log the current state as error to the console.
2925 Terminal
.prototype.error = function() {
2926 if (!this.debug
) return;
2927 if (!this.context
.console
|| !this.context
.console
.error
) return;
2928 var args
= Array
.prototype.slice
.call(arguments
);
2929 this.context
.console
.error
.apply(this.context
.console
, args
);
2933 * Resizes the terminal.
2935 * @param {number} x The number of columns to resize to.
2936 * @param {number} y The number of rows to resize to.
2938 Terminal
.prototype.resize = function(x
, y
) {
2946 if (x
=== this.cols
&& y
=== this.rows
) {
2956 ch
= [this.defAttr
, ' ', 1]; // does xterm use the default attr?
2957 i
= this.lines
.length
;
2959 while (this.lines
.get(i
).length
< x
) {
2960 this.lines
.get(i
).push(ch
);
2964 i
= this.lines
.length
;
2966 while (this.lines
.get(i
).length
> x
) {
2967 this.lines
.get(i
).pop();
2980 // y is rows, not this.y
2981 if (this.lines
.length
< y
+ this.ybase
) {
2982 if (this.ybase
> 0 && this.lines
.length
<= this.ybase
+ this.y
+ addToY
+ 1) {
2983 // There is room above the buffer and there are no empty elements below the line,
2987 if (this.ydisp
> 0) {
2988 // Viewport is at the top of the buffer, must increase downwards
2992 // Add a blank line if there is no buffer left at the top to scroll to, or if there
2993 // are blank lines after the cursor
2994 this.lines
.push(this.blankLine());
2997 if (this.children
.length
< y
) {
3003 if (this.lines
.length
> y
+ this.ybase
) {
3004 if (this.lines
.length
> this.ybase
+ this.y
+ 1) {
3005 // The line is a blank line below the cursor, remove it
3008 // The line is the cursor, scroll down
3013 if (this.children
.length
> y
) {
3014 el
= this.children
.shift();
3016 el
.parentNode
.removeChild(el
);
3022 // Make sure that the cursor stays on screen
3035 this.scrollBottom
= y
- 1;
3037 this.queueRefresh(0, this.rows
- 1);
3041 this.geometry
= [this.cols
, this.rows
];
3042 this.emit('resize', {terminal
: this, cols
: x
, rows
: y
});
3046 * Updates the range of rows to refresh
3047 * @param {number} y The number of rows to refresh next.
3049 Terminal
.prototype.updateRange = function(y
) {
3050 if (y
< this.refreshStart
) this.refreshStart
= y
;
3051 if (y
> this.refreshEnd
) this.refreshEnd
= y
;
3052 // if (y > this.refreshEnd) {
3053 // this.refreshEnd = y;
3054 // if (y > this.rows - 1) {
3055 // this.refreshEnd = this.rows - 1;
3061 * Set the range of refreshing to the maximum value
3063 Terminal
.prototype.maxRange = function() {
3064 this.refreshStart
= 0;
3065 this.refreshEnd
= this.rows
- 1;
3071 * Setup the tab stops.
3074 Terminal
.prototype.setupStops = function(i
) {
3076 if (!this.tabs
[i
]) {
3077 i
= this.prevStop(i
);
3084 for (; i
< this.cols
; i
+= 8) {
3085 this.tabs
[i
] = true;
3091 * Move the cursor to the previous tab stop from the given position (default is current).
3092 * @param {number} x The position to move the cursor to the previous tab stop.
3094 Terminal
.prototype.prevStop = function(x
) {
3095 if (x
== null) x
= this.x
;
3096 while (!this.tabs
[--x
] && x
> 0);
3097 return x
>= this.cols
3104 * Move the cursor one tab stop forward from the given position (default is current).
3105 * @param {number} x The position to move the cursor one tab stop forward.
3107 Terminal
.prototype.nextStop = function(x
) {
3108 if (x
== null) x
= this.x
;
3109 while (!this.tabs
[++x
] && x
< this.cols
);
3110 return x
>= this.cols
3117 * Erase in the identified line everything from "x" to the end of the line (right).
3118 * @param {number} x The column from which to start erasing to the end of the line.
3119 * @param {number} y The line in which to operate.
3121 Terminal
.prototype.eraseRight = function(x
, y
) {
3122 var line
= this.lines
.get(this.ybase
+ y
)
3123 , ch
= [this.eraseAttr(), ' ', 1]; // xterm
3126 for (; x
< this.cols
; x
++) {
3130 this.updateRange(y
);
3136 * Erase in the identified line everything from "x" to the start of the line (left).
3137 * @param {number} x The column from which to start erasing to the start of the line.
3138 * @param {number} y The line in which to operate.
3140 Terminal
.prototype.eraseLeft = function(x
, y
) {
3141 var line
= this.lines
.get(this.ybase
+ y
)
3142 , ch
= [this.eraseAttr(), ' ', 1]; // xterm
3145 while (x
--) line
[x
] = ch
;
3147 this.updateRange(y
);
3151 * Clears the entire buffer, making the prompt line the new first line.
3153 Terminal
.prototype.clear = function() {
3154 if (this.ybase
=== 0 && this.y
=== 0) {
3155 // Don't clear if it's already clear
3158 this.lines
.set(0, this.lines
.get(this.ybase
+ this.y
));
3159 this.lines
.length
= 1;
3163 for (var i
= 1; i
< this.rows
; i
++) {
3164 this.lines
.push(this.blankLine());
3166 this.queueRefresh(0, this.rows
- 1);
3167 this.emit('scroll', this.ydisp
);
3171 * Erase all content in the given line
3172 * @param {number} y The line to erase all of its contents.
3174 Terminal
.prototype.eraseLine = function(y
) {
3175 this.eraseRight(0, y
);
3180 * Return the data array of a blank line
3181 * @param {number} cur First bunch of data for each "blank" character.
3183 Terminal
.prototype.blankLine = function(cur
) {
3188 var ch
= [attr
, ' ', 1] // width defaults to 1 halfwidth character
3192 for (; i
< this.cols
; i
++) {
3201 * If cur return the back color xterm feature attribute. Else return defAttr.
3202 * @param {object} cur
3204 Terminal
.prototype.ch = function(cur
) {
3206 ? [this.eraseAttr(), ' ', 1]
3207 : [this.defAttr
, ' ', 1];
3212 * Evaluate if the current erminal is the given argument.
3213 * @param {object} term The terminal to evaluate
3215 Terminal
.prototype.is = function(term
) {
3216 var name
= this.termName
;
3217 return (name
+ '').indexOf(term
) === 0;
3222 * Emit the 'data' event and populate the given data.
3223 * @param {string} data The data to populate in the event.
3225 Terminal
.prototype.handler = function(data
) {
3226 // Input is being sent to the terminal, the terminal should focus the prompt.
3227 if (this.ybase
!== this.ydisp
) {
3228 this.scrollToBottom();
3230 this.emit('data', data
);
3235 * Emit the 'title' event and populate the given title.
3236 * @param {string} title The title to populate in the event.
3238 Terminal
.prototype.handleTitle = function(title
) {
3240 * This event is emitted when the title of the terminal is changed
3241 * from inside the terminal. The parameter is the new title.
3245 this.emit('title', title
);
3254 * ESC D Index (IND is 0x84).
3256 Terminal
.prototype.index = function() {
3258 if (this.y
> this.scrollBottom
) {
3262 this.state
= normal
;
3267 * ESC M Reverse Index (RI is 0x8d).
3269 * Move the cursor up one row, inserting a new blank line if necessary.
3271 Terminal
.prototype.reverseIndex = function() {
3273 if (this.y
=== this.scrollTop
) {
3274 // possibly move the code below to term.reverseScroll();
3275 // test: echo -ne '\e[1;1H\e[44m\eM\e[0m'
3276 // blankLine(true) is xterm/linux behavior
3277 this.lines
.shiftElements(this.y
+ this.ybase
, this.rows
- 1, 1);
3278 this.lines
.set(this.y
+ this.ybase
, this.blankLine(true));
3279 this.updateRange(this.scrollTop
);
3280 this.updateRange(this.scrollBottom
);
3284 this.state
= normal
;
3289 * ESC c Full Reset (RIS).
3291 Terminal
.prototype.reset = function() {
3292 this.options
.rows
= this.rows
;
3293 this.options
.cols
= this.cols
;
3294 var customKeydownHandler
= this.customKeydownHandler
;
3295 Terminal
.call(this, this.options
);
3296 this.customKeydownHandler
= customKeydownHandler
;
3297 this.queueRefresh(0, this.rows
- 1);
3298 this.viewport
.syncScrollArea();
3303 * ESC H Tab Set (HTS is 0x88).
3305 Terminal
.prototype.tabSet = function() {
3306 this.tabs
[this.x
] = true;
3307 this.state
= normal
;
3317 * Cursor Up Ps Times (default = 1) (CUU).
3319 Terminal
.prototype.cursorUp = function(params
) {
3320 var param
= params
[0];
3321 if (param
< 1) param
= 1;
3323 if (this.y
< 0) this.y
= 0;
3329 * Cursor Down Ps Times (default = 1) (CUD).
3331 Terminal
.prototype.cursorDown = function(params
) {
3332 var param
= params
[0];
3333 if (param
< 1) param
= 1;
3335 if (this.y
>= this.rows
) {
3336 this.y
= this.rows
- 1;
3343 * Cursor Forward Ps Times (default = 1) (CUF).
3345 Terminal
.prototype.cursorForward = function(params
) {
3346 var param
= params
[0];
3347 if (param
< 1) param
= 1;
3349 if (this.x
>= this.cols
) {
3350 this.x
= this.cols
- 1;
3357 * Cursor Backward Ps Times (default = 1) (CUB).
3359 Terminal
.prototype.cursorBackward = function(params
) {
3360 var param
= params
[0];
3361 if (param
< 1) param
= 1;
3363 if (this.x
< 0) this.x
= 0;
3369 * Cursor Position [row;column] (default = [1,1]) (CUP).
3371 Terminal
.prototype.cursorPos = function(params
) {
3374 row
= params
[0] - 1;
3376 if (params
.length
>= 2) {
3377 col
= params
[1] - 1;
3384 } else if (row
>= this.rows
) {
3385 row
= this.rows
- 1;
3390 } else if (col
>= this.cols
) {
3391 col
= this.cols
- 1;
3400 * CSI Ps J Erase in Display (ED).
3401 * Ps = 0 -> Erase Below (default).
3402 * Ps = 1 -> Erase Above.
3403 * Ps = 2 -> Erase All.
3404 * Ps = 3 -> Erase Saved Lines (xterm).
3406 * Erase in Display (DECSED).
3407 * Ps = 0 -> Selective Erase Below (default).
3408 * Ps = 1 -> Selective Erase Above.
3409 * Ps = 2 -> Selective Erase All.
3411 Terminal
.prototype.eraseInDisplay = function(params
) {
3413 switch (params
[0]) {
3415 this.eraseRight(this.x
, this.y
);
3417 for (; j
< this.rows
; j
++) {
3422 this.eraseLeft(this.x
, this.y
);
3430 while (j
--) this.eraseLine(j
);
3440 * CSI Ps K Erase in Line (EL).
3441 * Ps = 0 -> Erase to Right (default).
3442 * Ps = 1 -> Erase to Left.
3443 * Ps = 2 -> Erase All.
3445 * Erase in Line (DECSEL).
3446 * Ps = 0 -> Selective Erase to Right (default).
3447 * Ps = 1 -> Selective Erase to Left.
3448 * Ps = 2 -> Selective Erase All.
3450 Terminal
.prototype.eraseInLine = function(params
) {
3451 switch (params
[0]) {
3453 this.eraseRight(this.x
, this.y
);
3456 this.eraseLeft(this.x
, this.y
);
3459 this.eraseLine(this.y
);
3466 * CSI Pm m Character Attributes (SGR).
3467 * Ps = 0 -> Normal (default).
3469 * Ps = 4 -> Underlined.
3470 * Ps = 5 -> Blink (appears as Bold).
3471 * Ps = 7 -> Inverse.
3472 * Ps = 8 -> Invisible, i.e., hidden (VT300).
3473 * Ps = 2 2 -> Normal (neither bold nor faint).
3474 * Ps = 2 4 -> Not underlined.
3475 * Ps = 2 5 -> Steady (not blinking).
3476 * Ps = 2 7 -> Positive (not inverse).
3477 * Ps = 2 8 -> Visible, i.e., not hidden (VT300).
3478 * Ps = 3 0 -> Set foreground color to Black.
3479 * Ps = 3 1 -> Set foreground color to Red.
3480 * Ps = 3 2 -> Set foreground color to Green.
3481 * Ps = 3 3 -> Set foreground color to Yellow.
3482 * Ps = 3 4 -> Set foreground color to Blue.
3483 * Ps = 3 5 -> Set foreground color to Magenta.
3484 * Ps = 3 6 -> Set foreground color to Cyan.
3485 * Ps = 3 7 -> Set foreground color to White.
3486 * Ps = 3 9 -> Set foreground color to default (original).
3487 * Ps = 4 0 -> Set background color to Black.
3488 * Ps = 4 1 -> Set background color to Red.
3489 * Ps = 4 2 -> Set background color to Green.
3490 * Ps = 4 3 -> Set background color to Yellow.
3491 * Ps = 4 4 -> Set background color to Blue.
3492 * Ps = 4 5 -> Set background color to Magenta.
3493 * Ps = 4 6 -> Set background color to Cyan.
3494 * Ps = 4 7 -> Set background color to White.
3495 * Ps = 4 9 -> Set background color to default (original).
3497 * If 16-color support is compiled, the following apply. Assume
3498 * that xterm's resources are set so that the ISO color codes are
3499 * the first 8 of a set of 16. Then the aixterm colors are the
3500 * bright versions of the ISO colors:
3501 * Ps = 9 0 -> Set foreground color to Black.
3502 * Ps = 9 1 -> Set foreground color to Red.
3503 * Ps = 9 2 -> Set foreground color to Green.
3504 * Ps = 9 3 -> Set foreground color to Yellow.
3505 * Ps = 9 4 -> Set foreground color to Blue.
3506 * Ps = 9 5 -> Set foreground color to Magenta.
3507 * Ps = 9 6 -> Set foreground color to Cyan.
3508 * Ps = 9 7 -> Set foreground color to White.
3509 * Ps = 1 0 0 -> Set background color to Black.
3510 * Ps = 1 0 1 -> Set background color to Red.
3511 * Ps = 1 0 2 -> Set background color to Green.
3512 * Ps = 1 0 3 -> Set background color to Yellow.
3513 * Ps = 1 0 4 -> Set background color to Blue.
3514 * Ps = 1 0 5 -> Set background color to Magenta.
3515 * Ps = 1 0 6 -> Set background color to Cyan.
3516 * Ps = 1 0 7 -> Set background color to White.
3518 * If xterm is compiled with the 16-color support disabled, it
3519 * supports the following, from rxvt:
3520 * Ps = 1 0 0 -> Set foreground and background color to
3523 * If 88- or 256-color support is compiled, the following apply.
3524 * Ps = 3 8 ; 5 ; Ps -> Set foreground color to the second
3526 * Ps = 4 8 ; 5 ; Ps -> Set background color to the second
3529 Terminal
.prototype.charAttributes = function(params
) {
3530 // Optimize a single SGR0.
3531 if (params
.length
=== 1 && params
[0] === 0) {
3532 this.curAttr
= this.defAttr
;
3536 var l
= params
.length
3538 , flags
= this.curAttr
>> 18
3539 , fg
= (this.curAttr
>> 9) & 0x1ff
3540 , bg
= this.curAttr
& 0x1ff
3543 for (; i
< l
; i
++) {
3545 if (p
>= 30 && p
<= 37) {
3548 } else if (p
>= 40 && p
<= 47) {
3551 } else if (p
>= 90 && p
<= 97) {
3555 } else if (p
>= 100 && p
<= 107) {
3559 } else if (p
=== 0) {
3561 flags
= this.defAttr
>> 18;
3562 fg
= (this.defAttr
>> 9) & 0x1ff;
3563 bg
= this.defAttr
& 0x1ff;
3567 } else if (p
=== 1) {
3570 } else if (p
=== 4) {
3573 } else if (p
=== 5) {
3576 } else if (p
=== 7) {
3577 // inverse and positive
3578 // test with: echo -e '\e[31m\e[42mhello\e[7mworld\e[27mhi\e[m'
3580 } else if (p
=== 8) {
3583 } else if (p
=== 22) {
3586 } else if (p
=== 24) {
3589 } else if (p
=== 25) {
3592 } else if (p
=== 27) {
3595 } else if (p
=== 28) {
3598 } else if (p
=== 39) {
3600 fg
= (this.defAttr
>> 9) & 0x1ff;
3601 } else if (p
=== 49) {
3603 bg
= this.defAttr
& 0x1ff;
3604 } else if (p
=== 38) {
3606 if (params
[i
+ 1] === 2) {
3610 params
[i
+ 1] & 0xff,
3611 params
[i
+ 2] & 0xff);
3612 if (fg
=== -1) fg
= 0x1ff;
3614 } else if (params
[i
+ 1] === 5) {
3616 p
= params
[i
] & 0xff;
3619 } else if (p
=== 48) {
3621 if (params
[i
+ 1] === 2) {
3625 params
[i
+ 1] & 0xff,
3626 params
[i
+ 2] & 0xff);
3627 if (bg
=== -1) bg
= 0x1ff;
3629 } else if (params
[i
+ 1] === 5) {
3631 p
= params
[i
] & 0xff;
3634 } else if (p
=== 100) {
3636 fg
= (this.defAttr
>> 9) & 0x1ff;
3637 bg
= this.defAttr
& 0x1ff;
3639 this.error('Unknown SGR attribute: %d.', p
);
3643 this.curAttr
= (flags
<< 18) | (fg
<< 9) | bg
;
3648 * CSI Ps n Device Status Report (DSR).
3649 * Ps = 5 -> Status Report. Result (``OK'') is
3651 * Ps = 6 -> Report Cursor Position (CPR) [row;column].
3655 * Device Status Report (DSR, DEC-specific).
3656 * Ps = 6 -> Report Cursor Position (CPR) [row;column] as CSI
3657 * ? r ; c R (assumes page is zero).
3658 * Ps = 1 5 -> Report Printer status as CSI ? 1 0 n (ready).
3659 * or CSI ? 1 1 n (not ready).
3660 * Ps = 2 5 -> Report UDK status as CSI ? 2 0 n (unlocked)
3661 * or CSI ? 2 1 n (locked).
3662 * Ps = 2 6 -> Report Keyboard status as
3663 * CSI ? 2 7 ; 1 ; 0 ; 0 n (North American).
3664 * The last two parameters apply to VT400 & up, and denote key-
3665 * board ready and LK01 respectively.
3666 * Ps = 5 3 -> Report Locator status as
3667 * CSI ? 5 3 n Locator available, if compiled-in, or
3668 * CSI ? 5 0 n No Locator, if not.
3670 Terminal
.prototype.deviceStatus = function(params
) {
3672 switch (params
[0]) {
3675 this.send('\x1b[0n');
3686 } else if (this.prefix
=== '?') {
3687 // modern xterm doesnt seem to
3688 // respond to any of these except ?6, 6, and 5
3689 switch (params
[0]) {
3700 // this.send('\x1b[?11n');
3703 // dont support user defined keys
3704 // this.send('\x1b[?21n');
3707 // north american keyboard
3708 // this.send('\x1b[?27;1;0;0n');
3711 // no dec locator/mouse
3712 // this.send('\x1b[?50n');
3725 * Insert Ps (Blank) Character(s) (default = 1) (ICH).
3727 Terminal
.prototype.insertChars = function(params
) {
3728 var param
, row
, j
, ch
;
3731 if (param
< 1) param
= 1;
3733 row
= this.y
+ this.ybase
;
3735 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3737 while (param
-- && j
< this.cols
) {
3738 this.lines
.get(row
).splice(j
++, 0, ch
);
3739 this.lines
.get(row
).pop();
3745 * Cursor Next Line Ps Times (default = 1) (CNL).
3746 * same as CSI Ps B ?
3748 Terminal
.prototype.cursorNextLine = function(params
) {
3749 var param
= params
[0];
3750 if (param
< 1) param
= 1;
3752 if (this.y
>= this.rows
) {
3753 this.y
= this.rows
- 1;
3761 * Cursor Preceding Line Ps Times (default = 1) (CNL).
3764 Terminal
.prototype.cursorPrecedingLine = function(params
) {
3765 var param
= params
[0];
3766 if (param
< 1) param
= 1;
3768 if (this.y
< 0) this.y
= 0;
3775 * Cursor Character Absolute [column] (default = [row,1]) (CHA).
3777 Terminal
.prototype.cursorCharAbsolute = function(params
) {
3778 var param
= params
[0];
3779 if (param
< 1) param
= 1;
3786 * Insert Ps Line(s) (default = 1) (IL).
3788 Terminal
.prototype.insertLines = function(params
) {
3792 if (param
< 1) param
= 1;
3793 row
= this.y
+ this.ybase
;
3795 j
= this.rows
- 1 - this.scrollBottom
;
3796 j
= this.rows
- 1 + this.ybase
- j
+ 1;
3799 if (this.lines
.length
=== this.lines
.maxLength
) {
3800 // Trim the start of lines to make room for the new line
3801 this.lines
.trimStart(1);
3807 // test: echo -e '\e[44m\e[1L\e[0m'
3808 // blankLine(true) - xterm/linux behavior
3809 this.lines
.splice(row
, 0, this.blankLine(true));
3810 this.lines
.splice(j
, 1);
3814 this.updateRange(this.y
);
3815 this.updateRange(this.scrollBottom
);
3821 * Delete Ps Line(s) (default = 1) (DL).
3823 Terminal
.prototype.deleteLines = function(params
) {
3827 if (param
< 1) param
= 1;
3828 row
= this.y
+ this.ybase
;
3830 j
= this.rows
- 1 - this.scrollBottom
;
3831 j
= this.rows
- 1 + this.ybase
- j
;
3834 if (this.lines
.length
=== this.lines
.maxLength
) {
3835 // Trim the start of lines to make room for the new line
3836 this.lines
.trimStart(1);
3840 // test: echo -e '\e[44m\e[1M\e[0m'
3841 // blankLine(true) - xterm/linux behavior
3842 this.lines
.splice(j
+ 1, 0, this.blankLine(true));
3843 this.lines
.splice(row
, 1);
3847 this.updateRange(this.y
);
3848 this.updateRange(this.scrollBottom
);
3854 * Delete Ps Character(s) (default = 1) (DCH).
3856 Terminal
.prototype.deleteChars = function(params
) {
3860 if (param
< 1) param
= 1;
3862 row
= this.y
+ this.ybase
;
3863 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3866 this.lines
.get(row
).splice(this.x
, 1);
3867 this.lines
.get(row
).push(ch
);
3873 * Erase Ps Character(s) (default = 1) (ECH).
3875 Terminal
.prototype.eraseChars = function(params
) {
3876 var param
, row
, j
, ch
;
3879 if (param
< 1) param
= 1;
3881 row
= this.y
+ this.ybase
;
3883 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3885 while (param
-- && j
< this.cols
) {
3886 this.lines
.get(row
)[j
++] = ch
;
3891 * CSI Pm ` Character Position Absolute
3892 * [column] (default = [row,1]) (HPA).
3894 Terminal
.prototype.charPosAbsolute = function(params
) {
3895 var param
= params
[0];
3896 if (param
< 1) param
= 1;
3898 if (this.x
>= this.cols
) {
3899 this.x
= this.cols
- 1;
3906 * Horizontal Position Relative
3909 Terminal
.prototype.HPositionRelative = function(params
) {
3910 var param
= params
[0];
3911 if (param
< 1) param
= 1;
3913 if (this.x
>= this.cols
) {
3914 this.x
= this.cols
- 1;
3920 * CSI Ps c Send Device Attributes (Primary DA).
3921 * Ps = 0 or omitted -> request attributes from terminal. The
3922 * response depends on the decTerminalID resource setting.
3923 * -> CSI ? 1 ; 2 c (``VT100 with Advanced Video Option'')
3924 * -> CSI ? 1 ; 0 c (``VT101 with No Options'')
3925 * -> CSI ? 6 c (``VT102'')
3926 * -> CSI ? 6 0 ; 1 ; 2 ; 6 ; 8 ; 9 ; 1 5 ; c (``VT220'')
3927 * The VT100-style response parameters do not mean anything by
3928 * themselves. VT220 parameters do, telling the host what fea-
3929 * tures the terminal supports:
3930 * Ps = 1 -> 132-columns.
3931 * Ps = 2 -> Printer.
3932 * Ps = 6 -> Selective erase.
3933 * Ps = 8 -> User-defined keys.
3934 * Ps = 9 -> National replacement character sets.
3935 * Ps = 1 5 -> Technical characters.
3936 * Ps = 2 2 -> ANSI color, e.g., VT525.
3937 * Ps = 2 9 -> ANSI text locator (i.e., DEC Locator mode).
3939 * Send Device Attributes (Secondary DA).
3940 * Ps = 0 or omitted -> request the terminal's identification
3941 * code. The response depends on the decTerminalID resource set-
3942 * ting. It should apply only to VT220 and up, but xterm extends
3944 * -> CSI > Pp ; Pv ; Pc c
3945 * where Pp denotes the terminal type
3946 * Pp = 0 -> ``VT100''.
3947 * Pp = 1 -> ``VT220''.
3948 * and Pv is the firmware version (for xterm, this was originally
3949 * the XFree86 patch number, starting with 95). In a DEC termi-
3950 * nal, Pc indicates the ROM cartridge registration number and is
3953 * xterm/charproc.c - line 2012, for more information.
3954 * vim responds with ^[[?0c or ^[[?1c after the terminal's response (?)
3956 Terminal
.prototype.sendDeviceAttributes = function(params
) {
3957 if (params
[0] > 0) return;
3960 if (this.is('xterm')
3961 || this.is('rxvt-unicode')
3962 || this.is('screen')) {
3963 this.send('\x1b[?1;2c');
3964 } else if (this.is('linux')) {
3965 this.send('\x1b[?6c');
3967 } else if (this.prefix
=== '>') {
3969 // seem to spit this
3970 // out around ~370 times (?).
3971 if (this.is('xterm')) {
3972 this.send('\x1b[>0;276;0c');
3973 } else if (this.is('rxvt-unicode')) {
3974 this.send('\x1b[>85;95;0c');
3975 } else if (this.is('linux')) {
3976 // not supported by linux console.
3977 // linux console echoes parameters.
3978 this.send(params
[0] + 'c');
3979 } else if (this.is('screen')) {
3980 this.send('\x1b[>83;40003;0c');
3988 * Line Position Absolute [row] (default = [1,column]) (VPA).
3990 Terminal
.prototype.linePosAbsolute = function(params
) {
3991 var param
= params
[0];
3992 if (param
< 1) param
= 1;
3994 if (this.y
>= this.rows
) {
3995 this.y
= this.rows
- 1;
4001 * 145 65 e * VPR - Vertical Position Relative
4004 Terminal
.prototype.VPositionRelative = function(params
) {
4005 var param
= params
[0];
4006 if (param
< 1) param
= 1;
4008 if (this.y
>= this.rows
) {
4009 this.y
= this.rows
- 1;
4016 * Horizontal and Vertical Position [row;column] (default =
4019 Terminal
.prototype.HVPosition = function(params
) {
4020 if (params
[0] < 1) params
[0] = 1;
4021 if (params
[1] < 1) params
[1] = 1;
4023 this.y
= params
[0] - 1;
4024 if (this.y
>= this.rows
) {
4025 this.y
= this.rows
- 1;
4028 this.x
= params
[1] - 1;
4029 if (this.x
>= this.cols
) {
4030 this.x
= this.cols
- 1;
4036 * CSI Pm h Set Mode (SM).
4037 * Ps = 2 -> Keyboard Action Mode (AM).
4038 * Ps = 4 -> Insert Mode (IRM).
4039 * Ps = 1 2 -> Send/receive (SRM).
4040 * Ps = 2 0 -> Automatic Newline (LNM).
4042 * DEC Private Mode Set (DECSET).
4043 * Ps = 1 -> Application Cursor Keys (DECCKM).
4044 * Ps = 2 -> Designate USASCII for character sets G0-G3
4045 * (DECANM), and set VT100 mode.
4046 * Ps = 3 -> 132 Column Mode (DECCOLM).
4047 * Ps = 4 -> Smooth (Slow) Scroll (DECSCLM).
4048 * Ps = 5 -> Reverse Video (DECSCNM).
4049 * Ps = 6 -> Origin Mode (DECOM).
4050 * Ps = 7 -> Wraparound Mode (DECAWM).
4051 * Ps = 8 -> Auto-repeat Keys (DECARM).
4052 * Ps = 9 -> Send Mouse X & Y on button press. See the sec-
4053 * tion Mouse Tracking.
4054 * Ps = 1 0 -> Show toolbar (rxvt).
4055 * Ps = 1 2 -> Start Blinking Cursor (att610).
4056 * Ps = 1 8 -> Print form feed (DECPFF).
4057 * Ps = 1 9 -> Set print extent to full screen (DECPEX).
4058 * Ps = 2 5 -> Show Cursor (DECTCEM).
4059 * Ps = 3 0 -> Show scrollbar (rxvt).
4060 * Ps = 3 5 -> Enable font-shifting functions (rxvt).
4061 * Ps = 3 8 -> Enter Tektronix Mode (DECTEK).
4062 * Ps = 4 0 -> Allow 80 -> 132 Mode.
4063 * Ps = 4 1 -> more(1) fix (see curses resource).
4064 * Ps = 4 2 -> Enable Nation Replacement Character sets (DECN-
4066 * Ps = 4 4 -> Turn On Margin Bell.
4067 * Ps = 4 5 -> Reverse-wraparound Mode.
4068 * Ps = 4 6 -> Start Logging. This is normally disabled by a
4069 * compile-time option.
4070 * Ps = 4 7 -> Use Alternate Screen Buffer. (This may be dis-
4071 * abled by the titeInhibit resource).
4072 * Ps = 6 6 -> Application keypad (DECNKM).
4073 * Ps = 6 7 -> Backarrow key sends backspace (DECBKM).
4074 * Ps = 1 0 0 0 -> Send Mouse X & Y on button press and
4075 * release. See the section Mouse Tracking.
4076 * Ps = 1 0 0 1 -> Use Hilite Mouse Tracking.
4077 * Ps = 1 0 0 2 -> Use Cell Motion Mouse Tracking.
4078 * Ps = 1 0 0 3 -> Use All Motion Mouse Tracking.
4079 * Ps = 1 0 0 4 -> Send FocusIn/FocusOut events.
4080 * Ps = 1 0 0 5 -> Enable Extended Mouse Mode.
4081 * Ps = 1 0 1 0 -> Scroll to bottom on tty output (rxvt).
4082 * Ps = 1 0 1 1 -> Scroll to bottom on key press (rxvt).
4083 * Ps = 1 0 3 4 -> Interpret "meta" key, sets eighth bit.
4084 * (enables the eightBitInput resource).
4085 * Ps = 1 0 3 5 -> Enable special modifiers for Alt and Num-
4086 * Lock keys. (This enables the numLock resource).
4087 * Ps = 1 0 3 6 -> Send ESC when Meta modifies a key. (This
4088 * enables the metaSendsEscape resource).
4089 * Ps = 1 0 3 7 -> Send DEL from the editing-keypad Delete
4091 * Ps = 1 0 3 9 -> Send ESC when Alt modifies a key. (This
4092 * enables the altSendsEscape resource).
4093 * Ps = 1 0 4 0 -> Keep selection even if not highlighted.
4094 * (This enables the keepSelection resource).
4095 * Ps = 1 0 4 1 -> Use the CLIPBOARD selection. (This enables
4096 * the selectToClipboard resource).
4097 * Ps = 1 0 4 2 -> Enable Urgency window manager hint when
4098 * Control-G is received. (This enables the bellIsUrgent
4100 * Ps = 1 0 4 3 -> Enable raising of the window when Control-G
4101 * is received. (enables the popOnBell resource).
4102 * Ps = 1 0 4 7 -> Use Alternate Screen Buffer. (This may be
4103 * disabled by the titeInhibit resource).
4104 * Ps = 1 0 4 8 -> Save cursor as in DECSC. (This may be dis-
4105 * abled by the titeInhibit resource).
4106 * Ps = 1 0 4 9 -> Save cursor as in DECSC and use Alternate
4107 * Screen Buffer, clearing it first. (This may be disabled by
4108 * the titeInhibit resource). This combines the effects of the 1
4109 * 0 4 7 and 1 0 4 8 modes. Use this with terminfo-based
4110 * applications rather than the 4 7 mode.
4111 * Ps = 1 0 5 0 -> Set terminfo/termcap function-key mode.
4112 * Ps = 1 0 5 1 -> Set Sun function-key mode.
4113 * Ps = 1 0 5 2 -> Set HP function-key mode.
4114 * Ps = 1 0 5 3 -> Set SCO function-key mode.
4115 * Ps = 1 0 6 0 -> Set legacy keyboard emulation (X11R6).
4116 * Ps = 1 0 6 1 -> Set VT220 keyboard emulation.
4117 * Ps = 2 0 0 4 -> Set bracketed paste mode.
4119 * http: *vt100.net/docs/vt220-rm/chapter4.html
4121 Terminal
.prototype.setMode = function(params
) {
4122 if (typeof params
=== 'object') {
4123 var l
= params
.length
4126 for (; i
< l
; i
++) {
4127 this.setMode(params
[i
]);
4136 this.insertMode
= true;
4139 //this.convertEol = true;
4142 } else if (this.prefix
=== '?') {
4145 this.applicationCursor
= true;
4148 this.setgCharset(0, Terminal
.charsets
.US
);
4149 this.setgCharset(1, Terminal
.charsets
.US
);
4150 this.setgCharset(2, Terminal
.charsets
.US
);
4151 this.setgCharset(3, Terminal
.charsets
.US
);
4152 // set VT100 mode here
4154 case 3: // 132 col mode
4155 this.savedCols
= this.cols
;
4156 this.resize(132, this.rows
);
4159 this.originMode
= true;
4162 this.wraparoundMode
= true;
4165 // this.cursorBlink = true;
4168 this.log('Serial port requested application keypad.');
4169 this.applicationKeypad
= true;
4170 this.viewport
.syncScrollArea();
4172 case 9: // X10 Mouse
4173 // no release, no motion, no wheel, no modifiers.
4174 case 1000: // vt200 mouse
4176 // no modifiers, except control on the wheel.
4177 case 1002: // button event mouse
4178 case 1003: // any event mouse
4179 // any event - sends motion events,
4180 // even if there is no button held down.
4181 this.x10Mouse
= params
=== 9;
4182 this.vt200Mouse
= params
=== 1000;
4183 this.normalMouse
= params
> 1000;
4184 this.mouseEvents
= true;
4185 this.element
.style
.cursor
= 'default';
4186 this.log('Binding to mouse events.');
4188 case 1004: // send focusin/focusout events
4191 this.sendFocus
= true;
4193 case 1005: // utf8 ext mode mouse
4194 this.utfMouse
= true;
4195 // for wide terminals
4196 // simply encodes large values as utf8 characters
4198 case 1006: // sgr ext mode mouse
4199 this.sgrMouse
= true;
4200 // for wide terminals
4201 // does not add 32 to fields
4202 // press: ^[[<b;x;yM
4203 // release: ^[[<b;x;ym
4205 case 1015: // urxvt ext mode mouse
4206 this.urxvtMouse
= true;
4207 // for wide terminals
4208 // numbers for fields
4210 // motion: ^[[b;x;yT
4212 case 25: // show cursor
4213 this.cursorHidden
= false;
4215 case 1049: // alt screen buffer cursor
4216 //this.saveCursor();
4218 case 47: // alt screen buffer
4219 case 1047: // alt screen buffer
4227 scrollTop
: this.scrollTop
,
4228 scrollBottom
: this.scrollBottom
,
4230 // XXX save charset(s) here?
4231 // charset: this.charset,
4232 // glevel: this.glevel,
4233 // charsets: this.charsets
4236 this.viewport
.syncScrollArea();
4237 this.normal
= normal
;
4246 * CSI Pm l Reset Mode (RM).
4247 * Ps = 2 -> Keyboard Action Mode (AM).
4248 * Ps = 4 -> Replace Mode (IRM).
4249 * Ps = 1 2 -> Send/receive (SRM).
4250 * Ps = 2 0 -> Normal Linefeed (LNM).
4252 * DEC Private Mode Reset (DECRST).
4253 * Ps = 1 -> Normal Cursor Keys (DECCKM).
4254 * Ps = 2 -> Designate VT52 mode (DECANM).
4255 * Ps = 3 -> 80 Column Mode (DECCOLM).
4256 * Ps = 4 -> Jump (Fast) Scroll (DECSCLM).
4257 * Ps = 5 -> Normal Video (DECSCNM).
4258 * Ps = 6 -> Normal Cursor Mode (DECOM).
4259 * Ps = 7 -> No Wraparound Mode (DECAWM).
4260 * Ps = 8 -> No Auto-repeat Keys (DECARM).
4261 * Ps = 9 -> Don't send Mouse X & Y on button press.
4262 * Ps = 1 0 -> Hide toolbar (rxvt).
4263 * Ps = 1 2 -> Stop Blinking Cursor (att610).
4264 * Ps = 1 8 -> Don't print form feed (DECPFF).
4265 * Ps = 1 9 -> Limit print to scrolling region (DECPEX).
4266 * Ps = 2 5 -> Hide Cursor (DECTCEM).
4267 * Ps = 3 0 -> Don't show scrollbar (rxvt).
4268 * Ps = 3 5 -> Disable font-shifting functions (rxvt).
4269 * Ps = 4 0 -> Disallow 80 -> 132 Mode.
4270 * Ps = 4 1 -> No more(1) fix (see curses resource).
4271 * Ps = 4 2 -> Disable Nation Replacement Character sets (DEC-
4273 * Ps = 4 4 -> Turn Off Margin Bell.
4274 * Ps = 4 5 -> No Reverse-wraparound Mode.
4275 * Ps = 4 6 -> Stop Logging. (This is normally disabled by a
4276 * compile-time option).
4277 * Ps = 4 7 -> Use Normal Screen Buffer.
4278 * Ps = 6 6 -> Numeric keypad (DECNKM).
4279 * Ps = 6 7 -> Backarrow key sends delete (DECBKM).
4280 * Ps = 1 0 0 0 -> Don't send Mouse X & Y on button press and
4281 * release. See the section Mouse Tracking.
4282 * Ps = 1 0 0 1 -> Don't use Hilite Mouse Tracking.
4283 * Ps = 1 0 0 2 -> Don't use Cell Motion Mouse Tracking.
4284 * Ps = 1 0 0 3 -> Don't use All Motion Mouse Tracking.
4285 * Ps = 1 0 0 4 -> Don't send FocusIn/FocusOut events.
4286 * Ps = 1 0 0 5 -> Disable Extended Mouse Mode.
4287 * Ps = 1 0 1 0 -> Don't scroll to bottom on tty output
4289 * Ps = 1 0 1 1 -> Don't scroll to bottom on key press (rxvt).
4290 * Ps = 1 0 3 4 -> Don't interpret "meta" key. (This disables
4291 * the eightBitInput resource).
4292 * Ps = 1 0 3 5 -> Disable special modifiers for Alt and Num-
4293 * Lock keys. (This disables the numLock resource).
4294 * Ps = 1 0 3 6 -> Don't send ESC when Meta modifies a key.
4295 * (This disables the metaSendsEscape resource).
4296 * Ps = 1 0 3 7 -> Send VT220 Remove from the editing-keypad
4298 * Ps = 1 0 3 9 -> Don't send ESC when Alt modifies a key.
4299 * (This disables the altSendsEscape resource).
4300 * Ps = 1 0 4 0 -> Do not keep selection when not highlighted.
4301 * (This disables the keepSelection resource).
4302 * Ps = 1 0 4 1 -> Use the PRIMARY selection. (This disables
4303 * the selectToClipboard resource).
4304 * Ps = 1 0 4 2 -> Disable Urgency window manager hint when
4305 * Control-G is received. (This disables the bellIsUrgent
4307 * Ps = 1 0 4 3 -> Disable raising of the window when Control-
4308 * G is received. (This disables the popOnBell resource).
4309 * Ps = 1 0 4 7 -> Use Normal Screen Buffer, clearing screen
4310 * first if in the Alternate Screen. (This may be disabled by
4311 * the titeInhibit resource).
4312 * Ps = 1 0 4 8 -> Restore cursor as in DECRC. (This may be
4313 * disabled by the titeInhibit resource).
4314 * Ps = 1 0 4 9 -> Use Normal Screen Buffer and restore cursor
4315 * as in DECRC. (This may be disabled by the titeInhibit
4316 * resource). This combines the effects of the 1 0 4 7 and 1 0
4317 * 4 8 modes. Use this with terminfo-based applications rather
4318 * than the 4 7 mode.
4319 * Ps = 1 0 5 0 -> Reset terminfo/termcap function-key mode.
4320 * Ps = 1 0 5 1 -> Reset Sun function-key mode.
4321 * Ps = 1 0 5 2 -> Reset HP function-key mode.
4322 * Ps = 1 0 5 3 -> Reset SCO function-key mode.
4323 * Ps = 1 0 6 0 -> Reset legacy keyboard emulation (X11R6).
4324 * Ps = 1 0 6 1 -> Reset keyboard emulation to Sun/PC style.
4325 * Ps = 2 0 0 4 -> Reset bracketed paste mode.
4327 Terminal
.prototype.resetMode = function(params
) {
4328 if (typeof params
=== 'object') {
4329 var l
= params
.length
4332 for (; i
< l
; i
++) {
4333 this.resetMode(params
[i
]);
4342 this.insertMode
= false;
4345 //this.convertEol = false;
4348 } else if (this.prefix
=== '?') {
4351 this.applicationCursor
= false;
4354 if (this.cols
=== 132 && this.savedCols
) {
4355 this.resize(this.savedCols
, this.rows
);
4357 delete this.savedCols
;
4360 this.originMode
= false;
4363 this.wraparoundMode
= false;
4366 // this.cursorBlink = false;
4369 this.log('Switching back to normal keypad.');
4370 this.applicationKeypad
= false;
4371 this.viewport
.syncScrollArea();
4373 case 9: // X10 Mouse
4374 case 1000: // vt200 mouse
4375 case 1002: // button event mouse
4376 case 1003: // any event mouse
4377 this.x10Mouse
= false;
4378 this.vt200Mouse
= false;
4379 this.normalMouse
= false;
4380 this.mouseEvents
= false;
4381 this.element
.style
.cursor
= '';
4383 case 1004: // send focusin/focusout events
4384 this.sendFocus
= false;
4386 case 1005: // utf8 ext mode mouse
4387 this.utfMouse
= false;
4389 case 1006: // sgr ext mode mouse
4390 this.sgrMouse
= false;
4392 case 1015: // urxvt ext mode mouse
4393 this.urxvtMouse
= false;
4395 case 25: // hide cursor
4396 this.cursorHidden
= true;
4398 case 1049: // alt screen buffer cursor
4400 case 47: // normal screen buffer
4401 case 1047: // normal screen buffer - clearing it first
4403 this.lines
= this.normal
.lines
;
4404 this.ybase
= this.normal
.ybase
;
4405 this.ydisp
= this.normal
.ydisp
;
4406 this.x
= this.normal
.x
;
4407 this.y
= this.normal
.y
;
4408 this.scrollTop
= this.normal
.scrollTop
;
4409 this.scrollBottom
= this.normal
.scrollBottom
;
4410 this.tabs
= this.normal
.tabs
;
4412 // if (params === 1049) {
4413 // this.x = this.savedX;
4414 // this.y = this.savedY;
4416 this.queueRefresh(0, this.rows
- 1);
4417 this.viewport
.syncScrollArea();
4428 * Set Scrolling Region [top;bottom] (default = full size of win-
4432 Terminal
.prototype.setScrollRegion = function(params
) {
4433 if (this.prefix
) return;
4434 this.scrollTop
= (params
[0] || 1) - 1;
4435 this.scrollBottom
= (params
[1] || this.rows
) - 1;
4443 * Save cursor (ANSI.SYS).
4445 Terminal
.prototype.saveCursor = function(params
) {
4446 this.savedX
= this.x
;
4447 this.savedY
= this.y
;
4453 * Restore cursor (ANSI.SYS).
4455 Terminal
.prototype.restoreCursor = function(params
) {
4456 this.x
= this.savedX
|| 0;
4457 this.y
= this.savedY
|| 0;
4467 * Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
4469 Terminal
.prototype.cursorForwardTab = function(params
) {
4470 var param
= params
[0] || 1;
4472 this.x
= this.nextStop();
4478 * CSI Ps S Scroll up Ps lines (default = 1) (SU).
4480 Terminal
.prototype.scrollUp = function(params
) {
4481 var param
= params
[0] || 1;
4483 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
4484 this.lines
.splice(this.ybase
+ this.scrollBottom
, 0, this.blankLine());
4487 this.updateRange(this.scrollTop
);
4488 this.updateRange(this.scrollBottom
);
4493 * CSI Ps T Scroll down Ps lines (default = 1) (SD).
4495 Terminal
.prototype.scrollDown = function(params
) {
4496 var param
= params
[0] || 1;
4498 this.lines
.splice(this.ybase
+ this.scrollBottom
, 1);
4499 this.lines
.splice(this.ybase
+ this.scrollTop
, 0, this.blankLine());
4502 this.updateRange(this.scrollTop
);
4503 this.updateRange(this.scrollBottom
);
4508 * CSI Ps ; Ps ; Ps ; Ps ; Ps T
4509 * Initiate highlight mouse tracking. Parameters are
4510 * [func;startx;starty;firstrow;lastrow]. See the section Mouse
4513 Terminal
.prototype.initMouseTracking = function(params
) {
4514 // Relevant: DECSET 1001
4520 * Reset one or more features of the title modes to the default
4521 * value. Normally, "reset" disables the feature. It is possi-
4522 * ble to disable the ability to reset features by compiling a
4523 * different default for the title modes into xterm.
4524 * Ps = 0 -> Do not set window/icon labels using hexadecimal.
4525 * Ps = 1 -> Do not query window/icon labels using hexadeci-
4527 * Ps = 2 -> Do not set window/icon labels using UTF-8.
4528 * Ps = 3 -> Do not query window/icon labels using UTF-8.
4529 * (See discussion of "Title Modes").
4531 Terminal
.prototype.resetTitleModes = function(params
) {
4537 * CSI Ps Z Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
4539 Terminal
.prototype.cursorBackwardTab = function(params
) {
4540 var param
= params
[0] || 1;
4542 this.x
= this.prevStop();
4548 * CSI Ps b Repeat the preceding graphic character Ps times (REP).
4550 Terminal
.prototype.repeatPrecedingCharacter = function(params
) {
4551 var param
= params
[0] || 1
4552 , line
= this.lines
.get(this.ybase
+ this.y
)
4553 , ch
= line
[this.x
- 1] || [this.defAttr
, ' ', 1];
4555 while (param
--) line
[this.x
++] = ch
;
4560 * CSI Ps g Tab Clear (TBC).
4561 * Ps = 0 -> Clear Current Column (default).
4562 * Ps = 3 -> Clear All.
4564 * Ps = 2 -> Clear Stops on Line.
4565 * http://vt100.net/annarbor/aaa-ug/section6.html
4567 Terminal
.prototype.tabClear = function(params
) {
4568 var param
= params
[0];
4570 delete this.tabs
[this.x
];
4571 } else if (param
=== 3) {
4578 * CSI Pm i Media Copy (MC).
4579 * Ps = 0 -> Print screen (default).
4580 * Ps = 4 -> Turn off printer controller mode.
4581 * Ps = 5 -> Turn on printer controller mode.
4583 * Media Copy (MC, DEC-specific).
4584 * Ps = 1 -> Print line containing cursor.
4585 * Ps = 4 -> Turn off autoprint mode.
4586 * Ps = 5 -> Turn on autoprint mode.
4587 * Ps = 1 0 -> Print composed display, ignores DECPEX.
4588 * Ps = 1 1 -> Print all pages.
4590 Terminal
.prototype.mediaCopy = function(params
) {
4597 * Set or reset resource-values used by xterm to decide whether
4598 * to construct escape sequences holding information about the
4599 * modifiers pressed with a given key. The first parameter iden-
4600 * tifies the resource to set/reset. The second parameter is the
4601 * value to assign to the resource. If the second parameter is
4602 * omitted, the resource is reset to its initial value.
4603 * Ps = 1 -> modifyCursorKeys.
4604 * Ps = 2 -> modifyFunctionKeys.
4605 * Ps = 4 -> modifyOtherKeys.
4606 * If no parameters are given, all resources are reset to their
4609 Terminal
.prototype.setResources = function(params
) {
4616 * Disable modifiers which may be enabled via the CSI > Ps; Ps m
4617 * sequence. This corresponds to a resource value of "-1", which
4618 * cannot be set with the other sequence. The parameter identi-
4619 * fies the resource to be disabled:
4620 * Ps = 1 -> modifyCursorKeys.
4621 * Ps = 2 -> modifyFunctionKeys.
4622 * Ps = 4 -> modifyOtherKeys.
4623 * If the parameter is omitted, modifyFunctionKeys is disabled.
4624 * When modifyFunctionKeys is disabled, xterm uses the modifier
4625 * keys to make an extended sequence of functions rather than
4626 * adding a parameter to each function key to denote the modi-
4629 Terminal
.prototype.disableModifiers = function(params
) {
4636 * Set resource value pointerMode. This is used by xterm to
4637 * decide whether to hide the pointer cursor as the user types.
4638 * Valid values for the parameter:
4639 * Ps = 0 -> never hide the pointer.
4640 * Ps = 1 -> hide if the mouse tracking mode is not enabled.
4641 * Ps = 2 -> always hide the pointer. If no parameter is
4642 * given, xterm uses the default, which is 1 .
4644 Terminal
.prototype.setPointerMode = function(params
) {
4650 * CSI ! p Soft terminal reset (DECSTR).
4651 * http://vt100.net/docs/vt220-rm/table4-10.html
4653 Terminal
.prototype.softReset = function(params
) {
4654 this.cursorHidden
= false;
4655 this.insertMode
= false;
4656 this.originMode
= false;
4657 this.wraparoundMode
= false; // autowrap
4658 this.applicationKeypad
= false; // ?
4659 this.viewport
.syncScrollArea();
4660 this.applicationCursor
= false;
4662 this.scrollBottom
= this.rows
- 1;
4663 this.curAttr
= this.defAttr
;
4664 this.x
= this.y
= 0; // ?
4665 this.charset
= null;
4666 this.glevel
= 0; // ??
4667 this.charsets
= [null]; // ??
4673 * Request ANSI mode (DECRQM). For VT300 and up, reply is
4675 * where Ps is the mode number as in RM, and Pm is the mode
4677 * 0 - not recognized
4680 * 3 - permanently set
4681 * 4 - permanently reset
4683 Terminal
.prototype.requestAnsiMode = function(params
) {
4690 * Request DEC private mode (DECRQM). For VT300 and up, reply is
4692 * where Ps is the mode number as in DECSET, Pm is the mode value
4693 * as in the ANSI DECRQM.
4695 Terminal
.prototype.requestPrivateMode = function(params
) {
4702 * Set conformance level (DECSCL). Valid values for the first
4704 * Ps = 6 1 -> VT100.
4705 * Ps = 6 2 -> VT200.
4706 * Ps = 6 3 -> VT300.
4707 * Valid values for the second parameter:
4708 * Ps = 0 -> 8-bit controls.
4709 * Ps = 1 -> 7-bit controls (always set for VT100).
4710 * Ps = 2 -> 8-bit controls.
4712 Terminal
.prototype.setConformanceLevel = function(params
) {
4718 * CSI Ps q Load LEDs (DECLL).
4719 * Ps = 0 -> Clear all LEDS (default).
4720 * Ps = 1 -> Light Num Lock.
4721 * Ps = 2 -> Light Caps Lock.
4722 * Ps = 3 -> Light Scroll Lock.
4723 * Ps = 2 1 -> Extinguish Num Lock.
4724 * Ps = 2 2 -> Extinguish Caps Lock.
4725 * Ps = 2 3 -> Extinguish Scroll Lock.
4727 Terminal
.prototype.loadLEDs = function(params
) {
4734 * Set cursor style (DECSCUSR, VT520).
4735 * Ps = 0 -> blinking block.
4736 * Ps = 1 -> blinking block (default).
4737 * Ps = 2 -> steady block.
4738 * Ps = 3 -> blinking underline.
4739 * Ps = 4 -> steady underline.
4741 Terminal
.prototype.setCursorStyle = function(params
) {
4748 * Select character protection attribute (DECSCA). Valid values
4749 * for the parameter:
4750 * Ps = 0 -> DECSED and DECSEL can erase (default).
4751 * Ps = 1 -> DECSED and DECSEL cannot erase.
4752 * Ps = 2 -> DECSED and DECSEL can erase.
4754 Terminal
.prototype.setCharProtectionAttr = function(params
) {
4761 * Restore DEC Private Mode Values. The value of Ps previously
4762 * saved is restored. Ps values are the same as for DECSET.
4764 Terminal
.prototype.restorePrivateValues = function(params
) {
4770 * CSI Pt; Pl; Pb; Pr; Ps$ r
4771 * Change Attributes in Rectangular Area (DECCARA), VT400 and up.
4772 * Pt; Pl; Pb; Pr denotes the rectangle.
4773 * Ps denotes the SGR attributes to change: 0, 1, 4, 5, 7.
4774 * NOTE: xterm doesn't enable this code by default.
4776 Terminal
.prototype.setAttrInRectangle = function(params
) {
4786 for (; t
< b
+ 1; t
++) {
4787 line
= this.lines
.get(this.ybase
+ t
);
4788 for (i
= l
; i
< r
; i
++) {
4789 line
[i
] = [attr
, line
[i
][1]];
4794 this.updateRange(params
[0]);
4795 this.updateRange(params
[2]);
4800 * CSI Pc; Pt; Pl; Pb; Pr$ x
4801 * Fill Rectangular Area (DECFRA), VT420 and up.
4802 * Pc is the character to use.
4803 * Pt; Pl; Pb; Pr denotes the rectangle.
4804 * NOTE: xterm doesn't enable this code by default.
4806 Terminal
.prototype.fillRectangle = function(params
) {
4816 for (; t
< b
+ 1; t
++) {
4817 line
= this.lines
.get(this.ybase
+ t
);
4818 for (i
= l
; i
< r
; i
++) {
4819 line
[i
] = [line
[i
][0], String
.fromCharCode(ch
)];
4824 this.updateRange(params
[1]);
4825 this.updateRange(params
[3]);
4831 * Enable Locator Reporting (DECELR).
4832 * Valid values for the first parameter:
4833 * Ps = 0 -> Locator disabled (default).
4834 * Ps = 1 -> Locator enabled.
4835 * Ps = 2 -> Locator enabled for one report, then disabled.
4836 * The second parameter specifies the coordinate unit for locator
4838 * Valid values for the second parameter:
4839 * Pu = 0 <- or omitted -> default to character cells.
4840 * Pu = 1 <- device physical pixels.
4841 * Pu = 2 <- character cells.
4843 Terminal
.prototype.enableLocatorReporting = function(params
) {
4844 var val
= params
[0] > 0;
4845 //this.mouseEvents = val;
4846 //this.decLocator = val;
4851 * CSI Pt; Pl; Pb; Pr$ z
4852 * Erase Rectangular Area (DECERA), VT400 and up.
4853 * Pt; Pl; Pb; Pr denotes the rectangle.
4854 * NOTE: xterm doesn't enable this code by default.
4856 Terminal
.prototype.eraseRectangle = function(params
) {
4866 ch
= [this.eraseAttr(), ' ', 1]; // xterm?
4868 for (; t
< b
+ 1; t
++) {
4869 line
= this.lines
.get(this.ybase
+ t
);
4870 for (i
= l
; i
< r
; i
++) {
4876 this.updateRange(params
[0]);
4877 this.updateRange(params
[2]);
4883 * Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
4884 * NOTE: xterm doesn't enable this code by default.
4886 Terminal
.prototype.insertColumns = function() {
4887 var param
= params
[0]
4888 , l
= this.ybase
+ this.rows
4889 , ch
= [this.eraseAttr(), ' ', 1] // xterm?
4893 for (i
= this.ybase
; i
< l
; i
++) {
4894 this.lines
.get(i
).splice(this.x
+ 1, 0, ch
);
4895 this.lines
.get(i
).pop();
4905 * Delete P s Column(s) (default = 1) (DECDC), VT420 and up
4906 * NOTE: xterm doesn't enable this code by default.
4908 Terminal
.prototype.deleteColumns = function() {
4909 var param
= params
[0]
4910 , l
= this.ybase
+ this.rows
4911 , ch
= [this.eraseAttr(), ' ', 1] // xterm?
4915 for (i
= this.ybase
; i
< l
; i
++) {
4916 this.lines
.get(i
).splice(this.x
, 1);
4917 this.lines
.get(i
).push(ch
);
4928 Terminal
.charsets
= {};
4930 // DEC Special Character and Line Drawing Set.
4931 // http://vt100.net/docs/vt102-ug/table5-13.html
4932 // A lot of curses apps use this if they see TERM=xterm.
4933 // testing: echo -e '\e(0a\e(B'
4934 // The xterm output sometimes seems to conflict with the
4935 // reference above. xterm seems in line with the reference
4936 // when running vttest however.
4937 // The table below now uses xterm's output from vttest.
4938 Terminal
.charsets
.SCLD
= { // (0
4939 '`': '\u25c6', // '◆'
4940 'a': '\u2592', // '▒'
4941 'b': '\u0009', // '\t'
4942 'c': '\u000c', // '\f'
4943 'd': '\u000d', // '\r'
4944 'e': '\u000a', // '\n'
4945 'f': '\u00b0', // '°'
4946 'g': '\u00b1', // '±'
4947 'h': '\u2424', // '\u2424' (NL)
4948 'i': '\u000b', // '\v'
4949 'j': '\u2518', // '┘'
4950 'k': '\u2510', // '┐'
4951 'l': '\u250c', // '┌'
4952 'm': '\u2514', // '└'
4953 'n': '\u253c', // '┼'
4954 'o': '\u23ba', // '⎺'
4955 'p': '\u23bb', // '⎻'
4956 'q': '\u2500', // '─'
4957 'r': '\u23bc', // '⎼'
4958 's': '\u23bd', // '⎽'
4959 't': '\u251c', // '├'
4960 'u': '\u2524', // '┤'
4961 'v': '\u2534', // '┴'
4962 'w': '\u252c', // '┬'
4963 'x': '\u2502', // '│'
4964 'y': '\u2264', // '≤'
4965 'z': '\u2265', // '≥'
4966 '{': '\u03c0', // 'π'
4967 '|': '\u2260', // '≠'
4968 '}': '\u00a3', // '£'
4969 '~': '\u00b7' // '·'
4972 Terminal
.charsets
.UK
= null; // (A
4973 Terminal
.charsets
.US
= null; // (B (USASCII)
4974 Terminal
.charsets
.Dutch
= null; // (4
4975 Terminal
.charsets
.Finnish
= null; // (C or (5
4976 Terminal
.charsets
.French
= null; // (R
4977 Terminal
.charsets
.FrenchCanadian
= null; // (Q
4978 Terminal
.charsets
.German
= null; // (K
4979 Terminal
.charsets
.Italian
= null; // (Y
4980 Terminal
.charsets
.NorwegianDanish
= null; // (E or (6
4981 Terminal
.charsets
.Spanish
= null; // (Z
4982 Terminal
.charsets
.Swedish
= null; // (H or (7
4983 Terminal
.charsets
.Swiss
= null; // (=
4984 Terminal
.charsets
.ISOLatin
= null; // /A
4990 function on(el
, type
, handler
, capture
) {
4991 if (!Array
.isArray(el
)) {
4994 el
.forEach(function (element
) {
4995 element
.addEventListener(type
, handler
, capture
|| false);
4999 function off(el
, type
, handler
, capture
) {
5000 el
.removeEventListener(type
, handler
, capture
|| false);
5003 function cancel(ev
, force
) {
5004 if (!this.cancelEvents
&& !force
) {
5007 ev
.preventDefault();
5008 ev
.stopPropagation();
5012 function inherits(child
, parent
) {
5014 this.constructor = child
;
5016 f
.prototype = parent
.prototype;
5017 child
.prototype = new f
;
5020 // if bold is broken, we can't
5021 // use it in the terminal.
5022 function isBoldBroken(document
) {
5023 var body
= document
.getElementsByTagName('body')[0];
5024 var el
= document
.createElement('span');
5025 el
.innerHTML
= 'hello world';
5026 body
.appendChild(el
);
5027 var w1
= el
.scrollWidth
;
5028 el
.style
.fontWeight
= 'bold';
5029 var w2
= el
.scrollWidth
;
5030 body
.removeChild(el
);
5034 function indexOf(obj
, el
) {
5037 if (obj
[i
] === el
) return i
;
5042 function isThirdLevelShift(term
, ev
) {
5044 (term
.browser
.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) ||
5045 (term
.browser
.isMSWindows
&& ev
.altKey
&& ev
.ctrlKey
&& !ev
.metaKey
);
5047 if (ev
.type
== 'keypress') {
5048 return thirdLevelKey
;
5051 // Don't invoke for arrows, pageDown, home, backspace, etc. (on non-keypress events)
5052 return thirdLevelKey
&& (!ev
.keyCode
|| ev
.keyCode
> 47);
5055 function matchColor(r1
, g1
, b1
) {
5056 var hash
= (r1
<< 16) | (g1
<< 8) | b1
;
5058 if (matchColor
._cache
[hash
] != null) {
5059 return matchColor
._cache
[hash
];
5062 var ldiff
= Infinity
5071 for (; i
< Terminal
.vcolors
.length
; i
++) {
5072 c
= Terminal
.vcolors
[i
];
5077 diff
= matchColor
.distance(r1
, g1
, b1
, r2
, g2
, b2
);
5090 return matchColor
._cache
[hash
] = li
;
5093 matchColor
._cache
= {};
5095 // http://stackoverflow.com/questions/1633828
5096 matchColor
.distance = function(r1
, g1
, b1
, r2
, g2
, b2
) {
5097 return Math
.pow(30 * (r1
- r2
), 2)
5098 + Math
.pow(59 * (g1
- g2
), 2)
5099 + Math
.pow(11 * (b1
- b2
), 2);
5102 function each(obj
, iter
, con
) {
5103 if (obj
.forEach
) return obj
.forEach(iter
, con
);
5104 for (var i
= 0; i
< obj
.length
; i
++) {
5105 iter
.call(con
, obj
[i
], i
, obj
);
5109 function keys(obj
) {
5110 if (Object
.keys
) return Object
.keys(obj
);
5113 if (Object
.prototype.hasOwnProperty
.call(obj
, key
)) {
5120 var wcwidth
= (function(opts
) {
5121 // extracted from https://www.cl.cam.ac.uk/%7Emgk25/ucs/wcwidth.c
5122 // combining characters
5124 [0x0300, 0x036F], [0x0483, 0x0486], [0x0488, 0x0489],
5125 [0x0591, 0x05BD], [0x05BF, 0x05BF], [0x05C1, 0x05C2],
5126 [0x05C4, 0x05C5], [0x05C7, 0x05C7], [0x0600, 0x0603],
5127 [0x0610, 0x0615], [0x064B, 0x065E], [0x0670, 0x0670],
5128 [0x06D6, 0x06E4], [0x06E7, 0x06E8], [0x06EA, 0x06ED],
5129 [0x070F, 0x070F], [0x0711, 0x0711], [0x0730, 0x074A],
5130 [0x07A6, 0x07B0], [0x07EB, 0x07F3], [0x0901, 0x0902],
5131 [0x093C, 0x093C], [0x0941, 0x0948], [0x094D, 0x094D],
5132 [0x0951, 0x0954], [0x0962, 0x0963], [0x0981, 0x0981],
5133 [0x09BC, 0x09BC], [0x09C1, 0x09C4], [0x09CD, 0x09CD],
5134 [0x09E2, 0x09E3], [0x0A01, 0x0A02], [0x0A3C, 0x0A3C],
5135 [0x0A41, 0x0A42], [0x0A47, 0x0A48], [0x0A4B, 0x0A4D],
5136 [0x0A70, 0x0A71], [0x0A81, 0x0A82], [0x0ABC, 0x0ABC],
5137 [0x0AC1, 0x0AC5], [0x0AC7, 0x0AC8], [0x0ACD, 0x0ACD],
5138 [0x0AE2, 0x0AE3], [0x0B01, 0x0B01], [0x0B3C, 0x0B3C],
5139 [0x0B3F, 0x0B3F], [0x0B41, 0x0B43], [0x0B4D, 0x0B4D],
5140 [0x0B56, 0x0B56], [0x0B82, 0x0B82], [0x0BC0, 0x0BC0],
5141 [0x0BCD, 0x0BCD], [0x0C3E, 0x0C40], [0x0C46, 0x0C48],
5142 [0x0C4A, 0x0C4D], [0x0C55, 0x0C56], [0x0CBC, 0x0CBC],
5143 [0x0CBF, 0x0CBF], [0x0CC6, 0x0CC6], [0x0CCC, 0x0CCD],
5144 [0x0CE2, 0x0CE3], [0x0D41, 0x0D43], [0x0D4D, 0x0D4D],
5145 [0x0DCA, 0x0DCA], [0x0DD2, 0x0DD4], [0x0DD6, 0x0DD6],
5146 [0x0E31, 0x0E31], [0x0E34, 0x0E3A], [0x0E47, 0x0E4E],
5147 [0x0EB1, 0x0EB1], [0x0EB4, 0x0EB9], [0x0EBB, 0x0EBC],
5148 [0x0EC8, 0x0ECD], [0x0F18, 0x0F19], [0x0F35, 0x0F35],
5149 [0x0F37, 0x0F37], [0x0F39, 0x0F39], [0x0F71, 0x0F7E],
5150 [0x0F80, 0x0F84], [0x0F86, 0x0F87], [0x0F90, 0x0F97],
5151 [0x0F99, 0x0FBC], [0x0FC6, 0x0FC6], [0x102D, 0x1030],
5152 [0x1032, 0x1032], [0x1036, 0x1037], [0x1039, 0x1039],
5153 [0x1058, 0x1059], [0x1160, 0x11FF], [0x135F, 0x135F],
5154 [0x1712, 0x1714], [0x1732, 0x1734], [0x1752, 0x1753],
5155 [0x1772, 0x1773], [0x17B4, 0x17B5], [0x17B7, 0x17BD],
5156 [0x17C6, 0x17C6], [0x17C9, 0x17D3], [0x17DD, 0x17DD],
5157 [0x180B, 0x180D], [0x18A9, 0x18A9], [0x1920, 0x1922],
5158 [0x1927, 0x1928], [0x1932, 0x1932], [0x1939, 0x193B],
5159 [0x1A17, 0x1A18], [0x1B00, 0x1B03], [0x1B34, 0x1B34],
5160 [0x1B36, 0x1B3A], [0x1B3C, 0x1B3C], [0x1B42, 0x1B42],
5161 [0x1B6B, 0x1B73], [0x1DC0, 0x1DCA], [0x1DFE, 0x1DFF],
5162 [0x200B, 0x200F], [0x202A, 0x202E], [0x2060, 0x2063],
5163 [0x206A, 0x206F], [0x20D0, 0x20EF], [0x302A, 0x302F],
5164 [0x3099, 0x309A], [0xA806, 0xA806], [0xA80B, 0xA80B],
5165 [0xA825, 0xA826], [0xFB1E, 0xFB1E], [0xFE00, 0xFE0F],
5166 [0xFE20, 0xFE23], [0xFEFF, 0xFEFF], [0xFFF9, 0xFFFB],
5167 [0x10A01, 0x10A03], [0x10A05, 0x10A06], [0x10A0C, 0x10A0F],
5168 [0x10A38, 0x10A3A], [0x10A3F, 0x10A3F], [0x1D167, 0x1D169],
5169 [0x1D173, 0x1D182], [0x1D185, 0x1D18B], [0x1D1AA, 0x1D1AD],
5170 [0x1D242, 0x1D244], [0xE0001, 0xE0001], [0xE0020, 0xE007F],
5174 function bisearch(ucs
) {
5176 var max
= COMBINING
.length
- 1;
5178 if (ucs
< COMBINING
[0][0] || ucs
> COMBINING
[max
][1])
5180 while (max
>= min
) {
5181 mid
= Math
.floor((min
+ max
) / 2);
5182 if (ucs
> COMBINING
[mid
][1])
5184 else if (ucs
< COMBINING
[mid
][0])
5191 function wcwidth(ucs
) {
5192 // test for 8-bit control characters
5195 if (ucs
< 32 || (ucs
>= 0x7f && ucs
< 0xa0))
5196 return opts
.control
;
5197 // binary search in table of non-spacing characters
5200 // if we arrive here, ucs is not a combining or C0/C1 control character
5205 ucs
<= 0x115f || // Hangul Jamo init. consonants
5208 (ucs
>= 0x2e80 && ucs
<= 0xa4cf && ucs
!= 0x303f) || // CJK..Yi
5209 (ucs
>= 0xac00 && ucs
<= 0xd7a3) || // Hangul Syllables
5210 (ucs
>= 0xf900 && ucs
<= 0xfaff) || // CJK Compat Ideographs
5211 (ucs
>= 0xfe10 && ucs
<= 0xfe19) || // Vertical forms
5212 (ucs
>= 0xfe30 && ucs
<= 0xfe6f) || // CJK Compat Forms
5213 (ucs
>= 0xff00 && ucs
<= 0xff60) || // Fullwidth Forms
5214 (ucs
>= 0xffe0 && ucs
<= 0xffe6) ||
5215 (ucs
>= 0x20000 && ucs
<= 0x2fffd) ||
5216 (ucs
>= 0x30000 && ucs
<= 0x3fffd)
5221 })({nul
: 0, control
: 0}); // configurable options
5227 Terminal
.EventEmitter
= EventEmitter
;
5228 Terminal
.inherits
= inherits
;
5231 * Adds an event listener to the terminal.
5233 * @param {string} event The name of the event. TODO: Document all event types
5234 * @param {function} callback The function to call when the event is triggered.
5238 Terminal
.cancel
= cancel
;
5240 module
.exports
= Terminal
;