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