blob: 22c9f44a142f206f9aea69662ed86cb6cd0a2afa [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));
rgindaa09e7332012-08-17 12:49:51 -070054 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080055
rginda87b86462011-12-14 13:48:03 -080056 // The div that contains this terminal.
57 this.div_ = null;
58
rgindac9bc5502012-01-18 11:48:44 -080059 // The document that contains the scrollPort. Defaulted to the global
60 // document here so that the terminal is functional even if it hasn't been
61 // inserted into a document yet, but re-set in decorate().
62 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080063
rginda8ba33642011-12-14 12:31:31 -080064 // The rows that have scrolled off screen and are no longer addressable.
65 this.scrollbackRows_ = [];
66
rgindac9bc5502012-01-18 11:48:44 -080067 // Saved tab stops.
68 this.tabStops_ = [];
69
David Benjamin66e954d2012-05-05 21:08:12 -040070 // Keep track of whether default tab stops have been erased; after a TBC
71 // clears all tab stops, defaults aren't restored on resize until a reset.
72 this.defaultTabStops = true;
73
rginda8ba33642011-12-14 12:31:31 -080074 // The VT's notion of the top and bottom rows. Used during some VT
75 // cursor positioning and scrolling commands.
76 this.vtScrollTop_ = null;
77 this.vtScrollBottom_ = null;
78
79 // The DIV element for the visible cursor.
80 this.cursorNode_ = null;
81
Robert Ginda830583c2013-08-07 13:20:46 -070082 // The current cursor shape of the terminal.
83 this.cursorShape_ = hterm.Terminal.cursorShape.BLOCK;
84
Robert Gindaea2183e2014-07-17 09:51:51 -070085 // Cursor blink on/off cycle in ms, overwritten by prefs once they're loaded.
86 this.cursorBlinkCycle_ = [100, 100];
87
Mike Frysinger225c99d2019-10-20 14:02:37 -060088 // Whether to temporarily disable blinking.
89 this.cursorBlinkPause_ = false;
90
Robert Gindaea2183e2014-07-17 09:51:51 -070091 // Pre-bound onCursorBlink_ handler, so we don't have to do this for each
92 // cursor on/off servicing.
93 this.myOnCursorBlink_ = this.onCursorBlink_.bind(this);
94
rginda9f5222b2012-03-05 11:53:28 -080095 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -070096 // each output and keystroke. They are initialized by the preference manager.
Joel Hockey8ff48232019-09-24 13:15:17 -070097 /** @type {string} */
98 this.backgroundColor_ = '';
99 /** @type {string} */
100 this.foregroundColor_ = '';
Robert Ginda57f03b42012-09-13 11:02:48 -0700101 this.scrollOnOutput_ = null;
102 this.scrollOnKeystroke_ = null;
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400103 this.scrollWheelArrowKeys_ = null;
rginda9f5222b2012-03-05 11:53:28 -0800104
Robert Ginda6aec7eb2015-06-16 10:31:30 -0700105 // True if we should override mouse event reporting to allow local selection.
106 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -0800107
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400108 // Whether to auto hide the mouse cursor when typing.
109 this.setAutomaticMouseHiding();
110 // Timer to keep mouse visible while it's being used.
111 this.mouseHideDelay_ = null;
112
rgindaf0090c92012-02-10 14:58:52 -0800113 // Terminal bell sound.
114 this.bellAudio_ = this.document_.createElement('audio');
Mike Frysingerd826f1a2017-07-06 16:20:06 -0400115 this.bellAudio_.id = 'hterm:bell-audio';
rgindaf0090c92012-02-10 14:58:52 -0800116 this.bellAudio_.setAttribute('preload', 'auto');
117
Raymes Khoury3e44bc92018-05-17 10:54:23 +1000118 // The AccessibilityReader object for announcing command output.
119 this.accessibilityReader_ = null;
120
Mike Frysingercc114512017-09-11 21:39:17 -0400121 // The context menu object.
122 this.contextMenu = new hterm.ContextMenu();
123
Michael Kelly485ecd12014-06-09 11:41:56 -0400124 // All terminal bell notifications that have been generated (not necessarily
125 // shown).
126 this.bellNotificationList_ = [];
Joel Hockeyd4fca732019-09-20 16:57:03 -0700127 this.bellSquelchTimeout_ = null;
Michael Kelly485ecd12014-06-09 11:41:56 -0400128
129 // Whether we have permission to display notifications.
130 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400131
rginda6d397402012-01-17 10:58:29 -0800132 // Cursor position and attributes saved with DECSC.
133 this.savedOptions_ = {};
134
rginda8ba33642011-12-14 12:31:31 -0800135 // The current mode bits for the terminal.
136 this.options_ = new hterm.Options();
137
138 // Timeouts we might need to clear.
139 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800140
141 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800142 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800143
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800144 this.saveCursorAndState(true);
145
Zhu Qunying30d40712017-03-14 16:27:00 -0700146 // The keyboard handler.
rgindafeaf3142012-01-31 15:14:20 -0800147 this.keyboard = new hterm.Keyboard(this);
148
rginda87b86462011-12-14 13:48:03 -0800149 // General IO interface that can be given to third parties without exposing
150 // the entire terminal object.
151 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800152
rgindad5613292012-06-19 15:40:37 -0700153 // True if mouse-click-drag should scroll the terminal.
154 this.enableMouseDragScroll = true;
155
Robert Ginda57f03b42012-09-13 11:02:48 -0700156 this.copyOnSelect = null;
Mike Frysinger847577f2017-05-23 23:25:57 -0400157 this.mouseRightClickPaste = null;
rginda4bba5e12012-06-20 16:15:30 -0700158 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700159
Zhu Qunying30d40712017-03-14 16:27:00 -0700160 // Whether to use the default window copy behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700161 this.useDefaultWindowCopy = false;
162
163 this.clearSelectionAfterCopy = true;
164
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400165 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800166 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700167
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400168 // Whether we allow images to be shown.
169 this.allowImagesInline = null;
170
Gabriel Holodake8a09be2017-10-10 01:07:11 -0400171 this.reportFocus = false;
172
Joel Hockey3a44a442019-10-14 16:22:56 -0700173 this.setProfile(profileId || 'default',
Evan Jones5f9df812016-12-06 09:38:58 -0500174 function() { this.onTerminalReady(); }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800175};
176
177/**
Robert Ginda830583c2013-08-07 13:20:46 -0700178 * Possible cursor shapes.
179 */
180hterm.Terminal.cursorShape = {
181 BLOCK: 'BLOCK',
182 BEAM: 'BEAM',
183 UNDERLINE: 'UNDERLINE'
184};
185
186/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700187 * Clients should override this to be notified when the terminal is ready
188 * for use.
189 *
190 * The terminal initialization is asynchronous, and shouldn't be used before
191 * this method is called.
192 */
193hterm.Terminal.prototype.onTerminalReady = function() { };
194
195/**
rginda35c456b2012-02-09 17:29:05 -0800196 * Default tab with of 8 to match xterm.
197 */
198hterm.Terminal.prototype.tabWidth = 8;
199
200/**
rginda9f5222b2012-03-05 11:53:28 -0800201 * Select a preference profile.
202 *
203 * This will load the terminal preferences for the given profile name and
204 * associate subsequent preference changes with the new preference profile.
205 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500206 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800207 * characters will be removed from the name.
Joel Hockey0f933582019-08-27 18:01:51 -0700208 * @param {function()=} opt_callback Optional callback to invoke when the
209 * profile transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800210 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700211hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
212 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800213
Robert Ginda57f03b42012-09-13 11:02:48 -0700214 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800215
Robert Ginda57f03b42012-09-13 11:02:48 -0700216 if (this.prefs_)
217 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800218
Robert Ginda57f03b42012-09-13 11:02:48 -0700219 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
220 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800221 'alt-gr-mode': function(v) {
222 if (v == null) {
223 if (navigator.language.toLowerCase() == 'en-us') {
224 v = 'none';
225 } else {
226 v = 'right-alt';
227 }
228 } else if (typeof v == 'string') {
229 v = v.toLowerCase();
230 } else {
231 v = 'none';
232 }
233
234 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v))
235 v = 'none';
236
237 terminal.keyboard.altGrMode = v;
238 },
239
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700240 'alt-backspace-is-meta-backspace': function(v) {
241 terminal.keyboard.altBackspaceIsMetaBackspace = v;
242 },
243
Robert Ginda57f03b42012-09-13 11:02:48 -0700244 'alt-is-meta': function(v) {
245 terminal.keyboard.altIsMeta = v;
246 },
247
248 'alt-sends-what': function(v) {
249 if (!/^(escape|8-bit|browser-key)$/.test(v))
250 v = 'escape';
251
252 terminal.keyboard.altSendsWhat = v;
253 },
254
255 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800256 var ary = v.match(/^lib-resource:(\S+)/);
257 if (ary) {
258 terminal.bellAudio_.setAttribute('src',
259 lib.resource.getDataUrl(ary[1]));
260 } else {
261 terminal.bellAudio_.setAttribute('src', v);
262 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700263 },
264
Michael Kelly485ecd12014-06-09 11:41:56 -0400265 'desktop-notification-bell': function(v) {
266 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700267 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400268 Notification.permission === 'granted';
269 if (!terminal.desktopNotificationBell_) {
270 // Note: We don't call Notification.requestPermission here because
271 // Chrome requires the call be the result of a user action (such as an
272 // onclick handler), and pref listeners are run asynchronously.
273 //
274 // A way of working around this would be to display a dialog in the
275 // terminal with a "click-to-request-permission" button.
276 console.warn('desktop-notification-bell is true but we do not have ' +
277 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400278 }
279 } else {
280 terminal.desktopNotificationBell_ = false;
281 }
282 },
283
Robert Ginda57f03b42012-09-13 11:02:48 -0700284 'background-color': function(v) {
285 terminal.setBackgroundColor(v);
286 },
287
288 'background-image': function(v) {
289 terminal.scrollPort_.setBackgroundImage(v);
290 },
291
292 'background-size': function(v) {
293 terminal.scrollPort_.setBackgroundSize(v);
294 },
295
296 'background-position': function(v) {
297 terminal.scrollPort_.setBackgroundPosition(v);
298 },
299
300 'backspace-sends-backspace': function(v) {
301 terminal.keyboard.backspaceSendsBackspace = v;
302 },
303
Brad Town18654b62015-03-12 00:27:45 -0700304 'character-map-overrides': function(v) {
305 if (!(v == null || v instanceof Object)) {
306 console.warn('Preference character-map-modifications is not an ' +
307 'object: ' + v);
308 return;
309 }
310
Mike Frysinger095d4062017-06-14 00:29:48 -0700311 terminal.vt.characterMaps.reset();
312 terminal.vt.characterMaps.setOverrides(v);
Brad Town18654b62015-03-12 00:27:45 -0700313 },
314
Robert Ginda57f03b42012-09-13 11:02:48 -0700315 'cursor-blink': function(v) {
316 terminal.setCursorBlink(!!v);
317 },
318
Joel Hockey9d10ba12019-05-28 01:25:02 -0700319 'cursor-shape': function(v) {
320 terminal.setCursorShape(v);
321 },
322
Robert Gindaea2183e2014-07-17 09:51:51 -0700323 'cursor-blink-cycle': function(v) {
324 if (v instanceof Array &&
325 typeof v[0] == 'number' &&
326 typeof v[1] == 'number') {
327 terminal.cursorBlinkCycle_ = v;
328 } else if (typeof v == 'number') {
329 terminal.cursorBlinkCycle_ = [v, v];
330 } else {
331 // Fast blink indicates an error.
332 terminal.cursorBlinkCycle_ = [100, 100];
333 }
334 },
335
Robert Ginda57f03b42012-09-13 11:02:48 -0700336 'cursor-color': function(v) {
337 terminal.setCursorColor(v);
338 },
339
340 'color-palette-overrides': function(v) {
341 if (!(v == null || v instanceof Object || v instanceof Array)) {
342 console.warn('Preference color-palette-overrides is not an array or ' +
343 'object: ' + v);
344 return;
rginda9f5222b2012-03-05 11:53:28 -0800345 }
rginda9f5222b2012-03-05 11:53:28 -0800346
Robert Ginda57f03b42012-09-13 11:02:48 -0700347 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700348
Robert Ginda57f03b42012-09-13 11:02:48 -0700349 if (v) {
350 for (var key in v) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700351 var i = parseInt(key, 10);
Robert Ginda57f03b42012-09-13 11:02:48 -0700352 if (isNaN(i) || i < 0 || i > 255) {
353 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
354 continue;
355 }
356
357 if (v[i]) {
358 var rgb = lib.colors.normalizeCSS(v[i]);
359 if (rgb)
360 lib.colors.colorPalette[i] = rgb;
361 }
362 }
rginda30f20f62012-04-05 16:36:19 -0700363 }
rginda30f20f62012-04-05 16:36:19 -0700364
Evan Jones5f9df812016-12-06 09:38:58 -0500365 terminal.primaryScreen_.textAttributes.resetColorPalette();
Robert Ginda57f03b42012-09-13 11:02:48 -0700366 terminal.alternateScreen_.textAttributes.resetColorPalette();
367 },
rginda30f20f62012-04-05 16:36:19 -0700368
Robert Ginda57f03b42012-09-13 11:02:48 -0700369 'copy-on-select': function(v) {
370 terminal.copyOnSelect = !!v;
371 },
rginda9f5222b2012-03-05 11:53:28 -0800372
Rob Spies0bec09b2014-06-06 15:58:09 -0700373 'use-default-window-copy': function(v) {
374 terminal.useDefaultWindowCopy = !!v;
375 },
376
377 'clear-selection-after-copy': function(v) {
378 terminal.clearSelectionAfterCopy = !!v;
379 },
380
Robert Ginda7e5e9522014-03-14 12:23:58 -0700381 'ctrl-plus-minus-zero-zoom': function(v) {
382 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
383 },
384
Robert Gindafb5a3f92014-05-13 14:12:00 -0700385 'ctrl-c-copy': function(v) {
386 terminal.keyboard.ctrlCCopy = v;
387 },
388
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100389 'ctrl-v-paste': function(v) {
390 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700391 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100392 },
393
Cody Coljee-Gray7c6a0392018-10-25 13:18:28 -0700394 'paste-on-drop': function(v) {
395 terminal.scrollPort_.setPasteOnDrop(v);
396 },
397
Masaya Suzuki273aa982014-05-31 07:25:55 +0900398 'east-asian-ambiguous-as-two-column': function(v) {
399 lib.wc.regardCjkAmbiguous = v;
400 },
401
Robert Ginda57f03b42012-09-13 11:02:48 -0700402 'enable-8-bit-control': function(v) {
403 terminal.vt.enable8BitControl = !!v;
404 },
rginda30f20f62012-04-05 16:36:19 -0700405
Robert Ginda57f03b42012-09-13 11:02:48 -0700406 'enable-bold': function(v) {
407 terminal.syncBoldSafeState();
408 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400409
Robert Ginda3e278d72014-03-25 13:18:51 -0700410 'enable-bold-as-bright': function(v) {
411 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
412 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
413 },
414
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400415 'enable-blink': function(v) {
Mike Frysinger261597c2017-12-28 01:14:21 -0500416 terminal.setTextBlink(!!v);
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400417 },
418
Robert Ginda57f03b42012-09-13 11:02:48 -0700419 'enable-clipboard-write': function(v) {
420 terminal.vt.enableClipboardWrite = !!v;
421 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400422
Robert Ginda3755e752013-05-31 13:34:09 -0700423 'enable-dec12': function(v) {
424 terminal.vt.enableDec12 = !!v;
425 },
426
Mike Frysinger38f267d2018-09-07 02:50:59 -0400427 'enable-csi-j-3': function(v) {
428 terminal.vt.enableCsiJ3 = !!v;
429 },
430
Robert Ginda57f03b42012-09-13 11:02:48 -0700431 'font-family': function(v) {
432 terminal.syncFontFamily();
433 },
rginda30f20f62012-04-05 16:36:19 -0700434
Robert Ginda57f03b42012-09-13 11:02:48 -0700435 'font-size': function(v) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700436 v = parseInt(v, 10);
Mike Frysinger47853ac2017-12-14 00:44:10 -0500437 if (v <= 0) {
438 console.error(`Invalid font size: ${v}`);
439 return;
440 }
441
Robert Ginda57f03b42012-09-13 11:02:48 -0700442 terminal.setFontSize(v);
443 },
rginda9875d902012-08-20 16:21:57 -0700444
Robert Ginda57f03b42012-09-13 11:02:48 -0700445 'font-smoothing': function(v) {
446 terminal.syncFontFamily();
447 },
rgindade84e382012-04-20 15:39:31 -0700448
Robert Ginda57f03b42012-09-13 11:02:48 -0700449 'foreground-color': function(v) {
450 terminal.setForegroundColor(v);
451 },
rginda30f20f62012-04-05 16:36:19 -0700452
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400453 'hide-mouse-while-typing': function(v) {
454 terminal.setAutomaticMouseHiding(v);
455 },
456
Robert Ginda57f03b42012-09-13 11:02:48 -0700457 'home-keys-scroll': function(v) {
458 terminal.keyboard.homeKeysScroll = v;
459 },
rginda4bba5e12012-06-20 16:15:30 -0700460
Robert Gindaa8165692015-06-15 14:46:31 -0700461 'keybindings': function(v) {
462 terminal.keyboard.bindings.clear();
463
464 if (!v)
465 return;
466
467 if (!(v instanceof Object)) {
468 console.error('Error in keybindings preference: Expected object');
469 return;
470 }
471
472 try {
473 terminal.keyboard.bindings.addBindings(v);
474 } catch (ex) {
475 console.error('Error in keybindings preference: ' + ex);
476 }
477 },
478
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700479 'media-keys-are-fkeys': function(v) {
480 terminal.keyboard.mediaKeysAreFKeys = v;
481 },
482
Robert Ginda57f03b42012-09-13 11:02:48 -0700483 'meta-sends-escape': function(v) {
484 terminal.keyboard.metaSendsEscape = v;
485 },
rginda30f20f62012-04-05 16:36:19 -0700486
Mike Frysinger847577f2017-05-23 23:25:57 -0400487 'mouse-right-click-paste': function(v) {
488 terminal.mouseRightClickPaste = v;
489 },
490
Robert Ginda57f03b42012-09-13 11:02:48 -0700491 'mouse-paste-button': function(v) {
492 terminal.syncMousePasteButton();
493 },
rgindaa8ba17d2012-08-15 14:41:10 -0700494
Robert Gindae76aa9f2014-03-14 12:29:12 -0700495 'page-keys-scroll': function(v) {
496 terminal.keyboard.pageKeysScroll = v;
497 },
498
Robert Ginda40932892012-12-10 17:26:40 -0800499 'pass-alt-number': function(v) {
500 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800501 // Let Alt-1..9 pass to the browser (to control tab switching) on
502 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500503 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800504 }
505
506 terminal.passAltNumber = v;
507 },
508
509 'pass-ctrl-number': function(v) {
510 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800511 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
512 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500513 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800514 }
515
516 terminal.passCtrlNumber = v;
517 },
518
519 'pass-meta-number': function(v) {
520 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800521 // Let Meta-1..9 pass to the browser (to control tab switching) on
522 // OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500523 v = (hterm.os == 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800524 }
525
526 terminal.passMetaNumber = v;
527 },
528
Marius Schilder77857b32014-05-14 16:21:26 -0700529 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700530 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700531 },
532
Robert Ginda8cb7d902013-06-20 14:37:18 -0700533 'receive-encoding': function(v) {
534 if (!(/^(utf-8|raw)$/).test(v)) {
535 console.warn('Invalid value for "receive-encoding": ' + v);
536 v = 'utf-8';
537 }
538
539 terminal.vt.characterEncoding = v;
540 },
541
Robert Ginda57f03b42012-09-13 11:02:48 -0700542 'scroll-on-keystroke': function(v) {
543 terminal.scrollOnKeystroke_ = v;
544 },
rginda9f5222b2012-03-05 11:53:28 -0800545
Robert Ginda57f03b42012-09-13 11:02:48 -0700546 'scroll-on-output': function(v) {
547 terminal.scrollOnOutput_ = v;
548 },
rginda30f20f62012-04-05 16:36:19 -0700549
Robert Ginda57f03b42012-09-13 11:02:48 -0700550 'scrollbar-visible': function(v) {
551 terminal.setScrollbarVisible(v);
552 },
rginda9f5222b2012-03-05 11:53:28 -0800553
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400554 'scroll-wheel-may-send-arrow-keys': function(v) {
555 terminal.scrollWheelArrowKeys_ = v;
556 },
557
Rob Spies49039e52014-12-17 13:40:04 -0800558 'scroll-wheel-move-multiplier': function(v) {
559 terminal.setScrollWheelMoveMultipler(v);
560 },
561
Robert Ginda57f03b42012-09-13 11:02:48 -0700562 'shift-insert-paste': function(v) {
563 terminal.keyboard.shiftInsertPaste = v;
564 },
rginda9f5222b2012-03-05 11:53:28 -0800565
Mike Frysingera7768922017-07-28 15:00:12 -0400566 'terminal-encoding': function(v) {
Mike Frysingera1371e12017-08-17 01:37:17 -0400567 terminal.vt.setEncoding(v);
Mike Frysingera7768922017-07-28 15:00:12 -0400568 },
569
Robert Gindae76aa9f2014-03-14 12:29:12 -0700570 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400571 terminal.scrollPort_.setUserCssUrl(v);
572 },
573
574 'user-css-text': function(v) {
575 terminal.scrollPort_.setUserCssText(v);
576 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400577
578 'word-break-match-left': function(v) {
579 terminal.primaryScreen_.wordBreakMatchLeft = v;
580 terminal.alternateScreen_.wordBreakMatchLeft = v;
581 },
582
583 'word-break-match-right': function(v) {
584 terminal.primaryScreen_.wordBreakMatchRight = v;
585 terminal.alternateScreen_.wordBreakMatchRight = v;
586 },
587
588 'word-break-match-middle': function(v) {
589 terminal.primaryScreen_.wordBreakMatchMiddle = v;
590 terminal.alternateScreen_.wordBreakMatchMiddle = v;
591 },
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400592
593 'allow-images-inline': function(v) {
594 terminal.allowImagesInline = v;
595 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700596 });
rginda30f20f62012-04-05 16:36:19 -0700597
Robert Ginda57f03b42012-09-13 11:02:48 -0700598 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800599 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700600
601 if (opt_callback)
602 opt_callback();
603 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800604};
605
Rob Spies56953412014-04-28 14:09:47 -0700606/**
607 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500608 *
Joel Hockey0f933582019-08-27 18:01:51 -0700609 * @return {!hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700610 */
611hterm.Terminal.prototype.getPrefs = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700612 return lib.notNull(this.prefs_);
Rob Spies56953412014-04-28 14:09:47 -0700613};
614
Robert Gindaa063b202014-07-21 11:08:25 -0700615/**
616 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500617 *
618 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700619 */
620hterm.Terminal.prototype.setBracketedPaste = function(state) {
621 this.options_.bracketedPaste = state;
622};
Rob Spies56953412014-04-28 14:09:47 -0700623
rginda8e92a692012-05-20 19:37:20 -0700624/**
625 * Set the color for the cursor.
626 *
627 * If you want this setting to persist, set it through prefs_, rather than
628 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500629 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500630 * @param {string=} color The color to set. If not defined, we reset to the
631 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700632 */
633hterm.Terminal.prototype.setCursorColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500634 if (color === undefined)
Joel Hockeyd4fca732019-09-20 16:57:03 -0700635 color = this.prefs_.getString('cursor-color');
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500636
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400637 this.setCssVar('cursor-color', color);
rginda8e92a692012-05-20 19:37:20 -0700638};
639
640/**
641 * Return the current cursor color as a string.
Mike Frysinger23b5b832019-10-01 17:05:29 -0400642 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500643 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700644 */
645hterm.Terminal.prototype.getCursorColor = function() {
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400646 return this.getCssVar('cursor-color');
rginda8e92a692012-05-20 19:37:20 -0700647};
648
649/**
rgindad5613292012-06-19 15:40:37 -0700650 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500651 *
652 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700653 */
654hterm.Terminal.prototype.setSelectionEnabled = function(state) {
655 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700656};
657
658/**
rginda8e92a692012-05-20 19:37:20 -0700659 * Set the background color.
660 *
661 * If you want this setting to persist, set it through prefs_, rather than
662 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500663 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500664 * @param {string=} color The color to set. If not defined, we reset to the
665 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700666 */
667hterm.Terminal.prototype.setBackgroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500668 if (color === undefined)
Joel Hockeyd4fca732019-09-20 16:57:03 -0700669 color = this.prefs_.getString('background-color');
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500670
Joel Hockey8ff48232019-09-24 13:15:17 -0700671 this.backgroundColor_ = lib.colors.normalizeCSS(color) || '';
Robert Ginda57f03b42012-09-13 11:02:48 -0700672 this.primaryScreen_.textAttributes.setDefaults(
673 this.foregroundColor_, this.backgroundColor_);
674 this.alternateScreen_.textAttributes.setDefaults(
675 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700676 this.scrollPort_.setBackgroundColor(color);
677};
678
rginda9f5222b2012-03-05 11:53:28 -0800679/**
680 * Return the current terminal background color.
681 *
682 * Intended for use by other classes, so we don't have to expose the entire
683 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500684 *
685 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800686 */
687hterm.Terminal.prototype.getBackgroundColor = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700688 return lib.notNull(this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700689};
690
691/**
692 * Set the foreground color.
693 *
694 * If you want this setting to persist, set it through prefs_, rather than
695 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500696 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500697 * @param {string=} color The color to set. If not defined, we reset to the
698 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700699 */
700hterm.Terminal.prototype.setForegroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500701 if (color === undefined)
Joel Hockeyd4fca732019-09-20 16:57:03 -0700702 color = this.prefs_.getString('foreground-color');
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500703
Joel Hockey8ff48232019-09-24 13:15:17 -0700704 this.foregroundColor_ = lib.colors.normalizeCSS(color) || '';
Robert Ginda57f03b42012-09-13 11:02:48 -0700705 this.primaryScreen_.textAttributes.setDefaults(
706 this.foregroundColor_, this.backgroundColor_);
707 this.alternateScreen_.textAttributes.setDefaults(
708 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700709 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800710};
711
712/**
713 * Return the current terminal foreground color.
714 *
715 * Intended for use by other classes, so we don't have to expose the entire
716 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500717 *
718 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800719 */
720hterm.Terminal.prototype.getForegroundColor = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700721 return lib.notNull(this.foregroundColor_);
rginda9f5222b2012-03-05 11:53:28 -0800722};
723
724/**
rginda87b86462011-12-14 13:48:03 -0800725 * Create a new instance of a terminal command and run it with a given
726 * argument string.
727 *
Joel Hockeyd4fca732019-09-20 16:57:03 -0700728 * @param {!Function} commandClass The constructor for a terminal command.
Joel Hockey8081ea62019-08-26 16:52:32 -0700729 * @param {string} commandName The command to run for this terminal.
730 * @param {!Array<string>} args The arguments to pass to the command.
rginda87b86462011-12-14 13:48:03 -0800731 */
Joel Hockey8081ea62019-08-26 16:52:32 -0700732hterm.Terminal.prototype.runCommandClass = function(
733 commandClass, commandName, args) {
rgindaf522ce02012-04-17 17:49:17 -0700734 var environment = this.prefs_.get('environment');
735 if (typeof environment != 'object' || environment == null)
736 environment = {};
737
rginda87b86462011-12-14 13:48:03 -0800738 var self = this;
739 this.command = new commandClass(
Joel Hockey8081ea62019-08-26 16:52:32 -0700740 {
741 commandName: commandName,
742 args: args,
rginda87b86462011-12-14 13:48:03 -0800743 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700744 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800745 onExit: function(code) {
746 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800747 self.uninstallKeyboard();
Julian Watsondfbf8592019-11-05 18:05:12 +1100748 self.div_.dispatchEvent(new CustomEvent('terminal-closing'));
rginda9875d902012-08-20 16:21:57 -0700749 if (self.prefs_.get('close-on-exit'))
750 window.close();
rginda87b86462011-12-14 13:48:03 -0800751 }
752 });
753
rgindafeaf3142012-01-31 15:14:20 -0800754 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800755 this.command.run();
756};
757
758/**
rgindafeaf3142012-01-31 15:14:20 -0800759 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500760 *
761 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800762 */
763hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700764 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800765};
766
767/**
768 * Install the keyboard handler for this terminal.
769 *
770 * This will prevent the browser from seeing any keystrokes sent to the
771 * terminal.
772 */
773hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700774 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400775};
rgindafeaf3142012-01-31 15:14:20 -0800776
777/**
778 * Uninstall the keyboard handler for this terminal.
779 */
780hterm.Terminal.prototype.uninstallKeyboard = function() {
781 this.keyboard.installKeyboard(null);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400782};
rgindafeaf3142012-01-31 15:14:20 -0800783
784/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400785 * Set a CSS variable.
786 *
787 * Normally this is used to set variables in the hterm namespace.
788 *
789 * @param {string} name The variable to set.
Joel Hockeyd4fca732019-09-20 16:57:03 -0700790 * @param {string|number} value The value to assign to the variable.
Joel Hockey0f933582019-08-27 18:01:51 -0700791 * @param {string=} opt_prefix The variable namespace/prefix to use.
Mike Frysingercce97c42017-08-05 01:11:22 -0400792 */
793hterm.Terminal.prototype.setCssVar = function(name, value,
794 opt_prefix='--hterm-') {
795 this.document_.documentElement.style.setProperty(
Joel Hockeyd4fca732019-09-20 16:57:03 -0700796 `${opt_prefix}${name}`, value.toString());
Mike Frysingercce97c42017-08-05 01:11:22 -0400797};
798
799/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500800 * Get a CSS variable.
801 *
802 * Normally this is used to get variables in the hterm namespace.
803 *
804 * @param {string} name The variable to read.
Joel Hockey0f933582019-08-27 18:01:51 -0700805 * @param {string=} opt_prefix The variable namespace/prefix to use.
Mike Frysinger261597c2017-12-28 01:14:21 -0500806 * @return {string} The current setting for this variable.
807 */
808hterm.Terminal.prototype.getCssVar = function(name, opt_prefix='--hterm-') {
809 return this.document_.documentElement.style.getPropertyValue(
810 `${opt_prefix}${name}`);
811};
812
813/**
rginda35c456b2012-02-09 17:29:05 -0800814 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800815 *
816 * Call setFontSize(0) to reset to the default font size.
817 *
818 * This function does not modify the font-size preference.
819 *
820 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800821 */
822hterm.Terminal.prototype.setFontSize = function(px) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500823 if (px <= 0)
Joel Hockeyd4fca732019-09-20 16:57:03 -0700824 px = this.prefs_.getNumber('font-size');
rginda9f5222b2012-03-05 11:53:28 -0800825
rginda35c456b2012-02-09 17:29:05 -0800826 this.scrollPort_.setFontSize(px);
Mike Frysingercce97c42017-08-05 01:11:22 -0400827 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
828 this.setCssVar('charsize-height',
829 this.scrollPort_.characterSize.height + 'px');
rginda35c456b2012-02-09 17:29:05 -0800830};
831
832/**
833 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500834 *
835 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800836 */
837hterm.Terminal.prototype.getFontSize = function() {
838 return this.scrollPort_.getFontSize();
839};
840
841/**
rginda8e92a692012-05-20 19:37:20 -0700842 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500843 *
844 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700845 */
846hterm.Terminal.prototype.getFontFamily = function() {
847 return this.scrollPort_.getFontFamily();
848};
849
850/**
rginda35c456b2012-02-09 17:29:05 -0800851 * Set the CSS "font-family" for this terminal.
852 */
rginda9f5222b2012-03-05 11:53:28 -0800853hterm.Terminal.prototype.syncFontFamily = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700854 this.scrollPort_.setFontFamily(this.prefs_.getString('font-family'),
855 this.prefs_.getString('font-smoothing'));
rginda9f5222b2012-03-05 11:53:28 -0800856 this.syncBoldSafeState();
857};
858
rginda4bba5e12012-06-20 16:15:30 -0700859/**
860 * Set this.mousePasteButton based on the mouse-paste-button pref,
861 * autodetecting if necessary.
862 */
863hterm.Terminal.prototype.syncMousePasteButton = function() {
864 var button = this.prefs_.get('mouse-paste-button');
865 if (typeof button == 'number') {
866 this.mousePasteButton = button;
867 return;
868 }
869
Mike Frysingeree81a002017-12-12 16:14:53 -0500870 if (hterm.os != 'linux') {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400871 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700872 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400873 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700874 }
875};
876
877/**
878 * Enable or disable bold based on the enable-bold pref, autodetecting if
879 * necessary.
880 */
rginda9f5222b2012-03-05 11:53:28 -0800881hterm.Terminal.prototype.syncBoldSafeState = function() {
882 var enableBold = this.prefs_.get('enable-bold');
883 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700884 this.primaryScreen_.textAttributes.enableBold = enableBold;
885 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800886 return;
887 }
888
rgindaf7521392012-02-28 17:20:34 -0800889 var normalSize = this.scrollPort_.measureCharacterSize();
890 var boldSize = this.scrollPort_.measureCharacterSize('bold');
891
892 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800893 if (!isBoldSafe) {
894 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700895 'from normal. Font family is: ' +
896 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800897 }
rginda9f5222b2012-03-05 11:53:28 -0800898
Robert Gindaed016262012-10-26 16:27:09 -0700899 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
900 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800901};
902
903/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500904 * Control text blinking behavior.
905 *
906 * @param {boolean=} state Whether to enable support for blinking text.
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400907 */
Mike Frysinger261597c2017-12-28 01:14:21 -0500908hterm.Terminal.prototype.setTextBlink = function(state) {
909 if (state === undefined)
Joel Hockeyd4fca732019-09-20 16:57:03 -0700910 state = this.prefs_.getBoolean('enable-blink');
Mike Frysinger261597c2017-12-28 01:14:21 -0500911 this.setCssVar('blink-node-duration', state ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400912};
913
914/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400915 * Set the mouse cursor style based on the current terminal mode.
916 */
917hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -0400918 this.setCssVar('mouse-cursor-style',
919 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
920 'var(--hterm-mouse-cursor-text)' :
Mike Frysinger67f58f82018-11-22 13:38:22 -0500921 'var(--hterm-mouse-cursor-default)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400922};
923
924/**
rginda87b86462011-12-14 13:48:03 -0800925 * Return a copy of the current cursor position.
926 *
Joel Hockey0f933582019-08-27 18:01:51 -0700927 * @return {!hterm.RowCol} The RowCol object representing the current position.
rginda87b86462011-12-14 13:48:03 -0800928 */
929hterm.Terminal.prototype.saveCursor = function() {
930 return this.screen_.cursorPosition.clone();
931};
932
Evan Jones2600d4f2016-12-06 09:29:36 -0500933/**
934 * Return the current text attributes.
935 *
Joel Hockeyd4fca732019-09-20 16:57:03 -0700936 * @return {!hterm.TextAttributes}
Evan Jones2600d4f2016-12-06 09:29:36 -0500937 */
rgindaa19afe22012-01-25 15:40:22 -0800938hterm.Terminal.prototype.getTextAttributes = function() {
939 return this.screen_.textAttributes;
940};
941
Evan Jones2600d4f2016-12-06 09:29:36 -0500942/**
943 * Set the text attributes.
944 *
Joel Hockeyd4fca732019-09-20 16:57:03 -0700945 * @param {!hterm.TextAttributes} textAttributes The attributes to set.
Evan Jones2600d4f2016-12-06 09:29:36 -0500946 */
rginda1a09aa02012-06-18 21:11:25 -0700947hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
948 this.screen_.textAttributes = textAttributes;
949};
950
rginda87b86462011-12-14 13:48:03 -0800951/**
rgindaf522ce02012-04-17 17:49:17 -0700952 * Return the current browser zoom factor applied to the terminal.
953 *
954 * @return {number} The current browser zoom factor.
955 */
956hterm.Terminal.prototype.getZoomFactor = function() {
957 return this.scrollPort_.characterSize.zoomFactor;
958};
959
960/**
rginda9846e2f2012-01-27 13:53:33 -0800961 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -0500962 *
963 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -0800964 */
965hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800966 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800967};
968
969/**
rginda87b86462011-12-14 13:48:03 -0800970 * Restore a previously saved cursor position.
971 *
Joel Hockey0f933582019-08-27 18:01:51 -0700972 * @param {!hterm.RowCol} cursor The position to restore.
rginda87b86462011-12-14 13:48:03 -0800973 */
974hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700975 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
976 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800977 this.screen_.setCursorPosition(row, column);
978 if (cursor.column > column ||
979 cursor.column == column && cursor.overflow) {
980 this.screen_.cursorPosition.overflow = true;
981 }
rginda87b86462011-12-14 13:48:03 -0800982};
983
984/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400985 * Clear the cursor's overflow flag.
986 */
987hterm.Terminal.prototype.clearCursorOverflow = function() {
988 this.screen_.cursorPosition.overflow = false;
989};
990
991/**
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800992 * Save the current cursor state to the corresponding screens.
993 *
994 * See the hterm.Screen.CursorState class for more details.
995 *
996 * @param {boolean=} both If true, update both screens, else only update the
997 * current screen.
998 */
999hterm.Terminal.prototype.saveCursorAndState = function(both) {
1000 if (both) {
1001 this.primaryScreen_.saveCursorAndState(this.vt);
1002 this.alternateScreen_.saveCursorAndState(this.vt);
1003 } else
1004 this.screen_.saveCursorAndState(this.vt);
1005};
1006
1007/**
1008 * Restore the saved cursor state in the corresponding screens.
1009 *
1010 * See the hterm.Screen.CursorState class for more details.
1011 *
1012 * @param {boolean=} both If true, update both screens, else only update the
1013 * current screen.
1014 */
1015hterm.Terminal.prototype.restoreCursorAndState = function(both) {
1016 if (both) {
1017 this.primaryScreen_.restoreCursorAndState(this.vt);
1018 this.alternateScreen_.restoreCursorAndState(this.vt);
1019 } else
1020 this.screen_.restoreCursorAndState(this.vt);
1021};
1022
1023/**
Robert Ginda830583c2013-08-07 13:20:46 -07001024 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001025 *
1026 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -07001027 */
1028hterm.Terminal.prototype.setCursorShape = function(shape) {
1029 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001030 this.restyleCursor_();
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001031};
Robert Ginda830583c2013-08-07 13:20:46 -07001032
1033/**
1034 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001035 *
1036 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -07001037 */
1038hterm.Terminal.prototype.getCursorShape = function() {
1039 return this.cursorShape_;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001040};
Robert Ginda830583c2013-08-07 13:20:46 -07001041
1042/**
rginda87b86462011-12-14 13:48:03 -08001043 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001044 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001045 * @param {?number} columnCount
rginda87b86462011-12-14 13:48:03 -08001046 */
1047hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -08001048 if (columnCount == null) {
1049 this.div_.style.width = '100%';
1050 return;
1051 }
1052
Robert Ginda26806d12014-07-24 13:44:07 -07001053 this.div_.style.width = Math.ceil(
1054 this.scrollPort_.characterSize.width *
1055 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001056 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -08001057 this.scheduleSyncCursorPosition_();
1058};
rginda87b86462011-12-14 13:48:03 -08001059
rgindac9bc5502012-01-18 11:48:44 -08001060/**
rginda35c456b2012-02-09 17:29:05 -08001061 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001062 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001063 * @param {?number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -08001064 */
1065hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -08001066 if (rowCount == null) {
1067 this.div_.style.height = '100%';
1068 return;
1069 }
1070
rginda35c456b2012-02-09 17:29:05 -08001071 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -07001072 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -08001073 this.realizeSize_(this.screenSize.width, rowCount);
1074 this.scheduleSyncCursorPosition_();
1075};
1076
1077/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001078 * Deal with terminal size changes.
1079 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001080 * @param {number} columnCount The number of columns.
1081 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001082 */
1083hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
Mike Frysinger0206e262019-06-13 10:18:19 -04001084 let notify = false;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001085
Mike Frysinger0206e262019-06-13 10:18:19 -04001086 if (columnCount != this.screenSize.width) {
1087 notify = true;
1088 this.realizeWidth_(columnCount);
1089 }
1090
1091 if (rowCount != this.screenSize.height) {
1092 notify = true;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001093 this.realizeHeight_(rowCount);
Mike Frysinger0206e262019-06-13 10:18:19 -04001094 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001095
1096 // Send new terminal size to plugin.
Mike Frysinger0206e262019-06-13 10:18:19 -04001097 if (notify) {
1098 this.io.onTerminalResize_(columnCount, rowCount);
1099 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001100};
1101
1102/**
rgindac9bc5502012-01-18 11:48:44 -08001103 * Deal with terminal width changes.
1104 *
1105 * This function does what needs to be done when the terminal width changes
1106 * out from under us. It happens here rather than in onResize_() because this
1107 * code may need to run synchronously to handle programmatic changes of
1108 * terminal width.
1109 *
1110 * Relying on the browser to send us an async resize event means we may not be
1111 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001112 *
1113 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001114 */
1115hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001116 if (columnCount <= 0)
1117 throw new Error('Attempt to realize bad width: ' + columnCount);
1118
rgindac9bc5502012-01-18 11:48:44 -08001119 var deltaColumns = columnCount - this.screen_.getWidth();
Mike Frysinger0206e262019-06-13 10:18:19 -04001120 if (deltaColumns == 0) {
1121 // No change, so don't bother recalculating things.
1122 return;
1123 }
rgindac9bc5502012-01-18 11:48:44 -08001124
rginda87b86462011-12-14 13:48:03 -08001125 this.screenSize.width = columnCount;
1126 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001127
1128 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -04001129 if (this.defaultTabStops)
1130 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -08001131 } else {
1132 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -04001133 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -08001134 break;
1135
1136 this.tabStops_.pop();
1137 }
1138 }
1139
1140 this.screen_.setColumnCount(this.screenSize.width);
1141};
1142
1143/**
1144 * Deal with terminal height changes.
1145 *
1146 * This function does what needs to be done when the terminal height changes
1147 * out from under us. It happens here rather than in onResize_() because this
1148 * code may need to run synchronously to handle programmatic changes of
1149 * terminal height.
1150 *
1151 * Relying on the browser to send us an async resize event means we may not be
1152 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001153 *
1154 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001155 */
1156hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001157 if (rowCount <= 0)
1158 throw new Error('Attempt to realize bad height: ' + rowCount);
1159
rgindac9bc5502012-01-18 11:48:44 -08001160 var deltaRows = rowCount - this.screen_.getHeight();
Mike Frysinger0206e262019-06-13 10:18:19 -04001161 if (deltaRows == 0) {
1162 // No change, so don't bother recalculating things.
1163 return;
1164 }
rgindac9bc5502012-01-18 11:48:44 -08001165
1166 this.screenSize.height = rowCount;
1167
1168 var cursor = this.saveCursor();
1169
1170 if (deltaRows < 0) {
1171 // Screen got smaller.
1172 deltaRows *= -1;
1173 while (deltaRows) {
1174 var lastRow = this.getRowCount() - 1;
1175 if (lastRow - this.scrollbackRows_.length == cursor.row)
1176 break;
1177
1178 if (this.getRowText(lastRow))
1179 break;
1180
1181 this.screen_.popRow();
1182 deltaRows--;
1183 }
1184
1185 var ary = this.screen_.shiftRows(deltaRows);
1186 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1187
1188 // We just removed rows from the top of the screen, we need to update
1189 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001190 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001191 } else if (deltaRows > 0) {
1192 // Screen got larger.
1193
1194 if (deltaRows <= this.scrollbackRows_.length) {
1195 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1196 var rows = this.scrollbackRows_.splice(
1197 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1198 this.screen_.unshiftRows(rows);
1199 deltaRows -= scrollbackCount;
1200 cursor.row += scrollbackCount;
1201 }
1202
1203 if (deltaRows)
1204 this.appendRows_(deltaRows);
1205 }
1206
rginda35c456b2012-02-09 17:29:05 -08001207 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001208 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001209};
1210
1211/**
1212 * Scroll the terminal to the top of the scrollback buffer.
1213 */
1214hterm.Terminal.prototype.scrollHome = function() {
1215 this.scrollPort_.scrollRowToTop(0);
1216};
1217
1218/**
1219 * Scroll the terminal to the end.
1220 */
1221hterm.Terminal.prototype.scrollEnd = function() {
1222 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1223};
1224
1225/**
1226 * Scroll the terminal one page up (minus one line) relative to the current
1227 * position.
1228 */
1229hterm.Terminal.prototype.scrollPageUp = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001230 this.scrollPort_.scrollPageUp();
rginda87b86462011-12-14 13:48:03 -08001231};
1232
1233/**
1234 * Scroll the terminal one page down (minus one line) relative to the current
1235 * position.
1236 */
1237hterm.Terminal.prototype.scrollPageDown = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001238 this.scrollPort_.scrollPageDown();
rginda8ba33642011-12-14 12:31:31 -08001239};
1240
rgindac9bc5502012-01-18 11:48:44 -08001241/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001242 * Scroll the terminal one line up relative to the current position.
1243 */
1244hterm.Terminal.prototype.scrollLineUp = function() {
1245 var i = this.scrollPort_.getTopRowIndex();
1246 this.scrollPort_.scrollRowToTop(i - 1);
1247};
1248
1249/**
1250 * Scroll the terminal one line down relative to the current position.
1251 */
1252hterm.Terminal.prototype.scrollLineDown = function() {
1253 var i = this.scrollPort_.getTopRowIndex();
1254 this.scrollPort_.scrollRowToTop(i + 1);
1255};
1256
1257/**
Robert Ginda40932892012-12-10 17:26:40 -08001258 * Clear primary screen, secondary screen, and the scrollback buffer.
1259 */
1260hterm.Terminal.prototype.wipeContents = function() {
Mike Frysinger9c482b82018-09-07 02:49:36 -04001261 this.clearHome(this.primaryScreen_);
1262 this.clearHome(this.alternateScreen_);
1263
1264 this.clearScrollback();
1265};
1266
1267/**
1268 * Clear scrollback buffer.
1269 */
1270hterm.Terminal.prototype.clearScrollback = function() {
1271 // Move to the end of the buffer in case the screen was scrolled back.
1272 // We're going to throw it away which would leave the display invalid.
1273 this.scrollEnd();
1274
Robert Ginda40932892012-12-10 17:26:40 -08001275 this.scrollbackRows_.length = 0;
1276 this.scrollPort_.resetCache();
1277
Mike Frysinger9c482b82018-09-07 02:49:36 -04001278 [this.primaryScreen_, this.alternateScreen_].forEach((screen) => {
1279 const bottom = screen.getHeight();
1280 this.renumberRows_(0, bottom, screen);
1281 });
Robert Ginda40932892012-12-10 17:26:40 -08001282
1283 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001284 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001285};
1286
1287/**
rgindac9bc5502012-01-18 11:48:44 -08001288 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001289 *
1290 * Perform a full reset to the default values listed in
1291 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001292 */
rginda87b86462011-12-14 13:48:03 -08001293hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001294 this.vt.reset();
1295
rgindac9bc5502012-01-18 11:48:44 -08001296 this.clearAllTabStops();
1297 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001298
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001299 const resetScreen = (screen) => {
1300 // We want to make sure to reset the attributes before we clear the screen.
1301 // The attributes might be used to initialize default/empty rows.
1302 screen.textAttributes.reset();
1303 screen.textAttributes.resetColorPalette();
1304 this.clearHome(screen);
1305 screen.saveCursorAndState(this.vt);
1306 };
1307 resetScreen(this.primaryScreen_);
1308 resetScreen(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001309
Mike Frysinger84301d02017-11-29 13:28:46 -08001310 // Reset terminal options to their default values.
1311 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001312 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1313
Mike Frysinger84301d02017-11-29 13:28:46 -08001314 this.setVTScrollRegion(null, null);
1315
1316 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001317};
1318
rgindac9bc5502012-01-18 11:48:44 -08001319/**
1320 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001321 *
1322 * Perform a soft reset to the default values listed in
1323 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001324 */
rginda0f5c0292012-01-13 11:00:13 -08001325hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001326 this.vt.reset();
1327
rgindab8bc8932012-04-27 12:45:03 -07001328 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001329 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001330
Brad Townb62dfdc2015-03-16 19:07:15 -07001331 // We show the cursor on soft reset but do not alter the blink state.
1332 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1333
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001334 const resetScreen = (screen) => {
1335 // Xterm also resets the color palette on soft reset, even though it doesn't
1336 // seem to be documented anywhere.
1337 screen.textAttributes.reset();
1338 screen.textAttributes.resetColorPalette();
1339 screen.saveCursorAndState(this.vt);
1340 };
1341 resetScreen(this.primaryScreen_);
1342 resetScreen(this.alternateScreen_);
rgindaf522ce02012-04-17 17:49:17 -07001343
rgindab8bc8932012-04-27 12:45:03 -07001344 // The xterm man page explicitly says this will happen on soft reset.
1345 this.setVTScrollRegion(null, null);
1346
1347 // Xterm also shows the cursor on soft reset, but does not alter the blink
1348 // state.
rgindaa19afe22012-01-25 15:40:22 -08001349 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001350};
1351
rgindac9bc5502012-01-18 11:48:44 -08001352/**
1353 * Move the cursor forward to the next tab stop, or to the last column
1354 * if no more tab stops are set.
1355 */
1356hterm.Terminal.prototype.forwardTabStop = function() {
1357 var column = this.screen_.cursorPosition.column;
1358
1359 for (var i = 0; i < this.tabStops_.length; i++) {
1360 if (this.tabStops_[i] > column) {
1361 this.setCursorColumn(this.tabStops_[i]);
1362 return;
1363 }
1364 }
1365
David Benjamin66e954d2012-05-05 21:08:12 -04001366 // xterm does not clear the overflow flag on HT or CHT.
1367 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001368 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001369 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001370};
1371
rgindac9bc5502012-01-18 11:48:44 -08001372/**
1373 * Move the cursor backward to the previous tab stop, or to the first column
1374 * if no previous tab stops are set.
1375 */
1376hterm.Terminal.prototype.backwardTabStop = function() {
1377 var column = this.screen_.cursorPosition.column;
1378
1379 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1380 if (this.tabStops_[i] < column) {
1381 this.setCursorColumn(this.tabStops_[i]);
1382 return;
1383 }
1384 }
1385
1386 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001387};
1388
rgindac9bc5502012-01-18 11:48:44 -08001389/**
1390 * Set a tab stop at the given column.
1391 *
Joel Hockey0f933582019-08-27 18:01:51 -07001392 * @param {number} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001393 */
1394hterm.Terminal.prototype.setTabStop = function(column) {
1395 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1396 if (this.tabStops_[i] == column)
1397 return;
1398
1399 if (this.tabStops_[i] < column) {
1400 this.tabStops_.splice(i + 1, 0, column);
1401 return;
1402 }
1403 }
1404
1405 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001406};
1407
rgindac9bc5502012-01-18 11:48:44 -08001408/**
1409 * Clear the tab stop at the current cursor position.
1410 *
1411 * No effect if there is no tab stop at the current cursor position.
1412 */
1413hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1414 var column = this.screen_.cursorPosition.column;
1415
1416 var i = this.tabStops_.indexOf(column);
1417 if (i == -1)
1418 return;
1419
1420 this.tabStops_.splice(i, 1);
1421};
1422
1423/**
1424 * Clear all tab stops.
1425 */
1426hterm.Terminal.prototype.clearAllTabStops = function() {
1427 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001428 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001429};
1430
1431/**
1432 * Set up the default tab stops, starting from a given column.
1433 *
1434 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001435 * from the specified column, or 0 if no column is provided. It also flags
1436 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001437 *
1438 * This does not clear the existing tab stops first, use clearAllTabStops
1439 * for that.
1440 *
Joel Hockey0f933582019-08-27 18:01:51 -07001441 * @param {number=} opt_start Optional starting zero based starting column,
1442 * useful for filling out missing tab stops when the terminal is resized.
rgindac9bc5502012-01-18 11:48:44 -08001443 */
1444hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1445 var start = opt_start || 0;
1446 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001447 // Round start up to a default tab stop.
1448 start = start - 1 - ((start - 1) % w) + w;
1449 for (var i = start; i < this.screenSize.width; i += w) {
1450 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001451 }
David Benjamin66e954d2012-05-05 21:08:12 -04001452
1453 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001454};
1455
rginda6d397402012-01-17 10:58:29 -08001456/**
rginda8ba33642011-12-14 12:31:31 -08001457 * Interpret a sequence of characters.
1458 *
1459 * Incomplete escape sequences are buffered until the next call.
1460 *
1461 * @param {string} str Sequence of characters to interpret or pass through.
1462 */
1463hterm.Terminal.prototype.interpret = function(str) {
rginda8ba33642011-12-14 12:31:31 -08001464 this.scheduleSyncCursorPosition_();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001465 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001466};
1467
1468/**
1469 * Take over the given DIV for use as the terminal display.
1470 *
Joel Hockey0f933582019-08-27 18:01:51 -07001471 * @param {!Element} div The div to use as the terminal display.
rginda8ba33642011-12-14 12:31:31 -08001472 */
1473hterm.Terminal.prototype.decorate = function(div) {
Mike Frysinger5768a9d2017-12-26 12:57:44 -05001474 const charset = div.ownerDocument.characterSet.toLowerCase();
1475 if (charset != 'utf-8') {
1476 console.warn(`Document encoding should be set to utf-8, not "${charset}";` +
1477 ` Add <meta charset='utf-8'/> to your HTML <head> to fix.`);
1478 }
1479
rginda87b86462011-12-14 13:48:03 -08001480 this.div_ = div;
1481
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001482 this.accessibilityReader_ = new hterm.AccessibilityReader(div);
1483
Adrián Pérez-Orozco394e64f2018-12-17 17:20:16 -08001484 this.scrollPort_.decorate(div, () => this.setupScrollPort_());
1485};
1486
1487/**
1488 * Initialisation of ScrollPort properties which need to be set after its DOM
1489 * has been initialised.
Mike Frysinger23b5b832019-10-01 17:05:29 -04001490 *
Adrián Pérez-Orozco394e64f2018-12-17 17:20:16 -08001491 * @private
1492 */
1493hterm.Terminal.prototype.setupScrollPort_ = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -07001494 this.scrollPort_.setBackgroundImage(
1495 this.prefs_.getString('background-image'));
1496 this.scrollPort_.setBackgroundSize(this.prefs_.getString('background-size'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001497 this.scrollPort_.setBackgroundPosition(
Joel Hockeyd4fca732019-09-20 16:57:03 -07001498 this.prefs_.getString('background-position'));
1499 this.scrollPort_.setUserCssUrl(this.prefs_.getString('user-css'));
1500 this.scrollPort_.setUserCssText(this.prefs_.getString('user-css-text'));
1501 this.scrollPort_.setAccessibilityReader(
1502 lib.notNull(this.accessibilityReader_));
rginda30f20f62012-04-05 16:36:19 -07001503
rginda0918b652012-04-04 11:26:24 -07001504 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001505
Joel Hockeyd4fca732019-09-20 16:57:03 -07001506 this.setFontSize(this.prefs_.getNumber('font-size'));
rginda9f5222b2012-03-05 11:53:28 -08001507 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001508
Joel Hockeyd4fca732019-09-20 16:57:03 -07001509 this.setScrollbarVisible(this.prefs_.getBoolean('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001510 this.setScrollWheelMoveMultipler(
Joel Hockeyd4fca732019-09-20 16:57:03 -07001511 this.prefs_.getNumber('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001512
rginda8ba33642011-12-14 12:31:31 -08001513 this.document_ = this.scrollPort_.getDocument();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001514 this.accessibilityReader_.decorate(this.document_);
rginda8ba33642011-12-14 12:31:31 -08001515
Evan Jones5f9df812016-12-06 09:38:58 -05001516 this.document_.body.oncontextmenu = function() { return false; };
Mike Frysingercc114512017-09-11 21:39:17 -04001517 this.contextMenu.setDocument(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07001518
1519 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001520 var screenNode = this.scrollPort_.getScreenNode();
Joel Hockeyd4fca732019-09-20 16:57:03 -07001521 screenNode.addEventListener(
1522 'mousedown', /** @type {!EventListener} */ (onMouse));
1523 screenNode.addEventListener(
1524 'mouseup', /** @type {!EventListener} */ (onMouse));
1525 screenNode.addEventListener(
1526 'mousemove', /** @type {!EventListener} */ (onMouse));
rginda4bba5e12012-06-20 16:15:30 -07001527 this.scrollPort_.onScrollWheel = onMouse;
1528
Joel Hockeyd4fca732019-09-20 16:57:03 -07001529 screenNode.addEventListener(
1530 'keydown',
1531 /** @type {!EventListener} */ (this.onKeyboardActivity_.bind(this)));
Mike Frysinger02ded6d2018-06-21 14:25:20 -04001532
Toni Barzic0bfa8922013-11-22 11:18:35 -08001533 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001534 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001535 // Listen for mousedown events on the screenNode as in FF the focus
1536 // events don't bubble.
1537 screenNode.addEventListener('mousedown', function() {
1538 setTimeout(this.onFocusChange_.bind(this, true));
1539 }.bind(this));
1540
Toni Barzic0bfa8922013-11-22 11:18:35 -08001541 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001542 'blur', this.onFocusChange_.bind(this, false));
1543
1544 var style = this.document_.createElement('style');
Joel Hockeyd36efd62019-09-30 14:16:20 -07001545 style.textContent = `
1546.cursor-node[focus="false"] {
1547 box-sizing: border-box;
1548 background-color: transparent !important;
1549 border-width: 2px;
1550 border-style: solid;
1551}
1552menu {
1553 margin: 0;
1554 padding: 0;
1555 cursor: var(--hterm-mouse-cursor-pointer);
1556}
1557menuitem {
1558 white-space: nowrap;
1559 border-bottom: 1px dashed;
1560 display: block;
1561 padding: 0.3em 0.3em 0 0.3em;
1562}
1563menuitem.separator {
1564 border-bottom: none;
1565 height: 0.5em;
1566 padding: 0;
1567}
1568menuitem:hover {
1569 color: var(--hterm-cursor-color);
1570}
1571.wc-node {
1572 display: inline-block;
1573 text-align: center;
1574 width: calc(var(--hterm-charsize-width) * 2);
1575 line-height: var(--hterm-charsize-height);
1576}
1577:root {
1578 --hterm-charsize-width: ${this.scrollPort_.characterSize.width}px;
1579 --hterm-charsize-height: ${this.scrollPort_.characterSize.height}px;
1580 /* Default position hides the cursor for when the window is initializing. */
1581 --hterm-cursor-offset-col: -1;
1582 --hterm-cursor-offset-row: -1;
1583 --hterm-blink-node-duration: 0.7s;
1584 --hterm-mouse-cursor-default: default;
1585 --hterm-mouse-cursor-text: text;
1586 --hterm-mouse-cursor-pointer: pointer;
1587 --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);
1588}
1589.uri-node:hover {
1590 text-decoration: underline;
1591 cursor: var(--hterm-mouse-cursor-pointer);
1592}
1593@keyframes blink {
1594 from { opacity: 1.0; }
1595 to { opacity: 0.0; }
1596}
1597.blink-node {
1598 animation-name: blink;
1599 animation-duration: var(--hterm-blink-node-duration);
1600 animation-iteration-count: infinite;
1601 animation-timing-function: ease-in-out;
1602 animation-direction: alternate;
1603}`;
Mike Frysingerb74a6472018-06-22 13:37:08 -04001604 // Insert this stock style as the first node so that any user styles will
1605 // override w/out having to use !important everywhere. The rules above mix
1606 // runtime variables with default ones designed to be overridden by the user,
1607 // but we can wait for a concrete case from the users to determine the best
1608 // way to split the sheet up to before & after the user-css settings.
1609 this.document_.head.insertBefore(style, this.document_.head.firstChild);
rginda8e92a692012-05-20 19:37:20 -07001610
rginda8ba33642011-12-14 12:31:31 -08001611 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001612 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001613 this.cursorNode_.className = 'cursor-node';
Joel Hockeyd36efd62019-09-30 14:16:20 -07001614 this.cursorNode_.style.cssText = `
1615position: absolute;
1616left: calc(var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));
1617top: calc(var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));
1618display: ${this.options_.cursorVisible ? '' : 'none'};
1619width: var(--hterm-charsize-width);
1620height: var(--hterm-charsize-height);
1621background-color: var(--hterm-cursor-color);
1622border-color: var(--hterm-cursor-color);
1623-webkit-transition: opacity, background-color 100ms linear;
1624-moz-transition: opacity, background-color 100ms linear;`;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001625
Mike Frysingerf02a2cb2017-12-21 00:34:03 -05001626 this.setCursorColor();
Robert Gindafb1be6a2013-12-11 11:56:22 -08001627 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1628 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001629
rginda8ba33642011-12-14 12:31:31 -08001630 this.document_.body.appendChild(this.cursorNode_);
1631
rgindad5613292012-06-19 15:40:37 -07001632 // When 'enableMouseDragScroll' is off we reposition this element directly
1633 // under the mouse cursor after a click. This makes Chrome associate
1634 // subsequent mousemove events with the scroll-blocker. Since the
1635 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1636 // events do not cause the scrollport to scroll.
1637 //
1638 // It's a hack, but it's the cleanest way I could find.
1639 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001640 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
Raymes Khoury6dce2f82018-04-12 15:38:58 +10001641 this.scrollBlockerNode_.setAttribute('aria-hidden', 'true');
rgindad5613292012-06-19 15:40:37 -07001642 this.scrollBlockerNode_.style.cssText =
1643 ('position: absolute;' +
1644 'top: -99px;' +
1645 'display: block;' +
1646 'width: 10px;' +
1647 'height: 10px;');
1648 this.document_.body.appendChild(this.scrollBlockerNode_);
1649
rgindad5613292012-06-19 15:40:37 -07001650 this.scrollPort_.onScrollWheel = onMouse;
1651 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1652 ].forEach(function(event) {
1653 this.scrollBlockerNode_.addEventListener(event, onMouse);
Joel Hockeyd4fca732019-09-20 16:57:03 -07001654 this.cursorNode_.addEventListener(
1655 event, /** @type {!EventListener} */ (onMouse));
1656 this.document_.addEventListener(
1657 event, /** @type {!EventListener} */ (onMouse));
rgindad5613292012-06-19 15:40:37 -07001658 }.bind(this));
1659
1660 this.cursorNode_.addEventListener('mousedown', function() {
1661 setTimeout(this.focus.bind(this));
1662 }.bind(this));
1663
rginda8ba33642011-12-14 12:31:31 -08001664 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001665
rginda87b86462011-12-14 13:48:03 -08001666 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001667 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001668};
1669
rginda0918b652012-04-04 11:26:24 -07001670/**
1671 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001672 *
Joel Hockey0f933582019-08-27 18:01:51 -07001673 * @return {!Document}
rginda0918b652012-04-04 11:26:24 -07001674 */
rginda87b86462011-12-14 13:48:03 -08001675hterm.Terminal.prototype.getDocument = function() {
1676 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001677};
1678
1679/**
rginda0918b652012-04-04 11:26:24 -07001680 * Focus the terminal.
1681 */
1682hterm.Terminal.prototype.focus = function() {
1683 this.scrollPort_.focus();
1684};
1685
1686/**
Theodore Duboiscea9b782019-09-02 17:48:00 -07001687 * Unfocus the terminal.
1688 */
1689hterm.Terminal.prototype.blur = function() {
1690 this.scrollPort_.blur();
1691};
1692
1693/**
rginda8ba33642011-12-14 12:31:31 -08001694 * Return the HTML Element for a given row index.
1695 *
1696 * This is a method from the RowProvider interface. The ScrollPort uses
1697 * it to fetch rows on demand as they are scrolled into view.
1698 *
1699 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1700 * pairs to conserve memory.
1701 *
Joel Hockey0f933582019-08-27 18:01:51 -07001702 * @param {number} index The zero-based row index, measured relative to the
rginda8ba33642011-12-14 12:31:31 -08001703 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001704 * largest indices.
Joel Hockey0f933582019-08-27 18:01:51 -07001705 * @return {!Element} The 'x-row' element containing for the requested row.
Joel Hockeyd4fca732019-09-20 16:57:03 -07001706 * @override
rginda8ba33642011-12-14 12:31:31 -08001707 */
1708hterm.Terminal.prototype.getRowNode = function(index) {
1709 if (index < this.scrollbackRows_.length)
1710 return this.scrollbackRows_[index];
1711
1712 var screenIndex = index - this.scrollbackRows_.length;
1713 return this.screen_.rowsArray[screenIndex];
1714};
1715
1716/**
1717 * Return the text content for a given range of rows.
1718 *
1719 * This is a method from the RowProvider interface. The ScrollPort uses
1720 * it to fetch text content on demand when the user attempts to copy their
1721 * selection to the clipboard.
1722 *
Joel Hockey0f933582019-08-27 18:01:51 -07001723 * @param {number} start The zero-based row index to start from, measured
rginda8ba33642011-12-14 12:31:31 -08001724 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001725 * always have the largest indices.
Joel Hockey0f933582019-08-27 18:01:51 -07001726 * @param {number} end The zero-based row index to end on, measured
rginda8ba33642011-12-14 12:31:31 -08001727 * relative to the start of the scrollback buffer.
1728 * @return {string} A single string containing the text value of the range of
1729 * rows. Lines will be newline delimited, with no trailing newline.
1730 */
1731hterm.Terminal.prototype.getRowsText = function(start, end) {
1732 var ary = [];
1733 for (var i = start; i < end; i++) {
1734 var node = this.getRowNode(i);
1735 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001736 if (i < end - 1 && !node.getAttribute('line-overflow'))
1737 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001738 }
1739
rgindaa09e7332012-08-17 12:49:51 -07001740 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001741};
1742
1743/**
1744 * Return the text content for a given row.
1745 *
1746 * This is a method from the RowProvider interface. The ScrollPort uses
1747 * it to fetch text content on demand when the user attempts to copy their
1748 * selection to the clipboard.
1749 *
Joel Hockey0f933582019-08-27 18:01:51 -07001750 * @param {number} index The zero-based row index to return, measured
rginda8ba33642011-12-14 12:31:31 -08001751 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001752 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001753 * @return {string} A string containing the text value of the selected row.
1754 */
1755hterm.Terminal.prototype.getRowText = function(index) {
1756 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001757 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001758};
1759
1760/**
1761 * Return the total number of rows in the addressable screen and in the
1762 * scrollback buffer of this terminal.
1763 *
1764 * This is a method from the RowProvider interface. The ScrollPort uses
1765 * it to compute the size of the scrollbar.
1766 *
Joel Hockey0f933582019-08-27 18:01:51 -07001767 * @return {number} The number of rows in this terminal.
Joel Hockeyd4fca732019-09-20 16:57:03 -07001768 * @override
rginda8ba33642011-12-14 12:31:31 -08001769 */
1770hterm.Terminal.prototype.getRowCount = function() {
1771 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1772};
1773
1774/**
1775 * Create DOM nodes for new rows and append them to the end of the terminal.
1776 *
1777 * This is the only correct way to add a new DOM node for a row. Notice that
1778 * the new row is appended to the bottom of the list of rows, and does not
1779 * require renumbering (of the rowIndex property) of previous rows.
1780 *
1781 * If you think you want a new blank row somewhere in the middle of the
1782 * terminal, look into moveRows_().
1783 *
1784 * This method does not pay attention to vtScrollTop/Bottom, since you should
1785 * be using moveRows() in cases where they would matter.
1786 *
1787 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001788 *
1789 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001790 */
1791hterm.Terminal.prototype.appendRows_ = function(count) {
1792 var cursorRow = this.screen_.rowsArray.length;
1793 var offset = this.scrollbackRows_.length + cursorRow;
1794 for (var i = 0; i < count; i++) {
1795 var row = this.document_.createElement('x-row');
1796 row.appendChild(this.document_.createTextNode(''));
1797 row.rowIndex = offset + i;
1798 this.screen_.pushRow(row);
1799 }
1800
1801 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1802 if (extraRows > 0) {
1803 var ary = this.screen_.shiftRows(extraRows);
1804 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001805 if (this.scrollPort_.isScrolledEnd)
1806 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001807 }
1808
1809 if (cursorRow >= this.screen_.rowsArray.length)
1810 cursorRow = this.screen_.rowsArray.length - 1;
1811
rginda87b86462011-12-14 13:48:03 -08001812 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001813};
1814
1815/**
1816 * Relocate rows from one part of the addressable screen to another.
1817 *
1818 * This is used to recycle rows during VT scrolls (those which are driven
1819 * by VT commands, rather than by the user manipulating the scrollbar.)
1820 *
1821 * In this case, the blank lines scrolled into the scroll region are made of
1822 * the nodes we scrolled off. These have their rowIndex properties carefully
1823 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001824 *
1825 * @param {number} fromIndex The start index.
1826 * @param {number} count The number of rows to move.
1827 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001828 */
1829hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1830 var ary = this.screen_.removeRows(fromIndex, count);
1831 this.screen_.insertRows(toIndex, ary);
1832
1833 var start, end;
1834 if (fromIndex < toIndex) {
1835 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001836 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001837 } else {
1838 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001839 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001840 }
1841
1842 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001843 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001844};
1845
1846/**
1847 * Renumber the rowIndex property of the given range of rows.
1848 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001849 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08001850 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001851 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001852 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05001853 *
1854 * @param {number} start The start index.
1855 * @param {number} end The end index.
Joel Hockey0f933582019-08-27 18:01:51 -07001856 * @param {!hterm.Screen=} opt_screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08001857 */
Robert Ginda40932892012-12-10 17:26:40 -08001858hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1859 var screen = opt_screen || this.screen_;
1860
rginda8ba33642011-12-14 12:31:31 -08001861 var offset = this.scrollbackRows_.length;
1862 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001863 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001864 }
1865};
1866
1867/**
1868 * Print a string to the terminal.
1869 *
1870 * This respects the current insert and wraparound modes. It will add new lines
1871 * to the end of the terminal, scrolling off the top into the scrollback buffer
1872 * if necessary.
1873 *
1874 * The string is *not* parsed for escape codes. Use the interpret() method if
1875 * that's what you're after.
1876 *
Mike Frysingerfd449572019-09-23 03:18:14 -04001877 * @param {string} str The string to print.
rginda8ba33642011-12-14 12:31:31 -08001878 */
1879hterm.Terminal.prototype.print = function(str) {
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001880 this.scheduleSyncCursorPosition_();
1881
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001882 // Basic accessibility output for the screen reader.
Raymes Khoury177aec72018-06-26 10:58:53 +10001883 this.accessibilityReader_.announce(str);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001884
rgindaa9abdd82012-08-06 18:05:09 -07001885 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001886
Ricky Liang48f05cb2013-12-31 23:35:29 +08001887 var strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04001888 // Fun edge case: If the string only contains zero width codepoints (like
1889 // combining characters), we make sure to iterate at least once below.
1890 if (strWidth == 0 && str)
1891 strWidth = 1;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001892
1893 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001894 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1895 this.screen_.commitLineOverflow();
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001896 this.newLine(true);
rgindaa09e7332012-08-17 12:49:51 -07001897 }
rgindaa19afe22012-01-25 15:40:22 -08001898
Ricky Liang48f05cb2013-12-31 23:35:29 +08001899 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001900 var didOverflow = false;
1901 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001902
rgindaa9abdd82012-08-06 18:05:09 -07001903 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1904 didOverflow = true;
1905 count = this.screenSize.width - this.screen_.cursorPosition.column;
1906 }
rgindaa19afe22012-01-25 15:40:22 -08001907
rgindaa9abdd82012-08-06 18:05:09 -07001908 if (didOverflow && !this.options_.wraparound) {
1909 // If the string overflowed the line but wraparound is off, then the
1910 // last printed character should be the last of the string.
1911 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001912 substr = lib.wc.substr(str, startOffset, count - 1) +
1913 lib.wc.substr(str, strWidth - 1);
1914 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001915 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001916 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001917 }
rgindaa19afe22012-01-25 15:40:22 -08001918
Ricky Liang48f05cb2013-12-31 23:35:29 +08001919 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1920 for (var i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001921 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
1922 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001923
1924 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001925 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001926 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001927 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001928 }
1929 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001930 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07001931 }
1932
1933 this.screen_.maybeClipCurrentRow();
1934 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001935 }
rginda8ba33642011-12-14 12:31:31 -08001936
rginda9f5222b2012-03-05 11:53:28 -08001937 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001938 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001939};
1940
1941/**
rginda87b86462011-12-14 13:48:03 -08001942 * Set the VT scroll region.
1943 *
rginda87b86462011-12-14 13:48:03 -08001944 * This also resets the cursor position to the absolute (0, 0) position, since
1945 * that's what xterm appears to do.
1946 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001947 * Setting the scroll region to the full height of the terminal will clear
1948 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1949 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1950 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1951 * continue to work as most users would expect.
1952 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001953 * @param {?number} scrollTop The zero-based top of the scroll region.
1954 * @param {?number} scrollBottom The zero-based bottom of the scroll region,
rginda87b86462011-12-14 13:48:03 -08001955 * inclusive.
1956 */
1957hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001958 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001959 this.vtScrollTop_ = null;
1960 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001961 } else {
1962 this.vtScrollTop_ = scrollTop;
1963 this.vtScrollBottom_ = scrollBottom;
1964 }
rginda87b86462011-12-14 13:48:03 -08001965};
1966
1967/**
rginda8ba33642011-12-14 12:31:31 -08001968 * Return the top row index according to the VT.
1969 *
1970 * This will return 0 unless the terminal has been told to restrict scrolling
1971 * to some lower row. It is used for some VT cursor positioning and scrolling
1972 * commands.
1973 *
Joel Hockey0f933582019-08-27 18:01:51 -07001974 * @return {number} The topmost row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001975 */
1976hterm.Terminal.prototype.getVTScrollTop = function() {
1977 if (this.vtScrollTop_ != null)
1978 return this.vtScrollTop_;
1979
1980 return 0;
rginda87b86462011-12-14 13:48:03 -08001981};
rginda8ba33642011-12-14 12:31:31 -08001982
1983/**
1984 * Return the bottom row index according to the VT.
1985 *
1986 * This will return the height of the terminal unless the it has been told to
1987 * restrict scrolling to some higher row. It is used for some VT cursor
1988 * positioning and scrolling commands.
1989 *
Joel Hockey0f933582019-08-27 18:01:51 -07001990 * @return {number} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001991 */
1992hterm.Terminal.prototype.getVTScrollBottom = function() {
1993 if (this.vtScrollBottom_ != null)
1994 return this.vtScrollBottom_;
1995
rginda87b86462011-12-14 13:48:03 -08001996 return this.screenSize.height - 1;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001997};
rginda8ba33642011-12-14 12:31:31 -08001998
1999/**
2000 * Process a '\n' character.
2001 *
2002 * If the cursor is on the final row of the terminal this will append a new
2003 * blank row to the screen and scroll the topmost row into the scrollback
2004 * buffer.
2005 *
2006 * Otherwise, this moves the cursor to column zero of the next row.
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002007 *
2008 * @param {boolean=} dueToOverflow Whether the newline is due to wraparound of
2009 * the terminal.
rginda8ba33642011-12-14 12:31:31 -08002010 */
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002011hterm.Terminal.prototype.newLine = function(dueToOverflow = false) {
2012 if (!dueToOverflow)
2013 this.accessibilityReader_.newLine();
2014
Robert Ginda9937abc2013-07-25 16:09:23 -07002015 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
2016 this.screen_.rowsArray.length - 1);
2017
2018 if (this.vtScrollBottom_ != null) {
2019 // A VT Scroll region is active, we never append new rows.
2020 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
2021 // We're at the end of the VT Scroll Region, perform a VT scroll.
2022 this.vtScrollUp(1);
2023 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
2024 } else if (cursorAtEndOfScreen) {
2025 // We're at the end of the screen, the only thing to do is put the
2026 // cursor to column 0.
2027 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
2028 } else {
2029 // Anywhere else, advance the cursor row, and reset the column.
2030 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
2031 }
2032 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07002033 // We're at the end of the screen. Append a new row to the terminal,
2034 // shifting the top row into the scrollback.
2035 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08002036 } else {
rginda87b86462011-12-14 13:48:03 -08002037 // Anywhere else in the screen just moves the cursor.
2038 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08002039 }
2040};
2041
2042/**
2043 * Like newLine(), except maintain the cursor column.
2044 */
2045hterm.Terminal.prototype.lineFeed = function() {
2046 var column = this.screen_.cursorPosition.column;
2047 this.newLine();
2048 this.setCursorColumn(column);
2049};
2050
2051/**
rginda87b86462011-12-14 13:48:03 -08002052 * If autoCarriageReturn is set then newLine(), else lineFeed().
2053 */
2054hterm.Terminal.prototype.formFeed = function() {
2055 if (this.options_.autoCarriageReturn) {
2056 this.newLine();
2057 } else {
2058 this.lineFeed();
2059 }
2060};
2061
2062/**
2063 * Move the cursor up one row, possibly inserting a blank line.
2064 *
2065 * The cursor column is not changed.
2066 */
2067hterm.Terminal.prototype.reverseLineFeed = function() {
2068 var scrollTop = this.getVTScrollTop();
2069 var currentRow = this.screen_.cursorPosition.row;
2070
2071 if (currentRow == scrollTop) {
2072 this.insertLines(1);
2073 } else {
2074 this.setAbsoluteCursorRow(currentRow - 1);
2075 }
2076};
2077
2078/**
rginda8ba33642011-12-14 12:31:31 -08002079 * Replace all characters to the left of the current cursor with the space
2080 * character.
2081 *
2082 * TODO(rginda): This should probably *remove* the characters (not just replace
2083 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07002084 * position.
rginda8ba33642011-12-14 12:31:31 -08002085 */
2086hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08002087 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002088 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002089 const count = cursor.column + 1;
Mike Frysinger73e56462019-07-17 00:23:46 -05002090 this.screen_.overwriteString(' '.repeat(count), count);
rginda87b86462011-12-14 13:48:03 -08002091 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002092};
2093
2094/**
David Benjamin684a9b72012-05-01 17:19:58 -04002095 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08002096 *
2097 * The cursor position is unchanged.
2098 *
Robert Gindaf2547f12012-10-25 20:36:21 -07002099 * If the current background color is not the default background color this
2100 * will insert spaces rather than delete. This is unfortunate because the
2101 * trailing space will affect text selection, but it's difficult to come up
2102 * with a way to style empty space that wouldn't trip up the hterm.Screen
2103 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07002104 *
2105 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
2106 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
2107 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05002108 *
Joel Hockey0f933582019-08-27 18:01:51 -07002109 * @param {number=} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08002110 */
2111hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07002112 if (this.screen_.cursorPosition.overflow)
2113 return;
2114
Robert Ginda7fd57082012-09-25 14:41:47 -07002115 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
2116 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07002117
2118 if (this.screen_.textAttributes.background ===
2119 this.screen_.textAttributes.DEFAULT_COLOR) {
2120 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08002121 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07002122 this.screen_.cursorPosition.column + count) {
2123 this.screen_.deleteChars(count);
2124 this.clearCursorOverflow();
2125 return;
2126 }
2127 }
2128
rginda87b86462011-12-14 13:48:03 -08002129 var cursor = this.saveCursor();
Mike Frysinger73e56462019-07-17 00:23:46 -05002130 this.screen_.overwriteString(' '.repeat(count), count);
rginda87b86462011-12-14 13:48:03 -08002131 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002132 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002133};
2134
2135/**
2136 * Erase the current line.
2137 *
2138 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002139 */
2140hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08002141 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002142 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08002143 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002144 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002145};
2146
2147/**
David Benjamina08d78f2012-05-05 00:28:49 -04002148 * Erase all characters from the start of the screen to the current cursor
2149 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002150 *
2151 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002152 */
2153hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08002154 var cursor = this.saveCursor();
2155
2156 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002157
David Benjamina08d78f2012-05-05 00:28:49 -04002158 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002159 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002160 this.screen_.clearCursorRow();
2161 }
2162
rginda87b86462011-12-14 13:48:03 -08002163 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002164 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002165};
2166
2167/**
2168 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002169 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002170 *
2171 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002172 */
2173hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08002174 var cursor = this.saveCursor();
2175
2176 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002177
David Benjamina08d78f2012-05-05 00:28:49 -04002178 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08002179 for (var i = cursor.row + 1; i <= bottom; i++) {
2180 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002181 this.screen_.clearCursorRow();
2182 }
2183
rginda87b86462011-12-14 13:48:03 -08002184 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002185 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002186};
2187
2188/**
2189 * Fill the terminal with a given character.
2190 *
2191 * This methods does not respect the VT scroll region.
2192 *
2193 * @param {string} ch The character to use for the fill.
2194 */
2195hterm.Terminal.prototype.fill = function(ch) {
2196 var cursor = this.saveCursor();
2197
2198 this.setAbsoluteCursorPosition(0, 0);
2199 for (var row = 0; row < this.screenSize.height; row++) {
2200 for (var col = 0; col < this.screenSize.width; col++) {
2201 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002202 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002203 }
2204 }
2205
2206 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002207};
2208
2209/**
rginda9ea433c2012-03-16 11:57:00 -07002210 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002211 *
rginda9ea433c2012-03-16 11:57:00 -07002212 * This does not respect the scroll region.
2213 *
Joel Hockey0f933582019-08-27 18:01:51 -07002214 * @param {!hterm.Screen=} opt_screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002215 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002216 */
rginda9ea433c2012-03-16 11:57:00 -07002217hterm.Terminal.prototype.clearHome = function(opt_screen) {
2218 var screen = opt_screen || this.screen_;
2219 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002220
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002221 this.accessibilityReader_.clear();
2222
rginda11057d52012-04-25 12:29:56 -07002223 if (bottom == 0) {
2224 // Empty screen, nothing to do.
2225 return;
2226 }
2227
rgindae4d29232012-01-19 10:47:13 -08002228 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002229 screen.setCursorPosition(i, 0);
2230 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002231 }
2232
rginda9ea433c2012-03-16 11:57:00 -07002233 screen.setCursorPosition(0, 0);
2234};
2235
2236/**
2237 * Erase the entire display without changing the cursor position.
2238 *
2239 * The cursor position is unchanged. This does not respect the scroll
2240 * region.
2241 *
Joel Hockey0f933582019-08-27 18:01:51 -07002242 * @param {!hterm.Screen=} opt_screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002243 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002244 */
2245hterm.Terminal.prototype.clear = function(opt_screen) {
2246 var screen = opt_screen || this.screen_;
2247 var cursor = screen.cursorPosition.clone();
2248 this.clearHome(screen);
2249 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002250};
2251
2252/**
2253 * VT command to insert lines at the current cursor row.
2254 *
2255 * This respects the current scroll region. Rows pushed off the bottom are
2256 * lost (they won't show up in the scrollback buffer).
2257 *
Joel Hockey0f933582019-08-27 18:01:51 -07002258 * @param {number} count The number of lines to insert.
rginda8ba33642011-12-14 12:31:31 -08002259 */
2260hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07002261 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002262
2263 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002264 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002265
Robert Ginda579186b2012-09-26 11:40:04 -07002266 // The moveCount is the number of rows we need to relocate to make room for
2267 // the new row(s). The count is the distance to move them.
2268 var moveCount = bottom - cursorRow - count + 1;
2269 if (moveCount)
2270 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08002271
Robert Ginda579186b2012-09-26 11:40:04 -07002272 for (var i = count - 1; i >= 0; i--) {
2273 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002274 this.screen_.clearCursorRow();
2275 }
rginda8ba33642011-12-14 12:31:31 -08002276};
2277
2278/**
2279 * VT command to delete lines at the current cursor row.
2280 *
2281 * New rows are added to the bottom of scroll region to take their place. New
2282 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002283 *
2284 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002285 */
2286hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08002287 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002288
rginda87b86462011-12-14 13:48:03 -08002289 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08002290 var bottom = this.getVTScrollBottom();
2291
rginda87b86462011-12-14 13:48:03 -08002292 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002293 count = Math.min(count, maxCount);
2294
rginda87b86462011-12-14 13:48:03 -08002295 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08002296 if (count != maxCount)
2297 this.moveRows_(top, count, moveStart);
2298
2299 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002300 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002301 this.screen_.clearCursorRow();
2302 }
2303
rginda87b86462011-12-14 13:48:03 -08002304 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002305 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002306};
2307
2308/**
2309 * Inserts the given number of spaces at the current cursor position.
2310 *
rginda87b86462011-12-14 13:48:03 -08002311 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002312 *
2313 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002314 */
2315hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08002316 var cursor = this.saveCursor();
2317
Mike Frysinger73e56462019-07-17 00:23:46 -05002318 const ws = ' '.repeat(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002319 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002320 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002321
2322 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002323 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002324};
2325
2326/**
2327 * Forward-delete the specified number of characters starting at the cursor
2328 * position.
2329 *
Joel Hockey0f933582019-08-27 18:01:51 -07002330 * @param {number} count The number of characters to delete.
rginda8ba33642011-12-14 12:31:31 -08002331 */
2332hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002333 var deleted = this.screen_.deleteChars(count);
2334 if (deleted && !this.screen_.textAttributes.isDefault()) {
2335 var cursor = this.saveCursor();
2336 this.setCursorColumn(this.screenSize.width - deleted);
Mike Frysinger73e56462019-07-17 00:23:46 -05002337 this.screen_.insertString(' '.repeat(deleted));
Robert Ginda7fd57082012-09-25 14:41:47 -07002338 this.restoreCursor(cursor);
2339 }
2340
David Benjamin54e8bf62012-06-01 22:31:40 -04002341 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002342};
2343
2344/**
2345 * Shift rows in the scroll region upwards by a given number of lines.
2346 *
2347 * New rows are inserted at the bottom of the scroll region to fill the
2348 * vacated rows. The new rows not filled out with the current text attributes.
2349 *
2350 * This function does not affect the scrollback rows at all. Rows shifted
2351 * off the top are lost.
2352 *
rginda87b86462011-12-14 13:48:03 -08002353 * The cursor position is not altered.
2354 *
Joel Hockey0f933582019-08-27 18:01:51 -07002355 * @param {number} count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002356 */
2357hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002358 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002359
rginda87b86462011-12-14 13:48:03 -08002360 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002361 this.deleteLines(count);
2362
rginda87b86462011-12-14 13:48:03 -08002363 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002364};
2365
2366/**
2367 * Shift rows below the cursor down by a given number of lines.
2368 *
2369 * This function respects the current scroll region.
2370 *
2371 * New rows are inserted at the top of the scroll region to fill the
2372 * vacated rows. The new rows not filled out with the current text attributes.
2373 *
2374 * This function does not affect the scrollback rows at all. Rows shifted
2375 * off the bottom are lost.
2376 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07002377 * @param {number} count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002378 */
Joel Hockeyd4fca732019-09-20 16:57:03 -07002379hterm.Terminal.prototype.vtScrollDown = function(count) {
rginda87b86462011-12-14 13:48:03 -08002380 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002381
rginda87b86462011-12-14 13:48:03 -08002382 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
Joel Hockeyd4fca732019-09-20 16:57:03 -07002383 this.insertLines(count);
rginda8ba33642011-12-14 12:31:31 -08002384
rginda87b86462011-12-14 13:48:03 -08002385 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002386};
2387
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002388/**
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002389 * Enable accessibility-friendly features that have a performance impact.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002390 *
2391 * This will generate additional DOM nodes in an aria-live region that will
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002392 * cause Assitive Technology to announce the output of the terminal. It also
2393 * enables other features that aid assistive technology. All the features gated
2394 * behind this flag have a performance impact on the terminal which is why they
2395 * are made optional.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002396 *
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002397 * @param {boolean} enabled Whether to enable accessibility-friendly features.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002398 */
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002399hterm.Terminal.prototype.setAccessibilityEnabled = function(enabled) {
Raymes Khoury177aec72018-06-26 10:58:53 +10002400 this.accessibilityReader_.setAccessibilityEnabled(enabled);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002401};
rginda87b86462011-12-14 13:48:03 -08002402
rginda8ba33642011-12-14 12:31:31 -08002403/**
2404 * Set the cursor position.
2405 *
2406 * The cursor row is relative to the scroll region if the terminal has
2407 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2408 *
Joel Hockey0f933582019-08-27 18:01:51 -07002409 * @param {number} row The new zero-based cursor row.
2410 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002411 */
2412hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2413 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002414 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002415 } else {
rginda87b86462011-12-14 13:48:03 -08002416 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002417 }
rginda87b86462011-12-14 13:48:03 -08002418};
rginda8ba33642011-12-14 12:31:31 -08002419
Evan Jones2600d4f2016-12-06 09:29:36 -05002420/**
2421 * Move the cursor relative to its current position.
2422 *
2423 * @param {number} row
2424 * @param {number} column
2425 */
rginda87b86462011-12-14 13:48:03 -08002426hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2427 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002428 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2429 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002430 this.screen_.setCursorPosition(row, column);
2431};
2432
Evan Jones2600d4f2016-12-06 09:29:36 -05002433/**
2434 * Move the cursor to the specified position.
2435 *
2436 * @param {number} row
2437 * @param {number} column
2438 */
rginda87b86462011-12-14 13:48:03 -08002439hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002440 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2441 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002442 this.screen_.setCursorPosition(row, column);
2443};
2444
2445/**
2446 * Set the cursor column.
2447 *
Joel Hockey0f933582019-08-27 18:01:51 -07002448 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002449 */
2450hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002451 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002452};
2453
2454/**
2455 * Return the cursor column.
2456 *
Joel Hockey0f933582019-08-27 18:01:51 -07002457 * @return {number} The zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002458 */
2459hterm.Terminal.prototype.getCursorColumn = function() {
2460 return this.screen_.cursorPosition.column;
2461};
2462
2463/**
2464 * Set the cursor row.
2465 *
2466 * The cursor row is relative to the scroll region if the terminal has
2467 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2468 *
Joel Hockey0f933582019-08-27 18:01:51 -07002469 * @param {number} row The new cursor row.
rginda8ba33642011-12-14 12:31:31 -08002470 */
rginda87b86462011-12-14 13:48:03 -08002471hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2472 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002473};
2474
2475/**
2476 * Return the cursor row.
2477 *
Joel Hockey0f933582019-08-27 18:01:51 -07002478 * @return {number} The zero-based cursor row.
rginda8ba33642011-12-14 12:31:31 -08002479 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002480hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002481 return this.screen_.cursorPosition.row;
2482};
2483
2484/**
2485 * Request that the ScrollPort redraw itself soon.
2486 *
2487 * The redraw will happen asynchronously, soon after the call stack winds down.
2488 * Multiple calls will be coalesced into a single redraw.
2489 */
2490hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002491 if (this.timeouts_.redraw)
2492 return;
rginda8ba33642011-12-14 12:31:31 -08002493
2494 var self = this;
rginda87b86462011-12-14 13:48:03 -08002495 this.timeouts_.redraw = setTimeout(function() {
2496 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002497 self.scrollPort_.redraw_();
2498 }, 0);
2499};
2500
2501/**
2502 * Request that the ScrollPort be scrolled to the bottom.
2503 *
2504 * The scroll will happen asynchronously, soon after the call stack winds down.
2505 * Multiple calls will be coalesced into a single scroll.
2506 *
2507 * This affects the scrollbar position of the ScrollPort, and has nothing to
2508 * do with the VT scroll commands.
2509 */
2510hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2511 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002512 return;
rginda8ba33642011-12-14 12:31:31 -08002513
2514 var self = this;
2515 this.timeouts_.scrollDown = setTimeout(function() {
2516 delete self.timeouts_.scrollDown;
2517 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2518 }, 10);
2519};
2520
2521/**
2522 * Move the cursor up a specified number of rows.
2523 *
Joel Hockey0f933582019-08-27 18:01:51 -07002524 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002525 */
2526hterm.Terminal.prototype.cursorUp = function(count) {
Joel Hockey0f933582019-08-27 18:01:51 -07002527 this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002528};
2529
2530/**
2531 * Move the cursor down a specified number of rows.
2532 *
Joel Hockey0f933582019-08-27 18:01:51 -07002533 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002534 */
2535hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002536 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002537 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2538 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2539 this.screenSize.height - 1);
2540
rgindacbbd7482012-06-13 15:06:16 -07002541 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002542 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002543 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002544};
2545
2546/**
2547 * Move the cursor left a specified number of columns.
2548 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002549 * If reverse wraparound mode is enabled and the previous row wrapped into
2550 * the current row then we back up through the wraparound as well.
2551 *
Joel Hockey0f933582019-08-27 18:01:51 -07002552 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002553 */
2554hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002555 count = count || 1;
2556
2557 if (count < 1)
2558 return;
2559
2560 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002561 if (this.options_.reverseWraparound) {
2562 if (this.screen_.cursorPosition.overflow) {
2563 // If this cursor is in the right margin, consume one count to get it
2564 // back to the last column. This only applies when we're in reverse
2565 // wraparound mode.
2566 count--;
2567 this.clearCursorOverflow();
2568
2569 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002570 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002571 }
2572
Robert Gindabfb32622014-07-17 13:20:27 -07002573 var newRow = this.screen_.cursorPosition.row;
2574 var newColumn = currentColumn - count;
2575 if (newColumn < 0) {
2576 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2577 if (newRow < 0) {
2578 // xterm also wraps from row 0 to the last row.
2579 newRow = this.screenSize.height + newRow % this.screenSize.height;
2580 }
2581 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2582 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002583
Robert Gindabfb32622014-07-17 13:20:27 -07002584 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2585
2586 } else {
2587 var newColumn = Math.max(currentColumn - count, 0);
2588 this.setCursorColumn(newColumn);
2589 }
rginda8ba33642011-12-14 12:31:31 -08002590};
2591
2592/**
2593 * Move the cursor right a specified number of columns.
2594 *
Joel Hockey0f933582019-08-27 18:01:51 -07002595 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002596 */
2597hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002598 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002599
2600 if (count < 1)
2601 return;
2602
rgindacbbd7482012-06-13 15:06:16 -07002603 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002604 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002605 this.setCursorColumn(column);
2606};
2607
2608/**
2609 * Reverse the foreground and background colors of the terminal.
2610 *
2611 * This only affects text that was drawn with no attributes.
2612 *
2613 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2614 * been drawn with attributes that happen to coincide with the default
2615 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002616 *
2617 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002618 */
2619hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002620 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002621 if (state) {
Mike Frysinger31cb1562017-07-31 23:44:18 -04002622 this.scrollPort_.setForegroundColor(this.backgroundColor_);
2623 this.scrollPort_.setBackgroundColor(this.foregroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002624 } else {
Mike Frysinger31cb1562017-07-31 23:44:18 -04002625 this.scrollPort_.setForegroundColor(this.foregroundColor_);
2626 this.scrollPort_.setBackgroundColor(this.backgroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002627 }
2628};
2629
2630/**
rginda87b86462011-12-14 13:48:03 -08002631 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002632 *
2633 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002634 */
2635hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002636 this.cursorNode_.style.backgroundColor =
2637 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002638
2639 var self = this;
2640 setTimeout(function() {
Matheus Fernandes2d733082017-09-11 06:43:01 -04002641 self.restyleCursor_();
rginda6d397402012-01-17 10:58:29 -08002642 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002643
Michael Kelly485ecd12014-06-09 11:41:56 -04002644 // bellSquelchTimeout_ affects both audio and notification bells.
2645 if (this.bellSquelchTimeout_)
2646 return;
2647
Robert Ginda92e18102013-03-14 13:56:37 -07002648 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002649 this.bellAudio_.play();
Joel Hockeyd4fca732019-09-20 16:57:03 -07002650 this.bellSequelchTimeout_ = setTimeout(() => {
2651 this.bellSquelchTimeout_ = null;
2652 }, 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002653 } else {
Joel Hockeyd4fca732019-09-20 16:57:03 -07002654 this.bellSquelchTimeout_ = null;
Robert Ginda92e18102013-03-14 13:56:37 -07002655 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002656
2657 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingera5fb83c2017-06-22 14:48:35 -07002658 var n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002659 this.bellNotificationList_.push(n);
2660 // TODO: Should we try to raise the window here?
2661 n.onclick = function() { self.closeBellNotifications_(); };
2662 }
rginda87b86462011-12-14 13:48:03 -08002663};
2664
2665/**
rginda8ba33642011-12-14 12:31:31 -08002666 * Set the origin mode bit.
2667 *
2668 * If origin mode is on, certain VT cursor and scrolling commands measure their
2669 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2670 * to the top of the addressable screen.
2671 *
2672 * Defaults to off.
2673 *
2674 * @param {boolean} state True to set origin mode, false to unset.
2675 */
2676hterm.Terminal.prototype.setOriginMode = function(state) {
2677 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002678 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002679};
2680
2681/**
2682 * Set the insert mode bit.
2683 *
2684 * If insert mode is on, existing text beyond the cursor position will be
2685 * shifted right to make room for new text. Otherwise, new text overwrites
2686 * any existing text.
2687 *
2688 * Defaults to off.
2689 *
2690 * @param {boolean} state True to set insert mode, false to unset.
2691 */
2692hterm.Terminal.prototype.setInsertMode = function(state) {
2693 this.options_.insertMode = state;
2694};
2695
2696/**
rginda87b86462011-12-14 13:48:03 -08002697 * Set the auto carriage return bit.
2698 *
2699 * If auto carriage return is on then a formfeed character is interpreted
2700 * as a newline, otherwise it's the same as a linefeed. The difference boils
2701 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002702 *
2703 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002704 */
2705hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2706 this.options_.autoCarriageReturn = state;
2707};
2708
2709/**
rginda8ba33642011-12-14 12:31:31 -08002710 * Set the wraparound mode bit.
2711 *
2712 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2713 * to the start of the following row. Otherwise, the cursor is clamped to the
2714 * end of the screen and attempts to write past it are ignored.
2715 *
2716 * Defaults to on.
2717 *
2718 * @param {boolean} state True to set wraparound mode, false to unset.
2719 */
2720hterm.Terminal.prototype.setWraparound = function(state) {
2721 this.options_.wraparound = state;
2722};
2723
2724/**
2725 * Set the reverse-wraparound mode bit.
2726 *
2727 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2728 * to the end of the previous row. Otherwise, the cursor is clamped to column
2729 * 0.
2730 *
2731 * Defaults to off.
2732 *
2733 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2734 */
2735hterm.Terminal.prototype.setReverseWraparound = function(state) {
2736 this.options_.reverseWraparound = state;
2737};
2738
2739/**
2740 * Selects between the primary and alternate screens.
2741 *
2742 * If alternate mode is on, the alternate screen is active. Otherwise the
2743 * primary screen is active.
2744 *
2745 * Swapping screens has no effect on the scrollback buffer.
2746 *
2747 * Each screen maintains its own cursor position.
2748 *
2749 * Defaults to off.
2750 *
2751 * @param {boolean} state True to set alternate mode, false to unset.
2752 */
2753hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002754 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002755 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2756
rginda35c456b2012-02-09 17:29:05 -08002757 if (this.screen_.rowsArray.length &&
2758 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2759 // If the screen changed sizes while we were away, our rowIndexes may
2760 // be incorrect.
2761 var offset = this.scrollbackRows_.length;
2762 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002763 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002764 ary[i].rowIndex = offset + i;
2765 }
2766 }
rginda8ba33642011-12-14 12:31:31 -08002767
rginda35c456b2012-02-09 17:29:05 -08002768 this.realizeWidth_(this.screenSize.width);
2769 this.realizeHeight_(this.screenSize.height);
2770 this.scrollPort_.syncScrollHeight();
2771 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002772
rginda6d397402012-01-17 10:58:29 -08002773 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002774 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002775};
2776
2777/**
2778 * Set the cursor-blink mode bit.
2779 *
2780 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2781 * a visible cursor does not blink.
2782 *
2783 * You should make sure to turn blinking off if you're going to dispose of a
2784 * terminal, otherwise you'll leak a timeout.
2785 *
2786 * Defaults to on.
2787 *
2788 * @param {boolean} state True to set cursor-blink mode, false to unset.
2789 */
2790hterm.Terminal.prototype.setCursorBlink = function(state) {
2791 this.options_.cursorBlink = state;
2792
2793 if (!state && this.timeouts_.cursorBlink) {
2794 clearTimeout(this.timeouts_.cursorBlink);
2795 delete this.timeouts_.cursorBlink;
2796 }
2797
2798 if (this.options_.cursorVisible)
2799 this.setCursorVisible(true);
2800};
2801
2802/**
2803 * Set the cursor-visible mode bit.
2804 *
2805 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2806 *
2807 * Defaults to on.
2808 *
2809 * @param {boolean} state True to set cursor-visible mode, false to unset.
2810 */
2811hterm.Terminal.prototype.setCursorVisible = function(state) {
2812 this.options_.cursorVisible = state;
2813
2814 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002815 if (this.timeouts_.cursorBlink) {
2816 clearTimeout(this.timeouts_.cursorBlink);
2817 delete this.timeouts_.cursorBlink;
2818 }
rginda87b86462011-12-14 13:48:03 -08002819 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002820 return;
2821 }
2822
rginda87b86462011-12-14 13:48:03 -08002823 this.syncCursorPosition_();
2824
2825 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002826
2827 if (this.options_.cursorBlink) {
2828 if (this.timeouts_.cursorBlink)
2829 return;
2830
Robert Gindaea2183e2014-07-17 09:51:51 -07002831 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002832 } else {
2833 if (this.timeouts_.cursorBlink) {
2834 clearTimeout(this.timeouts_.cursorBlink);
2835 delete this.timeouts_.cursorBlink;
2836 }
2837 }
2838};
2839
2840/**
Mike Frysinger225c99d2019-10-20 14:02:37 -06002841 * Pause blinking temporarily.
2842 *
2843 * When the cursor moves around, it can be helpful to momentarily pause the
2844 * blinking. This could be when the user is typing in things, or when they're
2845 * moving around with the arrow keys.
2846 */
2847hterm.Terminal.prototype.pauseCursorBlink_ = function() {
2848 if (!this.options_.cursorBlink) {
2849 return;
2850 }
2851
2852 this.cursorBlinkPause_ = true;
2853
2854 // If a timeout is already pending, reset the clock due to the new input.
2855 if (this.timeouts_.cursorBlinkPause) {
2856 clearTimeout(this.timeouts_.cursorBlinkPause);
2857 }
2858 // After 500ms, resume blinking. That seems like a good balance between user
2859 // input timings & responsiveness to resume.
2860 this.timeouts_.cursorBlinkPause = setTimeout(() => {
2861 delete this.timeouts_.cursorBlinkPause;
2862 this.cursorBlinkPause_ = false;
2863 }, 500);
2864};
2865
2866/**
rginda87b86462011-12-14 13:48:03 -08002867 * Synchronizes the visible cursor and document selection with the current
2868 * cursor coordinates.
Raymes Khourye5d48982018-08-02 09:08:32 +10002869 *
2870 * @return {boolean} True if the cursor is onscreen and synced.
rginda8ba33642011-12-14 12:31:31 -08002871 */
2872hterm.Terminal.prototype.syncCursorPosition_ = function() {
2873 var topRowIndex = this.scrollPort_.getTopRowIndex();
2874 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2875 var cursorRowIndex = this.scrollbackRows_.length +
2876 this.screen_.cursorPosition.row;
2877
Raymes Khoury15697f42018-07-17 11:37:18 +10002878 let forceSyncSelection = false;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002879 if (this.accessibilityReader_.accessibilityEnabled) {
2880 // Report the new position of the cursor for accessibility purposes.
2881 const cursorColumnIndex = this.screen_.cursorPosition.column;
2882 const cursorLineText =
2883 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
Raymes Khoury15697f42018-07-17 11:37:18 +10002884 // This will force the selection to be sync'd to the cursor position if the
2885 // user has pressed a key. Generally we would only sync the cursor position
2886 // when selection is collapsed so that if the user has selected something
2887 // we don't clear the selection by moving the selection. However when a
2888 // screen reader is used, it's intuitive for entering a key to move the
2889 // selection to the cursor.
2890 forceSyncSelection = this.accessibilityReader_.hasUserGesture;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002891 this.accessibilityReader_.afterCursorChange(
2892 cursorLineText, cursorRowIndex, cursorColumnIndex);
2893 }
2894
rginda8ba33642011-12-14 12:31:31 -08002895 if (cursorRowIndex > bottomRowIndex) {
2896 // Cursor is scrolled off screen, move it outside of the visible area.
Mike Frysinger44c32202017-08-05 01:13:09 -04002897 this.setCssVar('cursor-offset-row', '-1');
Raymes Khourye5d48982018-08-02 09:08:32 +10002898 return false;
rginda8ba33642011-12-14 12:31:31 -08002899 }
2900
Robert Gindab837c052014-08-11 11:17:51 -07002901 if (this.options_.cursorVisible &&
2902 this.cursorNode_.style.display == 'none') {
2903 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2904 this.cursorNode_.style.display = '';
2905 }
2906
Mike Frysinger44c32202017-08-05 01:13:09 -04002907 // Position the cursor using CSS variable math. If we do the math in JS,
2908 // the float math will end up being more precise than the CSS which will
2909 // cause the cursor tracking to be off.
2910 this.setCssVar(
2911 'cursor-offset-row',
2912 `${cursorRowIndex - topRowIndex} + ` +
2913 `${this.scrollPort_.visibleRowTopMargin}px`);
2914 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08002915
2916 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04002917 '(' + this.screen_.cursorPosition.column +
2918 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08002919 ')');
2920
2921 // Update the caret for a11y purposes.
2922 var selection = this.document_.getSelection();
Raymes Khoury15697f42018-07-17 11:37:18 +10002923 if (selection && (selection.isCollapsed || forceSyncSelection)) {
rginda87b86462011-12-14 13:48:03 -08002924 this.screen_.syncSelectionCaret(selection);
Raymes Khoury15697f42018-07-17 11:37:18 +10002925 }
Raymes Khourye5d48982018-08-02 09:08:32 +10002926 return true;
rginda8ba33642011-12-14 12:31:31 -08002927};
2928
Robert Gindafb1be6a2013-12-11 11:56:22 -08002929/**
2930 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2931 * and character cell dimensions.
2932 */
Robert Ginda830583c2013-08-07 13:20:46 -07002933hterm.Terminal.prototype.restyleCursor_ = function() {
2934 var shape = this.cursorShape_;
2935
2936 if (this.cursorNode_.getAttribute('focus') == 'false') {
2937 // Always show a block cursor when unfocused.
2938 shape = hterm.Terminal.cursorShape.BLOCK;
2939 }
2940
2941 var style = this.cursorNode_.style;
2942
2943 switch (shape) {
2944 case hterm.Terminal.cursorShape.BEAM:
Robert Ginda830583c2013-08-07 13:20:46 -07002945 style.backgroundColor = 'transparent';
Joel Hockeyd4fca732019-09-20 16:57:03 -07002946 style.borderBottomStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07002947 style.borderLeftStyle = 'solid';
2948 break;
2949
2950 case hterm.Terminal.cursorShape.UNDERLINE:
Robert Ginda830583c2013-08-07 13:20:46 -07002951 style.backgroundColor = 'transparent';
2952 style.borderBottomStyle = 'solid';
Joel Hockeyd4fca732019-09-20 16:57:03 -07002953 style.borderLeftStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07002954 break;
2955
2956 default:
Mike Frysinger2fd079a2018-09-02 01:46:12 -04002957 style.backgroundColor = 'var(--hterm-cursor-color)';
Joel Hockeyd4fca732019-09-20 16:57:03 -07002958 style.borderBottomStyle = '';
2959 style.borderLeftStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07002960 break;
2961 }
2962};
2963
rginda8ba33642011-12-14 12:31:31 -08002964/**
2965 * Synchronizes the visible cursor with the current cursor coordinates.
2966 *
2967 * The sync will happen asynchronously, soon after the call stack winds down.
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002968 * Multiple calls will be coalesced into a single sync. This should be called
2969 * prior to the cursor actually changing position.
rginda8ba33642011-12-14 12:31:31 -08002970 */
2971hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2972 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002973 return;
rginda8ba33642011-12-14 12:31:31 -08002974
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002975 if (this.accessibilityReader_.accessibilityEnabled) {
2976 // Report the previous position of the cursor for accessibility purposes.
2977 const cursorRowIndex = this.scrollbackRows_.length +
2978 this.screen_.cursorPosition.row;
2979 const cursorColumnIndex = this.screen_.cursorPosition.column;
2980 const cursorLineText =
2981 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
2982 this.accessibilityReader_.beforeCursorChange(
2983 cursorLineText, cursorRowIndex, cursorColumnIndex);
2984 }
2985
rginda8ba33642011-12-14 12:31:31 -08002986 var self = this;
2987 this.timeouts_.syncCursor = setTimeout(function() {
2988 self.syncCursorPosition_();
2989 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002990 }, 0);
2991};
2992
rgindacc2996c2012-02-24 14:59:31 -08002993/**
rgindaf522ce02012-04-17 17:49:17 -07002994 * Show or hide the zoom warning.
2995 *
2996 * The zoom warning is a message warning the user that their browser zoom must
2997 * be set to 100% in order for hterm to function properly.
2998 *
2999 * @param {boolean} state True to show the message, false to hide it.
3000 */
3001hterm.Terminal.prototype.showZoomWarning_ = function(state) {
3002 if (!this.zoomWarningNode_) {
3003 if (!state)
3004 return;
3005
3006 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04003007 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07003008 this.zoomWarningNode_.style.cssText = (
3009 'color: black;' +
3010 'background-color: #ff2222;' +
3011 'font-size: large;' +
3012 'border-radius: 8px;' +
3013 'opacity: 0.75;' +
3014 'padding: 0.2em 0.5em 0.2em 0.5em;' +
3015 'top: 0.5em;' +
3016 'right: 1.2em;' +
3017 'position: absolute;' +
3018 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003019 '-webkit-user-select: none;' +
3020 '-moz-text-size-adjust: none;' +
3021 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05003022
3023 this.zoomWarningNode_.addEventListener('click', function(e) {
3024 this.parentNode.removeChild(this);
3025 });
rgindaf522ce02012-04-17 17:49:17 -07003026 }
3027
Mike Frysingerb7289952019-03-23 16:05:38 -07003028 this.zoomWarningNode_.textContent = lib.i18n.replaceReferences(
Robert Gindab4839c22013-02-28 16:52:10 -08003029 hterm.zoomWarningMessage,
Joel Hockeyd4fca732019-09-20 16:57:03 -07003030 [Math.floor(this.scrollPort_.characterSize.zoomFactor * 100)]);
Robert Gindab4839c22013-02-28 16:52:10 -08003031
rgindaf522ce02012-04-17 17:49:17 -07003032 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
3033
3034 if (state) {
3035 if (!this.zoomWarningNode_.parentNode)
3036 this.div_.parentNode.appendChild(this.zoomWarningNode_);
3037 } else if (this.zoomWarningNode_.parentNode) {
3038 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
3039 }
3040};
3041
3042/**
rgindacc2996c2012-02-24 14:59:31 -08003043 * Show the terminal overlay for a given amount of time.
3044 *
3045 * The terminal overlay appears in inverse video in a large font, centered
3046 * over the terminal. You should probably keep the overlay message brief,
3047 * since it's in a large font and you probably aren't going to check the size
3048 * of the terminal first.
3049 *
3050 * @param {string} msg The text (not HTML) message to display in the overlay.
Joel Hockey0f933582019-08-27 18:01:51 -07003051 * @param {number=} opt_timeout The amount of time to wait before fading out
rgindacc2996c2012-02-24 14:59:31 -08003052 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
3053 * stay up forever (or until the next overlay).
3054 */
3055hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08003056 if (!this.overlayNode_) {
3057 if (!this.div_)
3058 return;
3059
3060 this.overlayNode_ = this.document_.createElement('div');
3061 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08003062 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08003063 'font-size: xx-large;' +
3064 'opacity: 0.75;' +
3065 'padding: 0.2em 0.5em 0.2em 0.5em;' +
3066 'position: absolute;' +
3067 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003068 '-webkit-transition: opacity 180ms ease-in;' +
3069 '-moz-user-select: none;' +
3070 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08003071
3072 this.overlayNode_.addEventListener('mousedown', function(e) {
3073 e.preventDefault();
3074 e.stopPropagation();
3075 }, true);
rgindaf0090c92012-02-10 14:58:52 -08003076 }
3077
rginda9f5222b2012-03-05 11:53:28 -08003078 this.overlayNode_.style.color = this.prefs_.get('background-color');
3079 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
3080 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
3081
rgindaf0090c92012-02-10 14:58:52 -08003082 this.overlayNode_.textContent = msg;
3083 this.overlayNode_.style.opacity = '0.75';
3084
3085 if (!this.overlayNode_.parentNode)
3086 this.div_.appendChild(this.overlayNode_);
3087
Joel Hockeyd4fca732019-09-20 16:57:03 -07003088 var divSize = hterm.getClientSize(lib.notNull(this.div_));
Robert Ginda97769282013-02-01 15:30:30 -08003089 var overlaySize = hterm.getClientSize(this.overlayNode_);
3090
Robert Ginda8a59f762014-07-23 11:29:55 -07003091 this.overlayNode_.style.top =
3092 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08003093 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07003094 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08003095
rgindaf0090c92012-02-10 14:58:52 -08003096 if (this.overlayTimeout_)
3097 clearTimeout(this.overlayTimeout_);
3098
Raymes Khouryc7a06382018-07-04 10:25:45 +10003099 this.accessibilityReader_.assertiveAnnounce(msg);
3100
rgindacc2996c2012-02-24 14:59:31 -08003101 if (opt_timeout === null)
3102 return;
3103
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003104 this.overlayTimeout_ = setTimeout(() => {
3105 this.overlayNode_.style.opacity = '0';
3106 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
3107 }, opt_timeout || 1500);
3108};
3109
3110/**
3111 * Hide the terminal overlay immediately.
3112 *
3113 * Useful when we show an overlay for an event with an unknown end time.
3114 */
3115hterm.Terminal.prototype.hideOverlay = function() {
3116 if (this.overlayTimeout_)
3117 clearTimeout(this.overlayTimeout_);
3118 this.overlayTimeout_ = null;
3119
3120 if (this.overlayNode_.parentNode)
3121 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
3122 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08003123};
3124
rginda4bba5e12012-06-20 16:15:30 -07003125/**
3126 * Paste from the system clipboard to the terminal.
Mike Frysinger23b5b832019-10-01 17:05:29 -04003127 *
Joel Hockey0f933582019-08-27 18:01:51 -07003128 * @return {boolean}
rginda4bba5e12012-06-20 16:15:30 -07003129 */
3130hterm.Terminal.prototype.paste = function() {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003131 return hterm.pasteFromClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07003132};
3133
3134/**
3135 * Copy a string to the system clipboard.
3136 *
3137 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05003138 *
3139 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07003140 */
3141hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08003142 if (this.prefs_.get('enable-clipboard-notice'))
3143 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
3144
Mike Frysinger96eacae2019-01-02 18:13:56 -05003145 hterm.copySelectionToClipboard(this.document_, str);
rginda4bba5e12012-06-20 16:15:30 -07003146};
3147
Evan Jones2600d4f2016-12-06 09:29:36 -05003148/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003149 * Display an image.
3150 *
Mike Frysinger2558ed52019-01-14 01:03:41 -05003151 * Either URI or buffer or blob fields must be specified.
3152 *
Joel Hockey0f933582019-08-27 18:01:51 -07003153 * @param {{
3154 * name: (string|undefined),
3155 * size: (string|number|undefined),
3156 * preserveAspectRation: (boolean|undefined),
3157 * inline: (boolean|undefined),
3158 * width: (string|number|undefined),
3159 * height: (string|number|undefined),
3160 * align: (string|undefined),
3161 * url: (string|undefined),
3162 * buffer: (!ArrayBuffer|undefined),
3163 * blob: (!Blob|undefined),
3164 * type: (string|undefined),
3165 * }} options The image to display.
3166 * name A human readable string for the image
3167 * size The size (in bytes).
3168 * preserveAspectRatio Whether to preserve aspect.
3169 * inline Whether to display the image inline.
3170 * width The width of the image.
3171 * height The height of the image.
3172 * align Direction to align the image.
3173 * uri The source URI for the image.
3174 * buffer The ArrayBuffer image data.
3175 * blob The Blob image data.
3176 * type The MIME type of the image data.
3177 * @param {function()=} onLoad Callback when loading finishes.
3178 * @param {function(!Event)=} onError Callback when loading fails.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003179 */
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003180hterm.Terminal.prototype.displayImage = function(options, onLoad, onError) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003181 // Make sure we're actually given a resource to display.
Mike Frysinger2558ed52019-01-14 01:03:41 -05003182 if (options.uri === undefined && options.buffer === undefined &&
3183 options.blob === undefined)
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003184 return;
3185
3186 // Set up the defaults to simplify code below.
3187 if (!options.name)
3188 options.name = '';
3189
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003190 // See if the mime type is available. If not, guess from the filename.
3191 // We don't list all possible mime types because the browser can usually
3192 // guess it correctly. So list the ones that need a bit more help.
3193 if (!options.type) {
3194 const ary = options.name.split('.');
3195 const ext = ary[ary.length - 1].trim();
3196 switch (ext) {
3197 case 'svg':
3198 case 'svgz':
3199 options.type = 'image/svg+xml';
3200 break;
3201 }
3202 }
3203
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003204 // Has the user approved image display yet?
3205 if (this.allowImagesInline !== true) {
3206 this.newLine();
3207 const row = this.getRowNode(this.scrollbackRows_.length +
3208 this.getCursorRow() - 1);
3209
3210 if (this.allowImagesInline === false) {
3211 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
3212 'Inline Images Disabled');
3213 return;
3214 }
3215
3216 // Show a prompt.
3217 let button;
3218 const span = this.document_.createElement('span');
3219 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
3220 span.style.fontWeight = 'bold';
3221 span.style.borderWidth = '1px';
3222 span.style.borderStyle = 'dashed';
3223 button = this.document_.createElement('span');
3224 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3225 button.style.marginLeft = '1em';
3226 button.style.borderWidth = '1px';
3227 button.style.borderStyle = 'solid';
3228 button.addEventListener('click', () => {
3229 this.prefs_.set('allow-images-inline', false);
3230 });
3231 span.appendChild(button);
3232 button = this.document_.createElement('span');
3233 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3234 'allow this session');
3235 button.style.marginLeft = '1em';
3236 button.style.borderWidth = '1px';
3237 button.style.borderStyle = 'solid';
3238 button.addEventListener('click', () => {
3239 this.allowImagesInline = true;
3240 });
3241 span.appendChild(button);
3242 button = this.document_.createElement('span');
3243 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3244 button.style.marginLeft = '1em';
3245 button.style.borderWidth = '1px';
3246 button.style.borderStyle = 'solid';
3247 button.addEventListener('click', () => {
3248 this.prefs_.set('allow-images-inline', true);
3249 });
3250 span.appendChild(button);
3251
3252 row.appendChild(span);
3253 return;
3254 }
3255
3256 // See if we should show this object directly, or download it.
3257 if (options.inline) {
3258 const io = this.io.push();
3259 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
Joel Hockeyd4fca732019-09-20 16:57:03 -07003260 'Loading $1 ...'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003261
3262 // While we're loading the image, eat all the user's input.
3263 io.onVTKeystroke = io.sendString = () => {};
3264
3265 // Initialize this new image.
Joel Hockeyd4fca732019-09-20 16:57:03 -07003266 const img = this.document_.createElement('img');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003267 if (options.uri !== undefined) {
3268 img.src = options.uri;
3269 } else if (options.buffer !== undefined) {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003270 const blob = new Blob([options.buffer], {type: options.type});
Mike Frysinger2558ed52019-01-14 01:03:41 -05003271 img.src = URL.createObjectURL(blob);
3272 } else {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003273 const blob = new Blob([options.blob], {type: options.type});
Joel Hockeyd4fca732019-09-20 16:57:03 -07003274 img.src = URL.createObjectURL(blob);
Mike Frysinger2558ed52019-01-14 01:03:41 -05003275 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003276 img.title = img.alt = options.name;
3277
3278 // Attach the image to the page to let it load/render. It won't stay here.
3279 // This is needed so it's visible and the DOM can calculate the height. If
3280 // the image is hidden or not in the DOM, the height is always 0.
3281 this.document_.body.appendChild(img);
3282
3283 // Wait for the image to finish loading before we try moving it to the
3284 // right place in the terminal.
3285 img.onload = () => {
3286 // Now that we have the image dimensions, figure out how to show it.
3287 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
3288 img.style.maxWidth = `${this.document_.body.clientWidth}px`;
3289 img.style.maxHeight = `${this.document_.body.clientHeight}px`;
3290
3291 // Parse a width/height specification.
3292 const parseDim = (dim, maxDim, cssVar) => {
3293 if (!dim || dim == 'auto')
3294 return '';
3295
3296 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3297 if (ary) {
3298 if (ary[2] == '%')
Joel Hockeyd4fca732019-09-20 16:57:03 -07003299 return Math.floor(maxDim * ary[1] / 100) + 'px';
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003300 else if (ary[2] == 'px')
3301 return dim;
3302 else
3303 return `calc(${dim} * var(${cssVar}))`;
3304 }
3305
3306 return '';
3307 };
3308 img.style.width =
3309 parseDim(options.width, this.document_.body.clientWidth,
3310 '--hterm-charsize-width');
3311 img.style.height =
3312 parseDim(options.height, this.document_.body.clientHeight,
3313 '--hterm-charsize-height');
3314
3315 // Figure out how many rows the image occupies, then add that many.
Mike Frysingera0349392019-09-11 05:38:09 -04003316 // Note: This count will be inaccurate if the font size changes on us.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003317 const padRows = Math.ceil(img.clientHeight /
3318 this.scrollPort_.characterSize.height);
3319 for (let i = 0; i < padRows; ++i)
3320 this.newLine();
3321
3322 // Update the max height in case the user shrinks the character size.
3323 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3324
3325 // Move the image to the last row. This way when we scroll up, it doesn't
3326 // disappear when the first row gets clipped. It will disappear when we
3327 // scroll down and the last row is clipped ...
3328 this.document_.body.removeChild(img);
3329 // Create a wrapper node so we can do an absolute in a relative position.
3330 // This helps with rounding errors between JS & CSS counts.
3331 const div = this.document_.createElement('div');
3332 div.style.position = 'relative';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003333 div.style.textAlign = options.align || '';
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003334 img.style.position = 'absolute';
3335 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3336 div.appendChild(img);
3337 const row = this.getRowNode(this.scrollbackRows_.length +
3338 this.getCursorRow() - 1);
3339 row.appendChild(div);
3340
Mike Frysinger2558ed52019-01-14 01:03:41 -05003341 // Now that the image has been read, we can revoke the source.
3342 if (options.uri === undefined) {
3343 URL.revokeObjectURL(img.src);
3344 }
3345
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003346 io.hideOverlay();
3347 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003348
3349 if (onLoad)
3350 onLoad();
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003351 };
3352
3353 // If we got a malformed image, give up.
3354 img.onerror = (e) => {
3355 this.document_.body.removeChild(img);
3356 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
Mike Frysingere14a8c42018-03-10 00:17:30 -08003357 'Loading $1 failed'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003358 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003359
3360 if (onError)
3361 onError(e);
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003362 };
3363 } else {
3364 // We can't use chrome.downloads.download as that requires "downloads"
3365 // permissions, and that works only in extensions, not apps.
3366 const a = this.document_.createElement('a');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003367 if (options.uri !== undefined) {
3368 a.href = options.uri;
3369 } else if (options.buffer !== undefined) {
3370 const blob = new Blob([options.buffer]);
3371 a.href = URL.createObjectURL(blob);
3372 } else {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003373 a.href = URL.createObjectURL(lib.notNull(options.blob));
Mike Frysinger2558ed52019-01-14 01:03:41 -05003374 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003375 a.download = options.name;
3376 this.document_.body.appendChild(a);
3377 a.click();
3378 a.remove();
Mike Frysinger2558ed52019-01-14 01:03:41 -05003379 if (options.uri === undefined) {
3380 URL.revokeObjectURL(a.href);
3381 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003382 }
3383};
3384
3385/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003386 * Returns the selected text, or null if no text is selected.
3387 *
3388 * @return {string|null}
3389 */
rgindaa09e7332012-08-17 12:49:51 -07003390hterm.Terminal.prototype.getSelectionText = function() {
3391 var selection = this.scrollPort_.selection;
3392 selection.sync();
3393
3394 if (selection.isCollapsed)
3395 return null;
3396
rgindaa09e7332012-08-17 12:49:51 -07003397 // Start offset measures from the beginning of the line.
3398 var startOffset = selection.startOffset;
3399 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003400
Raymes Khoury334625a2018-06-25 10:29:40 +10003401 // If an x-row isn't selected, |node| will be null.
3402 if (!node)
3403 return null;
3404
Robert Gindafdbb3f22012-09-06 20:23:06 -07003405 if (node.nodeName != 'X-ROW') {
3406 // If the selection doesn't start on an x-row node, then it must be
3407 // somewhere inside the x-row. Add any characters from previous siblings
3408 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003409
3410 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3411 // If node is the text node in a styled span, move up to the span node.
3412 node = node.parentNode;
3413 }
3414
Robert Gindafdbb3f22012-09-06 20:23:06 -07003415 while (node.previousSibling) {
3416 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003417 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003418 }
rgindaa09e7332012-08-17 12:49:51 -07003419 }
3420
3421 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08003422 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
3423 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003424 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003425
Robert Gindafdbb3f22012-09-06 20:23:06 -07003426 if (node.nodeName != 'X-ROW') {
3427 // If the selection doesn't end on an x-row node, then it must be
3428 // somewhere inside the x-row. Add any characters from following siblings
3429 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003430
3431 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3432 // If node is the text node in a styled span, move up to the span node.
3433 node = node.parentNode;
3434 }
3435
Robert Gindafdbb3f22012-09-06 20:23:06 -07003436 while (node.nextSibling) {
3437 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003438 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003439 }
rgindaa09e7332012-08-17 12:49:51 -07003440 }
3441
3442 var rv = this.getRowsText(selection.startRow.rowIndex,
3443 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003444 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003445};
3446
rginda4bba5e12012-06-20 16:15:30 -07003447/**
3448 * Copy the current selection to the system clipboard, then clear it after a
3449 * short delay.
3450 */
3451hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07003452 var text = this.getSelectionText();
3453 if (text != null)
3454 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07003455};
3456
Joel Hockey0f933582019-08-27 18:01:51 -07003457/**
3458 * Show overlay with current terminal size.
3459 */
rgindaf0090c92012-02-10 14:58:52 -08003460hterm.Terminal.prototype.overlaySize = function() {
Theodore Duboisdd5f9a72019-09-06 23:28:42 -07003461 if (this.prefs_.get('enable-resize-status')) {
3462 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
3463 }
rgindaf0090c92012-02-10 14:58:52 -08003464};
3465
rginda87b86462011-12-14 13:48:03 -08003466/**
3467 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3468 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003469 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003470 */
3471hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08003472 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08003473 this.scrollPort_.scrollRowToBottom(this.getRowCount());
3474
Mike Frysinger225c99d2019-10-20 14:02:37 -06003475 this.pauseCursorBlink_();
3476
Mike Frysinger79669762018-12-30 20:51:10 -05003477 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08003478};
3479
3480/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003481 * Open the selected url.
3482 */
3483hterm.Terminal.prototype.openSelectedUrl_ = function() {
3484 var str = this.getSelectionText();
3485
3486 // If there is no selection, try and expand wherever they clicked.
3487 if (str == null) {
John Lincae9b732018-03-08 13:56:35 +08003488 this.screen_.expandSelectionForUrl(this.document_.getSelection());
Mike Frysinger70b94692017-01-26 18:57:50 -10003489 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003490
3491 // If clicking in empty space, return.
3492 if (str == null)
3493 return;
Mike Frysinger70b94692017-01-26 18:57:50 -10003494 }
3495
3496 // Make sure URL is valid before opening.
3497 if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0)
3498 return;
Mike Frysinger43472622017-06-26 18:11:07 -04003499
3500 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003501 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003502 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3503 // We have to whitelist a few protocols that lack authorities and thus
3504 // never use the //. Like mailto.
3505 switch (str.split(':', 1)[0]) {
3506 case 'mailto':
3507 break;
3508 default:
3509 str = 'http://' + str;
3510 break;
3511 }
3512 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003513
Mike Frysinger720fa832017-10-23 01:15:52 -04003514 hterm.openUrl(str);
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003515};
Mike Frysinger70b94692017-01-26 18:57:50 -10003516
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003517/**
3518 * Manage the automatic mouse hiding behavior while typing.
3519 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003520 * @param {?boolean=} v Whether to enable automatic hiding.
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003521 */
3522hterm.Terminal.prototype.setAutomaticMouseHiding = function(v=null) {
3523 // Since Chrome OS & macOS do this by default everywhere, we don't need to.
3524 // Linux & Windows seem to leave this to specific applications to manage.
3525 if (v === null)
3526 v = (hterm.os != 'cros' && hterm.os != 'mac');
3527
3528 this.mouseHideWhileTyping_ = !!v;
3529};
3530
3531/**
3532 * Handler for monitoring user keyboard activity.
3533 *
3534 * This isn't for processing the keystrokes directly, but for updating any
3535 * state that might toggle based on the user using the keyboard at all.
3536 *
Joel Hockey0f933582019-08-27 18:01:51 -07003537 * @param {!KeyboardEvent} e The keyboard event that triggered us.
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003538 */
3539hterm.Terminal.prototype.onKeyboardActivity_ = function(e) {
3540 // When the user starts typing, hide the mouse cursor.
3541 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_)
3542 this.setCssVar('mouse-cursor-style', 'none');
3543};
Mike Frysinger70b94692017-01-26 18:57:50 -10003544
3545/**
rgindad5613292012-06-19 15:40:37 -07003546 * Add the terminalRow and terminalColumn properties to mouse events and
3547 * then forward on to onMouse().
3548 *
3549 * The terminalRow and terminalColumn properties contain the (row, column)
3550 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003551 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003552 * @param {!MouseEvent} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003553 */
3554hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003555 if (e.processedByTerminalHandler_) {
3556 // We register our event handlers on the document, as well as the cursor
3557 // and the scroll blocker. Mouse events that occur on the cursor or
3558 // scroll blocker will also appear on the document, but we don't want to
3559 // process them twice.
3560 //
3561 // We can't just prevent bubbling because that has other side effects, so
3562 // we decorate the event object with this property instead.
3563 return;
3564 }
3565
Mike Frysinger468966c2018-08-28 13:48:51 -04003566 // Consume navigation events. Button 3 is usually "browser back" and
3567 // button 4 is "browser forward" which we don't want to happen.
3568 if (e.button > 2) {
3569 e.preventDefault();
3570 // We don't return so click events can be passed to the remote below.
3571 }
3572
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003573 var reportMouseEvents = (!this.defeatMouseReports_ &&
3574 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3575
rgindafaa74742012-08-21 13:34:03 -07003576 e.processedByTerminalHandler_ = true;
3577
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003578 // Handle auto hiding of mouse cursor while typing.
3579 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
3580 // Make sure the mouse cursor is visible.
3581 this.syncMouseStyle();
3582 // This debounce isn't perfect, but should work well enough for such a
3583 // simple implementation. If the user moved the mouse, we enabled this
3584 // debounce, and then moved the mouse just before the timeout, we wouldn't
3585 // debounce that later movement.
3586 this.mouseHideDelay_ = setTimeout(() => this.mouseHideDelay_ = null, 1000);
3587 }
3588
Robert Gindaeda48db2014-07-17 09:25:30 -07003589 // One based row/column stored on the mouse event.
Joel Hockeyd4fca732019-09-20 16:57:03 -07003590 e.terminalRow = Math.floor(
3591 (e.clientY - this.scrollPort_.visibleRowTopMargin) /
3592 this.scrollPort_.characterSize.height) + 1;
3593 e.terminalColumn = Math.floor(
3594 e.clientX / this.scrollPort_.characterSize.width) + 1;
Robert Gindaeda48db2014-07-17 09:25:30 -07003595
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003596 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
3597 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07003598 return;
3599 }
3600
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003601 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07003602 // If the cursor is visible and we're not sending mouse events to the
3603 // host app, then we want to hide the terminal cursor when the mouse
3604 // cursor is over top. This keeps the terminal cursor from interfering
3605 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003606 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3607 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3608 this.cursorNode_.style.display = 'none';
3609 } else if (this.cursorNode_.style.display == 'none') {
3610 this.cursorNode_.style.display = '';
3611 }
3612 }
rgindad5613292012-06-19 15:40:37 -07003613
Robert Ginda928cf632014-03-05 15:07:41 -08003614 if (e.type == 'mousedown') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003615 this.contextMenu.hide();
Mike Frysingercc114512017-09-11 21:39:17 -04003616
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003617 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003618 // If VT mouse reporting is disabled, or has been defeated with
3619 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003620 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003621 this.setSelectionEnabled(true);
3622 } else {
3623 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003624 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003625 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003626 this.setSelectionEnabled(false);
3627 e.preventDefault();
3628 }
3629 }
3630
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003631 if (!reportMouseEvents) {
John Lin2aad22e2018-03-16 13:58:11 +08003632 if (e.type == 'dblclick') {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003633 this.screen_.expandSelection(this.document_.getSelection());
John Lin2aad22e2018-03-16 13:58:11 +08003634 if (this.copyOnSelect)
Mike Frysinger406c76c2019-01-02 17:53:51 -05003635 this.copySelectionToClipboard();
rgindad5613292012-06-19 15:40:37 -07003636 }
3637
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003638 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003639 // Debounce this event with the dblclick event. If you try to doubleclick
3640 // a URL to open it, Chrome will fire click then dblclick, but we won't
3641 // have expanded the selection text at the first click event.
3642 clearTimeout(this.timeouts_.openUrl);
3643 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3644 500);
3645 return;
3646 }
3647
Mike Frysinger847577f2017-05-23 23:25:57 -04003648 if (e.type == 'mousedown') {
Mike Frysingercc114512017-09-11 21:39:17 -04003649 if (e.ctrlKey && e.button == 2 /* right button */) {
3650 e.preventDefault();
3651 this.contextMenu.show(e, this);
3652 } else if (e.button == this.mousePasteButton ||
3653 (this.mouseRightClickPaste && e.button == 2 /* right button */)) {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003654 if (!this.paste())
Mike Frysinger05a57f02017-08-27 17:48:55 -04003655 console.warn('Could not paste manually due to web restrictions');
Mike Frysinger847577f2017-05-23 23:25:57 -04003656 }
3657 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003658
Mike Frysinger2edd3612017-05-24 00:54:39 -04003659 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003660 !this.document_.getSelection().isCollapsed) {
Mike Frysinger406c76c2019-01-02 17:53:51 -05003661 this.copySelectionToClipboard();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003662 }
3663
3664 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3665 this.scrollBlockerNode_.engaged) {
3666 // Disengage the scroll-blocker after one of these events.
3667 this.scrollBlockerNode_.engaged = false;
3668 this.scrollBlockerNode_.style.top = '-99px';
3669 }
3670
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003671 // Emulate arrow key presses via scroll wheel events.
3672 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3673 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003674 if (e.type == 'wheel') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003675 const delta =
3676 this.scrollPort_.scrollWheelDelta(/** @type {!WheelEvent} */ (e));
Mike Frysingerc3030a82017-05-29 14:16:11 -04003677
Mike Frysinger321063c2018-08-29 15:33:14 -04003678 // Helper to turn a wheel event delta into a series of key presses.
3679 const deltaToArrows = (distance, charSize, arrowPos, arrowNeg) => {
3680 if (distance == 0) {
3681 return '';
3682 }
3683
3684 // Convert the scroll distance into a number of rows/cols.
3685 const cells = lib.f.smartFloorDivide(Math.abs(distance), charSize);
3686 const data = '\x1bO' + (distance < 0 ? arrowNeg : arrowPos);
3687 return data.repeat(cells);
3688 };
3689
3690 // The order between up/down and left/right doesn't really matter.
3691 this.io.sendString(
3692 // Up/down arrow keys.
3693 deltaToArrows(delta.y, this.scrollPort_.characterSize.height,
3694 'A', 'B') +
3695 // Left/right arrow keys.
3696 deltaToArrows(delta.x, this.scrollPort_.characterSize.width,
3697 'C', 'D')
3698 );
Mike Frysingerc3030a82017-05-29 14:16:11 -04003699
3700 e.preventDefault();
3701 }
3702 }
Robert Ginda928cf632014-03-05 15:07:41 -08003703 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003704 if (!this.scrollBlockerNode_.engaged) {
3705 if (e.type == 'mousedown') {
3706 // Move the scroll-blocker into place if we want to keep the scrollport
3707 // from scrolling.
3708 this.scrollBlockerNode_.engaged = true;
3709 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3710 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3711 } else if (e.type == 'mousemove') {
3712 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3713 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003714 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003715 e.preventDefault();
3716 }
3717 }
Robert Ginda928cf632014-03-05 15:07:41 -08003718
3719 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003720 }
3721
Robert Ginda928cf632014-03-05 15:07:41 -08003722 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3723 // Restore this on mouseup in case it was temporarily defeated with a
3724 // alt-mousedown. Only do this when the selection is empty so that
3725 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003726 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003727 }
rgindad5613292012-06-19 15:40:37 -07003728};
3729
3730/**
3731 * Clients should override this if they care to know about mouse events.
3732 *
3733 * The event parameter will be a normal DOM mouse click event with additional
3734 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003735 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003736 * @param {!MouseEvent} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003737 */
3738hterm.Terminal.prototype.onMouse = function(e) { };
3739
3740/**
rginda8e92a692012-05-20 19:37:20 -07003741 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003742 *
3743 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003744 */
Rob Spies06533ba2014-04-24 11:20:37 -07003745hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3746 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003747 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003748
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003749 if (this.reportFocus)
3750 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O');
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003751
Michael Kelly485ecd12014-06-09 11:41:56 -04003752 if (focused === true)
3753 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07003754};
3755
3756/**
rginda8ba33642011-12-14 12:31:31 -08003757 * React when the ScrollPort is scrolled.
3758 */
3759hterm.Terminal.prototype.onScroll_ = function() {
3760 this.scheduleSyncCursorPosition_();
3761};
3762
3763/**
rginda9846e2f2012-01-27 13:53:33 -08003764 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003765 *
Joel Hockeye25ce432019-09-25 19:12:28 -07003766 * @param {{text: string}} e The text of the paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08003767 */
3768hterm.Terminal.prototype.onPaste_ = function(e) {
Joel Hockeye25ce432019-09-25 19:12:28 -07003769 var data = e.text.replace(/\n/mg, '\r');
Mike Frysingere8c32c82018-03-11 14:57:28 -07003770 if (this.options_.bracketedPaste) {
3771 // We strip out most escape sequences as they can cause issues (like
3772 // inserting an \x1b[201~ midstream). We pass through whitespace
3773 // though: 0x08:\b 0x09:\t 0x0a:\n 0x0d:\r.
3774 // This matches xterm behavior.
3775 const filter = (data) => data.replace(/[\x00-\x07\x0b-\x0c\x0e-\x1f]/g, '');
3776 data = '\x1b[200~' + filter(data) + '\x1b[201~';
3777 }
Robert Gindaa063b202014-07-21 11:08:25 -07003778
3779 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003780};
3781
3782/**
rgindaa09e7332012-08-17 12:49:51 -07003783 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003784 *
Joel Hockey0f933582019-08-27 18:01:51 -07003785 * @param {!Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07003786 */
3787hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07003788 if (!this.useDefaultWindowCopy) {
3789 e.preventDefault();
3790 setTimeout(this.copySelectionToClipboard.bind(this), 0);
3791 }
rgindaa09e7332012-08-17 12:49:51 -07003792};
3793
3794/**
rginda8ba33642011-12-14 12:31:31 -08003795 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08003796 *
3797 * Note: This function should not directly contain code that alters the internal
3798 * state of the terminal. That kind of code belongs in realizeWidth or
3799 * realizeHeight, so that it can be executed synchronously in the case of a
3800 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08003801 */
3802hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08003803 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07003804 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08003805 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07003806 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08003807
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003808 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08003809 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003810 // gets removed from the document or during the initial load, and we can't
3811 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07003812 // This can also happen if called before the scrollPort calculates the
3813 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08003814 return;
3815 }
3816
rgindaa8ba17d2012-08-15 14:41:10 -07003817 var isNewSize = (columnCount != this.screenSize.width ||
3818 rowCount != this.screenSize.height);
Theodore Dubois651b0842019-09-07 14:32:09 -07003819 const wasScrolledEnd = this.scrollPort_.isScrolledEnd;
rgindaa8ba17d2012-08-15 14:41:10 -07003820
3821 // We do this even if the size didn't change, just to be sure everything is
3822 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04003823 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07003824 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07003825
3826 if (isNewSize)
3827 this.overlaySize();
3828
Robert Gindafb1be6a2013-12-11 11:56:22 -08003829 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07003830 this.scheduleSyncCursorPosition_();
Theodore Dubois651b0842019-09-07 14:32:09 -07003831
3832 if (wasScrolledEnd) {
3833 this.scrollEnd();
3834 }
rginda8ba33642011-12-14 12:31:31 -08003835};
3836
3837/**
3838 * Service the cursor blink timeout.
3839 */
3840hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07003841 if (!this.options_.cursorBlink) {
3842 delete this.timeouts_.cursorBlink;
3843 return;
3844 }
3845
Robert Ginda830583c2013-08-07 13:20:46 -07003846 if (this.cursorNode_.getAttribute('focus') == 'false' ||
Mike Frysinger225c99d2019-10-20 14:02:37 -06003847 this.cursorNode_.style.opacity == '0' ||
3848 this.cursorBlinkPause_) {
rginda87b86462011-12-14 13:48:03 -08003849 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07003850 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3851 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08003852 } else {
rginda87b86462011-12-14 13:48:03 -08003853 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07003854 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3855 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08003856 }
3857};
David Reveman8f552492012-03-28 12:18:41 -04003858
3859/**
3860 * Set the scrollbar-visible mode bit.
3861 *
3862 * If scrollbar-visible is on, the vertical scrollbar will be visible.
3863 * Otherwise it will not.
3864 *
3865 * Defaults to on.
3866 *
3867 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
3868 */
3869hterm.Terminal.prototype.setScrollbarVisible = function(state) {
3870 this.scrollPort_.setScrollbarVisible(state);
3871};
Michael Kelly485ecd12014-06-09 11:41:56 -04003872
3873/**
Rob Spies49039e52014-12-17 13:40:04 -08003874 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04003875 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08003876 *
3877 * Defaults to 1.
3878 *
Evan Jones2600d4f2016-12-06 09:29:36 -05003879 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08003880 */
3881hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3882 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3883};
3884
3885/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003886 * Close all web notifications created by terminal bells.
3887 */
3888hterm.Terminal.prototype.closeBellNotifications_ = function() {
3889 this.bellNotificationList_.forEach(function(n) {
3890 n.close();
3891 });
3892 this.bellNotificationList_.length = 0;
3893};
Raymes Khourye5d48982018-08-02 09:08:32 +10003894
3895/**
3896 * Syncs the cursor position when the scrollport gains focus.
3897 */
3898hterm.Terminal.prototype.onScrollportFocus_ = function() {
3899 // If the cursor is offscreen we set selection to the last row on the screen.
3900 const topRowIndex = this.scrollPort_.getTopRowIndex();
3901 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
3902 const selection = this.document_.getSelection();
3903 if (!this.syncCursorPosition_() && selection) {
3904 selection.collapse(this.getRowNode(bottomRowIndex));
3905 }
3906};