]>
git.proxmox.com Git - mirror_xterm.js.git/blob - src/xterm.js
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 this.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;
511 * Blur and re-focus instantly. This is due to a weird focus state on Chrome, when setting
512 * contentEditable to true on a focused element.
517 setTimeout(function () {
518 term
.element
.contentEditable
= false;
519 if (typeof callback
== 'function') {
526 * Bind to paste event and allow both keyboard and right-click pasting, without having the
527 * contentEditable value set to true.
529 Terminal
.bindPaste = function(term
) {
530 on(term
.element
, 'paste', function(ev
) {
531 if (ev
.clipboardData
) {
532 var text
= ev
.clipboardData
.getData('text/plain');
533 term
.emit('paste', text
, ev
);
536 * Cancel the paste event, or else things will be pasted twice:
537 * 1. by the terminal handler
538 * 2. by the browser, because of the contentEditable value being true
540 term
.cancel(ev
, true);
543 * After the paste event is completed, always set the contentEditable value to false.
545 term
.element
.contentEditable
= false;
550 * Hack pasting with keyboard, in order to make it work without contentEditable.
551 * When a user types Ctrl + Shift + V or Cmd + V on a Mac, lease the contentEditable value
554 on(term
.element
, 'keydown', function (ev
) {
555 var isEditable
= term
.element
.contentEditable
=== "true";
558 * If on a Mac, lease the contentEditable value temporarily, when the user presses
559 * the Cmd button, in a keydown event order to paste frictionlessly.
561 if (term
.isMac
&& ev
.metaKey
&& !isEditable
) {
562 term
.leaseContentEditable(5000);
565 if (!term
.isMac
&& ev
.keyCode
== 45 && ev
.shiftKey
&& !ev
.ctrlKey
&& !isEditable
) {
566 // Shift + Insert pastes on windows and many linuxes
567 term
.leaseContentEditable();
572 * Hack pasting with right-click in order to allow right-click paste, by leasing the
573 * contentEditable value as true.
575 on(term
.element
, 'contextmenu', function (ev
) {
576 term
.leaseContentEditable();
582 * Apply key handling to the terminal
584 Terminal
.bindKeys = function(term
) {
585 on(term
.element
, 'keydown', function(ev
) {
589 on(term
.element
, 'keypress', function(ev
) {
595 * Prepares text copied from terminal selection, to be saved in the clipboard by:
596 * 1. stripping all trailing white spaces
597 * 2. converting all non-breaking spaces to regular spaces
598 * @param {string} text The copied text that needs processing for storing in clipboard
602 Terminal
.prepareCopiedTextForClipboard = function (text
) {
603 var space
= String
.fromCharCode(32),
604 nonBreakingSpace
= String
.fromCharCode(160),
605 allNonBreakingSpaces
= new RegExp(nonBreakingSpace
, 'g'),
606 processedText
= text
.split('\n').map(function (line
) {
608 * Strip all trailing white spaces and convert all non-breaking spaces to regular
611 var processedLine
= line
.replace(/\s+$/g, '').replace(allNonBreakingSpaces
, space
);
613 return processedLine
;
616 return processedText
;
620 * Binds copy functionality to the given terminal.
623 Terminal
.bindCopy = function(term
) {
624 on(term
.element
, 'copy', function(ev
) {
625 var copiedText
= window
.getSelection().toString(),
626 text
= Terminal
.prepareCopiedTextForClipboard(copiedText
);
628 ev
.clipboardData
.setData('text/plain', text
);
634 * Cancel the cut event completely
636 Terminal
.bindCut = function(term
) {
637 on(term
.element
, 'cut', function (ev
) {
643 Terminal
.bindDrop = function (term
) {
645 * Do not perform the "drop" event. Altering the contents of the
646 * terminal with drag n drop is unwanted behavior.
648 on(term
.element
, 'drop', function (ev
) {
649 term
.cancel(ev
, true);
654 Terminal
.click = function (term
) {
656 * Do not perform the "drop" event. Altering the contents of the
657 * terminal with drag n drop is unwanted behavior.
659 on(term
.element
, 'click', function (ev
) {
660 term
.cancel(ev
, true);
666 * Insert the given row to the terminal or produce a new one
667 * if no row argument is passed. Return the inserted row.
669 Terminal
.prototype.insertRow = function (row
) {
670 if (typeof row
!= 'object') {
671 row
= document
.createElement('div');
674 this.rowContainer
.appendChild(row
);
675 this.children
.push(row
);
682 * Opens the terminal within an element.
684 * @param {HTMLElement} parent The element to create the terminal within.
688 Terminal
.prototype.open = function(parent
) {
689 var self
=this, i
=0, div
;
691 this.parent
= parent
|| this.parent
;
694 throw new Error('Terminal requires a parent element.');
698 * Grab global elements
700 this.context
= this.parent
.ownerDocument
.defaultView
;
701 this.document
= this.parent
.ownerDocument
;
702 this.body
= this.document
.getElementsByTagName('body')[0];
707 if (this.context
.navigator
&& this.context
.navigator
.userAgent
) {
708 this.isMSIE
= !!~this.context
.navigator
.userAgent
.indexOf('MSIE');
712 * Find the users platform. We use this to interpret the meta key
713 * and ISO third level shifts.
714 * http://stackoverflow.com/questions/19877924/what-is-the-list-of-possible-values-for-navigator-platform-as-of-today
716 if (this.context
.navigator
&& this.context
.navigator
.platform
) {
717 this.isMac
= contains(
718 this.context
.navigator
.platform
,
719 ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K']
721 this.isIpad
= this.context
.navigator
.platform
=== 'iPad';
722 this.isIphone
= this.context
.navigator
.platform
=== 'iPhone';
723 this.isMSWindows
= contains(
724 this.context
.navigator
.platform
,
725 ['Windows', 'Win16', 'Win32', 'WinCE']
730 * Create main element container
732 this.element
= this.document
.createElement('div');
733 this.element
.classList
.add('terminal');
734 this.element
.classList
.add('xterm');
735 this.element
.classList
.add('xterm-theme-' + this.theme
);
736 this.element
.setAttribute('tabindex', 0);
737 this.element
.spellcheck
= false;
740 * Create the container that will hold the lines of the terminal and then
741 * produce the lines the lines.
743 this.rowContainer
= document
.createElement('div');
744 this.rowContainer
.classList
.add('xterm-rows');
745 this.element
.appendChild(this.rowContainer
);
748 for (; i
< this.rows
; i
++) {
751 this.parent
.appendChild(this.element
);
754 this.refresh(0, this.rows
- 1);
756 // Initialize global actions that
757 // need to be taken on the document.
760 // Ensure there is a Terminal.focus.
763 on(this.element
, 'mouseup', function() {
764 var selection
= document
.getSelection(),
765 collapsed
= selection
.isCollapsed
,
766 isRange
= typeof collapsed
== 'boolean' ? !collapsed
: selection
.type
== 'Range';
772 // Listen for mouse events and translate
773 // them into terminal mouse protocols.
776 // Figure out whether boldness affects
777 // the character width of monospace fonts.
778 if (Terminal
.brokenBold
== null) {
779 Terminal
.brokenBold
= isBoldBroken(this.document
);
787 * Attempts to load an add-on using CommonJS or RequireJS (whichever is available).
788 * @param {string} addon The name of the addon to load
791 Terminal
.loadAddon = function(addon
, callback
) {
792 if (typeof exports
=== 'object' && typeof module
=== 'object') {
794 return require(__dirname
+ '/../addons/' + addon
);
795 } else if (typeof define
== 'function') {
797 return require(['../addons/' + addon
+ '/' + addon
], callback
);
799 console
.error('Cannot load a module without a CommonJS or RequireJS environment.');
805 // XTerm mouse events
806 // http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#Mouse%20Tracking
807 // To better understand these
808 // the xterm code is very helpful:
810 // button.c, charproc.c, misc.c
811 // Relevant functions in xterm/button.c:
812 // BtnCode, EmitButtonCode, EditorButton, SendMousePosition
813 Terminal
.prototype.bindMouse = function() {
814 var el
= this.element
818 var wheelEvent
= 'onmousewheel' in this.context
822 // mouseup, mousedown, mousewheel
823 // left click: ^[[M 3<^[[M#3<
824 // mousewheel up: ^[[M`3>
825 function sendButton(ev
) {
829 // get the xterm-style button
830 button
= getButton(ev
);
832 // get mouse coordinates
836 sendEvent(button
, pos
);
843 // keep it at the left
844 // button, just in case.
855 // motion example of a left click:
856 // ^[[M 3<^[[M@4<^[[M@5<^[[M@6<^[[M@7<^[[M#7<
857 function sendMove(ev
) {
864 // buttons marked as motions
865 // are incremented by 32
868 sendEvent(button
, pos
);
872 // position to characters
873 function encode(data
, ch
) {
874 if (!self
.utfMouse
) {
875 if (ch
=== 255) return data
.push(0);
876 if (ch
> 127) ch
= 127;
879 if (ch
=== 2047) return data
.push(0);
883 if (ch
> 2047) ch
= 2047;
884 data
.push(0xC0 | (ch
>> 6));
885 data
.push(0x80 | (ch
& 0x3F));
890 // send a mouse event:
891 // regular/utf8: ^[[M Cb Cx Cy
892 // urxvt: ^[[ Cb ; Cx ; Cy M
893 // sgr: ^[[ Cb ; Cx ; Cy M/m
894 // vt300: ^[[ 24(1/3/5)~ [ Cx , Cy ] \r
895 // locator: CSI P e ; P b ; P r ; P c ; P p & w
896 function sendEvent(button
, pos
) {
897 // self.emit('mouse', {
903 if (self
.vt300Mouse
) {
905 // http://www.vt100.net/docs/vt3xx-gp/chapter15.html
909 var data
= '\x1b[24';
910 if (button
=== 0) data
+= '1';
911 else if (button
=== 1) data
+= '3';
912 else if (button
=== 2) data
+= '5';
913 else if (button
=== 3) return;
915 data
+= '~[' + pos
.x
+ ',' + pos
.y
+ ']\r';
920 if (self
.decLocator
) {
925 if (button
=== 0) button
= 2;
926 else if (button
=== 1) button
= 4;
927 else if (button
=== 2) button
= 6;
928 else if (button
=== 3) button
= 3;
932 + (button
=== 3 ? 4 : 0)
943 if (self
.urxvtMouse
) {
948 self
.send('\x1b[' + button
+ ';' + pos
.x
+ ';' + pos
.y
+ 'M');
956 + ((button
& 3) === 3 ? button
& ~3 : button
)
961 + ((button
& 3) === 3 ? 'm' : 'M'));
967 encode(data
, button
);
971 self
.send('\x1b[M' + String
.fromCharCode
.apply(String
, data
));
974 function getButton(ev
) {
987 // 1, and 2 - with 64 added
990 button
= ev
.button
!= null
997 button
= button
=== 1 ? 0 : button
=== 4 ? 1 : button
;
1003 case 'DOMMouseScroll':
1004 button
= ev
.detail
< 0
1009 button
= ev
.wheelDeltaY
> 0
1015 // next three bits are the modifiers:
1016 // 4 = shift, 8 = meta, 16 = control
1017 shift
= ev
.shiftKey
? 4 : 0;
1018 meta
= ev
.metaKey
? 8 : 0;
1019 ctrl
= ev
.ctrlKey
? 16 : 0;
1020 mod
= shift
| meta
| ctrl
;
1023 if (self
.vt200Mouse
) {
1026 } else if (!self
.normalMouse
) {
1031 button
= (32 + (mod
<< 2)) + button
;
1036 // mouse coordinates measured in cols/rows
1037 function getCoords(ev
) {
1040 // ignore browsers without pageX for now
1041 if (ev
.pageX
== null) return;
1047 // should probably check offsetParent
1048 // but this is more portable
1049 while (el
&& el
!== self
.document
.documentElement
) {
1052 el
= 'offsetParent' in el
1057 // convert to cols/rows
1058 w
= self
.element
.clientWidth
;
1059 h
= self
.element
.clientHeight
;
1060 x
= Math
.round((x
/ w
) * self
.cols
);
1061 y
= Math
.round((y
/ h
) * self
.rows
);
1063 // be sure to avoid sending
1064 // bad positions to the program
1066 if (x
> self
.cols
) x
= self
.cols
;
1068 if (y
> self
.rows
) y
= self
.rows
;
1070 // xterm sends raw bytes and
1071 // starts at 32 (SP) for each.
1078 type
: ev
.type
=== wheelEvent
1084 on(el
, 'mousedown', function(ev
) {
1085 if (!self
.mouseEvents
) return;
1094 //if (self.vt200Mouse && !self.normalMouse) {
1095 if (self
.vt200Mouse
) {
1096 sendButton({ __proto__
: ev
, type
: 'mouseup' });
1097 return self
.cancel(ev
);
1101 if (self
.normalMouse
) on(self
.document
, 'mousemove', sendMove
);
1103 // x10 compatibility mode can't send button releases
1104 if (!self
.x10Mouse
) {
1105 on(self
.document
, 'mouseup', function up(ev
) {
1107 if (self
.normalMouse
) off(self
.document
, 'mousemove', sendMove
);
1108 off(self
.document
, 'mouseup', up
);
1109 return self
.cancel(ev
);
1113 return self
.cancel(ev
);
1116 //if (self.normalMouse) {
1117 // on(self.document, 'mousemove', sendMove);
1120 on(el
, wheelEvent
, function(ev
) {
1121 if (!self
.mouseEvents
) return;
1124 || self
.decLocator
) return;
1126 return self
.cancel(ev
);
1129 // allow mousewheel scrolling in
1130 // the shell for example
1131 on(el
, wheelEvent
, function(ev
) {
1132 if (self
.mouseEvents
) return;
1133 if (self
.applicationKeypad
) return;
1134 if (ev
.type
=== 'DOMMouseScroll') {
1135 self
.scrollDisp(ev
.detail
< 0 ? -1 : 1);
1137 self
.scrollDisp(ev
.wheelDeltaY
> 0 ? -1 : 1);
1139 return self
.cancel(ev
);
1144 * Destroys the terminal.
1148 Terminal
.prototype.destroy = function() {
1149 this.readable
= false;
1150 this.writable
= false;
1152 this.handler = function() {};
1153 this.write = function() {};
1154 if (this.element
.parentNode
) {
1155 this.element
.parentNode
.removeChild(this.element
);
1157 //this.emit('close');
1162 * Flags used to render terminal text properly
1175 * In the screen buffer, each character
1176 * is stored as a an array with a character
1177 * and a 32-bit integer.
1178 * First value: a utf-16 character.
1180 * Next 9 bits: background color (0-511).
1181 * Next 9 bits: foreground color (0-511).
1182 * Next 14 bits: a mask for misc. flags:
1183 * 1=bold, 2=underline, 4=blink, 8=inverse, 16=invisible
1187 * Refreshes terminal content within two rows (inclusive).
1189 * @param {number} start The row to start from (between 0 and terminal's height terminal - 1)
1190 * @param {number} end The row to end at (between fromRow and terminal's height terminal - 1)
1191 * @param {boolean} queue Whether the refresh should ran right now or be queued
1195 Terminal
.prototype.refresh = function(start
, end
, queue
) {
1198 // queue defaults to true
1199 queue
= (typeof queue
== 'undefined') ? true : queue
;
1202 * The refresh queue allows refresh to execute only approximately 30 times a second. For
1203 * commands that pass a significant amount of output to the write function, this prevents the
1204 * terminal from maxing out the CPU and making the UI unresponsive. While commands can still
1205 * run beyond what they do on the terminal, it is far better with a debounce in place as
1206 * every single terminal manipulation does not need to be constructed in the DOM.
1208 * A side-effect of this is that it makes ^C to interrupt a process seem more responsive.
1211 // If refresh should be queued, order the refresh and return.
1212 if (this._refreshIsQueued
) {
1213 // If a refresh has already been queued, just order a full refresh next
1214 this._fullRefreshNext
= true;
1216 setTimeout(function () {
1217 self
.refresh(start
, end
, false);
1219 this._refreshIsQueued
= true;
1224 // If refresh should be run right now (not be queued), release the lock
1225 this._refreshIsQueued
= false;
1227 // If multiple refreshes were requested, make a full refresh.
1228 if (this._fullRefreshNext
) {
1230 end
= this.rows
- 1;
1231 this._fullRefreshNext
= false // reset lock
1234 var x
, y
, i
, line
, out
, ch
, width
, data
, attr
, bg
, fg
, flags
, row
, parent
, focused
= document
.activeElement
;
1236 // If this is a big refresh, remove the terminal rows from the DOM for faster calculations
1237 if (end
- start
>= this.rows
/ 2) {
1238 parent
= this.element
.parentNode
;
1240 this.element
.removeChild(this.rowContainer
);
1247 if (end
>= this.rows
.length
) {
1248 this.log('`end` is too large. Most likely a bad CSR.');
1249 end
= this.rows
.length
- 1;
1252 for (; y
<= end
; y
++) {
1253 row
= y
+ this.ydisp
;
1255 line
= this.lines
[row
];
1258 if (this.y
=== y
- (this.ybase
- this.ydisp
)
1260 && !this.cursorHidden
) {
1266 attr
= this.defAttr
;
1269 for (; i
< width
; i
++) {
1273 if (i
=== x
) data
= -1;
1275 if (data
!== attr
) {
1276 if (attr
!== this.defAttr
) {
1279 if (data
!== this.defAttr
) {
1281 out
+= '<span class="reverse-video terminal-cursor';
1282 if (this.cursorBlink
) {
1287 var classNames
= [];
1290 fg
= (data
>> 9) & 0x1ff;
1293 if (flags
& Terminal
.flags
.BOLD
) {
1294 if (!Terminal
.brokenBold
) {
1295 classNames
.push('xterm-bold');
1297 // See: XTerm*boldColors
1298 if (fg
< 8) fg
+= 8;
1301 if (flags
& Terminal
.flags
.UNDERLINE
) {
1302 classNames
.push('xterm-underline');
1305 if (flags
& Terminal
.flags
.BLINK
) {
1306 classNames
.push('xterm-blink');
1310 * If inverse flag is on, then swap the foreground and background variables.
1312 if (flags
& Terminal
.flags
.INVERSE
) {
1313 /* One-line variable swap in JavaScript: http://stackoverflow.com/a/16201730 */
1314 bg = [fg, fg = bg][0];
1315 // Should inverse just be before the
1316 // above boldColors effect instead?
1317 if ((flags & 1) && fg < 8) fg += 8;
1320 if (flags & Terminal.flags.INVISIBLE) {
1321 classNames.push('xterm-hidden');
1325 * Weird situation: Invert flag used black foreground and white background results
1326 * in invalid background color, positioned at the 256 index of the 256 terminal
1327 * color map. Pin the colors manually in such a case.
1329 * Source: https://github.com/sourcelair/xterm.js/issues/57
1331 if (flags
& Terminal
.flags
.INVERSE
) {
1341 classNames
.push('xterm-bg-color-' + bg
);
1345 classNames
.push('xterm-color-' + fg
);
1349 if (classNames
.length
) {
1350 out
+= ' class="' + classNames
.join(' ') + '"';
1371 if (isWide(ch
)) i
++;
1380 if (attr
!== this.defAttr
) {
1384 this.children
[y
].innerHTML
= out
;
1388 this.element
.appendChild(this.rowContainer
);
1391 this.emit('refresh', {element
: this.element
, start
: start
, end
: end
});
1394 Terminal
.prototype.showCursor = function() {
1395 if (!this.cursorState
) {
1396 this.cursorState
= 1;
1397 this.refresh(this.y
, this.y
);
1401 Terminal
.prototype.scroll = function() {
1404 if (++this.ybase
=== this.scrollback
) {
1405 this.ybase
= this.ybase
/ 2 | 0;
1406 this.lines
= this.lines
.slice(-(this.ybase
+ this.rows
) + 1);
1409 this.ydisp
= this.ybase
;
1412 row
= this.ybase
+ this.rows
- 1;
1414 // subtract the bottom scroll region
1415 row
-= this.rows
- 1 - this.scrollBottom
;
1417 if (row
=== this.lines
.length
) {
1418 // potential optimization:
1419 // pushing is faster than splicing
1420 // when they amount to the same
1422 this.lines
.push(this.blankLine());
1425 this.lines
.splice(row
, 0, this.blankLine());
1428 if (this.scrollTop
!== 0) {
1429 if (this.ybase
!== 0) {
1431 this.ydisp
= this.ybase
;
1433 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
1437 this.updateRange(this.scrollTop
);
1438 this.updateRange(this.scrollBottom
);
1441 Terminal
.prototype.scrollDisp = function(disp
) {
1444 if (this.ydisp
> this.ybase
) {
1445 this.ydisp
= this.ybase
;
1446 } else if (this.ydisp
< 0) {
1450 this.refresh(0, this.rows
- 1);
1454 * Writes text to the terminal.
1456 * @param {string} text The text to write to the terminal.
1460 Terminal
.prototype.write = function(data
) {
1461 var l
= data
.length
, i
= 0, j
, cs
, ch
;
1463 this.refreshStart
= this.y
;
1464 this.refreshEnd
= this.y
;
1466 if (this.ybase
!== this.ydisp
) {
1467 this.ydisp
= this.ybase
;
1471 for (; i
< l
; i
++) {
1473 switch (this.state
) {
1484 if (this.convertEol
) {
1488 if (this.y
> this.scrollBottom
) {
1508 this.x
= this.nextStop();
1523 this.state
= escaped
;
1529 if (this.charset
&& this.charset
[ch
]) {
1530 ch
= this.charset
[ch
];
1533 if (this.x
>= this.cols
) {
1536 if (this.y
> this.scrollBottom
) {
1542 this.lines
[this.y
+ this.ybase
][this.x
] = [this.curAttr
, ch
];
1544 this.updateRange(this.y
);
1547 j
= this.y
+ this.ybase
;
1548 if (this.cols
< 2 || this.x
>= this.cols
) {
1549 this.lines
[j
][this.x
- 1] = [this.curAttr
, ' '];
1552 this.lines
[j
][this.x
] = [this.curAttr
, ' '];
1561 // ESC [ Control Sequence Introducer ( CSI is 0x9b).
1564 this.currentParam
= 0;
1568 // ESC ] Operating System Command ( OSC is 0x9d).
1571 this.currentParam
= 0;
1575 // ESC P Device Control String ( DCS is 0x90).
1578 this.currentParam
= 0;
1582 // ESC _ Application Program Command ( APC is 0x9f).
1584 this.state
= ignore
;
1587 // ESC ^ Privacy Message ( PM is 0x9e).
1589 this.state
= ignore
;
1592 // ESC c Full Reset (RIS).
1597 // ESC E Next Line ( NEL is 0x85).
1598 // ESC D Index ( IND is 0x84).
1606 // ESC M Reverse Index ( RI is 0x8d).
1608 this.reverseIndex();
1611 // ESC % Select default/utf-8 character set.
1612 // @ = default, G = utf-8
1614 //this.charset = null;
1616 this.setgCharset(0, Terminal
.charsets
.US
);
1617 this.state
= normal
;
1621 // ESC (,),*,+,-,. Designate G0-G2 Character Set.
1622 case '(': // <-- this seems to get all the attention
1648 this.state
= charset
;
1651 // Designate G3 Character Set (VT300).
1652 // A = ISO Latin-1 Supplemental.
1656 this.state
= charset
;
1661 // Single Shift Select of G2 Character Set
1662 // ( SS2 is 0x8e). This affects next character only.
1666 // Single Shift Select of G3 Character Set
1667 // ( SS3 is 0x8f). This affects next character only.
1671 // Invoke the G2 Character Set as GL (LS2).
1676 // Invoke the G3 Character Set as GL (LS3).
1681 // Invoke the G3 Character Set as GR (LS3R).
1686 // Invoke the G2 Character Set as GR (LS2R).
1691 // Invoke the G1 Character Set as GR (LS1R).
1696 // ESC 7 Save Cursor (DECSC).
1699 this.state
= normal
;
1702 // ESC 8 Restore Cursor (DECRC).
1704 this.restoreCursor();
1705 this.state
= normal
;
1708 // ESC # 3 DEC line height/width
1710 this.state
= normal
;
1714 // ESC H Tab Set (HTS is 0x88).
1719 // ESC = Application Keypad (DECPAM).
1721 this.log('Serial port requested application keypad.');
1722 this.applicationKeypad
= true;
1723 this.state
= normal
;
1726 // ESC > Normal Keypad (DECPNM).
1728 this.log('Switching back to normal keypad.');
1729 this.applicationKeypad
= false;
1730 this.state
= normal
;
1734 this.state
= normal
;
1735 this.error('Unknown ESC control: %s.', ch
);
1742 case '0': // DEC Special Character and Line Drawing Set.
1743 cs
= Terminal
.charsets
.SCLD
;
1746 cs
= Terminal
.charsets
.UK
;
1748 case 'B': // United States (USASCII).
1749 cs
= Terminal
.charsets
.US
;
1752 cs
= Terminal
.charsets
.Dutch
;
1754 case 'C': // Finnish
1756 cs
= Terminal
.charsets
.Finnish
;
1759 cs
= Terminal
.charsets
.French
;
1761 case 'Q': // FrenchCanadian
1762 cs
= Terminal
.charsets
.FrenchCanadian
;
1765 cs
= Terminal
.charsets
.German
;
1767 case 'Y': // Italian
1768 cs
= Terminal
.charsets
.Italian
;
1770 case 'E': // NorwegianDanish
1772 cs
= Terminal
.charsets
.NorwegianDanish
;
1774 case 'Z': // Spanish
1775 cs
= Terminal
.charsets
.Spanish
;
1777 case 'H': // Swedish
1779 cs
= Terminal
.charsets
.Swedish
;
1782 cs
= Terminal
.charsets
.Swiss
;
1784 case '/': // ISOLatin (actually /A)
1785 cs
= Terminal
.charsets
.ISOLatin
;
1789 cs
= Terminal
.charsets
.US
;
1792 this.setgCharset(this.gcharset
, cs
);
1793 this.gcharset
= null;
1794 this.state
= normal
;
1800 // Set Text Parameters.
1801 if (ch
=== '\x1b' || ch
=== '\x07') {
1802 if (ch
=== '\x1b') i
++;
1804 this.params
.push(this.currentParam
);
1806 switch (this.params
[0]) {
1810 if (this.params
[1]) {
1811 this.title
= this.params
[1];
1812 this.handleTitle(this.title
);
1820 // change dynamic colors
1832 // change dynamic ui colors
1844 // manipulate selection data
1862 this.currentParam
= 0;
1863 this.state
= normal
;
1865 if (!this.params
.length
) {
1866 if (ch
>= '0' && ch
<= '9') {
1868 this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1869 } else if (ch
=== ';') {
1870 this.params
.push(this.currentParam
);
1871 this.currentParam
= '';
1874 this.currentParam
+= ch
;
1881 if (ch
=== '?' || ch
=== '>' || ch
=== '!') {
1887 if (ch
>= '0' && ch
<= '9') {
1888 this.currentParam
= this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1892 // '$', '"', ' ', '\''
1893 if (ch
=== '$' || ch
=== '"' || ch
=== ' ' || ch
=== '\'') {
1898 this.params
.push(this.currentParam
);
1899 this.currentParam
= 0;
1902 if (ch
=== ';') break;
1904 this.state
= normal
;
1908 // Cursor Up Ps Times (default = 1) (CUU).
1910 this.cursorUp(this.params
);
1914 // Cursor Down Ps Times (default = 1) (CUD).
1916 this.cursorDown(this.params
);
1920 // Cursor Forward Ps Times (default = 1) (CUF).
1922 this.cursorForward(this.params
);
1926 // Cursor Backward Ps Times (default = 1) (CUB).
1928 this.cursorBackward(this.params
);
1932 // Cursor Position [row;column] (default = [1,1]) (CUP).
1934 this.cursorPos(this.params
);
1937 // CSI Ps J Erase in Display (ED).
1939 this.eraseInDisplay(this.params
);
1942 // CSI Ps K Erase in Line (EL).
1944 this.eraseInLine(this.params
);
1947 // CSI Pm m Character Attributes (SGR).
1950 this.charAttributes(this.params
);
1954 // CSI Ps n Device Status Report (DSR).
1957 this.deviceStatus(this.params
);
1966 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
1968 this.insertChars(this.params
);
1972 // Cursor Next Line Ps Times (default = 1) (CNL).
1974 this.cursorNextLine(this.params
);
1978 // Cursor Preceding Line Ps Times (default = 1) (CNL).
1980 this.cursorPrecedingLine(this.params
);
1984 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
1986 this.cursorCharAbsolute(this.params
);
1990 // Insert Ps Line(s) (default = 1) (IL).
1992 this.insertLines(this.params
);
1996 // Delete Ps Line(s) (default = 1) (DL).
1998 this.deleteLines(this.params
);
2002 // Delete Ps Character(s) (default = 1) (DCH).
2004 this.deleteChars(this.params
);
2008 // Erase Ps Character(s) (default = 1) (ECH).
2010 this.eraseChars(this.params
);
2013 // CSI Pm ` Character Position Absolute
2014 // [column] (default = [row,1]) (HPA).
2016 this.charPosAbsolute(this.params
);
2020 // Horizontal Position Relative
2022 this.HPositionRelative(this.params
);
2026 // Send Device Attributes (Primary DA).
2028 // Send Device Attributes (Secondary DA)
2030 this.sendDeviceAttributes(this.params
);
2034 // Line Position Absolute [row] (default = [1,column]) (VPA).
2036 this.linePosAbsolute(this.params
);
2039 // 145 65 e * VPR - Vertical Position Relative
2041 this.VPositionRelative(this.params
);
2045 // Horizontal and Vertical Position [row;column] (default =
2048 this.HVPosition(this.params
);
2051 // CSI Pm h Set Mode (SM).
2052 // CSI ? Pm h - mouse escape codes, cursor escape codes
2054 this.setMode(this.params
);
2057 // CSI Pm l Reset Mode (RM).
2060 this.resetMode(this.params
);
2064 // Set Scrolling Region [top;bottom] (default = full size of win-
2068 this.setScrollRegion(this.params
);
2072 // Save cursor (ANSI.SYS).
2074 this.saveCursor(this.params
);
2078 // Restore cursor (ANSI.SYS).
2080 this.restoreCursor(this.params
);
2088 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
2090 this.cursorForwardTab(this.params
);
2093 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
2095 this.scrollUp(this.params
);
2098 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
2099 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
2102 // if (this.prefix === '>') {
2103 // this.resetTitleModes(this.params);
2106 // if (this.params.length > 2) {
2107 // this.initMouseTracking(this.params);
2110 if (this.params
.length
< 2 && !this.prefix
) {
2111 this.scrollDown(this.params
);
2116 // Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
2118 this.cursorBackwardTab(this.params
);
2121 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
2123 this.repeatPrecedingCharacter(this.params
);
2126 // CSI Ps g Tab Clear (TBC).
2128 this.tabClear(this.params
);
2131 // CSI Pm i Media Copy (MC).
2134 // this.mediaCopy(this.params);
2137 // CSI Pm m Character Attributes (SGR).
2139 // case 'm': // duplicate
2140 // if (this.prefix === '>') {
2141 // this.setResources(this.params);
2143 // this.charAttributes(this.params);
2147 // CSI Ps n Device Status Report (DSR).
2149 // case 'n': // duplicate
2150 // if (this.prefix === '>') {
2151 // this.disableModifiers(this.params);
2153 // this.deviceStatus(this.params);
2157 // CSI > Ps p Set pointer mode.
2158 // CSI ! p Soft terminal reset (DECSTR).
2160 // Request ANSI mode (DECRQM).
2162 // Request DEC private mode (DECRQM).
2165 switch (this.prefix
) {
2167 // this.setPointerMode(this.params);
2170 this.softReset(this.params
);
2173 // if (this.postfix === '$') {
2174 // this.requestPrivateMode(this.params);
2178 // if (this.postfix === '"') {
2179 // this.setConformanceLevel(this.params);
2180 // } else if (this.postfix === '$') {
2181 // this.requestAnsiMode(this.params);
2187 // CSI Ps q Load LEDs (DECLL).
2191 // if (this.postfix === ' ') {
2192 // this.setCursorStyle(this.params);
2195 // if (this.postfix === '"') {
2196 // this.setCharProtectionAttr(this.params);
2199 // this.loadLEDs(this.params);
2203 // Set Scrolling Region [top;bottom] (default = full size of win-
2206 // CSI Pt; Pl; Pb; Pr; Ps$ r
2207 // case 'r': // duplicate
2208 // if (this.prefix === '?') {
2209 // this.restorePrivateValues(this.params);
2210 // } else if (this.postfix === '$') {
2211 // this.setAttrInRectangle(this.params);
2213 // this.setScrollRegion(this.params);
2217 // CSI s Save cursor (ANSI.SYS).
2219 // case 's': // duplicate
2220 // if (this.prefix === '?') {
2221 // this.savePrivateValues(this.params);
2223 // this.saveCursor(this.params);
2227 // CSI Ps ; Ps ; Ps t
2228 // CSI Pt; Pl; Pb; Pr; Ps$ t
2232 // if (this.postfix === '$') {
2233 // this.reverseAttrInRectangle(this.params);
2234 // } else if (this.postfix === ' ') {
2235 // this.setWarningBellVolume(this.params);
2237 // if (this.prefix === '>') {
2238 // this.setTitleModeFeature(this.params);
2240 // this.manipulateWindow(this.params);
2245 // CSI u Restore cursor (ANSI.SYS).
2247 // case 'u': // duplicate
2248 // if (this.postfix === ' ') {
2249 // this.setMarginBellVolume(this.params);
2251 // this.restoreCursor(this.params);
2255 // CSI Pt; Pl; Pb; Pr; Pp; Pt; Pl; Pp$ v
2257 // if (this.postfix === '$') {
2258 // this.copyRectagle(this.params);
2262 // CSI Pt ; Pl ; Pb ; Pr ' w
2264 // if (this.postfix === '\'') {
2265 // this.enableFilterRectangle(this.params);
2269 // CSI Ps x Request Terminal Parameters (DECREQTPARM).
2270 // CSI Ps x Select Attribute Change Extent (DECSACE).
2271 // CSI Pc; Pt; Pl; Pb; Pr$ x
2273 // if (this.postfix === '$') {
2274 // this.fillRectangle(this.params);
2276 // this.requestParameters(this.params);
2277 // //this.__(this.params);
2282 // CSI Pt; Pl; Pb; Pr$ z
2284 // if (this.postfix === '\'') {
2285 // this.enableLocatorReporting(this.params);
2286 // } else if (this.postfix === '$') {
2287 // this.eraseRectangle(this.params);
2292 // CSI Pt; Pl; Pb; Pr$ {
2294 // if (this.postfix === '\'') {
2295 // this.setLocatorEvents(this.params);
2296 // } else if (this.postfix === '$') {
2297 // this.selectiveEraseRectangle(this.params);
2303 // if (this.postfix === '\'') {
2304 // this.requestLocatorPosition(this.params);
2309 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
2311 // if (this.postfix === ' ') {
2312 // this.insertColumns(this.params);
2317 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
2319 // if (this.postfix === ' ') {
2320 // this.deleteColumns(this.params);
2325 this.error('Unknown CSI code: %s.', ch
);
2334 if (ch
=== '\x1b' || ch
=== '\x07') {
2335 if (ch
=== '\x1b') i
++;
2337 switch (this.prefix
) {
2338 // User-Defined Keys (DECUDK).
2342 // Request Status String (DECRQSS).
2343 // test: echo -e '\eP$q"p\e\\'
2345 var pt
= this.currentParam
2362 + (this.scrollTop
+ 1)
2364 + (this.scrollBottom
+ 1)
2374 this.error('Unknown DCS Pt: %s.', pt
);
2379 this.send('\x1bP' + +valid
+ '$r' + pt
+ '\x1b\\');
2382 // Set Termcap/Terminfo Data (xterm, experimental).
2386 // Request Termcap/Terminfo String (xterm, experimental)
2387 // Regular xterm does not even respond to this sequence.
2388 // This can cause a small glitch in vim.
2389 // test: echo -ne '\eP+q6b64\e\\'
2391 var pt
= this.currentParam
2394 this.send('\x1bP' + +valid
+ '+r' + pt
+ '\x1b\\');
2398 this.error('Unknown DCS prefix: %s.', this.prefix
);
2402 this.currentParam
= 0;
2404 this.state
= normal
;
2405 } else if (!this.currentParam
) {
2406 if (!this.prefix
&& ch
!== '$' && ch
!== '+') {
2407 this.currentParam
= ch
;
2408 } else if (this.prefix
.length
=== 2) {
2409 this.currentParam
= ch
;
2414 this.currentParam
+= ch
;
2420 if (ch
=== '\x1b' || ch
=== '\x07') {
2421 if (ch
=== '\x1b') i
++;
2422 this.state
= normal
;
2428 this.updateRange(this.y
);
2429 this.refresh(this.refreshStart
, this.refreshEnd
);
2432 Terminal
.prototype.writeln = function(data
) {
2433 this.write(data
+ '\r\n');
2437 // https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
2438 Terminal
.prototype.keyDown = function(ev
) {
2440 var result
= this.evaluateKeyEscapeSequence(ev
);
2442 if (result
.scrollDisp
) {
2443 this.scrollDisp(result
.scrollDisp
);
2444 return this.cancel(ev
);
2447 if (isThirdLevelShift(this, ev
)) {
2451 if (result
.cancel
) {
2452 // The event is canceled at the end already, is this necessary?
2453 this.cancel(ev
, true);
2460 this.emit('keydown', ev
);
2461 this.emit('key', result
.key
, ev
);
2463 this.handler(result
.key
);
2465 return this.cancel(ev
, true);
2469 * Returns an object that determines how a KeyboardEvent should be handled. The key of the
2470 * returned value is the new key code to pass to the PTY.
2472 * Reference: http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
2474 Terminal
.prototype.evaluateKeyEscapeSequence = function(ev
) {
2476 // Whether to cancel event propogation (NOTE: this may not be needed since the event is
2477 // canceled at the end of keyDown
2479 // The new key even to emit
2481 // The number of characters to scroll, if this is defined it will cancel the event
2482 scrollDisp
: undefined
2484 switch (ev
.keyCode
) {
2488 result
.key
= '\x08'; // ^H
2491 result
.key
= '\x7f'; // ^?
2496 result
.key
= '\x1b[Z';
2500 result
.cancel
= true;
2505 result
.cancel
= true;
2509 result
.key
= '\x1b';
2510 result
.cancel
= true;
2515 result
.key
= '\x1bb' // Jump a word back
2516 result
.cancel
= true;
2520 result
.key
= '\x1b[5D'; // Jump a word back
2523 if (this.applicationCursor
) {
2524 result
.key
= '\x1bOD'; // SS3 as ^[O for 7-bit
2527 result
.key
= '\x1b[D';
2532 result
.key
= '\x1bf' // Jump a word forward
2533 result
.cancel
= true;
2537 result
.key
= '\x1b[5C'; // Jump a word forward
2540 if (this.applicationCursor
) {
2541 result
.key
= '\x1bOC';
2544 result
.key
= '\x1b[C';
2548 if (this.applicationCursor
) {
2549 result
.key
= '\x1bOA';
2553 result
.scrollDisp
= -1;
2555 result
.key
= '\x1b[A';
2560 if (this.applicationCursor
) {
2561 result
.key
= '\x1bOB';
2565 result
.scrollDisp
= 1;
2567 result
.key
= '\x1b[B';
2572 if (!ev
.shiftKey
&& !ev
.ctrlKey
) {
2573 // <Ctrl> or <Shift> + <Insert> are used to
2574 // copy-paste on some systems.
2575 result
.key
= '\x1b[2~';
2579 case 46: result
.key
= '\x1b[3~'; break;
2582 if (this.applicationKeypad
) {
2583 result
.key
= '\x1bOH';
2586 result
.key
= '\x1bOH';
2590 if (this.applicationKeypad
) {
2591 result
.key
= '\x1bOF';
2594 result
.key
= '\x1bOF';
2599 result
.scrollDisp
= -(this.rows
- 1);
2601 result
.key
= '\x1b[5~';
2607 result
.scrollDisp
= this.rows
- 1;
2609 result
.key
= '\x1b[6~';
2613 case 112: result
.key
= '\x1bOP'; break;
2614 case 113: result
.key
= '\x1bOQ'; break;
2615 case 114: result
.key
= '\x1bOR'; break;
2616 case 115: result
.key
= '\x1bOS'; break;
2617 case 116: result
.key
= '\x1b[15~'; break;
2618 case 117: result
.key
= '\x1b[17~'; break;
2619 case 118: result
.key
= '\x1b[18~'; break;
2620 case 119: result
.key
= '\x1b[19~'; break;
2621 case 120: result
.key
= '\x1b[20~'; break;
2622 case 121: result
.key
= '\x1b[21~'; break;
2623 case 122: result
.key
= '\x1b[23~'; break;
2624 case 123: result
.key
= '\x1b[24~'; break;
2627 if (ev
.ctrlKey
&& !ev
.shiftKey
&& !ev
.altKey
&& !ev
.metaKey
) {
2628 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2629 result
.key
= String
.fromCharCode(ev
.keyCode
- 64);
2630 } else if (ev
.keyCode
=== 32) {
2632 result
.key
= String
.fromCharCode(0);
2633 } else if (ev
.keyCode
>= 51 && ev
.keyCode
<= 55) {
2634 // escape, file sep, group sep, record sep, unit sep
2635 result
.key
= String
.fromCharCode(ev
.keyCode
- 51 + 27);
2636 } else if (ev
.keyCode
=== 56) {
2638 result
.key
= String
.fromCharCode(127);
2639 } else if (ev
.keyCode
=== 219) {
2641 result
.key
= String
.fromCharCode(27);
2642 } else if (ev
.keyCode
=== 221) {
2644 result
.key
= String
.fromCharCode(29);
2646 } else if (!this.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) {
2647 // On Mac this is a third level shift. Use <Esc> instead.
2648 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2649 result
.key
= '\x1b' + String
.fromCharCode(ev
.keyCode
+ 32);
2650 } else if (ev
.keyCode
=== 192) {
2651 result
.key
= '\x1b`';
2652 } else if (ev
.keyCode
>= 48 && ev
.keyCode
<= 57) {
2653 result
.key
= '\x1b' + (ev
.keyCode
- 48);
2661 Terminal
.prototype.setgLevel = function(g
) {
2663 this.charset
= this.charsets
[g
];
2666 Terminal
.prototype.setgCharset = function(g
, charset
) {
2667 this.charsets
[g
] = charset
;
2668 if (this.glevel
=== g
) {
2669 this.charset
= charset
;
2673 Terminal
.prototype.keyPress = function(ev
) {
2678 } else if (ev
.which
== null) {
2680 } else if (ev
.which
!== 0 && ev
.charCode
!== 0) {
2687 (ev
.altKey
|| ev
.ctrlKey
|| ev
.metaKey
) && !isThirdLevelShift(this, ev
)
2692 key
= String
.fromCharCode(key
);
2695 * When a key is pressed and a character is sent to the terminal, then clear any text
2696 * selected in the terminal.
2699 this.clearSelection();
2702 this.emit('keypress', key
, ev
);
2703 this.emit('key', key
, ev
);
2707 this.cancel(ev
, true);
2712 Terminal
.prototype.send = function(data
) {
2716 setTimeout(function() {
2717 self
.handler(self
.queue
);
2725 Terminal
.prototype.bell = function() {
2726 if (!this.visualBell
) return;
2728 this.element
.style
.borderColor
= 'white';
2729 setTimeout(function() {
2730 self
.element
.style
.borderColor
= '';
2732 if (this.popOnBell
) this.focus();
2735 Terminal
.prototype.log = function() {
2736 if (!this.debug
) return;
2737 if (!this.context
.console
|| !this.context
.console
.log
) return;
2738 var args
= Array
.prototype.slice
.call(arguments
);
2739 this.context
.console
.log
.apply(this.context
.console
, args
);
2742 Terminal
.prototype.error = function() {
2743 if (!this.debug
) return;
2744 if (!this.context
.console
|| !this.context
.console
.error
) return;
2745 var args
= Array
.prototype.slice
.call(arguments
);
2746 this.context
.console
.error
.apply(this.context
.console
, args
);
2750 * Resizes the terminal.
2752 * @param {number} x The number of columns to resize to.
2753 * @param {number} y The number of rows to resize to.
2757 Terminal
.prototype.resize = function(x
, y
) {
2765 if (x
=== this.cols
&& y
=== this.rows
) {
2775 ch
= [this.defAttr
, ' ']; // does xterm use the default attr?
2776 i
= this.lines
.length
;
2778 while (this.lines
[i
].length
< x
) {
2779 this.lines
[i
].push(ch
);
2783 i
= this.lines
.length
;
2785 while (this.lines
[i
].length
> x
) {
2786 this.lines
[i
].pop();
2799 // y is rows, not this.y
2800 if (this.lines
.length
< y
+ this.ybase
) {
2801 if (this.ybase
> 0 && this.lines
.length
<= this.ybase
+ this.y
+ addToY
+ 1) {
2802 // There is room above the buffer and there are no empty elements below the line,
2808 // Add a blank line if there is no buffer left at the top to scroll to, or if there
2809 // are blank lines after the cursor
2810 this.lines
.push(this.blankLine());
2813 if (this.children
.length
< y
) {
2819 if (this.lines
.length
> y
+ this.ybase
) {
2820 if (this.lines
.length
> this.ybase
+ this.y
+ 1) {
2821 // The line is a blank line below the cursor, remove it
2824 // The line is the cursor, scroll down
2829 if (this.children
.length
> y
) {
2830 el
= this.children
.shift();
2832 el
.parentNode
.removeChild(el
);
2839 * Make sure that the cursor stays on screen
2853 this.scrollBottom
= y
- 1;
2855 this.refresh(0, this.rows
- 1);
2859 this.emit('resize', {terminal
: this, cols
: x
, rows
: y
});
2862 Terminal
.prototype.updateRange = function(y
) {
2863 if (y
< this.refreshStart
) this.refreshStart
= y
;
2864 if (y
> this.refreshEnd
) this.refreshEnd
= y
;
2865 // if (y > this.refreshEnd) {
2866 // this.refreshEnd = y;
2867 // if (y > this.rows - 1) {
2868 // this.refreshEnd = this.rows - 1;
2873 Terminal
.prototype.maxRange = function() {
2874 this.refreshStart
= 0;
2875 this.refreshEnd
= this.rows
- 1;
2878 Terminal
.prototype.setupStops = function(i
) {
2880 if (!this.tabs
[i
]) {
2881 i
= this.prevStop(i
);
2888 for (; i
< this.cols
; i
+= 8) {
2889 this.tabs
[i
] = true;
2893 Terminal
.prototype.prevStop = function(x
) {
2894 if (x
== null) x
= this.x
;
2895 while (!this.tabs
[--x
] && x
> 0);
2896 return x
>= this.cols
2901 Terminal
.prototype.nextStop = function(x
) {
2902 if (x
== null) x
= this.x
;
2903 while (!this.tabs
[++x
] && x
< this.cols
);
2904 return x
>= this.cols
2909 Terminal
.prototype.eraseRight = function(x
, y
) {
2910 var line
= this.lines
[this.ybase
+ y
]
2911 , ch
= [this.eraseAttr(), ' ']; // xterm
2914 for (; x
< this.cols
; x
++) {
2918 this.updateRange(y
);
2921 Terminal
.prototype.eraseLeft = function(x
, y
) {
2922 var line
= this.lines
[this.ybase
+ y
]
2923 , ch
= [this.eraseAttr(), ' ']; // xterm
2926 while (x
--) line
[x
] = ch
;
2928 this.updateRange(y
);
2931 Terminal
.prototype.eraseLine = function(y
) {
2932 this.eraseRight(0, y
);
2935 Terminal
.prototype.blankLine = function(cur
) {
2940 var ch
= [attr
, ' ']
2944 for (; i
< this.cols
; i
++) {
2951 Terminal
.prototype.ch = function(cur
) {
2953 ? [this.eraseAttr(), ' ']
2954 : [this.defAttr
, ' '];
2957 Terminal
.prototype.is = function(term
) {
2958 var name
= this.termName
;
2959 return (name
+ '').indexOf(term
) === 0;
2962 Terminal
.prototype.handler = function(data
) {
2963 this.emit('data', data
);
2966 Terminal
.prototype.handleTitle = function(title
) {
2967 this.emit('title', title
);
2974 // ESC D Index (IND is 0x84).
2975 Terminal
.prototype.index = function() {
2977 if (this.y
> this.scrollBottom
) {
2981 this.state
= normal
;
2984 // ESC M Reverse Index (RI is 0x8d).
2985 Terminal
.prototype.reverseIndex = function() {
2988 if (this.y
< this.scrollTop
) {
2990 // possibly move the code below to term.reverseScroll();
2991 // test: echo -ne '\e[1;1H\e[44m\eM\e[0m'
2992 // blankLine(true) is xterm/linux behavior
2993 this.lines
.splice(this.y
+ this.ybase
, 0, this.blankLine(true));
2994 j
= this.rows
- 1 - this.scrollBottom
;
2995 this.lines
.splice(this.rows
- 1 + this.ybase
- j
+ 1, 1);
2997 this.updateRange(this.scrollTop
);
2998 this.updateRange(this.scrollBottom
);
3000 this.state
= normal
;
3003 // ESC c Full Reset (RIS).
3004 Terminal
.prototype.reset = function() {
3005 this.options
.rows
= this.rows
;
3006 this.options
.cols
= this.cols
;
3007 Terminal
.call(this, this.options
);
3008 this.refresh(0, this.rows
- 1);
3011 // ESC H Tab Set (HTS is 0x88).
3012 Terminal
.prototype.tabSet = function() {
3013 this.tabs
[this.x
] = true;
3014 this.state
= normal
;
3022 // Cursor Up Ps Times (default = 1) (CUU).
3023 Terminal
.prototype.cursorUp = function(params
) {
3024 var param
= params
[0];
3025 if (param
< 1) param
= 1;
3027 if (this.y
< 0) this.y
= 0;
3031 // Cursor Down Ps Times (default = 1) (CUD).
3032 Terminal
.prototype.cursorDown = function(params
) {
3033 var param
= params
[0];
3034 if (param
< 1) param
= 1;
3036 if (this.y
>= this.rows
) {
3037 this.y
= this.rows
- 1;
3042 // Cursor Forward Ps Times (default = 1) (CUF).
3043 Terminal
.prototype.cursorForward = function(params
) {
3044 var param
= params
[0];
3045 if (param
< 1) param
= 1;
3047 if (this.x
>= this.cols
) {
3048 this.x
= this.cols
- 1;
3053 // Cursor Backward Ps Times (default = 1) (CUB).
3054 Terminal
.prototype.cursorBackward = function(params
) {
3055 var param
= params
[0];
3056 if (param
< 1) param
= 1;
3058 if (this.x
< 0) this.x
= 0;
3062 // Cursor Position [row;column] (default = [1,1]) (CUP).
3063 Terminal
.prototype.cursorPos = function(params
) {
3066 row
= params
[0] - 1;
3068 if (params
.length
>= 2) {
3069 col
= params
[1] - 1;
3076 } else if (row
>= this.rows
) {
3077 row
= this.rows
- 1;
3082 } else if (col
>= this.cols
) {
3083 col
= this.cols
- 1;
3090 // CSI Ps J Erase in Display (ED).
3091 // Ps = 0 -> Erase Below (default).
3092 // Ps = 1 -> Erase Above.
3093 // Ps = 2 -> Erase All.
3094 // Ps = 3 -> Erase Saved Lines (xterm).
3096 // Erase in Display (DECSED).
3097 // Ps = 0 -> Selective Erase Below (default).
3098 // Ps = 1 -> Selective Erase Above.
3099 // Ps = 2 -> Selective Erase All.
3100 Terminal
.prototype.eraseInDisplay = function(params
) {
3102 switch (params
[0]) {
3104 this.eraseRight(this.x
, this.y
);
3106 for (; j
< this.rows
; j
++) {
3111 this.eraseLeft(this.x
, this.y
);
3119 while (j
--) this.eraseLine(j
);
3127 // CSI Ps K Erase in Line (EL).
3128 // Ps = 0 -> Erase to Right (default).
3129 // Ps = 1 -> Erase to Left.
3130 // Ps = 2 -> Erase All.
3132 // Erase in Line (DECSEL).
3133 // Ps = 0 -> Selective Erase to Right (default).
3134 // Ps = 1 -> Selective Erase to Left.
3135 // Ps = 2 -> Selective Erase All.
3136 Terminal
.prototype.eraseInLine = function(params
) {
3137 switch (params
[0]) {
3139 this.eraseRight(this.x
, this.y
);
3142 this.eraseLeft(this.x
, this.y
);
3145 this.eraseLine(this.y
);
3150 // CSI Pm m Character Attributes (SGR).
3151 // Ps = 0 -> Normal (default).
3153 // Ps = 4 -> Underlined.
3154 // Ps = 5 -> Blink (appears as Bold).
3155 // Ps = 7 -> Inverse.
3156 // Ps = 8 -> Invisible, i.e., hidden (VT300).
3157 // Ps = 2 2 -> Normal (neither bold nor faint).
3158 // Ps = 2 4 -> Not underlined.
3159 // Ps = 2 5 -> Steady (not blinking).
3160 // Ps = 2 7 -> Positive (not inverse).
3161 // Ps = 2 8 -> Visible, i.e., not hidden (VT300).
3162 // Ps = 3 0 -> Set foreground color to Black.
3163 // Ps = 3 1 -> Set foreground color to Red.
3164 // Ps = 3 2 -> Set foreground color to Green.
3165 // Ps = 3 3 -> Set foreground color to Yellow.
3166 // Ps = 3 4 -> Set foreground color to Blue.
3167 // Ps = 3 5 -> Set foreground color to Magenta.
3168 // Ps = 3 6 -> Set foreground color to Cyan.
3169 // Ps = 3 7 -> Set foreground color to White.
3170 // Ps = 3 9 -> Set foreground color to default (original).
3171 // Ps = 4 0 -> Set background color to Black.
3172 // Ps = 4 1 -> Set background color to Red.
3173 // Ps = 4 2 -> Set background color to Green.
3174 // Ps = 4 3 -> Set background color to Yellow.
3175 // Ps = 4 4 -> Set background color to Blue.
3176 // Ps = 4 5 -> Set background color to Magenta.
3177 // Ps = 4 6 -> Set background color to Cyan.
3178 // Ps = 4 7 -> Set background color to White.
3179 // Ps = 4 9 -> Set background color to default (original).
3181 // If 16-color support is compiled, the following apply. Assume
3182 // that xterm's resources are set so that the ISO color codes are
3183 // the first 8 of a set of 16. Then the aixterm colors are the
3184 // bright versions of the ISO colors:
3185 // Ps = 9 0 -> Set foreground color to Black.
3186 // Ps = 9 1 -> Set foreground color to Red.
3187 // Ps = 9 2 -> Set foreground color to Green.
3188 // Ps = 9 3 -> Set foreground color to Yellow.
3189 // Ps = 9 4 -> Set foreground color to Blue.
3190 // Ps = 9 5 -> Set foreground color to Magenta.
3191 // Ps = 9 6 -> Set foreground color to Cyan.
3192 // Ps = 9 7 -> Set foreground color to White.
3193 // Ps = 1 0 0 -> Set background color to Black.
3194 // Ps = 1 0 1 -> Set background color to Red.
3195 // Ps = 1 0 2 -> Set background color to Green.
3196 // Ps = 1 0 3 -> Set background color to Yellow.
3197 // Ps = 1 0 4 -> Set background color to Blue.
3198 // Ps = 1 0 5 -> Set background color to Magenta.
3199 // Ps = 1 0 6 -> Set background color to Cyan.
3200 // Ps = 1 0 7 -> Set background color to White.
3202 // If xterm is compiled with the 16-color support disabled, it
3203 // supports the following, from rxvt:
3204 // Ps = 1 0 0 -> Set foreground and background color to
3207 // If 88- or 256-color support is compiled, the following apply.
3208 // Ps = 3 8 ; 5 ; Ps -> Set foreground color to the second
3210 // Ps = 4 8 ; 5 ; Ps -> Set background color to the second
3212 Terminal
.prototype.charAttributes = function(params
) {
3213 // Optimize a single SGR0.
3214 if (params
.length
=== 1 && params
[0] === 0) {
3215 this.curAttr
= this.defAttr
;
3219 var l
= params
.length
3221 , flags
= this.curAttr
>> 18
3222 , fg
= (this.curAttr
>> 9) & 0x1ff
3223 , bg
= this.curAttr
& 0x1ff
3226 for (; i
< l
; i
++) {
3228 if (p
>= 30 && p
<= 37) {
3231 } else if (p
>= 40 && p
<= 47) {
3234 } else if (p
>= 90 && p
<= 97) {
3238 } else if (p
>= 100 && p
<= 107) {
3242 } else if (p
=== 0) {
3244 flags
= this.defAttr
>> 18;
3245 fg
= (this.defAttr
>> 9) & 0x1ff;
3246 bg
= this.defAttr
& 0x1ff;
3250 } else if (p
=== 1) {
3253 } else if (p
=== 4) {
3256 } else if (p
=== 5) {
3259 } else if (p
=== 7) {
3260 // inverse and positive
3261 // test with: echo -e '\e[31m\e[42mhello\e[7mworld\e[27mhi\e[m'
3263 } else if (p
=== 8) {
3266 } else if (p
=== 22) {
3269 } else if (p
=== 24) {
3272 } else if (p
=== 25) {
3275 } else if (p
=== 27) {
3278 } else if (p
=== 28) {
3281 } else if (p
=== 39) {
3283 fg
= (this.defAttr
>> 9) & 0x1ff;
3284 } else if (p
=== 49) {
3286 bg
= this.defAttr
& 0x1ff;
3287 } else if (p
=== 38) {
3289 if (params
[i
+ 1] === 2) {
3293 params
[i
+ 1] & 0xff,
3294 params
[i
+ 2] & 0xff);
3295 if (fg
=== -1) fg
= 0x1ff;
3297 } else if (params
[i
+ 1] === 5) {
3299 p
= params
[i
] & 0xff;
3302 } else if (p
=== 48) {
3304 if (params
[i
+ 1] === 2) {
3308 params
[i
+ 1] & 0xff,
3309 params
[i
+ 2] & 0xff);
3310 if (bg
=== -1) bg
= 0x1ff;
3312 } else if (params
[i
+ 1] === 5) {
3314 p
= params
[i
] & 0xff;
3317 } else if (p
=== 100) {
3319 fg
= (this.defAttr
>> 9) & 0x1ff;
3320 bg
= this.defAttr
& 0x1ff;
3322 this.error('Unknown SGR attribute: %d.', p
);
3326 this.curAttr
= (flags
<< 18) | (fg
<< 9) | bg
;
3329 // CSI Ps n Device Status Report (DSR).
3330 // Ps = 5 -> Status Report. Result (``OK'') is
3332 // Ps = 6 -> Report Cursor Position (CPR) [row;column].
3336 // Device Status Report (DSR, DEC-specific).
3337 // Ps = 6 -> Report Cursor Position (CPR) [row;column] as CSI
3338 // ? r ; c R (assumes page is zero).
3339 // Ps = 1 5 -> Report Printer status as CSI ? 1 0 n (ready).
3340 // or CSI ? 1 1 n (not ready).
3341 // Ps = 2 5 -> Report UDK status as CSI ? 2 0 n (unlocked)
3342 // or CSI ? 2 1 n (locked).
3343 // Ps = 2 6 -> Report Keyboard status as
3344 // CSI ? 2 7 ; 1 ; 0 ; 0 n (North American).
3345 // The last two parameters apply to VT400 & up, and denote key-
3346 // board ready and LK01 respectively.
3347 // Ps = 5 3 -> Report Locator status as
3348 // CSI ? 5 3 n Locator available, if compiled-in, or
3349 // CSI ? 5 0 n No Locator, if not.
3350 Terminal
.prototype.deviceStatus = function(params
) {
3352 switch (params
[0]) {
3355 this.send('\x1b[0n');
3366 } else if (this.prefix
=== '?') {
3367 // modern xterm doesnt seem to
3368 // respond to any of these except ?6, 6, and 5
3369 switch (params
[0]) {
3380 // this.send('\x1b[?11n');
3383 // dont support user defined keys
3384 // this.send('\x1b[?21n');
3387 // north american keyboard
3388 // this.send('\x1b[?27;1;0;0n');
3391 // no dec locator/mouse
3392 // this.send('\x1b[?50n');
3403 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
3404 Terminal
.prototype.insertChars = function(params
) {
3405 var param
, row
, j
, ch
;
3408 if (param
< 1) param
= 1;
3410 row
= this.y
+ this.ybase
;
3412 ch
= [this.eraseAttr(), ' ']; // xterm
3414 while (param
-- && j
< this.cols
) {
3415 this.lines
[row
].splice(j
++, 0, ch
);
3416 this.lines
[row
].pop();
3421 // Cursor Next Line Ps Times (default = 1) (CNL).
3422 // same as CSI Ps B ?
3423 Terminal
.prototype.cursorNextLine = function(params
) {
3424 var param
= params
[0];
3425 if (param
< 1) param
= 1;
3427 if (this.y
>= this.rows
) {
3428 this.y
= this.rows
- 1;
3434 // Cursor Preceding Line Ps Times (default = 1) (CNL).
3436 Terminal
.prototype.cursorPrecedingLine = function(params
) {
3437 var param
= params
[0];
3438 if (param
< 1) param
= 1;
3440 if (this.y
< 0) this.y
= 0;
3445 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
3446 Terminal
.prototype.cursorCharAbsolute = function(params
) {
3447 var param
= params
[0];
3448 if (param
< 1) param
= 1;
3453 // Insert Ps Line(s) (default = 1) (IL).
3454 Terminal
.prototype.insertLines = function(params
) {
3458 if (param
< 1) param
= 1;
3459 row
= this.y
+ this.ybase
;
3461 j
= this.rows
- 1 - this.scrollBottom
;
3462 j
= this.rows
- 1 + this.ybase
- j
+ 1;
3465 // test: echo -e '\e[44m\e[1L\e[0m'
3466 // blankLine(true) - xterm/linux behavior
3467 this.lines
.splice(row
, 0, this.blankLine(true));
3468 this.lines
.splice(j
, 1);
3472 this.updateRange(this.y
);
3473 this.updateRange(this.scrollBottom
);
3477 // Delete Ps Line(s) (default = 1) (DL).
3478 Terminal
.prototype.deleteLines = function(params
) {
3482 if (param
< 1) param
= 1;
3483 row
= this.y
+ this.ybase
;
3485 j
= this.rows
- 1 - this.scrollBottom
;
3486 j
= this.rows
- 1 + this.ybase
- j
;
3489 // test: echo -e '\e[44m\e[1M\e[0m'
3490 // blankLine(true) - xterm/linux behavior
3491 this.lines
.splice(j
+ 1, 0, this.blankLine(true));
3492 this.lines
.splice(row
, 1);
3496 this.updateRange(this.y
);
3497 this.updateRange(this.scrollBottom
);
3501 // Delete Ps Character(s) (default = 1) (DCH).
3502 Terminal
.prototype.deleteChars = function(params
) {
3506 if (param
< 1) param
= 1;
3508 row
= this.y
+ this.ybase
;
3509 ch
= [this.eraseAttr(), ' ']; // xterm
3512 this.lines
[row
].splice(this.x
, 1);
3513 this.lines
[row
].push(ch
);
3518 // Erase Ps Character(s) (default = 1) (ECH).
3519 Terminal
.prototype.eraseChars = function(params
) {
3520 var param
, row
, j
, ch
;
3523 if (param
< 1) param
= 1;
3525 row
= this.y
+ this.ybase
;
3527 ch
= [this.eraseAttr(), ' ']; // xterm
3529 while (param
-- && j
< this.cols
) {
3530 this.lines
[row
][j
++] = ch
;
3534 // CSI Pm ` Character Position Absolute
3535 // [column] (default = [row,1]) (HPA).
3536 Terminal
.prototype.charPosAbsolute = function(params
) {
3537 var param
= params
[0];
3538 if (param
< 1) param
= 1;
3540 if (this.x
>= this.cols
) {
3541 this.x
= this.cols
- 1;
3546 // Horizontal Position Relative
3548 Terminal
.prototype.HPositionRelative = function(params
) {
3549 var param
= params
[0];
3550 if (param
< 1) param
= 1;
3552 if (this.x
>= this.cols
) {
3553 this.x
= this.cols
- 1;
3557 // CSI Ps c Send Device Attributes (Primary DA).
3558 // Ps = 0 or omitted -> request attributes from terminal. The
3559 // response depends on the decTerminalID resource setting.
3560 // -> CSI ? 1 ; 2 c (``VT100 with Advanced Video Option'')
3561 // -> CSI ? 1 ; 0 c (``VT101 with No Options'')
3562 // -> CSI ? 6 c (``VT102'')
3563 // -> CSI ? 6 0 ; 1 ; 2 ; 6 ; 8 ; 9 ; 1 5 ; c (``VT220'')
3564 // The VT100-style response parameters do not mean anything by
3565 // themselves. VT220 parameters do, telling the host what fea-
3566 // tures the terminal supports:
3567 // Ps = 1 -> 132-columns.
3568 // Ps = 2 -> Printer.
3569 // Ps = 6 -> Selective erase.
3570 // Ps = 8 -> User-defined keys.
3571 // Ps = 9 -> National replacement character sets.
3572 // Ps = 1 5 -> Technical characters.
3573 // Ps = 2 2 -> ANSI color, e.g., VT525.
3574 // Ps = 2 9 -> ANSI text locator (i.e., DEC Locator mode).
3576 // Send Device Attributes (Secondary DA).
3577 // Ps = 0 or omitted -> request the terminal's identification
3578 // code. The response depends on the decTerminalID resource set-
3579 // ting. It should apply only to VT220 and up, but xterm extends
3581 // -> CSI > Pp ; Pv ; Pc c
3582 // where Pp denotes the terminal type
3583 // Pp = 0 -> ``VT100''.
3584 // Pp = 1 -> ``VT220''.
3585 // and Pv is the firmware version (for xterm, this was originally
3586 // the XFree86 patch number, starting with 95). In a DEC termi-
3587 // nal, Pc indicates the ROM cartridge registration number and is
3589 // More information:
3590 // xterm/charproc.c - line 2012, for more information.
3591 // vim responds with ^[[?0c or ^[[?1c after the terminal's response (?)
3592 Terminal
.prototype.sendDeviceAttributes = function(params
) {
3593 if (params
[0] > 0) return;
3596 if (this.is('xterm')
3597 || this.is('rxvt-unicode')
3598 || this.is('screen')) {
3599 this.send('\x1b[?1;2c');
3600 } else if (this.is('linux')) {
3601 this.send('\x1b[?6c');
3603 } else if (this.prefix
=== '>') {
3605 // seem to spit this
3606 // out around ~370 times (?).
3607 if (this.is('xterm')) {
3608 this.send('\x1b[>0;276;0c');
3609 } else if (this.is('rxvt-unicode')) {
3610 this.send('\x1b[>85;95;0c');
3611 } else if (this.is('linux')) {
3612 // not supported by linux console.
3613 // linux console echoes parameters.
3614 this.send(params
[0] + 'c');
3615 } else if (this.is('screen')) {
3616 this.send('\x1b[>83;40003;0c');
3622 // Line Position Absolute [row] (default = [1,column]) (VPA).
3623 Terminal
.prototype.linePosAbsolute = function(params
) {
3624 var param
= params
[0];
3625 if (param
< 1) param
= 1;
3627 if (this.y
>= this.rows
) {
3628 this.y
= this.rows
- 1;
3632 // 145 65 e * VPR - Vertical Position Relative
3634 Terminal
.prototype.VPositionRelative = function(params
) {
3635 var param
= params
[0];
3636 if (param
< 1) param
= 1;
3638 if (this.y
>= this.rows
) {
3639 this.y
= this.rows
- 1;
3644 // Horizontal and Vertical Position [row;column] (default =
3646 Terminal
.prototype.HVPosition = function(params
) {
3647 if (params
[0] < 1) params
[0] = 1;
3648 if (params
[1] < 1) params
[1] = 1;
3650 this.y
= params
[0] - 1;
3651 if (this.y
>= this.rows
) {
3652 this.y
= this.rows
- 1;
3655 this.x
= params
[1] - 1;
3656 if (this.x
>= this.cols
) {
3657 this.x
= this.cols
- 1;
3661 // CSI Pm h Set Mode (SM).
3662 // Ps = 2 -> Keyboard Action Mode (AM).
3663 // Ps = 4 -> Insert Mode (IRM).
3664 // Ps = 1 2 -> Send/receive (SRM).
3665 // Ps = 2 0 -> Automatic Newline (LNM).
3667 // DEC Private Mode Set (DECSET).
3668 // Ps = 1 -> Application Cursor Keys (DECCKM).
3669 // Ps = 2 -> Designate USASCII for character sets G0-G3
3670 // (DECANM), and set VT100 mode.
3671 // Ps = 3 -> 132 Column Mode (DECCOLM).
3672 // Ps = 4 -> Smooth (Slow) Scroll (DECSCLM).
3673 // Ps = 5 -> Reverse Video (DECSCNM).
3674 // Ps = 6 -> Origin Mode (DECOM).
3675 // Ps = 7 -> Wraparound Mode (DECAWM).
3676 // Ps = 8 -> Auto-repeat Keys (DECARM).
3677 // Ps = 9 -> Send Mouse X & Y on button press. See the sec-
3678 // tion Mouse Tracking.
3679 // Ps = 1 0 -> Show toolbar (rxvt).
3680 // Ps = 1 2 -> Start Blinking Cursor (att610).
3681 // Ps = 1 8 -> Print form feed (DECPFF).
3682 // Ps = 1 9 -> Set print extent to full screen (DECPEX).
3683 // Ps = 2 5 -> Show Cursor (DECTCEM).
3684 // Ps = 3 0 -> Show scrollbar (rxvt).
3685 // Ps = 3 5 -> Enable font-shifting functions (rxvt).
3686 // Ps = 3 8 -> Enter Tektronix Mode (DECTEK).
3687 // Ps = 4 0 -> Allow 80 -> 132 Mode.
3688 // Ps = 4 1 -> more(1) fix (see curses resource).
3689 // Ps = 4 2 -> Enable Nation Replacement Character sets (DECN-
3691 // Ps = 4 4 -> Turn On Margin Bell.
3692 // Ps = 4 5 -> Reverse-wraparound Mode.
3693 // Ps = 4 6 -> Start Logging. This is normally disabled by a
3694 // compile-time option.
3695 // Ps = 4 7 -> Use Alternate Screen Buffer. (This may be dis-
3696 // abled by the titeInhibit resource).
3697 // Ps = 6 6 -> Application keypad (DECNKM).
3698 // Ps = 6 7 -> Backarrow key sends backspace (DECBKM).
3699 // Ps = 1 0 0 0 -> Send Mouse X & Y on button press and
3700 // release. See the section Mouse Tracking.
3701 // Ps = 1 0 0 1 -> Use Hilite Mouse Tracking.
3702 // Ps = 1 0 0 2 -> Use Cell Motion Mouse Tracking.
3703 // Ps = 1 0 0 3 -> Use All Motion Mouse Tracking.
3704 // Ps = 1 0 0 4 -> Send FocusIn/FocusOut events.
3705 // Ps = 1 0 0 5 -> Enable Extended Mouse Mode.
3706 // Ps = 1 0 1 0 -> Scroll to bottom on tty output (rxvt).
3707 // Ps = 1 0 1 1 -> Scroll to bottom on key press (rxvt).
3708 // Ps = 1 0 3 4 -> Interpret "meta" key, sets eighth bit.
3709 // (enables the eightBitInput resource).
3710 // Ps = 1 0 3 5 -> Enable special modifiers for Alt and Num-
3711 // Lock keys. (This enables the numLock resource).
3712 // Ps = 1 0 3 6 -> Send ESC when Meta modifies a key. (This
3713 // enables the metaSendsEscape resource).
3714 // Ps = 1 0 3 7 -> Send DEL from the editing-keypad Delete
3716 // Ps = 1 0 3 9 -> Send ESC when Alt modifies a key. (This
3717 // enables the altSendsEscape resource).
3718 // Ps = 1 0 4 0 -> Keep selection even if not highlighted.
3719 // (This enables the keepSelection resource).
3720 // Ps = 1 0 4 1 -> Use the CLIPBOARD selection. (This enables
3721 // the selectToClipboard resource).
3722 // Ps = 1 0 4 2 -> Enable Urgency window manager hint when
3723 // Control-G is received. (This enables the bellIsUrgent
3725 // Ps = 1 0 4 3 -> Enable raising of the window when Control-G
3726 // is received. (enables the popOnBell resource).
3727 // Ps = 1 0 4 7 -> Use Alternate Screen Buffer. (This may be
3728 // disabled by the titeInhibit resource).
3729 // Ps = 1 0 4 8 -> Save cursor as in DECSC. (This may be dis-
3730 // abled by the titeInhibit resource).
3731 // Ps = 1 0 4 9 -> Save cursor as in DECSC and use Alternate
3732 // Screen Buffer, clearing it first. (This may be disabled by
3733 // the titeInhibit resource). This combines the effects of the 1
3734 // 0 4 7 and 1 0 4 8 modes. Use this with terminfo-based
3735 // applications rather than the 4 7 mode.
3736 // Ps = 1 0 5 0 -> Set terminfo/termcap function-key mode.
3737 // Ps = 1 0 5 1 -> Set Sun function-key mode.
3738 // Ps = 1 0 5 2 -> Set HP function-key mode.
3739 // Ps = 1 0 5 3 -> Set SCO function-key mode.
3740 // Ps = 1 0 6 0 -> Set legacy keyboard emulation (X11R6).
3741 // Ps = 1 0 6 1 -> Set VT220 keyboard emulation.
3742 // Ps = 2 0 0 4 -> Set bracketed paste mode.
3744 // http://vt100.net/docs/vt220-rm/chapter4.html
3745 Terminal
.prototype.setMode = function(params
) {
3746 if (typeof params
=== 'object') {
3747 var l
= params
.length
3750 for (; i
< l
; i
++) {
3751 this.setMode(params
[i
]);
3760 this.insertMode
= true;
3763 //this.convertEol = true;
3766 } else if (this.prefix
=== '?') {
3769 this.applicationCursor
= true;
3772 this.setgCharset(0, Terminal
.charsets
.US
);
3773 this.setgCharset(1, Terminal
.charsets
.US
);
3774 this.setgCharset(2, Terminal
.charsets
.US
);
3775 this.setgCharset(3, Terminal
.charsets
.US
);
3776 // set VT100 mode here
3778 case 3: // 132 col mode
3779 this.savedCols
= this.cols
;
3780 this.resize(132, this.rows
);
3783 this.originMode
= true;
3786 this.wraparoundMode
= true;
3789 // this.cursorBlink = true;
3792 this.log('Serial port requested application keypad.');
3793 this.applicationKeypad
= true;
3795 case 9: // X10 Mouse
3796 // no release, no motion, no wheel, no modifiers.
3797 case 1000: // vt200 mouse
3799 // no modifiers, except control on the wheel.
3800 case 1002: // button event mouse
3801 case 1003: // any event mouse
3802 // any event - sends motion events,
3803 // even if there is no button held down.
3804 this.x10Mouse
= params
=== 9;
3805 this.vt200Mouse
= params
=== 1000;
3806 this.normalMouse
= params
> 1000;
3807 this.mouseEvents
= true;
3808 this.element
.style
.cursor
= 'default';
3809 this.log('Binding to mouse events.');
3811 case 1004: // send focusin/focusout events
3814 this.sendFocus
= true;
3816 case 1005: // utf8 ext mode mouse
3817 this.utfMouse
= true;
3818 // for wide terminals
3819 // simply encodes large values as utf8 characters
3821 case 1006: // sgr ext mode mouse
3822 this.sgrMouse
= true;
3823 // for wide terminals
3824 // does not add 32 to fields
3825 // press: ^[[<b;x;yM
3826 // release: ^[[<b;x;ym
3828 case 1015: // urxvt ext mode mouse
3829 this.urxvtMouse
= true;
3830 // for wide terminals
3831 // numbers for fields
3833 // motion: ^[[b;x;yT
3835 case 25: // show cursor
3836 this.cursorHidden
= false;
3838 case 1049: // alt screen buffer cursor
3839 //this.saveCursor();
3841 case 47: // alt screen buffer
3842 case 1047: // alt screen buffer
3850 scrollTop
: this.scrollTop
,
3851 scrollBottom
: this.scrollBottom
,
3853 // XXX save charset(s) here?
3854 // charset: this.charset,
3855 // glevel: this.glevel,
3856 // charsets: this.charsets
3859 this.normal
= normal
;
3867 // CSI Pm l Reset Mode (RM).
3868 // Ps = 2 -> Keyboard Action Mode (AM).
3869 // Ps = 4 -> Replace Mode (IRM).
3870 // Ps = 1 2 -> Send/receive (SRM).
3871 // Ps = 2 0 -> Normal Linefeed (LNM).
3873 // DEC Private Mode Reset (DECRST).
3874 // Ps = 1 -> Normal Cursor Keys (DECCKM).
3875 // Ps = 2 -> Designate VT52 mode (DECANM).
3876 // Ps = 3 -> 80 Column Mode (DECCOLM).
3877 // Ps = 4 -> Jump (Fast) Scroll (DECSCLM).
3878 // Ps = 5 -> Normal Video (DECSCNM).
3879 // Ps = 6 -> Normal Cursor Mode (DECOM).
3880 // Ps = 7 -> No Wraparound Mode (DECAWM).
3881 // Ps = 8 -> No Auto-repeat Keys (DECARM).
3882 // Ps = 9 -> Don't send Mouse X & Y on button press.
3883 // Ps = 1 0 -> Hide toolbar (rxvt).
3884 // Ps = 1 2 -> Stop Blinking Cursor (att610).
3885 // Ps = 1 8 -> Don't print form feed (DECPFF).
3886 // Ps = 1 9 -> Limit print to scrolling region (DECPEX).
3887 // Ps = 2 5 -> Hide Cursor (DECTCEM).
3888 // Ps = 3 0 -> Don't show scrollbar (rxvt).
3889 // Ps = 3 5 -> Disable font-shifting functions (rxvt).
3890 // Ps = 4 0 -> Disallow 80 -> 132 Mode.
3891 // Ps = 4 1 -> No more(1) fix (see curses resource).
3892 // Ps = 4 2 -> Disable Nation Replacement Character sets (DEC-
3894 // Ps = 4 4 -> Turn Off Margin Bell.
3895 // Ps = 4 5 -> No Reverse-wraparound Mode.
3896 // Ps = 4 6 -> Stop Logging. (This is normally disabled by a
3897 // compile-time option).
3898 // Ps = 4 7 -> Use Normal Screen Buffer.
3899 // Ps = 6 6 -> Numeric keypad (DECNKM).
3900 // Ps = 6 7 -> Backarrow key sends delete (DECBKM).
3901 // Ps = 1 0 0 0 -> Don't send Mouse X & Y on button press and
3902 // release. See the section Mouse Tracking.
3903 // Ps = 1 0 0 1 -> Don't use Hilite Mouse Tracking.
3904 // Ps = 1 0 0 2 -> Don't use Cell Motion Mouse Tracking.
3905 // Ps = 1 0 0 3 -> Don't use All Motion Mouse Tracking.
3906 // Ps = 1 0 0 4 -> Don't send FocusIn/FocusOut events.
3907 // Ps = 1 0 0 5 -> Disable Extended Mouse Mode.
3908 // Ps = 1 0 1 0 -> Don't scroll to bottom on tty output
3910 // Ps = 1 0 1 1 -> Don't scroll to bottom on key press (rxvt).
3911 // Ps = 1 0 3 4 -> Don't interpret "meta" key. (This disables
3912 // the eightBitInput resource).
3913 // Ps = 1 0 3 5 -> Disable special modifiers for Alt and Num-
3914 // Lock keys. (This disables the numLock resource).
3915 // Ps = 1 0 3 6 -> Don't send ESC when Meta modifies a key.
3916 // (This disables the metaSendsEscape resource).
3917 // Ps = 1 0 3 7 -> Send VT220 Remove from the editing-keypad
3919 // Ps = 1 0 3 9 -> Don't send ESC when Alt modifies a key.
3920 // (This disables the altSendsEscape resource).
3921 // Ps = 1 0 4 0 -> Do not keep selection when not highlighted.
3922 // (This disables the keepSelection resource).
3923 // Ps = 1 0 4 1 -> Use the PRIMARY selection. (This disables
3924 // the selectToClipboard resource).
3925 // Ps = 1 0 4 2 -> Disable Urgency window manager hint when
3926 // Control-G is received. (This disables the bellIsUrgent
3928 // Ps = 1 0 4 3 -> Disable raising of the window when Control-
3929 // G is received. (This disables the popOnBell resource).
3930 // Ps = 1 0 4 7 -> Use Normal Screen Buffer, clearing screen
3931 // first if in the Alternate Screen. (This may be disabled by
3932 // the titeInhibit resource).
3933 // Ps = 1 0 4 8 -> Restore cursor as in DECRC. (This may be
3934 // disabled by the titeInhibit resource).
3935 // Ps = 1 0 4 9 -> Use Normal Screen Buffer and restore cursor
3936 // as in DECRC. (This may be disabled by the titeInhibit
3937 // resource). This combines the effects of the 1 0 4 7 and 1 0
3938 // 4 8 modes. Use this with terminfo-based applications rather
3939 // than the 4 7 mode.
3940 // Ps = 1 0 5 0 -> Reset terminfo/termcap function-key mode.
3941 // Ps = 1 0 5 1 -> Reset Sun function-key mode.
3942 // Ps = 1 0 5 2 -> Reset HP function-key mode.
3943 // Ps = 1 0 5 3 -> Reset SCO function-key mode.
3944 // Ps = 1 0 6 0 -> Reset legacy keyboard emulation (X11R6).
3945 // Ps = 1 0 6 1 -> Reset keyboard emulation to Sun/PC style.
3946 // Ps = 2 0 0 4 -> Reset bracketed paste mode.
3947 Terminal
.prototype.resetMode = function(params
) {
3948 if (typeof params
=== 'object') {
3949 var l
= params
.length
3952 for (; i
< l
; i
++) {
3953 this.resetMode(params
[i
]);
3962 this.insertMode
= false;
3965 //this.convertEol = false;
3968 } else if (this.prefix
=== '?') {
3971 this.applicationCursor
= false;
3974 if (this.cols
=== 132 && this.savedCols
) {
3975 this.resize(this.savedCols
, this.rows
);
3977 delete this.savedCols
;
3980 this.originMode
= false;
3983 this.wraparoundMode
= false;
3986 // this.cursorBlink = false;
3989 this.log('Switching back to normal keypad.');
3990 this.applicationKeypad
= false;
3992 case 9: // X10 Mouse
3993 case 1000: // vt200 mouse
3994 case 1002: // button event mouse
3995 case 1003: // any event mouse
3996 this.x10Mouse
= false;
3997 this.vt200Mouse
= false;
3998 this.normalMouse
= false;
3999 this.mouseEvents
= false;
4000 this.element
.style
.cursor
= '';
4002 case 1004: // send focusin/focusout events
4003 this.sendFocus
= false;
4005 case 1005: // utf8 ext mode mouse
4006 this.utfMouse
= false;
4008 case 1006: // sgr ext mode mouse
4009 this.sgrMouse
= false;
4011 case 1015: // urxvt ext mode mouse
4012 this.urxvtMouse
= false;
4014 case 25: // hide cursor
4015 this.cursorHidden
= true;
4017 case 1049: // alt screen buffer cursor
4019 case 47: // normal screen buffer
4020 case 1047: // normal screen buffer - clearing it first
4022 this.lines
= this.normal
.lines
;
4023 this.ybase
= this.normal
.ybase
;
4024 this.ydisp
= this.normal
.ydisp
;
4025 this.x
= this.normal
.x
;
4026 this.y
= this.normal
.y
;
4027 this.scrollTop
= this.normal
.scrollTop
;
4028 this.scrollBottom
= this.normal
.scrollBottom
;
4029 this.tabs
= this.normal
.tabs
;
4031 // if (params === 1049) {
4032 // this.x = this.savedX;
4033 // this.y = this.savedY;
4035 this.refresh(0, this.rows
- 1);
4044 // Set Scrolling Region [top;bottom] (default = full size of win-
4047 Terminal
.prototype.setScrollRegion = function(params
) {
4048 if (this.prefix
) return;
4049 this.scrollTop
= (params
[0] || 1) - 1;
4050 this.scrollBottom
= (params
[1] || this.rows
) - 1;
4056 // Save cursor (ANSI.SYS).
4057 Terminal
.prototype.saveCursor = function(params
) {
4058 this.savedX
= this.x
;
4059 this.savedY
= this.y
;
4063 // Restore cursor (ANSI.SYS).
4064 Terminal
.prototype.restoreCursor = function(params
) {
4065 this.x
= this.savedX
|| 0;
4066 this.y
= this.savedY
|| 0;
4074 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
4075 Terminal
.prototype.cursorForwardTab = function(params
) {
4076 var param
= params
[0] || 1;
4078 this.x
= this.nextStop();
4082 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
4083 Terminal
.prototype.scrollUp = function(params
) {
4084 var param
= params
[0] || 1;
4086 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
4087 this.lines
.splice(this.ybase
+ this.scrollBottom
, 0, this.blankLine());
4090 this.updateRange(this.scrollTop
);
4091 this.updateRange(this.scrollBottom
);
4094 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
4095 Terminal
.prototype.scrollDown = function(params
) {
4096 var param
= params
[0] || 1;
4098 this.lines
.splice(this.ybase
+ this.scrollBottom
, 1);
4099 this.lines
.splice(this.ybase
+ this.scrollTop
, 0, this.blankLine());
4102 this.updateRange(this.scrollTop
);
4103 this.updateRange(this.scrollBottom
);
4106 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
4107 // Initiate highlight mouse tracking. Parameters are
4108 // [func;startx;starty;firstrow;lastrow]. See the section Mouse
4110 Terminal
.prototype.initMouseTracking = function(params
) {
4111 // Relevant: DECSET 1001
4115 // Reset one or more features of the title modes to the default
4116 // value. Normally, "reset" disables the feature. It is possi-
4117 // ble to disable the ability to reset features by compiling a
4118 // different default for the title modes into xterm.
4119 // Ps = 0 -> Do not set window/icon labels using hexadecimal.
4120 // Ps = 1 -> Do not query window/icon labels using hexadeci-
4122 // Ps = 2 -> Do not set window/icon labels using UTF-8.
4123 // Ps = 3 -> Do not query window/icon labels using UTF-8.
4124 // (See discussion of "Title Modes").
4125 Terminal
.prototype.resetTitleModes = function(params
) {
4129 // CSI Ps Z Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
4130 Terminal
.prototype.cursorBackwardTab = function(params
) {
4131 var param
= params
[0] || 1;
4133 this.x
= this.prevStop();
4137 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
4138 Terminal
.prototype.repeatPrecedingCharacter = function(params
) {
4139 var param
= params
[0] || 1
4140 , line
= this.lines
[this.ybase
+ this.y
]
4141 , ch
= line
[this.x
- 1] || [this.defAttr
, ' '];
4143 while (param
--) line
[this.x
++] = ch
;
4146 // CSI Ps g Tab Clear (TBC).
4147 // Ps = 0 -> Clear Current Column (default).
4148 // Ps = 3 -> Clear All.
4150 // Ps = 2 -> Clear Stops on Line.
4151 // http://vt100.net/annarbor/aaa-ug/section6.html
4152 Terminal
.prototype.tabClear = function(params
) {
4153 var param
= params
[0];
4155 delete this.tabs
[this.x
];
4156 } else if (param
=== 3) {
4161 // CSI Pm i Media Copy (MC).
4162 // Ps = 0 -> Print screen (default).
4163 // Ps = 4 -> Turn off printer controller mode.
4164 // Ps = 5 -> Turn on printer controller mode.
4166 // Media Copy (MC, DEC-specific).
4167 // Ps = 1 -> Print line containing cursor.
4168 // Ps = 4 -> Turn off autoprint mode.
4169 // Ps = 5 -> Turn on autoprint mode.
4170 // Ps = 1 0 -> Print composed display, ignores DECPEX.
4171 // Ps = 1 1 -> Print all pages.
4172 Terminal
.prototype.mediaCopy = function(params
) {
4177 // Set or reset resource-values used by xterm to decide whether
4178 // to construct escape sequences holding information about the
4179 // modifiers pressed with a given key. The first parameter iden-
4180 // tifies the resource to set/reset. The second parameter is the
4181 // value to assign to the resource. If the second parameter is
4182 // omitted, the resource is reset to its initial value.
4183 // Ps = 1 -> modifyCursorKeys.
4184 // Ps = 2 -> modifyFunctionKeys.
4185 // Ps = 4 -> modifyOtherKeys.
4186 // If no parameters are given, all resources are reset to their
4188 Terminal
.prototype.setResources = function(params
) {
4193 // Disable modifiers which may be enabled via the CSI > Ps; Ps m
4194 // sequence. This corresponds to a resource value of "-1", which
4195 // cannot be set with the other sequence. The parameter identi-
4196 // fies the resource to be disabled:
4197 // Ps = 1 -> modifyCursorKeys.
4198 // Ps = 2 -> modifyFunctionKeys.
4199 // Ps = 4 -> modifyOtherKeys.
4200 // If the parameter is omitted, modifyFunctionKeys is disabled.
4201 // When modifyFunctionKeys is disabled, xterm uses the modifier
4202 // keys to make an extended sequence of functions rather than
4203 // adding a parameter to each function key to denote the modi-
4205 Terminal
.prototype.disableModifiers = function(params
) {
4210 // Set resource value pointerMode. This is used by xterm to
4211 // decide whether to hide the pointer cursor as the user types.
4212 // Valid values for the parameter:
4213 // Ps = 0 -> never hide the pointer.
4214 // Ps = 1 -> hide if the mouse tracking mode is not enabled.
4215 // Ps = 2 -> always hide the pointer. If no parameter is
4216 // given, xterm uses the default, which is 1 .
4217 Terminal
.prototype.setPointerMode = function(params
) {
4221 // CSI ! p Soft terminal reset (DECSTR).
4222 // http://vt100.net/docs/vt220-rm/table4-10.html
4223 Terminal
.prototype.softReset = function(params
) {
4224 this.cursorHidden
= false;
4225 this.insertMode
= false;
4226 this.originMode
= false;
4227 this.wraparoundMode
= false; // autowrap
4228 this.applicationKeypad
= false; // ?
4229 this.applicationCursor
= false;
4231 this.scrollBottom
= this.rows
- 1;
4232 this.curAttr
= this.defAttr
;
4233 this.x
= this.y
= 0; // ?
4234 this.charset
= null;
4235 this.glevel
= 0; // ??
4236 this.charsets
= [null]; // ??
4240 // Request ANSI mode (DECRQM). For VT300 and up, reply is
4242 // where Ps is the mode number as in RM, and Pm is the mode
4244 // 0 - not recognized
4247 // 3 - permanently set
4248 // 4 - permanently reset
4249 Terminal
.prototype.requestAnsiMode = function(params
) {
4254 // Request DEC private mode (DECRQM). For VT300 and up, reply is
4256 // where Ps is the mode number as in DECSET, Pm is the mode value
4257 // as in the ANSI DECRQM.
4258 Terminal
.prototype.requestPrivateMode = function(params
) {
4263 // Set conformance level (DECSCL). Valid values for the first
4265 // Ps = 6 1 -> VT100.
4266 // Ps = 6 2 -> VT200.
4267 // Ps = 6 3 -> VT300.
4268 // Valid values for the second parameter:
4269 // Ps = 0 -> 8-bit controls.
4270 // Ps = 1 -> 7-bit controls (always set for VT100).
4271 // Ps = 2 -> 8-bit controls.
4272 Terminal
.prototype.setConformanceLevel = function(params
) {
4276 // CSI Ps q Load LEDs (DECLL).
4277 // Ps = 0 -> Clear all LEDS (default).
4278 // Ps = 1 -> Light Num Lock.
4279 // Ps = 2 -> Light Caps Lock.
4280 // Ps = 3 -> Light Scroll Lock.
4281 // Ps = 2 1 -> Extinguish Num Lock.
4282 // Ps = 2 2 -> Extinguish Caps Lock.
4283 // Ps = 2 3 -> Extinguish Scroll Lock.
4284 Terminal
.prototype.loadLEDs = function(params
) {
4289 // Set cursor style (DECSCUSR, VT520).
4290 // Ps = 0 -> blinking block.
4291 // Ps = 1 -> blinking block (default).
4292 // Ps = 2 -> steady block.
4293 // Ps = 3 -> blinking underline.
4294 // Ps = 4 -> steady underline.
4295 Terminal
.prototype.setCursorStyle = function(params
) {
4300 // Select character protection attribute (DECSCA). Valid values
4301 // for the parameter:
4302 // Ps = 0 -> DECSED and DECSEL can erase (default).
4303 // Ps = 1 -> DECSED and DECSEL cannot erase.
4304 // Ps = 2 -> DECSED and DECSEL can erase.
4305 Terminal
.prototype.setCharProtectionAttr = function(params
) {
4310 // Restore DEC Private Mode Values. The value of Ps previously
4311 // saved is restored. Ps values are the same as for DECSET.
4312 Terminal
.prototype.restorePrivateValues = function(params
) {
4316 // CSI Pt; Pl; Pb; Pr; Ps$ r
4317 // Change Attributes in Rectangular Area (DECCARA), VT400 and up.
4318 // Pt; Pl; Pb; Pr denotes the rectangle.
4319 // Ps denotes the SGR attributes to change: 0, 1, 4, 5, 7.
4320 // NOTE: xterm doesn't enable this code by default.
4321 Terminal
.prototype.setAttrInRectangle = function(params
) {
4331 for (; t
< b
+ 1; t
++) {
4332 line
= this.lines
[this.ybase
+ t
];
4333 for (i
= l
; i
< r
; i
++) {
4334 line
[i
] = [attr
, line
[i
][1]];
4339 this.updateRange(params
[0]);
4340 this.updateRange(params
[2]);
4344 // CSI Pc; Pt; Pl; Pb; Pr$ x
4345 // Fill Rectangular Area (DECFRA), VT420 and up.
4346 // Pc is the character to use.
4347 // Pt; Pl; Pb; Pr denotes the rectangle.
4348 // NOTE: xterm doesn't enable this code by default.
4349 Terminal
.prototype.fillRectangle = function(params
) {
4359 for (; t
< b
+ 1; t
++) {
4360 line
= this.lines
[this.ybase
+ t
];
4361 for (i
= l
; i
< r
; i
++) {
4362 line
[i
] = [line
[i
][0], String
.fromCharCode(ch
)];
4367 this.updateRange(params
[1]);
4368 this.updateRange(params
[3]);
4372 // Enable Locator Reporting (DECELR).
4373 // Valid values for the first parameter:
4374 // Ps = 0 -> Locator disabled (default).
4375 // Ps = 1 -> Locator enabled.
4376 // Ps = 2 -> Locator enabled for one report, then disabled.
4377 // The second parameter specifies the coordinate unit for locator
4379 // Valid values for the second parameter:
4380 // Pu = 0 <- or omitted -> default to character cells.
4381 // Pu = 1 <- device physical pixels.
4382 // Pu = 2 <- character cells.
4383 Terminal
.prototype.enableLocatorReporting = function(params
) {
4384 var val
= params
[0] > 0;
4385 //this.mouseEvents = val;
4386 //this.decLocator = val;
4389 // CSI Pt; Pl; Pb; Pr$ z
4390 // Erase Rectangular Area (DECERA), VT400 and up.
4391 // Pt; Pl; Pb; Pr denotes the rectangle.
4392 // NOTE: xterm doesn't enable this code by default.
4393 Terminal
.prototype.eraseRectangle = function(params
) {
4403 ch
= [this.eraseAttr(), ' ']; // xterm?
4405 for (; t
< b
+ 1; t
++) {
4406 line
= this.lines
[this.ybase
+ t
];
4407 for (i
= l
; i
< r
; i
++) {
4413 this.updateRange(params
[0]);
4414 this.updateRange(params
[2]);
4419 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
4420 // NOTE: xterm doesn't enable this code by default.
4421 Terminal
.prototype.insertColumns = function() {
4422 var param
= params
[0]
4423 , l
= this.ybase
+ this.rows
4424 , ch
= [this.eraseAttr(), ' '] // xterm?
4428 for (i
= this.ybase
; i
< l
; i
++) {
4429 this.lines
[i
].splice(this.x
+ 1, 0, ch
);
4430 this.lines
[i
].pop();
4439 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
4440 // NOTE: xterm doesn't enable this code by default.
4441 Terminal
.prototype.deleteColumns = function() {
4442 var param
= params
[0]
4443 , l
= this.ybase
+ this.rows
4444 , ch
= [this.eraseAttr(), ' '] // xterm?
4448 for (i
= this.ybase
; i
< l
; i
++) {
4449 this.lines
[i
].splice(this.x
, 1);
4450 this.lines
[i
].push(ch
);
4461 Terminal
.charsets
= {};
4463 // DEC Special Character and Line Drawing Set.
4464 // http://vt100.net/docs/vt102-ug/table5-13.html
4465 // A lot of curses apps use this if they see TERM=xterm.
4466 // testing: echo -e '\e(0a\e(B'
4467 // The xterm output sometimes seems to conflict with the
4468 // reference above. xterm seems in line with the reference
4469 // when running vttest however.
4470 // The table below now uses xterm's output from vttest.
4471 Terminal
.charsets
.SCLD
= { // (0
4472 '`': '\u25c6', // '◆'
4473 'a': '\u2592', // '▒'
4474 'b': '\u0009', // '\t'
4475 'c': '\u000c', // '\f'
4476 'd': '\u000d', // '\r'
4477 'e': '\u000a', // '\n'
4478 'f': '\u00b0', // '°'
4479 'g': '\u00b1', // '±'
4480 'h': '\u2424', // '\u2424' (NL)
4481 'i': '\u000b', // '\v'
4482 'j': '\u2518', // '┘'
4483 'k': '\u2510', // '┐'
4484 'l': '\u250c', // '┌'
4485 'm': '\u2514', // '└'
4486 'n': '\u253c', // '┼'
4487 'o': '\u23ba', // '⎺'
4488 'p': '\u23bb', // '⎻'
4489 'q': '\u2500', // '─'
4490 'r': '\u23bc', // '⎼'
4491 's': '\u23bd', // '⎽'
4492 't': '\u251c', // '├'
4493 'u': '\u2524', // '┤'
4494 'v': '\u2534', // '┴'
4495 'w': '\u252c', // '┬'
4496 'x': '\u2502', // '│'
4497 'y': '\u2264', // '≤'
4498 'z': '\u2265', // '≥'
4499 '{': '\u03c0', // 'π'
4500 '|': '\u2260', // '≠'
4501 '}': '\u00a3', // '£'
4502 '~': '\u00b7' // '·'
4505 Terminal
.charsets
.UK
= null; // (A
4506 Terminal
.charsets
.US
= null; // (B (USASCII)
4507 Terminal
.charsets
.Dutch
= null; // (4
4508 Terminal
.charsets
.Finnish
= null; // (C or (5
4509 Terminal
.charsets
.French
= null; // (R
4510 Terminal
.charsets
.FrenchCanadian
= null; // (Q
4511 Terminal
.charsets
.German
= null; // (K
4512 Terminal
.charsets
.Italian
= null; // (Y
4513 Terminal
.charsets
.NorwegianDanish
= null; // (E or (6
4514 Terminal
.charsets
.Spanish
= null; // (Z
4515 Terminal
.charsets
.Swedish
= null; // (H or (7
4516 Terminal
.charsets
.Swiss
= null; // (=
4517 Terminal
.charsets
.ISOLatin
= null; // /A
4523 function contains(el
, arr
) {
4524 for (var i
= 0; i
< arr
.length
; i
+= 1) {
4525 if (el
=== arr
[i
]) {
4532 function on(el
, type
, handler
, capture
) {
4533 if (!Array
.isArray(el
)) {
4536 el
.forEach(function (element
) {
4537 element
.addEventListener(type
, handler
, capture
|| false);
4541 function off(el
, type
, handler
, capture
) {
4542 el
.removeEventListener(type
, handler
, capture
|| false);
4545 function cancel(ev
, force
) {
4546 if (!this.cancelEvents
&& !force
) {
4549 ev
.preventDefault();
4550 ev
.stopPropagation();
4554 function inherits(child
, parent
) {
4556 this.constructor = child
;
4558 f
.prototype = parent
.prototype;
4559 child
.prototype = new f
;
4562 // if bold is broken, we can't
4563 // use it in the terminal.
4564 function isBoldBroken(document
) {
4565 var body
= document
.getElementsByTagName('body')[0];
4566 var el
= document
.createElement('span');
4567 el
.innerHTML
= 'hello world';
4568 body
.appendChild(el
);
4569 var w1
= el
.scrollWidth
;
4570 el
.style
.fontWeight
= 'bold';
4571 var w2
= el
.scrollWidth
;
4572 body
.removeChild(el
);
4576 var String
= this.String
;
4577 var setTimeout
= this.setTimeout
;
4578 var setInterval
= this.setInterval
;
4580 function indexOf(obj
, el
) {
4583 if (obj
[i
] === el
) return i
;
4588 function isThirdLevelShift(term
, ev
) {
4590 (term
.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) ||
4591 (term
.isMSWindows
&& ev
.altKey
&& ev
.ctrlKey
&& !ev
.metaKey
);
4593 // Don't invoke for arrows, pageDown, home, backspace, etc.
4594 return thirdLevelKey
&& (!ev
.keyCode
|| ev
.keyCode
> 47);
4597 function isWide(ch
) {
4598 if (ch
<= '\uff00') return false;
4599 return (ch
>= '\uff01' && ch
<= '\uffbe')
4600 || (ch
>= '\uffc2' && ch
<= '\uffc7')
4601 || (ch
>= '\uffca' && ch
<= '\uffcf')
4602 || (ch
>= '\uffd2' && ch
<= '\uffd7')
4603 || (ch
>= '\uffda' && ch
<= '\uffdc')
4604 || (ch
>= '\uffe0' && ch
<= '\uffe6')
4605 || (ch
>= '\uffe8' && ch
<= '\uffee');
4608 function matchColor(r1
, g1
, b1
) {
4609 var hash
= (r1
<< 16) | (g1
<< 8) | b1
;
4611 if (matchColor
._cache
[hash
] != null) {
4612 return matchColor
._cache
[hash
];
4615 var ldiff
= Infinity
4624 for (; i
< Terminal
.vcolors
.length
; i
++) {
4625 c
= Terminal
.vcolors
[i
];
4630 diff
= matchColor
.distance(r1
, g1
, b1
, r2
, g2
, b2
);
4643 return matchColor
._cache
[hash
] = li
;
4646 matchColor
._cache
= {};
4648 // http://stackoverflow.com/questions/1633828
4649 matchColor
.distance = function(r1
, g1
, b1
, r2
, g2
, b2
) {
4650 return Math
.pow(30 * (r1
- r2
), 2)
4651 + Math
.pow(59 * (g1
- g2
), 2)
4652 + Math
.pow(11 * (b1
- b2
), 2);
4655 function each(obj
, iter
, con
) {
4656 if (obj
.forEach
) return obj
.forEach(iter
, con
);
4657 for (var i
= 0; i
< obj
.length
; i
++) {
4658 iter
.call(con
, obj
[i
], i
, obj
);
4662 function keys(obj
) {
4663 if (Object
.keys
) return Object
.keys(obj
);
4666 if (Object
.prototype.hasOwnProperty
.call(obj
, key
)) {
4677 Terminal
.EventEmitter
= EventEmitter
;
4678 Terminal
.inherits
= inherits
;
4681 * Adds an event listener to the terminal.
4683 * @param {string} event The name of the event. TODO: Document all event types
4684 * @param {function} callback The function to call when the event is triggered.
4690 Terminal
.cancel
= cancel
;