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