]>
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;
145 function Terminal(options
) {
148 if (!(this instanceof Terminal
)) {
149 return new Terminal(arguments
[0], arguments
[1], arguments
[2]);
152 self
.cancel
= Terminal
.cancel
;
154 EventEmitter
.call(this);
156 if (typeof options
=== 'number') {
160 handler
: arguments
[2]
164 options
= options
|| {};
167 Object
.keys(Terminal
.defaults
).forEach(function(key
) {
168 if (options
[key
] == null) {
169 options
[key
] = Terminal
.options
[key
];
171 if (Terminal
[key
] !== Terminal
.defaults
[key
]) {
172 options
[key
] = Terminal
[key
];
175 self
[key
] = options
[key
];
178 if (options
.colors
.length
=== 8) {
179 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(8));
180 } else if (options
.colors
.length
=== 16) {
181 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(16));
182 } else if (options
.colors
.length
=== 10) {
183 options
.colors
= options
.colors
.slice(0, -2).concat(
184 Terminal
._colors
.slice(8, -2), options
.colors
.slice(-2));
185 } else if (options
.colors
.length
=== 18) {
186 options
.colors
= options
.colors
.concat(
187 Terminal
._colors
.slice(16, -2), options
.colors
.slice(-2));
189 this.colors
= options
.colors
;
191 this.options
= options
;
193 // this.context = options.context || window;
194 // this.document = options.document || document;
195 this.parent
= options
.body
|| options
.parent
196 || (document
? document
.getElementsByTagName('body')[0] : null);
198 this.cols
= options
.cols
|| options
.geometry
[0];
199 this.rows
= options
.rows
|| options
.geometry
[1];
201 if (options
.handler
) {
202 this.on('data', options
.handler
);
206 * The scroll position of the y cursor, ie. ybase + y = the y position within the entire
211 * The scroll position of the viewport
216 * The cursor's x position after ybase
221 * The cursor's y position after ybase
225 this.cursorState
= 0;
226 this.cursorHidden
= false;
231 this.scrollBottom
= this.rows
- 1;
234 this.applicationKeypad
= false;
235 this.applicationCursor
= false;
236 this.originMode
= false;
237 this.insertMode
= false;
238 this.wraparoundMode
= false;
243 this.gcharset
= null;
245 this.charsets
= [null];
269 this.readable
= true;
270 this.writable
= true;
272 this.defAttr
= (0 << 18) | (257 << 9) | (256 << 0);
273 this.curAttr
= this.defAttr
;
276 this.currentParam
= 0;
281 * An array of all lines in the entire buffer, including the prompt. The lines are array of
282 * characters which are 2-length arrays where [0] is an attribute and [1] is the character.
287 this.lines
.push(this.blankLine());
294 inherits(Terminal
, EventEmitter
);
296 // back_color_erase feature for xterm.
297 Terminal
.prototype.eraseAttr = function() {
298 // if (this.is('screen')) return this.defAttr;
299 return (this.defAttr
& ~0x1ff) | (this.curAttr
& 0x1ff);
307 Terminal
.tangoColors
= [
328 // Colors 0-15 + 16-255
329 // Much thanks to TooTallNate for writing this.
330 Terminal
.colors
= (function() {
331 var colors
= Terminal
.tangoColors
.slice()
332 , r
= [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff]
337 for (; i
< 216; i
++) {
338 out(r
[(i
/ 36) % 6 | 0], r
[(i
/ 6) % 6 | 0], r
[i
% 6]);
343 for (; i
< 24; i
++) {
348 function out(r
, g
, b
) {
349 colors
.push('#' + hex(r
) + hex(g
) + hex(b
));
354 return c
.length
< 2 ? '0' + c
: c
;
360 Terminal
._colors
= Terminal
.colors
.slice();
362 Terminal
.vcolors
= (function() {
364 , colors
= Terminal
.colors
368 for (; i
< 256; i
++) {
369 color
= parseInt(colors
[i
].substring(1), 16);
371 (color
>> 16) & 0xff,
384 Terminal
.defaults
= {
385 colors
: Terminal
.colors
,
397 // programFeatures: false,
401 Terminal
.options
= {};
403 Terminal
.focus
= null;
405 each(keys(Terminal
.defaults
), function(key
) {
406 Terminal
[key
] = Terminal
.defaults
[key
];
407 Terminal
.options
[key
] = Terminal
.defaults
[key
];
413 Terminal
.prototype.focus = function() {
414 if (document
.activeElement
=== this.element
) {
418 if (this.sendFocus
) {
423 this.element
.focus();
426 Terminal
.prototype.blur = function() {
427 if (Terminal
.focus
!== this) {
431 this.cursorState
= 0;
432 this.refresh(this.y
, this.y
);
435 if (this.sendFocus
) {
438 Terminal
.focus
= null;
442 * Initialize default behavior
444 Terminal
.prototype.initGlobal = function() {
445 Terminal
.bindKeys(this);
446 Terminal
.bindPaste(this);
447 Terminal
.bindCopy(this);
448 Terminal
.bindCut(this);
449 Terminal
.bindDrop(this);
453 * Clears all selected text, inside the terminal.
455 Terminal
.prototype.clearSelection = function() {
456 var selectionBaseNode
= window
.getSelection().baseNode
;
458 if (selectionBaseNode
&& (this.element
.contains(selectionBaseNode
.parentElement
))) {
459 window
.getSelection().removeAllRanges();
464 * This function temporarily enables (leases) the contentEditable value of the terminal, which
465 * should be set back to false within 5 seconds at most.
467 Terminal
.prototype.leaseContentEditable = function (ms
, callback
) {
470 term
.element
.contentEditable
= true;
471 setTimeout(function () {
472 term
.element
.contentEditable
= false;
473 if (typeof callback
== 'function') {
480 * Bind to paste event and allow both keyboard and right-click pasting, without having the
481 * contentEditable value set to true.
483 Terminal
.bindPaste = function(term
) {
484 on(term
.element
, 'paste', function(ev
) {
485 if (ev
.clipboardData
) {
486 var text
= ev
.clipboardData
.getData('text/plain');
487 term
.emit('paste', text
, ev
);
490 * Cancel the paste event, or else things will be pasted twice:
491 * 1. by the terminal handler
492 * 2. by the browser, because of the contentEditable value being true
494 term
.cancel(ev
, true);
497 * After the paste event is completed, always set the contentEditable value to false.
499 term
.element
.contentEditable
= false;
504 * Hack pasting with keyboard, in order to make it work without contentEditable.
505 * When a user types Ctrl + Shift + V or Cmd + V on a Mac, lease the contentEditable value
508 on(term
.element
, 'keydown', function (ev
) {
510 * If on a Mac, lease the contentEditable value temporarily, when the user presses
511 * the Cmd button, in order to cope with some sync issues on Safari.
513 if (term
.isMac
&& ev
.keyCode
== 91) {
514 term
.leaseContentEditable(1000);
517 if (ev
.keyCode
== 86) { // keyCode 96 corresponds to "v"
518 if (term
.isMac
&& ev
.metaKey
) {
519 term
.leaseContentEditable();
523 if (!term
.isMac
&& ev
.keyCode
== 45 && ev
.shiftKey
&& !ev
.ctrlKey
) {
524 // Shift + Insert pastes on windows and many linuxes
525 term
.leaseContentEditable();
530 * Hack pasting with right-click in order to allow right-click paste, by leasing the
531 * contentEditable value as true.
533 on(term
.element
, 'contextmenu', function (ev
) {
534 term
.leaseContentEditable();
540 * Apply key handling to the terminal
542 Terminal
.bindKeys = function(term
) {
543 on(term
.element
, 'keydown', function(ev
) {
547 on(term
.element
, 'keypress', function(ev
) {
556 Terminal
.bindCopy = function(term
) {
557 on(term
.element
, 'copy', function(ev
) {
558 var space
= String
.fromCharCode(32),
559 nonBreakingSpace
= String
.fromCharCode(160),
560 allNonBreakingSpaces
= new RegExp(nonBreakingSpace
, 'g'),
561 selectedText
= window
.getSelection().toString(),
562 copiedText
= selectedText
.split('\n').map(function (element
) {
564 * Strip all trailing white spaces and convert all non-breaking spaces to regular
567 var line
= element
.replace(/\s+$/g, '').replace(allNonBreakingSpaces
, space
);
571 ev
.clipboardData
.setData('text/plain', copiedText
);
577 * Cancel the cut event completely
579 Terminal
.bindCut = function(term
) {
580 on(term
.element
, 'cut', function (ev
) {
586 Terminal
.bindDrop = function (term
) {
588 * Do not perform the "drop" event. Altering the contents of the
589 * terminal with drag n drop is unwanted behavior.
591 on(term
.element
, 'drop', function (ev
) {
592 term
.cancel(ev
, true);
597 Terminal
.click = function (term
) {
599 * Do not perform the "drop" event. Altering the contents of the
600 * terminal with drag n drop is unwanted behavior.
602 on(term
.element
, 'click', function (ev
) {
603 term
.cancel(ev
, true);
609 * Insert the given row to the terminal or produce a new one
610 * if no row argument is passed. Return the inserted row.
612 Terminal
.prototype.insertRow = function (row
) {
613 if (typeof row
!= 'object') {
614 row
= document
.createElement('div');
617 this.rowContainer
.appendChild(row
);
618 this.children
.push(row
);
625 * Open Terminal in the DOM
627 Terminal
.prototype.open = function(parent
) {
628 var self
=this, i
=0, div
;
630 this.parent
= parent
|| this.parent
;
633 throw new Error('Terminal requires a parent element.');
637 * Grab global elements
639 this.context
= this.parent
.ownerDocument
.defaultView
;
640 this.document
= this.parent
.ownerDocument
;
641 this.body
= this.document
.getElementsByTagName('body')[0];
646 if (this.context
.navigator
&& this.context
.navigator
.userAgent
) {
647 this.isMac
= !!~this.context
.navigator
.userAgent
.indexOf('Mac');
648 this.isIpad
= !!~this.context
.navigator
.userAgent
.indexOf('iPad');
649 this.isIphone
= !!~this.context
.navigator
.userAgent
.indexOf('iPhone');
650 this.isMSIE
= !!~this.context
.navigator
.userAgent
.indexOf('MSIE');
654 * Create main element container
656 this.element
= this.document
.createElement('div');
657 this.element
.classList
.add('terminal');
658 this.element
.classList
.add('xterm');
659 this.element
.classList
.add('xterm-theme-' + this.theme
);
660 this.element
.setAttribute('tabindex', 0);
661 this.element
.spellcheck
= 'false';
662 this.element
.onfocus = function() {
663 self
.emit('focus', {terminal
: this});
665 this.element
.onblur = function() {
666 self
.emit('blur', {terminal
: this});
670 * Create the container that will hold the lines of the terminal and then
671 * produce the lines the lines.
673 this.rowContainer
= document
.createElement('div');
674 this.rowContainer
.classList
.add('xterm-rows');
675 this.element
.appendChild(this.rowContainer
);
678 for (; i
< this.rows
; i
++) {
681 this.parent
.appendChild(this.element
);
684 this.refresh(0, this.rows
- 1);
686 // Initialize global actions that
687 // need to be taken on the document.
690 // Ensure there is a Terminal.focus.
693 // Start blinking the cursor.
696 on(this.element
, 'mouseup', function() {
697 var selection
= document
.getSelection(),
698 collapsed
= selection
.isCollapsed
,
699 isRange
= typeof collapsed
== 'boolean' ? !collapsed
: selection
.type
== 'Range';
705 // Listen for mouse events and translate
706 // them into terminal mouse protocols.
709 // Figure out whether boldness affects
710 // the character width of monospace fonts.
711 if (Terminal
.brokenBold
== null) {
712 Terminal
.brokenBold
= isBoldBroken(this.document
);
718 // XTerm mouse events
719 // http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#Mouse%20Tracking
720 // To better understand these
721 // the xterm code is very helpful:
723 // button.c, charproc.c, misc.c
724 // Relevant functions in xterm/button.c:
725 // BtnCode, EmitButtonCode, EditorButton, SendMousePosition
726 Terminal
.prototype.bindMouse = function() {
727 var el
= this.element
731 var wheelEvent
= 'onmousewheel' in this.context
735 // mouseup, mousedown, mousewheel
736 // left click: ^[[M 3<^[[M#3<
737 // mousewheel up: ^[[M`3>
738 function sendButton(ev
) {
742 // get the xterm-style button
743 button
= getButton(ev
);
745 // get mouse coordinates
749 sendEvent(button
, pos
);
756 // keep it at the left
757 // button, just in case.
768 // motion example of a left click:
769 // ^[[M 3<^[[M@4<^[[M@5<^[[M@6<^[[M@7<^[[M#7<
770 function sendMove(ev
) {
777 // buttons marked as motions
778 // are incremented by 32
781 sendEvent(button
, pos
);
785 // position to characters
786 function encode(data
, ch
) {
787 if (!self
.utfMouse
) {
788 if (ch
=== 255) return data
.push(0);
789 if (ch
> 127) ch
= 127;
792 if (ch
=== 2047) return data
.push(0);
796 if (ch
> 2047) ch
= 2047;
797 data
.push(0xC0 | (ch
>> 6));
798 data
.push(0x80 | (ch
& 0x3F));
803 // send a mouse event:
804 // regular/utf8: ^[[M Cb Cx Cy
805 // urxvt: ^[[ Cb ; Cx ; Cy M
806 // sgr: ^[[ Cb ; Cx ; Cy M/m
807 // vt300: ^[[ 24(1/3/5)~ [ Cx , Cy ] \r
808 // locator: CSI P e ; P b ; P r ; P c ; P p & w
809 function sendEvent(button
, pos
) {
810 // self.emit('mouse', {
816 if (self
.vt300Mouse
) {
818 // http://www.vt100.net/docs/vt3xx-gp/chapter15.html
822 var data
= '\x1b[24';
823 if (button
=== 0) data
+= '1';
824 else if (button
=== 1) data
+= '3';
825 else if (button
=== 2) data
+= '5';
826 else if (button
=== 3) return;
828 data
+= '~[' + pos
.x
+ ',' + pos
.y
+ ']\r';
833 if (self
.decLocator
) {
838 if (button
=== 0) button
= 2;
839 else if (button
=== 1) button
= 4;
840 else if (button
=== 2) button
= 6;
841 else if (button
=== 3) button
= 3;
845 + (button
=== 3 ? 4 : 0)
856 if (self
.urxvtMouse
) {
861 self
.send('\x1b[' + button
+ ';' + pos
.x
+ ';' + pos
.y
+ 'M');
869 + ((button
& 3) === 3 ? button
& ~3 : button
)
874 + ((button
& 3) === 3 ? 'm' : 'M'));
880 encode(data
, button
);
884 self
.send('\x1b[M' + String
.fromCharCode
.apply(String
, data
));
887 function getButton(ev
) {
900 // 1, and 2 - with 64 added
903 button
= ev
.button
!= null
910 button
= button
=== 1 ? 0 : button
=== 4 ? 1 : button
;
916 case 'DOMMouseScroll':
917 button
= ev
.detail
< 0
922 button
= ev
.wheelDeltaY
> 0
928 // next three bits are the modifiers:
929 // 4 = shift, 8 = meta, 16 = control
930 shift
= ev
.shiftKey
? 4 : 0;
931 meta
= ev
.metaKey
? 8 : 0;
932 ctrl
= ev
.ctrlKey
? 16 : 0;
933 mod
= shift
| meta
| ctrl
;
936 if (self
.vt200Mouse
) {
939 } else if (!self
.normalMouse
) {
944 button
= (32 + (mod
<< 2)) + button
;
949 // mouse coordinates measured in cols/rows
950 function getCoords(ev
) {
953 // ignore browsers without pageX for now
954 if (ev
.pageX
== null) return;
960 // should probably check offsetParent
961 // but this is more portable
962 while (el
&& el
!== self
.document
.documentElement
) {
965 el
= 'offsetParent' in el
970 // convert to cols/rows
971 w
= self
.element
.clientWidth
;
972 h
= self
.element
.clientHeight
;
973 x
= Math
.round((x
/ w
) * self
.cols
);
974 y
= Math
.round((y
/ h
) * self
.rows
);
976 // be sure to avoid sending
977 // bad positions to the program
979 if (x
> self
.cols
) x
= self
.cols
;
981 if (y
> self
.rows
) y
= self
.rows
;
983 // xterm sends raw bytes and
984 // starts at 32 (SP) for each.
991 type
: ev
.type
=== wheelEvent
997 on(el
, 'mousedown', function(ev
) {
998 if (!self
.mouseEvents
) return;
1007 //if (self.vt200Mouse && !self.normalMouse) {
1008 if (self
.vt200Mouse
) {
1009 sendButton({ __proto__
: ev
, type
: 'mouseup' });
1010 return self
.cancel(ev
);
1014 if (self
.normalMouse
) on(self
.document
, 'mousemove', sendMove
);
1016 // x10 compatibility mode can't send button releases
1017 if (!self
.x10Mouse
) {
1018 on(self
.document
, 'mouseup', function up(ev
) {
1020 if (self
.normalMouse
) off(self
.document
, 'mousemove', sendMove
);
1021 off(self
.document
, 'mouseup', up
);
1022 return self
.cancel(ev
);
1026 return self
.cancel(ev
);
1029 //if (self.normalMouse) {
1030 // on(self.document, 'mousemove', sendMove);
1033 on(el
, wheelEvent
, function(ev
) {
1034 if (!self
.mouseEvents
) return;
1037 || self
.decLocator
) return;
1039 return self
.cancel(ev
);
1042 // allow mousewheel scrolling in
1043 // the shell for example
1044 on(el
, wheelEvent
, function(ev
) {
1045 if (self
.mouseEvents
) return;
1046 if (self
.applicationKeypad
) return;
1047 if (ev
.type
=== 'DOMMouseScroll') {
1048 self
.scrollDisp(ev
.detail
< 0 ? -1 : 1);
1050 self
.scrollDisp(ev
.wheelDeltaY
> 0 ? -1 : 1);
1052 return self
.cancel(ev
);
1060 Terminal
.prototype.destroy = function() {
1061 this.readable
= false;
1062 this.writable
= false;
1064 this.handler = function() {};
1065 this.write = function() {};
1066 if (this.element
.parentNode
) {
1067 this.element
.parentNode
.removeChild(this.element
);
1069 //this.emit('close');
1074 * Flags used to render terminal text properly
1087 * In the screen buffer, each character
1088 * is stored as a an array with a character
1089 * and a 32-bit integer.
1090 * First value: a utf-16 character.
1092 * Next 9 bits: background color (0-511).
1093 * Next 9 bits: foreground color (0-511).
1094 * Next 14 bits: a mask for misc. flags:
1095 * 1=bold, 2=underline, 4=blink, 8=inverse, 16=invisible
1097 Terminal
.prototype.refresh = function(start
, end
) {
1098 var x
, y
, i
, line
, out
, ch
, width
, data
, attr
, bg
, fg
, flags
, row
, parent
, focused
= document
.activeElement
;
1100 if (end
- start
>= this.rows
/ 2) {
1101 parent
= this.element
.parentNode
;
1102 if (parent
) parent
.removeChild(this.element
);
1108 if (end
>= this.lines
.length
) {
1109 this.log('`end` is too large. Most likely a bad CSR.');
1110 end
= this.lines
.length
- 1;
1113 for (; y
<= end
; y
++) {
1114 row
= y
+ this.ydisp
;
1116 line
= this.lines
[row
];
1119 if (this.y
=== y
- (this.ybase
- this.ydisp
)
1121 && !this.cursorHidden
) {
1127 attr
= this.defAttr
;
1130 for (; i
< width
; i
++) {
1134 if (i
=== x
) data
= -1;
1136 if (data
!== attr
) {
1137 if (attr
!== this.defAttr
) {
1140 if (data
!== this.defAttr
) {
1142 out
+= '<span class="reverse-video terminal-cursor">';
1144 var classNames
= [];
1147 fg
= (data
>> 9) & 0x1ff;
1150 if (flags
& Terminal
.flags
.BOLD
) {
1151 if (!Terminal
.brokenBold
) {
1152 classNames
.push('xterm-bold');
1154 // See: XTerm*boldColors
1155 if (fg
< 8) fg
+= 8;
1158 if (flags
& Terminal
.flags
.UNDERLINE
) {
1159 classNames
.push('xterm-underline');
1162 if (flags
& Terminal
.flags
.BLINK
) {
1163 classNames
.push('xterm-blink');
1167 * If inverse flag is on, then swap the foreground and background variables.
1169 if (flags
& Terminal
.flags
.INVERSE
) {
1170 /* One-line variable swap in JavaScript: http://stackoverflow.com/a/16201730 */
1171 bg = [fg, fg = bg][0];
1172 // Should inverse just be before the
1173 // above boldColors effect instead?
1174 if ((flags & 1) && fg < 8) fg += 8;
1177 if (flags & Terminal.flags.INVISIBLE) {
1178 classNames.push('xterm-hidden');
1182 * Weird situation: Invert flag used black foreground and white background results
1183 * in invalid background color, positioned at the 256 index of the 256 terminal
1184 * color map. Pin the colors manually in such a case.
1186 * Source: https://github.com/sourcelair/xterm.js/issues/57
1188 if (flags
& Terminal
.flags
.INVERSE
) {
1198 classNames
.push('xterm-bg-color-' + bg
);
1202 classNames
.push('xterm-color-' + fg
);
1206 if (classNames
.length
) {
1207 out
+= ' class="' + classNames
.join(' ') + '"';
1228 if (isWide(ch
)) i
++;
1237 if (attr
!== this.defAttr
) {
1241 this.children
[y
].innerHTML
= out
;
1245 parent
.appendChild(this.element
);
1249 * Return focus to previously focused element
1252 this.emit('refresh', {element
: this.element
, start
: start
, end
: end
});
1255 Terminal
.prototype._cursorBlink = function() {
1256 if (Terminal
.focus
!== this) return;
1257 this.cursorState
^= 1;
1258 this.refresh(this.y
, this.y
);
1261 Terminal
.prototype.showCursor = function() {
1262 if (!this.cursorState
) {
1263 this.cursorState
= 1;
1264 this.refresh(this.y
, this.y
);
1266 // Temporarily disabled:
1267 // this.refreshBlink();
1271 Terminal
.prototype.startBlink = function() {
1272 if (!this.cursorBlink
) return;
1274 this._blinker = function() {
1275 self
._cursorBlink();
1277 this._blink
= setInterval(this._blinker
, 500);
1280 Terminal
.prototype.refreshBlink = function() {
1281 if (!this.cursorBlink
) return;
1282 clearInterval(this._blink
);
1283 this._blink
= setInterval(this._blinker
, 500);
1286 Terminal
.prototype.scroll = function() {
1289 if (++this.ybase
=== this.scrollback
) {
1290 this.ybase
= this.ybase
/ 2 | 0;
1291 this.lines
= this.lines
.slice(-(this.ybase
+ this.rows
) + 1);
1294 this.ydisp
= this.ybase
;
1297 row
= this.ybase
+ this.rows
- 1;
1299 // subtract the bottom scroll region
1300 row
-= this.rows
- 1 - this.scrollBottom
;
1302 if (row
=== this.lines
.length
) {
1303 // potential optimization:
1304 // pushing is faster than splicing
1305 // when they amount to the same
1307 this.lines
.push(this.blankLine());
1310 this.lines
.splice(row
, 0, this.blankLine());
1313 if (this.scrollTop
!== 0) {
1314 if (this.ybase
!== 0) {
1316 this.ydisp
= this.ybase
;
1318 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
1322 this.updateRange(this.scrollTop
);
1323 this.updateRange(this.scrollBottom
);
1326 Terminal
.prototype.scrollDisp = function(disp
) {
1329 if (this.ydisp
> this.ybase
) {
1330 this.ydisp
= this.ybase
;
1331 } else if (this.ydisp
< 0) {
1335 this.refresh(0, this.rows
- 1);
1338 Terminal
.prototype.write = function(data
) {
1339 var l
= data
.length
, i
= 0, j
, cs
, ch
;
1341 this.refreshStart
= this.y
;
1342 this.refreshEnd
= this.y
;
1344 if (this.ybase
!== this.ydisp
) {
1345 this.ydisp
= this.ybase
;
1349 for (; i
< l
; i
++) {
1351 switch (this.state
) {
1362 if (this.convertEol
) {
1366 if (this.y
> this.scrollBottom
) {
1386 this.x
= this.nextStop();
1401 this.state
= escaped
;
1407 if (this.charset
&& this.charset
[ch
]) {
1408 ch
= this.charset
[ch
];
1411 if (this.x
>= this.cols
) {
1414 if (this.y
> this.scrollBottom
) {
1420 this.lines
[this.y
+ this.ybase
][this.x
] = [this.curAttr
, ch
];
1422 this.updateRange(this.y
);
1425 j
= this.y
+ this.ybase
;
1426 if (this.cols
< 2 || this.x
>= this.cols
) {
1427 this.lines
[j
][this.x
- 1] = [this.curAttr
, ' '];
1430 this.lines
[j
][this.x
] = [this.curAttr
, ' '];
1439 // ESC [ Control Sequence Introducer ( CSI is 0x9b).
1442 this.currentParam
= 0;
1446 // ESC ] Operating System Command ( OSC is 0x9d).
1449 this.currentParam
= 0;
1453 // ESC P Device Control String ( DCS is 0x90).
1456 this.currentParam
= 0;
1460 // ESC _ Application Program Command ( APC is 0x9f).
1462 this.state
= ignore
;
1465 // ESC ^ Privacy Message ( PM is 0x9e).
1467 this.state
= ignore
;
1470 // ESC c Full Reset (RIS).
1475 // ESC E Next Line ( NEL is 0x85).
1476 // ESC D Index ( IND is 0x84).
1484 // ESC M Reverse Index ( RI is 0x8d).
1486 this.reverseIndex();
1489 // ESC % Select default/utf-8 character set.
1490 // @ = default, G = utf-8
1492 //this.charset = null;
1494 this.setgCharset(0, Terminal
.charsets
.US
);
1495 this.state
= normal
;
1499 // ESC (,),*,+,-,. Designate G0-G2 Character Set.
1500 case '(': // <-- this seems to get all the attention
1526 this.state
= charset
;
1529 // Designate G3 Character Set (VT300).
1530 // A = ISO Latin-1 Supplemental.
1534 this.state
= charset
;
1539 // Single Shift Select of G2 Character Set
1540 // ( SS2 is 0x8e). This affects next character only.
1544 // Single Shift Select of G3 Character Set
1545 // ( SS3 is 0x8f). This affects next character only.
1549 // Invoke the G2 Character Set as GL (LS2).
1554 // Invoke the G3 Character Set as GL (LS3).
1559 // Invoke the G3 Character Set as GR (LS3R).
1564 // Invoke the G2 Character Set as GR (LS2R).
1569 // Invoke the G1 Character Set as GR (LS1R).
1574 // ESC 7 Save Cursor (DECSC).
1577 this.state
= normal
;
1580 // ESC 8 Restore Cursor (DECRC).
1582 this.restoreCursor();
1583 this.state
= normal
;
1586 // ESC # 3 DEC line height/width
1588 this.state
= normal
;
1592 // ESC H Tab Set (HTS is 0x88).
1597 // ESC = Application Keypad (DECPAM).
1599 this.log('Serial port requested application keypad.');
1600 this.applicationKeypad
= true;
1601 this.state
= normal
;
1604 // ESC > Normal Keypad (DECPNM).
1606 this.log('Switching back to normal keypad.');
1607 this.applicationKeypad
= false;
1608 this.state
= normal
;
1612 this.state
= normal
;
1613 this.error('Unknown ESC control: %s.', ch
);
1620 case '0': // DEC Special Character and Line Drawing Set.
1621 cs
= Terminal
.charsets
.SCLD
;
1624 cs
= Terminal
.charsets
.UK
;
1626 case 'B': // United States (USASCII).
1627 cs
= Terminal
.charsets
.US
;
1630 cs
= Terminal
.charsets
.Dutch
;
1632 case 'C': // Finnish
1634 cs
= Terminal
.charsets
.Finnish
;
1637 cs
= Terminal
.charsets
.French
;
1639 case 'Q': // FrenchCanadian
1640 cs
= Terminal
.charsets
.FrenchCanadian
;
1643 cs
= Terminal
.charsets
.German
;
1645 case 'Y': // Italian
1646 cs
= Terminal
.charsets
.Italian
;
1648 case 'E': // NorwegianDanish
1650 cs
= Terminal
.charsets
.NorwegianDanish
;
1652 case 'Z': // Spanish
1653 cs
= Terminal
.charsets
.Spanish
;
1655 case 'H': // Swedish
1657 cs
= Terminal
.charsets
.Swedish
;
1660 cs
= Terminal
.charsets
.Swiss
;
1662 case '/': // ISOLatin (actually /A)
1663 cs
= Terminal
.charsets
.ISOLatin
;
1667 cs
= Terminal
.charsets
.US
;
1670 this.setgCharset(this.gcharset
, cs
);
1671 this.gcharset
= null;
1672 this.state
= normal
;
1678 // Set Text Parameters.
1679 if (ch
=== '\x1b' || ch
=== '\x07') {
1680 if (ch
=== '\x1b') i
++;
1682 this.params
.push(this.currentParam
);
1684 switch (this.params
[0]) {
1688 if (this.params
[1]) {
1689 this.title
= this.params
[1];
1690 this.handleTitle(this.title
);
1698 // change dynamic colors
1710 // change dynamic ui colors
1722 // manipulate selection data
1740 this.currentParam
= 0;
1741 this.state
= normal
;
1743 if (!this.params
.length
) {
1744 if (ch
>= '0' && ch
<= '9') {
1746 this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1747 } else if (ch
=== ';') {
1748 this.params
.push(this.currentParam
);
1749 this.currentParam
= '';
1752 this.currentParam
+= ch
;
1759 if (ch
=== '?' || ch
=== '>' || ch
=== '!') {
1765 if (ch
>= '0' && ch
<= '9') {
1766 this.currentParam
= this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1770 // '$', '"', ' ', '\''
1771 if (ch
=== '$' || ch
=== '"' || ch
=== ' ' || ch
=== '\'') {
1776 this.params
.push(this.currentParam
);
1777 this.currentParam
= 0;
1780 if (ch
=== ';') break;
1782 this.state
= normal
;
1786 // Cursor Up Ps Times (default = 1) (CUU).
1788 this.cursorUp(this.params
);
1792 // Cursor Down Ps Times (default = 1) (CUD).
1794 this.cursorDown(this.params
);
1798 // Cursor Forward Ps Times (default = 1) (CUF).
1800 this.cursorForward(this.params
);
1804 // Cursor Backward Ps Times (default = 1) (CUB).
1806 this.cursorBackward(this.params
);
1810 // Cursor Position [row;column] (default = [1,1]) (CUP).
1812 this.cursorPos(this.params
);
1815 // CSI Ps J Erase in Display (ED).
1817 this.eraseInDisplay(this.params
);
1820 // CSI Ps K Erase in Line (EL).
1822 this.eraseInLine(this.params
);
1825 // CSI Pm m Character Attributes (SGR).
1828 this.charAttributes(this.params
);
1832 // CSI Ps n Device Status Report (DSR).
1835 this.deviceStatus(this.params
);
1844 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
1846 this.insertChars(this.params
);
1850 // Cursor Next Line Ps Times (default = 1) (CNL).
1852 this.cursorNextLine(this.params
);
1856 // Cursor Preceding Line Ps Times (default = 1) (CNL).
1858 this.cursorPrecedingLine(this.params
);
1862 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
1864 this.cursorCharAbsolute(this.params
);
1868 // Insert Ps Line(s) (default = 1) (IL).
1870 this.insertLines(this.params
);
1874 // Delete Ps Line(s) (default = 1) (DL).
1876 this.deleteLines(this.params
);
1880 // Delete Ps Character(s) (default = 1) (DCH).
1882 this.deleteChars(this.params
);
1886 // Erase Ps Character(s) (default = 1) (ECH).
1888 this.eraseChars(this.params
);
1891 // CSI Pm ` Character Position Absolute
1892 // [column] (default = [row,1]) (HPA).
1894 this.charPosAbsolute(this.params
);
1898 // Horizontal Position Relative
1900 this.HPositionRelative(this.params
);
1904 // Send Device Attributes (Primary DA).
1906 // Send Device Attributes (Secondary DA)
1908 this.sendDeviceAttributes(this.params
);
1912 // Line Position Absolute [row] (default = [1,column]) (VPA).
1914 this.linePosAbsolute(this.params
);
1917 // 145 65 e * VPR - Vertical Position Relative
1919 this.VPositionRelative(this.params
);
1923 // Horizontal and Vertical Position [row;column] (default =
1926 this.HVPosition(this.params
);
1929 // CSI Pm h Set Mode (SM).
1930 // CSI ? Pm h - mouse escape codes, cursor escape codes
1932 this.setMode(this.params
);
1935 // CSI Pm l Reset Mode (RM).
1938 this.resetMode(this.params
);
1942 // Set Scrolling Region [top;bottom] (default = full size of win-
1946 this.setScrollRegion(this.params
);
1950 // Save cursor (ANSI.SYS).
1952 this.saveCursor(this.params
);
1956 // Restore cursor (ANSI.SYS).
1958 this.restoreCursor(this.params
);
1966 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
1968 this.cursorForwardTab(this.params
);
1971 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
1973 this.scrollUp(this.params
);
1976 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
1977 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
1980 // if (this.prefix === '>') {
1981 // this.resetTitleModes(this.params);
1984 // if (this.params.length > 2) {
1985 // this.initMouseTracking(this.params);
1988 if (this.params
.length
< 2 && !this.prefix
) {
1989 this.scrollDown(this.params
);
1994 // Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
1996 this.cursorBackwardTab(this.params
);
1999 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
2001 this.repeatPrecedingCharacter(this.params
);
2004 // CSI Ps g Tab Clear (TBC).
2006 this.tabClear(this.params
);
2009 // CSI Pm i Media Copy (MC).
2012 // this.mediaCopy(this.params);
2015 // CSI Pm m Character Attributes (SGR).
2017 // case 'm': // duplicate
2018 // if (this.prefix === '>') {
2019 // this.setResources(this.params);
2021 // this.charAttributes(this.params);
2025 // CSI Ps n Device Status Report (DSR).
2027 // case 'n': // duplicate
2028 // if (this.prefix === '>') {
2029 // this.disableModifiers(this.params);
2031 // this.deviceStatus(this.params);
2035 // CSI > Ps p Set pointer mode.
2036 // CSI ! p Soft terminal reset (DECSTR).
2038 // Request ANSI mode (DECRQM).
2040 // Request DEC private mode (DECRQM).
2043 switch (this.prefix
) {
2045 // this.setPointerMode(this.params);
2048 this.softReset(this.params
);
2051 // if (this.postfix === '$') {
2052 // this.requestPrivateMode(this.params);
2056 // if (this.postfix === '"') {
2057 // this.setConformanceLevel(this.params);
2058 // } else if (this.postfix === '$') {
2059 // this.requestAnsiMode(this.params);
2065 // CSI Ps q Load LEDs (DECLL).
2069 // if (this.postfix === ' ') {
2070 // this.setCursorStyle(this.params);
2073 // if (this.postfix === '"') {
2074 // this.setCharProtectionAttr(this.params);
2077 // this.loadLEDs(this.params);
2081 // Set Scrolling Region [top;bottom] (default = full size of win-
2084 // CSI Pt; Pl; Pb; Pr; Ps$ r
2085 // case 'r': // duplicate
2086 // if (this.prefix === '?') {
2087 // this.restorePrivateValues(this.params);
2088 // } else if (this.postfix === '$') {
2089 // this.setAttrInRectangle(this.params);
2091 // this.setScrollRegion(this.params);
2095 // CSI s Save cursor (ANSI.SYS).
2097 // case 's': // duplicate
2098 // if (this.prefix === '?') {
2099 // this.savePrivateValues(this.params);
2101 // this.saveCursor(this.params);
2105 // CSI Ps ; Ps ; Ps t
2106 // CSI Pt; Pl; Pb; Pr; Ps$ t
2110 // if (this.postfix === '$') {
2111 // this.reverseAttrInRectangle(this.params);
2112 // } else if (this.postfix === ' ') {
2113 // this.setWarningBellVolume(this.params);
2115 // if (this.prefix === '>') {
2116 // this.setTitleModeFeature(this.params);
2118 // this.manipulateWindow(this.params);
2123 // CSI u Restore cursor (ANSI.SYS).
2125 // case 'u': // duplicate
2126 // if (this.postfix === ' ') {
2127 // this.setMarginBellVolume(this.params);
2129 // this.restoreCursor(this.params);
2133 // CSI Pt; Pl; Pb; Pr; Pp; Pt; Pl; Pp$ v
2135 // if (this.postfix === '$') {
2136 // this.copyRectagle(this.params);
2140 // CSI Pt ; Pl ; Pb ; Pr ' w
2142 // if (this.postfix === '\'') {
2143 // this.enableFilterRectangle(this.params);
2147 // CSI Ps x Request Terminal Parameters (DECREQTPARM).
2148 // CSI Ps x Select Attribute Change Extent (DECSACE).
2149 // CSI Pc; Pt; Pl; Pb; Pr$ x
2151 // if (this.postfix === '$') {
2152 // this.fillRectangle(this.params);
2154 // this.requestParameters(this.params);
2155 // //this.__(this.params);
2160 // CSI Pt; Pl; Pb; Pr$ z
2162 // if (this.postfix === '\'') {
2163 // this.enableLocatorReporting(this.params);
2164 // } else if (this.postfix === '$') {
2165 // this.eraseRectangle(this.params);
2170 // CSI Pt; Pl; Pb; Pr$ {
2172 // if (this.postfix === '\'') {
2173 // this.setLocatorEvents(this.params);
2174 // } else if (this.postfix === '$') {
2175 // this.selectiveEraseRectangle(this.params);
2181 // if (this.postfix === '\'') {
2182 // this.requestLocatorPosition(this.params);
2187 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
2189 // if (this.postfix === ' ') {
2190 // this.insertColumns(this.params);
2195 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
2197 // if (this.postfix === ' ') {
2198 // this.deleteColumns(this.params);
2203 this.error('Unknown CSI code: %s.', ch
);
2212 if (ch
=== '\x1b' || ch
=== '\x07') {
2213 if (ch
=== '\x1b') i
++;
2215 switch (this.prefix
) {
2216 // User-Defined Keys (DECUDK).
2220 // Request Status String (DECRQSS).
2221 // test: echo -e '\eP$q"p\e\\'
2223 var pt
= this.currentParam
2240 + (this.scrollTop
+ 1)
2242 + (this.scrollBottom
+ 1)
2252 this.error('Unknown DCS Pt: %s.', pt
);
2257 this.send('\x1bP' + +valid
+ '$r' + pt
+ '\x1b\\');
2260 // Set Termcap/Terminfo Data (xterm, experimental).
2264 // Request Termcap/Terminfo String (xterm, experimental)
2265 // Regular xterm does not even respond to this sequence.
2266 // This can cause a small glitch in vim.
2267 // test: echo -ne '\eP+q6b64\e\\'
2269 var pt
= this.currentParam
2272 this.send('\x1bP' + +valid
+ '+r' + pt
+ '\x1b\\');
2276 this.error('Unknown DCS prefix: %s.', this.prefix
);
2280 this.currentParam
= 0;
2282 this.state
= normal
;
2283 } else if (!this.currentParam
) {
2284 if (!this.prefix
&& ch
!== '$' && ch
!== '+') {
2285 this.currentParam
= ch
;
2286 } else if (this.prefix
.length
=== 2) {
2287 this.currentParam
= ch
;
2292 this.currentParam
+= ch
;
2298 if (ch
=== '\x1b' || ch
=== '\x07') {
2299 if (ch
=== '\x1b') i
++;
2300 this.state
= normal
;
2306 this.updateRange(this.y
);
2307 this.refresh(this.refreshStart
, this.refreshEnd
);
2310 Terminal
.prototype.writeln = function(data
) {
2311 this.write(data
+ '\r\n');
2315 // https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
2316 Terminal
.prototype.keyDown = function(ev
) {
2318 var result
= this.evaluateKeyEscapeSequence(ev
);
2320 if (result
.scrollDisp
) {
2321 this.scrollDisp(result
.scrollDisp
);
2322 return this.cancel(ev
);
2325 if (result
.cancel
) {
2326 // The event is canceled at the end already, is this necessary?
2327 this.cancel(ev
, true);
2330 if (!result
.key
|| (this.isMac
&& ev
.metaKey
)) {
2334 this.emit('keydown', ev
);
2335 this.emit('key', result
.key
, ev
);
2337 this.handler(result
.key
);
2339 return this.cancel(ev
, true);
2343 * Returns an object that determines how a KeyboardEvent should be handled. The key of the
2344 * returned value is the new key code to pass to the PTY.
2346 * Reference: http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
2348 Terminal
.prototype.evaluateKeyEscapeSequence = function(ev
) {
2350 // Whether to cancel event propogation (NOTE: this may not be needed since the event is
2351 // canceled at the end of keyDown
2353 // The new key even to emit
2355 // The number of characters to scroll, if this is defined it will cancel the event
2356 scrollDisp
: undefined
2358 switch (ev
.keyCode
) {
2362 result
.key
= '\x08'; // ^H
2365 result
.key
= '\x7f'; // ^?
2370 result
.key
= '\x1b[Z';
2374 result
.cancel
= true;
2379 result
.cancel
= true;
2383 result
.key
= '\x1b';
2384 result
.cancel
= true;
2389 result
.key
= '\x1bb' // Jump a word back
2390 result
.cancel
= true;
2394 result
.key
= '\x1b[5D'; // Jump a word back
2397 if (this.applicationCursor
) {
2398 result
.key
= '\x1bOD'; // SS3 as ^[O for 7-bit
2401 result
.key
= '\x1b[D';
2406 result
.key
= '\x1bf' // Jump a word forward
2407 result
.cancel
= true;
2411 result
.key
= '\x1b[5C'; // Jump a word forward
2414 if (this.applicationCursor
) {
2415 result
.key
= '\x1bOC';
2418 result
.key
= '\x1b[C';
2422 if (this.applicationCursor
) {
2423 result
.key
= '\x1bOA';
2427 result
.scrollDisp
= -1;
2429 result
.key
= '\x1b[A';
2434 if (this.applicationCursor
) {
2435 result
.key
= '\x1bOB';
2439 result
.scrollDisp
= 1;
2441 result
.key
= '\x1b[B';
2446 if (!ev
.shiftKey
&& !ev
.ctrlKey
) {
2447 // <Ctrl> or <Shift> + <Insert> are used to
2448 // copy-paste on some systems.
2449 result
.key
= '\x1b[2~';
2453 case 46: result
.key
= '\x1b[3~'; break;
2456 if (this.applicationKeypad
) {
2457 result
.key
= '\x1bOH';
2460 result
.key
= '\x1bOH';
2464 if (this.applicationKeypad
) {
2465 result
.key
= '\x1bOF';
2468 result
.key
= '\x1bOF';
2473 result
.scrollDisp
= -(this.rows
- 1);
2475 result
.key
= '\x1b[5~';
2481 result
.scrollDisp
= this.rows
- 1;
2483 result
.key
= '\x1b[6~';
2487 case 112: result
.key
= '\x1bOP'; break;
2488 case 113: result
.key
= '\x1bOQ'; break;
2489 case 114: result
.key
= '\x1bOR'; break;
2490 case 115: result
.key
= '\x1bOS'; break;
2491 case 116: result
.key
= '\x1b[15~'; break;
2492 case 117: result
.key
= '\x1b[17~'; break;
2493 case 118: result
.key
= '\x1b[18~'; break;
2494 case 119: result
.key
= '\x1b[19~'; break;
2495 case 120: result
.key
= '\x1b[20~'; break;
2496 case 121: result
.key
= '\x1b[21~'; break;
2497 case 122: result
.key
= '\x1b[23~'; break;
2498 case 123: result
.key
= '\x1b[24~'; break;
2501 if (ev
.ctrlKey
&& !ev
.shiftKey
&& !ev
.altKey
&& !ev
.metaKey
) {
2502 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2503 result
.key
= String
.fromCharCode(ev
.keyCode
- 64);
2504 } else if (ev
.keyCode
=== 32) {
2506 result
.key
= String
.fromCharCode(0);
2507 } else if (ev
.keyCode
>= 51 && ev
.keyCode
<= 55) {
2508 // escape, file sep, group sep, record sep, unit sep
2509 result
.key
= String
.fromCharCode(ev
.keyCode
- 51 + 27);
2510 } else if (ev
.keyCode
=== 56) {
2512 result
.key
= String
.fromCharCode(127);
2513 } else if (ev
.keyCode
=== 219) {
2515 result
.key
= String
.fromCharCode(27);
2516 } else if (ev
.keyCode
=== 221) {
2518 result
.key
= String
.fromCharCode(29);
2520 } else if ((!this.isMac
&& ev
.altKey
) || (this.isMac
&& ev
.metaKey
)) {
2521 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2522 result
.key
= '\x1b' + String
.fromCharCode(ev
.keyCode
+ 32);
2523 } else if (ev
.keyCode
=== 192) {
2524 result
.key
= '\x1b`';
2525 } else if (ev
.keyCode
>= 48 && ev
.keyCode
<= 57) {
2526 result
.key
= '\x1b' + (ev
.keyCode
- 48);
2534 Terminal
.prototype.setgLevel = function(g
) {
2536 this.charset
= this.charsets
[g
];
2539 Terminal
.prototype.setgCharset = function(g
, charset
) {
2540 this.charsets
[g
] = charset
;
2541 if (this.glevel
=== g
) {
2542 this.charset
= charset
;
2546 Terminal
.prototype.keyPress = function(ev
) {
2551 } else if (ev
.which
== null) {
2553 } else if (ev
.which
!== 0 && ev
.charCode
!== 0) {
2559 if (!key
|| ev
.ctrlKey
|| ev
.altKey
|| ev
.metaKey
) {
2563 key
= String
.fromCharCode(key
);
2566 * When a key is pressed and a character is sent to the terminal, then clear any text
2567 * selected in the terminal.
2570 this.clearSelection();
2573 this.emit('keypress', key
, ev
);
2574 this.emit('key', key
, ev
);
2578 this.cancel(ev
, true);
2583 Terminal
.prototype.send = function(data
) {
2587 setTimeout(function() {
2588 self
.handler(self
.queue
);
2596 Terminal
.prototype.bell = function() {
2597 if (!this.visualBell
) return;
2599 this.element
.style
.borderColor
= 'white';
2600 setTimeout(function() {
2601 self
.element
.style
.borderColor
= '';
2603 if (this.popOnBell
) this.focus();
2606 Terminal
.prototype.log = function() {
2607 if (!this.debug
) return;
2608 if (!this.context
.console
|| !this.context
.console
.log
) return;
2609 var args
= Array
.prototype.slice
.call(arguments
);
2610 this.context
.console
.log
.apply(this.context
.console
, args
);
2613 Terminal
.prototype.error = function() {
2614 if (!this.debug
) return;
2615 if (!this.context
.console
|| !this.context
.console
.error
) return;
2616 var args
= Array
.prototype.slice
.call(arguments
);
2617 this.context
.console
.error
.apply(this.context
.console
, args
);
2620 Terminal
.prototype.resize = function(x
, y
) {
2628 if (x
=== this.cols
&& y
=== this.rows
) {
2638 ch
= [this.defAttr
, ' ']; // does xterm use the default attr?
2639 i
= this.lines
.length
;
2641 while (this.lines
[i
].length
< x
) {
2642 this.lines
[i
].push(ch
);
2646 i
= this.lines
.length
;
2648 while (this.lines
[i
].length
> x
) {
2649 this.lines
[i
].pop();
2662 // y is rows, not this.y
2663 if (this.lines
.length
< y
+ this.ybase
) {
2664 if (this.ybase
> 0 && this.lines
.length
<= this.ybase
+ this.y
+ addToY
+ 1) {
2665 // There is room above the buffer and there are no empty elements below the line,
2671 // Add a blank line if there is no buffer left at the top to scroll to, or if there
2672 // are blank lines after the cursor
2673 this.lines
.push(this.blankLine());
2676 if (this.children
.length
< y
) {
2682 if (this.lines
.length
> y
+ this.ybase
) {
2683 if (this.lines
.length
> this.ybase
+ this.y
+ 1) {
2684 // The line is a blank line below the cursor, remove it
2687 // The line is the cursor, scroll down
2692 if (this.children
.length
> y
) {
2693 el
= this.children
.shift();
2695 el
.parentNode
.removeChild(el
);
2702 * Make sure that the cursor stays on screen
2716 this.scrollBottom
= y
- 1;
2718 this.refresh(0, this.rows
- 1);
2722 this.emit('resize', {terminal
: this, cols
: x
, rows
: y
});
2725 Terminal
.prototype.updateRange = function(y
) {
2726 if (y
< this.refreshStart
) this.refreshStart
= y
;
2727 if (y
> this.refreshEnd
) this.refreshEnd
= y
;
2728 // if (y > this.refreshEnd) {
2729 // this.refreshEnd = y;
2730 // if (y > this.rows - 1) {
2731 // this.refreshEnd = this.rows - 1;
2736 Terminal
.prototype.maxRange = function() {
2737 this.refreshStart
= 0;
2738 this.refreshEnd
= this.rows
- 1;
2741 Terminal
.prototype.setupStops = function(i
) {
2743 if (!this.tabs
[i
]) {
2744 i
= this.prevStop(i
);
2751 for (; i
< this.cols
; i
+= 8) {
2752 this.tabs
[i
] = true;
2756 Terminal
.prototype.prevStop = function(x
) {
2757 if (x
== null) x
= this.x
;
2758 while (!this.tabs
[--x
] && x
> 0);
2759 return x
>= this.cols
2764 Terminal
.prototype.nextStop = function(x
) {
2765 if (x
== null) x
= this.x
;
2766 while (!this.tabs
[++x
] && x
< this.cols
);
2767 return x
>= this.cols
2772 Terminal
.prototype.eraseRight = function(x
, y
) {
2773 var line
= this.lines
[this.ybase
+ y
]
2774 , ch
= [this.eraseAttr(), ' ']; // xterm
2777 for (; x
< this.cols
; x
++) {
2781 this.updateRange(y
);
2784 Terminal
.prototype.eraseLeft = function(x
, y
) {
2785 var line
= this.lines
[this.ybase
+ y
]
2786 , ch
= [this.eraseAttr(), ' ']; // xterm
2789 while (x
--) line
[x
] = ch
;
2791 this.updateRange(y
);
2794 Terminal
.prototype.eraseLine = function(y
) {
2795 this.eraseRight(0, y
);
2798 Terminal
.prototype.blankLine = function(cur
) {
2803 var ch
= [attr
, ' ']
2807 for (; i
< this.cols
; i
++) {
2814 Terminal
.prototype.ch = function(cur
) {
2816 ? [this.eraseAttr(), ' ']
2817 : [this.defAttr
, ' '];
2820 Terminal
.prototype.is = function(term
) {
2821 var name
= this.termName
;
2822 return (name
+ '').indexOf(term
) === 0;
2825 Terminal
.prototype.handler = function(data
) {
2826 this.emit('data', data
);
2829 Terminal
.prototype.handleTitle = function(title
) {
2830 this.emit('title', title
);
2837 // ESC D Index (IND is 0x84).
2838 Terminal
.prototype.index = function() {
2840 if (this.y
> this.scrollBottom
) {
2844 this.state
= normal
;
2847 // ESC M Reverse Index (RI is 0x8d).
2848 Terminal
.prototype.reverseIndex = function() {
2851 if (this.y
< this.scrollTop
) {
2853 // possibly move the code below to term.reverseScroll();
2854 // test: echo -ne '\e[1;1H\e[44m\eM\e[0m'
2855 // blankLine(true) is xterm/linux behavior
2856 this.lines
.splice(this.y
+ this.ybase
, 0, this.blankLine(true));
2857 j
= this.rows
- 1 - this.scrollBottom
;
2858 this.lines
.splice(this.rows
- 1 + this.ybase
- j
+ 1, 1);
2860 this.updateRange(this.scrollTop
);
2861 this.updateRange(this.scrollBottom
);
2863 this.state
= normal
;
2866 // ESC c Full Reset (RIS).
2867 Terminal
.prototype.reset = function() {
2868 this.options
.rows
= this.rows
;
2869 this.options
.cols
= this.cols
;
2870 Terminal
.call(this, this.options
);
2871 this.refresh(0, this.rows
- 1);
2874 // ESC H Tab Set (HTS is 0x88).
2875 Terminal
.prototype.tabSet = function() {
2876 this.tabs
[this.x
] = true;
2877 this.state
= normal
;
2885 // Cursor Up Ps Times (default = 1) (CUU).
2886 Terminal
.prototype.cursorUp = function(params
) {
2887 var param
= params
[0];
2888 if (param
< 1) param
= 1;
2890 if (this.y
< 0) this.y
= 0;
2894 // Cursor Down Ps Times (default = 1) (CUD).
2895 Terminal
.prototype.cursorDown = function(params
) {
2896 var param
= params
[0];
2897 if (param
< 1) param
= 1;
2899 if (this.y
>= this.rows
) {
2900 this.y
= this.rows
- 1;
2905 // Cursor Forward Ps Times (default = 1) (CUF).
2906 Terminal
.prototype.cursorForward = function(params
) {
2907 var param
= params
[0];
2908 if (param
< 1) param
= 1;
2910 if (this.x
>= this.cols
) {
2911 this.x
= this.cols
- 1;
2916 // Cursor Backward Ps Times (default = 1) (CUB).
2917 Terminal
.prototype.cursorBackward = function(params
) {
2918 var param
= params
[0];
2919 if (param
< 1) param
= 1;
2921 if (this.x
< 0) this.x
= 0;
2925 // Cursor Position [row;column] (default = [1,1]) (CUP).
2926 Terminal
.prototype.cursorPos = function(params
) {
2929 row
= params
[0] - 1;
2931 if (params
.length
>= 2) {
2932 col
= params
[1] - 1;
2939 } else if (row
>= this.rows
) {
2940 row
= this.rows
- 1;
2945 } else if (col
>= this.cols
) {
2946 col
= this.cols
- 1;
2953 // CSI Ps J Erase in Display (ED).
2954 // Ps = 0 -> Erase Below (default).
2955 // Ps = 1 -> Erase Above.
2956 // Ps = 2 -> Erase All.
2957 // Ps = 3 -> Erase Saved Lines (xterm).
2959 // Erase in Display (DECSED).
2960 // Ps = 0 -> Selective Erase Below (default).
2961 // Ps = 1 -> Selective Erase Above.
2962 // Ps = 2 -> Selective Erase All.
2963 Terminal
.prototype.eraseInDisplay = function(params
) {
2965 switch (params
[0]) {
2967 this.eraseRight(this.x
, this.y
);
2969 for (; j
< this.rows
; j
++) {
2974 this.eraseLeft(this.x
, this.y
);
2982 while (j
--) this.eraseLine(j
);
2990 // CSI Ps K Erase in Line (EL).
2991 // Ps = 0 -> Erase to Right (default).
2992 // Ps = 1 -> Erase to Left.
2993 // Ps = 2 -> Erase All.
2995 // Erase in Line (DECSEL).
2996 // Ps = 0 -> Selective Erase to Right (default).
2997 // Ps = 1 -> Selective Erase to Left.
2998 // Ps = 2 -> Selective Erase All.
2999 Terminal
.prototype.eraseInLine = function(params
) {
3000 switch (params
[0]) {
3002 this.eraseRight(this.x
, this.y
);
3005 this.eraseLeft(this.x
, this.y
);
3008 this.eraseLine(this.y
);
3013 // CSI Pm m Character Attributes (SGR).
3014 // Ps = 0 -> Normal (default).
3016 // Ps = 4 -> Underlined.
3017 // Ps = 5 -> Blink (appears as Bold).
3018 // Ps = 7 -> Inverse.
3019 // Ps = 8 -> Invisible, i.e., hidden (VT300).
3020 // Ps = 2 2 -> Normal (neither bold nor faint).
3021 // Ps = 2 4 -> Not underlined.
3022 // Ps = 2 5 -> Steady (not blinking).
3023 // Ps = 2 7 -> Positive (not inverse).
3024 // Ps = 2 8 -> Visible, i.e., not hidden (VT300).
3025 // Ps = 3 0 -> Set foreground color to Black.
3026 // Ps = 3 1 -> Set foreground color to Red.
3027 // Ps = 3 2 -> Set foreground color to Green.
3028 // Ps = 3 3 -> Set foreground color to Yellow.
3029 // Ps = 3 4 -> Set foreground color to Blue.
3030 // Ps = 3 5 -> Set foreground color to Magenta.
3031 // Ps = 3 6 -> Set foreground color to Cyan.
3032 // Ps = 3 7 -> Set foreground color to White.
3033 // Ps = 3 9 -> Set foreground color to default (original).
3034 // Ps = 4 0 -> Set background color to Black.
3035 // Ps = 4 1 -> Set background color to Red.
3036 // Ps = 4 2 -> Set background color to Green.
3037 // Ps = 4 3 -> Set background color to Yellow.
3038 // Ps = 4 4 -> Set background color to Blue.
3039 // Ps = 4 5 -> Set background color to Magenta.
3040 // Ps = 4 6 -> Set background color to Cyan.
3041 // Ps = 4 7 -> Set background color to White.
3042 // Ps = 4 9 -> Set background color to default (original).
3044 // If 16-color support is compiled, the following apply. Assume
3045 // that xterm's resources are set so that the ISO color codes are
3046 // the first 8 of a set of 16. Then the aixterm colors are the
3047 // bright versions of the ISO colors:
3048 // Ps = 9 0 -> Set foreground color to Black.
3049 // Ps = 9 1 -> Set foreground color to Red.
3050 // Ps = 9 2 -> Set foreground color to Green.
3051 // Ps = 9 3 -> Set foreground color to Yellow.
3052 // Ps = 9 4 -> Set foreground color to Blue.
3053 // Ps = 9 5 -> Set foreground color to Magenta.
3054 // Ps = 9 6 -> Set foreground color to Cyan.
3055 // Ps = 9 7 -> Set foreground color to White.
3056 // Ps = 1 0 0 -> Set background color to Black.
3057 // Ps = 1 0 1 -> Set background color to Red.
3058 // Ps = 1 0 2 -> Set background color to Green.
3059 // Ps = 1 0 3 -> Set background color to Yellow.
3060 // Ps = 1 0 4 -> Set background color to Blue.
3061 // Ps = 1 0 5 -> Set background color to Magenta.
3062 // Ps = 1 0 6 -> Set background color to Cyan.
3063 // Ps = 1 0 7 -> Set background color to White.
3065 // If xterm is compiled with the 16-color support disabled, it
3066 // supports the following, from rxvt:
3067 // Ps = 1 0 0 -> Set foreground and background color to
3070 // If 88- or 256-color support is compiled, the following apply.
3071 // Ps = 3 8 ; 5 ; Ps -> Set foreground color to the second
3073 // Ps = 4 8 ; 5 ; Ps -> Set background color to the second
3075 Terminal
.prototype.charAttributes = function(params
) {
3076 // Optimize a single SGR0.
3077 if (params
.length
=== 1 && params
[0] === 0) {
3078 this.curAttr
= this.defAttr
;
3082 var l
= params
.length
3084 , flags
= this.curAttr
>> 18
3085 , fg
= (this.curAttr
>> 9) & 0x1ff
3086 , bg
= this.curAttr
& 0x1ff
3089 for (; i
< l
; i
++) {
3091 if (p
>= 30 && p
<= 37) {
3094 } else if (p
>= 40 && p
<= 47) {
3097 } else if (p
>= 90 && p
<= 97) {
3101 } else if (p
>= 100 && p
<= 107) {
3105 } else if (p
=== 0) {
3107 flags
= this.defAttr
>> 18;
3108 fg
= (this.defAttr
>> 9) & 0x1ff;
3109 bg
= this.defAttr
& 0x1ff;
3113 } else if (p
=== 1) {
3116 } else if (p
=== 4) {
3119 } else if (p
=== 5) {
3122 } else if (p
=== 7) {
3123 // inverse and positive
3124 // test with: echo -e '\e[31m\e[42mhello\e[7mworld\e[27mhi\e[m'
3126 } else if (p
=== 8) {
3129 } else if (p
=== 22) {
3132 } else if (p
=== 24) {
3135 } else if (p
=== 25) {
3138 } else if (p
=== 27) {
3141 } else if (p
=== 28) {
3144 } else if (p
=== 39) {
3146 fg
= (this.defAttr
>> 9) & 0x1ff;
3147 } else if (p
=== 49) {
3149 bg
= this.defAttr
& 0x1ff;
3150 } else if (p
=== 38) {
3152 if (params
[i
+ 1] === 2) {
3156 params
[i
+ 1] & 0xff,
3157 params
[i
+ 2] & 0xff);
3158 if (fg
=== -1) fg
= 0x1ff;
3160 } else if (params
[i
+ 1] === 5) {
3162 p
= params
[i
] & 0xff;
3165 } else if (p
=== 48) {
3167 if (params
[i
+ 1] === 2) {
3171 params
[i
+ 1] & 0xff,
3172 params
[i
+ 2] & 0xff);
3173 if (bg
=== -1) bg
= 0x1ff;
3175 } else if (params
[i
+ 1] === 5) {
3177 p
= params
[i
] & 0xff;
3180 } else if (p
=== 100) {
3182 fg
= (this.defAttr
>> 9) & 0x1ff;
3183 bg
= this.defAttr
& 0x1ff;
3185 this.error('Unknown SGR attribute: %d.', p
);
3189 this.curAttr
= (flags
<< 18) | (fg
<< 9) | bg
;
3192 // CSI Ps n Device Status Report (DSR).
3193 // Ps = 5 -> Status Report. Result (``OK'') is
3195 // Ps = 6 -> Report Cursor Position (CPR) [row;column].
3199 // Device Status Report (DSR, DEC-specific).
3200 // Ps = 6 -> Report Cursor Position (CPR) [row;column] as CSI
3201 // ? r ; c R (assumes page is zero).
3202 // Ps = 1 5 -> Report Printer status as CSI ? 1 0 n (ready).
3203 // or CSI ? 1 1 n (not ready).
3204 // Ps = 2 5 -> Report UDK status as CSI ? 2 0 n (unlocked)
3205 // or CSI ? 2 1 n (locked).
3206 // Ps = 2 6 -> Report Keyboard status as
3207 // CSI ? 2 7 ; 1 ; 0 ; 0 n (North American).
3208 // The last two parameters apply to VT400 & up, and denote key-
3209 // board ready and LK01 respectively.
3210 // Ps = 5 3 -> Report Locator status as
3211 // CSI ? 5 3 n Locator available, if compiled-in, or
3212 // CSI ? 5 0 n No Locator, if not.
3213 Terminal
.prototype.deviceStatus = function(params
) {
3215 switch (params
[0]) {
3218 this.send('\x1b[0n');
3229 } else if (this.prefix
=== '?') {
3230 // modern xterm doesnt seem to
3231 // respond to any of these except ?6, 6, and 5
3232 switch (params
[0]) {
3243 // this.send('\x1b[?11n');
3246 // dont support user defined keys
3247 // this.send('\x1b[?21n');
3250 // north american keyboard
3251 // this.send('\x1b[?27;1;0;0n');
3254 // no dec locator/mouse
3255 // this.send('\x1b[?50n');
3266 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
3267 Terminal
.prototype.insertChars = function(params
) {
3268 var param
, row
, j
, ch
;
3271 if (param
< 1) param
= 1;
3273 row
= this.y
+ this.ybase
;
3275 ch
= [this.eraseAttr(), ' ']; // xterm
3277 while (param
-- && j
< this.cols
) {
3278 this.lines
[row
].splice(j
++, 0, ch
);
3279 this.lines
[row
].pop();
3284 // Cursor Next Line Ps Times (default = 1) (CNL).
3285 // same as CSI Ps B ?
3286 Terminal
.prototype.cursorNextLine = function(params
) {
3287 var param
= params
[0];
3288 if (param
< 1) param
= 1;
3290 if (this.y
>= this.rows
) {
3291 this.y
= this.rows
- 1;
3297 // Cursor Preceding Line Ps Times (default = 1) (CNL).
3299 Terminal
.prototype.cursorPrecedingLine = function(params
) {
3300 var param
= params
[0];
3301 if (param
< 1) param
= 1;
3303 if (this.y
< 0) this.y
= 0;
3308 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
3309 Terminal
.prototype.cursorCharAbsolute = function(params
) {
3310 var param
= params
[0];
3311 if (param
< 1) param
= 1;
3316 // Insert Ps Line(s) (default = 1) (IL).
3317 Terminal
.prototype.insertLines = function(params
) {
3321 if (param
< 1) param
= 1;
3322 row
= this.y
+ this.ybase
;
3324 j
= this.rows
- 1 - this.scrollBottom
;
3325 j
= this.rows
- 1 + this.ybase
- j
+ 1;
3328 // test: echo -e '\e[44m\e[1L\e[0m'
3329 // blankLine(true) - xterm/linux behavior
3330 this.lines
.splice(row
, 0, this.blankLine(true));
3331 this.lines
.splice(j
, 1);
3335 this.updateRange(this.y
);
3336 this.updateRange(this.scrollBottom
);
3340 // Delete Ps Line(s) (default = 1) (DL).
3341 Terminal
.prototype.deleteLines = function(params
) {
3345 if (param
< 1) param
= 1;
3346 row
= this.y
+ this.ybase
;
3348 j
= this.rows
- 1 - this.scrollBottom
;
3349 j
= this.rows
- 1 + this.ybase
- j
;
3352 // test: echo -e '\e[44m\e[1M\e[0m'
3353 // blankLine(true) - xterm/linux behavior
3354 this.lines
.splice(j
+ 1, 0, this.blankLine(true));
3355 this.lines
.splice(row
, 1);
3359 this.updateRange(this.y
);
3360 this.updateRange(this.scrollBottom
);
3364 // Delete Ps Character(s) (default = 1) (DCH).
3365 Terminal
.prototype.deleteChars = function(params
) {
3369 if (param
< 1) param
= 1;
3371 row
= this.y
+ this.ybase
;
3372 ch
= [this.eraseAttr(), ' ']; // xterm
3375 this.lines
[row
].splice(this.x
, 1);
3376 this.lines
[row
].push(ch
);
3381 // Erase Ps Character(s) (default = 1) (ECH).
3382 Terminal
.prototype.eraseChars = function(params
) {
3383 var param
, row
, j
, ch
;
3386 if (param
< 1) param
= 1;
3388 row
= this.y
+ this.ybase
;
3390 ch
= [this.eraseAttr(), ' ']; // xterm
3392 while (param
-- && j
< this.cols
) {
3393 this.lines
[row
][j
++] = ch
;
3397 // CSI Pm ` Character Position Absolute
3398 // [column] (default = [row,1]) (HPA).
3399 Terminal
.prototype.charPosAbsolute = function(params
) {
3400 var param
= params
[0];
3401 if (param
< 1) param
= 1;
3403 if (this.x
>= this.cols
) {
3404 this.x
= this.cols
- 1;
3409 // Horizontal Position Relative
3411 Terminal
.prototype.HPositionRelative = function(params
) {
3412 var param
= params
[0];
3413 if (param
< 1) param
= 1;
3415 if (this.x
>= this.cols
) {
3416 this.x
= this.cols
- 1;
3420 // CSI Ps c Send Device Attributes (Primary DA).
3421 // Ps = 0 or omitted -> request attributes from terminal. The
3422 // response depends on the decTerminalID resource setting.
3423 // -> CSI ? 1 ; 2 c (``VT100 with Advanced Video Option'')
3424 // -> CSI ? 1 ; 0 c (``VT101 with No Options'')
3425 // -> CSI ? 6 c (``VT102'')
3426 // -> CSI ? 6 0 ; 1 ; 2 ; 6 ; 8 ; 9 ; 1 5 ; c (``VT220'')
3427 // The VT100-style response parameters do not mean anything by
3428 // themselves. VT220 parameters do, telling the host what fea-
3429 // tures the terminal supports:
3430 // Ps = 1 -> 132-columns.
3431 // Ps = 2 -> Printer.
3432 // Ps = 6 -> Selective erase.
3433 // Ps = 8 -> User-defined keys.
3434 // Ps = 9 -> National replacement character sets.
3435 // Ps = 1 5 -> Technical characters.
3436 // Ps = 2 2 -> ANSI color, e.g., VT525.
3437 // Ps = 2 9 -> ANSI text locator (i.e., DEC Locator mode).
3439 // Send Device Attributes (Secondary DA).
3440 // Ps = 0 or omitted -> request the terminal's identification
3441 // code. The response depends on the decTerminalID resource set-
3442 // ting. It should apply only to VT220 and up, but xterm extends
3444 // -> CSI > Pp ; Pv ; Pc c
3445 // where Pp denotes the terminal type
3446 // Pp = 0 -> ``VT100''.
3447 // Pp = 1 -> ``VT220''.
3448 // and Pv is the firmware version (for xterm, this was originally
3449 // the XFree86 patch number, starting with 95). In a DEC termi-
3450 // nal, Pc indicates the ROM cartridge registration number and is
3452 // More information:
3453 // xterm/charproc.c - line 2012, for more information.
3454 // vim responds with ^[[?0c or ^[[?1c after the terminal's response (?)
3455 Terminal
.prototype.sendDeviceAttributes = function(params
) {
3456 if (params
[0] > 0) return;
3459 if (this.is('xterm')
3460 || this.is('rxvt-unicode')
3461 || this.is('screen')) {
3462 this.send('\x1b[?1;2c');
3463 } else if (this.is('linux')) {
3464 this.send('\x1b[?6c');
3466 } else if (this.prefix
=== '>') {
3468 // seem to spit this
3469 // out around ~370 times (?).
3470 if (this.is('xterm')) {
3471 this.send('\x1b[>0;276;0c');
3472 } else if (this.is('rxvt-unicode')) {
3473 this.send('\x1b[>85;95;0c');
3474 } else if (this.is('linux')) {
3475 // not supported by linux console.
3476 // linux console echoes parameters.
3477 this.send(params
[0] + 'c');
3478 } else if (this.is('screen')) {
3479 this.send('\x1b[>83;40003;0c');
3485 // Line Position Absolute [row] (default = [1,column]) (VPA).
3486 Terminal
.prototype.linePosAbsolute = function(params
) {
3487 var param
= params
[0];
3488 if (param
< 1) param
= 1;
3490 if (this.y
>= this.rows
) {
3491 this.y
= this.rows
- 1;
3495 // 145 65 e * VPR - Vertical Position Relative
3497 Terminal
.prototype.VPositionRelative = function(params
) {
3498 var param
= params
[0];
3499 if (param
< 1) param
= 1;
3501 if (this.y
>= this.rows
) {
3502 this.y
= this.rows
- 1;
3507 // Horizontal and Vertical Position [row;column] (default =
3509 Terminal
.prototype.HVPosition = function(params
) {
3510 if (params
[0] < 1) params
[0] = 1;
3511 if (params
[1] < 1) params
[1] = 1;
3513 this.y
= params
[0] - 1;
3514 if (this.y
>= this.rows
) {
3515 this.y
= this.rows
- 1;
3518 this.x
= params
[1] - 1;
3519 if (this.x
>= this.cols
) {
3520 this.x
= this.cols
- 1;
3524 // CSI Pm h Set Mode (SM).
3525 // Ps = 2 -> Keyboard Action Mode (AM).
3526 // Ps = 4 -> Insert Mode (IRM).
3527 // Ps = 1 2 -> Send/receive (SRM).
3528 // Ps = 2 0 -> Automatic Newline (LNM).
3530 // DEC Private Mode Set (DECSET).
3531 // Ps = 1 -> Application Cursor Keys (DECCKM).
3532 // Ps = 2 -> Designate USASCII for character sets G0-G3
3533 // (DECANM), and set VT100 mode.
3534 // Ps = 3 -> 132 Column Mode (DECCOLM).
3535 // Ps = 4 -> Smooth (Slow) Scroll (DECSCLM).
3536 // Ps = 5 -> Reverse Video (DECSCNM).
3537 // Ps = 6 -> Origin Mode (DECOM).
3538 // Ps = 7 -> Wraparound Mode (DECAWM).
3539 // Ps = 8 -> Auto-repeat Keys (DECARM).
3540 // Ps = 9 -> Send Mouse X & Y on button press. See the sec-
3541 // tion Mouse Tracking.
3542 // Ps = 1 0 -> Show toolbar (rxvt).
3543 // Ps = 1 2 -> Start Blinking Cursor (att610).
3544 // Ps = 1 8 -> Print form feed (DECPFF).
3545 // Ps = 1 9 -> Set print extent to full screen (DECPEX).
3546 // Ps = 2 5 -> Show Cursor (DECTCEM).
3547 // Ps = 3 0 -> Show scrollbar (rxvt).
3548 // Ps = 3 5 -> Enable font-shifting functions (rxvt).
3549 // Ps = 3 8 -> Enter Tektronix Mode (DECTEK).
3550 // Ps = 4 0 -> Allow 80 -> 132 Mode.
3551 // Ps = 4 1 -> more(1) fix (see curses resource).
3552 // Ps = 4 2 -> Enable Nation Replacement Character sets (DECN-
3554 // Ps = 4 4 -> Turn On Margin Bell.
3555 // Ps = 4 5 -> Reverse-wraparound Mode.
3556 // Ps = 4 6 -> Start Logging. This is normally disabled by a
3557 // compile-time option.
3558 // Ps = 4 7 -> Use Alternate Screen Buffer. (This may be dis-
3559 // abled by the titeInhibit resource).
3560 // Ps = 6 6 -> Application keypad (DECNKM).
3561 // Ps = 6 7 -> Backarrow key sends backspace (DECBKM).
3562 // Ps = 1 0 0 0 -> Send Mouse X & Y on button press and
3563 // release. See the section Mouse Tracking.
3564 // Ps = 1 0 0 1 -> Use Hilite Mouse Tracking.
3565 // Ps = 1 0 0 2 -> Use Cell Motion Mouse Tracking.
3566 // Ps = 1 0 0 3 -> Use All Motion Mouse Tracking.
3567 // Ps = 1 0 0 4 -> Send FocusIn/FocusOut events.
3568 // Ps = 1 0 0 5 -> Enable Extended Mouse Mode.
3569 // Ps = 1 0 1 0 -> Scroll to bottom on tty output (rxvt).
3570 // Ps = 1 0 1 1 -> Scroll to bottom on key press (rxvt).
3571 // Ps = 1 0 3 4 -> Interpret "meta" key, sets eighth bit.
3572 // (enables the eightBitInput resource).
3573 // Ps = 1 0 3 5 -> Enable special modifiers for Alt and Num-
3574 // Lock keys. (This enables the numLock resource).
3575 // Ps = 1 0 3 6 -> Send ESC when Meta modifies a key. (This
3576 // enables the metaSendsEscape resource).
3577 // Ps = 1 0 3 7 -> Send DEL from the editing-keypad Delete
3579 // Ps = 1 0 3 9 -> Send ESC when Alt modifies a key. (This
3580 // enables the altSendsEscape resource).
3581 // Ps = 1 0 4 0 -> Keep selection even if not highlighted.
3582 // (This enables the keepSelection resource).
3583 // Ps = 1 0 4 1 -> Use the CLIPBOARD selection. (This enables
3584 // the selectToClipboard resource).
3585 // Ps = 1 0 4 2 -> Enable Urgency window manager hint when
3586 // Control-G is received. (This enables the bellIsUrgent
3588 // Ps = 1 0 4 3 -> Enable raising of the window when Control-G
3589 // is received. (enables the popOnBell resource).
3590 // Ps = 1 0 4 7 -> Use Alternate Screen Buffer. (This may be
3591 // disabled by the titeInhibit resource).
3592 // Ps = 1 0 4 8 -> Save cursor as in DECSC. (This may be dis-
3593 // abled by the titeInhibit resource).
3594 // Ps = 1 0 4 9 -> Save cursor as in DECSC and use Alternate
3595 // Screen Buffer, clearing it first. (This may be disabled by
3596 // the titeInhibit resource). This combines the effects of the 1
3597 // 0 4 7 and 1 0 4 8 modes. Use this with terminfo-based
3598 // applications rather than the 4 7 mode.
3599 // Ps = 1 0 5 0 -> Set terminfo/termcap function-key mode.
3600 // Ps = 1 0 5 1 -> Set Sun function-key mode.
3601 // Ps = 1 0 5 2 -> Set HP function-key mode.
3602 // Ps = 1 0 5 3 -> Set SCO function-key mode.
3603 // Ps = 1 0 6 0 -> Set legacy keyboard emulation (X11R6).
3604 // Ps = 1 0 6 1 -> Set VT220 keyboard emulation.
3605 // Ps = 2 0 0 4 -> Set bracketed paste mode.
3607 // http://vt100.net/docs/vt220-rm/chapter4.html
3608 Terminal
.prototype.setMode = function(params
) {
3609 if (typeof params
=== 'object') {
3610 var l
= params
.length
3613 for (; i
< l
; i
++) {
3614 this.setMode(params
[i
]);
3623 this.insertMode
= true;
3626 //this.convertEol = true;
3629 } else if (this.prefix
=== '?') {
3632 this.applicationCursor
= true;
3635 this.setgCharset(0, Terminal
.charsets
.US
);
3636 this.setgCharset(1, Terminal
.charsets
.US
);
3637 this.setgCharset(2, Terminal
.charsets
.US
);
3638 this.setgCharset(3, Terminal
.charsets
.US
);
3639 // set VT100 mode here
3641 case 3: // 132 col mode
3642 this.savedCols
= this.cols
;
3643 this.resize(132, this.rows
);
3646 this.originMode
= true;
3649 this.wraparoundMode
= true;
3652 // this.cursorBlink = true;
3655 this.log('Serial port requested application keypad.');
3656 this.applicationKeypad
= true;
3658 case 9: // X10 Mouse
3659 // no release, no motion, no wheel, no modifiers.
3660 case 1000: // vt200 mouse
3662 // no modifiers, except control on the wheel.
3663 case 1002: // button event mouse
3664 case 1003: // any event mouse
3665 // any event - sends motion events,
3666 // even if there is no button held down.
3667 this.x10Mouse
= params
=== 9;
3668 this.vt200Mouse
= params
=== 1000;
3669 this.normalMouse
= params
> 1000;
3670 this.mouseEvents
= true;
3671 this.element
.style
.cursor
= 'default';
3672 this.log('Binding to mouse events.');
3674 case 1004: // send focusin/focusout events
3677 this.sendFocus
= true;
3679 case 1005: // utf8 ext mode mouse
3680 this.utfMouse
= true;
3681 // for wide terminals
3682 // simply encodes large values as utf8 characters
3684 case 1006: // sgr ext mode mouse
3685 this.sgrMouse
= true;
3686 // for wide terminals
3687 // does not add 32 to fields
3688 // press: ^[[<b;x;yM
3689 // release: ^[[<b;x;ym
3691 case 1015: // urxvt ext mode mouse
3692 this.urxvtMouse
= true;
3693 // for wide terminals
3694 // numbers for fields
3696 // motion: ^[[b;x;yT
3698 case 25: // show cursor
3699 this.cursorHidden
= false;
3701 case 1049: // alt screen buffer cursor
3702 //this.saveCursor();
3704 case 47: // alt screen buffer
3705 case 1047: // alt screen buffer
3713 scrollTop
: this.scrollTop
,
3714 scrollBottom
: this.scrollBottom
,
3716 // XXX save charset(s) here?
3717 // charset: this.charset,
3718 // glevel: this.glevel,
3719 // charsets: this.charsets
3722 this.normal
= normal
;
3730 // CSI Pm l Reset Mode (RM).
3731 // Ps = 2 -> Keyboard Action Mode (AM).
3732 // Ps = 4 -> Replace Mode (IRM).
3733 // Ps = 1 2 -> Send/receive (SRM).
3734 // Ps = 2 0 -> Normal Linefeed (LNM).
3736 // DEC Private Mode Reset (DECRST).
3737 // Ps = 1 -> Normal Cursor Keys (DECCKM).
3738 // Ps = 2 -> Designate VT52 mode (DECANM).
3739 // Ps = 3 -> 80 Column Mode (DECCOLM).
3740 // Ps = 4 -> Jump (Fast) Scroll (DECSCLM).
3741 // Ps = 5 -> Normal Video (DECSCNM).
3742 // Ps = 6 -> Normal Cursor Mode (DECOM).
3743 // Ps = 7 -> No Wraparound Mode (DECAWM).
3744 // Ps = 8 -> No Auto-repeat Keys (DECARM).
3745 // Ps = 9 -> Don't send Mouse X & Y on button press.
3746 // Ps = 1 0 -> Hide toolbar (rxvt).
3747 // Ps = 1 2 -> Stop Blinking Cursor (att610).
3748 // Ps = 1 8 -> Don't print form feed (DECPFF).
3749 // Ps = 1 9 -> Limit print to scrolling region (DECPEX).
3750 // Ps = 2 5 -> Hide Cursor (DECTCEM).
3751 // Ps = 3 0 -> Don't show scrollbar (rxvt).
3752 // Ps = 3 5 -> Disable font-shifting functions (rxvt).
3753 // Ps = 4 0 -> Disallow 80 -> 132 Mode.
3754 // Ps = 4 1 -> No more(1) fix (see curses resource).
3755 // Ps = 4 2 -> Disable Nation Replacement Character sets (DEC-
3757 // Ps = 4 4 -> Turn Off Margin Bell.
3758 // Ps = 4 5 -> No Reverse-wraparound Mode.
3759 // Ps = 4 6 -> Stop Logging. (This is normally disabled by a
3760 // compile-time option).
3761 // Ps = 4 7 -> Use Normal Screen Buffer.
3762 // Ps = 6 6 -> Numeric keypad (DECNKM).
3763 // Ps = 6 7 -> Backarrow key sends delete (DECBKM).
3764 // Ps = 1 0 0 0 -> Don't send Mouse X & Y on button press and
3765 // release. See the section Mouse Tracking.
3766 // Ps = 1 0 0 1 -> Don't use Hilite Mouse Tracking.
3767 // Ps = 1 0 0 2 -> Don't use Cell Motion Mouse Tracking.
3768 // Ps = 1 0 0 3 -> Don't use All Motion Mouse Tracking.
3769 // Ps = 1 0 0 4 -> Don't send FocusIn/FocusOut events.
3770 // Ps = 1 0 0 5 -> Disable Extended Mouse Mode.
3771 // Ps = 1 0 1 0 -> Don't scroll to bottom on tty output
3773 // Ps = 1 0 1 1 -> Don't scroll to bottom on key press (rxvt).
3774 // Ps = 1 0 3 4 -> Don't interpret "meta" key. (This disables
3775 // the eightBitInput resource).
3776 // Ps = 1 0 3 5 -> Disable special modifiers for Alt and Num-
3777 // Lock keys. (This disables the numLock resource).
3778 // Ps = 1 0 3 6 -> Don't send ESC when Meta modifies a key.
3779 // (This disables the metaSendsEscape resource).
3780 // Ps = 1 0 3 7 -> Send VT220 Remove from the editing-keypad
3782 // Ps = 1 0 3 9 -> Don't send ESC when Alt modifies a key.
3783 // (This disables the altSendsEscape resource).
3784 // Ps = 1 0 4 0 -> Do not keep selection when not highlighted.
3785 // (This disables the keepSelection resource).
3786 // Ps = 1 0 4 1 -> Use the PRIMARY selection. (This disables
3787 // the selectToClipboard resource).
3788 // Ps = 1 0 4 2 -> Disable Urgency window manager hint when
3789 // Control-G is received. (This disables the bellIsUrgent
3791 // Ps = 1 0 4 3 -> Disable raising of the window when Control-
3792 // G is received. (This disables the popOnBell resource).
3793 // Ps = 1 0 4 7 -> Use Normal Screen Buffer, clearing screen
3794 // first if in the Alternate Screen. (This may be disabled by
3795 // the titeInhibit resource).
3796 // Ps = 1 0 4 8 -> Restore cursor as in DECRC. (This may be
3797 // disabled by the titeInhibit resource).
3798 // Ps = 1 0 4 9 -> Use Normal Screen Buffer and restore cursor
3799 // as in DECRC. (This may be disabled by the titeInhibit
3800 // resource). This combines the effects of the 1 0 4 7 and 1 0
3801 // 4 8 modes. Use this with terminfo-based applications rather
3802 // than the 4 7 mode.
3803 // Ps = 1 0 5 0 -> Reset terminfo/termcap function-key mode.
3804 // Ps = 1 0 5 1 -> Reset Sun function-key mode.
3805 // Ps = 1 0 5 2 -> Reset HP function-key mode.
3806 // Ps = 1 0 5 3 -> Reset SCO function-key mode.
3807 // Ps = 1 0 6 0 -> Reset legacy keyboard emulation (X11R6).
3808 // Ps = 1 0 6 1 -> Reset keyboard emulation to Sun/PC style.
3809 // Ps = 2 0 0 4 -> Reset bracketed paste mode.
3810 Terminal
.prototype.resetMode = function(params
) {
3811 if (typeof params
=== 'object') {
3812 var l
= params
.length
3815 for (; i
< l
; i
++) {
3816 this.resetMode(params
[i
]);
3825 this.insertMode
= false;
3828 //this.convertEol = false;
3831 } else if (this.prefix
=== '?') {
3834 this.applicationCursor
= false;
3837 if (this.cols
=== 132 && this.savedCols
) {
3838 this.resize(this.savedCols
, this.rows
);
3840 delete this.savedCols
;
3843 this.originMode
= false;
3846 this.wraparoundMode
= false;
3849 // this.cursorBlink = false;
3852 this.log('Switching back to normal keypad.');
3853 this.applicationKeypad
= false;
3855 case 9: // X10 Mouse
3856 case 1000: // vt200 mouse
3857 case 1002: // button event mouse
3858 case 1003: // any event mouse
3859 this.x10Mouse
= false;
3860 this.vt200Mouse
= false;
3861 this.normalMouse
= false;
3862 this.mouseEvents
= false;
3863 this.element
.style
.cursor
= '';
3865 case 1004: // send focusin/focusout events
3866 this.sendFocus
= false;
3868 case 1005: // utf8 ext mode mouse
3869 this.utfMouse
= false;
3871 case 1006: // sgr ext mode mouse
3872 this.sgrMouse
= false;
3874 case 1015: // urxvt ext mode mouse
3875 this.urxvtMouse
= false;
3877 case 25: // hide cursor
3878 this.cursorHidden
= true;
3880 case 1049: // alt screen buffer cursor
3882 case 47: // normal screen buffer
3883 case 1047: // normal screen buffer - clearing it first
3885 this.lines
= this.normal
.lines
;
3886 this.ybase
= this.normal
.ybase
;
3887 this.ydisp
= this.normal
.ydisp
;
3888 this.x
= this.normal
.x
;
3889 this.y
= this.normal
.y
;
3890 this.scrollTop
= this.normal
.scrollTop
;
3891 this.scrollBottom
= this.normal
.scrollBottom
;
3892 this.tabs
= this.normal
.tabs
;
3894 // if (params === 1049) {
3895 // this.x = this.savedX;
3896 // this.y = this.savedY;
3898 this.refresh(0, this.rows
- 1);
3907 // Set Scrolling Region [top;bottom] (default = full size of win-
3910 Terminal
.prototype.setScrollRegion = function(params
) {
3911 if (this.prefix
) return;
3912 this.scrollTop
= (params
[0] || 1) - 1;
3913 this.scrollBottom
= (params
[1] || this.rows
) - 1;
3919 // Save cursor (ANSI.SYS).
3920 Terminal
.prototype.saveCursor = function(params
) {
3921 this.savedX
= this.x
;
3922 this.savedY
= this.y
;
3926 // Restore cursor (ANSI.SYS).
3927 Terminal
.prototype.restoreCursor = function(params
) {
3928 this.x
= this.savedX
|| 0;
3929 this.y
= this.savedY
|| 0;
3937 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
3938 Terminal
.prototype.cursorForwardTab = function(params
) {
3939 var param
= params
[0] || 1;
3941 this.x
= this.nextStop();
3945 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
3946 Terminal
.prototype.scrollUp = function(params
) {
3947 var param
= params
[0] || 1;
3949 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
3950 this.lines
.splice(this.ybase
+ this.scrollBottom
, 0, this.blankLine());
3953 this.updateRange(this.scrollTop
);
3954 this.updateRange(this.scrollBottom
);
3957 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
3958 Terminal
.prototype.scrollDown = function(params
) {
3959 var param
= params
[0] || 1;
3961 this.lines
.splice(this.ybase
+ this.scrollBottom
, 1);
3962 this.lines
.splice(this.ybase
+ this.scrollTop
, 0, this.blankLine());
3965 this.updateRange(this.scrollTop
);
3966 this.updateRange(this.scrollBottom
);
3969 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
3970 // Initiate highlight mouse tracking. Parameters are
3971 // [func;startx;starty;firstrow;lastrow]. See the section Mouse
3973 Terminal
.prototype.initMouseTracking = function(params
) {
3974 // Relevant: DECSET 1001
3978 // Reset one or more features of the title modes to the default
3979 // value. Normally, "reset" disables the feature. It is possi-
3980 // ble to disable the ability to reset features by compiling a
3981 // different default for the title modes into xterm.
3982 // Ps = 0 -> Do not set window/icon labels using hexadecimal.
3983 // Ps = 1 -> Do not query window/icon labels using hexadeci-
3985 // Ps = 2 -> Do not set window/icon labels using UTF-8.
3986 // Ps = 3 -> Do not query window/icon labels using UTF-8.
3987 // (See discussion of "Title Modes").
3988 Terminal
.prototype.resetTitleModes = function(params
) {
3992 // CSI Ps Z Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
3993 Terminal
.prototype.cursorBackwardTab = function(params
) {
3994 var param
= params
[0] || 1;
3996 this.x
= this.prevStop();
4000 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
4001 Terminal
.prototype.repeatPrecedingCharacter = function(params
) {
4002 var param
= params
[0] || 1
4003 , line
= this.lines
[this.ybase
+ this.y
]
4004 , ch
= line
[this.x
- 1] || [this.defAttr
, ' '];
4006 while (param
--) line
[this.x
++] = ch
;
4009 // CSI Ps g Tab Clear (TBC).
4010 // Ps = 0 -> Clear Current Column (default).
4011 // Ps = 3 -> Clear All.
4013 // Ps = 2 -> Clear Stops on Line.
4014 // http://vt100.net/annarbor/aaa-ug/section6.html
4015 Terminal
.prototype.tabClear = function(params
) {
4016 var param
= params
[0];
4018 delete this.tabs
[this.x
];
4019 } else if (param
=== 3) {
4024 // CSI Pm i Media Copy (MC).
4025 // Ps = 0 -> Print screen (default).
4026 // Ps = 4 -> Turn off printer controller mode.
4027 // Ps = 5 -> Turn on printer controller mode.
4029 // Media Copy (MC, DEC-specific).
4030 // Ps = 1 -> Print line containing cursor.
4031 // Ps = 4 -> Turn off autoprint mode.
4032 // Ps = 5 -> Turn on autoprint mode.
4033 // Ps = 1 0 -> Print composed display, ignores DECPEX.
4034 // Ps = 1 1 -> Print all pages.
4035 Terminal
.prototype.mediaCopy = function(params
) {
4040 // Set or reset resource-values used by xterm to decide whether
4041 // to construct escape sequences holding information about the
4042 // modifiers pressed with a given key. The first parameter iden-
4043 // tifies the resource to set/reset. The second parameter is the
4044 // value to assign to the resource. If the second parameter is
4045 // omitted, the resource is reset to its initial value.
4046 // Ps = 1 -> modifyCursorKeys.
4047 // Ps = 2 -> modifyFunctionKeys.
4048 // Ps = 4 -> modifyOtherKeys.
4049 // If no parameters are given, all resources are reset to their
4051 Terminal
.prototype.setResources = function(params
) {
4056 // Disable modifiers which may be enabled via the CSI > Ps; Ps m
4057 // sequence. This corresponds to a resource value of "-1", which
4058 // cannot be set with the other sequence. The parameter identi-
4059 // fies the resource to be disabled:
4060 // Ps = 1 -> modifyCursorKeys.
4061 // Ps = 2 -> modifyFunctionKeys.
4062 // Ps = 4 -> modifyOtherKeys.
4063 // If the parameter is omitted, modifyFunctionKeys is disabled.
4064 // When modifyFunctionKeys is disabled, xterm uses the modifier
4065 // keys to make an extended sequence of functions rather than
4066 // adding a parameter to each function key to denote the modi-
4068 Terminal
.prototype.disableModifiers = function(params
) {
4073 // Set resource value pointerMode. This is used by xterm to
4074 // decide whether to hide the pointer cursor as the user types.
4075 // Valid values for the parameter:
4076 // Ps = 0 -> never hide the pointer.
4077 // Ps = 1 -> hide if the mouse tracking mode is not enabled.
4078 // Ps = 2 -> always hide the pointer. If no parameter is
4079 // given, xterm uses the default, which is 1 .
4080 Terminal
.prototype.setPointerMode = function(params
) {
4084 // CSI ! p Soft terminal reset (DECSTR).
4085 // http://vt100.net/docs/vt220-rm/table4-10.html
4086 Terminal
.prototype.softReset = function(params
) {
4087 this.cursorHidden
= false;
4088 this.insertMode
= false;
4089 this.originMode
= false;
4090 this.wraparoundMode
= false; // autowrap
4091 this.applicationKeypad
= false; // ?
4092 this.applicationCursor
= false;
4094 this.scrollBottom
= this.rows
- 1;
4095 this.curAttr
= this.defAttr
;
4096 this.x
= this.y
= 0; // ?
4097 this.charset
= null;
4098 this.glevel
= 0; // ??
4099 this.charsets
= [null]; // ??
4103 // Request ANSI mode (DECRQM). For VT300 and up, reply is
4105 // where Ps is the mode number as in RM, and Pm is the mode
4107 // 0 - not recognized
4110 // 3 - permanently set
4111 // 4 - permanently reset
4112 Terminal
.prototype.requestAnsiMode = function(params
) {
4117 // Request DEC private mode (DECRQM). For VT300 and up, reply is
4119 // where Ps is the mode number as in DECSET, Pm is the mode value
4120 // as in the ANSI DECRQM.
4121 Terminal
.prototype.requestPrivateMode = function(params
) {
4126 // Set conformance level (DECSCL). Valid values for the first
4128 // Ps = 6 1 -> VT100.
4129 // Ps = 6 2 -> VT200.
4130 // Ps = 6 3 -> VT300.
4131 // Valid values for the second parameter:
4132 // Ps = 0 -> 8-bit controls.
4133 // Ps = 1 -> 7-bit controls (always set for VT100).
4134 // Ps = 2 -> 8-bit controls.
4135 Terminal
.prototype.setConformanceLevel = function(params
) {
4139 // CSI Ps q Load LEDs (DECLL).
4140 // Ps = 0 -> Clear all LEDS (default).
4141 // Ps = 1 -> Light Num Lock.
4142 // Ps = 2 -> Light Caps Lock.
4143 // Ps = 3 -> Light Scroll Lock.
4144 // Ps = 2 1 -> Extinguish Num Lock.
4145 // Ps = 2 2 -> Extinguish Caps Lock.
4146 // Ps = 2 3 -> Extinguish Scroll Lock.
4147 Terminal
.prototype.loadLEDs = function(params
) {
4152 // Set cursor style (DECSCUSR, VT520).
4153 // Ps = 0 -> blinking block.
4154 // Ps = 1 -> blinking block (default).
4155 // Ps = 2 -> steady block.
4156 // Ps = 3 -> blinking underline.
4157 // Ps = 4 -> steady underline.
4158 Terminal
.prototype.setCursorStyle = function(params
) {
4163 // Select character protection attribute (DECSCA). Valid values
4164 // for the parameter:
4165 // Ps = 0 -> DECSED and DECSEL can erase (default).
4166 // Ps = 1 -> DECSED and DECSEL cannot erase.
4167 // Ps = 2 -> DECSED and DECSEL can erase.
4168 Terminal
.prototype.setCharProtectionAttr = function(params
) {
4173 // Restore DEC Private Mode Values. The value of Ps previously
4174 // saved is restored. Ps values are the same as for DECSET.
4175 Terminal
.prototype.restorePrivateValues = function(params
) {
4179 // CSI Pt; Pl; Pb; Pr; Ps$ r
4180 // Change Attributes in Rectangular Area (DECCARA), VT400 and up.
4181 // Pt; Pl; Pb; Pr denotes the rectangle.
4182 // Ps denotes the SGR attributes to change: 0, 1, 4, 5, 7.
4183 // NOTE: xterm doesn't enable this code by default.
4184 Terminal
.prototype.setAttrInRectangle = function(params
) {
4194 for (; t
< b
+ 1; t
++) {
4195 line
= this.lines
[this.ybase
+ t
];
4196 for (i
= l
; i
< r
; i
++) {
4197 line
[i
] = [attr
, line
[i
][1]];
4202 this.updateRange(params
[0]);
4203 this.updateRange(params
[2]);
4207 // CSI Pc; Pt; Pl; Pb; Pr$ x
4208 // Fill Rectangular Area (DECFRA), VT420 and up.
4209 // Pc is the character to use.
4210 // Pt; Pl; Pb; Pr denotes the rectangle.
4211 // NOTE: xterm doesn't enable this code by default.
4212 Terminal
.prototype.fillRectangle = function(params
) {
4222 for (; t
< b
+ 1; t
++) {
4223 line
= this.lines
[this.ybase
+ t
];
4224 for (i
= l
; i
< r
; i
++) {
4225 line
[i
] = [line
[i
][0], String
.fromCharCode(ch
)];
4230 this.updateRange(params
[1]);
4231 this.updateRange(params
[3]);
4235 // Enable Locator Reporting (DECELR).
4236 // Valid values for the first parameter:
4237 // Ps = 0 -> Locator disabled (default).
4238 // Ps = 1 -> Locator enabled.
4239 // Ps = 2 -> Locator enabled for one report, then disabled.
4240 // The second parameter specifies the coordinate unit for locator
4242 // Valid values for the second parameter:
4243 // Pu = 0 <- or omitted -> default to character cells.
4244 // Pu = 1 <- device physical pixels.
4245 // Pu = 2 <- character cells.
4246 Terminal
.prototype.enableLocatorReporting = function(params
) {
4247 var val
= params
[0] > 0;
4248 //this.mouseEvents = val;
4249 //this.decLocator = val;
4252 // CSI Pt; Pl; Pb; Pr$ z
4253 // Erase Rectangular Area (DECERA), VT400 and up.
4254 // Pt; Pl; Pb; Pr denotes the rectangle.
4255 // NOTE: xterm doesn't enable this code by default.
4256 Terminal
.prototype.eraseRectangle = function(params
) {
4266 ch
= [this.eraseAttr(), ' ']; // xterm?
4268 for (; t
< b
+ 1; t
++) {
4269 line
= this.lines
[this.ybase
+ t
];
4270 for (i
= l
; i
< r
; i
++) {
4276 this.updateRange(params
[0]);
4277 this.updateRange(params
[2]);
4282 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
4283 // NOTE: xterm doesn't enable this code by default.
4284 Terminal
.prototype.insertColumns = function() {
4285 var param
= params
[0]
4286 , l
= this.ybase
+ this.rows
4287 , ch
= [this.eraseAttr(), ' '] // xterm?
4291 for (i
= this.ybase
; i
< l
; i
++) {
4292 this.lines
[i
].splice(this.x
+ 1, 0, ch
);
4293 this.lines
[i
].pop();
4302 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
4303 // NOTE: xterm doesn't enable this code by default.
4304 Terminal
.prototype.deleteColumns = function() {
4305 var param
= params
[0]
4306 , l
= this.ybase
+ this.rows
4307 , ch
= [this.eraseAttr(), ' '] // xterm?
4311 for (i
= this.ybase
; i
< l
; i
++) {
4312 this.lines
[i
].splice(this.x
, 1);
4313 this.lines
[i
].push(ch
);
4324 Terminal
.charsets
= {};
4326 // DEC Special Character and Line Drawing Set.
4327 // http://vt100.net/docs/vt102-ug/table5-13.html
4328 // A lot of curses apps use this if they see TERM=xterm.
4329 // testing: echo -e '\e(0a\e(B'
4330 // The xterm output sometimes seems to conflict with the
4331 // reference above. xterm seems in line with the reference
4332 // when running vttest however.
4333 // The table below now uses xterm's output from vttest.
4334 Terminal
.charsets
.SCLD
= { // (0
4335 '`': '\u25c6', // '◆'
4336 'a': '\u2592', // '▒'
4337 'b': '\u0009', // '\t'
4338 'c': '\u000c', // '\f'
4339 'd': '\u000d', // '\r'
4340 'e': '\u000a', // '\n'
4341 'f': '\u00b0', // '°'
4342 'g': '\u00b1', // '±'
4343 'h': '\u2424', // '\u2424' (NL)
4344 'i': '\u000b', // '\v'
4345 'j': '\u2518', // '┘'
4346 'k': '\u2510', // '┐'
4347 'l': '\u250c', // '┌'
4348 'm': '\u2514', // '└'
4349 'n': '\u253c', // '┼'
4350 'o': '\u23ba', // '⎺'
4351 'p': '\u23bb', // '⎻'
4352 'q': '\u2500', // '─'
4353 'r': '\u23bc', // '⎼'
4354 's': '\u23bd', // '⎽'
4355 't': '\u251c', // '├'
4356 'u': '\u2524', // '┤'
4357 'v': '\u2534', // '┴'
4358 'w': '\u252c', // '┬'
4359 'x': '\u2502', // '│'
4360 'y': '\u2264', // '≤'
4361 'z': '\u2265', // '≥'
4362 '{': '\u03c0', // 'π'
4363 '|': '\u2260', // '≠'
4364 '}': '\u00a3', // '£'
4365 '~': '\u00b7' // '·'
4368 Terminal
.charsets
.UK
= null; // (A
4369 Terminal
.charsets
.US
= null; // (B (USASCII)
4370 Terminal
.charsets
.Dutch
= null; // (4
4371 Terminal
.charsets
.Finnish
= null; // (C or (5
4372 Terminal
.charsets
.French
= null; // (R
4373 Terminal
.charsets
.FrenchCanadian
= null; // (Q
4374 Terminal
.charsets
.German
= null; // (K
4375 Terminal
.charsets
.Italian
= null; // (Y
4376 Terminal
.charsets
.NorwegianDanish
= null; // (E or (6
4377 Terminal
.charsets
.Spanish
= null; // (Z
4378 Terminal
.charsets
.Swedish
= null; // (H or (7
4379 Terminal
.charsets
.Swiss
= null; // (=
4380 Terminal
.charsets
.ISOLatin
= null; // /A
4386 function on(el
, type
, handler
, capture
) {
4387 if (!Array
.isArray(el
)) {
4390 el
.forEach(function (element
) {
4391 element
.addEventListener(type
, handler
, capture
|| false);
4395 function off(el
, type
, handler
, capture
) {
4396 el
.removeEventListener(type
, handler
, capture
|| false);
4399 function cancel(ev
, force
) {
4400 if (!this.cancelEvents
&& !force
) {
4403 ev
.preventDefault();
4404 ev
.stopPropagation();
4408 function inherits(child
, parent
) {
4410 this.constructor = child
;
4412 f
.prototype = parent
.prototype;
4413 child
.prototype = new f
;
4416 // if bold is broken, we can't
4417 // use it in the terminal.
4418 function isBoldBroken(document
) {
4419 var body
= document
.getElementsByTagName('body')[0];
4420 var el
= document
.createElement('span');
4421 el
.innerHTML
= 'hello world';
4422 body
.appendChild(el
);
4423 var w1
= el
.scrollWidth
;
4424 el
.style
.fontWeight
= 'bold';
4425 var w2
= el
.scrollWidth
;
4426 body
.removeChild(el
);
4430 var String
= this.String
;
4431 var setTimeout
= this.setTimeout
;
4432 var setInterval
= this.setInterval
;
4434 function indexOf(obj
, el
) {
4437 if (obj
[i
] === el
) return i
;
4442 function isWide(ch
) {
4443 if (ch
<= '\uff00') return false;
4444 return (ch
>= '\uff01' && ch
<= '\uffbe')
4445 || (ch
>= '\uffc2' && ch
<= '\uffc7')
4446 || (ch
>= '\uffca' && ch
<= '\uffcf')
4447 || (ch
>= '\uffd2' && ch
<= '\uffd7')
4448 || (ch
>= '\uffda' && ch
<= '\uffdc')
4449 || (ch
>= '\uffe0' && ch
<= '\uffe6')
4450 || (ch
>= '\uffe8' && ch
<= '\uffee');
4453 function matchColor(r1
, g1
, b1
) {
4454 var hash
= (r1
<< 16) | (g1
<< 8) | b1
;
4456 if (matchColor
._cache
[hash
] != null) {
4457 return matchColor
._cache
[hash
];
4460 var ldiff
= Infinity
4469 for (; i
< Terminal
.vcolors
.length
; i
++) {
4470 c
= Terminal
.vcolors
[i
];
4475 diff
= matchColor
.distance(r1
, g1
, b1
, r2
, g2
, b2
);
4488 return matchColor
._cache
[hash
] = li
;
4491 matchColor
._cache
= {};
4493 // http://stackoverflow.com/questions/1633828
4494 matchColor
.distance = function(r1
, g1
, b1
, r2
, g2
, b2
) {
4495 return Math
.pow(30 * (r1
- r2
), 2)
4496 + Math
.pow(59 * (g1
- g2
), 2)
4497 + Math
.pow(11 * (b1
- b2
), 2);
4500 function each(obj
, iter
, con
) {
4501 if (obj
.forEach
) return obj
.forEach(iter
, con
);
4502 for (var i
= 0; i
< obj
.length
; i
++) {
4503 iter
.call(con
, obj
[i
], i
, obj
);
4507 function keys(obj
) {
4508 if (Object
.keys
) return Object
.keys(obj
);
4511 if (Object
.prototype.hasOwnProperty
.call(obj
, key
)) {
4522 Terminal
.EventEmitter
= EventEmitter
;
4523 Terminal
.inherits
= inherits
;
4526 Terminal
.cancel
= cancel
;