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