blob: 5a997d95907b73d6bb1f8be2ce8ce4dd40ccef00 [file] [log] [blame]
rginda87b86462011-12-14 13:48:03 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rginda8ba33642011-12-14 12:31:31 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rgindacbbd7482012-06-13 15:06:16 -07005'use strict';
6
Masaya Suzuki273aa982014-05-31 07:25:55 +09007lib.rtdep('lib.colors', 'lib.PreferenceManager', 'lib.resource', 'lib.wc',
Raymes Khoury3e44bc92018-05-17 10:54:23 +10008 'lib.f', 'hterm.AccessibilityReader', 'hterm.Keyboard',
9 'hterm.Options', 'hterm.PreferenceManager', 'hterm.Screen',
10 'hterm.ScrollPort', 'hterm.Size', 'hterm.TextAttributes', 'hterm.VT');
rgindacbbd7482012-06-13 15:06:16 -070011
rginda8ba33642011-12-14 12:31:31 -080012/**
13 * Constructor for the Terminal class.
14 *
15 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
16 * classes to provide the complete terminal functionality.
17 *
18 * There are a number of lower-level Terminal methods that can be called
19 * directly to manipulate the cursor, text, scroll region, and other terminal
20 * attributes. However, the primary method is interpret(), which parses VT
21 * escape sequences and invokes the appropriate Terminal methods.
22 *
23 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
24 *
25 * TODO(rginda): Eventually we're going to need to support characters which are
26 * displayed twice as wide as standard latin characters. This is to support
27 * CJK (and possibly other character sets).
rginda9f5222b2012-03-05 11:53:28 -080028 *
Robert Ginda57f03b42012-09-13 11:02:48 -070029 * @param {string} opt_profileId Optional preference profile name. If not
rginda9f5222b2012-03-05 11:53:28 -080030 * provided, defaults to 'default'.
rginda8ba33642011-12-14 12:31:31 -080031 */
Robert Ginda57f03b42012-09-13 11:02:48 -070032hterm.Terminal = function(opt_profileId) {
33 this.profileId_ = null;
rginda9f5222b2012-03-05 11:53:28 -080034
rginda8ba33642011-12-14 12:31:31 -080035 // Two screen instances.
36 this.primaryScreen_ = new hterm.Screen();
37 this.alternateScreen_ = new hterm.Screen();
38
39 // The "current" screen.
40 this.screen_ = this.primaryScreen_;
41
rginda8ba33642011-12-14 12:31:31 -080042 // The local notion of the screen size. ScreenBuffers also have a size which
43 // indicates their present size. During size changes, the two may disagree.
44 // Also, the inactive screen's size is not altered until it is made the active
45 // screen.
46 this.screenSize = new hterm.Size(0, 0);
47
rginda8ba33642011-12-14 12:31:31 -080048 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080049 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080050 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
51 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080052 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
Raymes Khourye5d48982018-08-02 09:08:32 +100053 this.scrollPort_.subscribe('focus', this.onScrollportFocus_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070054 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080055
rginda87b86462011-12-14 13:48:03 -080056 // The div that contains this terminal.
57 this.div_ = null;
58
rgindac9bc5502012-01-18 11:48:44 -080059 // The document that contains the scrollPort. Defaulted to the global
60 // document here so that the terminal is functional even if it hasn't been
61 // inserted into a document yet, but re-set in decorate().
62 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080063
rginda8ba33642011-12-14 12:31:31 -080064 // The rows that have scrolled off screen and are no longer addressable.
65 this.scrollbackRows_ = [];
66
rgindac9bc5502012-01-18 11:48:44 -080067 // Saved tab stops.
68 this.tabStops_ = [];
69
David Benjamin66e954d2012-05-05 21:08:12 -040070 // Keep track of whether default tab stops have been erased; after a TBC
71 // clears all tab stops, defaults aren't restored on resize until a reset.
72 this.defaultTabStops = true;
73
rginda8ba33642011-12-14 12:31:31 -080074 // The VT's notion of the top and bottom rows. Used during some VT
75 // cursor positioning and scrolling commands.
76 this.vtScrollTop_ = null;
77 this.vtScrollBottom_ = null;
78
79 // The DIV element for the visible cursor.
80 this.cursorNode_ = null;
81
Robert Ginda830583c2013-08-07 13:20:46 -070082 // The current cursor shape of the terminal.
83 this.cursorShape_ = hterm.Terminal.cursorShape.BLOCK;
84
Robert Gindaea2183e2014-07-17 09:51:51 -070085 // Cursor blink on/off cycle in ms, overwritten by prefs once they're loaded.
86 this.cursorBlinkCycle_ = [100, 100];
87
88 // Pre-bound onCursorBlink_ handler, so we don't have to do this for each
89 // cursor on/off servicing.
90 this.myOnCursorBlink_ = this.onCursorBlink_.bind(this);
91
rginda9f5222b2012-03-05 11:53:28 -080092 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -070093 // each output and keystroke. They are initialized by the preference manager.
Robert Ginda8cb7d902013-06-20 14:37:18 -070094 this.backgroundColor_ = null;
95 this.foregroundColor_ = null;
Robert Ginda57f03b42012-09-13 11:02:48 -070096 this.scrollOnOutput_ = null;
97 this.scrollOnKeystroke_ = null;
Mike Frysinger3c9fa072017-07-13 10:21:13 -040098 this.scrollWheelArrowKeys_ = null;
rginda9f5222b2012-03-05 11:53:28 -080099
Robert Ginda6aec7eb2015-06-16 10:31:30 -0700100 // True if we should override mouse event reporting to allow local selection.
101 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -0800102
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400103 // Whether to auto hide the mouse cursor when typing.
104 this.setAutomaticMouseHiding();
105 // Timer to keep mouse visible while it's being used.
106 this.mouseHideDelay_ = null;
107
rgindaf0090c92012-02-10 14:58:52 -0800108 // Terminal bell sound.
109 this.bellAudio_ = this.document_.createElement('audio');
Mike Frysingerd826f1a2017-07-06 16:20:06 -0400110 this.bellAudio_.id = 'hterm:bell-audio';
rgindaf0090c92012-02-10 14:58:52 -0800111 this.bellAudio_.setAttribute('preload', 'auto');
112
Raymes Khoury3e44bc92018-05-17 10:54:23 +1000113 // The AccessibilityReader object for announcing command output.
114 this.accessibilityReader_ = null;
115
Mike Frysingercc114512017-09-11 21:39:17 -0400116 // The context menu object.
117 this.contextMenu = new hterm.ContextMenu();
118
Michael Kelly485ecd12014-06-09 11:41:56 -0400119 // All terminal bell notifications that have been generated (not necessarily
120 // shown).
121 this.bellNotificationList_ = [];
122
123 // Whether we have permission to display notifications.
124 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400125
rginda6d397402012-01-17 10:58:29 -0800126 // Cursor position and attributes saved with DECSC.
127 this.savedOptions_ = {};
128
rginda8ba33642011-12-14 12:31:31 -0800129 // The current mode bits for the terminal.
130 this.options_ = new hterm.Options();
131
132 // Timeouts we might need to clear.
133 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800134
135 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800136 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800137
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800138 this.saveCursorAndState(true);
139
Zhu Qunying30d40712017-03-14 16:27:00 -0700140 // The keyboard handler.
rgindafeaf3142012-01-31 15:14:20 -0800141 this.keyboard = new hterm.Keyboard(this);
142
rginda87b86462011-12-14 13:48:03 -0800143 // General IO interface that can be given to third parties without exposing
144 // the entire terminal object.
145 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800146
rgindad5613292012-06-19 15:40:37 -0700147 // True if mouse-click-drag should scroll the terminal.
148 this.enableMouseDragScroll = true;
149
Robert Ginda57f03b42012-09-13 11:02:48 -0700150 this.copyOnSelect = null;
Mike Frysinger847577f2017-05-23 23:25:57 -0400151 this.mouseRightClickPaste = null;
rginda4bba5e12012-06-20 16:15:30 -0700152 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700153
Zhu Qunying30d40712017-03-14 16:27:00 -0700154 // Whether to use the default window copy behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700155 this.useDefaultWindowCopy = false;
156
157 this.clearSelectionAfterCopy = true;
158
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400159 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800160 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700161
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400162 // Whether we allow images to be shown.
163 this.allowImagesInline = null;
164
Gabriel Holodake8a09be2017-10-10 01:07:11 -0400165 this.reportFocus = false;
166
Robert Ginda57f03b42012-09-13 11:02:48 -0700167 this.setProfile(opt_profileId || 'default',
Evan Jones5f9df812016-12-06 09:38:58 -0500168 function() { this.onTerminalReady(); }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800169};
170
171/**
Robert Ginda830583c2013-08-07 13:20:46 -0700172 * Possible cursor shapes.
173 */
174hterm.Terminal.cursorShape = {
175 BLOCK: 'BLOCK',
176 BEAM: 'BEAM',
177 UNDERLINE: 'UNDERLINE'
178};
179
180/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700181 * Clients should override this to be notified when the terminal is ready
182 * for use.
183 *
184 * The terminal initialization is asynchronous, and shouldn't be used before
185 * this method is called.
186 */
187hterm.Terminal.prototype.onTerminalReady = function() { };
188
189/**
rginda35c456b2012-02-09 17:29:05 -0800190 * Default tab with of 8 to match xterm.
191 */
192hterm.Terminal.prototype.tabWidth = 8;
193
194/**
rginda9f5222b2012-03-05 11:53:28 -0800195 * Select a preference profile.
196 *
197 * This will load the terminal preferences for the given profile name and
198 * associate subsequent preference changes with the new preference profile.
199 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500200 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800201 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700202 * @param {function} opt_callback Optional callback to invoke when the profile
203 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800204 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700205hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
206 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800207
Robert Ginda57f03b42012-09-13 11:02:48 -0700208 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800209
Robert Ginda57f03b42012-09-13 11:02:48 -0700210 if (this.prefs_)
211 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800212
Robert Ginda57f03b42012-09-13 11:02:48 -0700213 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
214 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800215 'alt-gr-mode': function(v) {
216 if (v == null) {
217 if (navigator.language.toLowerCase() == 'en-us') {
218 v = 'none';
219 } else {
220 v = 'right-alt';
221 }
222 } else if (typeof v == 'string') {
223 v = v.toLowerCase();
224 } else {
225 v = 'none';
226 }
227
228 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v))
229 v = 'none';
230
231 terminal.keyboard.altGrMode = v;
232 },
233
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700234 'alt-backspace-is-meta-backspace': function(v) {
235 terminal.keyboard.altBackspaceIsMetaBackspace = v;
236 },
237
Robert Ginda57f03b42012-09-13 11:02:48 -0700238 'alt-is-meta': function(v) {
239 terminal.keyboard.altIsMeta = v;
240 },
241
242 'alt-sends-what': function(v) {
243 if (!/^(escape|8-bit|browser-key)$/.test(v))
244 v = 'escape';
245
246 terminal.keyboard.altSendsWhat = v;
247 },
248
249 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800250 var ary = v.match(/^lib-resource:(\S+)/);
251 if (ary) {
252 terminal.bellAudio_.setAttribute('src',
253 lib.resource.getDataUrl(ary[1]));
254 } else {
255 terminal.bellAudio_.setAttribute('src', v);
256 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700257 },
258
Michael Kelly485ecd12014-06-09 11:41:56 -0400259 'desktop-notification-bell': function(v) {
260 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700261 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400262 Notification.permission === 'granted';
263 if (!terminal.desktopNotificationBell_) {
264 // Note: We don't call Notification.requestPermission here because
265 // Chrome requires the call be the result of a user action (such as an
266 // onclick handler), and pref listeners are run asynchronously.
267 //
268 // A way of working around this would be to display a dialog in the
269 // terminal with a "click-to-request-permission" button.
270 console.warn('desktop-notification-bell is true but we do not have ' +
271 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400272 }
273 } else {
274 terminal.desktopNotificationBell_ = false;
275 }
276 },
277
Robert Ginda57f03b42012-09-13 11:02:48 -0700278 'background-color': function(v) {
279 terminal.setBackgroundColor(v);
280 },
281
282 'background-image': function(v) {
283 terminal.scrollPort_.setBackgroundImage(v);
284 },
285
286 'background-size': function(v) {
287 terminal.scrollPort_.setBackgroundSize(v);
288 },
289
290 'background-position': function(v) {
291 terminal.scrollPort_.setBackgroundPosition(v);
292 },
293
294 'backspace-sends-backspace': function(v) {
295 terminal.keyboard.backspaceSendsBackspace = v;
296 },
297
Brad Town18654b62015-03-12 00:27:45 -0700298 'character-map-overrides': function(v) {
299 if (!(v == null || v instanceof Object)) {
300 console.warn('Preference character-map-modifications is not an ' +
301 'object: ' + v);
302 return;
303 }
304
Mike Frysinger095d4062017-06-14 00:29:48 -0700305 terminal.vt.characterMaps.reset();
306 terminal.vt.characterMaps.setOverrides(v);
Brad Town18654b62015-03-12 00:27:45 -0700307 },
308
Robert Ginda57f03b42012-09-13 11:02:48 -0700309 'cursor-blink': function(v) {
310 terminal.setCursorBlink(!!v);
311 },
312
Robert Gindaea2183e2014-07-17 09:51:51 -0700313 'cursor-blink-cycle': function(v) {
314 if (v instanceof Array &&
315 typeof v[0] == 'number' &&
316 typeof v[1] == 'number') {
317 terminal.cursorBlinkCycle_ = v;
318 } else if (typeof v == 'number') {
319 terminal.cursorBlinkCycle_ = [v, v];
320 } else {
321 // Fast blink indicates an error.
322 terminal.cursorBlinkCycle_ = [100, 100];
323 }
324 },
325
Robert Ginda57f03b42012-09-13 11:02:48 -0700326 'cursor-color': function(v) {
327 terminal.setCursorColor(v);
328 },
329
330 'color-palette-overrides': function(v) {
331 if (!(v == null || v instanceof Object || v instanceof Array)) {
332 console.warn('Preference color-palette-overrides is not an array or ' +
333 'object: ' + v);
334 return;
rginda9f5222b2012-03-05 11:53:28 -0800335 }
rginda9f5222b2012-03-05 11:53:28 -0800336
Robert Ginda57f03b42012-09-13 11:02:48 -0700337 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700338
Robert Ginda57f03b42012-09-13 11:02:48 -0700339 if (v) {
340 for (var key in v) {
341 var i = parseInt(key);
342 if (isNaN(i) || i < 0 || i > 255) {
343 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
344 continue;
345 }
346
347 if (v[i]) {
348 var rgb = lib.colors.normalizeCSS(v[i]);
349 if (rgb)
350 lib.colors.colorPalette[i] = rgb;
351 }
352 }
rginda30f20f62012-04-05 16:36:19 -0700353 }
rginda30f20f62012-04-05 16:36:19 -0700354
Evan Jones5f9df812016-12-06 09:38:58 -0500355 terminal.primaryScreen_.textAttributes.resetColorPalette();
Robert Ginda57f03b42012-09-13 11:02:48 -0700356 terminal.alternateScreen_.textAttributes.resetColorPalette();
357 },
rginda30f20f62012-04-05 16:36:19 -0700358
Robert Ginda57f03b42012-09-13 11:02:48 -0700359 'copy-on-select': function(v) {
360 terminal.copyOnSelect = !!v;
361 },
rginda9f5222b2012-03-05 11:53:28 -0800362
Rob Spies0bec09b2014-06-06 15:58:09 -0700363 'use-default-window-copy': function(v) {
364 terminal.useDefaultWindowCopy = !!v;
365 },
366
367 'clear-selection-after-copy': function(v) {
368 terminal.clearSelectionAfterCopy = !!v;
369 },
370
Robert Ginda7e5e9522014-03-14 12:23:58 -0700371 'ctrl-plus-minus-zero-zoom': function(v) {
372 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
373 },
374
Robert Gindafb5a3f92014-05-13 14:12:00 -0700375 'ctrl-c-copy': function(v) {
376 terminal.keyboard.ctrlCCopy = v;
377 },
378
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100379 'ctrl-v-paste': function(v) {
380 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700381 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100382 },
383
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
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400629 this.setCssVar('cursor-color', color);
rginda8e92a692012-05-20 19:37:20 -0700630};
631
632/**
633 * Return the current cursor color as a string.
Evan Jones2600d4f2016-12-06 09:29:36 -0500634 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700635 */
636hterm.Terminal.prototype.getCursorColor = function() {
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400637 return this.getCssVar('cursor-color');
rginda8e92a692012-05-20 19:37:20 -0700638};
639
640/**
rgindad5613292012-06-19 15:40:37 -0700641 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500642 *
643 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700644 */
645hterm.Terminal.prototype.setSelectionEnabled = function(state) {
646 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700647};
648
649/**
rginda8e92a692012-05-20 19:37:20 -0700650 * Set the background color.
651 *
652 * If you want this setting to persist, set it through prefs_, rather than
653 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500654 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500655 * @param {string=} color The color to set. If not defined, we reset to the
656 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700657 */
658hterm.Terminal.prototype.setBackgroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500659 if (color === undefined)
660 color = this.prefs_.get('background-color');
661
rgindacbbd7482012-06-13 15:06:16 -0700662 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700663 this.primaryScreen_.textAttributes.setDefaults(
664 this.foregroundColor_, this.backgroundColor_);
665 this.alternateScreen_.textAttributes.setDefaults(
666 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700667 this.scrollPort_.setBackgroundColor(color);
668};
669
rginda9f5222b2012-03-05 11:53:28 -0800670/**
671 * Return the current terminal background color.
672 *
673 * Intended for use by other classes, so we don't have to expose the entire
674 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500675 *
676 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800677 */
678hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700679 return this.backgroundColor_;
680};
681
682/**
683 * Set the foreground color.
684 *
685 * If you want this setting to persist, set it through prefs_, rather than
686 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500687 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500688 * @param {string=} color The color to set. If not defined, we reset to the
689 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700690 */
691hterm.Terminal.prototype.setForegroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500692 if (color === undefined)
693 color = this.prefs_.get('foreground-color');
694
rgindacbbd7482012-06-13 15:06:16 -0700695 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700696 this.primaryScreen_.textAttributes.setDefaults(
697 this.foregroundColor_, this.backgroundColor_);
698 this.alternateScreen_.textAttributes.setDefaults(
699 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700700 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800701};
702
703/**
704 * Return the current terminal foreground color.
705 *
706 * Intended for use by other classes, so we don't have to expose the entire
707 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500708 *
709 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800710 */
711hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700712 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800713};
714
715/**
rginda87b86462011-12-14 13:48:03 -0800716 * Create a new instance of a terminal command and run it with a given
717 * argument string.
718 *
719 * @param {function} commandClass The constructor for a terminal command.
720 * @param {string} argString The argument string to pass to the command.
721 */
722hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700723 var environment = this.prefs_.get('environment');
724 if (typeof environment != 'object' || environment == null)
725 environment = {};
726
rginda87b86462011-12-14 13:48:03 -0800727 var self = this;
728 this.command = new commandClass(
729 { argString: argString || '',
730 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700731 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800732 onExit: function(code) {
733 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800734 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700735 if (self.prefs_.get('close-on-exit'))
736 window.close();
rginda87b86462011-12-14 13:48:03 -0800737 }
738 });
739
rgindafeaf3142012-01-31 15:14:20 -0800740 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800741 this.command.run();
742};
743
744/**
rgindafeaf3142012-01-31 15:14:20 -0800745 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500746 *
747 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800748 */
749hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700750 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800751};
752
753/**
754 * Install the keyboard handler for this terminal.
755 *
756 * This will prevent the browser from seeing any keystrokes sent to the
757 * terminal.
758 */
759hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700760 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400761};
rgindafeaf3142012-01-31 15:14:20 -0800762
763/**
764 * Uninstall the keyboard handler for this terminal.
765 */
766hterm.Terminal.prototype.uninstallKeyboard = function() {
767 this.keyboard.installKeyboard(null);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400768};
rgindafeaf3142012-01-31 15:14:20 -0800769
770/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400771 * Set a CSS variable.
772 *
773 * Normally this is used to set variables in the hterm namespace.
774 *
775 * @param {string} name The variable to set.
776 * @param {string} value The value to assign to the variable.
777 * @param {string?} opt_prefix The variable namespace/prefix to use.
778 */
779hterm.Terminal.prototype.setCssVar = function(name, value,
780 opt_prefix='--hterm-') {
781 this.document_.documentElement.style.setProperty(
782 `${opt_prefix}${name}`, value);
783};
784
785/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500786 * Get a CSS variable.
787 *
788 * Normally this is used to get variables in the hterm namespace.
789 *
790 * @param {string} name The variable to read.
791 * @param {string?} opt_prefix The variable namespace/prefix to use.
792 * @return {string} The current setting for this variable.
793 */
794hterm.Terminal.prototype.getCssVar = function(name, opt_prefix='--hterm-') {
795 return this.document_.documentElement.style.getPropertyValue(
796 `${opt_prefix}${name}`);
797};
798
799/**
rginda35c456b2012-02-09 17:29:05 -0800800 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800801 *
802 * Call setFontSize(0) to reset to the default font size.
803 *
804 * This function does not modify the font-size preference.
805 *
806 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800807 */
808hterm.Terminal.prototype.setFontSize = function(px) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500809 if (px <= 0)
rginda9f5222b2012-03-05 11:53:28 -0800810 px = this.prefs_.get('font-size');
811
rginda35c456b2012-02-09 17:29:05 -0800812 this.scrollPort_.setFontSize(px);
Mike Frysingercce97c42017-08-05 01:11:22 -0400813 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
814 this.setCssVar('charsize-height',
815 this.scrollPort_.characterSize.height + 'px');
rginda35c456b2012-02-09 17:29:05 -0800816};
817
818/**
819 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500820 *
821 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800822 */
823hterm.Terminal.prototype.getFontSize = function() {
824 return this.scrollPort_.getFontSize();
825};
826
827/**
rginda8e92a692012-05-20 19:37:20 -0700828 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500829 *
830 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700831 */
832hterm.Terminal.prototype.getFontFamily = function() {
833 return this.scrollPort_.getFontFamily();
834};
835
836/**
rginda35c456b2012-02-09 17:29:05 -0800837 * Set the CSS "font-family" for this terminal.
838 */
rginda9f5222b2012-03-05 11:53:28 -0800839hterm.Terminal.prototype.syncFontFamily = function() {
840 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
841 this.prefs_.get('font-smoothing'));
842 this.syncBoldSafeState();
843};
844
rginda4bba5e12012-06-20 16:15:30 -0700845/**
846 * Set this.mousePasteButton based on the mouse-paste-button pref,
847 * autodetecting if necessary.
848 */
849hterm.Terminal.prototype.syncMousePasteButton = function() {
850 var button = this.prefs_.get('mouse-paste-button');
851 if (typeof button == 'number') {
852 this.mousePasteButton = button;
853 return;
854 }
855
Mike Frysingeree81a002017-12-12 16:14:53 -0500856 if (hterm.os != 'linux') {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400857 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700858 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400859 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700860 }
861};
862
863/**
864 * Enable or disable bold based on the enable-bold pref, autodetecting if
865 * necessary.
866 */
rginda9f5222b2012-03-05 11:53:28 -0800867hterm.Terminal.prototype.syncBoldSafeState = function() {
868 var enableBold = this.prefs_.get('enable-bold');
869 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700870 this.primaryScreen_.textAttributes.enableBold = enableBold;
871 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800872 return;
873 }
874
rgindaf7521392012-02-28 17:20:34 -0800875 var normalSize = this.scrollPort_.measureCharacterSize();
876 var boldSize = this.scrollPort_.measureCharacterSize('bold');
877
878 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800879 if (!isBoldSafe) {
880 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700881 'from normal. Font family is: ' +
882 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800883 }
rginda9f5222b2012-03-05 11:53:28 -0800884
Robert Gindaed016262012-10-26 16:27:09 -0700885 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
886 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800887};
888
889/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500890 * Control text blinking behavior.
891 *
892 * @param {boolean=} state Whether to enable support for blinking text.
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400893 */
Mike Frysinger261597c2017-12-28 01:14:21 -0500894hterm.Terminal.prototype.setTextBlink = function(state) {
895 if (state === undefined)
896 state = this.prefs_.get('enable-blink');
897 this.setCssVar('blink-node-duration', state ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400898};
899
900/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400901 * Set the mouse cursor style based on the current terminal mode.
902 */
903hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -0400904 this.setCssVar('mouse-cursor-style',
905 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
906 'var(--hterm-mouse-cursor-text)' :
907 'var(--hterm-mouse-cursor-pointer)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400908};
909
910/**
rginda87b86462011-12-14 13:48:03 -0800911 * Return a copy of the current cursor position.
912 *
913 * @return {hterm.RowCol} The RowCol object representing the current position.
914 */
915hterm.Terminal.prototype.saveCursor = function() {
916 return this.screen_.cursorPosition.clone();
917};
918
Evan Jones2600d4f2016-12-06 09:29:36 -0500919/**
920 * Return the current text attributes.
921 *
922 * @return {string}
923 */
rgindaa19afe22012-01-25 15:40:22 -0800924hterm.Terminal.prototype.getTextAttributes = function() {
925 return this.screen_.textAttributes;
926};
927
Evan Jones2600d4f2016-12-06 09:29:36 -0500928/**
929 * Set the text attributes.
930 *
931 * @param {string} textAttributes The attributes to set.
932 */
rginda1a09aa02012-06-18 21:11:25 -0700933hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
934 this.screen_.textAttributes = textAttributes;
935};
936
rginda87b86462011-12-14 13:48:03 -0800937/**
rgindaf522ce02012-04-17 17:49:17 -0700938 * Return the current browser zoom factor applied to the terminal.
939 *
940 * @return {number} The current browser zoom factor.
941 */
942hterm.Terminal.prototype.getZoomFactor = function() {
943 return this.scrollPort_.characterSize.zoomFactor;
944};
945
946/**
rginda9846e2f2012-01-27 13:53:33 -0800947 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -0500948 *
949 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -0800950 */
951hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800952 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800953};
954
955/**
rginda87b86462011-12-14 13:48:03 -0800956 * Restore a previously saved cursor position.
957 *
958 * @param {hterm.RowCol} cursor The position to restore.
959 */
960hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700961 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
962 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800963 this.screen_.setCursorPosition(row, column);
964 if (cursor.column > column ||
965 cursor.column == column && cursor.overflow) {
966 this.screen_.cursorPosition.overflow = true;
967 }
rginda87b86462011-12-14 13:48:03 -0800968};
969
970/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400971 * Clear the cursor's overflow flag.
972 */
973hterm.Terminal.prototype.clearCursorOverflow = function() {
974 this.screen_.cursorPosition.overflow = false;
975};
976
977/**
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800978 * Save the current cursor state to the corresponding screens.
979 *
980 * See the hterm.Screen.CursorState class for more details.
981 *
982 * @param {boolean=} both If true, update both screens, else only update the
983 * current screen.
984 */
985hterm.Terminal.prototype.saveCursorAndState = function(both) {
986 if (both) {
987 this.primaryScreen_.saveCursorAndState(this.vt);
988 this.alternateScreen_.saveCursorAndState(this.vt);
989 } else
990 this.screen_.saveCursorAndState(this.vt);
991};
992
993/**
994 * Restore the saved cursor state in the corresponding screens.
995 *
996 * See the hterm.Screen.CursorState class for more details.
997 *
998 * @param {boolean=} both If true, update both screens, else only update the
999 * current screen.
1000 */
1001hterm.Terminal.prototype.restoreCursorAndState = function(both) {
1002 if (both) {
1003 this.primaryScreen_.restoreCursorAndState(this.vt);
1004 this.alternateScreen_.restoreCursorAndState(this.vt);
1005 } else
1006 this.screen_.restoreCursorAndState(this.vt);
1007};
1008
1009/**
Robert Ginda830583c2013-08-07 13:20:46 -07001010 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001011 *
1012 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -07001013 */
1014hterm.Terminal.prototype.setCursorShape = function(shape) {
1015 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001016 this.restyleCursor_();
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001017};
Robert Ginda830583c2013-08-07 13:20:46 -07001018
1019/**
1020 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001021 *
1022 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -07001023 */
1024hterm.Terminal.prototype.getCursorShape = function() {
1025 return this.cursorShape_;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001026};
Robert Ginda830583c2013-08-07 13:20:46 -07001027
1028/**
rginda87b86462011-12-14 13:48:03 -08001029 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001030 *
1031 * @param {number} columnCount
rginda87b86462011-12-14 13:48:03 -08001032 */
1033hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -08001034 if (columnCount == null) {
1035 this.div_.style.width = '100%';
1036 return;
1037 }
1038
Robert Ginda26806d12014-07-24 13:44:07 -07001039 this.div_.style.width = Math.ceil(
1040 this.scrollPort_.characterSize.width *
1041 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001042 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -08001043 this.scheduleSyncCursorPosition_();
1044};
rginda87b86462011-12-14 13:48:03 -08001045
rgindac9bc5502012-01-18 11:48:44 -08001046/**
rginda35c456b2012-02-09 17:29:05 -08001047 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001048 *
1049 * @param {number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -08001050 */
1051hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -08001052 if (rowCount == null) {
1053 this.div_.style.height = '100%';
1054 return;
1055 }
1056
rginda35c456b2012-02-09 17:29:05 -08001057 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -07001058 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -08001059 this.realizeSize_(this.screenSize.width, rowCount);
1060 this.scheduleSyncCursorPosition_();
1061};
1062
1063/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001064 * Deal with terminal size changes.
1065 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001066 * @param {number} columnCount The number of columns.
1067 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001068 */
1069hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
1070 if (columnCount != this.screenSize.width)
1071 this.realizeWidth_(columnCount);
1072
1073 if (rowCount != this.screenSize.height)
1074 this.realizeHeight_(rowCount);
1075
1076 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -07001077 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001078};
1079
1080/**
rgindac9bc5502012-01-18 11:48:44 -08001081 * Deal with terminal width changes.
1082 *
1083 * This function does what needs to be done when the terminal width changes
1084 * out from under us. It happens here rather than in onResize_() because this
1085 * code may need to run synchronously to handle programmatic changes of
1086 * terminal width.
1087 *
1088 * Relying on the browser to send us an async resize event means we may not be
1089 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001090 *
1091 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001092 */
1093hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001094 if (columnCount <= 0)
1095 throw new Error('Attempt to realize bad width: ' + columnCount);
1096
rgindac9bc5502012-01-18 11:48:44 -08001097 var deltaColumns = columnCount - this.screen_.getWidth();
1098
rginda87b86462011-12-14 13:48:03 -08001099 this.screenSize.width = columnCount;
1100 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001101
1102 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -04001103 if (this.defaultTabStops)
1104 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -08001105 } else {
1106 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -04001107 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -08001108 break;
1109
1110 this.tabStops_.pop();
1111 }
1112 }
1113
1114 this.screen_.setColumnCount(this.screenSize.width);
1115};
1116
1117/**
1118 * Deal with terminal height changes.
1119 *
1120 * This function does what needs to be done when the terminal height changes
1121 * out from under us. It happens here rather than in onResize_() because this
1122 * code may need to run synchronously to handle programmatic changes of
1123 * terminal height.
1124 *
1125 * Relying on the browser to send us an async resize event means we may not be
1126 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001127 *
1128 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001129 */
1130hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001131 if (rowCount <= 0)
1132 throw new Error('Attempt to realize bad height: ' + rowCount);
1133
rgindac9bc5502012-01-18 11:48:44 -08001134 var deltaRows = rowCount - this.screen_.getHeight();
1135
1136 this.screenSize.height = rowCount;
1137
1138 var cursor = this.saveCursor();
1139
1140 if (deltaRows < 0) {
1141 // Screen got smaller.
1142 deltaRows *= -1;
1143 while (deltaRows) {
1144 var lastRow = this.getRowCount() - 1;
1145 if (lastRow - this.scrollbackRows_.length == cursor.row)
1146 break;
1147
1148 if (this.getRowText(lastRow))
1149 break;
1150
1151 this.screen_.popRow();
1152 deltaRows--;
1153 }
1154
1155 var ary = this.screen_.shiftRows(deltaRows);
1156 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1157
1158 // We just removed rows from the top of the screen, we need to update
1159 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001160 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001161 } else if (deltaRows > 0) {
1162 // Screen got larger.
1163
1164 if (deltaRows <= this.scrollbackRows_.length) {
1165 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1166 var rows = this.scrollbackRows_.splice(
1167 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1168 this.screen_.unshiftRows(rows);
1169 deltaRows -= scrollbackCount;
1170 cursor.row += scrollbackCount;
1171 }
1172
1173 if (deltaRows)
1174 this.appendRows_(deltaRows);
1175 }
1176
rginda35c456b2012-02-09 17:29:05 -08001177 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001178 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001179};
1180
1181/**
1182 * Scroll the terminal to the top of the scrollback buffer.
1183 */
1184hterm.Terminal.prototype.scrollHome = function() {
1185 this.scrollPort_.scrollRowToTop(0);
1186};
1187
1188/**
1189 * Scroll the terminal to the end.
1190 */
1191hterm.Terminal.prototype.scrollEnd = function() {
1192 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1193};
1194
1195/**
1196 * Scroll the terminal one page up (minus one line) relative to the current
1197 * position.
1198 */
1199hterm.Terminal.prototype.scrollPageUp = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001200 this.scrollPort_.scrollPageUp();
rginda87b86462011-12-14 13:48:03 -08001201};
1202
1203/**
1204 * Scroll the terminal one page down (minus one line) relative to the current
1205 * position.
1206 */
1207hterm.Terminal.prototype.scrollPageDown = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001208 this.scrollPort_.scrollPageDown();
rginda8ba33642011-12-14 12:31:31 -08001209};
1210
rgindac9bc5502012-01-18 11:48:44 -08001211/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001212 * Scroll the terminal one line up relative to the current position.
1213 */
1214hterm.Terminal.prototype.scrollLineUp = function() {
1215 var i = this.scrollPort_.getTopRowIndex();
1216 this.scrollPort_.scrollRowToTop(i - 1);
1217};
1218
1219/**
1220 * Scroll the terminal one line down relative to the current position.
1221 */
1222hterm.Terminal.prototype.scrollLineDown = function() {
1223 var i = this.scrollPort_.getTopRowIndex();
1224 this.scrollPort_.scrollRowToTop(i + 1);
1225};
1226
1227/**
Robert Ginda40932892012-12-10 17:26:40 -08001228 * Clear primary screen, secondary screen, and the scrollback buffer.
1229 */
1230hterm.Terminal.prototype.wipeContents = function() {
1231 this.scrollbackRows_.length = 0;
1232 this.scrollPort_.resetCache();
1233
1234 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
1235 var bottom = screen.getHeight();
1236 if (bottom > 0) {
1237 this.renumberRows_(0, bottom);
1238 this.clearHome(screen);
1239 }
1240 }.bind(this));
1241
1242 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001243 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001244};
1245
1246/**
rgindac9bc5502012-01-18 11:48:44 -08001247 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001248 *
1249 * Perform a full reset to the default values listed in
1250 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001251 */
rginda87b86462011-12-14 13:48:03 -08001252hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001253 this.vt.reset();
1254
rgindac9bc5502012-01-18 11:48:44 -08001255 this.clearAllTabStops();
1256 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001257
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001258 const resetScreen = (screen) => {
1259 // We want to make sure to reset the attributes before we clear the screen.
1260 // The attributes might be used to initialize default/empty rows.
1261 screen.textAttributes.reset();
1262 screen.textAttributes.resetColorPalette();
1263 this.clearHome(screen);
1264 screen.saveCursorAndState(this.vt);
1265 };
1266 resetScreen(this.primaryScreen_);
1267 resetScreen(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001268
Mike Frysinger84301d02017-11-29 13:28:46 -08001269 // Reset terminal options to their default values.
1270 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001271 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1272
Mike Frysinger84301d02017-11-29 13:28:46 -08001273 this.setVTScrollRegion(null, null);
1274
1275 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001276};
1277
rgindac9bc5502012-01-18 11:48:44 -08001278/**
1279 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001280 *
1281 * Perform a soft reset to the default values listed in
1282 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001283 */
rginda0f5c0292012-01-13 11:00:13 -08001284hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001285 this.vt.reset();
1286
rgindab8bc8932012-04-27 12:45:03 -07001287 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001288 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001289
Brad Townb62dfdc2015-03-16 19:07:15 -07001290 // We show the cursor on soft reset but do not alter the blink state.
1291 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1292
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001293 const resetScreen = (screen) => {
1294 // Xterm also resets the color palette on soft reset, even though it doesn't
1295 // seem to be documented anywhere.
1296 screen.textAttributes.reset();
1297 screen.textAttributes.resetColorPalette();
1298 screen.saveCursorAndState(this.vt);
1299 };
1300 resetScreen(this.primaryScreen_);
1301 resetScreen(this.alternateScreen_);
rgindaf522ce02012-04-17 17:49:17 -07001302
rgindab8bc8932012-04-27 12:45:03 -07001303 // The xterm man page explicitly says this will happen on soft reset.
1304 this.setVTScrollRegion(null, null);
1305
1306 // Xterm also shows the cursor on soft reset, but does not alter the blink
1307 // state.
rgindaa19afe22012-01-25 15:40:22 -08001308 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001309};
1310
rgindac9bc5502012-01-18 11:48:44 -08001311/**
1312 * Move the cursor forward to the next tab stop, or to the last column
1313 * if no more tab stops are set.
1314 */
1315hterm.Terminal.prototype.forwardTabStop = function() {
1316 var column = this.screen_.cursorPosition.column;
1317
1318 for (var i = 0; i < this.tabStops_.length; i++) {
1319 if (this.tabStops_[i] > column) {
1320 this.setCursorColumn(this.tabStops_[i]);
1321 return;
1322 }
1323 }
1324
David Benjamin66e954d2012-05-05 21:08:12 -04001325 // xterm does not clear the overflow flag on HT or CHT.
1326 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001327 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001328 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001329};
1330
rgindac9bc5502012-01-18 11:48:44 -08001331/**
1332 * Move the cursor backward to the previous tab stop, or to the first column
1333 * if no previous tab stops are set.
1334 */
1335hterm.Terminal.prototype.backwardTabStop = function() {
1336 var column = this.screen_.cursorPosition.column;
1337
1338 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1339 if (this.tabStops_[i] < column) {
1340 this.setCursorColumn(this.tabStops_[i]);
1341 return;
1342 }
1343 }
1344
1345 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001346};
1347
rgindac9bc5502012-01-18 11:48:44 -08001348/**
1349 * Set a tab stop at the given column.
1350 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001351 * @param {integer} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001352 */
1353hterm.Terminal.prototype.setTabStop = function(column) {
1354 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1355 if (this.tabStops_[i] == column)
1356 return;
1357
1358 if (this.tabStops_[i] < column) {
1359 this.tabStops_.splice(i + 1, 0, column);
1360 return;
1361 }
1362 }
1363
1364 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001365};
1366
rgindac9bc5502012-01-18 11:48:44 -08001367/**
1368 * Clear the tab stop at the current cursor position.
1369 *
1370 * No effect if there is no tab stop at the current cursor position.
1371 */
1372hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1373 var column = this.screen_.cursorPosition.column;
1374
1375 var i = this.tabStops_.indexOf(column);
1376 if (i == -1)
1377 return;
1378
1379 this.tabStops_.splice(i, 1);
1380};
1381
1382/**
1383 * Clear all tab stops.
1384 */
1385hterm.Terminal.prototype.clearAllTabStops = function() {
1386 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001387 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001388};
1389
1390/**
1391 * Set up the default tab stops, starting from a given column.
1392 *
1393 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001394 * from the specified column, or 0 if no column is provided. It also flags
1395 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001396 *
1397 * This does not clear the existing tab stops first, use clearAllTabStops
1398 * for that.
1399 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001400 * @param {integer} opt_start Optional starting zero based starting column, useful
rgindac9bc5502012-01-18 11:48:44 -08001401 * for filling out missing tab stops when the terminal is resized.
1402 */
1403hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1404 var start = opt_start || 0;
1405 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001406 // Round start up to a default tab stop.
1407 start = start - 1 - ((start - 1) % w) + w;
1408 for (var i = start; i < this.screenSize.width; i += w) {
1409 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001410 }
David Benjamin66e954d2012-05-05 21:08:12 -04001411
1412 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001413};
1414
rginda6d397402012-01-17 10:58:29 -08001415/**
rginda8ba33642011-12-14 12:31:31 -08001416 * Interpret a sequence of characters.
1417 *
1418 * Incomplete escape sequences are buffered until the next call.
1419 *
1420 * @param {string} str Sequence of characters to interpret or pass through.
1421 */
1422hterm.Terminal.prototype.interpret = function(str) {
rginda8ba33642011-12-14 12:31:31 -08001423 this.scheduleSyncCursorPosition_();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001424 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001425};
1426
1427/**
1428 * Take over the given DIV for use as the terminal display.
1429 *
1430 * @param {HTMLDivElement} div The div to use as the terminal display.
1431 */
1432hterm.Terminal.prototype.decorate = function(div) {
Mike Frysinger5768a9d2017-12-26 12:57:44 -05001433 const charset = div.ownerDocument.characterSet.toLowerCase();
1434 if (charset != 'utf-8') {
1435 console.warn(`Document encoding should be set to utf-8, not "${charset}";` +
1436 ` Add <meta charset='utf-8'/> to your HTML <head> to fix.`);
1437 }
1438
rginda87b86462011-12-14 13:48:03 -08001439 this.div_ = div;
1440
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001441 this.accessibilityReader_ = new hterm.AccessibilityReader(div);
1442
rginda8ba33642011-12-14 12:31:31 -08001443 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001444 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001445 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1446 this.scrollPort_.setBackgroundPosition(
1447 this.prefs_.get('background-position'));
Mike Frysinger08bad432017-04-24 00:50:54 -04001448 this.scrollPort_.setUserCssUrl(this.prefs_.get('user-css'));
1449 this.scrollPort_.setUserCssText(this.prefs_.get('user-css-text'));
Raymes Khoury177aec72018-06-26 10:58:53 +10001450 this.scrollPort_.setAccessibilityReader(this.accessibilityReader_);
rginda30f20f62012-04-05 16:36:19 -07001451
rginda0918b652012-04-04 11:26:24 -07001452 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001453
rginda9f5222b2012-03-05 11:53:28 -08001454 this.setFontSize(this.prefs_.get('font-size'));
1455 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001456
David Reveman8f552492012-03-28 12:18:41 -04001457 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001458 this.setScrollWheelMoveMultipler(
1459 this.prefs_.get('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001460
rginda8ba33642011-12-14 12:31:31 -08001461 this.document_ = this.scrollPort_.getDocument();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001462 this.accessibilityReader_.decorate(this.document_);
rginda8ba33642011-12-14 12:31:31 -08001463
Evan Jones5f9df812016-12-06 09:38:58 -05001464 this.document_.body.oncontextmenu = function() { return false; };
Mike Frysingercc114512017-09-11 21:39:17 -04001465 this.contextMenu.setDocument(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07001466
1467 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001468 var screenNode = this.scrollPort_.getScreenNode();
1469 screenNode.addEventListener('mousedown', onMouse);
1470 screenNode.addEventListener('mouseup', onMouse);
1471 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001472 this.scrollPort_.onScrollWheel = onMouse;
1473
Mike Frysinger02ded6d2018-06-21 14:25:20 -04001474 screenNode.addEventListener('keydown', this.onKeyboardActivity_.bind(this));
1475
Toni Barzic0bfa8922013-11-22 11:18:35 -08001476 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001477 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001478 // Listen for mousedown events on the screenNode as in FF the focus
1479 // events don't bubble.
1480 screenNode.addEventListener('mousedown', function() {
1481 setTimeout(this.onFocusChange_.bind(this, true));
1482 }.bind(this));
1483
Toni Barzic0bfa8922013-11-22 11:18:35 -08001484 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001485 'blur', this.onFocusChange_.bind(this, false));
1486
1487 var style = this.document_.createElement('style');
1488 style.textContent =
1489 ('.cursor-node[focus="false"] {' +
1490 ' box-sizing: border-box;' +
1491 ' background-color: transparent !important;' +
1492 ' border-width: 2px;' +
1493 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001494 '}' +
Mike Frysingercc114512017-09-11 21:39:17 -04001495 'menu {' +
1496 ' margin: 0;' +
1497 ' padding: 0;' +
1498 ' cursor: var(--hterm-mouse-cursor-pointer);' +
1499 '}' +
1500 'menuitem {' +
1501 ' white-space: nowrap;' +
1502 ' border-bottom: 1px dashed;' +
1503 ' display: block;' +
1504 ' padding: 0.3em 0.3em 0 0.3em;' +
1505 '}' +
1506 'menuitem.separator {' +
1507 ' border-bottom: none;' +
1508 ' height: 0.5em;' +
1509 ' padding: 0;' +
1510 '}' +
1511 'menuitem:hover {' +
1512 ' color: var(--hterm-cursor-color);' +
1513 '}' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001514 '.wc-node {' +
1515 ' display: inline-block;' +
1516 ' text-align: center;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001517 ' width: calc(var(--hterm-charsize-width) * 2);' +
Mike Frysinger4036f6e2017-05-31 14:02:55 -04001518 ' line-height: var(--hterm-charsize-height);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001519 '}' +
1520 ':root {' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001521 ' --hterm-charsize-width: ' + this.scrollPort_.characterSize.width + 'px;' +
1522 ' --hterm-charsize-height: ' + this.scrollPort_.characterSize.height + 'px;' +
Mike Frysingera27c0502017-08-23 22:37:10 -04001523 // Default position hides the cursor for when the window is initializing.
1524 ' --hterm-cursor-offset-col: -1;' +
1525 ' --hterm-cursor-offset-row: -1;' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001526 ' --hterm-blink-node-duration: 0.7s;' +
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001527 ' --hterm-mouse-cursor-text: text;' +
1528 ' --hterm-mouse-cursor-pointer: default;' +
1529 ' --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001530 '}' +
Mike Frysingerd3907202017-10-23 01:23:50 -04001531 '.uri-node:hover {' +
1532 ' text-decoration: underline;' +
Mike Frysingerb74a6472018-06-22 13:37:08 -04001533 ' cursor: var(--hterm-mouse-cursor-pointer), pointer;' +
Mike Frysingerd3907202017-10-23 01:23:50 -04001534 '}' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001535 '@keyframes blink {' +
1536 ' from { opacity: 1.0; }' +
1537 ' to { opacity: 0.0; }' +
1538 '}' +
1539 '.blink-node {' +
1540 ' animation-name: blink;' +
1541 ' animation-duration: var(--hterm-blink-node-duration);' +
1542 ' animation-iteration-count: infinite;' +
1543 ' animation-timing-function: ease-in-out;' +
1544 ' animation-direction: alternate;' +
rginda8e92a692012-05-20 19:37:20 -07001545 '}');
Mike Frysingerb74a6472018-06-22 13:37:08 -04001546 // Insert this stock style as the first node so that any user styles will
1547 // override w/out having to use !important everywhere. The rules above mix
1548 // runtime variables with default ones designed to be overridden by the user,
1549 // but we can wait for a concrete case from the users to determine the best
1550 // way to split the sheet up to before & after the user-css settings.
1551 this.document_.head.insertBefore(style, this.document_.head.firstChild);
rginda8e92a692012-05-20 19:37:20 -07001552
rginda8ba33642011-12-14 12:31:31 -08001553 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001554 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001555 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001556 this.cursorNode_.style.cssText =
1557 ('position: absolute;' +
Mike Frysinger44c32202017-08-05 01:13:09 -04001558 'left: calc(var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));' +
1559 'top: calc(var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));' +
Mike Frysingerd60f4c22018-03-15 21:25:30 -07001560 'display: ' + (this.options_.cursorVisible ? '' : 'none') + ';' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001561 'width: var(--hterm-charsize-width);' +
1562 'height: var(--hterm-charsize-height);' +
Mike Frysinger2fd079a2018-09-02 01:46:12 -04001563 'background-color: var(--hterm-cursor-color);' +
1564 'border-color: var(--hterm-cursor-color);' +
Rob Spies06533ba2014-04-24 11:20:37 -07001565 '-webkit-transition: opacity, background-color 100ms linear;' +
1566 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001567
Mike Frysingerf02a2cb2017-12-21 00:34:03 -05001568 this.setCursorColor();
Robert Gindafb1be6a2013-12-11 11:56:22 -08001569 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1570 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001571
rginda8ba33642011-12-14 12:31:31 -08001572 this.document_.body.appendChild(this.cursorNode_);
1573
rgindad5613292012-06-19 15:40:37 -07001574 // When 'enableMouseDragScroll' is off we reposition this element directly
1575 // under the mouse cursor after a click. This makes Chrome associate
1576 // subsequent mousemove events with the scroll-blocker. Since the
1577 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1578 // events do not cause the scrollport to scroll.
1579 //
1580 // It's a hack, but it's the cleanest way I could find.
1581 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001582 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
Raymes Khoury6dce2f82018-04-12 15:38:58 +10001583 this.scrollBlockerNode_.setAttribute('aria-hidden', 'true');
rgindad5613292012-06-19 15:40:37 -07001584 this.scrollBlockerNode_.style.cssText =
1585 ('position: absolute;' +
1586 'top: -99px;' +
1587 'display: block;' +
1588 'width: 10px;' +
1589 'height: 10px;');
1590 this.document_.body.appendChild(this.scrollBlockerNode_);
1591
rgindad5613292012-06-19 15:40:37 -07001592 this.scrollPort_.onScrollWheel = onMouse;
1593 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1594 ].forEach(function(event) {
1595 this.scrollBlockerNode_.addEventListener(event, onMouse);
1596 this.cursorNode_.addEventListener(event, onMouse);
1597 this.document_.addEventListener(event, onMouse);
1598 }.bind(this));
1599
1600 this.cursorNode_.addEventListener('mousedown', function() {
1601 setTimeout(this.focus.bind(this));
1602 }.bind(this));
1603
rginda8ba33642011-12-14 12:31:31 -08001604 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001605
rginda87b86462011-12-14 13:48:03 -08001606 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001607 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001608};
1609
rginda0918b652012-04-04 11:26:24 -07001610/**
1611 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001612 *
1613 * @return {HTMLDocument}
rginda0918b652012-04-04 11:26:24 -07001614 */
rginda87b86462011-12-14 13:48:03 -08001615hterm.Terminal.prototype.getDocument = function() {
1616 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001617};
1618
1619/**
rginda0918b652012-04-04 11:26:24 -07001620 * Focus the terminal.
1621 */
1622hterm.Terminal.prototype.focus = function() {
1623 this.scrollPort_.focus();
1624};
1625
1626/**
rginda8ba33642011-12-14 12:31:31 -08001627 * Return the HTML Element for a given row index.
1628 *
1629 * This is a method from the RowProvider interface. The ScrollPort uses
1630 * it to fetch rows on demand as they are scrolled into view.
1631 *
1632 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1633 * pairs to conserve memory.
1634 *
1635 * @param {integer} index The zero-based row index, measured relative to the
1636 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001637 * largest indices.
rginda8ba33642011-12-14 12:31:31 -08001638 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1639 */
1640hterm.Terminal.prototype.getRowNode = function(index) {
1641 if (index < this.scrollbackRows_.length)
1642 return this.scrollbackRows_[index];
1643
1644 var screenIndex = index - this.scrollbackRows_.length;
1645 return this.screen_.rowsArray[screenIndex];
1646};
1647
1648/**
1649 * Return the text content for a given range of rows.
1650 *
1651 * This is a method from the RowProvider interface. The ScrollPort uses
1652 * it to fetch text content on demand when the user attempts to copy their
1653 * selection to the clipboard.
1654 *
1655 * @param {integer} start The zero-based row index to start from, measured
1656 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001657 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001658 * @param {integer} end The zero-based row index to end on, measured
1659 * relative to the start of the scrollback buffer.
1660 * @return {string} A single string containing the text value of the range of
1661 * rows. Lines will be newline delimited, with no trailing newline.
1662 */
1663hterm.Terminal.prototype.getRowsText = function(start, end) {
1664 var ary = [];
1665 for (var i = start; i < end; i++) {
1666 var node = this.getRowNode(i);
1667 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001668 if (i < end - 1 && !node.getAttribute('line-overflow'))
1669 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001670 }
1671
rgindaa09e7332012-08-17 12:49:51 -07001672 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001673};
1674
1675/**
1676 * Return the text content for a given row.
1677 *
1678 * This is a method from the RowProvider interface. The ScrollPort uses
1679 * it to fetch text content on demand when the user attempts to copy their
1680 * selection to the clipboard.
1681 *
1682 * @param {integer} index The zero-based row index to return, measured
1683 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001684 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001685 * @return {string} A string containing the text value of the selected row.
1686 */
1687hterm.Terminal.prototype.getRowText = function(index) {
1688 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001689 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001690};
1691
1692/**
1693 * Return the total number of rows in the addressable screen and in the
1694 * scrollback buffer of this terminal.
1695 *
1696 * This is a method from the RowProvider interface. The ScrollPort uses
1697 * it to compute the size of the scrollbar.
1698 *
1699 * @return {integer} The number of rows in this terminal.
1700 */
1701hterm.Terminal.prototype.getRowCount = function() {
1702 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1703};
1704
1705/**
1706 * Create DOM nodes for new rows and append them to the end of the terminal.
1707 *
1708 * This is the only correct way to add a new DOM node for a row. Notice that
1709 * the new row is appended to the bottom of the list of rows, and does not
1710 * require renumbering (of the rowIndex property) of previous rows.
1711 *
1712 * If you think you want a new blank row somewhere in the middle of the
1713 * terminal, look into moveRows_().
1714 *
1715 * This method does not pay attention to vtScrollTop/Bottom, since you should
1716 * be using moveRows() in cases where they would matter.
1717 *
1718 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001719 *
1720 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001721 */
1722hterm.Terminal.prototype.appendRows_ = function(count) {
1723 var cursorRow = this.screen_.rowsArray.length;
1724 var offset = this.scrollbackRows_.length + cursorRow;
1725 for (var i = 0; i < count; i++) {
1726 var row = this.document_.createElement('x-row');
1727 row.appendChild(this.document_.createTextNode(''));
1728 row.rowIndex = offset + i;
1729 this.screen_.pushRow(row);
1730 }
1731
1732 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1733 if (extraRows > 0) {
1734 var ary = this.screen_.shiftRows(extraRows);
1735 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001736 if (this.scrollPort_.isScrolledEnd)
1737 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001738 }
1739
1740 if (cursorRow >= this.screen_.rowsArray.length)
1741 cursorRow = this.screen_.rowsArray.length - 1;
1742
rginda87b86462011-12-14 13:48:03 -08001743 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001744};
1745
1746/**
1747 * Relocate rows from one part of the addressable screen to another.
1748 *
1749 * This is used to recycle rows during VT scrolls (those which are driven
1750 * by VT commands, rather than by the user manipulating the scrollbar.)
1751 *
1752 * In this case, the blank lines scrolled into the scroll region are made of
1753 * the nodes we scrolled off. These have their rowIndex properties carefully
1754 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001755 *
1756 * @param {number} fromIndex The start index.
1757 * @param {number} count The number of rows to move.
1758 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001759 */
1760hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1761 var ary = this.screen_.removeRows(fromIndex, count);
1762 this.screen_.insertRows(toIndex, ary);
1763
1764 var start, end;
1765 if (fromIndex < toIndex) {
1766 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001767 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001768 } else {
1769 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001770 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001771 }
1772
1773 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001774 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001775};
1776
1777/**
1778 * Renumber the rowIndex property of the given range of rows.
1779 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001780 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08001781 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001782 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001783 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05001784 *
1785 * @param {number} start The start index.
1786 * @param {number} end The end index.
1787 * @param {hterm.Screen} opt_screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08001788 */
Robert Ginda40932892012-12-10 17:26:40 -08001789hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1790 var screen = opt_screen || this.screen_;
1791
rginda8ba33642011-12-14 12:31:31 -08001792 var offset = this.scrollbackRows_.length;
1793 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001794 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001795 }
1796};
1797
1798/**
1799 * Print a string to the terminal.
1800 *
1801 * This respects the current insert and wraparound modes. It will add new lines
1802 * to the end of the terminal, scrolling off the top into the scrollback buffer
1803 * if necessary.
1804 *
1805 * The string is *not* parsed for escape codes. Use the interpret() method if
1806 * that's what you're after.
1807 *
1808 * @param{string} str The string to print.
1809 */
1810hterm.Terminal.prototype.print = function(str) {
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001811 this.scheduleSyncCursorPosition_();
1812
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001813 // Basic accessibility output for the screen reader.
Raymes Khoury177aec72018-06-26 10:58:53 +10001814 this.accessibilityReader_.announce(str);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001815
rgindaa9abdd82012-08-06 18:05:09 -07001816 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001817
Ricky Liang48f05cb2013-12-31 23:35:29 +08001818 var strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04001819 // Fun edge case: If the string only contains zero width codepoints (like
1820 // combining characters), we make sure to iterate at least once below.
1821 if (strWidth == 0 && str)
1822 strWidth = 1;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001823
1824 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001825 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1826 this.screen_.commitLineOverflow();
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001827 this.newLine(true);
rgindaa09e7332012-08-17 12:49:51 -07001828 }
rgindaa19afe22012-01-25 15:40:22 -08001829
Ricky Liang48f05cb2013-12-31 23:35:29 +08001830 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001831 var didOverflow = false;
1832 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001833
rgindaa9abdd82012-08-06 18:05:09 -07001834 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1835 didOverflow = true;
1836 count = this.screenSize.width - this.screen_.cursorPosition.column;
1837 }
rgindaa19afe22012-01-25 15:40:22 -08001838
rgindaa9abdd82012-08-06 18:05:09 -07001839 if (didOverflow && !this.options_.wraparound) {
1840 // If the string overflowed the line but wraparound is off, then the
1841 // last printed character should be the last of the string.
1842 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001843 substr = lib.wc.substr(str, startOffset, count - 1) +
1844 lib.wc.substr(str, strWidth - 1);
1845 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001846 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001847 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001848 }
rgindaa19afe22012-01-25 15:40:22 -08001849
Ricky Liang48f05cb2013-12-31 23:35:29 +08001850 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1851 for (var i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001852 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
1853 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001854
1855 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001856 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001857 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001858 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001859 }
1860 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001861 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07001862 }
1863
1864 this.screen_.maybeClipCurrentRow();
1865 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001866 }
rginda8ba33642011-12-14 12:31:31 -08001867
rginda9f5222b2012-03-05 11:53:28 -08001868 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001869 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001870};
1871
1872/**
rginda87b86462011-12-14 13:48:03 -08001873 * Set the VT scroll region.
1874 *
rginda87b86462011-12-14 13:48:03 -08001875 * This also resets the cursor position to the absolute (0, 0) position, since
1876 * that's what xterm appears to do.
1877 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001878 * Setting the scroll region to the full height of the terminal will clear
1879 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1880 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1881 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1882 * continue to work as most users would expect.
1883 *
rginda87b86462011-12-14 13:48:03 -08001884 * @param {integer} scrollTop The zero-based top of the scroll region.
1885 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1886 * inclusive.
1887 */
1888hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001889 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001890 this.vtScrollTop_ = null;
1891 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001892 } else {
1893 this.vtScrollTop_ = scrollTop;
1894 this.vtScrollBottom_ = scrollBottom;
1895 }
rginda87b86462011-12-14 13:48:03 -08001896};
1897
1898/**
rginda8ba33642011-12-14 12:31:31 -08001899 * Return the top row index according to the VT.
1900 *
1901 * This will return 0 unless the terminal has been told to restrict scrolling
1902 * to some lower row. It is used for some VT cursor positioning and scrolling
1903 * commands.
1904 *
1905 * @return {integer} The topmost row in the terminal's scroll region.
1906 */
1907hterm.Terminal.prototype.getVTScrollTop = function() {
1908 if (this.vtScrollTop_ != null)
1909 return this.vtScrollTop_;
1910
1911 return 0;
rginda87b86462011-12-14 13:48:03 -08001912};
rginda8ba33642011-12-14 12:31:31 -08001913
1914/**
1915 * Return the bottom row index according to the VT.
1916 *
1917 * This will return the height of the terminal unless the it has been told to
1918 * restrict scrolling to some higher row. It is used for some VT cursor
1919 * positioning and scrolling commands.
1920 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001921 * @return {integer} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001922 */
1923hterm.Terminal.prototype.getVTScrollBottom = function() {
1924 if (this.vtScrollBottom_ != null)
1925 return this.vtScrollBottom_;
1926
rginda87b86462011-12-14 13:48:03 -08001927 return this.screenSize.height - 1;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001928};
rginda8ba33642011-12-14 12:31:31 -08001929
1930/**
1931 * Process a '\n' character.
1932 *
1933 * If the cursor is on the final row of the terminal this will append a new
1934 * blank row to the screen and scroll the topmost row into the scrollback
1935 * buffer.
1936 *
1937 * Otherwise, this moves the cursor to column zero of the next row.
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001938 *
1939 * @param {boolean=} dueToOverflow Whether the newline is due to wraparound of
1940 * the terminal.
rginda8ba33642011-12-14 12:31:31 -08001941 */
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001942hterm.Terminal.prototype.newLine = function(dueToOverflow = false) {
1943 if (!dueToOverflow)
1944 this.accessibilityReader_.newLine();
1945
Robert Ginda9937abc2013-07-25 16:09:23 -07001946 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1947 this.screen_.rowsArray.length - 1);
1948
1949 if (this.vtScrollBottom_ != null) {
1950 // A VT Scroll region is active, we never append new rows.
1951 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1952 // We're at the end of the VT Scroll Region, perform a VT scroll.
1953 this.vtScrollUp(1);
1954 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1955 } else if (cursorAtEndOfScreen) {
1956 // We're at the end of the screen, the only thing to do is put the
1957 // cursor to column 0.
1958 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1959 } else {
1960 // Anywhere else, advance the cursor row, and reset the column.
1961 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1962 }
1963 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001964 // We're at the end of the screen. Append a new row to the terminal,
1965 // shifting the top row into the scrollback.
1966 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001967 } else {
rginda87b86462011-12-14 13:48:03 -08001968 // Anywhere else in the screen just moves the cursor.
1969 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001970 }
1971};
1972
1973/**
1974 * Like newLine(), except maintain the cursor column.
1975 */
1976hterm.Terminal.prototype.lineFeed = function() {
1977 var column = this.screen_.cursorPosition.column;
1978 this.newLine();
1979 this.setCursorColumn(column);
1980};
1981
1982/**
rginda87b86462011-12-14 13:48:03 -08001983 * If autoCarriageReturn is set then newLine(), else lineFeed().
1984 */
1985hterm.Terminal.prototype.formFeed = function() {
1986 if (this.options_.autoCarriageReturn) {
1987 this.newLine();
1988 } else {
1989 this.lineFeed();
1990 }
1991};
1992
1993/**
1994 * Move the cursor up one row, possibly inserting a blank line.
1995 *
1996 * The cursor column is not changed.
1997 */
1998hterm.Terminal.prototype.reverseLineFeed = function() {
1999 var scrollTop = this.getVTScrollTop();
2000 var currentRow = this.screen_.cursorPosition.row;
2001
2002 if (currentRow == scrollTop) {
2003 this.insertLines(1);
2004 } else {
2005 this.setAbsoluteCursorRow(currentRow - 1);
2006 }
2007};
2008
2009/**
rginda8ba33642011-12-14 12:31:31 -08002010 * Replace all characters to the left of the current cursor with the space
2011 * character.
2012 *
2013 * TODO(rginda): This should probably *remove* the characters (not just replace
2014 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07002015 * position.
rginda8ba33642011-12-14 12:31:31 -08002016 */
2017hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08002018 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002019 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002020 const count = cursor.column + 1;
2021 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08002022 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002023};
2024
2025/**
David Benjamin684a9b72012-05-01 17:19:58 -04002026 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08002027 *
2028 * The cursor position is unchanged.
2029 *
Robert Gindaf2547f12012-10-25 20:36:21 -07002030 * If the current background color is not the default background color this
2031 * will insert spaces rather than delete. This is unfortunate because the
2032 * trailing space will affect text selection, but it's difficult to come up
2033 * with a way to style empty space that wouldn't trip up the hterm.Screen
2034 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07002035 *
2036 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
2037 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
2038 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05002039 *
2040 * @param {number} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08002041 */
2042hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07002043 if (this.screen_.cursorPosition.overflow)
2044 return;
2045
Robert Ginda7fd57082012-09-25 14:41:47 -07002046 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
2047 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07002048
2049 if (this.screen_.textAttributes.background ===
2050 this.screen_.textAttributes.DEFAULT_COLOR) {
2051 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08002052 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07002053 this.screen_.cursorPosition.column + count) {
2054 this.screen_.deleteChars(count);
2055 this.clearCursorOverflow();
2056 return;
2057 }
2058 }
2059
rginda87b86462011-12-14 13:48:03 -08002060 var cursor = this.saveCursor();
Mike Frysinger6380bed2017-08-24 18:46:39 -04002061 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08002062 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002063 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002064};
2065
2066/**
2067 * Erase the current line.
2068 *
2069 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002070 */
2071hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08002072 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002073 this.screen_.clearCursorRow();
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/**
David Benjamina08d78f2012-05-05 00:28:49 -04002079 * Erase all characters from the start of the screen to the current cursor
2080 * position, 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.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08002085 var cursor = this.saveCursor();
2086
2087 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002088
David Benjamina08d78f2012-05-05 00:28:49 -04002089 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002090 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002091 this.screen_.clearCursorRow();
2092 }
2093
rginda87b86462011-12-14 13:48:03 -08002094 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002095 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002096};
2097
2098/**
2099 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002100 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002101 *
2102 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002103 */
2104hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08002105 var cursor = this.saveCursor();
2106
2107 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002108
David Benjamina08d78f2012-05-05 00:28:49 -04002109 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08002110 for (var i = cursor.row + 1; i <= bottom; i++) {
2111 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002112 this.screen_.clearCursorRow();
2113 }
2114
rginda87b86462011-12-14 13:48:03 -08002115 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002116 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002117};
2118
2119/**
2120 * Fill the terminal with a given character.
2121 *
2122 * This methods does not respect the VT scroll region.
2123 *
2124 * @param {string} ch The character to use for the fill.
2125 */
2126hterm.Terminal.prototype.fill = function(ch) {
2127 var cursor = this.saveCursor();
2128
2129 this.setAbsoluteCursorPosition(0, 0);
2130 for (var row = 0; row < this.screenSize.height; row++) {
2131 for (var col = 0; col < this.screenSize.width; col++) {
2132 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002133 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002134 }
2135 }
2136
2137 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002138};
2139
2140/**
rginda9ea433c2012-03-16 11:57:00 -07002141 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002142 *
rginda9ea433c2012-03-16 11:57:00 -07002143 * This does not respect the scroll region.
2144 *
2145 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2146 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002147 */
rginda9ea433c2012-03-16 11:57:00 -07002148hterm.Terminal.prototype.clearHome = function(opt_screen) {
2149 var screen = opt_screen || this.screen_;
2150 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002151
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002152 this.accessibilityReader_.clear();
2153
rginda11057d52012-04-25 12:29:56 -07002154 if (bottom == 0) {
2155 // Empty screen, nothing to do.
2156 return;
2157 }
2158
rgindae4d29232012-01-19 10:47:13 -08002159 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002160 screen.setCursorPosition(i, 0);
2161 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002162 }
2163
rginda9ea433c2012-03-16 11:57:00 -07002164 screen.setCursorPosition(0, 0);
2165};
2166
2167/**
2168 * Erase the entire display without changing the cursor position.
2169 *
2170 * The cursor position is unchanged. This does not respect the scroll
2171 * region.
2172 *
2173 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2174 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002175 */
2176hterm.Terminal.prototype.clear = function(opt_screen) {
2177 var screen = opt_screen || this.screen_;
2178 var cursor = screen.cursorPosition.clone();
2179 this.clearHome(screen);
2180 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002181};
2182
2183/**
2184 * VT command to insert lines at the current cursor row.
2185 *
2186 * This respects the current scroll region. Rows pushed off the bottom are
2187 * lost (they won't show up in the scrollback buffer).
2188 *
rginda8ba33642011-12-14 12:31:31 -08002189 * @param {integer} count The number of lines to insert.
2190 */
2191hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07002192 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002193
2194 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002195 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002196
Robert Ginda579186b2012-09-26 11:40:04 -07002197 // The moveCount is the number of rows we need to relocate to make room for
2198 // the new row(s). The count is the distance to move them.
2199 var moveCount = bottom - cursorRow - count + 1;
2200 if (moveCount)
2201 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08002202
Robert Ginda579186b2012-09-26 11:40:04 -07002203 for (var i = count - 1; i >= 0; i--) {
2204 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002205 this.screen_.clearCursorRow();
2206 }
rginda8ba33642011-12-14 12:31:31 -08002207};
2208
2209/**
2210 * VT command to delete lines at the current cursor row.
2211 *
2212 * New rows are added to the bottom of scroll region to take their place. New
2213 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002214 *
2215 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002216 */
2217hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08002218 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002219
rginda87b86462011-12-14 13:48:03 -08002220 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08002221 var bottom = this.getVTScrollBottom();
2222
rginda87b86462011-12-14 13:48:03 -08002223 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002224 count = Math.min(count, maxCount);
2225
rginda87b86462011-12-14 13:48:03 -08002226 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08002227 if (count != maxCount)
2228 this.moveRows_(top, count, moveStart);
2229
2230 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002231 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002232 this.screen_.clearCursorRow();
2233 }
2234
rginda87b86462011-12-14 13:48:03 -08002235 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002236 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002237};
2238
2239/**
2240 * Inserts the given number of spaces at the current cursor position.
2241 *
rginda87b86462011-12-14 13:48:03 -08002242 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002243 *
2244 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002245 */
2246hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08002247 var cursor = this.saveCursor();
2248
rgindacbbd7482012-06-13 15:06:16 -07002249 var ws = lib.f.getWhitespace(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002250 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002251 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002252
2253 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002254 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002255};
2256
2257/**
2258 * Forward-delete the specified number of characters starting at the cursor
2259 * position.
2260 *
2261 * @param {integer} count The number of characters to delete.
2262 */
2263hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002264 var deleted = this.screen_.deleteChars(count);
2265 if (deleted && !this.screen_.textAttributes.isDefault()) {
2266 var cursor = this.saveCursor();
2267 this.setCursorColumn(this.screenSize.width - deleted);
2268 this.screen_.insertString(lib.f.getWhitespace(deleted));
2269 this.restoreCursor(cursor);
2270 }
2271
David Benjamin54e8bf62012-06-01 22:31:40 -04002272 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002273};
2274
2275/**
2276 * Shift rows in the scroll region upwards by a given number of lines.
2277 *
2278 * New rows are inserted at the bottom of the scroll region to fill the
2279 * vacated rows. The new rows not filled out with the current text attributes.
2280 *
2281 * This function does not affect the scrollback rows at all. Rows shifted
2282 * off the top are lost.
2283 *
rginda87b86462011-12-14 13:48:03 -08002284 * The cursor position is not altered.
2285 *
rginda8ba33642011-12-14 12:31:31 -08002286 * @param {integer} count The number of rows to scroll.
2287 */
2288hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002289 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002290
rginda87b86462011-12-14 13:48:03 -08002291 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002292 this.deleteLines(count);
2293
rginda87b86462011-12-14 13:48:03 -08002294 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002295};
2296
2297/**
2298 * Shift rows below the cursor down by a given number of lines.
2299 *
2300 * This function respects the current scroll region.
2301 *
2302 * New rows are inserted at the top of the scroll region to fill the
2303 * vacated rows. The new rows not filled out with the current text attributes.
2304 *
2305 * This function does not affect the scrollback rows at all. Rows shifted
2306 * off the bottom are lost.
2307 *
2308 * @param {integer} count The number of rows to scroll.
2309 */
2310hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08002311 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002312
rginda87b86462011-12-14 13:48:03 -08002313 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08002314 this.insertLines(opt_count);
2315
rginda87b86462011-12-14 13:48:03 -08002316 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002317};
2318
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002319/**
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002320 * Enable accessibility-friendly features that have a performance impact.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002321 *
2322 * This will generate additional DOM nodes in an aria-live region that will
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002323 * cause Assitive Technology to announce the output of the terminal. It also
2324 * enables other features that aid assistive technology. All the features gated
2325 * behind this flag have a performance impact on the terminal which is why they
2326 * are made optional.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002327 *
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002328 * @param {boolean} enabled Whether to enable accessibility-friendly features.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002329 */
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002330hterm.Terminal.prototype.setAccessibilityEnabled = function(enabled) {
Raymes Khoury177aec72018-06-26 10:58:53 +10002331 this.accessibilityReader_.setAccessibilityEnabled(enabled);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002332};
rginda87b86462011-12-14 13:48:03 -08002333
rginda8ba33642011-12-14 12:31:31 -08002334/**
2335 * Set the cursor position.
2336 *
2337 * The cursor row is relative to the scroll region if the terminal has
2338 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2339 *
2340 * @param {integer} row The new zero-based cursor row.
2341 * @param {integer} row The new zero-based cursor column.
2342 */
2343hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2344 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002345 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002346 } else {
rginda87b86462011-12-14 13:48:03 -08002347 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002348 }
rginda87b86462011-12-14 13:48:03 -08002349};
rginda8ba33642011-12-14 12:31:31 -08002350
Evan Jones2600d4f2016-12-06 09:29:36 -05002351/**
2352 * Move the cursor relative to its current position.
2353 *
2354 * @param {number} row
2355 * @param {number} column
2356 */
rginda87b86462011-12-14 13:48:03 -08002357hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2358 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002359 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2360 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002361 this.screen_.setCursorPosition(row, column);
2362};
2363
Evan Jones2600d4f2016-12-06 09:29:36 -05002364/**
2365 * Move the cursor to the specified position.
2366 *
2367 * @param {number} row
2368 * @param {number} column
2369 */
rginda87b86462011-12-14 13:48:03 -08002370hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002371 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2372 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002373 this.screen_.setCursorPosition(row, column);
2374};
2375
2376/**
2377 * Set the cursor column.
2378 *
2379 * @param {integer} column The new zero-based cursor column.
2380 */
2381hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002382 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002383};
2384
2385/**
2386 * Return the cursor column.
2387 *
2388 * @return {integer} The zero-based cursor column.
2389 */
2390hterm.Terminal.prototype.getCursorColumn = function() {
2391 return this.screen_.cursorPosition.column;
2392};
2393
2394/**
2395 * Set the cursor row.
2396 *
2397 * The cursor row is relative to the scroll region if the terminal has
2398 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2399 *
2400 * @param {integer} row The new cursor row.
2401 */
rginda87b86462011-12-14 13:48:03 -08002402hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2403 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002404};
2405
2406/**
2407 * Return the cursor row.
2408 *
2409 * @return {integer} The zero-based cursor row.
2410 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002411hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002412 return this.screen_.cursorPosition.row;
2413};
2414
2415/**
2416 * Request that the ScrollPort redraw itself soon.
2417 *
2418 * The redraw will happen asynchronously, soon after the call stack winds down.
2419 * Multiple calls will be coalesced into a single redraw.
2420 */
2421hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002422 if (this.timeouts_.redraw)
2423 return;
rginda8ba33642011-12-14 12:31:31 -08002424
2425 var self = this;
rginda87b86462011-12-14 13:48:03 -08002426 this.timeouts_.redraw = setTimeout(function() {
2427 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002428 self.scrollPort_.redraw_();
2429 }, 0);
2430};
2431
2432/**
2433 * Request that the ScrollPort be scrolled to the bottom.
2434 *
2435 * The scroll will happen asynchronously, soon after the call stack winds down.
2436 * Multiple calls will be coalesced into a single scroll.
2437 *
2438 * This affects the scrollbar position of the ScrollPort, and has nothing to
2439 * do with the VT scroll commands.
2440 */
2441hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2442 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002443 return;
rginda8ba33642011-12-14 12:31:31 -08002444
2445 var self = this;
2446 this.timeouts_.scrollDown = setTimeout(function() {
2447 delete self.timeouts_.scrollDown;
2448 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2449 }, 10);
2450};
2451
2452/**
2453 * Move the cursor up a specified number of rows.
2454 *
2455 * @param {integer} count The number of rows to move the cursor.
2456 */
2457hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002458 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002459};
2460
2461/**
2462 * Move the cursor down a specified number of rows.
2463 *
2464 * @param {integer} count The number of rows to move the cursor.
2465 */
2466hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002467 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002468 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2469 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2470 this.screenSize.height - 1);
2471
rgindacbbd7482012-06-13 15:06:16 -07002472 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002473 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002474 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002475};
2476
2477/**
2478 * Move the cursor left a specified number of columns.
2479 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002480 * If reverse wraparound mode is enabled and the previous row wrapped into
2481 * the current row then we back up through the wraparound as well.
2482 *
rginda8ba33642011-12-14 12:31:31 -08002483 * @param {integer} count The number of columns to move the cursor.
2484 */
2485hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002486 count = count || 1;
2487
2488 if (count < 1)
2489 return;
2490
2491 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002492 if (this.options_.reverseWraparound) {
2493 if (this.screen_.cursorPosition.overflow) {
2494 // If this cursor is in the right margin, consume one count to get it
2495 // back to the last column. This only applies when we're in reverse
2496 // wraparound mode.
2497 count--;
2498 this.clearCursorOverflow();
2499
2500 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002501 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002502 }
2503
Robert Gindabfb32622014-07-17 13:20:27 -07002504 var newRow = this.screen_.cursorPosition.row;
2505 var newColumn = currentColumn - count;
2506 if (newColumn < 0) {
2507 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2508 if (newRow < 0) {
2509 // xterm also wraps from row 0 to the last row.
2510 newRow = this.screenSize.height + newRow % this.screenSize.height;
2511 }
2512 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2513 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002514
Robert Gindabfb32622014-07-17 13:20:27 -07002515 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2516
2517 } else {
2518 var newColumn = Math.max(currentColumn - count, 0);
2519 this.setCursorColumn(newColumn);
2520 }
rginda8ba33642011-12-14 12:31:31 -08002521};
2522
2523/**
2524 * Move the cursor right a specified number of columns.
2525 *
2526 * @param {integer} count The number of columns to move the cursor.
2527 */
2528hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002529 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002530
2531 if (count < 1)
2532 return;
2533
rgindacbbd7482012-06-13 15:06:16 -07002534 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002535 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002536 this.setCursorColumn(column);
2537};
2538
2539/**
2540 * Reverse the foreground and background colors of the terminal.
2541 *
2542 * This only affects text that was drawn with no attributes.
2543 *
2544 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2545 * been drawn with attributes that happen to coincide with the default
2546 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002547 *
2548 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002549 */
2550hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002551 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002552 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002553 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2554 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002555 } else {
rginda9f5222b2012-03-05 11:53:28 -08002556 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2557 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002558 }
2559};
2560
2561/**
rginda87b86462011-12-14 13:48:03 -08002562 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002563 *
2564 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002565 */
2566hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002567 this.cursorNode_.style.backgroundColor =
2568 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002569
2570 var self = this;
2571 setTimeout(function() {
Matheus Fernandes2d733082017-09-11 06:43:01 -04002572 self.restyleCursor_();
rginda6d397402012-01-17 10:58:29 -08002573 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002574
Michael Kelly485ecd12014-06-09 11:41:56 -04002575 // bellSquelchTimeout_ affects both audio and notification bells.
2576 if (this.bellSquelchTimeout_)
2577 return;
2578
Robert Ginda92e18102013-03-14 13:56:37 -07002579 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002580 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002581 this.bellSequelchTimeout_ = setTimeout(function() {
2582 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002583 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002584 } else {
2585 delete this.bellSquelchTimeout_;
2586 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002587
2588 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingera5fb83c2017-06-22 14:48:35 -07002589 var n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002590 this.bellNotificationList_.push(n);
2591 // TODO: Should we try to raise the window here?
2592 n.onclick = function() { self.closeBellNotifications_(); };
2593 }
rginda87b86462011-12-14 13:48:03 -08002594};
2595
2596/**
rginda8ba33642011-12-14 12:31:31 -08002597 * Set the origin mode bit.
2598 *
2599 * If origin mode is on, certain VT cursor and scrolling commands measure their
2600 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2601 * to the top of the addressable screen.
2602 *
2603 * Defaults to off.
2604 *
2605 * @param {boolean} state True to set origin mode, false to unset.
2606 */
2607hterm.Terminal.prototype.setOriginMode = function(state) {
2608 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002609 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002610};
2611
2612/**
2613 * Set the insert mode bit.
2614 *
2615 * If insert mode is on, existing text beyond the cursor position will be
2616 * shifted right to make room for new text. Otherwise, new text overwrites
2617 * any existing text.
2618 *
2619 * Defaults to off.
2620 *
2621 * @param {boolean} state True to set insert mode, false to unset.
2622 */
2623hterm.Terminal.prototype.setInsertMode = function(state) {
2624 this.options_.insertMode = state;
2625};
2626
2627/**
rginda87b86462011-12-14 13:48:03 -08002628 * Set the auto carriage return bit.
2629 *
2630 * If auto carriage return is on then a formfeed character is interpreted
2631 * as a newline, otherwise it's the same as a linefeed. The difference boils
2632 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002633 *
2634 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002635 */
2636hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2637 this.options_.autoCarriageReturn = state;
2638};
2639
2640/**
rginda8ba33642011-12-14 12:31:31 -08002641 * Set the wraparound mode bit.
2642 *
2643 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2644 * to the start of the following row. Otherwise, the cursor is clamped to the
2645 * end of the screen and attempts to write past it are ignored.
2646 *
2647 * Defaults to on.
2648 *
2649 * @param {boolean} state True to set wraparound mode, false to unset.
2650 */
2651hterm.Terminal.prototype.setWraparound = function(state) {
2652 this.options_.wraparound = state;
2653};
2654
2655/**
2656 * Set the reverse-wraparound mode bit.
2657 *
2658 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2659 * to the end of the previous row. Otherwise, the cursor is clamped to column
2660 * 0.
2661 *
2662 * Defaults to off.
2663 *
2664 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2665 */
2666hterm.Terminal.prototype.setReverseWraparound = function(state) {
2667 this.options_.reverseWraparound = state;
2668};
2669
2670/**
2671 * Selects between the primary and alternate screens.
2672 *
2673 * If alternate mode is on, the alternate screen is active. Otherwise the
2674 * primary screen is active.
2675 *
2676 * Swapping screens has no effect on the scrollback buffer.
2677 *
2678 * Each screen maintains its own cursor position.
2679 *
2680 * Defaults to off.
2681 *
2682 * @param {boolean} state True to set alternate mode, false to unset.
2683 */
2684hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002685 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002686 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2687
rginda35c456b2012-02-09 17:29:05 -08002688 if (this.screen_.rowsArray.length &&
2689 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2690 // If the screen changed sizes while we were away, our rowIndexes may
2691 // be incorrect.
2692 var offset = this.scrollbackRows_.length;
2693 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002694 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002695 ary[i].rowIndex = offset + i;
2696 }
2697 }
rginda8ba33642011-12-14 12:31:31 -08002698
rginda35c456b2012-02-09 17:29:05 -08002699 this.realizeWidth_(this.screenSize.width);
2700 this.realizeHeight_(this.screenSize.height);
2701 this.scrollPort_.syncScrollHeight();
2702 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002703
rginda6d397402012-01-17 10:58:29 -08002704 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002705 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002706};
2707
2708/**
2709 * Set the cursor-blink mode bit.
2710 *
2711 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2712 * a visible cursor does not blink.
2713 *
2714 * You should make sure to turn blinking off if you're going to dispose of a
2715 * terminal, otherwise you'll leak a timeout.
2716 *
2717 * Defaults to on.
2718 *
2719 * @param {boolean} state True to set cursor-blink mode, false to unset.
2720 */
2721hterm.Terminal.prototype.setCursorBlink = function(state) {
2722 this.options_.cursorBlink = state;
2723
2724 if (!state && this.timeouts_.cursorBlink) {
2725 clearTimeout(this.timeouts_.cursorBlink);
2726 delete this.timeouts_.cursorBlink;
2727 }
2728
2729 if (this.options_.cursorVisible)
2730 this.setCursorVisible(true);
2731};
2732
2733/**
2734 * Set the cursor-visible mode bit.
2735 *
2736 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2737 *
2738 * Defaults to on.
2739 *
2740 * @param {boolean} state True to set cursor-visible mode, false to unset.
2741 */
2742hterm.Terminal.prototype.setCursorVisible = function(state) {
2743 this.options_.cursorVisible = state;
2744
2745 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002746 if (this.timeouts_.cursorBlink) {
2747 clearTimeout(this.timeouts_.cursorBlink);
2748 delete this.timeouts_.cursorBlink;
2749 }
rginda87b86462011-12-14 13:48:03 -08002750 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002751 return;
2752 }
2753
rginda87b86462011-12-14 13:48:03 -08002754 this.syncCursorPosition_();
2755
2756 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002757
2758 if (this.options_.cursorBlink) {
2759 if (this.timeouts_.cursorBlink)
2760 return;
2761
Robert Gindaea2183e2014-07-17 09:51:51 -07002762 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002763 } else {
2764 if (this.timeouts_.cursorBlink) {
2765 clearTimeout(this.timeouts_.cursorBlink);
2766 delete this.timeouts_.cursorBlink;
2767 }
2768 }
2769};
2770
2771/**
rginda87b86462011-12-14 13:48:03 -08002772 * Synchronizes the visible cursor and document selection with the current
2773 * cursor coordinates.
Raymes Khourye5d48982018-08-02 09:08:32 +10002774 *
2775 * @return {boolean} True if the cursor is onscreen and synced.
rginda8ba33642011-12-14 12:31:31 -08002776 */
2777hterm.Terminal.prototype.syncCursorPosition_ = function() {
2778 var topRowIndex = this.scrollPort_.getTopRowIndex();
2779 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2780 var cursorRowIndex = this.scrollbackRows_.length +
2781 this.screen_.cursorPosition.row;
2782
Raymes Khoury15697f42018-07-17 11:37:18 +10002783 let forceSyncSelection = false;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002784 if (this.accessibilityReader_.accessibilityEnabled) {
2785 // Report the new position of the cursor for accessibility purposes.
2786 const cursorColumnIndex = this.screen_.cursorPosition.column;
2787 const cursorLineText =
2788 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
Raymes Khoury15697f42018-07-17 11:37:18 +10002789 // This will force the selection to be sync'd to the cursor position if the
2790 // user has pressed a key. Generally we would only sync the cursor position
2791 // when selection is collapsed so that if the user has selected something
2792 // we don't clear the selection by moving the selection. However when a
2793 // screen reader is used, it's intuitive for entering a key to move the
2794 // selection to the cursor.
2795 forceSyncSelection = this.accessibilityReader_.hasUserGesture;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002796 this.accessibilityReader_.afterCursorChange(
2797 cursorLineText, cursorRowIndex, cursorColumnIndex);
2798 }
2799
rginda8ba33642011-12-14 12:31:31 -08002800 if (cursorRowIndex > bottomRowIndex) {
2801 // Cursor is scrolled off screen, move it outside of the visible area.
Mike Frysinger44c32202017-08-05 01:13:09 -04002802 this.setCssVar('cursor-offset-row', '-1');
Raymes Khourye5d48982018-08-02 09:08:32 +10002803 return false;
rginda8ba33642011-12-14 12:31:31 -08002804 }
2805
Robert Gindab837c052014-08-11 11:17:51 -07002806 if (this.options_.cursorVisible &&
2807 this.cursorNode_.style.display == 'none') {
2808 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2809 this.cursorNode_.style.display = '';
2810 }
2811
Mike Frysinger44c32202017-08-05 01:13:09 -04002812 // Position the cursor using CSS variable math. If we do the math in JS,
2813 // the float math will end up being more precise than the CSS which will
2814 // cause the cursor tracking to be off.
2815 this.setCssVar(
2816 'cursor-offset-row',
2817 `${cursorRowIndex - topRowIndex} + ` +
2818 `${this.scrollPort_.visibleRowTopMargin}px`);
2819 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08002820
2821 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04002822 '(' + this.screen_.cursorPosition.column +
2823 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08002824 ')');
2825
2826 // Update the caret for a11y purposes.
2827 var selection = this.document_.getSelection();
Raymes Khoury15697f42018-07-17 11:37:18 +10002828 if (selection && (selection.isCollapsed || forceSyncSelection)) {
rginda87b86462011-12-14 13:48:03 -08002829 this.screen_.syncSelectionCaret(selection);
Raymes Khoury15697f42018-07-17 11:37:18 +10002830 }
Raymes Khourye5d48982018-08-02 09:08:32 +10002831 return true;
rginda8ba33642011-12-14 12:31:31 -08002832};
2833
Robert Gindafb1be6a2013-12-11 11:56:22 -08002834/**
2835 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2836 * and character cell dimensions.
2837 */
Robert Ginda830583c2013-08-07 13:20:46 -07002838hterm.Terminal.prototype.restyleCursor_ = function() {
2839 var shape = this.cursorShape_;
2840
2841 if (this.cursorNode_.getAttribute('focus') == 'false') {
2842 // Always show a block cursor when unfocused.
2843 shape = hterm.Terminal.cursorShape.BLOCK;
2844 }
2845
2846 var style = this.cursorNode_.style;
2847
2848 switch (shape) {
2849 case hterm.Terminal.cursorShape.BEAM:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002850 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002851 style.backgroundColor = 'transparent';
2852 style.borderBottomStyle = null;
2853 style.borderLeftStyle = 'solid';
2854 break;
2855
2856 case hterm.Terminal.cursorShape.UNDERLINE:
2857 style.height = this.scrollPort_.characterSize.baseline + 'px';
2858 style.backgroundColor = 'transparent';
2859 style.borderBottomStyle = 'solid';
2860 // correct the size to put it exactly at the baseline
2861 style.borderLeftStyle = null;
2862 break;
2863
2864 default:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002865 style.height = 'var(--hterm-charsize-height)';
Mike Frysinger2fd079a2018-09-02 01:46:12 -04002866 style.backgroundColor = 'var(--hterm-cursor-color)';
Robert Ginda830583c2013-08-07 13:20:46 -07002867 style.borderBottomStyle = null;
2868 style.borderLeftStyle = null;
2869 break;
2870 }
2871};
2872
rginda8ba33642011-12-14 12:31:31 -08002873/**
2874 * Synchronizes the visible cursor with the current cursor coordinates.
2875 *
2876 * The sync will happen asynchronously, soon after the call stack winds down.
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002877 * Multiple calls will be coalesced into a single sync. This should be called
2878 * prior to the cursor actually changing position.
rginda8ba33642011-12-14 12:31:31 -08002879 */
2880hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2881 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002882 return;
rginda8ba33642011-12-14 12:31:31 -08002883
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002884 if (this.accessibilityReader_.accessibilityEnabled) {
2885 // Report the previous position of the cursor for accessibility purposes.
2886 const cursorRowIndex = this.scrollbackRows_.length +
2887 this.screen_.cursorPosition.row;
2888 const cursorColumnIndex = this.screen_.cursorPosition.column;
2889 const cursorLineText =
2890 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
2891 this.accessibilityReader_.beforeCursorChange(
2892 cursorLineText, cursorRowIndex, cursorColumnIndex);
2893 }
2894
rginda8ba33642011-12-14 12:31:31 -08002895 var self = this;
2896 this.timeouts_.syncCursor = setTimeout(function() {
2897 self.syncCursorPosition_();
2898 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002899 }, 0);
2900};
2901
rgindacc2996c2012-02-24 14:59:31 -08002902/**
rgindaf522ce02012-04-17 17:49:17 -07002903 * Show or hide the zoom warning.
2904 *
2905 * The zoom warning is a message warning the user that their browser zoom must
2906 * be set to 100% in order for hterm to function properly.
2907 *
2908 * @param {boolean} state True to show the message, false to hide it.
2909 */
2910hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2911 if (!this.zoomWarningNode_) {
2912 if (!state)
2913 return;
2914
2915 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002916 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07002917 this.zoomWarningNode_.style.cssText = (
2918 'color: black;' +
2919 'background-color: #ff2222;' +
2920 'font-size: large;' +
2921 'border-radius: 8px;' +
2922 'opacity: 0.75;' +
2923 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2924 'top: 0.5em;' +
2925 'right: 1.2em;' +
2926 'position: absolute;' +
2927 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002928 '-webkit-user-select: none;' +
2929 '-moz-text-size-adjust: none;' +
2930 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05002931
2932 this.zoomWarningNode_.addEventListener('click', function(e) {
2933 this.parentNode.removeChild(this);
2934 });
rgindaf522ce02012-04-17 17:49:17 -07002935 }
2936
Robert Gindab4839c22013-02-28 16:52:10 -08002937 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2938 hterm.zoomWarningMessage,
2939 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2940
rgindaf522ce02012-04-17 17:49:17 -07002941 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2942
2943 if (state) {
2944 if (!this.zoomWarningNode_.parentNode)
2945 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2946 } else if (this.zoomWarningNode_.parentNode) {
2947 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2948 }
2949};
2950
2951/**
rgindacc2996c2012-02-24 14:59:31 -08002952 * Show the terminal overlay for a given amount of time.
2953 *
2954 * The terminal overlay appears in inverse video in a large font, centered
2955 * over the terminal. You should probably keep the overlay message brief,
2956 * since it's in a large font and you probably aren't going to check the size
2957 * of the terminal first.
2958 *
2959 * @param {string} msg The text (not HTML) message to display in the overlay.
2960 * @param {number} opt_timeout The amount of time to wait before fading out
2961 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2962 * stay up forever (or until the next overlay).
2963 */
2964hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002965 if (!this.overlayNode_) {
2966 if (!this.div_)
2967 return;
2968
2969 this.overlayNode_ = this.document_.createElement('div');
2970 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002971 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002972 'font-size: xx-large;' +
2973 'opacity: 0.75;' +
2974 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2975 'position: absolute;' +
2976 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002977 '-webkit-transition: opacity 180ms ease-in;' +
2978 '-moz-user-select: none;' +
2979 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002980
2981 this.overlayNode_.addEventListener('mousedown', function(e) {
2982 e.preventDefault();
2983 e.stopPropagation();
2984 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002985 }
2986
rginda9f5222b2012-03-05 11:53:28 -08002987 this.overlayNode_.style.color = this.prefs_.get('background-color');
2988 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2989 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2990
rgindaf0090c92012-02-10 14:58:52 -08002991 this.overlayNode_.textContent = msg;
2992 this.overlayNode_.style.opacity = '0.75';
2993
2994 if (!this.overlayNode_.parentNode)
2995 this.div_.appendChild(this.overlayNode_);
2996
Robert Ginda97769282013-02-01 15:30:30 -08002997 var divSize = hterm.getClientSize(this.div_);
2998 var overlaySize = hterm.getClientSize(this.overlayNode_);
2999
Robert Ginda8a59f762014-07-23 11:29:55 -07003000 this.overlayNode_.style.top =
3001 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08003002 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07003003 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08003004
rgindaf0090c92012-02-10 14:58:52 -08003005 if (this.overlayTimeout_)
3006 clearTimeout(this.overlayTimeout_);
3007
Raymes Khouryc7a06382018-07-04 10:25:45 +10003008 this.accessibilityReader_.assertiveAnnounce(msg);
3009
rgindacc2996c2012-02-24 14:59:31 -08003010 if (opt_timeout === null)
3011 return;
3012
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003013 this.overlayTimeout_ = setTimeout(() => {
3014 this.overlayNode_.style.opacity = '0';
3015 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
3016 }, opt_timeout || 1500);
3017};
3018
3019/**
3020 * Hide the terminal overlay immediately.
3021 *
3022 * Useful when we show an overlay for an event with an unknown end time.
3023 */
3024hterm.Terminal.prototype.hideOverlay = function() {
3025 if (this.overlayTimeout_)
3026 clearTimeout(this.overlayTimeout_);
3027 this.overlayTimeout_ = null;
3028
3029 if (this.overlayNode_.parentNode)
3030 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
3031 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08003032};
3033
rginda4bba5e12012-06-20 16:15:30 -07003034/**
3035 * Paste from the system clipboard to the terminal.
3036 */
3037hterm.Terminal.prototype.paste = function() {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003038 return hterm.pasteFromClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07003039};
3040
3041/**
3042 * Copy a string to the system clipboard.
3043 *
3044 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05003045 *
3046 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07003047 */
3048hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08003049 if (this.prefs_.get('enable-clipboard-notice'))
3050 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
3051
rgindaa09e7332012-08-17 12:49:51 -07003052 var copySource = this.document_.createElement('pre');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04003053 copySource.id = 'hterm:copy-to-clipboard-source';
rginda4bba5e12012-06-20 16:15:30 -07003054 copySource.textContent = str;
3055 copySource.style.cssText = (
3056 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003057 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07003058 'position: absolute;' +
3059 'top: -99px');
3060
3061 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07003062
rginda4bba5e12012-06-20 16:15:30 -07003063 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07003064 var anchorNode = selection.anchorNode;
3065 var anchorOffset = selection.anchorOffset;
3066 var focusNode = selection.focusNode;
3067 var focusOffset = selection.focusOffset;
3068
rginda4bba5e12012-06-20 16:15:30 -07003069 selection.selectAllChildren(copySource);
3070
rgindaa09e7332012-08-17 12:49:51 -07003071 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07003072
Rob Spies56953412014-04-28 14:09:47 -07003073 // IE doesn't support selection.extend. This means that the selection
3074 // won't return on IE.
Robert Ginda6f4f0aa2014-07-28 10:21:39 -07003075 if (selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07003076 selection.collapse(anchorNode, anchorOffset);
3077 selection.extend(focusNode, focusOffset);
3078 }
rgindafaa74742012-08-21 13:34:03 -07003079
rginda4bba5e12012-06-20 16:15:30 -07003080 copySource.parentNode.removeChild(copySource);
3081};
3082
Evan Jones2600d4f2016-12-06 09:29:36 -05003083/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003084 * Display an image.
3085 *
3086 * @param {Object} options The image to display.
3087 * @param {string=} options.name A human readable string for the image.
3088 * @param {string|number=} options.size The size (in bytes).
3089 * @param {boolean=} options.preserveAspectRatio Whether to preserve aspect.
3090 * @param {boolean=} options.inline Whether to display the image inline.
3091 * @param {string|number=} options.width The width of the image.
3092 * @param {string|number=} options.height The height of the image.
3093 * @param {string=} options.align Direction to align the image.
3094 * @param {string} options.uri The source URI for the image.
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003095 * @param {function=} onLoad Callback when loading finishes.
3096 * @param {function(Event)=} onError Callback when loading fails.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003097 */
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003098hterm.Terminal.prototype.displayImage = function(options, onLoad, onError) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003099 // Make sure we're actually given a resource to display.
3100 if (options.uri === undefined)
3101 return;
3102
3103 // Set up the defaults to simplify code below.
3104 if (!options.name)
3105 options.name = '';
3106
3107 // Has the user approved image display yet?
3108 if (this.allowImagesInline !== true) {
3109 this.newLine();
3110 const row = this.getRowNode(this.scrollbackRows_.length +
3111 this.getCursorRow() - 1);
3112
3113 if (this.allowImagesInline === false) {
3114 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
3115 'Inline Images Disabled');
3116 return;
3117 }
3118
3119 // Show a prompt.
3120 let button;
3121 const span = this.document_.createElement('span');
3122 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
3123 span.style.fontWeight = 'bold';
3124 span.style.borderWidth = '1px';
3125 span.style.borderStyle = 'dashed';
3126 button = this.document_.createElement('span');
3127 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3128 button.style.marginLeft = '1em';
3129 button.style.borderWidth = '1px';
3130 button.style.borderStyle = 'solid';
3131 button.addEventListener('click', () => {
3132 this.prefs_.set('allow-images-inline', false);
3133 });
3134 span.appendChild(button);
3135 button = this.document_.createElement('span');
3136 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3137 'allow this session');
3138 button.style.marginLeft = '1em';
3139 button.style.borderWidth = '1px';
3140 button.style.borderStyle = 'solid';
3141 button.addEventListener('click', () => {
3142 this.allowImagesInline = true;
3143 });
3144 span.appendChild(button);
3145 button = this.document_.createElement('span');
3146 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3147 button.style.marginLeft = '1em';
3148 button.style.borderWidth = '1px';
3149 button.style.borderStyle = 'solid';
3150 button.addEventListener('click', () => {
3151 this.prefs_.set('allow-images-inline', true);
3152 });
3153 span.appendChild(button);
3154
3155 row.appendChild(span);
3156 return;
3157 }
3158
3159 // See if we should show this object directly, or download it.
3160 if (options.inline) {
3161 const io = this.io.push();
3162 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
3163 'Loading $1 ...'), null);
3164
3165 // While we're loading the image, eat all the user's input.
3166 io.onVTKeystroke = io.sendString = () => {};
3167
3168 // Initialize this new image.
3169 const img = this.document_.createElement('img');
3170 img.src = options.uri;
3171 img.title = img.alt = options.name;
3172
3173 // Attach the image to the page to let it load/render. It won't stay here.
3174 // This is needed so it's visible and the DOM can calculate the height. If
3175 // the image is hidden or not in the DOM, the height is always 0.
3176 this.document_.body.appendChild(img);
3177
3178 // Wait for the image to finish loading before we try moving it to the
3179 // right place in the terminal.
3180 img.onload = () => {
3181 // Now that we have the image dimensions, figure out how to show it.
3182 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
3183 img.style.maxWidth = `${this.document_.body.clientWidth}px`;
3184 img.style.maxHeight = `${this.document_.body.clientHeight}px`;
3185
3186 // Parse a width/height specification.
3187 const parseDim = (dim, maxDim, cssVar) => {
3188 if (!dim || dim == 'auto')
3189 return '';
3190
3191 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3192 if (ary) {
3193 if (ary[2] == '%')
3194 return maxDim * parseInt(ary[1]) / 100 + 'px';
3195 else if (ary[2] == 'px')
3196 return dim;
3197 else
3198 return `calc(${dim} * var(${cssVar}))`;
3199 }
3200
3201 return '';
3202 };
3203 img.style.width =
3204 parseDim(options.width, this.document_.body.clientWidth,
3205 '--hterm-charsize-width');
3206 img.style.height =
3207 parseDim(options.height, this.document_.body.clientHeight,
3208 '--hterm-charsize-height');
3209
3210 // Figure out how many rows the image occupies, then add that many.
3211 // XXX: This count will be inaccurate if the font size changes on us.
3212 const padRows = Math.ceil(img.clientHeight /
3213 this.scrollPort_.characterSize.height);
3214 for (let i = 0; i < padRows; ++i)
3215 this.newLine();
3216
3217 // Update the max height in case the user shrinks the character size.
3218 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3219
3220 // Move the image to the last row. This way when we scroll up, it doesn't
3221 // disappear when the first row gets clipped. It will disappear when we
3222 // scroll down and the last row is clipped ...
3223 this.document_.body.removeChild(img);
3224 // Create a wrapper node so we can do an absolute in a relative position.
3225 // This helps with rounding errors between JS & CSS counts.
3226 const div = this.document_.createElement('div');
3227 div.style.position = 'relative';
3228 div.style.textAlign = options.align;
3229 img.style.position = 'absolute';
3230 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3231 div.appendChild(img);
3232 const row = this.getRowNode(this.scrollbackRows_.length +
3233 this.getCursorRow() - 1);
3234 row.appendChild(div);
3235
3236 io.hideOverlay();
3237 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003238
3239 if (onLoad)
3240 onLoad();
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003241 };
3242
3243 // If we got a malformed image, give up.
3244 img.onerror = (e) => {
3245 this.document_.body.removeChild(img);
3246 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
Mike Frysingere14a8c42018-03-10 00:17:30 -08003247 'Loading $1 failed'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003248 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003249
3250 if (onError)
3251 onError(e);
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003252 };
3253 } else {
3254 // We can't use chrome.downloads.download as that requires "downloads"
3255 // permissions, and that works only in extensions, not apps.
3256 const a = this.document_.createElement('a');
3257 a.href = options.uri;
3258 a.download = options.name;
3259 this.document_.body.appendChild(a);
3260 a.click();
3261 a.remove();
3262 }
3263};
3264
3265/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003266 * Returns the selected text, or null if no text is selected.
3267 *
3268 * @return {string|null}
3269 */
rgindaa09e7332012-08-17 12:49:51 -07003270hterm.Terminal.prototype.getSelectionText = function() {
3271 var selection = this.scrollPort_.selection;
3272 selection.sync();
3273
3274 if (selection.isCollapsed)
3275 return null;
3276
rgindaa09e7332012-08-17 12:49:51 -07003277 // Start offset measures from the beginning of the line.
3278 var startOffset = selection.startOffset;
3279 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003280
Raymes Khoury334625a2018-06-25 10:29:40 +10003281 // If an x-row isn't selected, |node| will be null.
3282 if (!node)
3283 return null;
3284
Robert Gindafdbb3f22012-09-06 20:23:06 -07003285 if (node.nodeName != 'X-ROW') {
3286 // If the selection doesn't start on an x-row node, then it must be
3287 // somewhere inside the x-row. Add any characters from previous siblings
3288 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003289
3290 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3291 // If node is the text node in a styled span, move up to the span node.
3292 node = node.parentNode;
3293 }
3294
Robert Gindafdbb3f22012-09-06 20:23:06 -07003295 while (node.previousSibling) {
3296 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003297 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003298 }
rgindaa09e7332012-08-17 12:49:51 -07003299 }
3300
3301 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08003302 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
3303 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003304 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003305
Robert Gindafdbb3f22012-09-06 20:23:06 -07003306 if (node.nodeName != 'X-ROW') {
3307 // If the selection doesn't end on an x-row node, then it must be
3308 // somewhere inside the x-row. Add any characters from following siblings
3309 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003310
3311 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3312 // If node is the text node in a styled span, move up to the span node.
3313 node = node.parentNode;
3314 }
3315
Robert Gindafdbb3f22012-09-06 20:23:06 -07003316 while (node.nextSibling) {
3317 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003318 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003319 }
rgindaa09e7332012-08-17 12:49:51 -07003320 }
3321
3322 var rv = this.getRowsText(selection.startRow.rowIndex,
3323 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003324 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003325};
3326
rginda4bba5e12012-06-20 16:15:30 -07003327/**
3328 * Copy the current selection to the system clipboard, then clear it after a
3329 * short delay.
3330 */
3331hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07003332 var text = this.getSelectionText();
3333 if (text != null)
3334 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07003335};
3336
rgindaf0090c92012-02-10 14:58:52 -08003337hterm.Terminal.prototype.overlaySize = function() {
3338 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
3339};
3340
rginda87b86462011-12-14 13:48:03 -08003341/**
3342 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3343 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003344 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003345 */
3346hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08003347 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08003348 this.scrollPort_.scrollRowToBottom(this.getRowCount());
3349
Robert Ginda8cb7d902013-06-20 14:37:18 -07003350 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08003351};
3352
3353/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003354 * Open the selected url.
3355 */
3356hterm.Terminal.prototype.openSelectedUrl_ = function() {
3357 var str = this.getSelectionText();
3358
3359 // If there is no selection, try and expand wherever they clicked.
3360 if (str == null) {
John Lincae9b732018-03-08 13:56:35 +08003361 this.screen_.expandSelectionForUrl(this.document_.getSelection());
Mike Frysinger70b94692017-01-26 18:57:50 -10003362 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003363
3364 // If clicking in empty space, return.
3365 if (str == null)
3366 return;
Mike Frysinger70b94692017-01-26 18:57:50 -10003367 }
3368
3369 // Make sure URL is valid before opening.
3370 if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0)
3371 return;
Mike Frysinger43472622017-06-26 18:11:07 -04003372
3373 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003374 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003375 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3376 // We have to whitelist a few protocols that lack authorities and thus
3377 // never use the //. Like mailto.
3378 switch (str.split(':', 1)[0]) {
3379 case 'mailto':
3380 break;
3381 default:
3382 str = 'http://' + str;
3383 break;
3384 }
3385 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003386
Mike Frysinger720fa832017-10-23 01:15:52 -04003387 hterm.openUrl(str);
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003388};
Mike Frysinger70b94692017-01-26 18:57:50 -10003389
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003390/**
3391 * Manage the automatic mouse hiding behavior while typing.
3392 *
3393 * @param {boolean=} v Whether to enable automatic hiding.
3394 */
3395hterm.Terminal.prototype.setAutomaticMouseHiding = function(v=null) {
3396 // Since Chrome OS & macOS do this by default everywhere, we don't need to.
3397 // Linux & Windows seem to leave this to specific applications to manage.
3398 if (v === null)
3399 v = (hterm.os != 'cros' && hterm.os != 'mac');
3400
3401 this.mouseHideWhileTyping_ = !!v;
3402};
3403
3404/**
3405 * Handler for monitoring user keyboard activity.
3406 *
3407 * This isn't for processing the keystrokes directly, but for updating any
3408 * state that might toggle based on the user using the keyboard at all.
3409 *
3410 * @param {KeyboardEvent} e The keyboard event that triggered us.
3411 */
3412hterm.Terminal.prototype.onKeyboardActivity_ = function(e) {
3413 // When the user starts typing, hide the mouse cursor.
3414 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_)
3415 this.setCssVar('mouse-cursor-style', 'none');
3416};
Mike Frysinger70b94692017-01-26 18:57:50 -10003417
3418/**
rgindad5613292012-06-19 15:40:37 -07003419 * Add the terminalRow and terminalColumn properties to mouse events and
3420 * then forward on to onMouse().
3421 *
3422 * The terminalRow and terminalColumn properties contain the (row, column)
3423 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003424 *
3425 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003426 */
3427hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003428 if (e.processedByTerminalHandler_) {
3429 // We register our event handlers on the document, as well as the cursor
3430 // and the scroll blocker. Mouse events that occur on the cursor or
3431 // scroll blocker will also appear on the document, but we don't want to
3432 // process them twice.
3433 //
3434 // We can't just prevent bubbling because that has other side effects, so
3435 // we decorate the event object with this property instead.
3436 return;
3437 }
3438
Mike Frysinger468966c2018-08-28 13:48:51 -04003439 // Consume navigation events. Button 3 is usually "browser back" and
3440 // button 4 is "browser forward" which we don't want to happen.
3441 if (e.button > 2) {
3442 e.preventDefault();
3443 // We don't return so click events can be passed to the remote below.
3444 }
3445
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003446 var reportMouseEvents = (!this.defeatMouseReports_ &&
3447 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3448
rgindafaa74742012-08-21 13:34:03 -07003449 e.processedByTerminalHandler_ = true;
3450
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003451 // Handle auto hiding of mouse cursor while typing.
3452 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
3453 // Make sure the mouse cursor is visible.
3454 this.syncMouseStyle();
3455 // This debounce isn't perfect, but should work well enough for such a
3456 // simple implementation. If the user moved the mouse, we enabled this
3457 // debounce, and then moved the mouse just before the timeout, we wouldn't
3458 // debounce that later movement.
3459 this.mouseHideDelay_ = setTimeout(() => this.mouseHideDelay_ = null, 1000);
3460 }
3461
Robert Gindaeda48db2014-07-17 09:25:30 -07003462 // One based row/column stored on the mouse event.
3463 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
3464 this.scrollPort_.characterSize.height) + 1;
3465 e.terminalColumn = parseInt(e.clientX /
3466 this.scrollPort_.characterSize.width) + 1;
3467
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003468 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
3469 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07003470 return;
3471 }
3472
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003473 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07003474 // If the cursor is visible and we're not sending mouse events to the
3475 // host app, then we want to hide the terminal cursor when the mouse
3476 // cursor is over top. This keeps the terminal cursor from interfering
3477 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003478 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3479 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3480 this.cursorNode_.style.display = 'none';
3481 } else if (this.cursorNode_.style.display == 'none') {
3482 this.cursorNode_.style.display = '';
3483 }
3484 }
rgindad5613292012-06-19 15:40:37 -07003485
Robert Ginda928cf632014-03-05 15:07:41 -08003486 if (e.type == 'mousedown') {
Mike Frysingercc114512017-09-11 21:39:17 -04003487 this.contextMenu.hide(e);
3488
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003489 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003490 // If VT mouse reporting is disabled, or has been defeated with
3491 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003492 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003493 this.setSelectionEnabled(true);
3494 } else {
3495 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003496 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003497 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003498 this.setSelectionEnabled(false);
3499 e.preventDefault();
3500 }
3501 }
3502
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003503 if (!reportMouseEvents) {
John Lin2aad22e2018-03-16 13:58:11 +08003504 if (e.type == 'dblclick') {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003505 this.screen_.expandSelection(this.document_.getSelection());
John Lin2aad22e2018-03-16 13:58:11 +08003506 if (this.copyOnSelect)
3507 this.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07003508 }
3509
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003510 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003511 // Debounce this event with the dblclick event. If you try to doubleclick
3512 // a URL to open it, Chrome will fire click then dblclick, but we won't
3513 // have expanded the selection text at the first click event.
3514 clearTimeout(this.timeouts_.openUrl);
3515 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3516 500);
3517 return;
3518 }
3519
Mike Frysinger847577f2017-05-23 23:25:57 -04003520 if (e.type == 'mousedown') {
Mike Frysingercc114512017-09-11 21:39:17 -04003521 if (e.ctrlKey && e.button == 2 /* right button */) {
3522 e.preventDefault();
3523 this.contextMenu.show(e, this);
3524 } else if (e.button == this.mousePasteButton ||
3525 (this.mouseRightClickPaste && e.button == 2 /* right button */)) {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003526 if (!this.paste())
Mike Frysinger05a57f02017-08-27 17:48:55 -04003527 console.warn('Could not paste manually due to web restrictions');
Mike Frysinger847577f2017-05-23 23:25:57 -04003528 }
3529 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003530
Mike Frysinger2edd3612017-05-24 00:54:39 -04003531 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003532 !this.document_.getSelection().isCollapsed) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003533 this.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003534 }
3535
3536 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3537 this.scrollBlockerNode_.engaged) {
3538 // Disengage the scroll-blocker after one of these events.
3539 this.scrollBlockerNode_.engaged = false;
3540 this.scrollBlockerNode_.style.top = '-99px';
3541 }
3542
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003543 // Emulate arrow key presses via scroll wheel events.
3544 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3545 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003546 if (e.type == 'wheel') {
Mike Frysinger321063c2018-08-29 15:33:14 -04003547 const delta = this.scrollPort_.scrollWheelDelta(e);
Mike Frysingerc3030a82017-05-29 14:16:11 -04003548
Mike Frysinger321063c2018-08-29 15:33:14 -04003549 // Helper to turn a wheel event delta into a series of key presses.
3550 const deltaToArrows = (distance, charSize, arrowPos, arrowNeg) => {
3551 if (distance == 0) {
3552 return '';
3553 }
3554
3555 // Convert the scroll distance into a number of rows/cols.
3556 const cells = lib.f.smartFloorDivide(Math.abs(distance), charSize);
3557 const data = '\x1bO' + (distance < 0 ? arrowNeg : arrowPos);
3558 return data.repeat(cells);
3559 };
3560
3561 // The order between up/down and left/right doesn't really matter.
3562 this.io.sendString(
3563 // Up/down arrow keys.
3564 deltaToArrows(delta.y, this.scrollPort_.characterSize.height,
3565 'A', 'B') +
3566 // Left/right arrow keys.
3567 deltaToArrows(delta.x, this.scrollPort_.characterSize.width,
3568 'C', 'D')
3569 );
Mike Frysingerc3030a82017-05-29 14:16:11 -04003570
3571 e.preventDefault();
3572 }
3573 }
Robert Ginda928cf632014-03-05 15:07:41 -08003574 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003575 if (!this.scrollBlockerNode_.engaged) {
3576 if (e.type == 'mousedown') {
3577 // Move the scroll-blocker into place if we want to keep the scrollport
3578 // from scrolling.
3579 this.scrollBlockerNode_.engaged = true;
3580 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3581 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3582 } else if (e.type == 'mousemove') {
3583 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3584 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003585 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003586 e.preventDefault();
3587 }
3588 }
Robert Ginda928cf632014-03-05 15:07:41 -08003589
3590 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003591 }
3592
Robert Ginda928cf632014-03-05 15:07:41 -08003593 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3594 // Restore this on mouseup in case it was temporarily defeated with a
3595 // alt-mousedown. Only do this when the selection is empty so that
3596 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003597 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003598 }
rgindad5613292012-06-19 15:40:37 -07003599};
3600
3601/**
3602 * Clients should override this if they care to know about mouse events.
3603 *
3604 * The event parameter will be a normal DOM mouse click event with additional
3605 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003606 *
3607 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003608 */
3609hterm.Terminal.prototype.onMouse = function(e) { };
3610
3611/**
rginda8e92a692012-05-20 19:37:20 -07003612 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003613 *
3614 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003615 */
Rob Spies06533ba2014-04-24 11:20:37 -07003616hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3617 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003618 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003619
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003620 if (this.reportFocus)
3621 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O');
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003622
Michael Kelly485ecd12014-06-09 11:41:56 -04003623 if (focused === true)
3624 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07003625};
3626
3627/**
rginda8ba33642011-12-14 12:31:31 -08003628 * React when the ScrollPort is scrolled.
3629 */
3630hterm.Terminal.prototype.onScroll_ = function() {
3631 this.scheduleSyncCursorPosition_();
3632};
3633
3634/**
rginda9846e2f2012-01-27 13:53:33 -08003635 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003636 *
3637 * @param {Event} e The DOM paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08003638 */
3639hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaa063b202014-07-21 11:08:25 -07003640 var data = e.text.replace(/\n/mg, '\r');
Connor Hegartyf525ceb2014-09-03 13:36:03 -07003641 data = this.keyboard.encode(data);
Mike Frysingere8c32c82018-03-11 14:57:28 -07003642 if (this.options_.bracketedPaste) {
3643 // We strip out most escape sequences as they can cause issues (like
3644 // inserting an \x1b[201~ midstream). We pass through whitespace
3645 // though: 0x08:\b 0x09:\t 0x0a:\n 0x0d:\r.
3646 // This matches xterm behavior.
3647 const filter = (data) => data.replace(/[\x00-\x07\x0b-\x0c\x0e-\x1f]/g, '');
3648 data = '\x1b[200~' + filter(data) + '\x1b[201~';
3649 }
Robert Gindaa063b202014-07-21 11:08:25 -07003650
3651 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003652};
3653
3654/**
rgindaa09e7332012-08-17 12:49:51 -07003655 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003656 *
3657 * @param {Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07003658 */
3659hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07003660 if (!this.useDefaultWindowCopy) {
3661 e.preventDefault();
3662 setTimeout(this.copySelectionToClipboard.bind(this), 0);
3663 }
rgindaa09e7332012-08-17 12:49:51 -07003664};
3665
3666/**
rginda8ba33642011-12-14 12:31:31 -08003667 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08003668 *
3669 * Note: This function should not directly contain code that alters the internal
3670 * state of the terminal. That kind of code belongs in realizeWidth or
3671 * realizeHeight, so that it can be executed synchronously in the case of a
3672 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08003673 */
3674hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08003675 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07003676 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08003677 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07003678 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08003679
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003680 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08003681 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003682 // gets removed from the document or during the initial load, and we can't
3683 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07003684 // This can also happen if called before the scrollPort calculates the
3685 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08003686 return;
3687 }
3688
rgindaa8ba17d2012-08-15 14:41:10 -07003689 var isNewSize = (columnCount != this.screenSize.width ||
3690 rowCount != this.screenSize.height);
3691
3692 // We do this even if the size didn't change, just to be sure everything is
3693 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04003694 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07003695 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07003696
3697 if (isNewSize)
3698 this.overlaySize();
3699
Robert Gindafb1be6a2013-12-11 11:56:22 -08003700 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07003701 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08003702};
3703
3704/**
3705 * Service the cursor blink timeout.
3706 */
3707hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07003708 if (!this.options_.cursorBlink) {
3709 delete this.timeouts_.cursorBlink;
3710 return;
3711 }
3712
Robert Ginda830583c2013-08-07 13:20:46 -07003713 if (this.cursorNode_.getAttribute('focus') == 'false' ||
3714 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08003715 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07003716 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3717 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08003718 } else {
rginda87b86462011-12-14 13:48:03 -08003719 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07003720 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3721 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08003722 }
3723};
David Reveman8f552492012-03-28 12:18:41 -04003724
3725/**
3726 * Set the scrollbar-visible mode bit.
3727 *
3728 * If scrollbar-visible is on, the vertical scrollbar will be visible.
3729 * Otherwise it will not.
3730 *
3731 * Defaults to on.
3732 *
3733 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
3734 */
3735hterm.Terminal.prototype.setScrollbarVisible = function(state) {
3736 this.scrollPort_.setScrollbarVisible(state);
3737};
Michael Kelly485ecd12014-06-09 11:41:56 -04003738
3739/**
Rob Spies49039e52014-12-17 13:40:04 -08003740 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04003741 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08003742 *
3743 * Defaults to 1.
3744 *
Evan Jones2600d4f2016-12-06 09:29:36 -05003745 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08003746 */
3747hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3748 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3749};
3750
3751/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003752 * Close all web notifications created by terminal bells.
3753 */
3754hterm.Terminal.prototype.closeBellNotifications_ = function() {
3755 this.bellNotificationList_.forEach(function(n) {
3756 n.close();
3757 });
3758 this.bellNotificationList_.length = 0;
3759};
Raymes Khourye5d48982018-08-02 09:08:32 +10003760
3761/**
3762 * Syncs the cursor position when the scrollport gains focus.
3763 */
3764hterm.Terminal.prototype.onScrollportFocus_ = function() {
3765 // If the cursor is offscreen we set selection to the last row on the screen.
3766 const topRowIndex = this.scrollPort_.getTopRowIndex();
3767 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
3768 const selection = this.document_.getSelection();
3769 if (!this.syncCursorPosition_() && selection) {
3770 selection.collapse(this.getRowNode(bottomRowIndex));
3771 }
3772};