blob: 1c97b23f758558ecf88fe0ff9ca62b741db8558b [file] [log] [blame]
rginda87b86462011-12-14 13:48:03 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rginda8ba33642011-12-14 12:31:31 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rgindacbbd7482012-06-13 15:06:16 -07005'use strict';
6
rginda8ba33642011-12-14 12:31:31 -08007/**
8 * Constructor for the Terminal class.
9 *
10 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
11 * classes to provide the complete terminal functionality.
12 *
13 * There are a number of lower-level Terminal methods that can be called
14 * directly to manipulate the cursor, text, scroll region, and other terminal
15 * attributes. However, the primary method is interpret(), which parses VT
16 * escape sequences and invokes the appropriate Terminal methods.
17 *
18 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
19 *
20 * TODO(rginda): Eventually we're going to need to support characters which are
21 * displayed twice as wide as standard latin characters. This is to support
22 * CJK (and possibly other character sets).
rginda9f5222b2012-03-05 11:53:28 -080023 *
Joel Hockey3a44a442019-10-14 16:22:56 -070024 * @param {?string=} profileId Optional preference profile name. If not
25 * provided or null, defaults to 'default'.
Joel Hockey0f933582019-08-27 18:01:51 -070026 * @constructor
Joel Hockeyd4fca732019-09-20 16:57:03 -070027 * @implements {hterm.RowProvider}
rginda8ba33642011-12-14 12:31:31 -080028 */
Joel Hockey3a44a442019-10-14 16:22:56 -070029hterm.Terminal = function(profileId) {
Robert Ginda57f03b42012-09-13 11:02:48 -070030 this.profileId_ = null;
rginda9f5222b2012-03-05 11:53:28 -080031
Joel Hockeyd4fca732019-09-20 16:57:03 -070032 /** @type {?hterm.PreferenceManager} */
33 this.prefs_ = null;
34
rginda8ba33642011-12-14 12:31:31 -080035 // Two screen instances.
36 this.primaryScreen_ = new hterm.Screen();
37 this.alternateScreen_ = new hterm.Screen();
38
39 // The "current" screen.
40 this.screen_ = this.primaryScreen_;
41
rginda8ba33642011-12-14 12:31:31 -080042 // The local notion of the screen size. ScreenBuffers also have a size which
43 // indicates their present size. During size changes, the two may disagree.
44 // Also, the inactive screen's size is not altered until it is made the active
45 // screen.
46 this.screenSize = new hterm.Size(0, 0);
47
rginda8ba33642011-12-14 12:31:31 -080048 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080049 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080050 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
51 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080052 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
Raymes Khourye5d48982018-08-02 09:08:32 +100053 this.scrollPort_.subscribe('focus', this.onScrollportFocus_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070054 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080055
rginda87b86462011-12-14 13:48:03 -080056 // The div that contains this terminal.
57 this.div_ = null;
58
rgindac9bc5502012-01-18 11:48:44 -080059 // The document that contains the scrollPort. Defaulted to the global
60 // document here so that the terminal is functional even if it hasn't been
61 // inserted into a document yet, but re-set in decorate().
62 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080063
rginda8ba33642011-12-14 12:31:31 -080064 // The rows that have scrolled off screen and are no longer addressable.
65 this.scrollbackRows_ = [];
66
rgindac9bc5502012-01-18 11:48:44 -080067 // Saved tab stops.
68 this.tabStops_ = [];
69
David Benjamin66e954d2012-05-05 21:08:12 -040070 // Keep track of whether default tab stops have been erased; after a TBC
71 // clears all tab stops, defaults aren't restored on resize until a reset.
72 this.defaultTabStops = true;
73
rginda8ba33642011-12-14 12:31:31 -080074 // The VT's notion of the top and bottom rows. Used during some VT
75 // cursor positioning and scrolling commands.
76 this.vtScrollTop_ = null;
77 this.vtScrollBottom_ = null;
78
79 // The DIV element for the visible cursor.
80 this.cursorNode_ = null;
81
Robert Ginda830583c2013-08-07 13:20:46 -070082 // The current cursor shape of the terminal.
83 this.cursorShape_ = hterm.Terminal.cursorShape.BLOCK;
84
Robert Gindaea2183e2014-07-17 09:51:51 -070085 // Cursor blink on/off cycle in ms, overwritten by prefs once they're loaded.
86 this.cursorBlinkCycle_ = [100, 100];
87
Mike Frysinger225c99d2019-10-20 14:02:37 -060088 // Whether to temporarily disable blinking.
89 this.cursorBlinkPause_ = false;
90
Robert Gindaea2183e2014-07-17 09:51:51 -070091 // Pre-bound onCursorBlink_ handler, so we don't have to do this for each
92 // cursor on/off servicing.
93 this.myOnCursorBlink_ = this.onCursorBlink_.bind(this);
94
rginda9f5222b2012-03-05 11:53:28 -080095 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -070096 // each output and keystroke. They are initialized by the preference manager.
Joel Hockey8ff48232019-09-24 13:15:17 -070097 /** @type {string} */
98 this.backgroundColor_ = '';
99 /** @type {string} */
100 this.foregroundColor_ = '';
Robert Ginda57f03b42012-09-13 11:02:48 -0700101 this.scrollOnOutput_ = null;
102 this.scrollOnKeystroke_ = null;
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400103 this.scrollWheelArrowKeys_ = null;
rginda9f5222b2012-03-05 11:53:28 -0800104
Robert Ginda6aec7eb2015-06-16 10:31:30 -0700105 // True if we should override mouse event reporting to allow local selection.
106 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -0800107
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400108 // Whether to auto hide the mouse cursor when typing.
109 this.setAutomaticMouseHiding();
110 // Timer to keep mouse visible while it's being used.
111 this.mouseHideDelay_ = null;
112
rgindaf0090c92012-02-10 14:58:52 -0800113 // Terminal bell sound.
114 this.bellAudio_ = this.document_.createElement('audio');
Mike Frysingerd826f1a2017-07-06 16:20:06 -0400115 this.bellAudio_.id = 'hterm:bell-audio';
rgindaf0090c92012-02-10 14:58:52 -0800116 this.bellAudio_.setAttribute('preload', 'auto');
117
Raymes Khoury3e44bc92018-05-17 10:54:23 +1000118 // The AccessibilityReader object for announcing command output.
119 this.accessibilityReader_ = null;
120
Mike Frysingercc114512017-09-11 21:39:17 -0400121 // The context menu object.
122 this.contextMenu = new hterm.ContextMenu();
123
Michael Kelly485ecd12014-06-09 11:41:56 -0400124 // All terminal bell notifications that have been generated (not necessarily
125 // shown).
126 this.bellNotificationList_ = [];
Joel Hockeyd4fca732019-09-20 16:57:03 -0700127 this.bellSquelchTimeout_ = null;
Michael Kelly485ecd12014-06-09 11:41:56 -0400128
129 // Whether we have permission to display notifications.
130 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400131
rginda6d397402012-01-17 10:58:29 -0800132 // Cursor position and attributes saved with DECSC.
133 this.savedOptions_ = {};
134
rginda8ba33642011-12-14 12:31:31 -0800135 // The current mode bits for the terminal.
136 this.options_ = new hterm.Options();
137
138 // Timeouts we might need to clear.
139 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800140
141 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800142 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800143
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800144 this.saveCursorAndState(true);
145
Zhu Qunying30d40712017-03-14 16:27:00 -0700146 // The keyboard handler.
rgindafeaf3142012-01-31 15:14:20 -0800147 this.keyboard = new hterm.Keyboard(this);
148
rginda87b86462011-12-14 13:48:03 -0800149 // General IO interface that can be given to third parties without exposing
150 // the entire terminal object.
151 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800152
rgindad5613292012-06-19 15:40:37 -0700153 // True if mouse-click-drag should scroll the terminal.
154 this.enableMouseDragScroll = true;
155
Robert Ginda57f03b42012-09-13 11:02:48 -0700156 this.copyOnSelect = null;
Mike Frysinger847577f2017-05-23 23:25:57 -0400157 this.mouseRightClickPaste = null;
rginda4bba5e12012-06-20 16:15:30 -0700158 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700159
Zhu Qunying30d40712017-03-14 16:27:00 -0700160 // Whether to use the default window copy behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700161 this.useDefaultWindowCopy = false;
162
163 this.clearSelectionAfterCopy = true;
164
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400165 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800166 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700167
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400168 // Whether we allow images to be shown.
169 this.allowImagesInline = null;
170
Gabriel Holodake8a09be2017-10-10 01:07:11 -0400171 this.reportFocus = false;
172
Jason Linf129f3c2020-03-23 11:52:08 +1100173 // TODO(crbug.com/1063219) Remove this once the bug is fixed.
174 this.alwaysUseLegacyPasting = false;
175
Joel Hockey3a44a442019-10-14 16:22:56 -0700176 this.setProfile(profileId || 'default',
Evan Jones5f9df812016-12-06 09:38:58 -0500177 function() { this.onTerminalReady(); }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800178};
179
180/**
Robert Ginda830583c2013-08-07 13:20:46 -0700181 * Possible cursor shapes.
182 */
183hterm.Terminal.cursorShape = {
184 BLOCK: 'BLOCK',
185 BEAM: 'BEAM',
186 UNDERLINE: 'UNDERLINE'
187};
188
189/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700190 * Clients should override this to be notified when the terminal is ready
191 * for use.
192 *
193 * The terminal initialization is asynchronous, and shouldn't be used before
194 * this method is called.
195 */
196hterm.Terminal.prototype.onTerminalReady = function() { };
197
198/**
rginda35c456b2012-02-09 17:29:05 -0800199 * Default tab with of 8 to match xterm.
200 */
201hterm.Terminal.prototype.tabWidth = 8;
202
203/**
rginda9f5222b2012-03-05 11:53:28 -0800204 * Select a preference profile.
205 *
206 * This will load the terminal preferences for the given profile name and
207 * associate subsequent preference changes with the new preference profile.
208 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500209 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800210 * characters will be removed from the name.
Joel Hockey0f933582019-08-27 18:01:51 -0700211 * @param {function()=} opt_callback Optional callback to invoke when the
212 * profile transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800213 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700214hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
215 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800216
Robert Ginda57f03b42012-09-13 11:02:48 -0700217 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800218
Robert Ginda57f03b42012-09-13 11:02:48 -0700219 if (this.prefs_)
220 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800221
Robert Ginda57f03b42012-09-13 11:02:48 -0700222 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
Joel Hockey95a9e272020-03-16 21:19:53 -0700223
224 /**
225 * Clears and reloads key bindings. Used by preferences
226 * 'keybindings' and 'keybindings-os-defaults'.
227 *
228 * @param {*} bindings
229 * @param {*} useOsDefaults
230 */
231 function loadKeyBindings(bindings, useOsDefaults) {
232 terminal.keyboard.bindings.clear();
233
234 if (!bindings) {
235 return;
236 }
237
238 if (!(bindings instanceof Object)) {
239 console.error('Error in keybindings preference: Expected object');
240 return;
241 }
242
243 try {
244 terminal.keyboard.bindings.addBindings(bindings, !!useOsDefaults);
245 } catch (ex) {
246 console.error('Error in keybindings preference: ' + ex);
247 }
248 }
249
Robert Ginda57f03b42012-09-13 11:02:48 -0700250 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800251 'alt-gr-mode': function(v) {
252 if (v == null) {
253 if (navigator.language.toLowerCase() == 'en-us') {
254 v = 'none';
255 } else {
256 v = 'right-alt';
257 }
258 } else if (typeof v == 'string') {
259 v = v.toLowerCase();
260 } else {
261 v = 'none';
262 }
263
264 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v))
265 v = 'none';
266
267 terminal.keyboard.altGrMode = v;
268 },
269
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700270 'alt-backspace-is-meta-backspace': function(v) {
271 terminal.keyboard.altBackspaceIsMetaBackspace = v;
272 },
273
Robert Ginda57f03b42012-09-13 11:02:48 -0700274 'alt-is-meta': function(v) {
275 terminal.keyboard.altIsMeta = v;
276 },
277
278 'alt-sends-what': function(v) {
279 if (!/^(escape|8-bit|browser-key)$/.test(v))
280 v = 'escape';
281
282 terminal.keyboard.altSendsWhat = v;
283 },
284
285 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800286 var ary = v.match(/^lib-resource:(\S+)/);
287 if (ary) {
288 terminal.bellAudio_.setAttribute('src',
289 lib.resource.getDataUrl(ary[1]));
290 } else {
291 terminal.bellAudio_.setAttribute('src', v);
292 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700293 },
294
Michael Kelly485ecd12014-06-09 11:41:56 -0400295 'desktop-notification-bell': function(v) {
296 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700297 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400298 Notification.permission === 'granted';
299 if (!terminal.desktopNotificationBell_) {
300 // Note: We don't call Notification.requestPermission here because
301 // Chrome requires the call be the result of a user action (such as an
302 // onclick handler), and pref listeners are run asynchronously.
303 //
304 // A way of working around this would be to display a dialog in the
305 // terminal with a "click-to-request-permission" button.
306 console.warn('desktop-notification-bell is true but we do not have ' +
307 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400308 }
309 } else {
310 terminal.desktopNotificationBell_ = false;
311 }
312 },
313
Robert Ginda57f03b42012-09-13 11:02:48 -0700314 'background-color': function(v) {
315 terminal.setBackgroundColor(v);
316 },
317
318 'background-image': function(v) {
319 terminal.scrollPort_.setBackgroundImage(v);
320 },
321
322 'background-size': function(v) {
323 terminal.scrollPort_.setBackgroundSize(v);
324 },
325
326 'background-position': function(v) {
327 terminal.scrollPort_.setBackgroundPosition(v);
328 },
329
330 'backspace-sends-backspace': function(v) {
331 terminal.keyboard.backspaceSendsBackspace = v;
332 },
333
Brad Town18654b62015-03-12 00:27:45 -0700334 'character-map-overrides': function(v) {
335 if (!(v == null || v instanceof Object)) {
336 console.warn('Preference character-map-modifications is not an ' +
337 'object: ' + v);
338 return;
339 }
340
Mike Frysinger095d4062017-06-14 00:29:48 -0700341 terminal.vt.characterMaps.reset();
342 terminal.vt.characterMaps.setOverrides(v);
Brad Town18654b62015-03-12 00:27:45 -0700343 },
344
Robert Ginda57f03b42012-09-13 11:02:48 -0700345 'cursor-blink': function(v) {
346 terminal.setCursorBlink(!!v);
347 },
348
Joel Hockey9d10ba12019-05-28 01:25:02 -0700349 'cursor-shape': function(v) {
350 terminal.setCursorShape(v);
351 },
352
Robert Gindaea2183e2014-07-17 09:51:51 -0700353 'cursor-blink-cycle': function(v) {
354 if (v instanceof Array &&
355 typeof v[0] == 'number' &&
356 typeof v[1] == 'number') {
357 terminal.cursorBlinkCycle_ = v;
358 } else if (typeof v == 'number') {
359 terminal.cursorBlinkCycle_ = [v, v];
360 } else {
361 // Fast blink indicates an error.
362 terminal.cursorBlinkCycle_ = [100, 100];
363 }
364 },
365
Robert Ginda57f03b42012-09-13 11:02:48 -0700366 'cursor-color': function(v) {
367 terminal.setCursorColor(v);
368 },
369
370 'color-palette-overrides': function(v) {
371 if (!(v == null || v instanceof Object || v instanceof Array)) {
372 console.warn('Preference color-palette-overrides is not an array or ' +
373 'object: ' + v);
374 return;
rginda9f5222b2012-03-05 11:53:28 -0800375 }
rginda9f5222b2012-03-05 11:53:28 -0800376
Robert Ginda57f03b42012-09-13 11:02:48 -0700377 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700378
Robert Ginda57f03b42012-09-13 11:02:48 -0700379 if (v) {
380 for (var key in v) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700381 var i = parseInt(key, 10);
Robert Ginda57f03b42012-09-13 11:02:48 -0700382 if (isNaN(i) || i < 0 || i > 255) {
383 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
384 continue;
385 }
386
387 if (v[i]) {
388 var rgb = lib.colors.normalizeCSS(v[i]);
389 if (rgb)
390 lib.colors.colorPalette[i] = rgb;
391 }
392 }
rginda30f20f62012-04-05 16:36:19 -0700393 }
rginda30f20f62012-04-05 16:36:19 -0700394
Evan Jones5f9df812016-12-06 09:38:58 -0500395 terminal.primaryScreen_.textAttributes.resetColorPalette();
Robert Ginda57f03b42012-09-13 11:02:48 -0700396 terminal.alternateScreen_.textAttributes.resetColorPalette();
397 },
rginda30f20f62012-04-05 16:36:19 -0700398
Robert Ginda57f03b42012-09-13 11:02:48 -0700399 'copy-on-select': function(v) {
400 terminal.copyOnSelect = !!v;
401 },
rginda9f5222b2012-03-05 11:53:28 -0800402
Rob Spies0bec09b2014-06-06 15:58:09 -0700403 'use-default-window-copy': function(v) {
404 terminal.useDefaultWindowCopy = !!v;
405 },
406
407 'clear-selection-after-copy': function(v) {
408 terminal.clearSelectionAfterCopy = !!v;
409 },
410
Robert Ginda7e5e9522014-03-14 12:23:58 -0700411 'ctrl-plus-minus-zero-zoom': function(v) {
412 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
413 },
414
Robert Gindafb5a3f92014-05-13 14:12:00 -0700415 'ctrl-c-copy': function(v) {
416 terminal.keyboard.ctrlCCopy = v;
417 },
418
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100419 'ctrl-v-paste': function(v) {
420 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700421 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100422 },
423
Cody Coljee-Gray7c6a0392018-10-25 13:18:28 -0700424 'paste-on-drop': function(v) {
425 terminal.scrollPort_.setPasteOnDrop(v);
426 },
427
Masaya Suzuki273aa982014-05-31 07:25:55 +0900428 'east-asian-ambiguous-as-two-column': function(v) {
429 lib.wc.regardCjkAmbiguous = v;
430 },
431
Robert Ginda57f03b42012-09-13 11:02:48 -0700432 'enable-8-bit-control': function(v) {
433 terminal.vt.enable8BitControl = !!v;
434 },
rginda30f20f62012-04-05 16:36:19 -0700435
Robert Ginda57f03b42012-09-13 11:02:48 -0700436 'enable-bold': function(v) {
437 terminal.syncBoldSafeState();
438 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400439
Robert Ginda3e278d72014-03-25 13:18:51 -0700440 'enable-bold-as-bright': function(v) {
441 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
442 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
443 },
444
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400445 'enable-blink': function(v) {
Mike Frysinger261597c2017-12-28 01:14:21 -0500446 terminal.setTextBlink(!!v);
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400447 },
448
Robert Ginda57f03b42012-09-13 11:02:48 -0700449 'enable-clipboard-write': function(v) {
450 terminal.vt.enableClipboardWrite = !!v;
451 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400452
Robert Ginda3755e752013-05-31 13:34:09 -0700453 'enable-dec12': function(v) {
454 terminal.vt.enableDec12 = !!v;
455 },
456
Mike Frysinger38f267d2018-09-07 02:50:59 -0400457 'enable-csi-j-3': function(v) {
458 terminal.vt.enableCsiJ3 = !!v;
459 },
460
Robert Ginda57f03b42012-09-13 11:02:48 -0700461 'font-family': function(v) {
462 terminal.syncFontFamily();
463 },
rginda30f20f62012-04-05 16:36:19 -0700464
Robert Ginda57f03b42012-09-13 11:02:48 -0700465 'font-size': function(v) {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700466 v = parseInt(v, 10);
Mike Frysinger47853ac2017-12-14 00:44:10 -0500467 if (v <= 0) {
468 console.error(`Invalid font size: ${v}`);
469 return;
470 }
471
Robert Ginda57f03b42012-09-13 11:02:48 -0700472 terminal.setFontSize(v);
473 },
rginda9875d902012-08-20 16:21:57 -0700474
Robert Ginda57f03b42012-09-13 11:02:48 -0700475 'font-smoothing': function(v) {
476 terminal.syncFontFamily();
477 },
rgindade84e382012-04-20 15:39:31 -0700478
Robert Ginda57f03b42012-09-13 11:02:48 -0700479 'foreground-color': function(v) {
480 terminal.setForegroundColor(v);
481 },
rginda30f20f62012-04-05 16:36:19 -0700482
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400483 'hide-mouse-while-typing': function(v) {
484 terminal.setAutomaticMouseHiding(v);
485 },
486
Robert Ginda57f03b42012-09-13 11:02:48 -0700487 'home-keys-scroll': function(v) {
488 terminal.keyboard.homeKeysScroll = v;
489 },
rginda4bba5e12012-06-20 16:15:30 -0700490
Robert Gindaa8165692015-06-15 14:46:31 -0700491 'keybindings': function(v) {
Joel Hockey95a9e272020-03-16 21:19:53 -0700492 loadKeyBindings(v, terminal.prefs_.get('keybindings-os-defaults'));
493 },
Robert Gindaa8165692015-06-15 14:46:31 -0700494
Joel Hockey95a9e272020-03-16 21:19:53 -0700495 'keybindings-os-defaults': function(v) {
496 loadKeyBindings(terminal.prefs_.get('keybindings'), v);
Robert Gindaa8165692015-06-15 14:46:31 -0700497 },
498
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700499 'media-keys-are-fkeys': function(v) {
500 terminal.keyboard.mediaKeysAreFKeys = v;
501 },
502
Robert Ginda57f03b42012-09-13 11:02:48 -0700503 'meta-sends-escape': function(v) {
504 terminal.keyboard.metaSendsEscape = v;
505 },
rginda30f20f62012-04-05 16:36:19 -0700506
Mike Frysinger847577f2017-05-23 23:25:57 -0400507 'mouse-right-click-paste': function(v) {
508 terminal.mouseRightClickPaste = v;
509 },
510
Robert Ginda57f03b42012-09-13 11:02:48 -0700511 'mouse-paste-button': function(v) {
512 terminal.syncMousePasteButton();
513 },
rgindaa8ba17d2012-08-15 14:41:10 -0700514
Robert Gindae76aa9f2014-03-14 12:29:12 -0700515 'page-keys-scroll': function(v) {
516 terminal.keyboard.pageKeysScroll = v;
517 },
518
Robert Ginda40932892012-12-10 17:26:40 -0800519 'pass-alt-number': function(v) {
520 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700521 // Let Alt+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800522 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500523 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800524 }
525
526 terminal.passAltNumber = v;
527 },
528
529 'pass-ctrl-number': function(v) {
530 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700531 // Let Ctrl+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800532 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500533 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800534 }
535
536 terminal.passCtrlNumber = v;
537 },
538
Joel Hockey0e052042020-02-19 05:37:19 -0800539 'pass-ctrl-n': function(v) {
540 terminal.passCtrlN = v;
541 },
542
543 'pass-ctrl-t': function(v) {
544 terminal.passCtrlT = v;
545 },
546
547 'pass-ctrl-tab': function(v) {
548 terminal.passCtrlTab = v;
549 },
550
551 'pass-ctrl-w': function(v) {
552 terminal.passCtrlW = v;
553 },
554
Robert Ginda40932892012-12-10 17:26:40 -0800555 'pass-meta-number': function(v) {
556 if (v == null) {
Joel Hockey46a6e1d2020-03-11 20:01:57 -0700557 // Let Meta+1..9 pass to the browser (to control tab switching) on
Robert Ginda40932892012-12-10 17:26:40 -0800558 // OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500559 v = (hterm.os == 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800560 }
561
562 terminal.passMetaNumber = v;
563 },
564
Marius Schilder77857b32014-05-14 16:21:26 -0700565 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700566 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700567 },
568
Robert Ginda8cb7d902013-06-20 14:37:18 -0700569 'receive-encoding': function(v) {
570 if (!(/^(utf-8|raw)$/).test(v)) {
571 console.warn('Invalid value for "receive-encoding": ' + v);
572 v = 'utf-8';
573 }
574
575 terminal.vt.characterEncoding = v;
576 },
577
Robert Ginda57f03b42012-09-13 11:02:48 -0700578 'scroll-on-keystroke': function(v) {
579 terminal.scrollOnKeystroke_ = v;
580 },
rginda9f5222b2012-03-05 11:53:28 -0800581
Robert Ginda57f03b42012-09-13 11:02:48 -0700582 'scroll-on-output': function(v) {
583 terminal.scrollOnOutput_ = v;
584 },
rginda30f20f62012-04-05 16:36:19 -0700585
Robert Ginda57f03b42012-09-13 11:02:48 -0700586 'scrollbar-visible': function(v) {
587 terminal.setScrollbarVisible(v);
588 },
rginda9f5222b2012-03-05 11:53:28 -0800589
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400590 'scroll-wheel-may-send-arrow-keys': function(v) {
591 terminal.scrollWheelArrowKeys_ = v;
592 },
593
Rob Spies49039e52014-12-17 13:40:04 -0800594 'scroll-wheel-move-multiplier': function(v) {
595 terminal.setScrollWheelMoveMultipler(v);
596 },
597
Robert Ginda57f03b42012-09-13 11:02:48 -0700598 'shift-insert-paste': function(v) {
599 terminal.keyboard.shiftInsertPaste = v;
600 },
rginda9f5222b2012-03-05 11:53:28 -0800601
Mike Frysingera7768922017-07-28 15:00:12 -0400602 'terminal-encoding': function(v) {
Mike Frysingera1371e12017-08-17 01:37:17 -0400603 terminal.vt.setEncoding(v);
Mike Frysingera7768922017-07-28 15:00:12 -0400604 },
605
Robert Gindae76aa9f2014-03-14 12:29:12 -0700606 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400607 terminal.scrollPort_.setUserCssUrl(v);
608 },
609
610 'user-css-text': function(v) {
611 terminal.scrollPort_.setUserCssText(v);
612 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400613
614 'word-break-match-left': function(v) {
615 terminal.primaryScreen_.wordBreakMatchLeft = v;
616 terminal.alternateScreen_.wordBreakMatchLeft = v;
617 },
618
619 'word-break-match-right': function(v) {
620 terminal.primaryScreen_.wordBreakMatchRight = v;
621 terminal.alternateScreen_.wordBreakMatchRight = v;
622 },
623
624 'word-break-match-middle': function(v) {
625 terminal.primaryScreen_.wordBreakMatchMiddle = v;
626 terminal.alternateScreen_.wordBreakMatchMiddle = v;
627 },
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400628
629 'allow-images-inline': function(v) {
630 terminal.allowImagesInline = v;
631 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700632 });
rginda30f20f62012-04-05 16:36:19 -0700633
Robert Ginda57f03b42012-09-13 11:02:48 -0700634 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800635 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700636
637 if (opt_callback)
638 opt_callback();
639 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800640};
641
Rob Spies56953412014-04-28 14:09:47 -0700642/**
643 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500644 *
Joel Hockey0f933582019-08-27 18:01:51 -0700645 * @return {!hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700646 */
647hterm.Terminal.prototype.getPrefs = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700648 return lib.notNull(this.prefs_);
Rob Spies56953412014-04-28 14:09:47 -0700649};
650
Robert Gindaa063b202014-07-21 11:08:25 -0700651/**
652 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500653 *
654 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700655 */
656hterm.Terminal.prototype.setBracketedPaste = function(state) {
657 this.options_.bracketedPaste = state;
658};
Rob Spies56953412014-04-28 14:09:47 -0700659
rginda8e92a692012-05-20 19:37:20 -0700660/**
661 * Set the color for the cursor.
662 *
663 * If you want this setting to persist, set it through prefs_, rather than
664 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500665 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500666 * @param {string=} color The color to set. If not defined, we reset to the
667 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700668 */
669hterm.Terminal.prototype.setCursorColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500670 if (color === undefined)
Joel Hockeyd4fca732019-09-20 16:57:03 -0700671 color = this.prefs_.getString('cursor-color');
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500672
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400673 this.setCssVar('cursor-color', color);
rginda8e92a692012-05-20 19:37:20 -0700674};
675
676/**
677 * Return the current cursor color as a string.
Mike Frysinger23b5b832019-10-01 17:05:29 -0400678 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500679 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700680 */
681hterm.Terminal.prototype.getCursorColor = function() {
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400682 return this.getCssVar('cursor-color');
rginda8e92a692012-05-20 19:37:20 -0700683};
684
685/**
rgindad5613292012-06-19 15:40:37 -0700686 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500687 *
688 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700689 */
690hterm.Terminal.prototype.setSelectionEnabled = function(state) {
691 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700692};
693
694/**
rginda8e92a692012-05-20 19:37:20 -0700695 * Set the background color.
696 *
697 * If you want this setting to persist, set it through prefs_, rather than
698 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500699 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500700 * @param {string=} color The color to set. If not defined, we reset to the
701 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700702 */
703hterm.Terminal.prototype.setBackgroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500704 if (color === undefined)
Joel Hockeyd4fca732019-09-20 16:57:03 -0700705 color = this.prefs_.getString('background-color');
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500706
Joel Hockey8ff48232019-09-24 13:15:17 -0700707 this.backgroundColor_ = lib.colors.normalizeCSS(color) || '';
Robert Ginda57f03b42012-09-13 11:02:48 -0700708 this.primaryScreen_.textAttributes.setDefaults(
709 this.foregroundColor_, this.backgroundColor_);
710 this.alternateScreen_.textAttributes.setDefaults(
711 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700712 this.scrollPort_.setBackgroundColor(color);
713};
714
rginda9f5222b2012-03-05 11:53:28 -0800715/**
716 * Return the current terminal background color.
717 *
718 * Intended for use by other classes, so we don't have to expose the entire
719 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500720 *
721 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800722 */
723hterm.Terminal.prototype.getBackgroundColor = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700724 return lib.notNull(this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700725};
726
727/**
728 * Set the foreground color.
729 *
730 * If you want this setting to persist, set it through prefs_, rather than
731 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500732 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500733 * @param {string=} color The color to set. If not defined, we reset to the
734 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700735 */
736hterm.Terminal.prototype.setForegroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500737 if (color === undefined)
Joel Hockeyd4fca732019-09-20 16:57:03 -0700738 color = this.prefs_.getString('foreground-color');
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500739
Joel Hockey8ff48232019-09-24 13:15:17 -0700740 this.foregroundColor_ = lib.colors.normalizeCSS(color) || '';
Robert Ginda57f03b42012-09-13 11:02:48 -0700741 this.primaryScreen_.textAttributes.setDefaults(
742 this.foregroundColor_, this.backgroundColor_);
743 this.alternateScreen_.textAttributes.setDefaults(
744 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700745 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800746};
747
748/**
749 * Return the current terminal foreground color.
750 *
751 * Intended for use by other classes, so we don't have to expose the entire
752 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500753 *
754 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800755 */
756hterm.Terminal.prototype.getForegroundColor = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700757 return lib.notNull(this.foregroundColor_);
rginda9f5222b2012-03-05 11:53:28 -0800758};
759
760/**
rginda87b86462011-12-14 13:48:03 -0800761 * Create a new instance of a terminal command and run it with a given
762 * argument string.
763 *
Joel Hockeyd4fca732019-09-20 16:57:03 -0700764 * @param {!Function} commandClass The constructor for a terminal command.
Joel Hockey8081ea62019-08-26 16:52:32 -0700765 * @param {string} commandName The command to run for this terminal.
766 * @param {!Array<string>} args The arguments to pass to the command.
rginda87b86462011-12-14 13:48:03 -0800767 */
Joel Hockey8081ea62019-08-26 16:52:32 -0700768hterm.Terminal.prototype.runCommandClass = function(
769 commandClass, commandName, args) {
rgindaf522ce02012-04-17 17:49:17 -0700770 var environment = this.prefs_.get('environment');
771 if (typeof environment != 'object' || environment == null)
772 environment = {};
773
rginda87b86462011-12-14 13:48:03 -0800774 var self = this;
775 this.command = new commandClass(
Joel Hockey8081ea62019-08-26 16:52:32 -0700776 {
777 commandName: commandName,
778 args: args,
rginda87b86462011-12-14 13:48:03 -0800779 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700780 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800781 onExit: function(code) {
782 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800783 self.uninstallKeyboard();
Julian Watsondfbf8592019-11-05 18:05:12 +1100784 self.div_.dispatchEvent(new CustomEvent('terminal-closing'));
rginda9875d902012-08-20 16:21:57 -0700785 if (self.prefs_.get('close-on-exit'))
786 window.close();
rginda87b86462011-12-14 13:48:03 -0800787 }
788 });
789
rgindafeaf3142012-01-31 15:14:20 -0800790 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800791 this.command.run();
792};
793
794/**
rgindafeaf3142012-01-31 15:14:20 -0800795 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500796 *
797 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800798 */
799hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700800 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800801};
802
803/**
804 * Install the keyboard handler for this terminal.
805 *
806 * This will prevent the browser from seeing any keystrokes sent to the
807 * terminal.
808 */
809hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700810 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400811};
rgindafeaf3142012-01-31 15:14:20 -0800812
813/**
814 * Uninstall the keyboard handler for this terminal.
815 */
816hterm.Terminal.prototype.uninstallKeyboard = function() {
817 this.keyboard.installKeyboard(null);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400818};
rgindafeaf3142012-01-31 15:14:20 -0800819
820/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400821 * Set a CSS variable.
822 *
823 * Normally this is used to set variables in the hterm namespace.
824 *
825 * @param {string} name The variable to set.
Joel Hockeyd4fca732019-09-20 16:57:03 -0700826 * @param {string|number} value The value to assign to the variable.
Joel Hockey0f933582019-08-27 18:01:51 -0700827 * @param {string=} opt_prefix The variable namespace/prefix to use.
Mike Frysingercce97c42017-08-05 01:11:22 -0400828 */
829hterm.Terminal.prototype.setCssVar = function(name, value,
830 opt_prefix='--hterm-') {
831 this.document_.documentElement.style.setProperty(
Joel Hockeyd4fca732019-09-20 16:57:03 -0700832 `${opt_prefix}${name}`, value.toString());
Mike Frysingercce97c42017-08-05 01:11:22 -0400833};
834
835/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500836 * Get a CSS variable.
837 *
838 * Normally this is used to get variables in the hterm namespace.
839 *
840 * @param {string} name The variable to read.
Joel Hockey0f933582019-08-27 18:01:51 -0700841 * @param {string=} opt_prefix The variable namespace/prefix to use.
Mike Frysinger261597c2017-12-28 01:14:21 -0500842 * @return {string} The current setting for this variable.
843 */
844hterm.Terminal.prototype.getCssVar = function(name, opt_prefix='--hterm-') {
845 return this.document_.documentElement.style.getPropertyValue(
846 `${opt_prefix}${name}`);
847};
848
849/**
Jason Linbbbdb752020-03-06 16:26:59 +1100850 * Update CSS character size variables to match the scrollport.
851 */
852hterm.Terminal.prototype.updateCssCharsize_ = function() {
853 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
854 this.setCssVar('charsize-height',
855 this.scrollPort_.characterSize.height + 'px');
856};
857
858/**
rginda35c456b2012-02-09 17:29:05 -0800859 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800860 *
861 * Call setFontSize(0) to reset to the default font size.
862 *
863 * This function does not modify the font-size preference.
864 *
865 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800866 */
867hterm.Terminal.prototype.setFontSize = function(px) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500868 if (px <= 0)
Joel Hockeyd4fca732019-09-20 16:57:03 -0700869 px = this.prefs_.getNumber('font-size');
rginda9f5222b2012-03-05 11:53:28 -0800870
rginda35c456b2012-02-09 17:29:05 -0800871 this.scrollPort_.setFontSize(px);
Jason Linbbbdb752020-03-06 16:26:59 +1100872 this.updateCssCharsize_();
rginda35c456b2012-02-09 17:29:05 -0800873};
874
875/**
876 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500877 *
878 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800879 */
880hterm.Terminal.prototype.getFontSize = function() {
881 return this.scrollPort_.getFontSize();
882};
883
884/**
rginda8e92a692012-05-20 19:37:20 -0700885 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500886 *
887 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700888 */
889hterm.Terminal.prototype.getFontFamily = function() {
890 return this.scrollPort_.getFontFamily();
891};
892
893/**
rginda35c456b2012-02-09 17:29:05 -0800894 * Set the CSS "font-family" for this terminal.
895 */
rginda9f5222b2012-03-05 11:53:28 -0800896hterm.Terminal.prototype.syncFontFamily = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -0700897 this.scrollPort_.setFontFamily(this.prefs_.getString('font-family'),
898 this.prefs_.getString('font-smoothing'));
Jason Linbbbdb752020-03-06 16:26:59 +1100899 this.updateCssCharsize_();
rginda9f5222b2012-03-05 11:53:28 -0800900 this.syncBoldSafeState();
901};
902
rginda4bba5e12012-06-20 16:15:30 -0700903/**
904 * Set this.mousePasteButton based on the mouse-paste-button pref,
905 * autodetecting if necessary.
906 */
907hterm.Terminal.prototype.syncMousePasteButton = function() {
908 var button = this.prefs_.get('mouse-paste-button');
909 if (typeof button == 'number') {
910 this.mousePasteButton = button;
911 return;
912 }
913
Mike Frysingeree81a002017-12-12 16:14:53 -0500914 if (hterm.os != 'linux') {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400915 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700916 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400917 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700918 }
919};
920
921/**
922 * Enable or disable bold based on the enable-bold pref, autodetecting if
923 * necessary.
924 */
rginda9f5222b2012-03-05 11:53:28 -0800925hterm.Terminal.prototype.syncBoldSafeState = function() {
926 var enableBold = this.prefs_.get('enable-bold');
927 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700928 this.primaryScreen_.textAttributes.enableBold = enableBold;
929 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800930 return;
931 }
932
rgindaf7521392012-02-28 17:20:34 -0800933 var normalSize = this.scrollPort_.measureCharacterSize();
934 var boldSize = this.scrollPort_.measureCharacterSize('bold');
935
936 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800937 if (!isBoldSafe) {
938 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700939 'from normal. Font family is: ' +
940 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800941 }
rginda9f5222b2012-03-05 11:53:28 -0800942
Robert Gindaed016262012-10-26 16:27:09 -0700943 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
944 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800945};
946
947/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500948 * Control text blinking behavior.
949 *
950 * @param {boolean=} state Whether to enable support for blinking text.
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400951 */
Mike Frysinger261597c2017-12-28 01:14:21 -0500952hterm.Terminal.prototype.setTextBlink = function(state) {
953 if (state === undefined)
Joel Hockeyd4fca732019-09-20 16:57:03 -0700954 state = this.prefs_.getBoolean('enable-blink');
Mike Frysinger261597c2017-12-28 01:14:21 -0500955 this.setCssVar('blink-node-duration', state ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400956};
957
958/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400959 * Set the mouse cursor style based on the current terminal mode.
960 */
961hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -0400962 this.setCssVar('mouse-cursor-style',
963 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
964 'var(--hterm-mouse-cursor-text)' :
Mike Frysinger67f58f82018-11-22 13:38:22 -0500965 'var(--hterm-mouse-cursor-default)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400966};
967
968/**
rginda87b86462011-12-14 13:48:03 -0800969 * Return a copy of the current cursor position.
970 *
Joel Hockey0f933582019-08-27 18:01:51 -0700971 * @return {!hterm.RowCol} The RowCol object representing the current position.
rginda87b86462011-12-14 13:48:03 -0800972 */
973hterm.Terminal.prototype.saveCursor = function() {
974 return this.screen_.cursorPosition.clone();
975};
976
Evan Jones2600d4f2016-12-06 09:29:36 -0500977/**
978 * Return the current text attributes.
979 *
Joel Hockeyd4fca732019-09-20 16:57:03 -0700980 * @return {!hterm.TextAttributes}
Evan Jones2600d4f2016-12-06 09:29:36 -0500981 */
rgindaa19afe22012-01-25 15:40:22 -0800982hterm.Terminal.prototype.getTextAttributes = function() {
983 return this.screen_.textAttributes;
984};
985
Evan Jones2600d4f2016-12-06 09:29:36 -0500986/**
987 * Set the text attributes.
988 *
Joel Hockeyd4fca732019-09-20 16:57:03 -0700989 * @param {!hterm.TextAttributes} textAttributes The attributes to set.
Evan Jones2600d4f2016-12-06 09:29:36 -0500990 */
rginda1a09aa02012-06-18 21:11:25 -0700991hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
992 this.screen_.textAttributes = textAttributes;
993};
994
rginda87b86462011-12-14 13:48:03 -0800995/**
rgindaf522ce02012-04-17 17:49:17 -0700996 * Return the current browser zoom factor applied to the terminal.
997 *
998 * @return {number} The current browser zoom factor.
999 */
1000hterm.Terminal.prototype.getZoomFactor = function() {
1001 return this.scrollPort_.characterSize.zoomFactor;
1002};
1003
1004/**
rginda9846e2f2012-01-27 13:53:33 -08001005 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -05001006 *
1007 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -08001008 */
1009hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -08001010 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -08001011};
1012
1013/**
rginda87b86462011-12-14 13:48:03 -08001014 * Restore a previously saved cursor position.
1015 *
Joel Hockey0f933582019-08-27 18:01:51 -07001016 * @param {!hterm.RowCol} cursor The position to restore.
rginda87b86462011-12-14 13:48:03 -08001017 */
1018hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -07001019 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
1020 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -08001021 this.screen_.setCursorPosition(row, column);
1022 if (cursor.column > column ||
1023 cursor.column == column && cursor.overflow) {
1024 this.screen_.cursorPosition.overflow = true;
1025 }
rginda87b86462011-12-14 13:48:03 -08001026};
1027
1028/**
David Benjamin54e8bf62012-06-01 22:31:40 -04001029 * Clear the cursor's overflow flag.
1030 */
1031hterm.Terminal.prototype.clearCursorOverflow = function() {
1032 this.screen_.cursorPosition.overflow = false;
1033};
1034
1035/**
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001036 * Save the current cursor state to the corresponding screens.
1037 *
1038 * See the hterm.Screen.CursorState class for more details.
1039 *
1040 * @param {boolean=} both If true, update both screens, else only update the
1041 * current screen.
1042 */
1043hterm.Terminal.prototype.saveCursorAndState = function(both) {
1044 if (both) {
1045 this.primaryScreen_.saveCursorAndState(this.vt);
1046 this.alternateScreen_.saveCursorAndState(this.vt);
1047 } else
1048 this.screen_.saveCursorAndState(this.vt);
1049};
1050
1051/**
1052 * Restore the saved cursor state in the corresponding screens.
1053 *
1054 * See the hterm.Screen.CursorState class for more details.
1055 *
1056 * @param {boolean=} both If true, update both screens, else only update the
1057 * current screen.
1058 */
1059hterm.Terminal.prototype.restoreCursorAndState = function(both) {
1060 if (both) {
1061 this.primaryScreen_.restoreCursorAndState(this.vt);
1062 this.alternateScreen_.restoreCursorAndState(this.vt);
1063 } else
1064 this.screen_.restoreCursorAndState(this.vt);
1065};
1066
1067/**
Robert Ginda830583c2013-08-07 13:20:46 -07001068 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001069 *
1070 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -07001071 */
1072hterm.Terminal.prototype.setCursorShape = function(shape) {
1073 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001074 this.restyleCursor_();
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001075};
Robert Ginda830583c2013-08-07 13:20:46 -07001076
1077/**
1078 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001079 *
1080 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -07001081 */
1082hterm.Terminal.prototype.getCursorShape = function() {
1083 return this.cursorShape_;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001084};
Robert Ginda830583c2013-08-07 13:20:46 -07001085
1086/**
rginda87b86462011-12-14 13:48:03 -08001087 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001088 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001089 * @param {?number} columnCount
rginda87b86462011-12-14 13:48:03 -08001090 */
1091hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -08001092 if (columnCount == null) {
1093 this.div_.style.width = '100%';
1094 return;
1095 }
1096
Robert Ginda26806d12014-07-24 13:44:07 -07001097 this.div_.style.width = Math.ceil(
1098 this.scrollPort_.characterSize.width *
1099 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001100 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -08001101 this.scheduleSyncCursorPosition_();
1102};
rginda87b86462011-12-14 13:48:03 -08001103
rgindac9bc5502012-01-18 11:48:44 -08001104/**
rginda35c456b2012-02-09 17:29:05 -08001105 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001106 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001107 * @param {?number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -08001108 */
1109hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -08001110 if (rowCount == null) {
1111 this.div_.style.height = '100%';
1112 return;
1113 }
1114
rginda35c456b2012-02-09 17:29:05 -08001115 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -07001116 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -08001117 this.realizeSize_(this.screenSize.width, rowCount);
1118 this.scheduleSyncCursorPosition_();
1119};
1120
1121/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001122 * Deal with terminal size changes.
1123 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001124 * @param {number} columnCount The number of columns.
1125 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001126 */
1127hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
Mike Frysinger0206e262019-06-13 10:18:19 -04001128 let notify = false;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001129
Mike Frysinger0206e262019-06-13 10:18:19 -04001130 if (columnCount != this.screenSize.width) {
1131 notify = true;
1132 this.realizeWidth_(columnCount);
1133 }
1134
1135 if (rowCount != this.screenSize.height) {
1136 notify = true;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001137 this.realizeHeight_(rowCount);
Mike Frysinger0206e262019-06-13 10:18:19 -04001138 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001139
1140 // Send new terminal size to plugin.
Mike Frysinger0206e262019-06-13 10:18:19 -04001141 if (notify) {
1142 this.io.onTerminalResize_(columnCount, rowCount);
1143 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001144};
1145
1146/**
rgindac9bc5502012-01-18 11:48:44 -08001147 * Deal with terminal width changes.
1148 *
1149 * This function does what needs to be done when the terminal width changes
1150 * out from under us. It happens here rather than in onResize_() because this
1151 * code may need to run synchronously to handle programmatic changes of
1152 * terminal width.
1153 *
1154 * Relying on the browser to send us an async resize event means we may not be
1155 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001156 *
1157 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001158 */
1159hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001160 if (columnCount <= 0)
1161 throw new Error('Attempt to realize bad width: ' + columnCount);
1162
rgindac9bc5502012-01-18 11:48:44 -08001163 var deltaColumns = columnCount - this.screen_.getWidth();
Mike Frysinger0206e262019-06-13 10:18:19 -04001164 if (deltaColumns == 0) {
1165 // No change, so don't bother recalculating things.
1166 return;
1167 }
rgindac9bc5502012-01-18 11:48:44 -08001168
rginda87b86462011-12-14 13:48:03 -08001169 this.screenSize.width = columnCount;
1170 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001171
1172 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -04001173 if (this.defaultTabStops)
1174 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -08001175 } else {
1176 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -04001177 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -08001178 break;
1179
1180 this.tabStops_.pop();
1181 }
1182 }
1183
1184 this.screen_.setColumnCount(this.screenSize.width);
1185};
1186
1187/**
1188 * Deal with terminal height changes.
1189 *
1190 * This function does what needs to be done when the terminal height changes
1191 * out from under us. It happens here rather than in onResize_() because this
1192 * code may need to run synchronously to handle programmatic changes of
1193 * terminal height.
1194 *
1195 * Relying on the browser to send us an async resize event means we may not be
1196 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001197 *
1198 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001199 */
1200hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001201 if (rowCount <= 0)
1202 throw new Error('Attempt to realize bad height: ' + rowCount);
1203
rgindac9bc5502012-01-18 11:48:44 -08001204 var deltaRows = rowCount - this.screen_.getHeight();
Mike Frysinger0206e262019-06-13 10:18:19 -04001205 if (deltaRows == 0) {
1206 // No change, so don't bother recalculating things.
1207 return;
1208 }
rgindac9bc5502012-01-18 11:48:44 -08001209
1210 this.screenSize.height = rowCount;
1211
1212 var cursor = this.saveCursor();
1213
1214 if (deltaRows < 0) {
1215 // Screen got smaller.
1216 deltaRows *= -1;
1217 while (deltaRows) {
1218 var lastRow = this.getRowCount() - 1;
1219 if (lastRow - this.scrollbackRows_.length == cursor.row)
1220 break;
1221
1222 if (this.getRowText(lastRow))
1223 break;
1224
1225 this.screen_.popRow();
1226 deltaRows--;
1227 }
1228
1229 var ary = this.screen_.shiftRows(deltaRows);
1230 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1231
1232 // We just removed rows from the top of the screen, we need to update
1233 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001234 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001235 } else if (deltaRows > 0) {
1236 // Screen got larger.
1237
1238 if (deltaRows <= this.scrollbackRows_.length) {
1239 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1240 var rows = this.scrollbackRows_.splice(
1241 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1242 this.screen_.unshiftRows(rows);
1243 deltaRows -= scrollbackCount;
1244 cursor.row += scrollbackCount;
1245 }
1246
1247 if (deltaRows)
1248 this.appendRows_(deltaRows);
1249 }
1250
rginda35c456b2012-02-09 17:29:05 -08001251 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001252 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001253};
1254
1255/**
1256 * Scroll the terminal to the top of the scrollback buffer.
1257 */
1258hterm.Terminal.prototype.scrollHome = function() {
1259 this.scrollPort_.scrollRowToTop(0);
1260};
1261
1262/**
1263 * Scroll the terminal to the end.
1264 */
1265hterm.Terminal.prototype.scrollEnd = function() {
1266 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1267};
1268
1269/**
1270 * Scroll the terminal one page up (minus one line) relative to the current
1271 * position.
1272 */
1273hterm.Terminal.prototype.scrollPageUp = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001274 this.scrollPort_.scrollPageUp();
rginda87b86462011-12-14 13:48:03 -08001275};
1276
1277/**
1278 * Scroll the terminal one page down (minus one line) relative to the current
1279 * position.
1280 */
1281hterm.Terminal.prototype.scrollPageDown = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001282 this.scrollPort_.scrollPageDown();
rginda8ba33642011-12-14 12:31:31 -08001283};
1284
rgindac9bc5502012-01-18 11:48:44 -08001285/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001286 * Scroll the terminal one line up relative to the current position.
1287 */
1288hterm.Terminal.prototype.scrollLineUp = function() {
1289 var i = this.scrollPort_.getTopRowIndex();
1290 this.scrollPort_.scrollRowToTop(i - 1);
1291};
1292
1293/**
1294 * Scroll the terminal one line down relative to the current position.
1295 */
1296hterm.Terminal.prototype.scrollLineDown = function() {
1297 var i = this.scrollPort_.getTopRowIndex();
1298 this.scrollPort_.scrollRowToTop(i + 1);
1299};
1300
1301/**
Robert Ginda40932892012-12-10 17:26:40 -08001302 * Clear primary screen, secondary screen, and the scrollback buffer.
1303 */
1304hterm.Terminal.prototype.wipeContents = function() {
Mike Frysinger9c482b82018-09-07 02:49:36 -04001305 this.clearHome(this.primaryScreen_);
1306 this.clearHome(this.alternateScreen_);
1307
1308 this.clearScrollback();
1309};
1310
1311/**
1312 * Clear scrollback buffer.
1313 */
1314hterm.Terminal.prototype.clearScrollback = function() {
1315 // Move to the end of the buffer in case the screen was scrolled back.
1316 // We're going to throw it away which would leave the display invalid.
1317 this.scrollEnd();
1318
Robert Ginda40932892012-12-10 17:26:40 -08001319 this.scrollbackRows_.length = 0;
1320 this.scrollPort_.resetCache();
1321
Mike Frysinger9c482b82018-09-07 02:49:36 -04001322 [this.primaryScreen_, this.alternateScreen_].forEach((screen) => {
1323 const bottom = screen.getHeight();
1324 this.renumberRows_(0, bottom, screen);
1325 });
Robert Ginda40932892012-12-10 17:26:40 -08001326
1327 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001328 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001329};
1330
1331/**
rgindac9bc5502012-01-18 11:48:44 -08001332 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001333 *
1334 * Perform a full reset to the default values listed in
1335 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001336 */
rginda87b86462011-12-14 13:48:03 -08001337hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001338 this.vt.reset();
1339
rgindac9bc5502012-01-18 11:48:44 -08001340 this.clearAllTabStops();
1341 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001342
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001343 const resetScreen = (screen) => {
1344 // We want to make sure to reset the attributes before we clear the screen.
1345 // The attributes might be used to initialize default/empty rows.
1346 screen.textAttributes.reset();
1347 screen.textAttributes.resetColorPalette();
1348 this.clearHome(screen);
1349 screen.saveCursorAndState(this.vt);
1350 };
1351 resetScreen(this.primaryScreen_);
1352 resetScreen(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001353
Mike Frysinger84301d02017-11-29 13:28:46 -08001354 // Reset terminal options to their default values.
1355 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001356 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1357
Mike Frysinger84301d02017-11-29 13:28:46 -08001358 this.setVTScrollRegion(null, null);
1359
1360 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001361};
1362
rgindac9bc5502012-01-18 11:48:44 -08001363/**
1364 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001365 *
1366 * Perform a soft reset to the default values listed in
1367 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001368 */
rginda0f5c0292012-01-13 11:00:13 -08001369hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001370 this.vt.reset();
1371
rgindab8bc8932012-04-27 12:45:03 -07001372 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001373 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001374
Brad Townb62dfdc2015-03-16 19:07:15 -07001375 // We show the cursor on soft reset but do not alter the blink state.
1376 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1377
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001378 const resetScreen = (screen) => {
1379 // Xterm also resets the color palette on soft reset, even though it doesn't
1380 // seem to be documented anywhere.
1381 screen.textAttributes.reset();
1382 screen.textAttributes.resetColorPalette();
1383 screen.saveCursorAndState(this.vt);
1384 };
1385 resetScreen(this.primaryScreen_);
1386 resetScreen(this.alternateScreen_);
rgindaf522ce02012-04-17 17:49:17 -07001387
rgindab8bc8932012-04-27 12:45:03 -07001388 // The xterm man page explicitly says this will happen on soft reset.
1389 this.setVTScrollRegion(null, null);
1390
1391 // Xterm also shows the cursor on soft reset, but does not alter the blink
1392 // state.
rgindaa19afe22012-01-25 15:40:22 -08001393 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001394};
1395
rgindac9bc5502012-01-18 11:48:44 -08001396/**
1397 * Move the cursor forward to the next tab stop, or to the last column
1398 * if no more tab stops are set.
1399 */
1400hterm.Terminal.prototype.forwardTabStop = function() {
1401 var column = this.screen_.cursorPosition.column;
1402
1403 for (var i = 0; i < this.tabStops_.length; i++) {
1404 if (this.tabStops_[i] > column) {
1405 this.setCursorColumn(this.tabStops_[i]);
1406 return;
1407 }
1408 }
1409
David Benjamin66e954d2012-05-05 21:08:12 -04001410 // xterm does not clear the overflow flag on HT or CHT.
1411 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001412 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001413 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001414};
1415
rgindac9bc5502012-01-18 11:48:44 -08001416/**
1417 * Move the cursor backward to the previous tab stop, or to the first column
1418 * if no previous tab stops are set.
1419 */
1420hterm.Terminal.prototype.backwardTabStop = function() {
1421 var column = this.screen_.cursorPosition.column;
1422
1423 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1424 if (this.tabStops_[i] < column) {
1425 this.setCursorColumn(this.tabStops_[i]);
1426 return;
1427 }
1428 }
1429
1430 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001431};
1432
rgindac9bc5502012-01-18 11:48:44 -08001433/**
1434 * Set a tab stop at the given column.
1435 *
Joel Hockey0f933582019-08-27 18:01:51 -07001436 * @param {number} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001437 */
1438hterm.Terminal.prototype.setTabStop = function(column) {
1439 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1440 if (this.tabStops_[i] == column)
1441 return;
1442
1443 if (this.tabStops_[i] < column) {
1444 this.tabStops_.splice(i + 1, 0, column);
1445 return;
1446 }
1447 }
1448
1449 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001450};
1451
rgindac9bc5502012-01-18 11:48:44 -08001452/**
1453 * Clear the tab stop at the current cursor position.
1454 *
1455 * No effect if there is no tab stop at the current cursor position.
1456 */
1457hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1458 var column = this.screen_.cursorPosition.column;
1459
1460 var i = this.tabStops_.indexOf(column);
1461 if (i == -1)
1462 return;
1463
1464 this.tabStops_.splice(i, 1);
1465};
1466
1467/**
1468 * Clear all tab stops.
1469 */
1470hterm.Terminal.prototype.clearAllTabStops = function() {
1471 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001472 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001473};
1474
1475/**
1476 * Set up the default tab stops, starting from a given column.
1477 *
1478 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001479 * from the specified column, or 0 if no column is provided. It also flags
1480 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001481 *
1482 * This does not clear the existing tab stops first, use clearAllTabStops
1483 * for that.
1484 *
Joel Hockey0f933582019-08-27 18:01:51 -07001485 * @param {number=} opt_start Optional starting zero based starting column,
1486 * useful for filling out missing tab stops when the terminal is resized.
rgindac9bc5502012-01-18 11:48:44 -08001487 */
1488hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1489 var start = opt_start || 0;
1490 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001491 // Round start up to a default tab stop.
1492 start = start - 1 - ((start - 1) % w) + w;
1493 for (var i = start; i < this.screenSize.width; i += w) {
1494 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001495 }
David Benjamin66e954d2012-05-05 21:08:12 -04001496
1497 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001498};
1499
rginda6d397402012-01-17 10:58:29 -08001500/**
rginda8ba33642011-12-14 12:31:31 -08001501 * Interpret a sequence of characters.
1502 *
1503 * Incomplete escape sequences are buffered until the next call.
1504 *
1505 * @param {string} str Sequence of characters to interpret or pass through.
1506 */
1507hterm.Terminal.prototype.interpret = function(str) {
rginda8ba33642011-12-14 12:31:31 -08001508 this.scheduleSyncCursorPosition_();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001509 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001510};
1511
1512/**
1513 * Take over the given DIV for use as the terminal display.
1514 *
Joel Hockey0f933582019-08-27 18:01:51 -07001515 * @param {!Element} div The div to use as the terminal display.
rginda8ba33642011-12-14 12:31:31 -08001516 */
1517hterm.Terminal.prototype.decorate = function(div) {
Mike Frysinger5768a9d2017-12-26 12:57:44 -05001518 const charset = div.ownerDocument.characterSet.toLowerCase();
1519 if (charset != 'utf-8') {
1520 console.warn(`Document encoding should be set to utf-8, not "${charset}";` +
1521 ` Add <meta charset='utf-8'/> to your HTML <head> to fix.`);
1522 }
1523
rginda87b86462011-12-14 13:48:03 -08001524 this.div_ = div;
1525
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001526 this.accessibilityReader_ = new hterm.AccessibilityReader(div);
1527
Adrián Pérez-Orozco394e64f2018-12-17 17:20:16 -08001528 this.scrollPort_.decorate(div, () => this.setupScrollPort_());
1529};
1530
1531/**
1532 * Initialisation of ScrollPort properties which need to be set after its DOM
1533 * has been initialised.
Mike Frysinger23b5b832019-10-01 17:05:29 -04001534 *
Adrián Pérez-Orozco394e64f2018-12-17 17:20:16 -08001535 * @private
1536 */
1537hterm.Terminal.prototype.setupScrollPort_ = function() {
Joel Hockeyd4fca732019-09-20 16:57:03 -07001538 this.scrollPort_.setBackgroundImage(
1539 this.prefs_.getString('background-image'));
1540 this.scrollPort_.setBackgroundSize(this.prefs_.getString('background-size'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001541 this.scrollPort_.setBackgroundPosition(
Joel Hockeyd4fca732019-09-20 16:57:03 -07001542 this.prefs_.getString('background-position'));
1543 this.scrollPort_.setUserCssUrl(this.prefs_.getString('user-css'));
1544 this.scrollPort_.setUserCssText(this.prefs_.getString('user-css-text'));
1545 this.scrollPort_.setAccessibilityReader(
1546 lib.notNull(this.accessibilityReader_));
rginda30f20f62012-04-05 16:36:19 -07001547
rginda0918b652012-04-04 11:26:24 -07001548 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001549
Joel Hockeyd4fca732019-09-20 16:57:03 -07001550 this.setFontSize(this.prefs_.getNumber('font-size'));
rginda9f5222b2012-03-05 11:53:28 -08001551 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001552
Joel Hockeyd4fca732019-09-20 16:57:03 -07001553 this.setScrollbarVisible(this.prefs_.getBoolean('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001554 this.setScrollWheelMoveMultipler(
Joel Hockeyd4fca732019-09-20 16:57:03 -07001555 this.prefs_.getNumber('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001556
rginda8ba33642011-12-14 12:31:31 -08001557 this.document_ = this.scrollPort_.getDocument();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001558 this.accessibilityReader_.decorate(this.document_);
rginda8ba33642011-12-14 12:31:31 -08001559
Evan Jones5f9df812016-12-06 09:38:58 -05001560 this.document_.body.oncontextmenu = function() { return false; };
Mike Frysingercc114512017-09-11 21:39:17 -04001561 this.contextMenu.setDocument(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07001562
1563 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001564 var screenNode = this.scrollPort_.getScreenNode();
Joel Hockeyd4fca732019-09-20 16:57:03 -07001565 screenNode.addEventListener(
1566 'mousedown', /** @type {!EventListener} */ (onMouse));
1567 screenNode.addEventListener(
1568 'mouseup', /** @type {!EventListener} */ (onMouse));
1569 screenNode.addEventListener(
1570 'mousemove', /** @type {!EventListener} */ (onMouse));
rginda4bba5e12012-06-20 16:15:30 -07001571 this.scrollPort_.onScrollWheel = onMouse;
1572
Joel Hockeyd4fca732019-09-20 16:57:03 -07001573 screenNode.addEventListener(
1574 'keydown',
1575 /** @type {!EventListener} */ (this.onKeyboardActivity_.bind(this)));
Mike Frysinger02ded6d2018-06-21 14:25:20 -04001576
Toni Barzic0bfa8922013-11-22 11:18:35 -08001577 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001578 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001579 // Listen for mousedown events on the screenNode as in FF the focus
1580 // events don't bubble.
1581 screenNode.addEventListener('mousedown', function() {
1582 setTimeout(this.onFocusChange_.bind(this, true));
1583 }.bind(this));
1584
Toni Barzic0bfa8922013-11-22 11:18:35 -08001585 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001586 'blur', this.onFocusChange_.bind(this, false));
1587
1588 var style = this.document_.createElement('style');
Joel Hockeyd36efd62019-09-30 14:16:20 -07001589 style.textContent = `
1590.cursor-node[focus="false"] {
1591 box-sizing: border-box;
1592 background-color: transparent !important;
1593 border-width: 2px;
1594 border-style: solid;
1595}
1596menu {
1597 margin: 0;
1598 padding: 0;
1599 cursor: var(--hterm-mouse-cursor-pointer);
1600}
1601menuitem {
1602 white-space: nowrap;
1603 border-bottom: 1px dashed;
1604 display: block;
1605 padding: 0.3em 0.3em 0 0.3em;
1606}
1607menuitem.separator {
1608 border-bottom: none;
1609 height: 0.5em;
1610 padding: 0;
1611}
1612menuitem:hover {
1613 color: var(--hterm-cursor-color);
1614}
1615.wc-node {
1616 display: inline-block;
1617 text-align: center;
1618 width: calc(var(--hterm-charsize-width) * 2);
1619 line-height: var(--hterm-charsize-height);
1620}
1621:root {
1622 --hterm-charsize-width: ${this.scrollPort_.characterSize.width}px;
1623 --hterm-charsize-height: ${this.scrollPort_.characterSize.height}px;
1624 /* Default position hides the cursor for when the window is initializing. */
1625 --hterm-cursor-offset-col: -1;
1626 --hterm-cursor-offset-row: -1;
1627 --hterm-blink-node-duration: 0.7s;
1628 --hterm-mouse-cursor-default: default;
1629 --hterm-mouse-cursor-text: text;
1630 --hterm-mouse-cursor-pointer: pointer;
1631 --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);
1632}
1633.uri-node:hover {
1634 text-decoration: underline;
1635 cursor: var(--hterm-mouse-cursor-pointer);
1636}
1637@keyframes blink {
1638 from { opacity: 1.0; }
1639 to { opacity: 0.0; }
1640}
1641.blink-node {
1642 animation-name: blink;
1643 animation-duration: var(--hterm-blink-node-duration);
1644 animation-iteration-count: infinite;
1645 animation-timing-function: ease-in-out;
1646 animation-direction: alternate;
1647}`;
Mike Frysingerb74a6472018-06-22 13:37:08 -04001648 // Insert this stock style as the first node so that any user styles will
1649 // override w/out having to use !important everywhere. The rules above mix
1650 // runtime variables with default ones designed to be overridden by the user,
1651 // but we can wait for a concrete case from the users to determine the best
1652 // way to split the sheet up to before & after the user-css settings.
1653 this.document_.head.insertBefore(style, this.document_.head.firstChild);
rginda8e92a692012-05-20 19:37:20 -07001654
rginda8ba33642011-12-14 12:31:31 -08001655 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001656 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001657 this.cursorNode_.className = 'cursor-node';
Joel Hockeyd36efd62019-09-30 14:16:20 -07001658 this.cursorNode_.style.cssText = `
1659position: absolute;
1660left: calc(var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));
1661top: calc(var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));
1662display: ${this.options_.cursorVisible ? '' : 'none'};
1663width: var(--hterm-charsize-width);
1664height: var(--hterm-charsize-height);
1665background-color: var(--hterm-cursor-color);
1666border-color: var(--hterm-cursor-color);
1667-webkit-transition: opacity, background-color 100ms linear;
1668-moz-transition: opacity, background-color 100ms linear;`;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001669
Mike Frysingerf02a2cb2017-12-21 00:34:03 -05001670 this.setCursorColor();
Robert Gindafb1be6a2013-12-11 11:56:22 -08001671 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1672 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001673
rginda8ba33642011-12-14 12:31:31 -08001674 this.document_.body.appendChild(this.cursorNode_);
1675
rgindad5613292012-06-19 15:40:37 -07001676 // When 'enableMouseDragScroll' is off we reposition this element directly
1677 // under the mouse cursor after a click. This makes Chrome associate
1678 // subsequent mousemove events with the scroll-blocker. Since the
1679 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1680 // events do not cause the scrollport to scroll.
1681 //
1682 // It's a hack, but it's the cleanest way I could find.
1683 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001684 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
Raymes Khoury6dce2f82018-04-12 15:38:58 +10001685 this.scrollBlockerNode_.setAttribute('aria-hidden', 'true');
rgindad5613292012-06-19 15:40:37 -07001686 this.scrollBlockerNode_.style.cssText =
1687 ('position: absolute;' +
1688 'top: -99px;' +
1689 'display: block;' +
1690 'width: 10px;' +
1691 'height: 10px;');
1692 this.document_.body.appendChild(this.scrollBlockerNode_);
1693
rgindad5613292012-06-19 15:40:37 -07001694 this.scrollPort_.onScrollWheel = onMouse;
1695 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1696 ].forEach(function(event) {
1697 this.scrollBlockerNode_.addEventListener(event, onMouse);
Joel Hockeyd4fca732019-09-20 16:57:03 -07001698 this.cursorNode_.addEventListener(
1699 event, /** @type {!EventListener} */ (onMouse));
1700 this.document_.addEventListener(
1701 event, /** @type {!EventListener} */ (onMouse));
rgindad5613292012-06-19 15:40:37 -07001702 }.bind(this));
1703
1704 this.cursorNode_.addEventListener('mousedown', function() {
1705 setTimeout(this.focus.bind(this));
1706 }.bind(this));
1707
rginda8ba33642011-12-14 12:31:31 -08001708 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001709
rginda87b86462011-12-14 13:48:03 -08001710 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001711 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001712};
1713
rginda0918b652012-04-04 11:26:24 -07001714/**
1715 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001716 *
Joel Hockey0f933582019-08-27 18:01:51 -07001717 * @return {!Document}
rginda0918b652012-04-04 11:26:24 -07001718 */
rginda87b86462011-12-14 13:48:03 -08001719hterm.Terminal.prototype.getDocument = function() {
1720 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001721};
1722
1723/**
rginda0918b652012-04-04 11:26:24 -07001724 * Focus the terminal.
1725 */
1726hterm.Terminal.prototype.focus = function() {
1727 this.scrollPort_.focus();
1728};
1729
1730/**
Theodore Duboiscea9b782019-09-02 17:48:00 -07001731 * Unfocus the terminal.
1732 */
1733hterm.Terminal.prototype.blur = function() {
1734 this.scrollPort_.blur();
1735};
1736
1737/**
rginda8ba33642011-12-14 12:31:31 -08001738 * Return the HTML Element for a given row index.
1739 *
1740 * This is a method from the RowProvider interface. The ScrollPort uses
1741 * it to fetch rows on demand as they are scrolled into view.
1742 *
1743 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1744 * pairs to conserve memory.
1745 *
Joel Hockey0f933582019-08-27 18:01:51 -07001746 * @param {number} index The zero-based row index, measured relative to the
rginda8ba33642011-12-14 12:31:31 -08001747 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001748 * largest indices.
Joel Hockey0f933582019-08-27 18:01:51 -07001749 * @return {!Element} The 'x-row' element containing for the requested row.
Joel Hockeyd4fca732019-09-20 16:57:03 -07001750 * @override
rginda8ba33642011-12-14 12:31:31 -08001751 */
1752hterm.Terminal.prototype.getRowNode = function(index) {
1753 if (index < this.scrollbackRows_.length)
1754 return this.scrollbackRows_[index];
1755
1756 var screenIndex = index - this.scrollbackRows_.length;
1757 return this.screen_.rowsArray[screenIndex];
1758};
1759
1760/**
1761 * Return the text content for a given range of rows.
1762 *
1763 * This is a method from the RowProvider interface. The ScrollPort uses
1764 * it to fetch text content on demand when the user attempts to copy their
1765 * selection to the clipboard.
1766 *
Joel Hockey0f933582019-08-27 18:01:51 -07001767 * @param {number} start The zero-based row index to start from, measured
rginda8ba33642011-12-14 12:31:31 -08001768 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001769 * always have the largest indices.
Joel Hockey0f933582019-08-27 18:01:51 -07001770 * @param {number} end The zero-based row index to end on, measured
rginda8ba33642011-12-14 12:31:31 -08001771 * relative to the start of the scrollback buffer.
1772 * @return {string} A single string containing the text value of the range of
1773 * rows. Lines will be newline delimited, with no trailing newline.
1774 */
1775hterm.Terminal.prototype.getRowsText = function(start, end) {
1776 var ary = [];
1777 for (var i = start; i < end; i++) {
1778 var node = this.getRowNode(i);
1779 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001780 if (i < end - 1 && !node.getAttribute('line-overflow'))
1781 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001782 }
1783
rgindaa09e7332012-08-17 12:49:51 -07001784 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001785};
1786
1787/**
1788 * Return the text content for a given row.
1789 *
1790 * This is a method from the RowProvider interface. The ScrollPort uses
1791 * it to fetch text content on demand when the user attempts to copy their
1792 * selection to the clipboard.
1793 *
Joel Hockey0f933582019-08-27 18:01:51 -07001794 * @param {number} index The zero-based row index to return, measured
rginda8ba33642011-12-14 12:31:31 -08001795 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001796 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001797 * @return {string} A string containing the text value of the selected row.
1798 */
1799hterm.Terminal.prototype.getRowText = function(index) {
1800 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001801 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001802};
1803
1804/**
1805 * Return the total number of rows in the addressable screen and in the
1806 * scrollback buffer of this terminal.
1807 *
1808 * This is a method from the RowProvider interface. The ScrollPort uses
1809 * it to compute the size of the scrollbar.
1810 *
Joel Hockey0f933582019-08-27 18:01:51 -07001811 * @return {number} The number of rows in this terminal.
Joel Hockeyd4fca732019-09-20 16:57:03 -07001812 * @override
rginda8ba33642011-12-14 12:31:31 -08001813 */
1814hterm.Terminal.prototype.getRowCount = function() {
1815 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1816};
1817
1818/**
1819 * Create DOM nodes for new rows and append them to the end of the terminal.
1820 *
1821 * This is the only correct way to add a new DOM node for a row. Notice that
1822 * the new row is appended to the bottom of the list of rows, and does not
1823 * require renumbering (of the rowIndex property) of previous rows.
1824 *
1825 * If you think you want a new blank row somewhere in the middle of the
1826 * terminal, look into moveRows_().
1827 *
1828 * This method does not pay attention to vtScrollTop/Bottom, since you should
1829 * be using moveRows() in cases where they would matter.
1830 *
1831 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001832 *
1833 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001834 */
1835hterm.Terminal.prototype.appendRows_ = function(count) {
1836 var cursorRow = this.screen_.rowsArray.length;
1837 var offset = this.scrollbackRows_.length + cursorRow;
1838 for (var i = 0; i < count; i++) {
1839 var row = this.document_.createElement('x-row');
1840 row.appendChild(this.document_.createTextNode(''));
1841 row.rowIndex = offset + i;
1842 this.screen_.pushRow(row);
1843 }
1844
1845 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1846 if (extraRows > 0) {
1847 var ary = this.screen_.shiftRows(extraRows);
1848 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001849 if (this.scrollPort_.isScrolledEnd)
1850 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001851 }
1852
1853 if (cursorRow >= this.screen_.rowsArray.length)
1854 cursorRow = this.screen_.rowsArray.length - 1;
1855
rginda87b86462011-12-14 13:48:03 -08001856 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001857};
1858
1859/**
1860 * Relocate rows from one part of the addressable screen to another.
1861 *
1862 * This is used to recycle rows during VT scrolls (those which are driven
1863 * by VT commands, rather than by the user manipulating the scrollbar.)
1864 *
1865 * In this case, the blank lines scrolled into the scroll region are made of
1866 * the nodes we scrolled off. These have their rowIndex properties carefully
1867 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001868 *
1869 * @param {number} fromIndex The start index.
1870 * @param {number} count The number of rows to move.
1871 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001872 */
1873hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1874 var ary = this.screen_.removeRows(fromIndex, count);
1875 this.screen_.insertRows(toIndex, ary);
1876
1877 var start, end;
1878 if (fromIndex < toIndex) {
1879 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001880 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001881 } else {
1882 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001883 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001884 }
1885
1886 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001887 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001888};
1889
1890/**
1891 * Renumber the rowIndex property of the given range of rows.
1892 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001893 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08001894 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001895 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001896 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05001897 *
1898 * @param {number} start The start index.
1899 * @param {number} end The end index.
Joel Hockey0f933582019-08-27 18:01:51 -07001900 * @param {!hterm.Screen=} opt_screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08001901 */
Robert Ginda40932892012-12-10 17:26:40 -08001902hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1903 var screen = opt_screen || this.screen_;
1904
rginda8ba33642011-12-14 12:31:31 -08001905 var offset = this.scrollbackRows_.length;
1906 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001907 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001908 }
1909};
1910
1911/**
1912 * Print a string to the terminal.
1913 *
1914 * This respects the current insert and wraparound modes. It will add new lines
1915 * to the end of the terminal, scrolling off the top into the scrollback buffer
1916 * if necessary.
1917 *
1918 * The string is *not* parsed for escape codes. Use the interpret() method if
1919 * that's what you're after.
1920 *
Mike Frysingerfd449572019-09-23 03:18:14 -04001921 * @param {string} str The string to print.
rginda8ba33642011-12-14 12:31:31 -08001922 */
1923hterm.Terminal.prototype.print = function(str) {
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001924 this.scheduleSyncCursorPosition_();
1925
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001926 // Basic accessibility output for the screen reader.
Raymes Khoury177aec72018-06-26 10:58:53 +10001927 this.accessibilityReader_.announce(str);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001928
rgindaa9abdd82012-08-06 18:05:09 -07001929 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001930
Ricky Liang48f05cb2013-12-31 23:35:29 +08001931 var strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04001932 // Fun edge case: If the string only contains zero width codepoints (like
1933 // combining characters), we make sure to iterate at least once below.
1934 if (strWidth == 0 && str)
1935 strWidth = 1;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001936
1937 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001938 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1939 this.screen_.commitLineOverflow();
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001940 this.newLine(true);
rgindaa09e7332012-08-17 12:49:51 -07001941 }
rgindaa19afe22012-01-25 15:40:22 -08001942
Ricky Liang48f05cb2013-12-31 23:35:29 +08001943 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001944 var didOverflow = false;
1945 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001946
rgindaa9abdd82012-08-06 18:05:09 -07001947 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1948 didOverflow = true;
1949 count = this.screenSize.width - this.screen_.cursorPosition.column;
1950 }
rgindaa19afe22012-01-25 15:40:22 -08001951
rgindaa9abdd82012-08-06 18:05:09 -07001952 if (didOverflow && !this.options_.wraparound) {
1953 // If the string overflowed the line but wraparound is off, then the
1954 // last printed character should be the last of the string.
1955 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001956 substr = lib.wc.substr(str, startOffset, count - 1) +
1957 lib.wc.substr(str, strWidth - 1);
1958 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001959 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001960 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001961 }
rgindaa19afe22012-01-25 15:40:22 -08001962
Ricky Liang48f05cb2013-12-31 23:35:29 +08001963 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1964 for (var i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001965 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
1966 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001967
1968 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001969 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001970 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001971 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001972 }
1973 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001974 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07001975 }
1976
1977 this.screen_.maybeClipCurrentRow();
1978 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001979 }
rginda8ba33642011-12-14 12:31:31 -08001980
rginda9f5222b2012-03-05 11:53:28 -08001981 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001982 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001983};
1984
1985/**
rginda87b86462011-12-14 13:48:03 -08001986 * Set the VT scroll region.
1987 *
rginda87b86462011-12-14 13:48:03 -08001988 * This also resets the cursor position to the absolute (0, 0) position, since
1989 * that's what xterm appears to do.
1990 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001991 * Setting the scroll region to the full height of the terminal will clear
1992 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1993 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1994 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1995 * continue to work as most users would expect.
1996 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07001997 * @param {?number} scrollTop The zero-based top of the scroll region.
1998 * @param {?number} scrollBottom The zero-based bottom of the scroll region,
rginda87b86462011-12-14 13:48:03 -08001999 * inclusive.
2000 */
2001hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07002002 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08002003 this.vtScrollTop_ = null;
2004 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07002005 } else {
2006 this.vtScrollTop_ = scrollTop;
2007 this.vtScrollBottom_ = scrollBottom;
2008 }
rginda87b86462011-12-14 13:48:03 -08002009};
2010
2011/**
rginda8ba33642011-12-14 12:31:31 -08002012 * Return the top row index according to the VT.
2013 *
2014 * This will return 0 unless the terminal has been told to restrict scrolling
2015 * to some lower row. It is used for some VT cursor positioning and scrolling
2016 * commands.
2017 *
Joel Hockey0f933582019-08-27 18:01:51 -07002018 * @return {number} The topmost row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08002019 */
2020hterm.Terminal.prototype.getVTScrollTop = function() {
2021 if (this.vtScrollTop_ != null)
2022 return this.vtScrollTop_;
2023
2024 return 0;
rginda87b86462011-12-14 13:48:03 -08002025};
rginda8ba33642011-12-14 12:31:31 -08002026
2027/**
2028 * Return the bottom row index according to the VT.
2029 *
2030 * This will return the height of the terminal unless the it has been told to
2031 * restrict scrolling to some higher row. It is used for some VT cursor
2032 * positioning and scrolling commands.
2033 *
Joel Hockey0f933582019-08-27 18:01:51 -07002034 * @return {number} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08002035 */
2036hterm.Terminal.prototype.getVTScrollBottom = function() {
2037 if (this.vtScrollBottom_ != null)
2038 return this.vtScrollBottom_;
2039
rginda87b86462011-12-14 13:48:03 -08002040 return this.screenSize.height - 1;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04002041};
rginda8ba33642011-12-14 12:31:31 -08002042
2043/**
2044 * Process a '\n' character.
2045 *
2046 * If the cursor is on the final row of the terminal this will append a new
2047 * blank row to the screen and scroll the topmost row into the scrollback
2048 * buffer.
2049 *
2050 * Otherwise, this moves the cursor to column zero of the next row.
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002051 *
2052 * @param {boolean=} dueToOverflow Whether the newline is due to wraparound of
2053 * the terminal.
rginda8ba33642011-12-14 12:31:31 -08002054 */
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10002055hterm.Terminal.prototype.newLine = function(dueToOverflow = false) {
2056 if (!dueToOverflow)
2057 this.accessibilityReader_.newLine();
2058
Robert Ginda9937abc2013-07-25 16:09:23 -07002059 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
2060 this.screen_.rowsArray.length - 1);
2061
2062 if (this.vtScrollBottom_ != null) {
2063 // A VT Scroll region is active, we never append new rows.
2064 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
2065 // We're at the end of the VT Scroll Region, perform a VT scroll.
2066 this.vtScrollUp(1);
2067 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
2068 } else if (cursorAtEndOfScreen) {
2069 // We're at the end of the screen, the only thing to do is put the
2070 // cursor to column 0.
2071 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
2072 } else {
2073 // Anywhere else, advance the cursor row, and reset the column.
2074 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
2075 }
2076 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07002077 // We're at the end of the screen. Append a new row to the terminal,
2078 // shifting the top row into the scrollback.
2079 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08002080 } else {
rginda87b86462011-12-14 13:48:03 -08002081 // Anywhere else in the screen just moves the cursor.
2082 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08002083 }
2084};
2085
2086/**
2087 * Like newLine(), except maintain the cursor column.
2088 */
2089hterm.Terminal.prototype.lineFeed = function() {
2090 var column = this.screen_.cursorPosition.column;
2091 this.newLine();
2092 this.setCursorColumn(column);
2093};
2094
2095/**
rginda87b86462011-12-14 13:48:03 -08002096 * If autoCarriageReturn is set then newLine(), else lineFeed().
2097 */
2098hterm.Terminal.prototype.formFeed = function() {
2099 if (this.options_.autoCarriageReturn) {
2100 this.newLine();
2101 } else {
2102 this.lineFeed();
2103 }
2104};
2105
2106/**
2107 * Move the cursor up one row, possibly inserting a blank line.
2108 *
2109 * The cursor column is not changed.
2110 */
2111hterm.Terminal.prototype.reverseLineFeed = function() {
2112 var scrollTop = this.getVTScrollTop();
2113 var currentRow = this.screen_.cursorPosition.row;
2114
2115 if (currentRow == scrollTop) {
2116 this.insertLines(1);
2117 } else {
2118 this.setAbsoluteCursorRow(currentRow - 1);
2119 }
2120};
2121
2122/**
rginda8ba33642011-12-14 12:31:31 -08002123 * Replace all characters to the left of the current cursor with the space
2124 * character.
2125 *
2126 * TODO(rginda): This should probably *remove* the characters (not just replace
2127 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07002128 * position.
rginda8ba33642011-12-14 12:31:31 -08002129 */
2130hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08002131 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002132 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002133 const count = cursor.column + 1;
Mike Frysinger73e56462019-07-17 00:23:46 -05002134 this.screen_.overwriteString(' '.repeat(count), count);
rginda87b86462011-12-14 13:48:03 -08002135 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002136};
2137
2138/**
David Benjamin684a9b72012-05-01 17:19:58 -04002139 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08002140 *
2141 * The cursor position is unchanged.
2142 *
Robert Gindaf2547f12012-10-25 20:36:21 -07002143 * If the current background color is not the default background color this
2144 * will insert spaces rather than delete. This is unfortunate because the
2145 * trailing space will affect text selection, but it's difficult to come up
2146 * with a way to style empty space that wouldn't trip up the hterm.Screen
2147 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07002148 *
2149 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
2150 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
2151 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05002152 *
Joel Hockey0f933582019-08-27 18:01:51 -07002153 * @param {number=} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08002154 */
2155hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07002156 if (this.screen_.cursorPosition.overflow)
2157 return;
2158
Robert Ginda7fd57082012-09-25 14:41:47 -07002159 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
2160 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07002161
2162 if (this.screen_.textAttributes.background ===
2163 this.screen_.textAttributes.DEFAULT_COLOR) {
2164 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08002165 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07002166 this.screen_.cursorPosition.column + count) {
2167 this.screen_.deleteChars(count);
2168 this.clearCursorOverflow();
2169 return;
2170 }
2171 }
2172
rginda87b86462011-12-14 13:48:03 -08002173 var cursor = this.saveCursor();
Mike Frysinger73e56462019-07-17 00:23:46 -05002174 this.screen_.overwriteString(' '.repeat(count), count);
rginda87b86462011-12-14 13:48:03 -08002175 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002176 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002177};
2178
2179/**
2180 * Erase the current line.
2181 *
2182 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002183 */
2184hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08002185 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002186 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08002187 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002188 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002189};
2190
2191/**
David Benjamina08d78f2012-05-05 00:28:49 -04002192 * Erase all characters from the start of the screen to the current cursor
2193 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002194 *
2195 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002196 */
2197hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08002198 var cursor = this.saveCursor();
2199
2200 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002201
David Benjamina08d78f2012-05-05 00:28:49 -04002202 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002203 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002204 this.screen_.clearCursorRow();
2205 }
2206
rginda87b86462011-12-14 13:48:03 -08002207 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002208 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002209};
2210
2211/**
2212 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002213 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002214 *
2215 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002216 */
2217hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08002218 var cursor = this.saveCursor();
2219
2220 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002221
David Benjamina08d78f2012-05-05 00:28:49 -04002222 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08002223 for (var i = cursor.row + 1; i <= bottom; i++) {
2224 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002225 this.screen_.clearCursorRow();
2226 }
2227
rginda87b86462011-12-14 13:48:03 -08002228 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002229 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002230};
2231
2232/**
2233 * Fill the terminal with a given character.
2234 *
2235 * This methods does not respect the VT scroll region.
2236 *
2237 * @param {string} ch The character to use for the fill.
2238 */
2239hterm.Terminal.prototype.fill = function(ch) {
2240 var cursor = this.saveCursor();
2241
2242 this.setAbsoluteCursorPosition(0, 0);
2243 for (var row = 0; row < this.screenSize.height; row++) {
2244 for (var col = 0; col < this.screenSize.width; col++) {
2245 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002246 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002247 }
2248 }
2249
2250 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002251};
2252
2253/**
rginda9ea433c2012-03-16 11:57:00 -07002254 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002255 *
rginda9ea433c2012-03-16 11:57:00 -07002256 * This does not respect the scroll region.
2257 *
Joel Hockey0f933582019-08-27 18:01:51 -07002258 * @param {!hterm.Screen=} opt_screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002259 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002260 */
rginda9ea433c2012-03-16 11:57:00 -07002261hterm.Terminal.prototype.clearHome = function(opt_screen) {
2262 var screen = opt_screen || this.screen_;
2263 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002264
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002265 this.accessibilityReader_.clear();
2266
rginda11057d52012-04-25 12:29:56 -07002267 if (bottom == 0) {
2268 // Empty screen, nothing to do.
2269 return;
2270 }
2271
rgindae4d29232012-01-19 10:47:13 -08002272 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002273 screen.setCursorPosition(i, 0);
2274 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002275 }
2276
rginda9ea433c2012-03-16 11:57:00 -07002277 screen.setCursorPosition(0, 0);
2278};
2279
2280/**
2281 * Erase the entire display without changing the cursor position.
2282 *
2283 * The cursor position is unchanged. This does not respect the scroll
2284 * region.
2285 *
Joel Hockey0f933582019-08-27 18:01:51 -07002286 * @param {!hterm.Screen=} opt_screen Optional screen to operate on. Defaults
rginda9ea433c2012-03-16 11:57:00 -07002287 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002288 */
2289hterm.Terminal.prototype.clear = function(opt_screen) {
2290 var screen = opt_screen || this.screen_;
2291 var cursor = screen.cursorPosition.clone();
2292 this.clearHome(screen);
2293 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002294};
2295
2296/**
2297 * VT command to insert lines at the current cursor row.
2298 *
2299 * This respects the current scroll region. Rows pushed off the bottom are
2300 * lost (they won't show up in the scrollback buffer).
2301 *
Joel Hockey0f933582019-08-27 18:01:51 -07002302 * @param {number} count The number of lines to insert.
rginda8ba33642011-12-14 12:31:31 -08002303 */
2304hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07002305 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002306
2307 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002308 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002309
Robert Ginda579186b2012-09-26 11:40:04 -07002310 // The moveCount is the number of rows we need to relocate to make room for
2311 // the new row(s). The count is the distance to move them.
2312 var moveCount = bottom - cursorRow - count + 1;
2313 if (moveCount)
2314 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08002315
Robert Ginda579186b2012-09-26 11:40:04 -07002316 for (var i = count - 1; i >= 0; i--) {
2317 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002318 this.screen_.clearCursorRow();
2319 }
rginda8ba33642011-12-14 12:31:31 -08002320};
2321
2322/**
2323 * VT command to delete lines at the current cursor row.
2324 *
2325 * New rows are added to the bottom of scroll region to take their place. New
2326 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002327 *
2328 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002329 */
2330hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08002331 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002332
rginda87b86462011-12-14 13:48:03 -08002333 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08002334 var bottom = this.getVTScrollBottom();
2335
rginda87b86462011-12-14 13:48:03 -08002336 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002337 count = Math.min(count, maxCount);
2338
rginda87b86462011-12-14 13:48:03 -08002339 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08002340 if (count != maxCount)
2341 this.moveRows_(top, count, moveStart);
2342
2343 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002344 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002345 this.screen_.clearCursorRow();
2346 }
2347
rginda87b86462011-12-14 13:48:03 -08002348 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002349 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002350};
2351
2352/**
2353 * Inserts the given number of spaces at the current cursor position.
2354 *
rginda87b86462011-12-14 13:48:03 -08002355 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002356 *
2357 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002358 */
2359hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08002360 var cursor = this.saveCursor();
2361
Mike Frysinger73e56462019-07-17 00:23:46 -05002362 const ws = ' '.repeat(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002363 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002364 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002365
2366 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002367 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002368};
2369
2370/**
2371 * Forward-delete the specified number of characters starting at the cursor
2372 * position.
2373 *
Joel Hockey0f933582019-08-27 18:01:51 -07002374 * @param {number} count The number of characters to delete.
rginda8ba33642011-12-14 12:31:31 -08002375 */
2376hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002377 var deleted = this.screen_.deleteChars(count);
2378 if (deleted && !this.screen_.textAttributes.isDefault()) {
2379 var cursor = this.saveCursor();
2380 this.setCursorColumn(this.screenSize.width - deleted);
Mike Frysinger73e56462019-07-17 00:23:46 -05002381 this.screen_.insertString(' '.repeat(deleted));
Robert Ginda7fd57082012-09-25 14:41:47 -07002382 this.restoreCursor(cursor);
2383 }
2384
David Benjamin54e8bf62012-06-01 22:31:40 -04002385 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002386};
2387
2388/**
2389 * Shift rows in the scroll region upwards by a given number of lines.
2390 *
2391 * New rows are inserted at the bottom of the scroll region to fill the
2392 * vacated rows. The new rows not filled out with the current text attributes.
2393 *
2394 * This function does not affect the scrollback rows at all. Rows shifted
2395 * off the top are lost.
2396 *
rginda87b86462011-12-14 13:48:03 -08002397 * The cursor position is not altered.
2398 *
Joel Hockey0f933582019-08-27 18:01:51 -07002399 * @param {number} count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002400 */
2401hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002402 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002403
rginda87b86462011-12-14 13:48:03 -08002404 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002405 this.deleteLines(count);
2406
rginda87b86462011-12-14 13:48:03 -08002407 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002408};
2409
2410/**
2411 * Shift rows below the cursor down by a given number of lines.
2412 *
2413 * This function respects the current scroll region.
2414 *
2415 * New rows are inserted at the top of the scroll region to fill the
2416 * vacated rows. The new rows not filled out with the current text attributes.
2417 *
2418 * This function does not affect the scrollback rows at all. Rows shifted
2419 * off the bottom are lost.
2420 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07002421 * @param {number} count The number of rows to scroll.
rginda8ba33642011-12-14 12:31:31 -08002422 */
Joel Hockeyd4fca732019-09-20 16:57:03 -07002423hterm.Terminal.prototype.vtScrollDown = function(count) {
rginda87b86462011-12-14 13:48:03 -08002424 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002425
rginda87b86462011-12-14 13:48:03 -08002426 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
Joel Hockeyd4fca732019-09-20 16:57:03 -07002427 this.insertLines(count);
rginda8ba33642011-12-14 12:31:31 -08002428
rginda87b86462011-12-14 13:48:03 -08002429 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002430};
2431
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002432/**
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002433 * Enable accessibility-friendly features that have a performance impact.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002434 *
2435 * This will generate additional DOM nodes in an aria-live region that will
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002436 * cause Assitive Technology to announce the output of the terminal. It also
2437 * enables other features that aid assistive technology. All the features gated
2438 * behind this flag have a performance impact on the terminal which is why they
2439 * are made optional.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002440 *
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002441 * @param {boolean} enabled Whether to enable accessibility-friendly features.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002442 */
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002443hterm.Terminal.prototype.setAccessibilityEnabled = function(enabled) {
Raymes Khoury177aec72018-06-26 10:58:53 +10002444 this.accessibilityReader_.setAccessibilityEnabled(enabled);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002445};
rginda87b86462011-12-14 13:48:03 -08002446
rginda8ba33642011-12-14 12:31:31 -08002447/**
2448 * Set the cursor position.
2449 *
2450 * The cursor row is relative to the scroll region if the terminal has
2451 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2452 *
Joel Hockey0f933582019-08-27 18:01:51 -07002453 * @param {number} row The new zero-based cursor row.
2454 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002455 */
2456hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2457 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002458 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002459 } else {
rginda87b86462011-12-14 13:48:03 -08002460 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002461 }
rginda87b86462011-12-14 13:48:03 -08002462};
rginda8ba33642011-12-14 12:31:31 -08002463
Evan Jones2600d4f2016-12-06 09:29:36 -05002464/**
2465 * Move the cursor relative to its current position.
2466 *
2467 * @param {number} row
2468 * @param {number} column
2469 */
rginda87b86462011-12-14 13:48:03 -08002470hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2471 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002472 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2473 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002474 this.screen_.setCursorPosition(row, column);
2475};
2476
Evan Jones2600d4f2016-12-06 09:29:36 -05002477/**
2478 * Move the cursor to the specified position.
2479 *
2480 * @param {number} row
2481 * @param {number} column
2482 */
rginda87b86462011-12-14 13:48:03 -08002483hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002484 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2485 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002486 this.screen_.setCursorPosition(row, column);
2487};
2488
2489/**
2490 * Set the cursor column.
2491 *
Joel Hockey0f933582019-08-27 18:01:51 -07002492 * @param {number} column The new zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002493 */
2494hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002495 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002496};
2497
2498/**
2499 * Return the cursor column.
2500 *
Joel Hockey0f933582019-08-27 18:01:51 -07002501 * @return {number} The zero-based cursor column.
rginda8ba33642011-12-14 12:31:31 -08002502 */
2503hterm.Terminal.prototype.getCursorColumn = function() {
2504 return this.screen_.cursorPosition.column;
2505};
2506
2507/**
2508 * Set the cursor row.
2509 *
2510 * The cursor row is relative to the scroll region if the terminal has
2511 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2512 *
Joel Hockey0f933582019-08-27 18:01:51 -07002513 * @param {number} row The new cursor row.
rginda8ba33642011-12-14 12:31:31 -08002514 */
rginda87b86462011-12-14 13:48:03 -08002515hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2516 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002517};
2518
2519/**
2520 * Return the cursor row.
2521 *
Joel Hockey0f933582019-08-27 18:01:51 -07002522 * @return {number} The zero-based cursor row.
rginda8ba33642011-12-14 12:31:31 -08002523 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002524hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002525 return this.screen_.cursorPosition.row;
2526};
2527
2528/**
2529 * Request that the ScrollPort redraw itself soon.
2530 *
2531 * The redraw will happen asynchronously, soon after the call stack winds down.
2532 * Multiple calls will be coalesced into a single redraw.
2533 */
2534hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002535 if (this.timeouts_.redraw)
2536 return;
rginda8ba33642011-12-14 12:31:31 -08002537
2538 var self = this;
rginda87b86462011-12-14 13:48:03 -08002539 this.timeouts_.redraw = setTimeout(function() {
2540 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002541 self.scrollPort_.redraw_();
2542 }, 0);
2543};
2544
2545/**
2546 * Request that the ScrollPort be scrolled to the bottom.
2547 *
2548 * The scroll will happen asynchronously, soon after the call stack winds down.
2549 * Multiple calls will be coalesced into a single scroll.
2550 *
2551 * This affects the scrollbar position of the ScrollPort, and has nothing to
2552 * do with the VT scroll commands.
2553 */
2554hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2555 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002556 return;
rginda8ba33642011-12-14 12:31:31 -08002557
2558 var self = this;
2559 this.timeouts_.scrollDown = setTimeout(function() {
2560 delete self.timeouts_.scrollDown;
2561 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2562 }, 10);
2563};
2564
2565/**
2566 * Move the cursor up a specified number of rows.
2567 *
Joel Hockey0f933582019-08-27 18:01:51 -07002568 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002569 */
2570hterm.Terminal.prototype.cursorUp = function(count) {
Joel Hockey0f933582019-08-27 18:01:51 -07002571 this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002572};
2573
2574/**
2575 * Move the cursor down a specified number of rows.
2576 *
Joel Hockey0f933582019-08-27 18:01:51 -07002577 * @param {number} count The number of rows to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002578 */
2579hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002580 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002581 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2582 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2583 this.screenSize.height - 1);
2584
rgindacbbd7482012-06-13 15:06:16 -07002585 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002586 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002587 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002588};
2589
2590/**
2591 * Move the cursor left a specified number of columns.
2592 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002593 * If reverse wraparound mode is enabled and the previous row wrapped into
2594 * the current row then we back up through the wraparound as well.
2595 *
Joel Hockey0f933582019-08-27 18:01:51 -07002596 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002597 */
2598hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002599 count = count || 1;
2600
2601 if (count < 1)
2602 return;
2603
2604 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002605 if (this.options_.reverseWraparound) {
2606 if (this.screen_.cursorPosition.overflow) {
2607 // If this cursor is in the right margin, consume one count to get it
2608 // back to the last column. This only applies when we're in reverse
2609 // wraparound mode.
2610 count--;
2611 this.clearCursorOverflow();
2612
2613 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002614 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002615 }
2616
Robert Gindabfb32622014-07-17 13:20:27 -07002617 var newRow = this.screen_.cursorPosition.row;
2618 var newColumn = currentColumn - count;
2619 if (newColumn < 0) {
2620 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2621 if (newRow < 0) {
2622 // xterm also wraps from row 0 to the last row.
2623 newRow = this.screenSize.height + newRow % this.screenSize.height;
2624 }
2625 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2626 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002627
Robert Gindabfb32622014-07-17 13:20:27 -07002628 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2629
2630 } else {
2631 var newColumn = Math.max(currentColumn - count, 0);
2632 this.setCursorColumn(newColumn);
2633 }
rginda8ba33642011-12-14 12:31:31 -08002634};
2635
2636/**
2637 * Move the cursor right a specified number of columns.
2638 *
Joel Hockey0f933582019-08-27 18:01:51 -07002639 * @param {number} count The number of columns to move the cursor.
rginda8ba33642011-12-14 12:31:31 -08002640 */
2641hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002642 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002643
2644 if (count < 1)
2645 return;
2646
rgindacbbd7482012-06-13 15:06:16 -07002647 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002648 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002649 this.setCursorColumn(column);
2650};
2651
2652/**
2653 * Reverse the foreground and background colors of the terminal.
2654 *
2655 * This only affects text that was drawn with no attributes.
2656 *
2657 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2658 * been drawn with attributes that happen to coincide with the default
2659 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002660 *
2661 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002662 */
2663hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002664 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002665 if (state) {
Mike Frysinger31cb1562017-07-31 23:44:18 -04002666 this.scrollPort_.setForegroundColor(this.backgroundColor_);
2667 this.scrollPort_.setBackgroundColor(this.foregroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002668 } else {
Mike Frysinger31cb1562017-07-31 23:44:18 -04002669 this.scrollPort_.setForegroundColor(this.foregroundColor_);
2670 this.scrollPort_.setBackgroundColor(this.backgroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002671 }
2672};
2673
2674/**
rginda87b86462011-12-14 13:48:03 -08002675 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002676 *
2677 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002678 */
2679hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002680 this.cursorNode_.style.backgroundColor =
2681 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002682
2683 var self = this;
2684 setTimeout(function() {
Matheus Fernandes2d733082017-09-11 06:43:01 -04002685 self.restyleCursor_();
rginda6d397402012-01-17 10:58:29 -08002686 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002687
Michael Kelly485ecd12014-06-09 11:41:56 -04002688 // bellSquelchTimeout_ affects both audio and notification bells.
2689 if (this.bellSquelchTimeout_)
2690 return;
2691
Robert Ginda92e18102013-03-14 13:56:37 -07002692 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002693 this.bellAudio_.play();
Joel Hockeyd4fca732019-09-20 16:57:03 -07002694 this.bellSequelchTimeout_ = setTimeout(() => {
2695 this.bellSquelchTimeout_ = null;
2696 }, 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002697 } else {
Joel Hockeyd4fca732019-09-20 16:57:03 -07002698 this.bellSquelchTimeout_ = null;
Robert Ginda92e18102013-03-14 13:56:37 -07002699 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002700
2701 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingera5fb83c2017-06-22 14:48:35 -07002702 var n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002703 this.bellNotificationList_.push(n);
2704 // TODO: Should we try to raise the window here?
2705 n.onclick = function() { self.closeBellNotifications_(); };
2706 }
rginda87b86462011-12-14 13:48:03 -08002707};
2708
2709/**
rginda8ba33642011-12-14 12:31:31 -08002710 * Set the origin mode bit.
2711 *
2712 * If origin mode is on, certain VT cursor and scrolling commands measure their
2713 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2714 * to the top of the addressable screen.
2715 *
2716 * Defaults to off.
2717 *
2718 * @param {boolean} state True to set origin mode, false to unset.
2719 */
2720hterm.Terminal.prototype.setOriginMode = function(state) {
2721 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002722 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002723};
2724
2725/**
2726 * Set the insert mode bit.
2727 *
2728 * If insert mode is on, existing text beyond the cursor position will be
2729 * shifted right to make room for new text. Otherwise, new text overwrites
2730 * any existing text.
2731 *
2732 * Defaults to off.
2733 *
2734 * @param {boolean} state True to set insert mode, false to unset.
2735 */
2736hterm.Terminal.prototype.setInsertMode = function(state) {
2737 this.options_.insertMode = state;
2738};
2739
2740/**
rginda87b86462011-12-14 13:48:03 -08002741 * Set the auto carriage return bit.
2742 *
2743 * If auto carriage return is on then a formfeed character is interpreted
2744 * as a newline, otherwise it's the same as a linefeed. The difference boils
2745 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002746 *
2747 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002748 */
2749hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2750 this.options_.autoCarriageReturn = state;
2751};
2752
2753/**
rginda8ba33642011-12-14 12:31:31 -08002754 * Set the wraparound mode bit.
2755 *
2756 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2757 * to the start of the following row. Otherwise, the cursor is clamped to the
2758 * end of the screen and attempts to write past it are ignored.
2759 *
2760 * Defaults to on.
2761 *
2762 * @param {boolean} state True to set wraparound mode, false to unset.
2763 */
2764hterm.Terminal.prototype.setWraparound = function(state) {
2765 this.options_.wraparound = state;
2766};
2767
2768/**
2769 * Set the reverse-wraparound mode bit.
2770 *
2771 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2772 * to the end of the previous row. Otherwise, the cursor is clamped to column
2773 * 0.
2774 *
2775 * Defaults to off.
2776 *
2777 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2778 */
2779hterm.Terminal.prototype.setReverseWraparound = function(state) {
2780 this.options_.reverseWraparound = state;
2781};
2782
2783/**
2784 * Selects between the primary and alternate screens.
2785 *
2786 * If alternate mode is on, the alternate screen is active. Otherwise the
2787 * primary screen is active.
2788 *
2789 * Swapping screens has no effect on the scrollback buffer.
2790 *
2791 * Each screen maintains its own cursor position.
2792 *
2793 * Defaults to off.
2794 *
2795 * @param {boolean} state True to set alternate mode, false to unset.
2796 */
2797hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002798 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002799 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2800
rginda35c456b2012-02-09 17:29:05 -08002801 if (this.screen_.rowsArray.length &&
2802 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2803 // If the screen changed sizes while we were away, our rowIndexes may
2804 // be incorrect.
2805 var offset = this.scrollbackRows_.length;
2806 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002807 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002808 ary[i].rowIndex = offset + i;
2809 }
2810 }
rginda8ba33642011-12-14 12:31:31 -08002811
rginda35c456b2012-02-09 17:29:05 -08002812 this.realizeWidth_(this.screenSize.width);
2813 this.realizeHeight_(this.screenSize.height);
2814 this.scrollPort_.syncScrollHeight();
2815 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002816
rginda6d397402012-01-17 10:58:29 -08002817 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002818 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002819};
2820
2821/**
2822 * Set the cursor-blink mode bit.
2823 *
2824 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2825 * a visible cursor does not blink.
2826 *
2827 * You should make sure to turn blinking off if you're going to dispose of a
2828 * terminal, otherwise you'll leak a timeout.
2829 *
2830 * Defaults to on.
2831 *
2832 * @param {boolean} state True to set cursor-blink mode, false to unset.
2833 */
2834hterm.Terminal.prototype.setCursorBlink = function(state) {
2835 this.options_.cursorBlink = state;
2836
2837 if (!state && this.timeouts_.cursorBlink) {
2838 clearTimeout(this.timeouts_.cursorBlink);
2839 delete this.timeouts_.cursorBlink;
2840 }
2841
2842 if (this.options_.cursorVisible)
2843 this.setCursorVisible(true);
2844};
2845
2846/**
2847 * Set the cursor-visible mode bit.
2848 *
2849 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2850 *
2851 * Defaults to on.
2852 *
2853 * @param {boolean} state True to set cursor-visible mode, false to unset.
2854 */
2855hterm.Terminal.prototype.setCursorVisible = function(state) {
2856 this.options_.cursorVisible = state;
2857
2858 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002859 if (this.timeouts_.cursorBlink) {
2860 clearTimeout(this.timeouts_.cursorBlink);
2861 delete this.timeouts_.cursorBlink;
2862 }
rginda87b86462011-12-14 13:48:03 -08002863 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002864 return;
2865 }
2866
rginda87b86462011-12-14 13:48:03 -08002867 this.syncCursorPosition_();
2868
2869 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002870
2871 if (this.options_.cursorBlink) {
2872 if (this.timeouts_.cursorBlink)
2873 return;
2874
Robert Gindaea2183e2014-07-17 09:51:51 -07002875 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002876 } else {
2877 if (this.timeouts_.cursorBlink) {
2878 clearTimeout(this.timeouts_.cursorBlink);
2879 delete this.timeouts_.cursorBlink;
2880 }
2881 }
2882};
2883
2884/**
Mike Frysinger225c99d2019-10-20 14:02:37 -06002885 * Pause blinking temporarily.
2886 *
2887 * When the cursor moves around, it can be helpful to momentarily pause the
2888 * blinking. This could be when the user is typing in things, or when they're
2889 * moving around with the arrow keys.
2890 */
2891hterm.Terminal.prototype.pauseCursorBlink_ = function() {
2892 if (!this.options_.cursorBlink) {
2893 return;
2894 }
2895
2896 this.cursorBlinkPause_ = true;
2897
2898 // If a timeout is already pending, reset the clock due to the new input.
2899 if (this.timeouts_.cursorBlinkPause) {
2900 clearTimeout(this.timeouts_.cursorBlinkPause);
2901 }
2902 // After 500ms, resume blinking. That seems like a good balance between user
2903 // input timings & responsiveness to resume.
2904 this.timeouts_.cursorBlinkPause = setTimeout(() => {
2905 delete this.timeouts_.cursorBlinkPause;
2906 this.cursorBlinkPause_ = false;
2907 }, 500);
2908};
2909
2910/**
rginda87b86462011-12-14 13:48:03 -08002911 * Synchronizes the visible cursor and document selection with the current
2912 * cursor coordinates.
Raymes Khourye5d48982018-08-02 09:08:32 +10002913 *
2914 * @return {boolean} True if the cursor is onscreen and synced.
rginda8ba33642011-12-14 12:31:31 -08002915 */
2916hterm.Terminal.prototype.syncCursorPosition_ = function() {
2917 var topRowIndex = this.scrollPort_.getTopRowIndex();
2918 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2919 var cursorRowIndex = this.scrollbackRows_.length +
2920 this.screen_.cursorPosition.row;
2921
Raymes Khoury15697f42018-07-17 11:37:18 +10002922 let forceSyncSelection = false;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002923 if (this.accessibilityReader_.accessibilityEnabled) {
2924 // Report the new position of the cursor for accessibility purposes.
2925 const cursorColumnIndex = this.screen_.cursorPosition.column;
2926 const cursorLineText =
2927 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
Raymes Khoury15697f42018-07-17 11:37:18 +10002928 // This will force the selection to be sync'd to the cursor position if the
2929 // user has pressed a key. Generally we would only sync the cursor position
2930 // when selection is collapsed so that if the user has selected something
2931 // we don't clear the selection by moving the selection. However when a
2932 // screen reader is used, it's intuitive for entering a key to move the
2933 // selection to the cursor.
2934 forceSyncSelection = this.accessibilityReader_.hasUserGesture;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002935 this.accessibilityReader_.afterCursorChange(
2936 cursorLineText, cursorRowIndex, cursorColumnIndex);
2937 }
2938
rginda8ba33642011-12-14 12:31:31 -08002939 if (cursorRowIndex > bottomRowIndex) {
2940 // Cursor is scrolled off screen, move it outside of the visible area.
Mike Frysinger44c32202017-08-05 01:13:09 -04002941 this.setCssVar('cursor-offset-row', '-1');
Raymes Khourye5d48982018-08-02 09:08:32 +10002942 return false;
rginda8ba33642011-12-14 12:31:31 -08002943 }
2944
Robert Gindab837c052014-08-11 11:17:51 -07002945 if (this.options_.cursorVisible &&
2946 this.cursorNode_.style.display == 'none') {
2947 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2948 this.cursorNode_.style.display = '';
2949 }
2950
Mike Frysinger44c32202017-08-05 01:13:09 -04002951 // Position the cursor using CSS variable math. If we do the math in JS,
2952 // the float math will end up being more precise than the CSS which will
2953 // cause the cursor tracking to be off.
2954 this.setCssVar(
2955 'cursor-offset-row',
2956 `${cursorRowIndex - topRowIndex} + ` +
2957 `${this.scrollPort_.visibleRowTopMargin}px`);
2958 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08002959
2960 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04002961 '(' + this.screen_.cursorPosition.column +
2962 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08002963 ')');
2964
2965 // Update the caret for a11y purposes.
2966 var selection = this.document_.getSelection();
Raymes Khoury15697f42018-07-17 11:37:18 +10002967 if (selection && (selection.isCollapsed || forceSyncSelection)) {
rginda87b86462011-12-14 13:48:03 -08002968 this.screen_.syncSelectionCaret(selection);
Raymes Khoury15697f42018-07-17 11:37:18 +10002969 }
Raymes Khourye5d48982018-08-02 09:08:32 +10002970 return true;
rginda8ba33642011-12-14 12:31:31 -08002971};
2972
Robert Gindafb1be6a2013-12-11 11:56:22 -08002973/**
2974 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2975 * and character cell dimensions.
2976 */
Robert Ginda830583c2013-08-07 13:20:46 -07002977hterm.Terminal.prototype.restyleCursor_ = function() {
2978 var shape = this.cursorShape_;
2979
2980 if (this.cursorNode_.getAttribute('focus') == 'false') {
2981 // Always show a block cursor when unfocused.
2982 shape = hterm.Terminal.cursorShape.BLOCK;
2983 }
2984
2985 var style = this.cursorNode_.style;
2986
2987 switch (shape) {
2988 case hterm.Terminal.cursorShape.BEAM:
Robert Ginda830583c2013-08-07 13:20:46 -07002989 style.backgroundColor = 'transparent';
Joel Hockeyd4fca732019-09-20 16:57:03 -07002990 style.borderBottomStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07002991 style.borderLeftStyle = 'solid';
2992 break;
2993
2994 case hterm.Terminal.cursorShape.UNDERLINE:
Robert Ginda830583c2013-08-07 13:20:46 -07002995 style.backgroundColor = 'transparent';
2996 style.borderBottomStyle = 'solid';
Joel Hockeyd4fca732019-09-20 16:57:03 -07002997 style.borderLeftStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07002998 break;
2999
3000 default:
Mike Frysinger2fd079a2018-09-02 01:46:12 -04003001 style.backgroundColor = 'var(--hterm-cursor-color)';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003002 style.borderBottomStyle = '';
3003 style.borderLeftStyle = '';
Robert Ginda830583c2013-08-07 13:20:46 -07003004 break;
3005 }
3006};
3007
rginda8ba33642011-12-14 12:31:31 -08003008/**
3009 * Synchronizes the visible cursor with the current cursor coordinates.
3010 *
3011 * The sync will happen asynchronously, soon after the call stack winds down.
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003012 * Multiple calls will be coalesced into a single sync. This should be called
3013 * prior to the cursor actually changing position.
rginda8ba33642011-12-14 12:31:31 -08003014 */
3015hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
3016 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08003017 return;
rginda8ba33642011-12-14 12:31:31 -08003018
Raymes Khouryb199d4d2018-07-12 15:08:12 +10003019 if (this.accessibilityReader_.accessibilityEnabled) {
3020 // Report the previous position of the cursor for accessibility purposes.
3021 const cursorRowIndex = this.scrollbackRows_.length +
3022 this.screen_.cursorPosition.row;
3023 const cursorColumnIndex = this.screen_.cursorPosition.column;
3024 const cursorLineText =
3025 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
3026 this.accessibilityReader_.beforeCursorChange(
3027 cursorLineText, cursorRowIndex, cursorColumnIndex);
3028 }
3029
rginda8ba33642011-12-14 12:31:31 -08003030 var self = this;
3031 this.timeouts_.syncCursor = setTimeout(function() {
3032 self.syncCursorPosition_();
3033 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08003034 }, 0);
3035};
3036
rgindacc2996c2012-02-24 14:59:31 -08003037/**
rgindaf522ce02012-04-17 17:49:17 -07003038 * Show or hide the zoom warning.
3039 *
3040 * The zoom warning is a message warning the user that their browser zoom must
3041 * be set to 100% in order for hterm to function properly.
3042 *
3043 * @param {boolean} state True to show the message, false to hide it.
3044 */
3045hterm.Terminal.prototype.showZoomWarning_ = function(state) {
3046 if (!this.zoomWarningNode_) {
3047 if (!state)
3048 return;
3049
3050 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04003051 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07003052 this.zoomWarningNode_.style.cssText = (
3053 'color: black;' +
3054 'background-color: #ff2222;' +
3055 'font-size: large;' +
3056 'border-radius: 8px;' +
3057 'opacity: 0.75;' +
3058 'padding: 0.2em 0.5em 0.2em 0.5em;' +
3059 'top: 0.5em;' +
3060 'right: 1.2em;' +
3061 'position: absolute;' +
3062 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003063 '-webkit-user-select: none;' +
3064 '-moz-text-size-adjust: none;' +
3065 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05003066
3067 this.zoomWarningNode_.addEventListener('click', function(e) {
3068 this.parentNode.removeChild(this);
3069 });
rgindaf522ce02012-04-17 17:49:17 -07003070 }
3071
Mike Frysingerb7289952019-03-23 16:05:38 -07003072 this.zoomWarningNode_.textContent = lib.i18n.replaceReferences(
Robert Gindab4839c22013-02-28 16:52:10 -08003073 hterm.zoomWarningMessage,
Joel Hockeyd4fca732019-09-20 16:57:03 -07003074 [Math.floor(this.scrollPort_.characterSize.zoomFactor * 100)]);
Robert Gindab4839c22013-02-28 16:52:10 -08003075
rgindaf522ce02012-04-17 17:49:17 -07003076 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
3077
3078 if (state) {
3079 if (!this.zoomWarningNode_.parentNode)
3080 this.div_.parentNode.appendChild(this.zoomWarningNode_);
3081 } else if (this.zoomWarningNode_.parentNode) {
3082 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
3083 }
3084};
3085
3086/**
rgindacc2996c2012-02-24 14:59:31 -08003087 * Show the terminal overlay for a given amount of time.
3088 *
3089 * The terminal overlay appears in inverse video in a large font, centered
3090 * over the terminal. You should probably keep the overlay message brief,
3091 * since it's in a large font and you probably aren't going to check the size
3092 * of the terminal first.
3093 *
3094 * @param {string} msg The text (not HTML) message to display in the overlay.
Joel Hockey0f933582019-08-27 18:01:51 -07003095 * @param {number=} opt_timeout The amount of time to wait before fading out
rgindacc2996c2012-02-24 14:59:31 -08003096 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
3097 * stay up forever (or until the next overlay).
3098 */
3099hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08003100 if (!this.overlayNode_) {
3101 if (!this.div_)
3102 return;
3103
3104 this.overlayNode_ = this.document_.createElement('div');
3105 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08003106 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08003107 'font-size: xx-large;' +
3108 'opacity: 0.75;' +
3109 'padding: 0.2em 0.5em 0.2em 0.5em;' +
3110 'position: absolute;' +
3111 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003112 '-webkit-transition: opacity 180ms ease-in;' +
3113 '-moz-user-select: none;' +
3114 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08003115
3116 this.overlayNode_.addEventListener('mousedown', function(e) {
3117 e.preventDefault();
3118 e.stopPropagation();
3119 }, true);
rgindaf0090c92012-02-10 14:58:52 -08003120 }
3121
rginda9f5222b2012-03-05 11:53:28 -08003122 this.overlayNode_.style.color = this.prefs_.get('background-color');
3123 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
3124 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
3125
rgindaf0090c92012-02-10 14:58:52 -08003126 this.overlayNode_.textContent = msg;
3127 this.overlayNode_.style.opacity = '0.75';
3128
3129 if (!this.overlayNode_.parentNode)
3130 this.div_.appendChild(this.overlayNode_);
3131
Joel Hockeyd4fca732019-09-20 16:57:03 -07003132 var divSize = hterm.getClientSize(lib.notNull(this.div_));
Robert Ginda97769282013-02-01 15:30:30 -08003133 var overlaySize = hterm.getClientSize(this.overlayNode_);
3134
Robert Ginda8a59f762014-07-23 11:29:55 -07003135 this.overlayNode_.style.top =
3136 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08003137 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07003138 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08003139
rgindaf0090c92012-02-10 14:58:52 -08003140 if (this.overlayTimeout_)
3141 clearTimeout(this.overlayTimeout_);
3142
Raymes Khouryc7a06382018-07-04 10:25:45 +10003143 this.accessibilityReader_.assertiveAnnounce(msg);
3144
rgindacc2996c2012-02-24 14:59:31 -08003145 if (opt_timeout === null)
3146 return;
3147
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003148 this.overlayTimeout_ = setTimeout(() => {
3149 this.overlayNode_.style.opacity = '0';
3150 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
3151 }, opt_timeout || 1500);
3152};
3153
3154/**
3155 * Hide the terminal overlay immediately.
3156 *
3157 * Useful when we show an overlay for an event with an unknown end time.
3158 */
3159hterm.Terminal.prototype.hideOverlay = function() {
3160 if (this.overlayTimeout_)
3161 clearTimeout(this.overlayTimeout_);
3162 this.overlayTimeout_ = null;
3163
3164 if (this.overlayNode_.parentNode)
3165 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
3166 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08003167};
3168
rginda4bba5e12012-06-20 16:15:30 -07003169/**
3170 * Paste from the system clipboard to the terminal.
Mike Frysinger23b5b832019-10-01 17:05:29 -04003171 *
Jason Lin17cc89f2020-03-19 10:48:45 +11003172 * Note: In Chrome, this should work unless the user has rejected the permission
3173 * request. In Firefox extension environment, you'll need the "clipboardRead"
3174 * permission. In other environments, this might always fail as the browser
3175 * frequently blocks access for security reasons.
3176 *
3177 * @return {?boolean} If nagivator.clipboard.readText is available, the return
3178 * value is always null. Otherwise, this function uses legacy pasting and
3179 * returns a boolean indicating whether it is successful.
rginda4bba5e12012-06-20 16:15:30 -07003180 */
3181hterm.Terminal.prototype.paste = function() {
Jason Linf129f3c2020-03-23 11:52:08 +11003182 if (!this.alwaysUseLegacyPasting &&
3183 navigator.clipboard && navigator.clipboard.readText) {
Jason Lin17cc89f2020-03-19 10:48:45 +11003184 navigator.clipboard.readText().then((data) => this.onPasteData_(data));
3185 return null;
3186 } else {
3187 // Legacy pasting.
3188 try {
3189 return this.document_.execCommand('paste');
3190 } catch (firefoxException) {
3191 // Ignore this. FF 40 and older would incorrectly throw an exception if
3192 // there was an error instead of returning false.
3193 return false;
3194 }
3195 }
rginda4bba5e12012-06-20 16:15:30 -07003196};
3197
3198/**
3199 * Copy a string to the system clipboard.
3200 *
3201 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05003202 *
3203 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07003204 */
3205hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08003206 if (this.prefs_.get('enable-clipboard-notice'))
3207 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
3208
Mike Frysinger96eacae2019-01-02 18:13:56 -05003209 hterm.copySelectionToClipboard(this.document_, str);
rginda4bba5e12012-06-20 16:15:30 -07003210};
3211
Evan Jones2600d4f2016-12-06 09:29:36 -05003212/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003213 * Display an image.
3214 *
Mike Frysinger2558ed52019-01-14 01:03:41 -05003215 * Either URI or buffer or blob fields must be specified.
3216 *
Joel Hockey0f933582019-08-27 18:01:51 -07003217 * @param {{
3218 * name: (string|undefined),
3219 * size: (string|number|undefined),
3220 * preserveAspectRation: (boolean|undefined),
3221 * inline: (boolean|undefined),
3222 * width: (string|number|undefined),
3223 * height: (string|number|undefined),
3224 * align: (string|undefined),
3225 * url: (string|undefined),
3226 * buffer: (!ArrayBuffer|undefined),
3227 * blob: (!Blob|undefined),
3228 * type: (string|undefined),
3229 * }} options The image to display.
3230 * name A human readable string for the image
3231 * size The size (in bytes).
3232 * preserveAspectRatio Whether to preserve aspect.
3233 * inline Whether to display the image inline.
3234 * width The width of the image.
3235 * height The height of the image.
3236 * align Direction to align the image.
3237 * uri The source URI for the image.
3238 * buffer The ArrayBuffer image data.
3239 * blob The Blob image data.
3240 * type The MIME type of the image data.
3241 * @param {function()=} onLoad Callback when loading finishes.
3242 * @param {function(!Event)=} onError Callback when loading fails.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003243 */
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003244hterm.Terminal.prototype.displayImage = function(options, onLoad, onError) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003245 // Make sure we're actually given a resource to display.
Mike Frysinger2558ed52019-01-14 01:03:41 -05003246 if (options.uri === undefined && options.buffer === undefined &&
3247 options.blob === undefined)
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003248 return;
3249
3250 // Set up the defaults to simplify code below.
3251 if (!options.name)
3252 options.name = '';
3253
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003254 // See if the mime type is available. If not, guess from the filename.
3255 // We don't list all possible mime types because the browser can usually
3256 // guess it correctly. So list the ones that need a bit more help.
3257 if (!options.type) {
3258 const ary = options.name.split('.');
3259 const ext = ary[ary.length - 1].trim();
3260 switch (ext) {
3261 case 'svg':
3262 case 'svgz':
3263 options.type = 'image/svg+xml';
3264 break;
3265 }
3266 }
3267
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003268 // Has the user approved image display yet?
3269 if (this.allowImagesInline !== true) {
3270 this.newLine();
3271 const row = this.getRowNode(this.scrollbackRows_.length +
3272 this.getCursorRow() - 1);
3273
3274 if (this.allowImagesInline === false) {
3275 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
3276 'Inline Images Disabled');
3277 return;
3278 }
3279
3280 // Show a prompt.
3281 let button;
3282 const span = this.document_.createElement('span');
3283 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
3284 span.style.fontWeight = 'bold';
3285 span.style.borderWidth = '1px';
3286 span.style.borderStyle = 'dashed';
3287 button = this.document_.createElement('span');
3288 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3289 button.style.marginLeft = '1em';
3290 button.style.borderWidth = '1px';
3291 button.style.borderStyle = 'solid';
3292 button.addEventListener('click', () => {
3293 this.prefs_.set('allow-images-inline', false);
3294 });
3295 span.appendChild(button);
3296 button = this.document_.createElement('span');
3297 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3298 'allow this session');
3299 button.style.marginLeft = '1em';
3300 button.style.borderWidth = '1px';
3301 button.style.borderStyle = 'solid';
3302 button.addEventListener('click', () => {
3303 this.allowImagesInline = true;
3304 });
3305 span.appendChild(button);
3306 button = this.document_.createElement('span');
3307 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3308 button.style.marginLeft = '1em';
3309 button.style.borderWidth = '1px';
3310 button.style.borderStyle = 'solid';
3311 button.addEventListener('click', () => {
3312 this.prefs_.set('allow-images-inline', true);
3313 });
3314 span.appendChild(button);
3315
3316 row.appendChild(span);
3317 return;
3318 }
3319
3320 // See if we should show this object directly, or download it.
3321 if (options.inline) {
3322 const io = this.io.push();
3323 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
Joel Hockeyd4fca732019-09-20 16:57:03 -07003324 'Loading $1 ...'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003325
3326 // While we're loading the image, eat all the user's input.
3327 io.onVTKeystroke = io.sendString = () => {};
3328
3329 // Initialize this new image.
Joel Hockeyd4fca732019-09-20 16:57:03 -07003330 const img = this.document_.createElement('img');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003331 if (options.uri !== undefined) {
3332 img.src = options.uri;
3333 } else if (options.buffer !== undefined) {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003334 const blob = new Blob([options.buffer], {type: options.type});
Mike Frysinger2558ed52019-01-14 01:03:41 -05003335 img.src = URL.createObjectURL(blob);
3336 } else {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003337 const blob = new Blob([options.blob], {type: options.type});
Joel Hockeyd4fca732019-09-20 16:57:03 -07003338 img.src = URL.createObjectURL(blob);
Mike Frysinger2558ed52019-01-14 01:03:41 -05003339 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003340 img.title = img.alt = options.name;
3341
3342 // Attach the image to the page to let it load/render. It won't stay here.
3343 // This is needed so it's visible and the DOM can calculate the height. If
3344 // the image is hidden or not in the DOM, the height is always 0.
3345 this.document_.body.appendChild(img);
3346
3347 // Wait for the image to finish loading before we try moving it to the
3348 // right place in the terminal.
3349 img.onload = () => {
3350 // Now that we have the image dimensions, figure out how to show it.
3351 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
3352 img.style.maxWidth = `${this.document_.body.clientWidth}px`;
3353 img.style.maxHeight = `${this.document_.body.clientHeight}px`;
3354
3355 // Parse a width/height specification.
3356 const parseDim = (dim, maxDim, cssVar) => {
3357 if (!dim || dim == 'auto')
3358 return '';
3359
3360 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3361 if (ary) {
3362 if (ary[2] == '%')
Joel Hockeyd4fca732019-09-20 16:57:03 -07003363 return Math.floor(maxDim * ary[1] / 100) + 'px';
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003364 else if (ary[2] == 'px')
3365 return dim;
3366 else
3367 return `calc(${dim} * var(${cssVar}))`;
3368 }
3369
3370 return '';
3371 };
3372 img.style.width =
3373 parseDim(options.width, this.document_.body.clientWidth,
3374 '--hterm-charsize-width');
3375 img.style.height =
3376 parseDim(options.height, this.document_.body.clientHeight,
3377 '--hterm-charsize-height');
3378
3379 // Figure out how many rows the image occupies, then add that many.
Mike Frysingera0349392019-09-11 05:38:09 -04003380 // Note: This count will be inaccurate if the font size changes on us.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003381 const padRows = Math.ceil(img.clientHeight /
3382 this.scrollPort_.characterSize.height);
3383 for (let i = 0; i < padRows; ++i)
3384 this.newLine();
3385
3386 // Update the max height in case the user shrinks the character size.
3387 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3388
3389 // Move the image to the last row. This way when we scroll up, it doesn't
3390 // disappear when the first row gets clipped. It will disappear when we
3391 // scroll down and the last row is clipped ...
3392 this.document_.body.removeChild(img);
3393 // Create a wrapper node so we can do an absolute in a relative position.
3394 // This helps with rounding errors between JS & CSS counts.
3395 const div = this.document_.createElement('div');
3396 div.style.position = 'relative';
Joel Hockeyd4fca732019-09-20 16:57:03 -07003397 div.style.textAlign = options.align || '';
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003398 img.style.position = 'absolute';
3399 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3400 div.appendChild(img);
3401 const row = this.getRowNode(this.scrollbackRows_.length +
3402 this.getCursorRow() - 1);
3403 row.appendChild(div);
3404
Mike Frysinger2558ed52019-01-14 01:03:41 -05003405 // Now that the image has been read, we can revoke the source.
3406 if (options.uri === undefined) {
3407 URL.revokeObjectURL(img.src);
3408 }
3409
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003410 io.hideOverlay();
3411 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003412
3413 if (onLoad)
3414 onLoad();
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003415 };
3416
3417 // If we got a malformed image, give up.
3418 img.onerror = (e) => {
3419 this.document_.body.removeChild(img);
3420 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
Mike Frysingere14a8c42018-03-10 00:17:30 -08003421 'Loading $1 failed'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003422 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003423
3424 if (onError)
3425 onError(e);
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003426 };
3427 } else {
3428 // We can't use chrome.downloads.download as that requires "downloads"
3429 // permissions, and that works only in extensions, not apps.
3430 const a = this.document_.createElement('a');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003431 if (options.uri !== undefined) {
3432 a.href = options.uri;
3433 } else if (options.buffer !== undefined) {
3434 const blob = new Blob([options.buffer]);
3435 a.href = URL.createObjectURL(blob);
3436 } else {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003437 a.href = URL.createObjectURL(lib.notNull(options.blob));
Mike Frysinger2558ed52019-01-14 01:03:41 -05003438 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003439 a.download = options.name;
3440 this.document_.body.appendChild(a);
3441 a.click();
3442 a.remove();
Mike Frysinger2558ed52019-01-14 01:03:41 -05003443 if (options.uri === undefined) {
3444 URL.revokeObjectURL(a.href);
3445 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003446 }
3447};
3448
3449/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003450 * Returns the selected text, or null if no text is selected.
3451 *
3452 * @return {string|null}
3453 */
rgindaa09e7332012-08-17 12:49:51 -07003454hterm.Terminal.prototype.getSelectionText = function() {
3455 var selection = this.scrollPort_.selection;
3456 selection.sync();
3457
3458 if (selection.isCollapsed)
3459 return null;
3460
rgindaa09e7332012-08-17 12:49:51 -07003461 // Start offset measures from the beginning of the line.
3462 var startOffset = selection.startOffset;
3463 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003464
Raymes Khoury334625a2018-06-25 10:29:40 +10003465 // If an x-row isn't selected, |node| will be null.
3466 if (!node)
3467 return null;
3468
Robert Gindafdbb3f22012-09-06 20:23:06 -07003469 if (node.nodeName != 'X-ROW') {
3470 // If the selection doesn't start on an x-row node, then it must be
3471 // somewhere inside the x-row. Add any characters from previous siblings
3472 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003473
3474 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3475 // If node is the text node in a styled span, move up to the span node.
3476 node = node.parentNode;
3477 }
3478
Robert Gindafdbb3f22012-09-06 20:23:06 -07003479 while (node.previousSibling) {
3480 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003481 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003482 }
rgindaa09e7332012-08-17 12:49:51 -07003483 }
3484
3485 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08003486 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
3487 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003488 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003489
Robert Gindafdbb3f22012-09-06 20:23:06 -07003490 if (node.nodeName != 'X-ROW') {
3491 // If the selection doesn't end on an x-row node, then it must be
3492 // somewhere inside the x-row. Add any characters from following siblings
3493 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003494
3495 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3496 // If node is the text node in a styled span, move up to the span node.
3497 node = node.parentNode;
3498 }
3499
Robert Gindafdbb3f22012-09-06 20:23:06 -07003500 while (node.nextSibling) {
3501 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003502 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003503 }
rgindaa09e7332012-08-17 12:49:51 -07003504 }
3505
3506 var rv = this.getRowsText(selection.startRow.rowIndex,
3507 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003508 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003509};
3510
rginda4bba5e12012-06-20 16:15:30 -07003511/**
3512 * Copy the current selection to the system clipboard, then clear it after a
3513 * short delay.
3514 */
3515hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07003516 var text = this.getSelectionText();
3517 if (text != null)
3518 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07003519};
3520
Joel Hockey0f933582019-08-27 18:01:51 -07003521/**
3522 * Show overlay with current terminal size.
3523 */
rgindaf0090c92012-02-10 14:58:52 -08003524hterm.Terminal.prototype.overlaySize = function() {
Theodore Duboisdd5f9a72019-09-06 23:28:42 -07003525 if (this.prefs_.get('enable-resize-status')) {
3526 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
3527 }
rgindaf0090c92012-02-10 14:58:52 -08003528};
3529
rginda87b86462011-12-14 13:48:03 -08003530/**
3531 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3532 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003533 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003534 */
3535hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08003536 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08003537 this.scrollPort_.scrollRowToBottom(this.getRowCount());
3538
Mike Frysinger225c99d2019-10-20 14:02:37 -06003539 this.pauseCursorBlink_();
3540
Mike Frysinger79669762018-12-30 20:51:10 -05003541 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08003542};
3543
3544/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003545 * Open the selected url.
3546 */
3547hterm.Terminal.prototype.openSelectedUrl_ = function() {
3548 var str = this.getSelectionText();
3549
3550 // If there is no selection, try and expand wherever they clicked.
3551 if (str == null) {
John Lincae9b732018-03-08 13:56:35 +08003552 this.screen_.expandSelectionForUrl(this.document_.getSelection());
Mike Frysinger70b94692017-01-26 18:57:50 -10003553 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003554
3555 // If clicking in empty space, return.
3556 if (str == null)
3557 return;
Mike Frysinger70b94692017-01-26 18:57:50 -10003558 }
3559
3560 // Make sure URL is valid before opening.
3561 if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0)
3562 return;
Mike Frysinger43472622017-06-26 18:11:07 -04003563
3564 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003565 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003566 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3567 // We have to whitelist a few protocols that lack authorities and thus
3568 // never use the //. Like mailto.
3569 switch (str.split(':', 1)[0]) {
3570 case 'mailto':
3571 break;
3572 default:
3573 str = 'http://' + str;
3574 break;
3575 }
3576 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003577
Mike Frysinger720fa832017-10-23 01:15:52 -04003578 hterm.openUrl(str);
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003579};
Mike Frysinger70b94692017-01-26 18:57:50 -10003580
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003581/**
3582 * Manage the automatic mouse hiding behavior while typing.
3583 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003584 * @param {?boolean=} v Whether to enable automatic hiding.
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003585 */
3586hterm.Terminal.prototype.setAutomaticMouseHiding = function(v=null) {
3587 // Since Chrome OS & macOS do this by default everywhere, we don't need to.
3588 // Linux & Windows seem to leave this to specific applications to manage.
3589 if (v === null)
3590 v = (hterm.os != 'cros' && hterm.os != 'mac');
3591
3592 this.mouseHideWhileTyping_ = !!v;
3593};
3594
3595/**
3596 * Handler for monitoring user keyboard activity.
3597 *
3598 * This isn't for processing the keystrokes directly, but for updating any
3599 * state that might toggle based on the user using the keyboard at all.
3600 *
Joel Hockey0f933582019-08-27 18:01:51 -07003601 * @param {!KeyboardEvent} e The keyboard event that triggered us.
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003602 */
3603hterm.Terminal.prototype.onKeyboardActivity_ = function(e) {
3604 // When the user starts typing, hide the mouse cursor.
3605 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_)
3606 this.setCssVar('mouse-cursor-style', 'none');
3607};
Mike Frysinger70b94692017-01-26 18:57:50 -10003608
3609/**
rgindad5613292012-06-19 15:40:37 -07003610 * Add the terminalRow and terminalColumn properties to mouse events and
3611 * then forward on to onMouse().
3612 *
3613 * The terminalRow and terminalColumn properties contain the (row, column)
3614 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003615 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003616 * @param {!MouseEvent} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003617 */
3618hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003619 if (e.processedByTerminalHandler_) {
3620 // We register our event handlers on the document, as well as the cursor
3621 // and the scroll blocker. Mouse events that occur on the cursor or
3622 // scroll blocker will also appear on the document, but we don't want to
3623 // process them twice.
3624 //
3625 // We can't just prevent bubbling because that has other side effects, so
3626 // we decorate the event object with this property instead.
3627 return;
3628 }
3629
Mike Frysinger468966c2018-08-28 13:48:51 -04003630 // Consume navigation events. Button 3 is usually "browser back" and
3631 // button 4 is "browser forward" which we don't want to happen.
3632 if (e.button > 2) {
3633 e.preventDefault();
3634 // We don't return so click events can be passed to the remote below.
3635 }
3636
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003637 var reportMouseEvents = (!this.defeatMouseReports_ &&
3638 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3639
rgindafaa74742012-08-21 13:34:03 -07003640 e.processedByTerminalHandler_ = true;
3641
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003642 // Handle auto hiding of mouse cursor while typing.
3643 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
3644 // Make sure the mouse cursor is visible.
3645 this.syncMouseStyle();
3646 // This debounce isn't perfect, but should work well enough for such a
3647 // simple implementation. If the user moved the mouse, we enabled this
3648 // debounce, and then moved the mouse just before the timeout, we wouldn't
3649 // debounce that later movement.
3650 this.mouseHideDelay_ = setTimeout(() => this.mouseHideDelay_ = null, 1000);
3651 }
3652
Robert Gindaeda48db2014-07-17 09:25:30 -07003653 // One based row/column stored on the mouse event.
Joel Hockeyd4fca732019-09-20 16:57:03 -07003654 e.terminalRow = Math.floor(
3655 (e.clientY - this.scrollPort_.visibleRowTopMargin) /
3656 this.scrollPort_.characterSize.height) + 1;
3657 e.terminalColumn = Math.floor(
3658 e.clientX / this.scrollPort_.characterSize.width) + 1;
Robert Gindaeda48db2014-07-17 09:25:30 -07003659
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003660 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
3661 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07003662 return;
3663 }
3664
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003665 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07003666 // If the cursor is visible and we're not sending mouse events to the
3667 // host app, then we want to hide the terminal cursor when the mouse
3668 // cursor is over top. This keeps the terminal cursor from interfering
3669 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003670 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3671 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3672 this.cursorNode_.style.display = 'none';
3673 } else if (this.cursorNode_.style.display == 'none') {
3674 this.cursorNode_.style.display = '';
3675 }
3676 }
rgindad5613292012-06-19 15:40:37 -07003677
Robert Ginda928cf632014-03-05 15:07:41 -08003678 if (e.type == 'mousedown') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003679 this.contextMenu.hide();
Mike Frysingercc114512017-09-11 21:39:17 -04003680
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003681 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003682 // If VT mouse reporting is disabled, or has been defeated with
3683 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003684 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003685 this.setSelectionEnabled(true);
3686 } else {
3687 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003688 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003689 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003690 this.setSelectionEnabled(false);
3691 e.preventDefault();
3692 }
3693 }
3694
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003695 if (!reportMouseEvents) {
John Lin2aad22e2018-03-16 13:58:11 +08003696 if (e.type == 'dblclick') {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003697 this.screen_.expandSelection(this.document_.getSelection());
John Lin2aad22e2018-03-16 13:58:11 +08003698 if (this.copyOnSelect)
Mike Frysinger406c76c2019-01-02 17:53:51 -05003699 this.copySelectionToClipboard();
rgindad5613292012-06-19 15:40:37 -07003700 }
3701
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003702 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003703 // Debounce this event with the dblclick event. If you try to doubleclick
3704 // a URL to open it, Chrome will fire click then dblclick, but we won't
3705 // have expanded the selection text at the first click event.
3706 clearTimeout(this.timeouts_.openUrl);
3707 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3708 500);
3709 return;
3710 }
3711
Mike Frysinger847577f2017-05-23 23:25:57 -04003712 if (e.type == 'mousedown') {
Mike Frysingercc114512017-09-11 21:39:17 -04003713 if (e.ctrlKey && e.button == 2 /* right button */) {
3714 e.preventDefault();
3715 this.contextMenu.show(e, this);
3716 } else if (e.button == this.mousePasteButton ||
3717 (this.mouseRightClickPaste && e.button == 2 /* right button */)) {
Jason Lin17cc89f2020-03-19 10:48:45 +11003718 if (this.paste() === false)
Mike Frysinger05a57f02017-08-27 17:48:55 -04003719 console.warn('Could not paste manually due to web restrictions');
Mike Frysinger847577f2017-05-23 23:25:57 -04003720 }
3721 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003722
Mike Frysinger2edd3612017-05-24 00:54:39 -04003723 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003724 !this.document_.getSelection().isCollapsed) {
Mike Frysinger406c76c2019-01-02 17:53:51 -05003725 this.copySelectionToClipboard();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003726 }
3727
3728 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3729 this.scrollBlockerNode_.engaged) {
3730 // Disengage the scroll-blocker after one of these events.
3731 this.scrollBlockerNode_.engaged = false;
3732 this.scrollBlockerNode_.style.top = '-99px';
3733 }
3734
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003735 // Emulate arrow key presses via scroll wheel events.
3736 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3737 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003738 if (e.type == 'wheel') {
Joel Hockeyd4fca732019-09-20 16:57:03 -07003739 const delta =
3740 this.scrollPort_.scrollWheelDelta(/** @type {!WheelEvent} */ (e));
Mike Frysingerc3030a82017-05-29 14:16:11 -04003741
Mike Frysinger321063c2018-08-29 15:33:14 -04003742 // Helper to turn a wheel event delta into a series of key presses.
3743 const deltaToArrows = (distance, charSize, arrowPos, arrowNeg) => {
3744 if (distance == 0) {
3745 return '';
3746 }
3747
3748 // Convert the scroll distance into a number of rows/cols.
3749 const cells = lib.f.smartFloorDivide(Math.abs(distance), charSize);
3750 const data = '\x1bO' + (distance < 0 ? arrowNeg : arrowPos);
3751 return data.repeat(cells);
3752 };
3753
3754 // The order between up/down and left/right doesn't really matter.
3755 this.io.sendString(
3756 // Up/down arrow keys.
3757 deltaToArrows(delta.y, this.scrollPort_.characterSize.height,
3758 'A', 'B') +
3759 // Left/right arrow keys.
3760 deltaToArrows(delta.x, this.scrollPort_.characterSize.width,
3761 'C', 'D')
3762 );
Mike Frysingerc3030a82017-05-29 14:16:11 -04003763
3764 e.preventDefault();
3765 }
3766 }
Robert Ginda928cf632014-03-05 15:07:41 -08003767 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003768 if (!this.scrollBlockerNode_.engaged) {
3769 if (e.type == 'mousedown') {
3770 // Move the scroll-blocker into place if we want to keep the scrollport
3771 // from scrolling.
3772 this.scrollBlockerNode_.engaged = true;
3773 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3774 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3775 } else if (e.type == 'mousemove') {
3776 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3777 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003778 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003779 e.preventDefault();
3780 }
3781 }
Robert Ginda928cf632014-03-05 15:07:41 -08003782
3783 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003784 }
3785
Robert Ginda928cf632014-03-05 15:07:41 -08003786 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3787 // Restore this on mouseup in case it was temporarily defeated with a
3788 // alt-mousedown. Only do this when the selection is empty so that
3789 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003790 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003791 }
rgindad5613292012-06-19 15:40:37 -07003792};
3793
3794/**
3795 * Clients should override this if they care to know about mouse events.
3796 *
3797 * The event parameter will be a normal DOM mouse click event with additional
3798 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003799 *
Joel Hockeyd4fca732019-09-20 16:57:03 -07003800 * @param {!MouseEvent} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003801 */
3802hterm.Terminal.prototype.onMouse = function(e) { };
3803
3804/**
rginda8e92a692012-05-20 19:37:20 -07003805 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003806 *
3807 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003808 */
Rob Spies06533ba2014-04-24 11:20:37 -07003809hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3810 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003811 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003812
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003813 if (this.reportFocus)
3814 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O');
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003815
Michael Kelly485ecd12014-06-09 11:41:56 -04003816 if (focused === true)
3817 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07003818};
3819
3820/**
rginda8ba33642011-12-14 12:31:31 -08003821 * React when the ScrollPort is scrolled.
3822 */
3823hterm.Terminal.prototype.onScroll_ = function() {
3824 this.scheduleSyncCursorPosition_();
3825};
3826
3827/**
rginda9846e2f2012-01-27 13:53:33 -08003828 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003829 *
Joel Hockeye25ce432019-09-25 19:12:28 -07003830 * @param {{text: string}} e The text of the paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08003831 */
3832hterm.Terminal.prototype.onPaste_ = function(e) {
Jason Lin17cc89f2020-03-19 10:48:45 +11003833 this.onPasteData_(e.text);
3834};
3835
3836/**
3837 * Handle pasted data.
3838 *
3839 * @param {string} data The pasted data.
3840 */
3841hterm.Terminal.prototype.onPasteData_ = function(data) {
3842 data = data.replace(/\n/mg, '\r');
Mike Frysingere8c32c82018-03-11 14:57:28 -07003843 if (this.options_.bracketedPaste) {
3844 // We strip out most escape sequences as they can cause issues (like
3845 // inserting an \x1b[201~ midstream). We pass through whitespace
3846 // though: 0x08:\b 0x09:\t 0x0a:\n 0x0d:\r.
3847 // This matches xterm behavior.
3848 const filter = (data) => data.replace(/[\x00-\x07\x0b-\x0c\x0e-\x1f]/g, '');
3849 data = '\x1b[200~' + filter(data) + '\x1b[201~';
3850 }
Robert Gindaa063b202014-07-21 11:08:25 -07003851
3852 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003853};
3854
3855/**
rgindaa09e7332012-08-17 12:49:51 -07003856 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003857 *
Joel Hockey0f933582019-08-27 18:01:51 -07003858 * @param {!Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07003859 */
3860hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07003861 if (!this.useDefaultWindowCopy) {
3862 e.preventDefault();
3863 setTimeout(this.copySelectionToClipboard.bind(this), 0);
3864 }
rgindaa09e7332012-08-17 12:49:51 -07003865};
3866
3867/**
rginda8ba33642011-12-14 12:31:31 -08003868 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08003869 *
3870 * Note: This function should not directly contain code that alters the internal
3871 * state of the terminal. That kind of code belongs in realizeWidth or
3872 * realizeHeight, so that it can be executed synchronously in the case of a
3873 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08003874 */
3875hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08003876 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07003877 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08003878 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07003879 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08003880
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003881 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08003882 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003883 // gets removed from the document or during the initial load, and we can't
3884 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07003885 // This can also happen if called before the scrollPort calculates the
3886 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08003887 return;
3888 }
3889
rgindaa8ba17d2012-08-15 14:41:10 -07003890 var isNewSize = (columnCount != this.screenSize.width ||
3891 rowCount != this.screenSize.height);
Theodore Dubois651b0842019-09-07 14:32:09 -07003892 const wasScrolledEnd = this.scrollPort_.isScrolledEnd;
rgindaa8ba17d2012-08-15 14:41:10 -07003893
3894 // We do this even if the size didn't change, just to be sure everything is
3895 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04003896 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07003897 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07003898
3899 if (isNewSize)
3900 this.overlaySize();
3901
Robert Gindafb1be6a2013-12-11 11:56:22 -08003902 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07003903 this.scheduleSyncCursorPosition_();
Theodore Dubois651b0842019-09-07 14:32:09 -07003904
3905 if (wasScrolledEnd) {
3906 this.scrollEnd();
3907 }
rginda8ba33642011-12-14 12:31:31 -08003908};
3909
3910/**
3911 * Service the cursor blink timeout.
3912 */
3913hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07003914 if (!this.options_.cursorBlink) {
3915 delete this.timeouts_.cursorBlink;
3916 return;
3917 }
3918
Robert Ginda830583c2013-08-07 13:20:46 -07003919 if (this.cursorNode_.getAttribute('focus') == 'false' ||
Mike Frysinger225c99d2019-10-20 14:02:37 -06003920 this.cursorNode_.style.opacity == '0' ||
3921 this.cursorBlinkPause_) {
rginda87b86462011-12-14 13:48:03 -08003922 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07003923 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3924 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08003925 } else {
rginda87b86462011-12-14 13:48:03 -08003926 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07003927 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3928 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08003929 }
3930};
David Reveman8f552492012-03-28 12:18:41 -04003931
3932/**
3933 * Set the scrollbar-visible mode bit.
3934 *
3935 * If scrollbar-visible is on, the vertical scrollbar will be visible.
3936 * Otherwise it will not.
3937 *
3938 * Defaults to on.
3939 *
3940 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
3941 */
3942hterm.Terminal.prototype.setScrollbarVisible = function(state) {
3943 this.scrollPort_.setScrollbarVisible(state);
3944};
Michael Kelly485ecd12014-06-09 11:41:56 -04003945
3946/**
Rob Spies49039e52014-12-17 13:40:04 -08003947 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04003948 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08003949 *
3950 * Defaults to 1.
3951 *
Evan Jones2600d4f2016-12-06 09:29:36 -05003952 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08003953 */
3954hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3955 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3956};
3957
3958/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003959 * Close all web notifications created by terminal bells.
3960 */
3961hterm.Terminal.prototype.closeBellNotifications_ = function() {
3962 this.bellNotificationList_.forEach(function(n) {
3963 n.close();
3964 });
3965 this.bellNotificationList_.length = 0;
3966};
Raymes Khourye5d48982018-08-02 09:08:32 +10003967
3968/**
3969 * Syncs the cursor position when the scrollport gains focus.
3970 */
3971hterm.Terminal.prototype.onScrollportFocus_ = function() {
3972 // If the cursor is offscreen we set selection to the last row on the screen.
3973 const topRowIndex = this.scrollPort_.getTopRowIndex();
3974 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
3975 const selection = this.document_.getSelection();
3976 if (!this.syncCursorPosition_() && selection) {
3977 selection.collapse(this.getRowNode(bottomRowIndex));
3978 }
3979};