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