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