blob: 063bd60e6490919ed501a1d97977917ea245fcb5 [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
Michael Kelly485ecd12014-06-09 11:41:56 -0400116 // All terminal bell notifications that have been generated (not necessarily
117 // shown).
118 this.bellNotificationList_ = [];
119
120 // Whether we have permission to display notifications.
121 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400122
rginda6d397402012-01-17 10:58:29 -0800123 // Cursor position and attributes saved with DECSC.
124 this.savedOptions_ = {};
125
rginda8ba33642011-12-14 12:31:31 -0800126 // The current mode bits for the terminal.
127 this.options_ = new hterm.Options();
128
129 // Timeouts we might need to clear.
130 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800131
132 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800133 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800134
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800135 this.saveCursorAndState(true);
136
Zhu Qunying30d40712017-03-14 16:27:00 -0700137 // The keyboard handler.
rgindafeaf3142012-01-31 15:14:20 -0800138 this.keyboard = new hterm.Keyboard(this);
139
rginda87b86462011-12-14 13:48:03 -0800140 // General IO interface that can be given to third parties without exposing
141 // the entire terminal object.
142 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800143
rgindad5613292012-06-19 15:40:37 -0700144 // True if mouse-click-drag should scroll the terminal.
145 this.enableMouseDragScroll = true;
146
Robert Ginda57f03b42012-09-13 11:02:48 -0700147 this.copyOnSelect = null;
Mike Frysinger847577f2017-05-23 23:25:57 -0400148 this.mouseRightClickPaste = null;
rginda4bba5e12012-06-20 16:15:30 -0700149 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700150
Zhu Qunying30d40712017-03-14 16:27:00 -0700151 // Whether to use the default window copy behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700152 this.useDefaultWindowCopy = false;
153
154 this.clearSelectionAfterCopy = true;
155
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400156 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800157 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700158
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400159 // Whether we allow images to be shown.
160 this.allowImagesInline = null;
161
Gabriel Holodake8a09be2017-10-10 01:07:11 -0400162 this.reportFocus = false;
163
Robert Ginda57f03b42012-09-13 11:02:48 -0700164 this.setProfile(opt_profileId || 'default',
Evan Jones5f9df812016-12-06 09:38:58 -0500165 function() { this.onTerminalReady(); }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800166};
167
168/**
Robert Ginda830583c2013-08-07 13:20:46 -0700169 * Possible cursor shapes.
170 */
171hterm.Terminal.cursorShape = {
172 BLOCK: 'BLOCK',
173 BEAM: 'BEAM',
174 UNDERLINE: 'UNDERLINE'
175};
176
177/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700178 * Clients should override this to be notified when the terminal is ready
179 * for use.
180 *
181 * The terminal initialization is asynchronous, and shouldn't be used before
182 * this method is called.
183 */
184hterm.Terminal.prototype.onTerminalReady = function() { };
185
186/**
rginda35c456b2012-02-09 17:29:05 -0800187 * Default tab with of 8 to match xterm.
188 */
189hterm.Terminal.prototype.tabWidth = 8;
190
191/**
rginda9f5222b2012-03-05 11:53:28 -0800192 * Select a preference profile.
193 *
194 * This will load the terminal preferences for the given profile name and
195 * associate subsequent preference changes with the new preference profile.
196 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500197 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800198 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700199 * @param {function} opt_callback Optional callback to invoke when the profile
200 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800201 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700202hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
203 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800204
Robert Ginda57f03b42012-09-13 11:02:48 -0700205 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800206
Robert Ginda57f03b42012-09-13 11:02:48 -0700207 if (this.prefs_)
208 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800209
Robert Ginda57f03b42012-09-13 11:02:48 -0700210 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
211 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800212 'alt-gr-mode': function(v) {
213 if (v == null) {
214 if (navigator.language.toLowerCase() == 'en-us') {
215 v = 'none';
216 } else {
217 v = 'right-alt';
218 }
219 } else if (typeof v == 'string') {
220 v = v.toLowerCase();
221 } else {
222 v = 'none';
223 }
224
225 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v))
226 v = 'none';
227
228 terminal.keyboard.altGrMode = v;
229 },
230
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700231 'alt-backspace-is-meta-backspace': function(v) {
232 terminal.keyboard.altBackspaceIsMetaBackspace = v;
233 },
234
Robert Ginda57f03b42012-09-13 11:02:48 -0700235 'alt-is-meta': function(v) {
236 terminal.keyboard.altIsMeta = v;
237 },
238
239 'alt-sends-what': function(v) {
240 if (!/^(escape|8-bit|browser-key)$/.test(v))
241 v = 'escape';
242
243 terminal.keyboard.altSendsWhat = v;
244 },
245
246 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800247 var ary = v.match(/^lib-resource:(\S+)/);
248 if (ary) {
249 terminal.bellAudio_.setAttribute('src',
250 lib.resource.getDataUrl(ary[1]));
251 } else {
252 terminal.bellAudio_.setAttribute('src', v);
253 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700254 },
255
Michael Kelly485ecd12014-06-09 11:41:56 -0400256 'desktop-notification-bell': function(v) {
257 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700258 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400259 Notification.permission === 'granted';
260 if (!terminal.desktopNotificationBell_) {
261 // Note: We don't call Notification.requestPermission here because
262 // Chrome requires the call be the result of a user action (such as an
263 // onclick handler), and pref listeners are run asynchronously.
264 //
265 // A way of working around this would be to display a dialog in the
266 // terminal with a "click-to-request-permission" button.
267 console.warn('desktop-notification-bell is true but we do not have ' +
268 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400269 }
270 } else {
271 terminal.desktopNotificationBell_ = false;
272 }
273 },
274
Robert Ginda57f03b42012-09-13 11:02:48 -0700275 'background-color': function(v) {
276 terminal.setBackgroundColor(v);
277 },
278
279 'background-image': function(v) {
280 terminal.scrollPort_.setBackgroundImage(v);
281 },
282
283 'background-size': function(v) {
284 terminal.scrollPort_.setBackgroundSize(v);
285 },
286
287 'background-position': function(v) {
288 terminal.scrollPort_.setBackgroundPosition(v);
289 },
290
291 'backspace-sends-backspace': function(v) {
292 terminal.keyboard.backspaceSendsBackspace = v;
293 },
294
Brad Town18654b62015-03-12 00:27:45 -0700295 'character-map-overrides': function(v) {
296 if (!(v == null || v instanceof Object)) {
297 console.warn('Preference character-map-modifications is not an ' +
298 'object: ' + v);
299 return;
300 }
301
Mike Frysinger095d4062017-06-14 00:29:48 -0700302 terminal.vt.characterMaps.reset();
303 terminal.vt.characterMaps.setOverrides(v);
Brad Town18654b62015-03-12 00:27:45 -0700304 },
305
Robert Ginda57f03b42012-09-13 11:02:48 -0700306 'cursor-blink': function(v) {
307 terminal.setCursorBlink(!!v);
308 },
309
Robert Gindaea2183e2014-07-17 09:51:51 -0700310 'cursor-blink-cycle': function(v) {
311 if (v instanceof Array &&
312 typeof v[0] == 'number' &&
313 typeof v[1] == 'number') {
314 terminal.cursorBlinkCycle_ = v;
315 } else if (typeof v == 'number') {
316 terminal.cursorBlinkCycle_ = [v, v];
317 } else {
318 // Fast blink indicates an error.
319 terminal.cursorBlinkCycle_ = [100, 100];
320 }
321 },
322
Robert Ginda57f03b42012-09-13 11:02:48 -0700323 'cursor-color': function(v) {
324 terminal.setCursorColor(v);
325 },
326
327 'color-palette-overrides': function(v) {
328 if (!(v == null || v instanceof Object || v instanceof Array)) {
329 console.warn('Preference color-palette-overrides is not an array or ' +
330 'object: ' + v);
331 return;
rginda9f5222b2012-03-05 11:53:28 -0800332 }
rginda9f5222b2012-03-05 11:53:28 -0800333
Robert Ginda57f03b42012-09-13 11:02:48 -0700334 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700335
Robert Ginda57f03b42012-09-13 11:02:48 -0700336 if (v) {
337 for (var key in v) {
338 var i = parseInt(key);
339 if (isNaN(i) || i < 0 || i > 255) {
340 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
341 continue;
342 }
343
344 if (v[i]) {
345 var rgb = lib.colors.normalizeCSS(v[i]);
346 if (rgb)
347 lib.colors.colorPalette[i] = rgb;
348 }
349 }
rginda30f20f62012-04-05 16:36:19 -0700350 }
rginda30f20f62012-04-05 16:36:19 -0700351
Evan Jones5f9df812016-12-06 09:38:58 -0500352 terminal.primaryScreen_.textAttributes.resetColorPalette();
Robert Ginda57f03b42012-09-13 11:02:48 -0700353 terminal.alternateScreen_.textAttributes.resetColorPalette();
354 },
rginda30f20f62012-04-05 16:36:19 -0700355
Robert Ginda57f03b42012-09-13 11:02:48 -0700356 'copy-on-select': function(v) {
357 terminal.copyOnSelect = !!v;
358 },
rginda9f5222b2012-03-05 11:53:28 -0800359
Rob Spies0bec09b2014-06-06 15:58:09 -0700360 'use-default-window-copy': function(v) {
361 terminal.useDefaultWindowCopy = !!v;
362 },
363
364 'clear-selection-after-copy': function(v) {
365 terminal.clearSelectionAfterCopy = !!v;
366 },
367
Robert Ginda7e5e9522014-03-14 12:23:58 -0700368 'ctrl-plus-minus-zero-zoom': function(v) {
369 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
370 },
371
Robert Gindafb5a3f92014-05-13 14:12:00 -0700372 'ctrl-c-copy': function(v) {
373 terminal.keyboard.ctrlCCopy = v;
374 },
375
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100376 'ctrl-v-paste': function(v) {
377 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700378 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100379 },
380
Masaya Suzuki273aa982014-05-31 07:25:55 +0900381 'east-asian-ambiguous-as-two-column': function(v) {
382 lib.wc.regardCjkAmbiguous = v;
383 },
384
Robert Ginda57f03b42012-09-13 11:02:48 -0700385 'enable-8-bit-control': function(v) {
386 terminal.vt.enable8BitControl = !!v;
387 },
rginda30f20f62012-04-05 16:36:19 -0700388
Robert Ginda57f03b42012-09-13 11:02:48 -0700389 'enable-bold': function(v) {
390 terminal.syncBoldSafeState();
391 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400392
Robert Ginda3e278d72014-03-25 13:18:51 -0700393 'enable-bold-as-bright': function(v) {
394 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
395 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
396 },
397
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400398 'enable-blink': function(v) {
Mike Frysinger261597c2017-12-28 01:14:21 -0500399 terminal.setTextBlink(!!v);
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400400 },
401
Robert Ginda57f03b42012-09-13 11:02:48 -0700402 'enable-clipboard-write': function(v) {
403 terminal.vt.enableClipboardWrite = !!v;
404 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400405
Robert Ginda3755e752013-05-31 13:34:09 -0700406 'enable-dec12': function(v) {
407 terminal.vt.enableDec12 = !!v;
408 },
409
Robert Ginda57f03b42012-09-13 11:02:48 -0700410 'font-family': function(v) {
411 terminal.syncFontFamily();
412 },
rginda30f20f62012-04-05 16:36:19 -0700413
Robert Ginda57f03b42012-09-13 11:02:48 -0700414 'font-size': function(v) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500415 v = parseInt(v);
416 if (v <= 0) {
417 console.error(`Invalid font size: ${v}`);
418 return;
419 }
420
Robert Ginda57f03b42012-09-13 11:02:48 -0700421 terminal.setFontSize(v);
422 },
rginda9875d902012-08-20 16:21:57 -0700423
Robert Ginda57f03b42012-09-13 11:02:48 -0700424 'font-smoothing': function(v) {
425 terminal.syncFontFamily();
426 },
rgindade84e382012-04-20 15:39:31 -0700427
Robert Ginda57f03b42012-09-13 11:02:48 -0700428 'foreground-color': function(v) {
429 terminal.setForegroundColor(v);
430 },
rginda30f20f62012-04-05 16:36:19 -0700431
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400432 'hide-mouse-while-typing': function(v) {
433 terminal.setAutomaticMouseHiding(v);
434 },
435
Robert Ginda57f03b42012-09-13 11:02:48 -0700436 'home-keys-scroll': function(v) {
437 terminal.keyboard.homeKeysScroll = v;
438 },
rginda4bba5e12012-06-20 16:15:30 -0700439
Robert Gindaa8165692015-06-15 14:46:31 -0700440 'keybindings': function(v) {
441 terminal.keyboard.bindings.clear();
442
443 if (!v)
444 return;
445
446 if (!(v instanceof Object)) {
447 console.error('Error in keybindings preference: Expected object');
448 return;
449 }
450
451 try {
452 terminal.keyboard.bindings.addBindings(v);
453 } catch (ex) {
454 console.error('Error in keybindings preference: ' + ex);
455 }
456 },
457
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700458 'media-keys-are-fkeys': function(v) {
459 terminal.keyboard.mediaKeysAreFKeys = v;
460 },
461
Robert Ginda57f03b42012-09-13 11:02:48 -0700462 'meta-sends-escape': function(v) {
463 terminal.keyboard.metaSendsEscape = v;
464 },
rginda30f20f62012-04-05 16:36:19 -0700465
Mike Frysinger847577f2017-05-23 23:25:57 -0400466 'mouse-right-click-paste': function(v) {
467 terminal.mouseRightClickPaste = v;
468 },
469
Robert Ginda57f03b42012-09-13 11:02:48 -0700470 'mouse-paste-button': function(v) {
471 terminal.syncMousePasteButton();
472 },
rgindaa8ba17d2012-08-15 14:41:10 -0700473
Robert Gindae76aa9f2014-03-14 12:29:12 -0700474 'page-keys-scroll': function(v) {
475 terminal.keyboard.pageKeysScroll = v;
476 },
477
Robert Ginda40932892012-12-10 17:26:40 -0800478 'pass-alt-number': function(v) {
479 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800480 // Let Alt-1..9 pass to the browser (to control tab switching) on
481 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500482 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800483 }
484
485 terminal.passAltNumber = v;
486 },
487
488 'pass-ctrl-number': function(v) {
489 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800490 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
491 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500492 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800493 }
494
495 terminal.passCtrlNumber = v;
496 },
497
498 'pass-meta-number': function(v) {
499 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800500 // Let Meta-1..9 pass to the browser (to control tab switching) on
501 // OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500502 v = (hterm.os == 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800503 }
504
505 terminal.passMetaNumber = v;
506 },
507
Marius Schilder77857b32014-05-14 16:21:26 -0700508 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700509 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700510 },
511
Robert Ginda8cb7d902013-06-20 14:37:18 -0700512 'receive-encoding': function(v) {
513 if (!(/^(utf-8|raw)$/).test(v)) {
514 console.warn('Invalid value for "receive-encoding": ' + v);
515 v = 'utf-8';
516 }
517
518 terminal.vt.characterEncoding = v;
519 },
520
Robert Ginda57f03b42012-09-13 11:02:48 -0700521 'scroll-on-keystroke': function(v) {
522 terminal.scrollOnKeystroke_ = v;
523 },
rginda9f5222b2012-03-05 11:53:28 -0800524
Robert Ginda57f03b42012-09-13 11:02:48 -0700525 'scroll-on-output': function(v) {
526 terminal.scrollOnOutput_ = v;
527 },
rginda30f20f62012-04-05 16:36:19 -0700528
Robert Ginda57f03b42012-09-13 11:02:48 -0700529 'scrollbar-visible': function(v) {
530 terminal.setScrollbarVisible(v);
531 },
rginda9f5222b2012-03-05 11:53:28 -0800532
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400533 'scroll-wheel-may-send-arrow-keys': function(v) {
534 terminal.scrollWheelArrowKeys_ = v;
535 },
536
Rob Spies49039e52014-12-17 13:40:04 -0800537 'scroll-wheel-move-multiplier': function(v) {
538 terminal.setScrollWheelMoveMultipler(v);
539 },
540
Robert Ginda8cb7d902013-06-20 14:37:18 -0700541 'send-encoding': function(v) {
542 if (!(/^(utf-8|raw)$/).test(v)) {
543 console.warn('Invalid value for "send-encoding": ' + v);
544 v = 'utf-8';
545 }
546
547 terminal.keyboard.characterEncoding = v;
548 },
549
Robert Ginda57f03b42012-09-13 11:02:48 -0700550 'shift-insert-paste': function(v) {
551 terminal.keyboard.shiftInsertPaste = v;
552 },
rginda9f5222b2012-03-05 11:53:28 -0800553
Mike Frysingera7768922017-07-28 15:00:12 -0400554 'terminal-encoding': function(v) {
Mike Frysingera1371e12017-08-17 01:37:17 -0400555 terminal.vt.setEncoding(v);
Mike Frysingera7768922017-07-28 15:00:12 -0400556 },
557
Robert Gindae76aa9f2014-03-14 12:29:12 -0700558 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400559 terminal.scrollPort_.setUserCssUrl(v);
560 },
561
562 'user-css-text': function(v) {
563 terminal.scrollPort_.setUserCssText(v);
564 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400565
566 'word-break-match-left': function(v) {
567 terminal.primaryScreen_.wordBreakMatchLeft = v;
568 terminal.alternateScreen_.wordBreakMatchLeft = v;
569 },
570
571 'word-break-match-right': function(v) {
572 terminal.primaryScreen_.wordBreakMatchRight = v;
573 terminal.alternateScreen_.wordBreakMatchRight = v;
574 },
575
576 'word-break-match-middle': function(v) {
577 terminal.primaryScreen_.wordBreakMatchMiddle = v;
578 terminal.alternateScreen_.wordBreakMatchMiddle = v;
579 },
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400580
581 'allow-images-inline': function(v) {
582 terminal.allowImagesInline = v;
583 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700584 });
rginda30f20f62012-04-05 16:36:19 -0700585
Robert Ginda57f03b42012-09-13 11:02:48 -0700586 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800587 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700588
589 if (opt_callback)
590 opt_callback();
591 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800592};
593
Rob Spies56953412014-04-28 14:09:47 -0700594
595/**
596 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500597 *
598 * @return {hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700599 */
600hterm.Terminal.prototype.getPrefs = function() {
601 return this.prefs_;
602};
603
Robert Gindaa063b202014-07-21 11:08:25 -0700604/**
605 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500606 *
607 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700608 */
609hterm.Terminal.prototype.setBracketedPaste = function(state) {
610 this.options_.bracketedPaste = state;
611};
Rob Spies56953412014-04-28 14:09:47 -0700612
rginda8e92a692012-05-20 19:37:20 -0700613/**
614 * Set the color for the cursor.
615 *
616 * If you want this setting to persist, set it through prefs_, rather than
617 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500618 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500619 * @param {string=} color The color to set. If not defined, we reset to the
620 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700621 */
622hterm.Terminal.prototype.setCursorColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500623 if (color === undefined)
624 color = this.prefs_.get('cursor-color');
625
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400626 this.setCssVar('cursor-color', color);
rginda8e92a692012-05-20 19:37:20 -0700627};
628
629/**
630 * Return the current cursor color as a string.
Evan Jones2600d4f2016-12-06 09:29:36 -0500631 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700632 */
633hterm.Terminal.prototype.getCursorColor = function() {
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400634 return this.getCssVar('cursor-color');
rginda8e92a692012-05-20 19:37:20 -0700635};
636
637/**
rgindad5613292012-06-19 15:40:37 -0700638 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500639 *
640 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700641 */
642hterm.Terminal.prototype.setSelectionEnabled = function(state) {
643 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700644};
645
646/**
rginda8e92a692012-05-20 19:37:20 -0700647 * Set the background color.
648 *
649 * If you want this setting to persist, set it through prefs_, rather than
650 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500651 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500652 * @param {string=} color The color to set. If not defined, we reset to the
653 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700654 */
655hterm.Terminal.prototype.setBackgroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500656 if (color === undefined)
657 color = this.prefs_.get('background-color');
658
rgindacbbd7482012-06-13 15:06:16 -0700659 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700660 this.primaryScreen_.textAttributes.setDefaults(
661 this.foregroundColor_, this.backgroundColor_);
662 this.alternateScreen_.textAttributes.setDefaults(
663 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700664 this.scrollPort_.setBackgroundColor(color);
665};
666
rginda9f5222b2012-03-05 11:53:28 -0800667/**
668 * Return the current terminal background color.
669 *
670 * Intended for use by other classes, so we don't have to expose the entire
671 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500672 *
673 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800674 */
675hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700676 return this.backgroundColor_;
677};
678
679/**
680 * Set the foreground color.
681 *
682 * If you want this setting to persist, set it through prefs_, rather than
683 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500684 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500685 * @param {string=} color The color to set. If not defined, we reset to the
686 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700687 */
688hterm.Terminal.prototype.setForegroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500689 if (color === undefined)
690 color = this.prefs_.get('foreground-color');
691
rgindacbbd7482012-06-13 15:06:16 -0700692 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700693 this.primaryScreen_.textAttributes.setDefaults(
694 this.foregroundColor_, this.backgroundColor_);
695 this.alternateScreen_.textAttributes.setDefaults(
696 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700697 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800698};
699
700/**
701 * Return the current terminal foreground color.
702 *
703 * Intended for use by other classes, so we don't have to expose the entire
704 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500705 *
706 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800707 */
708hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700709 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800710};
711
712/**
rginda87b86462011-12-14 13:48:03 -0800713 * Create a new instance of a terminal command and run it with a given
714 * argument string.
715 *
716 * @param {function} commandClass The constructor for a terminal command.
717 * @param {string} argString The argument string to pass to the command.
718 */
719hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700720 var environment = this.prefs_.get('environment');
721 if (typeof environment != 'object' || environment == null)
722 environment = {};
723
rginda87b86462011-12-14 13:48:03 -0800724 var self = this;
725 this.command = new commandClass(
726 { argString: argString || '',
727 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700728 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800729 onExit: function(code) {
730 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800731 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700732 if (self.prefs_.get('close-on-exit'))
733 window.close();
rginda87b86462011-12-14 13:48:03 -0800734 }
735 });
736
rgindafeaf3142012-01-31 15:14:20 -0800737 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800738 this.command.run();
739};
740
741/**
rgindafeaf3142012-01-31 15:14:20 -0800742 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500743 *
744 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800745 */
746hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700747 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800748};
749
750/**
751 * Install the keyboard handler for this terminal.
752 *
753 * This will prevent the browser from seeing any keystrokes sent to the
754 * terminal.
755 */
756hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700757 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400758};
rgindafeaf3142012-01-31 15:14:20 -0800759
760/**
761 * Uninstall the keyboard handler for this terminal.
762 */
763hterm.Terminal.prototype.uninstallKeyboard = function() {
764 this.keyboard.installKeyboard(null);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400765};
rgindafeaf3142012-01-31 15:14:20 -0800766
767/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400768 * Set a CSS variable.
769 *
770 * Normally this is used to set variables in the hterm namespace.
771 *
772 * @param {string} name The variable to set.
773 * @param {string} value The value to assign to the variable.
774 * @param {string?} opt_prefix The variable namespace/prefix to use.
775 */
776hterm.Terminal.prototype.setCssVar = function(name, value,
777 opt_prefix='--hterm-') {
778 this.document_.documentElement.style.setProperty(
779 `${opt_prefix}${name}`, value);
780};
781
782/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500783 * Get a CSS variable.
784 *
785 * Normally this is used to get variables in the hterm namespace.
786 *
787 * @param {string} name The variable to read.
788 * @param {string?} opt_prefix The variable namespace/prefix to use.
789 * @return {string} The current setting for this variable.
790 */
791hterm.Terminal.prototype.getCssVar = function(name, opt_prefix='--hterm-') {
792 return this.document_.documentElement.style.getPropertyValue(
793 `${opt_prefix}${name}`);
794};
795
796/**
rginda35c456b2012-02-09 17:29:05 -0800797 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800798 *
799 * Call setFontSize(0) to reset to the default font size.
800 *
801 * This function does not modify the font-size preference.
802 *
803 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800804 */
805hterm.Terminal.prototype.setFontSize = function(px) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500806 if (px <= 0)
rginda9f5222b2012-03-05 11:53:28 -0800807 px = this.prefs_.get('font-size');
808
rginda35c456b2012-02-09 17:29:05 -0800809 this.scrollPort_.setFontSize(px);
Mike Frysingercce97c42017-08-05 01:11:22 -0400810 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
811 this.setCssVar('charsize-height',
812 this.scrollPort_.characterSize.height + 'px');
rginda35c456b2012-02-09 17:29:05 -0800813};
814
815/**
816 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500817 *
818 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800819 */
820hterm.Terminal.prototype.getFontSize = function() {
821 return this.scrollPort_.getFontSize();
822};
823
824/**
rginda8e92a692012-05-20 19:37:20 -0700825 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500826 *
827 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700828 */
829hterm.Terminal.prototype.getFontFamily = function() {
830 return this.scrollPort_.getFontFamily();
831};
832
833/**
rginda35c456b2012-02-09 17:29:05 -0800834 * Set the CSS "font-family" for this terminal.
835 */
rginda9f5222b2012-03-05 11:53:28 -0800836hterm.Terminal.prototype.syncFontFamily = function() {
837 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
838 this.prefs_.get('font-smoothing'));
839 this.syncBoldSafeState();
840};
841
rginda4bba5e12012-06-20 16:15:30 -0700842/**
843 * Set this.mousePasteButton based on the mouse-paste-button pref,
844 * autodetecting if necessary.
845 */
846hterm.Terminal.prototype.syncMousePasteButton = function() {
847 var button = this.prefs_.get('mouse-paste-button');
848 if (typeof button == 'number') {
849 this.mousePasteButton = button;
850 return;
851 }
852
Mike Frysingeree81a002017-12-12 16:14:53 -0500853 if (hterm.os != 'linux') {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400854 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700855 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400856 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700857 }
858};
859
860/**
861 * Enable or disable bold based on the enable-bold pref, autodetecting if
862 * necessary.
863 */
rginda9f5222b2012-03-05 11:53:28 -0800864hterm.Terminal.prototype.syncBoldSafeState = function() {
865 var enableBold = this.prefs_.get('enable-bold');
866 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700867 this.primaryScreen_.textAttributes.enableBold = enableBold;
868 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800869 return;
870 }
871
rgindaf7521392012-02-28 17:20:34 -0800872 var normalSize = this.scrollPort_.measureCharacterSize();
873 var boldSize = this.scrollPort_.measureCharacterSize('bold');
874
875 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800876 if (!isBoldSafe) {
877 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700878 'from normal. Font family is: ' +
879 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800880 }
rginda9f5222b2012-03-05 11:53:28 -0800881
Robert Gindaed016262012-10-26 16:27:09 -0700882 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
883 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800884};
885
886/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500887 * Control text blinking behavior.
888 *
889 * @param {boolean=} state Whether to enable support for blinking text.
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400890 */
Mike Frysinger261597c2017-12-28 01:14:21 -0500891hterm.Terminal.prototype.setTextBlink = function(state) {
892 if (state === undefined)
893 state = this.prefs_.get('enable-blink');
894 this.setCssVar('blink-node-duration', state ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400895};
896
897/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400898 * Set the mouse cursor style based on the current terminal mode.
899 */
900hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -0400901 this.setCssVar('mouse-cursor-style',
902 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
903 'var(--hterm-mouse-cursor-text)' :
904 'var(--hterm-mouse-cursor-pointer)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400905};
906
907/**
rginda87b86462011-12-14 13:48:03 -0800908 * Return a copy of the current cursor position.
909 *
910 * @return {hterm.RowCol} The RowCol object representing the current position.
911 */
912hterm.Terminal.prototype.saveCursor = function() {
913 return this.screen_.cursorPosition.clone();
914};
915
Evan Jones2600d4f2016-12-06 09:29:36 -0500916/**
917 * Return the current text attributes.
918 *
919 * @return {string}
920 */
rgindaa19afe22012-01-25 15:40:22 -0800921hterm.Terminal.prototype.getTextAttributes = function() {
922 return this.screen_.textAttributes;
923};
924
Evan Jones2600d4f2016-12-06 09:29:36 -0500925/**
926 * Set the text attributes.
927 *
928 * @param {string} textAttributes The attributes to set.
929 */
rginda1a09aa02012-06-18 21:11:25 -0700930hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
931 this.screen_.textAttributes = textAttributes;
932};
933
rginda87b86462011-12-14 13:48:03 -0800934/**
rgindaf522ce02012-04-17 17:49:17 -0700935 * Return the current browser zoom factor applied to the terminal.
936 *
937 * @return {number} The current browser zoom factor.
938 */
939hterm.Terminal.prototype.getZoomFactor = function() {
940 return this.scrollPort_.characterSize.zoomFactor;
941};
942
943/**
rginda9846e2f2012-01-27 13:53:33 -0800944 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -0500945 *
946 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -0800947 */
948hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800949 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800950};
951
952/**
rginda87b86462011-12-14 13:48:03 -0800953 * Restore a previously saved cursor position.
954 *
955 * @param {hterm.RowCol} cursor The position to restore.
956 */
957hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700958 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
959 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800960 this.screen_.setCursorPosition(row, column);
961 if (cursor.column > column ||
962 cursor.column == column && cursor.overflow) {
963 this.screen_.cursorPosition.overflow = true;
964 }
rginda87b86462011-12-14 13:48:03 -0800965};
966
967/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400968 * Clear the cursor's overflow flag.
969 */
970hterm.Terminal.prototype.clearCursorOverflow = function() {
971 this.screen_.cursorPosition.overflow = false;
972};
973
974/**
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800975 * Save the current cursor state to the corresponding screens.
976 *
977 * See the hterm.Screen.CursorState class for more details.
978 *
979 * @param {boolean=} both If true, update both screens, else only update the
980 * current screen.
981 */
982hterm.Terminal.prototype.saveCursorAndState = function(both) {
983 if (both) {
984 this.primaryScreen_.saveCursorAndState(this.vt);
985 this.alternateScreen_.saveCursorAndState(this.vt);
986 } else
987 this.screen_.saveCursorAndState(this.vt);
988};
989
990/**
991 * Restore the saved cursor state in the corresponding screens.
992 *
993 * See the hterm.Screen.CursorState class for more details.
994 *
995 * @param {boolean=} both If true, update both screens, else only update the
996 * current screen.
997 */
998hterm.Terminal.prototype.restoreCursorAndState = function(both) {
999 if (both) {
1000 this.primaryScreen_.restoreCursorAndState(this.vt);
1001 this.alternateScreen_.restoreCursorAndState(this.vt);
1002 } else
1003 this.screen_.restoreCursorAndState(this.vt);
1004};
1005
1006/**
Robert Ginda830583c2013-08-07 13:20:46 -07001007 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001008 *
1009 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -07001010 */
1011hterm.Terminal.prototype.setCursorShape = function(shape) {
1012 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001013 this.restyleCursor_();
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001014};
Robert Ginda830583c2013-08-07 13:20:46 -07001015
1016/**
1017 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001018 *
1019 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -07001020 */
1021hterm.Terminal.prototype.getCursorShape = function() {
1022 return this.cursorShape_;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001023};
Robert Ginda830583c2013-08-07 13:20:46 -07001024
1025/**
rginda87b86462011-12-14 13:48:03 -08001026 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001027 *
1028 * @param {number} columnCount
rginda87b86462011-12-14 13:48:03 -08001029 */
1030hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -08001031 if (columnCount == null) {
1032 this.div_.style.width = '100%';
1033 return;
1034 }
1035
Robert Ginda26806d12014-07-24 13:44:07 -07001036 this.div_.style.width = Math.ceil(
1037 this.scrollPort_.characterSize.width *
1038 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001039 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -08001040 this.scheduleSyncCursorPosition_();
1041};
rginda87b86462011-12-14 13:48:03 -08001042
rgindac9bc5502012-01-18 11:48:44 -08001043/**
rginda35c456b2012-02-09 17:29:05 -08001044 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001045 *
1046 * @param {number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -08001047 */
1048hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -08001049 if (rowCount == null) {
1050 this.div_.style.height = '100%';
1051 return;
1052 }
1053
rginda35c456b2012-02-09 17:29:05 -08001054 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -07001055 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -08001056 this.realizeSize_(this.screenSize.width, rowCount);
1057 this.scheduleSyncCursorPosition_();
1058};
1059
1060/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001061 * Deal with terminal size changes.
1062 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001063 * @param {number} columnCount The number of columns.
1064 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001065 */
1066hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
1067 if (columnCount != this.screenSize.width)
1068 this.realizeWidth_(columnCount);
1069
1070 if (rowCount != this.screenSize.height)
1071 this.realizeHeight_(rowCount);
1072
1073 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -07001074 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001075};
1076
1077/**
rgindac9bc5502012-01-18 11:48:44 -08001078 * Deal with terminal width changes.
1079 *
1080 * This function does what needs to be done when the terminal width changes
1081 * out from under us. It happens here rather than in onResize_() because this
1082 * code may need to run synchronously to handle programmatic changes of
1083 * terminal width.
1084 *
1085 * Relying on the browser to send us an async resize event means we may not be
1086 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001087 *
1088 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001089 */
1090hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001091 if (columnCount <= 0)
1092 throw new Error('Attempt to realize bad width: ' + columnCount);
1093
rgindac9bc5502012-01-18 11:48:44 -08001094 var deltaColumns = columnCount - this.screen_.getWidth();
1095
rginda87b86462011-12-14 13:48:03 -08001096 this.screenSize.width = columnCount;
1097 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001098
1099 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -04001100 if (this.defaultTabStops)
1101 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -08001102 } else {
1103 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -04001104 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -08001105 break;
1106
1107 this.tabStops_.pop();
1108 }
1109 }
1110
1111 this.screen_.setColumnCount(this.screenSize.width);
1112};
1113
1114/**
1115 * Deal with terminal height changes.
1116 *
1117 * This function does what needs to be done when the terminal height changes
1118 * out from under us. It happens here rather than in onResize_() because this
1119 * code may need to run synchronously to handle programmatic changes of
1120 * terminal height.
1121 *
1122 * Relying on the browser to send us an async resize event means we may not be
1123 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001124 *
1125 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001126 */
1127hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001128 if (rowCount <= 0)
1129 throw new Error('Attempt to realize bad height: ' + rowCount);
1130
rgindac9bc5502012-01-18 11:48:44 -08001131 var deltaRows = rowCount - this.screen_.getHeight();
1132
1133 this.screenSize.height = rowCount;
1134
1135 var cursor = this.saveCursor();
1136
1137 if (deltaRows < 0) {
1138 // Screen got smaller.
1139 deltaRows *= -1;
1140 while (deltaRows) {
1141 var lastRow = this.getRowCount() - 1;
1142 if (lastRow - this.scrollbackRows_.length == cursor.row)
1143 break;
1144
1145 if (this.getRowText(lastRow))
1146 break;
1147
1148 this.screen_.popRow();
1149 deltaRows--;
1150 }
1151
1152 var ary = this.screen_.shiftRows(deltaRows);
1153 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1154
1155 // We just removed rows from the top of the screen, we need to update
1156 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001157 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001158 } else if (deltaRows > 0) {
1159 // Screen got larger.
1160
1161 if (deltaRows <= this.scrollbackRows_.length) {
1162 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1163 var rows = this.scrollbackRows_.splice(
1164 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1165 this.screen_.unshiftRows(rows);
1166 deltaRows -= scrollbackCount;
1167 cursor.row += scrollbackCount;
1168 }
1169
1170 if (deltaRows)
1171 this.appendRows_(deltaRows);
1172 }
1173
rginda35c456b2012-02-09 17:29:05 -08001174 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001175 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001176};
1177
1178/**
1179 * Scroll the terminal to the top of the scrollback buffer.
1180 */
1181hterm.Terminal.prototype.scrollHome = function() {
1182 this.scrollPort_.scrollRowToTop(0);
1183};
1184
1185/**
1186 * Scroll the terminal to the end.
1187 */
1188hterm.Terminal.prototype.scrollEnd = function() {
1189 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1190};
1191
1192/**
1193 * Scroll the terminal one page up (minus one line) relative to the current
1194 * position.
1195 */
1196hterm.Terminal.prototype.scrollPageUp = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001197 this.scrollPort_.scrollPageUp();
rginda87b86462011-12-14 13:48:03 -08001198};
1199
1200/**
1201 * Scroll the terminal one page down (minus one line) relative to the current
1202 * position.
1203 */
1204hterm.Terminal.prototype.scrollPageDown = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001205 this.scrollPort_.scrollPageDown();
rginda8ba33642011-12-14 12:31:31 -08001206};
1207
rgindac9bc5502012-01-18 11:48:44 -08001208/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001209 * Scroll the terminal one line up relative to the current position.
1210 */
1211hterm.Terminal.prototype.scrollLineUp = function() {
1212 var i = this.scrollPort_.getTopRowIndex();
1213 this.scrollPort_.scrollRowToTop(i - 1);
1214};
1215
1216/**
1217 * Scroll the terminal one line down relative to the current position.
1218 */
1219hterm.Terminal.prototype.scrollLineDown = function() {
1220 var i = this.scrollPort_.getTopRowIndex();
1221 this.scrollPort_.scrollRowToTop(i + 1);
1222};
1223
1224/**
Robert Ginda40932892012-12-10 17:26:40 -08001225 * Clear primary screen, secondary screen, and the scrollback buffer.
1226 */
1227hterm.Terminal.prototype.wipeContents = function() {
1228 this.scrollbackRows_.length = 0;
1229 this.scrollPort_.resetCache();
1230
1231 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
1232 var bottom = screen.getHeight();
1233 if (bottom > 0) {
1234 this.renumberRows_(0, bottom);
1235 this.clearHome(screen);
1236 }
1237 }.bind(this));
1238
1239 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001240 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001241};
1242
1243/**
rgindac9bc5502012-01-18 11:48:44 -08001244 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001245 *
1246 * Perform a full reset to the default values listed in
1247 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001248 */
rginda87b86462011-12-14 13:48:03 -08001249hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001250 this.vt.reset();
1251
rgindac9bc5502012-01-18 11:48:44 -08001252 this.clearAllTabStops();
1253 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001254
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001255 const resetScreen = (screen) => {
1256 // We want to make sure to reset the attributes before we clear the screen.
1257 // The attributes might be used to initialize default/empty rows.
1258 screen.textAttributes.reset();
1259 screen.textAttributes.resetColorPalette();
1260 this.clearHome(screen);
1261 screen.saveCursorAndState(this.vt);
1262 };
1263 resetScreen(this.primaryScreen_);
1264 resetScreen(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001265
Mike Frysinger84301d02017-11-29 13:28:46 -08001266 // Reset terminal options to their default values.
1267 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001268 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1269
Mike Frysinger84301d02017-11-29 13:28:46 -08001270 this.setVTScrollRegion(null, null);
1271
1272 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001273};
1274
rgindac9bc5502012-01-18 11:48:44 -08001275/**
1276 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001277 *
1278 * Perform a soft reset to the default values listed in
1279 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001280 */
rginda0f5c0292012-01-13 11:00:13 -08001281hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001282 this.vt.reset();
1283
rgindab8bc8932012-04-27 12:45:03 -07001284 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001285 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001286
Brad Townb62dfdc2015-03-16 19:07:15 -07001287 // We show the cursor on soft reset but do not alter the blink state.
1288 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1289
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001290 const resetScreen = (screen) => {
1291 // Xterm also resets the color palette on soft reset, even though it doesn't
1292 // seem to be documented anywhere.
1293 screen.textAttributes.reset();
1294 screen.textAttributes.resetColorPalette();
1295 screen.saveCursorAndState(this.vt);
1296 };
1297 resetScreen(this.primaryScreen_);
1298 resetScreen(this.alternateScreen_);
rgindaf522ce02012-04-17 17:49:17 -07001299
rgindab8bc8932012-04-27 12:45:03 -07001300 // The xterm man page explicitly says this will happen on soft reset.
1301 this.setVTScrollRegion(null, null);
1302
1303 // Xterm also shows the cursor on soft reset, but does not alter the blink
1304 // state.
rgindaa19afe22012-01-25 15:40:22 -08001305 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001306};
1307
rgindac9bc5502012-01-18 11:48:44 -08001308/**
1309 * Move the cursor forward to the next tab stop, or to the last column
1310 * if no more tab stops are set.
1311 */
1312hterm.Terminal.prototype.forwardTabStop = function() {
1313 var column = this.screen_.cursorPosition.column;
1314
1315 for (var i = 0; i < this.tabStops_.length; i++) {
1316 if (this.tabStops_[i] > column) {
1317 this.setCursorColumn(this.tabStops_[i]);
1318 return;
1319 }
1320 }
1321
David Benjamin66e954d2012-05-05 21:08:12 -04001322 // xterm does not clear the overflow flag on HT or CHT.
1323 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001324 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001325 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001326};
1327
rgindac9bc5502012-01-18 11:48:44 -08001328/**
1329 * Move the cursor backward to the previous tab stop, or to the first column
1330 * if no previous tab stops are set.
1331 */
1332hterm.Terminal.prototype.backwardTabStop = function() {
1333 var column = this.screen_.cursorPosition.column;
1334
1335 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1336 if (this.tabStops_[i] < column) {
1337 this.setCursorColumn(this.tabStops_[i]);
1338 return;
1339 }
1340 }
1341
1342 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001343};
1344
rgindac9bc5502012-01-18 11:48:44 -08001345/**
1346 * Set a tab stop at the given column.
1347 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001348 * @param {integer} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001349 */
1350hterm.Terminal.prototype.setTabStop = function(column) {
1351 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1352 if (this.tabStops_[i] == column)
1353 return;
1354
1355 if (this.tabStops_[i] < column) {
1356 this.tabStops_.splice(i + 1, 0, column);
1357 return;
1358 }
1359 }
1360
1361 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001362};
1363
rgindac9bc5502012-01-18 11:48:44 -08001364/**
1365 * Clear the tab stop at the current cursor position.
1366 *
1367 * No effect if there is no tab stop at the current cursor position.
1368 */
1369hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1370 var column = this.screen_.cursorPosition.column;
1371
1372 var i = this.tabStops_.indexOf(column);
1373 if (i == -1)
1374 return;
1375
1376 this.tabStops_.splice(i, 1);
1377};
1378
1379/**
1380 * Clear all tab stops.
1381 */
1382hterm.Terminal.prototype.clearAllTabStops = function() {
1383 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001384 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001385};
1386
1387/**
1388 * Set up the default tab stops, starting from a given column.
1389 *
1390 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001391 * from the specified column, or 0 if no column is provided. It also flags
1392 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001393 *
1394 * This does not clear the existing tab stops first, use clearAllTabStops
1395 * for that.
1396 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001397 * @param {integer} opt_start Optional starting zero based starting column, useful
rgindac9bc5502012-01-18 11:48:44 -08001398 * for filling out missing tab stops when the terminal is resized.
1399 */
1400hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1401 var start = opt_start || 0;
1402 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001403 // Round start up to a default tab stop.
1404 start = start - 1 - ((start - 1) % w) + w;
1405 for (var i = start; i < this.screenSize.width; i += w) {
1406 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001407 }
David Benjamin66e954d2012-05-05 21:08:12 -04001408
1409 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001410};
1411
rginda6d397402012-01-17 10:58:29 -08001412/**
rginda8ba33642011-12-14 12:31:31 -08001413 * Interpret a sequence of characters.
1414 *
1415 * Incomplete escape sequences are buffered until the next call.
1416 *
1417 * @param {string} str Sequence of characters to interpret or pass through.
1418 */
1419hterm.Terminal.prototype.interpret = function(str) {
rginda8ba33642011-12-14 12:31:31 -08001420 this.scheduleSyncCursorPosition_();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001421 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001422};
1423
1424/**
1425 * Take over the given DIV for use as the terminal display.
1426 *
1427 * @param {HTMLDivElement} div The div to use as the terminal display.
1428 */
1429hterm.Terminal.prototype.decorate = function(div) {
Mike Frysinger5768a9d2017-12-26 12:57:44 -05001430 const charset = div.ownerDocument.characterSet.toLowerCase();
1431 if (charset != 'utf-8') {
1432 console.warn(`Document encoding should be set to utf-8, not "${charset}";` +
1433 ` Add <meta charset='utf-8'/> to your HTML <head> to fix.`);
1434 }
1435
rginda87b86462011-12-14 13:48:03 -08001436 this.div_ = div;
1437
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001438 this.accessibilityReader_ = new hterm.AccessibilityReader(div);
1439
rginda8ba33642011-12-14 12:31:31 -08001440 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001441 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001442 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1443 this.scrollPort_.setBackgroundPosition(
1444 this.prefs_.get('background-position'));
Mike Frysinger08bad432017-04-24 00:50:54 -04001445 this.scrollPort_.setUserCssUrl(this.prefs_.get('user-css'));
1446 this.scrollPort_.setUserCssText(this.prefs_.get('user-css-text'));
Raymes Khoury177aec72018-06-26 10:58:53 +10001447 this.scrollPort_.setAccessibilityReader(this.accessibilityReader_);
rginda30f20f62012-04-05 16:36:19 -07001448
rginda0918b652012-04-04 11:26:24 -07001449 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001450
rginda9f5222b2012-03-05 11:53:28 -08001451 this.setFontSize(this.prefs_.get('font-size'));
1452 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001453
David Reveman8f552492012-03-28 12:18:41 -04001454 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001455 this.setScrollWheelMoveMultipler(
1456 this.prefs_.get('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001457
rginda8ba33642011-12-14 12:31:31 -08001458 this.document_ = this.scrollPort_.getDocument();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001459 this.accessibilityReader_.decorate(this.document_);
rginda8ba33642011-12-14 12:31:31 -08001460
Evan Jones5f9df812016-12-06 09:38:58 -05001461 this.document_.body.oncontextmenu = function() { return false; };
rginda4bba5e12012-06-20 16:15:30 -07001462
1463 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001464 var screenNode = this.scrollPort_.getScreenNode();
1465 screenNode.addEventListener('mousedown', onMouse);
1466 screenNode.addEventListener('mouseup', onMouse);
1467 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001468 this.scrollPort_.onScrollWheel = onMouse;
1469
Mike Frysinger02ded6d2018-06-21 14:25:20 -04001470 screenNode.addEventListener('keydown', this.onKeyboardActivity_.bind(this));
1471
Toni Barzic0bfa8922013-11-22 11:18:35 -08001472 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001473 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001474 // Listen for mousedown events on the screenNode as in FF the focus
1475 // events don't bubble.
1476 screenNode.addEventListener('mousedown', function() {
1477 setTimeout(this.onFocusChange_.bind(this, true));
1478 }.bind(this));
1479
Toni Barzic0bfa8922013-11-22 11:18:35 -08001480 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001481 'blur', this.onFocusChange_.bind(this, false));
1482
1483 var style = this.document_.createElement('style');
1484 style.textContent =
1485 ('.cursor-node[focus="false"] {' +
1486 ' box-sizing: border-box;' +
1487 ' background-color: transparent !important;' +
1488 ' border-width: 2px;' +
1489 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001490 '}' +
1491 '.wc-node {' +
1492 ' display: inline-block;' +
1493 ' text-align: center;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001494 ' width: calc(var(--hterm-charsize-width) * 2);' +
Mike Frysinger4036f6e2017-05-31 14:02:55 -04001495 ' line-height: var(--hterm-charsize-height);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001496 '}' +
1497 ':root {' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001498 ' --hterm-charsize-width: ' + this.scrollPort_.characterSize.width + 'px;' +
1499 ' --hterm-charsize-height: ' + this.scrollPort_.characterSize.height + 'px;' +
Mike Frysingera27c0502017-08-23 22:37:10 -04001500 // Default position hides the cursor for when the window is initializing.
1501 ' --hterm-cursor-offset-col: -1;' +
1502 ' --hterm-cursor-offset-row: -1;' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001503 ' --hterm-blink-node-duration: 0.7s;' +
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001504 ' --hterm-mouse-cursor-text: text;' +
1505 ' --hterm-mouse-cursor-pointer: default;' +
1506 ' --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001507 '}' +
Mike Frysingerd3907202017-10-23 01:23:50 -04001508 '.uri-node:hover {' +
1509 ' text-decoration: underline;' +
Mike Frysingerb74a6472018-06-22 13:37:08 -04001510 ' cursor: var(--hterm-mouse-cursor-pointer), pointer;' +
Mike Frysingerd3907202017-10-23 01:23:50 -04001511 '}' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001512 '@keyframes blink {' +
1513 ' from { opacity: 1.0; }' +
1514 ' to { opacity: 0.0; }' +
1515 '}' +
1516 '.blink-node {' +
1517 ' animation-name: blink;' +
1518 ' animation-duration: var(--hterm-blink-node-duration);' +
1519 ' animation-iteration-count: infinite;' +
1520 ' animation-timing-function: ease-in-out;' +
1521 ' animation-direction: alternate;' +
rginda8e92a692012-05-20 19:37:20 -07001522 '}');
Mike Frysingerb74a6472018-06-22 13:37:08 -04001523 // Insert this stock style as the first node so that any user styles will
1524 // override w/out having to use !important everywhere. The rules above mix
1525 // runtime variables with default ones designed to be overridden by the user,
1526 // but we can wait for a concrete case from the users to determine the best
1527 // way to split the sheet up to before & after the user-css settings.
1528 this.document_.head.insertBefore(style, this.document_.head.firstChild);
rginda8e92a692012-05-20 19:37:20 -07001529
rginda8ba33642011-12-14 12:31:31 -08001530 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001531 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001532 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001533 this.cursorNode_.style.cssText =
1534 ('position: absolute;' +
Mike Frysinger44c32202017-08-05 01:13:09 -04001535 'left: calc(var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));' +
1536 'top: calc(var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));' +
Mike Frysingerd60f4c22018-03-15 21:25:30 -07001537 'display: ' + (this.options_.cursorVisible ? '' : 'none') + ';' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001538 'width: var(--hterm-charsize-width);' +
1539 'height: var(--hterm-charsize-height);' +
Mike Frysinger2fd079a2018-09-02 01:46:12 -04001540 'background-color: var(--hterm-cursor-color);' +
1541 'border-color: var(--hterm-cursor-color);' +
Rob Spies06533ba2014-04-24 11:20:37 -07001542 '-webkit-transition: opacity, background-color 100ms linear;' +
1543 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001544
Mike Frysingerf02a2cb2017-12-21 00:34:03 -05001545 this.setCursorColor();
Robert Gindafb1be6a2013-12-11 11:56:22 -08001546 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1547 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001548
rginda8ba33642011-12-14 12:31:31 -08001549 this.document_.body.appendChild(this.cursorNode_);
1550
rgindad5613292012-06-19 15:40:37 -07001551 // When 'enableMouseDragScroll' is off we reposition this element directly
1552 // under the mouse cursor after a click. This makes Chrome associate
1553 // subsequent mousemove events with the scroll-blocker. Since the
1554 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1555 // events do not cause the scrollport to scroll.
1556 //
1557 // It's a hack, but it's the cleanest way I could find.
1558 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001559 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
Raymes Khoury6dce2f82018-04-12 15:38:58 +10001560 this.scrollBlockerNode_.setAttribute('aria-hidden', 'true');
rgindad5613292012-06-19 15:40:37 -07001561 this.scrollBlockerNode_.style.cssText =
1562 ('position: absolute;' +
1563 'top: -99px;' +
1564 'display: block;' +
1565 'width: 10px;' +
1566 'height: 10px;');
1567 this.document_.body.appendChild(this.scrollBlockerNode_);
1568
rgindad5613292012-06-19 15:40:37 -07001569 this.scrollPort_.onScrollWheel = onMouse;
1570 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1571 ].forEach(function(event) {
1572 this.scrollBlockerNode_.addEventListener(event, onMouse);
1573 this.cursorNode_.addEventListener(event, onMouse);
1574 this.document_.addEventListener(event, onMouse);
1575 }.bind(this));
1576
1577 this.cursorNode_.addEventListener('mousedown', function() {
1578 setTimeout(this.focus.bind(this));
1579 }.bind(this));
1580
rginda8ba33642011-12-14 12:31:31 -08001581 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001582
rginda87b86462011-12-14 13:48:03 -08001583 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001584 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001585};
1586
rginda0918b652012-04-04 11:26:24 -07001587/**
1588 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001589 *
1590 * @return {HTMLDocument}
rginda0918b652012-04-04 11:26:24 -07001591 */
rginda87b86462011-12-14 13:48:03 -08001592hterm.Terminal.prototype.getDocument = function() {
1593 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001594};
1595
1596/**
rginda0918b652012-04-04 11:26:24 -07001597 * Focus the terminal.
1598 */
1599hterm.Terminal.prototype.focus = function() {
1600 this.scrollPort_.focus();
1601};
1602
1603/**
rginda8ba33642011-12-14 12:31:31 -08001604 * Return the HTML Element for a given row index.
1605 *
1606 * This is a method from the RowProvider interface. The ScrollPort uses
1607 * it to fetch rows on demand as they are scrolled into view.
1608 *
1609 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1610 * pairs to conserve memory.
1611 *
1612 * @param {integer} index The zero-based row index, measured relative to the
1613 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001614 * largest indices.
rginda8ba33642011-12-14 12:31:31 -08001615 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1616 */
1617hterm.Terminal.prototype.getRowNode = function(index) {
1618 if (index < this.scrollbackRows_.length)
1619 return this.scrollbackRows_[index];
1620
1621 var screenIndex = index - this.scrollbackRows_.length;
1622 return this.screen_.rowsArray[screenIndex];
1623};
1624
1625/**
1626 * Return the text content for a given range of rows.
1627 *
1628 * This is a method from the RowProvider interface. The ScrollPort uses
1629 * it to fetch text content on demand when the user attempts to copy their
1630 * selection to the clipboard.
1631 *
1632 * @param {integer} start The zero-based row index to start from, measured
1633 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001634 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001635 * @param {integer} end The zero-based row index to end on, measured
1636 * relative to the start of the scrollback buffer.
1637 * @return {string} A single string containing the text value of the range of
1638 * rows. Lines will be newline delimited, with no trailing newline.
1639 */
1640hterm.Terminal.prototype.getRowsText = function(start, end) {
1641 var ary = [];
1642 for (var i = start; i < end; i++) {
1643 var node = this.getRowNode(i);
1644 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001645 if (i < end - 1 && !node.getAttribute('line-overflow'))
1646 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001647 }
1648
rgindaa09e7332012-08-17 12:49:51 -07001649 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001650};
1651
1652/**
1653 * Return the text content for a given row.
1654 *
1655 * This is a method from the RowProvider interface. The ScrollPort uses
1656 * it to fetch text content on demand when the user attempts to copy their
1657 * selection to the clipboard.
1658 *
1659 * @param {integer} index The zero-based row index to return, measured
1660 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001661 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001662 * @return {string} A string containing the text value of the selected row.
1663 */
1664hterm.Terminal.prototype.getRowText = function(index) {
1665 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001666 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001667};
1668
1669/**
1670 * Return the total number of rows in the addressable screen and in the
1671 * scrollback buffer of this terminal.
1672 *
1673 * This is a method from the RowProvider interface. The ScrollPort uses
1674 * it to compute the size of the scrollbar.
1675 *
1676 * @return {integer} The number of rows in this terminal.
1677 */
1678hterm.Terminal.prototype.getRowCount = function() {
1679 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1680};
1681
1682/**
1683 * Create DOM nodes for new rows and append them to the end of the terminal.
1684 *
1685 * This is the only correct way to add a new DOM node for a row. Notice that
1686 * the new row is appended to the bottom of the list of rows, and does not
1687 * require renumbering (of the rowIndex property) of previous rows.
1688 *
1689 * If you think you want a new blank row somewhere in the middle of the
1690 * terminal, look into moveRows_().
1691 *
1692 * This method does not pay attention to vtScrollTop/Bottom, since you should
1693 * be using moveRows() in cases where they would matter.
1694 *
1695 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001696 *
1697 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001698 */
1699hterm.Terminal.prototype.appendRows_ = function(count) {
1700 var cursorRow = this.screen_.rowsArray.length;
1701 var offset = this.scrollbackRows_.length + cursorRow;
1702 for (var i = 0; i < count; i++) {
1703 var row = this.document_.createElement('x-row');
1704 row.appendChild(this.document_.createTextNode(''));
1705 row.rowIndex = offset + i;
1706 this.screen_.pushRow(row);
1707 }
1708
1709 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1710 if (extraRows > 0) {
1711 var ary = this.screen_.shiftRows(extraRows);
1712 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001713 if (this.scrollPort_.isScrolledEnd)
1714 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001715 }
1716
1717 if (cursorRow >= this.screen_.rowsArray.length)
1718 cursorRow = this.screen_.rowsArray.length - 1;
1719
rginda87b86462011-12-14 13:48:03 -08001720 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001721};
1722
1723/**
1724 * Relocate rows from one part of the addressable screen to another.
1725 *
1726 * This is used to recycle rows during VT scrolls (those which are driven
1727 * by VT commands, rather than by the user manipulating the scrollbar.)
1728 *
1729 * In this case, the blank lines scrolled into the scroll region are made of
1730 * the nodes we scrolled off. These have their rowIndex properties carefully
1731 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001732 *
1733 * @param {number} fromIndex The start index.
1734 * @param {number} count The number of rows to move.
1735 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001736 */
1737hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1738 var ary = this.screen_.removeRows(fromIndex, count);
1739 this.screen_.insertRows(toIndex, ary);
1740
1741 var start, end;
1742 if (fromIndex < toIndex) {
1743 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001744 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001745 } else {
1746 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001747 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001748 }
1749
1750 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001751 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001752};
1753
1754/**
1755 * Renumber the rowIndex property of the given range of rows.
1756 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001757 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08001758 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001759 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001760 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05001761 *
1762 * @param {number} start The start index.
1763 * @param {number} end The end index.
1764 * @param {hterm.Screen} opt_screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08001765 */
Robert Ginda40932892012-12-10 17:26:40 -08001766hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1767 var screen = opt_screen || this.screen_;
1768
rginda8ba33642011-12-14 12:31:31 -08001769 var offset = this.scrollbackRows_.length;
1770 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001771 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001772 }
1773};
1774
1775/**
1776 * Print a string to the terminal.
1777 *
1778 * This respects the current insert and wraparound modes. It will add new lines
1779 * to the end of the terminal, scrolling off the top into the scrollback buffer
1780 * if necessary.
1781 *
1782 * The string is *not* parsed for escape codes. Use the interpret() method if
1783 * that's what you're after.
1784 *
1785 * @param{string} str The string to print.
1786 */
1787hterm.Terminal.prototype.print = function(str) {
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001788 this.scheduleSyncCursorPosition_();
1789
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001790 // Basic accessibility output for the screen reader.
Raymes Khoury177aec72018-06-26 10:58:53 +10001791 this.accessibilityReader_.announce(str);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001792
rgindaa9abdd82012-08-06 18:05:09 -07001793 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001794
Ricky Liang48f05cb2013-12-31 23:35:29 +08001795 var strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04001796 // Fun edge case: If the string only contains zero width codepoints (like
1797 // combining characters), we make sure to iterate at least once below.
1798 if (strWidth == 0 && str)
1799 strWidth = 1;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001800
1801 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001802 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1803 this.screen_.commitLineOverflow();
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001804 this.newLine(true);
rgindaa09e7332012-08-17 12:49:51 -07001805 }
rgindaa19afe22012-01-25 15:40:22 -08001806
Ricky Liang48f05cb2013-12-31 23:35:29 +08001807 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001808 var didOverflow = false;
1809 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001810
rgindaa9abdd82012-08-06 18:05:09 -07001811 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1812 didOverflow = true;
1813 count = this.screenSize.width - this.screen_.cursorPosition.column;
1814 }
rgindaa19afe22012-01-25 15:40:22 -08001815
rgindaa9abdd82012-08-06 18:05:09 -07001816 if (didOverflow && !this.options_.wraparound) {
1817 // If the string overflowed the line but wraparound is off, then the
1818 // last printed character should be the last of the string.
1819 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001820 substr = lib.wc.substr(str, startOffset, count - 1) +
1821 lib.wc.substr(str, strWidth - 1);
1822 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001823 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001824 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001825 }
rgindaa19afe22012-01-25 15:40:22 -08001826
Ricky Liang48f05cb2013-12-31 23:35:29 +08001827 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1828 for (var i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001829 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
1830 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001831
1832 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001833 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001834 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001835 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001836 }
1837 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001838 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07001839 }
1840
1841 this.screen_.maybeClipCurrentRow();
1842 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001843 }
rginda8ba33642011-12-14 12:31:31 -08001844
rginda9f5222b2012-03-05 11:53:28 -08001845 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001846 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001847};
1848
1849/**
rginda87b86462011-12-14 13:48:03 -08001850 * Set the VT scroll region.
1851 *
rginda87b86462011-12-14 13:48:03 -08001852 * This also resets the cursor position to the absolute (0, 0) position, since
1853 * that's what xterm appears to do.
1854 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001855 * Setting the scroll region to the full height of the terminal will clear
1856 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1857 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1858 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1859 * continue to work as most users would expect.
1860 *
rginda87b86462011-12-14 13:48:03 -08001861 * @param {integer} scrollTop The zero-based top of the scroll region.
1862 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1863 * inclusive.
1864 */
1865hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001866 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001867 this.vtScrollTop_ = null;
1868 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001869 } else {
1870 this.vtScrollTop_ = scrollTop;
1871 this.vtScrollBottom_ = scrollBottom;
1872 }
rginda87b86462011-12-14 13:48:03 -08001873};
1874
1875/**
rginda8ba33642011-12-14 12:31:31 -08001876 * Return the top row index according to the VT.
1877 *
1878 * This will return 0 unless the terminal has been told to restrict scrolling
1879 * to some lower row. It is used for some VT cursor positioning and scrolling
1880 * commands.
1881 *
1882 * @return {integer} The topmost row in the terminal's scroll region.
1883 */
1884hterm.Terminal.prototype.getVTScrollTop = function() {
1885 if (this.vtScrollTop_ != null)
1886 return this.vtScrollTop_;
1887
1888 return 0;
rginda87b86462011-12-14 13:48:03 -08001889};
rginda8ba33642011-12-14 12:31:31 -08001890
1891/**
1892 * Return the bottom row index according to the VT.
1893 *
1894 * This will return the height of the terminal unless the it has been told to
1895 * restrict scrolling to some higher row. It is used for some VT cursor
1896 * positioning and scrolling commands.
1897 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001898 * @return {integer} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001899 */
1900hterm.Terminal.prototype.getVTScrollBottom = function() {
1901 if (this.vtScrollBottom_ != null)
1902 return this.vtScrollBottom_;
1903
rginda87b86462011-12-14 13:48:03 -08001904 return this.screenSize.height - 1;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001905};
rginda8ba33642011-12-14 12:31:31 -08001906
1907/**
1908 * Process a '\n' character.
1909 *
1910 * If the cursor is on the final row of the terminal this will append a new
1911 * blank row to the screen and scroll the topmost row into the scrollback
1912 * buffer.
1913 *
1914 * Otherwise, this moves the cursor to column zero of the next row.
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001915 *
1916 * @param {boolean=} dueToOverflow Whether the newline is due to wraparound of
1917 * the terminal.
rginda8ba33642011-12-14 12:31:31 -08001918 */
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001919hterm.Terminal.prototype.newLine = function(dueToOverflow = false) {
1920 if (!dueToOverflow)
1921 this.accessibilityReader_.newLine();
1922
Robert Ginda9937abc2013-07-25 16:09:23 -07001923 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1924 this.screen_.rowsArray.length - 1);
1925
1926 if (this.vtScrollBottom_ != null) {
1927 // A VT Scroll region is active, we never append new rows.
1928 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1929 // We're at the end of the VT Scroll Region, perform a VT scroll.
1930 this.vtScrollUp(1);
1931 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1932 } else if (cursorAtEndOfScreen) {
1933 // We're at the end of the screen, the only thing to do is put the
1934 // cursor to column 0.
1935 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1936 } else {
1937 // Anywhere else, advance the cursor row, and reset the column.
1938 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1939 }
1940 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001941 // We're at the end of the screen. Append a new row to the terminal,
1942 // shifting the top row into the scrollback.
1943 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001944 } else {
rginda87b86462011-12-14 13:48:03 -08001945 // Anywhere else in the screen just moves the cursor.
1946 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001947 }
1948};
1949
1950/**
1951 * Like newLine(), except maintain the cursor column.
1952 */
1953hterm.Terminal.prototype.lineFeed = function() {
1954 var column = this.screen_.cursorPosition.column;
1955 this.newLine();
1956 this.setCursorColumn(column);
1957};
1958
1959/**
rginda87b86462011-12-14 13:48:03 -08001960 * If autoCarriageReturn is set then newLine(), else lineFeed().
1961 */
1962hterm.Terminal.prototype.formFeed = function() {
1963 if (this.options_.autoCarriageReturn) {
1964 this.newLine();
1965 } else {
1966 this.lineFeed();
1967 }
1968};
1969
1970/**
1971 * Move the cursor up one row, possibly inserting a blank line.
1972 *
1973 * The cursor column is not changed.
1974 */
1975hterm.Terminal.prototype.reverseLineFeed = function() {
1976 var scrollTop = this.getVTScrollTop();
1977 var currentRow = this.screen_.cursorPosition.row;
1978
1979 if (currentRow == scrollTop) {
1980 this.insertLines(1);
1981 } else {
1982 this.setAbsoluteCursorRow(currentRow - 1);
1983 }
1984};
1985
1986/**
rginda8ba33642011-12-14 12:31:31 -08001987 * Replace all characters to the left of the current cursor with the space
1988 * character.
1989 *
1990 * TODO(rginda): This should probably *remove* the characters (not just replace
1991 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001992 * position.
rginda8ba33642011-12-14 12:31:31 -08001993 */
1994hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001995 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001996 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04001997 const count = cursor.column + 1;
1998 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08001999 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002000};
2001
2002/**
David Benjamin684a9b72012-05-01 17:19:58 -04002003 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08002004 *
2005 * The cursor position is unchanged.
2006 *
Robert Gindaf2547f12012-10-25 20:36:21 -07002007 * If the current background color is not the default background color this
2008 * will insert spaces rather than delete. This is unfortunate because the
2009 * trailing space will affect text selection, but it's difficult to come up
2010 * with a way to style empty space that wouldn't trip up the hterm.Screen
2011 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07002012 *
2013 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
2014 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
2015 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05002016 *
2017 * @param {number} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08002018 */
2019hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07002020 if (this.screen_.cursorPosition.overflow)
2021 return;
2022
Robert Ginda7fd57082012-09-25 14:41:47 -07002023 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
2024 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07002025
2026 if (this.screen_.textAttributes.background ===
2027 this.screen_.textAttributes.DEFAULT_COLOR) {
2028 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08002029 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07002030 this.screen_.cursorPosition.column + count) {
2031 this.screen_.deleteChars(count);
2032 this.clearCursorOverflow();
2033 return;
2034 }
2035 }
2036
rginda87b86462011-12-14 13:48:03 -08002037 var cursor = this.saveCursor();
Mike Frysinger6380bed2017-08-24 18:46:39 -04002038 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08002039 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002040 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002041};
2042
2043/**
2044 * Erase the current line.
2045 *
2046 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002047 */
2048hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08002049 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002050 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08002051 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002052 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002053};
2054
2055/**
David Benjamina08d78f2012-05-05 00:28:49 -04002056 * Erase all characters from the start of the screen to the current cursor
2057 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002058 *
2059 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002060 */
2061hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08002062 var cursor = this.saveCursor();
2063
2064 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002065
David Benjamina08d78f2012-05-05 00:28:49 -04002066 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002067 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002068 this.screen_.clearCursorRow();
2069 }
2070
rginda87b86462011-12-14 13:48:03 -08002071 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002072 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002073};
2074
2075/**
2076 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002077 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002078 *
2079 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002080 */
2081hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08002082 var cursor = this.saveCursor();
2083
2084 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002085
David Benjamina08d78f2012-05-05 00:28:49 -04002086 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08002087 for (var i = cursor.row + 1; i <= bottom; i++) {
2088 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002089 this.screen_.clearCursorRow();
2090 }
2091
rginda87b86462011-12-14 13:48:03 -08002092 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002093 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002094};
2095
2096/**
2097 * Fill the terminal with a given character.
2098 *
2099 * This methods does not respect the VT scroll region.
2100 *
2101 * @param {string} ch The character to use for the fill.
2102 */
2103hterm.Terminal.prototype.fill = function(ch) {
2104 var cursor = this.saveCursor();
2105
2106 this.setAbsoluteCursorPosition(0, 0);
2107 for (var row = 0; row < this.screenSize.height; row++) {
2108 for (var col = 0; col < this.screenSize.width; col++) {
2109 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002110 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002111 }
2112 }
2113
2114 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002115};
2116
2117/**
rginda9ea433c2012-03-16 11:57:00 -07002118 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002119 *
rginda9ea433c2012-03-16 11:57:00 -07002120 * This does not respect the scroll region.
2121 *
2122 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2123 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002124 */
rginda9ea433c2012-03-16 11:57:00 -07002125hterm.Terminal.prototype.clearHome = function(opt_screen) {
2126 var screen = opt_screen || this.screen_;
2127 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002128
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002129 this.accessibilityReader_.clear();
2130
rginda11057d52012-04-25 12:29:56 -07002131 if (bottom == 0) {
2132 // Empty screen, nothing to do.
2133 return;
2134 }
2135
rgindae4d29232012-01-19 10:47:13 -08002136 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002137 screen.setCursorPosition(i, 0);
2138 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002139 }
2140
rginda9ea433c2012-03-16 11:57:00 -07002141 screen.setCursorPosition(0, 0);
2142};
2143
2144/**
2145 * Erase the entire display without changing the cursor position.
2146 *
2147 * The cursor position is unchanged. This does not respect the scroll
2148 * region.
2149 *
2150 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2151 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002152 */
2153hterm.Terminal.prototype.clear = function(opt_screen) {
2154 var screen = opt_screen || this.screen_;
2155 var cursor = screen.cursorPosition.clone();
2156 this.clearHome(screen);
2157 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002158};
2159
2160/**
2161 * VT command to insert lines at the current cursor row.
2162 *
2163 * This respects the current scroll region. Rows pushed off the bottom are
2164 * lost (they won't show up in the scrollback buffer).
2165 *
rginda8ba33642011-12-14 12:31:31 -08002166 * @param {integer} count The number of lines to insert.
2167 */
2168hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07002169 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002170
2171 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002172 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002173
Robert Ginda579186b2012-09-26 11:40:04 -07002174 // The moveCount is the number of rows we need to relocate to make room for
2175 // the new row(s). The count is the distance to move them.
2176 var moveCount = bottom - cursorRow - count + 1;
2177 if (moveCount)
2178 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08002179
Robert Ginda579186b2012-09-26 11:40:04 -07002180 for (var i = count - 1; i >= 0; i--) {
2181 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002182 this.screen_.clearCursorRow();
2183 }
rginda8ba33642011-12-14 12:31:31 -08002184};
2185
2186/**
2187 * VT command to delete lines at the current cursor row.
2188 *
2189 * New rows are added to the bottom of scroll region to take their place. New
2190 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002191 *
2192 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002193 */
2194hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08002195 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002196
rginda87b86462011-12-14 13:48:03 -08002197 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08002198 var bottom = this.getVTScrollBottom();
2199
rginda87b86462011-12-14 13:48:03 -08002200 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002201 count = Math.min(count, maxCount);
2202
rginda87b86462011-12-14 13:48:03 -08002203 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08002204 if (count != maxCount)
2205 this.moveRows_(top, count, moveStart);
2206
2207 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002208 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002209 this.screen_.clearCursorRow();
2210 }
2211
rginda87b86462011-12-14 13:48:03 -08002212 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002213 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002214};
2215
2216/**
2217 * Inserts the given number of spaces at the current cursor position.
2218 *
rginda87b86462011-12-14 13:48:03 -08002219 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002220 *
2221 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002222 */
2223hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08002224 var cursor = this.saveCursor();
2225
rgindacbbd7482012-06-13 15:06:16 -07002226 var ws = lib.f.getWhitespace(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002227 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002228 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002229
2230 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002231 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002232};
2233
2234/**
2235 * Forward-delete the specified number of characters starting at the cursor
2236 * position.
2237 *
2238 * @param {integer} count The number of characters to delete.
2239 */
2240hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002241 var deleted = this.screen_.deleteChars(count);
2242 if (deleted && !this.screen_.textAttributes.isDefault()) {
2243 var cursor = this.saveCursor();
2244 this.setCursorColumn(this.screenSize.width - deleted);
2245 this.screen_.insertString(lib.f.getWhitespace(deleted));
2246 this.restoreCursor(cursor);
2247 }
2248
David Benjamin54e8bf62012-06-01 22:31:40 -04002249 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002250};
2251
2252/**
2253 * Shift rows in the scroll region upwards by a given number of lines.
2254 *
2255 * New rows are inserted at the bottom of the scroll region to fill the
2256 * vacated rows. The new rows not filled out with the current text attributes.
2257 *
2258 * This function does not affect the scrollback rows at all. Rows shifted
2259 * off the top are lost.
2260 *
rginda87b86462011-12-14 13:48:03 -08002261 * The cursor position is not altered.
2262 *
rginda8ba33642011-12-14 12:31:31 -08002263 * @param {integer} count The number of rows to scroll.
2264 */
2265hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002266 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002267
rginda87b86462011-12-14 13:48:03 -08002268 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002269 this.deleteLines(count);
2270
rginda87b86462011-12-14 13:48:03 -08002271 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002272};
2273
2274/**
2275 * Shift rows below the cursor down by a given number of lines.
2276 *
2277 * This function respects the current scroll region.
2278 *
2279 * New rows are inserted at the top of the scroll region to fill the
2280 * vacated rows. The new rows not filled out with the current text attributes.
2281 *
2282 * This function does not affect the scrollback rows at all. Rows shifted
2283 * off the bottom are lost.
2284 *
2285 * @param {integer} count The number of rows to scroll.
2286 */
2287hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08002288 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002289
rginda87b86462011-12-14 13:48:03 -08002290 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08002291 this.insertLines(opt_count);
2292
rginda87b86462011-12-14 13:48:03 -08002293 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002294};
2295
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002296/**
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002297 * Enable accessibility-friendly features that have a performance impact.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002298 *
2299 * This will generate additional DOM nodes in an aria-live region that will
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002300 * cause Assitive Technology to announce the output of the terminal. It also
2301 * enables other features that aid assistive technology. All the features gated
2302 * behind this flag have a performance impact on the terminal which is why they
2303 * are made optional.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002304 *
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002305 * @param {boolean} enabled Whether to enable accessibility-friendly features.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002306 */
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002307hterm.Terminal.prototype.setAccessibilityEnabled = function(enabled) {
Raymes Khoury177aec72018-06-26 10:58:53 +10002308 this.accessibilityReader_.setAccessibilityEnabled(enabled);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002309};
rginda87b86462011-12-14 13:48:03 -08002310
rginda8ba33642011-12-14 12:31:31 -08002311/**
2312 * Set the cursor position.
2313 *
2314 * The cursor row is relative to the scroll region if the terminal has
2315 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2316 *
2317 * @param {integer} row The new zero-based cursor row.
2318 * @param {integer} row The new zero-based cursor column.
2319 */
2320hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2321 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002322 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002323 } else {
rginda87b86462011-12-14 13:48:03 -08002324 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002325 }
rginda87b86462011-12-14 13:48:03 -08002326};
rginda8ba33642011-12-14 12:31:31 -08002327
Evan Jones2600d4f2016-12-06 09:29:36 -05002328/**
2329 * Move the cursor relative to its current position.
2330 *
2331 * @param {number} row
2332 * @param {number} column
2333 */
rginda87b86462011-12-14 13:48:03 -08002334hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2335 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002336 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2337 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002338 this.screen_.setCursorPosition(row, column);
2339};
2340
Evan Jones2600d4f2016-12-06 09:29:36 -05002341/**
2342 * Move the cursor to the specified position.
2343 *
2344 * @param {number} row
2345 * @param {number} column
2346 */
rginda87b86462011-12-14 13:48:03 -08002347hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002348 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2349 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002350 this.screen_.setCursorPosition(row, column);
2351};
2352
2353/**
2354 * Set the cursor column.
2355 *
2356 * @param {integer} column The new zero-based cursor column.
2357 */
2358hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002359 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002360};
2361
2362/**
2363 * Return the cursor column.
2364 *
2365 * @return {integer} The zero-based cursor column.
2366 */
2367hterm.Terminal.prototype.getCursorColumn = function() {
2368 return this.screen_.cursorPosition.column;
2369};
2370
2371/**
2372 * Set the cursor row.
2373 *
2374 * The cursor row is relative to the scroll region if the terminal has
2375 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2376 *
2377 * @param {integer} row The new cursor row.
2378 */
rginda87b86462011-12-14 13:48:03 -08002379hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2380 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002381};
2382
2383/**
2384 * Return the cursor row.
2385 *
2386 * @return {integer} The zero-based cursor row.
2387 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002388hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002389 return this.screen_.cursorPosition.row;
2390};
2391
2392/**
2393 * Request that the ScrollPort redraw itself soon.
2394 *
2395 * The redraw will happen asynchronously, soon after the call stack winds down.
2396 * Multiple calls will be coalesced into a single redraw.
2397 */
2398hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002399 if (this.timeouts_.redraw)
2400 return;
rginda8ba33642011-12-14 12:31:31 -08002401
2402 var self = this;
rginda87b86462011-12-14 13:48:03 -08002403 this.timeouts_.redraw = setTimeout(function() {
2404 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002405 self.scrollPort_.redraw_();
2406 }, 0);
2407};
2408
2409/**
2410 * Request that the ScrollPort be scrolled to the bottom.
2411 *
2412 * The scroll will happen asynchronously, soon after the call stack winds down.
2413 * Multiple calls will be coalesced into a single scroll.
2414 *
2415 * This affects the scrollbar position of the ScrollPort, and has nothing to
2416 * do with the VT scroll commands.
2417 */
2418hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2419 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002420 return;
rginda8ba33642011-12-14 12:31:31 -08002421
2422 var self = this;
2423 this.timeouts_.scrollDown = setTimeout(function() {
2424 delete self.timeouts_.scrollDown;
2425 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2426 }, 10);
2427};
2428
2429/**
2430 * Move the cursor up a specified number of rows.
2431 *
2432 * @param {integer} count The number of rows to move the cursor.
2433 */
2434hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002435 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002436};
2437
2438/**
2439 * Move the cursor down a specified number of rows.
2440 *
2441 * @param {integer} count The number of rows to move the cursor.
2442 */
2443hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002444 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002445 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2446 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2447 this.screenSize.height - 1);
2448
rgindacbbd7482012-06-13 15:06:16 -07002449 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002450 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002451 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002452};
2453
2454/**
2455 * Move the cursor left a specified number of columns.
2456 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002457 * If reverse wraparound mode is enabled and the previous row wrapped into
2458 * the current row then we back up through the wraparound as well.
2459 *
rginda8ba33642011-12-14 12:31:31 -08002460 * @param {integer} count The number of columns to move the cursor.
2461 */
2462hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002463 count = count || 1;
2464
2465 if (count < 1)
2466 return;
2467
2468 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002469 if (this.options_.reverseWraparound) {
2470 if (this.screen_.cursorPosition.overflow) {
2471 // If this cursor is in the right margin, consume one count to get it
2472 // back to the last column. This only applies when we're in reverse
2473 // wraparound mode.
2474 count--;
2475 this.clearCursorOverflow();
2476
2477 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002478 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002479 }
2480
Robert Gindabfb32622014-07-17 13:20:27 -07002481 var newRow = this.screen_.cursorPosition.row;
2482 var newColumn = currentColumn - count;
2483 if (newColumn < 0) {
2484 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2485 if (newRow < 0) {
2486 // xterm also wraps from row 0 to the last row.
2487 newRow = this.screenSize.height + newRow % this.screenSize.height;
2488 }
2489 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2490 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002491
Robert Gindabfb32622014-07-17 13:20:27 -07002492 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2493
2494 } else {
2495 var newColumn = Math.max(currentColumn - count, 0);
2496 this.setCursorColumn(newColumn);
2497 }
rginda8ba33642011-12-14 12:31:31 -08002498};
2499
2500/**
2501 * Move the cursor right a specified number of columns.
2502 *
2503 * @param {integer} count The number of columns to move the cursor.
2504 */
2505hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002506 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002507
2508 if (count < 1)
2509 return;
2510
rgindacbbd7482012-06-13 15:06:16 -07002511 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002512 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002513 this.setCursorColumn(column);
2514};
2515
2516/**
2517 * Reverse the foreground and background colors of the terminal.
2518 *
2519 * This only affects text that was drawn with no attributes.
2520 *
2521 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2522 * been drawn with attributes that happen to coincide with the default
2523 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002524 *
2525 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002526 */
2527hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002528 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002529 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002530 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2531 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002532 } else {
rginda9f5222b2012-03-05 11:53:28 -08002533 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2534 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002535 }
2536};
2537
2538/**
rginda87b86462011-12-14 13:48:03 -08002539 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002540 *
2541 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002542 */
2543hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002544 this.cursorNode_.style.backgroundColor =
2545 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002546
2547 var self = this;
2548 setTimeout(function() {
Matheus Fernandes2d733082017-09-11 06:43:01 -04002549 self.restyleCursor_();
rginda6d397402012-01-17 10:58:29 -08002550 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002551
Michael Kelly485ecd12014-06-09 11:41:56 -04002552 // bellSquelchTimeout_ affects both audio and notification bells.
2553 if (this.bellSquelchTimeout_)
2554 return;
2555
Robert Ginda92e18102013-03-14 13:56:37 -07002556 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002557 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002558 this.bellSequelchTimeout_ = setTimeout(function() {
2559 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002560 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002561 } else {
2562 delete this.bellSquelchTimeout_;
2563 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002564
2565 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingera5fb83c2017-06-22 14:48:35 -07002566 var n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002567 this.bellNotificationList_.push(n);
2568 // TODO: Should we try to raise the window here?
2569 n.onclick = function() { self.closeBellNotifications_(); };
2570 }
rginda87b86462011-12-14 13:48:03 -08002571};
2572
2573/**
rginda8ba33642011-12-14 12:31:31 -08002574 * Set the origin mode bit.
2575 *
2576 * If origin mode is on, certain VT cursor and scrolling commands measure their
2577 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2578 * to the top of the addressable screen.
2579 *
2580 * Defaults to off.
2581 *
2582 * @param {boolean} state True to set origin mode, false to unset.
2583 */
2584hterm.Terminal.prototype.setOriginMode = function(state) {
2585 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002586 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002587};
2588
2589/**
2590 * Set the insert mode bit.
2591 *
2592 * If insert mode is on, existing text beyond the cursor position will be
2593 * shifted right to make room for new text. Otherwise, new text overwrites
2594 * any existing text.
2595 *
2596 * Defaults to off.
2597 *
2598 * @param {boolean} state True to set insert mode, false to unset.
2599 */
2600hterm.Terminal.prototype.setInsertMode = function(state) {
2601 this.options_.insertMode = state;
2602};
2603
2604/**
rginda87b86462011-12-14 13:48:03 -08002605 * Set the auto carriage return bit.
2606 *
2607 * If auto carriage return is on then a formfeed character is interpreted
2608 * as a newline, otherwise it's the same as a linefeed. The difference boils
2609 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002610 *
2611 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002612 */
2613hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2614 this.options_.autoCarriageReturn = state;
2615};
2616
2617/**
rginda8ba33642011-12-14 12:31:31 -08002618 * Set the wraparound mode bit.
2619 *
2620 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2621 * to the start of the following row. Otherwise, the cursor is clamped to the
2622 * end of the screen and attempts to write past it are ignored.
2623 *
2624 * Defaults to on.
2625 *
2626 * @param {boolean} state True to set wraparound mode, false to unset.
2627 */
2628hterm.Terminal.prototype.setWraparound = function(state) {
2629 this.options_.wraparound = state;
2630};
2631
2632/**
2633 * Set the reverse-wraparound mode bit.
2634 *
2635 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2636 * to the end of the previous row. Otherwise, the cursor is clamped to column
2637 * 0.
2638 *
2639 * Defaults to off.
2640 *
2641 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2642 */
2643hterm.Terminal.prototype.setReverseWraparound = function(state) {
2644 this.options_.reverseWraparound = state;
2645};
2646
2647/**
2648 * Selects between the primary and alternate screens.
2649 *
2650 * If alternate mode is on, the alternate screen is active. Otherwise the
2651 * primary screen is active.
2652 *
2653 * Swapping screens has no effect on the scrollback buffer.
2654 *
2655 * Each screen maintains its own cursor position.
2656 *
2657 * Defaults to off.
2658 *
2659 * @param {boolean} state True to set alternate mode, false to unset.
2660 */
2661hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002662 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002663 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2664
rginda35c456b2012-02-09 17:29:05 -08002665 if (this.screen_.rowsArray.length &&
2666 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2667 // If the screen changed sizes while we were away, our rowIndexes may
2668 // be incorrect.
2669 var offset = this.scrollbackRows_.length;
2670 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002671 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002672 ary[i].rowIndex = offset + i;
2673 }
2674 }
rginda8ba33642011-12-14 12:31:31 -08002675
rginda35c456b2012-02-09 17:29:05 -08002676 this.realizeWidth_(this.screenSize.width);
2677 this.realizeHeight_(this.screenSize.height);
2678 this.scrollPort_.syncScrollHeight();
2679 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002680
rginda6d397402012-01-17 10:58:29 -08002681 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002682 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002683};
2684
2685/**
2686 * Set the cursor-blink mode bit.
2687 *
2688 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2689 * a visible cursor does not blink.
2690 *
2691 * You should make sure to turn blinking off if you're going to dispose of a
2692 * terminal, otherwise you'll leak a timeout.
2693 *
2694 * Defaults to on.
2695 *
2696 * @param {boolean} state True to set cursor-blink mode, false to unset.
2697 */
2698hterm.Terminal.prototype.setCursorBlink = function(state) {
2699 this.options_.cursorBlink = state;
2700
2701 if (!state && this.timeouts_.cursorBlink) {
2702 clearTimeout(this.timeouts_.cursorBlink);
2703 delete this.timeouts_.cursorBlink;
2704 }
2705
2706 if (this.options_.cursorVisible)
2707 this.setCursorVisible(true);
2708};
2709
2710/**
2711 * Set the cursor-visible mode bit.
2712 *
2713 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2714 *
2715 * Defaults to on.
2716 *
2717 * @param {boolean} state True to set cursor-visible mode, false to unset.
2718 */
2719hterm.Terminal.prototype.setCursorVisible = function(state) {
2720 this.options_.cursorVisible = state;
2721
2722 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002723 if (this.timeouts_.cursorBlink) {
2724 clearTimeout(this.timeouts_.cursorBlink);
2725 delete this.timeouts_.cursorBlink;
2726 }
rginda87b86462011-12-14 13:48:03 -08002727 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002728 return;
2729 }
2730
rginda87b86462011-12-14 13:48:03 -08002731 this.syncCursorPosition_();
2732
2733 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002734
2735 if (this.options_.cursorBlink) {
2736 if (this.timeouts_.cursorBlink)
2737 return;
2738
Robert Gindaea2183e2014-07-17 09:51:51 -07002739 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002740 } else {
2741 if (this.timeouts_.cursorBlink) {
2742 clearTimeout(this.timeouts_.cursorBlink);
2743 delete this.timeouts_.cursorBlink;
2744 }
2745 }
2746};
2747
2748/**
rginda87b86462011-12-14 13:48:03 -08002749 * Synchronizes the visible cursor and document selection with the current
2750 * cursor coordinates.
Raymes Khourye5d48982018-08-02 09:08:32 +10002751 *
2752 * @return {boolean} True if the cursor is onscreen and synced.
rginda8ba33642011-12-14 12:31:31 -08002753 */
2754hterm.Terminal.prototype.syncCursorPosition_ = function() {
2755 var topRowIndex = this.scrollPort_.getTopRowIndex();
2756 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2757 var cursorRowIndex = this.scrollbackRows_.length +
2758 this.screen_.cursorPosition.row;
2759
Raymes Khoury15697f42018-07-17 11:37:18 +10002760 let forceSyncSelection = false;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002761 if (this.accessibilityReader_.accessibilityEnabled) {
2762 // Report the new position of the cursor for accessibility purposes.
2763 const cursorColumnIndex = this.screen_.cursorPosition.column;
2764 const cursorLineText =
2765 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
Raymes Khoury15697f42018-07-17 11:37:18 +10002766 // This will force the selection to be sync'd to the cursor position if the
2767 // user has pressed a key. Generally we would only sync the cursor position
2768 // when selection is collapsed so that if the user has selected something
2769 // we don't clear the selection by moving the selection. However when a
2770 // screen reader is used, it's intuitive for entering a key to move the
2771 // selection to the cursor.
2772 forceSyncSelection = this.accessibilityReader_.hasUserGesture;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002773 this.accessibilityReader_.afterCursorChange(
2774 cursorLineText, cursorRowIndex, cursorColumnIndex);
2775 }
2776
rginda8ba33642011-12-14 12:31:31 -08002777 if (cursorRowIndex > bottomRowIndex) {
2778 // Cursor is scrolled off screen, move it outside of the visible area.
Mike Frysinger44c32202017-08-05 01:13:09 -04002779 this.setCssVar('cursor-offset-row', '-1');
Raymes Khourye5d48982018-08-02 09:08:32 +10002780 return false;
rginda8ba33642011-12-14 12:31:31 -08002781 }
2782
Robert Gindab837c052014-08-11 11:17:51 -07002783 if (this.options_.cursorVisible &&
2784 this.cursorNode_.style.display == 'none') {
2785 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2786 this.cursorNode_.style.display = '';
2787 }
2788
Mike Frysinger44c32202017-08-05 01:13:09 -04002789 // Position the cursor using CSS variable math. If we do the math in JS,
2790 // the float math will end up being more precise than the CSS which will
2791 // cause the cursor tracking to be off.
2792 this.setCssVar(
2793 'cursor-offset-row',
2794 `${cursorRowIndex - topRowIndex} + ` +
2795 `${this.scrollPort_.visibleRowTopMargin}px`);
2796 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08002797
2798 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04002799 '(' + this.screen_.cursorPosition.column +
2800 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08002801 ')');
2802
2803 // Update the caret for a11y purposes.
2804 var selection = this.document_.getSelection();
Raymes Khoury15697f42018-07-17 11:37:18 +10002805 if (selection && (selection.isCollapsed || forceSyncSelection)) {
rginda87b86462011-12-14 13:48:03 -08002806 this.screen_.syncSelectionCaret(selection);
Raymes Khoury15697f42018-07-17 11:37:18 +10002807 }
Raymes Khourye5d48982018-08-02 09:08:32 +10002808 return true;
rginda8ba33642011-12-14 12:31:31 -08002809};
2810
Robert Gindafb1be6a2013-12-11 11:56:22 -08002811/**
2812 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2813 * and character cell dimensions.
2814 */
Robert Ginda830583c2013-08-07 13:20:46 -07002815hterm.Terminal.prototype.restyleCursor_ = function() {
2816 var shape = this.cursorShape_;
2817
2818 if (this.cursorNode_.getAttribute('focus') == 'false') {
2819 // Always show a block cursor when unfocused.
2820 shape = hterm.Terminal.cursorShape.BLOCK;
2821 }
2822
2823 var style = this.cursorNode_.style;
2824
2825 switch (shape) {
2826 case hterm.Terminal.cursorShape.BEAM:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002827 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002828 style.backgroundColor = 'transparent';
2829 style.borderBottomStyle = null;
2830 style.borderLeftStyle = 'solid';
2831 break;
2832
2833 case hterm.Terminal.cursorShape.UNDERLINE:
2834 style.height = this.scrollPort_.characterSize.baseline + 'px';
2835 style.backgroundColor = 'transparent';
2836 style.borderBottomStyle = 'solid';
2837 // correct the size to put it exactly at the baseline
2838 style.borderLeftStyle = null;
2839 break;
2840
2841 default:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002842 style.height = 'var(--hterm-charsize-height)';
Mike Frysinger2fd079a2018-09-02 01:46:12 -04002843 style.backgroundColor = 'var(--hterm-cursor-color)';
Robert Ginda830583c2013-08-07 13:20:46 -07002844 style.borderBottomStyle = null;
2845 style.borderLeftStyle = null;
2846 break;
2847 }
2848};
2849
rginda8ba33642011-12-14 12:31:31 -08002850/**
2851 * Synchronizes the visible cursor with the current cursor coordinates.
2852 *
2853 * The sync will happen asynchronously, soon after the call stack winds down.
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002854 * Multiple calls will be coalesced into a single sync. This should be called
2855 * prior to the cursor actually changing position.
rginda8ba33642011-12-14 12:31:31 -08002856 */
2857hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2858 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002859 return;
rginda8ba33642011-12-14 12:31:31 -08002860
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002861 if (this.accessibilityReader_.accessibilityEnabled) {
2862 // Report the previous position of the cursor for accessibility purposes.
2863 const cursorRowIndex = this.scrollbackRows_.length +
2864 this.screen_.cursorPosition.row;
2865 const cursorColumnIndex = this.screen_.cursorPosition.column;
2866 const cursorLineText =
2867 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
2868 this.accessibilityReader_.beforeCursorChange(
2869 cursorLineText, cursorRowIndex, cursorColumnIndex);
2870 }
2871
rginda8ba33642011-12-14 12:31:31 -08002872 var self = this;
2873 this.timeouts_.syncCursor = setTimeout(function() {
2874 self.syncCursorPosition_();
2875 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002876 }, 0);
2877};
2878
rgindacc2996c2012-02-24 14:59:31 -08002879/**
rgindaf522ce02012-04-17 17:49:17 -07002880 * Show or hide the zoom warning.
2881 *
2882 * The zoom warning is a message warning the user that their browser zoom must
2883 * be set to 100% in order for hterm to function properly.
2884 *
2885 * @param {boolean} state True to show the message, false to hide it.
2886 */
2887hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2888 if (!this.zoomWarningNode_) {
2889 if (!state)
2890 return;
2891
2892 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002893 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07002894 this.zoomWarningNode_.style.cssText = (
2895 'color: black;' +
2896 'background-color: #ff2222;' +
2897 'font-size: large;' +
2898 'border-radius: 8px;' +
2899 'opacity: 0.75;' +
2900 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2901 'top: 0.5em;' +
2902 'right: 1.2em;' +
2903 'position: absolute;' +
2904 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002905 '-webkit-user-select: none;' +
2906 '-moz-text-size-adjust: none;' +
2907 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05002908
2909 this.zoomWarningNode_.addEventListener('click', function(e) {
2910 this.parentNode.removeChild(this);
2911 });
rgindaf522ce02012-04-17 17:49:17 -07002912 }
2913
Robert Gindab4839c22013-02-28 16:52:10 -08002914 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2915 hterm.zoomWarningMessage,
2916 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2917
rgindaf522ce02012-04-17 17:49:17 -07002918 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2919
2920 if (state) {
2921 if (!this.zoomWarningNode_.parentNode)
2922 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2923 } else if (this.zoomWarningNode_.parentNode) {
2924 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2925 }
2926};
2927
2928/**
rgindacc2996c2012-02-24 14:59:31 -08002929 * Show the terminal overlay for a given amount of time.
2930 *
2931 * The terminal overlay appears in inverse video in a large font, centered
2932 * over the terminal. You should probably keep the overlay message brief,
2933 * since it's in a large font and you probably aren't going to check the size
2934 * of the terminal first.
2935 *
2936 * @param {string} msg The text (not HTML) message to display in the overlay.
2937 * @param {number} opt_timeout The amount of time to wait before fading out
2938 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2939 * stay up forever (or until the next overlay).
2940 */
2941hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002942 if (!this.overlayNode_) {
2943 if (!this.div_)
2944 return;
2945
2946 this.overlayNode_ = this.document_.createElement('div');
2947 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002948 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002949 'font-size: xx-large;' +
2950 'opacity: 0.75;' +
2951 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2952 'position: absolute;' +
2953 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002954 '-webkit-transition: opacity 180ms ease-in;' +
2955 '-moz-user-select: none;' +
2956 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002957
2958 this.overlayNode_.addEventListener('mousedown', function(e) {
2959 e.preventDefault();
2960 e.stopPropagation();
2961 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002962 }
2963
rginda9f5222b2012-03-05 11:53:28 -08002964 this.overlayNode_.style.color = this.prefs_.get('background-color');
2965 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2966 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2967
rgindaf0090c92012-02-10 14:58:52 -08002968 this.overlayNode_.textContent = msg;
2969 this.overlayNode_.style.opacity = '0.75';
2970
2971 if (!this.overlayNode_.parentNode)
2972 this.div_.appendChild(this.overlayNode_);
2973
Robert Ginda97769282013-02-01 15:30:30 -08002974 var divSize = hterm.getClientSize(this.div_);
2975 var overlaySize = hterm.getClientSize(this.overlayNode_);
2976
Robert Ginda8a59f762014-07-23 11:29:55 -07002977 this.overlayNode_.style.top =
2978 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08002979 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07002980 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08002981
rgindaf0090c92012-02-10 14:58:52 -08002982 if (this.overlayTimeout_)
2983 clearTimeout(this.overlayTimeout_);
2984
Raymes Khouryc7a06382018-07-04 10:25:45 +10002985 this.accessibilityReader_.assertiveAnnounce(msg);
2986
rgindacc2996c2012-02-24 14:59:31 -08002987 if (opt_timeout === null)
2988 return;
2989
Mike Frysingerb6cfded2017-09-18 00:39:31 -04002990 this.overlayTimeout_ = setTimeout(() => {
2991 this.overlayNode_.style.opacity = '0';
2992 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
2993 }, opt_timeout || 1500);
2994};
2995
2996/**
2997 * Hide the terminal overlay immediately.
2998 *
2999 * Useful when we show an overlay for an event with an unknown end time.
3000 */
3001hterm.Terminal.prototype.hideOverlay = function() {
3002 if (this.overlayTimeout_)
3003 clearTimeout(this.overlayTimeout_);
3004 this.overlayTimeout_ = null;
3005
3006 if (this.overlayNode_.parentNode)
3007 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
3008 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08003009};
3010
rginda4bba5e12012-06-20 16:15:30 -07003011/**
3012 * Paste from the system clipboard to the terminal.
3013 */
3014hterm.Terminal.prototype.paste = function() {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003015 return hterm.pasteFromClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07003016};
3017
3018/**
3019 * Copy a string to the system clipboard.
3020 *
3021 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05003022 *
3023 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07003024 */
3025hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08003026 if (this.prefs_.get('enable-clipboard-notice'))
3027 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
3028
rgindaa09e7332012-08-17 12:49:51 -07003029 var copySource = this.document_.createElement('pre');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04003030 copySource.id = 'hterm:copy-to-clipboard-source';
rginda4bba5e12012-06-20 16:15:30 -07003031 copySource.textContent = str;
3032 copySource.style.cssText = (
3033 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003034 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07003035 'position: absolute;' +
3036 'top: -99px');
3037
3038 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07003039
rginda4bba5e12012-06-20 16:15:30 -07003040 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07003041 var anchorNode = selection.anchorNode;
3042 var anchorOffset = selection.anchorOffset;
3043 var focusNode = selection.focusNode;
3044 var focusOffset = selection.focusOffset;
3045
rginda4bba5e12012-06-20 16:15:30 -07003046 selection.selectAllChildren(copySource);
3047
rgindaa09e7332012-08-17 12:49:51 -07003048 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07003049
Rob Spies56953412014-04-28 14:09:47 -07003050 // IE doesn't support selection.extend. This means that the selection
3051 // won't return on IE.
Robert Ginda6f4f0aa2014-07-28 10:21:39 -07003052 if (selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07003053 selection.collapse(anchorNode, anchorOffset);
3054 selection.extend(focusNode, focusOffset);
3055 }
rgindafaa74742012-08-21 13:34:03 -07003056
rginda4bba5e12012-06-20 16:15:30 -07003057 copySource.parentNode.removeChild(copySource);
3058};
3059
Evan Jones2600d4f2016-12-06 09:29:36 -05003060/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003061 * Display an image.
3062 *
3063 * @param {Object} options The image to display.
3064 * @param {string=} options.name A human readable string for the image.
3065 * @param {string|number=} options.size The size (in bytes).
3066 * @param {boolean=} options.preserveAspectRatio Whether to preserve aspect.
3067 * @param {boolean=} options.inline Whether to display the image inline.
3068 * @param {string|number=} options.width The width of the image.
3069 * @param {string|number=} options.height The height of the image.
3070 * @param {string=} options.align Direction to align the image.
3071 * @param {string} options.uri The source URI for the image.
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003072 * @param {function=} onLoad Callback when loading finishes.
3073 * @param {function(Event)=} onError Callback when loading fails.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003074 */
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003075hterm.Terminal.prototype.displayImage = function(options, onLoad, onError) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003076 // Make sure we're actually given a resource to display.
3077 if (options.uri === undefined)
3078 return;
3079
3080 // Set up the defaults to simplify code below.
3081 if (!options.name)
3082 options.name = '';
3083
3084 // Has the user approved image display yet?
3085 if (this.allowImagesInline !== true) {
3086 this.newLine();
3087 const row = this.getRowNode(this.scrollbackRows_.length +
3088 this.getCursorRow() - 1);
3089
3090 if (this.allowImagesInline === false) {
3091 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
3092 'Inline Images Disabled');
3093 return;
3094 }
3095
3096 // Show a prompt.
3097 let button;
3098 const span = this.document_.createElement('span');
3099 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
3100 span.style.fontWeight = 'bold';
3101 span.style.borderWidth = '1px';
3102 span.style.borderStyle = 'dashed';
3103 button = this.document_.createElement('span');
3104 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3105 button.style.marginLeft = '1em';
3106 button.style.borderWidth = '1px';
3107 button.style.borderStyle = 'solid';
3108 button.addEventListener('click', () => {
3109 this.prefs_.set('allow-images-inline', false);
3110 });
3111 span.appendChild(button);
3112 button = this.document_.createElement('span');
3113 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3114 'allow this session');
3115 button.style.marginLeft = '1em';
3116 button.style.borderWidth = '1px';
3117 button.style.borderStyle = 'solid';
3118 button.addEventListener('click', () => {
3119 this.allowImagesInline = true;
3120 });
3121 span.appendChild(button);
3122 button = this.document_.createElement('span');
3123 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3124 button.style.marginLeft = '1em';
3125 button.style.borderWidth = '1px';
3126 button.style.borderStyle = 'solid';
3127 button.addEventListener('click', () => {
3128 this.prefs_.set('allow-images-inline', true);
3129 });
3130 span.appendChild(button);
3131
3132 row.appendChild(span);
3133 return;
3134 }
3135
3136 // See if we should show this object directly, or download it.
3137 if (options.inline) {
3138 const io = this.io.push();
3139 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
3140 'Loading $1 ...'), null);
3141
3142 // While we're loading the image, eat all the user's input.
3143 io.onVTKeystroke = io.sendString = () => {};
3144
3145 // Initialize this new image.
3146 const img = this.document_.createElement('img');
3147 img.src = options.uri;
3148 img.title = img.alt = options.name;
3149
3150 // Attach the image to the page to let it load/render. It won't stay here.
3151 // This is needed so it's visible and the DOM can calculate the height. If
3152 // the image is hidden or not in the DOM, the height is always 0.
3153 this.document_.body.appendChild(img);
3154
3155 // Wait for the image to finish loading before we try moving it to the
3156 // right place in the terminal.
3157 img.onload = () => {
3158 // Now that we have the image dimensions, figure out how to show it.
3159 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
3160 img.style.maxWidth = `${this.document_.body.clientWidth}px`;
3161 img.style.maxHeight = `${this.document_.body.clientHeight}px`;
3162
3163 // Parse a width/height specification.
3164 const parseDim = (dim, maxDim, cssVar) => {
3165 if (!dim || dim == 'auto')
3166 return '';
3167
3168 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3169 if (ary) {
3170 if (ary[2] == '%')
3171 return maxDim * parseInt(ary[1]) / 100 + 'px';
3172 else if (ary[2] == 'px')
3173 return dim;
3174 else
3175 return `calc(${dim} * var(${cssVar}))`;
3176 }
3177
3178 return '';
3179 };
3180 img.style.width =
3181 parseDim(options.width, this.document_.body.clientWidth,
3182 '--hterm-charsize-width');
3183 img.style.height =
3184 parseDim(options.height, this.document_.body.clientHeight,
3185 '--hterm-charsize-height');
3186
3187 // Figure out how many rows the image occupies, then add that many.
3188 // XXX: This count will be inaccurate if the font size changes on us.
3189 const padRows = Math.ceil(img.clientHeight /
3190 this.scrollPort_.characterSize.height);
3191 for (let i = 0; i < padRows; ++i)
3192 this.newLine();
3193
3194 // Update the max height in case the user shrinks the character size.
3195 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3196
3197 // Move the image to the last row. This way when we scroll up, it doesn't
3198 // disappear when the first row gets clipped. It will disappear when we
3199 // scroll down and the last row is clipped ...
3200 this.document_.body.removeChild(img);
3201 // Create a wrapper node so we can do an absolute in a relative position.
3202 // This helps with rounding errors between JS & CSS counts.
3203 const div = this.document_.createElement('div');
3204 div.style.position = 'relative';
3205 div.style.textAlign = options.align;
3206 img.style.position = 'absolute';
3207 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3208 div.appendChild(img);
3209 const row = this.getRowNode(this.scrollbackRows_.length +
3210 this.getCursorRow() - 1);
3211 row.appendChild(div);
3212
3213 io.hideOverlay();
3214 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003215
3216 if (onLoad)
3217 onLoad();
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003218 };
3219
3220 // If we got a malformed image, give up.
3221 img.onerror = (e) => {
3222 this.document_.body.removeChild(img);
3223 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
Mike Frysingere14a8c42018-03-10 00:17:30 -08003224 'Loading $1 failed'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003225 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003226
3227 if (onError)
3228 onError(e);
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003229 };
3230 } else {
3231 // We can't use chrome.downloads.download as that requires "downloads"
3232 // permissions, and that works only in extensions, not apps.
3233 const a = this.document_.createElement('a');
3234 a.href = options.uri;
3235 a.download = options.name;
3236 this.document_.body.appendChild(a);
3237 a.click();
3238 a.remove();
3239 }
3240};
3241
3242/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003243 * Returns the selected text, or null if no text is selected.
3244 *
3245 * @return {string|null}
3246 */
rgindaa09e7332012-08-17 12:49:51 -07003247hterm.Terminal.prototype.getSelectionText = function() {
3248 var selection = this.scrollPort_.selection;
3249 selection.sync();
3250
3251 if (selection.isCollapsed)
3252 return null;
3253
rgindaa09e7332012-08-17 12:49:51 -07003254 // Start offset measures from the beginning of the line.
3255 var startOffset = selection.startOffset;
3256 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003257
Raymes Khoury334625a2018-06-25 10:29:40 +10003258 // If an x-row isn't selected, |node| will be null.
3259 if (!node)
3260 return null;
3261
Robert Gindafdbb3f22012-09-06 20:23:06 -07003262 if (node.nodeName != 'X-ROW') {
3263 // If the selection doesn't start on an x-row node, then it must be
3264 // somewhere inside the x-row. Add any characters from previous siblings
3265 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003266
3267 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3268 // If node is the text node in a styled span, move up to the span node.
3269 node = node.parentNode;
3270 }
3271
Robert Gindafdbb3f22012-09-06 20:23:06 -07003272 while (node.previousSibling) {
3273 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003274 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003275 }
rgindaa09e7332012-08-17 12:49:51 -07003276 }
3277
3278 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08003279 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
3280 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003281 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003282
Robert Gindafdbb3f22012-09-06 20:23:06 -07003283 if (node.nodeName != 'X-ROW') {
3284 // If the selection doesn't end on an x-row node, then it must be
3285 // somewhere inside the x-row. Add any characters from following siblings
3286 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003287
3288 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3289 // If node is the text node in a styled span, move up to the span node.
3290 node = node.parentNode;
3291 }
3292
Robert Gindafdbb3f22012-09-06 20:23:06 -07003293 while (node.nextSibling) {
3294 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003295 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003296 }
rgindaa09e7332012-08-17 12:49:51 -07003297 }
3298
3299 var rv = this.getRowsText(selection.startRow.rowIndex,
3300 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003301 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003302};
3303
rginda4bba5e12012-06-20 16:15:30 -07003304/**
3305 * Copy the current selection to the system clipboard, then clear it after a
3306 * short delay.
3307 */
3308hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07003309 var text = this.getSelectionText();
3310 if (text != null)
3311 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07003312};
3313
rgindaf0090c92012-02-10 14:58:52 -08003314hterm.Terminal.prototype.overlaySize = function() {
3315 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
3316};
3317
rginda87b86462011-12-14 13:48:03 -08003318/**
3319 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3320 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003321 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003322 */
3323hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08003324 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08003325 this.scrollPort_.scrollRowToBottom(this.getRowCount());
3326
Robert Ginda8cb7d902013-06-20 14:37:18 -07003327 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08003328};
3329
3330/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003331 * Open the selected url.
3332 */
3333hterm.Terminal.prototype.openSelectedUrl_ = function() {
3334 var str = this.getSelectionText();
3335
3336 // If there is no selection, try and expand wherever they clicked.
3337 if (str == null) {
John Lincae9b732018-03-08 13:56:35 +08003338 this.screen_.expandSelectionForUrl(this.document_.getSelection());
Mike Frysinger70b94692017-01-26 18:57:50 -10003339 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003340
3341 // If clicking in empty space, return.
3342 if (str == null)
3343 return;
Mike Frysinger70b94692017-01-26 18:57:50 -10003344 }
3345
3346 // Make sure URL is valid before opening.
3347 if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0)
3348 return;
Mike Frysinger43472622017-06-26 18:11:07 -04003349
3350 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003351 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003352 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3353 // We have to whitelist a few protocols that lack authorities and thus
3354 // never use the //. Like mailto.
3355 switch (str.split(':', 1)[0]) {
3356 case 'mailto':
3357 break;
3358 default:
3359 str = 'http://' + str;
3360 break;
3361 }
3362 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003363
Mike Frysinger720fa832017-10-23 01:15:52 -04003364 hterm.openUrl(str);
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003365};
Mike Frysinger70b94692017-01-26 18:57:50 -10003366
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003367/**
3368 * Manage the automatic mouse hiding behavior while typing.
3369 *
3370 * @param {boolean=} v Whether to enable automatic hiding.
3371 */
3372hterm.Terminal.prototype.setAutomaticMouseHiding = function(v=null) {
3373 // Since Chrome OS & macOS do this by default everywhere, we don't need to.
3374 // Linux & Windows seem to leave this to specific applications to manage.
3375 if (v === null)
3376 v = (hterm.os != 'cros' && hterm.os != 'mac');
3377
3378 this.mouseHideWhileTyping_ = !!v;
3379};
3380
3381/**
3382 * Handler for monitoring user keyboard activity.
3383 *
3384 * This isn't for processing the keystrokes directly, but for updating any
3385 * state that might toggle based on the user using the keyboard at all.
3386 *
3387 * @param {KeyboardEvent} e The keyboard event that triggered us.
3388 */
3389hterm.Terminal.prototype.onKeyboardActivity_ = function(e) {
3390 // When the user starts typing, hide the mouse cursor.
3391 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_)
3392 this.setCssVar('mouse-cursor-style', 'none');
3393};
Mike Frysinger70b94692017-01-26 18:57:50 -10003394
3395/**
rgindad5613292012-06-19 15:40:37 -07003396 * Add the terminalRow and terminalColumn properties to mouse events and
3397 * then forward on to onMouse().
3398 *
3399 * The terminalRow and terminalColumn properties contain the (row, column)
3400 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003401 *
3402 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003403 */
3404hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003405 if (e.processedByTerminalHandler_) {
3406 // We register our event handlers on the document, as well as the cursor
3407 // and the scroll blocker. Mouse events that occur on the cursor or
3408 // scroll blocker will also appear on the document, but we don't want to
3409 // process them twice.
3410 //
3411 // We can't just prevent bubbling because that has other side effects, so
3412 // we decorate the event object with this property instead.
3413 return;
3414 }
3415
Mike Frysinger468966c2018-08-28 13:48:51 -04003416 // Consume navigation events. Button 3 is usually "browser back" and
3417 // button 4 is "browser forward" which we don't want to happen.
3418 if (e.button > 2) {
3419 e.preventDefault();
3420 // We don't return so click events can be passed to the remote below.
3421 }
3422
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003423 var reportMouseEvents = (!this.defeatMouseReports_ &&
3424 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3425
rgindafaa74742012-08-21 13:34:03 -07003426 e.processedByTerminalHandler_ = true;
3427
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003428 // Handle auto hiding of mouse cursor while typing.
3429 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
3430 // Make sure the mouse cursor is visible.
3431 this.syncMouseStyle();
3432 // This debounce isn't perfect, but should work well enough for such a
3433 // simple implementation. If the user moved the mouse, we enabled this
3434 // debounce, and then moved the mouse just before the timeout, we wouldn't
3435 // debounce that later movement.
3436 this.mouseHideDelay_ = setTimeout(() => this.mouseHideDelay_ = null, 1000);
3437 }
3438
Robert Gindaeda48db2014-07-17 09:25:30 -07003439 // One based row/column stored on the mouse event.
3440 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
3441 this.scrollPort_.characterSize.height) + 1;
3442 e.terminalColumn = parseInt(e.clientX /
3443 this.scrollPort_.characterSize.width) + 1;
3444
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003445 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
3446 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07003447 return;
3448 }
3449
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003450 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07003451 // If the cursor is visible and we're not sending mouse events to the
3452 // host app, then we want to hide the terminal cursor when the mouse
3453 // cursor is over top. This keeps the terminal cursor from interfering
3454 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003455 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3456 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3457 this.cursorNode_.style.display = 'none';
3458 } else if (this.cursorNode_.style.display == 'none') {
3459 this.cursorNode_.style.display = '';
3460 }
3461 }
rgindad5613292012-06-19 15:40:37 -07003462
Robert Ginda928cf632014-03-05 15:07:41 -08003463 if (e.type == 'mousedown') {
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003464 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003465 // If VT mouse reporting is disabled, or has been defeated with
3466 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003467 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003468 this.setSelectionEnabled(true);
3469 } else {
3470 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003471 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003472 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003473 this.setSelectionEnabled(false);
3474 e.preventDefault();
3475 }
3476 }
3477
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003478 if (!reportMouseEvents) {
John Lin2aad22e2018-03-16 13:58:11 +08003479 if (e.type == 'dblclick') {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003480 this.screen_.expandSelection(this.document_.getSelection());
John Lin2aad22e2018-03-16 13:58:11 +08003481 if (this.copyOnSelect)
3482 this.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07003483 }
3484
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003485 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003486 // Debounce this event with the dblclick event. If you try to doubleclick
3487 // a URL to open it, Chrome will fire click then dblclick, but we won't
3488 // have expanded the selection text at the first click event.
3489 clearTimeout(this.timeouts_.openUrl);
3490 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3491 500);
3492 return;
3493 }
3494
Mike Frysinger847577f2017-05-23 23:25:57 -04003495 if (e.type == 'mousedown') {
3496 if ((this.mouseRightClickPaste && e.button == 2 /* right button */) ||
Mike Frysinger2edd3612017-05-24 00:54:39 -04003497 e.button == this.mousePasteButton) {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003498 if (!this.paste())
Mike Frysinger05a57f02017-08-27 17:48:55 -04003499 console.warn('Could not paste manually due to web restrictions');
Mike Frysinger847577f2017-05-23 23:25:57 -04003500 }
3501 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003502
Mike Frysinger2edd3612017-05-24 00:54:39 -04003503 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003504 !this.document_.getSelection().isCollapsed) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003505 this.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003506 }
3507
3508 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3509 this.scrollBlockerNode_.engaged) {
3510 // Disengage the scroll-blocker after one of these events.
3511 this.scrollBlockerNode_.engaged = false;
3512 this.scrollBlockerNode_.style.top = '-99px';
3513 }
3514
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003515 // Emulate arrow key presses via scroll wheel events.
3516 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3517 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003518 if (e.type == 'wheel') {
Mike Frysinger321063c2018-08-29 15:33:14 -04003519 const delta = this.scrollPort_.scrollWheelDelta(e);
Mike Frysingerc3030a82017-05-29 14:16:11 -04003520
Mike Frysinger321063c2018-08-29 15:33:14 -04003521 // Helper to turn a wheel event delta into a series of key presses.
3522 const deltaToArrows = (distance, charSize, arrowPos, arrowNeg) => {
3523 if (distance == 0) {
3524 return '';
3525 }
3526
3527 // Convert the scroll distance into a number of rows/cols.
3528 const cells = lib.f.smartFloorDivide(Math.abs(distance), charSize);
3529 const data = '\x1bO' + (distance < 0 ? arrowNeg : arrowPos);
3530 return data.repeat(cells);
3531 };
3532
3533 // The order between up/down and left/right doesn't really matter.
3534 this.io.sendString(
3535 // Up/down arrow keys.
3536 deltaToArrows(delta.y, this.scrollPort_.characterSize.height,
3537 'A', 'B') +
3538 // Left/right arrow keys.
3539 deltaToArrows(delta.x, this.scrollPort_.characterSize.width,
3540 'C', 'D')
3541 );
Mike Frysingerc3030a82017-05-29 14:16:11 -04003542
3543 e.preventDefault();
3544 }
3545 }
Robert Ginda928cf632014-03-05 15:07:41 -08003546 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003547 if (!this.scrollBlockerNode_.engaged) {
3548 if (e.type == 'mousedown') {
3549 // Move the scroll-blocker into place if we want to keep the scrollport
3550 // from scrolling.
3551 this.scrollBlockerNode_.engaged = true;
3552 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3553 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3554 } else if (e.type == 'mousemove') {
3555 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3556 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003557 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003558 e.preventDefault();
3559 }
3560 }
Robert Ginda928cf632014-03-05 15:07:41 -08003561
3562 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003563 }
3564
Robert Ginda928cf632014-03-05 15:07:41 -08003565 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3566 // Restore this on mouseup in case it was temporarily defeated with a
3567 // alt-mousedown. Only do this when the selection is empty so that
3568 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003569 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003570 }
rgindad5613292012-06-19 15:40:37 -07003571};
3572
3573/**
3574 * Clients should override this if they care to know about mouse events.
3575 *
3576 * The event parameter will be a normal DOM mouse click event with additional
3577 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003578 *
3579 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003580 */
3581hterm.Terminal.prototype.onMouse = function(e) { };
3582
3583/**
rginda8e92a692012-05-20 19:37:20 -07003584 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003585 *
3586 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003587 */
Rob Spies06533ba2014-04-24 11:20:37 -07003588hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3589 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003590 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003591
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003592 if (this.reportFocus)
3593 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O');
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003594
Michael Kelly485ecd12014-06-09 11:41:56 -04003595 if (focused === true)
3596 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07003597};
3598
3599/**
rginda8ba33642011-12-14 12:31:31 -08003600 * React when the ScrollPort is scrolled.
3601 */
3602hterm.Terminal.prototype.onScroll_ = function() {
3603 this.scheduleSyncCursorPosition_();
3604};
3605
3606/**
rginda9846e2f2012-01-27 13:53:33 -08003607 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003608 *
3609 * @param {Event} e The DOM paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08003610 */
3611hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaa063b202014-07-21 11:08:25 -07003612 var data = e.text.replace(/\n/mg, '\r');
Connor Hegartyf525ceb2014-09-03 13:36:03 -07003613 data = this.keyboard.encode(data);
Mike Frysingere8c32c82018-03-11 14:57:28 -07003614 if (this.options_.bracketedPaste) {
3615 // We strip out most escape sequences as they can cause issues (like
3616 // inserting an \x1b[201~ midstream). We pass through whitespace
3617 // though: 0x08:\b 0x09:\t 0x0a:\n 0x0d:\r.
3618 // This matches xterm behavior.
3619 const filter = (data) => data.replace(/[\x00-\x07\x0b-\x0c\x0e-\x1f]/g, '');
3620 data = '\x1b[200~' + filter(data) + '\x1b[201~';
3621 }
Robert Gindaa063b202014-07-21 11:08:25 -07003622
3623 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003624};
3625
3626/**
rgindaa09e7332012-08-17 12:49:51 -07003627 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003628 *
3629 * @param {Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07003630 */
3631hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07003632 if (!this.useDefaultWindowCopy) {
3633 e.preventDefault();
3634 setTimeout(this.copySelectionToClipboard.bind(this), 0);
3635 }
rgindaa09e7332012-08-17 12:49:51 -07003636};
3637
3638/**
rginda8ba33642011-12-14 12:31:31 -08003639 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08003640 *
3641 * Note: This function should not directly contain code that alters the internal
3642 * state of the terminal. That kind of code belongs in realizeWidth or
3643 * realizeHeight, so that it can be executed synchronously in the case of a
3644 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08003645 */
3646hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08003647 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07003648 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08003649 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07003650 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08003651
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003652 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08003653 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003654 // gets removed from the document or during the initial load, and we can't
3655 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07003656 // This can also happen if called before the scrollPort calculates the
3657 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08003658 return;
3659 }
3660
rgindaa8ba17d2012-08-15 14:41:10 -07003661 var isNewSize = (columnCount != this.screenSize.width ||
3662 rowCount != this.screenSize.height);
3663
3664 // We do this even if the size didn't change, just to be sure everything is
3665 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04003666 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07003667 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07003668
3669 if (isNewSize)
3670 this.overlaySize();
3671
Robert Gindafb1be6a2013-12-11 11:56:22 -08003672 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07003673 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08003674};
3675
3676/**
3677 * Service the cursor blink timeout.
3678 */
3679hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07003680 if (!this.options_.cursorBlink) {
3681 delete this.timeouts_.cursorBlink;
3682 return;
3683 }
3684
Robert Ginda830583c2013-08-07 13:20:46 -07003685 if (this.cursorNode_.getAttribute('focus') == 'false' ||
3686 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08003687 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07003688 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3689 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08003690 } else {
rginda87b86462011-12-14 13:48:03 -08003691 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07003692 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3693 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08003694 }
3695};
David Reveman8f552492012-03-28 12:18:41 -04003696
3697/**
3698 * Set the scrollbar-visible mode bit.
3699 *
3700 * If scrollbar-visible is on, the vertical scrollbar will be visible.
3701 * Otherwise it will not.
3702 *
3703 * Defaults to on.
3704 *
3705 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
3706 */
3707hterm.Terminal.prototype.setScrollbarVisible = function(state) {
3708 this.scrollPort_.setScrollbarVisible(state);
3709};
Michael Kelly485ecd12014-06-09 11:41:56 -04003710
3711/**
Rob Spies49039e52014-12-17 13:40:04 -08003712 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04003713 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08003714 *
3715 * Defaults to 1.
3716 *
Evan Jones2600d4f2016-12-06 09:29:36 -05003717 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08003718 */
3719hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3720 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3721};
3722
3723/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003724 * Close all web notifications created by terminal bells.
3725 */
3726hterm.Terminal.prototype.closeBellNotifications_ = function() {
3727 this.bellNotificationList_.forEach(function(n) {
3728 n.close();
3729 });
3730 this.bellNotificationList_.length = 0;
3731};
Raymes Khourye5d48982018-08-02 09:08:32 +10003732
3733/**
3734 * Syncs the cursor position when the scrollport gains focus.
3735 */
3736hterm.Terminal.prototype.onScrollportFocus_ = function() {
3737 // If the cursor is offscreen we set selection to the last row on the screen.
3738 const topRowIndex = this.scrollPort_.getTopRowIndex();
3739 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
3740 const selection = this.document_.getSelection();
3741 if (!this.syncCursorPosition_() && selection) {
3742 selection.collapse(this.getRowNode(bottomRowIndex));
3743 }
3744};