blob: c2d65055a80187d45a91bd0f044e4db16bc3715d [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 *
Robert Ginda57f03b42012-09-13 11:02:48 -070024 * @param {string} opt_profileId Optional preference profile name. If not
rginda9f5222b2012-03-05 11:53:28 -080025 * provided, defaults to 'default'.
rginda8ba33642011-12-14 12:31:31 -080026 */
Robert Ginda57f03b42012-09-13 11:02:48 -070027hterm.Terminal = function(opt_profileId) {
28 this.profileId_ = null;
rginda9f5222b2012-03-05 11:53:28 -080029
rginda8ba33642011-12-14 12:31:31 -080030 // Two screen instances.
31 this.primaryScreen_ = new hterm.Screen();
32 this.alternateScreen_ = new hterm.Screen();
33
34 // The "current" screen.
35 this.screen_ = this.primaryScreen_;
36
rginda8ba33642011-12-14 12:31:31 -080037 // The local notion of the screen size. ScreenBuffers also have a size which
38 // indicates their present size. During size changes, the two may disagree.
39 // Also, the inactive screen's size is not altered until it is made the active
40 // screen.
41 this.screenSize = new hterm.Size(0, 0);
42
rginda8ba33642011-12-14 12:31:31 -080043 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080044 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080045 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
46 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080047 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
Raymes Khourye5d48982018-08-02 09:08:32 +100048 this.scrollPort_.subscribe('focus', this.onScrollportFocus_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070049 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080050
rginda87b86462011-12-14 13:48:03 -080051 // The div that contains this terminal.
52 this.div_ = null;
53
rgindac9bc5502012-01-18 11:48:44 -080054 // The document that contains the scrollPort. Defaulted to the global
55 // document here so that the terminal is functional even if it hasn't been
56 // inserted into a document yet, but re-set in decorate().
57 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080058
rginda8ba33642011-12-14 12:31:31 -080059 // The rows that have scrolled off screen and are no longer addressable.
60 this.scrollbackRows_ = [];
61
rgindac9bc5502012-01-18 11:48:44 -080062 // Saved tab stops.
63 this.tabStops_ = [];
64
David Benjamin66e954d2012-05-05 21:08:12 -040065 // Keep track of whether default tab stops have been erased; after a TBC
66 // clears all tab stops, defaults aren't restored on resize until a reset.
67 this.defaultTabStops = true;
68
rginda8ba33642011-12-14 12:31:31 -080069 // The VT's notion of the top and bottom rows. Used during some VT
70 // cursor positioning and scrolling commands.
71 this.vtScrollTop_ = null;
72 this.vtScrollBottom_ = null;
73
74 // The DIV element for the visible cursor.
75 this.cursorNode_ = null;
76
Robert Ginda830583c2013-08-07 13:20:46 -070077 // The current cursor shape of the terminal.
78 this.cursorShape_ = hterm.Terminal.cursorShape.BLOCK;
79
Robert Gindaea2183e2014-07-17 09:51:51 -070080 // Cursor blink on/off cycle in ms, overwritten by prefs once they're loaded.
81 this.cursorBlinkCycle_ = [100, 100];
82
83 // Pre-bound onCursorBlink_ handler, so we don't have to do this for each
84 // cursor on/off servicing.
85 this.myOnCursorBlink_ = this.onCursorBlink_.bind(this);
86
rginda9f5222b2012-03-05 11:53:28 -080087 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -070088 // each output and keystroke. They are initialized by the preference manager.
Robert Ginda8cb7d902013-06-20 14:37:18 -070089 this.backgroundColor_ = null;
90 this.foregroundColor_ = null;
Robert Ginda57f03b42012-09-13 11:02:48 -070091 this.scrollOnOutput_ = null;
92 this.scrollOnKeystroke_ = null;
Mike Frysinger3c9fa072017-07-13 10:21:13 -040093 this.scrollWheelArrowKeys_ = null;
rginda9f5222b2012-03-05 11:53:28 -080094
Robert Ginda6aec7eb2015-06-16 10:31:30 -070095 // True if we should override mouse event reporting to allow local selection.
96 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -080097
Mike Frysinger02ded6d2018-06-21 14:25:20 -040098 // Whether to auto hide the mouse cursor when typing.
99 this.setAutomaticMouseHiding();
100 // Timer to keep mouse visible while it's being used.
101 this.mouseHideDelay_ = null;
102
rgindaf0090c92012-02-10 14:58:52 -0800103 // Terminal bell sound.
104 this.bellAudio_ = this.document_.createElement('audio');
Mike Frysingerd826f1a2017-07-06 16:20:06 -0400105 this.bellAudio_.id = 'hterm:bell-audio';
rgindaf0090c92012-02-10 14:58:52 -0800106 this.bellAudio_.setAttribute('preload', 'auto');
107
Raymes Khoury3e44bc92018-05-17 10:54:23 +1000108 // The AccessibilityReader object for announcing command output.
109 this.accessibilityReader_ = null;
110
Mike Frysingercc114512017-09-11 21:39:17 -0400111 // The context menu object.
112 this.contextMenu = new hterm.ContextMenu();
113
Michael Kelly485ecd12014-06-09 11:41:56 -0400114 // All terminal bell notifications that have been generated (not necessarily
115 // shown).
116 this.bellNotificationList_ = [];
117
118 // Whether we have permission to display notifications.
119 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400120
rginda6d397402012-01-17 10:58:29 -0800121 // Cursor position and attributes saved with DECSC.
122 this.savedOptions_ = {};
123
rginda8ba33642011-12-14 12:31:31 -0800124 // The current mode bits for the terminal.
125 this.options_ = new hterm.Options();
126
127 // Timeouts we might need to clear.
128 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800129
130 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800131 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800132
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800133 this.saveCursorAndState(true);
134
Zhu Qunying30d40712017-03-14 16:27:00 -0700135 // The keyboard handler.
rgindafeaf3142012-01-31 15:14:20 -0800136 this.keyboard = new hterm.Keyboard(this);
137
rginda87b86462011-12-14 13:48:03 -0800138 // General IO interface that can be given to third parties without exposing
139 // the entire terminal object.
140 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800141
rgindad5613292012-06-19 15:40:37 -0700142 // True if mouse-click-drag should scroll the terminal.
143 this.enableMouseDragScroll = true;
144
Robert Ginda57f03b42012-09-13 11:02:48 -0700145 this.copyOnSelect = null;
Mike Frysinger847577f2017-05-23 23:25:57 -0400146 this.mouseRightClickPaste = null;
rginda4bba5e12012-06-20 16:15:30 -0700147 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700148
Zhu Qunying30d40712017-03-14 16:27:00 -0700149 // Whether to use the default window copy behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700150 this.useDefaultWindowCopy = false;
151
152 this.clearSelectionAfterCopy = true;
153
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400154 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800155 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700156
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400157 // Whether we allow images to be shown.
158 this.allowImagesInline = null;
159
Gabriel Holodake8a09be2017-10-10 01:07:11 -0400160 this.reportFocus = false;
161
Robert Ginda57f03b42012-09-13 11:02:48 -0700162 this.setProfile(opt_profileId || 'default',
Evan Jones5f9df812016-12-06 09:38:58 -0500163 function() { this.onTerminalReady(); }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800164};
165
166/**
Robert Ginda830583c2013-08-07 13:20:46 -0700167 * Possible cursor shapes.
168 */
169hterm.Terminal.cursorShape = {
170 BLOCK: 'BLOCK',
171 BEAM: 'BEAM',
172 UNDERLINE: 'UNDERLINE'
173};
174
175/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700176 * Clients should override this to be notified when the terminal is ready
177 * for use.
178 *
179 * The terminal initialization is asynchronous, and shouldn't be used before
180 * this method is called.
181 */
182hterm.Terminal.prototype.onTerminalReady = function() { };
183
184/**
rginda35c456b2012-02-09 17:29:05 -0800185 * Default tab with of 8 to match xterm.
186 */
187hterm.Terminal.prototype.tabWidth = 8;
188
189/**
rginda9f5222b2012-03-05 11:53:28 -0800190 * Select a preference profile.
191 *
192 * This will load the terminal preferences for the given profile name and
193 * associate subsequent preference changes with the new preference profile.
194 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500195 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800196 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700197 * @param {function} opt_callback Optional callback to invoke when the profile
198 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800199 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700200hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
201 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800202
Robert Ginda57f03b42012-09-13 11:02:48 -0700203 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800204
Robert Ginda57f03b42012-09-13 11:02:48 -0700205 if (this.prefs_)
206 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800207
Robert Ginda57f03b42012-09-13 11:02:48 -0700208 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
209 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800210 'alt-gr-mode': function(v) {
211 if (v == null) {
212 if (navigator.language.toLowerCase() == 'en-us') {
213 v = 'none';
214 } else {
215 v = 'right-alt';
216 }
217 } else if (typeof v == 'string') {
218 v = v.toLowerCase();
219 } else {
220 v = 'none';
221 }
222
223 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v))
224 v = 'none';
225
226 terminal.keyboard.altGrMode = v;
227 },
228
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700229 'alt-backspace-is-meta-backspace': function(v) {
230 terminal.keyboard.altBackspaceIsMetaBackspace = v;
231 },
232
Robert Ginda57f03b42012-09-13 11:02:48 -0700233 'alt-is-meta': function(v) {
234 terminal.keyboard.altIsMeta = v;
235 },
236
237 'alt-sends-what': function(v) {
238 if (!/^(escape|8-bit|browser-key)$/.test(v))
239 v = 'escape';
240
241 terminal.keyboard.altSendsWhat = v;
242 },
243
244 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800245 var ary = v.match(/^lib-resource:(\S+)/);
246 if (ary) {
247 terminal.bellAudio_.setAttribute('src',
248 lib.resource.getDataUrl(ary[1]));
249 } else {
250 terminal.bellAudio_.setAttribute('src', v);
251 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700252 },
253
Michael Kelly485ecd12014-06-09 11:41:56 -0400254 'desktop-notification-bell': function(v) {
255 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700256 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400257 Notification.permission === 'granted';
258 if (!terminal.desktopNotificationBell_) {
259 // Note: We don't call Notification.requestPermission here because
260 // Chrome requires the call be the result of a user action (such as an
261 // onclick handler), and pref listeners are run asynchronously.
262 //
263 // A way of working around this would be to display a dialog in the
264 // terminal with a "click-to-request-permission" button.
265 console.warn('desktop-notification-bell is true but we do not have ' +
266 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400267 }
268 } else {
269 terminal.desktopNotificationBell_ = false;
270 }
271 },
272
Robert Ginda57f03b42012-09-13 11:02:48 -0700273 'background-color': function(v) {
274 terminal.setBackgroundColor(v);
275 },
276
277 'background-image': function(v) {
278 terminal.scrollPort_.setBackgroundImage(v);
279 },
280
281 'background-size': function(v) {
282 terminal.scrollPort_.setBackgroundSize(v);
283 },
284
285 'background-position': function(v) {
286 terminal.scrollPort_.setBackgroundPosition(v);
287 },
288
289 'backspace-sends-backspace': function(v) {
290 terminal.keyboard.backspaceSendsBackspace = v;
291 },
292
Brad Town18654b62015-03-12 00:27:45 -0700293 'character-map-overrides': function(v) {
294 if (!(v == null || v instanceof Object)) {
295 console.warn('Preference character-map-modifications is not an ' +
296 'object: ' + v);
297 return;
298 }
299
Mike Frysinger095d4062017-06-14 00:29:48 -0700300 terminal.vt.characterMaps.reset();
301 terminal.vt.characterMaps.setOverrides(v);
Brad Town18654b62015-03-12 00:27:45 -0700302 },
303
Robert Ginda57f03b42012-09-13 11:02:48 -0700304 'cursor-blink': function(v) {
305 terminal.setCursorBlink(!!v);
306 },
307
Joel Hockey9d10ba12019-05-28 01:25:02 -0700308 'cursor-shape': function(v) {
309 terminal.setCursorShape(v);
310 },
311
Robert Gindaea2183e2014-07-17 09:51:51 -0700312 'cursor-blink-cycle': function(v) {
313 if (v instanceof Array &&
314 typeof v[0] == 'number' &&
315 typeof v[1] == 'number') {
316 terminal.cursorBlinkCycle_ = v;
317 } else if (typeof v == 'number') {
318 terminal.cursorBlinkCycle_ = [v, v];
319 } else {
320 // Fast blink indicates an error.
321 terminal.cursorBlinkCycle_ = [100, 100];
322 }
323 },
324
Robert Ginda57f03b42012-09-13 11:02:48 -0700325 'cursor-color': function(v) {
326 terminal.setCursorColor(v);
327 },
328
329 'color-palette-overrides': function(v) {
330 if (!(v == null || v instanceof Object || v instanceof Array)) {
331 console.warn('Preference color-palette-overrides is not an array or ' +
332 'object: ' + v);
333 return;
rginda9f5222b2012-03-05 11:53:28 -0800334 }
rginda9f5222b2012-03-05 11:53:28 -0800335
Robert Ginda57f03b42012-09-13 11:02:48 -0700336 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700337
Robert Ginda57f03b42012-09-13 11:02:48 -0700338 if (v) {
339 for (var key in v) {
340 var i = parseInt(key);
341 if (isNaN(i) || i < 0 || i > 255) {
342 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
343 continue;
344 }
345
346 if (v[i]) {
347 var rgb = lib.colors.normalizeCSS(v[i]);
348 if (rgb)
349 lib.colors.colorPalette[i] = rgb;
350 }
351 }
rginda30f20f62012-04-05 16:36:19 -0700352 }
rginda30f20f62012-04-05 16:36:19 -0700353
Evan Jones5f9df812016-12-06 09:38:58 -0500354 terminal.primaryScreen_.textAttributes.resetColorPalette();
Robert Ginda57f03b42012-09-13 11:02:48 -0700355 terminal.alternateScreen_.textAttributes.resetColorPalette();
356 },
rginda30f20f62012-04-05 16:36:19 -0700357
Robert Ginda57f03b42012-09-13 11:02:48 -0700358 'copy-on-select': function(v) {
359 terminal.copyOnSelect = !!v;
360 },
rginda9f5222b2012-03-05 11:53:28 -0800361
Rob Spies0bec09b2014-06-06 15:58:09 -0700362 'use-default-window-copy': function(v) {
363 terminal.useDefaultWindowCopy = !!v;
364 },
365
366 'clear-selection-after-copy': function(v) {
367 terminal.clearSelectionAfterCopy = !!v;
368 },
369
Robert Ginda7e5e9522014-03-14 12:23:58 -0700370 'ctrl-plus-minus-zero-zoom': function(v) {
371 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
372 },
373
Robert Gindafb5a3f92014-05-13 14:12:00 -0700374 'ctrl-c-copy': function(v) {
375 terminal.keyboard.ctrlCCopy = v;
376 },
377
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100378 'ctrl-v-paste': function(v) {
379 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700380 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100381 },
382
Cody Coljee-Gray7c6a0392018-10-25 13:18:28 -0700383 'paste-on-drop': function(v) {
384 terminal.scrollPort_.setPasteOnDrop(v);
385 },
386
Masaya Suzuki273aa982014-05-31 07:25:55 +0900387 'east-asian-ambiguous-as-two-column': function(v) {
388 lib.wc.regardCjkAmbiguous = v;
389 },
390
Robert Ginda57f03b42012-09-13 11:02:48 -0700391 'enable-8-bit-control': function(v) {
392 terminal.vt.enable8BitControl = !!v;
393 },
rginda30f20f62012-04-05 16:36:19 -0700394
Robert Ginda57f03b42012-09-13 11:02:48 -0700395 'enable-bold': function(v) {
396 terminal.syncBoldSafeState();
397 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400398
Robert Ginda3e278d72014-03-25 13:18:51 -0700399 'enable-bold-as-bright': function(v) {
400 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
401 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
402 },
403
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400404 'enable-blink': function(v) {
Mike Frysinger261597c2017-12-28 01:14:21 -0500405 terminal.setTextBlink(!!v);
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400406 },
407
Robert Ginda57f03b42012-09-13 11:02:48 -0700408 'enable-clipboard-write': function(v) {
409 terminal.vt.enableClipboardWrite = !!v;
410 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400411
Robert Ginda3755e752013-05-31 13:34:09 -0700412 'enable-dec12': function(v) {
413 terminal.vt.enableDec12 = !!v;
414 },
415
Mike Frysinger38f267d2018-09-07 02:50:59 -0400416 'enable-csi-j-3': function(v) {
417 terminal.vt.enableCsiJ3 = !!v;
418 },
419
Robert Ginda57f03b42012-09-13 11:02:48 -0700420 'font-family': function(v) {
421 terminal.syncFontFamily();
422 },
rginda30f20f62012-04-05 16:36:19 -0700423
Robert Ginda57f03b42012-09-13 11:02:48 -0700424 'font-size': function(v) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500425 v = parseInt(v);
426 if (v <= 0) {
427 console.error(`Invalid font size: ${v}`);
428 return;
429 }
430
Robert Ginda57f03b42012-09-13 11:02:48 -0700431 terminal.setFontSize(v);
432 },
rginda9875d902012-08-20 16:21:57 -0700433
Robert Ginda57f03b42012-09-13 11:02:48 -0700434 'font-smoothing': function(v) {
435 terminal.syncFontFamily();
436 },
rgindade84e382012-04-20 15:39:31 -0700437
Robert Ginda57f03b42012-09-13 11:02:48 -0700438 'foreground-color': function(v) {
439 terminal.setForegroundColor(v);
440 },
rginda30f20f62012-04-05 16:36:19 -0700441
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400442 'hide-mouse-while-typing': function(v) {
443 terminal.setAutomaticMouseHiding(v);
444 },
445
Robert Ginda57f03b42012-09-13 11:02:48 -0700446 'home-keys-scroll': function(v) {
447 terminal.keyboard.homeKeysScroll = v;
448 },
rginda4bba5e12012-06-20 16:15:30 -0700449
Robert Gindaa8165692015-06-15 14:46:31 -0700450 'keybindings': function(v) {
451 terminal.keyboard.bindings.clear();
452
453 if (!v)
454 return;
455
456 if (!(v instanceof Object)) {
457 console.error('Error in keybindings preference: Expected object');
458 return;
459 }
460
461 try {
462 terminal.keyboard.bindings.addBindings(v);
463 } catch (ex) {
464 console.error('Error in keybindings preference: ' + ex);
465 }
466 },
467
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700468 'media-keys-are-fkeys': function(v) {
469 terminal.keyboard.mediaKeysAreFKeys = v;
470 },
471
Robert Ginda57f03b42012-09-13 11:02:48 -0700472 'meta-sends-escape': function(v) {
473 terminal.keyboard.metaSendsEscape = v;
474 },
rginda30f20f62012-04-05 16:36:19 -0700475
Mike Frysinger847577f2017-05-23 23:25:57 -0400476 'mouse-right-click-paste': function(v) {
477 terminal.mouseRightClickPaste = v;
478 },
479
Robert Ginda57f03b42012-09-13 11:02:48 -0700480 'mouse-paste-button': function(v) {
481 terminal.syncMousePasteButton();
482 },
rgindaa8ba17d2012-08-15 14:41:10 -0700483
Robert Gindae76aa9f2014-03-14 12:29:12 -0700484 'page-keys-scroll': function(v) {
485 terminal.keyboard.pageKeysScroll = v;
486 },
487
Robert Ginda40932892012-12-10 17:26:40 -0800488 'pass-alt-number': function(v) {
489 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800490 // Let Alt-1..9 pass to the browser (to control tab switching) on
491 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500492 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800493 }
494
495 terminal.passAltNumber = v;
496 },
497
498 'pass-ctrl-number': function(v) {
499 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800500 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
501 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500502 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800503 }
504
505 terminal.passCtrlNumber = v;
506 },
507
508 'pass-meta-number': function(v) {
509 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800510 // Let Meta-1..9 pass to the browser (to control tab switching) on
511 // OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500512 v = (hterm.os == 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800513 }
514
515 terminal.passMetaNumber = v;
516 },
517
Marius Schilder77857b32014-05-14 16:21:26 -0700518 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700519 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700520 },
521
Robert Ginda8cb7d902013-06-20 14:37:18 -0700522 'receive-encoding': function(v) {
523 if (!(/^(utf-8|raw)$/).test(v)) {
524 console.warn('Invalid value for "receive-encoding": ' + v);
525 v = 'utf-8';
526 }
527
528 terminal.vt.characterEncoding = v;
529 },
530
Robert Ginda57f03b42012-09-13 11:02:48 -0700531 'scroll-on-keystroke': function(v) {
532 terminal.scrollOnKeystroke_ = v;
533 },
rginda9f5222b2012-03-05 11:53:28 -0800534
Robert Ginda57f03b42012-09-13 11:02:48 -0700535 'scroll-on-output': function(v) {
536 terminal.scrollOnOutput_ = v;
537 },
rginda30f20f62012-04-05 16:36:19 -0700538
Robert Ginda57f03b42012-09-13 11:02:48 -0700539 'scrollbar-visible': function(v) {
540 terminal.setScrollbarVisible(v);
541 },
rginda9f5222b2012-03-05 11:53:28 -0800542
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400543 'scroll-wheel-may-send-arrow-keys': function(v) {
544 terminal.scrollWheelArrowKeys_ = v;
545 },
546
Rob Spies49039e52014-12-17 13:40:04 -0800547 'scroll-wheel-move-multiplier': function(v) {
548 terminal.setScrollWheelMoveMultipler(v);
549 },
550
Robert Ginda57f03b42012-09-13 11:02:48 -0700551 'shift-insert-paste': function(v) {
552 terminal.keyboard.shiftInsertPaste = v;
553 },
rginda9f5222b2012-03-05 11:53:28 -0800554
Mike Frysingera7768922017-07-28 15:00:12 -0400555 'terminal-encoding': function(v) {
Mike Frysingera1371e12017-08-17 01:37:17 -0400556 terminal.vt.setEncoding(v);
Mike Frysingera7768922017-07-28 15:00:12 -0400557 },
558
Robert Gindae76aa9f2014-03-14 12:29:12 -0700559 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400560 terminal.scrollPort_.setUserCssUrl(v);
561 },
562
563 'user-css-text': function(v) {
564 terminal.scrollPort_.setUserCssText(v);
565 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400566
567 'word-break-match-left': function(v) {
568 terminal.primaryScreen_.wordBreakMatchLeft = v;
569 terminal.alternateScreen_.wordBreakMatchLeft = v;
570 },
571
572 'word-break-match-right': function(v) {
573 terminal.primaryScreen_.wordBreakMatchRight = v;
574 terminal.alternateScreen_.wordBreakMatchRight = v;
575 },
576
577 'word-break-match-middle': function(v) {
578 terminal.primaryScreen_.wordBreakMatchMiddle = v;
579 terminal.alternateScreen_.wordBreakMatchMiddle = v;
580 },
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400581
582 'allow-images-inline': function(v) {
583 terminal.allowImagesInline = v;
584 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700585 });
rginda30f20f62012-04-05 16:36:19 -0700586
Robert Ginda57f03b42012-09-13 11:02:48 -0700587 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800588 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700589
590 if (opt_callback)
591 opt_callback();
592 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800593};
594
Rob Spies56953412014-04-28 14:09:47 -0700595
596/**
597 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500598 *
599 * @return {hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700600 */
601hterm.Terminal.prototype.getPrefs = function() {
602 return this.prefs_;
603};
604
Robert Gindaa063b202014-07-21 11:08:25 -0700605/**
606 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500607 *
608 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700609 */
610hterm.Terminal.prototype.setBracketedPaste = function(state) {
611 this.options_.bracketedPaste = state;
612};
Rob Spies56953412014-04-28 14:09:47 -0700613
rginda8e92a692012-05-20 19:37:20 -0700614/**
615 * Set the color for the cursor.
616 *
617 * If you want this setting to persist, set it through prefs_, rather than
618 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500619 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500620 * @param {string=} color The color to set. If not defined, we reset to the
621 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700622 */
623hterm.Terminal.prototype.setCursorColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500624 if (color === undefined)
625 color = this.prefs_.get('cursor-color');
626
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400627 this.setCssVar('cursor-color', color);
rginda8e92a692012-05-20 19:37:20 -0700628};
629
630/**
631 * Return the current cursor color as a string.
Evan Jones2600d4f2016-12-06 09:29:36 -0500632 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700633 */
634hterm.Terminal.prototype.getCursorColor = function() {
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400635 return this.getCssVar('cursor-color');
rginda8e92a692012-05-20 19:37:20 -0700636};
637
638/**
rgindad5613292012-06-19 15:40:37 -0700639 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500640 *
641 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700642 */
643hterm.Terminal.prototype.setSelectionEnabled = function(state) {
644 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700645};
646
647/**
rginda8e92a692012-05-20 19:37:20 -0700648 * Set the background color.
649 *
650 * If you want this setting to persist, set it through prefs_, rather than
651 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500652 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500653 * @param {string=} color The color to set. If not defined, we reset to the
654 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700655 */
656hterm.Terminal.prototype.setBackgroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500657 if (color === undefined)
658 color = this.prefs_.get('background-color');
659
rgindacbbd7482012-06-13 15:06:16 -0700660 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700661 this.primaryScreen_.textAttributes.setDefaults(
662 this.foregroundColor_, this.backgroundColor_);
663 this.alternateScreen_.textAttributes.setDefaults(
664 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700665 this.scrollPort_.setBackgroundColor(color);
666};
667
rginda9f5222b2012-03-05 11:53:28 -0800668/**
669 * Return the current terminal background color.
670 *
671 * Intended for use by other classes, so we don't have to expose the entire
672 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500673 *
674 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800675 */
676hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700677 return this.backgroundColor_;
678};
679
680/**
681 * Set the foreground color.
682 *
683 * If you want this setting to persist, set it through prefs_, rather than
684 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500685 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500686 * @param {string=} color The color to set. If not defined, we reset to the
687 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700688 */
689hterm.Terminal.prototype.setForegroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500690 if (color === undefined)
691 color = this.prefs_.get('foreground-color');
692
rgindacbbd7482012-06-13 15:06:16 -0700693 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700694 this.primaryScreen_.textAttributes.setDefaults(
695 this.foregroundColor_, this.backgroundColor_);
696 this.alternateScreen_.textAttributes.setDefaults(
697 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700698 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800699};
700
701/**
702 * Return the current terminal foreground color.
703 *
704 * Intended for use by other classes, so we don't have to expose the entire
705 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500706 *
707 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800708 */
709hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700710 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800711};
712
713/**
rginda87b86462011-12-14 13:48:03 -0800714 * Create a new instance of a terminal command and run it with a given
715 * argument string.
716 *
717 * @param {function} commandClass The constructor for a terminal command.
718 * @param {string} argString The argument string to pass to the command.
719 */
720hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700721 var environment = this.prefs_.get('environment');
722 if (typeof environment != 'object' || environment == null)
723 environment = {};
724
rginda87b86462011-12-14 13:48:03 -0800725 var self = this;
726 this.command = new commandClass(
727 { argString: argString || '',
728 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700729 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800730 onExit: function(code) {
731 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800732 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700733 if (self.prefs_.get('close-on-exit'))
734 window.close();
rginda87b86462011-12-14 13:48:03 -0800735 }
736 });
737
rgindafeaf3142012-01-31 15:14:20 -0800738 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800739 this.command.run();
740};
741
742/**
rgindafeaf3142012-01-31 15:14:20 -0800743 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500744 *
745 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800746 */
747hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700748 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800749};
750
751/**
752 * Install the keyboard handler for this terminal.
753 *
754 * This will prevent the browser from seeing any keystrokes sent to the
755 * terminal.
756 */
757hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700758 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400759};
rgindafeaf3142012-01-31 15:14:20 -0800760
761/**
762 * Uninstall the keyboard handler for this terminal.
763 */
764hterm.Terminal.prototype.uninstallKeyboard = function() {
765 this.keyboard.installKeyboard(null);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400766};
rgindafeaf3142012-01-31 15:14:20 -0800767
768/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400769 * Set a CSS variable.
770 *
771 * Normally this is used to set variables in the hterm namespace.
772 *
773 * @param {string} name The variable to set.
774 * @param {string} value The value to assign to the variable.
775 * @param {string?} opt_prefix The variable namespace/prefix to use.
776 */
777hterm.Terminal.prototype.setCssVar = function(name, value,
778 opt_prefix='--hterm-') {
779 this.document_.documentElement.style.setProperty(
780 `${opt_prefix}${name}`, value);
781};
782
783/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500784 * Get a CSS variable.
785 *
786 * Normally this is used to get variables in the hterm namespace.
787 *
788 * @param {string} name The variable to read.
789 * @param {string?} opt_prefix The variable namespace/prefix to use.
790 * @return {string} The current setting for this variable.
791 */
792hterm.Terminal.prototype.getCssVar = function(name, opt_prefix='--hterm-') {
793 return this.document_.documentElement.style.getPropertyValue(
794 `${opt_prefix}${name}`);
795};
796
797/**
rginda35c456b2012-02-09 17:29:05 -0800798 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800799 *
800 * Call setFontSize(0) to reset to the default font size.
801 *
802 * This function does not modify the font-size preference.
803 *
804 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800805 */
806hterm.Terminal.prototype.setFontSize = function(px) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500807 if (px <= 0)
rginda9f5222b2012-03-05 11:53:28 -0800808 px = this.prefs_.get('font-size');
809
rginda35c456b2012-02-09 17:29:05 -0800810 this.scrollPort_.setFontSize(px);
Mike Frysingercce97c42017-08-05 01:11:22 -0400811 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
812 this.setCssVar('charsize-height',
813 this.scrollPort_.characterSize.height + 'px');
rginda35c456b2012-02-09 17:29:05 -0800814};
815
816/**
817 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500818 *
819 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800820 */
821hterm.Terminal.prototype.getFontSize = function() {
822 return this.scrollPort_.getFontSize();
823};
824
825/**
rginda8e92a692012-05-20 19:37:20 -0700826 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500827 *
828 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700829 */
830hterm.Terminal.prototype.getFontFamily = function() {
831 return this.scrollPort_.getFontFamily();
832};
833
834/**
rginda35c456b2012-02-09 17:29:05 -0800835 * Set the CSS "font-family" for this terminal.
836 */
rginda9f5222b2012-03-05 11:53:28 -0800837hterm.Terminal.prototype.syncFontFamily = function() {
838 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
839 this.prefs_.get('font-smoothing'));
840 this.syncBoldSafeState();
841};
842
rginda4bba5e12012-06-20 16:15:30 -0700843/**
844 * Set this.mousePasteButton based on the mouse-paste-button pref,
845 * autodetecting if necessary.
846 */
847hterm.Terminal.prototype.syncMousePasteButton = function() {
848 var button = this.prefs_.get('mouse-paste-button');
849 if (typeof button == 'number') {
850 this.mousePasteButton = button;
851 return;
852 }
853
Mike Frysingeree81a002017-12-12 16:14:53 -0500854 if (hterm.os != 'linux') {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400855 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700856 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400857 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700858 }
859};
860
861/**
862 * Enable or disable bold based on the enable-bold pref, autodetecting if
863 * necessary.
864 */
rginda9f5222b2012-03-05 11:53:28 -0800865hterm.Terminal.prototype.syncBoldSafeState = function() {
866 var enableBold = this.prefs_.get('enable-bold');
867 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700868 this.primaryScreen_.textAttributes.enableBold = enableBold;
869 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800870 return;
871 }
872
rgindaf7521392012-02-28 17:20:34 -0800873 var normalSize = this.scrollPort_.measureCharacterSize();
874 var boldSize = this.scrollPort_.measureCharacterSize('bold');
875
876 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800877 if (!isBoldSafe) {
878 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700879 'from normal. Font family is: ' +
880 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800881 }
rginda9f5222b2012-03-05 11:53:28 -0800882
Robert Gindaed016262012-10-26 16:27:09 -0700883 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
884 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800885};
886
887/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500888 * Control text blinking behavior.
889 *
890 * @param {boolean=} state Whether to enable support for blinking text.
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400891 */
Mike Frysinger261597c2017-12-28 01:14:21 -0500892hterm.Terminal.prototype.setTextBlink = function(state) {
893 if (state === undefined)
894 state = this.prefs_.get('enable-blink');
895 this.setCssVar('blink-node-duration', state ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400896};
897
898/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400899 * Set the mouse cursor style based on the current terminal mode.
900 */
901hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -0400902 this.setCssVar('mouse-cursor-style',
903 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
904 'var(--hterm-mouse-cursor-text)' :
Mike Frysinger67f58f82018-11-22 13:38:22 -0500905 'var(--hterm-mouse-cursor-default)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400906};
907
908/**
rginda87b86462011-12-14 13:48:03 -0800909 * Return a copy of the current cursor position.
910 *
911 * @return {hterm.RowCol} The RowCol object representing the current position.
912 */
913hterm.Terminal.prototype.saveCursor = function() {
914 return this.screen_.cursorPosition.clone();
915};
916
Evan Jones2600d4f2016-12-06 09:29:36 -0500917/**
918 * Return the current text attributes.
919 *
920 * @return {string}
921 */
rgindaa19afe22012-01-25 15:40:22 -0800922hterm.Terminal.prototype.getTextAttributes = function() {
923 return this.screen_.textAttributes;
924};
925
Evan Jones2600d4f2016-12-06 09:29:36 -0500926/**
927 * Set the text attributes.
928 *
929 * @param {string} textAttributes The attributes to set.
930 */
rginda1a09aa02012-06-18 21:11:25 -0700931hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
932 this.screen_.textAttributes = textAttributes;
933};
934
rginda87b86462011-12-14 13:48:03 -0800935/**
rgindaf522ce02012-04-17 17:49:17 -0700936 * Return the current browser zoom factor applied to the terminal.
937 *
938 * @return {number} The current browser zoom factor.
939 */
940hterm.Terminal.prototype.getZoomFactor = function() {
941 return this.scrollPort_.characterSize.zoomFactor;
942};
943
944/**
rginda9846e2f2012-01-27 13:53:33 -0800945 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -0500946 *
947 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -0800948 */
949hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800950 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800951};
952
953/**
rginda87b86462011-12-14 13:48:03 -0800954 * Restore a previously saved cursor position.
955 *
956 * @param {hterm.RowCol} cursor The position to restore.
957 */
958hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700959 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
960 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800961 this.screen_.setCursorPosition(row, column);
962 if (cursor.column > column ||
963 cursor.column == column && cursor.overflow) {
964 this.screen_.cursorPosition.overflow = true;
965 }
rginda87b86462011-12-14 13:48:03 -0800966};
967
968/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400969 * Clear the cursor's overflow flag.
970 */
971hterm.Terminal.prototype.clearCursorOverflow = function() {
972 this.screen_.cursorPosition.overflow = false;
973};
974
975/**
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800976 * Save the current cursor state to the corresponding screens.
977 *
978 * See the hterm.Screen.CursorState class for more details.
979 *
980 * @param {boolean=} both If true, update both screens, else only update the
981 * current screen.
982 */
983hterm.Terminal.prototype.saveCursorAndState = function(both) {
984 if (both) {
985 this.primaryScreen_.saveCursorAndState(this.vt);
986 this.alternateScreen_.saveCursorAndState(this.vt);
987 } else
988 this.screen_.saveCursorAndState(this.vt);
989};
990
991/**
992 * Restore the saved cursor state in the corresponding screens.
993 *
994 * See the hterm.Screen.CursorState class for more details.
995 *
996 * @param {boolean=} both If true, update both screens, else only update the
997 * current screen.
998 */
999hterm.Terminal.prototype.restoreCursorAndState = function(both) {
1000 if (both) {
1001 this.primaryScreen_.restoreCursorAndState(this.vt);
1002 this.alternateScreen_.restoreCursorAndState(this.vt);
1003 } else
1004 this.screen_.restoreCursorAndState(this.vt);
1005};
1006
1007/**
Robert Ginda830583c2013-08-07 13:20:46 -07001008 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001009 *
1010 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -07001011 */
1012hterm.Terminal.prototype.setCursorShape = function(shape) {
1013 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001014 this.restyleCursor_();
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001015};
Robert Ginda830583c2013-08-07 13:20:46 -07001016
1017/**
1018 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001019 *
1020 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -07001021 */
1022hterm.Terminal.prototype.getCursorShape = function() {
1023 return this.cursorShape_;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001024};
Robert Ginda830583c2013-08-07 13:20:46 -07001025
1026/**
rginda87b86462011-12-14 13:48:03 -08001027 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001028 *
1029 * @param {number} columnCount
rginda87b86462011-12-14 13:48:03 -08001030 */
1031hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -08001032 if (columnCount == null) {
1033 this.div_.style.width = '100%';
1034 return;
1035 }
1036
Robert Ginda26806d12014-07-24 13:44:07 -07001037 this.div_.style.width = Math.ceil(
1038 this.scrollPort_.characterSize.width *
1039 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001040 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -08001041 this.scheduleSyncCursorPosition_();
1042};
rginda87b86462011-12-14 13:48:03 -08001043
rgindac9bc5502012-01-18 11:48:44 -08001044/**
rginda35c456b2012-02-09 17:29:05 -08001045 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001046 *
1047 * @param {number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -08001048 */
1049hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -08001050 if (rowCount == null) {
1051 this.div_.style.height = '100%';
1052 return;
1053 }
1054
rginda35c456b2012-02-09 17:29:05 -08001055 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -07001056 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -08001057 this.realizeSize_(this.screenSize.width, rowCount);
1058 this.scheduleSyncCursorPosition_();
1059};
1060
1061/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001062 * Deal with terminal size changes.
1063 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001064 * @param {number} columnCount The number of columns.
1065 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001066 */
1067hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
Mike Frysinger0206e262019-06-13 10:18:19 -04001068 let notify = false;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001069
Mike Frysinger0206e262019-06-13 10:18:19 -04001070 if (columnCount != this.screenSize.width) {
1071 notify = true;
1072 this.realizeWidth_(columnCount);
1073 }
1074
1075 if (rowCount != this.screenSize.height) {
1076 notify = true;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001077 this.realizeHeight_(rowCount);
Mike Frysinger0206e262019-06-13 10:18:19 -04001078 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001079
1080 // Send new terminal size to plugin.
Mike Frysinger0206e262019-06-13 10:18:19 -04001081 if (notify) {
1082 this.io.onTerminalResize_(columnCount, rowCount);
1083 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001084};
1085
1086/**
rgindac9bc5502012-01-18 11:48:44 -08001087 * Deal with terminal width changes.
1088 *
1089 * This function does what needs to be done when the terminal width changes
1090 * out from under us. It happens here rather than in onResize_() because this
1091 * code may need to run synchronously to handle programmatic changes of
1092 * terminal width.
1093 *
1094 * Relying on the browser to send us an async resize event means we may not be
1095 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001096 *
1097 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001098 */
1099hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001100 if (columnCount <= 0)
1101 throw new Error('Attempt to realize bad width: ' + columnCount);
1102
rgindac9bc5502012-01-18 11:48:44 -08001103 var deltaColumns = columnCount - this.screen_.getWidth();
Mike Frysinger0206e262019-06-13 10:18:19 -04001104 if (deltaColumns == 0) {
1105 // No change, so don't bother recalculating things.
1106 return;
1107 }
rgindac9bc5502012-01-18 11:48:44 -08001108
rginda87b86462011-12-14 13:48:03 -08001109 this.screenSize.width = columnCount;
1110 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001111
1112 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -04001113 if (this.defaultTabStops)
1114 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -08001115 } else {
1116 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -04001117 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -08001118 break;
1119
1120 this.tabStops_.pop();
1121 }
1122 }
1123
1124 this.screen_.setColumnCount(this.screenSize.width);
1125};
1126
1127/**
1128 * Deal with terminal height changes.
1129 *
1130 * This function does what needs to be done when the terminal height changes
1131 * out from under us. It happens here rather than in onResize_() because this
1132 * code may need to run synchronously to handle programmatic changes of
1133 * terminal height.
1134 *
1135 * Relying on the browser to send us an async resize event means we may not be
1136 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001137 *
1138 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001139 */
1140hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001141 if (rowCount <= 0)
1142 throw new Error('Attempt to realize bad height: ' + rowCount);
1143
rgindac9bc5502012-01-18 11:48:44 -08001144 var deltaRows = rowCount - this.screen_.getHeight();
Mike Frysinger0206e262019-06-13 10:18:19 -04001145 if (deltaRows == 0) {
1146 // No change, so don't bother recalculating things.
1147 return;
1148 }
rgindac9bc5502012-01-18 11:48:44 -08001149
1150 this.screenSize.height = rowCount;
1151
1152 var cursor = this.saveCursor();
1153
1154 if (deltaRows < 0) {
1155 // Screen got smaller.
1156 deltaRows *= -1;
1157 while (deltaRows) {
1158 var lastRow = this.getRowCount() - 1;
1159 if (lastRow - this.scrollbackRows_.length == cursor.row)
1160 break;
1161
1162 if (this.getRowText(lastRow))
1163 break;
1164
1165 this.screen_.popRow();
1166 deltaRows--;
1167 }
1168
1169 var ary = this.screen_.shiftRows(deltaRows);
1170 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1171
1172 // We just removed rows from the top of the screen, we need to update
1173 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001174 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001175 } else if (deltaRows > 0) {
1176 // Screen got larger.
1177
1178 if (deltaRows <= this.scrollbackRows_.length) {
1179 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1180 var rows = this.scrollbackRows_.splice(
1181 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1182 this.screen_.unshiftRows(rows);
1183 deltaRows -= scrollbackCount;
1184 cursor.row += scrollbackCount;
1185 }
1186
1187 if (deltaRows)
1188 this.appendRows_(deltaRows);
1189 }
1190
rginda35c456b2012-02-09 17:29:05 -08001191 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001192 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001193};
1194
1195/**
1196 * Scroll the terminal to the top of the scrollback buffer.
1197 */
1198hterm.Terminal.prototype.scrollHome = function() {
1199 this.scrollPort_.scrollRowToTop(0);
1200};
1201
1202/**
1203 * Scroll the terminal to the end.
1204 */
1205hterm.Terminal.prototype.scrollEnd = function() {
1206 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1207};
1208
1209/**
1210 * Scroll the terminal one page up (minus one line) relative to the current
1211 * position.
1212 */
1213hterm.Terminal.prototype.scrollPageUp = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001214 this.scrollPort_.scrollPageUp();
rginda87b86462011-12-14 13:48:03 -08001215};
1216
1217/**
1218 * Scroll the terminal one page down (minus one line) relative to the current
1219 * position.
1220 */
1221hterm.Terminal.prototype.scrollPageDown = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001222 this.scrollPort_.scrollPageDown();
rginda8ba33642011-12-14 12:31:31 -08001223};
1224
rgindac9bc5502012-01-18 11:48:44 -08001225/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001226 * Scroll the terminal one line up relative to the current position.
1227 */
1228hterm.Terminal.prototype.scrollLineUp = function() {
1229 var i = this.scrollPort_.getTopRowIndex();
1230 this.scrollPort_.scrollRowToTop(i - 1);
1231};
1232
1233/**
1234 * Scroll the terminal one line down relative to the current position.
1235 */
1236hterm.Terminal.prototype.scrollLineDown = function() {
1237 var i = this.scrollPort_.getTopRowIndex();
1238 this.scrollPort_.scrollRowToTop(i + 1);
1239};
1240
1241/**
Robert Ginda40932892012-12-10 17:26:40 -08001242 * Clear primary screen, secondary screen, and the scrollback buffer.
1243 */
1244hterm.Terminal.prototype.wipeContents = function() {
Mike Frysinger9c482b82018-09-07 02:49:36 -04001245 this.clearHome(this.primaryScreen_);
1246 this.clearHome(this.alternateScreen_);
1247
1248 this.clearScrollback();
1249};
1250
1251/**
1252 * Clear scrollback buffer.
1253 */
1254hterm.Terminal.prototype.clearScrollback = function() {
1255 // Move to the end of the buffer in case the screen was scrolled back.
1256 // We're going to throw it away which would leave the display invalid.
1257 this.scrollEnd();
1258
Robert Ginda40932892012-12-10 17:26:40 -08001259 this.scrollbackRows_.length = 0;
1260 this.scrollPort_.resetCache();
1261
Mike Frysinger9c482b82018-09-07 02:49:36 -04001262 [this.primaryScreen_, this.alternateScreen_].forEach((screen) => {
1263 const bottom = screen.getHeight();
1264 this.renumberRows_(0, bottom, screen);
1265 });
Robert Ginda40932892012-12-10 17:26:40 -08001266
1267 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001268 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001269};
1270
1271/**
rgindac9bc5502012-01-18 11:48:44 -08001272 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001273 *
1274 * Perform a full reset to the default values listed in
1275 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001276 */
rginda87b86462011-12-14 13:48:03 -08001277hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001278 this.vt.reset();
1279
rgindac9bc5502012-01-18 11:48:44 -08001280 this.clearAllTabStops();
1281 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001282
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001283 const resetScreen = (screen) => {
1284 // We want to make sure to reset the attributes before we clear the screen.
1285 // The attributes might be used to initialize default/empty rows.
1286 screen.textAttributes.reset();
1287 screen.textAttributes.resetColorPalette();
1288 this.clearHome(screen);
1289 screen.saveCursorAndState(this.vt);
1290 };
1291 resetScreen(this.primaryScreen_);
1292 resetScreen(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001293
Mike Frysinger84301d02017-11-29 13:28:46 -08001294 // Reset terminal options to their default values.
1295 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001296 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1297
Mike Frysinger84301d02017-11-29 13:28:46 -08001298 this.setVTScrollRegion(null, null);
1299
1300 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001301};
1302
rgindac9bc5502012-01-18 11:48:44 -08001303/**
1304 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001305 *
1306 * Perform a soft reset to the default values listed in
1307 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001308 */
rginda0f5c0292012-01-13 11:00:13 -08001309hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001310 this.vt.reset();
1311
rgindab8bc8932012-04-27 12:45:03 -07001312 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001313 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001314
Brad Townb62dfdc2015-03-16 19:07:15 -07001315 // We show the cursor on soft reset but do not alter the blink state.
1316 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1317
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001318 const resetScreen = (screen) => {
1319 // Xterm also resets the color palette on soft reset, even though it doesn't
1320 // seem to be documented anywhere.
1321 screen.textAttributes.reset();
1322 screen.textAttributes.resetColorPalette();
1323 screen.saveCursorAndState(this.vt);
1324 };
1325 resetScreen(this.primaryScreen_);
1326 resetScreen(this.alternateScreen_);
rgindaf522ce02012-04-17 17:49:17 -07001327
rgindab8bc8932012-04-27 12:45:03 -07001328 // The xterm man page explicitly says this will happen on soft reset.
1329 this.setVTScrollRegion(null, null);
1330
1331 // Xterm also shows the cursor on soft reset, but does not alter the blink
1332 // state.
rgindaa19afe22012-01-25 15:40:22 -08001333 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001334};
1335
rgindac9bc5502012-01-18 11:48:44 -08001336/**
1337 * Move the cursor forward to the next tab stop, or to the last column
1338 * if no more tab stops are set.
1339 */
1340hterm.Terminal.prototype.forwardTabStop = function() {
1341 var column = this.screen_.cursorPosition.column;
1342
1343 for (var i = 0; i < this.tabStops_.length; i++) {
1344 if (this.tabStops_[i] > column) {
1345 this.setCursorColumn(this.tabStops_[i]);
1346 return;
1347 }
1348 }
1349
David Benjamin66e954d2012-05-05 21:08:12 -04001350 // xterm does not clear the overflow flag on HT or CHT.
1351 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001352 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001353 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001354};
1355
rgindac9bc5502012-01-18 11:48:44 -08001356/**
1357 * Move the cursor backward to the previous tab stop, or to the first column
1358 * if no previous tab stops are set.
1359 */
1360hterm.Terminal.prototype.backwardTabStop = function() {
1361 var column = this.screen_.cursorPosition.column;
1362
1363 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1364 if (this.tabStops_[i] < column) {
1365 this.setCursorColumn(this.tabStops_[i]);
1366 return;
1367 }
1368 }
1369
1370 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001371};
1372
rgindac9bc5502012-01-18 11:48:44 -08001373/**
1374 * Set a tab stop at the given column.
1375 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001376 * @param {integer} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001377 */
1378hterm.Terminal.prototype.setTabStop = function(column) {
1379 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1380 if (this.tabStops_[i] == column)
1381 return;
1382
1383 if (this.tabStops_[i] < column) {
1384 this.tabStops_.splice(i + 1, 0, column);
1385 return;
1386 }
1387 }
1388
1389 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001390};
1391
rgindac9bc5502012-01-18 11:48:44 -08001392/**
1393 * Clear the tab stop at the current cursor position.
1394 *
1395 * No effect if there is no tab stop at the current cursor position.
1396 */
1397hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1398 var column = this.screen_.cursorPosition.column;
1399
1400 var i = this.tabStops_.indexOf(column);
1401 if (i == -1)
1402 return;
1403
1404 this.tabStops_.splice(i, 1);
1405};
1406
1407/**
1408 * Clear all tab stops.
1409 */
1410hterm.Terminal.prototype.clearAllTabStops = function() {
1411 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001412 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001413};
1414
1415/**
1416 * Set up the default tab stops, starting from a given column.
1417 *
1418 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001419 * from the specified column, or 0 if no column is provided. It also flags
1420 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001421 *
1422 * This does not clear the existing tab stops first, use clearAllTabStops
1423 * for that.
1424 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001425 * @param {integer} opt_start Optional starting zero based starting column, useful
rgindac9bc5502012-01-18 11:48:44 -08001426 * for filling out missing tab stops when the terminal is resized.
1427 */
1428hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1429 var start = opt_start || 0;
1430 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001431 // Round start up to a default tab stop.
1432 start = start - 1 - ((start - 1) % w) + w;
1433 for (var i = start; i < this.screenSize.width; i += w) {
1434 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001435 }
David Benjamin66e954d2012-05-05 21:08:12 -04001436
1437 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001438};
1439
rginda6d397402012-01-17 10:58:29 -08001440/**
rginda8ba33642011-12-14 12:31:31 -08001441 * Interpret a sequence of characters.
1442 *
1443 * Incomplete escape sequences are buffered until the next call.
1444 *
1445 * @param {string} str Sequence of characters to interpret or pass through.
1446 */
1447hterm.Terminal.prototype.interpret = function(str) {
rginda8ba33642011-12-14 12:31:31 -08001448 this.scheduleSyncCursorPosition_();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001449 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001450};
1451
1452/**
1453 * Take over the given DIV for use as the terminal display.
1454 *
1455 * @param {HTMLDivElement} div The div to use as the terminal display.
1456 */
1457hterm.Terminal.prototype.decorate = function(div) {
Mike Frysinger5768a9d2017-12-26 12:57:44 -05001458 const charset = div.ownerDocument.characterSet.toLowerCase();
1459 if (charset != 'utf-8') {
1460 console.warn(`Document encoding should be set to utf-8, not "${charset}";` +
1461 ` Add <meta charset='utf-8'/> to your HTML <head> to fix.`);
1462 }
1463
rginda87b86462011-12-14 13:48:03 -08001464 this.div_ = div;
1465
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001466 this.accessibilityReader_ = new hterm.AccessibilityReader(div);
1467
Adrián Pérez-Orozco394e64f2018-12-17 17:20:16 -08001468 this.scrollPort_.decorate(div, () => this.setupScrollPort_());
1469};
1470
1471/**
1472 * Initialisation of ScrollPort properties which need to be set after its DOM
1473 * has been initialised.
1474 * @private
1475 */
1476hterm.Terminal.prototype.setupScrollPort_ = function() {
rginda30f20f62012-04-05 16:36:19 -07001477 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001478 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1479 this.scrollPort_.setBackgroundPosition(
1480 this.prefs_.get('background-position'));
Mike Frysinger08bad432017-04-24 00:50:54 -04001481 this.scrollPort_.setUserCssUrl(this.prefs_.get('user-css'));
1482 this.scrollPort_.setUserCssText(this.prefs_.get('user-css-text'));
Raymes Khoury177aec72018-06-26 10:58:53 +10001483 this.scrollPort_.setAccessibilityReader(this.accessibilityReader_);
rginda30f20f62012-04-05 16:36:19 -07001484
rginda0918b652012-04-04 11:26:24 -07001485 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001486
rginda9f5222b2012-03-05 11:53:28 -08001487 this.setFontSize(this.prefs_.get('font-size'));
1488 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001489
David Reveman8f552492012-03-28 12:18:41 -04001490 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001491 this.setScrollWheelMoveMultipler(
1492 this.prefs_.get('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001493
rginda8ba33642011-12-14 12:31:31 -08001494 this.document_ = this.scrollPort_.getDocument();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001495 this.accessibilityReader_.decorate(this.document_);
rginda8ba33642011-12-14 12:31:31 -08001496
Evan Jones5f9df812016-12-06 09:38:58 -05001497 this.document_.body.oncontextmenu = function() { return false; };
Mike Frysingercc114512017-09-11 21:39:17 -04001498 this.contextMenu.setDocument(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07001499
1500 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001501 var screenNode = this.scrollPort_.getScreenNode();
1502 screenNode.addEventListener('mousedown', onMouse);
1503 screenNode.addEventListener('mouseup', onMouse);
1504 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001505 this.scrollPort_.onScrollWheel = onMouse;
1506
Mike Frysinger02ded6d2018-06-21 14:25:20 -04001507 screenNode.addEventListener('keydown', this.onKeyboardActivity_.bind(this));
1508
Toni Barzic0bfa8922013-11-22 11:18:35 -08001509 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001510 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001511 // Listen for mousedown events on the screenNode as in FF the focus
1512 // events don't bubble.
1513 screenNode.addEventListener('mousedown', function() {
1514 setTimeout(this.onFocusChange_.bind(this, true));
1515 }.bind(this));
1516
Toni Barzic0bfa8922013-11-22 11:18:35 -08001517 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001518 'blur', this.onFocusChange_.bind(this, false));
1519
1520 var style = this.document_.createElement('style');
1521 style.textContent =
1522 ('.cursor-node[focus="false"] {' +
1523 ' box-sizing: border-box;' +
1524 ' background-color: transparent !important;' +
1525 ' border-width: 2px;' +
1526 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001527 '}' +
Mike Frysingercc114512017-09-11 21:39:17 -04001528 'menu {' +
1529 ' margin: 0;' +
1530 ' padding: 0;' +
1531 ' cursor: var(--hterm-mouse-cursor-pointer);' +
1532 '}' +
1533 'menuitem {' +
1534 ' white-space: nowrap;' +
1535 ' border-bottom: 1px dashed;' +
1536 ' display: block;' +
1537 ' padding: 0.3em 0.3em 0 0.3em;' +
1538 '}' +
1539 'menuitem.separator {' +
1540 ' border-bottom: none;' +
1541 ' height: 0.5em;' +
1542 ' padding: 0;' +
1543 '}' +
1544 'menuitem:hover {' +
1545 ' color: var(--hterm-cursor-color);' +
1546 '}' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001547 '.wc-node {' +
1548 ' display: inline-block;' +
1549 ' text-align: center;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001550 ' width: calc(var(--hterm-charsize-width) * 2);' +
Mike Frysinger4036f6e2017-05-31 14:02:55 -04001551 ' line-height: var(--hterm-charsize-height);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001552 '}' +
1553 ':root {' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001554 ' --hterm-charsize-width: ' + this.scrollPort_.characterSize.width + 'px;' +
1555 ' --hterm-charsize-height: ' + this.scrollPort_.characterSize.height + 'px;' +
Mike Frysingera27c0502017-08-23 22:37:10 -04001556 // Default position hides the cursor for when the window is initializing.
1557 ' --hterm-cursor-offset-col: -1;' +
1558 ' --hterm-cursor-offset-row: -1;' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001559 ' --hterm-blink-node-duration: 0.7s;' +
Mike Frysinger67f58f82018-11-22 13:38:22 -05001560 ' --hterm-mouse-cursor-default: default;' +
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001561 ' --hterm-mouse-cursor-text: text;' +
Mike Frysinger67f58f82018-11-22 13:38:22 -05001562 ' --hterm-mouse-cursor-pointer: pointer;' +
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001563 ' --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001564 '}' +
Mike Frysingerd3907202017-10-23 01:23:50 -04001565 '.uri-node:hover {' +
1566 ' text-decoration: underline;' +
Mike Frysinger67f58f82018-11-22 13:38:22 -05001567 ' cursor: var(--hterm-mouse-cursor-pointer);' +
Mike Frysingerd3907202017-10-23 01:23:50 -04001568 '}' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001569 '@keyframes blink {' +
1570 ' from { opacity: 1.0; }' +
1571 ' to { opacity: 0.0; }' +
1572 '}' +
1573 '.blink-node {' +
1574 ' animation-name: blink;' +
1575 ' animation-duration: var(--hterm-blink-node-duration);' +
1576 ' animation-iteration-count: infinite;' +
1577 ' animation-timing-function: ease-in-out;' +
1578 ' animation-direction: alternate;' +
rginda8e92a692012-05-20 19:37:20 -07001579 '}');
Mike Frysingerb74a6472018-06-22 13:37:08 -04001580 // Insert this stock style as the first node so that any user styles will
1581 // override w/out having to use !important everywhere. The rules above mix
1582 // runtime variables with default ones designed to be overridden by the user,
1583 // but we can wait for a concrete case from the users to determine the best
1584 // way to split the sheet up to before & after the user-css settings.
1585 this.document_.head.insertBefore(style, this.document_.head.firstChild);
rginda8e92a692012-05-20 19:37:20 -07001586
rginda8ba33642011-12-14 12:31:31 -08001587 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001588 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001589 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001590 this.cursorNode_.style.cssText =
1591 ('position: absolute;' +
Mike Frysinger44c32202017-08-05 01:13:09 -04001592 'left: calc(var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));' +
1593 'top: calc(var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));' +
Mike Frysingerd60f4c22018-03-15 21:25:30 -07001594 'display: ' + (this.options_.cursorVisible ? '' : 'none') + ';' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001595 'width: var(--hterm-charsize-width);' +
1596 'height: var(--hterm-charsize-height);' +
Mike Frysinger2fd079a2018-09-02 01:46:12 -04001597 'background-color: var(--hterm-cursor-color);' +
1598 'border-color: var(--hterm-cursor-color);' +
Rob Spies06533ba2014-04-24 11:20:37 -07001599 '-webkit-transition: opacity, background-color 100ms linear;' +
1600 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001601
Mike Frysingerf02a2cb2017-12-21 00:34:03 -05001602 this.setCursorColor();
Robert Gindafb1be6a2013-12-11 11:56:22 -08001603 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1604 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001605
rginda8ba33642011-12-14 12:31:31 -08001606 this.document_.body.appendChild(this.cursorNode_);
1607
rgindad5613292012-06-19 15:40:37 -07001608 // When 'enableMouseDragScroll' is off we reposition this element directly
1609 // under the mouse cursor after a click. This makes Chrome associate
1610 // subsequent mousemove events with the scroll-blocker. Since the
1611 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1612 // events do not cause the scrollport to scroll.
1613 //
1614 // It's a hack, but it's the cleanest way I could find.
1615 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001616 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
Raymes Khoury6dce2f82018-04-12 15:38:58 +10001617 this.scrollBlockerNode_.setAttribute('aria-hidden', 'true');
rgindad5613292012-06-19 15:40:37 -07001618 this.scrollBlockerNode_.style.cssText =
1619 ('position: absolute;' +
1620 'top: -99px;' +
1621 'display: block;' +
1622 'width: 10px;' +
1623 'height: 10px;');
1624 this.document_.body.appendChild(this.scrollBlockerNode_);
1625
rgindad5613292012-06-19 15:40:37 -07001626 this.scrollPort_.onScrollWheel = onMouse;
1627 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1628 ].forEach(function(event) {
1629 this.scrollBlockerNode_.addEventListener(event, onMouse);
1630 this.cursorNode_.addEventListener(event, onMouse);
1631 this.document_.addEventListener(event, onMouse);
1632 }.bind(this));
1633
1634 this.cursorNode_.addEventListener('mousedown', function() {
1635 setTimeout(this.focus.bind(this));
1636 }.bind(this));
1637
rginda8ba33642011-12-14 12:31:31 -08001638 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001639
rginda87b86462011-12-14 13:48:03 -08001640 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001641 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001642};
1643
rginda0918b652012-04-04 11:26:24 -07001644/**
1645 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001646 *
1647 * @return {HTMLDocument}
rginda0918b652012-04-04 11:26:24 -07001648 */
rginda87b86462011-12-14 13:48:03 -08001649hterm.Terminal.prototype.getDocument = function() {
1650 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001651};
1652
1653/**
rginda0918b652012-04-04 11:26:24 -07001654 * Focus the terminal.
1655 */
1656hterm.Terminal.prototype.focus = function() {
1657 this.scrollPort_.focus();
1658};
1659
1660/**
rginda8ba33642011-12-14 12:31:31 -08001661 * Return the HTML Element for a given row index.
1662 *
1663 * This is a method from the RowProvider interface. The ScrollPort uses
1664 * it to fetch rows on demand as they are scrolled into view.
1665 *
1666 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1667 * pairs to conserve memory.
1668 *
1669 * @param {integer} index The zero-based row index, measured relative to the
1670 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001671 * largest indices.
rginda8ba33642011-12-14 12:31:31 -08001672 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1673 */
1674hterm.Terminal.prototype.getRowNode = function(index) {
1675 if (index < this.scrollbackRows_.length)
1676 return this.scrollbackRows_[index];
1677
1678 var screenIndex = index - this.scrollbackRows_.length;
1679 return this.screen_.rowsArray[screenIndex];
1680};
1681
1682/**
1683 * Return the text content for a given range of rows.
1684 *
1685 * This is a method from the RowProvider interface. The ScrollPort uses
1686 * it to fetch text content on demand when the user attempts to copy their
1687 * selection to the clipboard.
1688 *
1689 * @param {integer} start The zero-based row index to start from, measured
1690 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001691 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001692 * @param {integer} end The zero-based row index to end on, measured
1693 * relative to the start of the scrollback buffer.
1694 * @return {string} A single string containing the text value of the range of
1695 * rows. Lines will be newline delimited, with no trailing newline.
1696 */
1697hterm.Terminal.prototype.getRowsText = function(start, end) {
1698 var ary = [];
1699 for (var i = start; i < end; i++) {
1700 var node = this.getRowNode(i);
1701 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001702 if (i < end - 1 && !node.getAttribute('line-overflow'))
1703 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001704 }
1705
rgindaa09e7332012-08-17 12:49:51 -07001706 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001707};
1708
1709/**
1710 * Return the text content for a given row.
1711 *
1712 * This is a method from the RowProvider interface. The ScrollPort uses
1713 * it to fetch text content on demand when the user attempts to copy their
1714 * selection to the clipboard.
1715 *
1716 * @param {integer} index The zero-based row index to return, measured
1717 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001718 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001719 * @return {string} A string containing the text value of the selected row.
1720 */
1721hterm.Terminal.prototype.getRowText = function(index) {
1722 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001723 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001724};
1725
1726/**
1727 * Return the total number of rows in the addressable screen and in the
1728 * scrollback buffer of this terminal.
1729 *
1730 * This is a method from the RowProvider interface. The ScrollPort uses
1731 * it to compute the size of the scrollbar.
1732 *
1733 * @return {integer} The number of rows in this terminal.
1734 */
1735hterm.Terminal.prototype.getRowCount = function() {
1736 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1737};
1738
1739/**
1740 * Create DOM nodes for new rows and append them to the end of the terminal.
1741 *
1742 * This is the only correct way to add a new DOM node for a row. Notice that
1743 * the new row is appended to the bottom of the list of rows, and does not
1744 * require renumbering (of the rowIndex property) of previous rows.
1745 *
1746 * If you think you want a new blank row somewhere in the middle of the
1747 * terminal, look into moveRows_().
1748 *
1749 * This method does not pay attention to vtScrollTop/Bottom, since you should
1750 * be using moveRows() in cases where they would matter.
1751 *
1752 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001753 *
1754 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001755 */
1756hterm.Terminal.prototype.appendRows_ = function(count) {
1757 var cursorRow = this.screen_.rowsArray.length;
1758 var offset = this.scrollbackRows_.length + cursorRow;
1759 for (var i = 0; i < count; i++) {
1760 var row = this.document_.createElement('x-row');
1761 row.appendChild(this.document_.createTextNode(''));
1762 row.rowIndex = offset + i;
1763 this.screen_.pushRow(row);
1764 }
1765
1766 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1767 if (extraRows > 0) {
1768 var ary = this.screen_.shiftRows(extraRows);
1769 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001770 if (this.scrollPort_.isScrolledEnd)
1771 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001772 }
1773
1774 if (cursorRow >= this.screen_.rowsArray.length)
1775 cursorRow = this.screen_.rowsArray.length - 1;
1776
rginda87b86462011-12-14 13:48:03 -08001777 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001778};
1779
1780/**
1781 * Relocate rows from one part of the addressable screen to another.
1782 *
1783 * This is used to recycle rows during VT scrolls (those which are driven
1784 * by VT commands, rather than by the user manipulating the scrollbar.)
1785 *
1786 * In this case, the blank lines scrolled into the scroll region are made of
1787 * the nodes we scrolled off. These have their rowIndex properties carefully
1788 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001789 *
1790 * @param {number} fromIndex The start index.
1791 * @param {number} count The number of rows to move.
1792 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001793 */
1794hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1795 var ary = this.screen_.removeRows(fromIndex, count);
1796 this.screen_.insertRows(toIndex, ary);
1797
1798 var start, end;
1799 if (fromIndex < toIndex) {
1800 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001801 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001802 } else {
1803 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001804 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001805 }
1806
1807 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001808 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001809};
1810
1811/**
1812 * Renumber the rowIndex property of the given range of rows.
1813 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001814 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08001815 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001816 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001817 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05001818 *
1819 * @param {number} start The start index.
1820 * @param {number} end The end index.
1821 * @param {hterm.Screen} opt_screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08001822 */
Robert Ginda40932892012-12-10 17:26:40 -08001823hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1824 var screen = opt_screen || this.screen_;
1825
rginda8ba33642011-12-14 12:31:31 -08001826 var offset = this.scrollbackRows_.length;
1827 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001828 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001829 }
1830};
1831
1832/**
1833 * Print a string to the terminal.
1834 *
1835 * This respects the current insert and wraparound modes. It will add new lines
1836 * to the end of the terminal, scrolling off the top into the scrollback buffer
1837 * if necessary.
1838 *
1839 * The string is *not* parsed for escape codes. Use the interpret() method if
1840 * that's what you're after.
1841 *
1842 * @param{string} str The string to print.
1843 */
1844hterm.Terminal.prototype.print = function(str) {
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001845 this.scheduleSyncCursorPosition_();
1846
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001847 // Basic accessibility output for the screen reader.
Raymes Khoury177aec72018-06-26 10:58:53 +10001848 this.accessibilityReader_.announce(str);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001849
rgindaa9abdd82012-08-06 18:05:09 -07001850 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001851
Ricky Liang48f05cb2013-12-31 23:35:29 +08001852 var strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04001853 // Fun edge case: If the string only contains zero width codepoints (like
1854 // combining characters), we make sure to iterate at least once below.
1855 if (strWidth == 0 && str)
1856 strWidth = 1;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001857
1858 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001859 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1860 this.screen_.commitLineOverflow();
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001861 this.newLine(true);
rgindaa09e7332012-08-17 12:49:51 -07001862 }
rgindaa19afe22012-01-25 15:40:22 -08001863
Ricky Liang48f05cb2013-12-31 23:35:29 +08001864 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001865 var didOverflow = false;
1866 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001867
rgindaa9abdd82012-08-06 18:05:09 -07001868 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1869 didOverflow = true;
1870 count = this.screenSize.width - this.screen_.cursorPosition.column;
1871 }
rgindaa19afe22012-01-25 15:40:22 -08001872
rgindaa9abdd82012-08-06 18:05:09 -07001873 if (didOverflow && !this.options_.wraparound) {
1874 // If the string overflowed the line but wraparound is off, then the
1875 // last printed character should be the last of the string.
1876 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001877 substr = lib.wc.substr(str, startOffset, count - 1) +
1878 lib.wc.substr(str, strWidth - 1);
1879 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001880 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001881 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001882 }
rgindaa19afe22012-01-25 15:40:22 -08001883
Ricky Liang48f05cb2013-12-31 23:35:29 +08001884 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1885 for (var i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001886 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
1887 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001888
1889 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001890 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001891 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001892 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001893 }
1894 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001895 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07001896 }
1897
1898 this.screen_.maybeClipCurrentRow();
1899 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001900 }
rginda8ba33642011-12-14 12:31:31 -08001901
rginda9f5222b2012-03-05 11:53:28 -08001902 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001903 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001904};
1905
1906/**
rginda87b86462011-12-14 13:48:03 -08001907 * Set the VT scroll region.
1908 *
rginda87b86462011-12-14 13:48:03 -08001909 * This also resets the cursor position to the absolute (0, 0) position, since
1910 * that's what xterm appears to do.
1911 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001912 * Setting the scroll region to the full height of the terminal will clear
1913 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1914 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1915 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1916 * continue to work as most users would expect.
1917 *
rginda87b86462011-12-14 13:48:03 -08001918 * @param {integer} scrollTop The zero-based top of the scroll region.
1919 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1920 * inclusive.
1921 */
1922hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001923 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001924 this.vtScrollTop_ = null;
1925 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001926 } else {
1927 this.vtScrollTop_ = scrollTop;
1928 this.vtScrollBottom_ = scrollBottom;
1929 }
rginda87b86462011-12-14 13:48:03 -08001930};
1931
1932/**
rginda8ba33642011-12-14 12:31:31 -08001933 * Return the top row index according to the VT.
1934 *
1935 * This will return 0 unless the terminal has been told to restrict scrolling
1936 * to some lower row. It is used for some VT cursor positioning and scrolling
1937 * commands.
1938 *
1939 * @return {integer} The topmost row in the terminal's scroll region.
1940 */
1941hterm.Terminal.prototype.getVTScrollTop = function() {
1942 if (this.vtScrollTop_ != null)
1943 return this.vtScrollTop_;
1944
1945 return 0;
rginda87b86462011-12-14 13:48:03 -08001946};
rginda8ba33642011-12-14 12:31:31 -08001947
1948/**
1949 * Return the bottom row index according to the VT.
1950 *
1951 * This will return the height of the terminal unless the it has been told to
1952 * restrict scrolling to some higher row. It is used for some VT cursor
1953 * positioning and scrolling commands.
1954 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001955 * @return {integer} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001956 */
1957hterm.Terminal.prototype.getVTScrollBottom = function() {
1958 if (this.vtScrollBottom_ != null)
1959 return this.vtScrollBottom_;
1960
rginda87b86462011-12-14 13:48:03 -08001961 return this.screenSize.height - 1;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001962};
rginda8ba33642011-12-14 12:31:31 -08001963
1964/**
1965 * Process a '\n' character.
1966 *
1967 * If the cursor is on the final row of the terminal this will append a new
1968 * blank row to the screen and scroll the topmost row into the scrollback
1969 * buffer.
1970 *
1971 * Otherwise, this moves the cursor to column zero of the next row.
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001972 *
1973 * @param {boolean=} dueToOverflow Whether the newline is due to wraparound of
1974 * the terminal.
rginda8ba33642011-12-14 12:31:31 -08001975 */
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001976hterm.Terminal.prototype.newLine = function(dueToOverflow = false) {
1977 if (!dueToOverflow)
1978 this.accessibilityReader_.newLine();
1979
Robert Ginda9937abc2013-07-25 16:09:23 -07001980 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1981 this.screen_.rowsArray.length - 1);
1982
1983 if (this.vtScrollBottom_ != null) {
1984 // A VT Scroll region is active, we never append new rows.
1985 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1986 // We're at the end of the VT Scroll Region, perform a VT scroll.
1987 this.vtScrollUp(1);
1988 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1989 } else if (cursorAtEndOfScreen) {
1990 // We're at the end of the screen, the only thing to do is put the
1991 // cursor to column 0.
1992 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1993 } else {
1994 // Anywhere else, advance the cursor row, and reset the column.
1995 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1996 }
1997 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001998 // We're at the end of the screen. Append a new row to the terminal,
1999 // shifting the top row into the scrollback.
2000 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08002001 } else {
rginda87b86462011-12-14 13:48:03 -08002002 // Anywhere else in the screen just moves the cursor.
2003 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08002004 }
2005};
2006
2007/**
2008 * Like newLine(), except maintain the cursor column.
2009 */
2010hterm.Terminal.prototype.lineFeed = function() {
2011 var column = this.screen_.cursorPosition.column;
2012 this.newLine();
2013 this.setCursorColumn(column);
2014};
2015
2016/**
rginda87b86462011-12-14 13:48:03 -08002017 * If autoCarriageReturn is set then newLine(), else lineFeed().
2018 */
2019hterm.Terminal.prototype.formFeed = function() {
2020 if (this.options_.autoCarriageReturn) {
2021 this.newLine();
2022 } else {
2023 this.lineFeed();
2024 }
2025};
2026
2027/**
2028 * Move the cursor up one row, possibly inserting a blank line.
2029 *
2030 * The cursor column is not changed.
2031 */
2032hterm.Terminal.prototype.reverseLineFeed = function() {
2033 var scrollTop = this.getVTScrollTop();
2034 var currentRow = this.screen_.cursorPosition.row;
2035
2036 if (currentRow == scrollTop) {
2037 this.insertLines(1);
2038 } else {
2039 this.setAbsoluteCursorRow(currentRow - 1);
2040 }
2041};
2042
2043/**
rginda8ba33642011-12-14 12:31:31 -08002044 * Replace all characters to the left of the current cursor with the space
2045 * character.
2046 *
2047 * TODO(rginda): This should probably *remove* the characters (not just replace
2048 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07002049 * position.
rginda8ba33642011-12-14 12:31:31 -08002050 */
2051hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08002052 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002053 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002054 const count = cursor.column + 1;
2055 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08002056 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002057};
2058
2059/**
David Benjamin684a9b72012-05-01 17:19:58 -04002060 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08002061 *
2062 * The cursor position is unchanged.
2063 *
Robert Gindaf2547f12012-10-25 20:36:21 -07002064 * If the current background color is not the default background color this
2065 * will insert spaces rather than delete. This is unfortunate because the
2066 * trailing space will affect text selection, but it's difficult to come up
2067 * with a way to style empty space that wouldn't trip up the hterm.Screen
2068 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07002069 *
2070 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
2071 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
2072 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05002073 *
2074 * @param {number} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08002075 */
2076hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07002077 if (this.screen_.cursorPosition.overflow)
2078 return;
2079
Robert Ginda7fd57082012-09-25 14:41:47 -07002080 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
2081 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07002082
2083 if (this.screen_.textAttributes.background ===
2084 this.screen_.textAttributes.DEFAULT_COLOR) {
2085 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08002086 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07002087 this.screen_.cursorPosition.column + count) {
2088 this.screen_.deleteChars(count);
2089 this.clearCursorOverflow();
2090 return;
2091 }
2092 }
2093
rginda87b86462011-12-14 13:48:03 -08002094 var cursor = this.saveCursor();
Mike Frysinger6380bed2017-08-24 18:46:39 -04002095 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08002096 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002097 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002098};
2099
2100/**
2101 * Erase the current line.
2102 *
2103 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002104 */
2105hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08002106 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002107 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08002108 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002109 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002110};
2111
2112/**
David Benjamina08d78f2012-05-05 00:28:49 -04002113 * Erase all characters from the start of the screen to the current cursor
2114 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002115 *
2116 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002117 */
2118hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08002119 var cursor = this.saveCursor();
2120
2121 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002122
David Benjamina08d78f2012-05-05 00:28:49 -04002123 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002124 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002125 this.screen_.clearCursorRow();
2126 }
2127
rginda87b86462011-12-14 13:48:03 -08002128 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002129 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002130};
2131
2132/**
2133 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002134 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002135 *
2136 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002137 */
2138hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08002139 var cursor = this.saveCursor();
2140
2141 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002142
David Benjamina08d78f2012-05-05 00:28:49 -04002143 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08002144 for (var i = cursor.row + 1; i <= bottom; i++) {
2145 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002146 this.screen_.clearCursorRow();
2147 }
2148
rginda87b86462011-12-14 13:48:03 -08002149 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002150 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002151};
2152
2153/**
2154 * Fill the terminal with a given character.
2155 *
2156 * This methods does not respect the VT scroll region.
2157 *
2158 * @param {string} ch The character to use for the fill.
2159 */
2160hterm.Terminal.prototype.fill = function(ch) {
2161 var cursor = this.saveCursor();
2162
2163 this.setAbsoluteCursorPosition(0, 0);
2164 for (var row = 0; row < this.screenSize.height; row++) {
2165 for (var col = 0; col < this.screenSize.width; col++) {
2166 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002167 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002168 }
2169 }
2170
2171 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002172};
2173
2174/**
rginda9ea433c2012-03-16 11:57:00 -07002175 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002176 *
rginda9ea433c2012-03-16 11:57:00 -07002177 * This does not respect the scroll region.
2178 *
2179 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2180 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002181 */
rginda9ea433c2012-03-16 11:57:00 -07002182hterm.Terminal.prototype.clearHome = function(opt_screen) {
2183 var screen = opt_screen || this.screen_;
2184 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002185
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002186 this.accessibilityReader_.clear();
2187
rginda11057d52012-04-25 12:29:56 -07002188 if (bottom == 0) {
2189 // Empty screen, nothing to do.
2190 return;
2191 }
2192
rgindae4d29232012-01-19 10:47:13 -08002193 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002194 screen.setCursorPosition(i, 0);
2195 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002196 }
2197
rginda9ea433c2012-03-16 11:57:00 -07002198 screen.setCursorPosition(0, 0);
2199};
2200
2201/**
2202 * Erase the entire display without changing the cursor position.
2203 *
2204 * The cursor position is unchanged. This does not respect the scroll
2205 * region.
2206 *
2207 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2208 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002209 */
2210hterm.Terminal.prototype.clear = function(opt_screen) {
2211 var screen = opt_screen || this.screen_;
2212 var cursor = screen.cursorPosition.clone();
2213 this.clearHome(screen);
2214 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002215};
2216
2217/**
2218 * VT command to insert lines at the current cursor row.
2219 *
2220 * This respects the current scroll region. Rows pushed off the bottom are
2221 * lost (they won't show up in the scrollback buffer).
2222 *
rginda8ba33642011-12-14 12:31:31 -08002223 * @param {integer} count The number of lines to insert.
2224 */
2225hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07002226 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002227
2228 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002229 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002230
Robert Ginda579186b2012-09-26 11:40:04 -07002231 // The moveCount is the number of rows we need to relocate to make room for
2232 // the new row(s). The count is the distance to move them.
2233 var moveCount = bottom - cursorRow - count + 1;
2234 if (moveCount)
2235 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08002236
Robert Ginda579186b2012-09-26 11:40:04 -07002237 for (var i = count - 1; i >= 0; i--) {
2238 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002239 this.screen_.clearCursorRow();
2240 }
rginda8ba33642011-12-14 12:31:31 -08002241};
2242
2243/**
2244 * VT command to delete lines at the current cursor row.
2245 *
2246 * New rows are added to the bottom of scroll region to take their place. New
2247 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002248 *
2249 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002250 */
2251hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08002252 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002253
rginda87b86462011-12-14 13:48:03 -08002254 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08002255 var bottom = this.getVTScrollBottom();
2256
rginda87b86462011-12-14 13:48:03 -08002257 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002258 count = Math.min(count, maxCount);
2259
rginda87b86462011-12-14 13:48:03 -08002260 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08002261 if (count != maxCount)
2262 this.moveRows_(top, count, moveStart);
2263
2264 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002265 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002266 this.screen_.clearCursorRow();
2267 }
2268
rginda87b86462011-12-14 13:48:03 -08002269 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002270 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002271};
2272
2273/**
2274 * Inserts the given number of spaces at the current cursor position.
2275 *
rginda87b86462011-12-14 13:48:03 -08002276 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002277 *
2278 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002279 */
2280hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08002281 var cursor = this.saveCursor();
2282
rgindacbbd7482012-06-13 15:06:16 -07002283 var ws = lib.f.getWhitespace(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002284 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002285 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002286
2287 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002288 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002289};
2290
2291/**
2292 * Forward-delete the specified number of characters starting at the cursor
2293 * position.
2294 *
2295 * @param {integer} count The number of characters to delete.
2296 */
2297hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002298 var deleted = this.screen_.deleteChars(count);
2299 if (deleted && !this.screen_.textAttributes.isDefault()) {
2300 var cursor = this.saveCursor();
2301 this.setCursorColumn(this.screenSize.width - deleted);
2302 this.screen_.insertString(lib.f.getWhitespace(deleted));
2303 this.restoreCursor(cursor);
2304 }
2305
David Benjamin54e8bf62012-06-01 22:31:40 -04002306 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002307};
2308
2309/**
2310 * Shift rows in the scroll region upwards by a given number of lines.
2311 *
2312 * New rows are inserted at the bottom of the scroll region to fill the
2313 * vacated rows. The new rows not filled out with the current text attributes.
2314 *
2315 * This function does not affect the scrollback rows at all. Rows shifted
2316 * off the top are lost.
2317 *
rginda87b86462011-12-14 13:48:03 -08002318 * The cursor position is not altered.
2319 *
rginda8ba33642011-12-14 12:31:31 -08002320 * @param {integer} count The number of rows to scroll.
2321 */
2322hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002323 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002324
rginda87b86462011-12-14 13:48:03 -08002325 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002326 this.deleteLines(count);
2327
rginda87b86462011-12-14 13:48:03 -08002328 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002329};
2330
2331/**
2332 * Shift rows below the cursor down by a given number of lines.
2333 *
2334 * This function respects the current scroll region.
2335 *
2336 * New rows are inserted at the top of the scroll region to fill the
2337 * vacated rows. The new rows not filled out with the current text attributes.
2338 *
2339 * This function does not affect the scrollback rows at all. Rows shifted
2340 * off the bottom are lost.
2341 *
2342 * @param {integer} count The number of rows to scroll.
2343 */
2344hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08002345 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002346
rginda87b86462011-12-14 13:48:03 -08002347 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08002348 this.insertLines(opt_count);
2349
rginda87b86462011-12-14 13:48:03 -08002350 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002351};
2352
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002353/**
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002354 * Enable accessibility-friendly features that have a performance impact.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002355 *
2356 * This will generate additional DOM nodes in an aria-live region that will
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002357 * cause Assitive Technology to announce the output of the terminal. It also
2358 * enables other features that aid assistive technology. All the features gated
2359 * behind this flag have a performance impact on the terminal which is why they
2360 * are made optional.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002361 *
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002362 * @param {boolean} enabled Whether to enable accessibility-friendly features.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002363 */
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002364hterm.Terminal.prototype.setAccessibilityEnabled = function(enabled) {
Raymes Khoury177aec72018-06-26 10:58:53 +10002365 this.accessibilityReader_.setAccessibilityEnabled(enabled);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002366};
rginda87b86462011-12-14 13:48:03 -08002367
rginda8ba33642011-12-14 12:31:31 -08002368/**
2369 * Set the cursor position.
2370 *
2371 * The cursor row is relative to the scroll region if the terminal has
2372 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2373 *
2374 * @param {integer} row The new zero-based cursor row.
2375 * @param {integer} row The new zero-based cursor column.
2376 */
2377hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2378 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002379 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002380 } else {
rginda87b86462011-12-14 13:48:03 -08002381 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002382 }
rginda87b86462011-12-14 13:48:03 -08002383};
rginda8ba33642011-12-14 12:31:31 -08002384
Evan Jones2600d4f2016-12-06 09:29:36 -05002385/**
2386 * Move the cursor relative to its current position.
2387 *
2388 * @param {number} row
2389 * @param {number} column
2390 */
rginda87b86462011-12-14 13:48:03 -08002391hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2392 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002393 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2394 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002395 this.screen_.setCursorPosition(row, column);
2396};
2397
Evan Jones2600d4f2016-12-06 09:29:36 -05002398/**
2399 * Move the cursor to the specified position.
2400 *
2401 * @param {number} row
2402 * @param {number} column
2403 */
rginda87b86462011-12-14 13:48:03 -08002404hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002405 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2406 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002407 this.screen_.setCursorPosition(row, column);
2408};
2409
2410/**
2411 * Set the cursor column.
2412 *
2413 * @param {integer} column The new zero-based cursor column.
2414 */
2415hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002416 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002417};
2418
2419/**
2420 * Return the cursor column.
2421 *
2422 * @return {integer} The zero-based cursor column.
2423 */
2424hterm.Terminal.prototype.getCursorColumn = function() {
2425 return this.screen_.cursorPosition.column;
2426};
2427
2428/**
2429 * Set the cursor row.
2430 *
2431 * The cursor row is relative to the scroll region if the terminal has
2432 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2433 *
2434 * @param {integer} row The new cursor row.
2435 */
rginda87b86462011-12-14 13:48:03 -08002436hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2437 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002438};
2439
2440/**
2441 * Return the cursor row.
2442 *
2443 * @return {integer} The zero-based cursor row.
2444 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002445hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002446 return this.screen_.cursorPosition.row;
2447};
2448
2449/**
2450 * Request that the ScrollPort redraw itself soon.
2451 *
2452 * The redraw will happen asynchronously, soon after the call stack winds down.
2453 * Multiple calls will be coalesced into a single redraw.
2454 */
2455hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002456 if (this.timeouts_.redraw)
2457 return;
rginda8ba33642011-12-14 12:31:31 -08002458
2459 var self = this;
rginda87b86462011-12-14 13:48:03 -08002460 this.timeouts_.redraw = setTimeout(function() {
2461 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002462 self.scrollPort_.redraw_();
2463 }, 0);
2464};
2465
2466/**
2467 * Request that the ScrollPort be scrolled to the bottom.
2468 *
2469 * The scroll will happen asynchronously, soon after the call stack winds down.
2470 * Multiple calls will be coalesced into a single scroll.
2471 *
2472 * This affects the scrollbar position of the ScrollPort, and has nothing to
2473 * do with the VT scroll commands.
2474 */
2475hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2476 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002477 return;
rginda8ba33642011-12-14 12:31:31 -08002478
2479 var self = this;
2480 this.timeouts_.scrollDown = setTimeout(function() {
2481 delete self.timeouts_.scrollDown;
2482 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2483 }, 10);
2484};
2485
2486/**
2487 * Move the cursor up a specified number of rows.
2488 *
2489 * @param {integer} count The number of rows to move the cursor.
2490 */
2491hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002492 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002493};
2494
2495/**
2496 * Move the cursor down a specified number of rows.
2497 *
2498 * @param {integer} count The number of rows to move the cursor.
2499 */
2500hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002501 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002502 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2503 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2504 this.screenSize.height - 1);
2505
rgindacbbd7482012-06-13 15:06:16 -07002506 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002507 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002508 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002509};
2510
2511/**
2512 * Move the cursor left a specified number of columns.
2513 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002514 * If reverse wraparound mode is enabled and the previous row wrapped into
2515 * the current row then we back up through the wraparound as well.
2516 *
rginda8ba33642011-12-14 12:31:31 -08002517 * @param {integer} count The number of columns to move the cursor.
2518 */
2519hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002520 count = count || 1;
2521
2522 if (count < 1)
2523 return;
2524
2525 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002526 if (this.options_.reverseWraparound) {
2527 if (this.screen_.cursorPosition.overflow) {
2528 // If this cursor is in the right margin, consume one count to get it
2529 // back to the last column. This only applies when we're in reverse
2530 // wraparound mode.
2531 count--;
2532 this.clearCursorOverflow();
2533
2534 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002535 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002536 }
2537
Robert Gindabfb32622014-07-17 13:20:27 -07002538 var newRow = this.screen_.cursorPosition.row;
2539 var newColumn = currentColumn - count;
2540 if (newColumn < 0) {
2541 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2542 if (newRow < 0) {
2543 // xterm also wraps from row 0 to the last row.
2544 newRow = this.screenSize.height + newRow % this.screenSize.height;
2545 }
2546 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2547 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002548
Robert Gindabfb32622014-07-17 13:20:27 -07002549 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2550
2551 } else {
2552 var newColumn = Math.max(currentColumn - count, 0);
2553 this.setCursorColumn(newColumn);
2554 }
rginda8ba33642011-12-14 12:31:31 -08002555};
2556
2557/**
2558 * Move the cursor right a specified number of columns.
2559 *
2560 * @param {integer} count The number of columns to move the cursor.
2561 */
2562hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002563 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002564
2565 if (count < 1)
2566 return;
2567
rgindacbbd7482012-06-13 15:06:16 -07002568 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002569 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002570 this.setCursorColumn(column);
2571};
2572
2573/**
2574 * Reverse the foreground and background colors of the terminal.
2575 *
2576 * This only affects text that was drawn with no attributes.
2577 *
2578 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2579 * been drawn with attributes that happen to coincide with the default
2580 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002581 *
2582 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002583 */
2584hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002585 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002586 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002587 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2588 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002589 } else {
rginda9f5222b2012-03-05 11:53:28 -08002590 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2591 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002592 }
2593};
2594
2595/**
rginda87b86462011-12-14 13:48:03 -08002596 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002597 *
2598 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002599 */
2600hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002601 this.cursorNode_.style.backgroundColor =
2602 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002603
2604 var self = this;
2605 setTimeout(function() {
Matheus Fernandes2d733082017-09-11 06:43:01 -04002606 self.restyleCursor_();
rginda6d397402012-01-17 10:58:29 -08002607 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002608
Michael Kelly485ecd12014-06-09 11:41:56 -04002609 // bellSquelchTimeout_ affects both audio and notification bells.
2610 if (this.bellSquelchTimeout_)
2611 return;
2612
Robert Ginda92e18102013-03-14 13:56:37 -07002613 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002614 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002615 this.bellSequelchTimeout_ = setTimeout(function() {
2616 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002617 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002618 } else {
2619 delete this.bellSquelchTimeout_;
2620 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002621
2622 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingera5fb83c2017-06-22 14:48:35 -07002623 var n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002624 this.bellNotificationList_.push(n);
2625 // TODO: Should we try to raise the window here?
2626 n.onclick = function() { self.closeBellNotifications_(); };
2627 }
rginda87b86462011-12-14 13:48:03 -08002628};
2629
2630/**
rginda8ba33642011-12-14 12:31:31 -08002631 * Set the origin mode bit.
2632 *
2633 * If origin mode is on, certain VT cursor and scrolling commands measure their
2634 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2635 * to the top of the addressable screen.
2636 *
2637 * Defaults to off.
2638 *
2639 * @param {boolean} state True to set origin mode, false to unset.
2640 */
2641hterm.Terminal.prototype.setOriginMode = function(state) {
2642 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002643 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002644};
2645
2646/**
2647 * Set the insert mode bit.
2648 *
2649 * If insert mode is on, existing text beyond the cursor position will be
2650 * shifted right to make room for new text. Otherwise, new text overwrites
2651 * any existing text.
2652 *
2653 * Defaults to off.
2654 *
2655 * @param {boolean} state True to set insert mode, false to unset.
2656 */
2657hterm.Terminal.prototype.setInsertMode = function(state) {
2658 this.options_.insertMode = state;
2659};
2660
2661/**
rginda87b86462011-12-14 13:48:03 -08002662 * Set the auto carriage return bit.
2663 *
2664 * If auto carriage return is on then a formfeed character is interpreted
2665 * as a newline, otherwise it's the same as a linefeed. The difference boils
2666 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002667 *
2668 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002669 */
2670hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2671 this.options_.autoCarriageReturn = state;
2672};
2673
2674/**
rginda8ba33642011-12-14 12:31:31 -08002675 * Set the wraparound mode bit.
2676 *
2677 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2678 * to the start of the following row. Otherwise, the cursor is clamped to the
2679 * end of the screen and attempts to write past it are ignored.
2680 *
2681 * Defaults to on.
2682 *
2683 * @param {boolean} state True to set wraparound mode, false to unset.
2684 */
2685hterm.Terminal.prototype.setWraparound = function(state) {
2686 this.options_.wraparound = state;
2687};
2688
2689/**
2690 * Set the reverse-wraparound mode bit.
2691 *
2692 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2693 * to the end of the previous row. Otherwise, the cursor is clamped to column
2694 * 0.
2695 *
2696 * Defaults to off.
2697 *
2698 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2699 */
2700hterm.Terminal.prototype.setReverseWraparound = function(state) {
2701 this.options_.reverseWraparound = state;
2702};
2703
2704/**
2705 * Selects between the primary and alternate screens.
2706 *
2707 * If alternate mode is on, the alternate screen is active. Otherwise the
2708 * primary screen is active.
2709 *
2710 * Swapping screens has no effect on the scrollback buffer.
2711 *
2712 * Each screen maintains its own cursor position.
2713 *
2714 * Defaults to off.
2715 *
2716 * @param {boolean} state True to set alternate mode, false to unset.
2717 */
2718hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002719 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002720 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2721
rginda35c456b2012-02-09 17:29:05 -08002722 if (this.screen_.rowsArray.length &&
2723 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2724 // If the screen changed sizes while we were away, our rowIndexes may
2725 // be incorrect.
2726 var offset = this.scrollbackRows_.length;
2727 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002728 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002729 ary[i].rowIndex = offset + i;
2730 }
2731 }
rginda8ba33642011-12-14 12:31:31 -08002732
rginda35c456b2012-02-09 17:29:05 -08002733 this.realizeWidth_(this.screenSize.width);
2734 this.realizeHeight_(this.screenSize.height);
2735 this.scrollPort_.syncScrollHeight();
2736 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002737
rginda6d397402012-01-17 10:58:29 -08002738 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002739 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002740};
2741
2742/**
2743 * Set the cursor-blink mode bit.
2744 *
2745 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2746 * a visible cursor does not blink.
2747 *
2748 * You should make sure to turn blinking off if you're going to dispose of a
2749 * terminal, otherwise you'll leak a timeout.
2750 *
2751 * Defaults to on.
2752 *
2753 * @param {boolean} state True to set cursor-blink mode, false to unset.
2754 */
2755hterm.Terminal.prototype.setCursorBlink = function(state) {
2756 this.options_.cursorBlink = state;
2757
2758 if (!state && this.timeouts_.cursorBlink) {
2759 clearTimeout(this.timeouts_.cursorBlink);
2760 delete this.timeouts_.cursorBlink;
2761 }
2762
2763 if (this.options_.cursorVisible)
2764 this.setCursorVisible(true);
2765};
2766
2767/**
2768 * Set the cursor-visible mode bit.
2769 *
2770 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2771 *
2772 * Defaults to on.
2773 *
2774 * @param {boolean} state True to set cursor-visible mode, false to unset.
2775 */
2776hterm.Terminal.prototype.setCursorVisible = function(state) {
2777 this.options_.cursorVisible = state;
2778
2779 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002780 if (this.timeouts_.cursorBlink) {
2781 clearTimeout(this.timeouts_.cursorBlink);
2782 delete this.timeouts_.cursorBlink;
2783 }
rginda87b86462011-12-14 13:48:03 -08002784 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002785 return;
2786 }
2787
rginda87b86462011-12-14 13:48:03 -08002788 this.syncCursorPosition_();
2789
2790 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002791
2792 if (this.options_.cursorBlink) {
2793 if (this.timeouts_.cursorBlink)
2794 return;
2795
Robert Gindaea2183e2014-07-17 09:51:51 -07002796 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002797 } else {
2798 if (this.timeouts_.cursorBlink) {
2799 clearTimeout(this.timeouts_.cursorBlink);
2800 delete this.timeouts_.cursorBlink;
2801 }
2802 }
2803};
2804
2805/**
rginda87b86462011-12-14 13:48:03 -08002806 * Synchronizes the visible cursor and document selection with the current
2807 * cursor coordinates.
Raymes Khourye5d48982018-08-02 09:08:32 +10002808 *
2809 * @return {boolean} True if the cursor is onscreen and synced.
rginda8ba33642011-12-14 12:31:31 -08002810 */
2811hterm.Terminal.prototype.syncCursorPosition_ = function() {
2812 var topRowIndex = this.scrollPort_.getTopRowIndex();
2813 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2814 var cursorRowIndex = this.scrollbackRows_.length +
2815 this.screen_.cursorPosition.row;
2816
Raymes Khoury15697f42018-07-17 11:37:18 +10002817 let forceSyncSelection = false;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002818 if (this.accessibilityReader_.accessibilityEnabled) {
2819 // Report the new position of the cursor for accessibility purposes.
2820 const cursorColumnIndex = this.screen_.cursorPosition.column;
2821 const cursorLineText =
2822 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
Raymes Khoury15697f42018-07-17 11:37:18 +10002823 // This will force the selection to be sync'd to the cursor position if the
2824 // user has pressed a key. Generally we would only sync the cursor position
2825 // when selection is collapsed so that if the user has selected something
2826 // we don't clear the selection by moving the selection. However when a
2827 // screen reader is used, it's intuitive for entering a key to move the
2828 // selection to the cursor.
2829 forceSyncSelection = this.accessibilityReader_.hasUserGesture;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002830 this.accessibilityReader_.afterCursorChange(
2831 cursorLineText, cursorRowIndex, cursorColumnIndex);
2832 }
2833
rginda8ba33642011-12-14 12:31:31 -08002834 if (cursorRowIndex > bottomRowIndex) {
2835 // Cursor is scrolled off screen, move it outside of the visible area.
Mike Frysinger44c32202017-08-05 01:13:09 -04002836 this.setCssVar('cursor-offset-row', '-1');
Raymes Khourye5d48982018-08-02 09:08:32 +10002837 return false;
rginda8ba33642011-12-14 12:31:31 -08002838 }
2839
Robert Gindab837c052014-08-11 11:17:51 -07002840 if (this.options_.cursorVisible &&
2841 this.cursorNode_.style.display == 'none') {
2842 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2843 this.cursorNode_.style.display = '';
2844 }
2845
Mike Frysinger44c32202017-08-05 01:13:09 -04002846 // Position the cursor using CSS variable math. If we do the math in JS,
2847 // the float math will end up being more precise than the CSS which will
2848 // cause the cursor tracking to be off.
2849 this.setCssVar(
2850 'cursor-offset-row',
2851 `${cursorRowIndex - topRowIndex} + ` +
2852 `${this.scrollPort_.visibleRowTopMargin}px`);
2853 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08002854
2855 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04002856 '(' + this.screen_.cursorPosition.column +
2857 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08002858 ')');
2859
2860 // Update the caret for a11y purposes.
2861 var selection = this.document_.getSelection();
Raymes Khoury15697f42018-07-17 11:37:18 +10002862 if (selection && (selection.isCollapsed || forceSyncSelection)) {
rginda87b86462011-12-14 13:48:03 -08002863 this.screen_.syncSelectionCaret(selection);
Raymes Khoury15697f42018-07-17 11:37:18 +10002864 }
Raymes Khourye5d48982018-08-02 09:08:32 +10002865 return true;
rginda8ba33642011-12-14 12:31:31 -08002866};
2867
Robert Gindafb1be6a2013-12-11 11:56:22 -08002868/**
2869 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2870 * and character cell dimensions.
2871 */
Robert Ginda830583c2013-08-07 13:20:46 -07002872hterm.Terminal.prototype.restyleCursor_ = function() {
2873 var shape = this.cursorShape_;
2874
2875 if (this.cursorNode_.getAttribute('focus') == 'false') {
2876 // Always show a block cursor when unfocused.
2877 shape = hterm.Terminal.cursorShape.BLOCK;
2878 }
2879
2880 var style = this.cursorNode_.style;
2881
2882 switch (shape) {
2883 case hterm.Terminal.cursorShape.BEAM:
Robert Ginda830583c2013-08-07 13:20:46 -07002884 style.backgroundColor = 'transparent';
2885 style.borderBottomStyle = null;
2886 style.borderLeftStyle = 'solid';
2887 break;
2888
2889 case hterm.Terminal.cursorShape.UNDERLINE:
Robert Ginda830583c2013-08-07 13:20:46 -07002890 style.backgroundColor = 'transparent';
2891 style.borderBottomStyle = 'solid';
Robert Ginda830583c2013-08-07 13:20:46 -07002892 style.borderLeftStyle = null;
2893 break;
2894
2895 default:
Mike Frysinger2fd079a2018-09-02 01:46:12 -04002896 style.backgroundColor = 'var(--hterm-cursor-color)';
Robert Ginda830583c2013-08-07 13:20:46 -07002897 style.borderBottomStyle = null;
2898 style.borderLeftStyle = null;
2899 break;
2900 }
2901};
2902
rginda8ba33642011-12-14 12:31:31 -08002903/**
2904 * Synchronizes the visible cursor with the current cursor coordinates.
2905 *
2906 * The sync will happen asynchronously, soon after the call stack winds down.
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002907 * Multiple calls will be coalesced into a single sync. This should be called
2908 * prior to the cursor actually changing position.
rginda8ba33642011-12-14 12:31:31 -08002909 */
2910hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2911 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002912 return;
rginda8ba33642011-12-14 12:31:31 -08002913
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002914 if (this.accessibilityReader_.accessibilityEnabled) {
2915 // Report the previous position of the cursor for accessibility purposes.
2916 const cursorRowIndex = this.scrollbackRows_.length +
2917 this.screen_.cursorPosition.row;
2918 const cursorColumnIndex = this.screen_.cursorPosition.column;
2919 const cursorLineText =
2920 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
2921 this.accessibilityReader_.beforeCursorChange(
2922 cursorLineText, cursorRowIndex, cursorColumnIndex);
2923 }
2924
rginda8ba33642011-12-14 12:31:31 -08002925 var self = this;
2926 this.timeouts_.syncCursor = setTimeout(function() {
2927 self.syncCursorPosition_();
2928 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002929 }, 0);
2930};
2931
rgindacc2996c2012-02-24 14:59:31 -08002932/**
rgindaf522ce02012-04-17 17:49:17 -07002933 * Show or hide the zoom warning.
2934 *
2935 * The zoom warning is a message warning the user that their browser zoom must
2936 * be set to 100% in order for hterm to function properly.
2937 *
2938 * @param {boolean} state True to show the message, false to hide it.
2939 */
2940hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2941 if (!this.zoomWarningNode_) {
2942 if (!state)
2943 return;
2944
2945 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002946 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07002947 this.zoomWarningNode_.style.cssText = (
2948 'color: black;' +
2949 'background-color: #ff2222;' +
2950 'font-size: large;' +
2951 'border-radius: 8px;' +
2952 'opacity: 0.75;' +
2953 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2954 'top: 0.5em;' +
2955 'right: 1.2em;' +
2956 'position: absolute;' +
2957 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002958 '-webkit-user-select: none;' +
2959 '-moz-text-size-adjust: none;' +
2960 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05002961
2962 this.zoomWarningNode_.addEventListener('click', function(e) {
2963 this.parentNode.removeChild(this);
2964 });
rgindaf522ce02012-04-17 17:49:17 -07002965 }
2966
Mike Frysingerb7289952019-03-23 16:05:38 -07002967 this.zoomWarningNode_.textContent = lib.i18n.replaceReferences(
Robert Gindab4839c22013-02-28 16:52:10 -08002968 hterm.zoomWarningMessage,
2969 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2970
rgindaf522ce02012-04-17 17:49:17 -07002971 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2972
2973 if (state) {
2974 if (!this.zoomWarningNode_.parentNode)
2975 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2976 } else if (this.zoomWarningNode_.parentNode) {
2977 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2978 }
2979};
2980
2981/**
rgindacc2996c2012-02-24 14:59:31 -08002982 * Show the terminal overlay for a given amount of time.
2983 *
2984 * The terminal overlay appears in inverse video in a large font, centered
2985 * over the terminal. You should probably keep the overlay message brief,
2986 * since it's in a large font and you probably aren't going to check the size
2987 * of the terminal first.
2988 *
2989 * @param {string} msg The text (not HTML) message to display in the overlay.
2990 * @param {number} opt_timeout The amount of time to wait before fading out
2991 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2992 * stay up forever (or until the next overlay).
2993 */
2994hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002995 if (!this.overlayNode_) {
2996 if (!this.div_)
2997 return;
2998
2999 this.overlayNode_ = this.document_.createElement('div');
3000 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08003001 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08003002 'font-size: xx-large;' +
3003 'opacity: 0.75;' +
3004 'padding: 0.2em 0.5em 0.2em 0.5em;' +
3005 'position: absolute;' +
3006 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003007 '-webkit-transition: opacity 180ms ease-in;' +
3008 '-moz-user-select: none;' +
3009 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08003010
3011 this.overlayNode_.addEventListener('mousedown', function(e) {
3012 e.preventDefault();
3013 e.stopPropagation();
3014 }, true);
rgindaf0090c92012-02-10 14:58:52 -08003015 }
3016
rginda9f5222b2012-03-05 11:53:28 -08003017 this.overlayNode_.style.color = this.prefs_.get('background-color');
3018 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
3019 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
3020
rgindaf0090c92012-02-10 14:58:52 -08003021 this.overlayNode_.textContent = msg;
3022 this.overlayNode_.style.opacity = '0.75';
3023
3024 if (!this.overlayNode_.parentNode)
3025 this.div_.appendChild(this.overlayNode_);
3026
Robert Ginda97769282013-02-01 15:30:30 -08003027 var divSize = hterm.getClientSize(this.div_);
3028 var overlaySize = hterm.getClientSize(this.overlayNode_);
3029
Robert Ginda8a59f762014-07-23 11:29:55 -07003030 this.overlayNode_.style.top =
3031 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08003032 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07003033 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08003034
rgindaf0090c92012-02-10 14:58:52 -08003035 if (this.overlayTimeout_)
3036 clearTimeout(this.overlayTimeout_);
3037
Raymes Khouryc7a06382018-07-04 10:25:45 +10003038 this.accessibilityReader_.assertiveAnnounce(msg);
3039
rgindacc2996c2012-02-24 14:59:31 -08003040 if (opt_timeout === null)
3041 return;
3042
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003043 this.overlayTimeout_ = setTimeout(() => {
3044 this.overlayNode_.style.opacity = '0';
3045 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
3046 }, opt_timeout || 1500);
3047};
3048
3049/**
3050 * Hide the terminal overlay immediately.
3051 *
3052 * Useful when we show an overlay for an event with an unknown end time.
3053 */
3054hterm.Terminal.prototype.hideOverlay = function() {
3055 if (this.overlayTimeout_)
3056 clearTimeout(this.overlayTimeout_);
3057 this.overlayTimeout_ = null;
3058
3059 if (this.overlayNode_.parentNode)
3060 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
3061 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08003062};
3063
rginda4bba5e12012-06-20 16:15:30 -07003064/**
3065 * Paste from the system clipboard to the terminal.
3066 */
3067hterm.Terminal.prototype.paste = function() {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003068 return hterm.pasteFromClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07003069};
3070
3071/**
3072 * Copy a string to the system clipboard.
3073 *
3074 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05003075 *
3076 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07003077 */
3078hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08003079 if (this.prefs_.get('enable-clipboard-notice'))
3080 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
3081
Mike Frysinger96eacae2019-01-02 18:13:56 -05003082 hterm.copySelectionToClipboard(this.document_, str);
rginda4bba5e12012-06-20 16:15:30 -07003083};
3084
Evan Jones2600d4f2016-12-06 09:29:36 -05003085/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003086 * Display an image.
3087 *
Mike Frysinger2558ed52019-01-14 01:03:41 -05003088 * Either URI or buffer or blob fields must be specified.
3089 *
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003090 * @param {Object} options The image to display.
3091 * @param {string=} options.name A human readable string for the image.
3092 * @param {string|number=} options.size The size (in bytes).
3093 * @param {boolean=} options.preserveAspectRatio Whether to preserve aspect.
3094 * @param {boolean=} options.inline Whether to display the image inline.
3095 * @param {string|number=} options.width The width of the image.
3096 * @param {string|number=} options.height The height of the image.
3097 * @param {string=} options.align Direction to align the image.
Mike Frysinger2558ed52019-01-14 01:03:41 -05003098 * @param {string=} options.uri The source URI for the image.
3099 * @param {ArrayBuffer=} options.buffer The ArrayBuffer image data.
3100 * @param {Blob=} options.blob The Blob image data.
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003101 * @param {string=} options.type The MIME type of the image data.
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003102 * @param {function=} onLoad Callback when loading finishes.
3103 * @param {function(Event)=} onError Callback when loading fails.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003104 */
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003105hterm.Terminal.prototype.displayImage = function(options, onLoad, onError) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003106 // Make sure we're actually given a resource to display.
Mike Frysinger2558ed52019-01-14 01:03:41 -05003107 if (options.uri === undefined && options.buffer === undefined &&
3108 options.blob === undefined)
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003109 return;
3110
3111 // Set up the defaults to simplify code below.
3112 if (!options.name)
3113 options.name = '';
3114
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003115 // See if the mime type is available. If not, guess from the filename.
3116 // We don't list all possible mime types because the browser can usually
3117 // guess it correctly. So list the ones that need a bit more help.
3118 if (!options.type) {
3119 const ary = options.name.split('.');
3120 const ext = ary[ary.length - 1].trim();
3121 switch (ext) {
3122 case 'svg':
3123 case 'svgz':
3124 options.type = 'image/svg+xml';
3125 break;
3126 }
3127 }
3128
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003129 // Has the user approved image display yet?
3130 if (this.allowImagesInline !== true) {
3131 this.newLine();
3132 const row = this.getRowNode(this.scrollbackRows_.length +
3133 this.getCursorRow() - 1);
3134
3135 if (this.allowImagesInline === false) {
3136 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
3137 'Inline Images Disabled');
3138 return;
3139 }
3140
3141 // Show a prompt.
3142 let button;
3143 const span = this.document_.createElement('span');
3144 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
3145 span.style.fontWeight = 'bold';
3146 span.style.borderWidth = '1px';
3147 span.style.borderStyle = 'dashed';
3148 button = this.document_.createElement('span');
3149 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3150 button.style.marginLeft = '1em';
3151 button.style.borderWidth = '1px';
3152 button.style.borderStyle = 'solid';
3153 button.addEventListener('click', () => {
3154 this.prefs_.set('allow-images-inline', false);
3155 });
3156 span.appendChild(button);
3157 button = this.document_.createElement('span');
3158 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3159 'allow this session');
3160 button.style.marginLeft = '1em';
3161 button.style.borderWidth = '1px';
3162 button.style.borderStyle = 'solid';
3163 button.addEventListener('click', () => {
3164 this.allowImagesInline = true;
3165 });
3166 span.appendChild(button);
3167 button = this.document_.createElement('span');
3168 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3169 button.style.marginLeft = '1em';
3170 button.style.borderWidth = '1px';
3171 button.style.borderStyle = 'solid';
3172 button.addEventListener('click', () => {
3173 this.prefs_.set('allow-images-inline', true);
3174 });
3175 span.appendChild(button);
3176
3177 row.appendChild(span);
3178 return;
3179 }
3180
3181 // See if we should show this object directly, or download it.
3182 if (options.inline) {
3183 const io = this.io.push();
3184 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
3185 'Loading $1 ...'), null);
3186
3187 // While we're loading the image, eat all the user's input.
3188 io.onVTKeystroke = io.sendString = () => {};
3189
3190 // Initialize this new image.
Adrián Pérez-Orozco6a550322018-08-31 14:36:06 -07003191 const img =
3192 /** @type {!HTMLImageElement} */ (this.document_.createElement('img'));
Mike Frysinger2558ed52019-01-14 01:03:41 -05003193 if (options.uri !== undefined) {
3194 img.src = options.uri;
3195 } else if (options.buffer !== undefined) {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003196 const blob = new Blob([options.buffer], {type: options.type});
Mike Frysinger2558ed52019-01-14 01:03:41 -05003197 img.src = URL.createObjectURL(blob);
3198 } else {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003199 const blob = new Blob([options.blob], {type: options.type});
Mike Frysinger2558ed52019-01-14 01:03:41 -05003200 img.src = URL.createObjectURL(options.blob);
3201 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003202 img.title = img.alt = options.name;
3203
3204 // Attach the image to the page to let it load/render. It won't stay here.
3205 // This is needed so it's visible and the DOM can calculate the height. If
3206 // the image is hidden or not in the DOM, the height is always 0.
3207 this.document_.body.appendChild(img);
3208
3209 // Wait for the image to finish loading before we try moving it to the
3210 // right place in the terminal.
3211 img.onload = () => {
3212 // Now that we have the image dimensions, figure out how to show it.
3213 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
3214 img.style.maxWidth = `${this.document_.body.clientWidth}px`;
3215 img.style.maxHeight = `${this.document_.body.clientHeight}px`;
3216
3217 // Parse a width/height specification.
3218 const parseDim = (dim, maxDim, cssVar) => {
3219 if (!dim || dim == 'auto')
3220 return '';
3221
3222 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3223 if (ary) {
3224 if (ary[2] == '%')
3225 return maxDim * parseInt(ary[1]) / 100 + 'px';
3226 else if (ary[2] == 'px')
3227 return dim;
3228 else
3229 return `calc(${dim} * var(${cssVar}))`;
3230 }
3231
3232 return '';
3233 };
3234 img.style.width =
3235 parseDim(options.width, this.document_.body.clientWidth,
3236 '--hterm-charsize-width');
3237 img.style.height =
3238 parseDim(options.height, this.document_.body.clientHeight,
3239 '--hterm-charsize-height');
3240
3241 // Figure out how many rows the image occupies, then add that many.
3242 // XXX: This count will be inaccurate if the font size changes on us.
3243 const padRows = Math.ceil(img.clientHeight /
3244 this.scrollPort_.characterSize.height);
3245 for (let i = 0; i < padRows; ++i)
3246 this.newLine();
3247
3248 // Update the max height in case the user shrinks the character size.
3249 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3250
3251 // Move the image to the last row. This way when we scroll up, it doesn't
3252 // disappear when the first row gets clipped. It will disappear when we
3253 // scroll down and the last row is clipped ...
3254 this.document_.body.removeChild(img);
3255 // Create a wrapper node so we can do an absolute in a relative position.
3256 // This helps with rounding errors between JS & CSS counts.
3257 const div = this.document_.createElement('div');
3258 div.style.position = 'relative';
3259 div.style.textAlign = options.align;
3260 img.style.position = 'absolute';
3261 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3262 div.appendChild(img);
3263 const row = this.getRowNode(this.scrollbackRows_.length +
3264 this.getCursorRow() - 1);
3265 row.appendChild(div);
3266
Mike Frysinger2558ed52019-01-14 01:03:41 -05003267 // Now that the image has been read, we can revoke the source.
3268 if (options.uri === undefined) {
3269 URL.revokeObjectURL(img.src);
3270 }
3271
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003272 io.hideOverlay();
3273 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003274
3275 if (onLoad)
3276 onLoad();
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003277 };
3278
3279 // If we got a malformed image, give up.
3280 img.onerror = (e) => {
3281 this.document_.body.removeChild(img);
3282 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
Mike Frysingere14a8c42018-03-10 00:17:30 -08003283 'Loading $1 failed'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003284 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003285
3286 if (onError)
3287 onError(e);
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003288 };
3289 } else {
3290 // We can't use chrome.downloads.download as that requires "downloads"
3291 // permissions, and that works only in extensions, not apps.
3292 const a = this.document_.createElement('a');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003293 if (options.uri !== undefined) {
3294 a.href = options.uri;
3295 } else if (options.buffer !== undefined) {
3296 const blob = new Blob([options.buffer]);
3297 a.href = URL.createObjectURL(blob);
3298 } else {
3299 a.href = URL.createObjectURL(options.blob);
3300 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003301 a.download = options.name;
3302 this.document_.body.appendChild(a);
3303 a.click();
3304 a.remove();
Mike Frysinger2558ed52019-01-14 01:03:41 -05003305 if (options.uri === undefined) {
3306 URL.revokeObjectURL(a.href);
3307 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003308 }
3309};
3310
3311/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003312 * Returns the selected text, or null if no text is selected.
3313 *
3314 * @return {string|null}
3315 */
rgindaa09e7332012-08-17 12:49:51 -07003316hterm.Terminal.prototype.getSelectionText = function() {
3317 var selection = this.scrollPort_.selection;
3318 selection.sync();
3319
3320 if (selection.isCollapsed)
3321 return null;
3322
rgindaa09e7332012-08-17 12:49:51 -07003323 // Start offset measures from the beginning of the line.
3324 var startOffset = selection.startOffset;
3325 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003326
Raymes Khoury334625a2018-06-25 10:29:40 +10003327 // If an x-row isn't selected, |node| will be null.
3328 if (!node)
3329 return null;
3330
Robert Gindafdbb3f22012-09-06 20:23:06 -07003331 if (node.nodeName != 'X-ROW') {
3332 // If the selection doesn't start on an x-row node, then it must be
3333 // somewhere inside the x-row. Add any characters from previous siblings
3334 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003335
3336 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3337 // If node is the text node in a styled span, move up to the span node.
3338 node = node.parentNode;
3339 }
3340
Robert Gindafdbb3f22012-09-06 20:23:06 -07003341 while (node.previousSibling) {
3342 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003343 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003344 }
rgindaa09e7332012-08-17 12:49:51 -07003345 }
3346
3347 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08003348 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
3349 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003350 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003351
Robert Gindafdbb3f22012-09-06 20:23:06 -07003352 if (node.nodeName != 'X-ROW') {
3353 // If the selection doesn't end on an x-row node, then it must be
3354 // somewhere inside the x-row. Add any characters from following siblings
3355 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003356
3357 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3358 // If node is the text node in a styled span, move up to the span node.
3359 node = node.parentNode;
3360 }
3361
Robert Gindafdbb3f22012-09-06 20:23:06 -07003362 while (node.nextSibling) {
3363 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003364 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003365 }
rgindaa09e7332012-08-17 12:49:51 -07003366 }
3367
3368 var rv = this.getRowsText(selection.startRow.rowIndex,
3369 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003370 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003371};
3372
rginda4bba5e12012-06-20 16:15:30 -07003373/**
3374 * Copy the current selection to the system clipboard, then clear it after a
3375 * short delay.
3376 */
3377hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07003378 var text = this.getSelectionText();
3379 if (text != null)
3380 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07003381};
3382
rgindaf0090c92012-02-10 14:58:52 -08003383hterm.Terminal.prototype.overlaySize = function() {
3384 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
3385};
3386
rginda87b86462011-12-14 13:48:03 -08003387/**
3388 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3389 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003390 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003391 */
3392hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08003393 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08003394 this.scrollPort_.scrollRowToBottom(this.getRowCount());
3395
Mike Frysinger79669762018-12-30 20:51:10 -05003396 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08003397};
3398
3399/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003400 * Open the selected url.
3401 */
3402hterm.Terminal.prototype.openSelectedUrl_ = function() {
3403 var str = this.getSelectionText();
3404
3405 // If there is no selection, try and expand wherever they clicked.
3406 if (str == null) {
John Lincae9b732018-03-08 13:56:35 +08003407 this.screen_.expandSelectionForUrl(this.document_.getSelection());
Mike Frysinger70b94692017-01-26 18:57:50 -10003408 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003409
3410 // If clicking in empty space, return.
3411 if (str == null)
3412 return;
Mike Frysinger70b94692017-01-26 18:57:50 -10003413 }
3414
3415 // Make sure URL is valid before opening.
3416 if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0)
3417 return;
Mike Frysinger43472622017-06-26 18:11:07 -04003418
3419 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003420 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003421 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3422 // We have to whitelist a few protocols that lack authorities and thus
3423 // never use the //. Like mailto.
3424 switch (str.split(':', 1)[0]) {
3425 case 'mailto':
3426 break;
3427 default:
3428 str = 'http://' + str;
3429 break;
3430 }
3431 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003432
Mike Frysinger720fa832017-10-23 01:15:52 -04003433 hterm.openUrl(str);
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003434};
Mike Frysinger70b94692017-01-26 18:57:50 -10003435
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003436/**
3437 * Manage the automatic mouse hiding behavior while typing.
3438 *
3439 * @param {boolean=} v Whether to enable automatic hiding.
3440 */
3441hterm.Terminal.prototype.setAutomaticMouseHiding = function(v=null) {
3442 // Since Chrome OS & macOS do this by default everywhere, we don't need to.
3443 // Linux & Windows seem to leave this to specific applications to manage.
3444 if (v === null)
3445 v = (hterm.os != 'cros' && hterm.os != 'mac');
3446
3447 this.mouseHideWhileTyping_ = !!v;
3448};
3449
3450/**
3451 * Handler for monitoring user keyboard activity.
3452 *
3453 * This isn't for processing the keystrokes directly, but for updating any
3454 * state that might toggle based on the user using the keyboard at all.
3455 *
3456 * @param {KeyboardEvent} e The keyboard event that triggered us.
3457 */
3458hterm.Terminal.prototype.onKeyboardActivity_ = function(e) {
3459 // When the user starts typing, hide the mouse cursor.
3460 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_)
3461 this.setCssVar('mouse-cursor-style', 'none');
3462};
Mike Frysinger70b94692017-01-26 18:57:50 -10003463
3464/**
rgindad5613292012-06-19 15:40:37 -07003465 * Add the terminalRow and terminalColumn properties to mouse events and
3466 * then forward on to onMouse().
3467 *
3468 * The terminalRow and terminalColumn properties contain the (row, column)
3469 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003470 *
3471 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003472 */
3473hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003474 if (e.processedByTerminalHandler_) {
3475 // We register our event handlers on the document, as well as the cursor
3476 // and the scroll blocker. Mouse events that occur on the cursor or
3477 // scroll blocker will also appear on the document, but we don't want to
3478 // process them twice.
3479 //
3480 // We can't just prevent bubbling because that has other side effects, so
3481 // we decorate the event object with this property instead.
3482 return;
3483 }
3484
Mike Frysinger468966c2018-08-28 13:48:51 -04003485 // Consume navigation events. Button 3 is usually "browser back" and
3486 // button 4 is "browser forward" which we don't want to happen.
3487 if (e.button > 2) {
3488 e.preventDefault();
3489 // We don't return so click events can be passed to the remote below.
3490 }
3491
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003492 var reportMouseEvents = (!this.defeatMouseReports_ &&
3493 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3494
rgindafaa74742012-08-21 13:34:03 -07003495 e.processedByTerminalHandler_ = true;
3496
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003497 // Handle auto hiding of mouse cursor while typing.
3498 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
3499 // Make sure the mouse cursor is visible.
3500 this.syncMouseStyle();
3501 // This debounce isn't perfect, but should work well enough for such a
3502 // simple implementation. If the user moved the mouse, we enabled this
3503 // debounce, and then moved the mouse just before the timeout, we wouldn't
3504 // debounce that later movement.
3505 this.mouseHideDelay_ = setTimeout(() => this.mouseHideDelay_ = null, 1000);
3506 }
3507
Robert Gindaeda48db2014-07-17 09:25:30 -07003508 // One based row/column stored on the mouse event.
3509 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
3510 this.scrollPort_.characterSize.height) + 1;
3511 e.terminalColumn = parseInt(e.clientX /
3512 this.scrollPort_.characterSize.width) + 1;
3513
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003514 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
3515 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07003516 return;
3517 }
3518
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003519 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07003520 // If the cursor is visible and we're not sending mouse events to the
3521 // host app, then we want to hide the terminal cursor when the mouse
3522 // cursor is over top. This keeps the terminal cursor from interfering
3523 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003524 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3525 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3526 this.cursorNode_.style.display = 'none';
3527 } else if (this.cursorNode_.style.display == 'none') {
3528 this.cursorNode_.style.display = '';
3529 }
3530 }
rgindad5613292012-06-19 15:40:37 -07003531
Robert Ginda928cf632014-03-05 15:07:41 -08003532 if (e.type == 'mousedown') {
Mike Frysingercc114512017-09-11 21:39:17 -04003533 this.contextMenu.hide(e);
3534
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003535 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003536 // If VT mouse reporting is disabled, or has been defeated with
3537 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003538 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003539 this.setSelectionEnabled(true);
3540 } else {
3541 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003542 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003543 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003544 this.setSelectionEnabled(false);
3545 e.preventDefault();
3546 }
3547 }
3548
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003549 if (!reportMouseEvents) {
John Lin2aad22e2018-03-16 13:58:11 +08003550 if (e.type == 'dblclick') {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003551 this.screen_.expandSelection(this.document_.getSelection());
John Lin2aad22e2018-03-16 13:58:11 +08003552 if (this.copyOnSelect)
Mike Frysinger406c76c2019-01-02 17:53:51 -05003553 this.copySelectionToClipboard();
rgindad5613292012-06-19 15:40:37 -07003554 }
3555
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003556 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003557 // Debounce this event with the dblclick event. If you try to doubleclick
3558 // a URL to open it, Chrome will fire click then dblclick, but we won't
3559 // have expanded the selection text at the first click event.
3560 clearTimeout(this.timeouts_.openUrl);
3561 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3562 500);
3563 return;
3564 }
3565
Mike Frysinger847577f2017-05-23 23:25:57 -04003566 if (e.type == 'mousedown') {
Mike Frysingercc114512017-09-11 21:39:17 -04003567 if (e.ctrlKey && e.button == 2 /* right button */) {
3568 e.preventDefault();
3569 this.contextMenu.show(e, this);
3570 } else if (e.button == this.mousePasteButton ||
3571 (this.mouseRightClickPaste && e.button == 2 /* right button */)) {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003572 if (!this.paste())
Mike Frysinger05a57f02017-08-27 17:48:55 -04003573 console.warn('Could not paste manually due to web restrictions');
Mike Frysinger847577f2017-05-23 23:25:57 -04003574 }
3575 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003576
Mike Frysinger2edd3612017-05-24 00:54:39 -04003577 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003578 !this.document_.getSelection().isCollapsed) {
Mike Frysinger406c76c2019-01-02 17:53:51 -05003579 this.copySelectionToClipboard();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003580 }
3581
3582 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3583 this.scrollBlockerNode_.engaged) {
3584 // Disengage the scroll-blocker after one of these events.
3585 this.scrollBlockerNode_.engaged = false;
3586 this.scrollBlockerNode_.style.top = '-99px';
3587 }
3588
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003589 // Emulate arrow key presses via scroll wheel events.
3590 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3591 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003592 if (e.type == 'wheel') {
Mike Frysinger321063c2018-08-29 15:33:14 -04003593 const delta = this.scrollPort_.scrollWheelDelta(e);
Mike Frysingerc3030a82017-05-29 14:16:11 -04003594
Mike Frysinger321063c2018-08-29 15:33:14 -04003595 // Helper to turn a wheel event delta into a series of key presses.
3596 const deltaToArrows = (distance, charSize, arrowPos, arrowNeg) => {
3597 if (distance == 0) {
3598 return '';
3599 }
3600
3601 // Convert the scroll distance into a number of rows/cols.
3602 const cells = lib.f.smartFloorDivide(Math.abs(distance), charSize);
3603 const data = '\x1bO' + (distance < 0 ? arrowNeg : arrowPos);
3604 return data.repeat(cells);
3605 };
3606
3607 // The order between up/down and left/right doesn't really matter.
3608 this.io.sendString(
3609 // Up/down arrow keys.
3610 deltaToArrows(delta.y, this.scrollPort_.characterSize.height,
3611 'A', 'B') +
3612 // Left/right arrow keys.
3613 deltaToArrows(delta.x, this.scrollPort_.characterSize.width,
3614 'C', 'D')
3615 );
Mike Frysingerc3030a82017-05-29 14:16:11 -04003616
3617 e.preventDefault();
3618 }
3619 }
Robert Ginda928cf632014-03-05 15:07:41 -08003620 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003621 if (!this.scrollBlockerNode_.engaged) {
3622 if (e.type == 'mousedown') {
3623 // Move the scroll-blocker into place if we want to keep the scrollport
3624 // from scrolling.
3625 this.scrollBlockerNode_.engaged = true;
3626 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3627 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3628 } else if (e.type == 'mousemove') {
3629 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3630 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003631 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003632 e.preventDefault();
3633 }
3634 }
Robert Ginda928cf632014-03-05 15:07:41 -08003635
3636 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003637 }
3638
Robert Ginda928cf632014-03-05 15:07:41 -08003639 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3640 // Restore this on mouseup in case it was temporarily defeated with a
3641 // alt-mousedown. Only do this when the selection is empty so that
3642 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003643 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003644 }
rgindad5613292012-06-19 15:40:37 -07003645};
3646
3647/**
3648 * Clients should override this if they care to know about mouse events.
3649 *
3650 * The event parameter will be a normal DOM mouse click event with additional
3651 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003652 *
3653 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003654 */
3655hterm.Terminal.prototype.onMouse = function(e) { };
3656
3657/**
rginda8e92a692012-05-20 19:37:20 -07003658 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003659 *
3660 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003661 */
Rob Spies06533ba2014-04-24 11:20:37 -07003662hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3663 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003664 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003665
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003666 if (this.reportFocus)
3667 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O');
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003668
Michael Kelly485ecd12014-06-09 11:41:56 -04003669 if (focused === true)
3670 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07003671};
3672
3673/**
rginda8ba33642011-12-14 12:31:31 -08003674 * React when the ScrollPort is scrolled.
3675 */
3676hterm.Terminal.prototype.onScroll_ = function() {
3677 this.scheduleSyncCursorPosition_();
3678};
3679
3680/**
rginda9846e2f2012-01-27 13:53:33 -08003681 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003682 *
3683 * @param {Event} e The DOM paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08003684 */
3685hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaa063b202014-07-21 11:08:25 -07003686 var data = e.text.replace(/\n/mg, '\r');
Mike Frysingere8c32c82018-03-11 14:57:28 -07003687 if (this.options_.bracketedPaste) {
3688 // We strip out most escape sequences as they can cause issues (like
3689 // inserting an \x1b[201~ midstream). We pass through whitespace
3690 // though: 0x08:\b 0x09:\t 0x0a:\n 0x0d:\r.
3691 // This matches xterm behavior.
3692 const filter = (data) => data.replace(/[\x00-\x07\x0b-\x0c\x0e-\x1f]/g, '');
3693 data = '\x1b[200~' + filter(data) + '\x1b[201~';
3694 }
Robert Gindaa063b202014-07-21 11:08:25 -07003695
3696 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003697};
3698
3699/**
rgindaa09e7332012-08-17 12:49:51 -07003700 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003701 *
3702 * @param {Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07003703 */
3704hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07003705 if (!this.useDefaultWindowCopy) {
3706 e.preventDefault();
3707 setTimeout(this.copySelectionToClipboard.bind(this), 0);
3708 }
rgindaa09e7332012-08-17 12:49:51 -07003709};
3710
3711/**
rginda8ba33642011-12-14 12:31:31 -08003712 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08003713 *
3714 * Note: This function should not directly contain code that alters the internal
3715 * state of the terminal. That kind of code belongs in realizeWidth or
3716 * realizeHeight, so that it can be executed synchronously in the case of a
3717 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08003718 */
3719hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08003720 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07003721 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08003722 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07003723 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08003724
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003725 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08003726 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003727 // gets removed from the document or during the initial load, and we can't
3728 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07003729 // This can also happen if called before the scrollPort calculates the
3730 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08003731 return;
3732 }
3733
rgindaa8ba17d2012-08-15 14:41:10 -07003734 var isNewSize = (columnCount != this.screenSize.width ||
3735 rowCount != this.screenSize.height);
3736
3737 // We do this even if the size didn't change, just to be sure everything is
3738 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04003739 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07003740 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07003741
3742 if (isNewSize)
3743 this.overlaySize();
3744
Robert Gindafb1be6a2013-12-11 11:56:22 -08003745 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07003746 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08003747};
3748
3749/**
3750 * Service the cursor blink timeout.
3751 */
3752hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07003753 if (!this.options_.cursorBlink) {
3754 delete this.timeouts_.cursorBlink;
3755 return;
3756 }
3757
Robert Ginda830583c2013-08-07 13:20:46 -07003758 if (this.cursorNode_.getAttribute('focus') == 'false' ||
3759 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08003760 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07003761 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3762 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08003763 } else {
rginda87b86462011-12-14 13:48:03 -08003764 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07003765 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3766 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08003767 }
3768};
David Reveman8f552492012-03-28 12:18:41 -04003769
3770/**
3771 * Set the scrollbar-visible mode bit.
3772 *
3773 * If scrollbar-visible is on, the vertical scrollbar will be visible.
3774 * Otherwise it will not.
3775 *
3776 * Defaults to on.
3777 *
3778 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
3779 */
3780hterm.Terminal.prototype.setScrollbarVisible = function(state) {
3781 this.scrollPort_.setScrollbarVisible(state);
3782};
Michael Kelly485ecd12014-06-09 11:41:56 -04003783
3784/**
Rob Spies49039e52014-12-17 13:40:04 -08003785 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04003786 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08003787 *
3788 * Defaults to 1.
3789 *
Evan Jones2600d4f2016-12-06 09:29:36 -05003790 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08003791 */
3792hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3793 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3794};
3795
3796/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003797 * Close all web notifications created by terminal bells.
3798 */
3799hterm.Terminal.prototype.closeBellNotifications_ = function() {
3800 this.bellNotificationList_.forEach(function(n) {
3801 n.close();
3802 });
3803 this.bellNotificationList_.length = 0;
3804};
Raymes Khourye5d48982018-08-02 09:08:32 +10003805
3806/**
3807 * Syncs the cursor position when the scrollport gains focus.
3808 */
3809hterm.Terminal.prototype.onScrollportFocus_ = function() {
3810 // If the cursor is offscreen we set selection to the last row on the screen.
3811 const topRowIndex = this.scrollPort_.getTopRowIndex();
3812 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
3813 const selection = this.document_.getSelection();
3814 if (!this.syncCursorPosition_() && selection) {
3815 selection.collapse(this.getRowNode(bottomRowIndex));
3816 }
3817};