2 * xterm.js: xterm, in the browser
3 * Copyright (c) 2014, sourceLair Limited (www.sourcelair.com (MIT License)
4 * Copyright (c) 2012-2013, Christopher Jeffrey (MIT License)
5 * https://github.com/chjj/term.js
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * Originally forked from (with the author's permission):
26 * Fabrice Bellard's javascript vt100 for jslinux:
27 * http://bellard.org/jslinux/
28 * Copyright (c) 2011 Fabrice Bellard
29 * The original design remains. The terminal itself
30 * has been extended to include xterm CSI codes, among
35 if (typeof exports
=== 'object' && typeof module
=== 'object') {
37 * CommonJS environment
39 module
.exports
= xterm
.call(this);
40 } else if (typeof define
== 'function') {
42 * Require.js is available
44 define([], xterm
.bind(window
));
47 * Plain browser environment
49 this.Xterm
= xterm
.call(this);
50 this.Terminal
= this.Xterm
; /* Backwards compatibility with term.js */
54 * Terminal Emulation References:
56 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.txt
57 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
58 * http://invisible-island.net/vttest/
59 * http://www.inwap.com/pdp10/ansicode.txt
60 * http://linux.die.net/man/4/console_codes
61 * http://linux.die.net/man/7/urxvt
70 var window
= this, document
= this.document
;
76 function EventEmitter() {
77 this._events
= this._events
|| {};
80 EventEmitter
.prototype.addListener = function(type
, listener
) {
81 this._events
[type
] = this._events
[type
] || [];
82 this._events
[type
].push(listener
);
85 EventEmitter
.prototype.on
= EventEmitter
.prototype.addListener
;
87 EventEmitter
.prototype.removeListener = function(type
, listener
) {
88 if (!this._events
[type
]) return;
90 var obj
= this._events
[type
]
94 if (obj
[i
] === listener
|| obj
[i
].listener
=== listener
) {
101 EventEmitter
.prototype.off
= EventEmitter
.prototype.removeListener
;
103 EventEmitter
.prototype.removeAllListeners = function(type
) {
104 if (this._events
[type
]) delete this._events
[type
];
107 EventEmitter
.prototype.once = function(type
, listener
) {
110 var args
= Array
.prototype.slice
.call(arguments
);
111 this.removeListener(type
, on
);
112 return listener
.apply(this, args
);
114 on
.listener
= listener
;
115 return this.on(type
, on
);
118 EventEmitter
.prototype.emit = function(type
) {
119 if (!this._events
[type
]) return;
121 var args
= Array
.prototype.slice
.call(arguments
, 1)
122 , obj
= this._events
[type
]
127 obj
[i
].apply(this, args
);
131 EventEmitter
.prototype.listeners = function(type
) {
132 return this._events
[type
] = this._events
[type
] || [];
137 * Encapsulates the logic for handling compositionstart, compositionupdate and compositionend
138 * events, displaying the in-progress composition to the UI and forwarding the final composition
140 * @param {HTMLTextAreaElement} textarea The textarea that xterm uses for input.
141 * @param {HTMLElement} compositionView The element to display the in-progress composition in.
142 * @param {Terminal} terminal The Terminal to forward the finished composition to.
144 function CompositionHelper(textarea
, compositionView
, terminal
) {
145 this.textarea
= textarea
;
146 this.compositionView
= compositionView
;
147 this.terminal
= terminal
;
149 // Whether input composition is currently happening, eg. via a mobile keyboard, speech input
150 // or IME. This variable determines whether the compositionText should be displayed on the UI.
151 this.isComposing
= false;
153 // The input currently being composed, eg. via a mobile keyboard, speech input or IME.
154 this.compositionText
= null;
156 // The position within the input textarea's value of the current composition.
157 this.compositionPosition
= { start
: null, end
: null };
159 // Whether a composition is in the process of being sent, setting this to false will cancel
160 // any in-progress composition.
161 this.isSendingComposition
= false;
165 * Handles the compositionstart event, activating the composition view.
167 CompositionHelper
.prototype.compositionstart = function() {
168 this.isComposing
= true;
169 this.compositionPosition
.start
= this.textarea
.value
.length
;
170 this.compositionView
.textContent
= '';
171 this.compositionView
.classList
.add('active');
175 * Handles the compositionupdate event, updating the composition view.
176 * @param {CompositionEvent} ev The event.
178 CompositionHelper
.prototype.compositionupdate = function(ev
) {
179 this.compositionView
.textContent
= ev
.data
;
180 this.updateCompositionElements();
182 setTimeout(function() {
183 self
.compositionPosition
.end
= self
.textarea
.value
.length
;
188 * Handles the compositionend event, hiding the composition view and sending the composition to
191 CompositionHelper
.prototype.compositionend = function() {
192 this.finalizeComposition(true);
196 * Handles the keydown event, routing any necessary events to the CompositionHelper functions.
197 * @return Whether the Terminal should continue processing the keydown event.
199 CompositionHelper
.prototype.keydown = function(ev
) {
200 if (this.isComposing
|| this.isSendingComposition
) {
201 if (ev
.keyCode
=== 229) {
202 // Continue composing if the keyCode is the "composition character"
204 } else if (ev
.keyCode
=== 16 || ev
.keyCode
=== 17 || ev
.keyCode
=== 18) {
205 // Continue composing if the keyCode is a modifier key
208 // Finish composition immediately. This is mainly here for the case where enter is
209 // pressed and the handler needs to be triggered before the command is executed.
210 this.finalizeComposition(false);
214 if (ev
.keyCode
=== 229) {
215 // If the "composition character" is used but gets to this point it means a non-composition
216 // character (eg. numbers and punctuation) was pressed when the IME was active.
217 this.handleAnyTextareaChanges();
225 * Finalizes the composition, resuming regular input actions. This is called when a composition
227 * @param {boolean} waitForPropogation Whether to wait for events to propogate before sending
228 * the input. This should be false if a non-composition keystroke is entered before the
229 * compositionend event is triggered, such as enter, so that the composition is send before
230 * the command is executed.
232 CompositionHelper
.prototype.finalizeComposition = function(waitForPropogation
) {
233 this.compositionView
.classList
.remove('active');
234 this.isComposing
= false;
235 this.clearTextareaPosition();
237 if (!waitForPropogation
) {
238 // Cancel any delayed composition send requests and send the input immediately.
239 this.isSendingComposition
= false;
240 var input
= this.textarea
.value
.substring(this.compositionPosition
.start
, this.compositionPosition
.end
);
241 this.terminal
.handler(input
);
243 // Make a deep copy of the composition position here as a new compositionstart event may
244 // fire before the setTimeout executes.
245 var currentCompositionPosition
= {
246 start
: this.compositionPosition
.start
,
247 end
: this.compositionPosition
.end
,
250 // Since composition* events happen before the changes take place in the textarea on most
251 // browsers, use a setTimeout with 0ms time to allow the native compositionend event to
252 // complete. This ensures the correct character is retrieved, this solution was used
254 // - The compositionend event's data property is unreliable, at least on Chromium
255 // - The last compositionupdate event's data property does not always accurately describe
256 // the character, a counter example being Korean where an ending consonsant can move to
257 // the following character if the following input is a vowel.
259 this.isSendingComposition
= true;
260 setTimeout(function () {
261 // Ensure that the input has not already been sent
262 if (self
.isSendingComposition
) {
263 self
.isSendingComposition
= false;
265 if (self
.isComposing
) {
266 // Use the end position to get the string if a new composition has started.
267 input
= self
.textarea
.value
.substring(currentCompositionPosition
.start
, currentCompositionPosition
.end
);
269 // Don't use the end position here in order to pick up any characters after the
270 // composition has finished, for example when typing a non-composition character
271 // (eg. 2) after a composition character.
272 input
= self
.textarea
.value
.substring(currentCompositionPosition
.start
);
274 self
.terminal
.handler(input
);
281 * Apply any changes made to the textarea after the current event chain is allowed to complete.
282 * This should be called when not currently composing but a keydown event with the "composition
283 * character" (229) is triggered, in order to allow non-composition text to be entered when an
286 CompositionHelper
.prototype.handleAnyTextareaChanges = function() {
287 var oldValue
= this.textarea
.value
;
289 setTimeout(function() {
290 // Ignore if a composition has started since the timeout
291 if (!self
.isComposing
) {
292 var newValue
= self
.textarea
.value
;
293 var diff
= newValue
.replace(oldValue
, '');
294 if (diff
.length
> 0) {
295 self
.terminal
.handler(diff
);
302 * Positions the composition view on top of the cursor and the textarea just below it (so the
303 * IME helper dialog is positioned correctly).
305 CompositionHelper
.prototype.updateCompositionElements = function() {
306 var cursor
= this.terminal
.element
.querySelector('.terminal-cursor');
308 this.compositionView
.style
.left
= cursor
.offsetLeft
+ 'px';
309 this.compositionView
.style
.top
= cursor
.offsetTop
+ 'px';
311 this.textarea
.style
.left
= cursor
.offsetLeft
+ 'px';
312 this.textarea
.style
.top
= (cursor
.offsetTop
+ cursor
.offsetHeight
) + 'px';
316 * Clears the textarea's position so that the cursor does not blink on IE.
319 CompositionHelper
.prototype.clearTextareaPosition = function() {
320 this.textarea
.style
.left
= undefined;
321 this.textarea
.style
.top
= undefined;
328 var normal
= 0, escaped
= 1, csi
= 2, osc
= 3, charset
= 4, dcs
= 5, ignore
= 6;
335 * Creates a new `Terminal` object.
337 * @param {object} options An object containing a set of options, the available options are:
338 * - cursorBlink (boolean): Whether the terminal cursor blinks
342 * @alias module:xterm/src/xterm
344 function Terminal(options
) {
347 if (!(this instanceof Terminal
)) {
348 return new Terminal(arguments
[0], arguments
[1], arguments
[2]);
351 self
.cancel
= Terminal
.cancel
;
353 EventEmitter
.call(this);
355 if (typeof options
=== 'number') {
359 handler
: arguments
[2]
363 options
= options
|| {};
366 Object
.keys(Terminal
.defaults
).forEach(function(key
) {
367 if (options
[key
] == null) {
368 options
[key
] = Terminal
.options
[key
];
370 if (Terminal
[key
] !== Terminal
.defaults
[key
]) {
371 options
[key
] = Terminal
[key
];
374 self
[key
] = options
[key
];
377 if (options
.colors
.length
=== 8) {
378 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(8));
379 } else if (options
.colors
.length
=== 16) {
380 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(16));
381 } else if (options
.colors
.length
=== 10) {
382 options
.colors
= options
.colors
.slice(0, -2).concat(
383 Terminal
._colors
.slice(8, -2), options
.colors
.slice(-2));
384 } else if (options
.colors
.length
=== 18) {
385 options
.colors
= options
.colors
.concat(
386 Terminal
._colors
.slice(16, -2), options
.colors
.slice(-2));
388 this.colors
= options
.colors
;
390 this.options
= options
;
392 // this.context = options.context || window;
393 // this.document = options.document || document;
394 this.parent
= options
.body
|| options
.parent
395 || (document
? document
.getElementsByTagName('body')[0] : null);
397 this.cols
= options
.cols
|| options
.geometry
[0];
398 this.rows
= options
.rows
|| options
.geometry
[1];
400 if (options
.handler
) {
401 this.on('data', options
.handler
);
405 * The scroll position of the y cursor, ie. ybase + y = the y position within the entire
411 * The scroll position of the viewport
416 * The cursor's x position after ybase
421 * The cursor's y position after ybase
426 * Used to debounce the refresh function
428 this.isRefreshing
= false;
431 * Whether there is a full terminal refresh queued
434 this.cursorState
= 0;
435 this.cursorHidden
= false;
440 this.scrollBottom
= this.rows
- 1;
443 this.applicationKeypad
= false;
444 this.applicationCursor
= false;
445 this.originMode
= false;
446 this.insertMode
= false;
447 this.wraparoundMode
= true; // defaults: xterm - true, vt100 - false
452 this.gcharset
= null;
454 this.charsets
= [null];
478 this.readable
= true;
479 this.writable
= true;
481 this.defAttr
= (0 << 18) | (257 << 9) | (256 << 0);
482 this.curAttr
= this.defAttr
;
485 this.currentParam
= 0;
489 // leftover surrogate high from previous write invocation
490 this.surrogate_high
= '';
493 * An array of all lines in the entire buffer, including the prompt. The lines are array of
494 * characters which are 2-length arrays where [0] is an attribute and [1] is the character.
499 this.lines
.push(this.blankLine());
506 inherits(Terminal
, EventEmitter
);
509 * back_color_erase feature for xterm.
511 Terminal
.prototype.eraseAttr = function() {
512 // if (this.is('screen')) return this.defAttr;
513 return (this.defAttr
& ~0x1ff) | (this.curAttr
& 0x1ff);
521 Terminal
.tangoColors
= [
542 // Colors 0-15 + 16-255
543 // Much thanks to TooTallNate for writing this.
544 Terminal
.colors
= (function() {
545 var colors
= Terminal
.tangoColors
.slice()
546 , r
= [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff]
551 for (; i
< 216; i
++) {
552 out(r
[(i
/ 36) % 6 | 0], r
[(i
/ 6) % 6 | 0], r
[i
% 6]);
557 for (; i
< 24; i
++) {
562 function out(r
, g
, b
) {
563 colors
.push('#' + hex(r
) + hex(g
) + hex(b
));
568 return c
.length
< 2 ? '0' + c
: c
;
574 Terminal
._colors
= Terminal
.colors
.slice();
576 Terminal
.vcolors
= (function() {
578 , colors
= Terminal
.colors
582 for (; i
< 256; i
++) {
583 color
= parseInt(colors
[i
].substring(1), 16);
585 (color
>> 16) & 0xff,
598 Terminal
.defaults
= {
599 colors
: Terminal
.colors
,
611 // programFeatures: false,
615 Terminal
.options
= {};
617 Terminal
.focus
= null;
619 each(keys(Terminal
.defaults
), function(key
) {
620 Terminal
[key
] = Terminal
.defaults
[key
];
621 Terminal
.options
[key
] = Terminal
.defaults
[key
];
625 * Focus the terminal. Delegates focus handling to the terminal's DOM element.
627 Terminal
.prototype.focus = function() {
628 if (document
.activeElement
=== this.textarea
) {
632 if (this.sendFocus
) {
636 this.element
.classList
.add('focus');
638 this.textarea
.focus();
639 Terminal
.focus
= this;
643 * Binds the desired focus behavior on a given terminal object.
647 Terminal
.bindFocus = function (term
) {
648 on(term
.element
, 'focus', function (ev
) {
649 if (term
.sendFocus
) {
654 Terminal
.focus
= term
;
655 term
.emit('focus', {terminal
: term
});
660 * Blur the terminal. Delegates blur handling to the terminal's DOM element.
662 Terminal
.prototype.blur = function() {
663 if (Terminal
.focus
!== this) {
667 this.element
.classList
.remove('focus');
668 this.cursorState
= 0;
669 this.refresh(this.y
, this.y
);
670 this.textarea
.blur();
671 if (this.sendFocus
) {
672 this.send('\x1b[0]');
674 Terminal
.focus
= null;
678 * Binds the desired blur behavior on a given terminal object.
682 Terminal
.bindBlur = function (term
) {
683 on(term
.element
, 'blur', function (ev
) {
684 if (term
.sendFocus
) {
687 Terminal
.focus
= null;
688 term
.emit('blur', {terminal
: term
});
693 * Initialize default behavior
695 Terminal
.prototype.initGlobal = function() {
696 Terminal
.bindPaste(this);
697 Terminal
.bindKeys(this);
698 Terminal
.bindCopy(this);
702 * Bind to paste event and allow both keyboard and right-click pasting, without having the
703 * contentEditable value set to true.
705 Terminal
.bindPaste = function(term
) {
706 on([term
.textarea
, term
.element
], 'paste', function(ev
) {
707 ev
.stopPropagation();
708 if (ev
.clipboardData
) {
709 var text
= ev
.clipboardData
.getData('text/plain');
711 term
.textarea
.value
= '';
712 return term
.cancel(ev
);
718 * Prepares text copied from terminal selection, to be saved in the clipboard by:
719 * 1. stripping all trailing white spaces
720 * 2. converting all non-breaking spaces to regular spaces
721 * @param {string} text The copied text that needs processing for storing in clipboard
725 Terminal
.prepareCopiedTextForClipboard = function (text
) {
726 var space
= String
.fromCharCode(32),
727 nonBreakingSpace
= String
.fromCharCode(160),
728 allNonBreakingSpaces
= new RegExp(nonBreakingSpace
, 'g'),
729 processedText
= text
.split('\n').map(function (line
) {
731 * Strip all trailing white spaces and convert all non-breaking spaces to regular
734 var processedLine
= line
.replace(/\s+$/g, '').replace(allNonBreakingSpaces
, space
);
736 return processedLine
;
739 return processedText
;
743 * Apply key handling to the terminal
745 Terminal
.bindKeys = function(term
) {
746 on(term
.element
, 'keydown', function(ev
) {
747 if (document
.activeElement
!= this) {
753 on(term
.element
, 'keypress', function(ev
) {
754 if (document
.activeElement
!= this) {
760 on(term
.element
, 'keyup', term
.focus
.bind(term
));
762 on(term
.textarea
, 'keydown', function(ev
) {
766 on(term
.textarea
, 'keypress', function(ev
) {
768 // Truncate the textarea's value, since it is not needed
772 on(term
.textarea
, 'compositionstart', term
.compositionHelper
.compositionstart
.bind(term
.compositionHelper
));
773 on(term
.textarea
, 'compositionupdate', term
.compositionHelper
.compositionupdate
.bind(term
.compositionHelper
));
774 on(term
.textarea
, 'compositionend', term
.compositionHelper
.compositionend
.bind(term
.compositionHelper
));
775 term
.on('refresh', term
.compositionHelper
.updateCompositionElements
.bind(term
.compositionHelper
));
779 * Binds copy functionality to the given terminal.
782 Terminal
.bindCopy = function(term
) {
783 on(term
.element
, 'copy', function(ev
) {
790 * Insert the given row to the terminal or produce a new one
791 * if no row argument is passed. Return the inserted row.
792 * @param {HTMLElement} row (optional) The row to append to the terminal.
794 Terminal
.prototype.insertRow = function (row
) {
795 if (typeof row
!= 'object') {
796 row
= document
.createElement('div');
799 this.rowContainer
.appendChild(row
);
800 this.children
.push(row
);
807 * Opens the terminal within an element.
809 * @param {HTMLElement} parent The element to create the terminal within.
811 Terminal
.prototype.open = function(parent
) {
812 var self
=this, i
=0, div
;
814 this.parent
= parent
|| this.parent
;
817 throw new Error('Terminal requires a parent element.');
821 * Grab global elements
823 this.context
= this.parent
.ownerDocument
.defaultView
;
824 this.document
= this.parent
.ownerDocument
;
825 this.body
= this.document
.getElementsByTagName('body')[0];
830 if (this.context
.navigator
&& this.context
.navigator
.userAgent
) {
831 this.isMSIE
= !!~this.context
.navigator
.userAgent
.indexOf('MSIE');
835 * Find the users platform. We use this to interpret the meta key
836 * and ISO third level shifts.
837 * http://stackoverflow.com/questions/19877924/what-is-the-list-of-possible-values-for-navigator-platform-as-of-today
839 if (this.context
.navigator
&& this.context
.navigator
.platform
) {
840 this.isMac
= contains(
841 this.context
.navigator
.platform
,
842 ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K']
844 this.isIpad
= this.context
.navigator
.platform
=== 'iPad';
845 this.isIphone
= this.context
.navigator
.platform
=== 'iPhone';
846 this.isMSWindows
= contains(
847 this.context
.navigator
.platform
,
848 ['Windows', 'Win16', 'Win32', 'WinCE']
853 * Create main element container
855 this.element
= this.document
.createElement('div');
856 this.element
.classList
.add('terminal');
857 this.element
.classList
.add('xterm');
858 this.element
.classList
.add('xterm-theme-' + this.theme
);
859 this.element
.setAttribute('tabindex', 0);
862 * Create the container that will hold the lines of the terminal and then
863 * produce the lines the lines.
865 this.rowContainer
= document
.createElement('div');
866 this.rowContainer
.classList
.add('xterm-rows');
867 this.element
.appendChild(this.rowContainer
);
871 * Create the container that will hold helpers like the textarea for
872 * capturing DOM Events. Then produce the helpers.
874 this.helperContainer
= document
.createElement('div');
875 this.helperContainer
.classList
.add('xterm-helpers');
876 // TODO: This should probably be inserted once it's filled to prevent an additional layout
877 this.element
.appendChild(this.helperContainer
);
878 this.textarea
= document
.createElement('textarea');
879 this.textarea
.classList
.add('xterm-helper-textarea');
880 this.textarea
.setAttribute('autocorrect', 'off');
881 this.textarea
.setAttribute('autocapitalize', 'off');
882 this.textarea
.setAttribute('spellcheck', 'false');
883 this.textarea
.tabIndex
= 0;
884 this.textarea
.onfocus = function() {
885 self
.emit('focus', {terminal
: self
});
887 this.textarea
.onblur = function() {
888 self
.emit('blur', {terminal
: self
});
890 this.helperContainer
.appendChild(this.textarea
);
892 this.compositionView
= document
.createElement('div');
893 this.compositionView
.classList
.add('composition-view');
894 this.compositionHelper
= new CompositionHelper(this.textarea
, this.compositionView
, this);
895 this.helperContainer
.appendChild(this.compositionView
);
897 for (; i
< this.rows
; i
++) {
900 this.parent
.appendChild(this.element
);
904 this.refresh(0, this.rows
- 1);
906 // Initialize global actions that
907 // need to be taken on the document.
910 // Ensure there is a Terminal.focus.
913 on(this.element
, 'mouseup', function() {
914 var selection
= document
.getSelection(),
915 collapsed
= selection
.isCollapsed
,
916 isRange
= typeof collapsed
== 'boolean' ? !collapsed
: selection
.type
== 'Range';
922 // Listen for mouse events and translate
923 // them into terminal mouse protocols.
926 // Figure out whether boldness affects
927 // the character width of monospace fonts.
928 if (Terminal
.brokenBold
== null) {
929 Terminal
.brokenBold
= isBoldBroken(this.document
);
937 * Attempts to load an add-on using CommonJS or RequireJS (whichever is available).
938 * @param {string} addon The name of the addon to load
941 Terminal
.loadAddon = function(addon
, callback
) {
942 if (typeof exports
=== 'object' && typeof module
=== 'object') {
944 return require(__dirname
+ '/../addons/' + addon
);
945 } else if (typeof define
== 'function') {
947 return require(['../addons/' + addon
+ '/' + addon
], callback
);
949 console
.error('Cannot load a module without a CommonJS or RequireJS environment.');
957 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#Mouse%20Tracking
958 * To better understand these
959 * the xterm code is very helpful:
961 * button.c, charproc.c, misc.c
962 * Relevant functions in xterm/button.c:
963 * BtnCode, EmitButtonCode, EditorButton, SendMousePosition
965 Terminal
.prototype.bindMouse = function() {
966 var el
= this.element
, self
= this, pressed
= 32;
967 var wheelEvent
= ('onmousewheel' in this.context
) ? 'mousewheel' : 'DOMMouseScroll';
969 // mouseup, mousedown, mousewheel
970 // left click: ^[[M 3<^[[M#3<
971 // mousewheel up: ^[[M`3>
972 function sendButton(ev
) {
976 // get the xterm-style button
977 button
= getButton(ev
);
979 // get mouse coordinates
983 sendEvent(button
, pos
);
985 switch (ev
.overrideType
|| ev
.type
) {
990 // keep it at the left
991 // button, just in case.
1002 // motion example of a left click:
1003 // ^[[M 3<^[[M@4<^[[M@5<^[[M@6<^[[M@7<^[[M#7<
1004 function sendMove(ev
) {
1005 var button
= pressed
1008 pos
= getCoords(ev
);
1011 // buttons marked as motions
1012 // are incremented by 32
1015 sendEvent(button
, pos
);
1018 // encode button and
1019 // position to characters
1020 function encode(data
, ch
) {
1021 if (!self
.utfMouse
) {
1022 if (ch
=== 255) return data
.push(0);
1023 if (ch
> 127) ch
= 127;
1026 if (ch
=== 2047) return data
.push(0);
1030 if (ch
> 2047) ch
= 2047;
1031 data
.push(0xC0 | (ch
>> 6));
1032 data
.push(0x80 | (ch
& 0x3F));
1037 // send a mouse event:
1038 // regular/utf8: ^[[M Cb Cx Cy
1039 // urxvt: ^[[ Cb ; Cx ; Cy M
1040 // sgr: ^[[ Cb ; Cx ; Cy M/m
1041 // vt300: ^[[ 24(1/3/5)~ [ Cx , Cy ] \r
1042 // locator: CSI P e ; P b ; P r ; P c ; P p & w
1043 function sendEvent(button
, pos
) {
1044 // self.emit('mouse', {
1050 if (self
.vt300Mouse
) {
1052 // http://www.vt100.net/docs/vt3xx-gp/chapter15.html
1056 var data
= '\x1b[24';
1057 if (button
=== 0) data
+= '1';
1058 else if (button
=== 1) data
+= '3';
1059 else if (button
=== 2) data
+= '5';
1060 else if (button
=== 3) return;
1062 data
+= '~[' + pos
.x
+ ',' + pos
.y
+ ']\r';
1067 if (self
.decLocator
) {
1072 if (button
=== 0) button
= 2;
1073 else if (button
=== 1) button
= 4;
1074 else if (button
=== 2) button
= 6;
1075 else if (button
=== 3) button
= 3;
1079 + (button
=== 3 ? 4 : 0)
1090 if (self
.urxvtMouse
) {
1095 self
.send('\x1b[' + button
+ ';' + pos
.x
+ ';' + pos
.y
+ 'M');
1099 if (self
.sgrMouse
) {
1103 + ((button
& 3) === 3 ? button
& ~3 : button
)
1108 + ((button
& 3) === 3 ? 'm' : 'M'));
1114 encode(data
, button
);
1115 encode(data
, pos
.x
);
1116 encode(data
, pos
.y
);
1118 self
.send('\x1b[M' + String
.fromCharCode
.apply(String
, data
));
1121 function getButton(ev
) {
1134 // 1, and 2 - with 64 added
1135 switch (ev
.overrideType
|| ev
.type
) {
1137 button
= ev
.button
!= null
1144 button
= button
=== 1 ? 0 : button
=== 4 ? 1 : button
;
1150 case 'DOMMouseScroll':
1151 button
= ev
.detail
< 0
1156 button
= ev
.wheelDeltaY
> 0
1162 // next three bits are the modifiers:
1163 // 4 = shift, 8 = meta, 16 = control
1164 shift
= ev
.shiftKey
? 4 : 0;
1165 meta
= ev
.metaKey
? 8 : 0;
1166 ctrl
= ev
.ctrlKey
? 16 : 0;
1167 mod
= shift
| meta
| ctrl
;
1170 if (self
.vt200Mouse
) {
1173 } else if (!self
.normalMouse
) {
1178 button
= (32 + (mod
<< 2)) + button
;
1183 // mouse coordinates measured in cols/rows
1184 function getCoords(ev
) {
1187 // ignore browsers without pageX for now
1188 if (ev
.pageX
== null) return;
1194 // should probably check offsetParent
1195 // but this is more portable
1196 while (el
&& el
!== self
.document
.documentElement
) {
1199 el
= 'offsetParent' in el
1204 // convert to cols/rows
1205 w
= self
.element
.clientWidth
;
1206 h
= self
.element
.clientHeight
;
1207 x
= Math
.round((x
/ w
) * self
.cols
);
1208 y
= Math
.round((y
/ h
) * self
.rows
);
1210 // be sure to avoid sending
1211 // bad positions to the program
1213 if (x
> self
.cols
) x
= self
.cols
;
1215 if (y
> self
.rows
) y
= self
.rows
;
1217 // xterm sends raw bytes and
1218 // starts at 32 (SP) for each.
1225 type
: (ev
.overrideType
|| ev
.type
) === wheelEvent
1227 : (ev
.overrideType
|| ev
.type
)
1231 on(el
, 'mousedown', function(ev
) {
1232 if (!self
.mouseEvents
) return;
1241 //if (self.vt200Mouse && !self.normalMouse) {
1242 if (self
.vt200Mouse
) {
1243 ev
.overrideType
= 'mouseup';
1245 return self
.cancel(ev
);
1249 if (self
.normalMouse
) on(self
.document
, 'mousemove', sendMove
);
1251 // x10 compatibility mode can't send button releases
1252 if (!self
.x10Mouse
) {
1253 on(self
.document
, 'mouseup', function up(ev
) {
1255 if (self
.normalMouse
) off(self
.document
, 'mousemove', sendMove
);
1256 off(self
.document
, 'mouseup', up
);
1257 return self
.cancel(ev
);
1261 return self
.cancel(ev
);
1264 //if (self.normalMouse) {
1265 // on(self.document, 'mousemove', sendMove);
1268 on(el
, wheelEvent
, function(ev
) {
1269 if (!self
.mouseEvents
) return;
1272 || self
.decLocator
) return;
1274 return self
.cancel(ev
);
1277 // allow mousewheel scrolling in
1278 // the shell for example
1279 on(el
, wheelEvent
, function(ev
) {
1280 if (self
.mouseEvents
) return;
1281 if (self
.applicationKeypad
) return;
1282 if (ev
.type
=== 'DOMMouseScroll') {
1283 self
.scrollDisp(ev
.detail
< 0 ? -1 : 1);
1285 self
.scrollDisp(ev
.wheelDeltaY
> 0 ? -1 : 1);
1287 return self
.cancel(ev
);
1292 * Destroys the terminal.
1294 Terminal
.prototype.destroy = function() {
1295 this.readable
= false;
1296 this.writable
= false;
1298 this.handler = function() {};
1299 this.write = function() {};
1300 if (this.element
.parentNode
) {
1301 this.element
.parentNode
.removeChild(this.element
);
1303 //this.emit('close');
1308 * Flags used to render terminal text properly
1319 * Refreshes (re-renders) terminal content within two rows (inclusive)
1323 * In the screen buffer, each character is stored as a an array with a character
1324 * and a 32-bit integer:
1325 * - First value: a utf-16 character.
1327 * - Next 9 bits: background color (0-511).
1328 * - Next 9 bits: foreground color (0-511).
1329 * - Next 14 bits: a mask for misc. flags:
1336 * @param {number} start The row to start from (between 0 and terminal's height terminal - 1)
1337 * @param {number} end The row to end at (between fromRow and terminal's height terminal - 1)
1338 * @param {boolean} queue Whether the refresh should ran right now or be queued
1340 Terminal
.prototype.refresh = function(start
, end
, queue
) {
1343 // queue defaults to true
1344 queue
= (typeof queue
== 'undefined') ? true : queue
;
1347 * The refresh queue allows refresh to execute only approximately 30 times a second. For
1348 * commands that pass a significant amount of output to the write function, this prevents the
1349 * terminal from maxing out the CPU and making the UI unresponsive. While commands can still
1350 * run beyond what they do on the terminal, it is far better with a debounce in place as
1351 * every single terminal manipulation does not need to be constructed in the DOM.
1353 * A side-effect of this is that it makes ^C to interrupt a process seem more responsive.
1356 // If refresh should be queued, order the refresh and return.
1357 if (this._refreshIsQueued
) {
1358 // If a refresh has already been queued, just order a full refresh next
1359 this._fullRefreshNext
= true;
1361 setTimeout(function () {
1362 self
.refresh(start
, end
, false);
1364 this._refreshIsQueued
= true;
1369 // If refresh should be run right now (not be queued), release the lock
1370 this._refreshIsQueued
= false;
1372 // If multiple refreshes were requested, make a full refresh.
1373 if (this._fullRefreshNext
) {
1375 end
= this.rows
- 1;
1376 this._fullRefreshNext
= false // reset lock
1379 var x
, y
, i
, line
, out
, ch
, ch_width
, width
, data
, attr
, bg
, fg
, flags
, row
, parent
, focused
= document
.activeElement
;
1381 // If this is a big refresh, remove the terminal rows from the DOM for faster calculations
1382 if (end
- start
>= this.rows
/ 2) {
1383 parent
= this.element
.parentNode
;
1385 this.element
.removeChild(this.rowContainer
);
1392 if (end
>= this.rows
.length
) {
1393 this.log('`end` is too large. Most likely a bad CSR.');
1394 end
= this.rows
.length
- 1;
1397 for (; y
<= end
; y
++) {
1398 row
= y
+ this.ydisp
;
1400 line
= this.lines
[row
];
1403 if (this.y
=== y
- (this.ybase
- this.ydisp
)
1405 && !this.cursorHidden
) {
1411 attr
= this.defAttr
;
1414 for (; i
< width
; i
++) {
1417 ch_width
= line
[i
][2];
1421 if (i
=== x
) data
= -1;
1423 if (data
!== attr
) {
1424 if (attr
!== this.defAttr
) {
1427 if (data
!== this.defAttr
) {
1429 out
+= '<span class="reverse-video terminal-cursor';
1430 if (this.cursorBlink
) {
1435 var classNames
= [];
1438 fg
= (data
>> 9) & 0x1ff;
1441 if (flags
& Terminal
.flags
.BOLD
) {
1442 if (!Terminal
.brokenBold
) {
1443 classNames
.push('xterm-bold');
1445 // See: XTerm*boldColors
1446 if (fg
< 8) fg
+= 8;
1449 if (flags
& Terminal
.flags
.UNDERLINE
) {
1450 classNames
.push('xterm-underline');
1453 if (flags
& Terminal
.flags
.BLINK
) {
1454 classNames
.push('xterm-blink');
1458 * If inverse flag is on, then swap the foreground and background variables.
1460 if (flags
& Terminal
.flags
.INVERSE
) {
1461 /* One-line variable swap in JavaScript: http://stackoverflow.com/a/16201730 */
1462 bg = [fg, fg = bg][0];
1463 // Should inverse just be before the
1464 // above boldColors effect instead?
1465 if ((flags & 1) && fg < 8) fg += 8;
1468 if (flags & Terminal.flags.INVISIBLE) {
1469 classNames.push('xterm-hidden');
1473 * Weird situation: Invert flag used black foreground and white background results
1474 * in invalid background color, positioned at the 256 index of the 256 terminal
1475 * color map. Pin the colors manually in such a case.
1477 * Source: https://github.com/sourcelair/xterm.js/issues/57
1479 if (flags
& Terminal
.flags
.INVERSE
) {
1489 classNames
.push('xterm-bg-color-' + bg
);
1493 classNames
.push('xterm-color-' + fg
);
1497 if (classNames
.length
) {
1498 out
+= ' class="' + classNames
.join(' ') + '"';
1527 if (attr
!== this.defAttr
) {
1531 this.children
[y
].innerHTML
= out
;
1535 this.element
.appendChild(this.rowContainer
);
1538 this.emit('refresh', {element
: this.element
, start
: start
, end
: end
});
1542 * Display the cursor element
1544 Terminal
.prototype.showCursor = function() {
1545 if (!this.cursorState
) {
1546 this.cursorState
= 1;
1547 this.refresh(this.y
, this.y
);
1552 * Scroll the terminal
1554 Terminal
.prototype.scroll = function() {
1557 if (++this.ybase
=== this.scrollback
) {
1558 this.ybase
= this.ybase
/ 2 | 0;
1559 this.lines
= this.lines
.slice(-(this.ybase
+ this.rows
) + 1);
1562 this.ydisp
= this.ybase
;
1565 row
= this.ybase
+ this.rows
- 1;
1567 // subtract the bottom scroll region
1568 row
-= this.rows
- 1 - this.scrollBottom
;
1570 if (row
=== this.lines
.length
) {
1571 // potential optimization:
1572 // pushing is faster than splicing
1573 // when they amount to the same
1575 this.lines
.push(this.blankLine());
1578 this.lines
.splice(row
, 0, this.blankLine());
1581 if (this.scrollTop
!== 0) {
1582 if (this.ybase
!== 0) {
1584 this.ydisp
= this.ybase
;
1586 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
1590 this.updateRange(this.scrollTop
);
1591 this.updateRange(this.scrollBottom
);
1595 * Scroll the display of the terminal
1596 * @param {number} disp The number of lines to scroll down (negatives scroll up).
1598 Terminal
.prototype.scrollDisp = function(disp
) {
1601 if (this.ydisp
> this.ybase
) {
1602 this.ydisp
= this.ybase
;
1603 } else if (this.ydisp
< 0) {
1607 this.refresh(0, this.rows
- 1);
1611 * Writes text to the terminal.
1612 * @param {string} text The text to write to the terminal.
1614 Terminal
.prototype.write = function(data
) {
1615 var l
= data
.length
, i
= 0, j
, cs
, ch
, code
, low
, ch_width
, row
;
1617 this.refreshStart
= this.y
;
1618 this.refreshEnd
= this.y
;
1620 if (this.ybase
!== this.ydisp
) {
1621 this.ydisp
= this.ybase
;
1625 // apply leftover surrogate high from last write
1626 if (this.surrogate_high
) {
1627 data
= this.surrogate_high
+ data
;
1628 this.surrogate_high
= '';
1631 for (; i
< l
; i
++) {
1634 // FIXME: higher chars than 0xa0 are not allowed in escape sequences
1635 // --> maybe move to default
1636 code
= data
.charCodeAt(i
);
1637 if (0xD800 <= code
&& code
<= 0xDBFF) {
1638 // we got a surrogate high
1639 // get surrogate low (next 2 bytes)
1640 low
= data
.charCodeAt(i
+1);
1642 // end of data stream, save surrogate high
1643 this.surrogate_high
= ch
;
1646 code
= ((code
- 0xD800) * 0x400) + (low
- 0xDC00) + 0x10000;
1647 ch
+= data
.charAt(i
+1);
1649 // surrogate low - already handled above
1650 if (0xDC00 <= code
&& code
<= 0xDFFF)
1653 switch (this.state
) {
1664 if (this.convertEol
) {
1668 if (this.y
> this.scrollBottom
) {
1688 this.x
= this.nextStop();
1703 this.state
= escaped
;
1708 // calculate print space
1709 // expensive call, therefore we save width in line buffer
1710 ch_width
= wcwidth(code
);
1713 if (this.charset
&& this.charset
[ch
]) {
1714 ch
= this.charset
[ch
];
1717 row
= this.y
+ this.ybase
;
1719 // insert combining char in last cell
1720 // FIXME: needs handling after cursor jumps
1721 if (!ch_width
&& this.x
) {
1723 // dont overflow left
1724 if (this.lines
[row
][this.x
-1]) {
1725 if (!this.lines
[row
][this.x
-1][2]) {
1727 // found empty cell after fullwidth, need to go 2 cells back
1728 if (this.lines
[row
][this.x
-2])
1729 this.lines
[row
][this.x
-2][1] += ch
;
1732 this.lines
[row
][this.x
-1][1] += ch
;
1734 this.updateRange(this.y
);
1739 // goto next line if ch would overflow
1740 // TODO: needs a global min terminal width of 2
1741 if (this.x
+ch_width
-1 >= this.cols
) {
1742 // autowrap - DECAWM
1743 if (this.wraparoundMode
) {
1746 if (this.y
> this.scrollBottom
) {
1751 this.x
= this.cols
-1;
1752 if(ch_width
===2) // FIXME: check for xterm behavior
1756 row
= this.y
+ this.ybase
;
1758 // insert mode: move characters to right
1759 if (this.insertMode
) {
1760 // do this twice for a fullwidth char
1761 for (var moves
=0; moves
<ch_width
; ++moves
) {
1762 // remove last cell, if it's width is 0
1763 // we have to adjust the second last cell as well
1764 var removed
= this.lines
[this.y
+ this.ybase
].pop();
1766 && this.lines
[row
][this.cols
-2]
1767 && this.lines
[row
][this.cols
-2][2]===2)
1768 this.lines
[row
][this.cols
-2] = [this.curAttr
, ' ', 1];
1770 // insert empty cell at cursor
1771 this.lines
[row
].splice(this.x
, 0, [this.curAttr
, ' ', 1]);
1775 this.lines
[row
][this.x
] = [this.curAttr
, ch
, ch_width
];
1777 this.updateRange(this.y
);
1779 // fullwidth char - set next cell width to zero and advance cursor
1781 this.lines
[row
][this.x
] = [this.curAttr
, '', 0];
1790 // ESC [ Control Sequence Introducer ( CSI is 0x9b).
1793 this.currentParam
= 0;
1797 // ESC ] Operating System Command ( OSC is 0x9d).
1800 this.currentParam
= 0;
1804 // ESC P Device Control String ( DCS is 0x90).
1807 this.currentParam
= 0;
1811 // ESC _ Application Program Command ( APC is 0x9f).
1813 this.state
= ignore
;
1816 // ESC ^ Privacy Message ( PM is 0x9e).
1818 this.state
= ignore
;
1821 // ESC c Full Reset (RIS).
1826 // ESC E Next Line ( NEL is 0x85).
1827 // ESC D Index ( IND is 0x84).
1835 // ESC M Reverse Index ( RI is 0x8d).
1837 this.reverseIndex();
1840 // ESC % Select default/utf-8 character set.
1841 // @ = default, G = utf-8
1843 //this.charset = null;
1845 this.setgCharset(0, Terminal
.charsets
.US
);
1846 this.state
= normal
;
1850 // ESC (,),*,+,-,. Designate G0-G2 Character Set.
1851 case '(': // <-- this seems to get all the attention
1877 this.state
= charset
;
1880 // Designate G3 Character Set (VT300).
1881 // A = ISO Latin-1 Supplemental.
1885 this.state
= charset
;
1890 // Single Shift Select of G2 Character Set
1891 // ( SS2 is 0x8e). This affects next character only.
1895 // Single Shift Select of G3 Character Set
1896 // ( SS3 is 0x8f). This affects next character only.
1900 // Invoke the G2 Character Set as GL (LS2).
1905 // Invoke the G3 Character Set as GL (LS3).
1910 // Invoke the G3 Character Set as GR (LS3R).
1915 // Invoke the G2 Character Set as GR (LS2R).
1920 // Invoke the G1 Character Set as GR (LS1R).
1925 // ESC 7 Save Cursor (DECSC).
1928 this.state
= normal
;
1931 // ESC 8 Restore Cursor (DECRC).
1933 this.restoreCursor();
1934 this.state
= normal
;
1937 // ESC # 3 DEC line height/width
1939 this.state
= normal
;
1943 // ESC H Tab Set (HTS is 0x88).
1948 // ESC = Application Keypad (DECPAM).
1950 this.log('Serial port requested application keypad.');
1951 this.applicationKeypad
= true;
1952 this.state
= normal
;
1955 // ESC > Normal Keypad (DECPNM).
1957 this.log('Switching back to normal keypad.');
1958 this.applicationKeypad
= false;
1959 this.state
= normal
;
1963 this.state
= normal
;
1964 this.error('Unknown ESC control: %s.', ch
);
1971 case '0': // DEC Special Character and Line Drawing Set.
1972 cs
= Terminal
.charsets
.SCLD
;
1975 cs
= Terminal
.charsets
.UK
;
1977 case 'B': // United States (USASCII).
1978 cs
= Terminal
.charsets
.US
;
1981 cs
= Terminal
.charsets
.Dutch
;
1983 case 'C': // Finnish
1985 cs
= Terminal
.charsets
.Finnish
;
1988 cs
= Terminal
.charsets
.French
;
1990 case 'Q': // FrenchCanadian
1991 cs
= Terminal
.charsets
.FrenchCanadian
;
1994 cs
= Terminal
.charsets
.German
;
1996 case 'Y': // Italian
1997 cs
= Terminal
.charsets
.Italian
;
1999 case 'E': // NorwegianDanish
2001 cs
= Terminal
.charsets
.NorwegianDanish
;
2003 case 'Z': // Spanish
2004 cs
= Terminal
.charsets
.Spanish
;
2006 case 'H': // Swedish
2008 cs
= Terminal
.charsets
.Swedish
;
2011 cs
= Terminal
.charsets
.Swiss
;
2013 case '/': // ISOLatin (actually /A)
2014 cs
= Terminal
.charsets
.ISOLatin
;
2018 cs
= Terminal
.charsets
.US
;
2021 this.setgCharset(this.gcharset
, cs
);
2022 this.gcharset
= null;
2023 this.state
= normal
;
2029 // Set Text Parameters.
2030 if (ch
=== '\x1b' || ch
=== '\x07') {
2031 if (ch
=== '\x1b') i
++;
2033 this.params
.push(this.currentParam
);
2035 switch (this.params
[0]) {
2039 if (this.params
[1]) {
2040 this.title
= this.params
[1];
2041 this.handleTitle(this.title
);
2049 // change dynamic colors
2061 // change dynamic ui colors
2073 // manipulate selection data
2091 this.currentParam
= 0;
2092 this.state
= normal
;
2094 if (!this.params
.length
) {
2095 if (ch
>= '0' && ch
<= '9') {
2097 this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
2098 } else if (ch
=== ';') {
2099 this.params
.push(this.currentParam
);
2100 this.currentParam
= '';
2103 this.currentParam
+= ch
;
2110 if (ch
=== '?' || ch
=== '>' || ch
=== '!') {
2116 if (ch
>= '0' && ch
<= '9') {
2117 this.currentParam
= this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
2121 // '$', '"', ' ', '\''
2122 if (ch
=== '$' || ch
=== '"' || ch
=== ' ' || ch
=== '\'') {
2127 this.params
.push(this.currentParam
);
2128 this.currentParam
= 0;
2131 if (ch
=== ';') break;
2133 this.state
= normal
;
2137 // Cursor Up Ps Times (default = 1) (CUU).
2139 this.cursorUp(this.params
);
2143 // Cursor Down Ps Times (default = 1) (CUD).
2145 this.cursorDown(this.params
);
2149 // Cursor Forward Ps Times (default = 1) (CUF).
2151 this.cursorForward(this.params
);
2155 // Cursor Backward Ps Times (default = 1) (CUB).
2157 this.cursorBackward(this.params
);
2161 // Cursor Position [row;column] (default = [1,1]) (CUP).
2163 this.cursorPos(this.params
);
2166 // CSI Ps J Erase in Display (ED).
2168 this.eraseInDisplay(this.params
);
2171 // CSI Ps K Erase in Line (EL).
2173 this.eraseInLine(this.params
);
2176 // CSI Pm m Character Attributes (SGR).
2179 this.charAttributes(this.params
);
2183 // CSI Ps n Device Status Report (DSR).
2186 this.deviceStatus(this.params
);
2195 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
2197 this.insertChars(this.params
);
2201 // Cursor Next Line Ps Times (default = 1) (CNL).
2203 this.cursorNextLine(this.params
);
2207 // Cursor Preceding Line Ps Times (default = 1) (CNL).
2209 this.cursorPrecedingLine(this.params
);
2213 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
2215 this.cursorCharAbsolute(this.params
);
2219 // Insert Ps Line(s) (default = 1) (IL).
2221 this.insertLines(this.params
);
2225 // Delete Ps Line(s) (default = 1) (DL).
2227 this.deleteLines(this.params
);
2231 // Delete Ps Character(s) (default = 1) (DCH).
2233 this.deleteChars(this.params
);
2237 // Erase Ps Character(s) (default = 1) (ECH).
2239 this.eraseChars(this.params
);
2242 // CSI Pm ` Character Position Absolute
2243 // [column] (default = [row,1]) (HPA).
2245 this.charPosAbsolute(this.params
);
2249 // Horizontal Position Relative
2251 this.HPositionRelative(this.params
);
2255 // Send Device Attributes (Primary DA).
2257 // Send Device Attributes (Secondary DA)
2259 this.sendDeviceAttributes(this.params
);
2263 // Line Position Absolute [row] (default = [1,column]) (VPA).
2265 this.linePosAbsolute(this.params
);
2268 // 145 65 e * VPR - Vertical Position Relative
2270 this.VPositionRelative(this.params
);
2274 // Horizontal and Vertical Position [row;column] (default =
2277 this.HVPosition(this.params
);
2280 // CSI Pm h Set Mode (SM).
2281 // CSI ? Pm h - mouse escape codes, cursor escape codes
2283 this.setMode(this.params
);
2286 // CSI Pm l Reset Mode (RM).
2289 this.resetMode(this.params
);
2293 // Set Scrolling Region [top;bottom] (default = full size of win-
2297 this.setScrollRegion(this.params
);
2301 // Save cursor (ANSI.SYS).
2303 this.saveCursor(this.params
);
2307 // Restore cursor (ANSI.SYS).
2309 this.restoreCursor(this.params
);
2317 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
2319 this.cursorForwardTab(this.params
);
2322 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
2324 this.scrollUp(this.params
);
2327 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
2328 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
2331 // if (this.prefix === '>') {
2332 // this.resetTitleModes(this.params);
2335 // if (this.params.length > 2) {
2336 // this.initMouseTracking(this.params);
2339 if (this.params
.length
< 2 && !this.prefix
) {
2340 this.scrollDown(this.params
);
2345 // Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
2347 this.cursorBackwardTab(this.params
);
2350 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
2352 this.repeatPrecedingCharacter(this.params
);
2355 // CSI Ps g Tab Clear (TBC).
2357 this.tabClear(this.params
);
2360 // CSI Pm i Media Copy (MC).
2363 // this.mediaCopy(this.params);
2366 // CSI Pm m Character Attributes (SGR).
2368 // case 'm': // duplicate
2369 // if (this.prefix === '>') {
2370 // this.setResources(this.params);
2372 // this.charAttributes(this.params);
2376 // CSI Ps n Device Status Report (DSR).
2378 // case 'n': // duplicate
2379 // if (this.prefix === '>') {
2380 // this.disableModifiers(this.params);
2382 // this.deviceStatus(this.params);
2386 // CSI > Ps p Set pointer mode.
2387 // CSI ! p Soft terminal reset (DECSTR).
2389 // Request ANSI mode (DECRQM).
2391 // Request DEC private mode (DECRQM).
2394 switch (this.prefix
) {
2396 // this.setPointerMode(this.params);
2399 this.softReset(this.params
);
2402 // if (this.postfix === '$') {
2403 // this.requestPrivateMode(this.params);
2407 // if (this.postfix === '"') {
2408 // this.setConformanceLevel(this.params);
2409 // } else if (this.postfix === '$') {
2410 // this.requestAnsiMode(this.params);
2416 // CSI Ps q Load LEDs (DECLL).
2420 // if (this.postfix === ' ') {
2421 // this.setCursorStyle(this.params);
2424 // if (this.postfix === '"') {
2425 // this.setCharProtectionAttr(this.params);
2428 // this.loadLEDs(this.params);
2432 // Set Scrolling Region [top;bottom] (default = full size of win-
2435 // CSI Pt; Pl; Pb; Pr; Ps$ r
2436 // case 'r': // duplicate
2437 // if (this.prefix === '?') {
2438 // this.restorePrivateValues(this.params);
2439 // } else if (this.postfix === '$') {
2440 // this.setAttrInRectangle(this.params);
2442 // this.setScrollRegion(this.params);
2446 // CSI s Save cursor (ANSI.SYS).
2448 // case 's': // duplicate
2449 // if (this.prefix === '?') {
2450 // this.savePrivateValues(this.params);
2452 // this.saveCursor(this.params);
2456 // CSI Ps ; Ps ; Ps t
2457 // CSI Pt; Pl; Pb; Pr; Ps$ t
2461 // if (this.postfix === '$') {
2462 // this.reverseAttrInRectangle(this.params);
2463 // } else if (this.postfix === ' ') {
2464 // this.setWarningBellVolume(this.params);
2466 // if (this.prefix === '>') {
2467 // this.setTitleModeFeature(this.params);
2469 // this.manipulateWindow(this.params);
2474 // CSI u Restore cursor (ANSI.SYS).
2476 // case 'u': // duplicate
2477 // if (this.postfix === ' ') {
2478 // this.setMarginBellVolume(this.params);
2480 // this.restoreCursor(this.params);
2484 // CSI Pt; Pl; Pb; Pr; Pp; Pt; Pl; Pp$ v
2486 // if (this.postfix === '$') {
2487 // this.copyRectagle(this.params);
2491 // CSI Pt ; Pl ; Pb ; Pr ' w
2493 // if (this.postfix === '\'') {
2494 // this.enableFilterRectangle(this.params);
2498 // CSI Ps x Request Terminal Parameters (DECREQTPARM).
2499 // CSI Ps x Select Attribute Change Extent (DECSACE).
2500 // CSI Pc; Pt; Pl; Pb; Pr$ x
2502 // if (this.postfix === '$') {
2503 // this.fillRectangle(this.params);
2505 // this.requestParameters(this.params);
2506 // //this.__(this.params);
2511 // CSI Pt; Pl; Pb; Pr$ z
2513 // if (this.postfix === '\'') {
2514 // this.enableLocatorReporting(this.params);
2515 // } else if (this.postfix === '$') {
2516 // this.eraseRectangle(this.params);
2521 // CSI Pt; Pl; Pb; Pr$ {
2523 // if (this.postfix === '\'') {
2524 // this.setLocatorEvents(this.params);
2525 // } else if (this.postfix === '$') {
2526 // this.selectiveEraseRectangle(this.params);
2532 // if (this.postfix === '\'') {
2533 // this.requestLocatorPosition(this.params);
2538 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
2540 // if (this.postfix === ' ') {
2541 // this.insertColumns(this.params);
2546 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
2548 // if (this.postfix === ' ') {
2549 // this.deleteColumns(this.params);
2554 this.error('Unknown CSI code: %s.', ch
);
2563 if (ch
=== '\x1b' || ch
=== '\x07') {
2564 if (ch
=== '\x1b') i
++;
2566 switch (this.prefix
) {
2567 // User-Defined Keys (DECUDK).
2571 // Request Status String (DECRQSS).
2572 // test: echo -e '\eP$q"p\e\\'
2574 var pt
= this.currentParam
2591 + (this.scrollTop
+ 1)
2593 + (this.scrollBottom
+ 1)
2603 this.error('Unknown DCS Pt: %s.', pt
);
2608 this.send('\x1bP' + +valid
+ '$r' + pt
+ '\x1b\\');
2611 // Set Termcap/Terminfo Data (xterm, experimental).
2615 // Request Termcap/Terminfo String (xterm, experimental)
2616 // Regular xterm does not even respond to this sequence.
2617 // This can cause a small glitch in vim.
2618 // test: echo -ne '\eP+q6b64\e\\'
2620 var pt
= this.currentParam
2623 this.send('\x1bP' + +valid
+ '+r' + pt
+ '\x1b\\');
2627 this.error('Unknown DCS prefix: %s.', this.prefix
);
2631 this.currentParam
= 0;
2633 this.state
= normal
;
2634 } else if (!this.currentParam
) {
2635 if (!this.prefix
&& ch
!== '$' && ch
!== '+') {
2636 this.currentParam
= ch
;
2637 } else if (this.prefix
.length
=== 2) {
2638 this.currentParam
= ch
;
2643 this.currentParam
+= ch
;
2649 if (ch
=== '\x1b' || ch
=== '\x07') {
2650 if (ch
=== '\x1b') i
++;
2651 this.state
= normal
;
2657 this.updateRange(this.y
);
2658 this.refresh(this.refreshStart
, this.refreshEnd
);
2662 * Writes text to the terminal, followed by a break line character (\n).
2663 * @param {string} text The text to write to the terminal.
2665 Terminal
.prototype.writeln = function(data
) {
2666 this.write(data
+ '\r\n');
2670 * Handle a keydown event
2672 * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
2673 * @param {KeyboardEvent} ev The keydown event to be handled.
2675 Terminal
.prototype.keyDown = function(ev
) {
2676 if (!this.compositionHelper
.keydown
.bind(this.compositionHelper
)(ev
)) {
2681 var result
= this.evaluateKeyEscapeSequence(ev
);
2683 if (result
.scrollDisp
) {
2684 this.scrollDisp(result
.scrollDisp
);
2685 return this.cancel(ev
);
2688 if (isThirdLevelShift(this, ev
)) {
2692 if (result
.cancel
) {
2693 // The event is canceled at the end already, is this necessary?
2694 this.cancel(ev
, true);
2701 this.emit('keydown', ev
);
2702 this.emit('key', result
.key
, ev
);
2704 this.handler(result
.key
);
2706 return this.cancel(ev
, true);
2710 * Returns an object that determines how a KeyboardEvent should be handled. The key of the
2711 * returned value is the new key code to pass to the PTY.
2713 * Reference: http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
2714 * @param {KeyboardEvent} ev The keyboard event to be translated to key escape sequence.
2716 Terminal
.prototype.evaluateKeyEscapeSequence = function(ev
) {
2718 // Whether to cancel event propogation (NOTE: this may not be needed since the event is
2719 // canceled at the end of keyDown
2721 // The new key even to emit
2723 // The number of characters to scroll, if this is defined it will cancel the event
2724 scrollDisp
: undefined
2726 switch (ev
.keyCode
) {
2730 result
.key
= '\x08'; // ^H
2733 result
.key
= '\x7f'; // ^?
2738 result
.key
= '\x1b[Z';
2742 result
.cancel
= true;
2747 result
.cancel
= true;
2751 result
.key
= '\x1b';
2752 result
.cancel
= true;
2757 result
.key
= '\x1bb' // Jump a word back
2758 result
.cancel
= true;
2762 result
.key
= '\x1b[5D'; // Jump a word back
2765 if (this.applicationCursor
) {
2766 result
.key
= '\x1bOD'; // SS3 as ^[O for 7-bit
2769 result
.key
= '\x1b[D';
2774 result
.key
= '\x1bf' // Jump a word forward
2775 result
.cancel
= true;
2779 result
.key
= '\x1b[5C'; // Jump a word forward
2782 if (this.applicationCursor
) {
2783 result
.key
= '\x1bOC';
2786 result
.key
= '\x1b[C';
2790 if (this.applicationCursor
) {
2791 result
.key
= '\x1bOA';
2795 result
.scrollDisp
= -1;
2797 result
.key
= '\x1b[A';
2802 if (this.applicationCursor
) {
2803 result
.key
= '\x1bOB';
2807 result
.scrollDisp
= 1;
2809 result
.key
= '\x1b[B';
2814 if (!ev
.shiftKey
&& !ev
.ctrlKey
) {
2815 // <Ctrl> or <Shift> + <Insert> are used to
2816 // copy-paste on some systems.
2817 result
.key
= '\x1b[2~';
2821 case 46: result
.key
= '\x1b[3~'; break;
2824 if (this.applicationKeypad
) {
2825 result
.key
= '\x1bOH';
2828 result
.key
= '\x1bOH';
2832 if (this.applicationKeypad
) {
2833 result
.key
= '\x1bOF';
2836 result
.key
= '\x1bOF';
2841 result
.scrollDisp
= -(this.rows
- 1);
2843 result
.key
= '\x1b[5~';
2849 result
.scrollDisp
= this.rows
- 1;
2851 result
.key
= '\x1b[6~';
2855 case 112: result
.key
= '\x1bOP'; break;
2856 case 113: result
.key
= '\x1bOQ'; break;
2857 case 114: result
.key
= '\x1bOR'; break;
2858 case 115: result
.key
= '\x1bOS'; break;
2859 case 116: result
.key
= '\x1b[15~'; break;
2860 case 117: result
.key
= '\x1b[17~'; break;
2861 case 118: result
.key
= '\x1b[18~'; break;
2862 case 119: result
.key
= '\x1b[19~'; break;
2863 case 120: result
.key
= '\x1b[20~'; break;
2864 case 121: result
.key
= '\x1b[21~'; break;
2865 case 122: result
.key
= '\x1b[23~'; break;
2866 case 123: result
.key
= '\x1b[24~'; break;
2869 if (ev
.ctrlKey
&& !ev
.shiftKey
&& !ev
.altKey
&& !ev
.metaKey
) {
2870 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2871 result
.key
= String
.fromCharCode(ev
.keyCode
- 64);
2872 } else if (ev
.keyCode
=== 32) {
2874 result
.key
= String
.fromCharCode(0);
2875 } else if (ev
.keyCode
>= 51 && ev
.keyCode
<= 55) {
2876 // escape, file sep, group sep, record sep, unit sep
2877 result
.key
= String
.fromCharCode(ev
.keyCode
- 51 + 27);
2878 } else if (ev
.keyCode
=== 56) {
2880 result
.key
= String
.fromCharCode(127);
2881 } else if (ev
.keyCode
=== 219) {
2883 result
.key
= String
.fromCharCode(27);
2884 } else if (ev
.keyCode
=== 221) {
2886 result
.key
= String
.fromCharCode(29);
2888 } else if (!this.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) {
2889 // On Mac this is a third level shift. Use <Esc> instead.
2890 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2891 result
.key
= '\x1b' + String
.fromCharCode(ev
.keyCode
+ 32);
2892 } else if (ev
.keyCode
=== 192) {
2893 result
.key
= '\x1b`';
2894 } else if (ev
.keyCode
>= 48 && ev
.keyCode
<= 57) {
2895 result
.key
= '\x1b' + (ev
.keyCode
- 48);
2904 * Set the G level of the terminal
2907 Terminal
.prototype.setgLevel = function(g
) {
2909 this.charset
= this.charsets
[g
];
2913 * Set the charset for the given G level of the terminal
2917 Terminal
.prototype.setgCharset = function(g
, charset
) {
2918 this.charsets
[g
] = charset
;
2919 if (this.glevel
=== g
) {
2920 this.charset
= charset
;
2925 * Handle a keypress event.
2927 * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
2928 * @param {KeyboardEvent} ev The keypress event to be handled.
2930 Terminal
.prototype.keyPress = function(ev
) {
2937 } else if (ev
.which
== null) {
2939 } else if (ev
.which
!== 0 && ev
.charCode
!== 0) {
2946 (ev
.altKey
|| ev
.ctrlKey
|| ev
.metaKey
) && !isThirdLevelShift(this, ev
)
2951 key
= String
.fromCharCode(key
);
2953 this.emit('keypress', key
, ev
);
2954 this.emit('key', key
, ev
);
2962 * Send data for handling to the terminal
2963 * @param {string} data
2965 Terminal
.prototype.send = function(data
) {
2969 setTimeout(function() {
2970 self
.handler(self
.queue
);
2980 * Note: We could do sweet things with webaudio here
2982 Terminal
.prototype.bell = function() {
2983 if (!this.visualBell
) return;
2985 this.element
.style
.borderColor
= 'white';
2986 setTimeout(function() {
2987 self
.element
.style
.borderColor
= '';
2989 if (this.popOnBell
) this.focus();
2993 * Log the current state to the console.
2995 Terminal
.prototype.log = function() {
2996 if (!this.debug
) return;
2997 if (!this.context
.console
|| !this.context
.console
.log
) return;
2998 var args
= Array
.prototype.slice
.call(arguments
);
2999 this.context
.console
.log
.apply(this.context
.console
, args
);
3003 * Log the current state as error to the console.
3005 Terminal
.prototype.error = function() {
3006 if (!this.debug
) return;
3007 if (!this.context
.console
|| !this.context
.console
.error
) return;
3008 var args
= Array
.prototype.slice
.call(arguments
);
3009 this.context
.console
.error
.apply(this.context
.console
, args
);
3013 * Resizes the terminal.
3015 * @param {number} x The number of columns to resize to.
3016 * @param {number} y The number of rows to resize to.
3018 Terminal
.prototype.resize = function(x
, y
) {
3026 if (x
=== this.cols
&& y
=== this.rows
) {
3036 ch
= [this.defAttr
, ' ', 1]; // does xterm use the default attr?
3037 i
= this.lines
.length
;
3039 while (this.lines
[i
].length
< x
) {
3040 this.lines
[i
].push(ch
);
3044 i
= this.lines
.length
;
3046 while (this.lines
[i
].length
> x
) {
3047 this.lines
[i
].pop();
3060 // y is rows, not this.y
3061 if (this.lines
.length
< y
+ this.ybase
) {
3062 if (this.ybase
> 0 && this.lines
.length
<= this.ybase
+ this.y
+ addToY
+ 1) {
3063 // There is room above the buffer and there are no empty elements below the line,
3067 if (this.ydisp
> 0) {
3068 // Viewport is at the top of the buffer, must increase downwards
3072 // Add a blank line if there is no buffer left at the top to scroll to, or if there
3073 // are blank lines after the cursor
3074 this.lines
.push(this.blankLine());
3077 if (this.children
.length
< y
) {
3083 if (this.lines
.length
> y
+ this.ybase
) {
3084 if (this.lines
.length
> this.ybase
+ this.y
+ 1) {
3085 // The line is a blank line below the cursor, remove it
3088 // The line is the cursor, scroll down
3093 if (this.children
.length
> y
) {
3094 el
= this.children
.shift();
3096 el
.parentNode
.removeChild(el
);
3103 * Make sure that the cursor stays on screen
3117 this.scrollBottom
= y
- 1;
3119 this.refresh(0, this.rows
- 1);
3123 this.emit('resize', {terminal
: this, cols
: x
, rows
: y
});
3127 * Updates the range of rows to refresh
3128 * @param {number} y The number of rows to refresh next.
3130 Terminal
.prototype.updateRange = function(y
) {
3131 if (y
< this.refreshStart
) this.refreshStart
= y
;
3132 if (y
> this.refreshEnd
) this.refreshEnd
= y
;
3133 // if (y > this.refreshEnd) {
3134 // this.refreshEnd = y;
3135 // if (y > this.rows - 1) {
3136 // this.refreshEnd = this.rows - 1;
3142 * Set the range of refreshing to the maximyum value
3144 Terminal
.prototype.maxRange = function() {
3145 this.refreshStart
= 0;
3146 this.refreshEnd
= this.rows
- 1;
3152 * Setup the tab stops.
3155 Terminal
.prototype.setupStops = function(i
) {
3157 if (!this.tabs
[i
]) {
3158 i
= this.prevStop(i
);
3165 for (; i
< this.cols
; i
+= 8) {
3166 this.tabs
[i
] = true;
3172 * Move the cursor to the previous tab stop from the given position (default is current).
3173 * @param {number} x The position to move the cursor to the previous tab stop.
3175 Terminal
.prototype.prevStop = function(x
) {
3176 if (x
== null) x
= this.x
;
3177 while (!this.tabs
[--x
] && x
> 0);
3178 return x
>= this.cols
3185 * Move the cursor one tab stop forward from the given position (default is current).
3186 * @param {number} x The position to move the cursor one tab stop forward.
3188 Terminal
.prototype.nextStop = function(x
) {
3189 if (x
== null) x
= this.x
;
3190 while (!this.tabs
[++x
] && x
< this.cols
);
3191 return x
>= this.cols
3198 * Erase in the identified line everything from "x" to the end of the line (right).
3199 * @param {number} x The column from which to start erasing to the end of the line.
3200 * @param {number} y The line in which to operate.
3202 Terminal
.prototype.eraseRight = function(x
, y
) {
3203 var line
= this.lines
[this.ybase
+ y
]
3204 , ch
= [this.eraseAttr(), ' ', 1]; // xterm
3207 for (; x
< this.cols
; x
++) {
3211 this.updateRange(y
);
3217 * Erase in the identified line everything from "x" to the start of the line (left).
3218 * @param {number} x The column from which to start erasing to the start of the line.
3219 * @param {number} y The line in which to operate.
3221 Terminal
.prototype.eraseLeft = function(x
, y
) {
3222 var line
= this.lines
[this.ybase
+ y
]
3223 , ch
= [this.eraseAttr(), ' ', 1]; // xterm
3226 while (x
--) line
[x
] = ch
;
3228 this.updateRange(y
);
3233 * Erase all content in the given line
3234 * @param {number} y The line to erase all of its contents.
3236 Terminal
.prototype.eraseLine = function(y
) {
3237 this.eraseRight(0, y
);
3242 * Return the data array of a blank line/
3243 * @param {number} cur First bunch of data for each "blank" character.
3245 Terminal
.prototype.blankLine = function(cur
) {
3250 var ch
= [attr
, ' ', 1] // width defaults to 1 halfwidth character
3254 for (; i
< this.cols
; i
++) {
3263 * If cur return the back color xterm feature attribute. Else return defAttr.
3264 * @param {object} cur
3266 Terminal
.prototype.ch = function(cur
) {
3268 ? [this.eraseAttr(), ' ', 1]
3269 : [this.defAttr
, ' ', 1];
3274 * Evaluate if the current erminal is the given argument.
3275 * @param {object} term The terminal to evaluate
3277 Terminal
.prototype.is = function(term
) {
3278 var name
= this.termName
;
3279 return (name
+ '').indexOf(term
) === 0;
3284 * Emit the 'data' event and populate the given data.
3285 * @param {string} data The data to populate in the event.
3287 Terminal
.prototype.handler = function(data
) {
3288 this.emit('data', data
);
3293 * Emit the 'title' event and populate the given title.
3294 * @param {string} title The title to populate in the event.
3296 Terminal
.prototype.handleTitle = function(title
) {
3297 this.emit('title', title
);
3306 * ESC D Index (IND is 0x84).
3308 Terminal
.prototype.index = function() {
3310 if (this.y
> this.scrollBottom
) {
3314 this.state
= normal
;
3319 * ESC M Reverse Index (RI is 0x8d).
3321 Terminal
.prototype.reverseIndex = function() {
3324 if (this.y
< this.scrollTop
) {
3326 // possibly move the code below to term.reverseScroll();
3327 // test: echo -ne '\e[1;1H\e[44m\eM\e[0m'
3328 // blankLine(true) is xterm/linux behavior
3329 this.lines
.splice(this.y
+ this.ybase
, 0, this.blankLine(true));
3330 j
= this.rows
- 1 - this.scrollBottom
;
3331 this.lines
.splice(this.rows
- 1 + this.ybase
- j
+ 1, 1);
3333 this.updateRange(this.scrollTop
);
3334 this.updateRange(this.scrollBottom
);
3336 this.state
= normal
;
3341 * ESC c Full Reset (RIS).
3343 Terminal
.prototype.reset = function() {
3344 this.options
.rows
= this.rows
;
3345 this.options
.cols
= this.cols
;
3346 Terminal
.call(this, this.options
);
3347 this.refresh(0, this.rows
- 1);
3352 * ESC H Tab Set (HTS is 0x88).
3354 Terminal
.prototype.tabSet = function() {
3355 this.tabs
[this.x
] = true;
3356 this.state
= normal
;
3366 * Cursor Up Ps Times (default = 1) (CUU).
3368 Terminal
.prototype.cursorUp = function(params
) {
3369 var param
= params
[0];
3370 if (param
< 1) param
= 1;
3372 if (this.y
< 0) this.y
= 0;
3378 * Cursor Down Ps Times (default = 1) (CUD).
3380 Terminal
.prototype.cursorDown = function(params
) {
3381 var param
= params
[0];
3382 if (param
< 1) param
= 1;
3384 if (this.y
>= this.rows
) {
3385 this.y
= this.rows
- 1;
3392 * Cursor Forward Ps Times (default = 1) (CUF).
3394 Terminal
.prototype.cursorForward = function(params
) {
3395 var param
= params
[0];
3396 if (param
< 1) param
= 1;
3398 if (this.x
>= this.cols
) {
3399 this.x
= this.cols
- 1;
3406 * Cursor Backward Ps Times (default = 1) (CUB).
3408 Terminal
.prototype.cursorBackward = function(params
) {
3409 var param
= params
[0];
3410 if (param
< 1) param
= 1;
3412 if (this.x
< 0) this.x
= 0;
3418 * Cursor Position [row;column] (default = [1,1]) (CUP).
3420 Terminal
.prototype.cursorPos = function(params
) {
3423 row
= params
[0] - 1;
3425 if (params
.length
>= 2) {
3426 col
= params
[1] - 1;
3433 } else if (row
>= this.rows
) {
3434 row
= this.rows
- 1;
3439 } else if (col
>= this.cols
) {
3440 col
= this.cols
- 1;
3449 * CSI Ps J Erase in Display (ED).
3450 * Ps = 0 -> Erase Below (default).
3451 * Ps = 1 -> Erase Above.
3452 * Ps = 2 -> Erase All.
3453 * Ps = 3 -> Erase Saved Lines (xterm).
3455 * Erase in Display (DECSED).
3456 * Ps = 0 -> Selective Erase Below (default).
3457 * Ps = 1 -> Selective Erase Above.
3458 * Ps = 2 -> Selective Erase All.
3460 Terminal
.prototype.eraseInDisplay = function(params
) {
3462 switch (params
[0]) {
3464 this.eraseRight(this.x
, this.y
);
3466 for (; j
< this.rows
; j
++) {
3471 this.eraseLeft(this.x
, this.y
);
3479 while (j
--) this.eraseLine(j
);
3489 * CSI Ps K Erase in Line (EL).
3490 * Ps = 0 -> Erase to Right (default).
3491 * Ps = 1 -> Erase to Left.
3492 * Ps = 2 -> Erase All.
3494 * Erase in Line (DECSEL).
3495 * Ps = 0 -> Selective Erase to Right (default).
3496 * Ps = 1 -> Selective Erase to Left.
3497 * Ps = 2 -> Selective Erase All.
3499 Terminal
.prototype.eraseInLine = function(params
) {
3500 switch (params
[0]) {
3502 this.eraseRight(this.x
, this.y
);
3505 this.eraseLeft(this.x
, this.y
);
3508 this.eraseLine(this.y
);
3515 * CSI Pm m Character Attributes (SGR).
3516 * Ps = 0 -> Normal (default).
3518 * Ps = 4 -> Underlined.
3519 * Ps = 5 -> Blink (appears as Bold).
3520 * Ps = 7 -> Inverse.
3521 * Ps = 8 -> Invisible, i.e., hidden (VT300).
3522 * Ps = 2 2 -> Normal (neither bold nor faint).
3523 * Ps = 2 4 -> Not underlined.
3524 * Ps = 2 5 -> Steady (not blinking).
3525 * Ps = 2 7 -> Positive (not inverse).
3526 * Ps = 2 8 -> Visible, i.e., not hidden (VT300).
3527 * Ps = 3 0 -> Set foreground color to Black.
3528 * Ps = 3 1 -> Set foreground color to Red.
3529 * Ps = 3 2 -> Set foreground color to Green.
3530 * Ps = 3 3 -> Set foreground color to Yellow.
3531 * Ps = 3 4 -> Set foreground color to Blue.
3532 * Ps = 3 5 -> Set foreground color to Magenta.
3533 * Ps = 3 6 -> Set foreground color to Cyan.
3534 * Ps = 3 7 -> Set foreground color to White.
3535 * Ps = 3 9 -> Set foreground color to default (original).
3536 * Ps = 4 0 -> Set background color to Black.
3537 * Ps = 4 1 -> Set background color to Red.
3538 * Ps = 4 2 -> Set background color to Green.
3539 * Ps = 4 3 -> Set background color to Yellow.
3540 * Ps = 4 4 -> Set background color to Blue.
3541 * Ps = 4 5 -> Set background color to Magenta.
3542 * Ps = 4 6 -> Set background color to Cyan.
3543 * Ps = 4 7 -> Set background color to White.
3544 * Ps = 4 9 -> Set background color to default (original).
3546 * If 16-color support is compiled, the following apply. Assume
3547 * that xterm's resources are set so that the ISO color codes are
3548 * the first 8 of a set of 16. Then the aixterm colors are the
3549 * bright versions of the ISO colors:
3550 * Ps = 9 0 -> Set foreground color to Black.
3551 * Ps = 9 1 -> Set foreground color to Red.
3552 * Ps = 9 2 -> Set foreground color to Green.
3553 * Ps = 9 3 -> Set foreground color to Yellow.
3554 * Ps = 9 4 -> Set foreground color to Blue.
3555 * Ps = 9 5 -> Set foreground color to Magenta.
3556 * Ps = 9 6 -> Set foreground color to Cyan.
3557 * Ps = 9 7 -> Set foreground color to White.
3558 * Ps = 1 0 0 -> Set background color to Black.
3559 * Ps = 1 0 1 -> Set background color to Red.
3560 * Ps = 1 0 2 -> Set background color to Green.
3561 * Ps = 1 0 3 -> Set background color to Yellow.
3562 * Ps = 1 0 4 -> Set background color to Blue.
3563 * Ps = 1 0 5 -> Set background color to Magenta.
3564 * Ps = 1 0 6 -> Set background color to Cyan.
3565 * Ps = 1 0 7 -> Set background color to White.
3567 * If xterm is compiled with the 16-color support disabled, it
3568 * supports the following, from rxvt:
3569 * Ps = 1 0 0 -> Set foreground and background color to
3572 * If 88- or 256-color support is compiled, the following apply.
3573 * Ps = 3 8 ; 5 ; Ps -> Set foreground color to the second
3575 * Ps = 4 8 ; 5 ; Ps -> Set background color to the second
3578 Terminal
.prototype.charAttributes = function(params
) {
3579 // Optimize a single SGR0.
3580 if (params
.length
=== 1 && params
[0] === 0) {
3581 this.curAttr
= this.defAttr
;
3585 var l
= params
.length
3587 , flags
= this.curAttr
>> 18
3588 , fg
= (this.curAttr
>> 9) & 0x1ff
3589 , bg
= this.curAttr
& 0x1ff
3592 for (; i
< l
; i
++) {
3594 if (p
>= 30 && p
<= 37) {
3597 } else if (p
>= 40 && p
<= 47) {
3600 } else if (p
>= 90 && p
<= 97) {
3604 } else if (p
>= 100 && p
<= 107) {
3608 } else if (p
=== 0) {
3610 flags
= this.defAttr
>> 18;
3611 fg
= (this.defAttr
>> 9) & 0x1ff;
3612 bg
= this.defAttr
& 0x1ff;
3616 } else if (p
=== 1) {
3619 } else if (p
=== 4) {
3622 } else if (p
=== 5) {
3625 } else if (p
=== 7) {
3626 // inverse and positive
3627 // test with: echo -e '\e[31m\e[42mhello\e[7mworld\e[27mhi\e[m'
3629 } else if (p
=== 8) {
3632 } else if (p
=== 22) {
3635 } else if (p
=== 24) {
3638 } else if (p
=== 25) {
3641 } else if (p
=== 27) {
3644 } else if (p
=== 28) {
3647 } else if (p
=== 39) {
3649 fg
= (this.defAttr
>> 9) & 0x1ff;
3650 } else if (p
=== 49) {
3652 bg
= this.defAttr
& 0x1ff;
3653 } else if (p
=== 38) {
3655 if (params
[i
+ 1] === 2) {
3659 params
[i
+ 1] & 0xff,
3660 params
[i
+ 2] & 0xff);
3661 if (fg
=== -1) fg
= 0x1ff;
3663 } else if (params
[i
+ 1] === 5) {
3665 p
= params
[i
] & 0xff;
3668 } else if (p
=== 48) {
3670 if (params
[i
+ 1] === 2) {
3674 params
[i
+ 1] & 0xff,
3675 params
[i
+ 2] & 0xff);
3676 if (bg
=== -1) bg
= 0x1ff;
3678 } else if (params
[i
+ 1] === 5) {
3680 p
= params
[i
] & 0xff;
3683 } else if (p
=== 100) {
3685 fg
= (this.defAttr
>> 9) & 0x1ff;
3686 bg
= this.defAttr
& 0x1ff;
3688 this.error('Unknown SGR attribute: %d.', p
);
3692 this.curAttr
= (flags
<< 18) | (fg
<< 9) | bg
;
3697 * CSI Ps n Device Status Report (DSR).
3698 * Ps = 5 -> Status Report. Result (``OK'') is
3700 * Ps = 6 -> Report Cursor Position (CPR) [row;column].
3704 * Device Status Report (DSR, DEC-specific).
3705 * Ps = 6 -> Report Cursor Position (CPR) [row;column] as CSI
3706 * ? r ; c R (assumes page is zero).
3707 * Ps = 1 5 -> Report Printer status as CSI ? 1 0 n (ready).
3708 * or CSI ? 1 1 n (not ready).
3709 * Ps = 2 5 -> Report UDK status as CSI ? 2 0 n (unlocked)
3710 * or CSI ? 2 1 n (locked).
3711 * Ps = 2 6 -> Report Keyboard status as
3712 * CSI ? 2 7 ; 1 ; 0 ; 0 n (North American).
3713 * The last two parameters apply to VT400 & up, and denote key-
3714 * board ready and LK01 respectively.
3715 * Ps = 5 3 -> Report Locator status as
3716 * CSI ? 5 3 n Locator available, if compiled-in, or
3717 * CSI ? 5 0 n No Locator, if not.
3719 Terminal
.prototype.deviceStatus = function(params
) {
3721 switch (params
[0]) {
3724 this.send('\x1b[0n');
3735 } else if (this.prefix
=== '?') {
3736 // modern xterm doesnt seem to
3737 // respond to any of these except ?6, 6, and 5
3738 switch (params
[0]) {
3749 // this.send('\x1b[?11n');
3752 // dont support user defined keys
3753 // this.send('\x1b[?21n');
3756 // north american keyboard
3757 // this.send('\x1b[?27;1;0;0n');
3760 // no dec locator/mouse
3761 // this.send('\x1b[?50n');
3774 * Insert Ps (Blank) Character(s) (default = 1) (ICH).
3776 Terminal
.prototype.insertChars = function(params
) {
3777 var param
, row
, j
, ch
;
3780 if (param
< 1) param
= 1;
3782 row
= this.y
+ this.ybase
;
3784 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3786 while (param
-- && j
< this.cols
) {
3787 this.lines
[row
].splice(j
++, 0, ch
);
3788 this.lines
[row
].pop();
3794 * Cursor Next Line Ps Times (default = 1) (CNL).
3795 * same as CSI Ps B ?
3797 Terminal
.prototype.cursorNextLine = function(params
) {
3798 var param
= params
[0];
3799 if (param
< 1) param
= 1;
3801 if (this.y
>= this.rows
) {
3802 this.y
= this.rows
- 1;
3810 * Cursor Preceding Line Ps Times (default = 1) (CNL).
3813 Terminal
.prototype.cursorPrecedingLine = function(params
) {
3814 var param
= params
[0];
3815 if (param
< 1) param
= 1;
3817 if (this.y
< 0) this.y
= 0;
3824 * Cursor Character Absolute [column] (default = [row,1]) (CHA).
3826 Terminal
.prototype.cursorCharAbsolute = function(params
) {
3827 var param
= params
[0];
3828 if (param
< 1) param
= 1;
3835 * Insert Ps Line(s) (default = 1) (IL).
3837 Terminal
.prototype.insertLines = function(params
) {
3841 if (param
< 1) param
= 1;
3842 row
= this.y
+ this.ybase
;
3844 j
= this.rows
- 1 - this.scrollBottom
;
3845 j
= this.rows
- 1 + this.ybase
- j
+ 1;
3848 // test: echo -e '\e[44m\e[1L\e[0m'
3849 // blankLine(true) - xterm/linux behavior
3850 this.lines
.splice(row
, 0, this.blankLine(true));
3851 this.lines
.splice(j
, 1);
3855 this.updateRange(this.y
);
3856 this.updateRange(this.scrollBottom
);
3862 * Delete Ps Line(s) (default = 1) (DL).
3864 Terminal
.prototype.deleteLines = function(params
) {
3868 if (param
< 1) param
= 1;
3869 row
= this.y
+ this.ybase
;
3871 j
= this.rows
- 1 - this.scrollBottom
;
3872 j
= this.rows
- 1 + this.ybase
- j
;
3875 // test: echo -e '\e[44m\e[1M\e[0m'
3876 // blankLine(true) - xterm/linux behavior
3877 this.lines
.splice(j
+ 1, 0, this.blankLine(true));
3878 this.lines
.splice(row
, 1);
3882 this.updateRange(this.y
);
3883 this.updateRange(this.scrollBottom
);
3889 * Delete Ps Character(s) (default = 1) (DCH).
3891 Terminal
.prototype.deleteChars = function(params
) {
3895 if (param
< 1) param
= 1;
3897 row
= this.y
+ this.ybase
;
3898 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3901 this.lines
[row
].splice(this.x
, 1);
3902 this.lines
[row
].push(ch
);
3908 * Erase Ps Character(s) (default = 1) (ECH).
3910 Terminal
.prototype.eraseChars = function(params
) {
3911 var param
, row
, j
, ch
;
3914 if (param
< 1) param
= 1;
3916 row
= this.y
+ this.ybase
;
3918 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3920 while (param
-- && j
< this.cols
) {
3921 this.lines
[row
][j
++] = ch
;
3926 * CSI Pm ` Character Position Absolute
3927 * [column] (default = [row,1]) (HPA).
3929 Terminal
.prototype.charPosAbsolute = function(params
) {
3930 var param
= params
[0];
3931 if (param
< 1) param
= 1;
3933 if (this.x
>= this.cols
) {
3934 this.x
= this.cols
- 1;
3941 * Horizontal Position Relative
3944 Terminal
.prototype.HPositionRelative = function(params
) {
3945 var param
= params
[0];
3946 if (param
< 1) param
= 1;
3948 if (this.x
>= this.cols
) {
3949 this.x
= this.cols
- 1;
3955 * CSI Ps c Send Device Attributes (Primary DA).
3956 * Ps = 0 or omitted -> request attributes from terminal. The
3957 * response depends on the decTerminalID resource setting.
3958 * -> CSI ? 1 ; 2 c (``VT100 with Advanced Video Option'')
3959 * -> CSI ? 1 ; 0 c (``VT101 with No Options'')
3960 * -> CSI ? 6 c (``VT102'')
3961 * -> CSI ? 6 0 ; 1 ; 2 ; 6 ; 8 ; 9 ; 1 5 ; c (``VT220'')
3962 * The VT100-style response parameters do not mean anything by
3963 * themselves. VT220 parameters do, telling the host what fea-
3964 * tures the terminal supports:
3965 * Ps = 1 -> 132-columns.
3966 * Ps = 2 -> Printer.
3967 * Ps = 6 -> Selective erase.
3968 * Ps = 8 -> User-defined keys.
3969 * Ps = 9 -> National replacement character sets.
3970 * Ps = 1 5 -> Technical characters.
3971 * Ps = 2 2 -> ANSI color, e.g., VT525.
3972 * Ps = 2 9 -> ANSI text locator (i.e., DEC Locator mode).
3974 * Send Device Attributes (Secondary DA).
3975 * Ps = 0 or omitted -> request the terminal's identification
3976 * code. The response depends on the decTerminalID resource set-
3977 * ting. It should apply only to VT220 and up, but xterm extends
3979 * -> CSI > Pp ; Pv ; Pc c
3980 * where Pp denotes the terminal type
3981 * Pp = 0 -> ``VT100''.
3982 * Pp = 1 -> ``VT220''.
3983 * and Pv is the firmware version (for xterm, this was originally
3984 * the XFree86 patch number, starting with 95). In a DEC termi-
3985 * nal, Pc indicates the ROM cartridge registration number and is
3988 * xterm/charproc.c - line 2012, for more information.
3989 * vim responds with ^[[?0c or ^[[?1c after the terminal's response (?)
3991 Terminal
.prototype.sendDeviceAttributes = function(params
) {
3992 if (params
[0] > 0) return;
3995 if (this.is('xterm')
3996 || this.is('rxvt-unicode')
3997 || this.is('screen')) {
3998 this.send('\x1b[?1;2c');
3999 } else if (this.is('linux')) {
4000 this.send('\x1b[?6c');
4002 } else if (this.prefix
=== '>') {
4004 // seem to spit this
4005 // out around ~370 times (?).
4006 if (this.is('xterm')) {
4007 this.send('\x1b[>0;276;0c');
4008 } else if (this.is('rxvt-unicode')) {
4009 this.send('\x1b[>85;95;0c');
4010 } else if (this.is('linux')) {
4011 // not supported by linux console.
4012 // linux console echoes parameters.
4013 this.send(params
[0] + 'c');
4014 } else if (this.is('screen')) {
4015 this.send('\x1b[>83;40003;0c');
4023 * Line Position Absolute [row] (default = [1,column]) (VPA).
4025 Terminal
.prototype.linePosAbsolute = function(params
) {
4026 var param
= params
[0];
4027 if (param
< 1) param
= 1;
4029 if (this.y
>= this.rows
) {
4030 this.y
= this.rows
- 1;
4036 * 145 65 e * VPR - Vertical Position Relative
4039 Terminal
.prototype.VPositionRelative = function(params
) {
4040 var param
= params
[0];
4041 if (param
< 1) param
= 1;
4043 if (this.y
>= this.rows
) {
4044 this.y
= this.rows
- 1;
4051 * Horizontal and Vertical Position [row;column] (default =
4054 Terminal
.prototype.HVPosition = function(params
) {
4055 if (params
[0] < 1) params
[0] = 1;
4056 if (params
[1] < 1) params
[1] = 1;
4058 this.y
= params
[0] - 1;
4059 if (this.y
>= this.rows
) {
4060 this.y
= this.rows
- 1;
4063 this.x
= params
[1] - 1;
4064 if (this.x
>= this.cols
) {
4065 this.x
= this.cols
- 1;
4071 * CSI Pm h Set Mode (SM).
4072 * Ps = 2 -> Keyboard Action Mode (AM).
4073 * Ps = 4 -> Insert Mode (IRM).
4074 * Ps = 1 2 -> Send/receive (SRM).
4075 * Ps = 2 0 -> Automatic Newline (LNM).
4077 * DEC Private Mode Set (DECSET).
4078 * Ps = 1 -> Application Cursor Keys (DECCKM).
4079 * Ps = 2 -> Designate USASCII for character sets G0-G3
4080 * (DECANM), and set VT100 mode.
4081 * Ps = 3 -> 132 Column Mode (DECCOLM).
4082 * Ps = 4 -> Smooth (Slow) Scroll (DECSCLM).
4083 * Ps = 5 -> Reverse Video (DECSCNM).
4084 * Ps = 6 -> Origin Mode (DECOM).
4085 * Ps = 7 -> Wraparound Mode (DECAWM).
4086 * Ps = 8 -> Auto-repeat Keys (DECARM).
4087 * Ps = 9 -> Send Mouse X & Y on button press. See the sec-
4088 * tion Mouse Tracking.
4089 * Ps = 1 0 -> Show toolbar (rxvt).
4090 * Ps = 1 2 -> Start Blinking Cursor (att610).
4091 * Ps = 1 8 -> Print form feed (DECPFF).
4092 * Ps = 1 9 -> Set print extent to full screen (DECPEX).
4093 * Ps = 2 5 -> Show Cursor (DECTCEM).
4094 * Ps = 3 0 -> Show scrollbar (rxvt).
4095 * Ps = 3 5 -> Enable font-shifting functions (rxvt).
4096 * Ps = 3 8 -> Enter Tektronix Mode (DECTEK).
4097 * Ps = 4 0 -> Allow 80 -> 132 Mode.
4098 * Ps = 4 1 -> more(1) fix (see curses resource).
4099 * Ps = 4 2 -> Enable Nation Replacement Character sets (DECN-
4101 * Ps = 4 4 -> Turn On Margin Bell.
4102 * Ps = 4 5 -> Reverse-wraparound Mode.
4103 * Ps = 4 6 -> Start Logging. This is normally disabled by a
4104 * compile-time option.
4105 * Ps = 4 7 -> Use Alternate Screen Buffer. (This may be dis-
4106 * abled by the titeInhibit resource).
4107 * Ps = 6 6 -> Application keypad (DECNKM).
4108 * Ps = 6 7 -> Backarrow key sends backspace (DECBKM).
4109 * Ps = 1 0 0 0 -> Send Mouse X & Y on button press and
4110 * release. See the section Mouse Tracking.
4111 * Ps = 1 0 0 1 -> Use Hilite Mouse Tracking.
4112 * Ps = 1 0 0 2 -> Use Cell Motion Mouse Tracking.
4113 * Ps = 1 0 0 3 -> Use All Motion Mouse Tracking.
4114 * Ps = 1 0 0 4 -> Send FocusIn/FocusOut events.
4115 * Ps = 1 0 0 5 -> Enable Extended Mouse Mode.
4116 * Ps = 1 0 1 0 -> Scroll to bottom on tty output (rxvt).
4117 * Ps = 1 0 1 1 -> Scroll to bottom on key press (rxvt).
4118 * Ps = 1 0 3 4 -> Interpret "meta" key, sets eighth bit.
4119 * (enables the eightBitInput resource).
4120 * Ps = 1 0 3 5 -> Enable special modifiers for Alt and Num-
4121 * Lock keys. (This enables the numLock resource).
4122 * Ps = 1 0 3 6 -> Send ESC when Meta modifies a key. (This
4123 * enables the metaSendsEscape resource).
4124 * Ps = 1 0 3 7 -> Send DEL from the editing-keypad Delete
4126 * Ps = 1 0 3 9 -> Send ESC when Alt modifies a key. (This
4127 * enables the altSendsEscape resource).
4128 * Ps = 1 0 4 0 -> Keep selection even if not highlighted.
4129 * (This enables the keepSelection resource).
4130 * Ps = 1 0 4 1 -> Use the CLIPBOARD selection. (This enables
4131 * the selectToClipboard resource).
4132 * Ps = 1 0 4 2 -> Enable Urgency window manager hint when
4133 * Control-G is received. (This enables the bellIsUrgent
4135 * Ps = 1 0 4 3 -> Enable raising of the window when Control-G
4136 * is received. (enables the popOnBell resource).
4137 * Ps = 1 0 4 7 -> Use Alternate Screen Buffer. (This may be
4138 * disabled by the titeInhibit resource).
4139 * Ps = 1 0 4 8 -> Save cursor as in DECSC. (This may be dis-
4140 * abled by the titeInhibit resource).
4141 * Ps = 1 0 4 9 -> Save cursor as in DECSC and use Alternate
4142 * Screen Buffer, clearing it first. (This may be disabled by
4143 * the titeInhibit resource). This combines the effects of the 1
4144 * 0 4 7 and 1 0 4 8 modes. Use this with terminfo-based
4145 * applications rather than the 4 7 mode.
4146 * Ps = 1 0 5 0 -> Set terminfo/termcap function-key mode.
4147 * Ps = 1 0 5 1 -> Set Sun function-key mode.
4148 * Ps = 1 0 5 2 -> Set HP function-key mode.
4149 * Ps = 1 0 5 3 -> Set SCO function-key mode.
4150 * Ps = 1 0 6 0 -> Set legacy keyboard emulation (X11R6).
4151 * Ps = 1 0 6 1 -> Set VT220 keyboard emulation.
4152 * Ps = 2 0 0 4 -> Set bracketed paste mode.
4154 * http: *vt100.net/docs/vt220-rm/chapter4.html
4156 Terminal
.prototype.setMode = function(params
) {
4157 if (typeof params
=== 'object') {
4158 var l
= params
.length
4161 for (; i
< l
; i
++) {
4162 this.setMode(params
[i
]);
4171 this.insertMode
= true;
4174 //this.convertEol = true;
4177 } else if (this.prefix
=== '?') {
4180 this.applicationCursor
= true;
4183 this.setgCharset(0, Terminal
.charsets
.US
);
4184 this.setgCharset(1, Terminal
.charsets
.US
);
4185 this.setgCharset(2, Terminal
.charsets
.US
);
4186 this.setgCharset(3, Terminal
.charsets
.US
);
4187 // set VT100 mode here
4189 case 3: // 132 col mode
4190 this.savedCols
= this.cols
;
4191 this.resize(132, this.rows
);
4194 this.originMode
= true;
4197 this.wraparoundMode
= true;
4200 // this.cursorBlink = true;
4203 this.log('Serial port requested application keypad.');
4204 this.applicationKeypad
= true;
4206 case 9: // X10 Mouse
4207 // no release, no motion, no wheel, no modifiers.
4208 case 1000: // vt200 mouse
4210 // no modifiers, except control on the wheel.
4211 case 1002: // button event mouse
4212 case 1003: // any event mouse
4213 // any event - sends motion events,
4214 // even if there is no button held down.
4215 this.x10Mouse
= params
=== 9;
4216 this.vt200Mouse
= params
=== 1000;
4217 this.normalMouse
= params
> 1000;
4218 this.mouseEvents
= true;
4219 this.element
.style
.cursor
= 'default';
4220 this.log('Binding to mouse events.');
4222 case 1004: // send focusin/focusout events
4225 this.sendFocus
= true;
4227 case 1005: // utf8 ext mode mouse
4228 this.utfMouse
= true;
4229 // for wide terminals
4230 // simply encodes large values as utf8 characters
4232 case 1006: // sgr ext mode mouse
4233 this.sgrMouse
= true;
4234 // for wide terminals
4235 // does not add 32 to fields
4236 // press: ^[[<b;x;yM
4237 // release: ^[[<b;x;ym
4239 case 1015: // urxvt ext mode mouse
4240 this.urxvtMouse
= true;
4241 // for wide terminals
4242 // numbers for fields
4244 // motion: ^[[b;x;yT
4246 case 25: // show cursor
4247 this.cursorHidden
= false;
4249 case 1049: // alt screen buffer cursor
4250 //this.saveCursor();
4252 case 47: // alt screen buffer
4253 case 1047: // alt screen buffer
4261 scrollTop
: this.scrollTop
,
4262 scrollBottom
: this.scrollBottom
,
4264 // XXX save charset(s) here?
4265 // charset: this.charset,
4266 // glevel: this.glevel,
4267 // charsets: this.charsets
4270 this.normal
= normal
;
4279 * CSI Pm l Reset Mode (RM).
4280 * Ps = 2 -> Keyboard Action Mode (AM).
4281 * Ps = 4 -> Replace Mode (IRM).
4282 * Ps = 1 2 -> Send/receive (SRM).
4283 * Ps = 2 0 -> Normal Linefeed (LNM).
4285 * DEC Private Mode Reset (DECRST).
4286 * Ps = 1 -> Normal Cursor Keys (DECCKM).
4287 * Ps = 2 -> Designate VT52 mode (DECANM).
4288 * Ps = 3 -> 80 Column Mode (DECCOLM).
4289 * Ps = 4 -> Jump (Fast) Scroll (DECSCLM).
4290 * Ps = 5 -> Normal Video (DECSCNM).
4291 * Ps = 6 -> Normal Cursor Mode (DECOM).
4292 * Ps = 7 -> No Wraparound Mode (DECAWM).
4293 * Ps = 8 -> No Auto-repeat Keys (DECARM).
4294 * Ps = 9 -> Don't send Mouse X & Y on button press.
4295 * Ps = 1 0 -> Hide toolbar (rxvt).
4296 * Ps = 1 2 -> Stop Blinking Cursor (att610).
4297 * Ps = 1 8 -> Don't print form feed (DECPFF).
4298 * Ps = 1 9 -> Limit print to scrolling region (DECPEX).
4299 * Ps = 2 5 -> Hide Cursor (DECTCEM).
4300 * Ps = 3 0 -> Don't show scrollbar (rxvt).
4301 * Ps = 3 5 -> Disable font-shifting functions (rxvt).
4302 * Ps = 4 0 -> Disallow 80 -> 132 Mode.
4303 * Ps = 4 1 -> No more(1) fix (see curses resource).
4304 * Ps = 4 2 -> Disable Nation Replacement Character sets (DEC-
4306 * Ps = 4 4 -> Turn Off Margin Bell.
4307 * Ps = 4 5 -> No Reverse-wraparound Mode.
4308 * Ps = 4 6 -> Stop Logging. (This is normally disabled by a
4309 * compile-time option).
4310 * Ps = 4 7 -> Use Normal Screen Buffer.
4311 * Ps = 6 6 -> Numeric keypad (DECNKM).
4312 * Ps = 6 7 -> Backarrow key sends delete (DECBKM).
4313 * Ps = 1 0 0 0 -> Don't send Mouse X & Y on button press and
4314 * release. See the section Mouse Tracking.
4315 * Ps = 1 0 0 1 -> Don't use Hilite Mouse Tracking.
4316 * Ps = 1 0 0 2 -> Don't use Cell Motion Mouse Tracking.
4317 * Ps = 1 0 0 3 -> Don't use All Motion Mouse Tracking.
4318 * Ps = 1 0 0 4 -> Don't send FocusIn/FocusOut events.
4319 * Ps = 1 0 0 5 -> Disable Extended Mouse Mode.
4320 * Ps = 1 0 1 0 -> Don't scroll to bottom on tty output
4322 * Ps = 1 0 1 1 -> Don't scroll to bottom on key press (rxvt).
4323 * Ps = 1 0 3 4 -> Don't interpret "meta" key. (This disables
4324 * the eightBitInput resource).
4325 * Ps = 1 0 3 5 -> Disable special modifiers for Alt and Num-
4326 * Lock keys. (This disables the numLock resource).
4327 * Ps = 1 0 3 6 -> Don't send ESC when Meta modifies a key.
4328 * (This disables the metaSendsEscape resource).
4329 * Ps = 1 0 3 7 -> Send VT220 Remove from the editing-keypad
4331 * Ps = 1 0 3 9 -> Don't send ESC when Alt modifies a key.
4332 * (This disables the altSendsEscape resource).
4333 * Ps = 1 0 4 0 -> Do not keep selection when not highlighted.
4334 * (This disables the keepSelection resource).
4335 * Ps = 1 0 4 1 -> Use the PRIMARY selection. (This disables
4336 * the selectToClipboard resource).
4337 * Ps = 1 0 4 2 -> Disable Urgency window manager hint when
4338 * Control-G is received. (This disables the bellIsUrgent
4340 * Ps = 1 0 4 3 -> Disable raising of the window when Control-
4341 * G is received. (This disables the popOnBell resource).
4342 * Ps = 1 0 4 7 -> Use Normal Screen Buffer, clearing screen
4343 * first if in the Alternate Screen. (This may be disabled by
4344 * the titeInhibit resource).
4345 * Ps = 1 0 4 8 -> Restore cursor as in DECRC. (This may be
4346 * disabled by the titeInhibit resource).
4347 * Ps = 1 0 4 9 -> Use Normal Screen Buffer and restore cursor
4348 * as in DECRC. (This may be disabled by the titeInhibit
4349 * resource). This combines the effects of the 1 0 4 7 and 1 0
4350 * 4 8 modes. Use this with terminfo-based applications rather
4351 * than the 4 7 mode.
4352 * Ps = 1 0 5 0 -> Reset terminfo/termcap function-key mode.
4353 * Ps = 1 0 5 1 -> Reset Sun function-key mode.
4354 * Ps = 1 0 5 2 -> Reset HP function-key mode.
4355 * Ps = 1 0 5 3 -> Reset SCO function-key mode.
4356 * Ps = 1 0 6 0 -> Reset legacy keyboard emulation (X11R6).
4357 * Ps = 1 0 6 1 -> Reset keyboard emulation to Sun/PC style.
4358 * Ps = 2 0 0 4 -> Reset bracketed paste mode.
4360 Terminal
.prototype.resetMode = function(params
) {
4361 if (typeof params
=== 'object') {
4362 var l
= params
.length
4365 for (; i
< l
; i
++) {
4366 this.resetMode(params
[i
]);
4375 this.insertMode
= false;
4378 //this.convertEol = false;
4381 } else if (this.prefix
=== '?') {
4384 this.applicationCursor
= false;
4387 if (this.cols
=== 132 && this.savedCols
) {
4388 this.resize(this.savedCols
, this.rows
);
4390 delete this.savedCols
;
4393 this.originMode
= false;
4396 this.wraparoundMode
= false;
4399 // this.cursorBlink = false;
4402 this.log('Switching back to normal keypad.');
4403 this.applicationKeypad
= false;
4405 case 9: // X10 Mouse
4406 case 1000: // vt200 mouse
4407 case 1002: // button event mouse
4408 case 1003: // any event mouse
4409 this.x10Mouse
= false;
4410 this.vt200Mouse
= false;
4411 this.normalMouse
= false;
4412 this.mouseEvents
= false;
4413 this.element
.style
.cursor
= '';
4415 case 1004: // send focusin/focusout events
4416 this.sendFocus
= false;
4418 case 1005: // utf8 ext mode mouse
4419 this.utfMouse
= false;
4421 case 1006: // sgr ext mode mouse
4422 this.sgrMouse
= false;
4424 case 1015: // urxvt ext mode mouse
4425 this.urxvtMouse
= false;
4427 case 25: // hide cursor
4428 this.cursorHidden
= true;
4430 case 1049: // alt screen buffer cursor
4432 case 47: // normal screen buffer
4433 case 1047: // normal screen buffer - clearing it first
4435 this.lines
= this.normal
.lines
;
4436 this.ybase
= this.normal
.ybase
;
4437 this.ydisp
= this.normal
.ydisp
;
4438 this.x
= this.normal
.x
;
4439 this.y
= this.normal
.y
;
4440 this.scrollTop
= this.normal
.scrollTop
;
4441 this.scrollBottom
= this.normal
.scrollBottom
;
4442 this.tabs
= this.normal
.tabs
;
4444 // if (params === 1049) {
4445 // this.x = this.savedX;
4446 // this.y = this.savedY;
4448 this.refresh(0, this.rows
- 1);
4459 * Set Scrolling Region [top;bottom] (default = full size of win-
4463 Terminal
.prototype.setScrollRegion = function(params
) {
4464 if (this.prefix
) return;
4465 this.scrollTop
= (params
[0] || 1) - 1;
4466 this.scrollBottom
= (params
[1] || this.rows
) - 1;
4474 * Save cursor (ANSI.SYS).
4476 Terminal
.prototype.saveCursor = function(params
) {
4477 this.savedX
= this.x
;
4478 this.savedY
= this.y
;
4484 * Restore cursor (ANSI.SYS).
4486 Terminal
.prototype.restoreCursor = function(params
) {
4487 this.x
= this.savedX
|| 0;
4488 this.y
= this.savedY
|| 0;
4498 * Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
4500 Terminal
.prototype.cursorForwardTab = function(params
) {
4501 var param
= params
[0] || 1;
4503 this.x
= this.nextStop();
4509 * CSI Ps S Scroll up Ps lines (default = 1) (SU).
4511 Terminal
.prototype.scrollUp = function(params
) {
4512 var param
= params
[0] || 1;
4514 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
4515 this.lines
.splice(this.ybase
+ this.scrollBottom
, 0, this.blankLine());
4518 this.updateRange(this.scrollTop
);
4519 this.updateRange(this.scrollBottom
);
4524 * CSI Ps T Scroll down Ps lines (default = 1) (SD).
4526 Terminal
.prototype.scrollDown = function(params
) {
4527 var param
= params
[0] || 1;
4529 this.lines
.splice(this.ybase
+ this.scrollBottom
, 1);
4530 this.lines
.splice(this.ybase
+ this.scrollTop
, 0, this.blankLine());
4533 this.updateRange(this.scrollTop
);
4534 this.updateRange(this.scrollBottom
);
4539 * CSI Ps ; Ps ; Ps ; Ps ; Ps T
4540 * Initiate highlight mouse tracking. Parameters are
4541 * [func;startx;starty;firstrow;lastrow]. See the section Mouse
4544 Terminal
.prototype.initMouseTracking = function(params
) {
4545 // Relevant: DECSET 1001
4551 * Reset one or more features of the title modes to the default
4552 * value. Normally, "reset" disables the feature. It is possi-
4553 * ble to disable the ability to reset features by compiling a
4554 * different default for the title modes into xterm.
4555 * Ps = 0 -> Do not set window/icon labels using hexadecimal.
4556 * Ps = 1 -> Do not query window/icon labels using hexadeci-
4558 * Ps = 2 -> Do not set window/icon labels using UTF-8.
4559 * Ps = 3 -> Do not query window/icon labels using UTF-8.
4560 * (See discussion of "Title Modes").
4562 Terminal
.prototype.resetTitleModes = function(params
) {
4568 * CSI Ps Z Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
4570 Terminal
.prototype.cursorBackwardTab = function(params
) {
4571 var param
= params
[0] || 1;
4573 this.x
= this.prevStop();
4579 * CSI Ps b Repeat the preceding graphic character Ps times (REP).
4581 Terminal
.prototype.repeatPrecedingCharacter = function(params
) {
4582 var param
= params
[0] || 1
4583 , line
= this.lines
[this.ybase
+ this.y
]
4584 , ch
= line
[this.x
- 1] || [this.defAttr
, ' ', 1];
4586 while (param
--) line
[this.x
++] = ch
;
4591 * CSI Ps g Tab Clear (TBC).
4592 * Ps = 0 -> Clear Current Column (default).
4593 * Ps = 3 -> Clear All.
4595 * Ps = 2 -> Clear Stops on Line.
4596 * http://vt100.net/annarbor/aaa-ug/section6.html
4598 Terminal
.prototype.tabClear = function(params
) {
4599 var param
= params
[0];
4601 delete this.tabs
[this.x
];
4602 } else if (param
=== 3) {
4609 * CSI Pm i Media Copy (MC).
4610 * Ps = 0 -> Print screen (default).
4611 * Ps = 4 -> Turn off printer controller mode.
4612 * Ps = 5 -> Turn on printer controller mode.
4614 * Media Copy (MC, DEC-specific).
4615 * Ps = 1 -> Print line containing cursor.
4616 * Ps = 4 -> Turn off autoprint mode.
4617 * Ps = 5 -> Turn on autoprint mode.
4618 * Ps = 1 0 -> Print composed display, ignores DECPEX.
4619 * Ps = 1 1 -> Print all pages.
4621 Terminal
.prototype.mediaCopy = function(params
) {
4628 * Set or reset resource-values used by xterm to decide whether
4629 * to construct escape sequences holding information about the
4630 * modifiers pressed with a given key. The first parameter iden-
4631 * tifies the resource to set/reset. The second parameter is the
4632 * value to assign to the resource. If the second parameter is
4633 * omitted, the resource is reset to its initial value.
4634 * Ps = 1 -> modifyCursorKeys.
4635 * Ps = 2 -> modifyFunctionKeys.
4636 * Ps = 4 -> modifyOtherKeys.
4637 * If no parameters are given, all resources are reset to their
4640 Terminal
.prototype.setResources = function(params
) {
4647 * Disable modifiers which may be enabled via the CSI > Ps; Ps m
4648 * sequence. This corresponds to a resource value of "-1", which
4649 * cannot be set with the other sequence. The parameter identi-
4650 * fies the resource to be disabled:
4651 * Ps = 1 -> modifyCursorKeys.
4652 * Ps = 2 -> modifyFunctionKeys.
4653 * Ps = 4 -> modifyOtherKeys.
4654 * If the parameter is omitted, modifyFunctionKeys is disabled.
4655 * When modifyFunctionKeys is disabled, xterm uses the modifier
4656 * keys to make an extended sequence of functions rather than
4657 * adding a parameter to each function key to denote the modi-
4660 Terminal
.prototype.disableModifiers = function(params
) {
4667 * Set resource value pointerMode. This is used by xterm to
4668 * decide whether to hide the pointer cursor as the user types.
4669 * Valid values for the parameter:
4670 * Ps = 0 -> never hide the pointer.
4671 * Ps = 1 -> hide if the mouse tracking mode is not enabled.
4672 * Ps = 2 -> always hide the pointer. If no parameter is
4673 * given, xterm uses the default, which is 1 .
4675 Terminal
.prototype.setPointerMode = function(params
) {
4681 * CSI ! p Soft terminal reset (DECSTR).
4682 * http://vt100.net/docs/vt220-rm/table4-10.html
4684 Terminal
.prototype.softReset = function(params
) {
4685 this.cursorHidden
= false;
4686 this.insertMode
= false;
4687 this.originMode
= false;
4688 this.wraparoundMode
= false; // autowrap
4689 this.applicationKeypad
= false; // ?
4690 this.applicationCursor
= false;
4692 this.scrollBottom
= this.rows
- 1;
4693 this.curAttr
= this.defAttr
;
4694 this.x
= this.y
= 0; // ?
4695 this.charset
= null;
4696 this.glevel
= 0; // ??
4697 this.charsets
= [null]; // ??
4703 * Request ANSI mode (DECRQM). For VT300 and up, reply is
4705 * where Ps is the mode number as in RM, and Pm is the mode
4707 * 0 - not recognized
4710 * 3 - permanently set
4711 * 4 - permanently reset
4713 Terminal
.prototype.requestAnsiMode = function(params
) {
4720 * Request DEC private mode (DECRQM). For VT300 and up, reply is
4722 * where Ps is the mode number as in DECSET, Pm is the mode value
4723 * as in the ANSI DECRQM.
4725 Terminal
.prototype.requestPrivateMode = function(params
) {
4732 * Set conformance level (DECSCL). Valid values for the first
4734 * Ps = 6 1 -> VT100.
4735 * Ps = 6 2 -> VT200.
4736 * Ps = 6 3 -> VT300.
4737 * Valid values for the second parameter:
4738 * Ps = 0 -> 8-bit controls.
4739 * Ps = 1 -> 7-bit controls (always set for VT100).
4740 * Ps = 2 -> 8-bit controls.
4742 Terminal
.prototype.setConformanceLevel = function(params
) {
4748 * CSI Ps q Load LEDs (DECLL).
4749 * Ps = 0 -> Clear all LEDS (default).
4750 * Ps = 1 -> Light Num Lock.
4751 * Ps = 2 -> Light Caps Lock.
4752 * Ps = 3 -> Light Scroll Lock.
4753 * Ps = 2 1 -> Extinguish Num Lock.
4754 * Ps = 2 2 -> Extinguish Caps Lock.
4755 * Ps = 2 3 -> Extinguish Scroll Lock.
4757 Terminal
.prototype.loadLEDs = function(params
) {
4764 * Set cursor style (DECSCUSR, VT520).
4765 * Ps = 0 -> blinking block.
4766 * Ps = 1 -> blinking block (default).
4767 * Ps = 2 -> steady block.
4768 * Ps = 3 -> blinking underline.
4769 * Ps = 4 -> steady underline.
4771 Terminal
.prototype.setCursorStyle = function(params
) {
4778 * Select character protection attribute (DECSCA). Valid values
4779 * for the parameter:
4780 * Ps = 0 -> DECSED and DECSEL can erase (default).
4781 * Ps = 1 -> DECSED and DECSEL cannot erase.
4782 * Ps = 2 -> DECSED and DECSEL can erase.
4784 Terminal
.prototype.setCharProtectionAttr = function(params
) {
4791 * Restore DEC Private Mode Values. The value of Ps previously
4792 * saved is restored. Ps values are the same as for DECSET.
4794 Terminal
.prototype.restorePrivateValues = function(params
) {
4800 * CSI Pt; Pl; Pb; Pr; Ps$ r
4801 * Change Attributes in Rectangular Area (DECCARA), VT400 and up.
4802 * Pt; Pl; Pb; Pr denotes the rectangle.
4803 * Ps denotes the SGR attributes to change: 0, 1, 4, 5, 7.
4804 * NOTE: xterm doesn't enable this code by default.
4806 Terminal
.prototype.setAttrInRectangle = function(params
) {
4816 for (; t
< b
+ 1; t
++) {
4817 line
= this.lines
[this.ybase
+ t
];
4818 for (i
= l
; i
< r
; i
++) {
4819 line
[i
] = [attr
, line
[i
][1]];
4824 this.updateRange(params
[0]);
4825 this.updateRange(params
[2]);
4830 * CSI Pc; Pt; Pl; Pb; Pr$ x
4831 * Fill Rectangular Area (DECFRA), VT420 and up.
4832 * Pc is the character to use.
4833 * Pt; Pl; Pb; Pr denotes the rectangle.
4834 * NOTE: xterm doesn't enable this code by default.
4836 Terminal
.prototype.fillRectangle = function(params
) {
4846 for (; t
< b
+ 1; t
++) {
4847 line
= this.lines
[this.ybase
+ t
];
4848 for (i
= l
; i
< r
; i
++) {
4849 line
[i
] = [line
[i
][0], String
.fromCharCode(ch
)];
4854 this.updateRange(params
[1]);
4855 this.updateRange(params
[3]);
4861 * Enable Locator Reporting (DECELR).
4862 * Valid values for the first parameter:
4863 * Ps = 0 -> Locator disabled (default).
4864 * Ps = 1 -> Locator enabled.
4865 * Ps = 2 -> Locator enabled for one report, then disabled.
4866 * The second parameter specifies the coordinate unit for locator
4868 * Valid values for the second parameter:
4869 * Pu = 0 <- or omitted -> default to character cells.
4870 * Pu = 1 <- device physical pixels.
4871 * Pu = 2 <- character cells.
4873 Terminal
.prototype.enableLocatorReporting = function(params
) {
4874 var val
= params
[0] > 0;
4875 //this.mouseEvents = val;
4876 //this.decLocator = val;
4881 * CSI Pt; Pl; Pb; Pr$ z
4882 * Erase Rectangular Area (DECERA), VT400 and up.
4883 * Pt; Pl; Pb; Pr denotes the rectangle.
4884 * NOTE: xterm doesn't enable this code by default.
4886 Terminal
.prototype.eraseRectangle = function(params
) {
4896 ch
= [this.eraseAttr(), ' ', 1]; // xterm?
4898 for (; t
< b
+ 1; t
++) {
4899 line
= this.lines
[this.ybase
+ t
];
4900 for (i
= l
; i
< r
; i
++) {
4906 this.updateRange(params
[0]);
4907 this.updateRange(params
[2]);
4913 * Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
4914 * NOTE: xterm doesn't enable this code by default.
4916 Terminal
.prototype.insertColumns = function() {
4917 var param
= params
[0]
4918 , l
= this.ybase
+ this.rows
4919 , ch
= [this.eraseAttr(), ' ', 1] // xterm?
4923 for (i
= this.ybase
; i
< l
; i
++) {
4924 this.lines
[i
].splice(this.x
+ 1, 0, ch
);
4925 this.lines
[i
].pop();
4935 * Delete P s Column(s) (default = 1) (DECDC), VT420 and up
4936 * NOTE: xterm doesn't enable this code by default.
4938 Terminal
.prototype.deleteColumns = function() {
4939 var param
= params
[0]
4940 , l
= this.ybase
+ this.rows
4941 , ch
= [this.eraseAttr(), ' ', 1] // xterm?
4945 for (i
= this.ybase
; i
< l
; i
++) {
4946 this.lines
[i
].splice(this.x
, 1);
4947 this.lines
[i
].push(ch
);
4958 Terminal
.charsets
= {};
4960 // DEC Special Character and Line Drawing Set.
4961 // http://vt100.net/docs/vt102-ug/table5-13.html
4962 // A lot of curses apps use this if they see TERM=xterm.
4963 // testing: echo -e '\e(0a\e(B'
4964 // The xterm output sometimes seems to conflict with the
4965 // reference above. xterm seems in line with the reference
4966 // when running vttest however.
4967 // The table below now uses xterm's output from vttest.
4968 Terminal
.charsets
.SCLD
= { // (0
4969 '`': '\u25c6', // '◆'
4970 'a': '\u2592', // '▒'
4971 'b': '\u0009', // '\t'
4972 'c': '\u000c', // '\f'
4973 'd': '\u000d', // '\r'
4974 'e': '\u000a', // '\n'
4975 'f': '\u00b0', // '°'
4976 'g': '\u00b1', // '±'
4977 'h': '\u2424', // '\u2424' (NL)
4978 'i': '\u000b', // '\v'
4979 'j': '\u2518', // '┘'
4980 'k': '\u2510', // '┐'
4981 'l': '\u250c', // '┌'
4982 'm': '\u2514', // '└'
4983 'n': '\u253c', // '┼'
4984 'o': '\u23ba', // '⎺'
4985 'p': '\u23bb', // '⎻'
4986 'q': '\u2500', // '─'
4987 'r': '\u23bc', // '⎼'
4988 's': '\u23bd', // '⎽'
4989 't': '\u251c', // '├'
4990 'u': '\u2524', // '┤'
4991 'v': '\u2534', // '┴'
4992 'w': '\u252c', // '┬'
4993 'x': '\u2502', // '│'
4994 'y': '\u2264', // '≤'
4995 'z': '\u2265', // '≥'
4996 '{': '\u03c0', // 'π'
4997 '|': '\u2260', // '≠'
4998 '}': '\u00a3', // '£'
4999 '~': '\u00b7' // '·'
5002 Terminal
.charsets
.UK
= null; // (A
5003 Terminal
.charsets
.US
= null; // (B (USASCII)
5004 Terminal
.charsets
.Dutch
= null; // (4
5005 Terminal
.charsets
.Finnish
= null; // (C or (5
5006 Terminal
.charsets
.French
= null; // (R
5007 Terminal
.charsets
.FrenchCanadian
= null; // (Q
5008 Terminal
.charsets
.German
= null; // (K
5009 Terminal
.charsets
.Italian
= null; // (Y
5010 Terminal
.charsets
.NorwegianDanish
= null; // (E or (6
5011 Terminal
.charsets
.Spanish
= null; // (Z
5012 Terminal
.charsets
.Swedish
= null; // (H or (7
5013 Terminal
.charsets
.Swiss
= null; // (=
5014 Terminal
.charsets
.ISOLatin
= null; // /A
5020 function contains(el
, arr
) {
5021 for (var i
= 0; i
< arr
.length
; i
+= 1) {
5022 if (el
=== arr
[i
]) {
5029 function on(el
, type
, handler
, capture
) {
5030 if (!Array
.isArray(el
)) {
5033 el
.forEach(function (element
) {
5034 element
.addEventListener(type
, handler
, capture
|| false);
5038 function off(el
, type
, handler
, capture
) {
5039 el
.removeEventListener(type
, handler
, capture
|| false);
5042 function cancel(ev
, force
) {
5043 if (!this.cancelEvents
&& !force
) {
5046 ev
.preventDefault();
5047 ev
.stopPropagation();
5051 function inherits(child
, parent
) {
5053 this.constructor = child
;
5055 f
.prototype = parent
.prototype;
5056 child
.prototype = new f
;
5059 // if bold is broken, we can't
5060 // use it in the terminal.
5061 function isBoldBroken(document
) {
5062 var body
= document
.getElementsByTagName('body')[0];
5063 var el
= document
.createElement('span');
5064 el
.innerHTML
= 'hello world';
5065 body
.appendChild(el
);
5066 var w1
= el
.scrollWidth
;
5067 el
.style
.fontWeight
= 'bold';
5068 var w2
= el
.scrollWidth
;
5069 body
.removeChild(el
);
5073 var String
= this.String
;
5074 var setTimeout
= this.setTimeout
;
5075 var setInterval
= this.setInterval
;
5077 function indexOf(obj
, el
) {
5080 if (obj
[i
] === el
) return i
;
5085 function isThirdLevelShift(term
, ev
) {
5087 (term
.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) ||
5088 (term
.isMSWindows
&& ev
.altKey
&& ev
.ctrlKey
&& !ev
.metaKey
);
5090 if (ev
.type
== 'keypress') {
5091 return thirdLevelKey
;
5094 // Don't invoke for arrows, pageDown, home, backspace, etc. (on non-keypress events)
5095 return thirdLevelKey
&& (!ev
.keyCode
|| ev
.keyCode
> 47);
5098 function matchColor(r1
, g1
, b1
) {
5099 var hash
= (r1
<< 16) | (g1
<< 8) | b1
;
5101 if (matchColor
._cache
[hash
] != null) {
5102 return matchColor
._cache
[hash
];
5105 var ldiff
= Infinity
5114 for (; i
< Terminal
.vcolors
.length
; i
++) {
5115 c
= Terminal
.vcolors
[i
];
5120 diff
= matchColor
.distance(r1
, g1
, b1
, r2
, g2
, b2
);
5133 return matchColor
._cache
[hash
] = li
;
5136 matchColor
._cache
= {};
5138 // http://stackoverflow.com/questions/1633828
5139 matchColor
.distance = function(r1
, g1
, b1
, r2
, g2
, b2
) {
5140 return Math
.pow(30 * (r1
- r2
), 2)
5141 + Math
.pow(59 * (g1
- g2
), 2)
5142 + Math
.pow(11 * (b1
- b2
), 2);
5145 function each(obj
, iter
, con
) {
5146 if (obj
.forEach
) return obj
.forEach(iter
, con
);
5147 for (var i
= 0; i
< obj
.length
; i
++) {
5148 iter
.call(con
, obj
[i
], i
, obj
);
5152 function keys(obj
) {
5153 if (Object
.keys
) return Object
.keys(obj
);
5156 if (Object
.prototype.hasOwnProperty
.call(obj
, key
)) {
5163 var wcwidth
= (function(opts
) {
5164 // extracted from https://www.cl.cam.ac.uk/%7Emgk25/ucs/wcwidth.c
5165 // combining characters
5167 [0x0300, 0x036F], [0x0483, 0x0486], [0x0488, 0x0489],
5168 [0x0591, 0x05BD], [0x05BF, 0x05BF], [0x05C1, 0x05C2],
5169 [0x05C4, 0x05C5], [0x05C7, 0x05C7], [0x0600, 0x0603],
5170 [0x0610, 0x0615], [0x064B, 0x065E], [0x0670, 0x0670],
5171 [0x06D6, 0x06E4], [0x06E7, 0x06E8], [0x06EA, 0x06ED],
5172 [0x070F, 0x070F], [0x0711, 0x0711], [0x0730, 0x074A],
5173 [0x07A6, 0x07B0], [0x07EB, 0x07F3], [0x0901, 0x0902],
5174 [0x093C, 0x093C], [0x0941, 0x0948], [0x094D, 0x094D],
5175 [0x0951, 0x0954], [0x0962, 0x0963], [0x0981, 0x0981],
5176 [0x09BC, 0x09BC], [0x09C1, 0x09C4], [0x09CD, 0x09CD],
5177 [0x09E2, 0x09E3], [0x0A01, 0x0A02], [0x0A3C, 0x0A3C],
5178 [0x0A41, 0x0A42], [0x0A47, 0x0A48], [0x0A4B, 0x0A4D],
5179 [0x0A70, 0x0A71], [0x0A81, 0x0A82], [0x0ABC, 0x0ABC],
5180 [0x0AC1, 0x0AC5], [0x0AC7, 0x0AC8], [0x0ACD, 0x0ACD],
5181 [0x0AE2, 0x0AE3], [0x0B01, 0x0B01], [0x0B3C, 0x0B3C],
5182 [0x0B3F, 0x0B3F], [0x0B41, 0x0B43], [0x0B4D, 0x0B4D],
5183 [0x0B56, 0x0B56], [0x0B82, 0x0B82], [0x0BC0, 0x0BC0],
5184 [0x0BCD, 0x0BCD], [0x0C3E, 0x0C40], [0x0C46, 0x0C48],
5185 [0x0C4A, 0x0C4D], [0x0C55, 0x0C56], [0x0CBC, 0x0CBC],
5186 [0x0CBF, 0x0CBF], [0x0CC6, 0x0CC6], [0x0CCC, 0x0CCD],
5187 [0x0CE2, 0x0CE3], [0x0D41, 0x0D43], [0x0D4D, 0x0D4D],
5188 [0x0DCA, 0x0DCA], [0x0DD2, 0x0DD4], [0x0DD6, 0x0DD6],
5189 [0x0E31, 0x0E31], [0x0E34, 0x0E3A], [0x0E47, 0x0E4E],
5190 [0x0EB1, 0x0EB1], [0x0EB4, 0x0EB9], [0x0EBB, 0x0EBC],
5191 [0x0EC8, 0x0ECD], [0x0F18, 0x0F19], [0x0F35, 0x0F35],
5192 [0x0F37, 0x0F37], [0x0F39, 0x0F39], [0x0F71, 0x0F7E],
5193 [0x0F80, 0x0F84], [0x0F86, 0x0F87], [0x0F90, 0x0F97],
5194 [0x0F99, 0x0FBC], [0x0FC6, 0x0FC6], [0x102D, 0x1030],
5195 [0x1032, 0x1032], [0x1036, 0x1037], [0x1039, 0x1039],
5196 [0x1058, 0x1059], [0x1160, 0x11FF], [0x135F, 0x135F],
5197 [0x1712, 0x1714], [0x1732, 0x1734], [0x1752, 0x1753],
5198 [0x1772, 0x1773], [0x17B4, 0x17B5], [0x17B7, 0x17BD],
5199 [0x17C6, 0x17C6], [0x17C9, 0x17D3], [0x17DD, 0x17DD],
5200 [0x180B, 0x180D], [0x18A9, 0x18A9], [0x1920, 0x1922],
5201 [0x1927, 0x1928], [0x1932, 0x1932], [0x1939, 0x193B],
5202 [0x1A17, 0x1A18], [0x1B00, 0x1B03], [0x1B34, 0x1B34],
5203 [0x1B36, 0x1B3A], [0x1B3C, 0x1B3C], [0x1B42, 0x1B42],
5204 [0x1B6B, 0x1B73], [0x1DC0, 0x1DCA], [0x1DFE, 0x1DFF],
5205 [0x200B, 0x200F], [0x202A, 0x202E], [0x2060, 0x2063],
5206 [0x206A, 0x206F], [0x20D0, 0x20EF], [0x302A, 0x302F],
5207 [0x3099, 0x309A], [0xA806, 0xA806], [0xA80B, 0xA80B],
5208 [0xA825, 0xA826], [0xFB1E, 0xFB1E], [0xFE00, 0xFE0F],
5209 [0xFE20, 0xFE23], [0xFEFF, 0xFEFF], [0xFFF9, 0xFFFB],
5210 [0x10A01, 0x10A03], [0x10A05, 0x10A06], [0x10A0C, 0x10A0F],
5211 [0x10A38, 0x10A3A], [0x10A3F, 0x10A3F], [0x1D167, 0x1D169],
5212 [0x1D173, 0x1D182], [0x1D185, 0x1D18B], [0x1D1AA, 0x1D1AD],
5213 [0x1D242, 0x1D244], [0xE0001, 0xE0001], [0xE0020, 0xE007F],
5217 function bisearch(ucs
) {
5219 var max
= COMBINING
.length
- 1;
5221 if (ucs
< COMBINING
[0][0] || ucs
> COMBINING
[max
][1])
5223 while (max
>= min
) {
5224 mid
= Math
.floor((min
+ max
) / 2);
5225 if (ucs
> COMBINING
[mid
][1])
5227 else if (ucs
< COMBINING
[mid
][0])
5234 function wcwidth(ucs
) {
5235 // test for 8-bit control characters
5238 if (ucs
< 32 || (ucs
>= 0x7f && ucs
< 0xa0))
5239 return opts
.control
;
5240 // binary search in table of non-spacing characters
5243 // if we arrive here, ucs is not a combining or C0/C1 control character
5248 ucs
<= 0x115f || // Hangul Jamo init. consonants
5251 (ucs
>= 0x2e80 && ucs
<= 0xa4cf && ucs
!= 0x303f) || // CJK..Yi
5252 (ucs
>= 0xac00 && ucs
<= 0xd7a3) || // Hangul Syllables
5253 (ucs
>= 0xf900 && ucs
<= 0xfaff) || // CJK Compat Ideographs
5254 (ucs
>= 0xfe10 && ucs
<= 0xfe19) || // Vertical forms
5255 (ucs
>= 0xfe30 && ucs
<= 0xfe6f) || // CJK Compat Forms
5256 (ucs
>= 0xff00 && ucs
<= 0xff60) || // Fullwidth Forms
5257 (ucs
>= 0xffe0 && ucs
<= 0xffe6) ||
5258 (ucs
>= 0x20000 && ucs
<= 0x2fffd) ||
5259 (ucs
>= 0x30000 && ucs
<= 0x3fffd)
5264 })({nul
: 0, control
: 0}); // configurable options
5270 Terminal
.EventEmitter
= EventEmitter
;
5271 Terminal
.CompositionHelper
= CompositionHelper
;
5272 Terminal
.inherits
= inherits
;
5275 * Adds an event listener to the terminal.
5277 * @param {string} event The name of the event. TODO: Document all event types
5278 * @param {function} callback The function to call when the event is triggered.
5282 Terminal
.cancel
= cancel
;