blob: 667c4a93091c379cb7adf499347bd8263a728e5c [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.
Joel Hockey8ff48232019-09-24 13:15:17 -070094 /** @type {string} */
95 this.backgroundColor_ = '';
96 /** @type {string} */
97 this.foregroundColor_ = '';
Robert Ginda57f03b42012-09-13 11:02:48 -070098 this.scrollOnOutput_ = null;
99 this.scrollOnKeystroke_ = null;
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400100 this.scrollWheelArrowKeys_ = null;
rginda9f5222b2012-03-05 11:53:28 -0800101
Robert Ginda6aec7eb2015-06-16 10:31:30 -0700102 // True if we should override mouse event reporting to allow local selection.
103 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -0800104
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400105 // Whether to auto hide the mouse cursor when typing.
106 this.setAutomaticMouseHiding();
107 // Timer to keep mouse visible while it's being used.
108 this.mouseHideDelay_ = null;
109
rgindaf0090c92012-02-10 14:58:52 -0800110 // Terminal bell sound.
111 this.bellAudio_ = this.document_.createElement('audio');
Mike Frysingerd826f1a2017-07-06 16:20:06 -0400112 this.bellAudio_.id = 'hterm:bell-audio';
rgindaf0090c92012-02-10 14:58:52 -0800113 this.bellAudio_.setAttribute('preload', 'auto');
114
Raymes Khoury3e44bc92018-05-17 10:54:23 +1000115 // The AccessibilityReader object for announcing command output.
116 this.accessibilityReader_ = null;
117
Mike Frysingercc114512017-09-11 21:39:17 -0400118 // The context menu object.
119 this.contextMenu = new hterm.ContextMenu();
120
Michael Kelly485ecd12014-06-09 11:41:56 -0400121 // All terminal bell notifications that have been generated (not necessarily
122 // shown).
123 this.bellNotificationList_ = [];
Joel Hockeyd4fca732019-09-20 16:57:03 -0700124 this.bellSquelchTimeout_ = null;
Michael Kelly485ecd12014-06-09 11:41:56 -0400125
126 // Whether we have permission to display notifications.
127 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400128
rginda6d397402012-01-17 10:58:29 -0800129 // Cursor position and attributes saved with DECSC.
130 this.savedOptions_ = {};
131
rginda8ba33642011-12-14 12:31:31 -0800132 // The current mode bits for the terminal.
133 this.options_ = new hterm.Options();
134
135 // Timeouts we might need to clear.
136 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800137
138 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800139 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800140
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800141 this.saveCursorAndState(true);
142
Zhu Qunying30d40712017-03-14 16:27:00 -0700143 // The keyboard handler.
rgindafeaf3142012-01-31 15:14:20 -0800144 this.keyboard = new hterm.Keyboard(this);
145
rginda87b86462011-12-14 13:48:03 -0800146 // General IO interface that can be given to third parties without exposing
147 // the entire terminal object.
148 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800149
rgindad5613292012-06-19 15:40:37 -0700150 // True if mouse-click-drag should scroll the terminal.
151 this.enableMouseDragScroll = true;
152
Robert Ginda57f03b42012-09-13 11:02:48 -0700153 this.copyOnSelect = null;
Mike Frysinger847577f2017-05-23 23:25:57 -0400154 this.mouseRightClickPaste = null;
rginda4bba5e12012-06-20 16:15:30 -0700155 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700156
Zhu Qunying30d40712017-03-14 16:27:00 -0700157 // Whether to use the default window copy behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700158 this.useDefaultWindowCopy = false;
159
160 this.clearSelectionAfterCopy = true;
161
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400162 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800163 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700164
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400165 // Whether we allow images to be shown.
166 this.allowImagesInline = null;
167
Gabriel Holodake8a09be2017-10-10 01:07:11 -0400168 this.reportFocus = false;
169
Robert Ginda57f03b42012-09-13 11:02:48 -0700170 this.setProfile(opt_profileId || 'default',
Evan Jones5f9df812016-12-06 09:38:58 -0500171 function() { this.onTerminalReady(); }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800172};
173
174/**
Robert Ginda830583c2013-08-07 13:20:46 -0700175 * Possible cursor shapes.
176 */
177hterm.Terminal.cursorShape = {
178 BLOCK: 'BLOCK',
179 BEAM: 'BEAM',
180 UNDERLINE: 'UNDERLINE'
181};
182
183/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700184 * Clients should override this to be notified when the terminal is ready
185 * for use.
186 *
187 * The terminal initialization is asynchronous, and shouldn't be used before
188 * this method is called.
189 */
190hterm.Terminal.prototype.onTerminalReady = function() { };
191
192/**
rginda35c456b2012-02-09 17:29:05 -0800193 * Default tab with of 8 to match xterm.
194 */
195hterm.Terminal.prototype.tabWidth = 8;
196
197/**
rginda9f5222b2012-03-05 11:53:28 -0800198 * Select a preference profile.
199 *
200 * This will load the terminal preferences for the given profile name and
201 * associate subsequent preference changes with the new preference profile.
202 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500203 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800204 * characters will be removed from the name.
Joel Hockey0f933582019-08-27 18:01:51 -0700205 * @param {function()=} opt_callback Optional callback to invoke when the
206 * profile transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800207 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700208hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
209 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800210
Robert Ginda57f03b42012-09-13 11:02:48 -0700211 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800212
Robert Ginda57f03b42012-09-13 11:02:48 -0700213 if (this.prefs_)
214 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800215
Robert Ginda57f03b42012-09-13 11:02:48 -0700216 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
217 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800218 'alt-gr-mode': function(v) {
219 if (v == null) {
220 if (navigator.language.toLowerCase() == 'en-us') {
221 v = 'none';
222 } else {
223 v = 'right-alt';
224 }
225 } else if (typeof v == 'string') {
226 v = v.toLowerCase();
227 } else {
228 v = 'none';
229 }
230
231 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v))
232 v = 'none';
233
234 terminal.keyboard.altGrMode = v;
235 },
236
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700237 'alt-backspace-is-meta-backspace': function(v) {
238 terminal.keyboard.altBackspaceIsMetaBackspace = v;
239 },
240
Robert Ginda57f03b42012-09-13 11:02:48 -0700241 'alt-is-meta': function(v) {
242 terminal.keyboard.altIsMeta = v;
243 },
244
245 'alt-sends-what': function(v) {
246 if (!/^(escape|8-bit|browser-key)$/.test(v))
247 v = 'escape';
248
249 terminal.keyboard.altSendsWhat = v;
250 },
251
252 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800253 var ary = v.match(/^lib-resource:(\S+)/);
254 if (ary) {
255 terminal.bellAudio_.setAttribute('src',
256 lib.resource.getDataUrl(ary[1]));
257 } else {
258 terminal.bellAudio_.setAttribute('src', v);
259 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700260 },
261
Michael Kelly485ecd12014-06-09 11:41:56 -0400262 'desktop-notification-bell': function(v) {
263 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700264 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400265 Notification.permission === 'granted';
266 if (!terminal.desktopNotificationBell_) {
267 // Note: We don't call Notification.requestPermission here because
268 // Chrome requires the call be the result of a user action (such as an
269 // onclick handler), and pref listeners are run asynchronously.
270 //
271 // A way of working around this would be to display a dialog in the
272 // terminal with a "click-to-request-permission" button.
273 console.warn('desktop-notification-bell is true but we do not have ' +
274 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400275 }
276 } else {
277 terminal.desktopNotificationBell_ = false;
278 }
279 },
280
Robert Ginda57f03b42012-09-13 11:02:48 -0700281 'background-color': function(v) {
282 terminal.setBackgroundColor(v);
283 },
284
285 'background-image': function(v) {
286 terminal.scrollPort_.setBackgroundImage(v);
287 },
288
289 'background-size': function(v) {
290 terminal.scrollPort_.setBackgroundSize(v);
291 },
292
293 'background-position': function(v) {
294 terminal.scrollPort_.setBackgroundPosition(v);
295 },
296
297 'backspace-sends-backspace': function(v) {
298 terminal.keyboard.backspaceSendsBackspace = v;
299 },
300
Brad Town18654b62015-03-12 00:27:45 -0700301 'character-map-overrides': function(v) {
302 if (!(v == null || v instanceof Object)) {
303 console.warn('Preference character-map-modifications is not an ' +
304 'object: ' + v);
305 return;
306 }
307
Mike Frysinger095d4062017-06-14 00:29:48 -0700308 terminal.vt.characterMaps.reset();
309 terminal.vt.characterMaps.setOverrides(v);
Brad Town18654b62015-03-12 00:27:45 -0700310 },
311
Robert Ginda57f03b42012-09-13 11:02:48 -0700312 'cursor-blink': function(v) {
313 terminal.setCursorBlink(!!v);
314 },
315
Joel Hockey9d10ba12019-05-28 01:25:02 -0700316 'cursor-shape': function(v) {
317 terminal.setCursorShape(v);
318 },
319
Robert Gindaea2183e2014-07-17 09:51:51 -0700320 'cursor-blink-cycle': function(v) {
321 if (v instanceof Array &&
322 typeof v[0] == 'number' &&
323 typeof v[1] == 'number') {
324 terminal.cursorBlinkCycle_ = v;
325 } else if (typeof v == 'number') {
326 terminal.cursorBlinkCycle_ = [v, v];
327 } else {
328 // Fast blink indicates an error.
329 terminal.cursorBlinkCycle_ = [100, 100];
330 }
331 },
332
Robert Ginda57f03b42012-09-13 11:02:48 -0700333 'cursor-color': function(v) {
334 terminal.setCursorColor(v);
335 },
336
337 'color-palette-overrides': function(v) {
338 if (!(v == null || v instanceof Object || v instanceof Array)) {
339 console.warn('Preference color-palette-overrides is not an array or ' +
340 'object: ' + v);
341 return;
rginda9f5222b2012-03-05 11:53:28 -0800342 }
rginda9f5222b2012-03-05 11:53:28 -0800343
Robert Ginda57f03b42012-09-13 11:02:48 -0700344 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700345
Robert Ginda57f03b42012-09-13 11:02:48 -0700346 if (v) {
347 for (var key in v) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700348 var i = parseInt(key, 10);
Robert Ginda57f03b42012-09-13 11:02:48 -0700349 if (isNaN(i) || i < 0 || i > 255) {
350 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
351 continue;
352 }
353
354 if (v[i]) {
355 var rgb = lib.colors.normalizeCSS(v[i]);
356 if (rgb)
357 lib.colors.colorPalette[i] = rgb;
358 }
359 }
rginda30f20f62012-04-05 16:36:19 -0700360 }
rginda30f20f62012-04-05 16:36:19 -0700361
Evan Jones5f9df812016-12-06 09:38:58 -0500362 terminal.primaryScreen_.textAttributes.resetColorPalette();
Robert Ginda57f03b42012-09-13 11:02:48 -0700363 terminal.alternateScreen_.textAttributes.resetColorPalette();
364 },
rginda30f20f62012-04-05 16:36:19 -0700365
Robert Ginda57f03b42012-09-13 11:02:48 -0700366 'copy-on-select': function(v) {
367 terminal.copyOnSelect = !!v;
368 },
rginda9f5222b2012-03-05 11:53:28 -0800369
Rob Spies0bec09b2014-06-06 15:58:09 -0700370 'use-default-window-copy': function(v) {
371 terminal.useDefaultWindowCopy = !!v;
372 },
373
374 'clear-selection-after-copy': function(v) {
375 terminal.clearSelectionAfterCopy = !!v;
376 },
377
Robert Ginda7e5e9522014-03-14 12:23:58 -0700378 'ctrl-plus-minus-zero-zoom': function(v) {
379 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
380 },
381
Robert Gindafb5a3f92014-05-13 14:12:00 -0700382 'ctrl-c-copy': function(v) {
383 terminal.keyboard.ctrlCCopy = v;
384 },
385
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100386 'ctrl-v-paste': function(v) {
387 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700388 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100389 },
390
Cody Coljee-Gray7c6a0392018-10-25 13:18:28 -0700391 'paste-on-drop': function(v) {
392 terminal.scrollPort_.setPasteOnDrop(v);
393 },
394
Masaya Suzuki273aa982014-05-31 07:25:55 +0900395 'east-asian-ambiguous-as-two-column': function(v) {
396 lib.wc.regardCjkAmbiguous = v;
397 },
398
Robert Ginda57f03b42012-09-13 11:02:48 -0700399 'enable-8-bit-control': function(v) {
400 terminal.vt.enable8BitControl = !!v;
401 },
rginda30f20f62012-04-05 16:36:19 -0700402
Robert Ginda57f03b42012-09-13 11:02:48 -0700403 'enable-bold': function(v) {
404 terminal.syncBoldSafeState();
405 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400406
Robert Ginda3e278d72014-03-25 13:18:51 -0700407 'enable-bold-as-bright': function(v) {
408 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
409 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
410 },
411
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400412 'enable-blink': function(v) {
Mike Frysinger261597c2017-12-28 01:14:21 -0500413 terminal.setTextBlink(!!v);
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400414 },
415
Robert Ginda57f03b42012-09-13 11:02:48 -0700416 'enable-clipboard-write': function(v) {
417 terminal.vt.enableClipboardWrite = !!v;
418 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400419
Robert Ginda3755e752013-05-31 13:34:09 -0700420 'enable-dec12': function(v) {
421 terminal.vt.enableDec12 = !!v;
422 },
423
Mike Frysinger38f267d2018-09-07 02:50:59 -0400424 'enable-csi-j-3': function(v) {
425 terminal.vt.enableCsiJ3 = !!v;
426 },
427
Robert Ginda57f03b42012-09-13 11:02:48 -0700428 'font-family': function(v) {
429 terminal.syncFontFamily();
430 },
rginda30f20f62012-04-05 16:36:19 -0700431
Robert Ginda57f03b42012-09-13 11:02:48 -0700432 'font-size': function(v) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700433 v = parseInt(v, 10);
Mike Frysinger47853ac2017-12-14 00:44:10 -0500434 if (v <= 0) {
435 console.error(`Invalid font size: ${v}`);
436 return;
437 }
438
Robert Ginda57f03b42012-09-13 11:02:48 -0700439 terminal.setFontSize(v);
440 },
rginda9875d902012-08-20 16:21:57 -0700441
Robert Ginda57f03b42012-09-13 11:02:48 -0700442 'font-smoothing': function(v) {
443 terminal.syncFontFamily();
444 },
rgindade84e382012-04-20 15:39:31 -0700445
Robert Ginda57f03b42012-09-13 11:02:48 -0700446 'foreground-color': function(v) {
447 terminal.setForegroundColor(v);
448 },
rginda30f20f62012-04-05 16:36:19 -0700449
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400450 'hide-mouse-while-typing': function(v) {
451 terminal.setAutomaticMouseHiding(v);
452 },
453
Robert Ginda57f03b42012-09-13 11:02:48 -0700454 'home-keys-scroll': function(v) {
455 terminal.keyboard.homeKeysScroll = v;
456 },
rginda4bba5e12012-06-20 16:15:30 -0700457
Robert Gindaa8165692015-06-15 14:46:31 -0700458 'keybindings': function(v) {
459 terminal.keyboard.bindings.clear();
460
461 if (!v)
462 return;
463
464 if (!(v instanceof Object)) {
465 console.error('Error in keybindings preference: Expected object');
466 return;
467 }
468
469 try {
470 terminal.keyboard.bindings.addBindings(v);
471 } catch (ex) {
472 console.error('Error in keybindings preference: ' + ex);
473 }
474 },
475
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700476 'media-keys-are-fkeys': function(v) {
477 terminal.keyboard.mediaKeysAreFKeys = v;
478 },
479
Robert Ginda57f03b42012-09-13 11:02:48 -0700480 'meta-sends-escape': function(v) {
481 terminal.keyboard.metaSendsEscape = v;
482 },
rginda30f20f62012-04-05 16:36:19 -0700483
Mike Frysinger847577f2017-05-23 23:25:57 -0400484 'mouse-right-click-paste': function(v) {
485 terminal.mouseRightClickPaste = v;
486 },
487
Robert Ginda57f03b42012-09-13 11:02:48 -0700488 'mouse-paste-button': function(v) {
489 terminal.syncMousePasteButton();
490 },
rgindaa8ba17d2012-08-15 14:41:10 -0700491
Robert Gindae76aa9f2014-03-14 12:29:12 -0700492 'page-keys-scroll': function(v) {
493 terminal.keyboard.pageKeysScroll = v;
494 },
495
Robert Ginda40932892012-12-10 17:26:40 -0800496 'pass-alt-number': function(v) {
497 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800498 // Let Alt-1..9 pass to the browser (to control tab switching) on
499 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500500 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800501 }
502
503 terminal.passAltNumber = v;
504 },
505
506 'pass-ctrl-number': function(v) {
507 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800508 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
509 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500510 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800511 }
512
513 terminal.passCtrlNumber = v;
514 },
515
516 'pass-meta-number': function(v) {
517 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800518 // Let Meta-1..9 pass to the browser (to control tab switching) on
519 // OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500520 v = (hterm.os == 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800521 }
522
523 terminal.passMetaNumber = v;
524 },
525
Marius Schilder77857b32014-05-14 16:21:26 -0700526 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700527 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700528 },
529
Robert Ginda8cb7d902013-06-20 14:37:18 -0700530 'receive-encoding': function(v) {
531 if (!(/^(utf-8|raw)$/).test(v)) {
532 console.warn('Invalid value for "receive-encoding": ' + v);
533 v = 'utf-8';
534 }
535
536 terminal.vt.characterEncoding = v;
537 },
538
Robert Ginda57f03b42012-09-13 11:02:48 -0700539 'scroll-on-keystroke': function(v) {
540 terminal.scrollOnKeystroke_ = v;
541 },
rginda9f5222b2012-03-05 11:53:28 -0800542
Robert Ginda57f03b42012-09-13 11:02:48 -0700543 'scroll-on-output': function(v) {
544 terminal.scrollOnOutput_ = v;
545 },
rginda30f20f62012-04-05 16:36:19 -0700546
Robert Ginda57f03b42012-09-13 11:02:48 -0700547 'scrollbar-visible': function(v) {
548 terminal.setScrollbarVisible(v);
549 },
rginda9f5222b2012-03-05 11:53:28 -0800550
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400551 'scroll-wheel-may-send-arrow-keys': function(v) {
552 terminal.scrollWheelArrowKeys_ = v;
553 },
554
Rob Spies49039e52014-12-17 13:40:04 -0800555 'scroll-wheel-move-multiplier': function(v) {
556 terminal.setScrollWheelMoveMultipler(v);
557 },
558
Robert Ginda57f03b42012-09-13 11:02:48 -0700559 'shift-insert-paste': function(v) {
560 terminal.keyboard.shiftInsertPaste = v;
561 },
rginda9f5222b2012-03-05 11:53:28 -0800562
Mike Frysingera7768922017-07-28 15:00:12 -0400563 'terminal-encoding': function(v) {
Mike Frysingera1371e12017-08-17 01:37:17 -0400564 terminal.vt.setEncoding(v);
Mike Frysingera7768922017-07-28 15:00:12 -0400565 },
566
Robert Gindae76aa9f2014-03-14 12:29:12 -0700567 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400568 terminal.scrollPort_.setUserCssUrl(v);
569 },
570
571 'user-css-text': function(v) {
572 terminal.scrollPort_.setUserCssText(v);
573 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400574
575 'word-break-match-left': function(v) {
576 terminal.primaryScreen_.wordBreakMatchLeft = v;
577 terminal.alternateScreen_.wordBreakMatchLeft = v;
578 },
579
580 'word-break-match-right': function(v) {
581 terminal.primaryScreen_.wordBreakMatchRight = v;
582 terminal.alternateScreen_.wordBreakMatchRight = v;
583 },
584
585 'word-break-match-middle': function(v) {
586 terminal.primaryScreen_.wordBreakMatchMiddle = v;
587 terminal.alternateScreen_.wordBreakMatchMiddle = v;
588 },
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400589
590 'allow-images-inline': function(v) {
591 terminal.allowImagesInline = v;
592 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700593 });
rginda30f20f62012-04-05 16:36:19 -0700594
Robert Ginda57f03b42012-09-13 11:02:48 -0700595 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800596 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700597
598 if (opt_callback)
599 opt_callback();
600 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800601};
602
Rob Spies56953412014-04-28 14:09:47 -0700603/**
604 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500605 *
Joel Hockey0f933582019-08-27 18:01:51 -0700606 * @return {!hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700607 */
608hterm.Terminal.prototype.getPrefs = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700609 return lib.notNull(this.prefs_);
Rob Spies56953412014-04-28 14:09:47 -0700610};
611
Robert Gindaa063b202014-07-21 11:08:25 -0700612/**
613 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500614 *
615 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700616 */
617hterm.Terminal.prototype.setBracketedPaste = function(state) {
618 this.options_.bracketedPaste = state;
619};
Rob Spies56953412014-04-28 14:09:47 -0700620
rginda8e92a692012-05-20 19:37:20 -0700621/**
622 * Set the color for the cursor.
623 *
624 * If you want this setting to persist, set it through prefs_, rather than
625 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500626 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500627 * @param {string=} color The color to set. If not defined, we reset to the
628 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700629 */
630hterm.Terminal.prototype.setCursorColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500631 if (color === undefined)
Joel Hockeyd4fca732019-09-20 16:57:03 -0700632 color = this.prefs_.getString('cursor-color');
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500633
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400634 this.setCssVar('cursor-color', color);
rginda8e92a692012-05-20 19:37:20 -0700635};
636
637/**
638 * Return the current cursor color as a string.
Evan Jones2600d4f2016-12-06 09:29:36 -0500639 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700640 */
641hterm.Terminal.prototype.getCursorColor = function() {
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400642 return this.getCssVar('cursor-color');
rginda8e92a692012-05-20 19:37:20 -0700643};
644
645/**
rgindad5613292012-06-19 15:40:37 -0700646 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500647 *
648 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700649 */
650hterm.Terminal.prototype.setSelectionEnabled = function(state) {
651 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700652};
653
654/**
rginda8e92a692012-05-20 19:37:20 -0700655 * Set the background color.
656 *
657 * If you want this setting to persist, set it through prefs_, rather than
658 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500659 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500660 * @param {string=} color The color to set. If not defined, we reset to the
661 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700662 */
663hterm.Terminal.prototype.setBackgroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500664 if (color === undefined)
Joel Hockeyd4fca732019-09-20 16:57:03 -0700665 color = this.prefs_.getString('background-color');
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500666
Joel Hockey8ff48232019-09-24 13:15:17 -0700667 this.backgroundColor_ = lib.colors.normalizeCSS(color) || '';
Robert Ginda57f03b42012-09-13 11:02:48 -0700668 this.primaryScreen_.textAttributes.setDefaults(
669 this.foregroundColor_, this.backgroundColor_);
670 this.alternateScreen_.textAttributes.setDefaults(
671 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700672 this.scrollPort_.setBackgroundColor(color);
673};
674
rginda9f5222b2012-03-05 11:53:28 -0800675/**
676 * Return the current terminal background color.
677 *
678 * Intended for use by other classes, so we don't have to expose the entire
679 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500680 *
681 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800682 */
683hterm.Terminal.prototype.getBackgroundColor = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700684 return lib.notNull(this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700685};
686
687/**
688 * Set the foreground color.
689 *
690 * If you want this setting to persist, set it through prefs_, rather than
691 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500692 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500693 * @param {string=} color The color to set. If not defined, we reset to the
694 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700695 */
696hterm.Terminal.prototype.setForegroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500697 if (color === undefined)
Joel Hockeyd4fca732019-09-20 16:57:03 -0700698 color = this.prefs_.getString('foreground-color');
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500699
Joel Hockey8ff48232019-09-24 13:15:17 -0700700 this.foregroundColor_ = lib.colors.normalizeCSS(color) || '';
Robert Ginda57f03b42012-09-13 11:02:48 -0700701 this.primaryScreen_.textAttributes.setDefaults(
702 this.foregroundColor_, this.backgroundColor_);
703 this.alternateScreen_.textAttributes.setDefaults(
704 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700705 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800706};
707
708/**
709 * Return the current terminal foreground color.
710 *
711 * Intended for use by other classes, so we don't have to expose the entire
712 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500713 *
714 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800715 */
716hterm.Terminal.prototype.getForegroundColor = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700717 return lib.notNull(this.foregroundColor_);
rginda9f5222b2012-03-05 11:53:28 -0800718};
719
720/**
rginda87b86462011-12-14 13:48:03 -0800721 * Create a new instance of a terminal command and run it with a given
722 * argument string.
723 *
Joel Hockeyd4fca732019-09-20 16:57:03 -0700724 * @param {!Function} commandClass The constructor for a terminal command.
Joel Hockey8081ea62019-08-26 16:52:32 -0700725 * @param {string} commandName The command to run for this terminal.
726 * @param {!Array<string>} args The arguments to pass to the command.
rginda87b86462011-12-14 13:48:03 -0800727 */
Joel Hockey8081ea62019-08-26 16:52:32 -0700728hterm.Terminal.prototype.runCommandClass = function(
729 commandClass, commandName, args) {
rgindaf522ce02012-04-17 17:49:17 -0700730 var environment = this.prefs_.get('environment');
731 if (typeof environment != 'object' || environment == null)
732 environment = {};
733
rginda87b86462011-12-14 13:48:03 -0800734 var self = this;
735 this.command = new commandClass(
Joel Hockey8081ea62019-08-26 16:52:32 -0700736 {
737 commandName: commandName,
738 args: args,
rginda87b86462011-12-14 13:48:03 -0800739 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700740 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800741 onExit: function(code) {
742 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800743 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700744 if (self.prefs_.get('close-on-exit'))
745 window.close();
rginda87b86462011-12-14 13:48:03 -0800746 }
747 });
748
rgindafeaf3142012-01-31 15:14:20 -0800749 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800750 this.command.run();
751};
752
753/**
rgindafeaf3142012-01-31 15:14:20 -0800754 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500755 *
756 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800757 */
758hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700759 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800760};
761
762/**
763 * Install the keyboard handler for this terminal.
764 *
765 * This will prevent the browser from seeing any keystrokes sent to the
766 * terminal.
767 */
768hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700769 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400770};
rgindafeaf3142012-01-31 15:14:20 -0800771
772/**
773 * Uninstall the keyboard handler for this terminal.
774 */
775hterm.Terminal.prototype.uninstallKeyboard = function() {
776 this.keyboard.installKeyboard(null);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400777};
rgindafeaf3142012-01-31 15:14:20 -0800778
779/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400780 * Set a CSS variable.
781 *
782 * Normally this is used to set variables in the hterm namespace.
783 *
784 * @param {string} name The variable to set.
Joel Hockeyd4fca732019-09-20 16:57:03 -0700785 * @param {string|number} value The value to assign to the variable.
Joel Hockey0f933582019-08-27 18:01:51 -0700786 * @param {string=} opt_prefix The variable namespace/prefix to use.
Mike Frysingercce97c42017-08-05 01:11:22 -0400787 */
788hterm.Terminal.prototype.setCssVar = function(name, value,
789 opt_prefix='--hterm-') {
790 this.document_.documentElement.style.setProperty(
Joel Hockeyd4fca732019-09-20 16:57:03 -0700791 `${opt_prefix}${name}`, value.toString());
Mike Frysingercce97c42017-08-05 01:11:22 -0400792};
793
794/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500795 * Get a CSS variable.
796 *
797 * Normally this is used to get variables in the hterm namespace.
798 *
799 * @param {string} name The variable to read.
Joel Hockey0f933582019-08-27 18:01:51 -0700800 * @param {string=} opt_prefix The variable namespace/prefix to use.
Mike Frysinger261597c2017-12-28 01:14:21 -0500801 * @return {string} The current setting for this variable.
802 */
803hterm.Terminal.prototype.getCssVar = function(name, opt_prefix='--hterm-') {
804 return this.document_.documentElement.style.getPropertyValue(
805 `${opt_prefix}${name}`);
806};
807
808/**
rginda35c456b2012-02-09 17:29:05 -0800809 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800810 *
811 * Call setFontSize(0) to reset to the default font size.
812 *
813 * This function does not modify the font-size preference.
814 *
815 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800816 */
817hterm.Terminal.prototype.setFontSize = function(px) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500818 if (px <= 0)
Joel Hockeyd4fca732019-09-20 16:57:03 -0700819 px = this.prefs_.getNumber('font-size');
rginda9f5222b2012-03-05 11:53:28 -0800820
rginda35c456b2012-02-09 17:29:05 -0800821 this.scrollPort_.setFontSize(px);
Mike Frysingercce97c42017-08-05 01:11:22 -0400822 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
823 this.setCssVar('charsize-height',
824 this.scrollPort_.characterSize.height + 'px');
rginda35c456b2012-02-09 17:29:05 -0800825};
826
827/**
828 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500829 *
830 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800831 */
832hterm.Terminal.prototype.getFontSize = function() {
833 return this.scrollPort_.getFontSize();
834};
835
836/**
rginda8e92a692012-05-20 19:37:20 -0700837 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500838 *
839 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700840 */
841hterm.Terminal.prototype.getFontFamily = function() {
842 return this.scrollPort_.getFontFamily();
843};
844
845/**
rginda35c456b2012-02-09 17:29:05 -0800846 * Set the CSS "font-family" for this terminal.
847 */
rginda9f5222b2012-03-05 11:53:28 -0800848hterm.Terminal.prototype.syncFontFamily = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700849 this.scrollPort_.setFontFamily(this.prefs_.getString('font-family'),
850 this.prefs_.getString('font-smoothing'));
rginda9f5222b2012-03-05 11:53:28 -0800851 this.syncBoldSafeState();
852};
853
rginda4bba5e12012-06-20 16:15:30 -0700854/**
855 * Set this.mousePasteButton based on the mouse-paste-button pref,
856 * autodetecting if necessary.
857 */
858hterm.Terminal.prototype.syncMousePasteButton = function() {
859 var button = this.prefs_.get('mouse-paste-button');
860 if (typeof button == 'number') {
861 this.mousePasteButton = button;
862 return;
863 }
864
Mike Frysingeree81a002017-12-12 16:14:53 -0500865 if (hterm.os != 'linux') {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400866 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700867 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400868 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700869 }
870};
871
872/**
873 * Enable or disable bold based on the enable-bold pref, autodetecting if
874 * necessary.
875 */
rginda9f5222b2012-03-05 11:53:28 -0800876hterm.Terminal.prototype.syncBoldSafeState = function() {
877 var enableBold = this.prefs_.get('enable-bold');
878 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700879 this.primaryScreen_.textAttributes.enableBold = enableBold;
880 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800881 return;
882 }
883
rgindaf7521392012-02-28 17:20:34 -0800884 var normalSize = this.scrollPort_.measureCharacterSize();
885 var boldSize = this.scrollPort_.measureCharacterSize('bold');
886
887 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800888 if (!isBoldSafe) {
889 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700890 'from normal. Font family is: ' +
891 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800892 }
rginda9f5222b2012-03-05 11:53:28 -0800893
Robert Gindaed016262012-10-26 16:27:09 -0700894 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
895 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800896};
897
898/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500899 * Control text blinking behavior.
900 *
901 * @param {boolean=} state Whether to enable support for blinking text.
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400902 */
Mike Frysinger261597c2017-12-28 01:14:21 -0500903hterm.Terminal.prototype.setTextBlink = function(state) {
904 if (state === undefined)
Joel Hockeyd4fca732019-09-20 16:57:03 -0700905 state = this.prefs_.getBoolean('enable-blink');
Mike Frysinger261597c2017-12-28 01:14:21 -0500906 this.setCssVar('blink-node-duration', state ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400907};
908
909/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400910 * Set the mouse cursor style based on the current terminal mode.
911 */
912hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -0400913 this.setCssVar('mouse-cursor-style',
914 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
915 'var(--hterm-mouse-cursor-text)' :
Mike Frysinger67f58f82018-11-22 13:38:22 -0500916 'var(--hterm-mouse-cursor-default)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400917};
918
919/**
rginda87b86462011-12-14 13:48:03 -0800920 * Return a copy of the current cursor position.
921 *
Joel Hockey0f933582019-08-27 18:01:51 -0700922 * @return {!hterm.RowCol} The RowCol object representing the current position.
rginda87b86462011-12-14 13:48:03 -0800923 */
924hterm.Terminal.prototype.saveCursor = function() {
925 return this.screen_.cursorPosition.clone();
926};
927
Evan Jones2600d4f2016-12-06 09:29:36 -0500928/**
929 * Return the current text attributes.
930 *
Joel Hockeyd4fca732019-09-20 16:57:03 -0700931 * @return {!hterm.TextAttributes}
Evan Jones2600d4f2016-12-06 09:29:36 -0500932 */
rgindaa19afe22012-01-25 15:40:22 -0800933hterm.Terminal.prototype.getTextAttributes = function() {
934 return this.screen_.textAttributes;
935};
936
Evan Jones2600d4f2016-12-06 09:29:36 -0500937/**
938 * Set the text attributes.
939 *
Joel Hockeyd4fca732019-09-20 16:57:03 -0700940 * @param {!hterm.TextAttributes} textAttributes The attributes to set.
Evan Jones2600d4f2016-12-06 09:29:36 -0500941 */
rginda1a09aa02012-06-18 21:11:25 -0700942hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
943 this.screen_.textAttributes = textAttributes;
944};
945
rginda87b86462011-12-14 13:48:03 -0800946/**
rgindaf522ce02012-04-17 17:49:17 -0700947 * Return the current browser zoom factor applied to the terminal.
948 *
949 * @return {number} The current browser zoom factor.
950 */
951hterm.Terminal.prototype.getZoomFactor = function() {
952 return this.scrollPort_.characterSize.zoomFactor;
953};
954
955/**
rginda9846e2f2012-01-27 13:53:33 -0800956 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -0500957 *
958 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -0800959 */
960hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800961 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800962};
963
964/**
rginda87b86462011-12-14 13:48:03 -0800965 * Restore a previously saved cursor position.
966 *
Joel Hockey0f933582019-08-27 18:01:51 -0700967 * @param {!hterm.RowCol} cursor The position to restore.
rginda87b86462011-12-14 13:48:03 -0800968 */
969hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700970 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
971 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800972 this.screen_.setCursorPosition(row, column);
973 if (cursor.column > column ||
974 cursor.column == column && cursor.overflow) {
975 this.screen_.cursorPosition.overflow = true;
976 }
rginda87b86462011-12-14 13:48:03 -0800977};
978
979/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400980 * Clear the cursor's overflow flag.
981 */
982hterm.Terminal.prototype.clearCursorOverflow = function() {
983 this.screen_.cursorPosition.overflow = false;
984};
985
986/**
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800987 * Save the current cursor state to the corresponding screens.
988 *
989 * See the hterm.Screen.CursorState class for more details.
990 *
991 * @param {boolean=} both If true, update both screens, else only update the
992 * current screen.
993 */
994hterm.Terminal.prototype.saveCursorAndState = function(both) {
995 if (both) {
996 this.primaryScreen_.saveCursorAndState(this.vt);
997 this.alternateScreen_.saveCursorAndState(this.vt);
998 } else
999 this.screen_.saveCursorAndState(this.vt);
1000};
1001
1002/**
1003 * Restore the saved cursor state in the corresponding screens.
1004 *
1005 * See the hterm.Screen.CursorState class for more details.
1006 *
1007 * @param {boolean=} both If true, update both screens, else only update the
1008 * current screen.
1009 */
1010hterm.Terminal.prototype.restoreCursorAndState = function(both) {
1011 if (both) {
1012 this.primaryScreen_.restoreCursorAndState(this.vt);
1013 this.alternateScreen_.restoreCursorAndState(this.vt);
1014 } else
1015 this.screen_.restoreCursorAndState(this.vt);
1016};
1017
1018/**
Robert Ginda830583c2013-08-07 13:20:46 -07001019 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001020 *
1021 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -07001022 */
1023hterm.Terminal.prototype.setCursorShape = function(shape) {
1024 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001025 this.restyleCursor_();
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001026};
Robert Ginda830583c2013-08-07 13:20:46 -07001027
1028/**
1029 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001030 *
1031 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -07001032 */
1033hterm.Terminal.prototype.getCursorShape = function() {
1034 return this.cursorShape_;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001035};
Robert Ginda830583c2013-08-07 13:20:46 -07001036
1037/**
rginda87b86462011-12-14 13:48:03 -08001038 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001039 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001040 * @param {?number} columnCount
rginda87b86462011-12-14 13:48:03 -08001041 */
1042hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -08001043 if (columnCount == null) {
1044 this.div_.style.width = '100%';
1045 return;
1046 }
1047
Robert Ginda26806d12014-07-24 13:44:07 -07001048 this.div_.style.width = Math.ceil(
1049 this.scrollPort_.characterSize.width *
1050 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001051 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -08001052 this.scheduleSyncCursorPosition_();
1053};
rginda87b86462011-12-14 13:48:03 -08001054
rgindac9bc5502012-01-18 11:48:44 -08001055/**
rginda35c456b2012-02-09 17:29:05 -08001056 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001057 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001058 * @param {?number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -08001059 */
1060hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -08001061 if (rowCount == null) {
1062 this.div_.style.height = '100%';
1063 return;
1064 }
1065
rginda35c456b2012-02-09 17:29:05 -08001066 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -07001067 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -08001068 this.realizeSize_(this.screenSize.width, rowCount);
1069 this.scheduleSyncCursorPosition_();
1070};
1071
1072/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001073 * Deal with terminal size changes.
1074 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001075 * @param {number} columnCount The number of columns.
1076 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001077 */
1078hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
Mike Frysinger0206e262019-06-13 10:18:19 -04001079 let notify = false;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001080
Mike Frysinger0206e262019-06-13 10:18:19 -04001081 if (columnCount != this.screenSize.width) {
1082 notify = true;
1083 this.realizeWidth_(columnCount);
1084 }
1085
1086 if (rowCount != this.screenSize.height) {
1087 notify = true;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001088 this.realizeHeight_(rowCount);
Mike Frysinger0206e262019-06-13 10:18:19 -04001089 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001090
1091 // Send new terminal size to plugin.
Mike Frysinger0206e262019-06-13 10:18:19 -04001092 if (notify) {
1093 this.io.onTerminalResize_(columnCount, rowCount);
1094 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001095};
1096
1097/**
rgindac9bc5502012-01-18 11:48:44 -08001098 * Deal with terminal width changes.
1099 *
1100 * This function does what needs to be done when the terminal width changes
1101 * out from under us. It happens here rather than in onResize_() because this
1102 * code may need to run synchronously to handle programmatic changes of
1103 * terminal width.
1104 *
1105 * Relying on the browser to send us an async resize event means we may not be
1106 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001107 *
1108 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001109 */
1110hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001111 if (columnCount <= 0)
1112 throw new Error('Attempt to realize bad width: ' + columnCount);
1113
rgindac9bc5502012-01-18 11:48:44 -08001114 var deltaColumns = columnCount - this.screen_.getWidth();
Mike Frysinger0206e262019-06-13 10:18:19 -04001115 if (deltaColumns == 0) {
1116 // No change, so don't bother recalculating things.
1117 return;
1118 }
rgindac9bc5502012-01-18 11:48:44 -08001119
rginda87b86462011-12-14 13:48:03 -08001120 this.screenSize.width = columnCount;
1121 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001122
1123 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -04001124 if (this.defaultTabStops)
1125 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -08001126 } else {
1127 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -04001128 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -08001129 break;
1130
1131 this.tabStops_.pop();
1132 }
1133 }
1134
1135 this.screen_.setColumnCount(this.screenSize.width);
1136};
1137
1138/**
1139 * Deal with terminal height changes.
1140 *
1141 * This function does what needs to be done when the terminal height changes
1142 * out from under us. It happens here rather than in onResize_() because this
1143 * code may need to run synchronously to handle programmatic changes of
1144 * terminal height.
1145 *
1146 * Relying on the browser to send us an async resize event means we may not be
1147 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001148 *
1149 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001150 */
1151hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001152 if (rowCount <= 0)
1153 throw new Error('Attempt to realize bad height: ' + rowCount);
1154
rgindac9bc5502012-01-18 11:48:44 -08001155 var deltaRows = rowCount - this.screen_.getHeight();
Mike Frysinger0206e262019-06-13 10:18:19 -04001156 if (deltaRows == 0) {
1157 // No change, so don't bother recalculating things.
1158 return;
1159 }
rgindac9bc5502012-01-18 11:48:44 -08001160
1161 this.screenSize.height = rowCount;
1162
1163 var cursor = this.saveCursor();
1164
1165 if (deltaRows < 0) {
1166 // Screen got smaller.
1167 deltaRows *= -1;
1168 while (deltaRows) {
1169 var lastRow = this.getRowCount() - 1;
1170 if (lastRow - this.scrollbackRows_.length == cursor.row)
1171 break;
1172
1173 if (this.getRowText(lastRow))
1174 break;
1175
1176 this.screen_.popRow();
1177 deltaRows--;
1178 }
1179
1180 var ary = this.screen_.shiftRows(deltaRows);
1181 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1182
1183 // We just removed rows from the top of the screen, we need to update
1184 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001185 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001186 } else if (deltaRows > 0) {
1187 // Screen got larger.
1188
1189 if (deltaRows <= this.scrollbackRows_.length) {
1190 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1191 var rows = this.scrollbackRows_.splice(
1192 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1193 this.screen_.unshiftRows(rows);
1194 deltaRows -= scrollbackCount;
1195 cursor.row += scrollbackCount;
1196 }
1197
1198 if (deltaRows)
1199 this.appendRows_(deltaRows);
1200 }
1201
rginda35c456b2012-02-09 17:29:05 -08001202 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001203 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001204};
1205
1206/**
1207 * Scroll the terminal to the top of the scrollback buffer.
1208 */
1209hterm.Terminal.prototype.scrollHome = function() {
1210 this.scrollPort_.scrollRowToTop(0);
1211};
1212
1213/**
1214 * Scroll the terminal to the end.
1215 */
1216hterm.Terminal.prototype.scrollEnd = function() {
1217 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1218};
1219
1220/**
1221 * Scroll the terminal one page up (minus one line) relative to the current
1222 * position.
1223 */
1224hterm.Terminal.prototype.scrollPageUp = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001225 this.scrollPort_.scrollPageUp();
rginda87b86462011-12-14 13:48:03 -08001226};
1227
1228/**
1229 * Scroll the terminal one page down (minus one line) relative to the current
1230 * position.
1231 */
1232hterm.Terminal.prototype.scrollPageDown = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001233 this.scrollPort_.scrollPageDown();
rginda8ba33642011-12-14 12:31:31 -08001234};
1235
rgindac9bc5502012-01-18 11:48:44 -08001236/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001237 * Scroll the terminal one line up relative to the current position.
1238 */
1239hterm.Terminal.prototype.scrollLineUp = function() {
1240 var i = this.scrollPort_.getTopRowIndex();
1241 this.scrollPort_.scrollRowToTop(i - 1);
1242};
1243
1244/**
1245 * Scroll the terminal one line down relative to the current position.
1246 */
1247hterm.Terminal.prototype.scrollLineDown = function() {
1248 var i = this.scrollPort_.getTopRowIndex();
1249 this.scrollPort_.scrollRowToTop(i + 1);
1250};
1251
1252/**
Robert Ginda40932892012-12-10 17:26:40 -08001253 * Clear primary screen, secondary screen, and the scrollback buffer.
1254 */
1255hterm.Terminal.prototype.wipeContents = function() {
Mike Frysinger9c482b82018-09-07 02:49:36 -04001256 this.clearHome(this.primaryScreen_);
1257 this.clearHome(this.alternateScreen_);
1258
1259 this.clearScrollback();
1260};
1261
1262/**
1263 * Clear scrollback buffer.
1264 */
1265hterm.Terminal.prototype.clearScrollback = function() {
1266 // Move to the end of the buffer in case the screen was scrolled back.
1267 // We're going to throw it away which would leave the display invalid.
1268 this.scrollEnd();
1269
Robert Ginda40932892012-12-10 17:26:40 -08001270 this.scrollbackRows_.length = 0;
1271 this.scrollPort_.resetCache();
1272
Mike Frysinger9c482b82018-09-07 02:49:36 -04001273 [this.primaryScreen_, this.alternateScreen_].forEach((screen) => {
1274 const bottom = screen.getHeight();
1275 this.renumberRows_(0, bottom, screen);
1276 });
Robert Ginda40932892012-12-10 17:26:40 -08001277
1278 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001279 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001280};
1281
1282/**
rgindac9bc5502012-01-18 11:48:44 -08001283 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001284 *
1285 * Perform a full reset to the default values listed in
1286 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001287 */
rginda87b86462011-12-14 13:48:03 -08001288hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001289 this.vt.reset();
1290
rgindac9bc5502012-01-18 11:48:44 -08001291 this.clearAllTabStops();
1292 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001293
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001294 const resetScreen = (screen) => {
1295 // We want to make sure to reset the attributes before we clear the screen.
1296 // The attributes might be used to initialize default/empty rows.
1297 screen.textAttributes.reset();
1298 screen.textAttributes.resetColorPalette();
1299 this.clearHome(screen);
1300 screen.saveCursorAndState(this.vt);
1301 };
1302 resetScreen(this.primaryScreen_);
1303 resetScreen(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001304
Mike Frysinger84301d02017-11-29 13:28:46 -08001305 // Reset terminal options to their default values.
1306 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001307 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1308
Mike Frysinger84301d02017-11-29 13:28:46 -08001309 this.setVTScrollRegion(null, null);
1310
1311 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001312};
1313
rgindac9bc5502012-01-18 11:48:44 -08001314/**
1315 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001316 *
1317 * Perform a soft reset to the default values listed in
1318 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001319 */
rginda0f5c0292012-01-13 11:00:13 -08001320hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001321 this.vt.reset();
1322
rgindab8bc8932012-04-27 12:45:03 -07001323 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001324 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001325
Brad Townb62dfdc2015-03-16 19:07:15 -07001326 // We show the cursor on soft reset but do not alter the blink state.
1327 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1328
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001329 const resetScreen = (screen) => {
1330 // Xterm also resets the color palette on soft reset, even though it doesn't
1331 // seem to be documented anywhere.
1332 screen.textAttributes.reset();
1333 screen.textAttributes.resetColorPalette();
1334 screen.saveCursorAndState(this.vt);
1335 };
1336 resetScreen(this.primaryScreen_);
1337 resetScreen(this.alternateScreen_);
rgindaf522ce02012-04-17 17:49:17 -07001338
rgindab8bc8932012-04-27 12:45:03 -07001339 // The xterm man page explicitly says this will happen on soft reset.
1340 this.setVTScrollRegion(null, null);
1341
1342 // Xterm also shows the cursor on soft reset, but does not alter the blink
1343 // state.
rgindaa19afe22012-01-25 15:40:22 -08001344 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001345};
1346
rgindac9bc5502012-01-18 11:48:44 -08001347/**
1348 * Move the cursor forward to the next tab stop, or to the last column
1349 * if no more tab stops are set.
1350 */
1351hterm.Terminal.prototype.forwardTabStop = function() {
1352 var column = this.screen_.cursorPosition.column;
1353
1354 for (var i = 0; i < this.tabStops_.length; i++) {
1355 if (this.tabStops_[i] > column) {
1356 this.setCursorColumn(this.tabStops_[i]);
1357 return;
1358 }
1359 }
1360
David Benjamin66e954d2012-05-05 21:08:12 -04001361 // xterm does not clear the overflow flag on HT or CHT.
1362 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001363 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001364 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001365};
1366
rgindac9bc5502012-01-18 11:48:44 -08001367/**
1368 * Move the cursor backward to the previous tab stop, or to the first column
1369 * if no previous tab stops are set.
1370 */
1371hterm.Terminal.prototype.backwardTabStop = function() {
1372 var column = this.screen_.cursorPosition.column;
1373
1374 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1375 if (this.tabStops_[i] < column) {
1376 this.setCursorColumn(this.tabStops_[i]);
1377 return;
1378 }
1379 }
1380
1381 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001382};
1383
rgindac9bc5502012-01-18 11:48:44 -08001384/**
1385 * Set a tab stop at the given column.
1386 *
Joel Hockey0f933582019-08-27 18:01:51 -07001387 * @param {number} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001388 */
1389hterm.Terminal.prototype.setTabStop = function(column) {
1390 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1391 if (this.tabStops_[i] == column)
1392 return;
1393
1394 if (this.tabStops_[i] < column) {
1395 this.tabStops_.splice(i + 1, 0, column);
1396 return;
1397 }
1398 }
1399
1400 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001401};
1402
rgindac9bc5502012-01-18 11:48:44 -08001403/**
1404 * Clear the tab stop at the current cursor position.
1405 *
1406 * No effect if there is no tab stop at the current cursor position.
1407 */
1408hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1409 var column = this.screen_.cursorPosition.column;
1410
1411 var i = this.tabStops_.indexOf(column);
1412 if (i == -1)
1413 return;
1414
1415 this.tabStops_.splice(i, 1);
1416};
1417
1418/**
1419 * Clear all tab stops.
1420 */
1421hterm.Terminal.prototype.clearAllTabStops = function() {
1422 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001423 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001424};
1425
1426/**
1427 * Set up the default tab stops, starting from a given column.
1428 *
1429 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001430 * from the specified column, or 0 if no column is provided. It also flags
1431 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001432 *
1433 * This does not clear the existing tab stops first, use clearAllTabStops
1434 * for that.
1435 *
Joel Hockey0f933582019-08-27 18:01:51 -07001436 * @param {number=} opt_start Optional starting zero based starting column,
1437 * useful for filling out missing tab stops when the terminal is resized.
rgindac9bc5502012-01-18 11:48:44 -08001438 */
1439hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1440 var start = opt_start || 0;
1441 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001442 // Round start up to a default tab stop.
1443 start = start - 1 - ((start - 1) % w) + w;
1444 for (var i = start; i < this.screenSize.width; i += w) {
1445 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001446 }
David Benjamin66e954d2012-05-05 21:08:12 -04001447
1448 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001449};
1450
rginda6d397402012-01-17 10:58:29 -08001451/**
rginda8ba33642011-12-14 12:31:31 -08001452 * Interpret a sequence of characters.
1453 *
1454 * Incomplete escape sequences are buffered until the next call.
1455 *
1456 * @param {string} str Sequence of characters to interpret or pass through.
1457 */
1458hterm.Terminal.prototype.interpret = function(str) {
rginda8ba33642011-12-14 12:31:31 -08001459 this.scheduleSyncCursorPosition_();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001460 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001461};
1462
1463/**
1464 * Take over the given DIV for use as the terminal display.
1465 *
Joel Hockey0f933582019-08-27 18:01:51 -07001466 * @param {!Element} div The div to use as the terminal display.
rginda8ba33642011-12-14 12:31:31 -08001467 */
1468hterm.Terminal.prototype.decorate = function(div) {
Mike Frysinger5768a9d2017-12-26 12:57:44 -05001469 const charset = div.ownerDocument.characterSet.toLowerCase();
1470 if (charset != 'utf-8') {
1471 console.warn(`Document encoding should be set to utf-8, not "${charset}";` +
1472 ` Add <meta charset='utf-8'/> to your HTML <head> to fix.`);
1473 }
1474
rginda87b86462011-12-14 13:48:03 -08001475 this.div_ = div;
1476
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001477 this.accessibilityReader_ = new hterm.AccessibilityReader(div);
1478
Adrián Pérez-Orozco394e64f2018-12-17 17:20:16 -08001479 this.scrollPort_.decorate(div, () => this.setupScrollPort_());
1480};
1481
1482/**
1483 * Initialisation of ScrollPort properties which need to be set after its DOM
1484 * has been initialised.
1485 * @private
1486 */
1487hterm.Terminal.prototype.setupScrollPort_ = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -07001488 this.scrollPort_.setBackgroundImage(
1489 this.prefs_.getString('background-image'));
1490 this.scrollPort_.setBackgroundSize(this.prefs_.getString('background-size'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001491 this.scrollPort_.setBackgroundPosition(
Joel Hockeyd4fca732019-09-20 16:57:03 -07001492 this.prefs_.getString('background-position'));
1493 this.scrollPort_.setUserCssUrl(this.prefs_.getString('user-css'));
1494 this.scrollPort_.setUserCssText(this.prefs_.getString('user-css-text'));
1495 this.scrollPort_.setAccessibilityReader(
1496 lib.notNull(this.accessibilityReader_));
rginda30f20f62012-04-05 16:36:19 -07001497
rginda0918b652012-04-04 11:26:24 -07001498 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001499
Joel Hockeyd4fca732019-09-20 16:57:03 -07001500 this.setFontSize(this.prefs_.getNumber('font-size'));
rginda9f5222b2012-03-05 11:53:28 -08001501 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001502
Joel Hockeyd4fca732019-09-20 16:57:03 -07001503 this.setScrollbarVisible(this.prefs_.getBoolean('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001504 this.setScrollWheelMoveMultipler(
Joel Hockeyd4fca732019-09-20 16:57:03 -07001505 this.prefs_.getNumber('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001506
rginda8ba33642011-12-14 12:31:31 -08001507 this.document_ = this.scrollPort_.getDocument();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001508 this.accessibilityReader_.decorate(this.document_);
rginda8ba33642011-12-14 12:31:31 -08001509
Evan Jones5f9df812016-12-06 09:38:58 -05001510 this.document_.body.oncontextmenu = function() { return false; };
Mike Frysingercc114512017-09-11 21:39:17 -04001511 this.contextMenu.setDocument(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07001512
1513 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001514 var screenNode = this.scrollPort_.getScreenNode();
Joel Hockeyd4fca732019-09-20 16:57:03 -07001515 screenNode.addEventListener(
1516 'mousedown', /** @type {!EventListener} */ (onMouse));
1517 screenNode.addEventListener(
1518 'mouseup', /** @type {!EventListener} */ (onMouse));
1519 screenNode.addEventListener(
1520 'mousemove', /** @type {!EventListener} */ (onMouse));
rginda4bba5e12012-06-20 16:15:30 -07001521 this.scrollPort_.onScrollWheel = onMouse;
1522
Joel Hockeyd4fca732019-09-20 16:57:03 -07001523 screenNode.addEventListener(
1524 'keydown',
1525 /** @type {!EventListener} */ (this.onKeyboardActivity_.bind(this)));
Mike Frysinger02ded6d2018-06-21 14:25:20 -04001526
Toni Barzic0bfa8922013-11-22 11:18:35 -08001527 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001528 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001529 // Listen for mousedown events on the screenNode as in FF the focus
1530 // events don't bubble.
1531 screenNode.addEventListener('mousedown', function() {
1532 setTimeout(this.onFocusChange_.bind(this, true));
1533 }.bind(this));
1534
Toni Barzic0bfa8922013-11-22 11:18:35 -08001535 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001536 'blur', this.onFocusChange_.bind(this, false));
1537
1538 var style = this.document_.createElement('style');
1539 style.textContent =
1540 ('.cursor-node[focus="false"] {' +
1541 ' box-sizing: border-box;' +
1542 ' background-color: transparent !important;' +
1543 ' border-width: 2px;' +
1544 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001545 '}' +
Mike Frysingercc114512017-09-11 21:39:17 -04001546 'menu {' +
1547 ' margin: 0;' +
1548 ' padding: 0;' +
1549 ' cursor: var(--hterm-mouse-cursor-pointer);' +
1550 '}' +
1551 'menuitem {' +
1552 ' white-space: nowrap;' +
1553 ' border-bottom: 1px dashed;' +
1554 ' display: block;' +
1555 ' padding: 0.3em 0.3em 0 0.3em;' +
1556 '}' +
1557 'menuitem.separator {' +
1558 ' border-bottom: none;' +
1559 ' height: 0.5em;' +
1560 ' padding: 0;' +
1561 '}' +
1562 'menuitem:hover {' +
1563 ' color: var(--hterm-cursor-color);' +
1564 '}' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001565 '.wc-node {' +
1566 ' display: inline-block;' +
1567 ' text-align: center;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001568 ' width: calc(var(--hterm-charsize-width) * 2);' +
Mike Frysinger4036f6e2017-05-31 14:02:55 -04001569 ' line-height: var(--hterm-charsize-height);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001570 '}' +
1571 ':root {' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001572 ' --hterm-charsize-width: ' + this.scrollPort_.characterSize.width + 'px;' +
1573 ' --hterm-charsize-height: ' + this.scrollPort_.characterSize.height + 'px;' +
Mike Frysingera27c0502017-08-23 22:37:10 -04001574 // Default position hides the cursor for when the window is initializing.
1575 ' --hterm-cursor-offset-col: -1;' +
1576 ' --hterm-cursor-offset-row: -1;' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001577 ' --hterm-blink-node-duration: 0.7s;' +
Mike Frysinger67f58f82018-11-22 13:38:22 -05001578 ' --hterm-mouse-cursor-default: default;' +
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001579 ' --hterm-mouse-cursor-text: text;' +
Mike Frysinger67f58f82018-11-22 13:38:22 -05001580 ' --hterm-mouse-cursor-pointer: pointer;' +
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001581 ' --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001582 '}' +
Mike Frysingerd3907202017-10-23 01:23:50 -04001583 '.uri-node:hover {' +
1584 ' text-decoration: underline;' +
Mike Frysinger67f58f82018-11-22 13:38:22 -05001585 ' cursor: var(--hterm-mouse-cursor-pointer);' +
Mike Frysingerd3907202017-10-23 01:23:50 -04001586 '}' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001587 '@keyframes blink {' +
1588 ' from { opacity: 1.0; }' +
1589 ' to { opacity: 0.0; }' +
1590 '}' +
1591 '.blink-node {' +
1592 ' animation-name: blink;' +
1593 ' animation-duration: var(--hterm-blink-node-duration);' +
1594 ' animation-iteration-count: infinite;' +
1595 ' animation-timing-function: ease-in-out;' +
1596 ' animation-direction: alternate;' +
rginda8e92a692012-05-20 19:37:20 -07001597 '}');
Mike Frysingerb74a6472018-06-22 13:37:08 -04001598 // Insert this stock style as the first node so that any user styles will
1599 // override w/out having to use !important everywhere. The rules above mix
1600 // runtime variables with default ones designed to be overridden by the user,
1601 // but we can wait for a concrete case from the users to determine the best
1602 // way to split the sheet up to before & after the user-css settings.
1603 this.document_.head.insertBefore(style, this.document_.head.firstChild);
rginda8e92a692012-05-20 19:37:20 -07001604
rginda8ba33642011-12-14 12:31:31 -08001605 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001606 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001607 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001608 this.cursorNode_.style.cssText =
1609 ('position: absolute;' +
Mike Frysinger44c32202017-08-05 01:13:09 -04001610 'left: calc(var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));' +
1611 'top: calc(var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));' +
Mike Frysingerd60f4c22018-03-15 21:25:30 -07001612 'display: ' + (this.options_.cursorVisible ? '' : 'none') + ';' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001613 'width: var(--hterm-charsize-width);' +
1614 'height: var(--hterm-charsize-height);' +
Mike Frysinger2fd079a2018-09-02 01:46:12 -04001615 'background-color: var(--hterm-cursor-color);' +
1616 'border-color: var(--hterm-cursor-color);' +
Rob Spies06533ba2014-04-24 11:20:37 -07001617 '-webkit-transition: opacity, background-color 100ms linear;' +
1618 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001619
Mike Frysingerf02a2cb2017-12-21 00:34:03 -05001620 this.setCursorColor();
Robert Gindafb1be6a2013-12-11 11:56:22 -08001621 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1622 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001623
rginda8ba33642011-12-14 12:31:31 -08001624 this.document_.body.appendChild(this.cursorNode_);
1625
rgindad5613292012-06-19 15:40:37 -07001626 // When 'enableMouseDragScroll' is off we reposition this element directly
1627 // under the mouse cursor after a click. This makes Chrome associate
1628 // subsequent mousemove events with the scroll-blocker. Since the
1629 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1630 // events do not cause the scrollport to scroll.
1631 //
1632 // It's a hack, but it's the cleanest way I could find.
1633 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001634 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
Raymes Khoury6dce2f82018-04-12 15:38:58 +10001635 this.scrollBlockerNode_.setAttribute('aria-hidden', 'true');
rgindad5613292012-06-19 15:40:37 -07001636 this.scrollBlockerNode_.style.cssText =
1637 ('position: absolute;' +
1638 'top: -99px;' +
1639 'display: block;' +
1640 'width: 10px;' +
1641 'height: 10px;');
1642 this.document_.body.appendChild(this.scrollBlockerNode_);
1643
rgindad5613292012-06-19 15:40:37 -07001644 this.scrollPort_.onScrollWheel = onMouse;
1645 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1646 ].forEach(function(event) {
1647 this.scrollBlockerNode_.addEventListener(event, onMouse);
Joel Hockeyd4fca732019-09-20 16:57:03 -07001648 this.cursorNode_.addEventListener(
1649 event, /** @type {!EventListener} */ (onMouse));
1650 this.document_.addEventListener(
1651 event, /** @type {!EventListener} */ (onMouse));
rgindad5613292012-06-19 15:40:37 -07001652 }.bind(this));
1653
1654 this.cursorNode_.addEventListener('mousedown', function() {
1655 setTimeout(this.focus.bind(this));
1656 }.bind(this));
1657
rginda8ba33642011-12-14 12:31:31 -08001658 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001659
rginda87b86462011-12-14 13:48:03 -08001660 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001661 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001662};
1663
rginda0918b652012-04-04 11:26:24 -07001664/**
1665 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001666 *
Joel Hockey0f933582019-08-27 18:01:51 -07001667 * @return {!Document}
rginda0918b652012-04-04 11:26:24 -07001668 */
rginda87b86462011-12-14 13:48:03 -08001669hterm.Terminal.prototype.getDocument = function() {
1670 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001671};
1672
1673/**
rginda0918b652012-04-04 11:26:24 -07001674 * Focus the terminal.
1675 */
1676hterm.Terminal.prototype.focus = function() {
1677 this.scrollPort_.focus();
1678};
1679
1680/**
Theodore Duboiscea9b782019-09-02 17:48:00 -07001681 * Unfocus the terminal.
1682 */
1683hterm.Terminal.prototype.blur = function() {
1684 this.scrollPort_.blur();
1685};
1686
1687/**
rginda8ba33642011-12-14 12:31:31 -08001688 * Return the HTML Element for a given row index.
1689 *
1690 * This is a method from the RowProvider interface. The ScrollPort uses
1691 * it to fetch rows on demand as they are scrolled into view.
1692 *
1693 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1694 * pairs to conserve memory.
1695 *
Joel Hockey0f933582019-08-27 18:01:51 -07001696 * @param {number} index The zero-based row index, measured relative to the
rginda8ba33642011-12-14 12:31:31 -08001697 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001698 * largest indices.
Joel Hockey0f933582019-08-27 18:01:51 -07001699 * @return {!Element} The 'x-row' element containing for the requested row.
Joel Hockeyd4fca732019-09-20 16:57:03 -07001700 * @override
rginda8ba33642011-12-14 12:31:31 -08001701 */
1702hterm.Terminal.prototype.getRowNode = function(index) {
1703 if (index < this.scrollbackRows_.length)
1704 return this.scrollbackRows_[index];
1705
1706 var screenIndex = index - this.scrollbackRows_.length;
1707 return this.screen_.rowsArray[screenIndex];
1708};
1709
1710/**
1711 * Return the text content for a given range of rows.
1712 *
1713 * This is a method from the RowProvider interface. The ScrollPort uses
1714 * it to fetch text content on demand when the user attempts to copy their
1715 * selection to the clipboard.
1716 *
Joel Hockey0f933582019-08-27 18:01:51 -07001717 * @param {number} start The zero-based row index to start from, measured
rginda8ba33642011-12-14 12:31:31 -08001718 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001719 * always have the largest indices.
Joel Hockey0f933582019-08-27 18:01:51 -07001720 * @param {number} end The zero-based row index to end on, measured
rginda8ba33642011-12-14 12:31:31 -08001721 * relative to the start of the scrollback buffer.
1722 * @return {string} A single string containing the text value of the range of
1723 * rows. Lines will be newline delimited, with no trailing newline.
1724 */
1725hterm.Terminal.prototype.getRowsText = function(start, end) {
1726 var ary = [];
1727 for (var i = start; i < end; i++) {
1728 var node = this.getRowNode(i);
1729 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001730 if (i < end - 1 && !node.getAttribute('line-overflow'))
1731 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001732 }
1733
rgindaa09e7332012-08-17 12:49:51 -07001734 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001735};
1736
1737/**
1738 * Return the text content for a given row.
1739 *
1740 * This is a method from the RowProvider interface. The ScrollPort uses
1741 * it to fetch text content on demand when the user attempts to copy their
1742 * selection to the clipboard.
1743 *
Joel Hockey0f933582019-08-27 18:01:51 -07001744 * @param {number} index The zero-based row index to return, measured
rginda8ba33642011-12-14 12:31:31 -08001745 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001746 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001747 * @return {string} A string containing the text value of the selected row.
1748 */
1749hterm.Terminal.prototype.getRowText = function(index) {
1750 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001751 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001752};
1753
1754/**
1755 * Return the total number of rows in the addressable screen and in the
1756 * scrollback buffer of this terminal.
1757 *
1758 * This is a method from the RowProvider interface. The ScrollPort uses
1759 * it to compute the size of the scrollbar.
1760 *
Joel Hockey0f933582019-08-27 18:01:51 -07001761 * @return {number} The number of rows in this terminal.
Joel Hockeyd4fca732019-09-20 16:57:03 -07001762 * @override
rginda8ba33642011-12-14 12:31:31 -08001763 */
1764hterm.Terminal.prototype.getRowCount = function() {
1765 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1766};
1767
1768/**
1769 * Create DOM nodes for new rows and append them to the end of the terminal.
1770 *
1771 * This is the only correct way to add a new DOM node for a row. Notice that
1772 * the new row is appended to the bottom of the list of rows, and does not
1773 * require renumbering (of the rowIndex property) of previous rows.
1774 *
1775 * If you think you want a new blank row somewhere in the middle of the
1776 * terminal, look into moveRows_().
1777 *
1778 * This method does not pay attention to vtScrollTop/Bottom, since you should
1779 * be using moveRows() in cases where they would matter.
1780 *
1781 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001782 *
1783 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001784 */
1785hterm.Terminal.prototype.appendRows_ = function(count) {
1786 var cursorRow = this.screen_.rowsArray.length;
1787 var offset = this.scrollbackRows_.length + cursorRow;
1788 for (var i = 0; i < count; i++) {
1789 var row = this.document_.createElement('x-row');
1790 row.appendChild(this.document_.createTextNode(''));
1791 row.rowIndex = offset + i;
1792 this.screen_.pushRow(row);
1793 }
1794
1795 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1796 if (extraRows > 0) {
1797 var ary = this.screen_.shiftRows(extraRows);
1798 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001799 if (this.scrollPort_.isScrolledEnd)
1800 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001801 }
1802
1803 if (cursorRow >= this.screen_.rowsArray.length)
1804 cursorRow = this.screen_.rowsArray.length - 1;
1805
rginda87b86462011-12-14 13:48:03 -08001806 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001807};
1808
1809/**
1810 * Relocate rows from one part of the addressable screen to another.
1811 *
1812 * This is used to recycle rows during VT scrolls (those which are driven
1813 * by VT commands, rather than by the user manipulating the scrollbar.)
1814 *
1815 * In this case, the blank lines scrolled into the scroll region are made of
1816 * the nodes we scrolled off. These have their rowIndex properties carefully
1817 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001818 *
1819 * @param {number} fromIndex The start index.
1820 * @param {number} count The number of rows to move.
1821 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001822 */
1823hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1824 var ary = this.screen_.removeRows(fromIndex, count);
1825 this.screen_.insertRows(toIndex, ary);
1826
1827 var start, end;
1828 if (fromIndex < toIndex) {
1829 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001830 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001831 } else {
1832 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001833 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001834 }
1835
1836 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001837 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001838};
1839
1840/**
1841 * Renumber the rowIndex property of the given range of rows.
1842 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001843 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08001844 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001845 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001846 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05001847 *
1848 * @param {number} start The start index.
1849 * @param {number} end The end index.
Joel Hockey0f933582019-08-27 18:01:51 -07001850 * @param {!hterm.Screen=} opt_screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08001851 */
Robert Ginda40932892012-12-10 17:26:40 -08001852hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1853 var screen = opt_screen || this.screen_;
1854
rginda8ba33642011-12-14 12:31:31 -08001855 var offset = this.scrollbackRows_.length;
1856 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001857 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001858 }
1859};
1860
1861/**
1862 * Print a string to the terminal.
1863 *
1864 * This respects the current insert and wraparound modes. It will add new lines
1865 * to the end of the terminal, scrolling off the top into the scrollback buffer
1866 * if necessary.
1867 *
1868 * The string is *not* parsed for escape codes. Use the interpret() method if
1869 * that's what you're after.
1870 *
1871 * @param{string} str The string to print.
1872 */
1873hterm.Terminal.prototype.print = function(str) {
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001874 this.scheduleSyncCursorPosition_();
1875
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001876 // Basic accessibility output for the screen reader.
Raymes Khoury177aec72018-06-26 10:58:53 +10001877 this.accessibilityReader_.announce(str);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001878
rgindaa9abdd82012-08-06 18:05:09 -07001879 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001880
Ricky Liang48f05cb2013-12-31 23:35:29 +08001881 var strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04001882 // Fun edge case: If the string only contains zero width codepoints (like
1883 // combining characters), we make sure to iterate at least once below.
1884 if (strWidth == 0 && str)
1885 strWidth = 1;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001886
1887 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001888 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1889 this.screen_.commitLineOverflow();
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001890 this.newLine(true);
rgindaa09e7332012-08-17 12:49:51 -07001891 }
rgindaa19afe22012-01-25 15:40:22 -08001892
Ricky Liang48f05cb2013-12-31 23:35:29 +08001893 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001894 var didOverflow = false;
1895 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001896
rgindaa9abdd82012-08-06 18:05:09 -07001897 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1898 didOverflow = true;
1899 count = this.screenSize.width - this.screen_.cursorPosition.column;
1900 }
rgindaa19afe22012-01-25 15:40:22 -08001901
rgindaa9abdd82012-08-06 18:05:09 -07001902 if (didOverflow && !this.options_.wraparound) {
1903 // If the string overflowed the line but wraparound is off, then the
1904 // last printed character should be the last of the string.
1905 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001906 substr = lib.wc.substr(str, startOffset, count - 1) +
1907 lib.wc.substr(str, strWidth - 1);
1908 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001909 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001910 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001911 }
rgindaa19afe22012-01-25 15:40:22 -08001912
Ricky Liang48f05cb2013-12-31 23:35:29 +08001913 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1914 for (var i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001915 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
1916 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001917
1918 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001919 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001920 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001921 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001922 }
1923 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001924 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07001925 }
1926
1927 this.screen_.maybeClipCurrentRow();
1928 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001929 }
rginda8ba33642011-12-14 12:31:31 -08001930
rginda9f5222b2012-03-05 11:53:28 -08001931 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001932 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001933};
1934
1935/**
rginda87b86462011-12-14 13:48:03 -08001936 * Set the VT scroll region.
1937 *
rginda87b86462011-12-14 13:48:03 -08001938 * This also resets the cursor position to the absolute (0, 0) position, since
1939 * that's what xterm appears to do.
1940 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001941 * Setting the scroll region to the full height of the terminal will clear
1942 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1943 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1944 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1945 * continue to work as most users would expect.
1946 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001947 * @param {?number} scrollTop The zero-based top of the scroll region.
1948 * @param {?number} scrollBottom The zero-based bottom of the scroll region,
rginda87b86462011-12-14 13:48:03 -08001949 * inclusive.
1950 */
1951hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001952 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001953 this.vtScrollTop_ = null;
1954 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001955 } else {
1956 this.vtScrollTop_ = scrollTop;
1957 this.vtScrollBottom_ = scrollBottom;
1958 }
rginda87b86462011-12-14 13:48:03 -08001959};
1960
1961/**
rginda8ba33642011-12-14 12:31:31 -08001962 * Return the top row index according to the VT.
1963 *
1964 * This will return 0 unless the terminal has been told to restrict scrolling
1965 * to some lower row. It is used for some VT cursor positioning and scrolling
1966 * commands.
1967 *
Joel Hockey0f933582019-08-27 18:01:51 -07001968 * @return {number} The topmost row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001969 */
1970hterm.Terminal.prototype.getVTScrollTop = function() {
1971 if (this.vtScrollTop_ != null)
1972 return this.vtScrollTop_;
1973
1974 return 0;
rginda87b86462011-12-14 13:48:03 -08001975};
rginda8ba33642011-12-14 12:31:31 -08001976
1977/**
1978 * Return the bottom row index according to the VT.
1979 *
1980 * This will return the height of the terminal unless the it has been told to
1981 * restrict scrolling to some higher row. It is used for some VT cursor
1982 * positioning and scrolling commands.
1983 *
Joel Hockey0f933582019-08-27 18:01:51 -07001984 * @return {number} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001985 */
1986hterm.Terminal.prototype.getVTScrollBottom = function() {
1987 if (this.vtScrollBottom_ != null)
1988 return this.vtScrollBottom_;
1989
rginda87b86462011-12-14 13:48:03 -08001990 return this.screenSize.height - 1;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001991};
rginda8ba33642011-12-14 12:31:31 -08001992
1993/**
1994 * Process a '\n' character.
1995 *
1996 * If the cursor is on the final row of the terminal this will append a new
1997 * blank row to the screen and scroll the topmost row into the scrollback
1998 * buffer.
1999 *
2000 * Otherwise, this moves the cursor to column zero of the next row.
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002001 *
2002 * @param {boolean=} dueToOverflow Whether the newline is due to wraparound of
2003 * the terminal.
rginda8ba33642011-12-14 12:31:31 -08002004 */
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002005hterm.Terminal.prototype.newLine = function(dueToOverflow = false) {
2006 if (!dueToOverflow)
2007 this.accessibilityReader_.newLine();
2008
Robert Ginda9937abc2013-07-25 16:09:23 -07002009 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
2010 this.screen_.rowsArray.length - 1);
2011
2012 if (this.vtScrollBottom_ != null) {
2013 // A VT Scroll region is active, we never append new rows.
2014 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
2015 // We're at the end of the VT Scroll Region, perform a VT scroll.
2016 this.vtScrollUp(1);
2017 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
2018 } else if (cursorAtEndOfScreen) {
2019 // We're at the end of the screen, the only thing to do is put the
2020 // cursor to column 0.
2021 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
2022 } else {
2023 // Anywhere else, advance the cursor row, and reset the column.
2024 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
2025 }
2026 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07002027 // We're at the end of the screen. Append a new row to the terminal,
2028 // shifting the top row into the scrollback.
2029 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08002030 } else {
rginda87b86462011-12-14 13:48:03 -08002031 // Anywhere else in the screen just moves the cursor.
2032 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08002033 }
2034};
2035
2036/**
2037 * Like newLine(), except maintain the cursor column.
2038 */
2039hterm.Terminal.prototype.lineFeed = function() {
2040 var column = this.screen_.cursorPosition.column;
2041 this.newLine();
2042 this.setCursorColumn(column);
2043};
2044
2045/**
rginda87b86462011-12-14 13:48:03 -08002046 * If autoCarriageReturn is set then newLine(), else lineFeed().
2047 */
2048hterm.Terminal.prototype.formFeed = function() {
2049 if (this.options_.autoCarriageReturn) {
2050 this.newLine();
2051 } else {
2052 this.lineFeed();
2053 }
2054};
2055
2056/**
2057 * Move the cursor up one row, possibly inserting a blank line.
2058 *
2059 * The cursor column is not changed.
2060 */
2061hterm.Terminal.prototype.reverseLineFeed = function() {
2062 var scrollTop = this.getVTScrollTop();
2063 var currentRow = this.screen_.cursorPosition.row;
2064
2065 if (currentRow == scrollTop) {
2066 this.insertLines(1);
2067 } else {
2068 this.setAbsoluteCursorRow(currentRow - 1);
2069 }
2070};
2071
2072/**
rginda8ba33642011-12-14 12:31:31 -08002073 * Replace all characters to the left of the current cursor with the space
2074 * character.
2075 *
2076 * TODO(rginda): This should probably *remove* the characters (not just replace
2077 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07002078 * position.
rginda8ba33642011-12-14 12:31:31 -08002079 */
2080hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08002081 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002082 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002083 const count = cursor.column + 1;
Mike Frysinger73e56462019-07-17 00:23:46 -05002084 this.screen_.overwriteString(' '.repeat(count), count);
rginda87b86462011-12-14 13:48:03 -08002085 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002086};
2087
2088/**
David Benjamin684a9b72012-05-01 17:19:58 -04002089 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08002090 *
2091 * The cursor position is unchanged.
2092 *
Robert Gindaf2547f12012-10-25 20:36:21 -07002093 * If the current background color is not the default background color this
2094 * will insert spaces rather than delete. This is unfortunate because the
2095 * trailing space will affect text selection, but it's difficult to come up
2096 * with a way to style empty space that wouldn't trip up the hterm.Screen
2097 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07002098 *
2099 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
2100 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
2101 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05002102 *
Joel Hockey0f933582019-08-27 18:01:51 -07002103 * @param {number=} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08002104 */
2105hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07002106 if (this.screen_.cursorPosition.overflow)
2107 return;
2108
Robert Ginda7fd57082012-09-25 14:41:47 -07002109 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
2110 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07002111
2112 if (this.screen_.textAttributes.background ===
2113 this.screen_.textAttributes.DEFAULT_COLOR) {
2114 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08002115 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07002116 this.screen_.cursorPosition.column + count) {
2117 this.screen_.deleteChars(count);
2118 this.clearCursorOverflow();
2119 return;
2120 }
2121 }
2122
rginda87b86462011-12-14 13:48:03 -08002123 var cursor = this.saveCursor();
Mike Frysinger73e56462019-07-17 00:23:46 -05002124 this.screen_.overwriteString(' '.repeat(count), count);
rginda87b86462011-12-14 13:48:03 -08002125 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002126 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002127};
2128
2129/**
2130 * Erase the current line.
2131 *
2132 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002133 */
2134hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08002135 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002136 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08002137 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002138 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002139};
2140
2141/**
David Benjamina08d78f2012-05-05 00:28:49 -04002142 * Erase all characters from the start of the screen to the current cursor
2143 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002144 *
2145 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002146 */
2147hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08002148 var cursor = this.saveCursor();
2149
2150 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002151
David Benjamina08d78f2012-05-05 00:28:49 -04002152 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002153 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002154 this.screen_.clearCursorRow();
2155 }
2156
rginda87b86462011-12-14 13:48:03 -08002157 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002158 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002159};
2160
2161/**
2162 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002163 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002164 *
2165 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002166 */
2167hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08002168 var cursor = this.saveCursor();
2169
2170 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002171
David Benjamina08d78f2012-05-05 00:28:49 -04002172 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08002173 for (var i = cursor.row + 1; i <= bottom; i++) {
2174 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002175 this.screen_.clearCursorRow();
2176 }
2177
rginda87b86462011-12-14 13:48:03 -08002178 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002179 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002180};
2181
2182/**
2183 * Fill the terminal with a given character.
2184 *
2185 * This methods does not respect the VT scroll region.
2186 *
2187 * @param {string} ch The character to use for the fill.
2188 */
2189hterm.Terminal.prototype.fill = function(ch) {
2190 var cursor = this.saveCursor();
2191
2192 this.setAbsoluteCursorPosition(0, 0);
2193 for (var row = 0; row < this.screenSize.height; row++) {
2194 for (var col = 0; col < this.screenSize.width; col++) {
2195 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002196 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002197 }
2198 }
2199
2200 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002201};
2202
2203/**
rginda9ea433c2012-03-16 11:57:00 -07002204 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002205 *
rginda9ea433c2012-03-16 11:57:00 -07002206 * This does not respect the scroll region.
2207 *
Joel Hockey0f933582019-08-27 18:01:51 -07002208 * @param {!hterm.Screen=} opt_screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002209 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002210 */
rginda9ea433c2012-03-16 11:57:00 -07002211hterm.Terminal.prototype.clearHome = function(opt_screen) {
2212 var screen = opt_screen || this.screen_;
2213 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002214
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002215 this.accessibilityReader_.clear();
2216
rginda11057d52012-04-25 12:29:56 -07002217 if (bottom == 0) {
2218 // Empty screen, nothing to do.
2219 return;
2220 }
2221
rgindae4d29232012-01-19 10:47:13 -08002222 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002223 screen.setCursorPosition(i, 0);
2224 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002225 }
2226
rginda9ea433c2012-03-16 11:57:00 -07002227 screen.setCursorPosition(0, 0);
2228};
2229
2230/**
2231 * Erase the entire display without changing the cursor position.
2232 *
2233 * The cursor position is unchanged. This does not respect the scroll
2234 * region.
2235 *
Joel Hockey0f933582019-08-27 18:01:51 -07002236 * @param {!hterm.Screen=} opt_screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002237 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002238 */
2239hterm.Terminal.prototype.clear = function(opt_screen) {
2240 var screen = opt_screen || this.screen_;
2241 var cursor = screen.cursorPosition.clone();
2242 this.clearHome(screen);
2243 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002244};
2245
2246/**
2247 * VT command to insert lines at the current cursor row.
2248 *
2249 * This respects the current scroll region. Rows pushed off the bottom are
2250 * lost (they won't show up in the scrollback buffer).
2251 *
Joel Hockey0f933582019-08-27 18:01:51 -07002252 * @param {number} count The number of lines to insert.
rginda8ba33642011-12-14 12:31:31 -08002253 */
2254hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07002255 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002256
2257 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002258 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002259
Robert Ginda579186b2012-09-26 11:40:04 -07002260 // The moveCount is the number of rows we need to relocate to make room for
2261 // the new row(s). The count is the distance to move them.
2262 var moveCount = bottom - cursorRow - count + 1;
2263 if (moveCount)
2264 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08002265
Robert Ginda579186b2012-09-26 11:40:04 -07002266 for (var i = count - 1; i >= 0; i--) {
2267 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002268 this.screen_.clearCursorRow();
2269 }
rginda8ba33642011-12-14 12:31:31 -08002270};
2271
2272/**
2273 * VT command to delete lines at the current cursor row.
2274 *
2275 * New rows are added to the bottom of scroll region to take their place. New
2276 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002277 *
2278 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002279 */
2280hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08002281 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002282
rginda87b86462011-12-14 13:48:03 -08002283 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08002284 var bottom = this.getVTScrollBottom();
2285
rginda87b86462011-12-14 13:48:03 -08002286 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002287 count = Math.min(count, maxCount);
2288
rginda87b86462011-12-14 13:48:03 -08002289 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08002290 if (count != maxCount)
2291 this.moveRows_(top, count, moveStart);
2292
2293 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002294 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002295 this.screen_.clearCursorRow();
2296 }
2297
rginda87b86462011-12-14 13:48:03 -08002298 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002299 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002300};
2301
2302/**
2303 * Inserts the given number of spaces at the current cursor position.
2304 *
rginda87b86462011-12-14 13:48:03 -08002305 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002306 *
2307 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002308 */
2309hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08002310 var cursor = this.saveCursor();
2311
Mike Frysinger73e56462019-07-17 00:23:46 -05002312 const ws = ' '.repeat(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002313 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002314 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002315
2316 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002317 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002318};
2319
2320/**
2321 * Forward-delete the specified number of characters starting at the cursor
2322 * position.
2323 *
Joel Hockey0f933582019-08-27 18:01:51 -07002324 * @param {number} count The number of characters to delete.
rginda8ba33642011-12-14 12:31:31 -08002325 */
2326hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002327 var deleted = this.screen_.deleteChars(count);
2328 if (deleted && !this.screen_.textAttributes.isDefault()) {
2329 var cursor = this.saveCursor();
2330 this.setCursorColumn(this.screenSize.width - deleted);
Mike Frysinger73e56462019-07-17 00:23:46 -05002331 this.screen_.insertString(' '.repeat(deleted));
Robert Ginda7fd57082012-09-25 14:41:47 -07002332 this.restoreCursor(cursor);
2333 }
2334
David Benjamin54e8bf62012-06-01 22:31:40 -04002335 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002336};
2337
2338/**
2339 * Shift rows in the scroll region upwards by a given number of lines.
2340 *
2341 * New rows are inserted at the bottom of the scroll region to fill the
2342 * vacated rows. The new rows not filled out with the current text attributes.
2343 *
2344 * This function does not affect the scrollback rows at all. Rows shifted
2345 * off the top are lost.
2346 *
rginda87b86462011-12-14 13:48:03 -08002347 * The cursor position is not altered.
2348 *
Joel Hockey0f933582019-08-27 18:01:51 -07002349 * @param {number} count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002350 */
2351hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002352 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002353
rginda87b86462011-12-14 13:48:03 -08002354 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002355 this.deleteLines(count);
2356
rginda87b86462011-12-14 13:48:03 -08002357 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002358};
2359
2360/**
2361 * Shift rows below the cursor down by a given number of lines.
2362 *
2363 * This function respects the current scroll region.
2364 *
2365 * New rows are inserted at the top of the scroll region to fill the
2366 * vacated rows. The new rows not filled out with the current text attributes.
2367 *
2368 * This function does not affect the scrollback rows at all. Rows shifted
2369 * off the bottom are lost.
2370 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07002371 * @param {number} count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002372 */
Joel Hockeyd4fca732019-09-20 16:57:03 -07002373hterm.Terminal.prototype.vtScrollDown = function(count) {
rginda87b86462011-12-14 13:48:03 -08002374 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002375
rginda87b86462011-12-14 13:48:03 -08002376 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
Joel Hockeyd4fca732019-09-20 16:57:03 -07002377 this.insertLines(count);
rginda8ba33642011-12-14 12:31:31 -08002378
rginda87b86462011-12-14 13:48:03 -08002379 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002380};
2381
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002382/**
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002383 * Enable accessibility-friendly features that have a performance impact.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002384 *
2385 * This will generate additional DOM nodes in an aria-live region that will
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002386 * cause Assitive Technology to announce the output of the terminal. It also
2387 * enables other features that aid assistive technology. All the features gated
2388 * behind this flag have a performance impact on the terminal which is why they
2389 * are made optional.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002390 *
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002391 * @param {boolean} enabled Whether to enable accessibility-friendly features.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002392 */
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002393hterm.Terminal.prototype.setAccessibilityEnabled = function(enabled) {
Raymes Khoury177aec72018-06-26 10:58:53 +10002394 this.accessibilityReader_.setAccessibilityEnabled(enabled);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002395};
rginda87b86462011-12-14 13:48:03 -08002396
rginda8ba33642011-12-14 12:31:31 -08002397/**
2398 * Set the cursor position.
2399 *
2400 * The cursor row is relative to the scroll region if the terminal has
2401 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2402 *
Joel Hockey0f933582019-08-27 18:01:51 -07002403 * @param {number} row The new zero-based cursor row.
2404 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002405 */
2406hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2407 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002408 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002409 } else {
rginda87b86462011-12-14 13:48:03 -08002410 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002411 }
rginda87b86462011-12-14 13:48:03 -08002412};
rginda8ba33642011-12-14 12:31:31 -08002413
Evan Jones2600d4f2016-12-06 09:29:36 -05002414/**
2415 * Move the cursor relative to its current position.
2416 *
2417 * @param {number} row
2418 * @param {number} column
2419 */
rginda87b86462011-12-14 13:48:03 -08002420hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2421 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002422 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2423 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002424 this.screen_.setCursorPosition(row, column);
2425};
2426
Evan Jones2600d4f2016-12-06 09:29:36 -05002427/**
2428 * Move the cursor to the specified position.
2429 *
2430 * @param {number} row
2431 * @param {number} column
2432 */
rginda87b86462011-12-14 13:48:03 -08002433hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002434 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2435 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002436 this.screen_.setCursorPosition(row, column);
2437};
2438
2439/**
2440 * Set the cursor column.
2441 *
Joel Hockey0f933582019-08-27 18:01:51 -07002442 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002443 */
2444hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002445 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002446};
2447
2448/**
2449 * Return the cursor column.
2450 *
Joel Hockey0f933582019-08-27 18:01:51 -07002451 * @return {number} The zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002452 */
2453hterm.Terminal.prototype.getCursorColumn = function() {
2454 return this.screen_.cursorPosition.column;
2455};
2456
2457/**
2458 * Set the cursor row.
2459 *
2460 * The cursor row is relative to the scroll region if the terminal has
2461 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2462 *
Joel Hockey0f933582019-08-27 18:01:51 -07002463 * @param {number} row The new cursor row.
rginda8ba33642011-12-14 12:31:31 -08002464 */
rginda87b86462011-12-14 13:48:03 -08002465hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2466 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002467};
2468
2469/**
2470 * Return the cursor row.
2471 *
Joel Hockey0f933582019-08-27 18:01:51 -07002472 * @return {number} The zero-based cursor row.
rginda8ba33642011-12-14 12:31:31 -08002473 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002474hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002475 return this.screen_.cursorPosition.row;
2476};
2477
2478/**
2479 * Request that the ScrollPort redraw itself soon.
2480 *
2481 * The redraw will happen asynchronously, soon after the call stack winds down.
2482 * Multiple calls will be coalesced into a single redraw.
2483 */
2484hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002485 if (this.timeouts_.redraw)
2486 return;
rginda8ba33642011-12-14 12:31:31 -08002487
2488 var self = this;
rginda87b86462011-12-14 13:48:03 -08002489 this.timeouts_.redraw = setTimeout(function() {
2490 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002491 self.scrollPort_.redraw_();
2492 }, 0);
2493};
2494
2495/**
2496 * Request that the ScrollPort be scrolled to the bottom.
2497 *
2498 * The scroll will happen asynchronously, soon after the call stack winds down.
2499 * Multiple calls will be coalesced into a single scroll.
2500 *
2501 * This affects the scrollbar position of the ScrollPort, and has nothing to
2502 * do with the VT scroll commands.
2503 */
2504hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2505 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002506 return;
rginda8ba33642011-12-14 12:31:31 -08002507
2508 var self = this;
2509 this.timeouts_.scrollDown = setTimeout(function() {
2510 delete self.timeouts_.scrollDown;
2511 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2512 }, 10);
2513};
2514
2515/**
2516 * Move the cursor up a specified number of rows.
2517 *
Joel Hockey0f933582019-08-27 18:01:51 -07002518 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002519 */
2520hterm.Terminal.prototype.cursorUp = function(count) {
Joel Hockey0f933582019-08-27 18:01:51 -07002521 this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002522};
2523
2524/**
2525 * Move the cursor down a specified number of rows.
2526 *
Joel Hockey0f933582019-08-27 18:01:51 -07002527 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002528 */
2529hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002530 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002531 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2532 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2533 this.screenSize.height - 1);
2534
rgindacbbd7482012-06-13 15:06:16 -07002535 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002536 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002537 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002538};
2539
2540/**
2541 * Move the cursor left a specified number of columns.
2542 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002543 * If reverse wraparound mode is enabled and the previous row wrapped into
2544 * the current row then we back up through the wraparound as well.
2545 *
Joel Hockey0f933582019-08-27 18:01:51 -07002546 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002547 */
2548hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002549 count = count || 1;
2550
2551 if (count < 1)
2552 return;
2553
2554 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002555 if (this.options_.reverseWraparound) {
2556 if (this.screen_.cursorPosition.overflow) {
2557 // If this cursor is in the right margin, consume one count to get it
2558 // back to the last column. This only applies when we're in reverse
2559 // wraparound mode.
2560 count--;
2561 this.clearCursorOverflow();
2562
2563 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002564 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002565 }
2566
Robert Gindabfb32622014-07-17 13:20:27 -07002567 var newRow = this.screen_.cursorPosition.row;
2568 var newColumn = currentColumn - count;
2569 if (newColumn < 0) {
2570 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2571 if (newRow < 0) {
2572 // xterm also wraps from row 0 to the last row.
2573 newRow = this.screenSize.height + newRow % this.screenSize.height;
2574 }
2575 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2576 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002577
Robert Gindabfb32622014-07-17 13:20:27 -07002578 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2579
2580 } else {
2581 var newColumn = Math.max(currentColumn - count, 0);
2582 this.setCursorColumn(newColumn);
2583 }
rginda8ba33642011-12-14 12:31:31 -08002584};
2585
2586/**
2587 * Move the cursor right a specified number of columns.
2588 *
Joel Hockey0f933582019-08-27 18:01:51 -07002589 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002590 */
2591hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002592 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002593
2594 if (count < 1)
2595 return;
2596
rgindacbbd7482012-06-13 15:06:16 -07002597 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002598 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002599 this.setCursorColumn(column);
2600};
2601
2602/**
2603 * Reverse the foreground and background colors of the terminal.
2604 *
2605 * This only affects text that was drawn with no attributes.
2606 *
2607 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2608 * been drawn with attributes that happen to coincide with the default
2609 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002610 *
2611 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002612 */
2613hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002614 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002615 if (state) {
Mike Frysinger31cb1562017-07-31 23:44:18 -04002616 this.scrollPort_.setForegroundColor(this.backgroundColor_);
2617 this.scrollPort_.setBackgroundColor(this.foregroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002618 } else {
Mike Frysinger31cb1562017-07-31 23:44:18 -04002619 this.scrollPort_.setForegroundColor(this.foregroundColor_);
2620 this.scrollPort_.setBackgroundColor(this.backgroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002621 }
2622};
2623
2624/**
rginda87b86462011-12-14 13:48:03 -08002625 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002626 *
2627 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002628 */
2629hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002630 this.cursorNode_.style.backgroundColor =
2631 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002632
2633 var self = this;
2634 setTimeout(function() {
Matheus Fernandes2d733082017-09-11 06:43:01 -04002635 self.restyleCursor_();
rginda6d397402012-01-17 10:58:29 -08002636 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002637
Michael Kelly485ecd12014-06-09 11:41:56 -04002638 // bellSquelchTimeout_ affects both audio and notification bells.
2639 if (this.bellSquelchTimeout_)
2640 return;
2641
Robert Ginda92e18102013-03-14 13:56:37 -07002642 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002643 this.bellAudio_.play();
Joel Hockeyd4fca732019-09-20 16:57:03 -07002644 this.bellSequelchTimeout_ = setTimeout(() => {
2645 this.bellSquelchTimeout_ = null;
2646 }, 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002647 } else {
Joel Hockeyd4fca732019-09-20 16:57:03 -07002648 this.bellSquelchTimeout_ = null;
Robert Ginda92e18102013-03-14 13:56:37 -07002649 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002650
2651 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingera5fb83c2017-06-22 14:48:35 -07002652 var n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002653 this.bellNotificationList_.push(n);
2654 // TODO: Should we try to raise the window here?
2655 n.onclick = function() { self.closeBellNotifications_(); };
2656 }
rginda87b86462011-12-14 13:48:03 -08002657};
2658
2659/**
rginda8ba33642011-12-14 12:31:31 -08002660 * Set the origin mode bit.
2661 *
2662 * If origin mode is on, certain VT cursor and scrolling commands measure their
2663 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2664 * to the top of the addressable screen.
2665 *
2666 * Defaults to off.
2667 *
2668 * @param {boolean} state True to set origin mode, false to unset.
2669 */
2670hterm.Terminal.prototype.setOriginMode = function(state) {
2671 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002672 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002673};
2674
2675/**
2676 * Set the insert mode bit.
2677 *
2678 * If insert mode is on, existing text beyond the cursor position will be
2679 * shifted right to make room for new text. Otherwise, new text overwrites
2680 * any existing text.
2681 *
2682 * Defaults to off.
2683 *
2684 * @param {boolean} state True to set insert mode, false to unset.
2685 */
2686hterm.Terminal.prototype.setInsertMode = function(state) {
2687 this.options_.insertMode = state;
2688};
2689
2690/**
rginda87b86462011-12-14 13:48:03 -08002691 * Set the auto carriage return bit.
2692 *
2693 * If auto carriage return is on then a formfeed character is interpreted
2694 * as a newline, otherwise it's the same as a linefeed. The difference boils
2695 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002696 *
2697 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002698 */
2699hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2700 this.options_.autoCarriageReturn = state;
2701};
2702
2703/**
rginda8ba33642011-12-14 12:31:31 -08002704 * Set the wraparound mode bit.
2705 *
2706 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2707 * to the start of the following row. Otherwise, the cursor is clamped to the
2708 * end of the screen and attempts to write past it are ignored.
2709 *
2710 * Defaults to on.
2711 *
2712 * @param {boolean} state True to set wraparound mode, false to unset.
2713 */
2714hterm.Terminal.prototype.setWraparound = function(state) {
2715 this.options_.wraparound = state;
2716};
2717
2718/**
2719 * Set the reverse-wraparound mode bit.
2720 *
2721 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2722 * to the end of the previous row. Otherwise, the cursor is clamped to column
2723 * 0.
2724 *
2725 * Defaults to off.
2726 *
2727 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2728 */
2729hterm.Terminal.prototype.setReverseWraparound = function(state) {
2730 this.options_.reverseWraparound = state;
2731};
2732
2733/**
2734 * Selects between the primary and alternate screens.
2735 *
2736 * If alternate mode is on, the alternate screen is active. Otherwise the
2737 * primary screen is active.
2738 *
2739 * Swapping screens has no effect on the scrollback buffer.
2740 *
2741 * Each screen maintains its own cursor position.
2742 *
2743 * Defaults to off.
2744 *
2745 * @param {boolean} state True to set alternate mode, false to unset.
2746 */
2747hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002748 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002749 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2750
rginda35c456b2012-02-09 17:29:05 -08002751 if (this.screen_.rowsArray.length &&
2752 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2753 // If the screen changed sizes while we were away, our rowIndexes may
2754 // be incorrect.
2755 var offset = this.scrollbackRows_.length;
2756 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002757 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002758 ary[i].rowIndex = offset + i;
2759 }
2760 }
rginda8ba33642011-12-14 12:31:31 -08002761
rginda35c456b2012-02-09 17:29:05 -08002762 this.realizeWidth_(this.screenSize.width);
2763 this.realizeHeight_(this.screenSize.height);
2764 this.scrollPort_.syncScrollHeight();
2765 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002766
rginda6d397402012-01-17 10:58:29 -08002767 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002768 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002769};
2770
2771/**
2772 * Set the cursor-blink mode bit.
2773 *
2774 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2775 * a visible cursor does not blink.
2776 *
2777 * You should make sure to turn blinking off if you're going to dispose of a
2778 * terminal, otherwise you'll leak a timeout.
2779 *
2780 * Defaults to on.
2781 *
2782 * @param {boolean} state True to set cursor-blink mode, false to unset.
2783 */
2784hterm.Terminal.prototype.setCursorBlink = function(state) {
2785 this.options_.cursorBlink = state;
2786
2787 if (!state && this.timeouts_.cursorBlink) {
2788 clearTimeout(this.timeouts_.cursorBlink);
2789 delete this.timeouts_.cursorBlink;
2790 }
2791
2792 if (this.options_.cursorVisible)
2793 this.setCursorVisible(true);
2794};
2795
2796/**
2797 * Set the cursor-visible mode bit.
2798 *
2799 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2800 *
2801 * Defaults to on.
2802 *
2803 * @param {boolean} state True to set cursor-visible mode, false to unset.
2804 */
2805hterm.Terminal.prototype.setCursorVisible = function(state) {
2806 this.options_.cursorVisible = state;
2807
2808 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002809 if (this.timeouts_.cursorBlink) {
2810 clearTimeout(this.timeouts_.cursorBlink);
2811 delete this.timeouts_.cursorBlink;
2812 }
rginda87b86462011-12-14 13:48:03 -08002813 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002814 return;
2815 }
2816
rginda87b86462011-12-14 13:48:03 -08002817 this.syncCursorPosition_();
2818
2819 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002820
2821 if (this.options_.cursorBlink) {
2822 if (this.timeouts_.cursorBlink)
2823 return;
2824
Robert Gindaea2183e2014-07-17 09:51:51 -07002825 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002826 } else {
2827 if (this.timeouts_.cursorBlink) {
2828 clearTimeout(this.timeouts_.cursorBlink);
2829 delete this.timeouts_.cursorBlink;
2830 }
2831 }
2832};
2833
2834/**
rginda87b86462011-12-14 13:48:03 -08002835 * Synchronizes the visible cursor and document selection with the current
2836 * cursor coordinates.
Raymes Khourye5d48982018-08-02 09:08:32 +10002837 *
2838 * @return {boolean} True if the cursor is onscreen and synced.
rginda8ba33642011-12-14 12:31:31 -08002839 */
2840hterm.Terminal.prototype.syncCursorPosition_ = function() {
2841 var topRowIndex = this.scrollPort_.getTopRowIndex();
2842 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2843 var cursorRowIndex = this.scrollbackRows_.length +
2844 this.screen_.cursorPosition.row;
2845
Raymes Khoury15697f42018-07-17 11:37:18 +10002846 let forceSyncSelection = false;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002847 if (this.accessibilityReader_.accessibilityEnabled) {
2848 // Report the new position of the cursor for accessibility purposes.
2849 const cursorColumnIndex = this.screen_.cursorPosition.column;
2850 const cursorLineText =
2851 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
Raymes Khoury15697f42018-07-17 11:37:18 +10002852 // This will force the selection to be sync'd to the cursor position if the
2853 // user has pressed a key. Generally we would only sync the cursor position
2854 // when selection is collapsed so that if the user has selected something
2855 // we don't clear the selection by moving the selection. However when a
2856 // screen reader is used, it's intuitive for entering a key to move the
2857 // selection to the cursor.
2858 forceSyncSelection = this.accessibilityReader_.hasUserGesture;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002859 this.accessibilityReader_.afterCursorChange(
2860 cursorLineText, cursorRowIndex, cursorColumnIndex);
2861 }
2862
rginda8ba33642011-12-14 12:31:31 -08002863 if (cursorRowIndex > bottomRowIndex) {
2864 // Cursor is scrolled off screen, move it outside of the visible area.
Mike Frysinger44c32202017-08-05 01:13:09 -04002865 this.setCssVar('cursor-offset-row', '-1');
Raymes Khourye5d48982018-08-02 09:08:32 +10002866 return false;
rginda8ba33642011-12-14 12:31:31 -08002867 }
2868
Robert Gindab837c052014-08-11 11:17:51 -07002869 if (this.options_.cursorVisible &&
2870 this.cursorNode_.style.display == 'none') {
2871 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2872 this.cursorNode_.style.display = '';
2873 }
2874
Mike Frysinger44c32202017-08-05 01:13:09 -04002875 // Position the cursor using CSS variable math. If we do the math in JS,
2876 // the float math will end up being more precise than the CSS which will
2877 // cause the cursor tracking to be off.
2878 this.setCssVar(
2879 'cursor-offset-row',
2880 `${cursorRowIndex - topRowIndex} + ` +
2881 `${this.scrollPort_.visibleRowTopMargin}px`);
2882 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08002883
2884 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04002885 '(' + this.screen_.cursorPosition.column +
2886 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08002887 ')');
2888
2889 // Update the caret for a11y purposes.
2890 var selection = this.document_.getSelection();
Raymes Khoury15697f42018-07-17 11:37:18 +10002891 if (selection && (selection.isCollapsed || forceSyncSelection)) {
rginda87b86462011-12-14 13:48:03 -08002892 this.screen_.syncSelectionCaret(selection);
Raymes Khoury15697f42018-07-17 11:37:18 +10002893 }
Raymes Khourye5d48982018-08-02 09:08:32 +10002894 return true;
rginda8ba33642011-12-14 12:31:31 -08002895};
2896
Robert Gindafb1be6a2013-12-11 11:56:22 -08002897/**
2898 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2899 * and character cell dimensions.
2900 */
Robert Ginda830583c2013-08-07 13:20:46 -07002901hterm.Terminal.prototype.restyleCursor_ = function() {
2902 var shape = this.cursorShape_;
2903
2904 if (this.cursorNode_.getAttribute('focus') == 'false') {
2905 // Always show a block cursor when unfocused.
2906 shape = hterm.Terminal.cursorShape.BLOCK;
2907 }
2908
2909 var style = this.cursorNode_.style;
2910
2911 switch (shape) {
2912 case hterm.Terminal.cursorShape.BEAM:
Robert Ginda830583c2013-08-07 13:20:46 -07002913 style.backgroundColor = 'transparent';
Joel Hockeyd4fca732019-09-20 16:57:03 -07002914 style.borderBottomStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07002915 style.borderLeftStyle = 'solid';
2916 break;
2917
2918 case hterm.Terminal.cursorShape.UNDERLINE:
Robert Ginda830583c2013-08-07 13:20:46 -07002919 style.backgroundColor = 'transparent';
2920 style.borderBottomStyle = 'solid';
Joel Hockeyd4fca732019-09-20 16:57:03 -07002921 style.borderLeftStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07002922 break;
2923
2924 default:
Mike Frysinger2fd079a2018-09-02 01:46:12 -04002925 style.backgroundColor = 'var(--hterm-cursor-color)';
Joel Hockeyd4fca732019-09-20 16:57:03 -07002926 style.borderBottomStyle = '';
2927 style.borderLeftStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07002928 break;
2929 }
2930};
2931
rginda8ba33642011-12-14 12:31:31 -08002932/**
2933 * Synchronizes the visible cursor with the current cursor coordinates.
2934 *
2935 * The sync will happen asynchronously, soon after the call stack winds down.
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002936 * Multiple calls will be coalesced into a single sync. This should be called
2937 * prior to the cursor actually changing position.
rginda8ba33642011-12-14 12:31:31 -08002938 */
2939hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2940 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002941 return;
rginda8ba33642011-12-14 12:31:31 -08002942
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002943 if (this.accessibilityReader_.accessibilityEnabled) {
2944 // Report the previous position of the cursor for accessibility purposes.
2945 const cursorRowIndex = this.scrollbackRows_.length +
2946 this.screen_.cursorPosition.row;
2947 const cursorColumnIndex = this.screen_.cursorPosition.column;
2948 const cursorLineText =
2949 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
2950 this.accessibilityReader_.beforeCursorChange(
2951 cursorLineText, cursorRowIndex, cursorColumnIndex);
2952 }
2953
rginda8ba33642011-12-14 12:31:31 -08002954 var self = this;
2955 this.timeouts_.syncCursor = setTimeout(function() {
2956 self.syncCursorPosition_();
2957 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002958 }, 0);
2959};
2960
rgindacc2996c2012-02-24 14:59:31 -08002961/**
rgindaf522ce02012-04-17 17:49:17 -07002962 * Show or hide the zoom warning.
2963 *
2964 * The zoom warning is a message warning the user that their browser zoom must
2965 * be set to 100% in order for hterm to function properly.
2966 *
2967 * @param {boolean} state True to show the message, false to hide it.
2968 */
2969hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2970 if (!this.zoomWarningNode_) {
2971 if (!state)
2972 return;
2973
2974 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002975 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07002976 this.zoomWarningNode_.style.cssText = (
2977 'color: black;' +
2978 'background-color: #ff2222;' +
2979 'font-size: large;' +
2980 'border-radius: 8px;' +
2981 'opacity: 0.75;' +
2982 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2983 'top: 0.5em;' +
2984 'right: 1.2em;' +
2985 'position: absolute;' +
2986 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002987 '-webkit-user-select: none;' +
2988 '-moz-text-size-adjust: none;' +
2989 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05002990
2991 this.zoomWarningNode_.addEventListener('click', function(e) {
2992 this.parentNode.removeChild(this);
2993 });
rgindaf522ce02012-04-17 17:49:17 -07002994 }
2995
Mike Frysingerb7289952019-03-23 16:05:38 -07002996 this.zoomWarningNode_.textContent = lib.i18n.replaceReferences(
Robert Gindab4839c22013-02-28 16:52:10 -08002997 hterm.zoomWarningMessage,
Joel Hockeyd4fca732019-09-20 16:57:03 -07002998 [Math.floor(this.scrollPort_.characterSize.zoomFactor * 100)]);
Robert Gindab4839c22013-02-28 16:52:10 -08002999
rgindaf522ce02012-04-17 17:49:17 -07003000 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
3001
3002 if (state) {
3003 if (!this.zoomWarningNode_.parentNode)
3004 this.div_.parentNode.appendChild(this.zoomWarningNode_);
3005 } else if (this.zoomWarningNode_.parentNode) {
3006 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
3007 }
3008};
3009
3010/**
rgindacc2996c2012-02-24 14:59:31 -08003011 * Show the terminal overlay for a given amount of time.
3012 *
3013 * The terminal overlay appears in inverse video in a large font, centered
3014 * over the terminal. You should probably keep the overlay message brief,
3015 * since it's in a large font and you probably aren't going to check the size
3016 * of the terminal first.
3017 *
3018 * @param {string} msg The text (not HTML) message to display in the overlay.
Joel Hockey0f933582019-08-27 18:01:51 -07003019 * @param {number=} opt_timeout The amount of time to wait before fading out
rgindacc2996c2012-02-24 14:59:31 -08003020 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
3021 * stay up forever (or until the next overlay).
3022 */
3023hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08003024 if (!this.overlayNode_) {
3025 if (!this.div_)
3026 return;
3027
3028 this.overlayNode_ = this.document_.createElement('div');
3029 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08003030 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08003031 'font-size: xx-large;' +
3032 'opacity: 0.75;' +
3033 'padding: 0.2em 0.5em 0.2em 0.5em;' +
3034 'position: absolute;' +
3035 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003036 '-webkit-transition: opacity 180ms ease-in;' +
3037 '-moz-user-select: none;' +
3038 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08003039
3040 this.overlayNode_.addEventListener('mousedown', function(e) {
3041 e.preventDefault();
3042 e.stopPropagation();
3043 }, true);
rgindaf0090c92012-02-10 14:58:52 -08003044 }
3045
rginda9f5222b2012-03-05 11:53:28 -08003046 this.overlayNode_.style.color = this.prefs_.get('background-color');
3047 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
3048 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
3049
rgindaf0090c92012-02-10 14:58:52 -08003050 this.overlayNode_.textContent = msg;
3051 this.overlayNode_.style.opacity = '0.75';
3052
3053 if (!this.overlayNode_.parentNode)
3054 this.div_.appendChild(this.overlayNode_);
3055
Joel Hockeyd4fca732019-09-20 16:57:03 -07003056 var divSize = hterm.getClientSize(lib.notNull(this.div_));
Robert Ginda97769282013-02-01 15:30:30 -08003057 var overlaySize = hterm.getClientSize(this.overlayNode_);
3058
Robert Ginda8a59f762014-07-23 11:29:55 -07003059 this.overlayNode_.style.top =
3060 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08003061 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07003062 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08003063
rgindaf0090c92012-02-10 14:58:52 -08003064 if (this.overlayTimeout_)
3065 clearTimeout(this.overlayTimeout_);
3066
Raymes Khouryc7a06382018-07-04 10:25:45 +10003067 this.accessibilityReader_.assertiveAnnounce(msg);
3068
rgindacc2996c2012-02-24 14:59:31 -08003069 if (opt_timeout === null)
3070 return;
3071
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003072 this.overlayTimeout_ = setTimeout(() => {
3073 this.overlayNode_.style.opacity = '0';
3074 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
3075 }, opt_timeout || 1500);
3076};
3077
3078/**
3079 * Hide the terminal overlay immediately.
3080 *
3081 * Useful when we show an overlay for an event with an unknown end time.
3082 */
3083hterm.Terminal.prototype.hideOverlay = function() {
3084 if (this.overlayTimeout_)
3085 clearTimeout(this.overlayTimeout_);
3086 this.overlayTimeout_ = null;
3087
3088 if (this.overlayNode_.parentNode)
3089 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
3090 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08003091};
3092
rginda4bba5e12012-06-20 16:15:30 -07003093/**
3094 * Paste from the system clipboard to the terminal.
Joel Hockey0f933582019-08-27 18:01:51 -07003095 * @return {boolean}
rginda4bba5e12012-06-20 16:15:30 -07003096 */
3097hterm.Terminal.prototype.paste = function() {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003098 return hterm.pasteFromClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07003099};
3100
3101/**
3102 * Copy a string to the system clipboard.
3103 *
3104 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05003105 *
3106 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07003107 */
3108hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08003109 if (this.prefs_.get('enable-clipboard-notice'))
3110 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
3111
Mike Frysinger96eacae2019-01-02 18:13:56 -05003112 hterm.copySelectionToClipboard(this.document_, str);
rginda4bba5e12012-06-20 16:15:30 -07003113};
3114
Evan Jones2600d4f2016-12-06 09:29:36 -05003115/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003116 * Display an image.
3117 *
Mike Frysinger2558ed52019-01-14 01:03:41 -05003118 * Either URI or buffer or blob fields must be specified.
3119 *
Joel Hockey0f933582019-08-27 18:01:51 -07003120 * @param {{
3121 * name: (string|undefined),
3122 * size: (string|number|undefined),
3123 * preserveAspectRation: (boolean|undefined),
3124 * inline: (boolean|undefined),
3125 * width: (string|number|undefined),
3126 * height: (string|number|undefined),
3127 * align: (string|undefined),
3128 * url: (string|undefined),
3129 * buffer: (!ArrayBuffer|undefined),
3130 * blob: (!Blob|undefined),
3131 * type: (string|undefined),
3132 * }} options The image to display.
3133 * name A human readable string for the image
3134 * size The size (in bytes).
3135 * preserveAspectRatio Whether to preserve aspect.
3136 * inline Whether to display the image inline.
3137 * width The width of the image.
3138 * height The height of the image.
3139 * align Direction to align the image.
3140 * uri The source URI for the image.
3141 * buffer The ArrayBuffer image data.
3142 * blob The Blob image data.
3143 * type The MIME type of the image data.
3144 * @param {function()=} onLoad Callback when loading finishes.
3145 * @param {function(!Event)=} onError Callback when loading fails.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003146 */
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003147hterm.Terminal.prototype.displayImage = function(options, onLoad, onError) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003148 // Make sure we're actually given a resource to display.
Mike Frysinger2558ed52019-01-14 01:03:41 -05003149 if (options.uri === undefined && options.buffer === undefined &&
3150 options.blob === undefined)
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003151 return;
3152
3153 // Set up the defaults to simplify code below.
3154 if (!options.name)
3155 options.name = '';
3156
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003157 // See if the mime type is available. If not, guess from the filename.
3158 // We don't list all possible mime types because the browser can usually
3159 // guess it correctly. So list the ones that need a bit more help.
3160 if (!options.type) {
3161 const ary = options.name.split('.');
3162 const ext = ary[ary.length - 1].trim();
3163 switch (ext) {
3164 case 'svg':
3165 case 'svgz':
3166 options.type = 'image/svg+xml';
3167 break;
3168 }
3169 }
3170
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003171 // Has the user approved image display yet?
3172 if (this.allowImagesInline !== true) {
3173 this.newLine();
3174 const row = this.getRowNode(this.scrollbackRows_.length +
3175 this.getCursorRow() - 1);
3176
3177 if (this.allowImagesInline === false) {
3178 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
3179 'Inline Images Disabled');
3180 return;
3181 }
3182
3183 // Show a prompt.
3184 let button;
3185 const span = this.document_.createElement('span');
3186 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
3187 span.style.fontWeight = 'bold';
3188 span.style.borderWidth = '1px';
3189 span.style.borderStyle = 'dashed';
3190 button = this.document_.createElement('span');
3191 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3192 button.style.marginLeft = '1em';
3193 button.style.borderWidth = '1px';
3194 button.style.borderStyle = 'solid';
3195 button.addEventListener('click', () => {
3196 this.prefs_.set('allow-images-inline', false);
3197 });
3198 span.appendChild(button);
3199 button = this.document_.createElement('span');
3200 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3201 'allow this session');
3202 button.style.marginLeft = '1em';
3203 button.style.borderWidth = '1px';
3204 button.style.borderStyle = 'solid';
3205 button.addEventListener('click', () => {
3206 this.allowImagesInline = true;
3207 });
3208 span.appendChild(button);
3209 button = this.document_.createElement('span');
3210 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3211 button.style.marginLeft = '1em';
3212 button.style.borderWidth = '1px';
3213 button.style.borderStyle = 'solid';
3214 button.addEventListener('click', () => {
3215 this.prefs_.set('allow-images-inline', true);
3216 });
3217 span.appendChild(button);
3218
3219 row.appendChild(span);
3220 return;
3221 }
3222
3223 // See if we should show this object directly, or download it.
3224 if (options.inline) {
3225 const io = this.io.push();
3226 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
Joel Hockeyd4fca732019-09-20 16:57:03 -07003227 'Loading $1 ...'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003228
3229 // While we're loading the image, eat all the user's input.
3230 io.onVTKeystroke = io.sendString = () => {};
3231
3232 // Initialize this new image.
Joel Hockeyd4fca732019-09-20 16:57:03 -07003233 const img = this.document_.createElement('img');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003234 if (options.uri !== undefined) {
3235 img.src = options.uri;
3236 } else if (options.buffer !== undefined) {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003237 const blob = new Blob([options.buffer], {type: options.type});
Mike Frysinger2558ed52019-01-14 01:03:41 -05003238 img.src = URL.createObjectURL(blob);
3239 } else {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003240 const blob = new Blob([options.blob], {type: options.type});
Joel Hockeyd4fca732019-09-20 16:57:03 -07003241 img.src = URL.createObjectURL(blob);
Mike Frysinger2558ed52019-01-14 01:03:41 -05003242 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003243 img.title = img.alt = options.name;
3244
3245 // Attach the image to the page to let it load/render. It won't stay here.
3246 // This is needed so it's visible and the DOM can calculate the height. If
3247 // the image is hidden or not in the DOM, the height is always 0.
3248 this.document_.body.appendChild(img);
3249
3250 // Wait for the image to finish loading before we try moving it to the
3251 // right place in the terminal.
3252 img.onload = () => {
3253 // Now that we have the image dimensions, figure out how to show it.
3254 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
3255 img.style.maxWidth = `${this.document_.body.clientWidth}px`;
3256 img.style.maxHeight = `${this.document_.body.clientHeight}px`;
3257
3258 // Parse a width/height specification.
3259 const parseDim = (dim, maxDim, cssVar) => {
3260 if (!dim || dim == 'auto')
3261 return '';
3262
3263 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3264 if (ary) {
3265 if (ary[2] == '%')
Joel Hockeyd4fca732019-09-20 16:57:03 -07003266 return Math.floor(maxDim * ary[1] / 100) + 'px';
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003267 else if (ary[2] == 'px')
3268 return dim;
3269 else
3270 return `calc(${dim} * var(${cssVar}))`;
3271 }
3272
3273 return '';
3274 };
3275 img.style.width =
3276 parseDim(options.width, this.document_.body.clientWidth,
3277 '--hterm-charsize-width');
3278 img.style.height =
3279 parseDim(options.height, this.document_.body.clientHeight,
3280 '--hterm-charsize-height');
3281
3282 // Figure out how many rows the image occupies, then add that many.
Mike Frysingera0349392019-09-11 05:38:09 -04003283 // Note: This count will be inaccurate if the font size changes on us.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003284 const padRows = Math.ceil(img.clientHeight /
3285 this.scrollPort_.characterSize.height);
3286 for (let i = 0; i < padRows; ++i)
3287 this.newLine();
3288
3289 // Update the max height in case the user shrinks the character size.
3290 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3291
3292 // Move the image to the last row. This way when we scroll up, it doesn't
3293 // disappear when the first row gets clipped. It will disappear when we
3294 // scroll down and the last row is clipped ...
3295 this.document_.body.removeChild(img);
3296 // Create a wrapper node so we can do an absolute in a relative position.
3297 // This helps with rounding errors between JS & CSS counts.
3298 const div = this.document_.createElement('div');
3299 div.style.position = 'relative';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003300 div.style.textAlign = options.align || '';
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003301 img.style.position = 'absolute';
3302 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3303 div.appendChild(img);
3304 const row = this.getRowNode(this.scrollbackRows_.length +
3305 this.getCursorRow() - 1);
3306 row.appendChild(div);
3307
Mike Frysinger2558ed52019-01-14 01:03:41 -05003308 // Now that the image has been read, we can revoke the source.
3309 if (options.uri === undefined) {
3310 URL.revokeObjectURL(img.src);
3311 }
3312
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003313 io.hideOverlay();
3314 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003315
3316 if (onLoad)
3317 onLoad();
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003318 };
3319
3320 // If we got a malformed image, give up.
3321 img.onerror = (e) => {
3322 this.document_.body.removeChild(img);
3323 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
Mike Frysingere14a8c42018-03-10 00:17:30 -08003324 'Loading $1 failed'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003325 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003326
3327 if (onError)
3328 onError(e);
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003329 };
3330 } else {
3331 // We can't use chrome.downloads.download as that requires "downloads"
3332 // permissions, and that works only in extensions, not apps.
3333 const a = this.document_.createElement('a');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003334 if (options.uri !== undefined) {
3335 a.href = options.uri;
3336 } else if (options.buffer !== undefined) {
3337 const blob = new Blob([options.buffer]);
3338 a.href = URL.createObjectURL(blob);
3339 } else {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003340 a.href = URL.createObjectURL(lib.notNull(options.blob));
Mike Frysinger2558ed52019-01-14 01:03:41 -05003341 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003342 a.download = options.name;
3343 this.document_.body.appendChild(a);
3344 a.click();
3345 a.remove();
Mike Frysinger2558ed52019-01-14 01:03:41 -05003346 if (options.uri === undefined) {
3347 URL.revokeObjectURL(a.href);
3348 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003349 }
3350};
3351
3352/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003353 * Returns the selected text, or null if no text is selected.
3354 *
3355 * @return {string|null}
3356 */
rgindaa09e7332012-08-17 12:49:51 -07003357hterm.Terminal.prototype.getSelectionText = function() {
3358 var selection = this.scrollPort_.selection;
3359 selection.sync();
3360
3361 if (selection.isCollapsed)
3362 return null;
3363
rgindaa09e7332012-08-17 12:49:51 -07003364 // Start offset measures from the beginning of the line.
3365 var startOffset = selection.startOffset;
3366 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003367
Raymes Khoury334625a2018-06-25 10:29:40 +10003368 // If an x-row isn't selected, |node| will be null.
3369 if (!node)
3370 return null;
3371
Robert Gindafdbb3f22012-09-06 20:23:06 -07003372 if (node.nodeName != 'X-ROW') {
3373 // If the selection doesn't start on an x-row node, then it must be
3374 // somewhere inside the x-row. Add any characters from previous siblings
3375 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003376
3377 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3378 // If node is the text node in a styled span, move up to the span node.
3379 node = node.parentNode;
3380 }
3381
Robert Gindafdbb3f22012-09-06 20:23:06 -07003382 while (node.previousSibling) {
3383 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003384 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003385 }
rgindaa09e7332012-08-17 12:49:51 -07003386 }
3387
3388 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08003389 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
3390 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003391 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003392
Robert Gindafdbb3f22012-09-06 20:23:06 -07003393 if (node.nodeName != 'X-ROW') {
3394 // If the selection doesn't end on an x-row node, then it must be
3395 // somewhere inside the x-row. Add any characters from following siblings
3396 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003397
3398 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3399 // If node is the text node in a styled span, move up to the span node.
3400 node = node.parentNode;
3401 }
3402
Robert Gindafdbb3f22012-09-06 20:23:06 -07003403 while (node.nextSibling) {
3404 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003405 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003406 }
rgindaa09e7332012-08-17 12:49:51 -07003407 }
3408
3409 var rv = this.getRowsText(selection.startRow.rowIndex,
3410 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003411 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003412};
3413
rginda4bba5e12012-06-20 16:15:30 -07003414/**
3415 * Copy the current selection to the system clipboard, then clear it after a
3416 * short delay.
3417 */
3418hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07003419 var text = this.getSelectionText();
3420 if (text != null)
3421 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07003422};
3423
Joel Hockey0f933582019-08-27 18:01:51 -07003424/**
3425 * Show overlay with current terminal size.
3426 */
rgindaf0090c92012-02-10 14:58:52 -08003427hterm.Terminal.prototype.overlaySize = function() {
Theodore Duboisdd5f9a72019-09-06 23:28:42 -07003428 if (this.prefs_.get('enable-resize-status')) {
3429 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
3430 }
rgindaf0090c92012-02-10 14:58:52 -08003431};
3432
rginda87b86462011-12-14 13:48:03 -08003433/**
3434 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3435 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003436 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003437 */
3438hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08003439 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08003440 this.scrollPort_.scrollRowToBottom(this.getRowCount());
3441
Mike Frysinger79669762018-12-30 20:51:10 -05003442 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08003443};
3444
3445/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003446 * Open the selected url.
3447 */
3448hterm.Terminal.prototype.openSelectedUrl_ = function() {
3449 var str = this.getSelectionText();
3450
3451 // If there is no selection, try and expand wherever they clicked.
3452 if (str == null) {
John Lincae9b732018-03-08 13:56:35 +08003453 this.screen_.expandSelectionForUrl(this.document_.getSelection());
Mike Frysinger70b94692017-01-26 18:57:50 -10003454 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003455
3456 // If clicking in empty space, return.
3457 if (str == null)
3458 return;
Mike Frysinger70b94692017-01-26 18:57:50 -10003459 }
3460
3461 // Make sure URL is valid before opening.
3462 if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0)
3463 return;
Mike Frysinger43472622017-06-26 18:11:07 -04003464
3465 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003466 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003467 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3468 // We have to whitelist a few protocols that lack authorities and thus
3469 // never use the //. Like mailto.
3470 switch (str.split(':', 1)[0]) {
3471 case 'mailto':
3472 break;
3473 default:
3474 str = 'http://' + str;
3475 break;
3476 }
3477 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003478
Mike Frysinger720fa832017-10-23 01:15:52 -04003479 hterm.openUrl(str);
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003480};
Mike Frysinger70b94692017-01-26 18:57:50 -10003481
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003482/**
3483 * Manage the automatic mouse hiding behavior while typing.
3484 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003485 * @param {?boolean=} v Whether to enable automatic hiding.
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003486 */
3487hterm.Terminal.prototype.setAutomaticMouseHiding = function(v=null) {
3488 // Since Chrome OS & macOS do this by default everywhere, we don't need to.
3489 // Linux & Windows seem to leave this to specific applications to manage.
3490 if (v === null)
3491 v = (hterm.os != 'cros' && hterm.os != 'mac');
3492
3493 this.mouseHideWhileTyping_ = !!v;
3494};
3495
3496/**
3497 * Handler for monitoring user keyboard activity.
3498 *
3499 * This isn't for processing the keystrokes directly, but for updating any
3500 * state that might toggle based on the user using the keyboard at all.
3501 *
Joel Hockey0f933582019-08-27 18:01:51 -07003502 * @param {!KeyboardEvent} e The keyboard event that triggered us.
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003503 */
3504hterm.Terminal.prototype.onKeyboardActivity_ = function(e) {
3505 // When the user starts typing, hide the mouse cursor.
3506 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_)
3507 this.setCssVar('mouse-cursor-style', 'none');
3508};
Mike Frysinger70b94692017-01-26 18:57:50 -10003509
3510/**
rgindad5613292012-06-19 15:40:37 -07003511 * Add the terminalRow and terminalColumn properties to mouse events and
3512 * then forward on to onMouse().
3513 *
3514 * The terminalRow and terminalColumn properties contain the (row, column)
3515 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003516 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003517 * @param {!MouseEvent} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003518 */
3519hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003520 if (e.processedByTerminalHandler_) {
3521 // We register our event handlers on the document, as well as the cursor
3522 // and the scroll blocker. Mouse events that occur on the cursor or
3523 // scroll blocker will also appear on the document, but we don't want to
3524 // process them twice.
3525 //
3526 // We can't just prevent bubbling because that has other side effects, so
3527 // we decorate the event object with this property instead.
3528 return;
3529 }
3530
Mike Frysinger468966c2018-08-28 13:48:51 -04003531 // Consume navigation events. Button 3 is usually "browser back" and
3532 // button 4 is "browser forward" which we don't want to happen.
3533 if (e.button > 2) {
3534 e.preventDefault();
3535 // We don't return so click events can be passed to the remote below.
3536 }
3537
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003538 var reportMouseEvents = (!this.defeatMouseReports_ &&
3539 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3540
rgindafaa74742012-08-21 13:34:03 -07003541 e.processedByTerminalHandler_ = true;
3542
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003543 // Handle auto hiding of mouse cursor while typing.
3544 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
3545 // Make sure the mouse cursor is visible.
3546 this.syncMouseStyle();
3547 // This debounce isn't perfect, but should work well enough for such a
3548 // simple implementation. If the user moved the mouse, we enabled this
3549 // debounce, and then moved the mouse just before the timeout, we wouldn't
3550 // debounce that later movement.
3551 this.mouseHideDelay_ = setTimeout(() => this.mouseHideDelay_ = null, 1000);
3552 }
3553
Robert Gindaeda48db2014-07-17 09:25:30 -07003554 // One based row/column stored on the mouse event.
Joel Hockeyd4fca732019-09-20 16:57:03 -07003555 e.terminalRow = Math.floor(
3556 (e.clientY - this.scrollPort_.visibleRowTopMargin) /
3557 this.scrollPort_.characterSize.height) + 1;
3558 e.terminalColumn = Math.floor(
3559 e.clientX / this.scrollPort_.characterSize.width) + 1;
Robert Gindaeda48db2014-07-17 09:25:30 -07003560
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003561 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
3562 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07003563 return;
3564 }
3565
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003566 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07003567 // If the cursor is visible and we're not sending mouse events to the
3568 // host app, then we want to hide the terminal cursor when the mouse
3569 // cursor is over top. This keeps the terminal cursor from interfering
3570 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003571 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3572 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3573 this.cursorNode_.style.display = 'none';
3574 } else if (this.cursorNode_.style.display == 'none') {
3575 this.cursorNode_.style.display = '';
3576 }
3577 }
rgindad5613292012-06-19 15:40:37 -07003578
Robert Ginda928cf632014-03-05 15:07:41 -08003579 if (e.type == 'mousedown') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003580 this.contextMenu.hide();
Mike Frysingercc114512017-09-11 21:39:17 -04003581
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003582 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003583 // If VT mouse reporting is disabled, or has been defeated with
3584 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003585 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003586 this.setSelectionEnabled(true);
3587 } else {
3588 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003589 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003590 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003591 this.setSelectionEnabled(false);
3592 e.preventDefault();
3593 }
3594 }
3595
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003596 if (!reportMouseEvents) {
John Lin2aad22e2018-03-16 13:58:11 +08003597 if (e.type == 'dblclick') {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003598 this.screen_.expandSelection(this.document_.getSelection());
John Lin2aad22e2018-03-16 13:58:11 +08003599 if (this.copyOnSelect)
Mike Frysinger406c76c2019-01-02 17:53:51 -05003600 this.copySelectionToClipboard();
rgindad5613292012-06-19 15:40:37 -07003601 }
3602
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003603 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003604 // Debounce this event with the dblclick event. If you try to doubleclick
3605 // a URL to open it, Chrome will fire click then dblclick, but we won't
3606 // have expanded the selection text at the first click event.
3607 clearTimeout(this.timeouts_.openUrl);
3608 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3609 500);
3610 return;
3611 }
3612
Mike Frysinger847577f2017-05-23 23:25:57 -04003613 if (e.type == 'mousedown') {
Mike Frysingercc114512017-09-11 21:39:17 -04003614 if (e.ctrlKey && e.button == 2 /* right button */) {
3615 e.preventDefault();
3616 this.contextMenu.show(e, this);
3617 } else if (e.button == this.mousePasteButton ||
3618 (this.mouseRightClickPaste && e.button == 2 /* right button */)) {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003619 if (!this.paste())
Mike Frysinger05a57f02017-08-27 17:48:55 -04003620 console.warn('Could not paste manually due to web restrictions');
Mike Frysinger847577f2017-05-23 23:25:57 -04003621 }
3622 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003623
Mike Frysinger2edd3612017-05-24 00:54:39 -04003624 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003625 !this.document_.getSelection().isCollapsed) {
Mike Frysinger406c76c2019-01-02 17:53:51 -05003626 this.copySelectionToClipboard();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003627 }
3628
3629 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3630 this.scrollBlockerNode_.engaged) {
3631 // Disengage the scroll-blocker after one of these events.
3632 this.scrollBlockerNode_.engaged = false;
3633 this.scrollBlockerNode_.style.top = '-99px';
3634 }
3635
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003636 // Emulate arrow key presses via scroll wheel events.
3637 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3638 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003639 if (e.type == 'wheel') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003640 const delta =
3641 this.scrollPort_.scrollWheelDelta(/** @type {!WheelEvent} */ (e));
Mike Frysingerc3030a82017-05-29 14:16:11 -04003642
Mike Frysinger321063c2018-08-29 15:33:14 -04003643 // Helper to turn a wheel event delta into a series of key presses.
3644 const deltaToArrows = (distance, charSize, arrowPos, arrowNeg) => {
3645 if (distance == 0) {
3646 return '';
3647 }
3648
3649 // Convert the scroll distance into a number of rows/cols.
3650 const cells = lib.f.smartFloorDivide(Math.abs(distance), charSize);
3651 const data = '\x1bO' + (distance < 0 ? arrowNeg : arrowPos);
3652 return data.repeat(cells);
3653 };
3654
3655 // The order between up/down and left/right doesn't really matter.
3656 this.io.sendString(
3657 // Up/down arrow keys.
3658 deltaToArrows(delta.y, this.scrollPort_.characterSize.height,
3659 'A', 'B') +
3660 // Left/right arrow keys.
3661 deltaToArrows(delta.x, this.scrollPort_.characterSize.width,
3662 'C', 'D')
3663 );
Mike Frysingerc3030a82017-05-29 14:16:11 -04003664
3665 e.preventDefault();
3666 }
3667 }
Robert Ginda928cf632014-03-05 15:07:41 -08003668 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003669 if (!this.scrollBlockerNode_.engaged) {
3670 if (e.type == 'mousedown') {
3671 // Move the scroll-blocker into place if we want to keep the scrollport
3672 // from scrolling.
3673 this.scrollBlockerNode_.engaged = true;
3674 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3675 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3676 } else if (e.type == 'mousemove') {
3677 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3678 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003679 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003680 e.preventDefault();
3681 }
3682 }
Robert Ginda928cf632014-03-05 15:07:41 -08003683
3684 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003685 }
3686
Robert Ginda928cf632014-03-05 15:07:41 -08003687 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3688 // Restore this on mouseup in case it was temporarily defeated with a
3689 // alt-mousedown. Only do this when the selection is empty so that
3690 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003691 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003692 }
rgindad5613292012-06-19 15:40:37 -07003693};
3694
3695/**
3696 * Clients should override this if they care to know about mouse events.
3697 *
3698 * The event parameter will be a normal DOM mouse click event with additional
3699 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003700 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003701 * @param {!MouseEvent} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003702 */
3703hterm.Terminal.prototype.onMouse = function(e) { };
3704
3705/**
rginda8e92a692012-05-20 19:37:20 -07003706 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003707 *
3708 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003709 */
Rob Spies06533ba2014-04-24 11:20:37 -07003710hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3711 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003712 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003713
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003714 if (this.reportFocus)
3715 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O');
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003716
Michael Kelly485ecd12014-06-09 11:41:56 -04003717 if (focused === true)
3718 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07003719};
3720
3721/**
rginda8ba33642011-12-14 12:31:31 -08003722 * React when the ScrollPort is scrolled.
3723 */
3724hterm.Terminal.prototype.onScroll_ = function() {
3725 this.scheduleSyncCursorPosition_();
3726};
3727
3728/**
rginda9846e2f2012-01-27 13:53:33 -08003729 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003730 *
Joel Hockeye25ce432019-09-25 19:12:28 -07003731 * @param {{text: string}} e The text of the paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08003732 */
3733hterm.Terminal.prototype.onPaste_ = function(e) {
Joel Hockeye25ce432019-09-25 19:12:28 -07003734 var data = e.text.replace(/\n/mg, '\r');
Mike Frysingere8c32c82018-03-11 14:57:28 -07003735 if (this.options_.bracketedPaste) {
3736 // We strip out most escape sequences as they can cause issues (like
3737 // inserting an \x1b[201~ midstream). We pass through whitespace
3738 // though: 0x08:\b 0x09:\t 0x0a:\n 0x0d:\r.
3739 // This matches xterm behavior.
3740 const filter = (data) => data.replace(/[\x00-\x07\x0b-\x0c\x0e-\x1f]/g, '');
3741 data = '\x1b[200~' + filter(data) + '\x1b[201~';
3742 }
Robert Gindaa063b202014-07-21 11:08:25 -07003743
3744 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003745};
3746
3747/**
rgindaa09e7332012-08-17 12:49:51 -07003748 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003749 *
Joel Hockey0f933582019-08-27 18:01:51 -07003750 * @param {!Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07003751 */
3752hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07003753 if (!this.useDefaultWindowCopy) {
3754 e.preventDefault();
3755 setTimeout(this.copySelectionToClipboard.bind(this), 0);
3756 }
rgindaa09e7332012-08-17 12:49:51 -07003757};
3758
3759/**
rginda8ba33642011-12-14 12:31:31 -08003760 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08003761 *
3762 * Note: This function should not directly contain code that alters the internal
3763 * state of the terminal. That kind of code belongs in realizeWidth or
3764 * realizeHeight, so that it can be executed synchronously in the case of a
3765 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08003766 */
3767hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08003768 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07003769 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08003770 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07003771 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08003772
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003773 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08003774 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003775 // gets removed from the document or during the initial load, and we can't
3776 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07003777 // This can also happen if called before the scrollPort calculates the
3778 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08003779 return;
3780 }
3781
rgindaa8ba17d2012-08-15 14:41:10 -07003782 var isNewSize = (columnCount != this.screenSize.width ||
3783 rowCount != this.screenSize.height);
Theodore Dubois651b0842019-09-07 14:32:09 -07003784 const wasScrolledEnd = this.scrollPort_.isScrolledEnd;
rgindaa8ba17d2012-08-15 14:41:10 -07003785
3786 // We do this even if the size didn't change, just to be sure everything is
3787 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04003788 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07003789 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07003790
3791 if (isNewSize)
3792 this.overlaySize();
3793
Robert Gindafb1be6a2013-12-11 11:56:22 -08003794 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07003795 this.scheduleSyncCursorPosition_();
Theodore Dubois651b0842019-09-07 14:32:09 -07003796
3797 if (wasScrolledEnd) {
3798 this.scrollEnd();
3799 }
rginda8ba33642011-12-14 12:31:31 -08003800};
3801
3802/**
3803 * Service the cursor blink timeout.
3804 */
3805hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07003806 if (!this.options_.cursorBlink) {
3807 delete this.timeouts_.cursorBlink;
3808 return;
3809 }
3810
Robert Ginda830583c2013-08-07 13:20:46 -07003811 if (this.cursorNode_.getAttribute('focus') == 'false' ||
3812 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08003813 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07003814 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3815 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08003816 } else {
rginda87b86462011-12-14 13:48:03 -08003817 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07003818 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3819 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08003820 }
3821};
David Reveman8f552492012-03-28 12:18:41 -04003822
3823/**
3824 * Set the scrollbar-visible mode bit.
3825 *
3826 * If scrollbar-visible is on, the vertical scrollbar will be visible.
3827 * Otherwise it will not.
3828 *
3829 * Defaults to on.
3830 *
3831 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
3832 */
3833hterm.Terminal.prototype.setScrollbarVisible = function(state) {
3834 this.scrollPort_.setScrollbarVisible(state);
3835};
Michael Kelly485ecd12014-06-09 11:41:56 -04003836
3837/**
Rob Spies49039e52014-12-17 13:40:04 -08003838 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04003839 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08003840 *
3841 * Defaults to 1.
3842 *
Evan Jones2600d4f2016-12-06 09:29:36 -05003843 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08003844 */
3845hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3846 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3847};
3848
3849/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003850 * Close all web notifications created by terminal bells.
3851 */
3852hterm.Terminal.prototype.closeBellNotifications_ = function() {
3853 this.bellNotificationList_.forEach(function(n) {
3854 n.close();
3855 });
3856 this.bellNotificationList_.length = 0;
3857};
Raymes Khourye5d48982018-08-02 09:08:32 +10003858
3859/**
3860 * Syncs the cursor position when the scrollport gains focus.
3861 */
3862hterm.Terminal.prototype.onScrollportFocus_ = function() {
3863 // If the cursor is offscreen we set selection to the last row on the screen.
3864 const topRowIndex = this.scrollPort_.getTopRowIndex();
3865 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
3866 const selection = this.document_.getSelection();
3867 if (!this.syncCursorPosition_() && selection) {
3868 selection.collapse(this.getRowNode(bottomRowIndex));
3869 }
3870};