]> git.proxmox.com Git - mirror_xterm.js.git/blob - dist/xterm.js
Merge remote-tracking branch 'upstream/master' into 361_circular_list_scrollback
[mirror_xterm.js.git] / dist / xterm.js
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){
2 /**
3 * @license MIT
4 */
5 "use strict";
6 /**
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
9 * to the handler.
10 */
11 var CompositionHelper = (function () {
12 /**
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.
17 */
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 };
25 }
26 ;
27 /**
28 * Handles the compositionstart event, activating the composition view.
29 */
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');
35 };
36 /**
37 * Handles the compositionupdate event, updating the composition view.
38 * @param {CompositionEvent} ev The event.
39 */
40 CompositionHelper.prototype.compositionupdate = function (ev) {
41 this.compositionView.textContent = ev.data;
42 this.updateCompositionElements();
43 var self = this;
44 setTimeout(function () {
45 self.compositionPosition.end = self.textarea.value.length;
46 }, 0);
47 };
48 /**
49 * Handles the compositionend event, hiding the composition view and sending the composition to
50 * the handler.
51 */
52 CompositionHelper.prototype.compositionend = function () {
53 this.finalizeComposition(true);
54 };
55 /**
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.
59 */
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"
64 return false;
65 }
66 else if (ev.keyCode === 16 || ev.keyCode === 17 || ev.keyCode === 18) {
67 // Continue composing if the keyCode is a modifier key
68 return false;
69 }
70 else {
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);
74 }
75 }
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();
80 return false;
81 }
82 return true;
83 };
84 /**
85 * Finalizes the composition, resuming regular input actions. This is called when a composition
86 * is ending.
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.
91 */
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);
101 }
102 else {
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,
108 };
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
112 // because:
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.
117 var self = this;
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;
123 var input;
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);
127 }
128 else {
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);
133 }
134 self.terminal.handler(input);
135 }
136 }, 0);
137 }
138 };
139 /**
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
143 * IME is active.
144 */
145 CompositionHelper.prototype.handleAnyTextareaChanges = function () {
146 var oldValue = this.textarea.value;
147 var self = this;
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);
155 }
156 }
157 }, 0);
158 };
159 /**
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.
164 */
165 CompositionHelper.prototype.updateCompositionElements = function (dontRecurse) {
166 if (!this.isComposing) {
167 return;
168 }
169 var cursor = this.terminal.element.querySelector('.terminal-cursor');
170 if (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
180 // text is.
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';
187 }
188 if (!dontRecurse) {
189 setTimeout(this.updateCompositionElements.bind(this, true), 0);
190 }
191 };
192 ;
193 /**
194 * Clears the textarea's position so that the cursor does not blink on IE.
195 * @private
196 */
197 CompositionHelper.prototype.clearTextareaPosition = function () {
198 this.textarea.style.left = '';
199 this.textarea.style.top = '';
200 };
201 ;
202 return CompositionHelper;
203 }());
204 exports.CompositionHelper = CompositionHelper;
205
206 },{}],2:[function(require,module,exports){
207 /**
208 * @license MIT
209 */
210 "use strict";
211 function EventEmitter() {
212 this._events = this._events || {};
213 }
214 exports.EventEmitter = EventEmitter;
215 EventEmitter.prototype.addListener = function (type, listener) {
216 this._events[type] = this._events[type] || [];
217 this._events[type].push(listener);
218 };
219 EventEmitter.prototype.on = EventEmitter.prototype.addListener;
220 EventEmitter.prototype.removeListener = function (type, listener) {
221 if (!this._events[type])
222 return;
223 var obj = this._events[type], i = obj.length;
224 while (i--) {
225 if (obj[i] === listener || obj[i].listener === listener) {
226 obj.splice(i, 1);
227 return;
228 }
229 }
230 };
231 EventEmitter.prototype.off = EventEmitter.prototype.removeListener;
232 EventEmitter.prototype.removeAllListeners = function (type) {
233 if (this._events[type])
234 delete this._events[type];
235 };
236 EventEmitter.prototype.once = function (type, listener) {
237 var self = this;
238 function on() {
239 var args = Array.prototype.slice.call(arguments);
240 this.removeListener(type, on);
241 return listener.apply(this, args);
242 }
243 on.listener = listener;
244 return this.on(type, on);
245 };
246 EventEmitter.prototype.emit = function (type) {
247 if (!this._events[type])
248 return;
249 var args = Array.prototype.slice.call(arguments, 1), obj = this._events[type], l = obj.length, i = 0;
250 for (; i < l; i++) {
251 obj[i].apply(this, args);
252 }
253 };
254 EventEmitter.prototype.listeners = function (type) {
255 return this._events[type] = this._events[type] || [];
256 };
257
258 },{}],3:[function(require,module,exports){
259 /**
260 * @license MIT
261 */
262 "use strict";
263 /**
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.
266 */
267 var Viewport = (function () {
268 /**
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.
274 */
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();
287 }
288 /**
289 * Refreshes row height, setting line-height, viewport height and scroll area height if
290 * necessary.
291 * @param charSize A character size measurement bounding rect object, if it doesn't exist it will
292 * be created.
293 */
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';
302 }
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';
307 }
308 this.scrollArea.style.height = (size.height * this.lastRecordedBufferLength) + 'px';
309 }
310 };
311 /**
312 * Updates dimensions and synchronizes the scroll area if necessary.
313 */
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;
318 this.refresh();
319 }
320 else if (this.lastRecordedViewportHeight !== this.terminal.rows) {
321 // If viewport height changed
322 this.refresh();
323 }
324 else {
325 // If size has changed, refresh viewport
326 var size = this.charMeasureElement.getBoundingClientRect();
327 if (size.height !== this.currentRowHeight) {
328 this.refresh(size);
329 }
330 }
331 // Sync scrollTop
332 var scrollTop = this.terminal.ydisp * this.currentRowHeight;
333 if (this.viewportElement.scrollTop !== scrollTop) {
334 this.viewportElement.scrollTop = scrollTop;
335 }
336 };
337 /**
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.
341 */
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);
346 };
347 /**
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
350 * `Viewport`.
351 * @param ev The mouse wheel event.
352 */
353 Viewport.prototype.onWheel = function (ev) {
354 if (ev.deltaY === 0) {
355 // Do nothing if it's not a vertical scroll event
356 return;
357 }
358 // Fallback to WheelEvent.DOM_DELTA_PIXEL
359 var multiplier = 1;
360 if (ev.deltaMode === WheelEvent.DOM_DELTA_LINE) {
361 multiplier = this.currentRowHeight;
362 }
363 else if (ev.deltaMode === WheelEvent.DOM_DELTA_PAGE) {
364 multiplier = this.currentRowHeight * this.terminal.rows;
365 }
366 this.viewportElement.scrollTop += ev.deltaY * multiplier;
367 // Prevent the page from scrolling when the terminal scrolls
368 ev.preventDefault();
369 };
370 ;
371 return Viewport;
372 }());
373 exports.Viewport = Viewport;
374
375 },{}],4:[function(require,module,exports){
376 /**
377 * Clipboard handler module: exports methods for handling all clipboard-related events in the
378 * terminal.
379 * @module xterm/handlers/Clipboard
380 * @license MIT
381 */
382 "use strict";
383 /**
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
388 * @returns {string}
389 */
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;
396 }).join('\n');
397 return processedText;
398 }
399 exports.prepareTextForClipboard = prepareTextForClipboard;
400 /**
401 * Binds copy functionality to the given terminal.
402 * @param {ClipboardEvent} ev The original copy event to be handled
403 */
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);
410 }
411 else {
412 ev.clipboardData.setData('text/plain', text);
413 }
414 ev.preventDefault(); // Prevent or the original text will be copied.
415 }
416 exports.copyHandler = copyHandler;
417 /**
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
421 */
422 function pasteHandler(ev, term) {
423 ev.stopPropagation();
424 var text;
425 var dispatchPaste = function (text) {
426 term.handler(text);
427 term.textarea.value = '';
428 return term.cancel(ev);
429 };
430 if (term.browser.isMSIE) {
431 if (window.clipboardData) {
432 text = window.clipboardData.getData('Text');
433 dispatchPaste(text);
434 }
435 }
436 else {
437 if (ev.clipboardData) {
438 text = ev.clipboardData.getData('text/plain');
439 dispatchPaste(text);
440 }
441 }
442 }
443 exports.pasteHandler = pasteHandler;
444 /**
445 * Bind to right-click event and allow right-click copy and paste.
446 *
447 * **Logic**
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 {MouseEvent} ev The original right click event to be handled
455 * @param {Terminal} term The terminal on which to apply the handled paste event
456 */
457 function rightClickHandler(ev, term) {
458 var s = document.getSelection(), selectedText = prepareTextForClipboard(s.toString()), clickIsOnSelection = false, x = ev.clientX, y = ev.clientY;
459 if (s.rangeCount) {
460 var r = s.getRangeAt(0), cr = r.getClientRects();
461 for (var i = 0; i < cr.length; i++) {
462 var rect = cr[i];
463 clickIsOnSelection = ((x > rect.left) && (x < rect.right) &&
464 (y > rect.top) && (y < rect.bottom));
465 if (clickIsOnSelection) {
466 break;
467 }
468 }
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 &nbsp;
472 // and there is not much pointing in copying a single space.
473 if (selectedText.match(/^\s$/) || !selectedText.length) {
474 clickIsOnSelection = false;
475 }
476 }
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;
494 }, 4);
495 }
496 }
497 exports.rightClickHandler = rightClickHandler;
498
499 },{}],5:[function(require,module,exports){
500 /**
501 * Attributes and methods to help with identifying the current browser and platform.
502 * @module xterm/utils/Browser
503 * @license MIT
504 */
505 "use strict";
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);
519
520 },{"./Generic.js":6}],6:[function(require,module,exports){
521 /**
522 * Generic utilities module with methods that can be helpful at different parts of the code base.
523 * @module xterm/utils/Generic
524 * @license MIT
525 */
526 "use strict";
527 /**
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
531 */
532 exports.contains = function (arr, el) {
533 return arr.indexOf(el) >= 0;
534 };
535
536 },{}],7:[function(require,module,exports){
537 /**
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
545 * other features.
546 * @license MIT
547 */
548 "use strict";
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');
554 /**
555 * Terminal Emulation References:
556 * http://vt100.net/
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
563 */
564 // Let it work inside Node.js for automated testing purposes.
565 var document = (typeof window != 'undefined') ? window.document : null;
566 /**
567 * States
568 */
569 var normal = 0, escaped = 1, csi = 2, osc = 3, charset = 4, dcs = 5, ignore = 6;
570 /**
571 * Terminal
572 */
573 /**
574 * Creates a new `Terminal` object.
575 *
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)
580 *
581 * @public
582 * @class Xterm Xterm
583 * @alias module:xterm/src/xterm
584 */
585 function Terminal(options) {
586 var self = this;
587 if (!(this instanceof Terminal)) {
588 return new Terminal(arguments[0], arguments[1], arguments[2]);
589 }
590 self.browser = Browser;
591 self.cancel = Terminal.cancel;
592 EventEmitter_js_1.EventEmitter.call(this);
593 if (typeof options === 'number') {
594 options = {
595 cols: arguments[0],
596 rows: arguments[1],
597 handler: arguments[2]
598 };
599 }
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];
606 }
607 }
608 self[key] = options[key];
609 });
610 if (options.colors.length === 8) {
611 options.colors = options.colors.concat(Terminal._colors.slice(8));
612 }
613 else if (options.colors.length === 16) {
614 options.colors = options.colors.concat(Terminal._colors.slice(16));
615 }
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));
618 }
619 else if (options.colors.length === 18) {
620 options.colors = options.colors.concat(Terminal._colors.slice(16, -2), options.colors.slice(-2));
621 }
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);
632 }
633 /**
634 * The scroll position of the y cursor, ie. ybase + y = the y position within the entire
635 * buffer
636 */
637 this.ybase = 0;
638 /**
639 * The scroll position of the viewport
640 */
641 this.ydisp = 0;
642 /**
643 * The cursor's x position after ybase
644 */
645 this.x = 0;
646 /**
647 * The cursor's y position after ybase
648 */
649 this.y = 0;
650 /**
651 * Used to debounce the refresh function
652 */
653 this.isRefreshing = false;
654 /**
655 * Whether there is a full terminal refresh queued
656 */
657 this.cursorState = 0;
658 this.cursorHidden = false;
659 this.convertEol;
660 this.state = 0;
661 this.queue = '';
662 this.scrollTop = 0;
663 this.scrollBottom = this.rows - 1;
664 this.customKeydownHandler = null;
665 // modes
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
671 this.normal = null;
672 // charset
673 this.charset = null;
674 this.gcharset = null;
675 this.glevel = 0;
676 this.charsets = [null];
677 // mouse properties
678 this.decLocator;
679 this.x10Mouse;
680 this.vt200Mouse;
681 this.vt300Mouse;
682 this.normalMouse;
683 this.mouseEvents;
684 this.sendFocus;
685 this.utfMouse;
686 this.sgrMouse;
687 this.urxvtMouse;
688 // misc
689 this.element;
690 this.children;
691 this.refreshStart;
692 this.refreshEnd;
693 this.savedX;
694 this.savedY;
695 this.savedCols;
696 // stream
697 this.readable = true;
698 this.writable = true;
699 this.defAttr = (0 << 18) | (257 << 9) | (256 << 0);
700 this.curAttr = this.defAttr;
701 this.params = [];
702 this.currentParam = 0;
703 this.prefix = '';
704 this.postfix = '';
705 // leftover surrogate high from previous write invocation
706 this.surrogate_high = '';
707 /**
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.
710 */
711 this.lines = [];
712 var i = this.rows;
713 while (i--) {
714 this.lines.push(this.blankLine());
715 }
716 this.tabs;
717 this.setupStops();
718 // Store if user went browsing history in scrollback
719 this.userScrolling = false;
720 }
721 inherits(Terminal, EventEmitter_js_1.EventEmitter);
722 /**
723 * back_color_erase feature for xterm.
724 */
725 Terminal.prototype.eraseAttr = function () {
726 // if (this.is('screen')) return this.defAttr;
727 return (this.defAttr & ~0x1ff) | (this.curAttr & 0x1ff);
728 };
729 /**
730 * Colors
731 */
732 // Colors 0-15
733 Terminal.tangoColors = [
734 // dark:
735 '#2e3436',
736 '#cc0000',
737 '#4e9a06',
738 '#c4a000',
739 '#3465a4',
740 '#75507b',
741 '#06989a',
742 '#d3d7cf',
743 // bright:
744 '#555753',
745 '#ef2929',
746 '#8ae234',
747 '#fce94f',
748 '#729fcf',
749 '#ad7fa8',
750 '#34e2e2',
751 '#eeeeec'
752 ];
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;
757 // 16-231
758 i = 0;
759 for (; i < 216; i++) {
760 out(r[(i / 36) % 6 | 0], r[(i / 6) % 6 | 0], r[i % 6]);
761 }
762 // 232-255 (grey)
763 i = 0;
764 for (; i < 24; i++) {
765 r = 8 + i * 10;
766 out(r, r, r);
767 }
768 function out(r, g, b) {
769 colors.push('#' + hex(r) + hex(g) + hex(b));
770 }
771 function hex(c) {
772 c = c.toString(16);
773 return c.length < 2 ? '0' + c : c;
774 }
775 return colors;
776 })();
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);
782 out.push([
783 (color >> 16) & 0xff,
784 (color >> 8) & 0xff,
785 color & 0xff
786 ]);
787 }
788 return out;
789 })();
790 /**
791 * Options
792 */
793 Terminal.defaults = {
794 colors: Terminal.colors,
795 theme: 'default',
796 convertEol: false,
797 termName: 'xterm',
798 geometry: [80, 24],
799 cursorBlink: false,
800 visualBell: false,
801 popOnBell: false,
802 scrollback: 1000,
803 screenKeys: false,
804 debug: false,
805 cancelEvents: false
806 };
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];
812 });
813 /**
814 * Focus the terminal. Delegates focus handling to the terminal's DOM element.
815 */
816 Terminal.prototype.focus = function () {
817 return this.textarea.focus();
818 };
819 /**
820 * Retrieves an option's value from the terminal.
821 * @param {string} key The option key.
822 */
823 Terminal.prototype.getOption = function (key, value) {
824 if (!(key in Terminal.defaults)) {
825 throw new Error('No option with key "' + key + '"');
826 }
827 if (typeof this.options[key] !== 'undefined') {
828 return this.options[key];
829 }
830 return this[key];
831 };
832 /**
833 * Sets an option on the terminal.
834 * @param {string} key The option key.
835 * @param {string} value The option value.
836 */
837 Terminal.prototype.setOption = function (key, value) {
838 if (!(key in Terminal.defaults)) {
839 throw new Error('No option with key "' + key + '"');
840 }
841 this[key] = value;
842 this.options[key] = value;
843 };
844 /**
845 * Binds the desired focus behavior on a given terminal object.
846 *
847 * @static
848 */
849 Terminal.bindFocus = function (term) {
850 on(term.textarea, 'focus', function (ev) {
851 if (term.sendFocus) {
852 term.send('\x1b[I');
853 }
854 term.element.classList.add('focus');
855 term.showCursor();
856 Terminal.focus = term;
857 term.emit('focus', { terminal: term });
858 });
859 };
860 /**
861 * Blur the terminal. Delegates blur handling to the terminal's DOM element.
862 */
863 Terminal.prototype.blur = function () {
864 return this.textarea.blur();
865 };
866 /**
867 * Binds the desired blur behavior on a given terminal object.
868 *
869 * @static
870 */
871 Terminal.bindBlur = function (term) {
872 on(term.textarea, 'blur', function (ev) {
873 term.refresh(term.y, term.y);
874 if (term.sendFocus) {
875 term.send('\x1b[O');
876 }
877 term.element.classList.remove('focus');
878 Terminal.focus = null;
879 term.emit('blur', { terminal: term });
880 });
881 };
882 /**
883 * Initialize default behavior
884 */
885 Terminal.prototype.initGlobal = function () {
886 var term = this;
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);
893 });
894 on(this.textarea, 'paste', function (ev) {
895 Clipboard_js_1.pasteHandler.call(this, ev, term);
896 });
897 on(this.element, 'paste', function (ev) {
898 Clipboard_js_1.pasteHandler.call(this, ev, term);
899 });
900 function rightClickHandlerWrapper(ev) {
901 Clipboard_js_1.rightClickHandler.call(this, ev, term);
902 }
903 if (term.browser.isFirefox) {
904 on(this.element, 'mousedown', function (ev) {
905 if (ev.button == 2) {
906 rightClickHandlerWrapper(ev);
907 }
908 });
909 }
910 else {
911 on(this.element, 'contextmenu', rightClickHandlerWrapper);
912 }
913 };
914 /**
915 * Apply key handling to the terminal
916 */
917 Terminal.bindKeys = function (term) {
918 on(term.element, 'keydown', function (ev) {
919 if (document.activeElement != this) {
920 return;
921 }
922 term.keyDown(ev);
923 }, true);
924 on(term.element, 'keypress', function (ev) {
925 if (document.activeElement != this) {
926 return;
927 }
928 term.keyPress(ev);
929 }, true);
930 on(term.element, 'keyup', term.focus.bind(term));
931 on(term.textarea, 'keydown', function (ev) {
932 term.keyDown(ev);
933 }, true);
934 on(term.textarea, 'keypress', function (ev) {
935 term.keyPress(ev);
936 // Truncate the textarea's value, since it is not needed
937 this.value = '';
938 }, true);
939 on(term.textarea, 'compositionstart', term.compositionHelper.compositionstart.bind(term.compositionHelper));
940 on(term.textarea, 'compositionupdate', term.compositionHelper.compositionupdate.bind(term.compositionHelper));
941 on(term.textarea, 'compositionend', term.compositionHelper.compositionend.bind(term.compositionHelper));
942 term.on('refresh', term.compositionHelper.updateCompositionElements.bind(term.compositionHelper));
943 };
944 /**
945 * Insert the given row to the terminal or produce a new one
946 * if no row argument is passed. Return the inserted row.
947 * @param {HTMLElement} row (optional) The row to append to the terminal.
948 */
949 Terminal.prototype.insertRow = function (row) {
950 if (typeof row != 'object') {
951 row = document.createElement('div');
952 }
953 this.rowContainer.appendChild(row);
954 this.children.push(row);
955 return row;
956 };
957 /**
958 * Opens the terminal within an element.
959 *
960 * @param {HTMLElement} parent The element to create the terminal within.
961 */
962 Terminal.prototype.open = function (parent) {
963 var self = this, i = 0, div;
964 this.parent = parent || this.parent;
965 if (!this.parent) {
966 throw new Error('Terminal requires a parent element.');
967 }
968 // Grab global elements
969 this.context = this.parent.ownerDocument.defaultView;
970 this.document = this.parent.ownerDocument;
971 this.body = this.document.getElementsByTagName('body')[0];
972 //Create main element container
973 this.element = this.document.createElement('div');
974 this.element.classList.add('terminal');
975 this.element.classList.add('xterm');
976 this.element.classList.add('xterm-theme-' + this.theme);
977 this.element.style.height;
978 this.element.setAttribute('tabindex', 0);
979 this.viewportElement = document.createElement('div');
980 this.viewportElement.classList.add('xterm-viewport');
981 this.element.appendChild(this.viewportElement);
982 this.viewportScrollArea = document.createElement('div');
983 this.viewportScrollArea.classList.add('xterm-scroll-area');
984 this.viewportElement.appendChild(this.viewportScrollArea);
985 // Create the container that will hold the lines of the terminal and then
986 // produce the lines the lines.
987 this.rowContainer = document.createElement('div');
988 this.rowContainer.classList.add('xterm-rows');
989 this.element.appendChild(this.rowContainer);
990 this.children = [];
991 // Create the container that will hold helpers like the textarea for
992 // capturing DOM Events. Then produce the helpers.
993 this.helperContainer = document.createElement('div');
994 this.helperContainer.classList.add('xterm-helpers');
995 // TODO: This should probably be inserted once it's filled to prevent an additional layout
996 this.element.appendChild(this.helperContainer);
997 this.textarea = document.createElement('textarea');
998 this.textarea.classList.add('xterm-helper-textarea');
999 this.textarea.setAttribute('autocorrect', 'off');
1000 this.textarea.setAttribute('autocapitalize', 'off');
1001 this.textarea.setAttribute('spellcheck', 'false');
1002 this.textarea.tabIndex = 0;
1003 this.textarea.addEventListener('focus', function () {
1004 self.emit('focus', { terminal: self });
1005 });
1006 this.textarea.addEventListener('blur', function () {
1007 self.emit('blur', { terminal: self });
1008 });
1009 this.helperContainer.appendChild(this.textarea);
1010 this.compositionView = document.createElement('div');
1011 this.compositionView.classList.add('composition-view');
1012 this.compositionHelper = new CompositionHelper_js_1.CompositionHelper(this.textarea, this.compositionView, this);
1013 this.helperContainer.appendChild(this.compositionView);
1014 this.charMeasureElement = document.createElement('div');
1015 this.charMeasureElement.classList.add('xterm-char-measure-element');
1016 this.charMeasureElement.innerHTML = 'W';
1017 this.helperContainer.appendChild(this.charMeasureElement);
1018 for (; i < this.rows; i++) {
1019 this.insertRow();
1020 }
1021 this.parent.appendChild(this.element);
1022 this.viewport = new Viewport_js_1.Viewport(this, this.viewportElement, this.viewportScrollArea, this.charMeasureElement);
1023 // Draw the screen.
1024 this.refresh(0, this.rows - 1);
1025 // Initialize global actions that
1026 // need to be taken on the document.
1027 this.initGlobal();
1028 // Ensure there is a Terminal.focus.
1029 this.focus();
1030 on(this.element, 'click', function () {
1031 var selection = document.getSelection(), collapsed = selection.isCollapsed, isRange = typeof collapsed == 'boolean' ? !collapsed : selection.type == 'Range';
1032 if (!isRange) {
1033 self.focus();
1034 }
1035 });
1036 // Listen for mouse events and translate
1037 // them into terminal mouse protocols.
1038 this.bindMouse();
1039 // Figure out whether boldness affects
1040 // the character width of monospace fonts.
1041 if (Terminal.brokenBold == null) {
1042 Terminal.brokenBold = isBoldBroken(this.document);
1043 }
1044 /**
1045 * This event is emitted when terminal has completed opening.
1046 *
1047 * @event open
1048 */
1049 this.emit('open');
1050 };
1051 /**
1052 * Attempts to load an add-on using CommonJS or RequireJS (whichever is available).
1053 * @param {string} addon The name of the addon to load
1054 * @static
1055 */
1056 Terminal.loadAddon = function (addon, callback) {
1057 if (typeof exports === 'object' && typeof module === 'object') {
1058 // CommonJS
1059 return require('./addons/' + addon + '/' + addon);
1060 }
1061 else if (typeof define == 'function') {
1062 // RequireJS
1063 return require(['./addons/' + addon + '/' + addon], callback);
1064 }
1065 else {
1066 console.error('Cannot load a module without a CommonJS or RequireJS environment.');
1067 return false;
1068 }
1069 };
1070 /**
1071 * XTerm mouse events
1072 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#Mouse%20Tracking
1073 * To better understand these
1074 * the xterm code is very helpful:
1075 * Relevant files:
1076 * button.c, charproc.c, misc.c
1077 * Relevant functions in xterm/button.c:
1078 * BtnCode, EmitButtonCode, EditorButton, SendMousePosition
1079 */
1080 Terminal.prototype.bindMouse = function () {
1081 var el = this.element, self = this, pressed = 32;
1082 // mouseup, mousedown, wheel
1083 // left click: ^[[M 3<^[[M#3<
1084 // wheel up: ^[[M`3>
1085 function sendButton(ev) {
1086 var button, pos;
1087 // get the xterm-style button
1088 button = getButton(ev);
1089 // get mouse coordinates
1090 pos = getCoords(ev);
1091 if (!pos)
1092 return;
1093 sendEvent(button, pos);
1094 switch (ev.overrideType || ev.type) {
1095 case 'mousedown':
1096 pressed = button;
1097 break;
1098 case 'mouseup':
1099 // keep it at the left
1100 // button, just in case.
1101 pressed = 32;
1102 break;
1103 case 'wheel':
1104 // nothing. don't
1105 // interfere with
1106 // `pressed`.
1107 break;
1108 }
1109 }
1110 // motion example of a left click:
1111 // ^[[M 3<^[[M@4<^[[M@5<^[[M@6<^[[M@7<^[[M#7<
1112 function sendMove(ev) {
1113 var button = pressed, pos;
1114 pos = getCoords(ev);
1115 if (!pos)
1116 return;
1117 // buttons marked as motions
1118 // are incremented by 32
1119 button += 32;
1120 sendEvent(button, pos);
1121 }
1122 // encode button and
1123 // position to characters
1124 function encode(data, ch) {
1125 if (!self.utfMouse) {
1126 if (ch === 255)
1127 return data.push(0);
1128 if (ch > 127)
1129 ch = 127;
1130 data.push(ch);
1131 }
1132 else {
1133 if (ch === 2047)
1134 return data.push(0);
1135 if (ch < 127) {
1136 data.push(ch);
1137 }
1138 else {
1139 if (ch > 2047)
1140 ch = 2047;
1141 data.push(0xC0 | (ch >> 6));
1142 data.push(0x80 | (ch & 0x3F));
1143 }
1144 }
1145 }
1146 // send a mouse event:
1147 // regular/utf8: ^[[M Cb Cx Cy
1148 // urxvt: ^[[ Cb ; Cx ; Cy M
1149 // sgr: ^[[ Cb ; Cx ; Cy M/m
1150 // vt300: ^[[ 24(1/3/5)~ [ Cx , Cy ] \r
1151 // locator: CSI P e ; P b ; P r ; P c ; P p & w
1152 function sendEvent(button, pos) {
1153 // self.emit('mouse', {
1154 // x: pos.x - 32,
1155 // y: pos.x - 32,
1156 // button: button
1157 // });
1158 if (self.vt300Mouse) {
1159 // NOTE: Unstable.
1160 // http://www.vt100.net/docs/vt3xx-gp/chapter15.html
1161 button &= 3;
1162 pos.x -= 32;
1163 pos.y -= 32;
1164 var data = '\x1b[24';
1165 if (button === 0)
1166 data += '1';
1167 else if (button === 1)
1168 data += '3';
1169 else if (button === 2)
1170 data += '5';
1171 else if (button === 3)
1172 return;
1173 else
1174 data += '0';
1175 data += '~[' + pos.x + ',' + pos.y + ']\r';
1176 self.send(data);
1177 return;
1178 }
1179 if (self.decLocator) {
1180 // NOTE: Unstable.
1181 button &= 3;
1182 pos.x -= 32;
1183 pos.y -= 32;
1184 if (button === 0)
1185 button = 2;
1186 else if (button === 1)
1187 button = 4;
1188 else if (button === 2)
1189 button = 6;
1190 else if (button === 3)
1191 button = 3;
1192 self.send('\x1b['
1193 + button
1194 + ';'
1195 + (button === 3 ? 4 : 0)
1196 + ';'
1197 + pos.y
1198 + ';'
1199 + pos.x
1200 + ';'
1201 + (pos.page || 0)
1202 + '&w');
1203 return;
1204 }
1205 if (self.urxvtMouse) {
1206 pos.x -= 32;
1207 pos.y -= 32;
1208 pos.x++;
1209 pos.y++;
1210 self.send('\x1b[' + button + ';' + pos.x + ';' + pos.y + 'M');
1211 return;
1212 }
1213 if (self.sgrMouse) {
1214 pos.x -= 32;
1215 pos.y -= 32;
1216 self.send('\x1b[<'
1217 + (((button & 3) === 3 ? button & ~3 : button) - 32)
1218 + ';'
1219 + pos.x
1220 + ';'
1221 + pos.y
1222 + ((button & 3) === 3 ? 'm' : 'M'));
1223 return;
1224 }
1225 var data = [];
1226 encode(data, button);
1227 encode(data, pos.x);
1228 encode(data, pos.y);
1229 self.send('\x1b[M' + String.fromCharCode.apply(String, data));
1230 }
1231 function getButton(ev) {
1232 var button, shift, meta, ctrl, mod;
1233 // two low bits:
1234 // 0 = left
1235 // 1 = middle
1236 // 2 = right
1237 // 3 = release
1238 // wheel up/down:
1239 // 1, and 2 - with 64 added
1240 switch (ev.overrideType || ev.type) {
1241 case 'mousedown':
1242 button = ev.button != null
1243 ? +ev.button
1244 : ev.which != null
1245 ? ev.which - 1
1246 : null;
1247 if (self.browser.isMSIE) {
1248 button = button === 1 ? 0 : button === 4 ? 1 : button;
1249 }
1250 break;
1251 case 'mouseup':
1252 button = 3;
1253 break;
1254 case 'DOMMouseScroll':
1255 button = ev.detail < 0
1256 ? 64
1257 : 65;
1258 break;
1259 case 'wheel':
1260 button = ev.wheelDeltaY > 0
1261 ? 64
1262 : 65;
1263 break;
1264 }
1265 // next three bits are the modifiers:
1266 // 4 = shift, 8 = meta, 16 = control
1267 shift = ev.shiftKey ? 4 : 0;
1268 meta = ev.metaKey ? 8 : 0;
1269 ctrl = ev.ctrlKey ? 16 : 0;
1270 mod = shift | meta | ctrl;
1271 // no mods
1272 if (self.vt200Mouse) {
1273 // ctrl only
1274 mod &= ctrl;
1275 }
1276 else if (!self.normalMouse) {
1277 mod = 0;
1278 }
1279 // increment to SP
1280 button = (32 + (mod << 2)) + button;
1281 return button;
1282 }
1283 // mouse coordinates measured in cols/rows
1284 function getCoords(ev) {
1285 var x, y, w, h, el;
1286 // ignore browsers without pageX for now
1287 if (ev.pageX == null)
1288 return;
1289 x = ev.pageX;
1290 y = ev.pageY;
1291 el = self.element;
1292 // should probably check offsetParent
1293 // but this is more portable
1294 while (el && el !== self.document.documentElement) {
1295 x -= el.offsetLeft;
1296 y -= el.offsetTop;
1297 el = 'offsetParent' in el
1298 ? el.offsetParent
1299 : el.parentNode;
1300 }
1301 // convert to cols/rows
1302 w = self.element.clientWidth;
1303 h = self.element.clientHeight;
1304 x = Math.ceil((x / w) * self.cols);
1305 y = Math.ceil((y / h) * self.rows);
1306 // be sure to avoid sending
1307 // bad positions to the program
1308 if (x < 0)
1309 x = 0;
1310 if (x > self.cols)
1311 x = self.cols;
1312 if (y < 0)
1313 y = 0;
1314 if (y > self.rows)
1315 y = self.rows;
1316 // xterm sends raw bytes and
1317 // starts at 32 (SP) for each.
1318 x += 32;
1319 y += 32;
1320 return {
1321 x: x,
1322 y: y,
1323 type: 'wheel'
1324 };
1325 }
1326 on(el, 'mousedown', function (ev) {
1327 if (!self.mouseEvents)
1328 return;
1329 // send the button
1330 sendButton(ev);
1331 // ensure focus
1332 self.focus();
1333 // fix for odd bug
1334 //if (self.vt200Mouse && !self.normalMouse) {
1335 if (self.vt200Mouse) {
1336 ev.overrideType = 'mouseup';
1337 sendButton(ev);
1338 return self.cancel(ev);
1339 }
1340 // bind events
1341 if (self.normalMouse)
1342 on(self.document, 'mousemove', sendMove);
1343 // x10 compatibility mode can't send button releases
1344 if (!self.x10Mouse) {
1345 on(self.document, 'mouseup', function up(ev) {
1346 sendButton(ev);
1347 if (self.normalMouse)
1348 off(self.document, 'mousemove', sendMove);
1349 off(self.document, 'mouseup', up);
1350 return self.cancel(ev);
1351 });
1352 }
1353 return self.cancel(ev);
1354 });
1355 //if (self.normalMouse) {
1356 // on(self.document, 'mousemove', sendMove);
1357 //}
1358 on(el, 'wheel', function (ev) {
1359 if (!self.mouseEvents)
1360 return;
1361 if (self.x10Mouse
1362 || self.vt300Mouse
1363 || self.decLocator)
1364 return;
1365 sendButton(ev);
1366 return self.cancel(ev);
1367 });
1368 // allow wheel scrolling in
1369 // the shell for example
1370 on(el, 'wheel', function (ev) {
1371 if (self.mouseEvents)
1372 return;
1373 self.viewport.onWheel(ev);
1374 return self.cancel(ev);
1375 });
1376 };
1377 /**
1378 * Destroys the terminal.
1379 */
1380 Terminal.prototype.destroy = function () {
1381 this.readable = false;
1382 this.writable = false;
1383 this._events = {};
1384 this.handler = function () { };
1385 this.write = function () { };
1386 if (this.element.parentNode) {
1387 this.element.parentNode.removeChild(this.element);
1388 }
1389 //this.emit('close');
1390 };
1391 /**
1392 * Flags used to render terminal text properly
1393 */
1394 Terminal.flags = {
1395 BOLD: 1,
1396 UNDERLINE: 2,
1397 BLINK: 4,
1398 INVERSE: 8,
1399 INVISIBLE: 16
1400 };
1401 /**
1402 * Refreshes (re-renders) terminal content within two rows (inclusive)
1403 *
1404 * Rendering Engine:
1405 *
1406 * In the screen buffer, each character is stored as a an array with a character
1407 * and a 32-bit integer:
1408 * - First value: a utf-16 character.
1409 * - Second value:
1410 * - Next 9 bits: background color (0-511).
1411 * - Next 9 bits: foreground color (0-511).
1412 * - Next 14 bits: a mask for misc. flags:
1413 * - 1=bold
1414 * - 2=underline
1415 * - 4=blink
1416 * - 8=inverse
1417 * - 16=invisible
1418 *
1419 * @param {number} start The row to start from (between 0 and terminal's height terminal - 1)
1420 * @param {number} end The row to end at (between fromRow and terminal's height terminal - 1)
1421 * @param {boolean} queue Whether the refresh should ran right now or be queued
1422 */
1423 Terminal.prototype.refresh = function (start, end, queue) {
1424 var self = this;
1425 // queue defaults to true
1426 queue = (typeof queue == 'undefined') ? true : queue;
1427 /**
1428 * The refresh queue allows refresh to execute only approximately 30 times a second. For
1429 * commands that pass a significant amount of output to the write function, this prevents the
1430 * terminal from maxing out the CPU and making the UI unresponsive. While commands can still
1431 * run beyond what they do on the terminal, it is far better with a debounce in place as
1432 * every single terminal manipulation does not need to be constructed in the DOM.
1433 *
1434 * A side-effect of this is that it makes ^C to interrupt a process seem more responsive.
1435 */
1436 if (queue) {
1437 // If refresh should be queued, order the refresh and return.
1438 if (this._refreshIsQueued) {
1439 // If a refresh has already been queued, just order a full refresh next
1440 this._fullRefreshNext = true;
1441 }
1442 else {
1443 setTimeout(function () {
1444 self.refresh(start, end, false);
1445 }, 34);
1446 this._refreshIsQueued = true;
1447 }
1448 return;
1449 }
1450 // If refresh should be run right now (not be queued), release the lock
1451 this._refreshIsQueued = false;
1452 // If multiple refreshes were requested, make a full refresh.
1453 if (this._fullRefreshNext) {
1454 start = 0;
1455 end = this.rows - 1;
1456 this._fullRefreshNext = false; // reset lock
1457 }
1458 var x, y, i, line, out, ch, ch_width, width, data, attr, bg, fg, flags, row, parent, focused = document.activeElement;
1459 // If this is a big refresh, remove the terminal rows from the DOM for faster calculations
1460 if (end - start >= this.rows / 2) {
1461 parent = this.element.parentNode;
1462 if (parent) {
1463 this.element.removeChild(this.rowContainer);
1464 }
1465 }
1466 width = this.cols;
1467 y = start;
1468 if (end >= this.rows.length) {
1469 this.log('`end` is too large. Most likely a bad CSR.');
1470 end = this.rows.length - 1;
1471 }
1472 for (; y <= end; y++) {
1473 row = y + this.ydisp;
1474 line = this.lines[row];
1475 out = '';
1476 if (this.y === y - (this.ybase - this.ydisp)
1477 && this.cursorState
1478 && !this.cursorHidden) {
1479 x = this.x;
1480 }
1481 else {
1482 x = -1;
1483 }
1484 attr = this.defAttr;
1485 i = 0;
1486 for (; i < width; i++) {
1487 data = line[i][0];
1488 ch = line[i][1];
1489 ch_width = line[i][2];
1490 if (!ch_width)
1491 continue;
1492 if (i === x)
1493 data = -1;
1494 if (data !== attr) {
1495 if (attr !== this.defAttr) {
1496 out += '</span>';
1497 }
1498 if (data !== this.defAttr) {
1499 if (data === -1) {
1500 out += '<span class="reverse-video terminal-cursor';
1501 if (this.cursorBlink) {
1502 out += ' blinking';
1503 }
1504 out += '">';
1505 }
1506 else {
1507 var classNames = [];
1508 bg = data & 0x1ff;
1509 fg = (data >> 9) & 0x1ff;
1510 flags = data >> 18;
1511 if (flags & Terminal.flags.BOLD) {
1512 if (!Terminal.brokenBold) {
1513 classNames.push('xterm-bold');
1514 }
1515 // See: XTerm*boldColors
1516 if (fg < 8)
1517 fg += 8;
1518 }
1519 if (flags & Terminal.flags.UNDERLINE) {
1520 classNames.push('xterm-underline');
1521 }
1522 if (flags & Terminal.flags.BLINK) {
1523 classNames.push('xterm-blink');
1524 }
1525 // If inverse flag is on, then swap the foreground and background variables.
1526 if (flags & Terminal.flags.INVERSE) {
1527 /* One-line variable swap in JavaScript: http://stackoverflow.com/a/16201730 */
1528 bg = [fg, fg = bg][0];
1529 // Should inverse just be before the
1530 // above boldColors effect instead?
1531 if ((flags & 1) && fg < 8)
1532 fg += 8;
1533 }
1534 if (flags & Terminal.flags.INVISIBLE) {
1535 classNames.push('xterm-hidden');
1536 }
1537 /**
1538 * Weird situation: Invert flag used black foreground and white background results
1539 * in invalid background color, positioned at the 256 index of the 256 terminal
1540 * color map. Pin the colors manually in such a case.
1541 *
1542 * Source: https://github.com/sourcelair/xterm.js/issues/57
1543 */
1544 if (flags & Terminal.flags.INVERSE) {
1545 if (bg == 257) {
1546 bg = 15;
1547 }
1548 if (fg == 256) {
1549 fg = 0;
1550 }
1551 }
1552 if (bg < 256) {
1553 classNames.push('xterm-bg-color-' + bg);
1554 }
1555 if (fg < 256) {
1556 classNames.push('xterm-color-' + fg);
1557 }
1558 out += '<span';
1559 if (classNames.length) {
1560 out += ' class="' + classNames.join(' ') + '"';
1561 }
1562 out += '>';
1563 }
1564 }
1565 }
1566 switch (ch) {
1567 case '&':
1568 out += '&amp;';
1569 break;
1570 case '<':
1571 out += '&lt;';
1572 break;
1573 case '>':
1574 out += '&gt;';
1575 break;
1576 default:
1577 if (ch <= ' ') {
1578 out += '&nbsp;';
1579 }
1580 else {
1581 out += ch;
1582 }
1583 break;
1584 }
1585 attr = data;
1586 }
1587 if (attr !== this.defAttr) {
1588 out += '</span>';
1589 }
1590 this.children[y].innerHTML = out;
1591 }
1592 if (parent) {
1593 this.element.appendChild(this.rowContainer);
1594 }
1595 this.emit('refresh', { element: this.element, start: start, end: end });
1596 };
1597 /**
1598 * Display the cursor element
1599 */
1600 Terminal.prototype.showCursor = function () {
1601 if (!this.cursorState) {
1602 this.cursorState = 1;
1603 this.refresh(this.y, this.y);
1604 }
1605 };
1606 /**
1607 * Scroll the terminal
1608 */
1609 Terminal.prototype.scroll = function () {
1610 var row;
1611 if (++this.ybase === this.scrollback) {
1612 this.ybase = this.ybase / 2 | 0;
1613 this.lines = this.lines.slice(-(this.ybase + this.rows) + 1);
1614 }
1615 if (!this.userScrolling) {
1616 this.ydisp = this.ybase;
1617 }
1618 // last line
1619 row = this.ybase + this.rows - 1;
1620 // subtract the bottom scroll region
1621 row -= this.rows - 1 - this.scrollBottom;
1622 if (row === this.lines.length) {
1623 // potential optimization:
1624 // pushing is faster than splicing
1625 // when they amount to the same
1626 // behavior.
1627 this.lines.push(this.blankLine());
1628 }
1629 else {
1630 // add our new line
1631 this.lines.splice(row, 0, this.blankLine());
1632 }
1633 if (this.scrollTop !== 0) {
1634 if (this.ybase !== 0) {
1635 this.ybase--;
1636 if (!this.userScrolling) {
1637 this.ydisp = this.ybase;
1638 }
1639 }
1640 this.lines.splice(this.ybase + this.scrollTop, 1);
1641 }
1642 // this.maxRange();
1643 this.updateRange(this.scrollTop);
1644 this.updateRange(this.scrollBottom);
1645 /**
1646 * This event is emitted whenever the terminal is scrolled.
1647 * The one parameter passed is the new y display position.
1648 *
1649 * @event scroll
1650 */
1651 this.emit('scroll', this.ydisp);
1652 };
1653 /**
1654 * Scroll the display of the terminal
1655 * @param {number} disp The number of lines to scroll down (negatives scroll up).
1656 * @param {boolean} suppressScrollEvent Don't emit the scroll event as scrollDisp. This is used
1657 * to avoid unwanted events being handled by the veiwport when the event was triggered from the
1658 * viewport originally.
1659 */
1660 Terminal.prototype.scrollDisp = function (disp, suppressScrollEvent) {
1661 if (disp < 0) {
1662 this.userScrolling = true;
1663 }
1664 else if (disp + this.ydisp >= this.ybase) {
1665 this.userScrolling = false;
1666 }
1667 this.ydisp += disp;
1668 if (this.ydisp > this.ybase) {
1669 this.ydisp = this.ybase;
1670 }
1671 else if (this.ydisp < 0) {
1672 this.ydisp = 0;
1673 }
1674 if (!suppressScrollEvent) {
1675 this.emit('scroll', this.ydisp);
1676 }
1677 this.refresh(0, this.rows - 1);
1678 };
1679 /**
1680 * Scroll the display of the terminal by a number of pages.
1681 * @param {number} pageCount The number of pages to scroll (negative scrolls up).
1682 */
1683 Terminal.prototype.scrollPages = function (pageCount) {
1684 this.scrollDisp(pageCount * (this.rows - 1));
1685 };
1686 /**
1687 * Scrolls the display of the terminal to the top.
1688 */
1689 Terminal.prototype.scrollToTop = function () {
1690 this.scrollDisp(-this.ydisp);
1691 };
1692 /**
1693 * Scrolls the display of the terminal to the bottom.
1694 */
1695 Terminal.prototype.scrollToBottom = function () {
1696 this.scrollDisp(this.ybase - this.ydisp);
1697 };
1698 /**
1699 * Writes text to the terminal.
1700 * @param {string} text The text to write to the terminal.
1701 */
1702 Terminal.prototype.write = function (data) {
1703 var l = data.length, i = 0, j, cs, ch, code, low, ch_width, row;
1704 this.refreshStart = this.y;
1705 this.refreshEnd = this.y;
1706 // apply leftover surrogate high from last write
1707 if (this.surrogate_high) {
1708 data = this.surrogate_high + data;
1709 this.surrogate_high = '';
1710 }
1711 for (; i < l; i++) {
1712 ch = data[i];
1713 // FIXME: higher chars than 0xa0 are not allowed in escape sequences
1714 // --> maybe move to default
1715 code = data.charCodeAt(i);
1716 if (0xD800 <= code && code <= 0xDBFF) {
1717 // we got a surrogate high
1718 // get surrogate low (next 2 bytes)
1719 low = data.charCodeAt(i + 1);
1720 if (isNaN(low)) {
1721 // end of data stream, save surrogate high
1722 this.surrogate_high = ch;
1723 continue;
1724 }
1725 code = ((code - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000;
1726 ch += data.charAt(i + 1);
1727 }
1728 // surrogate low - already handled above
1729 if (0xDC00 <= code && code <= 0xDFFF)
1730 continue;
1731 switch (this.state) {
1732 case normal:
1733 switch (ch) {
1734 case '\x07':
1735 this.bell();
1736 break;
1737 // '\n', '\v', '\f'
1738 case '\n':
1739 case '\x0b':
1740 case '\x0c':
1741 if (this.convertEol) {
1742 this.x = 0;
1743 }
1744 this.y++;
1745 if (this.y > this.scrollBottom) {
1746 this.y--;
1747 this.scroll();
1748 }
1749 break;
1750 // '\r'
1751 case '\r':
1752 this.x = 0;
1753 break;
1754 // '\b'
1755 case '\x08':
1756 if (this.x > 0) {
1757 this.x--;
1758 }
1759 break;
1760 // '\t'
1761 case '\t':
1762 this.x = this.nextStop();
1763 break;
1764 // shift out
1765 case '\x0e':
1766 this.setgLevel(1);
1767 break;
1768 // shift in
1769 case '\x0f':
1770 this.setgLevel(0);
1771 break;
1772 // '\e'
1773 case '\x1b':
1774 this.state = escaped;
1775 break;
1776 default:
1777 // ' '
1778 // calculate print space
1779 // expensive call, therefore we save width in line buffer
1780 ch_width = wcwidth(code);
1781 if (ch >= ' ') {
1782 if (this.charset && this.charset[ch]) {
1783 ch = this.charset[ch];
1784 }
1785 row = this.y + this.ybase;
1786 // insert combining char in last cell
1787 // FIXME: needs handling after cursor jumps
1788 if (!ch_width && this.x) {
1789 // dont overflow left
1790 if (this.lines[row][this.x - 1]) {
1791 if (!this.lines[row][this.x - 1][2]) {
1792 // found empty cell after fullwidth, need to go 2 cells back
1793 if (this.lines[row][this.x - 2])
1794 this.lines[row][this.x - 2][1] += ch;
1795 }
1796 else {
1797 this.lines[row][this.x - 1][1] += ch;
1798 }
1799 this.updateRange(this.y);
1800 }
1801 break;
1802 }
1803 // goto next line if ch would overflow
1804 // TODO: needs a global min terminal width of 2
1805 if (this.x + ch_width - 1 >= this.cols) {
1806 // autowrap - DECAWM
1807 if (this.wraparoundMode) {
1808 this.x = 0;
1809 this.y++;
1810 if (this.y > this.scrollBottom) {
1811 this.y--;
1812 this.scroll();
1813 }
1814 }
1815 else {
1816 this.x = this.cols - 1;
1817 if (ch_width === 2)
1818 continue;
1819 }
1820 }
1821 row = this.y + this.ybase;
1822 // insert mode: move characters to right
1823 if (this.insertMode) {
1824 // do this twice for a fullwidth char
1825 for (var moves = 0; moves < ch_width; ++moves) {
1826 // remove last cell, if it's width is 0
1827 // we have to adjust the second last cell as well
1828 var removed = this.lines[this.y + this.ybase].pop();
1829 if (removed[2] === 0
1830 && this.lines[row][this.cols - 2]
1831 && this.lines[row][this.cols - 2][2] === 2)
1832 this.lines[row][this.cols - 2] = [this.curAttr, ' ', 1];
1833 // insert empty cell at cursor
1834 this.lines[row].splice(this.x, 0, [this.curAttr, ' ', 1]);
1835 }
1836 }
1837 this.lines[row][this.x] = [this.curAttr, ch, ch_width];
1838 this.x++;
1839 this.updateRange(this.y);
1840 // fullwidth char - set next cell width to zero and advance cursor
1841 if (ch_width === 2) {
1842 this.lines[row][this.x] = [this.curAttr, '', 0];
1843 this.x++;
1844 }
1845 }
1846 break;
1847 }
1848 break;
1849 case escaped:
1850 switch (ch) {
1851 // ESC [ Control Sequence Introducer ( CSI is 0x9b).
1852 case '[':
1853 this.params = [];
1854 this.currentParam = 0;
1855 this.state = csi;
1856 break;
1857 // ESC ] Operating System Command ( OSC is 0x9d).
1858 case ']':
1859 this.params = [];
1860 this.currentParam = 0;
1861 this.state = osc;
1862 break;
1863 // ESC P Device Control String ( DCS is 0x90).
1864 case 'P':
1865 this.params = [];
1866 this.currentParam = 0;
1867 this.state = dcs;
1868 break;
1869 // ESC _ Application Program Command ( APC is 0x9f).
1870 case '_':
1871 this.state = ignore;
1872 break;
1873 // ESC ^ Privacy Message ( PM is 0x9e).
1874 case '^':
1875 this.state = ignore;
1876 break;
1877 // ESC c Full Reset (RIS).
1878 case 'c':
1879 this.reset();
1880 break;
1881 // ESC E Next Line ( NEL is 0x85).
1882 // ESC D Index ( IND is 0x84).
1883 case 'E':
1884 this.x = 0;
1885 ;
1886 case 'D':
1887 this.index();
1888 break;
1889 // ESC M Reverse Index ( RI is 0x8d).
1890 case 'M':
1891 this.reverseIndex();
1892 break;
1893 // ESC % Select default/utf-8 character set.
1894 // @ = default, G = utf-8
1895 case '%':
1896 //this.charset = null;
1897 this.setgLevel(0);
1898 this.setgCharset(0, Terminal.charsets.US);
1899 this.state = normal;
1900 i++;
1901 break;
1902 // ESC (,),*,+,-,. Designate G0-G2 Character Set.
1903 case '(': // <-- this seems to get all the attention
1904 case ')':
1905 case '*':
1906 case '+':
1907 case '-':
1908 case '.':
1909 switch (ch) {
1910 case '(':
1911 this.gcharset = 0;
1912 break;
1913 case ')':
1914 this.gcharset = 1;
1915 break;
1916 case '*':
1917 this.gcharset = 2;
1918 break;
1919 case '+':
1920 this.gcharset = 3;
1921 break;
1922 case '-':
1923 this.gcharset = 1;
1924 break;
1925 case '.':
1926 this.gcharset = 2;
1927 break;
1928 }
1929 this.state = charset;
1930 break;
1931 // Designate G3 Character Set (VT300).
1932 // A = ISO Latin-1 Supplemental.
1933 // Not implemented.
1934 case '/':
1935 this.gcharset = 3;
1936 this.state = charset;
1937 i--;
1938 break;
1939 // ESC N
1940 // Single Shift Select of G2 Character Set
1941 // ( SS2 is 0x8e). This affects next character only.
1942 case 'N':
1943 break;
1944 // ESC O
1945 // Single Shift Select of G3 Character Set
1946 // ( SS3 is 0x8f). This affects next character only.
1947 case 'O':
1948 break;
1949 // ESC n
1950 // Invoke the G2 Character Set as GL (LS2).
1951 case 'n':
1952 this.setgLevel(2);
1953 break;
1954 // ESC o
1955 // Invoke the G3 Character Set as GL (LS3).
1956 case 'o':
1957 this.setgLevel(3);
1958 break;
1959 // ESC |
1960 // Invoke the G3 Character Set as GR (LS3R).
1961 case '|':
1962 this.setgLevel(3);
1963 break;
1964 // ESC }
1965 // Invoke the G2 Character Set as GR (LS2R).
1966 case '}':
1967 this.setgLevel(2);
1968 break;
1969 // ESC ~
1970 // Invoke the G1 Character Set as GR (LS1R).
1971 case '~':
1972 this.setgLevel(1);
1973 break;
1974 // ESC 7 Save Cursor (DECSC).
1975 case '7':
1976 this.saveCursor();
1977 this.state = normal;
1978 break;
1979 // ESC 8 Restore Cursor (DECRC).
1980 case '8':
1981 this.restoreCursor();
1982 this.state = normal;
1983 break;
1984 // ESC # 3 DEC line height/width
1985 case '#':
1986 this.state = normal;
1987 i++;
1988 break;
1989 // ESC H Tab Set (HTS is 0x88).
1990 case 'H':
1991 this.tabSet();
1992 break;
1993 // ESC = Application Keypad (DECKPAM).
1994 case '=':
1995 this.log('Serial port requested application keypad.');
1996 this.applicationKeypad = true;
1997 this.viewport.syncScrollArea();
1998 this.state = normal;
1999 break;
2000 // ESC > Normal Keypad (DECKPNM).
2001 case '>':
2002 this.log('Switching back to normal keypad.');
2003 this.applicationKeypad = false;
2004 this.viewport.syncScrollArea();
2005 this.state = normal;
2006 break;
2007 default:
2008 this.state = normal;
2009 this.error('Unknown ESC control: %s.', ch);
2010 break;
2011 }
2012 break;
2013 case charset:
2014 switch (ch) {
2015 case '0':
2016 cs = Terminal.charsets.SCLD;
2017 break;
2018 case 'A':
2019 cs = Terminal.charsets.UK;
2020 break;
2021 case 'B':
2022 cs = Terminal.charsets.US;
2023 break;
2024 case '4':
2025 cs = Terminal.charsets.Dutch;
2026 break;
2027 case 'C': // Finnish
2028 case '5':
2029 cs = Terminal.charsets.Finnish;
2030 break;
2031 case 'R':
2032 cs = Terminal.charsets.French;
2033 break;
2034 case 'Q':
2035 cs = Terminal.charsets.FrenchCanadian;
2036 break;
2037 case 'K':
2038 cs = Terminal.charsets.German;
2039 break;
2040 case 'Y':
2041 cs = Terminal.charsets.Italian;
2042 break;
2043 case 'E': // NorwegianDanish
2044 case '6':
2045 cs = Terminal.charsets.NorwegianDanish;
2046 break;
2047 case 'Z':
2048 cs = Terminal.charsets.Spanish;
2049 break;
2050 case 'H': // Swedish
2051 case '7':
2052 cs = Terminal.charsets.Swedish;
2053 break;
2054 case '=':
2055 cs = Terminal.charsets.Swiss;
2056 break;
2057 case '/':
2058 cs = Terminal.charsets.ISOLatin;
2059 i++;
2060 break;
2061 default:
2062 cs = Terminal.charsets.US;
2063 break;
2064 }
2065 this.setgCharset(this.gcharset, cs);
2066 this.gcharset = null;
2067 this.state = normal;
2068 break;
2069 case osc:
2070 // OSC Ps ; Pt ST
2071 // OSC Ps ; Pt BEL
2072 // Set Text Parameters.
2073 if (ch === '\x1b' || ch === '\x07') {
2074 if (ch === '\x1b')
2075 i++;
2076 this.params.push(this.currentParam);
2077 switch (this.params[0]) {
2078 case 0:
2079 case 1:
2080 case 2:
2081 if (this.params[1]) {
2082 this.title = this.params[1];
2083 this.handleTitle(this.title);
2084 }
2085 break;
2086 case 3:
2087 // set X property
2088 break;
2089 case 4:
2090 case 5:
2091 // change dynamic colors
2092 break;
2093 case 10:
2094 case 11:
2095 case 12:
2096 case 13:
2097 case 14:
2098 case 15:
2099 case 16:
2100 case 17:
2101 case 18:
2102 case 19:
2103 // change dynamic ui colors
2104 break;
2105 case 46:
2106 // change log file
2107 break;
2108 case 50:
2109 // dynamic font
2110 break;
2111 case 51:
2112 // emacs shell
2113 break;
2114 case 52:
2115 // manipulate selection data
2116 break;
2117 case 104:
2118 case 105:
2119 case 110:
2120 case 111:
2121 case 112:
2122 case 113:
2123 case 114:
2124 case 115:
2125 case 116:
2126 case 117:
2127 case 118:
2128 // reset colors
2129 break;
2130 }
2131 this.params = [];
2132 this.currentParam = 0;
2133 this.state = normal;
2134 }
2135 else {
2136 if (!this.params.length) {
2137 if (ch >= '0' && ch <= '9') {
2138 this.currentParam =
2139 this.currentParam * 10 + ch.charCodeAt(0) - 48;
2140 }
2141 else if (ch === ';') {
2142 this.params.push(this.currentParam);
2143 this.currentParam = '';
2144 }
2145 }
2146 else {
2147 this.currentParam += ch;
2148 }
2149 }
2150 break;
2151 case csi:
2152 // '?', '>', '!'
2153 if (ch === '?' || ch === '>' || ch === '!') {
2154 this.prefix = ch;
2155 break;
2156 }
2157 // 0 - 9
2158 if (ch >= '0' && ch <= '9') {
2159 this.currentParam = this.currentParam * 10 + ch.charCodeAt(0) - 48;
2160 break;
2161 }
2162 // '$', '"', ' ', '\''
2163 if (ch === '$' || ch === '"' || ch === ' ' || ch === '\'') {
2164 this.postfix = ch;
2165 break;
2166 }
2167 this.params.push(this.currentParam);
2168 this.currentParam = 0;
2169 // ';'
2170 if (ch === ';')
2171 break;
2172 this.state = normal;
2173 switch (ch) {
2174 // CSI Ps A
2175 // Cursor Up Ps Times (default = 1) (CUU).
2176 case 'A':
2177 this.cursorUp(this.params);
2178 break;
2179 // CSI Ps B
2180 // Cursor Down Ps Times (default = 1) (CUD).
2181 case 'B':
2182 this.cursorDown(this.params);
2183 break;
2184 // CSI Ps C
2185 // Cursor Forward Ps Times (default = 1) (CUF).
2186 case 'C':
2187 this.cursorForward(this.params);
2188 break;
2189 // CSI Ps D
2190 // Cursor Backward Ps Times (default = 1) (CUB).
2191 case 'D':
2192 this.cursorBackward(this.params);
2193 break;
2194 // CSI Ps ; Ps H
2195 // Cursor Position [row;column] (default = [1,1]) (CUP).
2196 case 'H':
2197 this.cursorPos(this.params);
2198 break;
2199 // CSI Ps J Erase in Display (ED).
2200 case 'J':
2201 this.eraseInDisplay(this.params);
2202 break;
2203 // CSI Ps K Erase in Line (EL).
2204 case 'K':
2205 this.eraseInLine(this.params);
2206 break;
2207 // CSI Pm m Character Attributes (SGR).
2208 case 'm':
2209 if (!this.prefix) {
2210 this.charAttributes(this.params);
2211 }
2212 break;
2213 // CSI Ps n Device Status Report (DSR).
2214 case 'n':
2215 if (!this.prefix) {
2216 this.deviceStatus(this.params);
2217 }
2218 break;
2219 /**
2220 * Additions
2221 */
2222 // CSI Ps @
2223 // Insert Ps (Blank) Character(s) (default = 1) (ICH).
2224 case '@':
2225 this.insertChars(this.params);
2226 break;
2227 // CSI Ps E
2228 // Cursor Next Line Ps Times (default = 1) (CNL).
2229 case 'E':
2230 this.cursorNextLine(this.params);
2231 break;
2232 // CSI Ps F
2233 // Cursor Preceding Line Ps Times (default = 1) (CNL).
2234 case 'F':
2235 this.cursorPrecedingLine(this.params);
2236 break;
2237 // CSI Ps G
2238 // Cursor Character Absolute [column] (default = [row,1]) (CHA).
2239 case 'G':
2240 this.cursorCharAbsolute(this.params);
2241 break;
2242 // CSI Ps L
2243 // Insert Ps Line(s) (default = 1) (IL).
2244 case 'L':
2245 this.insertLines(this.params);
2246 break;
2247 // CSI Ps M
2248 // Delete Ps Line(s) (default = 1) (DL).
2249 case 'M':
2250 this.deleteLines(this.params);
2251 break;
2252 // CSI Ps P
2253 // Delete Ps Character(s) (default = 1) (DCH).
2254 case 'P':
2255 this.deleteChars(this.params);
2256 break;
2257 // CSI Ps X
2258 // Erase Ps Character(s) (default = 1) (ECH).
2259 case 'X':
2260 this.eraseChars(this.params);
2261 break;
2262 // CSI Pm ` Character Position Absolute
2263 // [column] (default = [row,1]) (HPA).
2264 case '`':
2265 this.charPosAbsolute(this.params);
2266 break;
2267 // 141 61 a * HPR -
2268 // Horizontal Position Relative
2269 case 'a':
2270 this.HPositionRelative(this.params);
2271 break;
2272 // CSI P s c
2273 // Send Device Attributes (Primary DA).
2274 // CSI > P s c
2275 // Send Device Attributes (Secondary DA)
2276 case 'c':
2277 this.sendDeviceAttributes(this.params);
2278 break;
2279 // CSI Pm d
2280 // Line Position Absolute [row] (default = [1,column]) (VPA).
2281 case 'd':
2282 this.linePosAbsolute(this.params);
2283 break;
2284 // 145 65 e * VPR - Vertical Position Relative
2285 case 'e':
2286 this.VPositionRelative(this.params);
2287 break;
2288 // CSI Ps ; Ps f
2289 // Horizontal and Vertical Position [row;column] (default =
2290 // [1,1]) (HVP).
2291 case 'f':
2292 this.HVPosition(this.params);
2293 break;
2294 // CSI Pm h Set Mode (SM).
2295 // CSI ? Pm h - mouse escape codes, cursor escape codes
2296 case 'h':
2297 this.setMode(this.params);
2298 break;
2299 // CSI Pm l Reset Mode (RM).
2300 // CSI ? Pm l
2301 case 'l':
2302 this.resetMode(this.params);
2303 break;
2304 // CSI Ps ; Ps r
2305 // Set Scrolling Region [top;bottom] (default = full size of win-
2306 // dow) (DECSTBM).
2307 // CSI ? Pm r
2308 case 'r':
2309 this.setScrollRegion(this.params);
2310 break;
2311 // CSI s
2312 // Save cursor (ANSI.SYS).
2313 case 's':
2314 this.saveCursor(this.params);
2315 break;
2316 // CSI u
2317 // Restore cursor (ANSI.SYS).
2318 case 'u':
2319 this.restoreCursor(this.params);
2320 break;
2321 /**
2322 * Lesser Used
2323 */
2324 // CSI Ps I
2325 // Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
2326 case 'I':
2327 this.cursorForwardTab(this.params);
2328 break;
2329 // CSI Ps S Scroll up Ps lines (default = 1) (SU).
2330 case 'S':
2331 this.scrollUp(this.params);
2332 break;
2333 // CSI Ps T Scroll down Ps lines (default = 1) (SD).
2334 // CSI Ps ; Ps ; Ps ; Ps ; Ps T
2335 // CSI > Ps; Ps T
2336 case 'T':
2337 // if (this.prefix === '>') {
2338 // this.resetTitleModes(this.params);
2339 // break;
2340 // }
2341 // if (this.params.length > 2) {
2342 // this.initMouseTracking(this.params);
2343 // break;
2344 // }
2345 if (this.params.length < 2 && !this.prefix) {
2346 this.scrollDown(this.params);
2347 }
2348 break;
2349 // CSI Ps Z
2350 // Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
2351 case 'Z':
2352 this.cursorBackwardTab(this.params);
2353 break;
2354 // CSI Ps b Repeat the preceding graphic character Ps times (REP).
2355 case 'b':
2356 this.repeatPrecedingCharacter(this.params);
2357 break;
2358 // CSI Ps g Tab Clear (TBC).
2359 case 'g':
2360 this.tabClear(this.params);
2361 break;
2362 // CSI Pm i Media Copy (MC).
2363 // CSI ? Pm i
2364 // case 'i':
2365 // this.mediaCopy(this.params);
2366 // break;
2367 // CSI Pm m Character Attributes (SGR).
2368 // CSI > Ps; Ps m
2369 // case 'm': // duplicate
2370 // if (this.prefix === '>') {
2371 // this.setResources(this.params);
2372 // } else {
2373 // this.charAttributes(this.params);
2374 // }
2375 // break;
2376 // CSI Ps n Device Status Report (DSR).
2377 // CSI > Ps n
2378 // case 'n': // duplicate
2379 // if (this.prefix === '>') {
2380 // this.disableModifiers(this.params);
2381 // } else {
2382 // this.deviceStatus(this.params);
2383 // }
2384 // break;
2385 // CSI > Ps p Set pointer mode.
2386 // CSI ! p Soft terminal reset (DECSTR).
2387 // CSI Ps$ p
2388 // Request ANSI mode (DECRQM).
2389 // CSI ? Ps$ p
2390 // Request DEC private mode (DECRQM).
2391 // CSI Ps ; Ps " p
2392 case 'p':
2393 switch (this.prefix) {
2394 // case '>':
2395 // this.setPointerMode(this.params);
2396 // break;
2397 case '!':
2398 this.softReset(this.params);
2399 break;
2400 }
2401 break;
2402 // CSI Ps q Load LEDs (DECLL).
2403 // CSI Ps SP q
2404 // CSI Ps " q
2405 // case 'q':
2406 // if (this.postfix === ' ') {
2407 // this.setCursorStyle(this.params);
2408 // break;
2409 // }
2410 // if (this.postfix === '"') {
2411 // this.setCharProtectionAttr(this.params);
2412 // break;
2413 // }
2414 // this.loadLEDs(this.params);
2415 // break;
2416 // CSI Ps ; Ps r
2417 // Set Scrolling Region [top;bottom] (default = full size of win-
2418 // dow) (DECSTBM).
2419 // CSI ? Pm r
2420 // CSI Pt; Pl; Pb; Pr; Ps$ r
2421 // case 'r': // duplicate
2422 // if (this.prefix === '?') {
2423 // this.restorePrivateValues(this.params);
2424 // } else if (this.postfix === '$') {
2425 // this.setAttrInRectangle(this.params);
2426 // } else {
2427 // this.setScrollRegion(this.params);
2428 // }
2429 // break;
2430 // CSI s Save cursor (ANSI.SYS).
2431 // CSI ? Pm s
2432 // case 's': // duplicate
2433 // if (this.prefix === '?') {
2434 // this.savePrivateValues(this.params);
2435 // } else {
2436 // this.saveCursor(this.params);
2437 // }
2438 // break;
2439 // CSI Ps ; Ps ; Ps t
2440 // CSI Pt; Pl; Pb; Pr; Ps$ t
2441 // CSI > Ps; Ps t
2442 // CSI Ps SP t
2443 // case 't':
2444 // if (this.postfix === '$') {
2445 // this.reverseAttrInRectangle(this.params);
2446 // } else if (this.postfix === ' ') {
2447 // this.setWarningBellVolume(this.params);
2448 // } else {
2449 // if (this.prefix === '>') {
2450 // this.setTitleModeFeature(this.params);
2451 // } else {
2452 // this.manipulateWindow(this.params);
2453 // }
2454 // }
2455 // break;
2456 // CSI u Restore cursor (ANSI.SYS).
2457 // CSI Ps SP u
2458 // case 'u': // duplicate
2459 // if (this.postfix === ' ') {
2460 // this.setMarginBellVolume(this.params);
2461 // } else {
2462 // this.restoreCursor(this.params);
2463 // }
2464 // break;
2465 // CSI Pt; Pl; Pb; Pr; Pp; Pt; Pl; Pp$ v
2466 // case 'v':
2467 // if (this.postfix === '$') {
2468 // this.copyRectagle(this.params);
2469 // }
2470 // break;
2471 // CSI Pt ; Pl ; Pb ; Pr ' w
2472 // case 'w':
2473 // if (this.postfix === '\'') {
2474 // this.enableFilterRectangle(this.params);
2475 // }
2476 // break;
2477 // CSI Ps x Request Terminal Parameters (DECREQTPARM).
2478 // CSI Ps x Select Attribute Change Extent (DECSACE).
2479 // CSI Pc; Pt; Pl; Pb; Pr$ x
2480 // case 'x':
2481 // if (this.postfix === '$') {
2482 // this.fillRectangle(this.params);
2483 // } else {
2484 // this.requestParameters(this.params);
2485 // //this.__(this.params);
2486 // }
2487 // break;
2488 // CSI Ps ; Pu ' z
2489 // CSI Pt; Pl; Pb; Pr$ z
2490 // case 'z':
2491 // if (this.postfix === '\'') {
2492 // this.enableLocatorReporting(this.params);
2493 // } else if (this.postfix === '$') {
2494 // this.eraseRectangle(this.params);
2495 // }
2496 // break;
2497 // CSI Pm ' {
2498 // CSI Pt; Pl; Pb; Pr$ {
2499 // case '{':
2500 // if (this.postfix === '\'') {
2501 // this.setLocatorEvents(this.params);
2502 // } else if (this.postfix === '$') {
2503 // this.selectiveEraseRectangle(this.params);
2504 // }
2505 // break;
2506 // CSI Ps ' |
2507 // case '|':
2508 // if (this.postfix === '\'') {
2509 // this.requestLocatorPosition(this.params);
2510 // }
2511 // break;
2512 // CSI P m SP }
2513 // Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
2514 // case '}':
2515 // if (this.postfix === ' ') {
2516 // this.insertColumns(this.params);
2517 // }
2518 // break;
2519 // CSI P m SP ~
2520 // Delete P s Column(s) (default = 1) (DECDC), VT420 and up
2521 // case '~':
2522 // if (this.postfix === ' ') {
2523 // this.deleteColumns(this.params);
2524 // }
2525 // break;
2526 default:
2527 this.error('Unknown CSI code: %s.', ch);
2528 break;
2529 }
2530 this.prefix = '';
2531 this.postfix = '';
2532 break;
2533 case dcs:
2534 if (ch === '\x1b' || ch === '\x07') {
2535 if (ch === '\x1b')
2536 i++;
2537 switch (this.prefix) {
2538 // User-Defined Keys (DECUDK).
2539 case '':
2540 break;
2541 // Request Status String (DECRQSS).
2542 // test: echo -e '\eP$q"p\e\\'
2543 case '$q':
2544 var pt = this.currentParam, valid = false;
2545 switch (pt) {
2546 // DECSCA
2547 case '"q':
2548 pt = '0"q';
2549 break;
2550 // DECSCL
2551 case '"p':
2552 pt = '61"p';
2553 break;
2554 // DECSTBM
2555 case 'r':
2556 pt = ''
2557 + (this.scrollTop + 1)
2558 + ';'
2559 + (this.scrollBottom + 1)
2560 + 'r';
2561 break;
2562 // SGR
2563 case 'm':
2564 pt = '0m';
2565 break;
2566 default:
2567 this.error('Unknown DCS Pt: %s.', pt);
2568 pt = '';
2569 break;
2570 }
2571 this.send('\x1bP' + +valid + '$r' + pt + '\x1b\\');
2572 break;
2573 // Set Termcap/Terminfo Data (xterm, experimental).
2574 case '+p':
2575 break;
2576 // Request Termcap/Terminfo String (xterm, experimental)
2577 // Regular xterm does not even respond to this sequence.
2578 // This can cause a small glitch in vim.
2579 // test: echo -ne '\eP+q6b64\e\\'
2580 case '+q':
2581 var pt = this.currentParam, valid = false;
2582 this.send('\x1bP' + +valid + '+r' + pt + '\x1b\\');
2583 break;
2584 default:
2585 this.error('Unknown DCS prefix: %s.', this.prefix);
2586 break;
2587 }
2588 this.currentParam = 0;
2589 this.prefix = '';
2590 this.state = normal;
2591 }
2592 else if (!this.currentParam) {
2593 if (!this.prefix && ch !== '$' && ch !== '+') {
2594 this.currentParam = ch;
2595 }
2596 else if (this.prefix.length === 2) {
2597 this.currentParam = ch;
2598 }
2599 else {
2600 this.prefix += ch;
2601 }
2602 }
2603 else {
2604 this.currentParam += ch;
2605 }
2606 break;
2607 case ignore:
2608 // For PM and APC.
2609 if (ch === '\x1b' || ch === '\x07') {
2610 if (ch === '\x1b')
2611 i++;
2612 this.state = normal;
2613 }
2614 break;
2615 }
2616 }
2617 this.updateRange(this.y);
2618 this.refresh(this.refreshStart, this.refreshEnd);
2619 };
2620 /**
2621 * Writes text to the terminal, followed by a break line character (\n).
2622 * @param {string} text The text to write to the terminal.
2623 */
2624 Terminal.prototype.writeln = function (data) {
2625 this.write(data + '\r\n');
2626 };
2627 /**
2628 * Attaches a custom keydown handler which is run before keys are processed, giving consumers of
2629 * xterm.js ultimate control as to what keys should be processed by the terminal and what keys
2630 * should not.
2631 * @param {function} customKeydownHandler The custom KeyboardEvent handler to attach. This is a
2632 * function that takes a KeyboardEvent, allowing consumers to stop propogation and/or prevent
2633 * the default action. The function returns whether the event should be processed by xterm.js.
2634 */
2635 Terminal.prototype.attachCustomKeydownHandler = function (customKeydownHandler) {
2636 this.customKeydownHandler = customKeydownHandler;
2637 };
2638 /**
2639 * Handle a keydown event
2640 * Key Resources:
2641 * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
2642 * @param {KeyboardEvent} ev The keydown event to be handled.
2643 */
2644 Terminal.prototype.keyDown = function (ev) {
2645 if (this.customKeydownHandler && this.customKeydownHandler(ev) === false) {
2646 return false;
2647 }
2648 if (!this.compositionHelper.keydown.bind(this.compositionHelper)(ev)) {
2649 if (this.ybase !== this.ydisp) {
2650 this.scrollToBottom();
2651 }
2652 return false;
2653 }
2654 var self = this;
2655 var result = this.evaluateKeyEscapeSequence(ev);
2656 if (result.scrollDisp) {
2657 this.scrollDisp(result.scrollDisp);
2658 return this.cancel(ev, true);
2659 }
2660 if (isThirdLevelShift(this, ev)) {
2661 return true;
2662 }
2663 if (result.cancel) {
2664 // The event is canceled at the end already, is this necessary?
2665 this.cancel(ev, true);
2666 }
2667 if (!result.key) {
2668 return true;
2669 }
2670 this.emit('keydown', ev);
2671 this.emit('key', result.key, ev);
2672 this.showCursor();
2673 this.handler(result.key);
2674 return this.cancel(ev, true);
2675 };
2676 /**
2677 * Returns an object that determines how a KeyboardEvent should be handled. The key of the
2678 * returned value is the new key code to pass to the PTY.
2679 *
2680 * Reference: http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
2681 * @param {KeyboardEvent} ev The keyboard event to be translated to key escape sequence.
2682 */
2683 Terminal.prototype.evaluateKeyEscapeSequence = function (ev) {
2684 var result = {
2685 // Whether to cancel event propogation (NOTE: this may not be needed since the event is
2686 // canceled at the end of keyDown
2687 cancel: false,
2688 // The new key even to emit
2689 key: undefined,
2690 // The number of characters to scroll, if this is defined it will cancel the event
2691 scrollDisp: undefined
2692 };
2693 var modifiers = ev.shiftKey << 0 | ev.altKey << 1 | ev.ctrlKey << 2 | ev.metaKey << 3;
2694 switch (ev.keyCode) {
2695 case 8:
2696 // backspace
2697 if (ev.shiftKey) {
2698 result.key = '\x08'; // ^H
2699 break;
2700 }
2701 result.key = '\x7f'; // ^?
2702 break;
2703 case 9:
2704 // tab
2705 if (ev.shiftKey) {
2706 result.key = '\x1b[Z';
2707 break;
2708 }
2709 result.key = '\t';
2710 result.cancel = true;
2711 break;
2712 case 13:
2713 // return/enter
2714 result.key = '\r';
2715 result.cancel = true;
2716 break;
2717 case 27:
2718 // escape
2719 result.key = '\x1b';
2720 result.cancel = true;
2721 break;
2722 case 37:
2723 // left-arrow
2724 if (modifiers) {
2725 result.key = '\x1b[1;' + (modifiers + 1) + 'D';
2726 // HACK: Make Alt + left-arrow behave like Ctrl + left-arrow: move one word backwards
2727 // http://unix.stackexchange.com/a/108106
2728 if (result.key == '\x1b[1;3D') {
2729 result.key = '\x1b[1;5D';
2730 }
2731 }
2732 else if (this.applicationCursor) {
2733 result.key = '\x1bOD';
2734 }
2735 else {
2736 result.key = '\x1b[D';
2737 }
2738 break;
2739 case 39:
2740 // right-arrow
2741 if (modifiers) {
2742 result.key = '\x1b[1;' + (modifiers + 1) + 'C';
2743 // HACK: Make Alt + right-arrow behave like Ctrl + right-arrow: move one word forward
2744 // http://unix.stackexchange.com/a/108106
2745 if (result.key == '\x1b[1;3C') {
2746 result.key = '\x1b[1;5C';
2747 }
2748 }
2749 else if (this.applicationCursor) {
2750 result.key = '\x1bOC';
2751 }
2752 else {
2753 result.key = '\x1b[C';
2754 }
2755 break;
2756 case 38:
2757 // up-arrow
2758 if (modifiers) {
2759 result.key = '\x1b[1;' + (modifiers + 1) + 'A';
2760 // HACK: Make Alt + up-arrow behave like Ctrl + up-arrow
2761 // http://unix.stackexchange.com/a/108106
2762 if (result.key == '\x1b[1;3A') {
2763 result.key = '\x1b[1;5A';
2764 }
2765 }
2766 else if (this.applicationCursor) {
2767 result.key = '\x1bOA';
2768 }
2769 else {
2770 result.key = '\x1b[A';
2771 }
2772 break;
2773 case 40:
2774 // down-arrow
2775 if (modifiers) {
2776 result.key = '\x1b[1;' + (modifiers + 1) + 'B';
2777 // HACK: Make Alt + down-arrow behave like Ctrl + down-arrow
2778 // http://unix.stackexchange.com/a/108106
2779 if (result.key == '\x1b[1;3B') {
2780 result.key = '\x1b[1;5B';
2781 }
2782 }
2783 else if (this.applicationCursor) {
2784 result.key = '\x1bOB';
2785 }
2786 else {
2787 result.key = '\x1b[B';
2788 }
2789 break;
2790 case 45:
2791 // insert
2792 if (!ev.shiftKey && !ev.ctrlKey) {
2793 // <Ctrl> or <Shift> + <Insert> are used to
2794 // copy-paste on some systems.
2795 result.key = '\x1b[2~';
2796 }
2797 break;
2798 case 46:
2799 // delete
2800 if (modifiers) {
2801 result.key = '\x1b[3;' + (modifiers + 1) + '~';
2802 }
2803 else {
2804 result.key = '\x1b[3~';
2805 }
2806 break;
2807 case 36:
2808 // home
2809 if (modifiers)
2810 result.key = '\x1b[1;' + (modifiers + 1) + 'H';
2811 else if (this.applicationCursor)
2812 result.key = '\x1bOH';
2813 else
2814 result.key = '\x1b[H';
2815 break;
2816 case 35:
2817 // end
2818 if (modifiers)
2819 result.key = '\x1b[1;' + (modifiers + 1) + 'F';
2820 else if (this.applicationCursor)
2821 result.key = '\x1bOF';
2822 else
2823 result.key = '\x1b[F';
2824 break;
2825 case 33:
2826 // page up
2827 if (ev.shiftKey) {
2828 result.scrollDisp = -(this.rows - 1);
2829 }
2830 else {
2831 result.key = '\x1b[5~';
2832 }
2833 break;
2834 case 34:
2835 // page down
2836 if (ev.shiftKey) {
2837 result.scrollDisp = this.rows - 1;
2838 }
2839 else {
2840 result.key = '\x1b[6~';
2841 }
2842 break;
2843 case 112:
2844 // F1-F12
2845 if (modifiers) {
2846 result.key = '\x1b[1;' + (modifiers + 1) + 'P';
2847 }
2848 else {
2849 result.key = '\x1bOP';
2850 }
2851 break;
2852 case 113:
2853 if (modifiers) {
2854 result.key = '\x1b[1;' + (modifiers + 1) + 'Q';
2855 }
2856 else {
2857 result.key = '\x1bOQ';
2858 }
2859 break;
2860 case 114:
2861 if (modifiers) {
2862 result.key = '\x1b[1;' + (modifiers + 1) + 'R';
2863 }
2864 else {
2865 result.key = '\x1bOR';
2866 }
2867 break;
2868 case 115:
2869 if (modifiers) {
2870 result.key = '\x1b[1;' + (modifiers + 1) + 'S';
2871 }
2872 else {
2873 result.key = '\x1bOS';
2874 }
2875 break;
2876 case 116:
2877 if (modifiers) {
2878 result.key = '\x1b[15;' + (modifiers + 1) + '~';
2879 }
2880 else {
2881 result.key = '\x1b[15~';
2882 }
2883 break;
2884 case 117:
2885 if (modifiers) {
2886 result.key = '\x1b[17;' + (modifiers + 1) + '~';
2887 }
2888 else {
2889 result.key = '\x1b[17~';
2890 }
2891 break;
2892 case 118:
2893 if (modifiers) {
2894 result.key = '\x1b[18;' + (modifiers + 1) + '~';
2895 }
2896 else {
2897 result.key = '\x1b[18~';
2898 }
2899 break;
2900 case 119:
2901 if (modifiers) {
2902 result.key = '\x1b[19;' + (modifiers + 1) + '~';
2903 }
2904 else {
2905 result.key = '\x1b[19~';
2906 }
2907 break;
2908 case 120:
2909 if (modifiers) {
2910 result.key = '\x1b[20;' + (modifiers + 1) + '~';
2911 }
2912 else {
2913 result.key = '\x1b[20~';
2914 }
2915 break;
2916 case 121:
2917 if (modifiers) {
2918 result.key = '\x1b[21;' + (modifiers + 1) + '~';
2919 }
2920 else {
2921 result.key = '\x1b[21~';
2922 }
2923 break;
2924 case 122:
2925 if (modifiers) {
2926 result.key = '\x1b[23;' + (modifiers + 1) + '~';
2927 }
2928 else {
2929 result.key = '\x1b[23~';
2930 }
2931 break;
2932 case 123:
2933 if (modifiers) {
2934 result.key = '\x1b[24;' + (modifiers + 1) + '~';
2935 }
2936 else {
2937 result.key = '\x1b[24~';
2938 }
2939 break;
2940 default:
2941 // a-z and space
2942 if (ev.ctrlKey && !ev.shiftKey && !ev.altKey && !ev.metaKey) {
2943 if (ev.keyCode >= 65 && ev.keyCode <= 90) {
2944 result.key = String.fromCharCode(ev.keyCode - 64);
2945 }
2946 else if (ev.keyCode === 32) {
2947 // NUL
2948 result.key = String.fromCharCode(0);
2949 }
2950 else if (ev.keyCode >= 51 && ev.keyCode <= 55) {
2951 // escape, file sep, group sep, record sep, unit sep
2952 result.key = String.fromCharCode(ev.keyCode - 51 + 27);
2953 }
2954 else if (ev.keyCode === 56) {
2955 // delete
2956 result.key = String.fromCharCode(127);
2957 }
2958 else if (ev.keyCode === 219) {
2959 // ^[ - Control Sequence Introducer (CSI)
2960 result.key = String.fromCharCode(27);
2961 }
2962 else if (ev.keyCode === 220) {
2963 // ^\ - String Terminator (ST)
2964 result.key = String.fromCharCode(28);
2965 }
2966 else if (ev.keyCode === 221) {
2967 // ^] - Operating System Command (OSC)
2968 result.key = String.fromCharCode(29);
2969 }
2970 }
2971 else if (!this.browser.isMac && ev.altKey && !ev.ctrlKey && !ev.metaKey) {
2972 // On Mac this is a third level shift. Use <Esc> instead.
2973 if (ev.keyCode >= 65 && ev.keyCode <= 90) {
2974 result.key = '\x1b' + String.fromCharCode(ev.keyCode + 32);
2975 }
2976 else if (ev.keyCode === 192) {
2977 result.key = '\x1b`';
2978 }
2979 else if (ev.keyCode >= 48 && ev.keyCode <= 57) {
2980 result.key = '\x1b' + (ev.keyCode - 48);
2981 }
2982 }
2983 break;
2984 }
2985 return result;
2986 };
2987 /**
2988 * Set the G level of the terminal
2989 * @param g
2990 */
2991 Terminal.prototype.setgLevel = function (g) {
2992 this.glevel = g;
2993 this.charset = this.charsets[g];
2994 };
2995 /**
2996 * Set the charset for the given G level of the terminal
2997 * @param g
2998 * @param charset
2999 */
3000 Terminal.prototype.setgCharset = function (g, charset) {
3001 this.charsets[g] = charset;
3002 if (this.glevel === g) {
3003 this.charset = charset;
3004 }
3005 };
3006 /**
3007 * Handle a keypress event.
3008 * Key Resources:
3009 * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
3010 * @param {KeyboardEvent} ev The keypress event to be handled.
3011 */
3012 Terminal.prototype.keyPress = function (ev) {
3013 var key;
3014 this.cancel(ev);
3015 if (ev.charCode) {
3016 key = ev.charCode;
3017 }
3018 else if (ev.which == null) {
3019 key = ev.keyCode;
3020 }
3021 else if (ev.which !== 0 && ev.charCode !== 0) {
3022 key = ev.which;
3023 }
3024 else {
3025 return false;
3026 }
3027 if (!key || ((ev.altKey || ev.ctrlKey || ev.metaKey) && !isThirdLevelShift(this, ev))) {
3028 return false;
3029 }
3030 key = String.fromCharCode(key);
3031 this.emit('keypress', key, ev);
3032 this.emit('key', key, ev);
3033 this.showCursor();
3034 this.handler(key);
3035 return false;
3036 };
3037 /**
3038 * Send data for handling to the terminal
3039 * @param {string} data
3040 */
3041 Terminal.prototype.send = function (data) {
3042 var self = this;
3043 if (!this.queue) {
3044 setTimeout(function () {
3045 self.handler(self.queue);
3046 self.queue = '';
3047 }, 1);
3048 }
3049 this.queue += data;
3050 };
3051 /**
3052 * Ring the bell.
3053 * Note: We could do sweet things with webaudio here
3054 */
3055 Terminal.prototype.bell = function () {
3056 if (!this.visualBell)
3057 return;
3058 var self = this;
3059 this.element.style.borderColor = 'white';
3060 setTimeout(function () {
3061 self.element.style.borderColor = '';
3062 }, 10);
3063 if (this.popOnBell)
3064 this.focus();
3065 };
3066 /**
3067 * Log the current state to the console.
3068 */
3069 Terminal.prototype.log = function () {
3070 if (!this.debug)
3071 return;
3072 if (!this.context.console || !this.context.console.log)
3073 return;
3074 var args = Array.prototype.slice.call(arguments);
3075 this.context.console.log.apply(this.context.console, args);
3076 };
3077 /**
3078 * Log the current state as error to the console.
3079 */
3080 Terminal.prototype.error = function () {
3081 if (!this.debug)
3082 return;
3083 if (!this.context.console || !this.context.console.error)
3084 return;
3085 var args = Array.prototype.slice.call(arguments);
3086 this.context.console.error.apply(this.context.console, args);
3087 };
3088 /**
3089 * Resizes the terminal.
3090 *
3091 * @param {number} x The number of columns to resize to.
3092 * @param {number} y The number of rows to resize to.
3093 */
3094 Terminal.prototype.resize = function (x, y) {
3095 var line, el, i, j, ch, addToY;
3096 if (x === this.cols && y === this.rows) {
3097 return;
3098 }
3099 if (x < 1)
3100 x = 1;
3101 if (y < 1)
3102 y = 1;
3103 // resize cols
3104 j = this.cols;
3105 if (j < x) {
3106 ch = [this.defAttr, ' ', 1]; // does xterm use the default attr?
3107 i = this.lines.length;
3108 while (i--) {
3109 while (this.lines[i].length < x) {
3110 this.lines[i].push(ch);
3111 }
3112 }
3113 }
3114 else {
3115 i = this.lines.length;
3116 while (i--) {
3117 while (this.lines[i].length > x) {
3118 this.lines[i].pop();
3119 }
3120 }
3121 }
3122 this.setupStops(j);
3123 this.cols = x;
3124 // resize rows
3125 j = this.rows;
3126 addToY = 0;
3127 if (j < y) {
3128 el = this.element;
3129 while (j++ < y) {
3130 // y is rows, not this.y
3131 if (this.lines.length < y + this.ybase) {
3132 if (this.ybase > 0 && this.lines.length <= this.ybase + this.y + addToY + 1) {
3133 // There is room above the buffer and there are no empty elements below the line,
3134 // scroll up
3135 this.ybase--;
3136 addToY++;
3137 if (this.ydisp > 0) {
3138 // Viewport is at the top of the buffer, must increase downwards
3139 this.ydisp--;
3140 }
3141 }
3142 else {
3143 // Add a blank line if there is no buffer left at the top to scroll to, or if there
3144 // are blank lines after the cursor
3145 this.lines.push(this.blankLine());
3146 }
3147 }
3148 if (this.children.length < y) {
3149 this.insertRow();
3150 }
3151 }
3152 }
3153 else {
3154 while (j-- > y) {
3155 if (this.lines.length > y + this.ybase) {
3156 if (this.lines.length > this.ybase + this.y + 1) {
3157 // The line is a blank line below the cursor, remove it
3158 this.lines.pop();
3159 }
3160 else {
3161 // The line is the cursor, scroll down
3162 this.ybase++;
3163 this.ydisp++;
3164 }
3165 }
3166 if (this.children.length > y) {
3167 el = this.children.shift();
3168 if (!el)
3169 continue;
3170 el.parentNode.removeChild(el);
3171 }
3172 }
3173 }
3174 this.rows = y;
3175 // Make sure that the cursor stays on screen
3176 if (this.y >= y) {
3177 this.y = y - 1;
3178 }
3179 if (addToY) {
3180 this.y += addToY;
3181 }
3182 if (this.x >= x) {
3183 this.x = x - 1;
3184 }
3185 this.scrollTop = 0;
3186 this.scrollBottom = y - 1;
3187 this.refresh(0, this.rows - 1);
3188 this.normal = null;
3189 this.geometry = [this.cols, this.rows];
3190 this.emit('resize', { terminal: this, cols: x, rows: y });
3191 };
3192 /**
3193 * Updates the range of rows to refresh
3194 * @param {number} y The number of rows to refresh next.
3195 */
3196 Terminal.prototype.updateRange = function (y) {
3197 if (y < this.refreshStart)
3198 this.refreshStart = y;
3199 if (y > this.refreshEnd)
3200 this.refreshEnd = y;
3201 // if (y > this.refreshEnd) {
3202 // this.refreshEnd = y;
3203 // if (y > this.rows - 1) {
3204 // this.refreshEnd = this.rows - 1;
3205 // }
3206 // }
3207 };
3208 /**
3209 * Set the range of refreshing to the maximum value
3210 */
3211 Terminal.prototype.maxRange = function () {
3212 this.refreshStart = 0;
3213 this.refreshEnd = this.rows - 1;
3214 };
3215 /**
3216 * Setup the tab stops.
3217 * @param {number} i
3218 */
3219 Terminal.prototype.setupStops = function (i) {
3220 if (i != null) {
3221 if (!this.tabs[i]) {
3222 i = this.prevStop(i);
3223 }
3224 }
3225 else {
3226 this.tabs = {};
3227 i = 0;
3228 }
3229 for (; i < this.cols; i += 8) {
3230 this.tabs[i] = true;
3231 }
3232 };
3233 /**
3234 * Move the cursor to the previous tab stop from the given position (default is current).
3235 * @param {number} x The position to move the cursor to the previous tab stop.
3236 */
3237 Terminal.prototype.prevStop = function (x) {
3238 if (x == null)
3239 x = this.x;
3240 while (!this.tabs[--x] && x > 0)
3241 ;
3242 return x >= this.cols
3243 ? this.cols - 1
3244 : x < 0 ? 0 : x;
3245 };
3246 /**
3247 * Move the cursor one tab stop forward from the given position (default is current).
3248 * @param {number} x The position to move the cursor one tab stop forward.
3249 */
3250 Terminal.prototype.nextStop = function (x) {
3251 if (x == null)
3252 x = this.x;
3253 while (!this.tabs[++x] && x < this.cols)
3254 ;
3255 return x >= this.cols
3256 ? this.cols - 1
3257 : x < 0 ? 0 : x;
3258 };
3259 /**
3260 * Erase in the identified line everything from "x" to the end of the line (right).
3261 * @param {number} x The column from which to start erasing to the end of the line.
3262 * @param {number} y The line in which to operate.
3263 */
3264 Terminal.prototype.eraseRight = function (x, y) {
3265 var line = this.lines[this.ybase + y], ch = [this.eraseAttr(), ' ', 1]; // xterm
3266 for (; x < this.cols; x++) {
3267 line[x] = ch;
3268 }
3269 this.updateRange(y);
3270 };
3271 /**
3272 * Erase in the identified line everything from "x" to the start of the line (left).
3273 * @param {number} x The column from which to start erasing to the start of the line.
3274 * @param {number} y The line in which to operate.
3275 */
3276 Terminal.prototype.eraseLeft = function (x, y) {
3277 var line = this.lines[this.ybase + y], ch = [this.eraseAttr(), ' ', 1]; // xterm
3278 x++;
3279 while (x--)
3280 line[x] = ch;
3281 this.updateRange(y);
3282 };
3283 /**
3284 * Clears the entire buffer, making the prompt line the new first line.
3285 */
3286 Terminal.prototype.clear = function () {
3287 if (this.ybase === 0 && this.y === 0) {
3288 // Don't clear if it's already clear
3289 return;
3290 }
3291 this.lines = [this.lines[this.ybase + this.y]];
3292 this.ydisp = 0;
3293 this.ybase = 0;
3294 this.y = 0;
3295 for (var i = 1; i < this.rows; i++) {
3296 this.lines.push(this.blankLine());
3297 }
3298 this.refresh(0, this.rows - 1);
3299 this.emit('scroll', this.ydisp);
3300 };
3301 /**
3302 * Erase all content in the given line
3303 * @param {number} y The line to erase all of its contents.
3304 */
3305 Terminal.prototype.eraseLine = function (y) {
3306 this.eraseRight(0, y);
3307 };
3308 /**
3309 * Return the data array of a blank line/
3310 * @param {number} cur First bunch of data for each "blank" character.
3311 */
3312 Terminal.prototype.blankLine = function (cur) {
3313 var attr = cur
3314 ? this.eraseAttr()
3315 : this.defAttr;
3316 var ch = [attr, ' ', 1] // width defaults to 1 halfwidth character
3317 , line = [], i = 0;
3318 for (; i < this.cols; i++) {
3319 line[i] = ch;
3320 }
3321 return line;
3322 };
3323 /**
3324 * If cur return the back color xterm feature attribute. Else return defAttr.
3325 * @param {object} cur
3326 */
3327 Terminal.prototype.ch = function (cur) {
3328 return cur
3329 ? [this.eraseAttr(), ' ', 1]
3330 : [this.defAttr, ' ', 1];
3331 };
3332 /**
3333 * Evaluate if the current erminal is the given argument.
3334 * @param {object} term The terminal to evaluate
3335 */
3336 Terminal.prototype.is = function (term) {
3337 var name = this.termName;
3338 return (name + '').indexOf(term) === 0;
3339 };
3340 /**
3341 * Emit the 'data' event and populate the given data.
3342 * @param {string} data The data to populate in the event.
3343 */
3344 Terminal.prototype.handler = function (data) {
3345 // Input is being sent to the terminal, the terminal should focus the prompt.
3346 if (this.ybase !== this.ydisp) {
3347 this.scrollToBottom();
3348 }
3349 this.emit('data', data);
3350 };
3351 /**
3352 * Emit the 'title' event and populate the given title.
3353 * @param {string} title The title to populate in the event.
3354 */
3355 Terminal.prototype.handleTitle = function (title) {
3356 /**
3357 * This event is emitted when the title of the terminal is changed
3358 * from inside the terminal. The parameter is the new title.
3359 *
3360 * @event title
3361 */
3362 this.emit('title', title);
3363 };
3364 /**
3365 * ESC
3366 */
3367 /**
3368 * ESC D Index (IND is 0x84).
3369 */
3370 Terminal.prototype.index = function () {
3371 this.y++;
3372 if (this.y > this.scrollBottom) {
3373 this.y--;
3374 this.scroll();
3375 }
3376 this.state = normal;
3377 };
3378 /**
3379 * ESC M Reverse Index (RI is 0x8d).
3380 */
3381 Terminal.prototype.reverseIndex = function () {
3382 var j;
3383 this.y--;
3384 if (this.y < this.scrollTop) {
3385 this.y++;
3386 // possibly move the code below to term.reverseScroll();
3387 // test: echo -ne '\e[1;1H\e[44m\eM\e[0m'
3388 // blankLine(true) is xterm/linux behavior
3389 this.lines.splice(this.y + this.ybase, 0, this.blankLine(true));
3390 j = this.rows - 1 - this.scrollBottom;
3391 this.lines.splice(this.rows - 1 + this.ybase - j + 1, 1);
3392 // this.maxRange();
3393 this.updateRange(this.scrollTop);
3394 this.updateRange(this.scrollBottom);
3395 }
3396 this.state = normal;
3397 };
3398 /**
3399 * ESC c Full Reset (RIS).
3400 */
3401 Terminal.prototype.reset = function () {
3402 this.options.rows = this.rows;
3403 this.options.cols = this.cols;
3404 var customKeydownHandler = this.customKeydownHandler;
3405 Terminal.call(this, this.options);
3406 this.customKeydownHandler = customKeydownHandler;
3407 this.refresh(0, this.rows - 1);
3408 this.viewport.syncScrollArea();
3409 };
3410 /**
3411 * ESC H Tab Set (HTS is 0x88).
3412 */
3413 Terminal.prototype.tabSet = function () {
3414 this.tabs[this.x] = true;
3415 this.state = normal;
3416 };
3417 /**
3418 * CSI
3419 */
3420 /**
3421 * CSI Ps A
3422 * Cursor Up Ps Times (default = 1) (CUU).
3423 */
3424 Terminal.prototype.cursorUp = function (params) {
3425 var param = params[0];
3426 if (param < 1)
3427 param = 1;
3428 this.y -= param;
3429 if (this.y < 0)
3430 this.y = 0;
3431 };
3432 /**
3433 * CSI Ps B
3434 * Cursor Down Ps Times (default = 1) (CUD).
3435 */
3436 Terminal.prototype.cursorDown = function (params) {
3437 var param = params[0];
3438 if (param < 1)
3439 param = 1;
3440 this.y += param;
3441 if (this.y >= this.rows) {
3442 this.y = this.rows - 1;
3443 }
3444 };
3445 /**
3446 * CSI Ps C
3447 * Cursor Forward Ps Times (default = 1) (CUF).
3448 */
3449 Terminal.prototype.cursorForward = function (params) {
3450 var param = params[0];
3451 if (param < 1)
3452 param = 1;
3453 this.x += param;
3454 if (this.x >= this.cols) {
3455 this.x = this.cols - 1;
3456 }
3457 };
3458 /**
3459 * CSI Ps D
3460 * Cursor Backward Ps Times (default = 1) (CUB).
3461 */
3462 Terminal.prototype.cursorBackward = function (params) {
3463 var param = params[0];
3464 if (param < 1)
3465 param = 1;
3466 this.x -= param;
3467 if (this.x < 0)
3468 this.x = 0;
3469 };
3470 /**
3471 * CSI Ps ; Ps H
3472 * Cursor Position [row;column] (default = [1,1]) (CUP).
3473 */
3474 Terminal.prototype.cursorPos = function (params) {
3475 var row, col;
3476 row = params[0] - 1;
3477 if (params.length >= 2) {
3478 col = params[1] - 1;
3479 }
3480 else {
3481 col = 0;
3482 }
3483 if (row < 0) {
3484 row = 0;
3485 }
3486 else if (row >= this.rows) {
3487 row = this.rows - 1;
3488 }
3489 if (col < 0) {
3490 col = 0;
3491 }
3492 else if (col >= this.cols) {
3493 col = this.cols - 1;
3494 }
3495 this.x = col;
3496 this.y = row;
3497 };
3498 /**
3499 * CSI Ps J Erase in Display (ED).
3500 * Ps = 0 -> Erase Below (default).
3501 * Ps = 1 -> Erase Above.
3502 * Ps = 2 -> Erase All.
3503 * Ps = 3 -> Erase Saved Lines (xterm).
3504 * CSI ? Ps J
3505 * Erase in Display (DECSED).
3506 * Ps = 0 -> Selective Erase Below (default).
3507 * Ps = 1 -> Selective Erase Above.
3508 * Ps = 2 -> Selective Erase All.
3509 */
3510 Terminal.prototype.eraseInDisplay = function (params) {
3511 var j;
3512 switch (params[0]) {
3513 case 0:
3514 this.eraseRight(this.x, this.y);
3515 j = this.y + 1;
3516 for (; j < this.rows; j++) {
3517 this.eraseLine(j);
3518 }
3519 break;
3520 case 1:
3521 this.eraseLeft(this.x, this.y);
3522 j = this.y;
3523 while (j--) {
3524 this.eraseLine(j);
3525 }
3526 break;
3527 case 2:
3528 j = this.rows;
3529 while (j--)
3530 this.eraseLine(j);
3531 break;
3532 case 3:
3533 ; // no saved lines
3534 break;
3535 }
3536 };
3537 /**
3538 * CSI Ps K Erase in Line (EL).
3539 * Ps = 0 -> Erase to Right (default).
3540 * Ps = 1 -> Erase to Left.
3541 * Ps = 2 -> Erase All.
3542 * CSI ? Ps K
3543 * Erase in Line (DECSEL).
3544 * Ps = 0 -> Selective Erase to Right (default).
3545 * Ps = 1 -> Selective Erase to Left.
3546 * Ps = 2 -> Selective Erase All.
3547 */
3548 Terminal.prototype.eraseInLine = function (params) {
3549 switch (params[0]) {
3550 case 0:
3551 this.eraseRight(this.x, this.y);
3552 break;
3553 case 1:
3554 this.eraseLeft(this.x, this.y);
3555 break;
3556 case 2:
3557 this.eraseLine(this.y);
3558 break;
3559 }
3560 };
3561 /**
3562 * CSI Pm m Character Attributes (SGR).
3563 * Ps = 0 -> Normal (default).
3564 * Ps = 1 -> Bold.
3565 * Ps = 4 -> Underlined.
3566 * Ps = 5 -> Blink (appears as Bold).
3567 * Ps = 7 -> Inverse.
3568 * Ps = 8 -> Invisible, i.e., hidden (VT300).
3569 * Ps = 2 2 -> Normal (neither bold nor faint).
3570 * Ps = 2 4 -> Not underlined.
3571 * Ps = 2 5 -> Steady (not blinking).
3572 * Ps = 2 7 -> Positive (not inverse).
3573 * Ps = 2 8 -> Visible, i.e., not hidden (VT300).
3574 * Ps = 3 0 -> Set foreground color to Black.
3575 * Ps = 3 1 -> Set foreground color to Red.
3576 * Ps = 3 2 -> Set foreground color to Green.
3577 * Ps = 3 3 -> Set foreground color to Yellow.
3578 * Ps = 3 4 -> Set foreground color to Blue.
3579 * Ps = 3 5 -> Set foreground color to Magenta.
3580 * Ps = 3 6 -> Set foreground color to Cyan.
3581 * Ps = 3 7 -> Set foreground color to White.
3582 * Ps = 3 9 -> Set foreground color to default (original).
3583 * Ps = 4 0 -> Set background color to Black.
3584 * Ps = 4 1 -> Set background color to Red.
3585 * Ps = 4 2 -> Set background color to Green.
3586 * Ps = 4 3 -> Set background color to Yellow.
3587 * Ps = 4 4 -> Set background color to Blue.
3588 * Ps = 4 5 -> Set background color to Magenta.
3589 * Ps = 4 6 -> Set background color to Cyan.
3590 * Ps = 4 7 -> Set background color to White.
3591 * Ps = 4 9 -> Set background color to default (original).
3592 *
3593 * If 16-color support is compiled, the following apply. Assume
3594 * that xterm's resources are set so that the ISO color codes are
3595 * the first 8 of a set of 16. Then the aixterm colors are the
3596 * bright versions of the ISO colors:
3597 * Ps = 9 0 -> Set foreground color to Black.
3598 * Ps = 9 1 -> Set foreground color to Red.
3599 * Ps = 9 2 -> Set foreground color to Green.
3600 * Ps = 9 3 -> Set foreground color to Yellow.
3601 * Ps = 9 4 -> Set foreground color to Blue.
3602 * Ps = 9 5 -> Set foreground color to Magenta.
3603 * Ps = 9 6 -> Set foreground color to Cyan.
3604 * Ps = 9 7 -> Set foreground color to White.
3605 * Ps = 1 0 0 -> Set background color to Black.
3606 * Ps = 1 0 1 -> Set background color to Red.
3607 * Ps = 1 0 2 -> Set background color to Green.
3608 * Ps = 1 0 3 -> Set background color to Yellow.
3609 * Ps = 1 0 4 -> Set background color to Blue.
3610 * Ps = 1 0 5 -> Set background color to Magenta.
3611 * Ps = 1 0 6 -> Set background color to Cyan.
3612 * Ps = 1 0 7 -> Set background color to White.
3613 *
3614 * If xterm is compiled with the 16-color support disabled, it
3615 * supports the following, from rxvt:
3616 * Ps = 1 0 0 -> Set foreground and background color to
3617 * default.
3618 *
3619 * If 88- or 256-color support is compiled, the following apply.
3620 * Ps = 3 8 ; 5 ; Ps -> Set foreground color to the second
3621 * Ps.
3622 * Ps = 4 8 ; 5 ; Ps -> Set background color to the second
3623 * Ps.
3624 */
3625 Terminal.prototype.charAttributes = function (params) {
3626 // Optimize a single SGR0.
3627 if (params.length === 1 && params[0] === 0) {
3628 this.curAttr = this.defAttr;
3629 return;
3630 }
3631 var l = params.length, i = 0, flags = this.curAttr >> 18, fg = (this.curAttr >> 9) & 0x1ff, bg = this.curAttr & 0x1ff, p;
3632 for (; i < l; i++) {
3633 p = params[i];
3634 if (p >= 30 && p <= 37) {
3635 // fg color 8
3636 fg = p - 30;
3637 }
3638 else if (p >= 40 && p <= 47) {
3639 // bg color 8
3640 bg = p - 40;
3641 }
3642 else if (p >= 90 && p <= 97) {
3643 // fg color 16
3644 p += 8;
3645 fg = p - 90;
3646 }
3647 else if (p >= 100 && p <= 107) {
3648 // bg color 16
3649 p += 8;
3650 bg = p - 100;
3651 }
3652 else if (p === 0) {
3653 // default
3654 flags = this.defAttr >> 18;
3655 fg = (this.defAttr >> 9) & 0x1ff;
3656 bg = this.defAttr & 0x1ff;
3657 }
3658 else if (p === 1) {
3659 // bold text
3660 flags |= 1;
3661 }
3662 else if (p === 4) {
3663 // underlined text
3664 flags |= 2;
3665 }
3666 else if (p === 5) {
3667 // blink
3668 flags |= 4;
3669 }
3670 else if (p === 7) {
3671 // inverse and positive
3672 // test with: echo -e '\e[31m\e[42mhello\e[7mworld\e[27mhi\e[m'
3673 flags |= 8;
3674 }
3675 else if (p === 8) {
3676 // invisible
3677 flags |= 16;
3678 }
3679 else if (p === 22) {
3680 // not bold
3681 flags &= ~1;
3682 }
3683 else if (p === 24) {
3684 // not underlined
3685 flags &= ~2;
3686 }
3687 else if (p === 25) {
3688 // not blink
3689 flags &= ~4;
3690 }
3691 else if (p === 27) {
3692 // not inverse
3693 flags &= ~8;
3694 }
3695 else if (p === 28) {
3696 // not invisible
3697 flags &= ~16;
3698 }
3699 else if (p === 39) {
3700 // reset fg
3701 fg = (this.defAttr >> 9) & 0x1ff;
3702 }
3703 else if (p === 49) {
3704 // reset bg
3705 bg = this.defAttr & 0x1ff;
3706 }
3707 else if (p === 38) {
3708 // fg color 256
3709 if (params[i + 1] === 2) {
3710 i += 2;
3711 fg = matchColor(params[i] & 0xff, params[i + 1] & 0xff, params[i + 2] & 0xff);
3712 if (fg === -1)
3713 fg = 0x1ff;
3714 i += 2;
3715 }
3716 else if (params[i + 1] === 5) {
3717 i += 2;
3718 p = params[i] & 0xff;
3719 fg = p;
3720 }
3721 }
3722 else if (p === 48) {
3723 // bg color 256
3724 if (params[i + 1] === 2) {
3725 i += 2;
3726 bg = matchColor(params[i] & 0xff, params[i + 1] & 0xff, params[i + 2] & 0xff);
3727 if (bg === -1)
3728 bg = 0x1ff;
3729 i += 2;
3730 }
3731 else if (params[i + 1] === 5) {
3732 i += 2;
3733 p = params[i] & 0xff;
3734 bg = p;
3735 }
3736 }
3737 else if (p === 100) {
3738 // reset fg/bg
3739 fg = (this.defAttr >> 9) & 0x1ff;
3740 bg = this.defAttr & 0x1ff;
3741 }
3742 else {
3743 this.error('Unknown SGR attribute: %d.', p);
3744 }
3745 }
3746 this.curAttr = (flags << 18) | (fg << 9) | bg;
3747 };
3748 /**
3749 * CSI Ps n Device Status Report (DSR).
3750 * Ps = 5 -> Status Report. Result (``OK'') is
3751 * CSI 0 n
3752 * Ps = 6 -> Report Cursor Position (CPR) [row;column].
3753 * Result is
3754 * CSI r ; c R
3755 * CSI ? Ps n
3756 * Device Status Report (DSR, DEC-specific).
3757 * Ps = 6 -> Report Cursor Position (CPR) [row;column] as CSI
3758 * ? r ; c R (assumes page is zero).
3759 * Ps = 1 5 -> Report Printer status as CSI ? 1 0 n (ready).
3760 * or CSI ? 1 1 n (not ready).
3761 * Ps = 2 5 -> Report UDK status as CSI ? 2 0 n (unlocked)
3762 * or CSI ? 2 1 n (locked).
3763 * Ps = 2 6 -> Report Keyboard status as
3764 * CSI ? 2 7 ; 1 ; 0 ; 0 n (North American).
3765 * The last two parameters apply to VT400 & up, and denote key-
3766 * board ready and LK01 respectively.
3767 * Ps = 5 3 -> Report Locator status as
3768 * CSI ? 5 3 n Locator available, if compiled-in, or
3769 * CSI ? 5 0 n No Locator, if not.
3770 */
3771 Terminal.prototype.deviceStatus = function (params) {
3772 if (!this.prefix) {
3773 switch (params[0]) {
3774 case 5:
3775 // status report
3776 this.send('\x1b[0n');
3777 break;
3778 case 6:
3779 // cursor position
3780 this.send('\x1b['
3781 + (this.y + 1)
3782 + ';'
3783 + (this.x + 1)
3784 + 'R');
3785 break;
3786 }
3787 }
3788 else if (this.prefix === '?') {
3789 // modern xterm doesnt seem to
3790 // respond to any of these except ?6, 6, and 5
3791 switch (params[0]) {
3792 case 6:
3793 // cursor position
3794 this.send('\x1b[?'
3795 + (this.y + 1)
3796 + ';'
3797 + (this.x + 1)
3798 + 'R');
3799 break;
3800 case 15:
3801 // no printer
3802 // this.send('\x1b[?11n');
3803 break;
3804 case 25:
3805 // dont support user defined keys
3806 // this.send('\x1b[?21n');
3807 break;
3808 case 26:
3809 // north american keyboard
3810 // this.send('\x1b[?27;1;0;0n');
3811 break;
3812 case 53:
3813 // no dec locator/mouse
3814 // this.send('\x1b[?50n');
3815 break;
3816 }
3817 }
3818 };
3819 /**
3820 * Additions
3821 */
3822 /**
3823 * CSI Ps @
3824 * Insert Ps (Blank) Character(s) (default = 1) (ICH).
3825 */
3826 Terminal.prototype.insertChars = function (params) {
3827 var param, row, j, ch;
3828 param = params[0];
3829 if (param < 1)
3830 param = 1;
3831 row = this.y + this.ybase;
3832 j = this.x;
3833 ch = [this.eraseAttr(), ' ', 1]; // xterm
3834 while (param-- && j < this.cols) {
3835 this.lines[row].splice(j++, 0, ch);
3836 this.lines[row].pop();
3837 }
3838 };
3839 /**
3840 * CSI Ps E
3841 * Cursor Next Line Ps Times (default = 1) (CNL).
3842 * same as CSI Ps B ?
3843 */
3844 Terminal.prototype.cursorNextLine = function (params) {
3845 var param = params[0];
3846 if (param < 1)
3847 param = 1;
3848 this.y += param;
3849 if (this.y >= this.rows) {
3850 this.y = this.rows - 1;
3851 }
3852 this.x = 0;
3853 };
3854 /**
3855 * CSI Ps F
3856 * Cursor Preceding Line Ps Times (default = 1) (CNL).
3857 * reuse CSI Ps A ?
3858 */
3859 Terminal.prototype.cursorPrecedingLine = function (params) {
3860 var param = params[0];
3861 if (param < 1)
3862 param = 1;
3863 this.y -= param;
3864 if (this.y < 0)
3865 this.y = 0;
3866 this.x = 0;
3867 };
3868 /**
3869 * CSI Ps G
3870 * Cursor Character Absolute [column] (default = [row,1]) (CHA).
3871 */
3872 Terminal.prototype.cursorCharAbsolute = function (params) {
3873 var param = params[0];
3874 if (param < 1)
3875 param = 1;
3876 this.x = param - 1;
3877 };
3878 /**
3879 * CSI Ps L
3880 * Insert Ps Line(s) (default = 1) (IL).
3881 */
3882 Terminal.prototype.insertLines = function (params) {
3883 var param, row, j;
3884 param = params[0];
3885 if (param < 1)
3886 param = 1;
3887 row = this.y + this.ybase;
3888 j = this.rows - 1 - this.scrollBottom;
3889 j = this.rows - 1 + this.ybase - j + 1;
3890 while (param--) {
3891 // test: echo -e '\e[44m\e[1L\e[0m'
3892 // blankLine(true) - xterm/linux behavior
3893 this.lines.splice(row, 0, this.blankLine(true));
3894 this.lines.splice(j, 1);
3895 }
3896 // this.maxRange();
3897 this.updateRange(this.y);
3898 this.updateRange(this.scrollBottom);
3899 };
3900 /**
3901 * CSI Ps M
3902 * Delete Ps Line(s) (default = 1) (DL).
3903 */
3904 Terminal.prototype.deleteLines = function (params) {
3905 var param, row, j;
3906 param = params[0];
3907 if (param < 1)
3908 param = 1;
3909 row = this.y + this.ybase;
3910 j = this.rows - 1 - this.scrollBottom;
3911 j = this.rows - 1 + this.ybase - j;
3912 while (param--) {
3913 // test: echo -e '\e[44m\e[1M\e[0m'
3914 // blankLine(true) - xterm/linux behavior
3915 this.lines.splice(j + 1, 0, this.blankLine(true));
3916 this.lines.splice(row, 1);
3917 }
3918 // this.maxRange();
3919 this.updateRange(this.y);
3920 this.updateRange(this.scrollBottom);
3921 };
3922 /**
3923 * CSI Ps P
3924 * Delete Ps Character(s) (default = 1) (DCH).
3925 */
3926 Terminal.prototype.deleteChars = function (params) {
3927 var param, row, ch;
3928 param = params[0];
3929 if (param < 1)
3930 param = 1;
3931 row = this.y + this.ybase;
3932 ch = [this.eraseAttr(), ' ', 1]; // xterm
3933 while (param--) {
3934 this.lines[row].splice(this.x, 1);
3935 this.lines[row].push(ch);
3936 }
3937 };
3938 /**
3939 * CSI Ps X
3940 * Erase Ps Character(s) (default = 1) (ECH).
3941 */
3942 Terminal.prototype.eraseChars = function (params) {
3943 var param, row, j, ch;
3944 param = params[0];
3945 if (param < 1)
3946 param = 1;
3947 row = this.y + this.ybase;
3948 j = this.x;
3949 ch = [this.eraseAttr(), ' ', 1]; // xterm
3950 while (param-- && j < this.cols) {
3951 this.lines[row][j++] = ch;
3952 }
3953 };
3954 /**
3955 * CSI Pm ` Character Position Absolute
3956 * [column] (default = [row,1]) (HPA).
3957 */
3958 Terminal.prototype.charPosAbsolute = function (params) {
3959 var param = params[0];
3960 if (param < 1)
3961 param = 1;
3962 this.x = param - 1;
3963 if (this.x >= this.cols) {
3964 this.x = this.cols - 1;
3965 }
3966 };
3967 /**
3968 * 141 61 a * HPR -
3969 * Horizontal Position Relative
3970 * reuse CSI Ps C ?
3971 */
3972 Terminal.prototype.HPositionRelative = function (params) {
3973 var param = params[0];
3974 if (param < 1)
3975 param = 1;
3976 this.x += param;
3977 if (this.x >= this.cols) {
3978 this.x = this.cols - 1;
3979 }
3980 };
3981 /**
3982 * CSI Ps c Send Device Attributes (Primary DA).
3983 * Ps = 0 or omitted -> request attributes from terminal. The
3984 * response depends on the decTerminalID resource setting.
3985 * -> CSI ? 1 ; 2 c (``VT100 with Advanced Video Option'')
3986 * -> CSI ? 1 ; 0 c (``VT101 with No Options'')
3987 * -> CSI ? 6 c (``VT102'')
3988 * -> CSI ? 6 0 ; 1 ; 2 ; 6 ; 8 ; 9 ; 1 5 ; c (``VT220'')
3989 * The VT100-style response parameters do not mean anything by
3990 * themselves. VT220 parameters do, telling the host what fea-
3991 * tures the terminal supports:
3992 * Ps = 1 -> 132-columns.
3993 * Ps = 2 -> Printer.
3994 * Ps = 6 -> Selective erase.
3995 * Ps = 8 -> User-defined keys.
3996 * Ps = 9 -> National replacement character sets.
3997 * Ps = 1 5 -> Technical characters.
3998 * Ps = 2 2 -> ANSI color, e.g., VT525.
3999 * Ps = 2 9 -> ANSI text locator (i.e., DEC Locator mode).
4000 * CSI > Ps c
4001 * Send Device Attributes (Secondary DA).
4002 * Ps = 0 or omitted -> request the terminal's identification
4003 * code. The response depends on the decTerminalID resource set-
4004 * ting. It should apply only to VT220 and up, but xterm extends
4005 * this to VT100.
4006 * -> CSI > Pp ; Pv ; Pc c
4007 * where Pp denotes the terminal type
4008 * Pp = 0 -> ``VT100''.
4009 * Pp = 1 -> ``VT220''.
4010 * and Pv is the firmware version (for xterm, this was originally
4011 * the XFree86 patch number, starting with 95). In a DEC termi-
4012 * nal, Pc indicates the ROM cartridge registration number and is
4013 * always zero.
4014 * More information:
4015 * xterm/charproc.c - line 2012, for more information.
4016 * vim responds with ^[[?0c or ^[[?1c after the terminal's response (?)
4017 */
4018 Terminal.prototype.sendDeviceAttributes = function (params) {
4019 if (params[0] > 0)
4020 return;
4021 if (!this.prefix) {
4022 if (this.is('xterm')
4023 || this.is('rxvt-unicode')
4024 || this.is('screen')) {
4025 this.send('\x1b[?1;2c');
4026 }
4027 else if (this.is('linux')) {
4028 this.send('\x1b[?6c');
4029 }
4030 }
4031 else if (this.prefix === '>') {
4032 // xterm and urxvt
4033 // seem to spit this
4034 // out around ~370 times (?).
4035 if (this.is('xterm')) {
4036 this.send('\x1b[>0;276;0c');
4037 }
4038 else if (this.is('rxvt-unicode')) {
4039 this.send('\x1b[>85;95;0c');
4040 }
4041 else if (this.is('linux')) {
4042 // not supported by linux console.
4043 // linux console echoes parameters.
4044 this.send(params[0] + 'c');
4045 }
4046 else if (this.is('screen')) {
4047 this.send('\x1b[>83;40003;0c');
4048 }
4049 }
4050 };
4051 /**
4052 * CSI Pm d
4053 * Line Position Absolute [row] (default = [1,column]) (VPA).
4054 */
4055 Terminal.prototype.linePosAbsolute = function (params) {
4056 var param = params[0];
4057 if (param < 1)
4058 param = 1;
4059 this.y = param - 1;
4060 if (this.y >= this.rows) {
4061 this.y = this.rows - 1;
4062 }
4063 };
4064 /**
4065 * 145 65 e * VPR - Vertical Position Relative
4066 * reuse CSI Ps B ?
4067 */
4068 Terminal.prototype.VPositionRelative = function (params) {
4069 var param = params[0];
4070 if (param < 1)
4071 param = 1;
4072 this.y += param;
4073 if (this.y >= this.rows) {
4074 this.y = this.rows - 1;
4075 }
4076 };
4077 /**
4078 * CSI Ps ; Ps f
4079 * Horizontal and Vertical Position [row;column] (default =
4080 * [1,1]) (HVP).
4081 */
4082 Terminal.prototype.HVPosition = function (params) {
4083 if (params[0] < 1)
4084 params[0] = 1;
4085 if (params[1] < 1)
4086 params[1] = 1;
4087 this.y = params[0] - 1;
4088 if (this.y >= this.rows) {
4089 this.y = this.rows - 1;
4090 }
4091 this.x = params[1] - 1;
4092 if (this.x >= this.cols) {
4093 this.x = this.cols - 1;
4094 }
4095 };
4096 /**
4097 * CSI Pm h Set Mode (SM).
4098 * Ps = 2 -> Keyboard Action Mode (AM).
4099 * Ps = 4 -> Insert Mode (IRM).
4100 * Ps = 1 2 -> Send/receive (SRM).
4101 * Ps = 2 0 -> Automatic Newline (LNM).
4102 * CSI ? Pm h
4103 * DEC Private Mode Set (DECSET).
4104 * Ps = 1 -> Application Cursor Keys (DECCKM).
4105 * Ps = 2 -> Designate USASCII for character sets G0-G3
4106 * (DECANM), and set VT100 mode.
4107 * Ps = 3 -> 132 Column Mode (DECCOLM).
4108 * Ps = 4 -> Smooth (Slow) Scroll (DECSCLM).
4109 * Ps = 5 -> Reverse Video (DECSCNM).
4110 * Ps = 6 -> Origin Mode (DECOM).
4111 * Ps = 7 -> Wraparound Mode (DECAWM).
4112 * Ps = 8 -> Auto-repeat Keys (DECARM).
4113 * Ps = 9 -> Send Mouse X & Y on button press. See the sec-
4114 * tion Mouse Tracking.
4115 * Ps = 1 0 -> Show toolbar (rxvt).
4116 * Ps = 1 2 -> Start Blinking Cursor (att610).
4117 * Ps = 1 8 -> Print form feed (DECPFF).
4118 * Ps = 1 9 -> Set print extent to full screen (DECPEX).
4119 * Ps = 2 5 -> Show Cursor (DECTCEM).
4120 * Ps = 3 0 -> Show scrollbar (rxvt).
4121 * Ps = 3 5 -> Enable font-shifting functions (rxvt).
4122 * Ps = 3 8 -> Enter Tektronix Mode (DECTEK).
4123 * Ps = 4 0 -> Allow 80 -> 132 Mode.
4124 * Ps = 4 1 -> more(1) fix (see curses resource).
4125 * Ps = 4 2 -> Enable Nation Replacement Character sets (DECN-
4126 * RCM).
4127 * Ps = 4 4 -> Turn On Margin Bell.
4128 * Ps = 4 5 -> Reverse-wraparound Mode.
4129 * Ps = 4 6 -> Start Logging. This is normally disabled by a
4130 * compile-time option.
4131 * Ps = 4 7 -> Use Alternate Screen Buffer. (This may be dis-
4132 * abled by the titeInhibit resource).
4133 * Ps = 6 6 -> Application keypad (DECNKM).
4134 * Ps = 6 7 -> Backarrow key sends backspace (DECBKM).
4135 * Ps = 1 0 0 0 -> Send Mouse X & Y on button press and
4136 * release. See the section Mouse Tracking.
4137 * Ps = 1 0 0 1 -> Use Hilite Mouse Tracking.
4138 * Ps = 1 0 0 2 -> Use Cell Motion Mouse Tracking.
4139 * Ps = 1 0 0 3 -> Use All Motion Mouse Tracking.
4140 * Ps = 1 0 0 4 -> Send FocusIn/FocusOut events.
4141 * Ps = 1 0 0 5 -> Enable Extended Mouse Mode.
4142 * Ps = 1 0 1 0 -> Scroll to bottom on tty output (rxvt).
4143 * Ps = 1 0 1 1 -> Scroll to bottom on key press (rxvt).
4144 * Ps = 1 0 3 4 -> Interpret "meta" key, sets eighth bit.
4145 * (enables the eightBitInput resource).
4146 * Ps = 1 0 3 5 -> Enable special modifiers for Alt and Num-
4147 * Lock keys. (This enables the numLock resource).
4148 * Ps = 1 0 3 6 -> Send ESC when Meta modifies a key. (This
4149 * enables the metaSendsEscape resource).
4150 * Ps = 1 0 3 7 -> Send DEL from the editing-keypad Delete
4151 * key.
4152 * Ps = 1 0 3 9 -> Send ESC when Alt modifies a key. (This
4153 * enables the altSendsEscape resource).
4154 * Ps = 1 0 4 0 -> Keep selection even if not highlighted.
4155 * (This enables the keepSelection resource).
4156 * Ps = 1 0 4 1 -> Use the CLIPBOARD selection. (This enables
4157 * the selectToClipboard resource).
4158 * Ps = 1 0 4 2 -> Enable Urgency window manager hint when
4159 * Control-G is received. (This enables the bellIsUrgent
4160 * resource).
4161 * Ps = 1 0 4 3 -> Enable raising of the window when Control-G
4162 * is received. (enables the popOnBell resource).
4163 * Ps = 1 0 4 7 -> Use Alternate Screen Buffer. (This may be
4164 * disabled by the titeInhibit resource).
4165 * Ps = 1 0 4 8 -> Save cursor as in DECSC. (This may be dis-
4166 * abled by the titeInhibit resource).
4167 * Ps = 1 0 4 9 -> Save cursor as in DECSC and use Alternate
4168 * Screen Buffer, clearing it first. (This may be disabled by
4169 * the titeInhibit resource). This combines the effects of the 1
4170 * 0 4 7 and 1 0 4 8 modes. Use this with terminfo-based
4171 * applications rather than the 4 7 mode.
4172 * Ps = 1 0 5 0 -> Set terminfo/termcap function-key mode.
4173 * Ps = 1 0 5 1 -> Set Sun function-key mode.
4174 * Ps = 1 0 5 2 -> Set HP function-key mode.
4175 * Ps = 1 0 5 3 -> Set SCO function-key mode.
4176 * Ps = 1 0 6 0 -> Set legacy keyboard emulation (X11R6).
4177 * Ps = 1 0 6 1 -> Set VT220 keyboard emulation.
4178 * Ps = 2 0 0 4 -> Set bracketed paste mode.
4179 * Modes:
4180 * http: *vt100.net/docs/vt220-rm/chapter4.html
4181 */
4182 Terminal.prototype.setMode = function (params) {
4183 if (typeof params === 'object') {
4184 var l = params.length, i = 0;
4185 for (; i < l; i++) {
4186 this.setMode(params[i]);
4187 }
4188 return;
4189 }
4190 if (!this.prefix) {
4191 switch (params) {
4192 case 4:
4193 this.insertMode = true;
4194 break;
4195 case 20:
4196 //this.convertEol = true;
4197 break;
4198 }
4199 }
4200 else if (this.prefix === '?') {
4201 switch (params) {
4202 case 1:
4203 this.applicationCursor = true;
4204 break;
4205 case 2:
4206 this.setgCharset(0, Terminal.charsets.US);
4207 this.setgCharset(1, Terminal.charsets.US);
4208 this.setgCharset(2, Terminal.charsets.US);
4209 this.setgCharset(3, Terminal.charsets.US);
4210 // set VT100 mode here
4211 break;
4212 case 3:
4213 this.savedCols = this.cols;
4214 this.resize(132, this.rows);
4215 break;
4216 case 6:
4217 this.originMode = true;
4218 break;
4219 case 7:
4220 this.wraparoundMode = true;
4221 break;
4222 case 12:
4223 // this.cursorBlink = true;
4224 break;
4225 case 66:
4226 this.log('Serial port requested application keypad.');
4227 this.applicationKeypad = true;
4228 this.viewport.syncScrollArea();
4229 break;
4230 case 9: // X10 Mouse
4231 // no release, no motion, no wheel, no modifiers.
4232 case 1000: // vt200 mouse
4233 // no motion.
4234 // no modifiers, except control on the wheel.
4235 case 1002: // button event mouse
4236 case 1003:
4237 // any event - sends motion events,
4238 // even if there is no button held down.
4239 this.x10Mouse = params === 9;
4240 this.vt200Mouse = params === 1000;
4241 this.normalMouse = params > 1000;
4242 this.mouseEvents = true;
4243 this.element.style.cursor = 'default';
4244 this.log('Binding to mouse events.');
4245 break;
4246 case 1004:
4247 // focusin: ^[[I
4248 // focusout: ^[[O
4249 this.sendFocus = true;
4250 break;
4251 case 1005:
4252 this.utfMouse = true;
4253 // for wide terminals
4254 // simply encodes large values as utf8 characters
4255 break;
4256 case 1006:
4257 this.sgrMouse = true;
4258 // for wide terminals
4259 // does not add 32 to fields
4260 // press: ^[[<b;x;yM
4261 // release: ^[[<b;x;ym
4262 break;
4263 case 1015:
4264 this.urxvtMouse = true;
4265 // for wide terminals
4266 // numbers for fields
4267 // press: ^[[b;x;yM
4268 // motion: ^[[b;x;yT
4269 break;
4270 case 25:
4271 this.cursorHidden = false;
4272 break;
4273 case 1049:
4274 //this.saveCursor();
4275 ; // FALL-THROUGH
4276 case 47: // alt screen buffer
4277 case 1047:
4278 if (!this.normal) {
4279 var normal = {
4280 lines: this.lines,
4281 ybase: this.ybase,
4282 ydisp: this.ydisp,
4283 x: this.x,
4284 y: this.y,
4285 scrollTop: this.scrollTop,
4286 scrollBottom: this.scrollBottom,
4287 tabs: this.tabs
4288 };
4289 this.reset();
4290 this.normal = normal;
4291 this.showCursor();
4292 }
4293 break;
4294 }
4295 }
4296 };
4297 /**
4298 * CSI Pm l Reset Mode (RM).
4299 * Ps = 2 -> Keyboard Action Mode (AM).
4300 * Ps = 4 -> Replace Mode (IRM).
4301 * Ps = 1 2 -> Send/receive (SRM).
4302 * Ps = 2 0 -> Normal Linefeed (LNM).
4303 * CSI ? Pm l
4304 * DEC Private Mode Reset (DECRST).
4305 * Ps = 1 -> Normal Cursor Keys (DECCKM).
4306 * Ps = 2 -> Designate VT52 mode (DECANM).
4307 * Ps = 3 -> 80 Column Mode (DECCOLM).
4308 * Ps = 4 -> Jump (Fast) Scroll (DECSCLM).
4309 * Ps = 5 -> Normal Video (DECSCNM).
4310 * Ps = 6 -> Normal Cursor Mode (DECOM).
4311 * Ps = 7 -> No Wraparound Mode (DECAWM).
4312 * Ps = 8 -> No Auto-repeat Keys (DECARM).
4313 * Ps = 9 -> Don't send Mouse X & Y on button press.
4314 * Ps = 1 0 -> Hide toolbar (rxvt).
4315 * Ps = 1 2 -> Stop Blinking Cursor (att610).
4316 * Ps = 1 8 -> Don't print form feed (DECPFF).
4317 * Ps = 1 9 -> Limit print to scrolling region (DECPEX).
4318 * Ps = 2 5 -> Hide Cursor (DECTCEM).
4319 * Ps = 3 0 -> Don't show scrollbar (rxvt).
4320 * Ps = 3 5 -> Disable font-shifting functions (rxvt).
4321 * Ps = 4 0 -> Disallow 80 -> 132 Mode.
4322 * Ps = 4 1 -> No more(1) fix (see curses resource).
4323 * Ps = 4 2 -> Disable Nation Replacement Character sets (DEC-
4324 * NRCM).
4325 * Ps = 4 4 -> Turn Off Margin Bell.
4326 * Ps = 4 5 -> No Reverse-wraparound Mode.
4327 * Ps = 4 6 -> Stop Logging. (This is normally disabled by a
4328 * compile-time option).
4329 * Ps = 4 7 -> Use Normal Screen Buffer.
4330 * Ps = 6 6 -> Numeric keypad (DECNKM).
4331 * Ps = 6 7 -> Backarrow key sends delete (DECBKM).
4332 * Ps = 1 0 0 0 -> Don't send Mouse X & Y on button press and
4333 * release. See the section Mouse Tracking.
4334 * Ps = 1 0 0 1 -> Don't use Hilite Mouse Tracking.
4335 * Ps = 1 0 0 2 -> Don't use Cell Motion Mouse Tracking.
4336 * Ps = 1 0 0 3 -> Don't use All Motion Mouse Tracking.
4337 * Ps = 1 0 0 4 -> Don't send FocusIn/FocusOut events.
4338 * Ps = 1 0 0 5 -> Disable Extended Mouse Mode.
4339 * Ps = 1 0 1 0 -> Don't scroll to bottom on tty output
4340 * (rxvt).
4341 * Ps = 1 0 1 1 -> Don't scroll to bottom on key press (rxvt).
4342 * Ps = 1 0 3 4 -> Don't interpret "meta" key. (This disables
4343 * the eightBitInput resource).
4344 * Ps = 1 0 3 5 -> Disable special modifiers for Alt and Num-
4345 * Lock keys. (This disables the numLock resource).
4346 * Ps = 1 0 3 6 -> Don't send ESC when Meta modifies a key.
4347 * (This disables the metaSendsEscape resource).
4348 * Ps = 1 0 3 7 -> Send VT220 Remove from the editing-keypad
4349 * Delete key.
4350 * Ps = 1 0 3 9 -> Don't send ESC when Alt modifies a key.
4351 * (This disables the altSendsEscape resource).
4352 * Ps = 1 0 4 0 -> Do not keep selection when not highlighted.
4353 * (This disables the keepSelection resource).
4354 * Ps = 1 0 4 1 -> Use the PRIMARY selection. (This disables
4355 * the selectToClipboard resource).
4356 * Ps = 1 0 4 2 -> Disable Urgency window manager hint when
4357 * Control-G is received. (This disables the bellIsUrgent
4358 * resource).
4359 * Ps = 1 0 4 3 -> Disable raising of the window when Control-
4360 * G is received. (This disables the popOnBell resource).
4361 * Ps = 1 0 4 7 -> Use Normal Screen Buffer, clearing screen
4362 * first if in the Alternate Screen. (This may be disabled by
4363 * the titeInhibit resource).
4364 * Ps = 1 0 4 8 -> Restore cursor as in DECRC. (This may be
4365 * disabled by the titeInhibit resource).
4366 * Ps = 1 0 4 9 -> Use Normal Screen Buffer and restore cursor
4367 * as in DECRC. (This may be disabled by the titeInhibit
4368 * resource). This combines the effects of the 1 0 4 7 and 1 0
4369 * 4 8 modes. Use this with terminfo-based applications rather
4370 * than the 4 7 mode.
4371 * Ps = 1 0 5 0 -> Reset terminfo/termcap function-key mode.
4372 * Ps = 1 0 5 1 -> Reset Sun function-key mode.
4373 * Ps = 1 0 5 2 -> Reset HP function-key mode.
4374 * Ps = 1 0 5 3 -> Reset SCO function-key mode.
4375 * Ps = 1 0 6 0 -> Reset legacy keyboard emulation (X11R6).
4376 * Ps = 1 0 6 1 -> Reset keyboard emulation to Sun/PC style.
4377 * Ps = 2 0 0 4 -> Reset bracketed paste mode.
4378 */
4379 Terminal.prototype.resetMode = function (params) {
4380 if (typeof params === 'object') {
4381 var l = params.length, i = 0;
4382 for (; i < l; i++) {
4383 this.resetMode(params[i]);
4384 }
4385 return;
4386 }
4387 if (!this.prefix) {
4388 switch (params) {
4389 case 4:
4390 this.insertMode = false;
4391 break;
4392 case 20:
4393 //this.convertEol = false;
4394 break;
4395 }
4396 }
4397 else if (this.prefix === '?') {
4398 switch (params) {
4399 case 1:
4400 this.applicationCursor = false;
4401 break;
4402 case 3:
4403 if (this.cols === 132 && this.savedCols) {
4404 this.resize(this.savedCols, this.rows);
4405 }
4406 delete this.savedCols;
4407 break;
4408 case 6:
4409 this.originMode = false;
4410 break;
4411 case 7:
4412 this.wraparoundMode = false;
4413 break;
4414 case 12:
4415 // this.cursorBlink = false;
4416 break;
4417 case 66:
4418 this.log('Switching back to normal keypad.');
4419 this.applicationKeypad = false;
4420 this.viewport.syncScrollArea();
4421 break;
4422 case 9: // X10 Mouse
4423 case 1000: // vt200 mouse
4424 case 1002: // button event mouse
4425 case 1003:
4426 this.x10Mouse = false;
4427 this.vt200Mouse = false;
4428 this.normalMouse = false;
4429 this.mouseEvents = false;
4430 this.element.style.cursor = '';
4431 break;
4432 case 1004:
4433 this.sendFocus = false;
4434 break;
4435 case 1005:
4436 this.utfMouse = false;
4437 break;
4438 case 1006:
4439 this.sgrMouse = false;
4440 break;
4441 case 1015:
4442 this.urxvtMouse = false;
4443 break;
4444 case 25:
4445 this.cursorHidden = true;
4446 break;
4447 case 1049:
4448 ; // FALL-THROUGH
4449 case 47: // normal screen buffer
4450 case 1047:
4451 if (this.normal) {
4452 this.lines = this.normal.lines;
4453 this.ybase = this.normal.ybase;
4454 this.ydisp = this.normal.ydisp;
4455 this.x = this.normal.x;
4456 this.y = this.normal.y;
4457 this.scrollTop = this.normal.scrollTop;
4458 this.scrollBottom = this.normal.scrollBottom;
4459 this.tabs = this.normal.tabs;
4460 this.normal = null;
4461 // if (params === 1049) {
4462 // this.x = this.savedX;
4463 // this.y = this.savedY;
4464 // }
4465 this.refresh(0, this.rows - 1);
4466 this.showCursor();
4467 }
4468 break;
4469 }
4470 }
4471 };
4472 /**
4473 * CSI Ps ; Ps r
4474 * Set Scrolling Region [top;bottom] (default = full size of win-
4475 * dow) (DECSTBM).
4476 * CSI ? Pm r
4477 */
4478 Terminal.prototype.setScrollRegion = function (params) {
4479 if (this.prefix)
4480 return;
4481 this.scrollTop = (params[0] || 1) - 1;
4482 this.scrollBottom = (params[1] || this.rows) - 1;
4483 this.x = 0;
4484 this.y = 0;
4485 };
4486 /**
4487 * CSI s
4488 * Save cursor (ANSI.SYS).
4489 */
4490 Terminal.prototype.saveCursor = function (params) {
4491 this.savedX = this.x;
4492 this.savedY = this.y;
4493 };
4494 /**
4495 * CSI u
4496 * Restore cursor (ANSI.SYS).
4497 */
4498 Terminal.prototype.restoreCursor = function (params) {
4499 this.x = this.savedX || 0;
4500 this.y = this.savedY || 0;
4501 };
4502 /**
4503 * Lesser Used
4504 */
4505 /**
4506 * CSI Ps I
4507 * Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
4508 */
4509 Terminal.prototype.cursorForwardTab = function (params) {
4510 var param = params[0] || 1;
4511 while (param--) {
4512 this.x = this.nextStop();
4513 }
4514 };
4515 /**
4516 * CSI Ps S Scroll up Ps lines (default = 1) (SU).
4517 */
4518 Terminal.prototype.scrollUp = function (params) {
4519 var param = params[0] || 1;
4520 while (param--) {
4521 this.lines.splice(this.ybase + this.scrollTop, 1);
4522 this.lines.splice(this.ybase + this.scrollBottom, 0, this.blankLine());
4523 }
4524 // this.maxRange();
4525 this.updateRange(this.scrollTop);
4526 this.updateRange(this.scrollBottom);
4527 };
4528 /**
4529 * CSI Ps T Scroll down Ps lines (default = 1) (SD).
4530 */
4531 Terminal.prototype.scrollDown = function (params) {
4532 var param = params[0] || 1;
4533 while (param--) {
4534 this.lines.splice(this.ybase + this.scrollBottom, 1);
4535 this.lines.splice(this.ybase + this.scrollTop, 0, this.blankLine());
4536 }
4537 // this.maxRange();
4538 this.updateRange(this.scrollTop);
4539 this.updateRange(this.scrollBottom);
4540 };
4541 /**
4542 * CSI Ps ; Ps ; Ps ; Ps ; Ps T
4543 * Initiate highlight mouse tracking. Parameters are
4544 * [func;startx;starty;firstrow;lastrow]. See the section Mouse
4545 * Tracking.
4546 */
4547 Terminal.prototype.initMouseTracking = function (params) {
4548 // Relevant: DECSET 1001
4549 };
4550 /**
4551 * CSI > Ps; Ps T
4552 * Reset one or more features of the title modes to the default
4553 * value. Normally, "reset" disables the feature. It is possi-
4554 * ble to disable the ability to reset features by compiling a
4555 * different default for the title modes into xterm.
4556 * Ps = 0 -> Do not set window/icon labels using hexadecimal.
4557 * Ps = 1 -> Do not query window/icon labels using hexadeci-
4558 * mal.
4559 * Ps = 2 -> Do not set window/icon labels using UTF-8.
4560 * Ps = 3 -> Do not query window/icon labels using UTF-8.
4561 * (See discussion of "Title Modes").
4562 */
4563 Terminal.prototype.resetTitleModes = function (params) {
4564 ;
4565 };
4566 /**
4567 * CSI Ps Z Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
4568 */
4569 Terminal.prototype.cursorBackwardTab = function (params) {
4570 var param = params[0] || 1;
4571 while (param--) {
4572 this.x = this.prevStop();
4573 }
4574 };
4575 /**
4576 * CSI Ps b Repeat the preceding graphic character Ps times (REP).
4577 */
4578 Terminal.prototype.repeatPrecedingCharacter = function (params) {
4579 var param = params[0] || 1, line = this.lines[this.ybase + this.y], ch = line[this.x - 1] || [this.defAttr, ' ', 1];
4580 while (param--)
4581 line[this.x++] = ch;
4582 };
4583 /**
4584 * CSI Ps g Tab Clear (TBC).
4585 * Ps = 0 -> Clear Current Column (default).
4586 * Ps = 3 -> Clear All.
4587 * Potentially:
4588 * Ps = 2 -> Clear Stops on Line.
4589 * http://vt100.net/annarbor/aaa-ug/section6.html
4590 */
4591 Terminal.prototype.tabClear = function (params) {
4592 var param = params[0];
4593 if (param <= 0) {
4594 delete this.tabs[this.x];
4595 }
4596 else if (param === 3) {
4597 this.tabs = {};
4598 }
4599 };
4600 /**
4601 * CSI Pm i Media Copy (MC).
4602 * Ps = 0 -> Print screen (default).
4603 * Ps = 4 -> Turn off printer controller mode.
4604 * Ps = 5 -> Turn on printer controller mode.
4605 * CSI ? Pm i
4606 * Media Copy (MC, DEC-specific).
4607 * Ps = 1 -> Print line containing cursor.
4608 * Ps = 4 -> Turn off autoprint mode.
4609 * Ps = 5 -> Turn on autoprint mode.
4610 * Ps = 1 0 -> Print composed display, ignores DECPEX.
4611 * Ps = 1 1 -> Print all pages.
4612 */
4613 Terminal.prototype.mediaCopy = function (params) {
4614 ;
4615 };
4616 /**
4617 * CSI > Ps; Ps m
4618 * Set or reset resource-values used by xterm to decide whether
4619 * to construct escape sequences holding information about the
4620 * modifiers pressed with a given key. The first parameter iden-
4621 * tifies the resource to set/reset. The second parameter is the
4622 * value to assign to the resource. If the second parameter is
4623 * omitted, the resource is reset to its initial value.
4624 * Ps = 1 -> modifyCursorKeys.
4625 * Ps = 2 -> modifyFunctionKeys.
4626 * Ps = 4 -> modifyOtherKeys.
4627 * If no parameters are given, all resources are reset to their
4628 * initial values.
4629 */
4630 Terminal.prototype.setResources = function (params) {
4631 ;
4632 };
4633 /**
4634 * CSI > Ps n
4635 * Disable modifiers which may be enabled via the CSI > Ps; Ps m
4636 * sequence. This corresponds to a resource value of "-1", which
4637 * cannot be set with the other sequence. The parameter identi-
4638 * fies the resource to be disabled:
4639 * Ps = 1 -> modifyCursorKeys.
4640 * Ps = 2 -> modifyFunctionKeys.
4641 * Ps = 4 -> modifyOtherKeys.
4642 * If the parameter is omitted, modifyFunctionKeys is disabled.
4643 * When modifyFunctionKeys is disabled, xterm uses the modifier
4644 * keys to make an extended sequence of functions rather than
4645 * adding a parameter to each function key to denote the modi-
4646 * fiers.
4647 */
4648 Terminal.prototype.disableModifiers = function (params) {
4649 ;
4650 };
4651 /**
4652 * CSI > Ps p
4653 * Set resource value pointerMode. This is used by xterm to
4654 * decide whether to hide the pointer cursor as the user types.
4655 * Valid values for the parameter:
4656 * Ps = 0 -> never hide the pointer.
4657 * Ps = 1 -> hide if the mouse tracking mode is not enabled.
4658 * Ps = 2 -> always hide the pointer. If no parameter is
4659 * given, xterm uses the default, which is 1 .
4660 */
4661 Terminal.prototype.setPointerMode = function (params) {
4662 ;
4663 };
4664 /**
4665 * CSI ! p Soft terminal reset (DECSTR).
4666 * http://vt100.net/docs/vt220-rm/table4-10.html
4667 */
4668 Terminal.prototype.softReset = function (params) {
4669 this.cursorHidden = false;
4670 this.insertMode = false;
4671 this.originMode = false;
4672 this.wraparoundMode = false; // autowrap
4673 this.applicationKeypad = false; // ?
4674 this.viewport.syncScrollArea();
4675 this.applicationCursor = false;
4676 this.scrollTop = 0;
4677 this.scrollBottom = this.rows - 1;
4678 this.curAttr = this.defAttr;
4679 this.x = this.y = 0; // ?
4680 this.charset = null;
4681 this.glevel = 0; // ??
4682 this.charsets = [null]; // ??
4683 };
4684 /**
4685 * CSI Ps$ p
4686 * Request ANSI mode (DECRQM). For VT300 and up, reply is
4687 * CSI Ps; Pm$ y
4688 * where Ps is the mode number as in RM, and Pm is the mode
4689 * value:
4690 * 0 - not recognized
4691 * 1 - set
4692 * 2 - reset
4693 * 3 - permanently set
4694 * 4 - permanently reset
4695 */
4696 Terminal.prototype.requestAnsiMode = function (params) {
4697 ;
4698 };
4699 /**
4700 * CSI ? Ps$ p
4701 * Request DEC private mode (DECRQM). For VT300 and up, reply is
4702 * CSI ? Ps; Pm$ p
4703 * where Ps is the mode number as in DECSET, Pm is the mode value
4704 * as in the ANSI DECRQM.
4705 */
4706 Terminal.prototype.requestPrivateMode = function (params) {
4707 ;
4708 };
4709 /**
4710 * CSI Ps ; Ps " p
4711 * Set conformance level (DECSCL). Valid values for the first
4712 * parameter:
4713 * Ps = 6 1 -> VT100.
4714 * Ps = 6 2 -> VT200.
4715 * Ps = 6 3 -> VT300.
4716 * Valid values for the second parameter:
4717 * Ps = 0 -> 8-bit controls.
4718 * Ps = 1 -> 7-bit controls (always set for VT100).
4719 * Ps = 2 -> 8-bit controls.
4720 */
4721 Terminal.prototype.setConformanceLevel = function (params) {
4722 ;
4723 };
4724 /**
4725 * CSI Ps q Load LEDs (DECLL).
4726 * Ps = 0 -> Clear all LEDS (default).
4727 * Ps = 1 -> Light Num Lock.
4728 * Ps = 2 -> Light Caps Lock.
4729 * Ps = 3 -> Light Scroll Lock.
4730 * Ps = 2 1 -> Extinguish Num Lock.
4731 * Ps = 2 2 -> Extinguish Caps Lock.
4732 * Ps = 2 3 -> Extinguish Scroll Lock.
4733 */
4734 Terminal.prototype.loadLEDs = function (params) {
4735 ;
4736 };
4737 /**
4738 * CSI Ps SP q
4739 * Set cursor style (DECSCUSR, VT520).
4740 * Ps = 0 -> blinking block.
4741 * Ps = 1 -> blinking block (default).
4742 * Ps = 2 -> steady block.
4743 * Ps = 3 -> blinking underline.
4744 * Ps = 4 -> steady underline.
4745 */
4746 Terminal.prototype.setCursorStyle = function (params) {
4747 ;
4748 };
4749 /**
4750 * CSI Ps " q
4751 * Select character protection attribute (DECSCA). Valid values
4752 * for the parameter:
4753 * Ps = 0 -> DECSED and DECSEL can erase (default).
4754 * Ps = 1 -> DECSED and DECSEL cannot erase.
4755 * Ps = 2 -> DECSED and DECSEL can erase.
4756 */
4757 Terminal.prototype.setCharProtectionAttr = function (params) {
4758 ;
4759 };
4760 /**
4761 * CSI ? Pm r
4762 * Restore DEC Private Mode Values. The value of Ps previously
4763 * saved is restored. Ps values are the same as for DECSET.
4764 */
4765 Terminal.prototype.restorePrivateValues = function (params) {
4766 ;
4767 };
4768 /**
4769 * CSI Pt; Pl; Pb; Pr; Ps$ r
4770 * Change Attributes in Rectangular Area (DECCARA), VT400 and up.
4771 * Pt; Pl; Pb; Pr denotes the rectangle.
4772 * Ps denotes the SGR attributes to change: 0, 1, 4, 5, 7.
4773 * NOTE: xterm doesn't enable this code by default.
4774 */
4775 Terminal.prototype.setAttrInRectangle = function (params) {
4776 var t = params[0], l = params[1], b = params[2], r = params[3], attr = params[4];
4777 var line, i;
4778 for (; t < b + 1; t++) {
4779 line = this.lines[this.ybase + t];
4780 for (i = l; i < r; i++) {
4781 line[i] = [attr, line[i][1]];
4782 }
4783 }
4784 // this.maxRange();
4785 this.updateRange(params[0]);
4786 this.updateRange(params[2]);
4787 };
4788 /**
4789 * CSI Pc; Pt; Pl; Pb; Pr$ x
4790 * Fill Rectangular Area (DECFRA), VT420 and up.
4791 * Pc is the character to use.
4792 * Pt; Pl; Pb; Pr denotes the rectangle.
4793 * NOTE: xterm doesn't enable this code by default.
4794 */
4795 Terminal.prototype.fillRectangle = function (params) {
4796 var ch = params[0], t = params[1], l = params[2], b = params[3], r = params[4];
4797 var line, i;
4798 for (; t < b + 1; t++) {
4799 line = this.lines[this.ybase + t];
4800 for (i = l; i < r; i++) {
4801 line[i] = [line[i][0], String.fromCharCode(ch)];
4802 }
4803 }
4804 // this.maxRange();
4805 this.updateRange(params[1]);
4806 this.updateRange(params[3]);
4807 };
4808 /**
4809 * CSI Ps ; Pu ' z
4810 * Enable Locator Reporting (DECELR).
4811 * Valid values for the first parameter:
4812 * Ps = 0 -> Locator disabled (default).
4813 * Ps = 1 -> Locator enabled.
4814 * Ps = 2 -> Locator enabled for one report, then disabled.
4815 * The second parameter specifies the coordinate unit for locator
4816 * reports.
4817 * Valid values for the second parameter:
4818 * Pu = 0 <- or omitted -> default to character cells.
4819 * Pu = 1 <- device physical pixels.
4820 * Pu = 2 <- character cells.
4821 */
4822 Terminal.prototype.enableLocatorReporting = function (params) {
4823 var val = params[0] > 0;
4824 //this.mouseEvents = val;
4825 //this.decLocator = val;
4826 };
4827 /**
4828 * CSI Pt; Pl; Pb; Pr$ z
4829 * Erase Rectangular Area (DECERA), VT400 and up.
4830 * Pt; Pl; Pb; Pr denotes the rectangle.
4831 * NOTE: xterm doesn't enable this code by default.
4832 */
4833 Terminal.prototype.eraseRectangle = function (params) {
4834 var t = params[0], l = params[1], b = params[2], r = params[3];
4835 var line, i, ch;
4836 ch = [this.eraseAttr(), ' ', 1]; // xterm?
4837 for (; t < b + 1; t++) {
4838 line = this.lines[this.ybase + t];
4839 for (i = l; i < r; i++) {
4840 line[i] = ch;
4841 }
4842 }
4843 // this.maxRange();
4844 this.updateRange(params[0]);
4845 this.updateRange(params[2]);
4846 };
4847 /**
4848 * CSI P m SP }
4849 * Insert P s Column(s) (default = 1) (DECIC), VT420 and up.
4850 * NOTE: xterm doesn't enable this code by default.
4851 */
4852 Terminal.prototype.insertColumns = function () {
4853 var param = params[0], l = this.ybase + this.rows, ch = [this.eraseAttr(), ' ', 1] // xterm?
4854 , i;
4855 while (param--) {
4856 for (i = this.ybase; i < l; i++) {
4857 this.lines[i].splice(this.x + 1, 0, ch);
4858 this.lines[i].pop();
4859 }
4860 }
4861 this.maxRange();
4862 };
4863 /**
4864 * CSI P m SP ~
4865 * Delete P s Column(s) (default = 1) (DECDC), VT420 and up
4866 * NOTE: xterm doesn't enable this code by default.
4867 */
4868 Terminal.prototype.deleteColumns = function () {
4869 var param = params[0], l = this.ybase + this.rows, ch = [this.eraseAttr(), ' ', 1] // xterm?
4870 , i;
4871 while (param--) {
4872 for (i = this.ybase; i < l; i++) {
4873 this.lines[i].splice(this.x, 1);
4874 this.lines[i].push(ch);
4875 }
4876 }
4877 this.maxRange();
4878 };
4879 /**
4880 * Character Sets
4881 */
4882 Terminal.charsets = {};
4883 // DEC Special Character and Line Drawing Set.
4884 // http://vt100.net/docs/vt102-ug/table5-13.html
4885 // A lot of curses apps use this if they see TERM=xterm.
4886 // testing: echo -e '\e(0a\e(B'
4887 // The xterm output sometimes seems to conflict with the
4888 // reference above. xterm seems in line with the reference
4889 // when running vttest however.
4890 // The table below now uses xterm's output from vttest.
4891 Terminal.charsets.SCLD = {
4892 '`': '\u25c6',
4893 'a': '\u2592',
4894 'b': '\u0009',
4895 'c': '\u000c',
4896 'd': '\u000d',
4897 'e': '\u000a',
4898 'f': '\u00b0',
4899 'g': '\u00b1',
4900 'h': '\u2424',
4901 'i': '\u000b',
4902 'j': '\u2518',
4903 'k': '\u2510',
4904 'l': '\u250c',
4905 'm': '\u2514',
4906 'n': '\u253c',
4907 'o': '\u23ba',
4908 'p': '\u23bb',
4909 'q': '\u2500',
4910 'r': '\u23bc',
4911 's': '\u23bd',
4912 't': '\u251c',
4913 'u': '\u2524',
4914 'v': '\u2534',
4915 'w': '\u252c',
4916 'x': '\u2502',
4917 'y': '\u2264',
4918 'z': '\u2265',
4919 '{': '\u03c0',
4920 '|': '\u2260',
4921 '}': '\u00a3',
4922 '~': '\u00b7' // '·'
4923 };
4924 Terminal.charsets.UK = null; // (A
4925 Terminal.charsets.US = null; // (B (USASCII)
4926 Terminal.charsets.Dutch = null; // (4
4927 Terminal.charsets.Finnish = null; // (C or (5
4928 Terminal.charsets.French = null; // (R
4929 Terminal.charsets.FrenchCanadian = null; // (Q
4930 Terminal.charsets.German = null; // (K
4931 Terminal.charsets.Italian = null; // (Y
4932 Terminal.charsets.NorwegianDanish = null; // (E or (6
4933 Terminal.charsets.Spanish = null; // (Z
4934 Terminal.charsets.Swedish = null; // (H or (7
4935 Terminal.charsets.Swiss = null; // (=
4936 Terminal.charsets.ISOLatin = null; // /A
4937 /**
4938 * Helpers
4939 */
4940 function on(el, type, handler, capture) {
4941 if (!Array.isArray(el)) {
4942 el = [el];
4943 }
4944 el.forEach(function (element) {
4945 element.addEventListener(type, handler, capture || false);
4946 });
4947 }
4948 function off(el, type, handler, capture) {
4949 el.removeEventListener(type, handler, capture || false);
4950 }
4951 function cancel(ev, force) {
4952 if (!this.cancelEvents && !force) {
4953 return;
4954 }
4955 ev.preventDefault();
4956 ev.stopPropagation();
4957 return false;
4958 }
4959 function inherits(child, parent) {
4960 function f() {
4961 this.constructor = child;
4962 }
4963 f.prototype = parent.prototype;
4964 child.prototype = new f;
4965 }
4966 // if bold is broken, we can't
4967 // use it in the terminal.
4968 function isBoldBroken(document) {
4969 var body = document.getElementsByTagName('body')[0];
4970 var el = document.createElement('span');
4971 el.innerHTML = 'hello world';
4972 body.appendChild(el);
4973 var w1 = el.scrollWidth;
4974 el.style.fontWeight = 'bold';
4975 var w2 = el.scrollWidth;
4976 body.removeChild(el);
4977 return w1 !== w2;
4978 }
4979 function indexOf(obj, el) {
4980 var i = obj.length;
4981 while (i--) {
4982 if (obj[i] === el)
4983 return i;
4984 }
4985 return -1;
4986 }
4987 function isThirdLevelShift(term, ev) {
4988 var thirdLevelKey = (term.browser.isMac && ev.altKey && !ev.ctrlKey && !ev.metaKey) ||
4989 (term.browser.isMSWindows && ev.altKey && ev.ctrlKey && !ev.metaKey);
4990 if (ev.type == 'keypress') {
4991 return thirdLevelKey;
4992 }
4993 // Don't invoke for arrows, pageDown, home, backspace, etc. (on non-keypress events)
4994 return thirdLevelKey && (!ev.keyCode || ev.keyCode > 47);
4995 }
4996 function matchColor(r1, g1, b1) {
4997 var hash = (r1 << 16) | (g1 << 8) | b1;
4998 if (matchColor._cache[hash] != null) {
4999 return matchColor._cache[hash];
5000 }
5001 var ldiff = Infinity, li = -1, i = 0, c, r2, g2, b2, diff;
5002 for (; i < Terminal.vcolors.length; i++) {
5003 c = Terminal.vcolors[i];
5004 r2 = c[0];
5005 g2 = c[1];
5006 b2 = c[2];
5007 diff = matchColor.distance(r1, g1, b1, r2, g2, b2);
5008 if (diff === 0) {
5009 li = i;
5010 break;
5011 }
5012 if (diff < ldiff) {
5013 ldiff = diff;
5014 li = i;
5015 }
5016 }
5017 return matchColor._cache[hash] = li;
5018 }
5019 matchColor._cache = {};
5020 // http://stackoverflow.com/questions/1633828
5021 matchColor.distance = function (r1, g1, b1, r2, g2, b2) {
5022 return Math.pow(30 * (r1 - r2), 2)
5023 + Math.pow(59 * (g1 - g2), 2)
5024 + Math.pow(11 * (b1 - b2), 2);
5025 };
5026 function each(obj, iter, con) {
5027 if (obj.forEach)
5028 return obj.forEach(iter, con);
5029 for (var i = 0; i < obj.length; i++) {
5030 iter.call(con, obj[i], i, obj);
5031 }
5032 }
5033 function keys(obj) {
5034 if (Object.keys)
5035 return Object.keys(obj);
5036 var key, keys = [];
5037 for (key in obj) {
5038 if (Object.prototype.hasOwnProperty.call(obj, key)) {
5039 keys.push(key);
5040 }
5041 }
5042 return keys;
5043 }
5044 var wcwidth = (function (opts) {
5045 // extracted from https://www.cl.cam.ac.uk/%7Emgk25/ucs/wcwidth.c
5046 // combining characters
5047 var COMBINING = [
5048 [0x0300, 0x036F], [0x0483, 0x0486], [0x0488, 0x0489],
5049 [0x0591, 0x05BD], [0x05BF, 0x05BF], [0x05C1, 0x05C2],
5050 [0x05C4, 0x05C5], [0x05C7, 0x05C7], [0x0600, 0x0603],
5051 [0x0610, 0x0615], [0x064B, 0x065E], [0x0670, 0x0670],
5052 [0x06D6, 0x06E4], [0x06E7, 0x06E8], [0x06EA, 0x06ED],
5053 [0x070F, 0x070F], [0x0711, 0x0711], [0x0730, 0x074A],
5054 [0x07A6, 0x07B0], [0x07EB, 0x07F3], [0x0901, 0x0902],
5055 [0x093C, 0x093C], [0x0941, 0x0948], [0x094D, 0x094D],
5056 [0x0951, 0x0954], [0x0962, 0x0963], [0x0981, 0x0981],
5057 [0x09BC, 0x09BC], [0x09C1, 0x09C4], [0x09CD, 0x09CD],
5058 [0x09E2, 0x09E3], [0x0A01, 0x0A02], [0x0A3C, 0x0A3C],
5059 [0x0A41, 0x0A42], [0x0A47, 0x0A48], [0x0A4B, 0x0A4D],
5060 [0x0A70, 0x0A71], [0x0A81, 0x0A82], [0x0ABC, 0x0ABC],
5061 [0x0AC1, 0x0AC5], [0x0AC7, 0x0AC8], [0x0ACD, 0x0ACD],
5062 [0x0AE2, 0x0AE3], [0x0B01, 0x0B01], [0x0B3C, 0x0B3C],
5063 [0x0B3F, 0x0B3F], [0x0B41, 0x0B43], [0x0B4D, 0x0B4D],
5064 [0x0B56, 0x0B56], [0x0B82, 0x0B82], [0x0BC0, 0x0BC0],
5065 [0x0BCD, 0x0BCD], [0x0C3E, 0x0C40], [0x0C46, 0x0C48],
5066 [0x0C4A, 0x0C4D], [0x0C55, 0x0C56], [0x0CBC, 0x0CBC],
5067 [0x0CBF, 0x0CBF], [0x0CC6, 0x0CC6], [0x0CCC, 0x0CCD],
5068 [0x0CE2, 0x0CE3], [0x0D41, 0x0D43], [0x0D4D, 0x0D4D],
5069 [0x0DCA, 0x0DCA], [0x0DD2, 0x0DD4], [0x0DD6, 0x0DD6],
5070 [0x0E31, 0x0E31], [0x0E34, 0x0E3A], [0x0E47, 0x0E4E],
5071 [0x0EB1, 0x0EB1], [0x0EB4, 0x0EB9], [0x0EBB, 0x0EBC],
5072 [0x0EC8, 0x0ECD], [0x0F18, 0x0F19], [0x0F35, 0x0F35],
5073 [0x0F37, 0x0F37], [0x0F39, 0x0F39], [0x0F71, 0x0F7E],
5074 [0x0F80, 0x0F84], [0x0F86, 0x0F87], [0x0F90, 0x0F97],
5075 [0x0F99, 0x0FBC], [0x0FC6, 0x0FC6], [0x102D, 0x1030],
5076 [0x1032, 0x1032], [0x1036, 0x1037], [0x1039, 0x1039],
5077 [0x1058, 0x1059], [0x1160, 0x11FF], [0x135F, 0x135F],
5078 [0x1712, 0x1714], [0x1732, 0x1734], [0x1752, 0x1753],
5079 [0x1772, 0x1773], [0x17B4, 0x17B5], [0x17B7, 0x17BD],
5080 [0x17C6, 0x17C6], [0x17C9, 0x17D3], [0x17DD, 0x17DD],
5081 [0x180B, 0x180D], [0x18A9, 0x18A9], [0x1920, 0x1922],
5082 [0x1927, 0x1928], [0x1932, 0x1932], [0x1939, 0x193B],
5083 [0x1A17, 0x1A18], [0x1B00, 0x1B03], [0x1B34, 0x1B34],
5084 [0x1B36, 0x1B3A], [0x1B3C, 0x1B3C], [0x1B42, 0x1B42],
5085 [0x1B6B, 0x1B73], [0x1DC0, 0x1DCA], [0x1DFE, 0x1DFF],
5086 [0x200B, 0x200F], [0x202A, 0x202E], [0x2060, 0x2063],
5087 [0x206A, 0x206F], [0x20D0, 0x20EF], [0x302A, 0x302F],
5088 [0x3099, 0x309A], [0xA806, 0xA806], [0xA80B, 0xA80B],
5089 [0xA825, 0xA826], [0xFB1E, 0xFB1E], [0xFE00, 0xFE0F],
5090 [0xFE20, 0xFE23], [0xFEFF, 0xFEFF], [0xFFF9, 0xFFFB],
5091 [0x10A01, 0x10A03], [0x10A05, 0x10A06], [0x10A0C, 0x10A0F],
5092 [0x10A38, 0x10A3A], [0x10A3F, 0x10A3F], [0x1D167, 0x1D169],
5093 [0x1D173, 0x1D182], [0x1D185, 0x1D18B], [0x1D1AA, 0x1D1AD],
5094 [0x1D242, 0x1D244], [0xE0001, 0xE0001], [0xE0020, 0xE007F],
5095 [0xE0100, 0xE01EF]
5096 ];
5097 // binary search
5098 function bisearch(ucs) {
5099 var min = 0;
5100 var max = COMBINING.length - 1;
5101 var mid;
5102 if (ucs < COMBINING[0][0] || ucs > COMBINING[max][1])
5103 return false;
5104 while (max >= min) {
5105 mid = Math.floor((min + max) / 2);
5106 if (ucs > COMBINING[mid][1])
5107 min = mid + 1;
5108 else if (ucs < COMBINING[mid][0])
5109 max = mid - 1;
5110 else
5111 return true;
5112 }
5113 return false;
5114 }
5115 function wcwidth(ucs) {
5116 // test for 8-bit control characters
5117 if (ucs === 0)
5118 return opts.nul;
5119 if (ucs < 32 || (ucs >= 0x7f && ucs < 0xa0))
5120 return opts.control;
5121 // binary search in table of non-spacing characters
5122 if (bisearch(ucs))
5123 return 0;
5124 // if we arrive here, ucs is not a combining or C0/C1 control character
5125 return 1 +
5126 (ucs >= 0x1100 &&
5127 (ucs <= 0x115f ||
5128 ucs == 0x2329 ||
5129 ucs == 0x232a ||
5130 (ucs >= 0x2e80 && ucs <= 0xa4cf && ucs != 0x303f) ||
5131 (ucs >= 0xac00 && ucs <= 0xd7a3) ||
5132 (ucs >= 0xf900 && ucs <= 0xfaff) ||
5133 (ucs >= 0xfe10 && ucs <= 0xfe19) ||
5134 (ucs >= 0xfe30 && ucs <= 0xfe6f) ||
5135 (ucs >= 0xff00 && ucs <= 0xff60) ||
5136 (ucs >= 0xffe0 && ucs <= 0xffe6) ||
5137 (ucs >= 0x20000 && ucs <= 0x2fffd) ||
5138 (ucs >= 0x30000 && ucs <= 0x3fffd)));
5139 }
5140 return wcwidth;
5141 })({ nul: 0, control: 0 }); // configurable options
5142 /**
5143 * Expose
5144 */
5145 Terminal.EventEmitter = EventEmitter_js_1.EventEmitter;
5146 Terminal.inherits = inherits;
5147 /**
5148 * Adds an event listener to the terminal.
5149 *
5150 * @param {string} event The name of the event. TODO: Document all event types
5151 * @param {function} callback The function to call when the event is triggered.
5152 */
5153 Terminal.on = on;
5154 Terminal.off = off;
5155 Terminal.cancel = cancel;
5156 module.exports = Terminal;
5157
5158 },{"./CompositionHelper.js":1,"./EventEmitter.js":2,"./Viewport.js":3,"./handlers/Clipboard.js":4,"./utils/Browser":5}]},{},[7])(7)
5159 });
5160 //# sourceMappingURL=xterm.js.map