blob: eff1df55df6b79f8250deef19e7a0592459acd24 [file] [log] [blame]
rginda87b86462011-12-14 13:48:03 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rginda8ba33642011-12-14 12:31:31 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rgindacbbd7482012-06-13 15:06:16 -07005'use strict';
6
rginda8ba33642011-12-14 12:31:31 -08007/**
8 * Constructor for the Terminal class.
9 *
10 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
11 * classes to provide the complete terminal functionality.
12 *
13 * There are a number of lower-level Terminal methods that can be called
14 * directly to manipulate the cursor, text, scroll region, and other terminal
15 * attributes. However, the primary method is interpret(), which parses VT
16 * escape sequences and invokes the appropriate Terminal methods.
17 *
18 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
19 *
20 * TODO(rginda): Eventually we're going to need to support characters which are
21 * displayed twice as wide as standard latin characters. This is to support
22 * CJK (and possibly other character sets).
rginda9f5222b2012-03-05 11:53:28 -080023 *
Joel Hockey3a44a442019-10-14 16:22:56 -070024 * @param {?string=} profileId Optional preference profile name. If not
25 * provided or null, defaults to 'default'.
Joel Hockey0f933582019-08-27 18:01:51 -070026 * @constructor
Joel Hockeyd4fca732019-09-20 16:57:03 -070027 * @implements {hterm.RowProvider}
rginda8ba33642011-12-14 12:31:31 -080028 */
Joel Hockey3a44a442019-10-14 16:22:56 -070029hterm.Terminal = function(profileId) {
Robert Ginda57f03b42012-09-13 11:02:48 -070030 this.profileId_ = null;
rginda9f5222b2012-03-05 11:53:28 -080031
Joel Hockeyd4fca732019-09-20 16:57:03 -070032 /** @type {?hterm.PreferenceManager} */
33 this.prefs_ = null;
34
rginda8ba33642011-12-14 12:31:31 -080035 // Two screen instances.
36 this.primaryScreen_ = new hterm.Screen();
37 this.alternateScreen_ = new hterm.Screen();
38
39 // The "current" screen.
40 this.screen_ = this.primaryScreen_;
41
rginda8ba33642011-12-14 12:31:31 -080042 // The local notion of the screen size. ScreenBuffers also have a size which
43 // indicates their present size. During size changes, the two may disagree.
44 // Also, the inactive screen's size is not altered until it is made the active
45 // screen.
46 this.screenSize = new hterm.Size(0, 0);
47
rginda8ba33642011-12-14 12:31:31 -080048 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080049 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080050 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
51 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080052 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
Raymes Khourye5d48982018-08-02 09:08:32 +100053 this.scrollPort_.subscribe('focus', this.onScrollportFocus_.bind(this));
Joel Hockey3e5aed82020-04-01 18:30:05 -070054 this.scrollPort_.subscribe('options', this.onOpenOptionsPage_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070055 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080056
rginda87b86462011-12-14 13:48:03 -080057 // The div that contains this terminal.
58 this.div_ = null;
59
rgindac9bc5502012-01-18 11:48:44 -080060 // The document that contains the scrollPort. Defaulted to the global
61 // document here so that the terminal is functional even if it hasn't been
62 // inserted into a document yet, but re-set in decorate().
63 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080064
rginda8ba33642011-12-14 12:31:31 -080065 // The rows that have scrolled off screen and are no longer addressable.
66 this.scrollbackRows_ = [];
67
rgindac9bc5502012-01-18 11:48:44 -080068 // Saved tab stops.
69 this.tabStops_ = [];
70
David Benjamin66e954d2012-05-05 21:08:12 -040071 // Keep track of whether default tab stops have been erased; after a TBC
72 // clears all tab stops, defaults aren't restored on resize until a reset.
73 this.defaultTabStops = true;
74
rginda8ba33642011-12-14 12:31:31 -080075 // The VT's notion of the top and bottom rows. Used during some VT
76 // cursor positioning and scrolling commands.
77 this.vtScrollTop_ = null;
78 this.vtScrollBottom_ = null;
79
80 // The DIV element for the visible cursor.
81 this.cursorNode_ = null;
82
Robert Ginda830583c2013-08-07 13:20:46 -070083 // The current cursor shape of the terminal.
84 this.cursorShape_ = hterm.Terminal.cursorShape.BLOCK;
85
Robert Gindaea2183e2014-07-17 09:51:51 -070086 // Cursor blink on/off cycle in ms, overwritten by prefs once they're loaded.
87 this.cursorBlinkCycle_ = [100, 100];
88
Mike Frysinger225c99d2019-10-20 14:02:37 -060089 // Whether to temporarily disable blinking.
90 this.cursorBlinkPause_ = false;
91
Robert Gindaea2183e2014-07-17 09:51:51 -070092 // Pre-bound onCursorBlink_ handler, so we don't have to do this for each
93 // cursor on/off servicing.
94 this.myOnCursorBlink_ = this.onCursorBlink_.bind(this);
95
rginda9f5222b2012-03-05 11:53:28 -080096 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -070097 // each output and keystroke. They are initialized by the preference manager.
Joel Hockey42dba8f2020-03-26 16:21:11 -070098 /** @type {?string} */
99 this.backgroundColor_ = null;
100 /** @type {?string} */
101 this.foregroundColor_ = null;
102
Robert Ginda57f03b42012-09-13 11:02:48 -0700103 this.scrollOnOutput_ = null;
104 this.scrollOnKeystroke_ = null;
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400105 this.scrollWheelArrowKeys_ = null;
rginda9f5222b2012-03-05 11:53:28 -0800106
Robert Ginda6aec7eb2015-06-16 10:31:30 -0700107 // True if we should override mouse event reporting to allow local selection.
108 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -0800109
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400110 // Whether to auto hide the mouse cursor when typing.
111 this.setAutomaticMouseHiding();
112 // Timer to keep mouse visible while it's being used.
113 this.mouseHideDelay_ = null;
114
rgindaf0090c92012-02-10 14:58:52 -0800115 // Terminal bell sound.
116 this.bellAudio_ = this.document_.createElement('audio');
Mike Frysingerd826f1a2017-07-06 16:20:06 -0400117 this.bellAudio_.id = 'hterm:bell-audio';
rgindaf0090c92012-02-10 14:58:52 -0800118 this.bellAudio_.setAttribute('preload', 'auto');
119
Raymes Khoury3e44bc92018-05-17 10:54:23 +1000120 // The AccessibilityReader object for announcing command output.
121 this.accessibilityReader_ = null;
122
Mike Frysingercc114512017-09-11 21:39:17 -0400123 // The context menu object.
124 this.contextMenu = new hterm.ContextMenu();
125
Michael Kelly485ecd12014-06-09 11:41:56 -0400126 // All terminal bell notifications that have been generated (not necessarily
127 // shown).
128 this.bellNotificationList_ = [];
Joel Hockeyd4fca732019-09-20 16:57:03 -0700129 this.bellSquelchTimeout_ = null;
Michael Kelly485ecd12014-06-09 11:41:56 -0400130
131 // Whether we have permission to display notifications.
132 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400133
rginda6d397402012-01-17 10:58:29 -0800134 // Cursor position and attributes saved with DECSC.
135 this.savedOptions_ = {};
136
rginda8ba33642011-12-14 12:31:31 -0800137 // The current mode bits for the terminal.
138 this.options_ = new hterm.Options();
139
140 // Timeouts we might need to clear.
141 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800142
143 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800144 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800145
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800146 this.saveCursorAndState(true);
147
Zhu Qunying30d40712017-03-14 16:27:00 -0700148 // The keyboard handler.
rgindafeaf3142012-01-31 15:14:20 -0800149 this.keyboard = new hterm.Keyboard(this);
150
rginda87b86462011-12-14 13:48:03 -0800151 // General IO interface that can be given to third parties without exposing
152 // the entire terminal object.
153 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800154
rgindad5613292012-06-19 15:40:37 -0700155 // True if mouse-click-drag should scroll the terminal.
156 this.enableMouseDragScroll = true;
157
Robert Ginda57f03b42012-09-13 11:02:48 -0700158 this.copyOnSelect = null;
Mike Frysinger847577f2017-05-23 23:25:57 -0400159 this.mouseRightClickPaste = null;
rginda4bba5e12012-06-20 16:15:30 -0700160 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700161
Zhu Qunying30d40712017-03-14 16:27:00 -0700162 // Whether to use the default window copy behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700163 this.useDefaultWindowCopy = false;
164
165 this.clearSelectionAfterCopy = true;
166
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400167 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800168 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700169
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400170 // Whether we allow images to be shown.
171 this.allowImagesInline = null;
172
Gabriel Holodake8a09be2017-10-10 01:07:11 -0400173 this.reportFocus = false;
174
Jason Linf129f3c2020-03-23 11:52:08 +1100175 // TODO(crbug.com/1063219) Remove this once the bug is fixed.
176 this.alwaysUseLegacyPasting = false;
177
Joel Hockey3a44a442019-10-14 16:22:56 -0700178 this.setProfile(profileId || 'default',
Evan Jones5f9df812016-12-06 09:38:58 -0500179 function() { this.onTerminalReady(); }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800180};
181
182/**
Robert Ginda830583c2013-08-07 13:20:46 -0700183 * Possible cursor shapes.
184 */
185hterm.Terminal.cursorShape = {
186 BLOCK: 'BLOCK',
187 BEAM: 'BEAM',
188 UNDERLINE: 'UNDERLINE'
189};
190
191/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700192 * Clients should override this to be notified when the terminal is ready
193 * for use.
194 *
195 * The terminal initialization is asynchronous, and shouldn't be used before
196 * this method is called.
197 */
198hterm.Terminal.prototype.onTerminalReady = function() { };
199
200/**
rginda35c456b2012-02-09 17:29:05 -0800201 * Default tab with of 8 to match xterm.
202 */
203hterm.Terminal.prototype.tabWidth = 8;
204
205/**
rginda9f5222b2012-03-05 11:53:28 -0800206 * Select a preference profile.
207 *
208 * This will load the terminal preferences for the given profile name and
209 * associate subsequent preference changes with the new preference profile.
210 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500211 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800212 * characters will be removed from the name.
Mike Frysingerec4225d2020-04-07 05:00:01 -0400213 * @param {function()=} callback Optional callback to invoke when the
Joel Hockey0f933582019-08-27 18:01:51 -0700214 * profile transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800215 */
Mike Frysingerec4225d2020-04-07 05:00:01 -0400216hterm.Terminal.prototype.setProfile = function(
217 profileId, callback = undefined) {
Robert Ginda57f03b42012-09-13 11:02:48 -0700218 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800219
Robert Ginda57f03b42012-09-13 11:02:48 -0700220 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800221
Mike Frysingerbdb34802020-04-07 03:47:32 -0400222 if (this.prefs_) {
Robert Ginda57f03b42012-09-13 11:02:48 -0700223 this.prefs_.deactivate();
Mike Frysingerbdb34802020-04-07 03:47:32 -0400224 }
rginda9f5222b2012-03-05 11:53:28 -0800225
Robert Ginda57f03b42012-09-13 11:02:48 -0700226 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
Joel Hockey95a9e272020-03-16 21:19:53 -0700227
228 /**
229 * Clears and reloads key bindings. Used by preferences
230 * 'keybindings' and 'keybindings-os-defaults'.
231 *
232 * @param {*} bindings
233 * @param {*} useOsDefaults
234 */
235 function loadKeyBindings(bindings, useOsDefaults) {
236 terminal.keyboard.bindings.clear();
237
238 if (!bindings) {
239 return;
240 }
241
242 if (!(bindings instanceof Object)) {
243 console.error('Error in keybindings preference: Expected object');
244 return;
245 }
246
247 try {
248 terminal.keyboard.bindings.addBindings(bindings, !!useOsDefaults);
249 } catch (ex) {
250 console.error('Error in keybindings preference: ' + ex);
251 }
252 }
253
Robert Ginda57f03b42012-09-13 11:02:48 -0700254 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800255 'alt-gr-mode': function(v) {
256 if (v == null) {
257 if (navigator.language.toLowerCase() == 'en-us') {
258 v = 'none';
259 } else {
260 v = 'right-alt';
261 }
262 } else if (typeof v == 'string') {
263 v = v.toLowerCase();
264 } else {
265 v = 'none';
266 }
267
Mike Frysingerbdb34802020-04-07 03:47:32 -0400268 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v)) {
Robert Ginda034ffa72015-02-26 14:02:37 -0800269 v = 'none';
Mike Frysingerbdb34802020-04-07 03:47:32 -0400270 }
Robert Ginda034ffa72015-02-26 14:02:37 -0800271
272 terminal.keyboard.altGrMode = v;
273 },
274
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700275 'alt-backspace-is-meta-backspace': function(v) {
276 terminal.keyboard.altBackspaceIsMetaBackspace = v;
277 },
278
Robert Ginda57f03b42012-09-13 11:02:48 -0700279 'alt-is-meta': function(v) {
280 terminal.keyboard.altIsMeta = v;
281 },
282
283 'alt-sends-what': function(v) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400284 if (!/^(escape|8-bit|browser-key)$/.test(v)) {
Robert Ginda57f03b42012-09-13 11:02:48 -0700285 v = 'escape';
Mike Frysingerbdb34802020-04-07 03:47:32 -0400286 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700287
288 terminal.keyboard.altSendsWhat = v;
289 },
290
291 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800292 var ary = v.match(/^lib-resource:(\S+)/);
293 if (ary) {
294 terminal.bellAudio_.setAttribute('src',
295 lib.resource.getDataUrl(ary[1]));
296 } else {
297 terminal.bellAudio_.setAttribute('src', v);
298 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700299 },
300
Michael Kelly485ecd12014-06-09 11:41:56 -0400301 'desktop-notification-bell': function(v) {
302 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700303 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400304 Notification.permission === 'granted';
305 if (!terminal.desktopNotificationBell_) {
306 // Note: We don't call Notification.requestPermission here because
307 // Chrome requires the call be the result of a user action (such as an
308 // onclick handler), and pref listeners are run asynchronously.
309 //
310 // A way of working around this would be to display a dialog in the
311 // terminal with a "click-to-request-permission" button.
312 console.warn('desktop-notification-bell is true but we do not have ' +
313 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400314 }
315 } else {
316 terminal.desktopNotificationBell_ = false;
317 }
318 },
319
Robert Ginda57f03b42012-09-13 11:02:48 -0700320 'background-color': function(v) {
321 terminal.setBackgroundColor(v);
322 },
323
324 'background-image': function(v) {
325 terminal.scrollPort_.setBackgroundImage(v);
326 },
327
328 'background-size': function(v) {
329 terminal.scrollPort_.setBackgroundSize(v);
330 },
331
332 'background-position': function(v) {
333 terminal.scrollPort_.setBackgroundPosition(v);
334 },
335
336 'backspace-sends-backspace': function(v) {
337 terminal.keyboard.backspaceSendsBackspace = v;
338 },
339
Brad Town18654b62015-03-12 00:27:45 -0700340 'character-map-overrides': function(v) {
341 if (!(v == null || v instanceof Object)) {
342 console.warn('Preference character-map-modifications is not an ' +
343 'object: ' + v);
344 return;
345 }
346
Mike Frysinger095d4062017-06-14 00:29:48 -0700347 terminal.vt.characterMaps.reset();
348 terminal.vt.characterMaps.setOverrides(v);
Brad Town18654b62015-03-12 00:27:45 -0700349 },
350
Robert Ginda57f03b42012-09-13 11:02:48 -0700351 'cursor-blink': function(v) {
352 terminal.setCursorBlink(!!v);
353 },
354
Joel Hockey9d10ba12019-05-28 01:25:02 -0700355 'cursor-shape': function(v) {
356 terminal.setCursorShape(v);
357 },
358
Robert Gindaea2183e2014-07-17 09:51:51 -0700359 'cursor-blink-cycle': function(v) {
360 if (v instanceof Array &&
361 typeof v[0] == 'number' &&
362 typeof v[1] == 'number') {
363 terminal.cursorBlinkCycle_ = v;
364 } else if (typeof v == 'number') {
365 terminal.cursorBlinkCycle_ = [v, v];
366 } else {
367 // Fast blink indicates an error.
368 terminal.cursorBlinkCycle_ = [100, 100];
369 }
370 },
371
Robert Ginda57f03b42012-09-13 11:02:48 -0700372 'cursor-color': function(v) {
373 terminal.setCursorColor(v);
374 },
375
376 'color-palette-overrides': function(v) {
377 if (!(v == null || v instanceof Object || v instanceof Array)) {
378 console.warn('Preference color-palette-overrides is not an array or ' +
379 'object: ' + v);
380 return;
rginda9f5222b2012-03-05 11:53:28 -0800381 }
rginda9f5222b2012-03-05 11:53:28 -0800382
Joel Hockey42dba8f2020-03-26 16:21:11 -0700383 // Call terminal.setColorPalette here and below with the new default
384 // value before changing it in lib.colors.colorPalette to ensure that
385 // CSS vars are updated.
386 lib.colors.stockColorPalette.forEach(
387 (c, i) => terminal.setColorPalette(i, c));
Robert Ginda57f03b42012-09-13 11:02:48 -0700388 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700389
Robert Ginda57f03b42012-09-13 11:02:48 -0700390 if (v) {
391 for (var key in v) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700392 var i = parseInt(key, 10);
Robert Ginda57f03b42012-09-13 11:02:48 -0700393 if (isNaN(i) || i < 0 || i > 255) {
394 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
395 continue;
396 }
397
398 if (v[i]) {
399 var rgb = lib.colors.normalizeCSS(v[i]);
Joel Hockey42dba8f2020-03-26 16:21:11 -0700400 if (rgb) {
401 terminal.setColorPalette(i, rgb);
Robert Ginda57f03b42012-09-13 11:02:48 -0700402 lib.colors.colorPalette[i] = rgb;
Joel Hockey42dba8f2020-03-26 16:21:11 -0700403 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700404 }
405 }
rginda30f20f62012-04-05 16:36:19 -0700406 }
rginda30f20f62012-04-05 16:36:19 -0700407
Joel Hockey42dba8f2020-03-26 16:21:11 -0700408 terminal.primaryScreen_.textAttributes.colorPaletteOverrides = [];
409 terminal.alternateScreen_.textAttributes.colorPaletteOverrides = [];
Robert Ginda57f03b42012-09-13 11:02:48 -0700410 },
rginda30f20f62012-04-05 16:36:19 -0700411
Robert Ginda57f03b42012-09-13 11:02:48 -0700412 'copy-on-select': function(v) {
413 terminal.copyOnSelect = !!v;
414 },
rginda9f5222b2012-03-05 11:53:28 -0800415
Rob Spies0bec09b2014-06-06 15:58:09 -0700416 'use-default-window-copy': function(v) {
417 terminal.useDefaultWindowCopy = !!v;
418 },
419
420 'clear-selection-after-copy': function(v) {
421 terminal.clearSelectionAfterCopy = !!v;
422 },
423
Robert Ginda7e5e9522014-03-14 12:23:58 -0700424 'ctrl-plus-minus-zero-zoom': function(v) {
425 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
426 },
427
Robert Gindafb5a3f92014-05-13 14:12:00 -0700428 'ctrl-c-copy': function(v) {
429 terminal.keyboard.ctrlCCopy = v;
430 },
431
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100432 'ctrl-v-paste': function(v) {
433 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700434 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100435 },
436
Cody Coljee-Gray7c6a0392018-10-25 13:18:28 -0700437 'paste-on-drop': function(v) {
438 terminal.scrollPort_.setPasteOnDrop(v);
439 },
440
Masaya Suzuki273aa982014-05-31 07:25:55 +0900441 'east-asian-ambiguous-as-two-column': function(v) {
442 lib.wc.regardCjkAmbiguous = v;
443 },
444
Robert Ginda57f03b42012-09-13 11:02:48 -0700445 'enable-8-bit-control': function(v) {
446 terminal.vt.enable8BitControl = !!v;
447 },
rginda30f20f62012-04-05 16:36:19 -0700448
Robert Ginda57f03b42012-09-13 11:02:48 -0700449 'enable-bold': function(v) {
450 terminal.syncBoldSafeState();
451 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400452
Robert Ginda3e278d72014-03-25 13:18:51 -0700453 'enable-bold-as-bright': function(v) {
454 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
455 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
456 },
457
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400458 'enable-blink': function(v) {
Mike Frysinger261597c2017-12-28 01:14:21 -0500459 terminal.setTextBlink(!!v);
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400460 },
461
Robert Ginda57f03b42012-09-13 11:02:48 -0700462 'enable-clipboard-write': function(v) {
463 terminal.vt.enableClipboardWrite = !!v;
464 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400465
Robert Ginda3755e752013-05-31 13:34:09 -0700466 'enable-dec12': function(v) {
467 terminal.vt.enableDec12 = !!v;
468 },
469
Mike Frysinger38f267d2018-09-07 02:50:59 -0400470 'enable-csi-j-3': function(v) {
471 terminal.vt.enableCsiJ3 = !!v;
472 },
473
Robert Ginda57f03b42012-09-13 11:02:48 -0700474 'font-family': function(v) {
475 terminal.syncFontFamily();
476 },
rginda30f20f62012-04-05 16:36:19 -0700477
Robert Ginda57f03b42012-09-13 11:02:48 -0700478 'font-size': function(v) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700479 v = parseInt(v, 10);
Mike Frysinger47853ac2017-12-14 00:44:10 -0500480 if (v <= 0) {
481 console.error(`Invalid font size: ${v}`);
482 return;
483 }
484
Robert Ginda57f03b42012-09-13 11:02:48 -0700485 terminal.setFontSize(v);
486 },
rginda9875d902012-08-20 16:21:57 -0700487
Robert Ginda57f03b42012-09-13 11:02:48 -0700488 'font-smoothing': function(v) {
489 terminal.syncFontFamily();
490 },
rgindade84e382012-04-20 15:39:31 -0700491
Robert Ginda57f03b42012-09-13 11:02:48 -0700492 'foreground-color': function(v) {
493 terminal.setForegroundColor(v);
494 },
rginda30f20f62012-04-05 16:36:19 -0700495
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400496 'hide-mouse-while-typing': function(v) {
497 terminal.setAutomaticMouseHiding(v);
498 },
499
Robert Ginda57f03b42012-09-13 11:02:48 -0700500 'home-keys-scroll': function(v) {
501 terminal.keyboard.homeKeysScroll = v;
502 },
rginda4bba5e12012-06-20 16:15:30 -0700503
Robert Gindaa8165692015-06-15 14:46:31 -0700504 'keybindings': function(v) {
Joel Hockey95a9e272020-03-16 21:19:53 -0700505 loadKeyBindings(v, terminal.prefs_.get('keybindings-os-defaults'));
506 },
Robert Gindaa8165692015-06-15 14:46:31 -0700507
Joel Hockey95a9e272020-03-16 21:19:53 -0700508 'keybindings-os-defaults': function(v) {
509 loadKeyBindings(terminal.prefs_.get('keybindings'), v);
Robert Gindaa8165692015-06-15 14:46:31 -0700510 },
511
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700512 'media-keys-are-fkeys': function(v) {
513 terminal.keyboard.mediaKeysAreFKeys = v;
514 },
515
Robert Ginda57f03b42012-09-13 11:02:48 -0700516 'meta-sends-escape': function(v) {
517 terminal.keyboard.metaSendsEscape = v;
518 },
rginda30f20f62012-04-05 16:36:19 -0700519
Mike Frysinger847577f2017-05-23 23:25:57 -0400520 'mouse-right-click-paste': function(v) {
521 terminal.mouseRightClickPaste = v;
522 },
523
Robert Ginda57f03b42012-09-13 11:02:48 -0700524 'mouse-paste-button': function(v) {
525 terminal.syncMousePasteButton();
526 },
rgindaa8ba17d2012-08-15 14:41:10 -0700527
Robert Gindae76aa9f2014-03-14 12:29:12 -0700528 'page-keys-scroll': function(v) {
529 terminal.keyboard.pageKeysScroll = v;
530 },
531
Robert Ginda40932892012-12-10 17:26:40 -0800532 'pass-alt-number': function(v) {
533 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700534 // Let Alt+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800535 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500536 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800537 }
538
539 terminal.passAltNumber = v;
540 },
541
542 'pass-ctrl-number': function(v) {
543 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700544 // Let Ctrl+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800545 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500546 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800547 }
548
549 terminal.passCtrlNumber = v;
550 },
551
Joel Hockey0e052042020-02-19 05:37:19 -0800552 'pass-ctrl-n': function(v) {
553 terminal.passCtrlN = v;
554 },
555
556 'pass-ctrl-t': function(v) {
557 terminal.passCtrlT = v;
558 },
559
560 'pass-ctrl-tab': function(v) {
561 terminal.passCtrlTab = v;
562 },
563
564 'pass-ctrl-w': function(v) {
565 terminal.passCtrlW = v;
566 },
567
Robert Ginda40932892012-12-10 17:26:40 -0800568 'pass-meta-number': function(v) {
569 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700570 // Let Meta+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800571 // OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500572 v = (hterm.os == 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800573 }
574
575 terminal.passMetaNumber = v;
576 },
577
Marius Schilder77857b32014-05-14 16:21:26 -0700578 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700579 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700580 },
581
Robert Ginda8cb7d902013-06-20 14:37:18 -0700582 'receive-encoding': function(v) {
583 if (!(/^(utf-8|raw)$/).test(v)) {
584 console.warn('Invalid value for "receive-encoding": ' + v);
585 v = 'utf-8';
586 }
587
588 terminal.vt.characterEncoding = v;
589 },
590
Robert Ginda57f03b42012-09-13 11:02:48 -0700591 'scroll-on-keystroke': function(v) {
592 terminal.scrollOnKeystroke_ = v;
593 },
rginda9f5222b2012-03-05 11:53:28 -0800594
Robert Ginda57f03b42012-09-13 11:02:48 -0700595 'scroll-on-output': function(v) {
596 terminal.scrollOnOutput_ = v;
597 },
rginda30f20f62012-04-05 16:36:19 -0700598
Robert Ginda57f03b42012-09-13 11:02:48 -0700599 'scrollbar-visible': function(v) {
600 terminal.setScrollbarVisible(v);
601 },
rginda9f5222b2012-03-05 11:53:28 -0800602
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400603 'scroll-wheel-may-send-arrow-keys': function(v) {
604 terminal.scrollWheelArrowKeys_ = v;
605 },
606
Rob Spies49039e52014-12-17 13:40:04 -0800607 'scroll-wheel-move-multiplier': function(v) {
608 terminal.setScrollWheelMoveMultipler(v);
609 },
610
Robert Ginda57f03b42012-09-13 11:02:48 -0700611 'shift-insert-paste': function(v) {
612 terminal.keyboard.shiftInsertPaste = v;
613 },
rginda9f5222b2012-03-05 11:53:28 -0800614
Mike Frysingera7768922017-07-28 15:00:12 -0400615 'terminal-encoding': function(v) {
Mike Frysingera1371e12017-08-17 01:37:17 -0400616 terminal.vt.setEncoding(v);
Mike Frysingera7768922017-07-28 15:00:12 -0400617 },
618
Robert Gindae76aa9f2014-03-14 12:29:12 -0700619 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400620 terminal.scrollPort_.setUserCssUrl(v);
621 },
622
623 'user-css-text': function(v) {
624 terminal.scrollPort_.setUserCssText(v);
625 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400626
627 'word-break-match-left': function(v) {
628 terminal.primaryScreen_.wordBreakMatchLeft = v;
629 terminal.alternateScreen_.wordBreakMatchLeft = v;
630 },
631
632 'word-break-match-right': function(v) {
633 terminal.primaryScreen_.wordBreakMatchRight = v;
634 terminal.alternateScreen_.wordBreakMatchRight = v;
635 },
636
637 'word-break-match-middle': function(v) {
638 terminal.primaryScreen_.wordBreakMatchMiddle = v;
639 terminal.alternateScreen_.wordBreakMatchMiddle = v;
640 },
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400641
642 'allow-images-inline': function(v) {
643 terminal.allowImagesInline = v;
644 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700645 });
rginda30f20f62012-04-05 16:36:19 -0700646
Robert Ginda57f03b42012-09-13 11:02:48 -0700647 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800648 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700649
Mike Frysingerec4225d2020-04-07 05:00:01 -0400650 if (callback) {
651 callback();
Mike Frysingerbdb34802020-04-07 03:47:32 -0400652 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700653 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800654};
655
Rob Spies56953412014-04-28 14:09:47 -0700656/**
657 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500658 *
Joel Hockey0f933582019-08-27 18:01:51 -0700659 * @return {!hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700660 */
661hterm.Terminal.prototype.getPrefs = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700662 return lib.notNull(this.prefs_);
Rob Spies56953412014-04-28 14:09:47 -0700663};
664
Robert Gindaa063b202014-07-21 11:08:25 -0700665/**
666 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500667 *
668 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700669 */
670hterm.Terminal.prototype.setBracketedPaste = function(state) {
671 this.options_.bracketedPaste = state;
672};
Rob Spies56953412014-04-28 14:09:47 -0700673
rginda8e92a692012-05-20 19:37:20 -0700674/**
675 * Set the color for the cursor.
676 *
677 * If you want this setting to persist, set it through prefs_, rather than
678 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500679 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500680 * @param {string=} color The color to set. If not defined, we reset to the
681 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700682 */
683hterm.Terminal.prototype.setCursorColor = function(color) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400684 if (color === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700685 color = this.prefs_.getString('cursor-color');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400686 }
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500687
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400688 this.setCssVar('cursor-color', color);
rginda8e92a692012-05-20 19:37:20 -0700689};
690
691/**
692 * Return the current cursor color as a string.
Mike Frysinger23b5b832019-10-01 17:05:29 -0400693 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500694 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700695 */
696hterm.Terminal.prototype.getCursorColor = function() {
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400697 return this.getCssVar('cursor-color');
rginda8e92a692012-05-20 19:37:20 -0700698};
699
700/**
rgindad5613292012-06-19 15:40:37 -0700701 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500702 *
703 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700704 */
705hterm.Terminal.prototype.setSelectionEnabled = function(state) {
706 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700707};
708
709/**
rginda8e92a692012-05-20 19:37:20 -0700710 * Set the background color.
711 *
712 * If you want this setting to persist, set it through prefs_, rather than
713 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500714 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500715 * @param {string=} color The color to set. If not defined, we reset to the
716 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700717 */
718hterm.Terminal.prototype.setBackgroundColor = function(color) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400719 if (color === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700720 color = this.prefs_.getString('background-color');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400721 }
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500722
Joel Hockey42dba8f2020-03-26 16:21:11 -0700723 this.backgroundColor_ = lib.colors.normalizeCSS(color);
724 this.setRgbColorCssVar('background-color', this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700725};
726
rginda9f5222b2012-03-05 11:53:28 -0800727/**
728 * Return the current terminal background color.
729 *
730 * Intended for use by other classes, so we don't have to expose the entire
731 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500732 *
Joel Hockey42dba8f2020-03-26 16:21:11 -0700733 * @return {?string}
rginda9f5222b2012-03-05 11:53:28 -0800734 */
735hterm.Terminal.prototype.getBackgroundColor = function() {
Joel Hockey42dba8f2020-03-26 16:21:11 -0700736 return this.backgroundColor_;
rginda8e92a692012-05-20 19:37:20 -0700737};
738
739/**
740 * Set the foreground color.
741 *
742 * If you want this setting to persist, set it through prefs_, rather than
743 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500744 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500745 * @param {string=} color The color to set. If not defined, we reset to the
746 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700747 */
748hterm.Terminal.prototype.setForegroundColor = function(color) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400749 if (color === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700750 color = this.prefs_.getString('foreground-color');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400751 }
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500752
Joel Hockey42dba8f2020-03-26 16:21:11 -0700753 this.foregroundColor_ = lib.colors.normalizeCSS(color);
754 this.setRgbColorCssVar('foreground-color', this.foregroundColor_);
rginda9f5222b2012-03-05 11:53:28 -0800755};
756
757/**
758 * Return the current terminal foreground color.
759 *
760 * Intended for use by other classes, so we don't have to expose the entire
761 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500762 *
Joel Hockey42dba8f2020-03-26 16:21:11 -0700763 * @return {?string}
rginda9f5222b2012-03-05 11:53:28 -0800764 */
765hterm.Terminal.prototype.getForegroundColor = function() {
Joel Hockey42dba8f2020-03-26 16:21:11 -0700766 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800767};
768
769/**
rginda87b86462011-12-14 13:48:03 -0800770 * Create a new instance of a terminal command and run it with a given
771 * argument string.
772 *
Joel Hockeyd4fca732019-09-20 16:57:03 -0700773 * @param {!Function} commandClass The constructor for a terminal command.
Joel Hockey8081ea62019-08-26 16:52:32 -0700774 * @param {string} commandName The command to run for this terminal.
775 * @param {!Array<string>} args The arguments to pass to the command.
rginda87b86462011-12-14 13:48:03 -0800776 */
Joel Hockey8081ea62019-08-26 16:52:32 -0700777hterm.Terminal.prototype.runCommandClass = function(
778 commandClass, commandName, args) {
rgindaf522ce02012-04-17 17:49:17 -0700779 var environment = this.prefs_.get('environment');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400780 if (typeof environment != 'object' || environment == null) {
rgindaf522ce02012-04-17 17:49:17 -0700781 environment = {};
Mike Frysingerbdb34802020-04-07 03:47:32 -0400782 }
rgindaf522ce02012-04-17 17:49:17 -0700783
rginda87b86462011-12-14 13:48:03 -0800784 var self = this;
785 this.command = new commandClass(
Joel Hockey8081ea62019-08-26 16:52:32 -0700786 {
787 commandName: commandName,
788 args: args,
rginda87b86462011-12-14 13:48:03 -0800789 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700790 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800791 onExit: function(code) {
792 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800793 self.uninstallKeyboard();
Julian Watsondfbf8592019-11-05 18:05:12 +1100794 self.div_.dispatchEvent(new CustomEvent('terminal-closing'));
Mike Frysingerbdb34802020-04-07 03:47:32 -0400795 if (self.prefs_.get('close-on-exit')) {
796 window.close();
797 }
rginda87b86462011-12-14 13:48:03 -0800798 }
799 });
800
rgindafeaf3142012-01-31 15:14:20 -0800801 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800802 this.command.run();
803};
804
805/**
rgindafeaf3142012-01-31 15:14:20 -0800806 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500807 *
808 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800809 */
810hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700811 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800812};
813
814/**
815 * Install the keyboard handler for this terminal.
816 *
817 * This will prevent the browser from seeing any keystrokes sent to the
818 * terminal.
819 */
820hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700821 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400822};
rgindafeaf3142012-01-31 15:14:20 -0800823
824/**
825 * Uninstall the keyboard handler for this terminal.
826 */
827hterm.Terminal.prototype.uninstallKeyboard = function() {
828 this.keyboard.installKeyboard(null);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400829};
rgindafeaf3142012-01-31 15:14:20 -0800830
831/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400832 * Set a CSS variable.
833 *
834 * Normally this is used to set variables in the hterm namespace.
835 *
836 * @param {string} name The variable to set.
Joel Hockeyd4fca732019-09-20 16:57:03 -0700837 * @param {string|number} value The value to assign to the variable.
Mike Frysingerec4225d2020-04-07 05:00:01 -0400838 * @param {string=} prefix The variable namespace/prefix to use.
Mike Frysingercce97c42017-08-05 01:11:22 -0400839 */
840hterm.Terminal.prototype.setCssVar = function(name, value,
Mike Frysingerec4225d2020-04-07 05:00:01 -0400841 prefix = '--hterm-') {
Mike Frysingercce97c42017-08-05 01:11:22 -0400842 this.document_.documentElement.style.setProperty(
Mike Frysingerec4225d2020-04-07 05:00:01 -0400843 `${prefix}${name}`, value.toString());
Mike Frysingercce97c42017-08-05 01:11:22 -0400844};
845
846/**
Joel Hockey42dba8f2020-03-26 16:21:11 -0700847 * Sets --hterm-{name} to the cracked rgb components (no alpha) if the provided
848 * input is valid.
849 *
850 * @param {string} name The variable to set.
851 * @param {?string} rgb The rgb value to assign to the variable.
852 */
853hterm.Terminal.prototype.setRgbColorCssVar = function(name, rgb) {
854 const ary = rgb ? lib.colors.crackRGB(rgb) : null;
855 if (ary) {
856 this.setCssVar(name, ary.slice(0, 3).join(','));
857 }
858};
859
860/**
861 * Sets the specified color for the active screen.
862 *
863 * @param {number} i The index into the 256 color palette to set.
864 * @param {?string} rgb The rgb value to assign to the variable.
865 */
866hterm.Terminal.prototype.setColorPalette = function(i, rgb) {
867 if (i >= 0 && i < 256 && rgb != null && rgb != this.getColorPalette[i]) {
868 this.setRgbColorCssVar(`color-${i}`, rgb);
869 this.screen_.textAttributes.colorPaletteOverrides[i] = rgb;
870 }
871};
872
873/**
874 * Returns the current value in the active screen of the specified color.
875 *
876 * @param {number} i Color palette index.
877 * @return {string} rgb color.
878 */
879hterm.Terminal.prototype.getColorPalette = function(i) {
880 return this.screen_.textAttributes.colorPaletteOverrides[i] ||
881 lib.colors.colorPalette[i];
882};
883
884/**
885 * Reset the specified color in the active screen to its default value.
886 *
887 * @param {number} i Color to reset
888 */
889hterm.Terminal.prototype.resetColor = function(i) {
890 this.setColorPalette(i, lib.colors.colorPalette[i]);
891 delete this.screen_.textAttributes.colorPaletteOverrides[i];
892};
893
894/**
895 * Reset the current screen color palette to the default state.
896 */
897hterm.Terminal.prototype.resetColorPalette = function() {
898 this.screen_.textAttributes.colorPaletteOverrides.forEach(
899 (c, i) => this.resetColor(i));
900};
901
902/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500903 * Get a CSS variable.
904 *
905 * Normally this is used to get variables in the hterm namespace.
906 *
907 * @param {string} name The variable to read.
Mike Frysingerec4225d2020-04-07 05:00:01 -0400908 * @param {string=} prefix The variable namespace/prefix to use.
Mike Frysinger261597c2017-12-28 01:14:21 -0500909 * @return {string} The current setting for this variable.
910 */
Mike Frysingerec4225d2020-04-07 05:00:01 -0400911hterm.Terminal.prototype.getCssVar = function(name, prefix = '--hterm-') {
Mike Frysinger261597c2017-12-28 01:14:21 -0500912 return this.document_.documentElement.style.getPropertyValue(
Mike Frysingerec4225d2020-04-07 05:00:01 -0400913 `${prefix}${name}`);
Mike Frysinger261597c2017-12-28 01:14:21 -0500914};
915
916/**
Jason Linbbbdb752020-03-06 16:26:59 +1100917 * Update CSS character size variables to match the scrollport.
918 */
919hterm.Terminal.prototype.updateCssCharsize_ = function() {
920 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
921 this.setCssVar('charsize-height',
922 this.scrollPort_.characterSize.height + 'px');
923};
924
925/**
rginda35c456b2012-02-09 17:29:05 -0800926 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800927 *
928 * Call setFontSize(0) to reset to the default font size.
929 *
930 * This function does not modify the font-size preference.
931 *
932 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800933 */
934hterm.Terminal.prototype.setFontSize = function(px) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400935 if (px <= 0) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700936 px = this.prefs_.getNumber('font-size');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400937 }
rginda9f5222b2012-03-05 11:53:28 -0800938
rginda35c456b2012-02-09 17:29:05 -0800939 this.scrollPort_.setFontSize(px);
Jason Linbbbdb752020-03-06 16:26:59 +1100940 this.updateCssCharsize_();
rginda35c456b2012-02-09 17:29:05 -0800941};
942
943/**
944 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500945 *
946 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800947 */
948hterm.Terminal.prototype.getFontSize = function() {
949 return this.scrollPort_.getFontSize();
950};
951
952/**
rginda8e92a692012-05-20 19:37:20 -0700953 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500954 *
955 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700956 */
957hterm.Terminal.prototype.getFontFamily = function() {
958 return this.scrollPort_.getFontFamily();
959};
960
961/**
rginda35c456b2012-02-09 17:29:05 -0800962 * Set the CSS "font-family" for this terminal.
963 */
rginda9f5222b2012-03-05 11:53:28 -0800964hterm.Terminal.prototype.syncFontFamily = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700965 this.scrollPort_.setFontFamily(this.prefs_.getString('font-family'),
966 this.prefs_.getString('font-smoothing'));
Jason Linbbbdb752020-03-06 16:26:59 +1100967 this.updateCssCharsize_();
rginda9f5222b2012-03-05 11:53:28 -0800968 this.syncBoldSafeState();
969};
970
rginda4bba5e12012-06-20 16:15:30 -0700971/**
972 * Set this.mousePasteButton based on the mouse-paste-button pref,
973 * autodetecting if necessary.
974 */
975hterm.Terminal.prototype.syncMousePasteButton = function() {
976 var button = this.prefs_.get('mouse-paste-button');
977 if (typeof button == 'number') {
978 this.mousePasteButton = button;
979 return;
980 }
981
Mike Frysingeree81a002017-12-12 16:14:53 -0500982 if (hterm.os != 'linux') {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400983 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700984 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400985 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700986 }
987};
988
989/**
990 * Enable or disable bold based on the enable-bold pref, autodetecting if
991 * necessary.
992 */
rginda9f5222b2012-03-05 11:53:28 -0800993hterm.Terminal.prototype.syncBoldSafeState = function() {
994 var enableBold = this.prefs_.get('enable-bold');
995 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700996 this.primaryScreen_.textAttributes.enableBold = enableBold;
997 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800998 return;
999 }
1000
rgindaf7521392012-02-28 17:20:34 -08001001 var normalSize = this.scrollPort_.measureCharacterSize();
1002 var boldSize = this.scrollPort_.measureCharacterSize('bold');
1003
1004 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -08001005 if (!isBoldSafe) {
1006 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -07001007 'from normal. Font family is: ' +
1008 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -08001009 }
rginda9f5222b2012-03-05 11:53:28 -08001010
Robert Gindaed016262012-10-26 16:27:09 -07001011 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
1012 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -08001013};
1014
1015/**
Mike Frysinger261597c2017-12-28 01:14:21 -05001016 * Control text blinking behavior.
1017 *
1018 * @param {boolean=} state Whether to enable support for blinking text.
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001019 */
Mike Frysinger261597c2017-12-28 01:14:21 -05001020hterm.Terminal.prototype.setTextBlink = function(state) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001021 if (state === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -07001022 state = this.prefs_.getBoolean('enable-blink');
Mike Frysingerbdb34802020-04-07 03:47:32 -04001023 }
Mike Frysinger261597c2017-12-28 01:14:21 -05001024 this.setCssVar('blink-node-duration', state ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001025};
1026
1027/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001028 * Set the mouse cursor style based on the current terminal mode.
1029 */
1030hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -04001031 this.setCssVar('mouse-cursor-style',
1032 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
1033 'var(--hterm-mouse-cursor-text)' :
Mike Frysinger67f58f82018-11-22 13:38:22 -05001034 'var(--hterm-mouse-cursor-default)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001035};
1036
1037/**
rginda87b86462011-12-14 13:48:03 -08001038 * Return a copy of the current cursor position.
1039 *
Joel Hockey0f933582019-08-27 18:01:51 -07001040 * @return {!hterm.RowCol} The RowCol object representing the current position.
rginda87b86462011-12-14 13:48:03 -08001041 */
1042hterm.Terminal.prototype.saveCursor = function() {
1043 return this.screen_.cursorPosition.clone();
1044};
1045
Evan Jones2600d4f2016-12-06 09:29:36 -05001046/**
1047 * Return the current text attributes.
1048 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001049 * @return {!hterm.TextAttributes}
Evan Jones2600d4f2016-12-06 09:29:36 -05001050 */
rgindaa19afe22012-01-25 15:40:22 -08001051hterm.Terminal.prototype.getTextAttributes = function() {
1052 return this.screen_.textAttributes;
1053};
1054
Evan Jones2600d4f2016-12-06 09:29:36 -05001055/**
1056 * Set the text attributes.
1057 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001058 * @param {!hterm.TextAttributes} textAttributes The attributes to set.
Evan Jones2600d4f2016-12-06 09:29:36 -05001059 */
rginda1a09aa02012-06-18 21:11:25 -07001060hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
1061 this.screen_.textAttributes = textAttributes;
1062};
1063
rginda87b86462011-12-14 13:48:03 -08001064/**
rgindaf522ce02012-04-17 17:49:17 -07001065 * Return the current browser zoom factor applied to the terminal.
1066 *
1067 * @return {number} The current browser zoom factor.
1068 */
1069hterm.Terminal.prototype.getZoomFactor = function() {
1070 return this.scrollPort_.characterSize.zoomFactor;
1071};
1072
1073/**
rginda9846e2f2012-01-27 13:53:33 -08001074 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -05001075 *
1076 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -08001077 */
1078hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -08001079 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -08001080};
1081
1082/**
rginda87b86462011-12-14 13:48:03 -08001083 * Restore a previously saved cursor position.
1084 *
Joel Hockey0f933582019-08-27 18:01:51 -07001085 * @param {!hterm.RowCol} cursor The position to restore.
rginda87b86462011-12-14 13:48:03 -08001086 */
1087hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -07001088 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
1089 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -08001090 this.screen_.setCursorPosition(row, column);
1091 if (cursor.column > column ||
1092 cursor.column == column && cursor.overflow) {
1093 this.screen_.cursorPosition.overflow = true;
1094 }
rginda87b86462011-12-14 13:48:03 -08001095};
1096
1097/**
David Benjamin54e8bf62012-06-01 22:31:40 -04001098 * Clear the cursor's overflow flag.
1099 */
1100hterm.Terminal.prototype.clearCursorOverflow = function() {
1101 this.screen_.cursorPosition.overflow = false;
1102};
1103
1104/**
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001105 * Save the current cursor state to the corresponding screens.
1106 *
1107 * See the hterm.Screen.CursorState class for more details.
1108 *
1109 * @param {boolean=} both If true, update both screens, else only update the
1110 * current screen.
1111 */
1112hterm.Terminal.prototype.saveCursorAndState = function(both) {
1113 if (both) {
1114 this.primaryScreen_.saveCursorAndState(this.vt);
1115 this.alternateScreen_.saveCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001116 } else {
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001117 this.screen_.saveCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001118 }
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001119};
1120
1121/**
1122 * Restore the saved cursor state in the corresponding screens.
1123 *
1124 * See the hterm.Screen.CursorState class for more details.
1125 *
1126 * @param {boolean=} both If true, update both screens, else only update the
1127 * current screen.
1128 */
1129hterm.Terminal.prototype.restoreCursorAndState = function(both) {
1130 if (both) {
1131 this.primaryScreen_.restoreCursorAndState(this.vt);
1132 this.alternateScreen_.restoreCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001133 } else {
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001134 this.screen_.restoreCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001135 }
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001136};
1137
1138/**
Robert Ginda830583c2013-08-07 13:20:46 -07001139 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001140 *
1141 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -07001142 */
1143hterm.Terminal.prototype.setCursorShape = function(shape) {
1144 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001145 this.restyleCursor_();
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001146};
Robert Ginda830583c2013-08-07 13:20:46 -07001147
1148/**
1149 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001150 *
1151 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -07001152 */
1153hterm.Terminal.prototype.getCursorShape = function() {
1154 return this.cursorShape_;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001155};
Robert Ginda830583c2013-08-07 13:20:46 -07001156
1157/**
rginda87b86462011-12-14 13:48:03 -08001158 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001159 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001160 * @param {?number} columnCount
rginda87b86462011-12-14 13:48:03 -08001161 */
1162hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -08001163 if (columnCount == null) {
1164 this.div_.style.width = '100%';
1165 return;
1166 }
1167
Robert Ginda26806d12014-07-24 13:44:07 -07001168 this.div_.style.width = Math.ceil(
1169 this.scrollPort_.characterSize.width *
1170 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001171 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -08001172 this.scheduleSyncCursorPosition_();
1173};
rginda87b86462011-12-14 13:48:03 -08001174
rgindac9bc5502012-01-18 11:48:44 -08001175/**
rginda35c456b2012-02-09 17:29:05 -08001176 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001177 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001178 * @param {?number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -08001179 */
1180hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -08001181 if (rowCount == null) {
1182 this.div_.style.height = '100%';
1183 return;
1184 }
1185
rginda35c456b2012-02-09 17:29:05 -08001186 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -07001187 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -08001188 this.realizeSize_(this.screenSize.width, rowCount);
1189 this.scheduleSyncCursorPosition_();
1190};
1191
1192/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001193 * Deal with terminal size changes.
1194 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001195 * @param {number} columnCount The number of columns.
1196 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001197 */
1198hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
Mike Frysinger0206e262019-06-13 10:18:19 -04001199 let notify = false;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001200
Mike Frysinger0206e262019-06-13 10:18:19 -04001201 if (columnCount != this.screenSize.width) {
1202 notify = true;
1203 this.realizeWidth_(columnCount);
1204 }
1205
1206 if (rowCount != this.screenSize.height) {
1207 notify = true;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001208 this.realizeHeight_(rowCount);
Mike Frysinger0206e262019-06-13 10:18:19 -04001209 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001210
1211 // Send new terminal size to plugin.
Mike Frysinger0206e262019-06-13 10:18:19 -04001212 if (notify) {
1213 this.io.onTerminalResize_(columnCount, rowCount);
1214 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001215};
1216
1217/**
rgindac9bc5502012-01-18 11:48:44 -08001218 * Deal with terminal width changes.
1219 *
1220 * This function does what needs to be done when the terminal width changes
1221 * out from under us. It happens here rather than in onResize_() because this
1222 * code may need to run synchronously to handle programmatic changes of
1223 * terminal width.
1224 *
1225 * Relying on the browser to send us an async resize event means we may not be
1226 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001227 *
1228 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001229 */
1230hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001231 if (columnCount <= 0) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001232 throw new Error('Attempt to realize bad width: ' + columnCount);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001233 }
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001234
rgindac9bc5502012-01-18 11:48:44 -08001235 var deltaColumns = columnCount - this.screen_.getWidth();
Mike Frysinger0206e262019-06-13 10:18:19 -04001236 if (deltaColumns == 0) {
1237 // No change, so don't bother recalculating things.
1238 return;
1239 }
rgindac9bc5502012-01-18 11:48:44 -08001240
rginda87b86462011-12-14 13:48:03 -08001241 this.screenSize.width = columnCount;
1242 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001243
1244 if (deltaColumns > 0) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001245 if (this.defaultTabStops) {
David Benjamin66e954d2012-05-05 21:08:12 -04001246 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001247 }
rgindac9bc5502012-01-18 11:48:44 -08001248 } else {
1249 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001250 if (this.tabStops_[i] < columnCount) {
rgindac9bc5502012-01-18 11:48:44 -08001251 break;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001252 }
rgindac9bc5502012-01-18 11:48:44 -08001253
1254 this.tabStops_.pop();
1255 }
1256 }
1257
1258 this.screen_.setColumnCount(this.screenSize.width);
1259};
1260
1261/**
1262 * Deal with terminal height changes.
1263 *
1264 * This function does what needs to be done when the terminal height changes
1265 * out from under us. It happens here rather than in onResize_() because this
1266 * code may need to run synchronously to handle programmatic changes of
1267 * terminal height.
1268 *
1269 * Relying on the browser to send us an async resize event means we may not be
1270 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001271 *
1272 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001273 */
1274hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001275 if (rowCount <= 0) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001276 throw new Error('Attempt to realize bad height: ' + rowCount);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001277 }
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001278
rgindac9bc5502012-01-18 11:48:44 -08001279 var deltaRows = rowCount - this.screen_.getHeight();
Mike Frysinger0206e262019-06-13 10:18:19 -04001280 if (deltaRows == 0) {
1281 // No change, so don't bother recalculating things.
1282 return;
1283 }
rgindac9bc5502012-01-18 11:48:44 -08001284
1285 this.screenSize.height = rowCount;
1286
1287 var cursor = this.saveCursor();
1288
1289 if (deltaRows < 0) {
1290 // Screen got smaller.
1291 deltaRows *= -1;
1292 while (deltaRows) {
1293 var lastRow = this.getRowCount() - 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001294 if (lastRow - this.scrollbackRows_.length == cursor.row) {
rgindac9bc5502012-01-18 11:48:44 -08001295 break;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001296 }
rgindac9bc5502012-01-18 11:48:44 -08001297
Mike Frysingerbdb34802020-04-07 03:47:32 -04001298 if (this.getRowText(lastRow)) {
rgindac9bc5502012-01-18 11:48:44 -08001299 break;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001300 }
rgindac9bc5502012-01-18 11:48:44 -08001301
1302 this.screen_.popRow();
1303 deltaRows--;
1304 }
1305
1306 var ary = this.screen_.shiftRows(deltaRows);
1307 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1308
1309 // We just removed rows from the top of the screen, we need to update
1310 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001311 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001312 } else if (deltaRows > 0) {
1313 // Screen got larger.
1314
1315 if (deltaRows <= this.scrollbackRows_.length) {
1316 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1317 var rows = this.scrollbackRows_.splice(
1318 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1319 this.screen_.unshiftRows(rows);
1320 deltaRows -= scrollbackCount;
1321 cursor.row += scrollbackCount;
1322 }
1323
Mike Frysingerbdb34802020-04-07 03:47:32 -04001324 if (deltaRows) {
rgindac9bc5502012-01-18 11:48:44 -08001325 this.appendRows_(deltaRows);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001326 }
rgindac9bc5502012-01-18 11:48:44 -08001327 }
1328
rginda35c456b2012-02-09 17:29:05 -08001329 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001330 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001331};
1332
1333/**
1334 * Scroll the terminal to the top of the scrollback buffer.
1335 */
1336hterm.Terminal.prototype.scrollHome = function() {
1337 this.scrollPort_.scrollRowToTop(0);
1338};
1339
1340/**
1341 * Scroll the terminal to the end.
1342 */
1343hterm.Terminal.prototype.scrollEnd = function() {
1344 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1345};
1346
1347/**
1348 * Scroll the terminal one page up (minus one line) relative to the current
1349 * position.
1350 */
1351hterm.Terminal.prototype.scrollPageUp = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001352 this.scrollPort_.scrollPageUp();
rginda87b86462011-12-14 13:48:03 -08001353};
1354
1355/**
1356 * Scroll the terminal one page down (minus one line) relative to the current
1357 * position.
1358 */
1359hterm.Terminal.prototype.scrollPageDown = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001360 this.scrollPort_.scrollPageDown();
rginda8ba33642011-12-14 12:31:31 -08001361};
1362
rgindac9bc5502012-01-18 11:48:44 -08001363/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001364 * Scroll the terminal one line up relative to the current position.
1365 */
1366hterm.Terminal.prototype.scrollLineUp = function() {
1367 var i = this.scrollPort_.getTopRowIndex();
1368 this.scrollPort_.scrollRowToTop(i - 1);
1369};
1370
1371/**
1372 * Scroll the terminal one line down relative to the current position.
1373 */
1374hterm.Terminal.prototype.scrollLineDown = function() {
1375 var i = this.scrollPort_.getTopRowIndex();
1376 this.scrollPort_.scrollRowToTop(i + 1);
1377};
1378
1379/**
Robert Ginda40932892012-12-10 17:26:40 -08001380 * Clear primary screen, secondary screen, and the scrollback buffer.
1381 */
1382hterm.Terminal.prototype.wipeContents = function() {
Mike Frysinger9c482b82018-09-07 02:49:36 -04001383 this.clearHome(this.primaryScreen_);
1384 this.clearHome(this.alternateScreen_);
1385
1386 this.clearScrollback();
1387};
1388
1389/**
1390 * Clear scrollback buffer.
1391 */
1392hterm.Terminal.prototype.clearScrollback = function() {
1393 // Move to the end of the buffer in case the screen was scrolled back.
1394 // We're going to throw it away which would leave the display invalid.
1395 this.scrollEnd();
1396
Robert Ginda40932892012-12-10 17:26:40 -08001397 this.scrollbackRows_.length = 0;
1398 this.scrollPort_.resetCache();
1399
Mike Frysinger9c482b82018-09-07 02:49:36 -04001400 [this.primaryScreen_, this.alternateScreen_].forEach((screen) => {
1401 const bottom = screen.getHeight();
1402 this.renumberRows_(0, bottom, screen);
1403 });
Robert Ginda40932892012-12-10 17:26:40 -08001404
1405 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001406 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001407};
1408
1409/**
rgindac9bc5502012-01-18 11:48:44 -08001410 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001411 *
1412 * Perform a full reset to the default values listed in
1413 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001414 */
rginda87b86462011-12-14 13:48:03 -08001415hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001416 this.vt.reset();
1417
rgindac9bc5502012-01-18 11:48:44 -08001418 this.clearAllTabStops();
1419 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001420
Joel Hockey42dba8f2020-03-26 16:21:11 -07001421 this.resetColorPalette();
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001422 const resetScreen = (screen) => {
1423 // We want to make sure to reset the attributes before we clear the screen.
1424 // The attributes might be used to initialize default/empty rows.
1425 screen.textAttributes.reset();
Joel Hockey42dba8f2020-03-26 16:21:11 -07001426 screen.textAttributes.colorPaletteOverrides = [];
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001427 this.clearHome(screen);
1428 screen.saveCursorAndState(this.vt);
1429 };
1430 resetScreen(this.primaryScreen_);
1431 resetScreen(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001432
Mike Frysinger84301d02017-11-29 13:28:46 -08001433 // Reset terminal options to their default values.
1434 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001435 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1436
Mike Frysinger84301d02017-11-29 13:28:46 -08001437 this.setVTScrollRegion(null, null);
1438
1439 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001440};
1441
rgindac9bc5502012-01-18 11:48:44 -08001442/**
1443 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001444 *
1445 * Perform a soft reset to the default values listed in
1446 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001447 */
rginda0f5c0292012-01-13 11:00:13 -08001448hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001449 this.vt.reset();
1450
rgindab8bc8932012-04-27 12:45:03 -07001451 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001452 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001453
Brad Townb62dfdc2015-03-16 19:07:15 -07001454 // We show the cursor on soft reset but do not alter the blink state.
1455 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1456
Joel Hockey42dba8f2020-03-26 16:21:11 -07001457 this.resetColorPalette();
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001458 const resetScreen = (screen) => {
1459 // Xterm also resets the color palette on soft reset, even though it doesn't
1460 // seem to be documented anywhere.
1461 screen.textAttributes.reset();
Joel Hockey42dba8f2020-03-26 16:21:11 -07001462 screen.textAttributes.colorPaletteOverrides = [];
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001463 screen.saveCursorAndState(this.vt);
1464 };
1465 resetScreen(this.primaryScreen_);
1466 resetScreen(this.alternateScreen_);
rgindaf522ce02012-04-17 17:49:17 -07001467
rgindab8bc8932012-04-27 12:45:03 -07001468 // The xterm man page explicitly says this will happen on soft reset.
1469 this.setVTScrollRegion(null, null);
1470
1471 // Xterm also shows the cursor on soft reset, but does not alter the blink
1472 // state.
rgindaa19afe22012-01-25 15:40:22 -08001473 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001474};
1475
rgindac9bc5502012-01-18 11:48:44 -08001476/**
1477 * Move the cursor forward to the next tab stop, or to the last column
1478 * if no more tab stops are set.
1479 */
1480hterm.Terminal.prototype.forwardTabStop = function() {
1481 var column = this.screen_.cursorPosition.column;
1482
1483 for (var i = 0; i < this.tabStops_.length; i++) {
1484 if (this.tabStops_[i] > column) {
1485 this.setCursorColumn(this.tabStops_[i]);
1486 return;
1487 }
1488 }
1489
David Benjamin66e954d2012-05-05 21:08:12 -04001490 // xterm does not clear the overflow flag on HT or CHT.
1491 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001492 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001493 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001494};
1495
rgindac9bc5502012-01-18 11:48:44 -08001496/**
1497 * Move the cursor backward to the previous tab stop, or to the first column
1498 * if no previous tab stops are set.
1499 */
1500hterm.Terminal.prototype.backwardTabStop = function() {
1501 var column = this.screen_.cursorPosition.column;
1502
1503 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1504 if (this.tabStops_[i] < column) {
1505 this.setCursorColumn(this.tabStops_[i]);
1506 return;
1507 }
1508 }
1509
1510 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001511};
1512
rgindac9bc5502012-01-18 11:48:44 -08001513/**
1514 * Set a tab stop at the given column.
1515 *
Joel Hockey0f933582019-08-27 18:01:51 -07001516 * @param {number} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001517 */
1518hterm.Terminal.prototype.setTabStop = function(column) {
1519 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001520 if (this.tabStops_[i] == column) {
rgindac9bc5502012-01-18 11:48:44 -08001521 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001522 }
rgindac9bc5502012-01-18 11:48:44 -08001523
1524 if (this.tabStops_[i] < column) {
1525 this.tabStops_.splice(i + 1, 0, column);
1526 return;
1527 }
1528 }
1529
1530 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001531};
1532
rgindac9bc5502012-01-18 11:48:44 -08001533/**
1534 * Clear the tab stop at the current cursor position.
1535 *
1536 * No effect if there is no tab stop at the current cursor position.
1537 */
1538hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1539 var column = this.screen_.cursorPosition.column;
1540
1541 var i = this.tabStops_.indexOf(column);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001542 if (i == -1) {
rgindac9bc5502012-01-18 11:48:44 -08001543 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001544 }
rgindac9bc5502012-01-18 11:48:44 -08001545
1546 this.tabStops_.splice(i, 1);
1547};
1548
1549/**
1550 * Clear all tab stops.
1551 */
1552hterm.Terminal.prototype.clearAllTabStops = function() {
1553 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001554 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001555};
1556
1557/**
1558 * Set up the default tab stops, starting from a given column.
1559 *
1560 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001561 * from the specified column, or 0 if no column is provided. It also flags
1562 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001563 *
1564 * This does not clear the existing tab stops first, use clearAllTabStops
1565 * for that.
1566 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04001567 * @param {number=} start Optional starting zero based starting column,
Joel Hockey0f933582019-08-27 18:01:51 -07001568 * useful for filling out missing tab stops when the terminal is resized.
rgindac9bc5502012-01-18 11:48:44 -08001569 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04001570hterm.Terminal.prototype.setDefaultTabStops = function(start = 0) {
rgindac9bc5502012-01-18 11:48:44 -08001571 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001572 // Round start up to a default tab stop.
1573 start = start - 1 - ((start - 1) % w) + w;
1574 for (var i = start; i < this.screenSize.width; i += w) {
1575 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001576 }
David Benjamin66e954d2012-05-05 21:08:12 -04001577
1578 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001579};
1580
rginda6d397402012-01-17 10:58:29 -08001581/**
rginda8ba33642011-12-14 12:31:31 -08001582 * Interpret a sequence of characters.
1583 *
1584 * Incomplete escape sequences are buffered until the next call.
1585 *
1586 * @param {string} str Sequence of characters to interpret or pass through.
1587 */
1588hterm.Terminal.prototype.interpret = function(str) {
rginda8ba33642011-12-14 12:31:31 -08001589 this.scheduleSyncCursorPosition_();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001590 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001591};
1592
1593/**
1594 * Take over the given DIV for use as the terminal display.
1595 *
Joel Hockey0f933582019-08-27 18:01:51 -07001596 * @param {!Element} div The div to use as the terminal display.
rginda8ba33642011-12-14 12:31:31 -08001597 */
1598hterm.Terminal.prototype.decorate = function(div) {
Mike Frysinger5768a9d2017-12-26 12:57:44 -05001599 const charset = div.ownerDocument.characterSet.toLowerCase();
1600 if (charset != 'utf-8') {
1601 console.warn(`Document encoding should be set to utf-8, not "${charset}";` +
1602 ` Add <meta charset='utf-8'/> to your HTML <head> to fix.`);
1603 }
1604
rginda87b86462011-12-14 13:48:03 -08001605 this.div_ = div;
1606
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001607 this.accessibilityReader_ = new hterm.AccessibilityReader(div);
1608
Adrián Pérez-Orozco394e64f2018-12-17 17:20:16 -08001609 this.scrollPort_.decorate(div, () => this.setupScrollPort_());
1610};
1611
1612/**
1613 * Initialisation of ScrollPort properties which need to be set after its DOM
1614 * has been initialised.
Mike Frysinger23b5b832019-10-01 17:05:29 -04001615 *
Adrián Pérez-Orozco394e64f2018-12-17 17:20:16 -08001616 * @private
1617 */
1618hterm.Terminal.prototype.setupScrollPort_ = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -07001619 this.scrollPort_.setBackgroundImage(
1620 this.prefs_.getString('background-image'));
1621 this.scrollPort_.setBackgroundSize(this.prefs_.getString('background-size'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001622 this.scrollPort_.setBackgroundPosition(
Joel Hockeyd4fca732019-09-20 16:57:03 -07001623 this.prefs_.getString('background-position'));
1624 this.scrollPort_.setUserCssUrl(this.prefs_.getString('user-css'));
1625 this.scrollPort_.setUserCssText(this.prefs_.getString('user-css-text'));
1626 this.scrollPort_.setAccessibilityReader(
1627 lib.notNull(this.accessibilityReader_));
rginda30f20f62012-04-05 16:36:19 -07001628
rginda0918b652012-04-04 11:26:24 -07001629 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001630
Joel Hockeyd4fca732019-09-20 16:57:03 -07001631 this.setFontSize(this.prefs_.getNumber('font-size'));
rginda9f5222b2012-03-05 11:53:28 -08001632 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001633
Joel Hockeyd4fca732019-09-20 16:57:03 -07001634 this.setScrollbarVisible(this.prefs_.getBoolean('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001635 this.setScrollWheelMoveMultipler(
Joel Hockeyd4fca732019-09-20 16:57:03 -07001636 this.prefs_.getNumber('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001637
rginda8ba33642011-12-14 12:31:31 -08001638 this.document_ = this.scrollPort_.getDocument();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001639 this.accessibilityReader_.decorate(this.document_);
rginda8ba33642011-12-14 12:31:31 -08001640
Evan Jones5f9df812016-12-06 09:38:58 -05001641 this.document_.body.oncontextmenu = function() { return false; };
Mike Frysingercc114512017-09-11 21:39:17 -04001642 this.contextMenu.setDocument(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07001643
1644 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001645 var screenNode = this.scrollPort_.getScreenNode();
Joel Hockeyd4fca732019-09-20 16:57:03 -07001646 screenNode.addEventListener(
1647 'mousedown', /** @type {!EventListener} */ (onMouse));
1648 screenNode.addEventListener(
1649 'mouseup', /** @type {!EventListener} */ (onMouse));
1650 screenNode.addEventListener(
1651 'mousemove', /** @type {!EventListener} */ (onMouse));
rginda4bba5e12012-06-20 16:15:30 -07001652 this.scrollPort_.onScrollWheel = onMouse;
1653
Joel Hockeyd4fca732019-09-20 16:57:03 -07001654 screenNode.addEventListener(
1655 'keydown',
1656 /** @type {!EventListener} */ (this.onKeyboardActivity_.bind(this)));
Mike Frysinger02ded6d2018-06-21 14:25:20 -04001657
Toni Barzic0bfa8922013-11-22 11:18:35 -08001658 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001659 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001660 // Listen for mousedown events on the screenNode as in FF the focus
1661 // events don't bubble.
1662 screenNode.addEventListener('mousedown', function() {
1663 setTimeout(this.onFocusChange_.bind(this, true));
1664 }.bind(this));
1665
Toni Barzic0bfa8922013-11-22 11:18:35 -08001666 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001667 'blur', this.onFocusChange_.bind(this, false));
1668
1669 var style = this.document_.createElement('style');
Joel Hockeyd36efd62019-09-30 14:16:20 -07001670 style.textContent = `
1671.cursor-node[focus="false"] {
1672 box-sizing: border-box;
1673 background-color: transparent !important;
1674 border-width: 2px;
1675 border-style: solid;
1676}
1677menu {
1678 margin: 0;
1679 padding: 0;
1680 cursor: var(--hterm-mouse-cursor-pointer);
1681}
1682menuitem {
1683 white-space: nowrap;
1684 border-bottom: 1px dashed;
1685 display: block;
1686 padding: 0.3em 0.3em 0 0.3em;
1687}
1688menuitem.separator {
1689 border-bottom: none;
1690 height: 0.5em;
1691 padding: 0;
1692}
1693menuitem:hover {
1694 color: var(--hterm-cursor-color);
1695}
1696.wc-node {
1697 display: inline-block;
1698 text-align: center;
1699 width: calc(var(--hterm-charsize-width) * 2);
1700 line-height: var(--hterm-charsize-height);
1701}
1702:root {
1703 --hterm-charsize-width: ${this.scrollPort_.characterSize.width}px;
1704 --hterm-charsize-height: ${this.scrollPort_.characterSize.height}px;
1705 /* Default position hides the cursor for when the window is initializing. */
1706 --hterm-cursor-offset-col: -1;
1707 --hterm-cursor-offset-row: -1;
1708 --hterm-blink-node-duration: 0.7s;
1709 --hterm-mouse-cursor-default: default;
1710 --hterm-mouse-cursor-text: text;
1711 --hterm-mouse-cursor-pointer: pointer;
1712 --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);
Joel Hockey42dba8f2020-03-26 16:21:11 -07001713
Joel Hockey42dba8f2020-03-26 16:21:11 -07001714${lib.colors.stockColorPalette.map((c, i) => `
1715 --hterm-color-${i}: ${lib.colors.crackRGB(c).slice(0, 3).join(',')};
1716`).join('')}
Joel Hockeyd36efd62019-09-30 14:16:20 -07001717}
1718.uri-node:hover {
1719 text-decoration: underline;
1720 cursor: var(--hterm-mouse-cursor-pointer);
1721}
1722@keyframes blink {
1723 from { opacity: 1.0; }
1724 to { opacity: 0.0; }
1725}
1726.blink-node {
1727 animation-name: blink;
1728 animation-duration: var(--hterm-blink-node-duration);
1729 animation-iteration-count: infinite;
1730 animation-timing-function: ease-in-out;
1731 animation-direction: alternate;
1732}`;
Mike Frysingerb74a6472018-06-22 13:37:08 -04001733 // Insert this stock style as the first node so that any user styles will
1734 // override w/out having to use !important everywhere. The rules above mix
1735 // runtime variables with default ones designed to be overridden by the user,
1736 // but we can wait for a concrete case from the users to determine the best
1737 // way to split the sheet up to before & after the user-css settings.
1738 this.document_.head.insertBefore(style, this.document_.head.firstChild);
rginda8e92a692012-05-20 19:37:20 -07001739
rginda8ba33642011-12-14 12:31:31 -08001740 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001741 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001742 this.cursorNode_.className = 'cursor-node';
Joel Hockeyd36efd62019-09-30 14:16:20 -07001743 this.cursorNode_.style.cssText = `
1744position: absolute;
1745left: calc(var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));
1746top: calc(var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));
1747display: ${this.options_.cursorVisible ? '' : 'none'};
1748width: var(--hterm-charsize-width);
1749height: var(--hterm-charsize-height);
1750background-color: var(--hterm-cursor-color);
1751border-color: var(--hterm-cursor-color);
1752-webkit-transition: opacity, background-color 100ms linear;
1753-moz-transition: opacity, background-color 100ms linear;`;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001754
Mike Frysingerf02a2cb2017-12-21 00:34:03 -05001755 this.setCursorColor();
Robert Gindafb1be6a2013-12-11 11:56:22 -08001756 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1757 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001758
rginda8ba33642011-12-14 12:31:31 -08001759 this.document_.body.appendChild(this.cursorNode_);
1760
rgindad5613292012-06-19 15:40:37 -07001761 // When 'enableMouseDragScroll' is off we reposition this element directly
1762 // under the mouse cursor after a click. This makes Chrome associate
1763 // subsequent mousemove events with the scroll-blocker. Since the
1764 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1765 // events do not cause the scrollport to scroll.
1766 //
1767 // It's a hack, but it's the cleanest way I could find.
1768 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001769 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
Raymes Khoury6dce2f82018-04-12 15:38:58 +10001770 this.scrollBlockerNode_.setAttribute('aria-hidden', 'true');
rgindad5613292012-06-19 15:40:37 -07001771 this.scrollBlockerNode_.style.cssText =
1772 ('position: absolute;' +
1773 'top: -99px;' +
1774 'display: block;' +
1775 'width: 10px;' +
1776 'height: 10px;');
1777 this.document_.body.appendChild(this.scrollBlockerNode_);
1778
rgindad5613292012-06-19 15:40:37 -07001779 this.scrollPort_.onScrollWheel = onMouse;
1780 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1781 ].forEach(function(event) {
1782 this.scrollBlockerNode_.addEventListener(event, onMouse);
Joel Hockeyd4fca732019-09-20 16:57:03 -07001783 this.cursorNode_.addEventListener(
1784 event, /** @type {!EventListener} */ (onMouse));
1785 this.document_.addEventListener(
1786 event, /** @type {!EventListener} */ (onMouse));
rgindad5613292012-06-19 15:40:37 -07001787 }.bind(this));
1788
1789 this.cursorNode_.addEventListener('mousedown', function() {
1790 setTimeout(this.focus.bind(this));
1791 }.bind(this));
1792
rginda8ba33642011-12-14 12:31:31 -08001793 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001794
rginda87b86462011-12-14 13:48:03 -08001795 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001796 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001797};
1798
rginda0918b652012-04-04 11:26:24 -07001799/**
1800 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001801 *
Joel Hockey0f933582019-08-27 18:01:51 -07001802 * @return {!Document}
rginda0918b652012-04-04 11:26:24 -07001803 */
rginda87b86462011-12-14 13:48:03 -08001804hterm.Terminal.prototype.getDocument = function() {
1805 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001806};
1807
1808/**
rginda0918b652012-04-04 11:26:24 -07001809 * Focus the terminal.
1810 */
1811hterm.Terminal.prototype.focus = function() {
1812 this.scrollPort_.focus();
1813};
1814
1815/**
Theodore Duboiscea9b782019-09-02 17:48:00 -07001816 * Unfocus the terminal.
1817 */
1818hterm.Terminal.prototype.blur = function() {
1819 this.scrollPort_.blur();
1820};
1821
1822/**
rginda8ba33642011-12-14 12:31:31 -08001823 * Return the HTML Element for a given row index.
1824 *
1825 * This is a method from the RowProvider interface. The ScrollPort uses
1826 * it to fetch rows on demand as they are scrolled into view.
1827 *
1828 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1829 * pairs to conserve memory.
1830 *
Joel Hockey0f933582019-08-27 18:01:51 -07001831 * @param {number} index The zero-based row index, measured relative to the
rginda8ba33642011-12-14 12:31:31 -08001832 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001833 * largest indices.
Joel Hockey0f933582019-08-27 18:01:51 -07001834 * @return {!Element} The 'x-row' element containing for the requested row.
Joel Hockeyd4fca732019-09-20 16:57:03 -07001835 * @override
rginda8ba33642011-12-14 12:31:31 -08001836 */
1837hterm.Terminal.prototype.getRowNode = function(index) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001838 if (index < this.scrollbackRows_.length) {
rginda8ba33642011-12-14 12:31:31 -08001839 return this.scrollbackRows_[index];
Mike Frysingerbdb34802020-04-07 03:47:32 -04001840 }
rginda8ba33642011-12-14 12:31:31 -08001841
1842 var screenIndex = index - this.scrollbackRows_.length;
1843 return this.screen_.rowsArray[screenIndex];
1844};
1845
1846/**
1847 * Return the text content for a given range of rows.
1848 *
1849 * This is a method from the RowProvider interface. The ScrollPort uses
1850 * it to fetch text content on demand when the user attempts to copy their
1851 * selection to the clipboard.
1852 *
Joel Hockey0f933582019-08-27 18:01:51 -07001853 * @param {number} start The zero-based row index to start from, measured
rginda8ba33642011-12-14 12:31:31 -08001854 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001855 * always have the largest indices.
Joel Hockey0f933582019-08-27 18:01:51 -07001856 * @param {number} end The zero-based row index to end on, measured
rginda8ba33642011-12-14 12:31:31 -08001857 * relative to the start of the scrollback buffer.
1858 * @return {string} A single string containing the text value of the range of
1859 * rows. Lines will be newline delimited, with no trailing newline.
1860 */
1861hterm.Terminal.prototype.getRowsText = function(start, end) {
1862 var ary = [];
1863 for (var i = start; i < end; i++) {
1864 var node = this.getRowNode(i);
1865 ary.push(node.textContent);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001866 if (i < end - 1 && !node.getAttribute('line-overflow')) {
rgindaa09e7332012-08-17 12:49:51 -07001867 ary.push('\n');
Mike Frysingerbdb34802020-04-07 03:47:32 -04001868 }
rginda8ba33642011-12-14 12:31:31 -08001869 }
1870
rgindaa09e7332012-08-17 12:49:51 -07001871 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001872};
1873
1874/**
1875 * Return the text content for a given row.
1876 *
1877 * This is a method from the RowProvider interface. The ScrollPort uses
1878 * it to fetch text content on demand when the user attempts to copy their
1879 * selection to the clipboard.
1880 *
Joel Hockey0f933582019-08-27 18:01:51 -07001881 * @param {number} index The zero-based row index to return, measured
rginda8ba33642011-12-14 12:31:31 -08001882 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001883 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001884 * @return {string} A string containing the text value of the selected row.
1885 */
1886hterm.Terminal.prototype.getRowText = function(index) {
1887 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001888 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001889};
1890
1891/**
1892 * Return the total number of rows in the addressable screen and in the
1893 * scrollback buffer of this terminal.
1894 *
1895 * This is a method from the RowProvider interface. The ScrollPort uses
1896 * it to compute the size of the scrollbar.
1897 *
Joel Hockey0f933582019-08-27 18:01:51 -07001898 * @return {number} The number of rows in this terminal.
Joel Hockeyd4fca732019-09-20 16:57:03 -07001899 * @override
rginda8ba33642011-12-14 12:31:31 -08001900 */
1901hterm.Terminal.prototype.getRowCount = function() {
1902 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1903};
1904
1905/**
1906 * Create DOM nodes for new rows and append them to the end of the terminal.
1907 *
1908 * This is the only correct way to add a new DOM node for a row. Notice that
1909 * the new row is appended to the bottom of the list of rows, and does not
1910 * require renumbering (of the rowIndex property) of previous rows.
1911 *
1912 * If you think you want a new blank row somewhere in the middle of the
1913 * terminal, look into moveRows_().
1914 *
1915 * This method does not pay attention to vtScrollTop/Bottom, since you should
1916 * be using moveRows() in cases where they would matter.
1917 *
1918 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001919 *
1920 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001921 */
1922hterm.Terminal.prototype.appendRows_ = function(count) {
1923 var cursorRow = this.screen_.rowsArray.length;
1924 var offset = this.scrollbackRows_.length + cursorRow;
1925 for (var i = 0; i < count; i++) {
1926 var row = this.document_.createElement('x-row');
1927 row.appendChild(this.document_.createTextNode(''));
1928 row.rowIndex = offset + i;
1929 this.screen_.pushRow(row);
1930 }
1931
1932 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1933 if (extraRows > 0) {
1934 var ary = this.screen_.shiftRows(extraRows);
1935 Array.prototype.push.apply(this.scrollbackRows_, ary);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001936 if (this.scrollPort_.isScrolledEnd) {
Robert Ginda36c5aa62012-10-15 11:17:47 -07001937 this.scheduleScrollDown_();
Mike Frysingerbdb34802020-04-07 03:47:32 -04001938 }
rginda8ba33642011-12-14 12:31:31 -08001939 }
1940
Mike Frysingerbdb34802020-04-07 03:47:32 -04001941 if (cursorRow >= this.screen_.rowsArray.length) {
rginda8ba33642011-12-14 12:31:31 -08001942 cursorRow = this.screen_.rowsArray.length - 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001943 }
rginda8ba33642011-12-14 12:31:31 -08001944
rginda87b86462011-12-14 13:48:03 -08001945 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001946};
1947
1948/**
1949 * Relocate rows from one part of the addressable screen to another.
1950 *
1951 * This is used to recycle rows during VT scrolls (those which are driven
1952 * by VT commands, rather than by the user manipulating the scrollbar.)
1953 *
1954 * In this case, the blank lines scrolled into the scroll region are made of
1955 * the nodes we scrolled off. These have their rowIndex properties carefully
1956 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001957 *
1958 * @param {number} fromIndex The start index.
1959 * @param {number} count The number of rows to move.
1960 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001961 */
1962hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1963 var ary = this.screen_.removeRows(fromIndex, count);
1964 this.screen_.insertRows(toIndex, ary);
1965
1966 var start, end;
1967 if (fromIndex < toIndex) {
1968 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001969 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001970 } else {
1971 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001972 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001973 }
1974
1975 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001976 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001977};
1978
1979/**
1980 * Renumber the rowIndex property of the given range of rows.
1981 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001982 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08001983 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001984 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001985 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05001986 *
1987 * @param {number} start The start index.
1988 * @param {number} end The end index.
Mike Frysingerec4225d2020-04-07 05:00:01 -04001989 * @param {!hterm.Screen=} screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08001990 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04001991hterm.Terminal.prototype.renumberRows_ = function(
1992 start, end, screen = undefined) {
1993 if (!screen) {
1994 screen = this.screen_;
1995 }
Robert Ginda40932892012-12-10 17:26:40 -08001996
rginda8ba33642011-12-14 12:31:31 -08001997 var offset = this.scrollbackRows_.length;
1998 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001999 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08002000 }
2001};
2002
2003/**
2004 * Print a string to the terminal.
2005 *
2006 * This respects the current insert and wraparound modes. It will add new lines
2007 * to the end of the terminal, scrolling off the top into the scrollback buffer
2008 * if necessary.
2009 *
2010 * The string is *not* parsed for escape codes. Use the interpret() method if
2011 * that's what you're after.
2012 *
Mike Frysingerfd449572019-09-23 03:18:14 -04002013 * @param {string} str The string to print.
rginda8ba33642011-12-14 12:31:31 -08002014 */
2015hterm.Terminal.prototype.print = function(str) {
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002016 this.scheduleSyncCursorPosition_();
2017
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002018 // Basic accessibility output for the screen reader.
Raymes Khoury177aec72018-06-26 10:58:53 +10002019 this.accessibilityReader_.announce(str);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002020
rgindaa9abdd82012-08-06 18:05:09 -07002021 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08002022
Ricky Liang48f05cb2013-12-31 23:35:29 +08002023 var strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04002024 // Fun edge case: If the string only contains zero width codepoints (like
2025 // combining characters), we make sure to iterate at least once below.
Mike Frysingerbdb34802020-04-07 03:47:32 -04002026 if (strWidth == 0 && str) {
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04002027 strWidth = 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002028 }
Ricky Liang48f05cb2013-12-31 23:35:29 +08002029
2030 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07002031 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
2032 this.screen_.commitLineOverflow();
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002033 this.newLine(true);
rgindaa09e7332012-08-17 12:49:51 -07002034 }
rgindaa19afe22012-01-25 15:40:22 -08002035
Ricky Liang48f05cb2013-12-31 23:35:29 +08002036 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07002037 var didOverflow = false;
2038 var substr;
rgindaa19afe22012-01-25 15:40:22 -08002039
rgindaa9abdd82012-08-06 18:05:09 -07002040 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
2041 didOverflow = true;
2042 count = this.screenSize.width - this.screen_.cursorPosition.column;
2043 }
rgindaa19afe22012-01-25 15:40:22 -08002044
rgindaa9abdd82012-08-06 18:05:09 -07002045 if (didOverflow && !this.options_.wraparound) {
2046 // If the string overflowed the line but wraparound is off, then the
2047 // last printed character should be the last of the string.
2048 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002049 substr = lib.wc.substr(str, startOffset, count - 1) +
2050 lib.wc.substr(str, strWidth - 1);
2051 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07002052 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08002053 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07002054 }
rgindaa19afe22012-01-25 15:40:22 -08002055
Ricky Liang48f05cb2013-12-31 23:35:29 +08002056 var tokens = hterm.TextAttributes.splitWidecharString(substr);
2057 for (var i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04002058 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
2059 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002060
2061 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04002062 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002063 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04002064 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002065 }
2066 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04002067 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07002068 }
2069
2070 this.screen_.maybeClipCurrentRow();
2071 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08002072 }
rginda8ba33642011-12-14 12:31:31 -08002073
Mike Frysingerbdb34802020-04-07 03:47:32 -04002074 if (this.scrollOnOutput_) {
rginda0f5c0292012-01-13 11:00:13 -08002075 this.scrollPort_.scrollRowToBottom(this.getRowCount());
Mike Frysingerbdb34802020-04-07 03:47:32 -04002076 }
rginda8ba33642011-12-14 12:31:31 -08002077};
2078
2079/**
rginda87b86462011-12-14 13:48:03 -08002080 * Set the VT scroll region.
2081 *
rginda87b86462011-12-14 13:48:03 -08002082 * This also resets the cursor position to the absolute (0, 0) position, since
2083 * that's what xterm appears to do.
2084 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07002085 * Setting the scroll region to the full height of the terminal will clear
2086 * the scroll region. This is *NOT* what most terminals do. We're explicitly
2087 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
2088 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
2089 * continue to work as most users would expect.
2090 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07002091 * @param {?number} scrollTop The zero-based top of the scroll region.
2092 * @param {?number} scrollBottom The zero-based bottom of the scroll region,
rginda87b86462011-12-14 13:48:03 -08002093 * inclusive.
2094 */
2095hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07002096 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08002097 this.vtScrollTop_ = null;
2098 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07002099 } else {
2100 this.vtScrollTop_ = scrollTop;
2101 this.vtScrollBottom_ = scrollBottom;
2102 }
rginda87b86462011-12-14 13:48:03 -08002103};
2104
2105/**
rginda8ba33642011-12-14 12:31:31 -08002106 * Return the top row index according to the VT.
2107 *
2108 * This will return 0 unless the terminal has been told to restrict scrolling
2109 * to some lower row. It is used for some VT cursor positioning and scrolling
2110 * commands.
2111 *
Joel Hockey0f933582019-08-27 18:01:51 -07002112 * @return {number} The topmost row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08002113 */
2114hterm.Terminal.prototype.getVTScrollTop = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002115 if (this.vtScrollTop_ != null) {
rginda8ba33642011-12-14 12:31:31 -08002116 return this.vtScrollTop_;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002117 }
rginda8ba33642011-12-14 12:31:31 -08002118
2119 return 0;
rginda87b86462011-12-14 13:48:03 -08002120};
rginda8ba33642011-12-14 12:31:31 -08002121
2122/**
2123 * Return the bottom row index according to the VT.
2124 *
2125 * This will return the height of the terminal unless the it has been told to
2126 * restrict scrolling to some higher row. It is used for some VT cursor
2127 * positioning and scrolling commands.
2128 *
Joel Hockey0f933582019-08-27 18:01:51 -07002129 * @return {number} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08002130 */
2131hterm.Terminal.prototype.getVTScrollBottom = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002132 if (this.vtScrollBottom_ != null) {
rginda8ba33642011-12-14 12:31:31 -08002133 return this.vtScrollBottom_;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002134 }
rginda8ba33642011-12-14 12:31:31 -08002135
rginda87b86462011-12-14 13:48:03 -08002136 return this.screenSize.height - 1;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04002137};
rginda8ba33642011-12-14 12:31:31 -08002138
2139/**
2140 * Process a '\n' character.
2141 *
2142 * If the cursor is on the final row of the terminal this will append a new
2143 * blank row to the screen and scroll the topmost row into the scrollback
2144 * buffer.
2145 *
2146 * Otherwise, this moves the cursor to column zero of the next row.
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002147 *
2148 * @param {boolean=} dueToOverflow Whether the newline is due to wraparound of
2149 * the terminal.
rginda8ba33642011-12-14 12:31:31 -08002150 */
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002151hterm.Terminal.prototype.newLine = function(dueToOverflow = false) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002152 if (!dueToOverflow) {
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002153 this.accessibilityReader_.newLine();
Mike Frysingerbdb34802020-04-07 03:47:32 -04002154 }
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002155
Robert Ginda9937abc2013-07-25 16:09:23 -07002156 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
2157 this.screen_.rowsArray.length - 1);
2158
2159 if (this.vtScrollBottom_ != null) {
2160 // A VT Scroll region is active, we never append new rows.
2161 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
2162 // We're at the end of the VT Scroll Region, perform a VT scroll.
2163 this.vtScrollUp(1);
2164 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
2165 } else if (cursorAtEndOfScreen) {
2166 // We're at the end of the screen, the only thing to do is put the
2167 // cursor to column 0.
2168 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
2169 } else {
2170 // Anywhere else, advance the cursor row, and reset the column.
2171 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
2172 }
2173 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07002174 // We're at the end of the screen. Append a new row to the terminal,
2175 // shifting the top row into the scrollback.
2176 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08002177 } else {
rginda87b86462011-12-14 13:48:03 -08002178 // Anywhere else in the screen just moves the cursor.
2179 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08002180 }
2181};
2182
2183/**
2184 * Like newLine(), except maintain the cursor column.
2185 */
2186hterm.Terminal.prototype.lineFeed = function() {
2187 var column = this.screen_.cursorPosition.column;
2188 this.newLine();
2189 this.setCursorColumn(column);
2190};
2191
2192/**
rginda87b86462011-12-14 13:48:03 -08002193 * If autoCarriageReturn is set then newLine(), else lineFeed().
2194 */
2195hterm.Terminal.prototype.formFeed = function() {
2196 if (this.options_.autoCarriageReturn) {
2197 this.newLine();
2198 } else {
2199 this.lineFeed();
2200 }
2201};
2202
2203/**
2204 * Move the cursor up one row, possibly inserting a blank line.
2205 *
2206 * The cursor column is not changed.
2207 */
2208hterm.Terminal.prototype.reverseLineFeed = function() {
2209 var scrollTop = this.getVTScrollTop();
2210 var currentRow = this.screen_.cursorPosition.row;
2211
2212 if (currentRow == scrollTop) {
2213 this.insertLines(1);
2214 } else {
2215 this.setAbsoluteCursorRow(currentRow - 1);
2216 }
2217};
2218
2219/**
rginda8ba33642011-12-14 12:31:31 -08002220 * Replace all characters to the left of the current cursor with the space
2221 * character.
2222 *
2223 * TODO(rginda): This should probably *remove* the characters (not just replace
2224 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07002225 * position.
rginda8ba33642011-12-14 12:31:31 -08002226 */
2227hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08002228 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002229 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002230 const count = cursor.column + 1;
Mike Frysinger73e56462019-07-17 00:23:46 -05002231 this.screen_.overwriteString(' '.repeat(count), count);
rginda87b86462011-12-14 13:48:03 -08002232 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002233};
2234
2235/**
David Benjamin684a9b72012-05-01 17:19:58 -04002236 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08002237 *
2238 * The cursor position is unchanged.
2239 *
Robert Gindaf2547f12012-10-25 20:36:21 -07002240 * If the current background color is not the default background color this
2241 * will insert spaces rather than delete. This is unfortunate because the
2242 * trailing space will affect text selection, but it's difficult to come up
2243 * with a way to style empty space that wouldn't trip up the hterm.Screen
2244 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07002245 *
2246 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
2247 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
2248 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05002249 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04002250 * @param {number=} count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08002251 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002252hterm.Terminal.prototype.eraseToRight = function(count = undefined) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002253 if (this.screen_.cursorPosition.overflow) {
Robert Gindacd5637d2013-10-30 14:59:10 -07002254 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002255 }
Robert Gindacd5637d2013-10-30 14:59:10 -07002256
Robert Ginda7fd57082012-09-25 14:41:47 -07002257 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
Mike Frysingerec4225d2020-04-07 05:00:01 -04002258 count = count ? Math.min(count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07002259
2260 if (this.screen_.textAttributes.background ===
2261 this.screen_.textAttributes.DEFAULT_COLOR) {
2262 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08002263 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07002264 this.screen_.cursorPosition.column + count) {
2265 this.screen_.deleteChars(count);
2266 this.clearCursorOverflow();
2267 return;
2268 }
2269 }
2270
rginda87b86462011-12-14 13:48:03 -08002271 var cursor = this.saveCursor();
Mike Frysinger73e56462019-07-17 00:23:46 -05002272 this.screen_.overwriteString(' '.repeat(count), count);
rginda87b86462011-12-14 13:48:03 -08002273 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002274 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002275};
2276
2277/**
2278 * Erase the current line.
2279 *
2280 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002281 */
2282hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08002283 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002284 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08002285 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002286 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002287};
2288
2289/**
David Benjamina08d78f2012-05-05 00:28:49 -04002290 * Erase all characters from the start of the screen to the current cursor
2291 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002292 *
2293 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002294 */
2295hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08002296 var cursor = this.saveCursor();
2297
2298 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002299
David Benjamina08d78f2012-05-05 00:28:49 -04002300 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002301 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002302 this.screen_.clearCursorRow();
2303 }
2304
rginda87b86462011-12-14 13:48:03 -08002305 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002306 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002307};
2308
2309/**
2310 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002311 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002312 *
2313 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002314 */
2315hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08002316 var cursor = this.saveCursor();
2317
2318 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002319
David Benjamina08d78f2012-05-05 00:28:49 -04002320 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08002321 for (var i = cursor.row + 1; i <= bottom; i++) {
2322 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002323 this.screen_.clearCursorRow();
2324 }
2325
rginda87b86462011-12-14 13:48:03 -08002326 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002327 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002328};
2329
2330/**
2331 * Fill the terminal with a given character.
2332 *
2333 * This methods does not respect the VT scroll region.
2334 *
2335 * @param {string} ch The character to use for the fill.
2336 */
2337hterm.Terminal.prototype.fill = function(ch) {
2338 var cursor = this.saveCursor();
2339
2340 this.setAbsoluteCursorPosition(0, 0);
2341 for (var row = 0; row < this.screenSize.height; row++) {
2342 for (var col = 0; col < this.screenSize.width; col++) {
2343 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002344 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002345 }
2346 }
2347
2348 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002349};
2350
2351/**
rginda9ea433c2012-03-16 11:57:00 -07002352 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002353 *
rginda9ea433c2012-03-16 11:57:00 -07002354 * This does not respect the scroll region.
2355 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04002356 * @param {!hterm.Screen=} screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002357 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002358 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002359hterm.Terminal.prototype.clearHome = function(screen = undefined) {
2360 if (!screen) {
2361 screen = this.screen_;
2362 }
rginda9ea433c2012-03-16 11:57:00 -07002363 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002364
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002365 this.accessibilityReader_.clear();
2366
rginda11057d52012-04-25 12:29:56 -07002367 if (bottom == 0) {
2368 // Empty screen, nothing to do.
2369 return;
2370 }
2371
rgindae4d29232012-01-19 10:47:13 -08002372 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002373 screen.setCursorPosition(i, 0);
2374 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002375 }
2376
rginda9ea433c2012-03-16 11:57:00 -07002377 screen.setCursorPosition(0, 0);
2378};
2379
2380/**
2381 * Erase the entire display without changing the cursor position.
2382 *
2383 * The cursor position is unchanged. This does not respect the scroll
2384 * region.
2385 *
Mike Frysingerec4225d2020-04-07 05:00:01 -04002386 * @param {!hterm.Screen=} screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002387 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002388 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04002389hterm.Terminal.prototype.clear = function(screen = undefined) {
2390 if (!screen) {
2391 screen = this.screen_;
2392 }
rginda9ea433c2012-03-16 11:57:00 -07002393 var cursor = screen.cursorPosition.clone();
2394 this.clearHome(screen);
2395 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002396};
2397
2398/**
2399 * VT command to insert lines at the current cursor row.
2400 *
2401 * This respects the current scroll region. Rows pushed off the bottom are
2402 * lost (they won't show up in the scrollback buffer).
2403 *
Joel Hockey0f933582019-08-27 18:01:51 -07002404 * @param {number} count The number of lines to insert.
rginda8ba33642011-12-14 12:31:31 -08002405 */
2406hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07002407 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002408
2409 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002410 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002411
Robert Ginda579186b2012-09-26 11:40:04 -07002412 // The moveCount is the number of rows we need to relocate to make room for
2413 // the new row(s). The count is the distance to move them.
2414 var moveCount = bottom - cursorRow - count + 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002415 if (moveCount) {
Robert Ginda579186b2012-09-26 11:40:04 -07002416 this.moveRows_(cursorRow, moveCount, cursorRow + count);
Mike Frysingerbdb34802020-04-07 03:47:32 -04002417 }
rginda8ba33642011-12-14 12:31:31 -08002418
Robert Ginda579186b2012-09-26 11:40:04 -07002419 for (var i = count - 1; i >= 0; i--) {
2420 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002421 this.screen_.clearCursorRow();
2422 }
rginda8ba33642011-12-14 12:31:31 -08002423};
2424
2425/**
2426 * VT command to delete lines at the current cursor row.
2427 *
2428 * New rows are added to the bottom of scroll region to take their place. New
2429 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002430 *
2431 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002432 */
2433hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08002434 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002435
rginda87b86462011-12-14 13:48:03 -08002436 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08002437 var bottom = this.getVTScrollBottom();
2438
rginda87b86462011-12-14 13:48:03 -08002439 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002440 count = Math.min(count, maxCount);
2441
rginda87b86462011-12-14 13:48:03 -08002442 var moveStart = bottom - count + 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002443 if (count != maxCount) {
rginda8ba33642011-12-14 12:31:31 -08002444 this.moveRows_(top, count, moveStart);
Mike Frysingerbdb34802020-04-07 03:47:32 -04002445 }
rginda8ba33642011-12-14 12:31:31 -08002446
2447 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002448 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002449 this.screen_.clearCursorRow();
2450 }
2451
rginda87b86462011-12-14 13:48:03 -08002452 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002453 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002454};
2455
2456/**
2457 * Inserts the given number of spaces at the current cursor position.
2458 *
rginda87b86462011-12-14 13:48:03 -08002459 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002460 *
2461 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002462 */
2463hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08002464 var cursor = this.saveCursor();
2465
Mike Frysinger73e56462019-07-17 00:23:46 -05002466 const ws = ' '.repeat(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002467 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002468 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002469
2470 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002471 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002472};
2473
2474/**
2475 * Forward-delete the specified number of characters starting at the cursor
2476 * position.
2477 *
Joel Hockey0f933582019-08-27 18:01:51 -07002478 * @param {number} count The number of characters to delete.
rginda8ba33642011-12-14 12:31:31 -08002479 */
2480hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002481 var deleted = this.screen_.deleteChars(count);
2482 if (deleted && !this.screen_.textAttributes.isDefault()) {
2483 var cursor = this.saveCursor();
2484 this.setCursorColumn(this.screenSize.width - deleted);
Mike Frysinger73e56462019-07-17 00:23:46 -05002485 this.screen_.insertString(' '.repeat(deleted));
Robert Ginda7fd57082012-09-25 14:41:47 -07002486 this.restoreCursor(cursor);
2487 }
2488
David Benjamin54e8bf62012-06-01 22:31:40 -04002489 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002490};
2491
2492/**
2493 * Shift rows in the scroll region upwards by a given number of lines.
2494 *
2495 * New rows are inserted at the bottom of the scroll region to fill the
2496 * vacated rows. The new rows not filled out with the current text attributes.
2497 *
2498 * This function does not affect the scrollback rows at all. Rows shifted
2499 * off the top are lost.
2500 *
rginda87b86462011-12-14 13:48:03 -08002501 * The cursor position is not altered.
2502 *
Joel Hockey0f933582019-08-27 18:01:51 -07002503 * @param {number} count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002504 */
2505hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002506 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002507
rginda87b86462011-12-14 13:48:03 -08002508 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002509 this.deleteLines(count);
2510
rginda87b86462011-12-14 13:48:03 -08002511 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002512};
2513
2514/**
2515 * Shift rows below the cursor down by a given number of lines.
2516 *
2517 * This function respects the current scroll region.
2518 *
2519 * New rows are inserted at the top of the scroll region to fill the
2520 * vacated rows. The new rows not filled out with the current text attributes.
2521 *
2522 * This function does not affect the scrollback rows at all. Rows shifted
2523 * off the bottom are lost.
2524 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07002525 * @param {number} count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002526 */
Joel Hockeyd4fca732019-09-20 16:57:03 -07002527hterm.Terminal.prototype.vtScrollDown = function(count) {
rginda87b86462011-12-14 13:48:03 -08002528 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002529
rginda87b86462011-12-14 13:48:03 -08002530 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
Joel Hockeyd4fca732019-09-20 16:57:03 -07002531 this.insertLines(count);
rginda8ba33642011-12-14 12:31:31 -08002532
rginda87b86462011-12-14 13:48:03 -08002533 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002534};
2535
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002536/**
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002537 * Enable accessibility-friendly features that have a performance impact.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002538 *
2539 * This will generate additional DOM nodes in an aria-live region that will
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002540 * cause Assitive Technology to announce the output of the terminal. It also
2541 * enables other features that aid assistive technology. All the features gated
2542 * behind this flag have a performance impact on the terminal which is why they
2543 * are made optional.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002544 *
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002545 * @param {boolean} enabled Whether to enable accessibility-friendly features.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002546 */
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002547hterm.Terminal.prototype.setAccessibilityEnabled = function(enabled) {
Raymes Khoury177aec72018-06-26 10:58:53 +10002548 this.accessibilityReader_.setAccessibilityEnabled(enabled);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002549};
rginda87b86462011-12-14 13:48:03 -08002550
rginda8ba33642011-12-14 12:31:31 -08002551/**
2552 * Set the cursor position.
2553 *
2554 * The cursor row is relative to the scroll region if the terminal has
2555 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2556 *
Joel Hockey0f933582019-08-27 18:01:51 -07002557 * @param {number} row The new zero-based cursor row.
2558 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002559 */
2560hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2561 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002562 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002563 } else {
rginda87b86462011-12-14 13:48:03 -08002564 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002565 }
rginda87b86462011-12-14 13:48:03 -08002566};
rginda8ba33642011-12-14 12:31:31 -08002567
Evan Jones2600d4f2016-12-06 09:29:36 -05002568/**
2569 * Move the cursor relative to its current position.
2570 *
2571 * @param {number} row
2572 * @param {number} column
2573 */
rginda87b86462011-12-14 13:48:03 -08002574hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2575 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002576 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2577 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002578 this.screen_.setCursorPosition(row, column);
2579};
2580
Evan Jones2600d4f2016-12-06 09:29:36 -05002581/**
2582 * Move the cursor to the specified position.
2583 *
2584 * @param {number} row
2585 * @param {number} column
2586 */
rginda87b86462011-12-14 13:48:03 -08002587hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002588 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2589 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002590 this.screen_.setCursorPosition(row, column);
2591};
2592
2593/**
2594 * Set the cursor column.
2595 *
Joel Hockey0f933582019-08-27 18:01:51 -07002596 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002597 */
2598hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002599 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002600};
2601
2602/**
2603 * Return the cursor column.
2604 *
Joel Hockey0f933582019-08-27 18:01:51 -07002605 * @return {number} The zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002606 */
2607hterm.Terminal.prototype.getCursorColumn = function() {
2608 return this.screen_.cursorPosition.column;
2609};
2610
2611/**
2612 * Set the cursor row.
2613 *
2614 * The cursor row is relative to the scroll region if the terminal has
2615 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2616 *
Joel Hockey0f933582019-08-27 18:01:51 -07002617 * @param {number} row The new cursor row.
rginda8ba33642011-12-14 12:31:31 -08002618 */
rginda87b86462011-12-14 13:48:03 -08002619hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2620 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002621};
2622
2623/**
2624 * Return the cursor row.
2625 *
Joel Hockey0f933582019-08-27 18:01:51 -07002626 * @return {number} The zero-based cursor row.
rginda8ba33642011-12-14 12:31:31 -08002627 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002628hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002629 return this.screen_.cursorPosition.row;
2630};
2631
2632/**
2633 * Request that the ScrollPort redraw itself soon.
2634 *
2635 * The redraw will happen asynchronously, soon after the call stack winds down.
2636 * Multiple calls will be coalesced into a single redraw.
2637 */
2638hterm.Terminal.prototype.scheduleRedraw_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002639 if (this.timeouts_.redraw) {
rginda87b86462011-12-14 13:48:03 -08002640 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002641 }
rginda8ba33642011-12-14 12:31:31 -08002642
2643 var self = this;
rginda87b86462011-12-14 13:48:03 -08002644 this.timeouts_.redraw = setTimeout(function() {
2645 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002646 self.scrollPort_.redraw_();
2647 }, 0);
2648};
2649
2650/**
2651 * Request that the ScrollPort be scrolled to the bottom.
2652 *
2653 * The scroll will happen asynchronously, soon after the call stack winds down.
2654 * Multiple calls will be coalesced into a single scroll.
2655 *
2656 * This affects the scrollbar position of the ScrollPort, and has nothing to
2657 * do with the VT scroll commands.
2658 */
2659hterm.Terminal.prototype.scheduleScrollDown_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002660 if (this.timeouts_.scrollDown) {
rginda87b86462011-12-14 13:48:03 -08002661 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002662 }
rginda8ba33642011-12-14 12:31:31 -08002663
2664 var self = this;
2665 this.timeouts_.scrollDown = setTimeout(function() {
2666 delete self.timeouts_.scrollDown;
2667 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2668 }, 10);
2669};
2670
2671/**
2672 * Move the cursor up a specified number of rows.
2673 *
Joel Hockey0f933582019-08-27 18:01:51 -07002674 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002675 */
2676hterm.Terminal.prototype.cursorUp = function(count) {
Joel Hockey0f933582019-08-27 18:01:51 -07002677 this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002678};
2679
2680/**
2681 * Move the cursor down a specified number of rows.
2682 *
Joel Hockey0f933582019-08-27 18:01:51 -07002683 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002684 */
2685hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002686 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002687 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2688 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2689 this.screenSize.height - 1);
2690
rgindacbbd7482012-06-13 15:06:16 -07002691 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002692 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002693 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002694};
2695
2696/**
2697 * Move the cursor left a specified number of columns.
2698 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002699 * If reverse wraparound mode is enabled and the previous row wrapped into
2700 * the current row then we back up through the wraparound as well.
2701 *
Joel Hockey0f933582019-08-27 18:01:51 -07002702 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002703 */
2704hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002705 count = count || 1;
2706
Mike Frysingerbdb34802020-04-07 03:47:32 -04002707 if (count < 1) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002708 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002709 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002710
2711 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002712 if (this.options_.reverseWraparound) {
2713 if (this.screen_.cursorPosition.overflow) {
2714 // If this cursor is in the right margin, consume one count to get it
2715 // back to the last column. This only applies when we're in reverse
2716 // wraparound mode.
2717 count--;
2718 this.clearCursorOverflow();
2719
Mike Frysingerbdb34802020-04-07 03:47:32 -04002720 if (!count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002721 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002722 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002723 }
2724
Robert Gindabfb32622014-07-17 13:20:27 -07002725 var newRow = this.screen_.cursorPosition.row;
2726 var newColumn = currentColumn - count;
2727 if (newColumn < 0) {
2728 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2729 if (newRow < 0) {
2730 // xterm also wraps from row 0 to the last row.
2731 newRow = this.screenSize.height + newRow % this.screenSize.height;
2732 }
2733 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2734 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002735
Robert Gindabfb32622014-07-17 13:20:27 -07002736 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2737
2738 } else {
2739 var newColumn = Math.max(currentColumn - count, 0);
2740 this.setCursorColumn(newColumn);
2741 }
rginda8ba33642011-12-14 12:31:31 -08002742};
2743
2744/**
2745 * Move the cursor right a specified number of columns.
2746 *
Joel Hockey0f933582019-08-27 18:01:51 -07002747 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002748 */
2749hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002750 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002751
Mike Frysingerbdb34802020-04-07 03:47:32 -04002752 if (count < 1) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002753 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002754 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002755
rgindacbbd7482012-06-13 15:06:16 -07002756 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002757 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002758 this.setCursorColumn(column);
2759};
2760
2761/**
2762 * Reverse the foreground and background colors of the terminal.
2763 *
2764 * This only affects text that was drawn with no attributes.
2765 *
2766 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2767 * been drawn with attributes that happen to coincide with the default
2768 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002769 *
2770 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002771 */
2772hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002773 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002774 if (state) {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002775 this.setRgbColorCssVar('foreground-color', this.backgroundColor_);
2776 this.setRgbColorCssVar('background-color', this.foregroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002777 } else {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002778 this.setRgbColorCssVar('foreground-color', this.foregroundColor_);
2779 this.setRgbColorCssVar('background-color', this.backgroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002780 }
2781};
2782
2783/**
rginda87b86462011-12-14 13:48:03 -08002784 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002785 *
2786 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002787 */
2788hterm.Terminal.prototype.ringBell = function() {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002789 this.cursorNode_.style.backgroundColor = 'rgb(var(--hterm-foreground-color))';
rginda87b86462011-12-14 13:48:03 -08002790
2791 var self = this;
2792 setTimeout(function() {
Matheus Fernandes2d733082017-09-11 06:43:01 -04002793 self.restyleCursor_();
rginda6d397402012-01-17 10:58:29 -08002794 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002795
Michael Kelly485ecd12014-06-09 11:41:56 -04002796 // bellSquelchTimeout_ affects both audio and notification bells.
Mike Frysingerbdb34802020-04-07 03:47:32 -04002797 if (this.bellSquelchTimeout_) {
Michael Kelly485ecd12014-06-09 11:41:56 -04002798 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002799 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002800
Robert Ginda92e18102013-03-14 13:56:37 -07002801 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002802 this.bellAudio_.play();
Joel Hockeyd4fca732019-09-20 16:57:03 -07002803 this.bellSequelchTimeout_ = setTimeout(() => {
2804 this.bellSquelchTimeout_ = null;
2805 }, 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002806 } else {
Joel Hockeyd4fca732019-09-20 16:57:03 -07002807 this.bellSquelchTimeout_ = null;
Robert Ginda92e18102013-03-14 13:56:37 -07002808 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002809
2810 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingera5fb83c2017-06-22 14:48:35 -07002811 var n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002812 this.bellNotificationList_.push(n);
2813 // TODO: Should we try to raise the window here?
2814 n.onclick = function() { self.closeBellNotifications_(); };
2815 }
rginda87b86462011-12-14 13:48:03 -08002816};
2817
2818/**
rginda8ba33642011-12-14 12:31:31 -08002819 * Set the origin mode bit.
2820 *
2821 * If origin mode is on, certain VT cursor and scrolling commands measure their
2822 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2823 * to the top of the addressable screen.
2824 *
2825 * Defaults to off.
2826 *
2827 * @param {boolean} state True to set origin mode, false to unset.
2828 */
2829hterm.Terminal.prototype.setOriginMode = function(state) {
2830 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002831 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002832};
2833
2834/**
2835 * Set the insert mode bit.
2836 *
2837 * If insert mode is on, existing text beyond the cursor position will be
2838 * shifted right to make room for new text. Otherwise, new text overwrites
2839 * any existing text.
2840 *
2841 * Defaults to off.
2842 *
2843 * @param {boolean} state True to set insert mode, false to unset.
2844 */
2845hterm.Terminal.prototype.setInsertMode = function(state) {
2846 this.options_.insertMode = state;
2847};
2848
2849/**
rginda87b86462011-12-14 13:48:03 -08002850 * Set the auto carriage return bit.
2851 *
2852 * If auto carriage return is on then a formfeed character is interpreted
2853 * as a newline, otherwise it's the same as a linefeed. The difference boils
2854 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002855 *
2856 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002857 */
2858hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2859 this.options_.autoCarriageReturn = state;
2860};
2861
2862/**
rginda8ba33642011-12-14 12:31:31 -08002863 * Set the wraparound mode bit.
2864 *
2865 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2866 * to the start of the following row. Otherwise, the cursor is clamped to the
2867 * end of the screen and attempts to write past it are ignored.
2868 *
2869 * Defaults to on.
2870 *
2871 * @param {boolean} state True to set wraparound mode, false to unset.
2872 */
2873hterm.Terminal.prototype.setWraparound = function(state) {
2874 this.options_.wraparound = state;
2875};
2876
2877/**
2878 * Set the reverse-wraparound mode bit.
2879 *
2880 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2881 * to the end of the previous row. Otherwise, the cursor is clamped to column
2882 * 0.
2883 *
2884 * Defaults to off.
2885 *
2886 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2887 */
2888hterm.Terminal.prototype.setReverseWraparound = function(state) {
2889 this.options_.reverseWraparound = state;
2890};
2891
2892/**
2893 * Selects between the primary and alternate screens.
2894 *
2895 * If alternate mode is on, the alternate screen is active. Otherwise the
2896 * primary screen is active.
2897 *
2898 * Swapping screens has no effect on the scrollback buffer.
2899 *
2900 * Each screen maintains its own cursor position.
2901 *
2902 * Defaults to off.
2903 *
2904 * @param {boolean} state True to set alternate mode, false to unset.
2905 */
2906hterm.Terminal.prototype.setAlternateMode = function(state) {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002907 if (state == (this.screen_ == this.alternateScreen_)) {
2908 return;
2909 }
2910 const oldOverrides = this.screen_.textAttributes.colorPaletteOverrides;
2911 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002912 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2913
Joel Hockey42dba8f2020-03-26 16:21:11 -07002914 // Swap color overrides.
2915 const newOverrides = this.screen_.textAttributes.colorPaletteOverrides;
2916 oldOverrides.forEach((c, i) => {
2917 if (!newOverrides.hasOwnProperty(i)) {
2918 this.setRgbColorCssVar(`color-${i}`, this.getColorPalette(i));
2919 }
2920 });
2921 newOverrides.forEach((c, i) => this.setRgbColorCssVar(`color-${i}`, c));
2922
rginda35c456b2012-02-09 17:29:05 -08002923 if (this.screen_.rowsArray.length &&
2924 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2925 // If the screen changed sizes while we were away, our rowIndexes may
2926 // be incorrect.
Joel Hockey42dba8f2020-03-26 16:21:11 -07002927 const offset = this.scrollbackRows_.length;
2928 const ary = this.screen_.rowsArray;
2929 for (let i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002930 ary[i].rowIndex = offset + i;
2931 }
2932 }
rginda8ba33642011-12-14 12:31:31 -08002933
rginda35c456b2012-02-09 17:29:05 -08002934 this.realizeWidth_(this.screenSize.width);
2935 this.realizeHeight_(this.screenSize.height);
2936 this.scrollPort_.syncScrollHeight();
2937 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002938
rginda6d397402012-01-17 10:58:29 -08002939 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002940 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002941};
2942
2943/**
2944 * Set the cursor-blink mode bit.
2945 *
2946 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2947 * a visible cursor does not blink.
2948 *
2949 * You should make sure to turn blinking off if you're going to dispose of a
2950 * terminal, otherwise you'll leak a timeout.
2951 *
2952 * Defaults to on.
2953 *
2954 * @param {boolean} state True to set cursor-blink mode, false to unset.
2955 */
2956hterm.Terminal.prototype.setCursorBlink = function(state) {
2957 this.options_.cursorBlink = state;
2958
2959 if (!state && this.timeouts_.cursorBlink) {
2960 clearTimeout(this.timeouts_.cursorBlink);
2961 delete this.timeouts_.cursorBlink;
2962 }
2963
Mike Frysingerbdb34802020-04-07 03:47:32 -04002964 if (this.options_.cursorVisible) {
rginda8ba33642011-12-14 12:31:31 -08002965 this.setCursorVisible(true);
Mike Frysingerbdb34802020-04-07 03:47:32 -04002966 }
rginda8ba33642011-12-14 12:31:31 -08002967};
2968
2969/**
2970 * Set the cursor-visible mode bit.
2971 *
2972 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2973 *
2974 * Defaults to on.
2975 *
2976 * @param {boolean} state True to set cursor-visible mode, false to unset.
2977 */
2978hterm.Terminal.prototype.setCursorVisible = function(state) {
2979 this.options_.cursorVisible = state;
2980
2981 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002982 if (this.timeouts_.cursorBlink) {
2983 clearTimeout(this.timeouts_.cursorBlink);
2984 delete this.timeouts_.cursorBlink;
2985 }
rginda87b86462011-12-14 13:48:03 -08002986 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002987 return;
2988 }
2989
rginda87b86462011-12-14 13:48:03 -08002990 this.syncCursorPosition_();
2991
2992 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002993
2994 if (this.options_.cursorBlink) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002995 if (this.timeouts_.cursorBlink) {
rginda8ba33642011-12-14 12:31:31 -08002996 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002997 }
rginda8ba33642011-12-14 12:31:31 -08002998
Robert Gindaea2183e2014-07-17 09:51:51 -07002999 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08003000 } else {
3001 if (this.timeouts_.cursorBlink) {
3002 clearTimeout(this.timeouts_.cursorBlink);
3003 delete this.timeouts_.cursorBlink;
3004 }
3005 }
3006};
3007
3008/**
Mike Frysinger225c99d2019-10-20 14:02:37 -06003009 * Pause blinking temporarily.
3010 *
3011 * When the cursor moves around, it can be helpful to momentarily pause the
3012 * blinking. This could be when the user is typing in things, or when they're
3013 * moving around with the arrow keys.
3014 */
3015hterm.Terminal.prototype.pauseCursorBlink_ = function() {
3016 if (!this.options_.cursorBlink) {
3017 return;
3018 }
3019
3020 this.cursorBlinkPause_ = true;
3021
3022 // If a timeout is already pending, reset the clock due to the new input.
3023 if (this.timeouts_.cursorBlinkPause) {
3024 clearTimeout(this.timeouts_.cursorBlinkPause);
3025 }
3026 // After 500ms, resume blinking. That seems like a good balance between user
3027 // input timings & responsiveness to resume.
3028 this.timeouts_.cursorBlinkPause = setTimeout(() => {
3029 delete this.timeouts_.cursorBlinkPause;
3030 this.cursorBlinkPause_ = false;
3031 }, 500);
3032};
3033
3034/**
rginda87b86462011-12-14 13:48:03 -08003035 * Synchronizes the visible cursor and document selection with the current
3036 * cursor coordinates.
Raymes Khourye5d48982018-08-02 09:08:32 +10003037 *
3038 * @return {boolean} True if the cursor is onscreen and synced.
rginda8ba33642011-12-14 12:31:31 -08003039 */
3040hterm.Terminal.prototype.syncCursorPosition_ = function() {
3041 var topRowIndex = this.scrollPort_.getTopRowIndex();
3042 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
3043 var cursorRowIndex = this.scrollbackRows_.length +
3044 this.screen_.cursorPosition.row;
3045
Raymes Khoury15697f42018-07-17 11:37:18 +10003046 let forceSyncSelection = false;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003047 if (this.accessibilityReader_.accessibilityEnabled) {
3048 // Report the new position of the cursor for accessibility purposes.
3049 const cursorColumnIndex = this.screen_.cursorPosition.column;
3050 const cursorLineText =
3051 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
Raymes Khoury15697f42018-07-17 11:37:18 +10003052 // This will force the selection to be sync'd to the cursor position if the
3053 // user has pressed a key. Generally we would only sync the cursor position
3054 // when selection is collapsed so that if the user has selected something
3055 // we don't clear the selection by moving the selection. However when a
3056 // screen reader is used, it's intuitive for entering a key to move the
3057 // selection to the cursor.
3058 forceSyncSelection = this.accessibilityReader_.hasUserGesture;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003059 this.accessibilityReader_.afterCursorChange(
3060 cursorLineText, cursorRowIndex, cursorColumnIndex);
3061 }
3062
rginda8ba33642011-12-14 12:31:31 -08003063 if (cursorRowIndex > bottomRowIndex) {
3064 // Cursor is scrolled off screen, move it outside of the visible area.
Mike Frysinger44c32202017-08-05 01:13:09 -04003065 this.setCssVar('cursor-offset-row', '-1');
Raymes Khourye5d48982018-08-02 09:08:32 +10003066 return false;
rginda8ba33642011-12-14 12:31:31 -08003067 }
3068
Robert Gindab837c052014-08-11 11:17:51 -07003069 if (this.options_.cursorVisible &&
3070 this.cursorNode_.style.display == 'none') {
3071 // Re-display the terminal cursor if it was hidden by the mouse cursor.
3072 this.cursorNode_.style.display = '';
3073 }
3074
Mike Frysinger44c32202017-08-05 01:13:09 -04003075 // Position the cursor using CSS variable math. If we do the math in JS,
3076 // the float math will end up being more precise than the CSS which will
3077 // cause the cursor tracking to be off.
3078 this.setCssVar(
3079 'cursor-offset-row',
3080 `${cursorRowIndex - topRowIndex} + ` +
3081 `${this.scrollPort_.visibleRowTopMargin}px`);
3082 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08003083
3084 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04003085 '(' + this.screen_.cursorPosition.column +
3086 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08003087 ')');
3088
3089 // Update the caret for a11y purposes.
3090 var selection = this.document_.getSelection();
Raymes Khoury15697f42018-07-17 11:37:18 +10003091 if (selection && (selection.isCollapsed || forceSyncSelection)) {
rginda87b86462011-12-14 13:48:03 -08003092 this.screen_.syncSelectionCaret(selection);
Raymes Khoury15697f42018-07-17 11:37:18 +10003093 }
Raymes Khourye5d48982018-08-02 09:08:32 +10003094 return true;
rginda8ba33642011-12-14 12:31:31 -08003095};
3096
Robert Gindafb1be6a2013-12-11 11:56:22 -08003097/**
3098 * Adjusts the style of this.cursorNode_ according to the current cursor shape
3099 * and character cell dimensions.
3100 */
Robert Ginda830583c2013-08-07 13:20:46 -07003101hterm.Terminal.prototype.restyleCursor_ = function() {
3102 var shape = this.cursorShape_;
3103
3104 if (this.cursorNode_.getAttribute('focus') == 'false') {
3105 // Always show a block cursor when unfocused.
3106 shape = hterm.Terminal.cursorShape.BLOCK;
3107 }
3108
3109 var style = this.cursorNode_.style;
3110
3111 switch (shape) {
3112 case hterm.Terminal.cursorShape.BEAM:
Robert Ginda830583c2013-08-07 13:20:46 -07003113 style.backgroundColor = 'transparent';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003114 style.borderBottomStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003115 style.borderLeftStyle = 'solid';
3116 break;
3117
3118 case hterm.Terminal.cursorShape.UNDERLINE:
Robert Ginda830583c2013-08-07 13:20:46 -07003119 style.backgroundColor = 'transparent';
3120 style.borderBottomStyle = 'solid';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003121 style.borderLeftStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003122 break;
3123
3124 default:
Mike Frysinger2fd079a2018-09-02 01:46:12 -04003125 style.backgroundColor = 'var(--hterm-cursor-color)';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003126 style.borderBottomStyle = '';
3127 style.borderLeftStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003128 break;
3129 }
3130};
3131
rginda8ba33642011-12-14 12:31:31 -08003132/**
3133 * Synchronizes the visible cursor with the current cursor coordinates.
3134 *
3135 * The sync will happen asynchronously, soon after the call stack winds down.
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003136 * Multiple calls will be coalesced into a single sync. This should be called
3137 * prior to the cursor actually changing position.
rginda8ba33642011-12-14 12:31:31 -08003138 */
3139hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003140 if (this.timeouts_.syncCursor) {
rginda87b86462011-12-14 13:48:03 -08003141 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003142 }
rginda8ba33642011-12-14 12:31:31 -08003143
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003144 if (this.accessibilityReader_.accessibilityEnabled) {
3145 // Report the previous position of the cursor for accessibility purposes.
3146 const cursorRowIndex = this.scrollbackRows_.length +
3147 this.screen_.cursorPosition.row;
3148 const cursorColumnIndex = this.screen_.cursorPosition.column;
3149 const cursorLineText =
3150 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
3151 this.accessibilityReader_.beforeCursorChange(
3152 cursorLineText, cursorRowIndex, cursorColumnIndex);
3153 }
3154
rginda8ba33642011-12-14 12:31:31 -08003155 var self = this;
3156 this.timeouts_.syncCursor = setTimeout(function() {
3157 self.syncCursorPosition_();
3158 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08003159 }, 0);
3160};
3161
rgindacc2996c2012-02-24 14:59:31 -08003162/**
rgindaf522ce02012-04-17 17:49:17 -07003163 * Show or hide the zoom warning.
3164 *
3165 * The zoom warning is a message warning the user that their browser zoom must
3166 * be set to 100% in order for hterm to function properly.
3167 *
3168 * @param {boolean} state True to show the message, false to hide it.
3169 */
3170hterm.Terminal.prototype.showZoomWarning_ = function(state) {
3171 if (!this.zoomWarningNode_) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003172 if (!state) {
rgindaf522ce02012-04-17 17:49:17 -07003173 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003174 }
rgindaf522ce02012-04-17 17:49:17 -07003175
3176 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04003177 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07003178 this.zoomWarningNode_.style.cssText = (
3179 'color: black;' +
3180 'background-color: #ff2222;' +
3181 'font-size: large;' +
3182 'border-radius: 8px;' +
3183 'opacity: 0.75;' +
3184 'padding: 0.2em 0.5em 0.2em 0.5em;' +
3185 'top: 0.5em;' +
3186 'right: 1.2em;' +
3187 'position: absolute;' +
3188 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003189 '-webkit-user-select: none;' +
3190 '-moz-text-size-adjust: none;' +
3191 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05003192
3193 this.zoomWarningNode_.addEventListener('click', function(e) {
3194 this.parentNode.removeChild(this);
3195 });
rgindaf522ce02012-04-17 17:49:17 -07003196 }
3197
Mike Frysingerb7289952019-03-23 16:05:38 -07003198 this.zoomWarningNode_.textContent = lib.i18n.replaceReferences(
Robert Gindab4839c22013-02-28 16:52:10 -08003199 hterm.zoomWarningMessage,
Joel Hockeyd4fca732019-09-20 16:57:03 -07003200 [Math.floor(this.scrollPort_.characterSize.zoomFactor * 100)]);
Robert Gindab4839c22013-02-28 16:52:10 -08003201
rgindaf522ce02012-04-17 17:49:17 -07003202 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
3203
3204 if (state) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003205 if (!this.zoomWarningNode_.parentNode) {
rgindaf522ce02012-04-17 17:49:17 -07003206 this.div_.parentNode.appendChild(this.zoomWarningNode_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003207 }
rgindaf522ce02012-04-17 17:49:17 -07003208 } else if (this.zoomWarningNode_.parentNode) {
3209 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
3210 }
3211};
3212
3213/**
rgindacc2996c2012-02-24 14:59:31 -08003214 * Show the terminal overlay for a given amount of time.
3215 *
3216 * The terminal overlay appears in inverse video in a large font, centered
3217 * over the terminal. You should probably keep the overlay message brief,
3218 * since it's in a large font and you probably aren't going to check the size
3219 * of the terminal first.
3220 *
3221 * @param {string} msg The text (not HTML) message to display in the overlay.
Mike Frysingerec4225d2020-04-07 05:00:01 -04003222 * @param {?number=} timeout The amount of time to wait before fading out
rgindacc2996c2012-02-24 14:59:31 -08003223 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
3224 * stay up forever (or until the next overlay).
3225 */
Mike Frysingerec4225d2020-04-07 05:00:01 -04003226hterm.Terminal.prototype.showOverlay = function(msg, timeout = 1500) {
rgindaf0090c92012-02-10 14:58:52 -08003227 if (!this.overlayNode_) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003228 if (!this.div_) {
rgindaf0090c92012-02-10 14:58:52 -08003229 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003230 }
rgindaf0090c92012-02-10 14:58:52 -08003231
3232 this.overlayNode_ = this.document_.createElement('div');
3233 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08003234 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08003235 'font-size: xx-large;' +
3236 'opacity: 0.75;' +
3237 'padding: 0.2em 0.5em 0.2em 0.5em;' +
3238 'position: absolute;' +
3239 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003240 '-webkit-transition: opacity 180ms ease-in;' +
3241 '-moz-user-select: none;' +
3242 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08003243
3244 this.overlayNode_.addEventListener('mousedown', function(e) {
3245 e.preventDefault();
3246 e.stopPropagation();
3247 }, true);
rgindaf0090c92012-02-10 14:58:52 -08003248 }
3249
rginda9f5222b2012-03-05 11:53:28 -08003250 this.overlayNode_.style.color = this.prefs_.get('background-color');
3251 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
3252 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
3253
rgindaf0090c92012-02-10 14:58:52 -08003254 this.overlayNode_.textContent = msg;
3255 this.overlayNode_.style.opacity = '0.75';
3256
Mike Frysingerbdb34802020-04-07 03:47:32 -04003257 if (!this.overlayNode_.parentNode) {
rgindaf0090c92012-02-10 14:58:52 -08003258 this.div_.appendChild(this.overlayNode_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003259 }
rgindaf0090c92012-02-10 14:58:52 -08003260
Joel Hockeyd4fca732019-09-20 16:57:03 -07003261 var divSize = hterm.getClientSize(lib.notNull(this.div_));
Robert Ginda97769282013-02-01 15:30:30 -08003262 var overlaySize = hterm.getClientSize(this.overlayNode_);
3263
Robert Ginda8a59f762014-07-23 11:29:55 -07003264 this.overlayNode_.style.top =
3265 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08003266 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07003267 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08003268
Mike Frysingerbdb34802020-04-07 03:47:32 -04003269 if (this.overlayTimeout_) {
rgindaf0090c92012-02-10 14:58:52 -08003270 clearTimeout(this.overlayTimeout_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003271 }
rgindaf0090c92012-02-10 14:58:52 -08003272
Raymes Khouryc7a06382018-07-04 10:25:45 +10003273 this.accessibilityReader_.assertiveAnnounce(msg);
3274
Mike Frysingerec4225d2020-04-07 05:00:01 -04003275 if (timeout === null) {
rgindacc2996c2012-02-24 14:59:31 -08003276 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003277 }
rgindacc2996c2012-02-24 14:59:31 -08003278
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003279 this.overlayTimeout_ = setTimeout(() => {
3280 this.overlayNode_.style.opacity = '0';
3281 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
Mike Frysingerec4225d2020-04-07 05:00:01 -04003282 }, timeout);
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003283};
3284
3285/**
3286 * Hide the terminal overlay immediately.
3287 *
3288 * Useful when we show an overlay for an event with an unknown end time.
3289 */
3290hterm.Terminal.prototype.hideOverlay = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003291 if (this.overlayTimeout_) {
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003292 clearTimeout(this.overlayTimeout_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003293 }
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003294 this.overlayTimeout_ = null;
3295
Mike Frysingerbdb34802020-04-07 03:47:32 -04003296 if (this.overlayNode_.parentNode) {
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003297 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003298 }
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003299 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08003300};
3301
rginda4bba5e12012-06-20 16:15:30 -07003302/**
3303 * Paste from the system clipboard to the terminal.
Mike Frysinger23b5b832019-10-01 17:05:29 -04003304 *
Jason Lin17cc89f2020-03-19 10:48:45 +11003305 * Note: In Chrome, this should work unless the user has rejected the permission
3306 * request. In Firefox extension environment, you'll need the "clipboardRead"
3307 * permission. In other environments, this might always fail as the browser
3308 * frequently blocks access for security reasons.
3309 *
3310 * @return {?boolean} If nagivator.clipboard.readText is available, the return
3311 * value is always null. Otherwise, this function uses legacy pasting and
3312 * returns a boolean indicating whether it is successful.
rginda4bba5e12012-06-20 16:15:30 -07003313 */
3314hterm.Terminal.prototype.paste = function() {
Jason Linf129f3c2020-03-23 11:52:08 +11003315 if (!this.alwaysUseLegacyPasting &&
3316 navigator.clipboard && navigator.clipboard.readText) {
Jason Lin17cc89f2020-03-19 10:48:45 +11003317 navigator.clipboard.readText().then((data) => this.onPasteData_(data));
3318 return null;
3319 } else {
3320 // Legacy pasting.
3321 try {
3322 return this.document_.execCommand('paste');
3323 } catch (firefoxException) {
3324 // Ignore this. FF 40 and older would incorrectly throw an exception if
3325 // there was an error instead of returning false.
3326 return false;
3327 }
3328 }
rginda4bba5e12012-06-20 16:15:30 -07003329};
3330
3331/**
3332 * Copy a string to the system clipboard.
3333 *
3334 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05003335 *
3336 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07003337 */
3338hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003339 if (this.prefs_.get('enable-clipboard-notice')) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08003340 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003341 }
Robert Ginda4e4d42c2014-03-04 14:07:23 -08003342
Mike Frysinger96eacae2019-01-02 18:13:56 -05003343 hterm.copySelectionToClipboard(this.document_, str);
rginda4bba5e12012-06-20 16:15:30 -07003344};
3345
Evan Jones2600d4f2016-12-06 09:29:36 -05003346/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003347 * Display an image.
3348 *
Mike Frysinger2558ed52019-01-14 01:03:41 -05003349 * Either URI or buffer or blob fields must be specified.
3350 *
Joel Hockey0f933582019-08-27 18:01:51 -07003351 * @param {{
3352 * name: (string|undefined),
3353 * size: (string|number|undefined),
3354 * preserveAspectRation: (boolean|undefined),
3355 * inline: (boolean|undefined),
3356 * width: (string|number|undefined),
3357 * height: (string|number|undefined),
3358 * align: (string|undefined),
3359 * url: (string|undefined),
3360 * buffer: (!ArrayBuffer|undefined),
3361 * blob: (!Blob|undefined),
3362 * type: (string|undefined),
3363 * }} options The image to display.
3364 * name A human readable string for the image
3365 * size The size (in bytes).
3366 * preserveAspectRatio Whether to preserve aspect.
3367 * inline Whether to display the image inline.
3368 * width The width of the image.
3369 * height The height of the image.
3370 * align Direction to align the image.
3371 * uri The source URI for the image.
3372 * buffer The ArrayBuffer image data.
3373 * blob The Blob image data.
3374 * type The MIME type of the image data.
3375 * @param {function()=} onLoad Callback when loading finishes.
3376 * @param {function(!Event)=} onError Callback when loading fails.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003377 */
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003378hterm.Terminal.prototype.displayImage = function(options, onLoad, onError) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003379 // Make sure we're actually given a resource to display.
Mike Frysinger2558ed52019-01-14 01:03:41 -05003380 if (options.uri === undefined && options.buffer === undefined &&
Mike Frysingerbdb34802020-04-07 03:47:32 -04003381 options.blob === undefined) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003382 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003383 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003384
3385 // Set up the defaults to simplify code below.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003386 if (!options.name) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003387 options.name = '';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003388 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003389
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003390 // See if the mime type is available. If not, guess from the filename.
3391 // We don't list all possible mime types because the browser can usually
3392 // guess it correctly. So list the ones that need a bit more help.
3393 if (!options.type) {
3394 const ary = options.name.split('.');
3395 const ext = ary[ary.length - 1].trim();
3396 switch (ext) {
3397 case 'svg':
3398 case 'svgz':
3399 options.type = 'image/svg+xml';
3400 break;
3401 }
3402 }
3403
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003404 // Has the user approved image display yet?
3405 if (this.allowImagesInline !== true) {
3406 this.newLine();
3407 const row = this.getRowNode(this.scrollbackRows_.length +
3408 this.getCursorRow() - 1);
3409
3410 if (this.allowImagesInline === false) {
3411 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
3412 'Inline Images Disabled');
3413 return;
3414 }
3415
3416 // Show a prompt.
3417 let button;
3418 const span = this.document_.createElement('span');
3419 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
3420 span.style.fontWeight = 'bold';
3421 span.style.borderWidth = '1px';
3422 span.style.borderStyle = 'dashed';
3423 button = this.document_.createElement('span');
3424 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3425 button.style.marginLeft = '1em';
3426 button.style.borderWidth = '1px';
3427 button.style.borderStyle = 'solid';
3428 button.addEventListener('click', () => {
3429 this.prefs_.set('allow-images-inline', false);
3430 });
3431 span.appendChild(button);
3432 button = this.document_.createElement('span');
3433 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3434 'allow this session');
3435 button.style.marginLeft = '1em';
3436 button.style.borderWidth = '1px';
3437 button.style.borderStyle = 'solid';
3438 button.addEventListener('click', () => {
3439 this.allowImagesInline = true;
3440 });
3441 span.appendChild(button);
3442 button = this.document_.createElement('span');
3443 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3444 button.style.marginLeft = '1em';
3445 button.style.borderWidth = '1px';
3446 button.style.borderStyle = 'solid';
3447 button.addEventListener('click', () => {
3448 this.prefs_.set('allow-images-inline', true);
3449 });
3450 span.appendChild(button);
3451
3452 row.appendChild(span);
3453 return;
3454 }
3455
3456 // See if we should show this object directly, or download it.
3457 if (options.inline) {
3458 const io = this.io.push();
3459 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
Joel Hockeyd4fca732019-09-20 16:57:03 -07003460 'Loading $1 ...'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003461
3462 // While we're loading the image, eat all the user's input.
3463 io.onVTKeystroke = io.sendString = () => {};
3464
3465 // Initialize this new image.
Joel Hockeyd4fca732019-09-20 16:57:03 -07003466 const img = this.document_.createElement('img');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003467 if (options.uri !== undefined) {
3468 img.src = options.uri;
3469 } else if (options.buffer !== undefined) {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003470 const blob = new Blob([options.buffer], {type: options.type});
Mike Frysinger2558ed52019-01-14 01:03:41 -05003471 img.src = URL.createObjectURL(blob);
3472 } else {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003473 const blob = new Blob([options.blob], {type: options.type});
Joel Hockeyd4fca732019-09-20 16:57:03 -07003474 img.src = URL.createObjectURL(blob);
Mike Frysinger2558ed52019-01-14 01:03:41 -05003475 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003476 img.title = img.alt = options.name;
3477
3478 // Attach the image to the page to let it load/render. It won't stay here.
3479 // This is needed so it's visible and the DOM can calculate the height. If
3480 // the image is hidden or not in the DOM, the height is always 0.
3481 this.document_.body.appendChild(img);
3482
3483 // Wait for the image to finish loading before we try moving it to the
3484 // right place in the terminal.
3485 img.onload = () => {
3486 // Now that we have the image dimensions, figure out how to show it.
3487 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
3488 img.style.maxWidth = `${this.document_.body.clientWidth}px`;
3489 img.style.maxHeight = `${this.document_.body.clientHeight}px`;
3490
3491 // Parse a width/height specification.
3492 const parseDim = (dim, maxDim, cssVar) => {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003493 if (!dim || dim == 'auto') {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003494 return '';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003495 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003496
3497 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3498 if (ary) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003499 if (ary[2] == '%') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003500 return Math.floor(maxDim * ary[1] / 100) + 'px';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003501 } else if (ary[2] == 'px') {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003502 return dim;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003503 } else {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003504 return `calc(${dim} * var(${cssVar}))`;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003505 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003506 }
3507
3508 return '';
3509 };
3510 img.style.width =
3511 parseDim(options.width, this.document_.body.clientWidth,
3512 '--hterm-charsize-width');
3513 img.style.height =
3514 parseDim(options.height, this.document_.body.clientHeight,
3515 '--hterm-charsize-height');
3516
3517 // Figure out how many rows the image occupies, then add that many.
Mike Frysingera0349392019-09-11 05:38:09 -04003518 // Note: This count will be inaccurate if the font size changes on us.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003519 const padRows = Math.ceil(img.clientHeight /
3520 this.scrollPort_.characterSize.height);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003521 for (let i = 0; i < padRows; ++i) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003522 this.newLine();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003523 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003524
3525 // Update the max height in case the user shrinks the character size.
3526 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3527
3528 // Move the image to the last row. This way when we scroll up, it doesn't
3529 // disappear when the first row gets clipped. It will disappear when we
3530 // scroll down and the last row is clipped ...
3531 this.document_.body.removeChild(img);
3532 // Create a wrapper node so we can do an absolute in a relative position.
3533 // This helps with rounding errors between JS & CSS counts.
3534 const div = this.document_.createElement('div');
3535 div.style.position = 'relative';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003536 div.style.textAlign = options.align || '';
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003537 img.style.position = 'absolute';
3538 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3539 div.appendChild(img);
3540 const row = this.getRowNode(this.scrollbackRows_.length +
3541 this.getCursorRow() - 1);
3542 row.appendChild(div);
3543
Mike Frysinger2558ed52019-01-14 01:03:41 -05003544 // Now that the image has been read, we can revoke the source.
3545 if (options.uri === undefined) {
3546 URL.revokeObjectURL(img.src);
3547 }
3548
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003549 io.hideOverlay();
3550 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003551
Mike Frysingerbdb34802020-04-07 03:47:32 -04003552 if (onLoad) {
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003553 onLoad();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003554 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003555 };
3556
3557 // If we got a malformed image, give up.
3558 img.onerror = (e) => {
3559 this.document_.body.removeChild(img);
3560 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
Mike Frysingere14a8c42018-03-10 00:17:30 -08003561 'Loading $1 failed'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003562 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003563
Mike Frysingerbdb34802020-04-07 03:47:32 -04003564 if (onError) {
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003565 onError(e);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003566 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003567 };
3568 } else {
3569 // We can't use chrome.downloads.download as that requires "downloads"
3570 // permissions, and that works only in extensions, not apps.
3571 const a = this.document_.createElement('a');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003572 if (options.uri !== undefined) {
3573 a.href = options.uri;
3574 } else if (options.buffer !== undefined) {
3575 const blob = new Blob([options.buffer]);
3576 a.href = URL.createObjectURL(blob);
3577 } else {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003578 a.href = URL.createObjectURL(lib.notNull(options.blob));
Mike Frysinger2558ed52019-01-14 01:03:41 -05003579 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003580 a.download = options.name;
3581 this.document_.body.appendChild(a);
3582 a.click();
3583 a.remove();
Mike Frysinger2558ed52019-01-14 01:03:41 -05003584 if (options.uri === undefined) {
3585 URL.revokeObjectURL(a.href);
3586 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003587 }
3588};
3589
3590/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003591 * Returns the selected text, or null if no text is selected.
3592 *
3593 * @return {string|null}
3594 */
rgindaa09e7332012-08-17 12:49:51 -07003595hterm.Terminal.prototype.getSelectionText = function() {
3596 var selection = this.scrollPort_.selection;
3597 selection.sync();
3598
Mike Frysingerbdb34802020-04-07 03:47:32 -04003599 if (selection.isCollapsed) {
rgindaa09e7332012-08-17 12:49:51 -07003600 return null;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003601 }
rgindaa09e7332012-08-17 12:49:51 -07003602
rgindaa09e7332012-08-17 12:49:51 -07003603 // Start offset measures from the beginning of the line.
3604 var startOffset = selection.startOffset;
3605 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003606
Raymes Khoury334625a2018-06-25 10:29:40 +10003607 // If an x-row isn't selected, |node| will be null.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003608 if (!node) {
Raymes Khoury334625a2018-06-25 10:29:40 +10003609 return null;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003610 }
Raymes Khoury334625a2018-06-25 10:29:40 +10003611
Robert Gindafdbb3f22012-09-06 20:23:06 -07003612 if (node.nodeName != 'X-ROW') {
3613 // If the selection doesn't start on an x-row node, then it must be
3614 // somewhere inside the x-row. Add any characters from previous siblings
3615 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003616
3617 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3618 // If node is the text node in a styled span, move up to the span node.
3619 node = node.parentNode;
3620 }
3621
Robert Gindafdbb3f22012-09-06 20:23:06 -07003622 while (node.previousSibling) {
3623 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003624 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003625 }
rgindaa09e7332012-08-17 12:49:51 -07003626 }
3627
3628 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08003629 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
3630 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003631 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003632
Robert Gindafdbb3f22012-09-06 20:23:06 -07003633 if (node.nodeName != 'X-ROW') {
3634 // If the selection doesn't end on an x-row node, then it must be
3635 // somewhere inside the x-row. Add any characters from following siblings
3636 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003637
3638 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3639 // If node is the text node in a styled span, move up to the span node.
3640 node = node.parentNode;
3641 }
3642
Robert Gindafdbb3f22012-09-06 20:23:06 -07003643 while (node.nextSibling) {
3644 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003645 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003646 }
rgindaa09e7332012-08-17 12:49:51 -07003647 }
3648
3649 var rv = this.getRowsText(selection.startRow.rowIndex,
3650 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003651 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003652};
3653
rginda4bba5e12012-06-20 16:15:30 -07003654/**
3655 * Copy the current selection to the system clipboard, then clear it after a
3656 * short delay.
3657 */
3658hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07003659 var text = this.getSelectionText();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003660 if (text != null) {
rgindaa09e7332012-08-17 12:49:51 -07003661 this.copyStringToClipboard(text);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003662 }
rginda4bba5e12012-06-20 16:15:30 -07003663};
3664
Joel Hockey0f933582019-08-27 18:01:51 -07003665/**
3666 * Show overlay with current terminal size.
3667 */
rgindaf0090c92012-02-10 14:58:52 -08003668hterm.Terminal.prototype.overlaySize = function() {
Theodore Duboisdd5f9a72019-09-06 23:28:42 -07003669 if (this.prefs_.get('enable-resize-status')) {
3670 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
3671 }
rgindaf0090c92012-02-10 14:58:52 -08003672};
3673
rginda87b86462011-12-14 13:48:03 -08003674/**
3675 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3676 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003677 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003678 */
3679hterm.Terminal.prototype.onVTKeystroke = function(string) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003680 if (this.scrollOnKeystroke_) {
rginda87b86462011-12-14 13:48:03 -08003681 this.scrollPort_.scrollRowToBottom(this.getRowCount());
Mike Frysingerbdb34802020-04-07 03:47:32 -04003682 }
rginda87b86462011-12-14 13:48:03 -08003683
Mike Frysinger225c99d2019-10-20 14:02:37 -06003684 this.pauseCursorBlink_();
3685
Mike Frysinger79669762018-12-30 20:51:10 -05003686 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08003687};
3688
3689/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003690 * Open the selected url.
3691 */
3692hterm.Terminal.prototype.openSelectedUrl_ = function() {
3693 var str = this.getSelectionText();
3694
3695 // If there is no selection, try and expand wherever they clicked.
3696 if (str == null) {
John Lincae9b732018-03-08 13:56:35 +08003697 this.screen_.expandSelectionForUrl(this.document_.getSelection());
Mike Frysinger70b94692017-01-26 18:57:50 -10003698 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003699
3700 // If clicking in empty space, return.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003701 if (str == null) {
Mike Frysinger498192d2017-06-26 18:23:31 -04003702 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003703 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003704 }
3705
3706 // Make sure URL is valid before opening.
Mike Frysinger968c2c92020-04-07 20:22:23 -04003707 if (str.length > 2048 || str.search(/[\s[\](){}<>"'\\^`]/) >= 0) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003708 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003709 }
Mike Frysinger43472622017-06-26 18:11:07 -04003710
3711 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003712 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003713 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3714 // We have to whitelist a few protocols that lack authorities and thus
3715 // never use the //. Like mailto.
3716 switch (str.split(':', 1)[0]) {
3717 case 'mailto':
3718 break;
3719 default:
3720 str = 'http://' + str;
3721 break;
3722 }
3723 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003724
Mike Frysinger720fa832017-10-23 01:15:52 -04003725 hterm.openUrl(str);
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003726};
Mike Frysinger70b94692017-01-26 18:57:50 -10003727
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003728/**
3729 * Manage the automatic mouse hiding behavior while typing.
3730 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003731 * @param {?boolean=} v Whether to enable automatic hiding.
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003732 */
Mike Frysinger1adc26e2020-04-08 00:17:30 -04003733hterm.Terminal.prototype.setAutomaticMouseHiding = function(v = null) {
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003734 // Since Chrome OS & macOS do this by default everywhere, we don't need to.
3735 // Linux & Windows seem to leave this to specific applications to manage.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003736 if (v === null) {
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003737 v = (hterm.os != 'cros' && hterm.os != 'mac');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003738 }
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003739
3740 this.mouseHideWhileTyping_ = !!v;
3741};
3742
3743/**
3744 * Handler for monitoring user keyboard activity.
3745 *
3746 * This isn't for processing the keystrokes directly, but for updating any
3747 * state that might toggle based on the user using the keyboard at all.
3748 *
Joel Hockey0f933582019-08-27 18:01:51 -07003749 * @param {!KeyboardEvent} e The keyboard event that triggered us.
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003750 */
3751hterm.Terminal.prototype.onKeyboardActivity_ = function(e) {
3752 // When the user starts typing, hide the mouse cursor.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003753 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003754 this.setCssVar('mouse-cursor-style', 'none');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003755 }
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003756};
Mike Frysinger70b94692017-01-26 18:57:50 -10003757
3758/**
rgindad5613292012-06-19 15:40:37 -07003759 * Add the terminalRow and terminalColumn properties to mouse events and
3760 * then forward on to onMouse().
3761 *
3762 * The terminalRow and terminalColumn properties contain the (row, column)
3763 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003764 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003765 * @param {!MouseEvent} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003766 */
3767hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003768 if (e.processedByTerminalHandler_) {
3769 // We register our event handlers on the document, as well as the cursor
3770 // and the scroll blocker. Mouse events that occur on the cursor or
3771 // scroll blocker will also appear on the document, but we don't want to
3772 // process them twice.
3773 //
3774 // We can't just prevent bubbling because that has other side effects, so
3775 // we decorate the event object with this property instead.
3776 return;
3777 }
3778
Mike Frysinger468966c2018-08-28 13:48:51 -04003779 // Consume navigation events. Button 3 is usually "browser back" and
3780 // button 4 is "browser forward" which we don't want to happen.
3781 if (e.button > 2) {
3782 e.preventDefault();
3783 // We don't return so click events can be passed to the remote below.
3784 }
3785
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003786 var reportMouseEvents = (!this.defeatMouseReports_ &&
3787 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3788
rgindafaa74742012-08-21 13:34:03 -07003789 e.processedByTerminalHandler_ = true;
3790
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003791 // Handle auto hiding of mouse cursor while typing.
3792 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
3793 // Make sure the mouse cursor is visible.
3794 this.syncMouseStyle();
3795 // This debounce isn't perfect, but should work well enough for such a
3796 // simple implementation. If the user moved the mouse, we enabled this
3797 // debounce, and then moved the mouse just before the timeout, we wouldn't
3798 // debounce that later movement.
3799 this.mouseHideDelay_ = setTimeout(() => this.mouseHideDelay_ = null, 1000);
3800 }
3801
Robert Gindaeda48db2014-07-17 09:25:30 -07003802 // One based row/column stored on the mouse event.
Joel Hockeyd4fca732019-09-20 16:57:03 -07003803 e.terminalRow = Math.floor(
3804 (e.clientY - this.scrollPort_.visibleRowTopMargin) /
3805 this.scrollPort_.characterSize.height) + 1;
3806 e.terminalColumn = Math.floor(
3807 e.clientX / this.scrollPort_.characterSize.width) + 1;
Robert Gindaeda48db2014-07-17 09:25:30 -07003808
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003809 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
3810 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07003811 return;
3812 }
3813
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003814 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07003815 // If the cursor is visible and we're not sending mouse events to the
3816 // host app, then we want to hide the terminal cursor when the mouse
3817 // cursor is over top. This keeps the terminal cursor from interfering
3818 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003819 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3820 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3821 this.cursorNode_.style.display = 'none';
3822 } else if (this.cursorNode_.style.display == 'none') {
3823 this.cursorNode_.style.display = '';
3824 }
3825 }
rgindad5613292012-06-19 15:40:37 -07003826
Robert Ginda928cf632014-03-05 15:07:41 -08003827 if (e.type == 'mousedown') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003828 this.contextMenu.hide();
Mike Frysingercc114512017-09-11 21:39:17 -04003829
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003830 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003831 // If VT mouse reporting is disabled, or has been defeated with
3832 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003833 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003834 this.setSelectionEnabled(true);
3835 } else {
3836 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003837 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003838 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003839 this.setSelectionEnabled(false);
3840 e.preventDefault();
3841 }
3842 }
3843
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003844 if (!reportMouseEvents) {
John Lin2aad22e2018-03-16 13:58:11 +08003845 if (e.type == 'dblclick') {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003846 this.screen_.expandSelection(this.document_.getSelection());
Mike Frysingerbdb34802020-04-07 03:47:32 -04003847 if (this.copyOnSelect) {
Mike Frysinger406c76c2019-01-02 17:53:51 -05003848 this.copySelectionToClipboard();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003849 }
rgindad5613292012-06-19 15:40:37 -07003850 }
3851
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003852 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003853 // Debounce this event with the dblclick event. If you try to doubleclick
3854 // a URL to open it, Chrome will fire click then dblclick, but we won't
3855 // have expanded the selection text at the first click event.
3856 clearTimeout(this.timeouts_.openUrl);
3857 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3858 500);
3859 return;
3860 }
3861
Mike Frysinger847577f2017-05-23 23:25:57 -04003862 if (e.type == 'mousedown') {
Mike Frysingercc114512017-09-11 21:39:17 -04003863 if (e.ctrlKey && e.button == 2 /* right button */) {
3864 e.preventDefault();
3865 this.contextMenu.show(e, this);
3866 } else if (e.button == this.mousePasteButton ||
3867 (this.mouseRightClickPaste && e.button == 2 /* right button */)) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003868 if (this.paste() === false) {
Mike Frysinger05a57f02017-08-27 17:48:55 -04003869 console.warn('Could not paste manually due to web restrictions');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003870 }
Mike Frysinger847577f2017-05-23 23:25:57 -04003871 }
3872 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003873
Mike Frysinger2edd3612017-05-24 00:54:39 -04003874 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003875 !this.document_.getSelection().isCollapsed) {
Mike Frysinger406c76c2019-01-02 17:53:51 -05003876 this.copySelectionToClipboard();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003877 }
3878
3879 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3880 this.scrollBlockerNode_.engaged) {
3881 // Disengage the scroll-blocker after one of these events.
3882 this.scrollBlockerNode_.engaged = false;
3883 this.scrollBlockerNode_.style.top = '-99px';
3884 }
3885
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003886 // Emulate arrow key presses via scroll wheel events.
3887 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3888 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003889 if (e.type == 'wheel') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003890 const delta =
3891 this.scrollPort_.scrollWheelDelta(/** @type {!WheelEvent} */ (e));
Mike Frysingerc3030a82017-05-29 14:16:11 -04003892
Mike Frysinger321063c2018-08-29 15:33:14 -04003893 // Helper to turn a wheel event delta into a series of key presses.
3894 const deltaToArrows = (distance, charSize, arrowPos, arrowNeg) => {
3895 if (distance == 0) {
3896 return '';
3897 }
3898
3899 // Convert the scroll distance into a number of rows/cols.
3900 const cells = lib.f.smartFloorDivide(Math.abs(distance), charSize);
3901 const data = '\x1bO' + (distance < 0 ? arrowNeg : arrowPos);
3902 return data.repeat(cells);
3903 };
3904
3905 // The order between up/down and left/right doesn't really matter.
3906 this.io.sendString(
3907 // Up/down arrow keys.
3908 deltaToArrows(delta.y, this.scrollPort_.characterSize.height,
3909 'A', 'B') +
3910 // Left/right arrow keys.
3911 deltaToArrows(delta.x, this.scrollPort_.characterSize.width,
3912 'C', 'D')
3913 );
Mike Frysingerc3030a82017-05-29 14:16:11 -04003914
3915 e.preventDefault();
3916 }
3917 }
Robert Ginda928cf632014-03-05 15:07:41 -08003918 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003919 if (!this.scrollBlockerNode_.engaged) {
3920 if (e.type == 'mousedown') {
3921 // Move the scroll-blocker into place if we want to keep the scrollport
3922 // from scrolling.
3923 this.scrollBlockerNode_.engaged = true;
3924 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3925 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3926 } else if (e.type == 'mousemove') {
3927 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3928 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003929 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003930 e.preventDefault();
3931 }
3932 }
Robert Ginda928cf632014-03-05 15:07:41 -08003933
3934 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003935 }
3936
Robert Ginda928cf632014-03-05 15:07:41 -08003937 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3938 // Restore this on mouseup in case it was temporarily defeated with a
3939 // alt-mousedown. Only do this when the selection is empty so that
3940 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003941 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003942 }
rgindad5613292012-06-19 15:40:37 -07003943};
3944
3945/**
3946 * Clients should override this if they care to know about mouse events.
3947 *
3948 * The event parameter will be a normal DOM mouse click event with additional
3949 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003950 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003951 * @param {!MouseEvent} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003952 */
3953hterm.Terminal.prototype.onMouse = function(e) { };
3954
3955/**
rginda8e92a692012-05-20 19:37:20 -07003956 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003957 *
3958 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003959 */
Rob Spies06533ba2014-04-24 11:20:37 -07003960hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3961 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003962 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003963
Mike Frysingerbdb34802020-04-07 03:47:32 -04003964 if (this.reportFocus) {
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003965 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003966 }
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003967
Mike Frysingerbdb34802020-04-07 03:47:32 -04003968 if (focused === true) {
Michael Kelly485ecd12014-06-09 11:41:56 -04003969 this.closeBellNotifications_();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003970 }
rginda8e92a692012-05-20 19:37:20 -07003971};
3972
3973/**
rginda8ba33642011-12-14 12:31:31 -08003974 * React when the ScrollPort is scrolled.
3975 */
3976hterm.Terminal.prototype.onScroll_ = function() {
3977 this.scheduleSyncCursorPosition_();
3978};
3979
3980/**
rginda9846e2f2012-01-27 13:53:33 -08003981 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003982 *
Joel Hockeye25ce432019-09-25 19:12:28 -07003983 * @param {{text: string}} e The text of the paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08003984 */
3985hterm.Terminal.prototype.onPaste_ = function(e) {
Jason Lin17cc89f2020-03-19 10:48:45 +11003986 this.onPasteData_(e.text);
3987};
3988
3989/**
3990 * Handle pasted data.
3991 *
3992 * @param {string} data The pasted data.
3993 */
3994hterm.Terminal.prototype.onPasteData_ = function(data) {
3995 data = data.replace(/\n/mg, '\r');
Mike Frysingere8c32c82018-03-11 14:57:28 -07003996 if (this.options_.bracketedPaste) {
3997 // We strip out most escape sequences as they can cause issues (like
3998 // inserting an \x1b[201~ midstream). We pass through whitespace
3999 // though: 0x08:\b 0x09:\t 0x0a:\n 0x0d:\r.
4000 // This matches xterm behavior.
Mike Frysingerd5436112020-04-07 20:30:15 -04004001 // eslint-disable-next-line no-control-regex
Mike Frysingere8c32c82018-03-11 14:57:28 -07004002 const filter = (data) => data.replace(/[\x00-\x07\x0b-\x0c\x0e-\x1f]/g, '');
4003 data = '\x1b[200~' + filter(data) + '\x1b[201~';
4004 }
Robert Gindaa063b202014-07-21 11:08:25 -07004005
4006 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08004007};
4008
4009/**
rgindaa09e7332012-08-17 12:49:51 -07004010 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05004011 *
Joel Hockey0f933582019-08-27 18:01:51 -07004012 * @param {!Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07004013 */
4014hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07004015 if (!this.useDefaultWindowCopy) {
4016 e.preventDefault();
4017 setTimeout(this.copySelectionToClipboard.bind(this), 0);
4018 }
rgindaa09e7332012-08-17 12:49:51 -07004019};
4020
4021/**
rginda8ba33642011-12-14 12:31:31 -08004022 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08004023 *
4024 * Note: This function should not directly contain code that alters the internal
4025 * state of the terminal. That kind of code belongs in realizeWidth or
4026 * realizeHeight, so that it can be executed synchronously in the case of a
4027 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08004028 */
4029hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08004030 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07004031 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08004032 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07004033 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08004034
Robert Ginda4e83f3a2012-09-04 15:25:25 -07004035 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08004036 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07004037 // gets removed from the document or during the initial load, and we can't
4038 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07004039 // This can also happen if called before the scrollPort calculates the
4040 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08004041 return;
4042 }
4043
rgindaa8ba17d2012-08-15 14:41:10 -07004044 var isNewSize = (columnCount != this.screenSize.width ||
4045 rowCount != this.screenSize.height);
Theodore Dubois651b0842019-09-07 14:32:09 -07004046 const wasScrolledEnd = this.scrollPort_.isScrolledEnd;
rgindaa8ba17d2012-08-15 14:41:10 -07004047
4048 // We do this even if the size didn't change, just to be sure everything is
4049 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04004050 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07004051 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07004052
Mike Frysingerbdb34802020-04-07 03:47:32 -04004053 if (isNewSize) {
rgindaa8ba17d2012-08-15 14:41:10 -07004054 this.overlaySize();
Mike Frysingerbdb34802020-04-07 03:47:32 -04004055 }
rgindaa8ba17d2012-08-15 14:41:10 -07004056
Robert Gindafb1be6a2013-12-11 11:56:22 -08004057 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07004058 this.scheduleSyncCursorPosition_();
Theodore Dubois651b0842019-09-07 14:32:09 -07004059
4060 if (wasScrolledEnd) {
4061 this.scrollEnd();
4062 }
rginda8ba33642011-12-14 12:31:31 -08004063};
4064
4065/**
4066 * Service the cursor blink timeout.
4067 */
4068hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07004069 if (!this.options_.cursorBlink) {
4070 delete this.timeouts_.cursorBlink;
4071 return;
4072 }
4073
Robert Ginda830583c2013-08-07 13:20:46 -07004074 if (this.cursorNode_.getAttribute('focus') == 'false' ||
Mike Frysinger225c99d2019-10-20 14:02:37 -06004075 this.cursorNode_.style.opacity == '0' ||
4076 this.cursorBlinkPause_) {
rginda87b86462011-12-14 13:48:03 -08004077 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07004078 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
4079 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08004080 } else {
rginda87b86462011-12-14 13:48:03 -08004081 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07004082 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
4083 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08004084 }
4085};
David Reveman8f552492012-03-28 12:18:41 -04004086
4087/**
4088 * Set the scrollbar-visible mode bit.
4089 *
4090 * If scrollbar-visible is on, the vertical scrollbar will be visible.
4091 * Otherwise it will not.
4092 *
4093 * Defaults to on.
4094 *
4095 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
4096 */
4097hterm.Terminal.prototype.setScrollbarVisible = function(state) {
4098 this.scrollPort_.setScrollbarVisible(state);
4099};
Michael Kelly485ecd12014-06-09 11:41:56 -04004100
4101/**
Rob Spies49039e52014-12-17 13:40:04 -08004102 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04004103 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08004104 *
4105 * Defaults to 1.
4106 *
Evan Jones2600d4f2016-12-06 09:29:36 -05004107 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08004108 */
4109hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
4110 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
4111};
4112
4113/**
Michael Kelly485ecd12014-06-09 11:41:56 -04004114 * Close all web notifications created by terminal bells.
4115 */
4116hterm.Terminal.prototype.closeBellNotifications_ = function() {
4117 this.bellNotificationList_.forEach(function(n) {
4118 n.close();
4119 });
4120 this.bellNotificationList_.length = 0;
4121};
Raymes Khourye5d48982018-08-02 09:08:32 +10004122
4123/**
4124 * Syncs the cursor position when the scrollport gains focus.
4125 */
4126hterm.Terminal.prototype.onScrollportFocus_ = function() {
4127 // If the cursor is offscreen we set selection to the last row on the screen.
4128 const topRowIndex = this.scrollPort_.getTopRowIndex();
4129 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
4130 const selection = this.document_.getSelection();
4131 if (!this.syncCursorPosition_() && selection) {
4132 selection.collapse(this.getRowNode(bottomRowIndex));
4133 }
4134};
Joel Hockey3e5aed82020-04-01 18:30:05 -07004135
4136/**
4137 * Clients can override this if they want to provide an options page.
4138 */
4139hterm.Terminal.prototype.onOpenOptionsPage = function() {};
4140
4141
4142/**
4143 * Called when user selects to open the options page.
4144 */
4145hterm.Terminal.prototype.onOpenOptionsPage_ = function() {
4146 this.onOpenOptionsPage();
4147};