Debian Codegraph @

src/term.js

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