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