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