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