blob: 1565ea6ae91c1525b4b026cb3a286ae7369efc2a [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 Hockey0f933582019-08-27 18:01:51 -070024 * @param {string=} opt_profileId Optional preference profile name. If not
rginda9f5222b2012-03-05 11:53:28 -080025 * provided, 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 */
Robert Ginda57f03b42012-09-13 11:02:48 -070029hterm.Terminal = function(opt_profileId) {
30 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
88 // Pre-bound onCursorBlink_ handler, so we don't have to do this for each
89 // cursor on/off servicing.
90 this.myOnCursorBlink_ = this.onCursorBlink_.bind(this);
91
rginda9f5222b2012-03-05 11:53:28 -080092 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -070093 // each output and keystroke. They are initialized by the preference manager.
Robert Ginda8cb7d902013-06-20 14:37:18 -070094 this.backgroundColor_ = null;
95 this.foregroundColor_ = null;
Robert Ginda57f03b42012-09-13 11:02:48 -070096 this.scrollOnOutput_ = null;
97 this.scrollOnKeystroke_ = null;
Mike Frysinger3c9fa072017-07-13 10:21:13 -040098 this.scrollWheelArrowKeys_ = null;
rginda9f5222b2012-03-05 11:53:28 -080099
Robert Ginda6aec7eb2015-06-16 10:31:30 -0700100 // True if we should override mouse event reporting to allow local selection.
101 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -0800102
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400103 // Whether to auto hide the mouse cursor when typing.
104 this.setAutomaticMouseHiding();
105 // Timer to keep mouse visible while it's being used.
106 this.mouseHideDelay_ = null;
107
rgindaf0090c92012-02-10 14:58:52 -0800108 // Terminal bell sound.
109 this.bellAudio_ = this.document_.createElement('audio');
Mike Frysingerd826f1a2017-07-06 16:20:06 -0400110 this.bellAudio_.id = 'hterm:bell-audio';
rgindaf0090c92012-02-10 14:58:52 -0800111 this.bellAudio_.setAttribute('preload', 'auto');
112
Raymes Khoury3e44bc92018-05-17 10:54:23 +1000113 // The AccessibilityReader object for announcing command output.
114 this.accessibilityReader_ = null;
115
Mike Frysingercc114512017-09-11 21:39:17 -0400116 // The context menu object.
117 this.contextMenu = new hterm.ContextMenu();
118
Michael Kelly485ecd12014-06-09 11:41:56 -0400119 // All terminal bell notifications that have been generated (not necessarily
120 // shown).
121 this.bellNotificationList_ = [];
Joel Hockeyd4fca732019-09-20 16:57:03 -0700122 this.bellSquelchTimeout_ = null;
Michael Kelly485ecd12014-06-09 11:41:56 -0400123
124 // Whether we have permission to display notifications.
125 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400126
rginda6d397402012-01-17 10:58:29 -0800127 // Cursor position and attributes saved with DECSC.
128 this.savedOptions_ = {};
129
rginda8ba33642011-12-14 12:31:31 -0800130 // The current mode bits for the terminal.
131 this.options_ = new hterm.Options();
132
133 // Timeouts we might need to clear.
134 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800135
136 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800137 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800138
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800139 this.saveCursorAndState(true);
140
Zhu Qunying30d40712017-03-14 16:27:00 -0700141 // The keyboard handler.
rgindafeaf3142012-01-31 15:14:20 -0800142 this.keyboard = new hterm.Keyboard(this);
143
rginda87b86462011-12-14 13:48:03 -0800144 // General IO interface that can be given to third parties without exposing
145 // the entire terminal object.
146 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800147
rgindad5613292012-06-19 15:40:37 -0700148 // True if mouse-click-drag should scroll the terminal.
149 this.enableMouseDragScroll = true;
150
Robert Ginda57f03b42012-09-13 11:02:48 -0700151 this.copyOnSelect = null;
Mike Frysinger847577f2017-05-23 23:25:57 -0400152 this.mouseRightClickPaste = null;
rginda4bba5e12012-06-20 16:15:30 -0700153 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700154
Zhu Qunying30d40712017-03-14 16:27:00 -0700155 // Whether to use the default window copy behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700156 this.useDefaultWindowCopy = false;
157
158 this.clearSelectionAfterCopy = true;
159
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400160 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800161 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700162
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400163 // Whether we allow images to be shown.
164 this.allowImagesInline = null;
165
Gabriel Holodake8a09be2017-10-10 01:07:11 -0400166 this.reportFocus = false;
167
Robert Ginda57f03b42012-09-13 11:02:48 -0700168 this.setProfile(opt_profileId || 'default',
Evan Jones5f9df812016-12-06 09:38:58 -0500169 function() { this.onTerminalReady(); }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800170};
171
172/**
Robert Ginda830583c2013-08-07 13:20:46 -0700173 * Possible cursor shapes.
174 */
175hterm.Terminal.cursorShape = {
176 BLOCK: 'BLOCK',
177 BEAM: 'BEAM',
178 UNDERLINE: 'UNDERLINE'
179};
180
181/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700182 * Clients should override this to be notified when the terminal is ready
183 * for use.
184 *
185 * The terminal initialization is asynchronous, and shouldn't be used before
186 * this method is called.
187 */
188hterm.Terminal.prototype.onTerminalReady = function() { };
189
190/**
rginda35c456b2012-02-09 17:29:05 -0800191 * Default tab with of 8 to match xterm.
192 */
193hterm.Terminal.prototype.tabWidth = 8;
194
195/**
rginda9f5222b2012-03-05 11:53:28 -0800196 * Select a preference profile.
197 *
198 * This will load the terminal preferences for the given profile name and
199 * associate subsequent preference changes with the new preference profile.
200 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500201 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800202 * characters will be removed from the name.
Joel Hockey0f933582019-08-27 18:01:51 -0700203 * @param {function()=} opt_callback Optional callback to invoke when the
204 * profile transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800205 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700206hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
207 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800208
Robert Ginda57f03b42012-09-13 11:02:48 -0700209 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800210
Robert Ginda57f03b42012-09-13 11:02:48 -0700211 if (this.prefs_)
212 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800213
Robert Ginda57f03b42012-09-13 11:02:48 -0700214 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
215 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800216 'alt-gr-mode': function(v) {
217 if (v == null) {
218 if (navigator.language.toLowerCase() == 'en-us') {
219 v = 'none';
220 } else {
221 v = 'right-alt';
222 }
223 } else if (typeof v == 'string') {
224 v = v.toLowerCase();
225 } else {
226 v = 'none';
227 }
228
229 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v))
230 v = 'none';
231
232 terminal.keyboard.altGrMode = v;
233 },
234
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700235 'alt-backspace-is-meta-backspace': function(v) {
236 terminal.keyboard.altBackspaceIsMetaBackspace = v;
237 },
238
Robert Ginda57f03b42012-09-13 11:02:48 -0700239 'alt-is-meta': function(v) {
240 terminal.keyboard.altIsMeta = v;
241 },
242
243 'alt-sends-what': function(v) {
244 if (!/^(escape|8-bit|browser-key)$/.test(v))
245 v = 'escape';
246
247 terminal.keyboard.altSendsWhat = v;
248 },
249
250 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800251 var ary = v.match(/^lib-resource:(\S+)/);
252 if (ary) {
253 terminal.bellAudio_.setAttribute('src',
254 lib.resource.getDataUrl(ary[1]));
255 } else {
256 terminal.bellAudio_.setAttribute('src', v);
257 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700258 },
259
Michael Kelly485ecd12014-06-09 11:41:56 -0400260 'desktop-notification-bell': function(v) {
261 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700262 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400263 Notification.permission === 'granted';
264 if (!terminal.desktopNotificationBell_) {
265 // Note: We don't call Notification.requestPermission here because
266 // Chrome requires the call be the result of a user action (such as an
267 // onclick handler), and pref listeners are run asynchronously.
268 //
269 // A way of working around this would be to display a dialog in the
270 // terminal with a "click-to-request-permission" button.
271 console.warn('desktop-notification-bell is true but we do not have ' +
272 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400273 }
274 } else {
275 terminal.desktopNotificationBell_ = false;
276 }
277 },
278
Robert Ginda57f03b42012-09-13 11:02:48 -0700279 'background-color': function(v) {
280 terminal.setBackgroundColor(v);
281 },
282
283 'background-image': function(v) {
284 terminal.scrollPort_.setBackgroundImage(v);
285 },
286
287 'background-size': function(v) {
288 terminal.scrollPort_.setBackgroundSize(v);
289 },
290
291 'background-position': function(v) {
292 terminal.scrollPort_.setBackgroundPosition(v);
293 },
294
295 'backspace-sends-backspace': function(v) {
296 terminal.keyboard.backspaceSendsBackspace = v;
297 },
298
Brad Town18654b62015-03-12 00:27:45 -0700299 'character-map-overrides': function(v) {
300 if (!(v == null || v instanceof Object)) {
301 console.warn('Preference character-map-modifications is not an ' +
302 'object: ' + v);
303 return;
304 }
305
Mike Frysinger095d4062017-06-14 00:29:48 -0700306 terminal.vt.characterMaps.reset();
307 terminal.vt.characterMaps.setOverrides(v);
Brad Town18654b62015-03-12 00:27:45 -0700308 },
309
Robert Ginda57f03b42012-09-13 11:02:48 -0700310 'cursor-blink': function(v) {
311 terminal.setCursorBlink(!!v);
312 },
313
Joel Hockey9d10ba12019-05-28 01:25:02 -0700314 'cursor-shape': function(v) {
315 terminal.setCursorShape(v);
316 },
317
Robert Gindaea2183e2014-07-17 09:51:51 -0700318 'cursor-blink-cycle': function(v) {
319 if (v instanceof Array &&
320 typeof v[0] == 'number' &&
321 typeof v[1] == 'number') {
322 terminal.cursorBlinkCycle_ = v;
323 } else if (typeof v == 'number') {
324 terminal.cursorBlinkCycle_ = [v, v];
325 } else {
326 // Fast blink indicates an error.
327 terminal.cursorBlinkCycle_ = [100, 100];
328 }
329 },
330
Robert Ginda57f03b42012-09-13 11:02:48 -0700331 'cursor-color': function(v) {
332 terminal.setCursorColor(v);
333 },
334
335 'color-palette-overrides': function(v) {
336 if (!(v == null || v instanceof Object || v instanceof Array)) {
337 console.warn('Preference color-palette-overrides is not an array or ' +
338 'object: ' + v);
339 return;
rginda9f5222b2012-03-05 11:53:28 -0800340 }
rginda9f5222b2012-03-05 11:53:28 -0800341
Robert Ginda57f03b42012-09-13 11:02:48 -0700342 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700343
Robert Ginda57f03b42012-09-13 11:02:48 -0700344 if (v) {
345 for (var key in v) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700346 var i = parseInt(key, 10);
Robert Ginda57f03b42012-09-13 11:02:48 -0700347 if (isNaN(i) || i < 0 || i > 255) {
348 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
349 continue;
350 }
351
352 if (v[i]) {
353 var rgb = lib.colors.normalizeCSS(v[i]);
354 if (rgb)
355 lib.colors.colorPalette[i] = rgb;
356 }
357 }
rginda30f20f62012-04-05 16:36:19 -0700358 }
rginda30f20f62012-04-05 16:36:19 -0700359
Evan Jones5f9df812016-12-06 09:38:58 -0500360 terminal.primaryScreen_.textAttributes.resetColorPalette();
Robert Ginda57f03b42012-09-13 11:02:48 -0700361 terminal.alternateScreen_.textAttributes.resetColorPalette();
362 },
rginda30f20f62012-04-05 16:36:19 -0700363
Robert Ginda57f03b42012-09-13 11:02:48 -0700364 'copy-on-select': function(v) {
365 terminal.copyOnSelect = !!v;
366 },
rginda9f5222b2012-03-05 11:53:28 -0800367
Rob Spies0bec09b2014-06-06 15:58:09 -0700368 'use-default-window-copy': function(v) {
369 terminal.useDefaultWindowCopy = !!v;
370 },
371
372 'clear-selection-after-copy': function(v) {
373 terminal.clearSelectionAfterCopy = !!v;
374 },
375
Robert Ginda7e5e9522014-03-14 12:23:58 -0700376 'ctrl-plus-minus-zero-zoom': function(v) {
377 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
378 },
379
Robert Gindafb5a3f92014-05-13 14:12:00 -0700380 'ctrl-c-copy': function(v) {
381 terminal.keyboard.ctrlCCopy = v;
382 },
383
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100384 'ctrl-v-paste': function(v) {
385 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700386 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100387 },
388
Cody Coljee-Gray7c6a0392018-10-25 13:18:28 -0700389 'paste-on-drop': function(v) {
390 terminal.scrollPort_.setPasteOnDrop(v);
391 },
392
Masaya Suzuki273aa982014-05-31 07:25:55 +0900393 'east-asian-ambiguous-as-two-column': function(v) {
394 lib.wc.regardCjkAmbiguous = v;
395 },
396
Robert Ginda57f03b42012-09-13 11:02:48 -0700397 'enable-8-bit-control': function(v) {
398 terminal.vt.enable8BitControl = !!v;
399 },
rginda30f20f62012-04-05 16:36:19 -0700400
Robert Ginda57f03b42012-09-13 11:02:48 -0700401 'enable-bold': function(v) {
402 terminal.syncBoldSafeState();
403 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400404
Robert Ginda3e278d72014-03-25 13:18:51 -0700405 'enable-bold-as-bright': function(v) {
406 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
407 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
408 },
409
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400410 'enable-blink': function(v) {
Mike Frysinger261597c2017-12-28 01:14:21 -0500411 terminal.setTextBlink(!!v);
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400412 },
413
Robert Ginda57f03b42012-09-13 11:02:48 -0700414 'enable-clipboard-write': function(v) {
415 terminal.vt.enableClipboardWrite = !!v;
416 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400417
Robert Ginda3755e752013-05-31 13:34:09 -0700418 'enable-dec12': function(v) {
419 terminal.vt.enableDec12 = !!v;
420 },
421
Mike Frysinger38f267d2018-09-07 02:50:59 -0400422 'enable-csi-j-3': function(v) {
423 terminal.vt.enableCsiJ3 = !!v;
424 },
425
Robert Ginda57f03b42012-09-13 11:02:48 -0700426 'font-family': function(v) {
427 terminal.syncFontFamily();
428 },
rginda30f20f62012-04-05 16:36:19 -0700429
Robert Ginda57f03b42012-09-13 11:02:48 -0700430 'font-size': function(v) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700431 v = parseInt(v, 10);
Mike Frysinger47853ac2017-12-14 00:44:10 -0500432 if (v <= 0) {
433 console.error(`Invalid font size: ${v}`);
434 return;
435 }
436
Robert Ginda57f03b42012-09-13 11:02:48 -0700437 terminal.setFontSize(v);
438 },
rginda9875d902012-08-20 16:21:57 -0700439
Robert Ginda57f03b42012-09-13 11:02:48 -0700440 'font-smoothing': function(v) {
441 terminal.syncFontFamily();
442 },
rgindade84e382012-04-20 15:39:31 -0700443
Robert Ginda57f03b42012-09-13 11:02:48 -0700444 'foreground-color': function(v) {
445 terminal.setForegroundColor(v);
446 },
rginda30f20f62012-04-05 16:36:19 -0700447
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400448 'hide-mouse-while-typing': function(v) {
449 terminal.setAutomaticMouseHiding(v);
450 },
451
Robert Ginda57f03b42012-09-13 11:02:48 -0700452 'home-keys-scroll': function(v) {
453 terminal.keyboard.homeKeysScroll = v;
454 },
rginda4bba5e12012-06-20 16:15:30 -0700455
Robert Gindaa8165692015-06-15 14:46:31 -0700456 'keybindings': function(v) {
457 terminal.keyboard.bindings.clear();
458
459 if (!v)
460 return;
461
462 if (!(v instanceof Object)) {
463 console.error('Error in keybindings preference: Expected object');
464 return;
465 }
466
467 try {
468 terminal.keyboard.bindings.addBindings(v);
469 } catch (ex) {
470 console.error('Error in keybindings preference: ' + ex);
471 }
472 },
473
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700474 'media-keys-are-fkeys': function(v) {
475 terminal.keyboard.mediaKeysAreFKeys = v;
476 },
477
Robert Ginda57f03b42012-09-13 11:02:48 -0700478 'meta-sends-escape': function(v) {
479 terminal.keyboard.metaSendsEscape = v;
480 },
rginda30f20f62012-04-05 16:36:19 -0700481
Mike Frysinger847577f2017-05-23 23:25:57 -0400482 'mouse-right-click-paste': function(v) {
483 terminal.mouseRightClickPaste = v;
484 },
485
Robert Ginda57f03b42012-09-13 11:02:48 -0700486 'mouse-paste-button': function(v) {
487 terminal.syncMousePasteButton();
488 },
rgindaa8ba17d2012-08-15 14:41:10 -0700489
Robert Gindae76aa9f2014-03-14 12:29:12 -0700490 'page-keys-scroll': function(v) {
491 terminal.keyboard.pageKeysScroll = v;
492 },
493
Robert Ginda40932892012-12-10 17:26:40 -0800494 'pass-alt-number': function(v) {
495 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800496 // Let Alt-1..9 pass to the browser (to control tab switching) on
497 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500498 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800499 }
500
501 terminal.passAltNumber = v;
502 },
503
504 'pass-ctrl-number': function(v) {
505 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800506 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
507 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500508 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800509 }
510
511 terminal.passCtrlNumber = v;
512 },
513
514 'pass-meta-number': function(v) {
515 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800516 // Let Meta-1..9 pass to the browser (to control tab switching) on
517 // OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500518 v = (hterm.os == 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800519 }
520
521 terminal.passMetaNumber = v;
522 },
523
Marius Schilder77857b32014-05-14 16:21:26 -0700524 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700525 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700526 },
527
Robert Ginda8cb7d902013-06-20 14:37:18 -0700528 'receive-encoding': function(v) {
529 if (!(/^(utf-8|raw)$/).test(v)) {
530 console.warn('Invalid value for "receive-encoding": ' + v);
531 v = 'utf-8';
532 }
533
534 terminal.vt.characterEncoding = v;
535 },
536
Robert Ginda57f03b42012-09-13 11:02:48 -0700537 'scroll-on-keystroke': function(v) {
538 terminal.scrollOnKeystroke_ = v;
539 },
rginda9f5222b2012-03-05 11:53:28 -0800540
Robert Ginda57f03b42012-09-13 11:02:48 -0700541 'scroll-on-output': function(v) {
542 terminal.scrollOnOutput_ = v;
543 },
rginda30f20f62012-04-05 16:36:19 -0700544
Robert Ginda57f03b42012-09-13 11:02:48 -0700545 'scrollbar-visible': function(v) {
546 terminal.setScrollbarVisible(v);
547 },
rginda9f5222b2012-03-05 11:53:28 -0800548
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400549 'scroll-wheel-may-send-arrow-keys': function(v) {
550 terminal.scrollWheelArrowKeys_ = v;
551 },
552
Rob Spies49039e52014-12-17 13:40:04 -0800553 'scroll-wheel-move-multiplier': function(v) {
554 terminal.setScrollWheelMoveMultipler(v);
555 },
556
Robert Ginda57f03b42012-09-13 11:02:48 -0700557 'shift-insert-paste': function(v) {
558 terminal.keyboard.shiftInsertPaste = v;
559 },
rginda9f5222b2012-03-05 11:53:28 -0800560
Mike Frysingera7768922017-07-28 15:00:12 -0400561 'terminal-encoding': function(v) {
Mike Frysingera1371e12017-08-17 01:37:17 -0400562 terminal.vt.setEncoding(v);
Mike Frysingera7768922017-07-28 15:00:12 -0400563 },
564
Robert Gindae76aa9f2014-03-14 12:29:12 -0700565 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400566 terminal.scrollPort_.setUserCssUrl(v);
567 },
568
569 'user-css-text': function(v) {
570 terminal.scrollPort_.setUserCssText(v);
571 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400572
573 'word-break-match-left': function(v) {
574 terminal.primaryScreen_.wordBreakMatchLeft = v;
575 terminal.alternateScreen_.wordBreakMatchLeft = v;
576 },
577
578 'word-break-match-right': function(v) {
579 terminal.primaryScreen_.wordBreakMatchRight = v;
580 terminal.alternateScreen_.wordBreakMatchRight = v;
581 },
582
583 'word-break-match-middle': function(v) {
584 terminal.primaryScreen_.wordBreakMatchMiddle = v;
585 terminal.alternateScreen_.wordBreakMatchMiddle = v;
586 },
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400587
588 'allow-images-inline': function(v) {
589 terminal.allowImagesInline = v;
590 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700591 });
rginda30f20f62012-04-05 16:36:19 -0700592
Robert Ginda57f03b42012-09-13 11:02:48 -0700593 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800594 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700595
596 if (opt_callback)
597 opt_callback();
598 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800599};
600
Rob Spies56953412014-04-28 14:09:47 -0700601/**
602 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500603 *
Joel Hockey0f933582019-08-27 18:01:51 -0700604 * @return {!hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700605 */
606hterm.Terminal.prototype.getPrefs = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700607 return lib.notNull(this.prefs_);
Rob Spies56953412014-04-28 14:09:47 -0700608};
609
Robert Gindaa063b202014-07-21 11:08:25 -0700610/**
611 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500612 *
613 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700614 */
615hterm.Terminal.prototype.setBracketedPaste = function(state) {
616 this.options_.bracketedPaste = state;
617};
Rob Spies56953412014-04-28 14:09:47 -0700618
rginda8e92a692012-05-20 19:37:20 -0700619/**
620 * Set the color for the cursor.
621 *
622 * If you want this setting to persist, set it through prefs_, rather than
623 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500624 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500625 * @param {string=} color The color to set. If not defined, we reset to the
626 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700627 */
628hterm.Terminal.prototype.setCursorColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500629 if (color === undefined)
Joel Hockeyd4fca732019-09-20 16:57:03 -0700630 color = this.prefs_.getString('cursor-color');
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500631
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400632 this.setCssVar('cursor-color', color);
rginda8e92a692012-05-20 19:37:20 -0700633};
634
635/**
636 * Return the current cursor color as a string.
Evan Jones2600d4f2016-12-06 09:29:36 -0500637 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700638 */
639hterm.Terminal.prototype.getCursorColor = function() {
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400640 return this.getCssVar('cursor-color');
rginda8e92a692012-05-20 19:37:20 -0700641};
642
643/**
rgindad5613292012-06-19 15:40:37 -0700644 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500645 *
646 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700647 */
648hterm.Terminal.prototype.setSelectionEnabled = function(state) {
649 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700650};
651
652/**
rginda8e92a692012-05-20 19:37:20 -0700653 * Set the background color.
654 *
655 * If you want this setting to persist, set it through prefs_, rather than
656 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500657 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500658 * @param {string=} color The color to set. If not defined, we reset to the
659 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700660 */
661hterm.Terminal.prototype.setBackgroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500662 if (color === undefined)
Joel Hockeyd4fca732019-09-20 16:57:03 -0700663 color = this.prefs_.getString('background-color');
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500664
rgindacbbd7482012-06-13 15:06:16 -0700665 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700666 this.primaryScreen_.textAttributes.setDefaults(
667 this.foregroundColor_, this.backgroundColor_);
668 this.alternateScreen_.textAttributes.setDefaults(
669 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700670 this.scrollPort_.setBackgroundColor(color);
671};
672
rginda9f5222b2012-03-05 11:53:28 -0800673/**
674 * Return the current terminal background color.
675 *
676 * Intended for use by other classes, so we don't have to expose the entire
677 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500678 *
679 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800680 */
681hterm.Terminal.prototype.getBackgroundColor = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700682 return lib.notNull(this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700683};
684
685/**
686 * Set the foreground color.
687 *
688 * If you want this setting to persist, set it through prefs_, rather than
689 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500690 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500691 * @param {string=} color The color to set. If not defined, we reset to the
692 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700693 */
694hterm.Terminal.prototype.setForegroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500695 if (color === undefined)
Joel Hockeyd4fca732019-09-20 16:57:03 -0700696 color = this.prefs_.getString('foreground-color');
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500697
rgindacbbd7482012-06-13 15:06:16 -0700698 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700699 this.primaryScreen_.textAttributes.setDefaults(
700 this.foregroundColor_, this.backgroundColor_);
701 this.alternateScreen_.textAttributes.setDefaults(
702 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700703 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800704};
705
706/**
707 * Return the current terminal foreground color.
708 *
709 * Intended for use by other classes, so we don't have to expose the entire
710 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500711 *
712 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800713 */
714hterm.Terminal.prototype.getForegroundColor = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700715 return lib.notNull(this.foregroundColor_);
rginda9f5222b2012-03-05 11:53:28 -0800716};
717
718/**
rginda87b86462011-12-14 13:48:03 -0800719 * Create a new instance of a terminal command and run it with a given
720 * argument string.
721 *
Joel Hockeyd4fca732019-09-20 16:57:03 -0700722 * @param {!Function} commandClass The constructor for a terminal command.
Joel Hockey8081ea62019-08-26 16:52:32 -0700723 * @param {string} commandName The command to run for this terminal.
724 * @param {!Array<string>} args The arguments to pass to the command.
rginda87b86462011-12-14 13:48:03 -0800725 */
Joel Hockey8081ea62019-08-26 16:52:32 -0700726hterm.Terminal.prototype.runCommandClass = function(
727 commandClass, commandName, args) {
rgindaf522ce02012-04-17 17:49:17 -0700728 var environment = this.prefs_.get('environment');
729 if (typeof environment != 'object' || environment == null)
730 environment = {};
731
rginda87b86462011-12-14 13:48:03 -0800732 var self = this;
733 this.command = new commandClass(
Joel Hockey8081ea62019-08-26 16:52:32 -0700734 {
735 commandName: commandName,
736 args: args,
rginda87b86462011-12-14 13:48:03 -0800737 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700738 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800739 onExit: function(code) {
740 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800741 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700742 if (self.prefs_.get('close-on-exit'))
743 window.close();
rginda87b86462011-12-14 13:48:03 -0800744 }
745 });
746
rgindafeaf3142012-01-31 15:14:20 -0800747 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800748 this.command.run();
749};
750
751/**
rgindafeaf3142012-01-31 15:14:20 -0800752 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500753 *
754 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800755 */
756hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700757 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800758};
759
760/**
761 * Install the keyboard handler for this terminal.
762 *
763 * This will prevent the browser from seeing any keystrokes sent to the
764 * terminal.
765 */
766hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700767 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400768};
rgindafeaf3142012-01-31 15:14:20 -0800769
770/**
771 * Uninstall the keyboard handler for this terminal.
772 */
773hterm.Terminal.prototype.uninstallKeyboard = function() {
774 this.keyboard.installKeyboard(null);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400775};
rgindafeaf3142012-01-31 15:14:20 -0800776
777/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400778 * Set a CSS variable.
779 *
780 * Normally this is used to set variables in the hterm namespace.
781 *
782 * @param {string} name The variable to set.
Joel Hockeyd4fca732019-09-20 16:57:03 -0700783 * @param {string|number} value The value to assign to the variable.
Joel Hockey0f933582019-08-27 18:01:51 -0700784 * @param {string=} opt_prefix The variable namespace/prefix to use.
Mike Frysingercce97c42017-08-05 01:11:22 -0400785 */
786hterm.Terminal.prototype.setCssVar = function(name, value,
787 opt_prefix='--hterm-') {
788 this.document_.documentElement.style.setProperty(
Joel Hockeyd4fca732019-09-20 16:57:03 -0700789 `${opt_prefix}${name}`, value.toString());
Mike Frysingercce97c42017-08-05 01:11:22 -0400790};
791
792/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500793 * Get a CSS variable.
794 *
795 * Normally this is used to get variables in the hterm namespace.
796 *
797 * @param {string} name The variable to read.
Joel Hockey0f933582019-08-27 18:01:51 -0700798 * @param {string=} opt_prefix The variable namespace/prefix to use.
Mike Frysinger261597c2017-12-28 01:14:21 -0500799 * @return {string} The current setting for this variable.
800 */
801hterm.Terminal.prototype.getCssVar = function(name, opt_prefix='--hterm-') {
802 return this.document_.documentElement.style.getPropertyValue(
803 `${opt_prefix}${name}`);
804};
805
806/**
rginda35c456b2012-02-09 17:29:05 -0800807 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800808 *
809 * Call setFontSize(0) to reset to the default font size.
810 *
811 * This function does not modify the font-size preference.
812 *
813 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800814 */
815hterm.Terminal.prototype.setFontSize = function(px) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500816 if (px <= 0)
Joel Hockeyd4fca732019-09-20 16:57:03 -0700817 px = this.prefs_.getNumber('font-size');
rginda9f5222b2012-03-05 11:53:28 -0800818
rginda35c456b2012-02-09 17:29:05 -0800819 this.scrollPort_.setFontSize(px);
Mike Frysingercce97c42017-08-05 01:11:22 -0400820 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
821 this.setCssVar('charsize-height',
822 this.scrollPort_.characterSize.height + 'px');
rginda35c456b2012-02-09 17:29:05 -0800823};
824
825/**
826 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500827 *
828 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800829 */
830hterm.Terminal.prototype.getFontSize = function() {
831 return this.scrollPort_.getFontSize();
832};
833
834/**
rginda8e92a692012-05-20 19:37:20 -0700835 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500836 *
837 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700838 */
839hterm.Terminal.prototype.getFontFamily = function() {
840 return this.scrollPort_.getFontFamily();
841};
842
843/**
rginda35c456b2012-02-09 17:29:05 -0800844 * Set the CSS "font-family" for this terminal.
845 */
rginda9f5222b2012-03-05 11:53:28 -0800846hterm.Terminal.prototype.syncFontFamily = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700847 this.scrollPort_.setFontFamily(this.prefs_.getString('font-family'),
848 this.prefs_.getString('font-smoothing'));
rginda9f5222b2012-03-05 11:53:28 -0800849 this.syncBoldSafeState();
850};
851
rginda4bba5e12012-06-20 16:15:30 -0700852/**
853 * Set this.mousePasteButton based on the mouse-paste-button pref,
854 * autodetecting if necessary.
855 */
856hterm.Terminal.prototype.syncMousePasteButton = function() {
857 var button = this.prefs_.get('mouse-paste-button');
858 if (typeof button == 'number') {
859 this.mousePasteButton = button;
860 return;
861 }
862
Mike Frysingeree81a002017-12-12 16:14:53 -0500863 if (hterm.os != 'linux') {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400864 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700865 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400866 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700867 }
868};
869
870/**
871 * Enable or disable bold based on the enable-bold pref, autodetecting if
872 * necessary.
873 */
rginda9f5222b2012-03-05 11:53:28 -0800874hterm.Terminal.prototype.syncBoldSafeState = function() {
875 var enableBold = this.prefs_.get('enable-bold');
876 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700877 this.primaryScreen_.textAttributes.enableBold = enableBold;
878 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800879 return;
880 }
881
rgindaf7521392012-02-28 17:20:34 -0800882 var normalSize = this.scrollPort_.measureCharacterSize();
883 var boldSize = this.scrollPort_.measureCharacterSize('bold');
884
885 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800886 if (!isBoldSafe) {
887 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700888 'from normal. Font family is: ' +
889 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800890 }
rginda9f5222b2012-03-05 11:53:28 -0800891
Robert Gindaed016262012-10-26 16:27:09 -0700892 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
893 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800894};
895
896/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500897 * Control text blinking behavior.
898 *
899 * @param {boolean=} state Whether to enable support for blinking text.
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400900 */
Mike Frysinger261597c2017-12-28 01:14:21 -0500901hterm.Terminal.prototype.setTextBlink = function(state) {
902 if (state === undefined)
Joel Hockeyd4fca732019-09-20 16:57:03 -0700903 state = this.prefs_.getBoolean('enable-blink');
Mike Frysinger261597c2017-12-28 01:14:21 -0500904 this.setCssVar('blink-node-duration', state ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400905};
906
907/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400908 * Set the mouse cursor style based on the current terminal mode.
909 */
910hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -0400911 this.setCssVar('mouse-cursor-style',
912 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
913 'var(--hterm-mouse-cursor-text)' :
Mike Frysinger67f58f82018-11-22 13:38:22 -0500914 'var(--hterm-mouse-cursor-default)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400915};
916
917/**
rginda87b86462011-12-14 13:48:03 -0800918 * Return a copy of the current cursor position.
919 *
Joel Hockey0f933582019-08-27 18:01:51 -0700920 * @return {!hterm.RowCol} The RowCol object representing the current position.
rginda87b86462011-12-14 13:48:03 -0800921 */
922hterm.Terminal.prototype.saveCursor = function() {
923 return this.screen_.cursorPosition.clone();
924};
925
Evan Jones2600d4f2016-12-06 09:29:36 -0500926/**
927 * Return the current text attributes.
928 *
Joel Hockeyd4fca732019-09-20 16:57:03 -0700929 * @return {!hterm.TextAttributes}
Evan Jones2600d4f2016-12-06 09:29:36 -0500930 */
rgindaa19afe22012-01-25 15:40:22 -0800931hterm.Terminal.prototype.getTextAttributes = function() {
932 return this.screen_.textAttributes;
933};
934
Evan Jones2600d4f2016-12-06 09:29:36 -0500935/**
936 * Set the text attributes.
937 *
Joel Hockeyd4fca732019-09-20 16:57:03 -0700938 * @param {!hterm.TextAttributes} textAttributes The attributes to set.
Evan Jones2600d4f2016-12-06 09:29:36 -0500939 */
rginda1a09aa02012-06-18 21:11:25 -0700940hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
941 this.screen_.textAttributes = textAttributes;
942};
943
rginda87b86462011-12-14 13:48:03 -0800944/**
rgindaf522ce02012-04-17 17:49:17 -0700945 * Return the current browser zoom factor applied to the terminal.
946 *
947 * @return {number} The current browser zoom factor.
948 */
949hterm.Terminal.prototype.getZoomFactor = function() {
950 return this.scrollPort_.characterSize.zoomFactor;
951};
952
953/**
rginda9846e2f2012-01-27 13:53:33 -0800954 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -0500955 *
956 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -0800957 */
958hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800959 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800960};
961
962/**
rginda87b86462011-12-14 13:48:03 -0800963 * Restore a previously saved cursor position.
964 *
Joel Hockey0f933582019-08-27 18:01:51 -0700965 * @param {!hterm.RowCol} cursor The position to restore.
rginda87b86462011-12-14 13:48:03 -0800966 */
967hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700968 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
969 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800970 this.screen_.setCursorPosition(row, column);
971 if (cursor.column > column ||
972 cursor.column == column && cursor.overflow) {
973 this.screen_.cursorPosition.overflow = true;
974 }
rginda87b86462011-12-14 13:48:03 -0800975};
976
977/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400978 * Clear the cursor's overflow flag.
979 */
980hterm.Terminal.prototype.clearCursorOverflow = function() {
981 this.screen_.cursorPosition.overflow = false;
982};
983
984/**
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800985 * Save the current cursor state to the corresponding screens.
986 *
987 * See the hterm.Screen.CursorState class for more details.
988 *
989 * @param {boolean=} both If true, update both screens, else only update the
990 * current screen.
991 */
992hterm.Terminal.prototype.saveCursorAndState = function(both) {
993 if (both) {
994 this.primaryScreen_.saveCursorAndState(this.vt);
995 this.alternateScreen_.saveCursorAndState(this.vt);
996 } else
997 this.screen_.saveCursorAndState(this.vt);
998};
999
1000/**
1001 * Restore the saved cursor state in the corresponding screens.
1002 *
1003 * See the hterm.Screen.CursorState class for more details.
1004 *
1005 * @param {boolean=} both If true, update both screens, else only update the
1006 * current screen.
1007 */
1008hterm.Terminal.prototype.restoreCursorAndState = function(both) {
1009 if (both) {
1010 this.primaryScreen_.restoreCursorAndState(this.vt);
1011 this.alternateScreen_.restoreCursorAndState(this.vt);
1012 } else
1013 this.screen_.restoreCursorAndState(this.vt);
1014};
1015
1016/**
Robert Ginda830583c2013-08-07 13:20:46 -07001017 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001018 *
1019 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -07001020 */
1021hterm.Terminal.prototype.setCursorShape = function(shape) {
1022 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001023 this.restyleCursor_();
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001024};
Robert Ginda830583c2013-08-07 13:20:46 -07001025
1026/**
1027 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001028 *
1029 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -07001030 */
1031hterm.Terminal.prototype.getCursorShape = function() {
1032 return this.cursorShape_;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001033};
Robert Ginda830583c2013-08-07 13:20:46 -07001034
1035/**
rginda87b86462011-12-14 13:48:03 -08001036 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001037 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001038 * @param {?number} columnCount
rginda87b86462011-12-14 13:48:03 -08001039 */
1040hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -08001041 if (columnCount == null) {
1042 this.div_.style.width = '100%';
1043 return;
1044 }
1045
Robert Ginda26806d12014-07-24 13:44:07 -07001046 this.div_.style.width = Math.ceil(
1047 this.scrollPort_.characterSize.width *
1048 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001049 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -08001050 this.scheduleSyncCursorPosition_();
1051};
rginda87b86462011-12-14 13:48:03 -08001052
rgindac9bc5502012-01-18 11:48:44 -08001053/**
rginda35c456b2012-02-09 17:29:05 -08001054 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001055 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001056 * @param {?number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -08001057 */
1058hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -08001059 if (rowCount == null) {
1060 this.div_.style.height = '100%';
1061 return;
1062 }
1063
rginda35c456b2012-02-09 17:29:05 -08001064 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -07001065 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -08001066 this.realizeSize_(this.screenSize.width, rowCount);
1067 this.scheduleSyncCursorPosition_();
1068};
1069
1070/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001071 * Deal with terminal size changes.
1072 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001073 * @param {number} columnCount The number of columns.
1074 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001075 */
1076hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
Mike Frysinger0206e262019-06-13 10:18:19 -04001077 let notify = false;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001078
Mike Frysinger0206e262019-06-13 10:18:19 -04001079 if (columnCount != this.screenSize.width) {
1080 notify = true;
1081 this.realizeWidth_(columnCount);
1082 }
1083
1084 if (rowCount != this.screenSize.height) {
1085 notify = true;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001086 this.realizeHeight_(rowCount);
Mike Frysinger0206e262019-06-13 10:18:19 -04001087 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001088
1089 // Send new terminal size to plugin.
Mike Frysinger0206e262019-06-13 10:18:19 -04001090 if (notify) {
1091 this.io.onTerminalResize_(columnCount, rowCount);
1092 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001093};
1094
1095/**
rgindac9bc5502012-01-18 11:48:44 -08001096 * Deal with terminal width changes.
1097 *
1098 * This function does what needs to be done when the terminal width changes
1099 * out from under us. It happens here rather than in onResize_() because this
1100 * code may need to run synchronously to handle programmatic changes of
1101 * terminal width.
1102 *
1103 * Relying on the browser to send us an async resize event means we may not be
1104 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001105 *
1106 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001107 */
1108hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001109 if (columnCount <= 0)
1110 throw new Error('Attempt to realize bad width: ' + columnCount);
1111
rgindac9bc5502012-01-18 11:48:44 -08001112 var deltaColumns = columnCount - this.screen_.getWidth();
Mike Frysinger0206e262019-06-13 10:18:19 -04001113 if (deltaColumns == 0) {
1114 // No change, so don't bother recalculating things.
1115 return;
1116 }
rgindac9bc5502012-01-18 11:48:44 -08001117
rginda87b86462011-12-14 13:48:03 -08001118 this.screenSize.width = columnCount;
1119 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001120
1121 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -04001122 if (this.defaultTabStops)
1123 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -08001124 } else {
1125 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -04001126 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -08001127 break;
1128
1129 this.tabStops_.pop();
1130 }
1131 }
1132
1133 this.screen_.setColumnCount(this.screenSize.width);
1134};
1135
1136/**
1137 * Deal with terminal height changes.
1138 *
1139 * This function does what needs to be done when the terminal height changes
1140 * out from under us. It happens here rather than in onResize_() because this
1141 * code may need to run synchronously to handle programmatic changes of
1142 * terminal height.
1143 *
1144 * Relying on the browser to send us an async resize event means we may not be
1145 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001146 *
1147 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001148 */
1149hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001150 if (rowCount <= 0)
1151 throw new Error('Attempt to realize bad height: ' + rowCount);
1152
rgindac9bc5502012-01-18 11:48:44 -08001153 var deltaRows = rowCount - this.screen_.getHeight();
Mike Frysinger0206e262019-06-13 10:18:19 -04001154 if (deltaRows == 0) {
1155 // No change, so don't bother recalculating things.
1156 return;
1157 }
rgindac9bc5502012-01-18 11:48:44 -08001158
1159 this.screenSize.height = rowCount;
1160
1161 var cursor = this.saveCursor();
1162
1163 if (deltaRows < 0) {
1164 // Screen got smaller.
1165 deltaRows *= -1;
1166 while (deltaRows) {
1167 var lastRow = this.getRowCount() - 1;
1168 if (lastRow - this.scrollbackRows_.length == cursor.row)
1169 break;
1170
1171 if (this.getRowText(lastRow))
1172 break;
1173
1174 this.screen_.popRow();
1175 deltaRows--;
1176 }
1177
1178 var ary = this.screen_.shiftRows(deltaRows);
1179 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1180
1181 // We just removed rows from the top of the screen, we need to update
1182 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001183 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001184 } else if (deltaRows > 0) {
1185 // Screen got larger.
1186
1187 if (deltaRows <= this.scrollbackRows_.length) {
1188 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1189 var rows = this.scrollbackRows_.splice(
1190 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1191 this.screen_.unshiftRows(rows);
1192 deltaRows -= scrollbackCount;
1193 cursor.row += scrollbackCount;
1194 }
1195
1196 if (deltaRows)
1197 this.appendRows_(deltaRows);
1198 }
1199
rginda35c456b2012-02-09 17:29:05 -08001200 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001201 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001202};
1203
1204/**
1205 * Scroll the terminal to the top of the scrollback buffer.
1206 */
1207hterm.Terminal.prototype.scrollHome = function() {
1208 this.scrollPort_.scrollRowToTop(0);
1209};
1210
1211/**
1212 * Scroll the terminal to the end.
1213 */
1214hterm.Terminal.prototype.scrollEnd = function() {
1215 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1216};
1217
1218/**
1219 * Scroll the terminal one page up (minus one line) relative to the current
1220 * position.
1221 */
1222hterm.Terminal.prototype.scrollPageUp = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001223 this.scrollPort_.scrollPageUp();
rginda87b86462011-12-14 13:48:03 -08001224};
1225
1226/**
1227 * Scroll the terminal one page down (minus one line) relative to the current
1228 * position.
1229 */
1230hterm.Terminal.prototype.scrollPageDown = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001231 this.scrollPort_.scrollPageDown();
rginda8ba33642011-12-14 12:31:31 -08001232};
1233
rgindac9bc5502012-01-18 11:48:44 -08001234/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001235 * Scroll the terminal one line up relative to the current position.
1236 */
1237hterm.Terminal.prototype.scrollLineUp = function() {
1238 var i = this.scrollPort_.getTopRowIndex();
1239 this.scrollPort_.scrollRowToTop(i - 1);
1240};
1241
1242/**
1243 * Scroll the terminal one line down relative to the current position.
1244 */
1245hterm.Terminal.prototype.scrollLineDown = function() {
1246 var i = this.scrollPort_.getTopRowIndex();
1247 this.scrollPort_.scrollRowToTop(i + 1);
1248};
1249
1250/**
Robert Ginda40932892012-12-10 17:26:40 -08001251 * Clear primary screen, secondary screen, and the scrollback buffer.
1252 */
1253hterm.Terminal.prototype.wipeContents = function() {
Mike Frysinger9c482b82018-09-07 02:49:36 -04001254 this.clearHome(this.primaryScreen_);
1255 this.clearHome(this.alternateScreen_);
1256
1257 this.clearScrollback();
1258};
1259
1260/**
1261 * Clear scrollback buffer.
1262 */
1263hterm.Terminal.prototype.clearScrollback = function() {
1264 // Move to the end of the buffer in case the screen was scrolled back.
1265 // We're going to throw it away which would leave the display invalid.
1266 this.scrollEnd();
1267
Robert Ginda40932892012-12-10 17:26:40 -08001268 this.scrollbackRows_.length = 0;
1269 this.scrollPort_.resetCache();
1270
Mike Frysinger9c482b82018-09-07 02:49:36 -04001271 [this.primaryScreen_, this.alternateScreen_].forEach((screen) => {
1272 const bottom = screen.getHeight();
1273 this.renumberRows_(0, bottom, screen);
1274 });
Robert Ginda40932892012-12-10 17:26:40 -08001275
1276 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001277 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001278};
1279
1280/**
rgindac9bc5502012-01-18 11:48:44 -08001281 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001282 *
1283 * Perform a full reset to the default values listed in
1284 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001285 */
rginda87b86462011-12-14 13:48:03 -08001286hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001287 this.vt.reset();
1288
rgindac9bc5502012-01-18 11:48:44 -08001289 this.clearAllTabStops();
1290 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001291
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001292 const resetScreen = (screen) => {
1293 // We want to make sure to reset the attributes before we clear the screen.
1294 // The attributes might be used to initialize default/empty rows.
1295 screen.textAttributes.reset();
1296 screen.textAttributes.resetColorPalette();
1297 this.clearHome(screen);
1298 screen.saveCursorAndState(this.vt);
1299 };
1300 resetScreen(this.primaryScreen_);
1301 resetScreen(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001302
Mike Frysinger84301d02017-11-29 13:28:46 -08001303 // Reset terminal options to their default values.
1304 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001305 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1306
Mike Frysinger84301d02017-11-29 13:28:46 -08001307 this.setVTScrollRegion(null, null);
1308
1309 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001310};
1311
rgindac9bc5502012-01-18 11:48:44 -08001312/**
1313 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001314 *
1315 * Perform a soft reset to the default values listed in
1316 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001317 */
rginda0f5c0292012-01-13 11:00:13 -08001318hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001319 this.vt.reset();
1320
rgindab8bc8932012-04-27 12:45:03 -07001321 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001322 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001323
Brad Townb62dfdc2015-03-16 19:07:15 -07001324 // We show the cursor on soft reset but do not alter the blink state.
1325 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1326
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001327 const resetScreen = (screen) => {
1328 // Xterm also resets the color palette on soft reset, even though it doesn't
1329 // seem to be documented anywhere.
1330 screen.textAttributes.reset();
1331 screen.textAttributes.resetColorPalette();
1332 screen.saveCursorAndState(this.vt);
1333 };
1334 resetScreen(this.primaryScreen_);
1335 resetScreen(this.alternateScreen_);
rgindaf522ce02012-04-17 17:49:17 -07001336
rgindab8bc8932012-04-27 12:45:03 -07001337 // The xterm man page explicitly says this will happen on soft reset.
1338 this.setVTScrollRegion(null, null);
1339
1340 // Xterm also shows the cursor on soft reset, but does not alter the blink
1341 // state.
rgindaa19afe22012-01-25 15:40:22 -08001342 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001343};
1344
rgindac9bc5502012-01-18 11:48:44 -08001345/**
1346 * Move the cursor forward to the next tab stop, or to the last column
1347 * if no more tab stops are set.
1348 */
1349hterm.Terminal.prototype.forwardTabStop = function() {
1350 var column = this.screen_.cursorPosition.column;
1351
1352 for (var i = 0; i < this.tabStops_.length; i++) {
1353 if (this.tabStops_[i] > column) {
1354 this.setCursorColumn(this.tabStops_[i]);
1355 return;
1356 }
1357 }
1358
David Benjamin66e954d2012-05-05 21:08:12 -04001359 // xterm does not clear the overflow flag on HT or CHT.
1360 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001361 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001362 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001363};
1364
rgindac9bc5502012-01-18 11:48:44 -08001365/**
1366 * Move the cursor backward to the previous tab stop, or to the first column
1367 * if no previous tab stops are set.
1368 */
1369hterm.Terminal.prototype.backwardTabStop = function() {
1370 var column = this.screen_.cursorPosition.column;
1371
1372 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1373 if (this.tabStops_[i] < column) {
1374 this.setCursorColumn(this.tabStops_[i]);
1375 return;
1376 }
1377 }
1378
1379 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001380};
1381
rgindac9bc5502012-01-18 11:48:44 -08001382/**
1383 * Set a tab stop at the given column.
1384 *
Joel Hockey0f933582019-08-27 18:01:51 -07001385 * @param {number} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001386 */
1387hterm.Terminal.prototype.setTabStop = function(column) {
1388 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1389 if (this.tabStops_[i] == column)
1390 return;
1391
1392 if (this.tabStops_[i] < column) {
1393 this.tabStops_.splice(i + 1, 0, column);
1394 return;
1395 }
1396 }
1397
1398 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001399};
1400
rgindac9bc5502012-01-18 11:48:44 -08001401/**
1402 * Clear the tab stop at the current cursor position.
1403 *
1404 * No effect if there is no tab stop at the current cursor position.
1405 */
1406hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1407 var column = this.screen_.cursorPosition.column;
1408
1409 var i = this.tabStops_.indexOf(column);
1410 if (i == -1)
1411 return;
1412
1413 this.tabStops_.splice(i, 1);
1414};
1415
1416/**
1417 * Clear all tab stops.
1418 */
1419hterm.Terminal.prototype.clearAllTabStops = function() {
1420 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001421 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001422};
1423
1424/**
1425 * Set up the default tab stops, starting from a given column.
1426 *
1427 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001428 * from the specified column, or 0 if no column is provided. It also flags
1429 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001430 *
1431 * This does not clear the existing tab stops first, use clearAllTabStops
1432 * for that.
1433 *
Joel Hockey0f933582019-08-27 18:01:51 -07001434 * @param {number=} opt_start Optional starting zero based starting column,
1435 * useful for filling out missing tab stops when the terminal is resized.
rgindac9bc5502012-01-18 11:48:44 -08001436 */
1437hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1438 var start = opt_start || 0;
1439 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001440 // Round start up to a default tab stop.
1441 start = start - 1 - ((start - 1) % w) + w;
1442 for (var i = start; i < this.screenSize.width; i += w) {
1443 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001444 }
David Benjamin66e954d2012-05-05 21:08:12 -04001445
1446 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001447};
1448
rginda6d397402012-01-17 10:58:29 -08001449/**
rginda8ba33642011-12-14 12:31:31 -08001450 * Interpret a sequence of characters.
1451 *
1452 * Incomplete escape sequences are buffered until the next call.
1453 *
1454 * @param {string} str Sequence of characters to interpret or pass through.
1455 */
1456hterm.Terminal.prototype.interpret = function(str) {
rginda8ba33642011-12-14 12:31:31 -08001457 this.scheduleSyncCursorPosition_();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001458 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001459};
1460
1461/**
1462 * Take over the given DIV for use as the terminal display.
1463 *
Joel Hockey0f933582019-08-27 18:01:51 -07001464 * @param {!Element} div The div to use as the terminal display.
rginda8ba33642011-12-14 12:31:31 -08001465 */
1466hterm.Terminal.prototype.decorate = function(div) {
Mike Frysinger5768a9d2017-12-26 12:57:44 -05001467 const charset = div.ownerDocument.characterSet.toLowerCase();
1468 if (charset != 'utf-8') {
1469 console.warn(`Document encoding should be set to utf-8, not "${charset}";` +
1470 ` Add <meta charset='utf-8'/> to your HTML <head> to fix.`);
1471 }
1472
rginda87b86462011-12-14 13:48:03 -08001473 this.div_ = div;
1474
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001475 this.accessibilityReader_ = new hterm.AccessibilityReader(div);
1476
Adrián Pérez-Orozco394e64f2018-12-17 17:20:16 -08001477 this.scrollPort_.decorate(div, () => this.setupScrollPort_());
1478};
1479
1480/**
1481 * Initialisation of ScrollPort properties which need to be set after its DOM
1482 * has been initialised.
1483 * @private
1484 */
1485hterm.Terminal.prototype.setupScrollPort_ = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -07001486 this.scrollPort_.setBackgroundImage(
1487 this.prefs_.getString('background-image'));
1488 this.scrollPort_.setBackgroundSize(this.prefs_.getString('background-size'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001489 this.scrollPort_.setBackgroundPosition(
Joel Hockeyd4fca732019-09-20 16:57:03 -07001490 this.prefs_.getString('background-position'));
1491 this.scrollPort_.setUserCssUrl(this.prefs_.getString('user-css'));
1492 this.scrollPort_.setUserCssText(this.prefs_.getString('user-css-text'));
1493 this.scrollPort_.setAccessibilityReader(
1494 lib.notNull(this.accessibilityReader_));
rginda30f20f62012-04-05 16:36:19 -07001495
rginda0918b652012-04-04 11:26:24 -07001496 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001497
Joel Hockeyd4fca732019-09-20 16:57:03 -07001498 this.setFontSize(this.prefs_.getNumber('font-size'));
rginda9f5222b2012-03-05 11:53:28 -08001499 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001500
Joel Hockeyd4fca732019-09-20 16:57:03 -07001501 this.setScrollbarVisible(this.prefs_.getBoolean('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001502 this.setScrollWheelMoveMultipler(
Joel Hockeyd4fca732019-09-20 16:57:03 -07001503 this.prefs_.getNumber('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001504
rginda8ba33642011-12-14 12:31:31 -08001505 this.document_ = this.scrollPort_.getDocument();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001506 this.accessibilityReader_.decorate(this.document_);
rginda8ba33642011-12-14 12:31:31 -08001507
Evan Jones5f9df812016-12-06 09:38:58 -05001508 this.document_.body.oncontextmenu = function() { return false; };
Mike Frysingercc114512017-09-11 21:39:17 -04001509 this.contextMenu.setDocument(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07001510
1511 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001512 var screenNode = this.scrollPort_.getScreenNode();
Joel Hockeyd4fca732019-09-20 16:57:03 -07001513 screenNode.addEventListener(
1514 'mousedown', /** @type {!EventListener} */ (onMouse));
1515 screenNode.addEventListener(
1516 'mouseup', /** @type {!EventListener} */ (onMouse));
1517 screenNode.addEventListener(
1518 'mousemove', /** @type {!EventListener} */ (onMouse));
rginda4bba5e12012-06-20 16:15:30 -07001519 this.scrollPort_.onScrollWheel = onMouse;
1520
Joel Hockeyd4fca732019-09-20 16:57:03 -07001521 screenNode.addEventListener(
1522 'keydown',
1523 /** @type {!EventListener} */ (this.onKeyboardActivity_.bind(this)));
Mike Frysinger02ded6d2018-06-21 14:25:20 -04001524
Toni Barzic0bfa8922013-11-22 11:18:35 -08001525 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001526 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001527 // Listen for mousedown events on the screenNode as in FF the focus
1528 // events don't bubble.
1529 screenNode.addEventListener('mousedown', function() {
1530 setTimeout(this.onFocusChange_.bind(this, true));
1531 }.bind(this));
1532
Toni Barzic0bfa8922013-11-22 11:18:35 -08001533 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001534 'blur', this.onFocusChange_.bind(this, false));
1535
1536 var style = this.document_.createElement('style');
1537 style.textContent =
1538 ('.cursor-node[focus="false"] {' +
1539 ' box-sizing: border-box;' +
1540 ' background-color: transparent !important;' +
1541 ' border-width: 2px;' +
1542 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001543 '}' +
Mike Frysingercc114512017-09-11 21:39:17 -04001544 'menu {' +
1545 ' margin: 0;' +
1546 ' padding: 0;' +
1547 ' cursor: var(--hterm-mouse-cursor-pointer);' +
1548 '}' +
1549 'menuitem {' +
1550 ' white-space: nowrap;' +
1551 ' border-bottom: 1px dashed;' +
1552 ' display: block;' +
1553 ' padding: 0.3em 0.3em 0 0.3em;' +
1554 '}' +
1555 'menuitem.separator {' +
1556 ' border-bottom: none;' +
1557 ' height: 0.5em;' +
1558 ' padding: 0;' +
1559 '}' +
1560 'menuitem:hover {' +
1561 ' color: var(--hterm-cursor-color);' +
1562 '}' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001563 '.wc-node {' +
1564 ' display: inline-block;' +
1565 ' text-align: center;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001566 ' width: calc(var(--hterm-charsize-width) * 2);' +
Mike Frysinger4036f6e2017-05-31 14:02:55 -04001567 ' line-height: var(--hterm-charsize-height);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001568 '}' +
1569 ':root {' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001570 ' --hterm-charsize-width: ' + this.scrollPort_.characterSize.width + 'px;' +
1571 ' --hterm-charsize-height: ' + this.scrollPort_.characterSize.height + 'px;' +
Mike Frysingera27c0502017-08-23 22:37:10 -04001572 // Default position hides the cursor for when the window is initializing.
1573 ' --hterm-cursor-offset-col: -1;' +
1574 ' --hterm-cursor-offset-row: -1;' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001575 ' --hterm-blink-node-duration: 0.7s;' +
Mike Frysinger67f58f82018-11-22 13:38:22 -05001576 ' --hterm-mouse-cursor-default: default;' +
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001577 ' --hterm-mouse-cursor-text: text;' +
Mike Frysinger67f58f82018-11-22 13:38:22 -05001578 ' --hterm-mouse-cursor-pointer: pointer;' +
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001579 ' --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001580 '}' +
Mike Frysingerd3907202017-10-23 01:23:50 -04001581 '.uri-node:hover {' +
1582 ' text-decoration: underline;' +
Mike Frysinger67f58f82018-11-22 13:38:22 -05001583 ' cursor: var(--hterm-mouse-cursor-pointer);' +
Mike Frysingerd3907202017-10-23 01:23:50 -04001584 '}' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001585 '@keyframes blink {' +
1586 ' from { opacity: 1.0; }' +
1587 ' to { opacity: 0.0; }' +
1588 '}' +
1589 '.blink-node {' +
1590 ' animation-name: blink;' +
1591 ' animation-duration: var(--hterm-blink-node-duration);' +
1592 ' animation-iteration-count: infinite;' +
1593 ' animation-timing-function: ease-in-out;' +
1594 ' animation-direction: alternate;' +
rginda8e92a692012-05-20 19:37:20 -07001595 '}');
Mike Frysingerb74a6472018-06-22 13:37:08 -04001596 // Insert this stock style as the first node so that any user styles will
1597 // override w/out having to use !important everywhere. The rules above mix
1598 // runtime variables with default ones designed to be overridden by the user,
1599 // but we can wait for a concrete case from the users to determine the best
1600 // way to split the sheet up to before & after the user-css settings.
1601 this.document_.head.insertBefore(style, this.document_.head.firstChild);
rginda8e92a692012-05-20 19:37:20 -07001602
rginda8ba33642011-12-14 12:31:31 -08001603 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001604 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001605 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001606 this.cursorNode_.style.cssText =
1607 ('position: absolute;' +
Mike Frysinger44c32202017-08-05 01:13:09 -04001608 'left: calc(var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));' +
1609 'top: calc(var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));' +
Mike Frysingerd60f4c22018-03-15 21:25:30 -07001610 'display: ' + (this.options_.cursorVisible ? '' : 'none') + ';' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001611 'width: var(--hterm-charsize-width);' +
1612 'height: var(--hterm-charsize-height);' +
Mike Frysinger2fd079a2018-09-02 01:46:12 -04001613 'background-color: var(--hterm-cursor-color);' +
1614 'border-color: var(--hterm-cursor-color);' +
Rob Spies06533ba2014-04-24 11:20:37 -07001615 '-webkit-transition: opacity, background-color 100ms linear;' +
1616 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001617
Mike Frysingerf02a2cb2017-12-21 00:34:03 -05001618 this.setCursorColor();
Robert Gindafb1be6a2013-12-11 11:56:22 -08001619 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1620 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001621
rginda8ba33642011-12-14 12:31:31 -08001622 this.document_.body.appendChild(this.cursorNode_);
1623
rgindad5613292012-06-19 15:40:37 -07001624 // When 'enableMouseDragScroll' is off we reposition this element directly
1625 // under the mouse cursor after a click. This makes Chrome associate
1626 // subsequent mousemove events with the scroll-blocker. Since the
1627 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1628 // events do not cause the scrollport to scroll.
1629 //
1630 // It's a hack, but it's the cleanest way I could find.
1631 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001632 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
Raymes Khoury6dce2f82018-04-12 15:38:58 +10001633 this.scrollBlockerNode_.setAttribute('aria-hidden', 'true');
rgindad5613292012-06-19 15:40:37 -07001634 this.scrollBlockerNode_.style.cssText =
1635 ('position: absolute;' +
1636 'top: -99px;' +
1637 'display: block;' +
1638 'width: 10px;' +
1639 'height: 10px;');
1640 this.document_.body.appendChild(this.scrollBlockerNode_);
1641
rgindad5613292012-06-19 15:40:37 -07001642 this.scrollPort_.onScrollWheel = onMouse;
1643 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1644 ].forEach(function(event) {
1645 this.scrollBlockerNode_.addEventListener(event, onMouse);
Joel Hockeyd4fca732019-09-20 16:57:03 -07001646 this.cursorNode_.addEventListener(
1647 event, /** @type {!EventListener} */ (onMouse));
1648 this.document_.addEventListener(
1649 event, /** @type {!EventListener} */ (onMouse));
rgindad5613292012-06-19 15:40:37 -07001650 }.bind(this));
1651
1652 this.cursorNode_.addEventListener('mousedown', function() {
1653 setTimeout(this.focus.bind(this));
1654 }.bind(this));
1655
rginda8ba33642011-12-14 12:31:31 -08001656 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001657
rginda87b86462011-12-14 13:48:03 -08001658 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001659 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001660};
1661
rginda0918b652012-04-04 11:26:24 -07001662/**
1663 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001664 *
Joel Hockey0f933582019-08-27 18:01:51 -07001665 * @return {!Document}
rginda0918b652012-04-04 11:26:24 -07001666 */
rginda87b86462011-12-14 13:48:03 -08001667hterm.Terminal.prototype.getDocument = function() {
1668 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001669};
1670
1671/**
rginda0918b652012-04-04 11:26:24 -07001672 * Focus the terminal.
1673 */
1674hterm.Terminal.prototype.focus = function() {
1675 this.scrollPort_.focus();
1676};
1677
1678/**
Theodore Duboiscea9b782019-09-02 17:48:00 -07001679 * Unfocus the terminal.
1680 */
1681hterm.Terminal.prototype.blur = function() {
1682 this.scrollPort_.blur();
1683};
1684
1685/**
rginda8ba33642011-12-14 12:31:31 -08001686 * Return the HTML Element for a given row index.
1687 *
1688 * This is a method from the RowProvider interface. The ScrollPort uses
1689 * it to fetch rows on demand as they are scrolled into view.
1690 *
1691 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1692 * pairs to conserve memory.
1693 *
Joel Hockey0f933582019-08-27 18:01:51 -07001694 * @param {number} index The zero-based row index, measured relative to the
rginda8ba33642011-12-14 12:31:31 -08001695 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001696 * largest indices.
Joel Hockey0f933582019-08-27 18:01:51 -07001697 * @return {!Element} The 'x-row' element containing for the requested row.
Joel Hockeyd4fca732019-09-20 16:57:03 -07001698 * @override
rginda8ba33642011-12-14 12:31:31 -08001699 */
1700hterm.Terminal.prototype.getRowNode = function(index) {
1701 if (index < this.scrollbackRows_.length)
1702 return this.scrollbackRows_[index];
1703
1704 var screenIndex = index - this.scrollbackRows_.length;
1705 return this.screen_.rowsArray[screenIndex];
1706};
1707
1708/**
1709 * Return the text content for a given range of rows.
1710 *
1711 * This is a method from the RowProvider interface. The ScrollPort uses
1712 * it to fetch text content on demand when the user attempts to copy their
1713 * selection to the clipboard.
1714 *
Joel Hockey0f933582019-08-27 18:01:51 -07001715 * @param {number} start The zero-based row index to start from, measured
rginda8ba33642011-12-14 12:31:31 -08001716 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001717 * always have the largest indices.
Joel Hockey0f933582019-08-27 18:01:51 -07001718 * @param {number} end The zero-based row index to end on, measured
rginda8ba33642011-12-14 12:31:31 -08001719 * relative to the start of the scrollback buffer.
1720 * @return {string} A single string containing the text value of the range of
1721 * rows. Lines will be newline delimited, with no trailing newline.
1722 */
1723hterm.Terminal.prototype.getRowsText = function(start, end) {
1724 var ary = [];
1725 for (var i = start; i < end; i++) {
1726 var node = this.getRowNode(i);
1727 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001728 if (i < end - 1 && !node.getAttribute('line-overflow'))
1729 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001730 }
1731
rgindaa09e7332012-08-17 12:49:51 -07001732 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001733};
1734
1735/**
1736 * Return the text content for a given row.
1737 *
1738 * This is a method from the RowProvider interface. The ScrollPort uses
1739 * it to fetch text content on demand when the user attempts to copy their
1740 * selection to the clipboard.
1741 *
Joel Hockey0f933582019-08-27 18:01:51 -07001742 * @param {number} index The zero-based row index to return, measured
rginda8ba33642011-12-14 12:31:31 -08001743 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001744 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001745 * @return {string} A string containing the text value of the selected row.
1746 */
1747hterm.Terminal.prototype.getRowText = function(index) {
1748 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001749 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001750};
1751
1752/**
1753 * Return the total number of rows in the addressable screen and in the
1754 * scrollback buffer of this terminal.
1755 *
1756 * This is a method from the RowProvider interface. The ScrollPort uses
1757 * it to compute the size of the scrollbar.
1758 *
Joel Hockey0f933582019-08-27 18:01:51 -07001759 * @return {number} The number of rows in this terminal.
Joel Hockeyd4fca732019-09-20 16:57:03 -07001760 * @override
rginda8ba33642011-12-14 12:31:31 -08001761 */
1762hterm.Terminal.prototype.getRowCount = function() {
1763 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1764};
1765
1766/**
1767 * Create DOM nodes for new rows and append them to the end of the terminal.
1768 *
1769 * This is the only correct way to add a new DOM node for a row. Notice that
1770 * the new row is appended to the bottom of the list of rows, and does not
1771 * require renumbering (of the rowIndex property) of previous rows.
1772 *
1773 * If you think you want a new blank row somewhere in the middle of the
1774 * terminal, look into moveRows_().
1775 *
1776 * This method does not pay attention to vtScrollTop/Bottom, since you should
1777 * be using moveRows() in cases where they would matter.
1778 *
1779 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001780 *
1781 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001782 */
1783hterm.Terminal.prototype.appendRows_ = function(count) {
1784 var cursorRow = this.screen_.rowsArray.length;
1785 var offset = this.scrollbackRows_.length + cursorRow;
1786 for (var i = 0; i < count; i++) {
1787 var row = this.document_.createElement('x-row');
1788 row.appendChild(this.document_.createTextNode(''));
1789 row.rowIndex = offset + i;
1790 this.screen_.pushRow(row);
1791 }
1792
1793 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1794 if (extraRows > 0) {
1795 var ary = this.screen_.shiftRows(extraRows);
1796 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001797 if (this.scrollPort_.isScrolledEnd)
1798 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001799 }
1800
1801 if (cursorRow >= this.screen_.rowsArray.length)
1802 cursorRow = this.screen_.rowsArray.length - 1;
1803
rginda87b86462011-12-14 13:48:03 -08001804 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001805};
1806
1807/**
1808 * Relocate rows from one part of the addressable screen to another.
1809 *
1810 * This is used to recycle rows during VT scrolls (those which are driven
1811 * by VT commands, rather than by the user manipulating the scrollbar.)
1812 *
1813 * In this case, the blank lines scrolled into the scroll region are made of
1814 * the nodes we scrolled off. These have their rowIndex properties carefully
1815 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001816 *
1817 * @param {number} fromIndex The start index.
1818 * @param {number} count The number of rows to move.
1819 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001820 */
1821hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1822 var ary = this.screen_.removeRows(fromIndex, count);
1823 this.screen_.insertRows(toIndex, ary);
1824
1825 var start, end;
1826 if (fromIndex < toIndex) {
1827 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001828 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001829 } else {
1830 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001831 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001832 }
1833
1834 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001835 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001836};
1837
1838/**
1839 * Renumber the rowIndex property of the given range of rows.
1840 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001841 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08001842 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001843 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001844 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05001845 *
1846 * @param {number} start The start index.
1847 * @param {number} end The end index.
Joel Hockey0f933582019-08-27 18:01:51 -07001848 * @param {!hterm.Screen=} opt_screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08001849 */
Robert Ginda40932892012-12-10 17:26:40 -08001850hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1851 var screen = opt_screen || this.screen_;
1852
rginda8ba33642011-12-14 12:31:31 -08001853 var offset = this.scrollbackRows_.length;
1854 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001855 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001856 }
1857};
1858
1859/**
1860 * Print a string to the terminal.
1861 *
1862 * This respects the current insert and wraparound modes. It will add new lines
1863 * to the end of the terminal, scrolling off the top into the scrollback buffer
1864 * if necessary.
1865 *
1866 * The string is *not* parsed for escape codes. Use the interpret() method if
1867 * that's what you're after.
1868 *
1869 * @param{string} str The string to print.
1870 */
1871hterm.Terminal.prototype.print = function(str) {
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001872 this.scheduleSyncCursorPosition_();
1873
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001874 // Basic accessibility output for the screen reader.
Raymes Khoury177aec72018-06-26 10:58:53 +10001875 this.accessibilityReader_.announce(str);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001876
rgindaa9abdd82012-08-06 18:05:09 -07001877 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001878
Ricky Liang48f05cb2013-12-31 23:35:29 +08001879 var strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04001880 // Fun edge case: If the string only contains zero width codepoints (like
1881 // combining characters), we make sure to iterate at least once below.
1882 if (strWidth == 0 && str)
1883 strWidth = 1;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001884
1885 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001886 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1887 this.screen_.commitLineOverflow();
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001888 this.newLine(true);
rgindaa09e7332012-08-17 12:49:51 -07001889 }
rgindaa19afe22012-01-25 15:40:22 -08001890
Ricky Liang48f05cb2013-12-31 23:35:29 +08001891 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001892 var didOverflow = false;
1893 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001894
rgindaa9abdd82012-08-06 18:05:09 -07001895 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1896 didOverflow = true;
1897 count = this.screenSize.width - this.screen_.cursorPosition.column;
1898 }
rgindaa19afe22012-01-25 15:40:22 -08001899
rgindaa9abdd82012-08-06 18:05:09 -07001900 if (didOverflow && !this.options_.wraparound) {
1901 // If the string overflowed the line but wraparound is off, then the
1902 // last printed character should be the last of the string.
1903 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001904 substr = lib.wc.substr(str, startOffset, count - 1) +
1905 lib.wc.substr(str, strWidth - 1);
1906 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001907 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001908 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001909 }
rgindaa19afe22012-01-25 15:40:22 -08001910
Ricky Liang48f05cb2013-12-31 23:35:29 +08001911 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1912 for (var i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001913 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
1914 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001915
1916 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001917 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001918 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001919 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001920 }
1921 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001922 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07001923 }
1924
1925 this.screen_.maybeClipCurrentRow();
1926 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001927 }
rginda8ba33642011-12-14 12:31:31 -08001928
rginda9f5222b2012-03-05 11:53:28 -08001929 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001930 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001931};
1932
1933/**
rginda87b86462011-12-14 13:48:03 -08001934 * Set the VT scroll region.
1935 *
rginda87b86462011-12-14 13:48:03 -08001936 * This also resets the cursor position to the absolute (0, 0) position, since
1937 * that's what xterm appears to do.
1938 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001939 * Setting the scroll region to the full height of the terminal will clear
1940 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1941 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1942 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1943 * continue to work as most users would expect.
1944 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001945 * @param {?number} scrollTop The zero-based top of the scroll region.
1946 * @param {?number} scrollBottom The zero-based bottom of the scroll region,
rginda87b86462011-12-14 13:48:03 -08001947 * inclusive.
1948 */
1949hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001950 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001951 this.vtScrollTop_ = null;
1952 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001953 } else {
1954 this.vtScrollTop_ = scrollTop;
1955 this.vtScrollBottom_ = scrollBottom;
1956 }
rginda87b86462011-12-14 13:48:03 -08001957};
1958
1959/**
rginda8ba33642011-12-14 12:31:31 -08001960 * Return the top row index according to the VT.
1961 *
1962 * This will return 0 unless the terminal has been told to restrict scrolling
1963 * to some lower row. It is used for some VT cursor positioning and scrolling
1964 * commands.
1965 *
Joel Hockey0f933582019-08-27 18:01:51 -07001966 * @return {number} The topmost row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001967 */
1968hterm.Terminal.prototype.getVTScrollTop = function() {
1969 if (this.vtScrollTop_ != null)
1970 return this.vtScrollTop_;
1971
1972 return 0;
rginda87b86462011-12-14 13:48:03 -08001973};
rginda8ba33642011-12-14 12:31:31 -08001974
1975/**
1976 * Return the bottom row index according to the VT.
1977 *
1978 * This will return the height of the terminal unless the it has been told to
1979 * restrict scrolling to some higher row. It is used for some VT cursor
1980 * positioning and scrolling commands.
1981 *
Joel Hockey0f933582019-08-27 18:01:51 -07001982 * @return {number} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001983 */
1984hterm.Terminal.prototype.getVTScrollBottom = function() {
1985 if (this.vtScrollBottom_ != null)
1986 return this.vtScrollBottom_;
1987
rginda87b86462011-12-14 13:48:03 -08001988 return this.screenSize.height - 1;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001989};
rginda8ba33642011-12-14 12:31:31 -08001990
1991/**
1992 * Process a '\n' character.
1993 *
1994 * If the cursor is on the final row of the terminal this will append a new
1995 * blank row to the screen and scroll the topmost row into the scrollback
1996 * buffer.
1997 *
1998 * Otherwise, this moves the cursor to column zero of the next row.
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001999 *
2000 * @param {boolean=} dueToOverflow Whether the newline is due to wraparound of
2001 * the terminal.
rginda8ba33642011-12-14 12:31:31 -08002002 */
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002003hterm.Terminal.prototype.newLine = function(dueToOverflow = false) {
2004 if (!dueToOverflow)
2005 this.accessibilityReader_.newLine();
2006
Robert Ginda9937abc2013-07-25 16:09:23 -07002007 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
2008 this.screen_.rowsArray.length - 1);
2009
2010 if (this.vtScrollBottom_ != null) {
2011 // A VT Scroll region is active, we never append new rows.
2012 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
2013 // We're at the end of the VT Scroll Region, perform a VT scroll.
2014 this.vtScrollUp(1);
2015 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
2016 } else if (cursorAtEndOfScreen) {
2017 // We're at the end of the screen, the only thing to do is put the
2018 // cursor to column 0.
2019 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
2020 } else {
2021 // Anywhere else, advance the cursor row, and reset the column.
2022 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
2023 }
2024 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07002025 // We're at the end of the screen. Append a new row to the terminal,
2026 // shifting the top row into the scrollback.
2027 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08002028 } else {
rginda87b86462011-12-14 13:48:03 -08002029 // Anywhere else in the screen just moves the cursor.
2030 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08002031 }
2032};
2033
2034/**
2035 * Like newLine(), except maintain the cursor column.
2036 */
2037hterm.Terminal.prototype.lineFeed = function() {
2038 var column = this.screen_.cursorPosition.column;
2039 this.newLine();
2040 this.setCursorColumn(column);
2041};
2042
2043/**
rginda87b86462011-12-14 13:48:03 -08002044 * If autoCarriageReturn is set then newLine(), else lineFeed().
2045 */
2046hterm.Terminal.prototype.formFeed = function() {
2047 if (this.options_.autoCarriageReturn) {
2048 this.newLine();
2049 } else {
2050 this.lineFeed();
2051 }
2052};
2053
2054/**
2055 * Move the cursor up one row, possibly inserting a blank line.
2056 *
2057 * The cursor column is not changed.
2058 */
2059hterm.Terminal.prototype.reverseLineFeed = function() {
2060 var scrollTop = this.getVTScrollTop();
2061 var currentRow = this.screen_.cursorPosition.row;
2062
2063 if (currentRow == scrollTop) {
2064 this.insertLines(1);
2065 } else {
2066 this.setAbsoluteCursorRow(currentRow - 1);
2067 }
2068};
2069
2070/**
rginda8ba33642011-12-14 12:31:31 -08002071 * Replace all characters to the left of the current cursor with the space
2072 * character.
2073 *
2074 * TODO(rginda): This should probably *remove* the characters (not just replace
2075 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07002076 * position.
rginda8ba33642011-12-14 12:31:31 -08002077 */
2078hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08002079 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002080 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002081 const count = cursor.column + 1;
Mike Frysinger73e56462019-07-17 00:23:46 -05002082 this.screen_.overwriteString(' '.repeat(count), count);
rginda87b86462011-12-14 13:48:03 -08002083 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002084};
2085
2086/**
David Benjamin684a9b72012-05-01 17:19:58 -04002087 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08002088 *
2089 * The cursor position is unchanged.
2090 *
Robert Gindaf2547f12012-10-25 20:36:21 -07002091 * If the current background color is not the default background color this
2092 * will insert spaces rather than delete. This is unfortunate because the
2093 * trailing space will affect text selection, but it's difficult to come up
2094 * with a way to style empty space that wouldn't trip up the hterm.Screen
2095 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07002096 *
2097 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
2098 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
2099 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05002100 *
Joel Hockey0f933582019-08-27 18:01:51 -07002101 * @param {number=} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08002102 */
2103hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07002104 if (this.screen_.cursorPosition.overflow)
2105 return;
2106
Robert Ginda7fd57082012-09-25 14:41:47 -07002107 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
2108 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07002109
2110 if (this.screen_.textAttributes.background ===
2111 this.screen_.textAttributes.DEFAULT_COLOR) {
2112 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08002113 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07002114 this.screen_.cursorPosition.column + count) {
2115 this.screen_.deleteChars(count);
2116 this.clearCursorOverflow();
2117 return;
2118 }
2119 }
2120
rginda87b86462011-12-14 13:48:03 -08002121 var cursor = this.saveCursor();
Mike Frysinger73e56462019-07-17 00:23:46 -05002122 this.screen_.overwriteString(' '.repeat(count), count);
rginda87b86462011-12-14 13:48:03 -08002123 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002124 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002125};
2126
2127/**
2128 * Erase the current line.
2129 *
2130 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002131 */
2132hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08002133 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002134 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08002135 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002136 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002137};
2138
2139/**
David Benjamina08d78f2012-05-05 00:28:49 -04002140 * Erase all characters from the start of the screen to the current cursor
2141 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002142 *
2143 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002144 */
2145hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08002146 var cursor = this.saveCursor();
2147
2148 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002149
David Benjamina08d78f2012-05-05 00:28:49 -04002150 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002151 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002152 this.screen_.clearCursorRow();
2153 }
2154
rginda87b86462011-12-14 13:48:03 -08002155 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002156 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002157};
2158
2159/**
2160 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002161 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002162 *
2163 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002164 */
2165hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08002166 var cursor = this.saveCursor();
2167
2168 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002169
David Benjamina08d78f2012-05-05 00:28:49 -04002170 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08002171 for (var i = cursor.row + 1; i <= bottom; i++) {
2172 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002173 this.screen_.clearCursorRow();
2174 }
2175
rginda87b86462011-12-14 13:48:03 -08002176 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002177 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002178};
2179
2180/**
2181 * Fill the terminal with a given character.
2182 *
2183 * This methods does not respect the VT scroll region.
2184 *
2185 * @param {string} ch The character to use for the fill.
2186 */
2187hterm.Terminal.prototype.fill = function(ch) {
2188 var cursor = this.saveCursor();
2189
2190 this.setAbsoluteCursorPosition(0, 0);
2191 for (var row = 0; row < this.screenSize.height; row++) {
2192 for (var col = 0; col < this.screenSize.width; col++) {
2193 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002194 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002195 }
2196 }
2197
2198 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002199};
2200
2201/**
rginda9ea433c2012-03-16 11:57:00 -07002202 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002203 *
rginda9ea433c2012-03-16 11:57:00 -07002204 * This does not respect the scroll region.
2205 *
Joel Hockey0f933582019-08-27 18:01:51 -07002206 * @param {!hterm.Screen=} opt_screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002207 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002208 */
rginda9ea433c2012-03-16 11:57:00 -07002209hterm.Terminal.prototype.clearHome = function(opt_screen) {
2210 var screen = opt_screen || this.screen_;
2211 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002212
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002213 this.accessibilityReader_.clear();
2214
rginda11057d52012-04-25 12:29:56 -07002215 if (bottom == 0) {
2216 // Empty screen, nothing to do.
2217 return;
2218 }
2219
rgindae4d29232012-01-19 10:47:13 -08002220 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002221 screen.setCursorPosition(i, 0);
2222 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002223 }
2224
rginda9ea433c2012-03-16 11:57:00 -07002225 screen.setCursorPosition(0, 0);
2226};
2227
2228/**
2229 * Erase the entire display without changing the cursor position.
2230 *
2231 * The cursor position is unchanged. This does not respect the scroll
2232 * region.
2233 *
Joel Hockey0f933582019-08-27 18:01:51 -07002234 * @param {!hterm.Screen=} opt_screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002235 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002236 */
2237hterm.Terminal.prototype.clear = function(opt_screen) {
2238 var screen = opt_screen || this.screen_;
2239 var cursor = screen.cursorPosition.clone();
2240 this.clearHome(screen);
2241 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002242};
2243
2244/**
2245 * VT command to insert lines at the current cursor row.
2246 *
2247 * This respects the current scroll region. Rows pushed off the bottom are
2248 * lost (they won't show up in the scrollback buffer).
2249 *
Joel Hockey0f933582019-08-27 18:01:51 -07002250 * @param {number} count The number of lines to insert.
rginda8ba33642011-12-14 12:31:31 -08002251 */
2252hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07002253 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002254
2255 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002256 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002257
Robert Ginda579186b2012-09-26 11:40:04 -07002258 // The moveCount is the number of rows we need to relocate to make room for
2259 // the new row(s). The count is the distance to move them.
2260 var moveCount = bottom - cursorRow - count + 1;
2261 if (moveCount)
2262 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08002263
Robert Ginda579186b2012-09-26 11:40:04 -07002264 for (var i = count - 1; i >= 0; i--) {
2265 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002266 this.screen_.clearCursorRow();
2267 }
rginda8ba33642011-12-14 12:31:31 -08002268};
2269
2270/**
2271 * VT command to delete lines at the current cursor row.
2272 *
2273 * New rows are added to the bottom of scroll region to take their place. New
2274 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002275 *
2276 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002277 */
2278hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08002279 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002280
rginda87b86462011-12-14 13:48:03 -08002281 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08002282 var bottom = this.getVTScrollBottom();
2283
rginda87b86462011-12-14 13:48:03 -08002284 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002285 count = Math.min(count, maxCount);
2286
rginda87b86462011-12-14 13:48:03 -08002287 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08002288 if (count != maxCount)
2289 this.moveRows_(top, count, moveStart);
2290
2291 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002292 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002293 this.screen_.clearCursorRow();
2294 }
2295
rginda87b86462011-12-14 13:48:03 -08002296 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002297 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002298};
2299
2300/**
2301 * Inserts the given number of spaces at the current cursor position.
2302 *
rginda87b86462011-12-14 13:48:03 -08002303 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002304 *
2305 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002306 */
2307hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08002308 var cursor = this.saveCursor();
2309
Mike Frysinger73e56462019-07-17 00:23:46 -05002310 const ws = ' '.repeat(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002311 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002312 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002313
2314 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002315 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002316};
2317
2318/**
2319 * Forward-delete the specified number of characters starting at the cursor
2320 * position.
2321 *
Joel Hockey0f933582019-08-27 18:01:51 -07002322 * @param {number} count The number of characters to delete.
rginda8ba33642011-12-14 12:31:31 -08002323 */
2324hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002325 var deleted = this.screen_.deleteChars(count);
2326 if (deleted && !this.screen_.textAttributes.isDefault()) {
2327 var cursor = this.saveCursor();
2328 this.setCursorColumn(this.screenSize.width - deleted);
Mike Frysinger73e56462019-07-17 00:23:46 -05002329 this.screen_.insertString(' '.repeat(deleted));
Robert Ginda7fd57082012-09-25 14:41:47 -07002330 this.restoreCursor(cursor);
2331 }
2332
David Benjamin54e8bf62012-06-01 22:31:40 -04002333 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002334};
2335
2336/**
2337 * Shift rows in the scroll region upwards by a given number of lines.
2338 *
2339 * New rows are inserted at the bottom of the scroll region to fill the
2340 * vacated rows. The new rows not filled out with the current text attributes.
2341 *
2342 * This function does not affect the scrollback rows at all. Rows shifted
2343 * off the top are lost.
2344 *
rginda87b86462011-12-14 13:48:03 -08002345 * The cursor position is not altered.
2346 *
Joel Hockey0f933582019-08-27 18:01:51 -07002347 * @param {number} count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002348 */
2349hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002350 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002351
rginda87b86462011-12-14 13:48:03 -08002352 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002353 this.deleteLines(count);
2354
rginda87b86462011-12-14 13:48:03 -08002355 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002356};
2357
2358/**
2359 * Shift rows below the cursor down by a given number of lines.
2360 *
2361 * This function respects the current scroll region.
2362 *
2363 * New rows are inserted at the top 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 bottom are lost.
2368 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07002369 * @param {number} count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002370 */
Joel Hockeyd4fca732019-09-20 16:57:03 -07002371hterm.Terminal.prototype.vtScrollDown = function(count) {
rginda87b86462011-12-14 13:48:03 -08002372 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002373
rginda87b86462011-12-14 13:48:03 -08002374 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
Joel Hockeyd4fca732019-09-20 16:57:03 -07002375 this.insertLines(count);
rginda8ba33642011-12-14 12:31:31 -08002376
rginda87b86462011-12-14 13:48:03 -08002377 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002378};
2379
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002380/**
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002381 * Enable accessibility-friendly features that have a performance impact.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002382 *
2383 * This will generate additional DOM nodes in an aria-live region that will
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002384 * cause Assitive Technology to announce the output of the terminal. It also
2385 * enables other features that aid assistive technology. All the features gated
2386 * behind this flag have a performance impact on the terminal which is why they
2387 * are made optional.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002388 *
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002389 * @param {boolean} enabled Whether to enable accessibility-friendly features.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002390 */
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002391hterm.Terminal.prototype.setAccessibilityEnabled = function(enabled) {
Raymes Khoury177aec72018-06-26 10:58:53 +10002392 this.accessibilityReader_.setAccessibilityEnabled(enabled);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002393};
rginda87b86462011-12-14 13:48:03 -08002394
rginda8ba33642011-12-14 12:31:31 -08002395/**
2396 * Set the cursor position.
2397 *
2398 * The cursor row is relative to the scroll region if the terminal has
2399 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2400 *
Joel Hockey0f933582019-08-27 18:01:51 -07002401 * @param {number} row The new zero-based cursor row.
2402 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002403 */
2404hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2405 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002406 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002407 } else {
rginda87b86462011-12-14 13:48:03 -08002408 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002409 }
rginda87b86462011-12-14 13:48:03 -08002410};
rginda8ba33642011-12-14 12:31:31 -08002411
Evan Jones2600d4f2016-12-06 09:29:36 -05002412/**
2413 * Move the cursor relative to its current position.
2414 *
2415 * @param {number} row
2416 * @param {number} column
2417 */
rginda87b86462011-12-14 13:48:03 -08002418hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2419 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002420 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2421 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002422 this.screen_.setCursorPosition(row, column);
2423};
2424
Evan Jones2600d4f2016-12-06 09:29:36 -05002425/**
2426 * Move the cursor to the specified position.
2427 *
2428 * @param {number} row
2429 * @param {number} column
2430 */
rginda87b86462011-12-14 13:48:03 -08002431hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002432 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2433 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002434 this.screen_.setCursorPosition(row, column);
2435};
2436
2437/**
2438 * Set the cursor column.
2439 *
Joel Hockey0f933582019-08-27 18:01:51 -07002440 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002441 */
2442hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002443 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002444};
2445
2446/**
2447 * Return the cursor column.
2448 *
Joel Hockey0f933582019-08-27 18:01:51 -07002449 * @return {number} The zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002450 */
2451hterm.Terminal.prototype.getCursorColumn = function() {
2452 return this.screen_.cursorPosition.column;
2453};
2454
2455/**
2456 * Set the cursor row.
2457 *
2458 * The cursor row is relative to the scroll region if the terminal has
2459 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2460 *
Joel Hockey0f933582019-08-27 18:01:51 -07002461 * @param {number} row The new cursor row.
rginda8ba33642011-12-14 12:31:31 -08002462 */
rginda87b86462011-12-14 13:48:03 -08002463hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2464 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002465};
2466
2467/**
2468 * Return the cursor row.
2469 *
Joel Hockey0f933582019-08-27 18:01:51 -07002470 * @return {number} The zero-based cursor row.
rginda8ba33642011-12-14 12:31:31 -08002471 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002472hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002473 return this.screen_.cursorPosition.row;
2474};
2475
2476/**
2477 * Request that the ScrollPort redraw itself soon.
2478 *
2479 * The redraw will happen asynchronously, soon after the call stack winds down.
2480 * Multiple calls will be coalesced into a single redraw.
2481 */
2482hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002483 if (this.timeouts_.redraw)
2484 return;
rginda8ba33642011-12-14 12:31:31 -08002485
2486 var self = this;
rginda87b86462011-12-14 13:48:03 -08002487 this.timeouts_.redraw = setTimeout(function() {
2488 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002489 self.scrollPort_.redraw_();
2490 }, 0);
2491};
2492
2493/**
2494 * Request that the ScrollPort be scrolled to the bottom.
2495 *
2496 * The scroll will happen asynchronously, soon after the call stack winds down.
2497 * Multiple calls will be coalesced into a single scroll.
2498 *
2499 * This affects the scrollbar position of the ScrollPort, and has nothing to
2500 * do with the VT scroll commands.
2501 */
2502hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2503 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002504 return;
rginda8ba33642011-12-14 12:31:31 -08002505
2506 var self = this;
2507 this.timeouts_.scrollDown = setTimeout(function() {
2508 delete self.timeouts_.scrollDown;
2509 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2510 }, 10);
2511};
2512
2513/**
2514 * Move the cursor up a specified number of rows.
2515 *
Joel Hockey0f933582019-08-27 18:01:51 -07002516 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002517 */
2518hterm.Terminal.prototype.cursorUp = function(count) {
Joel Hockey0f933582019-08-27 18:01:51 -07002519 this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002520};
2521
2522/**
2523 * Move the cursor down a specified number of rows.
2524 *
Joel Hockey0f933582019-08-27 18:01:51 -07002525 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002526 */
2527hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002528 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002529 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2530 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2531 this.screenSize.height - 1);
2532
rgindacbbd7482012-06-13 15:06:16 -07002533 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002534 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002535 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002536};
2537
2538/**
2539 * Move the cursor left a specified number of columns.
2540 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002541 * If reverse wraparound mode is enabled and the previous row wrapped into
2542 * the current row then we back up through the wraparound as well.
2543 *
Joel Hockey0f933582019-08-27 18:01:51 -07002544 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002545 */
2546hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002547 count = count || 1;
2548
2549 if (count < 1)
2550 return;
2551
2552 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002553 if (this.options_.reverseWraparound) {
2554 if (this.screen_.cursorPosition.overflow) {
2555 // If this cursor is in the right margin, consume one count to get it
2556 // back to the last column. This only applies when we're in reverse
2557 // wraparound mode.
2558 count--;
2559 this.clearCursorOverflow();
2560
2561 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002562 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002563 }
2564
Robert Gindabfb32622014-07-17 13:20:27 -07002565 var newRow = this.screen_.cursorPosition.row;
2566 var newColumn = currentColumn - count;
2567 if (newColumn < 0) {
2568 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2569 if (newRow < 0) {
2570 // xterm also wraps from row 0 to the last row.
2571 newRow = this.screenSize.height + newRow % this.screenSize.height;
2572 }
2573 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2574 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002575
Robert Gindabfb32622014-07-17 13:20:27 -07002576 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2577
2578 } else {
2579 var newColumn = Math.max(currentColumn - count, 0);
2580 this.setCursorColumn(newColumn);
2581 }
rginda8ba33642011-12-14 12:31:31 -08002582};
2583
2584/**
2585 * Move the cursor right a specified number of columns.
2586 *
Joel Hockey0f933582019-08-27 18:01:51 -07002587 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002588 */
2589hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002590 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002591
2592 if (count < 1)
2593 return;
2594
rgindacbbd7482012-06-13 15:06:16 -07002595 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002596 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002597 this.setCursorColumn(column);
2598};
2599
2600/**
2601 * Reverse the foreground and background colors of the terminal.
2602 *
2603 * This only affects text that was drawn with no attributes.
2604 *
2605 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2606 * been drawn with attributes that happen to coincide with the default
2607 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002608 *
2609 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002610 */
2611hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002612 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002613 if (state) {
Mike Frysinger31cb1562017-07-31 23:44:18 -04002614 this.scrollPort_.setForegroundColor(this.backgroundColor_);
2615 this.scrollPort_.setBackgroundColor(this.foregroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002616 } else {
Mike Frysinger31cb1562017-07-31 23:44:18 -04002617 this.scrollPort_.setForegroundColor(this.foregroundColor_);
2618 this.scrollPort_.setBackgroundColor(this.backgroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002619 }
2620};
2621
2622/**
rginda87b86462011-12-14 13:48:03 -08002623 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002624 *
2625 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002626 */
2627hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002628 this.cursorNode_.style.backgroundColor =
2629 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002630
2631 var self = this;
2632 setTimeout(function() {
Matheus Fernandes2d733082017-09-11 06:43:01 -04002633 self.restyleCursor_();
rginda6d397402012-01-17 10:58:29 -08002634 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002635
Michael Kelly485ecd12014-06-09 11:41:56 -04002636 // bellSquelchTimeout_ affects both audio and notification bells.
2637 if (this.bellSquelchTimeout_)
2638 return;
2639
Robert Ginda92e18102013-03-14 13:56:37 -07002640 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002641 this.bellAudio_.play();
Joel Hockeyd4fca732019-09-20 16:57:03 -07002642 this.bellSequelchTimeout_ = setTimeout(() => {
2643 this.bellSquelchTimeout_ = null;
2644 }, 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002645 } else {
Joel Hockeyd4fca732019-09-20 16:57:03 -07002646 this.bellSquelchTimeout_ = null;
Robert Ginda92e18102013-03-14 13:56:37 -07002647 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002648
2649 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingera5fb83c2017-06-22 14:48:35 -07002650 var n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002651 this.bellNotificationList_.push(n);
2652 // TODO: Should we try to raise the window here?
2653 n.onclick = function() { self.closeBellNotifications_(); };
2654 }
rginda87b86462011-12-14 13:48:03 -08002655};
2656
2657/**
rginda8ba33642011-12-14 12:31:31 -08002658 * Set the origin mode bit.
2659 *
2660 * If origin mode is on, certain VT cursor and scrolling commands measure their
2661 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2662 * to the top of the addressable screen.
2663 *
2664 * Defaults to off.
2665 *
2666 * @param {boolean} state True to set origin mode, false to unset.
2667 */
2668hterm.Terminal.prototype.setOriginMode = function(state) {
2669 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002670 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002671};
2672
2673/**
2674 * Set the insert mode bit.
2675 *
2676 * If insert mode is on, existing text beyond the cursor position will be
2677 * shifted right to make room for new text. Otherwise, new text overwrites
2678 * any existing text.
2679 *
2680 * Defaults to off.
2681 *
2682 * @param {boolean} state True to set insert mode, false to unset.
2683 */
2684hterm.Terminal.prototype.setInsertMode = function(state) {
2685 this.options_.insertMode = state;
2686};
2687
2688/**
rginda87b86462011-12-14 13:48:03 -08002689 * Set the auto carriage return bit.
2690 *
2691 * If auto carriage return is on then a formfeed character is interpreted
2692 * as a newline, otherwise it's the same as a linefeed. The difference boils
2693 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002694 *
2695 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002696 */
2697hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2698 this.options_.autoCarriageReturn = state;
2699};
2700
2701/**
rginda8ba33642011-12-14 12:31:31 -08002702 * Set the wraparound mode bit.
2703 *
2704 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2705 * to the start of the following row. Otherwise, the cursor is clamped to the
2706 * end of the screen and attempts to write past it are ignored.
2707 *
2708 * Defaults to on.
2709 *
2710 * @param {boolean} state True to set wraparound mode, false to unset.
2711 */
2712hterm.Terminal.prototype.setWraparound = function(state) {
2713 this.options_.wraparound = state;
2714};
2715
2716/**
2717 * Set the reverse-wraparound mode bit.
2718 *
2719 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2720 * to the end of the previous row. Otherwise, the cursor is clamped to column
2721 * 0.
2722 *
2723 * Defaults to off.
2724 *
2725 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2726 */
2727hterm.Terminal.prototype.setReverseWraparound = function(state) {
2728 this.options_.reverseWraparound = state;
2729};
2730
2731/**
2732 * Selects between the primary and alternate screens.
2733 *
2734 * If alternate mode is on, the alternate screen is active. Otherwise the
2735 * primary screen is active.
2736 *
2737 * Swapping screens has no effect on the scrollback buffer.
2738 *
2739 * Each screen maintains its own cursor position.
2740 *
2741 * Defaults to off.
2742 *
2743 * @param {boolean} state True to set alternate mode, false to unset.
2744 */
2745hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002746 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002747 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2748
rginda35c456b2012-02-09 17:29:05 -08002749 if (this.screen_.rowsArray.length &&
2750 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2751 // If the screen changed sizes while we were away, our rowIndexes may
2752 // be incorrect.
2753 var offset = this.scrollbackRows_.length;
2754 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002755 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002756 ary[i].rowIndex = offset + i;
2757 }
2758 }
rginda8ba33642011-12-14 12:31:31 -08002759
rginda35c456b2012-02-09 17:29:05 -08002760 this.realizeWidth_(this.screenSize.width);
2761 this.realizeHeight_(this.screenSize.height);
2762 this.scrollPort_.syncScrollHeight();
2763 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002764
rginda6d397402012-01-17 10:58:29 -08002765 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002766 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002767};
2768
2769/**
2770 * Set the cursor-blink mode bit.
2771 *
2772 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2773 * a visible cursor does not blink.
2774 *
2775 * You should make sure to turn blinking off if you're going to dispose of a
2776 * terminal, otherwise you'll leak a timeout.
2777 *
2778 * Defaults to on.
2779 *
2780 * @param {boolean} state True to set cursor-blink mode, false to unset.
2781 */
2782hterm.Terminal.prototype.setCursorBlink = function(state) {
2783 this.options_.cursorBlink = state;
2784
2785 if (!state && this.timeouts_.cursorBlink) {
2786 clearTimeout(this.timeouts_.cursorBlink);
2787 delete this.timeouts_.cursorBlink;
2788 }
2789
2790 if (this.options_.cursorVisible)
2791 this.setCursorVisible(true);
2792};
2793
2794/**
2795 * Set the cursor-visible mode bit.
2796 *
2797 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2798 *
2799 * Defaults to on.
2800 *
2801 * @param {boolean} state True to set cursor-visible mode, false to unset.
2802 */
2803hterm.Terminal.prototype.setCursorVisible = function(state) {
2804 this.options_.cursorVisible = state;
2805
2806 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002807 if (this.timeouts_.cursorBlink) {
2808 clearTimeout(this.timeouts_.cursorBlink);
2809 delete this.timeouts_.cursorBlink;
2810 }
rginda87b86462011-12-14 13:48:03 -08002811 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002812 return;
2813 }
2814
rginda87b86462011-12-14 13:48:03 -08002815 this.syncCursorPosition_();
2816
2817 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002818
2819 if (this.options_.cursorBlink) {
2820 if (this.timeouts_.cursorBlink)
2821 return;
2822
Robert Gindaea2183e2014-07-17 09:51:51 -07002823 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002824 } else {
2825 if (this.timeouts_.cursorBlink) {
2826 clearTimeout(this.timeouts_.cursorBlink);
2827 delete this.timeouts_.cursorBlink;
2828 }
2829 }
2830};
2831
2832/**
rginda87b86462011-12-14 13:48:03 -08002833 * Synchronizes the visible cursor and document selection with the current
2834 * cursor coordinates.
Raymes Khourye5d48982018-08-02 09:08:32 +10002835 *
2836 * @return {boolean} True if the cursor is onscreen and synced.
rginda8ba33642011-12-14 12:31:31 -08002837 */
2838hterm.Terminal.prototype.syncCursorPosition_ = function() {
2839 var topRowIndex = this.scrollPort_.getTopRowIndex();
2840 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2841 var cursorRowIndex = this.scrollbackRows_.length +
2842 this.screen_.cursorPosition.row;
2843
Raymes Khoury15697f42018-07-17 11:37:18 +10002844 let forceSyncSelection = false;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002845 if (this.accessibilityReader_.accessibilityEnabled) {
2846 // Report the new position of the cursor for accessibility purposes.
2847 const cursorColumnIndex = this.screen_.cursorPosition.column;
2848 const cursorLineText =
2849 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
Raymes Khoury15697f42018-07-17 11:37:18 +10002850 // This will force the selection to be sync'd to the cursor position if the
2851 // user has pressed a key. Generally we would only sync the cursor position
2852 // when selection is collapsed so that if the user has selected something
2853 // we don't clear the selection by moving the selection. However when a
2854 // screen reader is used, it's intuitive for entering a key to move the
2855 // selection to the cursor.
2856 forceSyncSelection = this.accessibilityReader_.hasUserGesture;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002857 this.accessibilityReader_.afterCursorChange(
2858 cursorLineText, cursorRowIndex, cursorColumnIndex);
2859 }
2860
rginda8ba33642011-12-14 12:31:31 -08002861 if (cursorRowIndex > bottomRowIndex) {
2862 // Cursor is scrolled off screen, move it outside of the visible area.
Mike Frysinger44c32202017-08-05 01:13:09 -04002863 this.setCssVar('cursor-offset-row', '-1');
Raymes Khourye5d48982018-08-02 09:08:32 +10002864 return false;
rginda8ba33642011-12-14 12:31:31 -08002865 }
2866
Robert Gindab837c052014-08-11 11:17:51 -07002867 if (this.options_.cursorVisible &&
2868 this.cursorNode_.style.display == 'none') {
2869 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2870 this.cursorNode_.style.display = '';
2871 }
2872
Mike Frysinger44c32202017-08-05 01:13:09 -04002873 // Position the cursor using CSS variable math. If we do the math in JS,
2874 // the float math will end up being more precise than the CSS which will
2875 // cause the cursor tracking to be off.
2876 this.setCssVar(
2877 'cursor-offset-row',
2878 `${cursorRowIndex - topRowIndex} + ` +
2879 `${this.scrollPort_.visibleRowTopMargin}px`);
2880 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08002881
2882 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04002883 '(' + this.screen_.cursorPosition.column +
2884 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08002885 ')');
2886
2887 // Update the caret for a11y purposes.
2888 var selection = this.document_.getSelection();
Raymes Khoury15697f42018-07-17 11:37:18 +10002889 if (selection && (selection.isCollapsed || forceSyncSelection)) {
rginda87b86462011-12-14 13:48:03 -08002890 this.screen_.syncSelectionCaret(selection);
Raymes Khoury15697f42018-07-17 11:37:18 +10002891 }
Raymes Khourye5d48982018-08-02 09:08:32 +10002892 return true;
rginda8ba33642011-12-14 12:31:31 -08002893};
2894
Robert Gindafb1be6a2013-12-11 11:56:22 -08002895/**
2896 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2897 * and character cell dimensions.
2898 */
Robert Ginda830583c2013-08-07 13:20:46 -07002899hterm.Terminal.prototype.restyleCursor_ = function() {
2900 var shape = this.cursorShape_;
2901
2902 if (this.cursorNode_.getAttribute('focus') == 'false') {
2903 // Always show a block cursor when unfocused.
2904 shape = hterm.Terminal.cursorShape.BLOCK;
2905 }
2906
2907 var style = this.cursorNode_.style;
2908
2909 switch (shape) {
2910 case hterm.Terminal.cursorShape.BEAM:
Robert Ginda830583c2013-08-07 13:20:46 -07002911 style.backgroundColor = 'transparent';
Joel Hockeyd4fca732019-09-20 16:57:03 -07002912 style.borderBottomStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07002913 style.borderLeftStyle = 'solid';
2914 break;
2915
2916 case hterm.Terminal.cursorShape.UNDERLINE:
Robert Ginda830583c2013-08-07 13:20:46 -07002917 style.backgroundColor = 'transparent';
2918 style.borderBottomStyle = 'solid';
Joel Hockeyd4fca732019-09-20 16:57:03 -07002919 style.borderLeftStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07002920 break;
2921
2922 default:
Mike Frysinger2fd079a2018-09-02 01:46:12 -04002923 style.backgroundColor = 'var(--hterm-cursor-color)';
Joel Hockeyd4fca732019-09-20 16:57:03 -07002924 style.borderBottomStyle = '';
2925 style.borderLeftStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07002926 break;
2927 }
2928};
2929
rginda8ba33642011-12-14 12:31:31 -08002930/**
2931 * Synchronizes the visible cursor with the current cursor coordinates.
2932 *
2933 * The sync will happen asynchronously, soon after the call stack winds down.
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002934 * Multiple calls will be coalesced into a single sync. This should be called
2935 * prior to the cursor actually changing position.
rginda8ba33642011-12-14 12:31:31 -08002936 */
2937hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2938 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002939 return;
rginda8ba33642011-12-14 12:31:31 -08002940
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002941 if (this.accessibilityReader_.accessibilityEnabled) {
2942 // Report the previous position of the cursor for accessibility purposes.
2943 const cursorRowIndex = this.scrollbackRows_.length +
2944 this.screen_.cursorPosition.row;
2945 const cursorColumnIndex = this.screen_.cursorPosition.column;
2946 const cursorLineText =
2947 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
2948 this.accessibilityReader_.beforeCursorChange(
2949 cursorLineText, cursorRowIndex, cursorColumnIndex);
2950 }
2951
rginda8ba33642011-12-14 12:31:31 -08002952 var self = this;
2953 this.timeouts_.syncCursor = setTimeout(function() {
2954 self.syncCursorPosition_();
2955 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002956 }, 0);
2957};
2958
rgindacc2996c2012-02-24 14:59:31 -08002959/**
rgindaf522ce02012-04-17 17:49:17 -07002960 * Show or hide the zoom warning.
2961 *
2962 * The zoom warning is a message warning the user that their browser zoom must
2963 * be set to 100% in order for hterm to function properly.
2964 *
2965 * @param {boolean} state True to show the message, false to hide it.
2966 */
2967hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2968 if (!this.zoomWarningNode_) {
2969 if (!state)
2970 return;
2971
2972 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002973 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07002974 this.zoomWarningNode_.style.cssText = (
2975 'color: black;' +
2976 'background-color: #ff2222;' +
2977 'font-size: large;' +
2978 'border-radius: 8px;' +
2979 'opacity: 0.75;' +
2980 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2981 'top: 0.5em;' +
2982 'right: 1.2em;' +
2983 'position: absolute;' +
2984 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002985 '-webkit-user-select: none;' +
2986 '-moz-text-size-adjust: none;' +
2987 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05002988
2989 this.zoomWarningNode_.addEventListener('click', function(e) {
2990 this.parentNode.removeChild(this);
2991 });
rgindaf522ce02012-04-17 17:49:17 -07002992 }
2993
Mike Frysingerb7289952019-03-23 16:05:38 -07002994 this.zoomWarningNode_.textContent = lib.i18n.replaceReferences(
Robert Gindab4839c22013-02-28 16:52:10 -08002995 hterm.zoomWarningMessage,
Joel Hockeyd4fca732019-09-20 16:57:03 -07002996 [Math.floor(this.scrollPort_.characterSize.zoomFactor * 100)]);
Robert Gindab4839c22013-02-28 16:52:10 -08002997
rgindaf522ce02012-04-17 17:49:17 -07002998 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2999
3000 if (state) {
3001 if (!this.zoomWarningNode_.parentNode)
3002 this.div_.parentNode.appendChild(this.zoomWarningNode_);
3003 } else if (this.zoomWarningNode_.parentNode) {
3004 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
3005 }
3006};
3007
3008/**
rgindacc2996c2012-02-24 14:59:31 -08003009 * Show the terminal overlay for a given amount of time.
3010 *
3011 * The terminal overlay appears in inverse video in a large font, centered
3012 * over the terminal. You should probably keep the overlay message brief,
3013 * since it's in a large font and you probably aren't going to check the size
3014 * of the terminal first.
3015 *
3016 * @param {string} msg The text (not HTML) message to display in the overlay.
Joel Hockey0f933582019-08-27 18:01:51 -07003017 * @param {number=} opt_timeout The amount of time to wait before fading out
rgindacc2996c2012-02-24 14:59:31 -08003018 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
3019 * stay up forever (or until the next overlay).
3020 */
3021hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08003022 if (!this.overlayNode_) {
3023 if (!this.div_)
3024 return;
3025
3026 this.overlayNode_ = this.document_.createElement('div');
3027 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08003028 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08003029 'font-size: xx-large;' +
3030 'opacity: 0.75;' +
3031 'padding: 0.2em 0.5em 0.2em 0.5em;' +
3032 'position: absolute;' +
3033 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003034 '-webkit-transition: opacity 180ms ease-in;' +
3035 '-moz-user-select: none;' +
3036 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08003037
3038 this.overlayNode_.addEventListener('mousedown', function(e) {
3039 e.preventDefault();
3040 e.stopPropagation();
3041 }, true);
rgindaf0090c92012-02-10 14:58:52 -08003042 }
3043
rginda9f5222b2012-03-05 11:53:28 -08003044 this.overlayNode_.style.color = this.prefs_.get('background-color');
3045 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
3046 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
3047
rgindaf0090c92012-02-10 14:58:52 -08003048 this.overlayNode_.textContent = msg;
3049 this.overlayNode_.style.opacity = '0.75';
3050
3051 if (!this.overlayNode_.parentNode)
3052 this.div_.appendChild(this.overlayNode_);
3053
Joel Hockeyd4fca732019-09-20 16:57:03 -07003054 var divSize = hterm.getClientSize(lib.notNull(this.div_));
Robert Ginda97769282013-02-01 15:30:30 -08003055 var overlaySize = hterm.getClientSize(this.overlayNode_);
3056
Robert Ginda8a59f762014-07-23 11:29:55 -07003057 this.overlayNode_.style.top =
3058 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08003059 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07003060 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08003061
rgindaf0090c92012-02-10 14:58:52 -08003062 if (this.overlayTimeout_)
3063 clearTimeout(this.overlayTimeout_);
3064
Raymes Khouryc7a06382018-07-04 10:25:45 +10003065 this.accessibilityReader_.assertiveAnnounce(msg);
3066
rgindacc2996c2012-02-24 14:59:31 -08003067 if (opt_timeout === null)
3068 return;
3069
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003070 this.overlayTimeout_ = setTimeout(() => {
3071 this.overlayNode_.style.opacity = '0';
3072 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
3073 }, opt_timeout || 1500);
3074};
3075
3076/**
3077 * Hide the terminal overlay immediately.
3078 *
3079 * Useful when we show an overlay for an event with an unknown end time.
3080 */
3081hterm.Terminal.prototype.hideOverlay = function() {
3082 if (this.overlayTimeout_)
3083 clearTimeout(this.overlayTimeout_);
3084 this.overlayTimeout_ = null;
3085
3086 if (this.overlayNode_.parentNode)
3087 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
3088 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08003089};
3090
rginda4bba5e12012-06-20 16:15:30 -07003091/**
3092 * Paste from the system clipboard to the terminal.
Joel Hockey0f933582019-08-27 18:01:51 -07003093 * @return {boolean}
rginda4bba5e12012-06-20 16:15:30 -07003094 */
3095hterm.Terminal.prototype.paste = function() {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003096 return hterm.pasteFromClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07003097};
3098
3099/**
3100 * Copy a string to the system clipboard.
3101 *
3102 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05003103 *
3104 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07003105 */
3106hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08003107 if (this.prefs_.get('enable-clipboard-notice'))
3108 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
3109
Mike Frysinger96eacae2019-01-02 18:13:56 -05003110 hterm.copySelectionToClipboard(this.document_, str);
rginda4bba5e12012-06-20 16:15:30 -07003111};
3112
Evan Jones2600d4f2016-12-06 09:29:36 -05003113/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003114 * Display an image.
3115 *
Mike Frysinger2558ed52019-01-14 01:03:41 -05003116 * Either URI or buffer or blob fields must be specified.
3117 *
Joel Hockey0f933582019-08-27 18:01:51 -07003118 * @param {{
3119 * name: (string|undefined),
3120 * size: (string|number|undefined),
3121 * preserveAspectRation: (boolean|undefined),
3122 * inline: (boolean|undefined),
3123 * width: (string|number|undefined),
3124 * height: (string|number|undefined),
3125 * align: (string|undefined),
3126 * url: (string|undefined),
3127 * buffer: (!ArrayBuffer|undefined),
3128 * blob: (!Blob|undefined),
3129 * type: (string|undefined),
3130 * }} options The image to display.
3131 * name A human readable string for the image
3132 * size The size (in bytes).
3133 * preserveAspectRatio Whether to preserve aspect.
3134 * inline Whether to display the image inline.
3135 * width The width of the image.
3136 * height The height of the image.
3137 * align Direction to align the image.
3138 * uri The source URI for the image.
3139 * buffer The ArrayBuffer image data.
3140 * blob The Blob image data.
3141 * type The MIME type of the image data.
3142 * @param {function()=} onLoad Callback when loading finishes.
3143 * @param {function(!Event)=} onError Callback when loading fails.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003144 */
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003145hterm.Terminal.prototype.displayImage = function(options, onLoad, onError) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003146 // Make sure we're actually given a resource to display.
Mike Frysinger2558ed52019-01-14 01:03:41 -05003147 if (options.uri === undefined && options.buffer === undefined &&
3148 options.blob === undefined)
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003149 return;
3150
3151 // Set up the defaults to simplify code below.
3152 if (!options.name)
3153 options.name = '';
3154
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003155 // See if the mime type is available. If not, guess from the filename.
3156 // We don't list all possible mime types because the browser can usually
3157 // guess it correctly. So list the ones that need a bit more help.
3158 if (!options.type) {
3159 const ary = options.name.split('.');
3160 const ext = ary[ary.length - 1].trim();
3161 switch (ext) {
3162 case 'svg':
3163 case 'svgz':
3164 options.type = 'image/svg+xml';
3165 break;
3166 }
3167 }
3168
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003169 // Has the user approved image display yet?
3170 if (this.allowImagesInline !== true) {
3171 this.newLine();
3172 const row = this.getRowNode(this.scrollbackRows_.length +
3173 this.getCursorRow() - 1);
3174
3175 if (this.allowImagesInline === false) {
3176 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
3177 'Inline Images Disabled');
3178 return;
3179 }
3180
3181 // Show a prompt.
3182 let button;
3183 const span = this.document_.createElement('span');
3184 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
3185 span.style.fontWeight = 'bold';
3186 span.style.borderWidth = '1px';
3187 span.style.borderStyle = 'dashed';
3188 button = this.document_.createElement('span');
3189 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3190 button.style.marginLeft = '1em';
3191 button.style.borderWidth = '1px';
3192 button.style.borderStyle = 'solid';
3193 button.addEventListener('click', () => {
3194 this.prefs_.set('allow-images-inline', false);
3195 });
3196 span.appendChild(button);
3197 button = this.document_.createElement('span');
3198 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3199 'allow this session');
3200 button.style.marginLeft = '1em';
3201 button.style.borderWidth = '1px';
3202 button.style.borderStyle = 'solid';
3203 button.addEventListener('click', () => {
3204 this.allowImagesInline = true;
3205 });
3206 span.appendChild(button);
3207 button = this.document_.createElement('span');
3208 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3209 button.style.marginLeft = '1em';
3210 button.style.borderWidth = '1px';
3211 button.style.borderStyle = 'solid';
3212 button.addEventListener('click', () => {
3213 this.prefs_.set('allow-images-inline', true);
3214 });
3215 span.appendChild(button);
3216
3217 row.appendChild(span);
3218 return;
3219 }
3220
3221 // See if we should show this object directly, or download it.
3222 if (options.inline) {
3223 const io = this.io.push();
3224 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
Joel Hockeyd4fca732019-09-20 16:57:03 -07003225 'Loading $1 ...'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003226
3227 // While we're loading the image, eat all the user's input.
3228 io.onVTKeystroke = io.sendString = () => {};
3229
3230 // Initialize this new image.
Joel Hockeyd4fca732019-09-20 16:57:03 -07003231 const img = this.document_.createElement('img');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003232 if (options.uri !== undefined) {
3233 img.src = options.uri;
3234 } else if (options.buffer !== undefined) {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003235 const blob = new Blob([options.buffer], {type: options.type});
Mike Frysinger2558ed52019-01-14 01:03:41 -05003236 img.src = URL.createObjectURL(blob);
3237 } else {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003238 const blob = new Blob([options.blob], {type: options.type});
Joel Hockeyd4fca732019-09-20 16:57:03 -07003239 img.src = URL.createObjectURL(blob);
Mike Frysinger2558ed52019-01-14 01:03:41 -05003240 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003241 img.title = img.alt = options.name;
3242
3243 // Attach the image to the page to let it load/render. It won't stay here.
3244 // This is needed so it's visible and the DOM can calculate the height. If
3245 // the image is hidden or not in the DOM, the height is always 0.
3246 this.document_.body.appendChild(img);
3247
3248 // Wait for the image to finish loading before we try moving it to the
3249 // right place in the terminal.
3250 img.onload = () => {
3251 // Now that we have the image dimensions, figure out how to show it.
3252 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
3253 img.style.maxWidth = `${this.document_.body.clientWidth}px`;
3254 img.style.maxHeight = `${this.document_.body.clientHeight}px`;
3255
3256 // Parse a width/height specification.
3257 const parseDim = (dim, maxDim, cssVar) => {
3258 if (!dim || dim == 'auto')
3259 return '';
3260
3261 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3262 if (ary) {
3263 if (ary[2] == '%')
Joel Hockeyd4fca732019-09-20 16:57:03 -07003264 return Math.floor(maxDim * ary[1] / 100) + 'px';
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003265 else if (ary[2] == 'px')
3266 return dim;
3267 else
3268 return `calc(${dim} * var(${cssVar}))`;
3269 }
3270
3271 return '';
3272 };
3273 img.style.width =
3274 parseDim(options.width, this.document_.body.clientWidth,
3275 '--hterm-charsize-width');
3276 img.style.height =
3277 parseDim(options.height, this.document_.body.clientHeight,
3278 '--hterm-charsize-height');
3279
3280 // Figure out how many rows the image occupies, then add that many.
Mike Frysingera0349392019-09-11 05:38:09 -04003281 // Note: This count will be inaccurate if the font size changes on us.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003282 const padRows = Math.ceil(img.clientHeight /
3283 this.scrollPort_.characterSize.height);
3284 for (let i = 0; i < padRows; ++i)
3285 this.newLine();
3286
3287 // Update the max height in case the user shrinks the character size.
3288 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3289
3290 // Move the image to the last row. This way when we scroll up, it doesn't
3291 // disappear when the first row gets clipped. It will disappear when we
3292 // scroll down and the last row is clipped ...
3293 this.document_.body.removeChild(img);
3294 // Create a wrapper node so we can do an absolute in a relative position.
3295 // This helps with rounding errors between JS & CSS counts.
3296 const div = this.document_.createElement('div');
3297 div.style.position = 'relative';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003298 div.style.textAlign = options.align || '';
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003299 img.style.position = 'absolute';
3300 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3301 div.appendChild(img);
3302 const row = this.getRowNode(this.scrollbackRows_.length +
3303 this.getCursorRow() - 1);
3304 row.appendChild(div);
3305
Mike Frysinger2558ed52019-01-14 01:03:41 -05003306 // Now that the image has been read, we can revoke the source.
3307 if (options.uri === undefined) {
3308 URL.revokeObjectURL(img.src);
3309 }
3310
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003311 io.hideOverlay();
3312 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003313
3314 if (onLoad)
3315 onLoad();
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003316 };
3317
3318 // If we got a malformed image, give up.
3319 img.onerror = (e) => {
3320 this.document_.body.removeChild(img);
3321 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
Mike Frysingere14a8c42018-03-10 00:17:30 -08003322 'Loading $1 failed'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003323 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003324
3325 if (onError)
3326 onError(e);
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003327 };
3328 } else {
3329 // We can't use chrome.downloads.download as that requires "downloads"
3330 // permissions, and that works only in extensions, not apps.
3331 const a = this.document_.createElement('a');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003332 if (options.uri !== undefined) {
3333 a.href = options.uri;
3334 } else if (options.buffer !== undefined) {
3335 const blob = new Blob([options.buffer]);
3336 a.href = URL.createObjectURL(blob);
3337 } else {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003338 a.href = URL.createObjectURL(lib.notNull(options.blob));
Mike Frysinger2558ed52019-01-14 01:03:41 -05003339 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003340 a.download = options.name;
3341 this.document_.body.appendChild(a);
3342 a.click();
3343 a.remove();
Mike Frysinger2558ed52019-01-14 01:03:41 -05003344 if (options.uri === undefined) {
3345 URL.revokeObjectURL(a.href);
3346 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003347 }
3348};
3349
3350/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003351 * Returns the selected text, or null if no text is selected.
3352 *
3353 * @return {string|null}
3354 */
rgindaa09e7332012-08-17 12:49:51 -07003355hterm.Terminal.prototype.getSelectionText = function() {
3356 var selection = this.scrollPort_.selection;
3357 selection.sync();
3358
3359 if (selection.isCollapsed)
3360 return null;
3361
rgindaa09e7332012-08-17 12:49:51 -07003362 // Start offset measures from the beginning of the line.
3363 var startOffset = selection.startOffset;
3364 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003365
Raymes Khoury334625a2018-06-25 10:29:40 +10003366 // If an x-row isn't selected, |node| will be null.
3367 if (!node)
3368 return null;
3369
Robert Gindafdbb3f22012-09-06 20:23:06 -07003370 if (node.nodeName != 'X-ROW') {
3371 // If the selection doesn't start on an x-row node, then it must be
3372 // somewhere inside the x-row. Add any characters from previous siblings
3373 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003374
3375 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3376 // If node is the text node in a styled span, move up to the span node.
3377 node = node.parentNode;
3378 }
3379
Robert Gindafdbb3f22012-09-06 20:23:06 -07003380 while (node.previousSibling) {
3381 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003382 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003383 }
rgindaa09e7332012-08-17 12:49:51 -07003384 }
3385
3386 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08003387 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
3388 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003389 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003390
Robert Gindafdbb3f22012-09-06 20:23:06 -07003391 if (node.nodeName != 'X-ROW') {
3392 // If the selection doesn't end on an x-row node, then it must be
3393 // somewhere inside the x-row. Add any characters from following siblings
3394 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003395
3396 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3397 // If node is the text node in a styled span, move up to the span node.
3398 node = node.parentNode;
3399 }
3400
Robert Gindafdbb3f22012-09-06 20:23:06 -07003401 while (node.nextSibling) {
3402 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003403 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003404 }
rgindaa09e7332012-08-17 12:49:51 -07003405 }
3406
3407 var rv = this.getRowsText(selection.startRow.rowIndex,
3408 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003409 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003410};
3411
rginda4bba5e12012-06-20 16:15:30 -07003412/**
3413 * Copy the current selection to the system clipboard, then clear it after a
3414 * short delay.
3415 */
3416hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07003417 var text = this.getSelectionText();
3418 if (text != null)
3419 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07003420};
3421
Joel Hockey0f933582019-08-27 18:01:51 -07003422/**
3423 * Show overlay with current terminal size.
3424 */
rgindaf0090c92012-02-10 14:58:52 -08003425hterm.Terminal.prototype.overlaySize = function() {
Theodore Duboisdd5f9a72019-09-06 23:28:42 -07003426 if (this.prefs_.get('enable-resize-status')) {
3427 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
3428 }
rgindaf0090c92012-02-10 14:58:52 -08003429};
3430
rginda87b86462011-12-14 13:48:03 -08003431/**
3432 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3433 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003434 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003435 */
3436hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08003437 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08003438 this.scrollPort_.scrollRowToBottom(this.getRowCount());
3439
Mike Frysinger79669762018-12-30 20:51:10 -05003440 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08003441};
3442
3443/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003444 * Open the selected url.
3445 */
3446hterm.Terminal.prototype.openSelectedUrl_ = function() {
3447 var str = this.getSelectionText();
3448
3449 // If there is no selection, try and expand wherever they clicked.
3450 if (str == null) {
John Lincae9b732018-03-08 13:56:35 +08003451 this.screen_.expandSelectionForUrl(this.document_.getSelection());
Mike Frysinger70b94692017-01-26 18:57:50 -10003452 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003453
3454 // If clicking in empty space, return.
3455 if (str == null)
3456 return;
Mike Frysinger70b94692017-01-26 18:57:50 -10003457 }
3458
3459 // Make sure URL is valid before opening.
3460 if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0)
3461 return;
Mike Frysinger43472622017-06-26 18:11:07 -04003462
3463 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003464 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003465 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3466 // We have to whitelist a few protocols that lack authorities and thus
3467 // never use the //. Like mailto.
3468 switch (str.split(':', 1)[0]) {
3469 case 'mailto':
3470 break;
3471 default:
3472 str = 'http://' + str;
3473 break;
3474 }
3475 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003476
Mike Frysinger720fa832017-10-23 01:15:52 -04003477 hterm.openUrl(str);
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003478};
Mike Frysinger70b94692017-01-26 18:57:50 -10003479
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003480/**
3481 * Manage the automatic mouse hiding behavior while typing.
3482 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003483 * @param {?boolean=} v Whether to enable automatic hiding.
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003484 */
3485hterm.Terminal.prototype.setAutomaticMouseHiding = function(v=null) {
3486 // Since Chrome OS & macOS do this by default everywhere, we don't need to.
3487 // Linux & Windows seem to leave this to specific applications to manage.
3488 if (v === null)
3489 v = (hterm.os != 'cros' && hterm.os != 'mac');
3490
3491 this.mouseHideWhileTyping_ = !!v;
3492};
3493
3494/**
3495 * Handler for monitoring user keyboard activity.
3496 *
3497 * This isn't for processing the keystrokes directly, but for updating any
3498 * state that might toggle based on the user using the keyboard at all.
3499 *
Joel Hockey0f933582019-08-27 18:01:51 -07003500 * @param {!KeyboardEvent} e The keyboard event that triggered us.
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003501 */
3502hterm.Terminal.prototype.onKeyboardActivity_ = function(e) {
3503 // When the user starts typing, hide the mouse cursor.
3504 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_)
3505 this.setCssVar('mouse-cursor-style', 'none');
3506};
Mike Frysinger70b94692017-01-26 18:57:50 -10003507
3508/**
rgindad5613292012-06-19 15:40:37 -07003509 * Add the terminalRow and terminalColumn properties to mouse events and
3510 * then forward on to onMouse().
3511 *
3512 * The terminalRow and terminalColumn properties contain the (row, column)
3513 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003514 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003515 * @param {!MouseEvent} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003516 */
3517hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003518 if (e.processedByTerminalHandler_) {
3519 // We register our event handlers on the document, as well as the cursor
3520 // and the scroll blocker. Mouse events that occur on the cursor or
3521 // scroll blocker will also appear on the document, but we don't want to
3522 // process them twice.
3523 //
3524 // We can't just prevent bubbling because that has other side effects, so
3525 // we decorate the event object with this property instead.
3526 return;
3527 }
3528
Mike Frysinger468966c2018-08-28 13:48:51 -04003529 // Consume navigation events. Button 3 is usually "browser back" and
3530 // button 4 is "browser forward" which we don't want to happen.
3531 if (e.button > 2) {
3532 e.preventDefault();
3533 // We don't return so click events can be passed to the remote below.
3534 }
3535
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003536 var reportMouseEvents = (!this.defeatMouseReports_ &&
3537 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3538
rgindafaa74742012-08-21 13:34:03 -07003539 e.processedByTerminalHandler_ = true;
3540
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003541 // Handle auto hiding of mouse cursor while typing.
3542 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
3543 // Make sure the mouse cursor is visible.
3544 this.syncMouseStyle();
3545 // This debounce isn't perfect, but should work well enough for such a
3546 // simple implementation. If the user moved the mouse, we enabled this
3547 // debounce, and then moved the mouse just before the timeout, we wouldn't
3548 // debounce that later movement.
3549 this.mouseHideDelay_ = setTimeout(() => this.mouseHideDelay_ = null, 1000);
3550 }
3551
Robert Gindaeda48db2014-07-17 09:25:30 -07003552 // One based row/column stored on the mouse event.
Joel Hockeyd4fca732019-09-20 16:57:03 -07003553 e.terminalRow = Math.floor(
3554 (e.clientY - this.scrollPort_.visibleRowTopMargin) /
3555 this.scrollPort_.characterSize.height) + 1;
3556 e.terminalColumn = Math.floor(
3557 e.clientX / this.scrollPort_.characterSize.width) + 1;
Robert Gindaeda48db2014-07-17 09:25:30 -07003558
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003559 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
3560 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07003561 return;
3562 }
3563
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003564 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07003565 // If the cursor is visible and we're not sending mouse events to the
3566 // host app, then we want to hide the terminal cursor when the mouse
3567 // cursor is over top. This keeps the terminal cursor from interfering
3568 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003569 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3570 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3571 this.cursorNode_.style.display = 'none';
3572 } else if (this.cursorNode_.style.display == 'none') {
3573 this.cursorNode_.style.display = '';
3574 }
3575 }
rgindad5613292012-06-19 15:40:37 -07003576
Robert Ginda928cf632014-03-05 15:07:41 -08003577 if (e.type == 'mousedown') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003578 this.contextMenu.hide();
Mike Frysingercc114512017-09-11 21:39:17 -04003579
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003580 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003581 // If VT mouse reporting is disabled, or has been defeated with
3582 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003583 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003584 this.setSelectionEnabled(true);
3585 } else {
3586 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003587 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003588 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003589 this.setSelectionEnabled(false);
3590 e.preventDefault();
3591 }
3592 }
3593
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003594 if (!reportMouseEvents) {
John Lin2aad22e2018-03-16 13:58:11 +08003595 if (e.type == 'dblclick') {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003596 this.screen_.expandSelection(this.document_.getSelection());
John Lin2aad22e2018-03-16 13:58:11 +08003597 if (this.copyOnSelect)
Mike Frysinger406c76c2019-01-02 17:53:51 -05003598 this.copySelectionToClipboard();
rgindad5613292012-06-19 15:40:37 -07003599 }
3600
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003601 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003602 // Debounce this event with the dblclick event. If you try to doubleclick
3603 // a URL to open it, Chrome will fire click then dblclick, but we won't
3604 // have expanded the selection text at the first click event.
3605 clearTimeout(this.timeouts_.openUrl);
3606 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3607 500);
3608 return;
3609 }
3610
Mike Frysinger847577f2017-05-23 23:25:57 -04003611 if (e.type == 'mousedown') {
Mike Frysingercc114512017-09-11 21:39:17 -04003612 if (e.ctrlKey && e.button == 2 /* right button */) {
3613 e.preventDefault();
3614 this.contextMenu.show(e, this);
3615 } else if (e.button == this.mousePasteButton ||
3616 (this.mouseRightClickPaste && e.button == 2 /* right button */)) {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003617 if (!this.paste())
Mike Frysinger05a57f02017-08-27 17:48:55 -04003618 console.warn('Could not paste manually due to web restrictions');
Mike Frysinger847577f2017-05-23 23:25:57 -04003619 }
3620 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003621
Mike Frysinger2edd3612017-05-24 00:54:39 -04003622 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003623 !this.document_.getSelection().isCollapsed) {
Mike Frysinger406c76c2019-01-02 17:53:51 -05003624 this.copySelectionToClipboard();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003625 }
3626
3627 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3628 this.scrollBlockerNode_.engaged) {
3629 // Disengage the scroll-blocker after one of these events.
3630 this.scrollBlockerNode_.engaged = false;
3631 this.scrollBlockerNode_.style.top = '-99px';
3632 }
3633
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003634 // Emulate arrow key presses via scroll wheel events.
3635 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3636 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003637 if (e.type == 'wheel') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003638 const delta =
3639 this.scrollPort_.scrollWheelDelta(/** @type {!WheelEvent} */ (e));
Mike Frysingerc3030a82017-05-29 14:16:11 -04003640
Mike Frysinger321063c2018-08-29 15:33:14 -04003641 // Helper to turn a wheel event delta into a series of key presses.
3642 const deltaToArrows = (distance, charSize, arrowPos, arrowNeg) => {
3643 if (distance == 0) {
3644 return '';
3645 }
3646
3647 // Convert the scroll distance into a number of rows/cols.
3648 const cells = lib.f.smartFloorDivide(Math.abs(distance), charSize);
3649 const data = '\x1bO' + (distance < 0 ? arrowNeg : arrowPos);
3650 return data.repeat(cells);
3651 };
3652
3653 // The order between up/down and left/right doesn't really matter.
3654 this.io.sendString(
3655 // Up/down arrow keys.
3656 deltaToArrows(delta.y, this.scrollPort_.characterSize.height,
3657 'A', 'B') +
3658 // Left/right arrow keys.
3659 deltaToArrows(delta.x, this.scrollPort_.characterSize.width,
3660 'C', 'D')
3661 );
Mike Frysingerc3030a82017-05-29 14:16:11 -04003662
3663 e.preventDefault();
3664 }
3665 }
Robert Ginda928cf632014-03-05 15:07:41 -08003666 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003667 if (!this.scrollBlockerNode_.engaged) {
3668 if (e.type == 'mousedown') {
3669 // Move the scroll-blocker into place if we want to keep the scrollport
3670 // from scrolling.
3671 this.scrollBlockerNode_.engaged = true;
3672 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3673 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3674 } else if (e.type == 'mousemove') {
3675 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3676 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003677 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003678 e.preventDefault();
3679 }
3680 }
Robert Ginda928cf632014-03-05 15:07:41 -08003681
3682 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003683 }
3684
Robert Ginda928cf632014-03-05 15:07:41 -08003685 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3686 // Restore this on mouseup in case it was temporarily defeated with a
3687 // alt-mousedown. Only do this when the selection is empty so that
3688 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003689 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003690 }
rgindad5613292012-06-19 15:40:37 -07003691};
3692
3693/**
3694 * Clients should override this if they care to know about mouse events.
3695 *
3696 * The event parameter will be a normal DOM mouse click event with additional
3697 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003698 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003699 * @param {!MouseEvent} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003700 */
3701hterm.Terminal.prototype.onMouse = function(e) { };
3702
3703/**
rginda8e92a692012-05-20 19:37:20 -07003704 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003705 *
3706 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003707 */
Rob Spies06533ba2014-04-24 11:20:37 -07003708hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3709 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003710 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003711
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003712 if (this.reportFocus)
3713 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O');
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003714
Michael Kelly485ecd12014-06-09 11:41:56 -04003715 if (focused === true)
3716 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07003717};
3718
3719/**
rginda8ba33642011-12-14 12:31:31 -08003720 * React when the ScrollPort is scrolled.
3721 */
3722hterm.Terminal.prototype.onScroll_ = function() {
3723 this.scheduleSyncCursorPosition_();
3724};
3725
3726/**
rginda9846e2f2012-01-27 13:53:33 -08003727 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003728 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003729 * @param {!ClipboardEvent} e The DOM paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08003730 */
3731hterm.Terminal.prototype.onPaste_ = function(e) {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003732 var data = e.clipboardData.getData('text').replace(/\n/mg, '\r');
Mike Frysingere8c32c82018-03-11 14:57:28 -07003733 if (this.options_.bracketedPaste) {
3734 // We strip out most escape sequences as they can cause issues (like
3735 // inserting an \x1b[201~ midstream). We pass through whitespace
3736 // though: 0x08:\b 0x09:\t 0x0a:\n 0x0d:\r.
3737 // This matches xterm behavior.
3738 const filter = (data) => data.replace(/[\x00-\x07\x0b-\x0c\x0e-\x1f]/g, '');
3739 data = '\x1b[200~' + filter(data) + '\x1b[201~';
3740 }
Robert Gindaa063b202014-07-21 11:08:25 -07003741
3742 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003743};
3744
3745/**
rgindaa09e7332012-08-17 12:49:51 -07003746 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003747 *
Joel Hockey0f933582019-08-27 18:01:51 -07003748 * @param {!Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07003749 */
3750hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07003751 if (!this.useDefaultWindowCopy) {
3752 e.preventDefault();
3753 setTimeout(this.copySelectionToClipboard.bind(this), 0);
3754 }
rgindaa09e7332012-08-17 12:49:51 -07003755};
3756
3757/**
rginda8ba33642011-12-14 12:31:31 -08003758 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08003759 *
3760 * Note: This function should not directly contain code that alters the internal
3761 * state of the terminal. That kind of code belongs in realizeWidth or
3762 * realizeHeight, so that it can be executed synchronously in the case of a
3763 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08003764 */
3765hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08003766 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07003767 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08003768 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07003769 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08003770
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003771 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08003772 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003773 // gets removed from the document or during the initial load, and we can't
3774 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07003775 // This can also happen if called before the scrollPort calculates the
3776 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08003777 return;
3778 }
3779
rgindaa8ba17d2012-08-15 14:41:10 -07003780 var isNewSize = (columnCount != this.screenSize.width ||
3781 rowCount != this.screenSize.height);
Theodore Dubois651b0842019-09-07 14:32:09 -07003782 const wasScrolledEnd = this.scrollPort_.isScrolledEnd;
rgindaa8ba17d2012-08-15 14:41:10 -07003783
3784 // We do this even if the size didn't change, just to be sure everything is
3785 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04003786 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07003787 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07003788
3789 if (isNewSize)
3790 this.overlaySize();
3791
Robert Gindafb1be6a2013-12-11 11:56:22 -08003792 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07003793 this.scheduleSyncCursorPosition_();
Theodore Dubois651b0842019-09-07 14:32:09 -07003794
3795 if (wasScrolledEnd) {
3796 this.scrollEnd();
3797 }
rginda8ba33642011-12-14 12:31:31 -08003798};
3799
3800/**
3801 * Service the cursor blink timeout.
3802 */
3803hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07003804 if (!this.options_.cursorBlink) {
3805 delete this.timeouts_.cursorBlink;
3806 return;
3807 }
3808
Robert Ginda830583c2013-08-07 13:20:46 -07003809 if (this.cursorNode_.getAttribute('focus') == 'false' ||
3810 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08003811 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07003812 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3813 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08003814 } else {
rginda87b86462011-12-14 13:48:03 -08003815 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07003816 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3817 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08003818 }
3819};
David Reveman8f552492012-03-28 12:18:41 -04003820
3821/**
3822 * Set the scrollbar-visible mode bit.
3823 *
3824 * If scrollbar-visible is on, the vertical scrollbar will be visible.
3825 * Otherwise it will not.
3826 *
3827 * Defaults to on.
3828 *
3829 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
3830 */
3831hterm.Terminal.prototype.setScrollbarVisible = function(state) {
3832 this.scrollPort_.setScrollbarVisible(state);
3833};
Michael Kelly485ecd12014-06-09 11:41:56 -04003834
3835/**
Rob Spies49039e52014-12-17 13:40:04 -08003836 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04003837 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08003838 *
3839 * Defaults to 1.
3840 *
Evan Jones2600d4f2016-12-06 09:29:36 -05003841 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08003842 */
3843hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3844 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3845};
3846
3847/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003848 * Close all web notifications created by terminal bells.
3849 */
3850hterm.Terminal.prototype.closeBellNotifications_ = function() {
3851 this.bellNotificationList_.forEach(function(n) {
3852 n.close();
3853 });
3854 this.bellNotificationList_.length = 0;
3855};
Raymes Khourye5d48982018-08-02 09:08:32 +10003856
3857/**
3858 * Syncs the cursor position when the scrollport gains focus.
3859 */
3860hterm.Terminal.prototype.onScrollportFocus_ = function() {
3861 // If the cursor is offscreen we set selection to the last row on the screen.
3862 const topRowIndex = this.scrollPort_.getTopRowIndex();
3863 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
3864 const selection = this.document_.getSelection();
3865 if (!this.syncCursorPosition_() && selection) {
3866 selection.collapse(this.getRowNode(bottomRowIndex));
3867 }
3868};