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