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