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