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