1 (function(f
){if(typeof exports
==="object"&&typeof module
!=="undefined"){module
.exports
=f()}else if(typeof define
==="function"&&define
.amd
){define([],f
)}else{var g
;if(typeof window
!=="undefined"){g
=window
}else if(typeof global
!=="undefined"){g
=global
}else if(typeof self
!=="undefined"){g
=self
}else{g
=this}g
.Terminal
= f()}})(function(){var define
,module
,exports
;return (function e(t
,n
,r
){function s(o
,u
){if(!n
[o
]){if(!t
[o
]){var a
=typeof require
=="function"&&require
;if(!u
&&a
)return a(o
,!0);if(i
)return i(o
,!0);var f
=new Error("Cannot find module '"+o
+"'");throw f
.code
="MODULE_NOT_FOUND",f
}var l
=n
[o
]={exports
:{}};t
[o
][0].call(l
.exports
,function(e
){var n
=t
[o
][1][e
];return s(n
?n
:e
)},l
,l
.exports
,e
,t
,n
,r
)}return n
[o
].exports
}var i
=typeof require
=="function"&&require
;for(var o
=0;o
<r
.length
;o
++)s(r
[o
]);return s
})({1:[function(require
,module
,exports
){
7 * Encapsulates the logic for handling compositionstart, compositionupdate and compositionend
8 * events, displaying the in-progress composition to the UI and forwarding the final composition
11 var CompositionHelper
= (function () {
13 * Creates a new CompositionHelper.
14 * @param textarea The textarea that xterm uses for input.
15 * @param compositionView The element to display the in-progress composition in.
16 * @param terminal The Terminal to forward the finished composition to.
18 function CompositionHelper(textarea
, compositionView
, terminal
) {
19 this.textarea
= textarea
;
20 this.compositionView
= compositionView
;
21 this.terminal
= terminal
;
22 this.isComposing
= false;
23 this.isSendingComposition
= false;
24 this.compositionPosition
= { start
: null, end
: null };
28 * Handles the compositionstart event, activating the composition view.
30 CompositionHelper
.prototype.compositionstart = function () {
31 this.isComposing
= true;
32 this.compositionPosition
.start
= this.textarea
.value
.length
;
33 this.compositionView
.textContent
= '';
34 this.compositionView
.classList
.add('active');
37 * Handles the compositionupdate event, updating the composition view.
38 * @param {CompositionEvent} ev The event.
40 CompositionHelper
.prototype.compositionupdate = function (ev
) {
41 this.compositionView
.textContent
= ev
.data
;
42 this.updateCompositionElements();
44 setTimeout(function () {
45 self
.compositionPosition
.end
= self
.textarea
.value
.length
;
49 * Handles the compositionend event, hiding the composition view and sending the composition to
52 CompositionHelper
.prototype.compositionend = function () {
53 this.finalizeComposition(true);
56 * Handles the keydown event, routing any necessary events to the CompositionHelper functions.
57 * @param ev The keydown event.
58 * @return Whether the Terminal should continue processing the keydown event.
60 CompositionHelper
.prototype.keydown = function (ev
) {
61 if (this.isComposing
|| this.isSendingComposition
) {
62 if (ev
.keyCode
=== 229) {
63 // Continue composing if the keyCode is the "composition character"
66 else if (ev
.keyCode
=== 16 || ev
.keyCode
=== 17 || ev
.keyCode
=== 18) {
67 // Continue composing if the keyCode is a modifier key
71 // Finish composition immediately. This is mainly here for the case where enter is
72 // pressed and the handler needs to be triggered before the command is executed.
73 this.finalizeComposition(false);
76 if (ev
.keyCode
=== 229) {
77 // If the "composition character" is used but gets to this point it means a non-composition
78 // character (eg. numbers and punctuation) was pressed when the IME was active.
79 this.handleAnyTextareaChanges();
85 * Finalizes the composition, resuming regular input actions. This is called when a composition
87 * @param waitForPropogation Whether to wait for events to propogate before sending
88 * the input. This should be false if a non-composition keystroke is entered before the
89 * compositionend event is triggered, such as enter, so that the composition is send before
90 * the command is executed.
92 CompositionHelper
.prototype.finalizeComposition = function (waitForPropogation
) {
93 this.compositionView
.classList
.remove('active');
94 this.isComposing
= false;
95 this.clearTextareaPosition();
96 if (!waitForPropogation
) {
97 // Cancel any delayed composition send requests and send the input immediately.
98 this.isSendingComposition
= false;
99 var input
= this.textarea
.value
.substring(this.compositionPosition
.start
, this.compositionPosition
.end
);
100 this.terminal
.handler(input
);
103 // Make a deep copy of the composition position here as a new compositionstart event may
104 // fire before the setTimeout executes.
105 var currentCompositionPosition
= {
106 start
: this.compositionPosition
.start
,
107 end
: this.compositionPosition
.end
,
109 // Since composition* events happen before the changes take place in the textarea on most
110 // browsers, use a setTimeout with 0ms time to allow the native compositionend event to
111 // complete. This ensures the correct character is retrieved, this solution was used
113 // - The compositionend event's data property is unreliable, at least on Chromium
114 // - The last compositionupdate event's data property does not always accurately describe
115 // the character, a counter example being Korean where an ending consonsant can move to
116 // the following character if the following input is a vowel.
118 this.isSendingComposition
= true;
119 setTimeout(function () {
120 // Ensure that the input has not already been sent
121 if (self
.isSendingComposition
) {
122 self
.isSendingComposition
= false;
124 if (self
.isComposing
) {
125 // Use the end position to get the string if a new composition has started.
126 input
= self
.textarea
.value
.substring(currentCompositionPosition
.start
, currentCompositionPosition
.end
);
129 // Don't use the end position here in order to pick up any characters after the
130 // composition has finished, for example when typing a non-composition character
131 // (eg. 2) after a composition character.
132 input
= self
.textarea
.value
.substring(currentCompositionPosition
.start
);
134 self
.terminal
.handler(input
);
140 * Apply any changes made to the textarea after the current event chain is allowed to complete.
141 * This should be called when not currently composing but a keydown event with the "composition
142 * character" (229) is triggered, in order to allow non-composition text to be entered when an
145 CompositionHelper
.prototype.handleAnyTextareaChanges = function () {
146 var oldValue
= this.textarea
.value
;
148 setTimeout(function () {
149 // Ignore if a composition has started since the timeout
150 if (!self
.isComposing
) {
151 var newValue
= self
.textarea
.value
;
152 var diff
= newValue
.replace(oldValue
, '');
153 if (diff
.length
> 0) {
154 self
.terminal
.handler(diff
);
160 * Positions the composition view on top of the cursor and the textarea just below it (so the
161 * IME helper dialog is positioned correctly).
162 * @param dontRecurse Whether to use setTimeout to recursively trigger another update, this is
163 * necessary as the IME events across browsers are not consistently triggered.
165 CompositionHelper
.prototype.updateCompositionElements = function (dontRecurse
) {
166 if (!this.isComposing
) {
169 var cursor
= this.terminal
.element
.querySelector('.terminal-cursor');
171 // Take .xterm-rows offsetTop into account as well in case it's positioned absolutely within
172 // the .xterm element.
173 var xtermRows
= this.terminal
.element
.querySelector('.xterm-rows');
174 var cursorTop
= xtermRows
.offsetTop
+ cursor
.offsetTop
;
175 this.compositionView
.style
.left
= cursor
.offsetLeft
+ 'px';
176 this.compositionView
.style
.top
= cursorTop
+ 'px';
177 this.compositionView
.style
.height
= cursor
.offsetHeight
+ 'px';
178 this.compositionView
.style
.lineHeight
= cursor
.offsetHeight
+ 'px';
179 // Sync the textarea to the exact position of the composition view so the IME knows where the
181 var compositionViewBounds
= this.compositionView
.getBoundingClientRect();
182 this.textarea
.style
.left
= cursor
.offsetLeft
+ 'px';
183 this.textarea
.style
.top
= cursorTop
+ 'px';
184 this.textarea
.style
.width
= compositionViewBounds
.width
+ 'px';
185 this.textarea
.style
.height
= compositionViewBounds
.height
+ 'px';
186 this.textarea
.style
.lineHeight
= compositionViewBounds
.height
+ 'px';
189 setTimeout(this.updateCompositionElements
.bind(this, true), 0);
194 * Clears the textarea's position so that the cursor does not blink on IE.
197 CompositionHelper
.prototype.clearTextareaPosition = function () {
198 this.textarea
.style
.left
= '';
199 this.textarea
.style
.top
= '';
202 return CompositionHelper
;
204 exports
.CompositionHelper
= CompositionHelper
;
206 },{}],2:[function(require
,module
,exports
){
211 function EventEmitter() {
212 this._events
= this._events
|| {};
214 exports
.EventEmitter
= EventEmitter
;
215 EventEmitter
.prototype.addListener = function (type
, listener
) {
216 this._events
[type
] = this._events
[type
] || [];
217 this._events
[type
].push(listener
);
219 EventEmitter
.prototype.on
= EventEmitter
.prototype.addListener
;
220 EventEmitter
.prototype.removeListener = function (type
, listener
) {
221 if (!this._events
[type
])
223 var obj
= this._events
[type
], i
= obj
.length
;
225 if (obj
[i
] === listener
|| obj
[i
].listener
=== listener
) {
231 EventEmitter
.prototype.off
= EventEmitter
.prototype.removeListener
;
232 EventEmitter
.prototype.removeAllListeners = function (type
) {
233 if (this._events
[type
])
234 delete this._events
[type
];
236 EventEmitter
.prototype.once = function (type
, listener
) {
239 var args
= Array
.prototype.slice
.call(arguments
);
240 this.removeListener(type
, on
);
241 return listener
.apply(this, args
);
243 on
.listener
= listener
;
244 return this.on(type
, on
);
246 EventEmitter
.prototype.emit = function (type
) {
247 if (!this._events
[type
])
249 var args
= Array
.prototype.slice
.call(arguments
, 1), obj
= this._events
[type
], l
= obj
.length
, i
= 0;
251 obj
[i
].apply(this, args
);
254 EventEmitter
.prototype.listeners = function (type
) {
255 return this._events
[type
] = this._events
[type
] || [];
258 },{}],3:[function(require
,module
,exports
){
264 * Represents the viewport of a terminal, the visible area within the larger buffer of output.
265 * Logic for the virtual scroll bar is included in this object.
267 var Viewport
= (function () {
269 * Creates a new Viewport.
270 * @param terminal The terminal this viewport belongs to.
271 * @param viewportElement The DOM element acting as the viewport.
272 * @param scrollArea The DOM element acting as the scroll area.
273 * @param charMeasureElement A DOM element used to measure the character size of. the terminal.
275 function Viewport(terminal
, viewportElement
, scrollArea
, charMeasureElement
) {
276 this.terminal
= terminal
;
277 this.viewportElement
= viewportElement
;
278 this.scrollArea
= scrollArea
;
279 this.charMeasureElement
= charMeasureElement
;
280 this.currentRowHeight
= 0;
281 this.lastRecordedBufferLength
= 0;
282 this.lastRecordedViewportHeight
= 0;
283 this.terminal
.on('scroll', this.syncScrollArea
.bind(this));
284 this.terminal
.on('resize', this.syncScrollArea
.bind(this));
285 this.viewportElement
.addEventListener('scroll', this.onScroll
.bind(this));
286 this.syncScrollArea();
289 * Refreshes row height, setting line-height, viewport height and scroll area height if
291 * @param charSize A character size measurement bounding rect object, if it doesn't exist it will
294 Viewport
.prototype.refresh = function (charSize
) {
295 var size
= charSize
|| this.charMeasureElement
.getBoundingClientRect();
296 if (size
.height
> 0) {
297 var rowHeightChanged
= size
.height
!== this.currentRowHeight
;
298 if (rowHeightChanged
) {
299 this.currentRowHeight
= size
.height
;
300 this.viewportElement
.style
.lineHeight
= size
.height
+ 'px';
301 this.terminal
.rowContainer
.style
.lineHeight
= size
.height
+ 'px';
303 var viewportHeightChanged
= this.lastRecordedViewportHeight
!== this.terminal
.rows
;
304 if (rowHeightChanged
|| viewportHeightChanged
) {
305 this.lastRecordedViewportHeight
= this.terminal
.rows
;
306 this.viewportElement
.style
.height
= size
.height
* this.terminal
.rows
+ 'px';
308 this.scrollArea
.style
.height
= (size
.height
* this.lastRecordedBufferLength
) + 'px';
312 * Updates dimensions and synchronizes the scroll area if necessary.
314 Viewport
.prototype.syncScrollArea = function () {
315 if (this.lastRecordedBufferLength
!== this.terminal
.lines
.length
) {
316 // If buffer height changed
317 this.lastRecordedBufferLength
= this.terminal
.lines
.length
;
320 else if (this.lastRecordedViewportHeight
!== this.terminal
.rows
) {
321 // If viewport height changed
325 // If size has changed, refresh viewport
326 var size
= this.charMeasureElement
.getBoundingClientRect();
327 if (size
.height
!== this.currentRowHeight
) {
332 var scrollTop
= this.terminal
.ydisp
* this.currentRowHeight
;
333 if (this.viewportElement
.scrollTop
!== scrollTop
) {
334 this.viewportElement
.scrollTop
= scrollTop
;
338 * Handles scroll events on the viewport, calculating the new viewport and requesting the
339 * terminal to scroll to it.
340 * @param ev The scroll event.
342 Viewport
.prototype.onScroll = function (ev
) {
343 var newRow
= Math
.round(this.viewportElement
.scrollTop
/ this.currentRowHeight
);
344 var diff
= newRow
- this.terminal
.ydisp
;
345 this.terminal
.scrollDisp(diff
, true);
348 * Handles mouse wheel events by adjusting the viewport's scrollTop and delegating the actual
349 * scrolling to `onScroll`, this event needs to be attached manually by the consumer of
351 * @param ev The mouse wheel event.
353 Viewport
.prototype.onWheel = function (ev
) {
354 if (ev
.deltaY
=== 0) {
355 // Do nothing if it's not a vertical scroll event
358 // Fallback to WheelEvent.DOM_DELTA_PIXEL
360 if (ev
.deltaMode
=== WheelEvent
.DOM_DELTA_LINE
) {
361 multiplier
= this.currentRowHeight
;
363 else if (ev
.deltaMode
=== WheelEvent
.DOM_DELTA_PAGE
) {
364 multiplier
= this.currentRowHeight
* this.terminal
.rows
;
366 this.viewportElement
.scrollTop
+= ev
.deltaY
* multiplier
;
367 // Prevent the page from scrolling when the terminal scrolls
373 exports
.Viewport
= Viewport
;
375 },{}],4:[function(require
,module
,exports
){
377 * Clipboard handler module: exports methods for handling all clipboard-related events in the
379 * @module xterm/handlers/Clipboard
384 * Prepares text copied from terminal selection, to be saved in the clipboard by:
385 * 1. stripping all trailing white spaces
386 * 2. converting all non-breaking spaces to regular spaces
387 * @param {string} text The copied text that needs processing for storing in clipboard
390 function prepareTextForClipboard(text
) {
391 var space
= String
.fromCharCode(32), nonBreakingSpace
= String
.fromCharCode(160), allNonBreakingSpaces
= new RegExp(nonBreakingSpace
, 'g'), processedText
= text
.split('\n').map(function (line
) {
392 // Strip all trailing white spaces and convert all non-breaking spaces
393 // to regular spaces.
394 var processedLine
= line
.replace(/\s+$/g, '').replace(allNonBreakingSpaces
, space
);
395 return processedLine
;
397 return processedText
;
399 exports
.prepareTextForClipboard
= prepareTextForClipboard
;
401 * Binds copy functionality to the given terminal.
402 * @param {ClipboardEvent} ev The original copy event to be handled
404 function copyHandler(ev
, term
) {
405 // We cast `window` to `any` type, because TypeScript has not declared the `clipboardData`
406 // property that we use below for Internet Explorer.
407 var copiedText
= window
.getSelection().toString(), text
= prepareTextForClipboard(copiedText
);
408 if (term
.browser
.isMSIE
) {
409 window
.clipboardData
.setData('Text', text
);
412 ev
.clipboardData
.setData('text/plain', text
);
414 ev
.preventDefault(); // Prevent or the original text will be copied.
416 exports
.copyHandler
= copyHandler
;
418 * Redirect the clipboard's data to the terminal's input handler.
419 * @param {ClipboardEvent} ev The original paste event to be handled
420 * @param {Terminal} term The terminal on which to apply the handled paste event
422 function pasteHandler(ev
, term
) {
423 ev
.stopPropagation();
425 var dispatchPaste = function (text
) {
427 term
.textarea
.value
= '';
428 return term
.cancel(ev
);
430 if (term
.browser
.isMSIE
) {
431 if (window
.clipboardData
) {
432 text
= window
.clipboardData
.getData('Text');
437 if (ev
.clipboardData
) {
438 text
= ev
.clipboardData
.getData('text/plain');
443 exports
.pasteHandler
= pasteHandler
;
445 * Bind to right-click event and allow right-click copy and paste.
448 * If text is selected and right-click happens on selected text, then
449 * do nothing to allow seamless copying.
450 * If no text is selected or right-click is outside of the selection
451 * area, then bring the terminal's input below the cursor, in order to
452 * trigger the event on the textarea and allow-right click paste, without
453 * caring about disappearing selection.
454 * @param {ClipboardEvent} ev The original paste event to be handled
455 * @param {Terminal} term The terminal on which to apply the handled paste event
457 function rightClickHandler(ev
, term
) {
458 var s
= document
.getSelection(), selectedText
= prepareTextForClipboard(s
.toString()), clickIsOnSelection
= false, x
= ev
.clientX
, y
= ev
.clientY
;
460 var r
= s
.getRangeAt(0), cr
= r
.getClientRects(), i
= void 0, rect
= void 0;
461 for (i
= 0; i
< cr
.length
; i
++) {
463 clickIsOnSelection
= ((x
> rect
.left
) && (x
< rect
.right
) &&
464 (y
> rect
.top
) && (y
< rect
.bottom
));
465 if (clickIsOnSelection
) {
469 // If we clicked on selection and selection is not a single space,
470 // then mark the right click as copy-only. We check for the single
471 // space selection, as this can happen when clicking on an
472 // and there is not much pointing in copying a single space.
473 if (selectedText
.match(/^\s$/) || !selectedText
.length
) {
474 clickIsOnSelection
= false;
477 // Bring textarea at the cursor position
478 if (!clickIsOnSelection
) {
479 term
.textarea
.style
.position
= 'fixed';
480 term
.textarea
.style
.width
= '20px';
481 term
.textarea
.style
.height
= '20px';
482 term
.textarea
.style
.left
= (x
- 10) + 'px';
483 term
.textarea
.style
.top
= (y
- 10) + 'px';
484 term
.textarea
.style
.zIndex
= '1000';
485 term
.textarea
.focus();
486 // Reset the terminal textarea's styling
487 setTimeout(function () {
488 term
.textarea
.style
.position
= null;
489 term
.textarea
.style
.width
= null;
490 term
.textarea
.style
.height
= null;
491 term
.textarea
.style
.left
= null;
492 term
.textarea
.style
.top
= null;
493 term
.textarea
.style
.zIndex
= null;
497 exports
.rightClickHandler
= rightClickHandler
;
499 },{}],5:[function(require
,module
,exports
){
501 * Attributes and methods to help with identifying the current browser and platform.
502 * @module xterm/utils/Browser
506 var Generic_js_1
= require('./Generic.js');
507 var isNode
= (typeof navigator
== 'undefined') ? true : false;
508 var userAgent
= (isNode
) ? 'node' : navigator
.userAgent
;
509 var platform
= (isNode
) ? 'node' : navigator
.platform
;
510 exports
.isFirefox
= !!~userAgent
.indexOf('Firefox');
511 exports
.isMSIE
= !!~userAgent
.indexOf('MSIE') || !!~userAgent
.indexOf('Trident');
512 // Find the users platform. We use this to interpret the meta key
513 // and ISO third level shifts.
514 // http://stackoverflow.com/q/19877924/577598
515 exports
.isMac
= Generic_js_1
.contains(['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K'], platform
);
516 exports
.isIpad
= platform
=== 'iPad';
517 exports
.isIphone
= platform
=== 'iPhone';
518 exports
.isMSWindows
= Generic_js_1
.contains(['Windows', 'Win16', 'Win32', 'WinCE'], platform
);
520 },{"./Generic.js":6}],6:[function(require
,module
,exports
){
522 * Generic utilities module with methods that can be helpful at different parts of the code base.
523 * @module xterm/utils/Generic
528 * Return if the given array contains the given element
529 * @param {Array} array The array to search for the given element.
530 * @param {Object} el The element to look for into the array
532 exports
.contains = function (arr
, el
) {
533 return arr
.indexOf(el
) >= 0;
536 },{}],7:[function(require
,module
,exports
){
538 * xterm.js: xterm, in the browser
539 * Originally forked from (with the author's permission):
540 * Fabrice Bellard's javascript vt100 for jslinux:
541 * http://bellard.org/jslinux/
542 * Copyright (c) 2011 Fabrice Bellard
543 * The original design remains. The terminal itself
544 * has been extended to include xterm CSI codes, among
549 var CompositionHelper_js_1
= require('./CompositionHelper.js');
550 var EventEmitter_js_1
= require('./EventEmitter.js');
551 var Viewport_js_1
= require('./Viewport.js');
552 var Clipboard_js_1
= require('./handlers/Clipboard.js');
553 var Browser
= require('./utils/Browser');
555 * Terminal Emulation References:
557 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.txt
558 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
559 * http://invisible-island.net/vttest/
560 * http://www.inwap.com/pdp10/ansicode.txt
561 * http://linux.die.net/man/4/console_codes
562 * http://linux.die.net/man/7/urxvt
564 // Let it work inside Node.js for automated testing purposes.
565 var document
= (typeof window
!= 'undefined') ? window
.document
: null;
569 var normal
= 0, escaped
= 1, csi
= 2, osc
= 3, charset
= 4, dcs
= 5, ignore
= 6;
574 * Creates a new `Terminal` object.
576 * @param {object} options An object containing a set of options, the available options are:
577 * - `cursorBlink` (boolean): Whether the terminal cursor blinks
578 * - `cols` (number): The number of columns of the terminal (horizontal size)
579 * - `rows` (number): The number of rows of the terminal (vertical size)
583 * @alias module:xterm/src/xterm
585 function Terminal(options
) {
587 if (!(this instanceof Terminal
)) {
588 return new Terminal(arguments
[0], arguments
[1], arguments
[2]);
590 self
.browser
= Browser
;
591 self
.cancel
= Terminal
.cancel
;
592 EventEmitter_js_1
.EventEmitter
.call(this);
593 if (typeof options
=== 'number') {
597 handler
: arguments
[2]
600 options
= options
|| {};
601 Object
.keys(Terminal
.defaults
).forEach(function (key
) {
602 if (options
[key
] == null) {
603 options
[key
] = Terminal
.options
[key
];
604 if (Terminal
[key
] !== Terminal
.defaults
[key
]) {
605 options
[key
] = Terminal
[key
];
608 self
[key
] = options
[key
];
610 if (options
.colors
.length
=== 8) {
611 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(8));
613 else if (options
.colors
.length
=== 16) {
614 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(16));
616 else if (options
.colors
.length
=== 10) {
617 options
.colors
= options
.colors
.slice(0, -2).concat(Terminal
._colors
.slice(8, -2), options
.colors
.slice(-2));
619 else if (options
.colors
.length
=== 18) {
620 options
.colors
= options
.colors
.concat(Terminal
._colors
.slice(16, -2), options
.colors
.slice(-2));
622 this.colors
= options
.colors
;
623 this.options
= options
;
624 // this.context = options.context || window;
625 // this.document = options.document || document;
626 this.parent
= options
.body
|| options
.parent
|| (document
? document
.getElementsByTagName('body')[0] : null);
627 this.cols
= options
.cols
|| options
.geometry
[0];
628 this.rows
= options
.rows
|| options
.geometry
[1];
629 this.geometry
= [this.cols
, this.rows
];
630 if (options
.handler
) {
631 this.on('data', options
.handler
);
634 * The scroll position of the y cursor, ie. ybase + y = the y position within the entire
639 * The scroll position of the viewport
643 * The cursor's x position after ybase
647 * The cursor's y position after ybase
651 * Used to debounce the refresh function
653 this.isRefreshing
= false;
655 * Whether there is a full terminal refresh queued
657 this.cursorState
= 0;
658 this.cursorHidden
= false;
663 this.scrollBottom
= this.rows
- 1;
664 this.customKeydownHandler
= null;
666 this.applicationKeypad
= false;
667 this.applicationCursor
= false;
668 this.originMode
= false;
669 this.insertMode
= false;
670 this.wraparoundMode
= true; // defaults: xterm - true, vt100 - false
674 this.gcharset
= null;
676 this.charsets
= [null];
697 this.readable
= true;
698 this.writable
= true;
699 this.defAttr
= (0 << 18) | (257 << 9) | (256 << 0);
700 this.curAttr
= this.defAttr
;
702 this.currentParam
= 0;
705 // leftover surrogate high from previous write invocation
706 this.surrogate_high
= '';
708 * An array of all lines in the entire buffer, including the prompt. The lines are array of
709 * characters which are 2-length arrays where [0] is an attribute and [1] is the character.
714 this.lines
.push(this.blankLine());
718 // Store if user went browsing history in scrollback
719 this.userScrolling
= false;
721 inherits(Terminal
, EventEmitter_js_1
.EventEmitter
);
723 * back_color_erase feature for xterm.
725 Terminal
.prototype.eraseAttr = function () {
726 // if (this.is('screen')) return this.defAttr;
727 return (this.defAttr
& ~0x1ff) | (this.curAttr
& 0x1ff);
733 Terminal
.tangoColors
= [
753 // Colors 0-15 + 16-255
754 // Much thanks to TooTallNate for writing this.
755 Terminal
.colors
= (function () {
756 var colors
= Terminal
.tangoColors
.slice(), r
= [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff], i
;
759 for (; i
< 216; i
++) {
760 out(r
[(i
/ 36) % 6 | 0], r
[(i
/ 6) % 6 | 0], r
[i
% 6]);
764 for (; i
< 24; i
++) {
768 function out(r
, g
, b
) {
769 colors
.push('#' + hex(r
) + hex(g
) + hex(b
));
773 return c
.length
< 2 ? '0' + c
: c
;
777 Terminal
._colors
= Terminal
.colors
.slice();
778 Terminal
.vcolors
= (function () {
779 var out
= [], colors
= Terminal
.colors
, i
= 0, color
;
780 for (; i
< 256; i
++) {
781 color
= parseInt(colors
[i
].substring(1), 16);
783 (color
>> 16) & 0xff,
793 Terminal
.defaults
= {
794 colors
: Terminal
.colors
,
807 Terminal
.options
= {};
808 Terminal
.focus
= null;
809 each(keys(Terminal
.defaults
), function (key
) {
810 Terminal
[key
] = Terminal
.defaults
[key
];
811 Terminal
.options
[key
] = Terminal
.defaults
[key
];
814 * Focus the terminal. Delegates focus handling to the terminal's DOM element.
816 Terminal
.prototype.focus = function () {
817 return this.textarea
.focus();
820 * Retrieves an option's value from the terminal.
821 * @param {string} key The option key.
823 Terminal
.prototype.getOption = function (key
, value
) {
824 if (!(key
in Terminal
.defaults
)) {
825 throw new Error('No option with key "' + key
+ '"');
827 if (typeof this.options
[key
] !== 'undefined') {
828 return this.options
[key
];
833 * Sets an option on the terminal.
834 * @param {string} key The option key.
835 * @param {string} value The option value.
837 Terminal
.prototype.setOption = function (key
, value
) {
838 if (!(key
in Terminal
.defaults
)) {
839 throw new Error('No option with key "' + key
+ '"');
842 this.options
[key
] = value
;
845 * Binds the desired focus behavior on a given terminal object.
849 Terminal
.bindFocus = function (term
) {
850 on(term
.textarea
, 'focus', function (ev
) {
851 if (term
.sendFocus
) {
854 term
.element
.classList
.add('focus');
856 Terminal
.focus
= term
;
857 term
.emit('focus', { terminal
: term
});
861 * Blur the terminal. Delegates blur handling to the terminal's DOM element.
863 Terminal
.prototype.blur = function () {
864 return this.textarea
.blur();
867 * Binds the desired blur behavior on a given terminal object.
871 Terminal
.bindBlur = function (term
) {
872 on(term
.textarea
, 'blur', function (ev
) {
873 term
.refresh(term
.y
, term
.y
);
874 if (term
.sendFocus
) {
877 term
.element
.classList
.remove('focus');
878 Terminal
.focus
= null;
879 term
.emit('blur', { terminal
: term
});
883 * Initialize default behavior
885 Terminal
.prototype.initGlobal = function () {
887 Terminal
.bindKeys(this);
888 Terminal
.bindFocus(this);
889 Terminal
.bindBlur(this);
890 // Bind clipboard functionality
891 on(this.element
, 'copy', function (ev
) {
892 Clipboard_js_1
.copyHandler
.call(this, ev
, term
);
894 on(this.textarea
, 'paste', function (ev
) {
895 Clipboard_js_1
.pasteHandler
.call(this, ev
, term
);
897 function rightClickHandlerWrapper(ev
) {
898 Clipboard_js_1
.rightClickHandler
.call(this, ev
, term
);
900 if (term
.browser
.isFirefox
) {
901 on(this.element
, 'mousedown', function (ev
) {
902 if (ev
.button
== 2) {
903 rightClickHandlerWrapper(ev
);
908 on(this.element
, 'contextmenu', rightClickHandlerWrapper
);
912 * Apply key handling to the terminal
914 Terminal
.bindKeys = function (term
) {
915 on(term
.element
, 'keydown', function (ev
) {
916 if (document
.activeElement
!= this) {
921 on(term
.element
, 'keypress', function (ev
) {
922 if (document
.activeElement
!= this) {
927 on(term
.element
, 'keyup', term
.focus
.bind(term
));
928 on(term
.textarea
, 'keydown', function (ev
) {
931 on(term
.textarea
, 'keypress', function (ev
) {
933 // Truncate the textarea's value, since it is not needed
936 on(term
.textarea
, 'compositionstart', term
.compositionHelper
.compositionstart
.bind(term
.compositionHelper
));
937 on(term
.textarea
, 'compositionupdate', term
.compositionHelper
.compositionupdate
.bind(term
.compositionHelper
));
938 on(term
.textarea
, 'compositionend', term
.compositionHelper
.compositionend
.bind(term
.compositionHelper
));
939 term
.on('refresh', term
.compositionHelper
.updateCompositionElements
.bind(term
.compositionHelper
));
942 * Insert the given row to the terminal or produce a new one
943 * if no row argument is passed. Return the inserted row.
944 * @param {HTMLElement} row (optional) The row to append to the terminal.
946 Terminal
.prototype.insertRow = function (row
) {
947 if (typeof row
!= 'object') {
948 row
= document
.createElement('div');
950 this.rowContainer
.appendChild(row
);
951 this.children
.push(row
);
955 * Opens the terminal within an element.
957 * @param {HTMLElement} parent The element to create the terminal within.
959 Terminal
.prototype.open = function (parent
) {
960 var self
= this, i
= 0, div
;
961 this.parent
= parent
|| this.parent
;
963 throw new Error('Terminal requires a parent element.');
965 // Grab global elements
966 this.context
= this.parent
.ownerDocument
.defaultView
;
967 this.document
= this.parent
.ownerDocument
;
968 this.body
= this.document
.getElementsByTagName('body')[0];
969 //Create main element container
970 this.element
= this.document
.createElement('div');
971 this.element
.classList
.add('terminal');
972 this.element
.classList
.add('xterm');
973 this.element
.classList
.add('xterm-theme-' + this.theme
);
974 this.element
.style
.height
;
975 this.element
.setAttribute('tabindex', 0);
976 this.viewportElement
= document
.createElement('div');
977 this.viewportElement
.classList
.add('xterm-viewport');
978 this.element
.appendChild(this.viewportElement
);
979 this.viewportScrollArea
= document
.createElement('div');
980 this.viewportScrollArea
.classList
.add('xterm-scroll-area');
981 this.viewportElement
.appendChild(this.viewportScrollArea
);
982 // Create the container that will hold the lines of the terminal and then
983 // produce the lines the lines.
984 this.rowContainer
= document
.createElement('div');
985 this.rowContainer
.classList
.add('xterm-rows');
986 this.element
.appendChild(this.rowContainer
);
988 // Create the container that will hold helpers like the textarea for
989 // capturing DOM Events. Then produce the helpers.
990 this.helperContainer
= document
.createElement('div');
991 this.helperContainer
.classList
.add('xterm-helpers');
992 // TODO: This should probably be inserted once it's filled to prevent an additional layout
993 this.element
.appendChild(this.helperContainer
);
994 this.textarea
= document
.createElement('textarea');
995 this.textarea
.classList
.add('xterm-helper-textarea');
996 this.textarea
.setAttribute('autocorrect', 'off');
997 this.textarea
.setAttribute('autocapitalize', 'off');
998 this.textarea
.setAttribute('spellcheck', 'false');
999 this.textarea
.tabIndex
= 0;
1000 this.textarea
.addEventListener('focus', function () {
1001 self
.emit('focus', { terminal
: self
});
1003 this.textarea
.addEventListener('blur', function () {
1004 self
.emit('blur', { terminal
: self
});
1006 this.helperContainer
.appendChild(this.textarea
);
1007 this.compositionView
= document
.createElement('div');
1008 this.compositionView
.classList
.add('composition-view');
1009 this.compositionHelper
= new CompositionHelper_js_1
.CompositionHelper(this.textarea
, this.compositionView
, this);
1010 this.helperContainer
.appendChild(this.compositionView
);
1011 this.charMeasureElement
= document
.createElement('div');
1012 this.charMeasureElement
.classList
.add('xterm-char-measure-element');
1013 this.charMeasureElement
.innerHTML
= 'W';
1014 this.helperContainer
.appendChild(this.charMeasureElement
);
1015 for (; i
< this.rows
; i
++) {
1018 this.parent
.appendChild(this.element
);
1019 this.viewport
= new Viewport_js_1
.Viewport(this, this.viewportElement
, this.viewportScrollArea
, this.charMeasureElement
);
1021 this.refresh(0, this.rows
- 1);
1022 // Initialize global actions that
1023 // need to be taken on the document.
1025 // Ensure there is a Terminal.focus.
1027 on(this.element
, 'click', function () {
1028 var selection
= document
.getSelection(), collapsed
= selection
.isCollapsed
, isRange
= typeof collapsed
== 'boolean' ? !collapsed
: selection
.type
== 'Range';
1033 // Listen for mouse events and translate
1034 // them into terminal mouse protocols.
1036 // Figure out whether boldness affects
1037 // the character width of monospace fonts.
1038 if (Terminal
.brokenBold
== null) {
1039 Terminal
.brokenBold
= isBoldBroken(this.document
);
1042 * This event is emitted when terminal has completed opening.
1049 * Attempts to load an add-on using CommonJS or RequireJS (whichever is available).
1050 * @param {string} addon The name of the addon to load
1053 Terminal
.loadAddon = function (addon
, callback
) {
1054 if (typeof exports
=== 'object' && typeof module
=== 'object') {
1056 return require('./addons/' + addon
+ '/' + addon
);
1058 else if (typeof define
== 'function') {
1060 return require(['./addons/' + addon
+ '/' + addon
], callback
);
1063 console
.error('Cannot load a module without a CommonJS or RequireJS environment.');
1068 * XTerm mouse events
1069 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#Mouse%20Tracking
1070 * To better understand these
1071 * the xterm code is very helpful:
1073 * button.c, charproc.c, misc.c
1074 * Relevant functions in xterm/button.c:
1075 * BtnCode, EmitButtonCode, EditorButton, SendMousePosition
1077 Terminal
.prototype.bindMouse = function () {
1078 var el
= this.element
, self
= this, pressed
= 32;
1079 // mouseup, mousedown, wheel
1080 // left click: ^[[M 3<^[[M#3<
1081 // wheel up: ^[[M`3>
1082 function sendButton(ev
) {
1084 // get the xterm-style button
1085 button
= getButton(ev
);
1086 // get mouse coordinates
1087 pos
= getCoords(ev
);
1090 sendEvent(button
, pos
);
1091 switch (ev
.overrideType
|| ev
.type
) {
1096 // keep it at the left
1097 // button, just in case.
1107 // motion example of a left click:
1108 // ^[[M 3<^[[M@4<^[[M@5<^[[M@6<^[[M@7<^[[M#7<
1109 function sendMove(ev
) {
1110 var button
= pressed
, pos
;
1111 pos
= getCoords(ev
);
1114 // buttons marked as motions
1115 // are incremented by 32
1117 sendEvent(button
, pos
);
1119 // encode button and
1120 // position to characters
1121 function encode(data
, ch
) {
1122 if (!self
.utfMouse
) {
1124 return data
.push(0);
1131 return data
.push(0);
1138 data
.push(0xC0 | (ch
>> 6));
1139 data
.push(0x80 | (ch
& 0x3F));
1143 // send a mouse event:
1144 // regular/utf8: ^[[M Cb Cx Cy
1145 // urxvt: ^[[ Cb ; Cx ; Cy M
1146 // sgr: ^[[ Cb ; Cx ; Cy M/m
1147 // vt300: ^[[ 24(1/3/5)~ [ Cx , Cy ] \r
1148 // locator: CSI P e ; P b ; P r ; P c ; P p & w
1149 function sendEvent(button
, pos
) {
1150 // self.emit('mouse', {
1155 if (self
.vt300Mouse
) {
1157 // http://www.vt100.net/docs/vt3xx-gp/chapter15.html
1161 var data
= '\x1b[24';
1164 else if (button
=== 1)
1166 else if (button
=== 2)
1168 else if (button
=== 3)
1172 data
+= '~[' + pos
.x
+ ',' + pos
.y
+ ']\r';
1176 if (self
.decLocator
) {
1183 else if (button
=== 1)
1185 else if (button
=== 2)
1187 else if (button
=== 3)
1192 + (button
=== 3 ? 4 : 0)
1202 if (self
.urxvtMouse
) {
1207 self
.send('\x1b[' + button
+ ';' + pos
.x
+ ';' + pos
.y
+ 'M');
1210 if (self
.sgrMouse
) {
1214 + ((button
& 3) === 3 ? button
& ~3 : button
)
1219 + ((button
& 3) === 3 ? 'm' : 'M'));
1223 encode(data
, button
);
1224 encode(data
, pos
.x
);
1225 encode(data
, pos
.y
);
1226 self
.send('\x1b[M' + String
.fromCharCode
.apply(String
, data
));
1228 function getButton(ev
) {
1229 var button
, shift
, meta
, ctrl
, mod
;
1236 // 1, and 2 - with 64 added
1237 switch (ev
.overrideType
|| ev
.type
) {
1239 button
= ev
.button
!= null
1244 if (self
.browser
.isMSIE
) {
1245 button
= button
=== 1 ? 0 : button
=== 4 ? 1 : button
;
1251 case 'DOMMouseScroll':
1252 button
= ev
.detail
< 0
1257 button
= ev
.wheelDeltaY
> 0
1262 // next three bits are the modifiers:
1263 // 4 = shift, 8 = meta, 16 = control
1264 shift
= ev
.shiftKey
? 4 : 0;
1265 meta
= ev
.metaKey
? 8 : 0;
1266 ctrl
= ev
.ctrlKey
? 16 : 0;
1267 mod
= shift
| meta
| ctrl
;
1269 if (self
.vt200Mouse
) {
1273 else if (!self
.normalMouse
) {
1277 button
= (32 + (mod
<< 2)) + button
;
1280 // mouse coordinates measured in cols/rows
1281 function getCoords(ev
) {
1283 // ignore browsers without pageX for now
1284 if (ev
.pageX
== null)
1289 // should probably check offsetParent
1290 // but this is more portable
1291 while (el
&& el
!== self
.document
.documentElement
) {
1294 el
= 'offsetParent' in el
1298 // convert to cols/rows
1299 w
= self
.element
.clientWidth
;
1300 h
= self
.element
.clientHeight
;
1301 x
= Math
.ceil((x
/ w
) * self
.cols
);
1302 y
= Math
.ceil((y
/ h
) * self
.rows
);
1303 // be sure to avoid sending
1304 // bad positions to the program
1313 // xterm sends raw bytes and
1314 // starts at 32 (SP) for each.
1323 on(el
, 'mousedown', function (ev
) {
1324 if (!self
.mouseEvents
)
1331 //if (self.vt200Mouse && !self.normalMouse) {
1332 if (self
.vt200Mouse
) {
1333 ev
.overrideType
= 'mouseup';
1335 return self
.cancel(ev
);
1338 if (self
.normalMouse
)
1339 on(self
.document
, 'mousemove', sendMove
);
1340 // x10 compatibility mode can't send button releases
1341 if (!self
.x10Mouse
) {
1342 on(self
.document
, 'mouseup', function up(ev
) {
1344 if (self
.normalMouse
)
1345 off(self
.document
, 'mousemove', sendMove
);
1346 off(self
.document
, 'mouseup', up
);
1347 return self
.cancel(ev
);
1350 return self
.cancel(ev
);
1352 //if (self.normalMouse) {
1353 // on(self.document, 'mousemove', sendMove);
1355 on(el
, 'wheel', function (ev
) {
1356 if (!self
.mouseEvents
)
1363 return self
.cancel(ev
);
1365 // allow wheel scrolling in
1366 // the shell for example
1367 on(el
, 'wheel', function (ev
) {
1368 if (self
.mouseEvents
)
1370 self
.viewport
.onWheel(ev
);
1371 return self
.cancel(ev
);
1375 * Destroys the terminal.
1377 Terminal
.prototype.destroy = function () {
1378 this.readable
= false;
1379 this.writable
= false;
1381 this.handler = function () { };
1382 this.write = function () { };
1383 if (this.element
.parentNode
) {
1384 this.element
.parentNode
.removeChild(this.element
);
1386 //this.emit('close');
1389 * Flags used to render terminal text properly
1399 * Refreshes (re-renders) terminal content within two rows (inclusive)
1403 * In the screen buffer, each character is stored as a an array with a character
1404 * and a 32-bit integer:
1405 * - First value: a utf-16 character.
1407 * - Next 9 bits: background color (0-511).
1408 * - Next 9 bits: foreground color (0-511).
1409 * - Next 14 bits: a mask for misc. flags:
1416 * @param {number} start The row to start from (between 0 and terminal's height terminal - 1)
1417 * @param {number} end The row to end at (between fromRow and terminal's height terminal - 1)
1418 * @param {boolean} queue Whether the refresh should ran right now or be queued
1420 Terminal
.prototype.refresh = function (start
, end
, queue
) {
1422 // queue defaults to true
1423 queue
= (typeof queue
== 'undefined') ? true : queue
;
1425 * The refresh queue allows refresh to execute only approximately 30 times a second. For
1426 * commands that pass a significant amount of output to the write function, this prevents the
1427 * terminal from maxing out the CPU and making the UI unresponsive. While commands can still
1428 * run beyond what they do on the terminal, it is far better with a debounce in place as
1429 * every single terminal manipulation does not need to be constructed in the DOM.
1431 * A side-effect of this is that it makes ^C to interrupt a process seem more responsive.
1434 // If refresh should be queued, order the refresh and return.
1435 if (this._refreshIsQueued
) {
1436 // If a refresh has already been queued, just order a full refresh next
1437 this._fullRefreshNext
= true;
1440 setTimeout(function () {
1441 self
.refresh(start
, end
, false);
1443 this._refreshIsQueued
= true;
1447 // If refresh should be run right now (not be queued), release the lock
1448 this._refreshIsQueued
= false;
1449 // If multiple refreshes were requested, make a full refresh.
1450 if (this._fullRefreshNext
) {
1452 end
= this.rows
- 1;
1453 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
;
1456 // If this is a big refresh, remove the terminal rows from the DOM for faster calculations
1457 if (end
- start
>= this.rows
/ 2) {
1458 parent
= this.element
.parentNode
;
1460 this.element
.removeChild(this.rowContainer
);
1465 if (end
>= this.rows
.length
) {
1466 this.log('`end` is too large. Most likely a bad CSR.');
1467 end
= this.rows
.length
- 1;
1469 for (; y
<= end
; y
++) {
1470 row
= y
+ this.ydisp
;
1471 line
= this.lines
[row
];
1473 if (this.y
=== y
- (this.ybase
- this.ydisp
)
1475 && !this.cursorHidden
) {
1481 attr
= this.defAttr
;
1483 for (; i
< width
; i
++) {
1486 ch_width
= line
[i
][2];
1491 if (data
!== attr
) {
1492 if (attr
!== this.defAttr
) {
1495 if (data
!== this.defAttr
) {
1497 out
+= '<span class="reverse-video terminal-cursor';
1498 if (this.cursorBlink
) {
1504 var classNames
= [];
1506 fg
= (data
>> 9) & 0x1ff;
1508 if (flags
& Terminal
.flags
.BOLD
) {
1509 if (!Terminal
.brokenBold
) {
1510 classNames
.push('xterm-bold');
1512 // See: XTerm*boldColors
1516 if (flags
& Terminal
.flags
.UNDERLINE
) {
1517 classNames
.push('xterm-underline');
1519 if (flags
& Terminal
.flags
.BLINK
) {
1520 classNames
.push('xterm-blink');
1522 // If inverse flag is on, then swap the foreground and background variables.
1523 if (flags
& Terminal
.flags
.INVERSE
) {
1524 /* One-line variable swap in JavaScript: http://stackoverflow.com/a/16201730 */
1525 bg = [fg, fg = bg][0];
1526 // Should inverse just be before the
1527 // above boldColors effect instead?
1528 if ((flags & 1) && fg < 8)
1531 if (flags & Terminal.flags.INVISIBLE) {
1532 classNames.push('xterm-hidden');
1535 * Weird situation: Invert flag used black foreground and white background results
1536 * in invalid background color, positioned at the 256 index of the 256 terminal
1537 * color map. Pin the colors manually in such a case.
1539 * Source: https://github.com/sourcelair/xterm.js/issues/57
1541 if (flags
& Terminal
.flags
.INVERSE
) {
1550 classNames
.push('xterm-bg-color-' + bg
);
1553 classNames
.push('xterm-color-' + fg
);
1556 if (classNames
.length
) {
1557 out
+= ' class="' + classNames
.join(' ') + '"';
1584 if (attr
!== this.defAttr
) {
1587 this.children
[y
].innerHTML
= out
;
1590 this.element
.appendChild(this.rowContainer
);
1592 this.emit('refresh', { element
: this.element
, start
: start
, end
: end
});
1595 * Display the cursor element
1597 Terminal
.prototype.showCursor = function () {
1598 if (!this.cursorState
) {
1599 this.cursorState
= 1;
1600 this.refresh(this.y
, this.y
);
1604 * Scroll the terminal
1606 Terminal
.prototype.scroll = function () {
1608 if (++this.ybase
=== this.scrollback
) {
1609 this.ybase
= this.ybase
/ 2 | 0;
1610 this.lines
= this.lines
.slice(-(this.ybase
+ this.rows
) + 1);
1612 if (!this.userScrolling
) {
1613 this.ydisp
= this.ybase
;
1616 row
= this.ybase
+ this.rows
- 1;
1617 // subtract the bottom scroll region
1618 row
-= this.rows
- 1 - this.scrollBottom
;
1619 if (row
=== this.lines
.length
) {
1620 // potential optimization:
1621 // pushing is faster than splicing
1622 // when they amount to the same
1624 this.lines
.push(this.blankLine());
1628 this.lines
.splice(row
, 0, this.blankLine());
1630 if (this.scrollTop
!== 0) {
1631 if (this.ybase
!== 0) {
1633 if (!this.userScrolling
) {
1634 this.ydisp
= this.ybase
;
1637 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
1640 this.updateRange(this.scrollTop
);
1641 this.updateRange(this.scrollBottom
);
1643 * This event is emitted whenever the terminal is scrolled.
1644 * The one parameter passed is the new y display position.
1648 this.emit('scroll', this.ydisp
);
1651 * Scroll the display of the terminal
1652 * @param {number} disp The number of lines to scroll down (negatives scroll up).
1653 * @param {boolean} suppressScrollEvent Don't emit the scroll event as scrollDisp. This is used
1654 * to avoid unwanted events being handled by the veiwport when the event was triggered from the
1655 * viewport originally.
1657 Terminal
.prototype.scrollDisp = function (disp
, suppressScrollEvent
) {
1659 this.userScrolling
= true;
1661 else if (disp
+ this.ydisp
>= this.ybase
) {
1662 this.userScrolling
= false;
1665 if (this.ydisp
> this.ybase
) {
1666 this.ydisp
= this.ybase
;
1668 else if (this.ydisp
< 0) {
1671 if (!suppressScrollEvent
) {
1672 this.emit('scroll', this.ydisp
);
1674 this.refresh(0, this.rows
- 1);
1677 * Scroll the display of the terminal by a number of pages.
1678 * @param {number} pageCount The number of pages to scroll (negative scrolls up).
1680 Terminal
.prototype.scrollPages = function (pageCount
) {
1681 this.scrollDisp(pageCount
* (this.rows
- 1));
1684 * Scrolls the display of the terminal to the top.
1686 Terminal
.prototype.scrollToTop = function () {
1687 this.scrollDisp(-this.ydisp
);
1690 * Scrolls the display of the terminal to the bottom.
1692 Terminal
.prototype.scrollToBottom = function () {
1693 this.scrollDisp(this.ybase
- this.ydisp
);
1696 * Writes text to the terminal.
1697 * @param {string} text The text to write to the terminal.
1699 Terminal
.prototype.write = function (data
) {
1700 var l
= data
.length
, i
= 0, j
, cs
, ch
, code
, low
, ch_width
, row
;
1701 this.refreshStart
= this.y
;
1702 this.refreshEnd
= this.y
;
1703 // apply leftover surrogate high from last write
1704 if (this.surrogate_high
) {
1705 data
= this.surrogate_high
+ data
;
1706 this.surrogate_high
= '';
1708 for (; i
< l
; i
++) {
1710 // FIXME: higher chars than 0xa0 are not allowed in escape sequences
1711 // --> maybe move to default
1712 code
= data
.charCodeAt(i
);
1713 if (0xD800 <= code
&& code
<= 0xDBFF) {
1714 // we got a surrogate high
1715 // get surrogate low (next 2 bytes)
1716 low
= data
.charCodeAt(i
+ 1);
1718 // end of data stream, save surrogate high
1719 this.surrogate_high
= ch
;
1722 code
= ((code
- 0xD800) * 0x400) + (low
- 0xDC00) + 0x10000;
1723 ch
+= data
.charAt(i
+ 1);
1725 // surrogate low - already handled above
1726 if (0xDC00 <= code
&& code
<= 0xDFFF)
1728 switch (this.state
) {
1738 if (this.convertEol
) {
1742 if (this.y
> this.scrollBottom
) {
1759 this.x
= this.nextStop();
1771 this.state
= escaped
;
1775 // calculate print space
1776 // expensive call, therefore we save width in line buffer
1777 ch_width
= wcwidth(code
);
1779 if (this.charset
&& this.charset
[ch
]) {
1780 ch
= this.charset
[ch
];
1782 row
= this.y
+ this.ybase
;
1783 // insert combining char in last cell
1784 // FIXME: needs handling after cursor jumps
1785 if (!ch_width
&& this.x
) {
1786 // dont overflow left
1787 if (this.lines
[row
][this.x
- 1]) {
1788 if (!this.lines
[row
][this.x
- 1][2]) {
1789 // found empty cell after fullwidth, need to go 2 cells back
1790 if (this.lines
[row
][this.x
- 2])
1791 this.lines
[row
][this.x
- 2][1] += ch
;
1794 this.lines
[row
][this.x
- 1][1] += ch
;
1796 this.updateRange(this.y
);
1800 // goto next line if ch would overflow
1801 // TODO: needs a global min terminal width of 2
1802 if (this.x
+ ch_width
- 1 >= this.cols
) {
1803 // autowrap - DECAWM
1804 if (this.wraparoundMode
) {
1807 if (this.y
> this.scrollBottom
) {
1813 this.x
= this.cols
- 1;
1818 row
= this.y
+ this.ybase
;
1819 // insert mode: move characters to right
1820 if (this.insertMode
) {
1821 // do this twice for a fullwidth char
1822 for (var moves
= 0; moves
< ch_width
; ++moves
) {
1823 // remove last cell, if it's width is 0
1824 // we have to adjust the second last cell as well
1825 var removed
= this.lines
[this.y
+ this.ybase
].pop();
1826 if (removed
[2] === 0
1827 && this.lines
[row
][this.cols
- 2]
1828 && this.lines
[row
][this.cols
- 2][2] === 2)
1829 this.lines
[row
][this.cols
- 2] = [this.curAttr
, ' ', 1];
1830 // insert empty cell at cursor
1831 this.lines
[row
].splice(this.x
, 0, [this.curAttr
, ' ', 1]);
1834 this.lines
[row
][this.x
] = [this.curAttr
, ch
, ch_width
];
1836 this.updateRange(this.y
);
1837 // fullwidth char - set next cell width to zero and advance cursor
1838 if (ch_width
=== 2) {
1839 this.lines
[row
][this.x
] = [this.curAttr
, '', 0];
1848 // ESC [ Control Sequence Introducer ( CSI is 0x9b).
1851 this.currentParam
= 0;
1854 // ESC ] Operating System Command ( OSC is 0x9d).
1857 this.currentParam
= 0;
1860 // ESC P Device Control String ( DCS is 0x90).
1863 this.currentParam
= 0;
1866 // ESC _ Application Program Command ( APC is 0x9f).
1868 this.state
= ignore
;
1870 // ESC ^ Privacy Message ( PM is 0x9e).
1872 this.state
= ignore
;
1874 // ESC c Full Reset (RIS).
1878 // ESC E Next Line ( NEL is 0x85).
1879 // ESC D Index ( IND is 0x84).
1886 // ESC M Reverse Index ( RI is 0x8d).
1888 this.reverseIndex();
1890 // ESC % Select default/utf-8 character set.
1891 // @ = default, G = utf-8
1893 //this.charset = null;
1895 this.setgCharset(0, Terminal
.charsets
.US
);
1896 this.state
= normal
;
1899 // ESC (,),*,+,-,. Designate G0-G2 Character Set.
1900 case '(': // <-- this seems to get all the attention
1926 this.state
= charset
;
1928 // Designate G3 Character Set (VT300).
1929 // A = ISO Latin-1 Supplemental.
1933 this.state
= charset
;
1937 // Single Shift Select of G2 Character Set
1938 // ( SS2 is 0x8e). This affects next character only.
1942 // Single Shift Select of G3 Character Set
1943 // ( SS3 is 0x8f). This affects next character only.
1947 // Invoke the G2 Character Set as GL (LS2).
1952 // Invoke the G3 Character Set as GL (LS3).
1957 // Invoke the G3 Character Set as GR (LS3R).
1962 // Invoke the G2 Character Set as GR (LS2R).
1967 // Invoke the G1 Character Set as GR (LS1R).
1971 // ESC 7 Save Cursor (DECSC).
1974 this.state
= normal
;
1976 // ESC 8 Restore Cursor (DECRC).
1978 this.restoreCursor();
1979 this.state
= normal
;
1981 // ESC # 3 DEC line height/width
1983 this.state
= normal
;
1986 // ESC H Tab Set (HTS is 0x88).
1990 // ESC = Application Keypad (DECKPAM).
1992 this.log('Serial port requested application keypad.');
1993 this.applicationKeypad
= true;
1994 this.viewport
.syncScrollArea();
1995 this.state
= normal
;
1997 // ESC > Normal Keypad (DECKPNM).
1999 this.log('Switching back to normal keypad.');
2000 this.applicationKeypad
= false;
2001 this.viewport
.syncScrollArea();
2002 this.state
= normal
;
2005 this.state
= normal
;
2006 this.error('Unknown ESC control: %s.', ch
);
2013 cs
= Terminal
.charsets
.SCLD
;
2016 cs
= Terminal
.charsets
.UK
;
2019 cs
= Terminal
.charsets
.US
;
2022 cs
= Terminal
.charsets
.Dutch
;
2024 case 'C': // Finnish
2026 cs
= Terminal
.charsets
.Finnish
;
2029 cs
= Terminal
.charsets
.French
;
2032 cs
= Terminal
.charsets
.FrenchCanadian
;
2035 cs
= Terminal
.charsets
.German
;
2038 cs
= Terminal
.charsets
.Italian
;
2040 case 'E': // NorwegianDanish
2042 cs
= Terminal
.charsets
.NorwegianDanish
;
2045 cs
= Terminal
.charsets
.Spanish
;
2047 case 'H': // Swedish
2049 cs
= Terminal
.charsets
.Swedish
;
2052 cs
= Terminal
.charsets
.Swiss
;
2055 cs
= Terminal
.charsets
.ISOLatin
;
2059 cs
= Terminal
.charsets
.US
;
2062 this.setgCharset(this.gcharset
, cs
);
2063 this.gcharset
= null;
2064 this.state
= normal
;
2069 // Set Text Parameters.
2070 if (ch
=== '\x1b' || ch
=== '\x07') {
2073 this.params
.push(this.currentParam
);
2074 switch (this.params
[0]) {
2078 if (this.params
[1]) {
2079 this.title
= this.params
[1];
2080 this.handleTitle(this.title
);
2088 // change dynamic colors
2100 // change dynamic ui colors
2112 // manipulate selection data
2129 this.currentParam
= 0;
2130 this.state
= normal
;
2133 if (!this.params
.length
) {
2134 if (ch
>= '0' && ch
<= '9') {
2136 this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
2138 else if (ch
=== ';') {
2139 this.params
.push(this.currentParam
);
2140 this.currentParam
= '';
2144 this.currentParam
+= ch
;
2150 if (ch
=== '?' || ch
=== '>' || ch
=== '!') {
2155 if (ch
>= '0' && ch
<= '9') {
2156 this.currentParam
= this.currentParam
* 10 + ch
.charCodeAt(0) - 48;
2159 // '$', '"', ' ', '\''
2160 if (ch
=== '$' || ch
=== '"' || ch
=== ' ' || ch
=== '\'') {
2164 this.params
.push(this.currentParam
);
2165 this.currentParam
= 0;
2169 this.state
= normal
;
2172 // Cursor Up Ps Times (default = 1) (CUU).
2174 this.cursorUp(this.params
);
2177 // Cursor Down Ps Times (default = 1) (CUD).
2179 this.cursorDown(this.params
);
2182 // Cursor Forward Ps Times (default = 1) (CUF).
2184 this.cursorForward(this.params
);
2187 // Cursor Backward Ps Times (default = 1) (CUB).
2189 this.cursorBackward(this.params
);
2192 // Cursor Position [row;column] (default = [1,1]) (CUP).
2194 this.cursorPos(this.params
);
2196 // CSI Ps J Erase in Display (ED).
2198 this.eraseInDisplay(this.params
);
2200 // CSI Ps K Erase in Line (EL).
2202 this.eraseInLine(this.params
);
2204 // CSI Pm m Character Attributes (SGR).
2207 this.charAttributes(this.params
);
2210 // CSI Ps n Device Status Report (DSR).
2213 this.deviceStatus(this.params
);
2220 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
2222 this.insertChars(this.params
);
2225 // Cursor Next Line Ps Times (default = 1) (CNL).
2227 this.cursorNextLine(this.params
);
2230 // Cursor Preceding Line Ps Times (default = 1) (CNL).
2232 this.cursorPrecedingLine(this.params
);
2235 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
2237 this.cursorCharAbsolute(this.params
);
2240 // Insert Ps Line(s) (default = 1) (IL).
2242 this.insertLines(this.params
);
2245 // Delete Ps Line(s) (default = 1) (DL).
2247 this.deleteLines(this.params
);
2250 // Delete Ps Character(s) (default = 1) (DCH).
2252 this.deleteChars(this.params
);
2255 // Erase Ps Character(s) (default = 1) (ECH).
2257 this.eraseChars(this.params
);
2259 // CSI Pm ` Character Position Absolute
2260 // [column] (default = [row,1]) (HPA).
2262 this.charPosAbsolute(this.params
);
2265 // Horizontal Position Relative
2267 this.HPositionRelative(this.params
);
2270 // Send Device Attributes (Primary DA).
2272 // Send Device Attributes (Secondary DA)
2274 this.sendDeviceAttributes(this.params
);
2277 // Line Position Absolute [row] (default = [1,column]) (VPA).
2279 this.linePosAbsolute(this.params
);
2281 // 145 65 e * VPR - Vertical Position Relative
2283 this.VPositionRelative(this.params
);
2286 // Horizontal and Vertical Position [row;column] (default =
2289 this.HVPosition(this.params
);
2291 // CSI Pm h Set Mode (SM).
2292 // CSI ? Pm h - mouse escape codes, cursor escape codes
2294 this.setMode(this.params
);
2296 // CSI Pm l Reset Mode (RM).
2299 this.resetMode(this.params
);
2302 // Set Scrolling Region [top;bottom] (default = full size of win-
2306 this.setScrollRegion(this.params
);
2309 // Save cursor (ANSI.SYS).
2311 this.saveCursor(this.params
);
2314 // Restore cursor (ANSI.SYS).
2316 this.restoreCursor(this.params
);
2322 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
2324 this.cursorForwardTab(this.params
);
2326 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
2328 this.scrollUp(this.params
);
2330 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
2331 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
2334 // if (this.prefix === '>') {
2335 // this.resetTitleModes(this.params);
2338 // if (this.params.length > 2) {
2339 // this.initMouseTracking(this.params);
2342 if (this.params
.length
< 2 && !this.prefix
) {
2343 this.scrollDown(this.params
);
2347 // Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
2349 this.cursorBackwardTab(this.params
);
2351 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
2353 this.repeatPrecedingCharacter(this.params
);
2355 // CSI Ps g Tab Clear (TBC).
2357 this.tabClear(this.params
);
2359 // CSI Pm i Media Copy (MC).
2362 // this.mediaCopy(this.params);
2364 // CSI Pm m Character Attributes (SGR).
2366 // case 'm': // duplicate
2367 // if (this.prefix === '>') {
2368 // this.setResources(this.params);
2370 // this.charAttributes(this.params);
2373 // CSI Ps n Device Status Report (DSR).
2375 // case 'n': // duplicate
2376 // if (this.prefix === '>') {
2377 // this.disableModifiers(this.params);
2379 // this.deviceStatus(this.params);
2382 // CSI > Ps p Set pointer mode.
2383 // CSI ! p Soft terminal reset (DECSTR).
2385 // Request ANSI mode (DECRQM).
2387 // Request DEC private mode (DECRQM).
2390 switch (this.prefix
) {
2392 // this.setPointerMode(this.params);
2395 this.softReset(this.params
);
2399 // CSI Ps q Load LEDs (DECLL).
2403 // if (this.postfix === ' ') {
2404 // this.setCursorStyle(this.params);
2407 // if (this.postfix === '"') {
2408 // this.setCharProtectionAttr(this.params);
2411 // this.loadLEDs(this.params);
2414 // Set Scrolling Region [top;bottom] (default = full size of win-
2417 // CSI Pt; Pl; Pb; Pr; Ps$ r
2418 // case 'r': // duplicate
2419 // if (this.prefix === '?') {
2420 // this.restorePrivateValues(this.params);
2421 // } else if (this.postfix === '$') {
2422 // this.setAttrInRectangle(this.params);
2424 // this.setScrollRegion(this.params);
2427 // CSI s Save cursor (ANSI.SYS).
2429 // case 's': // duplicate
2430 // if (this.prefix === '?') {
2431 // this.savePrivateValues(this.params);
2433 // this.saveCursor(this.params);
2436 // CSI Ps ; Ps ; Ps t
2437 // CSI Pt; Pl; Pb; Pr; Ps$ t
2441 // if (this.postfix === '$') {
2442 // this.reverseAttrInRectangle(this.params);
2443 // } else if (this.postfix === ' ') {
2444 // this.setWarningBellVolume(this.params);
2446 // if (this.prefix === '>') {
2447 // this.setTitleModeFeature(this.params);
2449 // this.manipulateWindow(this.params);
2453 // CSI u Restore cursor (ANSI.SYS).
2455 // case 'u': // duplicate
2456 // if (this.postfix === ' ') {
2457 // this.setMarginBellVolume(this.params);
2459 // this.restoreCursor(this.params);
2462 // CSI Pt; Pl; Pb; Pr; Pp; Pt; Pl; Pp$ v
2464 // if (this.postfix === '$') {
2465 // this.copyRectagle(this.params);
2468 // CSI Pt ; Pl ; Pb ; Pr ' w
2470 // if (this.postfix === '\'') {
2471 // this.enableFilterRectangle(this.params);
2474 // CSI Ps x Request Terminal Parameters (DECREQTPARM).
2475 // CSI Ps x Select Attribute Change Extent (DECSACE).
2476 // CSI Pc; Pt; Pl; Pb; Pr$ x
2478 // if (this.postfix === '$') {
2479 // this.fillRectangle(this.params);
2481 // this.requestParameters(this.params);
2482 // //this.__(this.params);
2486 // CSI Pt; Pl; Pb; Pr$ z
2488 // if (this.postfix === '\'') {
2489 // this.enableLocatorReporting(this.params);
2490 // } else if (this.postfix === '$') {
2491 // this.eraseRectangle(this.params);
2495 // CSI Pt; Pl; Pb; Pr$ {
2497 // if (this.postfix === '\'') {
2498 // this.setLocatorEvents(this.params);
2499 // } else if (this.postfix === '$') {
2500 // this.selectiveEraseRectangle(this.params);
2505 // if (this.postfix === '\'') {
2506 // this.requestLocatorPosition(this.params);
2510 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
2512 // if (this.postfix === ' ') {
2513 // this.insertColumns(this.params);
2517 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
2519 // if (this.postfix === ' ') {
2520 // this.deleteColumns(this.params);
2524 this.error('Unknown CSI code: %s.', ch
);
2531 if (ch
=== '\x1b' || ch
=== '\x07') {
2534 switch (this.prefix
) {
2535 // User-Defined Keys (DECUDK).
2538 // Request Status String (DECRQSS).
2539 // test: echo -e '\eP$q"p\e\\'
2541 var pt
= this.currentParam
, valid
= false;
2554 + (this.scrollTop
+ 1)
2556 + (this.scrollBottom
+ 1)
2564 this.error('Unknown DCS Pt: %s.', pt
);
2568 this.send('\x1bP' + +valid
+ '$r' + pt
+ '\x1b\\');
2570 // Set Termcap/Terminfo Data (xterm, experimental).
2573 // Request Termcap/Terminfo String (xterm, experimental)
2574 // Regular xterm does not even respond to this sequence.
2575 // This can cause a small glitch in vim.
2576 // test: echo -ne '\eP+q6b64\e\\'
2578 var pt
= this.currentParam
, valid
= false;
2579 this.send('\x1bP' + +valid
+ '+r' + pt
+ '\x1b\\');
2582 this.error('Unknown DCS prefix: %s.', this.prefix
);
2585 this.currentParam
= 0;
2587 this.state
= normal
;
2589 else if (!this.currentParam
) {
2590 if (!this.prefix
&& ch
!== '$' && ch
!== '+') {
2591 this.currentParam
= ch
;
2593 else if (this.prefix
.length
=== 2) {
2594 this.currentParam
= ch
;
2601 this.currentParam
+= ch
;
2606 if (ch
=== '\x1b' || ch
=== '\x07') {
2609 this.state
= normal
;
2614 this.updateRange(this.y
);
2615 this.refresh(this.refreshStart
, this.refreshEnd
);
2618 * Writes text to the terminal, followed by a break line character (\n).
2619 * @param {string} text The text to write to the terminal.
2621 Terminal
.prototype.writeln = function (data
) {
2622 this.write(data
+ '\r\n');
2625 * Attaches a custom keydown handler which is run before keys are processed, giving consumers of
2626 * xterm.js ultimate control as to what keys should be processed by the terminal and what keys
2628 * @param {function} customKeydownHandler The custom KeyboardEvent handler to attach. This is a
2629 * function that takes a KeyboardEvent, allowing consumers to stop propogation and/or prevent
2630 * the default action. The function returns whether the event should be processed by xterm.js.
2632 Terminal
.prototype.attachCustomKeydownHandler = function (customKeydownHandler
) {
2633 this.customKeydownHandler
= customKeydownHandler
;
2636 * Handle a keydown event
2638 * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
2639 * @param {KeyboardEvent} ev The keydown event to be handled.
2641 Terminal
.prototype.keyDown = function (ev
) {
2642 if (this.customKeydownHandler
&& this.customKeydownHandler(ev
) === false) {
2645 if (!this.compositionHelper
.keydown
.bind(this.compositionHelper
)(ev
)) {
2646 if (this.ybase
!== this.ydisp
) {
2647 this.scrollToBottom();
2652 var result
= this.evaluateKeyEscapeSequence(ev
);
2653 if (result
.scrollDisp
) {
2654 this.scrollDisp(result
.scrollDisp
);
2655 return this.cancel(ev
, true);
2657 if (isThirdLevelShift(this, ev
)) {
2660 if (result
.cancel
) {
2661 // The event is canceled at the end already, is this necessary?
2662 this.cancel(ev
, true);
2667 this.emit('keydown', ev
);
2668 this.emit('key', result
.key
, ev
);
2670 this.handler(result
.key
);
2671 return this.cancel(ev
, true);
2674 * Returns an object that determines how a KeyboardEvent should be handled. The key of the
2675 * returned value is the new key code to pass to the PTY.
2677 * Reference: http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
2678 * @param {KeyboardEvent} ev The keyboard event to be translated to key escape sequence.
2680 Terminal
.prototype.evaluateKeyEscapeSequence = function (ev
) {
2682 // Whether to cancel event propogation (NOTE: this may not be needed since the event is
2683 // canceled at the end of keyDown
2685 // The new key even to emit
2687 // The number of characters to scroll, if this is defined it will cancel the event
2688 scrollDisp
: undefined
2690 var modifiers
= ev
.shiftKey
<< 0 | ev
.altKey
<< 1 | ev
.ctrlKey
<< 2 | ev
.metaKey
<< 3;
2691 switch (ev
.keyCode
) {
2695 result
.key
= '\x08'; // ^H
2698 result
.key
= '\x7f'; // ^?
2703 result
.key
= '\x1b[Z';
2707 result
.cancel
= true;
2712 result
.cancel
= true;
2716 result
.key
= '\x1b';
2717 result
.cancel
= true;
2722 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'D';
2723 // HACK: Make Alt + left-arrow behave like Ctrl + left-arrow: move one word backwards
2724 // http://unix.stackexchange.com/a/108106
2725 if (result
.key
== '\x1b[1;3D') {
2726 result
.key
= '\x1b[1;5D';
2729 else if (this.applicationCursor
) {
2730 result
.key
= '\x1bOD';
2733 result
.key
= '\x1b[D';
2739 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'C';
2740 // HACK: Make Alt + right-arrow behave like Ctrl + right-arrow: move one word forward
2741 // http://unix.stackexchange.com/a/108106
2742 if (result
.key
== '\x1b[1;3C') {
2743 result
.key
= '\x1b[1;5C';
2746 else if (this.applicationCursor
) {
2747 result
.key
= '\x1bOC';
2750 result
.key
= '\x1b[C';
2756 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'A';
2757 // HACK: Make Alt + up-arrow behave like Ctrl + up-arrow
2758 // http://unix.stackexchange.com/a/108106
2759 if (result
.key
== '\x1b[1;3A') {
2760 result
.key
= '\x1b[1;5A';
2763 else if (this.applicationCursor
) {
2764 result
.key
= '\x1bOA';
2767 result
.key
= '\x1b[A';
2773 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'B';
2774 // HACK: Make Alt + down-arrow behave like Ctrl + down-arrow
2775 // http://unix.stackexchange.com/a/108106
2776 if (result
.key
== '\x1b[1;3B') {
2777 result
.key
= '\x1b[1;5B';
2780 else if (this.applicationCursor
) {
2781 result
.key
= '\x1bOB';
2784 result
.key
= '\x1b[B';
2789 if (!ev
.shiftKey
&& !ev
.ctrlKey
) {
2790 // <Ctrl> or <Shift> + <Insert> are used to
2791 // copy-paste on some systems.
2792 result
.key
= '\x1b[2~';
2798 result
.key
= '\x1b[3;' + (modifiers
+ 1) + '~';
2801 result
.key
= '\x1b[3~';
2807 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'H';
2808 else if (this.applicationCursor
)
2809 result
.key
= '\x1bOH';
2811 result
.key
= '\x1b[H';
2816 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'F';
2817 else if (this.applicationCursor
)
2818 result
.key
= '\x1bOF';
2820 result
.key
= '\x1b[F';
2825 result
.scrollDisp
= -(this.rows
- 1);
2828 result
.key
= '\x1b[5~';
2834 result
.scrollDisp
= this.rows
- 1;
2837 result
.key
= '\x1b[6~';
2843 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'P';
2846 result
.key
= '\x1bOP';
2851 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'Q';
2854 result
.key
= '\x1bOQ';
2859 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'R';
2862 result
.key
= '\x1bOR';
2867 result
.key
= '\x1b[1;' + (modifiers
+ 1) + 'S';
2870 result
.key
= '\x1bOS';
2875 result
.key
= '\x1b[15;' + (modifiers
+ 1) + '~';
2878 result
.key
= '\x1b[15~';
2883 result
.key
= '\x1b[17;' + (modifiers
+ 1) + '~';
2886 result
.key
= '\x1b[17~';
2891 result
.key
= '\x1b[18;' + (modifiers
+ 1) + '~';
2894 result
.key
= '\x1b[18~';
2899 result
.key
= '\x1b[19;' + (modifiers
+ 1) + '~';
2902 result
.key
= '\x1b[19~';
2907 result
.key
= '\x1b[20;' + (modifiers
+ 1) + '~';
2910 result
.key
= '\x1b[20~';
2915 result
.key
= '\x1b[21;' + (modifiers
+ 1) + '~';
2918 result
.key
= '\x1b[21~';
2923 result
.key
= '\x1b[23;' + (modifiers
+ 1) + '~';
2926 result
.key
= '\x1b[23~';
2931 result
.key
= '\x1b[24;' + (modifiers
+ 1) + '~';
2934 result
.key
= '\x1b[24~';
2939 if (ev
.ctrlKey
&& !ev
.shiftKey
&& !ev
.altKey
&& !ev
.metaKey
) {
2940 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2941 result
.key
= String
.fromCharCode(ev
.keyCode
- 64);
2943 else if (ev
.keyCode
=== 32) {
2945 result
.key
= String
.fromCharCode(0);
2947 else if (ev
.keyCode
>= 51 && ev
.keyCode
<= 55) {
2948 // escape, file sep, group sep, record sep, unit sep
2949 result
.key
= String
.fromCharCode(ev
.keyCode
- 51 + 27);
2951 else if (ev
.keyCode
=== 56) {
2953 result
.key
= String
.fromCharCode(127);
2955 else if (ev
.keyCode
=== 219) {
2956 // ^[ - Control Sequence Introducer (CSI)
2957 result
.key
= String
.fromCharCode(27);
2959 else if (ev
.keyCode
=== 220) {
2960 // ^\ - String Terminator (ST)
2961 result
.key
= String
.fromCharCode(28);
2963 else if (ev
.keyCode
=== 221) {
2964 // ^] - Operating System Command (OSC)
2965 result
.key
= String
.fromCharCode(29);
2968 else if (!this.browser
.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) {
2969 // On Mac this is a third level shift. Use <Esc> instead.
2970 if (ev
.keyCode
>= 65 && ev
.keyCode
<= 90) {
2971 result
.key
= '\x1b' + String
.fromCharCode(ev
.keyCode
+ 32);
2973 else if (ev
.keyCode
=== 192) {
2974 result
.key
= '\x1b`';
2976 else if (ev
.keyCode
>= 48 && ev
.keyCode
<= 57) {
2977 result
.key
= '\x1b' + (ev
.keyCode
- 48);
2985 * Set the G level of the terminal
2988 Terminal
.prototype.setgLevel = function (g
) {
2990 this.charset
= this.charsets
[g
];
2993 * Set the charset for the given G level of the terminal
2997 Terminal
.prototype.setgCharset = function (g
, charset
) {
2998 this.charsets
[g
] = charset
;
2999 if (this.glevel
=== g
) {
3000 this.charset
= charset
;
3004 * Handle a keypress event.
3006 * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
3007 * @param {KeyboardEvent} ev The keypress event to be handled.
3009 Terminal
.prototype.keyPress = function (ev
) {
3015 else if (ev
.which
== null) {
3018 else if (ev
.which
!== 0 && ev
.charCode
!== 0) {
3024 if (!key
|| ((ev
.altKey
|| ev
.ctrlKey
|| ev
.metaKey
) && !isThirdLevelShift(this, ev
))) {
3027 key
= String
.fromCharCode(key
);
3028 this.emit('keypress', key
, ev
);
3029 this.emit('key', key
, ev
);
3035 * Send data for handling to the terminal
3036 * @param {string} data
3038 Terminal
.prototype.send = function (data
) {
3041 setTimeout(function () {
3042 self
.handler(self
.queue
);
3050 * Note: We could do sweet things with webaudio here
3052 Terminal
.prototype.bell = function () {
3053 if (!this.visualBell
)
3056 this.element
.style
.borderColor
= 'white';
3057 setTimeout(function () {
3058 self
.element
.style
.borderColor
= '';
3064 * Log the current state to the console.
3066 Terminal
.prototype.log = function () {
3069 if (!this.context
.console
|| !this.context
.console
.log
)
3071 var args
= Array
.prototype.slice
.call(arguments
);
3072 this.context
.console
.log
.apply(this.context
.console
, args
);
3075 * Log the current state as error to the console.
3077 Terminal
.prototype.error = function () {
3080 if (!this.context
.console
|| !this.context
.console
.error
)
3082 var args
= Array
.prototype.slice
.call(arguments
);
3083 this.context
.console
.error
.apply(this.context
.console
, args
);
3086 * Resizes the terminal.
3088 * @param {number} x The number of columns to resize to.
3089 * @param {number} y The number of rows to resize to.
3091 Terminal
.prototype.resize = function (x
, y
) {
3092 var line
, el
, i
, j
, ch
, addToY
;
3093 if (x
=== this.cols
&& y
=== this.rows
) {
3103 ch
= [this.defAttr
, ' ', 1]; // does xterm use the default attr?
3104 i
= this.lines
.length
;
3106 while (this.lines
[i
].length
< x
) {
3107 this.lines
[i
].push(ch
);
3112 i
= this.lines
.length
;
3114 while (this.lines
[i
].length
> x
) {
3115 this.lines
[i
].pop();
3127 // y is rows, not this.y
3128 if (this.lines
.length
< y
+ this.ybase
) {
3129 if (this.ybase
> 0 && this.lines
.length
<= this.ybase
+ this.y
+ addToY
+ 1) {
3130 // There is room above the buffer and there are no empty elements below the line,
3134 if (this.ydisp
> 0) {
3135 // Viewport is at the top of the buffer, must increase downwards
3140 // Add a blank line if there is no buffer left at the top to scroll to, or if there
3141 // are blank lines after the cursor
3142 this.lines
.push(this.blankLine());
3145 if (this.children
.length
< y
) {
3152 if (this.lines
.length
> y
+ this.ybase
) {
3153 if (this.lines
.length
> this.ybase
+ this.y
+ 1) {
3154 // The line is a blank line below the cursor, remove it
3158 // The line is the cursor, scroll down
3163 if (this.children
.length
> y
) {
3164 el
= this.children
.shift();
3167 el
.parentNode
.removeChild(el
);
3172 // Make sure that the cursor stays on screen
3183 this.scrollBottom
= y
- 1;
3184 this.refresh(0, this.rows
- 1);
3186 this.geometry
= [this.cols
, this.rows
];
3187 this.emit('resize', { terminal
: this, cols
: x
, rows
: y
});
3190 * Updates the range of rows to refresh
3191 * @param {number} y The number of rows to refresh next.
3193 Terminal
.prototype.updateRange = function (y
) {
3194 if (y
< this.refreshStart
)
3195 this.refreshStart
= y
;
3196 if (y
> this.refreshEnd
)
3197 this.refreshEnd
= y
;
3198 // if (y > this.refreshEnd) {
3199 // this.refreshEnd = y;
3200 // if (y > this.rows - 1) {
3201 // this.refreshEnd = this.rows - 1;
3206 * Set the range of refreshing to the maximum value
3208 Terminal
.prototype.maxRange = function () {
3209 this.refreshStart
= 0;
3210 this.refreshEnd
= this.rows
- 1;
3213 * Setup the tab stops.
3216 Terminal
.prototype.setupStops = function (i
) {
3218 if (!this.tabs
[i
]) {
3219 i
= this.prevStop(i
);
3226 for (; i
< this.cols
; i
+= 8) {
3227 this.tabs
[i
] = true;
3231 * Move the cursor to the previous tab stop from the given position (default is current).
3232 * @param {number} x The position to move the cursor to the previous tab stop.
3234 Terminal
.prototype.prevStop = function (x
) {
3237 while (!this.tabs
[--x
] && x
> 0)
3239 return x
>= this.cols
3244 * Move the cursor one tab stop forward from the given position (default is current).
3245 * @param {number} x The position to move the cursor one tab stop forward.
3247 Terminal
.prototype.nextStop = function (x
) {
3250 while (!this.tabs
[++x
] && x
< this.cols
)
3252 return x
>= this.cols
3257 * Erase in the identified line everything from "x" to the end of the line (right).
3258 * @param {number} x The column from which to start erasing to the end of the line.
3259 * @param {number} y The line in which to operate.
3261 Terminal
.prototype.eraseRight = function (x
, y
) {
3262 var line
= this.lines
[this.ybase
+ y
], ch
= [this.eraseAttr(), ' ', 1]; // xterm
3263 for (; x
< this.cols
; x
++) {
3266 this.updateRange(y
);
3269 * Erase in the identified line everything from "x" to the start of the line (left).
3270 * @param {number} x The column from which to start erasing to the start of the line.
3271 * @param {number} y The line in which to operate.
3273 Terminal
.prototype.eraseLeft = function (x
, y
) {
3274 var line
= this.lines
[this.ybase
+ y
], ch
= [this.eraseAttr(), ' ', 1]; // xterm
3278 this.updateRange(y
);
3281 * Clears the entire buffer, making the prompt line the new first line.
3283 Terminal
.prototype.clear = function () {
3284 if (this.ybase
=== 0 && this.y
=== 0) {
3285 // Don't clear if it's already clear
3288 this.lines
= [this.lines
[this.ybase
+ this.y
]];
3292 for (var i
= 1; i
< this.rows
; i
++) {
3293 this.lines
.push(this.blankLine());
3295 this.refresh(0, this.rows
- 1);
3296 this.emit('scroll', this.ydisp
);
3299 * Erase all content in the given line
3300 * @param {number} y The line to erase all of its contents.
3302 Terminal
.prototype.eraseLine = function (y
) {
3303 this.eraseRight(0, y
);
3306 * Return the data array of a blank line/
3307 * @param {number} cur First bunch of data for each "blank" character.
3309 Terminal
.prototype.blankLine = function (cur
) {
3313 var ch
= [attr
, ' ', 1] // width defaults to 1 halfwidth character
3315 for (; i
< this.cols
; i
++) {
3321 * If cur return the back color xterm feature attribute. Else return defAttr.
3322 * @param {object} cur
3324 Terminal
.prototype.ch = function (cur
) {
3326 ? [this.eraseAttr(), ' ', 1]
3327 : [this.defAttr
, ' ', 1];
3330 * Evaluate if the current erminal is the given argument.
3331 * @param {object} term The terminal to evaluate
3333 Terminal
.prototype.is = function (term
) {
3334 var name
= this.termName
;
3335 return (name
+ '').indexOf(term
) === 0;
3338 * Emit the 'data' event and populate the given data.
3339 * @param {string} data The data to populate in the event.
3341 Terminal
.prototype.handler = function (data
) {
3342 // Input is being sent to the terminal, the terminal should focus the prompt.
3343 if (this.ybase
!== this.ydisp
) {
3344 this.scrollToBottom();
3346 this.emit('data', data
);
3349 * Emit the 'title' event and populate the given title.
3350 * @param {string} title The title to populate in the event.
3352 Terminal
.prototype.handleTitle = function (title
) {
3354 * This event is emitted when the title of the terminal is changed
3355 * from inside the terminal. The parameter is the new title.
3359 this.emit('title', title
);
3365 * ESC D Index (IND is 0x84).
3367 Terminal
.prototype.index = function () {
3369 if (this.y
> this.scrollBottom
) {
3373 this.state
= normal
;
3376 * ESC M Reverse Index (RI is 0x8d).
3378 Terminal
.prototype.reverseIndex = function () {
3381 if (this.y
< this.scrollTop
) {
3383 // possibly move the code below to term.reverseScroll();
3384 // test: echo -ne '\e[1;1H\e[44m\eM\e[0m'
3385 // blankLine(true) is xterm/linux behavior
3386 this.lines
.splice(this.y
+ this.ybase
, 0, this.blankLine(true));
3387 j
= this.rows
- 1 - this.scrollBottom
;
3388 this.lines
.splice(this.rows
- 1 + this.ybase
- j
+ 1, 1);
3390 this.updateRange(this.scrollTop
);
3391 this.updateRange(this.scrollBottom
);
3393 this.state
= normal
;
3396 * ESC c Full Reset (RIS).
3398 Terminal
.prototype.reset = function () {
3399 this.options
.rows
= this.rows
;
3400 this.options
.cols
= this.cols
;
3401 var customKeydownHandler
= this.customKeydownHandler
;
3402 Terminal
.call(this, this.options
);
3403 this.customKeydownHandler
= customKeydownHandler
;
3404 this.refresh(0, this.rows
- 1);
3405 this.viewport
.syncScrollArea();
3408 * ESC H Tab Set (HTS is 0x88).
3410 Terminal
.prototype.tabSet = function () {
3411 this.tabs
[this.x
] = true;
3412 this.state
= normal
;
3419 * Cursor Up Ps Times (default = 1) (CUU).
3421 Terminal
.prototype.cursorUp = function (params
) {
3422 var param
= params
[0];
3431 * Cursor Down Ps Times (default = 1) (CUD).
3433 Terminal
.prototype.cursorDown = function (params
) {
3434 var param
= params
[0];
3438 if (this.y
>= this.rows
) {
3439 this.y
= this.rows
- 1;
3444 * Cursor Forward Ps Times (default = 1) (CUF).
3446 Terminal
.prototype.cursorForward = function (params
) {
3447 var param
= params
[0];
3451 if (this.x
>= this.cols
) {
3452 this.x
= this.cols
- 1;
3457 * Cursor Backward Ps Times (default = 1) (CUB).
3459 Terminal
.prototype.cursorBackward = function (params
) {
3460 var param
= params
[0];
3469 * Cursor Position [row;column] (default = [1,1]) (CUP).
3471 Terminal
.prototype.cursorPos = function (params
) {
3473 row
= params
[0] - 1;
3474 if (params
.length
>= 2) {
3475 col
= params
[1] - 1;
3483 else if (row
>= this.rows
) {
3484 row
= this.rows
- 1;
3489 else if (col
>= this.cols
) {
3490 col
= this.cols
- 1;
3496 * CSI Ps J Erase in Display (ED).
3497 * Ps = 0 -> Erase Below (default).
3498 * Ps = 1 -> Erase Above.
3499 * Ps = 2 -> Erase All.
3500 * Ps = 3 -> Erase Saved Lines (xterm).
3502 * Erase in Display (DECSED).
3503 * Ps = 0 -> Selective Erase Below (default).
3504 * Ps = 1 -> Selective Erase Above.
3505 * Ps = 2 -> Selective Erase All.
3507 Terminal
.prototype.eraseInDisplay = function (params
) {
3509 switch (params
[0]) {
3511 this.eraseRight(this.x
, this.y
);
3513 for (; j
< this.rows
; j
++) {
3518 this.eraseLeft(this.x
, this.y
);
3535 * CSI Ps K Erase in Line (EL).
3536 * Ps = 0 -> Erase to Right (default).
3537 * Ps = 1 -> Erase to Left.
3538 * Ps = 2 -> Erase All.
3540 * Erase in Line (DECSEL).
3541 * Ps = 0 -> Selective Erase to Right (default).
3542 * Ps = 1 -> Selective Erase to Left.
3543 * Ps = 2 -> Selective Erase All.
3545 Terminal
.prototype.eraseInLine = function (params
) {
3546 switch (params
[0]) {
3548 this.eraseRight(this.x
, this.y
);
3551 this.eraseLeft(this.x
, this.y
);
3554 this.eraseLine(this.y
);
3559 * CSI Pm m Character Attributes (SGR).
3560 * Ps = 0 -> Normal (default).
3562 * Ps = 4 -> Underlined.
3563 * Ps = 5 -> Blink (appears as Bold).
3564 * Ps = 7 -> Inverse.
3565 * Ps = 8 -> Invisible, i.e., hidden (VT300).
3566 * Ps = 2 2 -> Normal (neither bold nor faint).
3567 * Ps = 2 4 -> Not underlined.
3568 * Ps = 2 5 -> Steady (not blinking).
3569 * Ps = 2 7 -> Positive (not inverse).
3570 * Ps = 2 8 -> Visible, i.e., not hidden (VT300).
3571 * Ps = 3 0 -> Set foreground color to Black.
3572 * Ps = 3 1 -> Set foreground color to Red.
3573 * Ps = 3 2 -> Set foreground color to Green.
3574 * Ps = 3 3 -> Set foreground color to Yellow.
3575 * Ps = 3 4 -> Set foreground color to Blue.
3576 * Ps = 3 5 -> Set foreground color to Magenta.
3577 * Ps = 3 6 -> Set foreground color to Cyan.
3578 * Ps = 3 7 -> Set foreground color to White.
3579 * Ps = 3 9 -> Set foreground color to default (original).
3580 * Ps = 4 0 -> Set background color to Black.
3581 * Ps = 4 1 -> Set background color to Red.
3582 * Ps = 4 2 -> Set background color to Green.
3583 * Ps = 4 3 -> Set background color to Yellow.
3584 * Ps = 4 4 -> Set background color to Blue.
3585 * Ps = 4 5 -> Set background color to Magenta.
3586 * Ps = 4 6 -> Set background color to Cyan.
3587 * Ps = 4 7 -> Set background color to White.
3588 * Ps = 4 9 -> Set background color to default (original).
3590 * If 16-color support is compiled, the following apply. Assume
3591 * that xterm's resources are set so that the ISO color codes are
3592 * the first 8 of a set of 16. Then the aixterm colors are the
3593 * bright versions of the ISO colors:
3594 * Ps = 9 0 -> Set foreground color to Black.
3595 * Ps = 9 1 -> Set foreground color to Red.
3596 * Ps = 9 2 -> Set foreground color to Green.
3597 * Ps = 9 3 -> Set foreground color to Yellow.
3598 * Ps = 9 4 -> Set foreground color to Blue.
3599 * Ps = 9 5 -> Set foreground color to Magenta.
3600 * Ps = 9 6 -> Set foreground color to Cyan.
3601 * Ps = 9 7 -> Set foreground color to White.
3602 * Ps = 1 0 0 -> Set background color to Black.
3603 * Ps = 1 0 1 -> Set background color to Red.
3604 * Ps = 1 0 2 -> Set background color to Green.
3605 * Ps = 1 0 3 -> Set background color to Yellow.
3606 * Ps = 1 0 4 -> Set background color to Blue.
3607 * Ps = 1 0 5 -> Set background color to Magenta.
3608 * Ps = 1 0 6 -> Set background color to Cyan.
3609 * Ps = 1 0 7 -> Set background color to White.
3611 * If xterm is compiled with the 16-color support disabled, it
3612 * supports the following, from rxvt:
3613 * Ps = 1 0 0 -> Set foreground and background color to
3616 * If 88- or 256-color support is compiled, the following apply.
3617 * Ps = 3 8 ; 5 ; Ps -> Set foreground color to the second
3619 * Ps = 4 8 ; 5 ; Ps -> Set background color to the second
3622 Terminal
.prototype.charAttributes = function (params
) {
3623 // Optimize a single SGR0.
3624 if (params
.length
=== 1 && params
[0] === 0) {
3625 this.curAttr
= this.defAttr
;
3628 var l
= params
.length
, i
= 0, flags
= this.curAttr
>> 18, fg
= (this.curAttr
>> 9) & 0x1ff, bg
= this.curAttr
& 0x1ff, p
;
3629 for (; i
< l
; i
++) {
3631 if (p
>= 30 && p
<= 37) {
3635 else if (p
>= 40 && p
<= 47) {
3639 else if (p
>= 90 && p
<= 97) {
3644 else if (p
>= 100 && p
<= 107) {
3651 flags
= this.defAttr
>> 18;
3652 fg
= (this.defAttr
>> 9) & 0x1ff;
3653 bg
= this.defAttr
& 0x1ff;
3668 // inverse and positive
3669 // test with: echo -e '\e[31m\e[42mhello\e[7mworld\e[27mhi\e[m'
3676 else if (p
=== 22) {
3680 else if (p
=== 24) {
3684 else if (p
=== 25) {
3688 else if (p
=== 27) {
3692 else if (p
=== 28) {
3696 else if (p
=== 39) {
3698 fg
= (this.defAttr
>> 9) & 0x1ff;
3700 else if (p
=== 49) {
3702 bg
= this.defAttr
& 0x1ff;
3704 else if (p
=== 38) {
3706 if (params
[i
+ 1] === 2) {
3708 fg
= matchColor(params
[i
] & 0xff, params
[i
+ 1] & 0xff, params
[i
+ 2] & 0xff);
3713 else if (params
[i
+ 1] === 5) {
3715 p
= params
[i
] & 0xff;
3719 else if (p
=== 48) {
3721 if (params
[i
+ 1] === 2) {
3723 bg
= matchColor(params
[i
] & 0xff, params
[i
+ 1] & 0xff, params
[i
+ 2] & 0xff);
3728 else if (params
[i
+ 1] === 5) {
3730 p
= params
[i
] & 0xff;
3734 else if (p
=== 100) {
3736 fg
= (this.defAttr
>> 9) & 0x1ff;
3737 bg
= this.defAttr
& 0x1ff;
3740 this.error('Unknown SGR attribute: %d.', p
);
3743 this.curAttr
= (flags
<< 18) | (fg
<< 9) | bg
;
3746 * CSI Ps n Device Status Report (DSR).
3747 * Ps = 5 -> Status Report. Result (``OK'') is
3749 * Ps = 6 -> Report Cursor Position (CPR) [row;column].
3753 * Device Status Report (DSR, DEC-specific).
3754 * Ps = 6 -> Report Cursor Position (CPR) [row;column] as CSI
3755 * ? r ; c R (assumes page is zero).
3756 * Ps = 1 5 -> Report Printer status as CSI ? 1 0 n (ready).
3757 * or CSI ? 1 1 n (not ready).
3758 * Ps = 2 5 -> Report UDK status as CSI ? 2 0 n (unlocked)
3759 * or CSI ? 2 1 n (locked).
3760 * Ps = 2 6 -> Report Keyboard status as
3761 * CSI ? 2 7 ; 1 ; 0 ; 0 n (North American).
3762 * The last two parameters apply to VT400 & up, and denote key-
3763 * board ready and LK01 respectively.
3764 * Ps = 5 3 -> Report Locator status as
3765 * CSI ? 5 3 n Locator available, if compiled-in, or
3766 * CSI ? 5 0 n No Locator, if not.
3768 Terminal
.prototype.deviceStatus = function (params
) {
3770 switch (params
[0]) {
3773 this.send('\x1b[0n');
3785 else if (this.prefix
=== '?') {
3786 // modern xterm doesnt seem to
3787 // respond to any of these except ?6, 6, and 5
3788 switch (params
[0]) {
3799 // this.send('\x1b[?11n');
3802 // dont support user defined keys
3803 // this.send('\x1b[?21n');
3806 // north american keyboard
3807 // this.send('\x1b[?27;1;0;0n');
3810 // no dec locator/mouse
3811 // this.send('\x1b[?50n');
3821 * Insert Ps (Blank) Character(s) (default = 1) (ICH).
3823 Terminal
.prototype.insertChars = function (params
) {
3824 var param
, row
, j
, ch
;
3828 row
= this.y
+ this.ybase
;
3830 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3831 while (param
-- && j
< this.cols
) {
3832 this.lines
[row
].splice(j
++, 0, ch
);
3833 this.lines
[row
].pop();
3838 * Cursor Next Line Ps Times (default = 1) (CNL).
3839 * same as CSI Ps B ?
3841 Terminal
.prototype.cursorNextLine = function (params
) {
3842 var param
= params
[0];
3846 if (this.y
>= this.rows
) {
3847 this.y
= this.rows
- 1;
3853 * Cursor Preceding Line Ps Times (default = 1) (CNL).
3856 Terminal
.prototype.cursorPrecedingLine = function (params
) {
3857 var param
= params
[0];
3867 * Cursor Character Absolute [column] (default = [row,1]) (CHA).
3869 Terminal
.prototype.cursorCharAbsolute = function (params
) {
3870 var param
= params
[0];
3877 * Insert Ps Line(s) (default = 1) (IL).
3879 Terminal
.prototype.insertLines = function (params
) {
3884 row
= this.y
+ this.ybase
;
3885 j
= this.rows
- 1 - this.scrollBottom
;
3886 j
= this.rows
- 1 + this.ybase
- j
+ 1;
3888 // test: echo -e '\e[44m\e[1L\e[0m'
3889 // blankLine(true) - xterm/linux behavior
3890 this.lines
.splice(row
, 0, this.blankLine(true));
3891 this.lines
.splice(j
, 1);
3894 this.updateRange(this.y
);
3895 this.updateRange(this.scrollBottom
);
3899 * Delete Ps Line(s) (default = 1) (DL).
3901 Terminal
.prototype.deleteLines = function (params
) {
3906 row
= this.y
+ this.ybase
;
3907 j
= this.rows
- 1 - this.scrollBottom
;
3908 j
= this.rows
- 1 + this.ybase
- j
;
3910 // test: echo -e '\e[44m\e[1M\e[0m'
3911 // blankLine(true) - xterm/linux behavior
3912 this.lines
.splice(j
+ 1, 0, this.blankLine(true));
3913 this.lines
.splice(row
, 1);
3916 this.updateRange(this.y
);
3917 this.updateRange(this.scrollBottom
);
3921 * Delete Ps Character(s) (default = 1) (DCH).
3923 Terminal
.prototype.deleteChars = function (params
) {
3928 row
= this.y
+ this.ybase
;
3929 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3931 this.lines
[row
].splice(this.x
, 1);
3932 this.lines
[row
].push(ch
);
3937 * Erase Ps Character(s) (default = 1) (ECH).
3939 Terminal
.prototype.eraseChars = function (params
) {
3940 var param
, row
, j
, ch
;
3944 row
= this.y
+ this.ybase
;
3946 ch
= [this.eraseAttr(), ' ', 1]; // xterm
3947 while (param
-- && j
< this.cols
) {
3948 this.lines
[row
][j
++] = ch
;
3952 * CSI Pm ` Character Position Absolute
3953 * [column] (default = [row,1]) (HPA).
3955 Terminal
.prototype.charPosAbsolute = function (params
) {
3956 var param
= params
[0];
3960 if (this.x
>= this.cols
) {
3961 this.x
= this.cols
- 1;
3966 * Horizontal Position Relative
3969 Terminal
.prototype.HPositionRelative = function (params
) {
3970 var param
= params
[0];
3974 if (this.x
>= this.cols
) {
3975 this.x
= this.cols
- 1;
3979 * CSI Ps c Send Device Attributes (Primary DA).
3980 * Ps = 0 or omitted -> request attributes from terminal. The
3981 * response depends on the decTerminalID resource setting.
3982 * -> CSI ? 1 ; 2 c (``VT100 with Advanced Video Option'')
3983 * -> CSI ? 1 ; 0 c (``VT101 with No Options'')
3984 * -> CSI ? 6 c (``VT102'')
3985 * -> CSI ? 6 0 ; 1 ; 2 ; 6 ; 8 ; 9 ; 1 5 ; c (``VT220'')
3986 * The VT100-style response parameters do not mean anything by
3987 * themselves. VT220 parameters do, telling the host what fea-
3988 * tures the terminal supports:
3989 * Ps = 1 -> 132-columns.
3990 * Ps = 2 -> Printer.
3991 * Ps = 6 -> Selective erase.
3992 * Ps = 8 -> User-defined keys.
3993 * Ps = 9 -> National replacement character sets.
3994 * Ps = 1 5 -> Technical characters.
3995 * Ps = 2 2 -> ANSI color, e.g., VT525.
3996 * Ps = 2 9 -> ANSI text locator (i.e., DEC Locator mode).
3998 * Send Device Attributes (Secondary DA).
3999 * Ps = 0 or omitted -> request the terminal's identification
4000 * code. The response depends on the decTerminalID resource set-
4001 * ting. It should apply only to VT220 and up, but xterm extends
4003 * -> CSI > Pp ; Pv ; Pc c
4004 * where Pp denotes the terminal type
4005 * Pp = 0 -> ``VT100''.
4006 * Pp = 1 -> ``VT220''.
4007 * and Pv is the firmware version (for xterm, this was originally
4008 * the XFree86 patch number, starting with 95). In a DEC termi-
4009 * nal, Pc indicates the ROM cartridge registration number and is
4012 * xterm/charproc.c - line 2012, for more information.
4013 * vim responds with ^[[?0c or ^[[?1c after the terminal's response (?)
4015 Terminal
.prototype.sendDeviceAttributes = function (params
) {
4019 if (this.is('xterm')
4020 || this.is('rxvt-unicode')
4021 || this.is('screen')) {
4022 this.send('\x1b[?1;2c');
4024 else if (this.is('linux')) {
4025 this.send('\x1b[?6c');
4028 else if (this.prefix
=== '>') {
4030 // seem to spit this
4031 // out around ~370 times (?).
4032 if (this.is('xterm')) {
4033 this.send('\x1b[>0;276;0c');
4035 else if (this.is('rxvt-unicode')) {
4036 this.send('\x1b[>85;95;0c');
4038 else if (this.is('linux')) {
4039 // not supported by linux console.
4040 // linux console echoes parameters.
4041 this.send(params
[0] + 'c');
4043 else if (this.is('screen')) {
4044 this.send('\x1b[>83;40003;0c');
4050 * Line Position Absolute [row] (default = [1,column]) (VPA).
4052 Terminal
.prototype.linePosAbsolute = function (params
) {
4053 var param
= params
[0];
4057 if (this.y
>= this.rows
) {
4058 this.y
= this.rows
- 1;
4062 * 145 65 e * VPR - Vertical Position Relative
4065 Terminal
.prototype.VPositionRelative = function (params
) {
4066 var param
= params
[0];
4070 if (this.y
>= this.rows
) {
4071 this.y
= this.rows
- 1;
4076 * Horizontal and Vertical Position [row;column] (default =
4079 Terminal
.prototype.HVPosition = function (params
) {
4084 this.y
= params
[0] - 1;
4085 if (this.y
>= this.rows
) {
4086 this.y
= this.rows
- 1;
4088 this.x
= params
[1] - 1;
4089 if (this.x
>= this.cols
) {
4090 this.x
= this.cols
- 1;
4094 * CSI Pm h Set Mode (SM).
4095 * Ps = 2 -> Keyboard Action Mode (AM).
4096 * Ps = 4 -> Insert Mode (IRM).
4097 * Ps = 1 2 -> Send/receive (SRM).
4098 * Ps = 2 0 -> Automatic Newline (LNM).
4100 * DEC Private Mode Set (DECSET).
4101 * Ps = 1 -> Application Cursor Keys (DECCKM).
4102 * Ps = 2 -> Designate USASCII for character sets G0-G3
4103 * (DECANM), and set VT100 mode.
4104 * Ps = 3 -> 132 Column Mode (DECCOLM).
4105 * Ps = 4 -> Smooth (Slow) Scroll (DECSCLM).
4106 * Ps = 5 -> Reverse Video (DECSCNM).
4107 * Ps = 6 -> Origin Mode (DECOM).
4108 * Ps = 7 -> Wraparound Mode (DECAWM).
4109 * Ps = 8 -> Auto-repeat Keys (DECARM).
4110 * Ps = 9 -> Send Mouse X & Y on button press. See the sec-
4111 * tion Mouse Tracking.
4112 * Ps = 1 0 -> Show toolbar (rxvt).
4113 * Ps = 1 2 -> Start Blinking Cursor (att610).
4114 * Ps = 1 8 -> Print form feed (DECPFF).
4115 * Ps = 1 9 -> Set print extent to full screen (DECPEX).
4116 * Ps = 2 5 -> Show Cursor (DECTCEM).
4117 * Ps = 3 0 -> Show scrollbar (rxvt).
4118 * Ps = 3 5 -> Enable font-shifting functions (rxvt).
4119 * Ps = 3 8 -> Enter Tektronix Mode (DECTEK).
4120 * Ps = 4 0 -> Allow 80 -> 132 Mode.
4121 * Ps = 4 1 -> more(1) fix (see curses resource).
4122 * Ps = 4 2 -> Enable Nation Replacement Character sets (DECN-
4124 * Ps = 4 4 -> Turn On Margin Bell.
4125 * Ps = 4 5 -> Reverse-wraparound Mode.
4126 * Ps = 4 6 -> Start Logging. This is normally disabled by a
4127 * compile-time option.
4128 * Ps = 4 7 -> Use Alternate Screen Buffer. (This may be dis-
4129 * abled by the titeInhibit resource).
4130 * Ps = 6 6 -> Application keypad (DECNKM).
4131 * Ps = 6 7 -> Backarrow key sends backspace (DECBKM).
4132 * Ps = 1 0 0 0 -> Send Mouse X & Y on button press and
4133 * release. See the section Mouse Tracking.
4134 * Ps = 1 0 0 1 -> Use Hilite Mouse Tracking.
4135 * Ps = 1 0 0 2 -> Use Cell Motion Mouse Tracking.
4136 * Ps = 1 0 0 3 -> Use All Motion Mouse Tracking.
4137 * Ps = 1 0 0 4 -> Send FocusIn/FocusOut events.
4138 * Ps = 1 0 0 5 -> Enable Extended Mouse Mode.
4139 * Ps = 1 0 1 0 -> Scroll to bottom on tty output (rxvt).
4140 * Ps = 1 0 1 1 -> Scroll to bottom on key press (rxvt).
4141 * Ps = 1 0 3 4 -> Interpret "meta" key, sets eighth bit.
4142 * (enables the eightBitInput resource).
4143 * Ps = 1 0 3 5 -> Enable special modifiers for Alt and Num-
4144 * Lock keys. (This enables the numLock resource).
4145 * Ps = 1 0 3 6 -> Send ESC when Meta modifies a key. (This
4146 * enables the metaSendsEscape resource).
4147 * Ps = 1 0 3 7 -> Send DEL from the editing-keypad Delete
4149 * Ps = 1 0 3 9 -> Send ESC when Alt modifies a key. (This
4150 * enables the altSendsEscape resource).
4151 * Ps = 1 0 4 0 -> Keep selection even if not highlighted.
4152 * (This enables the keepSelection resource).
4153 * Ps = 1 0 4 1 -> Use the CLIPBOARD selection. (This enables
4154 * the selectToClipboard resource).
4155 * Ps = 1 0 4 2 -> Enable Urgency window manager hint when
4156 * Control-G is received. (This enables the bellIsUrgent
4158 * Ps = 1 0 4 3 -> Enable raising of the window when Control-G
4159 * is received. (enables the popOnBell resource).
4160 * Ps = 1 0 4 7 -> Use Alternate Screen Buffer. (This may be
4161 * disabled by the titeInhibit resource).
4162 * Ps = 1 0 4 8 -> Save cursor as in DECSC. (This may be dis-
4163 * abled by the titeInhibit resource).
4164 * Ps = 1 0 4 9 -> Save cursor as in DECSC and use Alternate
4165 * Screen Buffer, clearing it first. (This may be disabled by
4166 * the titeInhibit resource). This combines the effects of the 1
4167 * 0 4 7 and 1 0 4 8 modes. Use this with terminfo-based
4168 * applications rather than the 4 7 mode.
4169 * Ps = 1 0 5 0 -> Set terminfo/termcap function-key mode.
4170 * Ps = 1 0 5 1 -> Set Sun function-key mode.
4171 * Ps = 1 0 5 2 -> Set HP function-key mode.
4172 * Ps = 1 0 5 3 -> Set SCO function-key mode.
4173 * Ps = 1 0 6 0 -> Set legacy keyboard emulation (X11R6).
4174 * Ps = 1 0 6 1 -> Set VT220 keyboard emulation.
4175 * Ps = 2 0 0 4 -> Set bracketed paste mode.
4177 * http: *vt100.net/docs/vt220-rm/chapter4.html
4179 Terminal
.prototype.setMode = function (params
) {
4180 if (typeof params
=== 'object') {
4181 var l
= params
.length
, i
= 0;
4182 for (; i
< l
; i
++) {
4183 this.setMode(params
[i
]);
4190 this.insertMode
= true;
4193 //this.convertEol = true;
4197 else if (this.prefix
=== '?') {
4200 this.applicationCursor
= true;
4203 this.setgCharset(0, Terminal
.charsets
.US
);
4204 this.setgCharset(1, Terminal
.charsets
.US
);
4205 this.setgCharset(2, Terminal
.charsets
.US
);
4206 this.setgCharset(3, Terminal
.charsets
.US
);
4207 // set VT100 mode here
4210 this.savedCols
= this.cols
;
4211 this.resize(132, this.rows
);
4214 this.originMode
= true;
4217 this.wraparoundMode
= true;
4220 // this.cursorBlink = true;
4223 this.log('Serial port requested application keypad.');
4224 this.applicationKeypad
= true;
4225 this.viewport
.syncScrollArea();
4227 case 9: // X10 Mouse
4228 // no release, no motion, no wheel, no modifiers.
4229 case 1000: // vt200 mouse
4231 // no modifiers, except control on the wheel.
4232 case 1002: // button event mouse
4234 // any event - sends motion events,
4235 // even if there is no button held down.
4236 this.x10Mouse
= params
=== 9;
4237 this.vt200Mouse
= params
=== 1000;
4238 this.normalMouse
= params
> 1000;
4239 this.mouseEvents
= true;
4240 this.element
.style
.cursor
= 'default';
4241 this.log('Binding to mouse events.');
4246 this.sendFocus
= true;
4249 this.utfMouse
= true;
4250 // for wide terminals
4251 // simply encodes large values as utf8 characters
4254 this.sgrMouse
= true;
4255 // for wide terminals
4256 // does not add 32 to fields
4257 // press: ^[[<b;x;yM
4258 // release: ^[[<b;x;ym
4261 this.urxvtMouse
= true;
4262 // for wide terminals
4263 // numbers for fields
4265 // motion: ^[[b;x;yT
4268 this.cursorHidden
= false;
4271 //this.saveCursor();
4273 case 47: // alt screen buffer
4282 scrollTop
: this.scrollTop
,
4283 scrollBottom
: this.scrollBottom
,
4287 this.normal
= normal
;
4295 * CSI Pm l Reset Mode (RM).
4296 * Ps = 2 -> Keyboard Action Mode (AM).
4297 * Ps = 4 -> Replace Mode (IRM).
4298 * Ps = 1 2 -> Send/receive (SRM).
4299 * Ps = 2 0 -> Normal Linefeed (LNM).
4301 * DEC Private Mode Reset (DECRST).
4302 * Ps = 1 -> Normal Cursor Keys (DECCKM).
4303 * Ps = 2 -> Designate VT52 mode (DECANM).
4304 * Ps = 3 -> 80 Column Mode (DECCOLM).
4305 * Ps = 4 -> Jump (Fast) Scroll (DECSCLM).
4306 * Ps = 5 -> Normal Video (DECSCNM).
4307 * Ps = 6 -> Normal Cursor Mode (DECOM).
4308 * Ps = 7 -> No Wraparound Mode (DECAWM).
4309 * Ps = 8 -> No Auto-repeat Keys (DECARM).
4310 * Ps = 9 -> Don't send Mouse X & Y on button press.
4311 * Ps = 1 0 -> Hide toolbar (rxvt).
4312 * Ps = 1 2 -> Stop Blinking Cursor (att610).
4313 * Ps = 1 8 -> Don't print form feed (DECPFF).
4314 * Ps = 1 9 -> Limit print to scrolling region (DECPEX).
4315 * Ps = 2 5 -> Hide Cursor (DECTCEM).
4316 * Ps = 3 0 -> Don't show scrollbar (rxvt).
4317 * Ps = 3 5 -> Disable font-shifting functions (rxvt).
4318 * Ps = 4 0 -> Disallow 80 -> 132 Mode.
4319 * Ps = 4 1 -> No more(1) fix (see curses resource).
4320 * Ps = 4 2 -> Disable Nation Replacement Character sets (DEC-
4322 * Ps = 4 4 -> Turn Off Margin Bell.
4323 * Ps = 4 5 -> No Reverse-wraparound Mode.
4324 * Ps = 4 6 -> Stop Logging. (This is normally disabled by a
4325 * compile-time option).
4326 * Ps = 4 7 -> Use Normal Screen Buffer.
4327 * Ps = 6 6 -> Numeric keypad (DECNKM).
4328 * Ps = 6 7 -> Backarrow key sends delete (DECBKM).
4329 * Ps = 1 0 0 0 -> Don't send Mouse X & Y on button press and
4330 * release. See the section Mouse Tracking.
4331 * Ps = 1 0 0 1 -> Don't use Hilite Mouse Tracking.
4332 * Ps = 1 0 0 2 -> Don't use Cell Motion Mouse Tracking.
4333 * Ps = 1 0 0 3 -> Don't use All Motion Mouse Tracking.
4334 * Ps = 1 0 0 4 -> Don't send FocusIn/FocusOut events.
4335 * Ps = 1 0 0 5 -> Disable Extended Mouse Mode.
4336 * Ps = 1 0 1 0 -> Don't scroll to bottom on tty output
4338 * Ps = 1 0 1 1 -> Don't scroll to bottom on key press (rxvt).
4339 * Ps = 1 0 3 4 -> Don't interpret "meta" key. (This disables
4340 * the eightBitInput resource).
4341 * Ps = 1 0 3 5 -> Disable special modifiers for Alt and Num-
4342 * Lock keys. (This disables the numLock resource).
4343 * Ps = 1 0 3 6 -> Don't send ESC when Meta modifies a key.
4344 * (This disables the metaSendsEscape resource).
4345 * Ps = 1 0 3 7 -> Send VT220 Remove from the editing-keypad
4347 * Ps = 1 0 3 9 -> Don't send ESC when Alt modifies a key.
4348 * (This disables the altSendsEscape resource).
4349 * Ps = 1 0 4 0 -> Do not keep selection when not highlighted.
4350 * (This disables the keepSelection resource).
4351 * Ps = 1 0 4 1 -> Use the PRIMARY selection. (This disables
4352 * the selectToClipboard resource).
4353 * Ps = 1 0 4 2 -> Disable Urgency window manager hint when
4354 * Control-G is received. (This disables the bellIsUrgent
4356 * Ps = 1 0 4 3 -> Disable raising of the window when Control-
4357 * G is received. (This disables the popOnBell resource).
4358 * Ps = 1 0 4 7 -> Use Normal Screen Buffer, clearing screen
4359 * first if in the Alternate Screen. (This may be disabled by
4360 * the titeInhibit resource).
4361 * Ps = 1 0 4 8 -> Restore cursor as in DECRC. (This may be
4362 * disabled by the titeInhibit resource).
4363 * Ps = 1 0 4 9 -> Use Normal Screen Buffer and restore cursor
4364 * as in DECRC. (This may be disabled by the titeInhibit
4365 * resource). This combines the effects of the 1 0 4 7 and 1 0
4366 * 4 8 modes. Use this with terminfo-based applications rather
4367 * than the 4 7 mode.
4368 * Ps = 1 0 5 0 -> Reset terminfo/termcap function-key mode.
4369 * Ps = 1 0 5 1 -> Reset Sun function-key mode.
4370 * Ps = 1 0 5 2 -> Reset HP function-key mode.
4371 * Ps = 1 0 5 3 -> Reset SCO function-key mode.
4372 * Ps = 1 0 6 0 -> Reset legacy keyboard emulation (X11R6).
4373 * Ps = 1 0 6 1 -> Reset keyboard emulation to Sun/PC style.
4374 * Ps = 2 0 0 4 -> Reset bracketed paste mode.
4376 Terminal
.prototype.resetMode = function (params
) {
4377 if (typeof params
=== 'object') {
4378 var l
= params
.length
, i
= 0;
4379 for (; i
< l
; i
++) {
4380 this.resetMode(params
[i
]);
4387 this.insertMode
= false;
4390 //this.convertEol = false;
4394 else if (this.prefix
=== '?') {
4397 this.applicationCursor
= false;
4400 if (this.cols
=== 132 && this.savedCols
) {
4401 this.resize(this.savedCols
, this.rows
);
4403 delete this.savedCols
;
4406 this.originMode
= false;
4409 this.wraparoundMode
= false;
4412 // this.cursorBlink = false;
4415 this.log('Switching back to normal keypad.');
4416 this.applicationKeypad
= false;
4417 this.viewport
.syncScrollArea();
4419 case 9: // X10 Mouse
4420 case 1000: // vt200 mouse
4421 case 1002: // button event mouse
4423 this.x10Mouse
= false;
4424 this.vt200Mouse
= false;
4425 this.normalMouse
= false;
4426 this.mouseEvents
= false;
4427 this.element
.style
.cursor
= '';
4430 this.sendFocus
= false;
4433 this.utfMouse
= false;
4436 this.sgrMouse
= false;
4439 this.urxvtMouse
= false;
4442 this.cursorHidden
= true;
4446 case 47: // normal screen buffer
4449 this.lines
= this.normal
.lines
;
4450 this.ybase
= this.normal
.ybase
;
4451 this.ydisp
= this.normal
.ydisp
;
4452 this.x
= this.normal
.x
;
4453 this.y
= this.normal
.y
;
4454 this.scrollTop
= this.normal
.scrollTop
;
4455 this.scrollBottom
= this.normal
.scrollBottom
;
4456 this.tabs
= this.normal
.tabs
;
4458 // if (params === 1049) {
4459 // this.x = this.savedX;
4460 // this.y = this.savedY;
4462 this.refresh(0, this.rows
- 1);
4471 * Set Scrolling Region [top;bottom] (default = full size of win-
4475 Terminal
.prototype.setScrollRegion = function (params
) {
4478 this.scrollTop
= (params
[0] || 1) - 1;
4479 this.scrollBottom
= (params
[1] || this.rows
) - 1;
4485 * Save cursor (ANSI.SYS).
4487 Terminal
.prototype.saveCursor = function (params
) {
4488 this.savedX
= this.x
;
4489 this.savedY
= this.y
;
4493 * Restore cursor (ANSI.SYS).
4495 Terminal
.prototype.restoreCursor = function (params
) {
4496 this.x
= this.savedX
|| 0;
4497 this.y
= this.savedY
|| 0;
4504 * Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
4506 Terminal
.prototype.cursorForwardTab = function (params
) {
4507 var param
= params
[0] || 1;
4509 this.x
= this.nextStop();
4513 * CSI Ps S Scroll up Ps lines (default = 1) (SU).
4515 Terminal
.prototype.scrollUp = function (params
) {
4516 var param
= params
[0] || 1;
4518 this.lines
.splice(this.ybase
+ this.scrollTop
, 1);
4519 this.lines
.splice(this.ybase
+ this.scrollBottom
, 0, this.blankLine());
4522 this.updateRange(this.scrollTop
);
4523 this.updateRange(this.scrollBottom
);
4526 * CSI Ps T Scroll down Ps lines (default = 1) (SD).
4528 Terminal
.prototype.scrollDown = function (params
) {
4529 var param
= params
[0] || 1;
4531 this.lines
.splice(this.ybase
+ this.scrollBottom
, 1);
4532 this.lines
.splice(this.ybase
+ this.scrollTop
, 0, this.blankLine());
4535 this.updateRange(this.scrollTop
);
4536 this.updateRange(this.scrollBottom
);
4539 * CSI Ps ; Ps ; Ps ; Ps ; Ps T
4540 * Initiate highlight mouse tracking. Parameters are
4541 * [func;startx;starty;firstrow;lastrow]. See the section Mouse
4544 Terminal
.prototype.initMouseTracking = function (params
) {
4545 // Relevant: DECSET 1001
4549 * Reset one or more features of the title modes to the default
4550 * value. Normally, "reset" disables the feature. It is possi-
4551 * ble to disable the ability to reset features by compiling a
4552 * different default for the title modes into xterm.
4553 * Ps = 0 -> Do not set window/icon labels using hexadecimal.
4554 * Ps = 1 -> Do not query window/icon labels using hexadeci-
4556 * Ps = 2 -> Do not set window/icon labels using UTF-8.
4557 * Ps = 3 -> Do not query window/icon labels using UTF-8.
4558 * (See discussion of "Title Modes").
4560 Terminal
.prototype.resetTitleModes = function (params
) {
4564 * CSI Ps Z Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
4566 Terminal
.prototype.cursorBackwardTab = function (params
) {
4567 var param
= params
[0] || 1;
4569 this.x
= this.prevStop();
4573 * CSI Ps b Repeat the preceding graphic character Ps times (REP).
4575 Terminal
.prototype.repeatPrecedingCharacter = function (params
) {
4576 var param
= params
[0] || 1, line
= this.lines
[this.ybase
+ this.y
], ch
= line
[this.x
- 1] || [this.defAttr
, ' ', 1];
4578 line
[this.x
++] = ch
;
4581 * CSI Ps g Tab Clear (TBC).
4582 * Ps = 0 -> Clear Current Column (default).
4583 * Ps = 3 -> Clear All.
4585 * Ps = 2 -> Clear Stops on Line.
4586 * http://vt100.net/annarbor/aaa-ug/section6.html
4588 Terminal
.prototype.tabClear = function (params
) {
4589 var param
= params
[0];
4591 delete this.tabs
[this.x
];
4593 else if (param
=== 3) {
4598 * CSI Pm i Media Copy (MC).
4599 * Ps = 0 -> Print screen (default).
4600 * Ps = 4 -> Turn off printer controller mode.
4601 * Ps = 5 -> Turn on printer controller mode.
4603 * Media Copy (MC, DEC-specific).
4604 * Ps = 1 -> Print line containing cursor.
4605 * Ps = 4 -> Turn off autoprint mode.
4606 * Ps = 5 -> Turn on autoprint mode.
4607 * Ps = 1 0 -> Print composed display, ignores DECPEX.
4608 * Ps = 1 1 -> Print all pages.
4610 Terminal
.prototype.mediaCopy = function (params
) {
4615 * Set or reset resource-values used by xterm to decide whether
4616 * to construct escape sequences holding information about the
4617 * modifiers pressed with a given key. The first parameter iden-
4618 * tifies the resource to set/reset. The second parameter is the
4619 * value to assign to the resource. If the second parameter is
4620 * omitted, the resource is reset to its initial value.
4621 * Ps = 1 -> modifyCursorKeys.
4622 * Ps = 2 -> modifyFunctionKeys.
4623 * Ps = 4 -> modifyOtherKeys.
4624 * If no parameters are given, all resources are reset to their
4627 Terminal
.prototype.setResources = function (params
) {
4632 * Disable modifiers which may be enabled via the CSI > Ps; Ps m
4633 * sequence. This corresponds to a resource value of "-1", which
4634 * cannot be set with the other sequence. The parameter identi-
4635 * fies the resource to be disabled:
4636 * Ps = 1 -> modifyCursorKeys.
4637 * Ps = 2 -> modifyFunctionKeys.
4638 * Ps = 4 -> modifyOtherKeys.
4639 * If the parameter is omitted, modifyFunctionKeys is disabled.
4640 * When modifyFunctionKeys is disabled, xterm uses the modifier
4641 * keys to make an extended sequence of functions rather than
4642 * adding a parameter to each function key to denote the modi-
4645 Terminal
.prototype.disableModifiers = function (params
) {
4650 * Set resource value pointerMode. This is used by xterm to
4651 * decide whether to hide the pointer cursor as the user types.
4652 * Valid values for the parameter:
4653 * Ps = 0 -> never hide the pointer.
4654 * Ps = 1 -> hide if the mouse tracking mode is not enabled.
4655 * Ps = 2 -> always hide the pointer. If no parameter is
4656 * given, xterm uses the default, which is 1 .
4658 Terminal
.prototype.setPointerMode = function (params
) {
4662 * CSI ! p Soft terminal reset (DECSTR).
4663 * http://vt100.net/docs/vt220-rm/table4-10.html
4665 Terminal
.prototype.softReset = function (params
) {
4666 this.cursorHidden
= false;
4667 this.insertMode
= false;
4668 this.originMode
= false;
4669 this.wraparoundMode
= false; // autowrap
4670 this.applicationKeypad
= false; // ?
4671 this.viewport
.syncScrollArea();
4672 this.applicationCursor
= false;
4674 this.scrollBottom
= this.rows
- 1;
4675 this.curAttr
= this.defAttr
;
4676 this.x
= this.y
= 0; // ?
4677 this.charset
= null;
4678 this.glevel
= 0; // ??
4679 this.charsets
= [null]; // ??
4683 * Request ANSI mode (DECRQM). For VT300 and up, reply is
4685 * where Ps is the mode number as in RM, and Pm is the mode
4687 * 0 - not recognized
4690 * 3 - permanently set
4691 * 4 - permanently reset
4693 Terminal
.prototype.requestAnsiMode = function (params
) {
4698 * Request DEC private mode (DECRQM). For VT300 and up, reply is
4700 * where Ps is the mode number as in DECSET, Pm is the mode value
4701 * as in the ANSI DECRQM.
4703 Terminal
.prototype.requestPrivateMode = function (params
) {
4708 * Set conformance level (DECSCL). Valid values for the first
4710 * Ps = 6 1 -> VT100.
4711 * Ps = 6 2 -> VT200.
4712 * Ps = 6 3 -> VT300.
4713 * Valid values for the second parameter:
4714 * Ps = 0 -> 8-bit controls.
4715 * Ps = 1 -> 7-bit controls (always set for VT100).
4716 * Ps = 2 -> 8-bit controls.
4718 Terminal
.prototype.setConformanceLevel = function (params
) {
4722 * CSI Ps q Load LEDs (DECLL).
4723 * Ps = 0 -> Clear all LEDS (default).
4724 * Ps = 1 -> Light Num Lock.
4725 * Ps = 2 -> Light Caps Lock.
4726 * Ps = 3 -> Light Scroll Lock.
4727 * Ps = 2 1 -> Extinguish Num Lock.
4728 * Ps = 2 2 -> Extinguish Caps Lock.
4729 * Ps = 2 3 -> Extinguish Scroll Lock.
4731 Terminal
.prototype.loadLEDs = function (params
) {
4736 * Set cursor style (DECSCUSR, VT520).
4737 * Ps = 0 -> blinking block.
4738 * Ps = 1 -> blinking block (default).
4739 * Ps = 2 -> steady block.
4740 * Ps = 3 -> blinking underline.
4741 * Ps = 4 -> steady underline.
4743 Terminal
.prototype.setCursorStyle = function (params
) {
4748 * Select character protection attribute (DECSCA). Valid values
4749 * for the parameter:
4750 * Ps = 0 -> DECSED and DECSEL can erase (default).
4751 * Ps = 1 -> DECSED and DECSEL cannot erase.
4752 * Ps = 2 -> DECSED and DECSEL can erase.
4754 Terminal
.prototype.setCharProtectionAttr = function (params
) {
4759 * Restore DEC Private Mode Values. The value of Ps previously
4760 * saved is restored. Ps values are the same as for DECSET.
4762 Terminal
.prototype.restorePrivateValues = function (params
) {
4766 * CSI Pt; Pl; Pb; Pr; Ps$ r
4767 * Change Attributes in Rectangular Area (DECCARA), VT400 and up.
4768 * Pt; Pl; Pb; Pr denotes the rectangle.
4769 * Ps denotes the SGR attributes to change: 0, 1, 4, 5, 7.
4770 * NOTE: xterm doesn't enable this code by default.
4772 Terminal
.prototype.setAttrInRectangle = function (params
) {
4773 var t
= params
[0], l
= params
[1], b
= params
[2], r
= params
[3], attr
= params
[4];
4775 for (; t
< b
+ 1; t
++) {
4776 line
= this.lines
[this.ybase
+ t
];
4777 for (i
= l
; i
< r
; i
++) {
4778 line
[i
] = [attr
, line
[i
][1]];
4782 this.updateRange(params
[0]);
4783 this.updateRange(params
[2]);
4786 * CSI Pc; Pt; Pl; Pb; Pr$ x
4787 * Fill Rectangular Area (DECFRA), VT420 and up.
4788 * Pc is the character to use.
4789 * Pt; Pl; Pb; Pr denotes the rectangle.
4790 * NOTE: xterm doesn't enable this code by default.
4792 Terminal
.prototype.fillRectangle = function (params
) {
4793 var ch
= params
[0], t
= params
[1], l
= params
[2], b
= params
[3], r
= params
[4];
4795 for (; t
< b
+ 1; t
++) {
4796 line
= this.lines
[this.ybase
+ t
];
4797 for (i
= l
; i
< r
; i
++) {
4798 line
[i
] = [line
[i
][0], String
.fromCharCode(ch
)];
4802 this.updateRange(params
[1]);
4803 this.updateRange(params
[3]);
4807 * Enable Locator Reporting (DECELR).
4808 * Valid values for the first parameter:
4809 * Ps = 0 -> Locator disabled (default).
4810 * Ps = 1 -> Locator enabled.
4811 * Ps = 2 -> Locator enabled for one report, then disabled.
4812 * The second parameter specifies the coordinate unit for locator
4814 * Valid values for the second parameter:
4815 * Pu = 0 <- or omitted -> default to character cells.
4816 * Pu = 1 <- device physical pixels.
4817 * Pu = 2 <- character cells.
4819 Terminal
.prototype.enableLocatorReporting = function (params
) {
4820 var val
= params
[0] > 0;
4821 //this.mouseEvents = val;
4822 //this.decLocator = val;
4825 * CSI Pt; Pl; Pb; Pr$ z
4826 * Erase Rectangular Area (DECERA), VT400 and up.
4827 * Pt; Pl; Pb; Pr denotes the rectangle.
4828 * NOTE: xterm doesn't enable this code by default.
4830 Terminal
.prototype.eraseRectangle = function (params
) {
4831 var t
= params
[0], l
= params
[1], b
= params
[2], r
= params
[3];
4833 ch
= [this.eraseAttr(), ' ', 1]; // xterm?
4834 for (; t
< b
+ 1; t
++) {
4835 line
= this.lines
[this.ybase
+ t
];
4836 for (i
= l
; i
< r
; i
++) {
4841 this.updateRange(params
[0]);
4842 this.updateRange(params
[2]);
4846 * Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
4847 * NOTE: xterm doesn't enable this code by default.
4849 Terminal
.prototype.insertColumns = function () {
4850 var param
= params
[0], l
= this.ybase
+ this.rows
, ch
= [this.eraseAttr(), ' ', 1] // xterm?
4853 for (i
= this.ybase
; i
< l
; i
++) {
4854 this.lines
[i
].splice(this.x
+ 1, 0, ch
);
4855 this.lines
[i
].pop();
4862 * Delete P s Column(s) (default = 1) (DECDC), VT420 and up
4863 * NOTE: xterm doesn't enable this code by default.
4865 Terminal
.prototype.deleteColumns = function () {
4866 var param
= params
[0], l
= this.ybase
+ this.rows
, ch
= [this.eraseAttr(), ' ', 1] // xterm?
4869 for (i
= this.ybase
; i
< l
; i
++) {
4870 this.lines
[i
].splice(this.x
, 1);
4871 this.lines
[i
].push(ch
);
4879 Terminal
.charsets
= {};
4880 // DEC Special Character and Line Drawing Set.
4881 // http://vt100.net/docs/vt102-ug/table5-13.html
4882 // A lot of curses apps use this if they see TERM=xterm.
4883 // testing: echo -e '\e(0a\e(B'
4884 // The xterm output sometimes seems to conflict with the
4885 // reference above. xterm seems in line with the reference
4886 // when running vttest however.
4887 // The table below now uses xterm's output from vttest.
4888 Terminal
.charsets
.SCLD
= {
4919 '~': '\u00b7' // '·'
4921 Terminal
.charsets
.UK
= null; // (A
4922 Terminal
.charsets
.US
= null; // (B (USASCII)
4923 Terminal
.charsets
.Dutch
= null; // (4
4924 Terminal
.charsets
.Finnish
= null; // (C or (5
4925 Terminal
.charsets
.French
= null; // (R
4926 Terminal
.charsets
.FrenchCanadian
= null; // (Q
4927 Terminal
.charsets
.German
= null; // (K
4928 Terminal
.charsets
.Italian
= null; // (Y
4929 Terminal
.charsets
.NorwegianDanish
= null; // (E or (6
4930 Terminal
.charsets
.Spanish
= null; // (Z
4931 Terminal
.charsets
.Swedish
= null; // (H or (7
4932 Terminal
.charsets
.Swiss
= null; // (=
4933 Terminal
.charsets
.ISOLatin
= null; // /A
4937 function on(el
, type
, handler
, capture
) {
4938 if (!Array
.isArray(el
)) {
4941 el
.forEach(function (element
) {
4942 element
.addEventListener(type
, handler
, capture
|| false);
4945 function off(el
, type
, handler
, capture
) {
4946 el
.removeEventListener(type
, handler
, capture
|| false);
4948 function cancel(ev
, force
) {
4949 if (!this.cancelEvents
&& !force
) {
4952 ev
.preventDefault();
4953 ev
.stopPropagation();
4956 function inherits(child
, parent
) {
4958 this.constructor = child
;
4960 f
.prototype = parent
.prototype;
4961 child
.prototype = new f
;
4963 // if bold is broken, we can't
4964 // use it in the terminal.
4965 function isBoldBroken(document
) {
4966 var body
= document
.getElementsByTagName('body')[0];
4967 var el
= document
.createElement('span');
4968 el
.innerHTML
= 'hello world';
4969 body
.appendChild(el
);
4970 var w1
= el
.scrollWidth
;
4971 el
.style
.fontWeight
= 'bold';
4972 var w2
= el
.scrollWidth
;
4973 body
.removeChild(el
);
4976 function indexOf(obj
, el
) {
4984 function isThirdLevelShift(term
, ev
) {
4985 var thirdLevelKey
= (term
.browser
.isMac
&& ev
.altKey
&& !ev
.ctrlKey
&& !ev
.metaKey
) ||
4986 (term
.browser
.isMSWindows
&& ev
.altKey
&& ev
.ctrlKey
&& !ev
.metaKey
);
4987 if (ev
.type
== 'keypress') {
4988 return thirdLevelKey
;
4990 // Don't invoke for arrows, pageDown, home, backspace, etc. (on non-keypress events)
4991 return thirdLevelKey
&& (!ev
.keyCode
|| ev
.keyCode
> 47);
4993 function matchColor(r1
, g1
, b1
) {
4994 var hash
= (r1
<< 16) | (g1
<< 8) | b1
;
4995 if (matchColor
._cache
[hash
] != null) {
4996 return matchColor
._cache
[hash
];
4998 var ldiff
= Infinity
, li
= -1, i
= 0, c
, r2
, g2
, b2
, diff
;
4999 for (; i
< Terminal
.vcolors
.length
; i
++) {
5000 c
= Terminal
.vcolors
[i
];
5004 diff
= matchColor
.distance(r1
, g1
, b1
, r2
, g2
, b2
);
5014 return matchColor
._cache
[hash
] = li
;
5016 matchColor
._cache
= {};
5017 // http://stackoverflow.com/questions/1633828
5018 matchColor
.distance = function (r1
, g1
, b1
, r2
, g2
, b2
) {
5019 return Math
.pow(30 * (r1
- r2
), 2)
5020 + Math
.pow(59 * (g1
- g2
), 2)
5021 + Math
.pow(11 * (b1
- b2
), 2);
5023 function each(obj
, iter
, con
) {
5025 return obj
.forEach(iter
, con
);
5026 for (var i
= 0; i
< obj
.length
; i
++) {
5027 iter
.call(con
, obj
[i
], i
, obj
);
5030 function keys(obj
) {
5032 return Object
.keys(obj
);
5035 if (Object
.prototype.hasOwnProperty
.call(obj
, key
)) {
5041 var wcwidth
= (function (opts
) {
5042 // extracted from https://www.cl.cam.ac.uk/%7Emgk25/ucs/wcwidth.c
5043 // combining characters
5045 [0x0300, 0x036F], [0x0483, 0x0486], [0x0488, 0x0489],
5046 [0x0591, 0x05BD], [0x05BF, 0x05BF], [0x05C1, 0x05C2],
5047 [0x05C4, 0x05C5], [0x05C7, 0x05C7], [0x0600, 0x0603],
5048 [0x0610, 0x0615], [0x064B, 0x065E], [0x0670, 0x0670],
5049 [0x06D6, 0x06E4], [0x06E7, 0x06E8], [0x06EA, 0x06ED],
5050 [0x070F, 0x070F], [0x0711, 0x0711], [0x0730, 0x074A],
5051 [0x07A6, 0x07B0], [0x07EB, 0x07F3], [0x0901, 0x0902],
5052 [0x093C, 0x093C], [0x0941, 0x0948], [0x094D, 0x094D],
5053 [0x0951, 0x0954], [0x0962, 0x0963], [0x0981, 0x0981],
5054 [0x09BC, 0x09BC], [0x09C1, 0x09C4], [0x09CD, 0x09CD],
5055 [0x09E2, 0x09E3], [0x0A01, 0x0A02], [0x0A3C, 0x0A3C],
5056 [0x0A41, 0x0A42], [0x0A47, 0x0A48], [0x0A4B, 0x0A4D],
5057 [0x0A70, 0x0A71], [0x0A81, 0x0A82], [0x0ABC, 0x0ABC],
5058 [0x0AC1, 0x0AC5], [0x0AC7, 0x0AC8], [0x0ACD, 0x0ACD],
5059 [0x0AE2, 0x0AE3], [0x0B01, 0x0B01], [0x0B3C, 0x0B3C],
5060 [0x0B3F, 0x0B3F], [0x0B41, 0x0B43], [0x0B4D, 0x0B4D],
5061 [0x0B56, 0x0B56], [0x0B82, 0x0B82], [0x0BC0, 0x0BC0],
5062 [0x0BCD, 0x0BCD], [0x0C3E, 0x0C40], [0x0C46, 0x0C48],
5063 [0x0C4A, 0x0C4D], [0x0C55, 0x0C56], [0x0CBC, 0x0CBC],
5064 [0x0CBF, 0x0CBF], [0x0CC6, 0x0CC6], [0x0CCC, 0x0CCD],
5065 [0x0CE2, 0x0CE3], [0x0D41, 0x0D43], [0x0D4D, 0x0D4D],
5066 [0x0DCA, 0x0DCA], [0x0DD2, 0x0DD4], [0x0DD6, 0x0DD6],
5067 [0x0E31, 0x0E31], [0x0E34, 0x0E3A], [0x0E47, 0x0E4E],
5068 [0x0EB1, 0x0EB1], [0x0EB4, 0x0EB9], [0x0EBB, 0x0EBC],
5069 [0x0EC8, 0x0ECD], [0x0F18, 0x0F19], [0x0F35, 0x0F35],
5070 [0x0F37, 0x0F37], [0x0F39, 0x0F39], [0x0F71, 0x0F7E],
5071 [0x0F80, 0x0F84], [0x0F86, 0x0F87], [0x0F90, 0x0F97],
5072 [0x0F99, 0x0FBC], [0x0FC6, 0x0FC6], [0x102D, 0x1030],
5073 [0x1032, 0x1032], [0x1036, 0x1037], [0x1039, 0x1039],
5074 [0x1058, 0x1059], [0x1160, 0x11FF], [0x135F, 0x135F],
5075 [0x1712, 0x1714], [0x1732, 0x1734], [0x1752, 0x1753],
5076 [0x1772, 0x1773], [0x17B4, 0x17B5], [0x17B7, 0x17BD],
5077 [0x17C6, 0x17C6], [0x17C9, 0x17D3], [0x17DD, 0x17DD],
5078 [0x180B, 0x180D], [0x18A9, 0x18A9], [0x1920, 0x1922],
5079 [0x1927, 0x1928], [0x1932, 0x1932], [0x1939, 0x193B],
5080 [0x1A17, 0x1A18], [0x1B00, 0x1B03], [0x1B34, 0x1B34],
5081 [0x1B36, 0x1B3A], [0x1B3C, 0x1B3C], [0x1B42, 0x1B42],
5082 [0x1B6B, 0x1B73], [0x1DC0, 0x1DCA], [0x1DFE, 0x1DFF],
5083 [0x200B, 0x200F], [0x202A, 0x202E], [0x2060, 0x2063],
5084 [0x206A, 0x206F], [0x20D0, 0x20EF], [0x302A, 0x302F],
5085 [0x3099, 0x309A], [0xA806, 0xA806], [0xA80B, 0xA80B],
5086 [0xA825, 0xA826], [0xFB1E, 0xFB1E], [0xFE00, 0xFE0F],
5087 [0xFE20, 0xFE23], [0xFEFF, 0xFEFF], [0xFFF9, 0xFFFB],
5088 [0x10A01, 0x10A03], [0x10A05, 0x10A06], [0x10A0C, 0x10A0F],
5089 [0x10A38, 0x10A3A], [0x10A3F, 0x10A3F], [0x1D167, 0x1D169],
5090 [0x1D173, 0x1D182], [0x1D185, 0x1D18B], [0x1D1AA, 0x1D1AD],
5091 [0x1D242, 0x1D244], [0xE0001, 0xE0001], [0xE0020, 0xE007F],
5095 function bisearch(ucs
) {
5097 var max
= COMBINING
.length
- 1;
5099 if (ucs
< COMBINING
[0][0] || ucs
> COMBINING
[max
][1])
5101 while (max
>= min
) {
5102 mid
= Math
.floor((min
+ max
) / 2);
5103 if (ucs
> COMBINING
[mid
][1])
5105 else if (ucs
< COMBINING
[mid
][0])
5112 function wcwidth(ucs
) {
5113 // test for 8-bit control characters
5116 if (ucs
< 32 || (ucs
>= 0x7f && ucs
< 0xa0))
5117 return opts
.control
;
5118 // binary search in table of non-spacing characters
5121 // if we arrive here, ucs is not a combining or C0/C1 control character
5127 (ucs
>= 0x2e80 && ucs
<= 0xa4cf && ucs
!= 0x303f) ||
5128 (ucs
>= 0xac00 && ucs
<= 0xd7a3) ||
5129 (ucs
>= 0xf900 && ucs
<= 0xfaff) ||
5130 (ucs
>= 0xfe10 && ucs
<= 0xfe19) ||
5131 (ucs
>= 0xfe30 && ucs
<= 0xfe6f) ||
5132 (ucs
>= 0xff00 && ucs
<= 0xff60) ||
5133 (ucs
>= 0xffe0 && ucs
<= 0xffe6) ||
5134 (ucs
>= 0x20000 && ucs
<= 0x2fffd) ||
5135 (ucs
>= 0x30000 && ucs
<= 0x3fffd)));
5138 })({ nul
: 0, control
: 0 }); // configurable options
5142 Terminal
.EventEmitter
= EventEmitter_js_1
.EventEmitter
;
5143 Terminal
.inherits
= inherits
;
5145 * Adds an event listener to the terminal.
5147 * @param {string} event The name of the event. TODO: Document all event types
5148 * @param {function} callback The function to call when the event is triggered.
5152 Terminal
.cancel
= cancel
;
5153 module
.exports
= Terminal
;
5155 },{"./CompositionHelper.js":1,"./EventEmitter.js":2,"./Viewport.js":3,"./handlers/Clipboard.js":4,"./utils/Browser":5}]},{},[7])(7)
5157 //# sourceMappingURL=xterm.js.map