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
= '';
327 function Viewport(terminal
, viewportElement
, charMeasureElement
) {
328 this.terminal
= terminal
;
329 this.viewportElement
= viewportElement
;
330 this.charMeasureElement
= charMeasureElement
;
331 this.scrollArea
= document
.createElement('div');
332 this.scrollArea
.classList
.add('xterm-scroll-area');
333 this.viewportElement
.appendChild(this.scrollArea
);
334 this.currentHeight
= 0;
335 this.lastScrollPosition
;
337 this.terminal
.on('refresh', this.refreshRowHeight
.bind(this));
338 // TODO: Attach this to a more sensible event
339 this.terminal
.on('refresh', this.syncScrollArea
.bind(this));
340 this.viewportElement
.addEventListener('scroll', this.onScroll
.bind(this));
343 Viewport
.prototype.refreshRowHeight = function() {
344 var size
= this.charMeasureElement
.getBoundingClientRect();
345 if (size
.height
> 0 && size
.height
!== this.currentHeight
) {
346 this.currentHeight
= size
.height
;
347 this.viewportElement
.style
.lineHeight
= size
.height
+ 'px';
348 this.viewportElement
.style
.height
= size
.height
* this.terminal
.rows
+ 'px';
350 // TODO: Should this be lines.length - ybase? or lines.length - rows?
351 this.scrollArea
.style
.height
= (size
.height
* this.terminal
.lines
.length
) + 'px';
354 Viewport
.prototype.syncScrollArea = function() {
355 //console.log('ybase: ' + this.terminal.ybase);
356 //console.log('ydisp: ' + this.terminal.ydisp);
357 this.viewportElement
.scrollTop
= this.terminal
.ydisp
* this.currentHeight
;
360 Viewport
.prototype.onScroll = function(ev
) {
361 console
.log('onScroll', ev
);
362 console
.log('this.viewportElement.scrollTop: ' + this.viewportElement
.scrollTop
);
363 console
.log('row: ' + Math
.round(this.viewportElement
.scrollTop
/ this.currentHeight
));
364 console
.log('lastScrollPosition: ' + this.lastScrollPosition
);
365 console
.log(' scrollTop: ' + this.viewportElement
.scrollTop
);
367 // This helps get around the case where scrollTop changes by 1 pixel by pressing up or down on the scrollbar
368 // It gets complicated as the scrollbar sometimes locks to a multiple of the row
369 if (this.lastScrollPosition
!== this.viewportElement
.scrollTop
) {
370 console
.log('in if');
371 this.lastScrollPosition
= this.viewportElement
.scrollTop
;
372 var newRow
= this.viewportElement
.scrollTop
/ this.currentHeight
;
373 var diff
= newRow
- this.terminal
.ydisp
;
375 console
.log('diff = 0');
377 ev
.stopPropagation();
380 var multiplier
= diff
< 0 ? -1 : 1;
381 diff
= Math
.max(1, Math
.round(Math
.abs(diff
))) * multiplier
;
382 console
.log('onScroll diff: ' + diff
);
383 this.terminal
.scrollDisp(diff
);
386 ev
.stopPropagation();
389 Viewport
.prototype.onWheel = function(ev
) {
390 //var newRow = this.viewportElement.scrollTop / this.currentHeight;
391 //console.log('ydisp: ' + this.terminal.ydisp);
392 //console.log('this.viewportElement.scrollTop: ' + this.viewportElement.scrollTop);
393 //console.log('this.currentHeight: ' + this.currentHeight);
395 //this.terminal.scrollDisp(this.terminal.ydisp - newRow);
396 var multiplier
= ev
.deltaY
< 0 ? -1 : 1;
397 var diff
= Math
.max(1, Math
.round(Math
.abs(ev
.deltaY
/ this.currentHeight
)));
399 console
.log('diff * multiplier: ' + diff
* multiplier
);
400 this.terminal
.scrollDisp(diff
* multiplier
);
401 //this.lastScrollPosition = this.viewportElement.scrollTop;
407 var normal
= 0, escaped
= 1, csi
= 2, osc
= 3, charset
= 4, dcs
= 5, ignore
= 6;
414 * Creates a new `Terminal` object.
416 * @param {object} options An object containing a set of options, the available options are:
417 * - cursorBlink (boolean): Whether the terminal cursor blinks
421 * @alias module:xterm/src/xterm
423 function Terminal(options
) {
426 if (!(this instanceof Terminal
)) {
427 return new Terminal(arguments
[0], arguments
[1], arguments
[2]);
430 self
.cancel
= Terminal
.cancel
;
432 EventEmitter
.call(this);
434 if (typeof options
=== 'number') {
438 handler
: arguments
[2]
442 options
= options
|| {};
445 Object
.keys(Terminal
.defaults
).forEach(function(key
) {
446 if (options
[key
] == null) {
447 options
[key
] = Terminal
.options
[key
];
449 if (Terminal
[key
] !== Terminal
.defaults
[key
]) {
450 options
[key
] = Terminal
[key
];
453 self
[key
] = options
[key
];
456 if (options
.colors
.length
=== 8) {
457 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(8));
458 } else if (options
.colors
.length
=== 16) {
459 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(16));
460 } else if (options
.colors
.length
=== 10) {
461 options
.colors
= options
.colors
.slice(0, -2).concat(
462 Terminal
._colors
.slice(8, -2), options
.colors
.slice(-2));
463 } else if (options
.colors
.length
=== 18) {
464 options
.colors
= options
.colors
.concat(
465 Terminal
._colors
.slice(16, -2), options
.colors
.slice(-2));
467 this.colors
= options
.colors
;
469 this.options
= options
;
471 // this.context = options.context || window;
472 // this.document = options.document || document;
473 this.parent
= options
.body
|| options
.parent
474 || (document
? document
.getElementsByTagName('body')[0] : null);
476 this.cols
= options
.cols
|| options
.geometry
[0];
477 this.rows
= options
.rows
|| options
.geometry
[1];
479 if (options
.handler
) {
480 this.on('data', options
.handler
);
484 * The scroll position of the y cursor, ie. ybase + y = the y position within the entire
490 * The scroll position of the viewport
495 * The cursor's x position after ybase
500 * The cursor's y position after ybase
505 * Used to debounce the refresh function
507 this.isRefreshing
= false;
510 * Whether there is a full terminal refresh queued
513 this.cursorState
= 0;
514 this.cursorHidden
= false;
519 this.scrollBottom
= this.rows
- 1;
520 this.customKeydownHandler
= null;
523 this.applicationKeypad
= false;
524 this.applicationCursor
= false;
525 this.originMode
= false;
526 this.insertMode
= false;
527 this.wraparoundMode
= true; // defaults: xterm - true, vt100 - false
532 this.gcharset
= null;
534 this.charsets
= [null];
558 this.readable
= true;
559 this.writable
= true;
561 this.defAttr
= (0 << 18) | (257 << 9) | (256 << 0);
562 this.curAttr
= this.defAttr
;
565 this.currentParam
= 0;
569 // leftover surrogate high from previous write invocation
570 this.surrogate_high
= '';
573 * An array of all lines in the entire buffer, including the prompt. The lines are array of
574 * characters which are 2-length arrays where [0] is an attribute and [1] is the character.
579 this.lines
.push(this.blankLine());
586 inherits(Terminal
, EventEmitter
);
589 * back_color_erase feature for xterm.
591 Terminal
.prototype.eraseAttr = function() {
592 // if (this.is('screen')) return this.defAttr;
593 return (this.defAttr
& ~0x1ff) | (this.curAttr
& 0x1ff);
601 Terminal
.tangoColors
= [
622 // Colors 0-15 + 16-255
623 // Much thanks to TooTallNate for writing this.
624 Terminal
.colors
= (function() {
625 var colors
= Terminal
.tangoColors
.slice()
626 , r
= [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff]
631 for (; i
< 216; i
++) {
632 out(r
[(i
/ 36) % 6 | 0], r
[(i
/ 6) % 6 | 0], r
[i
% 6]);
637 for (; i
< 24; i
++) {
642 function out(r
, g
, b
) {
643 colors
.push('#' + hex(r
) + hex(g
) + hex(b
));
648 return c
.length
< 2 ? '0' + c
: c
;
654 Terminal
._colors
= Terminal
.colors
.slice();
656 Terminal
.vcolors
= (function() {
658 , colors
= Terminal
.colors
662 for (; i
< 256; i
++) {
663 color
= parseInt(colors
[i
].substring(1), 16);
665 (color
>> 16) & 0xff,
678 Terminal
.defaults
= {
679 colors
: Terminal
.colors
,
691 // programFeatures: false,
695 Terminal
.options
= {};
697 Terminal
.focus
= null;
699 each(keys(Terminal
.defaults
), function(key
) {
700 Terminal
[key
] = Terminal
.defaults
[key
];
701 Terminal
.options
[key
] = Terminal
.defaults
[key
];
705 * Focus the terminal. Delegates focus handling to the terminal's DOM element.
707 Terminal
.prototype.focus = function() {
708 return this.textarea
.focus();
712 * Binds the desired focus behavior on a given terminal object.
716 Terminal
.bindFocus = function (term
) {
717 on(term
.textarea
, 'focus', function (ev
) {
718 if (term
.sendFocus
) {
721 term
.element
.classList
.add('focus');
723 Terminal
.focus
= term
;
724 term
.emit('focus', {terminal
: term
});
729 * Blur the terminal. Delegates blur handling to the terminal's DOM element.
731 Terminal
.prototype.blur = function() {
732 return this.textarea
.blur();
736 * Binds the desired blur behavior on a given terminal object.
740 Terminal
.bindBlur = function (term
) {
741 on(term
.textarea
, 'blur', function (ev
) {
742 term
.refresh(term
.y
, term
.y
);
743 if (term
.sendFocus
) {
746 term
.element
.classList
.remove('focus');
747 Terminal
.focus
= null;
748 term
.emit('blur', {terminal
: term
});
753 * Initialize default behavior
755 Terminal
.prototype.initGlobal = function() {
756 Terminal
.bindPaste(this);
757 Terminal
.bindKeys(this);
758 Terminal
.bindCopy(this);
759 Terminal
.bindFocus(this);
760 Terminal
.bindBlur(this);
764 * Bind to paste event and allow both keyboard and right-click pasting, without having the
765 * contentEditable value set to true.
767 Terminal
.bindPaste = function(term
) {
768 on([term
.textarea
, term
.element
], 'paste', function(ev
) {
769 ev
.stopPropagation();
770 if (ev
.clipboardData
) {
771 var text
= ev
.clipboardData
.getData('text/plain');
773 term
.textarea
.value
= '';
774 return term
.cancel(ev
);
780 * Prepares text copied from terminal selection, to be saved in the clipboard by:
781 * 1. stripping all trailing white spaces
782 * 2. converting all non-breaking spaces to regular spaces
783 * @param {string} text The copied text that needs processing for storing in clipboard
787 Terminal
.prepareCopiedTextForClipboard = function (text
) {
788 var space
= String
.fromCharCode(32),
789 nonBreakingSpace
= String
.fromCharCode(160),
790 allNonBreakingSpaces
= new RegExp(nonBreakingSpace
, 'g'),
791 processedText
= text
.split('\n').map(function (line
) {
793 * Strip all trailing white spaces and convert all non-breaking spaces to regular
796 var processedLine
= line
.replace(/\s+$/g, '').replace(allNonBreakingSpaces
, space
);
798 return processedLine
;
801 return processedText
;
805 * Apply key handling to the terminal
807 Terminal
.bindKeys = function(term
) {
808 on(term
.element
, 'keydown', function(ev
) {
809 if (document
.activeElement
!= this) {
815 on(term
.element
, 'keypress', function(ev
) {
816 if (document
.activeElement
!= this) {
822 on(term
.element
, 'keyup', term
.focus
.bind(term
));
824 on(term
.textarea
, 'keydown', function(ev
) {
828 on(term
.textarea
, 'keypress', function(ev
) {
830 // Truncate the textarea's value, since it is not needed
834 on(term
.textarea
, 'compositionstart', term
.compositionHelper
.compositionstart
.bind(term
.compositionHelper
));
835 on(term
.textarea
, 'compositionupdate', term
.compositionHelper
.compositionupdate
.bind(term
.compositionHelper
));
836 on(term
.textarea
, 'compositionend', term
.compositionHelper
.compositionend
.bind(term
.compositionHelper
));
837 term
.on('refresh', term
.compositionHelper
.updateCompositionElements
.bind(term
.compositionHelper
));
841 * Binds copy functionality to the given terminal.
844 Terminal
.bindCopy = function(term
) {
845 on(term
.element
, 'copy', function(ev
) {
852 * Insert the given row to the terminal or produce a new one
853 * if no row argument is passed. Return the inserted row.
854 * @param {HTMLElement} row (optional) The row to append to the terminal.
856 Terminal
.prototype.insertRow = function (row
) {
857 if (typeof row
!= 'object') {
858 row
= document
.createElement('div');
861 this.rowContainer
.appendChild(row
);
862 this.children
.push(row
);
868 * Opens the terminal within an element.
870 * @param {HTMLElement} parent The element to create the terminal within.
872 Terminal
.prototype.open = function(parent
) {
873 var self
=this, i
=0, div
;
875 this.parent
= parent
|| this.parent
;
878 throw new Error('Terminal requires a parent element.');
882 * Grab global elements
884 this.context
= this.parent
.ownerDocument
.defaultView
;
885 this.document
= this.parent
.ownerDocument
;
886 this.body
= this.document
.getElementsByTagName('body')[0];
891 if (this.context
.navigator
&& this.context
.navigator
.userAgent
) {
892 this.isMSIE
= !!~this.context
.navigator
.userAgent
.indexOf('MSIE');
896 * Find the users platform. We use this to interpret the meta key
897 * and ISO third level shifts.
898 * http://stackoverflow.com/questions/19877924/what-is-the-list-of-possible-values-for-navigator-platform-as-of-today
900 if (this.context
.navigator
&& this.context
.navigator
.platform
) {
901 this.isMac
= contains(
902 this.context
.navigator
.platform
,
903 ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K']
905 this.isIpad
= this.context
.navigator
.platform
=== 'iPad';
906 this.isIphone
= this.context
.navigator
.platform
=== 'iPhone';
907 this.isMSWindows
= contains(
908 this.context
.navigator
.platform
,
909 ['Windows', 'Win16', 'Win32', 'WinCE']
914 * Create main element container
916 this.element
= this.document
.createElement('div');
917 this.element
.classList
.add('terminal');
918 this.element
.classList
.add('xterm');
919 this.element
.classList
.add('xterm-theme-' + this.theme
);
921 this.element
.style
.height
922 this.element
.setAttribute('tabindex', 0);
924 this.viewportElement
= document
.createElement('div');
925 this.viewportElement
.classList
.add('xterm-viewport');
926 this.element
.appendChild(this.viewportElement
);
929 * Create the container that will hold the lines of the terminal and then
930 * produce the lines the lines.
932 this.rowContainer
= document
.createElement('div');
933 this.rowContainer
.classList
.add('xterm-rows');
934 this.viewportElement
.appendChild(this.rowContainer
);
938 * Create the container that will hold helpers like the textarea for
939 * capturing DOM Events. Then produce the helpers.
941 this.helperContainer
= document
.createElement('div');
942 this.helperContainer
.classList
.add('xterm-helpers');
943 // TODO: This should probably be inserted once it's filled to prevent an additional layout
944 this.element
.appendChild(this.helperContainer
);
945 this.textarea
= document
.createElement('textarea');
946 this.textarea
.classList
.add('xterm-helper-textarea');
947 this.textarea
.setAttribute('autocorrect', 'off');
948 this.textarea
.setAttribute('autocapitalize', 'off');
949 this.textarea
.setAttribute('spellcheck', 'false');
950 this.textarea
.tabIndex
= 0;
951 this.textarea
.addEventListener('focus', function() {
952 self
.emit('focus', {terminal
: self
});
954 this.textarea
.addEventListener('blur', function() {
955 self
.emit('blur', {terminal
: self
});
957 this.helperContainer
.appendChild(this.textarea
);
959 this.compositionView
= document
.createElement('div');
960 this.compositionView
.classList
.add('composition-view');
961 this.compositionHelper
= new CompositionHelper(this.textarea
, this.compositionView
, this);
962 this.helperContainer
.appendChild(this.compositionView
);
964 this.charMeasureElement
= document
.createElement('div');
965 this.charMeasureElement
.classList
.add('xterm-char-measure-element');
966 this.charMeasureElement
.innerHTML
= 'W';
967 this.helperContainer
.appendChild(this.charMeasureElement
);
970 this.viewport
= new Viewport(this, this.viewportElement
, this.charMeasureElement
);
972 for (; i
< this.rows
; i
++) {
975 this.parent
.appendChild(this.element
);
979 this.refresh(0, this.rows
- 1);
981 // Initialize global actions that
982 // need to be taken on the document.
985 // Ensure there is a Terminal.focus.
988 on(this.element
, 'mouseup', function() {
989 var selection
= document
.getSelection(),
990 collapsed
= selection
.isCollapsed
,
991 isRange
= typeof collapsed
== 'boolean' ? !collapsed
: selection
.type
== 'Range';
997 // Listen for mouse events and translate
998 // them into terminal mouse protocols.
1001 // Figure out whether boldness affects
1002 // the character width of monospace fonts.
1003 if (Terminal
.brokenBold
== null) {
1004 Terminal
.brokenBold
= isBoldBroken(this.document
);
1012 * Attempts to load an add-on using CommonJS or RequireJS (whichever is available).
1013 * @param {string} addon The name of the addon to load
1016 Terminal
.loadAddon = function(addon
, callback
) {
1017 if (typeof exports
=== 'object' && typeof module
=== 'object') {
1019 return require(__dirname
+ '/../addons/' + addon
);
1020 } else if (typeof define
== 'function') {
1022 return require(['../addons/' + addon
+ '/' + addon
], callback
);
1024 console
.error('Cannot load a module without a CommonJS or RequireJS environment.');
1031 * XTerm mouse events
1032 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#Mouse%20Tracking
1033 * To better understand these
1034 * the xterm code is very helpful:
1036 * button.c, charproc.c, misc.c
1037 * Relevant functions in xterm/button.c:
1038 * BtnCode, EmitButtonCode, EditorButton, SendMousePosition
1040 Terminal
.prototype.bindMouse = function() {
1041 var el
= this.element
, self
= this, pressed
= 32;
1042 var wheelEvent
= ('onmousewheel' in this.context
) ? 'mousewheel' : 'DOMMouseScroll';
1044 // mouseup, mousedown, mousewheel
1045 // left click: ^[[M 3<^[[M#3<
1046 // mousewheel up: ^[[M`3>
1047 function sendButton(ev
) {
1051 // get the xterm-style button
1052 button
= getButton(ev
);
1054 // get mouse coordinates
1055 pos
= getCoords(ev
);
1058 sendEvent(button
, pos
);
1060 switch (ev
.overrideType
|| ev
.type
) {
1065 // keep it at the left
1066 // button, just in case.
1077 // motion example of a left click:
1078 // ^[[M 3<^[[M@4<^[[M@5<^[[M@6<^[[M@7<^[[M#7<
1079 function sendMove(ev
) {
1080 var button
= pressed
1083 pos
= getCoords(ev
);
1086 // buttons marked as motions
1087 // are incremented by 32
1090 sendEvent(button
, pos
);
1093 // encode button and
1094 // position to characters
1095 function encode(data
, ch
) {
1096 if (!self
.utfMouse
) {
1097 if (ch
=== 255) return data
.push(0);
1098 if (ch
> 127) ch
= 127;
1101 if (ch
=== 2047) return data
.push(0);
1105 if (ch
> 2047) ch
= 2047;
1106 data
.push(0xC0 | (ch
>> 6));
1107 data
.push(0x80 | (ch
& 0x3F));
1112 // send a mouse event:
1113 // regular/utf8: ^[[M Cb Cx Cy
1114 // urxvt: ^[[ Cb ; Cx ; Cy M
1115 // sgr: ^[[ Cb ; Cx ; Cy M/m
1116 // vt300: ^[[ 24(1/3/5)~ [ Cx , Cy ] \r
1117 // locator: CSI P e ; P b ; P r ; P c ; P p & w
1118 function sendEvent(button
, pos
) {
1119 // self.emit('mouse', {
1125 if (self
.vt300Mouse
) {
1127 // http://www.vt100.net/docs/vt3xx-gp/chapter15.html
1131 var data
= '\x1b[24';
1132 if (button
=== 0) data
+= '1';
1133 else if (button
=== 1) data
+= '3';
1134 else if (button
=== 2) data
+= '5';
1135 else if (button
=== 3) return;
1137 data
+= '~[' + pos
.x
+ ',' + pos
.y
+ ']\r';
1142 if (self
.decLocator
) {
1147 if (button
=== 0) button
= 2;
1148 else if (button
=== 1) button
= 4;
1149 else if (button
=== 2) button
= 6;
1150 else if (button
=== 3) button
= 3;
1154 + (button
=== 3 ? 4 : 0)
1165 if (self
.urxvtMouse
) {
1170 self
.send('\x1b[' + button
+ ';' + pos
.x
+ ';' + pos
.y
+ 'M');
1174 if (self
.sgrMouse
) {
1178 + ((button
& 3) === 3 ? button
& ~3 : button
)
1183 + ((button
& 3) === 3 ? 'm' : 'M'));
1189 encode(data
, button
);
1190 encode(data
, pos
.x
);
1191 encode(data
, pos
.y
);
1193 self
.send('\x1b[M' + String
.fromCharCode
.apply(String
, data
));
1196 function getButton(ev
) {
1209 // 1, and 2 - with 64 added
1210 switch (ev
.overrideType
|| ev
.type
) {
1212 button
= ev
.button
!= null
1219 button
= button
=== 1 ? 0 : button
=== 4 ? 1 : button
;
1225 case 'DOMMouseScroll':
1226 button
= ev
.detail
< 0
1231 button
= ev
.wheelDeltaY
> 0
1237 // next three bits are the modifiers:
1238 // 4 = shift, 8 = meta, 16 = control
1239 shift
= ev
.shiftKey
? 4 : 0;
1240 meta
= ev
.metaKey
? 8 : 0;
1241 ctrl
= ev
.ctrlKey
? 16 : 0;
1242 mod
= shift
| meta
| ctrl
;
1245 if (self
.vt200Mouse
) {
1248 } else if (!self
.normalMouse
) {
1253 button
= (32 + (mod
<< 2)) + button
;
1258 // mouse coordinates measured in cols/rows
1259 function getCoords(ev
) {
1262 // ignore browsers without pageX for now
1263 if (ev
.pageX
== null) return;
1269 // should probably check offsetParent
1270 // but this is more portable
1271 while (el
&& el
!== self
.document
.documentElement
) {
1274 el
= 'offsetParent' in el
1279 // convert to cols/rows
1280 w
= self
.element
.clientWidth
;
1281 h
= self
.element
.clientHeight
;
1282 x
= Math
.round((x
/ w
) * self
.cols
);
1283 y
= Math
.round((y
/ h
) * self
.rows
);
1285 // be sure to avoid sending
1286 // bad positions to the program
1288 if (x
> self
.cols
) x
= self
.cols
;
1290 if (y
> self
.rows
) y
= self
.rows
;
1292 // xterm sends raw bytes and
1293 // starts at 32 (SP) for each.
1300 type
: (ev
.overrideType
|| ev
.type
) === wheelEvent
1302 : (ev
.overrideType
|| ev
.type
)
1306 on(el
, 'mousedown', function(ev
) {
1307 if (!self
.mouseEvents
) return;
1316 //if (self.vt200Mouse && !self.normalMouse) {
1317 if (self
.vt200Mouse
) {
1318 ev
.overrideType
= 'mouseup';
1320 return self
.cancel(ev
);
1324 if (self
.normalMouse
) on(self
.document
, 'mousemove', sendMove
);
1326 // x10 compatibility mode can't send button releases
1327 if (!self
.x10Mouse
) {
1328 on(self
.document
, 'mouseup', function up(ev
) {
1330 if (self
.normalMouse
) off(self
.document
, 'mousemove', sendMove
);
1331 off(self
.document
, 'mouseup', up
);
1332 return self
.cancel(ev
);
1336 return self
.cancel(ev
);
1339 //if (self.normalMouse) {
1340 // on(self.document, 'mousemove', sendMove);
1343 on(el
, wheelEvent
, function(ev
) {
1344 if (!self
.mouseEvents
) return;
1347 || self
.decLocator
) return;
1349 return self
.cancel(ev
);
1352 // allow mousewheel scrolling in
1353 // the shell for example
1354 on(el
, wheelEvent
, function(ev
) {
1355 if (self
.mouseEvents
) return;
1356 if (self
.applicationKeypad
) return;
1357 self
.viewport
.onWheel(ev
);
1358 /*if (ev.type === 'DOMMouseScroll') {
1359 self.scrollDisp(ev.detail < 0 ? -1 : 1);
1361 self.scrollDisp(ev.wheelDeltaY > 0 ? -1 : 1);
1363 return self
.cancel(ev
);
1368 * Destroys the terminal.
1370 Terminal
.prototype.destroy = function() {
1371 this.readable
= false;
1372 this.writable
= false;
1374 this.handler = function() {};
1375 this.write = function() {};
1376 if (this.element
.parentNode
) {
1377 this.element
.parentNode
.removeChild(this.element
);
1379 //this.emit('close');
1384 * Flags used to render terminal text properly
1395 * Refreshes (re-renders) terminal content within two rows (inclusive)
1399 * In the screen buffer, each character is stored as a an array with a character
1400 * and a 32-bit integer:
1401 * - First value: a utf-16 character.
1403 * - Next 9 bits: background color (0-511).
1404 * - Next 9 bits: foreground color (0-511).
1405 * - Next 14 bits: a mask for misc. flags:
1412 * @param {number} start The row to start from (between 0 and terminal's height terminal - 1)
1413 * @param {number} end The row to end at (between fromRow and terminal's height terminal - 1)
1414 * @param {boolean} queue Whether the refresh should ran right now or be queued
1416 Terminal
.prototype.refresh = function(start
, end
, queue
) {
1419 // queue defaults to true
1420 queue
= (typeof queue
== 'undefined') ? true : queue
;
1423 * The refresh queue allows refresh to execute only approximately 30 times a second. For
1424 * commands that pass a significant amount of output to the write function, this prevents the
1425 * terminal from maxing out the CPU and making the UI unresponsive. While commands can still
1426 * run beyond what they do on the terminal, it is far better with a debounce in place as
1427 * every single terminal manipulation does not need to be constructed in the DOM.
1429 * A side-effect of this is that it makes ^C to interrupt a process seem more responsive.
1432 // If refresh should be queued, order the refresh and return.
1433 if (this._refreshIsQueued
) {
1434 // If a refresh has already been queued, just order a full refresh next
1435 this._fullRefreshNext
= true;
1437 setTimeout(function () {
1438 self
.refresh(start
, end
, false);
1440 this._refreshIsQueued
= true;
1445 // If refresh should be run right now (not be queued), release the lock
1446 this._refreshIsQueued
= false;
1448 // If multiple refreshes were requested, make a full refresh.
1449 if (this._fullRefreshNext
) {
1451 end
= this.rows
- 1;
1452 this._fullRefreshNext
= false // reset lock
1455 var x
, y
, i
, line
, out
, ch
, ch_width
, width
, data
, attr
, bg
, fg
, flags
, row
, parent
, focused
= document
.activeElement
;
1457 // If this is a big refresh, remove the terminal rows from the DOM for faster calculations
1458 if (end
- start
>= this.rows
/ 2) {
1459 parent
= this.element
.parentNode
;
1461 this.viewportElement
.removeChild(this.rowContainer
);
1468 if (end
>= this.rows
.length
) {
1469 this.log('`end` is too large. Most likely a bad CSR.');
1470 end
= this.rows
.length
- 1;
1473 for (; y
<= end
; y
++) {
1474 row
= y
+ this.ydisp
;
1476 line
= this.lines
[row
];
1479 if (this.y
=== y
- (this.ybase
- this.ydisp
)
1481 && !this.cursorHidden
) {
1487 attr
= this.defAttr
;
1490 for (; i
< width
; i
++) {
1493 ch_width
= line
[i
][2];
1497 if (i
=== x
) data
= -1;
1499 if (data
!== attr
) {
1500 if (attr
!== this.defAttr
) {
1503 if (data
!== this.defAttr
) {
1505 out
+= '<span class="reverse-video terminal-cursor';
1506 if (this.cursorBlink
) {
1511 var classNames
= [];
1514 fg
= (data
>> 9) & 0x1ff;
1517 if (flags
& Terminal
.flags
.BOLD
) {
1518 if (!Terminal
.brokenBold
) {
1519 classNames
.push('xterm-bold');
1521 // See: XTerm*boldColors
1522 if (fg
< 8) fg
+= 8;
1525 if (flags
& Terminal
.flags
.UNDERLINE
) {
1526 classNames
.push('xterm-underline');
1529 if (flags
& Terminal
.flags
.BLINK
) {
1530 classNames
.push('xterm-blink');
1534 * If inverse flag is on, then swap the foreground and background variables.
1536 if (flags
& Terminal
.flags
.INVERSE
) {
1537 /* One-line variable swap in JavaScript: http://stackoverflow.com/a/16201730 */
1538 bg = [fg, fg = bg][0];
1539 // Should inverse just be before the
1540 // above boldColors effect instead?
1541 if ((flags & 1) && fg < 8) fg += 8;
1544 if (flags & Terminal.flags.INVISIBLE) {
1545 classNames.push('xterm-hidden');
1549 * Weird situation: Invert flag used black foreground and white background results
1550 * in invalid background color, positioned at the 256 index of the 256 terminal
1551 * color map. Pin the colors manually in such a case.
1553 * Source: https://github.com/sourcelair/xterm.js/issues/57
1555 if (flags
& Terminal
.flags
.INVERSE
) {
1565 classNames
.push('xterm-bg-color-' + bg
);
1569 classNames
.push('xterm-color-' + fg
);
1573 if (classNames
.length
) {
1574 out
+= ' class="' + classNames
.join(' ') + '"';
1603 if (attr
!== this.defAttr
) {
1607 this.children
[y
].innerHTML
= out
;
1611 this.viewportElement
.appendChild(this.rowContainer
);
1614 this.emit('refresh', {element
: this.element
, start
: start
, end
: end
});
1618 * Display the cursor element
1620 Terminal
.prototype.showCursor = function() {
1621 if (!this.cursorState
) {
1622 this.cursorState
= 1;
1623 this.refresh(this.y
, this.y
);
1628 * Scroll the terminal
1630 Terminal
.prototype.scroll = function() {
1633 if (++this.ybase
=== this.scrollback
) {
1634 this.ybase
= this.ybase
/ 2 | 0;
1635 this.lines
= this.lines
.slice(-(this.ybase
+ this.rows
) + 1);
1638 this.ydisp
= this.ybase
;
1641 row
= this.ybase
+ this.rows
- 1;
1643 // subtract the bottom scroll region
1644 row
-= this.rows
- 1 - this.scrollBottom
;
1646 if (row
=== this.lines
.length
) {
1647 // potential optimization:
1648 // pushing is faster than splicing
1649 // when they amount to the same
1651 this.lines
.push(this.blankLine());
1654 this.lines
.splice(row
, 0, this.blankLine());
1657 if (this.scrollTop
!== 0) {
1658 if (this.ybase
!== 0) {
1660 this.ydisp
= this.ybase
;
1662 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
1666 this.updateRange(this.scrollTop
);
1667 this.updateRange(this.scrollBottom
);
1669 this.viewport
.syncScrollArea();
1673 * Scroll the display of the terminal
1674 * @param {number} disp The number of lines to scroll down (negatives scroll up).
1676 Terminal
.prototype.scrollDisp = function(disp
) {
1679 if (this.ydisp
> this.ybase
) {
1680 this.ydisp
= this.ybase
;
1681 } else if (this.ydisp
< 0) {
1685 this.refresh(0, this.rows
- 1);
1689 * Writes text to the terminal.
1690 * @param {string} text The text to write to the terminal.
1692 Terminal
.prototype.write = function(data
) {
1693 var l
= data
.length
, i
= 0, j
, cs
, ch
, code
, low
, ch_width
, row
;
1695 this.refreshStart
= this.y
;
1696 this.refreshEnd
= this.y
;
1698 if (this.ybase
!== this.ydisp
) {
1699 this.ydisp
= this.ybase
;
1703 // apply leftover surrogate high from last write
1704 if (this.surrogate_high
) {
1705 data
= this.surrogate_high
+ data
;
1706 this.surrogate_high
= '';
1709 for (; i
< l
; i
++) {
1712 // FIXME: higher chars than 0xa0 are not allowed in escape sequences
1713 // --> maybe move to default
1714 code
= data
.charCodeAt(i
);
1715 if (0xD800 <= code
&& code
<= 0xDBFF) {
1716 // we got a surrogate high
1717 // get surrogate low (next 2 bytes)
1718 low
= data
.charCodeAt(i
+1);
1720 // end of data stream, save surrogate high
1721 this.surrogate_high
= ch
;
1724 code
= ((code
- 0xD800) * 0x400) + (low
- 0xDC00) + 0x10000;
1725 ch
+= data
.charAt(i
+1);
1727 // surrogate low - already handled above
1728 if (0xDC00 <= code
&& code
<= 0xDFFF)
1731 switch (this.state
) {
1742 if (this.convertEol
) {
1746 if (this.y
> this.scrollBottom
) {
1766 this.x
= this.nextStop();
1781 this.state
= escaped
;
1786 // calculate print space
1787 // expensive call, therefore we save width in line buffer
1788 ch_width
= wcwidth(code
);
1791 if (this.charset
&& this.charset
[ch
]) {
1792 ch
= this.charset
[ch
];
1795 row
= this.y
+ this.ybase
;
1797 // insert combining char in last cell
1798 // FIXME: needs handling after cursor jumps
1799 if (!ch_width
&& this.x
) {
1801 // dont overflow left
1802 if (this.lines
[row
][this.x
-1]) {
1803 if (!this.lines
[row
][this.x
-1][2]) {
1805 // found empty cell after fullwidth, need to go 2 cells back
1806 if (this.lines
[row
][this.x
-2])
1807 this.lines
[row
][this.x
-2][1] += ch
;
1810 this.lines
[row
][this.x
-1][1] += ch
;
1812 this.updateRange(this.y
);
1817 // goto next line if ch would overflow
1818 // TODO: needs a global min terminal width of 2
1819 if (this.x
+ch_width
-1 >= this.cols
) {
1820 // autowrap - DECAWM
1821 if (this.wraparoundMode
) {
1824 if (this.y
> this.scrollBottom
) {
1829 this.x
= this.cols
-1;
1830 if(ch_width
===2) // FIXME: check for xterm behavior
1834 row
= this.y
+ this.ybase
;
1836 // insert mode: move characters to right
1837 if (this.insertMode
) {
1838 // do this twice for a fullwidth char
1839 for (var moves
=0; moves
<ch_width
; ++moves
) {
1840 // remove last cell, if it's width is 0
1841 // we have to adjust the second last cell as well
1842 var removed
= this.lines
[this.y
+ this.ybase
].pop();
1844 && this.lines
[row
][this.cols
-2]
1845 && this.lines
[row
][this.cols
-2][2]===2)
1846 this.lines
[row
][this.cols
-2] = [this.curAttr
, ' ', 1];
1848 // insert empty cell at cursor
1849 this.lines
[row
].splice(this.x
, 0, [this.curAttr
, ' ', 1]);
1853 this.lines
[row
][this.x
] = [this.curAttr
, ch
, ch_width
];
1855 this.updateRange(this.y
);
1857 // fullwidth char - set next cell width to zero and advance cursor
1859 this.lines
[row
][this.x
] = [this.curAttr
, '', 0];
1868 // ESC [ Control Sequence Introducer ( CSI is 0x9b).
1871 this.currentParam
= 0;
1875 // ESC ] Operating System Command ( OSC is 0x9d).
1878 this.currentParam
= 0;
1882 // ESC P Device Control String ( DCS is 0x90).
1885 this.currentParam
= 0;
1889 // ESC _ Application Program Command ( APC is 0x9f).
1891 this.state
= ignore
;
1894 // ESC ^ Privacy Message ( PM is 0x9e).
1896 this.state
= ignore
;
1899 // ESC c Full Reset (RIS).
1904 // ESC E Next Line ( NEL is 0x85).
1905 // ESC D Index ( IND is 0x84).
1913 // ESC M Reverse Index ( RI is 0x8d).
1915 this.reverseIndex();
1918 // ESC % Select default/utf-8 character set.
1919 // @ = default, G = utf-8
1921 //this.charset = null;
1923 this.setgCharset(0, Terminal
.charsets
.US
);
1924 this.state
= normal
;
1928 // ESC (,),*,+,-,. Designate G0-G2 Character Set.
1929 case '(': // <-- this seems to get all the attention
1955 this.state
= charset
;
1958 // Designate G3 Character Set (VT300).
1959 // A = ISO Latin-1 Supplemental.
1963 this.state
= charset
;
1968 // Single Shift Select of G2 Character Set
1969 // ( SS2 is 0x8e). This affects next character only.
1973 // Single Shift Select of G3 Character Set
1974 // ( SS3 is 0x8f). This affects next character only.
1978 // Invoke the G2 Character Set as GL (LS2).
1983 // Invoke the G3 Character Set as GL (LS3).
1988 // Invoke the G3 Character Set as GR (LS3R).
1993 // Invoke the G2 Character Set as GR (LS2R).
1998 // Invoke the G1 Character Set as GR (LS1R).
2003 // ESC 7 Save Cursor (DECSC).
2006 this.state
= normal
;
2009 // ESC 8 Restore Cursor (DECRC).
2011 this.restoreCursor();
2012 this.state
= normal
;
2015 // ESC # 3 DEC line height/width
2017 this.state
= normal
;
2021 // ESC H Tab Set (HTS is 0x88).
2026 // ESC = Application Keypad (DECPAM).
2028 this.log('Serial port requested application keypad.');
2029 this.applicationKeypad
= true;
2030 this.state
= normal
;
2033 // ESC > Normal Keypad (DECPNM).
2035 this.log('Switching back to normal keypad.');
2036 this.applicationKeypad
= false;
2037 this.state
= normal
;
2041 this.state
= normal
;
2042 this.error('Unknown ESC control: %s.', ch
);
2049 case '0': // DEC Special Character and Line Drawing Set.
2050 cs
= Terminal
.charsets
.SCLD
;
2053 cs
= Terminal
.charsets
.UK
;
2055 case 'B': // United States (USASCII).
2056 cs
= Terminal
.charsets
.US
;
2059 cs
= Terminal
.charsets
.Dutch
;
2061 case 'C': // Finnish
2063 cs
= Terminal
.charsets
.Finnish
;
2066 cs
= Terminal
.charsets
.French
;
2068 case 'Q': // FrenchCanadian
2069 cs
= Terminal
.charsets
.FrenchCanadian
;
2072 cs
= Terminal
.charsets
.German
;
2074 case 'Y': // Italian
2075 cs
= Terminal
.charsets
.Italian
;
2077 case 'E': // NorwegianDanish
2079 cs
= Terminal
.charsets
.NorwegianDanish
;
2081 case 'Z': // Spanish
2082 cs
= Terminal
.charsets
.Spanish
;
2084 case 'H': // Swedish
2086 cs
= Terminal
.charsets
.Swedish
;
2089 cs
= Terminal
.charsets
.Swiss
;
2091 case '/': // ISOLatin (actually /A)
2092 cs
= Terminal
.charsets
.ISOLatin
;
2096 cs
= Terminal
.charsets
.US
;
2099 this.setgCharset(this.gcharset
, cs
);
2100 this.gcharset
= null;
2101 this.state
= normal
;
2107 // Set Text Parameters.
2108 if (ch
=== '\x1b' || ch
=== '\x07') {
2109 if (ch
=== '\x1b') i
++;
2111 this.params
.push(this.currentParam
);
2113 switch (this.params
[0]) {
2117 if (this.params
[1]) {
2118 this.title
= this.params
[1];
2119 this.handleTitle(this.title
);
2127 // change dynamic colors
2139 // change dynamic ui colors
2151 // manipulate selection data
2169 this.currentParam
= 0;
2170 this.state
= normal
;
2172 if (!this.params
.length
) {
2173 if (ch
>= '0' && ch
<= '9') {
2175 this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
2176 } else if (ch
=== ';') {
2177 this.params
.push(this.currentParam
);
2178 this.currentParam
= '';
2181 this.currentParam
+= ch
;
2188 if (ch
=== '?' || ch
=== '>' || ch
=== '!') {
2194 if (ch
>= '0' && ch
<= '9') {
2195 this.currentParam
= this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
2199 // '$', '"', ' ', '\''
2200 if (ch
=== '$' || ch
=== '"' || ch
=== ' ' || ch
=== '\'') {
2205 this.params
.push(this.currentParam
);
2206 this.currentParam
= 0;
2209 if (ch
=== ';') break;
2211 this.state
= normal
;
2215 // Cursor Up Ps Times (default = 1) (CUU).
2217 this.cursorUp(this.params
);
2221 // Cursor Down Ps Times (default = 1) (CUD).
2223 this.cursorDown(this.params
);
2227 // Cursor Forward Ps Times (default = 1) (CUF).
2229 this.cursorForward(this.params
);
2233 // Cursor Backward Ps Times (default = 1) (CUB).
2235 this.cursorBackward(this.params
);
2239 // Cursor Position [row;column] (default = [1,1]) (CUP).
2241 this.cursorPos(this.params
);
2244 // CSI Ps J Erase in Display (ED).
2246 this.eraseInDisplay(this.params
);
2249 // CSI Ps K Erase in Line (EL).
2251 this.eraseInLine(this.params
);
2254 // CSI Pm m Character Attributes (SGR).
2257 this.charAttributes(this.params
);
2261 // CSI Ps n Device Status Report (DSR).
2264 this.deviceStatus(this.params
);
2273 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
2275 this.insertChars(this.params
);
2279 // Cursor Next Line Ps Times (default = 1) (CNL).
2281 this.cursorNextLine(this.params
);
2285 // Cursor Preceding Line Ps Times (default = 1) (CNL).
2287 this.cursorPrecedingLine(this.params
);
2291 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
2293 this.cursorCharAbsolute(this.params
);
2297 // Insert Ps Line(s) (default = 1) (IL).
2299 this.insertLines(this.params
);
2303 // Delete Ps Line(s) (default = 1) (DL).
2305 this.deleteLines(this.params
);
2309 // Delete Ps Character(s) (default = 1) (DCH).
2311 this.deleteChars(this.params
);
2315 // Erase Ps Character(s) (default = 1) (ECH).
2317 this.eraseChars(this.params
);
2320 // CSI Pm ` Character Position Absolute
2321 // [column] (default = [row,1]) (HPA).
2323 this.charPosAbsolute(this.params
);
2327 // Horizontal Position Relative
2329 this.HPositionRelative(this.params
);
2333 // Send Device Attributes (Primary DA).
2335 // Send Device Attributes (Secondary DA)
2337 this.sendDeviceAttributes(this.params
);
2341 // Line Position Absolute [row] (default = [1,column]) (VPA).
2343 this.linePosAbsolute(this.params
);
2346 // 145 65 e * VPR - Vertical Position Relative
2348 this.VPositionRelative(this.params
);
2352 // Horizontal and Vertical Position [row;column] (default =
2355 this.HVPosition(this.params
);
2358 // CSI Pm h Set Mode (SM).
2359 // CSI ? Pm h - mouse escape codes, cursor escape codes
2361 this.setMode(this.params
);
2364 // CSI Pm l Reset Mode (RM).
2367 this.resetMode(this.params
);
2371 // Set Scrolling Region [top;bottom] (default = full size of win-
2375 this.setScrollRegion(this.params
);
2379 // Save cursor (ANSI.SYS).
2381 this.saveCursor(this.params
);
2385 // Restore cursor (ANSI.SYS).
2387 this.restoreCursor(this.params
);
2395 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
2397 this.cursorForwardTab(this.params
);
2400 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
2402 this.scrollUp(this.params
);
2405 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
2406 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
2409 // if (this.prefix === '>') {
2410 // this.resetTitleModes(this.params);
2413 // if (this.params.length > 2) {
2414 // this.initMouseTracking(this.params);
2417 if (this.params
.length
< 2 && !this.prefix
) {
2418 this.scrollDown(this.params
);
2423 // Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
2425 this.cursorBackwardTab(this.params
);
2428 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
2430 this.repeatPrecedingCharacter(this.params
);
2433 // CSI Ps g Tab Clear (TBC).
2435 this.tabClear(this.params
);
2438 // CSI Pm i Media Copy (MC).
2441 // this.mediaCopy(this.params);
2444 // CSI Pm m Character Attributes (SGR).
2446 // case 'm': // duplicate
2447 // if (this.prefix === '>') {
2448 // this.setResources(this.params);
2450 // this.charAttributes(this.params);
2454 // CSI Ps n Device Status Report (DSR).
2456 // case 'n': // duplicate
2457 // if (this.prefix === '>') {
2458 // this.disableModifiers(this.params);
2460 // this.deviceStatus(this.params);
2464 // CSI > Ps p Set pointer mode.
2465 // CSI ! p Soft terminal reset (DECSTR).
2467 // Request ANSI mode (DECRQM).
2469 // Request DEC private mode (DECRQM).
2472 switch (this.prefix
) {
2474 // this.setPointerMode(this.params);
2477 this.softReset(this.params
);
2480 // if (this.postfix === '$') {
2481 // this.requestPrivateMode(this.params);
2485 // if (this.postfix === '"') {
2486 // this.setConformanceLevel(this.params);
2487 // } else if (this.postfix === '$') {
2488 // this.requestAnsiMode(this.params);
2494 // CSI Ps q Load LEDs (DECLL).
2498 // if (this.postfix === ' ') {
2499 // this.setCursorStyle(this.params);
2502 // if (this.postfix === '"') {
2503 // this.setCharProtectionAttr(this.params);
2506 // this.loadLEDs(this.params);
2510 // Set Scrolling Region [top;bottom] (default = full size of win-
2513 // CSI Pt; Pl; Pb; Pr; Ps$ r
2514 // case 'r': // duplicate
2515 // if (this.prefix === '?') {
2516 // this.restorePrivateValues(this.params);
2517 // } else if (this.postfix === '$') {
2518 // this.setAttrInRectangle(this.params);
2520 // this.setScrollRegion(this.params);
2524 // CSI s Save cursor (ANSI.SYS).
2526 // case 's': // duplicate
2527 // if (this.prefix === '?') {
2528 // this.savePrivateValues(this.params);
2530 // this.saveCursor(this.params);
2534 // CSI Ps ; Ps ; Ps t
2535 // CSI Pt; Pl; Pb; Pr; Ps$ t
2539 // if (this.postfix === '$') {
2540 // this.reverseAttrInRectangle(this.params);
2541 // } else if (this.postfix === ' ') {
2542 // this.setWarningBellVolume(this.params);
2544 // if (this.prefix === '>') {
2545 // this.setTitleModeFeature(this.params);
2547 // this.manipulateWindow(this.params);
2552 // CSI u Restore cursor (ANSI.SYS).
2554 // case 'u': // duplicate
2555 // if (this.postfix === ' ') {
2556 // this.setMarginBellVolume(this.params);
2558 // this.restoreCursor(this.params);
2562 // CSI Pt; Pl; Pb; Pr; Pp; Pt; Pl; Pp$ v
2564 // if (this.postfix === '$') {
2565 // this.copyRectagle(this.params);
2569 // CSI Pt ; Pl ; Pb ; Pr ' w
2571 // if (this.postfix === '\'') {
2572 // this.enableFilterRectangle(this.params);
2576 // CSI Ps x Request Terminal Parameters (DECREQTPARM).
2577 // CSI Ps x Select Attribute Change Extent (DECSACE).
2578 // CSI Pc; Pt; Pl; Pb; Pr$ x
2580 // if (this.postfix === '$') {
2581 // this.fillRectangle(this.params);
2583 // this.requestParameters(this.params);
2584 // //this.__(this.params);
2589 // CSI Pt; Pl; Pb; Pr$ z
2591 // if (this.postfix === '\'') {
2592 // this.enableLocatorReporting(this.params);
2593 // } else if (this.postfix === '$') {
2594 // this.eraseRectangle(this.params);
2599 // CSI Pt; Pl; Pb; Pr$ {
2601 // if (this.postfix === '\'') {
2602 // this.setLocatorEvents(this.params);
2603 // } else if (this.postfix === '$') {
2604 // this.selectiveEraseRectangle(this.params);
2610 // if (this.postfix === '\'') {
2611 // this.requestLocatorPosition(this.params);
2616 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
2618 // if (this.postfix === ' ') {
2619 // this.insertColumns(this.params);
2624 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
2626 // if (this.postfix === ' ') {
2627 // this.deleteColumns(this.params);
2632 this.error('Unknown CSI code: %s.', ch
);
2641 if (ch
=== '\x1b' || ch
=== '\x07') {
2642 if (ch
=== '\x1b') i
++;
2644 switch (this.prefix
) {
2645 // User-Defined Keys (DECUDK).
2649 // Request Status String (DECRQSS).
2650 // test: echo -e '\eP$q"p\e\\'
2652 var pt
= this.currentParam
2669 + (this.scrollTop
+ 1)
2671 + (this.scrollBottom
+ 1)
2681 this.error('Unknown DCS Pt: %s.', pt
);
2686 this.send('\x1bP' + +valid
+ '$r' + pt
+ '\x1b\\');
2689 // Set Termcap/Terminfo Data (xterm, experimental).
2693 // Request Termcap/Terminfo String (xterm, experimental)
2694 // Regular xterm does not even respond to this sequence.
2695 // This can cause a small glitch in vim.
2696 // test: echo -ne '\eP+q6b64\e\\'
2698 var pt
= this.currentParam
2701 this.send('\x1bP' + +valid
+ '+r' + pt
+ '\x1b\\');
2705 this.error('Unknown DCS prefix: %s.', this.prefix
);
2709 this.currentParam
= 0;
2711 this.state
= normal
;
2712 } else if (!this.currentParam
) {
2713 if (!this.prefix
&& ch
!== '$' && ch
!== '+') {
2714 this.currentParam
= ch
;
2715 } else if (this.prefix
.length
=== 2) {
2716 this.currentParam
= ch
;
2721 this.currentParam
+= ch
;
2727 if (ch
=== '\x1b' || ch
=== '\x07') {
2728 if (ch
=== '\x1b') i
++;
2729 this.state
= normal
;
2735 this.updateRange(this.y
);
2736 this.refresh(this.refreshStart
, this.refreshEnd
);
2740 * Writes text to the terminal, followed by a break line character (\n).
2741 * @param {string} text The text to write to the terminal.
2743 Terminal
.prototype.writeln = function(data
) {
2744 this.write(data
+ '\r\n');
2748 * Attaches a custom keydown handler which is run before keys are processed, giving consumers of
2749 * xterm.js ultimate control as to what keys should be processed by the terminal and what keys
2751 * @param {function} customKeydownHandler The custom KeyboardEvent handler to attach. This is a
2752 * function that takes a KeyboardEvent, allowing consumers to stop propogation and/or prevent
2753 * the default action. The function returns whether the event should be processed by xterm.js.
2755 Terminal
.prototype.attachCustomKeydownHandler = function(customKeydownHandler
) {
2756 this.customKeydownHandler
= customKeydownHandler
;
2760 * Handle a keydown event
2762 * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
2763 * @param {KeyboardEvent} ev The keydown event to be handled.
2765 Terminal
.prototype.keyDown = function(ev
) {
2766 if (this.customKeydownHandler
&& this.customKeydownHandler(ev
) === false) {
2770 if (!this.compositionHelper
.keydown
.bind(this.compositionHelper
)(ev
)) {
2775 var result
= this.evaluateKeyEscapeSequence(ev
);
2777 if (result
.scrollDisp
) {
2778 this.scrollDisp(result
.scrollDisp
);
2779 return this.cancel(ev
);
2782 if (isThirdLevelShift(this, ev
)) {
2786 if (result
.cancel
) {
2787 // The event is canceled at the end already, is this necessary?
2788 this.cancel(ev
, true);
2795 this.emit('keydown', ev
);
2796 this.emit('key', result
.key
, ev
);
2798 this.handler(result
.key
);
2800 return this.cancel(ev
, true);
2804 * Returns an object that determines how a KeyboardEvent should be handled. The key of the
2805 * returned value is the new key code to pass to the PTY.
2807 * Reference: http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
2808 * @param {KeyboardEvent} ev The keyboard event to be translated to key escape sequence.
2810 Terminal
.prototype.evaluateKeyEscapeSequence = function(ev
) {
2812 // Whether to cancel event propogation (NOTE: this may not be needed since the event is
2813 // canceled at the end of keyDown
2815 // The new key even to emit
2817 // The number of characters to scroll, if this is defined it will cancel the event
2818 scrollDisp
: undefined
2820 var modifiers
= ev
.shiftKey
<< 0 | ev
.altKey
<< 1 | ev
.ctrlKey
<< 2 | ev
.metaKey
<< 3;
2821 switch (ev
.keyCode
) {
2825 result
.key
= '\x08'; // ^H
2828 result
.key
= '\x7f'; // ^?
2833 result
.key
= '\x1b[Z';
2837 result
.cancel
= true;
2842 result
.cancel
= true;
2846 result
.key
= '\x1b';
2847 result
.cancel
= true;
2852 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'D';
2853 else if (this.applicationCursor
)
2854 result
.key
= '\x1bOD';
2856 result
.key
= '\x1b[D';
2861 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'C';
2862 else if (this.applicationCursor
)
2863 result
.key
= '\x1bOC';
2865 result
.key
= '\x1b[C';
2870 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'A';
2871 else if (this.applicationCursor
)
2872 result
.key
= '\x1bOA';
2874 result
.key
= '\x1b[A';
2879 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'B';
2880 else if (this.applicationCursor
)
2881 result
.key
= '\x1bOB';
2883 result
.key
= '\x1b[B';
2887 if (!ev
.shiftKey
&& !ev
.ctrlKey
) {
2888 // <Ctrl> or <Shift> + <Insert> are used to
2889 // copy-paste on some systems.
2890 result
.key
= '\x1b[2~';
2894 case 46: result
.key
= '\x1b[3~'; break;
2898 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'H';
2899 else if (this.applicationCursor
)
2900 result
.key
= '\x1bOH';
2902 result
.key
= '\x1b[H';
2907 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'F';
2908 else if (this.applicationCursor
)
2909 result
.key
= '\x1bOF';
2911 result
.key
= '\x1b[F';
2916 result
.scrollDisp
= -(this.rows
- 1);
2918 result
.key
= '\x1b[5~';
2924 result
.scrollDisp
= this.rows
- 1;
2926 result
.key
= '\x1b[6~';
2930 case 112: result
.key
= '\x1bOP'; break;
2931 case 113: result
.key
= '\x1bOQ'; break;
2932 case 114: result
.key
= '\x1bOR'; break;
2933 case 115: result
.key
= '\x1bOS'; break;
2934 case 116: result
.key
= '\x1b[15~'; break;
2935 case 117: result
.key
= '\x1b[17~'; break;
2936 case 118: result
.key
= '\x1b[18~'; break;
2937 case 119: result
.key
= '\x1b[19~'; break;
2938 case 120: result
.key
= '\x1b[20~'; break;
2939 case 121: result
.key
= '\x1b[21~'; break;
2940 case 122: result
.key
= '\x1b[23~'; break;
2941 case 123: result
.key
= '\x1b[24~'; break;
2944 if (ev
.ctrlKey
&& !ev
.shiftKey
&& !ev
.altKey
&& !ev
.metaKey
) {
2945 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2946 result
.key
= String
.fromCharCode(ev
.keyCode
- 64);
2947 } else if (ev
.keyCode
=== 32) {
2949 result
.key
= String
.fromCharCode(0);
2950 } else if (ev
.keyCode
>= 51 && ev
.keyCode
<= 55) {
2951 // escape, file sep, group sep, record sep, unit sep
2952 result
.key
= String
.fromCharCode(ev
.keyCode
- 51 + 27);
2953 } else if (ev
.keyCode
=== 56) {
2955 result
.key
= String
.fromCharCode(127);
2956 } else if (ev
.keyCode
=== 219) {
2958 result
.key
= String
.fromCharCode(27);
2959 } else if (ev
.keyCode
=== 221) {
2961 result
.key
= String
.fromCharCode(29);
2963 } else if (!this.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) {
2964 // On Mac this is a third level shift. Use <Esc> instead.
2965 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2966 result
.key
= '\x1b' + String
.fromCharCode(ev
.keyCode
+ 32);
2967 } else if (ev
.keyCode
=== 192) {
2968 result
.key
= '\x1b`';
2969 } else if (ev
.keyCode
>= 48 && ev
.keyCode
<= 57) {
2970 result
.key
= '\x1b' + (ev
.keyCode
- 48);
2979 * Set the G level of the terminal
2982 Terminal
.prototype.setgLevel = function(g
) {
2984 this.charset
= this.charsets
[g
];
2988 * Set the charset for the given G level of the terminal
2992 Terminal
.prototype.setgCharset = function(g
, charset
) {
2993 this.charsets
[g
] = charset
;
2994 if (this.glevel
=== g
) {
2995 this.charset
= charset
;
3000 * Handle a keypress event.
3002 * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
3003 * @param {KeyboardEvent} ev The keypress event to be handled.
3005 Terminal
.prototype.keyPress = function(ev
) {
3012 } else if (ev
.which
== null) {
3014 } else if (ev
.which
!== 0 && ev
.charCode
!== 0) {
3021 (ev
.altKey
|| ev
.ctrlKey
|| ev
.metaKey
) && !isThirdLevelShift(this, ev
)
3026 key
= String
.fromCharCode(key
);
3028 this.emit('keypress', key
, ev
);
3029 this.emit('key', key
, ev
);
3037 * Send data for handling to the terminal
3038 * @param {string} data
3040 Terminal
.prototype.send = function(data
) {
3044 setTimeout(function() {
3045 self
.handler(self
.queue
);
3055 * Note: We could do sweet things with webaudio here
3057 Terminal
.prototype.bell = function() {
3058 if (!this.visualBell
) return;
3060 this.element
.style
.borderColor
= 'white';
3061 setTimeout(function() {
3062 self
.element
.style
.borderColor
= '';
3064 if (this.popOnBell
) this.focus();
3068 * Log the current state to the console.
3070 Terminal
.prototype.log = function() {
3071 if (!this.debug
) return;
3072 if (!this.context
.console
|| !this.context
.console
.log
) return;
3073 var args
= Array
.prototype.slice
.call(arguments
);
3074 this.context
.console
.log
.apply(this.context
.console
, args
);
3078 * Log the current state as error to the console.
3080 Terminal
.prototype.error = function() {
3081 if (!this.debug
) return;
3082 if (!this.context
.console
|| !this.context
.console
.error
) return;
3083 var args
= Array
.prototype.slice
.call(arguments
);
3084 this.context
.console
.error
.apply(this.context
.console
, args
);
3088 * Resizes the terminal.
3090 * @param {number} x The number of columns to resize to.
3091 * @param {number} y The number of rows to resize to.
3093 Terminal
.prototype.resize = function(x
, y
) {
3101 if (x
=== this.cols
&& y
=== this.rows
) {
3111 ch
= [this.defAttr
, ' ', 1]; // does xterm use the default attr?
3112 i
= this.lines
.length
;
3114 while (this.lines
[i
].length
< x
) {
3115 this.lines
[i
].push(ch
);
3119 i
= this.lines
.length
;
3121 while (this.lines
[i
].length
> x
) {
3122 this.lines
[i
].pop();
3135 // y is rows, not this.y
3136 if (this.lines
.length
< y
+ this.ybase
) {
3137 if (this.ybase
> 0 && this.lines
.length
<= this.ybase
+ this.y
+ addToY
+ 1) {
3138 // There is room above the buffer and there are no empty elements below the line,
3142 if (this.ydisp
> 0) {
3143 // Viewport is at the top of the buffer, must increase downwards
3147 // Add a blank line if there is no buffer left at the top to scroll to, or if there
3148 // are blank lines after the cursor
3149 this.lines
.push(this.blankLine());
3152 if (this.children
.length
< y
) {
3158 if (this.lines
.length
> y
+ this.ybase
) {
3159 if (this.lines
.length
> this.ybase
+ this.y
+ 1) {
3160 // The line is a blank line below the cursor, remove it
3163 // The line is the cursor, scroll down
3168 if (this.children
.length
> y
) {
3169 el
= this.children
.shift();
3171 el
.parentNode
.removeChild(el
);
3178 * Make sure that the cursor stays on screen
3192 this.scrollBottom
= y
- 1;
3194 this.refresh(0, this.rows
- 1);
3198 this.emit('resize', {terminal
: this, cols
: x
, rows
: y
});
3202 * Updates the range of rows to refresh
3203 * @param {number} y The number of rows to refresh next.
3205 Terminal
.prototype.updateRange = function(y
) {
3206 if (y
< this.refreshStart
) this.refreshStart
= y
;
3207 if (y
> this.refreshEnd
) this.refreshEnd
= y
;
3208 // if (y > this.refreshEnd) {
3209 // this.refreshEnd = y;
3210 // if (y > this.rows - 1) {
3211 // this.refreshEnd = this.rows - 1;
3217 * Set the range of refreshing to the maximyum value
3219 Terminal
.prototype.maxRange = function() {
3220 this.refreshStart
= 0;
3221 this.refreshEnd
= this.rows
- 1;
3227 * Setup the tab stops.
3230 Terminal
.prototype.setupStops = function(i
) {
3232 if (!this.tabs
[i
]) {
3233 i
= this.prevStop(i
);
3240 for (; i
< this.cols
; i
+= 8) {
3241 this.tabs
[i
] = true;
3247 * Move the cursor to the previous tab stop from the given position (default is current).
3248 * @param {number} x The position to move the cursor to the previous tab stop.
3250 Terminal
.prototype.prevStop = function(x
) {
3251 if (x
== null) x
= this.x
;
3252 while (!this.tabs
[--x
] && x
> 0);
3253 return x
>= this.cols
3260 * Move the cursor one tab stop forward from the given position (default is current).
3261 * @param {number} x The position to move the cursor one tab stop forward.
3263 Terminal
.prototype.nextStop = function(x
) {
3264 if (x
== null) x
= this.x
;
3265 while (!this.tabs
[++x
] && x
< this.cols
);
3266 return x
>= this.cols
3273 * Erase in the identified line everything from "x" to the end of the line (right).
3274 * @param {number} x The column from which to start erasing to the end of the line.
3275 * @param {number} y The line in which to operate.
3277 Terminal
.prototype.eraseRight = function(x
, y
) {
3278 var line
= this.lines
[this.ybase
+ y
]
3279 , ch
= [this.eraseAttr(), ' ', 1]; // xterm
3282 for (; x
< this.cols
; x
++) {
3286 this.updateRange(y
);
3292 * Erase in the identified line everything from "x" to the start of the line (left).
3293 * @param {number} x The column from which to start erasing to the start of the line.
3294 * @param {number} y The line in which to operate.
3296 Terminal
.prototype.eraseLeft = function(x
, y
) {
3297 var line
= this.lines
[this.ybase
+ y
]
3298 , ch
= [this.eraseAttr(), ' ', 1]; // xterm
3301 while (x
--) line
[x
] = ch
;
3303 this.updateRange(y
);
3308 * Erase all content in the given line
3309 * @param {number} y The line to erase all of its contents.
3311 Terminal
.prototype.eraseLine = function(y
) {
3312 this.eraseRight(0, y
);
3317 * Return the data array of a blank line/
3318 * @param {number} cur First bunch of data for each "blank" character.
3320 Terminal
.prototype.blankLine = function(cur
) {
3325 var ch
= [attr
, ' ', 1] // width defaults to 1 halfwidth character
3329 for (; i
< this.cols
; i
++) {
3338 * If cur return the back color xterm feature attribute. Else return defAttr.
3339 * @param {object} cur
3341 Terminal
.prototype.ch = function(cur
) {
3343 ? [this.eraseAttr(), ' ', 1]
3344 : [this.defAttr
, ' ', 1];
3349 * Evaluate if the current erminal is the given argument.
3350 * @param {object} term The terminal to evaluate
3352 Terminal
.prototype.is = function(term
) {
3353 var name
= this.termName
;
3354 return (name
+ '').indexOf(term
) === 0;
3359 * Emit the 'data' event and populate the given data.
3360 * @param {string} data The data to populate in the event.
3362 Terminal
.prototype.handler = function(data
) {
3363 this.emit('data', data
);
3368 * Emit the 'title' event and populate the given title.
3369 * @param {string} title The title to populate in the event.
3371 Terminal
.prototype.handleTitle = function(title
) {
3372 this.emit('title', title
);
3381 * ESC D Index (IND is 0x84).
3383 Terminal
.prototype.index = function() {
3385 if (this.y
> this.scrollBottom
) {
3389 this.state
= normal
;
3394 * ESC M Reverse Index (RI is 0x8d).
3396 Terminal
.prototype.reverseIndex = function() {
3399 if (this.y
< this.scrollTop
) {
3401 // possibly move the code below to term.reverseScroll();
3402 // test: echo -ne '\e[1;1H\e[44m\eM\e[0m'
3403 // blankLine(true) is xterm/linux behavior
3404 this.lines
.splice(this.y
+ this.ybase
, 0, this.blankLine(true));
3405 j
= this.rows
- 1 - this.scrollBottom
;
3406 this.lines
.splice(this.rows
- 1 + this.ybase
- j
+ 1, 1);
3408 this.updateRange(this.scrollTop
);
3409 this.updateRange(this.scrollBottom
);
3411 this.state
= normal
;
3416 * ESC c Full Reset (RIS).
3418 Terminal
.prototype.reset = function() {
3419 this.options
.rows
= this.rows
;
3420 this.options
.cols
= this.cols
;
3421 Terminal
.call(this, this.options
);
3422 this.refresh(0, this.rows
- 1);
3427 * ESC H Tab Set (HTS is 0x88).
3429 Terminal
.prototype.tabSet = function() {
3430 this.tabs
[this.x
] = true;
3431 this.state
= normal
;
3441 * Cursor Up Ps Times (default = 1) (CUU).
3443 Terminal
.prototype.cursorUp = function(params
) {
3444 var param
= params
[0];
3445 if (param
< 1) param
= 1;
3447 if (this.y
< 0) this.y
= 0;
3453 * Cursor Down Ps Times (default = 1) (CUD).
3455 Terminal
.prototype.cursorDown = function(params
) {
3456 var param
= params
[0];
3457 if (param
< 1) param
= 1;
3459 if (this.y
>= this.rows
) {
3460 this.y
= this.rows
- 1;
3467 * Cursor Forward Ps Times (default = 1) (CUF).
3469 Terminal
.prototype.cursorForward = function(params
) {
3470 var param
= params
[0];
3471 if (param
< 1) param
= 1;
3473 if (this.x
>= this.cols
) {
3474 this.x
= this.cols
- 1;
3481 * Cursor Backward Ps Times (default = 1) (CUB).
3483 Terminal
.prototype.cursorBackward = function(params
) {
3484 var param
= params
[0];
3485 if (param
< 1) param
= 1;
3487 if (this.x
< 0) this.x
= 0;
3493 * Cursor Position [row;column] (default = [1,1]) (CUP).
3495 Terminal
.prototype.cursorPos = function(params
) {
3498 row
= params
[0] - 1;
3500 if (params
.length
>= 2) {
3501 col
= params
[1] - 1;
3508 } else if (row
>= this.rows
) {
3509 row
= this.rows
- 1;
3514 } else if (col
>= this.cols
) {
3515 col
= this.cols
- 1;
3524 * CSI Ps J Erase in Display (ED).
3525 * Ps = 0 -> Erase Below (default).
3526 * Ps = 1 -> Erase Above.
3527 * Ps = 2 -> Erase All.
3528 * Ps = 3 -> Erase Saved Lines (xterm).
3530 * Erase in Display (DECSED).
3531 * Ps = 0 -> Selective Erase Below (default).
3532 * Ps = 1 -> Selective Erase Above.
3533 * Ps = 2 -> Selective Erase All.
3535 Terminal
.prototype.eraseInDisplay = function(params
) {
3537 switch (params
[0]) {
3539 this.eraseRight(this.x
, this.y
);
3541 for (; j
< this.rows
; j
++) {
3546 this.eraseLeft(this.x
, this.y
);
3554 while (j
--) this.eraseLine(j
);
3564 * CSI Ps K Erase in Line (EL).
3565 * Ps = 0 -> Erase to Right (default).
3566 * Ps = 1 -> Erase to Left.
3567 * Ps = 2 -> Erase All.
3569 * Erase in Line (DECSEL).
3570 * Ps = 0 -> Selective Erase to Right (default).
3571 * Ps = 1 -> Selective Erase to Left.
3572 * Ps = 2 -> Selective Erase All.
3574 Terminal
.prototype.eraseInLine = function(params
) {
3575 switch (params
[0]) {
3577 this.eraseRight(this.x
, this.y
);
3580 this.eraseLeft(this.x
, this.y
);
3583 this.eraseLine(this.y
);
3590 * CSI Pm m Character Attributes (SGR).
3591 * Ps = 0 -> Normal (default).
3593 * Ps = 4 -> Underlined.
3594 * Ps = 5 -> Blink (appears as Bold).
3595 * Ps = 7 -> Inverse.
3596 * Ps = 8 -> Invisible, i.e., hidden (VT300).
3597 * Ps = 2 2 -> Normal (neither bold nor faint).
3598 * Ps = 2 4 -> Not underlined.
3599 * Ps = 2 5 -> Steady (not blinking).
3600 * Ps = 2 7 -> Positive (not inverse).
3601 * Ps = 2 8 -> Visible, i.e., not hidden (VT300).
3602 * Ps = 3 0 -> Set foreground color to Black.
3603 * Ps = 3 1 -> Set foreground color to Red.
3604 * Ps = 3 2 -> Set foreground color to Green.
3605 * Ps = 3 3 -> Set foreground color to Yellow.
3606 * Ps = 3 4 -> Set foreground color to Blue.
3607 * Ps = 3 5 -> Set foreground color to Magenta.
3608 * Ps = 3 6 -> Set foreground color to Cyan.
3609 * Ps = 3 7 -> Set foreground color to White.
3610 * Ps = 3 9 -> Set foreground color to default (original).
3611 * Ps = 4 0 -> Set background color to Black.
3612 * Ps = 4 1 -> Set background color to Red.
3613 * Ps = 4 2 -> Set background color to Green.
3614 * Ps = 4 3 -> Set background color to Yellow.
3615 * Ps = 4 4 -> Set background color to Blue.
3616 * Ps = 4 5 -> Set background color to Magenta.
3617 * Ps = 4 6 -> Set background color to Cyan.
3618 * Ps = 4 7 -> Set background color to White.
3619 * Ps = 4 9 -> Set background color to default (original).
3621 * If 16-color support is compiled, the following apply. Assume
3622 * that xterm's resources are set so that the ISO color codes are
3623 * the first 8 of a set of 16. Then the aixterm colors are the
3624 * bright versions of the ISO colors:
3625 * Ps = 9 0 -> Set foreground color to Black.
3626 * Ps = 9 1 -> Set foreground color to Red.
3627 * Ps = 9 2 -> Set foreground color to Green.
3628 * Ps = 9 3 -> Set foreground color to Yellow.
3629 * Ps = 9 4 -> Set foreground color to Blue.
3630 * Ps = 9 5 -> Set foreground color to Magenta.
3631 * Ps = 9 6 -> Set foreground color to Cyan.
3632 * Ps = 9 7 -> Set foreground color to White.
3633 * Ps = 1 0 0 -> Set background color to Black.
3634 * Ps = 1 0 1 -> Set background color to Red.
3635 * Ps = 1 0 2 -> Set background color to Green.
3636 * Ps = 1 0 3 -> Set background color to Yellow.
3637 * Ps = 1 0 4 -> Set background color to Blue.
3638 * Ps = 1 0 5 -> Set background color to Magenta.
3639 * Ps = 1 0 6 -> Set background color to Cyan.
3640 * Ps = 1 0 7 -> Set background color to White.
3642 * If xterm is compiled with the 16-color support disabled, it
3643 * supports the following, from rxvt:
3644 * Ps = 1 0 0 -> Set foreground and background color to
3647 * If 88- or 256-color support is compiled, the following apply.
3648 * Ps = 3 8 ; 5 ; Ps -> Set foreground color to the second
3650 * Ps = 4 8 ; 5 ; Ps -> Set background color to the second
3653 Terminal
.prototype.charAttributes = function(params
) {
3654 // Optimize a single SGR0.
3655 if (params
.length
=== 1 && params
[0] === 0) {
3656 this.curAttr
= this.defAttr
;
3660 var l
= params
.length
3662 , flags
= this.curAttr
>> 18
3663 , fg
= (this.curAttr
>> 9) & 0x1ff
3664 , bg
= this.curAttr
& 0x1ff
3667 for (; i
< l
; i
++) {
3669 if (p
>= 30 && p
<= 37) {
3672 } else if (p
>= 40 && p
<= 47) {
3675 } else if (p
>= 90 && p
<= 97) {
3679 } else if (p
>= 100 && p
<= 107) {
3683 } else if (p
=== 0) {
3685 flags
= this.defAttr
>> 18;
3686 fg
= (this.defAttr
>> 9) & 0x1ff;
3687 bg
= this.defAttr
& 0x1ff;
3691 } else if (p
=== 1) {
3694 } else if (p
=== 4) {
3697 } else if (p
=== 5) {
3700 } else if (p
=== 7) {
3701 // inverse and positive
3702 // test with: echo -e '\e[31m\e[42mhello\e[7mworld\e[27mhi\e[m'
3704 } else if (p
=== 8) {
3707 } else if (p
=== 22) {
3710 } else if (p
=== 24) {
3713 } else if (p
=== 25) {
3716 } else if (p
=== 27) {
3719 } else if (p
=== 28) {
3722 } else if (p
=== 39) {
3724 fg
= (this.defAttr
>> 9) & 0x1ff;
3725 } else if (p
=== 49) {
3727 bg
= this.defAttr
& 0x1ff;
3728 } else if (p
=== 38) {
3730 if (params
[i
+ 1] === 2) {
3734 params
[i
+ 1] & 0xff,
3735 params
[i
+ 2] & 0xff);
3736 if (fg
=== -1) fg
= 0x1ff;
3738 } else if (params
[i
+ 1] === 5) {
3740 p
= params
[i
] & 0xff;
3743 } else if (p
=== 48) {
3745 if (params
[i
+ 1] === 2) {
3749 params
[i
+ 1] & 0xff,
3750 params
[i
+ 2] & 0xff);
3751 if (bg
=== -1) bg
= 0x1ff;
3753 } else if (params
[i
+ 1] === 5) {
3755 p
= params
[i
] & 0xff;
3758 } else if (p
=== 100) {
3760 fg
= (this.defAttr
>> 9) & 0x1ff;
3761 bg
= this.defAttr
& 0x1ff;
3763 this.error('Unknown SGR attribute: %d.', p
);
3767 this.curAttr
= (flags
<< 18) | (fg
<< 9) | bg
;
3772 * CSI Ps n Device Status Report (DSR).
3773 * Ps = 5 -> Status Report. Result (``OK'') is
3775 * Ps = 6 -> Report Cursor Position (CPR) [row;column].
3779 * Device Status Report (DSR, DEC-specific).
3780 * Ps = 6 -> Report Cursor Position (CPR) [row;column] as CSI
3781 * ? r ; c R (assumes page is zero).
3782 * Ps = 1 5 -> Report Printer status as CSI ? 1 0 n (ready).
3783 * or CSI ? 1 1 n (not ready).
3784 * Ps = 2 5 -> Report UDK status as CSI ? 2 0 n (unlocked)
3785 * or CSI ? 2 1 n (locked).
3786 * Ps = 2 6 -> Report Keyboard status as
3787 * CSI ? 2 7 ; 1 ; 0 ; 0 n (North American).
3788 * The last two parameters apply to VT400 & up, and denote key-
3789 * board ready and LK01 respectively.
3790 * Ps = 5 3 -> Report Locator status as
3791 * CSI ? 5 3 n Locator available, if compiled-in, or
3792 * CSI ? 5 0 n No Locator, if not.
3794 Terminal
.prototype.deviceStatus = function(params
) {
3796 switch (params
[0]) {
3799 this.send('\x1b[0n');
3810 } else if (this.prefix
=== '?') {
3811 // modern xterm doesnt seem to
3812 // respond to any of these except ?6, 6, and 5
3813 switch (params
[0]) {
3824 // this.send('\x1b[?11n');
3827 // dont support user defined keys
3828 // this.send('\x1b[?21n');
3831 // north american keyboard
3832 // this.send('\x1b[?27;1;0;0n');
3835 // no dec locator/mouse
3836 // this.send('\x1b[?50n');
3849 * Insert Ps (Blank) Character(s) (default = 1) (ICH).
3851 Terminal
.prototype.insertChars = function(params
) {
3852 var param
, row
, j
, ch
;
3855 if (param
< 1) param
= 1;
3857 row
= this.y
+ this.ybase
;
3859 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3861 while (param
-- && j
< this.cols
) {
3862 this.lines
[row
].splice(j
++, 0, ch
);
3863 this.lines
[row
].pop();
3869 * Cursor Next Line Ps Times (default = 1) (CNL).
3870 * same as CSI Ps B ?
3872 Terminal
.prototype.cursorNextLine = function(params
) {
3873 var param
= params
[0];
3874 if (param
< 1) param
= 1;
3876 if (this.y
>= this.rows
) {
3877 this.y
= this.rows
- 1;
3885 * Cursor Preceding Line Ps Times (default = 1) (CNL).
3888 Terminal
.prototype.cursorPrecedingLine = function(params
) {
3889 var param
= params
[0];
3890 if (param
< 1) param
= 1;
3892 if (this.y
< 0) this.y
= 0;
3899 * Cursor Character Absolute [column] (default = [row,1]) (CHA).
3901 Terminal
.prototype.cursorCharAbsolute = function(params
) {
3902 var param
= params
[0];
3903 if (param
< 1) param
= 1;
3910 * Insert Ps Line(s) (default = 1) (IL).
3912 Terminal
.prototype.insertLines = function(params
) {
3916 if (param
< 1) param
= 1;
3917 row
= this.y
+ this.ybase
;
3919 j
= this.rows
- 1 - this.scrollBottom
;
3920 j
= this.rows
- 1 + this.ybase
- j
+ 1;
3923 // test: echo -e '\e[44m\e[1L\e[0m'
3924 // blankLine(true) - xterm/linux behavior
3925 this.lines
.splice(row
, 0, this.blankLine(true));
3926 this.lines
.splice(j
, 1);
3930 this.updateRange(this.y
);
3931 this.updateRange(this.scrollBottom
);
3937 * Delete Ps Line(s) (default = 1) (DL).
3939 Terminal
.prototype.deleteLines = function(params
) {
3943 if (param
< 1) param
= 1;
3944 row
= this.y
+ this.ybase
;
3946 j
= this.rows
- 1 - this.scrollBottom
;
3947 j
= this.rows
- 1 + this.ybase
- j
;
3950 // test: echo -e '\e[44m\e[1M\e[0m'
3951 // blankLine(true) - xterm/linux behavior
3952 this.lines
.splice(j
+ 1, 0, this.blankLine(true));
3953 this.lines
.splice(row
, 1);
3957 this.updateRange(this.y
);
3958 this.updateRange(this.scrollBottom
);
3964 * Delete Ps Character(s) (default = 1) (DCH).
3966 Terminal
.prototype.deleteChars = function(params
) {
3970 if (param
< 1) param
= 1;
3972 row
= this.y
+ this.ybase
;
3973 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3976 this.lines
[row
].splice(this.x
, 1);
3977 this.lines
[row
].push(ch
);
3983 * Erase Ps Character(s) (default = 1) (ECH).
3985 Terminal
.prototype.eraseChars = function(params
) {
3986 var param
, row
, j
, ch
;
3989 if (param
< 1) param
= 1;
3991 row
= this.y
+ this.ybase
;
3993 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3995 while (param
-- && j
< this.cols
) {
3996 this.lines
[row
][j
++] = ch
;
4001 * CSI Pm ` Character Position Absolute
4002 * [column] (default = [row,1]) (HPA).
4004 Terminal
.prototype.charPosAbsolute = function(params
) {
4005 var param
= params
[0];
4006 if (param
< 1) param
= 1;
4008 if (this.x
>= this.cols
) {
4009 this.x
= this.cols
- 1;
4016 * Horizontal Position Relative
4019 Terminal
.prototype.HPositionRelative = function(params
) {
4020 var param
= params
[0];
4021 if (param
< 1) param
= 1;
4023 if (this.x
>= this.cols
) {
4024 this.x
= this.cols
- 1;
4030 * CSI Ps c Send Device Attributes (Primary DA).
4031 * Ps = 0 or omitted -> request attributes from terminal. The
4032 * response depends on the decTerminalID resource setting.
4033 * -> CSI ? 1 ; 2 c (``VT100 with Advanced Video Option'')
4034 * -> CSI ? 1 ; 0 c (``VT101 with No Options'')
4035 * -> CSI ? 6 c (``VT102'')
4036 * -> CSI ? 6 0 ; 1 ; 2 ; 6 ; 8 ; 9 ; 1 5 ; c (``VT220'')
4037 * The VT100-style response parameters do not mean anything by
4038 * themselves. VT220 parameters do, telling the host what fea-
4039 * tures the terminal supports:
4040 * Ps = 1 -> 132-columns.
4041 * Ps = 2 -> Printer.
4042 * Ps = 6 -> Selective erase.
4043 * Ps = 8 -> User-defined keys.
4044 * Ps = 9 -> National replacement character sets.
4045 * Ps = 1 5 -> Technical characters.
4046 * Ps = 2 2 -> ANSI color, e.g., VT525.
4047 * Ps = 2 9 -> ANSI text locator (i.e., DEC Locator mode).
4049 * Send Device Attributes (Secondary DA).
4050 * Ps = 0 or omitted -> request the terminal's identification
4051 * code. The response depends on the decTerminalID resource set-
4052 * ting. It should apply only to VT220 and up, but xterm extends
4054 * -> CSI > Pp ; Pv ; Pc c
4055 * where Pp denotes the terminal type
4056 * Pp = 0 -> ``VT100''.
4057 * Pp = 1 -> ``VT220''.
4058 * and Pv is the firmware version (for xterm, this was originally
4059 * the XFree86 patch number, starting with 95). In a DEC termi-
4060 * nal, Pc indicates the ROM cartridge registration number and is
4063 * xterm/charproc.c - line 2012, for more information.
4064 * vim responds with ^[[?0c or ^[[?1c after the terminal's response (?)
4066 Terminal
.prototype.sendDeviceAttributes = function(params
) {
4067 if (params
[0] > 0) return;
4070 if (this.is('xterm')
4071 || this.is('rxvt-unicode')
4072 || this.is('screen')) {
4073 this.send('\x1b[?1;2c');
4074 } else if (this.is('linux')) {
4075 this.send('\x1b[?6c');
4077 } else if (this.prefix
=== '>') {
4079 // seem to spit this
4080 // out around ~370 times (?).
4081 if (this.is('xterm')) {
4082 this.send('\x1b[>0;276;0c');
4083 } else if (this.is('rxvt-unicode')) {
4084 this.send('\x1b[>85;95;0c');
4085 } else if (this.is('linux')) {
4086 // not supported by linux console.
4087 // linux console echoes parameters.
4088 this.send(params
[0] + 'c');
4089 } else if (this.is('screen')) {
4090 this.send('\x1b[>83;40003;0c');
4098 * Line Position Absolute [row] (default = [1,column]) (VPA).
4100 Terminal
.prototype.linePosAbsolute = function(params
) {
4101 var param
= params
[0];
4102 if (param
< 1) param
= 1;
4104 if (this.y
>= this.rows
) {
4105 this.y
= this.rows
- 1;
4111 * 145 65 e * VPR - Vertical Position Relative
4114 Terminal
.prototype.VPositionRelative = function(params
) {
4115 var param
= params
[0];
4116 if (param
< 1) param
= 1;
4118 if (this.y
>= this.rows
) {
4119 this.y
= this.rows
- 1;
4126 * Horizontal and Vertical Position [row;column] (default =
4129 Terminal
.prototype.HVPosition = function(params
) {
4130 if (params
[0] < 1) params
[0] = 1;
4131 if (params
[1] < 1) params
[1] = 1;
4133 this.y
= params
[0] - 1;
4134 if (this.y
>= this.rows
) {
4135 this.y
= this.rows
- 1;
4138 this.x
= params
[1] - 1;
4139 if (this.x
>= this.cols
) {
4140 this.x
= this.cols
- 1;
4146 * CSI Pm h Set Mode (SM).
4147 * Ps = 2 -> Keyboard Action Mode (AM).
4148 * Ps = 4 -> Insert Mode (IRM).
4149 * Ps = 1 2 -> Send/receive (SRM).
4150 * Ps = 2 0 -> Automatic Newline (LNM).
4152 * DEC Private Mode Set (DECSET).
4153 * Ps = 1 -> Application Cursor Keys (DECCKM).
4154 * Ps = 2 -> Designate USASCII for character sets G0-G3
4155 * (DECANM), and set VT100 mode.
4156 * Ps = 3 -> 132 Column Mode (DECCOLM).
4157 * Ps = 4 -> Smooth (Slow) Scroll (DECSCLM).
4158 * Ps = 5 -> Reverse Video (DECSCNM).
4159 * Ps = 6 -> Origin Mode (DECOM).
4160 * Ps = 7 -> Wraparound Mode (DECAWM).
4161 * Ps = 8 -> Auto-repeat Keys (DECARM).
4162 * Ps = 9 -> Send Mouse X & Y on button press. See the sec-
4163 * tion Mouse Tracking.
4164 * Ps = 1 0 -> Show toolbar (rxvt).
4165 * Ps = 1 2 -> Start Blinking Cursor (att610).
4166 * Ps = 1 8 -> Print form feed (DECPFF).
4167 * Ps = 1 9 -> Set print extent to full screen (DECPEX).
4168 * Ps = 2 5 -> Show Cursor (DECTCEM).
4169 * Ps = 3 0 -> Show scrollbar (rxvt).
4170 * Ps = 3 5 -> Enable font-shifting functions (rxvt).
4171 * Ps = 3 8 -> Enter Tektronix Mode (DECTEK).
4172 * Ps = 4 0 -> Allow 80 -> 132 Mode.
4173 * Ps = 4 1 -> more(1) fix (see curses resource).
4174 * Ps = 4 2 -> Enable Nation Replacement Character sets (DECN-
4176 * Ps = 4 4 -> Turn On Margin Bell.
4177 * Ps = 4 5 -> Reverse-wraparound Mode.
4178 * Ps = 4 6 -> Start Logging. This is normally disabled by a
4179 * compile-time option.
4180 * Ps = 4 7 -> Use Alternate Screen Buffer. (This may be dis-
4181 * abled by the titeInhibit resource).
4182 * Ps = 6 6 -> Application keypad (DECNKM).
4183 * Ps = 6 7 -> Backarrow key sends backspace (DECBKM).
4184 * Ps = 1 0 0 0 -> Send Mouse X & Y on button press and
4185 * release. See the section Mouse Tracking.
4186 * Ps = 1 0 0 1 -> Use Hilite Mouse Tracking.
4187 * Ps = 1 0 0 2 -> Use Cell Motion Mouse Tracking.
4188 * Ps = 1 0 0 3 -> Use All Motion Mouse Tracking.
4189 * Ps = 1 0 0 4 -> Send FocusIn/FocusOut events.
4190 * Ps = 1 0 0 5 -> Enable Extended Mouse Mode.
4191 * Ps = 1 0 1 0 -> Scroll to bottom on tty output (rxvt).
4192 * Ps = 1 0 1 1 -> Scroll to bottom on key press (rxvt).
4193 * Ps = 1 0 3 4 -> Interpret "meta" key, sets eighth bit.
4194 * (enables the eightBitInput resource).
4195 * Ps = 1 0 3 5 -> Enable special modifiers for Alt and Num-
4196 * Lock keys. (This enables the numLock resource).
4197 * Ps = 1 0 3 6 -> Send ESC when Meta modifies a key. (This
4198 * enables the metaSendsEscape resource).
4199 * Ps = 1 0 3 7 -> Send DEL from the editing-keypad Delete
4201 * Ps = 1 0 3 9 -> Send ESC when Alt modifies a key. (This
4202 * enables the altSendsEscape resource).
4203 * Ps = 1 0 4 0 -> Keep selection even if not highlighted.
4204 * (This enables the keepSelection resource).
4205 * Ps = 1 0 4 1 -> Use the CLIPBOARD selection. (This enables
4206 * the selectToClipboard resource).
4207 * Ps = 1 0 4 2 -> Enable Urgency window manager hint when
4208 * Control-G is received. (This enables the bellIsUrgent
4210 * Ps = 1 0 4 3 -> Enable raising of the window when Control-G
4211 * is received. (enables the popOnBell resource).
4212 * Ps = 1 0 4 7 -> Use Alternate Screen Buffer. (This may be
4213 * disabled by the titeInhibit resource).
4214 * Ps = 1 0 4 8 -> Save cursor as in DECSC. (This may be dis-
4215 * abled by the titeInhibit resource).
4216 * Ps = 1 0 4 9 -> Save cursor as in DECSC and use Alternate
4217 * Screen Buffer, clearing it first. (This may be disabled by
4218 * the titeInhibit resource). This combines the effects of the 1
4219 * 0 4 7 and 1 0 4 8 modes. Use this with terminfo-based
4220 * applications rather than the 4 7 mode.
4221 * Ps = 1 0 5 0 -> Set terminfo/termcap function-key mode.
4222 * Ps = 1 0 5 1 -> Set Sun function-key mode.
4223 * Ps = 1 0 5 2 -> Set HP function-key mode.
4224 * Ps = 1 0 5 3 -> Set SCO function-key mode.
4225 * Ps = 1 0 6 0 -> Set legacy keyboard emulation (X11R6).
4226 * Ps = 1 0 6 1 -> Set VT220 keyboard emulation.
4227 * Ps = 2 0 0 4 -> Set bracketed paste mode.
4229 * http: *vt100.net/docs/vt220-rm/chapter4.html
4231 Terminal
.prototype.setMode = function(params
) {
4232 if (typeof params
=== 'object') {
4233 var l
= params
.length
4236 for (; i
< l
; i
++) {
4237 this.setMode(params
[i
]);
4246 this.insertMode
= true;
4249 //this.convertEol = true;
4252 } else if (this.prefix
=== '?') {
4255 this.applicationCursor
= true;
4258 this.setgCharset(0, Terminal
.charsets
.US
);
4259 this.setgCharset(1, Terminal
.charsets
.US
);
4260 this.setgCharset(2, Terminal
.charsets
.US
);
4261 this.setgCharset(3, Terminal
.charsets
.US
);
4262 // set VT100 mode here
4264 case 3: // 132 col mode
4265 this.savedCols
= this.cols
;
4266 this.resize(132, this.rows
);
4269 this.originMode
= true;
4272 this.wraparoundMode
= true;
4275 // this.cursorBlink = true;
4278 this.log('Serial port requested application keypad.');
4279 this.applicationKeypad
= true;
4281 case 9: // X10 Mouse
4282 // no release, no motion, no wheel, no modifiers.
4283 case 1000: // vt200 mouse
4285 // no modifiers, except control on the wheel.
4286 case 1002: // button event mouse
4287 case 1003: // any event mouse
4288 // any event - sends motion events,
4289 // even if there is no button held down.
4290 this.x10Mouse
= params
=== 9;
4291 this.vt200Mouse
= params
=== 1000;
4292 this.normalMouse
= params
> 1000;
4293 this.mouseEvents
= true;
4294 this.element
.style
.cursor
= 'default';
4295 this.log('Binding to mouse events.');
4297 case 1004: // send focusin/focusout events
4300 this.sendFocus
= true;
4302 case 1005: // utf8 ext mode mouse
4303 this.utfMouse
= true;
4304 // for wide terminals
4305 // simply encodes large values as utf8 characters
4307 case 1006: // sgr ext mode mouse
4308 this.sgrMouse
= true;
4309 // for wide terminals
4310 // does not add 32 to fields
4311 // press: ^[[<b;x;yM
4312 // release: ^[[<b;x;ym
4314 case 1015: // urxvt ext mode mouse
4315 this.urxvtMouse
= true;
4316 // for wide terminals
4317 // numbers for fields
4319 // motion: ^[[b;x;yT
4321 case 25: // show cursor
4322 this.cursorHidden
= false;
4324 case 1049: // alt screen buffer cursor
4325 //this.saveCursor();
4327 case 47: // alt screen buffer
4328 case 1047: // alt screen buffer
4336 scrollTop
: this.scrollTop
,
4337 scrollBottom
: this.scrollBottom
,
4339 // XXX save charset(s) here?
4340 // charset: this.charset,
4341 // glevel: this.glevel,
4342 // charsets: this.charsets
4345 this.normal
= normal
;
4354 * CSI Pm l Reset Mode (RM).
4355 * Ps = 2 -> Keyboard Action Mode (AM).
4356 * Ps = 4 -> Replace Mode (IRM).
4357 * Ps = 1 2 -> Send/receive (SRM).
4358 * Ps = 2 0 -> Normal Linefeed (LNM).
4360 * DEC Private Mode Reset (DECRST).
4361 * Ps = 1 -> Normal Cursor Keys (DECCKM).
4362 * Ps = 2 -> Designate VT52 mode (DECANM).
4363 * Ps = 3 -> 80 Column Mode (DECCOLM).
4364 * Ps = 4 -> Jump (Fast) Scroll (DECSCLM).
4365 * Ps = 5 -> Normal Video (DECSCNM).
4366 * Ps = 6 -> Normal Cursor Mode (DECOM).
4367 * Ps = 7 -> No Wraparound Mode (DECAWM).
4368 * Ps = 8 -> No Auto-repeat Keys (DECARM).
4369 * Ps = 9 -> Don't send Mouse X & Y on button press.
4370 * Ps = 1 0 -> Hide toolbar (rxvt).
4371 * Ps = 1 2 -> Stop Blinking Cursor (att610).
4372 * Ps = 1 8 -> Don't print form feed (DECPFF).
4373 * Ps = 1 9 -> Limit print to scrolling region (DECPEX).
4374 * Ps = 2 5 -> Hide Cursor (DECTCEM).
4375 * Ps = 3 0 -> Don't show scrollbar (rxvt).
4376 * Ps = 3 5 -> Disable font-shifting functions (rxvt).
4377 * Ps = 4 0 -> Disallow 80 -> 132 Mode.
4378 * Ps = 4 1 -> No more(1) fix (see curses resource).
4379 * Ps = 4 2 -> Disable Nation Replacement Character sets (DEC-
4381 * Ps = 4 4 -> Turn Off Margin Bell.
4382 * Ps = 4 5 -> No Reverse-wraparound Mode.
4383 * Ps = 4 6 -> Stop Logging. (This is normally disabled by a
4384 * compile-time option).
4385 * Ps = 4 7 -> Use Normal Screen Buffer.
4386 * Ps = 6 6 -> Numeric keypad (DECNKM).
4387 * Ps = 6 7 -> Backarrow key sends delete (DECBKM).
4388 * Ps = 1 0 0 0 -> Don't send Mouse X & Y on button press and
4389 * release. See the section Mouse Tracking.
4390 * Ps = 1 0 0 1 -> Don't use Hilite Mouse Tracking.
4391 * Ps = 1 0 0 2 -> Don't use Cell Motion Mouse Tracking.
4392 * Ps = 1 0 0 3 -> Don't use All Motion Mouse Tracking.
4393 * Ps = 1 0 0 4 -> Don't send FocusIn/FocusOut events.
4394 * Ps = 1 0 0 5 -> Disable Extended Mouse Mode.
4395 * Ps = 1 0 1 0 -> Don't scroll to bottom on tty output
4397 * Ps = 1 0 1 1 -> Don't scroll to bottom on key press (rxvt).
4398 * Ps = 1 0 3 4 -> Don't interpret "meta" key. (This disables
4399 * the eightBitInput resource).
4400 * Ps = 1 0 3 5 -> Disable special modifiers for Alt and Num-
4401 * Lock keys. (This disables the numLock resource).
4402 * Ps = 1 0 3 6 -> Don't send ESC when Meta modifies a key.
4403 * (This disables the metaSendsEscape resource).
4404 * Ps = 1 0 3 7 -> Send VT220 Remove from the editing-keypad
4406 * Ps = 1 0 3 9 -> Don't send ESC when Alt modifies a key.
4407 * (This disables the altSendsEscape resource).
4408 * Ps = 1 0 4 0 -> Do not keep selection when not highlighted.
4409 * (This disables the keepSelection resource).
4410 * Ps = 1 0 4 1 -> Use the PRIMARY selection. (This disables
4411 * the selectToClipboard resource).
4412 * Ps = 1 0 4 2 -> Disable Urgency window manager hint when
4413 * Control-G is received. (This disables the bellIsUrgent
4415 * Ps = 1 0 4 3 -> Disable raising of the window when Control-
4416 * G is received. (This disables the popOnBell resource).
4417 * Ps = 1 0 4 7 -> Use Normal Screen Buffer, clearing screen
4418 * first if in the Alternate Screen. (This may be disabled by
4419 * the titeInhibit resource).
4420 * Ps = 1 0 4 8 -> Restore cursor as in DECRC. (This may be
4421 * disabled by the titeInhibit resource).
4422 * Ps = 1 0 4 9 -> Use Normal Screen Buffer and restore cursor
4423 * as in DECRC. (This may be disabled by the titeInhibit
4424 * resource). This combines the effects of the 1 0 4 7 and 1 0
4425 * 4 8 modes. Use this with terminfo-based applications rather
4426 * than the 4 7 mode.
4427 * Ps = 1 0 5 0 -> Reset terminfo/termcap function-key mode.
4428 * Ps = 1 0 5 1 -> Reset Sun function-key mode.
4429 * Ps = 1 0 5 2 -> Reset HP function-key mode.
4430 * Ps = 1 0 5 3 -> Reset SCO function-key mode.
4431 * Ps = 1 0 6 0 -> Reset legacy keyboard emulation (X11R6).
4432 * Ps = 1 0 6 1 -> Reset keyboard emulation to Sun/PC style.
4433 * Ps = 2 0 0 4 -> Reset bracketed paste mode.
4435 Terminal
.prototype.resetMode = function(params
) {
4436 if (typeof params
=== 'object') {
4437 var l
= params
.length
4440 for (; i
< l
; i
++) {
4441 this.resetMode(params
[i
]);
4450 this.insertMode
= false;
4453 //this.convertEol = false;
4456 } else if (this.prefix
=== '?') {
4459 this.applicationCursor
= false;
4462 if (this.cols
=== 132 && this.savedCols
) {
4463 this.resize(this.savedCols
, this.rows
);
4465 delete this.savedCols
;
4468 this.originMode
= false;
4471 this.wraparoundMode
= false;
4474 // this.cursorBlink = false;
4477 this.log('Switching back to normal keypad.');
4478 this.applicationKeypad
= false;
4480 case 9: // X10 Mouse
4481 case 1000: // vt200 mouse
4482 case 1002: // button event mouse
4483 case 1003: // any event mouse
4484 this.x10Mouse
= false;
4485 this.vt200Mouse
= false;
4486 this.normalMouse
= false;
4487 this.mouseEvents
= false;
4488 this.element
.style
.cursor
= '';
4490 case 1004: // send focusin/focusout events
4491 this.sendFocus
= false;
4493 case 1005: // utf8 ext mode mouse
4494 this.utfMouse
= false;
4496 case 1006: // sgr ext mode mouse
4497 this.sgrMouse
= false;
4499 case 1015: // urxvt ext mode mouse
4500 this.urxvtMouse
= false;
4502 case 25: // hide cursor
4503 this.cursorHidden
= true;
4505 case 1049: // alt screen buffer cursor
4507 case 47: // normal screen buffer
4508 case 1047: // normal screen buffer - clearing it first
4510 this.lines
= this.normal
.lines
;
4511 this.ybase
= this.normal
.ybase
;
4512 this.ydisp
= this.normal
.ydisp
;
4513 this.x
= this.normal
.x
;
4514 this.y
= this.normal
.y
;
4515 this.scrollTop
= this.normal
.scrollTop
;
4516 this.scrollBottom
= this.normal
.scrollBottom
;
4517 this.tabs
= this.normal
.tabs
;
4519 // if (params === 1049) {
4520 // this.x = this.savedX;
4521 // this.y = this.savedY;
4523 this.refresh(0, this.rows
- 1);
4534 * Set Scrolling Region [top;bottom] (default = full size of win-
4538 Terminal
.prototype.setScrollRegion = function(params
) {
4539 if (this.prefix
) return;
4540 this.scrollTop
= (params
[0] || 1) - 1;
4541 this.scrollBottom
= (params
[1] || this.rows
) - 1;
4549 * Save cursor (ANSI.SYS).
4551 Terminal
.prototype.saveCursor = function(params
) {
4552 this.savedX
= this.x
;
4553 this.savedY
= this.y
;
4559 * Restore cursor (ANSI.SYS).
4561 Terminal
.prototype.restoreCursor = function(params
) {
4562 this.x
= this.savedX
|| 0;
4563 this.y
= this.savedY
|| 0;
4573 * Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
4575 Terminal
.prototype.cursorForwardTab = function(params
) {
4576 var param
= params
[0] || 1;
4578 this.x
= this.nextStop();
4584 * CSI Ps S Scroll up Ps lines (default = 1) (SU).
4586 Terminal
.prototype.scrollUp = function(params
) {
4587 var param
= params
[0] || 1;
4589 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
4590 this.lines
.splice(this.ybase
+ this.scrollBottom
, 0, this.blankLine());
4593 this.updateRange(this.scrollTop
);
4594 this.updateRange(this.scrollBottom
);
4599 * CSI Ps T Scroll down Ps lines (default = 1) (SD).
4601 Terminal
.prototype.scrollDown = function(params
) {
4602 var param
= params
[0] || 1;
4604 this.lines
.splice(this.ybase
+ this.scrollBottom
, 1);
4605 this.lines
.splice(this.ybase
+ this.scrollTop
, 0, this.blankLine());
4608 this.updateRange(this.scrollTop
);
4609 this.updateRange(this.scrollBottom
);
4614 * CSI Ps ; Ps ; Ps ; Ps ; Ps T
4615 * Initiate highlight mouse tracking. Parameters are
4616 * [func;startx;starty;firstrow;lastrow]. See the section Mouse
4619 Terminal
.prototype.initMouseTracking = function(params
) {
4620 // Relevant: DECSET 1001
4626 * Reset one or more features of the title modes to the default
4627 * value. Normally, "reset" disables the feature. It is possi-
4628 * ble to disable the ability to reset features by compiling a
4629 * different default for the title modes into xterm.
4630 * Ps = 0 -> Do not set window/icon labels using hexadecimal.
4631 * Ps = 1 -> Do not query window/icon labels using hexadeci-
4633 * Ps = 2 -> Do not set window/icon labels using UTF-8.
4634 * Ps = 3 -> Do not query window/icon labels using UTF-8.
4635 * (See discussion of "Title Modes").
4637 Terminal
.prototype.resetTitleModes = function(params
) {
4643 * CSI Ps Z Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
4645 Terminal
.prototype.cursorBackwardTab = function(params
) {
4646 var param
= params
[0] || 1;
4648 this.x
= this.prevStop();
4654 * CSI Ps b Repeat the preceding graphic character Ps times (REP).
4656 Terminal
.prototype.repeatPrecedingCharacter = function(params
) {
4657 var param
= params
[0] || 1
4658 , line
= this.lines
[this.ybase
+ this.y
]
4659 , ch
= line
[this.x
- 1] || [this.defAttr
, ' ', 1];
4661 while (param
--) line
[this.x
++] = ch
;
4666 * CSI Ps g Tab Clear (TBC).
4667 * Ps = 0 -> Clear Current Column (default).
4668 * Ps = 3 -> Clear All.
4670 * Ps = 2 -> Clear Stops on Line.
4671 * http://vt100.net/annarbor/aaa-ug/section6.html
4673 Terminal
.prototype.tabClear = function(params
) {
4674 var param
= params
[0];
4676 delete this.tabs
[this.x
];
4677 } else if (param
=== 3) {
4684 * CSI Pm i Media Copy (MC).
4685 * Ps = 0 -> Print screen (default).
4686 * Ps = 4 -> Turn off printer controller mode.
4687 * Ps = 5 -> Turn on printer controller mode.
4689 * Media Copy (MC, DEC-specific).
4690 * Ps = 1 -> Print line containing cursor.
4691 * Ps = 4 -> Turn off autoprint mode.
4692 * Ps = 5 -> Turn on autoprint mode.
4693 * Ps = 1 0 -> Print composed display, ignores DECPEX.
4694 * Ps = 1 1 -> Print all pages.
4696 Terminal
.prototype.mediaCopy = function(params
) {
4703 * Set or reset resource-values used by xterm to decide whether
4704 * to construct escape sequences holding information about the
4705 * modifiers pressed with a given key. The first parameter iden-
4706 * tifies the resource to set/reset. The second parameter is the
4707 * value to assign to the resource. If the second parameter is
4708 * omitted, the resource is reset to its initial value.
4709 * Ps = 1 -> modifyCursorKeys.
4710 * Ps = 2 -> modifyFunctionKeys.
4711 * Ps = 4 -> modifyOtherKeys.
4712 * If no parameters are given, all resources are reset to their
4715 Terminal
.prototype.setResources = function(params
) {
4722 * Disable modifiers which may be enabled via the CSI > Ps; Ps m
4723 * sequence. This corresponds to a resource value of "-1", which
4724 * cannot be set with the other sequence. The parameter identi-
4725 * fies the resource to be disabled:
4726 * Ps = 1 -> modifyCursorKeys.
4727 * Ps = 2 -> modifyFunctionKeys.
4728 * Ps = 4 -> modifyOtherKeys.
4729 * If the parameter is omitted, modifyFunctionKeys is disabled.
4730 * When modifyFunctionKeys is disabled, xterm uses the modifier
4731 * keys to make an extended sequence of functions rather than
4732 * adding a parameter to each function key to denote the modi-
4735 Terminal
.prototype.disableModifiers = function(params
) {
4742 * Set resource value pointerMode. This is used by xterm to
4743 * decide whether to hide the pointer cursor as the user types.
4744 * Valid values for the parameter:
4745 * Ps = 0 -> never hide the pointer.
4746 * Ps = 1 -> hide if the mouse tracking mode is not enabled.
4747 * Ps = 2 -> always hide the pointer. If no parameter is
4748 * given, xterm uses the default, which is 1 .
4750 Terminal
.prototype.setPointerMode = function(params
) {
4756 * CSI ! p Soft terminal reset (DECSTR).
4757 * http://vt100.net/docs/vt220-rm/table4-10.html
4759 Terminal
.prototype.softReset = function(params
) {
4760 this.cursorHidden
= false;
4761 this.insertMode
= false;
4762 this.originMode
= false;
4763 this.wraparoundMode
= false; // autowrap
4764 this.applicationKeypad
= false; // ?
4765 this.applicationCursor
= false;
4767 this.scrollBottom
= this.rows
- 1;
4768 this.curAttr
= this.defAttr
;
4769 this.x
= this.y
= 0; // ?
4770 this.charset
= null;
4771 this.glevel
= 0; // ??
4772 this.charsets
= [null]; // ??
4778 * Request ANSI mode (DECRQM). For VT300 and up, reply is
4780 * where Ps is the mode number as in RM, and Pm is the mode
4782 * 0 - not recognized
4785 * 3 - permanently set
4786 * 4 - permanently reset
4788 Terminal
.prototype.requestAnsiMode = function(params
) {
4795 * Request DEC private mode (DECRQM). For VT300 and up, reply is
4797 * where Ps is the mode number as in DECSET, Pm is the mode value
4798 * as in the ANSI DECRQM.
4800 Terminal
.prototype.requestPrivateMode = function(params
) {
4807 * Set conformance level (DECSCL). Valid values for the first
4809 * Ps = 6 1 -> VT100.
4810 * Ps = 6 2 -> VT200.
4811 * Ps = 6 3 -> VT300.
4812 * Valid values for the second parameter:
4813 * Ps = 0 -> 8-bit controls.
4814 * Ps = 1 -> 7-bit controls (always set for VT100).
4815 * Ps = 2 -> 8-bit controls.
4817 Terminal
.prototype.setConformanceLevel = function(params
) {
4823 * CSI Ps q Load LEDs (DECLL).
4824 * Ps = 0 -> Clear all LEDS (default).
4825 * Ps = 1 -> Light Num Lock.
4826 * Ps = 2 -> Light Caps Lock.
4827 * Ps = 3 -> Light Scroll Lock.
4828 * Ps = 2 1 -> Extinguish Num Lock.
4829 * Ps = 2 2 -> Extinguish Caps Lock.
4830 * Ps = 2 3 -> Extinguish Scroll Lock.
4832 Terminal
.prototype.loadLEDs = function(params
) {
4839 * Set cursor style (DECSCUSR, VT520).
4840 * Ps = 0 -> blinking block.
4841 * Ps = 1 -> blinking block (default).
4842 * Ps = 2 -> steady block.
4843 * Ps = 3 -> blinking underline.
4844 * Ps = 4 -> steady underline.
4846 Terminal
.prototype.setCursorStyle = function(params
) {
4853 * Select character protection attribute (DECSCA). Valid values
4854 * for the parameter:
4855 * Ps = 0 -> DECSED and DECSEL can erase (default).
4856 * Ps = 1 -> DECSED and DECSEL cannot erase.
4857 * Ps = 2 -> DECSED and DECSEL can erase.
4859 Terminal
.prototype.setCharProtectionAttr = function(params
) {
4866 * Restore DEC Private Mode Values. The value of Ps previously
4867 * saved is restored. Ps values are the same as for DECSET.
4869 Terminal
.prototype.restorePrivateValues = function(params
) {
4875 * CSI Pt; Pl; Pb; Pr; Ps$ r
4876 * Change Attributes in Rectangular Area (DECCARA), VT400 and up.
4877 * Pt; Pl; Pb; Pr denotes the rectangle.
4878 * Ps denotes the SGR attributes to change: 0, 1, 4, 5, 7.
4879 * NOTE: xterm doesn't enable this code by default.
4881 Terminal
.prototype.setAttrInRectangle = function(params
) {
4891 for (; t
< b
+ 1; t
++) {
4892 line
= this.lines
[this.ybase
+ t
];
4893 for (i
= l
; i
< r
; i
++) {
4894 line
[i
] = [attr
, line
[i
][1]];
4899 this.updateRange(params
[0]);
4900 this.updateRange(params
[2]);
4905 * CSI Pc; Pt; Pl; Pb; Pr$ x
4906 * Fill Rectangular Area (DECFRA), VT420 and up.
4907 * Pc is the character to use.
4908 * Pt; Pl; Pb; Pr denotes the rectangle.
4909 * NOTE: xterm doesn't enable this code by default.
4911 Terminal
.prototype.fillRectangle = function(params
) {
4921 for (; t
< b
+ 1; t
++) {
4922 line
= this.lines
[this.ybase
+ t
];
4923 for (i
= l
; i
< r
; i
++) {
4924 line
[i
] = [line
[i
][0], String
.fromCharCode(ch
)];
4929 this.updateRange(params
[1]);
4930 this.updateRange(params
[3]);
4936 * Enable Locator Reporting (DECELR).
4937 * Valid values for the first parameter:
4938 * Ps = 0 -> Locator disabled (default).
4939 * Ps = 1 -> Locator enabled.
4940 * Ps = 2 -> Locator enabled for one report, then disabled.
4941 * The second parameter specifies the coordinate unit for locator
4943 * Valid values for the second parameter:
4944 * Pu = 0 <- or omitted -> default to character cells.
4945 * Pu = 1 <- device physical pixels.
4946 * Pu = 2 <- character cells.
4948 Terminal
.prototype.enableLocatorReporting = function(params
) {
4949 var val
= params
[0] > 0;
4950 //this.mouseEvents = val;
4951 //this.decLocator = val;
4956 * CSI Pt; Pl; Pb; Pr$ z
4957 * Erase Rectangular Area (DECERA), VT400 and up.
4958 * Pt; Pl; Pb; Pr denotes the rectangle.
4959 * NOTE: xterm doesn't enable this code by default.
4961 Terminal
.prototype.eraseRectangle = function(params
) {
4971 ch
= [this.eraseAttr(), ' ', 1]; // xterm?
4973 for (; t
< b
+ 1; t
++) {
4974 line
= this.lines
[this.ybase
+ t
];
4975 for (i
= l
; i
< r
; i
++) {
4981 this.updateRange(params
[0]);
4982 this.updateRange(params
[2]);
4988 * Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
4989 * NOTE: xterm doesn't enable this code by default.
4991 Terminal
.prototype.insertColumns = function() {
4992 var param
= params
[0]
4993 , l
= this.ybase
+ this.rows
4994 , ch
= [this.eraseAttr(), ' ', 1] // xterm?
4998 for (i
= this.ybase
; i
< l
; i
++) {
4999 this.lines
[i
].splice(this.x
+ 1, 0, ch
);
5000 this.lines
[i
].pop();
5010 * Delete P s Column(s) (default = 1) (DECDC), VT420 and up
5011 * NOTE: xterm doesn't enable this code by default.
5013 Terminal
.prototype.deleteColumns = function() {
5014 var param
= params
[0]
5015 , l
= this.ybase
+ this.rows
5016 , ch
= [this.eraseAttr(), ' ', 1] // xterm?
5020 for (i
= this.ybase
; i
< l
; i
++) {
5021 this.lines
[i
].splice(this.x
, 1);
5022 this.lines
[i
].push(ch
);
5033 Terminal
.charsets
= {};
5035 // DEC Special Character and Line Drawing Set.
5036 // http://vt100.net/docs/vt102-ug/table5-13.html
5037 // A lot of curses apps use this if they see TERM=xterm.
5038 // testing: echo -e '\e(0a\e(B'
5039 // The xterm output sometimes seems to conflict with the
5040 // reference above. xterm seems in line with the reference
5041 // when running vttest however.
5042 // The table below now uses xterm's output from vttest.
5043 Terminal
.charsets
.SCLD
= { // (0
5044 '`': '\u25c6', // '◆'
5045 'a': '\u2592', // '▒'
5046 'b': '\u0009', // '\t'
5047 'c': '\u000c', // '\f'
5048 'd': '\u000d', // '\r'
5049 'e': '\u000a', // '\n'
5050 'f': '\u00b0', // '°'
5051 'g': '\u00b1', // '±'
5052 'h': '\u2424', // '\u2424' (NL)
5053 'i': '\u000b', // '\v'
5054 'j': '\u2518', // '┘'
5055 'k': '\u2510', // '┐'
5056 'l': '\u250c', // '┌'
5057 'm': '\u2514', // '└'
5058 'n': '\u253c', // '┼'
5059 'o': '\u23ba', // '⎺'
5060 'p': '\u23bb', // '⎻'
5061 'q': '\u2500', // '─'
5062 'r': '\u23bc', // '⎼'
5063 's': '\u23bd', // '⎽'
5064 't': '\u251c', // '├'
5065 'u': '\u2524', // '┤'
5066 'v': '\u2534', // '┴'
5067 'w': '\u252c', // '┬'
5068 'x': '\u2502', // '│'
5069 'y': '\u2264', // '≤'
5070 'z': '\u2265', // '≥'
5071 '{': '\u03c0', // 'π'
5072 '|': '\u2260', // '≠'
5073 '}': '\u00a3', // '£'
5074 '~': '\u00b7' // '·'
5077 Terminal
.charsets
.UK
= null; // (A
5078 Terminal
.charsets
.US
= null; // (B (USASCII)
5079 Terminal
.charsets
.Dutch
= null; // (4
5080 Terminal
.charsets
.Finnish
= null; // (C or (5
5081 Terminal
.charsets
.French
= null; // (R
5082 Terminal
.charsets
.FrenchCanadian
= null; // (Q
5083 Terminal
.charsets
.German
= null; // (K
5084 Terminal
.charsets
.Italian
= null; // (Y
5085 Terminal
.charsets
.NorwegianDanish
= null; // (E or (6
5086 Terminal
.charsets
.Spanish
= null; // (Z
5087 Terminal
.charsets
.Swedish
= null; // (H or (7
5088 Terminal
.charsets
.Swiss
= null; // (=
5089 Terminal
.charsets
.ISOLatin
= null; // /A
5095 function contains(el
, arr
) {
5096 for (var i
= 0; i
< arr
.length
; i
+= 1) {
5097 if (el
=== arr
[i
]) {
5104 function on(el
, type
, handler
, capture
) {
5105 if (!Array
.isArray(el
)) {
5108 el
.forEach(function (element
) {
5109 element
.addEventListener(type
, handler
, capture
|| false);
5113 function off(el
, type
, handler
, capture
) {
5114 el
.removeEventListener(type
, handler
, capture
|| false);
5117 function cancel(ev
, force
) {
5118 if (!this.cancelEvents
&& !force
) {
5121 ev
.preventDefault();
5122 ev
.stopPropagation();
5126 function inherits(child
, parent
) {
5128 this.constructor = child
;
5130 f
.prototype = parent
.prototype;
5131 child
.prototype = new f
;
5134 // if bold is broken, we can't
5135 // use it in the terminal.
5136 function isBoldBroken(document
) {
5137 var body
= document
.getElementsByTagName('body')[0];
5138 var el
= document
.createElement('span');
5139 el
.innerHTML
= 'hello world';
5140 body
.appendChild(el
);
5141 var w1
= el
.scrollWidth
;
5142 el
.style
.fontWeight
= 'bold';
5143 var w2
= el
.scrollWidth
;
5144 body
.removeChild(el
);
5148 var String
= this.String
;
5149 var setTimeout
= this.setTimeout
;
5150 var setInterval
= this.setInterval
;
5152 function indexOf(obj
, el
) {
5155 if (obj
[i
] === el
) return i
;
5160 function isThirdLevelShift(term
, ev
) {
5162 (term
.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) ||
5163 (term
.isMSWindows
&& ev
.altKey
&& ev
.ctrlKey
&& !ev
.metaKey
);
5165 if (ev
.type
== 'keypress') {
5166 return thirdLevelKey
;
5169 // Don't invoke for arrows, pageDown, home, backspace, etc. (on non-keypress events)
5170 return thirdLevelKey
&& (!ev
.keyCode
|| ev
.keyCode
> 47);
5173 function matchColor(r1
, g1
, b1
) {
5174 var hash
= (r1
<< 16) | (g1
<< 8) | b1
;
5176 if (matchColor
._cache
[hash
] != null) {
5177 return matchColor
._cache
[hash
];
5180 var ldiff
= Infinity
5189 for (; i
< Terminal
.vcolors
.length
; i
++) {
5190 c
= Terminal
.vcolors
[i
];
5195 diff
= matchColor
.distance(r1
, g1
, b1
, r2
, g2
, b2
);
5208 return matchColor
._cache
[hash
] = li
;
5211 matchColor
._cache
= {};
5213 // http://stackoverflow.com/questions/1633828
5214 matchColor
.distance = function(r1
, g1
, b1
, r2
, g2
, b2
) {
5215 return Math
.pow(30 * (r1
- r2
), 2)
5216 + Math
.pow(59 * (g1
- g2
), 2)
5217 + Math
.pow(11 * (b1
- b2
), 2);
5220 function each(obj
, iter
, con
) {
5221 if (obj
.forEach
) return obj
.forEach(iter
, con
);
5222 for (var i
= 0; i
< obj
.length
; i
++) {
5223 iter
.call(con
, obj
[i
], i
, obj
);
5227 function keys(obj
) {
5228 if (Object
.keys
) return Object
.keys(obj
);
5231 if (Object
.prototype.hasOwnProperty
.call(obj
, key
)) {
5238 var wcwidth
= (function(opts
) {
5239 // extracted from https://www.cl.cam.ac.uk/%7Emgk25/ucs/wcwidth.c
5240 // combining characters
5242 [0x0300, 0x036F], [0x0483, 0x0486], [0x0488, 0x0489],
5243 [0x0591, 0x05BD], [0x05BF, 0x05BF], [0x05C1, 0x05C2],
5244 [0x05C4, 0x05C5], [0x05C7, 0x05C7], [0x0600, 0x0603],
5245 [0x0610, 0x0615], [0x064B, 0x065E], [0x0670, 0x0670],
5246 [0x06D6, 0x06E4], [0x06E7, 0x06E8], [0x06EA, 0x06ED],
5247 [0x070F, 0x070F], [0x0711, 0x0711], [0x0730, 0x074A],
5248 [0x07A6, 0x07B0], [0x07EB, 0x07F3], [0x0901, 0x0902],
5249 [0x093C, 0x093C], [0x0941, 0x0948], [0x094D, 0x094D],
5250 [0x0951, 0x0954], [0x0962, 0x0963], [0x0981, 0x0981],
5251 [0x09BC, 0x09BC], [0x09C1, 0x09C4], [0x09CD, 0x09CD],
5252 [0x09E2, 0x09E3], [0x0A01, 0x0A02], [0x0A3C, 0x0A3C],
5253 [0x0A41, 0x0A42], [0x0A47, 0x0A48], [0x0A4B, 0x0A4D],
5254 [0x0A70, 0x0A71], [0x0A81, 0x0A82], [0x0ABC, 0x0ABC],
5255 [0x0AC1, 0x0AC5], [0x0AC7, 0x0AC8], [0x0ACD, 0x0ACD],
5256 [0x0AE2, 0x0AE3], [0x0B01, 0x0B01], [0x0B3C, 0x0B3C],
5257 [0x0B3F, 0x0B3F], [0x0B41, 0x0B43], [0x0B4D, 0x0B4D],
5258 [0x0B56, 0x0B56], [0x0B82, 0x0B82], [0x0BC0, 0x0BC0],
5259 [0x0BCD, 0x0BCD], [0x0C3E, 0x0C40], [0x0C46, 0x0C48],
5260 [0x0C4A, 0x0C4D], [0x0C55, 0x0C56], [0x0CBC, 0x0CBC],
5261 [0x0CBF, 0x0CBF], [0x0CC6, 0x0CC6], [0x0CCC, 0x0CCD],
5262 [0x0CE2, 0x0CE3], [0x0D41, 0x0D43], [0x0D4D, 0x0D4D],
5263 [0x0DCA, 0x0DCA], [0x0DD2, 0x0DD4], [0x0DD6, 0x0DD6],
5264 [0x0E31, 0x0E31], [0x0E34, 0x0E3A], [0x0E47, 0x0E4E],
5265 [0x0EB1, 0x0EB1], [0x0EB4, 0x0EB9], [0x0EBB, 0x0EBC],
5266 [0x0EC8, 0x0ECD], [0x0F18, 0x0F19], [0x0F35, 0x0F35],
5267 [0x0F37, 0x0F37], [0x0F39, 0x0F39], [0x0F71, 0x0F7E],
5268 [0x0F80, 0x0F84], [0x0F86, 0x0F87], [0x0F90, 0x0F97],
5269 [0x0F99, 0x0FBC], [0x0FC6, 0x0FC6], [0x102D, 0x1030],
5270 [0x1032, 0x1032], [0x1036, 0x1037], [0x1039, 0x1039],
5271 [0x1058, 0x1059], [0x1160, 0x11FF], [0x135F, 0x135F],
5272 [0x1712, 0x1714], [0x1732, 0x1734], [0x1752, 0x1753],
5273 [0x1772, 0x1773], [0x17B4, 0x17B5], [0x17B7, 0x17BD],
5274 [0x17C6, 0x17C6], [0x17C9, 0x17D3], [0x17DD, 0x17DD],
5275 [0x180B, 0x180D], [0x18A9, 0x18A9], [0x1920, 0x1922],
5276 [0x1927, 0x1928], [0x1932, 0x1932], [0x1939, 0x193B],
5277 [0x1A17, 0x1A18], [0x1B00, 0x1B03], [0x1B34, 0x1B34],
5278 [0x1B36, 0x1B3A], [0x1B3C, 0x1B3C], [0x1B42, 0x1B42],
5279 [0x1B6B, 0x1B73], [0x1DC0, 0x1DCA], [0x1DFE, 0x1DFF],
5280 [0x200B, 0x200F], [0x202A, 0x202E], [0x2060, 0x2063],
5281 [0x206A, 0x206F], [0x20D0, 0x20EF], [0x302A, 0x302F],
5282 [0x3099, 0x309A], [0xA806, 0xA806], [0xA80B, 0xA80B],
5283 [0xA825, 0xA826], [0xFB1E, 0xFB1E], [0xFE00, 0xFE0F],
5284 [0xFE20, 0xFE23], [0xFEFF, 0xFEFF], [0xFFF9, 0xFFFB],
5285 [0x10A01, 0x10A03], [0x10A05, 0x10A06], [0x10A0C, 0x10A0F],
5286 [0x10A38, 0x10A3A], [0x10A3F, 0x10A3F], [0x1D167, 0x1D169],
5287 [0x1D173, 0x1D182], [0x1D185, 0x1D18B], [0x1D1AA, 0x1D1AD],
5288 [0x1D242, 0x1D244], [0xE0001, 0xE0001], [0xE0020, 0xE007F],
5292 function bisearch(ucs
) {
5294 var max
= COMBINING
.length
- 1;
5296 if (ucs
< COMBINING
[0][0] || ucs
> COMBINING
[max
][1])
5298 while (max
>= min
) {
5299 mid
= Math
.floor((min
+ max
) / 2);
5300 if (ucs
> COMBINING
[mid
][1])
5302 else if (ucs
< COMBINING
[mid
][0])
5309 function wcwidth(ucs
) {
5310 // test for 8-bit control characters
5313 if (ucs
< 32 || (ucs
>= 0x7f && ucs
< 0xa0))
5314 return opts
.control
;
5315 // binary search in table of non-spacing characters
5318 // if we arrive here, ucs is not a combining or C0/C1 control character
5323 ucs
<= 0x115f || // Hangul Jamo init. consonants
5326 (ucs
>= 0x2e80 && ucs
<= 0xa4cf && ucs
!= 0x303f) || // CJK..Yi
5327 (ucs
>= 0xac00 && ucs
<= 0xd7a3) || // Hangul Syllables
5328 (ucs
>= 0xf900 && ucs
<= 0xfaff) || // CJK Compat Ideographs
5329 (ucs
>= 0xfe10 && ucs
<= 0xfe19) || // Vertical forms
5330 (ucs
>= 0xfe30 && ucs
<= 0xfe6f) || // CJK Compat Forms
5331 (ucs
>= 0xff00 && ucs
<= 0xff60) || // Fullwidth Forms
5332 (ucs
>= 0xffe0 && ucs
<= 0xffe6) ||
5333 (ucs
>= 0x20000 && ucs
<= 0x2fffd) ||
5334 (ucs
>= 0x30000 && ucs
<= 0x3fffd)
5339 })({nul
: 0, control
: 0}); // configurable options
5345 Terminal
.EventEmitter
= EventEmitter
;
5346 Terminal
.CompositionHelper
= CompositionHelper
;
5347 Terminal
.inherits
= inherits
;
5350 * Adds an event listener to the terminal.
5352 * @param {string} event The name of the event. TODO: Document all event types
5353 * @param {function} callback The function to call when the event is triggered.
5357 Terminal
.cancel
= cancel
;