blob: d9a13ab4b4582511ff30aa6a03ff89e0df4837ff [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.
Joel Hockey0f933582019-08-27 18:01:51 -0700213 * @param {function()=} opt_callback Optional callback to invoke when the
214 * profile transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800215 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700216hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
217 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800218
Robert Ginda57f03b42012-09-13 11:02:48 -0700219 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800220
Mike Frysingerbdb34802020-04-07 03:47:32 -0400221 if (this.prefs_) {
Robert Ginda57f03b42012-09-13 11:02:48 -0700222 this.prefs_.deactivate();
Mike Frysingerbdb34802020-04-07 03:47:32 -0400223 }
rginda9f5222b2012-03-05 11:53:28 -0800224
Robert Ginda57f03b42012-09-13 11:02:48 -0700225 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
Joel Hockey95a9e272020-03-16 21:19:53 -0700226
227 /**
228 * Clears and reloads key bindings. Used by preferences
229 * 'keybindings' and 'keybindings-os-defaults'.
230 *
231 * @param {*} bindings
232 * @param {*} useOsDefaults
233 */
234 function loadKeyBindings(bindings, useOsDefaults) {
235 terminal.keyboard.bindings.clear();
236
237 if (!bindings) {
238 return;
239 }
240
241 if (!(bindings instanceof Object)) {
242 console.error('Error in keybindings preference: Expected object');
243 return;
244 }
245
246 try {
247 terminal.keyboard.bindings.addBindings(bindings, !!useOsDefaults);
248 } catch (ex) {
249 console.error('Error in keybindings preference: ' + ex);
250 }
251 }
252
Robert Ginda57f03b42012-09-13 11:02:48 -0700253 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800254 'alt-gr-mode': function(v) {
255 if (v == null) {
256 if (navigator.language.toLowerCase() == 'en-us') {
257 v = 'none';
258 } else {
259 v = 'right-alt';
260 }
261 } else if (typeof v == 'string') {
262 v = v.toLowerCase();
263 } else {
264 v = 'none';
265 }
266
Mike Frysingerbdb34802020-04-07 03:47:32 -0400267 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v)) {
Robert Ginda034ffa72015-02-26 14:02:37 -0800268 v = 'none';
Mike Frysingerbdb34802020-04-07 03:47:32 -0400269 }
Robert Ginda034ffa72015-02-26 14:02:37 -0800270
271 terminal.keyboard.altGrMode = v;
272 },
273
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700274 'alt-backspace-is-meta-backspace': function(v) {
275 terminal.keyboard.altBackspaceIsMetaBackspace = v;
276 },
277
Robert Ginda57f03b42012-09-13 11:02:48 -0700278 'alt-is-meta': function(v) {
279 terminal.keyboard.altIsMeta = v;
280 },
281
282 'alt-sends-what': function(v) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400283 if (!/^(escape|8-bit|browser-key)$/.test(v)) {
Robert Ginda57f03b42012-09-13 11:02:48 -0700284 v = 'escape';
Mike Frysingerbdb34802020-04-07 03:47:32 -0400285 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700286
287 terminal.keyboard.altSendsWhat = v;
288 },
289
290 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800291 var ary = v.match(/^lib-resource:(\S+)/);
292 if (ary) {
293 terminal.bellAudio_.setAttribute('src',
294 lib.resource.getDataUrl(ary[1]));
295 } else {
296 terminal.bellAudio_.setAttribute('src', v);
297 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700298 },
299
Michael Kelly485ecd12014-06-09 11:41:56 -0400300 'desktop-notification-bell': function(v) {
301 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700302 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400303 Notification.permission === 'granted';
304 if (!terminal.desktopNotificationBell_) {
305 // Note: We don't call Notification.requestPermission here because
306 // Chrome requires the call be the result of a user action (such as an
307 // onclick handler), and pref listeners are run asynchronously.
308 //
309 // A way of working around this would be to display a dialog in the
310 // terminal with a "click-to-request-permission" button.
311 console.warn('desktop-notification-bell is true but we do not have ' +
312 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400313 }
314 } else {
315 terminal.desktopNotificationBell_ = false;
316 }
317 },
318
Robert Ginda57f03b42012-09-13 11:02:48 -0700319 'background-color': function(v) {
320 terminal.setBackgroundColor(v);
321 },
322
323 'background-image': function(v) {
324 terminal.scrollPort_.setBackgroundImage(v);
325 },
326
327 'background-size': function(v) {
328 terminal.scrollPort_.setBackgroundSize(v);
329 },
330
331 'background-position': function(v) {
332 terminal.scrollPort_.setBackgroundPosition(v);
333 },
334
335 'backspace-sends-backspace': function(v) {
336 terminal.keyboard.backspaceSendsBackspace = v;
337 },
338
Brad Town18654b62015-03-12 00:27:45 -0700339 'character-map-overrides': function(v) {
340 if (!(v == null || v instanceof Object)) {
341 console.warn('Preference character-map-modifications is not an ' +
342 'object: ' + v);
343 return;
344 }
345
Mike Frysinger095d4062017-06-14 00:29:48 -0700346 terminal.vt.characterMaps.reset();
347 terminal.vt.characterMaps.setOverrides(v);
Brad Town18654b62015-03-12 00:27:45 -0700348 },
349
Robert Ginda57f03b42012-09-13 11:02:48 -0700350 'cursor-blink': function(v) {
351 terminal.setCursorBlink(!!v);
352 },
353
Joel Hockey9d10ba12019-05-28 01:25:02 -0700354 'cursor-shape': function(v) {
355 terminal.setCursorShape(v);
356 },
357
Robert Gindaea2183e2014-07-17 09:51:51 -0700358 'cursor-blink-cycle': function(v) {
359 if (v instanceof Array &&
360 typeof v[0] == 'number' &&
361 typeof v[1] == 'number') {
362 terminal.cursorBlinkCycle_ = v;
363 } else if (typeof v == 'number') {
364 terminal.cursorBlinkCycle_ = [v, v];
365 } else {
366 // Fast blink indicates an error.
367 terminal.cursorBlinkCycle_ = [100, 100];
368 }
369 },
370
Robert Ginda57f03b42012-09-13 11:02:48 -0700371 'cursor-color': function(v) {
372 terminal.setCursorColor(v);
373 },
374
375 'color-palette-overrides': function(v) {
376 if (!(v == null || v instanceof Object || v instanceof Array)) {
377 console.warn('Preference color-palette-overrides is not an array or ' +
378 'object: ' + v);
379 return;
rginda9f5222b2012-03-05 11:53:28 -0800380 }
rginda9f5222b2012-03-05 11:53:28 -0800381
Joel Hockey42dba8f2020-03-26 16:21:11 -0700382 // Call terminal.setColorPalette here and below with the new default
383 // value before changing it in lib.colors.colorPalette to ensure that
384 // CSS vars are updated.
385 lib.colors.stockColorPalette.forEach(
386 (c, i) => terminal.setColorPalette(i, c));
Robert Ginda57f03b42012-09-13 11:02:48 -0700387 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700388
Robert Ginda57f03b42012-09-13 11:02:48 -0700389 if (v) {
390 for (var key in v) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700391 var i = parseInt(key, 10);
Robert Ginda57f03b42012-09-13 11:02:48 -0700392 if (isNaN(i) || i < 0 || i > 255) {
393 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
394 continue;
395 }
396
397 if (v[i]) {
398 var rgb = lib.colors.normalizeCSS(v[i]);
Joel Hockey42dba8f2020-03-26 16:21:11 -0700399 if (rgb) {
400 terminal.setColorPalette(i, rgb);
Robert Ginda57f03b42012-09-13 11:02:48 -0700401 lib.colors.colorPalette[i] = rgb;
Joel Hockey42dba8f2020-03-26 16:21:11 -0700402 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700403 }
404 }
rginda30f20f62012-04-05 16:36:19 -0700405 }
rginda30f20f62012-04-05 16:36:19 -0700406
Joel Hockey42dba8f2020-03-26 16:21:11 -0700407 terminal.primaryScreen_.textAttributes.colorPaletteOverrides = [];
408 terminal.alternateScreen_.textAttributes.colorPaletteOverrides = [];
Robert Ginda57f03b42012-09-13 11:02:48 -0700409 },
rginda30f20f62012-04-05 16:36:19 -0700410
Robert Ginda57f03b42012-09-13 11:02:48 -0700411 'copy-on-select': function(v) {
412 terminal.copyOnSelect = !!v;
413 },
rginda9f5222b2012-03-05 11:53:28 -0800414
Rob Spies0bec09b2014-06-06 15:58:09 -0700415 'use-default-window-copy': function(v) {
416 terminal.useDefaultWindowCopy = !!v;
417 },
418
419 'clear-selection-after-copy': function(v) {
420 terminal.clearSelectionAfterCopy = !!v;
421 },
422
Robert Ginda7e5e9522014-03-14 12:23:58 -0700423 'ctrl-plus-minus-zero-zoom': function(v) {
424 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
425 },
426
Robert Gindafb5a3f92014-05-13 14:12:00 -0700427 'ctrl-c-copy': function(v) {
428 terminal.keyboard.ctrlCCopy = v;
429 },
430
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100431 'ctrl-v-paste': function(v) {
432 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700433 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100434 },
435
Cody Coljee-Gray7c6a0392018-10-25 13:18:28 -0700436 'paste-on-drop': function(v) {
437 terminal.scrollPort_.setPasteOnDrop(v);
438 },
439
Masaya Suzuki273aa982014-05-31 07:25:55 +0900440 'east-asian-ambiguous-as-two-column': function(v) {
441 lib.wc.regardCjkAmbiguous = v;
442 },
443
Robert Ginda57f03b42012-09-13 11:02:48 -0700444 'enable-8-bit-control': function(v) {
445 terminal.vt.enable8BitControl = !!v;
446 },
rginda30f20f62012-04-05 16:36:19 -0700447
Robert Ginda57f03b42012-09-13 11:02:48 -0700448 'enable-bold': function(v) {
449 terminal.syncBoldSafeState();
450 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400451
Robert Ginda3e278d72014-03-25 13:18:51 -0700452 'enable-bold-as-bright': function(v) {
453 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
454 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
455 },
456
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400457 'enable-blink': function(v) {
Mike Frysinger261597c2017-12-28 01:14:21 -0500458 terminal.setTextBlink(!!v);
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400459 },
460
Robert Ginda57f03b42012-09-13 11:02:48 -0700461 'enable-clipboard-write': function(v) {
462 terminal.vt.enableClipboardWrite = !!v;
463 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400464
Robert Ginda3755e752013-05-31 13:34:09 -0700465 'enable-dec12': function(v) {
466 terminal.vt.enableDec12 = !!v;
467 },
468
Mike Frysinger38f267d2018-09-07 02:50:59 -0400469 'enable-csi-j-3': function(v) {
470 terminal.vt.enableCsiJ3 = !!v;
471 },
472
Robert Ginda57f03b42012-09-13 11:02:48 -0700473 'font-family': function(v) {
474 terminal.syncFontFamily();
475 },
rginda30f20f62012-04-05 16:36:19 -0700476
Robert Ginda57f03b42012-09-13 11:02:48 -0700477 'font-size': function(v) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700478 v = parseInt(v, 10);
Mike Frysinger47853ac2017-12-14 00:44:10 -0500479 if (v <= 0) {
480 console.error(`Invalid font size: ${v}`);
481 return;
482 }
483
Robert Ginda57f03b42012-09-13 11:02:48 -0700484 terminal.setFontSize(v);
485 },
rginda9875d902012-08-20 16:21:57 -0700486
Robert Ginda57f03b42012-09-13 11:02:48 -0700487 'font-smoothing': function(v) {
488 terminal.syncFontFamily();
489 },
rgindade84e382012-04-20 15:39:31 -0700490
Robert Ginda57f03b42012-09-13 11:02:48 -0700491 'foreground-color': function(v) {
492 terminal.setForegroundColor(v);
493 },
rginda30f20f62012-04-05 16:36:19 -0700494
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400495 'hide-mouse-while-typing': function(v) {
496 terminal.setAutomaticMouseHiding(v);
497 },
498
Robert Ginda57f03b42012-09-13 11:02:48 -0700499 'home-keys-scroll': function(v) {
500 terminal.keyboard.homeKeysScroll = v;
501 },
rginda4bba5e12012-06-20 16:15:30 -0700502
Robert Gindaa8165692015-06-15 14:46:31 -0700503 'keybindings': function(v) {
Joel Hockey95a9e272020-03-16 21:19:53 -0700504 loadKeyBindings(v, terminal.prefs_.get('keybindings-os-defaults'));
505 },
Robert Gindaa8165692015-06-15 14:46:31 -0700506
Joel Hockey95a9e272020-03-16 21:19:53 -0700507 'keybindings-os-defaults': function(v) {
508 loadKeyBindings(terminal.prefs_.get('keybindings'), v);
Robert Gindaa8165692015-06-15 14:46:31 -0700509 },
510
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700511 'media-keys-are-fkeys': function(v) {
512 terminal.keyboard.mediaKeysAreFKeys = v;
513 },
514
Robert Ginda57f03b42012-09-13 11:02:48 -0700515 'meta-sends-escape': function(v) {
516 terminal.keyboard.metaSendsEscape = v;
517 },
rginda30f20f62012-04-05 16:36:19 -0700518
Mike Frysinger847577f2017-05-23 23:25:57 -0400519 'mouse-right-click-paste': function(v) {
520 terminal.mouseRightClickPaste = v;
521 },
522
Robert Ginda57f03b42012-09-13 11:02:48 -0700523 'mouse-paste-button': function(v) {
524 terminal.syncMousePasteButton();
525 },
rgindaa8ba17d2012-08-15 14:41:10 -0700526
Robert Gindae76aa9f2014-03-14 12:29:12 -0700527 'page-keys-scroll': function(v) {
528 terminal.keyboard.pageKeysScroll = v;
529 },
530
Robert Ginda40932892012-12-10 17:26:40 -0800531 'pass-alt-number': function(v) {
532 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700533 // Let Alt+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800534 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500535 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800536 }
537
538 terminal.passAltNumber = v;
539 },
540
541 'pass-ctrl-number': function(v) {
542 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700543 // Let Ctrl+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800544 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500545 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800546 }
547
548 terminal.passCtrlNumber = v;
549 },
550
Joel Hockey0e052042020-02-19 05:37:19 -0800551 'pass-ctrl-n': function(v) {
552 terminal.passCtrlN = v;
553 },
554
555 'pass-ctrl-t': function(v) {
556 terminal.passCtrlT = v;
557 },
558
559 'pass-ctrl-tab': function(v) {
560 terminal.passCtrlTab = v;
561 },
562
563 'pass-ctrl-w': function(v) {
564 terminal.passCtrlW = v;
565 },
566
Robert Ginda40932892012-12-10 17:26:40 -0800567 'pass-meta-number': function(v) {
568 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700569 // Let Meta+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800570 // OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500571 v = (hterm.os == 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800572 }
573
574 terminal.passMetaNumber = v;
575 },
576
Marius Schilder77857b32014-05-14 16:21:26 -0700577 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700578 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700579 },
580
Robert Ginda8cb7d902013-06-20 14:37:18 -0700581 'receive-encoding': function(v) {
582 if (!(/^(utf-8|raw)$/).test(v)) {
583 console.warn('Invalid value for "receive-encoding": ' + v);
584 v = 'utf-8';
585 }
586
587 terminal.vt.characterEncoding = v;
588 },
589
Robert Ginda57f03b42012-09-13 11:02:48 -0700590 'scroll-on-keystroke': function(v) {
591 terminal.scrollOnKeystroke_ = v;
592 },
rginda9f5222b2012-03-05 11:53:28 -0800593
Robert Ginda57f03b42012-09-13 11:02:48 -0700594 'scroll-on-output': function(v) {
595 terminal.scrollOnOutput_ = v;
596 },
rginda30f20f62012-04-05 16:36:19 -0700597
Robert Ginda57f03b42012-09-13 11:02:48 -0700598 'scrollbar-visible': function(v) {
599 terminal.setScrollbarVisible(v);
600 },
rginda9f5222b2012-03-05 11:53:28 -0800601
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400602 'scroll-wheel-may-send-arrow-keys': function(v) {
603 terminal.scrollWheelArrowKeys_ = v;
604 },
605
Rob Spies49039e52014-12-17 13:40:04 -0800606 'scroll-wheel-move-multiplier': function(v) {
607 terminal.setScrollWheelMoveMultipler(v);
608 },
609
Robert Ginda57f03b42012-09-13 11:02:48 -0700610 'shift-insert-paste': function(v) {
611 terminal.keyboard.shiftInsertPaste = v;
612 },
rginda9f5222b2012-03-05 11:53:28 -0800613
Mike Frysingera7768922017-07-28 15:00:12 -0400614 'terminal-encoding': function(v) {
Mike Frysingera1371e12017-08-17 01:37:17 -0400615 terminal.vt.setEncoding(v);
Mike Frysingera7768922017-07-28 15:00:12 -0400616 },
617
Robert Gindae76aa9f2014-03-14 12:29:12 -0700618 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400619 terminal.scrollPort_.setUserCssUrl(v);
620 },
621
622 'user-css-text': function(v) {
623 terminal.scrollPort_.setUserCssText(v);
624 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400625
626 'word-break-match-left': function(v) {
627 terminal.primaryScreen_.wordBreakMatchLeft = v;
628 terminal.alternateScreen_.wordBreakMatchLeft = v;
629 },
630
631 'word-break-match-right': function(v) {
632 terminal.primaryScreen_.wordBreakMatchRight = v;
633 terminal.alternateScreen_.wordBreakMatchRight = v;
634 },
635
636 'word-break-match-middle': function(v) {
637 terminal.primaryScreen_.wordBreakMatchMiddle = v;
638 terminal.alternateScreen_.wordBreakMatchMiddle = v;
639 },
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400640
641 'allow-images-inline': function(v) {
642 terminal.allowImagesInline = v;
643 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700644 });
rginda30f20f62012-04-05 16:36:19 -0700645
Robert Ginda57f03b42012-09-13 11:02:48 -0700646 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800647 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700648
Mike Frysingerbdb34802020-04-07 03:47:32 -0400649 if (opt_callback) {
Robert Ginda57f03b42012-09-13 11:02:48 -0700650 opt_callback();
Mike Frysingerbdb34802020-04-07 03:47:32 -0400651 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700652 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800653};
654
Rob Spies56953412014-04-28 14:09:47 -0700655/**
656 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500657 *
Joel Hockey0f933582019-08-27 18:01:51 -0700658 * @return {!hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700659 */
660hterm.Terminal.prototype.getPrefs = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700661 return lib.notNull(this.prefs_);
Rob Spies56953412014-04-28 14:09:47 -0700662};
663
Robert Gindaa063b202014-07-21 11:08:25 -0700664/**
665 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500666 *
667 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700668 */
669hterm.Terminal.prototype.setBracketedPaste = function(state) {
670 this.options_.bracketedPaste = state;
671};
Rob Spies56953412014-04-28 14:09:47 -0700672
rginda8e92a692012-05-20 19:37:20 -0700673/**
674 * Set the color for the cursor.
675 *
676 * If you want this setting to persist, set it through prefs_, rather than
677 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500678 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500679 * @param {string=} color The color to set. If not defined, we reset to the
680 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700681 */
682hterm.Terminal.prototype.setCursorColor = function(color) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400683 if (color === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700684 color = this.prefs_.getString('cursor-color');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400685 }
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500686
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400687 this.setCssVar('cursor-color', color);
rginda8e92a692012-05-20 19:37:20 -0700688};
689
690/**
691 * Return the current cursor color as a string.
Mike Frysinger23b5b832019-10-01 17:05:29 -0400692 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500693 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700694 */
695hterm.Terminal.prototype.getCursorColor = function() {
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400696 return this.getCssVar('cursor-color');
rginda8e92a692012-05-20 19:37:20 -0700697};
698
699/**
rgindad5613292012-06-19 15:40:37 -0700700 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500701 *
702 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700703 */
704hterm.Terminal.prototype.setSelectionEnabled = function(state) {
705 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700706};
707
708/**
rginda8e92a692012-05-20 19:37:20 -0700709 * Set the background color.
710 *
711 * If you want this setting to persist, set it through prefs_, rather than
712 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500713 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500714 * @param {string=} color The color to set. If not defined, we reset to the
715 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700716 */
717hterm.Terminal.prototype.setBackgroundColor = function(color) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400718 if (color === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700719 color = this.prefs_.getString('background-color');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400720 }
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500721
Joel Hockey42dba8f2020-03-26 16:21:11 -0700722 this.backgroundColor_ = lib.colors.normalizeCSS(color);
723 this.setRgbColorCssVar('background-color', this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700724};
725
rginda9f5222b2012-03-05 11:53:28 -0800726/**
727 * Return the current terminal background color.
728 *
729 * Intended for use by other classes, so we don't have to expose the entire
730 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500731 *
Joel Hockey42dba8f2020-03-26 16:21:11 -0700732 * @return {?string}
rginda9f5222b2012-03-05 11:53:28 -0800733 */
734hterm.Terminal.prototype.getBackgroundColor = function() {
Joel Hockey42dba8f2020-03-26 16:21:11 -0700735 return this.backgroundColor_;
rginda8e92a692012-05-20 19:37:20 -0700736};
737
738/**
739 * Set the foreground color.
740 *
741 * If you want this setting to persist, set it through prefs_, rather than
742 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500743 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500744 * @param {string=} color The color to set. If not defined, we reset to the
745 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700746 */
747hterm.Terminal.prototype.setForegroundColor = function(color) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400748 if (color === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700749 color = this.prefs_.getString('foreground-color');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400750 }
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500751
Joel Hockey42dba8f2020-03-26 16:21:11 -0700752 this.foregroundColor_ = lib.colors.normalizeCSS(color);
753 this.setRgbColorCssVar('foreground-color', this.foregroundColor_);
rginda9f5222b2012-03-05 11:53:28 -0800754};
755
756/**
757 * Return the current terminal foreground color.
758 *
759 * Intended for use by other classes, so we don't have to expose the entire
760 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500761 *
Joel Hockey42dba8f2020-03-26 16:21:11 -0700762 * @return {?string}
rginda9f5222b2012-03-05 11:53:28 -0800763 */
764hterm.Terminal.prototype.getForegroundColor = function() {
Joel Hockey42dba8f2020-03-26 16:21:11 -0700765 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800766};
767
768/**
rginda87b86462011-12-14 13:48:03 -0800769 * Create a new instance of a terminal command and run it with a given
770 * argument string.
771 *
Joel Hockeyd4fca732019-09-20 16:57:03 -0700772 * @param {!Function} commandClass The constructor for a terminal command.
Joel Hockey8081ea62019-08-26 16:52:32 -0700773 * @param {string} commandName The command to run for this terminal.
774 * @param {!Array<string>} args The arguments to pass to the command.
rginda87b86462011-12-14 13:48:03 -0800775 */
Joel Hockey8081ea62019-08-26 16:52:32 -0700776hterm.Terminal.prototype.runCommandClass = function(
777 commandClass, commandName, args) {
rgindaf522ce02012-04-17 17:49:17 -0700778 var environment = this.prefs_.get('environment');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400779 if (typeof environment != 'object' || environment == null) {
rgindaf522ce02012-04-17 17:49:17 -0700780 environment = {};
Mike Frysingerbdb34802020-04-07 03:47:32 -0400781 }
rgindaf522ce02012-04-17 17:49:17 -0700782
rginda87b86462011-12-14 13:48:03 -0800783 var self = this;
784 this.command = new commandClass(
Joel Hockey8081ea62019-08-26 16:52:32 -0700785 {
786 commandName: commandName,
787 args: args,
rginda87b86462011-12-14 13:48:03 -0800788 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700789 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800790 onExit: function(code) {
791 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800792 self.uninstallKeyboard();
Julian Watsondfbf8592019-11-05 18:05:12 +1100793 self.div_.dispatchEvent(new CustomEvent('terminal-closing'));
Mike Frysingerbdb34802020-04-07 03:47:32 -0400794 if (self.prefs_.get('close-on-exit')) {
795 window.close();
796 }
rginda87b86462011-12-14 13:48:03 -0800797 }
798 });
799
rgindafeaf3142012-01-31 15:14:20 -0800800 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800801 this.command.run();
802};
803
804/**
rgindafeaf3142012-01-31 15:14:20 -0800805 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500806 *
807 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800808 */
809hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700810 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800811};
812
813/**
814 * Install the keyboard handler for this terminal.
815 *
816 * This will prevent the browser from seeing any keystrokes sent to the
817 * terminal.
818 */
819hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700820 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400821};
rgindafeaf3142012-01-31 15:14:20 -0800822
823/**
824 * Uninstall the keyboard handler for this terminal.
825 */
826hterm.Terminal.prototype.uninstallKeyboard = function() {
827 this.keyboard.installKeyboard(null);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400828};
rgindafeaf3142012-01-31 15:14:20 -0800829
830/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400831 * Set a CSS variable.
832 *
833 * Normally this is used to set variables in the hterm namespace.
834 *
835 * @param {string} name The variable to set.
Joel Hockeyd4fca732019-09-20 16:57:03 -0700836 * @param {string|number} value The value to assign to the variable.
Joel Hockey0f933582019-08-27 18:01:51 -0700837 * @param {string=} opt_prefix The variable namespace/prefix to use.
Mike Frysingercce97c42017-08-05 01:11:22 -0400838 */
839hterm.Terminal.prototype.setCssVar = function(name, value,
840 opt_prefix='--hterm-') {
841 this.document_.documentElement.style.setProperty(
Joel Hockeyd4fca732019-09-20 16:57:03 -0700842 `${opt_prefix}${name}`, value.toString());
Mike Frysingercce97c42017-08-05 01:11:22 -0400843};
844
845/**
Joel Hockey42dba8f2020-03-26 16:21:11 -0700846 * Sets --hterm-{name} to the cracked rgb components (no alpha) if the provided
847 * input is valid.
848 *
849 * @param {string} name The variable to set.
850 * @param {?string} rgb The rgb value to assign to the variable.
851 */
852hterm.Terminal.prototype.setRgbColorCssVar = function(name, rgb) {
853 const ary = rgb ? lib.colors.crackRGB(rgb) : null;
854 if (ary) {
855 this.setCssVar(name, ary.slice(0, 3).join(','));
856 }
857};
858
859/**
860 * Sets the specified color for the active screen.
861 *
862 * @param {number} i The index into the 256 color palette to set.
863 * @param {?string} rgb The rgb value to assign to the variable.
864 */
865hterm.Terminal.prototype.setColorPalette = function(i, rgb) {
866 if (i >= 0 && i < 256 && rgb != null && rgb != this.getColorPalette[i]) {
867 this.setRgbColorCssVar(`color-${i}`, rgb);
868 this.screen_.textAttributes.colorPaletteOverrides[i] = rgb;
869 }
870};
871
872/**
873 * Returns the current value in the active screen of the specified color.
874 *
875 * @param {number} i Color palette index.
876 * @return {string} rgb color.
877 */
878hterm.Terminal.prototype.getColorPalette = function(i) {
879 return this.screen_.textAttributes.colorPaletteOverrides[i] ||
880 lib.colors.colorPalette[i];
881};
882
883/**
884 * Reset the specified color in the active screen to its default value.
885 *
886 * @param {number} i Color to reset
887 */
888hterm.Terminal.prototype.resetColor = function(i) {
889 this.setColorPalette(i, lib.colors.colorPalette[i]);
890 delete this.screen_.textAttributes.colorPaletteOverrides[i];
891};
892
893/**
894 * Reset the current screen color palette to the default state.
895 */
896hterm.Terminal.prototype.resetColorPalette = function() {
897 this.screen_.textAttributes.colorPaletteOverrides.forEach(
898 (c, i) => this.resetColor(i));
899};
900
901/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500902 * Get a CSS variable.
903 *
904 * Normally this is used to get variables in the hterm namespace.
905 *
906 * @param {string} name The variable to read.
Joel Hockey0f933582019-08-27 18:01:51 -0700907 * @param {string=} opt_prefix The variable namespace/prefix to use.
Mike Frysinger261597c2017-12-28 01:14:21 -0500908 * @return {string} The current setting for this variable.
909 */
910hterm.Terminal.prototype.getCssVar = function(name, opt_prefix='--hterm-') {
911 return this.document_.documentElement.style.getPropertyValue(
912 `${opt_prefix}${name}`);
913};
914
915/**
Jason Linbbbdb752020-03-06 16:26:59 +1100916 * Update CSS character size variables to match the scrollport.
917 */
918hterm.Terminal.prototype.updateCssCharsize_ = function() {
919 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
920 this.setCssVar('charsize-height',
921 this.scrollPort_.characterSize.height + 'px');
922};
923
924/**
rginda35c456b2012-02-09 17:29:05 -0800925 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800926 *
927 * Call setFontSize(0) to reset to the default font size.
928 *
929 * This function does not modify the font-size preference.
930 *
931 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800932 */
933hterm.Terminal.prototype.setFontSize = function(px) {
Mike Frysingerbdb34802020-04-07 03:47:32 -0400934 if (px <= 0) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700935 px = this.prefs_.getNumber('font-size');
Mike Frysingerbdb34802020-04-07 03:47:32 -0400936 }
rginda9f5222b2012-03-05 11:53:28 -0800937
rginda35c456b2012-02-09 17:29:05 -0800938 this.scrollPort_.setFontSize(px);
Jason Linbbbdb752020-03-06 16:26:59 +1100939 this.updateCssCharsize_();
rginda35c456b2012-02-09 17:29:05 -0800940};
941
942/**
943 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500944 *
945 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800946 */
947hterm.Terminal.prototype.getFontSize = function() {
948 return this.scrollPort_.getFontSize();
949};
950
951/**
rginda8e92a692012-05-20 19:37:20 -0700952 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500953 *
954 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700955 */
956hterm.Terminal.prototype.getFontFamily = function() {
957 return this.scrollPort_.getFontFamily();
958};
959
960/**
rginda35c456b2012-02-09 17:29:05 -0800961 * Set the CSS "font-family" for this terminal.
962 */
rginda9f5222b2012-03-05 11:53:28 -0800963hterm.Terminal.prototype.syncFontFamily = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700964 this.scrollPort_.setFontFamily(this.prefs_.getString('font-family'),
965 this.prefs_.getString('font-smoothing'));
Jason Linbbbdb752020-03-06 16:26:59 +1100966 this.updateCssCharsize_();
rginda9f5222b2012-03-05 11:53:28 -0800967 this.syncBoldSafeState();
968};
969
rginda4bba5e12012-06-20 16:15:30 -0700970/**
971 * Set this.mousePasteButton based on the mouse-paste-button pref,
972 * autodetecting if necessary.
973 */
974hterm.Terminal.prototype.syncMousePasteButton = function() {
975 var button = this.prefs_.get('mouse-paste-button');
976 if (typeof button == 'number') {
977 this.mousePasteButton = button;
978 return;
979 }
980
Mike Frysingeree81a002017-12-12 16:14:53 -0500981 if (hterm.os != 'linux') {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400982 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700983 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400984 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700985 }
986};
987
988/**
989 * Enable or disable bold based on the enable-bold pref, autodetecting if
990 * necessary.
991 */
rginda9f5222b2012-03-05 11:53:28 -0800992hterm.Terminal.prototype.syncBoldSafeState = function() {
993 var enableBold = this.prefs_.get('enable-bold');
994 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700995 this.primaryScreen_.textAttributes.enableBold = enableBold;
996 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800997 return;
998 }
999
rgindaf7521392012-02-28 17:20:34 -08001000 var normalSize = this.scrollPort_.measureCharacterSize();
1001 var boldSize = this.scrollPort_.measureCharacterSize('bold');
1002
1003 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -08001004 if (!isBoldSafe) {
1005 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -07001006 'from normal. Font family is: ' +
1007 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -08001008 }
rginda9f5222b2012-03-05 11:53:28 -08001009
Robert Gindaed016262012-10-26 16:27:09 -07001010 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
1011 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -08001012};
1013
1014/**
Mike Frysinger261597c2017-12-28 01:14:21 -05001015 * Control text blinking behavior.
1016 *
1017 * @param {boolean=} state Whether to enable support for blinking text.
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001018 */
Mike Frysinger261597c2017-12-28 01:14:21 -05001019hterm.Terminal.prototype.setTextBlink = function(state) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001020 if (state === undefined) {
Joel Hockeyd4fca732019-09-20 16:57:03 -07001021 state = this.prefs_.getBoolean('enable-blink');
Mike Frysingerbdb34802020-04-07 03:47:32 -04001022 }
Mike Frysinger261597c2017-12-28 01:14:21 -05001023 this.setCssVar('blink-node-duration', state ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001024};
1025
1026/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001027 * Set the mouse cursor style based on the current terminal mode.
1028 */
1029hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -04001030 this.setCssVar('mouse-cursor-style',
1031 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
1032 'var(--hterm-mouse-cursor-text)' :
Mike Frysinger67f58f82018-11-22 13:38:22 -05001033 'var(--hterm-mouse-cursor-default)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001034};
1035
1036/**
rginda87b86462011-12-14 13:48:03 -08001037 * Return a copy of the current cursor position.
1038 *
Joel Hockey0f933582019-08-27 18:01:51 -07001039 * @return {!hterm.RowCol} The RowCol object representing the current position.
rginda87b86462011-12-14 13:48:03 -08001040 */
1041hterm.Terminal.prototype.saveCursor = function() {
1042 return this.screen_.cursorPosition.clone();
1043};
1044
Evan Jones2600d4f2016-12-06 09:29:36 -05001045/**
1046 * Return the current text attributes.
1047 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001048 * @return {!hterm.TextAttributes}
Evan Jones2600d4f2016-12-06 09:29:36 -05001049 */
rgindaa19afe22012-01-25 15:40:22 -08001050hterm.Terminal.prototype.getTextAttributes = function() {
1051 return this.screen_.textAttributes;
1052};
1053
Evan Jones2600d4f2016-12-06 09:29:36 -05001054/**
1055 * Set the text attributes.
1056 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001057 * @param {!hterm.TextAttributes} textAttributes The attributes to set.
Evan Jones2600d4f2016-12-06 09:29:36 -05001058 */
rginda1a09aa02012-06-18 21:11:25 -07001059hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
1060 this.screen_.textAttributes = textAttributes;
1061};
1062
rginda87b86462011-12-14 13:48:03 -08001063/**
rgindaf522ce02012-04-17 17:49:17 -07001064 * Return the current browser zoom factor applied to the terminal.
1065 *
1066 * @return {number} The current browser zoom factor.
1067 */
1068hterm.Terminal.prototype.getZoomFactor = function() {
1069 return this.scrollPort_.characterSize.zoomFactor;
1070};
1071
1072/**
rginda9846e2f2012-01-27 13:53:33 -08001073 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -05001074 *
1075 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -08001076 */
1077hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -08001078 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -08001079};
1080
1081/**
rginda87b86462011-12-14 13:48:03 -08001082 * Restore a previously saved cursor position.
1083 *
Joel Hockey0f933582019-08-27 18:01:51 -07001084 * @param {!hterm.RowCol} cursor The position to restore.
rginda87b86462011-12-14 13:48:03 -08001085 */
1086hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -07001087 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
1088 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -08001089 this.screen_.setCursorPosition(row, column);
1090 if (cursor.column > column ||
1091 cursor.column == column && cursor.overflow) {
1092 this.screen_.cursorPosition.overflow = true;
1093 }
rginda87b86462011-12-14 13:48:03 -08001094};
1095
1096/**
David Benjamin54e8bf62012-06-01 22:31:40 -04001097 * Clear the cursor's overflow flag.
1098 */
1099hterm.Terminal.prototype.clearCursorOverflow = function() {
1100 this.screen_.cursorPosition.overflow = false;
1101};
1102
1103/**
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001104 * Save the current cursor state to the corresponding screens.
1105 *
1106 * See the hterm.Screen.CursorState class for more details.
1107 *
1108 * @param {boolean=} both If true, update both screens, else only update the
1109 * current screen.
1110 */
1111hterm.Terminal.prototype.saveCursorAndState = function(both) {
1112 if (both) {
1113 this.primaryScreen_.saveCursorAndState(this.vt);
1114 this.alternateScreen_.saveCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001115 } else {
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001116 this.screen_.saveCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001117 }
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001118};
1119
1120/**
1121 * Restore the saved cursor state in the corresponding screens.
1122 *
1123 * See the hterm.Screen.CursorState class for more details.
1124 *
1125 * @param {boolean=} both If true, update both screens, else only update the
1126 * current screen.
1127 */
1128hterm.Terminal.prototype.restoreCursorAndState = function(both) {
1129 if (both) {
1130 this.primaryScreen_.restoreCursorAndState(this.vt);
1131 this.alternateScreen_.restoreCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001132 } else {
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001133 this.screen_.restoreCursorAndState(this.vt);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001134 }
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001135};
1136
1137/**
Robert Ginda830583c2013-08-07 13:20:46 -07001138 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001139 *
1140 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -07001141 */
1142hterm.Terminal.prototype.setCursorShape = function(shape) {
1143 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001144 this.restyleCursor_();
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001145};
Robert Ginda830583c2013-08-07 13:20:46 -07001146
1147/**
1148 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001149 *
1150 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -07001151 */
1152hterm.Terminal.prototype.getCursorShape = function() {
1153 return this.cursorShape_;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001154};
Robert Ginda830583c2013-08-07 13:20:46 -07001155
1156/**
rginda87b86462011-12-14 13:48:03 -08001157 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001158 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001159 * @param {?number} columnCount
rginda87b86462011-12-14 13:48:03 -08001160 */
1161hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -08001162 if (columnCount == null) {
1163 this.div_.style.width = '100%';
1164 return;
1165 }
1166
Robert Ginda26806d12014-07-24 13:44:07 -07001167 this.div_.style.width = Math.ceil(
1168 this.scrollPort_.characterSize.width *
1169 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001170 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -08001171 this.scheduleSyncCursorPosition_();
1172};
rginda87b86462011-12-14 13:48:03 -08001173
rgindac9bc5502012-01-18 11:48:44 -08001174/**
rginda35c456b2012-02-09 17:29:05 -08001175 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001176 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001177 * @param {?number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -08001178 */
1179hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -08001180 if (rowCount == null) {
1181 this.div_.style.height = '100%';
1182 return;
1183 }
1184
rginda35c456b2012-02-09 17:29:05 -08001185 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -07001186 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -08001187 this.realizeSize_(this.screenSize.width, rowCount);
1188 this.scheduleSyncCursorPosition_();
1189};
1190
1191/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001192 * Deal with terminal size changes.
1193 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001194 * @param {number} columnCount The number of columns.
1195 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001196 */
1197hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
Mike Frysinger0206e262019-06-13 10:18:19 -04001198 let notify = false;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001199
Mike Frysinger0206e262019-06-13 10:18:19 -04001200 if (columnCount != this.screenSize.width) {
1201 notify = true;
1202 this.realizeWidth_(columnCount);
1203 }
1204
1205 if (rowCount != this.screenSize.height) {
1206 notify = true;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001207 this.realizeHeight_(rowCount);
Mike Frysinger0206e262019-06-13 10:18:19 -04001208 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001209
1210 // Send new terminal size to plugin.
Mike Frysinger0206e262019-06-13 10:18:19 -04001211 if (notify) {
1212 this.io.onTerminalResize_(columnCount, rowCount);
1213 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001214};
1215
1216/**
rgindac9bc5502012-01-18 11:48:44 -08001217 * Deal with terminal width changes.
1218 *
1219 * This function does what needs to be done when the terminal width changes
1220 * out from under us. It happens here rather than in onResize_() because this
1221 * code may need to run synchronously to handle programmatic changes of
1222 * terminal width.
1223 *
1224 * Relying on the browser to send us an async resize event means we may not be
1225 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001226 *
1227 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001228 */
1229hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001230 if (columnCount <= 0) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001231 throw new Error('Attempt to realize bad width: ' + columnCount);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001232 }
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001233
rgindac9bc5502012-01-18 11:48:44 -08001234 var deltaColumns = columnCount - this.screen_.getWidth();
Mike Frysinger0206e262019-06-13 10:18:19 -04001235 if (deltaColumns == 0) {
1236 // No change, so don't bother recalculating things.
1237 return;
1238 }
rgindac9bc5502012-01-18 11:48:44 -08001239
rginda87b86462011-12-14 13:48:03 -08001240 this.screenSize.width = columnCount;
1241 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001242
1243 if (deltaColumns > 0) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001244 if (this.defaultTabStops) {
David Benjamin66e954d2012-05-05 21:08:12 -04001245 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001246 }
rgindac9bc5502012-01-18 11:48:44 -08001247 } else {
1248 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001249 if (this.tabStops_[i] < columnCount) {
rgindac9bc5502012-01-18 11:48:44 -08001250 break;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001251 }
rgindac9bc5502012-01-18 11:48:44 -08001252
1253 this.tabStops_.pop();
1254 }
1255 }
1256
1257 this.screen_.setColumnCount(this.screenSize.width);
1258};
1259
1260/**
1261 * Deal with terminal height changes.
1262 *
1263 * This function does what needs to be done when the terminal height changes
1264 * out from under us. It happens here rather than in onResize_() because this
1265 * code may need to run synchronously to handle programmatic changes of
1266 * terminal height.
1267 *
1268 * Relying on the browser to send us an async resize event means we may not be
1269 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001270 *
1271 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001272 */
1273hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001274 if (rowCount <= 0) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001275 throw new Error('Attempt to realize bad height: ' + rowCount);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001276 }
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001277
rgindac9bc5502012-01-18 11:48:44 -08001278 var deltaRows = rowCount - this.screen_.getHeight();
Mike Frysinger0206e262019-06-13 10:18:19 -04001279 if (deltaRows == 0) {
1280 // No change, so don't bother recalculating things.
1281 return;
1282 }
rgindac9bc5502012-01-18 11:48:44 -08001283
1284 this.screenSize.height = rowCount;
1285
1286 var cursor = this.saveCursor();
1287
1288 if (deltaRows < 0) {
1289 // Screen got smaller.
1290 deltaRows *= -1;
1291 while (deltaRows) {
1292 var lastRow = this.getRowCount() - 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001293 if (lastRow - this.scrollbackRows_.length == cursor.row) {
rgindac9bc5502012-01-18 11:48:44 -08001294 break;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001295 }
rgindac9bc5502012-01-18 11:48:44 -08001296
Mike Frysingerbdb34802020-04-07 03:47:32 -04001297 if (this.getRowText(lastRow)) {
rgindac9bc5502012-01-18 11:48:44 -08001298 break;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001299 }
rgindac9bc5502012-01-18 11:48:44 -08001300
1301 this.screen_.popRow();
1302 deltaRows--;
1303 }
1304
1305 var ary = this.screen_.shiftRows(deltaRows);
1306 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1307
1308 // We just removed rows from the top of the screen, we need to update
1309 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001310 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001311 } else if (deltaRows > 0) {
1312 // Screen got larger.
1313
1314 if (deltaRows <= this.scrollbackRows_.length) {
1315 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1316 var rows = this.scrollbackRows_.splice(
1317 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1318 this.screen_.unshiftRows(rows);
1319 deltaRows -= scrollbackCount;
1320 cursor.row += scrollbackCount;
1321 }
1322
Mike Frysingerbdb34802020-04-07 03:47:32 -04001323 if (deltaRows) {
rgindac9bc5502012-01-18 11:48:44 -08001324 this.appendRows_(deltaRows);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001325 }
rgindac9bc5502012-01-18 11:48:44 -08001326 }
1327
rginda35c456b2012-02-09 17:29:05 -08001328 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001329 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001330};
1331
1332/**
1333 * Scroll the terminal to the top of the scrollback buffer.
1334 */
1335hterm.Terminal.prototype.scrollHome = function() {
1336 this.scrollPort_.scrollRowToTop(0);
1337};
1338
1339/**
1340 * Scroll the terminal to the end.
1341 */
1342hterm.Terminal.prototype.scrollEnd = function() {
1343 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1344};
1345
1346/**
1347 * Scroll the terminal one page up (minus one line) relative to the current
1348 * position.
1349 */
1350hterm.Terminal.prototype.scrollPageUp = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001351 this.scrollPort_.scrollPageUp();
rginda87b86462011-12-14 13:48:03 -08001352};
1353
1354/**
1355 * Scroll the terminal one page down (minus one line) relative to the current
1356 * position.
1357 */
1358hterm.Terminal.prototype.scrollPageDown = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001359 this.scrollPort_.scrollPageDown();
rginda8ba33642011-12-14 12:31:31 -08001360};
1361
rgindac9bc5502012-01-18 11:48:44 -08001362/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001363 * Scroll the terminal one line up relative to the current position.
1364 */
1365hterm.Terminal.prototype.scrollLineUp = function() {
1366 var i = this.scrollPort_.getTopRowIndex();
1367 this.scrollPort_.scrollRowToTop(i - 1);
1368};
1369
1370/**
1371 * Scroll the terminal one line down relative to the current position.
1372 */
1373hterm.Terminal.prototype.scrollLineDown = function() {
1374 var i = this.scrollPort_.getTopRowIndex();
1375 this.scrollPort_.scrollRowToTop(i + 1);
1376};
1377
1378/**
Robert Ginda40932892012-12-10 17:26:40 -08001379 * Clear primary screen, secondary screen, and the scrollback buffer.
1380 */
1381hterm.Terminal.prototype.wipeContents = function() {
Mike Frysinger9c482b82018-09-07 02:49:36 -04001382 this.clearHome(this.primaryScreen_);
1383 this.clearHome(this.alternateScreen_);
1384
1385 this.clearScrollback();
1386};
1387
1388/**
1389 * Clear scrollback buffer.
1390 */
1391hterm.Terminal.prototype.clearScrollback = function() {
1392 // Move to the end of the buffer in case the screen was scrolled back.
1393 // We're going to throw it away which would leave the display invalid.
1394 this.scrollEnd();
1395
Robert Ginda40932892012-12-10 17:26:40 -08001396 this.scrollbackRows_.length = 0;
1397 this.scrollPort_.resetCache();
1398
Mike Frysinger9c482b82018-09-07 02:49:36 -04001399 [this.primaryScreen_, this.alternateScreen_].forEach((screen) => {
1400 const bottom = screen.getHeight();
1401 this.renumberRows_(0, bottom, screen);
1402 });
Robert Ginda40932892012-12-10 17:26:40 -08001403
1404 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001405 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001406};
1407
1408/**
rgindac9bc5502012-01-18 11:48:44 -08001409 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001410 *
1411 * Perform a full reset to the default values listed in
1412 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001413 */
rginda87b86462011-12-14 13:48:03 -08001414hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001415 this.vt.reset();
1416
rgindac9bc5502012-01-18 11:48:44 -08001417 this.clearAllTabStops();
1418 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001419
Joel Hockey42dba8f2020-03-26 16:21:11 -07001420 this.resetColorPalette();
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001421 const resetScreen = (screen) => {
1422 // We want to make sure to reset the attributes before we clear the screen.
1423 // The attributes might be used to initialize default/empty rows.
1424 screen.textAttributes.reset();
Joel Hockey42dba8f2020-03-26 16:21:11 -07001425 screen.textAttributes.colorPaletteOverrides = [];
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001426 this.clearHome(screen);
1427 screen.saveCursorAndState(this.vt);
1428 };
1429 resetScreen(this.primaryScreen_);
1430 resetScreen(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001431
Mike Frysinger84301d02017-11-29 13:28:46 -08001432 // Reset terminal options to their default values.
1433 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001434 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1435
Mike Frysinger84301d02017-11-29 13:28:46 -08001436 this.setVTScrollRegion(null, null);
1437
1438 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001439};
1440
rgindac9bc5502012-01-18 11:48:44 -08001441/**
1442 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001443 *
1444 * Perform a soft reset to the default values listed in
1445 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001446 */
rginda0f5c0292012-01-13 11:00:13 -08001447hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001448 this.vt.reset();
1449
rgindab8bc8932012-04-27 12:45:03 -07001450 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001451 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001452
Brad Townb62dfdc2015-03-16 19:07:15 -07001453 // We show the cursor on soft reset but do not alter the blink state.
1454 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1455
Joel Hockey42dba8f2020-03-26 16:21:11 -07001456 this.resetColorPalette();
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001457 const resetScreen = (screen) => {
1458 // Xterm also resets the color palette on soft reset, even though it doesn't
1459 // seem to be documented anywhere.
1460 screen.textAttributes.reset();
Joel Hockey42dba8f2020-03-26 16:21:11 -07001461 screen.textAttributes.colorPaletteOverrides = [];
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001462 screen.saveCursorAndState(this.vt);
1463 };
1464 resetScreen(this.primaryScreen_);
1465 resetScreen(this.alternateScreen_);
rgindaf522ce02012-04-17 17:49:17 -07001466
rgindab8bc8932012-04-27 12:45:03 -07001467 // The xterm man page explicitly says this will happen on soft reset.
1468 this.setVTScrollRegion(null, null);
1469
1470 // Xterm also shows the cursor on soft reset, but does not alter the blink
1471 // state.
rgindaa19afe22012-01-25 15:40:22 -08001472 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001473};
1474
rgindac9bc5502012-01-18 11:48:44 -08001475/**
1476 * Move the cursor forward to the next tab stop, or to the last column
1477 * if no more tab stops are set.
1478 */
1479hterm.Terminal.prototype.forwardTabStop = function() {
1480 var column = this.screen_.cursorPosition.column;
1481
1482 for (var i = 0; i < this.tabStops_.length; i++) {
1483 if (this.tabStops_[i] > column) {
1484 this.setCursorColumn(this.tabStops_[i]);
1485 return;
1486 }
1487 }
1488
David Benjamin66e954d2012-05-05 21:08:12 -04001489 // xterm does not clear the overflow flag on HT or CHT.
1490 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001491 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001492 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001493};
1494
rgindac9bc5502012-01-18 11:48:44 -08001495/**
1496 * Move the cursor backward to the previous tab stop, or to the first column
1497 * if no previous tab stops are set.
1498 */
1499hterm.Terminal.prototype.backwardTabStop = function() {
1500 var column = this.screen_.cursorPosition.column;
1501
1502 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1503 if (this.tabStops_[i] < column) {
1504 this.setCursorColumn(this.tabStops_[i]);
1505 return;
1506 }
1507 }
1508
1509 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001510};
1511
rgindac9bc5502012-01-18 11:48:44 -08001512/**
1513 * Set a tab stop at the given column.
1514 *
Joel Hockey0f933582019-08-27 18:01:51 -07001515 * @param {number} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001516 */
1517hterm.Terminal.prototype.setTabStop = function(column) {
1518 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04001519 if (this.tabStops_[i] == column) {
rgindac9bc5502012-01-18 11:48:44 -08001520 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001521 }
rgindac9bc5502012-01-18 11:48:44 -08001522
1523 if (this.tabStops_[i] < column) {
1524 this.tabStops_.splice(i + 1, 0, column);
1525 return;
1526 }
1527 }
1528
1529 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001530};
1531
rgindac9bc5502012-01-18 11:48:44 -08001532/**
1533 * Clear the tab stop at the current cursor position.
1534 *
1535 * No effect if there is no tab stop at the current cursor position.
1536 */
1537hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1538 var column = this.screen_.cursorPosition.column;
1539
1540 var i = this.tabStops_.indexOf(column);
Mike Frysingerbdb34802020-04-07 03:47:32 -04001541 if (i == -1) {
rgindac9bc5502012-01-18 11:48:44 -08001542 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04001543 }
rgindac9bc5502012-01-18 11:48:44 -08001544
1545 this.tabStops_.splice(i, 1);
1546};
1547
1548/**
1549 * Clear all tab stops.
1550 */
1551hterm.Terminal.prototype.clearAllTabStops = function() {
1552 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001553 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001554};
1555
1556/**
1557 * Set up the default tab stops, starting from a given column.
1558 *
1559 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001560 * from the specified column, or 0 if no column is provided. It also flags
1561 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001562 *
1563 * This does not clear the existing tab stops first, use clearAllTabStops
1564 * for that.
1565 *
Joel Hockey0f933582019-08-27 18:01:51 -07001566 * @param {number=} opt_start Optional starting zero based starting column,
1567 * useful for filling out missing tab stops when the terminal is resized.
rgindac9bc5502012-01-18 11:48:44 -08001568 */
1569hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1570 var start = opt_start || 0;
1571 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.
Joel Hockey0f933582019-08-27 18:01:51 -07001989 * @param {!hterm.Screen=} opt_screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08001990 */
Robert Ginda40932892012-12-10 17:26:40 -08001991hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1992 var screen = opt_screen || this.screen_;
1993
rginda8ba33642011-12-14 12:31:31 -08001994 var offset = this.scrollbackRows_.length;
1995 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001996 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001997 }
1998};
1999
2000/**
2001 * Print a string to the terminal.
2002 *
2003 * This respects the current insert and wraparound modes. It will add new lines
2004 * to the end of the terminal, scrolling off the top into the scrollback buffer
2005 * if necessary.
2006 *
2007 * The string is *not* parsed for escape codes. Use the interpret() method if
2008 * that's what you're after.
2009 *
Mike Frysingerfd449572019-09-23 03:18:14 -04002010 * @param {string} str The string to print.
rginda8ba33642011-12-14 12:31:31 -08002011 */
2012hterm.Terminal.prototype.print = function(str) {
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002013 this.scheduleSyncCursorPosition_();
2014
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002015 // Basic accessibility output for the screen reader.
Raymes Khoury177aec72018-06-26 10:58:53 +10002016 this.accessibilityReader_.announce(str);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002017
rgindaa9abdd82012-08-06 18:05:09 -07002018 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08002019
Ricky Liang48f05cb2013-12-31 23:35:29 +08002020 var strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04002021 // Fun edge case: If the string only contains zero width codepoints (like
2022 // combining characters), we make sure to iterate at least once below.
Mike Frysingerbdb34802020-04-07 03:47:32 -04002023 if (strWidth == 0 && str) {
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04002024 strWidth = 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002025 }
Ricky Liang48f05cb2013-12-31 23:35:29 +08002026
2027 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07002028 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
2029 this.screen_.commitLineOverflow();
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002030 this.newLine(true);
rgindaa09e7332012-08-17 12:49:51 -07002031 }
rgindaa19afe22012-01-25 15:40:22 -08002032
Ricky Liang48f05cb2013-12-31 23:35:29 +08002033 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07002034 var didOverflow = false;
2035 var substr;
rgindaa19afe22012-01-25 15:40:22 -08002036
rgindaa9abdd82012-08-06 18:05:09 -07002037 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
2038 didOverflow = true;
2039 count = this.screenSize.width - this.screen_.cursorPosition.column;
2040 }
rgindaa19afe22012-01-25 15:40:22 -08002041
rgindaa9abdd82012-08-06 18:05:09 -07002042 if (didOverflow && !this.options_.wraparound) {
2043 // If the string overflowed the line but wraparound is off, then the
2044 // last printed character should be the last of the string.
2045 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002046 substr = lib.wc.substr(str, startOffset, count - 1) +
2047 lib.wc.substr(str, strWidth - 1);
2048 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07002049 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08002050 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07002051 }
rgindaa19afe22012-01-25 15:40:22 -08002052
Ricky Liang48f05cb2013-12-31 23:35:29 +08002053 var tokens = hterm.TextAttributes.splitWidecharString(substr);
2054 for (var i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04002055 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
2056 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002057
2058 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04002059 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002060 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04002061 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002062 }
2063 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04002064 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07002065 }
2066
2067 this.screen_.maybeClipCurrentRow();
2068 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08002069 }
rginda8ba33642011-12-14 12:31:31 -08002070
Mike Frysingerbdb34802020-04-07 03:47:32 -04002071 if (this.scrollOnOutput_) {
rginda0f5c0292012-01-13 11:00:13 -08002072 this.scrollPort_.scrollRowToBottom(this.getRowCount());
Mike Frysingerbdb34802020-04-07 03:47:32 -04002073 }
rginda8ba33642011-12-14 12:31:31 -08002074};
2075
2076/**
rginda87b86462011-12-14 13:48:03 -08002077 * Set the VT scroll region.
2078 *
rginda87b86462011-12-14 13:48:03 -08002079 * This also resets the cursor position to the absolute (0, 0) position, since
2080 * that's what xterm appears to do.
2081 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07002082 * Setting the scroll region to the full height of the terminal will clear
2083 * the scroll region. This is *NOT* what most terminals do. We're explicitly
2084 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
2085 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
2086 * continue to work as most users would expect.
2087 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07002088 * @param {?number} scrollTop The zero-based top of the scroll region.
2089 * @param {?number} scrollBottom The zero-based bottom of the scroll region,
rginda87b86462011-12-14 13:48:03 -08002090 * inclusive.
2091 */
2092hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07002093 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08002094 this.vtScrollTop_ = null;
2095 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07002096 } else {
2097 this.vtScrollTop_ = scrollTop;
2098 this.vtScrollBottom_ = scrollBottom;
2099 }
rginda87b86462011-12-14 13:48:03 -08002100};
2101
2102/**
rginda8ba33642011-12-14 12:31:31 -08002103 * Return the top row index according to the VT.
2104 *
2105 * This will return 0 unless the terminal has been told to restrict scrolling
2106 * to some lower row. It is used for some VT cursor positioning and scrolling
2107 * commands.
2108 *
Joel Hockey0f933582019-08-27 18:01:51 -07002109 * @return {number} The topmost row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08002110 */
2111hterm.Terminal.prototype.getVTScrollTop = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002112 if (this.vtScrollTop_ != null) {
rginda8ba33642011-12-14 12:31:31 -08002113 return this.vtScrollTop_;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002114 }
rginda8ba33642011-12-14 12:31:31 -08002115
2116 return 0;
rginda87b86462011-12-14 13:48:03 -08002117};
rginda8ba33642011-12-14 12:31:31 -08002118
2119/**
2120 * Return the bottom row index according to the VT.
2121 *
2122 * This will return the height of the terminal unless the it has been told to
2123 * restrict scrolling to some higher row. It is used for some VT cursor
2124 * positioning and scrolling commands.
2125 *
Joel Hockey0f933582019-08-27 18:01:51 -07002126 * @return {number} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08002127 */
2128hterm.Terminal.prototype.getVTScrollBottom = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002129 if (this.vtScrollBottom_ != null) {
rginda8ba33642011-12-14 12:31:31 -08002130 return this.vtScrollBottom_;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002131 }
rginda8ba33642011-12-14 12:31:31 -08002132
rginda87b86462011-12-14 13:48:03 -08002133 return this.screenSize.height - 1;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04002134};
rginda8ba33642011-12-14 12:31:31 -08002135
2136/**
2137 * Process a '\n' character.
2138 *
2139 * If the cursor is on the final row of the terminal this will append a new
2140 * blank row to the screen and scroll the topmost row into the scrollback
2141 * buffer.
2142 *
2143 * Otherwise, this moves the cursor to column zero of the next row.
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002144 *
2145 * @param {boolean=} dueToOverflow Whether the newline is due to wraparound of
2146 * the terminal.
rginda8ba33642011-12-14 12:31:31 -08002147 */
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002148hterm.Terminal.prototype.newLine = function(dueToOverflow = false) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002149 if (!dueToOverflow) {
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002150 this.accessibilityReader_.newLine();
Mike Frysingerbdb34802020-04-07 03:47:32 -04002151 }
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002152
Robert Ginda9937abc2013-07-25 16:09:23 -07002153 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
2154 this.screen_.rowsArray.length - 1);
2155
2156 if (this.vtScrollBottom_ != null) {
2157 // A VT Scroll region is active, we never append new rows.
2158 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
2159 // We're at the end of the VT Scroll Region, perform a VT scroll.
2160 this.vtScrollUp(1);
2161 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
2162 } else if (cursorAtEndOfScreen) {
2163 // We're at the end of the screen, the only thing to do is put the
2164 // cursor to column 0.
2165 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
2166 } else {
2167 // Anywhere else, advance the cursor row, and reset the column.
2168 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
2169 }
2170 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07002171 // We're at the end of the screen. Append a new row to the terminal,
2172 // shifting the top row into the scrollback.
2173 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08002174 } else {
rginda87b86462011-12-14 13:48:03 -08002175 // Anywhere else in the screen just moves the cursor.
2176 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08002177 }
2178};
2179
2180/**
2181 * Like newLine(), except maintain the cursor column.
2182 */
2183hterm.Terminal.prototype.lineFeed = function() {
2184 var column = this.screen_.cursorPosition.column;
2185 this.newLine();
2186 this.setCursorColumn(column);
2187};
2188
2189/**
rginda87b86462011-12-14 13:48:03 -08002190 * If autoCarriageReturn is set then newLine(), else lineFeed().
2191 */
2192hterm.Terminal.prototype.formFeed = function() {
2193 if (this.options_.autoCarriageReturn) {
2194 this.newLine();
2195 } else {
2196 this.lineFeed();
2197 }
2198};
2199
2200/**
2201 * Move the cursor up one row, possibly inserting a blank line.
2202 *
2203 * The cursor column is not changed.
2204 */
2205hterm.Terminal.prototype.reverseLineFeed = function() {
2206 var scrollTop = this.getVTScrollTop();
2207 var currentRow = this.screen_.cursorPosition.row;
2208
2209 if (currentRow == scrollTop) {
2210 this.insertLines(1);
2211 } else {
2212 this.setAbsoluteCursorRow(currentRow - 1);
2213 }
2214};
2215
2216/**
rginda8ba33642011-12-14 12:31:31 -08002217 * Replace all characters to the left of the current cursor with the space
2218 * character.
2219 *
2220 * TODO(rginda): This should probably *remove* the characters (not just replace
2221 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07002222 * position.
rginda8ba33642011-12-14 12:31:31 -08002223 */
2224hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08002225 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002226 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002227 const count = cursor.column + 1;
Mike Frysinger73e56462019-07-17 00:23:46 -05002228 this.screen_.overwriteString(' '.repeat(count), count);
rginda87b86462011-12-14 13:48:03 -08002229 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002230};
2231
2232/**
David Benjamin684a9b72012-05-01 17:19:58 -04002233 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08002234 *
2235 * The cursor position is unchanged.
2236 *
Robert Gindaf2547f12012-10-25 20:36:21 -07002237 * If the current background color is not the default background color this
2238 * will insert spaces rather than delete. This is unfortunate because the
2239 * trailing space will affect text selection, but it's difficult to come up
2240 * with a way to style empty space that wouldn't trip up the hterm.Screen
2241 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07002242 *
2243 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
2244 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
2245 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05002246 *
Joel Hockey0f933582019-08-27 18:01:51 -07002247 * @param {number=} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08002248 */
2249hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002250 if (this.screen_.cursorPosition.overflow) {
Robert Gindacd5637d2013-10-30 14:59:10 -07002251 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002252 }
Robert Gindacd5637d2013-10-30 14:59:10 -07002253
Robert Ginda7fd57082012-09-25 14:41:47 -07002254 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
2255 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07002256
2257 if (this.screen_.textAttributes.background ===
2258 this.screen_.textAttributes.DEFAULT_COLOR) {
2259 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08002260 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07002261 this.screen_.cursorPosition.column + count) {
2262 this.screen_.deleteChars(count);
2263 this.clearCursorOverflow();
2264 return;
2265 }
2266 }
2267
rginda87b86462011-12-14 13:48:03 -08002268 var cursor = this.saveCursor();
Mike Frysinger73e56462019-07-17 00:23:46 -05002269 this.screen_.overwriteString(' '.repeat(count), count);
rginda87b86462011-12-14 13:48:03 -08002270 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002271 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002272};
2273
2274/**
2275 * Erase the current line.
2276 *
2277 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002278 */
2279hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08002280 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002281 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08002282 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002283 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002284};
2285
2286/**
David Benjamina08d78f2012-05-05 00:28:49 -04002287 * Erase all characters from the start of the screen to the current cursor
2288 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002289 *
2290 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002291 */
2292hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08002293 var cursor = this.saveCursor();
2294
2295 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002296
David Benjamina08d78f2012-05-05 00:28:49 -04002297 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002298 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002299 this.screen_.clearCursorRow();
2300 }
2301
rginda87b86462011-12-14 13:48:03 -08002302 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002303 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002304};
2305
2306/**
2307 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002308 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002309 *
2310 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002311 */
2312hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08002313 var cursor = this.saveCursor();
2314
2315 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002316
David Benjamina08d78f2012-05-05 00:28:49 -04002317 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08002318 for (var i = cursor.row + 1; i <= bottom; i++) {
2319 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002320 this.screen_.clearCursorRow();
2321 }
2322
rginda87b86462011-12-14 13:48:03 -08002323 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002324 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002325};
2326
2327/**
2328 * Fill the terminal with a given character.
2329 *
2330 * This methods does not respect the VT scroll region.
2331 *
2332 * @param {string} ch The character to use for the fill.
2333 */
2334hterm.Terminal.prototype.fill = function(ch) {
2335 var cursor = this.saveCursor();
2336
2337 this.setAbsoluteCursorPosition(0, 0);
2338 for (var row = 0; row < this.screenSize.height; row++) {
2339 for (var col = 0; col < this.screenSize.width; col++) {
2340 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002341 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002342 }
2343 }
2344
2345 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002346};
2347
2348/**
rginda9ea433c2012-03-16 11:57:00 -07002349 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002350 *
rginda9ea433c2012-03-16 11:57:00 -07002351 * This does not respect the scroll region.
2352 *
Joel Hockey0f933582019-08-27 18:01:51 -07002353 * @param {!hterm.Screen=} opt_screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002354 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002355 */
rginda9ea433c2012-03-16 11:57:00 -07002356hterm.Terminal.prototype.clearHome = function(opt_screen) {
2357 var screen = opt_screen || this.screen_;
2358 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002359
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002360 this.accessibilityReader_.clear();
2361
rginda11057d52012-04-25 12:29:56 -07002362 if (bottom == 0) {
2363 // Empty screen, nothing to do.
2364 return;
2365 }
2366
rgindae4d29232012-01-19 10:47:13 -08002367 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002368 screen.setCursorPosition(i, 0);
2369 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002370 }
2371
rginda9ea433c2012-03-16 11:57:00 -07002372 screen.setCursorPosition(0, 0);
2373};
2374
2375/**
2376 * Erase the entire display without changing the cursor position.
2377 *
2378 * The cursor position is unchanged. This does not respect the scroll
2379 * region.
2380 *
Joel Hockey0f933582019-08-27 18:01:51 -07002381 * @param {!hterm.Screen=} opt_screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002382 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002383 */
2384hterm.Terminal.prototype.clear = function(opt_screen) {
2385 var screen = opt_screen || this.screen_;
2386 var cursor = screen.cursorPosition.clone();
2387 this.clearHome(screen);
2388 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002389};
2390
2391/**
2392 * VT command to insert lines at the current cursor row.
2393 *
2394 * This respects the current scroll region. Rows pushed off the bottom are
2395 * lost (they won't show up in the scrollback buffer).
2396 *
Joel Hockey0f933582019-08-27 18:01:51 -07002397 * @param {number} count The number of lines to insert.
rginda8ba33642011-12-14 12:31:31 -08002398 */
2399hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07002400 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002401
2402 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002403 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002404
Robert Ginda579186b2012-09-26 11:40:04 -07002405 // The moveCount is the number of rows we need to relocate to make room for
2406 // the new row(s). The count is the distance to move them.
2407 var moveCount = bottom - cursorRow - count + 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002408 if (moveCount) {
Robert Ginda579186b2012-09-26 11:40:04 -07002409 this.moveRows_(cursorRow, moveCount, cursorRow + count);
Mike Frysingerbdb34802020-04-07 03:47:32 -04002410 }
rginda8ba33642011-12-14 12:31:31 -08002411
Robert Ginda579186b2012-09-26 11:40:04 -07002412 for (var i = count - 1; i >= 0; i--) {
2413 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002414 this.screen_.clearCursorRow();
2415 }
rginda8ba33642011-12-14 12:31:31 -08002416};
2417
2418/**
2419 * VT command to delete lines at the current cursor row.
2420 *
2421 * New rows are added to the bottom of scroll region to take their place. New
2422 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002423 *
2424 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002425 */
2426hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08002427 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002428
rginda87b86462011-12-14 13:48:03 -08002429 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08002430 var bottom = this.getVTScrollBottom();
2431
rginda87b86462011-12-14 13:48:03 -08002432 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002433 count = Math.min(count, maxCount);
2434
rginda87b86462011-12-14 13:48:03 -08002435 var moveStart = bottom - count + 1;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002436 if (count != maxCount) {
rginda8ba33642011-12-14 12:31:31 -08002437 this.moveRows_(top, count, moveStart);
Mike Frysingerbdb34802020-04-07 03:47:32 -04002438 }
rginda8ba33642011-12-14 12:31:31 -08002439
2440 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002441 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002442 this.screen_.clearCursorRow();
2443 }
2444
rginda87b86462011-12-14 13:48:03 -08002445 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002446 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002447};
2448
2449/**
2450 * Inserts the given number of spaces at the current cursor position.
2451 *
rginda87b86462011-12-14 13:48:03 -08002452 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002453 *
2454 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002455 */
2456hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08002457 var cursor = this.saveCursor();
2458
Mike Frysinger73e56462019-07-17 00:23:46 -05002459 const ws = ' '.repeat(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002460 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002461 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002462
2463 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002464 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002465};
2466
2467/**
2468 * Forward-delete the specified number of characters starting at the cursor
2469 * position.
2470 *
Joel Hockey0f933582019-08-27 18:01:51 -07002471 * @param {number} count The number of characters to delete.
rginda8ba33642011-12-14 12:31:31 -08002472 */
2473hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002474 var deleted = this.screen_.deleteChars(count);
2475 if (deleted && !this.screen_.textAttributes.isDefault()) {
2476 var cursor = this.saveCursor();
2477 this.setCursorColumn(this.screenSize.width - deleted);
Mike Frysinger73e56462019-07-17 00:23:46 -05002478 this.screen_.insertString(' '.repeat(deleted));
Robert Ginda7fd57082012-09-25 14:41:47 -07002479 this.restoreCursor(cursor);
2480 }
2481
David Benjamin54e8bf62012-06-01 22:31:40 -04002482 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002483};
2484
2485/**
2486 * Shift rows in the scroll region upwards by a given number of lines.
2487 *
2488 * New rows are inserted at the bottom of the scroll region to fill the
2489 * vacated rows. The new rows not filled out with the current text attributes.
2490 *
2491 * This function does not affect the scrollback rows at all. Rows shifted
2492 * off the top are lost.
2493 *
rginda87b86462011-12-14 13:48:03 -08002494 * The cursor position is not altered.
2495 *
Joel Hockey0f933582019-08-27 18:01:51 -07002496 * @param {number} count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002497 */
2498hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002499 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002500
rginda87b86462011-12-14 13:48:03 -08002501 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002502 this.deleteLines(count);
2503
rginda87b86462011-12-14 13:48:03 -08002504 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002505};
2506
2507/**
2508 * Shift rows below the cursor down by a given number of lines.
2509 *
2510 * This function respects the current scroll region.
2511 *
2512 * New rows are inserted at the top of the scroll region to fill the
2513 * vacated rows. The new rows not filled out with the current text attributes.
2514 *
2515 * This function does not affect the scrollback rows at all. Rows shifted
2516 * off the bottom are lost.
2517 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07002518 * @param {number} count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002519 */
Joel Hockeyd4fca732019-09-20 16:57:03 -07002520hterm.Terminal.prototype.vtScrollDown = function(count) {
rginda87b86462011-12-14 13:48:03 -08002521 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002522
rginda87b86462011-12-14 13:48:03 -08002523 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
Joel Hockeyd4fca732019-09-20 16:57:03 -07002524 this.insertLines(count);
rginda8ba33642011-12-14 12:31:31 -08002525
rginda87b86462011-12-14 13:48:03 -08002526 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002527};
2528
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002529/**
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002530 * Enable accessibility-friendly features that have a performance impact.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002531 *
2532 * This will generate additional DOM nodes in an aria-live region that will
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002533 * cause Assitive Technology to announce the output of the terminal. It also
2534 * enables other features that aid assistive technology. All the features gated
2535 * behind this flag have a performance impact on the terminal which is why they
2536 * are made optional.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002537 *
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002538 * @param {boolean} enabled Whether to enable accessibility-friendly features.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002539 */
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002540hterm.Terminal.prototype.setAccessibilityEnabled = function(enabled) {
Raymes Khoury177aec72018-06-26 10:58:53 +10002541 this.accessibilityReader_.setAccessibilityEnabled(enabled);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002542};
rginda87b86462011-12-14 13:48:03 -08002543
rginda8ba33642011-12-14 12:31:31 -08002544/**
2545 * Set the cursor position.
2546 *
2547 * The cursor row is relative to the scroll region if the terminal has
2548 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2549 *
Joel Hockey0f933582019-08-27 18:01:51 -07002550 * @param {number} row The new zero-based cursor row.
2551 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002552 */
2553hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2554 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002555 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002556 } else {
rginda87b86462011-12-14 13:48:03 -08002557 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002558 }
rginda87b86462011-12-14 13:48:03 -08002559};
rginda8ba33642011-12-14 12:31:31 -08002560
Evan Jones2600d4f2016-12-06 09:29:36 -05002561/**
2562 * Move the cursor relative to its current position.
2563 *
2564 * @param {number} row
2565 * @param {number} column
2566 */
rginda87b86462011-12-14 13:48:03 -08002567hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2568 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002569 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2570 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002571 this.screen_.setCursorPosition(row, column);
2572};
2573
Evan Jones2600d4f2016-12-06 09:29:36 -05002574/**
2575 * Move the cursor to the specified position.
2576 *
2577 * @param {number} row
2578 * @param {number} column
2579 */
rginda87b86462011-12-14 13:48:03 -08002580hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002581 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2582 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002583 this.screen_.setCursorPosition(row, column);
2584};
2585
2586/**
2587 * Set the cursor column.
2588 *
Joel Hockey0f933582019-08-27 18:01:51 -07002589 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002590 */
2591hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002592 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002593};
2594
2595/**
2596 * Return the cursor column.
2597 *
Joel Hockey0f933582019-08-27 18:01:51 -07002598 * @return {number} The zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002599 */
2600hterm.Terminal.prototype.getCursorColumn = function() {
2601 return this.screen_.cursorPosition.column;
2602};
2603
2604/**
2605 * Set the cursor row.
2606 *
2607 * The cursor row is relative to the scroll region if the terminal has
2608 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2609 *
Joel Hockey0f933582019-08-27 18:01:51 -07002610 * @param {number} row The new cursor row.
rginda8ba33642011-12-14 12:31:31 -08002611 */
rginda87b86462011-12-14 13:48:03 -08002612hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2613 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002614};
2615
2616/**
2617 * Return the cursor row.
2618 *
Joel Hockey0f933582019-08-27 18:01:51 -07002619 * @return {number} The zero-based cursor row.
rginda8ba33642011-12-14 12:31:31 -08002620 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002621hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002622 return this.screen_.cursorPosition.row;
2623};
2624
2625/**
2626 * Request that the ScrollPort redraw itself soon.
2627 *
2628 * The redraw will happen asynchronously, soon after the call stack winds down.
2629 * Multiple calls will be coalesced into a single redraw.
2630 */
2631hterm.Terminal.prototype.scheduleRedraw_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002632 if (this.timeouts_.redraw) {
rginda87b86462011-12-14 13:48:03 -08002633 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002634 }
rginda8ba33642011-12-14 12:31:31 -08002635
2636 var self = this;
rginda87b86462011-12-14 13:48:03 -08002637 this.timeouts_.redraw = setTimeout(function() {
2638 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002639 self.scrollPort_.redraw_();
2640 }, 0);
2641};
2642
2643/**
2644 * Request that the ScrollPort be scrolled to the bottom.
2645 *
2646 * The scroll will happen asynchronously, soon after the call stack winds down.
2647 * Multiple calls will be coalesced into a single scroll.
2648 *
2649 * This affects the scrollbar position of the ScrollPort, and has nothing to
2650 * do with the VT scroll commands.
2651 */
2652hterm.Terminal.prototype.scheduleScrollDown_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002653 if (this.timeouts_.scrollDown) {
rginda87b86462011-12-14 13:48:03 -08002654 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002655 }
rginda8ba33642011-12-14 12:31:31 -08002656
2657 var self = this;
2658 this.timeouts_.scrollDown = setTimeout(function() {
2659 delete self.timeouts_.scrollDown;
2660 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2661 }, 10);
2662};
2663
2664/**
2665 * Move the cursor up a specified number of rows.
2666 *
Joel Hockey0f933582019-08-27 18:01:51 -07002667 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002668 */
2669hterm.Terminal.prototype.cursorUp = function(count) {
Joel Hockey0f933582019-08-27 18:01:51 -07002670 this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002671};
2672
2673/**
2674 * Move the cursor down a specified number of rows.
2675 *
Joel Hockey0f933582019-08-27 18:01:51 -07002676 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002677 */
2678hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002679 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002680 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2681 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2682 this.screenSize.height - 1);
2683
rgindacbbd7482012-06-13 15:06:16 -07002684 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002685 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002686 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002687};
2688
2689/**
2690 * Move the cursor left a specified number of columns.
2691 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002692 * If reverse wraparound mode is enabled and the previous row wrapped into
2693 * the current row then we back up through the wraparound as well.
2694 *
Joel Hockey0f933582019-08-27 18:01:51 -07002695 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002696 */
2697hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002698 count = count || 1;
2699
Mike Frysingerbdb34802020-04-07 03:47:32 -04002700 if (count < 1) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002701 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002702 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002703
2704 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002705 if (this.options_.reverseWraparound) {
2706 if (this.screen_.cursorPosition.overflow) {
2707 // If this cursor is in the right margin, consume one count to get it
2708 // back to the last column. This only applies when we're in reverse
2709 // wraparound mode.
2710 count--;
2711 this.clearCursorOverflow();
2712
Mike Frysingerbdb34802020-04-07 03:47:32 -04002713 if (!count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002714 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002715 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002716 }
2717
Robert Gindabfb32622014-07-17 13:20:27 -07002718 var newRow = this.screen_.cursorPosition.row;
2719 var newColumn = currentColumn - count;
2720 if (newColumn < 0) {
2721 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2722 if (newRow < 0) {
2723 // xterm also wraps from row 0 to the last row.
2724 newRow = this.screenSize.height + newRow % this.screenSize.height;
2725 }
2726 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2727 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002728
Robert Gindabfb32622014-07-17 13:20:27 -07002729 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2730
2731 } else {
2732 var newColumn = Math.max(currentColumn - count, 0);
2733 this.setCursorColumn(newColumn);
2734 }
rginda8ba33642011-12-14 12:31:31 -08002735};
2736
2737/**
2738 * Move the cursor right a specified number of columns.
2739 *
Joel Hockey0f933582019-08-27 18:01:51 -07002740 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002741 */
2742hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002743 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002744
Mike Frysingerbdb34802020-04-07 03:47:32 -04002745 if (count < 1) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002746 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002747 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002748
rgindacbbd7482012-06-13 15:06:16 -07002749 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002750 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002751 this.setCursorColumn(column);
2752};
2753
2754/**
2755 * Reverse the foreground and background colors of the terminal.
2756 *
2757 * This only affects text that was drawn with no attributes.
2758 *
2759 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2760 * been drawn with attributes that happen to coincide with the default
2761 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002762 *
2763 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002764 */
2765hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002766 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002767 if (state) {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002768 this.setRgbColorCssVar('foreground-color', this.backgroundColor_);
2769 this.setRgbColorCssVar('background-color', this.foregroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002770 } else {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002771 this.setRgbColorCssVar('foreground-color', this.foregroundColor_);
2772 this.setRgbColorCssVar('background-color', this.backgroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002773 }
2774};
2775
2776/**
rginda87b86462011-12-14 13:48:03 -08002777 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002778 *
2779 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002780 */
2781hterm.Terminal.prototype.ringBell = function() {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002782 this.cursorNode_.style.backgroundColor = 'rgb(var(--hterm-foreground-color))';
rginda87b86462011-12-14 13:48:03 -08002783
2784 var self = this;
2785 setTimeout(function() {
Matheus Fernandes2d733082017-09-11 06:43:01 -04002786 self.restyleCursor_();
rginda6d397402012-01-17 10:58:29 -08002787 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002788
Michael Kelly485ecd12014-06-09 11:41:56 -04002789 // bellSquelchTimeout_ affects both audio and notification bells.
Mike Frysingerbdb34802020-04-07 03:47:32 -04002790 if (this.bellSquelchTimeout_) {
Michael Kelly485ecd12014-06-09 11:41:56 -04002791 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002792 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002793
Robert Ginda92e18102013-03-14 13:56:37 -07002794 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002795 this.bellAudio_.play();
Joel Hockeyd4fca732019-09-20 16:57:03 -07002796 this.bellSequelchTimeout_ = setTimeout(() => {
2797 this.bellSquelchTimeout_ = null;
2798 }, 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002799 } else {
Joel Hockeyd4fca732019-09-20 16:57:03 -07002800 this.bellSquelchTimeout_ = null;
Robert Ginda92e18102013-03-14 13:56:37 -07002801 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002802
2803 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingera5fb83c2017-06-22 14:48:35 -07002804 var n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002805 this.bellNotificationList_.push(n);
2806 // TODO: Should we try to raise the window here?
2807 n.onclick = function() { self.closeBellNotifications_(); };
2808 }
rginda87b86462011-12-14 13:48:03 -08002809};
2810
2811/**
rginda8ba33642011-12-14 12:31:31 -08002812 * Set the origin mode bit.
2813 *
2814 * If origin mode is on, certain VT cursor and scrolling commands measure their
2815 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2816 * to the top of the addressable screen.
2817 *
2818 * Defaults to off.
2819 *
2820 * @param {boolean} state True to set origin mode, false to unset.
2821 */
2822hterm.Terminal.prototype.setOriginMode = function(state) {
2823 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002824 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002825};
2826
2827/**
2828 * Set the insert mode bit.
2829 *
2830 * If insert mode is on, existing text beyond the cursor position will be
2831 * shifted right to make room for new text. Otherwise, new text overwrites
2832 * any existing text.
2833 *
2834 * Defaults to off.
2835 *
2836 * @param {boolean} state True to set insert mode, false to unset.
2837 */
2838hterm.Terminal.prototype.setInsertMode = function(state) {
2839 this.options_.insertMode = state;
2840};
2841
2842/**
rginda87b86462011-12-14 13:48:03 -08002843 * Set the auto carriage return bit.
2844 *
2845 * If auto carriage return is on then a formfeed character is interpreted
2846 * as a newline, otherwise it's the same as a linefeed. The difference boils
2847 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002848 *
2849 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002850 */
2851hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2852 this.options_.autoCarriageReturn = state;
2853};
2854
2855/**
rginda8ba33642011-12-14 12:31:31 -08002856 * Set the wraparound mode bit.
2857 *
2858 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2859 * to the start of the following row. Otherwise, the cursor is clamped to the
2860 * end of the screen and attempts to write past it are ignored.
2861 *
2862 * Defaults to on.
2863 *
2864 * @param {boolean} state True to set wraparound mode, false to unset.
2865 */
2866hterm.Terminal.prototype.setWraparound = function(state) {
2867 this.options_.wraparound = state;
2868};
2869
2870/**
2871 * Set the reverse-wraparound mode bit.
2872 *
2873 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2874 * to the end of the previous row. Otherwise, the cursor is clamped to column
2875 * 0.
2876 *
2877 * Defaults to off.
2878 *
2879 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2880 */
2881hterm.Terminal.prototype.setReverseWraparound = function(state) {
2882 this.options_.reverseWraparound = state;
2883};
2884
2885/**
2886 * Selects between the primary and alternate screens.
2887 *
2888 * If alternate mode is on, the alternate screen is active. Otherwise the
2889 * primary screen is active.
2890 *
2891 * Swapping screens has no effect on the scrollback buffer.
2892 *
2893 * Each screen maintains its own cursor position.
2894 *
2895 * Defaults to off.
2896 *
2897 * @param {boolean} state True to set alternate mode, false to unset.
2898 */
2899hterm.Terminal.prototype.setAlternateMode = function(state) {
Joel Hockey42dba8f2020-03-26 16:21:11 -07002900 if (state == (this.screen_ == this.alternateScreen_)) {
2901 return;
2902 }
2903 const oldOverrides = this.screen_.textAttributes.colorPaletteOverrides;
2904 const cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002905 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2906
Joel Hockey42dba8f2020-03-26 16:21:11 -07002907 // Swap color overrides.
2908 const newOverrides = this.screen_.textAttributes.colorPaletteOverrides;
2909 oldOverrides.forEach((c, i) => {
2910 if (!newOverrides.hasOwnProperty(i)) {
2911 this.setRgbColorCssVar(`color-${i}`, this.getColorPalette(i));
2912 }
2913 });
2914 newOverrides.forEach((c, i) => this.setRgbColorCssVar(`color-${i}`, c));
2915
rginda35c456b2012-02-09 17:29:05 -08002916 if (this.screen_.rowsArray.length &&
2917 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2918 // If the screen changed sizes while we were away, our rowIndexes may
2919 // be incorrect.
Joel Hockey42dba8f2020-03-26 16:21:11 -07002920 const offset = this.scrollbackRows_.length;
2921 const ary = this.screen_.rowsArray;
2922 for (let i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002923 ary[i].rowIndex = offset + i;
2924 }
2925 }
rginda8ba33642011-12-14 12:31:31 -08002926
rginda35c456b2012-02-09 17:29:05 -08002927 this.realizeWidth_(this.screenSize.width);
2928 this.realizeHeight_(this.screenSize.height);
2929 this.scrollPort_.syncScrollHeight();
2930 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002931
rginda6d397402012-01-17 10:58:29 -08002932 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002933 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002934};
2935
2936/**
2937 * Set the cursor-blink mode bit.
2938 *
2939 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2940 * a visible cursor does not blink.
2941 *
2942 * You should make sure to turn blinking off if you're going to dispose of a
2943 * terminal, otherwise you'll leak a timeout.
2944 *
2945 * Defaults to on.
2946 *
2947 * @param {boolean} state True to set cursor-blink mode, false to unset.
2948 */
2949hterm.Terminal.prototype.setCursorBlink = function(state) {
2950 this.options_.cursorBlink = state;
2951
2952 if (!state && this.timeouts_.cursorBlink) {
2953 clearTimeout(this.timeouts_.cursorBlink);
2954 delete this.timeouts_.cursorBlink;
2955 }
2956
Mike Frysingerbdb34802020-04-07 03:47:32 -04002957 if (this.options_.cursorVisible) {
rginda8ba33642011-12-14 12:31:31 -08002958 this.setCursorVisible(true);
Mike Frysingerbdb34802020-04-07 03:47:32 -04002959 }
rginda8ba33642011-12-14 12:31:31 -08002960};
2961
2962/**
2963 * Set the cursor-visible mode bit.
2964 *
2965 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2966 *
2967 * Defaults to on.
2968 *
2969 * @param {boolean} state True to set cursor-visible mode, false to unset.
2970 */
2971hterm.Terminal.prototype.setCursorVisible = function(state) {
2972 this.options_.cursorVisible = state;
2973
2974 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002975 if (this.timeouts_.cursorBlink) {
2976 clearTimeout(this.timeouts_.cursorBlink);
2977 delete this.timeouts_.cursorBlink;
2978 }
rginda87b86462011-12-14 13:48:03 -08002979 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002980 return;
2981 }
2982
rginda87b86462011-12-14 13:48:03 -08002983 this.syncCursorPosition_();
2984
2985 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002986
2987 if (this.options_.cursorBlink) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04002988 if (this.timeouts_.cursorBlink) {
rginda8ba33642011-12-14 12:31:31 -08002989 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04002990 }
rginda8ba33642011-12-14 12:31:31 -08002991
Robert Gindaea2183e2014-07-17 09:51:51 -07002992 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002993 } else {
2994 if (this.timeouts_.cursorBlink) {
2995 clearTimeout(this.timeouts_.cursorBlink);
2996 delete this.timeouts_.cursorBlink;
2997 }
2998 }
2999};
3000
3001/**
Mike Frysinger225c99d2019-10-20 14:02:37 -06003002 * Pause blinking temporarily.
3003 *
3004 * When the cursor moves around, it can be helpful to momentarily pause the
3005 * blinking. This could be when the user is typing in things, or when they're
3006 * moving around with the arrow keys.
3007 */
3008hterm.Terminal.prototype.pauseCursorBlink_ = function() {
3009 if (!this.options_.cursorBlink) {
3010 return;
3011 }
3012
3013 this.cursorBlinkPause_ = true;
3014
3015 // If a timeout is already pending, reset the clock due to the new input.
3016 if (this.timeouts_.cursorBlinkPause) {
3017 clearTimeout(this.timeouts_.cursorBlinkPause);
3018 }
3019 // After 500ms, resume blinking. That seems like a good balance between user
3020 // input timings & responsiveness to resume.
3021 this.timeouts_.cursorBlinkPause = setTimeout(() => {
3022 delete this.timeouts_.cursorBlinkPause;
3023 this.cursorBlinkPause_ = false;
3024 }, 500);
3025};
3026
3027/**
rginda87b86462011-12-14 13:48:03 -08003028 * Synchronizes the visible cursor and document selection with the current
3029 * cursor coordinates.
Raymes Khourye5d48982018-08-02 09:08:32 +10003030 *
3031 * @return {boolean} True if the cursor is onscreen and synced.
rginda8ba33642011-12-14 12:31:31 -08003032 */
3033hterm.Terminal.prototype.syncCursorPosition_ = function() {
3034 var topRowIndex = this.scrollPort_.getTopRowIndex();
3035 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
3036 var cursorRowIndex = this.scrollbackRows_.length +
3037 this.screen_.cursorPosition.row;
3038
Raymes Khoury15697f42018-07-17 11:37:18 +10003039 let forceSyncSelection = false;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003040 if (this.accessibilityReader_.accessibilityEnabled) {
3041 // Report the new position of the cursor for accessibility purposes.
3042 const cursorColumnIndex = this.screen_.cursorPosition.column;
3043 const cursorLineText =
3044 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
Raymes Khoury15697f42018-07-17 11:37:18 +10003045 // This will force the selection to be sync'd to the cursor position if the
3046 // user has pressed a key. Generally we would only sync the cursor position
3047 // when selection is collapsed so that if the user has selected something
3048 // we don't clear the selection by moving the selection. However when a
3049 // screen reader is used, it's intuitive for entering a key to move the
3050 // selection to the cursor.
3051 forceSyncSelection = this.accessibilityReader_.hasUserGesture;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003052 this.accessibilityReader_.afterCursorChange(
3053 cursorLineText, cursorRowIndex, cursorColumnIndex);
3054 }
3055
rginda8ba33642011-12-14 12:31:31 -08003056 if (cursorRowIndex > bottomRowIndex) {
3057 // Cursor is scrolled off screen, move it outside of the visible area.
Mike Frysinger44c32202017-08-05 01:13:09 -04003058 this.setCssVar('cursor-offset-row', '-1');
Raymes Khourye5d48982018-08-02 09:08:32 +10003059 return false;
rginda8ba33642011-12-14 12:31:31 -08003060 }
3061
Robert Gindab837c052014-08-11 11:17:51 -07003062 if (this.options_.cursorVisible &&
3063 this.cursorNode_.style.display == 'none') {
3064 // Re-display the terminal cursor if it was hidden by the mouse cursor.
3065 this.cursorNode_.style.display = '';
3066 }
3067
Mike Frysinger44c32202017-08-05 01:13:09 -04003068 // Position the cursor using CSS variable math. If we do the math in JS,
3069 // the float math will end up being more precise than the CSS which will
3070 // cause the cursor tracking to be off.
3071 this.setCssVar(
3072 'cursor-offset-row',
3073 `${cursorRowIndex - topRowIndex} + ` +
3074 `${this.scrollPort_.visibleRowTopMargin}px`);
3075 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08003076
3077 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04003078 '(' + this.screen_.cursorPosition.column +
3079 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08003080 ')');
3081
3082 // Update the caret for a11y purposes.
3083 var selection = this.document_.getSelection();
Raymes Khoury15697f42018-07-17 11:37:18 +10003084 if (selection && (selection.isCollapsed || forceSyncSelection)) {
rginda87b86462011-12-14 13:48:03 -08003085 this.screen_.syncSelectionCaret(selection);
Raymes Khoury15697f42018-07-17 11:37:18 +10003086 }
Raymes Khourye5d48982018-08-02 09:08:32 +10003087 return true;
rginda8ba33642011-12-14 12:31:31 -08003088};
3089
Robert Gindafb1be6a2013-12-11 11:56:22 -08003090/**
3091 * Adjusts the style of this.cursorNode_ according to the current cursor shape
3092 * and character cell dimensions.
3093 */
Robert Ginda830583c2013-08-07 13:20:46 -07003094hterm.Terminal.prototype.restyleCursor_ = function() {
3095 var shape = this.cursorShape_;
3096
3097 if (this.cursorNode_.getAttribute('focus') == 'false') {
3098 // Always show a block cursor when unfocused.
3099 shape = hterm.Terminal.cursorShape.BLOCK;
3100 }
3101
3102 var style = this.cursorNode_.style;
3103
3104 switch (shape) {
3105 case hterm.Terminal.cursorShape.BEAM:
Robert Ginda830583c2013-08-07 13:20:46 -07003106 style.backgroundColor = 'transparent';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003107 style.borderBottomStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003108 style.borderLeftStyle = 'solid';
3109 break;
3110
3111 case hterm.Terminal.cursorShape.UNDERLINE:
Robert Ginda830583c2013-08-07 13:20:46 -07003112 style.backgroundColor = 'transparent';
3113 style.borderBottomStyle = 'solid';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003114 style.borderLeftStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003115 break;
3116
3117 default:
Mike Frysinger2fd079a2018-09-02 01:46:12 -04003118 style.backgroundColor = 'var(--hterm-cursor-color)';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003119 style.borderBottomStyle = '';
3120 style.borderLeftStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003121 break;
3122 }
3123};
3124
rginda8ba33642011-12-14 12:31:31 -08003125/**
3126 * Synchronizes the visible cursor with the current cursor coordinates.
3127 *
3128 * The sync will happen asynchronously, soon after the call stack winds down.
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003129 * Multiple calls will be coalesced into a single sync. This should be called
3130 * prior to the cursor actually changing position.
rginda8ba33642011-12-14 12:31:31 -08003131 */
3132hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003133 if (this.timeouts_.syncCursor) {
rginda87b86462011-12-14 13:48:03 -08003134 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003135 }
rginda8ba33642011-12-14 12:31:31 -08003136
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003137 if (this.accessibilityReader_.accessibilityEnabled) {
3138 // Report the previous position of the cursor for accessibility purposes.
3139 const cursorRowIndex = this.scrollbackRows_.length +
3140 this.screen_.cursorPosition.row;
3141 const cursorColumnIndex = this.screen_.cursorPosition.column;
3142 const cursorLineText =
3143 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
3144 this.accessibilityReader_.beforeCursorChange(
3145 cursorLineText, cursorRowIndex, cursorColumnIndex);
3146 }
3147
rginda8ba33642011-12-14 12:31:31 -08003148 var self = this;
3149 this.timeouts_.syncCursor = setTimeout(function() {
3150 self.syncCursorPosition_();
3151 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08003152 }, 0);
3153};
3154
rgindacc2996c2012-02-24 14:59:31 -08003155/**
rgindaf522ce02012-04-17 17:49:17 -07003156 * Show or hide the zoom warning.
3157 *
3158 * The zoom warning is a message warning the user that their browser zoom must
3159 * be set to 100% in order for hterm to function properly.
3160 *
3161 * @param {boolean} state True to show the message, false to hide it.
3162 */
3163hterm.Terminal.prototype.showZoomWarning_ = function(state) {
3164 if (!this.zoomWarningNode_) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003165 if (!state) {
rgindaf522ce02012-04-17 17:49:17 -07003166 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003167 }
rgindaf522ce02012-04-17 17:49:17 -07003168
3169 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04003170 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07003171 this.zoomWarningNode_.style.cssText = (
3172 'color: black;' +
3173 'background-color: #ff2222;' +
3174 'font-size: large;' +
3175 'border-radius: 8px;' +
3176 'opacity: 0.75;' +
3177 'padding: 0.2em 0.5em 0.2em 0.5em;' +
3178 'top: 0.5em;' +
3179 'right: 1.2em;' +
3180 'position: absolute;' +
3181 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003182 '-webkit-user-select: none;' +
3183 '-moz-text-size-adjust: none;' +
3184 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05003185
3186 this.zoomWarningNode_.addEventListener('click', function(e) {
3187 this.parentNode.removeChild(this);
3188 });
rgindaf522ce02012-04-17 17:49:17 -07003189 }
3190
Mike Frysingerb7289952019-03-23 16:05:38 -07003191 this.zoomWarningNode_.textContent = lib.i18n.replaceReferences(
Robert Gindab4839c22013-02-28 16:52:10 -08003192 hterm.zoomWarningMessage,
Joel Hockeyd4fca732019-09-20 16:57:03 -07003193 [Math.floor(this.scrollPort_.characterSize.zoomFactor * 100)]);
Robert Gindab4839c22013-02-28 16:52:10 -08003194
rgindaf522ce02012-04-17 17:49:17 -07003195 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
3196
3197 if (state) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003198 if (!this.zoomWarningNode_.parentNode) {
rgindaf522ce02012-04-17 17:49:17 -07003199 this.div_.parentNode.appendChild(this.zoomWarningNode_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003200 }
rgindaf522ce02012-04-17 17:49:17 -07003201 } else if (this.zoomWarningNode_.parentNode) {
3202 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
3203 }
3204};
3205
3206/**
rgindacc2996c2012-02-24 14:59:31 -08003207 * Show the terminal overlay for a given amount of time.
3208 *
3209 * The terminal overlay appears in inverse video in a large font, centered
3210 * over the terminal. You should probably keep the overlay message brief,
3211 * since it's in a large font and you probably aren't going to check the size
3212 * of the terminal first.
3213 *
3214 * @param {string} msg The text (not HTML) message to display in the overlay.
Joel Hockey0f933582019-08-27 18:01:51 -07003215 * @param {number=} opt_timeout The amount of time to wait before fading out
rgindacc2996c2012-02-24 14:59:31 -08003216 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
3217 * stay up forever (or until the next overlay).
3218 */
3219hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08003220 if (!this.overlayNode_) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003221 if (!this.div_) {
rgindaf0090c92012-02-10 14:58:52 -08003222 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003223 }
rgindaf0090c92012-02-10 14:58:52 -08003224
3225 this.overlayNode_ = this.document_.createElement('div');
3226 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08003227 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08003228 'font-size: xx-large;' +
3229 'opacity: 0.75;' +
3230 'padding: 0.2em 0.5em 0.2em 0.5em;' +
3231 'position: absolute;' +
3232 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003233 '-webkit-transition: opacity 180ms ease-in;' +
3234 '-moz-user-select: none;' +
3235 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08003236
3237 this.overlayNode_.addEventListener('mousedown', function(e) {
3238 e.preventDefault();
3239 e.stopPropagation();
3240 }, true);
rgindaf0090c92012-02-10 14:58:52 -08003241 }
3242
rginda9f5222b2012-03-05 11:53:28 -08003243 this.overlayNode_.style.color = this.prefs_.get('background-color');
3244 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
3245 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
3246
rgindaf0090c92012-02-10 14:58:52 -08003247 this.overlayNode_.textContent = msg;
3248 this.overlayNode_.style.opacity = '0.75';
3249
Mike Frysingerbdb34802020-04-07 03:47:32 -04003250 if (!this.overlayNode_.parentNode) {
rgindaf0090c92012-02-10 14:58:52 -08003251 this.div_.appendChild(this.overlayNode_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003252 }
rgindaf0090c92012-02-10 14:58:52 -08003253
Joel Hockeyd4fca732019-09-20 16:57:03 -07003254 var divSize = hterm.getClientSize(lib.notNull(this.div_));
Robert Ginda97769282013-02-01 15:30:30 -08003255 var overlaySize = hterm.getClientSize(this.overlayNode_);
3256
Robert Ginda8a59f762014-07-23 11:29:55 -07003257 this.overlayNode_.style.top =
3258 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08003259 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07003260 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08003261
Mike Frysingerbdb34802020-04-07 03:47:32 -04003262 if (this.overlayTimeout_) {
rgindaf0090c92012-02-10 14:58:52 -08003263 clearTimeout(this.overlayTimeout_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003264 }
rgindaf0090c92012-02-10 14:58:52 -08003265
Raymes Khouryc7a06382018-07-04 10:25:45 +10003266 this.accessibilityReader_.assertiveAnnounce(msg);
3267
Mike Frysingerbdb34802020-04-07 03:47:32 -04003268 if (opt_timeout === null) {
rgindacc2996c2012-02-24 14:59:31 -08003269 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003270 }
rgindacc2996c2012-02-24 14:59:31 -08003271
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003272 this.overlayTimeout_ = setTimeout(() => {
3273 this.overlayNode_.style.opacity = '0';
3274 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
3275 }, opt_timeout || 1500);
3276};
3277
3278/**
3279 * Hide the terminal overlay immediately.
3280 *
3281 * Useful when we show an overlay for an event with an unknown end time.
3282 */
3283hterm.Terminal.prototype.hideOverlay = function() {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003284 if (this.overlayTimeout_) {
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003285 clearTimeout(this.overlayTimeout_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003286 }
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003287 this.overlayTimeout_ = null;
3288
Mike Frysingerbdb34802020-04-07 03:47:32 -04003289 if (this.overlayNode_.parentNode) {
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003290 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003291 }
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003292 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08003293};
3294
rginda4bba5e12012-06-20 16:15:30 -07003295/**
3296 * Paste from the system clipboard to the terminal.
Mike Frysinger23b5b832019-10-01 17:05:29 -04003297 *
Jason Lin17cc89f2020-03-19 10:48:45 +11003298 * Note: In Chrome, this should work unless the user has rejected the permission
3299 * request. In Firefox extension environment, you'll need the "clipboardRead"
3300 * permission. In other environments, this might always fail as the browser
3301 * frequently blocks access for security reasons.
3302 *
3303 * @return {?boolean} If nagivator.clipboard.readText is available, the return
3304 * value is always null. Otherwise, this function uses legacy pasting and
3305 * returns a boolean indicating whether it is successful.
rginda4bba5e12012-06-20 16:15:30 -07003306 */
3307hterm.Terminal.prototype.paste = function() {
Jason Linf129f3c2020-03-23 11:52:08 +11003308 if (!this.alwaysUseLegacyPasting &&
3309 navigator.clipboard && navigator.clipboard.readText) {
Jason Lin17cc89f2020-03-19 10:48:45 +11003310 navigator.clipboard.readText().then((data) => this.onPasteData_(data));
3311 return null;
3312 } else {
3313 // Legacy pasting.
3314 try {
3315 return this.document_.execCommand('paste');
3316 } catch (firefoxException) {
3317 // Ignore this. FF 40 and older would incorrectly throw an exception if
3318 // there was an error instead of returning false.
3319 return false;
3320 }
3321 }
rginda4bba5e12012-06-20 16:15:30 -07003322};
3323
3324/**
3325 * Copy a string to the system clipboard.
3326 *
3327 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05003328 *
3329 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07003330 */
3331hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003332 if (this.prefs_.get('enable-clipboard-notice')) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08003333 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003334 }
Robert Ginda4e4d42c2014-03-04 14:07:23 -08003335
Mike Frysinger96eacae2019-01-02 18:13:56 -05003336 hterm.copySelectionToClipboard(this.document_, str);
rginda4bba5e12012-06-20 16:15:30 -07003337};
3338
Evan Jones2600d4f2016-12-06 09:29:36 -05003339/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003340 * Display an image.
3341 *
Mike Frysinger2558ed52019-01-14 01:03:41 -05003342 * Either URI or buffer or blob fields must be specified.
3343 *
Joel Hockey0f933582019-08-27 18:01:51 -07003344 * @param {{
3345 * name: (string|undefined),
3346 * size: (string|number|undefined),
3347 * preserveAspectRation: (boolean|undefined),
3348 * inline: (boolean|undefined),
3349 * width: (string|number|undefined),
3350 * height: (string|number|undefined),
3351 * align: (string|undefined),
3352 * url: (string|undefined),
3353 * buffer: (!ArrayBuffer|undefined),
3354 * blob: (!Blob|undefined),
3355 * type: (string|undefined),
3356 * }} options The image to display.
3357 * name A human readable string for the image
3358 * size The size (in bytes).
3359 * preserveAspectRatio Whether to preserve aspect.
3360 * inline Whether to display the image inline.
3361 * width The width of the image.
3362 * height The height of the image.
3363 * align Direction to align the image.
3364 * uri The source URI for the image.
3365 * buffer The ArrayBuffer image data.
3366 * blob The Blob image data.
3367 * type The MIME type of the image data.
3368 * @param {function()=} onLoad Callback when loading finishes.
3369 * @param {function(!Event)=} onError Callback when loading fails.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003370 */
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003371hterm.Terminal.prototype.displayImage = function(options, onLoad, onError) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003372 // Make sure we're actually given a resource to display.
Mike Frysinger2558ed52019-01-14 01:03:41 -05003373 if (options.uri === undefined && options.buffer === undefined &&
Mike Frysingerbdb34802020-04-07 03:47:32 -04003374 options.blob === undefined) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003375 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003376 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003377
3378 // Set up the defaults to simplify code below.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003379 if (!options.name) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003380 options.name = '';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003381 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003382
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003383 // See if the mime type is available. If not, guess from the filename.
3384 // We don't list all possible mime types because the browser can usually
3385 // guess it correctly. So list the ones that need a bit more help.
3386 if (!options.type) {
3387 const ary = options.name.split('.');
3388 const ext = ary[ary.length - 1].trim();
3389 switch (ext) {
3390 case 'svg':
3391 case 'svgz':
3392 options.type = 'image/svg+xml';
3393 break;
3394 }
3395 }
3396
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003397 // Has the user approved image display yet?
3398 if (this.allowImagesInline !== true) {
3399 this.newLine();
3400 const row = this.getRowNode(this.scrollbackRows_.length +
3401 this.getCursorRow() - 1);
3402
3403 if (this.allowImagesInline === false) {
3404 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
3405 'Inline Images Disabled');
3406 return;
3407 }
3408
3409 // Show a prompt.
3410 let button;
3411 const span = this.document_.createElement('span');
3412 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
3413 span.style.fontWeight = 'bold';
3414 span.style.borderWidth = '1px';
3415 span.style.borderStyle = 'dashed';
3416 button = this.document_.createElement('span');
3417 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3418 button.style.marginLeft = '1em';
3419 button.style.borderWidth = '1px';
3420 button.style.borderStyle = 'solid';
3421 button.addEventListener('click', () => {
3422 this.prefs_.set('allow-images-inline', false);
3423 });
3424 span.appendChild(button);
3425 button = this.document_.createElement('span');
3426 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3427 'allow this session');
3428 button.style.marginLeft = '1em';
3429 button.style.borderWidth = '1px';
3430 button.style.borderStyle = 'solid';
3431 button.addEventListener('click', () => {
3432 this.allowImagesInline = true;
3433 });
3434 span.appendChild(button);
3435 button = this.document_.createElement('span');
3436 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3437 button.style.marginLeft = '1em';
3438 button.style.borderWidth = '1px';
3439 button.style.borderStyle = 'solid';
3440 button.addEventListener('click', () => {
3441 this.prefs_.set('allow-images-inline', true);
3442 });
3443 span.appendChild(button);
3444
3445 row.appendChild(span);
3446 return;
3447 }
3448
3449 // See if we should show this object directly, or download it.
3450 if (options.inline) {
3451 const io = this.io.push();
3452 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
Joel Hockeyd4fca732019-09-20 16:57:03 -07003453 'Loading $1 ...'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003454
3455 // While we're loading the image, eat all the user's input.
3456 io.onVTKeystroke = io.sendString = () => {};
3457
3458 // Initialize this new image.
Joel Hockeyd4fca732019-09-20 16:57:03 -07003459 const img = this.document_.createElement('img');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003460 if (options.uri !== undefined) {
3461 img.src = options.uri;
3462 } else if (options.buffer !== undefined) {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003463 const blob = new Blob([options.buffer], {type: options.type});
Mike Frysinger2558ed52019-01-14 01:03:41 -05003464 img.src = URL.createObjectURL(blob);
3465 } else {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003466 const blob = new Blob([options.blob], {type: options.type});
Joel Hockeyd4fca732019-09-20 16:57:03 -07003467 img.src = URL.createObjectURL(blob);
Mike Frysinger2558ed52019-01-14 01:03:41 -05003468 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003469 img.title = img.alt = options.name;
3470
3471 // Attach the image to the page to let it load/render. It won't stay here.
3472 // This is needed so it's visible and the DOM can calculate the height. If
3473 // the image is hidden or not in the DOM, the height is always 0.
3474 this.document_.body.appendChild(img);
3475
3476 // Wait for the image to finish loading before we try moving it to the
3477 // right place in the terminal.
3478 img.onload = () => {
3479 // Now that we have the image dimensions, figure out how to show it.
3480 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
3481 img.style.maxWidth = `${this.document_.body.clientWidth}px`;
3482 img.style.maxHeight = `${this.document_.body.clientHeight}px`;
3483
3484 // Parse a width/height specification.
3485 const parseDim = (dim, maxDim, cssVar) => {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003486 if (!dim || dim == 'auto') {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003487 return '';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003488 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003489
3490 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3491 if (ary) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003492 if (ary[2] == '%') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003493 return Math.floor(maxDim * ary[1] / 100) + 'px';
Mike Frysingerbdb34802020-04-07 03:47:32 -04003494 } else if (ary[2] == 'px') {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003495 return dim;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003496 } else {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003497 return `calc(${dim} * var(${cssVar}))`;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003498 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003499 }
3500
3501 return '';
3502 };
3503 img.style.width =
3504 parseDim(options.width, this.document_.body.clientWidth,
3505 '--hterm-charsize-width');
3506 img.style.height =
3507 parseDim(options.height, this.document_.body.clientHeight,
3508 '--hterm-charsize-height');
3509
3510 // Figure out how many rows the image occupies, then add that many.
Mike Frysingera0349392019-09-11 05:38:09 -04003511 // Note: This count will be inaccurate if the font size changes on us.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003512 const padRows = Math.ceil(img.clientHeight /
3513 this.scrollPort_.characterSize.height);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003514 for (let i = 0; i < padRows; ++i) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003515 this.newLine();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003516 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003517
3518 // Update the max height in case the user shrinks the character size.
3519 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3520
3521 // Move the image to the last row. This way when we scroll up, it doesn't
3522 // disappear when the first row gets clipped. It will disappear when we
3523 // scroll down and the last row is clipped ...
3524 this.document_.body.removeChild(img);
3525 // Create a wrapper node so we can do an absolute in a relative position.
3526 // This helps with rounding errors between JS & CSS counts.
3527 const div = this.document_.createElement('div');
3528 div.style.position = 'relative';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003529 div.style.textAlign = options.align || '';
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003530 img.style.position = 'absolute';
3531 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3532 div.appendChild(img);
3533 const row = this.getRowNode(this.scrollbackRows_.length +
3534 this.getCursorRow() - 1);
3535 row.appendChild(div);
3536
Mike Frysinger2558ed52019-01-14 01:03:41 -05003537 // Now that the image has been read, we can revoke the source.
3538 if (options.uri === undefined) {
3539 URL.revokeObjectURL(img.src);
3540 }
3541
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003542 io.hideOverlay();
3543 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003544
Mike Frysingerbdb34802020-04-07 03:47:32 -04003545 if (onLoad) {
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003546 onLoad();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003547 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003548 };
3549
3550 // If we got a malformed image, give up.
3551 img.onerror = (e) => {
3552 this.document_.body.removeChild(img);
3553 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
Mike Frysingere14a8c42018-03-10 00:17:30 -08003554 'Loading $1 failed'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003555 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003556
Mike Frysingerbdb34802020-04-07 03:47:32 -04003557 if (onError) {
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003558 onError(e);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003559 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003560 };
3561 } else {
3562 // We can't use chrome.downloads.download as that requires "downloads"
3563 // permissions, and that works only in extensions, not apps.
3564 const a = this.document_.createElement('a');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003565 if (options.uri !== undefined) {
3566 a.href = options.uri;
3567 } else if (options.buffer !== undefined) {
3568 const blob = new Blob([options.buffer]);
3569 a.href = URL.createObjectURL(blob);
3570 } else {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003571 a.href = URL.createObjectURL(lib.notNull(options.blob));
Mike Frysinger2558ed52019-01-14 01:03:41 -05003572 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003573 a.download = options.name;
3574 this.document_.body.appendChild(a);
3575 a.click();
3576 a.remove();
Mike Frysinger2558ed52019-01-14 01:03:41 -05003577 if (options.uri === undefined) {
3578 URL.revokeObjectURL(a.href);
3579 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003580 }
3581};
3582
3583/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003584 * Returns the selected text, or null if no text is selected.
3585 *
3586 * @return {string|null}
3587 */
rgindaa09e7332012-08-17 12:49:51 -07003588hterm.Terminal.prototype.getSelectionText = function() {
3589 var selection = this.scrollPort_.selection;
3590 selection.sync();
3591
Mike Frysingerbdb34802020-04-07 03:47:32 -04003592 if (selection.isCollapsed) {
rgindaa09e7332012-08-17 12:49:51 -07003593 return null;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003594 }
rgindaa09e7332012-08-17 12:49:51 -07003595
rgindaa09e7332012-08-17 12:49:51 -07003596 // Start offset measures from the beginning of the line.
3597 var startOffset = selection.startOffset;
3598 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003599
Raymes Khoury334625a2018-06-25 10:29:40 +10003600 // If an x-row isn't selected, |node| will be null.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003601 if (!node) {
Raymes Khoury334625a2018-06-25 10:29:40 +10003602 return null;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003603 }
Raymes Khoury334625a2018-06-25 10:29:40 +10003604
Robert Gindafdbb3f22012-09-06 20:23:06 -07003605 if (node.nodeName != 'X-ROW') {
3606 // If the selection doesn't start on an x-row node, then it must be
3607 // somewhere inside the x-row. Add any characters from previous siblings
3608 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003609
3610 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3611 // If node is the text node in a styled span, move up to the span node.
3612 node = node.parentNode;
3613 }
3614
Robert Gindafdbb3f22012-09-06 20:23:06 -07003615 while (node.previousSibling) {
3616 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003617 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003618 }
rgindaa09e7332012-08-17 12:49:51 -07003619 }
3620
3621 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08003622 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
3623 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003624 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003625
Robert Gindafdbb3f22012-09-06 20:23:06 -07003626 if (node.nodeName != 'X-ROW') {
3627 // If the selection doesn't end on an x-row node, then it must be
3628 // somewhere inside the x-row. Add any characters from following siblings
3629 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003630
3631 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3632 // If node is the text node in a styled span, move up to the span node.
3633 node = node.parentNode;
3634 }
3635
Robert Gindafdbb3f22012-09-06 20:23:06 -07003636 while (node.nextSibling) {
3637 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003638 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003639 }
rgindaa09e7332012-08-17 12:49:51 -07003640 }
3641
3642 var rv = this.getRowsText(selection.startRow.rowIndex,
3643 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003644 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003645};
3646
rginda4bba5e12012-06-20 16:15:30 -07003647/**
3648 * Copy the current selection to the system clipboard, then clear it after a
3649 * short delay.
3650 */
3651hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07003652 var text = this.getSelectionText();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003653 if (text != null) {
rgindaa09e7332012-08-17 12:49:51 -07003654 this.copyStringToClipboard(text);
Mike Frysingerbdb34802020-04-07 03:47:32 -04003655 }
rginda4bba5e12012-06-20 16:15:30 -07003656};
3657
Joel Hockey0f933582019-08-27 18:01:51 -07003658/**
3659 * Show overlay with current terminal size.
3660 */
rgindaf0090c92012-02-10 14:58:52 -08003661hterm.Terminal.prototype.overlaySize = function() {
Theodore Duboisdd5f9a72019-09-06 23:28:42 -07003662 if (this.prefs_.get('enable-resize-status')) {
3663 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
3664 }
rgindaf0090c92012-02-10 14:58:52 -08003665};
3666
rginda87b86462011-12-14 13:48:03 -08003667/**
3668 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3669 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003670 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003671 */
3672hterm.Terminal.prototype.onVTKeystroke = function(string) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003673 if (this.scrollOnKeystroke_) {
rginda87b86462011-12-14 13:48:03 -08003674 this.scrollPort_.scrollRowToBottom(this.getRowCount());
Mike Frysingerbdb34802020-04-07 03:47:32 -04003675 }
rginda87b86462011-12-14 13:48:03 -08003676
Mike Frysinger225c99d2019-10-20 14:02:37 -06003677 this.pauseCursorBlink_();
3678
Mike Frysinger79669762018-12-30 20:51:10 -05003679 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08003680};
3681
3682/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003683 * Open the selected url.
3684 */
3685hterm.Terminal.prototype.openSelectedUrl_ = function() {
3686 var str = this.getSelectionText();
3687
3688 // If there is no selection, try and expand wherever they clicked.
3689 if (str == null) {
John Lincae9b732018-03-08 13:56:35 +08003690 this.screen_.expandSelectionForUrl(this.document_.getSelection());
Mike Frysinger70b94692017-01-26 18:57:50 -10003691 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003692
3693 // If clicking in empty space, return.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003694 if (str == null) {
Mike Frysinger498192d2017-06-26 18:23:31 -04003695 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003696 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003697 }
3698
3699 // Make sure URL is valid before opening.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003700 if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003701 return;
Mike Frysingerbdb34802020-04-07 03:47:32 -04003702 }
Mike Frysinger43472622017-06-26 18:11:07 -04003703
3704 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003705 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003706 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3707 // We have to whitelist a few protocols that lack authorities and thus
3708 // never use the //. Like mailto.
3709 switch (str.split(':', 1)[0]) {
3710 case 'mailto':
3711 break;
3712 default:
3713 str = 'http://' + str;
3714 break;
3715 }
3716 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003717
Mike Frysinger720fa832017-10-23 01:15:52 -04003718 hterm.openUrl(str);
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003719};
Mike Frysinger70b94692017-01-26 18:57:50 -10003720
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003721/**
3722 * Manage the automatic mouse hiding behavior while typing.
3723 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003724 * @param {?boolean=} v Whether to enable automatic hiding.
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003725 */
3726hterm.Terminal.prototype.setAutomaticMouseHiding = function(v=null) {
3727 // Since Chrome OS & macOS do this by default everywhere, we don't need to.
3728 // Linux & Windows seem to leave this to specific applications to manage.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003729 if (v === null) {
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003730 v = (hterm.os != 'cros' && hterm.os != 'mac');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003731 }
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003732
3733 this.mouseHideWhileTyping_ = !!v;
3734};
3735
3736/**
3737 * Handler for monitoring user keyboard activity.
3738 *
3739 * This isn't for processing the keystrokes directly, but for updating any
3740 * state that might toggle based on the user using the keyboard at all.
3741 *
Joel Hockey0f933582019-08-27 18:01:51 -07003742 * @param {!KeyboardEvent} e The keyboard event that triggered us.
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003743 */
3744hterm.Terminal.prototype.onKeyboardActivity_ = function(e) {
3745 // When the user starts typing, hide the mouse cursor.
Mike Frysingerbdb34802020-04-07 03:47:32 -04003746 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003747 this.setCssVar('mouse-cursor-style', 'none');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003748 }
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003749};
Mike Frysinger70b94692017-01-26 18:57:50 -10003750
3751/**
rgindad5613292012-06-19 15:40:37 -07003752 * Add the terminalRow and terminalColumn properties to mouse events and
3753 * then forward on to onMouse().
3754 *
3755 * The terminalRow and terminalColumn properties contain the (row, column)
3756 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003757 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003758 * @param {!MouseEvent} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003759 */
3760hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003761 if (e.processedByTerminalHandler_) {
3762 // We register our event handlers on the document, as well as the cursor
3763 // and the scroll blocker. Mouse events that occur on the cursor or
3764 // scroll blocker will also appear on the document, but we don't want to
3765 // process them twice.
3766 //
3767 // We can't just prevent bubbling because that has other side effects, so
3768 // we decorate the event object with this property instead.
3769 return;
3770 }
3771
Mike Frysinger468966c2018-08-28 13:48:51 -04003772 // Consume navigation events. Button 3 is usually "browser back" and
3773 // button 4 is "browser forward" which we don't want to happen.
3774 if (e.button > 2) {
3775 e.preventDefault();
3776 // We don't return so click events can be passed to the remote below.
3777 }
3778
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003779 var reportMouseEvents = (!this.defeatMouseReports_ &&
3780 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3781
rgindafaa74742012-08-21 13:34:03 -07003782 e.processedByTerminalHandler_ = true;
3783
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003784 // Handle auto hiding of mouse cursor while typing.
3785 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
3786 // Make sure the mouse cursor is visible.
3787 this.syncMouseStyle();
3788 // This debounce isn't perfect, but should work well enough for such a
3789 // simple implementation. If the user moved the mouse, we enabled this
3790 // debounce, and then moved the mouse just before the timeout, we wouldn't
3791 // debounce that later movement.
3792 this.mouseHideDelay_ = setTimeout(() => this.mouseHideDelay_ = null, 1000);
3793 }
3794
Robert Gindaeda48db2014-07-17 09:25:30 -07003795 // One based row/column stored on the mouse event.
Joel Hockeyd4fca732019-09-20 16:57:03 -07003796 e.terminalRow = Math.floor(
3797 (e.clientY - this.scrollPort_.visibleRowTopMargin) /
3798 this.scrollPort_.characterSize.height) + 1;
3799 e.terminalColumn = Math.floor(
3800 e.clientX / this.scrollPort_.characterSize.width) + 1;
Robert Gindaeda48db2014-07-17 09:25:30 -07003801
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003802 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
3803 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07003804 return;
3805 }
3806
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003807 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07003808 // If the cursor is visible and we're not sending mouse events to the
3809 // host app, then we want to hide the terminal cursor when the mouse
3810 // cursor is over top. This keeps the terminal cursor from interfering
3811 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003812 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3813 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3814 this.cursorNode_.style.display = 'none';
3815 } else if (this.cursorNode_.style.display == 'none') {
3816 this.cursorNode_.style.display = '';
3817 }
3818 }
rgindad5613292012-06-19 15:40:37 -07003819
Robert Ginda928cf632014-03-05 15:07:41 -08003820 if (e.type == 'mousedown') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003821 this.contextMenu.hide();
Mike Frysingercc114512017-09-11 21:39:17 -04003822
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003823 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003824 // If VT mouse reporting is disabled, or has been defeated with
3825 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003826 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003827 this.setSelectionEnabled(true);
3828 } else {
3829 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003830 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003831 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003832 this.setSelectionEnabled(false);
3833 e.preventDefault();
3834 }
3835 }
3836
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003837 if (!reportMouseEvents) {
John Lin2aad22e2018-03-16 13:58:11 +08003838 if (e.type == 'dblclick') {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003839 this.screen_.expandSelection(this.document_.getSelection());
Mike Frysingerbdb34802020-04-07 03:47:32 -04003840 if (this.copyOnSelect) {
Mike Frysinger406c76c2019-01-02 17:53:51 -05003841 this.copySelectionToClipboard();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003842 }
rgindad5613292012-06-19 15:40:37 -07003843 }
3844
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003845 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003846 // Debounce this event with the dblclick event. If you try to doubleclick
3847 // a URL to open it, Chrome will fire click then dblclick, but we won't
3848 // have expanded the selection text at the first click event.
3849 clearTimeout(this.timeouts_.openUrl);
3850 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3851 500);
3852 return;
3853 }
3854
Mike Frysinger847577f2017-05-23 23:25:57 -04003855 if (e.type == 'mousedown') {
Mike Frysingercc114512017-09-11 21:39:17 -04003856 if (e.ctrlKey && e.button == 2 /* right button */) {
3857 e.preventDefault();
3858 this.contextMenu.show(e, this);
3859 } else if (e.button == this.mousePasteButton ||
3860 (this.mouseRightClickPaste && e.button == 2 /* right button */)) {
Mike Frysingerbdb34802020-04-07 03:47:32 -04003861 if (this.paste() === false) {
Mike Frysinger05a57f02017-08-27 17:48:55 -04003862 console.warn('Could not paste manually due to web restrictions');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003863 }
Mike Frysinger847577f2017-05-23 23:25:57 -04003864 }
3865 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003866
Mike Frysinger2edd3612017-05-24 00:54:39 -04003867 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003868 !this.document_.getSelection().isCollapsed) {
Mike Frysinger406c76c2019-01-02 17:53:51 -05003869 this.copySelectionToClipboard();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003870 }
3871
3872 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3873 this.scrollBlockerNode_.engaged) {
3874 // Disengage the scroll-blocker after one of these events.
3875 this.scrollBlockerNode_.engaged = false;
3876 this.scrollBlockerNode_.style.top = '-99px';
3877 }
3878
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003879 // Emulate arrow key presses via scroll wheel events.
3880 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3881 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003882 if (e.type == 'wheel') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003883 const delta =
3884 this.scrollPort_.scrollWheelDelta(/** @type {!WheelEvent} */ (e));
Mike Frysingerc3030a82017-05-29 14:16:11 -04003885
Mike Frysinger321063c2018-08-29 15:33:14 -04003886 // Helper to turn a wheel event delta into a series of key presses.
3887 const deltaToArrows = (distance, charSize, arrowPos, arrowNeg) => {
3888 if (distance == 0) {
3889 return '';
3890 }
3891
3892 // Convert the scroll distance into a number of rows/cols.
3893 const cells = lib.f.smartFloorDivide(Math.abs(distance), charSize);
3894 const data = '\x1bO' + (distance < 0 ? arrowNeg : arrowPos);
3895 return data.repeat(cells);
3896 };
3897
3898 // The order between up/down and left/right doesn't really matter.
3899 this.io.sendString(
3900 // Up/down arrow keys.
3901 deltaToArrows(delta.y, this.scrollPort_.characterSize.height,
3902 'A', 'B') +
3903 // Left/right arrow keys.
3904 deltaToArrows(delta.x, this.scrollPort_.characterSize.width,
3905 'C', 'D')
3906 );
Mike Frysingerc3030a82017-05-29 14:16:11 -04003907
3908 e.preventDefault();
3909 }
3910 }
Robert Ginda928cf632014-03-05 15:07:41 -08003911 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003912 if (!this.scrollBlockerNode_.engaged) {
3913 if (e.type == 'mousedown') {
3914 // Move the scroll-blocker into place if we want to keep the scrollport
3915 // from scrolling.
3916 this.scrollBlockerNode_.engaged = true;
3917 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3918 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3919 } else if (e.type == 'mousemove') {
3920 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3921 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003922 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003923 e.preventDefault();
3924 }
3925 }
Robert Ginda928cf632014-03-05 15:07:41 -08003926
3927 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003928 }
3929
Robert Ginda928cf632014-03-05 15:07:41 -08003930 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3931 // Restore this on mouseup in case it was temporarily defeated with a
3932 // alt-mousedown. Only do this when the selection is empty so that
3933 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003934 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003935 }
rgindad5613292012-06-19 15:40:37 -07003936};
3937
3938/**
3939 * Clients should override this if they care to know about mouse events.
3940 *
3941 * The event parameter will be a normal DOM mouse click event with additional
3942 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003943 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003944 * @param {!MouseEvent} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003945 */
3946hterm.Terminal.prototype.onMouse = function(e) { };
3947
3948/**
rginda8e92a692012-05-20 19:37:20 -07003949 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003950 *
3951 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003952 */
Rob Spies06533ba2014-04-24 11:20:37 -07003953hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3954 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003955 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003956
Mike Frysingerbdb34802020-04-07 03:47:32 -04003957 if (this.reportFocus) {
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003958 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O');
Mike Frysingerbdb34802020-04-07 03:47:32 -04003959 }
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003960
Mike Frysingerbdb34802020-04-07 03:47:32 -04003961 if (focused === true) {
Michael Kelly485ecd12014-06-09 11:41:56 -04003962 this.closeBellNotifications_();
Mike Frysingerbdb34802020-04-07 03:47:32 -04003963 }
rginda8e92a692012-05-20 19:37:20 -07003964};
3965
3966/**
rginda8ba33642011-12-14 12:31:31 -08003967 * React when the ScrollPort is scrolled.
3968 */
3969hterm.Terminal.prototype.onScroll_ = function() {
3970 this.scheduleSyncCursorPosition_();
3971};
3972
3973/**
rginda9846e2f2012-01-27 13:53:33 -08003974 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003975 *
Joel Hockeye25ce432019-09-25 19:12:28 -07003976 * @param {{text: string}} e The text of the paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08003977 */
3978hterm.Terminal.prototype.onPaste_ = function(e) {
Jason Lin17cc89f2020-03-19 10:48:45 +11003979 this.onPasteData_(e.text);
3980};
3981
3982/**
3983 * Handle pasted data.
3984 *
3985 * @param {string} data The pasted data.
3986 */
3987hterm.Terminal.prototype.onPasteData_ = function(data) {
3988 data = data.replace(/\n/mg, '\r');
Mike Frysingere8c32c82018-03-11 14:57:28 -07003989 if (this.options_.bracketedPaste) {
3990 // We strip out most escape sequences as they can cause issues (like
3991 // inserting an \x1b[201~ midstream). We pass through whitespace
3992 // though: 0x08:\b 0x09:\t 0x0a:\n 0x0d:\r.
3993 // This matches xterm behavior.
3994 const filter = (data) => data.replace(/[\x00-\x07\x0b-\x0c\x0e-\x1f]/g, '');
3995 data = '\x1b[200~' + filter(data) + '\x1b[201~';
3996 }
Robert Gindaa063b202014-07-21 11:08:25 -07003997
3998 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003999};
4000
4001/**
rgindaa09e7332012-08-17 12:49:51 -07004002 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05004003 *
Joel Hockey0f933582019-08-27 18:01:51 -07004004 * @param {!Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07004005 */
4006hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07004007 if (!this.useDefaultWindowCopy) {
4008 e.preventDefault();
4009 setTimeout(this.copySelectionToClipboard.bind(this), 0);
4010 }
rgindaa09e7332012-08-17 12:49:51 -07004011};
4012
4013/**
rginda8ba33642011-12-14 12:31:31 -08004014 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08004015 *
4016 * Note: This function should not directly contain code that alters the internal
4017 * state of the terminal. That kind of code belongs in realizeWidth or
4018 * realizeHeight, so that it can be executed synchronously in the case of a
4019 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08004020 */
4021hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08004022 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07004023 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08004024 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07004025 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08004026
Robert Ginda4e83f3a2012-09-04 15:25:25 -07004027 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08004028 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07004029 // gets removed from the document or during the initial load, and we can't
4030 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07004031 // This can also happen if called before the scrollPort calculates the
4032 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08004033 return;
4034 }
4035
rgindaa8ba17d2012-08-15 14:41:10 -07004036 var isNewSize = (columnCount != this.screenSize.width ||
4037 rowCount != this.screenSize.height);
Theodore Dubois651b0842019-09-07 14:32:09 -07004038 const wasScrolledEnd = this.scrollPort_.isScrolledEnd;
rgindaa8ba17d2012-08-15 14:41:10 -07004039
4040 // We do this even if the size didn't change, just to be sure everything is
4041 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04004042 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07004043 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07004044
Mike Frysingerbdb34802020-04-07 03:47:32 -04004045 if (isNewSize) {
rgindaa8ba17d2012-08-15 14:41:10 -07004046 this.overlaySize();
Mike Frysingerbdb34802020-04-07 03:47:32 -04004047 }
rgindaa8ba17d2012-08-15 14:41:10 -07004048
Robert Gindafb1be6a2013-12-11 11:56:22 -08004049 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07004050 this.scheduleSyncCursorPosition_();
Theodore Dubois651b0842019-09-07 14:32:09 -07004051
4052 if (wasScrolledEnd) {
4053 this.scrollEnd();
4054 }
rginda8ba33642011-12-14 12:31:31 -08004055};
4056
4057/**
4058 * Service the cursor blink timeout.
4059 */
4060hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07004061 if (!this.options_.cursorBlink) {
4062 delete this.timeouts_.cursorBlink;
4063 return;
4064 }
4065
Robert Ginda830583c2013-08-07 13:20:46 -07004066 if (this.cursorNode_.getAttribute('focus') == 'false' ||
Mike Frysinger225c99d2019-10-20 14:02:37 -06004067 this.cursorNode_.style.opacity == '0' ||
4068 this.cursorBlinkPause_) {
rginda87b86462011-12-14 13:48:03 -08004069 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07004070 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
4071 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08004072 } else {
rginda87b86462011-12-14 13:48:03 -08004073 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07004074 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
4075 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08004076 }
4077};
David Reveman8f552492012-03-28 12:18:41 -04004078
4079/**
4080 * Set the scrollbar-visible mode bit.
4081 *
4082 * If scrollbar-visible is on, the vertical scrollbar will be visible.
4083 * Otherwise it will not.
4084 *
4085 * Defaults to on.
4086 *
4087 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
4088 */
4089hterm.Terminal.prototype.setScrollbarVisible = function(state) {
4090 this.scrollPort_.setScrollbarVisible(state);
4091};
Michael Kelly485ecd12014-06-09 11:41:56 -04004092
4093/**
Rob Spies49039e52014-12-17 13:40:04 -08004094 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04004095 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08004096 *
4097 * Defaults to 1.
4098 *
Evan Jones2600d4f2016-12-06 09:29:36 -05004099 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08004100 */
4101hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
4102 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
4103};
4104
4105/**
Michael Kelly485ecd12014-06-09 11:41:56 -04004106 * Close all web notifications created by terminal bells.
4107 */
4108hterm.Terminal.prototype.closeBellNotifications_ = function() {
4109 this.bellNotificationList_.forEach(function(n) {
4110 n.close();
4111 });
4112 this.bellNotificationList_.length = 0;
4113};
Raymes Khourye5d48982018-08-02 09:08:32 +10004114
4115/**
4116 * Syncs the cursor position when the scrollport gains focus.
4117 */
4118hterm.Terminal.prototype.onScrollportFocus_ = function() {
4119 // If the cursor is offscreen we set selection to the last row on the screen.
4120 const topRowIndex = this.scrollPort_.getTopRowIndex();
4121 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
4122 const selection = this.document_.getSelection();
4123 if (!this.syncCursorPosition_() && selection) {
4124 selection.collapse(this.getRowNode(bottomRowIndex));
4125 }
4126};
Joel Hockey3e5aed82020-04-01 18:30:05 -07004127
4128/**
4129 * Clients can override this if they want to provide an options page.
4130 */
4131hterm.Terminal.prototype.onOpenOptionsPage = function() {};
4132
4133
4134/**
4135 * Called when user selects to open the options page.
4136 */
4137hterm.Terminal.prototype.onOpenOptionsPage_ = function() {
4138 this.onOpenOptionsPage();
4139};