]>
Commit | Line | Data |
---|---|---|
8bc844c0 | 1 | /** |
5af18f8e | 2 | * xterm.js: xterm, in the browser |
8bc844c0 CJ |
3 | * Originally forked from (with the author's permission): |
4 | * Fabrice Bellard's javascript vt100 for jslinux: | |
5 | * http://bellard.org/jslinux/ | |
6 | * Copyright (c) 2011 Fabrice Bellard | |
7 | * The original design remains. The terminal itself | |
8 | * has been extended to include xterm CSI codes, among | |
9 | * other features. | |
1d300911 | 10 | * @license MIT |
8bc844c0 CJ |
11 | */ |
12 | ||
081fe3f3 PK |
13 | import { CompositionHelper } from './CompositionHelper'; |
14 | import { EventEmitter } from './EventEmitter'; | |
15 | import { Viewport } from './Viewport'; | |
16 | import { rightClickHandler, pasteHandler, copyHandler } from './handlers/Clipboard'; | |
17 | import { CircularList } from './utils/CircularList'; | |
3de3c0c6 | 18 | import { C0 } from './EscapeSequences'; |
04b1ebf1 | 19 | import { InputHandler } from './InputHandler'; |
1848fff4 | 20 | import { Parser } from './Parser'; |
92068f36 | 21 | import { Renderer } from './Renderer'; |
2207d356 | 22 | import { Linkifier } from './Linkifier'; |
70fda994 | 23 | import { SelectionManager } from './SelectionManager'; |
081fe3f3 | 24 | import { CharMeasure } from './utils/CharMeasure'; |
bc70b3b3 | 25 | import * as Browser from './utils/Browser'; |
70fda994 | 26 | import * as Mouse from './utils/Mouse'; |
1848fff4 | 27 | import { CHARSETS } from './Charsets'; |
ed1a31d1 | 28 | |
db76868c PK |
29 | /** |
30 | * Terminal Emulation References: | |
31 | * http://vt100.net/ | |
32 | * http://invisible-island.net/xterm/ctlseqs/ctlseqs.txt | |
33 | * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html | |
34 | * http://invisible-island.net/vttest/ | |
35 | * http://www.inwap.com/pdp10/ansicode.txt | |
36 | * http://linux.die.net/man/4/console_codes | |
37 | * http://linux.die.net/man/7/urxvt | |
38 | */ | |
8bc844c0 | 39 | |
db76868c PK |
40 | // Let it work inside Node.js for automated testing purposes. |
41 | var document = (typeof window != 'undefined') ? window.document : null; | |
8bc844c0 | 42 | |
e66b1c57 DI |
43 | /** |
44 | * The amount of write requests to queue before sending an XOFF signal to the | |
45 | * pty process. This number must be small in order for ^C and similar sequences | |
46 | * to be responsive. | |
47 | */ | |
2b8820fd DI |
48 | var WRITE_BUFFER_PAUSE_THRESHOLD = 5; |
49 | ||
50 | /** | |
51 | * The number of writes to perform in a single batch before allowing the | |
52 | * renderer to catch up with a 0ms setTimeout. | |
53 | */ | |
54 | var WRITE_BATCH_SIZE = 300; | |
e66b1c57 | 55 | |
aca81c76 DI |
56 | /** |
57 | * The time between cursor blinks. This is driven by JS rather than a CSS | |
58 | * animation due to a bug in Chromium that causes it to use excessive CPU time. | |
59 | * See https://github.com/Microsoft/vscode/issues/22900 | |
60 | */ | |
61 | var CURSOR_BLINK_INTERVAL = 600; | |
62 | ||
db76868c PK |
63 | /** |
64 | * Terminal | |
65 | */ | |
8bc844c0 | 66 | |
db76868c PK |
67 | /** |
68 | * Creates a new `Terminal` object. | |
69 | * | |
70 | * @param {object} options An object containing a set of options, the available options are: | |
a9417c68 PK |
71 | * - `cursorBlink` (boolean): Whether the terminal cursor blinks |
72 | * - `cols` (number): The number of columns of the terminal (horizontal size) | |
73 | * - `rows` (number): The number of rows of the terminal (vertical size) | |
db76868c PK |
74 | * |
75 | * @public | |
76 | * @class Xterm Xterm | |
77 | * @alias module:xterm/src/xterm | |
78 | */ | |
79 | function Terminal(options) { | |
80 | var self = this; | |
8bc844c0 | 81 | |
db76868c PK |
82 | if (!(this instanceof Terminal)) { |
83 | return new Terminal(arguments[0], arguments[1], arguments[2]); | |
84 | } | |
8bc844c0 | 85 | |
bc70b3b3 | 86 | self.browser = Browser; |
db76868c | 87 | self.cancel = Terminal.cancel; |
8bc844c0 | 88 | |
db76868c | 89 | EventEmitter.call(this); |
5fd1948b | 90 | |
db76868c PK |
91 | if (typeof options === 'number') { |
92 | options = { | |
93 | cols: arguments[0], | |
94 | rows: arguments[1], | |
95 | handler: arguments[2] | |
96 | }; | |
97 | } | |
8bc844c0 | 98 | |
db76868c | 99 | options = options || {}; |
8bc844c0 | 100 | |
86dad1b0 | 101 | |
db76868c PK |
102 | Object.keys(Terminal.defaults).forEach(function(key) { |
103 | if (options[key] == null) { | |
104 | options[key] = Terminal.options[key]; | |
91273161 | 105 | |
db76868c PK |
106 | if (Terminal[key] !== Terminal.defaults[key]) { |
107 | options[key] = Terminal[key]; | |
3f455f90 | 108 | } |
db76868c PK |
109 | } |
110 | self[key] = options[key]; | |
111 | }); | |
112 | ||
113 | if (options.colors.length === 8) { | |
114 | options.colors = options.colors.concat(Terminal._colors.slice(8)); | |
115 | } else if (options.colors.length === 16) { | |
116 | options.colors = options.colors.concat(Terminal._colors.slice(16)); | |
117 | } else if (options.colors.length === 10) { | |
118 | options.colors = options.colors.slice(0, -2).concat( | |
119 | Terminal._colors.slice(8, -2), options.colors.slice(-2)); | |
120 | } else if (options.colors.length === 18) { | |
121 | options.colors = options.colors.concat( | |
122 | Terminal._colors.slice(16, -2), options.colors.slice(-2)); | |
123 | } | |
124 | this.colors = options.colors; | |
125 | ||
126 | this.options = options; | |
127 | ||
128 | // this.context = options.context || window; | |
129 | // this.document = options.document || document; | |
130 | this.parent = options.body || options.parent || ( | |
131 | document ? document.getElementsByTagName('body')[0] : null | |
132 | ); | |
133 | ||
134 | this.cols = options.cols || options.geometry[0]; | |
135 | this.rows = options.rows || options.geometry[1]; | |
a9417c68 | 136 | this.geometry = [this.cols, this.rows]; |
db76868c PK |
137 | |
138 | if (options.handler) { | |
139 | this.on('data', options.handler); | |
140 | } | |
141 | ||
142 | /** | |
143 | * The scroll position of the y cursor, ie. ybase + y = the y position within the entire | |
144 | * buffer | |
145 | */ | |
146 | this.ybase = 0; | |
147 | ||
148 | /** | |
149 | * The scroll position of the viewport | |
150 | */ | |
151 | this.ydisp = 0; | |
152 | ||
153 | /** | |
154 | * The cursor's x position after ybase | |
155 | */ | |
156 | this.x = 0; | |
157 | ||
158 | /** | |
159 | * The cursor's y position after ybase | |
160 | */ | |
161 | this.y = 0; | |
162 | ||
db76868c PK |
163 | this.cursorState = 0; |
164 | this.cursorHidden = false; | |
165 | this.convertEol; | |
db76868c PK |
166 | this.queue = ''; |
167 | this.scrollTop = 0; | |
168 | this.scrollBottom = this.rows - 1; | |
169 | this.customKeydownHandler = null; | |
aca81c76 | 170 | this.cursorBlinkInterval = null; |
db76868c PK |
171 | |
172 | // modes | |
173 | this.applicationKeypad = false; | |
174 | this.applicationCursor = false; | |
175 | this.originMode = false; | |
176 | this.insertMode = false; | |
177 | this.wraparoundMode = true; // defaults: xterm - true, vt100 - false | |
178 | this.normal = null; | |
179 | ||
180 | // charset | |
181 | this.charset = null; | |
182 | this.gcharset = null; | |
183 | this.glevel = 0; | |
184 | this.charsets = [null]; | |
185 | ||
186 | // mouse properties | |
187 | this.decLocator; | |
188 | this.x10Mouse; | |
189 | this.vt200Mouse; | |
190 | this.vt300Mouse; | |
191 | this.normalMouse; | |
192 | this.mouseEvents; | |
193 | this.sendFocus; | |
194 | this.utfMouse; | |
195 | this.sgrMouse; | |
196 | this.urxvtMouse; | |
197 | ||
198 | // misc | |
199 | this.element; | |
200 | this.children; | |
201 | this.refreshStart; | |
202 | this.refreshEnd; | |
203 | this.savedX; | |
204 | this.savedY; | |
205 | this.savedCols; | |
206 | ||
207 | // stream | |
208 | this.readable = true; | |
209 | this.writable = true; | |
210 | ||
211 | this.defAttr = (0 << 18) | (257 << 9) | (256 << 0); | |
212 | this.curAttr = this.defAttr; | |
213 | ||
214 | this.params = []; | |
215 | this.currentParam = 0; | |
216 | this.prefix = ''; | |
217 | this.postfix = ''; | |
218 | ||
04b1ebf1 | 219 | this.inputHandler = new InputHandler(this); |
a31921ae | 220 | this.parser = new Parser(this.inputHandler, this); |
cc78fdd6 DI |
221 | // Reuse renderer if the Terminal is being recreated via a Terminal.reset call. |
222 | this.renderer = this.renderer || null; | |
70fda994 | 223 | this.selectionManager = this.selectionManager || null; |
b0624cad | 224 | this.linkifier = this.linkifier || new Linkifier(); |
04b1ebf1 | 225 | |
b9d374af DI |
226 | // user input states |
227 | this.writeBuffer = []; | |
228 | this.writeInProgress = false; | |
e66b1c57 DI |
229 | |
230 | /** | |
231 | * Whether _xterm.js_ sent XOFF in order to catch up with the pty process. | |
232 | * This is a distinct state from writeStopped so that if the user requested | |
233 | * XOFF via ^S that it will not automatically resume when the writeBuffer goes | |
234 | * below threshold. | |
235 | */ | |
236 | this.xoffSentToCatchUp = false; | |
237 | ||
238 | /** Whether writing has been stopped as a result of XOFF */ | |
239 | this.writeStopped = false; | |
b9d374af | 240 | |
db76868c PK |
241 | // leftover surrogate high from previous write invocation |
242 | this.surrogate_high = ''; | |
243 | ||
244 | /** | |
245 | * An array of all lines in the entire buffer, including the prompt. The lines are array of | |
246 | * characters which are 2-length arrays where [0] is an attribute and [1] is the character. | |
247 | */ | |
607c8191 | 248 | this.lines = new CircularList(this.scrollback); |
db76868c PK |
249 | var i = this.rows; |
250 | while (i--) { | |
251 | this.lines.push(this.blankLine()); | |
252 | } | |
253 | ||
254 | this.tabs; | |
255 | this.setupStops(); | |
5e68acfc MK |
256 | |
257 | // Store if user went browsing history in scrollback | |
258 | this.userScrolling = false; | |
db76868c PK |
259 | } |
260 | ||
261 | inherits(Terminal, EventEmitter); | |
262 | ||
263 | /** | |
264 | * back_color_erase feature for xterm. | |
265 | */ | |
266 | Terminal.prototype.eraseAttr = function() { | |
267 | // if (this.is('screen')) return this.defAttr; | |
268 | return (this.defAttr & ~0x1ff) | (this.curAttr & 0x1ff); | |
269 | }; | |
8bc844c0 | 270 | |
db76868c PK |
271 | /** |
272 | * Colors | |
273 | */ | |
91273161 | 274 | |
db76868c PK |
275 | // Colors 0-15 |
276 | Terminal.tangoColors = [ | |
277 | // dark: | |
278 | '#2e3436', | |
279 | '#cc0000', | |
280 | '#4e9a06', | |
281 | '#c4a000', | |
282 | '#3465a4', | |
283 | '#75507b', | |
284 | '#06989a', | |
285 | '#d3d7cf', | |
286 | // bright: | |
287 | '#555753', | |
288 | '#ef2929', | |
289 | '#8ae234', | |
290 | '#fce94f', | |
291 | '#729fcf', | |
292 | '#ad7fa8', | |
293 | '#34e2e2', | |
294 | '#eeeeec' | |
295 | ]; | |
296 | ||
297 | // Colors 0-15 + 16-255 | |
298 | // Much thanks to TooTallNate for writing this. | |
299 | Terminal.colors = (function() { | |
300 | var colors = Terminal.tangoColors.slice() | |
301 | , r = [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff] | |
302 | , i; | |
303 | ||
304 | // 16-231 | |
305 | i = 0; | |
306 | for (; i < 216; i++) { | |
307 | out(r[(i / 36) % 6 | 0], r[(i / 6) % 6 | 0], r[i % 6]); | |
308 | } | |
309 | ||
310 | // 232-255 (grey) | |
311 | i = 0; | |
312 | for (; i < 24; i++) { | |
313 | r = 8 + i * 10; | |
314 | out(r, r, r); | |
315 | } | |
316 | ||
317 | function out(r, g, b) { | |
318 | colors.push('#' + hex(r) + hex(g) + hex(b)); | |
319 | } | |
320 | ||
321 | function hex(c) { | |
322 | c = c.toString(16); | |
323 | return c.length < 2 ? '0' + c : c; | |
324 | } | |
325 | ||
326 | return colors; | |
327 | })(); | |
328 | ||
329 | Terminal._colors = Terminal.colors.slice(); | |
330 | ||
331 | Terminal.vcolors = (function() { | |
332 | var out = [] | |
333 | , colors = Terminal.colors | |
334 | , i = 0 | |
335 | , color; | |
336 | ||
337 | for (; i < 256; i++) { | |
338 | color = parseInt(colors[i].substring(1), 16); | |
339 | out.push([ | |
340 | (color >> 16) & 0xff, | |
341 | (color >> 8) & 0xff, | |
342 | color & 0xff | |
343 | ]); | |
344 | } | |
345 | ||
346 | return out; | |
347 | })(); | |
5fd1948b | 348 | |
db76868c PK |
349 | /** |
350 | * Options | |
351 | */ | |
3f455f90 | 352 | |
db76868c PK |
353 | Terminal.defaults = { |
354 | colors: Terminal.colors, | |
355 | theme: 'default', | |
356 | convertEol: false, | |
357 | termName: 'xterm', | |
358 | geometry: [80, 24], | |
359 | cursorBlink: false, | |
0bd469f5 | 360 | cursorStyle: 'block', |
db76868c PK |
361 | visualBell: false, |
362 | popOnBell: false, | |
363 | scrollback: 1000, | |
364 | screenKeys: false, | |
365 | debug: false, | |
d9d60063 | 366 | cancelEvents: false, |
f4293a6d | 367 | disableStdin: false, |
c2a69721 | 368 | useFlowControl: false, |
f4293a6d | 369 | tabStopWidth: 8 |
db76868c PK |
370 | // programFeatures: false, |
371 | // focusKeys: false, | |
372 | }; | |
373 | ||
374 | Terminal.options = {}; | |
375 | ||
376 | Terminal.focus = null; | |
377 | ||
378 | each(keys(Terminal.defaults), function(key) { | |
379 | Terminal[key] = Terminal.defaults[key]; | |
380 | Terminal.options[key] = Terminal.defaults[key]; | |
381 | }); | |
3f455f90 | 382 | |
db76868c PK |
383 | /** |
384 | * Focus the terminal. Delegates focus handling to the terminal's DOM element. | |
385 | */ | |
386 | Terminal.prototype.focus = function() { | |
387 | return this.textarea.focus(); | |
388 | }; | |
3f455f90 | 389 | |
4b459fe0 PK |
390 | /** |
391 | * Retrieves an option's value from the terminal. | |
392 | * @param {string} key The option key. | |
393 | */ | |
394 | Terminal.prototype.getOption = function(key, value) { | |
395 | if (!(key in Terminal.defaults)) { | |
396 | throw new Error('No option with key "' + key + '"'); | |
397 | } | |
398 | ||
53e8ac9b | 399 | if (typeof this.options[key] !== 'undefined') { |
4b459fe0 PK |
400 | return this.options[key]; |
401 | } | |
402 | ||
403 | return this[key]; | |
404 | }; | |
405 | ||
ab5cc0ad DI |
406 | /** |
407 | * Sets an option on the terminal. | |
15e56bd8 DI |
408 | * @param {string} key The option key. |
409 | * @param {string} value The option value. | |
ab5cc0ad DI |
410 | */ |
411 | Terminal.prototype.setOption = function(key, value) { | |
412 | if (!(key in Terminal.defaults)) { | |
413 | throw new Error('No option with key "' + key + '"'); | |
414 | } | |
5a932b2a DI |
415 | switch (key) { |
416 | case 'scrollback': | |
417 | if (this.options[key] !== value) { | |
418 | if (this.lines.length > value) { | |
419 | const amountToTrim = this.lines.length - value; | |
420 | const needsRefresh = (this.ydisp - amountToTrim < 0); | |
421 | this.lines.trimStart(amountToTrim); | |
422 | this.ybase = Math.max(this.ybase - amountToTrim, 0); | |
423 | this.ydisp = Math.max(this.ydisp - amountToTrim, 0); | |
424 | if (needsRefresh) { | |
425 | this.refresh(0, this.rows - 1); | |
426 | } | |
427 | } | |
428 | this.lines.maxLength = value; | |
429 | this.viewport.syncScrollArea(); | |
430 | } | |
431 | break; | |
432 | } | |
ab5cc0ad DI |
433 | this[key] = value; |
434 | this.options[key] = value; | |
7679475b | 435 | switch (key) { |
aca81c76 | 436 | case 'cursorBlink': this.setCursorBlinking(value); break; |
0bd469f5 DI |
437 | case 'cursorStyle': |
438 | // Style 'block' applies with no class | |
439 | this.element.classList.toggle(`xterm-cursor-style-underline`, value === 'underline'); | |
440 | this.element.classList.toggle(`xterm-cursor-style-bar`, value === 'bar'); | |
441 | break; | |
8cb46f27 | 442 | case 'tabStopWidth': this.setupStops(); break; |
7679475b | 443 | } |
ab5cc0ad DI |
444 | }; |
445 | ||
aca81c76 DI |
446 | Terminal.prototype.restartCursorBlinking = function () { |
447 | this.setCursorBlinking(this.options.cursorBlink); | |
0c4ba9fd | 448 | }; |
aca81c76 DI |
449 | |
450 | Terminal.prototype.setCursorBlinking = function (enabled) { | |
451 | this.element.classList.toggle('xterm-cursor-blink', enabled); | |
0c4ba9fd | 452 | this.clearCursorBlinkingInterval(); |
aca81c76 DI |
453 | if (enabled) { |
454 | var self = this; | |
455 | this.cursorBlinkInterval = setInterval(function () { | |
456 | self.element.classList.toggle('xterm-cursor-blink-on'); | |
457 | }, CURSOR_BLINK_INTERVAL); | |
458 | } | |
0c4ba9fd DI |
459 | }; |
460 | ||
461 | Terminal.prototype.clearCursorBlinkingInterval = function () { | |
462 | this.element.classList.remove('xterm-cursor-blink-on'); | |
463 | if (this.cursorBlinkInterval) { | |
464 | clearInterval(this.cursorBlinkInterval); | |
465 | this.cursorBlinkInterval = null; | |
466 | } | |
467 | }; | |
aca81c76 | 468 | |
db76868c PK |
469 | /** |
470 | * Binds the desired focus behavior on a given terminal object. | |
471 | * | |
472 | * @static | |
473 | */ | |
474 | Terminal.bindFocus = function (term) { | |
475 | on(term.textarea, 'focus', function (ev) { | |
476 | if (term.sendFocus) { | |
3de3c0c6 | 477 | term.send(C0.ESC + '[I'); |
86dad1b0 | 478 | } |
db76868c PK |
479 | term.element.classList.add('focus'); |
480 | term.showCursor(); | |
aca81c76 | 481 | term.restartCursorBlinking.apply(term); |
db76868c PK |
482 | Terminal.focus = term; |
483 | term.emit('focus', {terminal: term}); | |
484 | }); | |
485 | }; | |
8bc844c0 | 486 | |
db76868c PK |
487 | /** |
488 | * Blur the terminal. Delegates blur handling to the terminal's DOM element. | |
489 | */ | |
490 | Terminal.prototype.blur = function() { | |
491 | return this.textarea.blur(); | |
492 | }; | |
8bc844c0 | 493 | |
db76868c PK |
494 | /** |
495 | * Binds the desired blur behavior on a given terminal object. | |
496 | * | |
497 | * @static | |
498 | */ | |
499 | Terminal.bindBlur = function (term) { | |
500 | on(term.textarea, 'blur', function (ev) { | |
3c635f3c | 501 | term.refresh(term.y, term.y); |
db76868c | 502 | if (term.sendFocus) { |
3de3c0c6 | 503 | term.send(C0.ESC + '[O'); |
db76868c PK |
504 | } |
505 | term.element.classList.remove('focus'); | |
0c4ba9fd | 506 | term.clearCursorBlinkingInterval.apply(term); |
db76868c PK |
507 | Terminal.focus = null; |
508 | term.emit('blur', {terminal: term}); | |
509 | }); | |
510 | }; | |
a68c8336 | 511 | |
db76868c PK |
512 | /** |
513 | * Initialize default behavior | |
514 | */ | |
515 | Terminal.prototype.initGlobal = function() { | |
42a1e4ef PK |
516 | var term = this; |
517 | ||
db76868c | 518 | Terminal.bindKeys(this); |
db76868c PK |
519 | Terminal.bindFocus(this); |
520 | Terminal.bindBlur(this); | |
3f455f90 | 521 | |
42a1e4ef | 522 | // Bind clipboard functionality |
5808de64 | 523 | on(this.element, 'copy', function (ev) { |
524 | copyHandler.call(this, ev, term); | |
525 | }); | |
42a1e4ef PK |
526 | on(this.textarea, 'paste', function (ev) { |
527 | pasteHandler.call(this, ev, term); | |
528 | }); | |
ab87dece PK |
529 | on(this.element, 'paste', function (ev) { |
530 | pasteHandler.call(this, ev, term); | |
531 | }); | |
35637797 | 532 | |
35637797 | 533 | function rightClickHandlerWrapper (ev) { |
42a1e4ef | 534 | rightClickHandler.call(this, ev, term); |
35637797 PK |
535 | } |
536 | ||
547db926 | 537 | if (term.browser.isFirefox) { |
35637797 PK |
538 | on(this.element, 'mousedown', function (ev) { |
539 | if (ev.button == 2) { | |
540 | rightClickHandlerWrapper(ev); | |
541 | } | |
542 | }); | |
543 | } else { | |
544 | on(this.element, 'contextmenu', rightClickHandlerWrapper); | |
545 | } | |
db76868c | 546 | }; |
8bc844c0 | 547 | |
db76868c PK |
548 | /** |
549 | * Apply key handling to the terminal | |
550 | */ | |
551 | Terminal.bindKeys = function(term) { | |
552 | on(term.element, 'keydown', function(ev) { | |
553 | if (document.activeElement != this) { | |
554 | return; | |
555 | } | |
556 | term.keyDown(ev); | |
557 | }, true); | |
8bc844c0 | 558 | |
db76868c PK |
559 | on(term.element, 'keypress', function(ev) { |
560 | if (document.activeElement != this) { | |
561 | return; | |
562 | } | |
563 | term.keyPress(ev); | |
564 | }, true); | |
8bc844c0 | 565 | |
a05bb502 DI |
566 | on(term.element, 'keyup', function(ev) { |
567 | if (!wasMondifierKeyOnlyEvent(ev)) { | |
568 | term.focus(term); | |
569 | } | |
570 | }, true); | |
3b322929 | 571 | |
db76868c PK |
572 | on(term.textarea, 'keydown', function(ev) { |
573 | term.keyDown(ev); | |
574 | }, true); | |
31161ffe | 575 | |
db76868c PK |
576 | on(term.textarea, 'keypress', function(ev) { |
577 | term.keyPress(ev); | |
578 | // Truncate the textarea's value, since it is not needed | |
579 | this.value = ''; | |
580 | }, true); | |
8bc844c0 | 581 | |
db76868c PK |
582 | on(term.textarea, 'compositionstart', term.compositionHelper.compositionstart.bind(term.compositionHelper)); |
583 | on(term.textarea, 'compositionupdate', term.compositionHelper.compositionupdate.bind(term.compositionHelper)); | |
584 | on(term.textarea, 'compositionend', term.compositionHelper.compositionend.bind(term.compositionHelper)); | |
585 | term.on('refresh', term.compositionHelper.updateCompositionElements.bind(term.compositionHelper)); | |
a1d71c91 DI |
586 | term.on('refresh', function (data) { |
587 | term.queueLinkification(data.start, data.end) | |
588 | }); | |
db76868c | 589 | }; |
5fd1948b | 590 | |
3f455f90 | 591 | |
db76868c PK |
592 | /** |
593 | * Insert the given row to the terminal or produce a new one | |
594 | * if no row argument is passed. Return the inserted row. | |
595 | * @param {HTMLElement} row (optional) The row to append to the terminal. | |
596 | */ | |
597 | Terminal.prototype.insertRow = function (row) { | |
598 | if (typeof row != 'object') { | |
599 | row = document.createElement('div'); | |
600 | } | |
cc7f4d0d | 601 | |
db76868c PK |
602 | this.rowContainer.appendChild(row); |
603 | this.children.push(row); | |
cd956bca | 604 | |
db76868c PK |
605 | return row; |
606 | }; | |
7988f634 | 607 | |
db76868c PK |
608 | /** |
609 | * Opens the terminal within an element. | |
610 | * | |
611 | * @param {HTMLElement} parent The element to create the terminal within. | |
35f746c9 | 612 | * @param {boolean} focus Focus the terminal, after it gets instantiated in the DOM |
db76868c | 613 | */ |
35f746c9 | 614 | Terminal.prototype.open = function(parent, focus) { |
db76868c PK |
615 | var self=this, i=0, div; |
616 | ||
617 | this.parent = parent || this.parent; | |
618 | ||
619 | if (!this.parent) { | |
620 | throw new Error('Terminal requires a parent element.'); | |
621 | } | |
622 | ||
623 | // Grab global elements | |
624 | this.context = this.parent.ownerDocument.defaultView; | |
625 | this.document = this.parent.ownerDocument; | |
626 | this.body = this.document.getElementsByTagName('body')[0]; | |
627 | ||
db76868c PK |
628 | //Create main element container |
629 | this.element = this.document.createElement('div'); | |
630 | this.element.classList.add('terminal'); | |
631 | this.element.classList.add('xterm'); | |
632 | this.element.classList.add('xterm-theme-' + this.theme); | |
aca81c76 | 633 | this.setCursorBlinking(this.options.cursorBlink); |
db76868c | 634 | |
35f746c9 | 635 | this.element.style.height; |
db76868c PK |
636 | this.element.setAttribute('tabindex', 0); |
637 | ||
638 | this.viewportElement = document.createElement('div'); | |
639 | this.viewportElement.classList.add('xterm-viewport'); | |
640 | this.element.appendChild(this.viewportElement); | |
641 | this.viewportScrollArea = document.createElement('div'); | |
642 | this.viewportScrollArea.classList.add('xterm-scroll-area'); | |
643 | this.viewportElement.appendChild(this.viewportScrollArea); | |
644 | ||
207c4cf9 DI |
645 | // Create the selection container. This needs to be added before the |
646 | // rowContainer as the selection must be below the text. | |
647 | this.selectionContainer = document.createElement('div'); | |
648 | this.selectionContainer.classList.add('xterm-selection'); | |
649 | this.element.appendChild(this.selectionContainer); | |
650 | ||
db76868c PK |
651 | // Create the container that will hold the lines of the terminal and then |
652 | // produce the lines the lines. | |
653 | this.rowContainer = document.createElement('div'); | |
654 | this.rowContainer.classList.add('xterm-rows'); | |
655 | this.element.appendChild(this.rowContainer); | |
656 | this.children = []; | |
b0624cad | 657 | this.linkifier.attachToDom(document, this.children); |
db76868c PK |
658 | |
659 | // Create the container that will hold helpers like the textarea for | |
660 | // capturing DOM Events. Then produce the helpers. | |
661 | this.helperContainer = document.createElement('div'); | |
662 | this.helperContainer.classList.add('xterm-helpers'); | |
663 | // TODO: This should probably be inserted once it's filled to prevent an additional layout | |
664 | this.element.appendChild(this.helperContainer); | |
665 | this.textarea = document.createElement('textarea'); | |
666 | this.textarea.classList.add('xterm-helper-textarea'); | |
667 | this.textarea.setAttribute('autocorrect', 'off'); | |
668 | this.textarea.setAttribute('autocapitalize', 'off'); | |
669 | this.textarea.setAttribute('spellcheck', 'false'); | |
670 | this.textarea.tabIndex = 0; | |
671 | this.textarea.addEventListener('focus', function() { | |
672 | self.emit('focus', {terminal: self}); | |
673 | }); | |
674 | this.textarea.addEventListener('blur', function() { | |
675 | self.emit('blur', {terminal: self}); | |
676 | }); | |
677 | this.helperContainer.appendChild(this.textarea); | |
678 | ||
679 | this.compositionView = document.createElement('div'); | |
680 | this.compositionView.classList.add('composition-view'); | |
681 | this.compositionHelper = new CompositionHelper(this.textarea, this.compositionView, this); | |
682 | this.helperContainer.appendChild(this.compositionView); | |
683 | ||
74483fb2 DI |
684 | this.charSizeStyleElement = document.createElement('style'); |
685 | this.helperContainer.appendChild(this.charSizeStyleElement); | |
db76868c PK |
686 | |
687 | for (; i < this.rows; i++) { | |
688 | this.insertRow(); | |
689 | } | |
690 | this.parent.appendChild(this.element); | |
691 | ||
202b54af | 692 | this.charMeasure = new CharMeasure(document, this.helperContainer); |
74483fb2 DI |
693 | this.charMeasure.on('charsizechanged', function () { |
694 | self.updateCharSizeCSS(); | |
695 | }); | |
4f18d842 DI |
696 | this.charMeasure.measure(); |
697 | ||
74483fb2 | 698 | this.viewport = new Viewport(this, this.viewportElement, this.viewportScrollArea, this.charMeasure); |
92068f36 | 699 | this.renderer = new Renderer(this); |
ad3ae67e | 700 | this.selectionManager = new SelectionManager(this, this.lines, this.rowContainer, this.selectionContainer, this.charMeasure); |
b594407c | 701 | this.selectionManager.on('refresh', data => this.renderer.refreshSelection(data.start, data.end)); |
ad3ae67e DI |
702 | this.on('scroll', () => { |
703 | console.log('scroll'); | |
704 | this.selectionManager.refresh(); | |
705 | }); | |
db76868c | 706 | |
97feb332 | 707 | // Setup loop that draws to screen |
3c635f3c | 708 | this.refresh(0, this.rows - 1); |
db76868c PK |
709 | |
710 | // Initialize global actions that | |
711 | // need to be taken on the document. | |
712 | this.initGlobal(); | |
713 | ||
35f746c9 PK |
714 | /** |
715 | * Automatic focus functionality. | |
716 | * TODO: Default to `false` starting with xterm.js 3.0. | |
717 | */ | |
718 | if (typeof focus == 'undefined') { | |
719 | let message = 'You did not pass the `focus` argument in `Terminal.prototype.open()`.\n'; | |
720 | ||
721 | message += 'The `focus` argument now defaults to `true` but starting with xterm.js 3.0 '; | |
722 | message += 'it will default to `false`.'; | |
723 | ||
724 | console.warn(message); | |
725 | focus = true; | |
726 | } | |
727 | ||
728 | if (focus) { | |
729 | this.focus(); | |
730 | } | |
db76868c | 731 | |
4e1bbee6 | 732 | on(this.element, 'click', function() { |
db76868c PK |
733 | var selection = document.getSelection(), |
734 | collapsed = selection.isCollapsed, | |
735 | isRange = typeof collapsed == 'boolean' ? !collapsed : selection.type == 'Range'; | |
736 | if (!isRange) { | |
737 | self.focus(); | |
738 | } | |
739 | }); | |
3f455f90 | 740 | |
db76868c PK |
741 | // Listen for mouse events and translate |
742 | // them into terminal mouse protocols. | |
743 | this.bindMouse(); | |
8bc844c0 | 744 | |
5712365c Y |
745 | /** |
746 | * This event is emitted when terminal has completed opening. | |
747 | * | |
748 | * @event open | |
749 | */ | |
db76868c PK |
750 | this.emit('open'); |
751 | }; | |
8bc844c0 | 752 | |
8bc844c0 | 753 | |
db76868c PK |
754 | /** |
755 | * Attempts to load an add-on using CommonJS or RequireJS (whichever is available). | |
756 | * @param {string} addon The name of the addon to load | |
757 | * @static | |
758 | */ | |
759 | Terminal.loadAddon = function(addon, callback) { | |
760 | if (typeof exports === 'object' && typeof module === 'object') { | |
761 | // CommonJS | |
56ecc77d | 762 | return require('./addons/' + addon + '/' + addon); |
db76868c PK |
763 | } else if (typeof define == 'function') { |
764 | // RequireJS | |
56ecc77d | 765 | return require(['./addons/' + addon + '/' + addon], callback); |
db76868c PK |
766 | } else { |
767 | console.error('Cannot load a module without a CommonJS or RequireJS environment.'); | |
768 | return false; | |
769 | } | |
770 | }; | |
8bc844c0 | 771 | |
74483fb2 DI |
772 | /** |
773 | * Updates the helper CSS class with any changes necessary after the terminal's | |
774 | * character width has been changed. | |
775 | */ | |
776 | Terminal.prototype.updateCharSizeCSS = function() { | |
3baa6b92 | 777 | this.charSizeStyleElement.textContent = |
67ca3a93 DI |
778 | `.xterm-wide-char{width:${this.charMeasure.width * 2}px;}` + |
779 | `.xterm-normal-char{width:${this.charMeasure.width}px;}` | |
74483fb2 | 780 | } |
8bc844c0 | 781 | |
db76868c PK |
782 | /** |
783 | * XTerm mouse events | |
784 | * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#Mouse%20Tracking | |
785 | * To better understand these | |
786 | * the xterm code is very helpful: | |
787 | * Relevant files: | |
788 | * button.c, charproc.c, misc.c | |
789 | * Relevant functions in xterm/button.c: | |
790 | * BtnCode, EmitButtonCode, EditorButton, SendMousePosition | |
791 | */ | |
792 | Terminal.prototype.bindMouse = function() { | |
793 | var el = this.element, self = this, pressed = 32; | |
794 | ||
795 | // mouseup, mousedown, wheel | |
796 | // left click: ^[[M 3<^[[M#3< | |
797 | // wheel up: ^[[M`3> | |
798 | function sendButton(ev) { | |
799 | var button | |
800 | , pos; | |
801 | ||
802 | // get the xterm-style button | |
803 | button = getButton(ev); | |
804 | ||
805 | // get mouse coordinates | |
70fda994 | 806 | pos = Mouse.getCoords(ev, this.rowContainer, this.charMeasure); |
db76868c PK |
807 | if (!pos) return; |
808 | ||
809 | sendEvent(button, pos); | |
810 | ||
811 | switch (ev.overrideType || ev.type) { | |
812 | case 'mousedown': | |
813 | pressed = button; | |
814 | break; | |
815 | case 'mouseup': | |
816 | // keep it at the left | |
817 | // button, just in case. | |
818 | pressed = 32; | |
819 | break; | |
820 | case 'wheel': | |
821 | // nothing. don't | |
822 | // interfere with | |
823 | // `pressed`. | |
824 | break; | |
825 | } | |
826 | } | |
827 | ||
828 | // motion example of a left click: | |
829 | // ^[[M 3<^[[M@4<^[[M@5<^[[M@6<^[[M@7<^[[M#7< | |
830 | function sendMove(ev) { | |
831 | var button = pressed | |
832 | , pos; | |
833 | ||
70fda994 | 834 | pos = Mouse.getCoords(ev, this.rowContainer, this.charMeasure); |
db76868c PK |
835 | if (!pos) return; |
836 | ||
837 | // buttons marked as motions | |
838 | // are incremented by 32 | |
839 | button += 32; | |
840 | ||
841 | sendEvent(button, pos); | |
842 | } | |
843 | ||
844 | // encode button and | |
845 | // position to characters | |
846 | function encode(data, ch) { | |
847 | if (!self.utfMouse) { | |
848 | if (ch === 255) return data.push(0); | |
849 | if (ch > 127) ch = 127; | |
850 | data.push(ch); | |
851 | } else { | |
852 | if (ch === 2047) return data.push(0); | |
853 | if (ch < 127) { | |
854 | data.push(ch); | |
855 | } else { | |
856 | if (ch > 2047) ch = 2047; | |
857 | data.push(0xC0 | (ch >> 6)); | |
858 | data.push(0x80 | (ch & 0x3F)); | |
859 | } | |
860 | } | |
861 | } | |
862 | ||
863 | // send a mouse event: | |
864 | // regular/utf8: ^[[M Cb Cx Cy | |
865 | // urxvt: ^[[ Cb ; Cx ; Cy M | |
866 | // sgr: ^[[ Cb ; Cx ; Cy M/m | |
867 | // vt300: ^[[ 24(1/3/5)~ [ Cx , Cy ] \r | |
868 | // locator: CSI P e ; P b ; P r ; P c ; P p & w | |
869 | function sendEvent(button, pos) { | |
870 | // self.emit('mouse', { | |
871 | // x: pos.x - 32, | |
872 | // y: pos.x - 32, | |
873 | // button: button | |
874 | // }); | |
875 | ||
876 | if (self.vt300Mouse) { | |
877 | // NOTE: Unstable. | |
878 | // http://www.vt100.net/docs/vt3xx-gp/chapter15.html | |
879 | button &= 3; | |
880 | pos.x -= 32; | |
881 | pos.y -= 32; | |
3de3c0c6 | 882 | var data = C0.ESC + '[24'; |
db76868c PK |
883 | if (button === 0) data += '1'; |
884 | else if (button === 1) data += '3'; | |
885 | else if (button === 2) data += '5'; | |
886 | else if (button === 3) return; | |
887 | else data += '0'; | |
888 | data += '~[' + pos.x + ',' + pos.y + ']\r'; | |
889 | self.send(data); | |
890 | return; | |
891 | } | |
00f4232e | 892 | |
db76868c PK |
893 | if (self.decLocator) { |
894 | // NOTE: Unstable. | |
895 | button &= 3; | |
896 | pos.x -= 32; | |
897 | pos.y -= 32; | |
898 | if (button === 0) button = 2; | |
899 | else if (button === 1) button = 4; | |
900 | else if (button === 2) button = 6; | |
901 | else if (button === 3) button = 3; | |
3de3c0c6 | 902 | self.send(C0.ESC + '[' |
db76868c PK |
903 | + button |
904 | + ';' | |
905 | + (button === 3 ? 4 : 0) | |
906 | + ';' | |
907 | + pos.y | |
908 | + ';' | |
909 | + pos.x | |
910 | + ';' | |
911 | + (pos.page || 0) | |
912 | + '&w'); | |
913 | return; | |
914 | } | |
00f4232e | 915 | |
db76868c PK |
916 | if (self.urxvtMouse) { |
917 | pos.x -= 32; | |
918 | pos.y -= 32; | |
919 | pos.x++; | |
920 | pos.y++; | |
3de3c0c6 | 921 | self.send(C0.ESC + '[' + button + ';' + pos.x + ';' + pos.y + 'M'); |
db76868c PK |
922 | return; |
923 | } | |
8bc844c0 | 924 | |
db76868c PK |
925 | if (self.sgrMouse) { |
926 | pos.x -= 32; | |
927 | pos.y -= 32; | |
3de3c0c6 | 928 | self.send(C0.ESC + '[<' |
a75bafc4 | 929 | + (((button & 3) === 3 ? button & ~3 : button) - 32) |
db76868c PK |
930 | + ';' |
931 | + pos.x | |
932 | + ';' | |
933 | + pos.y | |
934 | + ((button & 3) === 3 ? 'm' : 'M')); | |
935 | return; | |
936 | } | |
8bc844c0 | 937 | |
db76868c PK |
938 | var data = []; |
939 | ||
940 | encode(data, button); | |
941 | encode(data, pos.x); | |
942 | encode(data, pos.y); | |
943 | ||
3de3c0c6 | 944 | self.send(C0.ESC + '[M' + String.fromCharCode.apply(String, data)); |
db76868c PK |
945 | } |
946 | ||
947 | function getButton(ev) { | |
948 | var button | |
949 | , shift | |
950 | , meta | |
951 | , ctrl | |
952 | , mod; | |
953 | ||
954 | // two low bits: | |
955 | // 0 = left | |
956 | // 1 = middle | |
957 | // 2 = right | |
958 | // 3 = release | |
959 | // wheel up/down: | |
960 | // 1, and 2 - with 64 added | |
961 | switch (ev.overrideType || ev.type) { | |
962 | case 'mousedown': | |
963 | button = ev.button != null | |
964 | ? +ev.button | |
965 | : ev.which != null | |
966 | ? ev.which - 1 | |
967 | : null; | |
968 | ||
bc70b3b3 | 969 | if (self.browser.isMSIE) { |
db76868c PK |
970 | button = button === 1 ? 0 : button === 4 ? 1 : button; |
971 | } | |
972 | break; | |
973 | case 'mouseup': | |
974 | button = 3; | |
975 | break; | |
976 | case 'DOMMouseScroll': | |
977 | button = ev.detail < 0 | |
978 | ? 64 | |
979 | : 65; | |
980 | break; | |
981 | case 'wheel': | |
982 | button = ev.wheelDeltaY > 0 | |
983 | ? 64 | |
984 | : 65; | |
985 | break; | |
986 | } | |
8bc844c0 | 987 | |
db76868c PK |
988 | // next three bits are the modifiers: |
989 | // 4 = shift, 8 = meta, 16 = control | |
990 | shift = ev.shiftKey ? 4 : 0; | |
991 | meta = ev.metaKey ? 8 : 0; | |
992 | ctrl = ev.ctrlKey ? 16 : 0; | |
993 | mod = shift | meta | ctrl; | |
994 | ||
995 | // no mods | |
996 | if (self.vt200Mouse) { | |
997 | // ctrl only | |
998 | mod &= ctrl; | |
999 | } else if (!self.normalMouse) { | |
1000 | mod = 0; | |
1001 | } | |
8bc844c0 | 1002 | |
db76868c PK |
1003 | // increment to SP |
1004 | button = (32 + (mod << 2)) + button; | |
a52b7e7a | 1005 | |
db76868c PK |
1006 | return button; |
1007 | } | |
8bc844c0 | 1008 | |
db76868c | 1009 | // mouse coordinates measured in cols/rows |
70fda994 DI |
1010 | // function getCoords(ev) { |
1011 | // var x, y, w, h, el; | |
1012 | ||
1013 | // // ignore browsers without pageX for now | |
1014 | // if (ev.pageX == null) return; | |
1015 | ||
1016 | // x = ev.pageX; | |
1017 | // y = ev.pageY; | |
1018 | // el = self.rowContainer; | |
1019 | ||
1020 | // // should probably check offsetParent | |
1021 | // // but this is more portable | |
1022 | // while (el && el !== self.document.documentElement) { | |
1023 | // x -= el.offsetLeft; | |
1024 | // y -= el.offsetTop; | |
1025 | // el = 'offsetParent' in el | |
1026 | // ? el.offsetParent | |
1027 | // : el.parentNode; | |
1028 | // } | |
1029 | ||
1030 | // // convert to cols/rows | |
1031 | // x = Math.ceil(x / self.charMeasure.width); | |
1032 | // y = Math.ceil(y / self.charMeasure.height); | |
1033 | ||
1034 | // // be sure to avoid sending | |
1035 | // // bad positions to the program | |
1036 | // if (x < 0) x = 0; | |
1037 | // if (x > self.cols) x = self.cols; | |
1038 | // if (y < 0) y = 0; | |
1039 | // if (y > self.rows) y = self.rows; | |
1040 | ||
1041 | // // xterm sends raw bytes and | |
1042 | // // starts at 32 (SP) for each. | |
1043 | // x += 32; | |
1044 | // y += 32; | |
1045 | ||
1046 | // return { | |
1047 | // x: x, | |
1048 | // y: y, | |
1049 | // type: 'wheel' | |
1050 | // }; | |
1051 | // } | |
8bc844c0 | 1052 | |
db76868c PK |
1053 | on(el, 'mousedown', function(ev) { |
1054 | if (!self.mouseEvents) return; | |
8bc844c0 | 1055 | |
db76868c PK |
1056 | // send the button |
1057 | sendButton(ev); | |
8bc844c0 | 1058 | |
db76868c PK |
1059 | // ensure focus |
1060 | self.focus(); | |
8bc844c0 | 1061 | |
db76868c PK |
1062 | // fix for odd bug |
1063 | //if (self.vt200Mouse && !self.normalMouse) { | |
1064 | if (self.vt200Mouse) { | |
1065 | ev.overrideType = 'mouseup'; | |
1066 | sendButton(ev); | |
1067 | return self.cancel(ev); | |
1068 | } | |
8bc844c0 | 1069 | |
db76868c PK |
1070 | // bind events |
1071 | if (self.normalMouse) on(self.document, 'mousemove', sendMove); | |
b01165c1 | 1072 | |
db76868c PK |
1073 | // x10 compatibility mode can't send button releases |
1074 | if (!self.x10Mouse) { | |
1075 | on(self.document, 'mouseup', function up(ev) { | |
1076 | sendButton(ev); | |
1077 | if (self.normalMouse) off(self.document, 'mousemove', sendMove); | |
1078 | off(self.document, 'mouseup', up); | |
1079 | return self.cancel(ev); | |
4595a181 | 1080 | }); |
db76868c | 1081 | } |
29000fb7 | 1082 | |
db76868c PK |
1083 | return self.cancel(ev); |
1084 | }); | |
1085 | ||
1086 | //if (self.normalMouse) { | |
1087 | // on(self.document, 'mousemove', sendMove); | |
1088 | //} | |
1089 | ||
1090 | on(el, 'wheel', function(ev) { | |
1091 | if (!self.mouseEvents) return; | |
1092 | if (self.x10Mouse | |
1093 | || self.vt300Mouse | |
1094 | || self.decLocator) return; | |
1095 | sendButton(ev); | |
1096 | return self.cancel(ev); | |
1097 | }); | |
1098 | ||
1099 | // allow wheel scrolling in | |
1100 | // the shell for example | |
1101 | on(el, 'wheel', function(ev) { | |
1102 | if (self.mouseEvents) return; | |
db76868c PK |
1103 | self.viewport.onWheel(ev); |
1104 | return self.cancel(ev); | |
1105 | }); | |
1106 | }; | |
fc7b22dc | 1107 | |
db76868c PK |
1108 | /** |
1109 | * Destroys the terminal. | |
1110 | */ | |
1111 | Terminal.prototype.destroy = function() { | |
1112 | this.readable = false; | |
1113 | this.writable = false; | |
1114 | this._events = {}; | |
1115 | this.handler = function() {}; | |
1116 | this.write = function() {}; | |
833eb521 | 1117 | if (this.element && this.element.parentNode) { |
db76868c PK |
1118 | this.element.parentNode.removeChild(this.element); |
1119 | } | |
1120 | //this.emit('close'); | |
1121 | }; | |
670b0d58 | 1122 | |
db76868c | 1123 | /** |
92068f36 DI |
1124 | * Tells the renderer to refresh terminal content between two rows (inclusive) at the next |
1125 | * opportunity. | |
a1d71c91 DI |
1126 | * @param {number} start The row to start from (between 0 and this.rows - 1). |
1127 | * @param {number} end The row to end at (between start and this.rows - 1). | |
7234bfb6 | 1128 | */ |
92068f36 | 1129 | Terminal.prototype.refresh = function(start, end) { |
92068f36 DI |
1130 | if (this.renderer) { |
1131 | this.renderer.queueRefresh(start, end); | |
a1d71c91 DI |
1132 | } |
1133 | }; | |
2207d356 | 1134 | |
a1d71c91 DI |
1135 | /** |
1136 | * Queues linkification for the specified rows. | |
1137 | * @param {number} start The row to start from (between 0 and this.rows - 1). | |
1138 | * @param {number} end The row to end at (between start and this.rows - 1). | |
1139 | */ | |
1140 | Terminal.prototype.queueLinkification = function(start, end) { | |
1141 | if (this.linkifier) { | |
2207d356 DI |
1142 | for (let i = start; i <= end; i++) { |
1143 | this.linkifier.linkifyRow(i); | |
1144 | } | |
db76868c | 1145 | } |
a1d71c91 | 1146 | } |
8bc844c0 | 1147 | |
db76868c PK |
1148 | /** |
1149 | * Display the cursor element | |
1150 | */ | |
1151 | Terminal.prototype.showCursor = function() { | |
1152 | if (!this.cursorState) { | |
1153 | this.cursorState = 1; | |
3c635f3c | 1154 | this.refresh(this.y, this.y); |
db76868c PK |
1155 | } |
1156 | }; | |
8bc844c0 | 1157 | |
db76868c | 1158 | /** |
be56c72b | 1159 | * Scroll the terminal down 1 row, creating a blank line. |
db76868c PK |
1160 | */ |
1161 | Terminal.prototype.scroll = function() { | |
1162 | var row; | |
1163 | ||
c3f46e4a DI |
1164 | // Make room for the new row in lines |
1165 | if (this.lines.length === this.lines.maxLength) { | |
1166 | this.lines.trimStart(1); | |
1167 | this.ybase--; | |
1168 | if (this.ydisp !== 0) { | |
1169 | this.ydisp--; | |
1170 | } | |
1171 | } | |
1172 | ||
30a1f1cc | 1173 | this.ybase++; |
db76868c | 1174 | |
3b35d12e | 1175 | // TODO: Why is this done twice? |
5e68acfc MK |
1176 | if (!this.userScrolling) { |
1177 | this.ydisp = this.ybase; | |
1178 | } | |
db76868c PK |
1179 | |
1180 | // last line | |
1181 | row = this.ybase + this.rows - 1; | |
1182 | ||
1183 | // subtract the bottom scroll region | |
1184 | row -= this.rows - 1 - this.scrollBottom; | |
1185 | ||
6f7cb990 DI |
1186 | if (row === this.lines.length) { |
1187 | // Optimization: pushing is faster than splicing when they amount to the same behavior | |
1188 | this.lines.push(this.blankLine()); | |
1189 | } else { | |
1190 | // add our new line | |
1191 | this.lines.splice(row, 0, this.blankLine()); | |
1192 | } | |
db76868c PK |
1193 | |
1194 | if (this.scrollTop !== 0) { | |
1195 | if (this.ybase !== 0) { | |
1196 | this.ybase--; | |
5e68acfc MK |
1197 | if (!this.userScrolling) { |
1198 | this.ydisp = this.ybase; | |
1199 | } | |
db76868c PK |
1200 | } |
1201 | this.lines.splice(this.ybase + this.scrollTop, 1); | |
1202 | } | |
8bc844c0 | 1203 | |
db76868c PK |
1204 | // this.maxRange(); |
1205 | this.updateRange(this.scrollTop); | |
1206 | this.updateRange(this.scrollBottom); | |
8bc844c0 | 1207 | |
6dcf7267 Y |
1208 | /** |
1209 | * This event is emitted whenever the terminal is scrolled. | |
1210 | * The one parameter passed is the new y display position. | |
1211 | * | |
1212 | * @event scroll | |
1213 | */ | |
db76868c PK |
1214 | this.emit('scroll', this.ydisp); |
1215 | }; | |
1216 | ||
1217 | /** | |
1218 | * Scroll the display of the terminal | |
1219 | * @param {number} disp The number of lines to scroll down (negatives scroll up). | |
1220 | * @param {boolean} suppressScrollEvent Don't emit the scroll event as scrollDisp. This is used | |
1221 | * to avoid unwanted events being handled by the veiwport when the event was triggered from the | |
1222 | * viewport originally. | |
1223 | */ | |
1224 | Terminal.prototype.scrollDisp = function(disp, suppressScrollEvent) { | |
5e68acfc MK |
1225 | if (disp < 0) { |
1226 | this.userScrolling = true; | |
1227 | } else if (disp + this.ydisp >= this.ybase) { | |
1228 | this.userScrolling = false; | |
1229 | } | |
1230 | ||
db76868c | 1231 | this.ydisp += disp; |
8bc844c0 | 1232 | |
db76868c PK |
1233 | if (this.ydisp > this.ybase) { |
1234 | this.ydisp = this.ybase; | |
1235 | } else if (this.ydisp < 0) { | |
1236 | this.ydisp = 0; | |
1237 | } | |
8bc844c0 | 1238 | |
db76868c PK |
1239 | if (!suppressScrollEvent) { |
1240 | this.emit('scroll', this.ydisp); | |
1241 | } | |
8bc844c0 | 1242 | |
3c635f3c | 1243 | this.refresh(0, this.rows - 1); |
db76868c | 1244 | }; |
8bc844c0 | 1245 | |
fe0d878b DI |
1246 | /** |
1247 | * Scroll the display of the terminal by a number of pages. | |
0ad02a4a | 1248 | * @param {number} pageCount The number of pages to scroll (negative scrolls up). |
fe0d878b DI |
1249 | */ |
1250 | Terminal.prototype.scrollPages = function(pageCount) { | |
1251 | this.scrollDisp(pageCount * (this.rows - 1)); | |
1252 | } | |
1253 | ||
0bf7bf56 DI |
1254 | /** |
1255 | * Scrolls the display of the terminal to the top. | |
1256 | */ | |
e5d130b6 DI |
1257 | Terminal.prototype.scrollToTop = function() { |
1258 | this.scrollDisp(-this.ydisp); | |
1259 | } | |
1260 | ||
0bf7bf56 DI |
1261 | /** |
1262 | * Scrolls the display of the terminal to the bottom. | |
1263 | */ | |
e5d130b6 DI |
1264 | Terminal.prototype.scrollToBottom = function() { |
1265 | this.scrollDisp(this.ybase - this.ydisp); | |
1266 | } | |
1267 | ||
db76868c PK |
1268 | /** |
1269 | * Writes text to the terminal. | |
1270 | * @param {string} text The text to write to the terminal. | |
1271 | */ | |
1272 | Terminal.prototype.write = function(data) { | |
b9d374af | 1273 | this.writeBuffer.push(data); |
6b8c43ed | 1274 | |
e66b1c57 DI |
1275 | // Send XOFF to pause the pty process if the write buffer becomes too large so |
1276 | // xterm.js can catch up before more data is sent. This is necessary in order | |
1277 | // to keep signals such as ^C responsive. | |
c2a69721 | 1278 | if (this.options.useFlowControl && !this.xoffSentToCatchUp && this.writeBuffer.length >= WRITE_BUFFER_PAUSE_THRESHOLD) { |
6b8c43ed DI |
1279 | // XOFF - stop pty pipe |
1280 | // XON will be triggered by emulator before processing data chunk | |
0ec12555 | 1281 | this.send(C0.DC3); |
e66b1c57 | 1282 | this.xoffSentToCatchUp = true; |
6b8c43ed DI |
1283 | } |
1284 | ||
1285 | if (!this.writeInProgress && this.writeBuffer.length > 0) { | |
b9d374af DI |
1286 | // Kick off a write which will write all data in sequence recursively |
1287 | this.writeInProgress = true; | |
1288 | // Kick off an async innerWrite so more writes can come in while processing data | |
6b8c43ed DI |
1289 | var self = this; |
1290 | setTimeout(function () { | |
dc5efa88 | 1291 | self.innerWrite(); |
6b8c43ed | 1292 | }); |
b9d374af DI |
1293 | } |
1294 | } | |
1295 | ||
dc5efa88 | 1296 | Terminal.prototype.innerWrite = function() { |
2b8820fd DI |
1297 | var writeBatch = this.writeBuffer.splice(0, WRITE_BATCH_SIZE); |
1298 | while (writeBatch.length > 0) { | |
1299 | var data = writeBatch.shift(); | |
dc5efa88 | 1300 | var l = data.length, i = 0, j, cs, ch, code, low, ch_width, row; |
e66b1c57 | 1301 | |
dc5efa88 DI |
1302 | // If XOFF was sent in order to catch up with the pty process, resume it if |
1303 | // the writeBuffer is empty to allow more data to come in. | |
2b8820fd | 1304 | if (this.xoffSentToCatchUp && writeBatch.length === 0 && this.writeBuffer.length === 0) { |
0ec12555 | 1305 | this.send(C0.DC1); |
dc5efa88 DI |
1306 | this.xoffSentToCatchUp = false; |
1307 | } | |
db76868c | 1308 | |
dc5efa88 DI |
1309 | this.refreshStart = this.y; |
1310 | this.refreshEnd = this.y; | |
db76868c | 1311 | |
342e862d | 1312 | this.parser.parse(data); |
3f455f90 | 1313 | |
dc5efa88 | 1314 | this.updateRange(this.y); |
3c635f3c | 1315 | this.refresh(this.refreshStart, this.refreshEnd); |
b9d374af | 1316 | } |
2b8820fd DI |
1317 | if (this.writeBuffer.length > 0) { |
1318 | // Allow renderer to catch up before processing the next batch | |
1319 | var self = this; | |
1320 | setTimeout(function () { | |
1321 | self.innerWrite(); | |
1322 | }, 0); | |
1323 | } else { | |
1324 | this.writeInProgress = false; | |
1325 | } | |
db76868c | 1326 | }; |
3f455f90 | 1327 | |
db76868c PK |
1328 | /** |
1329 | * Writes text to the terminal, followed by a break line character (\n). | |
1330 | * @param {string} text The text to write to the terminal. | |
1331 | */ | |
1332 | Terminal.prototype.writeln = function(data) { | |
1333 | this.write(data + '\r\n'); | |
1334 | }; | |
3f455f90 | 1335 | |
db76868c PK |
1336 | /** |
1337 | * Attaches a custom keydown handler which is run before keys are processed, giving consumers of | |
1338 | * xterm.js ultimate control as to what keys should be processed by the terminal and what keys | |
1339 | * should not. | |
1340 | * @param {function} customKeydownHandler The custom KeyboardEvent handler to attach. This is a | |
1341 | * function that takes a KeyboardEvent, allowing consumers to stop propogation and/or prevent | |
1342 | * the default action. The function returns whether the event should be processed by xterm.js. | |
1343 | */ | |
1344 | Terminal.prototype.attachCustomKeydownHandler = function(customKeydownHandler) { | |
1345 | this.customKeydownHandler = customKeydownHandler; | |
1346 | } | |
3f455f90 | 1347 | |
a1d71c91 DI |
1348 | /** |
1349 | * Attaches a http(s) link handler, forcing web links to behave differently to | |
1350 | * regular <a> tags. This will trigger a refresh as links potentially need to be | |
1351 | * reconstructed. Calling this with null will remove the handler. | |
1352 | * @param {LinkHandler} handler The handler callback function. | |
1353 | */ | |
11f62bab | 1354 | Terminal.prototype.setHypertextLinkHandler = function(handler) { |
0f3ee21d DI |
1355 | if (!this.linkifier) { |
1356 | throw new Error('Cannot attach a hypertext link handler before Terminal.open is called'); | |
1357 | } | |
bef7137c | 1358 | this.linkifier.setHypertextLinkHandler(handler); |
0f3ee21d DI |
1359 | // Refresh to force links to refresh |
1360 | this.refresh(0, this.rows - 1); | |
1361 | } | |
1362 | ||
11f62bab DI |
1363 | /** |
1364 | * Attaches a validation callback for hypertext links. This is useful to use | |
1365 | * validation logic or to do something with the link's element and url. | |
1366 | * @param {LinkMatcherValidationCallback} callback The callback to use, this can | |
1367 | * be cleared with null. | |
1368 | */ | |
1369 | Terminal.prototype.setHypertextValidationCallback = function(handler) { | |
1370 | if (!this.linkifier) { | |
1371 | throw new Error('Cannot attach a hypertext validation callback before Terminal.open is called'); | |
1372 | } | |
1373 | this.linkifier.setHypertextValidationCallback(handler); | |
1374 | // Refresh to force links to refresh | |
1375 | this.refresh(0, this.rows - 1); | |
1376 | } | |
1377 | ||
c8bb3216 | 1378 | /** |
3b62aa44 DI |
1379 | * Registers a link matcher, allowing custom link patterns to be matched and |
1380 | * handled. | |
1381 | * @param {RegExp} regex The regular expression to search for, specifically | |
1382 | * this searches the textContent of the rows. You will want to use \s to match | |
1383 | * a space ' ' character for example. | |
1384 | * @param {LinkHandler} handler The callback when the link is called. | |
6198556e | 1385 | * @param {LinkMatcherOptions} [options] Options for the link matcher. |
3b62aa44 | 1386 | * @return {number} The ID of the new matcher, this can be used to deregister. |
c8bb3216 | 1387 | */ |
6198556e | 1388 | Terminal.prototype.registerLinkMatcher = function(regex, handler, options) { |
c8bb3216 | 1389 | if (this.linkifier) { |
6198556e | 1390 | var matcherId = this.linkifier.registerLinkMatcher(regex, handler, options); |
1c030f57 DI |
1391 | this.refresh(0, this.rows - 1); |
1392 | return matcherId; | |
c8bb3216 DI |
1393 | } |
1394 | } | |
1395 | ||
1396 | /** | |
1397 | * Deregisters a link matcher if it has been registered. | |
1398 | * @param {number} matcherId The link matcher's ID (returned after register) | |
1399 | */ | |
1400 | Terminal.prototype.deregisterLinkMatcher = function(matcherId) { | |
1401 | if (this.linkifier) { | |
1c030f57 DI |
1402 | if (this.linkifier.deregisterLinkMatcher(matcherId)) { |
1403 | this.refresh(0, this.rows - 1); | |
1404 | } | |
c8bb3216 DI |
1405 | } |
1406 | } | |
1407 | ||
db76868c PK |
1408 | /** |
1409 | * Handle a keydown event | |
1410 | * Key Resources: | |
1411 | * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent | |
1412 | * @param {KeyboardEvent} ev The keydown event to be handled. | |
1413 | */ | |
1414 | Terminal.prototype.keyDown = function(ev) { | |
c15fed38 DI |
1415 | if (this.customKeydownHandler && this.customKeydownHandler(ev) === false) { |
1416 | return false; | |
1417 | } | |
1418 | ||
aca81c76 DI |
1419 | this.restartCursorBlinking(); |
1420 | ||
db76868c | 1421 | if (!this.compositionHelper.keydown.bind(this.compositionHelper)(ev)) { |
3b2e89d8 DI |
1422 | if (this.ybase !== this.ydisp) { |
1423 | this.scrollToBottom(); | |
1424 | } | |
db76868c PK |
1425 | return false; |
1426 | } | |
3f455f90 | 1427 | |
db76868c PK |
1428 | var self = this; |
1429 | var result = this.evaluateKeyEscapeSequence(ev); | |
3f455f90 | 1430 | |
0ec12555 | 1431 | if (result.key === C0.DC3) { // XOFF |
e66b1c57 | 1432 | this.writeStopped = true; |
0ec12555 | 1433 | } else if (result.key === C0.DC1) { // XON |
e66b1c57 | 1434 | this.writeStopped = false; |
b9d374af DI |
1435 | } |
1436 | ||
db76868c PK |
1437 | if (result.scrollDisp) { |
1438 | this.scrollDisp(result.scrollDisp); | |
446c3958 | 1439 | return this.cancel(ev, true); |
db76868c | 1440 | } |
3f455f90 | 1441 | |
db76868c PK |
1442 | if (isThirdLevelShift(this, ev)) { |
1443 | return true; | |
1444 | } | |
3f455f90 | 1445 | |
446c3958 | 1446 | if (result.cancel) { |
db76868c PK |
1447 | // The event is canceled at the end already, is this necessary? |
1448 | this.cancel(ev, true); | |
1449 | } | |
3f455f90 | 1450 | |
db76868c PK |
1451 | if (!result.key) { |
1452 | return true; | |
1453 | } | |
3f455f90 | 1454 | |
db76868c PK |
1455 | this.emit('keydown', ev); |
1456 | this.emit('key', result.key, ev); | |
1457 | this.showCursor(); | |
1458 | this.handler(result.key); | |
3f455f90 | 1459 | |
db76868c PK |
1460 | return this.cancel(ev, true); |
1461 | }; | |
3f455f90 | 1462 | |
db76868c PK |
1463 | /** |
1464 | * Returns an object that determines how a KeyboardEvent should be handled. The key of the | |
1465 | * returned value is the new key code to pass to the PTY. | |
1466 | * | |
1467 | * Reference: http://invisible-island.net/xterm/ctlseqs/ctlseqs.html | |
1468 | * @param {KeyboardEvent} ev The keyboard event to be translated to key escape sequence. | |
1469 | */ | |
1470 | Terminal.prototype.evaluateKeyEscapeSequence = function(ev) { | |
1471 | var result = { | |
1472 | // Whether to cancel event propogation (NOTE: this may not be needed since the event is | |
1473 | // canceled at the end of keyDown | |
1474 | cancel: false, | |
1475 | // The new key even to emit | |
1476 | key: undefined, | |
1477 | // The number of characters to scroll, if this is defined it will cancel the event | |
1478 | scrollDisp: undefined | |
1479 | }; | |
1480 | var modifiers = ev.shiftKey << 0 | ev.altKey << 1 | ev.ctrlKey << 2 | ev.metaKey << 3; | |
1481 | switch (ev.keyCode) { | |
db76868c | 1482 | case 8: |
fca673d6 | 1483 | // backspace |
db76868c | 1484 | if (ev.shiftKey) { |
3de3c0c6 | 1485 | result.key = C0.BS; // ^H |
db76868c PK |
1486 | break; |
1487 | } | |
3de3c0c6 | 1488 | result.key = C0.DEL; // ^? |
db76868c | 1489 | break; |
db76868c | 1490 | case 9: |
fca673d6 | 1491 | // tab |
db76868c | 1492 | if (ev.shiftKey) { |
3de3c0c6 | 1493 | result.key = C0.ESC + '[Z'; |
db76868c PK |
1494 | break; |
1495 | } | |
3de3c0c6 | 1496 | result.key = C0.HT; |
db76868c PK |
1497 | result.cancel = true; |
1498 | break; | |
db76868c | 1499 | case 13: |
fca673d6 | 1500 | // return/enter |
3de3c0c6 | 1501 | result.key = C0.CR; |
db76868c PK |
1502 | result.cancel = true; |
1503 | break; | |
db76868c | 1504 | case 27: |
fca673d6 | 1505 | // escape |
3de3c0c6 | 1506 | result.key = C0.ESC; |
db76868c PK |
1507 | result.cancel = true; |
1508 | break; | |
db76868c | 1509 | case 37: |
fca673d6 | 1510 | // left-arrow |
db76868c | 1511 | if (modifiers) { |
3de3c0c6 | 1512 | result.key = C0.ESC + '[1;' + (modifiers + 1) + 'D'; |
db76868c PK |
1513 | // HACK: Make Alt + left-arrow behave like Ctrl + left-arrow: move one word backwards |
1514 | // http://unix.stackexchange.com/a/108106 | |
2824da37 | 1515 | // macOS uses different escape sequences than linux |
3de3c0c6 DI |
1516 | if (result.key == C0.ESC + '[1;3D') { |
1517 | result.key = (this.browser.isMac) ? C0.ESC + 'b' : C0.ESC + '[1;5D'; | |
3f455f90 | 1518 | } |
db76868c | 1519 | } else if (this.applicationCursor) { |
3de3c0c6 | 1520 | result.key = C0.ESC + 'OD'; |
db76868c | 1521 | } else { |
3de3c0c6 | 1522 | result.key = C0.ESC + '[D'; |
3f455f90 | 1523 | } |
db76868c | 1524 | break; |
db76868c | 1525 | case 39: |
fca673d6 | 1526 | // right-arrow |
db76868c | 1527 | if (modifiers) { |
3de3c0c6 | 1528 | result.key = C0.ESC + '[1;' + (modifiers + 1) + 'C'; |
db76868c PK |
1529 | // HACK: Make Alt + right-arrow behave like Ctrl + right-arrow: move one word forward |
1530 | // http://unix.stackexchange.com/a/108106 | |
2824da37 | 1531 | // macOS uses different escape sequences than linux |
3de3c0c6 DI |
1532 | if (result.key == C0.ESC + '[1;3C') { |
1533 | result.key = (this.browser.isMac) ? C0.ESC + 'f' : C0.ESC + '[1;5C'; | |
db76868c PK |
1534 | } |
1535 | } else if (this.applicationCursor) { | |
3de3c0c6 | 1536 | result.key = C0.ESC + 'OC'; |
db76868c | 1537 | } else { |
3de3c0c6 | 1538 | result.key = C0.ESC + '[C'; |
d4e9d34d | 1539 | } |
db76868c | 1540 | break; |
db76868c | 1541 | case 38: |
fca673d6 | 1542 | // up-arrow |
db76868c | 1543 | if (modifiers) { |
3de3c0c6 | 1544 | result.key = C0.ESC + '[1;' + (modifiers + 1) + 'A'; |
db76868c PK |
1545 | // HACK: Make Alt + up-arrow behave like Ctrl + up-arrow |
1546 | // http://unix.stackexchange.com/a/108106 | |
3de3c0c6 DI |
1547 | if (result.key == C0.ESC + '[1;3A') { |
1548 | result.key = C0.ESC + '[1;5A'; | |
db76868c PK |
1549 | } |
1550 | } else if (this.applicationCursor) { | |
3de3c0c6 | 1551 | result.key = C0.ESC + 'OA'; |
db76868c | 1552 | } else { |
3de3c0c6 | 1553 | result.key = C0.ESC + '[A'; |
8faea59e | 1554 | } |
db76868c | 1555 | break; |
db76868c | 1556 | case 40: |
fca673d6 | 1557 | // down-arrow |
db76868c | 1558 | if (modifiers) { |
3de3c0c6 | 1559 | result.key = C0.ESC + '[1;' + (modifiers + 1) + 'B'; |
db76868c PK |
1560 | // HACK: Make Alt + down-arrow behave like Ctrl + down-arrow |
1561 | // http://unix.stackexchange.com/a/108106 | |
3de3c0c6 DI |
1562 | if (result.key == C0.ESC + '[1;3B') { |
1563 | result.key = C0.ESC + '[1;5B'; | |
db76868c PK |
1564 | } |
1565 | } else if (this.applicationCursor) { | |
3de3c0c6 | 1566 | result.key = C0.ESC + 'OB'; |
db76868c | 1567 | } else { |
3de3c0c6 | 1568 | result.key = C0.ESC + '[B'; |
3a866cf2 | 1569 | } |
db76868c | 1570 | break; |
db76868c | 1571 | case 45: |
fca673d6 | 1572 | // insert |
db76868c PK |
1573 | if (!ev.shiftKey && !ev.ctrlKey) { |
1574 | // <Ctrl> or <Shift> + <Insert> are used to | |
1575 | // copy-paste on some systems. | |
3de3c0c6 | 1576 | result.key = C0.ESC + '[2~'; |
b01165c1 | 1577 | } |
db76868c | 1578 | break; |
db76868c | 1579 | case 46: |
fca673d6 | 1580 | // delete |
db76868c | 1581 | if (modifiers) { |
3de3c0c6 | 1582 | result.key = C0.ESC + '[3;' + (modifiers + 1) + '~'; |
db76868c | 1583 | } else { |
3de3c0c6 | 1584 | result.key = C0.ESC + '[3~'; |
3a866cf2 | 1585 | } |
db76868c | 1586 | break; |
db76868c | 1587 | case 36: |
fca673d6 | 1588 | // home |
db76868c | 1589 | if (modifiers) |
3de3c0c6 | 1590 | result.key = C0.ESC + '[1;' + (modifiers + 1) + 'H'; |
db76868c | 1591 | else if (this.applicationCursor) |
3de3c0c6 | 1592 | result.key = C0.ESC + 'OH'; |
db76868c | 1593 | else |
3de3c0c6 | 1594 | result.key = C0.ESC + '[H'; |
db76868c | 1595 | break; |
db76868c | 1596 | case 35: |
fca673d6 | 1597 | // end |
db76868c | 1598 | if (modifiers) |
3de3c0c6 | 1599 | result.key = C0.ESC + '[1;' + (modifiers + 1) + 'F'; |
db76868c | 1600 | else if (this.applicationCursor) |
3de3c0c6 | 1601 | result.key = C0.ESC + 'OF'; |
db76868c | 1602 | else |
3de3c0c6 | 1603 | result.key = C0.ESC + '[F'; |
db76868c | 1604 | break; |
db76868c | 1605 | case 33: |
fca673d6 | 1606 | // page up |
db76868c PK |
1607 | if (ev.shiftKey) { |
1608 | result.scrollDisp = -(this.rows - 1); | |
1609 | } else { | |
3de3c0c6 | 1610 | result.key = C0.ESC + '[5~'; |
3a866cf2 | 1611 | } |
db76868c | 1612 | break; |
db76868c | 1613 | case 34: |
fca673d6 | 1614 | // page down |
db76868c PK |
1615 | if (ev.shiftKey) { |
1616 | result.scrollDisp = this.rows - 1; | |
1617 | } else { | |
3de3c0c6 | 1618 | result.key = C0.ESC + '[6~'; |
3f455f90 | 1619 | } |
db76868c | 1620 | break; |
db76868c | 1621 | case 112: |
fca673d6 | 1622 | // F1-F12 |
db76868c | 1623 | if (modifiers) { |
3de3c0c6 | 1624 | result.key = C0.ESC + '[1;' + (modifiers + 1) + 'P'; |
db76868c | 1625 | } else { |
3de3c0c6 | 1626 | result.key = C0.ESC + 'OP'; |
3f455f90 | 1627 | } |
db76868c PK |
1628 | break; |
1629 | case 113: | |
1630 | if (modifiers) { | |
3de3c0c6 | 1631 | result.key = C0.ESC + '[1;' + (modifiers + 1) + 'Q'; |
3f455f90 | 1632 | } else { |
3de3c0c6 | 1633 | result.key = C0.ESC + 'OQ'; |
3f455f90 | 1634 | } |
db76868c PK |
1635 | break; |
1636 | case 114: | |
1637 | if (modifiers) { | |
3de3c0c6 | 1638 | result.key = C0.ESC + '[1;' + (modifiers + 1) + 'R'; |
db76868c | 1639 | } else { |
3de3c0c6 | 1640 | result.key = C0.ESC + 'OR'; |
3f455f90 | 1641 | } |
db76868c PK |
1642 | break; |
1643 | case 115: | |
1644 | if (modifiers) { | |
3de3c0c6 | 1645 | result.key = C0.ESC + '[1;' + (modifiers + 1) + 'S'; |
db76868c | 1646 | } else { |
3de3c0c6 | 1647 | result.key = C0.ESC + 'OS'; |
3f455f90 | 1648 | } |
db76868c PK |
1649 | break; |
1650 | case 116: | |
1651 | if (modifiers) { | |
3de3c0c6 | 1652 | result.key = C0.ESC + '[15;' + (modifiers + 1) + '~'; |
db76868c | 1653 | } else { |
3de3c0c6 | 1654 | result.key = C0.ESC + '[15~'; |
e721bdc9 | 1655 | } |
db76868c PK |
1656 | break; |
1657 | case 117: | |
1658 | if (modifiers) { | |
3de3c0c6 | 1659 | result.key = C0.ESC + '[17;' + (modifiers + 1) + '~'; |
db76868c | 1660 | } else { |
3de3c0c6 | 1661 | result.key = C0.ESC + '[17~'; |
3f455f90 | 1662 | } |
db76868c PK |
1663 | break; |
1664 | case 118: | |
1665 | if (modifiers) { | |
3de3c0c6 | 1666 | result.key = C0.ESC + '[18;' + (modifiers + 1) + '~'; |
db76868c | 1667 | } else { |
3de3c0c6 | 1668 | result.key = C0.ESC + '[18~'; |
3f455f90 | 1669 | } |
db76868c PK |
1670 | break; |
1671 | case 119: | |
1672 | if (modifiers) { | |
3de3c0c6 | 1673 | result.key = C0.ESC + '[19;' + (modifiers + 1) + '~'; |
db76868c | 1674 | } else { |
3de3c0c6 | 1675 | result.key = C0.ESC + '[19~'; |
3f455f90 | 1676 | } |
db76868c PK |
1677 | break; |
1678 | case 120: | |
1679 | if (modifiers) { | |
3de3c0c6 | 1680 | result.key = C0.ESC + '[20;' + (modifiers + 1) + '~'; |
db76868c | 1681 | } else { |
3de3c0c6 | 1682 | result.key = C0.ESC + '[20~'; |
eee99f62 | 1683 | } |
db76868c PK |
1684 | break; |
1685 | case 121: | |
1686 | if (modifiers) { | |
3de3c0c6 | 1687 | result.key = C0.ESC + '[21;' + (modifiers + 1) + '~'; |
db76868c | 1688 | } else { |
3de3c0c6 | 1689 | result.key = C0.ESC + '[21~'; |
3f455f90 | 1690 | } |
db76868c PK |
1691 | break; |
1692 | case 122: | |
1693 | if (modifiers) { | |
3de3c0c6 | 1694 | result.key = C0.ESC + '[23;' + (modifiers + 1) + '~'; |
3f455f90 | 1695 | } else { |
3de3c0c6 | 1696 | result.key = C0.ESC + '[23~'; |
3f455f90 | 1697 | } |
db76868c PK |
1698 | break; |
1699 | case 123: | |
1700 | if (modifiers) { | |
3de3c0c6 | 1701 | result.key = C0.ESC + '[24;' + (modifiers + 1) + '~'; |
db76868c | 1702 | } else { |
3de3c0c6 | 1703 | result.key = C0.ESC + '[24~'; |
3f455f90 | 1704 | } |
db76868c PK |
1705 | break; |
1706 | default: | |
1707 | // a-z and space | |
1708 | if (ev.ctrlKey && !ev.shiftKey && !ev.altKey && !ev.metaKey) { | |
1709 | if (ev.keyCode >= 65 && ev.keyCode <= 90) { | |
1710 | result.key = String.fromCharCode(ev.keyCode - 64); | |
1711 | } else if (ev.keyCode === 32) { | |
1712 | // NUL | |
1713 | result.key = String.fromCharCode(0); | |
1714 | } else if (ev.keyCode >= 51 && ev.keyCode <= 55) { | |
1715 | // escape, file sep, group sep, record sep, unit sep | |
1716 | result.key = String.fromCharCode(ev.keyCode - 51 + 27); | |
1717 | } else if (ev.keyCode === 56) { | |
1718 | // delete | |
1719 | result.key = String.fromCharCode(127); | |
1720 | } else if (ev.keyCode === 219) { | |
15a94240 | 1721 | // ^[ - Control Sequence Introducer (CSI) |
db76868c | 1722 | result.key = String.fromCharCode(27); |
15a94240 DI |
1723 | } else if (ev.keyCode === 220) { |
1724 | // ^\ - String Terminator (ST) | |
1725 | result.key = String.fromCharCode(28); | |
db76868c | 1726 | } else if (ev.keyCode === 221) { |
15a94240 | 1727 | // ^] - Operating System Command (OSC) |
db76868c PK |
1728 | result.key = String.fromCharCode(29); |
1729 | } | |
bc70b3b3 | 1730 | } else if (!this.browser.isMac && ev.altKey && !ev.ctrlKey && !ev.metaKey) { |
db76868c PK |
1731 | // On Mac this is a third level shift. Use <Esc> instead. |
1732 | if (ev.keyCode >= 65 && ev.keyCode <= 90) { | |
3de3c0c6 | 1733 | result.key = C0.ESC + String.fromCharCode(ev.keyCode + 32); |
db76868c | 1734 | } else if (ev.keyCode === 192) { |
3de3c0c6 | 1735 | result.key = C0.ESC + '`'; |
db76868c | 1736 | } else if (ev.keyCode >= 48 && ev.keyCode <= 57) { |
3de3c0c6 | 1737 | result.key = C0.ESC + (ev.keyCode - 48); |
db76868c | 1738 | } |
3f455f90 | 1739 | } |
db76868c PK |
1740 | break; |
1741 | } | |
b9d374af | 1742 | |
db76868c PK |
1743 | return result; |
1744 | }; | |
3f455f90 | 1745 | |
db76868c PK |
1746 | /** |
1747 | * Set the G level of the terminal | |
1748 | * @param g | |
1749 | */ | |
1750 | Terminal.prototype.setgLevel = function(g) { | |
1751 | this.glevel = g; | |
1752 | this.charset = this.charsets[g]; | |
1753 | }; | |
12a150a4 | 1754 | |
db76868c PK |
1755 | /** |
1756 | * Set the charset for the given G level of the terminal | |
1757 | * @param g | |
1758 | * @param charset | |
1759 | */ | |
1760 | Terminal.prototype.setgCharset = function(g, charset) { | |
1761 | this.charsets[g] = charset; | |
1762 | if (this.glevel === g) { | |
1763 | this.charset = charset; | |
1764 | } | |
1765 | }; | |
12a150a4 | 1766 | |
db76868c PK |
1767 | /** |
1768 | * Handle a keypress event. | |
1769 | * Key Resources: | |
1770 | * - https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent | |
1771 | * @param {KeyboardEvent} ev The keypress event to be handled. | |
1772 | */ | |
1773 | Terminal.prototype.keyPress = function(ev) { | |
1774 | var key; | |
3f455f90 | 1775 | |
db76868c | 1776 | this.cancel(ev); |
3f455f90 | 1777 | |
db76868c PK |
1778 | if (ev.charCode) { |
1779 | key = ev.charCode; | |
1780 | } else if (ev.which == null) { | |
1781 | key = ev.keyCode; | |
1782 | } else if (ev.which !== 0 && ev.charCode !== 0) { | |
1783 | key = ev.which; | |
1784 | } else { | |
1785 | return false; | |
1786 | } | |
3f455f90 | 1787 | |
db76868c PK |
1788 | if (!key || ( |
1789 | (ev.altKey || ev.ctrlKey || ev.metaKey) && !isThirdLevelShift(this, ev) | |
1790 | )) { | |
1791 | return false; | |
1792 | } | |
12a150a4 | 1793 | |
db76868c | 1794 | key = String.fromCharCode(key); |
3f455f90 | 1795 | |
db76868c PK |
1796 | this.emit('keypress', key, ev); |
1797 | this.emit('key', key, ev); | |
1798 | this.showCursor(); | |
1799 | this.handler(key); | |
12a150a4 | 1800 | |
db76868c PK |
1801 | return false; |
1802 | }; | |
3f455f90 | 1803 | |
db76868c PK |
1804 | /** |
1805 | * Send data for handling to the terminal | |
1806 | * @param {string} data | |
1807 | */ | |
1808 | Terminal.prototype.send = function(data) { | |
1809 | var self = this; | |
3f455f90 | 1810 | |
db76868c PK |
1811 | if (!this.queue) { |
1812 | setTimeout(function() { | |
1813 | self.handler(self.queue); | |
1814 | self.queue = ''; | |
1815 | }, 1); | |
1816 | } | |
3f455f90 | 1817 | |
db76868c PK |
1818 | this.queue += data; |
1819 | }; | |
3f455f90 | 1820 | |
db76868c PK |
1821 | /** |
1822 | * Ring the bell. | |
1823 | * Note: We could do sweet things with webaudio here | |
1824 | */ | |
1825 | Terminal.prototype.bell = function() { | |
1826 | if (!this.visualBell) return; | |
1827 | var self = this; | |
1828 | this.element.style.borderColor = 'white'; | |
1829 | setTimeout(function() { | |
1830 | self.element.style.borderColor = ''; | |
1831 | }, 10); | |
1832 | if (this.popOnBell) this.focus(); | |
1833 | }; | |
c3cf6a22 | 1834 | |
db76868c PK |
1835 | /** |
1836 | * Log the current state to the console. | |
1837 | */ | |
1838 | Terminal.prototype.log = function() { | |
1839 | if (!this.debug) return; | |
1840 | if (!this.context.console || !this.context.console.log) return; | |
1841 | var args = Array.prototype.slice.call(arguments); | |
1842 | this.context.console.log.apply(this.context.console, args); | |
1843 | }; | |
3f455f90 | 1844 | |
db76868c PK |
1845 | /** |
1846 | * Log the current state as error to the console. | |
1847 | */ | |
1848 | Terminal.prototype.error = function() { | |
1849 | if (!this.debug) return; | |
1850 | if (!this.context.console || !this.context.console.error) return; | |
1851 | var args = Array.prototype.slice.call(arguments); | |
1852 | this.context.console.error.apply(this.context.console, args); | |
1853 | }; | |
c3cf6a22 | 1854 | |
db76868c PK |
1855 | /** |
1856 | * Resizes the terminal. | |
1857 | * | |
1858 | * @param {number} x The number of columns to resize to. | |
1859 | * @param {number} y The number of rows to resize to. | |
1860 | */ | |
1861 | Terminal.prototype.resize = function(x, y) { | |
c3256556 | 1862 | if (isNaN(x) || isNaN(y)) { |
ee0d6b45 LB |
1863 | return; |
1864 | } | |
1865 | ||
db76868c PK |
1866 | var line |
1867 | , el | |
1868 | , i | |
1869 | , j | |
1870 | , ch | |
1871 | , addToY; | |
1872 | ||
1873 | if (x === this.cols && y === this.rows) { | |
1874 | return; | |
1875 | } | |
1876 | ||
1877 | if (x < 1) x = 1; | |
1878 | if (y < 1) y = 1; | |
1879 | ||
1880 | // resize cols | |
1881 | j = this.cols; | |
1882 | if (j < x) { | |
1883 | ch = [this.defAttr, ' ', 1]; // does xterm use the default attr? | |
1884 | i = this.lines.length; | |
1885 | while (i--) { | |
607c8191 DI |
1886 | while (this.lines.get(i).length < x) { |
1887 | this.lines.get(i).push(ch); | |
db76868c PK |
1888 | } |
1889 | } | |
db76868c | 1890 | } |
cb77b9dc | 1891 | |
db76868c | 1892 | this.cols = x; |
9a86e4e1 | 1893 | this.setupStops(this.cols); |
db76868c PK |
1894 | |
1895 | // resize rows | |
1896 | j = this.rows; | |
1897 | addToY = 0; | |
1898 | if (j < y) { | |
1899 | el = this.element; | |
1900 | while (j++ < y) { | |
1901 | // y is rows, not this.y | |
1902 | if (this.lines.length < y + this.ybase) { | |
1903 | if (this.ybase > 0 && this.lines.length <= this.ybase + this.y + addToY + 1) { | |
1904 | // There is room above the buffer and there are no empty elements below the line, | |
1905 | // scroll up | |
1906 | this.ybase--; | |
1907 | addToY++ | |
1908 | if (this.ydisp > 0) { | |
1909 | // Viewport is at the top of the buffer, must increase downwards | |
1910 | this.ydisp--; | |
1911 | } | |
1912 | } else { | |
1913 | // Add a blank line if there is no buffer left at the top to scroll to, or if there | |
1914 | // are blank lines after the cursor | |
1915 | this.lines.push(this.blankLine()); | |
1916 | } | |
1917 | } | |
1918 | if (this.children.length < y) { | |
1919 | this.insertRow(); | |
1920 | } | |
1921 | } | |
1922 | } else { // (j > y) | |
1923 | while (j-- > y) { | |
1924 | if (this.lines.length > y + this.ybase) { | |
1925 | if (this.lines.length > this.ybase + this.y + 1) { | |
1926 | // The line is a blank line below the cursor, remove it | |
1927 | this.lines.pop(); | |
1928 | } else { | |
1929 | // The line is the cursor, scroll down | |
1930 | this.ybase++; | |
1931 | this.ydisp++; | |
1932 | } | |
1933 | } | |
1934 | if (this.children.length > y) { | |
1935 | el = this.children.shift(); | |
1936 | if (!el) continue; | |
1937 | el.parentNode.removeChild(el); | |
1938 | } | |
1939 | } | |
1940 | } | |
1941 | this.rows = y; | |
3f455f90 | 1942 | |
db76868c PK |
1943 | // Make sure that the cursor stays on screen |
1944 | if (this.y >= y) { | |
1945 | this.y = y - 1; | |
1946 | } | |
1947 | if (addToY) { | |
1948 | this.y += addToY; | |
1949 | } | |
12a150a4 | 1950 | |
db76868c PK |
1951 | if (this.x >= x) { |
1952 | this.x = x - 1; | |
1953 | } | |
3f455f90 | 1954 | |
db76868c PK |
1955 | this.scrollTop = 0; |
1956 | this.scrollBottom = y - 1; | |
12a150a4 | 1957 | |
4f18d842 DI |
1958 | this.charMeasure.measure(); |
1959 | ||
3c635f3c | 1960 | this.refresh(0, this.rows - 1); |
3f455f90 | 1961 | |
db76868c | 1962 | this.normal = null; |
12a150a4 | 1963 | |
a9417c68 | 1964 | this.geometry = [this.cols, this.rows]; |
db76868c PK |
1965 | this.emit('resize', {terminal: this, cols: x, rows: y}); |
1966 | }; | |
3f455f90 | 1967 | |
db76868c PK |
1968 | /** |
1969 | * Updates the range of rows to refresh | |
1970 | * @param {number} y The number of rows to refresh next. | |
1971 | */ | |
1972 | Terminal.prototype.updateRange = function(y) { | |
1973 | if (y < this.refreshStart) this.refreshStart = y; | |
1974 | if (y > this.refreshEnd) this.refreshEnd = y; | |
1975 | // if (y > this.refreshEnd) { | |
1976 | // this.refreshEnd = y; | |
1977 | // if (y > this.rows - 1) { | |
1978 | // this.refreshEnd = this.rows - 1; | |
1979 | // } | |
1980 | // } | |
1981 | }; | |
3f455f90 | 1982 | |
db76868c | 1983 | /** |
0de3d839 | 1984 | * Set the range of refreshing to the maximum value |
db76868c PK |
1985 | */ |
1986 | Terminal.prototype.maxRange = function() { | |
1987 | this.refreshStart = 0; | |
1988 | this.refreshEnd = this.rows - 1; | |
1989 | }; | |
12a150a4 | 1990 | |
3f455f90 | 1991 | |
12a150a4 | 1992 | |
db76868c PK |
1993 | /** |
1994 | * Setup the tab stops. | |
1995 | * @param {number} i | |
1996 | */ | |
1997 | Terminal.prototype.setupStops = function(i) { | |
1998 | if (i != null) { | |
1999 | if (!this.tabs[i]) { | |
2000 | i = this.prevStop(i); | |
2001 | } | |
2002 | } else { | |
2003 | this.tabs = {}; | |
2004 | i = 0; | |
2005 | } | |
3f455f90 | 2006 | |
f4293a6d | 2007 | for (; i < this.cols; i += this.getOption('tabStopWidth')) { |
db76868c PK |
2008 | this.tabs[i] = true; |
2009 | } | |
2010 | }; | |
12a150a4 | 2011 | |
3f455f90 | 2012 | |
db76868c PK |
2013 | /** |
2014 | * Move the cursor to the previous tab stop from the given position (default is current). | |
2015 | * @param {number} x The position to move the cursor to the previous tab stop. | |
2016 | */ | |
2017 | Terminal.prototype.prevStop = function(x) { | |
2018 | if (x == null) x = this.x; | |
2019 | while (!this.tabs[--x] && x > 0); | |
2020 | return x >= this.cols | |
2021 | ? this.cols - 1 | |
2022 | : x < 0 ? 0 : x; | |
2023 | }; | |
12a150a4 | 2024 | |
3f455f90 | 2025 | |
db76868c PK |
2026 | /** |
2027 | * Move the cursor one tab stop forward from the given position (default is current). | |
2028 | * @param {number} x The position to move the cursor one tab stop forward. | |
2029 | */ | |
2030 | Terminal.prototype.nextStop = function(x) { | |
2031 | if (x == null) x = this.x; | |
2032 | while (!this.tabs[++x] && x < this.cols); | |
2033 | return x >= this.cols | |
2034 | ? this.cols - 1 | |
2035 | : x < 0 ? 0 : x; | |
2036 | }; | |
3f455f90 | 2037 | |
12a150a4 | 2038 | |
db76868c PK |
2039 | /** |
2040 | * Erase in the identified line everything from "x" to the end of the line (right). | |
2041 | * @param {number} x The column from which to start erasing to the end of the line. | |
2042 | * @param {number} y The line in which to operate. | |
2043 | */ | |
2044 | Terminal.prototype.eraseRight = function(x, y) { | |
6cd5c2b8 DI |
2045 | var line = this.lines.get(this.ybase + y); |
2046 | if (!line) { | |
2047 | return; | |
2048 | } | |
2049 | var ch = [this.eraseAttr(), ' ', 1]; // xterm | |
db76868c PK |
2050 | for (; x < this.cols; x++) { |
2051 | line[x] = ch; | |
2052 | } | |
db76868c PK |
2053 | this.updateRange(y); |
2054 | }; | |
12a150a4 | 2055 | |
3f455f90 | 2056 | |
12a150a4 | 2057 | |
db76868c PK |
2058 | /** |
2059 | * Erase in the identified line everything from "x" to the start of the line (left). | |
2060 | * @param {number} x The column from which to start erasing to the start of the line. | |
2061 | * @param {number} y The line in which to operate. | |
2062 | */ | |
2063 | Terminal.prototype.eraseLeft = function(x, y) { | |
6cd5c2b8 DI |
2064 | var line = this.lines.get(this.ybase + y); |
2065 | if (!line) { | |
2066 | return; | |
2067 | } | |
2068 | var ch = [this.eraseAttr(), ' ', 1]; // xterm | |
db76868c | 2069 | x++; |
6cd5c2b8 DI |
2070 | while (x--) { |
2071 | line[x] = ch; | |
2072 | } | |
db76868c PK |
2073 | this.updateRange(y); |
2074 | }; | |
3f455f90 | 2075 | |
76719413 DI |
2076 | /** |
2077 | * Clears the entire buffer, making the prompt line the new first line. | |
2078 | */ | |
2079 | Terminal.prototype.clear = function() { | |
852dac4d DI |
2080 | if (this.ybase === 0 && this.y === 0) { |
2081 | // Don't clear if it's already clear | |
2082 | return; | |
2083 | } | |
607c8191 DI |
2084 | this.lines.set(0, this.lines.get(this.ybase + this.y)); |
2085 | this.lines.length = 1; | |
76719413 DI |
2086 | this.ydisp = 0; |
2087 | this.ybase = 0; | |
2088 | this.y = 0; | |
76719413 DI |
2089 | for (var i = 1; i < this.rows; i++) { |
2090 | this.lines.push(this.blankLine()); | |
2091 | } | |
3c635f3c | 2092 | this.refresh(0, this.rows - 1); |
76719413 DI |
2093 | this.emit('scroll', this.ydisp); |
2094 | }; | |
3f455f90 | 2095 | |
db76868c PK |
2096 | /** |
2097 | * Erase all content in the given line | |
2098 | * @param {number} y The line to erase all of its contents. | |
2099 | */ | |
2100 | Terminal.prototype.eraseLine = function(y) { | |
2101 | this.eraseRight(0, y); | |
2102 | }; | |
3f455f90 | 2103 | |
3f455f90 | 2104 | |
db76868c | 2105 | /** |
cc5ae819 | 2106 | * Return the data array of a blank line |
db76868c PK |
2107 | * @param {number} cur First bunch of data for each "blank" character. |
2108 | */ | |
2109 | Terminal.prototype.blankLine = function(cur) { | |
2110 | var attr = cur | |
2111 | ? this.eraseAttr() | |
2112 | : this.defAttr; | |
12a150a4 | 2113 | |
db76868c PK |
2114 | var ch = [attr, ' ', 1] // width defaults to 1 halfwidth character |
2115 | , line = [] | |
2116 | , i = 0; | |
3f455f90 | 2117 | |
db76868c PK |
2118 | for (; i < this.cols; i++) { |
2119 | line[i] = ch; | |
2120 | } | |
12a150a4 | 2121 | |
db76868c PK |
2122 | return line; |
2123 | }; | |
3f455f90 | 2124 | |
12a150a4 | 2125 | |
db76868c PK |
2126 | /** |
2127 | * If cur return the back color xterm feature attribute. Else return defAttr. | |
2128 | * @param {object} cur | |
2129 | */ | |
2130 | Terminal.prototype.ch = function(cur) { | |
2131 | return cur | |
2132 | ? [this.eraseAttr(), ' ', 1] | |
2133 | : [this.defAttr, ' ', 1]; | |
2134 | }; | |
3f455f90 | 2135 | |
3f455f90 | 2136 | |
db76868c PK |
2137 | /** |
2138 | * Evaluate if the current erminal is the given argument. | |
2139 | * @param {object} term The terminal to evaluate | |
2140 | */ | |
2141 | Terminal.prototype.is = function(term) { | |
2142 | var name = this.termName; | |
2143 | return (name + '').indexOf(term) === 0; | |
2144 | }; | |
3f455f90 | 2145 | |
12a150a4 | 2146 | |
db76868c | 2147 | /** |
32e878db DI |
2148 | * Emit the 'data' event and populate the given data. |
2149 | * @param {string} data The data to populate in the event. | |
2150 | */ | |
db76868c | 2151 | Terminal.prototype.handler = function(data) { |
d9d60063 DI |
2152 | // Prevents all events to pty process if stdin is disabled |
2153 | if (this.options.disableStdin) { | |
2154 | return; | |
2155 | } | |
2156 | ||
2bc8adee DI |
2157 | // Input is being sent to the terminal, the terminal should focus the prompt. |
2158 | if (this.ybase !== this.ydisp) { | |
2159 | this.scrollToBottom(); | |
2160 | } | |
db76868c PK |
2161 | this.emit('data', data); |
2162 | }; | |
3f455f90 | 2163 | |
12a150a4 | 2164 | |
db76868c PK |
2165 | /** |
2166 | * Emit the 'title' event and populate the given title. | |
2167 | * @param {string} title The title to populate in the event. | |
2168 | */ | |
2169 | Terminal.prototype.handleTitle = function(title) { | |
1fc5a9aa Y |
2170 | /** |
2171 | * This event is emitted when the title of the terminal is changed | |
2172 | * from inside the terminal. The parameter is the new title. | |
2173 | * | |
2174 | * @event title | |
2175 | */ | |
db76868c PK |
2176 | this.emit('title', title); |
2177 | }; | |
3f455f90 | 2178 | |
3f455f90 | 2179 | |
db76868c PK |
2180 | /** |
2181 | * ESC | |
2182 | */ | |
3f455f90 | 2183 | |
db76868c PK |
2184 | /** |
2185 | * ESC D Index (IND is 0x84). | |
2186 | */ | |
2187 | Terminal.prototype.index = function() { | |
2188 | this.y++; | |
2189 | if (this.y > this.scrollBottom) { | |
2190 | this.y--; | |
2191 | this.scroll(); | |
2192 | } | |
5850cbcd | 2193 | // If the end of the line is hit, prevent this action from wrapping around to the next line. |
72063329 DI |
2194 | if (this.x >= this.cols) { |
2195 | this.x--; | |
2196 | } | |
db76868c | 2197 | }; |
3f455f90 | 2198 | |
3f455f90 | 2199 | |
db76868c PK |
2200 | /** |
2201 | * ESC M Reverse Index (RI is 0x8d). | |
3b35d12e DI |
2202 | * |
2203 | * Move the cursor up one row, inserting a new blank line if necessary. | |
db76868c PK |
2204 | */ |
2205 | Terminal.prototype.reverseIndex = function() { | |
2206 | var j; | |
3b35d12e | 2207 | if (this.y === this.scrollTop) { |
db76868c PK |
2208 | // possibly move the code below to term.reverseScroll(); |
2209 | // test: echo -ne '\e[1;1H\e[44m\eM\e[0m' | |
2210 | // blankLine(true) is xterm/linux behavior | |
bf16fdc0 DI |
2211 | this.lines.shiftElements(this.y + this.ybase, this.rows - 1, 1); |
2212 | this.lines.set(this.y + this.ybase, this.blankLine(true)); | |
db76868c PK |
2213 | this.updateRange(this.scrollTop); |
2214 | this.updateRange(this.scrollBottom); | |
3b35d12e DI |
2215 | } else { |
2216 | this.y--; | |
db76868c | 2217 | } |
db76868c | 2218 | }; |
3f455f90 | 2219 | |
12a150a4 | 2220 | |
db76868c PK |
2221 | /** |
2222 | * ESC c Full Reset (RIS). | |
2223 | */ | |
2224 | Terminal.prototype.reset = function() { | |
2225 | this.options.rows = this.rows; | |
2226 | this.options.cols = this.cols; | |
2227 | var customKeydownHandler = this.customKeydownHandler; | |
5cafcdb6 | 2228 | var cursorBlinkInterval = this.cursorBlinkInterval; |
db76868c PK |
2229 | Terminal.call(this, this.options); |
2230 | this.customKeydownHandler = customKeydownHandler; | |
5cafcdb6 | 2231 | this.cursorBlinkInterval = cursorBlinkInterval; |
3c635f3c | 2232 | this.refresh(0, this.rows - 1); |
c8b19493 | 2233 | this.viewport.syncScrollArea(); |
db76868c | 2234 | }; |
3f455f90 | 2235 | |
12a150a4 | 2236 | |
db76868c PK |
2237 | /** |
2238 | * ESC H Tab Set (HTS is 0x88). | |
2239 | */ | |
2240 | Terminal.prototype.tabSet = function() { | |
2241 | this.tabs[this.x] = true; | |
db76868c | 2242 | }; |
3f455f90 | 2243 | |
db76868c PK |
2244 | /** |
2245 | * Helpers | |
2246 | */ | |
2247 | ||
db76868c PK |
2248 | function on(el, type, handler, capture) { |
2249 | if (!Array.isArray(el)) { | |
2250 | el = [el]; | |
2251 | } | |
2252 | el.forEach(function (element) { | |
2253 | element.addEventListener(type, handler, capture || false); | |
2254 | }); | |
2255 | } | |
2256 | ||
2257 | function off(el, type, handler, capture) { | |
2258 | el.removeEventListener(type, handler, capture || false); | |
2259 | } | |
2260 | ||
2261 | function cancel(ev, force) { | |
2262 | if (!this.cancelEvents && !force) { | |
2263 | return; | |
2264 | } | |
2265 | ev.preventDefault(); | |
2266 | ev.stopPropagation(); | |
2267 | return false; | |
2268 | } | |
2269 | ||
2270 | function inherits(child, parent) { | |
2271 | function f() { | |
2272 | this.constructor = child; | |
2273 | } | |
2274 | f.prototype = parent.prototype; | |
2275 | child.prototype = new f; | |
2276 | } | |
2277 | ||
db76868c PK |
2278 | function indexOf(obj, el) { |
2279 | var i = obj.length; | |
2280 | while (i--) { | |
2281 | if (obj[i] === el) return i; | |
2282 | } | |
2283 | return -1; | |
2284 | } | |
2285 | ||
2286 | function isThirdLevelShift(term, ev) { | |
2287 | var thirdLevelKey = | |
bc70b3b3 PK |
2288 | (term.browser.isMac && ev.altKey && !ev.ctrlKey && !ev.metaKey) || |
2289 | (term.browser.isMSWindows && ev.altKey && ev.ctrlKey && !ev.metaKey); | |
db76868c PK |
2290 | |
2291 | if (ev.type == 'keypress') { | |
2292 | return thirdLevelKey; | |
2293 | } | |
2294 | ||
2295 | // Don't invoke for arrows, pageDown, home, backspace, etc. (on non-keypress events) | |
2296 | return thirdLevelKey && (!ev.keyCode || ev.keyCode > 47); | |
2297 | } | |
2298 | ||
db81c28b DI |
2299 | // Expose to InputHandler (temporary) |
2300 | Terminal.prototype.matchColor = matchColor; | |
2301 | ||
db76868c PK |
2302 | function matchColor(r1, g1, b1) { |
2303 | var hash = (r1 << 16) | (g1 << 8) | b1; | |
2304 | ||
2305 | if (matchColor._cache[hash] != null) { | |
2306 | return matchColor._cache[hash]; | |
2307 | } | |
2308 | ||
2309 | var ldiff = Infinity | |
2310 | , li = -1 | |
2311 | , i = 0 | |
2312 | , c | |
2313 | , r2 | |
2314 | , g2 | |
2315 | , b2 | |
2316 | , diff; | |
2317 | ||
2318 | for (; i < Terminal.vcolors.length; i++) { | |
2319 | c = Terminal.vcolors[i]; | |
2320 | r2 = c[0]; | |
2321 | g2 = c[1]; | |
2322 | b2 = c[2]; | |
2323 | ||
2324 | diff = matchColor.distance(r1, g1, b1, r2, g2, b2); | |
2325 | ||
2326 | if (diff === 0) { | |
2327 | li = i; | |
2328 | break; | |
2329 | } | |
2330 | ||
2331 | if (diff < ldiff) { | |
2332 | ldiff = diff; | |
2333 | li = i; | |
2334 | } | |
2335 | } | |
2336 | ||
2337 | return matchColor._cache[hash] = li; | |
2338 | } | |
2339 | ||
2340 | matchColor._cache = {}; | |
2341 | ||
2342 | // http://stackoverflow.com/questions/1633828 | |
2343 | matchColor.distance = function(r1, g1, b1, r2, g2, b2) { | |
2344 | return Math.pow(30 * (r1 - r2), 2) | |
2345 | + Math.pow(59 * (g1 - g2), 2) | |
2346 | + Math.pow(11 * (b1 - b2), 2); | |
2347 | }; | |
2348 | ||
2349 | function each(obj, iter, con) { | |
2350 | if (obj.forEach) return obj.forEach(iter, con); | |
2351 | for (var i = 0; i < obj.length; i++) { | |
2352 | iter.call(con, obj[i], i, obj); | |
2353 | } | |
2354 | } | |
2355 | ||
5e1ccb35 DI |
2356 | function wasMondifierKeyOnlyEvent(ev) { |
2357 | return ev.keyCode === 16 || // Shift | |
2358 | ev.keyCode === 17 || // Ctrl | |
2359 | ev.keyCode === 18; // Alt | |
2360 | } | |
db76868c | 2361 | |
db76868c PK |
2362 | function keys(obj) { |
2363 | if (Object.keys) return Object.keys(obj); | |
2364 | var key, keys = []; | |
2365 | for (key in obj) { | |
2366 | if (Object.prototype.hasOwnProperty.call(obj, key)) { | |
2367 | keys.push(key); | |
2368 | } | |
2369 | } | |
2370 | return keys; | |
2371 | } | |
2372 | ||
db76868c PK |
2373 | /** |
2374 | * Expose | |
2375 | */ | |
2376 | ||
2377 | Terminal.EventEmitter = EventEmitter; | |
db76868c PK |
2378 | Terminal.inherits = inherits; |
2379 | ||
2380 | /** | |
2381 | * Adds an event listener to the terminal. | |
2382 | * | |
2383 | * @param {string} event The name of the event. TODO: Document all event types | |
2384 | * @param {function} callback The function to call when the event is triggered. | |
2385 | */ | |
2386 | Terminal.on = on; | |
2387 | Terminal.off = off; | |
2388 | Terminal.cancel = cancel; | |
8bc844c0 | 2389 | |
ed1a31d1 | 2390 | module.exports = Terminal; |