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(dontRecurse
) {
306 if (!this.isComposing
) {
309 var cursor
= this.terminal
.element
.querySelector('.terminal-cursor');
311 this.compositionView
.style
.left
= cursor
.offsetLeft
+ 'px';
312 this.compositionView
.style
.top
= cursor
.offsetTop
+ 'px';
313 var compositionViewBounds
= this.compositionView
.getBoundingClientRect();
314 this.textarea
.style
.left
= cursor
.offsetLeft
+ compositionViewBounds
.width
+ 'px';
315 this.textarea
.style
.top
= (cursor
.offsetTop
+ cursor
.offsetHeight
) + 'px';
318 setTimeout(this.updateCompositionElements
.bind(this, true), 0);
323 * Clears the textarea's position so that the cursor does not blink on IE.
326 CompositionHelper
.prototype.clearTextareaPosition = function() {
327 this.textarea
.style
.left
= '';
328 this.textarea
.style
.top
= '';
332 * Represents the viewport of a terminal, the visible area within the larger buffer of output.
333 * Logic for the virtual scroll bar is included in this object.
334 * @param {Terminal} terminal The Terminal object.
335 * @param {HTMLElement} viewportElement The DOM element acting as the viewport
336 * @param {HTMLElement} charMeasureElement A DOM element used to measure the character size of
339 function Viewport(terminal
, viewportElement
, scrollArea
, charMeasureElement
) {
340 this.terminal
= terminal
;
341 this.viewportElement
= viewportElement
;
342 this.scrollArea
= scrollArea
;
343 this.charMeasureElement
= charMeasureElement
;
344 this.currentRowHeight
= 0;
345 this.lastRecordedBufferLength
= 0;
346 this.lastRecordedViewportHeight
= 0;
348 this.terminal
.on('scroll', this.syncScrollArea
.bind(this));
349 this.terminal
.on('resize', this.syncScrollArea
.bind(this));
350 this.viewportElement
.addEventListener('scroll', this.onScroll
.bind(this));
352 this.syncScrollArea();
356 * Refreshes row height, setting line-height, viewport height and scroll area height if
358 * @param {number|undefined} charSize A character size measurement bounding rect object, if it
359 * doesn't exist it will be created.
361 Viewport
.prototype.refresh = function(charSize
) {
362 var size
= charSize
|| this.charMeasureElement
.getBoundingClientRect();
363 if (size
.height
> 0) {
364 var rowHeightChanged
= size
.height
!== this.currentRowHeight
;
365 if (rowHeightChanged
) {
366 this.currentRowHeight
= size
.height
;
367 this.viewportElement
.style
.lineHeight
= size
.height
+ 'px';
368 this.terminal
.rowContainer
.style
.lineHeight
= size
.height
+ 'px';
370 var viewportHeightChanged
= this.lastRecordedViewportHeight
!== this.terminal
.rows
;
371 if (rowHeightChanged
|| viewportHeightChanged
) {
372 this.lastRecordedViewportHeight
= this.terminal
.rows
;
373 this.viewportElement
.style
.height
= size
.height
* this.terminal
.rows
+ 'px';
375 this.scrollArea
.style
.height
= (size
.height
* this.lastRecordedBufferLength
) + 'px';
380 * Updates dimensions and synchronizes the scroll area if necessary.
382 Viewport
.prototype.syncScrollArea = function() {
383 if (this.isApplicationMode
) {
384 // Fix scroll bar in application mode
385 this.lastRecordedBufferLength
= this.terminal
.rows
;
390 if (this.lastRecordedBufferLength
!== this.terminal
.lines
.length
) {
391 // If buffer height changed
392 this.lastRecordedBufferLength
= this.terminal
.lines
.length
;
394 } else if (this.lastRecordedViewportHeight
!== this.terminal
.rows
) {
395 // If viewport height changed
398 // If size has changed, refresh viewport
399 var size
= this.charMeasureElement
.getBoundingClientRect();
400 if (size
.height
!== this.currentRowHeight
) {
406 var scrollTop
= this.terminal
.ydisp
* this.currentRowHeight
;
407 if (this.viewportElement
.scrollTop
!== scrollTop
) {
408 this.viewportElement
.scrollTop
= scrollTop
;
413 * Sets the application mode of the viewport.
414 * @param {boolean} isApplicationMode Sets whether the terminal is in application mode. true
415 * for application mode (DECKPAM) and false for normal mode (DECKPNM).
417 Viewport
.prototype.setApplicationMode = function(isApplicationMode
) {
418 this.isApplicationMode
= isApplicationMode
;
419 this.syncScrollArea();
423 * Handles scroll events on the viewport, calculating the new viewport and requesting the
424 * terminal to scroll to it.
425 * @param {Event} ev The scroll event.
427 Viewport
.prototype.onScroll = function(ev
) {
428 if (this.isApplicationMode
) {
429 // Scrolling via the scroll bar is disabled during application mode
432 var newRow
= Math
.round(this.viewportElement
.scrollTop
/ this.currentRowHeight
);
433 var diff
= newRow
- this.terminal
.ydisp
;
434 this.terminal
.scrollDisp(diff
, true);
438 * Handles mouse wheel events by adjusting the viewport's scrollTop and delegating the actual
439 * scrolling to `onScroll`, this event needs to be attached manually by the consumer of
441 * @param {WheelEvent} ev The mouse wheel event.
443 Viewport
.prototype.onWheel = function(ev
) {
444 if (ev
.deltaY
=== 0) {
445 // Do nothing if it's not a vertical scroll event
448 // Fallback to WheelEvent.DOM_DELTA_PIXEL
450 if (ev
.deltaMode
=== WheelEvent
.DOM_DELTA_LINE
) {
451 multiplier
= this.currentRowHeight
;
452 } else if (ev
.deltaMode
=== WheelEvent
.DOM_DELTA_PAGE
) {
453 multiplier
= this.currentRowHeight
* this.terminal
.rows
;
455 this.viewportElement
.scrollTop
+= ev
.deltaY
* multiplier
;
456 // Prevent the page from scrolling when the terminal scrolls
463 var normal
= 0, escaped
= 1, csi
= 2, osc
= 3, charset
= 4, dcs
= 5, ignore
= 6;
470 * Creates a new `Terminal` object.
472 * @param {object} options An object containing a set of options, the available options are:
473 * - cursorBlink (boolean): Whether the terminal cursor blinks
477 * @alias module:xterm/src/xterm
479 function Terminal(options
) {
482 if (!(this instanceof Terminal
)) {
483 return new Terminal(arguments
[0], arguments
[1], arguments
[2]);
486 self
.cancel
= Terminal
.cancel
;
488 EventEmitter
.call(this);
490 if (typeof options
=== 'number') {
494 handler
: arguments
[2]
498 options
= options
|| {};
501 Object
.keys(Terminal
.defaults
).forEach(function(key
) {
502 if (options
[key
] == null) {
503 options
[key
] = Terminal
.options
[key
];
505 if (Terminal
[key
] !== Terminal
.defaults
[key
]) {
506 options
[key
] = Terminal
[key
];
509 self
[key
] = options
[key
];
512 if (options
.colors
.length
=== 8) {
513 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(8));
514 } else if (options
.colors
.length
=== 16) {
515 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(16));
516 } else if (options
.colors
.length
=== 10) {
517 options
.colors
= options
.colors
.slice(0, -2).concat(
518 Terminal
._colors
.slice(8, -2), options
.colors
.slice(-2));
519 } else if (options
.colors
.length
=== 18) {
520 options
.colors
= options
.colors
.concat(
521 Terminal
._colors
.slice(16, -2), options
.colors
.slice(-2));
523 this.colors
= options
.colors
;
525 this.options
= options
;
527 // this.context = options.context || window;
528 // this.document = options.document || document;
529 this.parent
= options
.body
|| options
.parent
530 || (document
? document
.getElementsByTagName('body')[0] : null);
532 this.cols
= options
.cols
|| options
.geometry
[0];
533 this.rows
= options
.rows
|| options
.geometry
[1];
535 if (options
.handler
) {
536 this.on('data', options
.handler
);
540 * The scroll position of the y cursor, ie. ybase + y = the y position within the entire
546 * The scroll position of the viewport
551 * The cursor's x position after ybase
556 * The cursor's y position after ybase
561 * Used to debounce the refresh function
563 this.isRefreshing
= false;
566 * Whether there is a full terminal refresh queued
569 this.cursorState
= 0;
570 this.cursorHidden
= false;
575 this.scrollBottom
= this.rows
- 1;
576 this.customKeydownHandler
= null;
579 this.applicationKeypad
= false;
580 this.applicationCursor
= false;
581 this.originMode
= false;
582 this.insertMode
= false;
583 this.wraparoundMode
= true; // defaults: xterm - true, vt100 - false
588 this.gcharset
= null;
590 this.charsets
= [null];
614 this.readable
= true;
615 this.writable
= true;
617 this.defAttr
= (0 << 18) | (257 << 9) | (256 << 0);
618 this.curAttr
= this.defAttr
;
621 this.currentParam
= 0;
625 // leftover surrogate high from previous write invocation
626 this.surrogate_high
= '';
629 * An array of all lines in the entire buffer, including the prompt. The lines are array of
630 * characters which are 2-length arrays where [0] is an attribute and [1] is the character.
635 this.lines
.push(this.blankLine());
642 inherits(Terminal
, EventEmitter
);
645 * back_color_erase feature for xterm.
647 Terminal
.prototype.eraseAttr = function() {
648 // if (this.is('screen')) return this.defAttr;
649 return (this.defAttr
& ~0x1ff) | (this.curAttr
& 0x1ff);
657 Terminal
.tangoColors
= [
678 // Colors 0-15 + 16-255
679 // Much thanks to TooTallNate for writing this.
680 Terminal
.colors
= (function() {
681 var colors
= Terminal
.tangoColors
.slice()
682 , r
= [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff]
687 for (; i
< 216; i
++) {
688 out(r
[(i
/ 36) % 6 | 0], r
[(i
/ 6) % 6 | 0], r
[i
% 6]);
693 for (; i
< 24; i
++) {
698 function out(r
, g
, b
) {
699 colors
.push('#' + hex(r
) + hex(g
) + hex(b
));
704 return c
.length
< 2 ? '0' + c
: c
;
710 Terminal
._colors
= Terminal
.colors
.slice();
712 Terminal
.vcolors
= (function() {
714 , colors
= Terminal
.colors
718 for (; i
< 256; i
++) {
719 color
= parseInt(colors
[i
].substring(1), 16);
721 (color
>> 16) & 0xff,
734 Terminal
.defaults
= {
735 colors
: Terminal
.colors
,
747 // programFeatures: false,
751 Terminal
.options
= {};
753 Terminal
.focus
= null;
755 each(keys(Terminal
.defaults
), function(key
) {
756 Terminal
[key
] = Terminal
.defaults
[key
];
757 Terminal
.options
[key
] = Terminal
.defaults
[key
];
761 * Focus the terminal. Delegates focus handling to the terminal's DOM element.
763 Terminal
.prototype.focus = function() {
764 return this.textarea
.focus();
768 * Binds the desired focus behavior on a given terminal object.
772 Terminal
.bindFocus = function (term
) {
773 on(term
.textarea
, 'focus', function (ev
) {
774 if (term
.sendFocus
) {
777 term
.element
.classList
.add('focus');
779 Terminal
.focus
= term
;
780 term
.emit('focus', {terminal
: term
});
785 * Blur the terminal. Delegates blur handling to the terminal's DOM element.
787 Terminal
.prototype.blur = function() {
788 return this.textarea
.blur();
792 * Binds the desired blur behavior on a given terminal object.
796 Terminal
.bindBlur = function (term
) {
797 on(term
.textarea
, 'blur', function (ev
) {
798 term
.refresh(term
.y
, term
.y
);
799 if (term
.sendFocus
) {
802 term
.element
.classList
.remove('focus');
803 Terminal
.focus
= null;
804 term
.emit('blur', {terminal
: term
});
809 * Initialize default behavior
811 Terminal
.prototype.initGlobal = function() {
812 Terminal
.bindPaste(this);
813 Terminal
.bindKeys(this);
814 Terminal
.bindCopy(this);
815 Terminal
.bindFocus(this);
816 Terminal
.bindBlur(this);
820 * Bind to paste event and allow both keyboard and right-click pasting, without having the
821 * contentEditable value set to true.
823 Terminal
.bindPaste = function(term
) {
824 on([term
.textarea
, term
.element
], 'paste', function(ev
) {
825 ev
.stopPropagation();
826 if (ev
.clipboardData
) {
827 var text
= ev
.clipboardData
.getData('text/plain');
829 term
.textarea
.value
= '';
830 return term
.cancel(ev
);
836 * Prepares text copied from terminal selection, to be saved in the clipboard by:
837 * 1. stripping all trailing white spaces
838 * 2. converting all non-breaking spaces to regular spaces
839 * @param {string} text The copied text that needs processing for storing in clipboard
843 Terminal
.prepareCopiedTextForClipboard = function (text
) {
844 var space
= String
.fromCharCode(32),
845 nonBreakingSpace
= String
.fromCharCode(160),
846 allNonBreakingSpaces
= new RegExp(nonBreakingSpace
, 'g'),
847 processedText
= text
.split('\n').map(function (line
) {
849 * Strip all trailing white spaces and convert all non-breaking spaces to regular
852 var processedLine
= line
.replace(/\s+$/g, '').replace(allNonBreakingSpaces
, space
);
854 return processedLine
;
857 return processedText
;
861 * Apply key handling to the terminal
863 Terminal
.bindKeys = function(term
) {
864 on(term
.element
, 'keydown', function(ev
) {
865 if (document
.activeElement
!= this) {
871 on(term
.element
, 'keypress', function(ev
) {
872 if (document
.activeElement
!= this) {
878 on(term
.element
, 'keyup', term
.focus
.bind(term
));
880 on(term
.textarea
, 'keydown', function(ev
) {
884 on(term
.textarea
, 'keypress', function(ev
) {
886 // Truncate the textarea's value, since it is not needed
890 on(term
.textarea
, 'compositionstart', term
.compositionHelper
.compositionstart
.bind(term
.compositionHelper
));
891 on(term
.textarea
, 'compositionupdate', term
.compositionHelper
.compositionupdate
.bind(term
.compositionHelper
));
892 on(term
.textarea
, 'compositionend', term
.compositionHelper
.compositionend
.bind(term
.compositionHelper
));
893 term
.on('refresh', term
.compositionHelper
.updateCompositionElements
.bind(term
.compositionHelper
));
897 * Binds copy functionality to the given terminal.
900 Terminal
.bindCopy = function(term
) {
901 on(term
.element
, 'copy', function(ev
) {
908 * Insert the given row to the terminal or produce a new one
909 * if no row argument is passed. Return the inserted row.
910 * @param {HTMLElement} row (optional) The row to append to the terminal.
912 Terminal
.prototype.insertRow = function (row
) {
913 if (typeof row
!= 'object') {
914 row
= document
.createElement('div');
917 this.rowContainer
.appendChild(row
);
918 this.children
.push(row
);
924 * Opens the terminal within an element.
926 * @param {HTMLElement} parent The element to create the terminal within.
928 Terminal
.prototype.open = function(parent
) {
929 var self
=this, i
=0, div
;
931 this.parent
= parent
|| this.parent
;
934 throw new Error('Terminal requires a parent element.');
938 * Grab global elements
940 this.context
= this.parent
.ownerDocument
.defaultView
;
941 this.document
= this.parent
.ownerDocument
;
942 this.body
= this.document
.getElementsByTagName('body')[0];
947 if (this.context
.navigator
&& this.context
.navigator
.userAgent
) {
948 this.isMSIE
= !!~this.context
.navigator
.userAgent
.indexOf('MSIE');
952 * Find the users platform. We use this to interpret the meta key
953 * and ISO third level shifts.
954 * http://stackoverflow.com/questions/19877924/what-is-the-list-of-possible-values-for-navigator-platform-as-of-today
956 if (this.context
.navigator
&& this.context
.navigator
.platform
) {
957 this.isMac
= contains(
958 this.context
.navigator
.platform
,
959 ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K']
961 this.isIpad
= this.context
.navigator
.platform
=== 'iPad';
962 this.isIphone
= this.context
.navigator
.platform
=== 'iPhone';
963 this.isMSWindows
= contains(
964 this.context
.navigator
.platform
,
965 ['Windows', 'Win16', 'Win32', 'WinCE']
970 * Create main element container
972 this.element
= this.document
.createElement('div');
973 this.element
.classList
.add('terminal');
974 this.element
.classList
.add('xterm');
975 this.element
.classList
.add('xterm-theme-' + this.theme
);
977 this.element
.style
.height
978 this.element
.setAttribute('tabindex', 0);
980 this.viewportElement
= document
.createElement('div');
981 this.viewportElement
.classList
.add('xterm-viewport');
982 this.element
.appendChild(this.viewportElement
);
983 this.viewportScrollArea
= document
.createElement('div');
984 this.viewportScrollArea
.classList
.add('xterm-scroll-area');
985 this.viewportElement
.appendChild(this.viewportScrollArea
);
988 * Create the container that will hold the lines of the terminal and then
989 * produce the lines the lines.
991 this.rowContainer
= document
.createElement('div');
992 this.rowContainer
.classList
.add('xterm-rows');
993 this.element
.appendChild(this.rowContainer
);
997 * Create the container that will hold helpers like the textarea for
998 * capturing DOM Events. Then produce the helpers.
1000 this.helperContainer
= document
.createElement('div');
1001 this.helperContainer
.classList
.add('xterm-helpers');
1002 // TODO: This should probably be inserted once it's filled to prevent an additional layout
1003 this.element
.appendChild(this.helperContainer
);
1004 this.textarea
= document
.createElement('textarea');
1005 this.textarea
.classList
.add('xterm-helper-textarea');
1006 this.textarea
.setAttribute('autocorrect', 'off');
1007 this.textarea
.setAttribute('autocapitalize', 'off');
1008 this.textarea
.setAttribute('spellcheck', 'false');
1009 this.textarea
.tabIndex
= 0;
1010 this.textarea
.addEventListener('focus', function() {
1011 self
.emit('focus', {terminal
: self
});
1013 this.textarea
.addEventListener('blur', function() {
1014 self
.emit('blur', {terminal
: self
});
1016 this.helperContainer
.appendChild(this.textarea
);
1018 this.compositionView
= document
.createElement('div');
1019 this.compositionView
.classList
.add('composition-view');
1020 this.compositionHelper
= new CompositionHelper(this.textarea
, this.compositionView
, this);
1021 this.helperContainer
.appendChild(this.compositionView
);
1023 this.charMeasureElement
= document
.createElement('div');
1024 this.charMeasureElement
.classList
.add('xterm-char-measure-element');
1025 this.charMeasureElement
.innerHTML
= 'W';
1026 this.helperContainer
.appendChild(this.charMeasureElement
);
1028 for (; i
< this.rows
; i
++) {
1031 this.parent
.appendChild(this.element
);
1033 this.viewport
= new Viewport(this, this.viewportElement
, this.viewportScrollArea
, this.charMeasureElement
);
1036 this.refresh(0, this.rows
- 1);
1038 // Initialize global actions that
1039 // need to be taken on the document.
1042 // Ensure there is a Terminal.focus.
1045 on(this.element
, 'mouseup', function() {
1046 var selection
= document
.getSelection(),
1047 collapsed
= selection
.isCollapsed
,
1048 isRange
= typeof collapsed
== 'boolean' ? !collapsed
: selection
.type
== 'Range';
1054 // Listen for mouse events and translate
1055 // them into terminal mouse protocols.
1058 // Figure out whether boldness affects
1059 // the character width of monospace fonts.
1060 if (Terminal
.brokenBold
== null) {
1061 Terminal
.brokenBold
= isBoldBroken(this.document
);
1069 * Attempts to load an add-on using CommonJS or RequireJS (whichever is available).
1070 * @param {string} addon The name of the addon to load
1073 Terminal
.loadAddon = function(addon
, callback
) {
1074 if (typeof exports
=== 'object' && typeof module
=== 'object') {
1076 return require(__dirname
+ '/../addons/' + addon
);
1077 } else if (typeof define
== 'function') {
1079 return require(['../addons/' + addon
+ '/' + addon
], callback
);
1081 console
.error('Cannot load a module without a CommonJS or RequireJS environment.');
1088 * XTerm mouse events
1089 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#Mouse%20Tracking
1090 * To better understand these
1091 * the xterm code is very helpful:
1093 * button.c, charproc.c, misc.c
1094 * Relevant functions in xterm/button.c:
1095 * BtnCode, EmitButtonCode, EditorButton, SendMousePosition
1097 Terminal
.prototype.bindMouse = function() {
1098 var el
= this.element
, self
= this, pressed
= 32;
1100 // mouseup, mousedown, wheel
1101 // left click: ^[[M 3<^[[M#3<
1102 // wheel up: ^[[M`3>
1103 function sendButton(ev
) {
1107 // get the xterm-style button
1108 button
= getButton(ev
);
1110 // get mouse coordinates
1111 pos
= getCoords(ev
);
1114 sendEvent(button
, pos
);
1116 switch (ev
.overrideType
|| ev
.type
) {
1121 // keep it at the left
1122 // button, just in case.
1133 // motion example of a left click:
1134 // ^[[M 3<^[[M@4<^[[M@5<^[[M@6<^[[M@7<^[[M#7<
1135 function sendMove(ev
) {
1136 var button
= pressed
1139 pos
= getCoords(ev
);
1142 // buttons marked as motions
1143 // are incremented by 32
1146 sendEvent(button
, pos
);
1149 // encode button and
1150 // position to characters
1151 function encode(data
, ch
) {
1152 if (!self
.utfMouse
) {
1153 if (ch
=== 255) return data
.push(0);
1154 if (ch
> 127) ch
= 127;
1157 if (ch
=== 2047) return data
.push(0);
1161 if (ch
> 2047) ch
= 2047;
1162 data
.push(0xC0 | (ch
>> 6));
1163 data
.push(0x80 | (ch
& 0x3F));
1168 // send a mouse event:
1169 // regular/utf8: ^[[M Cb Cx Cy
1170 // urxvt: ^[[ Cb ; Cx ; Cy M
1171 // sgr: ^[[ Cb ; Cx ; Cy M/m
1172 // vt300: ^[[ 24(1/3/5)~ [ Cx , Cy ] \r
1173 // locator: CSI P e ; P b ; P r ; P c ; P p & w
1174 function sendEvent(button
, pos
) {
1175 // self.emit('mouse', {
1181 if (self
.vt300Mouse
) {
1183 // http://www.vt100.net/docs/vt3xx-gp/chapter15.html
1187 var data
= '\x1b[24';
1188 if (button
=== 0) data
+= '1';
1189 else if (button
=== 1) data
+= '3';
1190 else if (button
=== 2) data
+= '5';
1191 else if (button
=== 3) return;
1193 data
+= '~[' + pos
.x
+ ',' + pos
.y
+ ']\r';
1198 if (self
.decLocator
) {
1203 if (button
=== 0) button
= 2;
1204 else if (button
=== 1) button
= 4;
1205 else if (button
=== 2) button
= 6;
1206 else if (button
=== 3) button
= 3;
1210 + (button
=== 3 ? 4 : 0)
1221 if (self
.urxvtMouse
) {
1226 self
.send('\x1b[' + button
+ ';' + pos
.x
+ ';' + pos
.y
+ 'M');
1230 if (self
.sgrMouse
) {
1234 + ((button
& 3) === 3 ? button
& ~3 : button
)
1239 + ((button
& 3) === 3 ? 'm' : 'M'));
1245 encode(data
, button
);
1246 encode(data
, pos
.x
);
1247 encode(data
, pos
.y
);
1249 self
.send('\x1b[M' + String
.fromCharCode
.apply(String
, data
));
1252 function getButton(ev
) {
1265 // 1, and 2 - with 64 added
1266 switch (ev
.overrideType
|| ev
.type
) {
1268 button
= ev
.button
!= null
1275 button
= button
=== 1 ? 0 : button
=== 4 ? 1 : button
;
1281 case 'DOMMouseScroll':
1282 button
= ev
.detail
< 0
1287 button
= ev
.wheelDeltaY
> 0
1293 // next three bits are the modifiers:
1294 // 4 = shift, 8 = meta, 16 = control
1295 shift
= ev
.shiftKey
? 4 : 0;
1296 meta
= ev
.metaKey
? 8 : 0;
1297 ctrl
= ev
.ctrlKey
? 16 : 0;
1298 mod
= shift
| meta
| ctrl
;
1301 if (self
.vt200Mouse
) {
1304 } else if (!self
.normalMouse
) {
1309 button
= (32 + (mod
<< 2)) + button
;
1314 // mouse coordinates measured in cols/rows
1315 function getCoords(ev
) {
1318 // ignore browsers without pageX for now
1319 if (ev
.pageX
== null) return;
1325 // should probably check offsetParent
1326 // but this is more portable
1327 while (el
&& el
!== self
.document
.documentElement
) {
1330 el
= 'offsetParent' in el
1335 // convert to cols/rows
1336 w
= self
.element
.clientWidth
;
1337 h
= self
.element
.clientHeight
;
1338 x
= Math
.ceil((x
/ w
) * self
.cols
);
1339 y
= Math
.ceil((y
/ h
) * self
.rows
);
1341 // be sure to avoid sending
1342 // bad positions to the program
1344 if (x
> self
.cols
) x
= self
.cols
;
1346 if (y
> self
.rows
) y
= self
.rows
;
1348 // xterm sends raw bytes and
1349 // starts at 32 (SP) for each.
1360 on(el
, 'mousedown', function(ev
) {
1361 if (!self
.mouseEvents
) return;
1370 //if (self.vt200Mouse && !self.normalMouse) {
1371 if (self
.vt200Mouse
) {
1372 ev
.overrideType
= 'mouseup';
1374 return self
.cancel(ev
);
1378 if (self
.normalMouse
) on(self
.document
, 'mousemove', sendMove
);
1380 // x10 compatibility mode can't send button releases
1381 if (!self
.x10Mouse
) {
1382 on(self
.document
, 'mouseup', function up(ev
) {
1384 if (self
.normalMouse
) off(self
.document
, 'mousemove', sendMove
);
1385 off(self
.document
, 'mouseup', up
);
1386 return self
.cancel(ev
);
1390 return self
.cancel(ev
);
1393 //if (self.normalMouse) {
1394 // on(self.document, 'mousemove', sendMove);
1397 on(el
, 'wheel', function(ev
) {
1398 if (!self
.mouseEvents
) return;
1401 || self
.decLocator
) return;
1403 return self
.cancel(ev
);
1406 // allow wheel scrolling in
1407 // the shell for example
1408 on(el
, 'wheel', function(ev
) {
1409 if (self
.mouseEvents
) return;
1410 if (self
.applicationKeypad
) return;
1411 self
.viewport
.onWheel(ev
);
1412 return self
.cancel(ev
);
1417 * Destroys the terminal.
1419 Terminal
.prototype.destroy = function() {
1420 this.readable
= false;
1421 this.writable
= false;
1423 this.handler = function() {};
1424 this.write = function() {};
1425 if (this.element
.parentNode
) {
1426 this.element
.parentNode
.removeChild(this.element
);
1428 //this.emit('close');
1433 * Flags used to render terminal text properly
1444 * Refreshes (re-renders) terminal content within two rows (inclusive)
1448 * In the screen buffer, each character is stored as a an array with a character
1449 * and a 32-bit integer:
1450 * - First value: a utf-16 character.
1452 * - Next 9 bits: background color (0-511).
1453 * - Next 9 bits: foreground color (0-511).
1454 * - Next 14 bits: a mask for misc. flags:
1461 * @param {number} start The row to start from (between 0 and terminal's height terminal - 1)
1462 * @param {number} end The row to end at (between fromRow and terminal's height terminal - 1)
1463 * @param {boolean} queue Whether the refresh should ran right now or be queued
1465 Terminal
.prototype.refresh = function(start
, end
, queue
) {
1468 // queue defaults to true
1469 queue
= (typeof queue
== 'undefined') ? true : queue
;
1472 * The refresh queue allows refresh to execute only approximately 30 times a second. For
1473 * commands that pass a significant amount of output to the write function, this prevents the
1474 * terminal from maxing out the CPU and making the UI unresponsive. While commands can still
1475 * run beyond what they do on the terminal, it is far better with a debounce in place as
1476 * every single terminal manipulation does not need to be constructed in the DOM.
1478 * A side-effect of this is that it makes ^C to interrupt a process seem more responsive.
1481 // If refresh should be queued, order the refresh and return.
1482 if (this._refreshIsQueued
) {
1483 // If a refresh has already been queued, just order a full refresh next
1484 this._fullRefreshNext
= true;
1486 setTimeout(function () {
1487 self
.refresh(start
, end
, false);
1489 this._refreshIsQueued
= true;
1494 // If refresh should be run right now (not be queued), release the lock
1495 this._refreshIsQueued
= false;
1497 // If multiple refreshes were requested, make a full refresh.
1498 if (this._fullRefreshNext
) {
1500 end
= this.rows
- 1;
1501 this._fullRefreshNext
= false // reset lock
1504 var x
, y
, i
, line
, out
, ch
, ch_width
, width
, data
, attr
, bg
, fg
, flags
, row
, parent
, focused
= document
.activeElement
;
1506 // If this is a big refresh, remove the terminal rows from the DOM for faster calculations
1507 if (end
- start
>= this.rows
/ 2) {
1508 parent
= this.element
.parentNode
;
1510 this.element
.removeChild(this.rowContainer
);
1517 if (end
>= this.rows
.length
) {
1518 this.log('`end` is too large. Most likely a bad CSR.');
1519 end
= this.rows
.length
- 1;
1522 for (; y
<= end
; y
++) {
1523 row
= y
+ this.ydisp
;
1525 line
= this.lines
[row
];
1528 if (this.y
=== y
- (this.ybase
- this.ydisp
)
1530 && !this.cursorHidden
) {
1536 attr
= this.defAttr
;
1539 for (; i
< width
; i
++) {
1542 ch_width
= line
[i
][2];
1546 if (i
=== x
) data
= -1;
1548 if (data
!== attr
) {
1549 if (attr
!== this.defAttr
) {
1552 if (data
!== this.defAttr
) {
1554 out
+= '<span class="reverse-video terminal-cursor';
1555 if (this.cursorBlink
) {
1560 var classNames
= [];
1563 fg
= (data
>> 9) & 0x1ff;
1566 if (flags
& Terminal
.flags
.BOLD
) {
1567 if (!Terminal
.brokenBold
) {
1568 classNames
.push('xterm-bold');
1570 // See: XTerm*boldColors
1571 if (fg
< 8) fg
+= 8;
1574 if (flags
& Terminal
.flags
.UNDERLINE
) {
1575 classNames
.push('xterm-underline');
1578 if (flags
& Terminal
.flags
.BLINK
) {
1579 classNames
.push('xterm-blink');
1583 * If inverse flag is on, then swap the foreground and background variables.
1585 if (flags
& Terminal
.flags
.INVERSE
) {
1586 /* One-line variable swap in JavaScript: http://stackoverflow.com/a/16201730 */
1587 bg = [fg, fg = bg][0];
1588 // Should inverse just be before the
1589 // above boldColors effect instead?
1590 if ((flags & 1) && fg < 8) fg += 8;
1593 if (flags & Terminal.flags.INVISIBLE) {
1594 classNames.push('xterm-hidden');
1598 * Weird situation: Invert flag used black foreground and white background results
1599 * in invalid background color, positioned at the 256 index of the 256 terminal
1600 * color map. Pin the colors manually in such a case.
1602 * Source: https://github.com/sourcelair/xterm.js/issues/57
1604 if (flags
& Terminal
.flags
.INVERSE
) {
1614 classNames
.push('xterm-bg-color-' + bg
);
1618 classNames
.push('xterm-color-' + fg
);
1622 if (classNames
.length
) {
1623 out
+= ' class="' + classNames
.join(' ') + '"';
1652 if (attr
!== this.defAttr
) {
1656 this.children
[y
].innerHTML
= out
;
1660 this.element
.appendChild(this.rowContainer
);
1663 this.emit('refresh', {element
: this.element
, start
: start
, end
: end
});
1667 * Display the cursor element
1669 Terminal
.prototype.showCursor = function() {
1670 if (!this.cursorState
) {
1671 this.cursorState
= 1;
1672 this.refresh(this.y
, this.y
);
1677 * Scroll the terminal
1679 Terminal
.prototype.scroll = function() {
1682 if (++this.ybase
=== this.scrollback
) {
1683 this.ybase
= this.ybase
/ 2 | 0;
1684 this.lines
= this.lines
.slice(-(this.ybase
+ this.rows
) + 1);
1687 this.ydisp
= this.ybase
;
1690 row
= this.ybase
+ this.rows
- 1;
1692 // subtract the bottom scroll region
1693 row
-= this.rows
- 1 - this.scrollBottom
;
1695 if (row
=== this.lines
.length
) {
1696 // potential optimization:
1697 // pushing is faster than splicing
1698 // when they amount to the same
1700 this.lines
.push(this.blankLine());
1703 this.lines
.splice(row
, 0, this.blankLine());
1706 if (this.scrollTop
!== 0) {
1707 if (this.ybase
!== 0) {
1709 this.ydisp
= this.ybase
;
1711 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
1715 this.updateRange(this.scrollTop
);
1716 this.updateRange(this.scrollBottom
);
1718 this.emit('scroll', this.ydisp
);
1722 * Scroll the display of the terminal
1723 * @param {number} disp The number of lines to scroll down (negatives scroll up).
1724 * @param {boolean} suppressScrollEvent Don't emit the scroll event as scrollDisp. This is used
1725 * to avoid unwanted events being handled by the veiwport when the event was triggered from the
1726 * viewport originally.
1728 Terminal
.prototype.scrollDisp = function(disp
, suppressScrollEvent
) {
1731 if (this.ydisp
> this.ybase
) {
1732 this.ydisp
= this.ybase
;
1733 } else if (this.ydisp
< 0) {
1737 if (!suppressScrollEvent
) {
1738 this.emit('scroll', this.ydisp
);
1741 this.refresh(0, this.rows
- 1);
1745 * Writes text to the terminal.
1746 * @param {string} text The text to write to the terminal.
1748 Terminal
.prototype.write = function(data
) {
1749 var l
= data
.length
, i
= 0, j
, cs
, ch
, code
, low
, ch_width
, row
;
1751 this.refreshStart
= this.y
;
1752 this.refreshEnd
= this.y
;
1754 if (this.ybase
!== this.ydisp
) {
1755 this.ydisp
= this.ybase
;
1756 this.emit('scroll', this.ydisp
);
1760 // apply leftover surrogate high from last write
1761 if (this.surrogate_high
) {
1762 data
= this.surrogate_high
+ data
;
1763 this.surrogate_high
= '';
1766 for (; i
< l
; i
++) {
1769 // FIXME: higher chars than 0xa0 are not allowed in escape sequences
1770 // --> maybe move to default
1771 code
= data
.charCodeAt(i
);
1772 if (0xD800 <= code
&& code
<= 0xDBFF) {
1773 // we got a surrogate high
1774 // get surrogate low (next 2 bytes)
1775 low
= data
.charCodeAt(i
+1);
1777 // end of data stream, save surrogate high
1778 this.surrogate_high
= ch
;
1781 code
= ((code
- 0xD800) * 0x400) + (low
- 0xDC00) + 0x10000;
1782 ch
+= data
.charAt(i
+1);
1784 // surrogate low - already handled above
1785 if (0xDC00 <= code
&& code
<= 0xDFFF)
1788 switch (this.state
) {
1799 if (this.convertEol
) {
1803 if (this.y
> this.scrollBottom
) {
1823 this.x
= this.nextStop();
1838 this.state
= escaped
;
1843 // calculate print space
1844 // expensive call, therefore we save width in line buffer
1845 ch_width
= wcwidth(code
);
1848 if (this.charset
&& this.charset
[ch
]) {
1849 ch
= this.charset
[ch
];
1852 row
= this.y
+ this.ybase
;
1854 // insert combining char in last cell
1855 // FIXME: needs handling after cursor jumps
1856 if (!ch_width
&& this.x
) {
1858 // dont overflow left
1859 if (this.lines
[row
][this.x
-1]) {
1860 if (!this.lines
[row
][this.x
-1][2]) {
1862 // found empty cell after fullwidth, need to go 2 cells back
1863 if (this.lines
[row
][this.x
-2])
1864 this.lines
[row
][this.x
-2][1] += ch
;
1867 this.lines
[row
][this.x
-1][1] += ch
;
1869 this.updateRange(this.y
);
1874 // goto next line if ch would overflow
1875 // TODO: needs a global min terminal width of 2
1876 if (this.x
+ch_width
-1 >= this.cols
) {
1877 // autowrap - DECAWM
1878 if (this.wraparoundMode
) {
1881 if (this.y
> this.scrollBottom
) {
1886 this.x
= this.cols
-1;
1887 if(ch_width
===2) // FIXME: check for xterm behavior
1891 row
= this.y
+ this.ybase
;
1893 // insert mode: move characters to right
1894 if (this.insertMode
) {
1895 // do this twice for a fullwidth char
1896 for (var moves
=0; moves
<ch_width
; ++moves
) {
1897 // remove last cell, if it's width is 0
1898 // we have to adjust the second last cell as well
1899 var removed
= this.lines
[this.y
+ this.ybase
].pop();
1901 && this.lines
[row
][this.cols
-2]
1902 && this.lines
[row
][this.cols
-2][2]===2)
1903 this.lines
[row
][this.cols
-2] = [this.curAttr
, ' ', 1];
1905 // insert empty cell at cursor
1906 this.lines
[row
].splice(this.x
, 0, [this.curAttr
, ' ', 1]);
1910 this.lines
[row
][this.x
] = [this.curAttr
, ch
, ch_width
];
1912 this.updateRange(this.y
);
1914 // fullwidth char - set next cell width to zero and advance cursor
1916 this.lines
[row
][this.x
] = [this.curAttr
, '', 0];
1925 // ESC [ Control Sequence Introducer ( CSI is 0x9b).
1928 this.currentParam
= 0;
1932 // ESC ] Operating System Command ( OSC is 0x9d).
1935 this.currentParam
= 0;
1939 // ESC P Device Control String ( DCS is 0x90).
1942 this.currentParam
= 0;
1946 // ESC _ Application Program Command ( APC is 0x9f).
1948 this.state
= ignore
;
1951 // ESC ^ Privacy Message ( PM is 0x9e).
1953 this.state
= ignore
;
1956 // ESC c Full Reset (RIS).
1961 // ESC E Next Line ( NEL is 0x85).
1962 // ESC D Index ( IND is 0x84).
1970 // ESC M Reverse Index ( RI is 0x8d).
1972 this.reverseIndex();
1975 // ESC % Select default/utf-8 character set.
1976 // @ = default, G = utf-8
1978 //this.charset = null;
1980 this.setgCharset(0, Terminal
.charsets
.US
);
1981 this.state
= normal
;
1985 // ESC (,),*,+,-,. Designate G0-G2 Character Set.
1986 case '(': // <-- this seems to get all the attention
2012 this.state
= charset
;
2015 // Designate G3 Character Set (VT300).
2016 // A = ISO Latin-1 Supplemental.
2020 this.state
= charset
;
2025 // Single Shift Select of G2 Character Set
2026 // ( SS2 is 0x8e). This affects next character only.
2030 // Single Shift Select of G3 Character Set
2031 // ( SS3 is 0x8f). This affects next character only.
2035 // Invoke the G2 Character Set as GL (LS2).
2040 // Invoke the G3 Character Set as GL (LS3).
2045 // Invoke the G3 Character Set as GR (LS3R).
2050 // Invoke the G2 Character Set as GR (LS2R).
2055 // Invoke the G1 Character Set as GR (LS1R).
2060 // ESC 7 Save Cursor (DECSC).
2063 this.state
= normal
;
2066 // ESC 8 Restore Cursor (DECRC).
2068 this.restoreCursor();
2069 this.state
= normal
;
2072 // ESC # 3 DEC line height/width
2074 this.state
= normal
;
2078 // ESC H Tab Set (HTS is 0x88).
2083 // ESC = Application Keypad (DECKPAM).
2085 this.log('Serial port requested application keypad.');
2086 this.applicationKeypad
= true;
2087 this.viewport
.setApplicationMode(true);
2088 this.state
= normal
;
2091 // ESC > Normal Keypad (DECKPNM).
2093 this.log('Switching back to normal keypad.');
2094 this.applicationKeypad
= false;
2095 this.viewport
.setApplicationMode(false);
2096 this.state
= normal
;
2100 this.state
= normal
;
2101 this.error('Unknown ESC control: %s.', ch
);
2108 case '0': // DEC Special Character and Line Drawing Set.
2109 cs
= Terminal
.charsets
.SCLD
;
2112 cs
= Terminal
.charsets
.UK
;
2114 case 'B': // United States (USASCII).
2115 cs
= Terminal
.charsets
.US
;
2118 cs
= Terminal
.charsets
.Dutch
;
2120 case 'C': // Finnish
2122 cs
= Terminal
.charsets
.Finnish
;
2125 cs
= Terminal
.charsets
.French
;
2127 case 'Q': // FrenchCanadian
2128 cs
= Terminal
.charsets
.FrenchCanadian
;
2131 cs
= Terminal
.charsets
.German
;
2133 case 'Y': // Italian
2134 cs
= Terminal
.charsets
.Italian
;
2136 case 'E': // NorwegianDanish
2138 cs
= Terminal
.charsets
.NorwegianDanish
;
2140 case 'Z': // Spanish
2141 cs
= Terminal
.charsets
.Spanish
;
2143 case 'H': // Swedish
2145 cs
= Terminal
.charsets
.Swedish
;
2148 cs
= Terminal
.charsets
.Swiss
;
2150 case '/': // ISOLatin (actually /A)
2151 cs
= Terminal
.charsets
.ISOLatin
;
2155 cs
= Terminal
.charsets
.US
;
2158 this.setgCharset(this.gcharset
, cs
);
2159 this.gcharset
= null;
2160 this.state
= normal
;
2166 // Set Text Parameters.
2167 if (ch
=== '\x1b' || ch
=== '\x07') {
2168 if (ch
=== '\x1b') i
++;
2170 this.params
.push(this.currentParam
);
2172 switch (this.params
[0]) {
2176 if (this.params
[1]) {
2177 this.title
= this.params
[1];
2178 this.handleTitle(this.title
);
2186 // change dynamic colors
2198 // change dynamic ui colors
2210 // manipulate selection data
2228 this.currentParam
= 0;
2229 this.state
= normal
;
2231 if (!this.params
.length
) {
2232 if (ch
>= '0' && ch
<= '9') {
2234 this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
2235 } else if (ch
=== ';') {
2236 this.params
.push(this.currentParam
);
2237 this.currentParam
= '';
2240 this.currentParam
+= ch
;
2247 if (ch
=== '?' || ch
=== '>' || ch
=== '!') {
2253 if (ch
>= '0' && ch
<= '9') {
2254 this.currentParam
= this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
2258 // '$', '"', ' ', '\''
2259 if (ch
=== '$' || ch
=== '"' || ch
=== ' ' || ch
=== '\'') {
2264 this.params
.push(this.currentParam
);
2265 this.currentParam
= 0;
2268 if (ch
=== ';') break;
2270 this.state
= normal
;
2274 // Cursor Up Ps Times (default = 1) (CUU).
2276 this.cursorUp(this.params
);
2280 // Cursor Down Ps Times (default = 1) (CUD).
2282 this.cursorDown(this.params
);
2286 // Cursor Forward Ps Times (default = 1) (CUF).
2288 this.cursorForward(this.params
);
2292 // Cursor Backward Ps Times (default = 1) (CUB).
2294 this.cursorBackward(this.params
);
2298 // Cursor Position [row;column] (default = [1,1]) (CUP).
2300 this.cursorPos(this.params
);
2303 // CSI Ps J Erase in Display (ED).
2305 this.eraseInDisplay(this.params
);
2308 // CSI Ps K Erase in Line (EL).
2310 this.eraseInLine(this.params
);
2313 // CSI Pm m Character Attributes (SGR).
2316 this.charAttributes(this.params
);
2320 // CSI Ps n Device Status Report (DSR).
2323 this.deviceStatus(this.params
);
2332 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
2334 this.insertChars(this.params
);
2338 // Cursor Next Line Ps Times (default = 1) (CNL).
2340 this.cursorNextLine(this.params
);
2344 // Cursor Preceding Line Ps Times (default = 1) (CNL).
2346 this.cursorPrecedingLine(this.params
);
2350 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
2352 this.cursorCharAbsolute(this.params
);
2356 // Insert Ps Line(s) (default = 1) (IL).
2358 this.insertLines(this.params
);
2362 // Delete Ps Line(s) (default = 1) (DL).
2364 this.deleteLines(this.params
);
2368 // Delete Ps Character(s) (default = 1) (DCH).
2370 this.deleteChars(this.params
);
2374 // Erase Ps Character(s) (default = 1) (ECH).
2376 this.eraseChars(this.params
);
2379 // CSI Pm ` Character Position Absolute
2380 // [column] (default = [row,1]) (HPA).
2382 this.charPosAbsolute(this.params
);
2386 // Horizontal Position Relative
2388 this.HPositionRelative(this.params
);
2392 // Send Device Attributes (Primary DA).
2394 // Send Device Attributes (Secondary DA)
2396 this.sendDeviceAttributes(this.params
);
2400 // Line Position Absolute [row] (default = [1,column]) (VPA).
2402 this.linePosAbsolute(this.params
);
2405 // 145 65 e * VPR - Vertical Position Relative
2407 this.VPositionRelative(this.params
);
2411 // Horizontal and Vertical Position [row;column] (default =
2414 this.HVPosition(this.params
);
2417 // CSI Pm h Set Mode (SM).
2418 // CSI ? Pm h - mouse escape codes, cursor escape codes
2420 this.setMode(this.params
);
2423 // CSI Pm l Reset Mode (RM).
2426 this.resetMode(this.params
);
2430 // Set Scrolling Region [top;bottom] (default = full size of win-
2434 this.setScrollRegion(this.params
);
2438 // Save cursor (ANSI.SYS).
2440 this.saveCursor(this.params
);
2444 // Restore cursor (ANSI.SYS).
2446 this.restoreCursor(this.params
);
2454 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
2456 this.cursorForwardTab(this.params
);
2459 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
2461 this.scrollUp(this.params
);
2464 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
2465 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
2468 // if (this.prefix === '>') {
2469 // this.resetTitleModes(this.params);
2472 // if (this.params.length > 2) {
2473 // this.initMouseTracking(this.params);
2476 if (this.params
.length
< 2 && !this.prefix
) {
2477 this.scrollDown(this.params
);
2482 // Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
2484 this.cursorBackwardTab(this.params
);
2487 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
2489 this.repeatPrecedingCharacter(this.params
);
2492 // CSI Ps g Tab Clear (TBC).
2494 this.tabClear(this.params
);
2497 // CSI Pm i Media Copy (MC).
2500 // this.mediaCopy(this.params);
2503 // CSI Pm m Character Attributes (SGR).
2505 // case 'm': // duplicate
2506 // if (this.prefix === '>') {
2507 // this.setResources(this.params);
2509 // this.charAttributes(this.params);
2513 // CSI Ps n Device Status Report (DSR).
2515 // case 'n': // duplicate
2516 // if (this.prefix === '>') {
2517 // this.disableModifiers(this.params);
2519 // this.deviceStatus(this.params);
2523 // CSI > Ps p Set pointer mode.
2524 // CSI ! p Soft terminal reset (DECSTR).
2526 // Request ANSI mode (DECRQM).
2528 // Request DEC private mode (DECRQM).
2531 switch (this.prefix
) {
2533 // this.setPointerMode(this.params);
2536 this.softReset(this.params
);
2539 // if (this.postfix === '$') {
2540 // this.requestPrivateMode(this.params);
2544 // if (this.postfix === '"') {
2545 // this.setConformanceLevel(this.params);
2546 // } else if (this.postfix === '$') {
2547 // this.requestAnsiMode(this.params);
2553 // CSI Ps q Load LEDs (DECLL).
2557 // if (this.postfix === ' ') {
2558 // this.setCursorStyle(this.params);
2561 // if (this.postfix === '"') {
2562 // this.setCharProtectionAttr(this.params);
2565 // this.loadLEDs(this.params);
2569 // Set Scrolling Region [top;bottom] (default = full size of win-
2572 // CSI Pt; Pl; Pb; Pr; Ps$ r
2573 // case 'r': // duplicate
2574 // if (this.prefix === '?') {
2575 // this.restorePrivateValues(this.params);
2576 // } else if (this.postfix === '$') {
2577 // this.setAttrInRectangle(this.params);
2579 // this.setScrollRegion(this.params);
2583 // CSI s Save cursor (ANSI.SYS).
2585 // case 's': // duplicate
2586 // if (this.prefix === '?') {
2587 // this.savePrivateValues(this.params);
2589 // this.saveCursor(this.params);
2593 // CSI Ps ; Ps ; Ps t
2594 // CSI Pt; Pl; Pb; Pr; Ps$ t
2598 // if (this.postfix === '$') {
2599 // this.reverseAttrInRectangle(this.params);
2600 // } else if (this.postfix === ' ') {
2601 // this.setWarningBellVolume(this.params);
2603 // if (this.prefix === '>') {
2604 // this.setTitleModeFeature(this.params);
2606 // this.manipulateWindow(this.params);
2611 // CSI u Restore cursor (ANSI.SYS).
2613 // case 'u': // duplicate
2614 // if (this.postfix === ' ') {
2615 // this.setMarginBellVolume(this.params);
2617 // this.restoreCursor(this.params);
2621 // CSI Pt; Pl; Pb; Pr; Pp; Pt; Pl; Pp$ v
2623 // if (this.postfix === '$') {
2624 // this.copyRectagle(this.params);
2628 // CSI Pt ; Pl ; Pb ; Pr ' w
2630 // if (this.postfix === '\'') {
2631 // this.enableFilterRectangle(this.params);
2635 // CSI Ps x Request Terminal Parameters (DECREQTPARM).
2636 // CSI Ps x Select Attribute Change Extent (DECSACE).
2637 // CSI Pc; Pt; Pl; Pb; Pr$ x
2639 // if (this.postfix === '$') {
2640 // this.fillRectangle(this.params);
2642 // this.requestParameters(this.params);
2643 // //this.__(this.params);
2648 // CSI Pt; Pl; Pb; Pr$ z
2650 // if (this.postfix === '\'') {
2651 // this.enableLocatorReporting(this.params);
2652 // } else if (this.postfix === '$') {
2653 // this.eraseRectangle(this.params);
2658 // CSI Pt; Pl; Pb; Pr$ {
2660 // if (this.postfix === '\'') {
2661 // this.setLocatorEvents(this.params);
2662 // } else if (this.postfix === '$') {
2663 // this.selectiveEraseRectangle(this.params);
2669 // if (this.postfix === '\'') {
2670 // this.requestLocatorPosition(this.params);
2675 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
2677 // if (this.postfix === ' ') {
2678 // this.insertColumns(this.params);
2683 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
2685 // if (this.postfix === ' ') {
2686 // this.deleteColumns(this.params);
2691 this.error('Unknown CSI code: %s.', ch
);
2700 if (ch
=== '\x1b' || ch
=== '\x07') {
2701 if (ch
=== '\x1b') i
++;
2703 switch (this.prefix
) {
2704 // User-Defined Keys (DECUDK).
2708 // Request Status String (DECRQSS).
2709 // test: echo -e '\eP$q"p\e\\'
2711 var pt
= this.currentParam
2728 + (this.scrollTop
+ 1)
2730 + (this.scrollBottom
+ 1)
2740 this.error('Unknown DCS Pt: %s.', pt
);
2745 this.send('\x1bP' + +valid
+ '$r' + pt
+ '\x1b\\');
2748 // Set Termcap/Terminfo Data (xterm, experimental).
2752 // Request Termcap/Terminfo String (xterm, experimental)
2753 // Regular xterm does not even respond to this sequence.
2754 // This can cause a small glitch in vim.
2755 // test: echo -ne '\eP+q6b64\e\\'
2757 var pt
= this.currentParam
2760 this.send('\x1bP' + +valid
+ '+r' + pt
+ '\x1b\\');
2764 this.error('Unknown DCS prefix: %s.', this.prefix
);
2768 this.currentParam
= 0;
2770 this.state
= normal
;
2771 } else if (!this.currentParam
) {
2772 if (!this.prefix
&& ch
!== '$' && ch
!== '+') {
2773 this.currentParam
= ch
;
2774 } else if (this.prefix
.length
=== 2) {
2775 this.currentParam
= ch
;
2780 this.currentParam
+= ch
;
2786 if (ch
=== '\x1b' || ch
=== '\x07') {
2787 if (ch
=== '\x1b') i
++;
2788 this.state
= normal
;
2794 this.updateRange(this.y
);
2795 this.refresh(this.refreshStart
, this.refreshEnd
);
2799 * Writes text to the terminal, followed by a break line character (\n).
2800 * @param {string} text The text to write to the terminal.
2802 Terminal
.prototype.writeln = function(data
) {
2803 this.write(data
+ '\r\n');
2807 * Attaches a custom keydown handler which is run before keys are processed, giving consumers of
2808 * xterm.js ultimate control as to what keys should be processed by the terminal and what keys
2810 * @param {function} customKeydownHandler The custom KeyboardEvent handler to attach. This is a
2811 * function that takes a KeyboardEvent, allowing consumers to stop propogation and/or prevent
2812 * the default action. The function returns whether the event should be processed by xterm.js.
2814 Terminal
.prototype.attachCustomKeydownHandler = function(customKeydownHandler
) {
2815 this.customKeydownHandler
= customKeydownHandler
;
2819 * Handle a keydown event
2821 * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
2822 * @param {KeyboardEvent} ev The keydown event to be handled.
2824 Terminal
.prototype.keyDown = function(ev
) {
2825 if (this.customKeydownHandler
&& this.customKeydownHandler(ev
) === false) {
2829 if (!this.compositionHelper
.keydown
.bind(this.compositionHelper
)(ev
)) {
2834 var result
= this.evaluateKeyEscapeSequence(ev
);
2836 if (result
.scrollDisp
) {
2837 this.scrollDisp(result
.scrollDisp
);
2838 return this.cancel(ev
);
2841 if (isThirdLevelShift(this, ev
)) {
2845 if (result
.cancel
) {
2846 // The event is canceled at the end already, is this necessary?
2847 this.cancel(ev
, true);
2854 this.emit('keydown', ev
);
2855 this.emit('key', result
.key
, ev
);
2857 this.handler(result
.key
);
2859 return this.cancel(ev
, true);
2863 * Returns an object that determines how a KeyboardEvent should be handled. The key of the
2864 * returned value is the new key code to pass to the PTY.
2866 * Reference: http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
2867 * @param {KeyboardEvent} ev The keyboard event to be translated to key escape sequence.
2869 Terminal
.prototype.evaluateKeyEscapeSequence = function(ev
) {
2871 // Whether to cancel event propogation (NOTE: this may not be needed since the event is
2872 // canceled at the end of keyDown
2874 // The new key even to emit
2876 // The number of characters to scroll, if this is defined it will cancel the event
2877 scrollDisp
: undefined
2879 var modifiers
= ev
.shiftKey
<< 0 | ev
.altKey
<< 1 | ev
.ctrlKey
<< 2 | ev
.metaKey
<< 3;
2880 switch (ev
.keyCode
) {
2884 result
.key
= '\x08'; // ^H
2887 result
.key
= '\x7f'; // ^?
2892 result
.key
= '\x1b[Z';
2896 result
.cancel
= true;
2901 result
.cancel
= true;
2905 result
.key
= '\x1b';
2906 result
.cancel
= true;
2911 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'D';
2912 // HACK: Make Alt + left-arrow behave like Ctrl + left-arrow: move one word backwards
2913 // http://unix.stackexchange.com/a/108106
2914 if (result
.key
== '\x1b[1;3D') {
2915 result
.key
= '\x1b[1;5D';
2917 } else if (this.applicationCursor
) {
2918 result
.key
= '\x1bOD';
2920 result
.key
= '\x1b[D';
2926 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'C';
2927 // HACK: Make Alt + right-arrow behave like Ctrl + right-arrow: move one word forward
2928 // http://unix.stackexchange.com/a/108106
2929 if (result
.key
== '\x1b[1;3C') {
2930 result
.key
= '\x1b[1;5C';
2932 } else if (this.applicationCursor
) {
2933 result
.key
= '\x1bOC';
2935 result
.key
= '\x1b[C';
2941 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'A';
2942 // HACK: Make Alt + up-arrow behave like Ctrl + up-arrow
2943 // http://unix.stackexchange.com/a/108106
2944 if (result
.key
== '\x1b[1;3A') {
2945 result
.key
= '\x1b[1;5A';
2947 } else if (this.applicationCursor
) {
2948 result
.key
= '\x1bOA';
2950 result
.key
= '\x1b[A';
2956 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'B';
2957 // HACK: Make Alt + down-arrow behave like Ctrl + down-arrow
2958 // http://unix.stackexchange.com/a/108106
2959 if (result
.key
== '\x1b[1;3B') {
2960 result
.key
= '\x1b[1;5B';
2962 } else if (this.applicationCursor
) {
2963 result
.key
= '\x1bOB';
2965 result
.key
= '\x1b[B';
2970 if (!ev
.shiftKey
&& !ev
.ctrlKey
) {
2971 // <Ctrl> or <Shift> + <Insert> are used to
2972 // copy-paste on some systems.
2973 result
.key
= '\x1b[2~';
2979 result
.key
= '\x1b[3;' + (modifiers
+ 1) + '~';
2981 result
.key
= '\x1b[3~';
2987 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'H';
2988 else if (this.applicationCursor
)
2989 result
.key
= '\x1bOH';
2991 result
.key
= '\x1b[H';
2996 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'F';
2997 else if (this.applicationCursor
)
2998 result
.key
= '\x1bOF';
3000 result
.key
= '\x1b[F';
3005 result
.scrollDisp
= -(this.rows
- 1);
3007 result
.key
= '\x1b[5~';
3013 result
.scrollDisp
= this.rows
- 1;
3015 result
.key
= '\x1b[6~';
3021 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'P';
3023 result
.key
= '\x1bOP';
3028 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'Q';
3030 result
.key
= '\x1bOQ';
3035 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'R';
3037 result
.key
= '\x1bOR';
3042 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'S';
3044 result
.key
= '\x1bOS';
3049 result
.key
= '\x1b[15;' + (modifiers
+ 1) + '~';
3051 result
.key
= '\x1b[15~';
3056 result
.key
= '\x1b[17;' + (modifiers
+ 1) + '~';
3058 result
.key
= '\x1b[17~';
3063 result
.key
= '\x1b[18;' + (modifiers
+ 1) + '~';
3065 result
.key
= '\x1b[18~';
3070 result
.key
= '\x1b[19;' + (modifiers
+ 1) + '~';
3072 result
.key
= '\x1b[19~';
3077 result
.key
= '\x1b[20;' + (modifiers
+ 1) + '~';
3079 result
.key
= '\x1b[20~';
3084 result
.key
= '\x1b[21;' + (modifiers
+ 1) + '~';
3086 result
.key
= '\x1b[21~';
3091 result
.key
= '\x1b[23;' + (modifiers
+ 1) + '~';
3093 result
.key
= '\x1b[23~';
3098 result
.key
= '\x1b[24;' + (modifiers
+ 1) + '~';
3100 result
.key
= '\x1b[24~';
3105 if (ev
.ctrlKey
&& !ev
.shiftKey
&& !ev
.altKey
&& !ev
.metaKey
) {
3106 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
3107 result
.key
= String
.fromCharCode(ev
.keyCode
- 64);
3108 } else if (ev
.keyCode
=== 32) {
3110 result
.key
= String
.fromCharCode(0);
3111 } else if (ev
.keyCode
>= 51 && ev
.keyCode
<= 55) {
3112 // escape, file sep, group sep, record sep, unit sep
3113 result
.key
= String
.fromCharCode(ev
.keyCode
- 51 + 27);
3114 } else if (ev
.keyCode
=== 56) {
3116 result
.key
= String
.fromCharCode(127);
3117 } else if (ev
.keyCode
=== 219) {
3119 result
.key
= String
.fromCharCode(27);
3120 } else if (ev
.keyCode
=== 221) {
3122 result
.key
= String
.fromCharCode(29);
3124 } else if (!this.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) {
3125 // On Mac this is a third level shift. Use <Esc> instead.
3126 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
3127 result
.key
= '\x1b' + String
.fromCharCode(ev
.keyCode
+ 32);
3128 } else if (ev
.keyCode
=== 192) {
3129 result
.key
= '\x1b`';
3130 } else if (ev
.keyCode
>= 48 && ev
.keyCode
<= 57) {
3131 result
.key
= '\x1b' + (ev
.keyCode
- 48);
3140 * Set the G level of the terminal
3143 Terminal
.prototype.setgLevel = function(g
) {
3145 this.charset
= this.charsets
[g
];
3149 * Set the charset for the given G level of the terminal
3153 Terminal
.prototype.setgCharset = function(g
, charset
) {
3154 this.charsets
[g
] = charset
;
3155 if (this.glevel
=== g
) {
3156 this.charset
= charset
;
3161 * Handle a keypress event.
3163 * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
3164 * @param {KeyboardEvent} ev The keypress event to be handled.
3166 Terminal
.prototype.keyPress = function(ev
) {
3173 } else if (ev
.which
== null) {
3175 } else if (ev
.which
!== 0 && ev
.charCode
!== 0) {
3182 (ev
.altKey
|| ev
.ctrlKey
|| ev
.metaKey
) && !isThirdLevelShift(this, ev
)
3187 key
= String
.fromCharCode(key
);
3189 this.emit('keypress', key
, ev
);
3190 this.emit('key', key
, ev
);
3198 * Send data for handling to the terminal
3199 * @param {string} data
3201 Terminal
.prototype.send = function(data
) {
3205 setTimeout(function() {
3206 self
.handler(self
.queue
);
3216 * Note: We could do sweet things with webaudio here
3218 Terminal
.prototype.bell = function() {
3219 if (!this.visualBell
) return;
3221 this.element
.style
.borderColor
= 'white';
3222 setTimeout(function() {
3223 self
.element
.style
.borderColor
= '';
3225 if (this.popOnBell
) this.focus();
3229 * Log the current state to the console.
3231 Terminal
.prototype.log = function() {
3232 if (!this.debug
) return;
3233 if (!this.context
.console
|| !this.context
.console
.log
) return;
3234 var args
= Array
.prototype.slice
.call(arguments
);
3235 this.context
.console
.log
.apply(this.context
.console
, args
);
3239 * Log the current state as error to the console.
3241 Terminal
.prototype.error = function() {
3242 if (!this.debug
) return;
3243 if (!this.context
.console
|| !this.context
.console
.error
) return;
3244 var args
= Array
.prototype.slice
.call(arguments
);
3245 this.context
.console
.error
.apply(this.context
.console
, args
);
3249 * Resizes the terminal.
3251 * @param {number} x The number of columns to resize to.
3252 * @param {number} y The number of rows to resize to.
3254 Terminal
.prototype.resize = function(x
, y
) {
3262 if (x
=== this.cols
&& y
=== this.rows
) {
3272 ch
= [this.defAttr
, ' ', 1]; // does xterm use the default attr?
3273 i
= this.lines
.length
;
3275 while (this.lines
[i
].length
< x
) {
3276 this.lines
[i
].push(ch
);
3280 i
= this.lines
.length
;
3282 while (this.lines
[i
].length
> x
) {
3283 this.lines
[i
].pop();
3296 // y is rows, not this.y
3297 if (this.lines
.length
< y
+ this.ybase
) {
3298 if (this.ybase
> 0 && this.lines
.length
<= this.ybase
+ this.y
+ addToY
+ 1) {
3299 // There is room above the buffer and there are no empty elements below the line,
3303 if (this.ydisp
> 0) {
3304 // Viewport is at the top of the buffer, must increase downwards
3308 // Add a blank line if there is no buffer left at the top to scroll to, or if there
3309 // are blank lines after the cursor
3310 this.lines
.push(this.blankLine());
3313 if (this.children
.length
< y
) {
3319 if (this.lines
.length
> y
+ this.ybase
) {
3320 if (this.lines
.length
> this.ybase
+ this.y
+ 1) {
3321 // The line is a blank line below the cursor, remove it
3324 // The line is the cursor, scroll down
3329 if (this.children
.length
> y
) {
3330 el
= this.children
.shift();
3332 el
.parentNode
.removeChild(el
);
3339 * Make sure that the cursor stays on screen
3353 this.scrollBottom
= y
- 1;
3355 this.refresh(0, this.rows
- 1);
3359 this.emit('resize', {terminal
: this, cols
: x
, rows
: y
});
3363 * Updates the range of rows to refresh
3364 * @param {number} y The number of rows to refresh next.
3366 Terminal
.prototype.updateRange = function(y
) {
3367 if (y
< this.refreshStart
) this.refreshStart
= y
;
3368 if (y
> this.refreshEnd
) this.refreshEnd
= y
;
3369 // if (y > this.refreshEnd) {
3370 // this.refreshEnd = y;
3371 // if (y > this.rows - 1) {
3372 // this.refreshEnd = this.rows - 1;
3378 * Set the range of refreshing to the maximyum value
3380 Terminal
.prototype.maxRange = function() {
3381 this.refreshStart
= 0;
3382 this.refreshEnd
= this.rows
- 1;
3388 * Setup the tab stops.
3391 Terminal
.prototype.setupStops = function(i
) {
3393 if (!this.tabs
[i
]) {
3394 i
= this.prevStop(i
);
3401 for (; i
< this.cols
; i
+= 8) {
3402 this.tabs
[i
] = true;
3408 * Move the cursor to the previous tab stop from the given position (default is current).
3409 * @param {number} x The position to move the cursor to the previous tab stop.
3411 Terminal
.prototype.prevStop = function(x
) {
3412 if (x
== null) x
= this.x
;
3413 while (!this.tabs
[--x
] && x
> 0);
3414 return x
>= this.cols
3421 * Move the cursor one tab stop forward from the given position (default is current).
3422 * @param {number} x The position to move the cursor one tab stop forward.
3424 Terminal
.prototype.nextStop = function(x
) {
3425 if (x
== null) x
= this.x
;
3426 while (!this.tabs
[++x
] && x
< this.cols
);
3427 return x
>= this.cols
3434 * Erase in the identified line everything from "x" to the end of the line (right).
3435 * @param {number} x The column from which to start erasing to the end of the line.
3436 * @param {number} y The line in which to operate.
3438 Terminal
.prototype.eraseRight = function(x
, y
) {
3439 var line
= this.lines
[this.ybase
+ y
]
3440 , ch
= [this.eraseAttr(), ' ', 1]; // xterm
3443 for (; x
< this.cols
; x
++) {
3447 this.updateRange(y
);
3453 * Erase in the identified line everything from "x" to the start of the line (left).
3454 * @param {number} x The column from which to start erasing to the start of the line.
3455 * @param {number} y The line in which to operate.
3457 Terminal
.prototype.eraseLeft = function(x
, y
) {
3458 var line
= this.lines
[this.ybase
+ y
]
3459 , ch
= [this.eraseAttr(), ' ', 1]; // xterm
3462 while (x
--) line
[x
] = ch
;
3464 this.updateRange(y
);
3469 * Erase all content in the given line
3470 * @param {number} y The line to erase all of its contents.
3472 Terminal
.prototype.eraseLine = function(y
) {
3473 this.eraseRight(0, y
);
3478 * Return the data array of a blank line/
3479 * @param {number} cur First bunch of data for each "blank" character.
3481 Terminal
.prototype.blankLine = function(cur
) {
3486 var ch
= [attr
, ' ', 1] // width defaults to 1 halfwidth character
3490 for (; i
< this.cols
; i
++) {
3499 * If cur return the back color xterm feature attribute. Else return defAttr.
3500 * @param {object} cur
3502 Terminal
.prototype.ch = function(cur
) {
3504 ? [this.eraseAttr(), ' ', 1]
3505 : [this.defAttr
, ' ', 1];
3510 * Evaluate if the current erminal is the given argument.
3511 * @param {object} term The terminal to evaluate
3513 Terminal
.prototype.is = function(term
) {
3514 var name
= this.termName
;
3515 return (name
+ '').indexOf(term
) === 0;
3520 * Emit the 'data' event and populate the given data.
3521 * @param {string} data The data to populate in the event.
3523 Terminal
.prototype.handler = function(data
) {
3524 this.emit('data', data
);
3529 * Emit the 'title' event and populate the given title.
3530 * @param {string} title The title to populate in the event.
3532 Terminal
.prototype.handleTitle = function(title
) {
3533 this.emit('title', title
);
3542 * ESC D Index (IND is 0x84).
3544 Terminal
.prototype.index = function() {
3546 if (this.y
> this.scrollBottom
) {
3550 this.state
= normal
;
3555 * ESC M Reverse Index (RI is 0x8d).
3557 Terminal
.prototype.reverseIndex = function() {
3560 if (this.y
< this.scrollTop
) {
3562 // possibly move the code below to term.reverseScroll();
3563 // test: echo -ne '\e[1;1H\e[44m\eM\e[0m'
3564 // blankLine(true) is xterm/linux behavior
3565 this.lines
.splice(this.y
+ this.ybase
, 0, this.blankLine(true));
3566 j
= this.rows
- 1 - this.scrollBottom
;
3567 this.lines
.splice(this.rows
- 1 + this.ybase
- j
+ 1, 1);
3569 this.updateRange(this.scrollTop
);
3570 this.updateRange(this.scrollBottom
);
3572 this.state
= normal
;
3577 * ESC c Full Reset (RIS).
3579 Terminal
.prototype.reset = function() {
3580 this.options
.rows
= this.rows
;
3581 this.options
.cols
= this.cols
;
3582 var customKeydownHandler
= this.customKeydownHandler
;
3583 Terminal
.call(this, this.options
);
3584 this.customKeydownHandler
= customKeydownHandler
;
3585 this.refresh(0, this.rows
- 1);
3590 * ESC H Tab Set (HTS is 0x88).
3592 Terminal
.prototype.tabSet = function() {
3593 this.tabs
[this.x
] = true;
3594 this.state
= normal
;
3604 * Cursor Up Ps Times (default = 1) (CUU).
3606 Terminal
.prototype.cursorUp = function(params
) {
3607 var param
= params
[0];
3608 if (param
< 1) param
= 1;
3610 if (this.y
< 0) this.y
= 0;
3616 * Cursor Down Ps Times (default = 1) (CUD).
3618 Terminal
.prototype.cursorDown = function(params
) {
3619 var param
= params
[0];
3620 if (param
< 1) param
= 1;
3622 if (this.y
>= this.rows
) {
3623 this.y
= this.rows
- 1;
3630 * Cursor Forward Ps Times (default = 1) (CUF).
3632 Terminal
.prototype.cursorForward = function(params
) {
3633 var param
= params
[0];
3634 if (param
< 1) param
= 1;
3636 if (this.x
>= this.cols
) {
3637 this.x
= this.cols
- 1;
3644 * Cursor Backward Ps Times (default = 1) (CUB).
3646 Terminal
.prototype.cursorBackward = function(params
) {
3647 var param
= params
[0];
3648 if (param
< 1) param
= 1;
3650 if (this.x
< 0) this.x
= 0;
3656 * Cursor Position [row;column] (default = [1,1]) (CUP).
3658 Terminal
.prototype.cursorPos = function(params
) {
3661 row
= params
[0] - 1;
3663 if (params
.length
>= 2) {
3664 col
= params
[1] - 1;
3671 } else if (row
>= this.rows
) {
3672 row
= this.rows
- 1;
3677 } else if (col
>= this.cols
) {
3678 col
= this.cols
- 1;
3687 * CSI Ps J Erase in Display (ED).
3688 * Ps = 0 -> Erase Below (default).
3689 * Ps = 1 -> Erase Above.
3690 * Ps = 2 -> Erase All.
3691 * Ps = 3 -> Erase Saved Lines (xterm).
3693 * Erase in Display (DECSED).
3694 * Ps = 0 -> Selective Erase Below (default).
3695 * Ps = 1 -> Selective Erase Above.
3696 * Ps = 2 -> Selective Erase All.
3698 Terminal
.prototype.eraseInDisplay = function(params
) {
3700 switch (params
[0]) {
3702 this.eraseRight(this.x
, this.y
);
3704 for (; j
< this.rows
; j
++) {
3709 this.eraseLeft(this.x
, this.y
);
3717 while (j
--) this.eraseLine(j
);
3727 * CSI Ps K Erase in Line (EL).
3728 * Ps = 0 -> Erase to Right (default).
3729 * Ps = 1 -> Erase to Left.
3730 * Ps = 2 -> Erase All.
3732 * Erase in Line (DECSEL).
3733 * Ps = 0 -> Selective Erase to Right (default).
3734 * Ps = 1 -> Selective Erase to Left.
3735 * Ps = 2 -> Selective Erase All.
3737 Terminal
.prototype.eraseInLine = function(params
) {
3738 switch (params
[0]) {
3740 this.eraseRight(this.x
, this.y
);
3743 this.eraseLeft(this.x
, this.y
);
3746 this.eraseLine(this.y
);
3753 * CSI Pm m Character Attributes (SGR).
3754 * Ps = 0 -> Normal (default).
3756 * Ps = 4 -> Underlined.
3757 * Ps = 5 -> Blink (appears as Bold).
3758 * Ps = 7 -> Inverse.
3759 * Ps = 8 -> Invisible, i.e., hidden (VT300).
3760 * Ps = 2 2 -> Normal (neither bold nor faint).
3761 * Ps = 2 4 -> Not underlined.
3762 * Ps = 2 5 -> Steady (not blinking).
3763 * Ps = 2 7 -> Positive (not inverse).
3764 * Ps = 2 8 -> Visible, i.e., not hidden (VT300).
3765 * Ps = 3 0 -> Set foreground color to Black.
3766 * Ps = 3 1 -> Set foreground color to Red.
3767 * Ps = 3 2 -> Set foreground color to Green.
3768 * Ps = 3 3 -> Set foreground color to Yellow.
3769 * Ps = 3 4 -> Set foreground color to Blue.
3770 * Ps = 3 5 -> Set foreground color to Magenta.
3771 * Ps = 3 6 -> Set foreground color to Cyan.
3772 * Ps = 3 7 -> Set foreground color to White.
3773 * Ps = 3 9 -> Set foreground color to default (original).
3774 * Ps = 4 0 -> Set background color to Black.
3775 * Ps = 4 1 -> Set background color to Red.
3776 * Ps = 4 2 -> Set background color to Green.
3777 * Ps = 4 3 -> Set background color to Yellow.
3778 * Ps = 4 4 -> Set background color to Blue.
3779 * Ps = 4 5 -> Set background color to Magenta.
3780 * Ps = 4 6 -> Set background color to Cyan.
3781 * Ps = 4 7 -> Set background color to White.
3782 * Ps = 4 9 -> Set background color to default (original).
3784 * If 16-color support is compiled, the following apply. Assume
3785 * that xterm's resources are set so that the ISO color codes are
3786 * the first 8 of a set of 16. Then the aixterm colors are the
3787 * bright versions of the ISO colors:
3788 * Ps = 9 0 -> Set foreground color to Black.
3789 * Ps = 9 1 -> Set foreground color to Red.
3790 * Ps = 9 2 -> Set foreground color to Green.
3791 * Ps = 9 3 -> Set foreground color to Yellow.
3792 * Ps = 9 4 -> Set foreground color to Blue.
3793 * Ps = 9 5 -> Set foreground color to Magenta.
3794 * Ps = 9 6 -> Set foreground color to Cyan.
3795 * Ps = 9 7 -> Set foreground color to White.
3796 * Ps = 1 0 0 -> Set background color to Black.
3797 * Ps = 1 0 1 -> Set background color to Red.
3798 * Ps = 1 0 2 -> Set background color to Green.
3799 * Ps = 1 0 3 -> Set background color to Yellow.
3800 * Ps = 1 0 4 -> Set background color to Blue.
3801 * Ps = 1 0 5 -> Set background color to Magenta.
3802 * Ps = 1 0 6 -> Set background color to Cyan.
3803 * Ps = 1 0 7 -> Set background color to White.
3805 * If xterm is compiled with the 16-color support disabled, it
3806 * supports the following, from rxvt:
3807 * Ps = 1 0 0 -> Set foreground and background color to
3810 * If 88- or 256-color support is compiled, the following apply.
3811 * Ps = 3 8 ; 5 ; Ps -> Set foreground color to the second
3813 * Ps = 4 8 ; 5 ; Ps -> Set background color to the second
3816 Terminal
.prototype.charAttributes = function(params
) {
3817 // Optimize a single SGR0.
3818 if (params
.length
=== 1 && params
[0] === 0) {
3819 this.curAttr
= this.defAttr
;
3823 var l
= params
.length
3825 , flags
= this.curAttr
>> 18
3826 , fg
= (this.curAttr
>> 9) & 0x1ff
3827 , bg
= this.curAttr
& 0x1ff
3830 for (; i
< l
; i
++) {
3832 if (p
>= 30 && p
<= 37) {
3835 } else if (p
>= 40 && p
<= 47) {
3838 } else if (p
>= 90 && p
<= 97) {
3842 } else if (p
>= 100 && p
<= 107) {
3846 } else if (p
=== 0) {
3848 flags
= this.defAttr
>> 18;
3849 fg
= (this.defAttr
>> 9) & 0x1ff;
3850 bg
= this.defAttr
& 0x1ff;
3854 } else if (p
=== 1) {
3857 } else if (p
=== 4) {
3860 } else if (p
=== 5) {
3863 } else if (p
=== 7) {
3864 // inverse and positive
3865 // test with: echo -e '\e[31m\e[42mhello\e[7mworld\e[27mhi\e[m'
3867 } else if (p
=== 8) {
3870 } else if (p
=== 22) {
3873 } else if (p
=== 24) {
3876 } else if (p
=== 25) {
3879 } else if (p
=== 27) {
3882 } else if (p
=== 28) {
3885 } else if (p
=== 39) {
3887 fg
= (this.defAttr
>> 9) & 0x1ff;
3888 } else if (p
=== 49) {
3890 bg
= this.defAttr
& 0x1ff;
3891 } else if (p
=== 38) {
3893 if (params
[i
+ 1] === 2) {
3897 params
[i
+ 1] & 0xff,
3898 params
[i
+ 2] & 0xff);
3899 if (fg
=== -1) fg
= 0x1ff;
3901 } else if (params
[i
+ 1] === 5) {
3903 p
= params
[i
] & 0xff;
3906 } else if (p
=== 48) {
3908 if (params
[i
+ 1] === 2) {
3912 params
[i
+ 1] & 0xff,
3913 params
[i
+ 2] & 0xff);
3914 if (bg
=== -1) bg
= 0x1ff;
3916 } else if (params
[i
+ 1] === 5) {
3918 p
= params
[i
] & 0xff;
3921 } else if (p
=== 100) {
3923 fg
= (this.defAttr
>> 9) & 0x1ff;
3924 bg
= this.defAttr
& 0x1ff;
3926 this.error('Unknown SGR attribute: %d.', p
);
3930 this.curAttr
= (flags
<< 18) | (fg
<< 9) | bg
;
3935 * CSI Ps n Device Status Report (DSR).
3936 * Ps = 5 -> Status Report. Result (``OK'') is
3938 * Ps = 6 -> Report Cursor Position (CPR) [row;column].
3942 * Device Status Report (DSR, DEC-specific).
3943 * Ps = 6 -> Report Cursor Position (CPR) [row;column] as CSI
3944 * ? r ; c R (assumes page is zero).
3945 * Ps = 1 5 -> Report Printer status as CSI ? 1 0 n (ready).
3946 * or CSI ? 1 1 n (not ready).
3947 * Ps = 2 5 -> Report UDK status as CSI ? 2 0 n (unlocked)
3948 * or CSI ? 2 1 n (locked).
3949 * Ps = 2 6 -> Report Keyboard status as
3950 * CSI ? 2 7 ; 1 ; 0 ; 0 n (North American).
3951 * The last two parameters apply to VT400 & up, and denote key-
3952 * board ready and LK01 respectively.
3953 * Ps = 5 3 -> Report Locator status as
3954 * CSI ? 5 3 n Locator available, if compiled-in, or
3955 * CSI ? 5 0 n No Locator, if not.
3957 Terminal
.prototype.deviceStatus = function(params
) {
3959 switch (params
[0]) {
3962 this.send('\x1b[0n');
3973 } else if (this.prefix
=== '?') {
3974 // modern xterm doesnt seem to
3975 // respond to any of these except ?6, 6, and 5
3976 switch (params
[0]) {
3987 // this.send('\x1b[?11n');
3990 // dont support user defined keys
3991 // this.send('\x1b[?21n');
3994 // north american keyboard
3995 // this.send('\x1b[?27;1;0;0n');
3998 // no dec locator/mouse
3999 // this.send('\x1b[?50n');
4012 * Insert Ps (Blank) Character(s) (default = 1) (ICH).
4014 Terminal
.prototype.insertChars = function(params
) {
4015 var param
, row
, j
, ch
;
4018 if (param
< 1) param
= 1;
4020 row
= this.y
+ this.ybase
;
4022 ch
= [this.eraseAttr(), ' ', 1]; // xterm
4024 while (param
-- && j
< this.cols
) {
4025 this.lines
[row
].splice(j
++, 0, ch
);
4026 this.lines
[row
].pop();
4032 * Cursor Next Line Ps Times (default = 1) (CNL).
4033 * same as CSI Ps B ?
4035 Terminal
.prototype.cursorNextLine = function(params
) {
4036 var param
= params
[0];
4037 if (param
< 1) param
= 1;
4039 if (this.y
>= this.rows
) {
4040 this.y
= this.rows
- 1;
4048 * Cursor Preceding Line Ps Times (default = 1) (CNL).
4051 Terminal
.prototype.cursorPrecedingLine = function(params
) {
4052 var param
= params
[0];
4053 if (param
< 1) param
= 1;
4055 if (this.y
< 0) this.y
= 0;
4062 * Cursor Character Absolute [column] (default = [row,1]) (CHA).
4064 Terminal
.prototype.cursorCharAbsolute = function(params
) {
4065 var param
= params
[0];
4066 if (param
< 1) param
= 1;
4073 * Insert Ps Line(s) (default = 1) (IL).
4075 Terminal
.prototype.insertLines = function(params
) {
4079 if (param
< 1) param
= 1;
4080 row
= this.y
+ this.ybase
;
4082 j
= this.rows
- 1 - this.scrollBottom
;
4083 j
= this.rows
- 1 + this.ybase
- j
+ 1;
4086 // test: echo -e '\e[44m\e[1L\e[0m'
4087 // blankLine(true) - xterm/linux behavior
4088 this.lines
.splice(row
, 0, this.blankLine(true));
4089 this.lines
.splice(j
, 1);
4093 this.updateRange(this.y
);
4094 this.updateRange(this.scrollBottom
);
4100 * Delete Ps Line(s) (default = 1) (DL).
4102 Terminal
.prototype.deleteLines = function(params
) {
4106 if (param
< 1) param
= 1;
4107 row
= this.y
+ this.ybase
;
4109 j
= this.rows
- 1 - this.scrollBottom
;
4110 j
= this.rows
- 1 + this.ybase
- j
;
4113 // test: echo -e '\e[44m\e[1M\e[0m'
4114 // blankLine(true) - xterm/linux behavior
4115 this.lines
.splice(j
+ 1, 0, this.blankLine(true));
4116 this.lines
.splice(row
, 1);
4120 this.updateRange(this.y
);
4121 this.updateRange(this.scrollBottom
);
4127 * Delete Ps Character(s) (default = 1) (DCH).
4129 Terminal
.prototype.deleteChars = function(params
) {
4133 if (param
< 1) param
= 1;
4135 row
= this.y
+ this.ybase
;
4136 ch
= [this.eraseAttr(), ' ', 1]; // xterm
4139 this.lines
[row
].splice(this.x
, 1);
4140 this.lines
[row
].push(ch
);
4146 * Erase Ps Character(s) (default = 1) (ECH).
4148 Terminal
.prototype.eraseChars = function(params
) {
4149 var param
, row
, j
, ch
;
4152 if (param
< 1) param
= 1;
4154 row
= this.y
+ this.ybase
;
4156 ch
= [this.eraseAttr(), ' ', 1]; // xterm
4158 while (param
-- && j
< this.cols
) {
4159 this.lines
[row
][j
++] = ch
;
4164 * CSI Pm ` Character Position Absolute
4165 * [column] (default = [row,1]) (HPA).
4167 Terminal
.prototype.charPosAbsolute = function(params
) {
4168 var param
= params
[0];
4169 if (param
< 1) param
= 1;
4171 if (this.x
>= this.cols
) {
4172 this.x
= this.cols
- 1;
4179 * Horizontal Position Relative
4182 Terminal
.prototype.HPositionRelative = function(params
) {
4183 var param
= params
[0];
4184 if (param
< 1) param
= 1;
4186 if (this.x
>= this.cols
) {
4187 this.x
= this.cols
- 1;
4193 * CSI Ps c Send Device Attributes (Primary DA).
4194 * Ps = 0 or omitted -> request attributes from terminal. The
4195 * response depends on the decTerminalID resource setting.
4196 * -> CSI ? 1 ; 2 c (``VT100 with Advanced Video Option'')
4197 * -> CSI ? 1 ; 0 c (``VT101 with No Options'')
4198 * -> CSI ? 6 c (``VT102'')
4199 * -> CSI ? 6 0 ; 1 ; 2 ; 6 ; 8 ; 9 ; 1 5 ; c (``VT220'')
4200 * The VT100-style response parameters do not mean anything by
4201 * themselves. VT220 parameters do, telling the host what fea-
4202 * tures the terminal supports:
4203 * Ps = 1 -> 132-columns.
4204 * Ps = 2 -> Printer.
4205 * Ps = 6 -> Selective erase.
4206 * Ps = 8 -> User-defined keys.
4207 * Ps = 9 -> National replacement character sets.
4208 * Ps = 1 5 -> Technical characters.
4209 * Ps = 2 2 -> ANSI color, e.g., VT525.
4210 * Ps = 2 9 -> ANSI text locator (i.e., DEC Locator mode).
4212 * Send Device Attributes (Secondary DA).
4213 * Ps = 0 or omitted -> request the terminal's identification
4214 * code. The response depends on the decTerminalID resource set-
4215 * ting. It should apply only to VT220 and up, but xterm extends
4217 * -> CSI > Pp ; Pv ; Pc c
4218 * where Pp denotes the terminal type
4219 * Pp = 0 -> ``VT100''.
4220 * Pp = 1 -> ``VT220''.
4221 * and Pv is the firmware version (for xterm, this was originally
4222 * the XFree86 patch number, starting with 95). In a DEC termi-
4223 * nal, Pc indicates the ROM cartridge registration number and is
4226 * xterm/charproc.c - line 2012, for more information.
4227 * vim responds with ^[[?0c or ^[[?1c after the terminal's response (?)
4229 Terminal
.prototype.sendDeviceAttributes = function(params
) {
4230 if (params
[0] > 0) return;
4233 if (this.is('xterm')
4234 || this.is('rxvt-unicode')
4235 || this.is('screen')) {
4236 this.send('\x1b[?1;2c');
4237 } else if (this.is('linux')) {
4238 this.send('\x1b[?6c');
4240 } else if (this.prefix
=== '>') {
4242 // seem to spit this
4243 // out around ~370 times (?).
4244 if (this.is('xterm')) {
4245 this.send('\x1b[>0;276;0c');
4246 } else if (this.is('rxvt-unicode')) {
4247 this.send('\x1b[>85;95;0c');
4248 } else if (this.is('linux')) {
4249 // not supported by linux console.
4250 // linux console echoes parameters.
4251 this.send(params
[0] + 'c');
4252 } else if (this.is('screen')) {
4253 this.send('\x1b[>83;40003;0c');
4261 * Line Position Absolute [row] (default = [1,column]) (VPA).
4263 Terminal
.prototype.linePosAbsolute = function(params
) {
4264 var param
= params
[0];
4265 if (param
< 1) param
= 1;
4267 if (this.y
>= this.rows
) {
4268 this.y
= this.rows
- 1;
4274 * 145 65 e * VPR - Vertical Position Relative
4277 Terminal
.prototype.VPositionRelative = function(params
) {
4278 var param
= params
[0];
4279 if (param
< 1) param
= 1;
4281 if (this.y
>= this.rows
) {
4282 this.y
= this.rows
- 1;
4289 * Horizontal and Vertical Position [row;column] (default =
4292 Terminal
.prototype.HVPosition = function(params
) {
4293 if (params
[0] < 1) params
[0] = 1;
4294 if (params
[1] < 1) params
[1] = 1;
4296 this.y
= params
[0] - 1;
4297 if (this.y
>= this.rows
) {
4298 this.y
= this.rows
- 1;
4301 this.x
= params
[1] - 1;
4302 if (this.x
>= this.cols
) {
4303 this.x
= this.cols
- 1;
4309 * CSI Pm h Set Mode (SM).
4310 * Ps = 2 -> Keyboard Action Mode (AM).
4311 * Ps = 4 -> Insert Mode (IRM).
4312 * Ps = 1 2 -> Send/receive (SRM).
4313 * Ps = 2 0 -> Automatic Newline (LNM).
4315 * DEC Private Mode Set (DECSET).
4316 * Ps = 1 -> Application Cursor Keys (DECCKM).
4317 * Ps = 2 -> Designate USASCII for character sets G0-G3
4318 * (DECANM), and set VT100 mode.
4319 * Ps = 3 -> 132 Column Mode (DECCOLM).
4320 * Ps = 4 -> Smooth (Slow) Scroll (DECSCLM).
4321 * Ps = 5 -> Reverse Video (DECSCNM).
4322 * Ps = 6 -> Origin Mode (DECOM).
4323 * Ps = 7 -> Wraparound Mode (DECAWM).
4324 * Ps = 8 -> Auto-repeat Keys (DECARM).
4325 * Ps = 9 -> Send Mouse X & Y on button press. See the sec-
4326 * tion Mouse Tracking.
4327 * Ps = 1 0 -> Show toolbar (rxvt).
4328 * Ps = 1 2 -> Start Blinking Cursor (att610).
4329 * Ps = 1 8 -> Print form feed (DECPFF).
4330 * Ps = 1 9 -> Set print extent to full screen (DECPEX).
4331 * Ps = 2 5 -> Show Cursor (DECTCEM).
4332 * Ps = 3 0 -> Show scrollbar (rxvt).
4333 * Ps = 3 5 -> Enable font-shifting functions (rxvt).
4334 * Ps = 3 8 -> Enter Tektronix Mode (DECTEK).
4335 * Ps = 4 0 -> Allow 80 -> 132 Mode.
4336 * Ps = 4 1 -> more(1) fix (see curses resource).
4337 * Ps = 4 2 -> Enable Nation Replacement Character sets (DECN-
4339 * Ps = 4 4 -> Turn On Margin Bell.
4340 * Ps = 4 5 -> Reverse-wraparound Mode.
4341 * Ps = 4 6 -> Start Logging. This is normally disabled by a
4342 * compile-time option.
4343 * Ps = 4 7 -> Use Alternate Screen Buffer. (This may be dis-
4344 * abled by the titeInhibit resource).
4345 * Ps = 6 6 -> Application keypad (DECNKM).
4346 * Ps = 6 7 -> Backarrow key sends backspace (DECBKM).
4347 * Ps = 1 0 0 0 -> Send Mouse X & Y on button press and
4348 * release. See the section Mouse Tracking.
4349 * Ps = 1 0 0 1 -> Use Hilite Mouse Tracking.
4350 * Ps = 1 0 0 2 -> Use Cell Motion Mouse Tracking.
4351 * Ps = 1 0 0 3 -> Use All Motion Mouse Tracking.
4352 * Ps = 1 0 0 4 -> Send FocusIn/FocusOut events.
4353 * Ps = 1 0 0 5 -> Enable Extended Mouse Mode.
4354 * Ps = 1 0 1 0 -> Scroll to bottom on tty output (rxvt).
4355 * Ps = 1 0 1 1 -> Scroll to bottom on key press (rxvt).
4356 * Ps = 1 0 3 4 -> Interpret "meta" key, sets eighth bit.
4357 * (enables the eightBitInput resource).
4358 * Ps = 1 0 3 5 -> Enable special modifiers for Alt and Num-
4359 * Lock keys. (This enables the numLock resource).
4360 * Ps = 1 0 3 6 -> Send ESC when Meta modifies a key. (This
4361 * enables the metaSendsEscape resource).
4362 * Ps = 1 0 3 7 -> Send DEL from the editing-keypad Delete
4364 * Ps = 1 0 3 9 -> Send ESC when Alt modifies a key. (This
4365 * enables the altSendsEscape resource).
4366 * Ps = 1 0 4 0 -> Keep selection even if not highlighted.
4367 * (This enables the keepSelection resource).
4368 * Ps = 1 0 4 1 -> Use the CLIPBOARD selection. (This enables
4369 * the selectToClipboard resource).
4370 * Ps = 1 0 4 2 -> Enable Urgency window manager hint when
4371 * Control-G is received. (This enables the bellIsUrgent
4373 * Ps = 1 0 4 3 -> Enable raising of the window when Control-G
4374 * is received. (enables the popOnBell resource).
4375 * Ps = 1 0 4 7 -> Use Alternate Screen Buffer. (This may be
4376 * disabled by the titeInhibit resource).
4377 * Ps = 1 0 4 8 -> Save cursor as in DECSC. (This may be dis-
4378 * abled by the titeInhibit resource).
4379 * Ps = 1 0 4 9 -> Save cursor as in DECSC and use Alternate
4380 * Screen Buffer, clearing it first. (This may be disabled by
4381 * the titeInhibit resource). This combines the effects of the 1
4382 * 0 4 7 and 1 0 4 8 modes. Use this with terminfo-based
4383 * applications rather than the 4 7 mode.
4384 * Ps = 1 0 5 0 -> Set terminfo/termcap function-key mode.
4385 * Ps = 1 0 5 1 -> Set Sun function-key mode.
4386 * Ps = 1 0 5 2 -> Set HP function-key mode.
4387 * Ps = 1 0 5 3 -> Set SCO function-key mode.
4388 * Ps = 1 0 6 0 -> Set legacy keyboard emulation (X11R6).
4389 * Ps = 1 0 6 1 -> Set VT220 keyboard emulation.
4390 * Ps = 2 0 0 4 -> Set bracketed paste mode.
4392 * http: *vt100.net/docs/vt220-rm/chapter4.html
4394 Terminal
.prototype.setMode = function(params
) {
4395 if (typeof params
=== 'object') {
4396 var l
= params
.length
4399 for (; i
< l
; i
++) {
4400 this.setMode(params
[i
]);
4409 this.insertMode
= true;
4412 //this.convertEol = true;
4415 } else if (this.prefix
=== '?') {
4418 this.applicationCursor
= true;
4421 this.setgCharset(0, Terminal
.charsets
.US
);
4422 this.setgCharset(1, Terminal
.charsets
.US
);
4423 this.setgCharset(2, Terminal
.charsets
.US
);
4424 this.setgCharset(3, Terminal
.charsets
.US
);
4425 // set VT100 mode here
4427 case 3: // 132 col mode
4428 this.savedCols
= this.cols
;
4429 this.resize(132, this.rows
);
4432 this.originMode
= true;
4435 this.wraparoundMode
= true;
4438 // this.cursorBlink = true;
4441 this.log('Serial port requested application keypad.');
4442 this.applicationKeypad
= true;
4443 this.viewport
.setApplicationMode(true);
4445 case 9: // X10 Mouse
4446 // no release, no motion, no wheel, no modifiers.
4447 case 1000: // vt200 mouse
4449 // no modifiers, except control on the wheel.
4450 case 1002: // button event mouse
4451 case 1003: // any event mouse
4452 // any event - sends motion events,
4453 // even if there is no button held down.
4454 this.x10Mouse
= params
=== 9;
4455 this.vt200Mouse
= params
=== 1000;
4456 this.normalMouse
= params
> 1000;
4457 this.mouseEvents
= true;
4458 this.element
.style
.cursor
= 'default';
4459 this.log('Binding to mouse events.');
4461 case 1004: // send focusin/focusout events
4464 this.sendFocus
= true;
4466 case 1005: // utf8 ext mode mouse
4467 this.utfMouse
= true;
4468 // for wide terminals
4469 // simply encodes large values as utf8 characters
4471 case 1006: // sgr ext mode mouse
4472 this.sgrMouse
= true;
4473 // for wide terminals
4474 // does not add 32 to fields
4475 // press: ^[[<b;x;yM
4476 // release: ^[[<b;x;ym
4478 case 1015: // urxvt ext mode mouse
4479 this.urxvtMouse
= true;
4480 // for wide terminals
4481 // numbers for fields
4483 // motion: ^[[b;x;yT
4485 case 25: // show cursor
4486 this.cursorHidden
= false;
4488 case 1049: // alt screen buffer cursor
4489 //this.saveCursor();
4491 case 47: // alt screen buffer
4492 case 1047: // alt screen buffer
4500 scrollTop
: this.scrollTop
,
4501 scrollBottom
: this.scrollBottom
,
4503 // XXX save charset(s) here?
4504 // charset: this.charset,
4505 // glevel: this.glevel,
4506 // charsets: this.charsets
4509 this.normal
= normal
;
4518 * CSI Pm l Reset Mode (RM).
4519 * Ps = 2 -> Keyboard Action Mode (AM).
4520 * Ps = 4 -> Replace Mode (IRM).
4521 * Ps = 1 2 -> Send/receive (SRM).
4522 * Ps = 2 0 -> Normal Linefeed (LNM).
4524 * DEC Private Mode Reset (DECRST).
4525 * Ps = 1 -> Normal Cursor Keys (DECCKM).
4526 * Ps = 2 -> Designate VT52 mode (DECANM).
4527 * Ps = 3 -> 80 Column Mode (DECCOLM).
4528 * Ps = 4 -> Jump (Fast) Scroll (DECSCLM).
4529 * Ps = 5 -> Normal Video (DECSCNM).
4530 * Ps = 6 -> Normal Cursor Mode (DECOM).
4531 * Ps = 7 -> No Wraparound Mode (DECAWM).
4532 * Ps = 8 -> No Auto-repeat Keys (DECARM).
4533 * Ps = 9 -> Don't send Mouse X & Y on button press.
4534 * Ps = 1 0 -> Hide toolbar (rxvt).
4535 * Ps = 1 2 -> Stop Blinking Cursor (att610).
4536 * Ps = 1 8 -> Don't print form feed (DECPFF).
4537 * Ps = 1 9 -> Limit print to scrolling region (DECPEX).
4538 * Ps = 2 5 -> Hide Cursor (DECTCEM).
4539 * Ps = 3 0 -> Don't show scrollbar (rxvt).
4540 * Ps = 3 5 -> Disable font-shifting functions (rxvt).
4541 * Ps = 4 0 -> Disallow 80 -> 132 Mode.
4542 * Ps = 4 1 -> No more(1) fix (see curses resource).
4543 * Ps = 4 2 -> Disable Nation Replacement Character sets (DEC-
4545 * Ps = 4 4 -> Turn Off Margin Bell.
4546 * Ps = 4 5 -> No Reverse-wraparound Mode.
4547 * Ps = 4 6 -> Stop Logging. (This is normally disabled by a
4548 * compile-time option).
4549 * Ps = 4 7 -> Use Normal Screen Buffer.
4550 * Ps = 6 6 -> Numeric keypad (DECNKM).
4551 * Ps = 6 7 -> Backarrow key sends delete (DECBKM).
4552 * Ps = 1 0 0 0 -> Don't send Mouse X & Y on button press and
4553 * release. See the section Mouse Tracking.
4554 * Ps = 1 0 0 1 -> Don't use Hilite Mouse Tracking.
4555 * Ps = 1 0 0 2 -> Don't use Cell Motion Mouse Tracking.
4556 * Ps = 1 0 0 3 -> Don't use All Motion Mouse Tracking.
4557 * Ps = 1 0 0 4 -> Don't send FocusIn/FocusOut events.
4558 * Ps = 1 0 0 5 -> Disable Extended Mouse Mode.
4559 * Ps = 1 0 1 0 -> Don't scroll to bottom on tty output
4561 * Ps = 1 0 1 1 -> Don't scroll to bottom on key press (rxvt).
4562 * Ps = 1 0 3 4 -> Don't interpret "meta" key. (This disables
4563 * the eightBitInput resource).
4564 * Ps = 1 0 3 5 -> Disable special modifiers for Alt and Num-
4565 * Lock keys. (This disables the numLock resource).
4566 * Ps = 1 0 3 6 -> Don't send ESC when Meta modifies a key.
4567 * (This disables the metaSendsEscape resource).
4568 * Ps = 1 0 3 7 -> Send VT220 Remove from the editing-keypad
4570 * Ps = 1 0 3 9 -> Don't send ESC when Alt modifies a key.
4571 * (This disables the altSendsEscape resource).
4572 * Ps = 1 0 4 0 -> Do not keep selection when not highlighted.
4573 * (This disables the keepSelection resource).
4574 * Ps = 1 0 4 1 -> Use the PRIMARY selection. (This disables
4575 * the selectToClipboard resource).
4576 * Ps = 1 0 4 2 -> Disable Urgency window manager hint when
4577 * Control-G is received. (This disables the bellIsUrgent
4579 * Ps = 1 0 4 3 -> Disable raising of the window when Control-
4580 * G is received. (This disables the popOnBell resource).
4581 * Ps = 1 0 4 7 -> Use Normal Screen Buffer, clearing screen
4582 * first if in the Alternate Screen. (This may be disabled by
4583 * the titeInhibit resource).
4584 * Ps = 1 0 4 8 -> Restore cursor as in DECRC. (This may be
4585 * disabled by the titeInhibit resource).
4586 * Ps = 1 0 4 9 -> Use Normal Screen Buffer and restore cursor
4587 * as in DECRC. (This may be disabled by the titeInhibit
4588 * resource). This combines the effects of the 1 0 4 7 and 1 0
4589 * 4 8 modes. Use this with terminfo-based applications rather
4590 * than the 4 7 mode.
4591 * Ps = 1 0 5 0 -> Reset terminfo/termcap function-key mode.
4592 * Ps = 1 0 5 1 -> Reset Sun function-key mode.
4593 * Ps = 1 0 5 2 -> Reset HP function-key mode.
4594 * Ps = 1 0 5 3 -> Reset SCO function-key mode.
4595 * Ps = 1 0 6 0 -> Reset legacy keyboard emulation (X11R6).
4596 * Ps = 1 0 6 1 -> Reset keyboard emulation to Sun/PC style.
4597 * Ps = 2 0 0 4 -> Reset bracketed paste mode.
4599 Terminal
.prototype.resetMode = function(params
) {
4600 if (typeof params
=== 'object') {
4601 var l
= params
.length
4604 for (; i
< l
; i
++) {
4605 this.resetMode(params
[i
]);
4614 this.insertMode
= false;
4617 //this.convertEol = false;
4620 } else if (this.prefix
=== '?') {
4623 this.applicationCursor
= false;
4626 if (this.cols
=== 132 && this.savedCols
) {
4627 this.resize(this.savedCols
, this.rows
);
4629 delete this.savedCols
;
4632 this.originMode
= false;
4635 this.wraparoundMode
= false;
4638 // this.cursorBlink = false;
4641 this.log('Switching back to normal keypad.');
4642 this.viewport
.setApplicationMode(false);
4643 this.applicationKeypad
= false;
4645 case 9: // X10 Mouse
4646 case 1000: // vt200 mouse
4647 case 1002: // button event mouse
4648 case 1003: // any event mouse
4649 this.x10Mouse
= false;
4650 this.vt200Mouse
= false;
4651 this.normalMouse
= false;
4652 this.mouseEvents
= false;
4653 this.element
.style
.cursor
= '';
4655 case 1004: // send focusin/focusout events
4656 this.sendFocus
= false;
4658 case 1005: // utf8 ext mode mouse
4659 this.utfMouse
= false;
4661 case 1006: // sgr ext mode mouse
4662 this.sgrMouse
= false;
4664 case 1015: // urxvt ext mode mouse
4665 this.urxvtMouse
= false;
4667 case 25: // hide cursor
4668 this.cursorHidden
= true;
4670 case 1049: // alt screen buffer cursor
4672 case 47: // normal screen buffer
4673 case 1047: // normal screen buffer - clearing it first
4675 this.lines
= this.normal
.lines
;
4676 this.ybase
= this.normal
.ybase
;
4677 this.ydisp
= this.normal
.ydisp
;
4678 this.x
= this.normal
.x
;
4679 this.y
= this.normal
.y
;
4680 this.scrollTop
= this.normal
.scrollTop
;
4681 this.scrollBottom
= this.normal
.scrollBottom
;
4682 this.tabs
= this.normal
.tabs
;
4684 // if (params === 1049) {
4685 // this.x = this.savedX;
4686 // this.y = this.savedY;
4688 this.refresh(0, this.rows
- 1);
4699 * Set Scrolling Region [top;bottom] (default = full size of win-
4703 Terminal
.prototype.setScrollRegion = function(params
) {
4704 if (this.prefix
) return;
4705 this.scrollTop
= (params
[0] || 1) - 1;
4706 this.scrollBottom
= (params
[1] || this.rows
) - 1;
4714 * Save cursor (ANSI.SYS).
4716 Terminal
.prototype.saveCursor = function(params
) {
4717 this.savedX
= this.x
;
4718 this.savedY
= this.y
;
4724 * Restore cursor (ANSI.SYS).
4726 Terminal
.prototype.restoreCursor = function(params
) {
4727 this.x
= this.savedX
|| 0;
4728 this.y
= this.savedY
|| 0;
4738 * Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
4740 Terminal
.prototype.cursorForwardTab = function(params
) {
4741 var param
= params
[0] || 1;
4743 this.x
= this.nextStop();
4749 * CSI Ps S Scroll up Ps lines (default = 1) (SU).
4751 Terminal
.prototype.scrollUp = function(params
) {
4752 var param
= params
[0] || 1;
4754 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
4755 this.lines
.splice(this.ybase
+ this.scrollBottom
, 0, this.blankLine());
4758 this.updateRange(this.scrollTop
);
4759 this.updateRange(this.scrollBottom
);
4764 * CSI Ps T Scroll down Ps lines (default = 1) (SD).
4766 Terminal
.prototype.scrollDown = function(params
) {
4767 var param
= params
[0] || 1;
4769 this.lines
.splice(this.ybase
+ this.scrollBottom
, 1);
4770 this.lines
.splice(this.ybase
+ this.scrollTop
, 0, this.blankLine());
4773 this.updateRange(this.scrollTop
);
4774 this.updateRange(this.scrollBottom
);
4779 * CSI Ps ; Ps ; Ps ; Ps ; Ps T
4780 * Initiate highlight mouse tracking. Parameters are
4781 * [func;startx;starty;firstrow;lastrow]. See the section Mouse
4784 Terminal
.prototype.initMouseTracking = function(params
) {
4785 // Relevant: DECSET 1001
4791 * Reset one or more features of the title modes to the default
4792 * value. Normally, "reset" disables the feature. It is possi-
4793 * ble to disable the ability to reset features by compiling a
4794 * different default for the title modes into xterm.
4795 * Ps = 0 -> Do not set window/icon labels using hexadecimal.
4796 * Ps = 1 -> Do not query window/icon labels using hexadeci-
4798 * Ps = 2 -> Do not set window/icon labels using UTF-8.
4799 * Ps = 3 -> Do not query window/icon labels using UTF-8.
4800 * (See discussion of "Title Modes").
4802 Terminal
.prototype.resetTitleModes = function(params
) {
4808 * CSI Ps Z Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
4810 Terminal
.prototype.cursorBackwardTab = function(params
) {
4811 var param
= params
[0] || 1;
4813 this.x
= this.prevStop();
4819 * CSI Ps b Repeat the preceding graphic character Ps times (REP).
4821 Terminal
.prototype.repeatPrecedingCharacter = function(params
) {
4822 var param
= params
[0] || 1
4823 , line
= this.lines
[this.ybase
+ this.y
]
4824 , ch
= line
[this.x
- 1] || [this.defAttr
, ' ', 1];
4826 while (param
--) line
[this.x
++] = ch
;
4831 * CSI Ps g Tab Clear (TBC).
4832 * Ps = 0 -> Clear Current Column (default).
4833 * Ps = 3 -> Clear All.
4835 * Ps = 2 -> Clear Stops on Line.
4836 * http://vt100.net/annarbor/aaa-ug/section6.html
4838 Terminal
.prototype.tabClear = function(params
) {
4839 var param
= params
[0];
4841 delete this.tabs
[this.x
];
4842 } else if (param
=== 3) {
4849 * CSI Pm i Media Copy (MC).
4850 * Ps = 0 -> Print screen (default).
4851 * Ps = 4 -> Turn off printer controller mode.
4852 * Ps = 5 -> Turn on printer controller mode.
4854 * Media Copy (MC, DEC-specific).
4855 * Ps = 1 -> Print line containing cursor.
4856 * Ps = 4 -> Turn off autoprint mode.
4857 * Ps = 5 -> Turn on autoprint mode.
4858 * Ps = 1 0 -> Print composed display, ignores DECPEX.
4859 * Ps = 1 1 -> Print all pages.
4861 Terminal
.prototype.mediaCopy = function(params
) {
4868 * Set or reset resource-values used by xterm to decide whether
4869 * to construct escape sequences holding information about the
4870 * modifiers pressed with a given key. The first parameter iden-
4871 * tifies the resource to set/reset. The second parameter is the
4872 * value to assign to the resource. If the second parameter is
4873 * omitted, the resource is reset to its initial value.
4874 * Ps = 1 -> modifyCursorKeys.
4875 * Ps = 2 -> modifyFunctionKeys.
4876 * Ps = 4 -> modifyOtherKeys.
4877 * If no parameters are given, all resources are reset to their
4880 Terminal
.prototype.setResources = function(params
) {
4887 * Disable modifiers which may be enabled via the CSI > Ps; Ps m
4888 * sequence. This corresponds to a resource value of "-1", which
4889 * cannot be set with the other sequence. The parameter identi-
4890 * fies the resource to be disabled:
4891 * Ps = 1 -> modifyCursorKeys.
4892 * Ps = 2 -> modifyFunctionKeys.
4893 * Ps = 4 -> modifyOtherKeys.
4894 * If the parameter is omitted, modifyFunctionKeys is disabled.
4895 * When modifyFunctionKeys is disabled, xterm uses the modifier
4896 * keys to make an extended sequence of functions rather than
4897 * adding a parameter to each function key to denote the modi-
4900 Terminal
.prototype.disableModifiers = function(params
) {
4907 * Set resource value pointerMode. This is used by xterm to
4908 * decide whether to hide the pointer cursor as the user types.
4909 * Valid values for the parameter:
4910 * Ps = 0 -> never hide the pointer.
4911 * Ps = 1 -> hide if the mouse tracking mode is not enabled.
4912 * Ps = 2 -> always hide the pointer. If no parameter is
4913 * given, xterm uses the default, which is 1 .
4915 Terminal
.prototype.setPointerMode = function(params
) {
4921 * CSI ! p Soft terminal reset (DECSTR).
4922 * http://vt100.net/docs/vt220-rm/table4-10.html
4924 Terminal
.prototype.softReset = function(params
) {
4925 this.cursorHidden
= false;
4926 this.insertMode
= false;
4927 this.originMode
= false;
4928 this.wraparoundMode
= false; // autowrap
4929 this.applicationKeypad
= false; // ?
4930 this.applicationCursor
= false;
4932 this.scrollBottom
= this.rows
- 1;
4933 this.curAttr
= this.defAttr
;
4934 this.x
= this.y
= 0; // ?
4935 this.charset
= null;
4936 this.glevel
= 0; // ??
4937 this.charsets
= [null]; // ??
4943 * Request ANSI mode (DECRQM). For VT300 and up, reply is
4945 * where Ps is the mode number as in RM, and Pm is the mode
4947 * 0 - not recognized
4950 * 3 - permanently set
4951 * 4 - permanently reset
4953 Terminal
.prototype.requestAnsiMode = function(params
) {
4960 * Request DEC private mode (DECRQM). For VT300 and up, reply is
4962 * where Ps is the mode number as in DECSET, Pm is the mode value
4963 * as in the ANSI DECRQM.
4965 Terminal
.prototype.requestPrivateMode = function(params
) {
4972 * Set conformance level (DECSCL). Valid values for the first
4974 * Ps = 6 1 -> VT100.
4975 * Ps = 6 2 -> VT200.
4976 * Ps = 6 3 -> VT300.
4977 * Valid values for the second parameter:
4978 * Ps = 0 -> 8-bit controls.
4979 * Ps = 1 -> 7-bit controls (always set for VT100).
4980 * Ps = 2 -> 8-bit controls.
4982 Terminal
.prototype.setConformanceLevel = function(params
) {
4988 * CSI Ps q Load LEDs (DECLL).
4989 * Ps = 0 -> Clear all LEDS (default).
4990 * Ps = 1 -> Light Num Lock.
4991 * Ps = 2 -> Light Caps Lock.
4992 * Ps = 3 -> Light Scroll Lock.
4993 * Ps = 2 1 -> Extinguish Num Lock.
4994 * Ps = 2 2 -> Extinguish Caps Lock.
4995 * Ps = 2 3 -> Extinguish Scroll Lock.
4997 Terminal
.prototype.loadLEDs = function(params
) {
5004 * Set cursor style (DECSCUSR, VT520).
5005 * Ps = 0 -> blinking block.
5006 * Ps = 1 -> blinking block (default).
5007 * Ps = 2 -> steady block.
5008 * Ps = 3 -> blinking underline.
5009 * Ps = 4 -> steady underline.
5011 Terminal
.prototype.setCursorStyle = function(params
) {
5018 * Select character protection attribute (DECSCA). Valid values
5019 * for the parameter:
5020 * Ps = 0 -> DECSED and DECSEL can erase (default).
5021 * Ps = 1 -> DECSED and DECSEL cannot erase.
5022 * Ps = 2 -> DECSED and DECSEL can erase.
5024 Terminal
.prototype.setCharProtectionAttr = function(params
) {
5031 * Restore DEC Private Mode Values. The value of Ps previously
5032 * saved is restored. Ps values are the same as for DECSET.
5034 Terminal
.prototype.restorePrivateValues = function(params
) {
5040 * CSI Pt; Pl; Pb; Pr; Ps$ r
5041 * Change Attributes in Rectangular Area (DECCARA), VT400 and up.
5042 * Pt; Pl; Pb; Pr denotes the rectangle.
5043 * Ps denotes the SGR attributes to change: 0, 1, 4, 5, 7.
5044 * NOTE: xterm doesn't enable this code by default.
5046 Terminal
.prototype.setAttrInRectangle = function(params
) {
5056 for (; t
< b
+ 1; t
++) {
5057 line
= this.lines
[this.ybase
+ t
];
5058 for (i
= l
; i
< r
; i
++) {
5059 line
[i
] = [attr
, line
[i
][1]];
5064 this.updateRange(params
[0]);
5065 this.updateRange(params
[2]);
5070 * CSI Pc; Pt; Pl; Pb; Pr$ x
5071 * Fill Rectangular Area (DECFRA), VT420 and up.
5072 * Pc is the character to use.
5073 * Pt; Pl; Pb; Pr denotes the rectangle.
5074 * NOTE: xterm doesn't enable this code by default.
5076 Terminal
.prototype.fillRectangle = function(params
) {
5086 for (; t
< b
+ 1; t
++) {
5087 line
= this.lines
[this.ybase
+ t
];
5088 for (i
= l
; i
< r
; i
++) {
5089 line
[i
] = [line
[i
][0], String
.fromCharCode(ch
)];
5094 this.updateRange(params
[1]);
5095 this.updateRange(params
[3]);
5101 * Enable Locator Reporting (DECELR).
5102 * Valid values for the first parameter:
5103 * Ps = 0 -> Locator disabled (default).
5104 * Ps = 1 -> Locator enabled.
5105 * Ps = 2 -> Locator enabled for one report, then disabled.
5106 * The second parameter specifies the coordinate unit for locator
5108 * Valid values for the second parameter:
5109 * Pu = 0 <- or omitted -> default to character cells.
5110 * Pu = 1 <- device physical pixels.
5111 * Pu = 2 <- character cells.
5113 Terminal
.prototype.enableLocatorReporting = function(params
) {
5114 var val
= params
[0] > 0;
5115 //this.mouseEvents = val;
5116 //this.decLocator = val;
5121 * CSI Pt; Pl; Pb; Pr$ z
5122 * Erase Rectangular Area (DECERA), VT400 and up.
5123 * Pt; Pl; Pb; Pr denotes the rectangle.
5124 * NOTE: xterm doesn't enable this code by default.
5126 Terminal
.prototype.eraseRectangle = function(params
) {
5136 ch
= [this.eraseAttr(), ' ', 1]; // xterm?
5138 for (; t
< b
+ 1; t
++) {
5139 line
= this.lines
[this.ybase
+ t
];
5140 for (i
= l
; i
< r
; i
++) {
5146 this.updateRange(params
[0]);
5147 this.updateRange(params
[2]);
5153 * Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
5154 * NOTE: xterm doesn't enable this code by default.
5156 Terminal
.prototype.insertColumns = function() {
5157 var param
= params
[0]
5158 , l
= this.ybase
+ this.rows
5159 , ch
= [this.eraseAttr(), ' ', 1] // xterm?
5163 for (i
= this.ybase
; i
< l
; i
++) {
5164 this.lines
[i
].splice(this.x
+ 1, 0, ch
);
5165 this.lines
[i
].pop();
5175 * Delete P s Column(s) (default = 1) (DECDC), VT420 and up
5176 * NOTE: xterm doesn't enable this code by default.
5178 Terminal
.prototype.deleteColumns = function() {
5179 var param
= params
[0]
5180 , l
= this.ybase
+ this.rows
5181 , ch
= [this.eraseAttr(), ' ', 1] // xterm?
5185 for (i
= this.ybase
; i
< l
; i
++) {
5186 this.lines
[i
].splice(this.x
, 1);
5187 this.lines
[i
].push(ch
);
5198 Terminal
.charsets
= {};
5200 // DEC Special Character and Line Drawing Set.
5201 // http://vt100.net/docs/vt102-ug/table5-13.html
5202 // A lot of curses apps use this if they see TERM=xterm.
5203 // testing: echo -e '\e(0a\e(B'
5204 // The xterm output sometimes seems to conflict with the
5205 // reference above. xterm seems in line with the reference
5206 // when running vttest however.
5207 // The table below now uses xterm's output from vttest.
5208 Terminal
.charsets
.SCLD
= { // (0
5209 '`': '\u25c6', // '◆'
5210 'a': '\u2592', // '▒'
5211 'b': '\u0009', // '\t'
5212 'c': '\u000c', // '\f'
5213 'd': '\u000d', // '\r'
5214 'e': '\u000a', // '\n'
5215 'f': '\u00b0', // '°'
5216 'g': '\u00b1', // '±'
5217 'h': '\u2424', // '\u2424' (NL)
5218 'i': '\u000b', // '\v'
5219 'j': '\u2518', // '┘'
5220 'k': '\u2510', // '┐'
5221 'l': '\u250c', // '┌'
5222 'm': '\u2514', // '└'
5223 'n': '\u253c', // '┼'
5224 'o': '\u23ba', // '⎺'
5225 'p': '\u23bb', // '⎻'
5226 'q': '\u2500', // '─'
5227 'r': '\u23bc', // '⎼'
5228 's': '\u23bd', // '⎽'
5229 't': '\u251c', // '├'
5230 'u': '\u2524', // '┤'
5231 'v': '\u2534', // '┴'
5232 'w': '\u252c', // '┬'
5233 'x': '\u2502', // '│'
5234 'y': '\u2264', // '≤'
5235 'z': '\u2265', // '≥'
5236 '{': '\u03c0', // 'π'
5237 '|': '\u2260', // '≠'
5238 '}': '\u00a3', // '£'
5239 '~': '\u00b7' // '·'
5242 Terminal
.charsets
.UK
= null; // (A
5243 Terminal
.charsets
.US
= null; // (B (USASCII)
5244 Terminal
.charsets
.Dutch
= null; // (4
5245 Terminal
.charsets
.Finnish
= null; // (C or (5
5246 Terminal
.charsets
.French
= null; // (R
5247 Terminal
.charsets
.FrenchCanadian
= null; // (Q
5248 Terminal
.charsets
.German
= null; // (K
5249 Terminal
.charsets
.Italian
= null; // (Y
5250 Terminal
.charsets
.NorwegianDanish
= null; // (E or (6
5251 Terminal
.charsets
.Spanish
= null; // (Z
5252 Terminal
.charsets
.Swedish
= null; // (H or (7
5253 Terminal
.charsets
.Swiss
= null; // (=
5254 Terminal
.charsets
.ISOLatin
= null; // /A
5260 function contains(el
, arr
) {
5261 for (var i
= 0; i
< arr
.length
; i
+= 1) {
5262 if (el
=== arr
[i
]) {
5269 function on(el
, type
, handler
, capture
) {
5270 if (!Array
.isArray(el
)) {
5273 el
.forEach(function (element
) {
5274 element
.addEventListener(type
, handler
, capture
|| false);
5278 function off(el
, type
, handler
, capture
) {
5279 el
.removeEventListener(type
, handler
, capture
|| false);
5282 function cancel(ev
, force
) {
5283 if (!this.cancelEvents
&& !force
) {
5286 ev
.preventDefault();
5287 ev
.stopPropagation();
5291 function inherits(child
, parent
) {
5293 this.constructor = child
;
5295 f
.prototype = parent
.prototype;
5296 child
.prototype = new f
;
5299 // if bold is broken, we can't
5300 // use it in the terminal.
5301 function isBoldBroken(document
) {
5302 var body
= document
.getElementsByTagName('body')[0];
5303 var el
= document
.createElement('span');
5304 el
.innerHTML
= 'hello world';
5305 body
.appendChild(el
);
5306 var w1
= el
.scrollWidth
;
5307 el
.style
.fontWeight
= 'bold';
5308 var w2
= el
.scrollWidth
;
5309 body
.removeChild(el
);
5313 var String
= this.String
;
5314 var setTimeout
= this.setTimeout
;
5315 var setInterval
= this.setInterval
;
5317 function indexOf(obj
, el
) {
5320 if (obj
[i
] === el
) return i
;
5325 function isThirdLevelShift(term
, ev
) {
5327 (term
.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) ||
5328 (term
.isMSWindows
&& ev
.altKey
&& ev
.ctrlKey
&& !ev
.metaKey
);
5330 if (ev
.type
== 'keypress') {
5331 return thirdLevelKey
;
5334 // Don't invoke for arrows, pageDown, home, backspace, etc. (on non-keypress events)
5335 return thirdLevelKey
&& (!ev
.keyCode
|| ev
.keyCode
> 47);
5338 function matchColor(r1
, g1
, b1
) {
5339 var hash
= (r1
<< 16) | (g1
<< 8) | b1
;
5341 if (matchColor
._cache
[hash
] != null) {
5342 return matchColor
._cache
[hash
];
5345 var ldiff
= Infinity
5354 for (; i
< Terminal
.vcolors
.length
; i
++) {
5355 c
= Terminal
.vcolors
[i
];
5360 diff
= matchColor
.distance(r1
, g1
, b1
, r2
, g2
, b2
);
5373 return matchColor
._cache
[hash
] = li
;
5376 matchColor
._cache
= {};
5378 // http://stackoverflow.com/questions/1633828
5379 matchColor
.distance = function(r1
, g1
, b1
, r2
, g2
, b2
) {
5380 return Math
.pow(30 * (r1
- r2
), 2)
5381 + Math
.pow(59 * (g1
- g2
), 2)
5382 + Math
.pow(11 * (b1
- b2
), 2);
5385 function each(obj
, iter
, con
) {
5386 if (obj
.forEach
) return obj
.forEach(iter
, con
);
5387 for (var i
= 0; i
< obj
.length
; i
++) {
5388 iter
.call(con
, obj
[i
], i
, obj
);
5392 function keys(obj
) {
5393 if (Object
.keys
) return Object
.keys(obj
);
5396 if (Object
.prototype.hasOwnProperty
.call(obj
, key
)) {
5403 var wcwidth
= (function(opts
) {
5404 // extracted from https://www.cl.cam.ac.uk/%7Emgk25/ucs/wcwidth.c
5405 // combining characters
5407 [0x0300, 0x036F], [0x0483, 0x0486], [0x0488, 0x0489],
5408 [0x0591, 0x05BD], [0x05BF, 0x05BF], [0x05C1, 0x05C2],
5409 [0x05C4, 0x05C5], [0x05C7, 0x05C7], [0x0600, 0x0603],
5410 [0x0610, 0x0615], [0x064B, 0x065E], [0x0670, 0x0670],
5411 [0x06D6, 0x06E4], [0x06E7, 0x06E8], [0x06EA, 0x06ED],
5412 [0x070F, 0x070F], [0x0711, 0x0711], [0x0730, 0x074A],
5413 [0x07A6, 0x07B0], [0x07EB, 0x07F3], [0x0901, 0x0902],
5414 [0x093C, 0x093C], [0x0941, 0x0948], [0x094D, 0x094D],
5415 [0x0951, 0x0954], [0x0962, 0x0963], [0x0981, 0x0981],
5416 [0x09BC, 0x09BC], [0x09C1, 0x09C4], [0x09CD, 0x09CD],
5417 [0x09E2, 0x09E3], [0x0A01, 0x0A02], [0x0A3C, 0x0A3C],
5418 [0x0A41, 0x0A42], [0x0A47, 0x0A48], [0x0A4B, 0x0A4D],
5419 [0x0A70, 0x0A71], [0x0A81, 0x0A82], [0x0ABC, 0x0ABC],
5420 [0x0AC1, 0x0AC5], [0x0AC7, 0x0AC8], [0x0ACD, 0x0ACD],
5421 [0x0AE2, 0x0AE3], [0x0B01, 0x0B01], [0x0B3C, 0x0B3C],
5422 [0x0B3F, 0x0B3F], [0x0B41, 0x0B43], [0x0B4D, 0x0B4D],
5423 [0x0B56, 0x0B56], [0x0B82, 0x0B82], [0x0BC0, 0x0BC0],
5424 [0x0BCD, 0x0BCD], [0x0C3E, 0x0C40], [0x0C46, 0x0C48],
5425 [0x0C4A, 0x0C4D], [0x0C55, 0x0C56], [0x0CBC, 0x0CBC],
5426 [0x0CBF, 0x0CBF], [0x0CC6, 0x0CC6], [0x0CCC, 0x0CCD],
5427 [0x0CE2, 0x0CE3], [0x0D41, 0x0D43], [0x0D4D, 0x0D4D],
5428 [0x0DCA, 0x0DCA], [0x0DD2, 0x0DD4], [0x0DD6, 0x0DD6],
5429 [0x0E31, 0x0E31], [0x0E34, 0x0E3A], [0x0E47, 0x0E4E],
5430 [0x0EB1, 0x0EB1], [0x0EB4, 0x0EB9], [0x0EBB, 0x0EBC],
5431 [0x0EC8, 0x0ECD], [0x0F18, 0x0F19], [0x0F35, 0x0F35],
5432 [0x0F37, 0x0F37], [0x0F39, 0x0F39], [0x0F71, 0x0F7E],
5433 [0x0F80, 0x0F84], [0x0F86, 0x0F87], [0x0F90, 0x0F97],
5434 [0x0F99, 0x0FBC], [0x0FC6, 0x0FC6], [0x102D, 0x1030],
5435 [0x1032, 0x1032], [0x1036, 0x1037], [0x1039, 0x1039],
5436 [0x1058, 0x1059], [0x1160, 0x11FF], [0x135F, 0x135F],
5437 [0x1712, 0x1714], [0x1732, 0x1734], [0x1752, 0x1753],
5438 [0x1772, 0x1773], [0x17B4, 0x17B5], [0x17B7, 0x17BD],
5439 [0x17C6, 0x17C6], [0x17C9, 0x17D3], [0x17DD, 0x17DD],
5440 [0x180B, 0x180D], [0x18A9, 0x18A9], [0x1920, 0x1922],
5441 [0x1927, 0x1928], [0x1932, 0x1932], [0x1939, 0x193B],
5442 [0x1A17, 0x1A18], [0x1B00, 0x1B03], [0x1B34, 0x1B34],
5443 [0x1B36, 0x1B3A], [0x1B3C, 0x1B3C], [0x1B42, 0x1B42],
5444 [0x1B6B, 0x1B73], [0x1DC0, 0x1DCA], [0x1DFE, 0x1DFF],
5445 [0x200B, 0x200F], [0x202A, 0x202E], [0x2060, 0x2063],
5446 [0x206A, 0x206F], [0x20D0, 0x20EF], [0x302A, 0x302F],
5447 [0x3099, 0x309A], [0xA806, 0xA806], [0xA80B, 0xA80B],
5448 [0xA825, 0xA826], [0xFB1E, 0xFB1E], [0xFE00, 0xFE0F],
5449 [0xFE20, 0xFE23], [0xFEFF, 0xFEFF], [0xFFF9, 0xFFFB],
5450 [0x10A01, 0x10A03], [0x10A05, 0x10A06], [0x10A0C, 0x10A0F],
5451 [0x10A38, 0x10A3A], [0x10A3F, 0x10A3F], [0x1D167, 0x1D169],
5452 [0x1D173, 0x1D182], [0x1D185, 0x1D18B], [0x1D1AA, 0x1D1AD],
5453 [0x1D242, 0x1D244], [0xE0001, 0xE0001], [0xE0020, 0xE007F],
5457 function bisearch(ucs
) {
5459 var max
= COMBINING
.length
- 1;
5461 if (ucs
< COMBINING
[0][0] || ucs
> COMBINING
[max
][1])
5463 while (max
>= min
) {
5464 mid
= Math
.floor((min
+ max
) / 2);
5465 if (ucs
> COMBINING
[mid
][1])
5467 else if (ucs
< COMBINING
[mid
][0])
5474 function wcwidth(ucs
) {
5475 // test for 8-bit control characters
5478 if (ucs
< 32 || (ucs
>= 0x7f && ucs
< 0xa0))
5479 return opts
.control
;
5480 // binary search in table of non-spacing characters
5483 // if we arrive here, ucs is not a combining or C0/C1 control character
5488 ucs
<= 0x115f || // Hangul Jamo init. consonants
5491 (ucs
>= 0x2e80 && ucs
<= 0xa4cf && ucs
!= 0x303f) || // CJK..Yi
5492 (ucs
>= 0xac00 && ucs
<= 0xd7a3) || // Hangul Syllables
5493 (ucs
>= 0xf900 && ucs
<= 0xfaff) || // CJK Compat Ideographs
5494 (ucs
>= 0xfe10 && ucs
<= 0xfe19) || // Vertical forms
5495 (ucs
>= 0xfe30 && ucs
<= 0xfe6f) || // CJK Compat Forms
5496 (ucs
>= 0xff00 && ucs
<= 0xff60) || // Fullwidth Forms
5497 (ucs
>= 0xffe0 && ucs
<= 0xffe6) ||
5498 (ucs
>= 0x20000 && ucs
<= 0x2fffd) ||
5499 (ucs
>= 0x30000 && ucs
<= 0x3fffd)
5504 })({nul
: 0, control
: 0}); // configurable options
5510 Terminal
.EventEmitter
= EventEmitter
;
5511 Terminal
.CompositionHelper
= CompositionHelper
;
5512 Terminal
.Viewport
= Viewport
;
5513 Terminal
.inherits
= inherits
;
5516 * Adds an event listener to the terminal.
5518 * @param {string} event The name of the event. TODO: Document all event types
5519 * @param {function} callback The function to call when the event is triggered.
5523 Terminal
.cancel
= cancel
;