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