]>
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 define
== 'function') {
37 * Require.js is available
39 define([], xterm
.bind(window
));
42 * Plain browser environment
44 this.Xterm
= xterm
.call(this);
45 this.Terminal
= this.Xterm
; /* Backwards compatibility with term.js */
49 * Terminal Emulation References:
51 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.txt
52 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
53 * http://invisible-island.net/vttest/
54 * http://www.inwap.com/pdp10/ansicode.txt
55 * http://linux.die.net/man/4/console_codes
56 * http://linux.die.net/man/7/urxvt
65 var window
= this, document
= this.document
;
71 function EventEmitter() {
72 this._events
= this._events
|| {};
75 EventEmitter
.prototype.addListener = function(type
, listener
) {
76 this._events
[type
] = this._events
[type
] || [];
77 this._events
[type
].push(listener
);
80 EventEmitter
.prototype.on
= EventEmitter
.prototype.addListener
;
82 EventEmitter
.prototype.removeListener = function(type
, listener
) {
83 if (!this._events
[type
]) return;
85 var obj
= this._events
[type
]
89 if (obj
[i
] === listener
|| obj
[i
].listener
=== listener
) {
96 EventEmitter
.prototype.off
= EventEmitter
.prototype.removeListener
;
98 EventEmitter
.prototype.removeAllListeners = function(type
) {
99 if (this._events
[type
]) delete this._events
[type
];
102 EventEmitter
.prototype.once = function(type
, listener
) {
104 var args
= Array
.prototype.slice
.call(arguments
);
105 this.removeListener(type
, on
);
106 return listener
.apply(this, args
);
108 on
.listener
= listener
;
109 return this.on(type
, on
);
112 EventEmitter
.prototype.emit = function(type
) {
113 if (!this._events
[type
]) return;
115 var args
= Array
.prototype.slice
.call(arguments
, 1)
116 , obj
= this._events
[type
]
121 obj
[i
].apply(this, args
);
125 EventEmitter
.prototype.listeners = function(type
) {
126 return this._events
[type
] = this._events
[type
] || [];
133 var normal
= 0, escaped
= 1, csi
= 2, osc
= 3, charset
= 4, dcs
= 5, ignore
= 6;
139 function Terminal(options
) {
142 if (!(this instanceof Terminal
)) {
143 return new Terminal(arguments
[0], arguments
[1], arguments
[2]);
146 self
.cancel
= Terminal
.cancel
;
148 EventEmitter
.call(this);
150 if (typeof options
=== 'number') {
154 handler
: arguments
[2]
158 options
= options
|| {};
161 Object
.keys(Terminal
.defaults
).forEach(function(key
) {
162 if (options
[key
] == null) {
163 options
[key
] = Terminal
.options
[key
];
165 if (Terminal
[key
] !== Terminal
.defaults
[key
]) {
166 options
[key
] = Terminal
[key
];
169 self
[key
] = options
[key
];
172 if (options
.colors
.length
=== 8) {
173 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(8));
174 } else if (options
.colors
.length
=== 16) {
175 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(16));
176 } else if (options
.colors
.length
=== 10) {
177 options
.colors
= options
.colors
.slice(0, -2).concat(
178 Terminal
._colors
.slice(8, -2), options
.colors
.slice(-2));
179 } else if (options
.colors
.length
=== 18) {
180 options
.colors
= options
.colors
.concat(
181 Terminal
._colors
.slice(16, -2), options
.colors
.slice(-2));
183 this.colors
= options
.colors
;
185 this.options
= options
;
187 // this.context = options.context || window;
188 // this.document = options.document || document;
189 this.parent
= options
.body
|| options
.parent
190 || (document
? document
.getElementsByTagName('body')[0] : null);
192 this.cols
= options
.cols
|| options
.geometry
[0];
193 this.rows
= options
.rows
|| options
.geometry
[1];
195 if (options
.handler
) {
196 this.on('data', options
.handler
);
203 this.cursorState
= 0;
204 this.cursorHidden
= false;
209 this.scrollBottom
= this.rows
- 1;
212 this.applicationKeypad
= false;
213 this.applicationCursor
= false;
214 this.originMode
= false;
215 this.insertMode
= false;
216 this.wraparoundMode
= false;
221 this.gcharset
= null;
223 this.charsets
= [null];
247 this.readable
= true;
248 this.writable
= true;
250 this.defAttr
= (0 << 18) | (257 << 9) | (256 << 0);
251 this.curAttr
= this.defAttr
;
254 this.currentParam
= 0;
261 this.lines
.push(this.blankLine());
268 inherits(Terminal
, EventEmitter
);
270 // back_color_erase feature for xterm.
271 Terminal
.prototype.eraseAttr = function() {
272 // if (this.is('screen')) return this.defAttr;
273 return (this.defAttr
& ~0x1ff) | (this.curAttr
& 0x1ff);
281 Terminal
.tangoColors
= [
302 // Colors 0-15 + 16-255
303 // Much thanks to TooTallNate for writing this.
304 Terminal
.colors
= (function() {
305 var colors
= Terminal
.tangoColors
.slice()
306 , r
= [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff]
311 for (; i
< 216; i
++) {
312 out(r
[(i
/ 36) % 6 | 0], r
[(i
/ 6) % 6 | 0], r
[i
% 6]);
317 for (; i
< 24; i
++) {
322 function out(r
, g
, b
) {
323 colors
.push('#' + hex(r
) + hex(g
) + hex(b
));
328 return c
.length
< 2 ? '0' + c
: c
;
334 Terminal
._colors
= Terminal
.colors
.slice();
336 Terminal
.vcolors
= (function() {
338 , colors
= Terminal
.colors
342 for (; i
< 256; i
++) {
343 color
= parseInt(colors
[i
].substring(1), 16);
345 (color
>> 16) & 0xff,
358 Terminal
.defaults
= {
359 colors
: Terminal
.colors
,
371 // programFeatures: false,
375 Terminal
.options
= {};
377 each(keys(Terminal
.defaults
), function(key
) {
378 Terminal
[key
] = Terminal
.defaults
[key
];
379 Terminal
.options
[key
] = Terminal
.defaults
[key
];
385 Terminal
.prototype.focus = function() {
386 if (document
.activeElement
=== this.element
) {
390 if (this.sendFocus
) {
395 this.element
.focus();
399 Terminal
.prototype.blur = function() {
400 if (Terminal
.focus
!== this) {
404 this.cursorState
= 0;
405 this.refresh(this.y
, this.y
);
408 if (this.sendFocus
) {
414 * Initialize default behavior
417 Terminal
.prototype.initGlobal = function() {
418 Terminal
.bindPaste(this);
419 Terminal
.bindKeys(this);
420 Terminal
.bindCopy(this);
421 Terminal
.bindDrop(this);
425 * Bind to paste event
427 Terminal
.bindPaste = function(term
) {
428 on(term
.element
, 'paste', function(ev
) {
429 if (ev
.clipboardData
) {
430 var text
= ev
.clipboardData
.getData('text/plain');
433 return term
.cancel(ev
, true);
439 * Apply key handling to the terminal
441 Terminal
.bindKeys = function(term
) {
442 on(term
.element
, 'keydown', function(ev
) {
446 on(term
.element
, 'keypress', function(ev
) {
455 Terminal
.bindCopy = function(term
) {
456 on(term
.element
, 'copy', function(ev
) {
462 Terminal
.bindDrop = function (term
) {
464 * Do not perform the "drop" event. Altering the contents of the
465 * terminal with drag n drop is unwanted behavior.
467 on(term
.element
, 'drop', function (ev
) {
468 term
.cancel(ev
, true);
474 * Insert the given row to the terminal or produce a new one
475 * if no row argument is passed. Return the inserted row.
477 Terminal
.prototype.insertRow = function (row
) {
478 if (typeof row
!= 'object') {
479 row
= document
.createElement('div');
482 this.rowContainer
.appendChild(row
);
483 this.children
.push(row
);
490 * Open Terminal in the DOM
492 Terminal
.prototype.open = function(parent
) {
493 var self
=this, i
=0, div
;
495 this.parent
= parent
|| this.parent
;
498 throw new Error('Terminal requires a parent element.');
502 * Grab global elements
504 this.context
= this.parent
.ownerDocument
.defaultView
;
505 this.document
= this.parent
.ownerDocument
;
506 this.body
= this.document
.getElementsByTagName('body')[0];
511 if (this.context
.navigator
&& this.context
.navigator
.userAgent
) {
512 this.isMac
= !!~this.context
.navigator
.userAgent
.indexOf('Mac');
513 this.isIpad
= !!~this.context
.navigator
.userAgent
.indexOf('iPad');
514 this.isIphone
= !!~this.context
.navigator
.userAgent
.indexOf('iPhone');
515 this.isMSIE
= !!~this.context
.navigator
.userAgent
.indexOf('MSIE');
519 * Create main element container
521 this.element
= this.document
.createElement('div');
522 this.element
.classList
.add('terminal');
523 this.element
.classList
.add('xterm');
524 this.element
.classList
.add('xterm-theme-' + this.theme
);
525 this.element
.setAttribute('tabindex', 0);
526 this.element
.contentEditable
= 'true';
527 this.element
.spellcheck
= 'false';
530 * Create the container that will hold the lines of the terminal and then
531 * produce the lines the lines.
533 this.rowContainer
= document
.createElement('div');
534 this.rowContainer
.classList
.add('xterm-rows');
535 this.element
.appendChild(this.rowContainer
);
538 for (; i
< this.rows
; i
++) {
541 this.parent
.appendChild(this.element
);
544 this.refresh(0, this.rows
- 1);
546 // Initialize global actions that
547 // need to be taken on the document.
550 // Ensure there is a Terminal.focus.
553 // Start blinking the cursor.
556 on(this.element
, 'mouseup', function() {
557 var selection
= document
.getSelection(),
558 collapsed
= selection
.isCollapsed
,
559 isRange
= typeof collapsed
== 'boolean' ? !collapsed
: selection
.type
== 'Range';
565 // Listen for mouse events and translate
566 // them into terminal mouse protocols.
569 // Figure out whether boldness affects
570 // the character width of monospace fonts.
571 if (Terminal
.brokenBold
== null) {
572 Terminal
.brokenBold
= isBoldBroken(this.document
);
578 // XTerm mouse events
579 // http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#Mouse%20Tracking
580 // To better understand these
581 // the xterm code is very helpful:
583 // button.c, charproc.c, misc.c
584 // Relevant functions in xterm/button.c:
585 // BtnCode, EmitButtonCode, EditorButton, SendMousePosition
586 Terminal
.prototype.bindMouse = function() {
587 var el
= this.element
591 var wheelEvent
= 'onmousewheel' in this.context
595 // mouseup, mousedown, mousewheel
596 // left click: ^[[M 3<^[[M#3<
597 // mousewheel up: ^[[M`3>
598 function sendButton(ev
) {
602 // get the xterm-style button
603 button
= getButton(ev
);
605 // get mouse coordinates
609 sendEvent(button
, pos
);
616 // keep it at the left
617 // button, just in case.
628 // motion example of a left click:
629 // ^[[M 3<^[[M@4<^[[M@5<^[[M@6<^[[M@7<^[[M#7<
630 function sendMove(ev
) {
637 // buttons marked as motions
638 // are incremented by 32
641 sendEvent(button
, pos
);
645 // position to characters
646 function encode(data
, ch
) {
647 if (!self
.utfMouse
) {
648 if (ch
=== 255) return data
.push(0);
649 if (ch
> 127) ch
= 127;
652 if (ch
=== 2047) return data
.push(0);
656 if (ch
> 2047) ch
= 2047;
657 data
.push(0xC0 | (ch
>> 6));
658 data
.push(0x80 | (ch
& 0x3F));
663 // send a mouse event:
664 // regular/utf8: ^[[M Cb Cx Cy
665 // urxvt: ^[[ Cb ; Cx ; Cy M
666 // sgr: ^[[ Cb ; Cx ; Cy M/m
667 // vt300: ^[[ 24(1/3/5)~ [ Cx , Cy ] \r
668 // locator: CSI P e ; P b ; P r ; P c ; P p & w
669 function sendEvent(button
, pos
) {
670 // self.emit('mouse', {
676 if (self
.vt300Mouse
) {
678 // http://www.vt100.net/docs/vt3xx-gp/chapter15.html
682 var data
= '\x1b[24';
683 if (button
=== 0) data
+= '1';
684 else if (button
=== 1) data
+= '3';
685 else if (button
=== 2) data
+= '5';
686 else if (button
=== 3) return;
688 data
+= '~[' + pos
.x
+ ',' + pos
.y
+ ']\r';
693 if (self
.decLocator
) {
698 if (button
=== 0) button
= 2;
699 else if (button
=== 1) button
= 4;
700 else if (button
=== 2) button
= 6;
701 else if (button
=== 3) button
= 3;
705 + (button
=== 3 ? 4 : 0)
716 if (self
.urxvtMouse
) {
721 self
.send('\x1b[' + button
+ ';' + pos
.x
+ ';' + pos
.y
+ 'M');
729 + ((button
& 3) === 3 ? button
& ~3 : button
)
734 + ((button
& 3) === 3 ? 'm' : 'M'));
740 encode(data
, button
);
744 self
.send('\x1b[M' + String
.fromCharCode
.apply(String
, data
));
747 function getButton(ev
) {
760 // 1, and 2 - with 64 added
763 button
= ev
.button
!= null
770 button
= button
=== 1 ? 0 : button
=== 4 ? 1 : button
;
776 case 'DOMMouseScroll':
777 button
= ev
.detail
< 0
782 button
= ev
.wheelDeltaY
> 0
788 // next three bits are the modifiers:
789 // 4 = shift, 8 = meta, 16 = control
790 shift
= ev
.shiftKey
? 4 : 0;
791 meta
= ev
.metaKey
? 8 : 0;
792 ctrl
= ev
.ctrlKey
? 16 : 0;
793 mod
= shift
| meta
| ctrl
;
796 if (self
.vt200Mouse
) {
799 } else if (!self
.normalMouse
) {
804 button
= (32 + (mod
<< 2)) + button
;
809 // mouse coordinates measured in cols/rows
810 function getCoords(ev
) {
813 // ignore browsers without pageX for now
814 if (ev
.pageX
== null) return;
820 // should probably check offsetParent
821 // but this is more portable
822 while (el
&& el
!== self
.document
.documentElement
) {
825 el
= 'offsetParent' in el
830 // convert to cols/rows
831 w
= self
.element
.clientWidth
;
832 h
= self
.element
.clientHeight
;
833 x
= Math
.round((x
/ w
) * self
.cols
);
834 y
= Math
.round((y
/ h
) * self
.rows
);
836 // be sure to avoid sending
837 // bad positions to the program
839 if (x
> self
.cols
) x
= self
.cols
;
841 if (y
> self
.rows
) y
= self
.rows
;
843 // xterm sends raw bytes and
844 // starts at 32 (SP) for each.
851 type
: ev
.type
=== wheelEvent
857 on(el
, 'mousedown', function(ev
) {
858 if (!self
.mouseEvents
) return;
867 //if (self.vt200Mouse && !self.normalMouse) {
868 if (self
.vt200Mouse
) {
869 sendButton({ __proto__
: ev
, type
: 'mouseup' });
870 return self
.cancel(ev
);
874 if (self
.normalMouse
) on(self
.document
, 'mousemove', sendMove
);
876 // x10 compatibility mode can't send button releases
877 if (!self
.x10Mouse
) {
878 on(self
.document
, 'mouseup', function up(ev
) {
880 if (self
.normalMouse
) off(self
.document
, 'mousemove', sendMove
);
881 off(self
.document
, 'mouseup', up
);
882 return self
.cancel(ev
);
886 return self
.cancel(ev
);
889 //if (self.normalMouse) {
890 // on(self.document, 'mousemove', sendMove);
893 on(el
, wheelEvent
, function(ev
) {
894 if (!self
.mouseEvents
) return;
897 || self
.decLocator
) return;
899 return self
.cancel(ev
);
902 // allow mousewheel scrolling in
903 // the shell for example
904 on(el
, wheelEvent
, function(ev
) {
905 if (self
.mouseEvents
) return;
906 if (self
.applicationKeypad
) return;
907 if (ev
.type
=== 'DOMMouseScroll') {
908 self
.scrollDisp(ev
.detail
< 0 ? -1 : 1);
910 self
.scrollDisp(ev
.wheelDeltaY
> 0 ? -1 : 1);
912 return self
.cancel(ev
);
920 Terminal
.prototype.destroy = function() {
921 this.readable
= false;
922 this.writable
= false;
924 this.handler = function() {};
925 this.write = function() {};
926 if (this.element
.parentNode
) {
927 this.element
.parentNode
.removeChild(this.element
);
929 //this.emit('close');
936 * In the screen buffer, each character
937 * is stored as a an array with a character
938 * and a 32-bit integer.
939 * First value: a utf-16 character.
941 * Next 9 bits: background color (0-511).
942 * Next 9 bits: foreground color (0-511).
943 * Next 14 bits: a mask for misc. flags:
944 * 1=bold, 2=underline, 4=blink, 8=inverse, 16=invisible
946 Terminal
.prototype.refresh = function(start
, end
) {
947 var x
, y
, i
, line
, out
, ch
, width
, data
, attr
, bg
, fg
, flags
, row
, parent
, focused
= document
.activeElement
;
949 if (end
- start
>= this.rows
/ 2) {
950 parent
= this.element
.parentNode
;
951 if (parent
) parent
.removeChild(this.element
);
957 if (end
>= this.lines
.length
) {
958 this.log('`end` is too large. Most likely a bad CSR.');
959 end
= this.lines
.length
- 1;
962 for (; y
<= end
; y
++) {
963 row
= y
+ this.ydisp
;
965 line
= this.lines
[row
];
970 && (this.ydisp
=== this.ybase
)
971 && !this.cursorHidden
) {
980 for (; i
< width
; i
++) {
984 if (i
=== x
) data
= -1;
987 if (attr
!== this.defAttr
) {
990 if (data
!== this.defAttr
) {
992 out
+= '<span class="reverse-video terminal-cursor">';
994 out
+= '<span class="';
997 fg
= (data
>> 9) & 0x1ff;
1002 if (!Terminal
.brokenBold
) {
1003 out
+= ' xterm-bold ';
1005 // See: XTerm*boldColors
1006 if (fg
< 8) fg
+= 8;
1011 out
+= ' xterm-underline ';
1016 out
+= ' xterm-blink ';
1021 bg
= (data
>> 9) & 0x1ff;
1023 // Should inverse just be before the
1024 // above boldColors effect instead?
1025 if ((flags
& 1) && fg
< 8) fg
+= 8;
1030 out
+= ' xterm-hidden ';
1034 out
+= ' xterm-bg-color-' + bg
+ ' ';
1038 out
+= ' xterm-color-' + fg
+ ' ';
1060 if (isWide(ch
)) i
++;
1069 if (attr
!== this.defAttr
) {
1073 this.children
[y
].innerHTML
= out
;
1077 parent
.appendChild(this.element
);
1081 * Return focus to previously focused element
1084 this.emit('refresh', {element
: this.element
, start
: start
, end
: end
});
1087 Terminal
.prototype._cursorBlink = function() {
1088 if (Terminal
.focus
!== this) return;
1089 this.cursorState
^= 1;
1090 this.refresh(this.y
, this.y
);
1093 Terminal
.prototype.showCursor = function() {
1094 if (!this.cursorState
) {
1095 this.cursorState
= 1;
1096 this.refresh(this.y
, this.y
);
1098 // Temporarily disabled:
1099 // this.refreshBlink();
1103 Terminal
.prototype.startBlink = function() {
1104 if (!this.cursorBlink
) return;
1106 this._blinker = function() {
1107 self
._cursorBlink();
1109 this._blink
= setInterval(this._blinker
, 500);
1112 Terminal
.prototype.refreshBlink = function() {
1113 if (!this.cursorBlink
) return;
1114 clearInterval(this._blink
);
1115 this._blink
= setInterval(this._blinker
, 500);
1118 Terminal
.prototype.scroll = function() {
1121 if (++this.ybase
=== this.scrollback
) {
1122 this.ybase
= this.ybase
/ 2 | 0;
1123 this.lines
= this.lines
.slice(-(this.ybase
+ this.rows
) + 1);
1126 this.ydisp
= this.ybase
;
1129 row
= this.ybase
+ this.rows
- 1;
1131 // subtract the bottom scroll region
1132 row
-= this.rows
- 1 - this.scrollBottom
;
1134 if (row
=== this.lines
.length
) {
1135 // potential optimization:
1136 // pushing is faster than splicing
1137 // when they amount to the same
1139 this.lines
.push(this.blankLine());
1142 this.lines
.splice(row
, 0, this.blankLine());
1145 if (this.scrollTop
!== 0) {
1146 if (this.ybase
!== 0) {
1148 this.ydisp
= this.ybase
;
1150 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
1154 this.updateRange(this.scrollTop
);
1155 this.updateRange(this.scrollBottom
);
1158 Terminal
.prototype.scrollDisp = function(disp
) {
1161 if (this.ydisp
> this.ybase
) {
1162 this.ydisp
= this.ybase
;
1163 } else if (this.ydisp
< 0) {
1167 this.refresh(0, this.rows
- 1);
1170 Terminal
.prototype.write = function(data
) {
1171 var l
= data
.length
, i
= 0, j
, cs
, ch
;
1173 this.refreshStart
= this.y
;
1174 this.refreshEnd
= this.y
;
1176 if (this.ybase
!== this.ydisp
) {
1177 this.ydisp
= this.ybase
;
1181 for (; i
< l
; i
++) {
1183 switch (this.state
) {
1194 if (this.convertEol
) {
1198 if (this.y
> this.scrollBottom
) {
1218 this.x
= this.nextStop();
1233 this.state
= escaped
;
1239 if (this.charset
&& this.charset
[ch
]) {
1240 ch
= this.charset
[ch
];
1243 if (this.x
>= this.cols
) {
1246 if (this.y
> this.scrollBottom
) {
1252 this.lines
[this.y
+ this.ybase
][this.x
] = [this.curAttr
, ch
];
1254 this.updateRange(this.y
);
1257 j
= this.y
+ this.ybase
;
1258 if (this.cols
< 2 || this.x
>= this.cols
) {
1259 this.lines
[j
][this.x
- 1] = [this.curAttr
, ' '];
1262 this.lines
[j
][this.x
] = [this.curAttr
, ' '];
1271 // ESC [ Control Sequence Introducer ( CSI is 0x9b).
1274 this.currentParam
= 0;
1278 // ESC ] Operating System Command ( OSC is 0x9d).
1281 this.currentParam
= 0;
1285 // ESC P Device Control String ( DCS is 0x90).
1288 this.currentParam
= 0;
1292 // ESC _ Application Program Command ( APC is 0x9f).
1294 this.state
= ignore
;
1297 // ESC ^ Privacy Message ( PM is 0x9e).
1299 this.state
= ignore
;
1302 // ESC c Full Reset (RIS).
1307 // ESC E Next Line ( NEL is 0x85).
1308 // ESC D Index ( IND is 0x84).
1316 // ESC M Reverse Index ( RI is 0x8d).
1318 this.reverseIndex();
1321 // ESC % Select default/utf-8 character set.
1322 // @ = default, G = utf-8
1324 //this.charset = null;
1326 this.setgCharset(0, Terminal
.charsets
.US
);
1327 this.state
= normal
;
1331 // ESC (,),*,+,-,. Designate G0-G2 Character Set.
1332 case '(': // <-- this seems to get all the attention
1358 this.state
= charset
;
1361 // Designate G3 Character Set (VT300).
1362 // A = ISO Latin-1 Supplemental.
1366 this.state
= charset
;
1371 // Single Shift Select of G2 Character Set
1372 // ( SS2 is 0x8e). This affects next character only.
1376 // Single Shift Select of G3 Character Set
1377 // ( SS3 is 0x8f). This affects next character only.
1381 // Invoke the G2 Character Set as GL (LS2).
1386 // Invoke the G3 Character Set as GL (LS3).
1391 // Invoke the G3 Character Set as GR (LS3R).
1396 // Invoke the G2 Character Set as GR (LS2R).
1401 // Invoke the G1 Character Set as GR (LS1R).
1406 // ESC 7 Save Cursor (DECSC).
1409 this.state
= normal
;
1412 // ESC 8 Restore Cursor (DECRC).
1414 this.restoreCursor();
1415 this.state
= normal
;
1418 // ESC # 3 DEC line height/width
1420 this.state
= normal
;
1424 // ESC H Tab Set (HTS is 0x88).
1429 // ESC = Application Keypad (DECPAM).
1431 this.log('Serial port requested application keypad.');
1432 this.applicationKeypad
= true;
1433 this.state
= normal
;
1436 // ESC > Normal Keypad (DECPNM).
1438 this.log('Switching back to normal keypad.');
1439 this.applicationKeypad
= false;
1440 this.state
= normal
;
1444 this.state
= normal
;
1445 this.error('Unknown ESC control: %s.', ch
);
1452 case '0': // DEC Special Character and Line Drawing Set.
1453 cs
= Terminal
.charsets
.SCLD
;
1456 cs
= Terminal
.charsets
.UK
;
1458 case 'B': // United States (USASCII).
1459 cs
= Terminal
.charsets
.US
;
1462 cs
= Terminal
.charsets
.Dutch
;
1464 case 'C': // Finnish
1466 cs
= Terminal
.charsets
.Finnish
;
1469 cs
= Terminal
.charsets
.French
;
1471 case 'Q': // FrenchCanadian
1472 cs
= Terminal
.charsets
.FrenchCanadian
;
1475 cs
= Terminal
.charsets
.German
;
1477 case 'Y': // Italian
1478 cs
= Terminal
.charsets
.Italian
;
1480 case 'E': // NorwegianDanish
1482 cs
= Terminal
.charsets
.NorwegianDanish
;
1484 case 'Z': // Spanish
1485 cs
= Terminal
.charsets
.Spanish
;
1487 case 'H': // Swedish
1489 cs
= Terminal
.charsets
.Swedish
;
1492 cs
= Terminal
.charsets
.Swiss
;
1494 case '/': // ISOLatin (actually /A)
1495 cs
= Terminal
.charsets
.ISOLatin
;
1499 cs
= Terminal
.charsets
.US
;
1502 this.setgCharset(this.gcharset
, cs
);
1503 this.gcharset
= null;
1504 this.state
= normal
;
1510 // Set Text Parameters.
1511 if (ch
=== '\x1b' || ch
=== '\x07') {
1512 if (ch
=== '\x1b') i
++;
1514 this.params
.push(this.currentParam
);
1516 switch (this.params
[0]) {
1520 if (this.params
[1]) {
1521 this.title
= this.params
[1];
1522 this.handleTitle(this.title
);
1530 // change dynamic colors
1542 // change dynamic ui colors
1554 // manipulate selection data
1572 this.currentParam
= 0;
1573 this.state
= normal
;
1575 if (!this.params
.length
) {
1576 if (ch
>= '0' && ch
<= '9') {
1578 this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1579 } else if (ch
=== ';') {
1580 this.params
.push(this.currentParam
);
1581 this.currentParam
= '';
1584 this.currentParam
+= ch
;
1591 if (ch
=== '?' || ch
=== '>' || ch
=== '!') {
1597 if (ch
>= '0' && ch
<= '9') {
1598 this.currentParam
= this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1602 // '$', '"', ' ', '\''
1603 if (ch
=== '$' || ch
=== '"' || ch
=== ' ' || ch
=== '\'') {
1608 this.params
.push(this.currentParam
);
1609 this.currentParam
= 0;
1612 if (ch
=== ';') break;
1614 this.state
= normal
;
1618 // Cursor Up Ps Times (default = 1) (CUU).
1620 this.cursorUp(this.params
);
1624 // Cursor Down Ps Times (default = 1) (CUD).
1626 this.cursorDown(this.params
);
1630 // Cursor Forward Ps Times (default = 1) (CUF).
1632 this.cursorForward(this.params
);
1636 // Cursor Backward Ps Times (default = 1) (CUB).
1638 this.cursorBackward(this.params
);
1642 // Cursor Position [row;column] (default = [1,1]) (CUP).
1644 this.cursorPos(this.params
);
1647 // CSI Ps J Erase in Display (ED).
1649 this.eraseInDisplay(this.params
);
1652 // CSI Ps K Erase in Line (EL).
1654 this.eraseInLine(this.params
);
1657 // CSI Pm m Character Attributes (SGR).
1660 this.charAttributes(this.params
);
1664 // CSI Ps n Device Status Report (DSR).
1667 this.deviceStatus(this.params
);
1676 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
1678 this.insertChars(this.params
);
1682 // Cursor Next Line Ps Times (default = 1) (CNL).
1684 this.cursorNextLine(this.params
);
1688 // Cursor Preceding Line Ps Times (default = 1) (CNL).
1690 this.cursorPrecedingLine(this.params
);
1694 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
1696 this.cursorCharAbsolute(this.params
);
1700 // Insert Ps Line(s) (default = 1) (IL).
1702 this.insertLines(this.params
);
1706 // Delete Ps Line(s) (default = 1) (DL).
1708 this.deleteLines(this.params
);
1712 // Delete Ps Character(s) (default = 1) (DCH).
1714 this.deleteChars(this.params
);
1718 // Erase Ps Character(s) (default = 1) (ECH).
1720 this.eraseChars(this.params
);
1723 // CSI Pm ` Character Position Absolute
1724 // [column] (default = [row,1]) (HPA).
1726 this.charPosAbsolute(this.params
);
1730 // Horizontal Position Relative
1732 this.HPositionRelative(this.params
);
1736 // Send Device Attributes (Primary DA).
1738 // Send Device Attributes (Secondary DA)
1740 this.sendDeviceAttributes(this.params
);
1744 // Line Position Absolute [row] (default = [1,column]) (VPA).
1746 this.linePosAbsolute(this.params
);
1749 // 145 65 e * VPR - Vertical Position Relative
1751 this.VPositionRelative(this.params
);
1755 // Horizontal and Vertical Position [row;column] (default =
1758 this.HVPosition(this.params
);
1761 // CSI Pm h Set Mode (SM).
1762 // CSI ? Pm h - mouse escape codes, cursor escape codes
1764 this.setMode(this.params
);
1767 // CSI Pm l Reset Mode (RM).
1770 this.resetMode(this.params
);
1774 // Set Scrolling Region [top;bottom] (default = full size of win-
1778 this.setScrollRegion(this.params
);
1782 // Save cursor (ANSI.SYS).
1784 this.saveCursor(this.params
);
1788 // Restore cursor (ANSI.SYS).
1790 this.restoreCursor(this.params
);
1798 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
1800 this.cursorForwardTab(this.params
);
1803 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
1805 this.scrollUp(this.params
);
1808 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
1809 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
1812 // if (this.prefix === '>') {
1813 // this.resetTitleModes(this.params);
1816 // if (this.params.length > 2) {
1817 // this.initMouseTracking(this.params);
1820 if (this.params
.length
< 2 && !this.prefix
) {
1821 this.scrollDown(this.params
);
1826 // Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
1828 this.cursorBackwardTab(this.params
);
1831 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
1833 this.repeatPrecedingCharacter(this.params
);
1836 // CSI Ps g Tab Clear (TBC).
1838 this.tabClear(this.params
);
1841 // CSI Pm i Media Copy (MC).
1844 // this.mediaCopy(this.params);
1847 // CSI Pm m Character Attributes (SGR).
1849 // case 'm': // duplicate
1850 // if (this.prefix === '>') {
1851 // this.setResources(this.params);
1853 // this.charAttributes(this.params);
1857 // CSI Ps n Device Status Report (DSR).
1859 // case 'n': // duplicate
1860 // if (this.prefix === '>') {
1861 // this.disableModifiers(this.params);
1863 // this.deviceStatus(this.params);
1867 // CSI > Ps p Set pointer mode.
1868 // CSI ! p Soft terminal reset (DECSTR).
1870 // Request ANSI mode (DECRQM).
1872 // Request DEC private mode (DECRQM).
1875 switch (this.prefix
) {
1877 // this.setPointerMode(this.params);
1880 this.softReset(this.params
);
1883 // if (this.postfix === '$') {
1884 // this.requestPrivateMode(this.params);
1888 // if (this.postfix === '"') {
1889 // this.setConformanceLevel(this.params);
1890 // } else if (this.postfix === '$') {
1891 // this.requestAnsiMode(this.params);
1897 // CSI Ps q Load LEDs (DECLL).
1901 // if (this.postfix === ' ') {
1902 // this.setCursorStyle(this.params);
1905 // if (this.postfix === '"') {
1906 // this.setCharProtectionAttr(this.params);
1909 // this.loadLEDs(this.params);
1913 // Set Scrolling Region [top;bottom] (default = full size of win-
1916 // CSI Pt; Pl; Pb; Pr; Ps$ r
1917 // case 'r': // duplicate
1918 // if (this.prefix === '?') {
1919 // this.restorePrivateValues(this.params);
1920 // } else if (this.postfix === '$') {
1921 // this.setAttrInRectangle(this.params);
1923 // this.setScrollRegion(this.params);
1927 // CSI s Save cursor (ANSI.SYS).
1929 // case 's': // duplicate
1930 // if (this.prefix === '?') {
1931 // this.savePrivateValues(this.params);
1933 // this.saveCursor(this.params);
1937 // CSI Ps ; Ps ; Ps t
1938 // CSI Pt; Pl; Pb; Pr; Ps$ t
1942 // if (this.postfix === '$') {
1943 // this.reverseAttrInRectangle(this.params);
1944 // } else if (this.postfix === ' ') {
1945 // this.setWarningBellVolume(this.params);
1947 // if (this.prefix === '>') {
1948 // this.setTitleModeFeature(this.params);
1950 // this.manipulateWindow(this.params);
1955 // CSI u Restore cursor (ANSI.SYS).
1957 // case 'u': // duplicate
1958 // if (this.postfix === ' ') {
1959 // this.setMarginBellVolume(this.params);
1961 // this.restoreCursor(this.params);
1965 // CSI Pt; Pl; Pb; Pr; Pp; Pt; Pl; Pp$ v
1967 // if (this.postfix === '$') {
1968 // this.copyRectagle(this.params);
1972 // CSI Pt ; Pl ; Pb ; Pr ' w
1974 // if (this.postfix === '\'') {
1975 // this.enableFilterRectangle(this.params);
1979 // CSI Ps x Request Terminal Parameters (DECREQTPARM).
1980 // CSI Ps x Select Attribute Change Extent (DECSACE).
1981 // CSI Pc; Pt; Pl; Pb; Pr$ x
1983 // if (this.postfix === '$') {
1984 // this.fillRectangle(this.params);
1986 // this.requestParameters(this.params);
1987 // //this.__(this.params);
1992 // CSI Pt; Pl; Pb; Pr$ z
1994 // if (this.postfix === '\'') {
1995 // this.enableLocatorReporting(this.params);
1996 // } else if (this.postfix === '$') {
1997 // this.eraseRectangle(this.params);
2002 // CSI Pt; Pl; Pb; Pr$ {
2004 // if (this.postfix === '\'') {
2005 // this.setLocatorEvents(this.params);
2006 // } else if (this.postfix === '$') {
2007 // this.selectiveEraseRectangle(this.params);
2013 // if (this.postfix === '\'') {
2014 // this.requestLocatorPosition(this.params);
2019 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
2021 // if (this.postfix === ' ') {
2022 // this.insertColumns(this.params);
2027 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
2029 // if (this.postfix === ' ') {
2030 // this.deleteColumns(this.params);
2035 this.error('Unknown CSI code: %s.', ch
);
2044 if (ch
=== '\x1b' || ch
=== '\x07') {
2045 if (ch
=== '\x1b') i
++;
2047 switch (this.prefix
) {
2048 // User-Defined Keys (DECUDK).
2052 // Request Status String (DECRQSS).
2053 // test: echo -e '\eP$q"p\e\\'
2055 var pt
= this.currentParam
2072 + (this.scrollTop
+ 1)
2074 + (this.scrollBottom
+ 1)
2084 this.error('Unknown DCS Pt: %s.', pt
);
2089 this.send('\x1bP' + +valid
+ '$r' + pt
+ '\x1b\\');
2092 // Set Termcap/Terminfo Data (xterm, experimental).
2096 // Request Termcap/Terminfo String (xterm, experimental)
2097 // Regular xterm does not even respond to this sequence.
2098 // This can cause a small glitch in vim.
2099 // test: echo -ne '\eP+q6b64\e\\'
2101 var pt
= this.currentParam
2104 this.send('\x1bP' + +valid
+ '+r' + pt
+ '\x1b\\');
2108 this.error('Unknown DCS prefix: %s.', this.prefix
);
2112 this.currentParam
= 0;
2114 this.state
= normal
;
2115 } else if (!this.currentParam
) {
2116 if (!this.prefix
&& ch
!== '$' && ch
!== '+') {
2117 this.currentParam
= ch
;
2118 } else if (this.prefix
.length
=== 2) {
2119 this.currentParam
= ch
;
2124 this.currentParam
+= ch
;
2130 if (ch
=== '\x1b' || ch
=== '\x07') {
2131 if (ch
=== '\x1b') i
++;
2132 this.state
= normal
;
2138 this.updateRange(this.y
);
2139 this.refresh(this.refreshStart
, this.refreshEnd
);
2142 Terminal
.prototype.writeln = function(data
) {
2143 this.write(data
+ '\r\n');
2147 // https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
2148 Terminal
.prototype.keyDown = function(ev
) {
2149 var self
= this, key
;
2151 switch (ev
.keyCode
) {
2167 this.cancel(ev
, true);
2172 this.cancel(ev
, true);
2177 this.cancel(ev
, true);
2182 this.cancel(ev
, true);
2183 key
= '\x1bb' // Jump a word back
2185 } else if (this.applicationCursor
) {
2186 key
= '\x1bOD'; // SS3 as ^[O for 7-bit
2194 this.cancel(ev
, true);
2195 key
= '\x1bf' // Jump a word forward
2197 } else if (this.applicationCursor
) {
2205 if (this.applicationCursor
) {
2210 this.scrollDisp(-1);
2211 return this.cancel(ev
);
2218 if (this.applicationCursor
) {
2224 return this.cancel(ev
);
2239 if (this.applicationKeypad
) {
2247 if (this.applicationKeypad
) {
2256 this.scrollDisp(-(this.rows
- 1));
2257 return this.cancel(ev
);
2265 this.scrollDisp(this.rows
- 1);
2266 return this.cancel(ev
);
2321 if (ev
.ctrlKey
&& !ev
.shiftKey
&& !ev
.altKey
&& !ev
.metaKey
) {
2322 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2323 key
= String
.fromCharCode(ev
.keyCode
- 64);
2324 } else if (ev
.keyCode
=== 32) {
2326 key
= String
.fromCharCode(0);
2327 } else if (ev
.keyCode
>= 51 && ev
.keyCode
<= 55) {
2328 // escape, file sep, group sep, record sep, unit sep
2329 key
= String
.fromCharCode(ev
.keyCode
- 51 + 27);
2330 } else if (ev
.keyCode
=== 56) {
2332 key
= String
.fromCharCode(127);
2333 } else if (ev
.keyCode
=== 219) {
2335 key
= String
.fromCharCode(27);
2336 } else if (ev
.keyCode
=== 221) {
2338 key
= String
.fromCharCode(29);
2340 } else if ((!this.isMac
&& ev
.altKey
) || (this.isMac
&& ev
.metaKey
)) {
2341 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2342 key
= '\x1b' + String
.fromCharCode(ev
.keyCode
+ 32);
2343 } else if (ev
.keyCode
=== 192) {
2345 } else if (ev
.keyCode
>= 48 && ev
.keyCode
<= 57) {
2346 key
= '\x1b' + (ev
.keyCode
- 48);
2352 if (!key
|| (this.isMac
&& ev
.metaKey
)) {
2356 this.emit('keydown', ev
);
2357 this.emit('key', key
, ev
);
2361 return this.cancel(ev
, true);
2364 Terminal
.prototype.setgLevel = function(g
) {
2366 this.charset
= this.charsets
[g
];
2369 Terminal
.prototype.setgCharset = function(g
, charset
) {
2370 this.charsets
[g
] = charset
;
2371 if (this.glevel
=== g
) {
2372 this.charset
= charset
;
2376 Terminal
.prototype.keyPress = function(ev
) {
2381 } else if (ev
.which
== null) {
2383 } else if (ev
.which
!== 0 && ev
.charCode
!== 0) {
2389 if (!key
|| ev
.ctrlKey
|| ev
.altKey
|| ev
.metaKey
) {
2393 key
= String
.fromCharCode(key
);
2395 this.emit('keypress', key
, ev
);
2396 this.emit('key', key
, ev
);
2400 this.cancel(ev
, true);
2405 Terminal
.prototype.send = function(data
) {
2409 setTimeout(function() {
2410 self
.handler(self
.queue
);
2418 Terminal
.prototype.bell = function() {
2419 if (!this.visualBell
) return;
2421 this.element
.style
.borderColor
= 'white';
2422 setTimeout(function() {
2423 self
.element
.style
.borderColor
= '';
2425 if (this.popOnBell
) this.focus();
2428 Terminal
.prototype.log = function() {
2429 if (!this.debug
) return;
2430 if (!this.context
.console
|| !this.context
.console
.log
) return;
2431 var args
= Array
.prototype.slice
.call(arguments
);
2432 this.context
.console
.log
.apply(this.context
.console
, args
);
2435 Terminal
.prototype.error = function() {
2436 if (!this.debug
) return;
2437 if (!this.context
.console
|| !this.context
.console
.error
) return;
2438 var args
= Array
.prototype.slice
.call(arguments
);
2439 this.context
.console
.error
.apply(this.context
.console
, args
);
2442 Terminal
.prototype.resize = function(x
, y
) {
2455 ch
= [this.defAttr
, ' ']; // does xterm use the default attr?
2456 i
= this.lines
.length
;
2458 while (this.lines
[i
].length
< x
) {
2459 this.lines
[i
].push(ch
);
2463 i
= this.lines
.length
;
2465 while (this.lines
[i
].length
> x
) {
2466 this.lines
[i
].pop();
2478 if (this.lines
.length
< y
+ this.ybase
) {
2479 this.lines
.push(this.blankLine());
2481 if (this.children
.length
< y
) {
2487 if (this.lines
.length
> y
+ this.ybase
) {
2490 if (this.children
.length
> y
) {
2491 el
= this.children
.pop();
2493 el
.parentNode
.removeChild(el
);
2500 * Make sure that the cursor stays on screen
2511 this.scrollBottom
= y
- 1;
2513 this.refresh(0, this.rows
- 1);
2517 this.emit('resize', {terminal
: this, cols
: x
, rows
: y
});
2520 Terminal
.prototype.updateRange = function(y
) {
2521 if (y
< this.refreshStart
) this.refreshStart
= y
;
2522 if (y
> this.refreshEnd
) this.refreshEnd
= y
;
2523 // if (y > this.refreshEnd) {
2524 // this.refreshEnd = y;
2525 // if (y > this.rows - 1) {
2526 // this.refreshEnd = this.rows - 1;
2531 Terminal
.prototype.maxRange = function() {
2532 this.refreshStart
= 0;
2533 this.refreshEnd
= this.rows
- 1;
2536 Terminal
.prototype.setupStops = function(i
) {
2538 if (!this.tabs
[i
]) {
2539 i
= this.prevStop(i
);
2546 for (; i
< this.cols
; i
+= 8) {
2547 this.tabs
[i
] = true;
2551 Terminal
.prototype.prevStop = function(x
) {
2552 if (x
== null) x
= this.x
;
2553 while (!this.tabs
[--x
] && x
> 0);
2554 return x
>= this.cols
2559 Terminal
.prototype.nextStop = function(x
) {
2560 if (x
== null) x
= this.x
;
2561 while (!this.tabs
[++x
] && x
< this.cols
);
2562 return x
>= this.cols
2567 Terminal
.prototype.eraseRight = function(x
, y
) {
2568 var line
= this.lines
[this.ybase
+ y
]
2569 , ch
= [this.eraseAttr(), ' ']; // xterm
2572 for (; x
< this.cols
; x
++) {
2576 this.updateRange(y
);
2579 Terminal
.prototype.eraseLeft = function(x
, y
) {
2580 var line
= this.lines
[this.ybase
+ y
]
2581 , ch
= [this.eraseAttr(), ' ']; // xterm
2584 while (x
--) line
[x
] = ch
;
2586 this.updateRange(y
);
2589 Terminal
.prototype.eraseLine = function(y
) {
2590 this.eraseRight(0, y
);
2593 Terminal
.prototype.blankLine = function(cur
) {
2598 var ch
= [attr
, ' ']
2602 for (; i
< this.cols
; i
++) {
2609 Terminal
.prototype.ch = function(cur
) {
2611 ? [this.eraseAttr(), ' ']
2612 : [this.defAttr
, ' '];
2615 Terminal
.prototype.is = function(term
) {
2616 var name
= this.termName
;
2617 return (name
+ '').indexOf(term
) === 0;
2620 Terminal
.prototype.handler = function(data
) {
2621 this.emit('data', data
);
2624 Terminal
.prototype.handleTitle = function(title
) {
2625 this.emit('title', title
);
2632 // ESC D Index (IND is 0x84).
2633 Terminal
.prototype.index = function() {
2635 if (this.y
> this.scrollBottom
) {
2639 this.state
= normal
;
2642 // ESC M Reverse Index (RI is 0x8d).
2643 Terminal
.prototype.reverseIndex = function() {
2646 if (this.y
< this.scrollTop
) {
2648 // possibly move the code below to term.reverseScroll();
2649 // test: echo -ne '\e[1;1H\e[44m\eM\e[0m'
2650 // blankLine(true) is xterm/linux behavior
2651 this.lines
.splice(this.y
+ this.ybase
, 0, this.blankLine(true));
2652 j
= this.rows
- 1 - this.scrollBottom
;
2653 this.lines
.splice(this.rows
- 1 + this.ybase
- j
+ 1, 1);
2655 this.updateRange(this.scrollTop
);
2656 this.updateRange(this.scrollBottom
);
2658 this.state
= normal
;
2661 // ESC c Full Reset (RIS).
2662 Terminal
.prototype.reset = function() {
2663 this.options
.rows
= this.rows
;
2664 this.options
.cols
= this.cols
;
2665 Terminal
.call(this, this.options
);
2666 this.refresh(0, this.rows
- 1);
2669 // ESC H Tab Set (HTS is 0x88).
2670 Terminal
.prototype.tabSet = function() {
2671 this.tabs
[this.x
] = true;
2672 this.state
= normal
;
2680 // Cursor Up Ps Times (default = 1) (CUU).
2681 Terminal
.prototype.cursorUp = function(params
) {
2682 var param
= params
[0];
2683 if (param
< 1) param
= 1;
2685 if (this.y
< 0) this.y
= 0;
2689 // Cursor Down Ps Times (default = 1) (CUD).
2690 Terminal
.prototype.cursorDown = function(params
) {
2691 var param
= params
[0];
2692 if (param
< 1) param
= 1;
2694 if (this.y
>= this.rows
) {
2695 this.y
= this.rows
- 1;
2700 // Cursor Forward Ps Times (default = 1) (CUF).
2701 Terminal
.prototype.cursorForward = function(params
) {
2702 var param
= params
[0];
2703 if (param
< 1) param
= 1;
2705 if (this.x
>= this.cols
) {
2706 this.x
= this.cols
- 1;
2711 // Cursor Backward Ps Times (default = 1) (CUB).
2712 Terminal
.prototype.cursorBackward = function(params
) {
2713 var param
= params
[0];
2714 if (param
< 1) param
= 1;
2716 if (this.x
< 0) this.x
= 0;
2720 // Cursor Position [row;column] (default = [1,1]) (CUP).
2721 Terminal
.prototype.cursorPos = function(params
) {
2724 row
= params
[0] - 1;
2726 if (params
.length
>= 2) {
2727 col
= params
[1] - 1;
2734 } else if (row
>= this.rows
) {
2735 row
= this.rows
- 1;
2740 } else if (col
>= this.cols
) {
2741 col
= this.cols
- 1;
2748 // CSI Ps J Erase in Display (ED).
2749 // Ps = 0 -> Erase Below (default).
2750 // Ps = 1 -> Erase Above.
2751 // Ps = 2 -> Erase All.
2752 // Ps = 3 -> Erase Saved Lines (xterm).
2754 // Erase in Display (DECSED).
2755 // Ps = 0 -> Selective Erase Below (default).
2756 // Ps = 1 -> Selective Erase Above.
2757 // Ps = 2 -> Selective Erase All.
2758 Terminal
.prototype.eraseInDisplay = function(params
) {
2760 switch (params
[0]) {
2762 this.eraseRight(this.x
, this.y
);
2764 for (; j
< this.rows
; j
++) {
2769 this.eraseLeft(this.x
, this.y
);
2777 while (j
--) this.eraseLine(j
);
2785 // CSI Ps K Erase in Line (EL).
2786 // Ps = 0 -> Erase to Right (default).
2787 // Ps = 1 -> Erase to Left.
2788 // Ps = 2 -> Erase All.
2790 // Erase in Line (DECSEL).
2791 // Ps = 0 -> Selective Erase to Right (default).
2792 // Ps = 1 -> Selective Erase to Left.
2793 // Ps = 2 -> Selective Erase All.
2794 Terminal
.prototype.eraseInLine = function(params
) {
2795 switch (params
[0]) {
2797 this.eraseRight(this.x
, this.y
);
2800 this.eraseLeft(this.x
, this.y
);
2803 this.eraseLine(this.y
);
2808 // CSI Pm m Character Attributes (SGR).
2809 // Ps = 0 -> Normal (default).
2811 // Ps = 4 -> Underlined.
2812 // Ps = 5 -> Blink (appears as Bold).
2813 // Ps = 7 -> Inverse.
2814 // Ps = 8 -> Invisible, i.e., hidden (VT300).
2815 // Ps = 2 2 -> Normal (neither bold nor faint).
2816 // Ps = 2 4 -> Not underlined.
2817 // Ps = 2 5 -> Steady (not blinking).
2818 // Ps = 2 7 -> Positive (not inverse).
2819 // Ps = 2 8 -> Visible, i.e., not hidden (VT300).
2820 // Ps = 3 0 -> Set foreground color to Black.
2821 // Ps = 3 1 -> Set foreground color to Red.
2822 // Ps = 3 2 -> Set foreground color to Green.
2823 // Ps = 3 3 -> Set foreground color to Yellow.
2824 // Ps = 3 4 -> Set foreground color to Blue.
2825 // Ps = 3 5 -> Set foreground color to Magenta.
2826 // Ps = 3 6 -> Set foreground color to Cyan.
2827 // Ps = 3 7 -> Set foreground color to White.
2828 // Ps = 3 9 -> Set foreground color to default (original).
2829 // Ps = 4 0 -> Set background color to Black.
2830 // Ps = 4 1 -> Set background color to Red.
2831 // Ps = 4 2 -> Set background color to Green.
2832 // Ps = 4 3 -> Set background color to Yellow.
2833 // Ps = 4 4 -> Set background color to Blue.
2834 // Ps = 4 5 -> Set background color to Magenta.
2835 // Ps = 4 6 -> Set background color to Cyan.
2836 // Ps = 4 7 -> Set background color to White.
2837 // Ps = 4 9 -> Set background color to default (original).
2839 // If 16-color support is compiled, the following apply. Assume
2840 // that xterm's resources are set so that the ISO color codes are
2841 // the first 8 of a set of 16. Then the aixterm colors are the
2842 // bright versions of the ISO colors:
2843 // Ps = 9 0 -> Set foreground color to Black.
2844 // Ps = 9 1 -> Set foreground color to Red.
2845 // Ps = 9 2 -> Set foreground color to Green.
2846 // Ps = 9 3 -> Set foreground color to Yellow.
2847 // Ps = 9 4 -> Set foreground color to Blue.
2848 // Ps = 9 5 -> Set foreground color to Magenta.
2849 // Ps = 9 6 -> Set foreground color to Cyan.
2850 // Ps = 9 7 -> Set foreground color to White.
2851 // Ps = 1 0 0 -> Set background color to Black.
2852 // Ps = 1 0 1 -> Set background color to Red.
2853 // Ps = 1 0 2 -> Set background color to Green.
2854 // Ps = 1 0 3 -> Set background color to Yellow.
2855 // Ps = 1 0 4 -> Set background color to Blue.
2856 // Ps = 1 0 5 -> Set background color to Magenta.
2857 // Ps = 1 0 6 -> Set background color to Cyan.
2858 // Ps = 1 0 7 -> Set background color to White.
2860 // If xterm is compiled with the 16-color support disabled, it
2861 // supports the following, from rxvt:
2862 // Ps = 1 0 0 -> Set foreground and background color to
2865 // If 88- or 256-color support is compiled, the following apply.
2866 // Ps = 3 8 ; 5 ; Ps -> Set foreground color to the second
2868 // Ps = 4 8 ; 5 ; Ps -> Set background color to the second
2870 Terminal
.prototype.charAttributes = function(params
) {
2871 // Optimize a single SGR0.
2872 if (params
.length
=== 1 && params
[0] === 0) {
2873 this.curAttr
= this.defAttr
;
2877 var l
= params
.length
2879 , flags
= this.curAttr
>> 18
2880 , fg
= (this.curAttr
>> 9) & 0x1ff
2881 , bg
= this.curAttr
& 0x1ff
2884 for (; i
< l
; i
++) {
2886 if (p
>= 30 && p
<= 37) {
2889 } else if (p
>= 40 && p
<= 47) {
2892 } else if (p
>= 90 && p
<= 97) {
2896 } else if (p
>= 100 && p
<= 107) {
2900 } else if (p
=== 0) {
2902 flags
= this.defAttr
>> 18;
2903 fg
= (this.defAttr
>> 9) & 0x1ff;
2904 bg
= this.defAttr
& 0x1ff;
2908 } else if (p
=== 1) {
2911 } else if (p
=== 4) {
2914 } else if (p
=== 5) {
2917 } else if (p
=== 7) {
2918 // inverse and positive
2919 // test with: echo -e '\e[31m\e[42mhello\e[7mworld\e[27mhi\e[m'
2921 } else if (p
=== 8) {
2924 } else if (p
=== 22) {
2927 } else if (p
=== 24) {
2930 } else if (p
=== 25) {
2933 } else if (p
=== 27) {
2936 } else if (p
=== 28) {
2939 } else if (p
=== 39) {
2941 fg
= (this.defAttr
>> 9) & 0x1ff;
2942 } else if (p
=== 49) {
2944 bg
= this.defAttr
& 0x1ff;
2945 } else if (p
=== 38) {
2947 if (params
[i
+ 1] === 2) {
2951 params
[i
+ 1] & 0xff,
2952 params
[i
+ 2] & 0xff);
2953 if (fg
=== -1) fg
= 0x1ff;
2955 } else if (params
[i
+ 1] === 5) {
2957 p
= params
[i
] & 0xff;
2960 } else if (p
=== 48) {
2962 if (params
[i
+ 1] === 2) {
2966 params
[i
+ 1] & 0xff,
2967 params
[i
+ 2] & 0xff);
2968 if (bg
=== -1) bg
= 0x1ff;
2970 } else if (params
[i
+ 1] === 5) {
2972 p
= params
[i
] & 0xff;
2975 } else if (p
=== 100) {
2977 fg
= (this.defAttr
>> 9) & 0x1ff;
2978 bg
= this.defAttr
& 0x1ff;
2980 this.error('Unknown SGR attribute: %d.', p
);
2984 this.curAttr
= (flags
<< 18) | (fg
<< 9) | bg
;
2987 // CSI Ps n Device Status Report (DSR).
2988 // Ps = 5 -> Status Report. Result (``OK'') is
2990 // Ps = 6 -> Report Cursor Position (CPR) [row;column].
2994 // Device Status Report (DSR, DEC-specific).
2995 // Ps = 6 -> Report Cursor Position (CPR) [row;column] as CSI
2996 // ? r ; c R (assumes page is zero).
2997 // Ps = 1 5 -> Report Printer status as CSI ? 1 0 n (ready).
2998 // or CSI ? 1 1 n (not ready).
2999 // Ps = 2 5 -> Report UDK status as CSI ? 2 0 n (unlocked)
3000 // or CSI ? 2 1 n (locked).
3001 // Ps = 2 6 -> Report Keyboard status as
3002 // CSI ? 2 7 ; 1 ; 0 ; 0 n (North American).
3003 // The last two parameters apply to VT400 & up, and denote key-
3004 // board ready and LK01 respectively.
3005 // Ps = 5 3 -> Report Locator status as
3006 // CSI ? 5 3 n Locator available, if compiled-in, or
3007 // CSI ? 5 0 n No Locator, if not.
3008 Terminal
.prototype.deviceStatus = function(params
) {
3010 switch (params
[0]) {
3013 this.send('\x1b[0n');
3024 } else if (this.prefix
=== '?') {
3025 // modern xterm doesnt seem to
3026 // respond to any of these except ?6, 6, and 5
3027 switch (params
[0]) {
3038 // this.send('\x1b[?11n');
3041 // dont support user defined keys
3042 // this.send('\x1b[?21n');
3045 // north american keyboard
3046 // this.send('\x1b[?27;1;0;0n');
3049 // no dec locator/mouse
3050 // this.send('\x1b[?50n');
3061 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
3062 Terminal
.prototype.insertChars = function(params
) {
3063 var param
, row
, j
, ch
;
3066 if (param
< 1) param
= 1;
3068 row
= this.y
+ this.ybase
;
3070 ch
= [this.eraseAttr(), ' ']; // xterm
3072 while (param
-- && j
< this.cols
) {
3073 this.lines
[row
].splice(j
++, 0, ch
);
3074 this.lines
[row
].pop();
3079 // Cursor Next Line Ps Times (default = 1) (CNL).
3080 // same as CSI Ps B ?
3081 Terminal
.prototype.cursorNextLine = function(params
) {
3082 var param
= params
[0];
3083 if (param
< 1) param
= 1;
3085 if (this.y
>= this.rows
) {
3086 this.y
= this.rows
- 1;
3092 // Cursor Preceding Line Ps Times (default = 1) (CNL).
3094 Terminal
.prototype.cursorPrecedingLine = function(params
) {
3095 var param
= params
[0];
3096 if (param
< 1) param
= 1;
3098 if (this.y
< 0) this.y
= 0;
3103 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
3104 Terminal
.prototype.cursorCharAbsolute = function(params
) {
3105 var param
= params
[0];
3106 if (param
< 1) param
= 1;
3111 // Insert Ps Line(s) (default = 1) (IL).
3112 Terminal
.prototype.insertLines = function(params
) {
3116 if (param
< 1) param
= 1;
3117 row
= this.y
+ this.ybase
;
3119 j
= this.rows
- 1 - this.scrollBottom
;
3120 j
= this.rows
- 1 + this.ybase
- j
+ 1;
3123 // test: echo -e '\e[44m\e[1L\e[0m'
3124 // blankLine(true) - xterm/linux behavior
3125 this.lines
.splice(row
, 0, this.blankLine(true));
3126 this.lines
.splice(j
, 1);
3130 this.updateRange(this.y
);
3131 this.updateRange(this.scrollBottom
);
3135 // Delete Ps Line(s) (default = 1) (DL).
3136 Terminal
.prototype.deleteLines = function(params
) {
3140 if (param
< 1) param
= 1;
3141 row
= this.y
+ this.ybase
;
3143 j
= this.rows
- 1 - this.scrollBottom
;
3144 j
= this.rows
- 1 + this.ybase
- j
;
3147 // test: echo -e '\e[44m\e[1M\e[0m'
3148 // blankLine(true) - xterm/linux behavior
3149 this.lines
.splice(j
+ 1, 0, this.blankLine(true));
3150 this.lines
.splice(row
, 1);
3154 this.updateRange(this.y
);
3155 this.updateRange(this.scrollBottom
);
3159 // Delete Ps Character(s) (default = 1) (DCH).
3160 Terminal
.prototype.deleteChars = function(params
) {
3164 if (param
< 1) param
= 1;
3166 row
= this.y
+ this.ybase
;
3167 ch
= [this.eraseAttr(), ' ']; // xterm
3170 this.lines
[row
].splice(this.x
, 1);
3171 this.lines
[row
].push(ch
);
3176 // Erase Ps Character(s) (default = 1) (ECH).
3177 Terminal
.prototype.eraseChars = function(params
) {
3178 var param
, row
, j
, ch
;
3181 if (param
< 1) param
= 1;
3183 row
= this.y
+ this.ybase
;
3185 ch
= [this.eraseAttr(), ' ']; // xterm
3187 while (param
-- && j
< this.cols
) {
3188 this.lines
[row
][j
++] = ch
;
3192 // CSI Pm ` Character Position Absolute
3193 // [column] (default = [row,1]) (HPA).
3194 Terminal
.prototype.charPosAbsolute = function(params
) {
3195 var param
= params
[0];
3196 if (param
< 1) param
= 1;
3198 if (this.x
>= this.cols
) {
3199 this.x
= this.cols
- 1;
3204 // Horizontal Position Relative
3206 Terminal
.prototype.HPositionRelative = function(params
) {
3207 var param
= params
[0];
3208 if (param
< 1) param
= 1;
3210 if (this.x
>= this.cols
) {
3211 this.x
= this.cols
- 1;
3215 // CSI Ps c Send Device Attributes (Primary DA).
3216 // Ps = 0 or omitted -> request attributes from terminal. The
3217 // response depends on the decTerminalID resource setting.
3218 // -> CSI ? 1 ; 2 c (``VT100 with Advanced Video Option'')
3219 // -> CSI ? 1 ; 0 c (``VT101 with No Options'')
3220 // -> CSI ? 6 c (``VT102'')
3221 // -> CSI ? 6 0 ; 1 ; 2 ; 6 ; 8 ; 9 ; 1 5 ; c (``VT220'')
3222 // The VT100-style response parameters do not mean anything by
3223 // themselves. VT220 parameters do, telling the host what fea-
3224 // tures the terminal supports:
3225 // Ps = 1 -> 132-columns.
3226 // Ps = 2 -> Printer.
3227 // Ps = 6 -> Selective erase.
3228 // Ps = 8 -> User-defined keys.
3229 // Ps = 9 -> National replacement character sets.
3230 // Ps = 1 5 -> Technical characters.
3231 // Ps = 2 2 -> ANSI color, e.g., VT525.
3232 // Ps = 2 9 -> ANSI text locator (i.e., DEC Locator mode).
3234 // Send Device Attributes (Secondary DA).
3235 // Ps = 0 or omitted -> request the terminal's identification
3236 // code. The response depends on the decTerminalID resource set-
3237 // ting. It should apply only to VT220 and up, but xterm extends
3239 // -> CSI > Pp ; Pv ; Pc c
3240 // where Pp denotes the terminal type
3241 // Pp = 0 -> ``VT100''.
3242 // Pp = 1 -> ``VT220''.
3243 // and Pv is the firmware version (for xterm, this was originally
3244 // the XFree86 patch number, starting with 95). In a DEC termi-
3245 // nal, Pc indicates the ROM cartridge registration number and is
3247 // More information:
3248 // xterm/charproc.c - line 2012, for more information.
3249 // vim responds with ^[[?0c or ^[[?1c after the terminal's response (?)
3250 Terminal
.prototype.sendDeviceAttributes = function(params
) {
3251 if (params
[0] > 0) return;
3254 if (this.is('xterm')
3255 || this.is('rxvt-unicode')
3256 || this.is('screen')) {
3257 this.send('\x1b[?1;2c');
3258 } else if (this.is('linux')) {
3259 this.send('\x1b[?6c');
3261 } else if (this.prefix
=== '>') {
3263 // seem to spit this
3264 // out around ~370 times (?).
3265 if (this.is('xterm')) {
3266 this.send('\x1b[>0;276;0c');
3267 } else if (this.is('rxvt-unicode')) {
3268 this.send('\x1b[>85;95;0c');
3269 } else if (this.is('linux')) {
3270 // not supported by linux console.
3271 // linux console echoes parameters.
3272 this.send(params
[0] + 'c');
3273 } else if (this.is('screen')) {
3274 this.send('\x1b[>83;40003;0c');
3280 // Line Position Absolute [row] (default = [1,column]) (VPA).
3281 Terminal
.prototype.linePosAbsolute = function(params
) {
3282 var param
= params
[0];
3283 if (param
< 1) param
= 1;
3285 if (this.y
>= this.rows
) {
3286 this.y
= this.rows
- 1;
3290 // 145 65 e * VPR - Vertical Position Relative
3292 Terminal
.prototype.VPositionRelative = function(params
) {
3293 var param
= params
[0];
3294 if (param
< 1) param
= 1;
3296 if (this.y
>= this.rows
) {
3297 this.y
= this.rows
- 1;
3302 // Horizontal and Vertical Position [row;column] (default =
3304 Terminal
.prototype.HVPosition = function(params
) {
3305 if (params
[0] < 1) params
[0] = 1;
3306 if (params
[1] < 1) params
[1] = 1;
3308 this.y
= params
[0] - 1;
3309 if (this.y
>= this.rows
) {
3310 this.y
= this.rows
- 1;
3313 this.x
= params
[1] - 1;
3314 if (this.x
>= this.cols
) {
3315 this.x
= this.cols
- 1;
3319 // CSI Pm h Set Mode (SM).
3320 // Ps = 2 -> Keyboard Action Mode (AM).
3321 // Ps = 4 -> Insert Mode (IRM).
3322 // Ps = 1 2 -> Send/receive (SRM).
3323 // Ps = 2 0 -> Automatic Newline (LNM).
3325 // DEC Private Mode Set (DECSET).
3326 // Ps = 1 -> Application Cursor Keys (DECCKM).
3327 // Ps = 2 -> Designate USASCII for character sets G0-G3
3328 // (DECANM), and set VT100 mode.
3329 // Ps = 3 -> 132 Column Mode (DECCOLM).
3330 // Ps = 4 -> Smooth (Slow) Scroll (DECSCLM).
3331 // Ps = 5 -> Reverse Video (DECSCNM).
3332 // Ps = 6 -> Origin Mode (DECOM).
3333 // Ps = 7 -> Wraparound Mode (DECAWM).
3334 // Ps = 8 -> Auto-repeat Keys (DECARM).
3335 // Ps = 9 -> Send Mouse X & Y on button press. See the sec-
3336 // tion Mouse Tracking.
3337 // Ps = 1 0 -> Show toolbar (rxvt).
3338 // Ps = 1 2 -> Start Blinking Cursor (att610).
3339 // Ps = 1 8 -> Print form feed (DECPFF).
3340 // Ps = 1 9 -> Set print extent to full screen (DECPEX).
3341 // Ps = 2 5 -> Show Cursor (DECTCEM).
3342 // Ps = 3 0 -> Show scrollbar (rxvt).
3343 // Ps = 3 5 -> Enable font-shifting functions (rxvt).
3344 // Ps = 3 8 -> Enter Tektronix Mode (DECTEK).
3345 // Ps = 4 0 -> Allow 80 -> 132 Mode.
3346 // Ps = 4 1 -> more(1) fix (see curses resource).
3347 // Ps = 4 2 -> Enable Nation Replacement Character sets (DECN-
3349 // Ps = 4 4 -> Turn On Margin Bell.
3350 // Ps = 4 5 -> Reverse-wraparound Mode.
3351 // Ps = 4 6 -> Start Logging. This is normally disabled by a
3352 // compile-time option.
3353 // Ps = 4 7 -> Use Alternate Screen Buffer. (This may be dis-
3354 // abled by the titeInhibit resource).
3355 // Ps = 6 6 -> Application keypad (DECNKM).
3356 // Ps = 6 7 -> Backarrow key sends backspace (DECBKM).
3357 // Ps = 1 0 0 0 -> Send Mouse X & Y on button press and
3358 // release. See the section Mouse Tracking.
3359 // Ps = 1 0 0 1 -> Use Hilite Mouse Tracking.
3360 // Ps = 1 0 0 2 -> Use Cell Motion Mouse Tracking.
3361 // Ps = 1 0 0 3 -> Use All Motion Mouse Tracking.
3362 // Ps = 1 0 0 4 -> Send FocusIn/FocusOut events.
3363 // Ps = 1 0 0 5 -> Enable Extended Mouse Mode.
3364 // Ps = 1 0 1 0 -> Scroll to bottom on tty output (rxvt).
3365 // Ps = 1 0 1 1 -> Scroll to bottom on key press (rxvt).
3366 // Ps = 1 0 3 4 -> Interpret "meta" key, sets eighth bit.
3367 // (enables the eightBitInput resource).
3368 // Ps = 1 0 3 5 -> Enable special modifiers for Alt and Num-
3369 // Lock keys. (This enables the numLock resource).
3370 // Ps = 1 0 3 6 -> Send ESC when Meta modifies a key. (This
3371 // enables the metaSendsEscape resource).
3372 // Ps = 1 0 3 7 -> Send DEL from the editing-keypad Delete
3374 // Ps = 1 0 3 9 -> Send ESC when Alt modifies a key. (This
3375 // enables the altSendsEscape resource).
3376 // Ps = 1 0 4 0 -> Keep selection even if not highlighted.
3377 // (This enables the keepSelection resource).
3378 // Ps = 1 0 4 1 -> Use the CLIPBOARD selection. (This enables
3379 // the selectToClipboard resource).
3380 // Ps = 1 0 4 2 -> Enable Urgency window manager hint when
3381 // Control-G is received. (This enables the bellIsUrgent
3383 // Ps = 1 0 4 3 -> Enable raising of the window when Control-G
3384 // is received. (enables the popOnBell resource).
3385 // Ps = 1 0 4 7 -> Use Alternate Screen Buffer. (This may be
3386 // disabled by the titeInhibit resource).
3387 // Ps = 1 0 4 8 -> Save cursor as in DECSC. (This may be dis-
3388 // abled by the titeInhibit resource).
3389 // Ps = 1 0 4 9 -> Save cursor as in DECSC and use Alternate
3390 // Screen Buffer, clearing it first. (This may be disabled by
3391 // the titeInhibit resource). This combines the effects of the 1
3392 // 0 4 7 and 1 0 4 8 modes. Use this with terminfo-based
3393 // applications rather than the 4 7 mode.
3394 // Ps = 1 0 5 0 -> Set terminfo/termcap function-key mode.
3395 // Ps = 1 0 5 1 -> Set Sun function-key mode.
3396 // Ps = 1 0 5 2 -> Set HP function-key mode.
3397 // Ps = 1 0 5 3 -> Set SCO function-key mode.
3398 // Ps = 1 0 6 0 -> Set legacy keyboard emulation (X11R6).
3399 // Ps = 1 0 6 1 -> Set VT220 keyboard emulation.
3400 // Ps = 2 0 0 4 -> Set bracketed paste mode.
3402 // http://vt100.net/docs/vt220-rm/chapter4.html
3403 Terminal
.prototype.setMode = function(params
) {
3404 if (typeof params
=== 'object') {
3405 var l
= params
.length
3408 for (; i
< l
; i
++) {
3409 this.setMode(params
[i
]);
3418 this.insertMode
= true;
3421 //this.convertEol = true;
3424 } else if (this.prefix
=== '?') {
3427 this.applicationCursor
= true;
3430 this.setgCharset(0, Terminal
.charsets
.US
);
3431 this.setgCharset(1, Terminal
.charsets
.US
);
3432 this.setgCharset(2, Terminal
.charsets
.US
);
3433 this.setgCharset(3, Terminal
.charsets
.US
);
3434 // set VT100 mode here
3436 case 3: // 132 col mode
3437 this.savedCols
= this.cols
;
3438 this.resize(132, this.rows
);
3441 this.originMode
= true;
3444 this.wraparoundMode
= true;
3447 // this.cursorBlink = true;
3450 this.log('Serial port requested application keypad.');
3451 this.applicationKeypad
= true;
3453 case 9: // X10 Mouse
3454 // no release, no motion, no wheel, no modifiers.
3455 case 1000: // vt200 mouse
3457 // no modifiers, except control on the wheel.
3458 case 1002: // button event mouse
3459 case 1003: // any event mouse
3460 // any event - sends motion events,
3461 // even if there is no button held down.
3462 this.x10Mouse
= params
=== 9;
3463 this.vt200Mouse
= params
=== 1000;
3464 this.normalMouse
= params
> 1000;
3465 this.mouseEvents
= true;
3466 this.element
.style
.cursor
= 'default';
3467 this.log('Binding to mouse events.');
3469 case 1004: // send focusin/focusout events
3472 this.sendFocus
= true;
3474 case 1005: // utf8 ext mode mouse
3475 this.utfMouse
= true;
3476 // for wide terminals
3477 // simply encodes large values as utf8 characters
3479 case 1006: // sgr ext mode mouse
3480 this.sgrMouse
= true;
3481 // for wide terminals
3482 // does not add 32 to fields
3483 // press: ^[[<b;x;yM
3484 // release: ^[[<b;x;ym
3486 case 1015: // urxvt ext mode mouse
3487 this.urxvtMouse
= true;
3488 // for wide terminals
3489 // numbers for fields
3491 // motion: ^[[b;x;yT
3493 case 25: // show cursor
3494 this.cursorHidden
= false;
3496 case 1049: // alt screen buffer cursor
3497 //this.saveCursor();
3499 case 47: // alt screen buffer
3500 case 1047: // alt screen buffer
3508 scrollTop
: this.scrollTop
,
3509 scrollBottom
: this.scrollBottom
,
3511 // XXX save charset(s) here?
3512 // charset: this.charset,
3513 // glevel: this.glevel,
3514 // charsets: this.charsets
3517 this.normal
= normal
;
3525 // CSI Pm l Reset Mode (RM).
3526 // Ps = 2 -> Keyboard Action Mode (AM).
3527 // Ps = 4 -> Replace Mode (IRM).
3528 // Ps = 1 2 -> Send/receive (SRM).
3529 // Ps = 2 0 -> Normal Linefeed (LNM).
3531 // DEC Private Mode Reset (DECRST).
3532 // Ps = 1 -> Normal Cursor Keys (DECCKM).
3533 // Ps = 2 -> Designate VT52 mode (DECANM).
3534 // Ps = 3 -> 80 Column Mode (DECCOLM).
3535 // Ps = 4 -> Jump (Fast) Scroll (DECSCLM).
3536 // Ps = 5 -> Normal Video (DECSCNM).
3537 // Ps = 6 -> Normal Cursor Mode (DECOM).
3538 // Ps = 7 -> No Wraparound Mode (DECAWM).
3539 // Ps = 8 -> No Auto-repeat Keys (DECARM).
3540 // Ps = 9 -> Don't send Mouse X & Y on button press.
3541 // Ps = 1 0 -> Hide toolbar (rxvt).
3542 // Ps = 1 2 -> Stop Blinking Cursor (att610).
3543 // Ps = 1 8 -> Don't print form feed (DECPFF).
3544 // Ps = 1 9 -> Limit print to scrolling region (DECPEX).
3545 // Ps = 2 5 -> Hide Cursor (DECTCEM).
3546 // Ps = 3 0 -> Don't show scrollbar (rxvt).
3547 // Ps = 3 5 -> Disable font-shifting functions (rxvt).
3548 // Ps = 4 0 -> Disallow 80 -> 132 Mode.
3549 // Ps = 4 1 -> No more(1) fix (see curses resource).
3550 // Ps = 4 2 -> Disable Nation Replacement Character sets (DEC-
3552 // Ps = 4 4 -> Turn Off Margin Bell.
3553 // Ps = 4 5 -> No Reverse-wraparound Mode.
3554 // Ps = 4 6 -> Stop Logging. (This is normally disabled by a
3555 // compile-time option).
3556 // Ps = 4 7 -> Use Normal Screen Buffer.
3557 // Ps = 6 6 -> Numeric keypad (DECNKM).
3558 // Ps = 6 7 -> Backarrow key sends delete (DECBKM).
3559 // Ps = 1 0 0 0 -> Don't send Mouse X & Y on button press and
3560 // release. See the section Mouse Tracking.
3561 // Ps = 1 0 0 1 -> Don't use Hilite Mouse Tracking.
3562 // Ps = 1 0 0 2 -> Don't use Cell Motion Mouse Tracking.
3563 // Ps = 1 0 0 3 -> Don't use All Motion Mouse Tracking.
3564 // Ps = 1 0 0 4 -> Don't send FocusIn/FocusOut events.
3565 // Ps = 1 0 0 5 -> Disable Extended Mouse Mode.
3566 // Ps = 1 0 1 0 -> Don't scroll to bottom on tty output
3568 // Ps = 1 0 1 1 -> Don't scroll to bottom on key press (rxvt).
3569 // Ps = 1 0 3 4 -> Don't interpret "meta" key. (This disables
3570 // the eightBitInput resource).
3571 // Ps = 1 0 3 5 -> Disable special modifiers for Alt and Num-
3572 // Lock keys. (This disables the numLock resource).
3573 // Ps = 1 0 3 6 -> Don't send ESC when Meta modifies a key.
3574 // (This disables the metaSendsEscape resource).
3575 // Ps = 1 0 3 7 -> Send VT220 Remove from the editing-keypad
3577 // Ps = 1 0 3 9 -> Don't send ESC when Alt modifies a key.
3578 // (This disables the altSendsEscape resource).
3579 // Ps = 1 0 4 0 -> Do not keep selection when not highlighted.
3580 // (This disables the keepSelection resource).
3581 // Ps = 1 0 4 1 -> Use the PRIMARY selection. (This disables
3582 // the selectToClipboard resource).
3583 // Ps = 1 0 4 2 -> Disable Urgency window manager hint when
3584 // Control-G is received. (This disables the bellIsUrgent
3586 // Ps = 1 0 4 3 -> Disable raising of the window when Control-
3587 // G is received. (This disables the popOnBell resource).
3588 // Ps = 1 0 4 7 -> Use Normal Screen Buffer, clearing screen
3589 // first if in the Alternate Screen. (This may be disabled by
3590 // the titeInhibit resource).
3591 // Ps = 1 0 4 8 -> Restore cursor as in DECRC. (This may be
3592 // disabled by the titeInhibit resource).
3593 // Ps = 1 0 4 9 -> Use Normal Screen Buffer and restore cursor
3594 // as in DECRC. (This may be disabled by the titeInhibit
3595 // resource). This combines the effects of the 1 0 4 7 and 1 0
3596 // 4 8 modes. Use this with terminfo-based applications rather
3597 // than the 4 7 mode.
3598 // Ps = 1 0 5 0 -> Reset terminfo/termcap function-key mode.
3599 // Ps = 1 0 5 1 -> Reset Sun function-key mode.
3600 // Ps = 1 0 5 2 -> Reset HP function-key mode.
3601 // Ps = 1 0 5 3 -> Reset SCO function-key mode.
3602 // Ps = 1 0 6 0 -> Reset legacy keyboard emulation (X11R6).
3603 // Ps = 1 0 6 1 -> Reset keyboard emulation to Sun/PC style.
3604 // Ps = 2 0 0 4 -> Reset bracketed paste mode.
3605 Terminal
.prototype.resetMode = function(params
) {
3606 if (typeof params
=== 'object') {
3607 var l
= params
.length
3610 for (; i
< l
; i
++) {
3611 this.resetMode(params
[i
]);
3620 this.insertMode
= false;
3623 //this.convertEol = false;
3626 } else if (this.prefix
=== '?') {
3629 this.applicationCursor
= false;
3632 if (this.cols
=== 132 && this.savedCols
) {
3633 this.resize(this.savedCols
, this.rows
);
3635 delete this.savedCols
;
3638 this.originMode
= false;
3641 this.wraparoundMode
= false;
3644 // this.cursorBlink = false;
3647 this.log('Switching back to normal keypad.');
3648 this.applicationKeypad
= false;
3650 case 9: // X10 Mouse
3651 case 1000: // vt200 mouse
3652 case 1002: // button event mouse
3653 case 1003: // any event mouse
3654 this.x10Mouse
= false;
3655 this.vt200Mouse
= false;
3656 this.normalMouse
= false;
3657 this.mouseEvents
= false;
3658 this.element
.style
.cursor
= '';
3660 case 1004: // send focusin/focusout events
3661 this.sendFocus
= false;
3663 case 1005: // utf8 ext mode mouse
3664 this.utfMouse
= false;
3666 case 1006: // sgr ext mode mouse
3667 this.sgrMouse
= false;
3669 case 1015: // urxvt ext mode mouse
3670 this.urxvtMouse
= false;
3672 case 25: // hide cursor
3673 this.cursorHidden
= true;
3675 case 1049: // alt screen buffer cursor
3677 case 47: // normal screen buffer
3678 case 1047: // normal screen buffer - clearing it first
3680 this.lines
= this.normal
.lines
;
3681 this.ybase
= this.normal
.ybase
;
3682 this.ydisp
= this.normal
.ydisp
;
3683 this.x
= this.normal
.x
;
3684 this.y
= this.normal
.y
;
3685 this.scrollTop
= this.normal
.scrollTop
;
3686 this.scrollBottom
= this.normal
.scrollBottom
;
3687 this.tabs
= this.normal
.tabs
;
3689 // if (params === 1049) {
3690 // this.x = this.savedX;
3691 // this.y = this.savedY;
3693 this.refresh(0, this.rows
- 1);
3702 // Set Scrolling Region [top;bottom] (default = full size of win-
3705 Terminal
.prototype.setScrollRegion = function(params
) {
3706 if (this.prefix
) return;
3707 this.scrollTop
= (params
[0] || 1) - 1;
3708 this.scrollBottom
= (params
[1] || this.rows
) - 1;
3714 // Save cursor (ANSI.SYS).
3715 Terminal
.prototype.saveCursor = function(params
) {
3716 this.savedX
= this.x
;
3717 this.savedY
= this.y
;
3721 // Restore cursor (ANSI.SYS).
3722 Terminal
.prototype.restoreCursor = function(params
) {
3723 this.x
= this.savedX
|| 0;
3724 this.y
= this.savedY
|| 0;
3732 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
3733 Terminal
.prototype.cursorForwardTab = function(params
) {
3734 var param
= params
[0] || 1;
3736 this.x
= this.nextStop();
3740 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
3741 Terminal
.prototype.scrollUp = function(params
) {
3742 var param
= params
[0] || 1;
3744 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
3745 this.lines
.splice(this.ybase
+ this.scrollBottom
, 0, this.blankLine());
3748 this.updateRange(this.scrollTop
);
3749 this.updateRange(this.scrollBottom
);
3752 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
3753 Terminal
.prototype.scrollDown = function(params
) {
3754 var param
= params
[0] || 1;
3756 this.lines
.splice(this.ybase
+ this.scrollBottom
, 1);
3757 this.lines
.splice(this.ybase
+ this.scrollTop
, 0, this.blankLine());
3760 this.updateRange(this.scrollTop
);
3761 this.updateRange(this.scrollBottom
);
3764 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
3765 // Initiate highlight mouse tracking. Parameters are
3766 // [func;startx;starty;firstrow;lastrow]. See the section Mouse
3768 Terminal
.prototype.initMouseTracking = function(params
) {
3769 // Relevant: DECSET 1001
3773 // Reset one or more features of the title modes to the default
3774 // value. Normally, "reset" disables the feature. It is possi-
3775 // ble to disable the ability to reset features by compiling a
3776 // different default for the title modes into xterm.
3777 // Ps = 0 -> Do not set window/icon labels using hexadecimal.
3778 // Ps = 1 -> Do not query window/icon labels using hexadeci-
3780 // Ps = 2 -> Do not set window/icon labels using UTF-8.
3781 // Ps = 3 -> Do not query window/icon labels using UTF-8.
3782 // (See discussion of "Title Modes").
3783 Terminal
.prototype.resetTitleModes = function(params
) {
3787 // CSI Ps Z Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
3788 Terminal
.prototype.cursorBackwardTab = function(params
) {
3789 var param
= params
[0] || 1;
3791 this.x
= this.prevStop();
3795 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
3796 Terminal
.prototype.repeatPrecedingCharacter = function(params
) {
3797 var param
= params
[0] || 1
3798 , line
= this.lines
[this.ybase
+ this.y
]
3799 , ch
= line
[this.x
- 1] || [this.defAttr
, ' '];
3801 while (param
--) line
[this.x
++] = ch
;
3804 // CSI Ps g Tab Clear (TBC).
3805 // Ps = 0 -> Clear Current Column (default).
3806 // Ps = 3 -> Clear All.
3808 // Ps = 2 -> Clear Stops on Line.
3809 // http://vt100.net/annarbor/aaa-ug/section6.html
3810 Terminal
.prototype.tabClear = function(params
) {
3811 var param
= params
[0];
3813 delete this.tabs
[this.x
];
3814 } else if (param
=== 3) {
3819 // CSI Pm i Media Copy (MC).
3820 // Ps = 0 -> Print screen (default).
3821 // Ps = 4 -> Turn off printer controller mode.
3822 // Ps = 5 -> Turn on printer controller mode.
3824 // Media Copy (MC, DEC-specific).
3825 // Ps = 1 -> Print line containing cursor.
3826 // Ps = 4 -> Turn off autoprint mode.
3827 // Ps = 5 -> Turn on autoprint mode.
3828 // Ps = 1 0 -> Print composed display, ignores DECPEX.
3829 // Ps = 1 1 -> Print all pages.
3830 Terminal
.prototype.mediaCopy = function(params
) {
3835 // Set or reset resource-values used by xterm to decide whether
3836 // to construct escape sequences holding information about the
3837 // modifiers pressed with a given key. The first parameter iden-
3838 // tifies the resource to set/reset. The second parameter is the
3839 // value to assign to the resource. If the second parameter is
3840 // omitted, the resource is reset to its initial value.
3841 // Ps = 1 -> modifyCursorKeys.
3842 // Ps = 2 -> modifyFunctionKeys.
3843 // Ps = 4 -> modifyOtherKeys.
3844 // If no parameters are given, all resources are reset to their
3846 Terminal
.prototype.setResources = function(params
) {
3851 // Disable modifiers which may be enabled via the CSI > Ps; Ps m
3852 // sequence. This corresponds to a resource value of "-1", which
3853 // cannot be set with the other sequence. The parameter identi-
3854 // fies the resource to be disabled:
3855 // Ps = 1 -> modifyCursorKeys.
3856 // Ps = 2 -> modifyFunctionKeys.
3857 // Ps = 4 -> modifyOtherKeys.
3858 // If the parameter is omitted, modifyFunctionKeys is disabled.
3859 // When modifyFunctionKeys is disabled, xterm uses the modifier
3860 // keys to make an extended sequence of functions rather than
3861 // adding a parameter to each function key to denote the modi-
3863 Terminal
.prototype.disableModifiers = function(params
) {
3868 // Set resource value pointerMode. This is used by xterm to
3869 // decide whether to hide the pointer cursor as the user types.
3870 // Valid values for the parameter:
3871 // Ps = 0 -> never hide the pointer.
3872 // Ps = 1 -> hide if the mouse tracking mode is not enabled.
3873 // Ps = 2 -> always hide the pointer. If no parameter is
3874 // given, xterm uses the default, which is 1 .
3875 Terminal
.prototype.setPointerMode = function(params
) {
3879 // CSI ! p Soft terminal reset (DECSTR).
3880 // http://vt100.net/docs/vt220-rm/table4-10.html
3881 Terminal
.prototype.softReset = function(params
) {
3882 this.cursorHidden
= false;
3883 this.insertMode
= false;
3884 this.originMode
= false;
3885 this.wraparoundMode
= false; // autowrap
3886 this.applicationKeypad
= false; // ?
3887 this.applicationCursor
= false;
3889 this.scrollBottom
= this.rows
- 1;
3890 this.curAttr
= this.defAttr
;
3891 this.x
= this.y
= 0; // ?
3892 this.charset
= null;
3893 this.glevel
= 0; // ??
3894 this.charsets
= [null]; // ??
3898 // Request ANSI mode (DECRQM). For VT300 and up, reply is
3900 // where Ps is the mode number as in RM, and Pm is the mode
3902 // 0 - not recognized
3905 // 3 - permanently set
3906 // 4 - permanently reset
3907 Terminal
.prototype.requestAnsiMode = function(params
) {
3912 // Request DEC private mode (DECRQM). For VT300 and up, reply is
3914 // where Ps is the mode number as in DECSET, Pm is the mode value
3915 // as in the ANSI DECRQM.
3916 Terminal
.prototype.requestPrivateMode = function(params
) {
3921 // Set conformance level (DECSCL). Valid values for the first
3923 // Ps = 6 1 -> VT100.
3924 // Ps = 6 2 -> VT200.
3925 // Ps = 6 3 -> VT300.
3926 // Valid values for the second parameter:
3927 // Ps = 0 -> 8-bit controls.
3928 // Ps = 1 -> 7-bit controls (always set for VT100).
3929 // Ps = 2 -> 8-bit controls.
3930 Terminal
.prototype.setConformanceLevel = function(params
) {
3934 // CSI Ps q Load LEDs (DECLL).
3935 // Ps = 0 -> Clear all LEDS (default).
3936 // Ps = 1 -> Light Num Lock.
3937 // Ps = 2 -> Light Caps Lock.
3938 // Ps = 3 -> Light Scroll Lock.
3939 // Ps = 2 1 -> Extinguish Num Lock.
3940 // Ps = 2 2 -> Extinguish Caps Lock.
3941 // Ps = 2 3 -> Extinguish Scroll Lock.
3942 Terminal
.prototype.loadLEDs = function(params
) {
3947 // Set cursor style (DECSCUSR, VT520).
3948 // Ps = 0 -> blinking block.
3949 // Ps = 1 -> blinking block (default).
3950 // Ps = 2 -> steady block.
3951 // Ps = 3 -> blinking underline.
3952 // Ps = 4 -> steady underline.
3953 Terminal
.prototype.setCursorStyle = function(params
) {
3958 // Select character protection attribute (DECSCA). Valid values
3959 // for the parameter:
3960 // Ps = 0 -> DECSED and DECSEL can erase (default).
3961 // Ps = 1 -> DECSED and DECSEL cannot erase.
3962 // Ps = 2 -> DECSED and DECSEL can erase.
3963 Terminal
.prototype.setCharProtectionAttr = function(params
) {
3968 // Restore DEC Private Mode Values. The value of Ps previously
3969 // saved is restored. Ps values are the same as for DECSET.
3970 Terminal
.prototype.restorePrivateValues = function(params
) {
3974 // CSI Pt; Pl; Pb; Pr; Ps$ r
3975 // Change Attributes in Rectangular Area (DECCARA), VT400 and up.
3976 // Pt; Pl; Pb; Pr denotes the rectangle.
3977 // Ps denotes the SGR attributes to change: 0, 1, 4, 5, 7.
3978 // NOTE: xterm doesn't enable this code by default.
3979 Terminal
.prototype.setAttrInRectangle = function(params
) {
3989 for (; t
< b
+ 1; t
++) {
3990 line
= this.lines
[this.ybase
+ t
];
3991 for (i
= l
; i
< r
; i
++) {
3992 line
[i
] = [attr
, line
[i
][1]];
3997 this.updateRange(params
[0]);
3998 this.updateRange(params
[2]);
4002 // CSI Pc; Pt; Pl; Pb; Pr$ x
4003 // Fill Rectangular Area (DECFRA), VT420 and up.
4004 // Pc is the character to use.
4005 // Pt; Pl; Pb; Pr denotes the rectangle.
4006 // NOTE: xterm doesn't enable this code by default.
4007 Terminal
.prototype.fillRectangle = function(params
) {
4017 for (; t
< b
+ 1; t
++) {
4018 line
= this.lines
[this.ybase
+ t
];
4019 for (i
= l
; i
< r
; i
++) {
4020 line
[i
] = [line
[i
][0], String
.fromCharCode(ch
)];
4025 this.updateRange(params
[1]);
4026 this.updateRange(params
[3]);
4030 // Enable Locator Reporting (DECELR).
4031 // Valid values for the first parameter:
4032 // Ps = 0 -> Locator disabled (default).
4033 // Ps = 1 -> Locator enabled.
4034 // Ps = 2 -> Locator enabled for one report, then disabled.
4035 // The second parameter specifies the coordinate unit for locator
4037 // Valid values for the second parameter:
4038 // Pu = 0 <- or omitted -> default to character cells.
4039 // Pu = 1 <- device physical pixels.
4040 // Pu = 2 <- character cells.
4041 Terminal
.prototype.enableLocatorReporting = function(params
) {
4042 var val
= params
[0] > 0;
4043 //this.mouseEvents = val;
4044 //this.decLocator = val;
4047 // CSI Pt; Pl; Pb; Pr$ z
4048 // Erase Rectangular Area (DECERA), VT400 and up.
4049 // Pt; Pl; Pb; Pr denotes the rectangle.
4050 // NOTE: xterm doesn't enable this code by default.
4051 Terminal
.prototype.eraseRectangle = function(params
) {
4061 ch
= [this.eraseAttr(), ' ']; // xterm?
4063 for (; t
< b
+ 1; t
++) {
4064 line
= this.lines
[this.ybase
+ t
];
4065 for (i
= l
; i
< r
; i
++) {
4071 this.updateRange(params
[0]);
4072 this.updateRange(params
[2]);
4077 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
4078 // NOTE: xterm doesn't enable this code by default.
4079 Terminal
.prototype.insertColumns = function() {
4080 var param
= params
[0]
4081 , l
= this.ybase
+ this.rows
4082 , ch
= [this.eraseAttr(), ' '] // xterm?
4086 for (i
= this.ybase
; i
< l
; i
++) {
4087 this.lines
[i
].splice(this.x
+ 1, 0, ch
);
4088 this.lines
[i
].pop();
4096 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
4097 // NOTE: xterm doesn't enable this code by default.
4098 Terminal
.prototype.deleteColumns = function() {
4099 var param
= params
[0]
4100 , l
= this.ybase
+ this.rows
4101 , ch
= [this.eraseAttr(), ' '] // xterm?
4105 for (i
= this.ybase
; i
< l
; i
++) {
4106 this.lines
[i
].splice(this.x
, 1);
4107 this.lines
[i
].push(ch
);
4115 Terminal
.prototype.copyBuffer = function(lines
) {
4116 var lines
= lines
|| this.lines
4119 for (var y
= 0; y
< lines
.length
; y
++) {
4121 for (var x
= 0; x
< lines
[y
].length
; x
++) {
4122 out
[y
][x
] = [lines
[y
][x
][0], lines
[y
][x
][1]];
4129 Terminal
.prototype.getCopyTextarea = function(text
) {
4130 var textarea
= this._copyTextarea
4131 , document
= this.document
;
4134 textarea
= document
.createElement('textarea');
4135 textarea
.style
.position
= 'absolute';
4136 textarea
.style
.left
= '-32000px';
4137 textarea
.style
.top
= '-32000px';
4138 textarea
.style
.width
= '0px';
4139 textarea
.style
.height
= '0px';
4140 textarea
.style
.opacity
= '0';
4141 textarea
.style
.backgroundColor
= 'transparent';
4142 textarea
.style
.borderStyle
= 'none';
4143 textarea
.style
.outlineStyle
= 'none';
4145 document
.getElementsByTagName('body')[0].appendChild(textarea
);
4147 this._copyTextarea
= textarea
;
4153 // NOTE: Only works for primary selection on X11.
4154 // Non-X11 users should use Ctrl-C instead.
4155 Terminal
.prototype.copyText = function(text
) {
4157 , textarea
= this.getCopyTextarea();
4159 this.emit('copy', text
);
4162 textarea
.textContent
= text
;
4163 textarea
.value
= text
;
4164 textarea
.setSelectionRange(0, text
.length
);
4166 setTimeout(function() {
4167 self
.element
.focus();
4172 Terminal
.prototype.keyPrefix = function(ev
, key
) {
4173 if (key
=== 'k' || key
=== '&') {
4175 } else if (key
=== 'p' || key
=== ']') {
4176 this.emit('request paste');
4177 } else if (key
=== 'c') {
4178 this.emit('request create');
4179 } else if (key
>= '0' && key
<= '9') {
4182 this.emit('request term', key
);
4183 } else if (key
=== 'n') {
4184 this.emit('request term next');
4185 } else if (key
=== 'P') {
4186 this.emit('request term previous');
4187 } else if (key
=== ':') {
4188 this.emit('request command mode');
4192 Terminal
.prototype.keySelect = function(ev
, key
) {
4195 if (key
=== '\x04') { // ctrl-d
4196 var y
= this.ydisp
+ this.y
;
4197 if (this.ydisp
=== this.ybase
) {
4198 // Mimic vim behavior
4199 this.y
= Math
.min(this.y
+ (this.rows
- 1) / 2 | 0, this.rows
- 1);
4200 this.refresh(0, this.rows
- 1);
4202 this.scrollDisp((this.rows
- 1) / 2 | 0);
4207 if (key
=== '\x15') { // ctrl-u
4208 var y
= this.ydisp
+ this.y
;
4209 if (this.ydisp
=== 0) {
4210 // Mimic vim behavior
4211 this.y
= Math
.max(this.y
- (this.rows
- 1) / 2 | 0, 0);
4212 this.refresh(0, this.rows
- 1);
4214 this.scrollDisp(-(this.rows
- 1) / 2 | 0);
4219 if (key
=== '\x06') { // ctrl-f
4220 var y
= this.ydisp
+ this.y
;
4221 this.scrollDisp(this.rows
- 1);
4225 if (key
=== '\x02') { // ctrl-b
4226 var y
= this.ydisp
+ this.y
;
4227 this.scrollDisp(-(this.rows
- 1));
4231 if (key
=== 'k' || key
=== '\x1b[A') {
4232 var y
= this.ydisp
+ this.y
;
4236 this.scrollDisp(-1);
4238 this.refresh(this.y
, this.y
+ 1);
4242 if (key
=== 'j' || key
=== '\x1b[B') {
4243 var y
= this.ydisp
+ this.y
;
4245 if (this.y
>= this.rows
) {
4246 this.y
= this.rows
- 1;
4249 this.refresh(this.y
- 1, this.y
);
4253 if (key
=== 'h' || key
=== '\x1b[D') {
4259 this.refresh(this.y
, this.y
);
4263 if (key
=== 'l' || key
=== '\x1b[C') {
4266 if (this.x
>= this.cols
) {
4267 this.x
= this.cols
- 1;
4269 this.refresh(this.y
, this.y
);
4273 if (key
=== 'w' || key
=== 'W') {
4276 var oyd
= this.ydisp
;
4280 var yb
= this.ydisp
;
4281 var saw_space
= false;
4284 var line
= this.lines
[yb
+ y
];
4285 while (x
< this.cols
) {
4286 if (line
[x
][1] <= ' ') {
4288 } else if (saw_space
) {
4293 if (x
>= this.cols
) x
= this.cols
- 1;
4294 if (x
=== this.cols
- 1 && line
[x
][1] <= ' ') {
4296 if (++y
>= this.rows
) {
4298 if (++yb
> this.ybase
) {
4309 this.x
= x
, this.y
= y
;
4310 this.scrollDisp(-this.ydisp
+ yb
);
4315 if (key
=== 'b' || key
=== 'B') {
4318 var oyd
= this.ydisp
;
4322 var yb
= this.ydisp
;
4325 var line
= this.lines
[yb
+ y
];
4326 var saw_space
= x
> 0 && line
[x
][1] > ' ' && line
[x
- 1][1] > ' ';
4328 if (line
[x
][1] <= ' ') {
4329 if (saw_space
&& (x
+ 1 < this.cols
&& line
[x
+ 1][1] > ' ')) {
4339 if (x
=== 0 && (line
[x
][1] <= ' ' || !saw_space
)) {
4354 this.x
= x
, this.y
= y
;
4355 this.scrollDisp(-this.ydisp
+ yb
);
4360 if (key
=== 'e' || key
=== 'E') {
4363 var yb
= this.ydisp
;
4364 if (x
>= this.cols
) x
--;
4367 var line
= this.lines
[yb
+ y
];
4368 while (x
< this.cols
) {
4369 if (line
[x
][1] <= ' ') {
4375 while (x
< this.cols
) {
4376 if (line
[x
][1] <= ' ') {
4377 if (x
- 1 >= 0 && line
[x
- 1][1] > ' ') {
4384 if (x
>= this.cols
) x
= this.cols
- 1;
4385 if (x
=== this.cols
- 1 && line
[x
][1] <= ' ') {
4387 if (++y
>= this.rows
) {
4389 if (++yb
> this.ybase
) {
4399 this.x
= x
, this.y
= y
;
4400 this.scrollDisp(-this.ydisp
+ yb
);
4405 if (key
=== '^' || key
=== '0') {
4410 } else if (key
=== '^') {
4411 var line
= this.lines
[this.ydisp
+ this.y
];
4413 while (x
< this.cols
) {
4414 if (line
[x
][1] > ' ') {
4419 if (x
>= this.cols
) x
= this.cols
- 1;
4423 this.refresh(this.y
, this.y
);
4429 var line
= this.lines
[this.ydisp
+ this.y
];
4430 var x
= this.cols
- 1;
4436 this.refresh(this.y
, this.y
);
4440 if (key
=== 'g' || key
=== 'G') {
4443 var oyd
= this.ydisp
;
4445 this.x
= 0, this.y
= 0;
4446 this.scrollDisp(-this.ydisp
);
4447 } else if (key
=== 'G') {
4448 this.x
= 0, this.y
= this.rows
- 1;
4449 this.scrollDisp(this.ybase
);
4454 if (key
=== 'H' || key
=== 'M' || key
=== 'L') {
4458 this.x
= 0, this.y
= 0;
4459 } else if (key
=== 'M') {
4460 this.x
= 0, this.y
= this.rows
/ 2 | 0;
4461 } else if (key
=== 'L') {
4462 this.x
= 0, this.y
= this.rows
- 1;
4464 this.refresh(oy
, oy
);
4465 this.refresh(this.y
, this.y
);
4469 if (key
=== '{' || key
=== '}') {
4472 var oyd
= this.ydisp
;
4475 var saw_full
= false;
4477 var first_is_space
= -1;
4478 var y
= this.y
+ (key
=== '{' ? -1 : 1);
4479 var yb
= this.ydisp
;
4487 } else if (key
=== '}') {
4488 if (y
>= this.rows
) {
4490 if (yb
< this.ybase
) yb
++;
4495 line
= this.lines
[yb
+ y
];
4497 for (i
= 0; i
< this.cols
; i
++) {
4498 if (line
[i
][1] > ' ') {
4499 if (first_is_space
=== -1) {
4504 } else if (i
=== this.cols
- 1) {
4505 if (first_is_space
=== -1) {
4507 } else if (first_is_space
=== 0) {
4509 } else if (first_is_space
=== 1) {
4510 if (saw_full
) found
= true;
4525 } else if (key
=== '}') {
4527 if (y
>= this.rows
) {
4529 if (yb
< this.ybase
) yb
++;
4539 } else if (key
=== '}') {
4545 this.x
= 0, this.y
= y
;
4546 this.scrollDisp(-this.ydisp
+ yb
);
4558 Terminal
.charsets
= {};
4560 // DEC Special Character and Line Drawing Set.
4561 // http://vt100.net/docs/vt102-ug/table5-13.html
4562 // A lot of curses apps use this if they see TERM=xterm.
4563 // testing: echo -e '\e(0a\e(B'
4564 // The xterm output sometimes seems to conflict with the
4565 // reference above. xterm seems in line with the reference
4566 // when running vttest however.
4567 // The table below now uses xterm's output from vttest.
4568 Terminal
.charsets
.SCLD
= { // (0
4569 '`': '\u25c6', // '◆'
4570 'a': '\u2592', // '▒'
4571 'b': '\u0009', // '\t'
4572 'c': '\u000c', // '\f'
4573 'd': '\u000d', // '\r'
4574 'e': '\u000a', // '\n'
4575 'f': '\u00b0', // '°'
4576 'g': '\u00b1', // '±'
4577 'h': '\u2424', // '\u2424' (NL)
4578 'i': '\u000b', // '\v'
4579 'j': '\u2518', // '┘'
4580 'k': '\u2510', // '┐'
4581 'l': '\u250c', // '┌'
4582 'm': '\u2514', // '└'
4583 'n': '\u253c', // '┼'
4584 'o': '\u23ba', // '⎺'
4585 'p': '\u23bb', // '⎻'
4586 'q': '\u2500', // '─'
4587 'r': '\u23bc', // '⎼'
4588 's': '\u23bd', // '⎽'
4589 't': '\u251c', // '├'
4590 'u': '\u2524', // '┤'
4591 'v': '\u2534', // '┴'
4592 'w': '\u252c', // '┬'
4593 'x': '\u2502', // '│'
4594 'y': '\u2264', // '≤'
4595 'z': '\u2265', // '≥'
4596 '{': '\u03c0', // 'π'
4597 '|': '\u2260', // '≠'
4598 '}': '\u00a3', // '£'
4599 '~': '\u00b7' // '·'
4602 Terminal
.charsets
.UK
= null; // (A
4603 Terminal
.charsets
.US
= null; // (B (USASCII)
4604 Terminal
.charsets
.Dutch
= null; // (4
4605 Terminal
.charsets
.Finnish
= null; // (C or (5
4606 Terminal
.charsets
.French
= null; // (R
4607 Terminal
.charsets
.FrenchCanadian
= null; // (Q
4608 Terminal
.charsets
.German
= null; // (K
4609 Terminal
.charsets
.Italian
= null; // (Y
4610 Terminal
.charsets
.NorwegianDanish
= null; // (E or (6
4611 Terminal
.charsets
.Spanish
= null; // (Z
4612 Terminal
.charsets
.Swedish
= null; // (H or (7
4613 Terminal
.charsets
.Swiss
= null; // (=
4614 Terminal
.charsets
.ISOLatin
= null; // /A
4620 function on(el
, type
, handler
, capture
) {
4621 if (!Array
.isArray(el
)) {
4624 el
.forEach(function (element
) {
4625 element
.addEventListener(type
, handler
, capture
|| false);
4629 function off(el
, type
, handler
, capture
) {
4630 el
.removeEventListener(type
, handler
, capture
|| false);
4633 function cancel(ev
, force
) {
4634 if (!this.cancelEvents
&& !force
) {
4637 ev
.preventDefault();
4638 ev
.stopPropagation();
4642 function inherits(child
, parent
) {
4644 this.constructor = child
;
4646 f
.prototype = parent
.prototype;
4647 child
.prototype = new f
;
4650 // if bold is broken, we can't
4651 // use it in the terminal.
4652 function isBoldBroken(document
) {
4653 var body
= document
.getElementsByTagName('body')[0];
4654 var el
= document
.createElement('span');
4655 el
.innerHTML
= 'hello world';
4656 body
.appendChild(el
);
4657 var w1
= el
.scrollWidth
;
4658 el
.style
.fontWeight
= 'bold';
4659 var w2
= el
.scrollWidth
;
4660 body
.removeChild(el
);
4664 var String
= this.String
;
4665 var setTimeout
= this.setTimeout
;
4666 var setInterval
= this.setInterval
;
4668 function indexOf(obj
, el
) {
4671 if (obj
[i
] === el
) return i
;
4676 function isWide(ch
) {
4677 if (ch
<= '\uff00') return false;
4678 return (ch
>= '\uff01' && ch
<= '\uffbe')
4679 || (ch
>= '\uffc2' && ch
<= '\uffc7')
4680 || (ch
>= '\uffca' && ch
<= '\uffcf')
4681 || (ch
>= '\uffd2' && ch
<= '\uffd7')
4682 || (ch
>= '\uffda' && ch
<= '\uffdc')
4683 || (ch
>= '\uffe0' && ch
<= '\uffe6')
4684 || (ch
>= '\uffe8' && ch
<= '\uffee');
4687 function matchColor(r1
, g1
, b1
) {
4688 var hash
= (r1
<< 16) | (g1
<< 8) | b1
;
4690 if (matchColor
._cache
[hash
] != null) {
4691 return matchColor
._cache
[hash
];
4694 var ldiff
= Infinity
4703 for (; i
< Terminal
.vcolors
.length
; i
++) {
4704 c
= Terminal
.vcolors
[i
];
4709 diff
= matchColor
.distance(r1
, g1
, b1
, r2
, g2
, b2
);
4722 return matchColor
._cache
[hash
] = li
;
4725 matchColor
._cache
= {};
4727 // http://stackoverflow.com/questions/1633828
4728 matchColor
.distance = function(r1
, g1
, b1
, r2
, g2
, b2
) {
4729 return Math
.pow(30 * (r1
- r2
), 2)
4730 + Math
.pow(59 * (g1
- g2
), 2)
4731 + Math
.pow(11 * (b1
- b2
), 2);
4734 function each(obj
, iter
, con
) {
4735 if (obj
.forEach
) return obj
.forEach(iter
, con
);
4736 for (var i
= 0; i
< obj
.length
; i
++) {
4737 iter
.call(con
, obj
[i
], i
, obj
);
4741 function keys(obj
) {
4742 if (Object
.keys
) return Object
.keys(obj
);
4745 if (Object
.prototype.hasOwnProperty
.call(obj
, key
)) {
4756 Terminal
.EventEmitter
= EventEmitter
;
4757 Terminal
.inherits
= inherits
;
4760 Terminal
.cancel
= cancel
;