]>
git.proxmox.com Git - mirror_xterm.js.git/blob - src/xterm.js
6319570d664ccec2021b467ecfd351c9b261247a
2 * xterm.js: xterm, in the browser
3 * Copyright (c) 2014, sourceLair Limited (www.sourcelair.com (MIT License)
4 * Copyright (c) 2012-2013, Christopher Jeffrey (MIT License)
5 * https://github.com/chjj/term.js
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * Originally forked from (with the author's permission):
26 * Fabrice Bellard's javascript vt100 for jslinux:
27 * http://bellard.org/jslinux/
28 * Copyright (c) 2011 Fabrice Bellard
29 * The original design remains. The terminal itself
30 * has been extended to include xterm CSI codes, among
35 if (typeof exports
=== 'object' && typeof module
=== 'object') {
37 * CommonJS environment
39 module
.exports
= xterm
.call(this);
40 } else if (typeof define
== 'function') {
42 * Require.js is available
44 define([], xterm
.bind(window
));
47 * Plain browser environment
49 this.Xterm
= xterm
.call(this);
50 this.Terminal
= this.Xterm
; /* Backwards compatibility with term.js */
54 * Terminal Emulation References:
56 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.txt
57 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
58 * http://invisible-island.net/vttest/
59 * http://www.inwap.com/pdp10/ansicode.txt
60 * http://linux.die.net/man/4/console_codes
61 * http://linux.die.net/man/7/urxvt
70 var window
= this, document
= this.document
;
76 function EventEmitter() {
77 this._events
= this._events
|| {};
80 EventEmitter
.prototype.addListener = function(type
, listener
) {
81 this._events
[type
] = this._events
[type
] || [];
82 this._events
[type
].push(listener
);
85 EventEmitter
.prototype.on
= EventEmitter
.prototype.addListener
;
87 EventEmitter
.prototype.removeListener = function(type
, listener
) {
88 if (!this._events
[type
]) return;
90 var obj
= this._events
[type
]
94 if (obj
[i
] === listener
|| obj
[i
].listener
=== listener
) {
101 EventEmitter
.prototype.off
= EventEmitter
.prototype.removeListener
;
103 EventEmitter
.prototype.removeAllListeners = function(type
) {
104 if (this._events
[type
]) delete this._events
[type
];
107 EventEmitter
.prototype.once = function(type
, listener
) {
110 var args
= Array
.prototype.slice
.call(arguments
);
111 self
.removeListener(type
, on
);
112 return listener
.apply(self
, args
);
114 on
.listener
= listener
;
115 return this.on(type
, on
);
118 EventEmitter
.prototype.emit = function(type
) {
119 if (!this._events
[type
]) return;
121 var args
= Array
.prototype.slice
.call(arguments
, 1)
122 , obj
= this._events
[type
]
127 obj
[i
].apply(this, args
);
131 EventEmitter
.prototype.listeners = function(type
) {
132 return this._events
[type
] = this._events
[type
] || [];
139 var normal
= 0, escaped
= 1, csi
= 2, osc
= 3, charset
= 4, dcs
= 5, ignore
= 6;
146 * Creates a new `Terminal` object.
148 * @param {object} options An object containing a set of options, the available options are:
149 * - cursorBlink (boolean): Whether the terminal cursor blinks
153 function Terminal(options
) {
156 if (!(this instanceof Terminal
)) {
157 return new Terminal(arguments
[0], arguments
[1], arguments
[2]);
160 self
.cancel
= Terminal
.cancel
;
162 EventEmitter
.call(this);
164 if (typeof options
=== 'number') {
168 handler
: arguments
[2]
172 options
= options
|| {};
175 Object
.keys(Terminal
.defaults
).forEach(function(key
) {
176 if (options
[key
] == null) {
177 options
[key
] = Terminal
.options
[key
];
179 if (Terminal
[key
] !== Terminal
.defaults
[key
]) {
180 options
[key
] = Terminal
[key
];
183 self
[key
] = options
[key
];
186 if (options
.colors
.length
=== 8) {
187 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(8));
188 } else if (options
.colors
.length
=== 16) {
189 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(16));
190 } else if (options
.colors
.length
=== 10) {
191 options
.colors
= options
.colors
.slice(0, -2).concat(
192 Terminal
._colors
.slice(8, -2), options
.colors
.slice(-2));
193 } else if (options
.colors
.length
=== 18) {
194 options
.colors
= options
.colors
.concat(
195 Terminal
._colors
.slice(16, -2), options
.colors
.slice(-2));
197 this.colors
= options
.colors
;
199 this.options
= options
;
201 // this.context = options.context || window;
202 // this.document = options.document || document;
203 this.parent
= options
.body
|| options
.parent
204 || (document
? document
.getElementsByTagName('body')[0] : null);
206 this.cols
= options
.cols
|| options
.geometry
[0];
207 this.rows
= options
.rows
|| options
.geometry
[1];
209 if (options
.handler
) {
210 this.on('data', options
.handler
);
214 * The scroll position of the y cursor, ie. ybase + y = the y position within the entire
219 * The scroll position of the viewport
224 * The cursor's x position after ybase
229 * The cursor's y position after ybase
234 * Used to debounce the refresh function
236 this.isRefreshing
= false;
239 * Whether there is a full terminal refresh queued
242 this.cursorState
= 0;
243 this.cursorHidden
= false;
248 this.scrollBottom
= this.rows
- 1;
251 this.applicationKeypad
= false;
252 this.applicationCursor
= false;
253 this.originMode
= false;
254 this.insertMode
= false;
255 this.wraparoundMode
= false;
260 this.gcharset
= null;
262 this.charsets
= [null];
286 this.readable
= true;
287 this.writable
= true;
289 this.defAttr
= (0 << 18) | (257 << 9) | (256 << 0);
290 this.curAttr
= this.defAttr
;
293 this.currentParam
= 0;
298 * An array of all lines in the entire buffer, including the prompt. The lines are array of
299 * characters which are 2-length arrays where [0] is an attribute and [1] is the character.
304 this.lines
.push(this.blankLine());
311 inherits(Terminal
, EventEmitter
);
313 // back_color_erase feature for xterm.
314 Terminal
.prototype.eraseAttr = function() {
315 // if (this.is('screen')) return this.defAttr;
316 return (this.defAttr
& ~0x1ff) | (this.curAttr
& 0x1ff);
324 Terminal
.tangoColors
= [
345 // Colors 0-15 + 16-255
346 // Much thanks to TooTallNate for writing this.
347 Terminal
.colors
= (function() {
348 var colors
= Terminal
.tangoColors
.slice()
349 , r
= [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff]
354 for (; i
< 216; i
++) {
355 out(r
[(i
/ 36) % 6 | 0], r
[(i
/ 6) % 6 | 0], r
[i
% 6]);
360 for (; i
< 24; i
++) {
365 function out(r
, g
, b
) {
366 colors
.push('#' + hex(r
) + hex(g
) + hex(b
));
371 return c
.length
< 2 ? '0' + c
: c
;
377 Terminal
._colors
= Terminal
.colors
.slice();
379 Terminal
.vcolors
= (function() {
381 , colors
= Terminal
.colors
385 for (; i
< 256; i
++) {
386 color
= parseInt(colors
[i
].substring(1), 16);
388 (color
>> 16) & 0xff,
401 Terminal
.defaults
= {
402 colors
: Terminal
.colors
,
414 // programFeatures: false,
418 Terminal
.options
= {};
420 Terminal
.focus
= null;
422 each(keys(Terminal
.defaults
), function(key
) {
423 Terminal
[key
] = Terminal
.defaults
[key
];
424 Terminal
.options
[key
] = Terminal
.defaults
[key
];
428 * Focus the terminal. Delegates focus handling to the terminal's DOM element.
432 Terminal
.prototype.focus = function() {
433 return this.element
.focus();
437 * Binds the desired focus behavior on a given terminal object.
441 Terminal
.bindFocus = function (term
) {
442 on(term
.element
, 'focus', function (ev
) {
443 if (term
.sendFocus
) {
448 Terminal
.focus
= term
;
449 term
.emit('focus', {terminal
: term
});
454 * Blur the terminal. Delegates blur handling to the terminal's DOM element.
458 Terminal
.prototype.blur = function() {
459 return terminal
.element
.blur();
463 * Binds the desired blur behavior on a given terminal object.
467 Terminal
.bindBlur = function (term
) {
468 on(term
.element
, 'blur', function (ev
) {
469 if (term
.sendFocus
) {
472 Terminal
.focus
= null;
473 term
.emit('blur', {terminal
: term
});
478 * Initialize default behavior
480 Terminal
.prototype.initGlobal = function() {
481 Terminal
.bindKeys(this);
482 Terminal
.bindPaste(this);
483 Terminal
.bindCopy(this);
484 Terminal
.bindCut(this);
485 Terminal
.bindDrop(this);
486 Terminal
.bindFocus(this);
487 Terminal
.bindBlur(this);
491 * Clears all selected text, inside the terminal.
493 Terminal
.prototype.clearSelection = function() {
494 var selectionBaseNode
= window
.getSelection().baseNode
;
496 if (selectionBaseNode
&& (this.element
.contains(selectionBaseNode
.parentElement
))) {
497 window
.getSelection().removeAllRanges();
502 * This function temporarily enables (leases) the contentEditable value of the terminal, which
503 * should be set back to false within 5 seconds at most.
505 Terminal
.prototype.leaseContentEditable = function (ms
, callback
) {
508 term
.element
.contentEditable
= true;
509 setTimeout(function () {
510 term
.element
.contentEditable
= false;
511 if (typeof callback
== 'function') {
518 * Bind to paste event and allow both keyboard and right-click pasting, without having the
519 * contentEditable value set to true.
521 Terminal
.bindPaste = function(term
) {
522 on(term
.element
, 'paste', function(ev
) {
523 if (ev
.clipboardData
) {
524 var text
= ev
.clipboardData
.getData('text/plain');
525 term
.emit('paste', text
, ev
);
528 * Cancel the paste event, or else things will be pasted twice:
529 * 1. by the terminal handler
530 * 2. by the browser, because of the contentEditable value being true
532 term
.cancel(ev
, true);
535 * After the paste event is completed, always set the contentEditable value to false.
537 term
.element
.contentEditable
= false;
542 * Hack pasting with keyboard, in order to make it work without contentEditable.
543 * When a user types Ctrl + Shift + V or Cmd + V on a Mac, lease the contentEditable value
546 on(term
.element
, 'keydown', function (ev
) {
548 * If on a Mac, lease the contentEditable value temporarily, when the user presses
549 * the Cmd button, in order to cope with some sync issues on Safari.
551 if (term
.isMac
&& ev
.keyCode
== 91) {
552 term
.leaseContentEditable(1000);
555 if (ev
.keyCode
== 86) { // keyCode 96 corresponds to "v"
556 if (term
.isMac
&& ev
.metaKey
) {
557 term
.leaseContentEditable();
561 if (!term
.isMac
&& ev
.keyCode
== 45 && ev
.shiftKey
&& !ev
.ctrlKey
) {
562 // Shift + Insert pastes on windows and many linuxes
563 term
.leaseContentEditable();
568 * Hack pasting with right-click in order to allow right-click paste, by leasing the
569 * contentEditable value as true.
571 on(term
.element
, 'contextmenu', function (ev
) {
572 term
.leaseContentEditable();
578 * Apply key handling to the terminal
580 Terminal
.bindKeys = function(term
) {
581 on(term
.element
, 'keydown', function(ev
) {
585 on(term
.element
, 'keypress', function(ev
) {
591 * Prepares text copied from terminal selection, to be saved in the clipboard by:
592 * 1. stripping all trailing white spaces
593 * 2. converting all non-breaking spaces to regular spaces
594 * @param {string} text The copied text that needs processing for storing in clipboard
598 Terminal
.prepareCopiedTextForClipboard = function (text
) {
599 var space
= String
.fromCharCode(32),
600 nonBreakingSpace
= String
.fromCharCode(160),
601 allNonBreakingSpaces
= new RegExp(nonBreakingSpace
, 'g'),
602 processedText
= text
.split('\n').map(function (line
) {
604 * Strip all trailing white spaces and convert all non-breaking spaces to regular
607 var processedLine
= line
.replace(/\s+$/g, '').replace(allNonBreakingSpaces
, space
);
609 return processedLine
;
612 return processedText
;
616 * Binds copy functionality to the given terminal.
619 Terminal
.bindCopy = function(term
) {
620 on(term
.element
, 'copy', function(ev
) {
621 var copiedText
= window
.getSelection().toString(),
622 text
= Terminal
.prepareCopiedTextForClipboard(copiedText
);
624 ev
.clipboardData
.setData('text/plain', text
);
630 * Cancel the cut event completely
632 Terminal
.bindCut = function(term
) {
633 on(term
.element
, 'cut', function (ev
) {
639 Terminal
.bindDrop = function (term
) {
641 * Do not perform the "drop" event. Altering the contents of the
642 * terminal with drag n drop is unwanted behavior.
644 on(term
.element
, 'drop', function (ev
) {
645 term
.cancel(ev
, true);
650 Terminal
.click = function (term
) {
652 * Do not perform the "drop" event. Altering the contents of the
653 * terminal with drag n drop is unwanted behavior.
655 on(term
.element
, 'click', function (ev
) {
656 term
.cancel(ev
, true);
662 * Insert the given row to the terminal or produce a new one
663 * if no row argument is passed. Return the inserted row.
665 Terminal
.prototype.insertRow = function (row
) {
666 if (typeof row
!= 'object') {
667 row
= document
.createElement('div');
670 this.rowContainer
.appendChild(row
);
671 this.children
.push(row
);
678 * Opens the terminal within an element.
680 * @param {HTMLElement} parent The element to create the terminal within.
684 Terminal
.prototype.open = function(parent
) {
685 var self
=this, i
=0, div
;
687 this.parent
= parent
|| this.parent
;
690 throw new Error('Terminal requires a parent element.');
694 * Grab global elements
696 this.context
= this.parent
.ownerDocument
.defaultView
;
697 this.document
= this.parent
.ownerDocument
;
698 this.body
= this.document
.getElementsByTagName('body')[0];
703 if (this.context
.navigator
&& this.context
.navigator
.userAgent
) {
704 this.isMSIE
= !!~this.context
.navigator
.userAgent
.indexOf('MSIE');
708 * Find the users platform. We use this to interpret the meta key
709 * and ISO third level shifts.
710 * http://stackoverflow.com/questions/19877924/what-is-the-list-of-possible-values-for-navigator-platform-as-of-today
712 if (this.context
.navigator
&& this.context
.navigator
.platform
) {
713 this.isMac
= contains(
714 this.context
.navigator
.platform
,
715 ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K']
717 this.isIpad
= this.context
.navigator
.platform
=== 'iPad';
718 this.isIphone
= this.context
.navigator
.platform
=== 'iPhone';
719 this.isMSWindows
= contains(
720 this.context
.navigator
.platform
,
721 ['Windows', 'Win16', 'Win32', 'WinCE']
726 * Create main element container
728 this.element
= this.document
.createElement('div');
729 this.element
.classList
.add('terminal');
730 this.element
.classList
.add('xterm');
731 this.element
.classList
.add('xterm-theme-' + this.theme
);
732 this.element
.setAttribute('tabindex', 0);
733 this.element
.spellcheck
= 'false';
736 * Create the container that will hold the lines of the terminal and then
737 * produce the lines the lines.
739 this.rowContainer
= document
.createElement('div');
740 this.rowContainer
.classList
.add('xterm-rows');
741 this.element
.appendChild(this.rowContainer
);
744 for (; i
< this.rows
; i
++) {
747 this.parent
.appendChild(this.element
);
750 this.refresh(0, this.rows
- 1);
752 // Initialize global actions that
753 // need to be taken on the document.
756 // Ensure there is a Terminal.focus.
759 on(this.element
, 'mouseup', function() {
760 var selection
= document
.getSelection(),
761 collapsed
= selection
.isCollapsed
,
762 isRange
= typeof collapsed
== 'boolean' ? !collapsed
: selection
.type
== 'Range';
768 // Listen for mouse events and translate
769 // them into terminal mouse protocols.
772 // Figure out whether boldness affects
773 // the character width of monospace fonts.
774 if (Terminal
.brokenBold
== null) {
775 Terminal
.brokenBold
= isBoldBroken(this.document
);
783 * Attempts to load an add-on using CommonJS or RequireJS (whichever is available).
784 * @param {string} addon The name of the addon to load
787 Terminal
.loadAddon = function(addon
, callback
) {
788 if (typeof exports
=== 'object' && typeof module
=== 'object') {
790 return require(__dirname
+ '/../addons/' + addon
);
791 } else if (typeof define
== 'function') {
793 return require(['../addons/' + addon
+ '/' + addon
], callback
);
795 console
.error('Cannot load a module without a CommonJS or RequireJS environment.');
801 // XTerm mouse events
802 // http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#Mouse%20Tracking
803 // To better understand these
804 // the xterm code is very helpful:
806 // button.c, charproc.c, misc.c
807 // Relevant functions in xterm/button.c:
808 // BtnCode, EmitButtonCode, EditorButton, SendMousePosition
809 Terminal
.prototype.bindMouse = function() {
810 var el
= this.element
814 var wheelEvent
= 'onmousewheel' in this.context
818 // mouseup, mousedown, mousewheel
819 // left click: ^[[M 3<^[[M#3<
820 // mousewheel up: ^[[M`3>
821 function sendButton(ev
) {
825 // get the xterm-style button
826 button
= getButton(ev
);
828 // get mouse coordinates
832 sendEvent(button
, pos
);
839 // keep it at the left
840 // button, just in case.
851 // motion example of a left click:
852 // ^[[M 3<^[[M@4<^[[M@5<^[[M@6<^[[M@7<^[[M#7<
853 function sendMove(ev
) {
860 // buttons marked as motions
861 // are incremented by 32
864 sendEvent(button
, pos
);
868 // position to characters
869 function encode(data
, ch
) {
870 if (!self
.utfMouse
) {
871 if (ch
=== 255) return data
.push(0);
872 if (ch
> 127) ch
= 127;
875 if (ch
=== 2047) return data
.push(0);
879 if (ch
> 2047) ch
= 2047;
880 data
.push(0xC0 | (ch
>> 6));
881 data
.push(0x80 | (ch
& 0x3F));
886 // send a mouse event:
887 // regular/utf8: ^[[M Cb Cx Cy
888 // urxvt: ^[[ Cb ; Cx ; Cy M
889 // sgr: ^[[ Cb ; Cx ; Cy M/m
890 // vt300: ^[[ 24(1/3/5)~ [ Cx , Cy ] \r
891 // locator: CSI P e ; P b ; P r ; P c ; P p & w
892 function sendEvent(button
, pos
) {
893 // self.emit('mouse', {
899 if (self
.vt300Mouse
) {
901 // http://www.vt100.net/docs/vt3xx-gp/chapter15.html
905 var data
= '\x1b[24';
906 if (button
=== 0) data
+= '1';
907 else if (button
=== 1) data
+= '3';
908 else if (button
=== 2) data
+= '5';
909 else if (button
=== 3) return;
911 data
+= '~[' + pos
.x
+ ',' + pos
.y
+ ']\r';
916 if (self
.decLocator
) {
921 if (button
=== 0) button
= 2;
922 else if (button
=== 1) button
= 4;
923 else if (button
=== 2) button
= 6;
924 else if (button
=== 3) button
= 3;
928 + (button
=== 3 ? 4 : 0)
939 if (self
.urxvtMouse
) {
944 self
.send('\x1b[' + button
+ ';' + pos
.x
+ ';' + pos
.y
+ 'M');
952 + ((button
& 3) === 3 ? button
& ~3 : button
)
957 + ((button
& 3) === 3 ? 'm' : 'M'));
963 encode(data
, button
);
967 self
.send('\x1b[M' + String
.fromCharCode
.apply(String
, data
));
970 function getButton(ev
) {
983 // 1, and 2 - with 64 added
986 button
= ev
.button
!= null
993 button
= button
=== 1 ? 0 : button
=== 4 ? 1 : button
;
999 case 'DOMMouseScroll':
1000 button
= ev
.detail
< 0
1005 button
= ev
.wheelDeltaY
> 0
1011 // next three bits are the modifiers:
1012 // 4 = shift, 8 = meta, 16 = control
1013 shift
= ev
.shiftKey
? 4 : 0;
1014 meta
= ev
.metaKey
? 8 : 0;
1015 ctrl
= ev
.ctrlKey
? 16 : 0;
1016 mod
= shift
| meta
| ctrl
;
1019 if (self
.vt200Mouse
) {
1022 } else if (!self
.normalMouse
) {
1027 button
= (32 + (mod
<< 2)) + button
;
1032 // mouse coordinates measured in cols/rows
1033 function getCoords(ev
) {
1036 // ignore browsers without pageX for now
1037 if (ev
.pageX
== null) return;
1043 // should probably check offsetParent
1044 // but this is more portable
1045 while (el
&& el
!== self
.document
.documentElement
) {
1048 el
= 'offsetParent' in el
1053 // convert to cols/rows
1054 w
= self
.element
.clientWidth
;
1055 h
= self
.element
.clientHeight
;
1056 x
= Math
.round((x
/ w
) * self
.cols
);
1057 y
= Math
.round((y
/ h
) * self
.rows
);
1059 // be sure to avoid sending
1060 // bad positions to the program
1062 if (x
> self
.cols
) x
= self
.cols
;
1064 if (y
> self
.rows
) y
= self
.rows
;
1066 // xterm sends raw bytes and
1067 // starts at 32 (SP) for each.
1074 type
: ev
.type
=== wheelEvent
1080 on(el
, 'mousedown', function(ev
) {
1081 if (!self
.mouseEvents
) return;
1090 //if (self.vt200Mouse && !self.normalMouse) {
1091 if (self
.vt200Mouse
) {
1092 sendButton({ __proto__
: ev
, type
: 'mouseup' });
1093 return self
.cancel(ev
);
1097 if (self
.normalMouse
) on(self
.document
, 'mousemove', sendMove
);
1099 // x10 compatibility mode can't send button releases
1100 if (!self
.x10Mouse
) {
1101 on(self
.document
, 'mouseup', function up(ev
) {
1103 if (self
.normalMouse
) off(self
.document
, 'mousemove', sendMove
);
1104 off(self
.document
, 'mouseup', up
);
1105 return self
.cancel(ev
);
1109 return self
.cancel(ev
);
1112 //if (self.normalMouse) {
1113 // on(self.document, 'mousemove', sendMove);
1116 on(el
, wheelEvent
, function(ev
) {
1117 if (!self
.mouseEvents
) return;
1120 || self
.decLocator
) return;
1122 return self
.cancel(ev
);
1125 // allow mousewheel scrolling in
1126 // the shell for example
1127 on(el
, wheelEvent
, function(ev
) {
1128 if (self
.mouseEvents
) return;
1129 if (self
.applicationKeypad
) return;
1130 if (ev
.type
=== 'DOMMouseScroll') {
1131 self
.scrollDisp(ev
.detail
< 0 ? -1 : 1);
1133 self
.scrollDisp(ev
.wheelDeltaY
> 0 ? -1 : 1);
1135 return self
.cancel(ev
);
1140 * Destroys the terminal.
1144 Terminal
.prototype.destroy = function() {
1145 this.readable
= false;
1146 this.writable
= false;
1148 this.handler = function() {};
1149 this.write = function() {};
1150 if (this.element
.parentNode
) {
1151 this.element
.parentNode
.removeChild(this.element
);
1153 //this.emit('close');
1158 * Flags used to render terminal text properly
1171 * In the screen buffer, each character
1172 * is stored as a an array with a character
1173 * and a 32-bit integer.
1174 * First value: a utf-16 character.
1176 * Next 9 bits: background color (0-511).
1177 * Next 9 bits: foreground color (0-511).
1178 * Next 14 bits: a mask for misc. flags:
1179 * 1=bold, 2=underline, 4=blink, 8=inverse, 16=invisible
1183 * Refreshes terminal content within two rows (inclusive).
1185 * @param {number} start The row to start from (between 0 and terminal's height terminal - 1)
1186 * @param {number} end The row to end at (between fromRow and terminal's height terminal - 1)
1187 * @param {boolean} queue Whether the refresh should ran right now or be queued
1191 Terminal
.prototype.refresh = function(start
, end
, queue
) {
1194 // queue defaults to true
1195 queue
= (typeof queue
== 'undefined') ? true : queue
;
1198 * The refresh queue allows refresh to execute only approximately 30 times a second. For
1199 * commands that pass a significant amount of output to the write function, this prevents the
1200 * terminal from maxing out the CPU and making the UI unresponsive. While commands can still
1201 * run beyond what they do on the terminal, it is far better with a debounce in place as
1202 * every single terminal manipulation does not need to be constructed in the DOM.
1204 * A side-effect of this is that it makes ^C to interrupt a process seem more responsive.
1207 // If refresh should be queued, order the refresh and return.
1208 if (this._refreshIsQueued
) {
1209 // If a refresh has already been queued, just order a full refresh next
1210 this._fullRefreshNext
= true;
1212 setTimeout(function () {
1213 self
.refresh(start
, end
, false);
1215 this._refreshIsQueued
= true;
1220 // If refresh should be run right now (not be queued), release the lock
1221 this._refreshIsQueued
= false;
1223 // If multiple refreshes were requested, make a full refresh.
1224 if (this._fullRefreshNext
) {
1226 end
= this.rows
- 1;
1227 this._fullRefreshNext
= false // reset lock
1230 var x
, y
, i
, line
, out
, ch
, width
, data
, attr
, bg
, fg
, flags
, row
, parent
, focused
= document
.activeElement
;
1232 // If this is a big refresh, remove the terminal rows from the DOM for faster calculations
1233 if (end
- start
>= this.rows
/ 2) {
1234 parent
= this.element
.parentNode
;
1236 this.element
.removeChild(this.rowContainer
);
1243 if (end
>= this.rows
.length
) {
1244 this.log('`end` is too large. Most likely a bad CSR.');
1245 end
= this.rows
.length
- 1;
1248 for (; y
<= end
; y
++) {
1249 row
= y
+ this.ydisp
;
1251 line
= this.lines
[row
];
1254 if (this.y
=== y
- (this.ybase
- this.ydisp
)
1256 && !this.cursorHidden
) {
1262 attr
= this.defAttr
;
1265 for (; i
< width
; i
++) {
1269 if (i
=== x
) data
= -1;
1271 if (data
!== attr
) {
1272 if (attr
!== this.defAttr
) {
1275 if (data
!== this.defAttr
) {
1277 out
+= '<span class="reverse-video terminal-cursor';
1278 if (this.cursorBlink
) {
1283 var classNames
= [];
1286 fg
= (data
>> 9) & 0x1ff;
1289 if (flags
& Terminal
.flags
.BOLD
) {
1290 if (!Terminal
.brokenBold
) {
1291 classNames
.push('xterm-bold');
1293 // See: XTerm*boldColors
1294 if (fg
< 8) fg
+= 8;
1297 if (flags
& Terminal
.flags
.UNDERLINE
) {
1298 classNames
.push('xterm-underline');
1301 if (flags
& Terminal
.flags
.BLINK
) {
1302 classNames
.push('xterm-blink');
1306 * If inverse flag is on, then swap the foreground and background variables.
1308 if (flags
& Terminal
.flags
.INVERSE
) {
1309 /* One-line variable swap in JavaScript: http://stackoverflow.com/a/16201730 */
1310 bg = [fg, fg = bg][0];
1311 // Should inverse just be before the
1312 // above boldColors effect instead?
1313 if ((flags & 1) && fg < 8) fg += 8;
1316 if (flags & Terminal.flags.INVISIBLE) {
1317 classNames.push('xterm-hidden');
1321 * Weird situation: Invert flag used black foreground and white background results
1322 * in invalid background color, positioned at the 256 index of the 256 terminal
1323 * color map. Pin the colors manually in such a case.
1325 * Source: https://github.com/sourcelair/xterm.js/issues/57
1327 if (flags
& Terminal
.flags
.INVERSE
) {
1337 classNames
.push('xterm-bg-color-' + bg
);
1341 classNames
.push('xterm-color-' + fg
);
1345 if (classNames
.length
) {
1346 out
+= ' class="' + classNames
.join(' ') + '"';
1367 if (isWide(ch
)) i
++;
1376 if (attr
!== this.defAttr
) {
1380 this.children
[y
].innerHTML
= out
;
1384 this.element
.appendChild(this.rowContainer
);
1387 this.emit('refresh', {element
: this.element
, start
: start
, end
: end
});
1390 Terminal
.prototype.showCursor = function() {
1391 if (!this.cursorState
) {
1392 this.cursorState
= 1;
1393 this.refresh(this.y
, this.y
);
1397 Terminal
.prototype.scroll = function() {
1400 if (++this.ybase
=== this.scrollback
) {
1401 this.ybase
= this.ybase
/ 2 | 0;
1402 this.lines
= this.lines
.slice(-(this.ybase
+ this.rows
) + 1);
1405 this.ydisp
= this.ybase
;
1408 row
= this.ybase
+ this.rows
- 1;
1410 // subtract the bottom scroll region
1411 row
-= this.rows
- 1 - this.scrollBottom
;
1413 if (row
=== this.lines
.length
) {
1414 // potential optimization:
1415 // pushing is faster than splicing
1416 // when they amount to the same
1418 this.lines
.push(this.blankLine());
1421 this.lines
.splice(row
, 0, this.blankLine());
1424 if (this.scrollTop
!== 0) {
1425 if (this.ybase
!== 0) {
1427 this.ydisp
= this.ybase
;
1429 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
1433 this.updateRange(this.scrollTop
);
1434 this.updateRange(this.scrollBottom
);
1437 Terminal
.prototype.scrollDisp = function(disp
) {
1440 if (this.ydisp
> this.ybase
) {
1441 this.ydisp
= this.ybase
;
1442 } else if (this.ydisp
< 0) {
1446 this.refresh(0, this.rows
- 1);
1450 * Writes text to the terminal.
1452 * @param {string} text The text to write to the terminal.
1456 Terminal
.prototype.write = function(data
) {
1457 var l
= data
.length
, i
= 0, j
, cs
, ch
;
1459 this.refreshStart
= this.y
;
1460 this.refreshEnd
= this.y
;
1462 if (this.ybase
!== this.ydisp
) {
1463 this.ydisp
= this.ybase
;
1467 for (; i
< l
; i
++) {
1469 switch (this.state
) {
1480 if (this.convertEol
) {
1484 if (this.y
> this.scrollBottom
) {
1504 this.x
= this.nextStop();
1519 this.state
= escaped
;
1525 if (this.charset
&& this.charset
[ch
]) {
1526 ch
= this.charset
[ch
];
1529 if (this.x
>= this.cols
) {
1532 if (this.y
> this.scrollBottom
) {
1538 this.lines
[this.y
+ this.ybase
][this.x
] = [this.curAttr
, ch
];
1540 this.updateRange(this.y
);
1543 j
= this.y
+ this.ybase
;
1544 if (this.cols
< 2 || this.x
>= this.cols
) {
1545 this.lines
[j
][this.x
- 1] = [this.curAttr
, ' '];
1548 this.lines
[j
][this.x
] = [this.curAttr
, ' '];
1557 // ESC [ Control Sequence Introducer ( CSI is 0x9b).
1560 this.currentParam
= 0;
1564 // ESC ] Operating System Command ( OSC is 0x9d).
1567 this.currentParam
= 0;
1571 // ESC P Device Control String ( DCS is 0x90).
1574 this.currentParam
= 0;
1578 // ESC _ Application Program Command ( APC is 0x9f).
1580 this.state
= ignore
;
1583 // ESC ^ Privacy Message ( PM is 0x9e).
1585 this.state
= ignore
;
1588 // ESC c Full Reset (RIS).
1593 // ESC E Next Line ( NEL is 0x85).
1594 // ESC D Index ( IND is 0x84).
1602 // ESC M Reverse Index ( RI is 0x8d).
1604 this.reverseIndex();
1607 // ESC % Select default/utf-8 character set.
1608 // @ = default, G = utf-8
1610 //this.charset = null;
1612 this.setgCharset(0, Terminal
.charsets
.US
);
1613 this.state
= normal
;
1617 // ESC (,),*,+,-,. Designate G0-G2 Character Set.
1618 case '(': // <-- this seems to get all the attention
1644 this.state
= charset
;
1647 // Designate G3 Character Set (VT300).
1648 // A = ISO Latin-1 Supplemental.
1652 this.state
= charset
;
1657 // Single Shift Select of G2 Character Set
1658 // ( SS2 is 0x8e). This affects next character only.
1662 // Single Shift Select of G3 Character Set
1663 // ( SS3 is 0x8f). This affects next character only.
1667 // Invoke the G2 Character Set as GL (LS2).
1672 // Invoke the G3 Character Set as GL (LS3).
1677 // Invoke the G3 Character Set as GR (LS3R).
1682 // Invoke the G2 Character Set as GR (LS2R).
1687 // Invoke the G1 Character Set as GR (LS1R).
1692 // ESC 7 Save Cursor (DECSC).
1695 this.state
= normal
;
1698 // ESC 8 Restore Cursor (DECRC).
1700 this.restoreCursor();
1701 this.state
= normal
;
1704 // ESC # 3 DEC line height/width
1706 this.state
= normal
;
1710 // ESC H Tab Set (HTS is 0x88).
1715 // ESC = Application Keypad (DECPAM).
1717 this.log('Serial port requested application keypad.');
1718 this.applicationKeypad
= true;
1719 this.state
= normal
;
1722 // ESC > Normal Keypad (DECPNM).
1724 this.log('Switching back to normal keypad.');
1725 this.applicationKeypad
= false;
1726 this.state
= normal
;
1730 this.state
= normal
;
1731 this.error('Unknown ESC control: %s.', ch
);
1738 case '0': // DEC Special Character and Line Drawing Set.
1739 cs
= Terminal
.charsets
.SCLD
;
1742 cs
= Terminal
.charsets
.UK
;
1744 case 'B': // United States (USASCII).
1745 cs
= Terminal
.charsets
.US
;
1748 cs
= Terminal
.charsets
.Dutch
;
1750 case 'C': // Finnish
1752 cs
= Terminal
.charsets
.Finnish
;
1755 cs
= Terminal
.charsets
.French
;
1757 case 'Q': // FrenchCanadian
1758 cs
= Terminal
.charsets
.FrenchCanadian
;
1761 cs
= Terminal
.charsets
.German
;
1763 case 'Y': // Italian
1764 cs
= Terminal
.charsets
.Italian
;
1766 case 'E': // NorwegianDanish
1768 cs
= Terminal
.charsets
.NorwegianDanish
;
1770 case 'Z': // Spanish
1771 cs
= Terminal
.charsets
.Spanish
;
1773 case 'H': // Swedish
1775 cs
= Terminal
.charsets
.Swedish
;
1778 cs
= Terminal
.charsets
.Swiss
;
1780 case '/': // ISOLatin (actually /A)
1781 cs
= Terminal
.charsets
.ISOLatin
;
1785 cs
= Terminal
.charsets
.US
;
1788 this.setgCharset(this.gcharset
, cs
);
1789 this.gcharset
= null;
1790 this.state
= normal
;
1796 // Set Text Parameters.
1797 if (ch
=== '\x1b' || ch
=== '\x07') {
1798 if (ch
=== '\x1b') i
++;
1800 this.params
.push(this.currentParam
);
1802 switch (this.params
[0]) {
1806 if (this.params
[1]) {
1807 this.title
= this.params
[1];
1808 this.handleTitle(this.title
);
1816 // change dynamic colors
1828 // change dynamic ui colors
1840 // manipulate selection data
1858 this.currentParam
= 0;
1859 this.state
= normal
;
1861 if (!this.params
.length
) {
1862 if (ch
>= '0' && ch
<= '9') {
1864 this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1865 } else if (ch
=== ';') {
1866 this.params
.push(this.currentParam
);
1867 this.currentParam
= '';
1870 this.currentParam
+= ch
;
1877 if (ch
=== '?' || ch
=== '>' || ch
=== '!') {
1883 if (ch
>= '0' && ch
<= '9') {
1884 this.currentParam
= this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1888 // '$', '"', ' ', '\''
1889 if (ch
=== '$' || ch
=== '"' || ch
=== ' ' || ch
=== '\'') {
1894 this.params
.push(this.currentParam
);
1895 this.currentParam
= 0;
1898 if (ch
=== ';') break;
1900 this.state
= normal
;
1904 // Cursor Up Ps Times (default = 1) (CUU).
1906 this.cursorUp(this.params
);
1910 // Cursor Down Ps Times (default = 1) (CUD).
1912 this.cursorDown(this.params
);
1916 // Cursor Forward Ps Times (default = 1) (CUF).
1918 this.cursorForward(this.params
);
1922 // Cursor Backward Ps Times (default = 1) (CUB).
1924 this.cursorBackward(this.params
);
1928 // Cursor Position [row;column] (default = [1,1]) (CUP).
1930 this.cursorPos(this.params
);
1933 // CSI Ps J Erase in Display (ED).
1935 this.eraseInDisplay(this.params
);
1938 // CSI Ps K Erase in Line (EL).
1940 this.eraseInLine(this.params
);
1943 // CSI Pm m Character Attributes (SGR).
1946 this.charAttributes(this.params
);
1950 // CSI Ps n Device Status Report (DSR).
1953 this.deviceStatus(this.params
);
1962 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
1964 this.insertChars(this.params
);
1968 // Cursor Next Line Ps Times (default = 1) (CNL).
1970 this.cursorNextLine(this.params
);
1974 // Cursor Preceding Line Ps Times (default = 1) (CNL).
1976 this.cursorPrecedingLine(this.params
);
1980 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
1982 this.cursorCharAbsolute(this.params
);
1986 // Insert Ps Line(s) (default = 1) (IL).
1988 this.insertLines(this.params
);
1992 // Delete Ps Line(s) (default = 1) (DL).
1994 this.deleteLines(this.params
);
1998 // Delete Ps Character(s) (default = 1) (DCH).
2000 this.deleteChars(this.params
);
2004 // Erase Ps Character(s) (default = 1) (ECH).
2006 this.eraseChars(this.params
);
2009 // CSI Pm ` Character Position Absolute
2010 // [column] (default = [row,1]) (HPA).
2012 this.charPosAbsolute(this.params
);
2016 // Horizontal Position Relative
2018 this.HPositionRelative(this.params
);
2022 // Send Device Attributes (Primary DA).
2024 // Send Device Attributes (Secondary DA)
2026 this.sendDeviceAttributes(this.params
);
2030 // Line Position Absolute [row] (default = [1,column]) (VPA).
2032 this.linePosAbsolute(this.params
);
2035 // 145 65 e * VPR - Vertical Position Relative
2037 this.VPositionRelative(this.params
);
2041 // Horizontal and Vertical Position [row;column] (default =
2044 this.HVPosition(this.params
);
2047 // CSI Pm h Set Mode (SM).
2048 // CSI ? Pm h - mouse escape codes, cursor escape codes
2050 this.setMode(this.params
);
2053 // CSI Pm l Reset Mode (RM).
2056 this.resetMode(this.params
);
2060 // Set Scrolling Region [top;bottom] (default = full size of win-
2064 this.setScrollRegion(this.params
);
2068 // Save cursor (ANSI.SYS).
2070 this.saveCursor(this.params
);
2074 // Restore cursor (ANSI.SYS).
2076 this.restoreCursor(this.params
);
2084 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
2086 this.cursorForwardTab(this.params
);
2089 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
2091 this.scrollUp(this.params
);
2094 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
2095 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
2098 // if (this.prefix === '>') {
2099 // this.resetTitleModes(this.params);
2102 // if (this.params.length > 2) {
2103 // this.initMouseTracking(this.params);
2106 if (this.params
.length
< 2 && !this.prefix
) {
2107 this.scrollDown(this.params
);
2112 // Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
2114 this.cursorBackwardTab(this.params
);
2117 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
2119 this.repeatPrecedingCharacter(this.params
);
2122 // CSI Ps g Tab Clear (TBC).
2124 this.tabClear(this.params
);
2127 // CSI Pm i Media Copy (MC).
2130 // this.mediaCopy(this.params);
2133 // CSI Pm m Character Attributes (SGR).
2135 // case 'm': // duplicate
2136 // if (this.prefix === '>') {
2137 // this.setResources(this.params);
2139 // this.charAttributes(this.params);
2143 // CSI Ps n Device Status Report (DSR).
2145 // case 'n': // duplicate
2146 // if (this.prefix === '>') {
2147 // this.disableModifiers(this.params);
2149 // this.deviceStatus(this.params);
2153 // CSI > Ps p Set pointer mode.
2154 // CSI ! p Soft terminal reset (DECSTR).
2156 // Request ANSI mode (DECRQM).
2158 // Request DEC private mode (DECRQM).
2161 switch (this.prefix
) {
2163 // this.setPointerMode(this.params);
2166 this.softReset(this.params
);
2169 // if (this.postfix === '$') {
2170 // this.requestPrivateMode(this.params);
2174 // if (this.postfix === '"') {
2175 // this.setConformanceLevel(this.params);
2176 // } else if (this.postfix === '$') {
2177 // this.requestAnsiMode(this.params);
2183 // CSI Ps q Load LEDs (DECLL).
2187 // if (this.postfix === ' ') {
2188 // this.setCursorStyle(this.params);
2191 // if (this.postfix === '"') {
2192 // this.setCharProtectionAttr(this.params);
2195 // this.loadLEDs(this.params);
2199 // Set Scrolling Region [top;bottom] (default = full size of win-
2202 // CSI Pt; Pl; Pb; Pr; Ps$ r
2203 // case 'r': // duplicate
2204 // if (this.prefix === '?') {
2205 // this.restorePrivateValues(this.params);
2206 // } else if (this.postfix === '$') {
2207 // this.setAttrInRectangle(this.params);
2209 // this.setScrollRegion(this.params);
2213 // CSI s Save cursor (ANSI.SYS).
2215 // case 's': // duplicate
2216 // if (this.prefix === '?') {
2217 // this.savePrivateValues(this.params);
2219 // this.saveCursor(this.params);
2223 // CSI Ps ; Ps ; Ps t
2224 // CSI Pt; Pl; Pb; Pr; Ps$ t
2228 // if (this.postfix === '$') {
2229 // this.reverseAttrInRectangle(this.params);
2230 // } else if (this.postfix === ' ') {
2231 // this.setWarningBellVolume(this.params);
2233 // if (this.prefix === '>') {
2234 // this.setTitleModeFeature(this.params);
2236 // this.manipulateWindow(this.params);
2241 // CSI u Restore cursor (ANSI.SYS).
2243 // case 'u': // duplicate
2244 // if (this.postfix === ' ') {
2245 // this.setMarginBellVolume(this.params);
2247 // this.restoreCursor(this.params);
2251 // CSI Pt; Pl; Pb; Pr; Pp; Pt; Pl; Pp$ v
2253 // if (this.postfix === '$') {
2254 // this.copyRectagle(this.params);
2258 // CSI Pt ; Pl ; Pb ; Pr ' w
2260 // if (this.postfix === '\'') {
2261 // this.enableFilterRectangle(this.params);
2265 // CSI Ps x Request Terminal Parameters (DECREQTPARM).
2266 // CSI Ps x Select Attribute Change Extent (DECSACE).
2267 // CSI Pc; Pt; Pl; Pb; Pr$ x
2269 // if (this.postfix === '$') {
2270 // this.fillRectangle(this.params);
2272 // this.requestParameters(this.params);
2273 // //this.__(this.params);
2278 // CSI Pt; Pl; Pb; Pr$ z
2280 // if (this.postfix === '\'') {
2281 // this.enableLocatorReporting(this.params);
2282 // } else if (this.postfix === '$') {
2283 // this.eraseRectangle(this.params);
2288 // CSI Pt; Pl; Pb; Pr$ {
2290 // if (this.postfix === '\'') {
2291 // this.setLocatorEvents(this.params);
2292 // } else if (this.postfix === '$') {
2293 // this.selectiveEraseRectangle(this.params);
2299 // if (this.postfix === '\'') {
2300 // this.requestLocatorPosition(this.params);
2305 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
2307 // if (this.postfix === ' ') {
2308 // this.insertColumns(this.params);
2313 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
2315 // if (this.postfix === ' ') {
2316 // this.deleteColumns(this.params);
2321 this.error('Unknown CSI code: %s.', ch
);
2330 if (ch
=== '\x1b' || ch
=== '\x07') {
2331 if (ch
=== '\x1b') i
++;
2333 switch (this.prefix
) {
2334 // User-Defined Keys (DECUDK).
2338 // Request Status String (DECRQSS).
2339 // test: echo -e '\eP$q"p\e\\'
2341 var pt
= this.currentParam
2358 + (this.scrollTop
+ 1)
2360 + (this.scrollBottom
+ 1)
2370 this.error('Unknown DCS Pt: %s.', pt
);
2375 this.send('\x1bP' + +valid
+ '$r' + pt
+ '\x1b\\');
2378 // Set Termcap/Terminfo Data (xterm, experimental).
2382 // Request Termcap/Terminfo String (xterm, experimental)
2383 // Regular xterm does not even respond to this sequence.
2384 // This can cause a small glitch in vim.
2385 // test: echo -ne '\eP+q6b64\e\\'
2387 var pt
= this.currentParam
2390 this.send('\x1bP' + +valid
+ '+r' + pt
+ '\x1b\\');
2394 this.error('Unknown DCS prefix: %s.', this.prefix
);
2398 this.currentParam
= 0;
2400 this.state
= normal
;
2401 } else if (!this.currentParam
) {
2402 if (!this.prefix
&& ch
!== '$' && ch
!== '+') {
2403 this.currentParam
= ch
;
2404 } else if (this.prefix
.length
=== 2) {
2405 this.currentParam
= ch
;
2410 this.currentParam
+= ch
;
2416 if (ch
=== '\x1b' || ch
=== '\x07') {
2417 if (ch
=== '\x1b') i
++;
2418 this.state
= normal
;
2424 this.updateRange(this.y
);
2425 this.refresh(this.refreshStart
, this.refreshEnd
);
2428 Terminal
.prototype.writeln = function(data
) {
2429 this.write(data
+ '\r\n');
2433 // https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
2434 Terminal
.prototype.keyDown = function(ev
) {
2436 var result
= this.evaluateKeyEscapeSequence(ev
);
2438 if (result
.scrollDisp
) {
2439 this.scrollDisp(result
.scrollDisp
);
2440 return this.cancel(ev
);
2443 if (isThirdLevelShift(this, ev
)) {
2447 if (result
.cancel
) {
2448 // The event is canceled at the end already, is this necessary?
2449 this.cancel(ev
, true);
2456 this.emit('keydown', ev
);
2457 this.emit('key', result
.key
, ev
);
2459 this.handler(result
.key
);
2461 return this.cancel(ev
, true);
2465 * Returns an object that determines how a KeyboardEvent should be handled. The key of the
2466 * returned value is the new key code to pass to the PTY.
2468 * Reference: http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
2470 Terminal
.prototype.evaluateKeyEscapeSequence = function(ev
) {
2472 // Whether to cancel event propogation (NOTE: this may not be needed since the event is
2473 // canceled at the end of keyDown
2475 // The new key even to emit
2477 // The number of characters to scroll, if this is defined it will cancel the event
2478 scrollDisp
: undefined
2480 switch (ev
.keyCode
) {
2484 result
.key
= '\x08'; // ^H
2487 result
.key
= '\x7f'; // ^?
2492 result
.key
= '\x1b[Z';
2496 result
.cancel
= true;
2501 result
.cancel
= true;
2505 result
.key
= '\x1b';
2506 result
.cancel
= true;
2511 result
.key
= '\x1bb' // Jump a word back
2512 result
.cancel
= true;
2516 result
.key
= '\x1b[5D'; // Jump a word back
2519 if (this.applicationCursor
) {
2520 result
.key
= '\x1bOD'; // SS3 as ^[O for 7-bit
2523 result
.key
= '\x1b[D';
2528 result
.key
= '\x1bf' // Jump a word forward
2529 result
.cancel
= true;
2533 result
.key
= '\x1b[5C'; // Jump a word forward
2536 if (this.applicationCursor
) {
2537 result
.key
= '\x1bOC';
2540 result
.key
= '\x1b[C';
2544 if (this.applicationCursor
) {
2545 result
.key
= '\x1bOA';
2549 result
.scrollDisp
= -1;
2551 result
.key
= '\x1b[A';
2556 if (this.applicationCursor
) {
2557 result
.key
= '\x1bOB';
2561 result
.scrollDisp
= 1;
2563 result
.key
= '\x1b[B';
2568 if (!ev
.shiftKey
&& !ev
.ctrlKey
) {
2569 // <Ctrl> or <Shift> + <Insert> are used to
2570 // copy-paste on some systems.
2571 result
.key
= '\x1b[2~';
2575 case 46: result
.key
= '\x1b[3~'; break;
2578 if (this.applicationKeypad
) {
2579 result
.key
= '\x1bOH';
2582 result
.key
= '\x1bOH';
2586 if (this.applicationKeypad
) {
2587 result
.key
= '\x1bOF';
2590 result
.key
= '\x1bOF';
2595 result
.scrollDisp
= -(this.rows
- 1);
2597 result
.key
= '\x1b[5~';
2603 result
.scrollDisp
= this.rows
- 1;
2605 result
.key
= '\x1b[6~';
2609 case 112: result
.key
= '\x1bOP'; break;
2610 case 113: result
.key
= '\x1bOQ'; break;
2611 case 114: result
.key
= '\x1bOR'; break;
2612 case 115: result
.key
= '\x1bOS'; break;
2613 case 116: result
.key
= '\x1b[15~'; break;
2614 case 117: result
.key
= '\x1b[17~'; break;
2615 case 118: result
.key
= '\x1b[18~'; break;
2616 case 119: result
.key
= '\x1b[19~'; break;
2617 case 120: result
.key
= '\x1b[20~'; break;
2618 case 121: result
.key
= '\x1b[21~'; break;
2619 case 122: result
.key
= '\x1b[23~'; break;
2620 case 123: result
.key
= '\x1b[24~'; break;
2623 if (ev
.ctrlKey
&& !ev
.shiftKey
&& !ev
.altKey
&& !ev
.metaKey
) {
2624 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2625 result
.key
= String
.fromCharCode(ev
.keyCode
- 64);
2626 } else if (ev
.keyCode
=== 32) {
2628 result
.key
= String
.fromCharCode(0);
2629 } else if (ev
.keyCode
>= 51 && ev
.keyCode
<= 55) {
2630 // escape, file sep, group sep, record sep, unit sep
2631 result
.key
= String
.fromCharCode(ev
.keyCode
- 51 + 27);
2632 } else if (ev
.keyCode
=== 56) {
2634 result
.key
= String
.fromCharCode(127);
2635 } else if (ev
.keyCode
=== 219) {
2637 result
.key
= String
.fromCharCode(27);
2638 } else if (ev
.keyCode
=== 221) {
2640 result
.key
= String
.fromCharCode(29);
2642 } else if (!this.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) {
2643 // On Mac this is a third level shift. Use <Esc> instead.
2644 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2645 result
.key
= '\x1b' + String
.fromCharCode(ev
.keyCode
+ 32);
2646 } else if (ev
.keyCode
=== 192) {
2647 result
.key
= '\x1b`';
2648 } else if (ev
.keyCode
>= 48 && ev
.keyCode
<= 57) {
2649 result
.key
= '\x1b' + (ev
.keyCode
- 48);
2657 Terminal
.prototype.setgLevel = function(g
) {
2659 this.charset
= this.charsets
[g
];
2662 Terminal
.prototype.setgCharset = function(g
, charset
) {
2663 this.charsets
[g
] = charset
;
2664 if (this.glevel
=== g
) {
2665 this.charset
= charset
;
2669 Terminal
.prototype.keyPress = function(ev
) {
2674 } else if (ev
.which
== null) {
2676 } else if (ev
.which
!== 0 && ev
.charCode
!== 0) {
2683 (ev
.altKey
|| ev
.ctrlKey
|| ev
.metaKey
) && !isThirdLevelShift(this, ev
)
2688 key
= String
.fromCharCode(key
);
2691 * When a key is pressed and a character is sent to the terminal, then clear any text
2692 * selected in the terminal.
2695 this.clearSelection();
2698 this.emit('keypress', key
, ev
);
2699 this.emit('key', key
, ev
);
2703 this.cancel(ev
, true);
2708 Terminal
.prototype.send = function(data
) {
2712 setTimeout(function() {
2713 self
.handler(self
.queue
);
2721 Terminal
.prototype.bell = function() {
2722 if (!this.visualBell
) return;
2724 this.element
.style
.borderColor
= 'white';
2725 setTimeout(function() {
2726 self
.element
.style
.borderColor
= '';
2728 if (this.popOnBell
) this.focus();
2731 Terminal
.prototype.log = function() {
2732 if (!this.debug
) return;
2733 if (!this.context
.console
|| !this.context
.console
.log
) return;
2734 var args
= Array
.prototype.slice
.call(arguments
);
2735 this.context
.console
.log
.apply(this.context
.console
, args
);
2738 Terminal
.prototype.error = function() {
2739 if (!this.debug
) return;
2740 if (!this.context
.console
|| !this.context
.console
.error
) return;
2741 var args
= Array
.prototype.slice
.call(arguments
);
2742 this.context
.console
.error
.apply(this.context
.console
, args
);
2746 * Resizes the terminal.
2748 * @param {number} x The number of columns to resize to.
2749 * @param {number} y The number of rows to resize to.
2753 Terminal
.prototype.resize = function(x
, y
) {
2761 if (x
=== this.cols
&& y
=== this.rows
) {
2771 ch
= [this.defAttr
, ' ']; // does xterm use the default attr?
2772 i
= this.lines
.length
;
2774 while (this.lines
[i
].length
< x
) {
2775 this.lines
[i
].push(ch
);
2779 i
= this.lines
.length
;
2781 while (this.lines
[i
].length
> x
) {
2782 this.lines
[i
].pop();
2795 // y is rows, not this.y
2796 if (this.lines
.length
< y
+ this.ybase
) {
2797 if (this.ybase
> 0 && this.lines
.length
<= this.ybase
+ this.y
+ addToY
+ 1) {
2798 // There is room above the buffer and there are no empty elements below the line,
2804 // Add a blank line if there is no buffer left at the top to scroll to, or if there
2805 // are blank lines after the cursor
2806 this.lines
.push(this.blankLine());
2809 if (this.children
.length
< y
) {
2815 if (this.lines
.length
> y
+ this.ybase
) {
2816 if (this.lines
.length
> this.ybase
+ this.y
+ 1) {
2817 // The line is a blank line below the cursor, remove it
2820 // The line is the cursor, scroll down
2825 if (this.children
.length
> y
) {
2826 el
= this.children
.shift();
2828 el
.parentNode
.removeChild(el
);
2835 * Make sure that the cursor stays on screen
2849 this.scrollBottom
= y
- 1;
2851 this.refresh(0, this.rows
- 1);
2855 this.emit('resize', {terminal
: this, cols
: x
, rows
: y
});
2858 Terminal
.prototype.updateRange = function(y
) {
2859 if (y
< this.refreshStart
) this.refreshStart
= y
;
2860 if (y
> this.refreshEnd
) this.refreshEnd
= y
;
2861 // if (y > this.refreshEnd) {
2862 // this.refreshEnd = y;
2863 // if (y > this.rows - 1) {
2864 // this.refreshEnd = this.rows - 1;
2869 Terminal
.prototype.maxRange = function() {
2870 this.refreshStart
= 0;
2871 this.refreshEnd
= this.rows
- 1;
2874 Terminal
.prototype.setupStops = function(i
) {
2876 if (!this.tabs
[i
]) {
2877 i
= this.prevStop(i
);
2884 for (; i
< this.cols
; i
+= 8) {
2885 this.tabs
[i
] = true;
2889 Terminal
.prototype.prevStop = function(x
) {
2890 if (x
== null) x
= this.x
;
2891 while (!this.tabs
[--x
] && x
> 0);
2892 return x
>= this.cols
2897 Terminal
.prototype.nextStop = function(x
) {
2898 if (x
== null) x
= this.x
;
2899 while (!this.tabs
[++x
] && x
< this.cols
);
2900 return x
>= this.cols
2905 Terminal
.prototype.eraseRight = function(x
, y
) {
2906 var line
= this.lines
[this.ybase
+ y
]
2907 , ch
= [this.eraseAttr(), ' ']; // xterm
2910 for (; x
< this.cols
; x
++) {
2914 this.updateRange(y
);
2917 Terminal
.prototype.eraseLeft = function(x
, y
) {
2918 var line
= this.lines
[this.ybase
+ y
]
2919 , ch
= [this.eraseAttr(), ' ']; // xterm
2922 while (x
--) line
[x
] = ch
;
2924 this.updateRange(y
);
2927 Terminal
.prototype.eraseLine = function(y
) {
2928 this.eraseRight(0, y
);
2931 Terminal
.prototype.blankLine = function(cur
) {
2936 var ch
= [attr
, ' ']
2940 for (; i
< this.cols
; i
++) {
2947 Terminal
.prototype.ch = function(cur
) {
2949 ? [this.eraseAttr(), ' ']
2950 : [this.defAttr
, ' '];
2953 Terminal
.prototype.is = function(term
) {
2954 var name
= this.termName
;
2955 return (name
+ '').indexOf(term
) === 0;
2958 Terminal
.prototype.handler = function(data
) {
2959 this.emit('data', data
);
2962 Terminal
.prototype.handleTitle = function(title
) {
2963 this.emit('title', title
);
2970 // ESC D Index (IND is 0x84).
2971 Terminal
.prototype.index = function() {
2973 if (this.y
> this.scrollBottom
) {
2977 this.state
= normal
;
2980 // ESC M Reverse Index (RI is 0x8d).
2981 Terminal
.prototype.reverseIndex = function() {
2984 if (this.y
< this.scrollTop
) {
2986 // possibly move the code below to term.reverseScroll();
2987 // test: echo -ne '\e[1;1H\e[44m\eM\e[0m'
2988 // blankLine(true) is xterm/linux behavior
2989 this.lines
.splice(this.y
+ this.ybase
, 0, this.blankLine(true));
2990 j
= this.rows
- 1 - this.scrollBottom
;
2991 this.lines
.splice(this.rows
- 1 + this.ybase
- j
+ 1, 1);
2993 this.updateRange(this.scrollTop
);
2994 this.updateRange(this.scrollBottom
);
2996 this.state
= normal
;
2999 // ESC c Full Reset (RIS).
3000 Terminal
.prototype.reset = function() {
3001 this.options
.rows
= this.rows
;
3002 this.options
.cols
= this.cols
;
3003 Terminal
.call(this, this.options
);
3004 this.refresh(0, this.rows
- 1);
3007 // ESC H Tab Set (HTS is 0x88).
3008 Terminal
.prototype.tabSet = function() {
3009 this.tabs
[this.x
] = true;
3010 this.state
= normal
;
3018 // Cursor Up Ps Times (default = 1) (CUU).
3019 Terminal
.prototype.cursorUp = function(params
) {
3020 var param
= params
[0];
3021 if (param
< 1) param
= 1;
3023 if (this.y
< 0) this.y
= 0;
3027 // Cursor Down Ps Times (default = 1) (CUD).
3028 Terminal
.prototype.cursorDown = function(params
) {
3029 var param
= params
[0];
3030 if (param
< 1) param
= 1;
3032 if (this.y
>= this.rows
) {
3033 this.y
= this.rows
- 1;
3038 // Cursor Forward Ps Times (default = 1) (CUF).
3039 Terminal
.prototype.cursorForward = function(params
) {
3040 var param
= params
[0];
3041 if (param
< 1) param
= 1;
3043 if (this.x
>= this.cols
) {
3044 this.x
= this.cols
- 1;
3049 // Cursor Backward Ps Times (default = 1) (CUB).
3050 Terminal
.prototype.cursorBackward = function(params
) {
3051 var param
= params
[0];
3052 if (param
< 1) param
= 1;
3054 if (this.x
< 0) this.x
= 0;
3058 // Cursor Position [row;column] (default = [1,1]) (CUP).
3059 Terminal
.prototype.cursorPos = function(params
) {
3062 row
= params
[0] - 1;
3064 if (params
.length
>= 2) {
3065 col
= params
[1] - 1;
3072 } else if (row
>= this.rows
) {
3073 row
= this.rows
- 1;
3078 } else if (col
>= this.cols
) {
3079 col
= this.cols
- 1;
3086 // CSI Ps J Erase in Display (ED).
3087 // Ps = 0 -> Erase Below (default).
3088 // Ps = 1 -> Erase Above.
3089 // Ps = 2 -> Erase All.
3090 // Ps = 3 -> Erase Saved Lines (xterm).
3092 // Erase in Display (DECSED).
3093 // Ps = 0 -> Selective Erase Below (default).
3094 // Ps = 1 -> Selective Erase Above.
3095 // Ps = 2 -> Selective Erase All.
3096 Terminal
.prototype.eraseInDisplay = function(params
) {
3098 switch (params
[0]) {
3100 this.eraseRight(this.x
, this.y
);
3102 for (; j
< this.rows
; j
++) {
3107 this.eraseLeft(this.x
, this.y
);
3115 while (j
--) this.eraseLine(j
);
3123 // CSI Ps K Erase in Line (EL).
3124 // Ps = 0 -> Erase to Right (default).
3125 // Ps = 1 -> Erase to Left.
3126 // Ps = 2 -> Erase All.
3128 // Erase in Line (DECSEL).
3129 // Ps = 0 -> Selective Erase to Right (default).
3130 // Ps = 1 -> Selective Erase to Left.
3131 // Ps = 2 -> Selective Erase All.
3132 Terminal
.prototype.eraseInLine = function(params
) {
3133 switch (params
[0]) {
3135 this.eraseRight(this.x
, this.y
);
3138 this.eraseLeft(this.x
, this.y
);
3141 this.eraseLine(this.y
);
3146 // CSI Pm m Character Attributes (SGR).
3147 // Ps = 0 -> Normal (default).
3149 // Ps = 4 -> Underlined.
3150 // Ps = 5 -> Blink (appears as Bold).
3151 // Ps = 7 -> Inverse.
3152 // Ps = 8 -> Invisible, i.e., hidden (VT300).
3153 // Ps = 2 2 -> Normal (neither bold nor faint).
3154 // Ps = 2 4 -> Not underlined.
3155 // Ps = 2 5 -> Steady (not blinking).
3156 // Ps = 2 7 -> Positive (not inverse).
3157 // Ps = 2 8 -> Visible, i.e., not hidden (VT300).
3158 // Ps = 3 0 -> Set foreground color to Black.
3159 // Ps = 3 1 -> Set foreground color to Red.
3160 // Ps = 3 2 -> Set foreground color to Green.
3161 // Ps = 3 3 -> Set foreground color to Yellow.
3162 // Ps = 3 4 -> Set foreground color to Blue.
3163 // Ps = 3 5 -> Set foreground color to Magenta.
3164 // Ps = 3 6 -> Set foreground color to Cyan.
3165 // Ps = 3 7 -> Set foreground color to White.
3166 // Ps = 3 9 -> Set foreground color to default (original).
3167 // Ps = 4 0 -> Set background color to Black.
3168 // Ps = 4 1 -> Set background color to Red.
3169 // Ps = 4 2 -> Set background color to Green.
3170 // Ps = 4 3 -> Set background color to Yellow.
3171 // Ps = 4 4 -> Set background color to Blue.
3172 // Ps = 4 5 -> Set background color to Magenta.
3173 // Ps = 4 6 -> Set background color to Cyan.
3174 // Ps = 4 7 -> Set background color to White.
3175 // Ps = 4 9 -> Set background color to default (original).
3177 // If 16-color support is compiled, the following apply. Assume
3178 // that xterm's resources are set so that the ISO color codes are
3179 // the first 8 of a set of 16. Then the aixterm colors are the
3180 // bright versions of the ISO colors:
3181 // Ps = 9 0 -> Set foreground color to Black.
3182 // Ps = 9 1 -> Set foreground color to Red.
3183 // Ps = 9 2 -> Set foreground color to Green.
3184 // Ps = 9 3 -> Set foreground color to Yellow.
3185 // Ps = 9 4 -> Set foreground color to Blue.
3186 // Ps = 9 5 -> Set foreground color to Magenta.
3187 // Ps = 9 6 -> Set foreground color to Cyan.
3188 // Ps = 9 7 -> Set foreground color to White.
3189 // Ps = 1 0 0 -> Set background color to Black.
3190 // Ps = 1 0 1 -> Set background color to Red.
3191 // Ps = 1 0 2 -> Set background color to Green.
3192 // Ps = 1 0 3 -> Set background color to Yellow.
3193 // Ps = 1 0 4 -> Set background color to Blue.
3194 // Ps = 1 0 5 -> Set background color to Magenta.
3195 // Ps = 1 0 6 -> Set background color to Cyan.
3196 // Ps = 1 0 7 -> Set background color to White.
3198 // If xterm is compiled with the 16-color support disabled, it
3199 // supports the following, from rxvt:
3200 // Ps = 1 0 0 -> Set foreground and background color to
3203 // If 88- or 256-color support is compiled, the following apply.
3204 // Ps = 3 8 ; 5 ; Ps -> Set foreground color to the second
3206 // Ps = 4 8 ; 5 ; Ps -> Set background color to the second
3208 Terminal
.prototype.charAttributes = function(params
) {
3209 // Optimize a single SGR0.
3210 if (params
.length
=== 1 && params
[0] === 0) {
3211 this.curAttr
= this.defAttr
;
3215 var l
= params
.length
3217 , flags
= this.curAttr
>> 18
3218 , fg
= (this.curAttr
>> 9) & 0x1ff
3219 , bg
= this.curAttr
& 0x1ff
3222 for (; i
< l
; i
++) {
3224 if (p
>= 30 && p
<= 37) {
3227 } else if (p
>= 40 && p
<= 47) {
3230 } else if (p
>= 90 && p
<= 97) {
3234 } else if (p
>= 100 && p
<= 107) {
3238 } else if (p
=== 0) {
3240 flags
= this.defAttr
>> 18;
3241 fg
= (this.defAttr
>> 9) & 0x1ff;
3242 bg
= this.defAttr
& 0x1ff;
3246 } else if (p
=== 1) {
3249 } else if (p
=== 4) {
3252 } else if (p
=== 5) {
3255 } else if (p
=== 7) {
3256 // inverse and positive
3257 // test with: echo -e '\e[31m\e[42mhello\e[7mworld\e[27mhi\e[m'
3259 } else if (p
=== 8) {
3262 } else if (p
=== 22) {
3265 } else if (p
=== 24) {
3268 } else if (p
=== 25) {
3271 } else if (p
=== 27) {
3274 } else if (p
=== 28) {
3277 } else if (p
=== 39) {
3279 fg
= (this.defAttr
>> 9) & 0x1ff;
3280 } else if (p
=== 49) {
3282 bg
= this.defAttr
& 0x1ff;
3283 } else if (p
=== 38) {
3285 if (params
[i
+ 1] === 2) {
3289 params
[i
+ 1] & 0xff,
3290 params
[i
+ 2] & 0xff);
3291 if (fg
=== -1) fg
= 0x1ff;
3293 } else if (params
[i
+ 1] === 5) {
3295 p
= params
[i
] & 0xff;
3298 } else if (p
=== 48) {
3300 if (params
[i
+ 1] === 2) {
3304 params
[i
+ 1] & 0xff,
3305 params
[i
+ 2] & 0xff);
3306 if (bg
=== -1) bg
= 0x1ff;
3308 } else if (params
[i
+ 1] === 5) {
3310 p
= params
[i
] & 0xff;
3313 } else if (p
=== 100) {
3315 fg
= (this.defAttr
>> 9) & 0x1ff;
3316 bg
= this.defAttr
& 0x1ff;
3318 this.error('Unknown SGR attribute: %d.', p
);
3322 this.curAttr
= (flags
<< 18) | (fg
<< 9) | bg
;
3325 // CSI Ps n Device Status Report (DSR).
3326 // Ps = 5 -> Status Report. Result (``OK'') is
3328 // Ps = 6 -> Report Cursor Position (CPR) [row;column].
3332 // Device Status Report (DSR, DEC-specific).
3333 // Ps = 6 -> Report Cursor Position (CPR) [row;column] as CSI
3334 // ? r ; c R (assumes page is zero).
3335 // Ps = 1 5 -> Report Printer status as CSI ? 1 0 n (ready).
3336 // or CSI ? 1 1 n (not ready).
3337 // Ps = 2 5 -> Report UDK status as CSI ? 2 0 n (unlocked)
3338 // or CSI ? 2 1 n (locked).
3339 // Ps = 2 6 -> Report Keyboard status as
3340 // CSI ? 2 7 ; 1 ; 0 ; 0 n (North American).
3341 // The last two parameters apply to VT400 & up, and denote key-
3342 // board ready and LK01 respectively.
3343 // Ps = 5 3 -> Report Locator status as
3344 // CSI ? 5 3 n Locator available, if compiled-in, or
3345 // CSI ? 5 0 n No Locator, if not.
3346 Terminal
.prototype.deviceStatus = function(params
) {
3348 switch (params
[0]) {
3351 this.send('\x1b[0n');
3362 } else if (this.prefix
=== '?') {
3363 // modern xterm doesnt seem to
3364 // respond to any of these except ?6, 6, and 5
3365 switch (params
[0]) {
3376 // this.send('\x1b[?11n');
3379 // dont support user defined keys
3380 // this.send('\x1b[?21n');
3383 // north american keyboard
3384 // this.send('\x1b[?27;1;0;0n');
3387 // no dec locator/mouse
3388 // this.send('\x1b[?50n');
3399 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
3400 Terminal
.prototype.insertChars = function(params
) {
3401 var param
, row
, j
, ch
;
3404 if (param
< 1) param
= 1;
3406 row
= this.y
+ this.ybase
;
3408 ch
= [this.eraseAttr(), ' ']; // xterm
3410 while (param
-- && j
< this.cols
) {
3411 this.lines
[row
].splice(j
++, 0, ch
);
3412 this.lines
[row
].pop();
3417 // Cursor Next Line Ps Times (default = 1) (CNL).
3418 // same as CSI Ps B ?
3419 Terminal
.prototype.cursorNextLine = function(params
) {
3420 var param
= params
[0];
3421 if (param
< 1) param
= 1;
3423 if (this.y
>= this.rows
) {
3424 this.y
= this.rows
- 1;
3430 // Cursor Preceding Line Ps Times (default = 1) (CNL).
3432 Terminal
.prototype.cursorPrecedingLine = function(params
) {
3433 var param
= params
[0];
3434 if (param
< 1) param
= 1;
3436 if (this.y
< 0) this.y
= 0;
3441 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
3442 Terminal
.prototype.cursorCharAbsolute = function(params
) {
3443 var param
= params
[0];
3444 if (param
< 1) param
= 1;
3449 // Insert Ps Line(s) (default = 1) (IL).
3450 Terminal
.prototype.insertLines = function(params
) {
3454 if (param
< 1) param
= 1;
3455 row
= this.y
+ this.ybase
;
3457 j
= this.rows
- 1 - this.scrollBottom
;
3458 j
= this.rows
- 1 + this.ybase
- j
+ 1;
3461 // test: echo -e '\e[44m\e[1L\e[0m'
3462 // blankLine(true) - xterm/linux behavior
3463 this.lines
.splice(row
, 0, this.blankLine(true));
3464 this.lines
.splice(j
, 1);
3468 this.updateRange(this.y
);
3469 this.updateRange(this.scrollBottom
);
3473 // Delete Ps Line(s) (default = 1) (DL).
3474 Terminal
.prototype.deleteLines = function(params
) {
3478 if (param
< 1) param
= 1;
3479 row
= this.y
+ this.ybase
;
3481 j
= this.rows
- 1 - this.scrollBottom
;
3482 j
= this.rows
- 1 + this.ybase
- j
;
3485 // test: echo -e '\e[44m\e[1M\e[0m'
3486 // blankLine(true) - xterm/linux behavior
3487 this.lines
.splice(j
+ 1, 0, this.blankLine(true));
3488 this.lines
.splice(row
, 1);
3492 this.updateRange(this.y
);
3493 this.updateRange(this.scrollBottom
);
3497 // Delete Ps Character(s) (default = 1) (DCH).
3498 Terminal
.prototype.deleteChars = function(params
) {
3502 if (param
< 1) param
= 1;
3504 row
= this.y
+ this.ybase
;
3505 ch
= [this.eraseAttr(), ' ']; // xterm
3508 this.lines
[row
].splice(this.x
, 1);
3509 this.lines
[row
].push(ch
);
3514 // Erase Ps Character(s) (default = 1) (ECH).
3515 Terminal
.prototype.eraseChars = function(params
) {
3516 var param
, row
, j
, ch
;
3519 if (param
< 1) param
= 1;
3521 row
= this.y
+ this.ybase
;
3523 ch
= [this.eraseAttr(), ' ']; // xterm
3525 while (param
-- && j
< this.cols
) {
3526 this.lines
[row
][j
++] = ch
;
3530 // CSI Pm ` Character Position Absolute
3531 // [column] (default = [row,1]) (HPA).
3532 Terminal
.prototype.charPosAbsolute = function(params
) {
3533 var param
= params
[0];
3534 if (param
< 1) param
= 1;
3536 if (this.x
>= this.cols
) {
3537 this.x
= this.cols
- 1;
3542 // Horizontal Position Relative
3544 Terminal
.prototype.HPositionRelative = function(params
) {
3545 var param
= params
[0];
3546 if (param
< 1) param
= 1;
3548 if (this.x
>= this.cols
) {
3549 this.x
= this.cols
- 1;
3553 // CSI Ps c Send Device Attributes (Primary DA).
3554 // Ps = 0 or omitted -> request attributes from terminal. The
3555 // response depends on the decTerminalID resource setting.
3556 // -> CSI ? 1 ; 2 c (``VT100 with Advanced Video Option'')
3557 // -> CSI ? 1 ; 0 c (``VT101 with No Options'')
3558 // -> CSI ? 6 c (``VT102'')
3559 // -> CSI ? 6 0 ; 1 ; 2 ; 6 ; 8 ; 9 ; 1 5 ; c (``VT220'')
3560 // The VT100-style response parameters do not mean anything by
3561 // themselves. VT220 parameters do, telling the host what fea-
3562 // tures the terminal supports:
3563 // Ps = 1 -> 132-columns.
3564 // Ps = 2 -> Printer.
3565 // Ps = 6 -> Selective erase.
3566 // Ps = 8 -> User-defined keys.
3567 // Ps = 9 -> National replacement character sets.
3568 // Ps = 1 5 -> Technical characters.
3569 // Ps = 2 2 -> ANSI color, e.g., VT525.
3570 // Ps = 2 9 -> ANSI text locator (i.e., DEC Locator mode).
3572 // Send Device Attributes (Secondary DA).
3573 // Ps = 0 or omitted -> request the terminal's identification
3574 // code. The response depends on the decTerminalID resource set-
3575 // ting. It should apply only to VT220 and up, but xterm extends
3577 // -> CSI > Pp ; Pv ; Pc c
3578 // where Pp denotes the terminal type
3579 // Pp = 0 -> ``VT100''.
3580 // Pp = 1 -> ``VT220''.
3581 // and Pv is the firmware version (for xterm, this was originally
3582 // the XFree86 patch number, starting with 95). In a DEC termi-
3583 // nal, Pc indicates the ROM cartridge registration number and is
3585 // More information:
3586 // xterm/charproc.c - line 2012, for more information.
3587 // vim responds with ^[[?0c or ^[[?1c after the terminal's response (?)
3588 Terminal
.prototype.sendDeviceAttributes = function(params
) {
3589 if (params
[0] > 0) return;
3592 if (this.is('xterm')
3593 || this.is('rxvt-unicode')
3594 || this.is('screen')) {
3595 this.send('\x1b[?1;2c');
3596 } else if (this.is('linux')) {
3597 this.send('\x1b[?6c');
3599 } else if (this.prefix
=== '>') {
3601 // seem to spit this
3602 // out around ~370 times (?).
3603 if (this.is('xterm')) {
3604 this.send('\x1b[>0;276;0c');
3605 } else if (this.is('rxvt-unicode')) {
3606 this.send('\x1b[>85;95;0c');
3607 } else if (this.is('linux')) {
3608 // not supported by linux console.
3609 // linux console echoes parameters.
3610 this.send(params
[0] + 'c');
3611 } else if (this.is('screen')) {
3612 this.send('\x1b[>83;40003;0c');
3618 // Line Position Absolute [row] (default = [1,column]) (VPA).
3619 Terminal
.prototype.linePosAbsolute = function(params
) {
3620 var param
= params
[0];
3621 if (param
< 1) param
= 1;
3623 if (this.y
>= this.rows
) {
3624 this.y
= this.rows
- 1;
3628 // 145 65 e * VPR - Vertical Position Relative
3630 Terminal
.prototype.VPositionRelative = function(params
) {
3631 var param
= params
[0];
3632 if (param
< 1) param
= 1;
3634 if (this.y
>= this.rows
) {
3635 this.y
= this.rows
- 1;
3640 // Horizontal and Vertical Position [row;column] (default =
3642 Terminal
.prototype.HVPosition = function(params
) {
3643 if (params
[0] < 1) params
[0] = 1;
3644 if (params
[1] < 1) params
[1] = 1;
3646 this.y
= params
[0] - 1;
3647 if (this.y
>= this.rows
) {
3648 this.y
= this.rows
- 1;
3651 this.x
= params
[1] - 1;
3652 if (this.x
>= this.cols
) {
3653 this.x
= this.cols
- 1;
3657 // CSI Pm h Set Mode (SM).
3658 // Ps = 2 -> Keyboard Action Mode (AM).
3659 // Ps = 4 -> Insert Mode (IRM).
3660 // Ps = 1 2 -> Send/receive (SRM).
3661 // Ps = 2 0 -> Automatic Newline (LNM).
3663 // DEC Private Mode Set (DECSET).
3664 // Ps = 1 -> Application Cursor Keys (DECCKM).
3665 // Ps = 2 -> Designate USASCII for character sets G0-G3
3666 // (DECANM), and set VT100 mode.
3667 // Ps = 3 -> 132 Column Mode (DECCOLM).
3668 // Ps = 4 -> Smooth (Slow) Scroll (DECSCLM).
3669 // Ps = 5 -> Reverse Video (DECSCNM).
3670 // Ps = 6 -> Origin Mode (DECOM).
3671 // Ps = 7 -> Wraparound Mode (DECAWM).
3672 // Ps = 8 -> Auto-repeat Keys (DECARM).
3673 // Ps = 9 -> Send Mouse X & Y on button press. See the sec-
3674 // tion Mouse Tracking.
3675 // Ps = 1 0 -> Show toolbar (rxvt).
3676 // Ps = 1 2 -> Start Blinking Cursor (att610).
3677 // Ps = 1 8 -> Print form feed (DECPFF).
3678 // Ps = 1 9 -> Set print extent to full screen (DECPEX).
3679 // Ps = 2 5 -> Show Cursor (DECTCEM).
3680 // Ps = 3 0 -> Show scrollbar (rxvt).
3681 // Ps = 3 5 -> Enable font-shifting functions (rxvt).
3682 // Ps = 3 8 -> Enter Tektronix Mode (DECTEK).
3683 // Ps = 4 0 -> Allow 80 -> 132 Mode.
3684 // Ps = 4 1 -> more(1) fix (see curses resource).
3685 // Ps = 4 2 -> Enable Nation Replacement Character sets (DECN-
3687 // Ps = 4 4 -> Turn On Margin Bell.
3688 // Ps = 4 5 -> Reverse-wraparound Mode.
3689 // Ps = 4 6 -> Start Logging. This is normally disabled by a
3690 // compile-time option.
3691 // Ps = 4 7 -> Use Alternate Screen Buffer. (This may be dis-
3692 // abled by the titeInhibit resource).
3693 // Ps = 6 6 -> Application keypad (DECNKM).
3694 // Ps = 6 7 -> Backarrow key sends backspace (DECBKM).
3695 // Ps = 1 0 0 0 -> Send Mouse X & Y on button press and
3696 // release. See the section Mouse Tracking.
3697 // Ps = 1 0 0 1 -> Use Hilite Mouse Tracking.
3698 // Ps = 1 0 0 2 -> Use Cell Motion Mouse Tracking.
3699 // Ps = 1 0 0 3 -> Use All Motion Mouse Tracking.
3700 // Ps = 1 0 0 4 -> Send FocusIn/FocusOut events.
3701 // Ps = 1 0 0 5 -> Enable Extended Mouse Mode.
3702 // Ps = 1 0 1 0 -> Scroll to bottom on tty output (rxvt).
3703 // Ps = 1 0 1 1 -> Scroll to bottom on key press (rxvt).
3704 // Ps = 1 0 3 4 -> Interpret "meta" key, sets eighth bit.
3705 // (enables the eightBitInput resource).
3706 // Ps = 1 0 3 5 -> Enable special modifiers for Alt and Num-
3707 // Lock keys. (This enables the numLock resource).
3708 // Ps = 1 0 3 6 -> Send ESC when Meta modifies a key. (This
3709 // enables the metaSendsEscape resource).
3710 // Ps = 1 0 3 7 -> Send DEL from the editing-keypad Delete
3712 // Ps = 1 0 3 9 -> Send ESC when Alt modifies a key. (This
3713 // enables the altSendsEscape resource).
3714 // Ps = 1 0 4 0 -> Keep selection even if not highlighted.
3715 // (This enables the keepSelection resource).
3716 // Ps = 1 0 4 1 -> Use the CLIPBOARD selection. (This enables
3717 // the selectToClipboard resource).
3718 // Ps = 1 0 4 2 -> Enable Urgency window manager hint when
3719 // Control-G is received. (This enables the bellIsUrgent
3721 // Ps = 1 0 4 3 -> Enable raising of the window when Control-G
3722 // is received. (enables the popOnBell resource).
3723 // Ps = 1 0 4 7 -> Use Alternate Screen Buffer. (This may be
3724 // disabled by the titeInhibit resource).
3725 // Ps = 1 0 4 8 -> Save cursor as in DECSC. (This may be dis-
3726 // abled by the titeInhibit resource).
3727 // Ps = 1 0 4 9 -> Save cursor as in DECSC and use Alternate
3728 // Screen Buffer, clearing it first. (This may be disabled by
3729 // the titeInhibit resource). This combines the effects of the 1
3730 // 0 4 7 and 1 0 4 8 modes. Use this with terminfo-based
3731 // applications rather than the 4 7 mode.
3732 // Ps = 1 0 5 0 -> Set terminfo/termcap function-key mode.
3733 // Ps = 1 0 5 1 -> Set Sun function-key mode.
3734 // Ps = 1 0 5 2 -> Set HP function-key mode.
3735 // Ps = 1 0 5 3 -> Set SCO function-key mode.
3736 // Ps = 1 0 6 0 -> Set legacy keyboard emulation (X11R6).
3737 // Ps = 1 0 6 1 -> Set VT220 keyboard emulation.
3738 // Ps = 2 0 0 4 -> Set bracketed paste mode.
3740 // http://vt100.net/docs/vt220-rm/chapter4.html
3741 Terminal
.prototype.setMode = function(params
) {
3742 if (typeof params
=== 'object') {
3743 var l
= params
.length
3746 for (; i
< l
; i
++) {
3747 this.setMode(params
[i
]);
3756 this.insertMode
= true;
3759 //this.convertEol = true;
3762 } else if (this.prefix
=== '?') {
3765 this.applicationCursor
= true;
3768 this.setgCharset(0, Terminal
.charsets
.US
);
3769 this.setgCharset(1, Terminal
.charsets
.US
);
3770 this.setgCharset(2, Terminal
.charsets
.US
);
3771 this.setgCharset(3, Terminal
.charsets
.US
);
3772 // set VT100 mode here
3774 case 3: // 132 col mode
3775 this.savedCols
= this.cols
;
3776 this.resize(132, this.rows
);
3779 this.originMode
= true;
3782 this.wraparoundMode
= true;
3785 // this.cursorBlink = true;
3788 this.log('Serial port requested application keypad.');
3789 this.applicationKeypad
= true;
3791 case 9: // X10 Mouse
3792 // no release, no motion, no wheel, no modifiers.
3793 case 1000: // vt200 mouse
3795 // no modifiers, except control on the wheel.
3796 case 1002: // button event mouse
3797 case 1003: // any event mouse
3798 // any event - sends motion events,
3799 // even if there is no button held down.
3800 this.x10Mouse
= params
=== 9;
3801 this.vt200Mouse
= params
=== 1000;
3802 this.normalMouse
= params
> 1000;
3803 this.mouseEvents
= true;
3804 this.element
.style
.cursor
= 'default';
3805 this.log('Binding to mouse events.');
3807 case 1004: // send focusin/focusout events
3810 this.sendFocus
= true;
3812 case 1005: // utf8 ext mode mouse
3813 this.utfMouse
= true;
3814 // for wide terminals
3815 // simply encodes large values as utf8 characters
3817 case 1006: // sgr ext mode mouse
3818 this.sgrMouse
= true;
3819 // for wide terminals
3820 // does not add 32 to fields
3821 // press: ^[[<b;x;yM
3822 // release: ^[[<b;x;ym
3824 case 1015: // urxvt ext mode mouse
3825 this.urxvtMouse
= true;
3826 // for wide terminals
3827 // numbers for fields
3829 // motion: ^[[b;x;yT
3831 case 25: // show cursor
3832 this.cursorHidden
= false;
3834 case 1049: // alt screen buffer cursor
3835 //this.saveCursor();
3837 case 47: // alt screen buffer
3838 case 1047: // alt screen buffer
3846 scrollTop
: this.scrollTop
,
3847 scrollBottom
: this.scrollBottom
,
3849 // XXX save charset(s) here?
3850 // charset: this.charset,
3851 // glevel: this.glevel,
3852 // charsets: this.charsets
3855 this.normal
= normal
;
3863 // CSI Pm l Reset Mode (RM).
3864 // Ps = 2 -> Keyboard Action Mode (AM).
3865 // Ps = 4 -> Replace Mode (IRM).
3866 // Ps = 1 2 -> Send/receive (SRM).
3867 // Ps = 2 0 -> Normal Linefeed (LNM).
3869 // DEC Private Mode Reset (DECRST).
3870 // Ps = 1 -> Normal Cursor Keys (DECCKM).
3871 // Ps = 2 -> Designate VT52 mode (DECANM).
3872 // Ps = 3 -> 80 Column Mode (DECCOLM).
3873 // Ps = 4 -> Jump (Fast) Scroll (DECSCLM).
3874 // Ps = 5 -> Normal Video (DECSCNM).
3875 // Ps = 6 -> Normal Cursor Mode (DECOM).
3876 // Ps = 7 -> No Wraparound Mode (DECAWM).
3877 // Ps = 8 -> No Auto-repeat Keys (DECARM).
3878 // Ps = 9 -> Don't send Mouse X & Y on button press.
3879 // Ps = 1 0 -> Hide toolbar (rxvt).
3880 // Ps = 1 2 -> Stop Blinking Cursor (att610).
3881 // Ps = 1 8 -> Don't print form feed (DECPFF).
3882 // Ps = 1 9 -> Limit print to scrolling region (DECPEX).
3883 // Ps = 2 5 -> Hide Cursor (DECTCEM).
3884 // Ps = 3 0 -> Don't show scrollbar (rxvt).
3885 // Ps = 3 5 -> Disable font-shifting functions (rxvt).
3886 // Ps = 4 0 -> Disallow 80 -> 132 Mode.
3887 // Ps = 4 1 -> No more(1) fix (see curses resource).
3888 // Ps = 4 2 -> Disable Nation Replacement Character sets (DEC-
3890 // Ps = 4 4 -> Turn Off Margin Bell.
3891 // Ps = 4 5 -> No Reverse-wraparound Mode.
3892 // Ps = 4 6 -> Stop Logging. (This is normally disabled by a
3893 // compile-time option).
3894 // Ps = 4 7 -> Use Normal Screen Buffer.
3895 // Ps = 6 6 -> Numeric keypad (DECNKM).
3896 // Ps = 6 7 -> Backarrow key sends delete (DECBKM).
3897 // Ps = 1 0 0 0 -> Don't send Mouse X & Y on button press and
3898 // release. See the section Mouse Tracking.
3899 // Ps = 1 0 0 1 -> Don't use Hilite Mouse Tracking.
3900 // Ps = 1 0 0 2 -> Don't use Cell Motion Mouse Tracking.
3901 // Ps = 1 0 0 3 -> Don't use All Motion Mouse Tracking.
3902 // Ps = 1 0 0 4 -> Don't send FocusIn/FocusOut events.
3903 // Ps = 1 0 0 5 -> Disable Extended Mouse Mode.
3904 // Ps = 1 0 1 0 -> Don't scroll to bottom on tty output
3906 // Ps = 1 0 1 1 -> Don't scroll to bottom on key press (rxvt).
3907 // Ps = 1 0 3 4 -> Don't interpret "meta" key. (This disables
3908 // the eightBitInput resource).
3909 // Ps = 1 0 3 5 -> Disable special modifiers for Alt and Num-
3910 // Lock keys. (This disables the numLock resource).
3911 // Ps = 1 0 3 6 -> Don't send ESC when Meta modifies a key.
3912 // (This disables the metaSendsEscape resource).
3913 // Ps = 1 0 3 7 -> Send VT220 Remove from the editing-keypad
3915 // Ps = 1 0 3 9 -> Don't send ESC when Alt modifies a key.
3916 // (This disables the altSendsEscape resource).
3917 // Ps = 1 0 4 0 -> Do not keep selection when not highlighted.
3918 // (This disables the keepSelection resource).
3919 // Ps = 1 0 4 1 -> Use the PRIMARY selection. (This disables
3920 // the selectToClipboard resource).
3921 // Ps = 1 0 4 2 -> Disable Urgency window manager hint when
3922 // Control-G is received. (This disables the bellIsUrgent
3924 // Ps = 1 0 4 3 -> Disable raising of the window when Control-
3925 // G is received. (This disables the popOnBell resource).
3926 // Ps = 1 0 4 7 -> Use Normal Screen Buffer, clearing screen
3927 // first if in the Alternate Screen. (This may be disabled by
3928 // the titeInhibit resource).
3929 // Ps = 1 0 4 8 -> Restore cursor as in DECRC. (This may be
3930 // disabled by the titeInhibit resource).
3931 // Ps = 1 0 4 9 -> Use Normal Screen Buffer and restore cursor
3932 // as in DECRC. (This may be disabled by the titeInhibit
3933 // resource). This combines the effects of the 1 0 4 7 and 1 0
3934 // 4 8 modes. Use this with terminfo-based applications rather
3935 // than the 4 7 mode.
3936 // Ps = 1 0 5 0 -> Reset terminfo/termcap function-key mode.
3937 // Ps = 1 0 5 1 -> Reset Sun function-key mode.
3938 // Ps = 1 0 5 2 -> Reset HP function-key mode.
3939 // Ps = 1 0 5 3 -> Reset SCO function-key mode.
3940 // Ps = 1 0 6 0 -> Reset legacy keyboard emulation (X11R6).
3941 // Ps = 1 0 6 1 -> Reset keyboard emulation to Sun/PC style.
3942 // Ps = 2 0 0 4 -> Reset bracketed paste mode.
3943 Terminal
.prototype.resetMode = function(params
) {
3944 if (typeof params
=== 'object') {
3945 var l
= params
.length
3948 for (; i
< l
; i
++) {
3949 this.resetMode(params
[i
]);
3958 this.insertMode
= false;
3961 //this.convertEol = false;
3964 } else if (this.prefix
=== '?') {
3967 this.applicationCursor
= false;
3970 if (this.cols
=== 132 && this.savedCols
) {
3971 this.resize(this.savedCols
, this.rows
);
3973 delete this.savedCols
;
3976 this.originMode
= false;
3979 this.wraparoundMode
= false;
3982 // this.cursorBlink = false;
3985 this.log('Switching back to normal keypad.');
3986 this.applicationKeypad
= false;
3988 case 9: // X10 Mouse
3989 case 1000: // vt200 mouse
3990 case 1002: // button event mouse
3991 case 1003: // any event mouse
3992 this.x10Mouse
= false;
3993 this.vt200Mouse
= false;
3994 this.normalMouse
= false;
3995 this.mouseEvents
= false;
3996 this.element
.style
.cursor
= '';
3998 case 1004: // send focusin/focusout events
3999 this.sendFocus
= false;
4001 case 1005: // utf8 ext mode mouse
4002 this.utfMouse
= false;
4004 case 1006: // sgr ext mode mouse
4005 this.sgrMouse
= false;
4007 case 1015: // urxvt ext mode mouse
4008 this.urxvtMouse
= false;
4010 case 25: // hide cursor
4011 this.cursorHidden
= true;
4013 case 1049: // alt screen buffer cursor
4015 case 47: // normal screen buffer
4016 case 1047: // normal screen buffer - clearing it first
4018 this.lines
= this.normal
.lines
;
4019 this.ybase
= this.normal
.ybase
;
4020 this.ydisp
= this.normal
.ydisp
;
4021 this.x
= this.normal
.x
;
4022 this.y
= this.normal
.y
;
4023 this.scrollTop
= this.normal
.scrollTop
;
4024 this.scrollBottom
= this.normal
.scrollBottom
;
4025 this.tabs
= this.normal
.tabs
;
4027 // if (params === 1049) {
4028 // this.x = this.savedX;
4029 // this.y = this.savedY;
4031 this.refresh(0, this.rows
- 1);
4040 // Set Scrolling Region [top;bottom] (default = full size of win-
4043 Terminal
.prototype.setScrollRegion = function(params
) {
4044 if (this.prefix
) return;
4045 this.scrollTop
= (params
[0] || 1) - 1;
4046 this.scrollBottom
= (params
[1] || this.rows
) - 1;
4052 // Save cursor (ANSI.SYS).
4053 Terminal
.prototype.saveCursor = function(params
) {
4054 this.savedX
= this.x
;
4055 this.savedY
= this.y
;
4059 // Restore cursor (ANSI.SYS).
4060 Terminal
.prototype.restoreCursor = function(params
) {
4061 this.x
= this.savedX
|| 0;
4062 this.y
= this.savedY
|| 0;
4070 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
4071 Terminal
.prototype.cursorForwardTab = function(params
) {
4072 var param
= params
[0] || 1;
4074 this.x
= this.nextStop();
4078 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
4079 Terminal
.prototype.scrollUp = function(params
) {
4080 var param
= params
[0] || 1;
4082 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
4083 this.lines
.splice(this.ybase
+ this.scrollBottom
, 0, this.blankLine());
4086 this.updateRange(this.scrollTop
);
4087 this.updateRange(this.scrollBottom
);
4090 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
4091 Terminal
.prototype.scrollDown = function(params
) {
4092 var param
= params
[0] || 1;
4094 this.lines
.splice(this.ybase
+ this.scrollBottom
, 1);
4095 this.lines
.splice(this.ybase
+ this.scrollTop
, 0, this.blankLine());
4098 this.updateRange(this.scrollTop
);
4099 this.updateRange(this.scrollBottom
);
4102 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
4103 // Initiate highlight mouse tracking. Parameters are
4104 // [func;startx;starty;firstrow;lastrow]. See the section Mouse
4106 Terminal
.prototype.initMouseTracking = function(params
) {
4107 // Relevant: DECSET 1001
4111 // Reset one or more features of the title modes to the default
4112 // value. Normally, "reset" disables the feature. It is possi-
4113 // ble to disable the ability to reset features by compiling a
4114 // different default for the title modes into xterm.
4115 // Ps = 0 -> Do not set window/icon labels using hexadecimal.
4116 // Ps = 1 -> Do not query window/icon labels using hexadeci-
4118 // Ps = 2 -> Do not set window/icon labels using UTF-8.
4119 // Ps = 3 -> Do not query window/icon labels using UTF-8.
4120 // (See discussion of "Title Modes").
4121 Terminal
.prototype.resetTitleModes = function(params
) {
4125 // CSI Ps Z Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
4126 Terminal
.prototype.cursorBackwardTab = function(params
) {
4127 var param
= params
[0] || 1;
4129 this.x
= this.prevStop();
4133 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
4134 Terminal
.prototype.repeatPrecedingCharacter = function(params
) {
4135 var param
= params
[0] || 1
4136 , line
= this.lines
[this.ybase
+ this.y
]
4137 , ch
= line
[this.x
- 1] || [this.defAttr
, ' '];
4139 while (param
--) line
[this.x
++] = ch
;
4142 // CSI Ps g Tab Clear (TBC).
4143 // Ps = 0 -> Clear Current Column (default).
4144 // Ps = 3 -> Clear All.
4146 // Ps = 2 -> Clear Stops on Line.
4147 // http://vt100.net/annarbor/aaa-ug/section6.html
4148 Terminal
.prototype.tabClear = function(params
) {
4149 var param
= params
[0];
4151 delete this.tabs
[this.x
];
4152 } else if (param
=== 3) {
4157 // CSI Pm i Media Copy (MC).
4158 // Ps = 0 -> Print screen (default).
4159 // Ps = 4 -> Turn off printer controller mode.
4160 // Ps = 5 -> Turn on printer controller mode.
4162 // Media Copy (MC, DEC-specific).
4163 // Ps = 1 -> Print line containing cursor.
4164 // Ps = 4 -> Turn off autoprint mode.
4165 // Ps = 5 -> Turn on autoprint mode.
4166 // Ps = 1 0 -> Print composed display, ignores DECPEX.
4167 // Ps = 1 1 -> Print all pages.
4168 Terminal
.prototype.mediaCopy = function(params
) {
4173 // Set or reset resource-values used by xterm to decide whether
4174 // to construct escape sequences holding information about the
4175 // modifiers pressed with a given key. The first parameter iden-
4176 // tifies the resource to set/reset. The second parameter is the
4177 // value to assign to the resource. If the second parameter is
4178 // omitted, the resource is reset to its initial value.
4179 // Ps = 1 -> modifyCursorKeys.
4180 // Ps = 2 -> modifyFunctionKeys.
4181 // Ps = 4 -> modifyOtherKeys.
4182 // If no parameters are given, all resources are reset to their
4184 Terminal
.prototype.setResources = function(params
) {
4189 // Disable modifiers which may be enabled via the CSI > Ps; Ps m
4190 // sequence. This corresponds to a resource value of "-1", which
4191 // cannot be set with the other sequence. The parameter identi-
4192 // fies the resource to be disabled:
4193 // Ps = 1 -> modifyCursorKeys.
4194 // Ps = 2 -> modifyFunctionKeys.
4195 // Ps = 4 -> modifyOtherKeys.
4196 // If the parameter is omitted, modifyFunctionKeys is disabled.
4197 // When modifyFunctionKeys is disabled, xterm uses the modifier
4198 // keys to make an extended sequence of functions rather than
4199 // adding a parameter to each function key to denote the modi-
4201 Terminal
.prototype.disableModifiers = function(params
) {
4206 // Set resource value pointerMode. This is used by xterm to
4207 // decide whether to hide the pointer cursor as the user types.
4208 // Valid values for the parameter:
4209 // Ps = 0 -> never hide the pointer.
4210 // Ps = 1 -> hide if the mouse tracking mode is not enabled.
4211 // Ps = 2 -> always hide the pointer. If no parameter is
4212 // given, xterm uses the default, which is 1 .
4213 Terminal
.prototype.setPointerMode = function(params
) {
4217 // CSI ! p Soft terminal reset (DECSTR).
4218 // http://vt100.net/docs/vt220-rm/table4-10.html
4219 Terminal
.prototype.softReset = function(params
) {
4220 this.cursorHidden
= false;
4221 this.insertMode
= false;
4222 this.originMode
= false;
4223 this.wraparoundMode
= false; // autowrap
4224 this.applicationKeypad
= false; // ?
4225 this.applicationCursor
= false;
4227 this.scrollBottom
= this.rows
- 1;
4228 this.curAttr
= this.defAttr
;
4229 this.x
= this.y
= 0; // ?
4230 this.charset
= null;
4231 this.glevel
= 0; // ??
4232 this.charsets
= [null]; // ??
4236 // Request ANSI mode (DECRQM). For VT300 and up, reply is
4238 // where Ps is the mode number as in RM, and Pm is the mode
4240 // 0 - not recognized
4243 // 3 - permanently set
4244 // 4 - permanently reset
4245 Terminal
.prototype.requestAnsiMode = function(params
) {
4250 // Request DEC private mode (DECRQM). For VT300 and up, reply is
4252 // where Ps is the mode number as in DECSET, Pm is the mode value
4253 // as in the ANSI DECRQM.
4254 Terminal
.prototype.requestPrivateMode = function(params
) {
4259 // Set conformance level (DECSCL). Valid values for the first
4261 // Ps = 6 1 -> VT100.
4262 // Ps = 6 2 -> VT200.
4263 // Ps = 6 3 -> VT300.
4264 // Valid values for the second parameter:
4265 // Ps = 0 -> 8-bit controls.
4266 // Ps = 1 -> 7-bit controls (always set for VT100).
4267 // Ps = 2 -> 8-bit controls.
4268 Terminal
.prototype.setConformanceLevel = function(params
) {
4272 // CSI Ps q Load LEDs (DECLL).
4273 // Ps = 0 -> Clear all LEDS (default).
4274 // Ps = 1 -> Light Num Lock.
4275 // Ps = 2 -> Light Caps Lock.
4276 // Ps = 3 -> Light Scroll Lock.
4277 // Ps = 2 1 -> Extinguish Num Lock.
4278 // Ps = 2 2 -> Extinguish Caps Lock.
4279 // Ps = 2 3 -> Extinguish Scroll Lock.
4280 Terminal
.prototype.loadLEDs = function(params
) {
4285 // Set cursor style (DECSCUSR, VT520).
4286 // Ps = 0 -> blinking block.
4287 // Ps = 1 -> blinking block (default).
4288 // Ps = 2 -> steady block.
4289 // Ps = 3 -> blinking underline.
4290 // Ps = 4 -> steady underline.
4291 Terminal
.prototype.setCursorStyle = function(params
) {
4296 // Select character protection attribute (DECSCA). Valid values
4297 // for the parameter:
4298 // Ps = 0 -> DECSED and DECSEL can erase (default).
4299 // Ps = 1 -> DECSED and DECSEL cannot erase.
4300 // Ps = 2 -> DECSED and DECSEL can erase.
4301 Terminal
.prototype.setCharProtectionAttr = function(params
) {
4306 // Restore DEC Private Mode Values. The value of Ps previously
4307 // saved is restored. Ps values are the same as for DECSET.
4308 Terminal
.prototype.restorePrivateValues = function(params
) {
4312 // CSI Pt; Pl; Pb; Pr; Ps$ r
4313 // Change Attributes in Rectangular Area (DECCARA), VT400 and up.
4314 // Pt; Pl; Pb; Pr denotes the rectangle.
4315 // Ps denotes the SGR attributes to change: 0, 1, 4, 5, 7.
4316 // NOTE: xterm doesn't enable this code by default.
4317 Terminal
.prototype.setAttrInRectangle = function(params
) {
4327 for (; t
< b
+ 1; t
++) {
4328 line
= this.lines
[this.ybase
+ t
];
4329 for (i
= l
; i
< r
; i
++) {
4330 line
[i
] = [attr
, line
[i
][1]];
4335 this.updateRange(params
[0]);
4336 this.updateRange(params
[2]);
4340 // CSI Pc; Pt; Pl; Pb; Pr$ x
4341 // Fill Rectangular Area (DECFRA), VT420 and up.
4342 // Pc is the character to use.
4343 // Pt; Pl; Pb; Pr denotes the rectangle.
4344 // NOTE: xterm doesn't enable this code by default.
4345 Terminal
.prototype.fillRectangle = function(params
) {
4355 for (; t
< b
+ 1; t
++) {
4356 line
= this.lines
[this.ybase
+ t
];
4357 for (i
= l
; i
< r
; i
++) {
4358 line
[i
] = [line
[i
][0], String
.fromCharCode(ch
)];
4363 this.updateRange(params
[1]);
4364 this.updateRange(params
[3]);
4368 // Enable Locator Reporting (DECELR).
4369 // Valid values for the first parameter:
4370 // Ps = 0 -> Locator disabled (default).
4371 // Ps = 1 -> Locator enabled.
4372 // Ps = 2 -> Locator enabled for one report, then disabled.
4373 // The second parameter specifies the coordinate unit for locator
4375 // Valid values for the second parameter:
4376 // Pu = 0 <- or omitted -> default to character cells.
4377 // Pu = 1 <- device physical pixels.
4378 // Pu = 2 <- character cells.
4379 Terminal
.prototype.enableLocatorReporting = function(params
) {
4380 var val
= params
[0] > 0;
4381 //this.mouseEvents = val;
4382 //this.decLocator = val;
4385 // CSI Pt; Pl; Pb; Pr$ z
4386 // Erase Rectangular Area (DECERA), VT400 and up.
4387 // Pt; Pl; Pb; Pr denotes the rectangle.
4388 // NOTE: xterm doesn't enable this code by default.
4389 Terminal
.prototype.eraseRectangle = function(params
) {
4399 ch
= [this.eraseAttr(), ' ']; // xterm?
4401 for (; t
< b
+ 1; t
++) {
4402 line
= this.lines
[this.ybase
+ t
];
4403 for (i
= l
; i
< r
; i
++) {
4409 this.updateRange(params
[0]);
4410 this.updateRange(params
[2]);
4415 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
4416 // NOTE: xterm doesn't enable this code by default.
4417 Terminal
.prototype.insertColumns = function() {
4418 var param
= params
[0]
4419 , l
= this.ybase
+ this.rows
4420 , ch
= [this.eraseAttr(), ' '] // xterm?
4424 for (i
= this.ybase
; i
< l
; i
++) {
4425 this.lines
[i
].splice(this.x
+ 1, 0, ch
);
4426 this.lines
[i
].pop();
4435 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
4436 // NOTE: xterm doesn't enable this code by default.
4437 Terminal
.prototype.deleteColumns = function() {
4438 var param
= params
[0]
4439 , l
= this.ybase
+ this.rows
4440 , ch
= [this.eraseAttr(), ' '] // xterm?
4444 for (i
= this.ybase
; i
< l
; i
++) {
4445 this.lines
[i
].splice(this.x
, 1);
4446 this.lines
[i
].push(ch
);
4457 Terminal
.charsets
= {};
4459 // DEC Special Character and Line Drawing Set.
4460 // http://vt100.net/docs/vt102-ug/table5-13.html
4461 // A lot of curses apps use this if they see TERM=xterm.
4462 // testing: echo -e '\e(0a\e(B'
4463 // The xterm output sometimes seems to conflict with the
4464 // reference above. xterm seems in line with the reference
4465 // when running vttest however.
4466 // The table below now uses xterm's output from vttest.
4467 Terminal
.charsets
.SCLD
= { // (0
4468 '`': '\u25c6', // '◆'
4469 'a': '\u2592', // '▒'
4470 'b': '\u0009', // '\t'
4471 'c': '\u000c', // '\f'
4472 'd': '\u000d', // '\r'
4473 'e': '\u000a', // '\n'
4474 'f': '\u00b0', // '°'
4475 'g': '\u00b1', // '±'
4476 'h': '\u2424', // '\u2424' (NL)
4477 'i': '\u000b', // '\v'
4478 'j': '\u2518', // '┘'
4479 'k': '\u2510', // '┐'
4480 'l': '\u250c', // '┌'
4481 'm': '\u2514', // '└'
4482 'n': '\u253c', // '┼'
4483 'o': '\u23ba', // '⎺'
4484 'p': '\u23bb', // '⎻'
4485 'q': '\u2500', // '─'
4486 'r': '\u23bc', // '⎼'
4487 's': '\u23bd', // '⎽'
4488 't': '\u251c', // '├'
4489 'u': '\u2524', // '┤'
4490 'v': '\u2534', // '┴'
4491 'w': '\u252c', // '┬'
4492 'x': '\u2502', // '│'
4493 'y': '\u2264', // '≤'
4494 'z': '\u2265', // '≥'
4495 '{': '\u03c0', // 'π'
4496 '|': '\u2260', // '≠'
4497 '}': '\u00a3', // '£'
4498 '~': '\u00b7' // '·'
4501 Terminal
.charsets
.UK
= null; // (A
4502 Terminal
.charsets
.US
= null; // (B (USASCII)
4503 Terminal
.charsets
.Dutch
= null; // (4
4504 Terminal
.charsets
.Finnish
= null; // (C or (5
4505 Terminal
.charsets
.French
= null; // (R
4506 Terminal
.charsets
.FrenchCanadian
= null; // (Q
4507 Terminal
.charsets
.German
= null; // (K
4508 Terminal
.charsets
.Italian
= null; // (Y
4509 Terminal
.charsets
.NorwegianDanish
= null; // (E or (6
4510 Terminal
.charsets
.Spanish
= null; // (Z
4511 Terminal
.charsets
.Swedish
= null; // (H or (7
4512 Terminal
.charsets
.Swiss
= null; // (=
4513 Terminal
.charsets
.ISOLatin
= null; // /A
4519 function contains(el
, arr
) {
4520 for (var i
= 0; i
< arr
.length
; i
+= 1) {
4521 if (el
=== arr
[i
]) {
4528 function on(el
, type
, handler
, capture
) {
4529 if (!Array
.isArray(el
)) {
4532 el
.forEach(function (element
) {
4533 element
.addEventListener(type
, handler
, capture
|| false);
4537 function off(el
, type
, handler
, capture
) {
4538 el
.removeEventListener(type
, handler
, capture
|| false);
4541 function cancel(ev
, force
) {
4542 if (!this.cancelEvents
&& !force
) {
4545 ev
.preventDefault();
4546 ev
.stopPropagation();
4550 function inherits(child
, parent
) {
4552 this.constructor = child
;
4554 f
.prototype = parent
.prototype;
4555 child
.prototype = new f
;
4558 // if bold is broken, we can't
4559 // use it in the terminal.
4560 function isBoldBroken(document
) {
4561 var body
= document
.getElementsByTagName('body')[0];
4562 var el
= document
.createElement('span');
4563 el
.innerHTML
= 'hello world';
4564 body
.appendChild(el
);
4565 var w1
= el
.scrollWidth
;
4566 el
.style
.fontWeight
= 'bold';
4567 var w2
= el
.scrollWidth
;
4568 body
.removeChild(el
);
4572 var String
= this.String
;
4573 var setTimeout
= this.setTimeout
;
4574 var setInterval
= this.setInterval
;
4576 function indexOf(obj
, el
) {
4579 if (obj
[i
] === el
) return i
;
4584 function isThirdLevelShift(term
, ev
) {
4586 (term
.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) ||
4587 (term
.isMSWindows
&& ev
.altKey
&& ev
.ctrlKey
&& !ev
.metaKey
);
4589 // Don't invoke for arrows, pageDown, home, backspace, etc.
4590 return thirdLevelKey
&& (!ev
.keyCode
|| ev
.keyCode
> 47);
4593 function isWide(ch
) {
4594 if (ch
<= '\uff00') return false;
4595 return (ch
>= '\uff01' && ch
<= '\uffbe')
4596 || (ch
>= '\uffc2' && ch
<= '\uffc7')
4597 || (ch
>= '\uffca' && ch
<= '\uffcf')
4598 || (ch
>= '\uffd2' && ch
<= '\uffd7')
4599 || (ch
>= '\uffda' && ch
<= '\uffdc')
4600 || (ch
>= '\uffe0' && ch
<= '\uffe6')
4601 || (ch
>= '\uffe8' && ch
<= '\uffee');
4604 function matchColor(r1
, g1
, b1
) {
4605 var hash
= (r1
<< 16) | (g1
<< 8) | b1
;
4607 if (matchColor
._cache
[hash
] != null) {
4608 return matchColor
._cache
[hash
];
4611 var ldiff
= Infinity
4620 for (; i
< Terminal
.vcolors
.length
; i
++) {
4621 c
= Terminal
.vcolors
[i
];
4626 diff
= matchColor
.distance(r1
, g1
, b1
, r2
, g2
, b2
);
4639 return matchColor
._cache
[hash
] = li
;
4642 matchColor
._cache
= {};
4644 // http://stackoverflow.com/questions/1633828
4645 matchColor
.distance = function(r1
, g1
, b1
, r2
, g2
, b2
) {
4646 return Math
.pow(30 * (r1
- r2
), 2)
4647 + Math
.pow(59 * (g1
- g2
), 2)
4648 + Math
.pow(11 * (b1
- b2
), 2);
4651 function each(obj
, iter
, con
) {
4652 if (obj
.forEach
) return obj
.forEach(iter
, con
);
4653 for (var i
= 0; i
< obj
.length
; i
++) {
4654 iter
.call(con
, obj
[i
], i
, obj
);
4658 function keys(obj
) {
4659 if (Object
.keys
) return Object
.keys(obj
);
4662 if (Object
.prototype.hasOwnProperty
.call(obj
, key
)) {
4673 Terminal
.EventEmitter
= EventEmitter
;
4674 Terminal
.inherits
= inherits
;
4677 * Adds an event listener to the terminal.
4679 * @param {string} event The name of the event. TODO: Document all event types
4680 * @param {function} callback The function to call when the event is triggered.
4686 Terminal
.cancel
= cancel
;