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