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