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