blob: 4a711d17470ee1db66b7457023d81f558eae3dc4 [file] [log] [blame]
rginda87b86462011-12-14 13:48:03 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rginda8ba33642011-12-14 12:31:31 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rgindacbbd7482012-06-13 15:06:16 -07005'use strict';
6
Masaya Suzuki273aa982014-05-31 07:25:55 +09007lib.rtdep('lib.colors', 'lib.PreferenceManager', 'lib.resource', 'lib.wc',
Raymes Khoury3e44bc92018-05-17 10:54:23 +10008 'lib.f', 'hterm.AccessibilityReader', 'hterm.Keyboard',
9 'hterm.Options', 'hterm.PreferenceManager', 'hterm.Screen',
10 'hterm.ScrollPort', 'hterm.Size', 'hterm.TextAttributes', 'hterm.VT');
rgindacbbd7482012-06-13 15:06:16 -070011
rginda8ba33642011-12-14 12:31:31 -080012/**
13 * Constructor for the Terminal class.
14 *
15 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
16 * classes to provide the complete terminal functionality.
17 *
18 * There are a number of lower-level Terminal methods that can be called
19 * directly to manipulate the cursor, text, scroll region, and other terminal
20 * attributes. However, the primary method is interpret(), which parses VT
21 * escape sequences and invokes the appropriate Terminal methods.
22 *
23 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
24 *
25 * TODO(rginda): Eventually we're going to need to support characters which are
26 * displayed twice as wide as standard latin characters. This is to support
27 * CJK (and possibly other character sets).
rginda9f5222b2012-03-05 11:53:28 -080028 *
Robert Ginda57f03b42012-09-13 11:02:48 -070029 * @param {string} opt_profileId Optional preference profile name. If not
rginda9f5222b2012-03-05 11:53:28 -080030 * provided, defaults to 'default'.
rginda8ba33642011-12-14 12:31:31 -080031 */
Robert Ginda57f03b42012-09-13 11:02:48 -070032hterm.Terminal = function(opt_profileId) {
33 this.profileId_ = null;
rginda9f5222b2012-03-05 11:53:28 -080034
rginda8ba33642011-12-14 12:31:31 -080035 // Two screen instances.
36 this.primaryScreen_ = new hterm.Screen();
37 this.alternateScreen_ = new hterm.Screen();
38
39 // The "current" screen.
40 this.screen_ = this.primaryScreen_;
41
rginda8ba33642011-12-14 12:31:31 -080042 // The local notion of the screen size. ScreenBuffers also have a size which
43 // indicates their present size. During size changes, the two may disagree.
44 // Also, the inactive screen's size is not altered until it is made the active
45 // screen.
46 this.screenSize = new hterm.Size(0, 0);
47
rginda8ba33642011-12-14 12:31:31 -080048 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080049 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080050 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
51 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080052 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
Raymes Khourye5d48982018-08-02 09:08:32 +100053 this.scrollPort_.subscribe('focus', this.onScrollportFocus_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070054 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080055
rginda87b86462011-12-14 13:48:03 -080056 // The div that contains this terminal.
57 this.div_ = null;
58
rgindac9bc5502012-01-18 11:48:44 -080059 // The document that contains the scrollPort. Defaulted to the global
60 // document here so that the terminal is functional even if it hasn't been
61 // inserted into a document yet, but re-set in decorate().
62 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080063
rginda8ba33642011-12-14 12:31:31 -080064 // The rows that have scrolled off screen and are no longer addressable.
65 this.scrollbackRows_ = [];
66
rgindac9bc5502012-01-18 11:48:44 -080067 // Saved tab stops.
68 this.tabStops_ = [];
69
David Benjamin66e954d2012-05-05 21:08:12 -040070 // Keep track of whether default tab stops have been erased; after a TBC
71 // clears all tab stops, defaults aren't restored on resize until a reset.
72 this.defaultTabStops = true;
73
rginda8ba33642011-12-14 12:31:31 -080074 // The VT's notion of the top and bottom rows. Used during some VT
75 // cursor positioning and scrolling commands.
76 this.vtScrollTop_ = null;
77 this.vtScrollBottom_ = null;
78
79 // The DIV element for the visible cursor.
80 this.cursorNode_ = null;
81
Robert Ginda830583c2013-08-07 13:20:46 -070082 // The current cursor shape of the terminal.
83 this.cursorShape_ = hterm.Terminal.cursorShape.BLOCK;
84
Robert Gindaea2183e2014-07-17 09:51:51 -070085 // Cursor blink on/off cycle in ms, overwritten by prefs once they're loaded.
86 this.cursorBlinkCycle_ = [100, 100];
87
88 // Pre-bound onCursorBlink_ handler, so we don't have to do this for each
89 // cursor on/off servicing.
90 this.myOnCursorBlink_ = this.onCursorBlink_.bind(this);
91
rginda9f5222b2012-03-05 11:53:28 -080092 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -070093 // each output and keystroke. They are initialized by the preference manager.
Robert Ginda8cb7d902013-06-20 14:37:18 -070094 this.backgroundColor_ = null;
95 this.foregroundColor_ = null;
Robert Ginda57f03b42012-09-13 11:02:48 -070096 this.scrollOnOutput_ = null;
97 this.scrollOnKeystroke_ = null;
Mike Frysinger3c9fa072017-07-13 10:21:13 -040098 this.scrollWheelArrowKeys_ = null;
rginda9f5222b2012-03-05 11:53:28 -080099
Robert Ginda6aec7eb2015-06-16 10:31:30 -0700100 // True if we should override mouse event reporting to allow local selection.
101 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -0800102
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400103 // Whether to auto hide the mouse cursor when typing.
104 this.setAutomaticMouseHiding();
105 // Timer to keep mouse visible while it's being used.
106 this.mouseHideDelay_ = null;
107
rgindaf0090c92012-02-10 14:58:52 -0800108 // Terminal bell sound.
109 this.bellAudio_ = this.document_.createElement('audio');
Mike Frysingerd826f1a2017-07-06 16:20:06 -0400110 this.bellAudio_.id = 'hterm:bell-audio';
rgindaf0090c92012-02-10 14:58:52 -0800111 this.bellAudio_.setAttribute('preload', 'auto');
112
Raymes Khoury3e44bc92018-05-17 10:54:23 +1000113 // The AccessibilityReader object for announcing command output.
114 this.accessibilityReader_ = null;
115
Mike Frysingercc114512017-09-11 21:39:17 -0400116 // The context menu object.
117 this.contextMenu = new hterm.ContextMenu();
118
Michael Kelly485ecd12014-06-09 11:41:56 -0400119 // All terminal bell notifications that have been generated (not necessarily
120 // shown).
121 this.bellNotificationList_ = [];
122
123 // Whether we have permission to display notifications.
124 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400125
rginda6d397402012-01-17 10:58:29 -0800126 // Cursor position and attributes saved with DECSC.
127 this.savedOptions_ = {};
128
rginda8ba33642011-12-14 12:31:31 -0800129 // The current mode bits for the terminal.
130 this.options_ = new hterm.Options();
131
132 // Timeouts we might need to clear.
133 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800134
135 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800136 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800137
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800138 this.saveCursorAndState(true);
139
Zhu Qunying30d40712017-03-14 16:27:00 -0700140 // The keyboard handler.
rgindafeaf3142012-01-31 15:14:20 -0800141 this.keyboard = new hterm.Keyboard(this);
142
rginda87b86462011-12-14 13:48:03 -0800143 // General IO interface that can be given to third parties without exposing
144 // the entire terminal object.
145 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800146
rgindad5613292012-06-19 15:40:37 -0700147 // True if mouse-click-drag should scroll the terminal.
148 this.enableMouseDragScroll = true;
149
Robert Ginda57f03b42012-09-13 11:02:48 -0700150 this.copyOnSelect = null;
Mike Frysinger847577f2017-05-23 23:25:57 -0400151 this.mouseRightClickPaste = null;
rginda4bba5e12012-06-20 16:15:30 -0700152 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700153
Zhu Qunying30d40712017-03-14 16:27:00 -0700154 // Whether to use the default window copy behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700155 this.useDefaultWindowCopy = false;
156
157 this.clearSelectionAfterCopy = true;
158
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400159 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800160 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700161
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400162 // Whether we allow images to be shown.
163 this.allowImagesInline = null;
164
Gabriel Holodake8a09be2017-10-10 01:07:11 -0400165 this.reportFocus = false;
166
Robert Ginda57f03b42012-09-13 11:02:48 -0700167 this.setProfile(opt_profileId || 'default',
Evan Jones5f9df812016-12-06 09:38:58 -0500168 function() { this.onTerminalReady(); }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800169};
170
171/**
Robert Ginda830583c2013-08-07 13:20:46 -0700172 * Possible cursor shapes.
173 */
174hterm.Terminal.cursorShape = {
175 BLOCK: 'BLOCK',
176 BEAM: 'BEAM',
177 UNDERLINE: 'UNDERLINE'
178};
179
180/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700181 * Clients should override this to be notified when the terminal is ready
182 * for use.
183 *
184 * The terminal initialization is asynchronous, and shouldn't be used before
185 * this method is called.
186 */
187hterm.Terminal.prototype.onTerminalReady = function() { };
188
189/**
rginda35c456b2012-02-09 17:29:05 -0800190 * Default tab with of 8 to match xterm.
191 */
192hterm.Terminal.prototype.tabWidth = 8;
193
194/**
rginda9f5222b2012-03-05 11:53:28 -0800195 * Select a preference profile.
196 *
197 * This will load the terminal preferences for the given profile name and
198 * associate subsequent preference changes with the new preference profile.
199 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500200 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800201 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700202 * @param {function} opt_callback Optional callback to invoke when the profile
203 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800204 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700205hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
206 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800207
Robert Ginda57f03b42012-09-13 11:02:48 -0700208 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800209
Robert Ginda57f03b42012-09-13 11:02:48 -0700210 if (this.prefs_)
211 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800212
Robert Ginda57f03b42012-09-13 11:02:48 -0700213 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
214 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800215 'alt-gr-mode': function(v) {
216 if (v == null) {
217 if (navigator.language.toLowerCase() == 'en-us') {
218 v = 'none';
219 } else {
220 v = 'right-alt';
221 }
222 } else if (typeof v == 'string') {
223 v = v.toLowerCase();
224 } else {
225 v = 'none';
226 }
227
228 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v))
229 v = 'none';
230
231 terminal.keyboard.altGrMode = v;
232 },
233
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700234 'alt-backspace-is-meta-backspace': function(v) {
235 terminal.keyboard.altBackspaceIsMetaBackspace = v;
236 },
237
Robert Ginda57f03b42012-09-13 11:02:48 -0700238 'alt-is-meta': function(v) {
239 terminal.keyboard.altIsMeta = v;
240 },
241
242 'alt-sends-what': function(v) {
243 if (!/^(escape|8-bit|browser-key)$/.test(v))
244 v = 'escape';
245
246 terminal.keyboard.altSendsWhat = v;
247 },
248
249 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800250 var ary = v.match(/^lib-resource:(\S+)/);
251 if (ary) {
252 terminal.bellAudio_.setAttribute('src',
253 lib.resource.getDataUrl(ary[1]));
254 } else {
255 terminal.bellAudio_.setAttribute('src', v);
256 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700257 },
258
Michael Kelly485ecd12014-06-09 11:41:56 -0400259 'desktop-notification-bell': function(v) {
260 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700261 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400262 Notification.permission === 'granted';
263 if (!terminal.desktopNotificationBell_) {
264 // Note: We don't call Notification.requestPermission here because
265 // Chrome requires the call be the result of a user action (such as an
266 // onclick handler), and pref listeners are run asynchronously.
267 //
268 // A way of working around this would be to display a dialog in the
269 // terminal with a "click-to-request-permission" button.
270 console.warn('desktop-notification-bell is true but we do not have ' +
271 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400272 }
273 } else {
274 terminal.desktopNotificationBell_ = false;
275 }
276 },
277
Robert Ginda57f03b42012-09-13 11:02:48 -0700278 'background-color': function(v) {
279 terminal.setBackgroundColor(v);
280 },
281
282 'background-image': function(v) {
283 terminal.scrollPort_.setBackgroundImage(v);
284 },
285
286 'background-size': function(v) {
287 terminal.scrollPort_.setBackgroundSize(v);
288 },
289
290 'background-position': function(v) {
291 terminal.scrollPort_.setBackgroundPosition(v);
292 },
293
294 'backspace-sends-backspace': function(v) {
295 terminal.keyboard.backspaceSendsBackspace = v;
296 },
297
Brad Town18654b62015-03-12 00:27:45 -0700298 'character-map-overrides': function(v) {
299 if (!(v == null || v instanceof Object)) {
300 console.warn('Preference character-map-modifications is not an ' +
301 'object: ' + v);
302 return;
303 }
304
Mike Frysinger095d4062017-06-14 00:29:48 -0700305 terminal.vt.characterMaps.reset();
306 terminal.vt.characterMaps.setOverrides(v);
Brad Town18654b62015-03-12 00:27:45 -0700307 },
308
Robert Ginda57f03b42012-09-13 11:02:48 -0700309 'cursor-blink': function(v) {
310 terminal.setCursorBlink(!!v);
311 },
312
Robert Gindaea2183e2014-07-17 09:51:51 -0700313 'cursor-blink-cycle': function(v) {
314 if (v instanceof Array &&
315 typeof v[0] == 'number' &&
316 typeof v[1] == 'number') {
317 terminal.cursorBlinkCycle_ = v;
318 } else if (typeof v == 'number') {
319 terminal.cursorBlinkCycle_ = [v, v];
320 } else {
321 // Fast blink indicates an error.
322 terminal.cursorBlinkCycle_ = [100, 100];
323 }
324 },
325
Robert Ginda57f03b42012-09-13 11:02:48 -0700326 'cursor-color': function(v) {
327 terminal.setCursorColor(v);
328 },
329
330 'color-palette-overrides': function(v) {
331 if (!(v == null || v instanceof Object || v instanceof Array)) {
332 console.warn('Preference color-palette-overrides is not an array or ' +
333 'object: ' + v);
334 return;
rginda9f5222b2012-03-05 11:53:28 -0800335 }
rginda9f5222b2012-03-05 11:53:28 -0800336
Robert Ginda57f03b42012-09-13 11:02:48 -0700337 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700338
Robert Ginda57f03b42012-09-13 11:02:48 -0700339 if (v) {
340 for (var key in v) {
341 var i = parseInt(key);
342 if (isNaN(i) || i < 0 || i > 255) {
343 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
344 continue;
345 }
346
347 if (v[i]) {
348 var rgb = lib.colors.normalizeCSS(v[i]);
349 if (rgb)
350 lib.colors.colorPalette[i] = rgb;
351 }
352 }
rginda30f20f62012-04-05 16:36:19 -0700353 }
rginda30f20f62012-04-05 16:36:19 -0700354
Evan Jones5f9df812016-12-06 09:38:58 -0500355 terminal.primaryScreen_.textAttributes.resetColorPalette();
Robert Ginda57f03b42012-09-13 11:02:48 -0700356 terminal.alternateScreen_.textAttributes.resetColorPalette();
357 },
rginda30f20f62012-04-05 16:36:19 -0700358
Robert Ginda57f03b42012-09-13 11:02:48 -0700359 'copy-on-select': function(v) {
360 terminal.copyOnSelect = !!v;
361 },
rginda9f5222b2012-03-05 11:53:28 -0800362
Rob Spies0bec09b2014-06-06 15:58:09 -0700363 'use-default-window-copy': function(v) {
364 terminal.useDefaultWindowCopy = !!v;
365 },
366
367 'clear-selection-after-copy': function(v) {
368 terminal.clearSelectionAfterCopy = !!v;
369 },
370
Robert Ginda7e5e9522014-03-14 12:23:58 -0700371 'ctrl-plus-minus-zero-zoom': function(v) {
372 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
373 },
374
Robert Gindafb5a3f92014-05-13 14:12:00 -0700375 'ctrl-c-copy': function(v) {
376 terminal.keyboard.ctrlCCopy = v;
377 },
378
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100379 'ctrl-v-paste': function(v) {
380 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700381 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100382 },
383
Cody Coljee-Gray7c6a0392018-10-25 13:18:28 -0700384 'paste-on-drop': function(v) {
385 terminal.scrollPort_.setPasteOnDrop(v);
386 },
387
Masaya Suzuki273aa982014-05-31 07:25:55 +0900388 'east-asian-ambiguous-as-two-column': function(v) {
389 lib.wc.regardCjkAmbiguous = v;
390 },
391
Robert Ginda57f03b42012-09-13 11:02:48 -0700392 'enable-8-bit-control': function(v) {
393 terminal.vt.enable8BitControl = !!v;
394 },
rginda30f20f62012-04-05 16:36:19 -0700395
Robert Ginda57f03b42012-09-13 11:02:48 -0700396 'enable-bold': function(v) {
397 terminal.syncBoldSafeState();
398 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400399
Robert Ginda3e278d72014-03-25 13:18:51 -0700400 'enable-bold-as-bright': function(v) {
401 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
402 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
403 },
404
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400405 'enable-blink': function(v) {
Mike Frysinger261597c2017-12-28 01:14:21 -0500406 terminal.setTextBlink(!!v);
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400407 },
408
Robert Ginda57f03b42012-09-13 11:02:48 -0700409 'enable-clipboard-write': function(v) {
410 terminal.vt.enableClipboardWrite = !!v;
411 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400412
Robert Ginda3755e752013-05-31 13:34:09 -0700413 'enable-dec12': function(v) {
414 terminal.vt.enableDec12 = !!v;
415 },
416
Mike Frysinger38f267d2018-09-07 02:50:59 -0400417 'enable-csi-j-3': function(v) {
418 terminal.vt.enableCsiJ3 = !!v;
419 },
420
Robert Ginda57f03b42012-09-13 11:02:48 -0700421 'font-family': function(v) {
422 terminal.syncFontFamily();
423 },
rginda30f20f62012-04-05 16:36:19 -0700424
Robert Ginda57f03b42012-09-13 11:02:48 -0700425 'font-size': function(v) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500426 v = parseInt(v);
427 if (v <= 0) {
428 console.error(`Invalid font size: ${v}`);
429 return;
430 }
431
Robert Ginda57f03b42012-09-13 11:02:48 -0700432 terminal.setFontSize(v);
433 },
rginda9875d902012-08-20 16:21:57 -0700434
Robert Ginda57f03b42012-09-13 11:02:48 -0700435 'font-smoothing': function(v) {
436 terminal.syncFontFamily();
437 },
rgindade84e382012-04-20 15:39:31 -0700438
Robert Ginda57f03b42012-09-13 11:02:48 -0700439 'foreground-color': function(v) {
440 terminal.setForegroundColor(v);
441 },
rginda30f20f62012-04-05 16:36:19 -0700442
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400443 'hide-mouse-while-typing': function(v) {
444 terminal.setAutomaticMouseHiding(v);
445 },
446
Robert Ginda57f03b42012-09-13 11:02:48 -0700447 'home-keys-scroll': function(v) {
448 terminal.keyboard.homeKeysScroll = v;
449 },
rginda4bba5e12012-06-20 16:15:30 -0700450
Robert Gindaa8165692015-06-15 14:46:31 -0700451 'keybindings': function(v) {
452 terminal.keyboard.bindings.clear();
453
454 if (!v)
455 return;
456
457 if (!(v instanceof Object)) {
458 console.error('Error in keybindings preference: Expected object');
459 return;
460 }
461
462 try {
463 terminal.keyboard.bindings.addBindings(v);
464 } catch (ex) {
465 console.error('Error in keybindings preference: ' + ex);
466 }
467 },
468
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700469 'media-keys-are-fkeys': function(v) {
470 terminal.keyboard.mediaKeysAreFKeys = v;
471 },
472
Robert Ginda57f03b42012-09-13 11:02:48 -0700473 'meta-sends-escape': function(v) {
474 terminal.keyboard.metaSendsEscape = v;
475 },
rginda30f20f62012-04-05 16:36:19 -0700476
Mike Frysinger847577f2017-05-23 23:25:57 -0400477 'mouse-right-click-paste': function(v) {
478 terminal.mouseRightClickPaste = v;
479 },
480
Robert Ginda57f03b42012-09-13 11:02:48 -0700481 'mouse-paste-button': function(v) {
482 terminal.syncMousePasteButton();
483 },
rgindaa8ba17d2012-08-15 14:41:10 -0700484
Robert Gindae76aa9f2014-03-14 12:29:12 -0700485 'page-keys-scroll': function(v) {
486 terminal.keyboard.pageKeysScroll = v;
487 },
488
Robert Ginda40932892012-12-10 17:26:40 -0800489 'pass-alt-number': function(v) {
490 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800491 // Let Alt-1..9 pass to the browser (to control tab switching) on
492 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500493 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800494 }
495
496 terminal.passAltNumber = v;
497 },
498
499 'pass-ctrl-number': function(v) {
500 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800501 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
502 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500503 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800504 }
505
506 terminal.passCtrlNumber = v;
507 },
508
509 'pass-meta-number': function(v) {
510 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800511 // Let Meta-1..9 pass to the browser (to control tab switching) on
512 // OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500513 v = (hterm.os == 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800514 }
515
516 terminal.passMetaNumber = v;
517 },
518
Marius Schilder77857b32014-05-14 16:21:26 -0700519 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700520 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700521 },
522
Robert Ginda8cb7d902013-06-20 14:37:18 -0700523 'receive-encoding': function(v) {
524 if (!(/^(utf-8|raw)$/).test(v)) {
525 console.warn('Invalid value for "receive-encoding": ' + v);
526 v = 'utf-8';
527 }
528
529 terminal.vt.characterEncoding = v;
530 },
531
Robert Ginda57f03b42012-09-13 11:02:48 -0700532 'scroll-on-keystroke': function(v) {
533 terminal.scrollOnKeystroke_ = v;
534 },
rginda9f5222b2012-03-05 11:53:28 -0800535
Robert Ginda57f03b42012-09-13 11:02:48 -0700536 'scroll-on-output': function(v) {
537 terminal.scrollOnOutput_ = v;
538 },
rginda30f20f62012-04-05 16:36:19 -0700539
Robert Ginda57f03b42012-09-13 11:02:48 -0700540 'scrollbar-visible': function(v) {
541 terminal.setScrollbarVisible(v);
542 },
rginda9f5222b2012-03-05 11:53:28 -0800543
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400544 'scroll-wheel-may-send-arrow-keys': function(v) {
545 terminal.scrollWheelArrowKeys_ = v;
546 },
547
Rob Spies49039e52014-12-17 13:40:04 -0800548 'scroll-wheel-move-multiplier': function(v) {
549 terminal.setScrollWheelMoveMultipler(v);
550 },
551
Robert Ginda8cb7d902013-06-20 14:37:18 -0700552 'send-encoding': function(v) {
553 if (!(/^(utf-8|raw)$/).test(v)) {
554 console.warn('Invalid value for "send-encoding": ' + v);
555 v = 'utf-8';
556 }
557
558 terminal.keyboard.characterEncoding = v;
559 },
560
Robert Ginda57f03b42012-09-13 11:02:48 -0700561 'shift-insert-paste': function(v) {
562 terminal.keyboard.shiftInsertPaste = v;
563 },
rginda9f5222b2012-03-05 11:53:28 -0800564
Mike Frysingera7768922017-07-28 15:00:12 -0400565 'terminal-encoding': function(v) {
Mike Frysingera1371e12017-08-17 01:37:17 -0400566 terminal.vt.setEncoding(v);
Mike Frysingera7768922017-07-28 15:00:12 -0400567 },
568
Robert Gindae76aa9f2014-03-14 12:29:12 -0700569 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400570 terminal.scrollPort_.setUserCssUrl(v);
571 },
572
573 'user-css-text': function(v) {
574 terminal.scrollPort_.setUserCssText(v);
575 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400576
577 'word-break-match-left': function(v) {
578 terminal.primaryScreen_.wordBreakMatchLeft = v;
579 terminal.alternateScreen_.wordBreakMatchLeft = v;
580 },
581
582 'word-break-match-right': function(v) {
583 terminal.primaryScreen_.wordBreakMatchRight = v;
584 terminal.alternateScreen_.wordBreakMatchRight = v;
585 },
586
587 'word-break-match-middle': function(v) {
588 terminal.primaryScreen_.wordBreakMatchMiddle = v;
589 terminal.alternateScreen_.wordBreakMatchMiddle = v;
590 },
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400591
592 'allow-images-inline': function(v) {
593 terminal.allowImagesInline = v;
594 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700595 });
rginda30f20f62012-04-05 16:36:19 -0700596
Robert Ginda57f03b42012-09-13 11:02:48 -0700597 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800598 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700599
600 if (opt_callback)
601 opt_callback();
602 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800603};
604
Rob Spies56953412014-04-28 14:09:47 -0700605
606/**
607 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500608 *
609 * @return {hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700610 */
611hterm.Terminal.prototype.getPrefs = function() {
612 return this.prefs_;
613};
614
Robert Gindaa063b202014-07-21 11:08:25 -0700615/**
616 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500617 *
618 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700619 */
620hterm.Terminal.prototype.setBracketedPaste = function(state) {
621 this.options_.bracketedPaste = state;
622};
Rob Spies56953412014-04-28 14:09:47 -0700623
rginda8e92a692012-05-20 19:37:20 -0700624/**
625 * Set the color for the cursor.
626 *
627 * If you want this setting to persist, set it through prefs_, rather than
628 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500629 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500630 * @param {string=} color The color to set. If not defined, we reset to the
631 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700632 */
633hterm.Terminal.prototype.setCursorColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500634 if (color === undefined)
635 color = this.prefs_.get('cursor-color');
636
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400637 this.setCssVar('cursor-color', color);
rginda8e92a692012-05-20 19:37:20 -0700638};
639
640/**
641 * Return the current cursor color as a string.
Evan Jones2600d4f2016-12-06 09:29:36 -0500642 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700643 */
644hterm.Terminal.prototype.getCursorColor = function() {
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400645 return this.getCssVar('cursor-color');
rginda8e92a692012-05-20 19:37:20 -0700646};
647
648/**
rgindad5613292012-06-19 15:40:37 -0700649 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500650 *
651 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700652 */
653hterm.Terminal.prototype.setSelectionEnabled = function(state) {
654 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700655};
656
657/**
rginda8e92a692012-05-20 19:37:20 -0700658 * Set the background color.
659 *
660 * If you want this setting to persist, set it through prefs_, rather than
661 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500662 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500663 * @param {string=} color The color to set. If not defined, we reset to the
664 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700665 */
666hterm.Terminal.prototype.setBackgroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500667 if (color === undefined)
668 color = this.prefs_.get('background-color');
669
rgindacbbd7482012-06-13 15:06:16 -0700670 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700671 this.primaryScreen_.textAttributes.setDefaults(
672 this.foregroundColor_, this.backgroundColor_);
673 this.alternateScreen_.textAttributes.setDefaults(
674 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700675 this.scrollPort_.setBackgroundColor(color);
676};
677
rginda9f5222b2012-03-05 11:53:28 -0800678/**
679 * Return the current terminal background color.
680 *
681 * Intended for use by other classes, so we don't have to expose the entire
682 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500683 *
684 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800685 */
686hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700687 return this.backgroundColor_;
688};
689
690/**
691 * Set the foreground color.
692 *
693 * If you want this setting to persist, set it through prefs_, rather than
694 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500695 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500696 * @param {string=} color The color to set. If not defined, we reset to the
697 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700698 */
699hterm.Terminal.prototype.setForegroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500700 if (color === undefined)
701 color = this.prefs_.get('foreground-color');
702
rgindacbbd7482012-06-13 15:06:16 -0700703 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700704 this.primaryScreen_.textAttributes.setDefaults(
705 this.foregroundColor_, this.backgroundColor_);
706 this.alternateScreen_.textAttributes.setDefaults(
707 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700708 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800709};
710
711/**
712 * Return the current terminal foreground color.
713 *
714 * Intended for use by other classes, so we don't have to expose the entire
715 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500716 *
717 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800718 */
719hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700720 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800721};
722
723/**
rginda87b86462011-12-14 13:48:03 -0800724 * Create a new instance of a terminal command and run it with a given
725 * argument string.
726 *
727 * @param {function} commandClass The constructor for a terminal command.
728 * @param {string} argString The argument string to pass to the command.
729 */
730hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700731 var environment = this.prefs_.get('environment');
732 if (typeof environment != 'object' || environment == null)
733 environment = {};
734
rginda87b86462011-12-14 13:48:03 -0800735 var self = this;
736 this.command = new commandClass(
737 { argString: argString || '',
738 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700739 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800740 onExit: function(code) {
741 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800742 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700743 if (self.prefs_.get('close-on-exit'))
744 window.close();
rginda87b86462011-12-14 13:48:03 -0800745 }
746 });
747
rgindafeaf3142012-01-31 15:14:20 -0800748 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800749 this.command.run();
750};
751
752/**
rgindafeaf3142012-01-31 15:14:20 -0800753 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500754 *
755 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800756 */
757hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700758 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800759};
760
761/**
762 * Install the keyboard handler for this terminal.
763 *
764 * This will prevent the browser from seeing any keystrokes sent to the
765 * terminal.
766 */
767hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700768 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400769};
rgindafeaf3142012-01-31 15:14:20 -0800770
771/**
772 * Uninstall the keyboard handler for this terminal.
773 */
774hterm.Terminal.prototype.uninstallKeyboard = function() {
775 this.keyboard.installKeyboard(null);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400776};
rgindafeaf3142012-01-31 15:14:20 -0800777
778/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400779 * Set a CSS variable.
780 *
781 * Normally this is used to set variables in the hterm namespace.
782 *
783 * @param {string} name The variable to set.
784 * @param {string} value The value to assign to the variable.
785 * @param {string?} opt_prefix The variable namespace/prefix to use.
786 */
787hterm.Terminal.prototype.setCssVar = function(name, value,
788 opt_prefix='--hterm-') {
789 this.document_.documentElement.style.setProperty(
790 `${opt_prefix}${name}`, value);
791};
792
793/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500794 * Get a CSS variable.
795 *
796 * Normally this is used to get variables in the hterm namespace.
797 *
798 * @param {string} name The variable to read.
799 * @param {string?} opt_prefix The variable namespace/prefix to use.
800 * @return {string} The current setting for this variable.
801 */
802hterm.Terminal.prototype.getCssVar = function(name, opt_prefix='--hterm-') {
803 return this.document_.documentElement.style.getPropertyValue(
804 `${opt_prefix}${name}`);
805};
806
807/**
rginda35c456b2012-02-09 17:29:05 -0800808 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800809 *
810 * Call setFontSize(0) to reset to the default font size.
811 *
812 * This function does not modify the font-size preference.
813 *
814 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800815 */
816hterm.Terminal.prototype.setFontSize = function(px) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500817 if (px <= 0)
rginda9f5222b2012-03-05 11:53:28 -0800818 px = this.prefs_.get('font-size');
819
rginda35c456b2012-02-09 17:29:05 -0800820 this.scrollPort_.setFontSize(px);
Mike Frysingercce97c42017-08-05 01:11:22 -0400821 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
822 this.setCssVar('charsize-height',
823 this.scrollPort_.characterSize.height + 'px');
rginda35c456b2012-02-09 17:29:05 -0800824};
825
826/**
827 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500828 *
829 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800830 */
831hterm.Terminal.prototype.getFontSize = function() {
832 return this.scrollPort_.getFontSize();
833};
834
835/**
rginda8e92a692012-05-20 19:37:20 -0700836 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500837 *
838 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700839 */
840hterm.Terminal.prototype.getFontFamily = function() {
841 return this.scrollPort_.getFontFamily();
842};
843
844/**
rginda35c456b2012-02-09 17:29:05 -0800845 * Set the CSS "font-family" for this terminal.
846 */
rginda9f5222b2012-03-05 11:53:28 -0800847hterm.Terminal.prototype.syncFontFamily = function() {
848 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
849 this.prefs_.get('font-smoothing'));
850 this.syncBoldSafeState();
851};
852
rginda4bba5e12012-06-20 16:15:30 -0700853/**
854 * Set this.mousePasteButton based on the mouse-paste-button pref,
855 * autodetecting if necessary.
856 */
857hterm.Terminal.prototype.syncMousePasteButton = function() {
858 var button = this.prefs_.get('mouse-paste-button');
859 if (typeof button == 'number') {
860 this.mousePasteButton = button;
861 return;
862 }
863
Mike Frysingeree81a002017-12-12 16:14:53 -0500864 if (hterm.os != 'linux') {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400865 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700866 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400867 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700868 }
869};
870
871/**
872 * Enable or disable bold based on the enable-bold pref, autodetecting if
873 * necessary.
874 */
rginda9f5222b2012-03-05 11:53:28 -0800875hterm.Terminal.prototype.syncBoldSafeState = function() {
876 var enableBold = this.prefs_.get('enable-bold');
877 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700878 this.primaryScreen_.textAttributes.enableBold = enableBold;
879 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800880 return;
881 }
882
rgindaf7521392012-02-28 17:20:34 -0800883 var normalSize = this.scrollPort_.measureCharacterSize();
884 var boldSize = this.scrollPort_.measureCharacterSize('bold');
885
886 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800887 if (!isBoldSafe) {
888 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700889 'from normal. Font family is: ' +
890 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800891 }
rginda9f5222b2012-03-05 11:53:28 -0800892
Robert Gindaed016262012-10-26 16:27:09 -0700893 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
894 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800895};
896
897/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500898 * Control text blinking behavior.
899 *
900 * @param {boolean=} state Whether to enable support for blinking text.
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400901 */
Mike Frysinger261597c2017-12-28 01:14:21 -0500902hterm.Terminal.prototype.setTextBlink = function(state) {
903 if (state === undefined)
904 state = this.prefs_.get('enable-blink');
905 this.setCssVar('blink-node-duration', state ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400906};
907
908/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400909 * Set the mouse cursor style based on the current terminal mode.
910 */
911hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -0400912 this.setCssVar('mouse-cursor-style',
913 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
914 'var(--hterm-mouse-cursor-text)' :
Mike Frysinger67f58f82018-11-22 13:38:22 -0500915 'var(--hterm-mouse-cursor-default)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400916};
917
918/**
rginda87b86462011-12-14 13:48:03 -0800919 * Return a copy of the current cursor position.
920 *
921 * @return {hterm.RowCol} The RowCol object representing the current position.
922 */
923hterm.Terminal.prototype.saveCursor = function() {
924 return this.screen_.cursorPosition.clone();
925};
926
Evan Jones2600d4f2016-12-06 09:29:36 -0500927/**
928 * Return the current text attributes.
929 *
930 * @return {string}
931 */
rgindaa19afe22012-01-25 15:40:22 -0800932hterm.Terminal.prototype.getTextAttributes = function() {
933 return this.screen_.textAttributes;
934};
935
Evan Jones2600d4f2016-12-06 09:29:36 -0500936/**
937 * Set the text attributes.
938 *
939 * @param {string} textAttributes The attributes to set.
940 */
rginda1a09aa02012-06-18 21:11:25 -0700941hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
942 this.screen_.textAttributes = textAttributes;
943};
944
rginda87b86462011-12-14 13:48:03 -0800945/**
rgindaf522ce02012-04-17 17:49:17 -0700946 * Return the current browser zoom factor applied to the terminal.
947 *
948 * @return {number} The current browser zoom factor.
949 */
950hterm.Terminal.prototype.getZoomFactor = function() {
951 return this.scrollPort_.characterSize.zoomFactor;
952};
953
954/**
rginda9846e2f2012-01-27 13:53:33 -0800955 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -0500956 *
957 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -0800958 */
959hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800960 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800961};
962
963/**
rginda87b86462011-12-14 13:48:03 -0800964 * Restore a previously saved cursor position.
965 *
966 * @param {hterm.RowCol} cursor The position to restore.
967 */
968hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700969 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
970 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800971 this.screen_.setCursorPosition(row, column);
972 if (cursor.column > column ||
973 cursor.column == column && cursor.overflow) {
974 this.screen_.cursorPosition.overflow = true;
975 }
rginda87b86462011-12-14 13:48:03 -0800976};
977
978/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400979 * Clear the cursor's overflow flag.
980 */
981hterm.Terminal.prototype.clearCursorOverflow = function() {
982 this.screen_.cursorPosition.overflow = false;
983};
984
985/**
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800986 * Save the current cursor state to the corresponding screens.
987 *
988 * See the hterm.Screen.CursorState class for more details.
989 *
990 * @param {boolean=} both If true, update both screens, else only update the
991 * current screen.
992 */
993hterm.Terminal.prototype.saveCursorAndState = function(both) {
994 if (both) {
995 this.primaryScreen_.saveCursorAndState(this.vt);
996 this.alternateScreen_.saveCursorAndState(this.vt);
997 } else
998 this.screen_.saveCursorAndState(this.vt);
999};
1000
1001/**
1002 * Restore the saved cursor state in the corresponding screens.
1003 *
1004 * See the hterm.Screen.CursorState class for more details.
1005 *
1006 * @param {boolean=} both If true, update both screens, else only update the
1007 * current screen.
1008 */
1009hterm.Terminal.prototype.restoreCursorAndState = function(both) {
1010 if (both) {
1011 this.primaryScreen_.restoreCursorAndState(this.vt);
1012 this.alternateScreen_.restoreCursorAndState(this.vt);
1013 } else
1014 this.screen_.restoreCursorAndState(this.vt);
1015};
1016
1017/**
Robert Ginda830583c2013-08-07 13:20:46 -07001018 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001019 *
1020 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -07001021 */
1022hterm.Terminal.prototype.setCursorShape = function(shape) {
1023 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001024 this.restyleCursor_();
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001025};
Robert Ginda830583c2013-08-07 13:20:46 -07001026
1027/**
1028 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001029 *
1030 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -07001031 */
1032hterm.Terminal.prototype.getCursorShape = function() {
1033 return this.cursorShape_;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001034};
Robert Ginda830583c2013-08-07 13:20:46 -07001035
1036/**
rginda87b86462011-12-14 13:48:03 -08001037 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001038 *
1039 * @param {number} columnCount
rginda87b86462011-12-14 13:48:03 -08001040 */
1041hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -08001042 if (columnCount == null) {
1043 this.div_.style.width = '100%';
1044 return;
1045 }
1046
Robert Ginda26806d12014-07-24 13:44:07 -07001047 this.div_.style.width = Math.ceil(
1048 this.scrollPort_.characterSize.width *
1049 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001050 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -08001051 this.scheduleSyncCursorPosition_();
1052};
rginda87b86462011-12-14 13:48:03 -08001053
rgindac9bc5502012-01-18 11:48:44 -08001054/**
rginda35c456b2012-02-09 17:29:05 -08001055 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001056 *
1057 * @param {number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -08001058 */
1059hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -08001060 if (rowCount == null) {
1061 this.div_.style.height = '100%';
1062 return;
1063 }
1064
rginda35c456b2012-02-09 17:29:05 -08001065 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -07001066 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -08001067 this.realizeSize_(this.screenSize.width, rowCount);
1068 this.scheduleSyncCursorPosition_();
1069};
1070
1071/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001072 * Deal with terminal size changes.
1073 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001074 * @param {number} columnCount The number of columns.
1075 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001076 */
1077hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
1078 if (columnCount != this.screenSize.width)
1079 this.realizeWidth_(columnCount);
1080
1081 if (rowCount != this.screenSize.height)
1082 this.realizeHeight_(rowCount);
1083
1084 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -07001085 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001086};
1087
1088/**
rgindac9bc5502012-01-18 11:48:44 -08001089 * Deal with terminal width changes.
1090 *
1091 * This function does what needs to be done when the terminal width changes
1092 * out from under us. It happens here rather than in onResize_() because this
1093 * code may need to run synchronously to handle programmatic changes of
1094 * terminal width.
1095 *
1096 * Relying on the browser to send us an async resize event means we may not be
1097 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001098 *
1099 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001100 */
1101hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001102 if (columnCount <= 0)
1103 throw new Error('Attempt to realize bad width: ' + columnCount);
1104
rgindac9bc5502012-01-18 11:48:44 -08001105 var deltaColumns = columnCount - this.screen_.getWidth();
1106
rginda87b86462011-12-14 13:48:03 -08001107 this.screenSize.width = columnCount;
1108 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001109
1110 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -04001111 if (this.defaultTabStops)
1112 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -08001113 } else {
1114 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -04001115 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -08001116 break;
1117
1118 this.tabStops_.pop();
1119 }
1120 }
1121
1122 this.screen_.setColumnCount(this.screenSize.width);
1123};
1124
1125/**
1126 * Deal with terminal height changes.
1127 *
1128 * This function does what needs to be done when the terminal height changes
1129 * out from under us. It happens here rather than in onResize_() because this
1130 * code may need to run synchronously to handle programmatic changes of
1131 * terminal height.
1132 *
1133 * Relying on the browser to send us an async resize event means we may not be
1134 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001135 *
1136 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001137 */
1138hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001139 if (rowCount <= 0)
1140 throw new Error('Attempt to realize bad height: ' + rowCount);
1141
rgindac9bc5502012-01-18 11:48:44 -08001142 var deltaRows = rowCount - this.screen_.getHeight();
1143
1144 this.screenSize.height = rowCount;
1145
1146 var cursor = this.saveCursor();
1147
1148 if (deltaRows < 0) {
1149 // Screen got smaller.
1150 deltaRows *= -1;
1151 while (deltaRows) {
1152 var lastRow = this.getRowCount() - 1;
1153 if (lastRow - this.scrollbackRows_.length == cursor.row)
1154 break;
1155
1156 if (this.getRowText(lastRow))
1157 break;
1158
1159 this.screen_.popRow();
1160 deltaRows--;
1161 }
1162
1163 var ary = this.screen_.shiftRows(deltaRows);
1164 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1165
1166 // We just removed rows from the top of the screen, we need to update
1167 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001168 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001169 } else if (deltaRows > 0) {
1170 // Screen got larger.
1171
1172 if (deltaRows <= this.scrollbackRows_.length) {
1173 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1174 var rows = this.scrollbackRows_.splice(
1175 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1176 this.screen_.unshiftRows(rows);
1177 deltaRows -= scrollbackCount;
1178 cursor.row += scrollbackCount;
1179 }
1180
1181 if (deltaRows)
1182 this.appendRows_(deltaRows);
1183 }
1184
rginda35c456b2012-02-09 17:29:05 -08001185 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001186 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001187};
1188
1189/**
1190 * Scroll the terminal to the top of the scrollback buffer.
1191 */
1192hterm.Terminal.prototype.scrollHome = function() {
1193 this.scrollPort_.scrollRowToTop(0);
1194};
1195
1196/**
1197 * Scroll the terminal to the end.
1198 */
1199hterm.Terminal.prototype.scrollEnd = function() {
1200 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1201};
1202
1203/**
1204 * Scroll the terminal one page up (minus one line) relative to the current
1205 * position.
1206 */
1207hterm.Terminal.prototype.scrollPageUp = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001208 this.scrollPort_.scrollPageUp();
rginda87b86462011-12-14 13:48:03 -08001209};
1210
1211/**
1212 * Scroll the terminal one page down (minus one line) relative to the current
1213 * position.
1214 */
1215hterm.Terminal.prototype.scrollPageDown = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001216 this.scrollPort_.scrollPageDown();
rginda8ba33642011-12-14 12:31:31 -08001217};
1218
rgindac9bc5502012-01-18 11:48:44 -08001219/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001220 * Scroll the terminal one line up relative to the current position.
1221 */
1222hterm.Terminal.prototype.scrollLineUp = function() {
1223 var i = this.scrollPort_.getTopRowIndex();
1224 this.scrollPort_.scrollRowToTop(i - 1);
1225};
1226
1227/**
1228 * Scroll the terminal one line down relative to the current position.
1229 */
1230hterm.Terminal.prototype.scrollLineDown = function() {
1231 var i = this.scrollPort_.getTopRowIndex();
1232 this.scrollPort_.scrollRowToTop(i + 1);
1233};
1234
1235/**
Robert Ginda40932892012-12-10 17:26:40 -08001236 * Clear primary screen, secondary screen, and the scrollback buffer.
1237 */
1238hterm.Terminal.prototype.wipeContents = function() {
Mike Frysinger9c482b82018-09-07 02:49:36 -04001239 this.clearHome(this.primaryScreen_);
1240 this.clearHome(this.alternateScreen_);
1241
1242 this.clearScrollback();
1243};
1244
1245/**
1246 * Clear scrollback buffer.
1247 */
1248hterm.Terminal.prototype.clearScrollback = function() {
1249 // Move to the end of the buffer in case the screen was scrolled back.
1250 // We're going to throw it away which would leave the display invalid.
1251 this.scrollEnd();
1252
Robert Ginda40932892012-12-10 17:26:40 -08001253 this.scrollbackRows_.length = 0;
1254 this.scrollPort_.resetCache();
1255
Mike Frysinger9c482b82018-09-07 02:49:36 -04001256 [this.primaryScreen_, this.alternateScreen_].forEach((screen) => {
1257 const bottom = screen.getHeight();
1258 this.renumberRows_(0, bottom, screen);
1259 });
Robert Ginda40932892012-12-10 17:26:40 -08001260
1261 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001262 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001263};
1264
1265/**
rgindac9bc5502012-01-18 11:48:44 -08001266 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001267 *
1268 * Perform a full reset to the default values listed in
1269 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001270 */
rginda87b86462011-12-14 13:48:03 -08001271hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001272 this.vt.reset();
1273
rgindac9bc5502012-01-18 11:48:44 -08001274 this.clearAllTabStops();
1275 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001276
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001277 const resetScreen = (screen) => {
1278 // We want to make sure to reset the attributes before we clear the screen.
1279 // The attributes might be used to initialize default/empty rows.
1280 screen.textAttributes.reset();
1281 screen.textAttributes.resetColorPalette();
1282 this.clearHome(screen);
1283 screen.saveCursorAndState(this.vt);
1284 };
1285 resetScreen(this.primaryScreen_);
1286 resetScreen(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001287
Mike Frysinger84301d02017-11-29 13:28:46 -08001288 // Reset terminal options to their default values.
1289 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001290 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1291
Mike Frysinger84301d02017-11-29 13:28:46 -08001292 this.setVTScrollRegion(null, null);
1293
1294 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001295};
1296
rgindac9bc5502012-01-18 11:48:44 -08001297/**
1298 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001299 *
1300 * Perform a soft reset to the default values listed in
1301 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001302 */
rginda0f5c0292012-01-13 11:00:13 -08001303hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001304 this.vt.reset();
1305
rgindab8bc8932012-04-27 12:45:03 -07001306 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001307 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001308
Brad Townb62dfdc2015-03-16 19:07:15 -07001309 // We show the cursor on soft reset but do not alter the blink state.
1310 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1311
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001312 const resetScreen = (screen) => {
1313 // Xterm also resets the color palette on soft reset, even though it doesn't
1314 // seem to be documented anywhere.
1315 screen.textAttributes.reset();
1316 screen.textAttributes.resetColorPalette();
1317 screen.saveCursorAndState(this.vt);
1318 };
1319 resetScreen(this.primaryScreen_);
1320 resetScreen(this.alternateScreen_);
rgindaf522ce02012-04-17 17:49:17 -07001321
rgindab8bc8932012-04-27 12:45:03 -07001322 // The xterm man page explicitly says this will happen on soft reset.
1323 this.setVTScrollRegion(null, null);
1324
1325 // Xterm also shows the cursor on soft reset, but does not alter the blink
1326 // state.
rgindaa19afe22012-01-25 15:40:22 -08001327 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001328};
1329
rgindac9bc5502012-01-18 11:48:44 -08001330/**
1331 * Move the cursor forward to the next tab stop, or to the last column
1332 * if no more tab stops are set.
1333 */
1334hterm.Terminal.prototype.forwardTabStop = function() {
1335 var column = this.screen_.cursorPosition.column;
1336
1337 for (var i = 0; i < this.tabStops_.length; i++) {
1338 if (this.tabStops_[i] > column) {
1339 this.setCursorColumn(this.tabStops_[i]);
1340 return;
1341 }
1342 }
1343
David Benjamin66e954d2012-05-05 21:08:12 -04001344 // xterm does not clear the overflow flag on HT or CHT.
1345 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001346 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001347 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001348};
1349
rgindac9bc5502012-01-18 11:48:44 -08001350/**
1351 * Move the cursor backward to the previous tab stop, or to the first column
1352 * if no previous tab stops are set.
1353 */
1354hterm.Terminal.prototype.backwardTabStop = function() {
1355 var column = this.screen_.cursorPosition.column;
1356
1357 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1358 if (this.tabStops_[i] < column) {
1359 this.setCursorColumn(this.tabStops_[i]);
1360 return;
1361 }
1362 }
1363
1364 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001365};
1366
rgindac9bc5502012-01-18 11:48:44 -08001367/**
1368 * Set a tab stop at the given column.
1369 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001370 * @param {integer} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001371 */
1372hterm.Terminal.prototype.setTabStop = function(column) {
1373 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1374 if (this.tabStops_[i] == column)
1375 return;
1376
1377 if (this.tabStops_[i] < column) {
1378 this.tabStops_.splice(i + 1, 0, column);
1379 return;
1380 }
1381 }
1382
1383 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001384};
1385
rgindac9bc5502012-01-18 11:48:44 -08001386/**
1387 * Clear the tab stop at the current cursor position.
1388 *
1389 * No effect if there is no tab stop at the current cursor position.
1390 */
1391hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1392 var column = this.screen_.cursorPosition.column;
1393
1394 var i = this.tabStops_.indexOf(column);
1395 if (i == -1)
1396 return;
1397
1398 this.tabStops_.splice(i, 1);
1399};
1400
1401/**
1402 * Clear all tab stops.
1403 */
1404hterm.Terminal.prototype.clearAllTabStops = function() {
1405 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001406 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001407};
1408
1409/**
1410 * Set up the default tab stops, starting from a given column.
1411 *
1412 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001413 * from the specified column, or 0 if no column is provided. It also flags
1414 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001415 *
1416 * This does not clear the existing tab stops first, use clearAllTabStops
1417 * for that.
1418 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001419 * @param {integer} opt_start Optional starting zero based starting column, useful
rgindac9bc5502012-01-18 11:48:44 -08001420 * for filling out missing tab stops when the terminal is resized.
1421 */
1422hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1423 var start = opt_start || 0;
1424 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001425 // Round start up to a default tab stop.
1426 start = start - 1 - ((start - 1) % w) + w;
1427 for (var i = start; i < this.screenSize.width; i += w) {
1428 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001429 }
David Benjamin66e954d2012-05-05 21:08:12 -04001430
1431 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001432};
1433
rginda6d397402012-01-17 10:58:29 -08001434/**
rginda8ba33642011-12-14 12:31:31 -08001435 * Interpret a sequence of characters.
1436 *
1437 * Incomplete escape sequences are buffered until the next call.
1438 *
1439 * @param {string} str Sequence of characters to interpret or pass through.
1440 */
1441hterm.Terminal.prototype.interpret = function(str) {
rginda8ba33642011-12-14 12:31:31 -08001442 this.scheduleSyncCursorPosition_();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001443 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001444};
1445
1446/**
1447 * Take over the given DIV for use as the terminal display.
1448 *
1449 * @param {HTMLDivElement} div The div to use as the terminal display.
1450 */
1451hterm.Terminal.prototype.decorate = function(div) {
Mike Frysinger5768a9d2017-12-26 12:57:44 -05001452 const charset = div.ownerDocument.characterSet.toLowerCase();
1453 if (charset != 'utf-8') {
1454 console.warn(`Document encoding should be set to utf-8, not "${charset}";` +
1455 ` Add <meta charset='utf-8'/> to your HTML <head> to fix.`);
1456 }
1457
rginda87b86462011-12-14 13:48:03 -08001458 this.div_ = div;
1459
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001460 this.accessibilityReader_ = new hterm.AccessibilityReader(div);
1461
rginda8ba33642011-12-14 12:31:31 -08001462 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001463 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001464 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1465 this.scrollPort_.setBackgroundPosition(
1466 this.prefs_.get('background-position'));
Mike Frysinger08bad432017-04-24 00:50:54 -04001467 this.scrollPort_.setUserCssUrl(this.prefs_.get('user-css'));
1468 this.scrollPort_.setUserCssText(this.prefs_.get('user-css-text'));
Raymes Khoury177aec72018-06-26 10:58:53 +10001469 this.scrollPort_.setAccessibilityReader(this.accessibilityReader_);
rginda30f20f62012-04-05 16:36:19 -07001470
rginda0918b652012-04-04 11:26:24 -07001471 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001472
rginda9f5222b2012-03-05 11:53:28 -08001473 this.setFontSize(this.prefs_.get('font-size'));
1474 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001475
David Reveman8f552492012-03-28 12:18:41 -04001476 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001477 this.setScrollWheelMoveMultipler(
1478 this.prefs_.get('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001479
rginda8ba33642011-12-14 12:31:31 -08001480 this.document_ = this.scrollPort_.getDocument();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001481 this.accessibilityReader_.decorate(this.document_);
rginda8ba33642011-12-14 12:31:31 -08001482
Evan Jones5f9df812016-12-06 09:38:58 -05001483 this.document_.body.oncontextmenu = function() { return false; };
Mike Frysingercc114512017-09-11 21:39:17 -04001484 this.contextMenu.setDocument(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07001485
1486 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001487 var screenNode = this.scrollPort_.getScreenNode();
1488 screenNode.addEventListener('mousedown', onMouse);
1489 screenNode.addEventListener('mouseup', onMouse);
1490 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001491 this.scrollPort_.onScrollWheel = onMouse;
1492
Mike Frysinger02ded6d2018-06-21 14:25:20 -04001493 screenNode.addEventListener('keydown', this.onKeyboardActivity_.bind(this));
1494
Toni Barzic0bfa8922013-11-22 11:18:35 -08001495 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001496 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001497 // Listen for mousedown events on the screenNode as in FF the focus
1498 // events don't bubble.
1499 screenNode.addEventListener('mousedown', function() {
1500 setTimeout(this.onFocusChange_.bind(this, true));
1501 }.bind(this));
1502
Toni Barzic0bfa8922013-11-22 11:18:35 -08001503 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001504 'blur', this.onFocusChange_.bind(this, false));
1505
1506 var style = this.document_.createElement('style');
1507 style.textContent =
1508 ('.cursor-node[focus="false"] {' +
1509 ' box-sizing: border-box;' +
1510 ' background-color: transparent !important;' +
1511 ' border-width: 2px;' +
1512 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001513 '}' +
Mike Frysingercc114512017-09-11 21:39:17 -04001514 'menu {' +
1515 ' margin: 0;' +
1516 ' padding: 0;' +
1517 ' cursor: var(--hterm-mouse-cursor-pointer);' +
1518 '}' +
1519 'menuitem {' +
1520 ' white-space: nowrap;' +
1521 ' border-bottom: 1px dashed;' +
1522 ' display: block;' +
1523 ' padding: 0.3em 0.3em 0 0.3em;' +
1524 '}' +
1525 'menuitem.separator {' +
1526 ' border-bottom: none;' +
1527 ' height: 0.5em;' +
1528 ' padding: 0;' +
1529 '}' +
1530 'menuitem:hover {' +
1531 ' color: var(--hterm-cursor-color);' +
1532 '}' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001533 '.wc-node {' +
1534 ' display: inline-block;' +
1535 ' text-align: center;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001536 ' width: calc(var(--hterm-charsize-width) * 2);' +
Mike Frysinger4036f6e2017-05-31 14:02:55 -04001537 ' line-height: var(--hterm-charsize-height);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001538 '}' +
1539 ':root {' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001540 ' --hterm-charsize-width: ' + this.scrollPort_.characterSize.width + 'px;' +
1541 ' --hterm-charsize-height: ' + this.scrollPort_.characterSize.height + 'px;' +
Mike Frysingera27c0502017-08-23 22:37:10 -04001542 // Default position hides the cursor for when the window is initializing.
1543 ' --hterm-cursor-offset-col: -1;' +
1544 ' --hterm-cursor-offset-row: -1;' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001545 ' --hterm-blink-node-duration: 0.7s;' +
Mike Frysinger67f58f82018-11-22 13:38:22 -05001546 ' --hterm-mouse-cursor-default: default;' +
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001547 ' --hterm-mouse-cursor-text: text;' +
Mike Frysinger67f58f82018-11-22 13:38:22 -05001548 ' --hterm-mouse-cursor-pointer: pointer;' +
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001549 ' --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001550 '}' +
Mike Frysingerd3907202017-10-23 01:23:50 -04001551 '.uri-node:hover {' +
1552 ' text-decoration: underline;' +
Mike Frysinger67f58f82018-11-22 13:38:22 -05001553 ' cursor: var(--hterm-mouse-cursor-pointer);' +
Mike Frysingerd3907202017-10-23 01:23:50 -04001554 '}' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001555 '@keyframes blink {' +
1556 ' from { opacity: 1.0; }' +
1557 ' to { opacity: 0.0; }' +
1558 '}' +
1559 '.blink-node {' +
1560 ' animation-name: blink;' +
1561 ' animation-duration: var(--hterm-blink-node-duration);' +
1562 ' animation-iteration-count: infinite;' +
1563 ' animation-timing-function: ease-in-out;' +
1564 ' animation-direction: alternate;' +
rginda8e92a692012-05-20 19:37:20 -07001565 '}');
Mike Frysingerb74a6472018-06-22 13:37:08 -04001566 // Insert this stock style as the first node so that any user styles will
1567 // override w/out having to use !important everywhere. The rules above mix
1568 // runtime variables with default ones designed to be overridden by the user,
1569 // but we can wait for a concrete case from the users to determine the best
1570 // way to split the sheet up to before & after the user-css settings.
1571 this.document_.head.insertBefore(style, this.document_.head.firstChild);
rginda8e92a692012-05-20 19:37:20 -07001572
rginda8ba33642011-12-14 12:31:31 -08001573 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001574 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001575 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001576 this.cursorNode_.style.cssText =
1577 ('position: absolute;' +
Mike Frysinger44c32202017-08-05 01:13:09 -04001578 'left: calc(var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));' +
1579 'top: calc(var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));' +
Mike Frysingerd60f4c22018-03-15 21:25:30 -07001580 'display: ' + (this.options_.cursorVisible ? '' : 'none') + ';' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001581 'width: var(--hterm-charsize-width);' +
1582 'height: var(--hterm-charsize-height);' +
Mike Frysinger2fd079a2018-09-02 01:46:12 -04001583 'background-color: var(--hterm-cursor-color);' +
1584 'border-color: var(--hterm-cursor-color);' +
Rob Spies06533ba2014-04-24 11:20:37 -07001585 '-webkit-transition: opacity, background-color 100ms linear;' +
1586 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001587
Mike Frysingerf02a2cb2017-12-21 00:34:03 -05001588 this.setCursorColor();
Robert Gindafb1be6a2013-12-11 11:56:22 -08001589 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1590 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001591
rginda8ba33642011-12-14 12:31:31 -08001592 this.document_.body.appendChild(this.cursorNode_);
1593
rgindad5613292012-06-19 15:40:37 -07001594 // When 'enableMouseDragScroll' is off we reposition this element directly
1595 // under the mouse cursor after a click. This makes Chrome associate
1596 // subsequent mousemove events with the scroll-blocker. Since the
1597 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1598 // events do not cause the scrollport to scroll.
1599 //
1600 // It's a hack, but it's the cleanest way I could find.
1601 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001602 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
Raymes Khoury6dce2f82018-04-12 15:38:58 +10001603 this.scrollBlockerNode_.setAttribute('aria-hidden', 'true');
rgindad5613292012-06-19 15:40:37 -07001604 this.scrollBlockerNode_.style.cssText =
1605 ('position: absolute;' +
1606 'top: -99px;' +
1607 'display: block;' +
1608 'width: 10px;' +
1609 'height: 10px;');
1610 this.document_.body.appendChild(this.scrollBlockerNode_);
1611
rgindad5613292012-06-19 15:40:37 -07001612 this.scrollPort_.onScrollWheel = onMouse;
1613 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1614 ].forEach(function(event) {
1615 this.scrollBlockerNode_.addEventListener(event, onMouse);
1616 this.cursorNode_.addEventListener(event, onMouse);
1617 this.document_.addEventListener(event, onMouse);
1618 }.bind(this));
1619
1620 this.cursorNode_.addEventListener('mousedown', function() {
1621 setTimeout(this.focus.bind(this));
1622 }.bind(this));
1623
rginda8ba33642011-12-14 12:31:31 -08001624 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001625
rginda87b86462011-12-14 13:48:03 -08001626 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001627 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001628};
1629
rginda0918b652012-04-04 11:26:24 -07001630/**
1631 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001632 *
1633 * @return {HTMLDocument}
rginda0918b652012-04-04 11:26:24 -07001634 */
rginda87b86462011-12-14 13:48:03 -08001635hterm.Terminal.prototype.getDocument = function() {
1636 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001637};
1638
1639/**
rginda0918b652012-04-04 11:26:24 -07001640 * Focus the terminal.
1641 */
1642hterm.Terminal.prototype.focus = function() {
1643 this.scrollPort_.focus();
1644};
1645
1646/**
rginda8ba33642011-12-14 12:31:31 -08001647 * Return the HTML Element for a given row index.
1648 *
1649 * This is a method from the RowProvider interface. The ScrollPort uses
1650 * it to fetch rows on demand as they are scrolled into view.
1651 *
1652 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1653 * pairs to conserve memory.
1654 *
1655 * @param {integer} index The zero-based row index, measured relative to the
1656 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001657 * largest indices.
rginda8ba33642011-12-14 12:31:31 -08001658 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1659 */
1660hterm.Terminal.prototype.getRowNode = function(index) {
1661 if (index < this.scrollbackRows_.length)
1662 return this.scrollbackRows_[index];
1663
1664 var screenIndex = index - this.scrollbackRows_.length;
1665 return this.screen_.rowsArray[screenIndex];
1666};
1667
1668/**
1669 * Return the text content for a given range of rows.
1670 *
1671 * This is a method from the RowProvider interface. The ScrollPort uses
1672 * it to fetch text content on demand when the user attempts to copy their
1673 * selection to the clipboard.
1674 *
1675 * @param {integer} start The zero-based row index to start from, measured
1676 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001677 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001678 * @param {integer} end The zero-based row index to end on, measured
1679 * relative to the start of the scrollback buffer.
1680 * @return {string} A single string containing the text value of the range of
1681 * rows. Lines will be newline delimited, with no trailing newline.
1682 */
1683hterm.Terminal.prototype.getRowsText = function(start, end) {
1684 var ary = [];
1685 for (var i = start; i < end; i++) {
1686 var node = this.getRowNode(i);
1687 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001688 if (i < end - 1 && !node.getAttribute('line-overflow'))
1689 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001690 }
1691
rgindaa09e7332012-08-17 12:49:51 -07001692 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001693};
1694
1695/**
1696 * Return the text content for a given row.
1697 *
1698 * This is a method from the RowProvider interface. The ScrollPort uses
1699 * it to fetch text content on demand when the user attempts to copy their
1700 * selection to the clipboard.
1701 *
1702 * @param {integer} index The zero-based row index to return, measured
1703 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001704 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001705 * @return {string} A string containing the text value of the selected row.
1706 */
1707hterm.Terminal.prototype.getRowText = function(index) {
1708 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001709 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001710};
1711
1712/**
1713 * Return the total number of rows in the addressable screen and in the
1714 * scrollback buffer of this terminal.
1715 *
1716 * This is a method from the RowProvider interface. The ScrollPort uses
1717 * it to compute the size of the scrollbar.
1718 *
1719 * @return {integer} The number of rows in this terminal.
1720 */
1721hterm.Terminal.prototype.getRowCount = function() {
1722 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1723};
1724
1725/**
1726 * Create DOM nodes for new rows and append them to the end of the terminal.
1727 *
1728 * This is the only correct way to add a new DOM node for a row. Notice that
1729 * the new row is appended to the bottom of the list of rows, and does not
1730 * require renumbering (of the rowIndex property) of previous rows.
1731 *
1732 * If you think you want a new blank row somewhere in the middle of the
1733 * terminal, look into moveRows_().
1734 *
1735 * This method does not pay attention to vtScrollTop/Bottom, since you should
1736 * be using moveRows() in cases where they would matter.
1737 *
1738 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001739 *
1740 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001741 */
1742hterm.Terminal.prototype.appendRows_ = function(count) {
1743 var cursorRow = this.screen_.rowsArray.length;
1744 var offset = this.scrollbackRows_.length + cursorRow;
1745 for (var i = 0; i < count; i++) {
1746 var row = this.document_.createElement('x-row');
1747 row.appendChild(this.document_.createTextNode(''));
1748 row.rowIndex = offset + i;
1749 this.screen_.pushRow(row);
1750 }
1751
1752 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1753 if (extraRows > 0) {
1754 var ary = this.screen_.shiftRows(extraRows);
1755 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001756 if (this.scrollPort_.isScrolledEnd)
1757 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001758 }
1759
1760 if (cursorRow >= this.screen_.rowsArray.length)
1761 cursorRow = this.screen_.rowsArray.length - 1;
1762
rginda87b86462011-12-14 13:48:03 -08001763 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001764};
1765
1766/**
1767 * Relocate rows from one part of the addressable screen to another.
1768 *
1769 * This is used to recycle rows during VT scrolls (those which are driven
1770 * by VT commands, rather than by the user manipulating the scrollbar.)
1771 *
1772 * In this case, the blank lines scrolled into the scroll region are made of
1773 * the nodes we scrolled off. These have their rowIndex properties carefully
1774 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001775 *
1776 * @param {number} fromIndex The start index.
1777 * @param {number} count The number of rows to move.
1778 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001779 */
1780hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1781 var ary = this.screen_.removeRows(fromIndex, count);
1782 this.screen_.insertRows(toIndex, ary);
1783
1784 var start, end;
1785 if (fromIndex < toIndex) {
1786 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001787 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001788 } else {
1789 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001790 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001791 }
1792
1793 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001794 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001795};
1796
1797/**
1798 * Renumber the rowIndex property of the given range of rows.
1799 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001800 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08001801 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001802 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001803 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05001804 *
1805 * @param {number} start The start index.
1806 * @param {number} end The end index.
1807 * @param {hterm.Screen} opt_screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08001808 */
Robert Ginda40932892012-12-10 17:26:40 -08001809hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1810 var screen = opt_screen || this.screen_;
1811
rginda8ba33642011-12-14 12:31:31 -08001812 var offset = this.scrollbackRows_.length;
1813 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001814 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001815 }
1816};
1817
1818/**
1819 * Print a string to the terminal.
1820 *
1821 * This respects the current insert and wraparound modes. It will add new lines
1822 * to the end of the terminal, scrolling off the top into the scrollback buffer
1823 * if necessary.
1824 *
1825 * The string is *not* parsed for escape codes. Use the interpret() method if
1826 * that's what you're after.
1827 *
1828 * @param{string} str The string to print.
1829 */
1830hterm.Terminal.prototype.print = function(str) {
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001831 this.scheduleSyncCursorPosition_();
1832
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001833 // Basic accessibility output for the screen reader.
Raymes Khoury177aec72018-06-26 10:58:53 +10001834 this.accessibilityReader_.announce(str);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001835
rgindaa9abdd82012-08-06 18:05:09 -07001836 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001837
Ricky Liang48f05cb2013-12-31 23:35:29 +08001838 var strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04001839 // Fun edge case: If the string only contains zero width codepoints (like
1840 // combining characters), we make sure to iterate at least once below.
1841 if (strWidth == 0 && str)
1842 strWidth = 1;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001843
1844 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001845 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1846 this.screen_.commitLineOverflow();
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001847 this.newLine(true);
rgindaa09e7332012-08-17 12:49:51 -07001848 }
rgindaa19afe22012-01-25 15:40:22 -08001849
Ricky Liang48f05cb2013-12-31 23:35:29 +08001850 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001851 var didOverflow = false;
1852 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001853
rgindaa9abdd82012-08-06 18:05:09 -07001854 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1855 didOverflow = true;
1856 count = this.screenSize.width - this.screen_.cursorPosition.column;
1857 }
rgindaa19afe22012-01-25 15:40:22 -08001858
rgindaa9abdd82012-08-06 18:05:09 -07001859 if (didOverflow && !this.options_.wraparound) {
1860 // If the string overflowed the line but wraparound is off, then the
1861 // last printed character should be the last of the string.
1862 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001863 substr = lib.wc.substr(str, startOffset, count - 1) +
1864 lib.wc.substr(str, strWidth - 1);
1865 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001866 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001867 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001868 }
rgindaa19afe22012-01-25 15:40:22 -08001869
Ricky Liang48f05cb2013-12-31 23:35:29 +08001870 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1871 for (var i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001872 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
1873 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001874
1875 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001876 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001877 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001878 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001879 }
1880 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001881 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07001882 }
1883
1884 this.screen_.maybeClipCurrentRow();
1885 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001886 }
rginda8ba33642011-12-14 12:31:31 -08001887
rginda9f5222b2012-03-05 11:53:28 -08001888 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001889 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001890};
1891
1892/**
rginda87b86462011-12-14 13:48:03 -08001893 * Set the VT scroll region.
1894 *
rginda87b86462011-12-14 13:48:03 -08001895 * This also resets the cursor position to the absolute (0, 0) position, since
1896 * that's what xterm appears to do.
1897 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001898 * Setting the scroll region to the full height of the terminal will clear
1899 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1900 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1901 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1902 * continue to work as most users would expect.
1903 *
rginda87b86462011-12-14 13:48:03 -08001904 * @param {integer} scrollTop The zero-based top of the scroll region.
1905 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1906 * inclusive.
1907 */
1908hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001909 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001910 this.vtScrollTop_ = null;
1911 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001912 } else {
1913 this.vtScrollTop_ = scrollTop;
1914 this.vtScrollBottom_ = scrollBottom;
1915 }
rginda87b86462011-12-14 13:48:03 -08001916};
1917
1918/**
rginda8ba33642011-12-14 12:31:31 -08001919 * Return the top row index according to the VT.
1920 *
1921 * This will return 0 unless the terminal has been told to restrict scrolling
1922 * to some lower row. It is used for some VT cursor positioning and scrolling
1923 * commands.
1924 *
1925 * @return {integer} The topmost row in the terminal's scroll region.
1926 */
1927hterm.Terminal.prototype.getVTScrollTop = function() {
1928 if (this.vtScrollTop_ != null)
1929 return this.vtScrollTop_;
1930
1931 return 0;
rginda87b86462011-12-14 13:48:03 -08001932};
rginda8ba33642011-12-14 12:31:31 -08001933
1934/**
1935 * Return the bottom row index according to the VT.
1936 *
1937 * This will return the height of the terminal unless the it has been told to
1938 * restrict scrolling to some higher row. It is used for some VT cursor
1939 * positioning and scrolling commands.
1940 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001941 * @return {integer} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001942 */
1943hterm.Terminal.prototype.getVTScrollBottom = function() {
1944 if (this.vtScrollBottom_ != null)
1945 return this.vtScrollBottom_;
1946
rginda87b86462011-12-14 13:48:03 -08001947 return this.screenSize.height - 1;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001948};
rginda8ba33642011-12-14 12:31:31 -08001949
1950/**
1951 * Process a '\n' character.
1952 *
1953 * If the cursor is on the final row of the terminal this will append a new
1954 * blank row to the screen and scroll the topmost row into the scrollback
1955 * buffer.
1956 *
1957 * Otherwise, this moves the cursor to column zero of the next row.
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001958 *
1959 * @param {boolean=} dueToOverflow Whether the newline is due to wraparound of
1960 * the terminal.
rginda8ba33642011-12-14 12:31:31 -08001961 */
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001962hterm.Terminal.prototype.newLine = function(dueToOverflow = false) {
1963 if (!dueToOverflow)
1964 this.accessibilityReader_.newLine();
1965
Robert Ginda9937abc2013-07-25 16:09:23 -07001966 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1967 this.screen_.rowsArray.length - 1);
1968
1969 if (this.vtScrollBottom_ != null) {
1970 // A VT Scroll region is active, we never append new rows.
1971 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1972 // We're at the end of the VT Scroll Region, perform a VT scroll.
1973 this.vtScrollUp(1);
1974 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1975 } else if (cursorAtEndOfScreen) {
1976 // We're at the end of the screen, the only thing to do is put the
1977 // cursor to column 0.
1978 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1979 } else {
1980 // Anywhere else, advance the cursor row, and reset the column.
1981 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1982 }
1983 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001984 // We're at the end of the screen. Append a new row to the terminal,
1985 // shifting the top row into the scrollback.
1986 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001987 } else {
rginda87b86462011-12-14 13:48:03 -08001988 // Anywhere else in the screen just moves the cursor.
1989 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001990 }
1991};
1992
1993/**
1994 * Like newLine(), except maintain the cursor column.
1995 */
1996hterm.Terminal.prototype.lineFeed = function() {
1997 var column = this.screen_.cursorPosition.column;
1998 this.newLine();
1999 this.setCursorColumn(column);
2000};
2001
2002/**
rginda87b86462011-12-14 13:48:03 -08002003 * If autoCarriageReturn is set then newLine(), else lineFeed().
2004 */
2005hterm.Terminal.prototype.formFeed = function() {
2006 if (this.options_.autoCarriageReturn) {
2007 this.newLine();
2008 } else {
2009 this.lineFeed();
2010 }
2011};
2012
2013/**
2014 * Move the cursor up one row, possibly inserting a blank line.
2015 *
2016 * The cursor column is not changed.
2017 */
2018hterm.Terminal.prototype.reverseLineFeed = function() {
2019 var scrollTop = this.getVTScrollTop();
2020 var currentRow = this.screen_.cursorPosition.row;
2021
2022 if (currentRow == scrollTop) {
2023 this.insertLines(1);
2024 } else {
2025 this.setAbsoluteCursorRow(currentRow - 1);
2026 }
2027};
2028
2029/**
rginda8ba33642011-12-14 12:31:31 -08002030 * Replace all characters to the left of the current cursor with the space
2031 * character.
2032 *
2033 * TODO(rginda): This should probably *remove* the characters (not just replace
2034 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07002035 * position.
rginda8ba33642011-12-14 12:31:31 -08002036 */
2037hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08002038 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002039 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002040 const count = cursor.column + 1;
2041 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08002042 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002043};
2044
2045/**
David Benjamin684a9b72012-05-01 17:19:58 -04002046 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08002047 *
2048 * The cursor position is unchanged.
2049 *
Robert Gindaf2547f12012-10-25 20:36:21 -07002050 * If the current background color is not the default background color this
2051 * will insert spaces rather than delete. This is unfortunate because the
2052 * trailing space will affect text selection, but it's difficult to come up
2053 * with a way to style empty space that wouldn't trip up the hterm.Screen
2054 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07002055 *
2056 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
2057 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
2058 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05002059 *
2060 * @param {number} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08002061 */
2062hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07002063 if (this.screen_.cursorPosition.overflow)
2064 return;
2065
Robert Ginda7fd57082012-09-25 14:41:47 -07002066 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
2067 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07002068
2069 if (this.screen_.textAttributes.background ===
2070 this.screen_.textAttributes.DEFAULT_COLOR) {
2071 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08002072 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07002073 this.screen_.cursorPosition.column + count) {
2074 this.screen_.deleteChars(count);
2075 this.clearCursorOverflow();
2076 return;
2077 }
2078 }
2079
rginda87b86462011-12-14 13:48:03 -08002080 var cursor = this.saveCursor();
Mike Frysinger6380bed2017-08-24 18:46:39 -04002081 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08002082 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002083 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002084};
2085
2086/**
2087 * Erase the current line.
2088 *
2089 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002090 */
2091hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08002092 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002093 this.screen_.clearCursorRow();
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/**
David Benjamina08d78f2012-05-05 00:28:49 -04002099 * Erase all characters from the start of the screen to the current cursor
2100 * position, 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.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08002105 var cursor = this.saveCursor();
2106
2107 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002108
David Benjamina08d78f2012-05-05 00:28:49 -04002109 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002110 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002111 this.screen_.clearCursorRow();
2112 }
2113
rginda87b86462011-12-14 13:48:03 -08002114 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002115 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002116};
2117
2118/**
2119 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002120 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002121 *
2122 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002123 */
2124hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08002125 var cursor = this.saveCursor();
2126
2127 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002128
David Benjamina08d78f2012-05-05 00:28:49 -04002129 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08002130 for (var i = cursor.row + 1; i <= bottom; i++) {
2131 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002132 this.screen_.clearCursorRow();
2133 }
2134
rginda87b86462011-12-14 13:48:03 -08002135 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002136 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002137};
2138
2139/**
2140 * Fill the terminal with a given character.
2141 *
2142 * This methods does not respect the VT scroll region.
2143 *
2144 * @param {string} ch The character to use for the fill.
2145 */
2146hterm.Terminal.prototype.fill = function(ch) {
2147 var cursor = this.saveCursor();
2148
2149 this.setAbsoluteCursorPosition(0, 0);
2150 for (var row = 0; row < this.screenSize.height; row++) {
2151 for (var col = 0; col < this.screenSize.width; col++) {
2152 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002153 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002154 }
2155 }
2156
2157 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002158};
2159
2160/**
rginda9ea433c2012-03-16 11:57:00 -07002161 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002162 *
rginda9ea433c2012-03-16 11:57:00 -07002163 * This does not respect the scroll region.
2164 *
2165 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2166 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002167 */
rginda9ea433c2012-03-16 11:57:00 -07002168hterm.Terminal.prototype.clearHome = function(opt_screen) {
2169 var screen = opt_screen || this.screen_;
2170 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002171
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002172 this.accessibilityReader_.clear();
2173
rginda11057d52012-04-25 12:29:56 -07002174 if (bottom == 0) {
2175 // Empty screen, nothing to do.
2176 return;
2177 }
2178
rgindae4d29232012-01-19 10:47:13 -08002179 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002180 screen.setCursorPosition(i, 0);
2181 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002182 }
2183
rginda9ea433c2012-03-16 11:57:00 -07002184 screen.setCursorPosition(0, 0);
2185};
2186
2187/**
2188 * Erase the entire display without changing the cursor position.
2189 *
2190 * The cursor position is unchanged. This does not respect the scroll
2191 * region.
2192 *
2193 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2194 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002195 */
2196hterm.Terminal.prototype.clear = function(opt_screen) {
2197 var screen = opt_screen || this.screen_;
2198 var cursor = screen.cursorPosition.clone();
2199 this.clearHome(screen);
2200 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002201};
2202
2203/**
2204 * VT command to insert lines at the current cursor row.
2205 *
2206 * This respects the current scroll region. Rows pushed off the bottom are
2207 * lost (they won't show up in the scrollback buffer).
2208 *
rginda8ba33642011-12-14 12:31:31 -08002209 * @param {integer} count The number of lines to insert.
2210 */
2211hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07002212 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002213
2214 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002215 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002216
Robert Ginda579186b2012-09-26 11:40:04 -07002217 // The moveCount is the number of rows we need to relocate to make room for
2218 // the new row(s). The count is the distance to move them.
2219 var moveCount = bottom - cursorRow - count + 1;
2220 if (moveCount)
2221 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08002222
Robert Ginda579186b2012-09-26 11:40:04 -07002223 for (var i = count - 1; i >= 0; i--) {
2224 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002225 this.screen_.clearCursorRow();
2226 }
rginda8ba33642011-12-14 12:31:31 -08002227};
2228
2229/**
2230 * VT command to delete lines at the current cursor row.
2231 *
2232 * New rows are added to the bottom of scroll region to take their place. New
2233 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002234 *
2235 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002236 */
2237hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08002238 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002239
rginda87b86462011-12-14 13:48:03 -08002240 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08002241 var bottom = this.getVTScrollBottom();
2242
rginda87b86462011-12-14 13:48:03 -08002243 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002244 count = Math.min(count, maxCount);
2245
rginda87b86462011-12-14 13:48:03 -08002246 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08002247 if (count != maxCount)
2248 this.moveRows_(top, count, moveStart);
2249
2250 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002251 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002252 this.screen_.clearCursorRow();
2253 }
2254
rginda87b86462011-12-14 13:48:03 -08002255 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002256 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002257};
2258
2259/**
2260 * Inserts the given number of spaces at the current cursor position.
2261 *
rginda87b86462011-12-14 13:48:03 -08002262 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002263 *
2264 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002265 */
2266hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08002267 var cursor = this.saveCursor();
2268
rgindacbbd7482012-06-13 15:06:16 -07002269 var ws = lib.f.getWhitespace(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002270 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002271 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002272
2273 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002274 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002275};
2276
2277/**
2278 * Forward-delete the specified number of characters starting at the cursor
2279 * position.
2280 *
2281 * @param {integer} count The number of characters to delete.
2282 */
2283hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002284 var deleted = this.screen_.deleteChars(count);
2285 if (deleted && !this.screen_.textAttributes.isDefault()) {
2286 var cursor = this.saveCursor();
2287 this.setCursorColumn(this.screenSize.width - deleted);
2288 this.screen_.insertString(lib.f.getWhitespace(deleted));
2289 this.restoreCursor(cursor);
2290 }
2291
David Benjamin54e8bf62012-06-01 22:31:40 -04002292 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002293};
2294
2295/**
2296 * Shift rows in the scroll region upwards by a given number of lines.
2297 *
2298 * New rows are inserted at the bottom of the scroll region to fill the
2299 * vacated rows. The new rows not filled out with the current text attributes.
2300 *
2301 * This function does not affect the scrollback rows at all. Rows shifted
2302 * off the top are lost.
2303 *
rginda87b86462011-12-14 13:48:03 -08002304 * The cursor position is not altered.
2305 *
rginda8ba33642011-12-14 12:31:31 -08002306 * @param {integer} count The number of rows to scroll.
2307 */
2308hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002309 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002310
rginda87b86462011-12-14 13:48:03 -08002311 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002312 this.deleteLines(count);
2313
rginda87b86462011-12-14 13:48:03 -08002314 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002315};
2316
2317/**
2318 * Shift rows below the cursor down by a given number of lines.
2319 *
2320 * This function respects the current scroll region.
2321 *
2322 * New rows are inserted at the top of the scroll region to fill the
2323 * vacated rows. The new rows not filled out with the current text attributes.
2324 *
2325 * This function does not affect the scrollback rows at all. Rows shifted
2326 * off the bottom are lost.
2327 *
2328 * @param {integer} count The number of rows to scroll.
2329 */
2330hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08002331 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002332
rginda87b86462011-12-14 13:48:03 -08002333 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08002334 this.insertLines(opt_count);
2335
rginda87b86462011-12-14 13:48:03 -08002336 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002337};
2338
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002339/**
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002340 * Enable accessibility-friendly features that have a performance impact.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002341 *
2342 * This will generate additional DOM nodes in an aria-live region that will
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002343 * cause Assitive Technology to announce the output of the terminal. It also
2344 * enables other features that aid assistive technology. All the features gated
2345 * behind this flag have a performance impact on the terminal which is why they
2346 * are made optional.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002347 *
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002348 * @param {boolean} enabled Whether to enable accessibility-friendly features.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002349 */
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002350hterm.Terminal.prototype.setAccessibilityEnabled = function(enabled) {
Raymes Khoury177aec72018-06-26 10:58:53 +10002351 this.accessibilityReader_.setAccessibilityEnabled(enabled);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002352};
rginda87b86462011-12-14 13:48:03 -08002353
rginda8ba33642011-12-14 12:31:31 -08002354/**
2355 * Set the cursor position.
2356 *
2357 * The cursor row is relative to the scroll region if the terminal has
2358 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2359 *
2360 * @param {integer} row The new zero-based cursor row.
2361 * @param {integer} row The new zero-based cursor column.
2362 */
2363hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2364 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002365 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002366 } else {
rginda87b86462011-12-14 13:48:03 -08002367 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002368 }
rginda87b86462011-12-14 13:48:03 -08002369};
rginda8ba33642011-12-14 12:31:31 -08002370
Evan Jones2600d4f2016-12-06 09:29:36 -05002371/**
2372 * Move the cursor relative to its current position.
2373 *
2374 * @param {number} row
2375 * @param {number} column
2376 */
rginda87b86462011-12-14 13:48:03 -08002377hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2378 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002379 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2380 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002381 this.screen_.setCursorPosition(row, column);
2382};
2383
Evan Jones2600d4f2016-12-06 09:29:36 -05002384/**
2385 * Move the cursor to the specified position.
2386 *
2387 * @param {number} row
2388 * @param {number} column
2389 */
rginda87b86462011-12-14 13:48:03 -08002390hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002391 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2392 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002393 this.screen_.setCursorPosition(row, column);
2394};
2395
2396/**
2397 * Set the cursor column.
2398 *
2399 * @param {integer} column The new zero-based cursor column.
2400 */
2401hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002402 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002403};
2404
2405/**
2406 * Return the cursor column.
2407 *
2408 * @return {integer} The zero-based cursor column.
2409 */
2410hterm.Terminal.prototype.getCursorColumn = function() {
2411 return this.screen_.cursorPosition.column;
2412};
2413
2414/**
2415 * Set the cursor row.
2416 *
2417 * The cursor row is relative to the scroll region if the terminal has
2418 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2419 *
2420 * @param {integer} row The new cursor row.
2421 */
rginda87b86462011-12-14 13:48:03 -08002422hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2423 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002424};
2425
2426/**
2427 * Return the cursor row.
2428 *
2429 * @return {integer} The zero-based cursor row.
2430 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002431hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002432 return this.screen_.cursorPosition.row;
2433};
2434
2435/**
2436 * Request that the ScrollPort redraw itself soon.
2437 *
2438 * The redraw will happen asynchronously, soon after the call stack winds down.
2439 * Multiple calls will be coalesced into a single redraw.
2440 */
2441hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002442 if (this.timeouts_.redraw)
2443 return;
rginda8ba33642011-12-14 12:31:31 -08002444
2445 var self = this;
rginda87b86462011-12-14 13:48:03 -08002446 this.timeouts_.redraw = setTimeout(function() {
2447 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002448 self.scrollPort_.redraw_();
2449 }, 0);
2450};
2451
2452/**
2453 * Request that the ScrollPort be scrolled to the bottom.
2454 *
2455 * The scroll will happen asynchronously, soon after the call stack winds down.
2456 * Multiple calls will be coalesced into a single scroll.
2457 *
2458 * This affects the scrollbar position of the ScrollPort, and has nothing to
2459 * do with the VT scroll commands.
2460 */
2461hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2462 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002463 return;
rginda8ba33642011-12-14 12:31:31 -08002464
2465 var self = this;
2466 this.timeouts_.scrollDown = setTimeout(function() {
2467 delete self.timeouts_.scrollDown;
2468 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2469 }, 10);
2470};
2471
2472/**
2473 * Move the cursor up a specified number of rows.
2474 *
2475 * @param {integer} count The number of rows to move the cursor.
2476 */
2477hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002478 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002479};
2480
2481/**
2482 * Move the cursor down a specified number of rows.
2483 *
2484 * @param {integer} count The number of rows to move the cursor.
2485 */
2486hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002487 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002488 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2489 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2490 this.screenSize.height - 1);
2491
rgindacbbd7482012-06-13 15:06:16 -07002492 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002493 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002494 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002495};
2496
2497/**
2498 * Move the cursor left a specified number of columns.
2499 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002500 * If reverse wraparound mode is enabled and the previous row wrapped into
2501 * the current row then we back up through the wraparound as well.
2502 *
rginda8ba33642011-12-14 12:31:31 -08002503 * @param {integer} count The number of columns to move the cursor.
2504 */
2505hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002506 count = count || 1;
2507
2508 if (count < 1)
2509 return;
2510
2511 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002512 if (this.options_.reverseWraparound) {
2513 if (this.screen_.cursorPosition.overflow) {
2514 // If this cursor is in the right margin, consume one count to get it
2515 // back to the last column. This only applies when we're in reverse
2516 // wraparound mode.
2517 count--;
2518 this.clearCursorOverflow();
2519
2520 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002521 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002522 }
2523
Robert Gindabfb32622014-07-17 13:20:27 -07002524 var newRow = this.screen_.cursorPosition.row;
2525 var newColumn = currentColumn - count;
2526 if (newColumn < 0) {
2527 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2528 if (newRow < 0) {
2529 // xterm also wraps from row 0 to the last row.
2530 newRow = this.screenSize.height + newRow % this.screenSize.height;
2531 }
2532 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2533 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002534
Robert Gindabfb32622014-07-17 13:20:27 -07002535 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2536
2537 } else {
2538 var newColumn = Math.max(currentColumn - count, 0);
2539 this.setCursorColumn(newColumn);
2540 }
rginda8ba33642011-12-14 12:31:31 -08002541};
2542
2543/**
2544 * Move the cursor right a specified number of columns.
2545 *
2546 * @param {integer} count The number of columns to move the cursor.
2547 */
2548hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002549 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002550
2551 if (count < 1)
2552 return;
2553
rgindacbbd7482012-06-13 15:06:16 -07002554 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002555 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002556 this.setCursorColumn(column);
2557};
2558
2559/**
2560 * Reverse the foreground and background colors of the terminal.
2561 *
2562 * This only affects text that was drawn with no attributes.
2563 *
2564 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2565 * been drawn with attributes that happen to coincide with the default
2566 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002567 *
2568 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002569 */
2570hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002571 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002572 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002573 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2574 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002575 } else {
rginda9f5222b2012-03-05 11:53:28 -08002576 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2577 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002578 }
2579};
2580
2581/**
rginda87b86462011-12-14 13:48:03 -08002582 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002583 *
2584 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002585 */
2586hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002587 this.cursorNode_.style.backgroundColor =
2588 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002589
2590 var self = this;
2591 setTimeout(function() {
Matheus Fernandes2d733082017-09-11 06:43:01 -04002592 self.restyleCursor_();
rginda6d397402012-01-17 10:58:29 -08002593 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002594
Michael Kelly485ecd12014-06-09 11:41:56 -04002595 // bellSquelchTimeout_ affects both audio and notification bells.
2596 if (this.bellSquelchTimeout_)
2597 return;
2598
Robert Ginda92e18102013-03-14 13:56:37 -07002599 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002600 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002601 this.bellSequelchTimeout_ = setTimeout(function() {
2602 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002603 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002604 } else {
2605 delete this.bellSquelchTimeout_;
2606 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002607
2608 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingera5fb83c2017-06-22 14:48:35 -07002609 var n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002610 this.bellNotificationList_.push(n);
2611 // TODO: Should we try to raise the window here?
2612 n.onclick = function() { self.closeBellNotifications_(); };
2613 }
rginda87b86462011-12-14 13:48:03 -08002614};
2615
2616/**
rginda8ba33642011-12-14 12:31:31 -08002617 * Set the origin mode bit.
2618 *
2619 * If origin mode is on, certain VT cursor and scrolling commands measure their
2620 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2621 * to the top of the addressable screen.
2622 *
2623 * Defaults to off.
2624 *
2625 * @param {boolean} state True to set origin mode, false to unset.
2626 */
2627hterm.Terminal.prototype.setOriginMode = function(state) {
2628 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002629 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002630};
2631
2632/**
2633 * Set the insert mode bit.
2634 *
2635 * If insert mode is on, existing text beyond the cursor position will be
2636 * shifted right to make room for new text. Otherwise, new text overwrites
2637 * any existing text.
2638 *
2639 * Defaults to off.
2640 *
2641 * @param {boolean} state True to set insert mode, false to unset.
2642 */
2643hterm.Terminal.prototype.setInsertMode = function(state) {
2644 this.options_.insertMode = state;
2645};
2646
2647/**
rginda87b86462011-12-14 13:48:03 -08002648 * Set the auto carriage return bit.
2649 *
2650 * If auto carriage return is on then a formfeed character is interpreted
2651 * as a newline, otherwise it's the same as a linefeed. The difference boils
2652 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002653 *
2654 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002655 */
2656hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2657 this.options_.autoCarriageReturn = state;
2658};
2659
2660/**
rginda8ba33642011-12-14 12:31:31 -08002661 * Set the wraparound mode bit.
2662 *
2663 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2664 * to the start of the following row. Otherwise, the cursor is clamped to the
2665 * end of the screen and attempts to write past it are ignored.
2666 *
2667 * Defaults to on.
2668 *
2669 * @param {boolean} state True to set wraparound mode, false to unset.
2670 */
2671hterm.Terminal.prototype.setWraparound = function(state) {
2672 this.options_.wraparound = state;
2673};
2674
2675/**
2676 * Set the reverse-wraparound mode bit.
2677 *
2678 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2679 * to the end of the previous row. Otherwise, the cursor is clamped to column
2680 * 0.
2681 *
2682 * Defaults to off.
2683 *
2684 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2685 */
2686hterm.Terminal.prototype.setReverseWraparound = function(state) {
2687 this.options_.reverseWraparound = state;
2688};
2689
2690/**
2691 * Selects between the primary and alternate screens.
2692 *
2693 * If alternate mode is on, the alternate screen is active. Otherwise the
2694 * primary screen is active.
2695 *
2696 * Swapping screens has no effect on the scrollback buffer.
2697 *
2698 * Each screen maintains its own cursor position.
2699 *
2700 * Defaults to off.
2701 *
2702 * @param {boolean} state True to set alternate mode, false to unset.
2703 */
2704hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002705 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002706 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2707
rginda35c456b2012-02-09 17:29:05 -08002708 if (this.screen_.rowsArray.length &&
2709 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2710 // If the screen changed sizes while we were away, our rowIndexes may
2711 // be incorrect.
2712 var offset = this.scrollbackRows_.length;
2713 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002714 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002715 ary[i].rowIndex = offset + i;
2716 }
2717 }
rginda8ba33642011-12-14 12:31:31 -08002718
rginda35c456b2012-02-09 17:29:05 -08002719 this.realizeWidth_(this.screenSize.width);
2720 this.realizeHeight_(this.screenSize.height);
2721 this.scrollPort_.syncScrollHeight();
2722 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002723
rginda6d397402012-01-17 10:58:29 -08002724 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002725 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002726};
2727
2728/**
2729 * Set the cursor-blink mode bit.
2730 *
2731 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2732 * a visible cursor does not blink.
2733 *
2734 * You should make sure to turn blinking off if you're going to dispose of a
2735 * terminal, otherwise you'll leak a timeout.
2736 *
2737 * Defaults to on.
2738 *
2739 * @param {boolean} state True to set cursor-blink mode, false to unset.
2740 */
2741hterm.Terminal.prototype.setCursorBlink = function(state) {
2742 this.options_.cursorBlink = state;
2743
2744 if (!state && this.timeouts_.cursorBlink) {
2745 clearTimeout(this.timeouts_.cursorBlink);
2746 delete this.timeouts_.cursorBlink;
2747 }
2748
2749 if (this.options_.cursorVisible)
2750 this.setCursorVisible(true);
2751};
2752
2753/**
2754 * Set the cursor-visible mode bit.
2755 *
2756 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2757 *
2758 * Defaults to on.
2759 *
2760 * @param {boolean} state True to set cursor-visible mode, false to unset.
2761 */
2762hterm.Terminal.prototype.setCursorVisible = function(state) {
2763 this.options_.cursorVisible = state;
2764
2765 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002766 if (this.timeouts_.cursorBlink) {
2767 clearTimeout(this.timeouts_.cursorBlink);
2768 delete this.timeouts_.cursorBlink;
2769 }
rginda87b86462011-12-14 13:48:03 -08002770 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002771 return;
2772 }
2773
rginda87b86462011-12-14 13:48:03 -08002774 this.syncCursorPosition_();
2775
2776 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002777
2778 if (this.options_.cursorBlink) {
2779 if (this.timeouts_.cursorBlink)
2780 return;
2781
Robert Gindaea2183e2014-07-17 09:51:51 -07002782 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002783 } else {
2784 if (this.timeouts_.cursorBlink) {
2785 clearTimeout(this.timeouts_.cursorBlink);
2786 delete this.timeouts_.cursorBlink;
2787 }
2788 }
2789};
2790
2791/**
rginda87b86462011-12-14 13:48:03 -08002792 * Synchronizes the visible cursor and document selection with the current
2793 * cursor coordinates.
Raymes Khourye5d48982018-08-02 09:08:32 +10002794 *
2795 * @return {boolean} True if the cursor is onscreen and synced.
rginda8ba33642011-12-14 12:31:31 -08002796 */
2797hterm.Terminal.prototype.syncCursorPosition_ = function() {
2798 var topRowIndex = this.scrollPort_.getTopRowIndex();
2799 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2800 var cursorRowIndex = this.scrollbackRows_.length +
2801 this.screen_.cursorPosition.row;
2802
Raymes Khoury15697f42018-07-17 11:37:18 +10002803 let forceSyncSelection = false;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002804 if (this.accessibilityReader_.accessibilityEnabled) {
2805 // Report the new position of the cursor for accessibility purposes.
2806 const cursorColumnIndex = this.screen_.cursorPosition.column;
2807 const cursorLineText =
2808 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
Raymes Khoury15697f42018-07-17 11:37:18 +10002809 // This will force the selection to be sync'd to the cursor position if the
2810 // user has pressed a key. Generally we would only sync the cursor position
2811 // when selection is collapsed so that if the user has selected something
2812 // we don't clear the selection by moving the selection. However when a
2813 // screen reader is used, it's intuitive for entering a key to move the
2814 // selection to the cursor.
2815 forceSyncSelection = this.accessibilityReader_.hasUserGesture;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002816 this.accessibilityReader_.afterCursorChange(
2817 cursorLineText, cursorRowIndex, cursorColumnIndex);
2818 }
2819
rginda8ba33642011-12-14 12:31:31 -08002820 if (cursorRowIndex > bottomRowIndex) {
2821 // Cursor is scrolled off screen, move it outside of the visible area.
Mike Frysinger44c32202017-08-05 01:13:09 -04002822 this.setCssVar('cursor-offset-row', '-1');
Raymes Khourye5d48982018-08-02 09:08:32 +10002823 return false;
rginda8ba33642011-12-14 12:31:31 -08002824 }
2825
Robert Gindab837c052014-08-11 11:17:51 -07002826 if (this.options_.cursorVisible &&
2827 this.cursorNode_.style.display == 'none') {
2828 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2829 this.cursorNode_.style.display = '';
2830 }
2831
Mike Frysinger44c32202017-08-05 01:13:09 -04002832 // Position the cursor using CSS variable math. If we do the math in JS,
2833 // the float math will end up being more precise than the CSS which will
2834 // cause the cursor tracking to be off.
2835 this.setCssVar(
2836 'cursor-offset-row',
2837 `${cursorRowIndex - topRowIndex} + ` +
2838 `${this.scrollPort_.visibleRowTopMargin}px`);
2839 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08002840
2841 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04002842 '(' + this.screen_.cursorPosition.column +
2843 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08002844 ')');
2845
2846 // Update the caret for a11y purposes.
2847 var selection = this.document_.getSelection();
Raymes Khoury15697f42018-07-17 11:37:18 +10002848 if (selection && (selection.isCollapsed || forceSyncSelection)) {
rginda87b86462011-12-14 13:48:03 -08002849 this.screen_.syncSelectionCaret(selection);
Raymes Khoury15697f42018-07-17 11:37:18 +10002850 }
Raymes Khourye5d48982018-08-02 09:08:32 +10002851 return true;
rginda8ba33642011-12-14 12:31:31 -08002852};
2853
Robert Gindafb1be6a2013-12-11 11:56:22 -08002854/**
2855 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2856 * and character cell dimensions.
2857 */
Robert Ginda830583c2013-08-07 13:20:46 -07002858hterm.Terminal.prototype.restyleCursor_ = function() {
2859 var shape = this.cursorShape_;
2860
2861 if (this.cursorNode_.getAttribute('focus') == 'false') {
2862 // Always show a block cursor when unfocused.
2863 shape = hterm.Terminal.cursorShape.BLOCK;
2864 }
2865
2866 var style = this.cursorNode_.style;
2867
2868 switch (shape) {
2869 case hterm.Terminal.cursorShape.BEAM:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002870 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002871 style.backgroundColor = 'transparent';
2872 style.borderBottomStyle = null;
2873 style.borderLeftStyle = 'solid';
2874 break;
2875
2876 case hterm.Terminal.cursorShape.UNDERLINE:
2877 style.height = this.scrollPort_.characterSize.baseline + 'px';
2878 style.backgroundColor = 'transparent';
2879 style.borderBottomStyle = 'solid';
2880 // correct the size to put it exactly at the baseline
2881 style.borderLeftStyle = null;
2882 break;
2883
2884 default:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002885 style.height = 'var(--hterm-charsize-height)';
Mike Frysinger2fd079a2018-09-02 01:46:12 -04002886 style.backgroundColor = 'var(--hterm-cursor-color)';
Robert Ginda830583c2013-08-07 13:20:46 -07002887 style.borderBottomStyle = null;
2888 style.borderLeftStyle = null;
2889 break;
2890 }
2891};
2892
rginda8ba33642011-12-14 12:31:31 -08002893/**
2894 * Synchronizes the visible cursor with the current cursor coordinates.
2895 *
2896 * The sync will happen asynchronously, soon after the call stack winds down.
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002897 * Multiple calls will be coalesced into a single sync. This should be called
2898 * prior to the cursor actually changing position.
rginda8ba33642011-12-14 12:31:31 -08002899 */
2900hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2901 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002902 return;
rginda8ba33642011-12-14 12:31:31 -08002903
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002904 if (this.accessibilityReader_.accessibilityEnabled) {
2905 // Report the previous position of the cursor for accessibility purposes.
2906 const cursorRowIndex = this.scrollbackRows_.length +
2907 this.screen_.cursorPosition.row;
2908 const cursorColumnIndex = this.screen_.cursorPosition.column;
2909 const cursorLineText =
2910 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
2911 this.accessibilityReader_.beforeCursorChange(
2912 cursorLineText, cursorRowIndex, cursorColumnIndex);
2913 }
2914
rginda8ba33642011-12-14 12:31:31 -08002915 var self = this;
2916 this.timeouts_.syncCursor = setTimeout(function() {
2917 self.syncCursorPosition_();
2918 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002919 }, 0);
2920};
2921
rgindacc2996c2012-02-24 14:59:31 -08002922/**
rgindaf522ce02012-04-17 17:49:17 -07002923 * Show or hide the zoom warning.
2924 *
2925 * The zoom warning is a message warning the user that their browser zoom must
2926 * be set to 100% in order for hterm to function properly.
2927 *
2928 * @param {boolean} state True to show the message, false to hide it.
2929 */
2930hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2931 if (!this.zoomWarningNode_) {
2932 if (!state)
2933 return;
2934
2935 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002936 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07002937 this.zoomWarningNode_.style.cssText = (
2938 'color: black;' +
2939 'background-color: #ff2222;' +
2940 'font-size: large;' +
2941 'border-radius: 8px;' +
2942 'opacity: 0.75;' +
2943 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2944 'top: 0.5em;' +
2945 'right: 1.2em;' +
2946 'position: absolute;' +
2947 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002948 '-webkit-user-select: none;' +
2949 '-moz-text-size-adjust: none;' +
2950 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05002951
2952 this.zoomWarningNode_.addEventListener('click', function(e) {
2953 this.parentNode.removeChild(this);
2954 });
rgindaf522ce02012-04-17 17:49:17 -07002955 }
2956
Robert Gindab4839c22013-02-28 16:52:10 -08002957 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2958 hterm.zoomWarningMessage,
2959 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2960
rgindaf522ce02012-04-17 17:49:17 -07002961 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2962
2963 if (state) {
2964 if (!this.zoomWarningNode_.parentNode)
2965 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2966 } else if (this.zoomWarningNode_.parentNode) {
2967 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2968 }
2969};
2970
2971/**
rgindacc2996c2012-02-24 14:59:31 -08002972 * Show the terminal overlay for a given amount of time.
2973 *
2974 * The terminal overlay appears in inverse video in a large font, centered
2975 * over the terminal. You should probably keep the overlay message brief,
2976 * since it's in a large font and you probably aren't going to check the size
2977 * of the terminal first.
2978 *
2979 * @param {string} msg The text (not HTML) message to display in the overlay.
2980 * @param {number} opt_timeout The amount of time to wait before fading out
2981 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2982 * stay up forever (or until the next overlay).
2983 */
2984hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002985 if (!this.overlayNode_) {
2986 if (!this.div_)
2987 return;
2988
2989 this.overlayNode_ = this.document_.createElement('div');
2990 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002991 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002992 'font-size: xx-large;' +
2993 'opacity: 0.75;' +
2994 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2995 'position: absolute;' +
2996 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002997 '-webkit-transition: opacity 180ms ease-in;' +
2998 '-moz-user-select: none;' +
2999 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08003000
3001 this.overlayNode_.addEventListener('mousedown', function(e) {
3002 e.preventDefault();
3003 e.stopPropagation();
3004 }, true);
rgindaf0090c92012-02-10 14:58:52 -08003005 }
3006
rginda9f5222b2012-03-05 11:53:28 -08003007 this.overlayNode_.style.color = this.prefs_.get('background-color');
3008 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
3009 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
3010
rgindaf0090c92012-02-10 14:58:52 -08003011 this.overlayNode_.textContent = msg;
3012 this.overlayNode_.style.opacity = '0.75';
3013
3014 if (!this.overlayNode_.parentNode)
3015 this.div_.appendChild(this.overlayNode_);
3016
Robert Ginda97769282013-02-01 15:30:30 -08003017 var divSize = hterm.getClientSize(this.div_);
3018 var overlaySize = hterm.getClientSize(this.overlayNode_);
3019
Robert Ginda8a59f762014-07-23 11:29:55 -07003020 this.overlayNode_.style.top =
3021 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08003022 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07003023 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08003024
rgindaf0090c92012-02-10 14:58:52 -08003025 if (this.overlayTimeout_)
3026 clearTimeout(this.overlayTimeout_);
3027
Raymes Khouryc7a06382018-07-04 10:25:45 +10003028 this.accessibilityReader_.assertiveAnnounce(msg);
3029
rgindacc2996c2012-02-24 14:59:31 -08003030 if (opt_timeout === null)
3031 return;
3032
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003033 this.overlayTimeout_ = setTimeout(() => {
3034 this.overlayNode_.style.opacity = '0';
3035 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
3036 }, opt_timeout || 1500);
3037};
3038
3039/**
3040 * Hide the terminal overlay immediately.
3041 *
3042 * Useful when we show an overlay for an event with an unknown end time.
3043 */
3044hterm.Terminal.prototype.hideOverlay = function() {
3045 if (this.overlayTimeout_)
3046 clearTimeout(this.overlayTimeout_);
3047 this.overlayTimeout_ = null;
3048
3049 if (this.overlayNode_.parentNode)
3050 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
3051 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08003052};
3053
rginda4bba5e12012-06-20 16:15:30 -07003054/**
3055 * Paste from the system clipboard to the terminal.
3056 */
3057hterm.Terminal.prototype.paste = function() {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003058 return hterm.pasteFromClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07003059};
3060
3061/**
3062 * Copy a string to the system clipboard.
3063 *
3064 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05003065 *
3066 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07003067 */
3068hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08003069 if (this.prefs_.get('enable-clipboard-notice'))
3070 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
3071
rgindaa09e7332012-08-17 12:49:51 -07003072 var copySource = this.document_.createElement('pre');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04003073 copySource.id = 'hterm:copy-to-clipboard-source';
rginda4bba5e12012-06-20 16:15:30 -07003074 copySource.textContent = str;
3075 copySource.style.cssText = (
3076 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003077 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07003078 'position: absolute;' +
3079 'top: -99px');
3080
3081 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07003082
rginda4bba5e12012-06-20 16:15:30 -07003083 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07003084 var anchorNode = selection.anchorNode;
3085 var anchorOffset = selection.anchorOffset;
3086 var focusNode = selection.focusNode;
3087 var focusOffset = selection.focusOffset;
3088
Mike Frysinger4968d492018-09-28 23:44:31 -04003089 // FF sometimes throws NS_ERROR_FAILURE exceptions when we make this call.
3090 // Catch it because a failure here leaks the copySource node.
3091 // https://bugzilla.mozilla.org/show_bug.cgi?id=1178676
3092 try {
3093 selection.selectAllChildren(copySource);
3094 } catch (ex) {}
rginda4bba5e12012-06-20 16:15:30 -07003095
rgindaa09e7332012-08-17 12:49:51 -07003096 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07003097
Rob Spies56953412014-04-28 14:09:47 -07003098 // IE doesn't support selection.extend. This means that the selection
3099 // won't return on IE.
Robert Ginda6f4f0aa2014-07-28 10:21:39 -07003100 if (selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07003101 selection.collapse(anchorNode, anchorOffset);
3102 selection.extend(focusNode, focusOffset);
3103 }
rgindafaa74742012-08-21 13:34:03 -07003104
rginda4bba5e12012-06-20 16:15:30 -07003105 copySource.parentNode.removeChild(copySource);
3106};
3107
Evan Jones2600d4f2016-12-06 09:29:36 -05003108/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003109 * Display an image.
3110 *
3111 * @param {Object} options The image to display.
3112 * @param {string=} options.name A human readable string for the image.
3113 * @param {string|number=} options.size The size (in bytes).
3114 * @param {boolean=} options.preserveAspectRatio Whether to preserve aspect.
3115 * @param {boolean=} options.inline Whether to display the image inline.
3116 * @param {string|number=} options.width The width of the image.
3117 * @param {string|number=} options.height The height of the image.
3118 * @param {string=} options.align Direction to align the image.
3119 * @param {string} options.uri The source URI for the image.
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003120 * @param {function=} onLoad Callback when loading finishes.
3121 * @param {function(Event)=} onError Callback when loading fails.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003122 */
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003123hterm.Terminal.prototype.displayImage = function(options, onLoad, onError) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003124 // Make sure we're actually given a resource to display.
3125 if (options.uri === undefined)
3126 return;
3127
3128 // Set up the defaults to simplify code below.
3129 if (!options.name)
3130 options.name = '';
3131
3132 // Has the user approved image display yet?
3133 if (this.allowImagesInline !== true) {
3134 this.newLine();
3135 const row = this.getRowNode(this.scrollbackRows_.length +
3136 this.getCursorRow() - 1);
3137
3138 if (this.allowImagesInline === false) {
3139 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
3140 'Inline Images Disabled');
3141 return;
3142 }
3143
3144 // Show a prompt.
3145 let button;
3146 const span = this.document_.createElement('span');
3147 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
3148 span.style.fontWeight = 'bold';
3149 span.style.borderWidth = '1px';
3150 span.style.borderStyle = 'dashed';
3151 button = this.document_.createElement('span');
3152 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3153 button.style.marginLeft = '1em';
3154 button.style.borderWidth = '1px';
3155 button.style.borderStyle = 'solid';
3156 button.addEventListener('click', () => {
3157 this.prefs_.set('allow-images-inline', false);
3158 });
3159 span.appendChild(button);
3160 button = this.document_.createElement('span');
3161 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3162 'allow this session');
3163 button.style.marginLeft = '1em';
3164 button.style.borderWidth = '1px';
3165 button.style.borderStyle = 'solid';
3166 button.addEventListener('click', () => {
3167 this.allowImagesInline = true;
3168 });
3169 span.appendChild(button);
3170 button = this.document_.createElement('span');
3171 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3172 button.style.marginLeft = '1em';
3173 button.style.borderWidth = '1px';
3174 button.style.borderStyle = 'solid';
3175 button.addEventListener('click', () => {
3176 this.prefs_.set('allow-images-inline', true);
3177 });
3178 span.appendChild(button);
3179
3180 row.appendChild(span);
3181 return;
3182 }
3183
3184 // See if we should show this object directly, or download it.
3185 if (options.inline) {
3186 const io = this.io.push();
3187 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
3188 'Loading $1 ...'), null);
3189
3190 // While we're loading the image, eat all the user's input.
3191 io.onVTKeystroke = io.sendString = () => {};
3192
3193 // Initialize this new image.
Adrián Pérez-Orozco6a550322018-08-31 14:36:06 -07003194 const img =
3195 /** @type {!HTMLImageElement} */ (this.document_.createElement('img'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003196 img.src = options.uri;
3197 img.title = img.alt = options.name;
3198
3199 // Attach the image to the page to let it load/render. It won't stay here.
3200 // This is needed so it's visible and the DOM can calculate the height. If
3201 // the image is hidden or not in the DOM, the height is always 0.
3202 this.document_.body.appendChild(img);
3203
3204 // Wait for the image to finish loading before we try moving it to the
3205 // right place in the terminal.
3206 img.onload = () => {
3207 // Now that we have the image dimensions, figure out how to show it.
3208 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
3209 img.style.maxWidth = `${this.document_.body.clientWidth}px`;
3210 img.style.maxHeight = `${this.document_.body.clientHeight}px`;
3211
3212 // Parse a width/height specification.
3213 const parseDim = (dim, maxDim, cssVar) => {
3214 if (!dim || dim == 'auto')
3215 return '';
3216
3217 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3218 if (ary) {
3219 if (ary[2] == '%')
3220 return maxDim * parseInt(ary[1]) / 100 + 'px';
3221 else if (ary[2] == 'px')
3222 return dim;
3223 else
3224 return `calc(${dim} * var(${cssVar}))`;
3225 }
3226
3227 return '';
3228 };
3229 img.style.width =
3230 parseDim(options.width, this.document_.body.clientWidth,
3231 '--hterm-charsize-width');
3232 img.style.height =
3233 parseDim(options.height, this.document_.body.clientHeight,
3234 '--hterm-charsize-height');
3235
3236 // Figure out how many rows the image occupies, then add that many.
3237 // XXX: This count will be inaccurate if the font size changes on us.
3238 const padRows = Math.ceil(img.clientHeight /
3239 this.scrollPort_.characterSize.height);
3240 for (let i = 0; i < padRows; ++i)
3241 this.newLine();
3242
3243 // Update the max height in case the user shrinks the character size.
3244 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3245
3246 // Move the image to the last row. This way when we scroll up, it doesn't
3247 // disappear when the first row gets clipped. It will disappear when we
3248 // scroll down and the last row is clipped ...
3249 this.document_.body.removeChild(img);
3250 // Create a wrapper node so we can do an absolute in a relative position.
3251 // This helps with rounding errors between JS & CSS counts.
3252 const div = this.document_.createElement('div');
3253 div.style.position = 'relative';
3254 div.style.textAlign = options.align;
3255 img.style.position = 'absolute';
3256 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3257 div.appendChild(img);
3258 const row = this.getRowNode(this.scrollbackRows_.length +
3259 this.getCursorRow() - 1);
3260 row.appendChild(div);
3261
3262 io.hideOverlay();
3263 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003264
3265 if (onLoad)
3266 onLoad();
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003267 };
3268
3269 // If we got a malformed image, give up.
3270 img.onerror = (e) => {
3271 this.document_.body.removeChild(img);
3272 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
Mike Frysingere14a8c42018-03-10 00:17:30 -08003273 'Loading $1 failed'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003274 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003275
3276 if (onError)
3277 onError(e);
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003278 };
3279 } else {
3280 // We can't use chrome.downloads.download as that requires "downloads"
3281 // permissions, and that works only in extensions, not apps.
3282 const a = this.document_.createElement('a');
3283 a.href = options.uri;
3284 a.download = options.name;
3285 this.document_.body.appendChild(a);
3286 a.click();
3287 a.remove();
3288 }
3289};
3290
3291/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003292 * Returns the selected text, or null if no text is selected.
3293 *
3294 * @return {string|null}
3295 */
rgindaa09e7332012-08-17 12:49:51 -07003296hterm.Terminal.prototype.getSelectionText = function() {
3297 var selection = this.scrollPort_.selection;
3298 selection.sync();
3299
3300 if (selection.isCollapsed)
3301 return null;
3302
rgindaa09e7332012-08-17 12:49:51 -07003303 // Start offset measures from the beginning of the line.
3304 var startOffset = selection.startOffset;
3305 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003306
Raymes Khoury334625a2018-06-25 10:29:40 +10003307 // If an x-row isn't selected, |node| will be null.
3308 if (!node)
3309 return null;
3310
Robert Gindafdbb3f22012-09-06 20:23:06 -07003311 if (node.nodeName != 'X-ROW') {
3312 // If the selection doesn't start on an x-row node, then it must be
3313 // somewhere inside the x-row. Add any characters from previous siblings
3314 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003315
3316 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3317 // If node is the text node in a styled span, move up to the span node.
3318 node = node.parentNode;
3319 }
3320
Robert Gindafdbb3f22012-09-06 20:23:06 -07003321 while (node.previousSibling) {
3322 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003323 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003324 }
rgindaa09e7332012-08-17 12:49:51 -07003325 }
3326
3327 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08003328 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
3329 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003330 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003331
Robert Gindafdbb3f22012-09-06 20:23:06 -07003332 if (node.nodeName != 'X-ROW') {
3333 // If the selection doesn't end on an x-row node, then it must be
3334 // somewhere inside the x-row. Add any characters from following siblings
3335 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003336
3337 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3338 // If node is the text node in a styled span, move up to the span node.
3339 node = node.parentNode;
3340 }
3341
Robert Gindafdbb3f22012-09-06 20:23:06 -07003342 while (node.nextSibling) {
3343 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003344 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003345 }
rgindaa09e7332012-08-17 12:49:51 -07003346 }
3347
3348 var rv = this.getRowsText(selection.startRow.rowIndex,
3349 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003350 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003351};
3352
rginda4bba5e12012-06-20 16:15:30 -07003353/**
3354 * Copy the current selection to the system clipboard, then clear it after a
3355 * short delay.
3356 */
3357hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07003358 var text = this.getSelectionText();
3359 if (text != null)
3360 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07003361};
3362
rgindaf0090c92012-02-10 14:58:52 -08003363hterm.Terminal.prototype.overlaySize = function() {
3364 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
3365};
3366
rginda87b86462011-12-14 13:48:03 -08003367/**
3368 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3369 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003370 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003371 */
3372hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08003373 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08003374 this.scrollPort_.scrollRowToBottom(this.getRowCount());
3375
Robert Ginda8cb7d902013-06-20 14:37:18 -07003376 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08003377};
3378
3379/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003380 * Open the selected url.
3381 */
3382hterm.Terminal.prototype.openSelectedUrl_ = function() {
3383 var str = this.getSelectionText();
3384
3385 // If there is no selection, try and expand wherever they clicked.
3386 if (str == null) {
John Lincae9b732018-03-08 13:56:35 +08003387 this.screen_.expandSelectionForUrl(this.document_.getSelection());
Mike Frysinger70b94692017-01-26 18:57:50 -10003388 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003389
3390 // If clicking in empty space, return.
3391 if (str == null)
3392 return;
Mike Frysinger70b94692017-01-26 18:57:50 -10003393 }
3394
3395 // Make sure URL is valid before opening.
3396 if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0)
3397 return;
Mike Frysinger43472622017-06-26 18:11:07 -04003398
3399 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003400 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003401 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3402 // We have to whitelist a few protocols that lack authorities and thus
3403 // never use the //. Like mailto.
3404 switch (str.split(':', 1)[0]) {
3405 case 'mailto':
3406 break;
3407 default:
3408 str = 'http://' + str;
3409 break;
3410 }
3411 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003412
Mike Frysinger720fa832017-10-23 01:15:52 -04003413 hterm.openUrl(str);
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003414};
Mike Frysinger70b94692017-01-26 18:57:50 -10003415
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003416/**
3417 * Manage the automatic mouse hiding behavior while typing.
3418 *
3419 * @param {boolean=} v Whether to enable automatic hiding.
3420 */
3421hterm.Terminal.prototype.setAutomaticMouseHiding = function(v=null) {
3422 // Since Chrome OS & macOS do this by default everywhere, we don't need to.
3423 // Linux & Windows seem to leave this to specific applications to manage.
3424 if (v === null)
3425 v = (hterm.os != 'cros' && hterm.os != 'mac');
3426
3427 this.mouseHideWhileTyping_ = !!v;
3428};
3429
3430/**
3431 * Handler for monitoring user keyboard activity.
3432 *
3433 * This isn't for processing the keystrokes directly, but for updating any
3434 * state that might toggle based on the user using the keyboard at all.
3435 *
3436 * @param {KeyboardEvent} e The keyboard event that triggered us.
3437 */
3438hterm.Terminal.prototype.onKeyboardActivity_ = function(e) {
3439 // When the user starts typing, hide the mouse cursor.
3440 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_)
3441 this.setCssVar('mouse-cursor-style', 'none');
3442};
Mike Frysinger70b94692017-01-26 18:57:50 -10003443
3444/**
rgindad5613292012-06-19 15:40:37 -07003445 * Add the terminalRow and terminalColumn properties to mouse events and
3446 * then forward on to onMouse().
3447 *
3448 * The terminalRow and terminalColumn properties contain the (row, column)
3449 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003450 *
3451 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003452 */
3453hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003454 if (e.processedByTerminalHandler_) {
3455 // We register our event handlers on the document, as well as the cursor
3456 // and the scroll blocker. Mouse events that occur on the cursor or
3457 // scroll blocker will also appear on the document, but we don't want to
3458 // process them twice.
3459 //
3460 // We can't just prevent bubbling because that has other side effects, so
3461 // we decorate the event object with this property instead.
3462 return;
3463 }
3464
Mike Frysinger468966c2018-08-28 13:48:51 -04003465 // Consume navigation events. Button 3 is usually "browser back" and
3466 // button 4 is "browser forward" which we don't want to happen.
3467 if (e.button > 2) {
3468 e.preventDefault();
3469 // We don't return so click events can be passed to the remote below.
3470 }
3471
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003472 var reportMouseEvents = (!this.defeatMouseReports_ &&
3473 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3474
rgindafaa74742012-08-21 13:34:03 -07003475 e.processedByTerminalHandler_ = true;
3476
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003477 // Handle auto hiding of mouse cursor while typing.
3478 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
3479 // Make sure the mouse cursor is visible.
3480 this.syncMouseStyle();
3481 // This debounce isn't perfect, but should work well enough for such a
3482 // simple implementation. If the user moved the mouse, we enabled this
3483 // debounce, and then moved the mouse just before the timeout, we wouldn't
3484 // debounce that later movement.
3485 this.mouseHideDelay_ = setTimeout(() => this.mouseHideDelay_ = null, 1000);
3486 }
3487
Robert Gindaeda48db2014-07-17 09:25:30 -07003488 // One based row/column stored on the mouse event.
3489 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
3490 this.scrollPort_.characterSize.height) + 1;
3491 e.terminalColumn = parseInt(e.clientX /
3492 this.scrollPort_.characterSize.width) + 1;
3493
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003494 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
3495 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07003496 return;
3497 }
3498
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003499 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07003500 // If the cursor is visible and we're not sending mouse events to the
3501 // host app, then we want to hide the terminal cursor when the mouse
3502 // cursor is over top. This keeps the terminal cursor from interfering
3503 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003504 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3505 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3506 this.cursorNode_.style.display = 'none';
3507 } else if (this.cursorNode_.style.display == 'none') {
3508 this.cursorNode_.style.display = '';
3509 }
3510 }
rgindad5613292012-06-19 15:40:37 -07003511
Robert Ginda928cf632014-03-05 15:07:41 -08003512 if (e.type == 'mousedown') {
Mike Frysingercc114512017-09-11 21:39:17 -04003513 this.contextMenu.hide(e);
3514
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003515 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003516 // If VT mouse reporting is disabled, or has been defeated with
3517 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003518 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003519 this.setSelectionEnabled(true);
3520 } else {
3521 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003522 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003523 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003524 this.setSelectionEnabled(false);
3525 e.preventDefault();
3526 }
3527 }
3528
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003529 if (!reportMouseEvents) {
John Lin2aad22e2018-03-16 13:58:11 +08003530 if (e.type == 'dblclick') {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003531 this.screen_.expandSelection(this.document_.getSelection());
John Lin2aad22e2018-03-16 13:58:11 +08003532 if (this.copyOnSelect)
3533 this.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07003534 }
3535
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003536 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003537 // Debounce this event with the dblclick event. If you try to doubleclick
3538 // a URL to open it, Chrome will fire click then dblclick, but we won't
3539 // have expanded the selection text at the first click event.
3540 clearTimeout(this.timeouts_.openUrl);
3541 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3542 500);
3543 return;
3544 }
3545
Mike Frysinger847577f2017-05-23 23:25:57 -04003546 if (e.type == 'mousedown') {
Mike Frysingercc114512017-09-11 21:39:17 -04003547 if (e.ctrlKey && e.button == 2 /* right button */) {
3548 e.preventDefault();
3549 this.contextMenu.show(e, this);
3550 } else if (e.button == this.mousePasteButton ||
3551 (this.mouseRightClickPaste && e.button == 2 /* right button */)) {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003552 if (!this.paste())
Mike Frysinger05a57f02017-08-27 17:48:55 -04003553 console.warn('Could not paste manually due to web restrictions');
Mike Frysinger847577f2017-05-23 23:25:57 -04003554 }
3555 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003556
Mike Frysinger2edd3612017-05-24 00:54:39 -04003557 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003558 !this.document_.getSelection().isCollapsed) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003559 this.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003560 }
3561
3562 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3563 this.scrollBlockerNode_.engaged) {
3564 // Disengage the scroll-blocker after one of these events.
3565 this.scrollBlockerNode_.engaged = false;
3566 this.scrollBlockerNode_.style.top = '-99px';
3567 }
3568
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003569 // Emulate arrow key presses via scroll wheel events.
3570 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3571 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003572 if (e.type == 'wheel') {
Mike Frysinger321063c2018-08-29 15:33:14 -04003573 const delta = this.scrollPort_.scrollWheelDelta(e);
Mike Frysingerc3030a82017-05-29 14:16:11 -04003574
Mike Frysinger321063c2018-08-29 15:33:14 -04003575 // Helper to turn a wheel event delta into a series of key presses.
3576 const deltaToArrows = (distance, charSize, arrowPos, arrowNeg) => {
3577 if (distance == 0) {
3578 return '';
3579 }
3580
3581 // Convert the scroll distance into a number of rows/cols.
3582 const cells = lib.f.smartFloorDivide(Math.abs(distance), charSize);
3583 const data = '\x1bO' + (distance < 0 ? arrowNeg : arrowPos);
3584 return data.repeat(cells);
3585 };
3586
3587 // The order between up/down and left/right doesn't really matter.
3588 this.io.sendString(
3589 // Up/down arrow keys.
3590 deltaToArrows(delta.y, this.scrollPort_.characterSize.height,
3591 'A', 'B') +
3592 // Left/right arrow keys.
3593 deltaToArrows(delta.x, this.scrollPort_.characterSize.width,
3594 'C', 'D')
3595 );
Mike Frysingerc3030a82017-05-29 14:16:11 -04003596
3597 e.preventDefault();
3598 }
3599 }
Robert Ginda928cf632014-03-05 15:07:41 -08003600 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003601 if (!this.scrollBlockerNode_.engaged) {
3602 if (e.type == 'mousedown') {
3603 // Move the scroll-blocker into place if we want to keep the scrollport
3604 // from scrolling.
3605 this.scrollBlockerNode_.engaged = true;
3606 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3607 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3608 } else if (e.type == 'mousemove') {
3609 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3610 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003611 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003612 e.preventDefault();
3613 }
3614 }
Robert Ginda928cf632014-03-05 15:07:41 -08003615
3616 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003617 }
3618
Robert Ginda928cf632014-03-05 15:07:41 -08003619 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3620 // Restore this on mouseup in case it was temporarily defeated with a
3621 // alt-mousedown. Only do this when the selection is empty so that
3622 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003623 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003624 }
rgindad5613292012-06-19 15:40:37 -07003625};
3626
3627/**
3628 * Clients should override this if they care to know about mouse events.
3629 *
3630 * The event parameter will be a normal DOM mouse click event with additional
3631 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003632 *
3633 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003634 */
3635hterm.Terminal.prototype.onMouse = function(e) { };
3636
3637/**
rginda8e92a692012-05-20 19:37:20 -07003638 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003639 *
3640 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003641 */
Rob Spies06533ba2014-04-24 11:20:37 -07003642hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3643 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003644 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003645
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003646 if (this.reportFocus)
3647 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O');
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003648
Michael Kelly485ecd12014-06-09 11:41:56 -04003649 if (focused === true)
3650 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07003651};
3652
3653/**
rginda8ba33642011-12-14 12:31:31 -08003654 * React when the ScrollPort is scrolled.
3655 */
3656hterm.Terminal.prototype.onScroll_ = function() {
3657 this.scheduleSyncCursorPosition_();
3658};
3659
3660/**
rginda9846e2f2012-01-27 13:53:33 -08003661 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003662 *
3663 * @param {Event} e The DOM paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08003664 */
3665hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaa063b202014-07-21 11:08:25 -07003666 var data = e.text.replace(/\n/mg, '\r');
Connor Hegartyf525ceb2014-09-03 13:36:03 -07003667 data = this.keyboard.encode(data);
Mike Frysingere8c32c82018-03-11 14:57:28 -07003668 if (this.options_.bracketedPaste) {
3669 // We strip out most escape sequences as they can cause issues (like
3670 // inserting an \x1b[201~ midstream). We pass through whitespace
3671 // though: 0x08:\b 0x09:\t 0x0a:\n 0x0d:\r.
3672 // This matches xterm behavior.
3673 const filter = (data) => data.replace(/[\x00-\x07\x0b-\x0c\x0e-\x1f]/g, '');
3674 data = '\x1b[200~' + filter(data) + '\x1b[201~';
3675 }
Robert Gindaa063b202014-07-21 11:08:25 -07003676
3677 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003678};
3679
3680/**
rgindaa09e7332012-08-17 12:49:51 -07003681 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003682 *
3683 * @param {Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07003684 */
3685hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07003686 if (!this.useDefaultWindowCopy) {
3687 e.preventDefault();
3688 setTimeout(this.copySelectionToClipboard.bind(this), 0);
3689 }
rgindaa09e7332012-08-17 12:49:51 -07003690};
3691
3692/**
rginda8ba33642011-12-14 12:31:31 -08003693 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08003694 *
3695 * Note: This function should not directly contain code that alters the internal
3696 * state of the terminal. That kind of code belongs in realizeWidth or
3697 * realizeHeight, so that it can be executed synchronously in the case of a
3698 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08003699 */
3700hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08003701 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07003702 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08003703 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07003704 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08003705
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003706 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08003707 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003708 // gets removed from the document or during the initial load, and we can't
3709 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07003710 // This can also happen if called before the scrollPort calculates the
3711 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08003712 return;
3713 }
3714
rgindaa8ba17d2012-08-15 14:41:10 -07003715 var isNewSize = (columnCount != this.screenSize.width ||
3716 rowCount != this.screenSize.height);
3717
3718 // We do this even if the size didn't change, just to be sure everything is
3719 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04003720 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07003721 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07003722
3723 if (isNewSize)
3724 this.overlaySize();
3725
Robert Gindafb1be6a2013-12-11 11:56:22 -08003726 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07003727 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08003728};
3729
3730/**
3731 * Service the cursor blink timeout.
3732 */
3733hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07003734 if (!this.options_.cursorBlink) {
3735 delete this.timeouts_.cursorBlink;
3736 return;
3737 }
3738
Robert Ginda830583c2013-08-07 13:20:46 -07003739 if (this.cursorNode_.getAttribute('focus') == 'false' ||
3740 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08003741 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07003742 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3743 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08003744 } else {
rginda87b86462011-12-14 13:48:03 -08003745 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07003746 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3747 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08003748 }
3749};
David Reveman8f552492012-03-28 12:18:41 -04003750
3751/**
3752 * Set the scrollbar-visible mode bit.
3753 *
3754 * If scrollbar-visible is on, the vertical scrollbar will be visible.
3755 * Otherwise it will not.
3756 *
3757 * Defaults to on.
3758 *
3759 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
3760 */
3761hterm.Terminal.prototype.setScrollbarVisible = function(state) {
3762 this.scrollPort_.setScrollbarVisible(state);
3763};
Michael Kelly485ecd12014-06-09 11:41:56 -04003764
3765/**
Rob Spies49039e52014-12-17 13:40:04 -08003766 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04003767 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08003768 *
3769 * Defaults to 1.
3770 *
Evan Jones2600d4f2016-12-06 09:29:36 -05003771 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08003772 */
3773hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3774 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3775};
3776
3777/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003778 * Close all web notifications created by terminal bells.
3779 */
3780hterm.Terminal.prototype.closeBellNotifications_ = function() {
3781 this.bellNotificationList_.forEach(function(n) {
3782 n.close();
3783 });
3784 this.bellNotificationList_.length = 0;
3785};
Raymes Khourye5d48982018-08-02 09:08:32 +10003786
3787/**
3788 * Syncs the cursor position when the scrollport gains focus.
3789 */
3790hterm.Terminal.prototype.onScrollportFocus_ = function() {
3791 // If the cursor is offscreen we set selection to the last row on the screen.
3792 const topRowIndex = this.scrollPort_.getTopRowIndex();
3793 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
3794 const selection = this.document_.getSelection();
3795 if (!this.syncCursorPosition_() && selection) {
3796 selection.collapse(this.getRowNode(bottomRowIndex));
3797 }
3798};