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