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