blob: d1e535814fa63b3e4b3cac1081fe934f657c8108 [file] [log] [blame]
rginda87b86462011-12-14 13:48:03 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rginda8ba33642011-12-14 12:31:31 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rgindacbbd7482012-06-13 15:06:16 -07005'use strict';
6
Masaya Suzuki273aa982014-05-31 07:25:55 +09007lib.rtdep('lib.colors', 'lib.PreferenceManager', 'lib.resource', 'lib.wc',
Raymes Khoury3e44bc92018-05-17 10:54:23 +10008 'lib.f', 'hterm.AccessibilityReader', 'hterm.Keyboard',
9 'hterm.Options', 'hterm.PreferenceManager', 'hterm.Screen',
10 'hterm.ScrollPort', 'hterm.Size', 'hterm.TextAttributes', 'hterm.VT');
rgindacbbd7482012-06-13 15:06:16 -070011
rginda8ba33642011-12-14 12:31:31 -080012/**
13 * Constructor for the Terminal class.
14 *
15 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
16 * classes to provide the complete terminal functionality.
17 *
18 * There are a number of lower-level Terminal methods that can be called
19 * directly to manipulate the cursor, text, scroll region, and other terminal
20 * attributes. However, the primary method is interpret(), which parses VT
21 * escape sequences and invokes the appropriate Terminal methods.
22 *
23 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
24 *
25 * TODO(rginda): Eventually we're going to need to support characters which are
26 * displayed twice as wide as standard latin characters. This is to support
27 * CJK (and possibly other character sets).
rginda9f5222b2012-03-05 11:53:28 -080028 *
Robert Ginda57f03b42012-09-13 11:02:48 -070029 * @param {string} opt_profileId Optional preference profile name. If not
rginda9f5222b2012-03-05 11:53:28 -080030 * provided, defaults to 'default'.
rginda8ba33642011-12-14 12:31:31 -080031 */
Robert Ginda57f03b42012-09-13 11:02:48 -070032hterm.Terminal = function(opt_profileId) {
33 this.profileId_ = null;
rginda9f5222b2012-03-05 11:53:28 -080034
rginda8ba33642011-12-14 12:31:31 -080035 // Two screen instances.
36 this.primaryScreen_ = new hterm.Screen();
37 this.alternateScreen_ = new hterm.Screen();
38
39 // The "current" screen.
40 this.screen_ = this.primaryScreen_;
41
rginda8ba33642011-12-14 12:31:31 -080042 // The local notion of the screen size. ScreenBuffers also have a size which
43 // indicates their present size. During size changes, the two may disagree.
44 // Also, the inactive screen's size is not altered until it is made the active
45 // screen.
46 this.screenSize = new hterm.Size(0, 0);
47
rginda8ba33642011-12-14 12:31:31 -080048 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080049 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080050 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
51 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080052 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
Raymes Khourye5d48982018-08-02 09:08:32 +100053 this.scrollPort_.subscribe('focus', this.onScrollportFocus_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070054 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080055
rginda87b86462011-12-14 13:48:03 -080056 // The div that contains this terminal.
57 this.div_ = null;
58
rgindac9bc5502012-01-18 11:48:44 -080059 // The document that contains the scrollPort. Defaulted to the global
60 // document here so that the terminal is functional even if it hasn't been
61 // inserted into a document yet, but re-set in decorate().
62 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080063
rginda8ba33642011-12-14 12:31:31 -080064 // The rows that have scrolled off screen and are no longer addressable.
65 this.scrollbackRows_ = [];
66
rgindac9bc5502012-01-18 11:48:44 -080067 // Saved tab stops.
68 this.tabStops_ = [];
69
David Benjamin66e954d2012-05-05 21:08:12 -040070 // Keep track of whether default tab stops have been erased; after a TBC
71 // clears all tab stops, defaults aren't restored on resize until a reset.
72 this.defaultTabStops = true;
73
rginda8ba33642011-12-14 12:31:31 -080074 // The VT's notion of the top and bottom rows. Used during some VT
75 // cursor positioning and scrolling commands.
76 this.vtScrollTop_ = null;
77 this.vtScrollBottom_ = null;
78
79 // The DIV element for the visible cursor.
80 this.cursorNode_ = null;
81
Robert Ginda830583c2013-08-07 13:20:46 -070082 // The current cursor shape of the terminal.
83 this.cursorShape_ = hterm.Terminal.cursorShape.BLOCK;
84
Robert Gindaea2183e2014-07-17 09:51:51 -070085 // Cursor blink on/off cycle in ms, overwritten by prefs once they're loaded.
86 this.cursorBlinkCycle_ = [100, 100];
87
88 // Pre-bound onCursorBlink_ handler, so we don't have to do this for each
89 // cursor on/off servicing.
90 this.myOnCursorBlink_ = this.onCursorBlink_.bind(this);
91
rginda9f5222b2012-03-05 11:53:28 -080092 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -070093 // each output and keystroke. They are initialized by the preference manager.
Robert Ginda8cb7d902013-06-20 14:37:18 -070094 this.backgroundColor_ = null;
95 this.foregroundColor_ = null;
Robert Ginda57f03b42012-09-13 11:02:48 -070096 this.scrollOnOutput_ = null;
97 this.scrollOnKeystroke_ = null;
Mike Frysinger3c9fa072017-07-13 10:21:13 -040098 this.scrollWheelArrowKeys_ = null;
rginda9f5222b2012-03-05 11:53:28 -080099
Robert Ginda6aec7eb2015-06-16 10:31:30 -0700100 // True if we should override mouse event reporting to allow local selection.
101 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -0800102
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400103 // Whether to auto hide the mouse cursor when typing.
104 this.setAutomaticMouseHiding();
105 // Timer to keep mouse visible while it's being used.
106 this.mouseHideDelay_ = null;
107
rgindaf0090c92012-02-10 14:58:52 -0800108 // Terminal bell sound.
109 this.bellAudio_ = this.document_.createElement('audio');
Mike Frysingerd826f1a2017-07-06 16:20:06 -0400110 this.bellAudio_.id = 'hterm:bell-audio';
rgindaf0090c92012-02-10 14:58:52 -0800111 this.bellAudio_.setAttribute('preload', 'auto');
112
Raymes Khoury3e44bc92018-05-17 10:54:23 +1000113 // The AccessibilityReader object for announcing command output.
114 this.accessibilityReader_ = null;
115
Mike Frysingercc114512017-09-11 21:39:17 -0400116 // The context menu object.
117 this.contextMenu = new hterm.ContextMenu();
118
Michael Kelly485ecd12014-06-09 11:41:56 -0400119 // All terminal bell notifications that have been generated (not necessarily
120 // shown).
121 this.bellNotificationList_ = [];
122
123 // Whether we have permission to display notifications.
124 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400125
rginda6d397402012-01-17 10:58:29 -0800126 // Cursor position and attributes saved with DECSC.
127 this.savedOptions_ = {};
128
rginda8ba33642011-12-14 12:31:31 -0800129 // The current mode bits for the terminal.
130 this.options_ = new hterm.Options();
131
132 // Timeouts we might need to clear.
133 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800134
135 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800136 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800137
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800138 this.saveCursorAndState(true);
139
Zhu Qunying30d40712017-03-14 16:27:00 -0700140 // The keyboard handler.
rgindafeaf3142012-01-31 15:14:20 -0800141 this.keyboard = new hterm.Keyboard(this);
142
rginda87b86462011-12-14 13:48:03 -0800143 // General IO interface that can be given to third parties without exposing
144 // the entire terminal object.
145 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800146
rgindad5613292012-06-19 15:40:37 -0700147 // True if mouse-click-drag should scroll the terminal.
148 this.enableMouseDragScroll = true;
149
Robert Ginda57f03b42012-09-13 11:02:48 -0700150 this.copyOnSelect = null;
Mike Frysinger847577f2017-05-23 23:25:57 -0400151 this.mouseRightClickPaste = null;
rginda4bba5e12012-06-20 16:15:30 -0700152 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700153
Zhu Qunying30d40712017-03-14 16:27:00 -0700154 // Whether to use the default window copy behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700155 this.useDefaultWindowCopy = false;
156
157 this.clearSelectionAfterCopy = true;
158
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400159 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800160 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700161
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400162 // Whether we allow images to be shown.
163 this.allowImagesInline = null;
164
Gabriel Holodake8a09be2017-10-10 01:07:11 -0400165 this.reportFocus = false;
166
Robert Ginda57f03b42012-09-13 11:02:48 -0700167 this.setProfile(opt_profileId || 'default',
Evan Jones5f9df812016-12-06 09:38:58 -0500168 function() { this.onTerminalReady(); }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800169};
170
171/**
Robert Ginda830583c2013-08-07 13:20:46 -0700172 * Possible cursor shapes.
173 */
174hterm.Terminal.cursorShape = {
175 BLOCK: 'BLOCK',
176 BEAM: 'BEAM',
177 UNDERLINE: 'UNDERLINE'
178};
179
180/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700181 * Clients should override this to be notified when the terminal is ready
182 * for use.
183 *
184 * The terminal initialization is asynchronous, and shouldn't be used before
185 * this method is called.
186 */
187hterm.Terminal.prototype.onTerminalReady = function() { };
188
189/**
rginda35c456b2012-02-09 17:29:05 -0800190 * Default tab with of 8 to match xterm.
191 */
192hterm.Terminal.prototype.tabWidth = 8;
193
194/**
rginda9f5222b2012-03-05 11:53:28 -0800195 * Select a preference profile.
196 *
197 * This will load the terminal preferences for the given profile name and
198 * associate subsequent preference changes with the new preference profile.
199 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500200 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800201 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700202 * @param {function} opt_callback Optional callback to invoke when the profile
203 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800204 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700205hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
206 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800207
Robert Ginda57f03b42012-09-13 11:02:48 -0700208 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800209
Robert Ginda57f03b42012-09-13 11:02:48 -0700210 if (this.prefs_)
211 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800212
Robert Ginda57f03b42012-09-13 11:02:48 -0700213 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
214 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800215 'alt-gr-mode': function(v) {
216 if (v == null) {
217 if (navigator.language.toLowerCase() == 'en-us') {
218 v = 'none';
219 } else {
220 v = 'right-alt';
221 }
222 } else if (typeof v == 'string') {
223 v = v.toLowerCase();
224 } else {
225 v = 'none';
226 }
227
228 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v))
229 v = 'none';
230
231 terminal.keyboard.altGrMode = v;
232 },
233
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700234 'alt-backspace-is-meta-backspace': function(v) {
235 terminal.keyboard.altBackspaceIsMetaBackspace = v;
236 },
237
Robert Ginda57f03b42012-09-13 11:02:48 -0700238 'alt-is-meta': function(v) {
239 terminal.keyboard.altIsMeta = v;
240 },
241
242 'alt-sends-what': function(v) {
243 if (!/^(escape|8-bit|browser-key)$/.test(v))
244 v = 'escape';
245
246 terminal.keyboard.altSendsWhat = v;
247 },
248
249 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800250 var ary = v.match(/^lib-resource:(\S+)/);
251 if (ary) {
252 terminal.bellAudio_.setAttribute('src',
253 lib.resource.getDataUrl(ary[1]));
254 } else {
255 terminal.bellAudio_.setAttribute('src', v);
256 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700257 },
258
Michael Kelly485ecd12014-06-09 11:41:56 -0400259 'desktop-notification-bell': function(v) {
260 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700261 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400262 Notification.permission === 'granted';
263 if (!terminal.desktopNotificationBell_) {
264 // Note: We don't call Notification.requestPermission here because
265 // Chrome requires the call be the result of a user action (such as an
266 // onclick handler), and pref listeners are run asynchronously.
267 //
268 // A way of working around this would be to display a dialog in the
269 // terminal with a "click-to-request-permission" button.
270 console.warn('desktop-notification-bell is true but we do not have ' +
271 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400272 }
273 } else {
274 terminal.desktopNotificationBell_ = false;
275 }
276 },
277
Robert Ginda57f03b42012-09-13 11:02:48 -0700278 'background-color': function(v) {
279 terminal.setBackgroundColor(v);
280 },
281
282 'background-image': function(v) {
283 terminal.scrollPort_.setBackgroundImage(v);
284 },
285
286 'background-size': function(v) {
287 terminal.scrollPort_.setBackgroundSize(v);
288 },
289
290 'background-position': function(v) {
291 terminal.scrollPort_.setBackgroundPosition(v);
292 },
293
294 'backspace-sends-backspace': function(v) {
295 terminal.keyboard.backspaceSendsBackspace = v;
296 },
297
Brad Town18654b62015-03-12 00:27:45 -0700298 'character-map-overrides': function(v) {
299 if (!(v == null || v instanceof Object)) {
300 console.warn('Preference character-map-modifications is not an ' +
301 'object: ' + v);
302 return;
303 }
304
Mike Frysinger095d4062017-06-14 00:29:48 -0700305 terminal.vt.characterMaps.reset();
306 terminal.vt.characterMaps.setOverrides(v);
Brad Town18654b62015-03-12 00:27:45 -0700307 },
308
Robert Ginda57f03b42012-09-13 11:02:48 -0700309 'cursor-blink': function(v) {
310 terminal.setCursorBlink(!!v);
311 },
312
Robert Gindaea2183e2014-07-17 09:51:51 -0700313 'cursor-blink-cycle': function(v) {
314 if (v instanceof Array &&
315 typeof v[0] == 'number' &&
316 typeof v[1] == 'number') {
317 terminal.cursorBlinkCycle_ = v;
318 } else if (typeof v == 'number') {
319 terminal.cursorBlinkCycle_ = [v, v];
320 } else {
321 // Fast blink indicates an error.
322 terminal.cursorBlinkCycle_ = [100, 100];
323 }
324 },
325
Robert Ginda57f03b42012-09-13 11:02:48 -0700326 'cursor-color': function(v) {
327 terminal.setCursorColor(v);
328 },
329
330 'color-palette-overrides': function(v) {
331 if (!(v == null || v instanceof Object || v instanceof Array)) {
332 console.warn('Preference color-palette-overrides is not an array or ' +
333 'object: ' + v);
334 return;
rginda9f5222b2012-03-05 11:53:28 -0800335 }
rginda9f5222b2012-03-05 11:53:28 -0800336
Robert Ginda57f03b42012-09-13 11:02:48 -0700337 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700338
Robert Ginda57f03b42012-09-13 11:02:48 -0700339 if (v) {
340 for (var key in v) {
341 var i = parseInt(key);
342 if (isNaN(i) || i < 0 || i > 255) {
343 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
344 continue;
345 }
346
347 if (v[i]) {
348 var rgb = lib.colors.normalizeCSS(v[i]);
349 if (rgb)
350 lib.colors.colorPalette[i] = rgb;
351 }
352 }
rginda30f20f62012-04-05 16:36:19 -0700353 }
rginda30f20f62012-04-05 16:36:19 -0700354
Evan Jones5f9df812016-12-06 09:38:58 -0500355 terminal.primaryScreen_.textAttributes.resetColorPalette();
Robert Ginda57f03b42012-09-13 11:02:48 -0700356 terminal.alternateScreen_.textAttributes.resetColorPalette();
357 },
rginda30f20f62012-04-05 16:36:19 -0700358
Robert Ginda57f03b42012-09-13 11:02:48 -0700359 'copy-on-select': function(v) {
360 terminal.copyOnSelect = !!v;
361 },
rginda9f5222b2012-03-05 11:53:28 -0800362
Rob Spies0bec09b2014-06-06 15:58:09 -0700363 'use-default-window-copy': function(v) {
364 terminal.useDefaultWindowCopy = !!v;
365 },
366
367 'clear-selection-after-copy': function(v) {
368 terminal.clearSelectionAfterCopy = !!v;
369 },
370
Robert Ginda7e5e9522014-03-14 12:23:58 -0700371 'ctrl-plus-minus-zero-zoom': function(v) {
372 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
373 },
374
Robert Gindafb5a3f92014-05-13 14:12:00 -0700375 'ctrl-c-copy': function(v) {
376 terminal.keyboard.ctrlCCopy = v;
377 },
378
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100379 'ctrl-v-paste': function(v) {
380 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700381 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100382 },
383
Cody Coljee-Gray7c6a0392018-10-25 13:18:28 -0700384 'paste-on-drop': function(v) {
385 terminal.scrollPort_.setPasteOnDrop(v);
386 },
387
Masaya Suzuki273aa982014-05-31 07:25:55 +0900388 'east-asian-ambiguous-as-two-column': function(v) {
389 lib.wc.regardCjkAmbiguous = v;
390 },
391
Robert Ginda57f03b42012-09-13 11:02:48 -0700392 'enable-8-bit-control': function(v) {
393 terminal.vt.enable8BitControl = !!v;
394 },
rginda30f20f62012-04-05 16:36:19 -0700395
Robert Ginda57f03b42012-09-13 11:02:48 -0700396 'enable-bold': function(v) {
397 terminal.syncBoldSafeState();
398 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400399
Robert Ginda3e278d72014-03-25 13:18:51 -0700400 'enable-bold-as-bright': function(v) {
401 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
402 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
403 },
404
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400405 'enable-blink': function(v) {
Mike Frysinger261597c2017-12-28 01:14:21 -0500406 terminal.setTextBlink(!!v);
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400407 },
408
Robert Ginda57f03b42012-09-13 11:02:48 -0700409 'enable-clipboard-write': function(v) {
410 terminal.vt.enableClipboardWrite = !!v;
411 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400412
Robert Ginda3755e752013-05-31 13:34:09 -0700413 'enable-dec12': function(v) {
414 terminal.vt.enableDec12 = !!v;
415 },
416
Mike Frysinger38f267d2018-09-07 02:50:59 -0400417 'enable-csi-j-3': function(v) {
418 terminal.vt.enableCsiJ3 = !!v;
419 },
420
Robert Ginda57f03b42012-09-13 11:02:48 -0700421 'font-family': function(v) {
422 terminal.syncFontFamily();
423 },
rginda30f20f62012-04-05 16:36:19 -0700424
Robert Ginda57f03b42012-09-13 11:02:48 -0700425 'font-size': function(v) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500426 v = parseInt(v);
427 if (v <= 0) {
428 console.error(`Invalid font size: ${v}`);
429 return;
430 }
431
Robert Ginda57f03b42012-09-13 11:02:48 -0700432 terminal.setFontSize(v);
433 },
rginda9875d902012-08-20 16:21:57 -0700434
Robert Ginda57f03b42012-09-13 11:02:48 -0700435 'font-smoothing': function(v) {
436 terminal.syncFontFamily();
437 },
rgindade84e382012-04-20 15:39:31 -0700438
Robert Ginda57f03b42012-09-13 11:02:48 -0700439 'foreground-color': function(v) {
440 terminal.setForegroundColor(v);
441 },
rginda30f20f62012-04-05 16:36:19 -0700442
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400443 'hide-mouse-while-typing': function(v) {
444 terminal.setAutomaticMouseHiding(v);
445 },
446
Robert Ginda57f03b42012-09-13 11:02:48 -0700447 'home-keys-scroll': function(v) {
448 terminal.keyboard.homeKeysScroll = v;
449 },
rginda4bba5e12012-06-20 16:15:30 -0700450
Robert Gindaa8165692015-06-15 14:46:31 -0700451 'keybindings': function(v) {
452 terminal.keyboard.bindings.clear();
453
454 if (!v)
455 return;
456
457 if (!(v instanceof Object)) {
458 console.error('Error in keybindings preference: Expected object');
459 return;
460 }
461
462 try {
463 terminal.keyboard.bindings.addBindings(v);
464 } catch (ex) {
465 console.error('Error in keybindings preference: ' + ex);
466 }
467 },
468
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700469 'media-keys-are-fkeys': function(v) {
470 terminal.keyboard.mediaKeysAreFKeys = v;
471 },
472
Robert Ginda57f03b42012-09-13 11:02:48 -0700473 'meta-sends-escape': function(v) {
474 terminal.keyboard.metaSendsEscape = v;
475 },
rginda30f20f62012-04-05 16:36:19 -0700476
Mike Frysinger847577f2017-05-23 23:25:57 -0400477 'mouse-right-click-paste': function(v) {
478 terminal.mouseRightClickPaste = v;
479 },
480
Robert Ginda57f03b42012-09-13 11:02:48 -0700481 'mouse-paste-button': function(v) {
482 terminal.syncMousePasteButton();
483 },
rgindaa8ba17d2012-08-15 14:41:10 -0700484
Robert Gindae76aa9f2014-03-14 12:29:12 -0700485 'page-keys-scroll': function(v) {
486 terminal.keyboard.pageKeysScroll = v;
487 },
488
Robert Ginda40932892012-12-10 17:26:40 -0800489 'pass-alt-number': function(v) {
490 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800491 // Let Alt-1..9 pass to the browser (to control tab switching) on
492 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500493 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800494 }
495
496 terminal.passAltNumber = v;
497 },
498
499 'pass-ctrl-number': function(v) {
500 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800501 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
502 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500503 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800504 }
505
506 terminal.passCtrlNumber = v;
507 },
508
509 'pass-meta-number': function(v) {
510 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800511 // Let Meta-1..9 pass to the browser (to control tab switching) on
512 // OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500513 v = (hterm.os == 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800514 }
515
516 terminal.passMetaNumber = v;
517 },
518
Marius Schilder77857b32014-05-14 16:21:26 -0700519 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700520 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700521 },
522
Robert Ginda8cb7d902013-06-20 14:37:18 -0700523 'receive-encoding': function(v) {
524 if (!(/^(utf-8|raw)$/).test(v)) {
525 console.warn('Invalid value for "receive-encoding": ' + v);
526 v = 'utf-8';
527 }
528
529 terminal.vt.characterEncoding = v;
530 },
531
Robert Ginda57f03b42012-09-13 11:02:48 -0700532 'scroll-on-keystroke': function(v) {
533 terminal.scrollOnKeystroke_ = v;
534 },
rginda9f5222b2012-03-05 11:53:28 -0800535
Robert Ginda57f03b42012-09-13 11:02:48 -0700536 'scroll-on-output': function(v) {
537 terminal.scrollOnOutput_ = v;
538 },
rginda30f20f62012-04-05 16:36:19 -0700539
Robert Ginda57f03b42012-09-13 11:02:48 -0700540 'scrollbar-visible': function(v) {
541 terminal.setScrollbarVisible(v);
542 },
rginda9f5222b2012-03-05 11:53:28 -0800543
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400544 'scroll-wheel-may-send-arrow-keys': function(v) {
545 terminal.scrollWheelArrowKeys_ = v;
546 },
547
Rob Spies49039e52014-12-17 13:40:04 -0800548 'scroll-wheel-move-multiplier': function(v) {
549 terminal.setScrollWheelMoveMultipler(v);
550 },
551
Robert Ginda57f03b42012-09-13 11:02:48 -0700552 'shift-insert-paste': function(v) {
553 terminal.keyboard.shiftInsertPaste = v;
554 },
rginda9f5222b2012-03-05 11:53:28 -0800555
Mike Frysingera7768922017-07-28 15:00:12 -0400556 'terminal-encoding': function(v) {
Mike Frysingera1371e12017-08-17 01:37:17 -0400557 terminal.vt.setEncoding(v);
Mike Frysingera7768922017-07-28 15:00:12 -0400558 },
559
Robert Gindae76aa9f2014-03-14 12:29:12 -0700560 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400561 terminal.scrollPort_.setUserCssUrl(v);
562 },
563
564 'user-css-text': function(v) {
565 terminal.scrollPort_.setUserCssText(v);
566 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400567
568 'word-break-match-left': function(v) {
569 terminal.primaryScreen_.wordBreakMatchLeft = v;
570 terminal.alternateScreen_.wordBreakMatchLeft = v;
571 },
572
573 'word-break-match-right': function(v) {
574 terminal.primaryScreen_.wordBreakMatchRight = v;
575 terminal.alternateScreen_.wordBreakMatchRight = v;
576 },
577
578 'word-break-match-middle': function(v) {
579 terminal.primaryScreen_.wordBreakMatchMiddle = v;
580 terminal.alternateScreen_.wordBreakMatchMiddle = v;
581 },
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400582
583 'allow-images-inline': function(v) {
584 terminal.allowImagesInline = v;
585 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700586 });
rginda30f20f62012-04-05 16:36:19 -0700587
Robert Ginda57f03b42012-09-13 11:02:48 -0700588 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800589 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700590
591 if (opt_callback)
592 opt_callback();
593 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800594};
595
Rob Spies56953412014-04-28 14:09:47 -0700596
597/**
598 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500599 *
600 * @return {hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700601 */
602hterm.Terminal.prototype.getPrefs = function() {
603 return this.prefs_;
604};
605
Robert Gindaa063b202014-07-21 11:08:25 -0700606/**
607 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500608 *
609 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700610 */
611hterm.Terminal.prototype.setBracketedPaste = function(state) {
612 this.options_.bracketedPaste = state;
613};
Rob Spies56953412014-04-28 14:09:47 -0700614
rginda8e92a692012-05-20 19:37:20 -0700615/**
616 * Set the color for the cursor.
617 *
618 * If you want this setting to persist, set it through prefs_, rather than
619 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500620 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500621 * @param {string=} color The color to set. If not defined, we reset to the
622 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700623 */
624hterm.Terminal.prototype.setCursorColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500625 if (color === undefined)
626 color = this.prefs_.get('cursor-color');
627
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400628 this.setCssVar('cursor-color', color);
rginda8e92a692012-05-20 19:37:20 -0700629};
630
631/**
632 * Return the current cursor color as a string.
Evan Jones2600d4f2016-12-06 09:29:36 -0500633 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700634 */
635hterm.Terminal.prototype.getCursorColor = function() {
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400636 return this.getCssVar('cursor-color');
rginda8e92a692012-05-20 19:37:20 -0700637};
638
639/**
rgindad5613292012-06-19 15:40:37 -0700640 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500641 *
642 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700643 */
644hterm.Terminal.prototype.setSelectionEnabled = function(state) {
645 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700646};
647
648/**
rginda8e92a692012-05-20 19:37:20 -0700649 * Set the background color.
650 *
651 * If you want this setting to persist, set it through prefs_, rather than
652 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500653 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500654 * @param {string=} color The color to set. If not defined, we reset to the
655 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700656 */
657hterm.Terminal.prototype.setBackgroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500658 if (color === undefined)
659 color = this.prefs_.get('background-color');
660
rgindacbbd7482012-06-13 15:06:16 -0700661 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700662 this.primaryScreen_.textAttributes.setDefaults(
663 this.foregroundColor_, this.backgroundColor_);
664 this.alternateScreen_.textAttributes.setDefaults(
665 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700666 this.scrollPort_.setBackgroundColor(color);
667};
668
rginda9f5222b2012-03-05 11:53:28 -0800669/**
670 * Return the current terminal background color.
671 *
672 * Intended for use by other classes, so we don't have to expose the entire
673 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500674 *
675 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800676 */
677hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700678 return this.backgroundColor_;
679};
680
681/**
682 * Set the foreground color.
683 *
684 * If you want this setting to persist, set it through prefs_, rather than
685 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500686 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500687 * @param {string=} color The color to set. If not defined, we reset to the
688 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700689 */
690hterm.Terminal.prototype.setForegroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500691 if (color === undefined)
692 color = this.prefs_.get('foreground-color');
693
rgindacbbd7482012-06-13 15:06:16 -0700694 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700695 this.primaryScreen_.textAttributes.setDefaults(
696 this.foregroundColor_, this.backgroundColor_);
697 this.alternateScreen_.textAttributes.setDefaults(
698 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700699 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800700};
701
702/**
703 * Return the current terminal foreground color.
704 *
705 * Intended for use by other classes, so we don't have to expose the entire
706 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500707 *
708 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800709 */
710hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700711 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800712};
713
714/**
rginda87b86462011-12-14 13:48:03 -0800715 * Create a new instance of a terminal command and run it with a given
716 * argument string.
717 *
718 * @param {function} commandClass The constructor for a terminal command.
719 * @param {string} argString The argument string to pass to the command.
720 */
721hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700722 var environment = this.prefs_.get('environment');
723 if (typeof environment != 'object' || environment == null)
724 environment = {};
725
rginda87b86462011-12-14 13:48:03 -0800726 var self = this;
727 this.command = new commandClass(
728 { argString: argString || '',
729 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700730 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800731 onExit: function(code) {
732 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800733 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700734 if (self.prefs_.get('close-on-exit'))
735 window.close();
rginda87b86462011-12-14 13:48:03 -0800736 }
737 });
738
rgindafeaf3142012-01-31 15:14:20 -0800739 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800740 this.command.run();
741};
742
743/**
rgindafeaf3142012-01-31 15:14:20 -0800744 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500745 *
746 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800747 */
748hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700749 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800750};
751
752/**
753 * Install the keyboard handler for this terminal.
754 *
755 * This will prevent the browser from seeing any keystrokes sent to the
756 * terminal.
757 */
758hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700759 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400760};
rgindafeaf3142012-01-31 15:14:20 -0800761
762/**
763 * Uninstall the keyboard handler for this terminal.
764 */
765hterm.Terminal.prototype.uninstallKeyboard = function() {
766 this.keyboard.installKeyboard(null);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400767};
rgindafeaf3142012-01-31 15:14:20 -0800768
769/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400770 * Set a CSS variable.
771 *
772 * Normally this is used to set variables in the hterm namespace.
773 *
774 * @param {string} name The variable to set.
775 * @param {string} value The value to assign to the variable.
776 * @param {string?} opt_prefix The variable namespace/prefix to use.
777 */
778hterm.Terminal.prototype.setCssVar = function(name, value,
779 opt_prefix='--hterm-') {
780 this.document_.documentElement.style.setProperty(
781 `${opt_prefix}${name}`, value);
782};
783
784/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500785 * Get a CSS variable.
786 *
787 * Normally this is used to get variables in the hterm namespace.
788 *
789 * @param {string} name The variable to read.
790 * @param {string?} opt_prefix The variable namespace/prefix to use.
791 * @return {string} The current setting for this variable.
792 */
793hterm.Terminal.prototype.getCssVar = function(name, opt_prefix='--hterm-') {
794 return this.document_.documentElement.style.getPropertyValue(
795 `${opt_prefix}${name}`);
796};
797
798/**
rginda35c456b2012-02-09 17:29:05 -0800799 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800800 *
801 * Call setFontSize(0) to reset to the default font size.
802 *
803 * This function does not modify the font-size preference.
804 *
805 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800806 */
807hterm.Terminal.prototype.setFontSize = function(px) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500808 if (px <= 0)
rginda9f5222b2012-03-05 11:53:28 -0800809 px = this.prefs_.get('font-size');
810
rginda35c456b2012-02-09 17:29:05 -0800811 this.scrollPort_.setFontSize(px);
Mike Frysingercce97c42017-08-05 01:11:22 -0400812 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
813 this.setCssVar('charsize-height',
814 this.scrollPort_.characterSize.height + 'px');
rginda35c456b2012-02-09 17:29:05 -0800815};
816
817/**
818 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500819 *
820 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800821 */
822hterm.Terminal.prototype.getFontSize = function() {
823 return this.scrollPort_.getFontSize();
824};
825
826/**
rginda8e92a692012-05-20 19:37:20 -0700827 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500828 *
829 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700830 */
831hterm.Terminal.prototype.getFontFamily = function() {
832 return this.scrollPort_.getFontFamily();
833};
834
835/**
rginda35c456b2012-02-09 17:29:05 -0800836 * Set the CSS "font-family" for this terminal.
837 */
rginda9f5222b2012-03-05 11:53:28 -0800838hterm.Terminal.prototype.syncFontFamily = function() {
839 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
840 this.prefs_.get('font-smoothing'));
841 this.syncBoldSafeState();
842};
843
rginda4bba5e12012-06-20 16:15:30 -0700844/**
845 * Set this.mousePasteButton based on the mouse-paste-button pref,
846 * autodetecting if necessary.
847 */
848hterm.Terminal.prototype.syncMousePasteButton = function() {
849 var button = this.prefs_.get('mouse-paste-button');
850 if (typeof button == 'number') {
851 this.mousePasteButton = button;
852 return;
853 }
854
Mike Frysingeree81a002017-12-12 16:14:53 -0500855 if (hterm.os != 'linux') {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400856 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700857 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400858 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700859 }
860};
861
862/**
863 * Enable or disable bold based on the enable-bold pref, autodetecting if
864 * necessary.
865 */
rginda9f5222b2012-03-05 11:53:28 -0800866hterm.Terminal.prototype.syncBoldSafeState = function() {
867 var enableBold = this.prefs_.get('enable-bold');
868 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700869 this.primaryScreen_.textAttributes.enableBold = enableBold;
870 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800871 return;
872 }
873
rgindaf7521392012-02-28 17:20:34 -0800874 var normalSize = this.scrollPort_.measureCharacterSize();
875 var boldSize = this.scrollPort_.measureCharacterSize('bold');
876
877 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800878 if (!isBoldSafe) {
879 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700880 'from normal. Font family is: ' +
881 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800882 }
rginda9f5222b2012-03-05 11:53:28 -0800883
Robert Gindaed016262012-10-26 16:27:09 -0700884 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
885 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800886};
887
888/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500889 * Control text blinking behavior.
890 *
891 * @param {boolean=} state Whether to enable support for blinking text.
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400892 */
Mike Frysinger261597c2017-12-28 01:14:21 -0500893hterm.Terminal.prototype.setTextBlink = function(state) {
894 if (state === undefined)
895 state = this.prefs_.get('enable-blink');
896 this.setCssVar('blink-node-duration', state ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400897};
898
899/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400900 * Set the mouse cursor style based on the current terminal mode.
901 */
902hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -0400903 this.setCssVar('mouse-cursor-style',
904 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
905 'var(--hterm-mouse-cursor-text)' :
Mike Frysinger67f58f82018-11-22 13:38:22 -0500906 'var(--hterm-mouse-cursor-default)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400907};
908
909/**
rginda87b86462011-12-14 13:48:03 -0800910 * Return a copy of the current cursor position.
911 *
912 * @return {hterm.RowCol} The RowCol object representing the current position.
913 */
914hterm.Terminal.prototype.saveCursor = function() {
915 return this.screen_.cursorPosition.clone();
916};
917
Evan Jones2600d4f2016-12-06 09:29:36 -0500918/**
919 * Return the current text attributes.
920 *
921 * @return {string}
922 */
rgindaa19afe22012-01-25 15:40:22 -0800923hterm.Terminal.prototype.getTextAttributes = function() {
924 return this.screen_.textAttributes;
925};
926
Evan Jones2600d4f2016-12-06 09:29:36 -0500927/**
928 * Set the text attributes.
929 *
930 * @param {string} textAttributes The attributes to set.
931 */
rginda1a09aa02012-06-18 21:11:25 -0700932hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
933 this.screen_.textAttributes = textAttributes;
934};
935
rginda87b86462011-12-14 13:48:03 -0800936/**
rgindaf522ce02012-04-17 17:49:17 -0700937 * Return the current browser zoom factor applied to the terminal.
938 *
939 * @return {number} The current browser zoom factor.
940 */
941hterm.Terminal.prototype.getZoomFactor = function() {
942 return this.scrollPort_.characterSize.zoomFactor;
943};
944
945/**
rginda9846e2f2012-01-27 13:53:33 -0800946 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -0500947 *
948 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -0800949 */
950hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800951 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800952};
953
954/**
rginda87b86462011-12-14 13:48:03 -0800955 * Restore a previously saved cursor position.
956 *
957 * @param {hterm.RowCol} cursor The position to restore.
958 */
959hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700960 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
961 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800962 this.screen_.setCursorPosition(row, column);
963 if (cursor.column > column ||
964 cursor.column == column && cursor.overflow) {
965 this.screen_.cursorPosition.overflow = true;
966 }
rginda87b86462011-12-14 13:48:03 -0800967};
968
969/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400970 * Clear the cursor's overflow flag.
971 */
972hterm.Terminal.prototype.clearCursorOverflow = function() {
973 this.screen_.cursorPosition.overflow = false;
974};
975
976/**
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800977 * Save the current cursor state to the corresponding screens.
978 *
979 * See the hterm.Screen.CursorState class for more details.
980 *
981 * @param {boolean=} both If true, update both screens, else only update the
982 * current screen.
983 */
984hterm.Terminal.prototype.saveCursorAndState = function(both) {
985 if (both) {
986 this.primaryScreen_.saveCursorAndState(this.vt);
987 this.alternateScreen_.saveCursorAndState(this.vt);
988 } else
989 this.screen_.saveCursorAndState(this.vt);
990};
991
992/**
993 * Restore the saved cursor state in the corresponding screens.
994 *
995 * See the hterm.Screen.CursorState class for more details.
996 *
997 * @param {boolean=} both If true, update both screens, else only update the
998 * current screen.
999 */
1000hterm.Terminal.prototype.restoreCursorAndState = function(both) {
1001 if (both) {
1002 this.primaryScreen_.restoreCursorAndState(this.vt);
1003 this.alternateScreen_.restoreCursorAndState(this.vt);
1004 } else
1005 this.screen_.restoreCursorAndState(this.vt);
1006};
1007
1008/**
Robert Ginda830583c2013-08-07 13:20:46 -07001009 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001010 *
1011 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -07001012 */
1013hterm.Terminal.prototype.setCursorShape = function(shape) {
1014 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001015 this.restyleCursor_();
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001016};
Robert Ginda830583c2013-08-07 13:20:46 -07001017
1018/**
1019 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001020 *
1021 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -07001022 */
1023hterm.Terminal.prototype.getCursorShape = function() {
1024 return this.cursorShape_;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001025};
Robert Ginda830583c2013-08-07 13:20:46 -07001026
1027/**
rginda87b86462011-12-14 13:48:03 -08001028 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001029 *
1030 * @param {number} columnCount
rginda87b86462011-12-14 13:48:03 -08001031 */
1032hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -08001033 if (columnCount == null) {
1034 this.div_.style.width = '100%';
1035 return;
1036 }
1037
Robert Ginda26806d12014-07-24 13:44:07 -07001038 this.div_.style.width = Math.ceil(
1039 this.scrollPort_.characterSize.width *
1040 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001041 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -08001042 this.scheduleSyncCursorPosition_();
1043};
rginda87b86462011-12-14 13:48:03 -08001044
rgindac9bc5502012-01-18 11:48:44 -08001045/**
rginda35c456b2012-02-09 17:29:05 -08001046 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001047 *
1048 * @param {number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -08001049 */
1050hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -08001051 if (rowCount == null) {
1052 this.div_.style.height = '100%';
1053 return;
1054 }
1055
rginda35c456b2012-02-09 17:29:05 -08001056 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -07001057 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -08001058 this.realizeSize_(this.screenSize.width, rowCount);
1059 this.scheduleSyncCursorPosition_();
1060};
1061
1062/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001063 * Deal with terminal size changes.
1064 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001065 * @param {number} columnCount The number of columns.
1066 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001067 */
1068hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
1069 if (columnCount != this.screenSize.width)
1070 this.realizeWidth_(columnCount);
1071
1072 if (rowCount != this.screenSize.height)
1073 this.realizeHeight_(rowCount);
1074
1075 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -07001076 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001077};
1078
1079/**
rgindac9bc5502012-01-18 11:48:44 -08001080 * Deal with terminal width changes.
1081 *
1082 * This function does what needs to be done when the terminal width changes
1083 * out from under us. It happens here rather than in onResize_() because this
1084 * code may need to run synchronously to handle programmatic changes of
1085 * terminal width.
1086 *
1087 * Relying on the browser to send us an async resize event means we may not be
1088 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001089 *
1090 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001091 */
1092hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001093 if (columnCount <= 0)
1094 throw new Error('Attempt to realize bad width: ' + columnCount);
1095
rgindac9bc5502012-01-18 11:48:44 -08001096 var deltaColumns = columnCount - this.screen_.getWidth();
1097
rginda87b86462011-12-14 13:48:03 -08001098 this.screenSize.width = columnCount;
1099 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001100
1101 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -04001102 if (this.defaultTabStops)
1103 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -08001104 } else {
1105 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -04001106 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -08001107 break;
1108
1109 this.tabStops_.pop();
1110 }
1111 }
1112
1113 this.screen_.setColumnCount(this.screenSize.width);
1114};
1115
1116/**
1117 * Deal with terminal height changes.
1118 *
1119 * This function does what needs to be done when the terminal height changes
1120 * out from under us. It happens here rather than in onResize_() because this
1121 * code may need to run synchronously to handle programmatic changes of
1122 * terminal height.
1123 *
1124 * Relying on the browser to send us an async resize event means we may not be
1125 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001126 *
1127 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001128 */
1129hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001130 if (rowCount <= 0)
1131 throw new Error('Attempt to realize bad height: ' + rowCount);
1132
rgindac9bc5502012-01-18 11:48:44 -08001133 var deltaRows = rowCount - this.screen_.getHeight();
1134
1135 this.screenSize.height = rowCount;
1136
1137 var cursor = this.saveCursor();
1138
1139 if (deltaRows < 0) {
1140 // Screen got smaller.
1141 deltaRows *= -1;
1142 while (deltaRows) {
1143 var lastRow = this.getRowCount() - 1;
1144 if (lastRow - this.scrollbackRows_.length == cursor.row)
1145 break;
1146
1147 if (this.getRowText(lastRow))
1148 break;
1149
1150 this.screen_.popRow();
1151 deltaRows--;
1152 }
1153
1154 var ary = this.screen_.shiftRows(deltaRows);
1155 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1156
1157 // We just removed rows from the top of the screen, we need to update
1158 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001159 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001160 } else if (deltaRows > 0) {
1161 // Screen got larger.
1162
1163 if (deltaRows <= this.scrollbackRows_.length) {
1164 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1165 var rows = this.scrollbackRows_.splice(
1166 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1167 this.screen_.unshiftRows(rows);
1168 deltaRows -= scrollbackCount;
1169 cursor.row += scrollbackCount;
1170 }
1171
1172 if (deltaRows)
1173 this.appendRows_(deltaRows);
1174 }
1175
rginda35c456b2012-02-09 17:29:05 -08001176 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001177 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001178};
1179
1180/**
1181 * Scroll the terminal to the top of the scrollback buffer.
1182 */
1183hterm.Terminal.prototype.scrollHome = function() {
1184 this.scrollPort_.scrollRowToTop(0);
1185};
1186
1187/**
1188 * Scroll the terminal to the end.
1189 */
1190hterm.Terminal.prototype.scrollEnd = function() {
1191 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1192};
1193
1194/**
1195 * Scroll the terminal one page up (minus one line) relative to the current
1196 * position.
1197 */
1198hterm.Terminal.prototype.scrollPageUp = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001199 this.scrollPort_.scrollPageUp();
rginda87b86462011-12-14 13:48:03 -08001200};
1201
1202/**
1203 * Scroll the terminal one page down (minus one line) relative to the current
1204 * position.
1205 */
1206hterm.Terminal.prototype.scrollPageDown = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001207 this.scrollPort_.scrollPageDown();
rginda8ba33642011-12-14 12:31:31 -08001208};
1209
rgindac9bc5502012-01-18 11:48:44 -08001210/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001211 * Scroll the terminal one line up relative to the current position.
1212 */
1213hterm.Terminal.prototype.scrollLineUp = function() {
1214 var i = this.scrollPort_.getTopRowIndex();
1215 this.scrollPort_.scrollRowToTop(i - 1);
1216};
1217
1218/**
1219 * Scroll the terminal one line down relative to the current position.
1220 */
1221hterm.Terminal.prototype.scrollLineDown = function() {
1222 var i = this.scrollPort_.getTopRowIndex();
1223 this.scrollPort_.scrollRowToTop(i + 1);
1224};
1225
1226/**
Robert Ginda40932892012-12-10 17:26:40 -08001227 * Clear primary screen, secondary screen, and the scrollback buffer.
1228 */
1229hterm.Terminal.prototype.wipeContents = function() {
Mike Frysinger9c482b82018-09-07 02:49:36 -04001230 this.clearHome(this.primaryScreen_);
1231 this.clearHome(this.alternateScreen_);
1232
1233 this.clearScrollback();
1234};
1235
1236/**
1237 * Clear scrollback buffer.
1238 */
1239hterm.Terminal.prototype.clearScrollback = function() {
1240 // Move to the end of the buffer in case the screen was scrolled back.
1241 // We're going to throw it away which would leave the display invalid.
1242 this.scrollEnd();
1243
Robert Ginda40932892012-12-10 17:26:40 -08001244 this.scrollbackRows_.length = 0;
1245 this.scrollPort_.resetCache();
1246
Mike Frysinger9c482b82018-09-07 02:49:36 -04001247 [this.primaryScreen_, this.alternateScreen_].forEach((screen) => {
1248 const bottom = screen.getHeight();
1249 this.renumberRows_(0, bottom, screen);
1250 });
Robert Ginda40932892012-12-10 17:26:40 -08001251
1252 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001253 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001254};
1255
1256/**
rgindac9bc5502012-01-18 11:48:44 -08001257 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001258 *
1259 * Perform a full reset to the default values listed in
1260 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001261 */
rginda87b86462011-12-14 13:48:03 -08001262hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001263 this.vt.reset();
1264
rgindac9bc5502012-01-18 11:48:44 -08001265 this.clearAllTabStops();
1266 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001267
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001268 const resetScreen = (screen) => {
1269 // We want to make sure to reset the attributes before we clear the screen.
1270 // The attributes might be used to initialize default/empty rows.
1271 screen.textAttributes.reset();
1272 screen.textAttributes.resetColorPalette();
1273 this.clearHome(screen);
1274 screen.saveCursorAndState(this.vt);
1275 };
1276 resetScreen(this.primaryScreen_);
1277 resetScreen(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001278
Mike Frysinger84301d02017-11-29 13:28:46 -08001279 // Reset terminal options to their default values.
1280 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001281 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1282
Mike Frysinger84301d02017-11-29 13:28:46 -08001283 this.setVTScrollRegion(null, null);
1284
1285 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001286};
1287
rgindac9bc5502012-01-18 11:48:44 -08001288/**
1289 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001290 *
1291 * Perform a soft reset to the default values listed in
1292 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001293 */
rginda0f5c0292012-01-13 11:00:13 -08001294hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001295 this.vt.reset();
1296
rgindab8bc8932012-04-27 12:45:03 -07001297 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001298 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001299
Brad Townb62dfdc2015-03-16 19:07:15 -07001300 // We show the cursor on soft reset but do not alter the blink state.
1301 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1302
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001303 const resetScreen = (screen) => {
1304 // Xterm also resets the color palette on soft reset, even though it doesn't
1305 // seem to be documented anywhere.
1306 screen.textAttributes.reset();
1307 screen.textAttributes.resetColorPalette();
1308 screen.saveCursorAndState(this.vt);
1309 };
1310 resetScreen(this.primaryScreen_);
1311 resetScreen(this.alternateScreen_);
rgindaf522ce02012-04-17 17:49:17 -07001312
rgindab8bc8932012-04-27 12:45:03 -07001313 // The xterm man page explicitly says this will happen on soft reset.
1314 this.setVTScrollRegion(null, null);
1315
1316 // Xterm also shows the cursor on soft reset, but does not alter the blink
1317 // state.
rgindaa19afe22012-01-25 15:40:22 -08001318 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001319};
1320
rgindac9bc5502012-01-18 11:48:44 -08001321/**
1322 * Move the cursor forward to the next tab stop, or to the last column
1323 * if no more tab stops are set.
1324 */
1325hterm.Terminal.prototype.forwardTabStop = function() {
1326 var column = this.screen_.cursorPosition.column;
1327
1328 for (var i = 0; i < this.tabStops_.length; i++) {
1329 if (this.tabStops_[i] > column) {
1330 this.setCursorColumn(this.tabStops_[i]);
1331 return;
1332 }
1333 }
1334
David Benjamin66e954d2012-05-05 21:08:12 -04001335 // xterm does not clear the overflow flag on HT or CHT.
1336 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001337 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001338 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001339};
1340
rgindac9bc5502012-01-18 11:48:44 -08001341/**
1342 * Move the cursor backward to the previous tab stop, or to the first column
1343 * if no previous tab stops are set.
1344 */
1345hterm.Terminal.prototype.backwardTabStop = function() {
1346 var column = this.screen_.cursorPosition.column;
1347
1348 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1349 if (this.tabStops_[i] < column) {
1350 this.setCursorColumn(this.tabStops_[i]);
1351 return;
1352 }
1353 }
1354
1355 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001356};
1357
rgindac9bc5502012-01-18 11:48:44 -08001358/**
1359 * Set a tab stop at the given column.
1360 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001361 * @param {integer} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001362 */
1363hterm.Terminal.prototype.setTabStop = function(column) {
1364 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1365 if (this.tabStops_[i] == column)
1366 return;
1367
1368 if (this.tabStops_[i] < column) {
1369 this.tabStops_.splice(i + 1, 0, column);
1370 return;
1371 }
1372 }
1373
1374 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001375};
1376
rgindac9bc5502012-01-18 11:48:44 -08001377/**
1378 * Clear the tab stop at the current cursor position.
1379 *
1380 * No effect if there is no tab stop at the current cursor position.
1381 */
1382hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1383 var column = this.screen_.cursorPosition.column;
1384
1385 var i = this.tabStops_.indexOf(column);
1386 if (i == -1)
1387 return;
1388
1389 this.tabStops_.splice(i, 1);
1390};
1391
1392/**
1393 * Clear all tab stops.
1394 */
1395hterm.Terminal.prototype.clearAllTabStops = function() {
1396 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001397 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001398};
1399
1400/**
1401 * Set up the default tab stops, starting from a given column.
1402 *
1403 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001404 * from the specified column, or 0 if no column is provided. It also flags
1405 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001406 *
1407 * This does not clear the existing tab stops first, use clearAllTabStops
1408 * for that.
1409 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001410 * @param {integer} opt_start Optional starting zero based starting column, useful
rgindac9bc5502012-01-18 11:48:44 -08001411 * for filling out missing tab stops when the terminal is resized.
1412 */
1413hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1414 var start = opt_start || 0;
1415 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001416 // Round start up to a default tab stop.
1417 start = start - 1 - ((start - 1) % w) + w;
1418 for (var i = start; i < this.screenSize.width; i += w) {
1419 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001420 }
David Benjamin66e954d2012-05-05 21:08:12 -04001421
1422 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001423};
1424
rginda6d397402012-01-17 10:58:29 -08001425/**
rginda8ba33642011-12-14 12:31:31 -08001426 * Interpret a sequence of characters.
1427 *
1428 * Incomplete escape sequences are buffered until the next call.
1429 *
1430 * @param {string} str Sequence of characters to interpret or pass through.
1431 */
1432hterm.Terminal.prototype.interpret = function(str) {
rginda8ba33642011-12-14 12:31:31 -08001433 this.scheduleSyncCursorPosition_();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001434 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001435};
1436
1437/**
1438 * Take over the given DIV for use as the terminal display.
1439 *
1440 * @param {HTMLDivElement} div The div to use as the terminal display.
1441 */
1442hterm.Terminal.prototype.decorate = function(div) {
Mike Frysinger5768a9d2017-12-26 12:57:44 -05001443 const charset = div.ownerDocument.characterSet.toLowerCase();
1444 if (charset != 'utf-8') {
1445 console.warn(`Document encoding should be set to utf-8, not "${charset}";` +
1446 ` Add <meta charset='utf-8'/> to your HTML <head> to fix.`);
1447 }
1448
rginda87b86462011-12-14 13:48:03 -08001449 this.div_ = div;
1450
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001451 this.accessibilityReader_ = new hterm.AccessibilityReader(div);
1452
rginda8ba33642011-12-14 12:31:31 -08001453 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001454 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001455 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1456 this.scrollPort_.setBackgroundPosition(
1457 this.prefs_.get('background-position'));
Mike Frysinger08bad432017-04-24 00:50:54 -04001458 this.scrollPort_.setUserCssUrl(this.prefs_.get('user-css'));
1459 this.scrollPort_.setUserCssText(this.prefs_.get('user-css-text'));
Raymes Khoury177aec72018-06-26 10:58:53 +10001460 this.scrollPort_.setAccessibilityReader(this.accessibilityReader_);
rginda30f20f62012-04-05 16:36:19 -07001461
rginda0918b652012-04-04 11:26:24 -07001462 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001463
rginda9f5222b2012-03-05 11:53:28 -08001464 this.setFontSize(this.prefs_.get('font-size'));
1465 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001466
David Reveman8f552492012-03-28 12:18:41 -04001467 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001468 this.setScrollWheelMoveMultipler(
1469 this.prefs_.get('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001470
rginda8ba33642011-12-14 12:31:31 -08001471 this.document_ = this.scrollPort_.getDocument();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001472 this.accessibilityReader_.decorate(this.document_);
rginda8ba33642011-12-14 12:31:31 -08001473
Evan Jones5f9df812016-12-06 09:38:58 -05001474 this.document_.body.oncontextmenu = function() { return false; };
Mike Frysingercc114512017-09-11 21:39:17 -04001475 this.contextMenu.setDocument(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07001476
1477 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001478 var screenNode = this.scrollPort_.getScreenNode();
1479 screenNode.addEventListener('mousedown', onMouse);
1480 screenNode.addEventListener('mouseup', onMouse);
1481 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001482 this.scrollPort_.onScrollWheel = onMouse;
1483
Mike Frysinger02ded6d2018-06-21 14:25:20 -04001484 screenNode.addEventListener('keydown', this.onKeyboardActivity_.bind(this));
1485
Toni Barzic0bfa8922013-11-22 11:18:35 -08001486 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001487 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001488 // Listen for mousedown events on the screenNode as in FF the focus
1489 // events don't bubble.
1490 screenNode.addEventListener('mousedown', function() {
1491 setTimeout(this.onFocusChange_.bind(this, true));
1492 }.bind(this));
1493
Toni Barzic0bfa8922013-11-22 11:18:35 -08001494 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001495 'blur', this.onFocusChange_.bind(this, false));
1496
1497 var style = this.document_.createElement('style');
1498 style.textContent =
1499 ('.cursor-node[focus="false"] {' +
1500 ' box-sizing: border-box;' +
1501 ' background-color: transparent !important;' +
1502 ' border-width: 2px;' +
1503 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001504 '}' +
Mike Frysingercc114512017-09-11 21:39:17 -04001505 'menu {' +
1506 ' margin: 0;' +
1507 ' padding: 0;' +
1508 ' cursor: var(--hterm-mouse-cursor-pointer);' +
1509 '}' +
1510 'menuitem {' +
1511 ' white-space: nowrap;' +
1512 ' border-bottom: 1px dashed;' +
1513 ' display: block;' +
1514 ' padding: 0.3em 0.3em 0 0.3em;' +
1515 '}' +
1516 'menuitem.separator {' +
1517 ' border-bottom: none;' +
1518 ' height: 0.5em;' +
1519 ' padding: 0;' +
1520 '}' +
1521 'menuitem:hover {' +
1522 ' color: var(--hterm-cursor-color);' +
1523 '}' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001524 '.wc-node {' +
1525 ' display: inline-block;' +
1526 ' text-align: center;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001527 ' width: calc(var(--hterm-charsize-width) * 2);' +
Mike Frysinger4036f6e2017-05-31 14:02:55 -04001528 ' line-height: var(--hterm-charsize-height);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001529 '}' +
1530 ':root {' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001531 ' --hterm-charsize-width: ' + this.scrollPort_.characterSize.width + 'px;' +
1532 ' --hterm-charsize-height: ' + this.scrollPort_.characterSize.height + 'px;' +
Mike Frysingera27c0502017-08-23 22:37:10 -04001533 // Default position hides the cursor for when the window is initializing.
1534 ' --hterm-cursor-offset-col: -1;' +
1535 ' --hterm-cursor-offset-row: -1;' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001536 ' --hterm-blink-node-duration: 0.7s;' +
Mike Frysinger67f58f82018-11-22 13:38:22 -05001537 ' --hterm-mouse-cursor-default: default;' +
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001538 ' --hterm-mouse-cursor-text: text;' +
Mike Frysinger67f58f82018-11-22 13:38:22 -05001539 ' --hterm-mouse-cursor-pointer: pointer;' +
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001540 ' --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001541 '}' +
Mike Frysingerd3907202017-10-23 01:23:50 -04001542 '.uri-node:hover {' +
1543 ' text-decoration: underline;' +
Mike Frysinger67f58f82018-11-22 13:38:22 -05001544 ' cursor: var(--hterm-mouse-cursor-pointer);' +
Mike Frysingerd3907202017-10-23 01:23:50 -04001545 '}' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001546 '@keyframes blink {' +
1547 ' from { opacity: 1.0; }' +
1548 ' to { opacity: 0.0; }' +
1549 '}' +
1550 '.blink-node {' +
1551 ' animation-name: blink;' +
1552 ' animation-duration: var(--hterm-blink-node-duration);' +
1553 ' animation-iteration-count: infinite;' +
1554 ' animation-timing-function: ease-in-out;' +
1555 ' animation-direction: alternate;' +
rginda8e92a692012-05-20 19:37:20 -07001556 '}');
Mike Frysingerb74a6472018-06-22 13:37:08 -04001557 // Insert this stock style as the first node so that any user styles will
1558 // override w/out having to use !important everywhere. The rules above mix
1559 // runtime variables with default ones designed to be overridden by the user,
1560 // but we can wait for a concrete case from the users to determine the best
1561 // way to split the sheet up to before & after the user-css settings.
1562 this.document_.head.insertBefore(style, this.document_.head.firstChild);
rginda8e92a692012-05-20 19:37:20 -07001563
rginda8ba33642011-12-14 12:31:31 -08001564 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001565 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001566 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001567 this.cursorNode_.style.cssText =
1568 ('position: absolute;' +
Mike Frysinger44c32202017-08-05 01:13:09 -04001569 'left: calc(var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));' +
1570 'top: calc(var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));' +
Mike Frysingerd60f4c22018-03-15 21:25:30 -07001571 'display: ' + (this.options_.cursorVisible ? '' : 'none') + ';' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001572 'width: var(--hterm-charsize-width);' +
1573 'height: var(--hterm-charsize-height);' +
Mike Frysinger2fd079a2018-09-02 01:46:12 -04001574 'background-color: var(--hterm-cursor-color);' +
1575 'border-color: var(--hterm-cursor-color);' +
Rob Spies06533ba2014-04-24 11:20:37 -07001576 '-webkit-transition: opacity, background-color 100ms linear;' +
1577 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001578
Mike Frysingerf02a2cb2017-12-21 00:34:03 -05001579 this.setCursorColor();
Robert Gindafb1be6a2013-12-11 11:56:22 -08001580 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1581 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001582
rginda8ba33642011-12-14 12:31:31 -08001583 this.document_.body.appendChild(this.cursorNode_);
1584
rgindad5613292012-06-19 15:40:37 -07001585 // When 'enableMouseDragScroll' is off we reposition this element directly
1586 // under the mouse cursor after a click. This makes Chrome associate
1587 // subsequent mousemove events with the scroll-blocker. Since the
1588 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1589 // events do not cause the scrollport to scroll.
1590 //
1591 // It's a hack, but it's the cleanest way I could find.
1592 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001593 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
Raymes Khoury6dce2f82018-04-12 15:38:58 +10001594 this.scrollBlockerNode_.setAttribute('aria-hidden', 'true');
rgindad5613292012-06-19 15:40:37 -07001595 this.scrollBlockerNode_.style.cssText =
1596 ('position: absolute;' +
1597 'top: -99px;' +
1598 'display: block;' +
1599 'width: 10px;' +
1600 'height: 10px;');
1601 this.document_.body.appendChild(this.scrollBlockerNode_);
1602
rgindad5613292012-06-19 15:40:37 -07001603 this.scrollPort_.onScrollWheel = onMouse;
1604 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1605 ].forEach(function(event) {
1606 this.scrollBlockerNode_.addEventListener(event, onMouse);
1607 this.cursorNode_.addEventListener(event, onMouse);
1608 this.document_.addEventListener(event, onMouse);
1609 }.bind(this));
1610
1611 this.cursorNode_.addEventListener('mousedown', function() {
1612 setTimeout(this.focus.bind(this));
1613 }.bind(this));
1614
rginda8ba33642011-12-14 12:31:31 -08001615 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001616
rginda87b86462011-12-14 13:48:03 -08001617 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001618 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001619};
1620
rginda0918b652012-04-04 11:26:24 -07001621/**
1622 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001623 *
1624 * @return {HTMLDocument}
rginda0918b652012-04-04 11:26:24 -07001625 */
rginda87b86462011-12-14 13:48:03 -08001626hterm.Terminal.prototype.getDocument = function() {
1627 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001628};
1629
1630/**
rginda0918b652012-04-04 11:26:24 -07001631 * Focus the terminal.
1632 */
1633hterm.Terminal.prototype.focus = function() {
1634 this.scrollPort_.focus();
1635};
1636
1637/**
rginda8ba33642011-12-14 12:31:31 -08001638 * Return the HTML Element for a given row index.
1639 *
1640 * This is a method from the RowProvider interface. The ScrollPort uses
1641 * it to fetch rows on demand as they are scrolled into view.
1642 *
1643 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1644 * pairs to conserve memory.
1645 *
1646 * @param {integer} index The zero-based row index, measured relative to the
1647 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001648 * largest indices.
rginda8ba33642011-12-14 12:31:31 -08001649 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1650 */
1651hterm.Terminal.prototype.getRowNode = function(index) {
1652 if (index < this.scrollbackRows_.length)
1653 return this.scrollbackRows_[index];
1654
1655 var screenIndex = index - this.scrollbackRows_.length;
1656 return this.screen_.rowsArray[screenIndex];
1657};
1658
1659/**
1660 * Return the text content for a given range of rows.
1661 *
1662 * This is a method from the RowProvider interface. The ScrollPort uses
1663 * it to fetch text content on demand when the user attempts to copy their
1664 * selection to the clipboard.
1665 *
1666 * @param {integer} start The zero-based row index to start from, measured
1667 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001668 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001669 * @param {integer} end The zero-based row index to end on, measured
1670 * relative to the start of the scrollback buffer.
1671 * @return {string} A single string containing the text value of the range of
1672 * rows. Lines will be newline delimited, with no trailing newline.
1673 */
1674hterm.Terminal.prototype.getRowsText = function(start, end) {
1675 var ary = [];
1676 for (var i = start; i < end; i++) {
1677 var node = this.getRowNode(i);
1678 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001679 if (i < end - 1 && !node.getAttribute('line-overflow'))
1680 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001681 }
1682
rgindaa09e7332012-08-17 12:49:51 -07001683 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001684};
1685
1686/**
1687 * Return the text content for a given row.
1688 *
1689 * This is a method from the RowProvider interface. The ScrollPort uses
1690 * it to fetch text content on demand when the user attempts to copy their
1691 * selection to the clipboard.
1692 *
1693 * @param {integer} index The zero-based row index to return, measured
1694 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001695 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001696 * @return {string} A string containing the text value of the selected row.
1697 */
1698hterm.Terminal.prototype.getRowText = function(index) {
1699 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001700 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001701};
1702
1703/**
1704 * Return the total number of rows in the addressable screen and in the
1705 * scrollback buffer of this terminal.
1706 *
1707 * This is a method from the RowProvider interface. The ScrollPort uses
1708 * it to compute the size of the scrollbar.
1709 *
1710 * @return {integer} The number of rows in this terminal.
1711 */
1712hterm.Terminal.prototype.getRowCount = function() {
1713 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1714};
1715
1716/**
1717 * Create DOM nodes for new rows and append them to the end of the terminal.
1718 *
1719 * This is the only correct way to add a new DOM node for a row. Notice that
1720 * the new row is appended to the bottom of the list of rows, and does not
1721 * require renumbering (of the rowIndex property) of previous rows.
1722 *
1723 * If you think you want a new blank row somewhere in the middle of the
1724 * terminal, look into moveRows_().
1725 *
1726 * This method does not pay attention to vtScrollTop/Bottom, since you should
1727 * be using moveRows() in cases where they would matter.
1728 *
1729 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001730 *
1731 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001732 */
1733hterm.Terminal.prototype.appendRows_ = function(count) {
1734 var cursorRow = this.screen_.rowsArray.length;
1735 var offset = this.scrollbackRows_.length + cursorRow;
1736 for (var i = 0; i < count; i++) {
1737 var row = this.document_.createElement('x-row');
1738 row.appendChild(this.document_.createTextNode(''));
1739 row.rowIndex = offset + i;
1740 this.screen_.pushRow(row);
1741 }
1742
1743 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1744 if (extraRows > 0) {
1745 var ary = this.screen_.shiftRows(extraRows);
1746 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001747 if (this.scrollPort_.isScrolledEnd)
1748 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001749 }
1750
1751 if (cursorRow >= this.screen_.rowsArray.length)
1752 cursorRow = this.screen_.rowsArray.length - 1;
1753
rginda87b86462011-12-14 13:48:03 -08001754 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001755};
1756
1757/**
1758 * Relocate rows from one part of the addressable screen to another.
1759 *
1760 * This is used to recycle rows during VT scrolls (those which are driven
1761 * by VT commands, rather than by the user manipulating the scrollbar.)
1762 *
1763 * In this case, the blank lines scrolled into the scroll region are made of
1764 * the nodes we scrolled off. These have their rowIndex properties carefully
1765 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001766 *
1767 * @param {number} fromIndex The start index.
1768 * @param {number} count The number of rows to move.
1769 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001770 */
1771hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1772 var ary = this.screen_.removeRows(fromIndex, count);
1773 this.screen_.insertRows(toIndex, ary);
1774
1775 var start, end;
1776 if (fromIndex < toIndex) {
1777 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001778 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001779 } else {
1780 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001781 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001782 }
1783
1784 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001785 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001786};
1787
1788/**
1789 * Renumber the rowIndex property of the given range of rows.
1790 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001791 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08001792 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001793 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001794 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05001795 *
1796 * @param {number} start The start index.
1797 * @param {number} end The end index.
1798 * @param {hterm.Screen} opt_screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08001799 */
Robert Ginda40932892012-12-10 17:26:40 -08001800hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1801 var screen = opt_screen || this.screen_;
1802
rginda8ba33642011-12-14 12:31:31 -08001803 var offset = this.scrollbackRows_.length;
1804 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001805 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001806 }
1807};
1808
1809/**
1810 * Print a string to the terminal.
1811 *
1812 * This respects the current insert and wraparound modes. It will add new lines
1813 * to the end of the terminal, scrolling off the top into the scrollback buffer
1814 * if necessary.
1815 *
1816 * The string is *not* parsed for escape codes. Use the interpret() method if
1817 * that's what you're after.
1818 *
1819 * @param{string} str The string to print.
1820 */
1821hterm.Terminal.prototype.print = function(str) {
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001822 this.scheduleSyncCursorPosition_();
1823
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001824 // Basic accessibility output for the screen reader.
Raymes Khoury177aec72018-06-26 10:58:53 +10001825 this.accessibilityReader_.announce(str);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001826
rgindaa9abdd82012-08-06 18:05:09 -07001827 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001828
Ricky Liang48f05cb2013-12-31 23:35:29 +08001829 var strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04001830 // Fun edge case: If the string only contains zero width codepoints (like
1831 // combining characters), we make sure to iterate at least once below.
1832 if (strWidth == 0 && str)
1833 strWidth = 1;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001834
1835 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001836 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1837 this.screen_.commitLineOverflow();
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001838 this.newLine(true);
rgindaa09e7332012-08-17 12:49:51 -07001839 }
rgindaa19afe22012-01-25 15:40:22 -08001840
Ricky Liang48f05cb2013-12-31 23:35:29 +08001841 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001842 var didOverflow = false;
1843 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001844
rgindaa9abdd82012-08-06 18:05:09 -07001845 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1846 didOverflow = true;
1847 count = this.screenSize.width - this.screen_.cursorPosition.column;
1848 }
rgindaa19afe22012-01-25 15:40:22 -08001849
rgindaa9abdd82012-08-06 18:05:09 -07001850 if (didOverflow && !this.options_.wraparound) {
1851 // If the string overflowed the line but wraparound is off, then the
1852 // last printed character should be the last of the string.
1853 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001854 substr = lib.wc.substr(str, startOffset, count - 1) +
1855 lib.wc.substr(str, strWidth - 1);
1856 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001857 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001858 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001859 }
rgindaa19afe22012-01-25 15:40:22 -08001860
Ricky Liang48f05cb2013-12-31 23:35:29 +08001861 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1862 for (var i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001863 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
1864 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001865
1866 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001867 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001868 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001869 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001870 }
1871 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001872 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07001873 }
1874
1875 this.screen_.maybeClipCurrentRow();
1876 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001877 }
rginda8ba33642011-12-14 12:31:31 -08001878
rginda9f5222b2012-03-05 11:53:28 -08001879 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001880 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001881};
1882
1883/**
rginda87b86462011-12-14 13:48:03 -08001884 * Set the VT scroll region.
1885 *
rginda87b86462011-12-14 13:48:03 -08001886 * This also resets the cursor position to the absolute (0, 0) position, since
1887 * that's what xterm appears to do.
1888 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001889 * Setting the scroll region to the full height of the terminal will clear
1890 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1891 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1892 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1893 * continue to work as most users would expect.
1894 *
rginda87b86462011-12-14 13:48:03 -08001895 * @param {integer} scrollTop The zero-based top of the scroll region.
1896 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1897 * inclusive.
1898 */
1899hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001900 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001901 this.vtScrollTop_ = null;
1902 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001903 } else {
1904 this.vtScrollTop_ = scrollTop;
1905 this.vtScrollBottom_ = scrollBottom;
1906 }
rginda87b86462011-12-14 13:48:03 -08001907};
1908
1909/**
rginda8ba33642011-12-14 12:31:31 -08001910 * Return the top row index according to the VT.
1911 *
1912 * This will return 0 unless the terminal has been told to restrict scrolling
1913 * to some lower row. It is used for some VT cursor positioning and scrolling
1914 * commands.
1915 *
1916 * @return {integer} The topmost row in the terminal's scroll region.
1917 */
1918hterm.Terminal.prototype.getVTScrollTop = function() {
1919 if (this.vtScrollTop_ != null)
1920 return this.vtScrollTop_;
1921
1922 return 0;
rginda87b86462011-12-14 13:48:03 -08001923};
rginda8ba33642011-12-14 12:31:31 -08001924
1925/**
1926 * Return the bottom row index according to the VT.
1927 *
1928 * This will return the height of the terminal unless the it has been told to
1929 * restrict scrolling to some higher row. It is used for some VT cursor
1930 * positioning and scrolling commands.
1931 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001932 * @return {integer} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001933 */
1934hterm.Terminal.prototype.getVTScrollBottom = function() {
1935 if (this.vtScrollBottom_ != null)
1936 return this.vtScrollBottom_;
1937
rginda87b86462011-12-14 13:48:03 -08001938 return this.screenSize.height - 1;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001939};
rginda8ba33642011-12-14 12:31:31 -08001940
1941/**
1942 * Process a '\n' character.
1943 *
1944 * If the cursor is on the final row of the terminal this will append a new
1945 * blank row to the screen and scroll the topmost row into the scrollback
1946 * buffer.
1947 *
1948 * Otherwise, this moves the cursor to column zero of the next row.
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001949 *
1950 * @param {boolean=} dueToOverflow Whether the newline is due to wraparound of
1951 * the terminal.
rginda8ba33642011-12-14 12:31:31 -08001952 */
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001953hterm.Terminal.prototype.newLine = function(dueToOverflow = false) {
1954 if (!dueToOverflow)
1955 this.accessibilityReader_.newLine();
1956
Robert Ginda9937abc2013-07-25 16:09:23 -07001957 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1958 this.screen_.rowsArray.length - 1);
1959
1960 if (this.vtScrollBottom_ != null) {
1961 // A VT Scroll region is active, we never append new rows.
1962 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1963 // We're at the end of the VT Scroll Region, perform a VT scroll.
1964 this.vtScrollUp(1);
1965 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1966 } else if (cursorAtEndOfScreen) {
1967 // We're at the end of the screen, the only thing to do is put the
1968 // cursor to column 0.
1969 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1970 } else {
1971 // Anywhere else, advance the cursor row, and reset the column.
1972 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1973 }
1974 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001975 // We're at the end of the screen. Append a new row to the terminal,
1976 // shifting the top row into the scrollback.
1977 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001978 } else {
rginda87b86462011-12-14 13:48:03 -08001979 // Anywhere else in the screen just moves the cursor.
1980 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001981 }
1982};
1983
1984/**
1985 * Like newLine(), except maintain the cursor column.
1986 */
1987hterm.Terminal.prototype.lineFeed = function() {
1988 var column = this.screen_.cursorPosition.column;
1989 this.newLine();
1990 this.setCursorColumn(column);
1991};
1992
1993/**
rginda87b86462011-12-14 13:48:03 -08001994 * If autoCarriageReturn is set then newLine(), else lineFeed().
1995 */
1996hterm.Terminal.prototype.formFeed = function() {
1997 if (this.options_.autoCarriageReturn) {
1998 this.newLine();
1999 } else {
2000 this.lineFeed();
2001 }
2002};
2003
2004/**
2005 * Move the cursor up one row, possibly inserting a blank line.
2006 *
2007 * The cursor column is not changed.
2008 */
2009hterm.Terminal.prototype.reverseLineFeed = function() {
2010 var scrollTop = this.getVTScrollTop();
2011 var currentRow = this.screen_.cursorPosition.row;
2012
2013 if (currentRow == scrollTop) {
2014 this.insertLines(1);
2015 } else {
2016 this.setAbsoluteCursorRow(currentRow - 1);
2017 }
2018};
2019
2020/**
rginda8ba33642011-12-14 12:31:31 -08002021 * Replace all characters to the left of the current cursor with the space
2022 * character.
2023 *
2024 * TODO(rginda): This should probably *remove* the characters (not just replace
2025 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07002026 * position.
rginda8ba33642011-12-14 12:31:31 -08002027 */
2028hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08002029 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002030 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002031 const count = cursor.column + 1;
2032 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08002033 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002034};
2035
2036/**
David Benjamin684a9b72012-05-01 17:19:58 -04002037 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08002038 *
2039 * The cursor position is unchanged.
2040 *
Robert Gindaf2547f12012-10-25 20:36:21 -07002041 * If the current background color is not the default background color this
2042 * will insert spaces rather than delete. This is unfortunate because the
2043 * trailing space will affect text selection, but it's difficult to come up
2044 * with a way to style empty space that wouldn't trip up the hterm.Screen
2045 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07002046 *
2047 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
2048 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
2049 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05002050 *
2051 * @param {number} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08002052 */
2053hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07002054 if (this.screen_.cursorPosition.overflow)
2055 return;
2056
Robert Ginda7fd57082012-09-25 14:41:47 -07002057 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
2058 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07002059
2060 if (this.screen_.textAttributes.background ===
2061 this.screen_.textAttributes.DEFAULT_COLOR) {
2062 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08002063 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07002064 this.screen_.cursorPosition.column + count) {
2065 this.screen_.deleteChars(count);
2066 this.clearCursorOverflow();
2067 return;
2068 }
2069 }
2070
rginda87b86462011-12-14 13:48:03 -08002071 var cursor = this.saveCursor();
Mike Frysinger6380bed2017-08-24 18:46:39 -04002072 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08002073 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002074 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002075};
2076
2077/**
2078 * Erase the current line.
2079 *
2080 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002081 */
2082hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08002083 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002084 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08002085 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002086 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002087};
2088
2089/**
David Benjamina08d78f2012-05-05 00:28:49 -04002090 * Erase all characters from the start of the screen to the current cursor
2091 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002092 *
2093 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002094 */
2095hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08002096 var cursor = this.saveCursor();
2097
2098 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002099
David Benjamina08d78f2012-05-05 00:28:49 -04002100 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002101 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002102 this.screen_.clearCursorRow();
2103 }
2104
rginda87b86462011-12-14 13:48:03 -08002105 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002106 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002107};
2108
2109/**
2110 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002111 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002112 *
2113 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002114 */
2115hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08002116 var cursor = this.saveCursor();
2117
2118 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002119
David Benjamina08d78f2012-05-05 00:28:49 -04002120 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08002121 for (var i = cursor.row + 1; i <= bottom; i++) {
2122 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002123 this.screen_.clearCursorRow();
2124 }
2125
rginda87b86462011-12-14 13:48:03 -08002126 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002127 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002128};
2129
2130/**
2131 * Fill the terminal with a given character.
2132 *
2133 * This methods does not respect the VT scroll region.
2134 *
2135 * @param {string} ch The character to use for the fill.
2136 */
2137hterm.Terminal.prototype.fill = function(ch) {
2138 var cursor = this.saveCursor();
2139
2140 this.setAbsoluteCursorPosition(0, 0);
2141 for (var row = 0; row < this.screenSize.height; row++) {
2142 for (var col = 0; col < this.screenSize.width; col++) {
2143 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002144 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002145 }
2146 }
2147
2148 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002149};
2150
2151/**
rginda9ea433c2012-03-16 11:57:00 -07002152 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002153 *
rginda9ea433c2012-03-16 11:57:00 -07002154 * This does not respect the scroll region.
2155 *
2156 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2157 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002158 */
rginda9ea433c2012-03-16 11:57:00 -07002159hterm.Terminal.prototype.clearHome = function(opt_screen) {
2160 var screen = opt_screen || this.screen_;
2161 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002162
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002163 this.accessibilityReader_.clear();
2164
rginda11057d52012-04-25 12:29:56 -07002165 if (bottom == 0) {
2166 // Empty screen, nothing to do.
2167 return;
2168 }
2169
rgindae4d29232012-01-19 10:47:13 -08002170 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002171 screen.setCursorPosition(i, 0);
2172 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002173 }
2174
rginda9ea433c2012-03-16 11:57:00 -07002175 screen.setCursorPosition(0, 0);
2176};
2177
2178/**
2179 * Erase the entire display without changing the cursor position.
2180 *
2181 * The cursor position is unchanged. This does not respect the scroll
2182 * region.
2183 *
2184 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2185 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002186 */
2187hterm.Terminal.prototype.clear = function(opt_screen) {
2188 var screen = opt_screen || this.screen_;
2189 var cursor = screen.cursorPosition.clone();
2190 this.clearHome(screen);
2191 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002192};
2193
2194/**
2195 * VT command to insert lines at the current cursor row.
2196 *
2197 * This respects the current scroll region. Rows pushed off the bottom are
2198 * lost (they won't show up in the scrollback buffer).
2199 *
rginda8ba33642011-12-14 12:31:31 -08002200 * @param {integer} count The number of lines to insert.
2201 */
2202hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07002203 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002204
2205 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002206 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002207
Robert Ginda579186b2012-09-26 11:40:04 -07002208 // The moveCount is the number of rows we need to relocate to make room for
2209 // the new row(s). The count is the distance to move them.
2210 var moveCount = bottom - cursorRow - count + 1;
2211 if (moveCount)
2212 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08002213
Robert Ginda579186b2012-09-26 11:40:04 -07002214 for (var i = count - 1; i >= 0; i--) {
2215 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002216 this.screen_.clearCursorRow();
2217 }
rginda8ba33642011-12-14 12:31:31 -08002218};
2219
2220/**
2221 * VT command to delete lines at the current cursor row.
2222 *
2223 * New rows are added to the bottom of scroll region to take their place. New
2224 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002225 *
2226 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002227 */
2228hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08002229 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002230
rginda87b86462011-12-14 13:48:03 -08002231 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08002232 var bottom = this.getVTScrollBottom();
2233
rginda87b86462011-12-14 13:48:03 -08002234 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002235 count = Math.min(count, maxCount);
2236
rginda87b86462011-12-14 13:48:03 -08002237 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08002238 if (count != maxCount)
2239 this.moveRows_(top, count, moveStart);
2240
2241 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002242 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002243 this.screen_.clearCursorRow();
2244 }
2245
rginda87b86462011-12-14 13:48:03 -08002246 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002247 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002248};
2249
2250/**
2251 * Inserts the given number of spaces at the current cursor position.
2252 *
rginda87b86462011-12-14 13:48:03 -08002253 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002254 *
2255 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002256 */
2257hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08002258 var cursor = this.saveCursor();
2259
rgindacbbd7482012-06-13 15:06:16 -07002260 var ws = lib.f.getWhitespace(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002261 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002262 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002263
2264 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002265 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002266};
2267
2268/**
2269 * Forward-delete the specified number of characters starting at the cursor
2270 * position.
2271 *
2272 * @param {integer} count The number of characters to delete.
2273 */
2274hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002275 var deleted = this.screen_.deleteChars(count);
2276 if (deleted && !this.screen_.textAttributes.isDefault()) {
2277 var cursor = this.saveCursor();
2278 this.setCursorColumn(this.screenSize.width - deleted);
2279 this.screen_.insertString(lib.f.getWhitespace(deleted));
2280 this.restoreCursor(cursor);
2281 }
2282
David Benjamin54e8bf62012-06-01 22:31:40 -04002283 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002284};
2285
2286/**
2287 * Shift rows in the scroll region upwards by a given number of lines.
2288 *
2289 * New rows are inserted at the bottom of the scroll region to fill the
2290 * vacated rows. The new rows not filled out with the current text attributes.
2291 *
2292 * This function does not affect the scrollback rows at all. Rows shifted
2293 * off the top are lost.
2294 *
rginda87b86462011-12-14 13:48:03 -08002295 * The cursor position is not altered.
2296 *
rginda8ba33642011-12-14 12:31:31 -08002297 * @param {integer} count The number of rows to scroll.
2298 */
2299hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002300 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002301
rginda87b86462011-12-14 13:48:03 -08002302 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002303 this.deleteLines(count);
2304
rginda87b86462011-12-14 13:48:03 -08002305 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002306};
2307
2308/**
2309 * Shift rows below the cursor down by a given number of lines.
2310 *
2311 * This function respects the current scroll region.
2312 *
2313 * New rows are inserted at the top of the scroll region to fill the
2314 * vacated rows. The new rows not filled out with the current text attributes.
2315 *
2316 * This function does not affect the scrollback rows at all. Rows shifted
2317 * off the bottom are lost.
2318 *
2319 * @param {integer} count The number of rows to scroll.
2320 */
2321hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08002322 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002323
rginda87b86462011-12-14 13:48:03 -08002324 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08002325 this.insertLines(opt_count);
2326
rginda87b86462011-12-14 13:48:03 -08002327 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002328};
2329
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002330/**
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002331 * Enable accessibility-friendly features that have a performance impact.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002332 *
2333 * This will generate additional DOM nodes in an aria-live region that will
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002334 * cause Assitive Technology to announce the output of the terminal. It also
2335 * enables other features that aid assistive technology. All the features gated
2336 * behind this flag have a performance impact on the terminal which is why they
2337 * are made optional.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002338 *
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002339 * @param {boolean} enabled Whether to enable accessibility-friendly features.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002340 */
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002341hterm.Terminal.prototype.setAccessibilityEnabled = function(enabled) {
Raymes Khoury177aec72018-06-26 10:58:53 +10002342 this.accessibilityReader_.setAccessibilityEnabled(enabled);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002343};
rginda87b86462011-12-14 13:48:03 -08002344
rginda8ba33642011-12-14 12:31:31 -08002345/**
2346 * Set the cursor position.
2347 *
2348 * The cursor row is relative to the scroll region if the terminal has
2349 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2350 *
2351 * @param {integer} row The new zero-based cursor row.
2352 * @param {integer} row The new zero-based cursor column.
2353 */
2354hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2355 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002356 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002357 } else {
rginda87b86462011-12-14 13:48:03 -08002358 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002359 }
rginda87b86462011-12-14 13:48:03 -08002360};
rginda8ba33642011-12-14 12:31:31 -08002361
Evan Jones2600d4f2016-12-06 09:29:36 -05002362/**
2363 * Move the cursor relative to its current position.
2364 *
2365 * @param {number} row
2366 * @param {number} column
2367 */
rginda87b86462011-12-14 13:48:03 -08002368hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2369 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002370 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2371 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002372 this.screen_.setCursorPosition(row, column);
2373};
2374
Evan Jones2600d4f2016-12-06 09:29:36 -05002375/**
2376 * Move the cursor to the specified position.
2377 *
2378 * @param {number} row
2379 * @param {number} column
2380 */
rginda87b86462011-12-14 13:48:03 -08002381hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002382 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2383 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002384 this.screen_.setCursorPosition(row, column);
2385};
2386
2387/**
2388 * Set the cursor column.
2389 *
2390 * @param {integer} column The new zero-based cursor column.
2391 */
2392hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002393 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002394};
2395
2396/**
2397 * Return the cursor column.
2398 *
2399 * @return {integer} The zero-based cursor column.
2400 */
2401hterm.Terminal.prototype.getCursorColumn = function() {
2402 return this.screen_.cursorPosition.column;
2403};
2404
2405/**
2406 * Set the cursor row.
2407 *
2408 * The cursor row is relative to the scroll region if the terminal has
2409 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2410 *
2411 * @param {integer} row The new cursor row.
2412 */
rginda87b86462011-12-14 13:48:03 -08002413hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2414 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002415};
2416
2417/**
2418 * Return the cursor row.
2419 *
2420 * @return {integer} The zero-based cursor row.
2421 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002422hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002423 return this.screen_.cursorPosition.row;
2424};
2425
2426/**
2427 * Request that the ScrollPort redraw itself soon.
2428 *
2429 * The redraw will happen asynchronously, soon after the call stack winds down.
2430 * Multiple calls will be coalesced into a single redraw.
2431 */
2432hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002433 if (this.timeouts_.redraw)
2434 return;
rginda8ba33642011-12-14 12:31:31 -08002435
2436 var self = this;
rginda87b86462011-12-14 13:48:03 -08002437 this.timeouts_.redraw = setTimeout(function() {
2438 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002439 self.scrollPort_.redraw_();
2440 }, 0);
2441};
2442
2443/**
2444 * Request that the ScrollPort be scrolled to the bottom.
2445 *
2446 * The scroll will happen asynchronously, soon after the call stack winds down.
2447 * Multiple calls will be coalesced into a single scroll.
2448 *
2449 * This affects the scrollbar position of the ScrollPort, and has nothing to
2450 * do with the VT scroll commands.
2451 */
2452hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2453 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002454 return;
rginda8ba33642011-12-14 12:31:31 -08002455
2456 var self = this;
2457 this.timeouts_.scrollDown = setTimeout(function() {
2458 delete self.timeouts_.scrollDown;
2459 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2460 }, 10);
2461};
2462
2463/**
2464 * Move the cursor up a specified number of rows.
2465 *
2466 * @param {integer} count The number of rows to move the cursor.
2467 */
2468hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002469 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002470};
2471
2472/**
2473 * Move the cursor down a specified number of rows.
2474 *
2475 * @param {integer} count The number of rows to move the cursor.
2476 */
2477hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002478 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002479 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2480 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2481 this.screenSize.height - 1);
2482
rgindacbbd7482012-06-13 15:06:16 -07002483 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002484 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002485 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002486};
2487
2488/**
2489 * Move the cursor left a specified number of columns.
2490 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002491 * If reverse wraparound mode is enabled and the previous row wrapped into
2492 * the current row then we back up through the wraparound as well.
2493 *
rginda8ba33642011-12-14 12:31:31 -08002494 * @param {integer} count The number of columns to move the cursor.
2495 */
2496hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002497 count = count || 1;
2498
2499 if (count < 1)
2500 return;
2501
2502 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002503 if (this.options_.reverseWraparound) {
2504 if (this.screen_.cursorPosition.overflow) {
2505 // If this cursor is in the right margin, consume one count to get it
2506 // back to the last column. This only applies when we're in reverse
2507 // wraparound mode.
2508 count--;
2509 this.clearCursorOverflow();
2510
2511 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002512 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002513 }
2514
Robert Gindabfb32622014-07-17 13:20:27 -07002515 var newRow = this.screen_.cursorPosition.row;
2516 var newColumn = currentColumn - count;
2517 if (newColumn < 0) {
2518 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2519 if (newRow < 0) {
2520 // xterm also wraps from row 0 to the last row.
2521 newRow = this.screenSize.height + newRow % this.screenSize.height;
2522 }
2523 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2524 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002525
Robert Gindabfb32622014-07-17 13:20:27 -07002526 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2527
2528 } else {
2529 var newColumn = Math.max(currentColumn - count, 0);
2530 this.setCursorColumn(newColumn);
2531 }
rginda8ba33642011-12-14 12:31:31 -08002532};
2533
2534/**
2535 * Move the cursor right a specified number of columns.
2536 *
2537 * @param {integer} count The number of columns to move the cursor.
2538 */
2539hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002540 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002541
2542 if (count < 1)
2543 return;
2544
rgindacbbd7482012-06-13 15:06:16 -07002545 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002546 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002547 this.setCursorColumn(column);
2548};
2549
2550/**
2551 * Reverse the foreground and background colors of the terminal.
2552 *
2553 * This only affects text that was drawn with no attributes.
2554 *
2555 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2556 * been drawn with attributes that happen to coincide with the default
2557 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002558 *
2559 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002560 */
2561hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002562 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002563 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002564 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2565 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002566 } else {
rginda9f5222b2012-03-05 11:53:28 -08002567 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2568 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002569 }
2570};
2571
2572/**
rginda87b86462011-12-14 13:48:03 -08002573 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002574 *
2575 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002576 */
2577hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002578 this.cursorNode_.style.backgroundColor =
2579 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002580
2581 var self = this;
2582 setTimeout(function() {
Matheus Fernandes2d733082017-09-11 06:43:01 -04002583 self.restyleCursor_();
rginda6d397402012-01-17 10:58:29 -08002584 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002585
Michael Kelly485ecd12014-06-09 11:41:56 -04002586 // bellSquelchTimeout_ affects both audio and notification bells.
2587 if (this.bellSquelchTimeout_)
2588 return;
2589
Robert Ginda92e18102013-03-14 13:56:37 -07002590 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002591 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002592 this.bellSequelchTimeout_ = setTimeout(function() {
2593 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002594 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002595 } else {
2596 delete this.bellSquelchTimeout_;
2597 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002598
2599 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingera5fb83c2017-06-22 14:48:35 -07002600 var n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002601 this.bellNotificationList_.push(n);
2602 // TODO: Should we try to raise the window here?
2603 n.onclick = function() { self.closeBellNotifications_(); };
2604 }
rginda87b86462011-12-14 13:48:03 -08002605};
2606
2607/**
rginda8ba33642011-12-14 12:31:31 -08002608 * Set the origin mode bit.
2609 *
2610 * If origin mode is on, certain VT cursor and scrolling commands measure their
2611 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2612 * to the top of the addressable screen.
2613 *
2614 * Defaults to off.
2615 *
2616 * @param {boolean} state True to set origin mode, false to unset.
2617 */
2618hterm.Terminal.prototype.setOriginMode = function(state) {
2619 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002620 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002621};
2622
2623/**
2624 * Set the insert mode bit.
2625 *
2626 * If insert mode is on, existing text beyond the cursor position will be
2627 * shifted right to make room for new text. Otherwise, new text overwrites
2628 * any existing text.
2629 *
2630 * Defaults to off.
2631 *
2632 * @param {boolean} state True to set insert mode, false to unset.
2633 */
2634hterm.Terminal.prototype.setInsertMode = function(state) {
2635 this.options_.insertMode = state;
2636};
2637
2638/**
rginda87b86462011-12-14 13:48:03 -08002639 * Set the auto carriage return bit.
2640 *
2641 * If auto carriage return is on then a formfeed character is interpreted
2642 * as a newline, otherwise it's the same as a linefeed. The difference boils
2643 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002644 *
2645 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002646 */
2647hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2648 this.options_.autoCarriageReturn = state;
2649};
2650
2651/**
rginda8ba33642011-12-14 12:31:31 -08002652 * Set the wraparound mode bit.
2653 *
2654 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2655 * to the start of the following row. Otherwise, the cursor is clamped to the
2656 * end of the screen and attempts to write past it are ignored.
2657 *
2658 * Defaults to on.
2659 *
2660 * @param {boolean} state True to set wraparound mode, false to unset.
2661 */
2662hterm.Terminal.prototype.setWraparound = function(state) {
2663 this.options_.wraparound = state;
2664};
2665
2666/**
2667 * Set the reverse-wraparound mode bit.
2668 *
2669 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2670 * to the end of the previous row. Otherwise, the cursor is clamped to column
2671 * 0.
2672 *
2673 * Defaults to off.
2674 *
2675 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2676 */
2677hterm.Terminal.prototype.setReverseWraparound = function(state) {
2678 this.options_.reverseWraparound = state;
2679};
2680
2681/**
2682 * Selects between the primary and alternate screens.
2683 *
2684 * If alternate mode is on, the alternate screen is active. Otherwise the
2685 * primary screen is active.
2686 *
2687 * Swapping screens has no effect on the scrollback buffer.
2688 *
2689 * Each screen maintains its own cursor position.
2690 *
2691 * Defaults to off.
2692 *
2693 * @param {boolean} state True to set alternate mode, false to unset.
2694 */
2695hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002696 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002697 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2698
rginda35c456b2012-02-09 17:29:05 -08002699 if (this.screen_.rowsArray.length &&
2700 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2701 // If the screen changed sizes while we were away, our rowIndexes may
2702 // be incorrect.
2703 var offset = this.scrollbackRows_.length;
2704 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002705 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002706 ary[i].rowIndex = offset + i;
2707 }
2708 }
rginda8ba33642011-12-14 12:31:31 -08002709
rginda35c456b2012-02-09 17:29:05 -08002710 this.realizeWidth_(this.screenSize.width);
2711 this.realizeHeight_(this.screenSize.height);
2712 this.scrollPort_.syncScrollHeight();
2713 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002714
rginda6d397402012-01-17 10:58:29 -08002715 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002716 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002717};
2718
2719/**
2720 * Set the cursor-blink mode bit.
2721 *
2722 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2723 * a visible cursor does not blink.
2724 *
2725 * You should make sure to turn blinking off if you're going to dispose of a
2726 * terminal, otherwise you'll leak a timeout.
2727 *
2728 * Defaults to on.
2729 *
2730 * @param {boolean} state True to set cursor-blink mode, false to unset.
2731 */
2732hterm.Terminal.prototype.setCursorBlink = function(state) {
2733 this.options_.cursorBlink = state;
2734
2735 if (!state && this.timeouts_.cursorBlink) {
2736 clearTimeout(this.timeouts_.cursorBlink);
2737 delete this.timeouts_.cursorBlink;
2738 }
2739
2740 if (this.options_.cursorVisible)
2741 this.setCursorVisible(true);
2742};
2743
2744/**
2745 * Set the cursor-visible mode bit.
2746 *
2747 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2748 *
2749 * Defaults to on.
2750 *
2751 * @param {boolean} state True to set cursor-visible mode, false to unset.
2752 */
2753hterm.Terminal.prototype.setCursorVisible = function(state) {
2754 this.options_.cursorVisible = state;
2755
2756 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002757 if (this.timeouts_.cursorBlink) {
2758 clearTimeout(this.timeouts_.cursorBlink);
2759 delete this.timeouts_.cursorBlink;
2760 }
rginda87b86462011-12-14 13:48:03 -08002761 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002762 return;
2763 }
2764
rginda87b86462011-12-14 13:48:03 -08002765 this.syncCursorPosition_();
2766
2767 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002768
2769 if (this.options_.cursorBlink) {
2770 if (this.timeouts_.cursorBlink)
2771 return;
2772
Robert Gindaea2183e2014-07-17 09:51:51 -07002773 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002774 } else {
2775 if (this.timeouts_.cursorBlink) {
2776 clearTimeout(this.timeouts_.cursorBlink);
2777 delete this.timeouts_.cursorBlink;
2778 }
2779 }
2780};
2781
2782/**
rginda87b86462011-12-14 13:48:03 -08002783 * Synchronizes the visible cursor and document selection with the current
2784 * cursor coordinates.
Raymes Khourye5d48982018-08-02 09:08:32 +10002785 *
2786 * @return {boolean} True if the cursor is onscreen and synced.
rginda8ba33642011-12-14 12:31:31 -08002787 */
2788hterm.Terminal.prototype.syncCursorPosition_ = function() {
2789 var topRowIndex = this.scrollPort_.getTopRowIndex();
2790 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2791 var cursorRowIndex = this.scrollbackRows_.length +
2792 this.screen_.cursorPosition.row;
2793
Raymes Khoury15697f42018-07-17 11:37:18 +10002794 let forceSyncSelection = false;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002795 if (this.accessibilityReader_.accessibilityEnabled) {
2796 // Report the new position of the cursor for accessibility purposes.
2797 const cursorColumnIndex = this.screen_.cursorPosition.column;
2798 const cursorLineText =
2799 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
Raymes Khoury15697f42018-07-17 11:37:18 +10002800 // This will force the selection to be sync'd to the cursor position if the
2801 // user has pressed a key. Generally we would only sync the cursor position
2802 // when selection is collapsed so that if the user has selected something
2803 // we don't clear the selection by moving the selection. However when a
2804 // screen reader is used, it's intuitive for entering a key to move the
2805 // selection to the cursor.
2806 forceSyncSelection = this.accessibilityReader_.hasUserGesture;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002807 this.accessibilityReader_.afterCursorChange(
2808 cursorLineText, cursorRowIndex, cursorColumnIndex);
2809 }
2810
rginda8ba33642011-12-14 12:31:31 -08002811 if (cursorRowIndex > bottomRowIndex) {
2812 // Cursor is scrolled off screen, move it outside of the visible area.
Mike Frysinger44c32202017-08-05 01:13:09 -04002813 this.setCssVar('cursor-offset-row', '-1');
Raymes Khourye5d48982018-08-02 09:08:32 +10002814 return false;
rginda8ba33642011-12-14 12:31:31 -08002815 }
2816
Robert Gindab837c052014-08-11 11:17:51 -07002817 if (this.options_.cursorVisible &&
2818 this.cursorNode_.style.display == 'none') {
2819 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2820 this.cursorNode_.style.display = '';
2821 }
2822
Mike Frysinger44c32202017-08-05 01:13:09 -04002823 // Position the cursor using CSS variable math. If we do the math in JS,
2824 // the float math will end up being more precise than the CSS which will
2825 // cause the cursor tracking to be off.
2826 this.setCssVar(
2827 'cursor-offset-row',
2828 `${cursorRowIndex - topRowIndex} + ` +
2829 `${this.scrollPort_.visibleRowTopMargin}px`);
2830 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08002831
2832 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04002833 '(' + this.screen_.cursorPosition.column +
2834 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08002835 ')');
2836
2837 // Update the caret for a11y purposes.
2838 var selection = this.document_.getSelection();
Raymes Khoury15697f42018-07-17 11:37:18 +10002839 if (selection && (selection.isCollapsed || forceSyncSelection)) {
rginda87b86462011-12-14 13:48:03 -08002840 this.screen_.syncSelectionCaret(selection);
Raymes Khoury15697f42018-07-17 11:37:18 +10002841 }
Raymes Khourye5d48982018-08-02 09:08:32 +10002842 return true;
rginda8ba33642011-12-14 12:31:31 -08002843};
2844
Robert Gindafb1be6a2013-12-11 11:56:22 -08002845/**
2846 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2847 * and character cell dimensions.
2848 */
Robert Ginda830583c2013-08-07 13:20:46 -07002849hterm.Terminal.prototype.restyleCursor_ = function() {
2850 var shape = this.cursorShape_;
2851
2852 if (this.cursorNode_.getAttribute('focus') == 'false') {
2853 // Always show a block cursor when unfocused.
2854 shape = hterm.Terminal.cursorShape.BLOCK;
2855 }
2856
2857 var style = this.cursorNode_.style;
2858
2859 switch (shape) {
2860 case hterm.Terminal.cursorShape.BEAM:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002861 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002862 style.backgroundColor = 'transparent';
2863 style.borderBottomStyle = null;
2864 style.borderLeftStyle = 'solid';
2865 break;
2866
2867 case hterm.Terminal.cursorShape.UNDERLINE:
2868 style.height = this.scrollPort_.characterSize.baseline + 'px';
2869 style.backgroundColor = 'transparent';
2870 style.borderBottomStyle = 'solid';
2871 // correct the size to put it exactly at the baseline
2872 style.borderLeftStyle = null;
2873 break;
2874
2875 default:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002876 style.height = 'var(--hterm-charsize-height)';
Mike Frysinger2fd079a2018-09-02 01:46:12 -04002877 style.backgroundColor = 'var(--hterm-cursor-color)';
Robert Ginda830583c2013-08-07 13:20:46 -07002878 style.borderBottomStyle = null;
2879 style.borderLeftStyle = null;
2880 break;
2881 }
2882};
2883
rginda8ba33642011-12-14 12:31:31 -08002884/**
2885 * Synchronizes the visible cursor with the current cursor coordinates.
2886 *
2887 * The sync will happen asynchronously, soon after the call stack winds down.
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002888 * Multiple calls will be coalesced into a single sync. This should be called
2889 * prior to the cursor actually changing position.
rginda8ba33642011-12-14 12:31:31 -08002890 */
2891hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2892 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002893 return;
rginda8ba33642011-12-14 12:31:31 -08002894
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002895 if (this.accessibilityReader_.accessibilityEnabled) {
2896 // Report the previous position of the cursor for accessibility purposes.
2897 const cursorRowIndex = this.scrollbackRows_.length +
2898 this.screen_.cursorPosition.row;
2899 const cursorColumnIndex = this.screen_.cursorPosition.column;
2900 const cursorLineText =
2901 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
2902 this.accessibilityReader_.beforeCursorChange(
2903 cursorLineText, cursorRowIndex, cursorColumnIndex);
2904 }
2905
rginda8ba33642011-12-14 12:31:31 -08002906 var self = this;
2907 this.timeouts_.syncCursor = setTimeout(function() {
2908 self.syncCursorPosition_();
2909 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002910 }, 0);
2911};
2912
rgindacc2996c2012-02-24 14:59:31 -08002913/**
rgindaf522ce02012-04-17 17:49:17 -07002914 * Show or hide the zoom warning.
2915 *
2916 * The zoom warning is a message warning the user that their browser zoom must
2917 * be set to 100% in order for hterm to function properly.
2918 *
2919 * @param {boolean} state True to show the message, false to hide it.
2920 */
2921hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2922 if (!this.zoomWarningNode_) {
2923 if (!state)
2924 return;
2925
2926 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002927 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07002928 this.zoomWarningNode_.style.cssText = (
2929 'color: black;' +
2930 'background-color: #ff2222;' +
2931 'font-size: large;' +
2932 'border-radius: 8px;' +
2933 'opacity: 0.75;' +
2934 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2935 'top: 0.5em;' +
2936 'right: 1.2em;' +
2937 'position: absolute;' +
2938 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002939 '-webkit-user-select: none;' +
2940 '-moz-text-size-adjust: none;' +
2941 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05002942
2943 this.zoomWarningNode_.addEventListener('click', function(e) {
2944 this.parentNode.removeChild(this);
2945 });
rgindaf522ce02012-04-17 17:49:17 -07002946 }
2947
Robert Gindab4839c22013-02-28 16:52:10 -08002948 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2949 hterm.zoomWarningMessage,
2950 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2951
rgindaf522ce02012-04-17 17:49:17 -07002952 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2953
2954 if (state) {
2955 if (!this.zoomWarningNode_.parentNode)
2956 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2957 } else if (this.zoomWarningNode_.parentNode) {
2958 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2959 }
2960};
2961
2962/**
rgindacc2996c2012-02-24 14:59:31 -08002963 * Show the terminal overlay for a given amount of time.
2964 *
2965 * The terminal overlay appears in inverse video in a large font, centered
2966 * over the terminal. You should probably keep the overlay message brief,
2967 * since it's in a large font and you probably aren't going to check the size
2968 * of the terminal first.
2969 *
2970 * @param {string} msg The text (not HTML) message to display in the overlay.
2971 * @param {number} opt_timeout The amount of time to wait before fading out
2972 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2973 * stay up forever (or until the next overlay).
2974 */
2975hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002976 if (!this.overlayNode_) {
2977 if (!this.div_)
2978 return;
2979
2980 this.overlayNode_ = this.document_.createElement('div');
2981 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002982 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002983 'font-size: xx-large;' +
2984 'opacity: 0.75;' +
2985 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2986 'position: absolute;' +
2987 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002988 '-webkit-transition: opacity 180ms ease-in;' +
2989 '-moz-user-select: none;' +
2990 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002991
2992 this.overlayNode_.addEventListener('mousedown', function(e) {
2993 e.preventDefault();
2994 e.stopPropagation();
2995 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002996 }
2997
rginda9f5222b2012-03-05 11:53:28 -08002998 this.overlayNode_.style.color = this.prefs_.get('background-color');
2999 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
3000 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
3001
rgindaf0090c92012-02-10 14:58:52 -08003002 this.overlayNode_.textContent = msg;
3003 this.overlayNode_.style.opacity = '0.75';
3004
3005 if (!this.overlayNode_.parentNode)
3006 this.div_.appendChild(this.overlayNode_);
3007
Robert Ginda97769282013-02-01 15:30:30 -08003008 var divSize = hterm.getClientSize(this.div_);
3009 var overlaySize = hterm.getClientSize(this.overlayNode_);
3010
Robert Ginda8a59f762014-07-23 11:29:55 -07003011 this.overlayNode_.style.top =
3012 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08003013 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07003014 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08003015
rgindaf0090c92012-02-10 14:58:52 -08003016 if (this.overlayTimeout_)
3017 clearTimeout(this.overlayTimeout_);
3018
Raymes Khouryc7a06382018-07-04 10:25:45 +10003019 this.accessibilityReader_.assertiveAnnounce(msg);
3020
rgindacc2996c2012-02-24 14:59:31 -08003021 if (opt_timeout === null)
3022 return;
3023
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003024 this.overlayTimeout_ = setTimeout(() => {
3025 this.overlayNode_.style.opacity = '0';
3026 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
3027 }, opt_timeout || 1500);
3028};
3029
3030/**
3031 * Hide the terminal overlay immediately.
3032 *
3033 * Useful when we show an overlay for an event with an unknown end time.
3034 */
3035hterm.Terminal.prototype.hideOverlay = function() {
3036 if (this.overlayTimeout_)
3037 clearTimeout(this.overlayTimeout_);
3038 this.overlayTimeout_ = null;
3039
3040 if (this.overlayNode_.parentNode)
3041 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
3042 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08003043};
3044
rginda4bba5e12012-06-20 16:15:30 -07003045/**
3046 * Paste from the system clipboard to the terminal.
3047 */
3048hterm.Terminal.prototype.paste = function() {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003049 return hterm.pasteFromClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07003050};
3051
3052/**
3053 * Copy a string to the system clipboard.
3054 *
3055 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05003056 *
3057 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07003058 */
3059hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08003060 if (this.prefs_.get('enable-clipboard-notice'))
3061 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
3062
rgindaa09e7332012-08-17 12:49:51 -07003063 var copySource = this.document_.createElement('pre');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04003064 copySource.id = 'hterm:copy-to-clipboard-source';
rginda4bba5e12012-06-20 16:15:30 -07003065 copySource.textContent = str;
3066 copySource.style.cssText = (
3067 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003068 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07003069 'position: absolute;' +
3070 'top: -99px');
3071
3072 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07003073
rginda4bba5e12012-06-20 16:15:30 -07003074 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07003075 var anchorNode = selection.anchorNode;
3076 var anchorOffset = selection.anchorOffset;
3077 var focusNode = selection.focusNode;
3078 var focusOffset = selection.focusOffset;
3079
Mike Frysinger4968d492018-09-28 23:44:31 -04003080 // FF sometimes throws NS_ERROR_FAILURE exceptions when we make this call.
3081 // Catch it because a failure here leaks the copySource node.
3082 // https://bugzilla.mozilla.org/show_bug.cgi?id=1178676
3083 try {
3084 selection.selectAllChildren(copySource);
3085 } catch (ex) {}
rginda4bba5e12012-06-20 16:15:30 -07003086
rgindaa09e7332012-08-17 12:49:51 -07003087 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07003088
Rob Spies56953412014-04-28 14:09:47 -07003089 // IE doesn't support selection.extend. This means that the selection
3090 // won't return on IE.
Robert Ginda6f4f0aa2014-07-28 10:21:39 -07003091 if (selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07003092 selection.collapse(anchorNode, anchorOffset);
3093 selection.extend(focusNode, focusOffset);
3094 }
rgindafaa74742012-08-21 13:34:03 -07003095
rginda4bba5e12012-06-20 16:15:30 -07003096 copySource.parentNode.removeChild(copySource);
3097};
3098
Evan Jones2600d4f2016-12-06 09:29:36 -05003099/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003100 * Display an image.
3101 *
3102 * @param {Object} options The image to display.
3103 * @param {string=} options.name A human readable string for the image.
3104 * @param {string|number=} options.size The size (in bytes).
3105 * @param {boolean=} options.preserveAspectRatio Whether to preserve aspect.
3106 * @param {boolean=} options.inline Whether to display the image inline.
3107 * @param {string|number=} options.width The width of the image.
3108 * @param {string|number=} options.height The height of the image.
3109 * @param {string=} options.align Direction to align the image.
3110 * @param {string} options.uri The source URI for the image.
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003111 * @param {function=} onLoad Callback when loading finishes.
3112 * @param {function(Event)=} onError Callback when loading fails.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003113 */
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003114hterm.Terminal.prototype.displayImage = function(options, onLoad, onError) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003115 // Make sure we're actually given a resource to display.
3116 if (options.uri === undefined)
3117 return;
3118
3119 // Set up the defaults to simplify code below.
3120 if (!options.name)
3121 options.name = '';
3122
3123 // Has the user approved image display yet?
3124 if (this.allowImagesInline !== true) {
3125 this.newLine();
3126 const row = this.getRowNode(this.scrollbackRows_.length +
3127 this.getCursorRow() - 1);
3128
3129 if (this.allowImagesInline === false) {
3130 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
3131 'Inline Images Disabled');
3132 return;
3133 }
3134
3135 // Show a prompt.
3136 let button;
3137 const span = this.document_.createElement('span');
3138 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
3139 span.style.fontWeight = 'bold';
3140 span.style.borderWidth = '1px';
3141 span.style.borderStyle = 'dashed';
3142 button = this.document_.createElement('span');
3143 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3144 button.style.marginLeft = '1em';
3145 button.style.borderWidth = '1px';
3146 button.style.borderStyle = 'solid';
3147 button.addEventListener('click', () => {
3148 this.prefs_.set('allow-images-inline', false);
3149 });
3150 span.appendChild(button);
3151 button = this.document_.createElement('span');
3152 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3153 'allow this session');
3154 button.style.marginLeft = '1em';
3155 button.style.borderWidth = '1px';
3156 button.style.borderStyle = 'solid';
3157 button.addEventListener('click', () => {
3158 this.allowImagesInline = true;
3159 });
3160 span.appendChild(button);
3161 button = this.document_.createElement('span');
3162 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3163 button.style.marginLeft = '1em';
3164 button.style.borderWidth = '1px';
3165 button.style.borderStyle = 'solid';
3166 button.addEventListener('click', () => {
3167 this.prefs_.set('allow-images-inline', true);
3168 });
3169 span.appendChild(button);
3170
3171 row.appendChild(span);
3172 return;
3173 }
3174
3175 // See if we should show this object directly, or download it.
3176 if (options.inline) {
3177 const io = this.io.push();
3178 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
3179 'Loading $1 ...'), null);
3180
3181 // While we're loading the image, eat all the user's input.
3182 io.onVTKeystroke = io.sendString = () => {};
3183
3184 // Initialize this new image.
Adrián Pérez-Orozco6a550322018-08-31 14:36:06 -07003185 const img =
3186 /** @type {!HTMLImageElement} */ (this.document_.createElement('img'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003187 img.src = options.uri;
3188 img.title = img.alt = options.name;
3189
3190 // Attach the image to the page to let it load/render. It won't stay here.
3191 // This is needed so it's visible and the DOM can calculate the height. If
3192 // the image is hidden or not in the DOM, the height is always 0.
3193 this.document_.body.appendChild(img);
3194
3195 // Wait for the image to finish loading before we try moving it to the
3196 // right place in the terminal.
3197 img.onload = () => {
3198 // Now that we have the image dimensions, figure out how to show it.
3199 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
3200 img.style.maxWidth = `${this.document_.body.clientWidth}px`;
3201 img.style.maxHeight = `${this.document_.body.clientHeight}px`;
3202
3203 // Parse a width/height specification.
3204 const parseDim = (dim, maxDim, cssVar) => {
3205 if (!dim || dim == 'auto')
3206 return '';
3207
3208 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3209 if (ary) {
3210 if (ary[2] == '%')
3211 return maxDim * parseInt(ary[1]) / 100 + 'px';
3212 else if (ary[2] == 'px')
3213 return dim;
3214 else
3215 return `calc(${dim} * var(${cssVar}))`;
3216 }
3217
3218 return '';
3219 };
3220 img.style.width =
3221 parseDim(options.width, this.document_.body.clientWidth,
3222 '--hterm-charsize-width');
3223 img.style.height =
3224 parseDim(options.height, this.document_.body.clientHeight,
3225 '--hterm-charsize-height');
3226
3227 // Figure out how many rows the image occupies, then add that many.
3228 // XXX: This count will be inaccurate if the font size changes on us.
3229 const padRows = Math.ceil(img.clientHeight /
3230 this.scrollPort_.characterSize.height);
3231 for (let i = 0; i < padRows; ++i)
3232 this.newLine();
3233
3234 // Update the max height in case the user shrinks the character size.
3235 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3236
3237 // Move the image to the last row. This way when we scroll up, it doesn't
3238 // disappear when the first row gets clipped. It will disappear when we
3239 // scroll down and the last row is clipped ...
3240 this.document_.body.removeChild(img);
3241 // Create a wrapper node so we can do an absolute in a relative position.
3242 // This helps with rounding errors between JS & CSS counts.
3243 const div = this.document_.createElement('div');
3244 div.style.position = 'relative';
3245 div.style.textAlign = options.align;
3246 img.style.position = 'absolute';
3247 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3248 div.appendChild(img);
3249 const row = this.getRowNode(this.scrollbackRows_.length +
3250 this.getCursorRow() - 1);
3251 row.appendChild(div);
3252
3253 io.hideOverlay();
3254 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003255
3256 if (onLoad)
3257 onLoad();
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003258 };
3259
3260 // If we got a malformed image, give up.
3261 img.onerror = (e) => {
3262 this.document_.body.removeChild(img);
3263 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
Mike Frysingere14a8c42018-03-10 00:17:30 -08003264 'Loading $1 failed'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003265 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003266
3267 if (onError)
3268 onError(e);
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003269 };
3270 } else {
3271 // We can't use chrome.downloads.download as that requires "downloads"
3272 // permissions, and that works only in extensions, not apps.
3273 const a = this.document_.createElement('a');
3274 a.href = options.uri;
3275 a.download = options.name;
3276 this.document_.body.appendChild(a);
3277 a.click();
3278 a.remove();
3279 }
3280};
3281
3282/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003283 * Returns the selected text, or null if no text is selected.
3284 *
3285 * @return {string|null}
3286 */
rgindaa09e7332012-08-17 12:49:51 -07003287hterm.Terminal.prototype.getSelectionText = function() {
3288 var selection = this.scrollPort_.selection;
3289 selection.sync();
3290
3291 if (selection.isCollapsed)
3292 return null;
3293
rgindaa09e7332012-08-17 12:49:51 -07003294 // Start offset measures from the beginning of the line.
3295 var startOffset = selection.startOffset;
3296 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003297
Raymes Khoury334625a2018-06-25 10:29:40 +10003298 // If an x-row isn't selected, |node| will be null.
3299 if (!node)
3300 return null;
3301
Robert Gindafdbb3f22012-09-06 20:23:06 -07003302 if (node.nodeName != 'X-ROW') {
3303 // If the selection doesn't start on an x-row node, then it must be
3304 // somewhere inside the x-row. Add any characters from previous siblings
3305 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003306
3307 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3308 // If node is the text node in a styled span, move up to the span node.
3309 node = node.parentNode;
3310 }
3311
Robert Gindafdbb3f22012-09-06 20:23:06 -07003312 while (node.previousSibling) {
3313 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003314 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003315 }
rgindaa09e7332012-08-17 12:49:51 -07003316 }
3317
3318 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08003319 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
3320 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003321 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003322
Robert Gindafdbb3f22012-09-06 20:23:06 -07003323 if (node.nodeName != 'X-ROW') {
3324 // If the selection doesn't end on an x-row node, then it must be
3325 // somewhere inside the x-row. Add any characters from following siblings
3326 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003327
3328 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3329 // If node is the text node in a styled span, move up to the span node.
3330 node = node.parentNode;
3331 }
3332
Robert Gindafdbb3f22012-09-06 20:23:06 -07003333 while (node.nextSibling) {
3334 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003335 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003336 }
rgindaa09e7332012-08-17 12:49:51 -07003337 }
3338
3339 var rv = this.getRowsText(selection.startRow.rowIndex,
3340 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003341 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003342};
3343
rginda4bba5e12012-06-20 16:15:30 -07003344/**
3345 * Copy the current selection to the system clipboard, then clear it after a
3346 * short delay.
3347 */
3348hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07003349 var text = this.getSelectionText();
3350 if (text != null)
3351 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07003352};
3353
rgindaf0090c92012-02-10 14:58:52 -08003354hterm.Terminal.prototype.overlaySize = function() {
3355 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
3356};
3357
rginda87b86462011-12-14 13:48:03 -08003358/**
3359 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3360 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003361 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003362 */
3363hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08003364 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08003365 this.scrollPort_.scrollRowToBottom(this.getRowCount());
3366
Mike Frysinger79669762018-12-30 20:51:10 -05003367 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08003368};
3369
3370/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003371 * Open the selected url.
3372 */
3373hterm.Terminal.prototype.openSelectedUrl_ = function() {
3374 var str = this.getSelectionText();
3375
3376 // If there is no selection, try and expand wherever they clicked.
3377 if (str == null) {
John Lincae9b732018-03-08 13:56:35 +08003378 this.screen_.expandSelectionForUrl(this.document_.getSelection());
Mike Frysinger70b94692017-01-26 18:57:50 -10003379 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003380
3381 // If clicking in empty space, return.
3382 if (str == null)
3383 return;
Mike Frysinger70b94692017-01-26 18:57:50 -10003384 }
3385
3386 // Make sure URL is valid before opening.
3387 if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0)
3388 return;
Mike Frysinger43472622017-06-26 18:11:07 -04003389
3390 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003391 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003392 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3393 // We have to whitelist a few protocols that lack authorities and thus
3394 // never use the //. Like mailto.
3395 switch (str.split(':', 1)[0]) {
3396 case 'mailto':
3397 break;
3398 default:
3399 str = 'http://' + str;
3400 break;
3401 }
3402 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003403
Mike Frysinger720fa832017-10-23 01:15:52 -04003404 hterm.openUrl(str);
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003405};
Mike Frysinger70b94692017-01-26 18:57:50 -10003406
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003407/**
3408 * Manage the automatic mouse hiding behavior while typing.
3409 *
3410 * @param {boolean=} v Whether to enable automatic hiding.
3411 */
3412hterm.Terminal.prototype.setAutomaticMouseHiding = function(v=null) {
3413 // Since Chrome OS & macOS do this by default everywhere, we don't need to.
3414 // Linux & Windows seem to leave this to specific applications to manage.
3415 if (v === null)
3416 v = (hterm.os != 'cros' && hterm.os != 'mac');
3417
3418 this.mouseHideWhileTyping_ = !!v;
3419};
3420
3421/**
3422 * Handler for monitoring user keyboard activity.
3423 *
3424 * This isn't for processing the keystrokes directly, but for updating any
3425 * state that might toggle based on the user using the keyboard at all.
3426 *
3427 * @param {KeyboardEvent} e The keyboard event that triggered us.
3428 */
3429hterm.Terminal.prototype.onKeyboardActivity_ = function(e) {
3430 // When the user starts typing, hide the mouse cursor.
3431 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_)
3432 this.setCssVar('mouse-cursor-style', 'none');
3433};
Mike Frysinger70b94692017-01-26 18:57:50 -10003434
3435/**
rgindad5613292012-06-19 15:40:37 -07003436 * Add the terminalRow and terminalColumn properties to mouse events and
3437 * then forward on to onMouse().
3438 *
3439 * The terminalRow and terminalColumn properties contain the (row, column)
3440 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003441 *
3442 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003443 */
3444hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003445 if (e.processedByTerminalHandler_) {
3446 // We register our event handlers on the document, as well as the cursor
3447 // and the scroll blocker. Mouse events that occur on the cursor or
3448 // scroll blocker will also appear on the document, but we don't want to
3449 // process them twice.
3450 //
3451 // We can't just prevent bubbling because that has other side effects, so
3452 // we decorate the event object with this property instead.
3453 return;
3454 }
3455
Mike Frysinger468966c2018-08-28 13:48:51 -04003456 // Consume navigation events. Button 3 is usually "browser back" and
3457 // button 4 is "browser forward" which we don't want to happen.
3458 if (e.button > 2) {
3459 e.preventDefault();
3460 // We don't return so click events can be passed to the remote below.
3461 }
3462
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003463 var reportMouseEvents = (!this.defeatMouseReports_ &&
3464 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3465
rgindafaa74742012-08-21 13:34:03 -07003466 e.processedByTerminalHandler_ = true;
3467
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003468 // Handle auto hiding of mouse cursor while typing.
3469 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
3470 // Make sure the mouse cursor is visible.
3471 this.syncMouseStyle();
3472 // This debounce isn't perfect, but should work well enough for such a
3473 // simple implementation. If the user moved the mouse, we enabled this
3474 // debounce, and then moved the mouse just before the timeout, we wouldn't
3475 // debounce that later movement.
3476 this.mouseHideDelay_ = setTimeout(() => this.mouseHideDelay_ = null, 1000);
3477 }
3478
Robert Gindaeda48db2014-07-17 09:25:30 -07003479 // One based row/column stored on the mouse event.
3480 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
3481 this.scrollPort_.characterSize.height) + 1;
3482 e.terminalColumn = parseInt(e.clientX /
3483 this.scrollPort_.characterSize.width) + 1;
3484
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003485 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
3486 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07003487 return;
3488 }
3489
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003490 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07003491 // If the cursor is visible and we're not sending mouse events to the
3492 // host app, then we want to hide the terminal cursor when the mouse
3493 // cursor is over top. This keeps the terminal cursor from interfering
3494 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003495 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3496 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3497 this.cursorNode_.style.display = 'none';
3498 } else if (this.cursorNode_.style.display == 'none') {
3499 this.cursorNode_.style.display = '';
3500 }
3501 }
rgindad5613292012-06-19 15:40:37 -07003502
Robert Ginda928cf632014-03-05 15:07:41 -08003503 if (e.type == 'mousedown') {
Mike Frysingercc114512017-09-11 21:39:17 -04003504 this.contextMenu.hide(e);
3505
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003506 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003507 // If VT mouse reporting is disabled, or has been defeated with
3508 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003509 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003510 this.setSelectionEnabled(true);
3511 } else {
3512 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003513 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003514 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003515 this.setSelectionEnabled(false);
3516 e.preventDefault();
3517 }
3518 }
3519
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003520 if (!reportMouseEvents) {
John Lin2aad22e2018-03-16 13:58:11 +08003521 if (e.type == 'dblclick') {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003522 this.screen_.expandSelection(this.document_.getSelection());
John Lin2aad22e2018-03-16 13:58:11 +08003523 if (this.copyOnSelect)
3524 this.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07003525 }
3526
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003527 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003528 // Debounce this event with the dblclick event. If you try to doubleclick
3529 // a URL to open it, Chrome will fire click then dblclick, but we won't
3530 // have expanded the selection text at the first click event.
3531 clearTimeout(this.timeouts_.openUrl);
3532 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3533 500);
3534 return;
3535 }
3536
Mike Frysinger847577f2017-05-23 23:25:57 -04003537 if (e.type == 'mousedown') {
Mike Frysingercc114512017-09-11 21:39:17 -04003538 if (e.ctrlKey && e.button == 2 /* right button */) {
3539 e.preventDefault();
3540 this.contextMenu.show(e, this);
3541 } else if (e.button == this.mousePasteButton ||
3542 (this.mouseRightClickPaste && e.button == 2 /* right button */)) {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003543 if (!this.paste())
Mike Frysinger05a57f02017-08-27 17:48:55 -04003544 console.warn('Could not paste manually due to web restrictions');
Mike Frysinger847577f2017-05-23 23:25:57 -04003545 }
3546 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003547
Mike Frysinger2edd3612017-05-24 00:54:39 -04003548 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003549 !this.document_.getSelection().isCollapsed) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003550 this.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003551 }
3552
3553 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3554 this.scrollBlockerNode_.engaged) {
3555 // Disengage the scroll-blocker after one of these events.
3556 this.scrollBlockerNode_.engaged = false;
3557 this.scrollBlockerNode_.style.top = '-99px';
3558 }
3559
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003560 // Emulate arrow key presses via scroll wheel events.
3561 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3562 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003563 if (e.type == 'wheel') {
Mike Frysinger321063c2018-08-29 15:33:14 -04003564 const delta = this.scrollPort_.scrollWheelDelta(e);
Mike Frysingerc3030a82017-05-29 14:16:11 -04003565
Mike Frysinger321063c2018-08-29 15:33:14 -04003566 // Helper to turn a wheel event delta into a series of key presses.
3567 const deltaToArrows = (distance, charSize, arrowPos, arrowNeg) => {
3568 if (distance == 0) {
3569 return '';
3570 }
3571
3572 // Convert the scroll distance into a number of rows/cols.
3573 const cells = lib.f.smartFloorDivide(Math.abs(distance), charSize);
3574 const data = '\x1bO' + (distance < 0 ? arrowNeg : arrowPos);
3575 return data.repeat(cells);
3576 };
3577
3578 // The order between up/down and left/right doesn't really matter.
3579 this.io.sendString(
3580 // Up/down arrow keys.
3581 deltaToArrows(delta.y, this.scrollPort_.characterSize.height,
3582 'A', 'B') +
3583 // Left/right arrow keys.
3584 deltaToArrows(delta.x, this.scrollPort_.characterSize.width,
3585 'C', 'D')
3586 );
Mike Frysingerc3030a82017-05-29 14:16:11 -04003587
3588 e.preventDefault();
3589 }
3590 }
Robert Ginda928cf632014-03-05 15:07:41 -08003591 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003592 if (!this.scrollBlockerNode_.engaged) {
3593 if (e.type == 'mousedown') {
3594 // Move the scroll-blocker into place if we want to keep the scrollport
3595 // from scrolling.
3596 this.scrollBlockerNode_.engaged = true;
3597 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3598 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3599 } else if (e.type == 'mousemove') {
3600 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3601 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003602 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003603 e.preventDefault();
3604 }
3605 }
Robert Ginda928cf632014-03-05 15:07:41 -08003606
3607 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003608 }
3609
Robert Ginda928cf632014-03-05 15:07:41 -08003610 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3611 // Restore this on mouseup in case it was temporarily defeated with a
3612 // alt-mousedown. Only do this when the selection is empty so that
3613 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003614 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003615 }
rgindad5613292012-06-19 15:40:37 -07003616};
3617
3618/**
3619 * Clients should override this if they care to know about mouse events.
3620 *
3621 * The event parameter will be a normal DOM mouse click event with additional
3622 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003623 *
3624 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003625 */
3626hterm.Terminal.prototype.onMouse = function(e) { };
3627
3628/**
rginda8e92a692012-05-20 19:37:20 -07003629 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003630 *
3631 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003632 */
Rob Spies06533ba2014-04-24 11:20:37 -07003633hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3634 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003635 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003636
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003637 if (this.reportFocus)
3638 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O');
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003639
Michael Kelly485ecd12014-06-09 11:41:56 -04003640 if (focused === true)
3641 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07003642};
3643
3644/**
rginda8ba33642011-12-14 12:31:31 -08003645 * React when the ScrollPort is scrolled.
3646 */
3647hterm.Terminal.prototype.onScroll_ = function() {
3648 this.scheduleSyncCursorPosition_();
3649};
3650
3651/**
rginda9846e2f2012-01-27 13:53:33 -08003652 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003653 *
3654 * @param {Event} e The DOM paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08003655 */
3656hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaa063b202014-07-21 11:08:25 -07003657 var data = e.text.replace(/\n/mg, '\r');
Mike Frysingere8c32c82018-03-11 14:57:28 -07003658 if (this.options_.bracketedPaste) {
3659 // We strip out most escape sequences as they can cause issues (like
3660 // inserting an \x1b[201~ midstream). We pass through whitespace
3661 // though: 0x08:\b 0x09:\t 0x0a:\n 0x0d:\r.
3662 // This matches xterm behavior.
3663 const filter = (data) => data.replace(/[\x00-\x07\x0b-\x0c\x0e-\x1f]/g, '');
3664 data = '\x1b[200~' + filter(data) + '\x1b[201~';
3665 }
Robert Gindaa063b202014-07-21 11:08:25 -07003666
3667 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003668};
3669
3670/**
rgindaa09e7332012-08-17 12:49:51 -07003671 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003672 *
3673 * @param {Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07003674 */
3675hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07003676 if (!this.useDefaultWindowCopy) {
3677 e.preventDefault();
3678 setTimeout(this.copySelectionToClipboard.bind(this), 0);
3679 }
rgindaa09e7332012-08-17 12:49:51 -07003680};
3681
3682/**
rginda8ba33642011-12-14 12:31:31 -08003683 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08003684 *
3685 * Note: This function should not directly contain code that alters the internal
3686 * state of the terminal. That kind of code belongs in realizeWidth or
3687 * realizeHeight, so that it can be executed synchronously in the case of a
3688 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08003689 */
3690hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08003691 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07003692 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08003693 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07003694 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08003695
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003696 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08003697 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003698 // gets removed from the document or during the initial load, and we can't
3699 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07003700 // This can also happen if called before the scrollPort calculates the
3701 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08003702 return;
3703 }
3704
rgindaa8ba17d2012-08-15 14:41:10 -07003705 var isNewSize = (columnCount != this.screenSize.width ||
3706 rowCount != this.screenSize.height);
3707
3708 // We do this even if the size didn't change, just to be sure everything is
3709 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04003710 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07003711 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07003712
3713 if (isNewSize)
3714 this.overlaySize();
3715
Robert Gindafb1be6a2013-12-11 11:56:22 -08003716 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07003717 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08003718};
3719
3720/**
3721 * Service the cursor blink timeout.
3722 */
3723hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07003724 if (!this.options_.cursorBlink) {
3725 delete this.timeouts_.cursorBlink;
3726 return;
3727 }
3728
Robert Ginda830583c2013-08-07 13:20:46 -07003729 if (this.cursorNode_.getAttribute('focus') == 'false' ||
3730 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08003731 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07003732 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3733 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08003734 } else {
rginda87b86462011-12-14 13:48:03 -08003735 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07003736 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3737 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08003738 }
3739};
David Reveman8f552492012-03-28 12:18:41 -04003740
3741/**
3742 * Set the scrollbar-visible mode bit.
3743 *
3744 * If scrollbar-visible is on, the vertical scrollbar will be visible.
3745 * Otherwise it will not.
3746 *
3747 * Defaults to on.
3748 *
3749 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
3750 */
3751hterm.Terminal.prototype.setScrollbarVisible = function(state) {
3752 this.scrollPort_.setScrollbarVisible(state);
3753};
Michael Kelly485ecd12014-06-09 11:41:56 -04003754
3755/**
Rob Spies49039e52014-12-17 13:40:04 -08003756 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04003757 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08003758 *
3759 * Defaults to 1.
3760 *
Evan Jones2600d4f2016-12-06 09:29:36 -05003761 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08003762 */
3763hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3764 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3765};
3766
3767/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003768 * Close all web notifications created by terminal bells.
3769 */
3770hterm.Terminal.prototype.closeBellNotifications_ = function() {
3771 this.bellNotificationList_.forEach(function(n) {
3772 n.close();
3773 });
3774 this.bellNotificationList_.length = 0;
3775};
Raymes Khourye5d48982018-08-02 09:08:32 +10003776
3777/**
3778 * Syncs the cursor position when the scrollport gains focus.
3779 */
3780hterm.Terminal.prototype.onScrollportFocus_ = function() {
3781 // If the cursor is offscreen we set selection to the last row on the screen.
3782 const topRowIndex = this.scrollPort_.getTopRowIndex();
3783 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
3784 const selection = this.document_.getSelection();
3785 if (!this.syncCursorPosition_() && selection) {
3786 selection.collapse(this.getRowNode(bottomRowIndex));
3787 }
3788};