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