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