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
= 5;
48 * The number of writes to perform in a single batch before allowing the
49 * renderer to catch up with a 0ms setTimeout.
51 var WRITE_BATCH_SIZE
= 300;
54 * The maximum number of refresh frames to skip when the write buffer is non-
55 * empty. Note that these frames may be intermingled with frames that are
56 * skipped via requestAnimationFrame's mechanism.
58 var MAX_REFRESH_FRAME_SKIP
= 5;
65 * Creates a new `Terminal` object.
67 * @param {object} options An object containing a set of options, the available options are:
68 * - `cursorBlink` (boolean): Whether the terminal cursor blinks
69 * - `cols` (number): The number of columns of the terminal (horizontal size)
70 * - `rows` (number): The number of rows of the terminal (vertical size)
74 * @alias module:xterm/src/xterm
76 function Terminal(options
) {
79 if (!(this instanceof Terminal
)) {
80 return new Terminal(arguments
[0], arguments
[1], arguments
[2]);
83 self
.browser
= Browser
;
84 self
.cancel
= Terminal
.cancel
;
86 EventEmitter
.call(this);
88 if (typeof options
=== 'number') {
96 options
= options
|| {};
99 Object
.keys(Terminal
.defaults
).forEach(function(key
) {
100 if (options
[key
] == null) {
101 options
[key
] = Terminal
.options
[key
];
103 if (Terminal
[key
] !== Terminal
.defaults
[key
]) {
104 options
[key
] = Terminal
[key
];
107 self
[key
] = options
[key
];
110 if (options
.colors
.length
=== 8) {
111 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(8));
112 } else if (options
.colors
.length
=== 16) {
113 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(16));
114 } else if (options
.colors
.length
=== 10) {
115 options
.colors
= options
.colors
.slice(0, -2).concat(
116 Terminal
._colors
.slice(8, -2), options
.colors
.slice(-2));
117 } else if (options
.colors
.length
=== 18) {
118 options
.colors
= options
.colors
.concat(
119 Terminal
._colors
.slice(16, -2), options
.colors
.slice(-2));
121 this.colors
= options
.colors
;
123 this.options
= options
;
125 // this.context = options.context || window;
126 // this.document = options.document || document;
127 this.parent
= options
.body
|| options
.parent
|| (
128 document
? document
.getElementsByTagName('body')[0] : null
131 this.cols
= options
.cols
|| options
.geometry
[0];
132 this.rows
= options
.rows
|| options
.geometry
[1];
133 this.geometry
= [this.cols
, this.rows
];
135 if (options
.handler
) {
136 this.on('data', options
.handler
);
140 * The scroll position of the y cursor, ie. ybase + y = the y position within the entire
146 * The scroll position of the viewport
151 * The cursor's x position after ybase
156 * The cursor's y position after ybase
160 /** A queue of the rows to be refreshed */
161 this.refreshRowsQueue
= [];
163 this.cursorState
= 0;
164 this.cursorHidden
= false;
169 this.scrollBottom
= this.rows
- 1;
170 this.customKeydownHandler
= null;
173 this.applicationKeypad
= false;
174 this.applicationCursor
= false;
175 this.originMode
= false;
176 this.insertMode
= false;
177 this.wraparoundMode
= true; // defaults: xterm - true, vt100 - false
182 this.gcharset
= null;
184 this.charsets
= [null];
208 this.readable
= true;
209 this.writable
= true;
211 this.defAttr
= (0 << 18) | (257 << 9) | (256 << 0);
212 this.curAttr
= this.defAttr
;
215 this.currentParam
= 0;
220 this.writeBuffer
= [];
221 this.writeInProgress
= false;
222 this.refreshFramesSkipped
= 0;
225 * Whether _xterm.js_ sent XOFF in order to catch up with the pty process.
226 * This is a distinct state from writeStopped so that if the user requested
227 * XOFF via ^S that it will not automatically resume when the writeBuffer goes
230 this.xoffSentToCatchUp
= false;
232 /** Whether writing has been stopped as a result of XOFF */
233 this.writeStopped
= false;
235 // leftover surrogate high from previous write invocation
236 this.surrogate_high
= '';
239 * An array of all lines in the entire buffer, including the prompt. The lines are array of
240 * characters which are 2-length arrays where [0] is an attribute and [1] is the character.
242 this.lines
= new CircularList(this.scrollback
);
245 this.lines
.push(this.blankLine());
251 // Store if user went browsing history in scrollback
252 this.userScrolling
= false;
255 inherits(Terminal
, EventEmitter
);
258 * back_color_erase feature for xterm.
260 Terminal
.prototype.eraseAttr = function() {
261 // if (this.is('screen')) return this.defAttr;
262 return (this.defAttr
& ~0x1ff) | (this.curAttr
& 0x1ff);
270 Terminal
.tangoColors
= [
291 // Colors 0-15 + 16-255
292 // Much thanks to TooTallNate for writing this.
293 Terminal
.colors
= (function() {
294 var colors
= Terminal
.tangoColors
.slice()
295 , r
= [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff]
300 for (; i
< 216; i
++) {
301 out(r
[(i
/ 36) % 6 | 0], r
[(i
/ 6) % 6 | 0], r
[i
% 6]);
306 for (; i
< 24; i
++) {
311 function out(r
, g
, b
) {
312 colors
.push('#' + hex(r
) + hex(g
) + hex(b
));
317 return c
.length
< 2 ? '0' + c
: c
;
323 Terminal
._colors
= Terminal
.colors
.slice();
325 Terminal
.vcolors
= (function() {
327 , colors
= Terminal
.colors
331 for (; i
< 256; i
++) {
332 color
= parseInt(colors
[i
].substring(1), 16);
334 (color
>> 16) & 0xff,
347 Terminal
.defaults
= {
348 colors
: Terminal
.colors
,
360 // programFeatures: false,
364 Terminal
.options
= {};
366 Terminal
.focus
= null;
368 each(keys(Terminal
.defaults
), function(key
) {
369 Terminal
[key
] = Terminal
.defaults
[key
];
370 Terminal
.options
[key
] = Terminal
.defaults
[key
];
374 * Focus the terminal. Delegates focus handling to the terminal's DOM element.
376 Terminal
.prototype.focus = function() {
377 return this.textarea
.focus();
381 * Retrieves an option's value from the terminal.
382 * @param {string} key The option key.
384 Terminal
.prototype.getOption = function(key
, value
) {
385 if (!(key
in Terminal
.defaults
)) {
386 throw new Error('No option with key "' + key
+ '"');
389 if (typeof this.options
[key
] !== 'undefined') {
390 return this.options
[key
];
397 * Sets an option on the terminal.
398 * @param {string} key The option key.
399 * @param {string} value The option value.
401 Terminal
.prototype.setOption = function(key
, value
) {
402 if (!(key
in Terminal
.defaults
)) {
403 throw new Error('No option with key "' + key
+ '"');
406 this.options
[key
] = value
;
410 * Binds the desired focus behavior on a given terminal object.
414 Terminal
.bindFocus = function (term
) {
415 on(term
.textarea
, 'focus', function (ev
) {
416 if (term
.sendFocus
) {
419 term
.element
.classList
.add('focus');
421 Terminal
.focus
= term
;
422 term
.emit('focus', {terminal
: term
});
427 * Blur the terminal. Delegates blur handling to the terminal's DOM element.
429 Terminal
.prototype.blur = function() {
430 return this.textarea
.blur();
434 * Binds the desired blur behavior on a given terminal object.
438 Terminal
.bindBlur = function (term
) {
439 on(term
.textarea
, 'blur', function (ev
) {
440 term
.queueRefresh(term
.y
, term
.y
);
441 if (term
.sendFocus
) {
444 term
.element
.classList
.remove('focus');
445 Terminal
.focus
= null;
446 term
.emit('blur', {terminal
: term
});
451 * Initialize default behavior
453 Terminal
.prototype.initGlobal = function() {
456 Terminal
.bindKeys(this);
457 Terminal
.bindFocus(this);
458 Terminal
.bindBlur(this);
460 // Bind clipboard functionality
461 on(this.element
, 'copy', function (ev
) {
462 copyHandler
.call(this, ev
, term
);
464 on(this.textarea
, 'paste', function (ev
) {
465 pasteHandler
.call(this, ev
, term
);
467 on(this.element
, 'paste', function (ev
) {
468 pasteHandler
.call(this, ev
, term
);
471 function rightClickHandlerWrapper (ev
) {
472 rightClickHandler
.call(this, ev
, term
);
475 if (term
.browser
.isFirefox
) {
476 on(this.element
, 'mousedown', function (ev
) {
477 if (ev
.button
== 2) {
478 rightClickHandlerWrapper(ev
);
482 on(this.element
, 'contextmenu', rightClickHandlerWrapper
);
487 * Apply key handling to the terminal
489 Terminal
.bindKeys = function(term
) {
490 on(term
.element
, 'keydown', function(ev
) {
491 if (document
.activeElement
!= this) {
497 on(term
.element
, 'keypress', function(ev
) {
498 if (document
.activeElement
!= this) {
504 on(term
.element
, 'keyup', term
.focus
.bind(term
));
506 on(term
.textarea
, 'keydown', function(ev
) {
510 on(term
.textarea
, 'keypress', function(ev
) {
512 // Truncate the textarea's value, since it is not needed
516 on(term
.textarea
, 'compositionstart', term
.compositionHelper
.compositionstart
.bind(term
.compositionHelper
));
517 on(term
.textarea
, 'compositionupdate', term
.compositionHelper
.compositionupdate
.bind(term
.compositionHelper
));
518 on(term
.textarea
, 'compositionend', term
.compositionHelper
.compositionend
.bind(term
.compositionHelper
));
519 term
.on('refresh', term
.compositionHelper
.updateCompositionElements
.bind(term
.compositionHelper
));
524 * Insert the given row to the terminal or produce a new one
525 * if no row argument is passed. Return the inserted row.
526 * @param {HTMLElement} row (optional) The row to append to the terminal.
528 Terminal
.prototype.insertRow = function (row
) {
529 if (typeof row
!= 'object') {
530 row
= document
.createElement('div');
533 this.rowContainer
.appendChild(row
);
534 this.children
.push(row
);
540 * Opens the terminal within an element.
542 * @param {HTMLElement} parent The element to create the terminal within.
544 Terminal
.prototype.open = function(parent
) {
545 var self
=this, i
=0, div
;
547 this.parent
= parent
|| this.parent
;
550 throw new Error('Terminal requires a parent element.');
553 // Grab global elements
554 this.context
= this.parent
.ownerDocument
.defaultView
;
555 this.document
= this.parent
.ownerDocument
;
556 this.body
= this.document
.getElementsByTagName('body')[0];
558 //Create main element container
559 this.element
= this.document
.createElement('div');
560 this.element
.classList
.add('terminal');
561 this.element
.classList
.add('xterm');
562 this.element
.classList
.add('xterm-theme-' + this.theme
);
564 this.element
.style
.height
565 this.element
.setAttribute('tabindex', 0);
567 this.viewportElement
= document
.createElement('div');
568 this.viewportElement
.classList
.add('xterm-viewport');
569 this.element
.appendChild(this.viewportElement
);
570 this.viewportScrollArea
= document
.createElement('div');
571 this.viewportScrollArea
.classList
.add('xterm-scroll-area');
572 this.viewportElement
.appendChild(this.viewportScrollArea
);
574 // Create the container that will hold the lines of the terminal and then
575 // produce the lines the lines.
576 this.rowContainer
= document
.createElement('div');
577 this.rowContainer
.classList
.add('xterm-rows');
578 this.element
.appendChild(this.rowContainer
);
581 // Create the container that will hold helpers like the textarea for
582 // capturing DOM Events. Then produce the helpers.
583 this.helperContainer
= document
.createElement('div');
584 this.helperContainer
.classList
.add('xterm-helpers');
585 // TODO: This should probably be inserted once it's filled to prevent an additional layout
586 this.element
.appendChild(this.helperContainer
);
587 this.textarea
= document
.createElement('textarea');
588 this.textarea
.classList
.add('xterm-helper-textarea');
589 this.textarea
.setAttribute('autocorrect', 'off');
590 this.textarea
.setAttribute('autocapitalize', 'off');
591 this.textarea
.setAttribute('spellcheck', 'false');
592 this.textarea
.tabIndex
= 0;
593 this.textarea
.addEventListener('focus', function() {
594 self
.emit('focus', {terminal
: self
});
596 this.textarea
.addEventListener('blur', function() {
597 self
.emit('blur', {terminal
: self
});
599 this.helperContainer
.appendChild(this.textarea
);
601 this.compositionView
= document
.createElement('div');
602 this.compositionView
.classList
.add('composition-view');
603 this.compositionHelper
= new CompositionHelper(this.textarea
, this.compositionView
, this);
604 this.helperContainer
.appendChild(this.compositionView
);
606 this.charMeasureElement
= document
.createElement('div');
607 this.charMeasureElement
.classList
.add('xterm-char-measure-element');
608 this.charMeasureElement
.innerHTML
= 'W';
609 this.helperContainer
.appendChild(this.charMeasureElement
);
611 for (; i
< this.rows
; i
++) {
614 this.parent
.appendChild(this.element
);
616 this.viewport
= new Viewport(this, this.viewportElement
, this.viewportScrollArea
, this.charMeasureElement
);
618 // Setup loop that draws to screen
619 this.queueRefresh(0, this.rows
- 1);
622 // Initialize global actions that
623 // need to be taken on the document.
626 // Ensure there is a Terminal.focus.
629 on(this.element
, 'click', function() {
630 var selection
= document
.getSelection(),
631 collapsed
= selection
.isCollapsed
,
632 isRange
= typeof collapsed
== 'boolean' ? !collapsed
: selection
.type
== 'Range';
638 // Listen for mouse events and translate
639 // them into terminal mouse protocols.
642 // Figure out whether boldness affects
643 // the character width of monospace fonts.
644 if (Terminal
.brokenBold
== null) {
645 Terminal
.brokenBold
= isBoldBroken(this.document
);
649 * This event is emitted when terminal has completed opening.
658 * Attempts to load an add-on using CommonJS or RequireJS (whichever is available).
659 * @param {string} addon The name of the addon to load
662 Terminal
.loadAddon = function(addon
, callback
) {
663 if (typeof exports
=== 'object' && typeof module
=== 'object') {
665 return require('./addons/' + addon
+ '/' + addon
);
666 } else if (typeof define
== 'function') {
668 return require(['./addons/' + addon
+ '/' + addon
], callback
);
670 console
.error('Cannot load a module without a CommonJS or RequireJS environment.');
678 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#Mouse%20Tracking
679 * To better understand these
680 * the xterm code is very helpful:
682 * button.c, charproc.c, misc.c
683 * Relevant functions in xterm/button.c:
684 * BtnCode, EmitButtonCode, EditorButton, SendMousePosition
686 Terminal
.prototype.bindMouse = function() {
687 var el
= this.element
, self
= this, pressed
= 32;
689 // mouseup, mousedown, wheel
690 // left click: ^[[M 3<^[[M#3<
692 function sendButton(ev
) {
696 // get the xterm-style button
697 button
= getButton(ev
);
699 // get mouse coordinates
703 sendEvent(button
, pos
);
705 switch (ev
.overrideType
|| ev
.type
) {
710 // keep it at the left
711 // button, just in case.
722 // motion example of a left click:
723 // ^[[M 3<^[[M@4<^[[M@5<^[[M@6<^[[M@7<^[[M#7<
724 function sendMove(ev
) {
731 // buttons marked as motions
732 // are incremented by 32
735 sendEvent(button
, pos
);
739 // position to characters
740 function encode(data
, ch
) {
741 if (!self
.utfMouse
) {
742 if (ch
=== 255) return data
.push(0);
743 if (ch
> 127) ch
= 127;
746 if (ch
=== 2047) return data
.push(0);
750 if (ch
> 2047) ch
= 2047;
751 data
.push(0xC0 | (ch
>> 6));
752 data
.push(0x80 | (ch
& 0x3F));
757 // send a mouse event:
758 // regular/utf8: ^[[M Cb Cx Cy
759 // urxvt: ^[[ Cb ; Cx ; Cy M
760 // sgr: ^[[ Cb ; Cx ; Cy M/m
761 // vt300: ^[[ 24(1/3/5)~ [ Cx , Cy ] \r
762 // locator: CSI P e ; P b ; P r ; P c ; P p & w
763 function sendEvent(button
, pos
) {
764 // self.emit('mouse', {
770 if (self
.vt300Mouse
) {
772 // http://www.vt100.net/docs/vt3xx-gp/chapter15.html
776 var data
= '\x1b[24';
777 if (button
=== 0) data
+= '1';
778 else if (button
=== 1) data
+= '3';
779 else if (button
=== 2) data
+= '5';
780 else if (button
=== 3) return;
782 data
+= '~[' + pos
.x
+ ',' + pos
.y
+ ']\r';
787 if (self
.decLocator
) {
792 if (button
=== 0) button
= 2;
793 else if (button
=== 1) button
= 4;
794 else if (button
=== 2) button
= 6;
795 else if (button
=== 3) button
= 3;
799 + (button
=== 3 ? 4 : 0)
810 if (self
.urxvtMouse
) {
815 self
.send('\x1b[' + button
+ ';' + pos
.x
+ ';' + pos
.y
+ 'M');
823 + (((button
& 3) === 3 ? button
& ~3 : button
) - 32)
828 + ((button
& 3) === 3 ? 'm' : 'M'));
834 encode(data
, button
);
838 self
.send('\x1b[M' + String
.fromCharCode
.apply(String
, data
));
841 function getButton(ev
) {
854 // 1, and 2 - with 64 added
855 switch (ev
.overrideType
|| ev
.type
) {
857 button
= ev
.button
!= null
863 if (self
.browser
.isMSIE
) {
864 button
= button
=== 1 ? 0 : button
=== 4 ? 1 : button
;
870 case 'DOMMouseScroll':
871 button
= ev
.detail
< 0
876 button
= ev
.wheelDeltaY
> 0
882 // next three bits are the modifiers:
883 // 4 = shift, 8 = meta, 16 = control
884 shift
= ev
.shiftKey
? 4 : 0;
885 meta
= ev
.metaKey
? 8 : 0;
886 ctrl
= ev
.ctrlKey
? 16 : 0;
887 mod
= shift
| meta
| ctrl
;
890 if (self
.vt200Mouse
) {
893 } else if (!self
.normalMouse
) {
898 button
= (32 + (mod
<< 2)) + button
;
903 // mouse coordinates measured in cols/rows
904 function getCoords(ev
) {
907 // ignore browsers without pageX for now
908 if (ev
.pageX
== null) return;
914 // should probably check offsetParent
915 // but this is more portable
916 while (el
&& el
!== self
.document
.documentElement
) {
919 el
= 'offsetParent' in el
924 // convert to cols/rows
925 w
= self
.element
.clientWidth
;
926 h
= self
.element
.clientHeight
;
927 x
= Math
.ceil((x
/ w
) * self
.cols
);
928 y
= Math
.ceil((y
/ h
) * self
.rows
);
930 // be sure to avoid sending
931 // bad positions to the program
933 if (x
> self
.cols
) x
= self
.cols
;
935 if (y
> self
.rows
) y
= self
.rows
;
937 // xterm sends raw bytes and
938 // starts at 32 (SP) for each.
949 on(el
, 'mousedown', function(ev
) {
950 if (!self
.mouseEvents
) return;
959 //if (self.vt200Mouse && !self.normalMouse) {
960 if (self
.vt200Mouse
) {
961 ev
.overrideType
= 'mouseup';
963 return self
.cancel(ev
);
967 if (self
.normalMouse
) on(self
.document
, 'mousemove', sendMove
);
969 // x10 compatibility mode can't send button releases
970 if (!self
.x10Mouse
) {
971 on(self
.document
, 'mouseup', function up(ev
) {
973 if (self
.normalMouse
) off(self
.document
, 'mousemove', sendMove
);
974 off(self
.document
, 'mouseup', up
);
975 return self
.cancel(ev
);
979 return self
.cancel(ev
);
982 //if (self.normalMouse) {
983 // on(self.document, 'mousemove', sendMove);
986 on(el
, 'wheel', function(ev
) {
987 if (!self
.mouseEvents
) return;
990 || self
.decLocator
) return;
992 return self
.cancel(ev
);
995 // allow wheel scrolling in
996 // the shell for example
997 on(el
, 'wheel', function(ev
) {
998 if (self
.mouseEvents
) return;
999 self
.viewport
.onWheel(ev
);
1000 return self
.cancel(ev
);
1005 * Destroys the terminal.
1007 Terminal
.prototype.destroy = function() {
1008 this.readable
= false;
1009 this.writable
= false;
1011 this.handler = function() {};
1012 this.write = function() {};
1013 if (this.element
.parentNode
) {
1014 this.element
.parentNode
.removeChild(this.element
);
1016 //this.emit('close');
1021 * Flags used to render terminal text properly
1032 * Queues a refresh between two rows (inclusive), to be done on next animation
1034 * @param {number} start The start row.
1035 * @param {number} end The end row.
1037 Terminal
.prototype.queueRefresh = function(start
, end
) {
1038 this.refreshRowsQueue
.push({ start
: start
, end
: end
});
1042 * Performs the refresh loop callback, calling refresh only if a refresh is
1043 * necessary before queueing up the next one.
1045 Terminal
.prototype.refreshLoop = function() {
1046 // Don't refresh if there were no row changes
1047 if (this.refreshRowsQueue
.length
> 0) {
1048 // Skip MAX_REFRESH_FRAME_SKIP frames if the writeBuffer is non-empty as it
1049 // will need to be immediately refreshed anyway. This saves a lot of
1050 // rendering time as the viewport DOM does not need to be refreshed, no
1051 // scroll events, no layouts, etc.
1052 var skipFrame
= this.writeBuffer
.length
> 0 && this.refreshFramesSkipped
++ <= MAX_REFRESH_FRAME_SKIP
;
1055 this.refreshFramesSkipped
= 0;
1058 if (this.refreshRowsQueue
.length
> 4) {
1059 // Just do a full refresh when 5+ refreshes are queued
1061 end
= this.rows
- 1;
1063 // Get start and end rows that need refreshing
1064 start
= this.refreshRowsQueue
[0].start
;
1065 end
= this.refreshRowsQueue
[0].end
;
1066 for (var i
= 1; i
< this.refreshRowsQueue
.length
; i
++) {
1067 if (this.refreshRowsQueue
[i
].start
< start
) {
1068 start
= this.refreshRowsQueue
[i
].start
;
1070 if (this.refreshRowsQueue
[i
].end
> end
) {
1071 end
= this.refreshRowsQueue
[i
].end
;
1075 this.refreshRowsQueue
= [];
1076 this.refresh(start
, end
);
1079 window
.requestAnimationFrame(this.refreshLoop
.bind(this));
1083 * Refreshes (re-renders) terminal content within two rows (inclusive)
1087 * In the screen buffer, each character is stored as a an array with a character
1088 * and a 32-bit integer:
1089 * - First value: a utf-16 character.
1091 * - Next 9 bits: background color (0-511).
1092 * - Next 9 bits: foreground color (0-511).
1093 * - Next 14 bits: a mask for misc. flags:
1100 * @param {number} start The row to start from (between 0 and terminal's height terminal - 1)
1101 * @param {number} end The row to end at (between fromRow and terminal's height terminal - 1)
1103 Terminal
.prototype.refresh = function(start
, end
) {
1106 var x
, y
, i
, line
, out
, ch
, ch_width
, width
, data
, attr
, bg
, fg
, flags
, row
, parent
, focused
= document
.activeElement
;
1108 // If this is a big refresh, remove the terminal rows from the DOM for faster calculations
1109 if (end
- start
>= this.rows
/ 2) {
1110 parent
= this.element
.parentNode
;
1112 this.element
.removeChild(this.rowContainer
);
1119 if (end
>= this.rows
.length
) {
1120 this.log('`end` is too large. Most likely a bad CSR.');
1121 end
= this.rows
.length
- 1;
1124 for (; y
<= end
; y
++) {
1125 row
= y
+ this.ydisp
;
1127 line
= this.lines
.get(row
);
1130 if (this.y
=== y
- (this.ybase
- this.ydisp
)
1132 && !this.cursorHidden
) {
1138 attr
= this.defAttr
;
1141 for (; i
< width
; i
++) {
1144 ch_width
= line
[i
][2];
1148 if (i
=== x
) data
= -1;
1150 if (data
!== attr
) {
1151 if (attr
!== this.defAttr
) {
1154 if (data
!== this.defAttr
) {
1156 out
+= '<span class="reverse-video terminal-cursor';
1157 if (this.cursorBlink
) {
1162 var classNames
= [];
1165 fg
= (data
>> 9) & 0x1ff;
1168 if (flags
& Terminal
.flags
.BOLD
) {
1169 if (!Terminal
.brokenBold
) {
1170 classNames
.push('xterm-bold');
1172 // See: XTerm*boldColors
1173 if (fg
< 8) fg
+= 8;
1176 if (flags
& Terminal
.flags
.UNDERLINE
) {
1177 classNames
.push('xterm-underline');
1180 if (flags
& Terminal
.flags
.BLINK
) {
1181 classNames
.push('xterm-blink');
1184 // If inverse flag is on, then swap the foreground and background variables.
1185 if (flags
& Terminal
.flags
.INVERSE
) {
1186 /* One-line variable swap in JavaScript: http://stackoverflow.com/a/16201730 */
1187 bg = [fg, fg = bg][0];
1188 // Should inverse just be before the
1189 // above boldColors effect instead?
1190 if ((flags & 1) && fg < 8) fg += 8;
1193 if (flags & Terminal.flags.INVISIBLE) {
1194 classNames.push('xterm-hidden');
1198 * Weird situation: Invert flag used black foreground and white background results
1199 * in invalid background color, positioned at the 256 index of the 256 terminal
1200 * color map. Pin the colors manually in such a case.
1202 * Source: https://github.com/sourcelair/xterm.js/issues/57
1204 if (flags
& Terminal
.flags
.INVERSE
) {
1214 classNames
.push('xterm-bg-color-' + bg
);
1218 classNames
.push('xterm-color-' + fg
);
1222 if (classNames
.length
) {
1223 out
+= ' class="' + classNames
.join(' ') + '"';
1252 if (attr
!== this.defAttr
) {
1256 this.children
[y
].innerHTML
= out
;
1260 this.element
.appendChild(this.rowContainer
);
1263 this.emit('refresh', {element
: this.element
, start
: start
, end
: end
});
1267 * Display the cursor element
1269 Terminal
.prototype.showCursor = function() {
1270 if (!this.cursorState
) {
1271 this.cursorState
= 1;
1272 this.queueRefresh(this.y
, this.y
);
1277 * Scroll the terminal down 1 row, creating a blank line.
1279 Terminal
.prototype.scroll = function() {
1284 // TODO: Why is this done twice?
1285 if (!this.userScrolling
) {
1286 this.ydisp
= this.ybase
;
1290 row
= this.ybase
+ this.rows
- 1;
1292 // subtract the bottom scroll region
1293 row
-= this.rows
- 1 - this.scrollBottom
;
1295 if (row
=== this.lines
.length
) {
1296 // Compensate ybase and ydisp if lines has hit the maximum buffer size
1297 if (this.lines
.length
=== this.lines
.maxLength
) {
1299 if (this.ydisp
!== 0) {
1303 // Optimization: pushing is faster than splicing when they amount to the same behavior
1304 this.lines
.push(this.blankLine());
1307 this.lines
.splice(row
, 0, this.blankLine());
1310 if (this.scrollTop
!== 0) {
1311 if (this.ybase
!== 0) {
1313 if (!this.userScrolling
) {
1314 this.ydisp
= this.ybase
;
1317 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
1321 this.updateRange(this.scrollTop
);
1322 this.updateRange(this.scrollBottom
);
1325 * This event is emitted whenever the terminal is scrolled.
1326 * The one parameter passed is the new y display position.
1330 this.emit('scroll', this.ydisp
);
1334 * Scroll the display of the terminal
1335 * @param {number} disp The number of lines to scroll down (negatives scroll up).
1336 * @param {boolean} suppressScrollEvent Don't emit the scroll event as scrollDisp. This is used
1337 * to avoid unwanted events being handled by the veiwport when the event was triggered from the
1338 * viewport originally.
1340 Terminal
.prototype.scrollDisp = function(disp
, suppressScrollEvent
) {
1342 this.userScrolling
= true;
1343 } else if (disp
+ this.ydisp
>= this.ybase
) {
1344 this.userScrolling
= false;
1349 if (this.ydisp
> this.ybase
) {
1350 this.ydisp
= this.ybase
;
1351 } else if (this.ydisp
< 0) {
1355 if (!suppressScrollEvent
) {
1356 this.emit('scroll', this.ydisp
);
1359 this.queueRefresh(0, this.rows
- 1);
1363 * Scroll the display of the terminal by a number of pages.
1364 * @param {number} pageCount The number of pages to scroll (negative scrolls up).
1366 Terminal
.prototype.scrollPages = function(pageCount
) {
1367 this.scrollDisp(pageCount
* (this.rows
- 1));
1371 * Scrolls the display of the terminal to the top.
1373 Terminal
.prototype.scrollToTop = function() {
1374 this.scrollDisp(-this.ydisp
);
1378 * Scrolls the display of the terminal to the bottom.
1380 Terminal
.prototype.scrollToBottom = function() {
1381 this.scrollDisp(this.ybase
- this.ydisp
);
1385 * Writes text to the terminal.
1386 * @param {string} text The text to write to the terminal.
1388 Terminal
.prototype.write = function(data
) {
1389 this.writeBuffer
.push(data
);
1391 // Send XOFF to pause the pty process if the write buffer becomes too large so
1392 // xterm.js can catch up before more data is sent. This is necessary in order
1393 // to keep signals such as ^C responsive.
1394 if (!this.xoffSentToCatchUp
&& this.writeBuffer
.length
>= WRITE_BUFFER_PAUSE_THRESHOLD
) {
1395 // XOFF - stop pty pipe
1396 // XON will be triggered by emulator before processing data chunk
1398 this.xoffSentToCatchUp
= true;
1401 if (!this.writeInProgress
&& this.writeBuffer
.length
> 0) {
1402 // Kick off a write which will write all data in sequence recursively
1403 this.writeInProgress
= true;
1404 // Kick off an async innerWrite so more writes can come in while processing data
1406 setTimeout(function () {
1412 Terminal
.prototype.innerWrite = function() {
1413 var writeBatch
= this.writeBuffer
.splice(0, WRITE_BATCH_SIZE
);
1414 while (writeBatch
.length
> 0) {
1415 var data
= writeBatch
.shift();
1416 var l
= data
.length
, i
= 0, j
, cs
, ch
, code
, low
, ch_width
, row
;
1418 // If XOFF was sent in order to catch up with the pty process, resume it if
1419 // the writeBuffer is empty to allow more data to come in.
1420 if (this.xoffSentToCatchUp
&& writeBatch
.length
=== 0 && this.writeBuffer
.length
=== 0) {
1422 this.xoffSentToCatchUp
= false;
1425 this.refreshStart
= this.y
;
1426 this.refreshEnd
= this.y
;
1428 // apply leftover surrogate high from last write
1429 if (this.surrogate_high
) {
1430 data
= this.surrogate_high
+ data
;
1431 this.surrogate_high
= '';
1434 for (; i
< l
; i
++) {
1437 // FIXME: higher chars than 0xa0 are not allowed in escape sequences
1438 // --> maybe move to default
1439 code
= data
.charCodeAt(i
);
1440 if (0xD800 <= code
&& code
<= 0xDBFF) {
1441 // we got a surrogate high
1442 // get surrogate low (next 2 bytes)
1443 low
= data
.charCodeAt(i
+1);
1445 // end of data stream, save surrogate high
1446 this.surrogate_high
= ch
;
1449 code
= ((code
- 0xD800) * 0x400) + (low
- 0xDC00) + 0x10000;
1450 ch
+= data
.charAt(i
+1);
1452 // surrogate low - already handled above
1453 if (0xDC00 <= code
&& code
<= 0xDFFF)
1455 switch (this.state
) {
1466 if (this.convertEol
) {
1470 if (this.y
> this.scrollBottom
) {
1490 this.x
= this.nextStop();
1505 this.state
= escaped
;
1510 // calculate print space
1511 // expensive call, therefore we save width in line buffer
1512 ch_width
= wcwidth(code
);
1515 if (this.charset
&& this.charset
[ch
]) {
1516 ch
= this.charset
[ch
];
1519 row
= this.y
+ this.ybase
;
1521 // insert combining char in last cell
1522 // FIXME: needs handling after cursor jumps
1523 if (!ch_width
&& this.x
) {
1524 // dont overflow left
1525 if (this.lines
.get(row
)[this.x
-1]) {
1526 if (!this.lines
.get(row
)[this.x
-1][2]) {
1528 // found empty cell after fullwidth, need to go 2 cells back
1529 if (this.lines
.get(row
)[this.x
-2])
1530 this.lines
.get(row
)[this.x
-2][1] += ch
;
1533 this.lines
.get(row
)[this.x
-1][1] += ch
;
1535 this.updateRange(this.y
);
1540 // goto next line if ch would overflow
1541 // TODO: needs a global min terminal width of 2
1542 if (this.x
+ch_width
-1 >= this.cols
) {
1543 // autowrap - DECAWM
1544 if (this.wraparoundMode
) {
1547 if (this.y
> this.scrollBottom
) {
1552 this.x
= this.cols
-1;
1553 if(ch_width
===2) // FIXME: check for xterm behavior
1557 row
= this.y
+ this.ybase
;
1559 // insert mode: move characters to right
1560 if (this.insertMode
) {
1561 // do this twice for a fullwidth char
1562 for (var moves
=0; moves
<ch_width
; ++moves
) {
1563 // remove last cell, if it's width is 0
1564 // we have to adjust the second last cell as well
1565 var removed
= this.lines
.get(this.y
+ this.ybase
).pop();
1567 && this.lines
.get(row
)[this.cols
-2]
1568 && this.lines
.get(row
)[this.cols
-2][2]===2)
1569 this.lines
.get(row
)[this.cols
-2] = [this.curAttr
, ' ', 1];
1571 // insert empty cell at cursor
1572 this.lines
.get(row
).splice(this.x
, 0, [this.curAttr
, ' ', 1]);
1576 this.lines
.get(row
)[this.x
] = [this.curAttr
, ch
, ch_width
];
1578 this.updateRange(this.y
);
1580 // fullwidth char - set next cell width to zero and advance cursor
1582 this.lines
.get(row
)[this.x
] = [this.curAttr
, '', 0];
1591 // ESC [ Control Sequence Introducer ( CSI is 0x9b).
1594 this.currentParam
= 0;
1598 // ESC ] Operating System Command ( OSC is 0x9d).
1601 this.currentParam
= 0;
1605 // ESC P Device Control String ( DCS is 0x90).
1608 this.currentParam
= 0;
1612 // ESC _ Application Program Command ( APC is 0x9f).
1614 this.state
= ignore
;
1617 // ESC ^ Privacy Message ( PM is 0x9e).
1619 this.state
= ignore
;
1622 // ESC c Full Reset (RIS).
1627 // ESC E Next Line ( NEL is 0x85).
1628 // ESC D Index ( IND is 0x84).
1636 // ESC M Reverse Index ( RI is 0x8d).
1638 this.reverseIndex();
1641 // ESC % Select default/utf-8 character set.
1642 // @ = default, G = utf-8
1644 //this.charset = null;
1646 this.setgCharset(0, Terminal
.charsets
.US
);
1647 this.state
= normal
;
1651 // ESC (,),*,+,-,. Designate G0-G2 Character Set.
1652 case '(': // <-- this seems to get all the attention
1678 this.state
= charset
;
1681 // Designate G3 Character Set (VT300).
1682 // A = ISO Latin-1 Supplemental.
1686 this.state
= charset
;
1691 // Single Shift Select of G2 Character Set
1692 // ( SS2 is 0x8e). This affects next character only.
1696 // Single Shift Select of G3 Character Set
1697 // ( SS3 is 0x8f). This affects next character only.
1701 // Invoke the G2 Character Set as GL (LS2).
1706 // Invoke the G3 Character Set as GL (LS3).
1711 // Invoke the G3 Character Set as GR (LS3R).
1716 // Invoke the G2 Character Set as GR (LS2R).
1721 // Invoke the G1 Character Set as GR (LS1R).
1726 // ESC 7 Save Cursor (DECSC).
1729 this.state
= normal
;
1732 // ESC 8 Restore Cursor (DECRC).
1734 this.restoreCursor();
1735 this.state
= normal
;
1738 // ESC # 3 DEC line height/width
1740 this.state
= normal
;
1744 // ESC H Tab Set (HTS is 0x88).
1749 // ESC = Application Keypad (DECKPAM).
1751 this.log('Serial port requested application keypad.');
1752 this.applicationKeypad
= true;
1753 this.viewport
.syncScrollArea();
1754 this.state
= normal
;
1757 // ESC > Normal Keypad (DECKPNM).
1759 this.log('Switching back to normal keypad.');
1760 this.applicationKeypad
= false;
1761 this.viewport
.syncScrollArea();
1762 this.state
= normal
;
1766 this.state
= normal
;
1767 this.error('Unknown ESC control: %s.', ch
);
1774 case '0': // DEC Special Character and Line Drawing Set.
1775 cs
= Terminal
.charsets
.SCLD
;
1778 cs
= Terminal
.charsets
.UK
;
1780 case 'B': // United States (USASCII).
1781 cs
= Terminal
.charsets
.US
;
1784 cs
= Terminal
.charsets
.Dutch
;
1786 case 'C': // Finnish
1788 cs
= Terminal
.charsets
.Finnish
;
1791 cs
= Terminal
.charsets
.French
;
1793 case 'Q': // FrenchCanadian
1794 cs
= Terminal
.charsets
.FrenchCanadian
;
1797 cs
= Terminal
.charsets
.German
;
1799 case 'Y': // Italian
1800 cs
= Terminal
.charsets
.Italian
;
1802 case 'E': // NorwegianDanish
1804 cs
= Terminal
.charsets
.NorwegianDanish
;
1806 case 'Z': // Spanish
1807 cs
= Terminal
.charsets
.Spanish
;
1809 case 'H': // Swedish
1811 cs
= Terminal
.charsets
.Swedish
;
1814 cs
= Terminal
.charsets
.Swiss
;
1816 case '/': // ISOLatin (actually /A)
1817 cs
= Terminal
.charsets
.ISOLatin
;
1821 cs
= Terminal
.charsets
.US
;
1824 this.setgCharset(this.gcharset
, cs
);
1825 this.gcharset
= null;
1826 this.state
= normal
;
1832 // Set Text Parameters.
1833 if (ch
=== '\x1b' || ch
=== '\x07') {
1834 if (ch
=== '\x1b') i
++;
1836 this.params
.push(this.currentParam
);
1838 switch (this.params
[0]) {
1842 if (this.params
[1]) {
1843 this.title
= this.params
[1];
1844 this.handleTitle(this.title
);
1852 // change dynamic colors
1864 // change dynamic ui colors
1876 // manipulate selection data
1894 this.currentParam
= 0;
1895 this.state
= normal
;
1897 if (!this.params
.length
) {
1898 if (ch
>= '0' && ch
<= '9') {
1900 this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1901 } else if (ch
=== ';') {
1902 this.params
.push(this.currentParam
);
1903 this.currentParam
= '';
1906 this.currentParam
+= ch
;
1913 if (ch
=== '?' || ch
=== '>' || ch
=== '!') {
1919 if (ch
>= '0' && ch
<= '9') {
1920 this.currentParam
= this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1924 // '$', '"', ' ', '\''
1925 if (ch
=== '$' || ch
=== '"' || ch
=== ' ' || ch
=== '\'') {
1930 this.params
.push(this.currentParam
);
1931 this.currentParam
= 0;
1934 if (ch
=== ';') break;
1936 this.state
= normal
;
1940 // Cursor Up Ps Times (default = 1) (CUU).
1942 this.cursorUp(this.params
);
1946 // Cursor Down Ps Times (default = 1) (CUD).
1948 this.cursorDown(this.params
);
1952 // Cursor Forward Ps Times (default = 1) (CUF).
1954 this.cursorForward(this.params
);
1958 // Cursor Backward Ps Times (default = 1) (CUB).
1960 this.cursorBackward(this.params
);
1964 // Cursor Position [row;column] (default = [1,1]) (CUP).
1966 this.cursorPos(this.params
);
1969 // CSI Ps J Erase in Display (ED).
1971 this.eraseInDisplay(this.params
);
1974 // CSI Ps K Erase in Line (EL).
1976 this.eraseInLine(this.params
);
1979 // CSI Pm m Character Attributes (SGR).
1982 this.charAttributes(this.params
);
1986 // CSI Ps n Device Status Report (DSR).
1989 this.deviceStatus(this.params
);
1998 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
2000 this.insertChars(this.params
);
2004 // Cursor Next Line Ps Times (default = 1) (CNL).
2006 this.cursorNextLine(this.params
);
2010 // Cursor Preceding Line Ps Times (default = 1) (CNL).
2012 this.cursorPrecedingLine(this.params
);
2016 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
2018 this.cursorCharAbsolute(this.params
);
2022 // Insert Ps Line(s) (default = 1) (IL).
2024 this.insertLines(this.params
);
2028 // Delete Ps Line(s) (default = 1) (DL).
2030 this.deleteLines(this.params
);
2034 // Delete Ps Character(s) (default = 1) (DCH).
2036 this.deleteChars(this.params
);
2040 // Erase Ps Character(s) (default = 1) (ECH).
2042 this.eraseChars(this.params
);
2045 // CSI Pm ` Character Position Absolute
2046 // [column] (default = [row,1]) (HPA).
2048 this.charPosAbsolute(this.params
);
2052 // Horizontal Position Relative
2054 this.HPositionRelative(this.params
);
2058 // Send Device Attributes (Primary DA).
2060 // Send Device Attributes (Secondary DA)
2062 this.sendDeviceAttributes(this.params
);
2066 // Line Position Absolute [row] (default = [1,column]) (VPA).
2068 this.linePosAbsolute(this.params
);
2071 // 145 65 e * VPR - Vertical Position Relative
2073 this.VPositionRelative(this.params
);
2077 // Horizontal and Vertical Position [row;column] (default =
2080 this.HVPosition(this.params
);
2083 // CSI Pm h Set Mode (SM).
2084 // CSI ? Pm h - mouse escape codes, cursor escape codes
2086 this.setMode(this.params
);
2089 // CSI Pm l Reset Mode (RM).
2092 this.resetMode(this.params
);
2096 // Set Scrolling Region [top;bottom] (default = full size of win-
2100 this.setScrollRegion(this.params
);
2104 // Save cursor (ANSI.SYS).
2106 this.saveCursor(this.params
);
2110 // Restore cursor (ANSI.SYS).
2112 this.restoreCursor(this.params
);
2120 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
2122 this.cursorForwardTab(this.params
);
2125 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
2127 this.scrollUp(this.params
);
2130 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
2131 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
2134 // if (this.prefix === '>') {
2135 // this.resetTitleModes(this.params);
2138 // if (this.params.length > 2) {
2139 // this.initMouseTracking(this.params);
2142 if (this.params
.length
< 2 && !this.prefix
) {
2143 this.scrollDown(this.params
);
2148 // Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
2150 this.cursorBackwardTab(this.params
);
2153 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
2155 this.repeatPrecedingCharacter(this.params
);
2158 // CSI Ps g Tab Clear (TBC).
2160 this.tabClear(this.params
);
2163 // CSI Pm i Media Copy (MC).
2166 // this.mediaCopy(this.params);
2169 // CSI Pm m Character Attributes (SGR).
2171 // case 'm': // duplicate
2172 // if (this.prefix === '>') {
2173 // this.setResources(this.params);
2175 // this.charAttributes(this.params);
2179 // CSI Ps n Device Status Report (DSR).
2181 // case 'n': // duplicate
2182 // if (this.prefix === '>') {
2183 // this.disableModifiers(this.params);
2185 // this.deviceStatus(this.params);
2189 // CSI > Ps p Set pointer mode.
2190 // CSI ! p Soft terminal reset (DECSTR).
2192 // Request ANSI mode (DECRQM).
2194 // Request DEC private mode (DECRQM).
2197 switch (this.prefix
) {
2199 // this.setPointerMode(this.params);
2202 this.softReset(this.params
);
2205 // if (this.postfix === '$') {
2206 // this.requestPrivateMode(this.params);
2210 // if (this.postfix === '"') {
2211 // this.setConformanceLevel(this.params);
2212 // } else if (this.postfix === '$') {
2213 // this.requestAnsiMode(this.params);
2219 // CSI Ps q Load LEDs (DECLL).
2223 // if (this.postfix === ' ') {
2224 // this.setCursorStyle(this.params);
2227 // if (this.postfix === '"') {
2228 // this.setCharProtectionAttr(this.params);
2231 // this.loadLEDs(this.params);
2235 // Set Scrolling Region [top;bottom] (default = full size of win-
2238 // CSI Pt; Pl; Pb; Pr; Ps$ r
2239 // case 'r': // duplicate
2240 // if (this.prefix === '?') {
2241 // this.restorePrivateValues(this.params);
2242 // } else if (this.postfix === '$') {
2243 // this.setAttrInRectangle(this.params);
2245 // this.setScrollRegion(this.params);
2249 // CSI s Save cursor (ANSI.SYS).
2251 // case 's': // duplicate
2252 // if (this.prefix === '?') {
2253 // this.savePrivateValues(this.params);
2255 // this.saveCursor(this.params);
2259 // CSI Ps ; Ps ; Ps t
2260 // CSI Pt; Pl; Pb; Pr; Ps$ t
2264 // if (this.postfix === '$') {
2265 // this.reverseAttrInRectangle(this.params);
2266 // } else if (this.postfix === ' ') {
2267 // this.setWarningBellVolume(this.params);
2269 // if (this.prefix === '>') {
2270 // this.setTitleModeFeature(this.params);
2272 // this.manipulateWindow(this.params);
2277 // CSI u Restore cursor (ANSI.SYS).
2279 // case 'u': // duplicate
2280 // if (this.postfix === ' ') {
2281 // this.setMarginBellVolume(this.params);
2283 // this.restoreCursor(this.params);
2287 // CSI Pt; Pl; Pb; Pr; Pp; Pt; Pl; Pp$ v
2289 // if (this.postfix === '$') {
2290 // this.copyRectagle(this.params);
2294 // CSI Pt ; Pl ; Pb ; Pr ' w
2296 // if (this.postfix === '\'') {
2297 // this.enableFilterRectangle(this.params);
2301 // CSI Ps x Request Terminal Parameters (DECREQTPARM).
2302 // CSI Ps x Select Attribute Change Extent (DECSACE).
2303 // CSI Pc; Pt; Pl; Pb; Pr$ x
2305 // if (this.postfix === '$') {
2306 // this.fillRectangle(this.params);
2308 // this.requestParameters(this.params);
2309 // //this.__(this.params);
2314 // CSI Pt; Pl; Pb; Pr$ z
2316 // if (this.postfix === '\'') {
2317 // this.enableLocatorReporting(this.params);
2318 // } else if (this.postfix === '$') {
2319 // this.eraseRectangle(this.params);
2324 // CSI Pt; Pl; Pb; Pr$ {
2326 // if (this.postfix === '\'') {
2327 // this.setLocatorEvents(this.params);
2328 // } else if (this.postfix === '$') {
2329 // this.selectiveEraseRectangle(this.params);
2335 // if (this.postfix === '\'') {
2336 // this.requestLocatorPosition(this.params);
2341 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
2343 // if (this.postfix === ' ') {
2344 // this.insertColumns(this.params);
2349 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
2351 // if (this.postfix === ' ') {
2352 // this.deleteColumns(this.params);
2357 this.error('Unknown CSI code: %s.', ch
);
2366 if (ch
=== '\x1b' || ch
=== '\x07') {
2367 if (ch
=== '\x1b') i
++;
2369 switch (this.prefix
) {
2370 // User-Defined Keys (DECUDK).
2374 // Request Status String (DECRQSS).
2375 // test: echo -e '\eP$q"p\e\\'
2377 var pt
= this.currentParam
2394 + (this.scrollTop
+ 1)
2396 + (this.scrollBottom
+ 1)
2406 this.error('Unknown DCS Pt: %s.', pt
);
2411 this.send('\x1bP' + +valid
+ '$r' + pt
+ '\x1b\\');
2414 // Set Termcap/Terminfo Data (xterm, experimental).
2418 // Request Termcap/Terminfo String (xterm, experimental)
2419 // Regular xterm does not even respond to this sequence.
2420 // This can cause a small glitch in vim.
2421 // test: echo -ne '\eP+q6b64\e\\'
2423 var pt
= this.currentParam
2426 this.send('\x1bP' + +valid
+ '+r' + pt
+ '\x1b\\');
2430 this.error('Unknown DCS prefix: %s.', this.prefix
);
2434 this.currentParam
= 0;
2436 this.state
= normal
;
2437 } else if (!this.currentParam
) {
2438 if (!this.prefix
&& ch
!== '$' && ch
!== '+') {
2439 this.currentParam
= ch
;
2440 } else if (this.prefix
.length
=== 2) {
2441 this.currentParam
= ch
;
2446 this.currentParam
+= ch
;
2452 if (ch
=== '\x1b' || ch
=== '\x07') {
2453 if (ch
=== '\x1b') i
++;
2454 this.state
= normal
;
2460 this.updateRange(this.y
);
2461 this.queueRefresh(this.refreshStart
, this.refreshEnd
);
2463 if (this.writeBuffer
.length
> 0) {
2464 // Allow renderer to catch up before processing the next batch
2466 setTimeout(function () {
2470 this.writeInProgress
= false;
2475 * Writes text to the terminal, followed by a break line character (\n).
2476 * @param {string} text The text to write to the terminal.
2478 Terminal
.prototype.writeln = function(data
) {
2479 this.write(data
+ '\r\n');
2483 * Attaches a custom keydown handler which is run before keys are processed, giving consumers of
2484 * xterm.js ultimate control as to what keys should be processed by the terminal and what keys
2486 * @param {function} customKeydownHandler The custom KeyboardEvent handler to attach. This is a
2487 * function that takes a KeyboardEvent, allowing consumers to stop propogation and/or prevent
2488 * the default action. The function returns whether the event should be processed by xterm.js.
2490 Terminal
.prototype.attachCustomKeydownHandler = function(customKeydownHandler
) {
2491 this.customKeydownHandler
= customKeydownHandler
;
2495 * Handle a keydown event
2497 * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
2498 * @param {KeyboardEvent} ev The keydown event to be handled.
2500 Terminal
.prototype.keyDown = function(ev
) {
2501 if (this.customKeydownHandler
&& this.customKeydownHandler(ev
) === false) {
2505 if (!this.compositionHelper
.keydown
.bind(this.compositionHelper
)(ev
)) {
2506 if (this.ybase
!== this.ydisp
) {
2507 this.scrollToBottom();
2513 var result
= this.evaluateKeyEscapeSequence(ev
);
2515 if (result
.key
=== '\x13') { // XOFF
2516 this.writeStopped
= true;
2517 } else if (result
.key
=== '\x11') { // XON
2518 this.writeStopped
= false;
2521 if (result
.scrollDisp
) {
2522 this.scrollDisp(result
.scrollDisp
);
2523 return this.cancel(ev
, true);
2526 if (isThirdLevelShift(this, ev
)) {
2530 if (result
.cancel
) {
2531 // The event is canceled at the end already, is this necessary?
2532 this.cancel(ev
, true);
2539 this.emit('keydown', ev
);
2540 this.emit('key', result
.key
, ev
);
2542 this.handler(result
.key
);
2544 return this.cancel(ev
, true);
2548 * Returns an object that determines how a KeyboardEvent should be handled. The key of the
2549 * returned value is the new key code to pass to the PTY.
2551 * Reference: http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
2552 * @param {KeyboardEvent} ev The keyboard event to be translated to key escape sequence.
2554 Terminal
.prototype.evaluateKeyEscapeSequence = function(ev
) {
2556 // Whether to cancel event propogation (NOTE: this may not be needed since the event is
2557 // canceled at the end of keyDown
2559 // The new key even to emit
2561 // The number of characters to scroll, if this is defined it will cancel the event
2562 scrollDisp
: undefined
2564 var modifiers
= ev
.shiftKey
<< 0 | ev
.altKey
<< 1 | ev
.ctrlKey
<< 2 | ev
.metaKey
<< 3;
2565 switch (ev
.keyCode
) {
2569 result
.key
= '\x08'; // ^H
2572 result
.key
= '\x7f'; // ^?
2577 result
.key
= '\x1b[Z';
2581 result
.cancel
= true;
2586 result
.cancel
= true;
2590 result
.key
= '\x1b';
2591 result
.cancel
= true;
2596 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'D';
2597 // HACK: Make Alt + left-arrow behave like Ctrl + left-arrow: move one word backwards
2598 // http://unix.stackexchange.com/a/108106
2599 // macOS uses different escape sequences than linux
2600 if (result
.key
== '\x1b[1;3D') {
2601 result
.key
= (this.browser
.isMac
) ? '\x1bb' : '\x1b[1;5D';
2603 } else if (this.applicationCursor
) {
2604 result
.key
= '\x1bOD';
2606 result
.key
= '\x1b[D';
2612 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'C';
2613 // HACK: Make Alt + right-arrow behave like Ctrl + right-arrow: move one word forward
2614 // http://unix.stackexchange.com/a/108106
2615 // macOS uses different escape sequences than linux
2616 if (result
.key
== '\x1b[1;3C') {
2617 result
.key
= (this.browser
.isMac
) ? '\x1bf' : '\x1b[1;5C';
2619 } else if (this.applicationCursor
) {
2620 result
.key
= '\x1bOC';
2622 result
.key
= '\x1b[C';
2628 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'A';
2629 // HACK: Make Alt + up-arrow behave like Ctrl + up-arrow
2630 // http://unix.stackexchange.com/a/108106
2631 if (result
.key
== '\x1b[1;3A') {
2632 result
.key
= '\x1b[1;5A';
2634 } else if (this.applicationCursor
) {
2635 result
.key
= '\x1bOA';
2637 result
.key
= '\x1b[A';
2643 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'B';
2644 // HACK: Make Alt + down-arrow behave like Ctrl + down-arrow
2645 // http://unix.stackexchange.com/a/108106
2646 if (result
.key
== '\x1b[1;3B') {
2647 result
.key
= '\x1b[1;5B';
2649 } else if (this.applicationCursor
) {
2650 result
.key
= '\x1bOB';
2652 result
.key
= '\x1b[B';
2657 if (!ev
.shiftKey
&& !ev
.ctrlKey
) {
2658 // <Ctrl> or <Shift> + <Insert> are used to
2659 // copy-paste on some systems.
2660 result
.key
= '\x1b[2~';
2666 result
.key
= '\x1b[3;' + (modifiers
+ 1) + '~';
2668 result
.key
= '\x1b[3~';
2674 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'H';
2675 else if (this.applicationCursor
)
2676 result
.key
= '\x1bOH';
2678 result
.key
= '\x1b[H';
2683 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'F';
2684 else if (this.applicationCursor
)
2685 result
.key
= '\x1bOF';
2687 result
.key
= '\x1b[F';
2692 result
.scrollDisp
= -(this.rows
- 1);
2694 result
.key
= '\x1b[5~';
2700 result
.scrollDisp
= this.rows
- 1;
2702 result
.key
= '\x1b[6~';
2708 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'P';
2710 result
.key
= '\x1bOP';
2715 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'Q';
2717 result
.key
= '\x1bOQ';
2722 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'R';
2724 result
.key
= '\x1bOR';
2729 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'S';
2731 result
.key
= '\x1bOS';
2736 result
.key
= '\x1b[15;' + (modifiers
+ 1) + '~';
2738 result
.key
= '\x1b[15~';
2743 result
.key
= '\x1b[17;' + (modifiers
+ 1) + '~';
2745 result
.key
= '\x1b[17~';
2750 result
.key
= '\x1b[18;' + (modifiers
+ 1) + '~';
2752 result
.key
= '\x1b[18~';
2757 result
.key
= '\x1b[19;' + (modifiers
+ 1) + '~';
2759 result
.key
= '\x1b[19~';
2764 result
.key
= '\x1b[20;' + (modifiers
+ 1) + '~';
2766 result
.key
= '\x1b[20~';
2771 result
.key
= '\x1b[21;' + (modifiers
+ 1) + '~';
2773 result
.key
= '\x1b[21~';
2778 result
.key
= '\x1b[23;' + (modifiers
+ 1) + '~';
2780 result
.key
= '\x1b[23~';
2785 result
.key
= '\x1b[24;' + (modifiers
+ 1) + '~';
2787 result
.key
= '\x1b[24~';
2792 if (ev
.ctrlKey
&& !ev
.shiftKey
&& !ev
.altKey
&& !ev
.metaKey
) {
2793 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2794 result
.key
= String
.fromCharCode(ev
.keyCode
- 64);
2795 } else if (ev
.keyCode
=== 32) {
2797 result
.key
= String
.fromCharCode(0);
2798 } else if (ev
.keyCode
>= 51 && ev
.keyCode
<= 55) {
2799 // escape, file sep, group sep, record sep, unit sep
2800 result
.key
= String
.fromCharCode(ev
.keyCode
- 51 + 27);
2801 } else if (ev
.keyCode
=== 56) {
2803 result
.key
= String
.fromCharCode(127);
2804 } else if (ev
.keyCode
=== 219) {
2805 // ^[ - Control Sequence Introducer (CSI)
2806 result
.key
= String
.fromCharCode(27);
2807 } else if (ev
.keyCode
=== 220) {
2808 // ^\ - String Terminator (ST)
2809 result
.key
= String
.fromCharCode(28);
2810 } else if (ev
.keyCode
=== 221) {
2811 // ^] - Operating System Command (OSC)
2812 result
.key
= String
.fromCharCode(29);
2814 } else if (!this.browser
.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) {
2815 // On Mac this is a third level shift. Use <Esc> instead.
2816 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2817 result
.key
= '\x1b' + String
.fromCharCode(ev
.keyCode
+ 32);
2818 } else if (ev
.keyCode
=== 192) {
2819 result
.key
= '\x1b`';
2820 } else if (ev
.keyCode
>= 48 && ev
.keyCode
<= 57) {
2821 result
.key
= '\x1b' + (ev
.keyCode
- 48);
2831 * Set the G level of the terminal
2834 Terminal
.prototype.setgLevel = function(g
) {
2836 this.charset
= this.charsets
[g
];
2840 * Set the charset for the given G level of the terminal
2844 Terminal
.prototype.setgCharset = function(g
, charset
) {
2845 this.charsets
[g
] = charset
;
2846 if (this.glevel
=== g
) {
2847 this.charset
= charset
;
2852 * Handle a keypress event.
2854 * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
2855 * @param {KeyboardEvent} ev The keypress event to be handled.
2857 Terminal
.prototype.keyPress = function(ev
) {
2864 } else if (ev
.which
== null) {
2866 } else if (ev
.which
!== 0 && ev
.charCode
!== 0) {
2873 (ev
.altKey
|| ev
.ctrlKey
|| ev
.metaKey
) && !isThirdLevelShift(this, ev
)
2878 key
= String
.fromCharCode(key
);
2880 this.emit('keypress', key
, ev
);
2881 this.emit('key', key
, ev
);
2889 * Send data for handling to the terminal
2890 * @param {string} data
2892 Terminal
.prototype.send = function(data
) {
2896 setTimeout(function() {
2897 self
.handler(self
.queue
);
2907 * Note: We could do sweet things with webaudio here
2909 Terminal
.prototype.bell = function() {
2910 if (!this.visualBell
) return;
2912 this.element
.style
.borderColor
= 'white';
2913 setTimeout(function() {
2914 self
.element
.style
.borderColor
= '';
2916 if (this.popOnBell
) this.focus();
2920 * Log the current state to the console.
2922 Terminal
.prototype.log = function() {
2923 if (!this.debug
) return;
2924 if (!this.context
.console
|| !this.context
.console
.log
) return;
2925 var args
= Array
.prototype.slice
.call(arguments
);
2926 this.context
.console
.log
.apply(this.context
.console
, args
);
2930 * Log the current state as error to the console.
2932 Terminal
.prototype.error = function() {
2933 if (!this.debug
) return;
2934 if (!this.context
.console
|| !this.context
.console
.error
) return;
2935 var args
= Array
.prototype.slice
.call(arguments
);
2936 this.context
.console
.error
.apply(this.context
.console
, args
);
2940 * Resizes the terminal.
2942 * @param {number} x The number of columns to resize to.
2943 * @param {number} y The number of rows to resize to.
2945 Terminal
.prototype.resize = function(x
, y
) {
2953 if (x
=== this.cols
&& y
=== this.rows
) {
2963 ch
= [this.defAttr
, ' ', 1]; // does xterm use the default attr?
2964 i
= this.lines
.length
;
2966 while (this.lines
.get(i
).length
< x
) {
2967 this.lines
.get(i
).push(ch
);
2971 i
= this.lines
.length
;
2973 while (this.lines
.get(i
).length
> x
) {
2974 this.lines
.get(i
).pop();
2987 // y is rows, not this.y
2988 if (this.lines
.length
< y
+ this.ybase
) {
2989 if (this.ybase
> 0 && this.lines
.length
<= this.ybase
+ this.y
+ addToY
+ 1) {
2990 // There is room above the buffer and there are no empty elements below the line,
2994 if (this.ydisp
> 0) {
2995 // Viewport is at the top of the buffer, must increase downwards
2999 // Add a blank line if there is no buffer left at the top to scroll to, or if there
3000 // are blank lines after the cursor
3001 this.lines
.push(this.blankLine());
3004 if (this.children
.length
< y
) {
3010 if (this.lines
.length
> y
+ this.ybase
) {
3011 if (this.lines
.length
> this.ybase
+ this.y
+ 1) {
3012 // The line is a blank line below the cursor, remove it
3015 // The line is the cursor, scroll down
3020 if (this.children
.length
> y
) {
3021 el
= this.children
.shift();
3023 el
.parentNode
.removeChild(el
);
3029 // Make sure that the cursor stays on screen
3042 this.scrollBottom
= y
- 1;
3044 this.queueRefresh(0, this.rows
- 1);
3048 this.geometry
= [this.cols
, this.rows
];
3049 this.emit('resize', {terminal
: this, cols
: x
, rows
: y
});
3053 * Updates the range of rows to refresh
3054 * @param {number} y The number of rows to refresh next.
3056 Terminal
.prototype.updateRange = function(y
) {
3057 if (y
< this.refreshStart
) this.refreshStart
= y
;
3058 if (y
> this.refreshEnd
) this.refreshEnd
= y
;
3059 // if (y > this.refreshEnd) {
3060 // this.refreshEnd = y;
3061 // if (y > this.rows - 1) {
3062 // this.refreshEnd = this.rows - 1;
3068 * Set the range of refreshing to the maximum value
3070 Terminal
.prototype.maxRange = function() {
3071 this.refreshStart
= 0;
3072 this.refreshEnd
= this.rows
- 1;
3078 * Setup the tab stops.
3081 Terminal
.prototype.setupStops = function(i
) {
3083 if (!this.tabs
[i
]) {
3084 i
= this.prevStop(i
);
3091 for (; i
< this.cols
; i
+= 8) {
3092 this.tabs
[i
] = true;
3098 * Move the cursor to the previous tab stop from the given position (default is current).
3099 * @param {number} x The position to move the cursor to the previous tab stop.
3101 Terminal
.prototype.prevStop = function(x
) {
3102 if (x
== null) x
= this.x
;
3103 while (!this.tabs
[--x
] && x
> 0);
3104 return x
>= this.cols
3111 * Move the cursor one tab stop forward from the given position (default is current).
3112 * @param {number} x The position to move the cursor one tab stop forward.
3114 Terminal
.prototype.nextStop = function(x
) {
3115 if (x
== null) x
= this.x
;
3116 while (!this.tabs
[++x
] && x
< this.cols
);
3117 return x
>= this.cols
3124 * Erase in the identified line everything from "x" to the end of the line (right).
3125 * @param {number} x The column from which to start erasing to the end of the line.
3126 * @param {number} y The line in which to operate.
3128 Terminal
.prototype.eraseRight = function(x
, y
) {
3129 var line
= this.lines
.get(this.ybase
+ y
)
3130 , ch
= [this.eraseAttr(), ' ', 1]; // xterm
3133 for (; x
< this.cols
; x
++) {
3137 this.updateRange(y
);
3143 * Erase in the identified line everything from "x" to the start of the line (left).
3144 * @param {number} x The column from which to start erasing to the start of the line.
3145 * @param {number} y The line in which to operate.
3147 Terminal
.prototype.eraseLeft = function(x
, y
) {
3148 var line
= this.lines
.get(this.ybase
+ y
)
3149 , ch
= [this.eraseAttr(), ' ', 1]; // xterm
3152 while (x
--) line
[x
] = ch
;
3154 this.updateRange(y
);
3158 * Clears the entire buffer, making the prompt line the new first line.
3160 Terminal
.prototype.clear = function() {
3161 if (this.ybase
=== 0 && this.y
=== 0) {
3162 // Don't clear if it's already clear
3165 this.lines
.set(0, this.lines
.get(this.ybase
+ this.y
));
3166 this.lines
.length
= 1;
3170 for (var i
= 1; i
< this.rows
; i
++) {
3171 this.lines
.push(this.blankLine());
3173 this.queueRefresh(0, this.rows
- 1);
3174 this.emit('scroll', this.ydisp
);
3178 * Erase all content in the given line
3179 * @param {number} y The line to erase all of its contents.
3181 Terminal
.prototype.eraseLine = function(y
) {
3182 this.eraseRight(0, y
);
3187 * Return the data array of a blank line
3188 * @param {number} cur First bunch of data for each "blank" character.
3190 Terminal
.prototype.blankLine = function(cur
) {
3195 var ch
= [attr
, ' ', 1] // width defaults to 1 halfwidth character
3199 for (; i
< this.cols
; i
++) {
3208 * If cur return the back color xterm feature attribute. Else return defAttr.
3209 * @param {object} cur
3211 Terminal
.prototype.ch = function(cur
) {
3213 ? [this.eraseAttr(), ' ', 1]
3214 : [this.defAttr
, ' ', 1];
3219 * Evaluate if the current erminal is the given argument.
3220 * @param {object} term The terminal to evaluate
3222 Terminal
.prototype.is = function(term
) {
3223 var name
= this.termName
;
3224 return (name
+ '').indexOf(term
) === 0;
3229 * Emit the 'data' event and populate the given data.
3230 * @param {string} data The data to populate in the event.
3232 Terminal
.prototype.handler = function(data
) {
3233 // Input is being sent to the terminal, the terminal should focus the prompt.
3234 if (this.ybase
!== this.ydisp
) {
3235 this.scrollToBottom();
3237 this.emit('data', data
);
3242 * Emit the 'title' event and populate the given title.
3243 * @param {string} title The title to populate in the event.
3245 Terminal
.prototype.handleTitle = function(title
) {
3247 * This event is emitted when the title of the terminal is changed
3248 * from inside the terminal. The parameter is the new title.
3252 this.emit('title', title
);
3261 * ESC D Index (IND is 0x84).
3263 Terminal
.prototype.index = function() {
3265 if (this.y
> this.scrollBottom
) {
3269 this.state
= normal
;
3274 * ESC M Reverse Index (RI is 0x8d).
3276 * Move the cursor up one row, inserting a new blank line if necessary.
3278 Terminal
.prototype.reverseIndex = function() {
3280 if (this.y
=== this.scrollTop
) {
3281 // possibly move the code below to term.reverseScroll();
3282 // test: echo -ne '\e[1;1H\e[44m\eM\e[0m'
3283 // blankLine(true) is xterm/linux behavior
3284 this.lines
.shiftElements(this.y
+ this.ybase
, this.rows
- 1, 1);
3285 this.lines
.set(this.y
+ this.ybase
, this.blankLine(true));
3286 this.updateRange(this.scrollTop
);
3287 this.updateRange(this.scrollBottom
);
3291 this.state
= normal
;
3296 * ESC c Full Reset (RIS).
3298 Terminal
.prototype.reset = function() {
3299 this.options
.rows
= this.rows
;
3300 this.options
.cols
= this.cols
;
3301 var customKeydownHandler
= this.customKeydownHandler
;
3302 Terminal
.call(this, this.options
);
3303 this.customKeydownHandler
= customKeydownHandler
;
3304 this.queueRefresh(0, this.rows
- 1);
3305 this.viewport
.syncScrollArea();
3310 * ESC H Tab Set (HTS is 0x88).
3312 Terminal
.prototype.tabSet = function() {
3313 this.tabs
[this.x
] = true;
3314 this.state
= normal
;
3324 * Cursor Up Ps Times (default = 1) (CUU).
3326 Terminal
.prototype.cursorUp = function(params
) {
3327 var param
= params
[0];
3328 if (param
< 1) param
= 1;
3330 if (this.y
< 0) this.y
= 0;
3336 * Cursor Down Ps Times (default = 1) (CUD).
3338 Terminal
.prototype.cursorDown = function(params
) {
3339 var param
= params
[0];
3340 if (param
< 1) param
= 1;
3342 if (this.y
>= this.rows
) {
3343 this.y
= this.rows
- 1;
3350 * Cursor Forward Ps Times (default = 1) (CUF).
3352 Terminal
.prototype.cursorForward = function(params
) {
3353 var param
= params
[0];
3354 if (param
< 1) param
= 1;
3356 if (this.x
>= this.cols
) {
3357 this.x
= this.cols
- 1;
3364 * Cursor Backward Ps Times (default = 1) (CUB).
3366 Terminal
.prototype.cursorBackward = function(params
) {
3367 var param
= params
[0];
3368 if (param
< 1) param
= 1;
3370 if (this.x
< 0) this.x
= 0;
3376 * Cursor Position [row;column] (default = [1,1]) (CUP).
3378 Terminal
.prototype.cursorPos = function(params
) {
3381 row
= params
[0] - 1;
3383 if (params
.length
>= 2) {
3384 col
= params
[1] - 1;
3391 } else if (row
>= this.rows
) {
3392 row
= this.rows
- 1;
3397 } else if (col
>= this.cols
) {
3398 col
= this.cols
- 1;
3407 * CSI Ps J Erase in Display (ED).
3408 * Ps = 0 -> Erase Below (default).
3409 * Ps = 1 -> Erase Above.
3410 * Ps = 2 -> Erase All.
3411 * Ps = 3 -> Erase Saved Lines (xterm).
3413 * Erase in Display (DECSED).
3414 * Ps = 0 -> Selective Erase Below (default).
3415 * Ps = 1 -> Selective Erase Above.
3416 * Ps = 2 -> Selective Erase All.
3418 Terminal
.prototype.eraseInDisplay = function(params
) {
3420 switch (params
[0]) {
3422 this.eraseRight(this.x
, this.y
);
3424 for (; j
< this.rows
; j
++) {
3429 this.eraseLeft(this.x
, this.y
);
3437 while (j
--) this.eraseLine(j
);
3447 * CSI Ps K Erase in Line (EL).
3448 * Ps = 0 -> Erase to Right (default).
3449 * Ps = 1 -> Erase to Left.
3450 * Ps = 2 -> Erase All.
3452 * Erase in Line (DECSEL).
3453 * Ps = 0 -> Selective Erase to Right (default).
3454 * Ps = 1 -> Selective Erase to Left.
3455 * Ps = 2 -> Selective Erase All.
3457 Terminal
.prototype.eraseInLine = function(params
) {
3458 switch (params
[0]) {
3460 this.eraseRight(this.x
, this.y
);
3463 this.eraseLeft(this.x
, this.y
);
3466 this.eraseLine(this.y
);
3473 * CSI Pm m Character Attributes (SGR).
3474 * Ps = 0 -> Normal (default).
3476 * Ps = 4 -> Underlined.
3477 * Ps = 5 -> Blink (appears as Bold).
3478 * Ps = 7 -> Inverse.
3479 * Ps = 8 -> Invisible, i.e., hidden (VT300).
3480 * Ps = 2 2 -> Normal (neither bold nor faint).
3481 * Ps = 2 4 -> Not underlined.
3482 * Ps = 2 5 -> Steady (not blinking).
3483 * Ps = 2 7 -> Positive (not inverse).
3484 * Ps = 2 8 -> Visible, i.e., not hidden (VT300).
3485 * Ps = 3 0 -> Set foreground color to Black.
3486 * Ps = 3 1 -> Set foreground color to Red.
3487 * Ps = 3 2 -> Set foreground color to Green.
3488 * Ps = 3 3 -> Set foreground color to Yellow.
3489 * Ps = 3 4 -> Set foreground color to Blue.
3490 * Ps = 3 5 -> Set foreground color to Magenta.
3491 * Ps = 3 6 -> Set foreground color to Cyan.
3492 * Ps = 3 7 -> Set foreground color to White.
3493 * Ps = 3 9 -> Set foreground color to default (original).
3494 * Ps = 4 0 -> Set background color to Black.
3495 * Ps = 4 1 -> Set background color to Red.
3496 * Ps = 4 2 -> Set background color to Green.
3497 * Ps = 4 3 -> Set background color to Yellow.
3498 * Ps = 4 4 -> Set background color to Blue.
3499 * Ps = 4 5 -> Set background color to Magenta.
3500 * Ps = 4 6 -> Set background color to Cyan.
3501 * Ps = 4 7 -> Set background color to White.
3502 * Ps = 4 9 -> Set background color to default (original).
3504 * If 16-color support is compiled, the following apply. Assume
3505 * that xterm's resources are set so that the ISO color codes are
3506 * the first 8 of a set of 16. Then the aixterm colors are the
3507 * bright versions of the ISO colors:
3508 * Ps = 9 0 -> Set foreground color to Black.
3509 * Ps = 9 1 -> Set foreground color to Red.
3510 * Ps = 9 2 -> Set foreground color to Green.
3511 * Ps = 9 3 -> Set foreground color to Yellow.
3512 * Ps = 9 4 -> Set foreground color to Blue.
3513 * Ps = 9 5 -> Set foreground color to Magenta.
3514 * Ps = 9 6 -> Set foreground color to Cyan.
3515 * Ps = 9 7 -> Set foreground color to White.
3516 * Ps = 1 0 0 -> Set background color to Black.
3517 * Ps = 1 0 1 -> Set background color to Red.
3518 * Ps = 1 0 2 -> Set background color to Green.
3519 * Ps = 1 0 3 -> Set background color to Yellow.
3520 * Ps = 1 0 4 -> Set background color to Blue.
3521 * Ps = 1 0 5 -> Set background color to Magenta.
3522 * Ps = 1 0 6 -> Set background color to Cyan.
3523 * Ps = 1 0 7 -> Set background color to White.
3525 * If xterm is compiled with the 16-color support disabled, it
3526 * supports the following, from rxvt:
3527 * Ps = 1 0 0 -> Set foreground and background color to
3530 * If 88- or 256-color support is compiled, the following apply.
3531 * Ps = 3 8 ; 5 ; Ps -> Set foreground color to the second
3533 * Ps = 4 8 ; 5 ; Ps -> Set background color to the second
3536 Terminal
.prototype.charAttributes = function(params
) {
3537 // Optimize a single SGR0.
3538 if (params
.length
=== 1 && params
[0] === 0) {
3539 this.curAttr
= this.defAttr
;
3543 var l
= params
.length
3545 , flags
= this.curAttr
>> 18
3546 , fg
= (this.curAttr
>> 9) & 0x1ff
3547 , bg
= this.curAttr
& 0x1ff
3550 for (; i
< l
; i
++) {
3552 if (p
>= 30 && p
<= 37) {
3555 } else if (p
>= 40 && p
<= 47) {
3558 } else if (p
>= 90 && p
<= 97) {
3562 } else if (p
>= 100 && p
<= 107) {
3566 } else if (p
=== 0) {
3568 flags
= this.defAttr
>> 18;
3569 fg
= (this.defAttr
>> 9) & 0x1ff;
3570 bg
= this.defAttr
& 0x1ff;
3574 } else if (p
=== 1) {
3577 } else if (p
=== 4) {
3580 } else if (p
=== 5) {
3583 } else if (p
=== 7) {
3584 // inverse and positive
3585 // test with: echo -e '\e[31m\e[42mhello\e[7mworld\e[27mhi\e[m'
3587 } else if (p
=== 8) {
3590 } else if (p
=== 22) {
3593 } else if (p
=== 24) {
3596 } else if (p
=== 25) {
3599 } else if (p
=== 27) {
3602 } else if (p
=== 28) {
3605 } else if (p
=== 39) {
3607 fg
= (this.defAttr
>> 9) & 0x1ff;
3608 } else if (p
=== 49) {
3610 bg
= this.defAttr
& 0x1ff;
3611 } else if (p
=== 38) {
3613 if (params
[i
+ 1] === 2) {
3617 params
[i
+ 1] & 0xff,
3618 params
[i
+ 2] & 0xff);
3619 if (fg
=== -1) fg
= 0x1ff;
3621 } else if (params
[i
+ 1] === 5) {
3623 p
= params
[i
] & 0xff;
3626 } else if (p
=== 48) {
3628 if (params
[i
+ 1] === 2) {
3632 params
[i
+ 1] & 0xff,
3633 params
[i
+ 2] & 0xff);
3634 if (bg
=== -1) bg
= 0x1ff;
3636 } else if (params
[i
+ 1] === 5) {
3638 p
= params
[i
] & 0xff;
3641 } else if (p
=== 100) {
3643 fg
= (this.defAttr
>> 9) & 0x1ff;
3644 bg
= this.defAttr
& 0x1ff;
3646 this.error('Unknown SGR attribute: %d.', p
);
3650 this.curAttr
= (flags
<< 18) | (fg
<< 9) | bg
;
3655 * CSI Ps n Device Status Report (DSR).
3656 * Ps = 5 -> Status Report. Result (``OK'') is
3658 * Ps = 6 -> Report Cursor Position (CPR) [row;column].
3662 * Device Status Report (DSR, DEC-specific).
3663 * Ps = 6 -> Report Cursor Position (CPR) [row;column] as CSI
3664 * ? r ; c R (assumes page is zero).
3665 * Ps = 1 5 -> Report Printer status as CSI ? 1 0 n (ready).
3666 * or CSI ? 1 1 n (not ready).
3667 * Ps = 2 5 -> Report UDK status as CSI ? 2 0 n (unlocked)
3668 * or CSI ? 2 1 n (locked).
3669 * Ps = 2 6 -> Report Keyboard status as
3670 * CSI ? 2 7 ; 1 ; 0 ; 0 n (North American).
3671 * The last two parameters apply to VT400 & up, and denote key-
3672 * board ready and LK01 respectively.
3673 * Ps = 5 3 -> Report Locator status as
3674 * CSI ? 5 3 n Locator available, if compiled-in, or
3675 * CSI ? 5 0 n No Locator, if not.
3677 Terminal
.prototype.deviceStatus = function(params
) {
3679 switch (params
[0]) {
3682 this.send('\x1b[0n');
3693 } else if (this.prefix
=== '?') {
3694 // modern xterm doesnt seem to
3695 // respond to any of these except ?6, 6, and 5
3696 switch (params
[0]) {
3707 // this.send('\x1b[?11n');
3710 // dont support user defined keys
3711 // this.send('\x1b[?21n');
3714 // north american keyboard
3715 // this.send('\x1b[?27;1;0;0n');
3718 // no dec locator/mouse
3719 // this.send('\x1b[?50n');
3732 * Insert Ps (Blank) Character(s) (default = 1) (ICH).
3734 Terminal
.prototype.insertChars = function(params
) {
3735 var param
, row
, j
, ch
;
3738 if (param
< 1) param
= 1;
3740 row
= this.y
+ this.ybase
;
3742 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3744 while (param
-- && j
< this.cols
) {
3745 this.lines
.get(row
).splice(j
++, 0, ch
);
3746 this.lines
.get(row
).pop();
3752 * Cursor Next Line Ps Times (default = 1) (CNL).
3753 * same as CSI Ps B ?
3755 Terminal
.prototype.cursorNextLine = function(params
) {
3756 var param
= params
[0];
3757 if (param
< 1) param
= 1;
3759 if (this.y
>= this.rows
) {
3760 this.y
= this.rows
- 1;
3768 * Cursor Preceding Line Ps Times (default = 1) (CNL).
3771 Terminal
.prototype.cursorPrecedingLine = function(params
) {
3772 var param
= params
[0];
3773 if (param
< 1) param
= 1;
3775 if (this.y
< 0) this.y
= 0;
3782 * Cursor Character Absolute [column] (default = [row,1]) (CHA).
3784 Terminal
.prototype.cursorCharAbsolute = function(params
) {
3785 var param
= params
[0];
3786 if (param
< 1) param
= 1;
3793 * Insert Ps Line(s) (default = 1) (IL).
3795 Terminal
.prototype.insertLines = function(params
) {
3799 if (param
< 1) param
= 1;
3800 row
= this.y
+ this.ybase
;
3802 j
= this.rows
- 1 - this.scrollBottom
;
3803 j
= this.rows
- 1 + this.ybase
- j
+ 1;
3806 if (this.lines
.length
=== this.lines
.maxLength
) {
3807 // Trim the start of lines to make room for the new line
3808 this.lines
.trimStart(1);
3814 // test: echo -e '\e[44m\e[1L\e[0m'
3815 // blankLine(true) - xterm/linux behavior
3816 this.lines
.splice(row
, 0, this.blankLine(true));
3817 this.lines
.splice(j
, 1);
3821 this.updateRange(this.y
);
3822 this.updateRange(this.scrollBottom
);
3828 * Delete Ps Line(s) (default = 1) (DL).
3830 Terminal
.prototype.deleteLines = function(params
) {
3834 if (param
< 1) param
= 1;
3835 row
= this.y
+ this.ybase
;
3837 j
= this.rows
- 1 - this.scrollBottom
;
3838 j
= this.rows
- 1 + this.ybase
- j
;
3841 if (this.lines
.length
=== this.lines
.maxLength
) {
3842 // Trim the start of lines to make room for the new line
3843 this.lines
.trimStart(1);
3847 // test: echo -e '\e[44m\e[1M\e[0m'
3848 // blankLine(true) - xterm/linux behavior
3849 this.lines
.splice(j
+ 1, 0, this.blankLine(true));
3850 this.lines
.splice(row
, 1);
3854 this.updateRange(this.y
);
3855 this.updateRange(this.scrollBottom
);
3861 * Delete Ps Character(s) (default = 1) (DCH).
3863 Terminal
.prototype.deleteChars = function(params
) {
3867 if (param
< 1) param
= 1;
3869 row
= this.y
+ this.ybase
;
3870 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3873 this.lines
.get(row
).splice(this.x
, 1);
3874 this.lines
.get(row
).push(ch
);
3880 * Erase Ps Character(s) (default = 1) (ECH).
3882 Terminal
.prototype.eraseChars = function(params
) {
3883 var param
, row
, j
, ch
;
3886 if (param
< 1) param
= 1;
3888 row
= this.y
+ this.ybase
;
3890 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3892 while (param
-- && j
< this.cols
) {
3893 this.lines
.get(row
)[j
++] = ch
;
3898 * CSI Pm ` Character Position Absolute
3899 * [column] (default = [row,1]) (HPA).
3901 Terminal
.prototype.charPosAbsolute = function(params
) {
3902 var param
= params
[0];
3903 if (param
< 1) param
= 1;
3905 if (this.x
>= this.cols
) {
3906 this.x
= this.cols
- 1;
3913 * Horizontal Position Relative
3916 Terminal
.prototype.HPositionRelative = function(params
) {
3917 var param
= params
[0];
3918 if (param
< 1) param
= 1;
3920 if (this.x
>= this.cols
) {
3921 this.x
= this.cols
- 1;
3927 * CSI Ps c Send Device Attributes (Primary DA).
3928 * Ps = 0 or omitted -> request attributes from terminal. The
3929 * response depends on the decTerminalID resource setting.
3930 * -> CSI ? 1 ; 2 c (``VT100 with Advanced Video Option'')
3931 * -> CSI ? 1 ; 0 c (``VT101 with No Options'')
3932 * -> CSI ? 6 c (``VT102'')
3933 * -> CSI ? 6 0 ; 1 ; 2 ; 6 ; 8 ; 9 ; 1 5 ; c (``VT220'')
3934 * The VT100-style response parameters do not mean anything by
3935 * themselves. VT220 parameters do, telling the host what fea-
3936 * tures the terminal supports:
3937 * Ps = 1 -> 132-columns.
3938 * Ps = 2 -> Printer.
3939 * Ps = 6 -> Selective erase.
3940 * Ps = 8 -> User-defined keys.
3941 * Ps = 9 -> National replacement character sets.
3942 * Ps = 1 5 -> Technical characters.
3943 * Ps = 2 2 -> ANSI color, e.g., VT525.
3944 * Ps = 2 9 -> ANSI text locator (i.e., DEC Locator mode).
3946 * Send Device Attributes (Secondary DA).
3947 * Ps = 0 or omitted -> request the terminal's identification
3948 * code. The response depends on the decTerminalID resource set-
3949 * ting. It should apply only to VT220 and up, but xterm extends
3951 * -> CSI > Pp ; Pv ; Pc c
3952 * where Pp denotes the terminal type
3953 * Pp = 0 -> ``VT100''.
3954 * Pp = 1 -> ``VT220''.
3955 * and Pv is the firmware version (for xterm, this was originally
3956 * the XFree86 patch number, starting with 95). In a DEC termi-
3957 * nal, Pc indicates the ROM cartridge registration number and is
3960 * xterm/charproc.c - line 2012, for more information.
3961 * vim responds with ^[[?0c or ^[[?1c after the terminal's response (?)
3963 Terminal
.prototype.sendDeviceAttributes = function(params
) {
3964 if (params
[0] > 0) return;
3967 if (this.is('xterm')
3968 || this.is('rxvt-unicode')
3969 || this.is('screen')) {
3970 this.send('\x1b[?1;2c');
3971 } else if (this.is('linux')) {
3972 this.send('\x1b[?6c');
3974 } else if (this.prefix
=== '>') {
3976 // seem to spit this
3977 // out around ~370 times (?).
3978 if (this.is('xterm')) {
3979 this.send('\x1b[>0;276;0c');
3980 } else if (this.is('rxvt-unicode')) {
3981 this.send('\x1b[>85;95;0c');
3982 } else if (this.is('linux')) {
3983 // not supported by linux console.
3984 // linux console echoes parameters.
3985 this.send(params
[0] + 'c');
3986 } else if (this.is('screen')) {
3987 this.send('\x1b[>83;40003;0c');
3995 * Line Position Absolute [row] (default = [1,column]) (VPA).
3997 Terminal
.prototype.linePosAbsolute = function(params
) {
3998 var param
= params
[0];
3999 if (param
< 1) param
= 1;
4001 if (this.y
>= this.rows
) {
4002 this.y
= this.rows
- 1;
4008 * 145 65 e * VPR - Vertical Position Relative
4011 Terminal
.prototype.VPositionRelative = function(params
) {
4012 var param
= params
[0];
4013 if (param
< 1) param
= 1;
4015 if (this.y
>= this.rows
) {
4016 this.y
= this.rows
- 1;
4023 * Horizontal and Vertical Position [row;column] (default =
4026 Terminal
.prototype.HVPosition = function(params
) {
4027 if (params
[0] < 1) params
[0] = 1;
4028 if (params
[1] < 1) params
[1] = 1;
4030 this.y
= params
[0] - 1;
4031 if (this.y
>= this.rows
) {
4032 this.y
= this.rows
- 1;
4035 this.x
= params
[1] - 1;
4036 if (this.x
>= this.cols
) {
4037 this.x
= this.cols
- 1;
4043 * CSI Pm h Set Mode (SM).
4044 * Ps = 2 -> Keyboard Action Mode (AM).
4045 * Ps = 4 -> Insert Mode (IRM).
4046 * Ps = 1 2 -> Send/receive (SRM).
4047 * Ps = 2 0 -> Automatic Newline (LNM).
4049 * DEC Private Mode Set (DECSET).
4050 * Ps = 1 -> Application Cursor Keys (DECCKM).
4051 * Ps = 2 -> Designate USASCII for character sets G0-G3
4052 * (DECANM), and set VT100 mode.
4053 * Ps = 3 -> 132 Column Mode (DECCOLM).
4054 * Ps = 4 -> Smooth (Slow) Scroll (DECSCLM).
4055 * Ps = 5 -> Reverse Video (DECSCNM).
4056 * Ps = 6 -> Origin Mode (DECOM).
4057 * Ps = 7 -> Wraparound Mode (DECAWM).
4058 * Ps = 8 -> Auto-repeat Keys (DECARM).
4059 * Ps = 9 -> Send Mouse X & Y on button press. See the sec-
4060 * tion Mouse Tracking.
4061 * Ps = 1 0 -> Show toolbar (rxvt).
4062 * Ps = 1 2 -> Start Blinking Cursor (att610).
4063 * Ps = 1 8 -> Print form feed (DECPFF).
4064 * Ps = 1 9 -> Set print extent to full screen (DECPEX).
4065 * Ps = 2 5 -> Show Cursor (DECTCEM).
4066 * Ps = 3 0 -> Show scrollbar (rxvt).
4067 * Ps = 3 5 -> Enable font-shifting functions (rxvt).
4068 * Ps = 3 8 -> Enter Tektronix Mode (DECTEK).
4069 * Ps = 4 0 -> Allow 80 -> 132 Mode.
4070 * Ps = 4 1 -> more(1) fix (see curses resource).
4071 * Ps = 4 2 -> Enable Nation Replacement Character sets (DECN-
4073 * Ps = 4 4 -> Turn On Margin Bell.
4074 * Ps = 4 5 -> Reverse-wraparound Mode.
4075 * Ps = 4 6 -> Start Logging. This is normally disabled by a
4076 * compile-time option.
4077 * Ps = 4 7 -> Use Alternate Screen Buffer. (This may be dis-
4078 * abled by the titeInhibit resource).
4079 * Ps = 6 6 -> Application keypad (DECNKM).
4080 * Ps = 6 7 -> Backarrow key sends backspace (DECBKM).
4081 * Ps = 1 0 0 0 -> Send Mouse X & Y on button press and
4082 * release. See the section Mouse Tracking.
4083 * Ps = 1 0 0 1 -> Use Hilite Mouse Tracking.
4084 * Ps = 1 0 0 2 -> Use Cell Motion Mouse Tracking.
4085 * Ps = 1 0 0 3 -> Use All Motion Mouse Tracking.
4086 * Ps = 1 0 0 4 -> Send FocusIn/FocusOut events.
4087 * Ps = 1 0 0 5 -> Enable Extended Mouse Mode.
4088 * Ps = 1 0 1 0 -> Scroll to bottom on tty output (rxvt).
4089 * Ps = 1 0 1 1 -> Scroll to bottom on key press (rxvt).
4090 * Ps = 1 0 3 4 -> Interpret "meta" key, sets eighth bit.
4091 * (enables the eightBitInput resource).
4092 * Ps = 1 0 3 5 -> Enable special modifiers for Alt and Num-
4093 * Lock keys. (This enables the numLock resource).
4094 * Ps = 1 0 3 6 -> Send ESC when Meta modifies a key. (This
4095 * enables the metaSendsEscape resource).
4096 * Ps = 1 0 3 7 -> Send DEL from the editing-keypad Delete
4098 * Ps = 1 0 3 9 -> Send ESC when Alt modifies a key. (This
4099 * enables the altSendsEscape resource).
4100 * Ps = 1 0 4 0 -> Keep selection even if not highlighted.
4101 * (This enables the keepSelection resource).
4102 * Ps = 1 0 4 1 -> Use the CLIPBOARD selection. (This enables
4103 * the selectToClipboard resource).
4104 * Ps = 1 0 4 2 -> Enable Urgency window manager hint when
4105 * Control-G is received. (This enables the bellIsUrgent
4107 * Ps = 1 0 4 3 -> Enable raising of the window when Control-G
4108 * is received. (enables the popOnBell resource).
4109 * Ps = 1 0 4 7 -> Use Alternate Screen Buffer. (This may be
4110 * disabled by the titeInhibit resource).
4111 * Ps = 1 0 4 8 -> Save cursor as in DECSC. (This may be dis-
4112 * abled by the titeInhibit resource).
4113 * Ps = 1 0 4 9 -> Save cursor as in DECSC and use Alternate
4114 * Screen Buffer, clearing it first. (This may be disabled by
4115 * the titeInhibit resource). This combines the effects of the 1
4116 * 0 4 7 and 1 0 4 8 modes. Use this with terminfo-based
4117 * applications rather than the 4 7 mode.
4118 * Ps = 1 0 5 0 -> Set terminfo/termcap function-key mode.
4119 * Ps = 1 0 5 1 -> Set Sun function-key mode.
4120 * Ps = 1 0 5 2 -> Set HP function-key mode.
4121 * Ps = 1 0 5 3 -> Set SCO function-key mode.
4122 * Ps = 1 0 6 0 -> Set legacy keyboard emulation (X11R6).
4123 * Ps = 1 0 6 1 -> Set VT220 keyboard emulation.
4124 * Ps = 2 0 0 4 -> Set bracketed paste mode.
4126 * http: *vt100.net/docs/vt220-rm/chapter4.html
4128 Terminal
.prototype.setMode = function(params
) {
4129 if (typeof params
=== 'object') {
4130 var l
= params
.length
4133 for (; i
< l
; i
++) {
4134 this.setMode(params
[i
]);
4143 this.insertMode
= true;
4146 //this.convertEol = true;
4149 } else if (this.prefix
=== '?') {
4152 this.applicationCursor
= true;
4155 this.setgCharset(0, Terminal
.charsets
.US
);
4156 this.setgCharset(1, Terminal
.charsets
.US
);
4157 this.setgCharset(2, Terminal
.charsets
.US
);
4158 this.setgCharset(3, Terminal
.charsets
.US
);
4159 // set VT100 mode here
4161 case 3: // 132 col mode
4162 this.savedCols
= this.cols
;
4163 this.resize(132, this.rows
);
4166 this.originMode
= true;
4169 this.wraparoundMode
= true;
4172 // this.cursorBlink = true;
4175 this.log('Serial port requested application keypad.');
4176 this.applicationKeypad
= true;
4177 this.viewport
.syncScrollArea();
4179 case 9: // X10 Mouse
4180 // no release, no motion, no wheel, no modifiers.
4181 case 1000: // vt200 mouse
4183 // no modifiers, except control on the wheel.
4184 case 1002: // button event mouse
4185 case 1003: // any event mouse
4186 // any event - sends motion events,
4187 // even if there is no button held down.
4188 this.x10Mouse
= params
=== 9;
4189 this.vt200Mouse
= params
=== 1000;
4190 this.normalMouse
= params
> 1000;
4191 this.mouseEvents
= true;
4192 this.element
.style
.cursor
= 'default';
4193 this.log('Binding to mouse events.');
4195 case 1004: // send focusin/focusout events
4198 this.sendFocus
= true;
4200 case 1005: // utf8 ext mode mouse
4201 this.utfMouse
= true;
4202 // for wide terminals
4203 // simply encodes large values as utf8 characters
4205 case 1006: // sgr ext mode mouse
4206 this.sgrMouse
= true;
4207 // for wide terminals
4208 // does not add 32 to fields
4209 // press: ^[[<b;x;yM
4210 // release: ^[[<b;x;ym
4212 case 1015: // urxvt ext mode mouse
4213 this.urxvtMouse
= true;
4214 // for wide terminals
4215 // numbers for fields
4217 // motion: ^[[b;x;yT
4219 case 25: // show cursor
4220 this.cursorHidden
= false;
4222 case 1049: // alt screen buffer cursor
4223 //this.saveCursor();
4225 case 47: // alt screen buffer
4226 case 1047: // alt screen buffer
4234 scrollTop
: this.scrollTop
,
4235 scrollBottom
: this.scrollBottom
,
4237 // XXX save charset(s) here?
4238 // charset: this.charset,
4239 // glevel: this.glevel,
4240 // charsets: this.charsets
4243 this.viewport
.syncScrollArea();
4244 this.normal
= normal
;
4253 * CSI Pm l Reset Mode (RM).
4254 * Ps = 2 -> Keyboard Action Mode (AM).
4255 * Ps = 4 -> Replace Mode (IRM).
4256 * Ps = 1 2 -> Send/receive (SRM).
4257 * Ps = 2 0 -> Normal Linefeed (LNM).
4259 * DEC Private Mode Reset (DECRST).
4260 * Ps = 1 -> Normal Cursor Keys (DECCKM).
4261 * Ps = 2 -> Designate VT52 mode (DECANM).
4262 * Ps = 3 -> 80 Column Mode (DECCOLM).
4263 * Ps = 4 -> Jump (Fast) Scroll (DECSCLM).
4264 * Ps = 5 -> Normal Video (DECSCNM).
4265 * Ps = 6 -> Normal Cursor Mode (DECOM).
4266 * Ps = 7 -> No Wraparound Mode (DECAWM).
4267 * Ps = 8 -> No Auto-repeat Keys (DECARM).
4268 * Ps = 9 -> Don't send Mouse X & Y on button press.
4269 * Ps = 1 0 -> Hide toolbar (rxvt).
4270 * Ps = 1 2 -> Stop Blinking Cursor (att610).
4271 * Ps = 1 8 -> Don't print form feed (DECPFF).
4272 * Ps = 1 9 -> Limit print to scrolling region (DECPEX).
4273 * Ps = 2 5 -> Hide Cursor (DECTCEM).
4274 * Ps = 3 0 -> Don't show scrollbar (rxvt).
4275 * Ps = 3 5 -> Disable font-shifting functions (rxvt).
4276 * Ps = 4 0 -> Disallow 80 -> 132 Mode.
4277 * Ps = 4 1 -> No more(1) fix (see curses resource).
4278 * Ps = 4 2 -> Disable Nation Replacement Character sets (DEC-
4280 * Ps = 4 4 -> Turn Off Margin Bell.
4281 * Ps = 4 5 -> No Reverse-wraparound Mode.
4282 * Ps = 4 6 -> Stop Logging. (This is normally disabled by a
4283 * compile-time option).
4284 * Ps = 4 7 -> Use Normal Screen Buffer.
4285 * Ps = 6 6 -> Numeric keypad (DECNKM).
4286 * Ps = 6 7 -> Backarrow key sends delete (DECBKM).
4287 * Ps = 1 0 0 0 -> Don't send Mouse X & Y on button press and
4288 * release. See the section Mouse Tracking.
4289 * Ps = 1 0 0 1 -> Don't use Hilite Mouse Tracking.
4290 * Ps = 1 0 0 2 -> Don't use Cell Motion Mouse Tracking.
4291 * Ps = 1 0 0 3 -> Don't use All Motion Mouse Tracking.
4292 * Ps = 1 0 0 4 -> Don't send FocusIn/FocusOut events.
4293 * Ps = 1 0 0 5 -> Disable Extended Mouse Mode.
4294 * Ps = 1 0 1 0 -> Don't scroll to bottom on tty output
4296 * Ps = 1 0 1 1 -> Don't scroll to bottom on key press (rxvt).
4297 * Ps = 1 0 3 4 -> Don't interpret "meta" key. (This disables
4298 * the eightBitInput resource).
4299 * Ps = 1 0 3 5 -> Disable special modifiers for Alt and Num-
4300 * Lock keys. (This disables the numLock resource).
4301 * Ps = 1 0 3 6 -> Don't send ESC when Meta modifies a key.
4302 * (This disables the metaSendsEscape resource).
4303 * Ps = 1 0 3 7 -> Send VT220 Remove from the editing-keypad
4305 * Ps = 1 0 3 9 -> Don't send ESC when Alt modifies a key.
4306 * (This disables the altSendsEscape resource).
4307 * Ps = 1 0 4 0 -> Do not keep selection when not highlighted.
4308 * (This disables the keepSelection resource).
4309 * Ps = 1 0 4 1 -> Use the PRIMARY selection. (This disables
4310 * the selectToClipboard resource).
4311 * Ps = 1 0 4 2 -> Disable Urgency window manager hint when
4312 * Control-G is received. (This disables the bellIsUrgent
4314 * Ps = 1 0 4 3 -> Disable raising of the window when Control-
4315 * G is received. (This disables the popOnBell resource).
4316 * Ps = 1 0 4 7 -> Use Normal Screen Buffer, clearing screen
4317 * first if in the Alternate Screen. (This may be disabled by
4318 * the titeInhibit resource).
4319 * Ps = 1 0 4 8 -> Restore cursor as in DECRC. (This may be
4320 * disabled by the titeInhibit resource).
4321 * Ps = 1 0 4 9 -> Use Normal Screen Buffer and restore cursor
4322 * as in DECRC. (This may be disabled by the titeInhibit
4323 * resource). This combines the effects of the 1 0 4 7 and 1 0
4324 * 4 8 modes. Use this with terminfo-based applications rather
4325 * than the 4 7 mode.
4326 * Ps = 1 0 5 0 -> Reset terminfo/termcap function-key mode.
4327 * Ps = 1 0 5 1 -> Reset Sun function-key mode.
4328 * Ps = 1 0 5 2 -> Reset HP function-key mode.
4329 * Ps = 1 0 5 3 -> Reset SCO function-key mode.
4330 * Ps = 1 0 6 0 -> Reset legacy keyboard emulation (X11R6).
4331 * Ps = 1 0 6 1 -> Reset keyboard emulation to Sun/PC style.
4332 * Ps = 2 0 0 4 -> Reset bracketed paste mode.
4334 Terminal
.prototype.resetMode = function(params
) {
4335 if (typeof params
=== 'object') {
4336 var l
= params
.length
4339 for (; i
< l
; i
++) {
4340 this.resetMode(params
[i
]);
4349 this.insertMode
= false;
4352 //this.convertEol = false;
4355 } else if (this.prefix
=== '?') {
4358 this.applicationCursor
= false;
4361 if (this.cols
=== 132 && this.savedCols
) {
4362 this.resize(this.savedCols
, this.rows
);
4364 delete this.savedCols
;
4367 this.originMode
= false;
4370 this.wraparoundMode
= false;
4373 // this.cursorBlink = false;
4376 this.log('Switching back to normal keypad.');
4377 this.applicationKeypad
= false;
4378 this.viewport
.syncScrollArea();
4380 case 9: // X10 Mouse
4381 case 1000: // vt200 mouse
4382 case 1002: // button event mouse
4383 case 1003: // any event mouse
4384 this.x10Mouse
= false;
4385 this.vt200Mouse
= false;
4386 this.normalMouse
= false;
4387 this.mouseEvents
= false;
4388 this.element
.style
.cursor
= '';
4390 case 1004: // send focusin/focusout events
4391 this.sendFocus
= false;
4393 case 1005: // utf8 ext mode mouse
4394 this.utfMouse
= false;
4396 case 1006: // sgr ext mode mouse
4397 this.sgrMouse
= false;
4399 case 1015: // urxvt ext mode mouse
4400 this.urxvtMouse
= false;
4402 case 25: // hide cursor
4403 this.cursorHidden
= true;
4405 case 1049: // alt screen buffer cursor
4407 case 47: // normal screen buffer
4408 case 1047: // normal screen buffer - clearing it first
4410 this.lines
= this.normal
.lines
;
4411 this.ybase
= this.normal
.ybase
;
4412 this.ydisp
= this.normal
.ydisp
;
4413 this.x
= this.normal
.x
;
4414 this.y
= this.normal
.y
;
4415 this.scrollTop
= this.normal
.scrollTop
;
4416 this.scrollBottom
= this.normal
.scrollBottom
;
4417 this.tabs
= this.normal
.tabs
;
4419 // if (params === 1049) {
4420 // this.x = this.savedX;
4421 // this.y = this.savedY;
4423 this.queueRefresh(0, this.rows
- 1);
4424 this.viewport
.syncScrollArea();
4435 * Set Scrolling Region [top;bottom] (default = full size of win-
4439 Terminal
.prototype.setScrollRegion = function(params
) {
4440 if (this.prefix
) return;
4441 this.scrollTop
= (params
[0] || 1) - 1;
4442 this.scrollBottom
= (params
[1] || this.rows
) - 1;
4450 * Save cursor (ANSI.SYS).
4452 Terminal
.prototype.saveCursor = function(params
) {
4453 this.savedX
= this.x
;
4454 this.savedY
= this.y
;
4460 * Restore cursor (ANSI.SYS).
4462 Terminal
.prototype.restoreCursor = function(params
) {
4463 this.x
= this.savedX
|| 0;
4464 this.y
= this.savedY
|| 0;
4474 * Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
4476 Terminal
.prototype.cursorForwardTab = function(params
) {
4477 var param
= params
[0] || 1;
4479 this.x
= this.nextStop();
4485 * CSI Ps S Scroll up Ps lines (default = 1) (SU).
4487 Terminal
.prototype.scrollUp = function(params
) {
4488 var param
= params
[0] || 1;
4490 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
4491 this.lines
.splice(this.ybase
+ this.scrollBottom
, 0, this.blankLine());
4494 this.updateRange(this.scrollTop
);
4495 this.updateRange(this.scrollBottom
);
4500 * CSI Ps T Scroll down Ps lines (default = 1) (SD).
4502 Terminal
.prototype.scrollDown = function(params
) {
4503 var param
= params
[0] || 1;
4505 this.lines
.splice(this.ybase
+ this.scrollBottom
, 1);
4506 this.lines
.splice(this.ybase
+ this.scrollTop
, 0, this.blankLine());
4509 this.updateRange(this.scrollTop
);
4510 this.updateRange(this.scrollBottom
);
4515 * CSI Ps ; Ps ; Ps ; Ps ; Ps T
4516 * Initiate highlight mouse tracking. Parameters are
4517 * [func;startx;starty;firstrow;lastrow]. See the section Mouse
4520 Terminal
.prototype.initMouseTracking = function(params
) {
4521 // Relevant: DECSET 1001
4527 * Reset one or more features of the title modes to the default
4528 * value. Normally, "reset" disables the feature. It is possi-
4529 * ble to disable the ability to reset features by compiling a
4530 * different default for the title modes into xterm.
4531 * Ps = 0 -> Do not set window/icon labels using hexadecimal.
4532 * Ps = 1 -> Do not query window/icon labels using hexadeci-
4534 * Ps = 2 -> Do not set window/icon labels using UTF-8.
4535 * Ps = 3 -> Do not query window/icon labels using UTF-8.
4536 * (See discussion of "Title Modes").
4538 Terminal
.prototype.resetTitleModes = function(params
) {
4544 * CSI Ps Z Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
4546 Terminal
.prototype.cursorBackwardTab = function(params
) {
4547 var param
= params
[0] || 1;
4549 this.x
= this.prevStop();
4555 * CSI Ps b Repeat the preceding graphic character Ps times (REP).
4557 Terminal
.prototype.repeatPrecedingCharacter = function(params
) {
4558 var param
= params
[0] || 1
4559 , line
= this.lines
.get(this.ybase
+ this.y
)
4560 , ch
= line
[this.x
- 1] || [this.defAttr
, ' ', 1];
4562 while (param
--) line
[this.x
++] = ch
;
4567 * CSI Ps g Tab Clear (TBC).
4568 * Ps = 0 -> Clear Current Column (default).
4569 * Ps = 3 -> Clear All.
4571 * Ps = 2 -> Clear Stops on Line.
4572 * http://vt100.net/annarbor/aaa-ug/section6.html
4574 Terminal
.prototype.tabClear = function(params
) {
4575 var param
= params
[0];
4577 delete this.tabs
[this.x
];
4578 } else if (param
=== 3) {
4585 * CSI Pm i Media Copy (MC).
4586 * Ps = 0 -> Print screen (default).
4587 * Ps = 4 -> Turn off printer controller mode.
4588 * Ps = 5 -> Turn on printer controller mode.
4590 * Media Copy (MC, DEC-specific).
4591 * Ps = 1 -> Print line containing cursor.
4592 * Ps = 4 -> Turn off autoprint mode.
4593 * Ps = 5 -> Turn on autoprint mode.
4594 * Ps = 1 0 -> Print composed display, ignores DECPEX.
4595 * Ps = 1 1 -> Print all pages.
4597 Terminal
.prototype.mediaCopy = function(params
) {
4604 * Set or reset resource-values used by xterm to decide whether
4605 * to construct escape sequences holding information about the
4606 * modifiers pressed with a given key. The first parameter iden-
4607 * tifies the resource to set/reset. The second parameter is the
4608 * value to assign to the resource. If the second parameter is
4609 * omitted, the resource is reset to its initial value.
4610 * Ps = 1 -> modifyCursorKeys.
4611 * Ps = 2 -> modifyFunctionKeys.
4612 * Ps = 4 -> modifyOtherKeys.
4613 * If no parameters are given, all resources are reset to their
4616 Terminal
.prototype.setResources = function(params
) {
4623 * Disable modifiers which may be enabled via the CSI > Ps; Ps m
4624 * sequence. This corresponds to a resource value of "-1", which
4625 * cannot be set with the other sequence. The parameter identi-
4626 * fies the resource to be disabled:
4627 * Ps = 1 -> modifyCursorKeys.
4628 * Ps = 2 -> modifyFunctionKeys.
4629 * Ps = 4 -> modifyOtherKeys.
4630 * If the parameter is omitted, modifyFunctionKeys is disabled.
4631 * When modifyFunctionKeys is disabled, xterm uses the modifier
4632 * keys to make an extended sequence of functions rather than
4633 * adding a parameter to each function key to denote the modi-
4636 Terminal
.prototype.disableModifiers = function(params
) {
4643 * Set resource value pointerMode. This is used by xterm to
4644 * decide whether to hide the pointer cursor as the user types.
4645 * Valid values for the parameter:
4646 * Ps = 0 -> never hide the pointer.
4647 * Ps = 1 -> hide if the mouse tracking mode is not enabled.
4648 * Ps = 2 -> always hide the pointer. If no parameter is
4649 * given, xterm uses the default, which is 1 .
4651 Terminal
.prototype.setPointerMode = function(params
) {
4657 * CSI ! p Soft terminal reset (DECSTR).
4658 * http://vt100.net/docs/vt220-rm/table4-10.html
4660 Terminal
.prototype.softReset = function(params
) {
4661 this.cursorHidden
= false;
4662 this.insertMode
= false;
4663 this.originMode
= false;
4664 this.wraparoundMode
= false; // autowrap
4665 this.applicationKeypad
= false; // ?
4666 this.viewport
.syncScrollArea();
4667 this.applicationCursor
= false;
4669 this.scrollBottom
= this.rows
- 1;
4670 this.curAttr
= this.defAttr
;
4671 this.x
= this.y
= 0; // ?
4672 this.charset
= null;
4673 this.glevel
= 0; // ??
4674 this.charsets
= [null]; // ??
4680 * Request ANSI mode (DECRQM). For VT300 and up, reply is
4682 * where Ps is the mode number as in RM, and Pm is the mode
4684 * 0 - not recognized
4687 * 3 - permanently set
4688 * 4 - permanently reset
4690 Terminal
.prototype.requestAnsiMode = function(params
) {
4697 * Request DEC private mode (DECRQM). For VT300 and up, reply is
4699 * where Ps is the mode number as in DECSET, Pm is the mode value
4700 * as in the ANSI DECRQM.
4702 Terminal
.prototype.requestPrivateMode = function(params
) {
4709 * Set conformance level (DECSCL). Valid values for the first
4711 * Ps = 6 1 -> VT100.
4712 * Ps = 6 2 -> VT200.
4713 * Ps = 6 3 -> VT300.
4714 * Valid values for the second parameter:
4715 * Ps = 0 -> 8-bit controls.
4716 * Ps = 1 -> 7-bit controls (always set for VT100).
4717 * Ps = 2 -> 8-bit controls.
4719 Terminal
.prototype.setConformanceLevel = function(params
) {
4725 * CSI Ps q Load LEDs (DECLL).
4726 * Ps = 0 -> Clear all LEDS (default).
4727 * Ps = 1 -> Light Num Lock.
4728 * Ps = 2 -> Light Caps Lock.
4729 * Ps = 3 -> Light Scroll Lock.
4730 * Ps = 2 1 -> Extinguish Num Lock.
4731 * Ps = 2 2 -> Extinguish Caps Lock.
4732 * Ps = 2 3 -> Extinguish Scroll Lock.
4734 Terminal
.prototype.loadLEDs = function(params
) {
4741 * Set cursor style (DECSCUSR, VT520).
4742 * Ps = 0 -> blinking block.
4743 * Ps = 1 -> blinking block (default).
4744 * Ps = 2 -> steady block.
4745 * Ps = 3 -> blinking underline.
4746 * Ps = 4 -> steady underline.
4748 Terminal
.prototype.setCursorStyle = function(params
) {
4755 * Select character protection attribute (DECSCA). Valid values
4756 * for the parameter:
4757 * Ps = 0 -> DECSED and DECSEL can erase (default).
4758 * Ps = 1 -> DECSED and DECSEL cannot erase.
4759 * Ps = 2 -> DECSED and DECSEL can erase.
4761 Terminal
.prototype.setCharProtectionAttr = function(params
) {
4768 * Restore DEC Private Mode Values. The value of Ps previously
4769 * saved is restored. Ps values are the same as for DECSET.
4771 Terminal
.prototype.restorePrivateValues = function(params
) {
4777 * CSI Pt; Pl; Pb; Pr; Ps$ r
4778 * Change Attributes in Rectangular Area (DECCARA), VT400 and up.
4779 * Pt; Pl; Pb; Pr denotes the rectangle.
4780 * Ps denotes the SGR attributes to change: 0, 1, 4, 5, 7.
4781 * NOTE: xterm doesn't enable this code by default.
4783 Terminal
.prototype.setAttrInRectangle = function(params
) {
4793 for (; t
< b
+ 1; t
++) {
4794 line
= this.lines
.get(this.ybase
+ t
);
4795 for (i
= l
; i
< r
; i
++) {
4796 line
[i
] = [attr
, line
[i
][1]];
4801 this.updateRange(params
[0]);
4802 this.updateRange(params
[2]);
4807 * CSI Pc; Pt; Pl; Pb; Pr$ x
4808 * Fill Rectangular Area (DECFRA), VT420 and up.
4809 * Pc is the character to use.
4810 * Pt; Pl; Pb; Pr denotes the rectangle.
4811 * NOTE: xterm doesn't enable this code by default.
4813 Terminal
.prototype.fillRectangle = function(params
) {
4823 for (; t
< b
+ 1; t
++) {
4824 line
= this.lines
.get(this.ybase
+ t
);
4825 for (i
= l
; i
< r
; i
++) {
4826 line
[i
] = [line
[i
][0], String
.fromCharCode(ch
)];
4831 this.updateRange(params
[1]);
4832 this.updateRange(params
[3]);
4838 * Enable Locator Reporting (DECELR).
4839 * Valid values for the first parameter:
4840 * Ps = 0 -> Locator disabled (default).
4841 * Ps = 1 -> Locator enabled.
4842 * Ps = 2 -> Locator enabled for one report, then disabled.
4843 * The second parameter specifies the coordinate unit for locator
4845 * Valid values for the second parameter:
4846 * Pu = 0 <- or omitted -> default to character cells.
4847 * Pu = 1 <- device physical pixels.
4848 * Pu = 2 <- character cells.
4850 Terminal
.prototype.enableLocatorReporting = function(params
) {
4851 var val
= params
[0] > 0;
4852 //this.mouseEvents = val;
4853 //this.decLocator = val;
4858 * CSI Pt; Pl; Pb; Pr$ z
4859 * Erase Rectangular Area (DECERA), VT400 and up.
4860 * Pt; Pl; Pb; Pr denotes the rectangle.
4861 * NOTE: xterm doesn't enable this code by default.
4863 Terminal
.prototype.eraseRectangle = function(params
) {
4873 ch
= [this.eraseAttr(), ' ', 1]; // xterm?
4875 for (; t
< b
+ 1; t
++) {
4876 line
= this.lines
.get(this.ybase
+ t
);
4877 for (i
= l
; i
< r
; i
++) {
4883 this.updateRange(params
[0]);
4884 this.updateRange(params
[2]);
4890 * Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
4891 * NOTE: xterm doesn't enable this code by default.
4893 Terminal
.prototype.insertColumns = function() {
4894 var param
= params
[0]
4895 , l
= this.ybase
+ this.rows
4896 , ch
= [this.eraseAttr(), ' ', 1] // xterm?
4900 for (i
= this.ybase
; i
< l
; i
++) {
4901 this.lines
.get(i
).splice(this.x
+ 1, 0, ch
);
4902 this.lines
.get(i
).pop();
4912 * Delete P s Column(s) (default = 1) (DECDC), VT420 and up
4913 * NOTE: xterm doesn't enable this code by default.
4915 Terminal
.prototype.deleteColumns = function() {
4916 var param
= params
[0]
4917 , l
= this.ybase
+ this.rows
4918 , ch
= [this.eraseAttr(), ' ', 1] // xterm?
4922 for (i
= this.ybase
; i
< l
; i
++) {
4923 this.lines
.get(i
).splice(this.x
, 1);
4924 this.lines
.get(i
).push(ch
);
4935 Terminal
.charsets
= {};
4937 // DEC Special Character and Line Drawing Set.
4938 // http://vt100.net/docs/vt102-ug/table5-13.html
4939 // A lot of curses apps use this if they see TERM=xterm.
4940 // testing: echo -e '\e(0a\e(B'
4941 // The xterm output sometimes seems to conflict with the
4942 // reference above. xterm seems in line with the reference
4943 // when running vttest however.
4944 // The table below now uses xterm's output from vttest.
4945 Terminal
.charsets
.SCLD
= { // (0
4946 '`': '\u25c6', // '◆'
4947 'a': '\u2592', // '▒'
4948 'b': '\u0009', // '\t'
4949 'c': '\u000c', // '\f'
4950 'd': '\u000d', // '\r'
4951 'e': '\u000a', // '\n'
4952 'f': '\u00b0', // '°'
4953 'g': '\u00b1', // '±'
4954 'h': '\u2424', // '\u2424' (NL)
4955 'i': '\u000b', // '\v'
4956 'j': '\u2518', // '┘'
4957 'k': '\u2510', // '┐'
4958 'l': '\u250c', // '┌'
4959 'm': '\u2514', // '└'
4960 'n': '\u253c', // '┼'
4961 'o': '\u23ba', // '⎺'
4962 'p': '\u23bb', // '⎻'
4963 'q': '\u2500', // '─'
4964 'r': '\u23bc', // '⎼'
4965 's': '\u23bd', // '⎽'
4966 't': '\u251c', // '├'
4967 'u': '\u2524', // '┤'
4968 'v': '\u2534', // '┴'
4969 'w': '\u252c', // '┬'
4970 'x': '\u2502', // '│'
4971 'y': '\u2264', // '≤'
4972 'z': '\u2265', // '≥'
4973 '{': '\u03c0', // 'π'
4974 '|': '\u2260', // '≠'
4975 '}': '\u00a3', // '£'
4976 '~': '\u00b7' // '·'
4979 Terminal
.charsets
.UK
= null; // (A
4980 Terminal
.charsets
.US
= null; // (B (USASCII)
4981 Terminal
.charsets
.Dutch
= null; // (4
4982 Terminal
.charsets
.Finnish
= null; // (C or (5
4983 Terminal
.charsets
.French
= null; // (R
4984 Terminal
.charsets
.FrenchCanadian
= null; // (Q
4985 Terminal
.charsets
.German
= null; // (K
4986 Terminal
.charsets
.Italian
= null; // (Y
4987 Terminal
.charsets
.NorwegianDanish
= null; // (E or (6
4988 Terminal
.charsets
.Spanish
= null; // (Z
4989 Terminal
.charsets
.Swedish
= null; // (H or (7
4990 Terminal
.charsets
.Swiss
= null; // (=
4991 Terminal
.charsets
.ISOLatin
= null; // /A
4997 function on(el
, type
, handler
, capture
) {
4998 if (!Array
.isArray(el
)) {
5001 el
.forEach(function (element
) {
5002 element
.addEventListener(type
, handler
, capture
|| false);
5006 function off(el
, type
, handler
, capture
) {
5007 el
.removeEventListener(type
, handler
, capture
|| false);
5010 function cancel(ev
, force
) {
5011 if (!this.cancelEvents
&& !force
) {
5014 ev
.preventDefault();
5015 ev
.stopPropagation();
5019 function inherits(child
, parent
) {
5021 this.constructor = child
;
5023 f
.prototype = parent
.prototype;
5024 child
.prototype = new f
;
5027 // if bold is broken, we can't
5028 // use it in the terminal.
5029 function isBoldBroken(document
) {
5030 var body
= document
.getElementsByTagName('body')[0];
5031 var el
= document
.createElement('span');
5032 el
.innerHTML
= 'hello world';
5033 body
.appendChild(el
);
5034 var w1
= el
.scrollWidth
;
5035 el
.style
.fontWeight
= 'bold';
5036 var w2
= el
.scrollWidth
;
5037 body
.removeChild(el
);
5041 function indexOf(obj
, el
) {
5044 if (obj
[i
] === el
) return i
;
5049 function isThirdLevelShift(term
, ev
) {
5051 (term
.browser
.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) ||
5052 (term
.browser
.isMSWindows
&& ev
.altKey
&& ev
.ctrlKey
&& !ev
.metaKey
);
5054 if (ev
.type
== 'keypress') {
5055 return thirdLevelKey
;
5058 // Don't invoke for arrows, pageDown, home, backspace, etc. (on non-keypress events)
5059 return thirdLevelKey
&& (!ev
.keyCode
|| ev
.keyCode
> 47);
5062 function matchColor(r1
, g1
, b1
) {
5063 var hash
= (r1
<< 16) | (g1
<< 8) | b1
;
5065 if (matchColor
._cache
[hash
] != null) {
5066 return matchColor
._cache
[hash
];
5069 var ldiff
= Infinity
5078 for (; i
< Terminal
.vcolors
.length
; i
++) {
5079 c
= Terminal
.vcolors
[i
];
5084 diff
= matchColor
.distance(r1
, g1
, b1
, r2
, g2
, b2
);
5097 return matchColor
._cache
[hash
] = li
;
5100 matchColor
._cache
= {};
5102 // http://stackoverflow.com/questions/1633828
5103 matchColor
.distance = function(r1
, g1
, b1
, r2
, g2
, b2
) {
5104 return Math
.pow(30 * (r1
- r2
), 2)
5105 + Math
.pow(59 * (g1
- g2
), 2)
5106 + Math
.pow(11 * (b1
- b2
), 2);
5109 function each(obj
, iter
, con
) {
5110 if (obj
.forEach
) return obj
.forEach(iter
, con
);
5111 for (var i
= 0; i
< obj
.length
; i
++) {
5112 iter
.call(con
, obj
[i
], i
, obj
);
5116 function keys(obj
) {
5117 if (Object
.keys
) return Object
.keys(obj
);
5120 if (Object
.prototype.hasOwnProperty
.call(obj
, key
)) {
5127 var wcwidth
= (function(opts
) {
5128 // extracted from https://www.cl.cam.ac.uk/%7Emgk25/ucs/wcwidth.c
5129 // combining characters
5131 [0x0300, 0x036F], [0x0483, 0x0486], [0x0488, 0x0489],
5132 [0x0591, 0x05BD], [0x05BF, 0x05BF], [0x05C1, 0x05C2],
5133 [0x05C4, 0x05C5], [0x05C7, 0x05C7], [0x0600, 0x0603],
5134 [0x0610, 0x0615], [0x064B, 0x065E], [0x0670, 0x0670],
5135 [0x06D6, 0x06E4], [0x06E7, 0x06E8], [0x06EA, 0x06ED],
5136 [0x070F, 0x070F], [0x0711, 0x0711], [0x0730, 0x074A],
5137 [0x07A6, 0x07B0], [0x07EB, 0x07F3], [0x0901, 0x0902],
5138 [0x093C, 0x093C], [0x0941, 0x0948], [0x094D, 0x094D],
5139 [0x0951, 0x0954], [0x0962, 0x0963], [0x0981, 0x0981],
5140 [0x09BC, 0x09BC], [0x09C1, 0x09C4], [0x09CD, 0x09CD],
5141 [0x09E2, 0x09E3], [0x0A01, 0x0A02], [0x0A3C, 0x0A3C],
5142 [0x0A41, 0x0A42], [0x0A47, 0x0A48], [0x0A4B, 0x0A4D],
5143 [0x0A70, 0x0A71], [0x0A81, 0x0A82], [0x0ABC, 0x0ABC],
5144 [0x0AC1, 0x0AC5], [0x0AC7, 0x0AC8], [0x0ACD, 0x0ACD],
5145 [0x0AE2, 0x0AE3], [0x0B01, 0x0B01], [0x0B3C, 0x0B3C],
5146 [0x0B3F, 0x0B3F], [0x0B41, 0x0B43], [0x0B4D, 0x0B4D],
5147 [0x0B56, 0x0B56], [0x0B82, 0x0B82], [0x0BC0, 0x0BC0],
5148 [0x0BCD, 0x0BCD], [0x0C3E, 0x0C40], [0x0C46, 0x0C48],
5149 [0x0C4A, 0x0C4D], [0x0C55, 0x0C56], [0x0CBC, 0x0CBC],
5150 [0x0CBF, 0x0CBF], [0x0CC6, 0x0CC6], [0x0CCC, 0x0CCD],
5151 [0x0CE2, 0x0CE3], [0x0D41, 0x0D43], [0x0D4D, 0x0D4D],
5152 [0x0DCA, 0x0DCA], [0x0DD2, 0x0DD4], [0x0DD6, 0x0DD6],
5153 [0x0E31, 0x0E31], [0x0E34, 0x0E3A], [0x0E47, 0x0E4E],
5154 [0x0EB1, 0x0EB1], [0x0EB4, 0x0EB9], [0x0EBB, 0x0EBC],
5155 [0x0EC8, 0x0ECD], [0x0F18, 0x0F19], [0x0F35, 0x0F35],
5156 [0x0F37, 0x0F37], [0x0F39, 0x0F39], [0x0F71, 0x0F7E],
5157 [0x0F80, 0x0F84], [0x0F86, 0x0F87], [0x0F90, 0x0F97],
5158 [0x0F99, 0x0FBC], [0x0FC6, 0x0FC6], [0x102D, 0x1030],
5159 [0x1032, 0x1032], [0x1036, 0x1037], [0x1039, 0x1039],
5160 [0x1058, 0x1059], [0x1160, 0x11FF], [0x135F, 0x135F],
5161 [0x1712, 0x1714], [0x1732, 0x1734], [0x1752, 0x1753],
5162 [0x1772, 0x1773], [0x17B4, 0x17B5], [0x17B7, 0x17BD],
5163 [0x17C6, 0x17C6], [0x17C9, 0x17D3], [0x17DD, 0x17DD],
5164 [0x180B, 0x180D], [0x18A9, 0x18A9], [0x1920, 0x1922],
5165 [0x1927, 0x1928], [0x1932, 0x1932], [0x1939, 0x193B],
5166 [0x1A17, 0x1A18], [0x1B00, 0x1B03], [0x1B34, 0x1B34],
5167 [0x1B36, 0x1B3A], [0x1B3C, 0x1B3C], [0x1B42, 0x1B42],
5168 [0x1B6B, 0x1B73], [0x1DC0, 0x1DCA], [0x1DFE, 0x1DFF],
5169 [0x200B, 0x200F], [0x202A, 0x202E], [0x2060, 0x2063],
5170 [0x206A, 0x206F], [0x20D0, 0x20EF], [0x302A, 0x302F],
5171 [0x3099, 0x309A], [0xA806, 0xA806], [0xA80B, 0xA80B],
5172 [0xA825, 0xA826], [0xFB1E, 0xFB1E], [0xFE00, 0xFE0F],
5173 [0xFE20, 0xFE23], [0xFEFF, 0xFEFF], [0xFFF9, 0xFFFB],
5174 [0x10A01, 0x10A03], [0x10A05, 0x10A06], [0x10A0C, 0x10A0F],
5175 [0x10A38, 0x10A3A], [0x10A3F, 0x10A3F], [0x1D167, 0x1D169],
5176 [0x1D173, 0x1D182], [0x1D185, 0x1D18B], [0x1D1AA, 0x1D1AD],
5177 [0x1D242, 0x1D244], [0xE0001, 0xE0001], [0xE0020, 0xE007F],
5181 function bisearch(ucs
) {
5183 var max
= COMBINING
.length
- 1;
5185 if (ucs
< COMBINING
[0][0] || ucs
> COMBINING
[max
][1])
5187 while (max
>= min
) {
5188 mid
= Math
.floor((min
+ max
) / 2);
5189 if (ucs
> COMBINING
[mid
][1])
5191 else if (ucs
< COMBINING
[mid
][0])
5198 function wcwidth(ucs
) {
5199 // test for 8-bit control characters
5202 if (ucs
< 32 || (ucs
>= 0x7f && ucs
< 0xa0))
5203 return opts
.control
;
5204 // binary search in table of non-spacing characters
5207 // if we arrive here, ucs is not a combining or C0/C1 control character
5212 ucs
<= 0x115f || // Hangul Jamo init. consonants
5215 (ucs
>= 0x2e80 && ucs
<= 0xa4cf && ucs
!= 0x303f) || // CJK..Yi
5216 (ucs
>= 0xac00 && ucs
<= 0xd7a3) || // Hangul Syllables
5217 (ucs
>= 0xf900 && ucs
<= 0xfaff) || // CJK Compat Ideographs
5218 (ucs
>= 0xfe10 && ucs
<= 0xfe19) || // Vertical forms
5219 (ucs
>= 0xfe30 && ucs
<= 0xfe6f) || // CJK Compat Forms
5220 (ucs
>= 0xff00 && ucs
<= 0xff60) || // Fullwidth Forms
5221 (ucs
>= 0xffe0 && ucs
<= 0xffe6) ||
5222 (ucs
>= 0x20000 && ucs
<= 0x2fffd) ||
5223 (ucs
>= 0x30000 && ucs
<= 0x3fffd)
5228 })({nul
: 0, control
: 0}); // configurable options
5234 Terminal
.EventEmitter
= EventEmitter
;
5235 Terminal
.inherits
= inherits
;
5238 * Adds an event listener to the terminal.
5240 * @param {string} event The name of the event. TODO: Document all event types
5241 * @param {function} callback The function to call when the event is triggered.
5245 Terminal
.cancel
= cancel
;
5247 module
.exports
= Terminal
;