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