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