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