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
34 import { EventEmitter
} from './EventEmitter.js';
35 import { Viewport
} from './Viewport.js';
38 * Terminal Emulation References:
40 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.txt
41 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
42 * http://invisible-island.net/vttest/
43 * http://www.inwap.com/pdp10/ansicode.txt
44 * http://linux.die.net/man/4/console_codes
45 * http://linux.die.net/man/7/urxvt
48 // Let it work inside Node.js for automated testing purposes.
49 var document
= (typeof window
!= 'undefined') ? window
.document
: null;
52 * Encapsulates the logic for handling compositionstart, compositionupdate and compositionend
53 * events, displaying the in-progress composition to the UI and forwarding the final composition
55 * @param {HTMLTextAreaElement} textarea The textarea that xterm uses for input.
56 * @param {HTMLElement} compositionView The element to display the in-progress composition in.
57 * @param {Terminal} terminal The Terminal to forward the finished composition to.
59 function CompositionHelper(textarea
, compositionView
, terminal
) {
60 this.textarea
= textarea
;
61 this.compositionView
= compositionView
;
62 this.terminal
= terminal
;
64 // Whether input composition is currently happening, eg. via a mobile keyboard, speech input
65 // or IME. This variable determines whether the compositionText should be displayed on the UI.
66 this.isComposing
= false;
68 // The input currently being composed, eg. via a mobile keyboard, speech input or IME.
69 this.compositionText
= null;
71 // The position within the input textarea's value of the current composition.
72 this.compositionPosition
= { start
: null, end
: null };
74 // Whether a composition is in the process of being sent, setting this to false will cancel
75 // any in-progress composition.
76 this.isSendingComposition
= false;
80 * Handles the compositionstart event, activating the composition view.
82 CompositionHelper
.prototype.compositionstart = function() {
83 this.isComposing
= true;
84 this.compositionPosition
.start
= this.textarea
.value
.length
;
85 this.compositionView
.textContent
= '';
86 this.compositionView
.classList
.add('active');
90 * Handles the compositionupdate event, updating the composition view.
91 * @param {CompositionEvent} ev The event.
93 CompositionHelper
.prototype.compositionupdate = function(ev
) {
94 this.compositionView
.textContent
= ev
.data
;
95 this.updateCompositionElements();
97 setTimeout(function() {
98 self
.compositionPosition
.end
= self
.textarea
.value
.length
;
103 * Handles the compositionend event, hiding the composition view and sending the composition to
106 CompositionHelper
.prototype.compositionend = function() {
107 this.finalizeComposition(true);
111 * Handles the keydown event, routing any necessary events to the CompositionHelper functions.
112 * @return Whether the Terminal should continue processing the keydown event.
114 CompositionHelper
.prototype.keydown = function(ev
) {
115 if (this.isComposing
|| this.isSendingComposition
) {
116 if (ev
.keyCode
=== 229) {
117 // Continue composing if the keyCode is the "composition character"
119 } else if (ev
.keyCode
=== 16 || ev
.keyCode
=== 17 || ev
.keyCode
=== 18) {
120 // Continue composing if the keyCode is a modifier key
123 // Finish composition immediately. This is mainly here for the case where enter is
124 // pressed and the handler needs to be triggered before the command is executed.
125 this.finalizeComposition(false);
129 if (ev
.keyCode
=== 229) {
130 // If the "composition character" is used but gets to this point it means a non-composition
131 // character (eg. numbers and punctuation) was pressed when the IME was active.
132 this.handleAnyTextareaChanges();
140 * Finalizes the composition, resuming regular input actions. This is called when a composition
142 * @param {boolean} waitForPropogation Whether to wait for events to propogate before sending
143 * the input. This should be false if a non-composition keystroke is entered before the
144 * compositionend event is triggered, such as enter, so that the composition is send before
145 * the command is executed.
147 CompositionHelper
.prototype.finalizeComposition = function(waitForPropogation
) {
148 this.compositionView
.classList
.remove('active');
149 this.isComposing
= false;
150 this.clearTextareaPosition();
152 if (!waitForPropogation
) {
153 // Cancel any delayed composition send requests and send the input immediately.
154 this.isSendingComposition
= false;
155 var input
= this.textarea
.value
.substring(this.compositionPosition
.start
, this.compositionPosition
.end
);
156 this.terminal
.handler(input
);
158 // Make a deep copy of the composition position here as a new compositionstart event may
159 // fire before the setTimeout executes.
160 var currentCompositionPosition
= {
161 start
: this.compositionPosition
.start
,
162 end
: this.compositionPosition
.end
,
165 // Since composition* events happen before the changes take place in the textarea on most
166 // browsers, use a setTimeout with 0ms time to allow the native compositionend event to
167 // complete. This ensures the correct character is retrieved, this solution was used
169 // - The compositionend event's data property is unreliable, at least on Chromium
170 // - The last compositionupdate event's data property does not always accurately describe
171 // the character, a counter example being Korean where an ending consonsant can move to
172 // the following character if the following input is a vowel.
174 this.isSendingComposition
= true;
175 setTimeout(function () {
176 // Ensure that the input has not already been sent
177 if (self
.isSendingComposition
) {
178 self
.isSendingComposition
= false;
180 if (self
.isComposing
) {
181 // Use the end position to get the string if a new composition has started.
182 input
= self
.textarea
.value
.substring(currentCompositionPosition
.start
, currentCompositionPosition
.end
);
184 // Don't use the end position here in order to pick up any characters after the
185 // composition has finished, for example when typing a non-composition character
186 // (eg. 2) after a composition character.
187 input
= self
.textarea
.value
.substring(currentCompositionPosition
.start
);
189 self
.terminal
.handler(input
);
196 * Apply any changes made to the textarea after the current event chain is allowed to complete.
197 * This should be called when not currently composing but a keydown event with the "composition
198 * character" (229) is triggered, in order to allow non-composition text to be entered when an
201 CompositionHelper
.prototype.handleAnyTextareaChanges = function() {
202 var oldValue
= this.textarea
.value
;
204 setTimeout(function() {
205 // Ignore if a composition has started since the timeout
206 if (!self
.isComposing
) {
207 var newValue
= self
.textarea
.value
;
208 var diff
= newValue
.replace(oldValue
, '');
209 if (diff
.length
> 0) {
210 self
.terminal
.handler(diff
);
217 * Positions the composition view on top of the cursor and the textarea just below it (so the
218 * IME helper dialog is positioned correctly).
220 CompositionHelper
.prototype.updateCompositionElements = function(dontRecurse
) {
221 if (!this.isComposing
) {
224 var cursor
= this.terminal
.element
.querySelector('.terminal-cursor');
226 this.compositionView
.style
.left
= cursor
.offsetLeft
+ 'px';
227 this.compositionView
.style
.top
= cursor
.offsetTop
+ 'px';
228 var compositionViewBounds
= this.compositionView
.getBoundingClientRect();
229 this.textarea
.style
.left
= cursor
.offsetLeft
+ compositionViewBounds
.width
+ 'px';
230 this.textarea
.style
.top
= (cursor
.offsetTop
+ cursor
.offsetHeight
) + 'px';
233 setTimeout(this.updateCompositionElements
.bind(this, true), 0);
238 * Clears the textarea's position so that the cursor does not blink on IE.
241 CompositionHelper
.prototype.clearTextareaPosition = function() {
242 this.textarea
.style
.left
= '';
243 this.textarea
.style
.top
= '';
249 var normal
= 0, escaped
= 1, csi
= 2, osc
= 3, charset
= 4, dcs
= 5, ignore
= 6;
256 * Creates a new `Terminal` object.
258 * @param {object} options An object containing a set of options, the available options are:
259 * - cursorBlink (boolean): Whether the terminal cursor blinks
263 * @alias module:xterm/src/xterm
265 function Terminal(options
) {
268 if (!(this instanceof Terminal
)) {
269 return new Terminal(arguments
[0], arguments
[1], arguments
[2]);
272 self
.cancel
= Terminal
.cancel
;
274 EventEmitter
.call(this);
276 if (typeof options
=== 'number') {
280 handler
: arguments
[2]
284 options
= options
|| {};
287 Object
.keys(Terminal
.defaults
).forEach(function(key
) {
288 if (options
[key
] == null) {
289 options
[key
] = Terminal
.options
[key
];
291 if (Terminal
[key
] !== Terminal
.defaults
[key
]) {
292 options
[key
] = Terminal
[key
];
295 self
[key
] = options
[key
];
298 if (options
.colors
.length
=== 8) {
299 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(8));
300 } else if (options
.colors
.length
=== 16) {
301 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(16));
302 } else if (options
.colors
.length
=== 10) {
303 options
.colors
= options
.colors
.slice(0, -2).concat(
304 Terminal
._colors
.slice(8, -2), options
.colors
.slice(-2));
305 } else if (options
.colors
.length
=== 18) {
306 options
.colors
= options
.colors
.concat(
307 Terminal
._colors
.slice(16, -2), options
.colors
.slice(-2));
309 this.colors
= options
.colors
;
311 this.options
= options
;
313 // this.context = options.context || window;
314 // this.document = options.document || document;
315 this.parent
= options
.body
|| options
.parent
316 || (document
? document
.getElementsByTagName('body')[0] : null);
318 this.cols
= options
.cols
|| options
.geometry
[0];
319 this.rows
= options
.rows
|| options
.geometry
[1];
321 if (options
.handler
) {
322 this.on('data', options
.handler
);
326 * The scroll position of the y cursor, ie. ybase + y = the y position within the entire
332 * The scroll position of the viewport
337 * The cursor's x position after ybase
342 * The cursor's y position after ybase
347 * Used to debounce the refresh function
349 this.isRefreshing
= false;
352 * Whether there is a full terminal refresh queued
355 this.cursorState
= 0;
356 this.cursorHidden
= false;
361 this.scrollBottom
= this.rows
- 1;
362 this.customKeydownHandler
= null;
365 this.applicationKeypad
= false;
366 this.applicationCursor
= false;
367 this.originMode
= false;
368 this.insertMode
= false;
369 this.wraparoundMode
= true; // defaults: xterm - true, vt100 - false
374 this.gcharset
= null;
376 this.charsets
= [null];
400 this.readable
= true;
401 this.writable
= true;
403 this.defAttr
= (0 << 18) | (257 << 9) | (256 << 0);
404 this.curAttr
= this.defAttr
;
407 this.currentParam
= 0;
411 // leftover surrogate high from previous write invocation
412 this.surrogate_high
= '';
415 * An array of all lines in the entire buffer, including the prompt. The lines are array of
416 * characters which are 2-length arrays where [0] is an attribute and [1] is the character.
421 this.lines
.push(this.blankLine());
428 inherits(Terminal
, EventEmitter
);
431 * back_color_erase feature for xterm.
433 Terminal
.prototype.eraseAttr = function() {
434 // if (this.is('screen')) return this.defAttr;
435 return (this.defAttr
& ~0x1ff) | (this.curAttr
& 0x1ff);
443 Terminal
.tangoColors
= [
464 // Colors 0-15 + 16-255
465 // Much thanks to TooTallNate for writing this.
466 Terminal
.colors
= (function() {
467 var colors
= Terminal
.tangoColors
.slice()
468 , r
= [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff]
473 for (; i
< 216; i
++) {
474 out(r
[(i
/ 36) % 6 | 0], r
[(i
/ 6) % 6 | 0], r
[i
% 6]);
479 for (; i
< 24; i
++) {
484 function out(r
, g
, b
) {
485 colors
.push('#' + hex(r
) + hex(g
) + hex(b
));
490 return c
.length
< 2 ? '0' + c
: c
;
496 Terminal
._colors
= Terminal
.colors
.slice();
498 Terminal
.vcolors
= (function() {
500 , colors
= Terminal
.colors
504 for (; i
< 256; i
++) {
505 color
= parseInt(colors
[i
].substring(1), 16);
507 (color
>> 16) & 0xff,
520 Terminal
.defaults
= {
521 colors
: Terminal
.colors
,
533 // programFeatures: false,
537 Terminal
.options
= {};
539 Terminal
.focus
= null;
541 each(keys(Terminal
.defaults
), function(key
) {
542 Terminal
[key
] = Terminal
.defaults
[key
];
543 Terminal
.options
[key
] = Terminal
.defaults
[key
];
547 * Focus the terminal. Delegates focus handling to the terminal's DOM element.
549 Terminal
.prototype.focus = function() {
550 return this.textarea
.focus();
554 * Binds the desired focus behavior on a given terminal object.
558 Terminal
.bindFocus = function (term
) {
559 on(term
.textarea
, 'focus', function (ev
) {
560 if (term
.sendFocus
) {
563 term
.element
.classList
.add('focus');
565 Terminal
.focus
= term
;
566 term
.emit('focus', {terminal
: term
});
571 * Blur the terminal. Delegates blur handling to the terminal's DOM element.
573 Terminal
.prototype.blur = function() {
574 return this.textarea
.blur();
578 * Binds the desired blur behavior on a given terminal object.
582 Terminal
.bindBlur = function (term
) {
583 on(term
.textarea
, 'blur', function (ev
) {
584 term
.refresh(term
.y
, term
.y
);
585 if (term
.sendFocus
) {
588 term
.element
.classList
.remove('focus');
589 Terminal
.focus
= null;
590 term
.emit('blur', {terminal
: term
});
595 * Initialize default behavior
597 Terminal
.prototype.initGlobal = function() {
598 Terminal
.bindPaste(this);
599 Terminal
.bindKeys(this);
600 Terminal
.bindCopy(this);
601 Terminal
.bindFocus(this);
602 Terminal
.bindBlur(this);
606 * Bind to paste event and allow both keyboard and right-click pasting, without having the
607 * contentEditable value set to true.
609 Terminal
.bindPaste = function(term
) {
610 on([term
.textarea
, term
.element
], 'paste', function(ev
) {
611 ev
.stopPropagation();
612 if (ev
.clipboardData
) {
613 var text
= ev
.clipboardData
.getData('text/plain');
615 term
.textarea
.value
= '';
616 return term
.cancel(ev
);
622 * Prepares text copied from terminal selection, to be saved in the clipboard by:
623 * 1. stripping all trailing white spaces
624 * 2. converting all non-breaking spaces to regular spaces
625 * @param {string} text The copied text that needs processing for storing in clipboard
629 Terminal
.prepareCopiedTextForClipboard = function (text
) {
630 var space
= String
.fromCharCode(32),
631 nonBreakingSpace
= String
.fromCharCode(160),
632 allNonBreakingSpaces
= new RegExp(nonBreakingSpace
, 'g'),
633 processedText
= text
.split('\n').map(function (line
) {
635 * Strip all trailing white spaces and convert all non-breaking spaces to regular
638 var processedLine
= line
.replace(/\s+$/g, '').replace(allNonBreakingSpaces
, space
);
640 return processedLine
;
643 return processedText
;
647 * Apply key handling to the terminal
649 Terminal
.bindKeys = function(term
) {
650 on(term
.element
, 'keydown', function(ev
) {
651 if (document
.activeElement
!= this) {
657 on(term
.element
, 'keypress', function(ev
) {
658 if (document
.activeElement
!= this) {
664 on(term
.element
, 'keyup', term
.focus
.bind(term
));
666 on(term
.textarea
, 'keydown', function(ev
) {
670 on(term
.textarea
, 'keypress', function(ev
) {
672 // Truncate the textarea's value, since it is not needed
676 on(term
.textarea
, 'compositionstart', term
.compositionHelper
.compositionstart
.bind(term
.compositionHelper
));
677 on(term
.textarea
, 'compositionupdate', term
.compositionHelper
.compositionupdate
.bind(term
.compositionHelper
));
678 on(term
.textarea
, 'compositionend', term
.compositionHelper
.compositionend
.bind(term
.compositionHelper
));
679 term
.on('refresh', term
.compositionHelper
.updateCompositionElements
.bind(term
.compositionHelper
));
683 * Binds copy functionality to the given terminal.
686 Terminal
.bindCopy = function(term
) {
687 on(term
.element
, 'copy', function(ev
) {
694 * Insert the given row to the terminal or produce a new one
695 * if no row argument is passed. Return the inserted row.
696 * @param {HTMLElement} row (optional) The row to append to the terminal.
698 Terminal
.prototype.insertRow = function (row
) {
699 if (typeof row
!= 'object') {
700 row
= document
.createElement('div');
703 this.rowContainer
.appendChild(row
);
704 this.children
.push(row
);
710 * Opens the terminal within an element.
712 * @param {HTMLElement} parent The element to create the terminal within.
714 Terminal
.prototype.open = function(parent
) {
715 var self
=this, i
=0, div
;
717 this.parent
= parent
|| this.parent
;
720 throw new Error('Terminal requires a parent element.');
724 * Grab global elements
726 this.context
= this.parent
.ownerDocument
.defaultView
;
727 this.document
= this.parent
.ownerDocument
;
728 this.body
= this.document
.getElementsByTagName('body')[0];
733 if (this.context
.navigator
&& this.context
.navigator
.userAgent
) {
734 this.isMSIE
= !!~this.context
.navigator
.userAgent
.indexOf('MSIE');
738 * Find the users platform. We use this to interpret the meta key
739 * and ISO third level shifts.
740 * http://stackoverflow.com/questions/19877924/what-is-the-list-of-possible-values-for-navigator-platform-as-of-today
742 if (this.context
.navigator
&& this.context
.navigator
.platform
) {
743 this.isMac
= contains(
744 this.context
.navigator
.platform
,
745 ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K']
747 this.isIpad
= this.context
.navigator
.platform
=== 'iPad';
748 this.isIphone
= this.context
.navigator
.platform
=== 'iPhone';
749 this.isMSWindows
= contains(
750 this.context
.navigator
.platform
,
751 ['Windows', 'Win16', 'Win32', 'WinCE']
756 * Create main element container
758 this.element
= this.document
.createElement('div');
759 this.element
.classList
.add('terminal');
760 this.element
.classList
.add('xterm');
761 this.element
.classList
.add('xterm-theme-' + this.theme
);
763 this.element
.style
.height
764 this.element
.setAttribute('tabindex', 0);
766 this.viewportElement
= document
.createElement('div');
767 this.viewportElement
.classList
.add('xterm-viewport');
768 this.element
.appendChild(this.viewportElement
);
769 this.viewportScrollArea
= document
.createElement('div');
770 this.viewportScrollArea
.classList
.add('xterm-scroll-area');
771 this.viewportElement
.appendChild(this.viewportScrollArea
);
774 * Create the container that will hold the lines of the terminal and then
775 * produce the lines the lines.
777 this.rowContainer
= document
.createElement('div');
778 this.rowContainer
.classList
.add('xterm-rows');
779 this.element
.appendChild(this.rowContainer
);
783 * Create the container that will hold helpers like the textarea for
784 * capturing DOM Events. Then produce the helpers.
786 this.helperContainer
= document
.createElement('div');
787 this.helperContainer
.classList
.add('xterm-helpers');
788 // TODO: This should probably be inserted once it's filled to prevent an additional layout
789 this.element
.appendChild(this.helperContainer
);
790 this.textarea
= document
.createElement('textarea');
791 this.textarea
.classList
.add('xterm-helper-textarea');
792 this.textarea
.setAttribute('autocorrect', 'off');
793 this.textarea
.setAttribute('autocapitalize', 'off');
794 this.textarea
.setAttribute('spellcheck', 'false');
795 this.textarea
.tabIndex
= 0;
796 this.textarea
.addEventListener('focus', function() {
797 self
.emit('focus', {terminal
: self
});
799 this.textarea
.addEventListener('blur', function() {
800 self
.emit('blur', {terminal
: self
});
802 this.helperContainer
.appendChild(this.textarea
);
804 this.compositionView
= document
.createElement('div');
805 this.compositionView
.classList
.add('composition-view');
806 this.compositionHelper
= new CompositionHelper(this.textarea
, this.compositionView
, this);
807 this.helperContainer
.appendChild(this.compositionView
);
809 this.charMeasureElement
= document
.createElement('div');
810 this.charMeasureElement
.classList
.add('xterm-char-measure-element');
811 this.charMeasureElement
.innerHTML
= 'W';
812 this.helperContainer
.appendChild(this.charMeasureElement
);
814 for (; i
< this.rows
; i
++) {
817 this.parent
.appendChild(this.element
);
819 this.viewport
= new Viewport(this, this.viewportElement
, this.viewportScrollArea
, this.charMeasureElement
);
822 this.refresh(0, this.rows
- 1);
824 // Initialize global actions that
825 // need to be taken on the document.
828 // Ensure there is a Terminal.focus.
831 on(this.element
, 'mouseup', function() {
832 var selection
= document
.getSelection(),
833 collapsed
= selection
.isCollapsed
,
834 isRange
= typeof collapsed
== 'boolean' ? !collapsed
: selection
.type
== 'Range';
840 // Listen for mouse events and translate
841 // them into terminal mouse protocols.
844 // Figure out whether boldness affects
845 // the character width of monospace fonts.
846 if (Terminal
.brokenBold
== null) {
847 Terminal
.brokenBold
= isBoldBroken(this.document
);
855 * Attempts to load an add-on using CommonJS or RequireJS (whichever is available).
856 * @param {string} addon The name of the addon to load
859 Terminal
.loadAddon = function(addon
, callback
) {
860 if (typeof exports
=== 'object' && typeof module
=== 'object') {
862 return require(__dirname
+ '/../addons/' + addon
);
863 } else if (typeof define
== 'function') {
865 return require(['../addons/' + addon
+ '/' + addon
], callback
);
867 console
.error('Cannot load a module without a CommonJS or RequireJS environment.');
875 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#Mouse%20Tracking
876 * To better understand these
877 * the xterm code is very helpful:
879 * button.c, charproc.c, misc.c
880 * Relevant functions in xterm/button.c:
881 * BtnCode, EmitButtonCode, EditorButton, SendMousePosition
883 Terminal
.prototype.bindMouse = function() {
884 var el
= this.element
, self
= this, pressed
= 32;
886 // mouseup, mousedown, wheel
887 // left click: ^[[M 3<^[[M#3<
889 function sendButton(ev
) {
893 // get the xterm-style button
894 button
= getButton(ev
);
896 // get mouse coordinates
900 sendEvent(button
, pos
);
902 switch (ev
.overrideType
|| ev
.type
) {
907 // keep it at the left
908 // button, just in case.
919 // motion example of a left click:
920 // ^[[M 3<^[[M@4<^[[M@5<^[[M@6<^[[M@7<^[[M#7<
921 function sendMove(ev
) {
928 // buttons marked as motions
929 // are incremented by 32
932 sendEvent(button
, pos
);
936 // position to characters
937 function encode(data
, ch
) {
938 if (!self
.utfMouse
) {
939 if (ch
=== 255) return data
.push(0);
940 if (ch
> 127) ch
= 127;
943 if (ch
=== 2047) return data
.push(0);
947 if (ch
> 2047) ch
= 2047;
948 data
.push(0xC0 | (ch
>> 6));
949 data
.push(0x80 | (ch
& 0x3F));
954 // send a mouse event:
955 // regular/utf8: ^[[M Cb Cx Cy
956 // urxvt: ^[[ Cb ; Cx ; Cy M
957 // sgr: ^[[ Cb ; Cx ; Cy M/m
958 // vt300: ^[[ 24(1/3/5)~ [ Cx , Cy ] \r
959 // locator: CSI P e ; P b ; P r ; P c ; P p & w
960 function sendEvent(button
, pos
) {
961 // self.emit('mouse', {
967 if (self
.vt300Mouse
) {
969 // http://www.vt100.net/docs/vt3xx-gp/chapter15.html
973 var data
= '\x1b[24';
974 if (button
=== 0) data
+= '1';
975 else if (button
=== 1) data
+= '3';
976 else if (button
=== 2) data
+= '5';
977 else if (button
=== 3) return;
979 data
+= '~[' + pos
.x
+ ',' + pos
.y
+ ']\r';
984 if (self
.decLocator
) {
989 if (button
=== 0) button
= 2;
990 else if (button
=== 1) button
= 4;
991 else if (button
=== 2) button
= 6;
992 else if (button
=== 3) button
= 3;
996 + (button
=== 3 ? 4 : 0)
1007 if (self
.urxvtMouse
) {
1012 self
.send('\x1b[' + button
+ ';' + pos
.x
+ ';' + pos
.y
+ 'M');
1016 if (self
.sgrMouse
) {
1020 + ((button
& 3) === 3 ? button
& ~3 : button
)
1025 + ((button
& 3) === 3 ? 'm' : 'M'));
1031 encode(data
, button
);
1032 encode(data
, pos
.x
);
1033 encode(data
, pos
.y
);
1035 self
.send('\x1b[M' + String
.fromCharCode
.apply(String
, data
));
1038 function getButton(ev
) {
1051 // 1, and 2 - with 64 added
1052 switch (ev
.overrideType
|| ev
.type
) {
1054 button
= ev
.button
!= null
1061 button
= button
=== 1 ? 0 : button
=== 4 ? 1 : button
;
1067 case 'DOMMouseScroll':
1068 button
= ev
.detail
< 0
1073 button
= ev
.wheelDeltaY
> 0
1079 // next three bits are the modifiers:
1080 // 4 = shift, 8 = meta, 16 = control
1081 shift
= ev
.shiftKey
? 4 : 0;
1082 meta
= ev
.metaKey
? 8 : 0;
1083 ctrl
= ev
.ctrlKey
? 16 : 0;
1084 mod
= shift
| meta
| ctrl
;
1087 if (self
.vt200Mouse
) {
1090 } else if (!self
.normalMouse
) {
1095 button
= (32 + (mod
<< 2)) + button
;
1100 // mouse coordinates measured in cols/rows
1101 function getCoords(ev
) {
1104 // ignore browsers without pageX for now
1105 if (ev
.pageX
== null) return;
1111 // should probably check offsetParent
1112 // but this is more portable
1113 while (el
&& el
!== self
.document
.documentElement
) {
1116 el
= 'offsetParent' in el
1121 // convert to cols/rows
1122 w
= self
.element
.clientWidth
;
1123 h
= self
.element
.clientHeight
;
1124 x
= Math
.ceil((x
/ w
) * self
.cols
);
1125 y
= Math
.ceil((y
/ h
) * self
.rows
);
1127 // be sure to avoid sending
1128 // bad positions to the program
1130 if (x
> self
.cols
) x
= self
.cols
;
1132 if (y
> self
.rows
) y
= self
.rows
;
1134 // xterm sends raw bytes and
1135 // starts at 32 (SP) for each.
1146 on(el
, 'mousedown', function(ev
) {
1147 if (!self
.mouseEvents
) return;
1156 //if (self.vt200Mouse && !self.normalMouse) {
1157 if (self
.vt200Mouse
) {
1158 ev
.overrideType
= 'mouseup';
1160 return self
.cancel(ev
);
1164 if (self
.normalMouse
) on(self
.document
, 'mousemove', sendMove
);
1166 // x10 compatibility mode can't send button releases
1167 if (!self
.x10Mouse
) {
1168 on(self
.document
, 'mouseup', function up(ev
) {
1170 if (self
.normalMouse
) off(self
.document
, 'mousemove', sendMove
);
1171 off(self
.document
, 'mouseup', up
);
1172 return self
.cancel(ev
);
1176 return self
.cancel(ev
);
1179 //if (self.normalMouse) {
1180 // on(self.document, 'mousemove', sendMove);
1183 on(el
, 'wheel', function(ev
) {
1184 if (!self
.mouseEvents
) return;
1187 || self
.decLocator
) return;
1189 return self
.cancel(ev
);
1192 // allow wheel scrolling in
1193 // the shell for example
1194 on(el
, 'wheel', function(ev
) {
1195 if (self
.mouseEvents
) return;
1196 if (self
.applicationKeypad
) return;
1197 self
.viewport
.onWheel(ev
);
1198 return self
.cancel(ev
);
1203 * Destroys the terminal.
1205 Terminal
.prototype.destroy = function() {
1206 this.readable
= false;
1207 this.writable
= false;
1209 this.handler = function() {};
1210 this.write = function() {};
1211 if (this.element
.parentNode
) {
1212 this.element
.parentNode
.removeChild(this.element
);
1214 //this.emit('close');
1219 * Flags used to render terminal text properly
1230 * Refreshes (re-renders) terminal content within two rows (inclusive)
1234 * In the screen buffer, each character is stored as a an array with a character
1235 * and a 32-bit integer:
1236 * - First value: a utf-16 character.
1238 * - Next 9 bits: background color (0-511).
1239 * - Next 9 bits: foreground color (0-511).
1240 * - Next 14 bits: a mask for misc. flags:
1247 * @param {number} start The row to start from (between 0 and terminal's height terminal - 1)
1248 * @param {number} end The row to end at (between fromRow and terminal's height terminal - 1)
1249 * @param {boolean} queue Whether the refresh should ran right now or be queued
1251 Terminal
.prototype.refresh = function(start
, end
, queue
) {
1254 // queue defaults to true
1255 queue
= (typeof queue
== 'undefined') ? true : queue
;
1258 * The refresh queue allows refresh to execute only approximately 30 times a second. For
1259 * commands that pass a significant amount of output to the write function, this prevents the
1260 * terminal from maxing out the CPU and making the UI unresponsive. While commands can still
1261 * run beyond what they do on the terminal, it is far better with a debounce in place as
1262 * every single terminal manipulation does not need to be constructed in the DOM.
1264 * A side-effect of this is that it makes ^C to interrupt a process seem more responsive.
1267 // If refresh should be queued, order the refresh and return.
1268 if (this._refreshIsQueued
) {
1269 // If a refresh has already been queued, just order a full refresh next
1270 this._fullRefreshNext
= true;
1272 setTimeout(function () {
1273 self
.refresh(start
, end
, false);
1275 this._refreshIsQueued
= true;
1280 // If refresh should be run right now (not be queued), release the lock
1281 this._refreshIsQueued
= false;
1283 // If multiple refreshes were requested, make a full refresh.
1284 if (this._fullRefreshNext
) {
1286 end
= this.rows
- 1;
1287 this._fullRefreshNext
= false // reset lock
1290 var x
, y
, i
, line
, out
, ch
, ch_width
, width
, data
, attr
, bg
, fg
, flags
, row
, parent
, focused
= document
.activeElement
;
1292 // If this is a big refresh, remove the terminal rows from the DOM for faster calculations
1293 if (end
- start
>= this.rows
/ 2) {
1294 parent
= this.element
.parentNode
;
1296 this.element
.removeChild(this.rowContainer
);
1303 if (end
>= this.rows
.length
) {
1304 this.log('`end` is too large. Most likely a bad CSR.');
1305 end
= this.rows
.length
- 1;
1308 for (; y
<= end
; y
++) {
1309 row
= y
+ this.ydisp
;
1311 line
= this.lines
[row
];
1314 if (this.y
=== y
- (this.ybase
- this.ydisp
)
1316 && !this.cursorHidden
) {
1322 attr
= this.defAttr
;
1325 for (; i
< width
; i
++) {
1328 ch_width
= line
[i
][2];
1332 if (i
=== x
) data
= -1;
1334 if (data
!== attr
) {
1335 if (attr
!== this.defAttr
) {
1338 if (data
!== this.defAttr
) {
1340 out
+= '<span class="reverse-video terminal-cursor';
1341 if (this.cursorBlink
) {
1346 var classNames
= [];
1349 fg
= (data
>> 9) & 0x1ff;
1352 if (flags
& Terminal
.flags
.BOLD
) {
1353 if (!Terminal
.brokenBold
) {
1354 classNames
.push('xterm-bold');
1356 // See: XTerm*boldColors
1357 if (fg
< 8) fg
+= 8;
1360 if (flags
& Terminal
.flags
.UNDERLINE
) {
1361 classNames
.push('xterm-underline');
1364 if (flags
& Terminal
.flags
.BLINK
) {
1365 classNames
.push('xterm-blink');
1369 * If inverse flag is on, then swap the foreground and background variables.
1371 if (flags
& Terminal
.flags
.INVERSE
) {
1372 /* One-line variable swap in JavaScript: http://stackoverflow.com/a/16201730 */
1373 bg = [fg, fg = bg][0];
1374 // Should inverse just be before the
1375 // above boldColors effect instead?
1376 if ((flags & 1) && fg < 8) fg += 8;
1379 if (flags & Terminal.flags.INVISIBLE) {
1380 classNames.push('xterm-hidden');
1384 * Weird situation: Invert flag used black foreground and white background results
1385 * in invalid background color, positioned at the 256 index of the 256 terminal
1386 * color map. Pin the colors manually in such a case.
1388 * Source: https://github.com/sourcelair/xterm.js/issues/57
1390 if (flags
& Terminal
.flags
.INVERSE
) {
1400 classNames
.push('xterm-bg-color-' + bg
);
1404 classNames
.push('xterm-color-' + fg
);
1408 if (classNames
.length
) {
1409 out
+= ' class="' + classNames
.join(' ') + '"';
1438 if (attr
!== this.defAttr
) {
1442 this.children
[y
].innerHTML
= out
;
1446 this.element
.appendChild(this.rowContainer
);
1449 this.emit('refresh', {element
: this.element
, start
: start
, end
: end
});
1453 * Display the cursor element
1455 Terminal
.prototype.showCursor = function() {
1456 if (!this.cursorState
) {
1457 this.cursorState
= 1;
1458 this.refresh(this.y
, this.y
);
1463 * Scroll the terminal
1465 Terminal
.prototype.scroll = function() {
1468 if (++this.ybase
=== this.scrollback
) {
1469 this.ybase
= this.ybase
/ 2 | 0;
1470 this.lines
= this.lines
.slice(-(this.ybase
+ this.rows
) + 1);
1473 this.ydisp
= this.ybase
;
1476 row
= this.ybase
+ this.rows
- 1;
1478 // subtract the bottom scroll region
1479 row
-= this.rows
- 1 - this.scrollBottom
;
1481 if (row
=== this.lines
.length
) {
1482 // potential optimization:
1483 // pushing is faster than splicing
1484 // when they amount to the same
1486 this.lines
.push(this.blankLine());
1489 this.lines
.splice(row
, 0, this.blankLine());
1492 if (this.scrollTop
!== 0) {
1493 if (this.ybase
!== 0) {
1495 this.ydisp
= this.ybase
;
1497 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
1501 this.updateRange(this.scrollTop
);
1502 this.updateRange(this.scrollBottom
);
1504 this.emit('scroll', this.ydisp
);
1508 * Scroll the display of the terminal
1509 * @param {number} disp The number of lines to scroll down (negatives scroll up).
1510 * @param {boolean} suppressScrollEvent Don't emit the scroll event as scrollDisp. This is used
1511 * to avoid unwanted events being handled by the veiwport when the event was triggered from the
1512 * viewport originally.
1514 Terminal
.prototype.scrollDisp = function(disp
, suppressScrollEvent
) {
1517 if (this.ydisp
> this.ybase
) {
1518 this.ydisp
= this.ybase
;
1519 } else if (this.ydisp
< 0) {
1523 if (!suppressScrollEvent
) {
1524 this.emit('scroll', this.ydisp
);
1527 this.refresh(0, this.rows
- 1);
1531 * Writes text to the terminal.
1532 * @param {string} text The text to write to the terminal.
1534 Terminal
.prototype.write = function(data
) {
1535 var l
= data
.length
, i
= 0, j
, cs
, ch
, code
, low
, ch_width
, row
;
1537 this.refreshStart
= this.y
;
1538 this.refreshEnd
= this.y
;
1540 if (this.ybase
!== this.ydisp
) {
1541 this.ydisp
= this.ybase
;
1542 this.emit('scroll', this.ydisp
);
1546 // apply leftover surrogate high from last write
1547 if (this.surrogate_high
) {
1548 data
= this.surrogate_high
+ data
;
1549 this.surrogate_high
= '';
1552 for (; i
< l
; i
++) {
1555 // FIXME: higher chars than 0xa0 are not allowed in escape sequences
1556 // --> maybe move to default
1557 code
= data
.charCodeAt(i
);
1558 if (0xD800 <= code
&& code
<= 0xDBFF) {
1559 // we got a surrogate high
1560 // get surrogate low (next 2 bytes)
1561 low
= data
.charCodeAt(i
+1);
1563 // end of data stream, save surrogate high
1564 this.surrogate_high
= ch
;
1567 code
= ((code
- 0xD800) * 0x400) + (low
- 0xDC00) + 0x10000;
1568 ch
+= data
.charAt(i
+1);
1570 // surrogate low - already handled above
1571 if (0xDC00 <= code
&& code
<= 0xDFFF)
1574 switch (this.state
) {
1585 if (this.convertEol
) {
1589 if (this.y
> this.scrollBottom
) {
1609 this.x
= this.nextStop();
1624 this.state
= escaped
;
1629 // calculate print space
1630 // expensive call, therefore we save width in line buffer
1631 ch_width
= wcwidth(code
);
1634 if (this.charset
&& this.charset
[ch
]) {
1635 ch
= this.charset
[ch
];
1638 row
= this.y
+ this.ybase
;
1640 // insert combining char in last cell
1641 // FIXME: needs handling after cursor jumps
1642 if (!ch_width
&& this.x
) {
1644 // dont overflow left
1645 if (this.lines
[row
][this.x
-1]) {
1646 if (!this.lines
[row
][this.x
-1][2]) {
1648 // found empty cell after fullwidth, need to go 2 cells back
1649 if (this.lines
[row
][this.x
-2])
1650 this.lines
[row
][this.x
-2][1] += ch
;
1653 this.lines
[row
][this.x
-1][1] += ch
;
1655 this.updateRange(this.y
);
1660 // goto next line if ch would overflow
1661 // TODO: needs a global min terminal width of 2
1662 if (this.x
+ch_width
-1 >= this.cols
) {
1663 // autowrap - DECAWM
1664 if (this.wraparoundMode
) {
1667 if (this.y
> this.scrollBottom
) {
1672 this.x
= this.cols
-1;
1673 if(ch_width
===2) // FIXME: check for xterm behavior
1677 row
= this.y
+ this.ybase
;
1679 // insert mode: move characters to right
1680 if (this.insertMode
) {
1681 // do this twice for a fullwidth char
1682 for (var moves
=0; moves
<ch_width
; ++moves
) {
1683 // remove last cell, if it's width is 0
1684 // we have to adjust the second last cell as well
1685 var removed
= this.lines
[this.y
+ this.ybase
].pop();
1687 && this.lines
[row
][this.cols
-2]
1688 && this.lines
[row
][this.cols
-2][2]===2)
1689 this.lines
[row
][this.cols
-2] = [this.curAttr
, ' ', 1];
1691 // insert empty cell at cursor
1692 this.lines
[row
].splice(this.x
, 0, [this.curAttr
, ' ', 1]);
1696 this.lines
[row
][this.x
] = [this.curAttr
, ch
, ch_width
];
1698 this.updateRange(this.y
);
1700 // fullwidth char - set next cell width to zero and advance cursor
1702 this.lines
[row
][this.x
] = [this.curAttr
, '', 0];
1711 // ESC [ Control Sequence Introducer ( CSI is 0x9b).
1714 this.currentParam
= 0;
1718 // ESC ] Operating System Command ( OSC is 0x9d).
1721 this.currentParam
= 0;
1725 // ESC P Device Control String ( DCS is 0x90).
1728 this.currentParam
= 0;
1732 // ESC _ Application Program Command ( APC is 0x9f).
1734 this.state
= ignore
;
1737 // ESC ^ Privacy Message ( PM is 0x9e).
1739 this.state
= ignore
;
1742 // ESC c Full Reset (RIS).
1747 // ESC E Next Line ( NEL is 0x85).
1748 // ESC D Index ( IND is 0x84).
1756 // ESC M Reverse Index ( RI is 0x8d).
1758 this.reverseIndex();
1761 // ESC % Select default/utf-8 character set.
1762 // @ = default, G = utf-8
1764 //this.charset = null;
1766 this.setgCharset(0, Terminal
.charsets
.US
);
1767 this.state
= normal
;
1771 // ESC (,),*,+,-,. Designate G0-G2 Character Set.
1772 case '(': // <-- this seems to get all the attention
1798 this.state
= charset
;
1801 // Designate G3 Character Set (VT300).
1802 // A = ISO Latin-1 Supplemental.
1806 this.state
= charset
;
1811 // Single Shift Select of G2 Character Set
1812 // ( SS2 is 0x8e). This affects next character only.
1816 // Single Shift Select of G3 Character Set
1817 // ( SS3 is 0x8f). This affects next character only.
1821 // Invoke the G2 Character Set as GL (LS2).
1826 // Invoke the G3 Character Set as GL (LS3).
1831 // Invoke the G3 Character Set as GR (LS3R).
1836 // Invoke the G2 Character Set as GR (LS2R).
1841 // Invoke the G1 Character Set as GR (LS1R).
1846 // ESC 7 Save Cursor (DECSC).
1849 this.state
= normal
;
1852 // ESC 8 Restore Cursor (DECRC).
1854 this.restoreCursor();
1855 this.state
= normal
;
1858 // ESC # 3 DEC line height/width
1860 this.state
= normal
;
1864 // ESC H Tab Set (HTS is 0x88).
1869 // ESC = Application Keypad (DECKPAM).
1871 this.log('Serial port requested application keypad.');
1872 this.applicationKeypad
= true;
1873 this.viewport
.setApplicationMode(true);
1874 this.state
= normal
;
1877 // ESC > Normal Keypad (DECKPNM).
1879 this.log('Switching back to normal keypad.');
1880 this.applicationKeypad
= false;
1881 this.viewport
.setApplicationMode(false);
1882 this.state
= normal
;
1886 this.state
= normal
;
1887 this.error('Unknown ESC control: %s.', ch
);
1894 case '0': // DEC Special Character and Line Drawing Set.
1895 cs
= Terminal
.charsets
.SCLD
;
1898 cs
= Terminal
.charsets
.UK
;
1900 case 'B': // United States (USASCII).
1901 cs
= Terminal
.charsets
.US
;
1904 cs
= Terminal
.charsets
.Dutch
;
1906 case 'C': // Finnish
1908 cs
= Terminal
.charsets
.Finnish
;
1911 cs
= Terminal
.charsets
.French
;
1913 case 'Q': // FrenchCanadian
1914 cs
= Terminal
.charsets
.FrenchCanadian
;
1917 cs
= Terminal
.charsets
.German
;
1919 case 'Y': // Italian
1920 cs
= Terminal
.charsets
.Italian
;
1922 case 'E': // NorwegianDanish
1924 cs
= Terminal
.charsets
.NorwegianDanish
;
1926 case 'Z': // Spanish
1927 cs
= Terminal
.charsets
.Spanish
;
1929 case 'H': // Swedish
1931 cs
= Terminal
.charsets
.Swedish
;
1934 cs
= Terminal
.charsets
.Swiss
;
1936 case '/': // ISOLatin (actually /A)
1937 cs
= Terminal
.charsets
.ISOLatin
;
1941 cs
= Terminal
.charsets
.US
;
1944 this.setgCharset(this.gcharset
, cs
);
1945 this.gcharset
= null;
1946 this.state
= normal
;
1952 // Set Text Parameters.
1953 if (ch
=== '\x1b' || ch
=== '\x07') {
1954 if (ch
=== '\x1b') i
++;
1956 this.params
.push(this.currentParam
);
1958 switch (this.params
[0]) {
1962 if (this.params
[1]) {
1963 this.title
= this.params
[1];
1964 this.handleTitle(this.title
);
1972 // change dynamic colors
1984 // change dynamic ui colors
1996 // manipulate selection data
2014 this.currentParam
= 0;
2015 this.state
= normal
;
2017 if (!this.params
.length
) {
2018 if (ch
>= '0' && ch
<= '9') {
2020 this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
2021 } else if (ch
=== ';') {
2022 this.params
.push(this.currentParam
);
2023 this.currentParam
= '';
2026 this.currentParam
+= ch
;
2033 if (ch
=== '?' || ch
=== '>' || ch
=== '!') {
2039 if (ch
>= '0' && ch
<= '9') {
2040 this.currentParam
= this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
2044 // '$', '"', ' ', '\''
2045 if (ch
=== '$' || ch
=== '"' || ch
=== ' ' || ch
=== '\'') {
2050 this.params
.push(this.currentParam
);
2051 this.currentParam
= 0;
2054 if (ch
=== ';') break;
2056 this.state
= normal
;
2060 // Cursor Up Ps Times (default = 1) (CUU).
2062 this.cursorUp(this.params
);
2066 // Cursor Down Ps Times (default = 1) (CUD).
2068 this.cursorDown(this.params
);
2072 // Cursor Forward Ps Times (default = 1) (CUF).
2074 this.cursorForward(this.params
);
2078 // Cursor Backward Ps Times (default = 1) (CUB).
2080 this.cursorBackward(this.params
);
2084 // Cursor Position [row;column] (default = [1,1]) (CUP).
2086 this.cursorPos(this.params
);
2089 // CSI Ps J Erase in Display (ED).
2091 this.eraseInDisplay(this.params
);
2094 // CSI Ps K Erase in Line (EL).
2096 this.eraseInLine(this.params
);
2099 // CSI Pm m Character Attributes (SGR).
2102 this.charAttributes(this.params
);
2106 // CSI Ps n Device Status Report (DSR).
2109 this.deviceStatus(this.params
);
2118 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
2120 this.insertChars(this.params
);
2124 // Cursor Next Line Ps Times (default = 1) (CNL).
2126 this.cursorNextLine(this.params
);
2130 // Cursor Preceding Line Ps Times (default = 1) (CNL).
2132 this.cursorPrecedingLine(this.params
);
2136 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
2138 this.cursorCharAbsolute(this.params
);
2142 // Insert Ps Line(s) (default = 1) (IL).
2144 this.insertLines(this.params
);
2148 // Delete Ps Line(s) (default = 1) (DL).
2150 this.deleteLines(this.params
);
2154 // Delete Ps Character(s) (default = 1) (DCH).
2156 this.deleteChars(this.params
);
2160 // Erase Ps Character(s) (default = 1) (ECH).
2162 this.eraseChars(this.params
);
2165 // CSI Pm ` Character Position Absolute
2166 // [column] (default = [row,1]) (HPA).
2168 this.charPosAbsolute(this.params
);
2172 // Horizontal Position Relative
2174 this.HPositionRelative(this.params
);
2178 // Send Device Attributes (Primary DA).
2180 // Send Device Attributes (Secondary DA)
2182 this.sendDeviceAttributes(this.params
);
2186 // Line Position Absolute [row] (default = [1,column]) (VPA).
2188 this.linePosAbsolute(this.params
);
2191 // 145 65 e * VPR - Vertical Position Relative
2193 this.VPositionRelative(this.params
);
2197 // Horizontal and Vertical Position [row;column] (default =
2200 this.HVPosition(this.params
);
2203 // CSI Pm h Set Mode (SM).
2204 // CSI ? Pm h - mouse escape codes, cursor escape codes
2206 this.setMode(this.params
);
2209 // CSI Pm l Reset Mode (RM).
2212 this.resetMode(this.params
);
2216 // Set Scrolling Region [top;bottom] (default = full size of win-
2220 this.setScrollRegion(this.params
);
2224 // Save cursor (ANSI.SYS).
2226 this.saveCursor(this.params
);
2230 // Restore cursor (ANSI.SYS).
2232 this.restoreCursor(this.params
);
2240 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
2242 this.cursorForwardTab(this.params
);
2245 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
2247 this.scrollUp(this.params
);
2250 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
2251 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
2254 // if (this.prefix === '>') {
2255 // this.resetTitleModes(this.params);
2258 // if (this.params.length > 2) {
2259 // this.initMouseTracking(this.params);
2262 if (this.params
.length
< 2 && !this.prefix
) {
2263 this.scrollDown(this.params
);
2268 // Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
2270 this.cursorBackwardTab(this.params
);
2273 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
2275 this.repeatPrecedingCharacter(this.params
);
2278 // CSI Ps g Tab Clear (TBC).
2280 this.tabClear(this.params
);
2283 // CSI Pm i Media Copy (MC).
2286 // this.mediaCopy(this.params);
2289 // CSI Pm m Character Attributes (SGR).
2291 // case 'm': // duplicate
2292 // if (this.prefix === '>') {
2293 // this.setResources(this.params);
2295 // this.charAttributes(this.params);
2299 // CSI Ps n Device Status Report (DSR).
2301 // case 'n': // duplicate
2302 // if (this.prefix === '>') {
2303 // this.disableModifiers(this.params);
2305 // this.deviceStatus(this.params);
2309 // CSI > Ps p Set pointer mode.
2310 // CSI ! p Soft terminal reset (DECSTR).
2312 // Request ANSI mode (DECRQM).
2314 // Request DEC private mode (DECRQM).
2317 switch (this.prefix
) {
2319 // this.setPointerMode(this.params);
2322 this.softReset(this.params
);
2325 // if (this.postfix === '$') {
2326 // this.requestPrivateMode(this.params);
2330 // if (this.postfix === '"') {
2331 // this.setConformanceLevel(this.params);
2332 // } else if (this.postfix === '$') {
2333 // this.requestAnsiMode(this.params);
2339 // CSI Ps q Load LEDs (DECLL).
2343 // if (this.postfix === ' ') {
2344 // this.setCursorStyle(this.params);
2347 // if (this.postfix === '"') {
2348 // this.setCharProtectionAttr(this.params);
2351 // this.loadLEDs(this.params);
2355 // Set Scrolling Region [top;bottom] (default = full size of win-
2358 // CSI Pt; Pl; Pb; Pr; Ps$ r
2359 // case 'r': // duplicate
2360 // if (this.prefix === '?') {
2361 // this.restorePrivateValues(this.params);
2362 // } else if (this.postfix === '$') {
2363 // this.setAttrInRectangle(this.params);
2365 // this.setScrollRegion(this.params);
2369 // CSI s Save cursor (ANSI.SYS).
2371 // case 's': // duplicate
2372 // if (this.prefix === '?') {
2373 // this.savePrivateValues(this.params);
2375 // this.saveCursor(this.params);
2379 // CSI Ps ; Ps ; Ps t
2380 // CSI Pt; Pl; Pb; Pr; Ps$ t
2384 // if (this.postfix === '$') {
2385 // this.reverseAttrInRectangle(this.params);
2386 // } else if (this.postfix === ' ') {
2387 // this.setWarningBellVolume(this.params);
2389 // if (this.prefix === '>') {
2390 // this.setTitleModeFeature(this.params);
2392 // this.manipulateWindow(this.params);
2397 // CSI u Restore cursor (ANSI.SYS).
2399 // case 'u': // duplicate
2400 // if (this.postfix === ' ') {
2401 // this.setMarginBellVolume(this.params);
2403 // this.restoreCursor(this.params);
2407 // CSI Pt; Pl; Pb; Pr; Pp; Pt; Pl; Pp$ v
2409 // if (this.postfix === '$') {
2410 // this.copyRectagle(this.params);
2414 // CSI Pt ; Pl ; Pb ; Pr ' w
2416 // if (this.postfix === '\'') {
2417 // this.enableFilterRectangle(this.params);
2421 // CSI Ps x Request Terminal Parameters (DECREQTPARM).
2422 // CSI Ps x Select Attribute Change Extent (DECSACE).
2423 // CSI Pc; Pt; Pl; Pb; Pr$ x
2425 // if (this.postfix === '$') {
2426 // this.fillRectangle(this.params);
2428 // this.requestParameters(this.params);
2429 // //this.__(this.params);
2434 // CSI Pt; Pl; Pb; Pr$ z
2436 // if (this.postfix === '\'') {
2437 // this.enableLocatorReporting(this.params);
2438 // } else if (this.postfix === '$') {
2439 // this.eraseRectangle(this.params);
2444 // CSI Pt; Pl; Pb; Pr$ {
2446 // if (this.postfix === '\'') {
2447 // this.setLocatorEvents(this.params);
2448 // } else if (this.postfix === '$') {
2449 // this.selectiveEraseRectangle(this.params);
2455 // if (this.postfix === '\'') {
2456 // this.requestLocatorPosition(this.params);
2461 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
2463 // if (this.postfix === ' ') {
2464 // this.insertColumns(this.params);
2469 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
2471 // if (this.postfix === ' ') {
2472 // this.deleteColumns(this.params);
2477 this.error('Unknown CSI code: %s.', ch
);
2486 if (ch
=== '\x1b' || ch
=== '\x07') {
2487 if (ch
=== '\x1b') i
++;
2489 switch (this.prefix
) {
2490 // User-Defined Keys (DECUDK).
2494 // Request Status String (DECRQSS).
2495 // test: echo -e '\eP$q"p\e\\'
2497 var pt
= this.currentParam
2514 + (this.scrollTop
+ 1)
2516 + (this.scrollBottom
+ 1)
2526 this.error('Unknown DCS Pt: %s.', pt
);
2531 this.send('\x1bP' + +valid
+ '$r' + pt
+ '\x1b\\');
2534 // Set Termcap/Terminfo Data (xterm, experimental).
2538 // Request Termcap/Terminfo String (xterm, experimental)
2539 // Regular xterm does not even respond to this sequence.
2540 // This can cause a small glitch in vim.
2541 // test: echo -ne '\eP+q6b64\e\\'
2543 var pt
= this.currentParam
2546 this.send('\x1bP' + +valid
+ '+r' + pt
+ '\x1b\\');
2550 this.error('Unknown DCS prefix: %s.', this.prefix
);
2554 this.currentParam
= 0;
2556 this.state
= normal
;
2557 } else if (!this.currentParam
) {
2558 if (!this.prefix
&& ch
!== '$' && ch
!== '+') {
2559 this.currentParam
= ch
;
2560 } else if (this.prefix
.length
=== 2) {
2561 this.currentParam
= ch
;
2566 this.currentParam
+= ch
;
2572 if (ch
=== '\x1b' || ch
=== '\x07') {
2573 if (ch
=== '\x1b') i
++;
2574 this.state
= normal
;
2580 this.updateRange(this.y
);
2581 this.refresh(this.refreshStart
, this.refreshEnd
);
2585 * Writes text to the terminal, followed by a break line character (\n).
2586 * @param {string} text The text to write to the terminal.
2588 Terminal
.prototype.writeln = function(data
) {
2589 this.write(data
+ '\r\n');
2593 * Attaches a custom keydown handler which is run before keys are processed, giving consumers of
2594 * xterm.js ultimate control as to what keys should be processed by the terminal and what keys
2596 * @param {function} customKeydownHandler The custom KeyboardEvent handler to attach. This is a
2597 * function that takes a KeyboardEvent, allowing consumers to stop propogation and/or prevent
2598 * the default action. The function returns whether the event should be processed by xterm.js.
2600 Terminal
.prototype.attachCustomKeydownHandler = function(customKeydownHandler
) {
2601 this.customKeydownHandler
= customKeydownHandler
;
2605 * Handle a keydown event
2607 * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
2608 * @param {KeyboardEvent} ev The keydown event to be handled.
2610 Terminal
.prototype.keyDown = function(ev
) {
2611 if (this.customKeydownHandler
&& this.customKeydownHandler(ev
) === false) {
2615 if (!this.compositionHelper
.keydown
.bind(this.compositionHelper
)(ev
)) {
2620 var result
= this.evaluateKeyEscapeSequence(ev
);
2622 if (result
.scrollDisp
) {
2623 this.scrollDisp(result
.scrollDisp
);
2624 return this.cancel(ev
);
2627 if (isThirdLevelShift(this, ev
)) {
2631 if (result
.cancel
) {
2632 // The event is canceled at the end already, is this necessary?
2633 this.cancel(ev
, true);
2640 this.emit('keydown', ev
);
2641 this.emit('key', result
.key
, ev
);
2643 this.handler(result
.key
);
2645 return this.cancel(ev
, true);
2649 * Returns an object that determines how a KeyboardEvent should be handled. The key of the
2650 * returned value is the new key code to pass to the PTY.
2652 * Reference: http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
2653 * @param {KeyboardEvent} ev The keyboard event to be translated to key escape sequence.
2655 Terminal
.prototype.evaluateKeyEscapeSequence = function(ev
) {
2657 // Whether to cancel event propogation (NOTE: this may not be needed since the event is
2658 // canceled at the end of keyDown
2660 // The new key even to emit
2662 // The number of characters to scroll, if this is defined it will cancel the event
2663 scrollDisp
: undefined
2665 var modifiers
= ev
.shiftKey
<< 0 | ev
.altKey
<< 1 | ev
.ctrlKey
<< 2 | ev
.metaKey
<< 3;
2666 switch (ev
.keyCode
) {
2670 result
.key
= '\x08'; // ^H
2673 result
.key
= '\x7f'; // ^?
2678 result
.key
= '\x1b[Z';
2682 result
.cancel
= true;
2687 result
.cancel
= true;
2691 result
.key
= '\x1b';
2692 result
.cancel
= true;
2697 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'D';
2698 // HACK: Make Alt + left-arrow behave like Ctrl + left-arrow: move one word backwards
2699 // http://unix.stackexchange.com/a/108106
2700 if (result
.key
== '\x1b[1;3D') {
2701 result
.key
= '\x1b[1;5D';
2703 } else if (this.applicationCursor
) {
2704 result
.key
= '\x1bOD';
2706 result
.key
= '\x1b[D';
2712 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'C';
2713 // HACK: Make Alt + right-arrow behave like Ctrl + right-arrow: move one word forward
2714 // http://unix.stackexchange.com/a/108106
2715 if (result
.key
== '\x1b[1;3C') {
2716 result
.key
= '\x1b[1;5C';
2718 } else if (this.applicationCursor
) {
2719 result
.key
= '\x1bOC';
2721 result
.key
= '\x1b[C';
2727 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'A';
2728 // HACK: Make Alt + up-arrow behave like Ctrl + up-arrow
2729 // http://unix.stackexchange.com/a/108106
2730 if (result
.key
== '\x1b[1;3A') {
2731 result
.key
= '\x1b[1;5A';
2733 } else if (this.applicationCursor
) {
2734 result
.key
= '\x1bOA';
2736 result
.key
= '\x1b[A';
2742 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'B';
2743 // HACK: Make Alt + down-arrow behave like Ctrl + down-arrow
2744 // http://unix.stackexchange.com/a/108106
2745 if (result
.key
== '\x1b[1;3B') {
2746 result
.key
= '\x1b[1;5B';
2748 } else if (this.applicationCursor
) {
2749 result
.key
= '\x1bOB';
2751 result
.key
= '\x1b[B';
2756 if (!ev
.shiftKey
&& !ev
.ctrlKey
) {
2757 // <Ctrl> or <Shift> + <Insert> are used to
2758 // copy-paste on some systems.
2759 result
.key
= '\x1b[2~';
2763 case 46: result
.key
= '\x1b[3~'; break;
2767 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'H';
2768 else if (this.applicationCursor
)
2769 result
.key
= '\x1bOH';
2771 result
.key
= '\x1b[H';
2776 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'F';
2777 else if (this.applicationCursor
)
2778 result
.key
= '\x1bOF';
2780 result
.key
= '\x1b[F';
2785 result
.scrollDisp
= -(this.rows
- 1);
2787 result
.key
= '\x1b[5~';
2793 result
.scrollDisp
= this.rows
- 1;
2795 result
.key
= '\x1b[6~';
2799 case 112: result
.key
= '\x1bOP'; break;
2800 case 113: result
.key
= '\x1bOQ'; break;
2801 case 114: result
.key
= '\x1bOR'; break;
2802 case 115: result
.key
= '\x1bOS'; break;
2803 case 116: result
.key
= '\x1b[15~'; break;
2804 case 117: result
.key
= '\x1b[17~'; break;
2805 case 118: result
.key
= '\x1b[18~'; break;
2806 case 119: result
.key
= '\x1b[19~'; break;
2807 case 120: result
.key
= '\x1b[20~'; break;
2808 case 121: result
.key
= '\x1b[21~'; break;
2809 case 122: result
.key
= '\x1b[23~'; break;
2810 case 123: result
.key
= '\x1b[24~'; break;
2813 if (ev
.ctrlKey
&& !ev
.shiftKey
&& !ev
.altKey
&& !ev
.metaKey
) {
2814 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2815 result
.key
= String
.fromCharCode(ev
.keyCode
- 64);
2816 } else if (ev
.keyCode
=== 32) {
2818 result
.key
= String
.fromCharCode(0);
2819 } else if (ev
.keyCode
>= 51 && ev
.keyCode
<= 55) {
2820 // escape, file sep, group sep, record sep, unit sep
2821 result
.key
= String
.fromCharCode(ev
.keyCode
- 51 + 27);
2822 } else if (ev
.keyCode
=== 56) {
2824 result
.key
= String
.fromCharCode(127);
2825 } else if (ev
.keyCode
=== 219) {
2827 result
.key
= String
.fromCharCode(27);
2828 } else if (ev
.keyCode
=== 221) {
2830 result
.key
= String
.fromCharCode(29);
2832 } else if (!this.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) {
2833 // On Mac this is a third level shift. Use <Esc> instead.
2834 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2835 result
.key
= '\x1b' + String
.fromCharCode(ev
.keyCode
+ 32);
2836 } else if (ev
.keyCode
=== 192) {
2837 result
.key
= '\x1b`';
2838 } else if (ev
.keyCode
>= 48 && ev
.keyCode
<= 57) {
2839 result
.key
= '\x1b' + (ev
.keyCode
- 48);
2848 * Set the G level of the terminal
2851 Terminal
.prototype.setgLevel = function(g
) {
2853 this.charset
= this.charsets
[g
];
2857 * Set the charset for the given G level of the terminal
2861 Terminal
.prototype.setgCharset = function(g
, charset
) {
2862 this.charsets
[g
] = charset
;
2863 if (this.glevel
=== g
) {
2864 this.charset
= charset
;
2869 * Handle a keypress event.
2871 * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
2872 * @param {KeyboardEvent} ev The keypress event to be handled.
2874 Terminal
.prototype.keyPress = function(ev
) {
2881 } else if (ev
.which
== null) {
2883 } else if (ev
.which
!== 0 && ev
.charCode
!== 0) {
2890 (ev
.altKey
|| ev
.ctrlKey
|| ev
.metaKey
) && !isThirdLevelShift(this, ev
)
2895 key
= String
.fromCharCode(key
);
2897 this.emit('keypress', key
, ev
);
2898 this.emit('key', key
, ev
);
2906 * Send data for handling to the terminal
2907 * @param {string} data
2909 Terminal
.prototype.send = function(data
) {
2913 setTimeout(function() {
2914 self
.handler(self
.queue
);
2924 * Note: We could do sweet things with webaudio here
2926 Terminal
.prototype.bell = function() {
2927 if (!this.visualBell
) return;
2929 this.element
.style
.borderColor
= 'white';
2930 setTimeout(function() {
2931 self
.element
.style
.borderColor
= '';
2933 if (this.popOnBell
) this.focus();
2937 * Log the current state to the console.
2939 Terminal
.prototype.log = function() {
2940 if (!this.debug
) return;
2941 if (!this.context
.console
|| !this.context
.console
.log
) return;
2942 var args
= Array
.prototype.slice
.call(arguments
);
2943 this.context
.console
.log
.apply(this.context
.console
, args
);
2947 * Log the current state as error to the console.
2949 Terminal
.prototype.error = function() {
2950 if (!this.debug
) return;
2951 if (!this.context
.console
|| !this.context
.console
.error
) return;
2952 var args
= Array
.prototype.slice
.call(arguments
);
2953 this.context
.console
.error
.apply(this.context
.console
, args
);
2957 * Resizes the terminal.
2959 * @param {number} x The number of columns to resize to.
2960 * @param {number} y The number of rows to resize to.
2962 Terminal
.prototype.resize = function(x
, y
) {
2970 if (x
=== this.cols
&& y
=== this.rows
) {
2980 ch
= [this.defAttr
, ' ', 1]; // does xterm use the default attr?
2981 i
= this.lines
.length
;
2983 while (this.lines
[i
].length
< x
) {
2984 this.lines
[i
].push(ch
);
2988 i
= this.lines
.length
;
2990 while (this.lines
[i
].length
> x
) {
2991 this.lines
[i
].pop();
3004 // y is rows, not this.y
3005 if (this.lines
.length
< y
+ this.ybase
) {
3006 if (this.ybase
> 0 && this.lines
.length
<= this.ybase
+ this.y
+ addToY
+ 1) {
3007 // There is room above the buffer and there are no empty elements below the line,
3011 if (this.ydisp
> 0) {
3012 // Viewport is at the top of the buffer, must increase downwards
3016 // Add a blank line if there is no buffer left at the top to scroll to, or if there
3017 // are blank lines after the cursor
3018 this.lines
.push(this.blankLine());
3021 if (this.children
.length
< y
) {
3027 if (this.lines
.length
> y
+ this.ybase
) {
3028 if (this.lines
.length
> this.ybase
+ this.y
+ 1) {
3029 // The line is a blank line below the cursor, remove it
3032 // The line is the cursor, scroll down
3037 if (this.children
.length
> y
) {
3038 el
= this.children
.shift();
3040 el
.parentNode
.removeChild(el
);
3047 * Make sure that the cursor stays on screen
3061 this.scrollBottom
= y
- 1;
3063 this.refresh(0, this.rows
- 1);
3067 this.emit('resize', {terminal
: this, cols
: x
, rows
: y
});
3071 * Updates the range of rows to refresh
3072 * @param {number} y The number of rows to refresh next.
3074 Terminal
.prototype.updateRange = function(y
) {
3075 if (y
< this.refreshStart
) this.refreshStart
= y
;
3076 if (y
> this.refreshEnd
) this.refreshEnd
= y
;
3077 // if (y > this.refreshEnd) {
3078 // this.refreshEnd = y;
3079 // if (y > this.rows - 1) {
3080 // this.refreshEnd = this.rows - 1;
3086 * Set the range of refreshing to the maximyum value
3088 Terminal
.prototype.maxRange = function() {
3089 this.refreshStart
= 0;
3090 this.refreshEnd
= this.rows
- 1;
3096 * Setup the tab stops.
3099 Terminal
.prototype.setupStops = function(i
) {
3101 if (!this.tabs
[i
]) {
3102 i
= this.prevStop(i
);
3109 for (; i
< this.cols
; i
+= 8) {
3110 this.tabs
[i
] = true;
3116 * Move the cursor to the previous tab stop from the given position (default is current).
3117 * @param {number} x The position to move the cursor to the previous tab stop.
3119 Terminal
.prototype.prevStop = function(x
) {
3120 if (x
== null) x
= this.x
;
3121 while (!this.tabs
[--x
] && x
> 0);
3122 return x
>= this.cols
3129 * Move the cursor one tab stop forward from the given position (default is current).
3130 * @param {number} x The position to move the cursor one tab stop forward.
3132 Terminal
.prototype.nextStop = function(x
) {
3133 if (x
== null) x
= this.x
;
3134 while (!this.tabs
[++x
] && x
< this.cols
);
3135 return x
>= this.cols
3142 * Erase in the identified line everything from "x" to the end of the line (right).
3143 * @param {number} x The column from which to start erasing to the end of the line.
3144 * @param {number} y The line in which to operate.
3146 Terminal
.prototype.eraseRight = function(x
, y
) {
3147 var line
= this.lines
[this.ybase
+ y
]
3148 , ch
= [this.eraseAttr(), ' ', 1]; // xterm
3151 for (; x
< this.cols
; x
++) {
3155 this.updateRange(y
);
3161 * Erase in the identified line everything from "x" to the start of the line (left).
3162 * @param {number} x The column from which to start erasing to the start of the line.
3163 * @param {number} y The line in which to operate.
3165 Terminal
.prototype.eraseLeft = function(x
, y
) {
3166 var line
= this.lines
[this.ybase
+ y
]
3167 , ch
= [this.eraseAttr(), ' ', 1]; // xterm
3170 while (x
--) line
[x
] = ch
;
3172 this.updateRange(y
);
3177 * Erase all content in the given line
3178 * @param {number} y The line to erase all of its contents.
3180 Terminal
.prototype.eraseLine = function(y
) {
3181 this.eraseRight(0, y
);
3186 * Return the data array of a blank line/
3187 * @param {number} cur First bunch of data for each "blank" character.
3189 Terminal
.prototype.blankLine = function(cur
) {
3194 var ch
= [attr
, ' ', 1] // width defaults to 1 halfwidth character
3198 for (; i
< this.cols
; i
++) {
3207 * If cur return the back color xterm feature attribute. Else return defAttr.
3208 * @param {object} cur
3210 Terminal
.prototype.ch = function(cur
) {
3212 ? [this.eraseAttr(), ' ', 1]
3213 : [this.defAttr
, ' ', 1];
3218 * Evaluate if the current erminal is the given argument.
3219 * @param {object} term The terminal to evaluate
3221 Terminal
.prototype.is = function(term
) {
3222 var name
= this.termName
;
3223 return (name
+ '').indexOf(term
) === 0;
3228 * Emit the 'data' event and populate the given data.
3229 * @param {string} data The data to populate in the event.
3231 Terminal
.prototype.handler = function(data
) {
3232 this.emit('data', data
);
3237 * Emit the 'title' event and populate the given title.
3238 * @param {string} title The title to populate in the event.
3240 Terminal
.prototype.handleTitle = function(title
) {
3241 this.emit('title', title
);
3250 * ESC D Index (IND is 0x84).
3252 Terminal
.prototype.index = function() {
3254 if (this.y
> this.scrollBottom
) {
3258 this.state
= normal
;
3263 * ESC M Reverse Index (RI is 0x8d).
3265 Terminal
.prototype.reverseIndex = function() {
3268 if (this.y
< this.scrollTop
) {
3270 // possibly move the code below to term.reverseScroll();
3271 // test: echo -ne '\e[1;1H\e[44m\eM\e[0m'
3272 // blankLine(true) is xterm/linux behavior
3273 this.lines
.splice(this.y
+ this.ybase
, 0, this.blankLine(true));
3274 j
= this.rows
- 1 - this.scrollBottom
;
3275 this.lines
.splice(this.rows
- 1 + this.ybase
- j
+ 1, 1);
3277 this.updateRange(this.scrollTop
);
3278 this.updateRange(this.scrollBottom
);
3280 this.state
= normal
;
3285 * ESC c Full Reset (RIS).
3287 Terminal
.prototype.reset = function() {
3288 this.options
.rows
= this.rows
;
3289 this.options
.cols
= this.cols
;
3290 var customKeydownHandler
= this.customKeydownHandler
;
3291 Terminal
.call(this, this.options
);
3292 this.customKeydownHandler
= customKeydownHandler
;
3293 this.refresh(0, this.rows
- 1);
3298 * ESC H Tab Set (HTS is 0x88).
3300 Terminal
.prototype.tabSet = function() {
3301 this.tabs
[this.x
] = true;
3302 this.state
= normal
;
3312 * Cursor Up Ps Times (default = 1) (CUU).
3314 Terminal
.prototype.cursorUp = function(params
) {
3315 var param
= params
[0];
3316 if (param
< 1) param
= 1;
3318 if (this.y
< 0) this.y
= 0;
3324 * Cursor Down Ps Times (default = 1) (CUD).
3326 Terminal
.prototype.cursorDown = function(params
) {
3327 var param
= params
[0];
3328 if (param
< 1) param
= 1;
3330 if (this.y
>= this.rows
) {
3331 this.y
= this.rows
- 1;
3338 * Cursor Forward Ps Times (default = 1) (CUF).
3340 Terminal
.prototype.cursorForward = function(params
) {
3341 var param
= params
[0];
3342 if (param
< 1) param
= 1;
3344 if (this.x
>= this.cols
) {
3345 this.x
= this.cols
- 1;
3352 * Cursor Backward Ps Times (default = 1) (CUB).
3354 Terminal
.prototype.cursorBackward = function(params
) {
3355 var param
= params
[0];
3356 if (param
< 1) param
= 1;
3358 if (this.x
< 0) this.x
= 0;
3364 * Cursor Position [row;column] (default = [1,1]) (CUP).
3366 Terminal
.prototype.cursorPos = function(params
) {
3369 row
= params
[0] - 1;
3371 if (params
.length
>= 2) {
3372 col
= params
[1] - 1;
3379 } else if (row
>= this.rows
) {
3380 row
= this.rows
- 1;
3385 } else if (col
>= this.cols
) {
3386 col
= this.cols
- 1;
3395 * CSI Ps J Erase in Display (ED).
3396 * Ps = 0 -> Erase Below (default).
3397 * Ps = 1 -> Erase Above.
3398 * Ps = 2 -> Erase All.
3399 * Ps = 3 -> Erase Saved Lines (xterm).
3401 * Erase in Display (DECSED).
3402 * Ps = 0 -> Selective Erase Below (default).
3403 * Ps = 1 -> Selective Erase Above.
3404 * Ps = 2 -> Selective Erase All.
3406 Terminal
.prototype.eraseInDisplay = function(params
) {
3408 switch (params
[0]) {
3410 this.eraseRight(this.x
, this.y
);
3412 for (; j
< this.rows
; j
++) {
3417 this.eraseLeft(this.x
, this.y
);
3425 while (j
--) this.eraseLine(j
);
3435 * CSI Ps K Erase in Line (EL).
3436 * Ps = 0 -> Erase to Right (default).
3437 * Ps = 1 -> Erase to Left.
3438 * Ps = 2 -> Erase All.
3440 * Erase in Line (DECSEL).
3441 * Ps = 0 -> Selective Erase to Right (default).
3442 * Ps = 1 -> Selective Erase to Left.
3443 * Ps = 2 -> Selective Erase All.
3445 Terminal
.prototype.eraseInLine = function(params
) {
3446 switch (params
[0]) {
3448 this.eraseRight(this.x
, this.y
);
3451 this.eraseLeft(this.x
, this.y
);
3454 this.eraseLine(this.y
);
3461 * CSI Pm m Character Attributes (SGR).
3462 * Ps = 0 -> Normal (default).
3464 * Ps = 4 -> Underlined.
3465 * Ps = 5 -> Blink (appears as Bold).
3466 * Ps = 7 -> Inverse.
3467 * Ps = 8 -> Invisible, i.e., hidden (VT300).
3468 * Ps = 2 2 -> Normal (neither bold nor faint).
3469 * Ps = 2 4 -> Not underlined.
3470 * Ps = 2 5 -> Steady (not blinking).
3471 * Ps = 2 7 -> Positive (not inverse).
3472 * Ps = 2 8 -> Visible, i.e., not hidden (VT300).
3473 * Ps = 3 0 -> Set foreground color to Black.
3474 * Ps = 3 1 -> Set foreground color to Red.
3475 * Ps = 3 2 -> Set foreground color to Green.
3476 * Ps = 3 3 -> Set foreground color to Yellow.
3477 * Ps = 3 4 -> Set foreground color to Blue.
3478 * Ps = 3 5 -> Set foreground color to Magenta.
3479 * Ps = 3 6 -> Set foreground color to Cyan.
3480 * Ps = 3 7 -> Set foreground color to White.
3481 * Ps = 3 9 -> Set foreground color to default (original).
3482 * Ps = 4 0 -> Set background color to Black.
3483 * Ps = 4 1 -> Set background color to Red.
3484 * Ps = 4 2 -> Set background color to Green.
3485 * Ps = 4 3 -> Set background color to Yellow.
3486 * Ps = 4 4 -> Set background color to Blue.
3487 * Ps = 4 5 -> Set background color to Magenta.
3488 * Ps = 4 6 -> Set background color to Cyan.
3489 * Ps = 4 7 -> Set background color to White.
3490 * Ps = 4 9 -> Set background color to default (original).
3492 * If 16-color support is compiled, the following apply. Assume
3493 * that xterm's resources are set so that the ISO color codes are
3494 * the first 8 of a set of 16. Then the aixterm colors are the
3495 * bright versions of the ISO colors:
3496 * Ps = 9 0 -> Set foreground color to Black.
3497 * Ps = 9 1 -> Set foreground color to Red.
3498 * Ps = 9 2 -> Set foreground color to Green.
3499 * Ps = 9 3 -> Set foreground color to Yellow.
3500 * Ps = 9 4 -> Set foreground color to Blue.
3501 * Ps = 9 5 -> Set foreground color to Magenta.
3502 * Ps = 9 6 -> Set foreground color to Cyan.
3503 * Ps = 9 7 -> Set foreground color to White.
3504 * Ps = 1 0 0 -> Set background color to Black.
3505 * Ps = 1 0 1 -> Set background color to Red.
3506 * Ps = 1 0 2 -> Set background color to Green.
3507 * Ps = 1 0 3 -> Set background color to Yellow.
3508 * Ps = 1 0 4 -> Set background color to Blue.
3509 * Ps = 1 0 5 -> Set background color to Magenta.
3510 * Ps = 1 0 6 -> Set background color to Cyan.
3511 * Ps = 1 0 7 -> Set background color to White.
3513 * If xterm is compiled with the 16-color support disabled, it
3514 * supports the following, from rxvt:
3515 * Ps = 1 0 0 -> Set foreground and background color to
3518 * If 88- or 256-color support is compiled, the following apply.
3519 * Ps = 3 8 ; 5 ; Ps -> Set foreground color to the second
3521 * Ps = 4 8 ; 5 ; Ps -> Set background color to the second
3524 Terminal
.prototype.charAttributes = function(params
) {
3525 // Optimize a single SGR0.
3526 if (params
.length
=== 1 && params
[0] === 0) {
3527 this.curAttr
= this.defAttr
;
3531 var l
= params
.length
3533 , flags
= this.curAttr
>> 18
3534 , fg
= (this.curAttr
>> 9) & 0x1ff
3535 , bg
= this.curAttr
& 0x1ff
3538 for (; i
< l
; i
++) {
3540 if (p
>= 30 && p
<= 37) {
3543 } else if (p
>= 40 && p
<= 47) {
3546 } else if (p
>= 90 && p
<= 97) {
3550 } else if (p
>= 100 && p
<= 107) {
3554 } else if (p
=== 0) {
3556 flags
= this.defAttr
>> 18;
3557 fg
= (this.defAttr
>> 9) & 0x1ff;
3558 bg
= this.defAttr
& 0x1ff;
3562 } else if (p
=== 1) {
3565 } else if (p
=== 4) {
3568 } else if (p
=== 5) {
3571 } else if (p
=== 7) {
3572 // inverse and positive
3573 // test with: echo -e '\e[31m\e[42mhello\e[7mworld\e[27mhi\e[m'
3575 } else if (p
=== 8) {
3578 } else if (p
=== 22) {
3581 } else if (p
=== 24) {
3584 } else if (p
=== 25) {
3587 } else if (p
=== 27) {
3590 } else if (p
=== 28) {
3593 } else if (p
=== 39) {
3595 fg
= (this.defAttr
>> 9) & 0x1ff;
3596 } else if (p
=== 49) {
3598 bg
= this.defAttr
& 0x1ff;
3599 } else if (p
=== 38) {
3601 if (params
[i
+ 1] === 2) {
3605 params
[i
+ 1] & 0xff,
3606 params
[i
+ 2] & 0xff);
3607 if (fg
=== -1) fg
= 0x1ff;
3609 } else if (params
[i
+ 1] === 5) {
3611 p
= params
[i
] & 0xff;
3614 } else if (p
=== 48) {
3616 if (params
[i
+ 1] === 2) {
3620 params
[i
+ 1] & 0xff,
3621 params
[i
+ 2] & 0xff);
3622 if (bg
=== -1) bg
= 0x1ff;
3624 } else if (params
[i
+ 1] === 5) {
3626 p
= params
[i
] & 0xff;
3629 } else if (p
=== 100) {
3631 fg
= (this.defAttr
>> 9) & 0x1ff;
3632 bg
= this.defAttr
& 0x1ff;
3634 this.error('Unknown SGR attribute: %d.', p
);
3638 this.curAttr
= (flags
<< 18) | (fg
<< 9) | bg
;
3643 * CSI Ps n Device Status Report (DSR).
3644 * Ps = 5 -> Status Report. Result (``OK'') is
3646 * Ps = 6 -> Report Cursor Position (CPR) [row;column].
3650 * Device Status Report (DSR, DEC-specific).
3651 * Ps = 6 -> Report Cursor Position (CPR) [row;column] as CSI
3652 * ? r ; c R (assumes page is zero).
3653 * Ps = 1 5 -> Report Printer status as CSI ? 1 0 n (ready).
3654 * or CSI ? 1 1 n (not ready).
3655 * Ps = 2 5 -> Report UDK status as CSI ? 2 0 n (unlocked)
3656 * or CSI ? 2 1 n (locked).
3657 * Ps = 2 6 -> Report Keyboard status as
3658 * CSI ? 2 7 ; 1 ; 0 ; 0 n (North American).
3659 * The last two parameters apply to VT400 & up, and denote key-
3660 * board ready and LK01 respectively.
3661 * Ps = 5 3 -> Report Locator status as
3662 * CSI ? 5 3 n Locator available, if compiled-in, or
3663 * CSI ? 5 0 n No Locator, if not.
3665 Terminal
.prototype.deviceStatus = function(params
) {
3667 switch (params
[0]) {
3670 this.send('\x1b[0n');
3681 } else if (this.prefix
=== '?') {
3682 // modern xterm doesnt seem to
3683 // respond to any of these except ?6, 6, and 5
3684 switch (params
[0]) {
3695 // this.send('\x1b[?11n');
3698 // dont support user defined keys
3699 // this.send('\x1b[?21n');
3702 // north american keyboard
3703 // this.send('\x1b[?27;1;0;0n');
3706 // no dec locator/mouse
3707 // this.send('\x1b[?50n');
3720 * Insert Ps (Blank) Character(s) (default = 1) (ICH).
3722 Terminal
.prototype.insertChars = function(params
) {
3723 var param
, row
, j
, ch
;
3726 if (param
< 1) param
= 1;
3728 row
= this.y
+ this.ybase
;
3730 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3732 while (param
-- && j
< this.cols
) {
3733 this.lines
[row
].splice(j
++, 0, ch
);
3734 this.lines
[row
].pop();
3740 * Cursor Next Line Ps Times (default = 1) (CNL).
3741 * same as CSI Ps B ?
3743 Terminal
.prototype.cursorNextLine = function(params
) {
3744 var param
= params
[0];
3745 if (param
< 1) param
= 1;
3747 if (this.y
>= this.rows
) {
3748 this.y
= this.rows
- 1;
3756 * Cursor Preceding Line Ps Times (default = 1) (CNL).
3759 Terminal
.prototype.cursorPrecedingLine = function(params
) {
3760 var param
= params
[0];
3761 if (param
< 1) param
= 1;
3763 if (this.y
< 0) this.y
= 0;
3770 * Cursor Character Absolute [column] (default = [row,1]) (CHA).
3772 Terminal
.prototype.cursorCharAbsolute = function(params
) {
3773 var param
= params
[0];
3774 if (param
< 1) param
= 1;
3781 * Insert Ps Line(s) (default = 1) (IL).
3783 Terminal
.prototype.insertLines = function(params
) {
3787 if (param
< 1) param
= 1;
3788 row
= this.y
+ this.ybase
;
3790 j
= this.rows
- 1 - this.scrollBottom
;
3791 j
= this.rows
- 1 + this.ybase
- j
+ 1;
3794 // test: echo -e '\e[44m\e[1L\e[0m'
3795 // blankLine(true) - xterm/linux behavior
3796 this.lines
.splice(row
, 0, this.blankLine(true));
3797 this.lines
.splice(j
, 1);
3801 this.updateRange(this.y
);
3802 this.updateRange(this.scrollBottom
);
3808 * Delete Ps Line(s) (default = 1) (DL).
3810 Terminal
.prototype.deleteLines = function(params
) {
3814 if (param
< 1) param
= 1;
3815 row
= this.y
+ this.ybase
;
3817 j
= this.rows
- 1 - this.scrollBottom
;
3818 j
= this.rows
- 1 + this.ybase
- j
;
3821 // test: echo -e '\e[44m\e[1M\e[0m'
3822 // blankLine(true) - xterm/linux behavior
3823 this.lines
.splice(j
+ 1, 0, this.blankLine(true));
3824 this.lines
.splice(row
, 1);
3828 this.updateRange(this.y
);
3829 this.updateRange(this.scrollBottom
);
3835 * Delete Ps Character(s) (default = 1) (DCH).
3837 Terminal
.prototype.deleteChars = function(params
) {
3841 if (param
< 1) param
= 1;
3843 row
= this.y
+ this.ybase
;
3844 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3847 this.lines
[row
].splice(this.x
, 1);
3848 this.lines
[row
].push(ch
);
3854 * Erase Ps Character(s) (default = 1) (ECH).
3856 Terminal
.prototype.eraseChars = function(params
) {
3857 var param
, row
, j
, ch
;
3860 if (param
< 1) param
= 1;
3862 row
= this.y
+ this.ybase
;
3864 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3866 while (param
-- && j
< this.cols
) {
3867 this.lines
[row
][j
++] = ch
;
3872 * CSI Pm ` Character Position Absolute
3873 * [column] (default = [row,1]) (HPA).
3875 Terminal
.prototype.charPosAbsolute = function(params
) {
3876 var param
= params
[0];
3877 if (param
< 1) param
= 1;
3879 if (this.x
>= this.cols
) {
3880 this.x
= this.cols
- 1;
3887 * Horizontal Position Relative
3890 Terminal
.prototype.HPositionRelative = function(params
) {
3891 var param
= params
[0];
3892 if (param
< 1) param
= 1;
3894 if (this.x
>= this.cols
) {
3895 this.x
= this.cols
- 1;
3901 * CSI Ps c Send Device Attributes (Primary DA).
3902 * Ps = 0 or omitted -> request attributes from terminal. The
3903 * response depends on the decTerminalID resource setting.
3904 * -> CSI ? 1 ; 2 c (``VT100 with Advanced Video Option'')
3905 * -> CSI ? 1 ; 0 c (``VT101 with No Options'')
3906 * -> CSI ? 6 c (``VT102'')
3907 * -> CSI ? 6 0 ; 1 ; 2 ; 6 ; 8 ; 9 ; 1 5 ; c (``VT220'')
3908 * The VT100-style response parameters do not mean anything by
3909 * themselves. VT220 parameters do, telling the host what fea-
3910 * tures the terminal supports:
3911 * Ps = 1 -> 132-columns.
3912 * Ps = 2 -> Printer.
3913 * Ps = 6 -> Selective erase.
3914 * Ps = 8 -> User-defined keys.
3915 * Ps = 9 -> National replacement character sets.
3916 * Ps = 1 5 -> Technical characters.
3917 * Ps = 2 2 -> ANSI color, e.g., VT525.
3918 * Ps = 2 9 -> ANSI text locator (i.e., DEC Locator mode).
3920 * Send Device Attributes (Secondary DA).
3921 * Ps = 0 or omitted -> request the terminal's identification
3922 * code. The response depends on the decTerminalID resource set-
3923 * ting. It should apply only to VT220 and up, but xterm extends
3925 * -> CSI > Pp ; Pv ; Pc c
3926 * where Pp denotes the terminal type
3927 * Pp = 0 -> ``VT100''.
3928 * Pp = 1 -> ``VT220''.
3929 * and Pv is the firmware version (for xterm, this was originally
3930 * the XFree86 patch number, starting with 95). In a DEC termi-
3931 * nal, Pc indicates the ROM cartridge registration number and is
3934 * xterm/charproc.c - line 2012, for more information.
3935 * vim responds with ^[[?0c or ^[[?1c after the terminal's response (?)
3937 Terminal
.prototype.sendDeviceAttributes = function(params
) {
3938 if (params
[0] > 0) return;
3941 if (this.is('xterm')
3942 || this.is('rxvt-unicode')
3943 || this.is('screen')) {
3944 this.send('\x1b[?1;2c');
3945 } else if (this.is('linux')) {
3946 this.send('\x1b[?6c');
3948 } else if (this.prefix
=== '>') {
3950 // seem to spit this
3951 // out around ~370 times (?).
3952 if (this.is('xterm')) {
3953 this.send('\x1b[>0;276;0c');
3954 } else if (this.is('rxvt-unicode')) {
3955 this.send('\x1b[>85;95;0c');
3956 } else if (this.is('linux')) {
3957 // not supported by linux console.
3958 // linux console echoes parameters.
3959 this.send(params
[0] + 'c');
3960 } else if (this.is('screen')) {
3961 this.send('\x1b[>83;40003;0c');
3969 * Line Position Absolute [row] (default = [1,column]) (VPA).
3971 Terminal
.prototype.linePosAbsolute = function(params
) {
3972 var param
= params
[0];
3973 if (param
< 1) param
= 1;
3975 if (this.y
>= this.rows
) {
3976 this.y
= this.rows
- 1;
3982 * 145 65 e * VPR - Vertical Position Relative
3985 Terminal
.prototype.VPositionRelative = function(params
) {
3986 var param
= params
[0];
3987 if (param
< 1) param
= 1;
3989 if (this.y
>= this.rows
) {
3990 this.y
= this.rows
- 1;
3997 * Horizontal and Vertical Position [row;column] (default =
4000 Terminal
.prototype.HVPosition = function(params
) {
4001 if (params
[0] < 1) params
[0] = 1;
4002 if (params
[1] < 1) params
[1] = 1;
4004 this.y
= params
[0] - 1;
4005 if (this.y
>= this.rows
) {
4006 this.y
= this.rows
- 1;
4009 this.x
= params
[1] - 1;
4010 if (this.x
>= this.cols
) {
4011 this.x
= this.cols
- 1;
4017 * CSI Pm h Set Mode (SM).
4018 * Ps = 2 -> Keyboard Action Mode (AM).
4019 * Ps = 4 -> Insert Mode (IRM).
4020 * Ps = 1 2 -> Send/receive (SRM).
4021 * Ps = 2 0 -> Automatic Newline (LNM).
4023 * DEC Private Mode Set (DECSET).
4024 * Ps = 1 -> Application Cursor Keys (DECCKM).
4025 * Ps = 2 -> Designate USASCII for character sets G0-G3
4026 * (DECANM), and set VT100 mode.
4027 * Ps = 3 -> 132 Column Mode (DECCOLM).
4028 * Ps = 4 -> Smooth (Slow) Scroll (DECSCLM).
4029 * Ps = 5 -> Reverse Video (DECSCNM).
4030 * Ps = 6 -> Origin Mode (DECOM).
4031 * Ps = 7 -> Wraparound Mode (DECAWM).
4032 * Ps = 8 -> Auto-repeat Keys (DECARM).
4033 * Ps = 9 -> Send Mouse X & Y on button press. See the sec-
4034 * tion Mouse Tracking.
4035 * Ps = 1 0 -> Show toolbar (rxvt).
4036 * Ps = 1 2 -> Start Blinking Cursor (att610).
4037 * Ps = 1 8 -> Print form feed (DECPFF).
4038 * Ps = 1 9 -> Set print extent to full screen (DECPEX).
4039 * Ps = 2 5 -> Show Cursor (DECTCEM).
4040 * Ps = 3 0 -> Show scrollbar (rxvt).
4041 * Ps = 3 5 -> Enable font-shifting functions (rxvt).
4042 * Ps = 3 8 -> Enter Tektronix Mode (DECTEK).
4043 * Ps = 4 0 -> Allow 80 -> 132 Mode.
4044 * Ps = 4 1 -> more(1) fix (see curses resource).
4045 * Ps = 4 2 -> Enable Nation Replacement Character sets (DECN-
4047 * Ps = 4 4 -> Turn On Margin Bell.
4048 * Ps = 4 5 -> Reverse-wraparound Mode.
4049 * Ps = 4 6 -> Start Logging. This is normally disabled by a
4050 * compile-time option.
4051 * Ps = 4 7 -> Use Alternate Screen Buffer. (This may be dis-
4052 * abled by the titeInhibit resource).
4053 * Ps = 6 6 -> Application keypad (DECNKM).
4054 * Ps = 6 7 -> Backarrow key sends backspace (DECBKM).
4055 * Ps = 1 0 0 0 -> Send Mouse X & Y on button press and
4056 * release. See the section Mouse Tracking.
4057 * Ps = 1 0 0 1 -> Use Hilite Mouse Tracking.
4058 * Ps = 1 0 0 2 -> Use Cell Motion Mouse Tracking.
4059 * Ps = 1 0 0 3 -> Use All Motion Mouse Tracking.
4060 * Ps = 1 0 0 4 -> Send FocusIn/FocusOut events.
4061 * Ps = 1 0 0 5 -> Enable Extended Mouse Mode.
4062 * Ps = 1 0 1 0 -> Scroll to bottom on tty output (rxvt).
4063 * Ps = 1 0 1 1 -> Scroll to bottom on key press (rxvt).
4064 * Ps = 1 0 3 4 -> Interpret "meta" key, sets eighth bit.
4065 * (enables the eightBitInput resource).
4066 * Ps = 1 0 3 5 -> Enable special modifiers for Alt and Num-
4067 * Lock keys. (This enables the numLock resource).
4068 * Ps = 1 0 3 6 -> Send ESC when Meta modifies a key. (This
4069 * enables the metaSendsEscape resource).
4070 * Ps = 1 0 3 7 -> Send DEL from the editing-keypad Delete
4072 * Ps = 1 0 3 9 -> Send ESC when Alt modifies a key. (This
4073 * enables the altSendsEscape resource).
4074 * Ps = 1 0 4 0 -> Keep selection even if not highlighted.
4075 * (This enables the keepSelection resource).
4076 * Ps = 1 0 4 1 -> Use the CLIPBOARD selection. (This enables
4077 * the selectToClipboard resource).
4078 * Ps = 1 0 4 2 -> Enable Urgency window manager hint when
4079 * Control-G is received. (This enables the bellIsUrgent
4081 * Ps = 1 0 4 3 -> Enable raising of the window when Control-G
4082 * is received. (enables the popOnBell resource).
4083 * Ps = 1 0 4 7 -> Use Alternate Screen Buffer. (This may be
4084 * disabled by the titeInhibit resource).
4085 * Ps = 1 0 4 8 -> Save cursor as in DECSC. (This may be dis-
4086 * abled by the titeInhibit resource).
4087 * Ps = 1 0 4 9 -> Save cursor as in DECSC and use Alternate
4088 * Screen Buffer, clearing it first. (This may be disabled by
4089 * the titeInhibit resource). This combines the effects of the 1
4090 * 0 4 7 and 1 0 4 8 modes. Use this with terminfo-based
4091 * applications rather than the 4 7 mode.
4092 * Ps = 1 0 5 0 -> Set terminfo/termcap function-key mode.
4093 * Ps = 1 0 5 1 -> Set Sun function-key mode.
4094 * Ps = 1 0 5 2 -> Set HP function-key mode.
4095 * Ps = 1 0 5 3 -> Set SCO function-key mode.
4096 * Ps = 1 0 6 0 -> Set legacy keyboard emulation (X11R6).
4097 * Ps = 1 0 6 1 -> Set VT220 keyboard emulation.
4098 * Ps = 2 0 0 4 -> Set bracketed paste mode.
4100 * http: *vt100.net/docs/vt220-rm/chapter4.html
4102 Terminal
.prototype.setMode = function(params
) {
4103 if (typeof params
=== 'object') {
4104 var l
= params
.length
4107 for (; i
< l
; i
++) {
4108 this.setMode(params
[i
]);
4117 this.insertMode
= true;
4120 //this.convertEol = true;
4123 } else if (this.prefix
=== '?') {
4126 this.applicationCursor
= true;
4129 this.setgCharset(0, Terminal
.charsets
.US
);
4130 this.setgCharset(1, Terminal
.charsets
.US
);
4131 this.setgCharset(2, Terminal
.charsets
.US
);
4132 this.setgCharset(3, Terminal
.charsets
.US
);
4133 // set VT100 mode here
4135 case 3: // 132 col mode
4136 this.savedCols
= this.cols
;
4137 this.resize(132, this.rows
);
4140 this.originMode
= true;
4143 this.wraparoundMode
= true;
4146 // this.cursorBlink = true;
4149 this.log('Serial port requested application keypad.');
4150 this.applicationKeypad
= true;
4151 this.viewport
.setApplicationMode(true);
4153 case 9: // X10 Mouse
4154 // no release, no motion, no wheel, no modifiers.
4155 case 1000: // vt200 mouse
4157 // no modifiers, except control on the wheel.
4158 case 1002: // button event mouse
4159 case 1003: // any event mouse
4160 // any event - sends motion events,
4161 // even if there is no button held down.
4162 this.x10Mouse
= params
=== 9;
4163 this.vt200Mouse
= params
=== 1000;
4164 this.normalMouse
= params
> 1000;
4165 this.mouseEvents
= true;
4166 this.element
.style
.cursor
= 'default';
4167 this.log('Binding to mouse events.');
4169 case 1004: // send focusin/focusout events
4172 this.sendFocus
= true;
4174 case 1005: // utf8 ext mode mouse
4175 this.utfMouse
= true;
4176 // for wide terminals
4177 // simply encodes large values as utf8 characters
4179 case 1006: // sgr ext mode mouse
4180 this.sgrMouse
= true;
4181 // for wide terminals
4182 // does not add 32 to fields
4183 // press: ^[[<b;x;yM
4184 // release: ^[[<b;x;ym
4186 case 1015: // urxvt ext mode mouse
4187 this.urxvtMouse
= true;
4188 // for wide terminals
4189 // numbers for fields
4191 // motion: ^[[b;x;yT
4193 case 25: // show cursor
4194 this.cursorHidden
= false;
4196 case 1049: // alt screen buffer cursor
4197 //this.saveCursor();
4199 case 47: // alt screen buffer
4200 case 1047: // alt screen buffer
4208 scrollTop
: this.scrollTop
,
4209 scrollBottom
: this.scrollBottom
,
4211 // XXX save charset(s) here?
4212 // charset: this.charset,
4213 // glevel: this.glevel,
4214 // charsets: this.charsets
4217 this.normal
= normal
;
4226 * CSI Pm l Reset Mode (RM).
4227 * Ps = 2 -> Keyboard Action Mode (AM).
4228 * Ps = 4 -> Replace Mode (IRM).
4229 * Ps = 1 2 -> Send/receive (SRM).
4230 * Ps = 2 0 -> Normal Linefeed (LNM).
4232 * DEC Private Mode Reset (DECRST).
4233 * Ps = 1 -> Normal Cursor Keys (DECCKM).
4234 * Ps = 2 -> Designate VT52 mode (DECANM).
4235 * Ps = 3 -> 80 Column Mode (DECCOLM).
4236 * Ps = 4 -> Jump (Fast) Scroll (DECSCLM).
4237 * Ps = 5 -> Normal Video (DECSCNM).
4238 * Ps = 6 -> Normal Cursor Mode (DECOM).
4239 * Ps = 7 -> No Wraparound Mode (DECAWM).
4240 * Ps = 8 -> No Auto-repeat Keys (DECARM).
4241 * Ps = 9 -> Don't send Mouse X & Y on button press.
4242 * Ps = 1 0 -> Hide toolbar (rxvt).
4243 * Ps = 1 2 -> Stop Blinking Cursor (att610).
4244 * Ps = 1 8 -> Don't print form feed (DECPFF).
4245 * Ps = 1 9 -> Limit print to scrolling region (DECPEX).
4246 * Ps = 2 5 -> Hide Cursor (DECTCEM).
4247 * Ps = 3 0 -> Don't show scrollbar (rxvt).
4248 * Ps = 3 5 -> Disable font-shifting functions (rxvt).
4249 * Ps = 4 0 -> Disallow 80 -> 132 Mode.
4250 * Ps = 4 1 -> No more(1) fix (see curses resource).
4251 * Ps = 4 2 -> Disable Nation Replacement Character sets (DEC-
4253 * Ps = 4 4 -> Turn Off Margin Bell.
4254 * Ps = 4 5 -> No Reverse-wraparound Mode.
4255 * Ps = 4 6 -> Stop Logging. (This is normally disabled by a
4256 * compile-time option).
4257 * Ps = 4 7 -> Use Normal Screen Buffer.
4258 * Ps = 6 6 -> Numeric keypad (DECNKM).
4259 * Ps = 6 7 -> Backarrow key sends delete (DECBKM).
4260 * Ps = 1 0 0 0 -> Don't send Mouse X & Y on button press and
4261 * release. See the section Mouse Tracking.
4262 * Ps = 1 0 0 1 -> Don't use Hilite Mouse Tracking.
4263 * Ps = 1 0 0 2 -> Don't use Cell Motion Mouse Tracking.
4264 * Ps = 1 0 0 3 -> Don't use All Motion Mouse Tracking.
4265 * Ps = 1 0 0 4 -> Don't send FocusIn/FocusOut events.
4266 * Ps = 1 0 0 5 -> Disable Extended Mouse Mode.
4267 * Ps = 1 0 1 0 -> Don't scroll to bottom on tty output
4269 * Ps = 1 0 1 1 -> Don't scroll to bottom on key press (rxvt).
4270 * Ps = 1 0 3 4 -> Don't interpret "meta" key. (This disables
4271 * the eightBitInput resource).
4272 * Ps = 1 0 3 5 -> Disable special modifiers for Alt and Num-
4273 * Lock keys. (This disables the numLock resource).
4274 * Ps = 1 0 3 6 -> Don't send ESC when Meta modifies a key.
4275 * (This disables the metaSendsEscape resource).
4276 * Ps = 1 0 3 7 -> Send VT220 Remove from the editing-keypad
4278 * Ps = 1 0 3 9 -> Don't send ESC when Alt modifies a key.
4279 * (This disables the altSendsEscape resource).
4280 * Ps = 1 0 4 0 -> Do not keep selection when not highlighted.
4281 * (This disables the keepSelection resource).
4282 * Ps = 1 0 4 1 -> Use the PRIMARY selection. (This disables
4283 * the selectToClipboard resource).
4284 * Ps = 1 0 4 2 -> Disable Urgency window manager hint when
4285 * Control-G is received. (This disables the bellIsUrgent
4287 * Ps = 1 0 4 3 -> Disable raising of the window when Control-
4288 * G is received. (This disables the popOnBell resource).
4289 * Ps = 1 0 4 7 -> Use Normal Screen Buffer, clearing screen
4290 * first if in the Alternate Screen. (This may be disabled by
4291 * the titeInhibit resource).
4292 * Ps = 1 0 4 8 -> Restore cursor as in DECRC. (This may be
4293 * disabled by the titeInhibit resource).
4294 * Ps = 1 0 4 9 -> Use Normal Screen Buffer and restore cursor
4295 * as in DECRC. (This may be disabled by the titeInhibit
4296 * resource). This combines the effects of the 1 0 4 7 and 1 0
4297 * 4 8 modes. Use this with terminfo-based applications rather
4298 * than the 4 7 mode.
4299 * Ps = 1 0 5 0 -> Reset terminfo/termcap function-key mode.
4300 * Ps = 1 0 5 1 -> Reset Sun function-key mode.
4301 * Ps = 1 0 5 2 -> Reset HP function-key mode.
4302 * Ps = 1 0 5 3 -> Reset SCO function-key mode.
4303 * Ps = 1 0 6 0 -> Reset legacy keyboard emulation (X11R6).
4304 * Ps = 1 0 6 1 -> Reset keyboard emulation to Sun/PC style.
4305 * Ps = 2 0 0 4 -> Reset bracketed paste mode.
4307 Terminal
.prototype.resetMode = function(params
) {
4308 if (typeof params
=== 'object') {
4309 var l
= params
.length
4312 for (; i
< l
; i
++) {
4313 this.resetMode(params
[i
]);
4322 this.insertMode
= false;
4325 //this.convertEol = false;
4328 } else if (this.prefix
=== '?') {
4331 this.applicationCursor
= false;
4334 if (this.cols
=== 132 && this.savedCols
) {
4335 this.resize(this.savedCols
, this.rows
);
4337 delete this.savedCols
;
4340 this.originMode
= false;
4343 this.wraparoundMode
= false;
4346 // this.cursorBlink = false;
4349 this.log('Switching back to normal keypad.');
4350 this.viewport
.setApplicationMode(false);
4351 this.applicationKeypad
= false;
4353 case 9: // X10 Mouse
4354 case 1000: // vt200 mouse
4355 case 1002: // button event mouse
4356 case 1003: // any event mouse
4357 this.x10Mouse
= false;
4358 this.vt200Mouse
= false;
4359 this.normalMouse
= false;
4360 this.mouseEvents
= false;
4361 this.element
.style
.cursor
= '';
4363 case 1004: // send focusin/focusout events
4364 this.sendFocus
= false;
4366 case 1005: // utf8 ext mode mouse
4367 this.utfMouse
= false;
4369 case 1006: // sgr ext mode mouse
4370 this.sgrMouse
= false;
4372 case 1015: // urxvt ext mode mouse
4373 this.urxvtMouse
= false;
4375 case 25: // hide cursor
4376 this.cursorHidden
= true;
4378 case 1049: // alt screen buffer cursor
4380 case 47: // normal screen buffer
4381 case 1047: // normal screen buffer - clearing it first
4383 this.lines
= this.normal
.lines
;
4384 this.ybase
= this.normal
.ybase
;
4385 this.ydisp
= this.normal
.ydisp
;
4386 this.x
= this.normal
.x
;
4387 this.y
= this.normal
.y
;
4388 this.scrollTop
= this.normal
.scrollTop
;
4389 this.scrollBottom
= this.normal
.scrollBottom
;
4390 this.tabs
= this.normal
.tabs
;
4392 // if (params === 1049) {
4393 // this.x = this.savedX;
4394 // this.y = this.savedY;
4396 this.refresh(0, this.rows
- 1);
4407 * Set Scrolling Region [top;bottom] (default = full size of win-
4411 Terminal
.prototype.setScrollRegion = function(params
) {
4412 if (this.prefix
) return;
4413 this.scrollTop
= (params
[0] || 1) - 1;
4414 this.scrollBottom
= (params
[1] || this.rows
) - 1;
4422 * Save cursor (ANSI.SYS).
4424 Terminal
.prototype.saveCursor = function(params
) {
4425 this.savedX
= this.x
;
4426 this.savedY
= this.y
;
4432 * Restore cursor (ANSI.SYS).
4434 Terminal
.prototype.restoreCursor = function(params
) {
4435 this.x
= this.savedX
|| 0;
4436 this.y
= this.savedY
|| 0;
4446 * Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
4448 Terminal
.prototype.cursorForwardTab = function(params
) {
4449 var param
= params
[0] || 1;
4451 this.x
= this.nextStop();
4457 * CSI Ps S Scroll up Ps lines (default = 1) (SU).
4459 Terminal
.prototype.scrollUp = function(params
) {
4460 var param
= params
[0] || 1;
4462 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
4463 this.lines
.splice(this.ybase
+ this.scrollBottom
, 0, this.blankLine());
4466 this.updateRange(this.scrollTop
);
4467 this.updateRange(this.scrollBottom
);
4472 * CSI Ps T Scroll down Ps lines (default = 1) (SD).
4474 Terminal
.prototype.scrollDown = function(params
) {
4475 var param
= params
[0] || 1;
4477 this.lines
.splice(this.ybase
+ this.scrollBottom
, 1);
4478 this.lines
.splice(this.ybase
+ this.scrollTop
, 0, this.blankLine());
4481 this.updateRange(this.scrollTop
);
4482 this.updateRange(this.scrollBottom
);
4487 * CSI Ps ; Ps ; Ps ; Ps ; Ps T
4488 * Initiate highlight mouse tracking. Parameters are
4489 * [func;startx;starty;firstrow;lastrow]. See the section Mouse
4492 Terminal
.prototype.initMouseTracking = function(params
) {
4493 // Relevant: DECSET 1001
4499 * Reset one or more features of the title modes to the default
4500 * value. Normally, "reset" disables the feature. It is possi-
4501 * ble to disable the ability to reset features by compiling a
4502 * different default for the title modes into xterm.
4503 * Ps = 0 -> Do not set window/icon labels using hexadecimal.
4504 * Ps = 1 -> Do not query window/icon labels using hexadeci-
4506 * Ps = 2 -> Do not set window/icon labels using UTF-8.
4507 * Ps = 3 -> Do not query window/icon labels using UTF-8.
4508 * (See discussion of "Title Modes").
4510 Terminal
.prototype.resetTitleModes = function(params
) {
4516 * CSI Ps Z Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
4518 Terminal
.prototype.cursorBackwardTab = function(params
) {
4519 var param
= params
[0] || 1;
4521 this.x
= this.prevStop();
4527 * CSI Ps b Repeat the preceding graphic character Ps times (REP).
4529 Terminal
.prototype.repeatPrecedingCharacter = function(params
) {
4530 var param
= params
[0] || 1
4531 , line
= this.lines
[this.ybase
+ this.y
]
4532 , ch
= line
[this.x
- 1] || [this.defAttr
, ' ', 1];
4534 while (param
--) line
[this.x
++] = ch
;
4539 * CSI Ps g Tab Clear (TBC).
4540 * Ps = 0 -> Clear Current Column (default).
4541 * Ps = 3 -> Clear All.
4543 * Ps = 2 -> Clear Stops on Line.
4544 * http://vt100.net/annarbor/aaa-ug/section6.html
4546 Terminal
.prototype.tabClear = function(params
) {
4547 var param
= params
[0];
4549 delete this.tabs
[this.x
];
4550 } else if (param
=== 3) {
4557 * CSI Pm i Media Copy (MC).
4558 * Ps = 0 -> Print screen (default).
4559 * Ps = 4 -> Turn off printer controller mode.
4560 * Ps = 5 -> Turn on printer controller mode.
4562 * Media Copy (MC, DEC-specific).
4563 * Ps = 1 -> Print line containing cursor.
4564 * Ps = 4 -> Turn off autoprint mode.
4565 * Ps = 5 -> Turn on autoprint mode.
4566 * Ps = 1 0 -> Print composed display, ignores DECPEX.
4567 * Ps = 1 1 -> Print all pages.
4569 Terminal
.prototype.mediaCopy = function(params
) {
4576 * Set or reset resource-values used by xterm to decide whether
4577 * to construct escape sequences holding information about the
4578 * modifiers pressed with a given key. The first parameter iden-
4579 * tifies the resource to set/reset. The second parameter is the
4580 * value to assign to the resource. If the second parameter is
4581 * omitted, the resource is reset to its initial value.
4582 * Ps = 1 -> modifyCursorKeys.
4583 * Ps = 2 -> modifyFunctionKeys.
4584 * Ps = 4 -> modifyOtherKeys.
4585 * If no parameters are given, all resources are reset to their
4588 Terminal
.prototype.setResources = function(params
) {
4595 * Disable modifiers which may be enabled via the CSI > Ps; Ps m
4596 * sequence. This corresponds to a resource value of "-1", which
4597 * cannot be set with the other sequence. The parameter identi-
4598 * fies the resource to be disabled:
4599 * Ps = 1 -> modifyCursorKeys.
4600 * Ps = 2 -> modifyFunctionKeys.
4601 * Ps = 4 -> modifyOtherKeys.
4602 * If the parameter is omitted, modifyFunctionKeys is disabled.
4603 * When modifyFunctionKeys is disabled, xterm uses the modifier
4604 * keys to make an extended sequence of functions rather than
4605 * adding a parameter to each function key to denote the modi-
4608 Terminal
.prototype.disableModifiers = function(params
) {
4615 * Set resource value pointerMode. This is used by xterm to
4616 * decide whether to hide the pointer cursor as the user types.
4617 * Valid values for the parameter:
4618 * Ps = 0 -> never hide the pointer.
4619 * Ps = 1 -> hide if the mouse tracking mode is not enabled.
4620 * Ps = 2 -> always hide the pointer. If no parameter is
4621 * given, xterm uses the default, which is 1 .
4623 Terminal
.prototype.setPointerMode = function(params
) {
4629 * CSI ! p Soft terminal reset (DECSTR).
4630 * http://vt100.net/docs/vt220-rm/table4-10.html
4632 Terminal
.prototype.softReset = function(params
) {
4633 this.cursorHidden
= false;
4634 this.insertMode
= false;
4635 this.originMode
= false;
4636 this.wraparoundMode
= false; // autowrap
4637 this.applicationKeypad
= false; // ?
4638 this.applicationCursor
= false;
4640 this.scrollBottom
= this.rows
- 1;
4641 this.curAttr
= this.defAttr
;
4642 this.x
= this.y
= 0; // ?
4643 this.charset
= null;
4644 this.glevel
= 0; // ??
4645 this.charsets
= [null]; // ??
4651 * Request ANSI mode (DECRQM). For VT300 and up, reply is
4653 * where Ps is the mode number as in RM, and Pm is the mode
4655 * 0 - not recognized
4658 * 3 - permanently set
4659 * 4 - permanently reset
4661 Terminal
.prototype.requestAnsiMode = function(params
) {
4668 * Request DEC private mode (DECRQM). For VT300 and up, reply is
4670 * where Ps is the mode number as in DECSET, Pm is the mode value
4671 * as in the ANSI DECRQM.
4673 Terminal
.prototype.requestPrivateMode = function(params
) {
4680 * Set conformance level (DECSCL). Valid values for the first
4682 * Ps = 6 1 -> VT100.
4683 * Ps = 6 2 -> VT200.
4684 * Ps = 6 3 -> VT300.
4685 * Valid values for the second parameter:
4686 * Ps = 0 -> 8-bit controls.
4687 * Ps = 1 -> 7-bit controls (always set for VT100).
4688 * Ps = 2 -> 8-bit controls.
4690 Terminal
.prototype.setConformanceLevel = function(params
) {
4696 * CSI Ps q Load LEDs (DECLL).
4697 * Ps = 0 -> Clear all LEDS (default).
4698 * Ps = 1 -> Light Num Lock.
4699 * Ps = 2 -> Light Caps Lock.
4700 * Ps = 3 -> Light Scroll Lock.
4701 * Ps = 2 1 -> Extinguish Num Lock.
4702 * Ps = 2 2 -> Extinguish Caps Lock.
4703 * Ps = 2 3 -> Extinguish Scroll Lock.
4705 Terminal
.prototype.loadLEDs = function(params
) {
4712 * Set cursor style (DECSCUSR, VT520).
4713 * Ps = 0 -> blinking block.
4714 * Ps = 1 -> blinking block (default).
4715 * Ps = 2 -> steady block.
4716 * Ps = 3 -> blinking underline.
4717 * Ps = 4 -> steady underline.
4719 Terminal
.prototype.setCursorStyle = function(params
) {
4726 * Select character protection attribute (DECSCA). Valid values
4727 * for the parameter:
4728 * Ps = 0 -> DECSED and DECSEL can erase (default).
4729 * Ps = 1 -> DECSED and DECSEL cannot erase.
4730 * Ps = 2 -> DECSED and DECSEL can erase.
4732 Terminal
.prototype.setCharProtectionAttr = function(params
) {
4739 * Restore DEC Private Mode Values. The value of Ps previously
4740 * saved is restored. Ps values are the same as for DECSET.
4742 Terminal
.prototype.restorePrivateValues = function(params
) {
4748 * CSI Pt; Pl; Pb; Pr; Ps$ r
4749 * Change Attributes in Rectangular Area (DECCARA), VT400 and up.
4750 * Pt; Pl; Pb; Pr denotes the rectangle.
4751 * Ps denotes the SGR attributes to change: 0, 1, 4, 5, 7.
4752 * NOTE: xterm doesn't enable this code by default.
4754 Terminal
.prototype.setAttrInRectangle = function(params
) {
4764 for (; t
< b
+ 1; t
++) {
4765 line
= this.lines
[this.ybase
+ t
];
4766 for (i
= l
; i
< r
; i
++) {
4767 line
[i
] = [attr
, line
[i
][1]];
4772 this.updateRange(params
[0]);
4773 this.updateRange(params
[2]);
4778 * CSI Pc; Pt; Pl; Pb; Pr$ x
4779 * Fill Rectangular Area (DECFRA), VT420 and up.
4780 * Pc is the character to use.
4781 * Pt; Pl; Pb; Pr denotes the rectangle.
4782 * NOTE: xterm doesn't enable this code by default.
4784 Terminal
.prototype.fillRectangle = function(params
) {
4794 for (; t
< b
+ 1; t
++) {
4795 line
= this.lines
[this.ybase
+ t
];
4796 for (i
= l
; i
< r
; i
++) {
4797 line
[i
] = [line
[i
][0], String
.fromCharCode(ch
)];
4802 this.updateRange(params
[1]);
4803 this.updateRange(params
[3]);
4809 * Enable Locator Reporting (DECELR).
4810 * Valid values for the first parameter:
4811 * Ps = 0 -> Locator disabled (default).
4812 * Ps = 1 -> Locator enabled.
4813 * Ps = 2 -> Locator enabled for one report, then disabled.
4814 * The second parameter specifies the coordinate unit for locator
4816 * Valid values for the second parameter:
4817 * Pu = 0 <- or omitted -> default to character cells.
4818 * Pu = 1 <- device physical pixels.
4819 * Pu = 2 <- character cells.
4821 Terminal
.prototype.enableLocatorReporting = function(params
) {
4822 var val
= params
[0] > 0;
4823 //this.mouseEvents = val;
4824 //this.decLocator = val;
4829 * CSI Pt; Pl; Pb; Pr$ z
4830 * Erase Rectangular Area (DECERA), VT400 and up.
4831 * Pt; Pl; Pb; Pr denotes the rectangle.
4832 * NOTE: xterm doesn't enable this code by default.
4834 Terminal
.prototype.eraseRectangle = function(params
) {
4844 ch
= [this.eraseAttr(), ' ', 1]; // xterm?
4846 for (; t
< b
+ 1; t
++) {
4847 line
= this.lines
[this.ybase
+ t
];
4848 for (i
= l
; i
< r
; i
++) {
4854 this.updateRange(params
[0]);
4855 this.updateRange(params
[2]);
4861 * Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
4862 * NOTE: xterm doesn't enable this code by default.
4864 Terminal
.prototype.insertColumns = function() {
4865 var param
= params
[0]
4866 , l
= this.ybase
+ this.rows
4867 , ch
= [this.eraseAttr(), ' ', 1] // xterm?
4871 for (i
= this.ybase
; i
< l
; i
++) {
4872 this.lines
[i
].splice(this.x
+ 1, 0, ch
);
4873 this.lines
[i
].pop();
4883 * Delete P s Column(s) (default = 1) (DECDC), VT420 and up
4884 * NOTE: xterm doesn't enable this code by default.
4886 Terminal
.prototype.deleteColumns = function() {
4887 var param
= params
[0]
4888 , l
= this.ybase
+ this.rows
4889 , ch
= [this.eraseAttr(), ' ', 1] // xterm?
4893 for (i
= this.ybase
; i
< l
; i
++) {
4894 this.lines
[i
].splice(this.x
, 1);
4895 this.lines
[i
].push(ch
);
4906 Terminal
.charsets
= {};
4908 // DEC Special Character and Line Drawing Set.
4909 // http://vt100.net/docs/vt102-ug/table5-13.html
4910 // A lot of curses apps use this if they see TERM=xterm.
4911 // testing: echo -e '\e(0a\e(B'
4912 // The xterm output sometimes seems to conflict with the
4913 // reference above. xterm seems in line with the reference
4914 // when running vttest however.
4915 // The table below now uses xterm's output from vttest.
4916 Terminal
.charsets
.SCLD
= { // (0
4917 '`': '\u25c6', // '◆'
4918 'a': '\u2592', // '▒'
4919 'b': '\u0009', // '\t'
4920 'c': '\u000c', // '\f'
4921 'd': '\u000d', // '\r'
4922 'e': '\u000a', // '\n'
4923 'f': '\u00b0', // '°'
4924 'g': '\u00b1', // '±'
4925 'h': '\u2424', // '\u2424' (NL)
4926 'i': '\u000b', // '\v'
4927 'j': '\u2518', // '┘'
4928 'k': '\u2510', // '┐'
4929 'l': '\u250c', // '┌'
4930 'm': '\u2514', // '└'
4931 'n': '\u253c', // '┼'
4932 'o': '\u23ba', // '⎺'
4933 'p': '\u23bb', // '⎻'
4934 'q': '\u2500', // '─'
4935 'r': '\u23bc', // '⎼'
4936 's': '\u23bd', // '⎽'
4937 't': '\u251c', // '├'
4938 'u': '\u2524', // '┤'
4939 'v': '\u2534', // '┴'
4940 'w': '\u252c', // '┬'
4941 'x': '\u2502', // '│'
4942 'y': '\u2264', // '≤'
4943 'z': '\u2265', // '≥'
4944 '{': '\u03c0', // 'π'
4945 '|': '\u2260', // '≠'
4946 '}': '\u00a3', // '£'
4947 '~': '\u00b7' // '·'
4950 Terminal
.charsets
.UK
= null; // (A
4951 Terminal
.charsets
.US
= null; // (B (USASCII)
4952 Terminal
.charsets
.Dutch
= null; // (4
4953 Terminal
.charsets
.Finnish
= null; // (C or (5
4954 Terminal
.charsets
.French
= null; // (R
4955 Terminal
.charsets
.FrenchCanadian
= null; // (Q
4956 Terminal
.charsets
.German
= null; // (K
4957 Terminal
.charsets
.Italian
= null; // (Y
4958 Terminal
.charsets
.NorwegianDanish
= null; // (E or (6
4959 Terminal
.charsets
.Spanish
= null; // (Z
4960 Terminal
.charsets
.Swedish
= null; // (H or (7
4961 Terminal
.charsets
.Swiss
= null; // (=
4962 Terminal
.charsets
.ISOLatin
= null; // /A
4968 function contains(el
, arr
) {
4969 for (var i
= 0; i
< arr
.length
; i
+= 1) {
4970 if (el
=== arr
[i
]) {
4977 function on(el
, type
, handler
, capture
) {
4978 if (!Array
.isArray(el
)) {
4981 el
.forEach(function (element
) {
4982 element
.addEventListener(type
, handler
, capture
|| false);
4986 function off(el
, type
, handler
, capture
) {
4987 el
.removeEventListener(type
, handler
, capture
|| false);
4990 function cancel(ev
, force
) {
4991 if (!this.cancelEvents
&& !force
) {
4994 ev
.preventDefault();
4995 ev
.stopPropagation();
4999 function inherits(child
, parent
) {
5001 this.constructor = child
;
5003 f
.prototype = parent
.prototype;
5004 child
.prototype = new f
;
5007 // if bold is broken, we can't
5008 // use it in the terminal.
5009 function isBoldBroken(document
) {
5010 var body
= document
.getElementsByTagName('body')[0];
5011 var el
= document
.createElement('span');
5012 el
.innerHTML
= 'hello world';
5013 body
.appendChild(el
);
5014 var w1
= el
.scrollWidth
;
5015 el
.style
.fontWeight
= 'bold';
5016 var w2
= el
.scrollWidth
;
5017 body
.removeChild(el
);
5021 function indexOf(obj
, el
) {
5024 if (obj
[i
] === el
) return i
;
5029 function isThirdLevelShift(term
, ev
) {
5031 (term
.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) ||
5032 (term
.isMSWindows
&& ev
.altKey
&& ev
.ctrlKey
&& !ev
.metaKey
);
5034 if (ev
.type
== 'keypress') {
5035 return thirdLevelKey
;
5038 // Don't invoke for arrows, pageDown, home, backspace, etc. (on non-keypress events)
5039 return thirdLevelKey
&& (!ev
.keyCode
|| ev
.keyCode
> 47);
5042 function matchColor(r1
, g1
, b1
) {
5043 var hash
= (r1
<< 16) | (g1
<< 8) | b1
;
5045 if (matchColor
._cache
[hash
] != null) {
5046 return matchColor
._cache
[hash
];
5049 var ldiff
= Infinity
5058 for (; i
< Terminal
.vcolors
.length
; i
++) {
5059 c
= Terminal
.vcolors
[i
];
5064 diff
= matchColor
.distance(r1
, g1
, b1
, r2
, g2
, b2
);
5077 return matchColor
._cache
[hash
] = li
;
5080 matchColor
._cache
= {};
5082 // http://stackoverflow.com/questions/1633828
5083 matchColor
.distance = function(r1
, g1
, b1
, r2
, g2
, b2
) {
5084 return Math
.pow(30 * (r1
- r2
), 2)
5085 + Math
.pow(59 * (g1
- g2
), 2)
5086 + Math
.pow(11 * (b1
- b2
), 2);
5089 function each(obj
, iter
, con
) {
5090 if (obj
.forEach
) return obj
.forEach(iter
, con
);
5091 for (var i
= 0; i
< obj
.length
; i
++) {
5092 iter
.call(con
, obj
[i
], i
, obj
);
5096 function keys(obj
) {
5097 if (Object
.keys
) return Object
.keys(obj
);
5100 if (Object
.prototype.hasOwnProperty
.call(obj
, key
)) {
5107 var wcwidth
= (function(opts
) {
5108 // extracted from https://www.cl.cam.ac.uk/%7Emgk25/ucs/wcwidth.c
5109 // combining characters
5111 [0x0300, 0x036F], [0x0483, 0x0486], [0x0488, 0x0489],
5112 [0x0591, 0x05BD], [0x05BF, 0x05BF], [0x05C1, 0x05C2],
5113 [0x05C4, 0x05C5], [0x05C7, 0x05C7], [0x0600, 0x0603],
5114 [0x0610, 0x0615], [0x064B, 0x065E], [0x0670, 0x0670],
5115 [0x06D6, 0x06E4], [0x06E7, 0x06E8], [0x06EA, 0x06ED],
5116 [0x070F, 0x070F], [0x0711, 0x0711], [0x0730, 0x074A],
5117 [0x07A6, 0x07B0], [0x07EB, 0x07F3], [0x0901, 0x0902],
5118 [0x093C, 0x093C], [0x0941, 0x0948], [0x094D, 0x094D],
5119 [0x0951, 0x0954], [0x0962, 0x0963], [0x0981, 0x0981],
5120 [0x09BC, 0x09BC], [0x09C1, 0x09C4], [0x09CD, 0x09CD],
5121 [0x09E2, 0x09E3], [0x0A01, 0x0A02], [0x0A3C, 0x0A3C],
5122 [0x0A41, 0x0A42], [0x0A47, 0x0A48], [0x0A4B, 0x0A4D],
5123 [0x0A70, 0x0A71], [0x0A81, 0x0A82], [0x0ABC, 0x0ABC],
5124 [0x0AC1, 0x0AC5], [0x0AC7, 0x0AC8], [0x0ACD, 0x0ACD],
5125 [0x0AE2, 0x0AE3], [0x0B01, 0x0B01], [0x0B3C, 0x0B3C],
5126 [0x0B3F, 0x0B3F], [0x0B41, 0x0B43], [0x0B4D, 0x0B4D],
5127 [0x0B56, 0x0B56], [0x0B82, 0x0B82], [0x0BC0, 0x0BC0],
5128 [0x0BCD, 0x0BCD], [0x0C3E, 0x0C40], [0x0C46, 0x0C48],
5129 [0x0C4A, 0x0C4D], [0x0C55, 0x0C56], [0x0CBC, 0x0CBC],
5130 [0x0CBF, 0x0CBF], [0x0CC6, 0x0CC6], [0x0CCC, 0x0CCD],
5131 [0x0CE2, 0x0CE3], [0x0D41, 0x0D43], [0x0D4D, 0x0D4D],
5132 [0x0DCA, 0x0DCA], [0x0DD2, 0x0DD4], [0x0DD6, 0x0DD6],
5133 [0x0E31, 0x0E31], [0x0E34, 0x0E3A], [0x0E47, 0x0E4E],
5134 [0x0EB1, 0x0EB1], [0x0EB4, 0x0EB9], [0x0EBB, 0x0EBC],
5135 [0x0EC8, 0x0ECD], [0x0F18, 0x0F19], [0x0F35, 0x0F35],
5136 [0x0F37, 0x0F37], [0x0F39, 0x0F39], [0x0F71, 0x0F7E],
5137 [0x0F80, 0x0F84], [0x0F86, 0x0F87], [0x0F90, 0x0F97],
5138 [0x0F99, 0x0FBC], [0x0FC6, 0x0FC6], [0x102D, 0x1030],
5139 [0x1032, 0x1032], [0x1036, 0x1037], [0x1039, 0x1039],
5140 [0x1058, 0x1059], [0x1160, 0x11FF], [0x135F, 0x135F],
5141 [0x1712, 0x1714], [0x1732, 0x1734], [0x1752, 0x1753],
5142 [0x1772, 0x1773], [0x17B4, 0x17B5], [0x17B7, 0x17BD],
5143 [0x17C6, 0x17C6], [0x17C9, 0x17D3], [0x17DD, 0x17DD],
5144 [0x180B, 0x180D], [0x18A9, 0x18A9], [0x1920, 0x1922],
5145 [0x1927, 0x1928], [0x1932, 0x1932], [0x1939, 0x193B],
5146 [0x1A17, 0x1A18], [0x1B00, 0x1B03], [0x1B34, 0x1B34],
5147 [0x1B36, 0x1B3A], [0x1B3C, 0x1B3C], [0x1B42, 0x1B42],
5148 [0x1B6B, 0x1B73], [0x1DC0, 0x1DCA], [0x1DFE, 0x1DFF],
5149 [0x200B, 0x200F], [0x202A, 0x202E], [0x2060, 0x2063],
5150 [0x206A, 0x206F], [0x20D0, 0x20EF], [0x302A, 0x302F],
5151 [0x3099, 0x309A], [0xA806, 0xA806], [0xA80B, 0xA80B],
5152 [0xA825, 0xA826], [0xFB1E, 0xFB1E], [0xFE00, 0xFE0F],
5153 [0xFE20, 0xFE23], [0xFEFF, 0xFEFF], [0xFFF9, 0xFFFB],
5154 [0x10A01, 0x10A03], [0x10A05, 0x10A06], [0x10A0C, 0x10A0F],
5155 [0x10A38, 0x10A3A], [0x10A3F, 0x10A3F], [0x1D167, 0x1D169],
5156 [0x1D173, 0x1D182], [0x1D185, 0x1D18B], [0x1D1AA, 0x1D1AD],
5157 [0x1D242, 0x1D244], [0xE0001, 0xE0001], [0xE0020, 0xE007F],
5161 function bisearch(ucs
) {
5163 var max
= COMBINING
.length
- 1;
5165 if (ucs
< COMBINING
[0][0] || ucs
> COMBINING
[max
][1])
5167 while (max
>= min
) {
5168 mid
= Math
.floor((min
+ max
) / 2);
5169 if (ucs
> COMBINING
[mid
][1])
5171 else if (ucs
< COMBINING
[mid
][0])
5178 function wcwidth(ucs
) {
5179 // test for 8-bit control characters
5182 if (ucs
< 32 || (ucs
>= 0x7f && ucs
< 0xa0))
5183 return opts
.control
;
5184 // binary search in table of non-spacing characters
5187 // if we arrive here, ucs is not a combining or C0/C1 control character
5192 ucs
<= 0x115f || // Hangul Jamo init. consonants
5195 (ucs
>= 0x2e80 && ucs
<= 0xa4cf && ucs
!= 0x303f) || // CJK..Yi
5196 (ucs
>= 0xac00 && ucs
<= 0xd7a3) || // Hangul Syllables
5197 (ucs
>= 0xf900 && ucs
<= 0xfaff) || // CJK Compat Ideographs
5198 (ucs
>= 0xfe10 && ucs
<= 0xfe19) || // Vertical forms
5199 (ucs
>= 0xfe30 && ucs
<= 0xfe6f) || // CJK Compat Forms
5200 (ucs
>= 0xff00 && ucs
<= 0xff60) || // Fullwidth Forms
5201 (ucs
>= 0xffe0 && ucs
<= 0xffe6) ||
5202 (ucs
>= 0x20000 && ucs
<= 0x2fffd) ||
5203 (ucs
>= 0x30000 && ucs
<= 0x3fffd)
5208 })({nul
: 0, control
: 0}); // configurable options
5214 Terminal
.EventEmitter
= EventEmitter
;
5215 Terminal
.CompositionHelper
= CompositionHelper
;
5216 Terminal
.Viewport
= Viewport
;
5217 Terminal
.inherits
= inherits
;
5220 * Adds an event listener to the terminal.
5222 * @param {string} event The name of the event. TODO: Document all event types
5223 * @param {function} callback The function to call when the event is triggered.
5227 Terminal
.cancel
= cancel
;
5229 module
.exports
= Terminal
;