]>
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
37 * Terminal Emulation References:
39 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.txt
40 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
41 * http://invisible-island.net/vttest/
42 * http://www.inwap.com/pdp10/ansicode.txt
43 * http://linux.die.net/man/4/console_codes
44 * http://linux.die.net/man/7/urxvt
54 , document
= this.document
;
60 function EventEmitter() {
61 this._events
= this._events
|| {};
64 EventEmitter
.prototype.addListener = function(type
, listener
) {
65 this._events
[type
] = this._events
[type
] || [];
66 this._events
[type
].push(listener
);
69 EventEmitter
.prototype.on
= EventEmitter
.prototype.addListener
;
71 EventEmitter
.prototype.removeListener = function(type
, listener
) {
72 if (!this._events
[type
]) return;
74 var obj
= this._events
[type
]
78 if (obj
[i
] === listener
|| obj
[i
].listener
=== listener
) {
85 EventEmitter
.prototype.off
= EventEmitter
.prototype.removeListener
;
87 EventEmitter
.prototype.removeAllListeners = function(type
) {
88 if (this._events
[type
]) delete this._events
[type
];
91 EventEmitter
.prototype.once = function(type
, listener
) {
93 var args
= Array
.prototype.slice
.call(arguments
);
94 this.removeListener(type
, on
);
95 return listener
.apply(this, args
);
97 on
.listener
= listener
;
98 return this.on(type
, on
);
101 EventEmitter
.prototype.emit = function(type
) {
102 if (!this._events
[type
]) return;
104 var args
= Array
.prototype.slice
.call(arguments
, 1)
105 , obj
= this._events
[type
]
110 obj
[i
].apply(this, args
);
114 EventEmitter
.prototype.listeners = function(type
) {
115 return this._events
[type
] = this._events
[type
] || [];
134 function Terminal(options
) {
137 if (!(this instanceof Terminal
)) {
138 return new Terminal(arguments
[0], arguments
[1], arguments
[2]);
141 EventEmitter
.call(this);
143 if (typeof options
=== 'number') {
147 handler
: arguments
[2]
151 options
= options
|| {};
153 each(keys(Terminal
.defaults
), function(key
) {
154 if (options
[key
] == null) {
155 options
[key
] = Terminal
.options
[key
];
157 if (Terminal
[key
] !== Terminal
.defaults
[key
]) {
158 options
[key
] = Terminal
[key
];
161 self
[key
] = options
[key
];
164 if (options
.colors
.length
=== 8) {
165 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(8));
166 } else if (options
.colors
.length
=== 16) {
167 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(16));
168 } else if (options
.colors
.length
=== 10) {
169 options
.colors
= options
.colors
.slice(0, -2).concat(
170 Terminal
._colors
.slice(8, -2), options
.colors
.slice(-2));
171 } else if (options
.colors
.length
=== 18) {
172 options
.colors
= options
.colors
.concat(
173 Terminal
._colors
.slice(16, -2), options
.colors
.slice(-2));
175 this.colors
= options
.colors
;
177 this.options
= options
;
179 // this.context = options.context || window;
180 // this.document = options.document || document;
181 this.parent
= options
.body
|| options
.parent
182 || (document
? document
.getElementsByTagName('body')[0] : null);
184 this.cols
= options
.cols
|| options
.geometry
[0];
185 this.rows
= options
.rows
|| options
.geometry
[1];
187 if (options
.handler
) {
188 this.on('data', options
.handler
);
195 this.cursorState
= 0;
196 this.cursorHidden
= false;
201 this.scrollBottom
= this.rows
- 1;
204 this.applicationKeypad
= false;
205 this.applicationCursor
= false;
206 this.originMode
= false;
207 this.insertMode
= false;
208 this.wraparoundMode
= false;
212 this.prefixMode
= false;
213 this.selectMode
= false;
214 this.visualMode
= false;
215 this.searchMode
= false;
218 this.entryPrefix
= 'Search: ';
225 this.gcharset
= null;
227 this.charsets
= [null];
251 this.readable
= true;
252 this.writable
= true;
254 this.defAttr
= (0 << 18) | (257 << 9) | (256 << 0);
255 this.curAttr
= this.defAttr
;
258 this.currentParam
= 0;
265 this.lines
.push(this.blankLine());
272 inherits(Terminal
, EventEmitter
);
274 // back_color_erase feature for xterm.
275 Terminal
.prototype.eraseAttr = function() {
276 // if (this.is('screen')) return this.defAttr;
277 return (this.defAttr
& ~0x1ff) | (this.curAttr
& 0x1ff);
285 Terminal
.tangoColors
= [
306 Terminal
.xtermColors
= [
311 '#cdcd00', // yellow3
313 '#cd00cd', // magenta3
321 '#5c5cff', // rgb:5c/5c/ff
322 '#ff00ff', // magenta
327 // Colors 0-15 + 16-255
328 // Much thanks to TooTallNate for writing this.
329 Terminal
.colors
= (function() {
330 var colors
= Terminal
.tangoColors
.slice()
331 , r
= [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff]
336 for (; i
< 216; i
++) {
337 out(r
[(i
/ 36) % 6 | 0], r
[(i
/ 6) % 6 | 0], r
[i
% 6]);
342 for (; i
< 24; i
++) {
347 function out(r
, g
, b
) {
348 colors
.push('#' + hex(r
) + hex(g
) + hex(b
));
353 return c
.length
< 2 ? '0' + c
: c
;
360 Terminal
.colors
[256] = '#000000';
361 Terminal
.colors
[257] = '#f0f0f0';
363 Terminal
._colors
= Terminal
.colors
.slice();
365 Terminal
.vcolors
= (function() {
367 , colors
= Terminal
.colors
371 for (; i
< 256; i
++) {
372 color
= parseInt(colors
[i
].substring(1), 16);
374 (color
>> 16) & 0xff,
387 Terminal
.defaults
= {
388 colors
: Terminal
.colors
,
399 // programFeatures: false,
403 Terminal
.options
= {};
405 each(keys(Terminal
.defaults
), function(key
) {
406 Terminal
[key
] = Terminal
.defaults
[key
];
407 Terminal
.options
[key
] = Terminal
.defaults
[key
];
414 Terminal
.focus
= null;
416 Terminal
.prototype.focus = function() {
417 if (Terminal
.focus
=== this) return;
419 if (Terminal
.focus
) {
420 Terminal
.focus
.blur();
423 if (this.sendFocus
) this.send('\x1b[I');
427 // this.element.focus();
432 // this.emit('focus');
434 Terminal
.focus
= this;
437 Terminal
.prototype.blur = function() {
438 if (Terminal
.focus
!== this) return;
440 this.cursorState
= 0;
441 this.refresh(this.y
, this.y
);
442 if (this.sendFocus
) this.send('\x1b[O');
445 // this.element.blur();
450 // this.emit('blur');
452 Terminal
.focus
= null;
456 * Initialize global behavior
459 Terminal
.prototype.initGlobal = function() {
460 var document
= this.document
;
462 Terminal
._boundDocs
= Terminal
._boundDocs
|| [];
463 if (~indexOf(Terminal
._boundDocs
, document
)) {
466 Terminal
._boundDocs
.push(document
);
468 Terminal
.bindPaste(document
);
470 Terminal
.bindKeys(document
);
472 Terminal
.bindCopy(document
);
474 if (this.isIpad
|| this.isIphone
) {
475 Terminal
.fixIpad(document
);
479 Terminal
.insertStyle(document
, this.colors
[256], this.colors
[257]);
484 * Bind to paste event
487 Terminal
.bindPaste = function(document
) {
488 // This seems to work well for ctrl-V and middle-click,
489 // even without the contentEditable workaround.
490 var window
= document
.defaultView
;
491 on(window
, 'paste', function(ev
) {
492 var term
= Terminal
.focus
;
494 if (ev
.clipboardData
) {
495 term
.send(ev
.clipboardData
.getData('text/plain'));
496 } else if (term
.context
.clipboardData
) {
497 term
.send(term
.context
.clipboardData
.getData('Text'));
499 // Not necessary. Do it anyway for good measure.
500 term
.element
.contentEditable
= 'inherit';
506 * Global Events for key handling
509 Terminal
.bindKeys = function(document
) {
510 // We should only need to check `target === body` below,
511 // but we can check everything for good measure.
512 on(document
, 'keydown', function(ev
) {
513 if (!Terminal
.focus
) return;
514 var target
= ev
.target
|| ev
.srcElement
;
516 if (target
=== Terminal
.focus
.element
517 || target
=== Terminal
.focus
.context
518 || target
=== Terminal
.focus
.document
519 || target
=== Terminal
.focus
.body
520 || target
=== Terminal
._textarea
521 || target
=== Terminal
.focus
.parent
) {
522 return Terminal
.focus
.keyDown(ev
);
526 on(document
, 'keypress', function(ev
) {
527 if (!Terminal
.focus
) return;
528 var target
= ev
.target
|| ev
.srcElement
;
530 if (target
=== Terminal
.focus
.element
531 || target
=== Terminal
.focus
.context
532 || target
=== Terminal
.focus
.document
533 || target
=== Terminal
.focus
.body
534 || target
=== Terminal
._textarea
535 || target
=== Terminal
.focus
.parent
) {
536 return Terminal
.focus
.keyPress(ev
);
540 // If we click somewhere other than a
541 // terminal, unfocus the terminal.
542 on(document
, 'mousedown', function(ev
) {
543 if (!Terminal
.focus
) return;
545 var el
= ev
.target
|| ev
.srcElement
;
549 if (el
=== Terminal
.focus
.element
) return;
550 } while (el
= el
.parentNode
);
552 Terminal
.focus
.blur();
557 * Copy Selection w/ Ctrl-C (Select Mode)
560 Terminal
.bindCopy = function(document
) {
561 var window
= document
.defaultView
;
563 // if (!('onbeforecopy' in document)) {
564 // // Copies to *only* the clipboard.
565 // on(window, 'copy', function fn(ev) {
566 // var term = Terminal.focus;
567 // if (!term) return;
568 // if (!term._selected) return;
569 // var text = term.grabText(
570 // term._selected.x1, term._selected.x2,
571 // term._selected.y1, term._selected.y2);
572 // term.emit('copy', text);
573 // ev.clipboardData.setData('text/plain', text);
578 // Copies to primary selection *and* clipboard.
579 // NOTE: This may work better on capture phase,
580 // or using the `beforecopy` event.
581 on(window
, 'copy', function(ev
) {
582 var term
= Terminal
.focus
;
584 if (!term
._selected
) return;
585 var textarea
= term
.getCopyTextarea();
586 var text
= term
.grabText(
587 term
._selected
.x1
, term
._selected
.x2
,
588 term
._selected
.y1
, term
._selected
.y2
);
589 term
.emit('copy', text
);
591 textarea
.textContent
= text
;
592 textarea
.value
= text
;
593 textarea
.setSelectionRange(0, text
.length
);
594 setTimeout(function() {
595 term
.element
.focus();
602 * Fix iPad - no idea if this works
605 Terminal
.fixIpad = function(document
) {
606 var textarea
= document
.createElement('textarea');
607 textarea
.style
.position
= 'absolute';
608 textarea
.style
.left
= '-32000px';
609 textarea
.style
.top
= '-32000px';
610 textarea
.style
.width
= '0px';
611 textarea
.style
.height
= '0px';
612 textarea
.style
.opacity
= '0';
613 textarea
.style
.backgroundColor
= 'transparent';
614 textarea
.style
.borderStyle
= 'none';
615 textarea
.style
.outlineStyle
= 'none';
616 textarea
.autocapitalize
='none';
617 textarea
.autocorrect
='off';
619 document
.getElementsByTagName('body')[0].appendChild(textarea
);
621 Terminal
._textarea
= textarea
;
623 setTimeout(function() {
629 * Insert a default style
632 Terminal
.insertStyle = function(document
, bg
, fg
) {
633 var style
= document
.getElementById('term-style');
636 var head
= document
.getElementsByTagName('head')[0];
639 var style
= document
.createElement('style');
640 style
.id
= 'term-style';
642 // textContent doesn't work well with IE for <style> elements.
646 + ' border: ' + bg
+ ' solid 5px;\n'
647 + ' font-family: "DejaVu Sans Mono", "Liberation Mono", monospace;\n'
648 + ' font-size: 11px;\n'
649 + ' color: ' + fg
+ ';\n'
650 + ' background: ' + bg
+ ';\n'
653 + '.terminal-cursor {\n'
654 + ' color: ' + bg
+ ';\n'
655 + ' background: ' + fg
+ ';\n'
659 // each(Terminal.colors, function(color, i) {
661 // out += '\n.term-bg-color-default { background-color: ' + color + '; }';
664 // out += '\n.term-fg-color-default { color: ' + color + '; }';
666 // out += '\n.term-bg-color-' + i + ' { background-color: ' + color + '; }';
667 // out += '\n.term-fg-color-' + i + ' { color: ' + color + '; }';
669 // style.innerHTML += out + '\n';
671 head
.insertBefore(style
, head
.firstChild
);
678 Terminal
.prototype.open = function(parent
) {
683 this.parent
= parent
|| this.parent
;
686 throw new Error('Terminal requires a parent element.');
689 // Grab global elements.
690 this.context
= this.parent
.ownerDocument
.defaultView
;
691 this.document
= this.parent
.ownerDocument
;
692 this.body
= this.document
.getElementsByTagName('body')[0];
694 // Parse user-agent strings.
695 if (this.context
.navigator
&& this.context
.navigator
.userAgent
) {
696 this.isMac
= !!~this.context
.navigator
.userAgent
.indexOf('Mac');
697 this.isIpad
= !!~this.context
.navigator
.userAgent
.indexOf('iPad');
698 this.isIphone
= !!~this.context
.navigator
.userAgent
.indexOf('iPhone');
699 this.isMSIE
= !!~this.context
.navigator
.userAgent
.indexOf('MSIE');
702 // Create our main terminal element.
703 this.element
= this.document
.createElement('div');
704 this.element
.className
= 'terminal';
705 this.element
.setAttribute('tabindex', 0);
707 // Create the lines for our terminal.
709 for (; i
< this.rows
; i
++) {
710 div
= this.document
.createElement('div');
711 this.element
.appendChild(div
);
712 this.children
.push(div
);
714 this.parent
.appendChild(this.element
);
717 this.refresh(0, this.rows
- 1);
719 // Initialize global actions that
720 // need to be taken on the document.
723 // Ensure there is a Terminal.focus.
726 // Start blinking the cursor.
729 // Bind to DOM events related
730 // to focus and paste behavior.
731 on(this.element
, 'focus', function() {
733 if (self
.isIpad
|| self
.isIphone
) {
734 Terminal
._textarea
.focus();
738 // This causes slightly funky behavior.
739 // on(this.element, 'blur', function() {
743 on(this.element
, 'mousedown', function() {
747 // Clickable paste workaround, using contentEditable.
748 // This probably shouldn't work,
749 // ... but it does. Firefox's paste
750 // event seems to only work for textareas?
751 on(this.element
, 'mousedown', function(ev
) {
752 var button
= ev
.button
!= null
760 button
= button
=== 1 ? 0 : button
=== 4 ? 1 : button
;
763 if (button
!== 2) return;
765 self
.element
.contentEditable
= 'true';
766 setTimeout(function() {
767 self
.element
.contentEditable
= 'inherit'; // 'false';
771 // Listen for mouse events and translate
772 // them into terminal mouse protocols.
775 // Figure out whether boldness affects
776 // the character width of monospace fonts.
777 if (Terminal
.brokenBold
== null) {
778 Terminal
.brokenBold
= isBoldBroken(this.document
);
781 // this.emit('open');
783 // This can be useful for pasting,
784 // as well as the iPad fix.
785 setTimeout(function() {
786 self
.element
.focus();
790 // XTerm mouse events
791 // http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#Mouse%20Tracking
792 // To better understand these
793 // the xterm code is very helpful:
795 // button.c, charproc.c, misc.c
796 // Relevant functions in xterm/button.c:
797 // BtnCode, EmitButtonCode, EditorButton, SendMousePosition
798 Terminal
.prototype.bindMouse = function() {
799 var el
= this.element
803 var wheelEvent
= 'onmousewheel' in this.context
807 // mouseup, mousedown, mousewheel
808 // left click: ^[[M 3<^[[M#3<
809 // mousewheel up: ^[[M`3>
810 function sendButton(ev
) {
814 // get the xterm-style button
815 button
= getButton(ev
);
817 // get mouse coordinates
821 sendEvent(button
, pos
);
828 // keep it at the left
829 // button, just in case.
840 // motion example of a left click:
841 // ^[[M 3<^[[M@4<^[[M@5<^[[M@6<^[[M@7<^[[M#7<
842 function sendMove(ev
) {
849 // buttons marked as motions
850 // are incremented by 32
853 sendEvent(button
, pos
);
857 // position to characters
858 function encode(data
, ch
) {
859 if (!self
.utfMouse
) {
860 if (ch
=== 255) return data
.push(0);
861 if (ch
> 127) ch
= 127;
864 if (ch
=== 2047) return data
.push(0);
868 if (ch
> 2047) ch
= 2047;
869 data
.push(0xC0 | (ch
>> 6));
870 data
.push(0x80 | (ch
& 0x3F));
875 // send a mouse event:
876 // regular/utf8: ^[[M Cb Cx Cy
877 // urxvt: ^[[ Cb ; Cx ; Cy M
878 // sgr: ^[[ Cb ; Cx ; Cy M/m
879 // vt300: ^[[ 24(1/3/5)~ [ Cx , Cy ] \r
880 // locator: CSI P e ; P b ; P r ; P c ; P p & w
881 function sendEvent(button
, pos
) {
882 // self.emit('mouse', {
888 if (self
.vt300Mouse
) {
890 // http://www.vt100.net/docs/vt3xx-gp/chapter15.html
894 var data
= '\x1b[24';
895 if (button
=== 0) data
+= '1';
896 else if (button
=== 1) data
+= '3';
897 else if (button
=== 2) data
+= '5';
898 else if (button
=== 3) return;
900 data
+= '~[' + pos
.x
+ ',' + pos
.y
+ ']\r';
905 if (self
.decLocator
) {
910 if (button
=== 0) button
= 2;
911 else if (button
=== 1) button
= 4;
912 else if (button
=== 2) button
= 6;
913 else if (button
=== 3) button
= 3;
917 + (button
=== 3 ? 4 : 0)
928 if (self
.urxvtMouse
) {
933 self
.send('\x1b[' + button
+ ';' + pos
.x
+ ';' + pos
.y
+ 'M');
941 + ((button
& 3) === 3 ? button
& ~3 : button
)
946 + ((button
& 3) === 3 ? 'm' : 'M'));
952 encode(data
, button
);
956 self
.send('\x1b[M' + String
.fromCharCode
.apply(String
, data
));
959 function getButton(ev
) {
972 // 1, and 2 - with 64 added
975 button
= ev
.button
!= null
982 button
= button
=== 1 ? 0 : button
=== 4 ? 1 : button
;
988 case 'DOMMouseScroll':
989 button
= ev
.detail
< 0
994 button
= ev
.wheelDeltaY
> 0
1000 // next three bits are the modifiers:
1001 // 4 = shift, 8 = meta, 16 = control
1002 shift
= ev
.shiftKey
? 4 : 0;
1003 meta
= ev
.metaKey
? 8 : 0;
1004 ctrl
= ev
.ctrlKey
? 16 : 0;
1005 mod
= shift
| meta
| ctrl
;
1008 if (self
.vt200Mouse
) {
1011 } else if (!self
.normalMouse
) {
1016 button
= (32 + (mod
<< 2)) + button
;
1021 // mouse coordinates measured in cols/rows
1022 function getCoords(ev
) {
1025 // ignore browsers without pageX for now
1026 if (ev
.pageX
== null) return;
1032 // should probably check offsetParent
1033 // but this is more portable
1034 while (el
&& el
!== self
.document
.documentElement
) {
1037 el
= 'offsetParent' in el
1042 // convert to cols/rows
1043 w
= self
.element
.clientWidth
;
1044 h
= self
.element
.clientHeight
;
1045 x
= Math
.round((x
/ w
) * self
.cols
);
1046 y
= Math
.round((y
/ h
) * self
.rows
);
1048 // be sure to avoid sending
1049 // bad positions to the program
1051 if (x
> self
.cols
) x
= self
.cols
;
1053 if (y
> self
.rows
) y
= self
.rows
;
1055 // xterm sends raw bytes and
1056 // starts at 32 (SP) for each.
1063 type
: ev
.type
=== wheelEvent
1069 on(el
, 'mousedown', function(ev
) {
1070 if (!self
.mouseEvents
) return;
1079 //if (self.vt200Mouse && !self.normalMouse) {
1080 if (self
.vt200Mouse
) {
1081 sendButton({ __proto__
: ev
, type
: 'mouseup' });
1086 if (self
.normalMouse
) on(self
.document
, 'mousemove', sendMove
);
1088 // x10 compatibility mode can't send button releases
1089 if (!self
.x10Mouse
) {
1090 on(self
.document
, 'mouseup', function up(ev
) {
1092 if (self
.normalMouse
) off(self
.document
, 'mousemove', sendMove
);
1093 off(self
.document
, 'mouseup', up
);
1101 //if (self.normalMouse) {
1102 // on(self.document, 'mousemove', sendMove);
1105 on(el
, wheelEvent
, function(ev
) {
1106 if (!self
.mouseEvents
) return;
1109 || self
.decLocator
) return;
1114 // allow mousewheel scrolling in
1115 // the shell for example
1116 on(el
, wheelEvent
, function(ev
) {
1117 if (self
.mouseEvents
) return;
1118 if (self
.applicationKeypad
) return;
1119 if (ev
.type
=== 'DOMMouseScroll') {
1120 self
.scrollDisp(ev
.detail
< 0 ? -5 : 5);
1122 self
.scrollDisp(ev
.wheelDeltaY
> 0 ? -5 : 5);
1132 Terminal
.prototype.destroy = function() {
1133 this.readable
= false;
1134 this.writable
= false;
1136 this.handler = function() {};
1137 this.write = function() {};
1138 if (this.element
.parentNode
) {
1139 this.element
.parentNode
.removeChild(this.element
);
1141 //this.emit('close');
1148 // In the screen buffer, each character
1149 // is stored as a an array with a character
1150 // and a 32-bit integer.
1151 // First value: a utf-16 character.
1153 // Next 9 bits: background color (0-511).
1154 // Next 9 bits: foreground color (0-511).
1155 // Next 14 bits: a mask for misc. flags:
1156 // 1=bold, 2=underline, 4=blink, 8=inverse, 16=invisible
1158 Terminal
.prototype.refresh = function(start
, end
) {
1174 if (end
- start
>= this.rows
/ 2) {
1175 parent
= this.element
.parentNode
;
1176 if (parent
) parent
.removeChild(this.element
);
1182 if (end
>= this.lines
.length
) {
1183 this.log('`end` is too large. Most likely a bad CSR.');
1184 end
= this.lines
.length
- 1;
1187 for (; y
<= end
; y
++) {
1188 row
= y
+ this.ydisp
;
1190 line
= this.lines
[row
];
1195 && (this.ydisp
=== this.ybase
|| this.selectMode
)
1196 && !this.cursorHidden
) {
1202 attr
= this.defAttr
;
1205 for (; i
< width
; i
++) {
1209 if (i
=== x
) data
= -1;
1211 if (data
!== attr
) {
1212 if (attr
!== this.defAttr
) {
1215 if (data
!== this.defAttr
) {
1217 out
+= '<span class="reverse-video terminal-cursor">';
1219 out
+= '<span style="';
1222 fg
= (data
>> 9) & 0x1ff;
1227 if (!Terminal
.brokenBold
) {
1228 out
+= 'font-weight:bold;';
1230 // See: XTerm*boldColors
1231 if (fg
< 8) fg
+= 8;
1236 out
+= 'text-decoration:underline;';
1242 out
= out
.slice(0, -1);
1245 out
+= 'text-decoration:blink;';
1251 bg
= (data
>> 9) & 0x1ff;
1253 // Should inverse just be before the
1254 // above boldColors effect instead?
1255 if ((flags
& 1) && fg
< 8) fg
+= 8;
1260 out
+= 'visibility:hidden;';
1263 // out += '" class="'
1264 // + 'term-bg-color-' + bg
1266 // + 'term-fg-color-' + fg
1270 out
+= 'background-color:'
1300 if (isWide(ch
)) i
++;
1309 if (attr
!== this.defAttr
) {
1313 this.children
[y
].innerHTML
= out
;
1316 if (parent
) parent
.appendChild(this.element
);
1319 Terminal
.prototype._cursorBlink = function() {
1320 if (Terminal
.focus
!== this) return;
1321 this.cursorState
^= 1;
1322 this.refresh(this.y
, this.y
);
1325 Terminal
.prototype.showCursor = function() {
1326 if (!this.cursorState
) {
1327 this.cursorState
= 1;
1328 this.refresh(this.y
, this.y
);
1330 // Temporarily disabled:
1331 // this.refreshBlink();
1335 Terminal
.prototype.startBlink = function() {
1336 if (!this.cursorBlink
) return;
1338 this._blinker = function() {
1339 self
._cursorBlink();
1341 this._blink
= setInterval(this._blinker
, 500);
1344 Terminal
.prototype.refreshBlink = function() {
1345 if (!this.cursorBlink
) return;
1346 clearInterval(this._blink
);
1347 this._blink
= setInterval(this._blinker
, 500);
1350 Terminal
.prototype.scroll = function() {
1353 if (++this.ybase
=== this.scrollback
) {
1354 this.ybase
= this.ybase
/ 2 | 0;
1355 this.lines
= this.lines
.slice(-(this.ybase
+ this.rows
) + 1);
1358 this.ydisp
= this.ybase
;
1361 row
= this.ybase
+ this.rows
- 1;
1363 // subtract the bottom scroll region
1364 row
-= this.rows
- 1 - this.scrollBottom
;
1366 if (row
=== this.lines
.length
) {
1367 // potential optimization:
1368 // pushing is faster than splicing
1369 // when they amount to the same
1371 this.lines
.push(this.blankLine());
1374 this.lines
.splice(row
, 0, this.blankLine());
1377 if (this.scrollTop
!== 0) {
1378 if (this.ybase
!== 0) {
1380 this.ydisp
= this.ybase
;
1382 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
1386 this.updateRange(this.scrollTop
);
1387 this.updateRange(this.scrollBottom
);
1390 Terminal
.prototype.scrollDisp = function(disp
) {
1393 if (this.ydisp
> this.ybase
) {
1394 this.ydisp
= this.ybase
;
1395 } else if (this.ydisp
< 0) {
1399 this.refresh(0, this.rows
- 1);
1402 Terminal
.prototype.write = function(data
) {
1409 this.refreshStart
= this.y
;
1410 this.refreshEnd
= this.y
;
1412 if (this.ybase
!== this.ydisp
) {
1413 this.ydisp
= this.ybase
;
1417 // this.log(JSON.stringify(data.replace(/\x1b/g, '^[')));
1419 for (; i
< l
; i
++) {
1421 switch (this.state
) {
1438 if (this.convertEol
) {
1441 // TODO: Implement eat_newline_glitch.
1442 // if (this.realX >= this.cols) break;
1445 if (this.y
> this.scrollBottom
) {
1465 this.x
= this.nextStop();
1480 this.state
= escaped
;
1486 if (this.charset
&& this.charset
[ch
]) {
1487 ch
= this.charset
[ch
];
1490 if (this.x
>= this.cols
) {
1493 if (this.y
> this.scrollBottom
) {
1499 this.lines
[this.y
+ this.ybase
][this.x
] = [this.curAttr
, ch
];
1501 this.updateRange(this.y
);
1504 j
= this.y
+ this.ybase
;
1505 if (this.cols
< 2 || this.x
>= this.cols
) {
1506 this.lines
[j
][this.x
- 1] = [this.curAttr
, ' '];
1509 this.lines
[j
][this.x
] = [this.curAttr
, ' '];
1518 // ESC [ Control Sequence Introducer ( CSI is 0x9b).
1521 this.currentParam
= 0;
1525 // ESC ] Operating System Command ( OSC is 0x9d).
1528 this.currentParam
= 0;
1532 // ESC P Device Control String ( DCS is 0x90).
1535 this.currentParam
= 0;
1539 // ESC _ Application Program Command ( APC is 0x9f).
1541 this.state
= ignore
;
1544 // ESC ^ Privacy Message ( PM is 0x9e).
1546 this.state
= ignore
;
1549 // ESC c Full Reset (RIS).
1554 // ESC E Next Line ( NEL is 0x85).
1555 // ESC D Index ( IND is 0x84).
1563 // ESC M Reverse Index ( RI is 0x8d).
1565 this.reverseIndex();
1568 // ESC % Select default/utf-8 character set.
1569 // @ = default, G = utf-8
1571 //this.charset = null;
1573 this.setgCharset(0, Terminal
.charsets
.US
);
1574 this.state
= normal
;
1578 // ESC (,),*,+,-,. Designate G0-G2 Character Set.
1579 case '(': // <-- this seems to get all the attention
1605 this.state
= charset
;
1608 // Designate G3 Character Set (VT300).
1609 // A = ISO Latin-1 Supplemental.
1613 this.state
= charset
;
1618 // Single Shift Select of G2 Character Set
1619 // ( SS2 is 0x8e). This affects next character only.
1623 // Single Shift Select of G3 Character Set
1624 // ( SS3 is 0x8f). This affects next character only.
1628 // Invoke the G2 Character Set as GL (LS2).
1633 // Invoke the G3 Character Set as GL (LS3).
1638 // Invoke the G3 Character Set as GR (LS3R).
1643 // Invoke the G2 Character Set as GR (LS2R).
1648 // Invoke the G1 Character Set as GR (LS1R).
1653 // ESC 7 Save Cursor (DECSC).
1656 this.state
= normal
;
1659 // ESC 8 Restore Cursor (DECRC).
1661 this.restoreCursor();
1662 this.state
= normal
;
1665 // ESC # 3 DEC line height/width
1667 this.state
= normal
;
1671 // ESC H Tab Set (HTS is 0x88).
1676 // ESC = Application Keypad (DECPAM).
1678 this.log('Serial port requested application keypad.');
1679 this.applicationKeypad
= true;
1680 this.state
= normal
;
1683 // ESC > Normal Keypad (DECPNM).
1685 this.log('Switching back to normal keypad.');
1686 this.applicationKeypad
= false;
1687 this.state
= normal
;
1691 this.state
= normal
;
1692 this.error('Unknown ESC control: %s.', ch
);
1699 case '0': // DEC Special Character and Line Drawing Set.
1700 cs
= Terminal
.charsets
.SCLD
;
1703 cs
= Terminal
.charsets
.UK
;
1705 case 'B': // United States (USASCII).
1706 cs
= Terminal
.charsets
.US
;
1709 cs
= Terminal
.charsets
.Dutch
;
1711 case 'C': // Finnish
1713 cs
= Terminal
.charsets
.Finnish
;
1716 cs
= Terminal
.charsets
.French
;
1718 case 'Q': // FrenchCanadian
1719 cs
= Terminal
.charsets
.FrenchCanadian
;
1722 cs
= Terminal
.charsets
.German
;
1724 case 'Y': // Italian
1725 cs
= Terminal
.charsets
.Italian
;
1727 case 'E': // NorwegianDanish
1729 cs
= Terminal
.charsets
.NorwegianDanish
;
1731 case 'Z': // Spanish
1732 cs
= Terminal
.charsets
.Spanish
;
1734 case 'H': // Swedish
1736 cs
= Terminal
.charsets
.Swedish
;
1739 cs
= Terminal
.charsets
.Swiss
;
1741 case '/': // ISOLatin (actually /A)
1742 cs
= Terminal
.charsets
.ISOLatin
;
1746 cs
= Terminal
.charsets
.US
;
1749 this.setgCharset(this.gcharset
, cs
);
1750 this.gcharset
= null;
1751 this.state
= normal
;
1757 // Set Text Parameters.
1758 if (ch
=== '\x1b' || ch
=== '\x07') {
1759 if (ch
=== '\x1b') i
++;
1761 this.params
.push(this.currentParam
);
1763 switch (this.params
[0]) {
1767 if (this.params
[1]) {
1768 this.title
= this.params
[1];
1769 this.handleTitle(this.title
);
1777 // change dynamic colors
1789 // change dynamic ui colors
1801 // manipulate selection data
1819 this.currentParam
= 0;
1820 this.state
= normal
;
1822 if (!this.params
.length
) {
1823 if (ch
>= '0' && ch
<= '9') {
1825 this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1826 } else if (ch
=== ';') {
1827 this.params
.push(this.currentParam
);
1828 this.currentParam
= '';
1831 this.currentParam
+= ch
;
1838 if (ch
=== '?' || ch
=== '>' || ch
=== '!') {
1844 if (ch
>= '0' && ch
<= '9') {
1845 this.currentParam
= this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
1849 // '$', '"', ' ', '\''
1850 if (ch
=== '$' || ch
=== '"' || ch
=== ' ' || ch
=== '\'') {
1855 this.params
.push(this.currentParam
);
1856 this.currentParam
= 0;
1859 if (ch
=== ';') break;
1861 this.state
= normal
;
1865 // Cursor Up Ps Times (default = 1) (CUU).
1867 this.cursorUp(this.params
);
1871 // Cursor Down Ps Times (default = 1) (CUD).
1873 this.cursorDown(this.params
);
1877 // Cursor Forward Ps Times (default = 1) (CUF).
1879 this.cursorForward(this.params
);
1883 // Cursor Backward Ps Times (default = 1) (CUB).
1885 this.cursorBackward(this.params
);
1889 // Cursor Position [row;column] (default = [1,1]) (CUP).
1891 this.cursorPos(this.params
);
1894 // CSI Ps J Erase in Display (ED).
1896 this.eraseInDisplay(this.params
);
1899 // CSI Ps K Erase in Line (EL).
1901 this.eraseInLine(this.params
);
1904 // CSI Pm m Character Attributes (SGR).
1907 this.charAttributes(this.params
);
1911 // CSI Ps n Device Status Report (DSR).
1914 this.deviceStatus(this.params
);
1923 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
1925 this.insertChars(this.params
);
1929 // Cursor Next Line Ps Times (default = 1) (CNL).
1931 this.cursorNextLine(this.params
);
1935 // Cursor Preceding Line Ps Times (default = 1) (CNL).
1937 this.cursorPrecedingLine(this.params
);
1941 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
1943 this.cursorCharAbsolute(this.params
);
1947 // Insert Ps Line(s) (default = 1) (IL).
1949 this.insertLines(this.params
);
1953 // Delete Ps Line(s) (default = 1) (DL).
1955 this.deleteLines(this.params
);
1959 // Delete Ps Character(s) (default = 1) (DCH).
1961 this.deleteChars(this.params
);
1965 // Erase Ps Character(s) (default = 1) (ECH).
1967 this.eraseChars(this.params
);
1970 // CSI Pm ` Character Position Absolute
1971 // [column] (default = [row,1]) (HPA).
1973 this.charPosAbsolute(this.params
);
1977 // Horizontal Position Relative
1979 this.HPositionRelative(this.params
);
1983 // Send Device Attributes (Primary DA).
1985 // Send Device Attributes (Secondary DA)
1987 this.sendDeviceAttributes(this.params
);
1991 // Line Position Absolute [row] (default = [1,column]) (VPA).
1993 this.linePosAbsolute(this.params
);
1996 // 145 65 e * VPR - Vertical Position Relative
1998 this.VPositionRelative(this.params
);
2002 // Horizontal and Vertical Position [row;column] (default =
2005 this.HVPosition(this.params
);
2008 // CSI Pm h Set Mode (SM).
2009 // CSI ? Pm h - mouse escape codes, cursor escape codes
2011 this.setMode(this.params
);
2014 // CSI Pm l Reset Mode (RM).
2017 this.resetMode(this.params
);
2021 // Set Scrolling Region [top;bottom] (default = full size of win-
2025 this.setScrollRegion(this.params
);
2029 // Save cursor (ANSI.SYS).
2031 this.saveCursor(this.params
);
2035 // Restore cursor (ANSI.SYS).
2037 this.restoreCursor(this.params
);
2045 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
2047 this.cursorForwardTab(this.params
);
2050 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
2052 this.scrollUp(this.params
);
2055 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
2056 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
2059 // if (this.prefix === '>') {
2060 // this.resetTitleModes(this.params);
2063 // if (this.params.length > 2) {
2064 // this.initMouseTracking(this.params);
2067 if (this.params
.length
< 2 && !this.prefix
) {
2068 this.scrollDown(this.params
);
2073 // Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
2075 this.cursorBackwardTab(this.params
);
2078 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
2080 this.repeatPrecedingCharacter(this.params
);
2083 // CSI Ps g Tab Clear (TBC).
2085 this.tabClear(this.params
);
2088 // CSI Pm i Media Copy (MC).
2091 // this.mediaCopy(this.params);
2094 // CSI Pm m Character Attributes (SGR).
2096 // case 'm': // duplicate
2097 // if (this.prefix === '>') {
2098 // this.setResources(this.params);
2100 // this.charAttributes(this.params);
2104 // CSI Ps n Device Status Report (DSR).
2106 // case 'n': // duplicate
2107 // if (this.prefix === '>') {
2108 // this.disableModifiers(this.params);
2110 // this.deviceStatus(this.params);
2114 // CSI > Ps p Set pointer mode.
2115 // CSI ! p Soft terminal reset (DECSTR).
2117 // Request ANSI mode (DECRQM).
2119 // Request DEC private mode (DECRQM).
2122 switch (this.prefix
) {
2124 // this.setPointerMode(this.params);
2127 this.softReset(this.params
);
2130 // if (this.postfix === '$') {
2131 // this.requestPrivateMode(this.params);
2135 // if (this.postfix === '"') {
2136 // this.setConformanceLevel(this.params);
2137 // } else if (this.postfix === '$') {
2138 // this.requestAnsiMode(this.params);
2144 // CSI Ps q Load LEDs (DECLL).
2148 // if (this.postfix === ' ') {
2149 // this.setCursorStyle(this.params);
2152 // if (this.postfix === '"') {
2153 // this.setCharProtectionAttr(this.params);
2156 // this.loadLEDs(this.params);
2160 // Set Scrolling Region [top;bottom] (default = full size of win-
2163 // CSI Pt; Pl; Pb; Pr; Ps$ r
2164 // case 'r': // duplicate
2165 // if (this.prefix === '?') {
2166 // this.restorePrivateValues(this.params);
2167 // } else if (this.postfix === '$') {
2168 // this.setAttrInRectangle(this.params);
2170 // this.setScrollRegion(this.params);
2174 // CSI s Save cursor (ANSI.SYS).
2176 // case 's': // duplicate
2177 // if (this.prefix === '?') {
2178 // this.savePrivateValues(this.params);
2180 // this.saveCursor(this.params);
2184 // CSI Ps ; Ps ; Ps t
2185 // CSI Pt; Pl; Pb; Pr; Ps$ t
2189 // if (this.postfix === '$') {
2190 // this.reverseAttrInRectangle(this.params);
2191 // } else if (this.postfix === ' ') {
2192 // this.setWarningBellVolume(this.params);
2194 // if (this.prefix === '>') {
2195 // this.setTitleModeFeature(this.params);
2197 // this.manipulateWindow(this.params);
2202 // CSI u Restore cursor (ANSI.SYS).
2204 // case 'u': // duplicate
2205 // if (this.postfix === ' ') {
2206 // this.setMarginBellVolume(this.params);
2208 // this.restoreCursor(this.params);
2212 // CSI Pt; Pl; Pb; Pr; Pp; Pt; Pl; Pp$ v
2214 // if (this.postfix === '$') {
2215 // this.copyRectagle(this.params);
2219 // CSI Pt ; Pl ; Pb ; Pr ' w
2221 // if (this.postfix === '\'') {
2222 // this.enableFilterRectangle(this.params);
2226 // CSI Ps x Request Terminal Parameters (DECREQTPARM).
2227 // CSI Ps x Select Attribute Change Extent (DECSACE).
2228 // CSI Pc; Pt; Pl; Pb; Pr$ x
2230 // if (this.postfix === '$') {
2231 // this.fillRectangle(this.params);
2233 // this.requestParameters(this.params);
2234 // //this.__(this.params);
2239 // CSI Pt; Pl; Pb; Pr$ z
2241 // if (this.postfix === '\'') {
2242 // this.enableLocatorReporting(this.params);
2243 // } else if (this.postfix === '$') {
2244 // this.eraseRectangle(this.params);
2249 // CSI Pt; Pl; Pb; Pr$ {
2251 // if (this.postfix === '\'') {
2252 // this.setLocatorEvents(this.params);
2253 // } else if (this.postfix === '$') {
2254 // this.selectiveEraseRectangle(this.params);
2260 // if (this.postfix === '\'') {
2261 // this.requestLocatorPosition(this.params);
2266 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
2268 // if (this.postfix === ' ') {
2269 // this.insertColumns(this.params);
2274 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
2276 // if (this.postfix === ' ') {
2277 // this.deleteColumns(this.params);
2282 this.error('Unknown CSI code: %s.', ch
);
2291 if (ch
=== '\x1b' || ch
=== '\x07') {
2292 if (ch
=== '\x1b') i
++;
2294 switch (this.prefix
) {
2295 // User-Defined Keys (DECUDK).
2299 // Request Status String (DECRQSS).
2300 // test: echo -e '\eP$q"p\e\\'
2302 var pt
= this.currentParam
2319 + (this.scrollTop
+ 1)
2321 + (this.scrollBottom
+ 1)
2331 this.error('Unknown DCS Pt: %s.', pt
);
2336 this.send('\x1bP' + +valid
+ '$r' + pt
+ '\x1b\\');
2339 // Set Termcap/Terminfo Data (xterm, experimental).
2343 // Request Termcap/Terminfo String (xterm, experimental)
2344 // Regular xterm does not even respond to this sequence.
2345 // This can cause a small glitch in vim.
2346 // test: echo -ne '\eP+q6b64\e\\'
2348 var pt
= this.currentParam
2351 this.send('\x1bP' + +valid
+ '+r' + pt
+ '\x1b\\');
2355 this.error('Unknown DCS prefix: %s.', this.prefix
);
2359 this.currentParam
= 0;
2361 this.state
= normal
;
2362 } else if (!this.currentParam
) {
2363 if (!this.prefix
&& ch
!== '$' && ch
!== '+') {
2364 this.currentParam
= ch
;
2365 } else if (this.prefix
.length
=== 2) {
2366 this.currentParam
= ch
;
2371 this.currentParam
+= ch
;
2377 if (ch
=== '\x1b' || ch
=== '\x07') {
2378 if (ch
=== '\x1b') i
++;
2379 this.state
= normal
;
2385 this.updateRange(this.y
);
2386 this.refresh(this.refreshStart
, this.refreshEnd
);
2389 Terminal
.prototype.writeln = function(data
) {
2390 this.write(data
+ '\r\n');
2394 // https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
2395 Terminal
.prototype.keyDown = function(ev
) {
2399 switch (ev
.keyCode
) {
2426 if (this.applicationCursor
) {
2427 key
= '\x1bOD'; // SS3 as ^[O for 7-bit
2428 //key = '\x8fD'; // SS3 as 0x8f for 8-bit
2435 if (this.applicationCursor
) {
2443 if (this.applicationCursor
) {
2448 this.scrollDisp(-1);
2456 if (this.applicationCursor
) {
2477 if (this.applicationKeypad
) {
2485 if (this.applicationKeypad
) {
2494 this.scrollDisp(-(this.rows
- 1));
2503 this.scrollDisp(this.rows
- 1);
2560 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2562 if (this.screenKeys
) {
2563 if (!this.prefixMode
&& !this.selectMode
&& ev
.keyCode
=== 65) {
2569 if (this.prefixMode
&& ev
.keyCode
=== 86) {
2574 if ((this.prefixMode
|| this.selectMode
) && ev
.keyCode
=== 67) {
2575 if (this.visualMode
) {
2576 setTimeout(function() {
2582 key
= String
.fromCharCode(ev
.keyCode
- 64);
2583 } else if (ev
.keyCode
=== 32) {
2585 key
= String
.fromCharCode(0);
2586 } else if (ev
.keyCode
>= 51 && ev
.keyCode
<= 55) {
2587 // escape, file sep, group sep, record sep, unit sep
2588 key
= String
.fromCharCode(ev
.keyCode
- 51 + 27);
2589 } else if (ev
.keyCode
=== 56) {
2591 key
= String
.fromCharCode(127);
2592 } else if (ev
.keyCode
=== 219) {
2594 key
= String
.fromCharCode(27);
2595 } else if (ev
.keyCode
=== 221) {
2597 key
= String
.fromCharCode(29);
2599 } else if ((!this.isMac
&& ev
.altKey
) || (this.isMac
&& ev
.metaKey
)) {
2600 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2601 key
= '\x1b' + String
.fromCharCode(ev
.keyCode
+ 32);
2602 } else if (ev
.keyCode
=== 192) {
2604 } else if (ev
.keyCode
>= 48 && ev
.keyCode
<= 57) {
2605 key
= '\x1b' + (ev
.keyCode
- 48);
2611 if (!key
) return true;
2613 if (this.prefixMode
) {
2618 if (this.selectMode
) {
2619 this.keySelect(ev
, key
);
2623 this.emit('keydown', ev
);
2624 this.emit('key', key
, ev
);
2632 Terminal
.prototype.setgLevel = function(g
) {
2634 this.charset
= this.charsets
[g
];
2637 Terminal
.prototype.setgCharset = function(g
, charset
) {
2638 this.charsets
[g
] = charset
;
2639 if (this.glevel
=== g
) {
2640 this.charset
= charset
;
2644 Terminal
.prototype.keyPress = function(ev
) {
2651 } else if (ev
.which
== null) {
2653 } else if (ev
.which
!== 0 && ev
.charCode
!== 0) {
2659 if (!key
|| ev
.ctrlKey
|| ev
.altKey
|| ev
.metaKey
) return false;
2661 key
= String
.fromCharCode(key
);
2663 if (this.prefixMode
) {
2665 this.keyPrefix(ev
, key
);
2669 if (this.selectMode
) {
2670 this.keySelect(ev
, key
);
2674 this.emit('keypress', key
, ev
);
2675 this.emit('key', key
, ev
);
2683 Terminal
.prototype.send = function(data
) {
2687 setTimeout(function() {
2688 self
.handler(self
.queue
);
2696 Terminal
.prototype.bell = function() {
2697 if (!this.visualBell
) return;
2699 this.element
.style
.borderColor
= 'white';
2700 setTimeout(function() {
2701 self
.element
.style
.borderColor
= '';
2703 if (this.popOnBell
) this.focus();
2706 Terminal
.prototype.log = function() {
2707 if (!this.debug
) return;
2708 if (!this.context
.console
|| !this.context
.console
.log
) return;
2709 var args
= Array
.prototype.slice
.call(arguments
);
2710 this.context
.console
.log
.apply(this.context
.console
, args
);
2713 Terminal
.prototype.error = function() {
2714 if (!this.debug
) return;
2715 if (!this.context
.console
|| !this.context
.console
.error
) return;
2716 var args
= Array
.prototype.slice
.call(arguments
);
2717 this.context
.console
.error
.apply(this.context
.console
, args
);
2720 Terminal
.prototype.resize = function(x
, y
) {
2733 ch
= [this.defAttr
, ' ']; // does xterm use the default attr?
2734 i
= this.lines
.length
;
2736 while (this.lines
[i
].length
< x
) {
2737 this.lines
[i
].push(ch
);
2741 i
= this.lines
.length
;
2743 while (this.lines
[i
].length
> x
) {
2744 this.lines
[i
].pop();
2756 if (this.lines
.length
< y
+ this.ybase
) {
2757 this.lines
.push(this.blankLine());
2759 if (this.children
.length
< y
) {
2760 line
= this.document
.createElement('div');
2761 el
.appendChild(line
);
2762 this.children
.push(line
);
2767 if (this.lines
.length
> y
+ this.ybase
) {
2770 if (this.children
.length
> y
) {
2771 el
= this.children
.pop();
2773 el
.parentNode
.removeChild(el
);
2779 // make sure the cursor stays on screen
2780 if (this.y
>= y
) this.y
= y
- 1;
2781 if (this.x
>= x
) this.x
= x
- 1;
2784 this.scrollBottom
= y
- 1;
2786 this.refresh(0, this.rows
- 1);
2788 // it's a real nightmare trying
2789 // to resize the original
2790 // screen buffer. just set it
2795 Terminal
.prototype.updateRange = function(y
) {
2796 if (y
< this.refreshStart
) this.refreshStart
= y
;
2797 if (y
> this.refreshEnd
) this.refreshEnd
= y
;
2798 // if (y > this.refreshEnd) {
2799 // this.refreshEnd = y;
2800 // if (y > this.rows - 1) {
2801 // this.refreshEnd = this.rows - 1;
2806 Terminal
.prototype.maxRange = function() {
2807 this.refreshStart
= 0;
2808 this.refreshEnd
= this.rows
- 1;
2811 Terminal
.prototype.setupStops = function(i
) {
2813 if (!this.tabs
[i
]) {
2814 i
= this.prevStop(i
);
2821 for (; i
< this.cols
; i
+= 8) {
2822 this.tabs
[i
] = true;
2826 Terminal
.prototype.prevStop = function(x
) {
2827 if (x
== null) x
= this.x
;
2828 while (!this.tabs
[--x
] && x
> 0);
2829 return x
>= this.cols
2834 Terminal
.prototype.nextStop = function(x
) {
2835 if (x
== null) x
= this.x
;
2836 while (!this.tabs
[++x
] && x
< this.cols
);
2837 return x
>= this.cols
2842 Terminal
.prototype.eraseRight = function(x
, y
) {
2843 var line
= this.lines
[this.ybase
+ y
]
2844 , ch
= [this.eraseAttr(), ' ']; // xterm
2847 for (; x
< this.cols
; x
++) {
2851 this.updateRange(y
);
2854 Terminal
.prototype.eraseLeft = function(x
, y
) {
2855 var line
= this.lines
[this.ybase
+ y
]
2856 , ch
= [this.eraseAttr(), ' ']; // xterm
2859 while (x
--) line
[x
] = ch
;
2861 this.updateRange(y
);
2864 Terminal
.prototype.eraseLine = function(y
) {
2865 this.eraseRight(0, y
);
2868 Terminal
.prototype.blankLine = function(cur
) {
2873 var ch
= [attr
, ' ']
2877 for (; i
< this.cols
; i
++) {
2884 Terminal
.prototype.ch = function(cur
) {
2886 ? [this.eraseAttr(), ' ']
2887 : [this.defAttr
, ' '];
2890 Terminal
.prototype.is = function(term
) {
2891 var name
= this.termName
;
2892 return (name
+ '').indexOf(term
) === 0;
2895 Terminal
.prototype.handler = function(data
) {
2896 this.emit('data', data
);
2899 Terminal
.prototype.handleTitle = function(title
) {
2900 this.emit('title', title
);
2907 // ESC D Index (IND is 0x84).
2908 Terminal
.prototype.index = function() {
2910 if (this.y
> this.scrollBottom
) {
2914 this.state
= normal
;
2917 // ESC M Reverse Index (RI is 0x8d).
2918 Terminal
.prototype.reverseIndex = function() {
2921 if (this.y
< this.scrollTop
) {
2923 // possibly move the code below to term.reverseScroll();
2924 // test: echo -ne '\e[1;1H\e[44m\eM\e[0m'
2925 // blankLine(true) is xterm/linux behavior
2926 this.lines
.splice(this.y
+ this.ybase
, 0, this.blankLine(true));
2927 j
= this.rows
- 1 - this.scrollBottom
;
2928 this.lines
.splice(this.rows
- 1 + this.ybase
- j
+ 1, 1);
2930 this.updateRange(this.scrollTop
);
2931 this.updateRange(this.scrollBottom
);
2933 this.state
= normal
;
2936 // ESC c Full Reset (RIS).
2937 Terminal
.prototype.reset = function() {
2938 this.options
.rows
= this.rows
;
2939 this.options
.cols
= this.cols
;
2940 Terminal
.call(this, this.options
);
2941 this.refresh(0, this.rows
- 1);
2944 // ESC H Tab Set (HTS is 0x88).
2945 Terminal
.prototype.tabSet = function() {
2946 this.tabs
[this.x
] = true;
2947 this.state
= normal
;
2955 // Cursor Up Ps Times (default = 1) (CUU).
2956 Terminal
.prototype.cursorUp = function(params
) {
2957 var param
= params
[0];
2958 if (param
< 1) param
= 1;
2960 if (this.y
< 0) this.y
= 0;
2964 // Cursor Down Ps Times (default = 1) (CUD).
2965 Terminal
.prototype.cursorDown = function(params
) {
2966 var param
= params
[0];
2967 if (param
< 1) param
= 1;
2969 if (this.y
>= this.rows
) {
2970 this.y
= this.rows
- 1;
2975 // Cursor Forward Ps Times (default = 1) (CUF).
2976 Terminal
.prototype.cursorForward = function(params
) {
2977 var param
= params
[0];
2978 if (param
< 1) param
= 1;
2980 if (this.x
>= this.cols
) {
2981 this.x
= this.cols
- 1;
2986 // Cursor Backward Ps Times (default = 1) (CUB).
2987 Terminal
.prototype.cursorBackward = function(params
) {
2988 var param
= params
[0];
2989 if (param
< 1) param
= 1;
2991 if (this.x
< 0) this.x
= 0;
2995 // Cursor Position [row;column] (default = [1,1]) (CUP).
2996 Terminal
.prototype.cursorPos = function(params
) {
2999 row
= params
[0] - 1;
3001 if (params
.length
>= 2) {
3002 col
= params
[1] - 1;
3009 } else if (row
>= this.rows
) {
3010 row
= this.rows
- 1;
3015 } else if (col
>= this.cols
) {
3016 col
= this.cols
- 1;
3023 // CSI Ps J Erase in Display (ED).
3024 // Ps = 0 -> Erase Below (default).
3025 // Ps = 1 -> Erase Above.
3026 // Ps = 2 -> Erase All.
3027 // Ps = 3 -> Erase Saved Lines (xterm).
3029 // Erase in Display (DECSED).
3030 // Ps = 0 -> Selective Erase Below (default).
3031 // Ps = 1 -> Selective Erase Above.
3032 // Ps = 2 -> Selective Erase All.
3033 Terminal
.prototype.eraseInDisplay = function(params
) {
3035 switch (params
[0]) {
3037 this.eraseRight(this.x
, this.y
);
3039 for (; j
< this.rows
; j
++) {
3044 this.eraseLeft(this.x
, this.y
);
3052 while (j
--) this.eraseLine(j
);
3060 // CSI Ps K Erase in Line (EL).
3061 // Ps = 0 -> Erase to Right (default).
3062 // Ps = 1 -> Erase to Left.
3063 // Ps = 2 -> Erase All.
3065 // Erase in Line (DECSEL).
3066 // Ps = 0 -> Selective Erase to Right (default).
3067 // Ps = 1 -> Selective Erase to Left.
3068 // Ps = 2 -> Selective Erase All.
3069 Terminal
.prototype.eraseInLine = function(params
) {
3070 switch (params
[0]) {
3072 this.eraseRight(this.x
, this.y
);
3075 this.eraseLeft(this.x
, this.y
);
3078 this.eraseLine(this.y
);
3083 // CSI Pm m Character Attributes (SGR).
3084 // Ps = 0 -> Normal (default).
3086 // Ps = 4 -> Underlined.
3087 // Ps = 5 -> Blink (appears as Bold).
3088 // Ps = 7 -> Inverse.
3089 // Ps = 8 -> Invisible, i.e., hidden (VT300).
3090 // Ps = 2 2 -> Normal (neither bold nor faint).
3091 // Ps = 2 4 -> Not underlined.
3092 // Ps = 2 5 -> Steady (not blinking).
3093 // Ps = 2 7 -> Positive (not inverse).
3094 // Ps = 2 8 -> Visible, i.e., not hidden (VT300).
3095 // Ps = 3 0 -> Set foreground color to Black.
3096 // Ps = 3 1 -> Set foreground color to Red.
3097 // Ps = 3 2 -> Set foreground color to Green.
3098 // Ps = 3 3 -> Set foreground color to Yellow.
3099 // Ps = 3 4 -> Set foreground color to Blue.
3100 // Ps = 3 5 -> Set foreground color to Magenta.
3101 // Ps = 3 6 -> Set foreground color to Cyan.
3102 // Ps = 3 7 -> Set foreground color to White.
3103 // Ps = 3 9 -> Set foreground color to default (original).
3104 // Ps = 4 0 -> Set background color to Black.
3105 // Ps = 4 1 -> Set background color to Red.
3106 // Ps = 4 2 -> Set background color to Green.
3107 // Ps = 4 3 -> Set background color to Yellow.
3108 // Ps = 4 4 -> Set background color to Blue.
3109 // Ps = 4 5 -> Set background color to Magenta.
3110 // Ps = 4 6 -> Set background color to Cyan.
3111 // Ps = 4 7 -> Set background color to White.
3112 // Ps = 4 9 -> Set background color to default (original).
3114 // If 16-color support is compiled, the following apply. Assume
3115 // that xterm's resources are set so that the ISO color codes are
3116 // the first 8 of a set of 16. Then the aixterm colors are the
3117 // bright versions of the ISO colors:
3118 // Ps = 9 0 -> Set foreground color to Black.
3119 // Ps = 9 1 -> Set foreground color to Red.
3120 // Ps = 9 2 -> Set foreground color to Green.
3121 // Ps = 9 3 -> Set foreground color to Yellow.
3122 // Ps = 9 4 -> Set foreground color to Blue.
3123 // Ps = 9 5 -> Set foreground color to Magenta.
3124 // Ps = 9 6 -> Set foreground color to Cyan.
3125 // Ps = 9 7 -> Set foreground color to White.
3126 // Ps = 1 0 0 -> Set background color to Black.
3127 // Ps = 1 0 1 -> Set background color to Red.
3128 // Ps = 1 0 2 -> Set background color to Green.
3129 // Ps = 1 0 3 -> Set background color to Yellow.
3130 // Ps = 1 0 4 -> Set background color to Blue.
3131 // Ps = 1 0 5 -> Set background color to Magenta.
3132 // Ps = 1 0 6 -> Set background color to Cyan.
3133 // Ps = 1 0 7 -> Set background color to White.
3135 // If xterm is compiled with the 16-color support disabled, it
3136 // supports the following, from rxvt:
3137 // Ps = 1 0 0 -> Set foreground and background color to
3140 // If 88- or 256-color support is compiled, the following apply.
3141 // Ps = 3 8 ; 5 ; Ps -> Set foreground color to the second
3143 // Ps = 4 8 ; 5 ; Ps -> Set background color to the second
3145 Terminal
.prototype.charAttributes = function(params
) {
3146 // Optimize a single SGR0.
3147 if (params
.length
=== 1 && params
[0] === 0) {
3148 this.curAttr
= this.defAttr
;
3152 var l
= params
.length
3154 , flags
= this.curAttr
>> 18
3155 , fg
= (this.curAttr
>> 9) & 0x1ff
3156 , bg
= this.curAttr
& 0x1ff
3159 for (; i
< l
; i
++) {
3161 if (p
>= 30 && p
<= 37) {
3164 } else if (p
>= 40 && p
<= 47) {
3167 } else if (p
>= 90 && p
<= 97) {
3171 } else if (p
>= 100 && p
<= 107) {
3175 } else if (p
=== 0) {
3177 flags
= this.defAttr
>> 18;
3178 fg
= (this.defAttr
>> 9) & 0x1ff;
3179 bg
= this.defAttr
& 0x1ff;
3183 } else if (p
=== 1) {
3186 } else if (p
=== 4) {
3189 } else if (p
=== 5) {
3192 } else if (p
=== 7) {
3193 // inverse and positive
3194 // test with: echo -e '\e[31m\e[42mhello\e[7mworld\e[27mhi\e[m'
3196 } else if (p
=== 8) {
3199 } else if (p
=== 22) {
3202 } else if (p
=== 24) {
3205 } else if (p
=== 25) {
3208 } else if (p
=== 27) {
3211 } else if (p
=== 28) {
3214 } else if (p
=== 39) {
3216 fg
= (this.defAttr
>> 9) & 0x1ff;
3217 } else if (p
=== 49) {
3219 bg
= this.defAttr
& 0x1ff;
3220 } else if (p
=== 38) {
3222 if (params
[i
+ 1] === 2) {
3226 params
[i
+ 1] & 0xff,
3227 params
[i
+ 2] & 0xff);
3228 if (fg
=== -1) fg
= 0x1ff;
3230 } else if (params
[i
+ 1] === 5) {
3232 p
= params
[i
] & 0xff;
3235 } else if (p
=== 48) {
3237 if (params
[i
+ 1] === 2) {
3241 params
[i
+ 1] & 0xff,
3242 params
[i
+ 2] & 0xff);
3243 if (bg
=== -1) bg
= 0x1ff;
3245 } else if (params
[i
+ 1] === 5) {
3247 p
= params
[i
] & 0xff;
3250 } else if (p
=== 100) {
3252 fg
= (this.defAttr
>> 9) & 0x1ff;
3253 bg
= this.defAttr
& 0x1ff;
3255 this.error('Unknown SGR attribute: %d.', p
);
3259 this.curAttr
= (flags
<< 18) | (fg
<< 9) | bg
;
3262 // CSI Ps n Device Status Report (DSR).
3263 // Ps = 5 -> Status Report. Result (``OK'') is
3265 // Ps = 6 -> Report Cursor Position (CPR) [row;column].
3269 // Device Status Report (DSR, DEC-specific).
3270 // Ps = 6 -> Report Cursor Position (CPR) [row;column] as CSI
3271 // ? r ; c R (assumes page is zero).
3272 // Ps = 1 5 -> Report Printer status as CSI ? 1 0 n (ready).
3273 // or CSI ? 1 1 n (not ready).
3274 // Ps = 2 5 -> Report UDK status as CSI ? 2 0 n (unlocked)
3275 // or CSI ? 2 1 n (locked).
3276 // Ps = 2 6 -> Report Keyboard status as
3277 // CSI ? 2 7 ; 1 ; 0 ; 0 n (North American).
3278 // The last two parameters apply to VT400 & up, and denote key-
3279 // board ready and LK01 respectively.
3280 // Ps = 5 3 -> Report Locator status as
3281 // CSI ? 5 3 n Locator available, if compiled-in, or
3282 // CSI ? 5 0 n No Locator, if not.
3283 Terminal
.prototype.deviceStatus = function(params
) {
3285 switch (params
[0]) {
3288 this.send('\x1b[0n');
3299 } else if (this.prefix
=== '?') {
3300 // modern xterm doesnt seem to
3301 // respond to any of these except ?6, 6, and 5
3302 switch (params
[0]) {
3313 // this.send('\x1b[?11n');
3316 // dont support user defined keys
3317 // this.send('\x1b[?21n');
3320 // north american keyboard
3321 // this.send('\x1b[?27;1;0;0n');
3324 // no dec locator/mouse
3325 // this.send('\x1b[?50n');
3336 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
3337 Terminal
.prototype.insertChars = function(params
) {
3338 var param
, row
, j
, ch
;
3341 if (param
< 1) param
= 1;
3343 row
= this.y
+ this.ybase
;
3345 ch
= [this.eraseAttr(), ' ']; // xterm
3347 while (param
-- && j
< this.cols
) {
3348 this.lines
[row
].splice(j
++, 0, ch
);
3349 this.lines
[row
].pop();
3354 // Cursor Next Line Ps Times (default = 1) (CNL).
3355 // same as CSI Ps B ?
3356 Terminal
.prototype.cursorNextLine = function(params
) {
3357 var param
= params
[0];
3358 if (param
< 1) param
= 1;
3360 if (this.y
>= this.rows
) {
3361 this.y
= this.rows
- 1;
3367 // Cursor Preceding Line Ps Times (default = 1) (CNL).
3369 Terminal
.prototype.cursorPrecedingLine = function(params
) {
3370 var param
= params
[0];
3371 if (param
< 1) param
= 1;
3373 if (this.y
< 0) this.y
= 0;
3378 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
3379 Terminal
.prototype.cursorCharAbsolute = function(params
) {
3380 var param
= params
[0];
3381 if (param
< 1) param
= 1;
3386 // Insert Ps Line(s) (default = 1) (IL).
3387 Terminal
.prototype.insertLines = function(params
) {
3391 if (param
< 1) param
= 1;
3392 row
= this.y
+ this.ybase
;
3394 j
= this.rows
- 1 - this.scrollBottom
;
3395 j
= this.rows
- 1 + this.ybase
- j
+ 1;
3398 // test: echo -e '\e[44m\e[1L\e[0m'
3399 // blankLine(true) - xterm/linux behavior
3400 this.lines
.splice(row
, 0, this.blankLine(true));
3401 this.lines
.splice(j
, 1);
3405 this.updateRange(this.y
);
3406 this.updateRange(this.scrollBottom
);
3410 // Delete Ps Line(s) (default = 1) (DL).
3411 Terminal
.prototype.deleteLines = function(params
) {
3415 if (param
< 1) param
= 1;
3416 row
= this.y
+ this.ybase
;
3418 j
= this.rows
- 1 - this.scrollBottom
;
3419 j
= this.rows
- 1 + this.ybase
- j
;
3422 // test: echo -e '\e[44m\e[1M\e[0m'
3423 // blankLine(true) - xterm/linux behavior
3424 this.lines
.splice(j
+ 1, 0, this.blankLine(true));
3425 this.lines
.splice(row
, 1);
3429 this.updateRange(this.y
);
3430 this.updateRange(this.scrollBottom
);
3434 // Delete Ps Character(s) (default = 1) (DCH).
3435 Terminal
.prototype.deleteChars = function(params
) {
3439 if (param
< 1) param
= 1;
3441 row
= this.y
+ this.ybase
;
3442 ch
= [this.eraseAttr(), ' ']; // xterm
3445 this.lines
[row
].splice(this.x
, 1);
3446 this.lines
[row
].push(ch
);
3451 // Erase Ps Character(s) (default = 1) (ECH).
3452 Terminal
.prototype.eraseChars = function(params
) {
3453 var param
, row
, j
, ch
;
3456 if (param
< 1) param
= 1;
3458 row
= this.y
+ this.ybase
;
3460 ch
= [this.eraseAttr(), ' ']; // xterm
3462 while (param
-- && j
< this.cols
) {
3463 this.lines
[row
][j
++] = ch
;
3467 // CSI Pm ` Character Position Absolute
3468 // [column] (default = [row,1]) (HPA).
3469 Terminal
.prototype.charPosAbsolute = function(params
) {
3470 var param
= params
[0];
3471 if (param
< 1) param
= 1;
3473 if (this.x
>= this.cols
) {
3474 this.x
= this.cols
- 1;
3479 // Horizontal Position Relative
3481 Terminal
.prototype.HPositionRelative = function(params
) {
3482 var param
= params
[0];
3483 if (param
< 1) param
= 1;
3485 if (this.x
>= this.cols
) {
3486 this.x
= this.cols
- 1;
3490 // CSI Ps c Send Device Attributes (Primary DA).
3491 // Ps = 0 or omitted -> request attributes from terminal. The
3492 // response depends on the decTerminalID resource setting.
3493 // -> CSI ? 1 ; 2 c (``VT100 with Advanced Video Option'')
3494 // -> CSI ? 1 ; 0 c (``VT101 with No Options'')
3495 // -> CSI ? 6 c (``VT102'')
3496 // -> CSI ? 6 0 ; 1 ; 2 ; 6 ; 8 ; 9 ; 1 5 ; c (``VT220'')
3497 // The VT100-style response parameters do not mean anything by
3498 // themselves. VT220 parameters do, telling the host what fea-
3499 // tures the terminal supports:
3500 // Ps = 1 -> 132-columns.
3501 // Ps = 2 -> Printer.
3502 // Ps = 6 -> Selective erase.
3503 // Ps = 8 -> User-defined keys.
3504 // Ps = 9 -> National replacement character sets.
3505 // Ps = 1 5 -> Technical characters.
3506 // Ps = 2 2 -> ANSI color, e.g., VT525.
3507 // Ps = 2 9 -> ANSI text locator (i.e., DEC Locator mode).
3509 // Send Device Attributes (Secondary DA).
3510 // Ps = 0 or omitted -> request the terminal's identification
3511 // code. The response depends on the decTerminalID resource set-
3512 // ting. It should apply only to VT220 and up, but xterm extends
3514 // -> CSI > Pp ; Pv ; Pc c
3515 // where Pp denotes the terminal type
3516 // Pp = 0 -> ``VT100''.
3517 // Pp = 1 -> ``VT220''.
3518 // and Pv is the firmware version (for xterm, this was originally
3519 // the XFree86 patch number, starting with 95). In a DEC termi-
3520 // nal, Pc indicates the ROM cartridge registration number and is
3522 // More information:
3523 // xterm/charproc.c - line 2012, for more information.
3524 // vim responds with ^[[?0c or ^[[?1c after the terminal's response (?)
3525 Terminal
.prototype.sendDeviceAttributes = function(params
) {
3526 if (params
[0] > 0) return;
3529 if (this.is('xterm')
3530 || this.is('rxvt-unicode')
3531 || this.is('screen')) {
3532 this.send('\x1b[?1;2c');
3533 } else if (this.is('linux')) {
3534 this.send('\x1b[?6c');
3536 } else if (this.prefix
=== '>') {
3538 // seem to spit this
3539 // out around ~370 times (?).
3540 if (this.is('xterm')) {
3541 this.send('\x1b[>0;276;0c');
3542 } else if (this.is('rxvt-unicode')) {
3543 this.send('\x1b[>85;95;0c');
3544 } else if (this.is('linux')) {
3545 // not supported by linux console.
3546 // linux console echoes parameters.
3547 this.send(params
[0] + 'c');
3548 } else if (this.is('screen')) {
3549 this.send('\x1b[>83;40003;0c');
3555 // Line Position Absolute [row] (default = [1,column]) (VPA).
3556 Terminal
.prototype.linePosAbsolute = function(params
) {
3557 var param
= params
[0];
3558 if (param
< 1) param
= 1;
3560 if (this.y
>= this.rows
) {
3561 this.y
= this.rows
- 1;
3565 // 145 65 e * VPR - Vertical Position Relative
3567 Terminal
.prototype.VPositionRelative = function(params
) {
3568 var param
= params
[0];
3569 if (param
< 1) param
= 1;
3571 if (this.y
>= this.rows
) {
3572 this.y
= this.rows
- 1;
3577 // Horizontal and Vertical Position [row;column] (default =
3579 Terminal
.prototype.HVPosition = function(params
) {
3580 if (params
[0] < 1) params
[0] = 1;
3581 if (params
[1] < 1) params
[1] = 1;
3583 this.y
= params
[0] - 1;
3584 if (this.y
>= this.rows
) {
3585 this.y
= this.rows
- 1;
3588 this.x
= params
[1] - 1;
3589 if (this.x
>= this.cols
) {
3590 this.x
= this.cols
- 1;
3594 // CSI Pm h Set Mode (SM).
3595 // Ps = 2 -> Keyboard Action Mode (AM).
3596 // Ps = 4 -> Insert Mode (IRM).
3597 // Ps = 1 2 -> Send/receive (SRM).
3598 // Ps = 2 0 -> Automatic Newline (LNM).
3600 // DEC Private Mode Set (DECSET).
3601 // Ps = 1 -> Application Cursor Keys (DECCKM).
3602 // Ps = 2 -> Designate USASCII for character sets G0-G3
3603 // (DECANM), and set VT100 mode.
3604 // Ps = 3 -> 132 Column Mode (DECCOLM).
3605 // Ps = 4 -> Smooth (Slow) Scroll (DECSCLM).
3606 // Ps = 5 -> Reverse Video (DECSCNM).
3607 // Ps = 6 -> Origin Mode (DECOM).
3608 // Ps = 7 -> Wraparound Mode (DECAWM).
3609 // Ps = 8 -> Auto-repeat Keys (DECARM).
3610 // Ps = 9 -> Send Mouse X & Y on button press. See the sec-
3611 // tion Mouse Tracking.
3612 // Ps = 1 0 -> Show toolbar (rxvt).
3613 // Ps = 1 2 -> Start Blinking Cursor (att610).
3614 // Ps = 1 8 -> Print form feed (DECPFF).
3615 // Ps = 1 9 -> Set print extent to full screen (DECPEX).
3616 // Ps = 2 5 -> Show Cursor (DECTCEM).
3617 // Ps = 3 0 -> Show scrollbar (rxvt).
3618 // Ps = 3 5 -> Enable font-shifting functions (rxvt).
3619 // Ps = 3 8 -> Enter Tektronix Mode (DECTEK).
3620 // Ps = 4 0 -> Allow 80 -> 132 Mode.
3621 // Ps = 4 1 -> more(1) fix (see curses resource).
3622 // Ps = 4 2 -> Enable Nation Replacement Character sets (DECN-
3624 // Ps = 4 4 -> Turn On Margin Bell.
3625 // Ps = 4 5 -> Reverse-wraparound Mode.
3626 // Ps = 4 6 -> Start Logging. This is normally disabled by a
3627 // compile-time option.
3628 // Ps = 4 7 -> Use Alternate Screen Buffer. (This may be dis-
3629 // abled by the titeInhibit resource).
3630 // Ps = 6 6 -> Application keypad (DECNKM).
3631 // Ps = 6 7 -> Backarrow key sends backspace (DECBKM).
3632 // Ps = 1 0 0 0 -> Send Mouse X & Y on button press and
3633 // release. See the section Mouse Tracking.
3634 // Ps = 1 0 0 1 -> Use Hilite Mouse Tracking.
3635 // Ps = 1 0 0 2 -> Use Cell Motion Mouse Tracking.
3636 // Ps = 1 0 0 3 -> Use All Motion Mouse Tracking.
3637 // Ps = 1 0 0 4 -> Send FocusIn/FocusOut events.
3638 // Ps = 1 0 0 5 -> Enable Extended Mouse Mode.
3639 // Ps = 1 0 1 0 -> Scroll to bottom on tty output (rxvt).
3640 // Ps = 1 0 1 1 -> Scroll to bottom on key press (rxvt).
3641 // Ps = 1 0 3 4 -> Interpret "meta" key, sets eighth bit.
3642 // (enables the eightBitInput resource).
3643 // Ps = 1 0 3 5 -> Enable special modifiers for Alt and Num-
3644 // Lock keys. (This enables the numLock resource).
3645 // Ps = 1 0 3 6 -> Send ESC when Meta modifies a key. (This
3646 // enables the metaSendsEscape resource).
3647 // Ps = 1 0 3 7 -> Send DEL from the editing-keypad Delete
3649 // Ps = 1 0 3 9 -> Send ESC when Alt modifies a key. (This
3650 // enables the altSendsEscape resource).
3651 // Ps = 1 0 4 0 -> Keep selection even if not highlighted.
3652 // (This enables the keepSelection resource).
3653 // Ps = 1 0 4 1 -> Use the CLIPBOARD selection. (This enables
3654 // the selectToClipboard resource).
3655 // Ps = 1 0 4 2 -> Enable Urgency window manager hint when
3656 // Control-G is received. (This enables the bellIsUrgent
3658 // Ps = 1 0 4 3 -> Enable raising of the window when Control-G
3659 // is received. (enables the popOnBell resource).
3660 // Ps = 1 0 4 7 -> Use Alternate Screen Buffer. (This may be
3661 // disabled by the titeInhibit resource).
3662 // Ps = 1 0 4 8 -> Save cursor as in DECSC. (This may be dis-
3663 // abled by the titeInhibit resource).
3664 // Ps = 1 0 4 9 -> Save cursor as in DECSC and use Alternate
3665 // Screen Buffer, clearing it first. (This may be disabled by
3666 // the titeInhibit resource). This combines the effects of the 1
3667 // 0 4 7 and 1 0 4 8 modes. Use this with terminfo-based
3668 // applications rather than the 4 7 mode.
3669 // Ps = 1 0 5 0 -> Set terminfo/termcap function-key mode.
3670 // Ps = 1 0 5 1 -> Set Sun function-key mode.
3671 // Ps = 1 0 5 2 -> Set HP function-key mode.
3672 // Ps = 1 0 5 3 -> Set SCO function-key mode.
3673 // Ps = 1 0 6 0 -> Set legacy keyboard emulation (X11R6).
3674 // Ps = 1 0 6 1 -> Set VT220 keyboard emulation.
3675 // Ps = 2 0 0 4 -> Set bracketed paste mode.
3677 // http://vt100.net/docs/vt220-rm/chapter4.html
3678 Terminal
.prototype.setMode = function(params
) {
3679 if (typeof params
=== 'object') {
3680 var l
= params
.length
3683 for (; i
< l
; i
++) {
3684 this.setMode(params
[i
]);
3693 this.insertMode
= true;
3696 //this.convertEol = true;
3699 } else if (this.prefix
=== '?') {
3702 this.applicationCursor
= true;
3705 this.setgCharset(0, Terminal
.charsets
.US
);
3706 this.setgCharset(1, Terminal
.charsets
.US
);
3707 this.setgCharset(2, Terminal
.charsets
.US
);
3708 this.setgCharset(3, Terminal
.charsets
.US
);
3709 // set VT100 mode here
3711 case 3: // 132 col mode
3712 this.savedCols
= this.cols
;
3713 this.resize(132, this.rows
);
3716 this.originMode
= true;
3719 this.wraparoundMode
= true;
3722 // this.cursorBlink = true;
3725 this.log('Serial port requested application keypad.');
3726 this.applicationKeypad
= true;
3728 case 9: // X10 Mouse
3729 // no release, no motion, no wheel, no modifiers.
3730 case 1000: // vt200 mouse
3732 // no modifiers, except control on the wheel.
3733 case 1002: // button event mouse
3734 case 1003: // any event mouse
3735 // any event - sends motion events,
3736 // even if there is no button held down.
3737 this.x10Mouse
= params
=== 9;
3738 this.vt200Mouse
= params
=== 1000;
3739 this.normalMouse
= params
> 1000;
3740 this.mouseEvents
= true;
3741 this.element
.style
.cursor
= 'default';
3742 this.log('Binding to mouse events.');
3744 case 1004: // send focusin/focusout events
3747 this.sendFocus
= true;
3749 case 1005: // utf8 ext mode mouse
3750 this.utfMouse
= true;
3751 // for wide terminals
3752 // simply encodes large values as utf8 characters
3754 case 1006: // sgr ext mode mouse
3755 this.sgrMouse
= true;
3756 // for wide terminals
3757 // does not add 32 to fields
3758 // press: ^[[<b;x;yM
3759 // release: ^[[<b;x;ym
3761 case 1015: // urxvt ext mode mouse
3762 this.urxvtMouse
= true;
3763 // for wide terminals
3764 // numbers for fields
3766 // motion: ^[[b;x;yT
3768 case 25: // show cursor
3769 this.cursorHidden
= false;
3771 case 1049: // alt screen buffer cursor
3772 //this.saveCursor();
3774 case 47: // alt screen buffer
3775 case 1047: // alt screen buffer
3783 scrollTop
: this.scrollTop
,
3784 scrollBottom
: this.scrollBottom
,
3786 // XXX save charset(s) here?
3787 // charset: this.charset,
3788 // glevel: this.glevel,
3789 // charsets: this.charsets
3792 this.normal
= normal
;
3800 // CSI Pm l Reset Mode (RM).
3801 // Ps = 2 -> Keyboard Action Mode (AM).
3802 // Ps = 4 -> Replace Mode (IRM).
3803 // Ps = 1 2 -> Send/receive (SRM).
3804 // Ps = 2 0 -> Normal Linefeed (LNM).
3806 // DEC Private Mode Reset (DECRST).
3807 // Ps = 1 -> Normal Cursor Keys (DECCKM).
3808 // Ps = 2 -> Designate VT52 mode (DECANM).
3809 // Ps = 3 -> 80 Column Mode (DECCOLM).
3810 // Ps = 4 -> Jump (Fast) Scroll (DECSCLM).
3811 // Ps = 5 -> Normal Video (DECSCNM).
3812 // Ps = 6 -> Normal Cursor Mode (DECOM).
3813 // Ps = 7 -> No Wraparound Mode (DECAWM).
3814 // Ps = 8 -> No Auto-repeat Keys (DECARM).
3815 // Ps = 9 -> Don't send Mouse X & Y on button press.
3816 // Ps = 1 0 -> Hide toolbar (rxvt).
3817 // Ps = 1 2 -> Stop Blinking Cursor (att610).
3818 // Ps = 1 8 -> Don't print form feed (DECPFF).
3819 // Ps = 1 9 -> Limit print to scrolling region (DECPEX).
3820 // Ps = 2 5 -> Hide Cursor (DECTCEM).
3821 // Ps = 3 0 -> Don't show scrollbar (rxvt).
3822 // Ps = 3 5 -> Disable font-shifting functions (rxvt).
3823 // Ps = 4 0 -> Disallow 80 -> 132 Mode.
3824 // Ps = 4 1 -> No more(1) fix (see curses resource).
3825 // Ps = 4 2 -> Disable Nation Replacement Character sets (DEC-
3827 // Ps = 4 4 -> Turn Off Margin Bell.
3828 // Ps = 4 5 -> No Reverse-wraparound Mode.
3829 // Ps = 4 6 -> Stop Logging. (This is normally disabled by a
3830 // compile-time option).
3831 // Ps = 4 7 -> Use Normal Screen Buffer.
3832 // Ps = 6 6 -> Numeric keypad (DECNKM).
3833 // Ps = 6 7 -> Backarrow key sends delete (DECBKM).
3834 // Ps = 1 0 0 0 -> Don't send Mouse X & Y on button press and
3835 // release. See the section Mouse Tracking.
3836 // Ps = 1 0 0 1 -> Don't use Hilite Mouse Tracking.
3837 // Ps = 1 0 0 2 -> Don't use Cell Motion Mouse Tracking.
3838 // Ps = 1 0 0 3 -> Don't use All Motion Mouse Tracking.
3839 // Ps = 1 0 0 4 -> Don't send FocusIn/FocusOut events.
3840 // Ps = 1 0 0 5 -> Disable Extended Mouse Mode.
3841 // Ps = 1 0 1 0 -> Don't scroll to bottom on tty output
3843 // Ps = 1 0 1 1 -> Don't scroll to bottom on key press (rxvt).
3844 // Ps = 1 0 3 4 -> Don't interpret "meta" key. (This disables
3845 // the eightBitInput resource).
3846 // Ps = 1 0 3 5 -> Disable special modifiers for Alt and Num-
3847 // Lock keys. (This disables the numLock resource).
3848 // Ps = 1 0 3 6 -> Don't send ESC when Meta modifies a key.
3849 // (This disables the metaSendsEscape resource).
3850 // Ps = 1 0 3 7 -> Send VT220 Remove from the editing-keypad
3852 // Ps = 1 0 3 9 -> Don't send ESC when Alt modifies a key.
3853 // (This disables the altSendsEscape resource).
3854 // Ps = 1 0 4 0 -> Do not keep selection when not highlighted.
3855 // (This disables the keepSelection resource).
3856 // Ps = 1 0 4 1 -> Use the PRIMARY selection. (This disables
3857 // the selectToClipboard resource).
3858 // Ps = 1 0 4 2 -> Disable Urgency window manager hint when
3859 // Control-G is received. (This disables the bellIsUrgent
3861 // Ps = 1 0 4 3 -> Disable raising of the window when Control-
3862 // G is received. (This disables the popOnBell resource).
3863 // Ps = 1 0 4 7 -> Use Normal Screen Buffer, clearing screen
3864 // first if in the Alternate Screen. (This may be disabled by
3865 // the titeInhibit resource).
3866 // Ps = 1 0 4 8 -> Restore cursor as in DECRC. (This may be
3867 // disabled by the titeInhibit resource).
3868 // Ps = 1 0 4 9 -> Use Normal Screen Buffer and restore cursor
3869 // as in DECRC. (This may be disabled by the titeInhibit
3870 // resource). This combines the effects of the 1 0 4 7 and 1 0
3871 // 4 8 modes. Use this with terminfo-based applications rather
3872 // than the 4 7 mode.
3873 // Ps = 1 0 5 0 -> Reset terminfo/termcap function-key mode.
3874 // Ps = 1 0 5 1 -> Reset Sun function-key mode.
3875 // Ps = 1 0 5 2 -> Reset HP function-key mode.
3876 // Ps = 1 0 5 3 -> Reset SCO function-key mode.
3877 // Ps = 1 0 6 0 -> Reset legacy keyboard emulation (X11R6).
3878 // Ps = 1 0 6 1 -> Reset keyboard emulation to Sun/PC style.
3879 // Ps = 2 0 0 4 -> Reset bracketed paste mode.
3880 Terminal
.prototype.resetMode = function(params
) {
3881 if (typeof params
=== 'object') {
3882 var l
= params
.length
3885 for (; i
< l
; i
++) {
3886 this.resetMode(params
[i
]);
3895 this.insertMode
= false;
3898 //this.convertEol = false;
3901 } else if (this.prefix
=== '?') {
3904 this.applicationCursor
= false;
3907 if (this.cols
=== 132 && this.savedCols
) {
3908 this.resize(this.savedCols
, this.rows
);
3910 delete this.savedCols
;
3913 this.originMode
= false;
3916 this.wraparoundMode
= false;
3919 // this.cursorBlink = false;
3922 this.log('Switching back to normal keypad.');
3923 this.applicationKeypad
= false;
3925 case 9: // X10 Mouse
3926 case 1000: // vt200 mouse
3927 case 1002: // button event mouse
3928 case 1003: // any event mouse
3929 this.x10Mouse
= false;
3930 this.vt200Mouse
= false;
3931 this.normalMouse
= false;
3932 this.mouseEvents
= false;
3933 this.element
.style
.cursor
= '';
3935 case 1004: // send focusin/focusout events
3936 this.sendFocus
= false;
3938 case 1005: // utf8 ext mode mouse
3939 this.utfMouse
= false;
3941 case 1006: // sgr ext mode mouse
3942 this.sgrMouse
= false;
3944 case 1015: // urxvt ext mode mouse
3945 this.urxvtMouse
= false;
3947 case 25: // hide cursor
3948 this.cursorHidden
= true;
3950 case 1049: // alt screen buffer cursor
3952 case 47: // normal screen buffer
3953 case 1047: // normal screen buffer - clearing it first
3955 this.lines
= this.normal
.lines
;
3956 this.ybase
= this.normal
.ybase
;
3957 this.ydisp
= this.normal
.ydisp
;
3958 this.x
= this.normal
.x
;
3959 this.y
= this.normal
.y
;
3960 this.scrollTop
= this.normal
.scrollTop
;
3961 this.scrollBottom
= this.normal
.scrollBottom
;
3962 this.tabs
= this.normal
.tabs
;
3964 // if (params === 1049) {
3965 // this.x = this.savedX;
3966 // this.y = this.savedY;
3968 this.refresh(0, this.rows
- 1);
3977 // Set Scrolling Region [top;bottom] (default = full size of win-
3980 Terminal
.prototype.setScrollRegion = function(params
) {
3981 if (this.prefix
) return;
3982 this.scrollTop
= (params
[0] || 1) - 1;
3983 this.scrollBottom
= (params
[1] || this.rows
) - 1;
3989 // Save cursor (ANSI.SYS).
3990 Terminal
.prototype.saveCursor = function(params
) {
3991 this.savedX
= this.x
;
3992 this.savedY
= this.y
;
3996 // Restore cursor (ANSI.SYS).
3997 Terminal
.prototype.restoreCursor = function(params
) {
3998 this.x
= this.savedX
|| 0;
3999 this.y
= this.savedY
|| 0;
4007 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
4008 Terminal
.prototype.cursorForwardTab = function(params
) {
4009 var param
= params
[0] || 1;
4011 this.x
= this.nextStop();
4015 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
4016 Terminal
.prototype.scrollUp = function(params
) {
4017 var param
= params
[0] || 1;
4019 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
4020 this.lines
.splice(this.ybase
+ this.scrollBottom
, 0, this.blankLine());
4023 this.updateRange(this.scrollTop
);
4024 this.updateRange(this.scrollBottom
);
4027 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
4028 Terminal
.prototype.scrollDown = function(params
) {
4029 var param
= params
[0] || 1;
4031 this.lines
.splice(this.ybase
+ this.scrollBottom
, 1);
4032 this.lines
.splice(this.ybase
+ this.scrollTop
, 0, this.blankLine());
4035 this.updateRange(this.scrollTop
);
4036 this.updateRange(this.scrollBottom
);
4039 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
4040 // Initiate highlight mouse tracking. Parameters are
4041 // [func;startx;starty;firstrow;lastrow]. See the section Mouse
4043 Terminal
.prototype.initMouseTracking = function(params
) {
4044 // Relevant: DECSET 1001
4048 // Reset one or more features of the title modes to the default
4049 // value. Normally, "reset" disables the feature. It is possi-
4050 // ble to disable the ability to reset features by compiling a
4051 // different default for the title modes into xterm.
4052 // Ps = 0 -> Do not set window/icon labels using hexadecimal.
4053 // Ps = 1 -> Do not query window/icon labels using hexadeci-
4055 // Ps = 2 -> Do not set window/icon labels using UTF-8.
4056 // Ps = 3 -> Do not query window/icon labels using UTF-8.
4057 // (See discussion of "Title Modes").
4058 Terminal
.prototype.resetTitleModes = function(params
) {
4062 // CSI Ps Z Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
4063 Terminal
.prototype.cursorBackwardTab = function(params
) {
4064 var param
= params
[0] || 1;
4066 this.x
= this.prevStop();
4070 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
4071 Terminal
.prototype.repeatPrecedingCharacter = function(params
) {
4072 var param
= params
[0] || 1
4073 , line
= this.lines
[this.ybase
+ this.y
]
4074 , ch
= line
[this.x
- 1] || [this.defAttr
, ' '];
4076 while (param
--) line
[this.x
++] = ch
;
4079 // CSI Ps g Tab Clear (TBC).
4080 // Ps = 0 -> Clear Current Column (default).
4081 // Ps = 3 -> Clear All.
4083 // Ps = 2 -> Clear Stops on Line.
4084 // http://vt100.net/annarbor/aaa-ug/section6.html
4085 Terminal
.prototype.tabClear = function(params
) {
4086 var param
= params
[0];
4088 delete this.tabs
[this.x
];
4089 } else if (param
=== 3) {
4094 // CSI Pm i Media Copy (MC).
4095 // Ps = 0 -> Print screen (default).
4096 // Ps = 4 -> Turn off printer controller mode.
4097 // Ps = 5 -> Turn on printer controller mode.
4099 // Media Copy (MC, DEC-specific).
4100 // Ps = 1 -> Print line containing cursor.
4101 // Ps = 4 -> Turn off autoprint mode.
4102 // Ps = 5 -> Turn on autoprint mode.
4103 // Ps = 1 0 -> Print composed display, ignores DECPEX.
4104 // Ps = 1 1 -> Print all pages.
4105 Terminal
.prototype.mediaCopy = function(params
) {
4110 // Set or reset resource-values used by xterm to decide whether
4111 // to construct escape sequences holding information about the
4112 // modifiers pressed with a given key. The first parameter iden-
4113 // tifies the resource to set/reset. The second parameter is the
4114 // value to assign to the resource. If the second parameter is
4115 // omitted, the resource is reset to its initial value.
4116 // Ps = 1 -> modifyCursorKeys.
4117 // Ps = 2 -> modifyFunctionKeys.
4118 // Ps = 4 -> modifyOtherKeys.
4119 // If no parameters are given, all resources are reset to their
4121 Terminal
.prototype.setResources = function(params
) {
4126 // Disable modifiers which may be enabled via the CSI > Ps; Ps m
4127 // sequence. This corresponds to a resource value of "-1", which
4128 // cannot be set with the other sequence. The parameter identi-
4129 // fies the resource to be disabled:
4130 // Ps = 1 -> modifyCursorKeys.
4131 // Ps = 2 -> modifyFunctionKeys.
4132 // Ps = 4 -> modifyOtherKeys.
4133 // If the parameter is omitted, modifyFunctionKeys is disabled.
4134 // When modifyFunctionKeys is disabled, xterm uses the modifier
4135 // keys to make an extended sequence of functions rather than
4136 // adding a parameter to each function key to denote the modi-
4138 Terminal
.prototype.disableModifiers = function(params
) {
4143 // Set resource value pointerMode. This is used by xterm to
4144 // decide whether to hide the pointer cursor as the user types.
4145 // Valid values for the parameter:
4146 // Ps = 0 -> never hide the pointer.
4147 // Ps = 1 -> hide if the mouse tracking mode is not enabled.
4148 // Ps = 2 -> always hide the pointer. If no parameter is
4149 // given, xterm uses the default, which is 1 .
4150 Terminal
.prototype.setPointerMode = function(params
) {
4154 // CSI ! p Soft terminal reset (DECSTR).
4155 // http://vt100.net/docs/vt220-rm/table4-10.html
4156 Terminal
.prototype.softReset = function(params
) {
4157 this.cursorHidden
= false;
4158 this.insertMode
= false;
4159 this.originMode
= false;
4160 this.wraparoundMode
= false; // autowrap
4161 this.applicationKeypad
= false; // ?
4162 this.applicationCursor
= false;
4164 this.scrollBottom
= this.rows
- 1;
4165 this.curAttr
= this.defAttr
;
4166 this.x
= this.y
= 0; // ?
4167 this.charset
= null;
4168 this.glevel
= 0; // ??
4169 this.charsets
= [null]; // ??
4173 // Request ANSI mode (DECRQM). For VT300 and up, reply is
4175 // where Ps is the mode number as in RM, and Pm is the mode
4177 // 0 - not recognized
4180 // 3 - permanently set
4181 // 4 - permanently reset
4182 Terminal
.prototype.requestAnsiMode = function(params
) {
4187 // Request DEC private mode (DECRQM). For VT300 and up, reply is
4189 // where Ps is the mode number as in DECSET, Pm is the mode value
4190 // as in the ANSI DECRQM.
4191 Terminal
.prototype.requestPrivateMode = function(params
) {
4196 // Set conformance level (DECSCL). Valid values for the first
4198 // Ps = 6 1 -> VT100.
4199 // Ps = 6 2 -> VT200.
4200 // Ps = 6 3 -> VT300.
4201 // Valid values for the second parameter:
4202 // Ps = 0 -> 8-bit controls.
4203 // Ps = 1 -> 7-bit controls (always set for VT100).
4204 // Ps = 2 -> 8-bit controls.
4205 Terminal
.prototype.setConformanceLevel = function(params
) {
4209 // CSI Ps q Load LEDs (DECLL).
4210 // Ps = 0 -> Clear all LEDS (default).
4211 // Ps = 1 -> Light Num Lock.
4212 // Ps = 2 -> Light Caps Lock.
4213 // Ps = 3 -> Light Scroll Lock.
4214 // Ps = 2 1 -> Extinguish Num Lock.
4215 // Ps = 2 2 -> Extinguish Caps Lock.
4216 // Ps = 2 3 -> Extinguish Scroll Lock.
4217 Terminal
.prototype.loadLEDs = function(params
) {
4222 // Set cursor style (DECSCUSR, VT520).
4223 // Ps = 0 -> blinking block.
4224 // Ps = 1 -> blinking block (default).
4225 // Ps = 2 -> steady block.
4226 // Ps = 3 -> blinking underline.
4227 // Ps = 4 -> steady underline.
4228 Terminal
.prototype.setCursorStyle = function(params
) {
4233 // Select character protection attribute (DECSCA). Valid values
4234 // for the parameter:
4235 // Ps = 0 -> DECSED and DECSEL can erase (default).
4236 // Ps = 1 -> DECSED and DECSEL cannot erase.
4237 // Ps = 2 -> DECSED and DECSEL can erase.
4238 Terminal
.prototype.setCharProtectionAttr = function(params
) {
4243 // Restore DEC Private Mode Values. The value of Ps previously
4244 // saved is restored. Ps values are the same as for DECSET.
4245 Terminal
.prototype.restorePrivateValues = function(params
) {
4249 // CSI Pt; Pl; Pb; Pr; Ps$ r
4250 // Change Attributes in Rectangular Area (DECCARA), VT400 and up.
4251 // Pt; Pl; Pb; Pr denotes the rectangle.
4252 // Ps denotes the SGR attributes to change: 0, 1, 4, 5, 7.
4253 // NOTE: xterm doesn't enable this code by default.
4254 Terminal
.prototype.setAttrInRectangle = function(params
) {
4264 for (; t
< b
+ 1; t
++) {
4265 line
= this.lines
[this.ybase
+ t
];
4266 for (i
= l
; i
< r
; i
++) {
4267 line
[i
] = [attr
, line
[i
][1]];
4272 this.updateRange(params
[0]);
4273 this.updateRange(params
[2]);
4277 // Save DEC Private Mode Values. Ps values are the same as for
4279 Terminal
.prototype.savePrivateValues = function(params
) {
4283 // CSI Ps ; Ps ; Ps t
4284 // Window manipulation (from dtterm, as well as extensions).
4285 // These controls may be disabled using the allowWindowOps
4286 // resource. Valid values for the first (and any additional
4288 // Ps = 1 -> De-iconify window.
4289 // Ps = 2 -> Iconify window.
4290 // Ps = 3 ; x ; y -> Move window to [x, y].
4291 // Ps = 4 ; height ; width -> Resize the xterm window to
4292 // height and width in pixels.
4293 // Ps = 5 -> Raise the xterm window to the front of the stack-
4295 // Ps = 6 -> Lower the xterm window to the bottom of the
4297 // Ps = 7 -> Refresh the xterm window.
4298 // Ps = 8 ; height ; width -> Resize the text area to
4299 // [height;width] in characters.
4300 // Ps = 9 ; 0 -> Restore maximized window.
4301 // Ps = 9 ; 1 -> Maximize window (i.e., resize to screen
4303 // Ps = 1 0 ; 0 -> Undo full-screen mode.
4304 // Ps = 1 0 ; 1 -> Change to full-screen.
4305 // Ps = 1 1 -> Report xterm window state. If the xterm window
4306 // is open (non-iconified), it returns CSI 1 t . If the xterm
4307 // window is iconified, it returns CSI 2 t .
4308 // Ps = 1 3 -> Report xterm window position. Result is CSI 3
4310 // Ps = 1 4 -> Report xterm window in pixels. Result is CSI
4311 // 4 ; height ; width t
4312 // Ps = 1 8 -> Report the size of the text area in characters.
4313 // Result is CSI 8 ; height ; width t
4314 // Ps = 1 9 -> Report the size of the screen in characters.
4315 // Result is CSI 9 ; height ; width t
4316 // Ps = 2 0 -> Report xterm window's icon label. Result is
4318 // Ps = 2 1 -> Report xterm window's title. Result is OSC l
4320 // Ps = 2 2 ; 0 -> Save xterm icon and window title on
4322 // Ps = 2 2 ; 1 -> Save xterm icon title on stack.
4323 // Ps = 2 2 ; 2 -> Save xterm window title on stack.
4324 // Ps = 2 3 ; 0 -> Restore xterm icon and window title from
4326 // Ps = 2 3 ; 1 -> Restore xterm icon title from stack.
4327 // Ps = 2 3 ; 2 -> Restore xterm window title from stack.
4328 // Ps >= 2 4 -> Resize to Ps lines (DECSLPP).
4329 Terminal
.prototype.manipulateWindow = function(params
) {
4333 // CSI Pt; Pl; Pb; Pr; Ps$ t
4334 // Reverse Attributes in Rectangular Area (DECRARA), VT400 and
4336 // Pt; Pl; Pb; Pr denotes the rectangle.
4337 // Ps denotes the attributes to reverse, i.e., 1, 4, 5, 7.
4338 // NOTE: xterm doesn't enable this code by default.
4339 Terminal
.prototype.reverseAttrInRectangle = function(params
) {
4344 // Set one or more features of the title modes. Each parameter
4345 // enables a single feature.
4346 // Ps = 0 -> Set window/icon labels using hexadecimal.
4347 // Ps = 1 -> Query window/icon labels using hexadecimal.
4348 // Ps = 2 -> Set window/icon labels using UTF-8.
4349 // Ps = 3 -> Query window/icon labels using UTF-8. (See dis-
4350 // cussion of "Title Modes")
4351 Terminal
.prototype.setTitleModeFeature = function(params
) {
4356 // Set warning-bell volume (DECSWBV, VT520).
4357 // Ps = 0 or 1 -> off.
4358 // Ps = 2 , 3 or 4 -> low.
4359 // Ps = 5 , 6 , 7 , or 8 -> high.
4360 Terminal
.prototype.setWarningBellVolume = function(params
) {
4365 // Set margin-bell volume (DECSMBV, VT520).
4367 // Ps = 2 , 3 or 4 -> low.
4368 // Ps = 0 , 5 , 6 , 7 , or 8 -> high.
4369 Terminal
.prototype.setMarginBellVolume = function(params
) {
4373 // CSI Pt; Pl; Pb; Pr; Pp; Pt; Pl; Pp$ v
4374 // Copy Rectangular Area (DECCRA, VT400 and up).
4375 // Pt; Pl; Pb; Pr denotes the rectangle.
4376 // Pp denotes the source page.
4377 // Pt; Pl denotes the target location.
4378 // Pp denotes the target page.
4379 // NOTE: xterm doesn't enable this code by default.
4380 Terminal
.prototype.copyRectangle = function(params
) {
4384 // CSI Pt ; Pl ; Pb ; Pr ' w
4385 // Enable Filter Rectangle (DECEFR), VT420 and up.
4386 // Parameters are [top;left;bottom;right].
4387 // Defines the coordinates of a filter rectangle and activates
4388 // it. Anytime the locator is detected outside of the filter
4389 // rectangle, an outside rectangle event is generated and the
4390 // rectangle is disabled. Filter rectangles are always treated
4391 // as "one-shot" events. Any parameters that are omitted default
4392 // to the current locator position. If all parameters are omit-
4393 // ted, any locator motion will be reported. DECELR always can-
4394 // cels any prevous rectangle definition.
4395 Terminal
.prototype.enableFilterRectangle = function(params
) {
4399 // CSI Ps x Request Terminal Parameters (DECREQTPARM).
4400 // if Ps is a "0" (default) or "1", and xterm is emulating VT100,
4401 // the control sequence elicits a response of the same form whose
4402 // parameters describe the terminal:
4403 // Ps -> the given Ps incremented by 2.
4404 // Pn = 1 <- no parity.
4405 // Pn = 1 <- eight bits.
4406 // Pn = 1 <- 2 8 transmit 38.4k baud.
4407 // Pn = 1 <- 2 8 receive 38.4k baud.
4408 // Pn = 1 <- clock multiplier.
4409 // Pn = 0 <- STP flags.
4410 Terminal
.prototype.requestParameters = function(params
) {
4414 // CSI Ps x Select Attribute Change Extent (DECSACE).
4415 // Ps = 0 -> from start to end position, wrapped.
4416 // Ps = 1 -> from start to end position, wrapped.
4417 // Ps = 2 -> rectangle (exact).
4418 Terminal
.prototype.selectChangeExtent = function(params
) {
4422 // CSI Pc; Pt; Pl; Pb; Pr$ x
4423 // Fill Rectangular Area (DECFRA), VT420 and up.
4424 // Pc is the character to use.
4425 // Pt; Pl; Pb; Pr denotes the rectangle.
4426 // NOTE: xterm doesn't enable this code by default.
4427 Terminal
.prototype.fillRectangle = function(params
) {
4437 for (; t
< b
+ 1; t
++) {
4438 line
= this.lines
[this.ybase
+ t
];
4439 for (i
= l
; i
< r
; i
++) {
4440 line
[i
] = [line
[i
][0], String
.fromCharCode(ch
)];
4445 this.updateRange(params
[1]);
4446 this.updateRange(params
[3]);
4450 // Enable Locator Reporting (DECELR).
4451 // Valid values for the first parameter:
4452 // Ps = 0 -> Locator disabled (default).
4453 // Ps = 1 -> Locator enabled.
4454 // Ps = 2 -> Locator enabled for one report, then disabled.
4455 // The second parameter specifies the coordinate unit for locator
4457 // Valid values for the second parameter:
4458 // Pu = 0 <- or omitted -> default to character cells.
4459 // Pu = 1 <- device physical pixels.
4460 // Pu = 2 <- character cells.
4461 Terminal
.prototype.enableLocatorReporting = function(params
) {
4462 var val
= params
[0] > 0;
4463 //this.mouseEvents = val;
4464 //this.decLocator = val;
4467 // CSI Pt; Pl; Pb; Pr$ z
4468 // Erase Rectangular Area (DECERA), VT400 and up.
4469 // Pt; Pl; Pb; Pr denotes the rectangle.
4470 // NOTE: xterm doesn't enable this code by default.
4471 Terminal
.prototype.eraseRectangle = function(params
) {
4481 ch
= [this.eraseAttr(), ' ']; // xterm?
4483 for (; t
< b
+ 1; t
++) {
4484 line
= this.lines
[this.ybase
+ t
];
4485 for (i
= l
; i
< r
; i
++) {
4491 this.updateRange(params
[0]);
4492 this.updateRange(params
[2]);
4496 // Select Locator Events (DECSLE).
4497 // Valid values for the first (and any additional parameters)
4499 // Ps = 0 -> only respond to explicit host requests (DECRQLP).
4500 // (This is default). It also cancels any filter
4502 // Ps = 1 -> report button down transitions.
4503 // Ps = 2 -> do not report button down transitions.
4504 // Ps = 3 -> report button up transitions.
4505 // Ps = 4 -> do not report button up transitions.
4506 Terminal
.prototype.setLocatorEvents = function(params
) {
4510 // CSI Pt; Pl; Pb; Pr$ {
4511 // Selective Erase Rectangular Area (DECSERA), VT400 and up.
4512 // Pt; Pl; Pb; Pr denotes the rectangle.
4513 Terminal
.prototype.selectiveEraseRectangle = function(params
) {
4518 // Request Locator Position (DECRQLP).
4519 // Valid values for the parameter are:
4520 // Ps = 0 , 1 or omitted -> transmit a single DECLRP locator
4523 // If Locator Reporting has been enabled by a DECELR, xterm will
4524 // respond with a DECLRP Locator Report. This report is also
4525 // generated on button up and down events if they have been
4526 // enabled with a DECSLE, or when the locator is detected outside
4527 // of a filter rectangle, if filter rectangles have been enabled
4530 // -> CSI Pe ; Pb ; Pr ; Pc ; Pp & w
4532 // Parameters are [event;button;row;column;page].
4533 // Valid values for the event:
4534 // Pe = 0 -> locator unavailable - no other parameters sent.
4535 // Pe = 1 -> request - xterm received a DECRQLP.
4536 // Pe = 2 -> left button down.
4537 // Pe = 3 -> left button up.
4538 // Pe = 4 -> middle button down.
4539 // Pe = 5 -> middle button up.
4540 // Pe = 6 -> right button down.
4541 // Pe = 7 -> right button up.
4542 // Pe = 8 -> M4 button down.
4543 // Pe = 9 -> M4 button up.
4544 // Pe = 1 0 -> locator outside filter rectangle.
4545 // ``button'' parameter is a bitmask indicating which buttons are
4547 // Pb = 0 <- no buttons down.
4548 // Pb & 1 <- right button down.
4549 // Pb & 2 <- middle button down.
4550 // Pb & 4 <- left button down.
4551 // Pb & 8 <- M4 button down.
4552 // ``row'' and ``column'' parameters are the coordinates of the
4553 // locator position in the xterm window, encoded as ASCII deci-
4555 // The ``page'' parameter is not used by xterm, and will be omit-
4557 Terminal
.prototype.requestLocatorPosition = function(params
) {
4562 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
4563 // NOTE: xterm doesn't enable this code by default.
4564 Terminal
.prototype.insertColumns = function() {
4565 var param
= params
[0]
4566 , l
= this.ybase
+ this.rows
4567 , ch
= [this.eraseAttr(), ' '] // xterm?
4571 for (i
= this.ybase
; i
< l
; i
++) {
4572 this.lines
[i
].splice(this.x
+ 1, 0, ch
);
4573 this.lines
[i
].pop();
4581 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
4582 // NOTE: xterm doesn't enable this code by default.
4583 Terminal
.prototype.deleteColumns = function() {
4584 var param
= params
[0]
4585 , l
= this.ybase
+ this.rows
4586 , ch
= [this.eraseAttr(), ' '] // xterm?
4590 for (i
= this.ybase
; i
< l
; i
++) {
4591 this.lines
[i
].splice(this.x
, 1);
4592 this.lines
[i
].push(ch
);
4600 * Prefix/Select/Visual/Search Modes
4603 Terminal
.prototype.enterPrefix = function() {
4604 this.prefixMode
= true;
4607 Terminal
.prototype.leavePrefix = function() {
4608 this.prefixMode
= false;
4611 Terminal
.prototype.enterSelect = function() {
4617 cursorHidden
: this.cursorHidden
,
4618 lines
: this.copyBuffer(this.lines
),
4621 this.write = function() {};
4622 this.selectMode
= true;
4623 this.visualMode
= false;
4624 this.cursorHidden
= false;
4625 this.refresh(this.y
, this.y
);
4628 Terminal
.prototype.leaveSelect = function() {
4629 this.x
= this._real
.x
;
4630 this.y
= this._real
.y
;
4631 this.ydisp
= this._real
.ydisp
;
4632 this.ybase
= this._real
.ybase
;
4633 this.cursorHidden
= this._real
.cursorHidden
;
4634 this.lines
= this._real
.lines
;
4635 this.write
= this._real
.write
;
4637 this.selectMode
= false;
4638 this.visualMode
= false;
4639 this.refresh(0, this.rows
- 1);
4642 Terminal
.prototype.enterVisual = function() {
4643 this._real
.preVisual
= this.copyBuffer(this.lines
);
4644 this.selectText(this.x
, this.x
, this.ydisp
+ this.y
, this.ydisp
+ this.y
);
4645 this.visualMode
= true;
4648 Terminal
.prototype.leaveVisual = function() {
4649 this.lines
= this._real
.preVisual
;
4650 delete this._real
.preVisual
;
4651 delete this._selected
;
4652 this.visualMode
= false;
4653 this.refresh(0, this.rows
- 1);
4656 Terminal
.prototype.enterSearch = function(down
) {
4658 this.searchMode
= true;
4659 this.searchDown
= down
;
4660 this._real
.preSearch
= this.copyBuffer(this.lines
);
4661 this._real
.preSearchX
= this.x
;
4662 this._real
.preSearchY
= this.y
;
4664 var bottom
= this.ydisp
+ this.rows
- 1;
4665 for (var i
= 0; i
< this.entryPrefix
.length
; i
++) {
4666 //this.lines[bottom][i][0] = (this.defAttr & ~0x1ff) | 4;
4667 //this.lines[bottom][i][1] = this.entryPrefix[i];
4668 this.lines
[bottom
][i
] = [
4669 (this.defAttr
& ~0x1ff) | 4,
4674 this.y
= this.rows
- 1;
4675 this.x
= this.entryPrefix
.length
;
4677 this.refresh(this.rows
- 1, this.rows
- 1);
4680 Terminal
.prototype.leaveSearch = function() {
4681 this.searchMode
= false;
4683 if (this._real
.preSearch
) {
4684 this.lines
= this._real
.preSearch
;
4685 this.x
= this._real
.preSearchX
;
4686 this.y
= this._real
.preSearchY
;
4687 delete this._real
.preSearch
;
4688 delete this._real
.preSearchX
;
4689 delete this._real
.preSearchY
;
4692 this.refresh(this.rows
- 1, this.rows
- 1);
4695 Terminal
.prototype.copyBuffer = function(lines
) {
4696 var lines
= lines
|| this.lines
4699 for (var y
= 0; y
< lines
.length
; y
++) {
4701 for (var x
= 0; x
< lines
[y
].length
; x
++) {
4702 out
[y
][x
] = [lines
[y
][x
][0], lines
[y
][x
][1]];
4709 Terminal
.prototype.getCopyTextarea = function(text
) {
4710 var textarea
= this._copyTextarea
4711 , document
= this.document
;
4714 textarea
= document
.createElement('textarea');
4715 textarea
.style
.position
= 'absolute';
4716 textarea
.style
.left
= '-32000px';
4717 textarea
.style
.top
= '-32000px';
4718 textarea
.style
.width
= '0px';
4719 textarea
.style
.height
= '0px';
4720 textarea
.style
.opacity
= '0';
4721 textarea
.style
.backgroundColor
= 'transparent';
4722 textarea
.style
.borderStyle
= 'none';
4723 textarea
.style
.outlineStyle
= 'none';
4725 document
.getElementsByTagName('body')[0].appendChild(textarea
);
4727 this._copyTextarea
= textarea
;
4733 // NOTE: Only works for primary selection on X11.
4734 // Non-X11 users should use Ctrl-C instead.
4735 Terminal
.prototype.copyText = function(text
) {
4737 , textarea
= this.getCopyTextarea();
4739 this.emit('copy', text
);
4742 textarea
.textContent
= text
;
4743 textarea
.value
= text
;
4744 textarea
.setSelectionRange(0, text
.length
);
4746 setTimeout(function() {
4747 self
.element
.focus();
4752 Terminal
.prototype.selectText = function(x1
, x2
, y1
, y2
) {
4763 if (this._selected
) {
4764 ox1
= this._selected
.x1
;
4765 ox2
= this._selected
.x2
;
4766 oy1
= this._selected
.y1
;
4767 oy2
= this._selected
.y2
;
4778 if (ox2
< ox1
&& oy1
=== oy2
) {
4784 for (y
= oy1
; y
<= oy2
; y
++) {
4793 for (; x
<= xl
; x
++) {
4794 if (this.lines
[y
][x
].old
!= null) {
4795 //this.lines[y][x][0] = this.lines[y][x].old;
4796 //delete this.lines[y][x].old;
4797 attr
= this.lines
[y
][x
].old
;
4798 delete this.lines
[y
][x
].old
;
4799 this.lines
[y
][x
] = [attr
, this.lines
[y
][x
][1]];
4804 y1
= this._selected
.y1
;
4805 x1
= this._selected
.x1
;
4808 y1
= Math
.max(y1
, 0);
4809 y1
= Math
.min(y1
, this.ydisp
+ this.rows
- 1);
4811 y2
= Math
.max(y2
, 0);
4812 y2
= Math
.min(y2
, this.ydisp
+ this.rows
- 1);
4814 this._selected
= { x1
: x1
, x2
: x2
, y1
: y1
, y2
: y2
};
4825 if (x2
< x1
&& y1
=== y2
) {
4831 for (y
= y1
; y
<= y2
; y
++) {
4840 for (; x
<= xl
; x
++) {
4841 //this.lines[y][x].old = this.lines[y][x][0];
4842 //this.lines[y][x][0] &= ~0x1ff;
4843 //this.lines[y][x][0] |= (0x1ff << 9) | 4;
4844 attr
= this.lines
[y
][x
][0];
4845 this.lines
[y
][x
] = [
4846 (attr
& ~0x1ff) | ((0x1ff << 9) | 4),
4849 this.lines
[y
][x
].old
= attr
;
4853 y1
= y1
- this.ydisp
;
4854 y2
= y2
- this.ydisp
;
4856 y1
= Math
.max(y1
, 0);
4857 y1
= Math
.min(y1
, this.rows
- 1);
4859 y2
= Math
.max(y2
, 0);
4860 y2
= Math
.min(y2
, this.rows
- 1);
4862 //this.refresh(y1, y2);
4863 this.refresh(0, this.rows
- 1);
4866 Terminal
.prototype.grabText = function(x1
, x2
, y1
, y2
) {
4884 if (x2
< x1
&& y1
=== y2
) {
4890 for (y
= y1
; y
<= y2
; y
++) {
4899 for (; x
<= xl
; x
++) {
4900 ch
= this.lines
[y
][x
][1];
4910 if (isWide(ch
)) x
++;
4916 // If we're not at the end of the
4917 // line, don't add a newline.
4918 for (x
= x2
, y
= y2
; x
< this.cols
; x
++) {
4919 if (this.lines
[y
][x
][1] !== ' ') {
4920 out
= out
.slice(0, -1);
4928 Terminal
.prototype.keyPrefix = function(ev
, key
) {
4929 if (key
=== 'k' || key
=== '&') {
4931 } else if (key
=== 'p' || key
=== ']') {
4932 this.emit('request paste');
4933 } else if (key
=== 'c') {
4934 this.emit('request create');
4935 } else if (key
>= '0' && key
<= '9') {
4938 this.emit('request term', key
);
4939 } else if (key
=== 'n') {
4940 this.emit('request term next');
4941 } else if (key
=== 'P') {
4942 this.emit('request term previous');
4943 } else if (key
=== ':') {
4944 this.emit('request command mode');
4945 } else if (key
=== '[') {
4950 Terminal
.prototype.keySelect = function(ev
, key
) {
4953 if (this.searchMode
|| key
=== 'n' || key
=== 'N') {
4954 return this.keySearch(ev
, key
);
4957 if (key
=== '\x04') { // ctrl-d
4958 var y
= this.ydisp
+ this.y
;
4959 if (this.ydisp
=== this.ybase
) {
4960 // Mimic vim behavior
4961 this.y
= Math
.min(this.y
+ (this.rows
- 1) / 2 | 0, this.rows
- 1);
4962 this.refresh(0, this.rows
- 1);
4964 this.scrollDisp((this.rows
- 1) / 2 | 0);
4966 if (this.visualMode
) {
4967 this.selectText(this.x
, this.x
, y
, this.ydisp
+ this.y
);
4972 if (key
=== '\x15') { // ctrl-u
4973 var y
= this.ydisp
+ this.y
;
4974 if (this.ydisp
=== 0) {
4975 // Mimic vim behavior
4976 this.y
= Math
.max(this.y
- (this.rows
- 1) / 2 | 0, 0);
4977 this.refresh(0, this.rows
- 1);
4979 this.scrollDisp(-(this.rows
- 1) / 2 | 0);
4981 if (this.visualMode
) {
4982 this.selectText(this.x
, this.x
, y
, this.ydisp
+ this.y
);
4987 if (key
=== '\x06') { // ctrl-f
4988 var y
= this.ydisp
+ this.y
;
4989 this.scrollDisp(this.rows
- 1);
4990 if (this.visualMode
) {
4991 this.selectText(this.x
, this.x
, y
, this.ydisp
+ this.y
);
4996 if (key
=== '\x02') { // ctrl-b
4997 var y
= this.ydisp
+ this.y
;
4998 this.scrollDisp(-(this.rows
- 1));
4999 if (this.visualMode
) {
5000 this.selectText(this.x
, this.x
, y
, this.ydisp
+ this.y
);
5005 if (key
=== 'k' || key
=== '\x1b[A') {
5006 var y
= this.ydisp
+ this.y
;
5010 this.scrollDisp(-1);
5012 if (this.visualMode
) {
5013 this.selectText(this.x
, this.x
, y
, this.ydisp
+ this.y
);
5015 this.refresh(this.y
, this.y
+ 1);
5020 if (key
=== 'j' || key
=== '\x1b[B') {
5021 var y
= this.ydisp
+ this.y
;
5023 if (this.y
>= this.rows
) {
5024 this.y
= this.rows
- 1;
5027 if (this.visualMode
) {
5028 this.selectText(this.x
, this.x
, y
, this.ydisp
+ this.y
);
5030 this.refresh(this.y
- 1, this.y
);
5035 if (key
=== 'h' || key
=== '\x1b[D') {
5041 if (this.visualMode
) {
5042 this.selectText(x
, this.x
, this.ydisp
+ this.y
, this.ydisp
+ this.y
);
5044 this.refresh(this.y
, this.y
);
5049 if (key
=== 'l' || key
=== '\x1b[C') {
5052 if (this.x
>= this.cols
) {
5053 this.x
= this.cols
- 1;
5055 if (this.visualMode
) {
5056 this.selectText(x
, this.x
, this.ydisp
+ this.y
, this.ydisp
+ this.y
);
5058 this.refresh(this.y
, this.y
);
5063 if (key
=== 'v' || key
=== ' ') {
5064 if (!this.visualMode
) {
5073 if (this.visualMode
) {
5074 var text
= this.grabText(
5075 this._selected
.x1
, this._selected
.x2
,
5076 this._selected
.y1
, this._selected
.y2
);
5077 this.copyText(text
);
5079 // this.leaveSelect();
5084 if (key
=== 'q' || key
=== '\x1b') {
5085 if (this.visualMode
) {
5093 if (key
=== 'w' || key
=== 'W') {
5096 var oyd
= this.ydisp
;
5100 var yb
= this.ydisp
;
5101 var saw_space
= false;
5104 var line
= this.lines
[yb
+ y
];
5105 while (x
< this.cols
) {
5106 if (line
[x
][1] <= ' ') {
5108 } else if (saw_space
) {
5113 if (x
>= this.cols
) x
= this.cols
- 1;
5114 if (x
=== this.cols
- 1 && line
[x
][1] <= ' ') {
5116 if (++y
>= this.rows
) {
5118 if (++yb
> this.ybase
) {
5129 this.x
= x
, this.y
= y
;
5130 this.scrollDisp(-this.ydisp
+ yb
);
5132 if (this.visualMode
) {
5133 this.selectText(ox
, this.x
, oy
+ oyd
, this.ydisp
+ this.y
);
5138 if (key
=== 'b' || key
=== 'B') {
5141 var oyd
= this.ydisp
;
5145 var yb
= this.ydisp
;
5148 var line
= this.lines
[yb
+ y
];
5149 var saw_space
= x
> 0 && line
[x
][1] > ' ' && line
[x
- 1][1] > ' ';
5151 if (line
[x
][1] <= ' ') {
5152 if (saw_space
&& (x
+ 1 < this.cols
&& line
[x
+ 1][1] > ' ')) {
5162 if (x
=== 0 && (line
[x
][1] <= ' ' || !saw_space
)) {
5177 this.x
= x
, this.y
= y
;
5178 this.scrollDisp(-this.ydisp
+ yb
);
5180 if (this.visualMode
) {
5181 this.selectText(ox
, this.x
, oy
+ oyd
, this.ydisp
+ this.y
);
5186 if (key
=== 'e' || key
=== 'E') {
5189 var yb
= this.ydisp
;
5190 if (x
>= this.cols
) x
--;
5193 var line
= this.lines
[yb
+ y
];
5194 while (x
< this.cols
) {
5195 if (line
[x
][1] <= ' ') {
5201 while (x
< this.cols
) {
5202 if (line
[x
][1] <= ' ') {
5203 if (x
- 1 >= 0 && line
[x
- 1][1] > ' ') {
5210 if (x
>= this.cols
) x
= this.cols
- 1;
5211 if (x
=== this.cols
- 1 && line
[x
][1] <= ' ') {
5213 if (++y
>= this.rows
) {
5215 if (++yb
> this.ybase
) {
5225 this.x
= x
, this.y
= y
;
5226 this.scrollDisp(-this.ydisp
+ yb
);
5228 if (this.visualMode
) {
5229 this.selectText(ox
, this.x
, oy
+ oyd
, this.ydisp
+ this.y
);
5234 if (key
=== '^' || key
=== '0') {
5239 } else if (key
=== '^') {
5240 var line
= this.lines
[this.ydisp
+ this.y
];
5242 while (x
< this.cols
) {
5243 if (line
[x
][1] > ' ') {
5248 if (x
>= this.cols
) x
= this.cols
- 1;
5252 if (this.visualMode
) {
5253 this.selectText(ox
, this.x
, this.ydisp
+ this.y
, this.ydisp
+ this.y
);
5255 this.refresh(this.y
, this.y
);
5262 var line
= this.lines
[this.ydisp
+ this.y
];
5263 var x
= this.cols
- 1;
5265 if (line
[x
][1] > ' ') {
5266 if (this.visualMode
&& x
< this.cols
- 1) x
++;
5273 if (this.visualMode
) {
5274 this.selectText(ox
, this.x
, this.ydisp
+ this.y
, this.ydisp
+ this.y
);
5276 this.refresh(this.y
, this.y
);
5281 if (key
=== 'g' || key
=== 'G') {
5284 var oyd
= this.ydisp
;
5286 this.x
= 0, this.y
= 0;
5287 this.scrollDisp(-this.ydisp
);
5288 } else if (key
=== 'G') {
5289 this.x
= 0, this.y
= this.rows
- 1;
5290 this.scrollDisp(this.ybase
);
5292 if (this.visualMode
) {
5293 this.selectText(ox
, this.x
, oy
+ oyd
, this.ydisp
+ this.y
);
5298 if (key
=== 'H' || key
=== 'M' || key
=== 'L') {
5302 this.x
= 0, this.y
= 0;
5303 } else if (key
=== 'M') {
5304 this.x
= 0, this.y
= this.rows
/ 2 | 0;
5305 } else if (key
=== 'L') {
5306 this.x
= 0, this.y
= this.rows
- 1;
5308 if (this.visualMode
) {
5309 this.selectText(ox
, this.x
, this.ydisp
+ oy
, this.ydisp
+ this.y
);
5311 this.refresh(oy
, oy
);
5312 this.refresh(this.y
, this.y
);
5317 if (key
=== '{' || key
=== '}') {
5320 var oyd
= this.ydisp
;
5323 var saw_full
= false;
5325 var first_is_space
= -1;
5326 var y
= this.y
+ (key
=== '{' ? -1 : 1);
5327 var yb
= this.ydisp
;
5335 } else if (key
=== '}') {
5336 if (y
>= this.rows
) {
5338 if (yb
< this.ybase
) yb
++;
5343 line
= this.lines
[yb
+ y
];
5345 for (i
= 0; i
< this.cols
; i
++) {
5346 if (line
[i
][1] > ' ') {
5347 if (first_is_space
=== -1) {
5352 } else if (i
=== this.cols
- 1) {
5353 if (first_is_space
=== -1) {
5355 } else if (first_is_space
=== 0) {
5357 } else if (first_is_space
=== 1) {
5358 if (saw_full
) found
= true;
5373 } else if (key
=== '}') {
5375 if (y
>= this.rows
) {
5377 if (yb
< this.ybase
) yb
++;
5387 } else if (key
=== '}') {
5393 this.x
= 0, this.y
= y
;
5394 this.scrollDisp(-this.ydisp
+ yb
);
5396 if (this.visualMode
) {
5397 this.selectText(ox
, this.x
, oy
+ oyd
, this.ydisp
+ this.y
);
5402 if (key
=== '/' || key
=== '?') {
5403 if (!this.visualMode
) {
5404 this.enterSearch(key
=== '/');
5412 Terminal
.prototype.keySearch = function(ev
, key
) {
5413 if (key
=== '\x1b') {
5418 if (key
=== '\r' || (!this.searchMode
&& (key
=== 'n' || key
=== 'N'))) {
5421 var entry
= this.entry
;
5424 this.refresh(0, this.rows
- 1);
5430 var oyd
= this.ydisp
;
5434 var wrapped
= false;
5436 var y
= this.ydisp
+ this.y
;
5438 var up
= key
=== 'N'
5443 line
= this.lines
[y
];
5445 while (x
< this.cols
) {
5446 for (i
= 0; i
< entry
.length
; i
++) {
5447 if (x
+ i
>= this.cols
) break;
5448 if (line
[x
+ i
][1] !== entry
[i
]) {
5450 } else if (line
[x
+ i
][1] === entry
[i
] && i
=== entry
.length
- 1) {
5464 if (y
> this.ybase
+ this.rows
- 1) {
5466 // this.setMessage('Search wrapped. Continuing at TOP.');
5474 // this.setMessage('Search wrapped. Continuing at BOTTOM.');
5476 y
= this.ybase
+ this.rows
- 1;
5482 if (y
- this.ybase
< 0) {
5485 if (yb
> this.ybase
) {
5486 y
= yb
- this.ybase
;
5494 this.x
= x
, this.y
= y
;
5495 this.scrollDisp(-this.ydisp
+ yb
);
5497 if (this.visualMode
) {
5498 this.selectText(ox
, this.x
, oy
+ oyd
, this.ydisp
+ this.y
);
5503 // this.setMessage("No matches found.");
5504 this.refresh(0, this.rows
- 1);
5509 if (key
=== '\b' || key
=== '\x7f') {
5510 if (this.entry
.length
=== 0) return;
5511 var bottom
= this.ydisp
+ this.rows
- 1;
5512 this.entry
= this.entry
.slice(0, -1);
5513 var i
= this.entryPrefix
.length
+ this.entry
.length
;
5514 //this.lines[bottom][i][1] = ' ';
5515 this.lines
[bottom
][i
] = [
5516 this.lines
[bottom
][i
][0],
5520 this.refresh(this.rows
- 1, this.rows
- 1);
5521 this.refresh(this.y
, this.y
);
5525 if (key
.length
=== 1 && key
>= ' ' && key
<= '~') {
5526 var bottom
= this.ydisp
+ this.rows
- 1;
5528 var i
= this.entryPrefix
.length
+ this.entry
.length
- 1;
5529 //this.lines[bottom][i][0] = (this.defAttr & ~0x1ff) | 4;
5530 //this.lines[bottom][i][1] = key;
5531 this.lines
[bottom
][i
] = [
5532 (this.defAttr
& ~0x1ff) | 4,
5536 this.refresh(this.rows
- 1, this.rows
- 1);
5537 this.refresh(this.y
, this.y
);
5548 Terminal
.charsets
= {};
5550 // DEC Special Character and Line Drawing Set.
5551 // http://vt100.net/docs/vt102-ug/table5-13.html
5552 // A lot of curses apps use this if they see TERM=xterm.
5553 // testing: echo -e '\e(0a\e(B'
5554 // The xterm output sometimes seems to conflict with the
5555 // reference above. xterm seems in line with the reference
5556 // when running vttest however.
5557 // The table below now uses xterm's output from vttest.
5558 Terminal
.charsets
.SCLD
= { // (0
5559 '`': '\u25c6', // '◆'
5560 'a': '\u2592', // '▒'
5561 'b': '\u0009', // '\t'
5562 'c': '\u000c', // '\f'
5563 'd': '\u000d', // '\r'
5564 'e': '\u000a', // '\n'
5565 'f': '\u00b0', // '°'
5566 'g': '\u00b1', // '±'
5567 'h': '\u2424', // '\u2424' (NL)
5568 'i': '\u000b', // '\v'
5569 'j': '\u2518', // '┘'
5570 'k': '\u2510', // '┐'
5571 'l': '\u250c', // '┌'
5572 'm': '\u2514', // '└'
5573 'n': '\u253c', // '┼'
5574 'o': '\u23ba', // '⎺'
5575 'p': '\u23bb', // '⎻'
5576 'q': '\u2500', // '─'
5577 'r': '\u23bc', // '⎼'
5578 's': '\u23bd', // '⎽'
5579 't': '\u251c', // '├'
5580 'u': '\u2524', // '┤'
5581 'v': '\u2534', // '┴'
5582 'w': '\u252c', // '┬'
5583 'x': '\u2502', // '│'
5584 'y': '\u2264', // '≤'
5585 'z': '\u2265', // '≥'
5586 '{': '\u03c0', // 'π'
5587 '|': '\u2260', // '≠'
5588 '}': '\u00a3', // '£'
5589 '~': '\u00b7' // '·'
5592 Terminal
.charsets
.UK
= null; // (A
5593 Terminal
.charsets
.US
= null; // (B (USASCII)
5594 Terminal
.charsets
.Dutch
= null; // (4
5595 Terminal
.charsets
.Finnish
= null; // (C or (5
5596 Terminal
.charsets
.French
= null; // (R
5597 Terminal
.charsets
.FrenchCanadian
= null; // (Q
5598 Terminal
.charsets
.German
= null; // (K
5599 Terminal
.charsets
.Italian
= null; // (Y
5600 Terminal
.charsets
.NorwegianDanish
= null; // (E or (6
5601 Terminal
.charsets
.Spanish
= null; // (Z
5602 Terminal
.charsets
.Swedish
= null; // (H or (7
5603 Terminal
.charsets
.Swiss
= null; // (=
5604 Terminal
.charsets
.ISOLatin
= null; // /A
5610 function on(el
, type
, handler
, capture
) {
5611 el
.addEventListener(type
, handler
, capture
|| false);
5614 function off(el
, type
, handler
, capture
) {
5615 el
.removeEventListener(type
, handler
, capture
|| false);
5618 function cancel(ev
) {
5619 if (ev
.preventDefault
) ev
.preventDefault();
5620 ev
.returnValue
= false;
5621 if (ev
.stopPropagation
) ev
.stopPropagation();
5622 ev
.cancelBubble
= true;
5626 function inherits(child
, parent
) {
5628 this.constructor = child
;
5630 f
.prototype = parent
.prototype;
5631 child
.prototype = new f
;
5634 // if bold is broken, we can't
5635 // use it in the terminal.
5636 function isBoldBroken(document
) {
5637 var body
= document
.getElementsByTagName('body')[0];
5638 var el
= document
.createElement('span');
5639 el
.innerHTML
= 'hello world';
5640 body
.appendChild(el
);
5641 var w1
= el
.scrollWidth
;
5642 el
.style
.fontWeight
= 'bold';
5643 var w2
= el
.scrollWidth
;
5644 body
.removeChild(el
);
5648 var String
= this.String
;
5649 var setTimeout
= this.setTimeout
;
5650 var setInterval
= this.setInterval
;
5652 function indexOf(obj
, el
) {
5655 if (obj
[i
] === el
) return i
;
5660 function isWide(ch
) {
5661 if (ch
<= '\uff00') return false;
5662 return (ch
>= '\uff01' && ch
<= '\uffbe')
5663 || (ch
>= '\uffc2' && ch
<= '\uffc7')
5664 || (ch
>= '\uffca' && ch
<= '\uffcf')
5665 || (ch
>= '\uffd2' && ch
<= '\uffd7')
5666 || (ch
>= '\uffda' && ch
<= '\uffdc')
5667 || (ch
>= '\uffe0' && ch
<= '\uffe6')
5668 || (ch
>= '\uffe8' && ch
<= '\uffee');
5671 function matchColor(r1
, g1
, b1
) {
5672 var hash
= (r1
<< 16) | (g1
<< 8) | b1
;
5674 if (matchColor
._cache
[hash
] != null) {
5675 return matchColor
._cache
[hash
];
5678 var ldiff
= Infinity
5687 for (; i
< Terminal
.vcolors
.length
; i
++) {
5688 c
= Terminal
.vcolors
[i
];
5693 diff
= matchColor
.distance(r1
, g1
, b1
, r2
, g2
, b2
);
5706 return matchColor
._cache
[hash
] = li
;
5709 matchColor
._cache
= {};
5711 // http://stackoverflow.com/questions/1633828
5712 matchColor
.distance = function(r1
, g1
, b1
, r2
, g2
, b2
) {
5713 return Math
.pow(30 * (r1
- r2
), 2)
5714 + Math
.pow(59 * (g1
- g2
), 2)
5715 + Math
.pow(11 * (b1
- b2
), 2);
5718 function each(obj
, iter
, con
) {
5719 if (obj
.forEach
) return obj
.forEach(iter
, con
);
5720 for (var i
= 0; i
< obj
.length
; i
++) {
5721 iter
.call(con
, obj
[i
], i
, obj
);
5725 function keys(obj
) {
5726 if (Object
.keys
) return Object
.keys(obj
);
5729 if (Object
.prototype.hasOwnProperty
.call(obj
, key
)) {
5740 Terminal
.EventEmitter
= EventEmitter
;
5741 Terminal
.inherits
= inherits
;
5744 Terminal
.cancel
= cancel
;
5746 if (typeof module
!== 'undefined') {
5747 module
.exports
= Terminal
;
5749 this.Terminal
= Terminal
;
5752 }).call(function() {
5753 return this || (typeof window
!== 'undefined' ? window
: global
);