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