blob: c408e624d832a599581946b4adbe76e8060e39fd [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
85 // The current color of the cursor.
86 this.cursorColor_ = null;
87
Robert Gindaea2183e2014-07-17 09:51:51 -070088 // Cursor blink on/off cycle in ms, overwritten by prefs once they're loaded.
89 this.cursorBlinkCycle_ = [100, 100];
90
91 // Pre-bound onCursorBlink_ handler, so we don't have to do this for each
92 // cursor on/off servicing.
93 this.myOnCursorBlink_ = this.onCursorBlink_.bind(this);
94
rginda9f5222b2012-03-05 11:53:28 -080095 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -070096 // each output and keystroke. They are initialized by the preference manager.
Robert Ginda8cb7d902013-06-20 14:37:18 -070097 this.backgroundColor_ = null;
98 this.foregroundColor_ = null;
Robert Ginda57f03b42012-09-13 11:02:48 -070099 this.scrollOnOutput_ = null;
100 this.scrollOnKeystroke_ = null;
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400101 this.scrollWheelArrowKeys_ = null;
rginda9f5222b2012-03-05 11:53:28 -0800102
Robert Ginda6aec7eb2015-06-16 10:31:30 -0700103 // True if we should override mouse event reporting to allow local selection.
104 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -0800105
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400106 // Whether to auto hide the mouse cursor when typing.
107 this.setAutomaticMouseHiding();
108 // Timer to keep mouse visible while it's being used.
109 this.mouseHideDelay_ = null;
110
rgindaf0090c92012-02-10 14:58:52 -0800111 // Terminal bell sound.
112 this.bellAudio_ = this.document_.createElement('audio');
Mike Frysingerd826f1a2017-07-06 16:20:06 -0400113 this.bellAudio_.id = 'hterm:bell-audio';
rgindaf0090c92012-02-10 14:58:52 -0800114 this.bellAudio_.setAttribute('preload', 'auto');
115
Raymes Khoury3e44bc92018-05-17 10:54:23 +1000116 // The AccessibilityReader object for announcing command output.
117 this.accessibilityReader_ = null;
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
Masaya Suzuki273aa982014-05-31 07:25:55 +0900384 'east-asian-ambiguous-as-two-column': function(v) {
385 lib.wc.regardCjkAmbiguous = v;
386 },
387
Robert Ginda57f03b42012-09-13 11:02:48 -0700388 'enable-8-bit-control': function(v) {
389 terminal.vt.enable8BitControl = !!v;
390 },
rginda30f20f62012-04-05 16:36:19 -0700391
Robert Ginda57f03b42012-09-13 11:02:48 -0700392 'enable-bold': function(v) {
393 terminal.syncBoldSafeState();
394 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400395
Robert Ginda3e278d72014-03-25 13:18:51 -0700396 'enable-bold-as-bright': function(v) {
397 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
398 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
399 },
400
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400401 'enable-blink': function(v) {
Mike Frysinger261597c2017-12-28 01:14:21 -0500402 terminal.setTextBlink(!!v);
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400403 },
404
Robert Ginda57f03b42012-09-13 11:02:48 -0700405 'enable-clipboard-write': function(v) {
406 terminal.vt.enableClipboardWrite = !!v;
407 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400408
Robert Ginda3755e752013-05-31 13:34:09 -0700409 'enable-dec12': function(v) {
410 terminal.vt.enableDec12 = !!v;
411 },
412
Robert Ginda57f03b42012-09-13 11:02:48 -0700413 'font-family': function(v) {
414 terminal.syncFontFamily();
415 },
rginda30f20f62012-04-05 16:36:19 -0700416
Robert Ginda57f03b42012-09-13 11:02:48 -0700417 'font-size': function(v) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500418 v = parseInt(v);
419 if (v <= 0) {
420 console.error(`Invalid font size: ${v}`);
421 return;
422 }
423
Robert Ginda57f03b42012-09-13 11:02:48 -0700424 terminal.setFontSize(v);
425 },
rginda9875d902012-08-20 16:21:57 -0700426
Robert Ginda57f03b42012-09-13 11:02:48 -0700427 'font-smoothing': function(v) {
428 terminal.syncFontFamily();
429 },
rgindade84e382012-04-20 15:39:31 -0700430
Robert Ginda57f03b42012-09-13 11:02:48 -0700431 'foreground-color': function(v) {
432 terminal.setForegroundColor(v);
433 },
rginda30f20f62012-04-05 16:36:19 -0700434
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400435 'hide-mouse-while-typing': function(v) {
436 terminal.setAutomaticMouseHiding(v);
437 },
438
Robert Ginda57f03b42012-09-13 11:02:48 -0700439 'home-keys-scroll': function(v) {
440 terminal.keyboard.homeKeysScroll = v;
441 },
rginda4bba5e12012-06-20 16:15:30 -0700442
Robert Gindaa8165692015-06-15 14:46:31 -0700443 'keybindings': function(v) {
444 terminal.keyboard.bindings.clear();
445
446 if (!v)
447 return;
448
449 if (!(v instanceof Object)) {
450 console.error('Error in keybindings preference: Expected object');
451 return;
452 }
453
454 try {
455 terminal.keyboard.bindings.addBindings(v);
456 } catch (ex) {
457 console.error('Error in keybindings preference: ' + ex);
458 }
459 },
460
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700461 'media-keys-are-fkeys': function(v) {
462 terminal.keyboard.mediaKeysAreFKeys = v;
463 },
464
Robert Ginda57f03b42012-09-13 11:02:48 -0700465 'meta-sends-escape': function(v) {
466 terminal.keyboard.metaSendsEscape = v;
467 },
rginda30f20f62012-04-05 16:36:19 -0700468
Mike Frysinger847577f2017-05-23 23:25:57 -0400469 'mouse-right-click-paste': function(v) {
470 terminal.mouseRightClickPaste = v;
471 },
472
Robert Ginda57f03b42012-09-13 11:02:48 -0700473 'mouse-paste-button': function(v) {
474 terminal.syncMousePasteButton();
475 },
rgindaa8ba17d2012-08-15 14:41:10 -0700476
Robert Gindae76aa9f2014-03-14 12:29:12 -0700477 'page-keys-scroll': function(v) {
478 terminal.keyboard.pageKeysScroll = v;
479 },
480
Robert Ginda40932892012-12-10 17:26:40 -0800481 'pass-alt-number': function(v) {
482 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800483 // Let Alt-1..9 pass to the browser (to control tab switching) on
484 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500485 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800486 }
487
488 terminal.passAltNumber = v;
489 },
490
491 'pass-ctrl-number': function(v) {
492 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800493 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
494 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500495 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800496 }
497
498 terminal.passCtrlNumber = v;
499 },
500
501 'pass-meta-number': function(v) {
502 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800503 // Let Meta-1..9 pass to the browser (to control tab switching) on
504 // OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500505 v = (hterm.os == 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800506 }
507
508 terminal.passMetaNumber = v;
509 },
510
Marius Schilder77857b32014-05-14 16:21:26 -0700511 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700512 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700513 },
514
Robert Ginda8cb7d902013-06-20 14:37:18 -0700515 'receive-encoding': function(v) {
516 if (!(/^(utf-8|raw)$/).test(v)) {
517 console.warn('Invalid value for "receive-encoding": ' + v);
518 v = 'utf-8';
519 }
520
521 terminal.vt.characterEncoding = v;
522 },
523
Robert Ginda57f03b42012-09-13 11:02:48 -0700524 'scroll-on-keystroke': function(v) {
525 terminal.scrollOnKeystroke_ = v;
526 },
rginda9f5222b2012-03-05 11:53:28 -0800527
Robert Ginda57f03b42012-09-13 11:02:48 -0700528 'scroll-on-output': function(v) {
529 terminal.scrollOnOutput_ = v;
530 },
rginda30f20f62012-04-05 16:36:19 -0700531
Robert Ginda57f03b42012-09-13 11:02:48 -0700532 'scrollbar-visible': function(v) {
533 terminal.setScrollbarVisible(v);
534 },
rginda9f5222b2012-03-05 11:53:28 -0800535
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400536 'scroll-wheel-may-send-arrow-keys': function(v) {
537 terminal.scrollWheelArrowKeys_ = v;
538 },
539
Rob Spies49039e52014-12-17 13:40:04 -0800540 'scroll-wheel-move-multiplier': function(v) {
541 terminal.setScrollWheelMoveMultipler(v);
542 },
543
Robert Ginda8cb7d902013-06-20 14:37:18 -0700544 'send-encoding': function(v) {
545 if (!(/^(utf-8|raw)$/).test(v)) {
546 console.warn('Invalid value for "send-encoding": ' + v);
547 v = 'utf-8';
548 }
549
550 terminal.keyboard.characterEncoding = v;
551 },
552
Robert Ginda57f03b42012-09-13 11:02:48 -0700553 'shift-insert-paste': function(v) {
554 terminal.keyboard.shiftInsertPaste = v;
555 },
rginda9f5222b2012-03-05 11:53:28 -0800556
Mike Frysingera7768922017-07-28 15:00:12 -0400557 'terminal-encoding': function(v) {
Mike Frysingera1371e12017-08-17 01:37:17 -0400558 terminal.vt.setEncoding(v);
Mike Frysingera7768922017-07-28 15:00:12 -0400559 },
560
Robert Gindae76aa9f2014-03-14 12:29:12 -0700561 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400562 terminal.scrollPort_.setUserCssUrl(v);
563 },
564
565 'user-css-text': function(v) {
566 terminal.scrollPort_.setUserCssText(v);
567 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400568
569 'word-break-match-left': function(v) {
570 terminal.primaryScreen_.wordBreakMatchLeft = v;
571 terminal.alternateScreen_.wordBreakMatchLeft = v;
572 },
573
574 'word-break-match-right': function(v) {
575 terminal.primaryScreen_.wordBreakMatchRight = v;
576 terminal.alternateScreen_.wordBreakMatchRight = v;
577 },
578
579 'word-break-match-middle': function(v) {
580 terminal.primaryScreen_.wordBreakMatchMiddle = v;
581 terminal.alternateScreen_.wordBreakMatchMiddle = v;
582 },
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400583
584 'allow-images-inline': function(v) {
585 terminal.allowImagesInline = v;
586 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700587 });
rginda30f20f62012-04-05 16:36:19 -0700588
Robert Ginda57f03b42012-09-13 11:02:48 -0700589 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800590 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700591
592 if (opt_callback)
593 opt_callback();
594 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800595};
596
Rob Spies56953412014-04-28 14:09:47 -0700597
598/**
599 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500600 *
601 * @return {hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700602 */
603hterm.Terminal.prototype.getPrefs = function() {
604 return this.prefs_;
605};
606
Robert Gindaa063b202014-07-21 11:08:25 -0700607/**
608 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500609 *
610 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700611 */
612hterm.Terminal.prototype.setBracketedPaste = function(state) {
613 this.options_.bracketedPaste = state;
614};
Rob Spies56953412014-04-28 14:09:47 -0700615
rginda8e92a692012-05-20 19:37:20 -0700616/**
617 * Set the color for the cursor.
618 *
619 * If you want this setting to persist, set it through prefs_, rather than
620 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500621 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500622 * @param {string=} color The color to set. If not defined, we reset to the
623 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700624 */
625hterm.Terminal.prototype.setCursorColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500626 if (color === undefined)
627 color = this.prefs_.get('cursor-color');
628
Robert Ginda830583c2013-08-07 13:20:46 -0700629 this.cursorColor_ = color;
rginda8e92a692012-05-20 19:37:20 -0700630 this.cursorNode_.style.backgroundColor = color;
631 this.cursorNode_.style.borderColor = color;
632};
633
634/**
635 * Return the current cursor color as a string.
Evan Jones2600d4f2016-12-06 09:29:36 -0500636 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700637 */
638hterm.Terminal.prototype.getCursorColor = function() {
Robert Ginda830583c2013-08-07 13:20:46 -0700639 return this.cursorColor_;
rginda8e92a692012-05-20 19:37:20 -0700640};
641
642/**
rgindad5613292012-06-19 15:40:37 -0700643 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500644 *
645 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700646 */
647hterm.Terminal.prototype.setSelectionEnabled = function(state) {
648 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700649};
650
651/**
rginda8e92a692012-05-20 19:37:20 -0700652 * Set the background color.
653 *
654 * If you want this setting to persist, set it through prefs_, rather than
655 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500656 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500657 * @param {string=} color The color to set. If not defined, we reset to the
658 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700659 */
660hterm.Terminal.prototype.setBackgroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500661 if (color === undefined)
662 color = this.prefs_.get('background-color');
663
rgindacbbd7482012-06-13 15:06:16 -0700664 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700665 this.primaryScreen_.textAttributes.setDefaults(
666 this.foregroundColor_, this.backgroundColor_);
667 this.alternateScreen_.textAttributes.setDefaults(
668 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700669 this.scrollPort_.setBackgroundColor(color);
670};
671
rginda9f5222b2012-03-05 11:53:28 -0800672/**
673 * Return the current terminal background color.
674 *
675 * Intended for use by other classes, so we don't have to expose the entire
676 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500677 *
678 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800679 */
680hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700681 return this.backgroundColor_;
682};
683
684/**
685 * Set the foreground color.
686 *
687 * If you want this setting to persist, set it through prefs_, rather than
688 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500689 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500690 * @param {string=} color The color to set. If not defined, we reset to the
691 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700692 */
693hterm.Terminal.prototype.setForegroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500694 if (color === undefined)
695 color = this.prefs_.get('foreground-color');
696
rgindacbbd7482012-06-13 15:06:16 -0700697 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700698 this.primaryScreen_.textAttributes.setDefaults(
699 this.foregroundColor_, this.backgroundColor_);
700 this.alternateScreen_.textAttributes.setDefaults(
701 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700702 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800703};
704
705/**
706 * Return the current terminal foreground color.
707 *
708 * Intended for use by other classes, so we don't have to expose the entire
709 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500710 *
711 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800712 */
713hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700714 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800715};
716
717/**
rginda87b86462011-12-14 13:48:03 -0800718 * Create a new instance of a terminal command and run it with a given
719 * argument string.
720 *
721 * @param {function} commandClass The constructor for a terminal command.
722 * @param {string} argString The argument string to pass to the command.
723 */
724hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700725 var environment = this.prefs_.get('environment');
726 if (typeof environment != 'object' || environment == null)
727 environment = {};
728
rginda87b86462011-12-14 13:48:03 -0800729 var self = this;
730 this.command = new commandClass(
731 { argString: argString || '',
732 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700733 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800734 onExit: function(code) {
735 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800736 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700737 if (self.prefs_.get('close-on-exit'))
738 window.close();
rginda87b86462011-12-14 13:48:03 -0800739 }
740 });
741
rgindafeaf3142012-01-31 15:14:20 -0800742 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800743 this.command.run();
744};
745
746/**
rgindafeaf3142012-01-31 15:14:20 -0800747 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500748 *
749 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800750 */
751hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700752 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800753};
754
755/**
756 * Install the keyboard handler for this terminal.
757 *
758 * This will prevent the browser from seeing any keystrokes sent to the
759 * terminal.
760 */
761hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700762 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400763};
rgindafeaf3142012-01-31 15:14:20 -0800764
765/**
766 * Uninstall the keyboard handler for this terminal.
767 */
768hterm.Terminal.prototype.uninstallKeyboard = function() {
769 this.keyboard.installKeyboard(null);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400770};
rgindafeaf3142012-01-31 15:14:20 -0800771
772/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400773 * Set a CSS variable.
774 *
775 * Normally this is used to set variables in the hterm namespace.
776 *
777 * @param {string} name The variable to set.
778 * @param {string} value The value to assign to the variable.
779 * @param {string?} opt_prefix The variable namespace/prefix to use.
780 */
781hterm.Terminal.prototype.setCssVar = function(name, value,
782 opt_prefix='--hterm-') {
783 this.document_.documentElement.style.setProperty(
784 `${opt_prefix}${name}`, value);
785};
786
787/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500788 * Get a CSS variable.
789 *
790 * Normally this is used to get variables in the hterm namespace.
791 *
792 * @param {string} name The variable to read.
793 * @param {string?} opt_prefix The variable namespace/prefix to use.
794 * @return {string} The current setting for this variable.
795 */
796hterm.Terminal.prototype.getCssVar = function(name, opt_prefix='--hterm-') {
797 return this.document_.documentElement.style.getPropertyValue(
798 `${opt_prefix}${name}`);
799};
800
801/**
rginda35c456b2012-02-09 17:29:05 -0800802 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800803 *
804 * Call setFontSize(0) to reset to the default font size.
805 *
806 * This function does not modify the font-size preference.
807 *
808 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800809 */
810hterm.Terminal.prototype.setFontSize = function(px) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500811 if (px <= 0)
rginda9f5222b2012-03-05 11:53:28 -0800812 px = this.prefs_.get('font-size');
813
rginda35c456b2012-02-09 17:29:05 -0800814 this.scrollPort_.setFontSize(px);
Mike Frysingercce97c42017-08-05 01:11:22 -0400815 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
816 this.setCssVar('charsize-height',
817 this.scrollPort_.characterSize.height + 'px');
rginda35c456b2012-02-09 17:29:05 -0800818};
819
820/**
821 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500822 *
823 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800824 */
825hterm.Terminal.prototype.getFontSize = function() {
826 return this.scrollPort_.getFontSize();
827};
828
829/**
rginda8e92a692012-05-20 19:37:20 -0700830 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500831 *
832 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700833 */
834hterm.Terminal.prototype.getFontFamily = function() {
835 return this.scrollPort_.getFontFamily();
836};
837
838/**
rginda35c456b2012-02-09 17:29:05 -0800839 * Set the CSS "font-family" for this terminal.
840 */
rginda9f5222b2012-03-05 11:53:28 -0800841hterm.Terminal.prototype.syncFontFamily = function() {
842 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
843 this.prefs_.get('font-smoothing'));
844 this.syncBoldSafeState();
845};
846
rginda4bba5e12012-06-20 16:15:30 -0700847/**
848 * Set this.mousePasteButton based on the mouse-paste-button pref,
849 * autodetecting if necessary.
850 */
851hterm.Terminal.prototype.syncMousePasteButton = function() {
852 var button = this.prefs_.get('mouse-paste-button');
853 if (typeof button == 'number') {
854 this.mousePasteButton = button;
855 return;
856 }
857
Mike Frysingeree81a002017-12-12 16:14:53 -0500858 if (hterm.os != 'linux') {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400859 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700860 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400861 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700862 }
863};
864
865/**
866 * Enable or disable bold based on the enable-bold pref, autodetecting if
867 * necessary.
868 */
rginda9f5222b2012-03-05 11:53:28 -0800869hterm.Terminal.prototype.syncBoldSafeState = function() {
870 var enableBold = this.prefs_.get('enable-bold');
871 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700872 this.primaryScreen_.textAttributes.enableBold = enableBold;
873 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800874 return;
875 }
876
rgindaf7521392012-02-28 17:20:34 -0800877 var normalSize = this.scrollPort_.measureCharacterSize();
878 var boldSize = this.scrollPort_.measureCharacterSize('bold');
879
880 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800881 if (!isBoldSafe) {
882 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700883 'from normal. Font family is: ' +
884 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800885 }
rginda9f5222b2012-03-05 11:53:28 -0800886
Robert Gindaed016262012-10-26 16:27:09 -0700887 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
888 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800889};
890
891/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500892 * Control text blinking behavior.
893 *
894 * @param {boolean=} state Whether to enable support for blinking text.
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400895 */
Mike Frysinger261597c2017-12-28 01:14:21 -0500896hterm.Terminal.prototype.setTextBlink = function(state) {
897 if (state === undefined)
898 state = this.prefs_.get('enable-blink');
899 this.setCssVar('blink-node-duration', state ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400900};
901
902/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400903 * Set the mouse cursor style based on the current terminal mode.
904 */
905hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -0400906 this.setCssVar('mouse-cursor-style',
907 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
908 'var(--hterm-mouse-cursor-text)' :
909 'var(--hterm-mouse-cursor-pointer)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400910};
911
912/**
rginda87b86462011-12-14 13:48:03 -0800913 * Return a copy of the current cursor position.
914 *
915 * @return {hterm.RowCol} The RowCol object representing the current position.
916 */
917hterm.Terminal.prototype.saveCursor = function() {
918 return this.screen_.cursorPosition.clone();
919};
920
Evan Jones2600d4f2016-12-06 09:29:36 -0500921/**
922 * Return the current text attributes.
923 *
924 * @return {string}
925 */
rgindaa19afe22012-01-25 15:40:22 -0800926hterm.Terminal.prototype.getTextAttributes = function() {
927 return this.screen_.textAttributes;
928};
929
Evan Jones2600d4f2016-12-06 09:29:36 -0500930/**
931 * Set the text attributes.
932 *
933 * @param {string} textAttributes The attributes to set.
934 */
rginda1a09aa02012-06-18 21:11:25 -0700935hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
936 this.screen_.textAttributes = textAttributes;
937};
938
rginda87b86462011-12-14 13:48:03 -0800939/**
rgindaf522ce02012-04-17 17:49:17 -0700940 * Return the current browser zoom factor applied to the terminal.
941 *
942 * @return {number} The current browser zoom factor.
943 */
944hterm.Terminal.prototype.getZoomFactor = function() {
945 return this.scrollPort_.characterSize.zoomFactor;
946};
947
948/**
rginda9846e2f2012-01-27 13:53:33 -0800949 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -0500950 *
951 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -0800952 */
953hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800954 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800955};
956
957/**
rginda87b86462011-12-14 13:48:03 -0800958 * Restore a previously saved cursor position.
959 *
960 * @param {hterm.RowCol} cursor The position to restore.
961 */
962hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700963 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
964 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800965 this.screen_.setCursorPosition(row, column);
966 if (cursor.column > column ||
967 cursor.column == column && cursor.overflow) {
968 this.screen_.cursorPosition.overflow = true;
969 }
rginda87b86462011-12-14 13:48:03 -0800970};
971
972/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400973 * Clear the cursor's overflow flag.
974 */
975hterm.Terminal.prototype.clearCursorOverflow = function() {
976 this.screen_.cursorPosition.overflow = false;
977};
978
979/**
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800980 * Save the current cursor state to the corresponding screens.
981 *
982 * See the hterm.Screen.CursorState class for more details.
983 *
984 * @param {boolean=} both If true, update both screens, else only update the
985 * current screen.
986 */
987hterm.Terminal.prototype.saveCursorAndState = function(both) {
988 if (both) {
989 this.primaryScreen_.saveCursorAndState(this.vt);
990 this.alternateScreen_.saveCursorAndState(this.vt);
991 } else
992 this.screen_.saveCursorAndState(this.vt);
993};
994
995/**
996 * Restore the saved cursor state in the corresponding screens.
997 *
998 * See the hterm.Screen.CursorState class for more details.
999 *
1000 * @param {boolean=} both If true, update both screens, else only update the
1001 * current screen.
1002 */
1003hterm.Terminal.prototype.restoreCursorAndState = function(both) {
1004 if (both) {
1005 this.primaryScreen_.restoreCursorAndState(this.vt);
1006 this.alternateScreen_.restoreCursorAndState(this.vt);
1007 } else
1008 this.screen_.restoreCursorAndState(this.vt);
1009};
1010
1011/**
Robert Ginda830583c2013-08-07 13:20:46 -07001012 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001013 *
1014 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -07001015 */
1016hterm.Terminal.prototype.setCursorShape = function(shape) {
1017 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001018 this.restyleCursor_();
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001019};
Robert Ginda830583c2013-08-07 13:20:46 -07001020
1021/**
1022 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001023 *
1024 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -07001025 */
1026hterm.Terminal.prototype.getCursorShape = function() {
1027 return this.cursorShape_;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001028};
Robert Ginda830583c2013-08-07 13:20:46 -07001029
1030/**
rginda87b86462011-12-14 13:48:03 -08001031 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001032 *
1033 * @param {number} columnCount
rginda87b86462011-12-14 13:48:03 -08001034 */
1035hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -08001036 if (columnCount == null) {
1037 this.div_.style.width = '100%';
1038 return;
1039 }
1040
Robert Ginda26806d12014-07-24 13:44:07 -07001041 this.div_.style.width = Math.ceil(
1042 this.scrollPort_.characterSize.width *
1043 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001044 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -08001045 this.scheduleSyncCursorPosition_();
1046};
rginda87b86462011-12-14 13:48:03 -08001047
rgindac9bc5502012-01-18 11:48:44 -08001048/**
rginda35c456b2012-02-09 17:29:05 -08001049 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001050 *
1051 * @param {number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -08001052 */
1053hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -08001054 if (rowCount == null) {
1055 this.div_.style.height = '100%';
1056 return;
1057 }
1058
rginda35c456b2012-02-09 17:29:05 -08001059 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -07001060 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -08001061 this.realizeSize_(this.screenSize.width, rowCount);
1062 this.scheduleSyncCursorPosition_();
1063};
1064
1065/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001066 * Deal with terminal size changes.
1067 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001068 * @param {number} columnCount The number of columns.
1069 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001070 */
1071hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
1072 if (columnCount != this.screenSize.width)
1073 this.realizeWidth_(columnCount);
1074
1075 if (rowCount != this.screenSize.height)
1076 this.realizeHeight_(rowCount);
1077
1078 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -07001079 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001080};
1081
1082/**
rgindac9bc5502012-01-18 11:48:44 -08001083 * Deal with terminal width changes.
1084 *
1085 * This function does what needs to be done when the terminal width changes
1086 * out from under us. It happens here rather than in onResize_() because this
1087 * code may need to run synchronously to handle programmatic changes of
1088 * terminal width.
1089 *
1090 * Relying on the browser to send us an async resize event means we may not be
1091 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001092 *
1093 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001094 */
1095hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001096 if (columnCount <= 0)
1097 throw new Error('Attempt to realize bad width: ' + columnCount);
1098
rgindac9bc5502012-01-18 11:48:44 -08001099 var deltaColumns = columnCount - this.screen_.getWidth();
1100
rginda87b86462011-12-14 13:48:03 -08001101 this.screenSize.width = columnCount;
1102 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001103
1104 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -04001105 if (this.defaultTabStops)
1106 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -08001107 } else {
1108 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -04001109 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -08001110 break;
1111
1112 this.tabStops_.pop();
1113 }
1114 }
1115
1116 this.screen_.setColumnCount(this.screenSize.width);
1117};
1118
1119/**
1120 * Deal with terminal height changes.
1121 *
1122 * This function does what needs to be done when the terminal height changes
1123 * out from under us. It happens here rather than in onResize_() because this
1124 * code may need to run synchronously to handle programmatic changes of
1125 * terminal height.
1126 *
1127 * Relying on the browser to send us an async resize event means we may not be
1128 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001129 *
1130 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001131 */
1132hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001133 if (rowCount <= 0)
1134 throw new Error('Attempt to realize bad height: ' + rowCount);
1135
rgindac9bc5502012-01-18 11:48:44 -08001136 var deltaRows = rowCount - this.screen_.getHeight();
1137
1138 this.screenSize.height = rowCount;
1139
1140 var cursor = this.saveCursor();
1141
1142 if (deltaRows < 0) {
1143 // Screen got smaller.
1144 deltaRows *= -1;
1145 while (deltaRows) {
1146 var lastRow = this.getRowCount() - 1;
1147 if (lastRow - this.scrollbackRows_.length == cursor.row)
1148 break;
1149
1150 if (this.getRowText(lastRow))
1151 break;
1152
1153 this.screen_.popRow();
1154 deltaRows--;
1155 }
1156
1157 var ary = this.screen_.shiftRows(deltaRows);
1158 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1159
1160 // We just removed rows from the top of the screen, we need to update
1161 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001162 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001163 } else if (deltaRows > 0) {
1164 // Screen got larger.
1165
1166 if (deltaRows <= this.scrollbackRows_.length) {
1167 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1168 var rows = this.scrollbackRows_.splice(
1169 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1170 this.screen_.unshiftRows(rows);
1171 deltaRows -= scrollbackCount;
1172 cursor.row += scrollbackCount;
1173 }
1174
1175 if (deltaRows)
1176 this.appendRows_(deltaRows);
1177 }
1178
rginda35c456b2012-02-09 17:29:05 -08001179 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001180 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001181};
1182
1183/**
1184 * Scroll the terminal to the top of the scrollback buffer.
1185 */
1186hterm.Terminal.prototype.scrollHome = function() {
1187 this.scrollPort_.scrollRowToTop(0);
1188};
1189
1190/**
1191 * Scroll the terminal to the end.
1192 */
1193hterm.Terminal.prototype.scrollEnd = function() {
1194 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1195};
1196
1197/**
1198 * Scroll the terminal one page up (minus one line) relative to the current
1199 * position.
1200 */
1201hterm.Terminal.prototype.scrollPageUp = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001202 this.scrollPort_.scrollPageUp();
rginda87b86462011-12-14 13:48:03 -08001203};
1204
1205/**
1206 * Scroll the terminal one page down (minus one line) relative to the current
1207 * position.
1208 */
1209hterm.Terminal.prototype.scrollPageDown = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001210 this.scrollPort_.scrollPageDown();
rginda8ba33642011-12-14 12:31:31 -08001211};
1212
rgindac9bc5502012-01-18 11:48:44 -08001213/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001214 * Scroll the terminal one line up relative to the current position.
1215 */
1216hterm.Terminal.prototype.scrollLineUp = function() {
1217 var i = this.scrollPort_.getTopRowIndex();
1218 this.scrollPort_.scrollRowToTop(i - 1);
1219};
1220
1221/**
1222 * Scroll the terminal one line down relative to the current position.
1223 */
1224hterm.Terminal.prototype.scrollLineDown = function() {
1225 var i = this.scrollPort_.getTopRowIndex();
1226 this.scrollPort_.scrollRowToTop(i + 1);
1227};
1228
1229/**
Robert Ginda40932892012-12-10 17:26:40 -08001230 * Clear primary screen, secondary screen, and the scrollback buffer.
1231 */
1232hterm.Terminal.prototype.wipeContents = function() {
1233 this.scrollbackRows_.length = 0;
1234 this.scrollPort_.resetCache();
1235
1236 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
1237 var bottom = screen.getHeight();
1238 if (bottom > 0) {
1239 this.renumberRows_(0, bottom);
1240 this.clearHome(screen);
1241 }
1242 }.bind(this));
1243
1244 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001245 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001246};
1247
1248/**
rgindac9bc5502012-01-18 11:48:44 -08001249 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001250 *
1251 * Perform a full reset to the default values listed in
1252 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001253 */
rginda87b86462011-12-14 13:48:03 -08001254hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001255 this.vt.reset();
1256
rgindac9bc5502012-01-18 11:48:44 -08001257 this.clearAllTabStops();
1258 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001259
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001260 const resetScreen = (screen) => {
1261 // We want to make sure to reset the attributes before we clear the screen.
1262 // The attributes might be used to initialize default/empty rows.
1263 screen.textAttributes.reset();
1264 screen.textAttributes.resetColorPalette();
1265 this.clearHome(screen);
1266 screen.saveCursorAndState(this.vt);
1267 };
1268 resetScreen(this.primaryScreen_);
1269 resetScreen(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001270
Mike Frysinger84301d02017-11-29 13:28:46 -08001271 // Reset terminal options to their default values.
1272 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001273 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1274
Mike Frysinger84301d02017-11-29 13:28:46 -08001275 this.setVTScrollRegion(null, null);
1276
1277 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001278};
1279
rgindac9bc5502012-01-18 11:48:44 -08001280/**
1281 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001282 *
1283 * Perform a soft reset to the default values listed in
1284 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001285 */
rginda0f5c0292012-01-13 11:00:13 -08001286hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001287 this.vt.reset();
1288
rgindab8bc8932012-04-27 12:45:03 -07001289 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001290 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001291
Brad Townb62dfdc2015-03-16 19:07:15 -07001292 // We show the cursor on soft reset but do not alter the blink state.
1293 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1294
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001295 const resetScreen = (screen) => {
1296 // Xterm also resets the color palette on soft reset, even though it doesn't
1297 // seem to be documented anywhere.
1298 screen.textAttributes.reset();
1299 screen.textAttributes.resetColorPalette();
1300 screen.saveCursorAndState(this.vt);
1301 };
1302 resetScreen(this.primaryScreen_);
1303 resetScreen(this.alternateScreen_);
rgindaf522ce02012-04-17 17:49:17 -07001304
rgindab8bc8932012-04-27 12:45:03 -07001305 // The xterm man page explicitly says this will happen on soft reset.
1306 this.setVTScrollRegion(null, null);
1307
1308 // Xterm also shows the cursor on soft reset, but does not alter the blink
1309 // state.
rgindaa19afe22012-01-25 15:40:22 -08001310 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001311};
1312
rgindac9bc5502012-01-18 11:48:44 -08001313/**
1314 * Move the cursor forward to the next tab stop, or to the last column
1315 * if no more tab stops are set.
1316 */
1317hterm.Terminal.prototype.forwardTabStop = function() {
1318 var column = this.screen_.cursorPosition.column;
1319
1320 for (var i = 0; i < this.tabStops_.length; i++) {
1321 if (this.tabStops_[i] > column) {
1322 this.setCursorColumn(this.tabStops_[i]);
1323 return;
1324 }
1325 }
1326
David Benjamin66e954d2012-05-05 21:08:12 -04001327 // xterm does not clear the overflow flag on HT or CHT.
1328 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001329 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001330 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001331};
1332
rgindac9bc5502012-01-18 11:48:44 -08001333/**
1334 * Move the cursor backward to the previous tab stop, or to the first column
1335 * if no previous tab stops are set.
1336 */
1337hterm.Terminal.prototype.backwardTabStop = function() {
1338 var column = this.screen_.cursorPosition.column;
1339
1340 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1341 if (this.tabStops_[i] < column) {
1342 this.setCursorColumn(this.tabStops_[i]);
1343 return;
1344 }
1345 }
1346
1347 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001348};
1349
rgindac9bc5502012-01-18 11:48:44 -08001350/**
1351 * Set a tab stop at the given column.
1352 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001353 * @param {integer} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001354 */
1355hterm.Terminal.prototype.setTabStop = function(column) {
1356 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1357 if (this.tabStops_[i] == column)
1358 return;
1359
1360 if (this.tabStops_[i] < column) {
1361 this.tabStops_.splice(i + 1, 0, column);
1362 return;
1363 }
1364 }
1365
1366 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001367};
1368
rgindac9bc5502012-01-18 11:48:44 -08001369/**
1370 * Clear the tab stop at the current cursor position.
1371 *
1372 * No effect if there is no tab stop at the current cursor position.
1373 */
1374hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1375 var column = this.screen_.cursorPosition.column;
1376
1377 var i = this.tabStops_.indexOf(column);
1378 if (i == -1)
1379 return;
1380
1381 this.tabStops_.splice(i, 1);
1382};
1383
1384/**
1385 * Clear all tab stops.
1386 */
1387hterm.Terminal.prototype.clearAllTabStops = function() {
1388 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001389 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001390};
1391
1392/**
1393 * Set up the default tab stops, starting from a given column.
1394 *
1395 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001396 * from the specified column, or 0 if no column is provided. It also flags
1397 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001398 *
1399 * This does not clear the existing tab stops first, use clearAllTabStops
1400 * for that.
1401 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001402 * @param {integer} opt_start Optional starting zero based starting column, useful
rgindac9bc5502012-01-18 11:48:44 -08001403 * for filling out missing tab stops when the terminal is resized.
1404 */
1405hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1406 var start = opt_start || 0;
1407 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001408 // Round start up to a default tab stop.
1409 start = start - 1 - ((start - 1) % w) + w;
1410 for (var i = start; i < this.screenSize.width; i += w) {
1411 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001412 }
David Benjamin66e954d2012-05-05 21:08:12 -04001413
1414 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001415};
1416
rginda6d397402012-01-17 10:58:29 -08001417/**
rginda8ba33642011-12-14 12:31:31 -08001418 * Interpret a sequence of characters.
1419 *
1420 * Incomplete escape sequences are buffered until the next call.
1421 *
1422 * @param {string} str Sequence of characters to interpret or pass through.
1423 */
1424hterm.Terminal.prototype.interpret = function(str) {
rginda8ba33642011-12-14 12:31:31 -08001425 this.scheduleSyncCursorPosition_();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001426 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001427};
1428
1429/**
1430 * Take over the given DIV for use as the terminal display.
1431 *
1432 * @param {HTMLDivElement} div The div to use as the terminal display.
1433 */
1434hterm.Terminal.prototype.decorate = function(div) {
Mike Frysinger5768a9d2017-12-26 12:57:44 -05001435 const charset = div.ownerDocument.characterSet.toLowerCase();
1436 if (charset != 'utf-8') {
1437 console.warn(`Document encoding should be set to utf-8, not "${charset}";` +
1438 ` Add <meta charset='utf-8'/> to your HTML <head> to fix.`);
1439 }
1440
rginda87b86462011-12-14 13:48:03 -08001441 this.div_ = div;
1442
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001443 this.accessibilityReader_ = new hterm.AccessibilityReader(div);
1444
rginda8ba33642011-12-14 12:31:31 -08001445 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001446 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001447 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1448 this.scrollPort_.setBackgroundPosition(
1449 this.prefs_.get('background-position'));
Mike Frysinger08bad432017-04-24 00:50:54 -04001450 this.scrollPort_.setUserCssUrl(this.prefs_.get('user-css'));
1451 this.scrollPort_.setUserCssText(this.prefs_.get('user-css-text'));
Raymes Khoury177aec72018-06-26 10:58:53 +10001452 this.scrollPort_.setAccessibilityReader(this.accessibilityReader_);
rginda30f20f62012-04-05 16:36:19 -07001453
rginda0918b652012-04-04 11:26:24 -07001454 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001455
rginda9f5222b2012-03-05 11:53:28 -08001456 this.setFontSize(this.prefs_.get('font-size'));
1457 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001458
David Reveman8f552492012-03-28 12:18:41 -04001459 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001460 this.setScrollWheelMoveMultipler(
1461 this.prefs_.get('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001462
rginda8ba33642011-12-14 12:31:31 -08001463 this.document_ = this.scrollPort_.getDocument();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001464 this.accessibilityReader_.decorate(this.document_);
rginda8ba33642011-12-14 12:31:31 -08001465
Evan Jones5f9df812016-12-06 09:38:58 -05001466 this.document_.body.oncontextmenu = function() { return false; };
rginda4bba5e12012-06-20 16:15:30 -07001467
1468 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001469 var screenNode = this.scrollPort_.getScreenNode();
1470 screenNode.addEventListener('mousedown', onMouse);
1471 screenNode.addEventListener('mouseup', onMouse);
1472 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001473 this.scrollPort_.onScrollWheel = onMouse;
1474
Mike Frysinger02ded6d2018-06-21 14:25:20 -04001475 screenNode.addEventListener('keydown', this.onKeyboardActivity_.bind(this));
1476
Toni Barzic0bfa8922013-11-22 11:18:35 -08001477 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001478 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001479 // Listen for mousedown events on the screenNode as in FF the focus
1480 // events don't bubble.
1481 screenNode.addEventListener('mousedown', function() {
1482 setTimeout(this.onFocusChange_.bind(this, true));
1483 }.bind(this));
1484
Toni Barzic0bfa8922013-11-22 11:18:35 -08001485 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001486 'blur', this.onFocusChange_.bind(this, false));
1487
1488 var style = this.document_.createElement('style');
1489 style.textContent =
1490 ('.cursor-node[focus="false"] {' +
1491 ' box-sizing: border-box;' +
1492 ' background-color: transparent !important;' +
1493 ' border-width: 2px;' +
1494 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001495 '}' +
1496 '.wc-node {' +
1497 ' display: inline-block;' +
1498 ' text-align: center;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001499 ' width: calc(var(--hterm-charsize-width) * 2);' +
Mike Frysinger4036f6e2017-05-31 14:02:55 -04001500 ' line-height: var(--hterm-charsize-height);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001501 '}' +
1502 ':root {' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001503 ' --hterm-charsize-width: ' + this.scrollPort_.characterSize.width + 'px;' +
1504 ' --hterm-charsize-height: ' + this.scrollPort_.characterSize.height + 'px;' +
Mike Frysingera27c0502017-08-23 22:37:10 -04001505 // Default position hides the cursor for when the window is initializing.
1506 ' --hterm-cursor-offset-col: -1;' +
1507 ' --hterm-cursor-offset-row: -1;' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001508 ' --hterm-blink-node-duration: 0.7s;' +
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001509 ' --hterm-mouse-cursor-text: text;' +
1510 ' --hterm-mouse-cursor-pointer: default;' +
1511 ' --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001512 '}' +
Mike Frysingerd3907202017-10-23 01:23:50 -04001513 '.uri-node:hover {' +
1514 ' text-decoration: underline;' +
Mike Frysingerb74a6472018-06-22 13:37:08 -04001515 ' cursor: var(--hterm-mouse-cursor-pointer), pointer;' +
Mike Frysingerd3907202017-10-23 01:23:50 -04001516 '}' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001517 '@keyframes blink {' +
1518 ' from { opacity: 1.0; }' +
1519 ' to { opacity: 0.0; }' +
1520 '}' +
1521 '.blink-node {' +
1522 ' animation-name: blink;' +
1523 ' animation-duration: var(--hterm-blink-node-duration);' +
1524 ' animation-iteration-count: infinite;' +
1525 ' animation-timing-function: ease-in-out;' +
1526 ' animation-direction: alternate;' +
rginda8e92a692012-05-20 19:37:20 -07001527 '}');
Mike Frysingerb74a6472018-06-22 13:37:08 -04001528 // Insert this stock style as the first node so that any user styles will
1529 // override w/out having to use !important everywhere. The rules above mix
1530 // runtime variables with default ones designed to be overridden by the user,
1531 // but we can wait for a concrete case from the users to determine the best
1532 // way to split the sheet up to before & after the user-css settings.
1533 this.document_.head.insertBefore(style, this.document_.head.firstChild);
rginda8e92a692012-05-20 19:37:20 -07001534
rginda8ba33642011-12-14 12:31:31 -08001535 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001536 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001537 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001538 this.cursorNode_.style.cssText =
1539 ('position: absolute;' +
Mike Frysinger44c32202017-08-05 01:13:09 -04001540 'left: calc(var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));' +
1541 'top: calc(var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));' +
Mike Frysingerd60f4c22018-03-15 21:25:30 -07001542 'display: ' + (this.options_.cursorVisible ? '' : 'none') + ';' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001543 'width: var(--hterm-charsize-width);' +
1544 'height: var(--hterm-charsize-height);' +
Rob Spies06533ba2014-04-24 11:20:37 -07001545 '-webkit-transition: opacity, background-color 100ms linear;' +
1546 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001547
Mike Frysingerf02a2cb2017-12-21 00:34:03 -05001548 this.setCursorColor();
Robert Gindafb1be6a2013-12-11 11:56:22 -08001549 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1550 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001551
rginda8ba33642011-12-14 12:31:31 -08001552 this.document_.body.appendChild(this.cursorNode_);
1553
rgindad5613292012-06-19 15:40:37 -07001554 // When 'enableMouseDragScroll' is off we reposition this element directly
1555 // under the mouse cursor after a click. This makes Chrome associate
1556 // subsequent mousemove events with the scroll-blocker. Since the
1557 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1558 // events do not cause the scrollport to scroll.
1559 //
1560 // It's a hack, but it's the cleanest way I could find.
1561 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001562 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
Raymes Khoury6dce2f82018-04-12 15:38:58 +10001563 this.scrollBlockerNode_.setAttribute('aria-hidden', 'true');
rgindad5613292012-06-19 15:40:37 -07001564 this.scrollBlockerNode_.style.cssText =
1565 ('position: absolute;' +
1566 'top: -99px;' +
1567 'display: block;' +
1568 'width: 10px;' +
1569 'height: 10px;');
1570 this.document_.body.appendChild(this.scrollBlockerNode_);
1571
rgindad5613292012-06-19 15:40:37 -07001572 this.scrollPort_.onScrollWheel = onMouse;
1573 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1574 ].forEach(function(event) {
1575 this.scrollBlockerNode_.addEventListener(event, onMouse);
1576 this.cursorNode_.addEventListener(event, onMouse);
1577 this.document_.addEventListener(event, onMouse);
1578 }.bind(this));
1579
1580 this.cursorNode_.addEventListener('mousedown', function() {
1581 setTimeout(this.focus.bind(this));
1582 }.bind(this));
1583
rginda8ba33642011-12-14 12:31:31 -08001584 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001585
rginda87b86462011-12-14 13:48:03 -08001586 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001587 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001588};
1589
rginda0918b652012-04-04 11:26:24 -07001590/**
1591 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001592 *
1593 * @return {HTMLDocument}
rginda0918b652012-04-04 11:26:24 -07001594 */
rginda87b86462011-12-14 13:48:03 -08001595hterm.Terminal.prototype.getDocument = function() {
1596 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001597};
1598
1599/**
rginda0918b652012-04-04 11:26:24 -07001600 * Focus the terminal.
1601 */
1602hterm.Terminal.prototype.focus = function() {
1603 this.scrollPort_.focus();
1604};
1605
1606/**
rginda8ba33642011-12-14 12:31:31 -08001607 * Return the HTML Element for a given row index.
1608 *
1609 * This is a method from the RowProvider interface. The ScrollPort uses
1610 * it to fetch rows on demand as they are scrolled into view.
1611 *
1612 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1613 * pairs to conserve memory.
1614 *
1615 * @param {integer} index The zero-based row index, measured relative to the
1616 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001617 * largest indices.
rginda8ba33642011-12-14 12:31:31 -08001618 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1619 */
1620hterm.Terminal.prototype.getRowNode = function(index) {
1621 if (index < this.scrollbackRows_.length)
1622 return this.scrollbackRows_[index];
1623
1624 var screenIndex = index - this.scrollbackRows_.length;
1625 return this.screen_.rowsArray[screenIndex];
1626};
1627
1628/**
1629 * Return the text content for a given range of rows.
1630 *
1631 * This is a method from the RowProvider interface. The ScrollPort uses
1632 * it to fetch text content on demand when the user attempts to copy their
1633 * selection to the clipboard.
1634 *
1635 * @param {integer} start The zero-based row index to start from, measured
1636 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001637 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001638 * @param {integer} end The zero-based row index to end on, measured
1639 * relative to the start of the scrollback buffer.
1640 * @return {string} A single string containing the text value of the range of
1641 * rows. Lines will be newline delimited, with no trailing newline.
1642 */
1643hterm.Terminal.prototype.getRowsText = function(start, end) {
1644 var ary = [];
1645 for (var i = start; i < end; i++) {
1646 var node = this.getRowNode(i);
1647 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001648 if (i < end - 1 && !node.getAttribute('line-overflow'))
1649 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001650 }
1651
rgindaa09e7332012-08-17 12:49:51 -07001652 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001653};
1654
1655/**
1656 * Return the text content for a given row.
1657 *
1658 * This is a method from the RowProvider interface. The ScrollPort uses
1659 * it to fetch text content on demand when the user attempts to copy their
1660 * selection to the clipboard.
1661 *
1662 * @param {integer} index The zero-based row index to return, measured
1663 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001664 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001665 * @return {string} A string containing the text value of the selected row.
1666 */
1667hterm.Terminal.prototype.getRowText = function(index) {
1668 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001669 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001670};
1671
1672/**
1673 * Return the total number of rows in the addressable screen and in the
1674 * scrollback buffer of this terminal.
1675 *
1676 * This is a method from the RowProvider interface. The ScrollPort uses
1677 * it to compute the size of the scrollbar.
1678 *
1679 * @return {integer} The number of rows in this terminal.
1680 */
1681hterm.Terminal.prototype.getRowCount = function() {
1682 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1683};
1684
1685/**
1686 * Create DOM nodes for new rows and append them to the end of the terminal.
1687 *
1688 * This is the only correct way to add a new DOM node for a row. Notice that
1689 * the new row is appended to the bottom of the list of rows, and does not
1690 * require renumbering (of the rowIndex property) of previous rows.
1691 *
1692 * If you think you want a new blank row somewhere in the middle of the
1693 * terminal, look into moveRows_().
1694 *
1695 * This method does not pay attention to vtScrollTop/Bottom, since you should
1696 * be using moveRows() in cases where they would matter.
1697 *
1698 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001699 *
1700 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001701 */
1702hterm.Terminal.prototype.appendRows_ = function(count) {
1703 var cursorRow = this.screen_.rowsArray.length;
1704 var offset = this.scrollbackRows_.length + cursorRow;
1705 for (var i = 0; i < count; i++) {
1706 var row = this.document_.createElement('x-row');
1707 row.appendChild(this.document_.createTextNode(''));
1708 row.rowIndex = offset + i;
1709 this.screen_.pushRow(row);
1710 }
1711
1712 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1713 if (extraRows > 0) {
1714 var ary = this.screen_.shiftRows(extraRows);
1715 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001716 if (this.scrollPort_.isScrolledEnd)
1717 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001718 }
1719
1720 if (cursorRow >= this.screen_.rowsArray.length)
1721 cursorRow = this.screen_.rowsArray.length - 1;
1722
rginda87b86462011-12-14 13:48:03 -08001723 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001724};
1725
1726/**
1727 * Relocate rows from one part of the addressable screen to another.
1728 *
1729 * This is used to recycle rows during VT scrolls (those which are driven
1730 * by VT commands, rather than by the user manipulating the scrollbar.)
1731 *
1732 * In this case, the blank lines scrolled into the scroll region are made of
1733 * the nodes we scrolled off. These have their rowIndex properties carefully
1734 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001735 *
1736 * @param {number} fromIndex The start index.
1737 * @param {number} count The number of rows to move.
1738 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001739 */
1740hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1741 var ary = this.screen_.removeRows(fromIndex, count);
1742 this.screen_.insertRows(toIndex, ary);
1743
1744 var start, end;
1745 if (fromIndex < toIndex) {
1746 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001747 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001748 } else {
1749 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001750 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001751 }
1752
1753 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001754 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001755};
1756
1757/**
1758 * Renumber the rowIndex property of the given range of rows.
1759 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001760 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08001761 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001762 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001763 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05001764 *
1765 * @param {number} start The start index.
1766 * @param {number} end The end index.
1767 * @param {hterm.Screen} opt_screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08001768 */
Robert Ginda40932892012-12-10 17:26:40 -08001769hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1770 var screen = opt_screen || this.screen_;
1771
rginda8ba33642011-12-14 12:31:31 -08001772 var offset = this.scrollbackRows_.length;
1773 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001774 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001775 }
1776};
1777
1778/**
1779 * Print a string to the terminal.
1780 *
1781 * This respects the current insert and wraparound modes. It will add new lines
1782 * to the end of the terminal, scrolling off the top into the scrollback buffer
1783 * if necessary.
1784 *
1785 * The string is *not* parsed for escape codes. Use the interpret() method if
1786 * that's what you're after.
1787 *
1788 * @param{string} str The string to print.
1789 */
1790hterm.Terminal.prototype.print = function(str) {
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001791 this.scheduleSyncCursorPosition_();
1792
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001793 // Basic accessibility output for the screen reader.
Raymes Khoury177aec72018-06-26 10:58:53 +10001794 this.accessibilityReader_.announce(str);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001795
rgindaa9abdd82012-08-06 18:05:09 -07001796 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001797
Ricky Liang48f05cb2013-12-31 23:35:29 +08001798 var strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04001799 // Fun edge case: If the string only contains zero width codepoints (like
1800 // combining characters), we make sure to iterate at least once below.
1801 if (strWidth == 0 && str)
1802 strWidth = 1;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001803
1804 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001805 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1806 this.screen_.commitLineOverflow();
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001807 this.newLine(true);
rgindaa09e7332012-08-17 12:49:51 -07001808 }
rgindaa19afe22012-01-25 15:40:22 -08001809
Ricky Liang48f05cb2013-12-31 23:35:29 +08001810 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001811 var didOverflow = false;
1812 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001813
rgindaa9abdd82012-08-06 18:05:09 -07001814 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1815 didOverflow = true;
1816 count = this.screenSize.width - this.screen_.cursorPosition.column;
1817 }
rgindaa19afe22012-01-25 15:40:22 -08001818
rgindaa9abdd82012-08-06 18:05:09 -07001819 if (didOverflow && !this.options_.wraparound) {
1820 // If the string overflowed the line but wraparound is off, then the
1821 // last printed character should be the last of the string.
1822 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001823 substr = lib.wc.substr(str, startOffset, count - 1) +
1824 lib.wc.substr(str, strWidth - 1);
1825 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001826 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001827 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001828 }
rgindaa19afe22012-01-25 15:40:22 -08001829
Ricky Liang48f05cb2013-12-31 23:35:29 +08001830 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1831 for (var i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001832 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
1833 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001834
1835 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001836 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001837 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001838 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001839 }
1840 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001841 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07001842 }
1843
1844 this.screen_.maybeClipCurrentRow();
1845 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001846 }
rginda8ba33642011-12-14 12:31:31 -08001847
rginda9f5222b2012-03-05 11:53:28 -08001848 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001849 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001850};
1851
1852/**
rginda87b86462011-12-14 13:48:03 -08001853 * Set the VT scroll region.
1854 *
rginda87b86462011-12-14 13:48:03 -08001855 * This also resets the cursor position to the absolute (0, 0) position, since
1856 * that's what xterm appears to do.
1857 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001858 * Setting the scroll region to the full height of the terminal will clear
1859 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1860 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1861 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1862 * continue to work as most users would expect.
1863 *
rginda87b86462011-12-14 13:48:03 -08001864 * @param {integer} scrollTop The zero-based top of the scroll region.
1865 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1866 * inclusive.
1867 */
1868hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001869 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001870 this.vtScrollTop_ = null;
1871 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001872 } else {
1873 this.vtScrollTop_ = scrollTop;
1874 this.vtScrollBottom_ = scrollBottom;
1875 }
rginda87b86462011-12-14 13:48:03 -08001876};
1877
1878/**
rginda8ba33642011-12-14 12:31:31 -08001879 * Return the top row index according to the VT.
1880 *
1881 * This will return 0 unless the terminal has been told to restrict scrolling
1882 * to some lower row. It is used for some VT cursor positioning and scrolling
1883 * commands.
1884 *
1885 * @return {integer} The topmost row in the terminal's scroll region.
1886 */
1887hterm.Terminal.prototype.getVTScrollTop = function() {
1888 if (this.vtScrollTop_ != null)
1889 return this.vtScrollTop_;
1890
1891 return 0;
rginda87b86462011-12-14 13:48:03 -08001892};
rginda8ba33642011-12-14 12:31:31 -08001893
1894/**
1895 * Return the bottom row index according to the VT.
1896 *
1897 * This will return the height of the terminal unless the it has been told to
1898 * restrict scrolling to some higher row. It is used for some VT cursor
1899 * positioning and scrolling commands.
1900 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001901 * @return {integer} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001902 */
1903hterm.Terminal.prototype.getVTScrollBottom = function() {
1904 if (this.vtScrollBottom_ != null)
1905 return this.vtScrollBottom_;
1906
rginda87b86462011-12-14 13:48:03 -08001907 return this.screenSize.height - 1;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001908};
rginda8ba33642011-12-14 12:31:31 -08001909
1910/**
1911 * Process a '\n' character.
1912 *
1913 * If the cursor is on the final row of the terminal this will append a new
1914 * blank row to the screen and scroll the topmost row into the scrollback
1915 * buffer.
1916 *
1917 * Otherwise, this moves the cursor to column zero of the next row.
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001918 *
1919 * @param {boolean=} dueToOverflow Whether the newline is due to wraparound of
1920 * the terminal.
rginda8ba33642011-12-14 12:31:31 -08001921 */
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001922hterm.Terminal.prototype.newLine = function(dueToOverflow = false) {
1923 if (!dueToOverflow)
1924 this.accessibilityReader_.newLine();
1925
Robert Ginda9937abc2013-07-25 16:09:23 -07001926 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1927 this.screen_.rowsArray.length - 1);
1928
1929 if (this.vtScrollBottom_ != null) {
1930 // A VT Scroll region is active, we never append new rows.
1931 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1932 // We're at the end of the VT Scroll Region, perform a VT scroll.
1933 this.vtScrollUp(1);
1934 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1935 } else if (cursorAtEndOfScreen) {
1936 // We're at the end of the screen, the only thing to do is put the
1937 // cursor to column 0.
1938 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1939 } else {
1940 // Anywhere else, advance the cursor row, and reset the column.
1941 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1942 }
1943 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001944 // We're at the end of the screen. Append a new row to the terminal,
1945 // shifting the top row into the scrollback.
1946 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001947 } else {
rginda87b86462011-12-14 13:48:03 -08001948 // Anywhere else in the screen just moves the cursor.
1949 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001950 }
1951};
1952
1953/**
1954 * Like newLine(), except maintain the cursor column.
1955 */
1956hterm.Terminal.prototype.lineFeed = function() {
1957 var column = this.screen_.cursorPosition.column;
1958 this.newLine();
1959 this.setCursorColumn(column);
1960};
1961
1962/**
rginda87b86462011-12-14 13:48:03 -08001963 * If autoCarriageReturn is set then newLine(), else lineFeed().
1964 */
1965hterm.Terminal.prototype.formFeed = function() {
1966 if (this.options_.autoCarriageReturn) {
1967 this.newLine();
1968 } else {
1969 this.lineFeed();
1970 }
1971};
1972
1973/**
1974 * Move the cursor up one row, possibly inserting a blank line.
1975 *
1976 * The cursor column is not changed.
1977 */
1978hterm.Terminal.prototype.reverseLineFeed = function() {
1979 var scrollTop = this.getVTScrollTop();
1980 var currentRow = this.screen_.cursorPosition.row;
1981
1982 if (currentRow == scrollTop) {
1983 this.insertLines(1);
1984 } else {
1985 this.setAbsoluteCursorRow(currentRow - 1);
1986 }
1987};
1988
1989/**
rginda8ba33642011-12-14 12:31:31 -08001990 * Replace all characters to the left of the current cursor with the space
1991 * character.
1992 *
1993 * TODO(rginda): This should probably *remove* the characters (not just replace
1994 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001995 * position.
rginda8ba33642011-12-14 12:31:31 -08001996 */
1997hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001998 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001999 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002000 const count = cursor.column + 1;
2001 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08002002 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002003};
2004
2005/**
David Benjamin684a9b72012-05-01 17:19:58 -04002006 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08002007 *
2008 * The cursor position is unchanged.
2009 *
Robert Gindaf2547f12012-10-25 20:36:21 -07002010 * If the current background color is not the default background color this
2011 * will insert spaces rather than delete. This is unfortunate because the
2012 * trailing space will affect text selection, but it's difficult to come up
2013 * with a way to style empty space that wouldn't trip up the hterm.Screen
2014 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07002015 *
2016 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
2017 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
2018 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05002019 *
2020 * @param {number} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08002021 */
2022hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07002023 if (this.screen_.cursorPosition.overflow)
2024 return;
2025
Robert Ginda7fd57082012-09-25 14:41:47 -07002026 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
2027 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07002028
2029 if (this.screen_.textAttributes.background ===
2030 this.screen_.textAttributes.DEFAULT_COLOR) {
2031 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08002032 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07002033 this.screen_.cursorPosition.column + count) {
2034 this.screen_.deleteChars(count);
2035 this.clearCursorOverflow();
2036 return;
2037 }
2038 }
2039
rginda87b86462011-12-14 13:48:03 -08002040 var cursor = this.saveCursor();
Mike Frysinger6380bed2017-08-24 18:46:39 -04002041 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08002042 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002043 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002044};
2045
2046/**
2047 * Erase the current line.
2048 *
2049 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002050 */
2051hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08002052 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002053 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08002054 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002055 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002056};
2057
2058/**
David Benjamina08d78f2012-05-05 00:28:49 -04002059 * Erase all characters from the start of the screen to the current cursor
2060 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002061 *
2062 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002063 */
2064hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08002065 var cursor = this.saveCursor();
2066
2067 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002068
David Benjamina08d78f2012-05-05 00:28:49 -04002069 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002070 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002071 this.screen_.clearCursorRow();
2072 }
2073
rginda87b86462011-12-14 13:48:03 -08002074 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002075 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002076};
2077
2078/**
2079 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002080 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002081 *
2082 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002083 */
2084hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08002085 var cursor = this.saveCursor();
2086
2087 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002088
David Benjamina08d78f2012-05-05 00:28:49 -04002089 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08002090 for (var i = cursor.row + 1; i <= bottom; i++) {
2091 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002092 this.screen_.clearCursorRow();
2093 }
2094
rginda87b86462011-12-14 13:48:03 -08002095 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002096 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002097};
2098
2099/**
2100 * Fill the terminal with a given character.
2101 *
2102 * This methods does not respect the VT scroll region.
2103 *
2104 * @param {string} ch The character to use for the fill.
2105 */
2106hterm.Terminal.prototype.fill = function(ch) {
2107 var cursor = this.saveCursor();
2108
2109 this.setAbsoluteCursorPosition(0, 0);
2110 for (var row = 0; row < this.screenSize.height; row++) {
2111 for (var col = 0; col < this.screenSize.width; col++) {
2112 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002113 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002114 }
2115 }
2116
2117 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002118};
2119
2120/**
rginda9ea433c2012-03-16 11:57:00 -07002121 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002122 *
rginda9ea433c2012-03-16 11:57:00 -07002123 * This does not respect the scroll region.
2124 *
2125 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2126 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002127 */
rginda9ea433c2012-03-16 11:57:00 -07002128hterm.Terminal.prototype.clearHome = function(opt_screen) {
2129 var screen = opt_screen || this.screen_;
2130 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002131
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002132 this.accessibilityReader_.clear();
2133
rginda11057d52012-04-25 12:29:56 -07002134 if (bottom == 0) {
2135 // Empty screen, nothing to do.
2136 return;
2137 }
2138
rgindae4d29232012-01-19 10:47:13 -08002139 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002140 screen.setCursorPosition(i, 0);
2141 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002142 }
2143
rginda9ea433c2012-03-16 11:57:00 -07002144 screen.setCursorPosition(0, 0);
2145};
2146
2147/**
2148 * Erase the entire display without changing the cursor position.
2149 *
2150 * The cursor position is unchanged. This does not respect the scroll
2151 * region.
2152 *
2153 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2154 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002155 */
2156hterm.Terminal.prototype.clear = function(opt_screen) {
2157 var screen = opt_screen || this.screen_;
2158 var cursor = screen.cursorPosition.clone();
2159 this.clearHome(screen);
2160 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002161};
2162
2163/**
2164 * VT command to insert lines at the current cursor row.
2165 *
2166 * This respects the current scroll region. Rows pushed off the bottom are
2167 * lost (they won't show up in the scrollback buffer).
2168 *
rginda8ba33642011-12-14 12:31:31 -08002169 * @param {integer} count The number of lines to insert.
2170 */
2171hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07002172 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002173
2174 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002175 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002176
Robert Ginda579186b2012-09-26 11:40:04 -07002177 // The moveCount is the number of rows we need to relocate to make room for
2178 // the new row(s). The count is the distance to move them.
2179 var moveCount = bottom - cursorRow - count + 1;
2180 if (moveCount)
2181 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08002182
Robert Ginda579186b2012-09-26 11:40:04 -07002183 for (var i = count - 1; i >= 0; i--) {
2184 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002185 this.screen_.clearCursorRow();
2186 }
rginda8ba33642011-12-14 12:31:31 -08002187};
2188
2189/**
2190 * VT command to delete lines at the current cursor row.
2191 *
2192 * New rows are added to the bottom of scroll region to take their place. New
2193 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002194 *
2195 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002196 */
2197hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08002198 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002199
rginda87b86462011-12-14 13:48:03 -08002200 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08002201 var bottom = this.getVTScrollBottom();
2202
rginda87b86462011-12-14 13:48:03 -08002203 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002204 count = Math.min(count, maxCount);
2205
rginda87b86462011-12-14 13:48:03 -08002206 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08002207 if (count != maxCount)
2208 this.moveRows_(top, count, moveStart);
2209
2210 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002211 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002212 this.screen_.clearCursorRow();
2213 }
2214
rginda87b86462011-12-14 13:48:03 -08002215 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002216 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002217};
2218
2219/**
2220 * Inserts the given number of spaces at the current cursor position.
2221 *
rginda87b86462011-12-14 13:48:03 -08002222 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002223 *
2224 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002225 */
2226hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08002227 var cursor = this.saveCursor();
2228
rgindacbbd7482012-06-13 15:06:16 -07002229 var ws = lib.f.getWhitespace(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002230 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002231 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002232
2233 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002234 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002235};
2236
2237/**
2238 * Forward-delete the specified number of characters starting at the cursor
2239 * position.
2240 *
2241 * @param {integer} count The number of characters to delete.
2242 */
2243hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002244 var deleted = this.screen_.deleteChars(count);
2245 if (deleted && !this.screen_.textAttributes.isDefault()) {
2246 var cursor = this.saveCursor();
2247 this.setCursorColumn(this.screenSize.width - deleted);
2248 this.screen_.insertString(lib.f.getWhitespace(deleted));
2249 this.restoreCursor(cursor);
2250 }
2251
David Benjamin54e8bf62012-06-01 22:31:40 -04002252 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002253};
2254
2255/**
2256 * Shift rows in the scroll region upwards by a given number of lines.
2257 *
2258 * New rows are inserted at the bottom of the scroll region to fill the
2259 * vacated rows. The new rows not filled out with the current text attributes.
2260 *
2261 * This function does not affect the scrollback rows at all. Rows shifted
2262 * off the top are lost.
2263 *
rginda87b86462011-12-14 13:48:03 -08002264 * The cursor position is not altered.
2265 *
rginda8ba33642011-12-14 12:31:31 -08002266 * @param {integer} count The number of rows to scroll.
2267 */
2268hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002269 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002270
rginda87b86462011-12-14 13:48:03 -08002271 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002272 this.deleteLines(count);
2273
rginda87b86462011-12-14 13:48:03 -08002274 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002275};
2276
2277/**
2278 * Shift rows below the cursor down by a given number of lines.
2279 *
2280 * This function respects the current scroll region.
2281 *
2282 * New rows are inserted at the top of the scroll region to fill the
2283 * vacated rows. The new rows not filled out with the current text attributes.
2284 *
2285 * This function does not affect the scrollback rows at all. Rows shifted
2286 * off the bottom are lost.
2287 *
2288 * @param {integer} count The number of rows to scroll.
2289 */
2290hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08002291 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002292
rginda87b86462011-12-14 13:48:03 -08002293 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08002294 this.insertLines(opt_count);
2295
rginda87b86462011-12-14 13:48:03 -08002296 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002297};
2298
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002299/**
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002300 * Enable accessibility-friendly features that have a performance impact.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002301 *
2302 * This will generate additional DOM nodes in an aria-live region that will
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002303 * cause Assitive Technology to announce the output of the terminal. It also
2304 * enables other features that aid assistive technology. All the features gated
2305 * behind this flag have a performance impact on the terminal which is why they
2306 * are made optional.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002307 *
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002308 * @param {boolean} enabled Whether to enable accessibility-friendly features.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002309 */
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002310hterm.Terminal.prototype.setAccessibilityEnabled = function(enabled) {
Raymes Khoury177aec72018-06-26 10:58:53 +10002311 this.accessibilityReader_.setAccessibilityEnabled(enabled);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002312};
rginda87b86462011-12-14 13:48:03 -08002313
rginda8ba33642011-12-14 12:31:31 -08002314/**
2315 * Set the cursor position.
2316 *
2317 * The cursor row is relative to the scroll region if the terminal has
2318 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2319 *
2320 * @param {integer} row The new zero-based cursor row.
2321 * @param {integer} row The new zero-based cursor column.
2322 */
2323hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2324 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002325 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002326 } else {
rginda87b86462011-12-14 13:48:03 -08002327 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002328 }
rginda87b86462011-12-14 13:48:03 -08002329};
rginda8ba33642011-12-14 12:31:31 -08002330
Evan Jones2600d4f2016-12-06 09:29:36 -05002331/**
2332 * Move the cursor relative to its current position.
2333 *
2334 * @param {number} row
2335 * @param {number} column
2336 */
rginda87b86462011-12-14 13:48:03 -08002337hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2338 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002339 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2340 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002341 this.screen_.setCursorPosition(row, column);
2342};
2343
Evan Jones2600d4f2016-12-06 09:29:36 -05002344/**
2345 * Move the cursor to the specified position.
2346 *
2347 * @param {number} row
2348 * @param {number} column
2349 */
rginda87b86462011-12-14 13:48:03 -08002350hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002351 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2352 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002353 this.screen_.setCursorPosition(row, column);
2354};
2355
2356/**
2357 * Set the cursor column.
2358 *
2359 * @param {integer} column The new zero-based cursor column.
2360 */
2361hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002362 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002363};
2364
2365/**
2366 * Return the cursor column.
2367 *
2368 * @return {integer} The zero-based cursor column.
2369 */
2370hterm.Terminal.prototype.getCursorColumn = function() {
2371 return this.screen_.cursorPosition.column;
2372};
2373
2374/**
2375 * Set the cursor row.
2376 *
2377 * The cursor row is relative to the scroll region if the terminal has
2378 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2379 *
2380 * @param {integer} row The new cursor row.
2381 */
rginda87b86462011-12-14 13:48:03 -08002382hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2383 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002384};
2385
2386/**
2387 * Return the cursor row.
2388 *
2389 * @return {integer} The zero-based cursor row.
2390 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002391hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002392 return this.screen_.cursorPosition.row;
2393};
2394
2395/**
2396 * Request that the ScrollPort redraw itself soon.
2397 *
2398 * The redraw will happen asynchronously, soon after the call stack winds down.
2399 * Multiple calls will be coalesced into a single redraw.
2400 */
2401hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002402 if (this.timeouts_.redraw)
2403 return;
rginda8ba33642011-12-14 12:31:31 -08002404
2405 var self = this;
rginda87b86462011-12-14 13:48:03 -08002406 this.timeouts_.redraw = setTimeout(function() {
2407 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002408 self.scrollPort_.redraw_();
2409 }, 0);
2410};
2411
2412/**
2413 * Request that the ScrollPort be scrolled to the bottom.
2414 *
2415 * The scroll will happen asynchronously, soon after the call stack winds down.
2416 * Multiple calls will be coalesced into a single scroll.
2417 *
2418 * This affects the scrollbar position of the ScrollPort, and has nothing to
2419 * do with the VT scroll commands.
2420 */
2421hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2422 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002423 return;
rginda8ba33642011-12-14 12:31:31 -08002424
2425 var self = this;
2426 this.timeouts_.scrollDown = setTimeout(function() {
2427 delete self.timeouts_.scrollDown;
2428 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2429 }, 10);
2430};
2431
2432/**
2433 * Move the cursor up a specified number of rows.
2434 *
2435 * @param {integer} count The number of rows to move the cursor.
2436 */
2437hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002438 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002439};
2440
2441/**
2442 * Move the cursor down a specified number of rows.
2443 *
2444 * @param {integer} count The number of rows to move the cursor.
2445 */
2446hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002447 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002448 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2449 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2450 this.screenSize.height - 1);
2451
rgindacbbd7482012-06-13 15:06:16 -07002452 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002453 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002454 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002455};
2456
2457/**
2458 * Move the cursor left a specified number of columns.
2459 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002460 * If reverse wraparound mode is enabled and the previous row wrapped into
2461 * the current row then we back up through the wraparound as well.
2462 *
rginda8ba33642011-12-14 12:31:31 -08002463 * @param {integer} count The number of columns to move the cursor.
2464 */
2465hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002466 count = count || 1;
2467
2468 if (count < 1)
2469 return;
2470
2471 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002472 if (this.options_.reverseWraparound) {
2473 if (this.screen_.cursorPosition.overflow) {
2474 // If this cursor is in the right margin, consume one count to get it
2475 // back to the last column. This only applies when we're in reverse
2476 // wraparound mode.
2477 count--;
2478 this.clearCursorOverflow();
2479
2480 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002481 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002482 }
2483
Robert Gindabfb32622014-07-17 13:20:27 -07002484 var newRow = this.screen_.cursorPosition.row;
2485 var newColumn = currentColumn - count;
2486 if (newColumn < 0) {
2487 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2488 if (newRow < 0) {
2489 // xterm also wraps from row 0 to the last row.
2490 newRow = this.screenSize.height + newRow % this.screenSize.height;
2491 }
2492 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2493 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002494
Robert Gindabfb32622014-07-17 13:20:27 -07002495 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2496
2497 } else {
2498 var newColumn = Math.max(currentColumn - count, 0);
2499 this.setCursorColumn(newColumn);
2500 }
rginda8ba33642011-12-14 12:31:31 -08002501};
2502
2503/**
2504 * Move the cursor right a specified number of columns.
2505 *
2506 * @param {integer} count The number of columns to move the cursor.
2507 */
2508hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002509 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002510
2511 if (count < 1)
2512 return;
2513
rgindacbbd7482012-06-13 15:06:16 -07002514 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002515 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002516 this.setCursorColumn(column);
2517};
2518
2519/**
2520 * Reverse the foreground and background colors of the terminal.
2521 *
2522 * This only affects text that was drawn with no attributes.
2523 *
2524 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2525 * been drawn with attributes that happen to coincide with the default
2526 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002527 *
2528 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002529 */
2530hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002531 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002532 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002533 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2534 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002535 } else {
rginda9f5222b2012-03-05 11:53:28 -08002536 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2537 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002538 }
2539};
2540
2541/**
rginda87b86462011-12-14 13:48:03 -08002542 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002543 *
2544 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002545 */
2546hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002547 this.cursorNode_.style.backgroundColor =
2548 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002549
2550 var self = this;
2551 setTimeout(function() {
Matheus Fernandes2d733082017-09-11 06:43:01 -04002552 self.restyleCursor_();
rginda6d397402012-01-17 10:58:29 -08002553 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002554
Michael Kelly485ecd12014-06-09 11:41:56 -04002555 // bellSquelchTimeout_ affects both audio and notification bells.
2556 if (this.bellSquelchTimeout_)
2557 return;
2558
Robert Ginda92e18102013-03-14 13:56:37 -07002559 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002560 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002561 this.bellSequelchTimeout_ = setTimeout(function() {
2562 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002563 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002564 } else {
2565 delete this.bellSquelchTimeout_;
2566 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002567
2568 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingera5fb83c2017-06-22 14:48:35 -07002569 var n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002570 this.bellNotificationList_.push(n);
2571 // TODO: Should we try to raise the window here?
2572 n.onclick = function() { self.closeBellNotifications_(); };
2573 }
rginda87b86462011-12-14 13:48:03 -08002574};
2575
2576/**
rginda8ba33642011-12-14 12:31:31 -08002577 * Set the origin mode bit.
2578 *
2579 * If origin mode is on, certain VT cursor and scrolling commands measure their
2580 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2581 * to the top of the addressable screen.
2582 *
2583 * Defaults to off.
2584 *
2585 * @param {boolean} state True to set origin mode, false to unset.
2586 */
2587hterm.Terminal.prototype.setOriginMode = function(state) {
2588 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002589 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002590};
2591
2592/**
2593 * Set the insert mode bit.
2594 *
2595 * If insert mode is on, existing text beyond the cursor position will be
2596 * shifted right to make room for new text. Otherwise, new text overwrites
2597 * any existing text.
2598 *
2599 * Defaults to off.
2600 *
2601 * @param {boolean} state True to set insert mode, false to unset.
2602 */
2603hterm.Terminal.prototype.setInsertMode = function(state) {
2604 this.options_.insertMode = state;
2605};
2606
2607/**
rginda87b86462011-12-14 13:48:03 -08002608 * Set the auto carriage return bit.
2609 *
2610 * If auto carriage return is on then a formfeed character is interpreted
2611 * as a newline, otherwise it's the same as a linefeed. The difference boils
2612 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002613 *
2614 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002615 */
2616hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2617 this.options_.autoCarriageReturn = state;
2618};
2619
2620/**
rginda8ba33642011-12-14 12:31:31 -08002621 * Set the wraparound mode bit.
2622 *
2623 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2624 * to the start of the following row. Otherwise, the cursor is clamped to the
2625 * end of the screen and attempts to write past it are ignored.
2626 *
2627 * Defaults to on.
2628 *
2629 * @param {boolean} state True to set wraparound mode, false to unset.
2630 */
2631hterm.Terminal.prototype.setWraparound = function(state) {
2632 this.options_.wraparound = state;
2633};
2634
2635/**
2636 * Set the reverse-wraparound mode bit.
2637 *
2638 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2639 * to the end of the previous row. Otherwise, the cursor is clamped to column
2640 * 0.
2641 *
2642 * Defaults to off.
2643 *
2644 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2645 */
2646hterm.Terminal.prototype.setReverseWraparound = function(state) {
2647 this.options_.reverseWraparound = state;
2648};
2649
2650/**
2651 * Selects between the primary and alternate screens.
2652 *
2653 * If alternate mode is on, the alternate screen is active. Otherwise the
2654 * primary screen is active.
2655 *
2656 * Swapping screens has no effect on the scrollback buffer.
2657 *
2658 * Each screen maintains its own cursor position.
2659 *
2660 * Defaults to off.
2661 *
2662 * @param {boolean} state True to set alternate mode, false to unset.
2663 */
2664hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002665 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002666 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2667
rginda35c456b2012-02-09 17:29:05 -08002668 if (this.screen_.rowsArray.length &&
2669 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2670 // If the screen changed sizes while we were away, our rowIndexes may
2671 // be incorrect.
2672 var offset = this.scrollbackRows_.length;
2673 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002674 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002675 ary[i].rowIndex = offset + i;
2676 }
2677 }
rginda8ba33642011-12-14 12:31:31 -08002678
rginda35c456b2012-02-09 17:29:05 -08002679 this.realizeWidth_(this.screenSize.width);
2680 this.realizeHeight_(this.screenSize.height);
2681 this.scrollPort_.syncScrollHeight();
2682 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002683
rginda6d397402012-01-17 10:58:29 -08002684 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002685 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002686};
2687
2688/**
2689 * Set the cursor-blink mode bit.
2690 *
2691 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2692 * a visible cursor does not blink.
2693 *
2694 * You should make sure to turn blinking off if you're going to dispose of a
2695 * terminal, otherwise you'll leak a timeout.
2696 *
2697 * Defaults to on.
2698 *
2699 * @param {boolean} state True to set cursor-blink mode, false to unset.
2700 */
2701hterm.Terminal.prototype.setCursorBlink = function(state) {
2702 this.options_.cursorBlink = state;
2703
2704 if (!state && this.timeouts_.cursorBlink) {
2705 clearTimeout(this.timeouts_.cursorBlink);
2706 delete this.timeouts_.cursorBlink;
2707 }
2708
2709 if (this.options_.cursorVisible)
2710 this.setCursorVisible(true);
2711};
2712
2713/**
2714 * Set the cursor-visible mode bit.
2715 *
2716 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2717 *
2718 * Defaults to on.
2719 *
2720 * @param {boolean} state True to set cursor-visible mode, false to unset.
2721 */
2722hterm.Terminal.prototype.setCursorVisible = function(state) {
2723 this.options_.cursorVisible = state;
2724
2725 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002726 if (this.timeouts_.cursorBlink) {
2727 clearTimeout(this.timeouts_.cursorBlink);
2728 delete this.timeouts_.cursorBlink;
2729 }
rginda87b86462011-12-14 13:48:03 -08002730 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002731 return;
2732 }
2733
rginda87b86462011-12-14 13:48:03 -08002734 this.syncCursorPosition_();
2735
2736 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002737
2738 if (this.options_.cursorBlink) {
2739 if (this.timeouts_.cursorBlink)
2740 return;
2741
Robert Gindaea2183e2014-07-17 09:51:51 -07002742 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002743 } else {
2744 if (this.timeouts_.cursorBlink) {
2745 clearTimeout(this.timeouts_.cursorBlink);
2746 delete this.timeouts_.cursorBlink;
2747 }
2748 }
2749};
2750
2751/**
rginda87b86462011-12-14 13:48:03 -08002752 * Synchronizes the visible cursor and document selection with the current
2753 * cursor coordinates.
Raymes Khourye5d48982018-08-02 09:08:32 +10002754 *
2755 * @return {boolean} True if the cursor is onscreen and synced.
rginda8ba33642011-12-14 12:31:31 -08002756 */
2757hterm.Terminal.prototype.syncCursorPosition_ = function() {
2758 var topRowIndex = this.scrollPort_.getTopRowIndex();
2759 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2760 var cursorRowIndex = this.scrollbackRows_.length +
2761 this.screen_.cursorPosition.row;
2762
Raymes Khoury15697f42018-07-17 11:37:18 +10002763 let forceSyncSelection = false;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002764 if (this.accessibilityReader_.accessibilityEnabled) {
2765 // Report the new position of the cursor for accessibility purposes.
2766 const cursorColumnIndex = this.screen_.cursorPosition.column;
2767 const cursorLineText =
2768 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
Raymes Khoury15697f42018-07-17 11:37:18 +10002769 // This will force the selection to be sync'd to the cursor position if the
2770 // user has pressed a key. Generally we would only sync the cursor position
2771 // when selection is collapsed so that if the user has selected something
2772 // we don't clear the selection by moving the selection. However when a
2773 // screen reader is used, it's intuitive for entering a key to move the
2774 // selection to the cursor.
2775 forceSyncSelection = this.accessibilityReader_.hasUserGesture;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002776 this.accessibilityReader_.afterCursorChange(
2777 cursorLineText, cursorRowIndex, cursorColumnIndex);
2778 }
2779
rginda8ba33642011-12-14 12:31:31 -08002780 if (cursorRowIndex > bottomRowIndex) {
2781 // Cursor is scrolled off screen, move it outside of the visible area.
Mike Frysinger44c32202017-08-05 01:13:09 -04002782 this.setCssVar('cursor-offset-row', '-1');
Raymes Khourye5d48982018-08-02 09:08:32 +10002783 return false;
rginda8ba33642011-12-14 12:31:31 -08002784 }
2785
Robert Gindab837c052014-08-11 11:17:51 -07002786 if (this.options_.cursorVisible &&
2787 this.cursorNode_.style.display == 'none') {
2788 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2789 this.cursorNode_.style.display = '';
2790 }
2791
Mike Frysinger44c32202017-08-05 01:13:09 -04002792 // Position the cursor using CSS variable math. If we do the math in JS,
2793 // the float math will end up being more precise than the CSS which will
2794 // cause the cursor tracking to be off.
2795 this.setCssVar(
2796 'cursor-offset-row',
2797 `${cursorRowIndex - topRowIndex} + ` +
2798 `${this.scrollPort_.visibleRowTopMargin}px`);
2799 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08002800
2801 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04002802 '(' + this.screen_.cursorPosition.column +
2803 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08002804 ')');
2805
2806 // Update the caret for a11y purposes.
2807 var selection = this.document_.getSelection();
Raymes Khoury15697f42018-07-17 11:37:18 +10002808 if (selection && (selection.isCollapsed || forceSyncSelection)) {
rginda87b86462011-12-14 13:48:03 -08002809 this.screen_.syncSelectionCaret(selection);
Raymes Khoury15697f42018-07-17 11:37:18 +10002810 }
Raymes Khourye5d48982018-08-02 09:08:32 +10002811 return true;
rginda8ba33642011-12-14 12:31:31 -08002812};
2813
Robert Gindafb1be6a2013-12-11 11:56:22 -08002814/**
2815 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2816 * and character cell dimensions.
2817 */
Robert Ginda830583c2013-08-07 13:20:46 -07002818hterm.Terminal.prototype.restyleCursor_ = function() {
2819 var shape = this.cursorShape_;
2820
2821 if (this.cursorNode_.getAttribute('focus') == 'false') {
2822 // Always show a block cursor when unfocused.
2823 shape = hterm.Terminal.cursorShape.BLOCK;
2824 }
2825
2826 var style = this.cursorNode_.style;
2827
2828 switch (shape) {
2829 case hterm.Terminal.cursorShape.BEAM:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002830 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002831 style.backgroundColor = 'transparent';
2832 style.borderBottomStyle = null;
2833 style.borderLeftStyle = 'solid';
2834 break;
2835
2836 case hterm.Terminal.cursorShape.UNDERLINE:
2837 style.height = this.scrollPort_.characterSize.baseline + 'px';
2838 style.backgroundColor = 'transparent';
2839 style.borderBottomStyle = 'solid';
2840 // correct the size to put it exactly at the baseline
2841 style.borderLeftStyle = null;
2842 break;
2843
2844 default:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002845 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002846 style.backgroundColor = this.cursorColor_;
2847 style.borderBottomStyle = null;
2848 style.borderLeftStyle = null;
2849 break;
2850 }
2851};
2852
rginda8ba33642011-12-14 12:31:31 -08002853/**
2854 * Synchronizes the visible cursor with the current cursor coordinates.
2855 *
2856 * The sync will happen asynchronously, soon after the call stack winds down.
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002857 * Multiple calls will be coalesced into a single sync. This should be called
2858 * prior to the cursor actually changing position.
rginda8ba33642011-12-14 12:31:31 -08002859 */
2860hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2861 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002862 return;
rginda8ba33642011-12-14 12:31:31 -08002863
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002864 if (this.accessibilityReader_.accessibilityEnabled) {
2865 // Report the previous position of the cursor for accessibility purposes.
2866 const cursorRowIndex = this.scrollbackRows_.length +
2867 this.screen_.cursorPosition.row;
2868 const cursorColumnIndex = this.screen_.cursorPosition.column;
2869 const cursorLineText =
2870 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
2871 this.accessibilityReader_.beforeCursorChange(
2872 cursorLineText, cursorRowIndex, cursorColumnIndex);
2873 }
2874
rginda8ba33642011-12-14 12:31:31 -08002875 var self = this;
2876 this.timeouts_.syncCursor = setTimeout(function() {
2877 self.syncCursorPosition_();
2878 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002879 }, 0);
2880};
2881
rgindacc2996c2012-02-24 14:59:31 -08002882/**
rgindaf522ce02012-04-17 17:49:17 -07002883 * Show or hide the zoom warning.
2884 *
2885 * The zoom warning is a message warning the user that their browser zoom must
2886 * be set to 100% in order for hterm to function properly.
2887 *
2888 * @param {boolean} state True to show the message, false to hide it.
2889 */
2890hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2891 if (!this.zoomWarningNode_) {
2892 if (!state)
2893 return;
2894
2895 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002896 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07002897 this.zoomWarningNode_.style.cssText = (
2898 'color: black;' +
2899 'background-color: #ff2222;' +
2900 'font-size: large;' +
2901 'border-radius: 8px;' +
2902 'opacity: 0.75;' +
2903 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2904 'top: 0.5em;' +
2905 'right: 1.2em;' +
2906 'position: absolute;' +
2907 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002908 '-webkit-user-select: none;' +
2909 '-moz-text-size-adjust: none;' +
2910 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05002911
2912 this.zoomWarningNode_.addEventListener('click', function(e) {
2913 this.parentNode.removeChild(this);
2914 });
rgindaf522ce02012-04-17 17:49:17 -07002915 }
2916
Robert Gindab4839c22013-02-28 16:52:10 -08002917 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2918 hterm.zoomWarningMessage,
2919 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2920
rgindaf522ce02012-04-17 17:49:17 -07002921 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2922
2923 if (state) {
2924 if (!this.zoomWarningNode_.parentNode)
2925 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2926 } else if (this.zoomWarningNode_.parentNode) {
2927 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2928 }
2929};
2930
2931/**
rgindacc2996c2012-02-24 14:59:31 -08002932 * Show the terminal overlay for a given amount of time.
2933 *
2934 * The terminal overlay appears in inverse video in a large font, centered
2935 * over the terminal. You should probably keep the overlay message brief,
2936 * since it's in a large font and you probably aren't going to check the size
2937 * of the terminal first.
2938 *
2939 * @param {string} msg The text (not HTML) message to display in the overlay.
2940 * @param {number} opt_timeout The amount of time to wait before fading out
2941 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2942 * stay up forever (or until the next overlay).
2943 */
2944hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002945 if (!this.overlayNode_) {
2946 if (!this.div_)
2947 return;
2948
2949 this.overlayNode_ = this.document_.createElement('div');
2950 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002951 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002952 'font-size: xx-large;' +
2953 'opacity: 0.75;' +
2954 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2955 'position: absolute;' +
2956 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002957 '-webkit-transition: opacity 180ms ease-in;' +
2958 '-moz-user-select: none;' +
2959 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002960
2961 this.overlayNode_.addEventListener('mousedown', function(e) {
2962 e.preventDefault();
2963 e.stopPropagation();
2964 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002965 }
2966
rginda9f5222b2012-03-05 11:53:28 -08002967 this.overlayNode_.style.color = this.prefs_.get('background-color');
2968 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2969 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2970
rgindaf0090c92012-02-10 14:58:52 -08002971 this.overlayNode_.textContent = msg;
2972 this.overlayNode_.style.opacity = '0.75';
2973
2974 if (!this.overlayNode_.parentNode)
2975 this.div_.appendChild(this.overlayNode_);
2976
Robert Ginda97769282013-02-01 15:30:30 -08002977 var divSize = hterm.getClientSize(this.div_);
2978 var overlaySize = hterm.getClientSize(this.overlayNode_);
2979
Robert Ginda8a59f762014-07-23 11:29:55 -07002980 this.overlayNode_.style.top =
2981 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08002982 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07002983 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08002984
rgindaf0090c92012-02-10 14:58:52 -08002985 if (this.overlayTimeout_)
2986 clearTimeout(this.overlayTimeout_);
2987
Raymes Khouryc7a06382018-07-04 10:25:45 +10002988 this.accessibilityReader_.assertiveAnnounce(msg);
2989
rgindacc2996c2012-02-24 14:59:31 -08002990 if (opt_timeout === null)
2991 return;
2992
Mike Frysingerb6cfded2017-09-18 00:39:31 -04002993 this.overlayTimeout_ = setTimeout(() => {
2994 this.overlayNode_.style.opacity = '0';
2995 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
2996 }, opt_timeout || 1500);
2997};
2998
2999/**
3000 * Hide the terminal overlay immediately.
3001 *
3002 * Useful when we show an overlay for an event with an unknown end time.
3003 */
3004hterm.Terminal.prototype.hideOverlay = function() {
3005 if (this.overlayTimeout_)
3006 clearTimeout(this.overlayTimeout_);
3007 this.overlayTimeout_ = null;
3008
3009 if (this.overlayNode_.parentNode)
3010 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
3011 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08003012};
3013
rginda4bba5e12012-06-20 16:15:30 -07003014/**
3015 * Paste from the system clipboard to the terminal.
3016 */
3017hterm.Terminal.prototype.paste = function() {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003018 return hterm.pasteFromClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07003019};
3020
3021/**
3022 * Copy a string to the system clipboard.
3023 *
3024 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05003025 *
3026 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07003027 */
3028hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08003029 if (this.prefs_.get('enable-clipboard-notice'))
3030 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
3031
rgindaa09e7332012-08-17 12:49:51 -07003032 var copySource = this.document_.createElement('pre');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04003033 copySource.id = 'hterm:copy-to-clipboard-source';
rginda4bba5e12012-06-20 16:15:30 -07003034 copySource.textContent = str;
3035 copySource.style.cssText = (
3036 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003037 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07003038 'position: absolute;' +
3039 'top: -99px');
3040
3041 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07003042
rginda4bba5e12012-06-20 16:15:30 -07003043 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07003044 var anchorNode = selection.anchorNode;
3045 var anchorOffset = selection.anchorOffset;
3046 var focusNode = selection.focusNode;
3047 var focusOffset = selection.focusOffset;
3048
rginda4bba5e12012-06-20 16:15:30 -07003049 selection.selectAllChildren(copySource);
3050
rgindaa09e7332012-08-17 12:49:51 -07003051 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07003052
Rob Spies56953412014-04-28 14:09:47 -07003053 // IE doesn't support selection.extend. This means that the selection
3054 // won't return on IE.
Robert Ginda6f4f0aa2014-07-28 10:21:39 -07003055 if (selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07003056 selection.collapse(anchorNode, anchorOffset);
3057 selection.extend(focusNode, focusOffset);
3058 }
rgindafaa74742012-08-21 13:34:03 -07003059
rginda4bba5e12012-06-20 16:15:30 -07003060 copySource.parentNode.removeChild(copySource);
3061};
3062
Evan Jones2600d4f2016-12-06 09:29:36 -05003063/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003064 * Display an image.
3065 *
3066 * @param {Object} options The image to display.
3067 * @param {string=} options.name A human readable string for the image.
3068 * @param {string|number=} options.size The size (in bytes).
3069 * @param {boolean=} options.preserveAspectRatio Whether to preserve aspect.
3070 * @param {boolean=} options.inline Whether to display the image inline.
3071 * @param {string|number=} options.width The width of the image.
3072 * @param {string|number=} options.height The height of the image.
3073 * @param {string=} options.align Direction to align the image.
3074 * @param {string} options.uri The source URI for the image.
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003075 * @param {function=} onLoad Callback when loading finishes.
3076 * @param {function(Event)=} onError Callback when loading fails.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003077 */
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003078hterm.Terminal.prototype.displayImage = function(options, onLoad, onError) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003079 // Make sure we're actually given a resource to display.
3080 if (options.uri === undefined)
3081 return;
3082
3083 // Set up the defaults to simplify code below.
3084 if (!options.name)
3085 options.name = '';
3086
3087 // Has the user approved image display yet?
3088 if (this.allowImagesInline !== true) {
3089 this.newLine();
3090 const row = this.getRowNode(this.scrollbackRows_.length +
3091 this.getCursorRow() - 1);
3092
3093 if (this.allowImagesInline === false) {
3094 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
3095 'Inline Images Disabled');
3096 return;
3097 }
3098
3099 // Show a prompt.
3100 let button;
3101 const span = this.document_.createElement('span');
3102 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
3103 span.style.fontWeight = 'bold';
3104 span.style.borderWidth = '1px';
3105 span.style.borderStyle = 'dashed';
3106 button = this.document_.createElement('span');
3107 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3108 button.style.marginLeft = '1em';
3109 button.style.borderWidth = '1px';
3110 button.style.borderStyle = 'solid';
3111 button.addEventListener('click', () => {
3112 this.prefs_.set('allow-images-inline', false);
3113 });
3114 span.appendChild(button);
3115 button = this.document_.createElement('span');
3116 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3117 'allow this session');
3118 button.style.marginLeft = '1em';
3119 button.style.borderWidth = '1px';
3120 button.style.borderStyle = 'solid';
3121 button.addEventListener('click', () => {
3122 this.allowImagesInline = true;
3123 });
3124 span.appendChild(button);
3125 button = this.document_.createElement('span');
3126 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3127 button.style.marginLeft = '1em';
3128 button.style.borderWidth = '1px';
3129 button.style.borderStyle = 'solid';
3130 button.addEventListener('click', () => {
3131 this.prefs_.set('allow-images-inline', true);
3132 });
3133 span.appendChild(button);
3134
3135 row.appendChild(span);
3136 return;
3137 }
3138
3139 // See if we should show this object directly, or download it.
3140 if (options.inline) {
3141 const io = this.io.push();
3142 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
3143 'Loading $1 ...'), null);
3144
3145 // While we're loading the image, eat all the user's input.
3146 io.onVTKeystroke = io.sendString = () => {};
3147
3148 // Initialize this new image.
3149 const img = this.document_.createElement('img');
3150 img.src = options.uri;
3151 img.title = img.alt = options.name;
3152
3153 // Attach the image to the page to let it load/render. It won't stay here.
3154 // This is needed so it's visible and the DOM can calculate the height. If
3155 // the image is hidden or not in the DOM, the height is always 0.
3156 this.document_.body.appendChild(img);
3157
3158 // Wait for the image to finish loading before we try moving it to the
3159 // right place in the terminal.
3160 img.onload = () => {
3161 // Now that we have the image dimensions, figure out how to show it.
3162 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
3163 img.style.maxWidth = `${this.document_.body.clientWidth}px`;
3164 img.style.maxHeight = `${this.document_.body.clientHeight}px`;
3165
3166 // Parse a width/height specification.
3167 const parseDim = (dim, maxDim, cssVar) => {
3168 if (!dim || dim == 'auto')
3169 return '';
3170
3171 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3172 if (ary) {
3173 if (ary[2] == '%')
3174 return maxDim * parseInt(ary[1]) / 100 + 'px';
3175 else if (ary[2] == 'px')
3176 return dim;
3177 else
3178 return `calc(${dim} * var(${cssVar}))`;
3179 }
3180
3181 return '';
3182 };
3183 img.style.width =
3184 parseDim(options.width, this.document_.body.clientWidth,
3185 '--hterm-charsize-width');
3186 img.style.height =
3187 parseDim(options.height, this.document_.body.clientHeight,
3188 '--hterm-charsize-height');
3189
3190 // Figure out how many rows the image occupies, then add that many.
3191 // XXX: This count will be inaccurate if the font size changes on us.
3192 const padRows = Math.ceil(img.clientHeight /
3193 this.scrollPort_.characterSize.height);
3194 for (let i = 0; i < padRows; ++i)
3195 this.newLine();
3196
3197 // Update the max height in case the user shrinks the character size.
3198 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3199
3200 // Move the image to the last row. This way when we scroll up, it doesn't
3201 // disappear when the first row gets clipped. It will disappear when we
3202 // scroll down and the last row is clipped ...
3203 this.document_.body.removeChild(img);
3204 // Create a wrapper node so we can do an absolute in a relative position.
3205 // This helps with rounding errors between JS & CSS counts.
3206 const div = this.document_.createElement('div');
3207 div.style.position = 'relative';
3208 div.style.textAlign = options.align;
3209 img.style.position = 'absolute';
3210 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3211 div.appendChild(img);
3212 const row = this.getRowNode(this.scrollbackRows_.length +
3213 this.getCursorRow() - 1);
3214 row.appendChild(div);
3215
3216 io.hideOverlay();
3217 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003218
3219 if (onLoad)
3220 onLoad();
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003221 };
3222
3223 // If we got a malformed image, give up.
3224 img.onerror = (e) => {
3225 this.document_.body.removeChild(img);
3226 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
Mike Frysingere14a8c42018-03-10 00:17:30 -08003227 'Loading $1 failed'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003228 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003229
3230 if (onError)
3231 onError(e);
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003232 };
3233 } else {
3234 // We can't use chrome.downloads.download as that requires "downloads"
3235 // permissions, and that works only in extensions, not apps.
3236 const a = this.document_.createElement('a');
3237 a.href = options.uri;
3238 a.download = options.name;
3239 this.document_.body.appendChild(a);
3240 a.click();
3241 a.remove();
3242 }
3243};
3244
3245/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003246 * Returns the selected text, or null if no text is selected.
3247 *
3248 * @return {string|null}
3249 */
rgindaa09e7332012-08-17 12:49:51 -07003250hterm.Terminal.prototype.getSelectionText = function() {
3251 var selection = this.scrollPort_.selection;
3252 selection.sync();
3253
3254 if (selection.isCollapsed)
3255 return null;
3256
rgindaa09e7332012-08-17 12:49:51 -07003257 // Start offset measures from the beginning of the line.
3258 var startOffset = selection.startOffset;
3259 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003260
Raymes Khoury334625a2018-06-25 10:29:40 +10003261 // If an x-row isn't selected, |node| will be null.
3262 if (!node)
3263 return null;
3264
Robert Gindafdbb3f22012-09-06 20:23:06 -07003265 if (node.nodeName != 'X-ROW') {
3266 // If the selection doesn't start on an x-row node, then it must be
3267 // somewhere inside the x-row. Add any characters from previous siblings
3268 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003269
3270 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3271 // If node is the text node in a styled span, move up to the span node.
3272 node = node.parentNode;
3273 }
3274
Robert Gindafdbb3f22012-09-06 20:23:06 -07003275 while (node.previousSibling) {
3276 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003277 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003278 }
rgindaa09e7332012-08-17 12:49:51 -07003279 }
3280
3281 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08003282 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
3283 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003284 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003285
Robert Gindafdbb3f22012-09-06 20:23:06 -07003286 if (node.nodeName != 'X-ROW') {
3287 // If the selection doesn't end on an x-row node, then it must be
3288 // somewhere inside the x-row. Add any characters from following siblings
3289 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003290
3291 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3292 // If node is the text node in a styled span, move up to the span node.
3293 node = node.parentNode;
3294 }
3295
Robert Gindafdbb3f22012-09-06 20:23:06 -07003296 while (node.nextSibling) {
3297 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003298 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003299 }
rgindaa09e7332012-08-17 12:49:51 -07003300 }
3301
3302 var rv = this.getRowsText(selection.startRow.rowIndex,
3303 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003304 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003305};
3306
rginda4bba5e12012-06-20 16:15:30 -07003307/**
3308 * Copy the current selection to the system clipboard, then clear it after a
3309 * short delay.
3310 */
3311hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07003312 var text = this.getSelectionText();
3313 if (text != null)
3314 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07003315};
3316
rgindaf0090c92012-02-10 14:58:52 -08003317hterm.Terminal.prototype.overlaySize = function() {
3318 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
3319};
3320
rginda87b86462011-12-14 13:48:03 -08003321/**
3322 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3323 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003324 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003325 */
3326hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08003327 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08003328 this.scrollPort_.scrollRowToBottom(this.getRowCount());
3329
Robert Ginda8cb7d902013-06-20 14:37:18 -07003330 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08003331};
3332
3333/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003334 * Open the selected url.
3335 */
3336hterm.Terminal.prototype.openSelectedUrl_ = function() {
3337 var str = this.getSelectionText();
3338
3339 // If there is no selection, try and expand wherever they clicked.
3340 if (str == null) {
John Lincae9b732018-03-08 13:56:35 +08003341 this.screen_.expandSelectionForUrl(this.document_.getSelection());
Mike Frysinger70b94692017-01-26 18:57:50 -10003342 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003343
3344 // If clicking in empty space, return.
3345 if (str == null)
3346 return;
Mike Frysinger70b94692017-01-26 18:57:50 -10003347 }
3348
3349 // Make sure URL is valid before opening.
3350 if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0)
3351 return;
Mike Frysinger43472622017-06-26 18:11:07 -04003352
3353 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003354 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003355 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3356 // We have to whitelist a few protocols that lack authorities and thus
3357 // never use the //. Like mailto.
3358 switch (str.split(':', 1)[0]) {
3359 case 'mailto':
3360 break;
3361 default:
3362 str = 'http://' + str;
3363 break;
3364 }
3365 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003366
Mike Frysinger720fa832017-10-23 01:15:52 -04003367 hterm.openUrl(str);
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003368};
Mike Frysinger70b94692017-01-26 18:57:50 -10003369
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003370/**
3371 * Manage the automatic mouse hiding behavior while typing.
3372 *
3373 * @param {boolean=} v Whether to enable automatic hiding.
3374 */
3375hterm.Terminal.prototype.setAutomaticMouseHiding = function(v=null) {
3376 // Since Chrome OS & macOS do this by default everywhere, we don't need to.
3377 // Linux & Windows seem to leave this to specific applications to manage.
3378 if (v === null)
3379 v = (hterm.os != 'cros' && hterm.os != 'mac');
3380
3381 this.mouseHideWhileTyping_ = !!v;
3382};
3383
3384/**
3385 * Handler for monitoring user keyboard activity.
3386 *
3387 * This isn't for processing the keystrokes directly, but for updating any
3388 * state that might toggle based on the user using the keyboard at all.
3389 *
3390 * @param {KeyboardEvent} e The keyboard event that triggered us.
3391 */
3392hterm.Terminal.prototype.onKeyboardActivity_ = function(e) {
3393 // When the user starts typing, hide the mouse cursor.
3394 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_)
3395 this.setCssVar('mouse-cursor-style', 'none');
3396};
Mike Frysinger70b94692017-01-26 18:57:50 -10003397
3398/**
rgindad5613292012-06-19 15:40:37 -07003399 * Add the terminalRow and terminalColumn properties to mouse events and
3400 * then forward on to onMouse().
3401 *
3402 * The terminalRow and terminalColumn properties contain the (row, column)
3403 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003404 *
3405 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003406 */
3407hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003408 if (e.processedByTerminalHandler_) {
3409 // We register our event handlers on the document, as well as the cursor
3410 // and the scroll blocker. Mouse events that occur on the cursor or
3411 // scroll blocker will also appear on the document, but we don't want to
3412 // process them twice.
3413 //
3414 // We can't just prevent bubbling because that has other side effects, so
3415 // we decorate the event object with this property instead.
3416 return;
3417 }
3418
Mike Frysinger468966c2018-08-28 13:48:51 -04003419 // Consume navigation events. Button 3 is usually "browser back" and
3420 // button 4 is "browser forward" which we don't want to happen.
3421 if (e.button > 2) {
3422 e.preventDefault();
3423 // We don't return so click events can be passed to the remote below.
3424 }
3425
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003426 var reportMouseEvents = (!this.defeatMouseReports_ &&
3427 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3428
rgindafaa74742012-08-21 13:34:03 -07003429 e.processedByTerminalHandler_ = true;
3430
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003431 // Handle auto hiding of mouse cursor while typing.
3432 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
3433 // Make sure the mouse cursor is visible.
3434 this.syncMouseStyle();
3435 // This debounce isn't perfect, but should work well enough for such a
3436 // simple implementation. If the user moved the mouse, we enabled this
3437 // debounce, and then moved the mouse just before the timeout, we wouldn't
3438 // debounce that later movement.
3439 this.mouseHideDelay_ = setTimeout(() => this.mouseHideDelay_ = null, 1000);
3440 }
3441
Robert Gindaeda48db2014-07-17 09:25:30 -07003442 // One based row/column stored on the mouse event.
3443 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
3444 this.scrollPort_.characterSize.height) + 1;
3445 e.terminalColumn = parseInt(e.clientX /
3446 this.scrollPort_.characterSize.width) + 1;
3447
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003448 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
3449 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07003450 return;
3451 }
3452
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003453 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07003454 // If the cursor is visible and we're not sending mouse events to the
3455 // host app, then we want to hide the terminal cursor when the mouse
3456 // cursor is over top. This keeps the terminal cursor from interfering
3457 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003458 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3459 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3460 this.cursorNode_.style.display = 'none';
3461 } else if (this.cursorNode_.style.display == 'none') {
3462 this.cursorNode_.style.display = '';
3463 }
3464 }
rgindad5613292012-06-19 15:40:37 -07003465
Robert Ginda928cf632014-03-05 15:07:41 -08003466 if (e.type == 'mousedown') {
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003467 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003468 // If VT mouse reporting is disabled, or has been defeated with
3469 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003470 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003471 this.setSelectionEnabled(true);
3472 } else {
3473 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003474 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003475 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003476 this.setSelectionEnabled(false);
3477 e.preventDefault();
3478 }
3479 }
3480
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003481 if (!reportMouseEvents) {
John Lin2aad22e2018-03-16 13:58:11 +08003482 if (e.type == 'dblclick') {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003483 this.screen_.expandSelection(this.document_.getSelection());
John Lin2aad22e2018-03-16 13:58:11 +08003484 if (this.copyOnSelect)
3485 this.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07003486 }
3487
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003488 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003489 // Debounce this event with the dblclick event. If you try to doubleclick
3490 // a URL to open it, Chrome will fire click then dblclick, but we won't
3491 // have expanded the selection text at the first click event.
3492 clearTimeout(this.timeouts_.openUrl);
3493 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3494 500);
3495 return;
3496 }
3497
Mike Frysinger847577f2017-05-23 23:25:57 -04003498 if (e.type == 'mousedown') {
3499 if ((this.mouseRightClickPaste && e.button == 2 /* right button */) ||
Mike Frysinger2edd3612017-05-24 00:54:39 -04003500 e.button == this.mousePasteButton) {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003501 if (!this.paste())
Mike Frysinger05a57f02017-08-27 17:48:55 -04003502 console.warn('Could not paste manually due to web restrictions');
Mike Frysinger847577f2017-05-23 23:25:57 -04003503 }
3504 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003505
Mike Frysinger2edd3612017-05-24 00:54:39 -04003506 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003507 !this.document_.getSelection().isCollapsed) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003508 this.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003509 }
3510
3511 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3512 this.scrollBlockerNode_.engaged) {
3513 // Disengage the scroll-blocker after one of these events.
3514 this.scrollBlockerNode_.engaged = false;
3515 this.scrollBlockerNode_.style.top = '-99px';
3516 }
3517
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003518 // Emulate arrow key presses via scroll wheel events.
3519 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3520 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003521 if (e.type == 'wheel') {
Mike Frysinger321063c2018-08-29 15:33:14 -04003522 const delta = this.scrollPort_.scrollWheelDelta(e);
Mike Frysingerc3030a82017-05-29 14:16:11 -04003523
Mike Frysinger321063c2018-08-29 15:33:14 -04003524 // Helper to turn a wheel event delta into a series of key presses.
3525 const deltaToArrows = (distance, charSize, arrowPos, arrowNeg) => {
3526 if (distance == 0) {
3527 return '';
3528 }
3529
3530 // Convert the scroll distance into a number of rows/cols.
3531 const cells = lib.f.smartFloorDivide(Math.abs(distance), charSize);
3532 const data = '\x1bO' + (distance < 0 ? arrowNeg : arrowPos);
3533 return data.repeat(cells);
3534 };
3535
3536 // The order between up/down and left/right doesn't really matter.
3537 this.io.sendString(
3538 // Up/down arrow keys.
3539 deltaToArrows(delta.y, this.scrollPort_.characterSize.height,
3540 'A', 'B') +
3541 // Left/right arrow keys.
3542 deltaToArrows(delta.x, this.scrollPort_.characterSize.width,
3543 'C', 'D')
3544 );
Mike Frysingerc3030a82017-05-29 14:16:11 -04003545
3546 e.preventDefault();
3547 }
3548 }
Robert Ginda928cf632014-03-05 15:07:41 -08003549 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003550 if (!this.scrollBlockerNode_.engaged) {
3551 if (e.type == 'mousedown') {
3552 // Move the scroll-blocker into place if we want to keep the scrollport
3553 // from scrolling.
3554 this.scrollBlockerNode_.engaged = true;
3555 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3556 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3557 } else if (e.type == 'mousemove') {
3558 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3559 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003560 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003561 e.preventDefault();
3562 }
3563 }
Robert Ginda928cf632014-03-05 15:07:41 -08003564
3565 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003566 }
3567
Robert Ginda928cf632014-03-05 15:07:41 -08003568 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3569 // Restore this on mouseup in case it was temporarily defeated with a
3570 // alt-mousedown. Only do this when the selection is empty so that
3571 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003572 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003573 }
rgindad5613292012-06-19 15:40:37 -07003574};
3575
3576/**
3577 * Clients should override this if they care to know about mouse events.
3578 *
3579 * The event parameter will be a normal DOM mouse click event with additional
3580 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003581 *
3582 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003583 */
3584hterm.Terminal.prototype.onMouse = function(e) { };
3585
3586/**
rginda8e92a692012-05-20 19:37:20 -07003587 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003588 *
3589 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003590 */
Rob Spies06533ba2014-04-24 11:20:37 -07003591hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3592 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003593 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003594
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003595 if (this.reportFocus)
3596 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O');
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003597
Michael Kelly485ecd12014-06-09 11:41:56 -04003598 if (focused === true)
3599 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07003600};
3601
3602/**
rginda8ba33642011-12-14 12:31:31 -08003603 * React when the ScrollPort is scrolled.
3604 */
3605hterm.Terminal.prototype.onScroll_ = function() {
3606 this.scheduleSyncCursorPosition_();
3607};
3608
3609/**
rginda9846e2f2012-01-27 13:53:33 -08003610 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003611 *
3612 * @param {Event} e The DOM paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08003613 */
3614hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaa063b202014-07-21 11:08:25 -07003615 var data = e.text.replace(/\n/mg, '\r');
Connor Hegartyf525ceb2014-09-03 13:36:03 -07003616 data = this.keyboard.encode(data);
Mike Frysingere8c32c82018-03-11 14:57:28 -07003617 if (this.options_.bracketedPaste) {
3618 // We strip out most escape sequences as they can cause issues (like
3619 // inserting an \x1b[201~ midstream). We pass through whitespace
3620 // though: 0x08:\b 0x09:\t 0x0a:\n 0x0d:\r.
3621 // This matches xterm behavior.
3622 const filter = (data) => data.replace(/[\x00-\x07\x0b-\x0c\x0e-\x1f]/g, '');
3623 data = '\x1b[200~' + filter(data) + '\x1b[201~';
3624 }
Robert Gindaa063b202014-07-21 11:08:25 -07003625
3626 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003627};
3628
3629/**
rgindaa09e7332012-08-17 12:49:51 -07003630 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003631 *
3632 * @param {Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07003633 */
3634hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07003635 if (!this.useDefaultWindowCopy) {
3636 e.preventDefault();
3637 setTimeout(this.copySelectionToClipboard.bind(this), 0);
3638 }
rgindaa09e7332012-08-17 12:49:51 -07003639};
3640
3641/**
rginda8ba33642011-12-14 12:31:31 -08003642 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08003643 *
3644 * Note: This function should not directly contain code that alters the internal
3645 * state of the terminal. That kind of code belongs in realizeWidth or
3646 * realizeHeight, so that it can be executed synchronously in the case of a
3647 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08003648 */
3649hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08003650 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07003651 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08003652 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07003653 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08003654
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003655 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08003656 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003657 // gets removed from the document or during the initial load, and we can't
3658 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07003659 // This can also happen if called before the scrollPort calculates the
3660 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08003661 return;
3662 }
3663
rgindaa8ba17d2012-08-15 14:41:10 -07003664 var isNewSize = (columnCount != this.screenSize.width ||
3665 rowCount != this.screenSize.height);
3666
3667 // We do this even if the size didn't change, just to be sure everything is
3668 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04003669 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07003670 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07003671
3672 if (isNewSize)
3673 this.overlaySize();
3674
Robert Gindafb1be6a2013-12-11 11:56:22 -08003675 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07003676 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08003677};
3678
3679/**
3680 * Service the cursor blink timeout.
3681 */
3682hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07003683 if (!this.options_.cursorBlink) {
3684 delete this.timeouts_.cursorBlink;
3685 return;
3686 }
3687
Robert Ginda830583c2013-08-07 13:20:46 -07003688 if (this.cursorNode_.getAttribute('focus') == 'false' ||
3689 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08003690 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07003691 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3692 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08003693 } else {
rginda87b86462011-12-14 13:48:03 -08003694 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07003695 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3696 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08003697 }
3698};
David Reveman8f552492012-03-28 12:18:41 -04003699
3700/**
3701 * Set the scrollbar-visible mode bit.
3702 *
3703 * If scrollbar-visible is on, the vertical scrollbar will be visible.
3704 * Otherwise it will not.
3705 *
3706 * Defaults to on.
3707 *
3708 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
3709 */
3710hterm.Terminal.prototype.setScrollbarVisible = function(state) {
3711 this.scrollPort_.setScrollbarVisible(state);
3712};
Michael Kelly485ecd12014-06-09 11:41:56 -04003713
3714/**
Rob Spies49039e52014-12-17 13:40:04 -08003715 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04003716 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08003717 *
3718 * Defaults to 1.
3719 *
Evan Jones2600d4f2016-12-06 09:29:36 -05003720 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08003721 */
3722hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3723 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3724};
3725
3726/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003727 * Close all web notifications created by terminal bells.
3728 */
3729hterm.Terminal.prototype.closeBellNotifications_ = function() {
3730 this.bellNotificationList_.forEach(function(n) {
3731 n.close();
3732 });
3733 this.bellNotificationList_.length = 0;
3734};
Raymes Khourye5d48982018-08-02 09:08:32 +10003735
3736/**
3737 * Syncs the cursor position when the scrollport gains focus.
3738 */
3739hterm.Terminal.prototype.onScrollportFocus_ = function() {
3740 // If the cursor is offscreen we set selection to the last row on the screen.
3741 const topRowIndex = this.scrollPort_.getTopRowIndex();
3742 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
3743 const selection = this.document_.getSelection();
3744 if (!this.syncCursorPosition_() && selection) {
3745 selection.collapse(this.getRowNode(bottomRowIndex));
3746 }
3747};