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