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