blob: 034485bf0320425d2256b9b89d6dafef570366bd [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
Masaya Suzuki273aa982014-05-31 07:25:55 +09007lib.rtdep('lib.colors', 'lib.PreferenceManager', 'lib.resource', 'lib.wc',
Raymes Khoury3e44bc92018-05-17 10:54:23 +10008 'lib.f', 'hterm.AccessibilityReader', 'hterm.Keyboard',
9 'hterm.Options', 'hterm.PreferenceManager', 'hterm.Screen',
10 'hterm.ScrollPort', 'hterm.Size', 'hterm.TextAttributes', 'hterm.VT');
rgindacbbd7482012-06-13 15:06:16 -070011
rginda8ba33642011-12-14 12:31:31 -080012/**
13 * Constructor for the Terminal class.
14 *
15 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
16 * classes to provide the complete terminal functionality.
17 *
18 * There are a number of lower-level Terminal methods that can be called
19 * directly to manipulate the cursor, text, scroll region, and other terminal
20 * attributes. However, the primary method is interpret(), which parses VT
21 * escape sequences and invokes the appropriate Terminal methods.
22 *
23 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
24 *
25 * TODO(rginda): Eventually we're going to need to support characters which are
26 * displayed twice as wide as standard latin characters. This is to support
27 * CJK (and possibly other character sets).
rginda9f5222b2012-03-05 11:53:28 -080028 *
Robert Ginda57f03b42012-09-13 11:02:48 -070029 * @param {string} opt_profileId Optional preference profile name. If not
rginda9f5222b2012-03-05 11:53:28 -080030 * provided, defaults to 'default'.
rginda8ba33642011-12-14 12:31:31 -080031 */
Robert Ginda57f03b42012-09-13 11:02:48 -070032hterm.Terminal = function(opt_profileId) {
33 this.profileId_ = null;
rginda9f5222b2012-03-05 11:53:28 -080034
rginda8ba33642011-12-14 12:31:31 -080035 // Two screen instances.
36 this.primaryScreen_ = new hterm.Screen();
37 this.alternateScreen_ = new hterm.Screen();
38
39 // The "current" screen.
40 this.screen_ = this.primaryScreen_;
41
rginda8ba33642011-12-14 12:31:31 -080042 // The local notion of the screen size. ScreenBuffers also have a size which
43 // indicates their present size. During size changes, the two may disagree.
44 // Also, the inactive screen's size is not altered until it is made the active
45 // screen.
46 this.screenSize = new hterm.Size(0, 0);
47
rginda8ba33642011-12-14 12:31:31 -080048 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080049 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080050 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
51 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080052 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
Raymes Khourye5d48982018-08-02 09:08:32 +100053 this.scrollPort_.subscribe('focus', this.onScrollportFocus_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070054 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080055
rginda87b86462011-12-14 13:48:03 -080056 // The div that contains this terminal.
57 this.div_ = null;
58
rgindac9bc5502012-01-18 11:48:44 -080059 // The document that contains the scrollPort. Defaulted to the global
60 // document here so that the terminal is functional even if it hasn't been
61 // inserted into a document yet, but re-set in decorate().
62 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080063
rginda8ba33642011-12-14 12:31:31 -080064 // The rows that have scrolled off screen and are no longer addressable.
65 this.scrollbackRows_ = [];
66
rgindac9bc5502012-01-18 11:48:44 -080067 // Saved tab stops.
68 this.tabStops_ = [];
69
David Benjamin66e954d2012-05-05 21:08:12 -040070 // Keep track of whether default tab stops have been erased; after a TBC
71 // clears all tab stops, defaults aren't restored on resize until a reset.
72 this.defaultTabStops = true;
73
rginda8ba33642011-12-14 12:31:31 -080074 // The VT's notion of the top and bottom rows. Used during some VT
75 // cursor positioning and scrolling commands.
76 this.vtScrollTop_ = null;
77 this.vtScrollBottom_ = null;
78
79 // The DIV element for the visible cursor.
80 this.cursorNode_ = null;
81
Robert Ginda830583c2013-08-07 13:20:46 -070082 // The current cursor shape of the terminal.
83 this.cursorShape_ = hterm.Terminal.cursorShape.BLOCK;
84
Robert Gindaea2183e2014-07-17 09:51:51 -070085 // Cursor blink on/off cycle in ms, overwritten by prefs once they're loaded.
86 this.cursorBlinkCycle_ = [100, 100];
87
88 // Pre-bound onCursorBlink_ handler, so we don't have to do this for each
89 // cursor on/off servicing.
90 this.myOnCursorBlink_ = this.onCursorBlink_.bind(this);
91
rginda9f5222b2012-03-05 11:53:28 -080092 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -070093 // each output and keystroke. They are initialized by the preference manager.
Robert Ginda8cb7d902013-06-20 14:37:18 -070094 this.backgroundColor_ = null;
95 this.foregroundColor_ = null;
Robert Ginda57f03b42012-09-13 11:02:48 -070096 this.scrollOnOutput_ = null;
97 this.scrollOnKeystroke_ = null;
Mike Frysinger3c9fa072017-07-13 10:21:13 -040098 this.scrollWheelArrowKeys_ = null;
rginda9f5222b2012-03-05 11:53:28 -080099
Robert Ginda6aec7eb2015-06-16 10:31:30 -0700100 // True if we should override mouse event reporting to allow local selection.
101 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -0800102
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400103 // Whether to auto hide the mouse cursor when typing.
104 this.setAutomaticMouseHiding();
105 // Timer to keep mouse visible while it's being used.
106 this.mouseHideDelay_ = null;
107
rgindaf0090c92012-02-10 14:58:52 -0800108 // Terminal bell sound.
109 this.bellAudio_ = this.document_.createElement('audio');
Mike Frysingerd826f1a2017-07-06 16:20:06 -0400110 this.bellAudio_.id = 'hterm:bell-audio';
rgindaf0090c92012-02-10 14:58:52 -0800111 this.bellAudio_.setAttribute('preload', 'auto');
112
Raymes Khoury3e44bc92018-05-17 10:54:23 +1000113 // The AccessibilityReader object for announcing command output.
114 this.accessibilityReader_ = null;
115
Mike Frysingercc114512017-09-11 21:39:17 -0400116 // The context menu object.
117 this.contextMenu = new hterm.ContextMenu();
118
Michael Kelly485ecd12014-06-09 11:41:56 -0400119 // All terminal bell notifications that have been generated (not necessarily
120 // shown).
121 this.bellNotificationList_ = [];
122
123 // Whether we have permission to display notifications.
124 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400125
rginda6d397402012-01-17 10:58:29 -0800126 // Cursor position and attributes saved with DECSC.
127 this.savedOptions_ = {};
128
rginda8ba33642011-12-14 12:31:31 -0800129 // The current mode bits for the terminal.
130 this.options_ = new hterm.Options();
131
132 // Timeouts we might need to clear.
133 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800134
135 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800136 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800137
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800138 this.saveCursorAndState(true);
139
Zhu Qunying30d40712017-03-14 16:27:00 -0700140 // The keyboard handler.
rgindafeaf3142012-01-31 15:14:20 -0800141 this.keyboard = new hterm.Keyboard(this);
142
rginda87b86462011-12-14 13:48:03 -0800143 // General IO interface that can be given to third parties without exposing
144 // the entire terminal object.
145 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800146
rgindad5613292012-06-19 15:40:37 -0700147 // True if mouse-click-drag should scroll the terminal.
148 this.enableMouseDragScroll = true;
149
Robert Ginda57f03b42012-09-13 11:02:48 -0700150 this.copyOnSelect = null;
Mike Frysinger847577f2017-05-23 23:25:57 -0400151 this.mouseRightClickPaste = null;
rginda4bba5e12012-06-20 16:15:30 -0700152 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700153
Zhu Qunying30d40712017-03-14 16:27:00 -0700154 // Whether to use the default window copy behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700155 this.useDefaultWindowCopy = false;
156
157 this.clearSelectionAfterCopy = true;
158
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400159 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800160 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700161
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400162 // Whether we allow images to be shown.
163 this.allowImagesInline = null;
164
Gabriel Holodake8a09be2017-10-10 01:07:11 -0400165 this.reportFocus = false;
166
Robert Ginda57f03b42012-09-13 11:02:48 -0700167 this.setProfile(opt_profileId || 'default',
Evan Jones5f9df812016-12-06 09:38:58 -0500168 function() { this.onTerminalReady(); }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800169};
170
171/**
Robert Ginda830583c2013-08-07 13:20:46 -0700172 * Possible cursor shapes.
173 */
174hterm.Terminal.cursorShape = {
175 BLOCK: 'BLOCK',
176 BEAM: 'BEAM',
177 UNDERLINE: 'UNDERLINE'
178};
179
180/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700181 * Clients should override this to be notified when the terminal is ready
182 * for use.
183 *
184 * The terminal initialization is asynchronous, and shouldn't be used before
185 * this method is called.
186 */
187hterm.Terminal.prototype.onTerminalReady = function() { };
188
189/**
rginda35c456b2012-02-09 17:29:05 -0800190 * Default tab with of 8 to match xterm.
191 */
192hterm.Terminal.prototype.tabWidth = 8;
193
194/**
rginda9f5222b2012-03-05 11:53:28 -0800195 * Select a preference profile.
196 *
197 * This will load the terminal preferences for the given profile name and
198 * associate subsequent preference changes with the new preference profile.
199 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500200 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800201 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700202 * @param {function} opt_callback Optional callback to invoke when the profile
203 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800204 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700205hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
206 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800207
Robert Ginda57f03b42012-09-13 11:02:48 -0700208 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800209
Robert Ginda57f03b42012-09-13 11:02:48 -0700210 if (this.prefs_)
211 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800212
Robert Ginda57f03b42012-09-13 11:02:48 -0700213 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
214 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800215 'alt-gr-mode': function(v) {
216 if (v == null) {
217 if (navigator.language.toLowerCase() == 'en-us') {
218 v = 'none';
219 } else {
220 v = 'right-alt';
221 }
222 } else if (typeof v == 'string') {
223 v = v.toLowerCase();
224 } else {
225 v = 'none';
226 }
227
228 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v))
229 v = 'none';
230
231 terminal.keyboard.altGrMode = v;
232 },
233
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700234 'alt-backspace-is-meta-backspace': function(v) {
235 terminal.keyboard.altBackspaceIsMetaBackspace = v;
236 },
237
Robert Ginda57f03b42012-09-13 11:02:48 -0700238 'alt-is-meta': function(v) {
239 terminal.keyboard.altIsMeta = v;
240 },
241
242 'alt-sends-what': function(v) {
243 if (!/^(escape|8-bit|browser-key)$/.test(v))
244 v = 'escape';
245
246 terminal.keyboard.altSendsWhat = v;
247 },
248
249 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800250 var ary = v.match(/^lib-resource:(\S+)/);
251 if (ary) {
252 terminal.bellAudio_.setAttribute('src',
253 lib.resource.getDataUrl(ary[1]));
254 } else {
255 terminal.bellAudio_.setAttribute('src', v);
256 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700257 },
258
Michael Kelly485ecd12014-06-09 11:41:56 -0400259 'desktop-notification-bell': function(v) {
260 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700261 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400262 Notification.permission === 'granted';
263 if (!terminal.desktopNotificationBell_) {
264 // Note: We don't call Notification.requestPermission here because
265 // Chrome requires the call be the result of a user action (such as an
266 // onclick handler), and pref listeners are run asynchronously.
267 //
268 // A way of working around this would be to display a dialog in the
269 // terminal with a "click-to-request-permission" button.
270 console.warn('desktop-notification-bell is true but we do not have ' +
271 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400272 }
273 } else {
274 terminal.desktopNotificationBell_ = false;
275 }
276 },
277
Robert Ginda57f03b42012-09-13 11:02:48 -0700278 'background-color': function(v) {
279 terminal.setBackgroundColor(v);
280 },
281
282 'background-image': function(v) {
283 terminal.scrollPort_.setBackgroundImage(v);
284 },
285
286 'background-size': function(v) {
287 terminal.scrollPort_.setBackgroundSize(v);
288 },
289
290 'background-position': function(v) {
291 terminal.scrollPort_.setBackgroundPosition(v);
292 },
293
294 'backspace-sends-backspace': function(v) {
295 terminal.keyboard.backspaceSendsBackspace = v;
296 },
297
Brad Town18654b62015-03-12 00:27:45 -0700298 'character-map-overrides': function(v) {
299 if (!(v == null || v instanceof Object)) {
300 console.warn('Preference character-map-modifications is not an ' +
301 'object: ' + v);
302 return;
303 }
304
Mike Frysinger095d4062017-06-14 00:29:48 -0700305 terminal.vt.characterMaps.reset();
306 terminal.vt.characterMaps.setOverrides(v);
Brad Town18654b62015-03-12 00:27:45 -0700307 },
308
Robert Ginda57f03b42012-09-13 11:02:48 -0700309 'cursor-blink': function(v) {
310 terminal.setCursorBlink(!!v);
311 },
312
Robert Gindaea2183e2014-07-17 09:51:51 -0700313 'cursor-blink-cycle': function(v) {
314 if (v instanceof Array &&
315 typeof v[0] == 'number' &&
316 typeof v[1] == 'number') {
317 terminal.cursorBlinkCycle_ = v;
318 } else if (typeof v == 'number') {
319 terminal.cursorBlinkCycle_ = [v, v];
320 } else {
321 // Fast blink indicates an error.
322 terminal.cursorBlinkCycle_ = [100, 100];
323 }
324 },
325
Robert Ginda57f03b42012-09-13 11:02:48 -0700326 'cursor-color': function(v) {
327 terminal.setCursorColor(v);
328 },
329
330 'color-palette-overrides': function(v) {
331 if (!(v == null || v instanceof Object || v instanceof Array)) {
332 console.warn('Preference color-palette-overrides is not an array or ' +
333 'object: ' + v);
334 return;
rginda9f5222b2012-03-05 11:53:28 -0800335 }
rginda9f5222b2012-03-05 11:53:28 -0800336
Robert Ginda57f03b42012-09-13 11:02:48 -0700337 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700338
Robert Ginda57f03b42012-09-13 11:02:48 -0700339 if (v) {
340 for (var key in v) {
341 var i = parseInt(key);
342 if (isNaN(i) || i < 0 || i > 255) {
343 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
344 continue;
345 }
346
347 if (v[i]) {
348 var rgb = lib.colors.normalizeCSS(v[i]);
349 if (rgb)
350 lib.colors.colorPalette[i] = rgb;
351 }
352 }
rginda30f20f62012-04-05 16:36:19 -0700353 }
rginda30f20f62012-04-05 16:36:19 -0700354
Evan Jones5f9df812016-12-06 09:38:58 -0500355 terminal.primaryScreen_.textAttributes.resetColorPalette();
Robert Ginda57f03b42012-09-13 11:02:48 -0700356 terminal.alternateScreen_.textAttributes.resetColorPalette();
357 },
rginda30f20f62012-04-05 16:36:19 -0700358
Robert Ginda57f03b42012-09-13 11:02:48 -0700359 'copy-on-select': function(v) {
360 terminal.copyOnSelect = !!v;
361 },
rginda9f5222b2012-03-05 11:53:28 -0800362
Rob Spies0bec09b2014-06-06 15:58:09 -0700363 'use-default-window-copy': function(v) {
364 terminal.useDefaultWindowCopy = !!v;
365 },
366
367 'clear-selection-after-copy': function(v) {
368 terminal.clearSelectionAfterCopy = !!v;
369 },
370
Robert Ginda7e5e9522014-03-14 12:23:58 -0700371 'ctrl-plus-minus-zero-zoom': function(v) {
372 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
373 },
374
Robert Gindafb5a3f92014-05-13 14:12:00 -0700375 'ctrl-c-copy': function(v) {
376 terminal.keyboard.ctrlCCopy = v;
377 },
378
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100379 'ctrl-v-paste': function(v) {
380 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700381 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100382 },
383
Cody Coljee-Gray7c6a0392018-10-25 13:18:28 -0700384 'paste-on-drop': function(v) {
385 terminal.scrollPort_.setPasteOnDrop(v);
386 },
387
Masaya Suzuki273aa982014-05-31 07:25:55 +0900388 'east-asian-ambiguous-as-two-column': function(v) {
389 lib.wc.regardCjkAmbiguous = v;
390 },
391
Robert Ginda57f03b42012-09-13 11:02:48 -0700392 'enable-8-bit-control': function(v) {
393 terminal.vt.enable8BitControl = !!v;
394 },
rginda30f20f62012-04-05 16:36:19 -0700395
Robert Ginda57f03b42012-09-13 11:02:48 -0700396 'enable-bold': function(v) {
397 terminal.syncBoldSafeState();
398 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400399
Robert Ginda3e278d72014-03-25 13:18:51 -0700400 'enable-bold-as-bright': function(v) {
401 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
402 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
403 },
404
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400405 'enable-blink': function(v) {
Mike Frysinger261597c2017-12-28 01:14:21 -0500406 terminal.setTextBlink(!!v);
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400407 },
408
Robert Ginda57f03b42012-09-13 11:02:48 -0700409 'enable-clipboard-write': function(v) {
410 terminal.vt.enableClipboardWrite = !!v;
411 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400412
Robert Ginda3755e752013-05-31 13:34:09 -0700413 'enable-dec12': function(v) {
414 terminal.vt.enableDec12 = !!v;
415 },
416
Mike Frysinger38f267d2018-09-07 02:50:59 -0400417 'enable-csi-j-3': function(v) {
418 terminal.vt.enableCsiJ3 = !!v;
419 },
420
Robert Ginda57f03b42012-09-13 11:02:48 -0700421 'font-family': function(v) {
422 terminal.syncFontFamily();
423 },
rginda30f20f62012-04-05 16:36:19 -0700424
Robert Ginda57f03b42012-09-13 11:02:48 -0700425 'font-size': function(v) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500426 v = parseInt(v);
427 if (v <= 0) {
428 console.error(`Invalid font size: ${v}`);
429 return;
430 }
431
Robert Ginda57f03b42012-09-13 11:02:48 -0700432 terminal.setFontSize(v);
433 },
rginda9875d902012-08-20 16:21:57 -0700434
Robert Ginda57f03b42012-09-13 11:02:48 -0700435 'font-smoothing': function(v) {
436 terminal.syncFontFamily();
437 },
rgindade84e382012-04-20 15:39:31 -0700438
Robert Ginda57f03b42012-09-13 11:02:48 -0700439 'foreground-color': function(v) {
440 terminal.setForegroundColor(v);
441 },
rginda30f20f62012-04-05 16:36:19 -0700442
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400443 'hide-mouse-while-typing': function(v) {
444 terminal.setAutomaticMouseHiding(v);
445 },
446
Robert Ginda57f03b42012-09-13 11:02:48 -0700447 'home-keys-scroll': function(v) {
448 terminal.keyboard.homeKeysScroll = v;
449 },
rginda4bba5e12012-06-20 16:15:30 -0700450
Robert Gindaa8165692015-06-15 14:46:31 -0700451 'keybindings': function(v) {
452 terminal.keyboard.bindings.clear();
453
454 if (!v)
455 return;
456
457 if (!(v instanceof Object)) {
458 console.error('Error in keybindings preference: Expected object');
459 return;
460 }
461
462 try {
463 terminal.keyboard.bindings.addBindings(v);
464 } catch (ex) {
465 console.error('Error in keybindings preference: ' + ex);
466 }
467 },
468
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700469 'media-keys-are-fkeys': function(v) {
470 terminal.keyboard.mediaKeysAreFKeys = v;
471 },
472
Robert Ginda57f03b42012-09-13 11:02:48 -0700473 'meta-sends-escape': function(v) {
474 terminal.keyboard.metaSendsEscape = v;
475 },
rginda30f20f62012-04-05 16:36:19 -0700476
Mike Frysinger847577f2017-05-23 23:25:57 -0400477 'mouse-right-click-paste': function(v) {
478 terminal.mouseRightClickPaste = v;
479 },
480
Robert Ginda57f03b42012-09-13 11:02:48 -0700481 'mouse-paste-button': function(v) {
482 terminal.syncMousePasteButton();
483 },
rgindaa8ba17d2012-08-15 14:41:10 -0700484
Robert Gindae76aa9f2014-03-14 12:29:12 -0700485 'page-keys-scroll': function(v) {
486 terminal.keyboard.pageKeysScroll = v;
487 },
488
Robert Ginda40932892012-12-10 17:26:40 -0800489 'pass-alt-number': function(v) {
490 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800491 // Let Alt-1..9 pass to the browser (to control tab switching) on
492 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500493 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800494 }
495
496 terminal.passAltNumber = v;
497 },
498
499 'pass-ctrl-number': function(v) {
500 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800501 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
502 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500503 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800504 }
505
506 terminal.passCtrlNumber = v;
507 },
508
509 'pass-meta-number': function(v) {
510 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800511 // Let Meta-1..9 pass to the browser (to control tab switching) on
512 // OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500513 v = (hterm.os == 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800514 }
515
516 terminal.passMetaNumber = v;
517 },
518
Marius Schilder77857b32014-05-14 16:21:26 -0700519 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700520 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700521 },
522
Robert Ginda8cb7d902013-06-20 14:37:18 -0700523 'receive-encoding': function(v) {
524 if (!(/^(utf-8|raw)$/).test(v)) {
525 console.warn('Invalid value for "receive-encoding": ' + v);
526 v = 'utf-8';
527 }
528
529 terminal.vt.characterEncoding = v;
530 },
531
Robert Ginda57f03b42012-09-13 11:02:48 -0700532 'scroll-on-keystroke': function(v) {
533 terminal.scrollOnKeystroke_ = v;
534 },
rginda9f5222b2012-03-05 11:53:28 -0800535
Robert Ginda57f03b42012-09-13 11:02:48 -0700536 'scroll-on-output': function(v) {
537 terminal.scrollOnOutput_ = v;
538 },
rginda30f20f62012-04-05 16:36:19 -0700539
Robert Ginda57f03b42012-09-13 11:02:48 -0700540 'scrollbar-visible': function(v) {
541 terminal.setScrollbarVisible(v);
542 },
rginda9f5222b2012-03-05 11:53:28 -0800543
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400544 'scroll-wheel-may-send-arrow-keys': function(v) {
545 terminal.scrollWheelArrowKeys_ = v;
546 },
547
Rob Spies49039e52014-12-17 13:40:04 -0800548 'scroll-wheel-move-multiplier': function(v) {
549 terminal.setScrollWheelMoveMultipler(v);
550 },
551
Robert Ginda57f03b42012-09-13 11:02:48 -0700552 'shift-insert-paste': function(v) {
553 terminal.keyboard.shiftInsertPaste = v;
554 },
rginda9f5222b2012-03-05 11:53:28 -0800555
Mike Frysingera7768922017-07-28 15:00:12 -0400556 'terminal-encoding': function(v) {
Mike Frysingera1371e12017-08-17 01:37:17 -0400557 terminal.vt.setEncoding(v);
Mike Frysingera7768922017-07-28 15:00:12 -0400558 },
559
Robert Gindae76aa9f2014-03-14 12:29:12 -0700560 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400561 terminal.scrollPort_.setUserCssUrl(v);
562 },
563
564 'user-css-text': function(v) {
565 terminal.scrollPort_.setUserCssText(v);
566 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400567
568 'word-break-match-left': function(v) {
569 terminal.primaryScreen_.wordBreakMatchLeft = v;
570 terminal.alternateScreen_.wordBreakMatchLeft = v;
571 },
572
573 'word-break-match-right': function(v) {
574 terminal.primaryScreen_.wordBreakMatchRight = v;
575 terminal.alternateScreen_.wordBreakMatchRight = v;
576 },
577
578 'word-break-match-middle': function(v) {
579 terminal.primaryScreen_.wordBreakMatchMiddle = v;
580 terminal.alternateScreen_.wordBreakMatchMiddle = v;
581 },
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400582
583 'allow-images-inline': function(v) {
584 terminal.allowImagesInline = v;
585 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700586 });
rginda30f20f62012-04-05 16:36:19 -0700587
Robert Ginda57f03b42012-09-13 11:02:48 -0700588 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800589 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700590
591 if (opt_callback)
592 opt_callback();
593 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800594};
595
Rob Spies56953412014-04-28 14:09:47 -0700596
597/**
598 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500599 *
600 * @return {hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700601 */
602hterm.Terminal.prototype.getPrefs = function() {
603 return this.prefs_;
604};
605
Robert Gindaa063b202014-07-21 11:08:25 -0700606/**
607 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500608 *
609 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700610 */
611hterm.Terminal.prototype.setBracketedPaste = function(state) {
612 this.options_.bracketedPaste = state;
613};
Rob Spies56953412014-04-28 14:09:47 -0700614
rginda8e92a692012-05-20 19:37:20 -0700615/**
616 * Set the color for the cursor.
617 *
618 * If you want this setting to persist, set it through prefs_, rather than
619 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500620 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500621 * @param {string=} color The color to set. If not defined, we reset to the
622 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700623 */
624hterm.Terminal.prototype.setCursorColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500625 if (color === undefined)
626 color = this.prefs_.get('cursor-color');
627
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400628 this.setCssVar('cursor-color', color);
rginda8e92a692012-05-20 19:37:20 -0700629};
630
631/**
632 * Return the current cursor color as a string.
Evan Jones2600d4f2016-12-06 09:29:36 -0500633 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700634 */
635hterm.Terminal.prototype.getCursorColor = function() {
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400636 return this.getCssVar('cursor-color');
rginda8e92a692012-05-20 19:37:20 -0700637};
638
639/**
rgindad5613292012-06-19 15:40:37 -0700640 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500641 *
642 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700643 */
644hterm.Terminal.prototype.setSelectionEnabled = function(state) {
645 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700646};
647
648/**
rginda8e92a692012-05-20 19:37:20 -0700649 * Set the background color.
650 *
651 * If you want this setting to persist, set it through prefs_, rather than
652 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500653 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500654 * @param {string=} color The color to set. If not defined, we reset to the
655 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700656 */
657hterm.Terminal.prototype.setBackgroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500658 if (color === undefined)
659 color = this.prefs_.get('background-color');
660
rgindacbbd7482012-06-13 15:06:16 -0700661 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700662 this.primaryScreen_.textAttributes.setDefaults(
663 this.foregroundColor_, this.backgroundColor_);
664 this.alternateScreen_.textAttributes.setDefaults(
665 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700666 this.scrollPort_.setBackgroundColor(color);
667};
668
rginda9f5222b2012-03-05 11:53:28 -0800669/**
670 * Return the current terminal background color.
671 *
672 * Intended for use by other classes, so we don't have to expose the entire
673 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500674 *
675 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800676 */
677hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700678 return this.backgroundColor_;
679};
680
681/**
682 * Set the foreground color.
683 *
684 * If you want this setting to persist, set it through prefs_, rather than
685 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500686 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500687 * @param {string=} color The color to set. If not defined, we reset to the
688 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700689 */
690hterm.Terminal.prototype.setForegroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500691 if (color === undefined)
692 color = this.prefs_.get('foreground-color');
693
rgindacbbd7482012-06-13 15:06:16 -0700694 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700695 this.primaryScreen_.textAttributes.setDefaults(
696 this.foregroundColor_, this.backgroundColor_);
697 this.alternateScreen_.textAttributes.setDefaults(
698 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700699 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800700};
701
702/**
703 * Return the current terminal foreground color.
704 *
705 * Intended for use by other classes, so we don't have to expose the entire
706 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500707 *
708 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800709 */
710hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700711 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800712};
713
714/**
rginda87b86462011-12-14 13:48:03 -0800715 * Create a new instance of a terminal command and run it with a given
716 * argument string.
717 *
718 * @param {function} commandClass The constructor for a terminal command.
719 * @param {string} argString The argument string to pass to the command.
720 */
721hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700722 var environment = this.prefs_.get('environment');
723 if (typeof environment != 'object' || environment == null)
724 environment = {};
725
rginda87b86462011-12-14 13:48:03 -0800726 var self = this;
727 this.command = new commandClass(
728 { argString: argString || '',
729 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700730 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800731 onExit: function(code) {
732 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800733 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700734 if (self.prefs_.get('close-on-exit'))
735 window.close();
rginda87b86462011-12-14 13:48:03 -0800736 }
737 });
738
rgindafeaf3142012-01-31 15:14:20 -0800739 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800740 this.command.run();
741};
742
743/**
rgindafeaf3142012-01-31 15:14:20 -0800744 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500745 *
746 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800747 */
748hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700749 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800750};
751
752/**
753 * Install the keyboard handler for this terminal.
754 *
755 * This will prevent the browser from seeing any keystrokes sent to the
756 * terminal.
757 */
758hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700759 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400760};
rgindafeaf3142012-01-31 15:14:20 -0800761
762/**
763 * Uninstall the keyboard handler for this terminal.
764 */
765hterm.Terminal.prototype.uninstallKeyboard = function() {
766 this.keyboard.installKeyboard(null);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400767};
rgindafeaf3142012-01-31 15:14:20 -0800768
769/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400770 * Set a CSS variable.
771 *
772 * Normally this is used to set variables in the hterm namespace.
773 *
774 * @param {string} name The variable to set.
775 * @param {string} value The value to assign to the variable.
776 * @param {string?} opt_prefix The variable namespace/prefix to use.
777 */
778hterm.Terminal.prototype.setCssVar = function(name, value,
779 opt_prefix='--hterm-') {
780 this.document_.documentElement.style.setProperty(
781 `${opt_prefix}${name}`, value);
782};
783
784/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500785 * Get a CSS variable.
786 *
787 * Normally this is used to get variables in the hterm namespace.
788 *
789 * @param {string} name The variable to read.
790 * @param {string?} opt_prefix The variable namespace/prefix to use.
791 * @return {string} The current setting for this variable.
792 */
793hterm.Terminal.prototype.getCssVar = function(name, opt_prefix='--hterm-') {
794 return this.document_.documentElement.style.getPropertyValue(
795 `${opt_prefix}${name}`);
796};
797
798/**
rginda35c456b2012-02-09 17:29:05 -0800799 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800800 *
801 * Call setFontSize(0) to reset to the default font size.
802 *
803 * This function does not modify the font-size preference.
804 *
805 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800806 */
807hterm.Terminal.prototype.setFontSize = function(px) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500808 if (px <= 0)
rginda9f5222b2012-03-05 11:53:28 -0800809 px = this.prefs_.get('font-size');
810
rginda35c456b2012-02-09 17:29:05 -0800811 this.scrollPort_.setFontSize(px);
Mike Frysingercce97c42017-08-05 01:11:22 -0400812 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
813 this.setCssVar('charsize-height',
814 this.scrollPort_.characterSize.height + 'px');
rginda35c456b2012-02-09 17:29:05 -0800815};
816
817/**
818 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500819 *
820 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800821 */
822hterm.Terminal.prototype.getFontSize = function() {
823 return this.scrollPort_.getFontSize();
824};
825
826/**
rginda8e92a692012-05-20 19:37:20 -0700827 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500828 *
829 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700830 */
831hterm.Terminal.prototype.getFontFamily = function() {
832 return this.scrollPort_.getFontFamily();
833};
834
835/**
rginda35c456b2012-02-09 17:29:05 -0800836 * Set the CSS "font-family" for this terminal.
837 */
rginda9f5222b2012-03-05 11:53:28 -0800838hterm.Terminal.prototype.syncFontFamily = function() {
839 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
840 this.prefs_.get('font-smoothing'));
841 this.syncBoldSafeState();
842};
843
rginda4bba5e12012-06-20 16:15:30 -0700844/**
845 * Set this.mousePasteButton based on the mouse-paste-button pref,
846 * autodetecting if necessary.
847 */
848hterm.Terminal.prototype.syncMousePasteButton = function() {
849 var button = this.prefs_.get('mouse-paste-button');
850 if (typeof button == 'number') {
851 this.mousePasteButton = button;
852 return;
853 }
854
Mike Frysingeree81a002017-12-12 16:14:53 -0500855 if (hterm.os != 'linux') {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400856 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700857 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400858 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700859 }
860};
861
862/**
863 * Enable or disable bold based on the enable-bold pref, autodetecting if
864 * necessary.
865 */
rginda9f5222b2012-03-05 11:53:28 -0800866hterm.Terminal.prototype.syncBoldSafeState = function() {
867 var enableBold = this.prefs_.get('enable-bold');
868 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700869 this.primaryScreen_.textAttributes.enableBold = enableBold;
870 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800871 return;
872 }
873
rgindaf7521392012-02-28 17:20:34 -0800874 var normalSize = this.scrollPort_.measureCharacterSize();
875 var boldSize = this.scrollPort_.measureCharacterSize('bold');
876
877 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800878 if (!isBoldSafe) {
879 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700880 'from normal. Font family is: ' +
881 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800882 }
rginda9f5222b2012-03-05 11:53:28 -0800883
Robert Gindaed016262012-10-26 16:27:09 -0700884 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
885 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800886};
887
888/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500889 * Control text blinking behavior.
890 *
891 * @param {boolean=} state Whether to enable support for blinking text.
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400892 */
Mike Frysinger261597c2017-12-28 01:14:21 -0500893hterm.Terminal.prototype.setTextBlink = function(state) {
894 if (state === undefined)
895 state = this.prefs_.get('enable-blink');
896 this.setCssVar('blink-node-duration', state ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400897};
898
899/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400900 * Set the mouse cursor style based on the current terminal mode.
901 */
902hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -0400903 this.setCssVar('mouse-cursor-style',
904 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
905 'var(--hterm-mouse-cursor-text)' :
Mike Frysinger67f58f82018-11-22 13:38:22 -0500906 'var(--hterm-mouse-cursor-default)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400907};
908
909/**
rginda87b86462011-12-14 13:48:03 -0800910 * Return a copy of the current cursor position.
911 *
912 * @return {hterm.RowCol} The RowCol object representing the current position.
913 */
914hterm.Terminal.prototype.saveCursor = function() {
915 return this.screen_.cursorPosition.clone();
916};
917
Evan Jones2600d4f2016-12-06 09:29:36 -0500918/**
919 * Return the current text attributes.
920 *
921 * @return {string}
922 */
rgindaa19afe22012-01-25 15:40:22 -0800923hterm.Terminal.prototype.getTextAttributes = function() {
924 return this.screen_.textAttributes;
925};
926
Evan Jones2600d4f2016-12-06 09:29:36 -0500927/**
928 * Set the text attributes.
929 *
930 * @param {string} textAttributes The attributes to set.
931 */
rginda1a09aa02012-06-18 21:11:25 -0700932hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
933 this.screen_.textAttributes = textAttributes;
934};
935
rginda87b86462011-12-14 13:48:03 -0800936/**
rgindaf522ce02012-04-17 17:49:17 -0700937 * Return the current browser zoom factor applied to the terminal.
938 *
939 * @return {number} The current browser zoom factor.
940 */
941hterm.Terminal.prototype.getZoomFactor = function() {
942 return this.scrollPort_.characterSize.zoomFactor;
943};
944
945/**
rginda9846e2f2012-01-27 13:53:33 -0800946 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -0500947 *
948 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -0800949 */
950hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800951 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800952};
953
954/**
rginda87b86462011-12-14 13:48:03 -0800955 * Restore a previously saved cursor position.
956 *
957 * @param {hterm.RowCol} cursor The position to restore.
958 */
959hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700960 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
961 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800962 this.screen_.setCursorPosition(row, column);
963 if (cursor.column > column ||
964 cursor.column == column && cursor.overflow) {
965 this.screen_.cursorPosition.overflow = true;
966 }
rginda87b86462011-12-14 13:48:03 -0800967};
968
969/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400970 * Clear the cursor's overflow flag.
971 */
972hterm.Terminal.prototype.clearCursorOverflow = function() {
973 this.screen_.cursorPosition.overflow = false;
974};
975
976/**
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800977 * Save the current cursor state to the corresponding screens.
978 *
979 * See the hterm.Screen.CursorState class for more details.
980 *
981 * @param {boolean=} both If true, update both screens, else only update the
982 * current screen.
983 */
984hterm.Terminal.prototype.saveCursorAndState = function(both) {
985 if (both) {
986 this.primaryScreen_.saveCursorAndState(this.vt);
987 this.alternateScreen_.saveCursorAndState(this.vt);
988 } else
989 this.screen_.saveCursorAndState(this.vt);
990};
991
992/**
993 * Restore the saved cursor state in the corresponding screens.
994 *
995 * See the hterm.Screen.CursorState class for more details.
996 *
997 * @param {boolean=} both If true, update both screens, else only update the
998 * current screen.
999 */
1000hterm.Terminal.prototype.restoreCursorAndState = function(both) {
1001 if (both) {
1002 this.primaryScreen_.restoreCursorAndState(this.vt);
1003 this.alternateScreen_.restoreCursorAndState(this.vt);
1004 } else
1005 this.screen_.restoreCursorAndState(this.vt);
1006};
1007
1008/**
Robert Ginda830583c2013-08-07 13:20:46 -07001009 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001010 *
1011 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -07001012 */
1013hterm.Terminal.prototype.setCursorShape = function(shape) {
1014 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001015 this.restyleCursor_();
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001016};
Robert Ginda830583c2013-08-07 13:20:46 -07001017
1018/**
1019 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001020 *
1021 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -07001022 */
1023hterm.Terminal.prototype.getCursorShape = function() {
1024 return this.cursorShape_;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001025};
Robert Ginda830583c2013-08-07 13:20:46 -07001026
1027/**
rginda87b86462011-12-14 13:48:03 -08001028 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001029 *
1030 * @param {number} columnCount
rginda87b86462011-12-14 13:48:03 -08001031 */
1032hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -08001033 if (columnCount == null) {
1034 this.div_.style.width = '100%';
1035 return;
1036 }
1037
Robert Ginda26806d12014-07-24 13:44:07 -07001038 this.div_.style.width = Math.ceil(
1039 this.scrollPort_.characterSize.width *
1040 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001041 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -08001042 this.scheduleSyncCursorPosition_();
1043};
rginda87b86462011-12-14 13:48:03 -08001044
rgindac9bc5502012-01-18 11:48:44 -08001045/**
rginda35c456b2012-02-09 17:29:05 -08001046 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001047 *
1048 * @param {number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -08001049 */
1050hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -08001051 if (rowCount == null) {
1052 this.div_.style.height = '100%';
1053 return;
1054 }
1055
rginda35c456b2012-02-09 17:29:05 -08001056 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -07001057 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -08001058 this.realizeSize_(this.screenSize.width, rowCount);
1059 this.scheduleSyncCursorPosition_();
1060};
1061
1062/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001063 * Deal with terminal size changes.
1064 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001065 * @param {number} columnCount The number of columns.
1066 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001067 */
1068hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
1069 if (columnCount != this.screenSize.width)
1070 this.realizeWidth_(columnCount);
1071
1072 if (rowCount != this.screenSize.height)
1073 this.realizeHeight_(rowCount);
1074
1075 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -07001076 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001077};
1078
1079/**
rgindac9bc5502012-01-18 11:48:44 -08001080 * Deal with terminal width changes.
1081 *
1082 * This function does what needs to be done when the terminal width changes
1083 * out from under us. It happens here rather than in onResize_() because this
1084 * code may need to run synchronously to handle programmatic changes of
1085 * terminal width.
1086 *
1087 * Relying on the browser to send us an async resize event means we may not be
1088 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001089 *
1090 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001091 */
1092hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001093 if (columnCount <= 0)
1094 throw new Error('Attempt to realize bad width: ' + columnCount);
1095
rgindac9bc5502012-01-18 11:48:44 -08001096 var deltaColumns = columnCount - this.screen_.getWidth();
1097
rginda87b86462011-12-14 13:48:03 -08001098 this.screenSize.width = columnCount;
1099 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001100
1101 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -04001102 if (this.defaultTabStops)
1103 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -08001104 } else {
1105 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -04001106 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -08001107 break;
1108
1109 this.tabStops_.pop();
1110 }
1111 }
1112
1113 this.screen_.setColumnCount(this.screenSize.width);
1114};
1115
1116/**
1117 * Deal with terminal height changes.
1118 *
1119 * This function does what needs to be done when the terminal height changes
1120 * out from under us. It happens here rather than in onResize_() because this
1121 * code may need to run synchronously to handle programmatic changes of
1122 * terminal height.
1123 *
1124 * Relying on the browser to send us an async resize event means we may not be
1125 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001126 *
1127 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001128 */
1129hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001130 if (rowCount <= 0)
1131 throw new Error('Attempt to realize bad height: ' + rowCount);
1132
rgindac9bc5502012-01-18 11:48:44 -08001133 var deltaRows = rowCount - this.screen_.getHeight();
1134
1135 this.screenSize.height = rowCount;
1136
1137 var cursor = this.saveCursor();
1138
1139 if (deltaRows < 0) {
1140 // Screen got smaller.
1141 deltaRows *= -1;
1142 while (deltaRows) {
1143 var lastRow = this.getRowCount() - 1;
1144 if (lastRow - this.scrollbackRows_.length == cursor.row)
1145 break;
1146
1147 if (this.getRowText(lastRow))
1148 break;
1149
1150 this.screen_.popRow();
1151 deltaRows--;
1152 }
1153
1154 var ary = this.screen_.shiftRows(deltaRows);
1155 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1156
1157 // We just removed rows from the top of the screen, we need to update
1158 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001159 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001160 } else if (deltaRows > 0) {
1161 // Screen got larger.
1162
1163 if (deltaRows <= this.scrollbackRows_.length) {
1164 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1165 var rows = this.scrollbackRows_.splice(
1166 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1167 this.screen_.unshiftRows(rows);
1168 deltaRows -= scrollbackCount;
1169 cursor.row += scrollbackCount;
1170 }
1171
1172 if (deltaRows)
1173 this.appendRows_(deltaRows);
1174 }
1175
rginda35c456b2012-02-09 17:29:05 -08001176 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001177 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001178};
1179
1180/**
1181 * Scroll the terminal to the top of the scrollback buffer.
1182 */
1183hterm.Terminal.prototype.scrollHome = function() {
1184 this.scrollPort_.scrollRowToTop(0);
1185};
1186
1187/**
1188 * Scroll the terminal to the end.
1189 */
1190hterm.Terminal.prototype.scrollEnd = function() {
1191 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1192};
1193
1194/**
1195 * Scroll the terminal one page up (minus one line) relative to the current
1196 * position.
1197 */
1198hterm.Terminal.prototype.scrollPageUp = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001199 this.scrollPort_.scrollPageUp();
rginda87b86462011-12-14 13:48:03 -08001200};
1201
1202/**
1203 * Scroll the terminal one page down (minus one line) relative to the current
1204 * position.
1205 */
1206hterm.Terminal.prototype.scrollPageDown = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001207 this.scrollPort_.scrollPageDown();
rginda8ba33642011-12-14 12:31:31 -08001208};
1209
rgindac9bc5502012-01-18 11:48:44 -08001210/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001211 * Scroll the terminal one line up relative to the current position.
1212 */
1213hterm.Terminal.prototype.scrollLineUp = function() {
1214 var i = this.scrollPort_.getTopRowIndex();
1215 this.scrollPort_.scrollRowToTop(i - 1);
1216};
1217
1218/**
1219 * Scroll the terminal one line down relative to the current position.
1220 */
1221hterm.Terminal.prototype.scrollLineDown = function() {
1222 var i = this.scrollPort_.getTopRowIndex();
1223 this.scrollPort_.scrollRowToTop(i + 1);
1224};
1225
1226/**
Robert Ginda40932892012-12-10 17:26:40 -08001227 * Clear primary screen, secondary screen, and the scrollback buffer.
1228 */
1229hterm.Terminal.prototype.wipeContents = function() {
Mike Frysinger9c482b82018-09-07 02:49:36 -04001230 this.clearHome(this.primaryScreen_);
1231 this.clearHome(this.alternateScreen_);
1232
1233 this.clearScrollback();
1234};
1235
1236/**
1237 * Clear scrollback buffer.
1238 */
1239hterm.Terminal.prototype.clearScrollback = function() {
1240 // Move to the end of the buffer in case the screen was scrolled back.
1241 // We're going to throw it away which would leave the display invalid.
1242 this.scrollEnd();
1243
Robert Ginda40932892012-12-10 17:26:40 -08001244 this.scrollbackRows_.length = 0;
1245 this.scrollPort_.resetCache();
1246
Mike Frysinger9c482b82018-09-07 02:49:36 -04001247 [this.primaryScreen_, this.alternateScreen_].forEach((screen) => {
1248 const bottom = screen.getHeight();
1249 this.renumberRows_(0, bottom, screen);
1250 });
Robert Ginda40932892012-12-10 17:26:40 -08001251
1252 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001253 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001254};
1255
1256/**
rgindac9bc5502012-01-18 11:48:44 -08001257 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001258 *
1259 * Perform a full reset to the default values listed in
1260 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001261 */
rginda87b86462011-12-14 13:48:03 -08001262hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001263 this.vt.reset();
1264
rgindac9bc5502012-01-18 11:48:44 -08001265 this.clearAllTabStops();
1266 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001267
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001268 const resetScreen = (screen) => {
1269 // We want to make sure to reset the attributes before we clear the screen.
1270 // The attributes might be used to initialize default/empty rows.
1271 screen.textAttributes.reset();
1272 screen.textAttributes.resetColorPalette();
1273 this.clearHome(screen);
1274 screen.saveCursorAndState(this.vt);
1275 };
1276 resetScreen(this.primaryScreen_);
1277 resetScreen(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001278
Mike Frysinger84301d02017-11-29 13:28:46 -08001279 // Reset terminal options to their default values.
1280 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001281 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1282
Mike Frysinger84301d02017-11-29 13:28:46 -08001283 this.setVTScrollRegion(null, null);
1284
1285 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001286};
1287
rgindac9bc5502012-01-18 11:48:44 -08001288/**
1289 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001290 *
1291 * Perform a soft reset to the default values listed in
1292 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001293 */
rginda0f5c0292012-01-13 11:00:13 -08001294hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001295 this.vt.reset();
1296
rgindab8bc8932012-04-27 12:45:03 -07001297 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001298 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001299
Brad Townb62dfdc2015-03-16 19:07:15 -07001300 // We show the cursor on soft reset but do not alter the blink state.
1301 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1302
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001303 const resetScreen = (screen) => {
1304 // Xterm also resets the color palette on soft reset, even though it doesn't
1305 // seem to be documented anywhere.
1306 screen.textAttributes.reset();
1307 screen.textAttributes.resetColorPalette();
1308 screen.saveCursorAndState(this.vt);
1309 };
1310 resetScreen(this.primaryScreen_);
1311 resetScreen(this.alternateScreen_);
rgindaf522ce02012-04-17 17:49:17 -07001312
rgindab8bc8932012-04-27 12:45:03 -07001313 // The xterm man page explicitly says this will happen on soft reset.
1314 this.setVTScrollRegion(null, null);
1315
1316 // Xterm also shows the cursor on soft reset, but does not alter the blink
1317 // state.
rgindaa19afe22012-01-25 15:40:22 -08001318 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001319};
1320
rgindac9bc5502012-01-18 11:48:44 -08001321/**
1322 * Move the cursor forward to the next tab stop, or to the last column
1323 * if no more tab stops are set.
1324 */
1325hterm.Terminal.prototype.forwardTabStop = function() {
1326 var column = this.screen_.cursorPosition.column;
1327
1328 for (var i = 0; i < this.tabStops_.length; i++) {
1329 if (this.tabStops_[i] > column) {
1330 this.setCursorColumn(this.tabStops_[i]);
1331 return;
1332 }
1333 }
1334
David Benjamin66e954d2012-05-05 21:08:12 -04001335 // xterm does not clear the overflow flag on HT or CHT.
1336 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001337 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001338 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001339};
1340
rgindac9bc5502012-01-18 11:48:44 -08001341/**
1342 * Move the cursor backward to the previous tab stop, or to the first column
1343 * if no previous tab stops are set.
1344 */
1345hterm.Terminal.prototype.backwardTabStop = function() {
1346 var column = this.screen_.cursorPosition.column;
1347
1348 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1349 if (this.tabStops_[i] < column) {
1350 this.setCursorColumn(this.tabStops_[i]);
1351 return;
1352 }
1353 }
1354
1355 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001356};
1357
rgindac9bc5502012-01-18 11:48:44 -08001358/**
1359 * Set a tab stop at the given column.
1360 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001361 * @param {integer} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001362 */
1363hterm.Terminal.prototype.setTabStop = function(column) {
1364 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1365 if (this.tabStops_[i] == column)
1366 return;
1367
1368 if (this.tabStops_[i] < column) {
1369 this.tabStops_.splice(i + 1, 0, column);
1370 return;
1371 }
1372 }
1373
1374 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001375};
1376
rgindac9bc5502012-01-18 11:48:44 -08001377/**
1378 * Clear the tab stop at the current cursor position.
1379 *
1380 * No effect if there is no tab stop at the current cursor position.
1381 */
1382hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1383 var column = this.screen_.cursorPosition.column;
1384
1385 var i = this.tabStops_.indexOf(column);
1386 if (i == -1)
1387 return;
1388
1389 this.tabStops_.splice(i, 1);
1390};
1391
1392/**
1393 * Clear all tab stops.
1394 */
1395hterm.Terminal.prototype.clearAllTabStops = function() {
1396 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001397 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001398};
1399
1400/**
1401 * Set up the default tab stops, starting from a given column.
1402 *
1403 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001404 * from the specified column, or 0 if no column is provided. It also flags
1405 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001406 *
1407 * This does not clear the existing tab stops first, use clearAllTabStops
1408 * for that.
1409 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001410 * @param {integer} opt_start Optional starting zero based starting column, useful
rgindac9bc5502012-01-18 11:48:44 -08001411 * for filling out missing tab stops when the terminal is resized.
1412 */
1413hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1414 var start = opt_start || 0;
1415 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001416 // Round start up to a default tab stop.
1417 start = start - 1 - ((start - 1) % w) + w;
1418 for (var i = start; i < this.screenSize.width; i += w) {
1419 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001420 }
David Benjamin66e954d2012-05-05 21:08:12 -04001421
1422 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001423};
1424
rginda6d397402012-01-17 10:58:29 -08001425/**
rginda8ba33642011-12-14 12:31:31 -08001426 * Interpret a sequence of characters.
1427 *
1428 * Incomplete escape sequences are buffered until the next call.
1429 *
1430 * @param {string} str Sequence of characters to interpret or pass through.
1431 */
1432hterm.Terminal.prototype.interpret = function(str) {
rginda8ba33642011-12-14 12:31:31 -08001433 this.scheduleSyncCursorPosition_();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001434 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001435};
1436
1437/**
1438 * Take over the given DIV for use as the terminal display.
1439 *
1440 * @param {HTMLDivElement} div The div to use as the terminal display.
1441 */
1442hterm.Terminal.prototype.decorate = function(div) {
Mike Frysinger5768a9d2017-12-26 12:57:44 -05001443 const charset = div.ownerDocument.characterSet.toLowerCase();
1444 if (charset != 'utf-8') {
1445 console.warn(`Document encoding should be set to utf-8, not "${charset}";` +
1446 ` Add <meta charset='utf-8'/> to your HTML <head> to fix.`);
1447 }
1448
rginda87b86462011-12-14 13:48:03 -08001449 this.div_ = div;
1450
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001451 this.accessibilityReader_ = new hterm.AccessibilityReader(div);
1452
Adrián Pérez-Orozco394e64f2018-12-17 17:20:16 -08001453 this.scrollPort_.decorate(div, () => this.setupScrollPort_());
1454};
1455
1456/**
1457 * Initialisation of ScrollPort properties which need to be set after its DOM
1458 * has been initialised.
1459 * @private
1460 */
1461hterm.Terminal.prototype.setupScrollPort_ = function() {
rginda30f20f62012-04-05 16:36:19 -07001462 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001463 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1464 this.scrollPort_.setBackgroundPosition(
1465 this.prefs_.get('background-position'));
Mike Frysinger08bad432017-04-24 00:50:54 -04001466 this.scrollPort_.setUserCssUrl(this.prefs_.get('user-css'));
1467 this.scrollPort_.setUserCssText(this.prefs_.get('user-css-text'));
Raymes Khoury177aec72018-06-26 10:58:53 +10001468 this.scrollPort_.setAccessibilityReader(this.accessibilityReader_);
rginda30f20f62012-04-05 16:36:19 -07001469
rginda0918b652012-04-04 11:26:24 -07001470 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001471
rginda9f5222b2012-03-05 11:53:28 -08001472 this.setFontSize(this.prefs_.get('font-size'));
1473 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001474
David Reveman8f552492012-03-28 12:18:41 -04001475 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001476 this.setScrollWheelMoveMultipler(
1477 this.prefs_.get('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001478
rginda8ba33642011-12-14 12:31:31 -08001479 this.document_ = this.scrollPort_.getDocument();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001480 this.accessibilityReader_.decorate(this.document_);
rginda8ba33642011-12-14 12:31:31 -08001481
Evan Jones5f9df812016-12-06 09:38:58 -05001482 this.document_.body.oncontextmenu = function() { return false; };
Mike Frysingercc114512017-09-11 21:39:17 -04001483 this.contextMenu.setDocument(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07001484
1485 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001486 var screenNode = this.scrollPort_.getScreenNode();
1487 screenNode.addEventListener('mousedown', onMouse);
1488 screenNode.addEventListener('mouseup', onMouse);
1489 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001490 this.scrollPort_.onScrollWheel = onMouse;
1491
Mike Frysinger02ded6d2018-06-21 14:25:20 -04001492 screenNode.addEventListener('keydown', this.onKeyboardActivity_.bind(this));
1493
Toni Barzic0bfa8922013-11-22 11:18:35 -08001494 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001495 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001496 // Listen for mousedown events on the screenNode as in FF the focus
1497 // events don't bubble.
1498 screenNode.addEventListener('mousedown', function() {
1499 setTimeout(this.onFocusChange_.bind(this, true));
1500 }.bind(this));
1501
Toni Barzic0bfa8922013-11-22 11:18:35 -08001502 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001503 'blur', this.onFocusChange_.bind(this, false));
1504
1505 var style = this.document_.createElement('style');
1506 style.textContent =
1507 ('.cursor-node[focus="false"] {' +
1508 ' box-sizing: border-box;' +
1509 ' background-color: transparent !important;' +
1510 ' border-width: 2px;' +
1511 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001512 '}' +
Mike Frysingercc114512017-09-11 21:39:17 -04001513 'menu {' +
1514 ' margin: 0;' +
1515 ' padding: 0;' +
1516 ' cursor: var(--hterm-mouse-cursor-pointer);' +
1517 '}' +
1518 'menuitem {' +
1519 ' white-space: nowrap;' +
1520 ' border-bottom: 1px dashed;' +
1521 ' display: block;' +
1522 ' padding: 0.3em 0.3em 0 0.3em;' +
1523 '}' +
1524 'menuitem.separator {' +
1525 ' border-bottom: none;' +
1526 ' height: 0.5em;' +
1527 ' padding: 0;' +
1528 '}' +
1529 'menuitem:hover {' +
1530 ' color: var(--hterm-cursor-color);' +
1531 '}' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001532 '.wc-node {' +
1533 ' display: inline-block;' +
1534 ' text-align: center;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001535 ' width: calc(var(--hterm-charsize-width) * 2);' +
Mike Frysinger4036f6e2017-05-31 14:02:55 -04001536 ' line-height: var(--hterm-charsize-height);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001537 '}' +
1538 ':root {' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001539 ' --hterm-charsize-width: ' + this.scrollPort_.characterSize.width + 'px;' +
1540 ' --hterm-charsize-height: ' + this.scrollPort_.characterSize.height + 'px;' +
Mike Frysingera27c0502017-08-23 22:37:10 -04001541 // Default position hides the cursor for when the window is initializing.
1542 ' --hterm-cursor-offset-col: -1;' +
1543 ' --hterm-cursor-offset-row: -1;' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001544 ' --hterm-blink-node-duration: 0.7s;' +
Mike Frysinger67f58f82018-11-22 13:38:22 -05001545 ' --hterm-mouse-cursor-default: default;' +
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001546 ' --hterm-mouse-cursor-text: text;' +
Mike Frysinger67f58f82018-11-22 13:38:22 -05001547 ' --hterm-mouse-cursor-pointer: pointer;' +
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001548 ' --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001549 '}' +
Mike Frysingerd3907202017-10-23 01:23:50 -04001550 '.uri-node:hover {' +
1551 ' text-decoration: underline;' +
Mike Frysinger67f58f82018-11-22 13:38:22 -05001552 ' cursor: var(--hterm-mouse-cursor-pointer);' +
Mike Frysingerd3907202017-10-23 01:23:50 -04001553 '}' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001554 '@keyframes blink {' +
1555 ' from { opacity: 1.0; }' +
1556 ' to { opacity: 0.0; }' +
1557 '}' +
1558 '.blink-node {' +
1559 ' animation-name: blink;' +
1560 ' animation-duration: var(--hterm-blink-node-duration);' +
1561 ' animation-iteration-count: infinite;' +
1562 ' animation-timing-function: ease-in-out;' +
1563 ' animation-direction: alternate;' +
rginda8e92a692012-05-20 19:37:20 -07001564 '}');
Mike Frysingerb74a6472018-06-22 13:37:08 -04001565 // Insert this stock style as the first node so that any user styles will
1566 // override w/out having to use !important everywhere. The rules above mix
1567 // runtime variables with default ones designed to be overridden by the user,
1568 // but we can wait for a concrete case from the users to determine the best
1569 // way to split the sheet up to before & after the user-css settings.
1570 this.document_.head.insertBefore(style, this.document_.head.firstChild);
rginda8e92a692012-05-20 19:37:20 -07001571
rginda8ba33642011-12-14 12:31:31 -08001572 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001573 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001574 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001575 this.cursorNode_.style.cssText =
1576 ('position: absolute;' +
Mike Frysinger44c32202017-08-05 01:13:09 -04001577 'left: calc(var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));' +
1578 'top: calc(var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));' +
Mike Frysingerd60f4c22018-03-15 21:25:30 -07001579 'display: ' + (this.options_.cursorVisible ? '' : 'none') + ';' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001580 'width: var(--hterm-charsize-width);' +
1581 'height: var(--hterm-charsize-height);' +
Mike Frysinger2fd079a2018-09-02 01:46:12 -04001582 'background-color: var(--hterm-cursor-color);' +
1583 'border-color: var(--hterm-cursor-color);' +
Rob Spies06533ba2014-04-24 11:20:37 -07001584 '-webkit-transition: opacity, background-color 100ms linear;' +
1585 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001586
Mike Frysingerf02a2cb2017-12-21 00:34:03 -05001587 this.setCursorColor();
Robert Gindafb1be6a2013-12-11 11:56:22 -08001588 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1589 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001590
rginda8ba33642011-12-14 12:31:31 -08001591 this.document_.body.appendChild(this.cursorNode_);
1592
rgindad5613292012-06-19 15:40:37 -07001593 // When 'enableMouseDragScroll' is off we reposition this element directly
1594 // under the mouse cursor after a click. This makes Chrome associate
1595 // subsequent mousemove events with the scroll-blocker. Since the
1596 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1597 // events do not cause the scrollport to scroll.
1598 //
1599 // It's a hack, but it's the cleanest way I could find.
1600 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001601 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
Raymes Khoury6dce2f82018-04-12 15:38:58 +10001602 this.scrollBlockerNode_.setAttribute('aria-hidden', 'true');
rgindad5613292012-06-19 15:40:37 -07001603 this.scrollBlockerNode_.style.cssText =
1604 ('position: absolute;' +
1605 'top: -99px;' +
1606 'display: block;' +
1607 'width: 10px;' +
1608 'height: 10px;');
1609 this.document_.body.appendChild(this.scrollBlockerNode_);
1610
rgindad5613292012-06-19 15:40:37 -07001611 this.scrollPort_.onScrollWheel = onMouse;
1612 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1613 ].forEach(function(event) {
1614 this.scrollBlockerNode_.addEventListener(event, onMouse);
1615 this.cursorNode_.addEventListener(event, onMouse);
1616 this.document_.addEventListener(event, onMouse);
1617 }.bind(this));
1618
1619 this.cursorNode_.addEventListener('mousedown', function() {
1620 setTimeout(this.focus.bind(this));
1621 }.bind(this));
1622
rginda8ba33642011-12-14 12:31:31 -08001623 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001624
rginda87b86462011-12-14 13:48:03 -08001625 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001626 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001627};
1628
rginda0918b652012-04-04 11:26:24 -07001629/**
1630 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001631 *
1632 * @return {HTMLDocument}
rginda0918b652012-04-04 11:26:24 -07001633 */
rginda87b86462011-12-14 13:48:03 -08001634hterm.Terminal.prototype.getDocument = function() {
1635 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001636};
1637
1638/**
rginda0918b652012-04-04 11:26:24 -07001639 * Focus the terminal.
1640 */
1641hterm.Terminal.prototype.focus = function() {
1642 this.scrollPort_.focus();
1643};
1644
1645/**
rginda8ba33642011-12-14 12:31:31 -08001646 * Return the HTML Element for a given row index.
1647 *
1648 * This is a method from the RowProvider interface. The ScrollPort uses
1649 * it to fetch rows on demand as they are scrolled into view.
1650 *
1651 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1652 * pairs to conserve memory.
1653 *
1654 * @param {integer} index The zero-based row index, measured relative to the
1655 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001656 * largest indices.
rginda8ba33642011-12-14 12:31:31 -08001657 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1658 */
1659hterm.Terminal.prototype.getRowNode = function(index) {
1660 if (index < this.scrollbackRows_.length)
1661 return this.scrollbackRows_[index];
1662
1663 var screenIndex = index - this.scrollbackRows_.length;
1664 return this.screen_.rowsArray[screenIndex];
1665};
1666
1667/**
1668 * Return the text content for a given range of rows.
1669 *
1670 * This is a method from the RowProvider interface. The ScrollPort uses
1671 * it to fetch text content on demand when the user attempts to copy their
1672 * selection to the clipboard.
1673 *
1674 * @param {integer} start The zero-based row index to start from, measured
1675 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001676 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001677 * @param {integer} end The zero-based row index to end on, measured
1678 * relative to the start of the scrollback buffer.
1679 * @return {string} A single string containing the text value of the range of
1680 * rows. Lines will be newline delimited, with no trailing newline.
1681 */
1682hterm.Terminal.prototype.getRowsText = function(start, end) {
1683 var ary = [];
1684 for (var i = start; i < end; i++) {
1685 var node = this.getRowNode(i);
1686 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001687 if (i < end - 1 && !node.getAttribute('line-overflow'))
1688 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001689 }
1690
rgindaa09e7332012-08-17 12:49:51 -07001691 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001692};
1693
1694/**
1695 * Return the text content for a given row.
1696 *
1697 * This is a method from the RowProvider interface. The ScrollPort uses
1698 * it to fetch text content on demand when the user attempts to copy their
1699 * selection to the clipboard.
1700 *
1701 * @param {integer} index The zero-based row index to return, measured
1702 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001703 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001704 * @return {string} A string containing the text value of the selected row.
1705 */
1706hterm.Terminal.prototype.getRowText = function(index) {
1707 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001708 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001709};
1710
1711/**
1712 * Return the total number of rows in the addressable screen and in the
1713 * scrollback buffer of this terminal.
1714 *
1715 * This is a method from the RowProvider interface. The ScrollPort uses
1716 * it to compute the size of the scrollbar.
1717 *
1718 * @return {integer} The number of rows in this terminal.
1719 */
1720hterm.Terminal.prototype.getRowCount = function() {
1721 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1722};
1723
1724/**
1725 * Create DOM nodes for new rows and append them to the end of the terminal.
1726 *
1727 * This is the only correct way to add a new DOM node for a row. Notice that
1728 * the new row is appended to the bottom of the list of rows, and does not
1729 * require renumbering (of the rowIndex property) of previous rows.
1730 *
1731 * If you think you want a new blank row somewhere in the middle of the
1732 * terminal, look into moveRows_().
1733 *
1734 * This method does not pay attention to vtScrollTop/Bottom, since you should
1735 * be using moveRows() in cases where they would matter.
1736 *
1737 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001738 *
1739 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001740 */
1741hterm.Terminal.prototype.appendRows_ = function(count) {
1742 var cursorRow = this.screen_.rowsArray.length;
1743 var offset = this.scrollbackRows_.length + cursorRow;
1744 for (var i = 0; i < count; i++) {
1745 var row = this.document_.createElement('x-row');
1746 row.appendChild(this.document_.createTextNode(''));
1747 row.rowIndex = offset + i;
1748 this.screen_.pushRow(row);
1749 }
1750
1751 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1752 if (extraRows > 0) {
1753 var ary = this.screen_.shiftRows(extraRows);
1754 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001755 if (this.scrollPort_.isScrolledEnd)
1756 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001757 }
1758
1759 if (cursorRow >= this.screen_.rowsArray.length)
1760 cursorRow = this.screen_.rowsArray.length - 1;
1761
rginda87b86462011-12-14 13:48:03 -08001762 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001763};
1764
1765/**
1766 * Relocate rows from one part of the addressable screen to another.
1767 *
1768 * This is used to recycle rows during VT scrolls (those which are driven
1769 * by VT commands, rather than by the user manipulating the scrollbar.)
1770 *
1771 * In this case, the blank lines scrolled into the scroll region are made of
1772 * the nodes we scrolled off. These have their rowIndex properties carefully
1773 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001774 *
1775 * @param {number} fromIndex The start index.
1776 * @param {number} count The number of rows to move.
1777 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001778 */
1779hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1780 var ary = this.screen_.removeRows(fromIndex, count);
1781 this.screen_.insertRows(toIndex, ary);
1782
1783 var start, end;
1784 if (fromIndex < toIndex) {
1785 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001786 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001787 } else {
1788 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001789 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001790 }
1791
1792 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001793 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001794};
1795
1796/**
1797 * Renumber the rowIndex property of the given range of rows.
1798 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001799 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08001800 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001801 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001802 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05001803 *
1804 * @param {number} start The start index.
1805 * @param {number} end The end index.
1806 * @param {hterm.Screen} opt_screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08001807 */
Robert Ginda40932892012-12-10 17:26:40 -08001808hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1809 var screen = opt_screen || this.screen_;
1810
rginda8ba33642011-12-14 12:31:31 -08001811 var offset = this.scrollbackRows_.length;
1812 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001813 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001814 }
1815};
1816
1817/**
1818 * Print a string to the terminal.
1819 *
1820 * This respects the current insert and wraparound modes. It will add new lines
1821 * to the end of the terminal, scrolling off the top into the scrollback buffer
1822 * if necessary.
1823 *
1824 * The string is *not* parsed for escape codes. Use the interpret() method if
1825 * that's what you're after.
1826 *
1827 * @param{string} str The string to print.
1828 */
1829hterm.Terminal.prototype.print = function(str) {
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001830 this.scheduleSyncCursorPosition_();
1831
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001832 // Basic accessibility output for the screen reader.
Raymes Khoury177aec72018-06-26 10:58:53 +10001833 this.accessibilityReader_.announce(str);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001834
rgindaa9abdd82012-08-06 18:05:09 -07001835 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001836
Ricky Liang48f05cb2013-12-31 23:35:29 +08001837 var strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04001838 // Fun edge case: If the string only contains zero width codepoints (like
1839 // combining characters), we make sure to iterate at least once below.
1840 if (strWidth == 0 && str)
1841 strWidth = 1;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001842
1843 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001844 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1845 this.screen_.commitLineOverflow();
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001846 this.newLine(true);
rgindaa09e7332012-08-17 12:49:51 -07001847 }
rgindaa19afe22012-01-25 15:40:22 -08001848
Ricky Liang48f05cb2013-12-31 23:35:29 +08001849 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001850 var didOverflow = false;
1851 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001852
rgindaa9abdd82012-08-06 18:05:09 -07001853 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1854 didOverflow = true;
1855 count = this.screenSize.width - this.screen_.cursorPosition.column;
1856 }
rgindaa19afe22012-01-25 15:40:22 -08001857
rgindaa9abdd82012-08-06 18:05:09 -07001858 if (didOverflow && !this.options_.wraparound) {
1859 // If the string overflowed the line but wraparound is off, then the
1860 // last printed character should be the last of the string.
1861 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001862 substr = lib.wc.substr(str, startOffset, count - 1) +
1863 lib.wc.substr(str, strWidth - 1);
1864 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001865 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001866 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001867 }
rgindaa19afe22012-01-25 15:40:22 -08001868
Ricky Liang48f05cb2013-12-31 23:35:29 +08001869 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1870 for (var i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001871 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
1872 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001873
1874 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001875 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001876 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001877 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001878 }
1879 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001880 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07001881 }
1882
1883 this.screen_.maybeClipCurrentRow();
1884 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001885 }
rginda8ba33642011-12-14 12:31:31 -08001886
rginda9f5222b2012-03-05 11:53:28 -08001887 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001888 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001889};
1890
1891/**
rginda87b86462011-12-14 13:48:03 -08001892 * Set the VT scroll region.
1893 *
rginda87b86462011-12-14 13:48:03 -08001894 * This also resets the cursor position to the absolute (0, 0) position, since
1895 * that's what xterm appears to do.
1896 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001897 * Setting the scroll region to the full height of the terminal will clear
1898 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1899 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1900 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1901 * continue to work as most users would expect.
1902 *
rginda87b86462011-12-14 13:48:03 -08001903 * @param {integer} scrollTop The zero-based top of the scroll region.
1904 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1905 * inclusive.
1906 */
1907hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001908 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001909 this.vtScrollTop_ = null;
1910 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001911 } else {
1912 this.vtScrollTop_ = scrollTop;
1913 this.vtScrollBottom_ = scrollBottom;
1914 }
rginda87b86462011-12-14 13:48:03 -08001915};
1916
1917/**
rginda8ba33642011-12-14 12:31:31 -08001918 * Return the top row index according to the VT.
1919 *
1920 * This will return 0 unless the terminal has been told to restrict scrolling
1921 * to some lower row. It is used for some VT cursor positioning and scrolling
1922 * commands.
1923 *
1924 * @return {integer} The topmost row in the terminal's scroll region.
1925 */
1926hterm.Terminal.prototype.getVTScrollTop = function() {
1927 if (this.vtScrollTop_ != null)
1928 return this.vtScrollTop_;
1929
1930 return 0;
rginda87b86462011-12-14 13:48:03 -08001931};
rginda8ba33642011-12-14 12:31:31 -08001932
1933/**
1934 * Return the bottom row index according to the VT.
1935 *
1936 * This will return the height of the terminal unless the it has been told to
1937 * restrict scrolling to some higher row. It is used for some VT cursor
1938 * positioning and scrolling commands.
1939 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001940 * @return {integer} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001941 */
1942hterm.Terminal.prototype.getVTScrollBottom = function() {
1943 if (this.vtScrollBottom_ != null)
1944 return this.vtScrollBottom_;
1945
rginda87b86462011-12-14 13:48:03 -08001946 return this.screenSize.height - 1;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001947};
rginda8ba33642011-12-14 12:31:31 -08001948
1949/**
1950 * Process a '\n' character.
1951 *
1952 * If the cursor is on the final row of the terminal this will append a new
1953 * blank row to the screen and scroll the topmost row into the scrollback
1954 * buffer.
1955 *
1956 * Otherwise, this moves the cursor to column zero of the next row.
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001957 *
1958 * @param {boolean=} dueToOverflow Whether the newline is due to wraparound of
1959 * the terminal.
rginda8ba33642011-12-14 12:31:31 -08001960 */
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001961hterm.Terminal.prototype.newLine = function(dueToOverflow = false) {
1962 if (!dueToOverflow)
1963 this.accessibilityReader_.newLine();
1964
Robert Ginda9937abc2013-07-25 16:09:23 -07001965 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1966 this.screen_.rowsArray.length - 1);
1967
1968 if (this.vtScrollBottom_ != null) {
1969 // A VT Scroll region is active, we never append new rows.
1970 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1971 // We're at the end of the VT Scroll Region, perform a VT scroll.
1972 this.vtScrollUp(1);
1973 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1974 } else if (cursorAtEndOfScreen) {
1975 // We're at the end of the screen, the only thing to do is put the
1976 // cursor to column 0.
1977 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1978 } else {
1979 // Anywhere else, advance the cursor row, and reset the column.
1980 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1981 }
1982 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001983 // We're at the end of the screen. Append a new row to the terminal,
1984 // shifting the top row into the scrollback.
1985 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001986 } else {
rginda87b86462011-12-14 13:48:03 -08001987 // Anywhere else in the screen just moves the cursor.
1988 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001989 }
1990};
1991
1992/**
1993 * Like newLine(), except maintain the cursor column.
1994 */
1995hterm.Terminal.prototype.lineFeed = function() {
1996 var column = this.screen_.cursorPosition.column;
1997 this.newLine();
1998 this.setCursorColumn(column);
1999};
2000
2001/**
rginda87b86462011-12-14 13:48:03 -08002002 * If autoCarriageReturn is set then newLine(), else lineFeed().
2003 */
2004hterm.Terminal.prototype.formFeed = function() {
2005 if (this.options_.autoCarriageReturn) {
2006 this.newLine();
2007 } else {
2008 this.lineFeed();
2009 }
2010};
2011
2012/**
2013 * Move the cursor up one row, possibly inserting a blank line.
2014 *
2015 * The cursor column is not changed.
2016 */
2017hterm.Terminal.prototype.reverseLineFeed = function() {
2018 var scrollTop = this.getVTScrollTop();
2019 var currentRow = this.screen_.cursorPosition.row;
2020
2021 if (currentRow == scrollTop) {
2022 this.insertLines(1);
2023 } else {
2024 this.setAbsoluteCursorRow(currentRow - 1);
2025 }
2026};
2027
2028/**
rginda8ba33642011-12-14 12:31:31 -08002029 * Replace all characters to the left of the current cursor with the space
2030 * character.
2031 *
2032 * TODO(rginda): This should probably *remove* the characters (not just replace
2033 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07002034 * position.
rginda8ba33642011-12-14 12:31:31 -08002035 */
2036hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08002037 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002038 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002039 const count = cursor.column + 1;
2040 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08002041 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002042};
2043
2044/**
David Benjamin684a9b72012-05-01 17:19:58 -04002045 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08002046 *
2047 * The cursor position is unchanged.
2048 *
Robert Gindaf2547f12012-10-25 20:36:21 -07002049 * If the current background color is not the default background color this
2050 * will insert spaces rather than delete. This is unfortunate because the
2051 * trailing space will affect text selection, but it's difficult to come up
2052 * with a way to style empty space that wouldn't trip up the hterm.Screen
2053 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07002054 *
2055 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
2056 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
2057 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05002058 *
2059 * @param {number} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08002060 */
2061hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07002062 if (this.screen_.cursorPosition.overflow)
2063 return;
2064
Robert Ginda7fd57082012-09-25 14:41:47 -07002065 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
2066 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07002067
2068 if (this.screen_.textAttributes.background ===
2069 this.screen_.textAttributes.DEFAULT_COLOR) {
2070 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08002071 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07002072 this.screen_.cursorPosition.column + count) {
2073 this.screen_.deleteChars(count);
2074 this.clearCursorOverflow();
2075 return;
2076 }
2077 }
2078
rginda87b86462011-12-14 13:48:03 -08002079 var cursor = this.saveCursor();
Mike Frysinger6380bed2017-08-24 18:46:39 -04002080 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08002081 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002082 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002083};
2084
2085/**
2086 * Erase the current line.
2087 *
2088 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002089 */
2090hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08002091 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002092 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08002093 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002094 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002095};
2096
2097/**
David Benjamina08d78f2012-05-05 00:28:49 -04002098 * Erase all characters from the start of the screen to the current cursor
2099 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002100 *
2101 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002102 */
2103hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08002104 var cursor = this.saveCursor();
2105
2106 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002107
David Benjamina08d78f2012-05-05 00:28:49 -04002108 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002109 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002110 this.screen_.clearCursorRow();
2111 }
2112
rginda87b86462011-12-14 13:48:03 -08002113 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002114 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002115};
2116
2117/**
2118 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002119 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002120 *
2121 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002122 */
2123hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08002124 var cursor = this.saveCursor();
2125
2126 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002127
David Benjamina08d78f2012-05-05 00:28:49 -04002128 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08002129 for (var i = cursor.row + 1; i <= bottom; i++) {
2130 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002131 this.screen_.clearCursorRow();
2132 }
2133
rginda87b86462011-12-14 13:48:03 -08002134 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002135 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002136};
2137
2138/**
2139 * Fill the terminal with a given character.
2140 *
2141 * This methods does not respect the VT scroll region.
2142 *
2143 * @param {string} ch The character to use for the fill.
2144 */
2145hterm.Terminal.prototype.fill = function(ch) {
2146 var cursor = this.saveCursor();
2147
2148 this.setAbsoluteCursorPosition(0, 0);
2149 for (var row = 0; row < this.screenSize.height; row++) {
2150 for (var col = 0; col < this.screenSize.width; col++) {
2151 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002152 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002153 }
2154 }
2155
2156 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002157};
2158
2159/**
rginda9ea433c2012-03-16 11:57:00 -07002160 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002161 *
rginda9ea433c2012-03-16 11:57:00 -07002162 * This does not respect the scroll region.
2163 *
2164 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2165 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002166 */
rginda9ea433c2012-03-16 11:57:00 -07002167hterm.Terminal.prototype.clearHome = function(opt_screen) {
2168 var screen = opt_screen || this.screen_;
2169 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002170
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002171 this.accessibilityReader_.clear();
2172
rginda11057d52012-04-25 12:29:56 -07002173 if (bottom == 0) {
2174 // Empty screen, nothing to do.
2175 return;
2176 }
2177
rgindae4d29232012-01-19 10:47:13 -08002178 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002179 screen.setCursorPosition(i, 0);
2180 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002181 }
2182
rginda9ea433c2012-03-16 11:57:00 -07002183 screen.setCursorPosition(0, 0);
2184};
2185
2186/**
2187 * Erase the entire display without changing the cursor position.
2188 *
2189 * The cursor position is unchanged. This does not respect the scroll
2190 * region.
2191 *
2192 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2193 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002194 */
2195hterm.Terminal.prototype.clear = function(opt_screen) {
2196 var screen = opt_screen || this.screen_;
2197 var cursor = screen.cursorPosition.clone();
2198 this.clearHome(screen);
2199 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002200};
2201
2202/**
2203 * VT command to insert lines at the current cursor row.
2204 *
2205 * This respects the current scroll region. Rows pushed off the bottom are
2206 * lost (they won't show up in the scrollback buffer).
2207 *
rginda8ba33642011-12-14 12:31:31 -08002208 * @param {integer} count The number of lines to insert.
2209 */
2210hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07002211 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002212
2213 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002214 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002215
Robert Ginda579186b2012-09-26 11:40:04 -07002216 // The moveCount is the number of rows we need to relocate to make room for
2217 // the new row(s). The count is the distance to move them.
2218 var moveCount = bottom - cursorRow - count + 1;
2219 if (moveCount)
2220 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08002221
Robert Ginda579186b2012-09-26 11:40:04 -07002222 for (var i = count - 1; i >= 0; i--) {
2223 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002224 this.screen_.clearCursorRow();
2225 }
rginda8ba33642011-12-14 12:31:31 -08002226};
2227
2228/**
2229 * VT command to delete lines at the current cursor row.
2230 *
2231 * New rows are added to the bottom of scroll region to take their place. New
2232 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002233 *
2234 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002235 */
2236hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08002237 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002238
rginda87b86462011-12-14 13:48:03 -08002239 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08002240 var bottom = this.getVTScrollBottom();
2241
rginda87b86462011-12-14 13:48:03 -08002242 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002243 count = Math.min(count, maxCount);
2244
rginda87b86462011-12-14 13:48:03 -08002245 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08002246 if (count != maxCount)
2247 this.moveRows_(top, count, moveStart);
2248
2249 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002250 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002251 this.screen_.clearCursorRow();
2252 }
2253
rginda87b86462011-12-14 13:48:03 -08002254 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002255 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002256};
2257
2258/**
2259 * Inserts the given number of spaces at the current cursor position.
2260 *
rginda87b86462011-12-14 13:48:03 -08002261 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002262 *
2263 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002264 */
2265hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08002266 var cursor = this.saveCursor();
2267
rgindacbbd7482012-06-13 15:06:16 -07002268 var ws = lib.f.getWhitespace(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002269 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002270 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002271
2272 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002273 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002274};
2275
2276/**
2277 * Forward-delete the specified number of characters starting at the cursor
2278 * position.
2279 *
2280 * @param {integer} count The number of characters to delete.
2281 */
2282hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002283 var deleted = this.screen_.deleteChars(count);
2284 if (deleted && !this.screen_.textAttributes.isDefault()) {
2285 var cursor = this.saveCursor();
2286 this.setCursorColumn(this.screenSize.width - deleted);
2287 this.screen_.insertString(lib.f.getWhitespace(deleted));
2288 this.restoreCursor(cursor);
2289 }
2290
David Benjamin54e8bf62012-06-01 22:31:40 -04002291 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002292};
2293
2294/**
2295 * Shift rows in the scroll region upwards by a given number of lines.
2296 *
2297 * New rows are inserted at the bottom of the scroll region to fill the
2298 * vacated rows. The new rows not filled out with the current text attributes.
2299 *
2300 * This function does not affect the scrollback rows at all. Rows shifted
2301 * off the top are lost.
2302 *
rginda87b86462011-12-14 13:48:03 -08002303 * The cursor position is not altered.
2304 *
rginda8ba33642011-12-14 12:31:31 -08002305 * @param {integer} count The number of rows to scroll.
2306 */
2307hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002308 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002309
rginda87b86462011-12-14 13:48:03 -08002310 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002311 this.deleteLines(count);
2312
rginda87b86462011-12-14 13:48:03 -08002313 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002314};
2315
2316/**
2317 * Shift rows below the cursor down by a given number of lines.
2318 *
2319 * This function respects the current scroll region.
2320 *
2321 * New rows are inserted at the top of the scroll region to fill the
2322 * vacated rows. The new rows not filled out with the current text attributes.
2323 *
2324 * This function does not affect the scrollback rows at all. Rows shifted
2325 * off the bottom are lost.
2326 *
2327 * @param {integer} count The number of rows to scroll.
2328 */
2329hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08002330 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002331
rginda87b86462011-12-14 13:48:03 -08002332 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08002333 this.insertLines(opt_count);
2334
rginda87b86462011-12-14 13:48:03 -08002335 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002336};
2337
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002338/**
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002339 * Enable accessibility-friendly features that have a performance impact.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002340 *
2341 * This will generate additional DOM nodes in an aria-live region that will
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002342 * cause Assitive Technology to announce the output of the terminal. It also
2343 * enables other features that aid assistive technology. All the features gated
2344 * behind this flag have a performance impact on the terminal which is why they
2345 * are made optional.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002346 *
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002347 * @param {boolean} enabled Whether to enable accessibility-friendly features.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002348 */
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002349hterm.Terminal.prototype.setAccessibilityEnabled = function(enabled) {
Raymes Khoury177aec72018-06-26 10:58:53 +10002350 this.accessibilityReader_.setAccessibilityEnabled(enabled);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002351};
rginda87b86462011-12-14 13:48:03 -08002352
rginda8ba33642011-12-14 12:31:31 -08002353/**
2354 * Set the cursor position.
2355 *
2356 * The cursor row is relative to the scroll region if the terminal has
2357 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2358 *
2359 * @param {integer} row The new zero-based cursor row.
2360 * @param {integer} row The new zero-based cursor column.
2361 */
2362hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2363 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002364 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002365 } else {
rginda87b86462011-12-14 13:48:03 -08002366 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002367 }
rginda87b86462011-12-14 13:48:03 -08002368};
rginda8ba33642011-12-14 12:31:31 -08002369
Evan Jones2600d4f2016-12-06 09:29:36 -05002370/**
2371 * Move the cursor relative to its current position.
2372 *
2373 * @param {number} row
2374 * @param {number} column
2375 */
rginda87b86462011-12-14 13:48:03 -08002376hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2377 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002378 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2379 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002380 this.screen_.setCursorPosition(row, column);
2381};
2382
Evan Jones2600d4f2016-12-06 09:29:36 -05002383/**
2384 * Move the cursor to the specified position.
2385 *
2386 * @param {number} row
2387 * @param {number} column
2388 */
rginda87b86462011-12-14 13:48:03 -08002389hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002390 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2391 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002392 this.screen_.setCursorPosition(row, column);
2393};
2394
2395/**
2396 * Set the cursor column.
2397 *
2398 * @param {integer} column The new zero-based cursor column.
2399 */
2400hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002401 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002402};
2403
2404/**
2405 * Return the cursor column.
2406 *
2407 * @return {integer} The zero-based cursor column.
2408 */
2409hterm.Terminal.prototype.getCursorColumn = function() {
2410 return this.screen_.cursorPosition.column;
2411};
2412
2413/**
2414 * Set the cursor row.
2415 *
2416 * The cursor row is relative to the scroll region if the terminal has
2417 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2418 *
2419 * @param {integer} row The new cursor row.
2420 */
rginda87b86462011-12-14 13:48:03 -08002421hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2422 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002423};
2424
2425/**
2426 * Return the cursor row.
2427 *
2428 * @return {integer} The zero-based cursor row.
2429 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002430hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002431 return this.screen_.cursorPosition.row;
2432};
2433
2434/**
2435 * Request that the ScrollPort redraw itself soon.
2436 *
2437 * The redraw will happen asynchronously, soon after the call stack winds down.
2438 * Multiple calls will be coalesced into a single redraw.
2439 */
2440hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002441 if (this.timeouts_.redraw)
2442 return;
rginda8ba33642011-12-14 12:31:31 -08002443
2444 var self = this;
rginda87b86462011-12-14 13:48:03 -08002445 this.timeouts_.redraw = setTimeout(function() {
2446 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002447 self.scrollPort_.redraw_();
2448 }, 0);
2449};
2450
2451/**
2452 * Request that the ScrollPort be scrolled to the bottom.
2453 *
2454 * The scroll will happen asynchronously, soon after the call stack winds down.
2455 * Multiple calls will be coalesced into a single scroll.
2456 *
2457 * This affects the scrollbar position of the ScrollPort, and has nothing to
2458 * do with the VT scroll commands.
2459 */
2460hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2461 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002462 return;
rginda8ba33642011-12-14 12:31:31 -08002463
2464 var self = this;
2465 this.timeouts_.scrollDown = setTimeout(function() {
2466 delete self.timeouts_.scrollDown;
2467 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2468 }, 10);
2469};
2470
2471/**
2472 * Move the cursor up a specified number of rows.
2473 *
2474 * @param {integer} count The number of rows to move the cursor.
2475 */
2476hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002477 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002478};
2479
2480/**
2481 * Move the cursor down a specified number of rows.
2482 *
2483 * @param {integer} count The number of rows to move the cursor.
2484 */
2485hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002486 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002487 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2488 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2489 this.screenSize.height - 1);
2490
rgindacbbd7482012-06-13 15:06:16 -07002491 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002492 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002493 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002494};
2495
2496/**
2497 * Move the cursor left a specified number of columns.
2498 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002499 * If reverse wraparound mode is enabled and the previous row wrapped into
2500 * the current row then we back up through the wraparound as well.
2501 *
rginda8ba33642011-12-14 12:31:31 -08002502 * @param {integer} count The number of columns to move the cursor.
2503 */
2504hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002505 count = count || 1;
2506
2507 if (count < 1)
2508 return;
2509
2510 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002511 if (this.options_.reverseWraparound) {
2512 if (this.screen_.cursorPosition.overflow) {
2513 // If this cursor is in the right margin, consume one count to get it
2514 // back to the last column. This only applies when we're in reverse
2515 // wraparound mode.
2516 count--;
2517 this.clearCursorOverflow();
2518
2519 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002520 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002521 }
2522
Robert Gindabfb32622014-07-17 13:20:27 -07002523 var newRow = this.screen_.cursorPosition.row;
2524 var newColumn = currentColumn - count;
2525 if (newColumn < 0) {
2526 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2527 if (newRow < 0) {
2528 // xterm also wraps from row 0 to the last row.
2529 newRow = this.screenSize.height + newRow % this.screenSize.height;
2530 }
2531 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2532 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002533
Robert Gindabfb32622014-07-17 13:20:27 -07002534 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2535
2536 } else {
2537 var newColumn = Math.max(currentColumn - count, 0);
2538 this.setCursorColumn(newColumn);
2539 }
rginda8ba33642011-12-14 12:31:31 -08002540};
2541
2542/**
2543 * Move the cursor right a specified number of columns.
2544 *
2545 * @param {integer} count The number of columns to move the cursor.
2546 */
2547hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002548 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002549
2550 if (count < 1)
2551 return;
2552
rgindacbbd7482012-06-13 15:06:16 -07002553 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002554 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002555 this.setCursorColumn(column);
2556};
2557
2558/**
2559 * Reverse the foreground and background colors of the terminal.
2560 *
2561 * This only affects text that was drawn with no attributes.
2562 *
2563 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2564 * been drawn with attributes that happen to coincide with the default
2565 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002566 *
2567 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002568 */
2569hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002570 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002571 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002572 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2573 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002574 } else {
rginda9f5222b2012-03-05 11:53:28 -08002575 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2576 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002577 }
2578};
2579
2580/**
rginda87b86462011-12-14 13:48:03 -08002581 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002582 *
2583 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002584 */
2585hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002586 this.cursorNode_.style.backgroundColor =
2587 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002588
2589 var self = this;
2590 setTimeout(function() {
Matheus Fernandes2d733082017-09-11 06:43:01 -04002591 self.restyleCursor_();
rginda6d397402012-01-17 10:58:29 -08002592 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002593
Michael Kelly485ecd12014-06-09 11:41:56 -04002594 // bellSquelchTimeout_ affects both audio and notification bells.
2595 if (this.bellSquelchTimeout_)
2596 return;
2597
Robert Ginda92e18102013-03-14 13:56:37 -07002598 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002599 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002600 this.bellSequelchTimeout_ = setTimeout(function() {
2601 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002602 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002603 } else {
2604 delete this.bellSquelchTimeout_;
2605 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002606
2607 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingera5fb83c2017-06-22 14:48:35 -07002608 var n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002609 this.bellNotificationList_.push(n);
2610 // TODO: Should we try to raise the window here?
2611 n.onclick = function() { self.closeBellNotifications_(); };
2612 }
rginda87b86462011-12-14 13:48:03 -08002613};
2614
2615/**
rginda8ba33642011-12-14 12:31:31 -08002616 * Set the origin mode bit.
2617 *
2618 * If origin mode is on, certain VT cursor and scrolling commands measure their
2619 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2620 * to the top of the addressable screen.
2621 *
2622 * Defaults to off.
2623 *
2624 * @param {boolean} state True to set origin mode, false to unset.
2625 */
2626hterm.Terminal.prototype.setOriginMode = function(state) {
2627 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002628 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002629};
2630
2631/**
2632 * Set the insert mode bit.
2633 *
2634 * If insert mode is on, existing text beyond the cursor position will be
2635 * shifted right to make room for new text. Otherwise, new text overwrites
2636 * any existing text.
2637 *
2638 * Defaults to off.
2639 *
2640 * @param {boolean} state True to set insert mode, false to unset.
2641 */
2642hterm.Terminal.prototype.setInsertMode = function(state) {
2643 this.options_.insertMode = state;
2644};
2645
2646/**
rginda87b86462011-12-14 13:48:03 -08002647 * Set the auto carriage return bit.
2648 *
2649 * If auto carriage return is on then a formfeed character is interpreted
2650 * as a newline, otherwise it's the same as a linefeed. The difference boils
2651 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002652 *
2653 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002654 */
2655hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2656 this.options_.autoCarriageReturn = state;
2657};
2658
2659/**
rginda8ba33642011-12-14 12:31:31 -08002660 * Set the wraparound mode bit.
2661 *
2662 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2663 * to the start of the following row. Otherwise, the cursor is clamped to the
2664 * end of the screen and attempts to write past it are ignored.
2665 *
2666 * Defaults to on.
2667 *
2668 * @param {boolean} state True to set wraparound mode, false to unset.
2669 */
2670hterm.Terminal.prototype.setWraparound = function(state) {
2671 this.options_.wraparound = state;
2672};
2673
2674/**
2675 * Set the reverse-wraparound mode bit.
2676 *
2677 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2678 * to the end of the previous row. Otherwise, the cursor is clamped to column
2679 * 0.
2680 *
2681 * Defaults to off.
2682 *
2683 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2684 */
2685hterm.Terminal.prototype.setReverseWraparound = function(state) {
2686 this.options_.reverseWraparound = state;
2687};
2688
2689/**
2690 * Selects between the primary and alternate screens.
2691 *
2692 * If alternate mode is on, the alternate screen is active. Otherwise the
2693 * primary screen is active.
2694 *
2695 * Swapping screens has no effect on the scrollback buffer.
2696 *
2697 * Each screen maintains its own cursor position.
2698 *
2699 * Defaults to off.
2700 *
2701 * @param {boolean} state True to set alternate mode, false to unset.
2702 */
2703hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002704 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002705 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2706
rginda35c456b2012-02-09 17:29:05 -08002707 if (this.screen_.rowsArray.length &&
2708 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2709 // If the screen changed sizes while we were away, our rowIndexes may
2710 // be incorrect.
2711 var offset = this.scrollbackRows_.length;
2712 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002713 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002714 ary[i].rowIndex = offset + i;
2715 }
2716 }
rginda8ba33642011-12-14 12:31:31 -08002717
rginda35c456b2012-02-09 17:29:05 -08002718 this.realizeWidth_(this.screenSize.width);
2719 this.realizeHeight_(this.screenSize.height);
2720 this.scrollPort_.syncScrollHeight();
2721 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002722
rginda6d397402012-01-17 10:58:29 -08002723 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002724 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002725};
2726
2727/**
2728 * Set the cursor-blink mode bit.
2729 *
2730 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2731 * a visible cursor does not blink.
2732 *
2733 * You should make sure to turn blinking off if you're going to dispose of a
2734 * terminal, otherwise you'll leak a timeout.
2735 *
2736 * Defaults to on.
2737 *
2738 * @param {boolean} state True to set cursor-blink mode, false to unset.
2739 */
2740hterm.Terminal.prototype.setCursorBlink = function(state) {
2741 this.options_.cursorBlink = state;
2742
2743 if (!state && this.timeouts_.cursorBlink) {
2744 clearTimeout(this.timeouts_.cursorBlink);
2745 delete this.timeouts_.cursorBlink;
2746 }
2747
2748 if (this.options_.cursorVisible)
2749 this.setCursorVisible(true);
2750};
2751
2752/**
2753 * Set the cursor-visible mode bit.
2754 *
2755 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2756 *
2757 * Defaults to on.
2758 *
2759 * @param {boolean} state True to set cursor-visible mode, false to unset.
2760 */
2761hterm.Terminal.prototype.setCursorVisible = function(state) {
2762 this.options_.cursorVisible = state;
2763
2764 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002765 if (this.timeouts_.cursorBlink) {
2766 clearTimeout(this.timeouts_.cursorBlink);
2767 delete this.timeouts_.cursorBlink;
2768 }
rginda87b86462011-12-14 13:48:03 -08002769 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002770 return;
2771 }
2772
rginda87b86462011-12-14 13:48:03 -08002773 this.syncCursorPosition_();
2774
2775 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002776
2777 if (this.options_.cursorBlink) {
2778 if (this.timeouts_.cursorBlink)
2779 return;
2780
Robert Gindaea2183e2014-07-17 09:51:51 -07002781 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002782 } else {
2783 if (this.timeouts_.cursorBlink) {
2784 clearTimeout(this.timeouts_.cursorBlink);
2785 delete this.timeouts_.cursorBlink;
2786 }
2787 }
2788};
2789
2790/**
rginda87b86462011-12-14 13:48:03 -08002791 * Synchronizes the visible cursor and document selection with the current
2792 * cursor coordinates.
Raymes Khourye5d48982018-08-02 09:08:32 +10002793 *
2794 * @return {boolean} True if the cursor is onscreen and synced.
rginda8ba33642011-12-14 12:31:31 -08002795 */
2796hterm.Terminal.prototype.syncCursorPosition_ = function() {
2797 var topRowIndex = this.scrollPort_.getTopRowIndex();
2798 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2799 var cursorRowIndex = this.scrollbackRows_.length +
2800 this.screen_.cursorPosition.row;
2801
Raymes Khoury15697f42018-07-17 11:37:18 +10002802 let forceSyncSelection = false;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002803 if (this.accessibilityReader_.accessibilityEnabled) {
2804 // Report the new position of the cursor for accessibility purposes.
2805 const cursorColumnIndex = this.screen_.cursorPosition.column;
2806 const cursorLineText =
2807 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
Raymes Khoury15697f42018-07-17 11:37:18 +10002808 // This will force the selection to be sync'd to the cursor position if the
2809 // user has pressed a key. Generally we would only sync the cursor position
2810 // when selection is collapsed so that if the user has selected something
2811 // we don't clear the selection by moving the selection. However when a
2812 // screen reader is used, it's intuitive for entering a key to move the
2813 // selection to the cursor.
2814 forceSyncSelection = this.accessibilityReader_.hasUserGesture;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002815 this.accessibilityReader_.afterCursorChange(
2816 cursorLineText, cursorRowIndex, cursorColumnIndex);
2817 }
2818
rginda8ba33642011-12-14 12:31:31 -08002819 if (cursorRowIndex > bottomRowIndex) {
2820 // Cursor is scrolled off screen, move it outside of the visible area.
Mike Frysinger44c32202017-08-05 01:13:09 -04002821 this.setCssVar('cursor-offset-row', '-1');
Raymes Khourye5d48982018-08-02 09:08:32 +10002822 return false;
rginda8ba33642011-12-14 12:31:31 -08002823 }
2824
Robert Gindab837c052014-08-11 11:17:51 -07002825 if (this.options_.cursorVisible &&
2826 this.cursorNode_.style.display == 'none') {
2827 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2828 this.cursorNode_.style.display = '';
2829 }
2830
Mike Frysinger44c32202017-08-05 01:13:09 -04002831 // Position the cursor using CSS variable math. If we do the math in JS,
2832 // the float math will end up being more precise than the CSS which will
2833 // cause the cursor tracking to be off.
2834 this.setCssVar(
2835 'cursor-offset-row',
2836 `${cursorRowIndex - topRowIndex} + ` +
2837 `${this.scrollPort_.visibleRowTopMargin}px`);
2838 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08002839
2840 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04002841 '(' + this.screen_.cursorPosition.column +
2842 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08002843 ')');
2844
2845 // Update the caret for a11y purposes.
2846 var selection = this.document_.getSelection();
Raymes Khoury15697f42018-07-17 11:37:18 +10002847 if (selection && (selection.isCollapsed || forceSyncSelection)) {
rginda87b86462011-12-14 13:48:03 -08002848 this.screen_.syncSelectionCaret(selection);
Raymes Khoury15697f42018-07-17 11:37:18 +10002849 }
Raymes Khourye5d48982018-08-02 09:08:32 +10002850 return true;
rginda8ba33642011-12-14 12:31:31 -08002851};
2852
Robert Gindafb1be6a2013-12-11 11:56:22 -08002853/**
2854 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2855 * and character cell dimensions.
2856 */
Robert Ginda830583c2013-08-07 13:20:46 -07002857hterm.Terminal.prototype.restyleCursor_ = function() {
2858 var shape = this.cursorShape_;
2859
2860 if (this.cursorNode_.getAttribute('focus') == 'false') {
2861 // Always show a block cursor when unfocused.
2862 shape = hterm.Terminal.cursorShape.BLOCK;
2863 }
2864
2865 var style = this.cursorNode_.style;
2866
2867 switch (shape) {
2868 case hterm.Terminal.cursorShape.BEAM:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002869 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002870 style.backgroundColor = 'transparent';
2871 style.borderBottomStyle = null;
2872 style.borderLeftStyle = 'solid';
2873 break;
2874
2875 case hterm.Terminal.cursorShape.UNDERLINE:
2876 style.height = this.scrollPort_.characterSize.baseline + 'px';
2877 style.backgroundColor = 'transparent';
2878 style.borderBottomStyle = 'solid';
2879 // correct the size to put it exactly at the baseline
2880 style.borderLeftStyle = null;
2881 break;
2882
2883 default:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002884 style.height = 'var(--hterm-charsize-height)';
Mike Frysinger2fd079a2018-09-02 01:46:12 -04002885 style.backgroundColor = 'var(--hterm-cursor-color)';
Robert Ginda830583c2013-08-07 13:20:46 -07002886 style.borderBottomStyle = null;
2887 style.borderLeftStyle = null;
2888 break;
2889 }
2890};
2891
rginda8ba33642011-12-14 12:31:31 -08002892/**
2893 * Synchronizes the visible cursor with the current cursor coordinates.
2894 *
2895 * The sync will happen asynchronously, soon after the call stack winds down.
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002896 * Multiple calls will be coalesced into a single sync. This should be called
2897 * prior to the cursor actually changing position.
rginda8ba33642011-12-14 12:31:31 -08002898 */
2899hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2900 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002901 return;
rginda8ba33642011-12-14 12:31:31 -08002902
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002903 if (this.accessibilityReader_.accessibilityEnabled) {
2904 // Report the previous position of the cursor for accessibility purposes.
2905 const cursorRowIndex = this.scrollbackRows_.length +
2906 this.screen_.cursorPosition.row;
2907 const cursorColumnIndex = this.screen_.cursorPosition.column;
2908 const cursorLineText =
2909 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
2910 this.accessibilityReader_.beforeCursorChange(
2911 cursorLineText, cursorRowIndex, cursorColumnIndex);
2912 }
2913
rginda8ba33642011-12-14 12:31:31 -08002914 var self = this;
2915 this.timeouts_.syncCursor = setTimeout(function() {
2916 self.syncCursorPosition_();
2917 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002918 }, 0);
2919};
2920
rgindacc2996c2012-02-24 14:59:31 -08002921/**
rgindaf522ce02012-04-17 17:49:17 -07002922 * Show or hide the zoom warning.
2923 *
2924 * The zoom warning is a message warning the user that their browser zoom must
2925 * be set to 100% in order for hterm to function properly.
2926 *
2927 * @param {boolean} state True to show the message, false to hide it.
2928 */
2929hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2930 if (!this.zoomWarningNode_) {
2931 if (!state)
2932 return;
2933
2934 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002935 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07002936 this.zoomWarningNode_.style.cssText = (
2937 'color: black;' +
2938 'background-color: #ff2222;' +
2939 'font-size: large;' +
2940 'border-radius: 8px;' +
2941 'opacity: 0.75;' +
2942 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2943 'top: 0.5em;' +
2944 'right: 1.2em;' +
2945 'position: absolute;' +
2946 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002947 '-webkit-user-select: none;' +
2948 '-moz-text-size-adjust: none;' +
2949 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05002950
2951 this.zoomWarningNode_.addEventListener('click', function(e) {
2952 this.parentNode.removeChild(this);
2953 });
rgindaf522ce02012-04-17 17:49:17 -07002954 }
2955
Robert Gindab4839c22013-02-28 16:52:10 -08002956 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2957 hterm.zoomWarningMessage,
2958 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2959
rgindaf522ce02012-04-17 17:49:17 -07002960 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2961
2962 if (state) {
2963 if (!this.zoomWarningNode_.parentNode)
2964 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2965 } else if (this.zoomWarningNode_.parentNode) {
2966 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2967 }
2968};
2969
2970/**
rgindacc2996c2012-02-24 14:59:31 -08002971 * Show the terminal overlay for a given amount of time.
2972 *
2973 * The terminal overlay appears in inverse video in a large font, centered
2974 * over the terminal. You should probably keep the overlay message brief,
2975 * since it's in a large font and you probably aren't going to check the size
2976 * of the terminal first.
2977 *
2978 * @param {string} msg The text (not HTML) message to display in the overlay.
2979 * @param {number} opt_timeout The amount of time to wait before fading out
2980 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2981 * stay up forever (or until the next overlay).
2982 */
2983hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002984 if (!this.overlayNode_) {
2985 if (!this.div_)
2986 return;
2987
2988 this.overlayNode_ = this.document_.createElement('div');
2989 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002990 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002991 'font-size: xx-large;' +
2992 'opacity: 0.75;' +
2993 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2994 'position: absolute;' +
2995 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002996 '-webkit-transition: opacity 180ms ease-in;' +
2997 '-moz-user-select: none;' +
2998 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002999
3000 this.overlayNode_.addEventListener('mousedown', function(e) {
3001 e.preventDefault();
3002 e.stopPropagation();
3003 }, true);
rgindaf0090c92012-02-10 14:58:52 -08003004 }
3005
rginda9f5222b2012-03-05 11:53:28 -08003006 this.overlayNode_.style.color = this.prefs_.get('background-color');
3007 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
3008 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
3009
rgindaf0090c92012-02-10 14:58:52 -08003010 this.overlayNode_.textContent = msg;
3011 this.overlayNode_.style.opacity = '0.75';
3012
3013 if (!this.overlayNode_.parentNode)
3014 this.div_.appendChild(this.overlayNode_);
3015
Robert Ginda97769282013-02-01 15:30:30 -08003016 var divSize = hterm.getClientSize(this.div_);
3017 var overlaySize = hterm.getClientSize(this.overlayNode_);
3018
Robert Ginda8a59f762014-07-23 11:29:55 -07003019 this.overlayNode_.style.top =
3020 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08003021 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07003022 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08003023
rgindaf0090c92012-02-10 14:58:52 -08003024 if (this.overlayTimeout_)
3025 clearTimeout(this.overlayTimeout_);
3026
Raymes Khouryc7a06382018-07-04 10:25:45 +10003027 this.accessibilityReader_.assertiveAnnounce(msg);
3028
rgindacc2996c2012-02-24 14:59:31 -08003029 if (opt_timeout === null)
3030 return;
3031
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003032 this.overlayTimeout_ = setTimeout(() => {
3033 this.overlayNode_.style.opacity = '0';
3034 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
3035 }, opt_timeout || 1500);
3036};
3037
3038/**
3039 * Hide the terminal overlay immediately.
3040 *
3041 * Useful when we show an overlay for an event with an unknown end time.
3042 */
3043hterm.Terminal.prototype.hideOverlay = function() {
3044 if (this.overlayTimeout_)
3045 clearTimeout(this.overlayTimeout_);
3046 this.overlayTimeout_ = null;
3047
3048 if (this.overlayNode_.parentNode)
3049 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
3050 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08003051};
3052
rginda4bba5e12012-06-20 16:15:30 -07003053/**
3054 * Paste from the system clipboard to the terminal.
3055 */
3056hterm.Terminal.prototype.paste = function() {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003057 return hterm.pasteFromClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07003058};
3059
3060/**
3061 * Copy a string to the system clipboard.
3062 *
3063 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05003064 *
3065 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07003066 */
3067hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08003068 if (this.prefs_.get('enable-clipboard-notice'))
3069 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
3070
Mike Frysinger96eacae2019-01-02 18:13:56 -05003071 hterm.copySelectionToClipboard(this.document_, str);
rginda4bba5e12012-06-20 16:15:30 -07003072};
3073
Evan Jones2600d4f2016-12-06 09:29:36 -05003074/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003075 * Display an image.
3076 *
3077 * @param {Object} options The image to display.
3078 * @param {string=} options.name A human readable string for the image.
3079 * @param {string|number=} options.size The size (in bytes).
3080 * @param {boolean=} options.preserveAspectRatio Whether to preserve aspect.
3081 * @param {boolean=} options.inline Whether to display the image inline.
3082 * @param {string|number=} options.width The width of the image.
3083 * @param {string|number=} options.height The height of the image.
3084 * @param {string=} options.align Direction to align the image.
3085 * @param {string} options.uri The source URI for the image.
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003086 * @param {function=} onLoad Callback when loading finishes.
3087 * @param {function(Event)=} onError Callback when loading fails.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003088 */
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003089hterm.Terminal.prototype.displayImage = function(options, onLoad, onError) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003090 // Make sure we're actually given a resource to display.
3091 if (options.uri === undefined)
3092 return;
3093
3094 // Set up the defaults to simplify code below.
3095 if (!options.name)
3096 options.name = '';
3097
3098 // Has the user approved image display yet?
3099 if (this.allowImagesInline !== true) {
3100 this.newLine();
3101 const row = this.getRowNode(this.scrollbackRows_.length +
3102 this.getCursorRow() - 1);
3103
3104 if (this.allowImagesInline === false) {
3105 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
3106 'Inline Images Disabled');
3107 return;
3108 }
3109
3110 // Show a prompt.
3111 let button;
3112 const span = this.document_.createElement('span');
3113 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
3114 span.style.fontWeight = 'bold';
3115 span.style.borderWidth = '1px';
3116 span.style.borderStyle = 'dashed';
3117 button = this.document_.createElement('span');
3118 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3119 button.style.marginLeft = '1em';
3120 button.style.borderWidth = '1px';
3121 button.style.borderStyle = 'solid';
3122 button.addEventListener('click', () => {
3123 this.prefs_.set('allow-images-inline', false);
3124 });
3125 span.appendChild(button);
3126 button = this.document_.createElement('span');
3127 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3128 'allow this session');
3129 button.style.marginLeft = '1em';
3130 button.style.borderWidth = '1px';
3131 button.style.borderStyle = 'solid';
3132 button.addEventListener('click', () => {
3133 this.allowImagesInline = true;
3134 });
3135 span.appendChild(button);
3136 button = this.document_.createElement('span');
3137 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3138 button.style.marginLeft = '1em';
3139 button.style.borderWidth = '1px';
3140 button.style.borderStyle = 'solid';
3141 button.addEventListener('click', () => {
3142 this.prefs_.set('allow-images-inline', true);
3143 });
3144 span.appendChild(button);
3145
3146 row.appendChild(span);
3147 return;
3148 }
3149
3150 // See if we should show this object directly, or download it.
3151 if (options.inline) {
3152 const io = this.io.push();
3153 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
3154 'Loading $1 ...'), null);
3155
3156 // While we're loading the image, eat all the user's input.
3157 io.onVTKeystroke = io.sendString = () => {};
3158
3159 // Initialize this new image.
Adrián Pérez-Orozco6a550322018-08-31 14:36:06 -07003160 const img =
3161 /** @type {!HTMLImageElement} */ (this.document_.createElement('img'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003162 img.src = options.uri;
3163 img.title = img.alt = options.name;
3164
3165 // Attach the image to the page to let it load/render. It won't stay here.
3166 // This is needed so it's visible and the DOM can calculate the height. If
3167 // the image is hidden or not in the DOM, the height is always 0.
3168 this.document_.body.appendChild(img);
3169
3170 // Wait for the image to finish loading before we try moving it to the
3171 // right place in the terminal.
3172 img.onload = () => {
3173 // Now that we have the image dimensions, figure out how to show it.
3174 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
3175 img.style.maxWidth = `${this.document_.body.clientWidth}px`;
3176 img.style.maxHeight = `${this.document_.body.clientHeight}px`;
3177
3178 // Parse a width/height specification.
3179 const parseDim = (dim, maxDim, cssVar) => {
3180 if (!dim || dim == 'auto')
3181 return '';
3182
3183 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3184 if (ary) {
3185 if (ary[2] == '%')
3186 return maxDim * parseInt(ary[1]) / 100 + 'px';
3187 else if (ary[2] == 'px')
3188 return dim;
3189 else
3190 return `calc(${dim} * var(${cssVar}))`;
3191 }
3192
3193 return '';
3194 };
3195 img.style.width =
3196 parseDim(options.width, this.document_.body.clientWidth,
3197 '--hterm-charsize-width');
3198 img.style.height =
3199 parseDim(options.height, this.document_.body.clientHeight,
3200 '--hterm-charsize-height');
3201
3202 // Figure out how many rows the image occupies, then add that many.
3203 // XXX: This count will be inaccurate if the font size changes on us.
3204 const padRows = Math.ceil(img.clientHeight /
3205 this.scrollPort_.characterSize.height);
3206 for (let i = 0; i < padRows; ++i)
3207 this.newLine();
3208
3209 // Update the max height in case the user shrinks the character size.
3210 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3211
3212 // Move the image to the last row. This way when we scroll up, it doesn't
3213 // disappear when the first row gets clipped. It will disappear when we
3214 // scroll down and the last row is clipped ...
3215 this.document_.body.removeChild(img);
3216 // Create a wrapper node so we can do an absolute in a relative position.
3217 // This helps with rounding errors between JS & CSS counts.
3218 const div = this.document_.createElement('div');
3219 div.style.position = 'relative';
3220 div.style.textAlign = options.align;
3221 img.style.position = 'absolute';
3222 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3223 div.appendChild(img);
3224 const row = this.getRowNode(this.scrollbackRows_.length +
3225 this.getCursorRow() - 1);
3226 row.appendChild(div);
3227
3228 io.hideOverlay();
3229 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003230
3231 if (onLoad)
3232 onLoad();
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003233 };
3234
3235 // If we got a malformed image, give up.
3236 img.onerror = (e) => {
3237 this.document_.body.removeChild(img);
3238 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
Mike Frysingere14a8c42018-03-10 00:17:30 -08003239 'Loading $1 failed'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003240 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003241
3242 if (onError)
3243 onError(e);
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003244 };
3245 } else {
3246 // We can't use chrome.downloads.download as that requires "downloads"
3247 // permissions, and that works only in extensions, not apps.
3248 const a = this.document_.createElement('a');
3249 a.href = options.uri;
3250 a.download = options.name;
3251 this.document_.body.appendChild(a);
3252 a.click();
3253 a.remove();
3254 }
3255};
3256
3257/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003258 * Returns the selected text, or null if no text is selected.
3259 *
3260 * @return {string|null}
3261 */
rgindaa09e7332012-08-17 12:49:51 -07003262hterm.Terminal.prototype.getSelectionText = function() {
3263 var selection = this.scrollPort_.selection;
3264 selection.sync();
3265
3266 if (selection.isCollapsed)
3267 return null;
3268
rgindaa09e7332012-08-17 12:49:51 -07003269 // Start offset measures from the beginning of the line.
3270 var startOffset = selection.startOffset;
3271 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003272
Raymes Khoury334625a2018-06-25 10:29:40 +10003273 // If an x-row isn't selected, |node| will be null.
3274 if (!node)
3275 return null;
3276
Robert Gindafdbb3f22012-09-06 20:23:06 -07003277 if (node.nodeName != 'X-ROW') {
3278 // If the selection doesn't start on an x-row node, then it must be
3279 // somewhere inside the x-row. Add any characters from previous siblings
3280 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003281
3282 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3283 // If node is the text node in a styled span, move up to the span node.
3284 node = node.parentNode;
3285 }
3286
Robert Gindafdbb3f22012-09-06 20:23:06 -07003287 while (node.previousSibling) {
3288 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003289 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003290 }
rgindaa09e7332012-08-17 12:49:51 -07003291 }
3292
3293 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08003294 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
3295 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003296 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003297
Robert Gindafdbb3f22012-09-06 20:23:06 -07003298 if (node.nodeName != 'X-ROW') {
3299 // If the selection doesn't end on an x-row node, then it must be
3300 // somewhere inside the x-row. Add any characters from following siblings
3301 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003302
3303 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3304 // If node is the text node in a styled span, move up to the span node.
3305 node = node.parentNode;
3306 }
3307
Robert Gindafdbb3f22012-09-06 20:23:06 -07003308 while (node.nextSibling) {
3309 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003310 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003311 }
rgindaa09e7332012-08-17 12:49:51 -07003312 }
3313
3314 var rv = this.getRowsText(selection.startRow.rowIndex,
3315 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003316 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003317};
3318
rginda4bba5e12012-06-20 16:15:30 -07003319/**
3320 * Copy the current selection to the system clipboard, then clear it after a
3321 * short delay.
3322 */
3323hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07003324 var text = this.getSelectionText();
3325 if (text != null)
3326 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07003327};
3328
rgindaf0090c92012-02-10 14:58:52 -08003329hterm.Terminal.prototype.overlaySize = function() {
3330 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
3331};
3332
rginda87b86462011-12-14 13:48:03 -08003333/**
3334 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3335 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003336 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003337 */
3338hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08003339 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08003340 this.scrollPort_.scrollRowToBottom(this.getRowCount());
3341
Mike Frysinger79669762018-12-30 20:51:10 -05003342 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08003343};
3344
3345/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003346 * Open the selected url.
3347 */
3348hterm.Terminal.prototype.openSelectedUrl_ = function() {
3349 var str = this.getSelectionText();
3350
3351 // If there is no selection, try and expand wherever they clicked.
3352 if (str == null) {
John Lincae9b732018-03-08 13:56:35 +08003353 this.screen_.expandSelectionForUrl(this.document_.getSelection());
Mike Frysinger70b94692017-01-26 18:57:50 -10003354 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003355
3356 // If clicking in empty space, return.
3357 if (str == null)
3358 return;
Mike Frysinger70b94692017-01-26 18:57:50 -10003359 }
3360
3361 // Make sure URL is valid before opening.
3362 if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0)
3363 return;
Mike Frysinger43472622017-06-26 18:11:07 -04003364
3365 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003366 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003367 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3368 // We have to whitelist a few protocols that lack authorities and thus
3369 // never use the //. Like mailto.
3370 switch (str.split(':', 1)[0]) {
3371 case 'mailto':
3372 break;
3373 default:
3374 str = 'http://' + str;
3375 break;
3376 }
3377 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003378
Mike Frysinger720fa832017-10-23 01:15:52 -04003379 hterm.openUrl(str);
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003380};
Mike Frysinger70b94692017-01-26 18:57:50 -10003381
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003382/**
3383 * Manage the automatic mouse hiding behavior while typing.
3384 *
3385 * @param {boolean=} v Whether to enable automatic hiding.
3386 */
3387hterm.Terminal.prototype.setAutomaticMouseHiding = function(v=null) {
3388 // Since Chrome OS & macOS do this by default everywhere, we don't need to.
3389 // Linux & Windows seem to leave this to specific applications to manage.
3390 if (v === null)
3391 v = (hterm.os != 'cros' && hterm.os != 'mac');
3392
3393 this.mouseHideWhileTyping_ = !!v;
3394};
3395
3396/**
3397 * Handler for monitoring user keyboard activity.
3398 *
3399 * This isn't for processing the keystrokes directly, but for updating any
3400 * state that might toggle based on the user using the keyboard at all.
3401 *
3402 * @param {KeyboardEvent} e The keyboard event that triggered us.
3403 */
3404hterm.Terminal.prototype.onKeyboardActivity_ = function(e) {
3405 // When the user starts typing, hide the mouse cursor.
3406 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_)
3407 this.setCssVar('mouse-cursor-style', 'none');
3408};
Mike Frysinger70b94692017-01-26 18:57:50 -10003409
3410/**
rgindad5613292012-06-19 15:40:37 -07003411 * Add the terminalRow and terminalColumn properties to mouse events and
3412 * then forward on to onMouse().
3413 *
3414 * The terminalRow and terminalColumn properties contain the (row, column)
3415 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003416 *
3417 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003418 */
3419hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003420 if (e.processedByTerminalHandler_) {
3421 // We register our event handlers on the document, as well as the cursor
3422 // and the scroll blocker. Mouse events that occur on the cursor or
3423 // scroll blocker will also appear on the document, but we don't want to
3424 // process them twice.
3425 //
3426 // We can't just prevent bubbling because that has other side effects, so
3427 // we decorate the event object with this property instead.
3428 return;
3429 }
3430
Mike Frysinger468966c2018-08-28 13:48:51 -04003431 // Consume navigation events. Button 3 is usually "browser back" and
3432 // button 4 is "browser forward" which we don't want to happen.
3433 if (e.button > 2) {
3434 e.preventDefault();
3435 // We don't return so click events can be passed to the remote below.
3436 }
3437
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003438 var reportMouseEvents = (!this.defeatMouseReports_ &&
3439 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3440
rgindafaa74742012-08-21 13:34:03 -07003441 e.processedByTerminalHandler_ = true;
3442
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003443 // Handle auto hiding of mouse cursor while typing.
3444 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
3445 // Make sure the mouse cursor is visible.
3446 this.syncMouseStyle();
3447 // This debounce isn't perfect, but should work well enough for such a
3448 // simple implementation. If the user moved the mouse, we enabled this
3449 // debounce, and then moved the mouse just before the timeout, we wouldn't
3450 // debounce that later movement.
3451 this.mouseHideDelay_ = setTimeout(() => this.mouseHideDelay_ = null, 1000);
3452 }
3453
Robert Gindaeda48db2014-07-17 09:25:30 -07003454 // One based row/column stored on the mouse event.
3455 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
3456 this.scrollPort_.characterSize.height) + 1;
3457 e.terminalColumn = parseInt(e.clientX /
3458 this.scrollPort_.characterSize.width) + 1;
3459
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003460 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
3461 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07003462 return;
3463 }
3464
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003465 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07003466 // If the cursor is visible and we're not sending mouse events to the
3467 // host app, then we want to hide the terminal cursor when the mouse
3468 // cursor is over top. This keeps the terminal cursor from interfering
3469 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003470 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3471 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3472 this.cursorNode_.style.display = 'none';
3473 } else if (this.cursorNode_.style.display == 'none') {
3474 this.cursorNode_.style.display = '';
3475 }
3476 }
rgindad5613292012-06-19 15:40:37 -07003477
Robert Ginda928cf632014-03-05 15:07:41 -08003478 if (e.type == 'mousedown') {
Mike Frysingercc114512017-09-11 21:39:17 -04003479 this.contextMenu.hide(e);
3480
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003481 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003482 // If VT mouse reporting is disabled, or has been defeated with
3483 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003484 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003485 this.setSelectionEnabled(true);
3486 } else {
3487 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003488 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003489 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003490 this.setSelectionEnabled(false);
3491 e.preventDefault();
3492 }
3493 }
3494
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003495 if (!reportMouseEvents) {
John Lin2aad22e2018-03-16 13:58:11 +08003496 if (e.type == 'dblclick') {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003497 this.screen_.expandSelection(this.document_.getSelection());
John Lin2aad22e2018-03-16 13:58:11 +08003498 if (this.copyOnSelect)
Mike Frysinger406c76c2019-01-02 17:53:51 -05003499 this.copySelectionToClipboard();
rgindad5613292012-06-19 15:40:37 -07003500 }
3501
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003502 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003503 // Debounce this event with the dblclick event. If you try to doubleclick
3504 // a URL to open it, Chrome will fire click then dblclick, but we won't
3505 // have expanded the selection text at the first click event.
3506 clearTimeout(this.timeouts_.openUrl);
3507 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3508 500);
3509 return;
3510 }
3511
Mike Frysinger847577f2017-05-23 23:25:57 -04003512 if (e.type == 'mousedown') {
Mike Frysingercc114512017-09-11 21:39:17 -04003513 if (e.ctrlKey && e.button == 2 /* right button */) {
3514 e.preventDefault();
3515 this.contextMenu.show(e, this);
3516 } else if (e.button == this.mousePasteButton ||
3517 (this.mouseRightClickPaste && e.button == 2 /* right button */)) {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003518 if (!this.paste())
Mike Frysinger05a57f02017-08-27 17:48:55 -04003519 console.warn('Could not paste manually due to web restrictions');
Mike Frysinger847577f2017-05-23 23:25:57 -04003520 }
3521 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003522
Mike Frysinger2edd3612017-05-24 00:54:39 -04003523 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003524 !this.document_.getSelection().isCollapsed) {
Mike Frysinger406c76c2019-01-02 17:53:51 -05003525 this.copySelectionToClipboard();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003526 }
3527
3528 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3529 this.scrollBlockerNode_.engaged) {
3530 // Disengage the scroll-blocker after one of these events.
3531 this.scrollBlockerNode_.engaged = false;
3532 this.scrollBlockerNode_.style.top = '-99px';
3533 }
3534
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003535 // Emulate arrow key presses via scroll wheel events.
3536 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3537 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003538 if (e.type == 'wheel') {
Mike Frysinger321063c2018-08-29 15:33:14 -04003539 const delta = this.scrollPort_.scrollWheelDelta(e);
Mike Frysingerc3030a82017-05-29 14:16:11 -04003540
Mike Frysinger321063c2018-08-29 15:33:14 -04003541 // Helper to turn a wheel event delta into a series of key presses.
3542 const deltaToArrows = (distance, charSize, arrowPos, arrowNeg) => {
3543 if (distance == 0) {
3544 return '';
3545 }
3546
3547 // Convert the scroll distance into a number of rows/cols.
3548 const cells = lib.f.smartFloorDivide(Math.abs(distance), charSize);
3549 const data = '\x1bO' + (distance < 0 ? arrowNeg : arrowPos);
3550 return data.repeat(cells);
3551 };
3552
3553 // The order between up/down and left/right doesn't really matter.
3554 this.io.sendString(
3555 // Up/down arrow keys.
3556 deltaToArrows(delta.y, this.scrollPort_.characterSize.height,
3557 'A', 'B') +
3558 // Left/right arrow keys.
3559 deltaToArrows(delta.x, this.scrollPort_.characterSize.width,
3560 'C', 'D')
3561 );
Mike Frysingerc3030a82017-05-29 14:16:11 -04003562
3563 e.preventDefault();
3564 }
3565 }
Robert Ginda928cf632014-03-05 15:07:41 -08003566 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003567 if (!this.scrollBlockerNode_.engaged) {
3568 if (e.type == 'mousedown') {
3569 // Move the scroll-blocker into place if we want to keep the scrollport
3570 // from scrolling.
3571 this.scrollBlockerNode_.engaged = true;
3572 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3573 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3574 } else if (e.type == 'mousemove') {
3575 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3576 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003577 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003578 e.preventDefault();
3579 }
3580 }
Robert Ginda928cf632014-03-05 15:07:41 -08003581
3582 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003583 }
3584
Robert Ginda928cf632014-03-05 15:07:41 -08003585 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3586 // Restore this on mouseup in case it was temporarily defeated with a
3587 // alt-mousedown. Only do this when the selection is empty so that
3588 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003589 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003590 }
rgindad5613292012-06-19 15:40:37 -07003591};
3592
3593/**
3594 * Clients should override this if they care to know about mouse events.
3595 *
3596 * The event parameter will be a normal DOM mouse click event with additional
3597 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003598 *
3599 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003600 */
3601hterm.Terminal.prototype.onMouse = function(e) { };
3602
3603/**
rginda8e92a692012-05-20 19:37:20 -07003604 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003605 *
3606 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003607 */
Rob Spies06533ba2014-04-24 11:20:37 -07003608hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3609 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003610 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003611
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003612 if (this.reportFocus)
3613 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O');
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003614
Michael Kelly485ecd12014-06-09 11:41:56 -04003615 if (focused === true)
3616 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07003617};
3618
3619/**
rginda8ba33642011-12-14 12:31:31 -08003620 * React when the ScrollPort is scrolled.
3621 */
3622hterm.Terminal.prototype.onScroll_ = function() {
3623 this.scheduleSyncCursorPosition_();
3624};
3625
3626/**
rginda9846e2f2012-01-27 13:53:33 -08003627 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003628 *
3629 * @param {Event} e The DOM paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08003630 */
3631hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaa063b202014-07-21 11:08:25 -07003632 var data = e.text.replace(/\n/mg, '\r');
Mike Frysingere8c32c82018-03-11 14:57:28 -07003633 if (this.options_.bracketedPaste) {
3634 // We strip out most escape sequences as they can cause issues (like
3635 // inserting an \x1b[201~ midstream). We pass through whitespace
3636 // though: 0x08:\b 0x09:\t 0x0a:\n 0x0d:\r.
3637 // This matches xterm behavior.
3638 const filter = (data) => data.replace(/[\x00-\x07\x0b-\x0c\x0e-\x1f]/g, '');
3639 data = '\x1b[200~' + filter(data) + '\x1b[201~';
3640 }
Robert Gindaa063b202014-07-21 11:08:25 -07003641
3642 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003643};
3644
3645/**
rgindaa09e7332012-08-17 12:49:51 -07003646 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003647 *
3648 * @param {Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07003649 */
3650hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07003651 if (!this.useDefaultWindowCopy) {
3652 e.preventDefault();
3653 setTimeout(this.copySelectionToClipboard.bind(this), 0);
3654 }
rgindaa09e7332012-08-17 12:49:51 -07003655};
3656
3657/**
rginda8ba33642011-12-14 12:31:31 -08003658 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08003659 *
3660 * Note: This function should not directly contain code that alters the internal
3661 * state of the terminal. That kind of code belongs in realizeWidth or
3662 * realizeHeight, so that it can be executed synchronously in the case of a
3663 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08003664 */
3665hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08003666 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07003667 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08003668 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07003669 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08003670
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003671 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08003672 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003673 // gets removed from the document or during the initial load, and we can't
3674 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07003675 // This can also happen if called before the scrollPort calculates the
3676 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08003677 return;
3678 }
3679
rgindaa8ba17d2012-08-15 14:41:10 -07003680 var isNewSize = (columnCount != this.screenSize.width ||
3681 rowCount != this.screenSize.height);
3682
3683 // We do this even if the size didn't change, just to be sure everything is
3684 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04003685 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07003686 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07003687
3688 if (isNewSize)
3689 this.overlaySize();
3690
Robert Gindafb1be6a2013-12-11 11:56:22 -08003691 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07003692 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08003693};
3694
3695/**
3696 * Service the cursor blink timeout.
3697 */
3698hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07003699 if (!this.options_.cursorBlink) {
3700 delete this.timeouts_.cursorBlink;
3701 return;
3702 }
3703
Robert Ginda830583c2013-08-07 13:20:46 -07003704 if (this.cursorNode_.getAttribute('focus') == 'false' ||
3705 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08003706 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07003707 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3708 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08003709 } else {
rginda87b86462011-12-14 13:48:03 -08003710 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07003711 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3712 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08003713 }
3714};
David Reveman8f552492012-03-28 12:18:41 -04003715
3716/**
3717 * Set the scrollbar-visible mode bit.
3718 *
3719 * If scrollbar-visible is on, the vertical scrollbar will be visible.
3720 * Otherwise it will not.
3721 *
3722 * Defaults to on.
3723 *
3724 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
3725 */
3726hterm.Terminal.prototype.setScrollbarVisible = function(state) {
3727 this.scrollPort_.setScrollbarVisible(state);
3728};
Michael Kelly485ecd12014-06-09 11:41:56 -04003729
3730/**
Rob Spies49039e52014-12-17 13:40:04 -08003731 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04003732 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08003733 *
3734 * Defaults to 1.
3735 *
Evan Jones2600d4f2016-12-06 09:29:36 -05003736 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08003737 */
3738hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3739 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3740};
3741
3742/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003743 * Close all web notifications created by terminal bells.
3744 */
3745hterm.Terminal.prototype.closeBellNotifications_ = function() {
3746 this.bellNotificationList_.forEach(function(n) {
3747 n.close();
3748 });
3749 this.bellNotificationList_.length = 0;
3750};
Raymes Khourye5d48982018-08-02 09:08:32 +10003751
3752/**
3753 * Syncs the cursor position when the scrollport gains focus.
3754 */
3755hterm.Terminal.prototype.onScrollportFocus_ = function() {
3756 // If the cursor is offscreen we set selection to the last row on the screen.
3757 const topRowIndex = this.scrollPort_.getTopRowIndex();
3758 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
3759 const selection = this.document_.getSelection();
3760 if (!this.syncCursorPosition_() && selection) {
3761 selection.collapse(this.getRowNode(bottomRowIndex));
3762 }
3763};