blob: 320dab1ff40dd5a74287983c52060dc0541e01b1 [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();
rginda9875d902012-08-20 16:21:57 -0700748 if (self.prefs_.get('close-on-exit'))
749 window.close();
rginda87b86462011-12-14 13:48:03 -0800750 }
751 });
752
rgindafeaf3142012-01-31 15:14:20 -0800753 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800754 this.command.run();
755};
756
757/**
rgindafeaf3142012-01-31 15:14:20 -0800758 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500759 *
760 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800761 */
762hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700763 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800764};
765
766/**
767 * Install the keyboard handler for this terminal.
768 *
769 * This will prevent the browser from seeing any keystrokes sent to the
770 * terminal.
771 */
772hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700773 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400774};
rgindafeaf3142012-01-31 15:14:20 -0800775
776/**
777 * Uninstall the keyboard handler for this terminal.
778 */
779hterm.Terminal.prototype.uninstallKeyboard = function() {
780 this.keyboard.installKeyboard(null);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400781};
rgindafeaf3142012-01-31 15:14:20 -0800782
783/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400784 * Set a CSS variable.
785 *
786 * Normally this is used to set variables in the hterm namespace.
787 *
788 * @param {string} name The variable to set.
Joel Hockeyd4fca732019-09-20 16:57:03 -0700789 * @param {string|number} value The value to assign to the variable.
Joel Hockey0f933582019-08-27 18:01:51 -0700790 * @param {string=} opt_prefix The variable namespace/prefix to use.
Mike Frysingercce97c42017-08-05 01:11:22 -0400791 */
792hterm.Terminal.prototype.setCssVar = function(name, value,
793 opt_prefix='--hterm-') {
794 this.document_.documentElement.style.setProperty(
Joel Hockeyd4fca732019-09-20 16:57:03 -0700795 `${opt_prefix}${name}`, value.toString());
Mike Frysingercce97c42017-08-05 01:11:22 -0400796};
797
798/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500799 * Get a CSS variable.
800 *
801 * Normally this is used to get variables in the hterm namespace.
802 *
803 * @param {string} name The variable to read.
Joel Hockey0f933582019-08-27 18:01:51 -0700804 * @param {string=} opt_prefix The variable namespace/prefix to use.
Mike Frysinger261597c2017-12-28 01:14:21 -0500805 * @return {string} The current setting for this variable.
806 */
807hterm.Terminal.prototype.getCssVar = function(name, opt_prefix='--hterm-') {
808 return this.document_.documentElement.style.getPropertyValue(
809 `${opt_prefix}${name}`);
810};
811
812/**
rginda35c456b2012-02-09 17:29:05 -0800813 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800814 *
815 * Call setFontSize(0) to reset to the default font size.
816 *
817 * This function does not modify the font-size preference.
818 *
819 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800820 */
821hterm.Terminal.prototype.setFontSize = function(px) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500822 if (px <= 0)
Joel Hockeyd4fca732019-09-20 16:57:03 -0700823 px = this.prefs_.getNumber('font-size');
rginda9f5222b2012-03-05 11:53:28 -0800824
rginda35c456b2012-02-09 17:29:05 -0800825 this.scrollPort_.setFontSize(px);
Mike Frysingercce97c42017-08-05 01:11:22 -0400826 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
827 this.setCssVar('charsize-height',
828 this.scrollPort_.characterSize.height + 'px');
rginda35c456b2012-02-09 17:29:05 -0800829};
830
831/**
832 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500833 *
834 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800835 */
836hterm.Terminal.prototype.getFontSize = function() {
837 return this.scrollPort_.getFontSize();
838};
839
840/**
rginda8e92a692012-05-20 19:37:20 -0700841 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500842 *
843 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700844 */
845hterm.Terminal.prototype.getFontFamily = function() {
846 return this.scrollPort_.getFontFamily();
847};
848
849/**
rginda35c456b2012-02-09 17:29:05 -0800850 * Set the CSS "font-family" for this terminal.
851 */
rginda9f5222b2012-03-05 11:53:28 -0800852hterm.Terminal.prototype.syncFontFamily = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700853 this.scrollPort_.setFontFamily(this.prefs_.getString('font-family'),
854 this.prefs_.getString('font-smoothing'));
rginda9f5222b2012-03-05 11:53:28 -0800855 this.syncBoldSafeState();
856};
857
rginda4bba5e12012-06-20 16:15:30 -0700858/**
859 * Set this.mousePasteButton based on the mouse-paste-button pref,
860 * autodetecting if necessary.
861 */
862hterm.Terminal.prototype.syncMousePasteButton = function() {
863 var button = this.prefs_.get('mouse-paste-button');
864 if (typeof button == 'number') {
865 this.mousePasteButton = button;
866 return;
867 }
868
Mike Frysingeree81a002017-12-12 16:14:53 -0500869 if (hterm.os != 'linux') {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400870 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700871 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400872 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700873 }
874};
875
876/**
877 * Enable or disable bold based on the enable-bold pref, autodetecting if
878 * necessary.
879 */
rginda9f5222b2012-03-05 11:53:28 -0800880hterm.Terminal.prototype.syncBoldSafeState = function() {
881 var enableBold = this.prefs_.get('enable-bold');
882 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700883 this.primaryScreen_.textAttributes.enableBold = enableBold;
884 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800885 return;
886 }
887
rgindaf7521392012-02-28 17:20:34 -0800888 var normalSize = this.scrollPort_.measureCharacterSize();
889 var boldSize = this.scrollPort_.measureCharacterSize('bold');
890
891 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800892 if (!isBoldSafe) {
893 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700894 'from normal. Font family is: ' +
895 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800896 }
rginda9f5222b2012-03-05 11:53:28 -0800897
Robert Gindaed016262012-10-26 16:27:09 -0700898 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
899 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800900};
901
902/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500903 * Control text blinking behavior.
904 *
905 * @param {boolean=} state Whether to enable support for blinking text.
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400906 */
Mike Frysinger261597c2017-12-28 01:14:21 -0500907hterm.Terminal.prototype.setTextBlink = function(state) {
908 if (state === undefined)
Joel Hockeyd4fca732019-09-20 16:57:03 -0700909 state = this.prefs_.getBoolean('enable-blink');
Mike Frysinger261597c2017-12-28 01:14:21 -0500910 this.setCssVar('blink-node-duration', state ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400911};
912
913/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400914 * Set the mouse cursor style based on the current terminal mode.
915 */
916hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -0400917 this.setCssVar('mouse-cursor-style',
918 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
919 'var(--hterm-mouse-cursor-text)' :
Mike Frysinger67f58f82018-11-22 13:38:22 -0500920 'var(--hterm-mouse-cursor-default)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400921};
922
923/**
rginda87b86462011-12-14 13:48:03 -0800924 * Return a copy of the current cursor position.
925 *
Joel Hockey0f933582019-08-27 18:01:51 -0700926 * @return {!hterm.RowCol} The RowCol object representing the current position.
rginda87b86462011-12-14 13:48:03 -0800927 */
928hterm.Terminal.prototype.saveCursor = function() {
929 return this.screen_.cursorPosition.clone();
930};
931
Evan Jones2600d4f2016-12-06 09:29:36 -0500932/**
933 * Return the current text attributes.
934 *
Joel Hockeyd4fca732019-09-20 16:57:03 -0700935 * @return {!hterm.TextAttributes}
Evan Jones2600d4f2016-12-06 09:29:36 -0500936 */
rgindaa19afe22012-01-25 15:40:22 -0800937hterm.Terminal.prototype.getTextAttributes = function() {
938 return this.screen_.textAttributes;
939};
940
Evan Jones2600d4f2016-12-06 09:29:36 -0500941/**
942 * Set the text attributes.
943 *
Joel Hockeyd4fca732019-09-20 16:57:03 -0700944 * @param {!hterm.TextAttributes} textAttributes The attributes to set.
Evan Jones2600d4f2016-12-06 09:29:36 -0500945 */
rginda1a09aa02012-06-18 21:11:25 -0700946hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
947 this.screen_.textAttributes = textAttributes;
948};
949
rginda87b86462011-12-14 13:48:03 -0800950/**
rgindaf522ce02012-04-17 17:49:17 -0700951 * Return the current browser zoom factor applied to the terminal.
952 *
953 * @return {number} The current browser zoom factor.
954 */
955hterm.Terminal.prototype.getZoomFactor = function() {
956 return this.scrollPort_.characterSize.zoomFactor;
957};
958
959/**
rginda9846e2f2012-01-27 13:53:33 -0800960 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -0500961 *
962 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -0800963 */
964hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800965 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800966};
967
968/**
rginda87b86462011-12-14 13:48:03 -0800969 * Restore a previously saved cursor position.
970 *
Joel Hockey0f933582019-08-27 18:01:51 -0700971 * @param {!hterm.RowCol} cursor The position to restore.
rginda87b86462011-12-14 13:48:03 -0800972 */
973hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700974 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
975 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800976 this.screen_.setCursorPosition(row, column);
977 if (cursor.column > column ||
978 cursor.column == column && cursor.overflow) {
979 this.screen_.cursorPosition.overflow = true;
980 }
rginda87b86462011-12-14 13:48:03 -0800981};
982
983/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400984 * Clear the cursor's overflow flag.
985 */
986hterm.Terminal.prototype.clearCursorOverflow = function() {
987 this.screen_.cursorPosition.overflow = false;
988};
989
990/**
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800991 * Save the current cursor state to the corresponding screens.
992 *
993 * See the hterm.Screen.CursorState class for more details.
994 *
995 * @param {boolean=} both If true, update both screens, else only update the
996 * current screen.
997 */
998hterm.Terminal.prototype.saveCursorAndState = function(both) {
999 if (both) {
1000 this.primaryScreen_.saveCursorAndState(this.vt);
1001 this.alternateScreen_.saveCursorAndState(this.vt);
1002 } else
1003 this.screen_.saveCursorAndState(this.vt);
1004};
1005
1006/**
1007 * Restore the saved cursor state in the corresponding screens.
1008 *
1009 * See the hterm.Screen.CursorState class for more details.
1010 *
1011 * @param {boolean=} both If true, update both screens, else only update the
1012 * current screen.
1013 */
1014hterm.Terminal.prototype.restoreCursorAndState = function(both) {
1015 if (both) {
1016 this.primaryScreen_.restoreCursorAndState(this.vt);
1017 this.alternateScreen_.restoreCursorAndState(this.vt);
1018 } else
1019 this.screen_.restoreCursorAndState(this.vt);
1020};
1021
1022/**
Robert Ginda830583c2013-08-07 13:20:46 -07001023 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001024 *
1025 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -07001026 */
1027hterm.Terminal.prototype.setCursorShape = function(shape) {
1028 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001029 this.restyleCursor_();
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001030};
Robert Ginda830583c2013-08-07 13:20:46 -07001031
1032/**
1033 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001034 *
1035 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -07001036 */
1037hterm.Terminal.prototype.getCursorShape = function() {
1038 return this.cursorShape_;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001039};
Robert Ginda830583c2013-08-07 13:20:46 -07001040
1041/**
rginda87b86462011-12-14 13:48:03 -08001042 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001043 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001044 * @param {?number} columnCount
rginda87b86462011-12-14 13:48:03 -08001045 */
1046hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -08001047 if (columnCount == null) {
1048 this.div_.style.width = '100%';
1049 return;
1050 }
1051
Robert Ginda26806d12014-07-24 13:44:07 -07001052 this.div_.style.width = Math.ceil(
1053 this.scrollPort_.characterSize.width *
1054 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001055 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -08001056 this.scheduleSyncCursorPosition_();
1057};
rginda87b86462011-12-14 13:48:03 -08001058
rgindac9bc5502012-01-18 11:48:44 -08001059/**
rginda35c456b2012-02-09 17:29:05 -08001060 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001061 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001062 * @param {?number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -08001063 */
1064hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -08001065 if (rowCount == null) {
1066 this.div_.style.height = '100%';
1067 return;
1068 }
1069
rginda35c456b2012-02-09 17:29:05 -08001070 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -07001071 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -08001072 this.realizeSize_(this.screenSize.width, rowCount);
1073 this.scheduleSyncCursorPosition_();
1074};
1075
1076/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001077 * Deal with terminal size changes.
1078 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001079 * @param {number} columnCount The number of columns.
1080 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001081 */
1082hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
Mike Frysinger0206e262019-06-13 10:18:19 -04001083 let notify = false;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001084
Mike Frysinger0206e262019-06-13 10:18:19 -04001085 if (columnCount != this.screenSize.width) {
1086 notify = true;
1087 this.realizeWidth_(columnCount);
1088 }
1089
1090 if (rowCount != this.screenSize.height) {
1091 notify = true;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001092 this.realizeHeight_(rowCount);
Mike Frysinger0206e262019-06-13 10:18:19 -04001093 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001094
1095 // Send new terminal size to plugin.
Mike Frysinger0206e262019-06-13 10:18:19 -04001096 if (notify) {
1097 this.io.onTerminalResize_(columnCount, rowCount);
1098 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001099};
1100
1101/**
rgindac9bc5502012-01-18 11:48:44 -08001102 * Deal with terminal width changes.
1103 *
1104 * This function does what needs to be done when the terminal width changes
1105 * out from under us. It happens here rather than in onResize_() because this
1106 * code may need to run synchronously to handle programmatic changes of
1107 * terminal width.
1108 *
1109 * Relying on the browser to send us an async resize event means we may not be
1110 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001111 *
1112 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001113 */
1114hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001115 if (columnCount <= 0)
1116 throw new Error('Attempt to realize bad width: ' + columnCount);
1117
rgindac9bc5502012-01-18 11:48:44 -08001118 var deltaColumns = columnCount - this.screen_.getWidth();
Mike Frysinger0206e262019-06-13 10:18:19 -04001119 if (deltaColumns == 0) {
1120 // No change, so don't bother recalculating things.
1121 return;
1122 }
rgindac9bc5502012-01-18 11:48:44 -08001123
rginda87b86462011-12-14 13:48:03 -08001124 this.screenSize.width = columnCount;
1125 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001126
1127 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -04001128 if (this.defaultTabStops)
1129 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -08001130 } else {
1131 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -04001132 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -08001133 break;
1134
1135 this.tabStops_.pop();
1136 }
1137 }
1138
1139 this.screen_.setColumnCount(this.screenSize.width);
1140};
1141
1142/**
1143 * Deal with terminal height changes.
1144 *
1145 * This function does what needs to be done when the terminal height changes
1146 * out from under us. It happens here rather than in onResize_() because this
1147 * code may need to run synchronously to handle programmatic changes of
1148 * terminal height.
1149 *
1150 * Relying on the browser to send us an async resize event means we may not be
1151 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001152 *
1153 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001154 */
1155hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001156 if (rowCount <= 0)
1157 throw new Error('Attempt to realize bad height: ' + rowCount);
1158
rgindac9bc5502012-01-18 11:48:44 -08001159 var deltaRows = rowCount - this.screen_.getHeight();
Mike Frysinger0206e262019-06-13 10:18:19 -04001160 if (deltaRows == 0) {
1161 // No change, so don't bother recalculating things.
1162 return;
1163 }
rgindac9bc5502012-01-18 11:48:44 -08001164
1165 this.screenSize.height = rowCount;
1166
1167 var cursor = this.saveCursor();
1168
1169 if (deltaRows < 0) {
1170 // Screen got smaller.
1171 deltaRows *= -1;
1172 while (deltaRows) {
1173 var lastRow = this.getRowCount() - 1;
1174 if (lastRow - this.scrollbackRows_.length == cursor.row)
1175 break;
1176
1177 if (this.getRowText(lastRow))
1178 break;
1179
1180 this.screen_.popRow();
1181 deltaRows--;
1182 }
1183
1184 var ary = this.screen_.shiftRows(deltaRows);
1185 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1186
1187 // We just removed rows from the top of the screen, we need to update
1188 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001189 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001190 } else if (deltaRows > 0) {
1191 // Screen got larger.
1192
1193 if (deltaRows <= this.scrollbackRows_.length) {
1194 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1195 var rows = this.scrollbackRows_.splice(
1196 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1197 this.screen_.unshiftRows(rows);
1198 deltaRows -= scrollbackCount;
1199 cursor.row += scrollbackCount;
1200 }
1201
1202 if (deltaRows)
1203 this.appendRows_(deltaRows);
1204 }
1205
rginda35c456b2012-02-09 17:29:05 -08001206 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001207 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001208};
1209
1210/**
1211 * Scroll the terminal to the top of the scrollback buffer.
1212 */
1213hterm.Terminal.prototype.scrollHome = function() {
1214 this.scrollPort_.scrollRowToTop(0);
1215};
1216
1217/**
1218 * Scroll the terminal to the end.
1219 */
1220hterm.Terminal.prototype.scrollEnd = function() {
1221 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1222};
1223
1224/**
1225 * Scroll the terminal one page up (minus one line) relative to the current
1226 * position.
1227 */
1228hterm.Terminal.prototype.scrollPageUp = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001229 this.scrollPort_.scrollPageUp();
rginda87b86462011-12-14 13:48:03 -08001230};
1231
1232/**
1233 * Scroll the terminal one page down (minus one line) relative to the current
1234 * position.
1235 */
1236hterm.Terminal.prototype.scrollPageDown = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001237 this.scrollPort_.scrollPageDown();
rginda8ba33642011-12-14 12:31:31 -08001238};
1239
rgindac9bc5502012-01-18 11:48:44 -08001240/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001241 * Scroll the terminal one line up relative to the current position.
1242 */
1243hterm.Terminal.prototype.scrollLineUp = function() {
1244 var i = this.scrollPort_.getTopRowIndex();
1245 this.scrollPort_.scrollRowToTop(i - 1);
1246};
1247
1248/**
1249 * Scroll the terminal one line down relative to the current position.
1250 */
1251hterm.Terminal.prototype.scrollLineDown = function() {
1252 var i = this.scrollPort_.getTopRowIndex();
1253 this.scrollPort_.scrollRowToTop(i + 1);
1254};
1255
1256/**
Robert Ginda40932892012-12-10 17:26:40 -08001257 * Clear primary screen, secondary screen, and the scrollback buffer.
1258 */
1259hterm.Terminal.prototype.wipeContents = function() {
Mike Frysinger9c482b82018-09-07 02:49:36 -04001260 this.clearHome(this.primaryScreen_);
1261 this.clearHome(this.alternateScreen_);
1262
1263 this.clearScrollback();
1264};
1265
1266/**
1267 * Clear scrollback buffer.
1268 */
1269hterm.Terminal.prototype.clearScrollback = function() {
1270 // Move to the end of the buffer in case the screen was scrolled back.
1271 // We're going to throw it away which would leave the display invalid.
1272 this.scrollEnd();
1273
Robert Ginda40932892012-12-10 17:26:40 -08001274 this.scrollbackRows_.length = 0;
1275 this.scrollPort_.resetCache();
1276
Mike Frysinger9c482b82018-09-07 02:49:36 -04001277 [this.primaryScreen_, this.alternateScreen_].forEach((screen) => {
1278 const bottom = screen.getHeight();
1279 this.renumberRows_(0, bottom, screen);
1280 });
Robert Ginda40932892012-12-10 17:26:40 -08001281
1282 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001283 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001284};
1285
1286/**
rgindac9bc5502012-01-18 11:48:44 -08001287 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001288 *
1289 * Perform a full reset to the default values listed in
1290 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001291 */
rginda87b86462011-12-14 13:48:03 -08001292hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001293 this.vt.reset();
1294
rgindac9bc5502012-01-18 11:48:44 -08001295 this.clearAllTabStops();
1296 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001297
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001298 const resetScreen = (screen) => {
1299 // We want to make sure to reset the attributes before we clear the screen.
1300 // The attributes might be used to initialize default/empty rows.
1301 screen.textAttributes.reset();
1302 screen.textAttributes.resetColorPalette();
1303 this.clearHome(screen);
1304 screen.saveCursorAndState(this.vt);
1305 };
1306 resetScreen(this.primaryScreen_);
1307 resetScreen(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001308
Mike Frysinger84301d02017-11-29 13:28:46 -08001309 // Reset terminal options to their default values.
1310 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001311 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1312
Mike Frysinger84301d02017-11-29 13:28:46 -08001313 this.setVTScrollRegion(null, null);
1314
1315 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001316};
1317
rgindac9bc5502012-01-18 11:48:44 -08001318/**
1319 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001320 *
1321 * Perform a soft reset to the default values listed in
1322 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001323 */
rginda0f5c0292012-01-13 11:00:13 -08001324hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001325 this.vt.reset();
1326
rgindab8bc8932012-04-27 12:45:03 -07001327 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001328 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001329
Brad Townb62dfdc2015-03-16 19:07:15 -07001330 // We show the cursor on soft reset but do not alter the blink state.
1331 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1332
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001333 const resetScreen = (screen) => {
1334 // Xterm also resets the color palette on soft reset, even though it doesn't
1335 // seem to be documented anywhere.
1336 screen.textAttributes.reset();
1337 screen.textAttributes.resetColorPalette();
1338 screen.saveCursorAndState(this.vt);
1339 };
1340 resetScreen(this.primaryScreen_);
1341 resetScreen(this.alternateScreen_);
rgindaf522ce02012-04-17 17:49:17 -07001342
rgindab8bc8932012-04-27 12:45:03 -07001343 // The xterm man page explicitly says this will happen on soft reset.
1344 this.setVTScrollRegion(null, null);
1345
1346 // Xterm also shows the cursor on soft reset, but does not alter the blink
1347 // state.
rgindaa19afe22012-01-25 15:40:22 -08001348 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001349};
1350
rgindac9bc5502012-01-18 11:48:44 -08001351/**
1352 * Move the cursor forward to the next tab stop, or to the last column
1353 * if no more tab stops are set.
1354 */
1355hterm.Terminal.prototype.forwardTabStop = function() {
1356 var column = this.screen_.cursorPosition.column;
1357
1358 for (var i = 0; i < this.tabStops_.length; i++) {
1359 if (this.tabStops_[i] > column) {
1360 this.setCursorColumn(this.tabStops_[i]);
1361 return;
1362 }
1363 }
1364
David Benjamin66e954d2012-05-05 21:08:12 -04001365 // xterm does not clear the overflow flag on HT or CHT.
1366 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001367 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001368 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001369};
1370
rgindac9bc5502012-01-18 11:48:44 -08001371/**
1372 * Move the cursor backward to the previous tab stop, or to the first column
1373 * if no previous tab stops are set.
1374 */
1375hterm.Terminal.prototype.backwardTabStop = function() {
1376 var column = this.screen_.cursorPosition.column;
1377
1378 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1379 if (this.tabStops_[i] < column) {
1380 this.setCursorColumn(this.tabStops_[i]);
1381 return;
1382 }
1383 }
1384
1385 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001386};
1387
rgindac9bc5502012-01-18 11:48:44 -08001388/**
1389 * Set a tab stop at the given column.
1390 *
Joel Hockey0f933582019-08-27 18:01:51 -07001391 * @param {number} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001392 */
1393hterm.Terminal.prototype.setTabStop = function(column) {
1394 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1395 if (this.tabStops_[i] == column)
1396 return;
1397
1398 if (this.tabStops_[i] < column) {
1399 this.tabStops_.splice(i + 1, 0, column);
1400 return;
1401 }
1402 }
1403
1404 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001405};
1406
rgindac9bc5502012-01-18 11:48:44 -08001407/**
1408 * Clear the tab stop at the current cursor position.
1409 *
1410 * No effect if there is no tab stop at the current cursor position.
1411 */
1412hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1413 var column = this.screen_.cursorPosition.column;
1414
1415 var i = this.tabStops_.indexOf(column);
1416 if (i == -1)
1417 return;
1418
1419 this.tabStops_.splice(i, 1);
1420};
1421
1422/**
1423 * Clear all tab stops.
1424 */
1425hterm.Terminal.prototype.clearAllTabStops = function() {
1426 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001427 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001428};
1429
1430/**
1431 * Set up the default tab stops, starting from a given column.
1432 *
1433 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001434 * from the specified column, or 0 if no column is provided. It also flags
1435 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001436 *
1437 * This does not clear the existing tab stops first, use clearAllTabStops
1438 * for that.
1439 *
Joel Hockey0f933582019-08-27 18:01:51 -07001440 * @param {number=} opt_start Optional starting zero based starting column,
1441 * useful for filling out missing tab stops when the terminal is resized.
rgindac9bc5502012-01-18 11:48:44 -08001442 */
1443hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1444 var start = opt_start || 0;
1445 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001446 // Round start up to a default tab stop.
1447 start = start - 1 - ((start - 1) % w) + w;
1448 for (var i = start; i < this.screenSize.width; i += w) {
1449 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001450 }
David Benjamin66e954d2012-05-05 21:08:12 -04001451
1452 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001453};
1454
rginda6d397402012-01-17 10:58:29 -08001455/**
rginda8ba33642011-12-14 12:31:31 -08001456 * Interpret a sequence of characters.
1457 *
1458 * Incomplete escape sequences are buffered until the next call.
1459 *
1460 * @param {string} str Sequence of characters to interpret or pass through.
1461 */
1462hterm.Terminal.prototype.interpret = function(str) {
rginda8ba33642011-12-14 12:31:31 -08001463 this.scheduleSyncCursorPosition_();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001464 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001465};
1466
1467/**
1468 * Take over the given DIV for use as the terminal display.
1469 *
Joel Hockey0f933582019-08-27 18:01:51 -07001470 * @param {!Element} div The div to use as the terminal display.
rginda8ba33642011-12-14 12:31:31 -08001471 */
1472hterm.Terminal.prototype.decorate = function(div) {
Mike Frysinger5768a9d2017-12-26 12:57:44 -05001473 const charset = div.ownerDocument.characterSet.toLowerCase();
1474 if (charset != 'utf-8') {
1475 console.warn(`Document encoding should be set to utf-8, not "${charset}";` +
1476 ` Add <meta charset='utf-8'/> to your HTML <head> to fix.`);
1477 }
1478
rginda87b86462011-12-14 13:48:03 -08001479 this.div_ = div;
1480
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001481 this.accessibilityReader_ = new hterm.AccessibilityReader(div);
1482
Adrián Pérez-Orozco394e64f2018-12-17 17:20:16 -08001483 this.scrollPort_.decorate(div, () => this.setupScrollPort_());
1484};
1485
1486/**
1487 * Initialisation of ScrollPort properties which need to be set after its DOM
1488 * has been initialised.
Mike Frysinger23b5b832019-10-01 17:05:29 -04001489 *
Adrián Pérez-Orozco394e64f2018-12-17 17:20:16 -08001490 * @private
1491 */
1492hterm.Terminal.prototype.setupScrollPort_ = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -07001493 this.scrollPort_.setBackgroundImage(
1494 this.prefs_.getString('background-image'));
1495 this.scrollPort_.setBackgroundSize(this.prefs_.getString('background-size'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001496 this.scrollPort_.setBackgroundPosition(
Joel Hockeyd4fca732019-09-20 16:57:03 -07001497 this.prefs_.getString('background-position'));
1498 this.scrollPort_.setUserCssUrl(this.prefs_.getString('user-css'));
1499 this.scrollPort_.setUserCssText(this.prefs_.getString('user-css-text'));
1500 this.scrollPort_.setAccessibilityReader(
1501 lib.notNull(this.accessibilityReader_));
rginda30f20f62012-04-05 16:36:19 -07001502
rginda0918b652012-04-04 11:26:24 -07001503 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001504
Joel Hockeyd4fca732019-09-20 16:57:03 -07001505 this.setFontSize(this.prefs_.getNumber('font-size'));
rginda9f5222b2012-03-05 11:53:28 -08001506 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001507
Joel Hockeyd4fca732019-09-20 16:57:03 -07001508 this.setScrollbarVisible(this.prefs_.getBoolean('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001509 this.setScrollWheelMoveMultipler(
Joel Hockeyd4fca732019-09-20 16:57:03 -07001510 this.prefs_.getNumber('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001511
rginda8ba33642011-12-14 12:31:31 -08001512 this.document_ = this.scrollPort_.getDocument();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001513 this.accessibilityReader_.decorate(this.document_);
rginda8ba33642011-12-14 12:31:31 -08001514
Evan Jones5f9df812016-12-06 09:38:58 -05001515 this.document_.body.oncontextmenu = function() { return false; };
Mike Frysingercc114512017-09-11 21:39:17 -04001516 this.contextMenu.setDocument(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07001517
1518 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001519 var screenNode = this.scrollPort_.getScreenNode();
Joel Hockeyd4fca732019-09-20 16:57:03 -07001520 screenNode.addEventListener(
1521 'mousedown', /** @type {!EventListener} */ (onMouse));
1522 screenNode.addEventListener(
1523 'mouseup', /** @type {!EventListener} */ (onMouse));
1524 screenNode.addEventListener(
1525 'mousemove', /** @type {!EventListener} */ (onMouse));
rginda4bba5e12012-06-20 16:15:30 -07001526 this.scrollPort_.onScrollWheel = onMouse;
1527
Joel Hockeyd4fca732019-09-20 16:57:03 -07001528 screenNode.addEventListener(
1529 'keydown',
1530 /** @type {!EventListener} */ (this.onKeyboardActivity_.bind(this)));
Mike Frysinger02ded6d2018-06-21 14:25:20 -04001531
Toni Barzic0bfa8922013-11-22 11:18:35 -08001532 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001533 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001534 // Listen for mousedown events on the screenNode as in FF the focus
1535 // events don't bubble.
1536 screenNode.addEventListener('mousedown', function() {
1537 setTimeout(this.onFocusChange_.bind(this, true));
1538 }.bind(this));
1539
Toni Barzic0bfa8922013-11-22 11:18:35 -08001540 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001541 'blur', this.onFocusChange_.bind(this, false));
1542
1543 var style = this.document_.createElement('style');
Joel Hockeyd36efd62019-09-30 14:16:20 -07001544 style.textContent = `
1545.cursor-node[focus="false"] {
1546 box-sizing: border-box;
1547 background-color: transparent !important;
1548 border-width: 2px;
1549 border-style: solid;
1550}
1551menu {
1552 margin: 0;
1553 padding: 0;
1554 cursor: var(--hterm-mouse-cursor-pointer);
1555}
1556menuitem {
1557 white-space: nowrap;
1558 border-bottom: 1px dashed;
1559 display: block;
1560 padding: 0.3em 0.3em 0 0.3em;
1561}
1562menuitem.separator {
1563 border-bottom: none;
1564 height: 0.5em;
1565 padding: 0;
1566}
1567menuitem:hover {
1568 color: var(--hterm-cursor-color);
1569}
1570.wc-node {
1571 display: inline-block;
1572 text-align: center;
1573 width: calc(var(--hterm-charsize-width) * 2);
1574 line-height: var(--hterm-charsize-height);
1575}
1576:root {
1577 --hterm-charsize-width: ${this.scrollPort_.characterSize.width}px;
1578 --hterm-charsize-height: ${this.scrollPort_.characterSize.height}px;
1579 /* Default position hides the cursor for when the window is initializing. */
1580 --hterm-cursor-offset-col: -1;
1581 --hterm-cursor-offset-row: -1;
1582 --hterm-blink-node-duration: 0.7s;
1583 --hterm-mouse-cursor-default: default;
1584 --hterm-mouse-cursor-text: text;
1585 --hterm-mouse-cursor-pointer: pointer;
1586 --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);
1587}
1588.uri-node:hover {
1589 text-decoration: underline;
1590 cursor: var(--hterm-mouse-cursor-pointer);
1591}
1592@keyframes blink {
1593 from { opacity: 1.0; }
1594 to { opacity: 0.0; }
1595}
1596.blink-node {
1597 animation-name: blink;
1598 animation-duration: var(--hterm-blink-node-duration);
1599 animation-iteration-count: infinite;
1600 animation-timing-function: ease-in-out;
1601 animation-direction: alternate;
1602}`;
Mike Frysingerb74a6472018-06-22 13:37:08 -04001603 // Insert this stock style as the first node so that any user styles will
1604 // override w/out having to use !important everywhere. The rules above mix
1605 // runtime variables with default ones designed to be overridden by the user,
1606 // but we can wait for a concrete case from the users to determine the best
1607 // way to split the sheet up to before & after the user-css settings.
1608 this.document_.head.insertBefore(style, this.document_.head.firstChild);
rginda8e92a692012-05-20 19:37:20 -07001609
rginda8ba33642011-12-14 12:31:31 -08001610 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001611 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001612 this.cursorNode_.className = 'cursor-node';
Joel Hockeyd36efd62019-09-30 14:16:20 -07001613 this.cursorNode_.style.cssText = `
1614position: absolute;
1615left: calc(var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));
1616top: calc(var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));
1617display: ${this.options_.cursorVisible ? '' : 'none'};
1618width: var(--hterm-charsize-width);
1619height: var(--hterm-charsize-height);
1620background-color: var(--hterm-cursor-color);
1621border-color: var(--hterm-cursor-color);
1622-webkit-transition: opacity, background-color 100ms linear;
1623-moz-transition: opacity, background-color 100ms linear;`;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001624
Mike Frysingerf02a2cb2017-12-21 00:34:03 -05001625 this.setCursorColor();
Robert Gindafb1be6a2013-12-11 11:56:22 -08001626 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1627 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001628
rginda8ba33642011-12-14 12:31:31 -08001629 this.document_.body.appendChild(this.cursorNode_);
1630
rgindad5613292012-06-19 15:40:37 -07001631 // When 'enableMouseDragScroll' is off we reposition this element directly
1632 // under the mouse cursor after a click. This makes Chrome associate
1633 // subsequent mousemove events with the scroll-blocker. Since the
1634 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1635 // events do not cause the scrollport to scroll.
1636 //
1637 // It's a hack, but it's the cleanest way I could find.
1638 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001639 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
Raymes Khoury6dce2f82018-04-12 15:38:58 +10001640 this.scrollBlockerNode_.setAttribute('aria-hidden', 'true');
rgindad5613292012-06-19 15:40:37 -07001641 this.scrollBlockerNode_.style.cssText =
1642 ('position: absolute;' +
1643 'top: -99px;' +
1644 'display: block;' +
1645 'width: 10px;' +
1646 'height: 10px;');
1647 this.document_.body.appendChild(this.scrollBlockerNode_);
1648
rgindad5613292012-06-19 15:40:37 -07001649 this.scrollPort_.onScrollWheel = onMouse;
1650 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1651 ].forEach(function(event) {
1652 this.scrollBlockerNode_.addEventListener(event, onMouse);
Joel Hockeyd4fca732019-09-20 16:57:03 -07001653 this.cursorNode_.addEventListener(
1654 event, /** @type {!EventListener} */ (onMouse));
1655 this.document_.addEventListener(
1656 event, /** @type {!EventListener} */ (onMouse));
rgindad5613292012-06-19 15:40:37 -07001657 }.bind(this));
1658
1659 this.cursorNode_.addEventListener('mousedown', function() {
1660 setTimeout(this.focus.bind(this));
1661 }.bind(this));
1662
rginda8ba33642011-12-14 12:31:31 -08001663 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001664
rginda87b86462011-12-14 13:48:03 -08001665 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001666 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001667};
1668
rginda0918b652012-04-04 11:26:24 -07001669/**
1670 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001671 *
Joel Hockey0f933582019-08-27 18:01:51 -07001672 * @return {!Document}
rginda0918b652012-04-04 11:26:24 -07001673 */
rginda87b86462011-12-14 13:48:03 -08001674hterm.Terminal.prototype.getDocument = function() {
1675 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001676};
1677
1678/**
rginda0918b652012-04-04 11:26:24 -07001679 * Focus the terminal.
1680 */
1681hterm.Terminal.prototype.focus = function() {
1682 this.scrollPort_.focus();
1683};
1684
1685/**
Theodore Duboiscea9b782019-09-02 17:48:00 -07001686 * Unfocus the terminal.
1687 */
1688hterm.Terminal.prototype.blur = function() {
1689 this.scrollPort_.blur();
1690};
1691
1692/**
rginda8ba33642011-12-14 12:31:31 -08001693 * Return the HTML Element for a given row index.
1694 *
1695 * This is a method from the RowProvider interface. The ScrollPort uses
1696 * it to fetch rows on demand as they are scrolled into view.
1697 *
1698 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1699 * pairs to conserve memory.
1700 *
Joel Hockey0f933582019-08-27 18:01:51 -07001701 * @param {number} index The zero-based row index, measured relative to the
rginda8ba33642011-12-14 12:31:31 -08001702 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001703 * largest indices.
Joel Hockey0f933582019-08-27 18:01:51 -07001704 * @return {!Element} The 'x-row' element containing for the requested row.
Joel Hockeyd4fca732019-09-20 16:57:03 -07001705 * @override
rginda8ba33642011-12-14 12:31:31 -08001706 */
1707hterm.Terminal.prototype.getRowNode = function(index) {
1708 if (index < this.scrollbackRows_.length)
1709 return this.scrollbackRows_[index];
1710
1711 var screenIndex = index - this.scrollbackRows_.length;
1712 return this.screen_.rowsArray[screenIndex];
1713};
1714
1715/**
1716 * Return the text content for a given range of rows.
1717 *
1718 * This is a method from the RowProvider interface. The ScrollPort uses
1719 * it to fetch text content on demand when the user attempts to copy their
1720 * selection to the clipboard.
1721 *
Joel Hockey0f933582019-08-27 18:01:51 -07001722 * @param {number} start The zero-based row index to start from, measured
rginda8ba33642011-12-14 12:31:31 -08001723 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001724 * always have the largest indices.
Joel Hockey0f933582019-08-27 18:01:51 -07001725 * @param {number} end The zero-based row index to end on, measured
rginda8ba33642011-12-14 12:31:31 -08001726 * relative to the start of the scrollback buffer.
1727 * @return {string} A single string containing the text value of the range of
1728 * rows. Lines will be newline delimited, with no trailing newline.
1729 */
1730hterm.Terminal.prototype.getRowsText = function(start, end) {
1731 var ary = [];
1732 for (var i = start; i < end; i++) {
1733 var node = this.getRowNode(i);
1734 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001735 if (i < end - 1 && !node.getAttribute('line-overflow'))
1736 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001737 }
1738
rgindaa09e7332012-08-17 12:49:51 -07001739 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001740};
1741
1742/**
1743 * Return the text content for a given row.
1744 *
1745 * This is a method from the RowProvider interface. The ScrollPort uses
1746 * it to fetch text content on demand when the user attempts to copy their
1747 * selection to the clipboard.
1748 *
Joel Hockey0f933582019-08-27 18:01:51 -07001749 * @param {number} index The zero-based row index to return, measured
rginda8ba33642011-12-14 12:31:31 -08001750 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001751 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001752 * @return {string} A string containing the text value of the selected row.
1753 */
1754hterm.Terminal.prototype.getRowText = function(index) {
1755 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001756 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001757};
1758
1759/**
1760 * Return the total number of rows in the addressable screen and in the
1761 * scrollback buffer of this terminal.
1762 *
1763 * This is a method from the RowProvider interface. The ScrollPort uses
1764 * it to compute the size of the scrollbar.
1765 *
Joel Hockey0f933582019-08-27 18:01:51 -07001766 * @return {number} The number of rows in this terminal.
Joel Hockeyd4fca732019-09-20 16:57:03 -07001767 * @override
rginda8ba33642011-12-14 12:31:31 -08001768 */
1769hterm.Terminal.prototype.getRowCount = function() {
1770 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1771};
1772
1773/**
1774 * Create DOM nodes for new rows and append them to the end of the terminal.
1775 *
1776 * This is the only correct way to add a new DOM node for a row. Notice that
1777 * the new row is appended to the bottom of the list of rows, and does not
1778 * require renumbering (of the rowIndex property) of previous rows.
1779 *
1780 * If you think you want a new blank row somewhere in the middle of the
1781 * terminal, look into moveRows_().
1782 *
1783 * This method does not pay attention to vtScrollTop/Bottom, since you should
1784 * be using moveRows() in cases where they would matter.
1785 *
1786 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001787 *
1788 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001789 */
1790hterm.Terminal.prototype.appendRows_ = function(count) {
1791 var cursorRow = this.screen_.rowsArray.length;
1792 var offset = this.scrollbackRows_.length + cursorRow;
1793 for (var i = 0; i < count; i++) {
1794 var row = this.document_.createElement('x-row');
1795 row.appendChild(this.document_.createTextNode(''));
1796 row.rowIndex = offset + i;
1797 this.screen_.pushRow(row);
1798 }
1799
1800 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1801 if (extraRows > 0) {
1802 var ary = this.screen_.shiftRows(extraRows);
1803 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001804 if (this.scrollPort_.isScrolledEnd)
1805 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001806 }
1807
1808 if (cursorRow >= this.screen_.rowsArray.length)
1809 cursorRow = this.screen_.rowsArray.length - 1;
1810
rginda87b86462011-12-14 13:48:03 -08001811 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001812};
1813
1814/**
1815 * Relocate rows from one part of the addressable screen to another.
1816 *
1817 * This is used to recycle rows during VT scrolls (those which are driven
1818 * by VT commands, rather than by the user manipulating the scrollbar.)
1819 *
1820 * In this case, the blank lines scrolled into the scroll region are made of
1821 * the nodes we scrolled off. These have their rowIndex properties carefully
1822 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001823 *
1824 * @param {number} fromIndex The start index.
1825 * @param {number} count The number of rows to move.
1826 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001827 */
1828hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1829 var ary = this.screen_.removeRows(fromIndex, count);
1830 this.screen_.insertRows(toIndex, ary);
1831
1832 var start, end;
1833 if (fromIndex < toIndex) {
1834 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001835 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001836 } else {
1837 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001838 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001839 }
1840
1841 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001842 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001843};
1844
1845/**
1846 * Renumber the rowIndex property of the given range of rows.
1847 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001848 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08001849 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001850 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001851 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05001852 *
1853 * @param {number} start The start index.
1854 * @param {number} end The end index.
Joel Hockey0f933582019-08-27 18:01:51 -07001855 * @param {!hterm.Screen=} opt_screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08001856 */
Robert Ginda40932892012-12-10 17:26:40 -08001857hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1858 var screen = opt_screen || this.screen_;
1859
rginda8ba33642011-12-14 12:31:31 -08001860 var offset = this.scrollbackRows_.length;
1861 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001862 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001863 }
1864};
1865
1866/**
1867 * Print a string to the terminal.
1868 *
1869 * This respects the current insert and wraparound modes. It will add new lines
1870 * to the end of the terminal, scrolling off the top into the scrollback buffer
1871 * if necessary.
1872 *
1873 * The string is *not* parsed for escape codes. Use the interpret() method if
1874 * that's what you're after.
1875 *
Mike Frysingerfd449572019-09-23 03:18:14 -04001876 * @param {string} str The string to print.
rginda8ba33642011-12-14 12:31:31 -08001877 */
1878hterm.Terminal.prototype.print = function(str) {
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001879 this.scheduleSyncCursorPosition_();
1880
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001881 // Basic accessibility output for the screen reader.
Raymes Khoury177aec72018-06-26 10:58:53 +10001882 this.accessibilityReader_.announce(str);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001883
rgindaa9abdd82012-08-06 18:05:09 -07001884 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001885
Ricky Liang48f05cb2013-12-31 23:35:29 +08001886 var strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04001887 // Fun edge case: If the string only contains zero width codepoints (like
1888 // combining characters), we make sure to iterate at least once below.
1889 if (strWidth == 0 && str)
1890 strWidth = 1;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001891
1892 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001893 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1894 this.screen_.commitLineOverflow();
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001895 this.newLine(true);
rgindaa09e7332012-08-17 12:49:51 -07001896 }
rgindaa19afe22012-01-25 15:40:22 -08001897
Ricky Liang48f05cb2013-12-31 23:35:29 +08001898 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001899 var didOverflow = false;
1900 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001901
rgindaa9abdd82012-08-06 18:05:09 -07001902 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1903 didOverflow = true;
1904 count = this.screenSize.width - this.screen_.cursorPosition.column;
1905 }
rgindaa19afe22012-01-25 15:40:22 -08001906
rgindaa9abdd82012-08-06 18:05:09 -07001907 if (didOverflow && !this.options_.wraparound) {
1908 // If the string overflowed the line but wraparound is off, then the
1909 // last printed character should be the last of the string.
1910 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001911 substr = lib.wc.substr(str, startOffset, count - 1) +
1912 lib.wc.substr(str, strWidth - 1);
1913 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001914 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001915 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001916 }
rgindaa19afe22012-01-25 15:40:22 -08001917
Ricky Liang48f05cb2013-12-31 23:35:29 +08001918 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1919 for (var i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001920 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
1921 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001922
1923 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001924 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001925 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001926 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001927 }
1928 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001929 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07001930 }
1931
1932 this.screen_.maybeClipCurrentRow();
1933 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001934 }
rginda8ba33642011-12-14 12:31:31 -08001935
rginda9f5222b2012-03-05 11:53:28 -08001936 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001937 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001938};
1939
1940/**
rginda87b86462011-12-14 13:48:03 -08001941 * Set the VT scroll region.
1942 *
rginda87b86462011-12-14 13:48:03 -08001943 * This also resets the cursor position to the absolute (0, 0) position, since
1944 * that's what xterm appears to do.
1945 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001946 * Setting the scroll region to the full height of the terminal will clear
1947 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1948 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1949 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1950 * continue to work as most users would expect.
1951 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001952 * @param {?number} scrollTop The zero-based top of the scroll region.
1953 * @param {?number} scrollBottom The zero-based bottom of the scroll region,
rginda87b86462011-12-14 13:48:03 -08001954 * inclusive.
1955 */
1956hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001957 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001958 this.vtScrollTop_ = null;
1959 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001960 } else {
1961 this.vtScrollTop_ = scrollTop;
1962 this.vtScrollBottom_ = scrollBottom;
1963 }
rginda87b86462011-12-14 13:48:03 -08001964};
1965
1966/**
rginda8ba33642011-12-14 12:31:31 -08001967 * Return the top row index according to the VT.
1968 *
1969 * This will return 0 unless the terminal has been told to restrict scrolling
1970 * to some lower row. It is used for some VT cursor positioning and scrolling
1971 * commands.
1972 *
Joel Hockey0f933582019-08-27 18:01:51 -07001973 * @return {number} The topmost row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001974 */
1975hterm.Terminal.prototype.getVTScrollTop = function() {
1976 if (this.vtScrollTop_ != null)
1977 return this.vtScrollTop_;
1978
1979 return 0;
rginda87b86462011-12-14 13:48:03 -08001980};
rginda8ba33642011-12-14 12:31:31 -08001981
1982/**
1983 * Return the bottom row index according to the VT.
1984 *
1985 * This will return the height of the terminal unless the it has been told to
1986 * restrict scrolling to some higher row. It is used for some VT cursor
1987 * positioning and scrolling commands.
1988 *
Joel Hockey0f933582019-08-27 18:01:51 -07001989 * @return {number} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001990 */
1991hterm.Terminal.prototype.getVTScrollBottom = function() {
1992 if (this.vtScrollBottom_ != null)
1993 return this.vtScrollBottom_;
1994
rginda87b86462011-12-14 13:48:03 -08001995 return this.screenSize.height - 1;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001996};
rginda8ba33642011-12-14 12:31:31 -08001997
1998/**
1999 * Process a '\n' character.
2000 *
2001 * If the cursor is on the final row of the terminal this will append a new
2002 * blank row to the screen and scroll the topmost row into the scrollback
2003 * buffer.
2004 *
2005 * Otherwise, this moves the cursor to column zero of the next row.
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002006 *
2007 * @param {boolean=} dueToOverflow Whether the newline is due to wraparound of
2008 * the terminal.
rginda8ba33642011-12-14 12:31:31 -08002009 */
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002010hterm.Terminal.prototype.newLine = function(dueToOverflow = false) {
2011 if (!dueToOverflow)
2012 this.accessibilityReader_.newLine();
2013
Robert Ginda9937abc2013-07-25 16:09:23 -07002014 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
2015 this.screen_.rowsArray.length - 1);
2016
2017 if (this.vtScrollBottom_ != null) {
2018 // A VT Scroll region is active, we never append new rows.
2019 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
2020 // We're at the end of the VT Scroll Region, perform a VT scroll.
2021 this.vtScrollUp(1);
2022 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
2023 } else if (cursorAtEndOfScreen) {
2024 // We're at the end of the screen, the only thing to do is put the
2025 // cursor to column 0.
2026 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
2027 } else {
2028 // Anywhere else, advance the cursor row, and reset the column.
2029 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
2030 }
2031 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07002032 // We're at the end of the screen. Append a new row to the terminal,
2033 // shifting the top row into the scrollback.
2034 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08002035 } else {
rginda87b86462011-12-14 13:48:03 -08002036 // Anywhere else in the screen just moves the cursor.
2037 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08002038 }
2039};
2040
2041/**
2042 * Like newLine(), except maintain the cursor column.
2043 */
2044hterm.Terminal.prototype.lineFeed = function() {
2045 var column = this.screen_.cursorPosition.column;
2046 this.newLine();
2047 this.setCursorColumn(column);
2048};
2049
2050/**
rginda87b86462011-12-14 13:48:03 -08002051 * If autoCarriageReturn is set then newLine(), else lineFeed().
2052 */
2053hterm.Terminal.prototype.formFeed = function() {
2054 if (this.options_.autoCarriageReturn) {
2055 this.newLine();
2056 } else {
2057 this.lineFeed();
2058 }
2059};
2060
2061/**
2062 * Move the cursor up one row, possibly inserting a blank line.
2063 *
2064 * The cursor column is not changed.
2065 */
2066hterm.Terminal.prototype.reverseLineFeed = function() {
2067 var scrollTop = this.getVTScrollTop();
2068 var currentRow = this.screen_.cursorPosition.row;
2069
2070 if (currentRow == scrollTop) {
2071 this.insertLines(1);
2072 } else {
2073 this.setAbsoluteCursorRow(currentRow - 1);
2074 }
2075};
2076
2077/**
rginda8ba33642011-12-14 12:31:31 -08002078 * Replace all characters to the left of the current cursor with the space
2079 * character.
2080 *
2081 * TODO(rginda): This should probably *remove* the characters (not just replace
2082 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07002083 * position.
rginda8ba33642011-12-14 12:31:31 -08002084 */
2085hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08002086 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002087 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002088 const count = cursor.column + 1;
Mike Frysinger73e56462019-07-17 00:23:46 -05002089 this.screen_.overwriteString(' '.repeat(count), count);
rginda87b86462011-12-14 13:48:03 -08002090 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002091};
2092
2093/**
David Benjamin684a9b72012-05-01 17:19:58 -04002094 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08002095 *
2096 * The cursor position is unchanged.
2097 *
Robert Gindaf2547f12012-10-25 20:36:21 -07002098 * If the current background color is not the default background color this
2099 * will insert spaces rather than delete. This is unfortunate because the
2100 * trailing space will affect text selection, but it's difficult to come up
2101 * with a way to style empty space that wouldn't trip up the hterm.Screen
2102 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07002103 *
2104 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
2105 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
2106 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05002107 *
Joel Hockey0f933582019-08-27 18:01:51 -07002108 * @param {number=} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08002109 */
2110hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07002111 if (this.screen_.cursorPosition.overflow)
2112 return;
2113
Robert Ginda7fd57082012-09-25 14:41:47 -07002114 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
2115 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07002116
2117 if (this.screen_.textAttributes.background ===
2118 this.screen_.textAttributes.DEFAULT_COLOR) {
2119 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08002120 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07002121 this.screen_.cursorPosition.column + count) {
2122 this.screen_.deleteChars(count);
2123 this.clearCursorOverflow();
2124 return;
2125 }
2126 }
2127
rginda87b86462011-12-14 13:48:03 -08002128 var cursor = this.saveCursor();
Mike Frysinger73e56462019-07-17 00:23:46 -05002129 this.screen_.overwriteString(' '.repeat(count), count);
rginda87b86462011-12-14 13:48:03 -08002130 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002131 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002132};
2133
2134/**
2135 * Erase the current line.
2136 *
2137 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002138 */
2139hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08002140 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002141 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08002142 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002143 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002144};
2145
2146/**
David Benjamina08d78f2012-05-05 00:28:49 -04002147 * Erase all characters from the start of the screen to the current cursor
2148 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002149 *
2150 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002151 */
2152hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08002153 var cursor = this.saveCursor();
2154
2155 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002156
David Benjamina08d78f2012-05-05 00:28:49 -04002157 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002158 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002159 this.screen_.clearCursorRow();
2160 }
2161
rginda87b86462011-12-14 13:48:03 -08002162 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002163 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002164};
2165
2166/**
2167 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002168 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002169 *
2170 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002171 */
2172hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08002173 var cursor = this.saveCursor();
2174
2175 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002176
David Benjamina08d78f2012-05-05 00:28:49 -04002177 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08002178 for (var i = cursor.row + 1; i <= bottom; i++) {
2179 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002180 this.screen_.clearCursorRow();
2181 }
2182
rginda87b86462011-12-14 13:48:03 -08002183 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002184 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002185};
2186
2187/**
2188 * Fill the terminal with a given character.
2189 *
2190 * This methods does not respect the VT scroll region.
2191 *
2192 * @param {string} ch The character to use for the fill.
2193 */
2194hterm.Terminal.prototype.fill = function(ch) {
2195 var cursor = this.saveCursor();
2196
2197 this.setAbsoluteCursorPosition(0, 0);
2198 for (var row = 0; row < this.screenSize.height; row++) {
2199 for (var col = 0; col < this.screenSize.width; col++) {
2200 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002201 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002202 }
2203 }
2204
2205 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002206};
2207
2208/**
rginda9ea433c2012-03-16 11:57:00 -07002209 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002210 *
rginda9ea433c2012-03-16 11:57:00 -07002211 * This does not respect the scroll region.
2212 *
Joel Hockey0f933582019-08-27 18:01:51 -07002213 * @param {!hterm.Screen=} opt_screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002214 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002215 */
rginda9ea433c2012-03-16 11:57:00 -07002216hterm.Terminal.prototype.clearHome = function(opt_screen) {
2217 var screen = opt_screen || this.screen_;
2218 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002219
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002220 this.accessibilityReader_.clear();
2221
rginda11057d52012-04-25 12:29:56 -07002222 if (bottom == 0) {
2223 // Empty screen, nothing to do.
2224 return;
2225 }
2226
rgindae4d29232012-01-19 10:47:13 -08002227 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002228 screen.setCursorPosition(i, 0);
2229 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002230 }
2231
rginda9ea433c2012-03-16 11:57:00 -07002232 screen.setCursorPosition(0, 0);
2233};
2234
2235/**
2236 * Erase the entire display without changing the cursor position.
2237 *
2238 * The cursor position is unchanged. This does not respect the scroll
2239 * region.
2240 *
Joel Hockey0f933582019-08-27 18:01:51 -07002241 * @param {!hterm.Screen=} opt_screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002242 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002243 */
2244hterm.Terminal.prototype.clear = function(opt_screen) {
2245 var screen = opt_screen || this.screen_;
2246 var cursor = screen.cursorPosition.clone();
2247 this.clearHome(screen);
2248 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002249};
2250
2251/**
2252 * VT command to insert lines at the current cursor row.
2253 *
2254 * This respects the current scroll region. Rows pushed off the bottom are
2255 * lost (they won't show up in the scrollback buffer).
2256 *
Joel Hockey0f933582019-08-27 18:01:51 -07002257 * @param {number} count The number of lines to insert.
rginda8ba33642011-12-14 12:31:31 -08002258 */
2259hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07002260 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002261
2262 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002263 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002264
Robert Ginda579186b2012-09-26 11:40:04 -07002265 // The moveCount is the number of rows we need to relocate to make room for
2266 // the new row(s). The count is the distance to move them.
2267 var moveCount = bottom - cursorRow - count + 1;
2268 if (moveCount)
2269 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08002270
Robert Ginda579186b2012-09-26 11:40:04 -07002271 for (var i = count - 1; i >= 0; i--) {
2272 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002273 this.screen_.clearCursorRow();
2274 }
rginda8ba33642011-12-14 12:31:31 -08002275};
2276
2277/**
2278 * VT command to delete lines at the current cursor row.
2279 *
2280 * New rows are added to the bottom of scroll region to take their place. New
2281 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002282 *
2283 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002284 */
2285hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08002286 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002287
rginda87b86462011-12-14 13:48:03 -08002288 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08002289 var bottom = this.getVTScrollBottom();
2290
rginda87b86462011-12-14 13:48:03 -08002291 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002292 count = Math.min(count, maxCount);
2293
rginda87b86462011-12-14 13:48:03 -08002294 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08002295 if (count != maxCount)
2296 this.moveRows_(top, count, moveStart);
2297
2298 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002299 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002300 this.screen_.clearCursorRow();
2301 }
2302
rginda87b86462011-12-14 13:48:03 -08002303 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002304 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002305};
2306
2307/**
2308 * Inserts the given number of spaces at the current cursor position.
2309 *
rginda87b86462011-12-14 13:48:03 -08002310 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002311 *
2312 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002313 */
2314hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08002315 var cursor = this.saveCursor();
2316
Mike Frysinger73e56462019-07-17 00:23:46 -05002317 const ws = ' '.repeat(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002318 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002319 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002320
2321 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002322 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002323};
2324
2325/**
2326 * Forward-delete the specified number of characters starting at the cursor
2327 * position.
2328 *
Joel Hockey0f933582019-08-27 18:01:51 -07002329 * @param {number} count The number of characters to delete.
rginda8ba33642011-12-14 12:31:31 -08002330 */
2331hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002332 var deleted = this.screen_.deleteChars(count);
2333 if (deleted && !this.screen_.textAttributes.isDefault()) {
2334 var cursor = this.saveCursor();
2335 this.setCursorColumn(this.screenSize.width - deleted);
Mike Frysinger73e56462019-07-17 00:23:46 -05002336 this.screen_.insertString(' '.repeat(deleted));
Robert Ginda7fd57082012-09-25 14:41:47 -07002337 this.restoreCursor(cursor);
2338 }
2339
David Benjamin54e8bf62012-06-01 22:31:40 -04002340 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002341};
2342
2343/**
2344 * Shift rows in the scroll region upwards by a given number of lines.
2345 *
2346 * New rows are inserted at the bottom of the scroll region to fill the
2347 * vacated rows. The new rows not filled out with the current text attributes.
2348 *
2349 * This function does not affect the scrollback rows at all. Rows shifted
2350 * off the top are lost.
2351 *
rginda87b86462011-12-14 13:48:03 -08002352 * The cursor position is not altered.
2353 *
Joel Hockey0f933582019-08-27 18:01:51 -07002354 * @param {number} count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002355 */
2356hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002357 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002358
rginda87b86462011-12-14 13:48:03 -08002359 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002360 this.deleteLines(count);
2361
rginda87b86462011-12-14 13:48:03 -08002362 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002363};
2364
2365/**
2366 * Shift rows below the cursor down by a given number of lines.
2367 *
2368 * This function respects the current scroll region.
2369 *
2370 * New rows are inserted at the top of the scroll region to fill the
2371 * vacated rows. The new rows not filled out with the current text attributes.
2372 *
2373 * This function does not affect the scrollback rows at all. Rows shifted
2374 * off the bottom are lost.
2375 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07002376 * @param {number} count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002377 */
Joel Hockeyd4fca732019-09-20 16:57:03 -07002378hterm.Terminal.prototype.vtScrollDown = function(count) {
rginda87b86462011-12-14 13:48:03 -08002379 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002380
rginda87b86462011-12-14 13:48:03 -08002381 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
Joel Hockeyd4fca732019-09-20 16:57:03 -07002382 this.insertLines(count);
rginda8ba33642011-12-14 12:31:31 -08002383
rginda87b86462011-12-14 13:48:03 -08002384 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002385};
2386
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002387/**
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002388 * Enable accessibility-friendly features that have a performance impact.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002389 *
2390 * This will generate additional DOM nodes in an aria-live region that will
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002391 * cause Assitive Technology to announce the output of the terminal. It also
2392 * enables other features that aid assistive technology. All the features gated
2393 * behind this flag have a performance impact on the terminal which is why they
2394 * are made optional.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002395 *
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002396 * @param {boolean} enabled Whether to enable accessibility-friendly features.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002397 */
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002398hterm.Terminal.prototype.setAccessibilityEnabled = function(enabled) {
Raymes Khoury177aec72018-06-26 10:58:53 +10002399 this.accessibilityReader_.setAccessibilityEnabled(enabled);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002400};
rginda87b86462011-12-14 13:48:03 -08002401
rginda8ba33642011-12-14 12:31:31 -08002402/**
2403 * Set the cursor position.
2404 *
2405 * The cursor row is relative to the scroll region if the terminal has
2406 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2407 *
Joel Hockey0f933582019-08-27 18:01:51 -07002408 * @param {number} row The new zero-based cursor row.
2409 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002410 */
2411hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2412 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002413 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002414 } else {
rginda87b86462011-12-14 13:48:03 -08002415 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002416 }
rginda87b86462011-12-14 13:48:03 -08002417};
rginda8ba33642011-12-14 12:31:31 -08002418
Evan Jones2600d4f2016-12-06 09:29:36 -05002419/**
2420 * Move the cursor relative to its current position.
2421 *
2422 * @param {number} row
2423 * @param {number} column
2424 */
rginda87b86462011-12-14 13:48:03 -08002425hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2426 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002427 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2428 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002429 this.screen_.setCursorPosition(row, column);
2430};
2431
Evan Jones2600d4f2016-12-06 09:29:36 -05002432/**
2433 * Move the cursor to the specified position.
2434 *
2435 * @param {number} row
2436 * @param {number} column
2437 */
rginda87b86462011-12-14 13:48:03 -08002438hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002439 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2440 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002441 this.screen_.setCursorPosition(row, column);
2442};
2443
2444/**
2445 * Set the cursor column.
2446 *
Joel Hockey0f933582019-08-27 18:01:51 -07002447 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002448 */
2449hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002450 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002451};
2452
2453/**
2454 * Return the cursor column.
2455 *
Joel Hockey0f933582019-08-27 18:01:51 -07002456 * @return {number} The zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002457 */
2458hterm.Terminal.prototype.getCursorColumn = function() {
2459 return this.screen_.cursorPosition.column;
2460};
2461
2462/**
2463 * Set the cursor row.
2464 *
2465 * The cursor row is relative to the scroll region if the terminal has
2466 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2467 *
Joel Hockey0f933582019-08-27 18:01:51 -07002468 * @param {number} row The new cursor row.
rginda8ba33642011-12-14 12:31:31 -08002469 */
rginda87b86462011-12-14 13:48:03 -08002470hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2471 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002472};
2473
2474/**
2475 * Return the cursor row.
2476 *
Joel Hockey0f933582019-08-27 18:01:51 -07002477 * @return {number} The zero-based cursor row.
rginda8ba33642011-12-14 12:31:31 -08002478 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002479hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002480 return this.screen_.cursorPosition.row;
2481};
2482
2483/**
2484 * Request that the ScrollPort redraw itself soon.
2485 *
2486 * The redraw will happen asynchronously, soon after the call stack winds down.
2487 * Multiple calls will be coalesced into a single redraw.
2488 */
2489hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002490 if (this.timeouts_.redraw)
2491 return;
rginda8ba33642011-12-14 12:31:31 -08002492
2493 var self = this;
rginda87b86462011-12-14 13:48:03 -08002494 this.timeouts_.redraw = setTimeout(function() {
2495 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002496 self.scrollPort_.redraw_();
2497 }, 0);
2498};
2499
2500/**
2501 * Request that the ScrollPort be scrolled to the bottom.
2502 *
2503 * The scroll will happen asynchronously, soon after the call stack winds down.
2504 * Multiple calls will be coalesced into a single scroll.
2505 *
2506 * This affects the scrollbar position of the ScrollPort, and has nothing to
2507 * do with the VT scroll commands.
2508 */
2509hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2510 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002511 return;
rginda8ba33642011-12-14 12:31:31 -08002512
2513 var self = this;
2514 this.timeouts_.scrollDown = setTimeout(function() {
2515 delete self.timeouts_.scrollDown;
2516 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2517 }, 10);
2518};
2519
2520/**
2521 * Move the cursor up a specified number of rows.
2522 *
Joel Hockey0f933582019-08-27 18:01:51 -07002523 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002524 */
2525hterm.Terminal.prototype.cursorUp = function(count) {
Joel Hockey0f933582019-08-27 18:01:51 -07002526 this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002527};
2528
2529/**
2530 * Move the cursor down a specified number of rows.
2531 *
Joel Hockey0f933582019-08-27 18:01:51 -07002532 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002533 */
2534hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002535 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002536 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2537 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2538 this.screenSize.height - 1);
2539
rgindacbbd7482012-06-13 15:06:16 -07002540 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002541 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002542 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002543};
2544
2545/**
2546 * Move the cursor left a specified number of columns.
2547 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002548 * If reverse wraparound mode is enabled and the previous row wrapped into
2549 * the current row then we back up through the wraparound as well.
2550 *
Joel Hockey0f933582019-08-27 18:01:51 -07002551 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002552 */
2553hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002554 count = count || 1;
2555
2556 if (count < 1)
2557 return;
2558
2559 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002560 if (this.options_.reverseWraparound) {
2561 if (this.screen_.cursorPosition.overflow) {
2562 // If this cursor is in the right margin, consume one count to get it
2563 // back to the last column. This only applies when we're in reverse
2564 // wraparound mode.
2565 count--;
2566 this.clearCursorOverflow();
2567
2568 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002569 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002570 }
2571
Robert Gindabfb32622014-07-17 13:20:27 -07002572 var newRow = this.screen_.cursorPosition.row;
2573 var newColumn = currentColumn - count;
2574 if (newColumn < 0) {
2575 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2576 if (newRow < 0) {
2577 // xterm also wraps from row 0 to the last row.
2578 newRow = this.screenSize.height + newRow % this.screenSize.height;
2579 }
2580 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2581 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002582
Robert Gindabfb32622014-07-17 13:20:27 -07002583 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2584
2585 } else {
2586 var newColumn = Math.max(currentColumn - count, 0);
2587 this.setCursorColumn(newColumn);
2588 }
rginda8ba33642011-12-14 12:31:31 -08002589};
2590
2591/**
2592 * Move the cursor right a specified number of columns.
2593 *
Joel Hockey0f933582019-08-27 18:01:51 -07002594 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002595 */
2596hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002597 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002598
2599 if (count < 1)
2600 return;
2601
rgindacbbd7482012-06-13 15:06:16 -07002602 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002603 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002604 this.setCursorColumn(column);
2605};
2606
2607/**
2608 * Reverse the foreground and background colors of the terminal.
2609 *
2610 * This only affects text that was drawn with no attributes.
2611 *
2612 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2613 * been drawn with attributes that happen to coincide with the default
2614 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002615 *
2616 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002617 */
2618hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002619 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002620 if (state) {
Mike Frysinger31cb1562017-07-31 23:44:18 -04002621 this.scrollPort_.setForegroundColor(this.backgroundColor_);
2622 this.scrollPort_.setBackgroundColor(this.foregroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002623 } else {
Mike Frysinger31cb1562017-07-31 23:44:18 -04002624 this.scrollPort_.setForegroundColor(this.foregroundColor_);
2625 this.scrollPort_.setBackgroundColor(this.backgroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002626 }
2627};
2628
2629/**
rginda87b86462011-12-14 13:48:03 -08002630 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002631 *
2632 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002633 */
2634hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002635 this.cursorNode_.style.backgroundColor =
2636 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002637
2638 var self = this;
2639 setTimeout(function() {
Matheus Fernandes2d733082017-09-11 06:43:01 -04002640 self.restyleCursor_();
rginda6d397402012-01-17 10:58:29 -08002641 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002642
Michael Kelly485ecd12014-06-09 11:41:56 -04002643 // bellSquelchTimeout_ affects both audio and notification bells.
2644 if (this.bellSquelchTimeout_)
2645 return;
2646
Robert Ginda92e18102013-03-14 13:56:37 -07002647 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002648 this.bellAudio_.play();
Joel Hockeyd4fca732019-09-20 16:57:03 -07002649 this.bellSequelchTimeout_ = setTimeout(() => {
2650 this.bellSquelchTimeout_ = null;
2651 }, 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002652 } else {
Joel Hockeyd4fca732019-09-20 16:57:03 -07002653 this.bellSquelchTimeout_ = null;
Robert Ginda92e18102013-03-14 13:56:37 -07002654 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002655
2656 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingera5fb83c2017-06-22 14:48:35 -07002657 var n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002658 this.bellNotificationList_.push(n);
2659 // TODO: Should we try to raise the window here?
2660 n.onclick = function() { self.closeBellNotifications_(); };
2661 }
rginda87b86462011-12-14 13:48:03 -08002662};
2663
2664/**
rginda8ba33642011-12-14 12:31:31 -08002665 * Set the origin mode bit.
2666 *
2667 * If origin mode is on, certain VT cursor and scrolling commands measure their
2668 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2669 * to the top of the addressable screen.
2670 *
2671 * Defaults to off.
2672 *
2673 * @param {boolean} state True to set origin mode, false to unset.
2674 */
2675hterm.Terminal.prototype.setOriginMode = function(state) {
2676 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002677 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002678};
2679
2680/**
2681 * Set the insert mode bit.
2682 *
2683 * If insert mode is on, existing text beyond the cursor position will be
2684 * shifted right to make room for new text. Otherwise, new text overwrites
2685 * any existing text.
2686 *
2687 * Defaults to off.
2688 *
2689 * @param {boolean} state True to set insert mode, false to unset.
2690 */
2691hterm.Terminal.prototype.setInsertMode = function(state) {
2692 this.options_.insertMode = state;
2693};
2694
2695/**
rginda87b86462011-12-14 13:48:03 -08002696 * Set the auto carriage return bit.
2697 *
2698 * If auto carriage return is on then a formfeed character is interpreted
2699 * as a newline, otherwise it's the same as a linefeed. The difference boils
2700 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002701 *
2702 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002703 */
2704hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2705 this.options_.autoCarriageReturn = state;
2706};
2707
2708/**
rginda8ba33642011-12-14 12:31:31 -08002709 * Set the wraparound mode bit.
2710 *
2711 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2712 * to the start of the following row. Otherwise, the cursor is clamped to the
2713 * end of the screen and attempts to write past it are ignored.
2714 *
2715 * Defaults to on.
2716 *
2717 * @param {boolean} state True to set wraparound mode, false to unset.
2718 */
2719hterm.Terminal.prototype.setWraparound = function(state) {
2720 this.options_.wraparound = state;
2721};
2722
2723/**
2724 * Set the reverse-wraparound mode bit.
2725 *
2726 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2727 * to the end of the previous row. Otherwise, the cursor is clamped to column
2728 * 0.
2729 *
2730 * Defaults to off.
2731 *
2732 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2733 */
2734hterm.Terminal.prototype.setReverseWraparound = function(state) {
2735 this.options_.reverseWraparound = state;
2736};
2737
2738/**
2739 * Selects between the primary and alternate screens.
2740 *
2741 * If alternate mode is on, the alternate screen is active. Otherwise the
2742 * primary screen is active.
2743 *
2744 * Swapping screens has no effect on the scrollback buffer.
2745 *
2746 * Each screen maintains its own cursor position.
2747 *
2748 * Defaults to off.
2749 *
2750 * @param {boolean} state True to set alternate mode, false to unset.
2751 */
2752hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002753 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002754 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2755
rginda35c456b2012-02-09 17:29:05 -08002756 if (this.screen_.rowsArray.length &&
2757 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2758 // If the screen changed sizes while we were away, our rowIndexes may
2759 // be incorrect.
2760 var offset = this.scrollbackRows_.length;
2761 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002762 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002763 ary[i].rowIndex = offset + i;
2764 }
2765 }
rginda8ba33642011-12-14 12:31:31 -08002766
rginda35c456b2012-02-09 17:29:05 -08002767 this.realizeWidth_(this.screenSize.width);
2768 this.realizeHeight_(this.screenSize.height);
2769 this.scrollPort_.syncScrollHeight();
2770 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002771
rginda6d397402012-01-17 10:58:29 -08002772 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002773 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002774};
2775
2776/**
2777 * Set the cursor-blink mode bit.
2778 *
2779 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2780 * a visible cursor does not blink.
2781 *
2782 * You should make sure to turn blinking off if you're going to dispose of a
2783 * terminal, otherwise you'll leak a timeout.
2784 *
2785 * Defaults to on.
2786 *
2787 * @param {boolean} state True to set cursor-blink mode, false to unset.
2788 */
2789hterm.Terminal.prototype.setCursorBlink = function(state) {
2790 this.options_.cursorBlink = state;
2791
2792 if (!state && this.timeouts_.cursorBlink) {
2793 clearTimeout(this.timeouts_.cursorBlink);
2794 delete this.timeouts_.cursorBlink;
2795 }
2796
2797 if (this.options_.cursorVisible)
2798 this.setCursorVisible(true);
2799};
2800
2801/**
2802 * Set the cursor-visible mode bit.
2803 *
2804 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2805 *
2806 * Defaults to on.
2807 *
2808 * @param {boolean} state True to set cursor-visible mode, false to unset.
2809 */
2810hterm.Terminal.prototype.setCursorVisible = function(state) {
2811 this.options_.cursorVisible = state;
2812
2813 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002814 if (this.timeouts_.cursorBlink) {
2815 clearTimeout(this.timeouts_.cursorBlink);
2816 delete this.timeouts_.cursorBlink;
2817 }
rginda87b86462011-12-14 13:48:03 -08002818 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002819 return;
2820 }
2821
rginda87b86462011-12-14 13:48:03 -08002822 this.syncCursorPosition_();
2823
2824 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002825
2826 if (this.options_.cursorBlink) {
2827 if (this.timeouts_.cursorBlink)
2828 return;
2829
Robert Gindaea2183e2014-07-17 09:51:51 -07002830 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002831 } else {
2832 if (this.timeouts_.cursorBlink) {
2833 clearTimeout(this.timeouts_.cursorBlink);
2834 delete this.timeouts_.cursorBlink;
2835 }
2836 }
2837};
2838
2839/**
Mike Frysinger225c99d2019-10-20 14:02:37 -06002840 * Pause blinking temporarily.
2841 *
2842 * When the cursor moves around, it can be helpful to momentarily pause the
2843 * blinking. This could be when the user is typing in things, or when they're
2844 * moving around with the arrow keys.
2845 */
2846hterm.Terminal.prototype.pauseCursorBlink_ = function() {
2847 if (!this.options_.cursorBlink) {
2848 return;
2849 }
2850
2851 this.cursorBlinkPause_ = true;
2852
2853 // If a timeout is already pending, reset the clock due to the new input.
2854 if (this.timeouts_.cursorBlinkPause) {
2855 clearTimeout(this.timeouts_.cursorBlinkPause);
2856 }
2857 // After 500ms, resume blinking. That seems like a good balance between user
2858 // input timings & responsiveness to resume.
2859 this.timeouts_.cursorBlinkPause = setTimeout(() => {
2860 delete this.timeouts_.cursorBlinkPause;
2861 this.cursorBlinkPause_ = false;
2862 }, 500);
2863};
2864
2865/**
rginda87b86462011-12-14 13:48:03 -08002866 * Synchronizes the visible cursor and document selection with the current
2867 * cursor coordinates.
Raymes Khourye5d48982018-08-02 09:08:32 +10002868 *
2869 * @return {boolean} True if the cursor is onscreen and synced.
rginda8ba33642011-12-14 12:31:31 -08002870 */
2871hterm.Terminal.prototype.syncCursorPosition_ = function() {
2872 var topRowIndex = this.scrollPort_.getTopRowIndex();
2873 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2874 var cursorRowIndex = this.scrollbackRows_.length +
2875 this.screen_.cursorPosition.row;
2876
Raymes Khoury15697f42018-07-17 11:37:18 +10002877 let forceSyncSelection = false;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002878 if (this.accessibilityReader_.accessibilityEnabled) {
2879 // Report the new position of the cursor for accessibility purposes.
2880 const cursorColumnIndex = this.screen_.cursorPosition.column;
2881 const cursorLineText =
2882 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
Raymes Khoury15697f42018-07-17 11:37:18 +10002883 // This will force the selection to be sync'd to the cursor position if the
2884 // user has pressed a key. Generally we would only sync the cursor position
2885 // when selection is collapsed so that if the user has selected something
2886 // we don't clear the selection by moving the selection. However when a
2887 // screen reader is used, it's intuitive for entering a key to move the
2888 // selection to the cursor.
2889 forceSyncSelection = this.accessibilityReader_.hasUserGesture;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002890 this.accessibilityReader_.afterCursorChange(
2891 cursorLineText, cursorRowIndex, cursorColumnIndex);
2892 }
2893
rginda8ba33642011-12-14 12:31:31 -08002894 if (cursorRowIndex > bottomRowIndex) {
2895 // Cursor is scrolled off screen, move it outside of the visible area.
Mike Frysinger44c32202017-08-05 01:13:09 -04002896 this.setCssVar('cursor-offset-row', '-1');
Raymes Khourye5d48982018-08-02 09:08:32 +10002897 return false;
rginda8ba33642011-12-14 12:31:31 -08002898 }
2899
Robert Gindab837c052014-08-11 11:17:51 -07002900 if (this.options_.cursorVisible &&
2901 this.cursorNode_.style.display == 'none') {
2902 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2903 this.cursorNode_.style.display = '';
2904 }
2905
Mike Frysinger44c32202017-08-05 01:13:09 -04002906 // Position the cursor using CSS variable math. If we do the math in JS,
2907 // the float math will end up being more precise than the CSS which will
2908 // cause the cursor tracking to be off.
2909 this.setCssVar(
2910 'cursor-offset-row',
2911 `${cursorRowIndex - topRowIndex} + ` +
2912 `${this.scrollPort_.visibleRowTopMargin}px`);
2913 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08002914
2915 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04002916 '(' + this.screen_.cursorPosition.column +
2917 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08002918 ')');
2919
2920 // Update the caret for a11y purposes.
2921 var selection = this.document_.getSelection();
Raymes Khoury15697f42018-07-17 11:37:18 +10002922 if (selection && (selection.isCollapsed || forceSyncSelection)) {
rginda87b86462011-12-14 13:48:03 -08002923 this.screen_.syncSelectionCaret(selection);
Raymes Khoury15697f42018-07-17 11:37:18 +10002924 }
Raymes Khourye5d48982018-08-02 09:08:32 +10002925 return true;
rginda8ba33642011-12-14 12:31:31 -08002926};
2927
Robert Gindafb1be6a2013-12-11 11:56:22 -08002928/**
2929 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2930 * and character cell dimensions.
2931 */
Robert Ginda830583c2013-08-07 13:20:46 -07002932hterm.Terminal.prototype.restyleCursor_ = function() {
2933 var shape = this.cursorShape_;
2934
2935 if (this.cursorNode_.getAttribute('focus') == 'false') {
2936 // Always show a block cursor when unfocused.
2937 shape = hterm.Terminal.cursorShape.BLOCK;
2938 }
2939
2940 var style = this.cursorNode_.style;
2941
2942 switch (shape) {
2943 case hterm.Terminal.cursorShape.BEAM:
Robert Ginda830583c2013-08-07 13:20:46 -07002944 style.backgroundColor = 'transparent';
Joel Hockeyd4fca732019-09-20 16:57:03 -07002945 style.borderBottomStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07002946 style.borderLeftStyle = 'solid';
2947 break;
2948
2949 case hterm.Terminal.cursorShape.UNDERLINE:
Robert Ginda830583c2013-08-07 13:20:46 -07002950 style.backgroundColor = 'transparent';
2951 style.borderBottomStyle = 'solid';
Joel Hockeyd4fca732019-09-20 16:57:03 -07002952 style.borderLeftStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07002953 break;
2954
2955 default:
Mike Frysinger2fd079a2018-09-02 01:46:12 -04002956 style.backgroundColor = 'var(--hterm-cursor-color)';
Joel Hockeyd4fca732019-09-20 16:57:03 -07002957 style.borderBottomStyle = '';
2958 style.borderLeftStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07002959 break;
2960 }
2961};
2962
rginda8ba33642011-12-14 12:31:31 -08002963/**
2964 * Synchronizes the visible cursor with the current cursor coordinates.
2965 *
2966 * The sync will happen asynchronously, soon after the call stack winds down.
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002967 * Multiple calls will be coalesced into a single sync. This should be called
2968 * prior to the cursor actually changing position.
rginda8ba33642011-12-14 12:31:31 -08002969 */
2970hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2971 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002972 return;
rginda8ba33642011-12-14 12:31:31 -08002973
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002974 if (this.accessibilityReader_.accessibilityEnabled) {
2975 // Report the previous position of the cursor for accessibility purposes.
2976 const cursorRowIndex = this.scrollbackRows_.length +
2977 this.screen_.cursorPosition.row;
2978 const cursorColumnIndex = this.screen_.cursorPosition.column;
2979 const cursorLineText =
2980 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
2981 this.accessibilityReader_.beforeCursorChange(
2982 cursorLineText, cursorRowIndex, cursorColumnIndex);
2983 }
2984
rginda8ba33642011-12-14 12:31:31 -08002985 var self = this;
2986 this.timeouts_.syncCursor = setTimeout(function() {
2987 self.syncCursorPosition_();
2988 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002989 }, 0);
2990};
2991
rgindacc2996c2012-02-24 14:59:31 -08002992/**
rgindaf522ce02012-04-17 17:49:17 -07002993 * Show or hide the zoom warning.
2994 *
2995 * The zoom warning is a message warning the user that their browser zoom must
2996 * be set to 100% in order for hterm to function properly.
2997 *
2998 * @param {boolean} state True to show the message, false to hide it.
2999 */
3000hterm.Terminal.prototype.showZoomWarning_ = function(state) {
3001 if (!this.zoomWarningNode_) {
3002 if (!state)
3003 return;
3004
3005 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04003006 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07003007 this.zoomWarningNode_.style.cssText = (
3008 'color: black;' +
3009 'background-color: #ff2222;' +
3010 'font-size: large;' +
3011 'border-radius: 8px;' +
3012 'opacity: 0.75;' +
3013 'padding: 0.2em 0.5em 0.2em 0.5em;' +
3014 'top: 0.5em;' +
3015 'right: 1.2em;' +
3016 'position: absolute;' +
3017 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003018 '-webkit-user-select: none;' +
3019 '-moz-text-size-adjust: none;' +
3020 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05003021
3022 this.zoomWarningNode_.addEventListener('click', function(e) {
3023 this.parentNode.removeChild(this);
3024 });
rgindaf522ce02012-04-17 17:49:17 -07003025 }
3026
Mike Frysingerb7289952019-03-23 16:05:38 -07003027 this.zoomWarningNode_.textContent = lib.i18n.replaceReferences(
Robert Gindab4839c22013-02-28 16:52:10 -08003028 hterm.zoomWarningMessage,
Joel Hockeyd4fca732019-09-20 16:57:03 -07003029 [Math.floor(this.scrollPort_.characterSize.zoomFactor * 100)]);
Robert Gindab4839c22013-02-28 16:52:10 -08003030
rgindaf522ce02012-04-17 17:49:17 -07003031 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
3032
3033 if (state) {
3034 if (!this.zoomWarningNode_.parentNode)
3035 this.div_.parentNode.appendChild(this.zoomWarningNode_);
3036 } else if (this.zoomWarningNode_.parentNode) {
3037 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
3038 }
3039};
3040
3041/**
rgindacc2996c2012-02-24 14:59:31 -08003042 * Show the terminal overlay for a given amount of time.
3043 *
3044 * The terminal overlay appears in inverse video in a large font, centered
3045 * over the terminal. You should probably keep the overlay message brief,
3046 * since it's in a large font and you probably aren't going to check the size
3047 * of the terminal first.
3048 *
3049 * @param {string} msg The text (not HTML) message to display in the overlay.
Joel Hockey0f933582019-08-27 18:01:51 -07003050 * @param {number=} opt_timeout The amount of time to wait before fading out
rgindacc2996c2012-02-24 14:59:31 -08003051 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
3052 * stay up forever (or until the next overlay).
3053 */
3054hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08003055 if (!this.overlayNode_) {
3056 if (!this.div_)
3057 return;
3058
3059 this.overlayNode_ = this.document_.createElement('div');
3060 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08003061 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08003062 'font-size: xx-large;' +
3063 'opacity: 0.75;' +
3064 'padding: 0.2em 0.5em 0.2em 0.5em;' +
3065 'position: absolute;' +
3066 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003067 '-webkit-transition: opacity 180ms ease-in;' +
3068 '-moz-user-select: none;' +
3069 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08003070
3071 this.overlayNode_.addEventListener('mousedown', function(e) {
3072 e.preventDefault();
3073 e.stopPropagation();
3074 }, true);
rgindaf0090c92012-02-10 14:58:52 -08003075 }
3076
rginda9f5222b2012-03-05 11:53:28 -08003077 this.overlayNode_.style.color = this.prefs_.get('background-color');
3078 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
3079 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
3080
rgindaf0090c92012-02-10 14:58:52 -08003081 this.overlayNode_.textContent = msg;
3082 this.overlayNode_.style.opacity = '0.75';
3083
3084 if (!this.overlayNode_.parentNode)
3085 this.div_.appendChild(this.overlayNode_);
3086
Joel Hockeyd4fca732019-09-20 16:57:03 -07003087 var divSize = hterm.getClientSize(lib.notNull(this.div_));
Robert Ginda97769282013-02-01 15:30:30 -08003088 var overlaySize = hterm.getClientSize(this.overlayNode_);
3089
Robert Ginda8a59f762014-07-23 11:29:55 -07003090 this.overlayNode_.style.top =
3091 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08003092 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07003093 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08003094
rgindaf0090c92012-02-10 14:58:52 -08003095 if (this.overlayTimeout_)
3096 clearTimeout(this.overlayTimeout_);
3097
Raymes Khouryc7a06382018-07-04 10:25:45 +10003098 this.accessibilityReader_.assertiveAnnounce(msg);
3099
rgindacc2996c2012-02-24 14:59:31 -08003100 if (opt_timeout === null)
3101 return;
3102
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003103 this.overlayTimeout_ = setTimeout(() => {
3104 this.overlayNode_.style.opacity = '0';
3105 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
3106 }, opt_timeout || 1500);
3107};
3108
3109/**
3110 * Hide the terminal overlay immediately.
3111 *
3112 * Useful when we show an overlay for an event with an unknown end time.
3113 */
3114hterm.Terminal.prototype.hideOverlay = function() {
3115 if (this.overlayTimeout_)
3116 clearTimeout(this.overlayTimeout_);
3117 this.overlayTimeout_ = null;
3118
3119 if (this.overlayNode_.parentNode)
3120 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
3121 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08003122};
3123
rginda4bba5e12012-06-20 16:15:30 -07003124/**
3125 * Paste from the system clipboard to the terminal.
Mike Frysinger23b5b832019-10-01 17:05:29 -04003126 *
Joel Hockey0f933582019-08-27 18:01:51 -07003127 * @return {boolean}
rginda4bba5e12012-06-20 16:15:30 -07003128 */
3129hterm.Terminal.prototype.paste = function() {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003130 return hterm.pasteFromClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07003131};
3132
3133/**
3134 * Copy a string to the system clipboard.
3135 *
3136 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05003137 *
3138 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07003139 */
3140hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08003141 if (this.prefs_.get('enable-clipboard-notice'))
3142 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
3143
Mike Frysinger96eacae2019-01-02 18:13:56 -05003144 hterm.copySelectionToClipboard(this.document_, str);
rginda4bba5e12012-06-20 16:15:30 -07003145};
3146
Evan Jones2600d4f2016-12-06 09:29:36 -05003147/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003148 * Display an image.
3149 *
Mike Frysinger2558ed52019-01-14 01:03:41 -05003150 * Either URI or buffer or blob fields must be specified.
3151 *
Joel Hockey0f933582019-08-27 18:01:51 -07003152 * @param {{
3153 * name: (string|undefined),
3154 * size: (string|number|undefined),
3155 * preserveAspectRation: (boolean|undefined),
3156 * inline: (boolean|undefined),
3157 * width: (string|number|undefined),
3158 * height: (string|number|undefined),
3159 * align: (string|undefined),
3160 * url: (string|undefined),
3161 * buffer: (!ArrayBuffer|undefined),
3162 * blob: (!Blob|undefined),
3163 * type: (string|undefined),
3164 * }} options The image to display.
3165 * name A human readable string for the image
3166 * size The size (in bytes).
3167 * preserveAspectRatio Whether to preserve aspect.
3168 * inline Whether to display the image inline.
3169 * width The width of the image.
3170 * height The height of the image.
3171 * align Direction to align the image.
3172 * uri The source URI for the image.
3173 * buffer The ArrayBuffer image data.
3174 * blob The Blob image data.
3175 * type The MIME type of the image data.
3176 * @param {function()=} onLoad Callback when loading finishes.
3177 * @param {function(!Event)=} onError Callback when loading fails.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003178 */
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003179hterm.Terminal.prototype.displayImage = function(options, onLoad, onError) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003180 // Make sure we're actually given a resource to display.
Mike Frysinger2558ed52019-01-14 01:03:41 -05003181 if (options.uri === undefined && options.buffer === undefined &&
3182 options.blob === undefined)
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003183 return;
3184
3185 // Set up the defaults to simplify code below.
3186 if (!options.name)
3187 options.name = '';
3188
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003189 // See if the mime type is available. If not, guess from the filename.
3190 // We don't list all possible mime types because the browser can usually
3191 // guess it correctly. So list the ones that need a bit more help.
3192 if (!options.type) {
3193 const ary = options.name.split('.');
3194 const ext = ary[ary.length - 1].trim();
3195 switch (ext) {
3196 case 'svg':
3197 case 'svgz':
3198 options.type = 'image/svg+xml';
3199 break;
3200 }
3201 }
3202
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003203 // Has the user approved image display yet?
3204 if (this.allowImagesInline !== true) {
3205 this.newLine();
3206 const row = this.getRowNode(this.scrollbackRows_.length +
3207 this.getCursorRow() - 1);
3208
3209 if (this.allowImagesInline === false) {
3210 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
3211 'Inline Images Disabled');
3212 return;
3213 }
3214
3215 // Show a prompt.
3216 let button;
3217 const span = this.document_.createElement('span');
3218 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
3219 span.style.fontWeight = 'bold';
3220 span.style.borderWidth = '1px';
3221 span.style.borderStyle = 'dashed';
3222 button = this.document_.createElement('span');
3223 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3224 button.style.marginLeft = '1em';
3225 button.style.borderWidth = '1px';
3226 button.style.borderStyle = 'solid';
3227 button.addEventListener('click', () => {
3228 this.prefs_.set('allow-images-inline', false);
3229 });
3230 span.appendChild(button);
3231 button = this.document_.createElement('span');
3232 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3233 'allow this session');
3234 button.style.marginLeft = '1em';
3235 button.style.borderWidth = '1px';
3236 button.style.borderStyle = 'solid';
3237 button.addEventListener('click', () => {
3238 this.allowImagesInline = true;
3239 });
3240 span.appendChild(button);
3241 button = this.document_.createElement('span');
3242 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3243 button.style.marginLeft = '1em';
3244 button.style.borderWidth = '1px';
3245 button.style.borderStyle = 'solid';
3246 button.addEventListener('click', () => {
3247 this.prefs_.set('allow-images-inline', true);
3248 });
3249 span.appendChild(button);
3250
3251 row.appendChild(span);
3252 return;
3253 }
3254
3255 // See if we should show this object directly, or download it.
3256 if (options.inline) {
3257 const io = this.io.push();
3258 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
Joel Hockeyd4fca732019-09-20 16:57:03 -07003259 'Loading $1 ...'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003260
3261 // While we're loading the image, eat all the user's input.
3262 io.onVTKeystroke = io.sendString = () => {};
3263
3264 // Initialize this new image.
Joel Hockeyd4fca732019-09-20 16:57:03 -07003265 const img = this.document_.createElement('img');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003266 if (options.uri !== undefined) {
3267 img.src = options.uri;
3268 } else if (options.buffer !== undefined) {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003269 const blob = new Blob([options.buffer], {type: options.type});
Mike Frysinger2558ed52019-01-14 01:03:41 -05003270 img.src = URL.createObjectURL(blob);
3271 } else {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003272 const blob = new Blob([options.blob], {type: options.type});
Joel Hockeyd4fca732019-09-20 16:57:03 -07003273 img.src = URL.createObjectURL(blob);
Mike Frysinger2558ed52019-01-14 01:03:41 -05003274 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003275 img.title = img.alt = options.name;
3276
3277 // Attach the image to the page to let it load/render. It won't stay here.
3278 // This is needed so it's visible and the DOM can calculate the height. If
3279 // the image is hidden or not in the DOM, the height is always 0.
3280 this.document_.body.appendChild(img);
3281
3282 // Wait for the image to finish loading before we try moving it to the
3283 // right place in the terminal.
3284 img.onload = () => {
3285 // Now that we have the image dimensions, figure out how to show it.
3286 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
3287 img.style.maxWidth = `${this.document_.body.clientWidth}px`;
3288 img.style.maxHeight = `${this.document_.body.clientHeight}px`;
3289
3290 // Parse a width/height specification.
3291 const parseDim = (dim, maxDim, cssVar) => {
3292 if (!dim || dim == 'auto')
3293 return '';
3294
3295 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3296 if (ary) {
3297 if (ary[2] == '%')
Joel Hockeyd4fca732019-09-20 16:57:03 -07003298 return Math.floor(maxDim * ary[1] / 100) + 'px';
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003299 else if (ary[2] == 'px')
3300 return dim;
3301 else
3302 return `calc(${dim} * var(${cssVar}))`;
3303 }
3304
3305 return '';
3306 };
3307 img.style.width =
3308 parseDim(options.width, this.document_.body.clientWidth,
3309 '--hterm-charsize-width');
3310 img.style.height =
3311 parseDim(options.height, this.document_.body.clientHeight,
3312 '--hterm-charsize-height');
3313
3314 // Figure out how many rows the image occupies, then add that many.
Mike Frysingera0349392019-09-11 05:38:09 -04003315 // Note: This count will be inaccurate if the font size changes on us.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003316 const padRows = Math.ceil(img.clientHeight /
3317 this.scrollPort_.characterSize.height);
3318 for (let i = 0; i < padRows; ++i)
3319 this.newLine();
3320
3321 // Update the max height in case the user shrinks the character size.
3322 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3323
3324 // Move the image to the last row. This way when we scroll up, it doesn't
3325 // disappear when the first row gets clipped. It will disappear when we
3326 // scroll down and the last row is clipped ...
3327 this.document_.body.removeChild(img);
3328 // Create a wrapper node so we can do an absolute in a relative position.
3329 // This helps with rounding errors between JS & CSS counts.
3330 const div = this.document_.createElement('div');
3331 div.style.position = 'relative';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003332 div.style.textAlign = options.align || '';
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003333 img.style.position = 'absolute';
3334 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3335 div.appendChild(img);
3336 const row = this.getRowNode(this.scrollbackRows_.length +
3337 this.getCursorRow() - 1);
3338 row.appendChild(div);
3339
Mike Frysinger2558ed52019-01-14 01:03:41 -05003340 // Now that the image has been read, we can revoke the source.
3341 if (options.uri === undefined) {
3342 URL.revokeObjectURL(img.src);
3343 }
3344
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003345 io.hideOverlay();
3346 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003347
3348 if (onLoad)
3349 onLoad();
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003350 };
3351
3352 // If we got a malformed image, give up.
3353 img.onerror = (e) => {
3354 this.document_.body.removeChild(img);
3355 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
Mike Frysingere14a8c42018-03-10 00:17:30 -08003356 'Loading $1 failed'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003357 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003358
3359 if (onError)
3360 onError(e);
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003361 };
3362 } else {
3363 // We can't use chrome.downloads.download as that requires "downloads"
3364 // permissions, and that works only in extensions, not apps.
3365 const a = this.document_.createElement('a');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003366 if (options.uri !== undefined) {
3367 a.href = options.uri;
3368 } else if (options.buffer !== undefined) {
3369 const blob = new Blob([options.buffer]);
3370 a.href = URL.createObjectURL(blob);
3371 } else {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003372 a.href = URL.createObjectURL(lib.notNull(options.blob));
Mike Frysinger2558ed52019-01-14 01:03:41 -05003373 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003374 a.download = options.name;
3375 this.document_.body.appendChild(a);
3376 a.click();
3377 a.remove();
Mike Frysinger2558ed52019-01-14 01:03:41 -05003378 if (options.uri === undefined) {
3379 URL.revokeObjectURL(a.href);
3380 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003381 }
3382};
3383
3384/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003385 * Returns the selected text, or null if no text is selected.
3386 *
3387 * @return {string|null}
3388 */
rgindaa09e7332012-08-17 12:49:51 -07003389hterm.Terminal.prototype.getSelectionText = function() {
3390 var selection = this.scrollPort_.selection;
3391 selection.sync();
3392
3393 if (selection.isCollapsed)
3394 return null;
3395
rgindaa09e7332012-08-17 12:49:51 -07003396 // Start offset measures from the beginning of the line.
3397 var startOffset = selection.startOffset;
3398 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003399
Raymes Khoury334625a2018-06-25 10:29:40 +10003400 // If an x-row isn't selected, |node| will be null.
3401 if (!node)
3402 return null;
3403
Robert Gindafdbb3f22012-09-06 20:23:06 -07003404 if (node.nodeName != 'X-ROW') {
3405 // If the selection doesn't start on an x-row node, then it must be
3406 // somewhere inside the x-row. Add any characters from previous siblings
3407 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003408
3409 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3410 // If node is the text node in a styled span, move up to the span node.
3411 node = node.parentNode;
3412 }
3413
Robert Gindafdbb3f22012-09-06 20:23:06 -07003414 while (node.previousSibling) {
3415 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003416 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003417 }
rgindaa09e7332012-08-17 12:49:51 -07003418 }
3419
3420 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08003421 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
3422 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003423 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003424
Robert Gindafdbb3f22012-09-06 20:23:06 -07003425 if (node.nodeName != 'X-ROW') {
3426 // If the selection doesn't end on an x-row node, then it must be
3427 // somewhere inside the x-row. Add any characters from following siblings
3428 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003429
3430 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3431 // If node is the text node in a styled span, move up to the span node.
3432 node = node.parentNode;
3433 }
3434
Robert Gindafdbb3f22012-09-06 20:23:06 -07003435 while (node.nextSibling) {
3436 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003437 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003438 }
rgindaa09e7332012-08-17 12:49:51 -07003439 }
3440
3441 var rv = this.getRowsText(selection.startRow.rowIndex,
3442 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003443 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003444};
3445
rginda4bba5e12012-06-20 16:15:30 -07003446/**
3447 * Copy the current selection to the system clipboard, then clear it after a
3448 * short delay.
3449 */
3450hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07003451 var text = this.getSelectionText();
3452 if (text != null)
3453 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07003454};
3455
Joel Hockey0f933582019-08-27 18:01:51 -07003456/**
3457 * Show overlay with current terminal size.
3458 */
rgindaf0090c92012-02-10 14:58:52 -08003459hterm.Terminal.prototype.overlaySize = function() {
Theodore Duboisdd5f9a72019-09-06 23:28:42 -07003460 if (this.prefs_.get('enable-resize-status')) {
3461 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
3462 }
rgindaf0090c92012-02-10 14:58:52 -08003463};
3464
rginda87b86462011-12-14 13:48:03 -08003465/**
3466 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3467 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003468 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003469 */
3470hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08003471 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08003472 this.scrollPort_.scrollRowToBottom(this.getRowCount());
3473
Mike Frysinger225c99d2019-10-20 14:02:37 -06003474 this.pauseCursorBlink_();
3475
Mike Frysinger79669762018-12-30 20:51:10 -05003476 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08003477};
3478
3479/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003480 * Open the selected url.
3481 */
3482hterm.Terminal.prototype.openSelectedUrl_ = function() {
3483 var str = this.getSelectionText();
3484
3485 // If there is no selection, try and expand wherever they clicked.
3486 if (str == null) {
John Lincae9b732018-03-08 13:56:35 +08003487 this.screen_.expandSelectionForUrl(this.document_.getSelection());
Mike Frysinger70b94692017-01-26 18:57:50 -10003488 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003489
3490 // If clicking in empty space, return.
3491 if (str == null)
3492 return;
Mike Frysinger70b94692017-01-26 18:57:50 -10003493 }
3494
3495 // Make sure URL is valid before opening.
3496 if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0)
3497 return;
Mike Frysinger43472622017-06-26 18:11:07 -04003498
3499 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003500 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003501 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3502 // We have to whitelist a few protocols that lack authorities and thus
3503 // never use the //. Like mailto.
3504 switch (str.split(':', 1)[0]) {
3505 case 'mailto':
3506 break;
3507 default:
3508 str = 'http://' + str;
3509 break;
3510 }
3511 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003512
Mike Frysinger720fa832017-10-23 01:15:52 -04003513 hterm.openUrl(str);
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003514};
Mike Frysinger70b94692017-01-26 18:57:50 -10003515
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003516/**
3517 * Manage the automatic mouse hiding behavior while typing.
3518 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003519 * @param {?boolean=} v Whether to enable automatic hiding.
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003520 */
3521hterm.Terminal.prototype.setAutomaticMouseHiding = function(v=null) {
3522 // Since Chrome OS & macOS do this by default everywhere, we don't need to.
3523 // Linux & Windows seem to leave this to specific applications to manage.
3524 if (v === null)
3525 v = (hterm.os != 'cros' && hterm.os != 'mac');
3526
3527 this.mouseHideWhileTyping_ = !!v;
3528};
3529
3530/**
3531 * Handler for monitoring user keyboard activity.
3532 *
3533 * This isn't for processing the keystrokes directly, but for updating any
3534 * state that might toggle based on the user using the keyboard at all.
3535 *
Joel Hockey0f933582019-08-27 18:01:51 -07003536 * @param {!KeyboardEvent} e The keyboard event that triggered us.
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003537 */
3538hterm.Terminal.prototype.onKeyboardActivity_ = function(e) {
3539 // When the user starts typing, hide the mouse cursor.
3540 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_)
3541 this.setCssVar('mouse-cursor-style', 'none');
3542};
Mike Frysinger70b94692017-01-26 18:57:50 -10003543
3544/**
rgindad5613292012-06-19 15:40:37 -07003545 * Add the terminalRow and terminalColumn properties to mouse events and
3546 * then forward on to onMouse().
3547 *
3548 * The terminalRow and terminalColumn properties contain the (row, column)
3549 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003550 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003551 * @param {!MouseEvent} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003552 */
3553hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003554 if (e.processedByTerminalHandler_) {
3555 // We register our event handlers on the document, as well as the cursor
3556 // and the scroll blocker. Mouse events that occur on the cursor or
3557 // scroll blocker will also appear on the document, but we don't want to
3558 // process them twice.
3559 //
3560 // We can't just prevent bubbling because that has other side effects, so
3561 // we decorate the event object with this property instead.
3562 return;
3563 }
3564
Mike Frysinger468966c2018-08-28 13:48:51 -04003565 // Consume navigation events. Button 3 is usually "browser back" and
3566 // button 4 is "browser forward" which we don't want to happen.
3567 if (e.button > 2) {
3568 e.preventDefault();
3569 // We don't return so click events can be passed to the remote below.
3570 }
3571
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003572 var reportMouseEvents = (!this.defeatMouseReports_ &&
3573 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3574
rgindafaa74742012-08-21 13:34:03 -07003575 e.processedByTerminalHandler_ = true;
3576
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003577 // Handle auto hiding of mouse cursor while typing.
3578 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
3579 // Make sure the mouse cursor is visible.
3580 this.syncMouseStyle();
3581 // This debounce isn't perfect, but should work well enough for such a
3582 // simple implementation. If the user moved the mouse, we enabled this
3583 // debounce, and then moved the mouse just before the timeout, we wouldn't
3584 // debounce that later movement.
3585 this.mouseHideDelay_ = setTimeout(() => this.mouseHideDelay_ = null, 1000);
3586 }
3587
Robert Gindaeda48db2014-07-17 09:25:30 -07003588 // One based row/column stored on the mouse event.
Joel Hockeyd4fca732019-09-20 16:57:03 -07003589 e.terminalRow = Math.floor(
3590 (e.clientY - this.scrollPort_.visibleRowTopMargin) /
3591 this.scrollPort_.characterSize.height) + 1;
3592 e.terminalColumn = Math.floor(
3593 e.clientX / this.scrollPort_.characterSize.width) + 1;
Robert Gindaeda48db2014-07-17 09:25:30 -07003594
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003595 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
3596 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07003597 return;
3598 }
3599
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003600 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07003601 // If the cursor is visible and we're not sending mouse events to the
3602 // host app, then we want to hide the terminal cursor when the mouse
3603 // cursor is over top. This keeps the terminal cursor from interfering
3604 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003605 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3606 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3607 this.cursorNode_.style.display = 'none';
3608 } else if (this.cursorNode_.style.display == 'none') {
3609 this.cursorNode_.style.display = '';
3610 }
3611 }
rgindad5613292012-06-19 15:40:37 -07003612
Robert Ginda928cf632014-03-05 15:07:41 -08003613 if (e.type == 'mousedown') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003614 this.contextMenu.hide();
Mike Frysingercc114512017-09-11 21:39:17 -04003615
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003616 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003617 // If VT mouse reporting is disabled, or has been defeated with
3618 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003619 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003620 this.setSelectionEnabled(true);
3621 } else {
3622 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003623 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003624 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003625 this.setSelectionEnabled(false);
3626 e.preventDefault();
3627 }
3628 }
3629
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003630 if (!reportMouseEvents) {
John Lin2aad22e2018-03-16 13:58:11 +08003631 if (e.type == 'dblclick') {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003632 this.screen_.expandSelection(this.document_.getSelection());
John Lin2aad22e2018-03-16 13:58:11 +08003633 if (this.copyOnSelect)
Mike Frysinger406c76c2019-01-02 17:53:51 -05003634 this.copySelectionToClipboard();
rgindad5613292012-06-19 15:40:37 -07003635 }
3636
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003637 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003638 // Debounce this event with the dblclick event. If you try to doubleclick
3639 // a URL to open it, Chrome will fire click then dblclick, but we won't
3640 // have expanded the selection text at the first click event.
3641 clearTimeout(this.timeouts_.openUrl);
3642 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3643 500);
3644 return;
3645 }
3646
Mike Frysinger847577f2017-05-23 23:25:57 -04003647 if (e.type == 'mousedown') {
Mike Frysingercc114512017-09-11 21:39:17 -04003648 if (e.ctrlKey && e.button == 2 /* right button */) {
3649 e.preventDefault();
3650 this.contextMenu.show(e, this);
3651 } else if (e.button == this.mousePasteButton ||
3652 (this.mouseRightClickPaste && e.button == 2 /* right button */)) {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003653 if (!this.paste())
Mike Frysinger05a57f02017-08-27 17:48:55 -04003654 console.warn('Could not paste manually due to web restrictions');
Mike Frysinger847577f2017-05-23 23:25:57 -04003655 }
3656 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003657
Mike Frysinger2edd3612017-05-24 00:54:39 -04003658 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003659 !this.document_.getSelection().isCollapsed) {
Mike Frysinger406c76c2019-01-02 17:53:51 -05003660 this.copySelectionToClipboard();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003661 }
3662
3663 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3664 this.scrollBlockerNode_.engaged) {
3665 // Disengage the scroll-blocker after one of these events.
3666 this.scrollBlockerNode_.engaged = false;
3667 this.scrollBlockerNode_.style.top = '-99px';
3668 }
3669
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003670 // Emulate arrow key presses via scroll wheel events.
3671 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3672 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003673 if (e.type == 'wheel') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003674 const delta =
3675 this.scrollPort_.scrollWheelDelta(/** @type {!WheelEvent} */ (e));
Mike Frysingerc3030a82017-05-29 14:16:11 -04003676
Mike Frysinger321063c2018-08-29 15:33:14 -04003677 // Helper to turn a wheel event delta into a series of key presses.
3678 const deltaToArrows = (distance, charSize, arrowPos, arrowNeg) => {
3679 if (distance == 0) {
3680 return '';
3681 }
3682
3683 // Convert the scroll distance into a number of rows/cols.
3684 const cells = lib.f.smartFloorDivide(Math.abs(distance), charSize);
3685 const data = '\x1bO' + (distance < 0 ? arrowNeg : arrowPos);
3686 return data.repeat(cells);
3687 };
3688
3689 // The order between up/down and left/right doesn't really matter.
3690 this.io.sendString(
3691 // Up/down arrow keys.
3692 deltaToArrows(delta.y, this.scrollPort_.characterSize.height,
3693 'A', 'B') +
3694 // Left/right arrow keys.
3695 deltaToArrows(delta.x, this.scrollPort_.characterSize.width,
3696 'C', 'D')
3697 );
Mike Frysingerc3030a82017-05-29 14:16:11 -04003698
3699 e.preventDefault();
3700 }
3701 }
Robert Ginda928cf632014-03-05 15:07:41 -08003702 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003703 if (!this.scrollBlockerNode_.engaged) {
3704 if (e.type == 'mousedown') {
3705 // Move the scroll-blocker into place if we want to keep the scrollport
3706 // from scrolling.
3707 this.scrollBlockerNode_.engaged = true;
3708 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3709 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3710 } else if (e.type == 'mousemove') {
3711 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3712 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003713 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003714 e.preventDefault();
3715 }
3716 }
Robert Ginda928cf632014-03-05 15:07:41 -08003717
3718 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003719 }
3720
Robert Ginda928cf632014-03-05 15:07:41 -08003721 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3722 // Restore this on mouseup in case it was temporarily defeated with a
3723 // alt-mousedown. Only do this when the selection is empty so that
3724 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003725 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003726 }
rgindad5613292012-06-19 15:40:37 -07003727};
3728
3729/**
3730 * Clients should override this if they care to know about mouse events.
3731 *
3732 * The event parameter will be a normal DOM mouse click event with additional
3733 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003734 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003735 * @param {!MouseEvent} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003736 */
3737hterm.Terminal.prototype.onMouse = function(e) { };
3738
3739/**
rginda8e92a692012-05-20 19:37:20 -07003740 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003741 *
3742 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003743 */
Rob Spies06533ba2014-04-24 11:20:37 -07003744hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3745 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003746 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003747
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003748 if (this.reportFocus)
3749 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O');
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003750
Michael Kelly485ecd12014-06-09 11:41:56 -04003751 if (focused === true)
3752 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07003753};
3754
3755/**
rginda8ba33642011-12-14 12:31:31 -08003756 * React when the ScrollPort is scrolled.
3757 */
3758hterm.Terminal.prototype.onScroll_ = function() {
3759 this.scheduleSyncCursorPosition_();
3760};
3761
3762/**
rginda9846e2f2012-01-27 13:53:33 -08003763 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003764 *
Joel Hockeye25ce432019-09-25 19:12:28 -07003765 * @param {{text: string}} e The text of the paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08003766 */
3767hterm.Terminal.prototype.onPaste_ = function(e) {
Joel Hockeye25ce432019-09-25 19:12:28 -07003768 var data = e.text.replace(/\n/mg, '\r');
Mike Frysingere8c32c82018-03-11 14:57:28 -07003769 if (this.options_.bracketedPaste) {
3770 // We strip out most escape sequences as they can cause issues (like
3771 // inserting an \x1b[201~ midstream). We pass through whitespace
3772 // though: 0x08:\b 0x09:\t 0x0a:\n 0x0d:\r.
3773 // This matches xterm behavior.
3774 const filter = (data) => data.replace(/[\x00-\x07\x0b-\x0c\x0e-\x1f]/g, '');
3775 data = '\x1b[200~' + filter(data) + '\x1b[201~';
3776 }
Robert Gindaa063b202014-07-21 11:08:25 -07003777
3778 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003779};
3780
3781/**
rgindaa09e7332012-08-17 12:49:51 -07003782 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003783 *
Joel Hockey0f933582019-08-27 18:01:51 -07003784 * @param {!Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07003785 */
3786hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07003787 if (!this.useDefaultWindowCopy) {
3788 e.preventDefault();
3789 setTimeout(this.copySelectionToClipboard.bind(this), 0);
3790 }
rgindaa09e7332012-08-17 12:49:51 -07003791};
3792
3793/**
rginda8ba33642011-12-14 12:31:31 -08003794 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08003795 *
3796 * Note: This function should not directly contain code that alters the internal
3797 * state of the terminal. That kind of code belongs in realizeWidth or
3798 * realizeHeight, so that it can be executed synchronously in the case of a
3799 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08003800 */
3801hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08003802 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07003803 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08003804 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07003805 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08003806
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003807 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08003808 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003809 // gets removed from the document or during the initial load, and we can't
3810 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07003811 // This can also happen if called before the scrollPort calculates the
3812 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08003813 return;
3814 }
3815
rgindaa8ba17d2012-08-15 14:41:10 -07003816 var isNewSize = (columnCount != this.screenSize.width ||
3817 rowCount != this.screenSize.height);
Theodore Dubois651b0842019-09-07 14:32:09 -07003818 const wasScrolledEnd = this.scrollPort_.isScrolledEnd;
rgindaa8ba17d2012-08-15 14:41:10 -07003819
3820 // We do this even if the size didn't change, just to be sure everything is
3821 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04003822 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07003823 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07003824
3825 if (isNewSize)
3826 this.overlaySize();
3827
Robert Gindafb1be6a2013-12-11 11:56:22 -08003828 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07003829 this.scheduleSyncCursorPosition_();
Theodore Dubois651b0842019-09-07 14:32:09 -07003830
3831 if (wasScrolledEnd) {
3832 this.scrollEnd();
3833 }
rginda8ba33642011-12-14 12:31:31 -08003834};
3835
3836/**
3837 * Service the cursor blink timeout.
3838 */
3839hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07003840 if (!this.options_.cursorBlink) {
3841 delete this.timeouts_.cursorBlink;
3842 return;
3843 }
3844
Robert Ginda830583c2013-08-07 13:20:46 -07003845 if (this.cursorNode_.getAttribute('focus') == 'false' ||
Mike Frysinger225c99d2019-10-20 14:02:37 -06003846 this.cursorNode_.style.opacity == '0' ||
3847 this.cursorBlinkPause_) {
rginda87b86462011-12-14 13:48:03 -08003848 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07003849 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3850 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08003851 } else {
rginda87b86462011-12-14 13:48:03 -08003852 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07003853 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3854 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08003855 }
3856};
David Reveman8f552492012-03-28 12:18:41 -04003857
3858/**
3859 * Set the scrollbar-visible mode bit.
3860 *
3861 * If scrollbar-visible is on, the vertical scrollbar will be visible.
3862 * Otherwise it will not.
3863 *
3864 * Defaults to on.
3865 *
3866 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
3867 */
3868hterm.Terminal.prototype.setScrollbarVisible = function(state) {
3869 this.scrollPort_.setScrollbarVisible(state);
3870};
Michael Kelly485ecd12014-06-09 11:41:56 -04003871
3872/**
Rob Spies49039e52014-12-17 13:40:04 -08003873 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04003874 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08003875 *
3876 * Defaults to 1.
3877 *
Evan Jones2600d4f2016-12-06 09:29:36 -05003878 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08003879 */
3880hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3881 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3882};
3883
3884/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003885 * Close all web notifications created by terminal bells.
3886 */
3887hterm.Terminal.prototype.closeBellNotifications_ = function() {
3888 this.bellNotificationList_.forEach(function(n) {
3889 n.close();
3890 });
3891 this.bellNotificationList_.length = 0;
3892};
Raymes Khourye5d48982018-08-02 09:08:32 +10003893
3894/**
3895 * Syncs the cursor position when the scrollport gains focus.
3896 */
3897hterm.Terminal.prototype.onScrollportFocus_ = function() {
3898 // If the cursor is offscreen we set selection to the last row on the screen.
3899 const topRowIndex = this.scrollPort_.getTopRowIndex();
3900 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
3901 const selection = this.document_.getSelection();
3902 if (!this.syncCursorPosition_() && selection) {
3903 selection.collapse(this.getRowNode(bottomRowIndex));
3904 }
3905};