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