2 * xterm.js: xterm, in the browser
3 * Copyright (c) 2014, sourceLair Limited (www.sourcelair.com (MIT License)
4 * Copyright (c) 2012-2013, Christopher Jeffrey (MIT License)
5 * https://github.com/chjj/term.js
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * Originally forked from (with the author's permission):
26 * Fabrice Bellard's javascript vt100 for jslinux:
27 * http://bellard.org/jslinux/
28 * Copyright (c) 2011 Fabrice Bellard
29 * The original design remains. The terminal itself
30 * has been extended to include xterm CSI codes, among
35 if (typeof exports
=== 'object' && typeof module
=== 'object') {
37 * CommonJS environment
39 module
.exports
= xterm
.call(this);
40 } else if (typeof define
== 'function') {
42 * Require.js is available
44 define([], xterm
.bind(window
));
47 * Plain browser environment
49 this.Xterm
= xterm
.call(this);
50 this.Terminal
= this.Xterm
; /* Backwards compatibility with term.js */
54 * Terminal Emulation References:
56 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.txt
57 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
58 * http://invisible-island.net/vttest/
59 * http://www.inwap.com/pdp10/ansicode.txt
60 * http://linux.die.net/man/4/console_codes
61 * http://linux.die.net/man/7/urxvt
70 var window
= this, document
= this.document
;
76 function EventEmitter() {
77 this._events
= this._events
|| {};
80 EventEmitter
.prototype.addListener = function(type
, listener
) {
81 this._events
[type
] = this._events
[type
] || [];
82 this._events
[type
].push(listener
);
85 EventEmitter
.prototype.on
= EventEmitter
.prototype.addListener
;
87 EventEmitter
.prototype.removeListener = function(type
, listener
) {
88 if (!this._events
[type
]) return;
90 var obj
= this._events
[type
]
94 if (obj
[i
] === listener
|| obj
[i
].listener
=== listener
) {
101 EventEmitter
.prototype.off
= EventEmitter
.prototype.removeListener
;
103 EventEmitter
.prototype.removeAllListeners = function(type
) {
104 if (this._events
[type
]) delete this._events
[type
];
107 EventEmitter
.prototype.once = function(type
, listener
) {
110 var args
= Array
.prototype.slice
.call(arguments
);
111 this.removeListener(type
, on
);
112 return listener
.apply(this, args
);
114 on
.listener
= listener
;
115 return this.on(type
, on
);
118 EventEmitter
.prototype.emit = function(type
) {
119 if (!this._events
[type
]) return;
121 var args
= Array
.prototype.slice
.call(arguments
, 1)
122 , obj
= this._events
[type
]
127 obj
[i
].apply(this, args
);
131 EventEmitter
.prototype.listeners = function(type
) {
132 return this._events
[type
] = this._events
[type
] || [];
137 * Encapsulates the logic for handling compositionstart, compositionupdate and compositionend
138 * events, displaying the in-progress composition to the UI and forwarding the final composition
140 * @param {HTMLTextAreaElement} textarea The textarea that xterm uses for input.
141 * @param {HTMLElement} compositionView The element to display the in-progress composition in.
142 * @param {Terminal} terminal The Terminal to forward the finished composition to.
144 function CompositionHelper(textarea
, compositionView
, terminal
) {
145 this.textarea
= textarea
;
146 this.compositionView
= compositionView
;
147 this.terminal
= terminal
;
149 // Whether input composition is currently happening, eg. via a mobile keyboard, speech input
150 // or IME. This variable determines whether the compositionText should be displayed on the UI.
151 this.isComposing
= false;
153 // The input currently being composed, eg. via a mobile keyboard, speech input or IME.
154 this.compositionText
= null;
156 // The position within the input textarea's value of the current composition.
157 this.compositionPosition
= { start
: null, end
: null };
159 // Whether a composition is in the process of being sent, setting this to false will cancel
160 // any in-progress composition.
161 this.isSendingComposition
= false;
165 * Handles the compositionstart event, activating the composition view.
167 CompositionHelper
.prototype.compositionstart = function() {
168 this.isComposing
= true;
169 this.compositionPosition
.start
= this.textarea
.value
.length
;
170 this.compositionView
.textContent
= '';
171 this.compositionView
.classList
.add('active');
175 * Handles the compositionupdate event, updating the composition view.
176 * @param {CompositionEvent} ev The event.
178 CompositionHelper
.prototype.compositionupdate = function(ev
) {
179 this.compositionView
.textContent
= ev
.data
;
180 this.updateCompositionElements();
182 setTimeout(function() {
183 self
.compositionPosition
.end
= self
.textarea
.value
.length
;
188 * Handles the compositionend event, hiding the composition view and sending the composition to
191 CompositionHelper
.prototype.compositionend = function() {
192 this.finalizeComposition(true);
196 * Handles the keydown event, routing any necessary events to the CompositionHelper functions.
197 * @return Whether the Terminal should continue processing the keydown event.
199 CompositionHelper
.prototype.keydown = function(ev
) {
200 if (this.isComposing
|| this.isSendingComposition
) {
201 if (ev
.keyCode
=== 229) {
202 // Continue composing if the keyCode is the "composition character"
204 } else if (ev
.keyCode
=== 16 || ev
.keyCode
=== 17 || ev
.keyCode
=== 18) {
205 // Continue composing if the keyCode is a modifier key
208 // Finish composition immediately. This is mainly here for the case where enter is
209 // pressed and the handler needs to be triggered before the command is executed.
210 this.finalizeComposition(false);
214 if (ev
.keyCode
=== 229) {
215 // If the "composition character" is used but gets to this point it means a non-composition
216 // character (eg. numbers and punctuation) was pressed when the IME was active.
217 this.handleAnyTextareaChanges();
225 * Finalizes the composition, resuming regular input actions. This is called when a composition
227 * @param {boolean} waitForPropogation Whether to wait for events to propogate before sending
228 * the input. This should be false if a non-composition keystroke is entered before the
229 * compositionend event is triggered, such as enter, so that the composition is send before
230 * the command is executed.
232 CompositionHelper
.prototype.finalizeComposition = function(waitForPropogation
) {
233 this.compositionView
.classList
.remove('active');
234 this.isComposing
= false;
235 this.clearTextareaPosition();
237 if (!waitForPropogation
) {
238 // Cancel any delayed composition send requests and send the input immediately.
239 this.isSendingComposition
= false;
240 var input
= this.textarea
.value
.substring(this.compositionPosition
.start
, this.compositionPosition
.end
);
241 this.terminal
.handler(input
);
243 // Make a deep copy of the composition position here as a new compositionstart event may
244 // fire before the setTimeout executes.
245 var currentCompositionPosition
= {
246 start
: this.compositionPosition
.start
,
247 end
: this.compositionPosition
.end
,
250 // Since composition* events happen before the changes take place in the textarea on most
251 // browsers, use a setTimeout with 0ms time to allow the native compositionend event to
252 // complete. This ensures the correct character is retrieved, this solution was used
254 // - The compositionend event's data property is unreliable, at least on Chromium
255 // - The last compositionupdate event's data property does not always accurately describe
256 // the character, a counter example being Korean where an ending consonsant can move to
257 // the following character if the following input is a vowel.
259 this.isSendingComposition
= true;
260 setTimeout(function () {
261 // Ensure that the input has not already been sent
262 if (self
.isSendingComposition
) {
263 self
.isSendingComposition
= false;
265 if (self
.isComposing
) {
266 // Use the end position to get the string if a new composition has started.
267 input
= self
.textarea
.value
.substring(currentCompositionPosition
.start
, currentCompositionPosition
.end
);
269 // Don't use the end position here in order to pick up any characters after the
270 // composition has finished, for example when typing a non-composition character
271 // (eg. 2) after a composition character.
272 input
= self
.textarea
.value
.substring(currentCompositionPosition
.start
);
274 self
.terminal
.handler(input
);
281 * Apply any changes made to the textarea after the current event chain is allowed to complete.
282 * This should be called when not currently composing but a keydown event with the "composition
283 * character" (229) is triggered, in order to allow non-composition text to be entered when an
286 CompositionHelper
.prototype.handleAnyTextareaChanges = function() {
287 var oldValue
= this.textarea
.value
;
289 setTimeout(function() {
290 // Ignore if a composition has started since the timeout
291 if (!self
.isComposing
) {
292 var newValue
= self
.textarea
.value
;
293 var diff
= newValue
.replace(oldValue
, '');
294 if (diff
.length
> 0) {
295 self
.terminal
.handler(diff
);
302 * Positions the composition view on top of the cursor and the textarea just below it (so the
303 * IME helper dialog is positioned correctly).
305 CompositionHelper
.prototype.updateCompositionElements = function() {
306 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 this.textarea
.style
.left
= cursor
.offsetLeft
+ 'px';
314 this.textarea
.style
.top
= (cursor
.offsetTop
+ cursor
.offsetHeight
) + 'px';
319 * Clears the textarea's position so that the cursor does not blink on IE.
322 CompositionHelper
.prototype.clearTextareaPosition = function() {
323 this.textarea
.style
.left
= '';
324 this.textarea
.style
.top
= '';
328 * Represents the viewport of a terminal, the visible area within the larger buffer of output.
329 * Logic for the virtual scroll bar is included in this object.
330 * @param {Terminal} terminal The Terminal object.
331 * @param {HTMLElement} viewportElement The DOM element acting as the viewport
332 * @param {HTMLElement} charMeasureElement A DOM element used to measure the character size of
335 function Viewport(terminal
, viewportElement
, scrollArea
, charMeasureElement
) {
336 this.terminal
= terminal
;
337 this.viewportElement
= viewportElement
;
338 this.scrollArea
= scrollArea
;
339 this.charMeasureElement
= charMeasureElement
;
340 this.currentRowHeight
= 0;
341 this.lastRecordedBufferLength
= 0;
342 this.lastRecordedViewportHeight
= 0;
344 this.terminal
.on('scroll', this.syncScrollArea
.bind(this));
345 this.terminal
.on('resize', this.syncScrollArea
.bind(this));
346 this.viewportElement
.addEventListener('scroll', this.onScroll
.bind(this));
348 this.syncScrollArea();
352 * Refreshes row height, setting line-height, viewport height and scroll area height if
354 * @param {number|undefined} charSize A character size measurement bounding rect object, if it
355 * doesn't exist it will be created.
357 Viewport
.prototype.refresh = function(charSize
) {
358 var size
= charSize
|| this.charMeasureElement
.getBoundingClientRect();
359 if (size
.height
> 0) {
360 if (size
.height
!== this.currentRowHeight
) {
361 this.currentRowHeight
= size
.height
;
362 this.viewportElement
.style
.lineHeight
= size
.height
+ 'px';
363 this.terminal
.rowContainer
.style
.lineHeight
= size
.height
+ 'px';
365 if (this.lastRecordedViewportHeight
!== this.terminal
.rows
) {
366 this.lastRecordedViewportHeight
= this.terminal
.rows
;
367 this.viewportElement
.style
.height
= size
.height
* this.terminal
.rows
+ 'px';
369 this.scrollArea
.style
.height
= (size
.height
* this.lastRecordedBufferLength
) + 'px';
374 * Updates dimensions and synchronizes the scroll area if necessary.
376 Viewport
.prototype.syncScrollArea = function() {
377 if (this.isApplicationMode
) {
378 // Fix scroll bar in application mode
379 this.lastRecordedBufferLength
= this.terminal
.rows
;
384 if (this.lastRecordedBufferLength
!== this.terminal
.lines
.length
) {
385 // If buffer height changed
386 this.lastRecordedBufferLength
= this.terminal
.lines
.length
;
388 } else if (this.lastRecordedViewportHeight
!== this.terminal
.rows
) {
389 // If viewport height changed
392 // If size has changed, refresh viewport
393 var size
= this.charMeasureElement
.getBoundingClientRect();
394 if (size
.height
!== this.currentRowHeight
) {
400 var scrollTop
= this.terminal
.ydisp
* this.currentRowHeight
;
401 if (this.viewportElement
.scrollTop
!== scrollTop
) {
402 this.viewportElement
.scrollTop
= scrollTop
;
407 * Sets the application mode of the viewport.
408 * @param {boolean} isApplicationMode Sets whether the terminal is in application mode. true
409 * for application mode (DECKPAM) and false for normal mode (DECKPNM).
411 Viewport
.prototype.setApplicationMode = function(isApplicationMode
) {
412 this.isApplicationMode
= isApplicationMode
;
413 this.syncScrollArea();
417 * Handles scroll events on the viewport, calculating the new viewport and requesting the
418 * terminal to scroll to it.
419 * @param {Event} ev The scroll event.
421 Viewport
.prototype.onScroll = function(ev
) {
422 var newRow
= Math
.round(this.viewportElement
.scrollTop
/ this.currentRowHeight
);
423 var diff
= newRow
- this.terminal
.ydisp
;
424 this.terminal
.scrollDisp(diff
, true);
428 * Handles mouse wheel events by adjusting the viewport's scrollTop and delegating the actual
429 * scrolling to `onScroll`, this event needs to be attached manually by the consumer of
431 * @param {WheelEvent} ev The mouse wheel event.
433 Viewport
.prototype.onWheel = function(ev
) {
434 if (ev
.deltaY
=== 0) {
435 // Do nothing if it's not a vertical scroll event
438 // Fallback to WheelEvent.DOM_DELTA_PIXEL
440 if (ev
.deltaMode
=== WheelEvent
.DOM_DELTA_LINE
) {
441 multiplier
= this.currentRowHeight
;
442 } else if (ev
.deltaMode
=== WheelEvent
.DOM_DELTA_PAGE
) {
443 multiplier
= this.currentRowHeight
* this.terminal
.rows
;
445 this.viewportElement
.scrollTop
+= ev
.deltaY
* multiplier
;
446 // Prevent the page from scrolling when the terminal scrolls
453 var normal
= 0, escaped
= 1, csi
= 2, osc
= 3, charset
= 4, dcs
= 5, ignore
= 6;
460 * Creates a new `Terminal` object.
462 * @param {object} options An object containing a set of options, the available options are:
463 * - cursorBlink (boolean): Whether the terminal cursor blinks
467 * @alias module:xterm/src/xterm
469 function Terminal(options
) {
472 if (!(this instanceof Terminal
)) {
473 return new Terminal(arguments
[0], arguments
[1], arguments
[2]);
476 self
.cancel
= Terminal
.cancel
;
478 EventEmitter
.call(this);
480 if (typeof options
=== 'number') {
484 handler
: arguments
[2]
488 options
= options
|| {};
491 Object
.keys(Terminal
.defaults
).forEach(function(key
) {
492 if (options
[key
] == null) {
493 options
[key
] = Terminal
.options
[key
];
495 if (Terminal
[key
] !== Terminal
.defaults
[key
]) {
496 options
[key
] = Terminal
[key
];
499 self
[key
] = options
[key
];
502 if (options
.colors
.length
=== 8) {
503 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(8));
504 } else if (options
.colors
.length
=== 16) {
505 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(16));
506 } else if (options
.colors
.length
=== 10) {
507 options
.colors
= options
.colors
.slice(0, -2).concat(
508 Terminal
._colors
.slice(8, -2), options
.colors
.slice(-2));
509 } else if (options
.colors
.length
=== 18) {
510 options
.colors
= options
.colors
.concat(
511 Terminal
._colors
.slice(16, -2), options
.colors
.slice(-2));
513 this.colors
= options
.colors
;
515 this.options
= options
;
517 // this.context = options.context || window;
518 // this.document = options.document || document;
519 this.parent
= options
.body
|| options
.parent
520 || (document
? document
.getElementsByTagName('body')[0] : null);
522 this.cols
= options
.cols
|| options
.geometry
[0];
523 this.rows
= options
.rows
|| options
.geometry
[1];
525 if (options
.handler
) {
526 this.on('data', options
.handler
);
530 * The scroll position of the y cursor, ie. ybase + y = the y position within the entire
536 * The scroll position of the viewport
541 * The cursor's x position after ybase
546 * The cursor's y position after ybase
551 * Used to debounce the refresh function
553 this.isRefreshing
= false;
556 * Whether there is a full terminal refresh queued
559 this.cursorState
= 0;
560 this.cursorHidden
= false;
565 this.scrollBottom
= this.rows
- 1;
566 this.customKeydownHandler
= null;
569 this.applicationKeypad
= false;
570 this.applicationCursor
= false;
571 this.originMode
= false;
572 this.insertMode
= false;
573 this.wraparoundMode
= true; // defaults: xterm - true, vt100 - false
578 this.gcharset
= null;
580 this.charsets
= [null];
604 this.readable
= true;
605 this.writable
= true;
607 this.defAttr
= (0 << 18) | (257 << 9) | (256 << 0);
608 this.curAttr
= this.defAttr
;
611 this.currentParam
= 0;
615 // leftover surrogate high from previous write invocation
616 this.surrogate_high
= '';
619 * An array of all lines in the entire buffer, including the prompt. The lines are array of
620 * characters which are 2-length arrays where [0] is an attribute and [1] is the character.
625 this.lines
.push(this.blankLine());
632 inherits(Terminal
, EventEmitter
);
635 * back_color_erase feature for xterm.
637 Terminal
.prototype.eraseAttr = function() {
638 // if (this.is('screen')) return this.defAttr;
639 return (this.defAttr
& ~0x1ff) | (this.curAttr
& 0x1ff);
647 Terminal
.tangoColors
= [
668 // Colors 0-15 + 16-255
669 // Much thanks to TooTallNate for writing this.
670 Terminal
.colors
= (function() {
671 var colors
= Terminal
.tangoColors
.slice()
672 , r
= [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff]
677 for (; i
< 216; i
++) {
678 out(r
[(i
/ 36) % 6 | 0], r
[(i
/ 6) % 6 | 0], r
[i
% 6]);
683 for (; i
< 24; i
++) {
688 function out(r
, g
, b
) {
689 colors
.push('#' + hex(r
) + hex(g
) + hex(b
));
694 return c
.length
< 2 ? '0' + c
: c
;
700 Terminal
._colors
= Terminal
.colors
.slice();
702 Terminal
.vcolors
= (function() {
704 , colors
= Terminal
.colors
708 for (; i
< 256; i
++) {
709 color
= parseInt(colors
[i
].substring(1), 16);
711 (color
>> 16) & 0xff,
724 Terminal
.defaults
= {
725 colors
: Terminal
.colors
,
737 // programFeatures: false,
741 Terminal
.options
= {};
743 Terminal
.focus
= null;
745 each(keys(Terminal
.defaults
), function(key
) {
746 Terminal
[key
] = Terminal
.defaults
[key
];
747 Terminal
.options
[key
] = Terminal
.defaults
[key
];
751 * Focus the terminal. Delegates focus handling to the terminal's DOM element.
753 Terminal
.prototype.focus = function() {
754 return this.textarea
.focus();
758 * Binds the desired focus behavior on a given terminal object.
762 Terminal
.bindFocus = function (term
) {
763 on(term
.textarea
, 'focus', function (ev
) {
764 if (term
.sendFocus
) {
767 term
.element
.classList
.add('focus');
769 Terminal
.focus
= term
;
770 term
.emit('focus', {terminal
: term
});
775 * Blur the terminal. Delegates blur handling to the terminal's DOM element.
777 Terminal
.prototype.blur = function() {
778 return this.textarea
.blur();
782 * Binds the desired blur behavior on a given terminal object.
786 Terminal
.bindBlur = function (term
) {
787 on(term
.textarea
, 'blur', function (ev
) {
788 term
.refresh(term
.y
, term
.y
);
789 if (term
.sendFocus
) {
792 term
.element
.classList
.remove('focus');
793 Terminal
.focus
= null;
794 term
.emit('blur', {terminal
: term
});
799 * Initialize default behavior
801 Terminal
.prototype.initGlobal = function() {
802 Terminal
.bindPaste(this);
803 Terminal
.bindKeys(this);
804 Terminal
.bindCopy(this);
805 Terminal
.bindFocus(this);
806 Terminal
.bindBlur(this);
810 * Bind to paste event and allow both keyboard and right-click pasting, without having the
811 * contentEditable value set to true.
813 Terminal
.bindPaste = function(term
) {
814 on([term
.textarea
, term
.element
], 'paste', function(ev
) {
815 ev
.stopPropagation();
816 if (ev
.clipboardData
) {
817 var text
= ev
.clipboardData
.getData('text/plain');
819 term
.textarea
.value
= '';
820 return term
.cancel(ev
);
826 * Prepares text copied from terminal selection, to be saved in the clipboard by:
827 * 1. stripping all trailing white spaces
828 * 2. converting all non-breaking spaces to regular spaces
829 * @param {string} text The copied text that needs processing for storing in clipboard
833 Terminal
.prepareCopiedTextForClipboard = function (text
) {
834 var space
= String
.fromCharCode(32),
835 nonBreakingSpace
= String
.fromCharCode(160),
836 allNonBreakingSpaces
= new RegExp(nonBreakingSpace
, 'g'),
837 processedText
= text
.split('\n').map(function (line
) {
839 * Strip all trailing white spaces and convert all non-breaking spaces to regular
842 var processedLine
= line
.replace(/\s+$/g, '').replace(allNonBreakingSpaces
, space
);
844 return processedLine
;
847 return processedText
;
851 * Apply key handling to the terminal
853 Terminal
.bindKeys = function(term
) {
854 on(term
.element
, 'keydown', function(ev
) {
855 if (document
.activeElement
!= this) {
861 on(term
.element
, 'keypress', function(ev
) {
862 if (document
.activeElement
!= this) {
868 on(term
.element
, 'keyup', term
.focus
.bind(term
));
870 on(term
.textarea
, 'keydown', function(ev
) {
874 on(term
.textarea
, 'keypress', function(ev
) {
876 // Truncate the textarea's value, since it is not needed
880 on(term
.textarea
, 'compositionstart', term
.compositionHelper
.compositionstart
.bind(term
.compositionHelper
));
881 on(term
.textarea
, 'compositionupdate', term
.compositionHelper
.compositionupdate
.bind(term
.compositionHelper
));
882 on(term
.textarea
, 'compositionend', term
.compositionHelper
.compositionend
.bind(term
.compositionHelper
));
883 term
.on('refresh', term
.compositionHelper
.updateCompositionElements
.bind(term
.compositionHelper
));
887 * Binds copy functionality to the given terminal.
890 Terminal
.bindCopy = function(term
) {
891 on(term
.element
, 'copy', function(ev
) {
898 * Insert the given row to the terminal or produce a new one
899 * if no row argument is passed. Return the inserted row.
900 * @param {HTMLElement} row (optional) The row to append to the terminal.
902 Terminal
.prototype.insertRow = function (row
) {
903 if (typeof row
!= 'object') {
904 row
= document
.createElement('div');
907 this.rowContainer
.appendChild(row
);
908 this.children
.push(row
);
914 * Opens the terminal within an element.
916 * @param {HTMLElement} parent The element to create the terminal within.
918 Terminal
.prototype.open = function(parent
) {
919 var self
=this, i
=0, div
;
921 this.parent
= parent
|| this.parent
;
924 throw new Error('Terminal requires a parent element.');
928 * Grab global elements
930 this.context
= this.parent
.ownerDocument
.defaultView
;
931 this.document
= this.parent
.ownerDocument
;
932 this.body
= this.document
.getElementsByTagName('body')[0];
937 if (this.context
.navigator
&& this.context
.navigator
.userAgent
) {
938 this.isMSIE
= !!~this.context
.navigator
.userAgent
.indexOf('MSIE');
942 * Find the users platform. We use this to interpret the meta key
943 * and ISO third level shifts.
944 * http://stackoverflow.com/questions/19877924/what-is-the-list-of-possible-values-for-navigator-platform-as-of-today
946 if (this.context
.navigator
&& this.context
.navigator
.platform
) {
947 this.isMac
= contains(
948 this.context
.navigator
.platform
,
949 ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K']
951 this.isIpad
= this.context
.navigator
.platform
=== 'iPad';
952 this.isIphone
= this.context
.navigator
.platform
=== 'iPhone';
953 this.isMSWindows
= contains(
954 this.context
.navigator
.platform
,
955 ['Windows', 'Win16', 'Win32', 'WinCE']
960 * Create main element container
962 this.element
= this.document
.createElement('div');
963 this.element
.classList
.add('terminal');
964 this.element
.classList
.add('xterm');
965 this.element
.classList
.add('xterm-theme-' + this.theme
);
967 this.element
.style
.height
968 this.element
.setAttribute('tabindex', 0);
970 this.viewportElement
= document
.createElement('div');
971 this.viewportElement
.classList
.add('xterm-viewport');
972 this.element
.appendChild(this.viewportElement
);
973 this.viewportScrollArea
= document
.createElement('div');
974 this.viewportScrollArea
.classList
.add('xterm-scroll-area');
975 this.viewportElement
.appendChild(this.viewportScrollArea
);
978 * Create the container that will hold the lines of the terminal and then
979 * produce the lines the lines.
981 this.rowContainer
= document
.createElement('div');
982 this.rowContainer
.classList
.add('xterm-rows');
983 this.element
.appendChild(this.rowContainer
);
987 * Create the container that will hold helpers like the textarea for
988 * capturing DOM Events. Then produce the helpers.
990 this.helperContainer
= document
.createElement('div');
991 this.helperContainer
.classList
.add('xterm-helpers');
992 // TODO: This should probably be inserted once it's filled to prevent an additional layout
993 this.element
.appendChild(this.helperContainer
);
994 this.textarea
= document
.createElement('textarea');
995 this.textarea
.classList
.add('xterm-helper-textarea');
996 this.textarea
.setAttribute('autocorrect', 'off');
997 this.textarea
.setAttribute('autocapitalize', 'off');
998 this.textarea
.setAttribute('spellcheck', 'false');
999 this.textarea
.tabIndex
= 0;
1000 this.textarea
.addEventListener('focus', function() {
1001 self
.emit('focus', {terminal
: self
});
1003 this.textarea
.addEventListener('blur', function() {
1004 self
.emit('blur', {terminal
: self
});
1006 this.helperContainer
.appendChild(this.textarea
);
1008 this.compositionView
= document
.createElement('div');
1009 this.compositionView
.classList
.add('composition-view');
1010 this.compositionHelper
= new CompositionHelper(this.textarea
, this.compositionView
, this);
1011 this.helperContainer
.appendChild(this.compositionView
);
1013 this.charMeasureElement
= document
.createElement('div');
1014 this.charMeasureElement
.classList
.add('xterm-char-measure-element');
1015 this.charMeasureElement
.innerHTML
= 'W';
1016 this.helperContainer
.appendChild(this.charMeasureElement
);
1018 for (; i
< this.rows
; i
++) {
1021 this.parent
.appendChild(this.element
);
1023 this.viewport
= new Viewport(this, this.viewportElement
, this.viewportScrollArea
, this.charMeasureElement
);
1026 this.refresh(0, this.rows
- 1);
1028 // Initialize global actions that
1029 // need to be taken on the document.
1032 // Ensure there is a Terminal.focus.
1035 on(this.element
, 'mouseup', function() {
1036 var selection
= document
.getSelection(),
1037 collapsed
= selection
.isCollapsed
,
1038 isRange
= typeof collapsed
== 'boolean' ? !collapsed
: selection
.type
== 'Range';
1044 // Listen for mouse events and translate
1045 // them into terminal mouse protocols.
1048 // Figure out whether boldness affects
1049 // the character width of monospace fonts.
1050 if (Terminal
.brokenBold
== null) {
1051 Terminal
.brokenBold
= isBoldBroken(this.document
);
1059 * Attempts to load an add-on using CommonJS or RequireJS (whichever is available).
1060 * @param {string} addon The name of the addon to load
1063 Terminal
.loadAddon = function(addon
, callback
) {
1064 if (typeof exports
=== 'object' && typeof module
=== 'object') {
1066 return require(__dirname
+ '/../addons/' + addon
);
1067 } else if (typeof define
== 'function') {
1069 return require(['../addons/' + addon
+ '/' + addon
], callback
);
1071 console
.error('Cannot load a module without a CommonJS or RequireJS environment.');
1078 * XTerm mouse events
1079 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#Mouse%20Tracking
1080 * To better understand these
1081 * the xterm code is very helpful:
1083 * button.c, charproc.c, misc.c
1084 * Relevant functions in xterm/button.c:
1085 * BtnCode, EmitButtonCode, EditorButton, SendMousePosition
1087 Terminal
.prototype.bindMouse = function() {
1088 var el
= this.element
, self
= this, pressed
= 32;
1090 // mouseup, mousedown, wheel
1091 // left click: ^[[M 3<^[[M#3<
1092 // wheel up: ^[[M`3>
1093 function sendButton(ev
) {
1097 // get the xterm-style button
1098 button
= getButton(ev
);
1100 // get mouse coordinates
1101 pos
= getCoords(ev
);
1104 sendEvent(button
, pos
);
1106 switch (ev
.overrideType
|| ev
.type
) {
1111 // keep it at the left
1112 // button, just in case.
1123 // motion example of a left click:
1124 // ^[[M 3<^[[M@4<^[[M@5<^[[M@6<^[[M@7<^[[M#7<
1125 function sendMove(ev
) {
1126 var button
= pressed
1129 pos
= getCoords(ev
);
1132 // buttons marked as motions
1133 // are incremented by 32
1136 sendEvent(button
, pos
);
1139 // encode button and
1140 // position to characters
1141 function encode(data
, ch
) {
1142 if (!self
.utfMouse
) {
1143 if (ch
=== 255) return data
.push(0);
1144 if (ch
> 127) ch
= 127;
1147 if (ch
=== 2047) return data
.push(0);
1151 if (ch
> 2047) ch
= 2047;
1152 data
.push(0xC0 | (ch
>> 6));
1153 data
.push(0x80 | (ch
& 0x3F));
1158 // send a mouse event:
1159 // regular/utf8: ^[[M Cb Cx Cy
1160 // urxvt: ^[[ Cb ; Cx ; Cy M
1161 // sgr: ^[[ Cb ; Cx ; Cy M/m
1162 // vt300: ^[[ 24(1/3/5)~ [ Cx , Cy ] \r
1163 // locator: CSI P e ; P b ; P r ; P c ; P p & w
1164 function sendEvent(button
, pos
) {
1165 // self.emit('mouse', {
1171 if (self
.vt300Mouse
) {
1173 // http://www.vt100.net/docs/vt3xx-gp/chapter15.html
1177 var data
= '\x1b[24';
1178 if (button
=== 0) data
+= '1';
1179 else if (button
=== 1) data
+= '3';
1180 else if (button
=== 2) data
+= '5';
1181 else if (button
=== 3) return;
1183 data
+= '~[' + pos
.x
+ ',' + pos
.y
+ ']\r';
1188 if (self
.decLocator
) {
1193 if (button
=== 0) button
= 2;
1194 else if (button
=== 1) button
= 4;
1195 else if (button
=== 2) button
= 6;
1196 else if (button
=== 3) button
= 3;
1200 + (button
=== 3 ? 4 : 0)
1211 if (self
.urxvtMouse
) {
1216 self
.send('\x1b[' + button
+ ';' + pos
.x
+ ';' + pos
.y
+ 'M');
1220 if (self
.sgrMouse
) {
1224 + ((button
& 3) === 3 ? button
& ~3 : button
)
1229 + ((button
& 3) === 3 ? 'm' : 'M'));
1235 encode(data
, button
);
1236 encode(data
, pos
.x
);
1237 encode(data
, pos
.y
);
1239 self
.send('\x1b[M' + String
.fromCharCode
.apply(String
, data
));
1242 function getButton(ev
) {
1255 // 1, and 2 - with 64 added
1256 switch (ev
.overrideType
|| ev
.type
) {
1258 button
= ev
.button
!= null
1265 button
= button
=== 1 ? 0 : button
=== 4 ? 1 : button
;
1271 case 'DOMMouseScroll':
1272 button
= ev
.detail
< 0
1277 button
= ev
.wheelDeltaY
> 0
1283 // next three bits are the modifiers:
1284 // 4 = shift, 8 = meta, 16 = control
1285 shift
= ev
.shiftKey
? 4 : 0;
1286 meta
= ev
.metaKey
? 8 : 0;
1287 ctrl
= ev
.ctrlKey
? 16 : 0;
1288 mod
= shift
| meta
| ctrl
;
1291 if (self
.vt200Mouse
) {
1294 } else if (!self
.normalMouse
) {
1299 button
= (32 + (mod
<< 2)) + button
;
1304 // mouse coordinates measured in cols/rows
1305 function getCoords(ev
) {
1308 // ignore browsers without pageX for now
1309 if (ev
.pageX
== null) return;
1315 // should probably check offsetParent
1316 // but this is more portable
1317 while (el
&& el
!== self
.document
.documentElement
) {
1320 el
= 'offsetParent' in el
1325 // convert to cols/rows
1326 w
= self
.element
.clientWidth
;
1327 h
= self
.element
.clientHeight
;
1328 x
= Math
.round((x
/ w
) * self
.cols
);
1329 y
= Math
.round((y
/ h
) * self
.rows
);
1331 // be sure to avoid sending
1332 // bad positions to the program
1334 if (x
> self
.cols
) x
= self
.cols
;
1336 if (y
> self
.rows
) y
= self
.rows
;
1338 // xterm sends raw bytes and
1339 // starts at 32 (SP) for each.
1350 on(el
, 'mousedown', function(ev
) {
1351 if (!self
.mouseEvents
) return;
1360 //if (self.vt200Mouse && !self.normalMouse) {
1361 if (self
.vt200Mouse
) {
1362 ev
.overrideType
= 'mouseup';
1364 return self
.cancel(ev
);
1368 if (self
.normalMouse
) on(self
.document
, 'mousemove', sendMove
);
1370 // x10 compatibility mode can't send button releases
1371 if (!self
.x10Mouse
) {
1372 on(self
.document
, 'mouseup', function up(ev
) {
1374 if (self
.normalMouse
) off(self
.document
, 'mousemove', sendMove
);
1375 off(self
.document
, 'mouseup', up
);
1376 return self
.cancel(ev
);
1380 return self
.cancel(ev
);
1383 //if (self.normalMouse) {
1384 // on(self.document, 'mousemove', sendMove);
1387 on(el
, 'wheel', function(ev
) {
1388 if (!self
.mouseEvents
) return;
1391 || self
.decLocator
) return;
1393 return self
.cancel(ev
);
1396 // allow wheel scrolling in
1397 // the shell for example
1398 on(el
, 'wheel', function(ev
) {
1399 if (self
.mouseEvents
) return;
1400 if (self
.applicationKeypad
) return;
1401 self
.viewport
.onWheel(ev
);
1402 return self
.cancel(ev
);
1407 * Destroys the terminal.
1409 Terminal
.prototype.destroy = function() {
1410 this.readable
= false;
1411 this.writable
= false;
1413 this.handler = function() {};
1414 this.write = function() {};
1415 if (this.element
.parentNode
) {
1416 this.element
.parentNode
.removeChild(this.element
);
1418 //this.emit('close');
1423 * Flags used to render terminal text properly
1434 * Refreshes (re-renders) terminal content within two rows (inclusive)
1438 * In the screen buffer, each character is stored as a an array with a character
1439 * and a 32-bit integer:
1440 * - First value: a utf-16 character.
1442 * - Next 9 bits: background color (0-511).
1443 * - Next 9 bits: foreground color (0-511).
1444 * - Next 14 bits: a mask for misc. flags:
1451 * @param {number} start The row to start from (between 0 and terminal's height terminal - 1)
1452 * @param {number} end The row to end at (between fromRow and terminal's height terminal - 1)
1453 * @param {boolean} queue Whether the refresh should ran right now or be queued
1455 Terminal
.prototype.refresh = function(start
, end
, queue
) {
1458 // queue defaults to true
1459 queue
= (typeof queue
== 'undefined') ? true : queue
;
1462 * The refresh queue allows refresh to execute only approximately 30 times a second. For
1463 * commands that pass a significant amount of output to the write function, this prevents the
1464 * terminal from maxing out the CPU and making the UI unresponsive. While commands can still
1465 * run beyond what they do on the terminal, it is far better with a debounce in place as
1466 * every single terminal manipulation does not need to be constructed in the DOM.
1468 * A side-effect of this is that it makes ^C to interrupt a process seem more responsive.
1471 // If refresh should be queued, order the refresh and return.
1472 if (this._refreshIsQueued
) {
1473 // If a refresh has already been queued, just order a full refresh next
1474 this._fullRefreshNext
= true;
1476 setTimeout(function () {
1477 self
.refresh(start
, end
, false);
1479 this._refreshIsQueued
= true;
1484 // If refresh should be run right now (not be queued), release the lock
1485 this._refreshIsQueued
= false;
1487 // If multiple refreshes were requested, make a full refresh.
1488 if (this._fullRefreshNext
) {
1490 end
= this.rows
- 1;
1491 this._fullRefreshNext
= false // reset lock
1494 var x
, y
, i
, line
, out
, ch
, ch_width
, width
, data
, attr
, bg
, fg
, flags
, row
, parent
, focused
= document
.activeElement
;
1496 // If this is a big refresh, remove the terminal rows from the DOM for faster calculations
1497 if (end
- start
>= this.rows
/ 2) {
1498 parent
= this.element
.parentNode
;
1500 this.element
.removeChild(this.rowContainer
);
1507 if (end
>= this.rows
.length
) {
1508 this.log('`end` is too large. Most likely a bad CSR.');
1509 end
= this.rows
.length
- 1;
1512 for (; y
<= end
; y
++) {
1513 row
= y
+ this.ydisp
;
1515 line
= this.lines
[row
];
1518 if (this.y
=== y
- (this.ybase
- this.ydisp
)
1520 && !this.cursorHidden
) {
1526 attr
= this.defAttr
;
1529 for (; i
< width
; i
++) {
1532 ch_width
= line
[i
][2];
1536 if (i
=== x
) data
= -1;
1538 if (data
!== attr
) {
1539 if (attr
!== this.defAttr
) {
1542 if (data
!== this.defAttr
) {
1544 out
+= '<span class="reverse-video terminal-cursor';
1545 if (this.cursorBlink
) {
1550 var classNames
= [];
1553 fg
= (data
>> 9) & 0x1ff;
1556 if (flags
& Terminal
.flags
.BOLD
) {
1557 if (!Terminal
.brokenBold
) {
1558 classNames
.push('xterm-bold');
1560 // See: XTerm*boldColors
1561 if (fg
< 8) fg
+= 8;
1564 if (flags
& Terminal
.flags
.UNDERLINE
) {
1565 classNames
.push('xterm-underline');
1568 if (flags
& Terminal
.flags
.BLINK
) {
1569 classNames
.push('xterm-blink');
1573 * If inverse flag is on, then swap the foreground and background variables.
1575 if (flags
& Terminal
.flags
.INVERSE
) {
1576 /* One-line variable swap in JavaScript: http://stackoverflow.com/a/16201730 */
1577 bg = [fg, fg = bg][0];
1578 // Should inverse just be before the
1579 // above boldColors effect instead?
1580 if ((flags & 1) && fg < 8) fg += 8;
1583 if (flags & Terminal.flags.INVISIBLE) {
1584 classNames.push('xterm-hidden');
1588 * Weird situation: Invert flag used black foreground and white background results
1589 * in invalid background color, positioned at the 256 index of the 256 terminal
1590 * color map. Pin the colors manually in such a case.
1592 * Source: https://github.com/sourcelair/xterm.js/issues/57
1594 if (flags
& Terminal
.flags
.INVERSE
) {
1604 classNames
.push('xterm-bg-color-' + bg
);
1608 classNames
.push('xterm-color-' + fg
);
1612 if (classNames
.length
) {
1613 out
+= ' class="' + classNames
.join(' ') + '"';
1642 if (attr
!== this.defAttr
) {
1646 this.children
[y
].innerHTML
= out
;
1650 this.element
.appendChild(this.rowContainer
);
1653 this.emit('refresh', {element
: this.element
, start
: start
, end
: end
});
1657 * Display the cursor element
1659 Terminal
.prototype.showCursor = function() {
1660 if (!this.cursorState
) {
1661 this.cursorState
= 1;
1662 this.refresh(this.y
, this.y
);
1667 * Scroll the terminal
1669 Terminal
.prototype.scroll = function() {
1672 if (++this.ybase
=== this.scrollback
) {
1673 this.ybase
= this.ybase
/ 2 | 0;
1674 this.lines
= this.lines
.slice(-(this.ybase
+ this.rows
) + 1);
1677 this.ydisp
= this.ybase
;
1680 row
= this.ybase
+ this.rows
- 1;
1682 // subtract the bottom scroll region
1683 row
-= this.rows
- 1 - this.scrollBottom
;
1685 if (row
=== this.lines
.length
) {
1686 // potential optimization:
1687 // pushing is faster than splicing
1688 // when they amount to the same
1690 this.lines
.push(this.blankLine());
1693 this.lines
.splice(row
, 0, this.blankLine());
1696 if (this.scrollTop
!== 0) {
1697 if (this.ybase
!== 0) {
1699 this.ydisp
= this.ybase
;
1701 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
1705 this.updateRange(this.scrollTop
);
1706 this.updateRange(this.scrollBottom
);
1708 this.emit('scroll', this.ydisp
);
1712 * Scroll the display of the terminal
1713 * @param {number} disp The number of lines to scroll down (negatives scroll up).
1714 * @param {boolean} suppressScrollEvent Don't emit the scroll event as scrollDisp. This is used
1715 * to avoid unwanted events being handled by the veiwport when the event was triggered from the
1716 * viewport originally.
1718 Terminal
.prototype.scrollDisp = function(disp
, suppressScrollEvent
) {
1721 if (this.ydisp
> this.ybase
) {
1722 this.ydisp
= this.ybase
;
1723 } else if (this.ydisp
< 0) {
1727 if (!suppressScrollEvent
) {
1728 this.emit('scroll', this.ydisp
);
1731 this.refresh(0, this.rows
- 1);
1735 * Writes text to the terminal.
1736 * @param {string} text The text to write to the terminal.
1738 Terminal
.prototype.write = function(data
) {
1739 var l
= data
.length
, i
= 0, j
, cs
, ch
, code
, low
, ch_width
, row
;
1741 this.refreshStart
= this.y
;
1742 this.refreshEnd
= this.y
;
1744 if (this.ybase
!== this.ydisp
) {
1745 this.ydisp
= this.ybase
;
1746 this.emit('scroll', this.ydisp
);
1750 // apply leftover surrogate high from last write
1751 if (this.surrogate_high
) {
1752 data
= this.surrogate_high
+ data
;
1753 this.surrogate_high
= '';
1756 for (; i
< l
; i
++) {
1759 // FIXME: higher chars than 0xa0 are not allowed in escape sequences
1760 // --> maybe move to default
1761 code
= data
.charCodeAt(i
);
1762 if (0xD800 <= code
&& code
<= 0xDBFF) {
1763 // we got a surrogate high
1764 // get surrogate low (next 2 bytes)
1765 low
= data
.charCodeAt(i
+1);
1767 // end of data stream, save surrogate high
1768 this.surrogate_high
= ch
;
1771 code
= ((code
- 0xD800) * 0x400) + (low
- 0xDC00) + 0x10000;
1772 ch
+= data
.charAt(i
+1);
1774 // surrogate low - already handled above
1775 if (0xDC00 <= code
&& code
<= 0xDFFF)
1778 switch (this.state
) {
1789 if (this.convertEol
) {
1793 if (this.y
> this.scrollBottom
) {
1813 this.x
= this.nextStop();
1828 this.state
= escaped
;
1833 // calculate print space
1834 // expensive call, therefore we save width in line buffer
1835 ch_width
= wcwidth(code
);
1838 if (this.charset
&& this.charset
[ch
]) {
1839 ch
= this.charset
[ch
];
1842 row
= this.y
+ this.ybase
;
1844 // insert combining char in last cell
1845 // FIXME: needs handling after cursor jumps
1846 if (!ch_width
&& this.x
) {
1848 // dont overflow left
1849 if (this.lines
[row
][this.x
-1]) {
1850 if (!this.lines
[row
][this.x
-1][2]) {
1852 // found empty cell after fullwidth, need to go 2 cells back
1853 if (this.lines
[row
][this.x
-2])
1854 this.lines
[row
][this.x
-2][1] += ch
;
1857 this.lines
[row
][this.x
-1][1] += ch
;
1859 this.updateRange(this.y
);
1864 // goto next line if ch would overflow
1865 // TODO: needs a global min terminal width of 2
1866 if (this.x
+ch_width
-1 >= this.cols
) {
1867 // autowrap - DECAWM
1868 if (this.wraparoundMode
) {
1871 if (this.y
> this.scrollBottom
) {
1876 this.x
= this.cols
-1;
1877 if(ch_width
===2) // FIXME: check for xterm behavior
1881 row
= this.y
+ this.ybase
;
1883 // insert mode: move characters to right
1884 if (this.insertMode
) {
1885 // do this twice for a fullwidth char
1886 for (var moves
=0; moves
<ch_width
; ++moves
) {
1887 // remove last cell, if it's width is 0
1888 // we have to adjust the second last cell as well
1889 var removed
= this.lines
[this.y
+ this.ybase
].pop();
1891 && this.lines
[row
][this.cols
-2]
1892 && this.lines
[row
][this.cols
-2][2]===2)
1893 this.lines
[row
][this.cols
-2] = [this.curAttr
, ' ', 1];
1895 // insert empty cell at cursor
1896 this.lines
[row
].splice(this.x
, 0, [this.curAttr
, ' ', 1]);
1900 this.lines
[row
][this.x
] = [this.curAttr
, ch
, ch_width
];
1902 this.updateRange(this.y
);
1904 // fullwidth char - set next cell width to zero and advance cursor
1906 this.lines
[row
][this.x
] = [this.curAttr
, '', 0];
1915 // ESC [ Control Sequence Introducer ( CSI is 0x9b).
1918 this.currentParam
= 0;
1922 // ESC ] Operating System Command ( OSC is 0x9d).
1925 this.currentParam
= 0;
1929 // ESC P Device Control String ( DCS is 0x90).
1932 this.currentParam
= 0;
1936 // ESC _ Application Program Command ( APC is 0x9f).
1938 this.state
= ignore
;
1941 // ESC ^ Privacy Message ( PM is 0x9e).
1943 this.state
= ignore
;
1946 // ESC c Full Reset (RIS).
1951 // ESC E Next Line ( NEL is 0x85).
1952 // ESC D Index ( IND is 0x84).
1960 // ESC M Reverse Index ( RI is 0x8d).
1962 this.reverseIndex();
1965 // ESC % Select default/utf-8 character set.
1966 // @ = default, G = utf-8
1968 //this.charset = null;
1970 this.setgCharset(0, Terminal
.charsets
.US
);
1971 this.state
= normal
;
1975 // ESC (,),*,+,-,. Designate G0-G2 Character Set.
1976 case '(': // <-- this seems to get all the attention
2002 this.state
= charset
;
2005 // Designate G3 Character Set (VT300).
2006 // A = ISO Latin-1 Supplemental.
2010 this.state
= charset
;
2015 // Single Shift Select of G2 Character Set
2016 // ( SS2 is 0x8e). This affects next character only.
2020 // Single Shift Select of G3 Character Set
2021 // ( SS3 is 0x8f). This affects next character only.
2025 // Invoke the G2 Character Set as GL (LS2).
2030 // Invoke the G3 Character Set as GL (LS3).
2035 // Invoke the G3 Character Set as GR (LS3R).
2040 // Invoke the G2 Character Set as GR (LS2R).
2045 // Invoke the G1 Character Set as GR (LS1R).
2050 // ESC 7 Save Cursor (DECSC).
2053 this.state
= normal
;
2056 // ESC 8 Restore Cursor (DECRC).
2058 this.restoreCursor();
2059 this.state
= normal
;
2062 // ESC # 3 DEC line height/width
2064 this.state
= normal
;
2068 // ESC H Tab Set (HTS is 0x88).
2073 // ESC = Application Keypad (DECKPAM).
2075 this.log('Serial port requested application keypad.');
2076 this.applicationKeypad
= true;
2077 this.viewport
.setApplicationMode(true);
2078 this.state
= normal
;
2081 // ESC > Normal Keypad (DECKPNM).
2083 this.log('Switching back to normal keypad.');
2084 this.applicationKeypad
= false;
2085 this.viewport
.setApplicationMode(false);
2086 this.state
= normal
;
2090 this.state
= normal
;
2091 this.error('Unknown ESC control: %s.', ch
);
2098 case '0': // DEC Special Character and Line Drawing Set.
2099 cs
= Terminal
.charsets
.SCLD
;
2102 cs
= Terminal
.charsets
.UK
;
2104 case 'B': // United States (USASCII).
2105 cs
= Terminal
.charsets
.US
;
2108 cs
= Terminal
.charsets
.Dutch
;
2110 case 'C': // Finnish
2112 cs
= Terminal
.charsets
.Finnish
;
2115 cs
= Terminal
.charsets
.French
;
2117 case 'Q': // FrenchCanadian
2118 cs
= Terminal
.charsets
.FrenchCanadian
;
2121 cs
= Terminal
.charsets
.German
;
2123 case 'Y': // Italian
2124 cs
= Terminal
.charsets
.Italian
;
2126 case 'E': // NorwegianDanish
2128 cs
= Terminal
.charsets
.NorwegianDanish
;
2130 case 'Z': // Spanish
2131 cs
= Terminal
.charsets
.Spanish
;
2133 case 'H': // Swedish
2135 cs
= Terminal
.charsets
.Swedish
;
2138 cs
= Terminal
.charsets
.Swiss
;
2140 case '/': // ISOLatin (actually /A)
2141 cs
= Terminal
.charsets
.ISOLatin
;
2145 cs
= Terminal
.charsets
.US
;
2148 this.setgCharset(this.gcharset
, cs
);
2149 this.gcharset
= null;
2150 this.state
= normal
;
2156 // Set Text Parameters.
2157 if (ch
=== '\x1b' || ch
=== '\x07') {
2158 if (ch
=== '\x1b') i
++;
2160 this.params
.push(this.currentParam
);
2162 switch (this.params
[0]) {
2166 if (this.params
[1]) {
2167 this.title
= this.params
[1];
2168 this.handleTitle(this.title
);
2176 // change dynamic colors
2188 // change dynamic ui colors
2200 // manipulate selection data
2218 this.currentParam
= 0;
2219 this.state
= normal
;
2221 if (!this.params
.length
) {
2222 if (ch
>= '0' && ch
<= '9') {
2224 this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
2225 } else if (ch
=== ';') {
2226 this.params
.push(this.currentParam
);
2227 this.currentParam
= '';
2230 this.currentParam
+= ch
;
2237 if (ch
=== '?' || ch
=== '>' || ch
=== '!') {
2243 if (ch
>= '0' && ch
<= '9') {
2244 this.currentParam
= this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
2248 // '$', '"', ' ', '\''
2249 if (ch
=== '$' || ch
=== '"' || ch
=== ' ' || ch
=== '\'') {
2254 this.params
.push(this.currentParam
);
2255 this.currentParam
= 0;
2258 if (ch
=== ';') break;
2260 this.state
= normal
;
2264 // Cursor Up Ps Times (default = 1) (CUU).
2266 this.cursorUp(this.params
);
2270 // Cursor Down Ps Times (default = 1) (CUD).
2272 this.cursorDown(this.params
);
2276 // Cursor Forward Ps Times (default = 1) (CUF).
2278 this.cursorForward(this.params
);
2282 // Cursor Backward Ps Times (default = 1) (CUB).
2284 this.cursorBackward(this.params
);
2288 // Cursor Position [row;column] (default = [1,1]) (CUP).
2290 this.cursorPos(this.params
);
2293 // CSI Ps J Erase in Display (ED).
2295 this.eraseInDisplay(this.params
);
2298 // CSI Ps K Erase in Line (EL).
2300 this.eraseInLine(this.params
);
2303 // CSI Pm m Character Attributes (SGR).
2306 this.charAttributes(this.params
);
2310 // CSI Ps n Device Status Report (DSR).
2313 this.deviceStatus(this.params
);
2322 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
2324 this.insertChars(this.params
);
2328 // Cursor Next Line Ps Times (default = 1) (CNL).
2330 this.cursorNextLine(this.params
);
2334 // Cursor Preceding Line Ps Times (default = 1) (CNL).
2336 this.cursorPrecedingLine(this.params
);
2340 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
2342 this.cursorCharAbsolute(this.params
);
2346 // Insert Ps Line(s) (default = 1) (IL).
2348 this.insertLines(this.params
);
2352 // Delete Ps Line(s) (default = 1) (DL).
2354 this.deleteLines(this.params
);
2358 // Delete Ps Character(s) (default = 1) (DCH).
2360 this.deleteChars(this.params
);
2364 // Erase Ps Character(s) (default = 1) (ECH).
2366 this.eraseChars(this.params
);
2369 // CSI Pm ` Character Position Absolute
2370 // [column] (default = [row,1]) (HPA).
2372 this.charPosAbsolute(this.params
);
2376 // Horizontal Position Relative
2378 this.HPositionRelative(this.params
);
2382 // Send Device Attributes (Primary DA).
2384 // Send Device Attributes (Secondary DA)
2386 this.sendDeviceAttributes(this.params
);
2390 // Line Position Absolute [row] (default = [1,column]) (VPA).
2392 this.linePosAbsolute(this.params
);
2395 // 145 65 e * VPR - Vertical Position Relative
2397 this.VPositionRelative(this.params
);
2401 // Horizontal and Vertical Position [row;column] (default =
2404 this.HVPosition(this.params
);
2407 // CSI Pm h Set Mode (SM).
2408 // CSI ? Pm h - mouse escape codes, cursor escape codes
2410 this.setMode(this.params
);
2413 // CSI Pm l Reset Mode (RM).
2416 this.resetMode(this.params
);
2420 // Set Scrolling Region [top;bottom] (default = full size of win-
2424 this.setScrollRegion(this.params
);
2428 // Save cursor (ANSI.SYS).
2430 this.saveCursor(this.params
);
2434 // Restore cursor (ANSI.SYS).
2436 this.restoreCursor(this.params
);
2444 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
2446 this.cursorForwardTab(this.params
);
2449 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
2451 this.scrollUp(this.params
);
2454 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
2455 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
2458 // if (this.prefix === '>') {
2459 // this.resetTitleModes(this.params);
2462 // if (this.params.length > 2) {
2463 // this.initMouseTracking(this.params);
2466 if (this.params
.length
< 2 && !this.prefix
) {
2467 this.scrollDown(this.params
);
2472 // Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
2474 this.cursorBackwardTab(this.params
);
2477 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
2479 this.repeatPrecedingCharacter(this.params
);
2482 // CSI Ps g Tab Clear (TBC).
2484 this.tabClear(this.params
);
2487 // CSI Pm i Media Copy (MC).
2490 // this.mediaCopy(this.params);
2493 // CSI Pm m Character Attributes (SGR).
2495 // case 'm': // duplicate
2496 // if (this.prefix === '>') {
2497 // this.setResources(this.params);
2499 // this.charAttributes(this.params);
2503 // CSI Ps n Device Status Report (DSR).
2505 // case 'n': // duplicate
2506 // if (this.prefix === '>') {
2507 // this.disableModifiers(this.params);
2509 // this.deviceStatus(this.params);
2513 // CSI > Ps p Set pointer mode.
2514 // CSI ! p Soft terminal reset (DECSTR).
2516 // Request ANSI mode (DECRQM).
2518 // Request DEC private mode (DECRQM).
2521 switch (this.prefix
) {
2523 // this.setPointerMode(this.params);
2526 this.softReset(this.params
);
2529 // if (this.postfix === '$') {
2530 // this.requestPrivateMode(this.params);
2534 // if (this.postfix === '"') {
2535 // this.setConformanceLevel(this.params);
2536 // } else if (this.postfix === '$') {
2537 // this.requestAnsiMode(this.params);
2543 // CSI Ps q Load LEDs (DECLL).
2547 // if (this.postfix === ' ') {
2548 // this.setCursorStyle(this.params);
2551 // if (this.postfix === '"') {
2552 // this.setCharProtectionAttr(this.params);
2555 // this.loadLEDs(this.params);
2559 // Set Scrolling Region [top;bottom] (default = full size of win-
2562 // CSI Pt; Pl; Pb; Pr; Ps$ r
2563 // case 'r': // duplicate
2564 // if (this.prefix === '?') {
2565 // this.restorePrivateValues(this.params);
2566 // } else if (this.postfix === '$') {
2567 // this.setAttrInRectangle(this.params);
2569 // this.setScrollRegion(this.params);
2573 // CSI s Save cursor (ANSI.SYS).
2575 // case 's': // duplicate
2576 // if (this.prefix === '?') {
2577 // this.savePrivateValues(this.params);
2579 // this.saveCursor(this.params);
2583 // CSI Ps ; Ps ; Ps t
2584 // CSI Pt; Pl; Pb; Pr; Ps$ t
2588 // if (this.postfix === '$') {
2589 // this.reverseAttrInRectangle(this.params);
2590 // } else if (this.postfix === ' ') {
2591 // this.setWarningBellVolume(this.params);
2593 // if (this.prefix === '>') {
2594 // this.setTitleModeFeature(this.params);
2596 // this.manipulateWindow(this.params);
2601 // CSI u Restore cursor (ANSI.SYS).
2603 // case 'u': // duplicate
2604 // if (this.postfix === ' ') {
2605 // this.setMarginBellVolume(this.params);
2607 // this.restoreCursor(this.params);
2611 // CSI Pt; Pl; Pb; Pr; Pp; Pt; Pl; Pp$ v
2613 // if (this.postfix === '$') {
2614 // this.copyRectagle(this.params);
2618 // CSI Pt ; Pl ; Pb ; Pr ' w
2620 // if (this.postfix === '\'') {
2621 // this.enableFilterRectangle(this.params);
2625 // CSI Ps x Request Terminal Parameters (DECREQTPARM).
2626 // CSI Ps x Select Attribute Change Extent (DECSACE).
2627 // CSI Pc; Pt; Pl; Pb; Pr$ x
2629 // if (this.postfix === '$') {
2630 // this.fillRectangle(this.params);
2632 // this.requestParameters(this.params);
2633 // //this.__(this.params);
2638 // CSI Pt; Pl; Pb; Pr$ z
2640 // if (this.postfix === '\'') {
2641 // this.enableLocatorReporting(this.params);
2642 // } else if (this.postfix === '$') {
2643 // this.eraseRectangle(this.params);
2648 // CSI Pt; Pl; Pb; Pr$ {
2650 // if (this.postfix === '\'') {
2651 // this.setLocatorEvents(this.params);
2652 // } else if (this.postfix === '$') {
2653 // this.selectiveEraseRectangle(this.params);
2659 // if (this.postfix === '\'') {
2660 // this.requestLocatorPosition(this.params);
2665 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
2667 // if (this.postfix === ' ') {
2668 // this.insertColumns(this.params);
2673 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
2675 // if (this.postfix === ' ') {
2676 // this.deleteColumns(this.params);
2681 this.error('Unknown CSI code: %s.', ch
);
2690 if (ch
=== '\x1b' || ch
=== '\x07') {
2691 if (ch
=== '\x1b') i
++;
2693 switch (this.prefix
) {
2694 // User-Defined Keys (DECUDK).
2698 // Request Status String (DECRQSS).
2699 // test: echo -e '\eP$q"p\e\\'
2701 var pt
= this.currentParam
2718 + (this.scrollTop
+ 1)
2720 + (this.scrollBottom
+ 1)
2730 this.error('Unknown DCS Pt: %s.', pt
);
2735 this.send('\x1bP' + +valid
+ '$r' + pt
+ '\x1b\\');
2738 // Set Termcap/Terminfo Data (xterm, experimental).
2742 // Request Termcap/Terminfo String (xterm, experimental)
2743 // Regular xterm does not even respond to this sequence.
2744 // This can cause a small glitch in vim.
2745 // test: echo -ne '\eP+q6b64\e\\'
2747 var pt
= this.currentParam
2750 this.send('\x1bP' + +valid
+ '+r' + pt
+ '\x1b\\');
2754 this.error('Unknown DCS prefix: %s.', this.prefix
);
2758 this.currentParam
= 0;
2760 this.state
= normal
;
2761 } else if (!this.currentParam
) {
2762 if (!this.prefix
&& ch
!== '$' && ch
!== '+') {
2763 this.currentParam
= ch
;
2764 } else if (this.prefix
.length
=== 2) {
2765 this.currentParam
= ch
;
2770 this.currentParam
+= ch
;
2776 if (ch
=== '\x1b' || ch
=== '\x07') {
2777 if (ch
=== '\x1b') i
++;
2778 this.state
= normal
;
2784 this.updateRange(this.y
);
2785 this.refresh(this.refreshStart
, this.refreshEnd
);
2789 * Writes text to the terminal, followed by a break line character (\n).
2790 * @param {string} text The text to write to the terminal.
2792 Terminal
.prototype.writeln = function(data
) {
2793 this.write(data
+ '\r\n');
2797 * Attaches a custom keydown handler which is run before keys are processed, giving consumers of
2798 * xterm.js ultimate control as to what keys should be processed by the terminal and what keys
2800 * @param {function} customKeydownHandler The custom KeyboardEvent handler to attach. This is a
2801 * function that takes a KeyboardEvent, allowing consumers to stop propogation and/or prevent
2802 * the default action. The function returns whether the event should be processed by xterm.js.
2804 Terminal
.prototype.attachCustomKeydownHandler = function(customKeydownHandler
) {
2805 this.customKeydownHandler
= customKeydownHandler
;
2809 * Handle a keydown event
2811 * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
2812 * @param {KeyboardEvent} ev The keydown event to be handled.
2814 Terminal
.prototype.keyDown = function(ev
) {
2815 if (this.customKeydownHandler
&& this.customKeydownHandler(ev
) === false) {
2819 if (!this.compositionHelper
.keydown
.bind(this.compositionHelper
)(ev
)) {
2824 var result
= this.evaluateKeyEscapeSequence(ev
);
2826 if (result
.scrollDisp
) {
2827 this.scrollDisp(result
.scrollDisp
);
2828 return this.cancel(ev
);
2831 if (isThirdLevelShift(this, ev
)) {
2835 if (result
.cancel
) {
2836 // The event is canceled at the end already, is this necessary?
2837 this.cancel(ev
, true);
2844 this.emit('keydown', ev
);
2845 this.emit('key', result
.key
, ev
);
2847 this.handler(result
.key
);
2849 return this.cancel(ev
, true);
2853 * Returns an object that determines how a KeyboardEvent should be handled. The key of the
2854 * returned value is the new key code to pass to the PTY.
2856 * Reference: http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
2857 * @param {KeyboardEvent} ev The keyboard event to be translated to key escape sequence.
2859 Terminal
.prototype.evaluateKeyEscapeSequence = function(ev
) {
2861 // Whether to cancel event propogation (NOTE: this may not be needed since the event is
2862 // canceled at the end of keyDown
2864 // The new key even to emit
2866 // The number of characters to scroll, if this is defined it will cancel the event
2867 scrollDisp
: undefined
2869 var modifiers
= ev
.shiftKey
<< 0 | ev
.altKey
<< 1 | ev
.ctrlKey
<< 2 | ev
.metaKey
<< 3;
2870 switch (ev
.keyCode
) {
2874 result
.key
= '\x08'; // ^H
2877 result
.key
= '\x7f'; // ^?
2882 result
.key
= '\x1b[Z';
2886 result
.cancel
= true;
2891 result
.cancel
= true;
2895 result
.key
= '\x1b';
2896 result
.cancel
= true;
2901 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'D';
2902 else if (this.applicationCursor
)
2903 result
.key
= '\x1bOD';
2905 result
.key
= '\x1b[D';
2910 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'C';
2911 else if (this.applicationCursor
)
2912 result
.key
= '\x1bOC';
2914 result
.key
= '\x1b[C';
2919 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'A';
2920 else if (this.applicationCursor
)
2921 result
.key
= '\x1bOA';
2923 result
.key
= '\x1b[A';
2928 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'B';
2929 else if (this.applicationCursor
)
2930 result
.key
= '\x1bOB';
2932 result
.key
= '\x1b[B';
2936 if (!ev
.shiftKey
&& !ev
.ctrlKey
) {
2937 // <Ctrl> or <Shift> + <Insert> are used to
2938 // copy-paste on some systems.
2939 result
.key
= '\x1b[2~';
2943 case 46: result
.key
= '\x1b[3~'; break;
2947 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'H';
2948 else if (this.applicationCursor
)
2949 result
.key
= '\x1bOH';
2951 result
.key
= '\x1b[H';
2956 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'F';
2957 else if (this.applicationCursor
)
2958 result
.key
= '\x1bOF';
2960 result
.key
= '\x1b[F';
2965 result
.scrollDisp
= -(this.rows
- 1);
2967 result
.key
= '\x1b[5~';
2973 result
.scrollDisp
= this.rows
- 1;
2975 result
.key
= '\x1b[6~';
2979 case 112: result
.key
= '\x1bOP'; break;
2980 case 113: result
.key
= '\x1bOQ'; break;
2981 case 114: result
.key
= '\x1bOR'; break;
2982 case 115: result
.key
= '\x1bOS'; break;
2983 case 116: result
.key
= '\x1b[15~'; break;
2984 case 117: result
.key
= '\x1b[17~'; break;
2985 case 118: result
.key
= '\x1b[18~'; break;
2986 case 119: result
.key
= '\x1b[19~'; break;
2987 case 120: result
.key
= '\x1b[20~'; break;
2988 case 121: result
.key
= '\x1b[21~'; break;
2989 case 122: result
.key
= '\x1b[23~'; break;
2990 case 123: result
.key
= '\x1b[24~'; break;
2993 if (ev
.ctrlKey
&& !ev
.shiftKey
&& !ev
.altKey
&& !ev
.metaKey
) {
2994 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2995 result
.key
= String
.fromCharCode(ev
.keyCode
- 64);
2996 } else if (ev
.keyCode
=== 32) {
2998 result
.key
= String
.fromCharCode(0);
2999 } else if (ev
.keyCode
>= 51 && ev
.keyCode
<= 55) {
3000 // escape, file sep, group sep, record sep, unit sep
3001 result
.key
= String
.fromCharCode(ev
.keyCode
- 51 + 27);
3002 } else if (ev
.keyCode
=== 56) {
3004 result
.key
= String
.fromCharCode(127);
3005 } else if (ev
.keyCode
=== 219) {
3007 result
.key
= String
.fromCharCode(27);
3008 } else if (ev
.keyCode
=== 221) {
3010 result
.key
= String
.fromCharCode(29);
3012 } else if (!this.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) {
3013 // On Mac this is a third level shift. Use <Esc> instead.
3014 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
3015 result
.key
= '\x1b' + String
.fromCharCode(ev
.keyCode
+ 32);
3016 } else if (ev
.keyCode
=== 192) {
3017 result
.key
= '\x1b`';
3018 } else if (ev
.keyCode
>= 48 && ev
.keyCode
<= 57) {
3019 result
.key
= '\x1b' + (ev
.keyCode
- 48);
3028 * Set the G level of the terminal
3031 Terminal
.prototype.setgLevel = function(g
) {
3033 this.charset
= this.charsets
[g
];
3037 * Set the charset for the given G level of the terminal
3041 Terminal
.prototype.setgCharset = function(g
, charset
) {
3042 this.charsets
[g
] = charset
;
3043 if (this.glevel
=== g
) {
3044 this.charset
= charset
;
3049 * Handle a keypress event.
3051 * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
3052 * @param {KeyboardEvent} ev The keypress event to be handled.
3054 Terminal
.prototype.keyPress = function(ev
) {
3061 } else if (ev
.which
== null) {
3063 } else if (ev
.which
!== 0 && ev
.charCode
!== 0) {
3070 (ev
.altKey
|| ev
.ctrlKey
|| ev
.metaKey
) && !isThirdLevelShift(this, ev
)
3075 key
= String
.fromCharCode(key
);
3077 this.emit('keypress', key
, ev
);
3078 this.emit('key', key
, ev
);
3086 * Send data for handling to the terminal
3087 * @param {string} data
3089 Terminal
.prototype.send = function(data
) {
3093 setTimeout(function() {
3094 self
.handler(self
.queue
);
3104 * Note: We could do sweet things with webaudio here
3106 Terminal
.prototype.bell = function() {
3107 if (!this.visualBell
) return;
3109 this.element
.style
.borderColor
= 'white';
3110 setTimeout(function() {
3111 self
.element
.style
.borderColor
= '';
3113 if (this.popOnBell
) this.focus();
3117 * Log the current state to the console.
3119 Terminal
.prototype.log = function() {
3120 if (!this.debug
) return;
3121 if (!this.context
.console
|| !this.context
.console
.log
) return;
3122 var args
= Array
.prototype.slice
.call(arguments
);
3123 this.context
.console
.log
.apply(this.context
.console
, args
);
3127 * Log the current state as error to the console.
3129 Terminal
.prototype.error = function() {
3130 if (!this.debug
) return;
3131 if (!this.context
.console
|| !this.context
.console
.error
) return;
3132 var args
= Array
.prototype.slice
.call(arguments
);
3133 this.context
.console
.error
.apply(this.context
.console
, args
);
3137 * Resizes the terminal.
3139 * @param {number} x The number of columns to resize to.
3140 * @param {number} y The number of rows to resize to.
3142 Terminal
.prototype.resize = function(x
, y
) {
3150 if (x
=== this.cols
&& y
=== this.rows
) {
3160 ch
= [this.defAttr
, ' ', 1]; // does xterm use the default attr?
3161 i
= this.lines
.length
;
3163 while (this.lines
[i
].length
< x
) {
3164 this.lines
[i
].push(ch
);
3168 i
= this.lines
.length
;
3170 while (this.lines
[i
].length
> x
) {
3171 this.lines
[i
].pop();
3184 // y is rows, not this.y
3185 if (this.lines
.length
< y
+ this.ybase
) {
3186 if (this.ybase
> 0 && this.lines
.length
<= this.ybase
+ this.y
+ addToY
+ 1) {
3187 // There is room above the buffer and there are no empty elements below the line,
3191 if (this.ydisp
> 0) {
3192 // Viewport is at the top of the buffer, must increase downwards
3196 // Add a blank line if there is no buffer left at the top to scroll to, or if there
3197 // are blank lines after the cursor
3198 this.lines
.push(this.blankLine());
3201 if (this.children
.length
< y
) {
3207 if (this.lines
.length
> y
+ this.ybase
) {
3208 if (this.lines
.length
> this.ybase
+ this.y
+ 1) {
3209 // The line is a blank line below the cursor, remove it
3212 // The line is the cursor, scroll down
3217 if (this.children
.length
> y
) {
3218 el
= this.children
.shift();
3220 el
.parentNode
.removeChild(el
);
3227 * Make sure that the cursor stays on screen
3241 this.scrollBottom
= y
- 1;
3243 this.refresh(0, this.rows
- 1);
3247 this.emit('resize', {terminal
: this, cols
: x
, rows
: y
});
3251 * Updates the range of rows to refresh
3252 * @param {number} y The number of rows to refresh next.
3254 Terminal
.prototype.updateRange = function(y
) {
3255 if (y
< this.refreshStart
) this.refreshStart
= y
;
3256 if (y
> this.refreshEnd
) this.refreshEnd
= y
;
3257 // if (y > this.refreshEnd) {
3258 // this.refreshEnd = y;
3259 // if (y > this.rows - 1) {
3260 // this.refreshEnd = this.rows - 1;
3266 * Set the range of refreshing to the maximyum value
3268 Terminal
.prototype.maxRange = function() {
3269 this.refreshStart
= 0;
3270 this.refreshEnd
= this.rows
- 1;
3276 * Setup the tab stops.
3279 Terminal
.prototype.setupStops = function(i
) {
3281 if (!this.tabs
[i
]) {
3282 i
= this.prevStop(i
);
3289 for (; i
< this.cols
; i
+= 8) {
3290 this.tabs
[i
] = true;
3296 * Move the cursor to the previous tab stop from the given position (default is current).
3297 * @param {number} x The position to move the cursor to the previous tab stop.
3299 Terminal
.prototype.prevStop = function(x
) {
3300 if (x
== null) x
= this.x
;
3301 while (!this.tabs
[--x
] && x
> 0);
3302 return x
>= this.cols
3309 * Move the cursor one tab stop forward from the given position (default is current).
3310 * @param {number} x The position to move the cursor one tab stop forward.
3312 Terminal
.prototype.nextStop = function(x
) {
3313 if (x
== null) x
= this.x
;
3314 while (!this.tabs
[++x
] && x
< this.cols
);
3315 return x
>= this.cols
3322 * Erase in the identified line everything from "x" to the end of the line (right).
3323 * @param {number} x The column from which to start erasing to the end of the line.
3324 * @param {number} y The line in which to operate.
3326 Terminal
.prototype.eraseRight = function(x
, y
) {
3327 var line
= this.lines
[this.ybase
+ y
]
3328 , ch
= [this.eraseAttr(), ' ', 1]; // xterm
3331 for (; x
< this.cols
; x
++) {
3335 this.updateRange(y
);
3341 * Erase in the identified line everything from "x" to the start of the line (left).
3342 * @param {number} x The column from which to start erasing to the start of the line.
3343 * @param {number} y The line in which to operate.
3345 Terminal
.prototype.eraseLeft = function(x
, y
) {
3346 var line
= this.lines
[this.ybase
+ y
]
3347 , ch
= [this.eraseAttr(), ' ', 1]; // xterm
3350 while (x
--) line
[x
] = ch
;
3352 this.updateRange(y
);
3357 * Erase all content in the given line
3358 * @param {number} y The line to erase all of its contents.
3360 Terminal
.prototype.eraseLine = function(y
) {
3361 this.eraseRight(0, y
);
3366 * Return the data array of a blank line/
3367 * @param {number} cur First bunch of data for each "blank" character.
3369 Terminal
.prototype.blankLine = function(cur
) {
3374 var ch
= [attr
, ' ', 1] // width defaults to 1 halfwidth character
3378 for (; i
< this.cols
; i
++) {
3387 * If cur return the back color xterm feature attribute. Else return defAttr.
3388 * @param {object} cur
3390 Terminal
.prototype.ch = function(cur
) {
3392 ? [this.eraseAttr(), ' ', 1]
3393 : [this.defAttr
, ' ', 1];
3398 * Evaluate if the current erminal is the given argument.
3399 * @param {object} term The terminal to evaluate
3401 Terminal
.prototype.is = function(term
) {
3402 var name
= this.termName
;
3403 return (name
+ '').indexOf(term
) === 0;
3408 * Emit the 'data' event and populate the given data.
3409 * @param {string} data The data to populate in the event.
3411 Terminal
.prototype.handler = function(data
) {
3412 this.emit('data', data
);
3417 * Emit the 'title' event and populate the given title.
3418 * @param {string} title The title to populate in the event.
3420 Terminal
.prototype.handleTitle = function(title
) {
3421 this.emit('title', title
);
3430 * ESC D Index (IND is 0x84).
3432 Terminal
.prototype.index = function() {
3434 if (this.y
> this.scrollBottom
) {
3438 this.state
= normal
;
3443 * ESC M Reverse Index (RI is 0x8d).
3445 Terminal
.prototype.reverseIndex = function() {
3448 if (this.y
< this.scrollTop
) {
3450 // possibly move the code below to term.reverseScroll();
3451 // test: echo -ne '\e[1;1H\e[44m\eM\e[0m'
3452 // blankLine(true) is xterm/linux behavior
3453 this.lines
.splice(this.y
+ this.ybase
, 0, this.blankLine(true));
3454 j
= this.rows
- 1 - this.scrollBottom
;
3455 this.lines
.splice(this.rows
- 1 + this.ybase
- j
+ 1, 1);
3457 this.updateRange(this.scrollTop
);
3458 this.updateRange(this.scrollBottom
);
3460 this.state
= normal
;
3465 * ESC c Full Reset (RIS).
3467 Terminal
.prototype.reset = function() {
3468 this.options
.rows
= this.rows
;
3469 this.options
.cols
= this.cols
;
3470 Terminal
.call(this, this.options
);
3471 this.refresh(0, this.rows
- 1);
3476 * ESC H Tab Set (HTS is 0x88).
3478 Terminal
.prototype.tabSet = function() {
3479 this.tabs
[this.x
] = true;
3480 this.state
= normal
;
3490 * Cursor Up Ps Times (default = 1) (CUU).
3492 Terminal
.prototype.cursorUp = function(params
) {
3493 var param
= params
[0];
3494 if (param
< 1) param
= 1;
3496 if (this.y
< 0) this.y
= 0;
3502 * Cursor Down Ps Times (default = 1) (CUD).
3504 Terminal
.prototype.cursorDown = function(params
) {
3505 var param
= params
[0];
3506 if (param
< 1) param
= 1;
3508 if (this.y
>= this.rows
) {
3509 this.y
= this.rows
- 1;
3516 * Cursor Forward Ps Times (default = 1) (CUF).
3518 Terminal
.prototype.cursorForward = function(params
) {
3519 var param
= params
[0];
3520 if (param
< 1) param
= 1;
3522 if (this.x
>= this.cols
) {
3523 this.x
= this.cols
- 1;
3530 * Cursor Backward Ps Times (default = 1) (CUB).
3532 Terminal
.prototype.cursorBackward = function(params
) {
3533 var param
= params
[0];
3534 if (param
< 1) param
= 1;
3536 if (this.x
< 0) this.x
= 0;
3542 * Cursor Position [row;column] (default = [1,1]) (CUP).
3544 Terminal
.prototype.cursorPos = function(params
) {
3547 row
= params
[0] - 1;
3549 if (params
.length
>= 2) {
3550 col
= params
[1] - 1;
3557 } else if (row
>= this.rows
) {
3558 row
= this.rows
- 1;
3563 } else if (col
>= this.cols
) {
3564 col
= this.cols
- 1;
3573 * CSI Ps J Erase in Display (ED).
3574 * Ps = 0 -> Erase Below (default).
3575 * Ps = 1 -> Erase Above.
3576 * Ps = 2 -> Erase All.
3577 * Ps = 3 -> Erase Saved Lines (xterm).
3579 * Erase in Display (DECSED).
3580 * Ps = 0 -> Selective Erase Below (default).
3581 * Ps = 1 -> Selective Erase Above.
3582 * Ps = 2 -> Selective Erase All.
3584 Terminal
.prototype.eraseInDisplay = function(params
) {
3586 switch (params
[0]) {
3588 this.eraseRight(this.x
, this.y
);
3590 for (; j
< this.rows
; j
++) {
3595 this.eraseLeft(this.x
, this.y
);
3603 while (j
--) this.eraseLine(j
);
3613 * CSI Ps K Erase in Line (EL).
3614 * Ps = 0 -> Erase to Right (default).
3615 * Ps = 1 -> Erase to Left.
3616 * Ps = 2 -> Erase All.
3618 * Erase in Line (DECSEL).
3619 * Ps = 0 -> Selective Erase to Right (default).
3620 * Ps = 1 -> Selective Erase to Left.
3621 * Ps = 2 -> Selective Erase All.
3623 Terminal
.prototype.eraseInLine = function(params
) {
3624 switch (params
[0]) {
3626 this.eraseRight(this.x
, this.y
);
3629 this.eraseLeft(this.x
, this.y
);
3632 this.eraseLine(this.y
);
3639 * CSI Pm m Character Attributes (SGR).
3640 * Ps = 0 -> Normal (default).
3642 * Ps = 4 -> Underlined.
3643 * Ps = 5 -> Blink (appears as Bold).
3644 * Ps = 7 -> Inverse.
3645 * Ps = 8 -> Invisible, i.e., hidden (VT300).
3646 * Ps = 2 2 -> Normal (neither bold nor faint).
3647 * Ps = 2 4 -> Not underlined.
3648 * Ps = 2 5 -> Steady (not blinking).
3649 * Ps = 2 7 -> Positive (not inverse).
3650 * Ps = 2 8 -> Visible, i.e., not hidden (VT300).
3651 * Ps = 3 0 -> Set foreground color to Black.
3652 * Ps = 3 1 -> Set foreground color to Red.
3653 * Ps = 3 2 -> Set foreground color to Green.
3654 * Ps = 3 3 -> Set foreground color to Yellow.
3655 * Ps = 3 4 -> Set foreground color to Blue.
3656 * Ps = 3 5 -> Set foreground color to Magenta.
3657 * Ps = 3 6 -> Set foreground color to Cyan.
3658 * Ps = 3 7 -> Set foreground color to White.
3659 * Ps = 3 9 -> Set foreground color to default (original).
3660 * Ps = 4 0 -> Set background color to Black.
3661 * Ps = 4 1 -> Set background color to Red.
3662 * Ps = 4 2 -> Set background color to Green.
3663 * Ps = 4 3 -> Set background color to Yellow.
3664 * Ps = 4 4 -> Set background color to Blue.
3665 * Ps = 4 5 -> Set background color to Magenta.
3666 * Ps = 4 6 -> Set background color to Cyan.
3667 * Ps = 4 7 -> Set background color to White.
3668 * Ps = 4 9 -> Set background color to default (original).
3670 * If 16-color support is compiled, the following apply. Assume
3671 * that xterm's resources are set so that the ISO color codes are
3672 * the first 8 of a set of 16. Then the aixterm colors are the
3673 * bright versions of the ISO colors:
3674 * Ps = 9 0 -> Set foreground color to Black.
3675 * Ps = 9 1 -> Set foreground color to Red.
3676 * Ps = 9 2 -> Set foreground color to Green.
3677 * Ps = 9 3 -> Set foreground color to Yellow.
3678 * Ps = 9 4 -> Set foreground color to Blue.
3679 * Ps = 9 5 -> Set foreground color to Magenta.
3680 * Ps = 9 6 -> Set foreground color to Cyan.
3681 * Ps = 9 7 -> Set foreground color to White.
3682 * Ps = 1 0 0 -> Set background color to Black.
3683 * Ps = 1 0 1 -> Set background color to Red.
3684 * Ps = 1 0 2 -> Set background color to Green.
3685 * Ps = 1 0 3 -> Set background color to Yellow.
3686 * Ps = 1 0 4 -> Set background color to Blue.
3687 * Ps = 1 0 5 -> Set background color to Magenta.
3688 * Ps = 1 0 6 -> Set background color to Cyan.
3689 * Ps = 1 0 7 -> Set background color to White.
3691 * If xterm is compiled with the 16-color support disabled, it
3692 * supports the following, from rxvt:
3693 * Ps = 1 0 0 -> Set foreground and background color to
3696 * If 88- or 256-color support is compiled, the following apply.
3697 * Ps = 3 8 ; 5 ; Ps -> Set foreground color to the second
3699 * Ps = 4 8 ; 5 ; Ps -> Set background color to the second
3702 Terminal
.prototype.charAttributes = function(params
) {
3703 // Optimize a single SGR0.
3704 if (params
.length
=== 1 && params
[0] === 0) {
3705 this.curAttr
= this.defAttr
;
3709 var l
= params
.length
3711 , flags
= this.curAttr
>> 18
3712 , fg
= (this.curAttr
>> 9) & 0x1ff
3713 , bg
= this.curAttr
& 0x1ff
3716 for (; i
< l
; i
++) {
3718 if (p
>= 30 && p
<= 37) {
3721 } else if (p
>= 40 && p
<= 47) {
3724 } else if (p
>= 90 && p
<= 97) {
3728 } else if (p
>= 100 && p
<= 107) {
3732 } else if (p
=== 0) {
3734 flags
= this.defAttr
>> 18;
3735 fg
= (this.defAttr
>> 9) & 0x1ff;
3736 bg
= this.defAttr
& 0x1ff;
3740 } else if (p
=== 1) {
3743 } else if (p
=== 4) {
3746 } else if (p
=== 5) {
3749 } else if (p
=== 7) {
3750 // inverse and positive
3751 // test with: echo -e '\e[31m\e[42mhello\e[7mworld\e[27mhi\e[m'
3753 } else if (p
=== 8) {
3756 } else if (p
=== 22) {
3759 } else if (p
=== 24) {
3762 } else if (p
=== 25) {
3765 } else if (p
=== 27) {
3768 } else if (p
=== 28) {
3771 } else if (p
=== 39) {
3773 fg
= (this.defAttr
>> 9) & 0x1ff;
3774 } else if (p
=== 49) {
3776 bg
= this.defAttr
& 0x1ff;
3777 } else if (p
=== 38) {
3779 if (params
[i
+ 1] === 2) {
3783 params
[i
+ 1] & 0xff,
3784 params
[i
+ 2] & 0xff);
3785 if (fg
=== -1) fg
= 0x1ff;
3787 } else if (params
[i
+ 1] === 5) {
3789 p
= params
[i
] & 0xff;
3792 } else if (p
=== 48) {
3794 if (params
[i
+ 1] === 2) {
3798 params
[i
+ 1] & 0xff,
3799 params
[i
+ 2] & 0xff);
3800 if (bg
=== -1) bg
= 0x1ff;
3802 } else if (params
[i
+ 1] === 5) {
3804 p
= params
[i
] & 0xff;
3807 } else if (p
=== 100) {
3809 fg
= (this.defAttr
>> 9) & 0x1ff;
3810 bg
= this.defAttr
& 0x1ff;
3812 this.error('Unknown SGR attribute: %d.', p
);
3816 this.curAttr
= (flags
<< 18) | (fg
<< 9) | bg
;
3821 * CSI Ps n Device Status Report (DSR).
3822 * Ps = 5 -> Status Report. Result (``OK'') is
3824 * Ps = 6 -> Report Cursor Position (CPR) [row;column].
3828 * Device Status Report (DSR, DEC-specific).
3829 * Ps = 6 -> Report Cursor Position (CPR) [row;column] as CSI
3830 * ? r ; c R (assumes page is zero).
3831 * Ps = 1 5 -> Report Printer status as CSI ? 1 0 n (ready).
3832 * or CSI ? 1 1 n (not ready).
3833 * Ps = 2 5 -> Report UDK status as CSI ? 2 0 n (unlocked)
3834 * or CSI ? 2 1 n (locked).
3835 * Ps = 2 6 -> Report Keyboard status as
3836 * CSI ? 2 7 ; 1 ; 0 ; 0 n (North American).
3837 * The last two parameters apply to VT400 & up, and denote key-
3838 * board ready and LK01 respectively.
3839 * Ps = 5 3 -> Report Locator status as
3840 * CSI ? 5 3 n Locator available, if compiled-in, or
3841 * CSI ? 5 0 n No Locator, if not.
3843 Terminal
.prototype.deviceStatus = function(params
) {
3845 switch (params
[0]) {
3848 this.send('\x1b[0n');
3859 } else if (this.prefix
=== '?') {
3860 // modern xterm doesnt seem to
3861 // respond to any of these except ?6, 6, and 5
3862 switch (params
[0]) {
3873 // this.send('\x1b[?11n');
3876 // dont support user defined keys
3877 // this.send('\x1b[?21n');
3880 // north american keyboard
3881 // this.send('\x1b[?27;1;0;0n');
3884 // no dec locator/mouse
3885 // this.send('\x1b[?50n');
3898 * Insert Ps (Blank) Character(s) (default = 1) (ICH).
3900 Terminal
.prototype.insertChars = function(params
) {
3901 var param
, row
, j
, ch
;
3904 if (param
< 1) param
= 1;
3906 row
= this.y
+ this.ybase
;
3908 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3910 while (param
-- && j
< this.cols
) {
3911 this.lines
[row
].splice(j
++, 0, ch
);
3912 this.lines
[row
].pop();
3918 * Cursor Next Line Ps Times (default = 1) (CNL).
3919 * same as CSI Ps B ?
3921 Terminal
.prototype.cursorNextLine = function(params
) {
3922 var param
= params
[0];
3923 if (param
< 1) param
= 1;
3925 if (this.y
>= this.rows
) {
3926 this.y
= this.rows
- 1;
3934 * Cursor Preceding Line Ps Times (default = 1) (CNL).
3937 Terminal
.prototype.cursorPrecedingLine = function(params
) {
3938 var param
= params
[0];
3939 if (param
< 1) param
= 1;
3941 if (this.y
< 0) this.y
= 0;
3948 * Cursor Character Absolute [column] (default = [row,1]) (CHA).
3950 Terminal
.prototype.cursorCharAbsolute = function(params
) {
3951 var param
= params
[0];
3952 if (param
< 1) param
= 1;
3959 * Insert Ps Line(s) (default = 1) (IL).
3961 Terminal
.prototype.insertLines = function(params
) {
3965 if (param
< 1) param
= 1;
3966 row
= this.y
+ this.ybase
;
3968 j
= this.rows
- 1 - this.scrollBottom
;
3969 j
= this.rows
- 1 + this.ybase
- j
+ 1;
3972 // test: echo -e '\e[44m\e[1L\e[0m'
3973 // blankLine(true) - xterm/linux behavior
3974 this.lines
.splice(row
, 0, this.blankLine(true));
3975 this.lines
.splice(j
, 1);
3979 this.updateRange(this.y
);
3980 this.updateRange(this.scrollBottom
);
3986 * Delete Ps Line(s) (default = 1) (DL).
3988 Terminal
.prototype.deleteLines = function(params
) {
3992 if (param
< 1) param
= 1;
3993 row
= this.y
+ this.ybase
;
3995 j
= this.rows
- 1 - this.scrollBottom
;
3996 j
= this.rows
- 1 + this.ybase
- j
;
3999 // test: echo -e '\e[44m\e[1M\e[0m'
4000 // blankLine(true) - xterm/linux behavior
4001 this.lines
.splice(j
+ 1, 0, this.blankLine(true));
4002 this.lines
.splice(row
, 1);
4006 this.updateRange(this.y
);
4007 this.updateRange(this.scrollBottom
);
4013 * Delete Ps Character(s) (default = 1) (DCH).
4015 Terminal
.prototype.deleteChars = function(params
) {
4019 if (param
< 1) param
= 1;
4021 row
= this.y
+ this.ybase
;
4022 ch
= [this.eraseAttr(), ' ', 1]; // xterm
4025 this.lines
[row
].splice(this.x
, 1);
4026 this.lines
[row
].push(ch
);
4032 * Erase Ps Character(s) (default = 1) (ECH).
4034 Terminal
.prototype.eraseChars = function(params
) {
4035 var param
, row
, j
, ch
;
4038 if (param
< 1) param
= 1;
4040 row
= this.y
+ this.ybase
;
4042 ch
= [this.eraseAttr(), ' ', 1]; // xterm
4044 while (param
-- && j
< this.cols
) {
4045 this.lines
[row
][j
++] = ch
;
4050 * CSI Pm ` Character Position Absolute
4051 * [column] (default = [row,1]) (HPA).
4053 Terminal
.prototype.charPosAbsolute = function(params
) {
4054 var param
= params
[0];
4055 if (param
< 1) param
= 1;
4057 if (this.x
>= this.cols
) {
4058 this.x
= this.cols
- 1;
4065 * Horizontal Position Relative
4068 Terminal
.prototype.HPositionRelative = function(params
) {
4069 var param
= params
[0];
4070 if (param
< 1) param
= 1;
4072 if (this.x
>= this.cols
) {
4073 this.x
= this.cols
- 1;
4079 * CSI Ps c Send Device Attributes (Primary DA).
4080 * Ps = 0 or omitted -> request attributes from terminal. The
4081 * response depends on the decTerminalID resource setting.
4082 * -> CSI ? 1 ; 2 c (``VT100 with Advanced Video Option'')
4083 * -> CSI ? 1 ; 0 c (``VT101 with No Options'')
4084 * -> CSI ? 6 c (``VT102'')
4085 * -> CSI ? 6 0 ; 1 ; 2 ; 6 ; 8 ; 9 ; 1 5 ; c (``VT220'')
4086 * The VT100-style response parameters do not mean anything by
4087 * themselves. VT220 parameters do, telling the host what fea-
4088 * tures the terminal supports:
4089 * Ps = 1 -> 132-columns.
4090 * Ps = 2 -> Printer.
4091 * Ps = 6 -> Selective erase.
4092 * Ps = 8 -> User-defined keys.
4093 * Ps = 9 -> National replacement character sets.
4094 * Ps = 1 5 -> Technical characters.
4095 * Ps = 2 2 -> ANSI color, e.g., VT525.
4096 * Ps = 2 9 -> ANSI text locator (i.e., DEC Locator mode).
4098 * Send Device Attributes (Secondary DA).
4099 * Ps = 0 or omitted -> request the terminal's identification
4100 * code. The response depends on the decTerminalID resource set-
4101 * ting. It should apply only to VT220 and up, but xterm extends
4103 * -> CSI > Pp ; Pv ; Pc c
4104 * where Pp denotes the terminal type
4105 * Pp = 0 -> ``VT100''.
4106 * Pp = 1 -> ``VT220''.
4107 * and Pv is the firmware version (for xterm, this was originally
4108 * the XFree86 patch number, starting with 95). In a DEC termi-
4109 * nal, Pc indicates the ROM cartridge registration number and is
4112 * xterm/charproc.c - line 2012, for more information.
4113 * vim responds with ^[[?0c or ^[[?1c after the terminal's response (?)
4115 Terminal
.prototype.sendDeviceAttributes = function(params
) {
4116 if (params
[0] > 0) return;
4119 if (this.is('xterm')
4120 || this.is('rxvt-unicode')
4121 || this.is('screen')) {
4122 this.send('\x1b[?1;2c');
4123 } else if (this.is('linux')) {
4124 this.send('\x1b[?6c');
4126 } else if (this.prefix
=== '>') {
4128 // seem to spit this
4129 // out around ~370 times (?).
4130 if (this.is('xterm')) {
4131 this.send('\x1b[>0;276;0c');
4132 } else if (this.is('rxvt-unicode')) {
4133 this.send('\x1b[>85;95;0c');
4134 } else if (this.is('linux')) {
4135 // not supported by linux console.
4136 // linux console echoes parameters.
4137 this.send(params
[0] + 'c');
4138 } else if (this.is('screen')) {
4139 this.send('\x1b[>83;40003;0c');
4147 * Line Position Absolute [row] (default = [1,column]) (VPA).
4149 Terminal
.prototype.linePosAbsolute = function(params
) {
4150 var param
= params
[0];
4151 if (param
< 1) param
= 1;
4153 if (this.y
>= this.rows
) {
4154 this.y
= this.rows
- 1;
4160 * 145 65 e * VPR - Vertical Position Relative
4163 Terminal
.prototype.VPositionRelative = function(params
) {
4164 var param
= params
[0];
4165 if (param
< 1) param
= 1;
4167 if (this.y
>= this.rows
) {
4168 this.y
= this.rows
- 1;
4175 * Horizontal and Vertical Position [row;column] (default =
4178 Terminal
.prototype.HVPosition = function(params
) {
4179 if (params
[0] < 1) params
[0] = 1;
4180 if (params
[1] < 1) params
[1] = 1;
4182 this.y
= params
[0] - 1;
4183 if (this.y
>= this.rows
) {
4184 this.y
= this.rows
- 1;
4187 this.x
= params
[1] - 1;
4188 if (this.x
>= this.cols
) {
4189 this.x
= this.cols
- 1;
4195 * CSI Pm h Set Mode (SM).
4196 * Ps = 2 -> Keyboard Action Mode (AM).
4197 * Ps = 4 -> Insert Mode (IRM).
4198 * Ps = 1 2 -> Send/receive (SRM).
4199 * Ps = 2 0 -> Automatic Newline (LNM).
4201 * DEC Private Mode Set (DECSET).
4202 * Ps = 1 -> Application Cursor Keys (DECCKM).
4203 * Ps = 2 -> Designate USASCII for character sets G0-G3
4204 * (DECANM), and set VT100 mode.
4205 * Ps = 3 -> 132 Column Mode (DECCOLM).
4206 * Ps = 4 -> Smooth (Slow) Scroll (DECSCLM).
4207 * Ps = 5 -> Reverse Video (DECSCNM).
4208 * Ps = 6 -> Origin Mode (DECOM).
4209 * Ps = 7 -> Wraparound Mode (DECAWM).
4210 * Ps = 8 -> Auto-repeat Keys (DECARM).
4211 * Ps = 9 -> Send Mouse X & Y on button press. See the sec-
4212 * tion Mouse Tracking.
4213 * Ps = 1 0 -> Show toolbar (rxvt).
4214 * Ps = 1 2 -> Start Blinking Cursor (att610).
4215 * Ps = 1 8 -> Print form feed (DECPFF).
4216 * Ps = 1 9 -> Set print extent to full screen (DECPEX).
4217 * Ps = 2 5 -> Show Cursor (DECTCEM).
4218 * Ps = 3 0 -> Show scrollbar (rxvt).
4219 * Ps = 3 5 -> Enable font-shifting functions (rxvt).
4220 * Ps = 3 8 -> Enter Tektronix Mode (DECTEK).
4221 * Ps = 4 0 -> Allow 80 -> 132 Mode.
4222 * Ps = 4 1 -> more(1) fix (see curses resource).
4223 * Ps = 4 2 -> Enable Nation Replacement Character sets (DECN-
4225 * Ps = 4 4 -> Turn On Margin Bell.
4226 * Ps = 4 5 -> Reverse-wraparound Mode.
4227 * Ps = 4 6 -> Start Logging. This is normally disabled by a
4228 * compile-time option.
4229 * Ps = 4 7 -> Use Alternate Screen Buffer. (This may be dis-
4230 * abled by the titeInhibit resource).
4231 * Ps = 6 6 -> Application keypad (DECNKM).
4232 * Ps = 6 7 -> Backarrow key sends backspace (DECBKM).
4233 * Ps = 1 0 0 0 -> Send Mouse X & Y on button press and
4234 * release. See the section Mouse Tracking.
4235 * Ps = 1 0 0 1 -> Use Hilite Mouse Tracking.
4236 * Ps = 1 0 0 2 -> Use Cell Motion Mouse Tracking.
4237 * Ps = 1 0 0 3 -> Use All Motion Mouse Tracking.
4238 * Ps = 1 0 0 4 -> Send FocusIn/FocusOut events.
4239 * Ps = 1 0 0 5 -> Enable Extended Mouse Mode.
4240 * Ps = 1 0 1 0 -> Scroll to bottom on tty output (rxvt).
4241 * Ps = 1 0 1 1 -> Scroll to bottom on key press (rxvt).
4242 * Ps = 1 0 3 4 -> Interpret "meta" key, sets eighth bit.
4243 * (enables the eightBitInput resource).
4244 * Ps = 1 0 3 5 -> Enable special modifiers for Alt and Num-
4245 * Lock keys. (This enables the numLock resource).
4246 * Ps = 1 0 3 6 -> Send ESC when Meta modifies a key. (This
4247 * enables the metaSendsEscape resource).
4248 * Ps = 1 0 3 7 -> Send DEL from the editing-keypad Delete
4250 * Ps = 1 0 3 9 -> Send ESC when Alt modifies a key. (This
4251 * enables the altSendsEscape resource).
4252 * Ps = 1 0 4 0 -> Keep selection even if not highlighted.
4253 * (This enables the keepSelection resource).
4254 * Ps = 1 0 4 1 -> Use the CLIPBOARD selection. (This enables
4255 * the selectToClipboard resource).
4256 * Ps = 1 0 4 2 -> Enable Urgency window manager hint when
4257 * Control-G is received. (This enables the bellIsUrgent
4259 * Ps = 1 0 4 3 -> Enable raising of the window when Control-G
4260 * is received. (enables the popOnBell resource).
4261 * Ps = 1 0 4 7 -> Use Alternate Screen Buffer. (This may be
4262 * disabled by the titeInhibit resource).
4263 * Ps = 1 0 4 8 -> Save cursor as in DECSC. (This may be dis-
4264 * abled by the titeInhibit resource).
4265 * Ps = 1 0 4 9 -> Save cursor as in DECSC and use Alternate
4266 * Screen Buffer, clearing it first. (This may be disabled by
4267 * the titeInhibit resource). This combines the effects of the 1
4268 * 0 4 7 and 1 0 4 8 modes. Use this with terminfo-based
4269 * applications rather than the 4 7 mode.
4270 * Ps = 1 0 5 0 -> Set terminfo/termcap function-key mode.
4271 * Ps = 1 0 5 1 -> Set Sun function-key mode.
4272 * Ps = 1 0 5 2 -> Set HP function-key mode.
4273 * Ps = 1 0 5 3 -> Set SCO function-key mode.
4274 * Ps = 1 0 6 0 -> Set legacy keyboard emulation (X11R6).
4275 * Ps = 1 0 6 1 -> Set VT220 keyboard emulation.
4276 * Ps = 2 0 0 4 -> Set bracketed paste mode.
4278 * http: *vt100.net/docs/vt220-rm/chapter4.html
4280 Terminal
.prototype.setMode = function(params
) {
4281 if (typeof params
=== 'object') {
4282 var l
= params
.length
4285 for (; i
< l
; i
++) {
4286 this.setMode(params
[i
]);
4295 this.insertMode
= true;
4298 //this.convertEol = true;
4301 } else if (this.prefix
=== '?') {
4304 this.applicationCursor
= true;
4307 this.setgCharset(0, Terminal
.charsets
.US
);
4308 this.setgCharset(1, Terminal
.charsets
.US
);
4309 this.setgCharset(2, Terminal
.charsets
.US
);
4310 this.setgCharset(3, Terminal
.charsets
.US
);
4311 // set VT100 mode here
4313 case 3: // 132 col mode
4314 this.savedCols
= this.cols
;
4315 this.resize(132, this.rows
);
4318 this.originMode
= true;
4321 this.wraparoundMode
= true;
4324 // this.cursorBlink = true;
4327 this.log('Serial port requested application keypad.');
4328 this.applicationKeypad
= true;
4329 this.viewport
.setApplicationMode(true);
4331 case 9: // X10 Mouse
4332 // no release, no motion, no wheel, no modifiers.
4333 case 1000: // vt200 mouse
4335 // no modifiers, except control on the wheel.
4336 case 1002: // button event mouse
4337 case 1003: // any event mouse
4338 // any event - sends motion events,
4339 // even if there is no button held down.
4340 this.x10Mouse
= params
=== 9;
4341 this.vt200Mouse
= params
=== 1000;
4342 this.normalMouse
= params
> 1000;
4343 this.mouseEvents
= true;
4344 this.element
.style
.cursor
= 'default';
4345 this.log('Binding to mouse events.');
4347 case 1004: // send focusin/focusout events
4350 this.sendFocus
= true;
4352 case 1005: // utf8 ext mode mouse
4353 this.utfMouse
= true;
4354 // for wide terminals
4355 // simply encodes large values as utf8 characters
4357 case 1006: // sgr ext mode mouse
4358 this.sgrMouse
= true;
4359 // for wide terminals
4360 // does not add 32 to fields
4361 // press: ^[[<b;x;yM
4362 // release: ^[[<b;x;ym
4364 case 1015: // urxvt ext mode mouse
4365 this.urxvtMouse
= true;
4366 // for wide terminals
4367 // numbers for fields
4369 // motion: ^[[b;x;yT
4371 case 25: // show cursor
4372 this.cursorHidden
= false;
4374 case 1049: // alt screen buffer cursor
4375 //this.saveCursor();
4377 case 47: // alt screen buffer
4378 case 1047: // alt screen buffer
4386 scrollTop
: this.scrollTop
,
4387 scrollBottom
: this.scrollBottom
,
4389 // XXX save charset(s) here?
4390 // charset: this.charset,
4391 // glevel: this.glevel,
4392 // charsets: this.charsets
4395 this.normal
= normal
;
4404 * CSI Pm l Reset Mode (RM).
4405 * Ps = 2 -> Keyboard Action Mode (AM).
4406 * Ps = 4 -> Replace Mode (IRM).
4407 * Ps = 1 2 -> Send/receive (SRM).
4408 * Ps = 2 0 -> Normal Linefeed (LNM).
4410 * DEC Private Mode Reset (DECRST).
4411 * Ps = 1 -> Normal Cursor Keys (DECCKM).
4412 * Ps = 2 -> Designate VT52 mode (DECANM).
4413 * Ps = 3 -> 80 Column Mode (DECCOLM).
4414 * Ps = 4 -> Jump (Fast) Scroll (DECSCLM).
4415 * Ps = 5 -> Normal Video (DECSCNM).
4416 * Ps = 6 -> Normal Cursor Mode (DECOM).
4417 * Ps = 7 -> No Wraparound Mode (DECAWM).
4418 * Ps = 8 -> No Auto-repeat Keys (DECARM).
4419 * Ps = 9 -> Don't send Mouse X & Y on button press.
4420 * Ps = 1 0 -> Hide toolbar (rxvt).
4421 * Ps = 1 2 -> Stop Blinking Cursor (att610).
4422 * Ps = 1 8 -> Don't print form feed (DECPFF).
4423 * Ps = 1 9 -> Limit print to scrolling region (DECPEX).
4424 * Ps = 2 5 -> Hide Cursor (DECTCEM).
4425 * Ps = 3 0 -> Don't show scrollbar (rxvt).
4426 * Ps = 3 5 -> Disable font-shifting functions (rxvt).
4427 * Ps = 4 0 -> Disallow 80 -> 132 Mode.
4428 * Ps = 4 1 -> No more(1) fix (see curses resource).
4429 * Ps = 4 2 -> Disable Nation Replacement Character sets (DEC-
4431 * Ps = 4 4 -> Turn Off Margin Bell.
4432 * Ps = 4 5 -> No Reverse-wraparound Mode.
4433 * Ps = 4 6 -> Stop Logging. (This is normally disabled by a
4434 * compile-time option).
4435 * Ps = 4 7 -> Use Normal Screen Buffer.
4436 * Ps = 6 6 -> Numeric keypad (DECNKM).
4437 * Ps = 6 7 -> Backarrow key sends delete (DECBKM).
4438 * Ps = 1 0 0 0 -> Don't send Mouse X & Y on button press and
4439 * release. See the section Mouse Tracking.
4440 * Ps = 1 0 0 1 -> Don't use Hilite Mouse Tracking.
4441 * Ps = 1 0 0 2 -> Don't use Cell Motion Mouse Tracking.
4442 * Ps = 1 0 0 3 -> Don't use All Motion Mouse Tracking.
4443 * Ps = 1 0 0 4 -> Don't send FocusIn/FocusOut events.
4444 * Ps = 1 0 0 5 -> Disable Extended Mouse Mode.
4445 * Ps = 1 0 1 0 -> Don't scroll to bottom on tty output
4447 * Ps = 1 0 1 1 -> Don't scroll to bottom on key press (rxvt).
4448 * Ps = 1 0 3 4 -> Don't interpret "meta" key. (This disables
4449 * the eightBitInput resource).
4450 * Ps = 1 0 3 5 -> Disable special modifiers for Alt and Num-
4451 * Lock keys. (This disables the numLock resource).
4452 * Ps = 1 0 3 6 -> Don't send ESC when Meta modifies a key.
4453 * (This disables the metaSendsEscape resource).
4454 * Ps = 1 0 3 7 -> Send VT220 Remove from the editing-keypad
4456 * Ps = 1 0 3 9 -> Don't send ESC when Alt modifies a key.
4457 * (This disables the altSendsEscape resource).
4458 * Ps = 1 0 4 0 -> Do not keep selection when not highlighted.
4459 * (This disables the keepSelection resource).
4460 * Ps = 1 0 4 1 -> Use the PRIMARY selection. (This disables
4461 * the selectToClipboard resource).
4462 * Ps = 1 0 4 2 -> Disable Urgency window manager hint when
4463 * Control-G is received. (This disables the bellIsUrgent
4465 * Ps = 1 0 4 3 -> Disable raising of the window when Control-
4466 * G is received. (This disables the popOnBell resource).
4467 * Ps = 1 0 4 7 -> Use Normal Screen Buffer, clearing screen
4468 * first if in the Alternate Screen. (This may be disabled by
4469 * the titeInhibit resource).
4470 * Ps = 1 0 4 8 -> Restore cursor as in DECRC. (This may be
4471 * disabled by the titeInhibit resource).
4472 * Ps = 1 0 4 9 -> Use Normal Screen Buffer and restore cursor
4473 * as in DECRC. (This may be disabled by the titeInhibit
4474 * resource). This combines the effects of the 1 0 4 7 and 1 0
4475 * 4 8 modes. Use this with terminfo-based applications rather
4476 * than the 4 7 mode.
4477 * Ps = 1 0 5 0 -> Reset terminfo/termcap function-key mode.
4478 * Ps = 1 0 5 1 -> Reset Sun function-key mode.
4479 * Ps = 1 0 5 2 -> Reset HP function-key mode.
4480 * Ps = 1 0 5 3 -> Reset SCO function-key mode.
4481 * Ps = 1 0 6 0 -> Reset legacy keyboard emulation (X11R6).
4482 * Ps = 1 0 6 1 -> Reset keyboard emulation to Sun/PC style.
4483 * Ps = 2 0 0 4 -> Reset bracketed paste mode.
4485 Terminal
.prototype.resetMode = function(params
) {
4486 if (typeof params
=== 'object') {
4487 var l
= params
.length
4490 for (; i
< l
; i
++) {
4491 this.resetMode(params
[i
]);
4500 this.insertMode
= false;
4503 //this.convertEol = false;
4506 } else if (this.prefix
=== '?') {
4509 this.applicationCursor
= false;
4512 if (this.cols
=== 132 && this.savedCols
) {
4513 this.resize(this.savedCols
, this.rows
);
4515 delete this.savedCols
;
4518 this.originMode
= false;
4521 this.wraparoundMode
= false;
4524 // this.cursorBlink = false;
4527 this.log('Switching back to normal keypad.');
4528 this.viewport
.setApplicationMode(false);
4529 this.applicationKeypad
= false;
4531 case 9: // X10 Mouse
4532 case 1000: // vt200 mouse
4533 case 1002: // button event mouse
4534 case 1003: // any event mouse
4535 this.x10Mouse
= false;
4536 this.vt200Mouse
= false;
4537 this.normalMouse
= false;
4538 this.mouseEvents
= false;
4539 this.element
.style
.cursor
= '';
4541 case 1004: // send focusin/focusout events
4542 this.sendFocus
= false;
4544 case 1005: // utf8 ext mode mouse
4545 this.utfMouse
= false;
4547 case 1006: // sgr ext mode mouse
4548 this.sgrMouse
= false;
4550 case 1015: // urxvt ext mode mouse
4551 this.urxvtMouse
= false;
4553 case 25: // hide cursor
4554 this.cursorHidden
= true;
4556 case 1049: // alt screen buffer cursor
4558 case 47: // normal screen buffer
4559 case 1047: // normal screen buffer - clearing it first
4561 this.lines
= this.normal
.lines
;
4562 this.ybase
= this.normal
.ybase
;
4563 this.ydisp
= this.normal
.ydisp
;
4564 this.x
= this.normal
.x
;
4565 this.y
= this.normal
.y
;
4566 this.scrollTop
= this.normal
.scrollTop
;
4567 this.scrollBottom
= this.normal
.scrollBottom
;
4568 this.tabs
= this.normal
.tabs
;
4570 // if (params === 1049) {
4571 // this.x = this.savedX;
4572 // this.y = this.savedY;
4574 this.refresh(0, this.rows
- 1);
4585 * Set Scrolling Region [top;bottom] (default = full size of win-
4589 Terminal
.prototype.setScrollRegion = function(params
) {
4590 if (this.prefix
) return;
4591 this.scrollTop
= (params
[0] || 1) - 1;
4592 this.scrollBottom
= (params
[1] || this.rows
) - 1;
4600 * Save cursor (ANSI.SYS).
4602 Terminal
.prototype.saveCursor = function(params
) {
4603 this.savedX
= this.x
;
4604 this.savedY
= this.y
;
4610 * Restore cursor (ANSI.SYS).
4612 Terminal
.prototype.restoreCursor = function(params
) {
4613 this.x
= this.savedX
|| 0;
4614 this.y
= this.savedY
|| 0;
4624 * Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
4626 Terminal
.prototype.cursorForwardTab = function(params
) {
4627 var param
= params
[0] || 1;
4629 this.x
= this.nextStop();
4635 * CSI Ps S Scroll up Ps lines (default = 1) (SU).
4637 Terminal
.prototype.scrollUp = function(params
) {
4638 var param
= params
[0] || 1;
4640 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
4641 this.lines
.splice(this.ybase
+ this.scrollBottom
, 0, this.blankLine());
4644 this.updateRange(this.scrollTop
);
4645 this.updateRange(this.scrollBottom
);
4650 * CSI Ps T Scroll down Ps lines (default = 1) (SD).
4652 Terminal
.prototype.scrollDown = function(params
) {
4653 var param
= params
[0] || 1;
4655 this.lines
.splice(this.ybase
+ this.scrollBottom
, 1);
4656 this.lines
.splice(this.ybase
+ this.scrollTop
, 0, this.blankLine());
4659 this.updateRange(this.scrollTop
);
4660 this.updateRange(this.scrollBottom
);
4665 * CSI Ps ; Ps ; Ps ; Ps ; Ps T
4666 * Initiate highlight mouse tracking. Parameters are
4667 * [func;startx;starty;firstrow;lastrow]. See the section Mouse
4670 Terminal
.prototype.initMouseTracking = function(params
) {
4671 // Relevant: DECSET 1001
4677 * Reset one or more features of the title modes to the default
4678 * value. Normally, "reset" disables the feature. It is possi-
4679 * ble to disable the ability to reset features by compiling a
4680 * different default for the title modes into xterm.
4681 * Ps = 0 -> Do not set window/icon labels using hexadecimal.
4682 * Ps = 1 -> Do not query window/icon labels using hexadeci-
4684 * Ps = 2 -> Do not set window/icon labels using UTF-8.
4685 * Ps = 3 -> Do not query window/icon labels using UTF-8.
4686 * (See discussion of "Title Modes").
4688 Terminal
.prototype.resetTitleModes = function(params
) {
4694 * CSI Ps Z Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
4696 Terminal
.prototype.cursorBackwardTab = function(params
) {
4697 var param
= params
[0] || 1;
4699 this.x
= this.prevStop();
4705 * CSI Ps b Repeat the preceding graphic character Ps times (REP).
4707 Terminal
.prototype.repeatPrecedingCharacter = function(params
) {
4708 var param
= params
[0] || 1
4709 , line
= this.lines
[this.ybase
+ this.y
]
4710 , ch
= line
[this.x
- 1] || [this.defAttr
, ' ', 1];
4712 while (param
--) line
[this.x
++] = ch
;
4717 * CSI Ps g Tab Clear (TBC).
4718 * Ps = 0 -> Clear Current Column (default).
4719 * Ps = 3 -> Clear All.
4721 * Ps = 2 -> Clear Stops on Line.
4722 * http://vt100.net/annarbor/aaa-ug/section6.html
4724 Terminal
.prototype.tabClear = function(params
) {
4725 var param
= params
[0];
4727 delete this.tabs
[this.x
];
4728 } else if (param
=== 3) {
4735 * CSI Pm i Media Copy (MC).
4736 * Ps = 0 -> Print screen (default).
4737 * Ps = 4 -> Turn off printer controller mode.
4738 * Ps = 5 -> Turn on printer controller mode.
4740 * Media Copy (MC, DEC-specific).
4741 * Ps = 1 -> Print line containing cursor.
4742 * Ps = 4 -> Turn off autoprint mode.
4743 * Ps = 5 -> Turn on autoprint mode.
4744 * Ps = 1 0 -> Print composed display, ignores DECPEX.
4745 * Ps = 1 1 -> Print all pages.
4747 Terminal
.prototype.mediaCopy = function(params
) {
4754 * Set or reset resource-values used by xterm to decide whether
4755 * to construct escape sequences holding information about the
4756 * modifiers pressed with a given key. The first parameter iden-
4757 * tifies the resource to set/reset. The second parameter is the
4758 * value to assign to the resource. If the second parameter is
4759 * omitted, the resource is reset to its initial value.
4760 * Ps = 1 -> modifyCursorKeys.
4761 * Ps = 2 -> modifyFunctionKeys.
4762 * Ps = 4 -> modifyOtherKeys.
4763 * If no parameters are given, all resources are reset to their
4766 Terminal
.prototype.setResources = function(params
) {
4773 * Disable modifiers which may be enabled via the CSI > Ps; Ps m
4774 * sequence. This corresponds to a resource value of "-1", which
4775 * cannot be set with the other sequence. The parameter identi-
4776 * fies the resource to be disabled:
4777 * Ps = 1 -> modifyCursorKeys.
4778 * Ps = 2 -> modifyFunctionKeys.
4779 * Ps = 4 -> modifyOtherKeys.
4780 * If the parameter is omitted, modifyFunctionKeys is disabled.
4781 * When modifyFunctionKeys is disabled, xterm uses the modifier
4782 * keys to make an extended sequence of functions rather than
4783 * adding a parameter to each function key to denote the modi-
4786 Terminal
.prototype.disableModifiers = function(params
) {
4793 * Set resource value pointerMode. This is used by xterm to
4794 * decide whether to hide the pointer cursor as the user types.
4795 * Valid values for the parameter:
4796 * Ps = 0 -> never hide the pointer.
4797 * Ps = 1 -> hide if the mouse tracking mode is not enabled.
4798 * Ps = 2 -> always hide the pointer. If no parameter is
4799 * given, xterm uses the default, which is 1 .
4801 Terminal
.prototype.setPointerMode = function(params
) {
4807 * CSI ! p Soft terminal reset (DECSTR).
4808 * http://vt100.net/docs/vt220-rm/table4-10.html
4810 Terminal
.prototype.softReset = function(params
) {
4811 this.cursorHidden
= false;
4812 this.insertMode
= false;
4813 this.originMode
= false;
4814 this.wraparoundMode
= false; // autowrap
4815 this.applicationKeypad
= false; // ?
4816 this.applicationCursor
= false;
4818 this.scrollBottom
= this.rows
- 1;
4819 this.curAttr
= this.defAttr
;
4820 this.x
= this.y
= 0; // ?
4821 this.charset
= null;
4822 this.glevel
= 0; // ??
4823 this.charsets
= [null]; // ??
4829 * Request ANSI mode (DECRQM). For VT300 and up, reply is
4831 * where Ps is the mode number as in RM, and Pm is the mode
4833 * 0 - not recognized
4836 * 3 - permanently set
4837 * 4 - permanently reset
4839 Terminal
.prototype.requestAnsiMode = function(params
) {
4846 * Request DEC private mode (DECRQM). For VT300 and up, reply is
4848 * where Ps is the mode number as in DECSET, Pm is the mode value
4849 * as in the ANSI DECRQM.
4851 Terminal
.prototype.requestPrivateMode = function(params
) {
4858 * Set conformance level (DECSCL). Valid values for the first
4860 * Ps = 6 1 -> VT100.
4861 * Ps = 6 2 -> VT200.
4862 * Ps = 6 3 -> VT300.
4863 * Valid values for the second parameter:
4864 * Ps = 0 -> 8-bit controls.
4865 * Ps = 1 -> 7-bit controls (always set for VT100).
4866 * Ps = 2 -> 8-bit controls.
4868 Terminal
.prototype.setConformanceLevel = function(params
) {
4874 * CSI Ps q Load LEDs (DECLL).
4875 * Ps = 0 -> Clear all LEDS (default).
4876 * Ps = 1 -> Light Num Lock.
4877 * Ps = 2 -> Light Caps Lock.
4878 * Ps = 3 -> Light Scroll Lock.
4879 * Ps = 2 1 -> Extinguish Num Lock.
4880 * Ps = 2 2 -> Extinguish Caps Lock.
4881 * Ps = 2 3 -> Extinguish Scroll Lock.
4883 Terminal
.prototype.loadLEDs = function(params
) {
4890 * Set cursor style (DECSCUSR, VT520).
4891 * Ps = 0 -> blinking block.
4892 * Ps = 1 -> blinking block (default).
4893 * Ps = 2 -> steady block.
4894 * Ps = 3 -> blinking underline.
4895 * Ps = 4 -> steady underline.
4897 Terminal
.prototype.setCursorStyle = function(params
) {
4904 * Select character protection attribute (DECSCA). Valid values
4905 * for the parameter:
4906 * Ps = 0 -> DECSED and DECSEL can erase (default).
4907 * Ps = 1 -> DECSED and DECSEL cannot erase.
4908 * Ps = 2 -> DECSED and DECSEL can erase.
4910 Terminal
.prototype.setCharProtectionAttr = function(params
) {
4917 * Restore DEC Private Mode Values. The value of Ps previously
4918 * saved is restored. Ps values are the same as for DECSET.
4920 Terminal
.prototype.restorePrivateValues = function(params
) {
4926 * CSI Pt; Pl; Pb; Pr; Ps$ r
4927 * Change Attributes in Rectangular Area (DECCARA), VT400 and up.
4928 * Pt; Pl; Pb; Pr denotes the rectangle.
4929 * Ps denotes the SGR attributes to change: 0, 1, 4, 5, 7.
4930 * NOTE: xterm doesn't enable this code by default.
4932 Terminal
.prototype.setAttrInRectangle = function(params
) {
4942 for (; t
< b
+ 1; t
++) {
4943 line
= this.lines
[this.ybase
+ t
];
4944 for (i
= l
; i
< r
; i
++) {
4945 line
[i
] = [attr
, line
[i
][1]];
4950 this.updateRange(params
[0]);
4951 this.updateRange(params
[2]);
4956 * CSI Pc; Pt; Pl; Pb; Pr$ x
4957 * Fill Rectangular Area (DECFRA), VT420 and up.
4958 * Pc is the character to use.
4959 * Pt; Pl; Pb; Pr denotes the rectangle.
4960 * NOTE: xterm doesn't enable this code by default.
4962 Terminal
.prototype.fillRectangle = function(params
) {
4972 for (; t
< b
+ 1; t
++) {
4973 line
= this.lines
[this.ybase
+ t
];
4974 for (i
= l
; i
< r
; i
++) {
4975 line
[i
] = [line
[i
][0], String
.fromCharCode(ch
)];
4980 this.updateRange(params
[1]);
4981 this.updateRange(params
[3]);
4987 * Enable Locator Reporting (DECELR).
4988 * Valid values for the first parameter:
4989 * Ps = 0 -> Locator disabled (default).
4990 * Ps = 1 -> Locator enabled.
4991 * Ps = 2 -> Locator enabled for one report, then disabled.
4992 * The second parameter specifies the coordinate unit for locator
4994 * Valid values for the second parameter:
4995 * Pu = 0 <- or omitted -> default to character cells.
4996 * Pu = 1 <- device physical pixels.
4997 * Pu = 2 <- character cells.
4999 Terminal
.prototype.enableLocatorReporting = function(params
) {
5000 var val
= params
[0] > 0;
5001 //this.mouseEvents = val;
5002 //this.decLocator = val;
5007 * CSI Pt; Pl; Pb; Pr$ z
5008 * Erase Rectangular Area (DECERA), VT400 and up.
5009 * Pt; Pl; Pb; Pr denotes the rectangle.
5010 * NOTE: xterm doesn't enable this code by default.
5012 Terminal
.prototype.eraseRectangle = function(params
) {
5022 ch
= [this.eraseAttr(), ' ', 1]; // xterm?
5024 for (; t
< b
+ 1; t
++) {
5025 line
= this.lines
[this.ybase
+ t
];
5026 for (i
= l
; i
< r
; i
++) {
5032 this.updateRange(params
[0]);
5033 this.updateRange(params
[2]);
5039 * Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
5040 * NOTE: xterm doesn't enable this code by default.
5042 Terminal
.prototype.insertColumns = function() {
5043 var param
= params
[0]
5044 , l
= this.ybase
+ this.rows
5045 , ch
= [this.eraseAttr(), ' ', 1] // xterm?
5049 for (i
= this.ybase
; i
< l
; i
++) {
5050 this.lines
[i
].splice(this.x
+ 1, 0, ch
);
5051 this.lines
[i
].pop();
5061 * Delete P s Column(s) (default = 1) (DECDC), VT420 and up
5062 * NOTE: xterm doesn't enable this code by default.
5064 Terminal
.prototype.deleteColumns = function() {
5065 var param
= params
[0]
5066 , l
= this.ybase
+ this.rows
5067 , ch
= [this.eraseAttr(), ' ', 1] // xterm?
5071 for (i
= this.ybase
; i
< l
; i
++) {
5072 this.lines
[i
].splice(this.x
, 1);
5073 this.lines
[i
].push(ch
);
5084 Terminal
.charsets
= {};
5086 // DEC Special Character and Line Drawing Set.
5087 // http://vt100.net/docs/vt102-ug/table5-13.html
5088 // A lot of curses apps use this if they see TERM=xterm.
5089 // testing: echo -e '\e(0a\e(B'
5090 // The xterm output sometimes seems to conflict with the
5091 // reference above. xterm seems in line with the reference
5092 // when running vttest however.
5093 // The table below now uses xterm's output from vttest.
5094 Terminal
.charsets
.SCLD
= { // (0
5095 '`': '\u25c6', // '◆'
5096 'a': '\u2592', // '▒'
5097 'b': '\u0009', // '\t'
5098 'c': '\u000c', // '\f'
5099 'd': '\u000d', // '\r'
5100 'e': '\u000a', // '\n'
5101 'f': '\u00b0', // '°'
5102 'g': '\u00b1', // '±'
5103 'h': '\u2424', // '\u2424' (NL)
5104 'i': '\u000b', // '\v'
5105 'j': '\u2518', // '┘'
5106 'k': '\u2510', // '┐'
5107 'l': '\u250c', // '┌'
5108 'm': '\u2514', // '└'
5109 'n': '\u253c', // '┼'
5110 'o': '\u23ba', // '⎺'
5111 'p': '\u23bb', // '⎻'
5112 'q': '\u2500', // '─'
5113 'r': '\u23bc', // '⎼'
5114 's': '\u23bd', // '⎽'
5115 't': '\u251c', // '├'
5116 'u': '\u2524', // '┤'
5117 'v': '\u2534', // '┴'
5118 'w': '\u252c', // '┬'
5119 'x': '\u2502', // '│'
5120 'y': '\u2264', // '≤'
5121 'z': '\u2265', // '≥'
5122 '{': '\u03c0', // 'π'
5123 '|': '\u2260', // '≠'
5124 '}': '\u00a3', // '£'
5125 '~': '\u00b7' // '·'
5128 Terminal
.charsets
.UK
= null; // (A
5129 Terminal
.charsets
.US
= null; // (B (USASCII)
5130 Terminal
.charsets
.Dutch
= null; // (4
5131 Terminal
.charsets
.Finnish
= null; // (C or (5
5132 Terminal
.charsets
.French
= null; // (R
5133 Terminal
.charsets
.FrenchCanadian
= null; // (Q
5134 Terminal
.charsets
.German
= null; // (K
5135 Terminal
.charsets
.Italian
= null; // (Y
5136 Terminal
.charsets
.NorwegianDanish
= null; // (E or (6
5137 Terminal
.charsets
.Spanish
= null; // (Z
5138 Terminal
.charsets
.Swedish
= null; // (H or (7
5139 Terminal
.charsets
.Swiss
= null; // (=
5140 Terminal
.charsets
.ISOLatin
= null; // /A
5146 function contains(el
, arr
) {
5147 for (var i
= 0; i
< arr
.length
; i
+= 1) {
5148 if (el
=== arr
[i
]) {
5155 function on(el
, type
, handler
, capture
) {
5156 if (!Array
.isArray(el
)) {
5159 el
.forEach(function (element
) {
5160 element
.addEventListener(type
, handler
, capture
|| false);
5164 function off(el
, type
, handler
, capture
) {
5165 el
.removeEventListener(type
, handler
, capture
|| false);
5168 function cancel(ev
, force
) {
5169 if (!this.cancelEvents
&& !force
) {
5172 ev
.preventDefault();
5173 ev
.stopPropagation();
5177 function inherits(child
, parent
) {
5179 this.constructor = child
;
5181 f
.prototype = parent
.prototype;
5182 child
.prototype = new f
;
5185 // if bold is broken, we can't
5186 // use it in the terminal.
5187 function isBoldBroken(document
) {
5188 var body
= document
.getElementsByTagName('body')[0];
5189 var el
= document
.createElement('span');
5190 el
.innerHTML
= 'hello world';
5191 body
.appendChild(el
);
5192 var w1
= el
.scrollWidth
;
5193 el
.style
.fontWeight
= 'bold';
5194 var w2
= el
.scrollWidth
;
5195 body
.removeChild(el
);
5199 var String
= this.String
;
5200 var setTimeout
= this.setTimeout
;
5201 var setInterval
= this.setInterval
;
5203 function indexOf(obj
, el
) {
5206 if (obj
[i
] === el
) return i
;
5211 function isThirdLevelShift(term
, ev
) {
5213 (term
.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) ||
5214 (term
.isMSWindows
&& ev
.altKey
&& ev
.ctrlKey
&& !ev
.metaKey
);
5216 if (ev
.type
== 'keypress') {
5217 return thirdLevelKey
;
5220 // Don't invoke for arrows, pageDown, home, backspace, etc. (on non-keypress events)
5221 return thirdLevelKey
&& (!ev
.keyCode
|| ev
.keyCode
> 47);
5224 function matchColor(r1
, g1
, b1
) {
5225 var hash
= (r1
<< 16) | (g1
<< 8) | b1
;
5227 if (matchColor
._cache
[hash
] != null) {
5228 return matchColor
._cache
[hash
];
5231 var ldiff
= Infinity
5240 for (; i
< Terminal
.vcolors
.length
; i
++) {
5241 c
= Terminal
.vcolors
[i
];
5246 diff
= matchColor
.distance(r1
, g1
, b1
, r2
, g2
, b2
);
5259 return matchColor
._cache
[hash
] = li
;
5262 matchColor
._cache
= {};
5264 // http://stackoverflow.com/questions/1633828
5265 matchColor
.distance = function(r1
, g1
, b1
, r2
, g2
, b2
) {
5266 return Math
.pow(30 * (r1
- r2
), 2)
5267 + Math
.pow(59 * (g1
- g2
), 2)
5268 + Math
.pow(11 * (b1
- b2
), 2);
5271 function each(obj
, iter
, con
) {
5272 if (obj
.forEach
) return obj
.forEach(iter
, con
);
5273 for (var i
= 0; i
< obj
.length
; i
++) {
5274 iter
.call(con
, obj
[i
], i
, obj
);
5278 function keys(obj
) {
5279 if (Object
.keys
) return Object
.keys(obj
);
5282 if (Object
.prototype.hasOwnProperty
.call(obj
, key
)) {
5289 var wcwidth
= (function(opts
) {
5290 // extracted from https://www.cl.cam.ac.uk/%7Emgk25/ucs/wcwidth.c
5291 // combining characters
5293 [0x0300, 0x036F], [0x0483, 0x0486], [0x0488, 0x0489],
5294 [0x0591, 0x05BD], [0x05BF, 0x05BF], [0x05C1, 0x05C2],
5295 [0x05C4, 0x05C5], [0x05C7, 0x05C7], [0x0600, 0x0603],
5296 [0x0610, 0x0615], [0x064B, 0x065E], [0x0670, 0x0670],
5297 [0x06D6, 0x06E4], [0x06E7, 0x06E8], [0x06EA, 0x06ED],
5298 [0x070F, 0x070F], [0x0711, 0x0711], [0x0730, 0x074A],
5299 [0x07A6, 0x07B0], [0x07EB, 0x07F3], [0x0901, 0x0902],
5300 [0x093C, 0x093C], [0x0941, 0x0948], [0x094D, 0x094D],
5301 [0x0951, 0x0954], [0x0962, 0x0963], [0x0981, 0x0981],
5302 [0x09BC, 0x09BC], [0x09C1, 0x09C4], [0x09CD, 0x09CD],
5303 [0x09E2, 0x09E3], [0x0A01, 0x0A02], [0x0A3C, 0x0A3C],
5304 [0x0A41, 0x0A42], [0x0A47, 0x0A48], [0x0A4B, 0x0A4D],
5305 [0x0A70, 0x0A71], [0x0A81, 0x0A82], [0x0ABC, 0x0ABC],
5306 [0x0AC1, 0x0AC5], [0x0AC7, 0x0AC8], [0x0ACD, 0x0ACD],
5307 [0x0AE2, 0x0AE3], [0x0B01, 0x0B01], [0x0B3C, 0x0B3C],
5308 [0x0B3F, 0x0B3F], [0x0B41, 0x0B43], [0x0B4D, 0x0B4D],
5309 [0x0B56, 0x0B56], [0x0B82, 0x0B82], [0x0BC0, 0x0BC0],
5310 [0x0BCD, 0x0BCD], [0x0C3E, 0x0C40], [0x0C46, 0x0C48],
5311 [0x0C4A, 0x0C4D], [0x0C55, 0x0C56], [0x0CBC, 0x0CBC],
5312 [0x0CBF, 0x0CBF], [0x0CC6, 0x0CC6], [0x0CCC, 0x0CCD],
5313 [0x0CE2, 0x0CE3], [0x0D41, 0x0D43], [0x0D4D, 0x0D4D],
5314 [0x0DCA, 0x0DCA], [0x0DD2, 0x0DD4], [0x0DD6, 0x0DD6],
5315 [0x0E31, 0x0E31], [0x0E34, 0x0E3A], [0x0E47, 0x0E4E],
5316 [0x0EB1, 0x0EB1], [0x0EB4, 0x0EB9], [0x0EBB, 0x0EBC],
5317 [0x0EC8, 0x0ECD], [0x0F18, 0x0F19], [0x0F35, 0x0F35],
5318 [0x0F37, 0x0F37], [0x0F39, 0x0F39], [0x0F71, 0x0F7E],
5319 [0x0F80, 0x0F84], [0x0F86, 0x0F87], [0x0F90, 0x0F97],
5320 [0x0F99, 0x0FBC], [0x0FC6, 0x0FC6], [0x102D, 0x1030],
5321 [0x1032, 0x1032], [0x1036, 0x1037], [0x1039, 0x1039],
5322 [0x1058, 0x1059], [0x1160, 0x11FF], [0x135F, 0x135F],
5323 [0x1712, 0x1714], [0x1732, 0x1734], [0x1752, 0x1753],
5324 [0x1772, 0x1773], [0x17B4, 0x17B5], [0x17B7, 0x17BD],
5325 [0x17C6, 0x17C6], [0x17C9, 0x17D3], [0x17DD, 0x17DD],
5326 [0x180B, 0x180D], [0x18A9, 0x18A9], [0x1920, 0x1922],
5327 [0x1927, 0x1928], [0x1932, 0x1932], [0x1939, 0x193B],
5328 [0x1A17, 0x1A18], [0x1B00, 0x1B03], [0x1B34, 0x1B34],
5329 [0x1B36, 0x1B3A], [0x1B3C, 0x1B3C], [0x1B42, 0x1B42],
5330 [0x1B6B, 0x1B73], [0x1DC0, 0x1DCA], [0x1DFE, 0x1DFF],
5331 [0x200B, 0x200F], [0x202A, 0x202E], [0x2060, 0x2063],
5332 [0x206A, 0x206F], [0x20D0, 0x20EF], [0x302A, 0x302F],
5333 [0x3099, 0x309A], [0xA806, 0xA806], [0xA80B, 0xA80B],
5334 [0xA825, 0xA826], [0xFB1E, 0xFB1E], [0xFE00, 0xFE0F],
5335 [0xFE20, 0xFE23], [0xFEFF, 0xFEFF], [0xFFF9, 0xFFFB],
5336 [0x10A01, 0x10A03], [0x10A05, 0x10A06], [0x10A0C, 0x10A0F],
5337 [0x10A38, 0x10A3A], [0x10A3F, 0x10A3F], [0x1D167, 0x1D169],
5338 [0x1D173, 0x1D182], [0x1D185, 0x1D18B], [0x1D1AA, 0x1D1AD],
5339 [0x1D242, 0x1D244], [0xE0001, 0xE0001], [0xE0020, 0xE007F],
5343 function bisearch(ucs
) {
5345 var max
= COMBINING
.length
- 1;
5347 if (ucs
< COMBINING
[0][0] || ucs
> COMBINING
[max
][1])
5349 while (max
>= min
) {
5350 mid
= Math
.floor((min
+ max
) / 2);
5351 if (ucs
> COMBINING
[mid
][1])
5353 else if (ucs
< COMBINING
[mid
][0])
5360 function wcwidth(ucs
) {
5361 // test for 8-bit control characters
5364 if (ucs
< 32 || (ucs
>= 0x7f && ucs
< 0xa0))
5365 return opts
.control
;
5366 // binary search in table of non-spacing characters
5369 // if we arrive here, ucs is not a combining or C0/C1 control character
5374 ucs
<= 0x115f || // Hangul Jamo init. consonants
5377 (ucs
>= 0x2e80 && ucs
<= 0xa4cf && ucs
!= 0x303f) || // CJK..Yi
5378 (ucs
>= 0xac00 && ucs
<= 0xd7a3) || // Hangul Syllables
5379 (ucs
>= 0xf900 && ucs
<= 0xfaff) || // CJK Compat Ideographs
5380 (ucs
>= 0xfe10 && ucs
<= 0xfe19) || // Vertical forms
5381 (ucs
>= 0xfe30 && ucs
<= 0xfe6f) || // CJK Compat Forms
5382 (ucs
>= 0xff00 && ucs
<= 0xff60) || // Fullwidth Forms
5383 (ucs
>= 0xffe0 && ucs
<= 0xffe6) ||
5384 (ucs
>= 0x20000 && ucs
<= 0x2fffd) ||
5385 (ucs
>= 0x30000 && ucs
<= 0x3fffd)
5390 })({nul
: 0, control
: 0}); // configurable options
5396 Terminal
.EventEmitter
= EventEmitter
;
5397 Terminal
.CompositionHelper
= CompositionHelper
;
5398 Terminal
.Viewport
= Viewport
;
5399 Terminal
.inherits
= inherits
;
5402 * Adds an event listener to the terminal.
5404 * @param {string} event The name of the event. TODO: Document all event types
5405 * @param {function} callback The function to call when the event is triggered.
5409 Terminal
.cancel
= cancel
;