]>
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
241 this.queuedRefresh
= false;
243 this.cursorState
= 0;
244 this.cursorHidden
= false;
249 this.scrollBottom
= this.rows
- 1;
252 this.applicationKeypad
= false;
253 this.applicationCursor
= false;
254 this.originMode
= false;
255 this.insertMode
= false;
256 this.wraparoundMode
= false;
261 this.gcharset
= null;
263 this.charsets
= [null];
287 this.readable
= true;
288 this.writable
= true;
290 this.defAttr
= (0 << 18) | (257 << 9) | (256 << 0);
291 this.curAttr
= this.defAttr
;
294 this.currentParam
= 0;
299 * An array of all lines in the entire buffer, including the prompt. The lines are array of
300 * characters which are 2-length arrays where [0] is an attribute and [1] is the character.
305 this.lines
.push(this.blankLine());
310 this.debounceRefresh();
313 inherits(Terminal
, EventEmitter
);
315 // back_color_erase feature for xterm.
316 Terminal
.prototype.eraseAttr = function() {
317 // if (this.is('screen')) return this.defAttr;
318 return (this.defAttr
& ~0x1ff) | (this.curAttr
& 0x1ff);
322 * Allow refresh to execute only approximately 30 times a second. For commands that pass a
323 * significant amount of output to the write function, this prevents the terminal from maxing
324 * out the CPU and making the UI unresponsive. While commands can still run beyond what they do
325 * on the terminal, it is far better with a debounce in place as every single terminal
326 * manipulation does not need to be constructed in the DOM.
328 * A side-effect of this is that it makes ^C to interrupt a process seem more responsive.
330 Terminal
.prototype.debounceRefresh = function () {
332 window
.setInterval(function () {
333 self
.isRefreshing
= false;
334 if (self
.queuedRefresh
) {
335 // Do a full refresh in case multiple refreshes were requested.
336 self
.refresh(0, self
.rows
- 1);
346 Terminal
.tangoColors
= [
367 // Colors 0-15 + 16-255
368 // Much thanks to TooTallNate for writing this.
369 Terminal
.colors
= (function() {
370 var colors
= Terminal
.tangoColors
.slice()
371 , r
= [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff]
376 for (; i
< 216; i
++) {
377 out(r
[(i
/ 36) % 6 | 0], r
[(i
/ 6) % 6 | 0], r
[i
% 6]);
382 for (; i
< 24; i
++) {
387 function out(r
, g
, b
) {
388 colors
.push('#' + hex(r
) + hex(g
) + hex(b
));
393 return c
.length
< 2 ? '0' + c
: c
;
399 Terminal
._colors
= Terminal
.colors
.slice();
401 Terminal
.vcolors
= (function() {
403 , colors
= Terminal
.colors
407 for (; i
< 256; i
++) {
408 color
= parseInt(colors
[i
].substring(1), 16);
410 (color
>> 16) & 0xff,
423 Terminal
.defaults
= {
424 colors
: Terminal
.colors
,
436 // programFeatures: false,
440 Terminal
.options
= {};
442 Terminal
.focus
= null;
444 each(keys(Terminal
.defaults
), function(key
) {
445 Terminal
[key
] = Terminal
.defaults
[key
];
446 Terminal
.options
[key
] = Terminal
.defaults
[key
];
450 * Focus the terminal.
454 Terminal
.prototype.focus = function() {
455 if (document
.activeElement
=== this.element
) {
459 if (this.sendFocus
) {
464 this.element
.focus();
467 Terminal
.prototype.blur = function() {
468 if (Terminal
.focus
!== this) {
472 this.cursorState
= 0;
473 this.refresh(this.y
, this.y
);
476 if (this.sendFocus
) {
479 Terminal
.focus
= null;
483 * Initialize default behavior
485 Terminal
.prototype.initGlobal = function() {
486 Terminal
.bindKeys(this);
487 Terminal
.bindPaste(this);
488 Terminal
.bindCopy(this);
489 Terminal
.bindCut(this);
490 Terminal
.bindDrop(this);
494 * Clears all selected text, inside the terminal.
496 Terminal
.prototype.clearSelection = function() {
497 var selectionBaseNode
= window
.getSelection().baseNode
;
499 if (selectionBaseNode
&& (this.element
.contains(selectionBaseNode
.parentElement
))) {
500 window
.getSelection().removeAllRanges();
505 * This function temporarily enables (leases) the contentEditable value of the terminal, which
506 * should be set back to false within 5 seconds at most.
508 Terminal
.prototype.leaseContentEditable = function (ms
, callback
) {
511 term
.element
.contentEditable
= true;
512 setTimeout(function () {
513 term
.element
.contentEditable
= false;
514 if (typeof callback
== 'function') {
521 * Bind to paste event and allow both keyboard and right-click pasting, without having the
522 * contentEditable value set to true.
524 Terminal
.bindPaste = function(term
) {
525 on(term
.element
, 'paste', function(ev
) {
526 if (ev
.clipboardData
) {
527 var text
= ev
.clipboardData
.getData('text/plain');
528 term
.emit('paste', text
, ev
);
531 * Cancel the paste event, or else things will be pasted twice:
532 * 1. by the terminal handler
533 * 2. by the browser, because of the contentEditable value being true
535 term
.cancel(ev
, true);
538 * After the paste event is completed, always set the contentEditable value to false.
540 term
.element
.contentEditable
= false;
545 * Hack pasting with keyboard, in order to make it work without contentEditable.
546 * When a user types Ctrl + Shift + V or Cmd + V on a Mac, lease the contentEditable value
549 on(term
.element
, 'keydown', function (ev
) {
551 * If on a Mac, lease the contentEditable value temporarily, when the user presses
552 * the Cmd button, in order to cope with some sync issues on Safari.
554 if (term
.isMac
&& ev
.keyCode
== 91) {
555 term
.leaseContentEditable(1000);
558 if (ev
.keyCode
== 86) { // keyCode 96 corresponds to "v"
559 if (term
.isMac
&& ev
.metaKey
) {
560 term
.leaseContentEditable();
564 if (!term
.isMac
&& ev
.keyCode
== 45 && ev
.shiftKey
&& !ev
.ctrlKey
) {
565 // Shift + Insert pastes on windows and many linuxes
566 term
.leaseContentEditable();
571 * Hack pasting with right-click in order to allow right-click paste, by leasing the
572 * contentEditable value as true.
574 on(term
.element
, 'contextmenu', function (ev
) {
575 term
.leaseContentEditable();
581 * Apply key handling to the terminal
583 Terminal
.bindKeys = function(term
) {
584 on(term
.element
, 'keydown', function(ev
) {
588 on(term
.element
, 'keypress', function(ev
) {
594 * Prepares text copied from terminal selection, to be saved in the clipboard by:
595 * 1. stripping all trailing white spaces
596 * 2. converting all non-breaking spaces to regular spaces
597 * @param {string} text The copied text that needs processing for storing in clipboard
601 Terminal
.prepareCopiedTextForClipboard = function (text
) {
602 var space
= String
.fromCharCode(32),
603 nonBreakingSpace
= String
.fromCharCode(160),
604 allNonBreakingSpaces
= new RegExp(nonBreakingSpace
, 'g'),
605 processedText
= text
.split('\n').map(function (line
) {
607 * Strip all trailing white spaces and convert all non-breaking spaces to regular
610 var processedLine
= line
.replace(/\s+$/g, '').replace(allNonBreakingSpaces
, space
);
612 return processedLine
;
615 return processedText
;
619 * Binds copy functionality to the given terminal.
622 Terminal
.bindCopy = function(term
) {
623 on(term
.element
, 'copy', function(ev
) {
624 var copiedText
= window
.getSelection().toString(),
625 text
= Terminal
.prepareCopiedTextForClipboard(copiedText
);
627 ev
.clipboardData
.setData('text/plain', text
);
633 * Cancel the cut event completely
635 Terminal
.bindCut = function(term
) {
636 on(term
.element
, 'cut', function (ev
) {
642 Terminal
.bindDrop = function (term
) {
644 * Do not perform the "drop" event. Altering the contents of the
645 * terminal with drag n drop is unwanted behavior.
647 on(term
.element
, 'drop', function (ev
) {
648 term
.cancel(ev
, true);
653 Terminal
.click = function (term
) {
655 * Do not perform the "drop" event. Altering the contents of the
656 * terminal with drag n drop is unwanted behavior.
658 on(term
.element
, 'click', function (ev
) {
659 term
.cancel(ev
, true);
665 * Insert the given row to the terminal or produce a new one
666 * if no row argument is passed. Return the inserted row.
668 Terminal
.prototype.insertRow = function (row
) {
669 if (typeof row
!= 'object') {
670 row
= document
.createElement('div');
673 this.rowContainer
.appendChild(row
);
674 this.children
.push(row
);
681 * Opens the terminal within an element.
683 * @param {HTMLElement} parent The element to create the terminal within.
687 Terminal
.prototype.open = function(parent
) {
688 var self
=this, i
=0, div
;
690 this.parent
= parent
|| this.parent
;
693 throw new Error('Terminal requires a parent element.');
697 * Grab global elements
699 this.context
= this.parent
.ownerDocument
.defaultView
;
700 this.document
= this.parent
.ownerDocument
;
701 this.body
= this.document
.getElementsByTagName('body')[0];
706 if (this.context
.navigator
&& this.context
.navigator
.userAgent
) {
707 this.isMSIE
= !!~this.context
.navigator
.userAgent
.indexOf('MSIE');
711 * Find the users platform. We use this to interpret the meta key
712 * and ISO third level shifts.
713 * http://stackoverflow.com/questions/19877924/what-is-the-list-of-possible-values-for-navigator-platform-as-of-today
715 if (this.context
.navigator
&& this.context
.navigator
.platform
) {
716 this.isMac
= contains(
717 this.context
.navigator
.platform
,
718 ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K']
720 this.isIpad
= this.context
.navigator
.platform
=== 'iPad';
721 this.isIphone
= this.context
.navigator
.platform
=== 'iPhone';
722 this.isMSWindows
= contains(
723 this.context
.navigator
.platform
,
724 ['Windows', 'Win16', 'Win32', 'WinCE']
729 * Create main element container
731 this.element
= this.document
.createElement('div');
732 this.element
.classList
.add('terminal');
733 this.element
.classList
.add('xterm');
734 this.element
.classList
.add('xterm-theme-' + this.theme
);
735 this.element
.setAttribute('tabindex', 0);
736 this.element
.spellcheck
= 'false';
737 this.element
.onfocus = function() {
738 self
.emit('focus', {terminal
: this});
740 this.element
.onblur = function() {
741 self
.emit('blur', {terminal
: this});
745 * Create the container that will hold the lines of the terminal and then
746 * produce the lines the lines.
748 this.rowContainer
= document
.createElement('div');
749 this.rowContainer
.classList
.add('xterm-rows');
750 this.element
.appendChild(this.rowContainer
);
753 for (; i
< this.rows
; i
++) {
756 this.parent
.appendChild(this.element
);
759 this.refresh(0, this.rows
- 1);
761 // Initialize global actions that
762 // need to be taken on the document.
765 // Ensure there is a Terminal.focus.
768 // Start blinking the cursor.
771 on(this.element
, 'mouseup', function() {
772 var selection
= document
.getSelection(),
773 collapsed
= selection
.isCollapsed
,
774 isRange
= typeof collapsed
== 'boolean' ? !collapsed
: selection
.type
== 'Range';
780 // Listen for mouse events and translate
781 // them into terminal mouse protocols.
784 // Figure out whether boldness affects
785 // the character width of monospace fonts.
786 if (Terminal
.brokenBold
== null) {
787 Terminal
.brokenBold
= isBoldBroken(this.document
);
795 * Attempts to load an add-on using CommonJS or RequireJS (whichever is available).
796 * @param {string} addon The name of the addon to load
799 Terminal
.loadAddon = function(addon
, callback
) {
800 if (typeof exports
=== 'object' && typeof module
=== 'object') {
802 return require(__dirname
+ '/../addons/' + addon
);
803 } else if (typeof define
== 'function') {
805 return require(['../addons/' + addon
+ '/' + addon
], callback
);
807 console
.error('Cannot load a module without a CommonJS or RequireJS environment.');
813 // XTerm mouse events
814 // http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#Mouse%20Tracking
815 // To better understand these
816 // the xterm code is very helpful:
818 // button.c, charproc.c, misc.c
819 // Relevant functions in xterm/button.c:
820 // BtnCode, EmitButtonCode, EditorButton, SendMousePosition
821 Terminal
.prototype.bindMouse = function() {
822 var el
= this.element
826 var wheelEvent
= 'onmousewheel' in this.context
830 // mouseup, mousedown, mousewheel
831 // left click: ^[[M 3<^[[M#3<
832 // mousewheel up: ^[[M`3>
833 function sendButton(ev
) {
837 // get the xterm-style button
838 button
= getButton(ev
);
840 // get mouse coordinates
844 sendEvent(button
, pos
);
851 // keep it at the left
852 // button, just in case.
863 // motion example of a left click:
864 // ^[[M 3<^[[M@4<^[[M@5<^[[M@6<^[[M@7<^[[M#7<
865 function sendMove(ev
) {
872 // buttons marked as motions
873 // are incremented by 32
876 sendEvent(button
, pos
);
880 // position to characters
881 function encode(data
, ch
) {
882 if (!self
.utfMouse
) {
883 if (ch
=== 255) return data
.push(0);
884 if (ch
> 127) ch
= 127;
887 if (ch
=== 2047) return data
.push(0);
891 if (ch
> 2047) ch
= 2047;
892 data
.push(0xC0 | (ch
>> 6));
893 data
.push(0x80 | (ch
& 0x3F));
898 // send a mouse event:
899 // regular/utf8: ^[[M Cb Cx Cy
900 // urxvt: ^[[ Cb ; Cx ; Cy M
901 // sgr: ^[[ Cb ; Cx ; Cy M/m
902 // vt300: ^[[ 24(1/3/5)~ [ Cx , Cy ] \r
903 // locator: CSI P e ; P b ; P r ; P c ; P p & w
904 function sendEvent(button
, pos
) {
905 // self.emit('mouse', {
911 if (self
.vt300Mouse
) {
913 // http://www.vt100.net/docs/vt3xx-gp/chapter15.html
917 var data
= '\x1b[24';
918 if (button
=== 0) data
+= '1';
919 else if (button
=== 1) data
+= '3';
920 else if (button
=== 2) data
+= '5';
921 else if (button
=== 3) return;
923 data
+= '~[' + pos
.x
+ ',' + pos
.y
+ ']\r';
928 if (self
.decLocator
) {
933 if (button
=== 0) button
= 2;
934 else if (button
=== 1) button
= 4;
935 else if (button
=== 2) button
= 6;
936 else if (button
=== 3) button
= 3;
940 + (button
=== 3 ? 4 : 0)
951 if (self
.urxvtMouse
) {
956 self
.send('\x1b[' + button
+ ';' + pos
.x
+ ';' + pos
.y
+ 'M');
964 + ((button
& 3) === 3 ? button
& ~3 : button
)
969 + ((button
& 3) === 3 ? 'm' : 'M'));
975 encode(data
, button
);
979 self
.send('\x1b[M' + String
.fromCharCode
.apply(String
, data
));
982 function getButton(ev
) {
995 // 1, and 2 - with 64 added
998 button
= ev
.button
!= null
1005 button
= button
=== 1 ? 0 : button
=== 4 ? 1 : button
;
1011 case 'DOMMouseScroll':
1012 button
= ev
.detail
< 0
1017 button
= ev
.wheelDeltaY
> 0
1023 // next three bits are the modifiers:
1024 // 4 = shift, 8 = meta, 16 = control
1025 shift
= ev
.shiftKey
? 4 : 0;
1026 meta
= ev
.metaKey
? 8 : 0;
1027 ctrl
= ev
.ctrlKey
? 16 : 0;
1028 mod
= shift
| meta
| ctrl
;
1031 if (self
.vt200Mouse
) {
1034 } else if (!self
.normalMouse
) {
1039 button
= (32 + (mod
<< 2)) + button
;
1044 // mouse coordinates measured in cols/rows
1045 function getCoords(ev
) {
1048 // ignore browsers without pageX for now
1049 if (ev
.pageX
== null) return;
1055 // should probably check offsetParent
1056 // but this is more portable
1057 while (el
&& el
!== self
.document
.documentElement
) {
1060 el
= 'offsetParent' in el
1065 // convert to cols/rows
1066 w
= self
.element
.clientWidth
;
1067 h
= self
.element
.clientHeight
;
1068 x
= Math
.round((x
/ w
) * self
.cols
);
1069 y
= Math
.round((y
/ h
) * self
.rows
);
1071 // be sure to avoid sending
1072 // bad positions to the program
1074 if (x
> self
.cols
) x
= self
.cols
;
1076 if (y
> self
.rows
) y
= self
.rows
;
1078 // xterm sends raw bytes and
1079 // starts at 32 (SP) for each.
1086 type
: ev
.type
=== wheelEvent
1092 on(el
, 'mousedown', function(ev
) {
1093 if (!self
.mouseEvents
) return;
1102 //if (self.vt200Mouse && !self.normalMouse) {
1103 if (self
.vt200Mouse
) {
1104 sendButton({ __proto__
: ev
, type
: 'mouseup' });
1105 return self
.cancel(ev
);
1109 if (self
.normalMouse
) on(self
.document
, 'mousemove', sendMove
);
1111 // x10 compatibility mode can't send button releases
1112 if (!self
.x10Mouse
) {
1113 on(self
.document
, 'mouseup', function up(ev
) {
1115 if (self
.normalMouse
) off(self
.document
, 'mousemove', sendMove
);
1116 off(self
.document
, 'mouseup', up
);
1117 return self
.cancel(ev
);
1121 return self
.cancel(ev
);
1124 //if (self.normalMouse) {
1125 // on(self.document, 'mousemove', sendMove);
1128 on(el
, wheelEvent
, function(ev
) {
1129 if (!self
.mouseEvents
) return;
1132 || self
.decLocator
) return;
1134 return self
.cancel(ev
);
1137 // allow mousewheel scrolling in
1138 // the shell for example
1139 on(el
, wheelEvent
, function(ev
) {
1140 if (self
.mouseEvents
) return;
1141 if (self
.applicationKeypad
) return;
1142 if (ev
.type
=== 'DOMMouseScroll') {
1143 self
.scrollDisp(ev
.detail
< 0 ? -1 : 1);
1145 self
.scrollDisp(ev
.wheelDeltaY
> 0 ? -1 : 1);
1147 return self
.cancel(ev
);
1152 * Destroys the terminal.
1156 Terminal
.prototype.destroy = function() {
1157 this.readable
= false;
1158 this.writable
= false;
1160 this.handler = function() {};
1161 this.write = function() {};
1162 if (this.element
.parentNode
) {
1163 this.element
.parentNode
.removeChild(this.element
);
1165 //this.emit('close');
1170 * Flags used to render terminal text properly
1183 * In the screen buffer, each character
1184 * is stored as a an array with a character
1185 * and a 32-bit integer.
1186 * First value: a utf-16 character.
1188 * Next 9 bits: background color (0-511).
1189 * Next 9 bits: foreground color (0-511).
1190 * Next 14 bits: a mask for misc. flags:
1191 * 1=bold, 2=underline, 4=blink, 8=inverse, 16=invisible
1195 * Refreshes terminal content within two rows (inclusive).
1197 * @param {number} start The row to start from (between 0 and terminal's height terminal - 1)
1198 * @param {number} end The row to end at (between fromRow and terminal's height terminal - 1)
1202 Terminal
.prototype.refresh = function(start
, end
) {
1203 var x
, y
, i
, line
, out
, ch
, width
, data
, attr
, bg
, fg
, flags
, row
, parent
, focused
= document
.activeElement
;
1205 if (this.isRefreshing
) {
1206 this.queuedRefresh
= true;
1209 this.isRefreshing
= true;
1211 if (end
- start
>= this.rows
/ 2) {
1212 parent
= this.element
.parentNode
;
1213 if (parent
) parent
.removeChild(this.element
);
1219 if (end
>= this.rows
.length
) {
1220 this.log('`end` is too large. Most likely a bad CSR.');
1221 end
= this.rows
.length
- 1;
1224 for (; y
<= end
; y
++) {
1225 row
= y
+ this.ydisp
;
1227 line
= this.lines
[row
];
1230 if (this.y
=== y
- (this.ybase
- this.ydisp
)
1232 && !this.cursorHidden
) {
1238 attr
= this.defAttr
;
1241 for (; i
< width
; i
++) {
1245 if (i
=== x
) data
= -1;
1247 if (data
!== attr
) {
1248 if (attr
!== this.defAttr
) {
1251 if (data
!== this.defAttr
) {
1253 out
+= '<span class="reverse-video terminal-cursor">';
1255 var classNames
= [];
1258 fg
= (data
>> 9) & 0x1ff;
1261 if (flags
& Terminal
.flags
.BOLD
) {
1262 if (!Terminal
.brokenBold
) {
1263 classNames
.push('xterm-bold');
1265 // See: XTerm*boldColors
1266 if (fg
< 8) fg
+= 8;
1269 if (flags
& Terminal
.flags
.UNDERLINE
) {
1270 classNames
.push('xterm-underline');
1273 if (flags
& Terminal
.flags
.BLINK
) {
1274 classNames
.push('xterm-blink');
1278 * If inverse flag is on, then swap the foreground and background variables.
1280 if (flags
& Terminal
.flags
.INVERSE
) {
1281 /* One-line variable swap in JavaScript: http://stackoverflow.com/a/16201730 */
1282 bg = [fg, fg = bg][0];
1283 // Should inverse just be before the
1284 // above boldColors effect instead?
1285 if ((flags & 1) && fg < 8) fg += 8;
1288 if (flags & Terminal.flags.INVISIBLE) {
1289 classNames.push('xterm-hidden');
1293 * Weird situation: Invert flag used black foreground and white background results
1294 * in invalid background color, positioned at the 256 index of the 256 terminal
1295 * color map. Pin the colors manually in such a case.
1297 * Source: https://github.com/sourcelair/xterm.js/issues/57
1299 if (flags
& Terminal
.flags
.INVERSE
) {
1309 classNames
.push('xterm-bg-color-' + bg
);
1313 classNames
.push('xterm-color-' + fg
);
1317 if (classNames
.length
) {
1318 out
+= ' class="' + classNames
.join(' ') + '"';
1339 if (isWide(ch
)) i
++;
1348 if (attr
!== this.defAttr
) {
1352 this.children
[y
].innerHTML
= out
;
1356 parent
.appendChild(this.element
);
1360 * Return focus to previously focused element
1363 this.emit('refresh', {element
: this.element
, start
: start
, end
: end
});
1366 Terminal
.prototype._cursorBlink = function() {
1367 if (document
.activeElement
!== this.element
) return;
1368 this.cursorState
^= 1;
1369 this.refresh(this.y
, this.y
);
1372 Terminal
.prototype.showCursor = function() {
1373 if (!this.cursorState
) {
1374 this.cursorState
= 1;
1375 this.refresh(this.y
, this.y
);
1377 this.refreshBlink();
1381 Terminal
.prototype.startBlink = function() {
1382 if (!this.cursorBlink
) return;
1383 this._blink
= setInterval(this._cursorBlink
.bind(this), 500);
1386 Terminal
.prototype.refreshBlink = function() {
1387 if (!this.cursorBlink
) return;
1388 clearInterval(this._blink
);
1389 this._blink
= setInterval(this._cursorBlink
.bind(this), 500);
1392 Terminal
.prototype.scroll = function() {
1395 if (++this.ybase
=== this.scrollback
) {
1396 this.ybase
= this.ybase
/ 2 | 0;
1397 this.lines
= this.lines
.slice(-(this.ybase
+ this.rows
) + 1);
1400 this.ydisp
= this.ybase
;
1403 row
= this.ybase
+ this.rows
- 1;
1405 // subtract the bottom scroll region
1406 row
-= this.rows
- 1 - this.scrollBottom
;
1408 if (row
=== this.lines
.length
) {
1409 // potential optimization:
1410 // pushing is faster than splicing
1411 // when they amount to the same
1413 this.lines
.push(this.blankLine());
1416 this.lines
.splice(row
, 0, this.blankLine());
1419 if (this.scrollTop
!== 0) {
1420 if (this.ybase
!== 0) {
1422 this.ydisp
= this.ybase
;
1424 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
1428 this.updateRange(this.scrollTop
);
1429 this.updateRange(this.scrollBottom
);
1432 Terminal
.prototype.scrollDisp = function(disp
) {
1435 if (this.ydisp
> this.ybase
) {
1436 this.ydisp
= this.ybase
;
1437 } else if (this.ydisp
< 0) {
1441 this.refresh(0, this.rows
- 1);
1445 * Writes text to the terminal.
1447 * @param {string} text The text to write to the terminal.
1451 Terminal
.prototype.write = function(data
) {
1452 var l
= data
.length
, i
= 0, j
, cs
, ch
;
1454 this.refreshStart
= this.y
;
1455 this.refreshEnd
= this.y
;
1457 if (this.ybase
!== this.ydisp
) {
1458 this.ydisp
= this.ybase
;
1462 for (; i
< l
; i
++) {
1464 switch (this.state
) {
1475 if (this.convertEol
) {
1479 if (this.y
> this.scrollBottom
) {
1499 this.x
= this.nextStop();
1514 this.state
= escaped
;
1520 if (this.charset
&& this.charset
[ch
]) {
1521 ch
= this.charset
[ch
];
1524 if (this.x
>= this.cols
) {
1527 if (this.y
> this.scrollBottom
) {
1533 this.lines
[this.y
+ this.ybase
][this.x
] = [this.curAttr
, ch
];
1535 this.updateRange(this.y
);
1538 j
= this.y
+ this.ybase
;
1539 if (this.cols
< 2 || this.x
>= this.cols
) {
1540 this.lines
[j
][this.x
- 1] = [this.curAttr
, ' '];
1543 this.lines
[j
][this.x
] = [this.curAttr
, ' '];
1552 // ESC [ Control Sequence Introducer ( CSI is 0x9b).
1555 this.currentParam
= 0;
1559 // ESC ] Operating System Command ( OSC is 0x9d).
1562 this.currentParam
= 0;
1566 // ESC P Device Control String ( DCS is 0x90).
1569 this.currentParam
= 0;
1573 // ESC _ Application Program Command ( APC is 0x9f).
1575 this.state
= ignore
;
1578 // ESC ^ Privacy Message ( PM is 0x9e).
1580 this.state
= ignore
;
1583 // ESC c Full Reset (RIS).
1588 // ESC E Next Line ( NEL is 0x85).
1589 // ESC D Index ( IND is 0x84).
1597 // ESC M Reverse Index ( RI is 0x8d).
1599 this.reverseIndex();
1602 // ESC % Select default/utf-8 character set.
1603 // @ = default, G = utf-8
1605 //this.charset = null;
1607 this.setgCharset(0, Terminal
.charsets
.US
);
1608 this.state
= normal
;
1612 // ESC (,),*,+,-,. Designate G0-G2 Character Set.
1613 case '(': // <-- this seems to get all the attention
1639 this.state
= charset
;
1642 // Designate G3 Character Set (VT300).
1643 // A = ISO Latin-1 Supplemental.
1647 this.state
= charset
;
1652 // Single Shift Select of G2 Character Set
1653 // ( SS2 is 0x8e). This affects next character only.
1657 // Single Shift Select of G3 Character Set
1658 // ( SS3 is 0x8f). This affects next character only.
1662 // Invoke the G2 Character Set as GL (LS2).
1667 // Invoke the G3 Character Set as GL (LS3).
1672 // Invoke the G3 Character Set as GR (LS3R).
1677 // Invoke the G2 Character Set as GR (LS2R).
1682 // Invoke the G1 Character Set as GR (LS1R).
1687 // ESC 7 Save Cursor (DECSC).
1690 this.state
= normal
;
1693 // ESC 8 Restore Cursor (DECRC).
1695 this.restoreCursor();
1696 this.state
= normal
;
1699 // ESC # 3 DEC line height/width
1701 this.state
= normal
;
1705 // ESC H Tab Set (HTS is 0x88).
1710 // ESC = Application Keypad (DECPAM).
1712 this.log('Serial port requested application keypad.');
1713 this.applicationKeypad
= true;
1714 this.state
= normal
;
1717 // ESC > Normal Keypad (DECPNM).
1719 this.log('Switching back to normal keypad.');
1720 this.applicationKeypad
= false;
1721 this.state
= normal
;
1725 this.state
= normal
;
1726 this.error('Unknown ESC control: %s.', ch
);
1733 case '0': // DEC Special Character and Line Drawing Set.
1734 cs
= Terminal
.charsets
.SCLD
;
1737 cs
= Terminal
.charsets
.UK
;
1739 case 'B': // United States (USASCII).
1740 cs
= Terminal
.charsets
.US
;
1743 cs
= Terminal
.charsets
.Dutch
;
1745 case 'C': // Finnish
1747 cs
= Terminal
.charsets
.Finnish
;
1750 cs
= Terminal
.charsets
.French
;
1752 case 'Q': // FrenchCanadian
1753 cs
= Terminal
.charsets
.FrenchCanadian
;
1756 cs
= Terminal
.charsets
.German
;
1758 case 'Y': // Italian
1759 cs
= Terminal
.charsets
.Italian
;
1761 case 'E': // NorwegianDanish
1763 cs
= Terminal
.charsets
.NorwegianDanish
;
1765 case 'Z': // Spanish
1766 cs
= Terminal
.charsets
.Spanish
;
1768 case 'H': // Swedish
1770 cs
= Terminal
.charsets
.Swedish
;
1773 cs
= Terminal
.charsets
.Swiss
;
1775 case '/': // ISOLatin (actually /A)
1776 cs
= Terminal
.charsets
.ISOLatin
;
1780 cs
= Terminal
.charsets
.US
;
1783 this.setgCharset(this.gcharset
, cs
);
1784 this.gcharset
= null;
1785 this.state
= normal
;
1791 // Set Text Parameters.
1792 if (ch
=== '\x1b' || ch
=== '\x07') {
1793 if (ch
=== '\x1b') i
++;
1795 this.params
.push(this.currentParam
);
1797 switch (this.params
[0]) {
1801 if (this.params
[1]) {
1802 this.title
= this.params
[1];
1803 this.handleTitle(this.title
);
1811 // change dynamic colors
1823 // change dynamic ui colors
1835 // manipulate selection data
1853 this.currentParam
= 0;
1854 this.state
= normal
;
1856 if (!this.params
.length
) {
1857 if (ch
>= '0' && ch
<= '9') {
1859 this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1860 } else if (ch
=== ';') {
1861 this.params
.push(this.currentParam
);
1862 this.currentParam
= '';
1865 this.currentParam
+= ch
;
1872 if (ch
=== '?' || ch
=== '>' || ch
=== '!') {
1878 if (ch
>= '0' && ch
<= '9') {
1879 this.currentParam
= this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1883 // '$', '"', ' ', '\''
1884 if (ch
=== '$' || ch
=== '"' || ch
=== ' ' || ch
=== '\'') {
1889 this.params
.push(this.currentParam
);
1890 this.currentParam
= 0;
1893 if (ch
=== ';') break;
1895 this.state
= normal
;
1899 // Cursor Up Ps Times (default = 1) (CUU).
1901 this.cursorUp(this.params
);
1905 // Cursor Down Ps Times (default = 1) (CUD).
1907 this.cursorDown(this.params
);
1911 // Cursor Forward Ps Times (default = 1) (CUF).
1913 this.cursorForward(this.params
);
1917 // Cursor Backward Ps Times (default = 1) (CUB).
1919 this.cursorBackward(this.params
);
1923 // Cursor Position [row;column] (default = [1,1]) (CUP).
1925 this.cursorPos(this.params
);
1928 // CSI Ps J Erase in Display (ED).
1930 this.eraseInDisplay(this.params
);
1933 // CSI Ps K Erase in Line (EL).
1935 this.eraseInLine(this.params
);
1938 // CSI Pm m Character Attributes (SGR).
1941 this.charAttributes(this.params
);
1945 // CSI Ps n Device Status Report (DSR).
1948 this.deviceStatus(this.params
);
1957 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
1959 this.insertChars(this.params
);
1963 // Cursor Next Line Ps Times (default = 1) (CNL).
1965 this.cursorNextLine(this.params
);
1969 // Cursor Preceding Line Ps Times (default = 1) (CNL).
1971 this.cursorPrecedingLine(this.params
);
1975 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
1977 this.cursorCharAbsolute(this.params
);
1981 // Insert Ps Line(s) (default = 1) (IL).
1983 this.insertLines(this.params
);
1987 // Delete Ps Line(s) (default = 1) (DL).
1989 this.deleteLines(this.params
);
1993 // Delete Ps Character(s) (default = 1) (DCH).
1995 this.deleteChars(this.params
);
1999 // Erase Ps Character(s) (default = 1) (ECH).
2001 this.eraseChars(this.params
);
2004 // CSI Pm ` Character Position Absolute
2005 // [column] (default = [row,1]) (HPA).
2007 this.charPosAbsolute(this.params
);
2011 // Horizontal Position Relative
2013 this.HPositionRelative(this.params
);
2017 // Send Device Attributes (Primary DA).
2019 // Send Device Attributes (Secondary DA)
2021 this.sendDeviceAttributes(this.params
);
2025 // Line Position Absolute [row] (default = [1,column]) (VPA).
2027 this.linePosAbsolute(this.params
);
2030 // 145 65 e * VPR - Vertical Position Relative
2032 this.VPositionRelative(this.params
);
2036 // Horizontal and Vertical Position [row;column] (default =
2039 this.HVPosition(this.params
);
2042 // CSI Pm h Set Mode (SM).
2043 // CSI ? Pm h - mouse escape codes, cursor escape codes
2045 this.setMode(this.params
);
2048 // CSI Pm l Reset Mode (RM).
2051 this.resetMode(this.params
);
2055 // Set Scrolling Region [top;bottom] (default = full size of win-
2059 this.setScrollRegion(this.params
);
2063 // Save cursor (ANSI.SYS).
2065 this.saveCursor(this.params
);
2069 // Restore cursor (ANSI.SYS).
2071 this.restoreCursor(this.params
);
2079 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
2081 this.cursorForwardTab(this.params
);
2084 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
2086 this.scrollUp(this.params
);
2089 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
2090 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
2093 // if (this.prefix === '>') {
2094 // this.resetTitleModes(this.params);
2097 // if (this.params.length > 2) {
2098 // this.initMouseTracking(this.params);
2101 if (this.params
.length
< 2 && !this.prefix
) {
2102 this.scrollDown(this.params
);
2107 // Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
2109 this.cursorBackwardTab(this.params
);
2112 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
2114 this.repeatPrecedingCharacter(this.params
);
2117 // CSI Ps g Tab Clear (TBC).
2119 this.tabClear(this.params
);
2122 // CSI Pm i Media Copy (MC).
2125 // this.mediaCopy(this.params);
2128 // CSI Pm m Character Attributes (SGR).
2130 // case 'm': // duplicate
2131 // if (this.prefix === '>') {
2132 // this.setResources(this.params);
2134 // this.charAttributes(this.params);
2138 // CSI Ps n Device Status Report (DSR).
2140 // case 'n': // duplicate
2141 // if (this.prefix === '>') {
2142 // this.disableModifiers(this.params);
2144 // this.deviceStatus(this.params);
2148 // CSI > Ps p Set pointer mode.
2149 // CSI ! p Soft terminal reset (DECSTR).
2151 // Request ANSI mode (DECRQM).
2153 // Request DEC private mode (DECRQM).
2156 switch (this.prefix
) {
2158 // this.setPointerMode(this.params);
2161 this.softReset(this.params
);
2164 // if (this.postfix === '$') {
2165 // this.requestPrivateMode(this.params);
2169 // if (this.postfix === '"') {
2170 // this.setConformanceLevel(this.params);
2171 // } else if (this.postfix === '$') {
2172 // this.requestAnsiMode(this.params);
2178 // CSI Ps q Load LEDs (DECLL).
2182 // if (this.postfix === ' ') {
2183 // this.setCursorStyle(this.params);
2186 // if (this.postfix === '"') {
2187 // this.setCharProtectionAttr(this.params);
2190 // this.loadLEDs(this.params);
2194 // Set Scrolling Region [top;bottom] (default = full size of win-
2197 // CSI Pt; Pl; Pb; Pr; Ps$ r
2198 // case 'r': // duplicate
2199 // if (this.prefix === '?') {
2200 // this.restorePrivateValues(this.params);
2201 // } else if (this.postfix === '$') {
2202 // this.setAttrInRectangle(this.params);
2204 // this.setScrollRegion(this.params);
2208 // CSI s Save cursor (ANSI.SYS).
2210 // case 's': // duplicate
2211 // if (this.prefix === '?') {
2212 // this.savePrivateValues(this.params);
2214 // this.saveCursor(this.params);
2218 // CSI Ps ; Ps ; Ps t
2219 // CSI Pt; Pl; Pb; Pr; Ps$ t
2223 // if (this.postfix === '$') {
2224 // this.reverseAttrInRectangle(this.params);
2225 // } else if (this.postfix === ' ') {
2226 // this.setWarningBellVolume(this.params);
2228 // if (this.prefix === '>') {
2229 // this.setTitleModeFeature(this.params);
2231 // this.manipulateWindow(this.params);
2236 // CSI u Restore cursor (ANSI.SYS).
2238 // case 'u': // duplicate
2239 // if (this.postfix === ' ') {
2240 // this.setMarginBellVolume(this.params);
2242 // this.restoreCursor(this.params);
2246 // CSI Pt; Pl; Pb; Pr; Pp; Pt; Pl; Pp$ v
2248 // if (this.postfix === '$') {
2249 // this.copyRectagle(this.params);
2253 // CSI Pt ; Pl ; Pb ; Pr ' w
2255 // if (this.postfix === '\'') {
2256 // this.enableFilterRectangle(this.params);
2260 // CSI Ps x Request Terminal Parameters (DECREQTPARM).
2261 // CSI Ps x Select Attribute Change Extent (DECSACE).
2262 // CSI Pc; Pt; Pl; Pb; Pr$ x
2264 // if (this.postfix === '$') {
2265 // this.fillRectangle(this.params);
2267 // this.requestParameters(this.params);
2268 // //this.__(this.params);
2273 // CSI Pt; Pl; Pb; Pr$ z
2275 // if (this.postfix === '\'') {
2276 // this.enableLocatorReporting(this.params);
2277 // } else if (this.postfix === '$') {
2278 // this.eraseRectangle(this.params);
2283 // CSI Pt; Pl; Pb; Pr$ {
2285 // if (this.postfix === '\'') {
2286 // this.setLocatorEvents(this.params);
2287 // } else if (this.postfix === '$') {
2288 // this.selectiveEraseRectangle(this.params);
2294 // if (this.postfix === '\'') {
2295 // this.requestLocatorPosition(this.params);
2300 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
2302 // if (this.postfix === ' ') {
2303 // this.insertColumns(this.params);
2308 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
2310 // if (this.postfix === ' ') {
2311 // this.deleteColumns(this.params);
2316 this.error('Unknown CSI code: %s.', ch
);
2325 if (ch
=== '\x1b' || ch
=== '\x07') {
2326 if (ch
=== '\x1b') i
++;
2328 switch (this.prefix
) {
2329 // User-Defined Keys (DECUDK).
2333 // Request Status String (DECRQSS).
2334 // test: echo -e '\eP$q"p\e\\'
2336 var pt
= this.currentParam
2353 + (this.scrollTop
+ 1)
2355 + (this.scrollBottom
+ 1)
2365 this.error('Unknown DCS Pt: %s.', pt
);
2370 this.send('\x1bP' + +valid
+ '$r' + pt
+ '\x1b\\');
2373 // Set Termcap/Terminfo Data (xterm, experimental).
2377 // Request Termcap/Terminfo String (xterm, experimental)
2378 // Regular xterm does not even respond to this sequence.
2379 // This can cause a small glitch in vim.
2380 // test: echo -ne '\eP+q6b64\e\\'
2382 var pt
= this.currentParam
2385 this.send('\x1bP' + +valid
+ '+r' + pt
+ '\x1b\\');
2389 this.error('Unknown DCS prefix: %s.', this.prefix
);
2393 this.currentParam
= 0;
2395 this.state
= normal
;
2396 } else if (!this.currentParam
) {
2397 if (!this.prefix
&& ch
!== '$' && ch
!== '+') {
2398 this.currentParam
= ch
;
2399 } else if (this.prefix
.length
=== 2) {
2400 this.currentParam
= ch
;
2405 this.currentParam
+= ch
;
2411 if (ch
=== '\x1b' || ch
=== '\x07') {
2412 if (ch
=== '\x1b') i
++;
2413 this.state
= normal
;
2419 this.updateRange(this.y
);
2420 this.refresh(this.refreshStart
, this.refreshEnd
);
2423 Terminal
.prototype.writeln = function(data
) {
2424 this.write(data
+ '\r\n');
2428 // https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
2429 Terminal
.prototype.keyDown = function(ev
) {
2431 var result
= this.evaluateKeyEscapeSequence(ev
);
2433 if (result
.scrollDisp
) {
2434 this.scrollDisp(result
.scrollDisp
);
2435 return this.cancel(ev
);
2438 if (isThirdLevelShift(this, ev
)) {
2442 if (result
.cancel
) {
2443 // The event is canceled at the end already, is this necessary?
2444 this.cancel(ev
, true);
2451 this.emit('keydown', ev
);
2452 this.emit('key', result
.key
, ev
);
2454 this.handler(result
.key
);
2456 return this.cancel(ev
, true);
2460 * Returns an object that determines how a KeyboardEvent should be handled. The key of the
2461 * returned value is the new key code to pass to the PTY.
2463 * Reference: http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
2465 Terminal
.prototype.evaluateKeyEscapeSequence = function(ev
) {
2467 // Whether to cancel event propogation (NOTE: this may not be needed since the event is
2468 // canceled at the end of keyDown
2470 // The new key even to emit
2472 // The number of characters to scroll, if this is defined it will cancel the event
2473 scrollDisp
: undefined
2475 switch (ev
.keyCode
) {
2479 result
.key
= '\x08'; // ^H
2482 result
.key
= '\x7f'; // ^?
2487 result
.key
= '\x1b[Z';
2491 result
.cancel
= true;
2496 result
.cancel
= true;
2500 result
.key
= '\x1b';
2501 result
.cancel
= true;
2506 result
.key
= '\x1bb' // Jump a word back
2507 result
.cancel
= true;
2511 result
.key
= '\x1b[5D'; // Jump a word back
2514 if (this.applicationCursor
) {
2515 result
.key
= '\x1bOD'; // SS3 as ^[O for 7-bit
2518 result
.key
= '\x1b[D';
2523 result
.key
= '\x1bf' // Jump a word forward
2524 result
.cancel
= true;
2528 result
.key
= '\x1b[5C'; // Jump a word forward
2531 if (this.applicationCursor
) {
2532 result
.key
= '\x1bOC';
2535 result
.key
= '\x1b[C';
2539 if (this.applicationCursor
) {
2540 result
.key
= '\x1bOA';
2544 result
.scrollDisp
= -1;
2546 result
.key
= '\x1b[A';
2551 if (this.applicationCursor
) {
2552 result
.key
= '\x1bOB';
2556 result
.scrollDisp
= 1;
2558 result
.key
= '\x1b[B';
2563 if (!ev
.shiftKey
&& !ev
.ctrlKey
) {
2564 // <Ctrl> or <Shift> + <Insert> are used to
2565 // copy-paste on some systems.
2566 result
.key
= '\x1b[2~';
2570 case 46: result
.key
= '\x1b[3~'; break;
2573 if (this.applicationKeypad
) {
2574 result
.key
= '\x1bOH';
2577 result
.key
= '\x1bOH';
2581 if (this.applicationKeypad
) {
2582 result
.key
= '\x1bOF';
2585 result
.key
= '\x1bOF';
2590 result
.scrollDisp
= -(this.rows
- 1);
2592 result
.key
= '\x1b[5~';
2598 result
.scrollDisp
= this.rows
- 1;
2600 result
.key
= '\x1b[6~';
2604 case 112: result
.key
= '\x1bOP'; break;
2605 case 113: result
.key
= '\x1bOQ'; break;
2606 case 114: result
.key
= '\x1bOR'; break;
2607 case 115: result
.key
= '\x1bOS'; break;
2608 case 116: result
.key
= '\x1b[15~'; break;
2609 case 117: result
.key
= '\x1b[17~'; break;
2610 case 118: result
.key
= '\x1b[18~'; break;
2611 case 119: result
.key
= '\x1b[19~'; break;
2612 case 120: result
.key
= '\x1b[20~'; break;
2613 case 121: result
.key
= '\x1b[21~'; break;
2614 case 122: result
.key
= '\x1b[23~'; break;
2615 case 123: result
.key
= '\x1b[24~'; break;
2618 if (ev
.ctrlKey
&& !ev
.shiftKey
&& !ev
.altKey
&& !ev
.metaKey
) {
2619 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2620 result
.key
= String
.fromCharCode(ev
.keyCode
- 64);
2621 } else if (ev
.keyCode
=== 32) {
2623 result
.key
= String
.fromCharCode(0);
2624 } else if (ev
.keyCode
>= 51 && ev
.keyCode
<= 55) {
2625 // escape, file sep, group sep, record sep, unit sep
2626 result
.key
= String
.fromCharCode(ev
.keyCode
- 51 + 27);
2627 } else if (ev
.keyCode
=== 56) {
2629 result
.key
= String
.fromCharCode(127);
2630 } else if (ev
.keyCode
=== 219) {
2632 result
.key
= String
.fromCharCode(27);
2633 } else if (ev
.keyCode
=== 221) {
2635 result
.key
= String
.fromCharCode(29);
2637 } else if (!this.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) {
2638 // On Mac this is a third level shift. Use <Esc> instead.
2639 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2640 result
.key
= '\x1b' + String
.fromCharCode(ev
.keyCode
+ 32);
2641 } else if (ev
.keyCode
=== 192) {
2642 result
.key
= '\x1b`';
2643 } else if (ev
.keyCode
>= 48 && ev
.keyCode
<= 57) {
2644 result
.key
= '\x1b' + (ev
.keyCode
- 48);
2652 Terminal
.prototype.setgLevel = function(g
) {
2654 this.charset
= this.charsets
[g
];
2657 Terminal
.prototype.setgCharset = function(g
, charset
) {
2658 this.charsets
[g
] = charset
;
2659 if (this.glevel
=== g
) {
2660 this.charset
= charset
;
2664 Terminal
.prototype.keyPress = function(ev
) {
2669 } else if (ev
.which
== null) {
2671 } else if (ev
.which
!== 0 && ev
.charCode
!== 0) {
2678 (ev
.altKey
|| ev
.ctrlKey
|| ev
.metaKey
) && !isThirdLevelShift(this, ev
)
2683 key
= String
.fromCharCode(key
);
2686 * When a key is pressed and a character is sent to the terminal, then clear any text
2687 * selected in the terminal.
2690 this.clearSelection();
2693 this.emit('keypress', key
, ev
);
2694 this.emit('key', key
, ev
);
2698 this.cancel(ev
, true);
2703 Terminal
.prototype.send = function(data
) {
2707 setTimeout(function() {
2708 self
.handler(self
.queue
);
2716 Terminal
.prototype.bell = function() {
2717 if (!this.visualBell
) return;
2719 this.element
.style
.borderColor
= 'white';
2720 setTimeout(function() {
2721 self
.element
.style
.borderColor
= '';
2723 if (this.popOnBell
) this.focus();
2726 Terminal
.prototype.log = function() {
2727 if (!this.debug
) return;
2728 if (!this.context
.console
|| !this.context
.console
.log
) return;
2729 var args
= Array
.prototype.slice
.call(arguments
);
2730 this.context
.console
.log
.apply(this.context
.console
, args
);
2733 Terminal
.prototype.error = function() {
2734 if (!this.debug
) return;
2735 if (!this.context
.console
|| !this.context
.console
.error
) return;
2736 var args
= Array
.prototype.slice
.call(arguments
);
2737 this.context
.console
.error
.apply(this.context
.console
, args
);
2741 * Resizes the terminal.
2743 * @param {number} x The number of columns to resize to.
2744 * @param {number} y The number of rows to resize to.
2748 Terminal
.prototype.resize = function(x
, y
) {
2756 if (x
=== this.cols
&& y
=== this.rows
) {
2766 ch
= [this.defAttr
, ' ']; // does xterm use the default attr?
2767 i
= this.lines
.length
;
2769 while (this.lines
[i
].length
< x
) {
2770 this.lines
[i
].push(ch
);
2774 i
= this.lines
.length
;
2776 while (this.lines
[i
].length
> x
) {
2777 this.lines
[i
].pop();
2790 // y is rows, not this.y
2791 if (this.lines
.length
< y
+ this.ybase
) {
2792 if (this.ybase
> 0 && this.lines
.length
<= this.ybase
+ this.y
+ addToY
+ 1) {
2793 // There is room above the buffer and there are no empty elements below the line,
2799 // Add a blank line if there is no buffer left at the top to scroll to, or if there
2800 // are blank lines after the cursor
2801 this.lines
.push(this.blankLine());
2804 if (this.children
.length
< y
) {
2810 if (this.lines
.length
> y
+ this.ybase
) {
2811 if (this.lines
.length
> this.ybase
+ this.y
+ 1) {
2812 // The line is a blank line below the cursor, remove it
2815 // The line is the cursor, scroll down
2820 if (this.children
.length
> y
) {
2821 el
= this.children
.shift();
2823 el
.parentNode
.removeChild(el
);
2830 * Make sure that the cursor stays on screen
2844 this.scrollBottom
= y
- 1;
2846 this.refresh(0, this.rows
- 1);
2850 this.emit('resize', {terminal
: this, cols
: x
, rows
: y
});
2853 Terminal
.prototype.updateRange = function(y
) {
2854 if (y
< this.refreshStart
) this.refreshStart
= y
;
2855 if (y
> this.refreshEnd
) this.refreshEnd
= y
;
2856 // if (y > this.refreshEnd) {
2857 // this.refreshEnd = y;
2858 // if (y > this.rows - 1) {
2859 // this.refreshEnd = this.rows - 1;
2864 Terminal
.prototype.maxRange = function() {
2865 this.refreshStart
= 0;
2866 this.refreshEnd
= this.rows
- 1;
2869 Terminal
.prototype.setupStops = function(i
) {
2871 if (!this.tabs
[i
]) {
2872 i
= this.prevStop(i
);
2879 for (; i
< this.cols
; i
+= 8) {
2880 this.tabs
[i
] = true;
2884 Terminal
.prototype.prevStop = function(x
) {
2885 if (x
== null) x
= this.x
;
2886 while (!this.tabs
[--x
] && x
> 0);
2887 return x
>= this.cols
2892 Terminal
.prototype.nextStop = function(x
) {
2893 if (x
== null) x
= this.x
;
2894 while (!this.tabs
[++x
] && x
< this.cols
);
2895 return x
>= this.cols
2900 Terminal
.prototype.eraseRight = function(x
, y
) {
2901 var line
= this.lines
[this.ybase
+ y
]
2902 , ch
= [this.eraseAttr(), ' ']; // xterm
2905 for (; x
< this.cols
; x
++) {
2909 this.updateRange(y
);
2912 Terminal
.prototype.eraseLeft = function(x
, y
) {
2913 var line
= this.lines
[this.ybase
+ y
]
2914 , ch
= [this.eraseAttr(), ' ']; // xterm
2917 while (x
--) line
[x
] = ch
;
2919 this.updateRange(y
);
2922 Terminal
.prototype.eraseLine = function(y
) {
2923 this.eraseRight(0, y
);
2926 Terminal
.prototype.blankLine = function(cur
) {
2931 var ch
= [attr
, ' ']
2935 for (; i
< this.cols
; i
++) {
2942 Terminal
.prototype.ch = function(cur
) {
2944 ? [this.eraseAttr(), ' ']
2945 : [this.defAttr
, ' '];
2948 Terminal
.prototype.is = function(term
) {
2949 var name
= this.termName
;
2950 return (name
+ '').indexOf(term
) === 0;
2953 Terminal
.prototype.handler = function(data
) {
2954 this.emit('data', data
);
2957 Terminal
.prototype.handleTitle = function(title
) {
2958 this.emit('title', title
);
2965 // ESC D Index (IND is 0x84).
2966 Terminal
.prototype.index = function() {
2968 if (this.y
> this.scrollBottom
) {
2972 this.state
= normal
;
2975 // ESC M Reverse Index (RI is 0x8d).
2976 Terminal
.prototype.reverseIndex = function() {
2979 if (this.y
< this.scrollTop
) {
2981 // possibly move the code below to term.reverseScroll();
2982 // test: echo -ne '\e[1;1H\e[44m\eM\e[0m'
2983 // blankLine(true) is xterm/linux behavior
2984 this.lines
.splice(this.y
+ this.ybase
, 0, this.blankLine(true));
2985 j
= this.rows
- 1 - this.scrollBottom
;
2986 this.lines
.splice(this.rows
- 1 + this.ybase
- j
+ 1, 1);
2988 this.updateRange(this.scrollTop
);
2989 this.updateRange(this.scrollBottom
);
2991 this.state
= normal
;
2994 // ESC c Full Reset (RIS).
2995 Terminal
.prototype.reset = function() {
2996 this.options
.rows
= this.rows
;
2997 this.options
.cols
= this.cols
;
2998 Terminal
.call(this, this.options
);
2999 this.refresh(0, this.rows
- 1);
3002 // ESC H Tab Set (HTS is 0x88).
3003 Terminal
.prototype.tabSet = function() {
3004 this.tabs
[this.x
] = true;
3005 this.state
= normal
;
3013 // Cursor Up Ps Times (default = 1) (CUU).
3014 Terminal
.prototype.cursorUp = function(params
) {
3015 var param
= params
[0];
3016 if (param
< 1) param
= 1;
3018 if (this.y
< 0) this.y
= 0;
3022 // Cursor Down Ps Times (default = 1) (CUD).
3023 Terminal
.prototype.cursorDown = function(params
) {
3024 var param
= params
[0];
3025 if (param
< 1) param
= 1;
3027 if (this.y
>= this.rows
) {
3028 this.y
= this.rows
- 1;
3033 // Cursor Forward Ps Times (default = 1) (CUF).
3034 Terminal
.prototype.cursorForward = function(params
) {
3035 var param
= params
[0];
3036 if (param
< 1) param
= 1;
3038 if (this.x
>= this.cols
) {
3039 this.x
= this.cols
- 1;
3044 // Cursor Backward Ps Times (default = 1) (CUB).
3045 Terminal
.prototype.cursorBackward = function(params
) {
3046 var param
= params
[0];
3047 if (param
< 1) param
= 1;
3049 if (this.x
< 0) this.x
= 0;
3053 // Cursor Position [row;column] (default = [1,1]) (CUP).
3054 Terminal
.prototype.cursorPos = function(params
) {
3057 row
= params
[0] - 1;
3059 if (params
.length
>= 2) {
3060 col
= params
[1] - 1;
3067 } else if (row
>= this.rows
) {
3068 row
= this.rows
- 1;
3073 } else if (col
>= this.cols
) {
3074 col
= this.cols
- 1;
3081 // CSI Ps J Erase in Display (ED).
3082 // Ps = 0 -> Erase Below (default).
3083 // Ps = 1 -> Erase Above.
3084 // Ps = 2 -> Erase All.
3085 // Ps = 3 -> Erase Saved Lines (xterm).
3087 // Erase in Display (DECSED).
3088 // Ps = 0 -> Selective Erase Below (default).
3089 // Ps = 1 -> Selective Erase Above.
3090 // Ps = 2 -> Selective Erase All.
3091 Terminal
.prototype.eraseInDisplay = function(params
) {
3093 switch (params
[0]) {
3095 this.eraseRight(this.x
, this.y
);
3097 for (; j
< this.rows
; j
++) {
3102 this.eraseLeft(this.x
, this.y
);
3110 while (j
--) this.eraseLine(j
);
3118 // CSI Ps K Erase in Line (EL).
3119 // Ps = 0 -> Erase to Right (default).
3120 // Ps = 1 -> Erase to Left.
3121 // Ps = 2 -> Erase All.
3123 // Erase in Line (DECSEL).
3124 // Ps = 0 -> Selective Erase to Right (default).
3125 // Ps = 1 -> Selective Erase to Left.
3126 // Ps = 2 -> Selective Erase All.
3127 Terminal
.prototype.eraseInLine = function(params
) {
3128 switch (params
[0]) {
3130 this.eraseRight(this.x
, this.y
);
3133 this.eraseLeft(this.x
, this.y
);
3136 this.eraseLine(this.y
);
3141 // CSI Pm m Character Attributes (SGR).
3142 // Ps = 0 -> Normal (default).
3144 // Ps = 4 -> Underlined.
3145 // Ps = 5 -> Blink (appears as Bold).
3146 // Ps = 7 -> Inverse.
3147 // Ps = 8 -> Invisible, i.e., hidden (VT300).
3148 // Ps = 2 2 -> Normal (neither bold nor faint).
3149 // Ps = 2 4 -> Not underlined.
3150 // Ps = 2 5 -> Steady (not blinking).
3151 // Ps = 2 7 -> Positive (not inverse).
3152 // Ps = 2 8 -> Visible, i.e., not hidden (VT300).
3153 // Ps = 3 0 -> Set foreground color to Black.
3154 // Ps = 3 1 -> Set foreground color to Red.
3155 // Ps = 3 2 -> Set foreground color to Green.
3156 // Ps = 3 3 -> Set foreground color to Yellow.
3157 // Ps = 3 4 -> Set foreground color to Blue.
3158 // Ps = 3 5 -> Set foreground color to Magenta.
3159 // Ps = 3 6 -> Set foreground color to Cyan.
3160 // Ps = 3 7 -> Set foreground color to White.
3161 // Ps = 3 9 -> Set foreground color to default (original).
3162 // Ps = 4 0 -> Set background color to Black.
3163 // Ps = 4 1 -> Set background color to Red.
3164 // Ps = 4 2 -> Set background color to Green.
3165 // Ps = 4 3 -> Set background color to Yellow.
3166 // Ps = 4 4 -> Set background color to Blue.
3167 // Ps = 4 5 -> Set background color to Magenta.
3168 // Ps = 4 6 -> Set background color to Cyan.
3169 // Ps = 4 7 -> Set background color to White.
3170 // Ps = 4 9 -> Set background color to default (original).
3172 // If 16-color support is compiled, the following apply. Assume
3173 // that xterm's resources are set so that the ISO color codes are
3174 // the first 8 of a set of 16. Then the aixterm colors are the
3175 // bright versions of the ISO colors:
3176 // Ps = 9 0 -> Set foreground color to Black.
3177 // Ps = 9 1 -> Set foreground color to Red.
3178 // Ps = 9 2 -> Set foreground color to Green.
3179 // Ps = 9 3 -> Set foreground color to Yellow.
3180 // Ps = 9 4 -> Set foreground color to Blue.
3181 // Ps = 9 5 -> Set foreground color to Magenta.
3182 // Ps = 9 6 -> Set foreground color to Cyan.
3183 // Ps = 9 7 -> Set foreground color to White.
3184 // Ps = 1 0 0 -> Set background color to Black.
3185 // Ps = 1 0 1 -> Set background color to Red.
3186 // Ps = 1 0 2 -> Set background color to Green.
3187 // Ps = 1 0 3 -> Set background color to Yellow.
3188 // Ps = 1 0 4 -> Set background color to Blue.
3189 // Ps = 1 0 5 -> Set background color to Magenta.
3190 // Ps = 1 0 6 -> Set background color to Cyan.
3191 // Ps = 1 0 7 -> Set background color to White.
3193 // If xterm is compiled with the 16-color support disabled, it
3194 // supports the following, from rxvt:
3195 // Ps = 1 0 0 -> Set foreground and background color to
3198 // If 88- or 256-color support is compiled, the following apply.
3199 // Ps = 3 8 ; 5 ; Ps -> Set foreground color to the second
3201 // Ps = 4 8 ; 5 ; Ps -> Set background color to the second
3203 Terminal
.prototype.charAttributes = function(params
) {
3204 // Optimize a single SGR0.
3205 if (params
.length
=== 1 && params
[0] === 0) {
3206 this.curAttr
= this.defAttr
;
3210 var l
= params
.length
3212 , flags
= this.curAttr
>> 18
3213 , fg
= (this.curAttr
>> 9) & 0x1ff
3214 , bg
= this.curAttr
& 0x1ff
3217 for (; i
< l
; i
++) {
3219 if (p
>= 30 && p
<= 37) {
3222 } else if (p
>= 40 && p
<= 47) {
3225 } else if (p
>= 90 && p
<= 97) {
3229 } else if (p
>= 100 && p
<= 107) {
3233 } else if (p
=== 0) {
3235 flags
= this.defAttr
>> 18;
3236 fg
= (this.defAttr
>> 9) & 0x1ff;
3237 bg
= this.defAttr
& 0x1ff;
3241 } else if (p
=== 1) {
3244 } else if (p
=== 4) {
3247 } else if (p
=== 5) {
3250 } else if (p
=== 7) {
3251 // inverse and positive
3252 // test with: echo -e '\e[31m\e[42mhello\e[7mworld\e[27mhi\e[m'
3254 } else if (p
=== 8) {
3257 } else if (p
=== 22) {
3260 } else if (p
=== 24) {
3263 } else if (p
=== 25) {
3266 } else if (p
=== 27) {
3269 } else if (p
=== 28) {
3272 } else if (p
=== 39) {
3274 fg
= (this.defAttr
>> 9) & 0x1ff;
3275 } else if (p
=== 49) {
3277 bg
= this.defAttr
& 0x1ff;
3278 } else if (p
=== 38) {
3280 if (params
[i
+ 1] === 2) {
3284 params
[i
+ 1] & 0xff,
3285 params
[i
+ 2] & 0xff);
3286 if (fg
=== -1) fg
= 0x1ff;
3288 } else if (params
[i
+ 1] === 5) {
3290 p
= params
[i
] & 0xff;
3293 } else if (p
=== 48) {
3295 if (params
[i
+ 1] === 2) {
3299 params
[i
+ 1] & 0xff,
3300 params
[i
+ 2] & 0xff);
3301 if (bg
=== -1) bg
= 0x1ff;
3303 } else if (params
[i
+ 1] === 5) {
3305 p
= params
[i
] & 0xff;
3308 } else if (p
=== 100) {
3310 fg
= (this.defAttr
>> 9) & 0x1ff;
3311 bg
= this.defAttr
& 0x1ff;
3313 this.error('Unknown SGR attribute: %d.', p
);
3317 this.curAttr
= (flags
<< 18) | (fg
<< 9) | bg
;
3320 // CSI Ps n Device Status Report (DSR).
3321 // Ps = 5 -> Status Report. Result (``OK'') is
3323 // Ps = 6 -> Report Cursor Position (CPR) [row;column].
3327 // Device Status Report (DSR, DEC-specific).
3328 // Ps = 6 -> Report Cursor Position (CPR) [row;column] as CSI
3329 // ? r ; c R (assumes page is zero).
3330 // Ps = 1 5 -> Report Printer status as CSI ? 1 0 n (ready).
3331 // or CSI ? 1 1 n (not ready).
3332 // Ps = 2 5 -> Report UDK status as CSI ? 2 0 n (unlocked)
3333 // or CSI ? 2 1 n (locked).
3334 // Ps = 2 6 -> Report Keyboard status as
3335 // CSI ? 2 7 ; 1 ; 0 ; 0 n (North American).
3336 // The last two parameters apply to VT400 & up, and denote key-
3337 // board ready and LK01 respectively.
3338 // Ps = 5 3 -> Report Locator status as
3339 // CSI ? 5 3 n Locator available, if compiled-in, or
3340 // CSI ? 5 0 n No Locator, if not.
3341 Terminal
.prototype.deviceStatus = function(params
) {
3343 switch (params
[0]) {
3346 this.send('\x1b[0n');
3357 } else if (this.prefix
=== '?') {
3358 // modern xterm doesnt seem to
3359 // respond to any of these except ?6, 6, and 5
3360 switch (params
[0]) {
3371 // this.send('\x1b[?11n');
3374 // dont support user defined keys
3375 // this.send('\x1b[?21n');
3378 // north american keyboard
3379 // this.send('\x1b[?27;1;0;0n');
3382 // no dec locator/mouse
3383 // this.send('\x1b[?50n');
3394 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
3395 Terminal
.prototype.insertChars = function(params
) {
3396 var param
, row
, j
, ch
;
3399 if (param
< 1) param
= 1;
3401 row
= this.y
+ this.ybase
;
3403 ch
= [this.eraseAttr(), ' ']; // xterm
3405 while (param
-- && j
< this.cols
) {
3406 this.lines
[row
].splice(j
++, 0, ch
);
3407 this.lines
[row
].pop();
3412 // Cursor Next Line Ps Times (default = 1) (CNL).
3413 // same as CSI Ps B ?
3414 Terminal
.prototype.cursorNextLine = function(params
) {
3415 var param
= params
[0];
3416 if (param
< 1) param
= 1;
3418 if (this.y
>= this.rows
) {
3419 this.y
= this.rows
- 1;
3425 // Cursor Preceding Line Ps Times (default = 1) (CNL).
3427 Terminal
.prototype.cursorPrecedingLine = function(params
) {
3428 var param
= params
[0];
3429 if (param
< 1) param
= 1;
3431 if (this.y
< 0) this.y
= 0;
3436 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
3437 Terminal
.prototype.cursorCharAbsolute = function(params
) {
3438 var param
= params
[0];
3439 if (param
< 1) param
= 1;
3444 // Insert Ps Line(s) (default = 1) (IL).
3445 Terminal
.prototype.insertLines = function(params
) {
3449 if (param
< 1) param
= 1;
3450 row
= this.y
+ this.ybase
;
3452 j
= this.rows
- 1 - this.scrollBottom
;
3453 j
= this.rows
- 1 + this.ybase
- j
+ 1;
3456 // test: echo -e '\e[44m\e[1L\e[0m'
3457 // blankLine(true) - xterm/linux behavior
3458 this.lines
.splice(row
, 0, this.blankLine(true));
3459 this.lines
.splice(j
, 1);
3463 this.updateRange(this.y
);
3464 this.updateRange(this.scrollBottom
);
3468 // Delete Ps Line(s) (default = 1) (DL).
3469 Terminal
.prototype.deleteLines = function(params
) {
3473 if (param
< 1) param
= 1;
3474 row
= this.y
+ this.ybase
;
3476 j
= this.rows
- 1 - this.scrollBottom
;
3477 j
= this.rows
- 1 + this.ybase
- j
;
3480 // test: echo -e '\e[44m\e[1M\e[0m'
3481 // blankLine(true) - xterm/linux behavior
3482 this.lines
.splice(j
+ 1, 0, this.blankLine(true));
3483 this.lines
.splice(row
, 1);
3487 this.updateRange(this.y
);
3488 this.updateRange(this.scrollBottom
);
3492 // Delete Ps Character(s) (default = 1) (DCH).
3493 Terminal
.prototype.deleteChars = function(params
) {
3497 if (param
< 1) param
= 1;
3499 row
= this.y
+ this.ybase
;
3500 ch
= [this.eraseAttr(), ' ']; // xterm
3503 this.lines
[row
].splice(this.x
, 1);
3504 this.lines
[row
].push(ch
);
3509 // Erase Ps Character(s) (default = 1) (ECH).
3510 Terminal
.prototype.eraseChars = function(params
) {
3511 var param
, row
, j
, ch
;
3514 if (param
< 1) param
= 1;
3516 row
= this.y
+ this.ybase
;
3518 ch
= [this.eraseAttr(), ' ']; // xterm
3520 while (param
-- && j
< this.cols
) {
3521 this.lines
[row
][j
++] = ch
;
3525 // CSI Pm ` Character Position Absolute
3526 // [column] (default = [row,1]) (HPA).
3527 Terminal
.prototype.charPosAbsolute = function(params
) {
3528 var param
= params
[0];
3529 if (param
< 1) param
= 1;
3531 if (this.x
>= this.cols
) {
3532 this.x
= this.cols
- 1;
3537 // Horizontal Position Relative
3539 Terminal
.prototype.HPositionRelative = function(params
) {
3540 var param
= params
[0];
3541 if (param
< 1) param
= 1;
3543 if (this.x
>= this.cols
) {
3544 this.x
= this.cols
- 1;
3548 // CSI Ps c Send Device Attributes (Primary DA).
3549 // Ps = 0 or omitted -> request attributes from terminal. The
3550 // response depends on the decTerminalID resource setting.
3551 // -> CSI ? 1 ; 2 c (``VT100 with Advanced Video Option'')
3552 // -> CSI ? 1 ; 0 c (``VT101 with No Options'')
3553 // -> CSI ? 6 c (``VT102'')
3554 // -> CSI ? 6 0 ; 1 ; 2 ; 6 ; 8 ; 9 ; 1 5 ; c (``VT220'')
3555 // The VT100-style response parameters do not mean anything by
3556 // themselves. VT220 parameters do, telling the host what fea-
3557 // tures the terminal supports:
3558 // Ps = 1 -> 132-columns.
3559 // Ps = 2 -> Printer.
3560 // Ps = 6 -> Selective erase.
3561 // Ps = 8 -> User-defined keys.
3562 // Ps = 9 -> National replacement character sets.
3563 // Ps = 1 5 -> Technical characters.
3564 // Ps = 2 2 -> ANSI color, e.g., VT525.
3565 // Ps = 2 9 -> ANSI text locator (i.e., DEC Locator mode).
3567 // Send Device Attributes (Secondary DA).
3568 // Ps = 0 or omitted -> request the terminal's identification
3569 // code. The response depends on the decTerminalID resource set-
3570 // ting. It should apply only to VT220 and up, but xterm extends
3572 // -> CSI > Pp ; Pv ; Pc c
3573 // where Pp denotes the terminal type
3574 // Pp = 0 -> ``VT100''.
3575 // Pp = 1 -> ``VT220''.
3576 // and Pv is the firmware version (for xterm, this was originally
3577 // the XFree86 patch number, starting with 95). In a DEC termi-
3578 // nal, Pc indicates the ROM cartridge registration number and is
3580 // More information:
3581 // xterm/charproc.c - line 2012, for more information.
3582 // vim responds with ^[[?0c or ^[[?1c after the terminal's response (?)
3583 Terminal
.prototype.sendDeviceAttributes = function(params
) {
3584 if (params
[0] > 0) return;
3587 if (this.is('xterm')
3588 || this.is('rxvt-unicode')
3589 || this.is('screen')) {
3590 this.send('\x1b[?1;2c');
3591 } else if (this.is('linux')) {
3592 this.send('\x1b[?6c');
3594 } else if (this.prefix
=== '>') {
3596 // seem to spit this
3597 // out around ~370 times (?).
3598 if (this.is('xterm')) {
3599 this.send('\x1b[>0;276;0c');
3600 } else if (this.is('rxvt-unicode')) {
3601 this.send('\x1b[>85;95;0c');
3602 } else if (this.is('linux')) {
3603 // not supported by linux console.
3604 // linux console echoes parameters.
3605 this.send(params
[0] + 'c');
3606 } else if (this.is('screen')) {
3607 this.send('\x1b[>83;40003;0c');
3613 // Line Position Absolute [row] (default = [1,column]) (VPA).
3614 Terminal
.prototype.linePosAbsolute = function(params
) {
3615 var param
= params
[0];
3616 if (param
< 1) param
= 1;
3618 if (this.y
>= this.rows
) {
3619 this.y
= this.rows
- 1;
3623 // 145 65 e * VPR - Vertical Position Relative
3625 Terminal
.prototype.VPositionRelative = function(params
) {
3626 var param
= params
[0];
3627 if (param
< 1) param
= 1;
3629 if (this.y
>= this.rows
) {
3630 this.y
= this.rows
- 1;
3635 // Horizontal and Vertical Position [row;column] (default =
3637 Terminal
.prototype.HVPosition = function(params
) {
3638 if (params
[0] < 1) params
[0] = 1;
3639 if (params
[1] < 1) params
[1] = 1;
3641 this.y
= params
[0] - 1;
3642 if (this.y
>= this.rows
) {
3643 this.y
= this.rows
- 1;
3646 this.x
= params
[1] - 1;
3647 if (this.x
>= this.cols
) {
3648 this.x
= this.cols
- 1;
3652 // CSI Pm h Set Mode (SM).
3653 // Ps = 2 -> Keyboard Action Mode (AM).
3654 // Ps = 4 -> Insert Mode (IRM).
3655 // Ps = 1 2 -> Send/receive (SRM).
3656 // Ps = 2 0 -> Automatic Newline (LNM).
3658 // DEC Private Mode Set (DECSET).
3659 // Ps = 1 -> Application Cursor Keys (DECCKM).
3660 // Ps = 2 -> Designate USASCII for character sets G0-G3
3661 // (DECANM), and set VT100 mode.
3662 // Ps = 3 -> 132 Column Mode (DECCOLM).
3663 // Ps = 4 -> Smooth (Slow) Scroll (DECSCLM).
3664 // Ps = 5 -> Reverse Video (DECSCNM).
3665 // Ps = 6 -> Origin Mode (DECOM).
3666 // Ps = 7 -> Wraparound Mode (DECAWM).
3667 // Ps = 8 -> Auto-repeat Keys (DECARM).
3668 // Ps = 9 -> Send Mouse X & Y on button press. See the sec-
3669 // tion Mouse Tracking.
3670 // Ps = 1 0 -> Show toolbar (rxvt).
3671 // Ps = 1 2 -> Start Blinking Cursor (att610).
3672 // Ps = 1 8 -> Print form feed (DECPFF).
3673 // Ps = 1 9 -> Set print extent to full screen (DECPEX).
3674 // Ps = 2 5 -> Show Cursor (DECTCEM).
3675 // Ps = 3 0 -> Show scrollbar (rxvt).
3676 // Ps = 3 5 -> Enable font-shifting functions (rxvt).
3677 // Ps = 3 8 -> Enter Tektronix Mode (DECTEK).
3678 // Ps = 4 0 -> Allow 80 -> 132 Mode.
3679 // Ps = 4 1 -> more(1) fix (see curses resource).
3680 // Ps = 4 2 -> Enable Nation Replacement Character sets (DECN-
3682 // Ps = 4 4 -> Turn On Margin Bell.
3683 // Ps = 4 5 -> Reverse-wraparound Mode.
3684 // Ps = 4 6 -> Start Logging. This is normally disabled by a
3685 // compile-time option.
3686 // Ps = 4 7 -> Use Alternate Screen Buffer. (This may be dis-
3687 // abled by the titeInhibit resource).
3688 // Ps = 6 6 -> Application keypad (DECNKM).
3689 // Ps = 6 7 -> Backarrow key sends backspace (DECBKM).
3690 // Ps = 1 0 0 0 -> Send Mouse X & Y on button press and
3691 // release. See the section Mouse Tracking.
3692 // Ps = 1 0 0 1 -> Use Hilite Mouse Tracking.
3693 // Ps = 1 0 0 2 -> Use Cell Motion Mouse Tracking.
3694 // Ps = 1 0 0 3 -> Use All Motion Mouse Tracking.
3695 // Ps = 1 0 0 4 -> Send FocusIn/FocusOut events.
3696 // Ps = 1 0 0 5 -> Enable Extended Mouse Mode.
3697 // Ps = 1 0 1 0 -> Scroll to bottom on tty output (rxvt).
3698 // Ps = 1 0 1 1 -> Scroll to bottom on key press (rxvt).
3699 // Ps = 1 0 3 4 -> Interpret "meta" key, sets eighth bit.
3700 // (enables the eightBitInput resource).
3701 // Ps = 1 0 3 5 -> Enable special modifiers for Alt and Num-
3702 // Lock keys. (This enables the numLock resource).
3703 // Ps = 1 0 3 6 -> Send ESC when Meta modifies a key. (This
3704 // enables the metaSendsEscape resource).
3705 // Ps = 1 0 3 7 -> Send DEL from the editing-keypad Delete
3707 // Ps = 1 0 3 9 -> Send ESC when Alt modifies a key. (This
3708 // enables the altSendsEscape resource).
3709 // Ps = 1 0 4 0 -> Keep selection even if not highlighted.
3710 // (This enables the keepSelection resource).
3711 // Ps = 1 0 4 1 -> Use the CLIPBOARD selection. (This enables
3712 // the selectToClipboard resource).
3713 // Ps = 1 0 4 2 -> Enable Urgency window manager hint when
3714 // Control-G is received. (This enables the bellIsUrgent
3716 // Ps = 1 0 4 3 -> Enable raising of the window when Control-G
3717 // is received. (enables the popOnBell resource).
3718 // Ps = 1 0 4 7 -> Use Alternate Screen Buffer. (This may be
3719 // disabled by the titeInhibit resource).
3720 // Ps = 1 0 4 8 -> Save cursor as in DECSC. (This may be dis-
3721 // abled by the titeInhibit resource).
3722 // Ps = 1 0 4 9 -> Save cursor as in DECSC and use Alternate
3723 // Screen Buffer, clearing it first. (This may be disabled by
3724 // the titeInhibit resource). This combines the effects of the 1
3725 // 0 4 7 and 1 0 4 8 modes. Use this with terminfo-based
3726 // applications rather than the 4 7 mode.
3727 // Ps = 1 0 5 0 -> Set terminfo/termcap function-key mode.
3728 // Ps = 1 0 5 1 -> Set Sun function-key mode.
3729 // Ps = 1 0 5 2 -> Set HP function-key mode.
3730 // Ps = 1 0 5 3 -> Set SCO function-key mode.
3731 // Ps = 1 0 6 0 -> Set legacy keyboard emulation (X11R6).
3732 // Ps = 1 0 6 1 -> Set VT220 keyboard emulation.
3733 // Ps = 2 0 0 4 -> Set bracketed paste mode.
3735 // http://vt100.net/docs/vt220-rm/chapter4.html
3736 Terminal
.prototype.setMode = function(params
) {
3737 if (typeof params
=== 'object') {
3738 var l
= params
.length
3741 for (; i
< l
; i
++) {
3742 this.setMode(params
[i
]);
3751 this.insertMode
= true;
3754 //this.convertEol = true;
3757 } else if (this.prefix
=== '?') {
3760 this.applicationCursor
= true;
3763 this.setgCharset(0, Terminal
.charsets
.US
);
3764 this.setgCharset(1, Terminal
.charsets
.US
);
3765 this.setgCharset(2, Terminal
.charsets
.US
);
3766 this.setgCharset(3, Terminal
.charsets
.US
);
3767 // set VT100 mode here
3769 case 3: // 132 col mode
3770 this.savedCols
= this.cols
;
3771 this.resize(132, this.rows
);
3774 this.originMode
= true;
3777 this.wraparoundMode
= true;
3780 // this.cursorBlink = true;
3783 this.log('Serial port requested application keypad.');
3784 this.applicationKeypad
= true;
3786 case 9: // X10 Mouse
3787 // no release, no motion, no wheel, no modifiers.
3788 case 1000: // vt200 mouse
3790 // no modifiers, except control on the wheel.
3791 case 1002: // button event mouse
3792 case 1003: // any event mouse
3793 // any event - sends motion events,
3794 // even if there is no button held down.
3795 this.x10Mouse
= params
=== 9;
3796 this.vt200Mouse
= params
=== 1000;
3797 this.normalMouse
= params
> 1000;
3798 this.mouseEvents
= true;
3799 this.element
.style
.cursor
= 'default';
3800 this.log('Binding to mouse events.');
3802 case 1004: // send focusin/focusout events
3805 this.sendFocus
= true;
3807 case 1005: // utf8 ext mode mouse
3808 this.utfMouse
= true;
3809 // for wide terminals
3810 // simply encodes large values as utf8 characters
3812 case 1006: // sgr ext mode mouse
3813 this.sgrMouse
= true;
3814 // for wide terminals
3815 // does not add 32 to fields
3816 // press: ^[[<b;x;yM
3817 // release: ^[[<b;x;ym
3819 case 1015: // urxvt ext mode mouse
3820 this.urxvtMouse
= true;
3821 // for wide terminals
3822 // numbers for fields
3824 // motion: ^[[b;x;yT
3826 case 25: // show cursor
3827 this.cursorHidden
= false;
3829 case 1049: // alt screen buffer cursor
3830 //this.saveCursor();
3832 case 47: // alt screen buffer
3833 case 1047: // alt screen buffer
3841 scrollTop
: this.scrollTop
,
3842 scrollBottom
: this.scrollBottom
,
3844 // XXX save charset(s) here?
3845 // charset: this.charset,
3846 // glevel: this.glevel,
3847 // charsets: this.charsets
3850 this.normal
= normal
;
3858 // CSI Pm l Reset Mode (RM).
3859 // Ps = 2 -> Keyboard Action Mode (AM).
3860 // Ps = 4 -> Replace Mode (IRM).
3861 // Ps = 1 2 -> Send/receive (SRM).
3862 // Ps = 2 0 -> Normal Linefeed (LNM).
3864 // DEC Private Mode Reset (DECRST).
3865 // Ps = 1 -> Normal Cursor Keys (DECCKM).
3866 // Ps = 2 -> Designate VT52 mode (DECANM).
3867 // Ps = 3 -> 80 Column Mode (DECCOLM).
3868 // Ps = 4 -> Jump (Fast) Scroll (DECSCLM).
3869 // Ps = 5 -> Normal Video (DECSCNM).
3870 // Ps = 6 -> Normal Cursor Mode (DECOM).
3871 // Ps = 7 -> No Wraparound Mode (DECAWM).
3872 // Ps = 8 -> No Auto-repeat Keys (DECARM).
3873 // Ps = 9 -> Don't send Mouse X & Y on button press.
3874 // Ps = 1 0 -> Hide toolbar (rxvt).
3875 // Ps = 1 2 -> Stop Blinking Cursor (att610).
3876 // Ps = 1 8 -> Don't print form feed (DECPFF).
3877 // Ps = 1 9 -> Limit print to scrolling region (DECPEX).
3878 // Ps = 2 5 -> Hide Cursor (DECTCEM).
3879 // Ps = 3 0 -> Don't show scrollbar (rxvt).
3880 // Ps = 3 5 -> Disable font-shifting functions (rxvt).
3881 // Ps = 4 0 -> Disallow 80 -> 132 Mode.
3882 // Ps = 4 1 -> No more(1) fix (see curses resource).
3883 // Ps = 4 2 -> Disable Nation Replacement Character sets (DEC-
3885 // Ps = 4 4 -> Turn Off Margin Bell.
3886 // Ps = 4 5 -> No Reverse-wraparound Mode.
3887 // Ps = 4 6 -> Stop Logging. (This is normally disabled by a
3888 // compile-time option).
3889 // Ps = 4 7 -> Use Normal Screen Buffer.
3890 // Ps = 6 6 -> Numeric keypad (DECNKM).
3891 // Ps = 6 7 -> Backarrow key sends delete (DECBKM).
3892 // Ps = 1 0 0 0 -> Don't send Mouse X & Y on button press and
3893 // release. See the section Mouse Tracking.
3894 // Ps = 1 0 0 1 -> Don't use Hilite Mouse Tracking.
3895 // Ps = 1 0 0 2 -> Don't use Cell Motion Mouse Tracking.
3896 // Ps = 1 0 0 3 -> Don't use All Motion Mouse Tracking.
3897 // Ps = 1 0 0 4 -> Don't send FocusIn/FocusOut events.
3898 // Ps = 1 0 0 5 -> Disable Extended Mouse Mode.
3899 // Ps = 1 0 1 0 -> Don't scroll to bottom on tty output
3901 // Ps = 1 0 1 1 -> Don't scroll to bottom on key press (rxvt).
3902 // Ps = 1 0 3 4 -> Don't interpret "meta" key. (This disables
3903 // the eightBitInput resource).
3904 // Ps = 1 0 3 5 -> Disable special modifiers for Alt and Num-
3905 // Lock keys. (This disables the numLock resource).
3906 // Ps = 1 0 3 6 -> Don't send ESC when Meta modifies a key.
3907 // (This disables the metaSendsEscape resource).
3908 // Ps = 1 0 3 7 -> Send VT220 Remove from the editing-keypad
3910 // Ps = 1 0 3 9 -> Don't send ESC when Alt modifies a key.
3911 // (This disables the altSendsEscape resource).
3912 // Ps = 1 0 4 0 -> Do not keep selection when not highlighted.
3913 // (This disables the keepSelection resource).
3914 // Ps = 1 0 4 1 -> Use the PRIMARY selection. (This disables
3915 // the selectToClipboard resource).
3916 // Ps = 1 0 4 2 -> Disable Urgency window manager hint when
3917 // Control-G is received. (This disables the bellIsUrgent
3919 // Ps = 1 0 4 3 -> Disable raising of the window when Control-
3920 // G is received. (This disables the popOnBell resource).
3921 // Ps = 1 0 4 7 -> Use Normal Screen Buffer, clearing screen
3922 // first if in the Alternate Screen. (This may be disabled by
3923 // the titeInhibit resource).
3924 // Ps = 1 0 4 8 -> Restore cursor as in DECRC. (This may be
3925 // disabled by the titeInhibit resource).
3926 // Ps = 1 0 4 9 -> Use Normal Screen Buffer and restore cursor
3927 // as in DECRC. (This may be disabled by the titeInhibit
3928 // resource). This combines the effects of the 1 0 4 7 and 1 0
3929 // 4 8 modes. Use this with terminfo-based applications rather
3930 // than the 4 7 mode.
3931 // Ps = 1 0 5 0 -> Reset terminfo/termcap function-key mode.
3932 // Ps = 1 0 5 1 -> Reset Sun function-key mode.
3933 // Ps = 1 0 5 2 -> Reset HP function-key mode.
3934 // Ps = 1 0 5 3 -> Reset SCO function-key mode.
3935 // Ps = 1 0 6 0 -> Reset legacy keyboard emulation (X11R6).
3936 // Ps = 1 0 6 1 -> Reset keyboard emulation to Sun/PC style.
3937 // Ps = 2 0 0 4 -> Reset bracketed paste mode.
3938 Terminal
.prototype.resetMode = function(params
) {
3939 if (typeof params
=== 'object') {
3940 var l
= params
.length
3943 for (; i
< l
; i
++) {
3944 this.resetMode(params
[i
]);
3953 this.insertMode
= false;
3956 //this.convertEol = false;
3959 } else if (this.prefix
=== '?') {
3962 this.applicationCursor
= false;
3965 if (this.cols
=== 132 && this.savedCols
) {
3966 this.resize(this.savedCols
, this.rows
);
3968 delete this.savedCols
;
3971 this.originMode
= false;
3974 this.wraparoundMode
= false;
3977 // this.cursorBlink = false;
3980 this.log('Switching back to normal keypad.');
3981 this.applicationKeypad
= false;
3983 case 9: // X10 Mouse
3984 case 1000: // vt200 mouse
3985 case 1002: // button event mouse
3986 case 1003: // any event mouse
3987 this.x10Mouse
= false;
3988 this.vt200Mouse
= false;
3989 this.normalMouse
= false;
3990 this.mouseEvents
= false;
3991 this.element
.style
.cursor
= '';
3993 case 1004: // send focusin/focusout events
3994 this.sendFocus
= false;
3996 case 1005: // utf8 ext mode mouse
3997 this.utfMouse
= false;
3999 case 1006: // sgr ext mode mouse
4000 this.sgrMouse
= false;
4002 case 1015: // urxvt ext mode mouse
4003 this.urxvtMouse
= false;
4005 case 25: // hide cursor
4006 this.cursorHidden
= true;
4008 case 1049: // alt screen buffer cursor
4010 case 47: // normal screen buffer
4011 case 1047: // normal screen buffer - clearing it first
4013 this.lines
= this.normal
.lines
;
4014 this.ybase
= this.normal
.ybase
;
4015 this.ydisp
= this.normal
.ydisp
;
4016 this.x
= this.normal
.x
;
4017 this.y
= this.normal
.y
;
4018 this.scrollTop
= this.normal
.scrollTop
;
4019 this.scrollBottom
= this.normal
.scrollBottom
;
4020 this.tabs
= this.normal
.tabs
;
4022 // if (params === 1049) {
4023 // this.x = this.savedX;
4024 // this.y = this.savedY;
4026 this.refresh(0, this.rows
- 1);
4035 // Set Scrolling Region [top;bottom] (default = full size of win-
4038 Terminal
.prototype.setScrollRegion = function(params
) {
4039 if (this.prefix
) return;
4040 this.scrollTop
= (params
[0] || 1) - 1;
4041 this.scrollBottom
= (params
[1] || this.rows
) - 1;
4047 // Save cursor (ANSI.SYS).
4048 Terminal
.prototype.saveCursor = function(params
) {
4049 this.savedX
= this.x
;
4050 this.savedY
= this.y
;
4054 // Restore cursor (ANSI.SYS).
4055 Terminal
.prototype.restoreCursor = function(params
) {
4056 this.x
= this.savedX
|| 0;
4057 this.y
= this.savedY
|| 0;
4065 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
4066 Terminal
.prototype.cursorForwardTab = function(params
) {
4067 var param
= params
[0] || 1;
4069 this.x
= this.nextStop();
4073 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
4074 Terminal
.prototype.scrollUp = function(params
) {
4075 var param
= params
[0] || 1;
4077 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
4078 this.lines
.splice(this.ybase
+ this.scrollBottom
, 0, this.blankLine());
4081 this.updateRange(this.scrollTop
);
4082 this.updateRange(this.scrollBottom
);
4085 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
4086 Terminal
.prototype.scrollDown = function(params
) {
4087 var param
= params
[0] || 1;
4089 this.lines
.splice(this.ybase
+ this.scrollBottom
, 1);
4090 this.lines
.splice(this.ybase
+ this.scrollTop
, 0, this.blankLine());
4093 this.updateRange(this.scrollTop
);
4094 this.updateRange(this.scrollBottom
);
4097 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
4098 // Initiate highlight mouse tracking. Parameters are
4099 // [func;startx;starty;firstrow;lastrow]. See the section Mouse
4101 Terminal
.prototype.initMouseTracking = function(params
) {
4102 // Relevant: DECSET 1001
4106 // Reset one or more features of the title modes to the default
4107 // value. Normally, "reset" disables the feature. It is possi-
4108 // ble to disable the ability to reset features by compiling a
4109 // different default for the title modes into xterm.
4110 // Ps = 0 -> Do not set window/icon labels using hexadecimal.
4111 // Ps = 1 -> Do not query window/icon labels using hexadeci-
4113 // Ps = 2 -> Do not set window/icon labels using UTF-8.
4114 // Ps = 3 -> Do not query window/icon labels using UTF-8.
4115 // (See discussion of "Title Modes").
4116 Terminal
.prototype.resetTitleModes = function(params
) {
4120 // CSI Ps Z Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
4121 Terminal
.prototype.cursorBackwardTab = function(params
) {
4122 var param
= params
[0] || 1;
4124 this.x
= this.prevStop();
4128 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
4129 Terminal
.prototype.repeatPrecedingCharacter = function(params
) {
4130 var param
= params
[0] || 1
4131 , line
= this.lines
[this.ybase
+ this.y
]
4132 , ch
= line
[this.x
- 1] || [this.defAttr
, ' '];
4134 while (param
--) line
[this.x
++] = ch
;
4137 // CSI Ps g Tab Clear (TBC).
4138 // Ps = 0 -> Clear Current Column (default).
4139 // Ps = 3 -> Clear All.
4141 // Ps = 2 -> Clear Stops on Line.
4142 // http://vt100.net/annarbor/aaa-ug/section6.html
4143 Terminal
.prototype.tabClear = function(params
) {
4144 var param
= params
[0];
4146 delete this.tabs
[this.x
];
4147 } else if (param
=== 3) {
4152 // CSI Pm i Media Copy (MC).
4153 // Ps = 0 -> Print screen (default).
4154 // Ps = 4 -> Turn off printer controller mode.
4155 // Ps = 5 -> Turn on printer controller mode.
4157 // Media Copy (MC, DEC-specific).
4158 // Ps = 1 -> Print line containing cursor.
4159 // Ps = 4 -> Turn off autoprint mode.
4160 // Ps = 5 -> Turn on autoprint mode.
4161 // Ps = 1 0 -> Print composed display, ignores DECPEX.
4162 // Ps = 1 1 -> Print all pages.
4163 Terminal
.prototype.mediaCopy = function(params
) {
4168 // Set or reset resource-values used by xterm to decide whether
4169 // to construct escape sequences holding information about the
4170 // modifiers pressed with a given key. The first parameter iden-
4171 // tifies the resource to set/reset. The second parameter is the
4172 // value to assign to the resource. If the second parameter is
4173 // omitted, the resource is reset to its initial value.
4174 // Ps = 1 -> modifyCursorKeys.
4175 // Ps = 2 -> modifyFunctionKeys.
4176 // Ps = 4 -> modifyOtherKeys.
4177 // If no parameters are given, all resources are reset to their
4179 Terminal
.prototype.setResources = function(params
) {
4184 // Disable modifiers which may be enabled via the CSI > Ps; Ps m
4185 // sequence. This corresponds to a resource value of "-1", which
4186 // cannot be set with the other sequence. The parameter identi-
4187 // fies the resource to be disabled:
4188 // Ps = 1 -> modifyCursorKeys.
4189 // Ps = 2 -> modifyFunctionKeys.
4190 // Ps = 4 -> modifyOtherKeys.
4191 // If the parameter is omitted, modifyFunctionKeys is disabled.
4192 // When modifyFunctionKeys is disabled, xterm uses the modifier
4193 // keys to make an extended sequence of functions rather than
4194 // adding a parameter to each function key to denote the modi-
4196 Terminal
.prototype.disableModifiers = function(params
) {
4201 // Set resource value pointerMode. This is used by xterm to
4202 // decide whether to hide the pointer cursor as the user types.
4203 // Valid values for the parameter:
4204 // Ps = 0 -> never hide the pointer.
4205 // Ps = 1 -> hide if the mouse tracking mode is not enabled.
4206 // Ps = 2 -> always hide the pointer. If no parameter is
4207 // given, xterm uses the default, which is 1 .
4208 Terminal
.prototype.setPointerMode = function(params
) {
4212 // CSI ! p Soft terminal reset (DECSTR).
4213 // http://vt100.net/docs/vt220-rm/table4-10.html
4214 Terminal
.prototype.softReset = function(params
) {
4215 this.cursorHidden
= false;
4216 this.insertMode
= false;
4217 this.originMode
= false;
4218 this.wraparoundMode
= false; // autowrap
4219 this.applicationKeypad
= false; // ?
4220 this.applicationCursor
= false;
4222 this.scrollBottom
= this.rows
- 1;
4223 this.curAttr
= this.defAttr
;
4224 this.x
= this.y
= 0; // ?
4225 this.charset
= null;
4226 this.glevel
= 0; // ??
4227 this.charsets
= [null]; // ??
4231 // Request ANSI mode (DECRQM). For VT300 and up, reply is
4233 // where Ps is the mode number as in RM, and Pm is the mode
4235 // 0 - not recognized
4238 // 3 - permanently set
4239 // 4 - permanently reset
4240 Terminal
.prototype.requestAnsiMode = function(params
) {
4245 // Request DEC private mode (DECRQM). For VT300 and up, reply is
4247 // where Ps is the mode number as in DECSET, Pm is the mode value
4248 // as in the ANSI DECRQM.
4249 Terminal
.prototype.requestPrivateMode = function(params
) {
4254 // Set conformance level (DECSCL). Valid values for the first
4256 // Ps = 6 1 -> VT100.
4257 // Ps = 6 2 -> VT200.
4258 // Ps = 6 3 -> VT300.
4259 // Valid values for the second parameter:
4260 // Ps = 0 -> 8-bit controls.
4261 // Ps = 1 -> 7-bit controls (always set for VT100).
4262 // Ps = 2 -> 8-bit controls.
4263 Terminal
.prototype.setConformanceLevel = function(params
) {
4267 // CSI Ps q Load LEDs (DECLL).
4268 // Ps = 0 -> Clear all LEDS (default).
4269 // Ps = 1 -> Light Num Lock.
4270 // Ps = 2 -> Light Caps Lock.
4271 // Ps = 3 -> Light Scroll Lock.
4272 // Ps = 2 1 -> Extinguish Num Lock.
4273 // Ps = 2 2 -> Extinguish Caps Lock.
4274 // Ps = 2 3 -> Extinguish Scroll Lock.
4275 Terminal
.prototype.loadLEDs = function(params
) {
4280 // Set cursor style (DECSCUSR, VT520).
4281 // Ps = 0 -> blinking block.
4282 // Ps = 1 -> blinking block (default).
4283 // Ps = 2 -> steady block.
4284 // Ps = 3 -> blinking underline.
4285 // Ps = 4 -> steady underline.
4286 Terminal
.prototype.setCursorStyle = function(params
) {
4291 // Select character protection attribute (DECSCA). Valid values
4292 // for the parameter:
4293 // Ps = 0 -> DECSED and DECSEL can erase (default).
4294 // Ps = 1 -> DECSED and DECSEL cannot erase.
4295 // Ps = 2 -> DECSED and DECSEL can erase.
4296 Terminal
.prototype.setCharProtectionAttr = function(params
) {
4301 // Restore DEC Private Mode Values. The value of Ps previously
4302 // saved is restored. Ps values are the same as for DECSET.
4303 Terminal
.prototype.restorePrivateValues = function(params
) {
4307 // CSI Pt; Pl; Pb; Pr; Ps$ r
4308 // Change Attributes in Rectangular Area (DECCARA), VT400 and up.
4309 // Pt; Pl; Pb; Pr denotes the rectangle.
4310 // Ps denotes the SGR attributes to change: 0, 1, 4, 5, 7.
4311 // NOTE: xterm doesn't enable this code by default.
4312 Terminal
.prototype.setAttrInRectangle = function(params
) {
4322 for (; t
< b
+ 1; t
++) {
4323 line
= this.lines
[this.ybase
+ t
];
4324 for (i
= l
; i
< r
; i
++) {
4325 line
[i
] = [attr
, line
[i
][1]];
4330 this.updateRange(params
[0]);
4331 this.updateRange(params
[2]);
4335 // CSI Pc; Pt; Pl; Pb; Pr$ x
4336 // Fill Rectangular Area (DECFRA), VT420 and up.
4337 // Pc is the character to use.
4338 // Pt; Pl; Pb; Pr denotes the rectangle.
4339 // NOTE: xterm doesn't enable this code by default.
4340 Terminal
.prototype.fillRectangle = function(params
) {
4350 for (; t
< b
+ 1; t
++) {
4351 line
= this.lines
[this.ybase
+ t
];
4352 for (i
= l
; i
< r
; i
++) {
4353 line
[i
] = [line
[i
][0], String
.fromCharCode(ch
)];
4358 this.updateRange(params
[1]);
4359 this.updateRange(params
[3]);
4363 // Enable Locator Reporting (DECELR).
4364 // Valid values for the first parameter:
4365 // Ps = 0 -> Locator disabled (default).
4366 // Ps = 1 -> Locator enabled.
4367 // Ps = 2 -> Locator enabled for one report, then disabled.
4368 // The second parameter specifies the coordinate unit for locator
4370 // Valid values for the second parameter:
4371 // Pu = 0 <- or omitted -> default to character cells.
4372 // Pu = 1 <- device physical pixels.
4373 // Pu = 2 <- character cells.
4374 Terminal
.prototype.enableLocatorReporting = function(params
) {
4375 var val
= params
[0] > 0;
4376 //this.mouseEvents = val;
4377 //this.decLocator = val;
4380 // CSI Pt; Pl; Pb; Pr$ z
4381 // Erase Rectangular Area (DECERA), VT400 and up.
4382 // Pt; Pl; Pb; Pr denotes the rectangle.
4383 // NOTE: xterm doesn't enable this code by default.
4384 Terminal
.prototype.eraseRectangle = function(params
) {
4394 ch
= [this.eraseAttr(), ' ']; // xterm?
4396 for (; t
< b
+ 1; t
++) {
4397 line
= this.lines
[this.ybase
+ t
];
4398 for (i
= l
; i
< r
; i
++) {
4404 this.updateRange(params
[0]);
4405 this.updateRange(params
[2]);
4410 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
4411 // NOTE: xterm doesn't enable this code by default.
4412 Terminal
.prototype.insertColumns = function() {
4413 var param
= params
[0]
4414 , l
= this.ybase
+ this.rows
4415 , ch
= [this.eraseAttr(), ' '] // xterm?
4419 for (i
= this.ybase
; i
< l
; i
++) {
4420 this.lines
[i
].splice(this.x
+ 1, 0, ch
);
4421 this.lines
[i
].pop();
4430 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
4431 // NOTE: xterm doesn't enable this code by default.
4432 Terminal
.prototype.deleteColumns = function() {
4433 var param
= params
[0]
4434 , l
= this.ybase
+ this.rows
4435 , ch
= [this.eraseAttr(), ' '] // xterm?
4439 for (i
= this.ybase
; i
< l
; i
++) {
4440 this.lines
[i
].splice(this.x
, 1);
4441 this.lines
[i
].push(ch
);
4452 Terminal
.charsets
= {};
4454 // DEC Special Character and Line Drawing Set.
4455 // http://vt100.net/docs/vt102-ug/table5-13.html
4456 // A lot of curses apps use this if they see TERM=xterm.
4457 // testing: echo -e '\e(0a\e(B'
4458 // The xterm output sometimes seems to conflict with the
4459 // reference above. xterm seems in line with the reference
4460 // when running vttest however.
4461 // The table below now uses xterm's output from vttest.
4462 Terminal
.charsets
.SCLD
= { // (0
4463 '`': '\u25c6', // '◆'
4464 'a': '\u2592', // '▒'
4465 'b': '\u0009', // '\t'
4466 'c': '\u000c', // '\f'
4467 'd': '\u000d', // '\r'
4468 'e': '\u000a', // '\n'
4469 'f': '\u00b0', // '°'
4470 'g': '\u00b1', // '±'
4471 'h': '\u2424', // '\u2424' (NL)
4472 'i': '\u000b', // '\v'
4473 'j': '\u2518', // '┘'
4474 'k': '\u2510', // '┐'
4475 'l': '\u250c', // '┌'
4476 'm': '\u2514', // '└'
4477 'n': '\u253c', // '┼'
4478 'o': '\u23ba', // '⎺'
4479 'p': '\u23bb', // '⎻'
4480 'q': '\u2500', // '─'
4481 'r': '\u23bc', // '⎼'
4482 's': '\u23bd', // '⎽'
4483 't': '\u251c', // '├'
4484 'u': '\u2524', // '┤'
4485 'v': '\u2534', // '┴'
4486 'w': '\u252c', // '┬'
4487 'x': '\u2502', // '│'
4488 'y': '\u2264', // '≤'
4489 'z': '\u2265', // '≥'
4490 '{': '\u03c0', // 'π'
4491 '|': '\u2260', // '≠'
4492 '}': '\u00a3', // '£'
4493 '~': '\u00b7' // '·'
4496 Terminal
.charsets
.UK
= null; // (A
4497 Terminal
.charsets
.US
= null; // (B (USASCII)
4498 Terminal
.charsets
.Dutch
= null; // (4
4499 Terminal
.charsets
.Finnish
= null; // (C or (5
4500 Terminal
.charsets
.French
= null; // (R
4501 Terminal
.charsets
.FrenchCanadian
= null; // (Q
4502 Terminal
.charsets
.German
= null; // (K
4503 Terminal
.charsets
.Italian
= null; // (Y
4504 Terminal
.charsets
.NorwegianDanish
= null; // (E or (6
4505 Terminal
.charsets
.Spanish
= null; // (Z
4506 Terminal
.charsets
.Swedish
= null; // (H or (7
4507 Terminal
.charsets
.Swiss
= null; // (=
4508 Terminal
.charsets
.ISOLatin
= null; // /A
4514 function contains(el
, arr
) {
4515 for (var i
= 0; i
< arr
.length
; i
+= 1) {
4516 if (el
=== arr
[i
]) {
4523 function on(el
, type
, handler
, capture
) {
4524 if (!Array
.isArray(el
)) {
4527 el
.forEach(function (element
) {
4528 element
.addEventListener(type
, handler
, capture
|| false);
4532 function off(el
, type
, handler
, capture
) {
4533 el
.removeEventListener(type
, handler
, capture
|| false);
4536 function cancel(ev
, force
) {
4537 if (!this.cancelEvents
&& !force
) {
4540 ev
.preventDefault();
4541 ev
.stopPropagation();
4545 function inherits(child
, parent
) {
4547 this.constructor = child
;
4549 f
.prototype = parent
.prototype;
4550 child
.prototype = new f
;
4553 // if bold is broken, we can't
4554 // use it in the terminal.
4555 function isBoldBroken(document
) {
4556 var body
= document
.getElementsByTagName('body')[0];
4557 var el
= document
.createElement('span');
4558 el
.innerHTML
= 'hello world';
4559 body
.appendChild(el
);
4560 var w1
= el
.scrollWidth
;
4561 el
.style
.fontWeight
= 'bold';
4562 var w2
= el
.scrollWidth
;
4563 body
.removeChild(el
);
4567 var String
= this.String
;
4568 var setTimeout
= this.setTimeout
;
4569 var setInterval
= this.setInterval
;
4571 function indexOf(obj
, el
) {
4574 if (obj
[i
] === el
) return i
;
4579 function isThirdLevelShift(term
, ev
) {
4581 (term
.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) ||
4582 (term
.isMSWindows
&& ev
.altKey
&& ev
.ctrlKey
&& !ev
.metaKey
);
4584 // Don't invoke for arrows, pageDown, home, backspace, etc.
4585 return thirdLevelKey
&& (!ev
.keyCode
|| ev
.keyCode
> 47);
4588 function isWide(ch
) {
4589 if (ch
<= '\uff00') return false;
4590 return (ch
>= '\uff01' && ch
<= '\uffbe')
4591 || (ch
>= '\uffc2' && ch
<= '\uffc7')
4592 || (ch
>= '\uffca' && ch
<= '\uffcf')
4593 || (ch
>= '\uffd2' && ch
<= '\uffd7')
4594 || (ch
>= '\uffda' && ch
<= '\uffdc')
4595 || (ch
>= '\uffe0' && ch
<= '\uffe6')
4596 || (ch
>= '\uffe8' && ch
<= '\uffee');
4599 function matchColor(r1
, g1
, b1
) {
4600 var hash
= (r1
<< 16) | (g1
<< 8) | b1
;
4602 if (matchColor
._cache
[hash
] != null) {
4603 return matchColor
._cache
[hash
];
4606 var ldiff
= Infinity
4615 for (; i
< Terminal
.vcolors
.length
; i
++) {
4616 c
= Terminal
.vcolors
[i
];
4621 diff
= matchColor
.distance(r1
, g1
, b1
, r2
, g2
, b2
);
4634 return matchColor
._cache
[hash
] = li
;
4637 matchColor
._cache
= {};
4639 // http://stackoverflow.com/questions/1633828
4640 matchColor
.distance = function(r1
, g1
, b1
, r2
, g2
, b2
) {
4641 return Math
.pow(30 * (r1
- r2
), 2)
4642 + Math
.pow(59 * (g1
- g2
), 2)
4643 + Math
.pow(11 * (b1
- b2
), 2);
4646 function each(obj
, iter
, con
) {
4647 if (obj
.forEach
) return obj
.forEach(iter
, con
);
4648 for (var i
= 0; i
< obj
.length
; i
++) {
4649 iter
.call(con
, obj
[i
], i
, obj
);
4653 function keys(obj
) {
4654 if (Object
.keys
) return Object
.keys(obj
);
4657 if (Object
.prototype.hasOwnProperty
.call(obj
, key
)) {
4668 Terminal
.EventEmitter
= EventEmitter
;
4669 Terminal
.inherits
= inherits
;
4672 * Adds an event listener to the terminal.
4674 * @param {string} event The name of the event. TODO: Document all event types
4675 * @param {function} callback The function to call when the event is triggered.
4681 Terminal
.cancel
= cancel
;