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