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