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