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