blob: c8fcbb431d90c10a5317553d983205d2d2be68fa [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.
Joel Hockey8081ea62019-08-26 16:52:32 -0700718 * @param {string} commandName The command to run for this terminal.
719 * @param {!Array<string>} args The arguments to pass to the command.
rginda87b86462011-12-14 13:48:03 -0800720 */
Joel Hockey8081ea62019-08-26 16:52:32 -0700721hterm.Terminal.prototype.runCommandClass = function(
722 commandClass, commandName, args) {
rgindaf522ce02012-04-17 17:49:17 -0700723 var environment = this.prefs_.get('environment');
724 if (typeof environment != 'object' || environment == null)
725 environment = {};
726
rginda87b86462011-12-14 13:48:03 -0800727 var self = this;
728 this.command = new commandClass(
Joel Hockey8081ea62019-08-26 16:52:32 -0700729 {
730 commandName: commandName,
731 args: args,
rginda87b86462011-12-14 13:48:03 -0800732 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700733 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800734 onExit: function(code) {
735 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800736 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700737 if (self.prefs_.get('close-on-exit'))
738 window.close();
rginda87b86462011-12-14 13:48:03 -0800739 }
740 });
741
rgindafeaf3142012-01-31 15:14:20 -0800742 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800743 this.command.run();
744};
745
746/**
rgindafeaf3142012-01-31 15:14:20 -0800747 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500748 *
749 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800750 */
751hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700752 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800753};
754
755/**
756 * Install the keyboard handler for this terminal.
757 *
758 * This will prevent the browser from seeing any keystrokes sent to the
759 * terminal.
760 */
761hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700762 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400763};
rgindafeaf3142012-01-31 15:14:20 -0800764
765/**
766 * Uninstall the keyboard handler for this terminal.
767 */
768hterm.Terminal.prototype.uninstallKeyboard = function() {
769 this.keyboard.installKeyboard(null);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400770};
rgindafeaf3142012-01-31 15:14:20 -0800771
772/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400773 * Set a CSS variable.
774 *
775 * Normally this is used to set variables in the hterm namespace.
776 *
777 * @param {string} name The variable to set.
778 * @param {string} value The value to assign to the variable.
779 * @param {string?} opt_prefix The variable namespace/prefix to use.
780 */
781hterm.Terminal.prototype.setCssVar = function(name, value,
782 opt_prefix='--hterm-') {
783 this.document_.documentElement.style.setProperty(
784 `${opt_prefix}${name}`, value);
785};
786
787/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500788 * Get a CSS variable.
789 *
790 * Normally this is used to get variables in the hterm namespace.
791 *
792 * @param {string} name The variable to read.
793 * @param {string?} opt_prefix The variable namespace/prefix to use.
794 * @return {string} The current setting for this variable.
795 */
796hterm.Terminal.prototype.getCssVar = function(name, opt_prefix='--hterm-') {
797 return this.document_.documentElement.style.getPropertyValue(
798 `${opt_prefix}${name}`);
799};
800
801/**
rginda35c456b2012-02-09 17:29:05 -0800802 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800803 *
804 * Call setFontSize(0) to reset to the default font size.
805 *
806 * This function does not modify the font-size preference.
807 *
808 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800809 */
810hterm.Terminal.prototype.setFontSize = function(px) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500811 if (px <= 0)
rginda9f5222b2012-03-05 11:53:28 -0800812 px = this.prefs_.get('font-size');
813
rginda35c456b2012-02-09 17:29:05 -0800814 this.scrollPort_.setFontSize(px);
Mike Frysingercce97c42017-08-05 01:11:22 -0400815 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
816 this.setCssVar('charsize-height',
817 this.scrollPort_.characterSize.height + 'px');
rginda35c456b2012-02-09 17:29:05 -0800818};
819
820/**
821 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500822 *
823 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800824 */
825hterm.Terminal.prototype.getFontSize = function() {
826 return this.scrollPort_.getFontSize();
827};
828
829/**
rginda8e92a692012-05-20 19:37:20 -0700830 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500831 *
832 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700833 */
834hterm.Terminal.prototype.getFontFamily = function() {
835 return this.scrollPort_.getFontFamily();
836};
837
838/**
rginda35c456b2012-02-09 17:29:05 -0800839 * Set the CSS "font-family" for this terminal.
840 */
rginda9f5222b2012-03-05 11:53:28 -0800841hterm.Terminal.prototype.syncFontFamily = function() {
842 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
843 this.prefs_.get('font-smoothing'));
844 this.syncBoldSafeState();
845};
846
rginda4bba5e12012-06-20 16:15:30 -0700847/**
848 * Set this.mousePasteButton based on the mouse-paste-button pref,
849 * autodetecting if necessary.
850 */
851hterm.Terminal.prototype.syncMousePasteButton = function() {
852 var button = this.prefs_.get('mouse-paste-button');
853 if (typeof button == 'number') {
854 this.mousePasteButton = button;
855 return;
856 }
857
Mike Frysingeree81a002017-12-12 16:14:53 -0500858 if (hterm.os != 'linux') {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400859 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700860 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400861 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700862 }
863};
864
865/**
866 * Enable or disable bold based on the enable-bold pref, autodetecting if
867 * necessary.
868 */
rginda9f5222b2012-03-05 11:53:28 -0800869hterm.Terminal.prototype.syncBoldSafeState = function() {
870 var enableBold = this.prefs_.get('enable-bold');
871 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700872 this.primaryScreen_.textAttributes.enableBold = enableBold;
873 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800874 return;
875 }
876
rgindaf7521392012-02-28 17:20:34 -0800877 var normalSize = this.scrollPort_.measureCharacterSize();
878 var boldSize = this.scrollPort_.measureCharacterSize('bold');
879
880 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800881 if (!isBoldSafe) {
882 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700883 'from normal. Font family is: ' +
884 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800885 }
rginda9f5222b2012-03-05 11:53:28 -0800886
Robert Gindaed016262012-10-26 16:27:09 -0700887 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
888 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800889};
890
891/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500892 * Control text blinking behavior.
893 *
894 * @param {boolean=} state Whether to enable support for blinking text.
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400895 */
Mike Frysinger261597c2017-12-28 01:14:21 -0500896hterm.Terminal.prototype.setTextBlink = function(state) {
897 if (state === undefined)
898 state = this.prefs_.get('enable-blink');
899 this.setCssVar('blink-node-duration', state ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400900};
901
902/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400903 * Set the mouse cursor style based on the current terminal mode.
904 */
905hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -0400906 this.setCssVar('mouse-cursor-style',
907 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
908 'var(--hterm-mouse-cursor-text)' :
Mike Frysinger67f58f82018-11-22 13:38:22 -0500909 'var(--hterm-mouse-cursor-default)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400910};
911
912/**
rginda87b86462011-12-14 13:48:03 -0800913 * Return a copy of the current cursor position.
914 *
915 * @return {hterm.RowCol} The RowCol object representing the current position.
916 */
917hterm.Terminal.prototype.saveCursor = function() {
918 return this.screen_.cursorPosition.clone();
919};
920
Evan Jones2600d4f2016-12-06 09:29:36 -0500921/**
922 * Return the current text attributes.
923 *
924 * @return {string}
925 */
rgindaa19afe22012-01-25 15:40:22 -0800926hterm.Terminal.prototype.getTextAttributes = function() {
927 return this.screen_.textAttributes;
928};
929
Evan Jones2600d4f2016-12-06 09:29:36 -0500930/**
931 * Set the text attributes.
932 *
933 * @param {string} textAttributes The attributes to set.
934 */
rginda1a09aa02012-06-18 21:11:25 -0700935hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
936 this.screen_.textAttributes = textAttributes;
937};
938
rginda87b86462011-12-14 13:48:03 -0800939/**
rgindaf522ce02012-04-17 17:49:17 -0700940 * Return the current browser zoom factor applied to the terminal.
941 *
942 * @return {number} The current browser zoom factor.
943 */
944hterm.Terminal.prototype.getZoomFactor = function() {
945 return this.scrollPort_.characterSize.zoomFactor;
946};
947
948/**
rginda9846e2f2012-01-27 13:53:33 -0800949 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -0500950 *
951 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -0800952 */
953hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800954 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800955};
956
957/**
rginda87b86462011-12-14 13:48:03 -0800958 * Restore a previously saved cursor position.
959 *
960 * @param {hterm.RowCol} cursor The position to restore.
961 */
962hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700963 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
964 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800965 this.screen_.setCursorPosition(row, column);
966 if (cursor.column > column ||
967 cursor.column == column && cursor.overflow) {
968 this.screen_.cursorPosition.overflow = true;
969 }
rginda87b86462011-12-14 13:48:03 -0800970};
971
972/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400973 * Clear the cursor's overflow flag.
974 */
975hterm.Terminal.prototype.clearCursorOverflow = function() {
976 this.screen_.cursorPosition.overflow = false;
977};
978
979/**
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800980 * Save the current cursor state to the corresponding screens.
981 *
982 * See the hterm.Screen.CursorState class for more details.
983 *
984 * @param {boolean=} both If true, update both screens, else only update the
985 * current screen.
986 */
987hterm.Terminal.prototype.saveCursorAndState = function(both) {
988 if (both) {
989 this.primaryScreen_.saveCursorAndState(this.vt);
990 this.alternateScreen_.saveCursorAndState(this.vt);
991 } else
992 this.screen_.saveCursorAndState(this.vt);
993};
994
995/**
996 * Restore the saved cursor state in the corresponding screens.
997 *
998 * See the hterm.Screen.CursorState class for more details.
999 *
1000 * @param {boolean=} both If true, update both screens, else only update the
1001 * current screen.
1002 */
1003hterm.Terminal.prototype.restoreCursorAndState = function(both) {
1004 if (both) {
1005 this.primaryScreen_.restoreCursorAndState(this.vt);
1006 this.alternateScreen_.restoreCursorAndState(this.vt);
1007 } else
1008 this.screen_.restoreCursorAndState(this.vt);
1009};
1010
1011/**
Robert Ginda830583c2013-08-07 13:20:46 -07001012 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001013 *
1014 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -07001015 */
1016hterm.Terminal.prototype.setCursorShape = function(shape) {
1017 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001018 this.restyleCursor_();
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001019};
Robert Ginda830583c2013-08-07 13:20:46 -07001020
1021/**
1022 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001023 *
1024 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -07001025 */
1026hterm.Terminal.prototype.getCursorShape = function() {
1027 return this.cursorShape_;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001028};
Robert Ginda830583c2013-08-07 13:20:46 -07001029
1030/**
rginda87b86462011-12-14 13:48:03 -08001031 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001032 *
1033 * @param {number} columnCount
rginda87b86462011-12-14 13:48:03 -08001034 */
1035hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -08001036 if (columnCount == null) {
1037 this.div_.style.width = '100%';
1038 return;
1039 }
1040
Robert Ginda26806d12014-07-24 13:44:07 -07001041 this.div_.style.width = Math.ceil(
1042 this.scrollPort_.characterSize.width *
1043 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001044 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -08001045 this.scheduleSyncCursorPosition_();
1046};
rginda87b86462011-12-14 13:48:03 -08001047
rgindac9bc5502012-01-18 11:48:44 -08001048/**
rginda35c456b2012-02-09 17:29:05 -08001049 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001050 *
1051 * @param {number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -08001052 */
1053hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -08001054 if (rowCount == null) {
1055 this.div_.style.height = '100%';
1056 return;
1057 }
1058
rginda35c456b2012-02-09 17:29:05 -08001059 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -07001060 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -08001061 this.realizeSize_(this.screenSize.width, rowCount);
1062 this.scheduleSyncCursorPosition_();
1063};
1064
1065/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001066 * Deal with terminal size changes.
1067 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001068 * @param {number} columnCount The number of columns.
1069 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001070 */
1071hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
Mike Frysinger0206e262019-06-13 10:18:19 -04001072 let notify = false;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001073
Mike Frysinger0206e262019-06-13 10:18:19 -04001074 if (columnCount != this.screenSize.width) {
1075 notify = true;
1076 this.realizeWidth_(columnCount);
1077 }
1078
1079 if (rowCount != this.screenSize.height) {
1080 notify = true;
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001081 this.realizeHeight_(rowCount);
Mike Frysinger0206e262019-06-13 10:18:19 -04001082 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001083
1084 // Send new terminal size to plugin.
Mike Frysinger0206e262019-06-13 10:18:19 -04001085 if (notify) {
1086 this.io.onTerminalResize_(columnCount, rowCount);
1087 }
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001088};
1089
1090/**
rgindac9bc5502012-01-18 11:48:44 -08001091 * Deal with terminal width changes.
1092 *
1093 * This function does what needs to be done when the terminal width changes
1094 * out from under us. It happens here rather than in onResize_() because this
1095 * code may need to run synchronously to handle programmatic changes of
1096 * terminal width.
1097 *
1098 * Relying on the browser to send us an async resize event means we may not be
1099 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001100 *
1101 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001102 */
1103hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001104 if (columnCount <= 0)
1105 throw new Error('Attempt to realize bad width: ' + columnCount);
1106
rgindac9bc5502012-01-18 11:48:44 -08001107 var deltaColumns = columnCount - this.screen_.getWidth();
Mike Frysinger0206e262019-06-13 10:18:19 -04001108 if (deltaColumns == 0) {
1109 // No change, so don't bother recalculating things.
1110 return;
1111 }
rgindac9bc5502012-01-18 11:48:44 -08001112
rginda87b86462011-12-14 13:48:03 -08001113 this.screenSize.width = columnCount;
1114 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001115
1116 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -04001117 if (this.defaultTabStops)
1118 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -08001119 } else {
1120 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -04001121 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -08001122 break;
1123
1124 this.tabStops_.pop();
1125 }
1126 }
1127
1128 this.screen_.setColumnCount(this.screenSize.width);
1129};
1130
1131/**
1132 * Deal with terminal height changes.
1133 *
1134 * This function does what needs to be done when the terminal height changes
1135 * out from under us. It happens here rather than in onResize_() because this
1136 * code may need to run synchronously to handle programmatic changes of
1137 * terminal height.
1138 *
1139 * Relying on the browser to send us an async resize event means we may not be
1140 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001141 *
1142 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001143 */
1144hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001145 if (rowCount <= 0)
1146 throw new Error('Attempt to realize bad height: ' + rowCount);
1147
rgindac9bc5502012-01-18 11:48:44 -08001148 var deltaRows = rowCount - this.screen_.getHeight();
Mike Frysinger0206e262019-06-13 10:18:19 -04001149 if (deltaRows == 0) {
1150 // No change, so don't bother recalculating things.
1151 return;
1152 }
rgindac9bc5502012-01-18 11:48:44 -08001153
1154 this.screenSize.height = rowCount;
1155
1156 var cursor = this.saveCursor();
1157
1158 if (deltaRows < 0) {
1159 // Screen got smaller.
1160 deltaRows *= -1;
1161 while (deltaRows) {
1162 var lastRow = this.getRowCount() - 1;
1163 if (lastRow - this.scrollbackRows_.length == cursor.row)
1164 break;
1165
1166 if (this.getRowText(lastRow))
1167 break;
1168
1169 this.screen_.popRow();
1170 deltaRows--;
1171 }
1172
1173 var ary = this.screen_.shiftRows(deltaRows);
1174 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1175
1176 // We just removed rows from the top of the screen, we need to update
1177 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001178 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001179 } else if (deltaRows > 0) {
1180 // Screen got larger.
1181
1182 if (deltaRows <= this.scrollbackRows_.length) {
1183 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1184 var rows = this.scrollbackRows_.splice(
1185 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1186 this.screen_.unshiftRows(rows);
1187 deltaRows -= scrollbackCount;
1188 cursor.row += scrollbackCount;
1189 }
1190
1191 if (deltaRows)
1192 this.appendRows_(deltaRows);
1193 }
1194
rginda35c456b2012-02-09 17:29:05 -08001195 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001196 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001197};
1198
1199/**
1200 * Scroll the terminal to the top of the scrollback buffer.
1201 */
1202hterm.Terminal.prototype.scrollHome = function() {
1203 this.scrollPort_.scrollRowToTop(0);
1204};
1205
1206/**
1207 * Scroll the terminal to the end.
1208 */
1209hterm.Terminal.prototype.scrollEnd = function() {
1210 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1211};
1212
1213/**
1214 * Scroll the terminal one page up (minus one line) relative to the current
1215 * position.
1216 */
1217hterm.Terminal.prototype.scrollPageUp = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001218 this.scrollPort_.scrollPageUp();
rginda87b86462011-12-14 13:48:03 -08001219};
1220
1221/**
1222 * Scroll the terminal one page down (minus one line) relative to the current
1223 * position.
1224 */
1225hterm.Terminal.prototype.scrollPageDown = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001226 this.scrollPort_.scrollPageDown();
rginda8ba33642011-12-14 12:31:31 -08001227};
1228
rgindac9bc5502012-01-18 11:48:44 -08001229/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001230 * Scroll the terminal one line up relative to the current position.
1231 */
1232hterm.Terminal.prototype.scrollLineUp = function() {
1233 var i = this.scrollPort_.getTopRowIndex();
1234 this.scrollPort_.scrollRowToTop(i - 1);
1235};
1236
1237/**
1238 * Scroll the terminal one line down relative to the current position.
1239 */
1240hterm.Terminal.prototype.scrollLineDown = function() {
1241 var i = this.scrollPort_.getTopRowIndex();
1242 this.scrollPort_.scrollRowToTop(i + 1);
1243};
1244
1245/**
Robert Ginda40932892012-12-10 17:26:40 -08001246 * Clear primary screen, secondary screen, and the scrollback buffer.
1247 */
1248hterm.Terminal.prototype.wipeContents = function() {
Mike Frysinger9c482b82018-09-07 02:49:36 -04001249 this.clearHome(this.primaryScreen_);
1250 this.clearHome(this.alternateScreen_);
1251
1252 this.clearScrollback();
1253};
1254
1255/**
1256 * Clear scrollback buffer.
1257 */
1258hterm.Terminal.prototype.clearScrollback = function() {
1259 // Move to the end of the buffer in case the screen was scrolled back.
1260 // We're going to throw it away which would leave the display invalid.
1261 this.scrollEnd();
1262
Robert Ginda40932892012-12-10 17:26:40 -08001263 this.scrollbackRows_.length = 0;
1264 this.scrollPort_.resetCache();
1265
Mike Frysinger9c482b82018-09-07 02:49:36 -04001266 [this.primaryScreen_, this.alternateScreen_].forEach((screen) => {
1267 const bottom = screen.getHeight();
1268 this.renumberRows_(0, bottom, screen);
1269 });
Robert Ginda40932892012-12-10 17:26:40 -08001270
1271 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001272 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001273};
1274
1275/**
rgindac9bc5502012-01-18 11:48:44 -08001276 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001277 *
1278 * Perform a full reset to the default values listed in
1279 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001280 */
rginda87b86462011-12-14 13:48:03 -08001281hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001282 this.vt.reset();
1283
rgindac9bc5502012-01-18 11:48:44 -08001284 this.clearAllTabStops();
1285 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001286
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001287 const resetScreen = (screen) => {
1288 // We want to make sure to reset the attributes before we clear the screen.
1289 // The attributes might be used to initialize default/empty rows.
1290 screen.textAttributes.reset();
1291 screen.textAttributes.resetColorPalette();
1292 this.clearHome(screen);
1293 screen.saveCursorAndState(this.vt);
1294 };
1295 resetScreen(this.primaryScreen_);
1296 resetScreen(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001297
Mike Frysinger84301d02017-11-29 13:28:46 -08001298 // Reset terminal options to their default values.
1299 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001300 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1301
Mike Frysinger84301d02017-11-29 13:28:46 -08001302 this.setVTScrollRegion(null, null);
1303
1304 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001305};
1306
rgindac9bc5502012-01-18 11:48:44 -08001307/**
1308 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001309 *
1310 * Perform a soft reset to the default values listed in
1311 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001312 */
rginda0f5c0292012-01-13 11:00:13 -08001313hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001314 this.vt.reset();
1315
rgindab8bc8932012-04-27 12:45:03 -07001316 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001317 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001318
Brad Townb62dfdc2015-03-16 19:07:15 -07001319 // We show the cursor on soft reset but do not alter the blink state.
1320 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1321
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001322 const resetScreen = (screen) => {
1323 // Xterm also resets the color palette on soft reset, even though it doesn't
1324 // seem to be documented anywhere.
1325 screen.textAttributes.reset();
1326 screen.textAttributes.resetColorPalette();
1327 screen.saveCursorAndState(this.vt);
1328 };
1329 resetScreen(this.primaryScreen_);
1330 resetScreen(this.alternateScreen_);
rgindaf522ce02012-04-17 17:49:17 -07001331
rgindab8bc8932012-04-27 12:45:03 -07001332 // The xterm man page explicitly says this will happen on soft reset.
1333 this.setVTScrollRegion(null, null);
1334
1335 // Xterm also shows the cursor on soft reset, but does not alter the blink
1336 // state.
rgindaa19afe22012-01-25 15:40:22 -08001337 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001338};
1339
rgindac9bc5502012-01-18 11:48:44 -08001340/**
1341 * Move the cursor forward to the next tab stop, or to the last column
1342 * if no more tab stops are set.
1343 */
1344hterm.Terminal.prototype.forwardTabStop = function() {
1345 var column = this.screen_.cursorPosition.column;
1346
1347 for (var i = 0; i < this.tabStops_.length; i++) {
1348 if (this.tabStops_[i] > column) {
1349 this.setCursorColumn(this.tabStops_[i]);
1350 return;
1351 }
1352 }
1353
David Benjamin66e954d2012-05-05 21:08:12 -04001354 // xterm does not clear the overflow flag on HT or CHT.
1355 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001356 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001357 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001358};
1359
rgindac9bc5502012-01-18 11:48:44 -08001360/**
1361 * Move the cursor backward to the previous tab stop, or to the first column
1362 * if no previous tab stops are set.
1363 */
1364hterm.Terminal.prototype.backwardTabStop = function() {
1365 var column = this.screen_.cursorPosition.column;
1366
1367 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1368 if (this.tabStops_[i] < column) {
1369 this.setCursorColumn(this.tabStops_[i]);
1370 return;
1371 }
1372 }
1373
1374 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001375};
1376
rgindac9bc5502012-01-18 11:48:44 -08001377/**
1378 * Set a tab stop at the given column.
1379 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001380 * @param {integer} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001381 */
1382hterm.Terminal.prototype.setTabStop = function(column) {
1383 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1384 if (this.tabStops_[i] == column)
1385 return;
1386
1387 if (this.tabStops_[i] < column) {
1388 this.tabStops_.splice(i + 1, 0, column);
1389 return;
1390 }
1391 }
1392
1393 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001394};
1395
rgindac9bc5502012-01-18 11:48:44 -08001396/**
1397 * Clear the tab stop at the current cursor position.
1398 *
1399 * No effect if there is no tab stop at the current cursor position.
1400 */
1401hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1402 var column = this.screen_.cursorPosition.column;
1403
1404 var i = this.tabStops_.indexOf(column);
1405 if (i == -1)
1406 return;
1407
1408 this.tabStops_.splice(i, 1);
1409};
1410
1411/**
1412 * Clear all tab stops.
1413 */
1414hterm.Terminal.prototype.clearAllTabStops = function() {
1415 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001416 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001417};
1418
1419/**
1420 * Set up the default tab stops, starting from a given column.
1421 *
1422 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001423 * from the specified column, or 0 if no column is provided. It also flags
1424 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001425 *
1426 * This does not clear the existing tab stops first, use clearAllTabStops
1427 * for that.
1428 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001429 * @param {integer} opt_start Optional starting zero based starting column, useful
rgindac9bc5502012-01-18 11:48:44 -08001430 * for filling out missing tab stops when the terminal is resized.
1431 */
1432hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1433 var start = opt_start || 0;
1434 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001435 // Round start up to a default tab stop.
1436 start = start - 1 - ((start - 1) % w) + w;
1437 for (var i = start; i < this.screenSize.width; i += w) {
1438 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001439 }
David Benjamin66e954d2012-05-05 21:08:12 -04001440
1441 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001442};
1443
rginda6d397402012-01-17 10:58:29 -08001444/**
rginda8ba33642011-12-14 12:31:31 -08001445 * Interpret a sequence of characters.
1446 *
1447 * Incomplete escape sequences are buffered until the next call.
1448 *
1449 * @param {string} str Sequence of characters to interpret or pass through.
1450 */
1451hterm.Terminal.prototype.interpret = function(str) {
rginda8ba33642011-12-14 12:31:31 -08001452 this.scheduleSyncCursorPosition_();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001453 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001454};
1455
1456/**
1457 * Take over the given DIV for use as the terminal display.
1458 *
1459 * @param {HTMLDivElement} div The div to use as the terminal display.
1460 */
1461hterm.Terminal.prototype.decorate = function(div) {
Mike Frysinger5768a9d2017-12-26 12:57:44 -05001462 const charset = div.ownerDocument.characterSet.toLowerCase();
1463 if (charset != 'utf-8') {
1464 console.warn(`Document encoding should be set to utf-8, not "${charset}";` +
1465 ` Add <meta charset='utf-8'/> to your HTML <head> to fix.`);
1466 }
1467
rginda87b86462011-12-14 13:48:03 -08001468 this.div_ = div;
1469
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001470 this.accessibilityReader_ = new hterm.AccessibilityReader(div);
1471
Adrián Pérez-Orozco394e64f2018-12-17 17:20:16 -08001472 this.scrollPort_.decorate(div, () => this.setupScrollPort_());
1473};
1474
1475/**
1476 * Initialisation of ScrollPort properties which need to be set after its DOM
1477 * has been initialised.
1478 * @private
1479 */
1480hterm.Terminal.prototype.setupScrollPort_ = function() {
rginda30f20f62012-04-05 16:36:19 -07001481 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001482 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1483 this.scrollPort_.setBackgroundPosition(
1484 this.prefs_.get('background-position'));
Mike Frysinger08bad432017-04-24 00:50:54 -04001485 this.scrollPort_.setUserCssUrl(this.prefs_.get('user-css'));
1486 this.scrollPort_.setUserCssText(this.prefs_.get('user-css-text'));
Raymes Khoury177aec72018-06-26 10:58:53 +10001487 this.scrollPort_.setAccessibilityReader(this.accessibilityReader_);
rginda30f20f62012-04-05 16:36:19 -07001488
rginda0918b652012-04-04 11:26:24 -07001489 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001490
rginda9f5222b2012-03-05 11:53:28 -08001491 this.setFontSize(this.prefs_.get('font-size'));
1492 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001493
David Reveman8f552492012-03-28 12:18:41 -04001494 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001495 this.setScrollWheelMoveMultipler(
1496 this.prefs_.get('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001497
rginda8ba33642011-12-14 12:31:31 -08001498 this.document_ = this.scrollPort_.getDocument();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001499 this.accessibilityReader_.decorate(this.document_);
rginda8ba33642011-12-14 12:31:31 -08001500
Evan Jones5f9df812016-12-06 09:38:58 -05001501 this.document_.body.oncontextmenu = function() { return false; };
Mike Frysingercc114512017-09-11 21:39:17 -04001502 this.contextMenu.setDocument(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07001503
1504 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001505 var screenNode = this.scrollPort_.getScreenNode();
1506 screenNode.addEventListener('mousedown', onMouse);
1507 screenNode.addEventListener('mouseup', onMouse);
1508 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001509 this.scrollPort_.onScrollWheel = onMouse;
1510
Mike Frysinger02ded6d2018-06-21 14:25:20 -04001511 screenNode.addEventListener('keydown', this.onKeyboardActivity_.bind(this));
1512
Toni Barzic0bfa8922013-11-22 11:18:35 -08001513 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001514 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001515 // Listen for mousedown events on the screenNode as in FF the focus
1516 // events don't bubble.
1517 screenNode.addEventListener('mousedown', function() {
1518 setTimeout(this.onFocusChange_.bind(this, true));
1519 }.bind(this));
1520
Toni Barzic0bfa8922013-11-22 11:18:35 -08001521 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001522 'blur', this.onFocusChange_.bind(this, false));
1523
1524 var style = this.document_.createElement('style');
1525 style.textContent =
1526 ('.cursor-node[focus="false"] {' +
1527 ' box-sizing: border-box;' +
1528 ' background-color: transparent !important;' +
1529 ' border-width: 2px;' +
1530 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001531 '}' +
Mike Frysingercc114512017-09-11 21:39:17 -04001532 'menu {' +
1533 ' margin: 0;' +
1534 ' padding: 0;' +
1535 ' cursor: var(--hterm-mouse-cursor-pointer);' +
1536 '}' +
1537 'menuitem {' +
1538 ' white-space: nowrap;' +
1539 ' border-bottom: 1px dashed;' +
1540 ' display: block;' +
1541 ' padding: 0.3em 0.3em 0 0.3em;' +
1542 '}' +
1543 'menuitem.separator {' +
1544 ' border-bottom: none;' +
1545 ' height: 0.5em;' +
1546 ' padding: 0;' +
1547 '}' +
1548 'menuitem:hover {' +
1549 ' color: var(--hterm-cursor-color);' +
1550 '}' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001551 '.wc-node {' +
1552 ' display: inline-block;' +
1553 ' text-align: center;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001554 ' width: calc(var(--hterm-charsize-width) * 2);' +
Mike Frysinger4036f6e2017-05-31 14:02:55 -04001555 ' line-height: var(--hterm-charsize-height);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001556 '}' +
1557 ':root {' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001558 ' --hterm-charsize-width: ' + this.scrollPort_.characterSize.width + 'px;' +
1559 ' --hterm-charsize-height: ' + this.scrollPort_.characterSize.height + 'px;' +
Mike Frysingera27c0502017-08-23 22:37:10 -04001560 // Default position hides the cursor for when the window is initializing.
1561 ' --hterm-cursor-offset-col: -1;' +
1562 ' --hterm-cursor-offset-row: -1;' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001563 ' --hterm-blink-node-duration: 0.7s;' +
Mike Frysinger67f58f82018-11-22 13:38:22 -05001564 ' --hterm-mouse-cursor-default: default;' +
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001565 ' --hterm-mouse-cursor-text: text;' +
Mike Frysinger67f58f82018-11-22 13:38:22 -05001566 ' --hterm-mouse-cursor-pointer: pointer;' +
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001567 ' --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001568 '}' +
Mike Frysingerd3907202017-10-23 01:23:50 -04001569 '.uri-node:hover {' +
1570 ' text-decoration: underline;' +
Mike Frysinger67f58f82018-11-22 13:38:22 -05001571 ' cursor: var(--hterm-mouse-cursor-pointer);' +
Mike Frysingerd3907202017-10-23 01:23:50 -04001572 '}' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001573 '@keyframes blink {' +
1574 ' from { opacity: 1.0; }' +
1575 ' to { opacity: 0.0; }' +
1576 '}' +
1577 '.blink-node {' +
1578 ' animation-name: blink;' +
1579 ' animation-duration: var(--hterm-blink-node-duration);' +
1580 ' animation-iteration-count: infinite;' +
1581 ' animation-timing-function: ease-in-out;' +
1582 ' animation-direction: alternate;' +
rginda8e92a692012-05-20 19:37:20 -07001583 '}');
Mike Frysingerb74a6472018-06-22 13:37:08 -04001584 // Insert this stock style as the first node so that any user styles will
1585 // override w/out having to use !important everywhere. The rules above mix
1586 // runtime variables with default ones designed to be overridden by the user,
1587 // but we can wait for a concrete case from the users to determine the best
1588 // way to split the sheet up to before & after the user-css settings.
1589 this.document_.head.insertBefore(style, this.document_.head.firstChild);
rginda8e92a692012-05-20 19:37:20 -07001590
rginda8ba33642011-12-14 12:31:31 -08001591 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001592 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001593 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001594 this.cursorNode_.style.cssText =
1595 ('position: absolute;' +
Mike Frysinger44c32202017-08-05 01:13:09 -04001596 'left: calc(var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));' +
1597 'top: calc(var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));' +
Mike Frysingerd60f4c22018-03-15 21:25:30 -07001598 'display: ' + (this.options_.cursorVisible ? '' : 'none') + ';' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001599 'width: var(--hterm-charsize-width);' +
1600 'height: var(--hterm-charsize-height);' +
Mike Frysinger2fd079a2018-09-02 01:46:12 -04001601 'background-color: var(--hterm-cursor-color);' +
1602 'border-color: var(--hterm-cursor-color);' +
Rob Spies06533ba2014-04-24 11:20:37 -07001603 '-webkit-transition: opacity, background-color 100ms linear;' +
1604 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001605
Mike Frysingerf02a2cb2017-12-21 00:34:03 -05001606 this.setCursorColor();
Robert Gindafb1be6a2013-12-11 11:56:22 -08001607 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1608 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001609
rginda8ba33642011-12-14 12:31:31 -08001610 this.document_.body.appendChild(this.cursorNode_);
1611
rgindad5613292012-06-19 15:40:37 -07001612 // When 'enableMouseDragScroll' is off we reposition this element directly
1613 // under the mouse cursor after a click. This makes Chrome associate
1614 // subsequent mousemove events with the scroll-blocker. Since the
1615 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1616 // events do not cause the scrollport to scroll.
1617 //
1618 // It's a hack, but it's the cleanest way I could find.
1619 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001620 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
Raymes Khoury6dce2f82018-04-12 15:38:58 +10001621 this.scrollBlockerNode_.setAttribute('aria-hidden', 'true');
rgindad5613292012-06-19 15:40:37 -07001622 this.scrollBlockerNode_.style.cssText =
1623 ('position: absolute;' +
1624 'top: -99px;' +
1625 'display: block;' +
1626 'width: 10px;' +
1627 'height: 10px;');
1628 this.document_.body.appendChild(this.scrollBlockerNode_);
1629
rgindad5613292012-06-19 15:40:37 -07001630 this.scrollPort_.onScrollWheel = onMouse;
1631 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1632 ].forEach(function(event) {
1633 this.scrollBlockerNode_.addEventListener(event, onMouse);
1634 this.cursorNode_.addEventListener(event, onMouse);
1635 this.document_.addEventListener(event, onMouse);
1636 }.bind(this));
1637
1638 this.cursorNode_.addEventListener('mousedown', function() {
1639 setTimeout(this.focus.bind(this));
1640 }.bind(this));
1641
rginda8ba33642011-12-14 12:31:31 -08001642 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001643
rginda87b86462011-12-14 13:48:03 -08001644 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001645 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001646};
1647
rginda0918b652012-04-04 11:26:24 -07001648/**
1649 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001650 *
1651 * @return {HTMLDocument}
rginda0918b652012-04-04 11:26:24 -07001652 */
rginda87b86462011-12-14 13:48:03 -08001653hterm.Terminal.prototype.getDocument = function() {
1654 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001655};
1656
1657/**
rginda0918b652012-04-04 11:26:24 -07001658 * Focus the terminal.
1659 */
1660hterm.Terminal.prototype.focus = function() {
1661 this.scrollPort_.focus();
1662};
1663
1664/**
rginda8ba33642011-12-14 12:31:31 -08001665 * Return the HTML Element for a given row index.
1666 *
1667 * This is a method from the RowProvider interface. The ScrollPort uses
1668 * it to fetch rows on demand as they are scrolled into view.
1669 *
1670 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1671 * pairs to conserve memory.
1672 *
1673 * @param {integer} index The zero-based row index, measured relative to the
1674 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001675 * largest indices.
rginda8ba33642011-12-14 12:31:31 -08001676 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1677 */
1678hterm.Terminal.prototype.getRowNode = function(index) {
1679 if (index < this.scrollbackRows_.length)
1680 return this.scrollbackRows_[index];
1681
1682 var screenIndex = index - this.scrollbackRows_.length;
1683 return this.screen_.rowsArray[screenIndex];
1684};
1685
1686/**
1687 * Return the text content for a given range of rows.
1688 *
1689 * This is a method from the RowProvider interface. The ScrollPort uses
1690 * it to fetch text content on demand when the user attempts to copy their
1691 * selection to the clipboard.
1692 *
1693 * @param {integer} start The zero-based row index to start from, measured
1694 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001695 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001696 * @param {integer} end The zero-based row index to end on, measured
1697 * relative to the start of the scrollback buffer.
1698 * @return {string} A single string containing the text value of the range of
1699 * rows. Lines will be newline delimited, with no trailing newline.
1700 */
1701hterm.Terminal.prototype.getRowsText = function(start, end) {
1702 var ary = [];
1703 for (var i = start; i < end; i++) {
1704 var node = this.getRowNode(i);
1705 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001706 if (i < end - 1 && !node.getAttribute('line-overflow'))
1707 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001708 }
1709
rgindaa09e7332012-08-17 12:49:51 -07001710 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001711};
1712
1713/**
1714 * Return the text content for a given row.
1715 *
1716 * This is a method from the RowProvider interface. The ScrollPort uses
1717 * it to fetch text content on demand when the user attempts to copy their
1718 * selection to the clipboard.
1719 *
1720 * @param {integer} index The zero-based row index to return, measured
1721 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001722 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001723 * @return {string} A string containing the text value of the selected row.
1724 */
1725hterm.Terminal.prototype.getRowText = function(index) {
1726 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001727 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001728};
1729
1730/**
1731 * Return the total number of rows in the addressable screen and in the
1732 * scrollback buffer of this terminal.
1733 *
1734 * This is a method from the RowProvider interface. The ScrollPort uses
1735 * it to compute the size of the scrollbar.
1736 *
1737 * @return {integer} The number of rows in this terminal.
1738 */
1739hterm.Terminal.prototype.getRowCount = function() {
1740 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1741};
1742
1743/**
1744 * Create DOM nodes for new rows and append them to the end of the terminal.
1745 *
1746 * This is the only correct way to add a new DOM node for a row. Notice that
1747 * the new row is appended to the bottom of the list of rows, and does not
1748 * require renumbering (of the rowIndex property) of previous rows.
1749 *
1750 * If you think you want a new blank row somewhere in the middle of the
1751 * terminal, look into moveRows_().
1752 *
1753 * This method does not pay attention to vtScrollTop/Bottom, since you should
1754 * be using moveRows() in cases where they would matter.
1755 *
1756 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001757 *
1758 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001759 */
1760hterm.Terminal.prototype.appendRows_ = function(count) {
1761 var cursorRow = this.screen_.rowsArray.length;
1762 var offset = this.scrollbackRows_.length + cursorRow;
1763 for (var i = 0; i < count; i++) {
1764 var row = this.document_.createElement('x-row');
1765 row.appendChild(this.document_.createTextNode(''));
1766 row.rowIndex = offset + i;
1767 this.screen_.pushRow(row);
1768 }
1769
1770 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1771 if (extraRows > 0) {
1772 var ary = this.screen_.shiftRows(extraRows);
1773 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001774 if (this.scrollPort_.isScrolledEnd)
1775 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001776 }
1777
1778 if (cursorRow >= this.screen_.rowsArray.length)
1779 cursorRow = this.screen_.rowsArray.length - 1;
1780
rginda87b86462011-12-14 13:48:03 -08001781 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001782};
1783
1784/**
1785 * Relocate rows from one part of the addressable screen to another.
1786 *
1787 * This is used to recycle rows during VT scrolls (those which are driven
1788 * by VT commands, rather than by the user manipulating the scrollbar.)
1789 *
1790 * In this case, the blank lines scrolled into the scroll region are made of
1791 * the nodes we scrolled off. These have their rowIndex properties carefully
1792 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001793 *
1794 * @param {number} fromIndex The start index.
1795 * @param {number} count The number of rows to move.
1796 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001797 */
1798hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1799 var ary = this.screen_.removeRows(fromIndex, count);
1800 this.screen_.insertRows(toIndex, ary);
1801
1802 var start, end;
1803 if (fromIndex < toIndex) {
1804 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001805 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001806 } else {
1807 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001808 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001809 }
1810
1811 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001812 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001813};
1814
1815/**
1816 * Renumber the rowIndex property of the given range of rows.
1817 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001818 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08001819 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001820 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001821 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05001822 *
1823 * @param {number} start The start index.
1824 * @param {number} end The end index.
1825 * @param {hterm.Screen} opt_screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08001826 */
Robert Ginda40932892012-12-10 17:26:40 -08001827hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1828 var screen = opt_screen || this.screen_;
1829
rginda8ba33642011-12-14 12:31:31 -08001830 var offset = this.scrollbackRows_.length;
1831 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001832 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001833 }
1834};
1835
1836/**
1837 * Print a string to the terminal.
1838 *
1839 * This respects the current insert and wraparound modes. It will add new lines
1840 * to the end of the terminal, scrolling off the top into the scrollback buffer
1841 * if necessary.
1842 *
1843 * The string is *not* parsed for escape codes. Use the interpret() method if
1844 * that's what you're after.
1845 *
1846 * @param{string} str The string to print.
1847 */
1848hterm.Terminal.prototype.print = function(str) {
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001849 this.scheduleSyncCursorPosition_();
1850
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001851 // Basic accessibility output for the screen reader.
Raymes Khoury177aec72018-06-26 10:58:53 +10001852 this.accessibilityReader_.announce(str);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001853
rgindaa9abdd82012-08-06 18:05:09 -07001854 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001855
Ricky Liang48f05cb2013-12-31 23:35:29 +08001856 var strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04001857 // Fun edge case: If the string only contains zero width codepoints (like
1858 // combining characters), we make sure to iterate at least once below.
1859 if (strWidth == 0 && str)
1860 strWidth = 1;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001861
1862 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001863 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1864 this.screen_.commitLineOverflow();
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001865 this.newLine(true);
rgindaa09e7332012-08-17 12:49:51 -07001866 }
rgindaa19afe22012-01-25 15:40:22 -08001867
Ricky Liang48f05cb2013-12-31 23:35:29 +08001868 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001869 var didOverflow = false;
1870 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001871
rgindaa9abdd82012-08-06 18:05:09 -07001872 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1873 didOverflow = true;
1874 count = this.screenSize.width - this.screen_.cursorPosition.column;
1875 }
rgindaa19afe22012-01-25 15:40:22 -08001876
rgindaa9abdd82012-08-06 18:05:09 -07001877 if (didOverflow && !this.options_.wraparound) {
1878 // If the string overflowed the line but wraparound is off, then the
1879 // last printed character should be the last of the string.
1880 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001881 substr = lib.wc.substr(str, startOffset, count - 1) +
1882 lib.wc.substr(str, strWidth - 1);
1883 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001884 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001885 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001886 }
rgindaa19afe22012-01-25 15:40:22 -08001887
Ricky Liang48f05cb2013-12-31 23:35:29 +08001888 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1889 for (var i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001890 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
1891 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001892
1893 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001894 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001895 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001896 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001897 }
1898 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001899 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07001900 }
1901
1902 this.screen_.maybeClipCurrentRow();
1903 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001904 }
rginda8ba33642011-12-14 12:31:31 -08001905
rginda9f5222b2012-03-05 11:53:28 -08001906 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001907 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001908};
1909
1910/**
rginda87b86462011-12-14 13:48:03 -08001911 * Set the VT scroll region.
1912 *
rginda87b86462011-12-14 13:48:03 -08001913 * This also resets the cursor position to the absolute (0, 0) position, since
1914 * that's what xterm appears to do.
1915 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001916 * Setting the scroll region to the full height of the terminal will clear
1917 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1918 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1919 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1920 * continue to work as most users would expect.
1921 *
rginda87b86462011-12-14 13:48:03 -08001922 * @param {integer} scrollTop The zero-based top of the scroll region.
1923 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1924 * inclusive.
1925 */
1926hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001927 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001928 this.vtScrollTop_ = null;
1929 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001930 } else {
1931 this.vtScrollTop_ = scrollTop;
1932 this.vtScrollBottom_ = scrollBottom;
1933 }
rginda87b86462011-12-14 13:48:03 -08001934};
1935
1936/**
rginda8ba33642011-12-14 12:31:31 -08001937 * Return the top row index according to the VT.
1938 *
1939 * This will return 0 unless the terminal has been told to restrict scrolling
1940 * to some lower row. It is used for some VT cursor positioning and scrolling
1941 * commands.
1942 *
1943 * @return {integer} The topmost row in the terminal's scroll region.
1944 */
1945hterm.Terminal.prototype.getVTScrollTop = function() {
1946 if (this.vtScrollTop_ != null)
1947 return this.vtScrollTop_;
1948
1949 return 0;
rginda87b86462011-12-14 13:48:03 -08001950};
rginda8ba33642011-12-14 12:31:31 -08001951
1952/**
1953 * Return the bottom row index according to the VT.
1954 *
1955 * This will return the height of the terminal unless the it has been told to
1956 * restrict scrolling to some higher row. It is used for some VT cursor
1957 * positioning and scrolling commands.
1958 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001959 * @return {integer} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001960 */
1961hterm.Terminal.prototype.getVTScrollBottom = function() {
1962 if (this.vtScrollBottom_ != null)
1963 return this.vtScrollBottom_;
1964
rginda87b86462011-12-14 13:48:03 -08001965 return this.screenSize.height - 1;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001966};
rginda8ba33642011-12-14 12:31:31 -08001967
1968/**
1969 * Process a '\n' character.
1970 *
1971 * If the cursor is on the final row of the terminal this will append a new
1972 * blank row to the screen and scroll the topmost row into the scrollback
1973 * buffer.
1974 *
1975 * Otherwise, this moves the cursor to column zero of the next row.
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001976 *
1977 * @param {boolean=} dueToOverflow Whether the newline is due to wraparound of
1978 * the terminal.
rginda8ba33642011-12-14 12:31:31 -08001979 */
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001980hterm.Terminal.prototype.newLine = function(dueToOverflow = false) {
1981 if (!dueToOverflow)
1982 this.accessibilityReader_.newLine();
1983
Robert Ginda9937abc2013-07-25 16:09:23 -07001984 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1985 this.screen_.rowsArray.length - 1);
1986
1987 if (this.vtScrollBottom_ != null) {
1988 // A VT Scroll region is active, we never append new rows.
1989 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1990 // We're at the end of the VT Scroll Region, perform a VT scroll.
1991 this.vtScrollUp(1);
1992 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1993 } else if (cursorAtEndOfScreen) {
1994 // We're at the end of the screen, the only thing to do is put the
1995 // cursor to column 0.
1996 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1997 } else {
1998 // Anywhere else, advance the cursor row, and reset the column.
1999 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
2000 }
2001 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07002002 // We're at the end of the screen. Append a new row to the terminal,
2003 // shifting the top row into the scrollback.
2004 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08002005 } else {
rginda87b86462011-12-14 13:48:03 -08002006 // Anywhere else in the screen just moves the cursor.
2007 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08002008 }
2009};
2010
2011/**
2012 * Like newLine(), except maintain the cursor column.
2013 */
2014hterm.Terminal.prototype.lineFeed = function() {
2015 var column = this.screen_.cursorPosition.column;
2016 this.newLine();
2017 this.setCursorColumn(column);
2018};
2019
2020/**
rginda87b86462011-12-14 13:48:03 -08002021 * If autoCarriageReturn is set then newLine(), else lineFeed().
2022 */
2023hterm.Terminal.prototype.formFeed = function() {
2024 if (this.options_.autoCarriageReturn) {
2025 this.newLine();
2026 } else {
2027 this.lineFeed();
2028 }
2029};
2030
2031/**
2032 * Move the cursor up one row, possibly inserting a blank line.
2033 *
2034 * The cursor column is not changed.
2035 */
2036hterm.Terminal.prototype.reverseLineFeed = function() {
2037 var scrollTop = this.getVTScrollTop();
2038 var currentRow = this.screen_.cursorPosition.row;
2039
2040 if (currentRow == scrollTop) {
2041 this.insertLines(1);
2042 } else {
2043 this.setAbsoluteCursorRow(currentRow - 1);
2044 }
2045};
2046
2047/**
rginda8ba33642011-12-14 12:31:31 -08002048 * Replace all characters to the left of the current cursor with the space
2049 * character.
2050 *
2051 * TODO(rginda): This should probably *remove* the characters (not just replace
2052 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07002053 * position.
rginda8ba33642011-12-14 12:31:31 -08002054 */
2055hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08002056 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002057 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002058 const count = cursor.column + 1;
Mike Frysinger73e56462019-07-17 00:23:46 -05002059 this.screen_.overwriteString(' '.repeat(count), count);
rginda87b86462011-12-14 13:48:03 -08002060 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002061};
2062
2063/**
David Benjamin684a9b72012-05-01 17:19:58 -04002064 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08002065 *
2066 * The cursor position is unchanged.
2067 *
Robert Gindaf2547f12012-10-25 20:36:21 -07002068 * If the current background color is not the default background color this
2069 * will insert spaces rather than delete. This is unfortunate because the
2070 * trailing space will affect text selection, but it's difficult to come up
2071 * with a way to style empty space that wouldn't trip up the hterm.Screen
2072 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07002073 *
2074 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
2075 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
2076 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05002077 *
2078 * @param {number} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08002079 */
2080hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07002081 if (this.screen_.cursorPosition.overflow)
2082 return;
2083
Robert Ginda7fd57082012-09-25 14:41:47 -07002084 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
2085 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07002086
2087 if (this.screen_.textAttributes.background ===
2088 this.screen_.textAttributes.DEFAULT_COLOR) {
2089 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08002090 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07002091 this.screen_.cursorPosition.column + count) {
2092 this.screen_.deleteChars(count);
2093 this.clearCursorOverflow();
2094 return;
2095 }
2096 }
2097
rginda87b86462011-12-14 13:48:03 -08002098 var cursor = this.saveCursor();
Mike Frysinger73e56462019-07-17 00:23:46 -05002099 this.screen_.overwriteString(' '.repeat(count), count);
rginda87b86462011-12-14 13:48:03 -08002100 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002101 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002102};
2103
2104/**
2105 * Erase the current line.
2106 *
2107 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002108 */
2109hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08002110 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002111 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08002112 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002113 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002114};
2115
2116/**
David Benjamina08d78f2012-05-05 00:28:49 -04002117 * Erase all characters from the start of the screen to the current cursor
2118 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002119 *
2120 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002121 */
2122hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08002123 var cursor = this.saveCursor();
2124
2125 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002126
David Benjamina08d78f2012-05-05 00:28:49 -04002127 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002128 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002129 this.screen_.clearCursorRow();
2130 }
2131
rginda87b86462011-12-14 13:48:03 -08002132 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002133 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002134};
2135
2136/**
2137 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002138 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002139 *
2140 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002141 */
2142hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08002143 var cursor = this.saveCursor();
2144
2145 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002146
David Benjamina08d78f2012-05-05 00:28:49 -04002147 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08002148 for (var i = cursor.row + 1; i <= bottom; i++) {
2149 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002150 this.screen_.clearCursorRow();
2151 }
2152
rginda87b86462011-12-14 13:48:03 -08002153 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002154 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002155};
2156
2157/**
2158 * Fill the terminal with a given character.
2159 *
2160 * This methods does not respect the VT scroll region.
2161 *
2162 * @param {string} ch The character to use for the fill.
2163 */
2164hterm.Terminal.prototype.fill = function(ch) {
2165 var cursor = this.saveCursor();
2166
2167 this.setAbsoluteCursorPosition(0, 0);
2168 for (var row = 0; row < this.screenSize.height; row++) {
2169 for (var col = 0; col < this.screenSize.width; col++) {
2170 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002171 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002172 }
2173 }
2174
2175 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002176};
2177
2178/**
rginda9ea433c2012-03-16 11:57:00 -07002179 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002180 *
rginda9ea433c2012-03-16 11:57:00 -07002181 * This does not respect the scroll region.
2182 *
2183 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2184 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002185 */
rginda9ea433c2012-03-16 11:57:00 -07002186hterm.Terminal.prototype.clearHome = function(opt_screen) {
2187 var screen = opt_screen || this.screen_;
2188 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002189
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002190 this.accessibilityReader_.clear();
2191
rginda11057d52012-04-25 12:29:56 -07002192 if (bottom == 0) {
2193 // Empty screen, nothing to do.
2194 return;
2195 }
2196
rgindae4d29232012-01-19 10:47:13 -08002197 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002198 screen.setCursorPosition(i, 0);
2199 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002200 }
2201
rginda9ea433c2012-03-16 11:57:00 -07002202 screen.setCursorPosition(0, 0);
2203};
2204
2205/**
2206 * Erase the entire display without changing the cursor position.
2207 *
2208 * The cursor position is unchanged. This does not respect the scroll
2209 * region.
2210 *
2211 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2212 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002213 */
2214hterm.Terminal.prototype.clear = function(opt_screen) {
2215 var screen = opt_screen || this.screen_;
2216 var cursor = screen.cursorPosition.clone();
2217 this.clearHome(screen);
2218 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002219};
2220
2221/**
2222 * VT command to insert lines at the current cursor row.
2223 *
2224 * This respects the current scroll region. Rows pushed off the bottom are
2225 * lost (they won't show up in the scrollback buffer).
2226 *
rginda8ba33642011-12-14 12:31:31 -08002227 * @param {integer} count The number of lines to insert.
2228 */
2229hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07002230 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002231
2232 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002233 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002234
Robert Ginda579186b2012-09-26 11:40:04 -07002235 // The moveCount is the number of rows we need to relocate to make room for
2236 // the new row(s). The count is the distance to move them.
2237 var moveCount = bottom - cursorRow - count + 1;
2238 if (moveCount)
2239 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08002240
Robert Ginda579186b2012-09-26 11:40:04 -07002241 for (var i = count - 1; i >= 0; i--) {
2242 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002243 this.screen_.clearCursorRow();
2244 }
rginda8ba33642011-12-14 12:31:31 -08002245};
2246
2247/**
2248 * VT command to delete lines at the current cursor row.
2249 *
2250 * New rows are added to the bottom of scroll region to take their place. New
2251 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002252 *
2253 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002254 */
2255hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08002256 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002257
rginda87b86462011-12-14 13:48:03 -08002258 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08002259 var bottom = this.getVTScrollBottom();
2260
rginda87b86462011-12-14 13:48:03 -08002261 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002262 count = Math.min(count, maxCount);
2263
rginda87b86462011-12-14 13:48:03 -08002264 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08002265 if (count != maxCount)
2266 this.moveRows_(top, count, moveStart);
2267
2268 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002269 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002270 this.screen_.clearCursorRow();
2271 }
2272
rginda87b86462011-12-14 13:48:03 -08002273 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002274 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002275};
2276
2277/**
2278 * Inserts the given number of spaces at the current cursor position.
2279 *
rginda87b86462011-12-14 13:48:03 -08002280 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002281 *
2282 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002283 */
2284hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08002285 var cursor = this.saveCursor();
2286
Mike Frysinger73e56462019-07-17 00:23:46 -05002287 const ws = ' '.repeat(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002288 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002289 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002290
2291 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002292 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002293};
2294
2295/**
2296 * Forward-delete the specified number of characters starting at the cursor
2297 * position.
2298 *
2299 * @param {integer} count The number of characters to delete.
2300 */
2301hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002302 var deleted = this.screen_.deleteChars(count);
2303 if (deleted && !this.screen_.textAttributes.isDefault()) {
2304 var cursor = this.saveCursor();
2305 this.setCursorColumn(this.screenSize.width - deleted);
Mike Frysinger73e56462019-07-17 00:23:46 -05002306 this.screen_.insertString(' '.repeat(deleted));
Robert Ginda7fd57082012-09-25 14:41:47 -07002307 this.restoreCursor(cursor);
2308 }
2309
David Benjamin54e8bf62012-06-01 22:31:40 -04002310 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002311};
2312
2313/**
2314 * Shift rows in the scroll region upwards by a given number of lines.
2315 *
2316 * New rows are inserted at the bottom of the scroll region to fill the
2317 * vacated rows. The new rows not filled out with the current text attributes.
2318 *
2319 * This function does not affect the scrollback rows at all. Rows shifted
2320 * off the top are lost.
2321 *
rginda87b86462011-12-14 13:48:03 -08002322 * The cursor position is not altered.
2323 *
rginda8ba33642011-12-14 12:31:31 -08002324 * @param {integer} count The number of rows to scroll.
2325 */
2326hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002327 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002328
rginda87b86462011-12-14 13:48:03 -08002329 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002330 this.deleteLines(count);
2331
rginda87b86462011-12-14 13:48:03 -08002332 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002333};
2334
2335/**
2336 * Shift rows below the cursor down by a given number of lines.
2337 *
2338 * This function respects the current scroll region.
2339 *
2340 * New rows are inserted at the top of the scroll region to fill the
2341 * vacated rows. The new rows not filled out with the current text attributes.
2342 *
2343 * This function does not affect the scrollback rows at all. Rows shifted
2344 * off the bottom are lost.
2345 *
2346 * @param {integer} count The number of rows to scroll.
2347 */
2348hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08002349 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002350
rginda87b86462011-12-14 13:48:03 -08002351 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08002352 this.insertLines(opt_count);
2353
rginda87b86462011-12-14 13:48:03 -08002354 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002355};
2356
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002357/**
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002358 * Enable accessibility-friendly features that have a performance impact.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002359 *
2360 * This will generate additional DOM nodes in an aria-live region that will
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002361 * cause Assitive Technology to announce the output of the terminal. It also
2362 * enables other features that aid assistive technology. All the features gated
2363 * behind this flag have a performance impact on the terminal which is why they
2364 * are made optional.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002365 *
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002366 * @param {boolean} enabled Whether to enable accessibility-friendly features.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002367 */
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002368hterm.Terminal.prototype.setAccessibilityEnabled = function(enabled) {
Raymes Khoury177aec72018-06-26 10:58:53 +10002369 this.accessibilityReader_.setAccessibilityEnabled(enabled);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002370};
rginda87b86462011-12-14 13:48:03 -08002371
rginda8ba33642011-12-14 12:31:31 -08002372/**
2373 * Set the cursor position.
2374 *
2375 * The cursor row is relative to the scroll region if the terminal has
2376 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2377 *
2378 * @param {integer} row The new zero-based cursor row.
2379 * @param {integer} row The new zero-based cursor column.
2380 */
2381hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2382 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002383 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002384 } else {
rginda87b86462011-12-14 13:48:03 -08002385 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002386 }
rginda87b86462011-12-14 13:48:03 -08002387};
rginda8ba33642011-12-14 12:31:31 -08002388
Evan Jones2600d4f2016-12-06 09:29:36 -05002389/**
2390 * Move the cursor relative to its current position.
2391 *
2392 * @param {number} row
2393 * @param {number} column
2394 */
rginda87b86462011-12-14 13:48:03 -08002395hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2396 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002397 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2398 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002399 this.screen_.setCursorPosition(row, column);
2400};
2401
Evan Jones2600d4f2016-12-06 09:29:36 -05002402/**
2403 * Move the cursor to the specified position.
2404 *
2405 * @param {number} row
2406 * @param {number} column
2407 */
rginda87b86462011-12-14 13:48:03 -08002408hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002409 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2410 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002411 this.screen_.setCursorPosition(row, column);
2412};
2413
2414/**
2415 * Set the cursor column.
2416 *
2417 * @param {integer} column The new zero-based cursor column.
2418 */
2419hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002420 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002421};
2422
2423/**
2424 * Return the cursor column.
2425 *
2426 * @return {integer} The zero-based cursor column.
2427 */
2428hterm.Terminal.prototype.getCursorColumn = function() {
2429 return this.screen_.cursorPosition.column;
2430};
2431
2432/**
2433 * Set the cursor row.
2434 *
2435 * The cursor row is relative to the scroll region if the terminal has
2436 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2437 *
2438 * @param {integer} row The new cursor row.
2439 */
rginda87b86462011-12-14 13:48:03 -08002440hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2441 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002442};
2443
2444/**
2445 * Return the cursor row.
2446 *
2447 * @return {integer} The zero-based cursor row.
2448 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002449hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002450 return this.screen_.cursorPosition.row;
2451};
2452
2453/**
2454 * Request that the ScrollPort redraw itself soon.
2455 *
2456 * The redraw will happen asynchronously, soon after the call stack winds down.
2457 * Multiple calls will be coalesced into a single redraw.
2458 */
2459hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002460 if (this.timeouts_.redraw)
2461 return;
rginda8ba33642011-12-14 12:31:31 -08002462
2463 var self = this;
rginda87b86462011-12-14 13:48:03 -08002464 this.timeouts_.redraw = setTimeout(function() {
2465 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002466 self.scrollPort_.redraw_();
2467 }, 0);
2468};
2469
2470/**
2471 * Request that the ScrollPort be scrolled to the bottom.
2472 *
2473 * The scroll will happen asynchronously, soon after the call stack winds down.
2474 * Multiple calls will be coalesced into a single scroll.
2475 *
2476 * This affects the scrollbar position of the ScrollPort, and has nothing to
2477 * do with the VT scroll commands.
2478 */
2479hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2480 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002481 return;
rginda8ba33642011-12-14 12:31:31 -08002482
2483 var self = this;
2484 this.timeouts_.scrollDown = setTimeout(function() {
2485 delete self.timeouts_.scrollDown;
2486 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2487 }, 10);
2488};
2489
2490/**
2491 * Move the cursor up a specified number of rows.
2492 *
2493 * @param {integer} count The number of rows to move the cursor.
2494 */
2495hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002496 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002497};
2498
2499/**
2500 * Move the cursor down a specified number of rows.
2501 *
2502 * @param {integer} count The number of rows to move the cursor.
2503 */
2504hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002505 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002506 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2507 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2508 this.screenSize.height - 1);
2509
rgindacbbd7482012-06-13 15:06:16 -07002510 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002511 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002512 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002513};
2514
2515/**
2516 * Move the cursor left a specified number of columns.
2517 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002518 * If reverse wraparound mode is enabled and the previous row wrapped into
2519 * the current row then we back up through the wraparound as well.
2520 *
rginda8ba33642011-12-14 12:31:31 -08002521 * @param {integer} count The number of columns to move the cursor.
2522 */
2523hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002524 count = count || 1;
2525
2526 if (count < 1)
2527 return;
2528
2529 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002530 if (this.options_.reverseWraparound) {
2531 if (this.screen_.cursorPosition.overflow) {
2532 // If this cursor is in the right margin, consume one count to get it
2533 // back to the last column. This only applies when we're in reverse
2534 // wraparound mode.
2535 count--;
2536 this.clearCursorOverflow();
2537
2538 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002539 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002540 }
2541
Robert Gindabfb32622014-07-17 13:20:27 -07002542 var newRow = this.screen_.cursorPosition.row;
2543 var newColumn = currentColumn - count;
2544 if (newColumn < 0) {
2545 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2546 if (newRow < 0) {
2547 // xterm also wraps from row 0 to the last row.
2548 newRow = this.screenSize.height + newRow % this.screenSize.height;
2549 }
2550 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2551 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002552
Robert Gindabfb32622014-07-17 13:20:27 -07002553 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2554
2555 } else {
2556 var newColumn = Math.max(currentColumn - count, 0);
2557 this.setCursorColumn(newColumn);
2558 }
rginda8ba33642011-12-14 12:31:31 -08002559};
2560
2561/**
2562 * Move the cursor right a specified number of columns.
2563 *
2564 * @param {integer} count The number of columns to move the cursor.
2565 */
2566hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002567 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002568
2569 if (count < 1)
2570 return;
2571
rgindacbbd7482012-06-13 15:06:16 -07002572 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002573 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002574 this.setCursorColumn(column);
2575};
2576
2577/**
2578 * Reverse the foreground and background colors of the terminal.
2579 *
2580 * This only affects text that was drawn with no attributes.
2581 *
2582 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2583 * been drawn with attributes that happen to coincide with the default
2584 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002585 *
2586 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002587 */
2588hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002589 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002590 if (state) {
Mike Frysinger31cb1562017-07-31 23:44:18 -04002591 this.scrollPort_.setForegroundColor(this.backgroundColor_);
2592 this.scrollPort_.setBackgroundColor(this.foregroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002593 } else {
Mike Frysinger31cb1562017-07-31 23:44:18 -04002594 this.scrollPort_.setForegroundColor(this.foregroundColor_);
2595 this.scrollPort_.setBackgroundColor(this.backgroundColor_);
rginda8ba33642011-12-14 12:31:31 -08002596 }
2597};
2598
2599/**
rginda87b86462011-12-14 13:48:03 -08002600 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002601 *
2602 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002603 */
2604hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002605 this.cursorNode_.style.backgroundColor =
2606 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002607
2608 var self = this;
2609 setTimeout(function() {
Matheus Fernandes2d733082017-09-11 06:43:01 -04002610 self.restyleCursor_();
rginda6d397402012-01-17 10:58:29 -08002611 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002612
Michael Kelly485ecd12014-06-09 11:41:56 -04002613 // bellSquelchTimeout_ affects both audio and notification bells.
2614 if (this.bellSquelchTimeout_)
2615 return;
2616
Robert Ginda92e18102013-03-14 13:56:37 -07002617 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002618 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002619 this.bellSequelchTimeout_ = setTimeout(function() {
2620 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002621 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002622 } else {
2623 delete this.bellSquelchTimeout_;
2624 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002625
2626 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingera5fb83c2017-06-22 14:48:35 -07002627 var n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002628 this.bellNotificationList_.push(n);
2629 // TODO: Should we try to raise the window here?
2630 n.onclick = function() { self.closeBellNotifications_(); };
2631 }
rginda87b86462011-12-14 13:48:03 -08002632};
2633
2634/**
rginda8ba33642011-12-14 12:31:31 -08002635 * Set the origin mode bit.
2636 *
2637 * If origin mode is on, certain VT cursor and scrolling commands measure their
2638 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2639 * to the top of the addressable screen.
2640 *
2641 * Defaults to off.
2642 *
2643 * @param {boolean} state True to set origin mode, false to unset.
2644 */
2645hterm.Terminal.prototype.setOriginMode = function(state) {
2646 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002647 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002648};
2649
2650/**
2651 * Set the insert mode bit.
2652 *
2653 * If insert mode is on, existing text beyond the cursor position will be
2654 * shifted right to make room for new text. Otherwise, new text overwrites
2655 * any existing text.
2656 *
2657 * Defaults to off.
2658 *
2659 * @param {boolean} state True to set insert mode, false to unset.
2660 */
2661hterm.Terminal.prototype.setInsertMode = function(state) {
2662 this.options_.insertMode = state;
2663};
2664
2665/**
rginda87b86462011-12-14 13:48:03 -08002666 * Set the auto carriage return bit.
2667 *
2668 * If auto carriage return is on then a formfeed character is interpreted
2669 * as a newline, otherwise it's the same as a linefeed. The difference boils
2670 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002671 *
2672 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002673 */
2674hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2675 this.options_.autoCarriageReturn = state;
2676};
2677
2678/**
rginda8ba33642011-12-14 12:31:31 -08002679 * Set the wraparound mode bit.
2680 *
2681 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2682 * to the start of the following row. Otherwise, the cursor is clamped to the
2683 * end of the screen and attempts to write past it are ignored.
2684 *
2685 * Defaults to on.
2686 *
2687 * @param {boolean} state True to set wraparound mode, false to unset.
2688 */
2689hterm.Terminal.prototype.setWraparound = function(state) {
2690 this.options_.wraparound = state;
2691};
2692
2693/**
2694 * Set the reverse-wraparound mode bit.
2695 *
2696 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2697 * to the end of the previous row. Otherwise, the cursor is clamped to column
2698 * 0.
2699 *
2700 * Defaults to off.
2701 *
2702 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2703 */
2704hterm.Terminal.prototype.setReverseWraparound = function(state) {
2705 this.options_.reverseWraparound = state;
2706};
2707
2708/**
2709 * Selects between the primary and alternate screens.
2710 *
2711 * If alternate mode is on, the alternate screen is active. Otherwise the
2712 * primary screen is active.
2713 *
2714 * Swapping screens has no effect on the scrollback buffer.
2715 *
2716 * Each screen maintains its own cursor position.
2717 *
2718 * Defaults to off.
2719 *
2720 * @param {boolean} state True to set alternate mode, false to unset.
2721 */
2722hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002723 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002724 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2725
rginda35c456b2012-02-09 17:29:05 -08002726 if (this.screen_.rowsArray.length &&
2727 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2728 // If the screen changed sizes while we were away, our rowIndexes may
2729 // be incorrect.
2730 var offset = this.scrollbackRows_.length;
2731 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002732 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002733 ary[i].rowIndex = offset + i;
2734 }
2735 }
rginda8ba33642011-12-14 12:31:31 -08002736
rginda35c456b2012-02-09 17:29:05 -08002737 this.realizeWidth_(this.screenSize.width);
2738 this.realizeHeight_(this.screenSize.height);
2739 this.scrollPort_.syncScrollHeight();
2740 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002741
rginda6d397402012-01-17 10:58:29 -08002742 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002743 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002744};
2745
2746/**
2747 * Set the cursor-blink mode bit.
2748 *
2749 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2750 * a visible cursor does not blink.
2751 *
2752 * You should make sure to turn blinking off if you're going to dispose of a
2753 * terminal, otherwise you'll leak a timeout.
2754 *
2755 * Defaults to on.
2756 *
2757 * @param {boolean} state True to set cursor-blink mode, false to unset.
2758 */
2759hterm.Terminal.prototype.setCursorBlink = function(state) {
2760 this.options_.cursorBlink = state;
2761
2762 if (!state && this.timeouts_.cursorBlink) {
2763 clearTimeout(this.timeouts_.cursorBlink);
2764 delete this.timeouts_.cursorBlink;
2765 }
2766
2767 if (this.options_.cursorVisible)
2768 this.setCursorVisible(true);
2769};
2770
2771/**
2772 * Set the cursor-visible mode bit.
2773 *
2774 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2775 *
2776 * Defaults to on.
2777 *
2778 * @param {boolean} state True to set cursor-visible mode, false to unset.
2779 */
2780hterm.Terminal.prototype.setCursorVisible = function(state) {
2781 this.options_.cursorVisible = state;
2782
2783 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002784 if (this.timeouts_.cursorBlink) {
2785 clearTimeout(this.timeouts_.cursorBlink);
2786 delete this.timeouts_.cursorBlink;
2787 }
rginda87b86462011-12-14 13:48:03 -08002788 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002789 return;
2790 }
2791
rginda87b86462011-12-14 13:48:03 -08002792 this.syncCursorPosition_();
2793
2794 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002795
2796 if (this.options_.cursorBlink) {
2797 if (this.timeouts_.cursorBlink)
2798 return;
2799
Robert Gindaea2183e2014-07-17 09:51:51 -07002800 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002801 } else {
2802 if (this.timeouts_.cursorBlink) {
2803 clearTimeout(this.timeouts_.cursorBlink);
2804 delete this.timeouts_.cursorBlink;
2805 }
2806 }
2807};
2808
2809/**
rginda87b86462011-12-14 13:48:03 -08002810 * Synchronizes the visible cursor and document selection with the current
2811 * cursor coordinates.
Raymes Khourye5d48982018-08-02 09:08:32 +10002812 *
2813 * @return {boolean} True if the cursor is onscreen and synced.
rginda8ba33642011-12-14 12:31:31 -08002814 */
2815hterm.Terminal.prototype.syncCursorPosition_ = function() {
2816 var topRowIndex = this.scrollPort_.getTopRowIndex();
2817 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2818 var cursorRowIndex = this.scrollbackRows_.length +
2819 this.screen_.cursorPosition.row;
2820
Raymes Khoury15697f42018-07-17 11:37:18 +10002821 let forceSyncSelection = false;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002822 if (this.accessibilityReader_.accessibilityEnabled) {
2823 // Report the new position of the cursor for accessibility purposes.
2824 const cursorColumnIndex = this.screen_.cursorPosition.column;
2825 const cursorLineText =
2826 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
Raymes Khoury15697f42018-07-17 11:37:18 +10002827 // This will force the selection to be sync'd to the cursor position if the
2828 // user has pressed a key. Generally we would only sync the cursor position
2829 // when selection is collapsed so that if the user has selected something
2830 // we don't clear the selection by moving the selection. However when a
2831 // screen reader is used, it's intuitive for entering a key to move the
2832 // selection to the cursor.
2833 forceSyncSelection = this.accessibilityReader_.hasUserGesture;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002834 this.accessibilityReader_.afterCursorChange(
2835 cursorLineText, cursorRowIndex, cursorColumnIndex);
2836 }
2837
rginda8ba33642011-12-14 12:31:31 -08002838 if (cursorRowIndex > bottomRowIndex) {
2839 // Cursor is scrolled off screen, move it outside of the visible area.
Mike Frysinger44c32202017-08-05 01:13:09 -04002840 this.setCssVar('cursor-offset-row', '-1');
Raymes Khourye5d48982018-08-02 09:08:32 +10002841 return false;
rginda8ba33642011-12-14 12:31:31 -08002842 }
2843
Robert Gindab837c052014-08-11 11:17:51 -07002844 if (this.options_.cursorVisible &&
2845 this.cursorNode_.style.display == 'none') {
2846 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2847 this.cursorNode_.style.display = '';
2848 }
2849
Mike Frysinger44c32202017-08-05 01:13:09 -04002850 // Position the cursor using CSS variable math. If we do the math in JS,
2851 // the float math will end up being more precise than the CSS which will
2852 // cause the cursor tracking to be off.
2853 this.setCssVar(
2854 'cursor-offset-row',
2855 `${cursorRowIndex - topRowIndex} + ` +
2856 `${this.scrollPort_.visibleRowTopMargin}px`);
2857 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08002858
2859 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04002860 '(' + this.screen_.cursorPosition.column +
2861 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08002862 ')');
2863
2864 // Update the caret for a11y purposes.
2865 var selection = this.document_.getSelection();
Raymes Khoury15697f42018-07-17 11:37:18 +10002866 if (selection && (selection.isCollapsed || forceSyncSelection)) {
rginda87b86462011-12-14 13:48:03 -08002867 this.screen_.syncSelectionCaret(selection);
Raymes Khoury15697f42018-07-17 11:37:18 +10002868 }
Raymes Khourye5d48982018-08-02 09:08:32 +10002869 return true;
rginda8ba33642011-12-14 12:31:31 -08002870};
2871
Robert Gindafb1be6a2013-12-11 11:56:22 -08002872/**
2873 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2874 * and character cell dimensions.
2875 */
Robert Ginda830583c2013-08-07 13:20:46 -07002876hterm.Terminal.prototype.restyleCursor_ = function() {
2877 var shape = this.cursorShape_;
2878
2879 if (this.cursorNode_.getAttribute('focus') == 'false') {
2880 // Always show a block cursor when unfocused.
2881 shape = hterm.Terminal.cursorShape.BLOCK;
2882 }
2883
2884 var style = this.cursorNode_.style;
2885
2886 switch (shape) {
2887 case hterm.Terminal.cursorShape.BEAM:
Robert Ginda830583c2013-08-07 13:20:46 -07002888 style.backgroundColor = 'transparent';
2889 style.borderBottomStyle = null;
2890 style.borderLeftStyle = 'solid';
2891 break;
2892
2893 case hterm.Terminal.cursorShape.UNDERLINE:
Robert Ginda830583c2013-08-07 13:20:46 -07002894 style.backgroundColor = 'transparent';
2895 style.borderBottomStyle = 'solid';
Robert Ginda830583c2013-08-07 13:20:46 -07002896 style.borderLeftStyle = null;
2897 break;
2898
2899 default:
Mike Frysinger2fd079a2018-09-02 01:46:12 -04002900 style.backgroundColor = 'var(--hterm-cursor-color)';
Robert Ginda830583c2013-08-07 13:20:46 -07002901 style.borderBottomStyle = null;
2902 style.borderLeftStyle = null;
2903 break;
2904 }
2905};
2906
rginda8ba33642011-12-14 12:31:31 -08002907/**
2908 * Synchronizes the visible cursor with the current cursor coordinates.
2909 *
2910 * The sync will happen asynchronously, soon after the call stack winds down.
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002911 * Multiple calls will be coalesced into a single sync. This should be called
2912 * prior to the cursor actually changing position.
rginda8ba33642011-12-14 12:31:31 -08002913 */
2914hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2915 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002916 return;
rginda8ba33642011-12-14 12:31:31 -08002917
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002918 if (this.accessibilityReader_.accessibilityEnabled) {
2919 // Report the previous position of the cursor for accessibility purposes.
2920 const cursorRowIndex = this.scrollbackRows_.length +
2921 this.screen_.cursorPosition.row;
2922 const cursorColumnIndex = this.screen_.cursorPosition.column;
2923 const cursorLineText =
2924 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
2925 this.accessibilityReader_.beforeCursorChange(
2926 cursorLineText, cursorRowIndex, cursorColumnIndex);
2927 }
2928
rginda8ba33642011-12-14 12:31:31 -08002929 var self = this;
2930 this.timeouts_.syncCursor = setTimeout(function() {
2931 self.syncCursorPosition_();
2932 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002933 }, 0);
2934};
2935
rgindacc2996c2012-02-24 14:59:31 -08002936/**
rgindaf522ce02012-04-17 17:49:17 -07002937 * Show or hide the zoom warning.
2938 *
2939 * The zoom warning is a message warning the user that their browser zoom must
2940 * be set to 100% in order for hterm to function properly.
2941 *
2942 * @param {boolean} state True to show the message, false to hide it.
2943 */
2944hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2945 if (!this.zoomWarningNode_) {
2946 if (!state)
2947 return;
2948
2949 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002950 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07002951 this.zoomWarningNode_.style.cssText = (
2952 'color: black;' +
2953 'background-color: #ff2222;' +
2954 'font-size: large;' +
2955 'border-radius: 8px;' +
2956 'opacity: 0.75;' +
2957 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2958 'top: 0.5em;' +
2959 'right: 1.2em;' +
2960 'position: absolute;' +
2961 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002962 '-webkit-user-select: none;' +
2963 '-moz-text-size-adjust: none;' +
2964 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05002965
2966 this.zoomWarningNode_.addEventListener('click', function(e) {
2967 this.parentNode.removeChild(this);
2968 });
rgindaf522ce02012-04-17 17:49:17 -07002969 }
2970
Mike Frysingerb7289952019-03-23 16:05:38 -07002971 this.zoomWarningNode_.textContent = lib.i18n.replaceReferences(
Robert Gindab4839c22013-02-28 16:52:10 -08002972 hterm.zoomWarningMessage,
2973 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2974
rgindaf522ce02012-04-17 17:49:17 -07002975 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2976
2977 if (state) {
2978 if (!this.zoomWarningNode_.parentNode)
2979 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2980 } else if (this.zoomWarningNode_.parentNode) {
2981 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2982 }
2983};
2984
2985/**
rgindacc2996c2012-02-24 14:59:31 -08002986 * Show the terminal overlay for a given amount of time.
2987 *
2988 * The terminal overlay appears in inverse video in a large font, centered
2989 * over the terminal. You should probably keep the overlay message brief,
2990 * since it's in a large font and you probably aren't going to check the size
2991 * of the terminal first.
2992 *
2993 * @param {string} msg The text (not HTML) message to display in the overlay.
2994 * @param {number} opt_timeout The amount of time to wait before fading out
2995 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2996 * stay up forever (or until the next overlay).
2997 */
2998hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002999 if (!this.overlayNode_) {
3000 if (!this.div_)
3001 return;
3002
3003 this.overlayNode_ = this.document_.createElement('div');
3004 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08003005 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08003006 'font-size: xx-large;' +
3007 'opacity: 0.75;' +
3008 'padding: 0.2em 0.5em 0.2em 0.5em;' +
3009 'position: absolute;' +
3010 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003011 '-webkit-transition: opacity 180ms ease-in;' +
3012 '-moz-user-select: none;' +
3013 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08003014
3015 this.overlayNode_.addEventListener('mousedown', function(e) {
3016 e.preventDefault();
3017 e.stopPropagation();
3018 }, true);
rgindaf0090c92012-02-10 14:58:52 -08003019 }
3020
rginda9f5222b2012-03-05 11:53:28 -08003021 this.overlayNode_.style.color = this.prefs_.get('background-color');
3022 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
3023 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
3024
rgindaf0090c92012-02-10 14:58:52 -08003025 this.overlayNode_.textContent = msg;
3026 this.overlayNode_.style.opacity = '0.75';
3027
3028 if (!this.overlayNode_.parentNode)
3029 this.div_.appendChild(this.overlayNode_);
3030
Robert Ginda97769282013-02-01 15:30:30 -08003031 var divSize = hterm.getClientSize(this.div_);
3032 var overlaySize = hterm.getClientSize(this.overlayNode_);
3033
Robert Ginda8a59f762014-07-23 11:29:55 -07003034 this.overlayNode_.style.top =
3035 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08003036 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07003037 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08003038
rgindaf0090c92012-02-10 14:58:52 -08003039 if (this.overlayTimeout_)
3040 clearTimeout(this.overlayTimeout_);
3041
Raymes Khouryc7a06382018-07-04 10:25:45 +10003042 this.accessibilityReader_.assertiveAnnounce(msg);
3043
rgindacc2996c2012-02-24 14:59:31 -08003044 if (opt_timeout === null)
3045 return;
3046
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003047 this.overlayTimeout_ = setTimeout(() => {
3048 this.overlayNode_.style.opacity = '0';
3049 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
3050 }, opt_timeout || 1500);
3051};
3052
3053/**
3054 * Hide the terminal overlay immediately.
3055 *
3056 * Useful when we show an overlay for an event with an unknown end time.
3057 */
3058hterm.Terminal.prototype.hideOverlay = function() {
3059 if (this.overlayTimeout_)
3060 clearTimeout(this.overlayTimeout_);
3061 this.overlayTimeout_ = null;
3062
3063 if (this.overlayNode_.parentNode)
3064 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
3065 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08003066};
3067
rginda4bba5e12012-06-20 16:15:30 -07003068/**
3069 * Paste from the system clipboard to the terminal.
3070 */
3071hterm.Terminal.prototype.paste = function() {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003072 return hterm.pasteFromClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07003073};
3074
3075/**
3076 * Copy a string to the system clipboard.
3077 *
3078 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05003079 *
3080 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07003081 */
3082hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08003083 if (this.prefs_.get('enable-clipboard-notice'))
3084 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
3085
Mike Frysinger96eacae2019-01-02 18:13:56 -05003086 hterm.copySelectionToClipboard(this.document_, str);
rginda4bba5e12012-06-20 16:15:30 -07003087};
3088
Evan Jones2600d4f2016-12-06 09:29:36 -05003089/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003090 * Display an image.
3091 *
Mike Frysinger2558ed52019-01-14 01:03:41 -05003092 * Either URI or buffer or blob fields must be specified.
3093 *
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003094 * @param {Object} options The image to display.
3095 * @param {string=} options.name A human readable string for the image.
3096 * @param {string|number=} options.size The size (in bytes).
3097 * @param {boolean=} options.preserveAspectRatio Whether to preserve aspect.
3098 * @param {boolean=} options.inline Whether to display the image inline.
3099 * @param {string|number=} options.width The width of the image.
3100 * @param {string|number=} options.height The height of the image.
3101 * @param {string=} options.align Direction to align the image.
Mike Frysinger2558ed52019-01-14 01:03:41 -05003102 * @param {string=} options.uri The source URI for the image.
3103 * @param {ArrayBuffer=} options.buffer The ArrayBuffer image data.
3104 * @param {Blob=} options.blob The Blob image data.
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003105 * @param {string=} options.type The MIME type of the image data.
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003106 * @param {function=} onLoad Callback when loading finishes.
3107 * @param {function(Event)=} onError Callback when loading fails.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003108 */
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003109hterm.Terminal.prototype.displayImage = function(options, onLoad, onError) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003110 // Make sure we're actually given a resource to display.
Mike Frysinger2558ed52019-01-14 01:03:41 -05003111 if (options.uri === undefined && options.buffer === undefined &&
3112 options.blob === undefined)
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003113 return;
3114
3115 // Set up the defaults to simplify code below.
3116 if (!options.name)
3117 options.name = '';
3118
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003119 // See if the mime type is available. If not, guess from the filename.
3120 // We don't list all possible mime types because the browser can usually
3121 // guess it correctly. So list the ones that need a bit more help.
3122 if (!options.type) {
3123 const ary = options.name.split('.');
3124 const ext = ary[ary.length - 1].trim();
3125 switch (ext) {
3126 case 'svg':
3127 case 'svgz':
3128 options.type = 'image/svg+xml';
3129 break;
3130 }
3131 }
3132
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003133 // Has the user approved image display yet?
3134 if (this.allowImagesInline !== true) {
3135 this.newLine();
3136 const row = this.getRowNode(this.scrollbackRows_.length +
3137 this.getCursorRow() - 1);
3138
3139 if (this.allowImagesInline === false) {
3140 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
3141 'Inline Images Disabled');
3142 return;
3143 }
3144
3145 // Show a prompt.
3146 let button;
3147 const span = this.document_.createElement('span');
3148 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
3149 span.style.fontWeight = 'bold';
3150 span.style.borderWidth = '1px';
3151 span.style.borderStyle = 'dashed';
3152 button = this.document_.createElement('span');
3153 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3154 button.style.marginLeft = '1em';
3155 button.style.borderWidth = '1px';
3156 button.style.borderStyle = 'solid';
3157 button.addEventListener('click', () => {
3158 this.prefs_.set('allow-images-inline', false);
3159 });
3160 span.appendChild(button);
3161 button = this.document_.createElement('span');
3162 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3163 'allow this session');
3164 button.style.marginLeft = '1em';
3165 button.style.borderWidth = '1px';
3166 button.style.borderStyle = 'solid';
3167 button.addEventListener('click', () => {
3168 this.allowImagesInline = true;
3169 });
3170 span.appendChild(button);
3171 button = this.document_.createElement('span');
3172 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3173 button.style.marginLeft = '1em';
3174 button.style.borderWidth = '1px';
3175 button.style.borderStyle = 'solid';
3176 button.addEventListener('click', () => {
3177 this.prefs_.set('allow-images-inline', true);
3178 });
3179 span.appendChild(button);
3180
3181 row.appendChild(span);
3182 return;
3183 }
3184
3185 // See if we should show this object directly, or download it.
3186 if (options.inline) {
3187 const io = this.io.push();
3188 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
3189 'Loading $1 ...'), null);
3190
3191 // While we're loading the image, eat all the user's input.
3192 io.onVTKeystroke = io.sendString = () => {};
3193
3194 // Initialize this new image.
Adrián Pérez-Orozco6a550322018-08-31 14:36:06 -07003195 const img =
3196 /** @type {!HTMLImageElement} */ (this.document_.createElement('img'));
Mike Frysinger2558ed52019-01-14 01:03:41 -05003197 if (options.uri !== undefined) {
3198 img.src = options.uri;
3199 } else if (options.buffer !== undefined) {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003200 const blob = new Blob([options.buffer], {type: options.type});
Mike Frysinger2558ed52019-01-14 01:03:41 -05003201 img.src = URL.createObjectURL(blob);
3202 } else {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003203 const blob = new Blob([options.blob], {type: options.type});
Mike Frysinger2558ed52019-01-14 01:03:41 -05003204 img.src = URL.createObjectURL(options.blob);
3205 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003206 img.title = img.alt = options.name;
3207
3208 // Attach the image to the page to let it load/render. It won't stay here.
3209 // This is needed so it's visible and the DOM can calculate the height. If
3210 // the image is hidden or not in the DOM, the height is always 0.
3211 this.document_.body.appendChild(img);
3212
3213 // Wait for the image to finish loading before we try moving it to the
3214 // right place in the terminal.
3215 img.onload = () => {
3216 // Now that we have the image dimensions, figure out how to show it.
3217 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
3218 img.style.maxWidth = `${this.document_.body.clientWidth}px`;
3219 img.style.maxHeight = `${this.document_.body.clientHeight}px`;
3220
3221 // Parse a width/height specification.
3222 const parseDim = (dim, maxDim, cssVar) => {
3223 if (!dim || dim == 'auto')
3224 return '';
3225
3226 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3227 if (ary) {
3228 if (ary[2] == '%')
3229 return maxDim * parseInt(ary[1]) / 100 + 'px';
3230 else if (ary[2] == 'px')
3231 return dim;
3232 else
3233 return `calc(${dim} * var(${cssVar}))`;
3234 }
3235
3236 return '';
3237 };
3238 img.style.width =
3239 parseDim(options.width, this.document_.body.clientWidth,
3240 '--hterm-charsize-width');
3241 img.style.height =
3242 parseDim(options.height, this.document_.body.clientHeight,
3243 '--hterm-charsize-height');
3244
3245 // Figure out how many rows the image occupies, then add that many.
3246 // XXX: This count will be inaccurate if the font size changes on us.
3247 const padRows = Math.ceil(img.clientHeight /
3248 this.scrollPort_.characterSize.height);
3249 for (let i = 0; i < padRows; ++i)
3250 this.newLine();
3251
3252 // Update the max height in case the user shrinks the character size.
3253 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3254
3255 // Move the image to the last row. This way when we scroll up, it doesn't
3256 // disappear when the first row gets clipped. It will disappear when we
3257 // scroll down and the last row is clipped ...
3258 this.document_.body.removeChild(img);
3259 // Create a wrapper node so we can do an absolute in a relative position.
3260 // This helps with rounding errors between JS & CSS counts.
3261 const div = this.document_.createElement('div');
3262 div.style.position = 'relative';
3263 div.style.textAlign = options.align;
3264 img.style.position = 'absolute';
3265 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3266 div.appendChild(img);
3267 const row = this.getRowNode(this.scrollbackRows_.length +
3268 this.getCursorRow() - 1);
3269 row.appendChild(div);
3270
Mike Frysinger2558ed52019-01-14 01:03:41 -05003271 // Now that the image has been read, we can revoke the source.
3272 if (options.uri === undefined) {
3273 URL.revokeObjectURL(img.src);
3274 }
3275
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003276 io.hideOverlay();
3277 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003278
3279 if (onLoad)
3280 onLoad();
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003281 };
3282
3283 // If we got a malformed image, give up.
3284 img.onerror = (e) => {
3285 this.document_.body.removeChild(img);
3286 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
Mike Frysingere14a8c42018-03-10 00:17:30 -08003287 'Loading $1 failed'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003288 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003289
3290 if (onError)
3291 onError(e);
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003292 };
3293 } else {
3294 // We can't use chrome.downloads.download as that requires "downloads"
3295 // permissions, and that works only in extensions, not apps.
3296 const a = this.document_.createElement('a');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003297 if (options.uri !== undefined) {
3298 a.href = options.uri;
3299 } else if (options.buffer !== undefined) {
3300 const blob = new Blob([options.buffer]);
3301 a.href = URL.createObjectURL(blob);
3302 } else {
3303 a.href = URL.createObjectURL(options.blob);
3304 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003305 a.download = options.name;
3306 this.document_.body.appendChild(a);
3307 a.click();
3308 a.remove();
Mike Frysinger2558ed52019-01-14 01:03:41 -05003309 if (options.uri === undefined) {
3310 URL.revokeObjectURL(a.href);
3311 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003312 }
3313};
3314
3315/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003316 * Returns the selected text, or null if no text is selected.
3317 *
3318 * @return {string|null}
3319 */
rgindaa09e7332012-08-17 12:49:51 -07003320hterm.Terminal.prototype.getSelectionText = function() {
3321 var selection = this.scrollPort_.selection;
3322 selection.sync();
3323
3324 if (selection.isCollapsed)
3325 return null;
3326
rgindaa09e7332012-08-17 12:49:51 -07003327 // Start offset measures from the beginning of the line.
3328 var startOffset = selection.startOffset;
3329 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003330
Raymes Khoury334625a2018-06-25 10:29:40 +10003331 // If an x-row isn't selected, |node| will be null.
3332 if (!node)
3333 return null;
3334
Robert Gindafdbb3f22012-09-06 20:23:06 -07003335 if (node.nodeName != 'X-ROW') {
3336 // If the selection doesn't start on an x-row node, then it must be
3337 // somewhere inside the x-row. Add any characters from previous siblings
3338 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003339
3340 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3341 // If node is the text node in a styled span, move up to the span node.
3342 node = node.parentNode;
3343 }
3344
Robert Gindafdbb3f22012-09-06 20:23:06 -07003345 while (node.previousSibling) {
3346 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003347 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003348 }
rgindaa09e7332012-08-17 12:49:51 -07003349 }
3350
3351 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08003352 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
3353 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003354 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003355
Robert Gindafdbb3f22012-09-06 20:23:06 -07003356 if (node.nodeName != 'X-ROW') {
3357 // If the selection doesn't end on an x-row node, then it must be
3358 // somewhere inside the x-row. Add any characters from following siblings
3359 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003360
3361 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3362 // If node is the text node in a styled span, move up to the span node.
3363 node = node.parentNode;
3364 }
3365
Robert Gindafdbb3f22012-09-06 20:23:06 -07003366 while (node.nextSibling) {
3367 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003368 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003369 }
rgindaa09e7332012-08-17 12:49:51 -07003370 }
3371
3372 var rv = this.getRowsText(selection.startRow.rowIndex,
3373 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003374 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003375};
3376
rginda4bba5e12012-06-20 16:15:30 -07003377/**
3378 * Copy the current selection to the system clipboard, then clear it after a
3379 * short delay.
3380 */
3381hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07003382 var text = this.getSelectionText();
3383 if (text != null)
3384 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07003385};
3386
rgindaf0090c92012-02-10 14:58:52 -08003387hterm.Terminal.prototype.overlaySize = function() {
3388 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
3389};
3390
rginda87b86462011-12-14 13:48:03 -08003391/**
3392 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3393 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003394 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003395 */
3396hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08003397 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08003398 this.scrollPort_.scrollRowToBottom(this.getRowCount());
3399
Mike Frysinger79669762018-12-30 20:51:10 -05003400 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08003401};
3402
3403/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003404 * Open the selected url.
3405 */
3406hterm.Terminal.prototype.openSelectedUrl_ = function() {
3407 var str = this.getSelectionText();
3408
3409 // If there is no selection, try and expand wherever they clicked.
3410 if (str == null) {
John Lincae9b732018-03-08 13:56:35 +08003411 this.screen_.expandSelectionForUrl(this.document_.getSelection());
Mike Frysinger70b94692017-01-26 18:57:50 -10003412 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003413
3414 // If clicking in empty space, return.
3415 if (str == null)
3416 return;
Mike Frysinger70b94692017-01-26 18:57:50 -10003417 }
3418
3419 // Make sure URL is valid before opening.
3420 if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0)
3421 return;
Mike Frysinger43472622017-06-26 18:11:07 -04003422
3423 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003424 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003425 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3426 // We have to whitelist a few protocols that lack authorities and thus
3427 // never use the //. Like mailto.
3428 switch (str.split(':', 1)[0]) {
3429 case 'mailto':
3430 break;
3431 default:
3432 str = 'http://' + str;
3433 break;
3434 }
3435 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003436
Mike Frysinger720fa832017-10-23 01:15:52 -04003437 hterm.openUrl(str);
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003438};
Mike Frysinger70b94692017-01-26 18:57:50 -10003439
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003440/**
3441 * Manage the automatic mouse hiding behavior while typing.
3442 *
3443 * @param {boolean=} v Whether to enable automatic hiding.
3444 */
3445hterm.Terminal.prototype.setAutomaticMouseHiding = function(v=null) {
3446 // Since Chrome OS & macOS do this by default everywhere, we don't need to.
3447 // Linux & Windows seem to leave this to specific applications to manage.
3448 if (v === null)
3449 v = (hterm.os != 'cros' && hterm.os != 'mac');
3450
3451 this.mouseHideWhileTyping_ = !!v;
3452};
3453
3454/**
3455 * Handler for monitoring user keyboard activity.
3456 *
3457 * This isn't for processing the keystrokes directly, but for updating any
3458 * state that might toggle based on the user using the keyboard at all.
3459 *
3460 * @param {KeyboardEvent} e The keyboard event that triggered us.
3461 */
3462hterm.Terminal.prototype.onKeyboardActivity_ = function(e) {
3463 // When the user starts typing, hide the mouse cursor.
3464 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_)
3465 this.setCssVar('mouse-cursor-style', 'none');
3466};
Mike Frysinger70b94692017-01-26 18:57:50 -10003467
3468/**
rgindad5613292012-06-19 15:40:37 -07003469 * Add the terminalRow and terminalColumn properties to mouse events and
3470 * then forward on to onMouse().
3471 *
3472 * The terminalRow and terminalColumn properties contain the (row, column)
3473 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003474 *
3475 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003476 */
3477hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003478 if (e.processedByTerminalHandler_) {
3479 // We register our event handlers on the document, as well as the cursor
3480 // and the scroll blocker. Mouse events that occur on the cursor or
3481 // scroll blocker will also appear on the document, but we don't want to
3482 // process them twice.
3483 //
3484 // We can't just prevent bubbling because that has other side effects, so
3485 // we decorate the event object with this property instead.
3486 return;
3487 }
3488
Mike Frysinger468966c2018-08-28 13:48:51 -04003489 // Consume navigation events. Button 3 is usually "browser back" and
3490 // button 4 is "browser forward" which we don't want to happen.
3491 if (e.button > 2) {
3492 e.preventDefault();
3493 // We don't return so click events can be passed to the remote below.
3494 }
3495
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003496 var reportMouseEvents = (!this.defeatMouseReports_ &&
3497 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3498
rgindafaa74742012-08-21 13:34:03 -07003499 e.processedByTerminalHandler_ = true;
3500
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003501 // Handle auto hiding of mouse cursor while typing.
3502 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
3503 // Make sure the mouse cursor is visible.
3504 this.syncMouseStyle();
3505 // This debounce isn't perfect, but should work well enough for such a
3506 // simple implementation. If the user moved the mouse, we enabled this
3507 // debounce, and then moved the mouse just before the timeout, we wouldn't
3508 // debounce that later movement.
3509 this.mouseHideDelay_ = setTimeout(() => this.mouseHideDelay_ = null, 1000);
3510 }
3511
Robert Gindaeda48db2014-07-17 09:25:30 -07003512 // One based row/column stored on the mouse event.
3513 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
3514 this.scrollPort_.characterSize.height) + 1;
3515 e.terminalColumn = parseInt(e.clientX /
3516 this.scrollPort_.characterSize.width) + 1;
3517
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003518 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
3519 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07003520 return;
3521 }
3522
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003523 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07003524 // If the cursor is visible and we're not sending mouse events to the
3525 // host app, then we want to hide the terminal cursor when the mouse
3526 // cursor is over top. This keeps the terminal cursor from interfering
3527 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003528 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3529 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3530 this.cursorNode_.style.display = 'none';
3531 } else if (this.cursorNode_.style.display == 'none') {
3532 this.cursorNode_.style.display = '';
3533 }
3534 }
rgindad5613292012-06-19 15:40:37 -07003535
Robert Ginda928cf632014-03-05 15:07:41 -08003536 if (e.type == 'mousedown') {
Mike Frysingercc114512017-09-11 21:39:17 -04003537 this.contextMenu.hide(e);
3538
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003539 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003540 // If VT mouse reporting is disabled, or has been defeated with
3541 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003542 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003543 this.setSelectionEnabled(true);
3544 } else {
3545 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003546 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003547 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003548 this.setSelectionEnabled(false);
3549 e.preventDefault();
3550 }
3551 }
3552
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003553 if (!reportMouseEvents) {
John Lin2aad22e2018-03-16 13:58:11 +08003554 if (e.type == 'dblclick') {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003555 this.screen_.expandSelection(this.document_.getSelection());
John Lin2aad22e2018-03-16 13:58:11 +08003556 if (this.copyOnSelect)
Mike Frysinger406c76c2019-01-02 17:53:51 -05003557 this.copySelectionToClipboard();
rgindad5613292012-06-19 15:40:37 -07003558 }
3559
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003560 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003561 // Debounce this event with the dblclick event. If you try to doubleclick
3562 // a URL to open it, Chrome will fire click then dblclick, but we won't
3563 // have expanded the selection text at the first click event.
3564 clearTimeout(this.timeouts_.openUrl);
3565 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3566 500);
3567 return;
3568 }
3569
Mike Frysinger847577f2017-05-23 23:25:57 -04003570 if (e.type == 'mousedown') {
Mike Frysingercc114512017-09-11 21:39:17 -04003571 if (e.ctrlKey && e.button == 2 /* right button */) {
3572 e.preventDefault();
3573 this.contextMenu.show(e, this);
3574 } else if (e.button == this.mousePasteButton ||
3575 (this.mouseRightClickPaste && e.button == 2 /* right button */)) {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003576 if (!this.paste())
Mike Frysinger05a57f02017-08-27 17:48:55 -04003577 console.warn('Could not paste manually due to web restrictions');
Mike Frysinger847577f2017-05-23 23:25:57 -04003578 }
3579 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003580
Mike Frysinger2edd3612017-05-24 00:54:39 -04003581 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003582 !this.document_.getSelection().isCollapsed) {
Mike Frysinger406c76c2019-01-02 17:53:51 -05003583 this.copySelectionToClipboard();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003584 }
3585
3586 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3587 this.scrollBlockerNode_.engaged) {
3588 // Disengage the scroll-blocker after one of these events.
3589 this.scrollBlockerNode_.engaged = false;
3590 this.scrollBlockerNode_.style.top = '-99px';
3591 }
3592
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003593 // Emulate arrow key presses via scroll wheel events.
3594 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3595 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003596 if (e.type == 'wheel') {
Mike Frysinger321063c2018-08-29 15:33:14 -04003597 const delta = this.scrollPort_.scrollWheelDelta(e);
Mike Frysingerc3030a82017-05-29 14:16:11 -04003598
Mike Frysinger321063c2018-08-29 15:33:14 -04003599 // Helper to turn a wheel event delta into a series of key presses.
3600 const deltaToArrows = (distance, charSize, arrowPos, arrowNeg) => {
3601 if (distance == 0) {
3602 return '';
3603 }
3604
3605 // Convert the scroll distance into a number of rows/cols.
3606 const cells = lib.f.smartFloorDivide(Math.abs(distance), charSize);
3607 const data = '\x1bO' + (distance < 0 ? arrowNeg : arrowPos);
3608 return data.repeat(cells);
3609 };
3610
3611 // The order between up/down and left/right doesn't really matter.
3612 this.io.sendString(
3613 // Up/down arrow keys.
3614 deltaToArrows(delta.y, this.scrollPort_.characterSize.height,
3615 'A', 'B') +
3616 // Left/right arrow keys.
3617 deltaToArrows(delta.x, this.scrollPort_.characterSize.width,
3618 'C', 'D')
3619 );
Mike Frysingerc3030a82017-05-29 14:16:11 -04003620
3621 e.preventDefault();
3622 }
3623 }
Robert Ginda928cf632014-03-05 15:07:41 -08003624 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003625 if (!this.scrollBlockerNode_.engaged) {
3626 if (e.type == 'mousedown') {
3627 // Move the scroll-blocker into place if we want to keep the scrollport
3628 // from scrolling.
3629 this.scrollBlockerNode_.engaged = true;
3630 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3631 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3632 } else if (e.type == 'mousemove') {
3633 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3634 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003635 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003636 e.preventDefault();
3637 }
3638 }
Robert Ginda928cf632014-03-05 15:07:41 -08003639
3640 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003641 }
3642
Robert Ginda928cf632014-03-05 15:07:41 -08003643 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3644 // Restore this on mouseup in case it was temporarily defeated with a
3645 // alt-mousedown. Only do this when the selection is empty so that
3646 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003647 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003648 }
rgindad5613292012-06-19 15:40:37 -07003649};
3650
3651/**
3652 * Clients should override this if they care to know about mouse events.
3653 *
3654 * The event parameter will be a normal DOM mouse click event with additional
3655 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003656 *
3657 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003658 */
3659hterm.Terminal.prototype.onMouse = function(e) { };
3660
3661/**
rginda8e92a692012-05-20 19:37:20 -07003662 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003663 *
3664 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003665 */
Rob Spies06533ba2014-04-24 11:20:37 -07003666hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3667 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003668 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003669
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003670 if (this.reportFocus)
3671 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O');
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003672
Michael Kelly485ecd12014-06-09 11:41:56 -04003673 if (focused === true)
3674 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07003675};
3676
3677/**
rginda8ba33642011-12-14 12:31:31 -08003678 * React when the ScrollPort is scrolled.
3679 */
3680hterm.Terminal.prototype.onScroll_ = function() {
3681 this.scheduleSyncCursorPosition_();
3682};
3683
3684/**
rginda9846e2f2012-01-27 13:53:33 -08003685 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003686 *
3687 * @param {Event} e The DOM paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08003688 */
3689hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaa063b202014-07-21 11:08:25 -07003690 var data = e.text.replace(/\n/mg, '\r');
Mike Frysingere8c32c82018-03-11 14:57:28 -07003691 if (this.options_.bracketedPaste) {
3692 // We strip out most escape sequences as they can cause issues (like
3693 // inserting an \x1b[201~ midstream). We pass through whitespace
3694 // though: 0x08:\b 0x09:\t 0x0a:\n 0x0d:\r.
3695 // This matches xterm behavior.
3696 const filter = (data) => data.replace(/[\x00-\x07\x0b-\x0c\x0e-\x1f]/g, '');
3697 data = '\x1b[200~' + filter(data) + '\x1b[201~';
3698 }
Robert Gindaa063b202014-07-21 11:08:25 -07003699
3700 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003701};
3702
3703/**
rgindaa09e7332012-08-17 12:49:51 -07003704 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003705 *
3706 * @param {Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07003707 */
3708hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07003709 if (!this.useDefaultWindowCopy) {
3710 e.preventDefault();
3711 setTimeout(this.copySelectionToClipboard.bind(this), 0);
3712 }
rgindaa09e7332012-08-17 12:49:51 -07003713};
3714
3715/**
rginda8ba33642011-12-14 12:31:31 -08003716 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08003717 *
3718 * Note: This function should not directly contain code that alters the internal
3719 * state of the terminal. That kind of code belongs in realizeWidth or
3720 * realizeHeight, so that it can be executed synchronously in the case of a
3721 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08003722 */
3723hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08003724 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07003725 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08003726 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07003727 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08003728
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003729 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08003730 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003731 // gets removed from the document or during the initial load, and we can't
3732 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07003733 // This can also happen if called before the scrollPort calculates the
3734 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08003735 return;
3736 }
3737
rgindaa8ba17d2012-08-15 14:41:10 -07003738 var isNewSize = (columnCount != this.screenSize.width ||
3739 rowCount != this.screenSize.height);
3740
3741 // We do this even if the size didn't change, just to be sure everything is
3742 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04003743 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07003744 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07003745
3746 if (isNewSize)
3747 this.overlaySize();
3748
Robert Gindafb1be6a2013-12-11 11:56:22 -08003749 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07003750 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08003751};
3752
3753/**
3754 * Service the cursor blink timeout.
3755 */
3756hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07003757 if (!this.options_.cursorBlink) {
3758 delete this.timeouts_.cursorBlink;
3759 return;
3760 }
3761
Robert Ginda830583c2013-08-07 13:20:46 -07003762 if (this.cursorNode_.getAttribute('focus') == 'false' ||
3763 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08003764 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07003765 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3766 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08003767 } else {
rginda87b86462011-12-14 13:48:03 -08003768 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07003769 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3770 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08003771 }
3772};
David Reveman8f552492012-03-28 12:18:41 -04003773
3774/**
3775 * Set the scrollbar-visible mode bit.
3776 *
3777 * If scrollbar-visible is on, the vertical scrollbar will be visible.
3778 * Otherwise it will not.
3779 *
3780 * Defaults to on.
3781 *
3782 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
3783 */
3784hterm.Terminal.prototype.setScrollbarVisible = function(state) {
3785 this.scrollPort_.setScrollbarVisible(state);
3786};
Michael Kelly485ecd12014-06-09 11:41:56 -04003787
3788/**
Rob Spies49039e52014-12-17 13:40:04 -08003789 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04003790 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08003791 *
3792 * Defaults to 1.
3793 *
Evan Jones2600d4f2016-12-06 09:29:36 -05003794 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08003795 */
3796hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3797 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3798};
3799
3800/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003801 * Close all web notifications created by terminal bells.
3802 */
3803hterm.Terminal.prototype.closeBellNotifications_ = function() {
3804 this.bellNotificationList_.forEach(function(n) {
3805 n.close();
3806 });
3807 this.bellNotificationList_.length = 0;
3808};
Raymes Khourye5d48982018-08-02 09:08:32 +10003809
3810/**
3811 * Syncs the cursor position when the scrollport gains focus.
3812 */
3813hterm.Terminal.prototype.onScrollportFocus_ = function() {
3814 // If the cursor is offscreen we set selection to the last row on the screen.
3815 const topRowIndex = this.scrollPort_.getTopRowIndex();
3816 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
3817 const selection = this.document_.getSelection();
3818 if (!this.syncCursorPosition_() && selection) {
3819 selection.collapse(this.getRowNode(bottomRowIndex));
3820 }
3821};