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