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