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