blob: d20c29e7faabff78ef66111c14a6d5a8703ceef6 [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
Joel Hockey0e052042020-02-19 05:37:19 -0800519 'pass-ctrl-n': function(v) {
520 terminal.passCtrlN = v;
521 },
522
523 'pass-ctrl-t': function(v) {
524 terminal.passCtrlT = v;
525 },
526
527 'pass-ctrl-tab': function(v) {
528 terminal.passCtrlTab = v;
529 },
530
531 'pass-ctrl-w': function(v) {
532 terminal.passCtrlW = v;
533 },
534
Robert Ginda40932892012-12-10 17:26:40 -0800535 'pass-meta-number': function(v) {
536 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800537 // Let Meta-1..9 pass to the browser (to control tab switching) on
538 // OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500539 v = (hterm.os == 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800540 }
541
542 terminal.passMetaNumber = v;
543 },
544
Marius Schilder77857b32014-05-14 16:21:26 -0700545 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700546 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700547 },
548
Robert Ginda8cb7d902013-06-20 14:37:18 -0700549 'receive-encoding': function(v) {
550 if (!(/^(utf-8|raw)$/).test(v)) {
551 console.warn('Invalid value for "receive-encoding": ' + v);
552 v = 'utf-8';
553 }
554
555 terminal.vt.characterEncoding = v;
556 },
557
Robert Ginda57f03b42012-09-13 11:02:48 -0700558 'scroll-on-keystroke': function(v) {
559 terminal.scrollOnKeystroke_ = v;
560 },
rginda9f5222b2012-03-05 11:53:28 -0800561
Robert Ginda57f03b42012-09-13 11:02:48 -0700562 'scroll-on-output': function(v) {
563 terminal.scrollOnOutput_ = v;
564 },
rginda30f20f62012-04-05 16:36:19 -0700565
Robert Ginda57f03b42012-09-13 11:02:48 -0700566 'scrollbar-visible': function(v) {
567 terminal.setScrollbarVisible(v);
568 },
rginda9f5222b2012-03-05 11:53:28 -0800569
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400570 'scroll-wheel-may-send-arrow-keys': function(v) {
571 terminal.scrollWheelArrowKeys_ = v;
572 },
573
Rob Spies49039e52014-12-17 13:40:04 -0800574 'scroll-wheel-move-multiplier': function(v) {
575 terminal.setScrollWheelMoveMultipler(v);
576 },
577
Robert Ginda57f03b42012-09-13 11:02:48 -0700578 'shift-insert-paste': function(v) {
579 terminal.keyboard.shiftInsertPaste = v;
580 },
rginda9f5222b2012-03-05 11:53:28 -0800581
Mike Frysingera7768922017-07-28 15:00:12 -0400582 'terminal-encoding': function(v) {
Mike Frysingera1371e12017-08-17 01:37:17 -0400583 terminal.vt.setEncoding(v);
Mike Frysingera7768922017-07-28 15:00:12 -0400584 },
585
Robert Gindae76aa9f2014-03-14 12:29:12 -0700586 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400587 terminal.scrollPort_.setUserCssUrl(v);
588 },
589
590 'user-css-text': function(v) {
591 terminal.scrollPort_.setUserCssText(v);
592 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400593
594 'word-break-match-left': function(v) {
595 terminal.primaryScreen_.wordBreakMatchLeft = v;
596 terminal.alternateScreen_.wordBreakMatchLeft = v;
597 },
598
599 'word-break-match-right': function(v) {
600 terminal.primaryScreen_.wordBreakMatchRight = v;
601 terminal.alternateScreen_.wordBreakMatchRight = v;
602 },
603
604 'word-break-match-middle': function(v) {
605 terminal.primaryScreen_.wordBreakMatchMiddle = v;
606 terminal.alternateScreen_.wordBreakMatchMiddle = v;
607 },
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400608
609 'allow-images-inline': function(v) {
610 terminal.allowImagesInline = v;
611 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700612 });
rginda30f20f62012-04-05 16:36:19 -0700613
Robert Ginda57f03b42012-09-13 11:02:48 -0700614 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800615 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700616
617 if (opt_callback)
618 opt_callback();
619 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800620};
621
Rob Spies56953412014-04-28 14:09:47 -0700622/**
623 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500624 *
Joel Hockey0f933582019-08-27 18:01:51 -0700625 * @return {!hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700626 */
627hterm.Terminal.prototype.getPrefs = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700628 return lib.notNull(this.prefs_);
Rob Spies56953412014-04-28 14:09:47 -0700629};
630
Robert Gindaa063b202014-07-21 11:08:25 -0700631/**
632 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500633 *
634 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700635 */
636hterm.Terminal.prototype.setBracketedPaste = function(state) {
637 this.options_.bracketedPaste = state;
638};
Rob Spies56953412014-04-28 14:09:47 -0700639
rginda8e92a692012-05-20 19:37:20 -0700640/**
641 * Set the color for the cursor.
642 *
643 * If you want this setting to persist, set it through prefs_, rather than
644 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500645 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500646 * @param {string=} color The color to set. If not defined, we reset to the
647 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700648 */
649hterm.Terminal.prototype.setCursorColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500650 if (color === undefined)
Joel Hockeyd4fca732019-09-20 16:57:03 -0700651 color = this.prefs_.getString('cursor-color');
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500652
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400653 this.setCssVar('cursor-color', color);
rginda8e92a692012-05-20 19:37:20 -0700654};
655
656/**
657 * Return the current cursor color as a string.
Mike Frysinger23b5b832019-10-01 17:05:29 -0400658 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500659 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700660 */
661hterm.Terminal.prototype.getCursorColor = function() {
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400662 return this.getCssVar('cursor-color');
rginda8e92a692012-05-20 19:37:20 -0700663};
664
665/**
rgindad5613292012-06-19 15:40:37 -0700666 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500667 *
668 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700669 */
670hterm.Terminal.prototype.setSelectionEnabled = function(state) {
671 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700672};
673
674/**
rginda8e92a692012-05-20 19:37:20 -0700675 * Set the background color.
676 *
677 * If you want this setting to persist, set it through prefs_, rather than
678 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500679 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500680 * @param {string=} color The color to set. If not defined, we reset to the
681 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700682 */
683hterm.Terminal.prototype.setBackgroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500684 if (color === undefined)
Joel Hockeyd4fca732019-09-20 16:57:03 -0700685 color = this.prefs_.getString('background-color');
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500686
Joel Hockey8ff48232019-09-24 13:15:17 -0700687 this.backgroundColor_ = lib.colors.normalizeCSS(color) || '';
Robert Ginda57f03b42012-09-13 11:02:48 -0700688 this.primaryScreen_.textAttributes.setDefaults(
689 this.foregroundColor_, this.backgroundColor_);
690 this.alternateScreen_.textAttributes.setDefaults(
691 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700692 this.scrollPort_.setBackgroundColor(color);
693};
694
rginda9f5222b2012-03-05 11:53:28 -0800695/**
696 * Return the current terminal background color.
697 *
698 * Intended for use by other classes, so we don't have to expose the entire
699 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500700 *
701 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800702 */
703hterm.Terminal.prototype.getBackgroundColor = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700704 return lib.notNull(this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700705};
706
707/**
708 * Set the foreground color.
709 *
710 * If you want this setting to persist, set it through prefs_, rather than
711 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500712 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500713 * @param {string=} color The color to set. If not defined, we reset to the
714 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700715 */
716hterm.Terminal.prototype.setForegroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500717 if (color === undefined)
Joel Hockeyd4fca732019-09-20 16:57:03 -0700718 color = this.prefs_.getString('foreground-color');
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500719
Joel Hockey8ff48232019-09-24 13:15:17 -0700720 this.foregroundColor_ = lib.colors.normalizeCSS(color) || '';
Robert Ginda57f03b42012-09-13 11:02:48 -0700721 this.primaryScreen_.textAttributes.setDefaults(
722 this.foregroundColor_, this.backgroundColor_);
723 this.alternateScreen_.textAttributes.setDefaults(
724 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700725 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800726};
727
728/**
729 * Return the current terminal foreground color.
730 *
731 * Intended for use by other classes, so we don't have to expose the entire
732 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500733 *
734 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800735 */
736hterm.Terminal.prototype.getForegroundColor = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700737 return lib.notNull(this.foregroundColor_);
rginda9f5222b2012-03-05 11:53:28 -0800738};
739
740/**
rginda87b86462011-12-14 13:48:03 -0800741 * Create a new instance of a terminal command and run it with a given
742 * argument string.
743 *
Joel Hockeyd4fca732019-09-20 16:57:03 -0700744 * @param {!Function} commandClass The constructor for a terminal command.
Joel Hockey8081ea62019-08-26 16:52:32 -0700745 * @param {string} commandName The command to run for this terminal.
746 * @param {!Array<string>} args The arguments to pass to the command.
rginda87b86462011-12-14 13:48:03 -0800747 */
Joel Hockey8081ea62019-08-26 16:52:32 -0700748hterm.Terminal.prototype.runCommandClass = function(
749 commandClass, commandName, args) {
rgindaf522ce02012-04-17 17:49:17 -0700750 var environment = this.prefs_.get('environment');
751 if (typeof environment != 'object' || environment == null)
752 environment = {};
753
rginda87b86462011-12-14 13:48:03 -0800754 var self = this;
755 this.command = new commandClass(
Joel Hockey8081ea62019-08-26 16:52:32 -0700756 {
757 commandName: commandName,
758 args: args,
rginda87b86462011-12-14 13:48:03 -0800759 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700760 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800761 onExit: function(code) {
762 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800763 self.uninstallKeyboard();
Julian Watsondfbf8592019-11-05 18:05:12 +1100764 self.div_.dispatchEvent(new CustomEvent('terminal-closing'));
rginda9875d902012-08-20 16:21:57 -0700765 if (self.prefs_.get('close-on-exit'))
766 window.close();
rginda87b86462011-12-14 13:48:03 -0800767 }
768 });
769
rgindafeaf3142012-01-31 15:14:20 -0800770 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800771 this.command.run();
772};
773
774/**
rgindafeaf3142012-01-31 15:14:20 -0800775 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500776 *
777 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800778 */
779hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700780 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800781};
782
783/**
784 * Install the keyboard handler for this terminal.
785 *
786 * This will prevent the browser from seeing any keystrokes sent to the
787 * terminal.
788 */
789hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700790 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400791};
rgindafeaf3142012-01-31 15:14:20 -0800792
793/**
794 * Uninstall the keyboard handler for this terminal.
795 */
796hterm.Terminal.prototype.uninstallKeyboard = function() {
797 this.keyboard.installKeyboard(null);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400798};
rgindafeaf3142012-01-31 15:14:20 -0800799
800/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400801 * Set a CSS variable.
802 *
803 * Normally this is used to set variables in the hterm namespace.
804 *
805 * @param {string} name The variable to set.
Joel Hockeyd4fca732019-09-20 16:57:03 -0700806 * @param {string|number} value The value to assign to the variable.
Joel Hockey0f933582019-08-27 18:01:51 -0700807 * @param {string=} opt_prefix The variable namespace/prefix to use.
Mike Frysingercce97c42017-08-05 01:11:22 -0400808 */
809hterm.Terminal.prototype.setCssVar = function(name, value,
810 opt_prefix='--hterm-') {
811 this.document_.documentElement.style.setProperty(
Joel Hockeyd4fca732019-09-20 16:57:03 -0700812 `${opt_prefix}${name}`, value.toString());
Mike Frysingercce97c42017-08-05 01:11:22 -0400813};
814
815/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500816 * Get a CSS variable.
817 *
818 * Normally this is used to get variables in the hterm namespace.
819 *
820 * @param {string} name The variable to read.
Joel Hockey0f933582019-08-27 18:01:51 -0700821 * @param {string=} opt_prefix The variable namespace/prefix to use.
Mike Frysinger261597c2017-12-28 01:14:21 -0500822 * @return {string} The current setting for this variable.
823 */
824hterm.Terminal.prototype.getCssVar = function(name, opt_prefix='--hterm-') {
825 return this.document_.documentElement.style.getPropertyValue(
826 `${opt_prefix}${name}`);
827};
828
829/**
rginda35c456b2012-02-09 17:29:05 -0800830 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800831 *
832 * Call setFontSize(0) to reset to the default font size.
833 *
834 * This function does not modify the font-size preference.
835 *
836 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800837 */
838hterm.Terminal.prototype.setFontSize = function(px) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500839 if (px <= 0)
Joel Hockeyd4fca732019-09-20 16:57:03 -0700840 px = this.prefs_.getNumber('font-size');
rginda9f5222b2012-03-05 11:53:28 -0800841
rginda35c456b2012-02-09 17:29:05 -0800842 this.scrollPort_.setFontSize(px);
Mike Frysingercce97c42017-08-05 01:11:22 -0400843 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
844 this.setCssVar('charsize-height',
845 this.scrollPort_.characterSize.height + 'px');
rginda35c456b2012-02-09 17:29:05 -0800846};
847
848/**
849 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500850 *
851 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800852 */
853hterm.Terminal.prototype.getFontSize = function() {
854 return this.scrollPort_.getFontSize();
855};
856
857/**
rginda8e92a692012-05-20 19:37:20 -0700858 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500859 *
860 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700861 */
862hterm.Terminal.prototype.getFontFamily = function() {
863 return this.scrollPort_.getFontFamily();
864};
865
866/**
rginda35c456b2012-02-09 17:29:05 -0800867 * Set the CSS "font-family" for this terminal.
868 */
rginda9f5222b2012-03-05 11:53:28 -0800869hterm.Terminal.prototype.syncFontFamily = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700870 this.scrollPort_.setFontFamily(this.prefs_.getString('font-family'),
871 this.prefs_.getString('font-smoothing'));
rginda9f5222b2012-03-05 11:53:28 -0800872 this.syncBoldSafeState();
873};
874
rginda4bba5e12012-06-20 16:15:30 -0700875/**
876 * Set this.mousePasteButton based on the mouse-paste-button pref,
877 * autodetecting if necessary.
878 */
879hterm.Terminal.prototype.syncMousePasteButton = function() {
880 var button = this.prefs_.get('mouse-paste-button');
881 if (typeof button == 'number') {
882 this.mousePasteButton = button;
883 return;
884 }
885
Mike Frysingeree81a002017-12-12 16:14:53 -0500886 if (hterm.os != 'linux') {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400887 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700888 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400889 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700890 }
891};
892
893/**
894 * Enable or disable bold based on the enable-bold pref, autodetecting if
895 * necessary.
896 */
rginda9f5222b2012-03-05 11:53:28 -0800897hterm.Terminal.prototype.syncBoldSafeState = function() {
898 var enableBold = this.prefs_.get('enable-bold');
899 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700900 this.primaryScreen_.textAttributes.enableBold = enableBold;
901 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800902 return;
903 }
904
rgindaf7521392012-02-28 17:20:34 -0800905 var normalSize = this.scrollPort_.measureCharacterSize();
906 var boldSize = this.scrollPort_.measureCharacterSize('bold');
907
908 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800909 if (!isBoldSafe) {
910 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700911 'from normal. Font family is: ' +
912 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800913 }
rginda9f5222b2012-03-05 11:53:28 -0800914
Robert Gindaed016262012-10-26 16:27:09 -0700915 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
916 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800917};
918
919/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500920 * Control text blinking behavior.
921 *
922 * @param {boolean=} state Whether to enable support for blinking text.
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400923 */
Mike Frysinger261597c2017-12-28 01:14:21 -0500924hterm.Terminal.prototype.setTextBlink = function(state) {
925 if (state === undefined)
Joel Hockeyd4fca732019-09-20 16:57:03 -0700926 state = this.prefs_.getBoolean('enable-blink');
Mike Frysinger261597c2017-12-28 01:14:21 -0500927 this.setCssVar('blink-node-duration', state ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400928};
929
930/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400931 * Set the mouse cursor style based on the current terminal mode.
932 */
933hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -0400934 this.setCssVar('mouse-cursor-style',
935 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
936 'var(--hterm-mouse-cursor-text)' :
Mike Frysinger67f58f82018-11-22 13:38:22 -0500937 'var(--hterm-mouse-cursor-default)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400938};
939
940/**
rginda87b86462011-12-14 13:48:03 -0800941 * Return a copy of the current cursor position.
942 *
Joel Hockey0f933582019-08-27 18:01:51 -0700943 * @return {!hterm.RowCol} The RowCol object representing the current position.
rginda87b86462011-12-14 13:48:03 -0800944 */
945hterm.Terminal.prototype.saveCursor = function() {
946 return this.screen_.cursorPosition.clone();
947};
948
Evan Jones2600d4f2016-12-06 09:29:36 -0500949/**
950 * Return the current text attributes.
951 *
Joel Hockeyd4fca732019-09-20 16:57:03 -0700952 * @return {!hterm.TextAttributes}
Evan Jones2600d4f2016-12-06 09:29:36 -0500953 */
rgindaa19afe22012-01-25 15:40:22 -0800954hterm.Terminal.prototype.getTextAttributes = function() {
955 return this.screen_.textAttributes;
956};
957
Evan Jones2600d4f2016-12-06 09:29:36 -0500958/**
959 * Set the text attributes.
960 *
Joel Hockeyd4fca732019-09-20 16:57:03 -0700961 * @param {!hterm.TextAttributes} textAttributes The attributes to set.
Evan Jones2600d4f2016-12-06 09:29:36 -0500962 */
rginda1a09aa02012-06-18 21:11:25 -0700963hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
964 this.screen_.textAttributes = textAttributes;
965};
966
rginda87b86462011-12-14 13:48:03 -0800967/**
rgindaf522ce02012-04-17 17:49:17 -0700968 * Return the current browser zoom factor applied to the terminal.
969 *
970 * @return {number} The current browser zoom factor.
971 */
972hterm.Terminal.prototype.getZoomFactor = function() {
973 return this.scrollPort_.characterSize.zoomFactor;
974};
975
976/**
rginda9846e2f2012-01-27 13:53:33 -0800977 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -0500978 *
979 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -0800980 */
981hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800982 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800983};
984
985/**
rginda87b86462011-12-14 13:48:03 -0800986 * Restore a previously saved cursor position.
987 *
Joel Hockey0f933582019-08-27 18:01:51 -0700988 * @param {!hterm.RowCol} cursor The position to restore.
rginda87b86462011-12-14 13:48:03 -0800989 */
990hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700991 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
992 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800993 this.screen_.setCursorPosition(row, column);
994 if (cursor.column > column ||
995 cursor.column == column && cursor.overflow) {
996 this.screen_.cursorPosition.overflow = true;
997 }
rginda87b86462011-12-14 13:48:03 -0800998};
999
1000/**
David Benjamin54e8bf62012-06-01 22:31:40 -04001001 * Clear the cursor's overflow flag.
1002 */
1003hterm.Terminal.prototype.clearCursorOverflow = function() {
1004 this.screen_.cursorPosition.overflow = false;
1005};
1006
1007/**
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001008 * Save the current cursor state to the corresponding screens.
1009 *
1010 * See the hterm.Screen.CursorState class for more details.
1011 *
1012 * @param {boolean=} both If true, update both screens, else only update the
1013 * current screen.
1014 */
1015hterm.Terminal.prototype.saveCursorAndState = function(both) {
1016 if (both) {
1017 this.primaryScreen_.saveCursorAndState(this.vt);
1018 this.alternateScreen_.saveCursorAndState(this.vt);
1019 } else
1020 this.screen_.saveCursorAndState(this.vt);
1021};
1022
1023/**
1024 * Restore the saved cursor state in the corresponding screens.
1025 *
1026 * See the hterm.Screen.CursorState class for more details.
1027 *
1028 * @param {boolean=} both If true, update both screens, else only update the
1029 * current screen.
1030 */
1031hterm.Terminal.prototype.restoreCursorAndState = function(both) {
1032 if (both) {
1033 this.primaryScreen_.restoreCursorAndState(this.vt);
1034 this.alternateScreen_.restoreCursorAndState(this.vt);
1035 } else
1036 this.screen_.restoreCursorAndState(this.vt);
1037};
1038
1039/**
Robert Ginda830583c2013-08-07 13:20:46 -07001040 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001041 *
1042 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -07001043 */
1044hterm.Terminal.prototype.setCursorShape = function(shape) {
1045 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001046 this.restyleCursor_();
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001047};
Robert Ginda830583c2013-08-07 13:20:46 -07001048
1049/**
1050 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001051 *
1052 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -07001053 */
1054hterm.Terminal.prototype.getCursorShape = function() {
1055 return this.cursorShape_;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001056};
Robert Ginda830583c2013-08-07 13:20:46 -07001057
1058/**
rginda87b86462011-12-14 13:48:03 -08001059 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001060 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001061 * @param {?number} columnCount
rginda87b86462011-12-14 13:48:03 -08001062 */
1063hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -08001064 if (columnCount == null) {
1065 this.div_.style.width = '100%';
1066 return;
1067 }
1068
Robert Ginda26806d12014-07-24 13:44:07 -07001069 this.div_.style.width = Math.ceil(
1070 this.scrollPort_.characterSize.width *
1071 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001072 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -08001073 this.scheduleSyncCursorPosition_();
1074};
rginda87b86462011-12-14 13:48:03 -08001075
rgindac9bc5502012-01-18 11:48:44 -08001076/**
rginda35c456b2012-02-09 17:29:05 -08001077 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001078 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001079 * @param {?number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -08001080 */
1081hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -08001082 if (rowCount == null) {
1083 this.div_.style.height = '100%';
1084 return;
1085 }
1086
rginda35c456b2012-02-09 17:29:05 -08001087 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -07001088 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -08001089 this.realizeSize_(this.screenSize.width, rowCount);
1090 this.scheduleSyncCursorPosition_();
1091};
1092
1093/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001094 * Deal with terminal size changes.
1095 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001096 * @param {number} columnCount The number of columns.
1097 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001098 */
1099hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
Mike Frysinger0206e262019-06-13 10:18:19 -04001100 let notify = false;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001101
Mike Frysinger0206e262019-06-13 10:18:19 -04001102 if (columnCount != this.screenSize.width) {
1103 notify = true;
1104 this.realizeWidth_(columnCount);
1105 }
1106
1107 if (rowCount != this.screenSize.height) {
1108 notify = true;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001109 this.realizeHeight_(rowCount);
Mike Frysinger0206e262019-06-13 10:18:19 -04001110 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001111
1112 // Send new terminal size to plugin.
Mike Frysinger0206e262019-06-13 10:18:19 -04001113 if (notify) {
1114 this.io.onTerminalResize_(columnCount, rowCount);
1115 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001116};
1117
1118/**
rgindac9bc5502012-01-18 11:48:44 -08001119 * Deal with terminal width changes.
1120 *
1121 * This function does what needs to be done when the terminal width changes
1122 * out from under us. It happens here rather than in onResize_() because this
1123 * code may need to run synchronously to handle programmatic changes of
1124 * terminal width.
1125 *
1126 * Relying on the browser to send us an async resize event means we may not be
1127 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001128 *
1129 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001130 */
1131hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001132 if (columnCount <= 0)
1133 throw new Error('Attempt to realize bad width: ' + columnCount);
1134
rgindac9bc5502012-01-18 11:48:44 -08001135 var deltaColumns = columnCount - this.screen_.getWidth();
Mike Frysinger0206e262019-06-13 10:18:19 -04001136 if (deltaColumns == 0) {
1137 // No change, so don't bother recalculating things.
1138 return;
1139 }
rgindac9bc5502012-01-18 11:48:44 -08001140
rginda87b86462011-12-14 13:48:03 -08001141 this.screenSize.width = columnCount;
1142 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001143
1144 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -04001145 if (this.defaultTabStops)
1146 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -08001147 } else {
1148 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -04001149 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -08001150 break;
1151
1152 this.tabStops_.pop();
1153 }
1154 }
1155
1156 this.screen_.setColumnCount(this.screenSize.width);
1157};
1158
1159/**
1160 * Deal with terminal height changes.
1161 *
1162 * This function does what needs to be done when the terminal height changes
1163 * out from under us. It happens here rather than in onResize_() because this
1164 * code may need to run synchronously to handle programmatic changes of
1165 * terminal height.
1166 *
1167 * Relying on the browser to send us an async resize event means we may not be
1168 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001169 *
1170 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001171 */
1172hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001173 if (rowCount <= 0)
1174 throw new Error('Attempt to realize bad height: ' + rowCount);
1175
rgindac9bc5502012-01-18 11:48:44 -08001176 var deltaRows = rowCount - this.screen_.getHeight();
Mike Frysinger0206e262019-06-13 10:18:19 -04001177 if (deltaRows == 0) {
1178 // No change, so don't bother recalculating things.
1179 return;
1180 }
rgindac9bc5502012-01-18 11:48:44 -08001181
1182 this.screenSize.height = rowCount;
1183
1184 var cursor = this.saveCursor();
1185
1186 if (deltaRows < 0) {
1187 // Screen got smaller.
1188 deltaRows *= -1;
1189 while (deltaRows) {
1190 var lastRow = this.getRowCount() - 1;
1191 if (lastRow - this.scrollbackRows_.length == cursor.row)
1192 break;
1193
1194 if (this.getRowText(lastRow))
1195 break;
1196
1197 this.screen_.popRow();
1198 deltaRows--;
1199 }
1200
1201 var ary = this.screen_.shiftRows(deltaRows);
1202 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1203
1204 // We just removed rows from the top of the screen, we need to update
1205 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001206 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001207 } else if (deltaRows > 0) {
1208 // Screen got larger.
1209
1210 if (deltaRows <= this.scrollbackRows_.length) {
1211 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1212 var rows = this.scrollbackRows_.splice(
1213 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1214 this.screen_.unshiftRows(rows);
1215 deltaRows -= scrollbackCount;
1216 cursor.row += scrollbackCount;
1217 }
1218
1219 if (deltaRows)
1220 this.appendRows_(deltaRows);
1221 }
1222
rginda35c456b2012-02-09 17:29:05 -08001223 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001224 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001225};
1226
1227/**
1228 * Scroll the terminal to the top of the scrollback buffer.
1229 */
1230hterm.Terminal.prototype.scrollHome = function() {
1231 this.scrollPort_.scrollRowToTop(0);
1232};
1233
1234/**
1235 * Scroll the terminal to the end.
1236 */
1237hterm.Terminal.prototype.scrollEnd = function() {
1238 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1239};
1240
1241/**
1242 * Scroll the terminal one page up (minus one line) relative to the current
1243 * position.
1244 */
1245hterm.Terminal.prototype.scrollPageUp = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001246 this.scrollPort_.scrollPageUp();
rginda87b86462011-12-14 13:48:03 -08001247};
1248
1249/**
1250 * Scroll the terminal one page down (minus one line) relative to the current
1251 * position.
1252 */
1253hterm.Terminal.prototype.scrollPageDown = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001254 this.scrollPort_.scrollPageDown();
rginda8ba33642011-12-14 12:31:31 -08001255};
1256
rgindac9bc5502012-01-18 11:48:44 -08001257/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001258 * Scroll the terminal one line up relative to the current position.
1259 */
1260hterm.Terminal.prototype.scrollLineUp = function() {
1261 var i = this.scrollPort_.getTopRowIndex();
1262 this.scrollPort_.scrollRowToTop(i - 1);
1263};
1264
1265/**
1266 * Scroll the terminal one line down relative to the current position.
1267 */
1268hterm.Terminal.prototype.scrollLineDown = function() {
1269 var i = this.scrollPort_.getTopRowIndex();
1270 this.scrollPort_.scrollRowToTop(i + 1);
1271};
1272
1273/**
Robert Ginda40932892012-12-10 17:26:40 -08001274 * Clear primary screen, secondary screen, and the scrollback buffer.
1275 */
1276hterm.Terminal.prototype.wipeContents = function() {
Mike Frysinger9c482b82018-09-07 02:49:36 -04001277 this.clearHome(this.primaryScreen_);
1278 this.clearHome(this.alternateScreen_);
1279
1280 this.clearScrollback();
1281};
1282
1283/**
1284 * Clear scrollback buffer.
1285 */
1286hterm.Terminal.prototype.clearScrollback = function() {
1287 // Move to the end of the buffer in case the screen was scrolled back.
1288 // We're going to throw it away which would leave the display invalid.
1289 this.scrollEnd();
1290
Robert Ginda40932892012-12-10 17:26:40 -08001291 this.scrollbackRows_.length = 0;
1292 this.scrollPort_.resetCache();
1293
Mike Frysinger9c482b82018-09-07 02:49:36 -04001294 [this.primaryScreen_, this.alternateScreen_].forEach((screen) => {
1295 const bottom = screen.getHeight();
1296 this.renumberRows_(0, bottom, screen);
1297 });
Robert Ginda40932892012-12-10 17:26:40 -08001298
1299 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001300 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001301};
1302
1303/**
rgindac9bc5502012-01-18 11:48:44 -08001304 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001305 *
1306 * Perform a full reset to the default values listed in
1307 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001308 */
rginda87b86462011-12-14 13:48:03 -08001309hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001310 this.vt.reset();
1311
rgindac9bc5502012-01-18 11:48:44 -08001312 this.clearAllTabStops();
1313 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001314
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001315 const resetScreen = (screen) => {
1316 // We want to make sure to reset the attributes before we clear the screen.
1317 // The attributes might be used to initialize default/empty rows.
1318 screen.textAttributes.reset();
1319 screen.textAttributes.resetColorPalette();
1320 this.clearHome(screen);
1321 screen.saveCursorAndState(this.vt);
1322 };
1323 resetScreen(this.primaryScreen_);
1324 resetScreen(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001325
Mike Frysinger84301d02017-11-29 13:28:46 -08001326 // Reset terminal options to their default values.
1327 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001328 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1329
Mike Frysinger84301d02017-11-29 13:28:46 -08001330 this.setVTScrollRegion(null, null);
1331
1332 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001333};
1334
rgindac9bc5502012-01-18 11:48:44 -08001335/**
1336 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001337 *
1338 * Perform a soft reset to the default values listed in
1339 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001340 */
rginda0f5c0292012-01-13 11:00:13 -08001341hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001342 this.vt.reset();
1343
rgindab8bc8932012-04-27 12:45:03 -07001344 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001345 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001346
Brad Townb62dfdc2015-03-16 19:07:15 -07001347 // We show the cursor on soft reset but do not alter the blink state.
1348 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1349
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001350 const resetScreen = (screen) => {
1351 // Xterm also resets the color palette on soft reset, even though it doesn't
1352 // seem to be documented anywhere.
1353 screen.textAttributes.reset();
1354 screen.textAttributes.resetColorPalette();
1355 screen.saveCursorAndState(this.vt);
1356 };
1357 resetScreen(this.primaryScreen_);
1358 resetScreen(this.alternateScreen_);
rgindaf522ce02012-04-17 17:49:17 -07001359
rgindab8bc8932012-04-27 12:45:03 -07001360 // The xterm man page explicitly says this will happen on soft reset.
1361 this.setVTScrollRegion(null, null);
1362
1363 // Xterm also shows the cursor on soft reset, but does not alter the blink
1364 // state.
rgindaa19afe22012-01-25 15:40:22 -08001365 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001366};
1367
rgindac9bc5502012-01-18 11:48:44 -08001368/**
1369 * Move the cursor forward to the next tab stop, or to the last column
1370 * if no more tab stops are set.
1371 */
1372hterm.Terminal.prototype.forwardTabStop = function() {
1373 var column = this.screen_.cursorPosition.column;
1374
1375 for (var i = 0; i < this.tabStops_.length; i++) {
1376 if (this.tabStops_[i] > column) {
1377 this.setCursorColumn(this.tabStops_[i]);
1378 return;
1379 }
1380 }
1381
David Benjamin66e954d2012-05-05 21:08:12 -04001382 // xterm does not clear the overflow flag on HT or CHT.
1383 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001384 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001385 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001386};
1387
rgindac9bc5502012-01-18 11:48:44 -08001388/**
1389 * Move the cursor backward to the previous tab stop, or to the first column
1390 * if no previous tab stops are set.
1391 */
1392hterm.Terminal.prototype.backwardTabStop = function() {
1393 var column = this.screen_.cursorPosition.column;
1394
1395 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1396 if (this.tabStops_[i] < column) {
1397 this.setCursorColumn(this.tabStops_[i]);
1398 return;
1399 }
1400 }
1401
1402 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001403};
1404
rgindac9bc5502012-01-18 11:48:44 -08001405/**
1406 * Set a tab stop at the given column.
1407 *
Joel Hockey0f933582019-08-27 18:01:51 -07001408 * @param {number} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001409 */
1410hterm.Terminal.prototype.setTabStop = function(column) {
1411 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1412 if (this.tabStops_[i] == column)
1413 return;
1414
1415 if (this.tabStops_[i] < column) {
1416 this.tabStops_.splice(i + 1, 0, column);
1417 return;
1418 }
1419 }
1420
1421 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001422};
1423
rgindac9bc5502012-01-18 11:48:44 -08001424/**
1425 * Clear the tab stop at the current cursor position.
1426 *
1427 * No effect if there is no tab stop at the current cursor position.
1428 */
1429hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1430 var column = this.screen_.cursorPosition.column;
1431
1432 var i = this.tabStops_.indexOf(column);
1433 if (i == -1)
1434 return;
1435
1436 this.tabStops_.splice(i, 1);
1437};
1438
1439/**
1440 * Clear all tab stops.
1441 */
1442hterm.Terminal.prototype.clearAllTabStops = function() {
1443 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001444 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001445};
1446
1447/**
1448 * Set up the default tab stops, starting from a given column.
1449 *
1450 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001451 * from the specified column, or 0 if no column is provided. It also flags
1452 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001453 *
1454 * This does not clear the existing tab stops first, use clearAllTabStops
1455 * for that.
1456 *
Joel Hockey0f933582019-08-27 18:01:51 -07001457 * @param {number=} opt_start Optional starting zero based starting column,
1458 * useful for filling out missing tab stops when the terminal is resized.
rgindac9bc5502012-01-18 11:48:44 -08001459 */
1460hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1461 var start = opt_start || 0;
1462 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001463 // Round start up to a default tab stop.
1464 start = start - 1 - ((start - 1) % w) + w;
1465 for (var i = start; i < this.screenSize.width; i += w) {
1466 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001467 }
David Benjamin66e954d2012-05-05 21:08:12 -04001468
1469 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001470};
1471
rginda6d397402012-01-17 10:58:29 -08001472/**
rginda8ba33642011-12-14 12:31:31 -08001473 * Interpret a sequence of characters.
1474 *
1475 * Incomplete escape sequences are buffered until the next call.
1476 *
1477 * @param {string} str Sequence of characters to interpret or pass through.
1478 */
1479hterm.Terminal.prototype.interpret = function(str) {
rginda8ba33642011-12-14 12:31:31 -08001480 this.scheduleSyncCursorPosition_();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001481 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001482};
1483
1484/**
1485 * Take over the given DIV for use as the terminal display.
1486 *
Joel Hockey0f933582019-08-27 18:01:51 -07001487 * @param {!Element} div The div to use as the terminal display.
rginda8ba33642011-12-14 12:31:31 -08001488 */
1489hterm.Terminal.prototype.decorate = function(div) {
Mike Frysinger5768a9d2017-12-26 12:57:44 -05001490 const charset = div.ownerDocument.characterSet.toLowerCase();
1491 if (charset != 'utf-8') {
1492 console.warn(`Document encoding should be set to utf-8, not "${charset}";` +
1493 ` Add <meta charset='utf-8'/> to your HTML <head> to fix.`);
1494 }
1495
rginda87b86462011-12-14 13:48:03 -08001496 this.div_ = div;
1497
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001498 this.accessibilityReader_ = new hterm.AccessibilityReader(div);
1499
Adrián Pérez-Orozco394e64f2018-12-17 17:20:16 -08001500 this.scrollPort_.decorate(div, () => this.setupScrollPort_());
1501};
1502
1503/**
1504 * Initialisation of ScrollPort properties which need to be set after its DOM
1505 * has been initialised.
Mike Frysinger23b5b832019-10-01 17:05:29 -04001506 *
Adrián Pérez-Orozco394e64f2018-12-17 17:20:16 -08001507 * @private
1508 */
1509hterm.Terminal.prototype.setupScrollPort_ = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -07001510 this.scrollPort_.setBackgroundImage(
1511 this.prefs_.getString('background-image'));
1512 this.scrollPort_.setBackgroundSize(this.prefs_.getString('background-size'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001513 this.scrollPort_.setBackgroundPosition(
Joel Hockeyd4fca732019-09-20 16:57:03 -07001514 this.prefs_.getString('background-position'));
1515 this.scrollPort_.setUserCssUrl(this.prefs_.getString('user-css'));
1516 this.scrollPort_.setUserCssText(this.prefs_.getString('user-css-text'));
1517 this.scrollPort_.setAccessibilityReader(
1518 lib.notNull(this.accessibilityReader_));
rginda30f20f62012-04-05 16:36:19 -07001519
rginda0918b652012-04-04 11:26:24 -07001520 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001521
Joel Hockeyd4fca732019-09-20 16:57:03 -07001522 this.setFontSize(this.prefs_.getNumber('font-size'));
rginda9f5222b2012-03-05 11:53:28 -08001523 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001524
Joel Hockeyd4fca732019-09-20 16:57:03 -07001525 this.setScrollbarVisible(this.prefs_.getBoolean('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001526 this.setScrollWheelMoveMultipler(
Joel Hockeyd4fca732019-09-20 16:57:03 -07001527 this.prefs_.getNumber('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001528
rginda8ba33642011-12-14 12:31:31 -08001529 this.document_ = this.scrollPort_.getDocument();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001530 this.accessibilityReader_.decorate(this.document_);
rginda8ba33642011-12-14 12:31:31 -08001531
Evan Jones5f9df812016-12-06 09:38:58 -05001532 this.document_.body.oncontextmenu = function() { return false; };
Mike Frysingercc114512017-09-11 21:39:17 -04001533 this.contextMenu.setDocument(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07001534
1535 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001536 var screenNode = this.scrollPort_.getScreenNode();
Joel Hockeyd4fca732019-09-20 16:57:03 -07001537 screenNode.addEventListener(
1538 'mousedown', /** @type {!EventListener} */ (onMouse));
1539 screenNode.addEventListener(
1540 'mouseup', /** @type {!EventListener} */ (onMouse));
1541 screenNode.addEventListener(
1542 'mousemove', /** @type {!EventListener} */ (onMouse));
rginda4bba5e12012-06-20 16:15:30 -07001543 this.scrollPort_.onScrollWheel = onMouse;
1544
Joel Hockeyd4fca732019-09-20 16:57:03 -07001545 screenNode.addEventListener(
1546 'keydown',
1547 /** @type {!EventListener} */ (this.onKeyboardActivity_.bind(this)));
Mike Frysinger02ded6d2018-06-21 14:25:20 -04001548
Toni Barzic0bfa8922013-11-22 11:18:35 -08001549 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001550 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001551 // Listen for mousedown events on the screenNode as in FF the focus
1552 // events don't bubble.
1553 screenNode.addEventListener('mousedown', function() {
1554 setTimeout(this.onFocusChange_.bind(this, true));
1555 }.bind(this));
1556
Toni Barzic0bfa8922013-11-22 11:18:35 -08001557 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001558 'blur', this.onFocusChange_.bind(this, false));
1559
1560 var style = this.document_.createElement('style');
Joel Hockeyd36efd62019-09-30 14:16:20 -07001561 style.textContent = `
1562.cursor-node[focus="false"] {
1563 box-sizing: border-box;
1564 background-color: transparent !important;
1565 border-width: 2px;
1566 border-style: solid;
1567}
1568menu {
1569 margin: 0;
1570 padding: 0;
1571 cursor: var(--hterm-mouse-cursor-pointer);
1572}
1573menuitem {
1574 white-space: nowrap;
1575 border-bottom: 1px dashed;
1576 display: block;
1577 padding: 0.3em 0.3em 0 0.3em;
1578}
1579menuitem.separator {
1580 border-bottom: none;
1581 height: 0.5em;
1582 padding: 0;
1583}
1584menuitem:hover {
1585 color: var(--hterm-cursor-color);
1586}
1587.wc-node {
1588 display: inline-block;
1589 text-align: center;
1590 width: calc(var(--hterm-charsize-width) * 2);
1591 line-height: var(--hterm-charsize-height);
1592}
1593:root {
1594 --hterm-charsize-width: ${this.scrollPort_.characterSize.width}px;
1595 --hterm-charsize-height: ${this.scrollPort_.characterSize.height}px;
1596 /* Default position hides the cursor for when the window is initializing. */
1597 --hterm-cursor-offset-col: -1;
1598 --hterm-cursor-offset-row: -1;
1599 --hterm-blink-node-duration: 0.7s;
1600 --hterm-mouse-cursor-default: default;
1601 --hterm-mouse-cursor-text: text;
1602 --hterm-mouse-cursor-pointer: pointer;
1603 --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);
1604}
1605.uri-node:hover {
1606 text-decoration: underline;
1607 cursor: var(--hterm-mouse-cursor-pointer);
1608}
1609@keyframes blink {
1610 from { opacity: 1.0; }
1611 to { opacity: 0.0; }
1612}
1613.blink-node {
1614 animation-name: blink;
1615 animation-duration: var(--hterm-blink-node-duration);
1616 animation-iteration-count: infinite;
1617 animation-timing-function: ease-in-out;
1618 animation-direction: alternate;
1619}`;
Mike Frysingerb74a6472018-06-22 13:37:08 -04001620 // Insert this stock style as the first node so that any user styles will
1621 // override w/out having to use !important everywhere. The rules above mix
1622 // runtime variables with default ones designed to be overridden by the user,
1623 // but we can wait for a concrete case from the users to determine the best
1624 // way to split the sheet up to before & after the user-css settings.
1625 this.document_.head.insertBefore(style, this.document_.head.firstChild);
rginda8e92a692012-05-20 19:37:20 -07001626
rginda8ba33642011-12-14 12:31:31 -08001627 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001628 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001629 this.cursorNode_.className = 'cursor-node';
Joel Hockeyd36efd62019-09-30 14:16:20 -07001630 this.cursorNode_.style.cssText = `
1631position: absolute;
1632left: calc(var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));
1633top: calc(var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));
1634display: ${this.options_.cursorVisible ? '' : 'none'};
1635width: var(--hterm-charsize-width);
1636height: var(--hterm-charsize-height);
1637background-color: var(--hterm-cursor-color);
1638border-color: var(--hterm-cursor-color);
1639-webkit-transition: opacity, background-color 100ms linear;
1640-moz-transition: opacity, background-color 100ms linear;`;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001641
Mike Frysingerf02a2cb2017-12-21 00:34:03 -05001642 this.setCursorColor();
Robert Gindafb1be6a2013-12-11 11:56:22 -08001643 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1644 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001645
rginda8ba33642011-12-14 12:31:31 -08001646 this.document_.body.appendChild(this.cursorNode_);
1647
rgindad5613292012-06-19 15:40:37 -07001648 // When 'enableMouseDragScroll' is off we reposition this element directly
1649 // under the mouse cursor after a click. This makes Chrome associate
1650 // subsequent mousemove events with the scroll-blocker. Since the
1651 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1652 // events do not cause the scrollport to scroll.
1653 //
1654 // It's a hack, but it's the cleanest way I could find.
1655 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001656 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
Raymes Khoury6dce2f82018-04-12 15:38:58 +10001657 this.scrollBlockerNode_.setAttribute('aria-hidden', 'true');
rgindad5613292012-06-19 15:40:37 -07001658 this.scrollBlockerNode_.style.cssText =
1659 ('position: absolute;' +
1660 'top: -99px;' +
1661 'display: block;' +
1662 'width: 10px;' +
1663 'height: 10px;');
1664 this.document_.body.appendChild(this.scrollBlockerNode_);
1665
rgindad5613292012-06-19 15:40:37 -07001666 this.scrollPort_.onScrollWheel = onMouse;
1667 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1668 ].forEach(function(event) {
1669 this.scrollBlockerNode_.addEventListener(event, onMouse);
Joel Hockeyd4fca732019-09-20 16:57:03 -07001670 this.cursorNode_.addEventListener(
1671 event, /** @type {!EventListener} */ (onMouse));
1672 this.document_.addEventListener(
1673 event, /** @type {!EventListener} */ (onMouse));
rgindad5613292012-06-19 15:40:37 -07001674 }.bind(this));
1675
1676 this.cursorNode_.addEventListener('mousedown', function() {
1677 setTimeout(this.focus.bind(this));
1678 }.bind(this));
1679
rginda8ba33642011-12-14 12:31:31 -08001680 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001681
rginda87b86462011-12-14 13:48:03 -08001682 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001683 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001684};
1685
rginda0918b652012-04-04 11:26:24 -07001686/**
1687 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001688 *
Joel Hockey0f933582019-08-27 18:01:51 -07001689 * @return {!Document}
rginda0918b652012-04-04 11:26:24 -07001690 */
rginda87b86462011-12-14 13:48:03 -08001691hterm.Terminal.prototype.getDocument = function() {
1692 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001693};
1694
1695/**
rginda0918b652012-04-04 11:26:24 -07001696 * Focus the terminal.
1697 */
1698hterm.Terminal.prototype.focus = function() {
1699 this.scrollPort_.focus();
1700};
1701
1702/**
Theodore Duboiscea9b782019-09-02 17:48:00 -07001703 * Unfocus the terminal.
1704 */
1705hterm.Terminal.prototype.blur = function() {
1706 this.scrollPort_.blur();
1707};
1708
1709/**
rginda8ba33642011-12-14 12:31:31 -08001710 * Return the HTML Element for a given row index.
1711 *
1712 * This is a method from the RowProvider interface. The ScrollPort uses
1713 * it to fetch rows on demand as they are scrolled into view.
1714 *
1715 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1716 * pairs to conserve memory.
1717 *
Joel Hockey0f933582019-08-27 18:01:51 -07001718 * @param {number} index The zero-based row index, measured relative to the
rginda8ba33642011-12-14 12:31:31 -08001719 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001720 * largest indices.
Joel Hockey0f933582019-08-27 18:01:51 -07001721 * @return {!Element} The 'x-row' element containing for the requested row.
Joel Hockeyd4fca732019-09-20 16:57:03 -07001722 * @override
rginda8ba33642011-12-14 12:31:31 -08001723 */
1724hterm.Terminal.prototype.getRowNode = function(index) {
1725 if (index < this.scrollbackRows_.length)
1726 return this.scrollbackRows_[index];
1727
1728 var screenIndex = index - this.scrollbackRows_.length;
1729 return this.screen_.rowsArray[screenIndex];
1730};
1731
1732/**
1733 * Return the text content for a given range of rows.
1734 *
1735 * This is a method from the RowProvider interface. The ScrollPort uses
1736 * it to fetch text content on demand when the user attempts to copy their
1737 * selection to the clipboard.
1738 *
Joel Hockey0f933582019-08-27 18:01:51 -07001739 * @param {number} start The zero-based row index to start from, measured
rginda8ba33642011-12-14 12:31:31 -08001740 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001741 * always have the largest indices.
Joel Hockey0f933582019-08-27 18:01:51 -07001742 * @param {number} end The zero-based row index to end on, measured
rginda8ba33642011-12-14 12:31:31 -08001743 * relative to the start of the scrollback buffer.
1744 * @return {string} A single string containing the text value of the range of
1745 * rows. Lines will be newline delimited, with no trailing newline.
1746 */
1747hterm.Terminal.prototype.getRowsText = function(start, end) {
1748 var ary = [];
1749 for (var i = start; i < end; i++) {
1750 var node = this.getRowNode(i);
1751 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001752 if (i < end - 1 && !node.getAttribute('line-overflow'))
1753 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001754 }
1755
rgindaa09e7332012-08-17 12:49:51 -07001756 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001757};
1758
1759/**
1760 * Return the text content for a given row.
1761 *
1762 * This is a method from the RowProvider interface. The ScrollPort uses
1763 * it to fetch text content on demand when the user attempts to copy their
1764 * selection to the clipboard.
1765 *
Joel Hockey0f933582019-08-27 18:01:51 -07001766 * @param {number} index The zero-based row index to return, measured
rginda8ba33642011-12-14 12:31:31 -08001767 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001768 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001769 * @return {string} A string containing the text value of the selected row.
1770 */
1771hterm.Terminal.prototype.getRowText = function(index) {
1772 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001773 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001774};
1775
1776/**
1777 * Return the total number of rows in the addressable screen and in the
1778 * scrollback buffer of this terminal.
1779 *
1780 * This is a method from the RowProvider interface. The ScrollPort uses
1781 * it to compute the size of the scrollbar.
1782 *
Joel Hockey0f933582019-08-27 18:01:51 -07001783 * @return {number} The number of rows in this terminal.
Joel Hockeyd4fca732019-09-20 16:57:03 -07001784 * @override
rginda8ba33642011-12-14 12:31:31 -08001785 */
1786hterm.Terminal.prototype.getRowCount = function() {
1787 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1788};
1789
1790/**
1791 * Create DOM nodes for new rows and append them to the end of the terminal.
1792 *
1793 * This is the only correct way to add a new DOM node for a row. Notice that
1794 * the new row is appended to the bottom of the list of rows, and does not
1795 * require renumbering (of the rowIndex property) of previous rows.
1796 *
1797 * If you think you want a new blank row somewhere in the middle of the
1798 * terminal, look into moveRows_().
1799 *
1800 * This method does not pay attention to vtScrollTop/Bottom, since you should
1801 * be using moveRows() in cases where they would matter.
1802 *
1803 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001804 *
1805 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001806 */
1807hterm.Terminal.prototype.appendRows_ = function(count) {
1808 var cursorRow = this.screen_.rowsArray.length;
1809 var offset = this.scrollbackRows_.length + cursorRow;
1810 for (var i = 0; i < count; i++) {
1811 var row = this.document_.createElement('x-row');
1812 row.appendChild(this.document_.createTextNode(''));
1813 row.rowIndex = offset + i;
1814 this.screen_.pushRow(row);
1815 }
1816
1817 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1818 if (extraRows > 0) {
1819 var ary = this.screen_.shiftRows(extraRows);
1820 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001821 if (this.scrollPort_.isScrolledEnd)
1822 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001823 }
1824
1825 if (cursorRow >= this.screen_.rowsArray.length)
1826 cursorRow = this.screen_.rowsArray.length - 1;
1827
rginda87b86462011-12-14 13:48:03 -08001828 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001829};
1830
1831/**
1832 * Relocate rows from one part of the addressable screen to another.
1833 *
1834 * This is used to recycle rows during VT scrolls (those which are driven
1835 * by VT commands, rather than by the user manipulating the scrollbar.)
1836 *
1837 * In this case, the blank lines scrolled into the scroll region are made of
1838 * the nodes we scrolled off. These have their rowIndex properties carefully
1839 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001840 *
1841 * @param {number} fromIndex The start index.
1842 * @param {number} count The number of rows to move.
1843 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001844 */
1845hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1846 var ary = this.screen_.removeRows(fromIndex, count);
1847 this.screen_.insertRows(toIndex, ary);
1848
1849 var start, end;
1850 if (fromIndex < toIndex) {
1851 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001852 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001853 } else {
1854 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001855 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001856 }
1857
1858 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001859 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001860};
1861
1862/**
1863 * Renumber the rowIndex property of the given range of rows.
1864 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001865 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08001866 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001867 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001868 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05001869 *
1870 * @param {number} start The start index.
1871 * @param {number} end The end index.
Joel Hockey0f933582019-08-27 18:01:51 -07001872 * @param {!hterm.Screen=} opt_screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08001873 */
Robert Ginda40932892012-12-10 17:26:40 -08001874hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1875 var screen = opt_screen || this.screen_;
1876
rginda8ba33642011-12-14 12:31:31 -08001877 var offset = this.scrollbackRows_.length;
1878 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001879 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001880 }
1881};
1882
1883/**
1884 * Print a string to the terminal.
1885 *
1886 * This respects the current insert and wraparound modes. It will add new lines
1887 * to the end of the terminal, scrolling off the top into the scrollback buffer
1888 * if necessary.
1889 *
1890 * The string is *not* parsed for escape codes. Use the interpret() method if
1891 * that's what you're after.
1892 *
Mike Frysingerfd449572019-09-23 03:18:14 -04001893 * @param {string} str The string to print.
rginda8ba33642011-12-14 12:31:31 -08001894 */
1895hterm.Terminal.prototype.print = function(str) {
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001896 this.scheduleSyncCursorPosition_();
1897
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001898 // Basic accessibility output for the screen reader.
Raymes Khoury177aec72018-06-26 10:58:53 +10001899 this.accessibilityReader_.announce(str);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001900
rgindaa9abdd82012-08-06 18:05:09 -07001901 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001902
Ricky Liang48f05cb2013-12-31 23:35:29 +08001903 var strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04001904 // Fun edge case: If the string only contains zero width codepoints (like
1905 // combining characters), we make sure to iterate at least once below.
1906 if (strWidth == 0 && str)
1907 strWidth = 1;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001908
1909 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001910 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1911 this.screen_.commitLineOverflow();
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001912 this.newLine(true);
rgindaa09e7332012-08-17 12:49:51 -07001913 }
rgindaa19afe22012-01-25 15:40:22 -08001914
Ricky Liang48f05cb2013-12-31 23:35:29 +08001915 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001916 var didOverflow = false;
1917 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001918
rgindaa9abdd82012-08-06 18:05:09 -07001919 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1920 didOverflow = true;
1921 count = this.screenSize.width - this.screen_.cursorPosition.column;
1922 }
rgindaa19afe22012-01-25 15:40:22 -08001923
rgindaa9abdd82012-08-06 18:05:09 -07001924 if (didOverflow && !this.options_.wraparound) {
1925 // If the string overflowed the line but wraparound is off, then the
1926 // last printed character should be the last of the string.
1927 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001928 substr = lib.wc.substr(str, startOffset, count - 1) +
1929 lib.wc.substr(str, strWidth - 1);
1930 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001931 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001932 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001933 }
rgindaa19afe22012-01-25 15:40:22 -08001934
Ricky Liang48f05cb2013-12-31 23:35:29 +08001935 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1936 for (var i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001937 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
1938 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001939
1940 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001941 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001942 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001943 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001944 }
1945 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001946 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07001947 }
1948
1949 this.screen_.maybeClipCurrentRow();
1950 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001951 }
rginda8ba33642011-12-14 12:31:31 -08001952
rginda9f5222b2012-03-05 11:53:28 -08001953 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001954 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001955};
1956
1957/**
rginda87b86462011-12-14 13:48:03 -08001958 * Set the VT scroll region.
1959 *
rginda87b86462011-12-14 13:48:03 -08001960 * This also resets the cursor position to the absolute (0, 0) position, since
1961 * that's what xterm appears to do.
1962 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001963 * Setting the scroll region to the full height of the terminal will clear
1964 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1965 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1966 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1967 * continue to work as most users would expect.
1968 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001969 * @param {?number} scrollTop The zero-based top of the scroll region.
1970 * @param {?number} scrollBottom The zero-based bottom of the scroll region,
rginda87b86462011-12-14 13:48:03 -08001971 * inclusive.
1972 */
1973hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001974 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001975 this.vtScrollTop_ = null;
1976 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001977 } else {
1978 this.vtScrollTop_ = scrollTop;
1979 this.vtScrollBottom_ = scrollBottom;
1980 }
rginda87b86462011-12-14 13:48:03 -08001981};
1982
1983/**
rginda8ba33642011-12-14 12:31:31 -08001984 * Return the top row index according to the VT.
1985 *
1986 * This will return 0 unless the terminal has been told to restrict scrolling
1987 * to some lower row. It is used for some VT cursor positioning and scrolling
1988 * commands.
1989 *
Joel Hockey0f933582019-08-27 18:01:51 -07001990 * @return {number} The topmost row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001991 */
1992hterm.Terminal.prototype.getVTScrollTop = function() {
1993 if (this.vtScrollTop_ != null)
1994 return this.vtScrollTop_;
1995
1996 return 0;
rginda87b86462011-12-14 13:48:03 -08001997};
rginda8ba33642011-12-14 12:31:31 -08001998
1999/**
2000 * Return the bottom row index according to the VT.
2001 *
2002 * This will return the height of the terminal unless the it has been told to
2003 * restrict scrolling to some higher row. It is used for some VT cursor
2004 * positioning and scrolling commands.
2005 *
Joel Hockey0f933582019-08-27 18:01:51 -07002006 * @return {number} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08002007 */
2008hterm.Terminal.prototype.getVTScrollBottom = function() {
2009 if (this.vtScrollBottom_ != null)
2010 return this.vtScrollBottom_;
2011
rginda87b86462011-12-14 13:48:03 -08002012 return this.screenSize.height - 1;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04002013};
rginda8ba33642011-12-14 12:31:31 -08002014
2015/**
2016 * Process a '\n' character.
2017 *
2018 * If the cursor is on the final row of the terminal this will append a new
2019 * blank row to the screen and scroll the topmost row into the scrollback
2020 * buffer.
2021 *
2022 * Otherwise, this moves the cursor to column zero of the next row.
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002023 *
2024 * @param {boolean=} dueToOverflow Whether the newline is due to wraparound of
2025 * the terminal.
rginda8ba33642011-12-14 12:31:31 -08002026 */
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002027hterm.Terminal.prototype.newLine = function(dueToOverflow = false) {
2028 if (!dueToOverflow)
2029 this.accessibilityReader_.newLine();
2030
Robert Ginda9937abc2013-07-25 16:09:23 -07002031 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
2032 this.screen_.rowsArray.length - 1);
2033
2034 if (this.vtScrollBottom_ != null) {
2035 // A VT Scroll region is active, we never append new rows.
2036 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
2037 // We're at the end of the VT Scroll Region, perform a VT scroll.
2038 this.vtScrollUp(1);
2039 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
2040 } else if (cursorAtEndOfScreen) {
2041 // We're at the end of the screen, the only thing to do is put the
2042 // cursor to column 0.
2043 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
2044 } else {
2045 // Anywhere else, advance the cursor row, and reset the column.
2046 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
2047 }
2048 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07002049 // We're at the end of the screen. Append a new row to the terminal,
2050 // shifting the top row into the scrollback.
2051 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08002052 } else {
rginda87b86462011-12-14 13:48:03 -08002053 // Anywhere else in the screen just moves the cursor.
2054 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08002055 }
2056};
2057
2058/**
2059 * Like newLine(), except maintain the cursor column.
2060 */
2061hterm.Terminal.prototype.lineFeed = function() {
2062 var column = this.screen_.cursorPosition.column;
2063 this.newLine();
2064 this.setCursorColumn(column);
2065};
2066
2067/**
rginda87b86462011-12-14 13:48:03 -08002068 * If autoCarriageReturn is set then newLine(), else lineFeed().
2069 */
2070hterm.Terminal.prototype.formFeed = function() {
2071 if (this.options_.autoCarriageReturn) {
2072 this.newLine();
2073 } else {
2074 this.lineFeed();
2075 }
2076};
2077
2078/**
2079 * Move the cursor up one row, possibly inserting a blank line.
2080 *
2081 * The cursor column is not changed.
2082 */
2083hterm.Terminal.prototype.reverseLineFeed = function() {
2084 var scrollTop = this.getVTScrollTop();
2085 var currentRow = this.screen_.cursorPosition.row;
2086
2087 if (currentRow == scrollTop) {
2088 this.insertLines(1);
2089 } else {
2090 this.setAbsoluteCursorRow(currentRow - 1);
2091 }
2092};
2093
2094/**
rginda8ba33642011-12-14 12:31:31 -08002095 * Replace all characters to the left of the current cursor with the space
2096 * character.
2097 *
2098 * TODO(rginda): This should probably *remove* the characters (not just replace
2099 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07002100 * position.
rginda8ba33642011-12-14 12:31:31 -08002101 */
2102hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08002103 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002104 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002105 const count = cursor.column + 1;
Mike Frysinger73e56462019-07-17 00:23:46 -05002106 this.screen_.overwriteString(' '.repeat(count), count);
rginda87b86462011-12-14 13:48:03 -08002107 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002108};
2109
2110/**
David Benjamin684a9b72012-05-01 17:19:58 -04002111 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08002112 *
2113 * The cursor position is unchanged.
2114 *
Robert Gindaf2547f12012-10-25 20:36:21 -07002115 * If the current background color is not the default background color this
2116 * will insert spaces rather than delete. This is unfortunate because the
2117 * trailing space will affect text selection, but it's difficult to come up
2118 * with a way to style empty space that wouldn't trip up the hterm.Screen
2119 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07002120 *
2121 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
2122 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
2123 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05002124 *
Joel Hockey0f933582019-08-27 18:01:51 -07002125 * @param {number=} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08002126 */
2127hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07002128 if (this.screen_.cursorPosition.overflow)
2129 return;
2130
Robert Ginda7fd57082012-09-25 14:41:47 -07002131 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
2132 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07002133
2134 if (this.screen_.textAttributes.background ===
2135 this.screen_.textAttributes.DEFAULT_COLOR) {
2136 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08002137 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07002138 this.screen_.cursorPosition.column + count) {
2139 this.screen_.deleteChars(count);
2140 this.clearCursorOverflow();
2141 return;
2142 }
2143 }
2144
rginda87b86462011-12-14 13:48:03 -08002145 var cursor = this.saveCursor();
Mike Frysinger73e56462019-07-17 00:23:46 -05002146 this.screen_.overwriteString(' '.repeat(count), count);
rginda87b86462011-12-14 13:48:03 -08002147 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002148 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002149};
2150
2151/**
2152 * Erase the current line.
2153 *
2154 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002155 */
2156hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08002157 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002158 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08002159 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002160 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002161};
2162
2163/**
David Benjamina08d78f2012-05-05 00:28:49 -04002164 * Erase all characters from the start of the screen to the current cursor
2165 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002166 *
2167 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002168 */
2169hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08002170 var cursor = this.saveCursor();
2171
2172 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002173
David Benjamina08d78f2012-05-05 00:28:49 -04002174 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002175 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002176 this.screen_.clearCursorRow();
2177 }
2178
rginda87b86462011-12-14 13:48:03 -08002179 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002180 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002181};
2182
2183/**
2184 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002185 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002186 *
2187 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002188 */
2189hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08002190 var cursor = this.saveCursor();
2191
2192 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002193
David Benjamina08d78f2012-05-05 00:28:49 -04002194 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08002195 for (var i = cursor.row + 1; i <= bottom; i++) {
2196 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002197 this.screen_.clearCursorRow();
2198 }
2199
rginda87b86462011-12-14 13:48:03 -08002200 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002201 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002202};
2203
2204/**
2205 * Fill the terminal with a given character.
2206 *
2207 * This methods does not respect the VT scroll region.
2208 *
2209 * @param {string} ch The character to use for the fill.
2210 */
2211hterm.Terminal.prototype.fill = function(ch) {
2212 var cursor = this.saveCursor();
2213
2214 this.setAbsoluteCursorPosition(0, 0);
2215 for (var row = 0; row < this.screenSize.height; row++) {
2216 for (var col = 0; col < this.screenSize.width; col++) {
2217 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002218 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002219 }
2220 }
2221
2222 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002223};
2224
2225/**
rginda9ea433c2012-03-16 11:57:00 -07002226 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002227 *
rginda9ea433c2012-03-16 11:57:00 -07002228 * This does not respect the scroll region.
2229 *
Joel Hockey0f933582019-08-27 18:01:51 -07002230 * @param {!hterm.Screen=} opt_screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002231 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002232 */
rginda9ea433c2012-03-16 11:57:00 -07002233hterm.Terminal.prototype.clearHome = function(opt_screen) {
2234 var screen = opt_screen || this.screen_;
2235 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002236
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002237 this.accessibilityReader_.clear();
2238
rginda11057d52012-04-25 12:29:56 -07002239 if (bottom == 0) {
2240 // Empty screen, nothing to do.
2241 return;
2242 }
2243
rgindae4d29232012-01-19 10:47:13 -08002244 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002245 screen.setCursorPosition(i, 0);
2246 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002247 }
2248
rginda9ea433c2012-03-16 11:57:00 -07002249 screen.setCursorPosition(0, 0);
2250};
2251
2252/**
2253 * Erase the entire display without changing the cursor position.
2254 *
2255 * The cursor position is unchanged. This does not respect the scroll
2256 * region.
2257 *
Joel Hockey0f933582019-08-27 18:01:51 -07002258 * @param {!hterm.Screen=} opt_screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002259 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002260 */
2261hterm.Terminal.prototype.clear = function(opt_screen) {
2262 var screen = opt_screen || this.screen_;
2263 var cursor = screen.cursorPosition.clone();
2264 this.clearHome(screen);
2265 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002266};
2267
2268/**
2269 * VT command to insert lines at the current cursor row.
2270 *
2271 * This respects the current scroll region. Rows pushed off the bottom are
2272 * lost (they won't show up in the scrollback buffer).
2273 *
Joel Hockey0f933582019-08-27 18:01:51 -07002274 * @param {number} count The number of lines to insert.
rginda8ba33642011-12-14 12:31:31 -08002275 */
2276hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07002277 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002278
2279 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002280 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002281
Robert Ginda579186b2012-09-26 11:40:04 -07002282 // The moveCount is the number of rows we need to relocate to make room for
2283 // the new row(s). The count is the distance to move them.
2284 var moveCount = bottom - cursorRow - count + 1;
2285 if (moveCount)
2286 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08002287
Robert Ginda579186b2012-09-26 11:40:04 -07002288 for (var i = count - 1; i >= 0; i--) {
2289 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002290 this.screen_.clearCursorRow();
2291 }
rginda8ba33642011-12-14 12:31:31 -08002292};
2293
2294/**
2295 * VT command to delete lines at the current cursor row.
2296 *
2297 * New rows are added to the bottom of scroll region to take their place. New
2298 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002299 *
2300 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002301 */
2302hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08002303 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002304
rginda87b86462011-12-14 13:48:03 -08002305 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08002306 var bottom = this.getVTScrollBottom();
2307
rginda87b86462011-12-14 13:48:03 -08002308 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002309 count = Math.min(count, maxCount);
2310
rginda87b86462011-12-14 13:48:03 -08002311 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08002312 if (count != maxCount)
2313 this.moveRows_(top, count, moveStart);
2314
2315 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002316 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002317 this.screen_.clearCursorRow();
2318 }
2319
rginda87b86462011-12-14 13:48:03 -08002320 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002321 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002322};
2323
2324/**
2325 * Inserts the given number of spaces at the current cursor position.
2326 *
rginda87b86462011-12-14 13:48:03 -08002327 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002328 *
2329 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002330 */
2331hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08002332 var cursor = this.saveCursor();
2333
Mike Frysinger73e56462019-07-17 00:23:46 -05002334 const ws = ' '.repeat(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002335 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002336 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002337
2338 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002339 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002340};
2341
2342/**
2343 * Forward-delete the specified number of characters starting at the cursor
2344 * position.
2345 *
Joel Hockey0f933582019-08-27 18:01:51 -07002346 * @param {number} count The number of characters to delete.
rginda8ba33642011-12-14 12:31:31 -08002347 */
2348hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002349 var deleted = this.screen_.deleteChars(count);
2350 if (deleted && !this.screen_.textAttributes.isDefault()) {
2351 var cursor = this.saveCursor();
2352 this.setCursorColumn(this.screenSize.width - deleted);
Mike Frysinger73e56462019-07-17 00:23:46 -05002353 this.screen_.insertString(' '.repeat(deleted));
Robert Ginda7fd57082012-09-25 14:41:47 -07002354 this.restoreCursor(cursor);
2355 }
2356
David Benjamin54e8bf62012-06-01 22:31:40 -04002357 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002358};
2359
2360/**
2361 * Shift rows in the scroll region upwards by a given number of lines.
2362 *
2363 * New rows are inserted at the bottom of the scroll region to fill the
2364 * vacated rows. The new rows not filled out with the current text attributes.
2365 *
2366 * This function does not affect the scrollback rows at all. Rows shifted
2367 * off the top are lost.
2368 *
rginda87b86462011-12-14 13:48:03 -08002369 * The cursor position is not altered.
2370 *
Joel Hockey0f933582019-08-27 18:01:51 -07002371 * @param {number} count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002372 */
2373hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002374 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002375
rginda87b86462011-12-14 13:48:03 -08002376 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002377 this.deleteLines(count);
2378
rginda87b86462011-12-14 13:48:03 -08002379 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002380};
2381
2382/**
2383 * Shift rows below the cursor down by a given number of lines.
2384 *
2385 * This function respects the current scroll region.
2386 *
2387 * New rows are inserted at the top of the scroll region to fill the
2388 * vacated rows. The new rows not filled out with the current text attributes.
2389 *
2390 * This function does not affect the scrollback rows at all. Rows shifted
2391 * off the bottom are lost.
2392 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07002393 * @param {number} count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002394 */
Joel Hockeyd4fca732019-09-20 16:57:03 -07002395hterm.Terminal.prototype.vtScrollDown = function(count) {
rginda87b86462011-12-14 13:48:03 -08002396 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002397
rginda87b86462011-12-14 13:48:03 -08002398 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
Joel Hockeyd4fca732019-09-20 16:57:03 -07002399 this.insertLines(count);
rginda8ba33642011-12-14 12:31:31 -08002400
rginda87b86462011-12-14 13:48:03 -08002401 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002402};
2403
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002404/**
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002405 * Enable accessibility-friendly features that have a performance impact.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002406 *
2407 * This will generate additional DOM nodes in an aria-live region that will
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002408 * cause Assitive Technology to announce the output of the terminal. It also
2409 * enables other features that aid assistive technology. All the features gated
2410 * behind this flag have a performance impact on the terminal which is why they
2411 * are made optional.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002412 *
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002413 * @param {boolean} enabled Whether to enable accessibility-friendly features.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002414 */
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002415hterm.Terminal.prototype.setAccessibilityEnabled = function(enabled) {
Raymes Khoury177aec72018-06-26 10:58:53 +10002416 this.accessibilityReader_.setAccessibilityEnabled(enabled);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002417};
rginda87b86462011-12-14 13:48:03 -08002418
rginda8ba33642011-12-14 12:31:31 -08002419/**
2420 * Set the cursor position.
2421 *
2422 * The cursor row is relative to the scroll region if the terminal has
2423 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2424 *
Joel Hockey0f933582019-08-27 18:01:51 -07002425 * @param {number} row The new zero-based cursor row.
2426 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002427 */
2428hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2429 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002430 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002431 } else {
rginda87b86462011-12-14 13:48:03 -08002432 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002433 }
rginda87b86462011-12-14 13:48:03 -08002434};
rginda8ba33642011-12-14 12:31:31 -08002435
Evan Jones2600d4f2016-12-06 09:29:36 -05002436/**
2437 * Move the cursor relative to its current position.
2438 *
2439 * @param {number} row
2440 * @param {number} column
2441 */
rginda87b86462011-12-14 13:48:03 -08002442hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2443 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002444 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2445 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002446 this.screen_.setCursorPosition(row, column);
2447};
2448
Evan Jones2600d4f2016-12-06 09:29:36 -05002449/**
2450 * Move the cursor to the specified position.
2451 *
2452 * @param {number} row
2453 * @param {number} column
2454 */
rginda87b86462011-12-14 13:48:03 -08002455hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002456 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2457 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002458 this.screen_.setCursorPosition(row, column);
2459};
2460
2461/**
2462 * Set the cursor column.
2463 *
Joel Hockey0f933582019-08-27 18:01:51 -07002464 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002465 */
2466hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002467 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002468};
2469
2470/**
2471 * Return the cursor column.
2472 *
Joel Hockey0f933582019-08-27 18:01:51 -07002473 * @return {number} The zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002474 */
2475hterm.Terminal.prototype.getCursorColumn = function() {
2476 return this.screen_.cursorPosition.column;
2477};
2478
2479/**
2480 * Set the cursor row.
2481 *
2482 * The cursor row is relative to the scroll region if the terminal has
2483 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2484 *
Joel Hockey0f933582019-08-27 18:01:51 -07002485 * @param {number} row The new cursor row.
rginda8ba33642011-12-14 12:31:31 -08002486 */
rginda87b86462011-12-14 13:48:03 -08002487hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2488 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002489};
2490
2491/**
2492 * Return the cursor row.
2493 *
Joel Hockey0f933582019-08-27 18:01:51 -07002494 * @return {number} The zero-based cursor row.
rginda8ba33642011-12-14 12:31:31 -08002495 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002496hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002497 return this.screen_.cursorPosition.row;
2498};
2499
2500/**
2501 * Request that the ScrollPort redraw itself soon.
2502 *
2503 * The redraw will happen asynchronously, soon after the call stack winds down.
2504 * Multiple calls will be coalesced into a single redraw.
2505 */
2506hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002507 if (this.timeouts_.redraw)
2508 return;
rginda8ba33642011-12-14 12:31:31 -08002509
2510 var self = this;
rginda87b86462011-12-14 13:48:03 -08002511 this.timeouts_.redraw = setTimeout(function() {
2512 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002513 self.scrollPort_.redraw_();
2514 }, 0);
2515};
2516
2517/**
2518 * Request that the ScrollPort be scrolled to the bottom.
2519 *
2520 * The scroll will happen asynchronously, soon after the call stack winds down.
2521 * Multiple calls will be coalesced into a single scroll.
2522 *
2523 * This affects the scrollbar position of the ScrollPort, and has nothing to
2524 * do with the VT scroll commands.
2525 */
2526hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2527 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002528 return;
rginda8ba33642011-12-14 12:31:31 -08002529
2530 var self = this;
2531 this.timeouts_.scrollDown = setTimeout(function() {
2532 delete self.timeouts_.scrollDown;
2533 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2534 }, 10);
2535};
2536
2537/**
2538 * Move the cursor up a specified number of rows.
2539 *
Joel Hockey0f933582019-08-27 18:01:51 -07002540 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002541 */
2542hterm.Terminal.prototype.cursorUp = function(count) {
Joel Hockey0f933582019-08-27 18:01:51 -07002543 this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002544};
2545
2546/**
2547 * Move the cursor down a specified number of rows.
2548 *
Joel Hockey0f933582019-08-27 18:01:51 -07002549 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002550 */
2551hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002552 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002553 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2554 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2555 this.screenSize.height - 1);
2556
rgindacbbd7482012-06-13 15:06:16 -07002557 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002558 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002559 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002560};
2561
2562/**
2563 * Move the cursor left a specified number of columns.
2564 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002565 * If reverse wraparound mode is enabled and the previous row wrapped into
2566 * the current row then we back up through the wraparound as well.
2567 *
Joel Hockey0f933582019-08-27 18:01:51 -07002568 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002569 */
2570hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002571 count = count || 1;
2572
2573 if (count < 1)
2574 return;
2575
2576 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002577 if (this.options_.reverseWraparound) {
2578 if (this.screen_.cursorPosition.overflow) {
2579 // If this cursor is in the right margin, consume one count to get it
2580 // back to the last column. This only applies when we're in reverse
2581 // wraparound mode.
2582 count--;
2583 this.clearCursorOverflow();
2584
2585 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002586 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002587 }
2588
Robert Gindabfb32622014-07-17 13:20:27 -07002589 var newRow = this.screen_.cursorPosition.row;
2590 var newColumn = currentColumn - count;
2591 if (newColumn < 0) {
2592 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2593 if (newRow < 0) {
2594 // xterm also wraps from row 0 to the last row.
2595 newRow = this.screenSize.height + newRow % this.screenSize.height;
2596 }
2597 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2598 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002599
Robert Gindabfb32622014-07-17 13:20:27 -07002600 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2601
2602 } else {
2603 var newColumn = Math.max(currentColumn - count, 0);
2604 this.setCursorColumn(newColumn);
2605 }
rginda8ba33642011-12-14 12:31:31 -08002606};
2607
2608/**
2609 * Move the cursor right a specified number of columns.
2610 *
Joel Hockey0f933582019-08-27 18:01:51 -07002611 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002612 */
2613hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002614 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002615
2616 if (count < 1)
2617 return;
2618
rgindacbbd7482012-06-13 15:06:16 -07002619 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002620 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002621 this.setCursorColumn(column);
2622};
2623
2624/**
2625 * Reverse the foreground and background colors of the terminal.
2626 *
2627 * This only affects text that was drawn with no attributes.
2628 *
2629 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2630 * been drawn with attributes that happen to coincide with the default
2631 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002632 *
2633 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002634 */
2635hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002636 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002637 if (state) {
Mike Frysinger31cb1562017-07-31 23:44:18 -04002638 this.scrollPort_.setForegroundColor(this.backgroundColor_);
2639 this.scrollPort_.setBackgroundColor(this.foregroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002640 } else {
Mike Frysinger31cb1562017-07-31 23:44:18 -04002641 this.scrollPort_.setForegroundColor(this.foregroundColor_);
2642 this.scrollPort_.setBackgroundColor(this.backgroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002643 }
2644};
2645
2646/**
rginda87b86462011-12-14 13:48:03 -08002647 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002648 *
2649 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002650 */
2651hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002652 this.cursorNode_.style.backgroundColor =
2653 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002654
2655 var self = this;
2656 setTimeout(function() {
Matheus Fernandes2d733082017-09-11 06:43:01 -04002657 self.restyleCursor_();
rginda6d397402012-01-17 10:58:29 -08002658 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002659
Michael Kelly485ecd12014-06-09 11:41:56 -04002660 // bellSquelchTimeout_ affects both audio and notification bells.
2661 if (this.bellSquelchTimeout_)
2662 return;
2663
Robert Ginda92e18102013-03-14 13:56:37 -07002664 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002665 this.bellAudio_.play();
Joel Hockeyd4fca732019-09-20 16:57:03 -07002666 this.bellSequelchTimeout_ = setTimeout(() => {
2667 this.bellSquelchTimeout_ = null;
2668 }, 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002669 } else {
Joel Hockeyd4fca732019-09-20 16:57:03 -07002670 this.bellSquelchTimeout_ = null;
Robert Ginda92e18102013-03-14 13:56:37 -07002671 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002672
2673 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingera5fb83c2017-06-22 14:48:35 -07002674 var n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002675 this.bellNotificationList_.push(n);
2676 // TODO: Should we try to raise the window here?
2677 n.onclick = function() { self.closeBellNotifications_(); };
2678 }
rginda87b86462011-12-14 13:48:03 -08002679};
2680
2681/**
rginda8ba33642011-12-14 12:31:31 -08002682 * Set the origin mode bit.
2683 *
2684 * If origin mode is on, certain VT cursor and scrolling commands measure their
2685 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2686 * to the top of the addressable screen.
2687 *
2688 * Defaults to off.
2689 *
2690 * @param {boolean} state True to set origin mode, false to unset.
2691 */
2692hterm.Terminal.prototype.setOriginMode = function(state) {
2693 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002694 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002695};
2696
2697/**
2698 * Set the insert mode bit.
2699 *
2700 * If insert mode is on, existing text beyond the cursor position will be
2701 * shifted right to make room for new text. Otherwise, new text overwrites
2702 * any existing text.
2703 *
2704 * Defaults to off.
2705 *
2706 * @param {boolean} state True to set insert mode, false to unset.
2707 */
2708hterm.Terminal.prototype.setInsertMode = function(state) {
2709 this.options_.insertMode = state;
2710};
2711
2712/**
rginda87b86462011-12-14 13:48:03 -08002713 * Set the auto carriage return bit.
2714 *
2715 * If auto carriage return is on then a formfeed character is interpreted
2716 * as a newline, otherwise it's the same as a linefeed. The difference boils
2717 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002718 *
2719 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002720 */
2721hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2722 this.options_.autoCarriageReturn = state;
2723};
2724
2725/**
rginda8ba33642011-12-14 12:31:31 -08002726 * Set the wraparound mode bit.
2727 *
2728 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2729 * to the start of the following row. Otherwise, the cursor is clamped to the
2730 * end of the screen and attempts to write past it are ignored.
2731 *
2732 * Defaults to on.
2733 *
2734 * @param {boolean} state True to set wraparound mode, false to unset.
2735 */
2736hterm.Terminal.prototype.setWraparound = function(state) {
2737 this.options_.wraparound = state;
2738};
2739
2740/**
2741 * Set the reverse-wraparound mode bit.
2742 *
2743 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2744 * to the end of the previous row. Otherwise, the cursor is clamped to column
2745 * 0.
2746 *
2747 * Defaults to off.
2748 *
2749 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2750 */
2751hterm.Terminal.prototype.setReverseWraparound = function(state) {
2752 this.options_.reverseWraparound = state;
2753};
2754
2755/**
2756 * Selects between the primary and alternate screens.
2757 *
2758 * If alternate mode is on, the alternate screen is active. Otherwise the
2759 * primary screen is active.
2760 *
2761 * Swapping screens has no effect on the scrollback buffer.
2762 *
2763 * Each screen maintains its own cursor position.
2764 *
2765 * Defaults to off.
2766 *
2767 * @param {boolean} state True to set alternate mode, false to unset.
2768 */
2769hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002770 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002771 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2772
rginda35c456b2012-02-09 17:29:05 -08002773 if (this.screen_.rowsArray.length &&
2774 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2775 // If the screen changed sizes while we were away, our rowIndexes may
2776 // be incorrect.
2777 var offset = this.scrollbackRows_.length;
2778 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002779 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002780 ary[i].rowIndex = offset + i;
2781 }
2782 }
rginda8ba33642011-12-14 12:31:31 -08002783
rginda35c456b2012-02-09 17:29:05 -08002784 this.realizeWidth_(this.screenSize.width);
2785 this.realizeHeight_(this.screenSize.height);
2786 this.scrollPort_.syncScrollHeight();
2787 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002788
rginda6d397402012-01-17 10:58:29 -08002789 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002790 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002791};
2792
2793/**
2794 * Set the cursor-blink mode bit.
2795 *
2796 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2797 * a visible cursor does not blink.
2798 *
2799 * You should make sure to turn blinking off if you're going to dispose of a
2800 * terminal, otherwise you'll leak a timeout.
2801 *
2802 * Defaults to on.
2803 *
2804 * @param {boolean} state True to set cursor-blink mode, false to unset.
2805 */
2806hterm.Terminal.prototype.setCursorBlink = function(state) {
2807 this.options_.cursorBlink = state;
2808
2809 if (!state && this.timeouts_.cursorBlink) {
2810 clearTimeout(this.timeouts_.cursorBlink);
2811 delete this.timeouts_.cursorBlink;
2812 }
2813
2814 if (this.options_.cursorVisible)
2815 this.setCursorVisible(true);
2816};
2817
2818/**
2819 * Set the cursor-visible mode bit.
2820 *
2821 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2822 *
2823 * Defaults to on.
2824 *
2825 * @param {boolean} state True to set cursor-visible mode, false to unset.
2826 */
2827hterm.Terminal.prototype.setCursorVisible = function(state) {
2828 this.options_.cursorVisible = state;
2829
2830 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002831 if (this.timeouts_.cursorBlink) {
2832 clearTimeout(this.timeouts_.cursorBlink);
2833 delete this.timeouts_.cursorBlink;
2834 }
rginda87b86462011-12-14 13:48:03 -08002835 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002836 return;
2837 }
2838
rginda87b86462011-12-14 13:48:03 -08002839 this.syncCursorPosition_();
2840
2841 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002842
2843 if (this.options_.cursorBlink) {
2844 if (this.timeouts_.cursorBlink)
2845 return;
2846
Robert Gindaea2183e2014-07-17 09:51:51 -07002847 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002848 } else {
2849 if (this.timeouts_.cursorBlink) {
2850 clearTimeout(this.timeouts_.cursorBlink);
2851 delete this.timeouts_.cursorBlink;
2852 }
2853 }
2854};
2855
2856/**
Mike Frysinger225c99d2019-10-20 14:02:37 -06002857 * Pause blinking temporarily.
2858 *
2859 * When the cursor moves around, it can be helpful to momentarily pause the
2860 * blinking. This could be when the user is typing in things, or when they're
2861 * moving around with the arrow keys.
2862 */
2863hterm.Terminal.prototype.pauseCursorBlink_ = function() {
2864 if (!this.options_.cursorBlink) {
2865 return;
2866 }
2867
2868 this.cursorBlinkPause_ = true;
2869
2870 // If a timeout is already pending, reset the clock due to the new input.
2871 if (this.timeouts_.cursorBlinkPause) {
2872 clearTimeout(this.timeouts_.cursorBlinkPause);
2873 }
2874 // After 500ms, resume blinking. That seems like a good balance between user
2875 // input timings & responsiveness to resume.
2876 this.timeouts_.cursorBlinkPause = setTimeout(() => {
2877 delete this.timeouts_.cursorBlinkPause;
2878 this.cursorBlinkPause_ = false;
2879 }, 500);
2880};
2881
2882/**
rginda87b86462011-12-14 13:48:03 -08002883 * Synchronizes the visible cursor and document selection with the current
2884 * cursor coordinates.
Raymes Khourye5d48982018-08-02 09:08:32 +10002885 *
2886 * @return {boolean} True if the cursor is onscreen and synced.
rginda8ba33642011-12-14 12:31:31 -08002887 */
2888hterm.Terminal.prototype.syncCursorPosition_ = function() {
2889 var topRowIndex = this.scrollPort_.getTopRowIndex();
2890 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2891 var cursorRowIndex = this.scrollbackRows_.length +
2892 this.screen_.cursorPosition.row;
2893
Raymes Khoury15697f42018-07-17 11:37:18 +10002894 let forceSyncSelection = false;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002895 if (this.accessibilityReader_.accessibilityEnabled) {
2896 // Report the new position of the cursor for accessibility purposes.
2897 const cursorColumnIndex = this.screen_.cursorPosition.column;
2898 const cursorLineText =
2899 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
Raymes Khoury15697f42018-07-17 11:37:18 +10002900 // This will force the selection to be sync'd to the cursor position if the
2901 // user has pressed a key. Generally we would only sync the cursor position
2902 // when selection is collapsed so that if the user has selected something
2903 // we don't clear the selection by moving the selection. However when a
2904 // screen reader is used, it's intuitive for entering a key to move the
2905 // selection to the cursor.
2906 forceSyncSelection = this.accessibilityReader_.hasUserGesture;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002907 this.accessibilityReader_.afterCursorChange(
2908 cursorLineText, cursorRowIndex, cursorColumnIndex);
2909 }
2910
rginda8ba33642011-12-14 12:31:31 -08002911 if (cursorRowIndex > bottomRowIndex) {
2912 // Cursor is scrolled off screen, move it outside of the visible area.
Mike Frysinger44c32202017-08-05 01:13:09 -04002913 this.setCssVar('cursor-offset-row', '-1');
Raymes Khourye5d48982018-08-02 09:08:32 +10002914 return false;
rginda8ba33642011-12-14 12:31:31 -08002915 }
2916
Robert Gindab837c052014-08-11 11:17:51 -07002917 if (this.options_.cursorVisible &&
2918 this.cursorNode_.style.display == 'none') {
2919 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2920 this.cursorNode_.style.display = '';
2921 }
2922
Mike Frysinger44c32202017-08-05 01:13:09 -04002923 // Position the cursor using CSS variable math. If we do the math in JS,
2924 // the float math will end up being more precise than the CSS which will
2925 // cause the cursor tracking to be off.
2926 this.setCssVar(
2927 'cursor-offset-row',
2928 `${cursorRowIndex - topRowIndex} + ` +
2929 `${this.scrollPort_.visibleRowTopMargin}px`);
2930 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08002931
2932 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04002933 '(' + this.screen_.cursorPosition.column +
2934 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08002935 ')');
2936
2937 // Update the caret for a11y purposes.
2938 var selection = this.document_.getSelection();
Raymes Khoury15697f42018-07-17 11:37:18 +10002939 if (selection && (selection.isCollapsed || forceSyncSelection)) {
rginda87b86462011-12-14 13:48:03 -08002940 this.screen_.syncSelectionCaret(selection);
Raymes Khoury15697f42018-07-17 11:37:18 +10002941 }
Raymes Khourye5d48982018-08-02 09:08:32 +10002942 return true;
rginda8ba33642011-12-14 12:31:31 -08002943};
2944
Robert Gindafb1be6a2013-12-11 11:56:22 -08002945/**
2946 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2947 * and character cell dimensions.
2948 */
Robert Ginda830583c2013-08-07 13:20:46 -07002949hterm.Terminal.prototype.restyleCursor_ = function() {
2950 var shape = this.cursorShape_;
2951
2952 if (this.cursorNode_.getAttribute('focus') == 'false') {
2953 // Always show a block cursor when unfocused.
2954 shape = hterm.Terminal.cursorShape.BLOCK;
2955 }
2956
2957 var style = this.cursorNode_.style;
2958
2959 switch (shape) {
2960 case hterm.Terminal.cursorShape.BEAM:
Robert Ginda830583c2013-08-07 13:20:46 -07002961 style.backgroundColor = 'transparent';
Joel Hockeyd4fca732019-09-20 16:57:03 -07002962 style.borderBottomStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07002963 style.borderLeftStyle = 'solid';
2964 break;
2965
2966 case hterm.Terminal.cursorShape.UNDERLINE:
Robert Ginda830583c2013-08-07 13:20:46 -07002967 style.backgroundColor = 'transparent';
2968 style.borderBottomStyle = 'solid';
Joel Hockeyd4fca732019-09-20 16:57:03 -07002969 style.borderLeftStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07002970 break;
2971
2972 default:
Mike Frysinger2fd079a2018-09-02 01:46:12 -04002973 style.backgroundColor = 'var(--hterm-cursor-color)';
Joel Hockeyd4fca732019-09-20 16:57:03 -07002974 style.borderBottomStyle = '';
2975 style.borderLeftStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07002976 break;
2977 }
2978};
2979
rginda8ba33642011-12-14 12:31:31 -08002980/**
2981 * Synchronizes the visible cursor with the current cursor coordinates.
2982 *
2983 * The sync will happen asynchronously, soon after the call stack winds down.
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002984 * Multiple calls will be coalesced into a single sync. This should be called
2985 * prior to the cursor actually changing position.
rginda8ba33642011-12-14 12:31:31 -08002986 */
2987hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2988 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002989 return;
rginda8ba33642011-12-14 12:31:31 -08002990
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002991 if (this.accessibilityReader_.accessibilityEnabled) {
2992 // Report the previous position of the cursor for accessibility purposes.
2993 const cursorRowIndex = this.scrollbackRows_.length +
2994 this.screen_.cursorPosition.row;
2995 const cursorColumnIndex = this.screen_.cursorPosition.column;
2996 const cursorLineText =
2997 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
2998 this.accessibilityReader_.beforeCursorChange(
2999 cursorLineText, cursorRowIndex, cursorColumnIndex);
3000 }
3001
rginda8ba33642011-12-14 12:31:31 -08003002 var self = this;
3003 this.timeouts_.syncCursor = setTimeout(function() {
3004 self.syncCursorPosition_();
3005 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08003006 }, 0);
3007};
3008
rgindacc2996c2012-02-24 14:59:31 -08003009/**
rgindaf522ce02012-04-17 17:49:17 -07003010 * Show or hide the zoom warning.
3011 *
3012 * The zoom warning is a message warning the user that their browser zoom must
3013 * be set to 100% in order for hterm to function properly.
3014 *
3015 * @param {boolean} state True to show the message, false to hide it.
3016 */
3017hterm.Terminal.prototype.showZoomWarning_ = function(state) {
3018 if (!this.zoomWarningNode_) {
3019 if (!state)
3020 return;
3021
3022 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04003023 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07003024 this.zoomWarningNode_.style.cssText = (
3025 'color: black;' +
3026 'background-color: #ff2222;' +
3027 'font-size: large;' +
3028 'border-radius: 8px;' +
3029 'opacity: 0.75;' +
3030 'padding: 0.2em 0.5em 0.2em 0.5em;' +
3031 'top: 0.5em;' +
3032 'right: 1.2em;' +
3033 'position: absolute;' +
3034 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003035 '-webkit-user-select: none;' +
3036 '-moz-text-size-adjust: none;' +
3037 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05003038
3039 this.zoomWarningNode_.addEventListener('click', function(e) {
3040 this.parentNode.removeChild(this);
3041 });
rgindaf522ce02012-04-17 17:49:17 -07003042 }
3043
Mike Frysingerb7289952019-03-23 16:05:38 -07003044 this.zoomWarningNode_.textContent = lib.i18n.replaceReferences(
Robert Gindab4839c22013-02-28 16:52:10 -08003045 hterm.zoomWarningMessage,
Joel Hockeyd4fca732019-09-20 16:57:03 -07003046 [Math.floor(this.scrollPort_.characterSize.zoomFactor * 100)]);
Robert Gindab4839c22013-02-28 16:52:10 -08003047
rgindaf522ce02012-04-17 17:49:17 -07003048 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
3049
3050 if (state) {
3051 if (!this.zoomWarningNode_.parentNode)
3052 this.div_.parentNode.appendChild(this.zoomWarningNode_);
3053 } else if (this.zoomWarningNode_.parentNode) {
3054 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
3055 }
3056};
3057
3058/**
rgindacc2996c2012-02-24 14:59:31 -08003059 * Show the terminal overlay for a given amount of time.
3060 *
3061 * The terminal overlay appears in inverse video in a large font, centered
3062 * over the terminal. You should probably keep the overlay message brief,
3063 * since it's in a large font and you probably aren't going to check the size
3064 * of the terminal first.
3065 *
3066 * @param {string} msg The text (not HTML) message to display in the overlay.
Joel Hockey0f933582019-08-27 18:01:51 -07003067 * @param {number=} opt_timeout The amount of time to wait before fading out
rgindacc2996c2012-02-24 14:59:31 -08003068 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
3069 * stay up forever (or until the next overlay).
3070 */
3071hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08003072 if (!this.overlayNode_) {
3073 if (!this.div_)
3074 return;
3075
3076 this.overlayNode_ = this.document_.createElement('div');
3077 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08003078 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08003079 'font-size: xx-large;' +
3080 'opacity: 0.75;' +
3081 'padding: 0.2em 0.5em 0.2em 0.5em;' +
3082 'position: absolute;' +
3083 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003084 '-webkit-transition: opacity 180ms ease-in;' +
3085 '-moz-user-select: none;' +
3086 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08003087
3088 this.overlayNode_.addEventListener('mousedown', function(e) {
3089 e.preventDefault();
3090 e.stopPropagation();
3091 }, true);
rgindaf0090c92012-02-10 14:58:52 -08003092 }
3093
rginda9f5222b2012-03-05 11:53:28 -08003094 this.overlayNode_.style.color = this.prefs_.get('background-color');
3095 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
3096 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
3097
rgindaf0090c92012-02-10 14:58:52 -08003098 this.overlayNode_.textContent = msg;
3099 this.overlayNode_.style.opacity = '0.75';
3100
3101 if (!this.overlayNode_.parentNode)
3102 this.div_.appendChild(this.overlayNode_);
3103
Joel Hockeyd4fca732019-09-20 16:57:03 -07003104 var divSize = hterm.getClientSize(lib.notNull(this.div_));
Robert Ginda97769282013-02-01 15:30:30 -08003105 var overlaySize = hterm.getClientSize(this.overlayNode_);
3106
Robert Ginda8a59f762014-07-23 11:29:55 -07003107 this.overlayNode_.style.top =
3108 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08003109 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07003110 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08003111
rgindaf0090c92012-02-10 14:58:52 -08003112 if (this.overlayTimeout_)
3113 clearTimeout(this.overlayTimeout_);
3114
Raymes Khouryc7a06382018-07-04 10:25:45 +10003115 this.accessibilityReader_.assertiveAnnounce(msg);
3116
rgindacc2996c2012-02-24 14:59:31 -08003117 if (opt_timeout === null)
3118 return;
3119
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003120 this.overlayTimeout_ = setTimeout(() => {
3121 this.overlayNode_.style.opacity = '0';
3122 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
3123 }, opt_timeout || 1500);
3124};
3125
3126/**
3127 * Hide the terminal overlay immediately.
3128 *
3129 * Useful when we show an overlay for an event with an unknown end time.
3130 */
3131hterm.Terminal.prototype.hideOverlay = function() {
3132 if (this.overlayTimeout_)
3133 clearTimeout(this.overlayTimeout_);
3134 this.overlayTimeout_ = null;
3135
3136 if (this.overlayNode_.parentNode)
3137 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
3138 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08003139};
3140
rginda4bba5e12012-06-20 16:15:30 -07003141/**
3142 * Paste from the system clipboard to the terminal.
Mike Frysinger23b5b832019-10-01 17:05:29 -04003143 *
Joel Hockey0f933582019-08-27 18:01:51 -07003144 * @return {boolean}
rginda4bba5e12012-06-20 16:15:30 -07003145 */
3146hterm.Terminal.prototype.paste = function() {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003147 return hterm.pasteFromClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07003148};
3149
3150/**
3151 * Copy a string to the system clipboard.
3152 *
3153 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05003154 *
3155 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07003156 */
3157hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08003158 if (this.prefs_.get('enable-clipboard-notice'))
3159 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
3160
Mike Frysinger96eacae2019-01-02 18:13:56 -05003161 hterm.copySelectionToClipboard(this.document_, str);
rginda4bba5e12012-06-20 16:15:30 -07003162};
3163
Evan Jones2600d4f2016-12-06 09:29:36 -05003164/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003165 * Display an image.
3166 *
Mike Frysinger2558ed52019-01-14 01:03:41 -05003167 * Either URI or buffer or blob fields must be specified.
3168 *
Joel Hockey0f933582019-08-27 18:01:51 -07003169 * @param {{
3170 * name: (string|undefined),
3171 * size: (string|number|undefined),
3172 * preserveAspectRation: (boolean|undefined),
3173 * inline: (boolean|undefined),
3174 * width: (string|number|undefined),
3175 * height: (string|number|undefined),
3176 * align: (string|undefined),
3177 * url: (string|undefined),
3178 * buffer: (!ArrayBuffer|undefined),
3179 * blob: (!Blob|undefined),
3180 * type: (string|undefined),
3181 * }} options The image to display.
3182 * name A human readable string for the image
3183 * size The size (in bytes).
3184 * preserveAspectRatio Whether to preserve aspect.
3185 * inline Whether to display the image inline.
3186 * width The width of the image.
3187 * height The height of the image.
3188 * align Direction to align the image.
3189 * uri The source URI for the image.
3190 * buffer The ArrayBuffer image data.
3191 * blob The Blob image data.
3192 * type The MIME type of the image data.
3193 * @param {function()=} onLoad Callback when loading finishes.
3194 * @param {function(!Event)=} onError Callback when loading fails.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003195 */
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003196hterm.Terminal.prototype.displayImage = function(options, onLoad, onError) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003197 // Make sure we're actually given a resource to display.
Mike Frysinger2558ed52019-01-14 01:03:41 -05003198 if (options.uri === undefined && options.buffer === undefined &&
3199 options.blob === undefined)
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003200 return;
3201
3202 // Set up the defaults to simplify code below.
3203 if (!options.name)
3204 options.name = '';
3205
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003206 // See if the mime type is available. If not, guess from the filename.
3207 // We don't list all possible mime types because the browser can usually
3208 // guess it correctly. So list the ones that need a bit more help.
3209 if (!options.type) {
3210 const ary = options.name.split('.');
3211 const ext = ary[ary.length - 1].trim();
3212 switch (ext) {
3213 case 'svg':
3214 case 'svgz':
3215 options.type = 'image/svg+xml';
3216 break;
3217 }
3218 }
3219
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003220 // Has the user approved image display yet?
3221 if (this.allowImagesInline !== true) {
3222 this.newLine();
3223 const row = this.getRowNode(this.scrollbackRows_.length +
3224 this.getCursorRow() - 1);
3225
3226 if (this.allowImagesInline === false) {
3227 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
3228 'Inline Images Disabled');
3229 return;
3230 }
3231
3232 // Show a prompt.
3233 let button;
3234 const span = this.document_.createElement('span');
3235 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
3236 span.style.fontWeight = 'bold';
3237 span.style.borderWidth = '1px';
3238 span.style.borderStyle = 'dashed';
3239 button = this.document_.createElement('span');
3240 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3241 button.style.marginLeft = '1em';
3242 button.style.borderWidth = '1px';
3243 button.style.borderStyle = 'solid';
3244 button.addEventListener('click', () => {
3245 this.prefs_.set('allow-images-inline', false);
3246 });
3247 span.appendChild(button);
3248 button = this.document_.createElement('span');
3249 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3250 'allow this session');
3251 button.style.marginLeft = '1em';
3252 button.style.borderWidth = '1px';
3253 button.style.borderStyle = 'solid';
3254 button.addEventListener('click', () => {
3255 this.allowImagesInline = true;
3256 });
3257 span.appendChild(button);
3258 button = this.document_.createElement('span');
3259 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3260 button.style.marginLeft = '1em';
3261 button.style.borderWidth = '1px';
3262 button.style.borderStyle = 'solid';
3263 button.addEventListener('click', () => {
3264 this.prefs_.set('allow-images-inline', true);
3265 });
3266 span.appendChild(button);
3267
3268 row.appendChild(span);
3269 return;
3270 }
3271
3272 // See if we should show this object directly, or download it.
3273 if (options.inline) {
3274 const io = this.io.push();
3275 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
Joel Hockeyd4fca732019-09-20 16:57:03 -07003276 'Loading $1 ...'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003277
3278 // While we're loading the image, eat all the user's input.
3279 io.onVTKeystroke = io.sendString = () => {};
3280
3281 // Initialize this new image.
Joel Hockeyd4fca732019-09-20 16:57:03 -07003282 const img = this.document_.createElement('img');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003283 if (options.uri !== undefined) {
3284 img.src = options.uri;
3285 } else if (options.buffer !== undefined) {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003286 const blob = new Blob([options.buffer], {type: options.type});
Mike Frysinger2558ed52019-01-14 01:03:41 -05003287 img.src = URL.createObjectURL(blob);
3288 } else {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003289 const blob = new Blob([options.blob], {type: options.type});
Joel Hockeyd4fca732019-09-20 16:57:03 -07003290 img.src = URL.createObjectURL(blob);
Mike Frysinger2558ed52019-01-14 01:03:41 -05003291 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003292 img.title = img.alt = options.name;
3293
3294 // Attach the image to the page to let it load/render. It won't stay here.
3295 // This is needed so it's visible and the DOM can calculate the height. If
3296 // the image is hidden or not in the DOM, the height is always 0.
3297 this.document_.body.appendChild(img);
3298
3299 // Wait for the image to finish loading before we try moving it to the
3300 // right place in the terminal.
3301 img.onload = () => {
3302 // Now that we have the image dimensions, figure out how to show it.
3303 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
3304 img.style.maxWidth = `${this.document_.body.clientWidth}px`;
3305 img.style.maxHeight = `${this.document_.body.clientHeight}px`;
3306
3307 // Parse a width/height specification.
3308 const parseDim = (dim, maxDim, cssVar) => {
3309 if (!dim || dim == 'auto')
3310 return '';
3311
3312 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3313 if (ary) {
3314 if (ary[2] == '%')
Joel Hockeyd4fca732019-09-20 16:57:03 -07003315 return Math.floor(maxDim * ary[1] / 100) + 'px';
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003316 else if (ary[2] == 'px')
3317 return dim;
3318 else
3319 return `calc(${dim} * var(${cssVar}))`;
3320 }
3321
3322 return '';
3323 };
3324 img.style.width =
3325 parseDim(options.width, this.document_.body.clientWidth,
3326 '--hterm-charsize-width');
3327 img.style.height =
3328 parseDim(options.height, this.document_.body.clientHeight,
3329 '--hterm-charsize-height');
3330
3331 // Figure out how many rows the image occupies, then add that many.
Mike Frysingera0349392019-09-11 05:38:09 -04003332 // Note: This count will be inaccurate if the font size changes on us.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003333 const padRows = Math.ceil(img.clientHeight /
3334 this.scrollPort_.characterSize.height);
3335 for (let i = 0; i < padRows; ++i)
3336 this.newLine();
3337
3338 // Update the max height in case the user shrinks the character size.
3339 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3340
3341 // Move the image to the last row. This way when we scroll up, it doesn't
3342 // disappear when the first row gets clipped. It will disappear when we
3343 // scroll down and the last row is clipped ...
3344 this.document_.body.removeChild(img);
3345 // Create a wrapper node so we can do an absolute in a relative position.
3346 // This helps with rounding errors between JS & CSS counts.
3347 const div = this.document_.createElement('div');
3348 div.style.position = 'relative';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003349 div.style.textAlign = options.align || '';
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003350 img.style.position = 'absolute';
3351 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3352 div.appendChild(img);
3353 const row = this.getRowNode(this.scrollbackRows_.length +
3354 this.getCursorRow() - 1);
3355 row.appendChild(div);
3356
Mike Frysinger2558ed52019-01-14 01:03:41 -05003357 // Now that the image has been read, we can revoke the source.
3358 if (options.uri === undefined) {
3359 URL.revokeObjectURL(img.src);
3360 }
3361
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003362 io.hideOverlay();
3363 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003364
3365 if (onLoad)
3366 onLoad();
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003367 };
3368
3369 // If we got a malformed image, give up.
3370 img.onerror = (e) => {
3371 this.document_.body.removeChild(img);
3372 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
Mike Frysingere14a8c42018-03-10 00:17:30 -08003373 'Loading $1 failed'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003374 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003375
3376 if (onError)
3377 onError(e);
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003378 };
3379 } else {
3380 // We can't use chrome.downloads.download as that requires "downloads"
3381 // permissions, and that works only in extensions, not apps.
3382 const a = this.document_.createElement('a');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003383 if (options.uri !== undefined) {
3384 a.href = options.uri;
3385 } else if (options.buffer !== undefined) {
3386 const blob = new Blob([options.buffer]);
3387 a.href = URL.createObjectURL(blob);
3388 } else {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003389 a.href = URL.createObjectURL(lib.notNull(options.blob));
Mike Frysinger2558ed52019-01-14 01:03:41 -05003390 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003391 a.download = options.name;
3392 this.document_.body.appendChild(a);
3393 a.click();
3394 a.remove();
Mike Frysinger2558ed52019-01-14 01:03:41 -05003395 if (options.uri === undefined) {
3396 URL.revokeObjectURL(a.href);
3397 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003398 }
3399};
3400
3401/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003402 * Returns the selected text, or null if no text is selected.
3403 *
3404 * @return {string|null}
3405 */
rgindaa09e7332012-08-17 12:49:51 -07003406hterm.Terminal.prototype.getSelectionText = function() {
3407 var selection = this.scrollPort_.selection;
3408 selection.sync();
3409
3410 if (selection.isCollapsed)
3411 return null;
3412
rgindaa09e7332012-08-17 12:49:51 -07003413 // Start offset measures from the beginning of the line.
3414 var startOffset = selection.startOffset;
3415 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003416
Raymes Khoury334625a2018-06-25 10:29:40 +10003417 // If an x-row isn't selected, |node| will be null.
3418 if (!node)
3419 return null;
3420
Robert Gindafdbb3f22012-09-06 20:23:06 -07003421 if (node.nodeName != 'X-ROW') {
3422 // If the selection doesn't start on an x-row node, then it must be
3423 // somewhere inside the x-row. Add any characters from previous siblings
3424 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003425
3426 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3427 // If node is the text node in a styled span, move up to the span node.
3428 node = node.parentNode;
3429 }
3430
Robert Gindafdbb3f22012-09-06 20:23:06 -07003431 while (node.previousSibling) {
3432 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003433 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003434 }
rgindaa09e7332012-08-17 12:49:51 -07003435 }
3436
3437 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08003438 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
3439 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003440 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003441
Robert Gindafdbb3f22012-09-06 20:23:06 -07003442 if (node.nodeName != 'X-ROW') {
3443 // If the selection doesn't end on an x-row node, then it must be
3444 // somewhere inside the x-row. Add any characters from following siblings
3445 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003446
3447 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3448 // If node is the text node in a styled span, move up to the span node.
3449 node = node.parentNode;
3450 }
3451
Robert Gindafdbb3f22012-09-06 20:23:06 -07003452 while (node.nextSibling) {
3453 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003454 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003455 }
rgindaa09e7332012-08-17 12:49:51 -07003456 }
3457
3458 var rv = this.getRowsText(selection.startRow.rowIndex,
3459 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003460 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003461};
3462
rginda4bba5e12012-06-20 16:15:30 -07003463/**
3464 * Copy the current selection to the system clipboard, then clear it after a
3465 * short delay.
3466 */
3467hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07003468 var text = this.getSelectionText();
3469 if (text != null)
3470 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07003471};
3472
Joel Hockey0f933582019-08-27 18:01:51 -07003473/**
3474 * Show overlay with current terminal size.
3475 */
rgindaf0090c92012-02-10 14:58:52 -08003476hterm.Terminal.prototype.overlaySize = function() {
Theodore Duboisdd5f9a72019-09-06 23:28:42 -07003477 if (this.prefs_.get('enable-resize-status')) {
3478 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
3479 }
rgindaf0090c92012-02-10 14:58:52 -08003480};
3481
rginda87b86462011-12-14 13:48:03 -08003482/**
3483 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3484 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003485 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003486 */
3487hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08003488 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08003489 this.scrollPort_.scrollRowToBottom(this.getRowCount());
3490
Mike Frysinger225c99d2019-10-20 14:02:37 -06003491 this.pauseCursorBlink_();
3492
Mike Frysinger79669762018-12-30 20:51:10 -05003493 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08003494};
3495
3496/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003497 * Open the selected url.
3498 */
3499hterm.Terminal.prototype.openSelectedUrl_ = function() {
3500 var str = this.getSelectionText();
3501
3502 // If there is no selection, try and expand wherever they clicked.
3503 if (str == null) {
John Lincae9b732018-03-08 13:56:35 +08003504 this.screen_.expandSelectionForUrl(this.document_.getSelection());
Mike Frysinger70b94692017-01-26 18:57:50 -10003505 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003506
3507 // If clicking in empty space, return.
3508 if (str == null)
3509 return;
Mike Frysinger70b94692017-01-26 18:57:50 -10003510 }
3511
3512 // Make sure URL is valid before opening.
3513 if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0)
3514 return;
Mike Frysinger43472622017-06-26 18:11:07 -04003515
3516 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003517 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003518 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3519 // We have to whitelist a few protocols that lack authorities and thus
3520 // never use the //. Like mailto.
3521 switch (str.split(':', 1)[0]) {
3522 case 'mailto':
3523 break;
3524 default:
3525 str = 'http://' + str;
3526 break;
3527 }
3528 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003529
Mike Frysinger720fa832017-10-23 01:15:52 -04003530 hterm.openUrl(str);
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003531};
Mike Frysinger70b94692017-01-26 18:57:50 -10003532
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003533/**
3534 * Manage the automatic mouse hiding behavior while typing.
3535 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003536 * @param {?boolean=} v Whether to enable automatic hiding.
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003537 */
3538hterm.Terminal.prototype.setAutomaticMouseHiding = function(v=null) {
3539 // Since Chrome OS & macOS do this by default everywhere, we don't need to.
3540 // Linux & Windows seem to leave this to specific applications to manage.
3541 if (v === null)
3542 v = (hterm.os != 'cros' && hterm.os != 'mac');
3543
3544 this.mouseHideWhileTyping_ = !!v;
3545};
3546
3547/**
3548 * Handler for monitoring user keyboard activity.
3549 *
3550 * This isn't for processing the keystrokes directly, but for updating any
3551 * state that might toggle based on the user using the keyboard at all.
3552 *
Joel Hockey0f933582019-08-27 18:01:51 -07003553 * @param {!KeyboardEvent} e The keyboard event that triggered us.
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003554 */
3555hterm.Terminal.prototype.onKeyboardActivity_ = function(e) {
3556 // When the user starts typing, hide the mouse cursor.
3557 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_)
3558 this.setCssVar('mouse-cursor-style', 'none');
3559};
Mike Frysinger70b94692017-01-26 18:57:50 -10003560
3561/**
rgindad5613292012-06-19 15:40:37 -07003562 * Add the terminalRow and terminalColumn properties to mouse events and
3563 * then forward on to onMouse().
3564 *
3565 * The terminalRow and terminalColumn properties contain the (row, column)
3566 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003567 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003568 * @param {!MouseEvent} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003569 */
3570hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003571 if (e.processedByTerminalHandler_) {
3572 // We register our event handlers on the document, as well as the cursor
3573 // and the scroll blocker. Mouse events that occur on the cursor or
3574 // scroll blocker will also appear on the document, but we don't want to
3575 // process them twice.
3576 //
3577 // We can't just prevent bubbling because that has other side effects, so
3578 // we decorate the event object with this property instead.
3579 return;
3580 }
3581
Mike Frysinger468966c2018-08-28 13:48:51 -04003582 // Consume navigation events. Button 3 is usually "browser back" and
3583 // button 4 is "browser forward" which we don't want to happen.
3584 if (e.button > 2) {
3585 e.preventDefault();
3586 // We don't return so click events can be passed to the remote below.
3587 }
3588
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003589 var reportMouseEvents = (!this.defeatMouseReports_ &&
3590 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3591
rgindafaa74742012-08-21 13:34:03 -07003592 e.processedByTerminalHandler_ = true;
3593
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003594 // Handle auto hiding of mouse cursor while typing.
3595 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
3596 // Make sure the mouse cursor is visible.
3597 this.syncMouseStyle();
3598 // This debounce isn't perfect, but should work well enough for such a
3599 // simple implementation. If the user moved the mouse, we enabled this
3600 // debounce, and then moved the mouse just before the timeout, we wouldn't
3601 // debounce that later movement.
3602 this.mouseHideDelay_ = setTimeout(() => this.mouseHideDelay_ = null, 1000);
3603 }
3604
Robert Gindaeda48db2014-07-17 09:25:30 -07003605 // One based row/column stored on the mouse event.
Joel Hockeyd4fca732019-09-20 16:57:03 -07003606 e.terminalRow = Math.floor(
3607 (e.clientY - this.scrollPort_.visibleRowTopMargin) /
3608 this.scrollPort_.characterSize.height) + 1;
3609 e.terminalColumn = Math.floor(
3610 e.clientX / this.scrollPort_.characterSize.width) + 1;
Robert Gindaeda48db2014-07-17 09:25:30 -07003611
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003612 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
3613 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07003614 return;
3615 }
3616
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003617 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07003618 // If the cursor is visible and we're not sending mouse events to the
3619 // host app, then we want to hide the terminal cursor when the mouse
3620 // cursor is over top. This keeps the terminal cursor from interfering
3621 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003622 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3623 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3624 this.cursorNode_.style.display = 'none';
3625 } else if (this.cursorNode_.style.display == 'none') {
3626 this.cursorNode_.style.display = '';
3627 }
3628 }
rgindad5613292012-06-19 15:40:37 -07003629
Robert Ginda928cf632014-03-05 15:07:41 -08003630 if (e.type == 'mousedown') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003631 this.contextMenu.hide();
Mike Frysingercc114512017-09-11 21:39:17 -04003632
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003633 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003634 // If VT mouse reporting is disabled, or has been defeated with
3635 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003636 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003637 this.setSelectionEnabled(true);
3638 } else {
3639 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003640 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003641 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003642 this.setSelectionEnabled(false);
3643 e.preventDefault();
3644 }
3645 }
3646
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003647 if (!reportMouseEvents) {
John Lin2aad22e2018-03-16 13:58:11 +08003648 if (e.type == 'dblclick') {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003649 this.screen_.expandSelection(this.document_.getSelection());
John Lin2aad22e2018-03-16 13:58:11 +08003650 if (this.copyOnSelect)
Mike Frysinger406c76c2019-01-02 17:53:51 -05003651 this.copySelectionToClipboard();
rgindad5613292012-06-19 15:40:37 -07003652 }
3653
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003654 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003655 // Debounce this event with the dblclick event. If you try to doubleclick
3656 // a URL to open it, Chrome will fire click then dblclick, but we won't
3657 // have expanded the selection text at the first click event.
3658 clearTimeout(this.timeouts_.openUrl);
3659 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3660 500);
3661 return;
3662 }
3663
Mike Frysinger847577f2017-05-23 23:25:57 -04003664 if (e.type == 'mousedown') {
Mike Frysingercc114512017-09-11 21:39:17 -04003665 if (e.ctrlKey && e.button == 2 /* right button */) {
3666 e.preventDefault();
3667 this.contextMenu.show(e, this);
3668 } else if (e.button == this.mousePasteButton ||
3669 (this.mouseRightClickPaste && e.button == 2 /* right button */)) {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003670 if (!this.paste())
Mike Frysinger05a57f02017-08-27 17:48:55 -04003671 console.warn('Could not paste manually due to web restrictions');
Mike Frysinger847577f2017-05-23 23:25:57 -04003672 }
3673 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003674
Mike Frysinger2edd3612017-05-24 00:54:39 -04003675 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003676 !this.document_.getSelection().isCollapsed) {
Mike Frysinger406c76c2019-01-02 17:53:51 -05003677 this.copySelectionToClipboard();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003678 }
3679
3680 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3681 this.scrollBlockerNode_.engaged) {
3682 // Disengage the scroll-blocker after one of these events.
3683 this.scrollBlockerNode_.engaged = false;
3684 this.scrollBlockerNode_.style.top = '-99px';
3685 }
3686
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003687 // Emulate arrow key presses via scroll wheel events.
3688 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3689 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003690 if (e.type == 'wheel') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003691 const delta =
3692 this.scrollPort_.scrollWheelDelta(/** @type {!WheelEvent} */ (e));
Mike Frysingerc3030a82017-05-29 14:16:11 -04003693
Mike Frysinger321063c2018-08-29 15:33:14 -04003694 // Helper to turn a wheel event delta into a series of key presses.
3695 const deltaToArrows = (distance, charSize, arrowPos, arrowNeg) => {
3696 if (distance == 0) {
3697 return '';
3698 }
3699
3700 // Convert the scroll distance into a number of rows/cols.
3701 const cells = lib.f.smartFloorDivide(Math.abs(distance), charSize);
3702 const data = '\x1bO' + (distance < 0 ? arrowNeg : arrowPos);
3703 return data.repeat(cells);
3704 };
3705
3706 // The order between up/down and left/right doesn't really matter.
3707 this.io.sendString(
3708 // Up/down arrow keys.
3709 deltaToArrows(delta.y, this.scrollPort_.characterSize.height,
3710 'A', 'B') +
3711 // Left/right arrow keys.
3712 deltaToArrows(delta.x, this.scrollPort_.characterSize.width,
3713 'C', 'D')
3714 );
Mike Frysingerc3030a82017-05-29 14:16:11 -04003715
3716 e.preventDefault();
3717 }
3718 }
Robert Ginda928cf632014-03-05 15:07:41 -08003719 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003720 if (!this.scrollBlockerNode_.engaged) {
3721 if (e.type == 'mousedown') {
3722 // Move the scroll-blocker into place if we want to keep the scrollport
3723 // from scrolling.
3724 this.scrollBlockerNode_.engaged = true;
3725 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3726 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3727 } else if (e.type == 'mousemove') {
3728 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3729 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003730 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003731 e.preventDefault();
3732 }
3733 }
Robert Ginda928cf632014-03-05 15:07:41 -08003734
3735 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003736 }
3737
Robert Ginda928cf632014-03-05 15:07:41 -08003738 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3739 // Restore this on mouseup in case it was temporarily defeated with a
3740 // alt-mousedown. Only do this when the selection is empty so that
3741 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003742 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003743 }
rgindad5613292012-06-19 15:40:37 -07003744};
3745
3746/**
3747 * Clients should override this if they care to know about mouse events.
3748 *
3749 * The event parameter will be a normal DOM mouse click event with additional
3750 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003751 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003752 * @param {!MouseEvent} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003753 */
3754hterm.Terminal.prototype.onMouse = function(e) { };
3755
3756/**
rginda8e92a692012-05-20 19:37:20 -07003757 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003758 *
3759 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003760 */
Rob Spies06533ba2014-04-24 11:20:37 -07003761hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3762 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003763 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003764
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003765 if (this.reportFocus)
3766 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O');
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003767
Michael Kelly485ecd12014-06-09 11:41:56 -04003768 if (focused === true)
3769 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07003770};
3771
3772/**
rginda8ba33642011-12-14 12:31:31 -08003773 * React when the ScrollPort is scrolled.
3774 */
3775hterm.Terminal.prototype.onScroll_ = function() {
3776 this.scheduleSyncCursorPosition_();
3777};
3778
3779/**
rginda9846e2f2012-01-27 13:53:33 -08003780 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003781 *
Joel Hockeye25ce432019-09-25 19:12:28 -07003782 * @param {{text: string}} e The text of the paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08003783 */
3784hterm.Terminal.prototype.onPaste_ = function(e) {
Joel Hockeye25ce432019-09-25 19:12:28 -07003785 var data = e.text.replace(/\n/mg, '\r');
Mike Frysingere8c32c82018-03-11 14:57:28 -07003786 if (this.options_.bracketedPaste) {
3787 // We strip out most escape sequences as they can cause issues (like
3788 // inserting an \x1b[201~ midstream). We pass through whitespace
3789 // though: 0x08:\b 0x09:\t 0x0a:\n 0x0d:\r.
3790 // This matches xterm behavior.
3791 const filter = (data) => data.replace(/[\x00-\x07\x0b-\x0c\x0e-\x1f]/g, '');
3792 data = '\x1b[200~' + filter(data) + '\x1b[201~';
3793 }
Robert Gindaa063b202014-07-21 11:08:25 -07003794
3795 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003796};
3797
3798/**
rgindaa09e7332012-08-17 12:49:51 -07003799 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003800 *
Joel Hockey0f933582019-08-27 18:01:51 -07003801 * @param {!Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07003802 */
3803hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07003804 if (!this.useDefaultWindowCopy) {
3805 e.preventDefault();
3806 setTimeout(this.copySelectionToClipboard.bind(this), 0);
3807 }
rgindaa09e7332012-08-17 12:49:51 -07003808};
3809
3810/**
rginda8ba33642011-12-14 12:31:31 -08003811 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08003812 *
3813 * Note: This function should not directly contain code that alters the internal
3814 * state of the terminal. That kind of code belongs in realizeWidth or
3815 * realizeHeight, so that it can be executed synchronously in the case of a
3816 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08003817 */
3818hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08003819 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07003820 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08003821 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07003822 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08003823
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003824 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08003825 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003826 // gets removed from the document or during the initial load, and we can't
3827 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07003828 // This can also happen if called before the scrollPort calculates the
3829 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08003830 return;
3831 }
3832
rgindaa8ba17d2012-08-15 14:41:10 -07003833 var isNewSize = (columnCount != this.screenSize.width ||
3834 rowCount != this.screenSize.height);
Theodore Dubois651b0842019-09-07 14:32:09 -07003835 const wasScrolledEnd = this.scrollPort_.isScrolledEnd;
rgindaa8ba17d2012-08-15 14:41:10 -07003836
3837 // We do this even if the size didn't change, just to be sure everything is
3838 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04003839 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07003840 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07003841
3842 if (isNewSize)
3843 this.overlaySize();
3844
Robert Gindafb1be6a2013-12-11 11:56:22 -08003845 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07003846 this.scheduleSyncCursorPosition_();
Theodore Dubois651b0842019-09-07 14:32:09 -07003847
3848 if (wasScrolledEnd) {
3849 this.scrollEnd();
3850 }
rginda8ba33642011-12-14 12:31:31 -08003851};
3852
3853/**
3854 * Service the cursor blink timeout.
3855 */
3856hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07003857 if (!this.options_.cursorBlink) {
3858 delete this.timeouts_.cursorBlink;
3859 return;
3860 }
3861
Robert Ginda830583c2013-08-07 13:20:46 -07003862 if (this.cursorNode_.getAttribute('focus') == 'false' ||
Mike Frysinger225c99d2019-10-20 14:02:37 -06003863 this.cursorNode_.style.opacity == '0' ||
3864 this.cursorBlinkPause_) {
rginda87b86462011-12-14 13:48:03 -08003865 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07003866 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3867 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08003868 } else {
rginda87b86462011-12-14 13:48:03 -08003869 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07003870 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3871 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08003872 }
3873};
David Reveman8f552492012-03-28 12:18:41 -04003874
3875/**
3876 * Set the scrollbar-visible mode bit.
3877 *
3878 * If scrollbar-visible is on, the vertical scrollbar will be visible.
3879 * Otherwise it will not.
3880 *
3881 * Defaults to on.
3882 *
3883 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
3884 */
3885hterm.Terminal.prototype.setScrollbarVisible = function(state) {
3886 this.scrollPort_.setScrollbarVisible(state);
3887};
Michael Kelly485ecd12014-06-09 11:41:56 -04003888
3889/**
Rob Spies49039e52014-12-17 13:40:04 -08003890 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04003891 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08003892 *
3893 * Defaults to 1.
3894 *
Evan Jones2600d4f2016-12-06 09:29:36 -05003895 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08003896 */
3897hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3898 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3899};
3900
3901/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003902 * Close all web notifications created by terminal bells.
3903 */
3904hterm.Terminal.prototype.closeBellNotifications_ = function() {
3905 this.bellNotificationList_.forEach(function(n) {
3906 n.close();
3907 });
3908 this.bellNotificationList_.length = 0;
3909};
Raymes Khourye5d48982018-08-02 09:08:32 +10003910
3911/**
3912 * Syncs the cursor position when the scrollport gains focus.
3913 */
3914hterm.Terminal.prototype.onScrollportFocus_ = function() {
3915 // If the cursor is offscreen we set selection to the last row on the screen.
3916 const topRowIndex = this.scrollPort_.getTopRowIndex();
3917 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
3918 const selection = this.document_.getSelection();
3919 if (!this.syncCursorPosition_() && selection) {
3920 selection.collapse(this.getRowNode(bottomRowIndex));
3921 }
3922};