blob: cd673bb508100403eb17c3c4c442eacbb7558b58 [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
rginda8ba33642011-12-14 12:31:31 -08007/**
8 * Constructor for the Terminal class.
9 *
10 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
11 * classes to provide the complete terminal functionality.
12 *
13 * There are a number of lower-level Terminal methods that can be called
14 * directly to manipulate the cursor, text, scroll region, and other terminal
15 * attributes. However, the primary method is interpret(), which parses VT
16 * escape sequences and invokes the appropriate Terminal methods.
17 *
18 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
19 *
20 * TODO(rginda): Eventually we're going to need to support characters which are
21 * displayed twice as wide as standard latin characters. This is to support
22 * CJK (and possibly other character sets).
rginda9f5222b2012-03-05 11:53:28 -080023 *
Robert Ginda57f03b42012-09-13 11:02:48 -070024 * @param {string} opt_profileId Optional preference profile name. If not
rginda9f5222b2012-03-05 11:53:28 -080025 * provided, defaults to 'default'.
rginda8ba33642011-12-14 12:31:31 -080026 */
Robert Ginda57f03b42012-09-13 11:02:48 -070027hterm.Terminal = function(opt_profileId) {
28 this.profileId_ = null;
rginda9f5222b2012-03-05 11:53:28 -080029
rginda8ba33642011-12-14 12:31:31 -080030 // Two screen instances.
31 this.primaryScreen_ = new hterm.Screen();
32 this.alternateScreen_ = new hterm.Screen();
33
34 // The "current" screen.
35 this.screen_ = this.primaryScreen_;
36
rginda8ba33642011-12-14 12:31:31 -080037 // The local notion of the screen size. ScreenBuffers also have a size which
38 // indicates their present size. During size changes, the two may disagree.
39 // Also, the inactive screen's size is not altered until it is made the active
40 // screen.
41 this.screenSize = new hterm.Size(0, 0);
42
rginda8ba33642011-12-14 12:31:31 -080043 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080044 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080045 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
46 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080047 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
Raymes Khourye5d48982018-08-02 09:08:32 +100048 this.scrollPort_.subscribe('focus', this.onScrollportFocus_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070049 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080050
rginda87b86462011-12-14 13:48:03 -080051 // The div that contains this terminal.
52 this.div_ = null;
53
rgindac9bc5502012-01-18 11:48:44 -080054 // The document that contains the scrollPort. Defaulted to the global
55 // document here so that the terminal is functional even if it hasn't been
56 // inserted into a document yet, but re-set in decorate().
57 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080058
rginda8ba33642011-12-14 12:31:31 -080059 // The rows that have scrolled off screen and are no longer addressable.
60 this.scrollbackRows_ = [];
61
rgindac9bc5502012-01-18 11:48:44 -080062 // Saved tab stops.
63 this.tabStops_ = [];
64
David Benjamin66e954d2012-05-05 21:08:12 -040065 // Keep track of whether default tab stops have been erased; after a TBC
66 // clears all tab stops, defaults aren't restored on resize until a reset.
67 this.defaultTabStops = true;
68
rginda8ba33642011-12-14 12:31:31 -080069 // The VT's notion of the top and bottom rows. Used during some VT
70 // cursor positioning and scrolling commands.
71 this.vtScrollTop_ = null;
72 this.vtScrollBottom_ = null;
73
74 // The DIV element for the visible cursor.
75 this.cursorNode_ = null;
76
Robert Ginda830583c2013-08-07 13:20:46 -070077 // The current cursor shape of the terminal.
78 this.cursorShape_ = hterm.Terminal.cursorShape.BLOCK;
79
Robert Gindaea2183e2014-07-17 09:51:51 -070080 // Cursor blink on/off cycle in ms, overwritten by prefs once they're loaded.
81 this.cursorBlinkCycle_ = [100, 100];
82
83 // Pre-bound onCursorBlink_ handler, so we don't have to do this for each
84 // cursor on/off servicing.
85 this.myOnCursorBlink_ = this.onCursorBlink_.bind(this);
86
rginda9f5222b2012-03-05 11:53:28 -080087 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -070088 // each output and keystroke. They are initialized by the preference manager.
Robert Ginda8cb7d902013-06-20 14:37:18 -070089 this.backgroundColor_ = null;
90 this.foregroundColor_ = null;
Robert Ginda57f03b42012-09-13 11:02:48 -070091 this.scrollOnOutput_ = null;
92 this.scrollOnKeystroke_ = null;
Mike Frysinger3c9fa072017-07-13 10:21:13 -040093 this.scrollWheelArrowKeys_ = null;
rginda9f5222b2012-03-05 11:53:28 -080094
Robert Ginda6aec7eb2015-06-16 10:31:30 -070095 // True if we should override mouse event reporting to allow local selection.
96 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -080097
Mike Frysinger02ded6d2018-06-21 14:25:20 -040098 // Whether to auto hide the mouse cursor when typing.
99 this.setAutomaticMouseHiding();
100 // Timer to keep mouse visible while it's being used.
101 this.mouseHideDelay_ = null;
102
rgindaf0090c92012-02-10 14:58:52 -0800103 // Terminal bell sound.
104 this.bellAudio_ = this.document_.createElement('audio');
Mike Frysingerd826f1a2017-07-06 16:20:06 -0400105 this.bellAudio_.id = 'hterm:bell-audio';
rgindaf0090c92012-02-10 14:58:52 -0800106 this.bellAudio_.setAttribute('preload', 'auto');
107
Raymes Khoury3e44bc92018-05-17 10:54:23 +1000108 // The AccessibilityReader object for announcing command output.
109 this.accessibilityReader_ = null;
110
Mike Frysingercc114512017-09-11 21:39:17 -0400111 // The context menu object.
112 this.contextMenu = new hterm.ContextMenu();
113
Michael Kelly485ecd12014-06-09 11:41:56 -0400114 // All terminal bell notifications that have been generated (not necessarily
115 // shown).
116 this.bellNotificationList_ = [];
117
118 // Whether we have permission to display notifications.
119 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400120
rginda6d397402012-01-17 10:58:29 -0800121 // Cursor position and attributes saved with DECSC.
122 this.savedOptions_ = {};
123
rginda8ba33642011-12-14 12:31:31 -0800124 // The current mode bits for the terminal.
125 this.options_ = new hterm.Options();
126
127 // Timeouts we might need to clear.
128 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800129
130 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800131 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800132
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800133 this.saveCursorAndState(true);
134
Zhu Qunying30d40712017-03-14 16:27:00 -0700135 // The keyboard handler.
rgindafeaf3142012-01-31 15:14:20 -0800136 this.keyboard = new hterm.Keyboard(this);
137
rginda87b86462011-12-14 13:48:03 -0800138 // General IO interface that can be given to third parties without exposing
139 // the entire terminal object.
140 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800141
rgindad5613292012-06-19 15:40:37 -0700142 // True if mouse-click-drag should scroll the terminal.
143 this.enableMouseDragScroll = true;
144
Robert Ginda57f03b42012-09-13 11:02:48 -0700145 this.copyOnSelect = null;
Mike Frysinger847577f2017-05-23 23:25:57 -0400146 this.mouseRightClickPaste = null;
rginda4bba5e12012-06-20 16:15:30 -0700147 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700148
Zhu Qunying30d40712017-03-14 16:27:00 -0700149 // Whether to use the default window copy behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700150 this.useDefaultWindowCopy = false;
151
152 this.clearSelectionAfterCopy = true;
153
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400154 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800155 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700156
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400157 // Whether we allow images to be shown.
158 this.allowImagesInline = null;
159
Gabriel Holodake8a09be2017-10-10 01:07:11 -0400160 this.reportFocus = false;
161
Robert Ginda57f03b42012-09-13 11:02:48 -0700162 this.setProfile(opt_profileId || 'default',
Evan Jones5f9df812016-12-06 09:38:58 -0500163 function() { this.onTerminalReady(); }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800164};
165
166/**
Robert Ginda830583c2013-08-07 13:20:46 -0700167 * Possible cursor shapes.
168 */
169hterm.Terminal.cursorShape = {
170 BLOCK: 'BLOCK',
171 BEAM: 'BEAM',
172 UNDERLINE: 'UNDERLINE'
173};
174
175/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700176 * Clients should override this to be notified when the terminal is ready
177 * for use.
178 *
179 * The terminal initialization is asynchronous, and shouldn't be used before
180 * this method is called.
181 */
182hterm.Terminal.prototype.onTerminalReady = function() { };
183
184/**
rginda35c456b2012-02-09 17:29:05 -0800185 * Default tab with of 8 to match xterm.
186 */
187hterm.Terminal.prototype.tabWidth = 8;
188
189/**
rginda9f5222b2012-03-05 11:53:28 -0800190 * Select a preference profile.
191 *
192 * This will load the terminal preferences for the given profile name and
193 * associate subsequent preference changes with the new preference profile.
194 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500195 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800196 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700197 * @param {function} opt_callback Optional callback to invoke when the profile
198 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800199 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700200hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
201 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800202
Robert Ginda57f03b42012-09-13 11:02:48 -0700203 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800204
Robert Ginda57f03b42012-09-13 11:02:48 -0700205 if (this.prefs_)
206 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800207
Robert Ginda57f03b42012-09-13 11:02:48 -0700208 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
209 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800210 'alt-gr-mode': function(v) {
211 if (v == null) {
212 if (navigator.language.toLowerCase() == 'en-us') {
213 v = 'none';
214 } else {
215 v = 'right-alt';
216 }
217 } else if (typeof v == 'string') {
218 v = v.toLowerCase();
219 } else {
220 v = 'none';
221 }
222
223 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v))
224 v = 'none';
225
226 terminal.keyboard.altGrMode = v;
227 },
228
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700229 'alt-backspace-is-meta-backspace': function(v) {
230 terminal.keyboard.altBackspaceIsMetaBackspace = v;
231 },
232
Robert Ginda57f03b42012-09-13 11:02:48 -0700233 'alt-is-meta': function(v) {
234 terminal.keyboard.altIsMeta = v;
235 },
236
237 'alt-sends-what': function(v) {
238 if (!/^(escape|8-bit|browser-key)$/.test(v))
239 v = 'escape';
240
241 terminal.keyboard.altSendsWhat = v;
242 },
243
244 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800245 var ary = v.match(/^lib-resource:(\S+)/);
246 if (ary) {
247 terminal.bellAudio_.setAttribute('src',
248 lib.resource.getDataUrl(ary[1]));
249 } else {
250 terminal.bellAudio_.setAttribute('src', v);
251 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700252 },
253
Michael Kelly485ecd12014-06-09 11:41:56 -0400254 'desktop-notification-bell': function(v) {
255 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700256 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400257 Notification.permission === 'granted';
258 if (!terminal.desktopNotificationBell_) {
259 // Note: We don't call Notification.requestPermission here because
260 // Chrome requires the call be the result of a user action (such as an
261 // onclick handler), and pref listeners are run asynchronously.
262 //
263 // A way of working around this would be to display a dialog in the
264 // terminal with a "click-to-request-permission" button.
265 console.warn('desktop-notification-bell is true but we do not have ' +
266 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400267 }
268 } else {
269 terminal.desktopNotificationBell_ = false;
270 }
271 },
272
Robert Ginda57f03b42012-09-13 11:02:48 -0700273 'background-color': function(v) {
274 terminal.setBackgroundColor(v);
275 },
276
277 'background-image': function(v) {
278 terminal.scrollPort_.setBackgroundImage(v);
279 },
280
281 'background-size': function(v) {
282 terminal.scrollPort_.setBackgroundSize(v);
283 },
284
285 'background-position': function(v) {
286 terminal.scrollPort_.setBackgroundPosition(v);
287 },
288
289 'backspace-sends-backspace': function(v) {
290 terminal.keyboard.backspaceSendsBackspace = v;
291 },
292
Brad Town18654b62015-03-12 00:27:45 -0700293 'character-map-overrides': function(v) {
294 if (!(v == null || v instanceof Object)) {
295 console.warn('Preference character-map-modifications is not an ' +
296 'object: ' + v);
297 return;
298 }
299
Mike Frysinger095d4062017-06-14 00:29:48 -0700300 terminal.vt.characterMaps.reset();
301 terminal.vt.characterMaps.setOverrides(v);
Brad Town18654b62015-03-12 00:27:45 -0700302 },
303
Robert Ginda57f03b42012-09-13 11:02:48 -0700304 'cursor-blink': function(v) {
305 terminal.setCursorBlink(!!v);
306 },
307
Robert Gindaea2183e2014-07-17 09:51:51 -0700308 'cursor-blink-cycle': function(v) {
309 if (v instanceof Array &&
310 typeof v[0] == 'number' &&
311 typeof v[1] == 'number') {
312 terminal.cursorBlinkCycle_ = v;
313 } else if (typeof v == 'number') {
314 terminal.cursorBlinkCycle_ = [v, v];
315 } else {
316 // Fast blink indicates an error.
317 terminal.cursorBlinkCycle_ = [100, 100];
318 }
319 },
320
Robert Ginda57f03b42012-09-13 11:02:48 -0700321 'cursor-color': function(v) {
322 terminal.setCursorColor(v);
323 },
324
325 'color-palette-overrides': function(v) {
326 if (!(v == null || v instanceof Object || v instanceof Array)) {
327 console.warn('Preference color-palette-overrides is not an array or ' +
328 'object: ' + v);
329 return;
rginda9f5222b2012-03-05 11:53:28 -0800330 }
rginda9f5222b2012-03-05 11:53:28 -0800331
Robert Ginda57f03b42012-09-13 11:02:48 -0700332 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700333
Robert Ginda57f03b42012-09-13 11:02:48 -0700334 if (v) {
335 for (var key in v) {
336 var i = parseInt(key);
337 if (isNaN(i) || i < 0 || i > 255) {
338 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
339 continue;
340 }
341
342 if (v[i]) {
343 var rgb = lib.colors.normalizeCSS(v[i]);
344 if (rgb)
345 lib.colors.colorPalette[i] = rgb;
346 }
347 }
rginda30f20f62012-04-05 16:36:19 -0700348 }
rginda30f20f62012-04-05 16:36:19 -0700349
Evan Jones5f9df812016-12-06 09:38:58 -0500350 terminal.primaryScreen_.textAttributes.resetColorPalette();
Robert Ginda57f03b42012-09-13 11:02:48 -0700351 terminal.alternateScreen_.textAttributes.resetColorPalette();
352 },
rginda30f20f62012-04-05 16:36:19 -0700353
Robert Ginda57f03b42012-09-13 11:02:48 -0700354 'copy-on-select': function(v) {
355 terminal.copyOnSelect = !!v;
356 },
rginda9f5222b2012-03-05 11:53:28 -0800357
Rob Spies0bec09b2014-06-06 15:58:09 -0700358 'use-default-window-copy': function(v) {
359 terminal.useDefaultWindowCopy = !!v;
360 },
361
362 'clear-selection-after-copy': function(v) {
363 terminal.clearSelectionAfterCopy = !!v;
364 },
365
Robert Ginda7e5e9522014-03-14 12:23:58 -0700366 'ctrl-plus-minus-zero-zoom': function(v) {
367 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
368 },
369
Robert Gindafb5a3f92014-05-13 14:12:00 -0700370 'ctrl-c-copy': function(v) {
371 terminal.keyboard.ctrlCCopy = v;
372 },
373
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100374 'ctrl-v-paste': function(v) {
375 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700376 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100377 },
378
Cody Coljee-Gray7c6a0392018-10-25 13:18:28 -0700379 'paste-on-drop': function(v) {
380 terminal.scrollPort_.setPasteOnDrop(v);
381 },
382
Masaya Suzuki273aa982014-05-31 07:25:55 +0900383 'east-asian-ambiguous-as-two-column': function(v) {
384 lib.wc.regardCjkAmbiguous = v;
385 },
386
Robert Ginda57f03b42012-09-13 11:02:48 -0700387 'enable-8-bit-control': function(v) {
388 terminal.vt.enable8BitControl = !!v;
389 },
rginda30f20f62012-04-05 16:36:19 -0700390
Robert Ginda57f03b42012-09-13 11:02:48 -0700391 'enable-bold': function(v) {
392 terminal.syncBoldSafeState();
393 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400394
Robert Ginda3e278d72014-03-25 13:18:51 -0700395 'enable-bold-as-bright': function(v) {
396 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
397 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
398 },
399
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400400 'enable-blink': function(v) {
Mike Frysinger261597c2017-12-28 01:14:21 -0500401 terminal.setTextBlink(!!v);
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400402 },
403
Robert Ginda57f03b42012-09-13 11:02:48 -0700404 'enable-clipboard-write': function(v) {
405 terminal.vt.enableClipboardWrite = !!v;
406 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400407
Robert Ginda3755e752013-05-31 13:34:09 -0700408 'enable-dec12': function(v) {
409 terminal.vt.enableDec12 = !!v;
410 },
411
Mike Frysinger38f267d2018-09-07 02:50:59 -0400412 'enable-csi-j-3': function(v) {
413 terminal.vt.enableCsiJ3 = !!v;
414 },
415
Robert Ginda57f03b42012-09-13 11:02:48 -0700416 'font-family': function(v) {
417 terminal.syncFontFamily();
418 },
rginda30f20f62012-04-05 16:36:19 -0700419
Robert Ginda57f03b42012-09-13 11:02:48 -0700420 'font-size': function(v) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500421 v = parseInt(v);
422 if (v <= 0) {
423 console.error(`Invalid font size: ${v}`);
424 return;
425 }
426
Robert Ginda57f03b42012-09-13 11:02:48 -0700427 terminal.setFontSize(v);
428 },
rginda9875d902012-08-20 16:21:57 -0700429
Robert Ginda57f03b42012-09-13 11:02:48 -0700430 'font-smoothing': function(v) {
431 terminal.syncFontFamily();
432 },
rgindade84e382012-04-20 15:39:31 -0700433
Robert Ginda57f03b42012-09-13 11:02:48 -0700434 'foreground-color': function(v) {
435 terminal.setForegroundColor(v);
436 },
rginda30f20f62012-04-05 16:36:19 -0700437
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400438 'hide-mouse-while-typing': function(v) {
439 terminal.setAutomaticMouseHiding(v);
440 },
441
Robert Ginda57f03b42012-09-13 11:02:48 -0700442 'home-keys-scroll': function(v) {
443 terminal.keyboard.homeKeysScroll = v;
444 },
rginda4bba5e12012-06-20 16:15:30 -0700445
Robert Gindaa8165692015-06-15 14:46:31 -0700446 'keybindings': function(v) {
447 terminal.keyboard.bindings.clear();
448
449 if (!v)
450 return;
451
452 if (!(v instanceof Object)) {
453 console.error('Error in keybindings preference: Expected object');
454 return;
455 }
456
457 try {
458 terminal.keyboard.bindings.addBindings(v);
459 } catch (ex) {
460 console.error('Error in keybindings preference: ' + ex);
461 }
462 },
463
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700464 'media-keys-are-fkeys': function(v) {
465 terminal.keyboard.mediaKeysAreFKeys = v;
466 },
467
Robert Ginda57f03b42012-09-13 11:02:48 -0700468 'meta-sends-escape': function(v) {
469 terminal.keyboard.metaSendsEscape = v;
470 },
rginda30f20f62012-04-05 16:36:19 -0700471
Mike Frysinger847577f2017-05-23 23:25:57 -0400472 'mouse-right-click-paste': function(v) {
473 terminal.mouseRightClickPaste = v;
474 },
475
Robert Ginda57f03b42012-09-13 11:02:48 -0700476 'mouse-paste-button': function(v) {
477 terminal.syncMousePasteButton();
478 },
rgindaa8ba17d2012-08-15 14:41:10 -0700479
Robert Gindae76aa9f2014-03-14 12:29:12 -0700480 'page-keys-scroll': function(v) {
481 terminal.keyboard.pageKeysScroll = v;
482 },
483
Robert Ginda40932892012-12-10 17:26:40 -0800484 'pass-alt-number': function(v) {
485 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800486 // Let Alt-1..9 pass to the browser (to control tab switching) on
487 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500488 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800489 }
490
491 terminal.passAltNumber = v;
492 },
493
494 'pass-ctrl-number': function(v) {
495 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800496 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
497 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500498 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800499 }
500
501 terminal.passCtrlNumber = v;
502 },
503
504 'pass-meta-number': function(v) {
505 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800506 // Let Meta-1..9 pass to the browser (to control tab switching) on
507 // OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500508 v = (hterm.os == 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800509 }
510
511 terminal.passMetaNumber = v;
512 },
513
Marius Schilder77857b32014-05-14 16:21:26 -0700514 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700515 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700516 },
517
Robert Ginda8cb7d902013-06-20 14:37:18 -0700518 'receive-encoding': function(v) {
519 if (!(/^(utf-8|raw)$/).test(v)) {
520 console.warn('Invalid value for "receive-encoding": ' + v);
521 v = 'utf-8';
522 }
523
524 terminal.vt.characterEncoding = v;
525 },
526
Robert Ginda57f03b42012-09-13 11:02:48 -0700527 'scroll-on-keystroke': function(v) {
528 terminal.scrollOnKeystroke_ = v;
529 },
rginda9f5222b2012-03-05 11:53:28 -0800530
Robert Ginda57f03b42012-09-13 11:02:48 -0700531 'scroll-on-output': function(v) {
532 terminal.scrollOnOutput_ = v;
533 },
rginda30f20f62012-04-05 16:36:19 -0700534
Robert Ginda57f03b42012-09-13 11:02:48 -0700535 'scrollbar-visible': function(v) {
536 terminal.setScrollbarVisible(v);
537 },
rginda9f5222b2012-03-05 11:53:28 -0800538
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400539 'scroll-wheel-may-send-arrow-keys': function(v) {
540 terminal.scrollWheelArrowKeys_ = v;
541 },
542
Rob Spies49039e52014-12-17 13:40:04 -0800543 'scroll-wheel-move-multiplier': function(v) {
544 terminal.setScrollWheelMoveMultipler(v);
545 },
546
Robert Ginda57f03b42012-09-13 11:02:48 -0700547 'shift-insert-paste': function(v) {
548 terminal.keyboard.shiftInsertPaste = v;
549 },
rginda9f5222b2012-03-05 11:53:28 -0800550
Mike Frysingera7768922017-07-28 15:00:12 -0400551 'terminal-encoding': function(v) {
Mike Frysingera1371e12017-08-17 01:37:17 -0400552 terminal.vt.setEncoding(v);
Mike Frysingera7768922017-07-28 15:00:12 -0400553 },
554
Robert Gindae76aa9f2014-03-14 12:29:12 -0700555 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400556 terminal.scrollPort_.setUserCssUrl(v);
557 },
558
559 'user-css-text': function(v) {
560 terminal.scrollPort_.setUserCssText(v);
561 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400562
563 'word-break-match-left': function(v) {
564 terminal.primaryScreen_.wordBreakMatchLeft = v;
565 terminal.alternateScreen_.wordBreakMatchLeft = v;
566 },
567
568 'word-break-match-right': function(v) {
569 terminal.primaryScreen_.wordBreakMatchRight = v;
570 terminal.alternateScreen_.wordBreakMatchRight = v;
571 },
572
573 'word-break-match-middle': function(v) {
574 terminal.primaryScreen_.wordBreakMatchMiddle = v;
575 terminal.alternateScreen_.wordBreakMatchMiddle = v;
576 },
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400577
578 'allow-images-inline': function(v) {
579 terminal.allowImagesInline = v;
580 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700581 });
rginda30f20f62012-04-05 16:36:19 -0700582
Robert Ginda57f03b42012-09-13 11:02:48 -0700583 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800584 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700585
586 if (opt_callback)
587 opt_callback();
588 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800589};
590
Rob Spies56953412014-04-28 14:09:47 -0700591
592/**
593 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500594 *
595 * @return {hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700596 */
597hterm.Terminal.prototype.getPrefs = function() {
598 return this.prefs_;
599};
600
Robert Gindaa063b202014-07-21 11:08:25 -0700601/**
602 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500603 *
604 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700605 */
606hterm.Terminal.prototype.setBracketedPaste = function(state) {
607 this.options_.bracketedPaste = state;
608};
Rob Spies56953412014-04-28 14:09:47 -0700609
rginda8e92a692012-05-20 19:37:20 -0700610/**
611 * Set the color for the cursor.
612 *
613 * If you want this setting to persist, set it through prefs_, rather than
614 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500615 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500616 * @param {string=} color The color to set. If not defined, we reset to the
617 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700618 */
619hterm.Terminal.prototype.setCursorColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500620 if (color === undefined)
621 color = this.prefs_.get('cursor-color');
622
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400623 this.setCssVar('cursor-color', color);
rginda8e92a692012-05-20 19:37:20 -0700624};
625
626/**
627 * Return the current cursor color as a string.
Evan Jones2600d4f2016-12-06 09:29:36 -0500628 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700629 */
630hterm.Terminal.prototype.getCursorColor = function() {
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400631 return this.getCssVar('cursor-color');
rginda8e92a692012-05-20 19:37:20 -0700632};
633
634/**
rgindad5613292012-06-19 15:40:37 -0700635 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500636 *
637 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700638 */
639hterm.Terminal.prototype.setSelectionEnabled = function(state) {
640 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700641};
642
643/**
rginda8e92a692012-05-20 19:37:20 -0700644 * Set the background color.
645 *
646 * If you want this setting to persist, set it through prefs_, rather than
647 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500648 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500649 * @param {string=} color The color to set. If not defined, we reset to the
650 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700651 */
652hterm.Terminal.prototype.setBackgroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500653 if (color === undefined)
654 color = this.prefs_.get('background-color');
655
rgindacbbd7482012-06-13 15:06:16 -0700656 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700657 this.primaryScreen_.textAttributes.setDefaults(
658 this.foregroundColor_, this.backgroundColor_);
659 this.alternateScreen_.textAttributes.setDefaults(
660 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700661 this.scrollPort_.setBackgroundColor(color);
662};
663
rginda9f5222b2012-03-05 11:53:28 -0800664/**
665 * Return the current terminal background color.
666 *
667 * Intended for use by other classes, so we don't have to expose the entire
668 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500669 *
670 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800671 */
672hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700673 return this.backgroundColor_;
674};
675
676/**
677 * Set the foreground color.
678 *
679 * If you want this setting to persist, set it through prefs_, rather than
680 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500681 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500682 * @param {string=} color The color to set. If not defined, we reset to the
683 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700684 */
685hterm.Terminal.prototype.setForegroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500686 if (color === undefined)
687 color = this.prefs_.get('foreground-color');
688
rgindacbbd7482012-06-13 15:06:16 -0700689 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700690 this.primaryScreen_.textAttributes.setDefaults(
691 this.foregroundColor_, this.backgroundColor_);
692 this.alternateScreen_.textAttributes.setDefaults(
693 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700694 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800695};
696
697/**
698 * Return the current terminal foreground color.
699 *
700 * Intended for use by other classes, so we don't have to expose the entire
701 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500702 *
703 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800704 */
705hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700706 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800707};
708
709/**
rginda87b86462011-12-14 13:48:03 -0800710 * Create a new instance of a terminal command and run it with a given
711 * argument string.
712 *
713 * @param {function} commandClass The constructor for a terminal command.
714 * @param {string} argString The argument string to pass to the command.
715 */
716hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700717 var environment = this.prefs_.get('environment');
718 if (typeof environment != 'object' || environment == null)
719 environment = {};
720
rginda87b86462011-12-14 13:48:03 -0800721 var self = this;
722 this.command = new commandClass(
723 { argString: argString || '',
724 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700725 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800726 onExit: function(code) {
727 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800728 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700729 if (self.prefs_.get('close-on-exit'))
730 window.close();
rginda87b86462011-12-14 13:48:03 -0800731 }
732 });
733
rgindafeaf3142012-01-31 15:14:20 -0800734 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800735 this.command.run();
736};
737
738/**
rgindafeaf3142012-01-31 15:14:20 -0800739 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500740 *
741 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800742 */
743hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700744 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800745};
746
747/**
748 * Install the keyboard handler for this terminal.
749 *
750 * This will prevent the browser from seeing any keystrokes sent to the
751 * terminal.
752 */
753hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700754 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400755};
rgindafeaf3142012-01-31 15:14:20 -0800756
757/**
758 * Uninstall the keyboard handler for this terminal.
759 */
760hterm.Terminal.prototype.uninstallKeyboard = function() {
761 this.keyboard.installKeyboard(null);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400762};
rgindafeaf3142012-01-31 15:14:20 -0800763
764/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400765 * Set a CSS variable.
766 *
767 * Normally this is used to set variables in the hterm namespace.
768 *
769 * @param {string} name The variable to set.
770 * @param {string} value The value to assign to the variable.
771 * @param {string?} opt_prefix The variable namespace/prefix to use.
772 */
773hterm.Terminal.prototype.setCssVar = function(name, value,
774 opt_prefix='--hterm-') {
775 this.document_.documentElement.style.setProperty(
776 `${opt_prefix}${name}`, value);
777};
778
779/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500780 * Get a CSS variable.
781 *
782 * Normally this is used to get variables in the hterm namespace.
783 *
784 * @param {string} name The variable to read.
785 * @param {string?} opt_prefix The variable namespace/prefix to use.
786 * @return {string} The current setting for this variable.
787 */
788hterm.Terminal.prototype.getCssVar = function(name, opt_prefix='--hterm-') {
789 return this.document_.documentElement.style.getPropertyValue(
790 `${opt_prefix}${name}`);
791};
792
793/**
rginda35c456b2012-02-09 17:29:05 -0800794 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800795 *
796 * Call setFontSize(0) to reset to the default font size.
797 *
798 * This function does not modify the font-size preference.
799 *
800 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800801 */
802hterm.Terminal.prototype.setFontSize = function(px) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500803 if (px <= 0)
rginda9f5222b2012-03-05 11:53:28 -0800804 px = this.prefs_.get('font-size');
805
rginda35c456b2012-02-09 17:29:05 -0800806 this.scrollPort_.setFontSize(px);
Mike Frysingercce97c42017-08-05 01:11:22 -0400807 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
808 this.setCssVar('charsize-height',
809 this.scrollPort_.characterSize.height + 'px');
rginda35c456b2012-02-09 17:29:05 -0800810};
811
812/**
813 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500814 *
815 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800816 */
817hterm.Terminal.prototype.getFontSize = function() {
818 return this.scrollPort_.getFontSize();
819};
820
821/**
rginda8e92a692012-05-20 19:37:20 -0700822 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500823 *
824 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700825 */
826hterm.Terminal.prototype.getFontFamily = function() {
827 return this.scrollPort_.getFontFamily();
828};
829
830/**
rginda35c456b2012-02-09 17:29:05 -0800831 * Set the CSS "font-family" for this terminal.
832 */
rginda9f5222b2012-03-05 11:53:28 -0800833hterm.Terminal.prototype.syncFontFamily = function() {
834 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
835 this.prefs_.get('font-smoothing'));
836 this.syncBoldSafeState();
837};
838
rginda4bba5e12012-06-20 16:15:30 -0700839/**
840 * Set this.mousePasteButton based on the mouse-paste-button pref,
841 * autodetecting if necessary.
842 */
843hterm.Terminal.prototype.syncMousePasteButton = function() {
844 var button = this.prefs_.get('mouse-paste-button');
845 if (typeof button == 'number') {
846 this.mousePasteButton = button;
847 return;
848 }
849
Mike Frysingeree81a002017-12-12 16:14:53 -0500850 if (hterm.os != 'linux') {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400851 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700852 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400853 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700854 }
855};
856
857/**
858 * Enable or disable bold based on the enable-bold pref, autodetecting if
859 * necessary.
860 */
rginda9f5222b2012-03-05 11:53:28 -0800861hterm.Terminal.prototype.syncBoldSafeState = function() {
862 var enableBold = this.prefs_.get('enable-bold');
863 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700864 this.primaryScreen_.textAttributes.enableBold = enableBold;
865 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800866 return;
867 }
868
rgindaf7521392012-02-28 17:20:34 -0800869 var normalSize = this.scrollPort_.measureCharacterSize();
870 var boldSize = this.scrollPort_.measureCharacterSize('bold');
871
872 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800873 if (!isBoldSafe) {
874 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700875 'from normal. Font family is: ' +
876 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800877 }
rginda9f5222b2012-03-05 11:53:28 -0800878
Robert Gindaed016262012-10-26 16:27:09 -0700879 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
880 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800881};
882
883/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500884 * Control text blinking behavior.
885 *
886 * @param {boolean=} state Whether to enable support for blinking text.
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400887 */
Mike Frysinger261597c2017-12-28 01:14:21 -0500888hterm.Terminal.prototype.setTextBlink = function(state) {
889 if (state === undefined)
890 state = this.prefs_.get('enable-blink');
891 this.setCssVar('blink-node-duration', state ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400892};
893
894/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400895 * Set the mouse cursor style based on the current terminal mode.
896 */
897hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -0400898 this.setCssVar('mouse-cursor-style',
899 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
900 'var(--hterm-mouse-cursor-text)' :
Mike Frysinger67f58f82018-11-22 13:38:22 -0500901 'var(--hterm-mouse-cursor-default)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400902};
903
904/**
rginda87b86462011-12-14 13:48:03 -0800905 * Return a copy of the current cursor position.
906 *
907 * @return {hterm.RowCol} The RowCol object representing the current position.
908 */
909hterm.Terminal.prototype.saveCursor = function() {
910 return this.screen_.cursorPosition.clone();
911};
912
Evan Jones2600d4f2016-12-06 09:29:36 -0500913/**
914 * Return the current text attributes.
915 *
916 * @return {string}
917 */
rgindaa19afe22012-01-25 15:40:22 -0800918hterm.Terminal.prototype.getTextAttributes = function() {
919 return this.screen_.textAttributes;
920};
921
Evan Jones2600d4f2016-12-06 09:29:36 -0500922/**
923 * Set the text attributes.
924 *
925 * @param {string} textAttributes The attributes to set.
926 */
rginda1a09aa02012-06-18 21:11:25 -0700927hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
928 this.screen_.textAttributes = textAttributes;
929};
930
rginda87b86462011-12-14 13:48:03 -0800931/**
rgindaf522ce02012-04-17 17:49:17 -0700932 * Return the current browser zoom factor applied to the terminal.
933 *
934 * @return {number} The current browser zoom factor.
935 */
936hterm.Terminal.prototype.getZoomFactor = function() {
937 return this.scrollPort_.characterSize.zoomFactor;
938};
939
940/**
rginda9846e2f2012-01-27 13:53:33 -0800941 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -0500942 *
943 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -0800944 */
945hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800946 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800947};
948
949/**
rginda87b86462011-12-14 13:48:03 -0800950 * Restore a previously saved cursor position.
951 *
952 * @param {hterm.RowCol} cursor The position to restore.
953 */
954hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700955 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
956 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800957 this.screen_.setCursorPosition(row, column);
958 if (cursor.column > column ||
959 cursor.column == column && cursor.overflow) {
960 this.screen_.cursorPosition.overflow = true;
961 }
rginda87b86462011-12-14 13:48:03 -0800962};
963
964/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400965 * Clear the cursor's overflow flag.
966 */
967hterm.Terminal.prototype.clearCursorOverflow = function() {
968 this.screen_.cursorPosition.overflow = false;
969};
970
971/**
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800972 * Save the current cursor state to the corresponding screens.
973 *
974 * See the hterm.Screen.CursorState class for more details.
975 *
976 * @param {boolean=} both If true, update both screens, else only update the
977 * current screen.
978 */
979hterm.Terminal.prototype.saveCursorAndState = function(both) {
980 if (both) {
981 this.primaryScreen_.saveCursorAndState(this.vt);
982 this.alternateScreen_.saveCursorAndState(this.vt);
983 } else
984 this.screen_.saveCursorAndState(this.vt);
985};
986
987/**
988 * Restore the saved cursor state in the corresponding screens.
989 *
990 * See the hterm.Screen.CursorState class for more details.
991 *
992 * @param {boolean=} both If true, update both screens, else only update the
993 * current screen.
994 */
995hterm.Terminal.prototype.restoreCursorAndState = function(both) {
996 if (both) {
997 this.primaryScreen_.restoreCursorAndState(this.vt);
998 this.alternateScreen_.restoreCursorAndState(this.vt);
999 } else
1000 this.screen_.restoreCursorAndState(this.vt);
1001};
1002
1003/**
Robert Ginda830583c2013-08-07 13:20:46 -07001004 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001005 *
1006 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -07001007 */
1008hterm.Terminal.prototype.setCursorShape = function(shape) {
1009 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001010 this.restyleCursor_();
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001011};
Robert Ginda830583c2013-08-07 13:20:46 -07001012
1013/**
1014 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001015 *
1016 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -07001017 */
1018hterm.Terminal.prototype.getCursorShape = function() {
1019 return this.cursorShape_;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001020};
Robert Ginda830583c2013-08-07 13:20:46 -07001021
1022/**
rginda87b86462011-12-14 13:48:03 -08001023 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001024 *
1025 * @param {number} columnCount
rginda87b86462011-12-14 13:48:03 -08001026 */
1027hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -08001028 if (columnCount == null) {
1029 this.div_.style.width = '100%';
1030 return;
1031 }
1032
Robert Ginda26806d12014-07-24 13:44:07 -07001033 this.div_.style.width = Math.ceil(
1034 this.scrollPort_.characterSize.width *
1035 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001036 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -08001037 this.scheduleSyncCursorPosition_();
1038};
rginda87b86462011-12-14 13:48:03 -08001039
rgindac9bc5502012-01-18 11:48:44 -08001040/**
rginda35c456b2012-02-09 17:29:05 -08001041 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001042 *
1043 * @param {number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -08001044 */
1045hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -08001046 if (rowCount == null) {
1047 this.div_.style.height = '100%';
1048 return;
1049 }
1050
rginda35c456b2012-02-09 17:29:05 -08001051 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -07001052 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -08001053 this.realizeSize_(this.screenSize.width, rowCount);
1054 this.scheduleSyncCursorPosition_();
1055};
1056
1057/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001058 * Deal with terminal size changes.
1059 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001060 * @param {number} columnCount The number of columns.
1061 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001062 */
1063hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
1064 if (columnCount != this.screenSize.width)
1065 this.realizeWidth_(columnCount);
1066
1067 if (rowCount != this.screenSize.height)
1068 this.realizeHeight_(rowCount);
1069
1070 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -07001071 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001072};
1073
1074/**
rgindac9bc5502012-01-18 11:48:44 -08001075 * Deal with terminal width changes.
1076 *
1077 * This function does what needs to be done when the terminal width changes
1078 * out from under us. It happens here rather than in onResize_() because this
1079 * code may need to run synchronously to handle programmatic changes of
1080 * terminal width.
1081 *
1082 * Relying on the browser to send us an async resize event means we may not be
1083 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001084 *
1085 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001086 */
1087hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001088 if (columnCount <= 0)
1089 throw new Error('Attempt to realize bad width: ' + columnCount);
1090
rgindac9bc5502012-01-18 11:48:44 -08001091 var deltaColumns = columnCount - this.screen_.getWidth();
1092
rginda87b86462011-12-14 13:48:03 -08001093 this.screenSize.width = columnCount;
1094 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001095
1096 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -04001097 if (this.defaultTabStops)
1098 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -08001099 } else {
1100 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -04001101 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -08001102 break;
1103
1104 this.tabStops_.pop();
1105 }
1106 }
1107
1108 this.screen_.setColumnCount(this.screenSize.width);
1109};
1110
1111/**
1112 * Deal with terminal height changes.
1113 *
1114 * This function does what needs to be done when the terminal height changes
1115 * out from under us. It happens here rather than in onResize_() because this
1116 * code may need to run synchronously to handle programmatic changes of
1117 * terminal height.
1118 *
1119 * Relying on the browser to send us an async resize event means we may not be
1120 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001121 *
1122 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001123 */
1124hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001125 if (rowCount <= 0)
1126 throw new Error('Attempt to realize bad height: ' + rowCount);
1127
rgindac9bc5502012-01-18 11:48:44 -08001128 var deltaRows = rowCount - this.screen_.getHeight();
1129
1130 this.screenSize.height = rowCount;
1131
1132 var cursor = this.saveCursor();
1133
1134 if (deltaRows < 0) {
1135 // Screen got smaller.
1136 deltaRows *= -1;
1137 while (deltaRows) {
1138 var lastRow = this.getRowCount() - 1;
1139 if (lastRow - this.scrollbackRows_.length == cursor.row)
1140 break;
1141
1142 if (this.getRowText(lastRow))
1143 break;
1144
1145 this.screen_.popRow();
1146 deltaRows--;
1147 }
1148
1149 var ary = this.screen_.shiftRows(deltaRows);
1150 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1151
1152 // We just removed rows from the top of the screen, we need to update
1153 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001154 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001155 } else if (deltaRows > 0) {
1156 // Screen got larger.
1157
1158 if (deltaRows <= this.scrollbackRows_.length) {
1159 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1160 var rows = this.scrollbackRows_.splice(
1161 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1162 this.screen_.unshiftRows(rows);
1163 deltaRows -= scrollbackCount;
1164 cursor.row += scrollbackCount;
1165 }
1166
1167 if (deltaRows)
1168 this.appendRows_(deltaRows);
1169 }
1170
rginda35c456b2012-02-09 17:29:05 -08001171 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001172 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001173};
1174
1175/**
1176 * Scroll the terminal to the top of the scrollback buffer.
1177 */
1178hterm.Terminal.prototype.scrollHome = function() {
1179 this.scrollPort_.scrollRowToTop(0);
1180};
1181
1182/**
1183 * Scroll the terminal to the end.
1184 */
1185hterm.Terminal.prototype.scrollEnd = function() {
1186 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1187};
1188
1189/**
1190 * Scroll the terminal one page up (minus one line) relative to the current
1191 * position.
1192 */
1193hterm.Terminal.prototype.scrollPageUp = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001194 this.scrollPort_.scrollPageUp();
rginda87b86462011-12-14 13:48:03 -08001195};
1196
1197/**
1198 * Scroll the terminal one page down (minus one line) relative to the current
1199 * position.
1200 */
1201hterm.Terminal.prototype.scrollPageDown = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001202 this.scrollPort_.scrollPageDown();
rginda8ba33642011-12-14 12:31:31 -08001203};
1204
rgindac9bc5502012-01-18 11:48:44 -08001205/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001206 * Scroll the terminal one line up relative to the current position.
1207 */
1208hterm.Terminal.prototype.scrollLineUp = function() {
1209 var i = this.scrollPort_.getTopRowIndex();
1210 this.scrollPort_.scrollRowToTop(i - 1);
1211};
1212
1213/**
1214 * Scroll the terminal one line down relative to the current position.
1215 */
1216hterm.Terminal.prototype.scrollLineDown = function() {
1217 var i = this.scrollPort_.getTopRowIndex();
1218 this.scrollPort_.scrollRowToTop(i + 1);
1219};
1220
1221/**
Robert Ginda40932892012-12-10 17:26:40 -08001222 * Clear primary screen, secondary screen, and the scrollback buffer.
1223 */
1224hterm.Terminal.prototype.wipeContents = function() {
Mike Frysinger9c482b82018-09-07 02:49:36 -04001225 this.clearHome(this.primaryScreen_);
1226 this.clearHome(this.alternateScreen_);
1227
1228 this.clearScrollback();
1229};
1230
1231/**
1232 * Clear scrollback buffer.
1233 */
1234hterm.Terminal.prototype.clearScrollback = function() {
1235 // Move to the end of the buffer in case the screen was scrolled back.
1236 // We're going to throw it away which would leave the display invalid.
1237 this.scrollEnd();
1238
Robert Ginda40932892012-12-10 17:26:40 -08001239 this.scrollbackRows_.length = 0;
1240 this.scrollPort_.resetCache();
1241
Mike Frysinger9c482b82018-09-07 02:49:36 -04001242 [this.primaryScreen_, this.alternateScreen_].forEach((screen) => {
1243 const bottom = screen.getHeight();
1244 this.renumberRows_(0, bottom, screen);
1245 });
Robert Ginda40932892012-12-10 17:26:40 -08001246
1247 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001248 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001249};
1250
1251/**
rgindac9bc5502012-01-18 11:48:44 -08001252 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001253 *
1254 * Perform a full reset to the default values listed in
1255 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001256 */
rginda87b86462011-12-14 13:48:03 -08001257hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001258 this.vt.reset();
1259
rgindac9bc5502012-01-18 11:48:44 -08001260 this.clearAllTabStops();
1261 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001262
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001263 const resetScreen = (screen) => {
1264 // We want to make sure to reset the attributes before we clear the screen.
1265 // The attributes might be used to initialize default/empty rows.
1266 screen.textAttributes.reset();
1267 screen.textAttributes.resetColorPalette();
1268 this.clearHome(screen);
1269 screen.saveCursorAndState(this.vt);
1270 };
1271 resetScreen(this.primaryScreen_);
1272 resetScreen(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001273
Mike Frysinger84301d02017-11-29 13:28:46 -08001274 // Reset terminal options to their default values.
1275 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001276 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1277
Mike Frysinger84301d02017-11-29 13:28:46 -08001278 this.setVTScrollRegion(null, null);
1279
1280 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001281};
1282
rgindac9bc5502012-01-18 11:48:44 -08001283/**
1284 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001285 *
1286 * Perform a soft reset to the default values listed in
1287 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001288 */
rginda0f5c0292012-01-13 11:00:13 -08001289hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001290 this.vt.reset();
1291
rgindab8bc8932012-04-27 12:45:03 -07001292 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001293 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001294
Brad Townb62dfdc2015-03-16 19:07:15 -07001295 // We show the cursor on soft reset but do not alter the blink state.
1296 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1297
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001298 const resetScreen = (screen) => {
1299 // Xterm also resets the color palette on soft reset, even though it doesn't
1300 // seem to be documented anywhere.
1301 screen.textAttributes.reset();
1302 screen.textAttributes.resetColorPalette();
1303 screen.saveCursorAndState(this.vt);
1304 };
1305 resetScreen(this.primaryScreen_);
1306 resetScreen(this.alternateScreen_);
rgindaf522ce02012-04-17 17:49:17 -07001307
rgindab8bc8932012-04-27 12:45:03 -07001308 // The xterm man page explicitly says this will happen on soft reset.
1309 this.setVTScrollRegion(null, null);
1310
1311 // Xterm also shows the cursor on soft reset, but does not alter the blink
1312 // state.
rgindaa19afe22012-01-25 15:40:22 -08001313 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001314};
1315
rgindac9bc5502012-01-18 11:48:44 -08001316/**
1317 * Move the cursor forward to the next tab stop, or to the last column
1318 * if no more tab stops are set.
1319 */
1320hterm.Terminal.prototype.forwardTabStop = function() {
1321 var column = this.screen_.cursorPosition.column;
1322
1323 for (var i = 0; i < this.tabStops_.length; i++) {
1324 if (this.tabStops_[i] > column) {
1325 this.setCursorColumn(this.tabStops_[i]);
1326 return;
1327 }
1328 }
1329
David Benjamin66e954d2012-05-05 21:08:12 -04001330 // xterm does not clear the overflow flag on HT or CHT.
1331 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001332 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001333 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001334};
1335
rgindac9bc5502012-01-18 11:48:44 -08001336/**
1337 * Move the cursor backward to the previous tab stop, or to the first column
1338 * if no previous tab stops are set.
1339 */
1340hterm.Terminal.prototype.backwardTabStop = function() {
1341 var column = this.screen_.cursorPosition.column;
1342
1343 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1344 if (this.tabStops_[i] < column) {
1345 this.setCursorColumn(this.tabStops_[i]);
1346 return;
1347 }
1348 }
1349
1350 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001351};
1352
rgindac9bc5502012-01-18 11:48:44 -08001353/**
1354 * Set a tab stop at the given column.
1355 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001356 * @param {integer} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001357 */
1358hterm.Terminal.prototype.setTabStop = function(column) {
1359 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1360 if (this.tabStops_[i] == column)
1361 return;
1362
1363 if (this.tabStops_[i] < column) {
1364 this.tabStops_.splice(i + 1, 0, column);
1365 return;
1366 }
1367 }
1368
1369 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001370};
1371
rgindac9bc5502012-01-18 11:48:44 -08001372/**
1373 * Clear the tab stop at the current cursor position.
1374 *
1375 * No effect if there is no tab stop at the current cursor position.
1376 */
1377hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1378 var column = this.screen_.cursorPosition.column;
1379
1380 var i = this.tabStops_.indexOf(column);
1381 if (i == -1)
1382 return;
1383
1384 this.tabStops_.splice(i, 1);
1385};
1386
1387/**
1388 * Clear all tab stops.
1389 */
1390hterm.Terminal.prototype.clearAllTabStops = function() {
1391 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001392 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001393};
1394
1395/**
1396 * Set up the default tab stops, starting from a given column.
1397 *
1398 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001399 * from the specified column, or 0 if no column is provided. It also flags
1400 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001401 *
1402 * This does not clear the existing tab stops first, use clearAllTabStops
1403 * for that.
1404 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001405 * @param {integer} opt_start Optional starting zero based starting column, useful
rgindac9bc5502012-01-18 11:48:44 -08001406 * for filling out missing tab stops when the terminal is resized.
1407 */
1408hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1409 var start = opt_start || 0;
1410 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001411 // Round start up to a default tab stop.
1412 start = start - 1 - ((start - 1) % w) + w;
1413 for (var i = start; i < this.screenSize.width; i += w) {
1414 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001415 }
David Benjamin66e954d2012-05-05 21:08:12 -04001416
1417 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001418};
1419
rginda6d397402012-01-17 10:58:29 -08001420/**
rginda8ba33642011-12-14 12:31:31 -08001421 * Interpret a sequence of characters.
1422 *
1423 * Incomplete escape sequences are buffered until the next call.
1424 *
1425 * @param {string} str Sequence of characters to interpret or pass through.
1426 */
1427hterm.Terminal.prototype.interpret = function(str) {
rginda8ba33642011-12-14 12:31:31 -08001428 this.scheduleSyncCursorPosition_();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001429 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001430};
1431
1432/**
1433 * Take over the given DIV for use as the terminal display.
1434 *
1435 * @param {HTMLDivElement} div The div to use as the terminal display.
1436 */
1437hterm.Terminal.prototype.decorate = function(div) {
Mike Frysinger5768a9d2017-12-26 12:57:44 -05001438 const charset = div.ownerDocument.characterSet.toLowerCase();
1439 if (charset != 'utf-8') {
1440 console.warn(`Document encoding should be set to utf-8, not "${charset}";` +
1441 ` Add <meta charset='utf-8'/> to your HTML <head> to fix.`);
1442 }
1443
rginda87b86462011-12-14 13:48:03 -08001444 this.div_ = div;
1445
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001446 this.accessibilityReader_ = new hterm.AccessibilityReader(div);
1447
Adrián Pérez-Orozco394e64f2018-12-17 17:20:16 -08001448 this.scrollPort_.decorate(div, () => this.setupScrollPort_());
1449};
1450
1451/**
1452 * Initialisation of ScrollPort properties which need to be set after its DOM
1453 * has been initialised.
1454 * @private
1455 */
1456hterm.Terminal.prototype.setupScrollPort_ = function() {
rginda30f20f62012-04-05 16:36:19 -07001457 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001458 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1459 this.scrollPort_.setBackgroundPosition(
1460 this.prefs_.get('background-position'));
Mike Frysinger08bad432017-04-24 00:50:54 -04001461 this.scrollPort_.setUserCssUrl(this.prefs_.get('user-css'));
1462 this.scrollPort_.setUserCssText(this.prefs_.get('user-css-text'));
Raymes Khoury177aec72018-06-26 10:58:53 +10001463 this.scrollPort_.setAccessibilityReader(this.accessibilityReader_);
rginda30f20f62012-04-05 16:36:19 -07001464
rginda0918b652012-04-04 11:26:24 -07001465 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001466
rginda9f5222b2012-03-05 11:53:28 -08001467 this.setFontSize(this.prefs_.get('font-size'));
1468 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001469
David Reveman8f552492012-03-28 12:18:41 -04001470 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001471 this.setScrollWheelMoveMultipler(
1472 this.prefs_.get('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001473
rginda8ba33642011-12-14 12:31:31 -08001474 this.document_ = this.scrollPort_.getDocument();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001475 this.accessibilityReader_.decorate(this.document_);
rginda8ba33642011-12-14 12:31:31 -08001476
Evan Jones5f9df812016-12-06 09:38:58 -05001477 this.document_.body.oncontextmenu = function() { return false; };
Mike Frysingercc114512017-09-11 21:39:17 -04001478 this.contextMenu.setDocument(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07001479
1480 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001481 var screenNode = this.scrollPort_.getScreenNode();
1482 screenNode.addEventListener('mousedown', onMouse);
1483 screenNode.addEventListener('mouseup', onMouse);
1484 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001485 this.scrollPort_.onScrollWheel = onMouse;
1486
Mike Frysinger02ded6d2018-06-21 14:25:20 -04001487 screenNode.addEventListener('keydown', this.onKeyboardActivity_.bind(this));
1488
Toni Barzic0bfa8922013-11-22 11:18:35 -08001489 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001490 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001491 // Listen for mousedown events on the screenNode as in FF the focus
1492 // events don't bubble.
1493 screenNode.addEventListener('mousedown', function() {
1494 setTimeout(this.onFocusChange_.bind(this, true));
1495 }.bind(this));
1496
Toni Barzic0bfa8922013-11-22 11:18:35 -08001497 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001498 'blur', this.onFocusChange_.bind(this, false));
1499
1500 var style = this.document_.createElement('style');
1501 style.textContent =
1502 ('.cursor-node[focus="false"] {' +
1503 ' box-sizing: border-box;' +
1504 ' background-color: transparent !important;' +
1505 ' border-width: 2px;' +
1506 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001507 '}' +
Mike Frysingercc114512017-09-11 21:39:17 -04001508 'menu {' +
1509 ' margin: 0;' +
1510 ' padding: 0;' +
1511 ' cursor: var(--hterm-mouse-cursor-pointer);' +
1512 '}' +
1513 'menuitem {' +
1514 ' white-space: nowrap;' +
1515 ' border-bottom: 1px dashed;' +
1516 ' display: block;' +
1517 ' padding: 0.3em 0.3em 0 0.3em;' +
1518 '}' +
1519 'menuitem.separator {' +
1520 ' border-bottom: none;' +
1521 ' height: 0.5em;' +
1522 ' padding: 0;' +
1523 '}' +
1524 'menuitem:hover {' +
1525 ' color: var(--hterm-cursor-color);' +
1526 '}' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001527 '.wc-node {' +
1528 ' display: inline-block;' +
1529 ' text-align: center;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001530 ' width: calc(var(--hterm-charsize-width) * 2);' +
Mike Frysinger4036f6e2017-05-31 14:02:55 -04001531 ' line-height: var(--hterm-charsize-height);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001532 '}' +
1533 ':root {' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001534 ' --hterm-charsize-width: ' + this.scrollPort_.characterSize.width + 'px;' +
1535 ' --hterm-charsize-height: ' + this.scrollPort_.characterSize.height + 'px;' +
Mike Frysingera27c0502017-08-23 22:37:10 -04001536 // Default position hides the cursor for when the window is initializing.
1537 ' --hterm-cursor-offset-col: -1;' +
1538 ' --hterm-cursor-offset-row: -1;' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001539 ' --hterm-blink-node-duration: 0.7s;' +
Mike Frysinger67f58f82018-11-22 13:38:22 -05001540 ' --hterm-mouse-cursor-default: default;' +
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001541 ' --hterm-mouse-cursor-text: text;' +
Mike Frysinger67f58f82018-11-22 13:38:22 -05001542 ' --hterm-mouse-cursor-pointer: pointer;' +
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001543 ' --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001544 '}' +
Mike Frysingerd3907202017-10-23 01:23:50 -04001545 '.uri-node:hover {' +
1546 ' text-decoration: underline;' +
Mike Frysinger67f58f82018-11-22 13:38:22 -05001547 ' cursor: var(--hterm-mouse-cursor-pointer);' +
Mike Frysingerd3907202017-10-23 01:23:50 -04001548 '}' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001549 '@keyframes blink {' +
1550 ' from { opacity: 1.0; }' +
1551 ' to { opacity: 0.0; }' +
1552 '}' +
1553 '.blink-node {' +
1554 ' animation-name: blink;' +
1555 ' animation-duration: var(--hterm-blink-node-duration);' +
1556 ' animation-iteration-count: infinite;' +
1557 ' animation-timing-function: ease-in-out;' +
1558 ' animation-direction: alternate;' +
rginda8e92a692012-05-20 19:37:20 -07001559 '}');
Mike Frysingerb74a6472018-06-22 13:37:08 -04001560 // Insert this stock style as the first node so that any user styles will
1561 // override w/out having to use !important everywhere. The rules above mix
1562 // runtime variables with default ones designed to be overridden by the user,
1563 // but we can wait for a concrete case from the users to determine the best
1564 // way to split the sheet up to before & after the user-css settings.
1565 this.document_.head.insertBefore(style, this.document_.head.firstChild);
rginda8e92a692012-05-20 19:37:20 -07001566
rginda8ba33642011-12-14 12:31:31 -08001567 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001568 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001569 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001570 this.cursorNode_.style.cssText =
1571 ('position: absolute;' +
Mike Frysinger44c32202017-08-05 01:13:09 -04001572 'left: calc(var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));' +
1573 'top: calc(var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));' +
Mike Frysingerd60f4c22018-03-15 21:25:30 -07001574 'display: ' + (this.options_.cursorVisible ? '' : 'none') + ';' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001575 'width: var(--hterm-charsize-width);' +
1576 'height: var(--hterm-charsize-height);' +
Mike Frysinger2fd079a2018-09-02 01:46:12 -04001577 'background-color: var(--hterm-cursor-color);' +
1578 'border-color: var(--hterm-cursor-color);' +
Rob Spies06533ba2014-04-24 11:20:37 -07001579 '-webkit-transition: opacity, background-color 100ms linear;' +
1580 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001581
Mike Frysingerf02a2cb2017-12-21 00:34:03 -05001582 this.setCursorColor();
Robert Gindafb1be6a2013-12-11 11:56:22 -08001583 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1584 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001585
rginda8ba33642011-12-14 12:31:31 -08001586 this.document_.body.appendChild(this.cursorNode_);
1587
rgindad5613292012-06-19 15:40:37 -07001588 // When 'enableMouseDragScroll' is off we reposition this element directly
1589 // under the mouse cursor after a click. This makes Chrome associate
1590 // subsequent mousemove events with the scroll-blocker. Since the
1591 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1592 // events do not cause the scrollport to scroll.
1593 //
1594 // It's a hack, but it's the cleanest way I could find.
1595 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001596 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
Raymes Khoury6dce2f82018-04-12 15:38:58 +10001597 this.scrollBlockerNode_.setAttribute('aria-hidden', 'true');
rgindad5613292012-06-19 15:40:37 -07001598 this.scrollBlockerNode_.style.cssText =
1599 ('position: absolute;' +
1600 'top: -99px;' +
1601 'display: block;' +
1602 'width: 10px;' +
1603 'height: 10px;');
1604 this.document_.body.appendChild(this.scrollBlockerNode_);
1605
rgindad5613292012-06-19 15:40:37 -07001606 this.scrollPort_.onScrollWheel = onMouse;
1607 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1608 ].forEach(function(event) {
1609 this.scrollBlockerNode_.addEventListener(event, onMouse);
1610 this.cursorNode_.addEventListener(event, onMouse);
1611 this.document_.addEventListener(event, onMouse);
1612 }.bind(this));
1613
1614 this.cursorNode_.addEventListener('mousedown', function() {
1615 setTimeout(this.focus.bind(this));
1616 }.bind(this));
1617
rginda8ba33642011-12-14 12:31:31 -08001618 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001619
rginda87b86462011-12-14 13:48:03 -08001620 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001621 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001622};
1623
rginda0918b652012-04-04 11:26:24 -07001624/**
1625 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001626 *
1627 * @return {HTMLDocument}
rginda0918b652012-04-04 11:26:24 -07001628 */
rginda87b86462011-12-14 13:48:03 -08001629hterm.Terminal.prototype.getDocument = function() {
1630 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001631};
1632
1633/**
rginda0918b652012-04-04 11:26:24 -07001634 * Focus the terminal.
1635 */
1636hterm.Terminal.prototype.focus = function() {
1637 this.scrollPort_.focus();
1638};
1639
1640/**
rginda8ba33642011-12-14 12:31:31 -08001641 * Return the HTML Element for a given row index.
1642 *
1643 * This is a method from the RowProvider interface. The ScrollPort uses
1644 * it to fetch rows on demand as they are scrolled into view.
1645 *
1646 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1647 * pairs to conserve memory.
1648 *
1649 * @param {integer} index The zero-based row index, measured relative to the
1650 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001651 * largest indices.
rginda8ba33642011-12-14 12:31:31 -08001652 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1653 */
1654hterm.Terminal.prototype.getRowNode = function(index) {
1655 if (index < this.scrollbackRows_.length)
1656 return this.scrollbackRows_[index];
1657
1658 var screenIndex = index - this.scrollbackRows_.length;
1659 return this.screen_.rowsArray[screenIndex];
1660};
1661
1662/**
1663 * Return the text content for a given range of rows.
1664 *
1665 * This is a method from the RowProvider interface. The ScrollPort uses
1666 * it to fetch text content on demand when the user attempts to copy their
1667 * selection to the clipboard.
1668 *
1669 * @param {integer} start The zero-based row index to start from, measured
1670 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001671 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001672 * @param {integer} end The zero-based row index to end on, measured
1673 * relative to the start of the scrollback buffer.
1674 * @return {string} A single string containing the text value of the range of
1675 * rows. Lines will be newline delimited, with no trailing newline.
1676 */
1677hterm.Terminal.prototype.getRowsText = function(start, end) {
1678 var ary = [];
1679 for (var i = start; i < end; i++) {
1680 var node = this.getRowNode(i);
1681 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001682 if (i < end - 1 && !node.getAttribute('line-overflow'))
1683 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001684 }
1685
rgindaa09e7332012-08-17 12:49:51 -07001686 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001687};
1688
1689/**
1690 * Return the text content for a given row.
1691 *
1692 * This is a method from the RowProvider interface. The ScrollPort uses
1693 * it to fetch text content on demand when the user attempts to copy their
1694 * selection to the clipboard.
1695 *
1696 * @param {integer} index The zero-based row index to return, measured
1697 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001698 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001699 * @return {string} A string containing the text value of the selected row.
1700 */
1701hterm.Terminal.prototype.getRowText = function(index) {
1702 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001703 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001704};
1705
1706/**
1707 * Return the total number of rows in the addressable screen and in the
1708 * scrollback buffer of this terminal.
1709 *
1710 * This is a method from the RowProvider interface. The ScrollPort uses
1711 * it to compute the size of the scrollbar.
1712 *
1713 * @return {integer} The number of rows in this terminal.
1714 */
1715hterm.Terminal.prototype.getRowCount = function() {
1716 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1717};
1718
1719/**
1720 * Create DOM nodes for new rows and append them to the end of the terminal.
1721 *
1722 * This is the only correct way to add a new DOM node for a row. Notice that
1723 * the new row is appended to the bottom of the list of rows, and does not
1724 * require renumbering (of the rowIndex property) of previous rows.
1725 *
1726 * If you think you want a new blank row somewhere in the middle of the
1727 * terminal, look into moveRows_().
1728 *
1729 * This method does not pay attention to vtScrollTop/Bottom, since you should
1730 * be using moveRows() in cases where they would matter.
1731 *
1732 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001733 *
1734 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001735 */
1736hterm.Terminal.prototype.appendRows_ = function(count) {
1737 var cursorRow = this.screen_.rowsArray.length;
1738 var offset = this.scrollbackRows_.length + cursorRow;
1739 for (var i = 0; i < count; i++) {
1740 var row = this.document_.createElement('x-row');
1741 row.appendChild(this.document_.createTextNode(''));
1742 row.rowIndex = offset + i;
1743 this.screen_.pushRow(row);
1744 }
1745
1746 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1747 if (extraRows > 0) {
1748 var ary = this.screen_.shiftRows(extraRows);
1749 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001750 if (this.scrollPort_.isScrolledEnd)
1751 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001752 }
1753
1754 if (cursorRow >= this.screen_.rowsArray.length)
1755 cursorRow = this.screen_.rowsArray.length - 1;
1756
rginda87b86462011-12-14 13:48:03 -08001757 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001758};
1759
1760/**
1761 * Relocate rows from one part of the addressable screen to another.
1762 *
1763 * This is used to recycle rows during VT scrolls (those which are driven
1764 * by VT commands, rather than by the user manipulating the scrollbar.)
1765 *
1766 * In this case, the blank lines scrolled into the scroll region are made of
1767 * the nodes we scrolled off. These have their rowIndex properties carefully
1768 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001769 *
1770 * @param {number} fromIndex The start index.
1771 * @param {number} count The number of rows to move.
1772 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001773 */
1774hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1775 var ary = this.screen_.removeRows(fromIndex, count);
1776 this.screen_.insertRows(toIndex, ary);
1777
1778 var start, end;
1779 if (fromIndex < toIndex) {
1780 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001781 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001782 } else {
1783 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001784 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001785 }
1786
1787 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001788 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001789};
1790
1791/**
1792 * Renumber the rowIndex property of the given range of rows.
1793 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001794 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08001795 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001796 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001797 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05001798 *
1799 * @param {number} start The start index.
1800 * @param {number} end The end index.
1801 * @param {hterm.Screen} opt_screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08001802 */
Robert Ginda40932892012-12-10 17:26:40 -08001803hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1804 var screen = opt_screen || this.screen_;
1805
rginda8ba33642011-12-14 12:31:31 -08001806 var offset = this.scrollbackRows_.length;
1807 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001808 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001809 }
1810};
1811
1812/**
1813 * Print a string to the terminal.
1814 *
1815 * This respects the current insert and wraparound modes. It will add new lines
1816 * to the end of the terminal, scrolling off the top into the scrollback buffer
1817 * if necessary.
1818 *
1819 * The string is *not* parsed for escape codes. Use the interpret() method if
1820 * that's what you're after.
1821 *
1822 * @param{string} str The string to print.
1823 */
1824hterm.Terminal.prototype.print = function(str) {
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001825 this.scheduleSyncCursorPosition_();
1826
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001827 // Basic accessibility output for the screen reader.
Raymes Khoury177aec72018-06-26 10:58:53 +10001828 this.accessibilityReader_.announce(str);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001829
rgindaa9abdd82012-08-06 18:05:09 -07001830 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001831
Ricky Liang48f05cb2013-12-31 23:35:29 +08001832 var strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04001833 // Fun edge case: If the string only contains zero width codepoints (like
1834 // combining characters), we make sure to iterate at least once below.
1835 if (strWidth == 0 && str)
1836 strWidth = 1;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001837
1838 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001839 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1840 this.screen_.commitLineOverflow();
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001841 this.newLine(true);
rgindaa09e7332012-08-17 12:49:51 -07001842 }
rgindaa19afe22012-01-25 15:40:22 -08001843
Ricky Liang48f05cb2013-12-31 23:35:29 +08001844 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001845 var didOverflow = false;
1846 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001847
rgindaa9abdd82012-08-06 18:05:09 -07001848 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1849 didOverflow = true;
1850 count = this.screenSize.width - this.screen_.cursorPosition.column;
1851 }
rgindaa19afe22012-01-25 15:40:22 -08001852
rgindaa9abdd82012-08-06 18:05:09 -07001853 if (didOverflow && !this.options_.wraparound) {
1854 // If the string overflowed the line but wraparound is off, then the
1855 // last printed character should be the last of the string.
1856 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001857 substr = lib.wc.substr(str, startOffset, count - 1) +
1858 lib.wc.substr(str, strWidth - 1);
1859 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001860 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001861 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001862 }
rgindaa19afe22012-01-25 15:40:22 -08001863
Ricky Liang48f05cb2013-12-31 23:35:29 +08001864 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1865 for (var i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001866 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
1867 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001868
1869 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001870 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001871 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001872 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001873 }
1874 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001875 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07001876 }
1877
1878 this.screen_.maybeClipCurrentRow();
1879 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001880 }
rginda8ba33642011-12-14 12:31:31 -08001881
rginda9f5222b2012-03-05 11:53:28 -08001882 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001883 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001884};
1885
1886/**
rginda87b86462011-12-14 13:48:03 -08001887 * Set the VT scroll region.
1888 *
rginda87b86462011-12-14 13:48:03 -08001889 * This also resets the cursor position to the absolute (0, 0) position, since
1890 * that's what xterm appears to do.
1891 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001892 * Setting the scroll region to the full height of the terminal will clear
1893 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1894 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1895 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1896 * continue to work as most users would expect.
1897 *
rginda87b86462011-12-14 13:48:03 -08001898 * @param {integer} scrollTop The zero-based top of the scroll region.
1899 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1900 * inclusive.
1901 */
1902hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001903 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001904 this.vtScrollTop_ = null;
1905 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001906 } else {
1907 this.vtScrollTop_ = scrollTop;
1908 this.vtScrollBottom_ = scrollBottom;
1909 }
rginda87b86462011-12-14 13:48:03 -08001910};
1911
1912/**
rginda8ba33642011-12-14 12:31:31 -08001913 * Return the top row index according to the VT.
1914 *
1915 * This will return 0 unless the terminal has been told to restrict scrolling
1916 * to some lower row. It is used for some VT cursor positioning and scrolling
1917 * commands.
1918 *
1919 * @return {integer} The topmost row in the terminal's scroll region.
1920 */
1921hterm.Terminal.prototype.getVTScrollTop = function() {
1922 if (this.vtScrollTop_ != null)
1923 return this.vtScrollTop_;
1924
1925 return 0;
rginda87b86462011-12-14 13:48:03 -08001926};
rginda8ba33642011-12-14 12:31:31 -08001927
1928/**
1929 * Return the bottom row index according to the VT.
1930 *
1931 * This will return the height of the terminal unless the it has been told to
1932 * restrict scrolling to some higher row. It is used for some VT cursor
1933 * positioning and scrolling commands.
1934 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001935 * @return {integer} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001936 */
1937hterm.Terminal.prototype.getVTScrollBottom = function() {
1938 if (this.vtScrollBottom_ != null)
1939 return this.vtScrollBottom_;
1940
rginda87b86462011-12-14 13:48:03 -08001941 return this.screenSize.height - 1;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001942};
rginda8ba33642011-12-14 12:31:31 -08001943
1944/**
1945 * Process a '\n' character.
1946 *
1947 * If the cursor is on the final row of the terminal this will append a new
1948 * blank row to the screen and scroll the topmost row into the scrollback
1949 * buffer.
1950 *
1951 * Otherwise, this moves the cursor to column zero of the next row.
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001952 *
1953 * @param {boolean=} dueToOverflow Whether the newline is due to wraparound of
1954 * the terminal.
rginda8ba33642011-12-14 12:31:31 -08001955 */
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001956hterm.Terminal.prototype.newLine = function(dueToOverflow = false) {
1957 if (!dueToOverflow)
1958 this.accessibilityReader_.newLine();
1959
Robert Ginda9937abc2013-07-25 16:09:23 -07001960 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1961 this.screen_.rowsArray.length - 1);
1962
1963 if (this.vtScrollBottom_ != null) {
1964 // A VT Scroll region is active, we never append new rows.
1965 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1966 // We're at the end of the VT Scroll Region, perform a VT scroll.
1967 this.vtScrollUp(1);
1968 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1969 } else if (cursorAtEndOfScreen) {
1970 // We're at the end of the screen, the only thing to do is put the
1971 // cursor to column 0.
1972 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1973 } else {
1974 // Anywhere else, advance the cursor row, and reset the column.
1975 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1976 }
1977 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001978 // We're at the end of the screen. Append a new row to the terminal,
1979 // shifting the top row into the scrollback.
1980 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001981 } else {
rginda87b86462011-12-14 13:48:03 -08001982 // Anywhere else in the screen just moves the cursor.
1983 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001984 }
1985};
1986
1987/**
1988 * Like newLine(), except maintain the cursor column.
1989 */
1990hterm.Terminal.prototype.lineFeed = function() {
1991 var column = this.screen_.cursorPosition.column;
1992 this.newLine();
1993 this.setCursorColumn(column);
1994};
1995
1996/**
rginda87b86462011-12-14 13:48:03 -08001997 * If autoCarriageReturn is set then newLine(), else lineFeed().
1998 */
1999hterm.Terminal.prototype.formFeed = function() {
2000 if (this.options_.autoCarriageReturn) {
2001 this.newLine();
2002 } else {
2003 this.lineFeed();
2004 }
2005};
2006
2007/**
2008 * Move the cursor up one row, possibly inserting a blank line.
2009 *
2010 * The cursor column is not changed.
2011 */
2012hterm.Terminal.prototype.reverseLineFeed = function() {
2013 var scrollTop = this.getVTScrollTop();
2014 var currentRow = this.screen_.cursorPosition.row;
2015
2016 if (currentRow == scrollTop) {
2017 this.insertLines(1);
2018 } else {
2019 this.setAbsoluteCursorRow(currentRow - 1);
2020 }
2021};
2022
2023/**
rginda8ba33642011-12-14 12:31:31 -08002024 * Replace all characters to the left of the current cursor with the space
2025 * character.
2026 *
2027 * TODO(rginda): This should probably *remove* the characters (not just replace
2028 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07002029 * position.
rginda8ba33642011-12-14 12:31:31 -08002030 */
2031hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08002032 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002033 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002034 const count = cursor.column + 1;
2035 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08002036 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002037};
2038
2039/**
David Benjamin684a9b72012-05-01 17:19:58 -04002040 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08002041 *
2042 * The cursor position is unchanged.
2043 *
Robert Gindaf2547f12012-10-25 20:36:21 -07002044 * If the current background color is not the default background color this
2045 * will insert spaces rather than delete. This is unfortunate because the
2046 * trailing space will affect text selection, but it's difficult to come up
2047 * with a way to style empty space that wouldn't trip up the hterm.Screen
2048 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07002049 *
2050 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
2051 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
2052 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05002053 *
2054 * @param {number} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08002055 */
2056hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07002057 if (this.screen_.cursorPosition.overflow)
2058 return;
2059
Robert Ginda7fd57082012-09-25 14:41:47 -07002060 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
2061 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07002062
2063 if (this.screen_.textAttributes.background ===
2064 this.screen_.textAttributes.DEFAULT_COLOR) {
2065 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08002066 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07002067 this.screen_.cursorPosition.column + count) {
2068 this.screen_.deleteChars(count);
2069 this.clearCursorOverflow();
2070 return;
2071 }
2072 }
2073
rginda87b86462011-12-14 13:48:03 -08002074 var cursor = this.saveCursor();
Mike Frysinger6380bed2017-08-24 18:46:39 -04002075 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08002076 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002077 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002078};
2079
2080/**
2081 * Erase the current line.
2082 *
2083 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002084 */
2085hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08002086 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002087 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08002088 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002089 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002090};
2091
2092/**
David Benjamina08d78f2012-05-05 00:28:49 -04002093 * Erase all characters from the start of the screen to the current cursor
2094 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002095 *
2096 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002097 */
2098hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08002099 var cursor = this.saveCursor();
2100
2101 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002102
David Benjamina08d78f2012-05-05 00:28:49 -04002103 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002104 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002105 this.screen_.clearCursorRow();
2106 }
2107
rginda87b86462011-12-14 13:48:03 -08002108 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002109 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002110};
2111
2112/**
2113 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002114 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002115 *
2116 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002117 */
2118hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08002119 var cursor = this.saveCursor();
2120
2121 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002122
David Benjamina08d78f2012-05-05 00:28:49 -04002123 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08002124 for (var i = cursor.row + 1; i <= bottom; i++) {
2125 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002126 this.screen_.clearCursorRow();
2127 }
2128
rginda87b86462011-12-14 13:48:03 -08002129 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002130 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002131};
2132
2133/**
2134 * Fill the terminal with a given character.
2135 *
2136 * This methods does not respect the VT scroll region.
2137 *
2138 * @param {string} ch The character to use for the fill.
2139 */
2140hterm.Terminal.prototype.fill = function(ch) {
2141 var cursor = this.saveCursor();
2142
2143 this.setAbsoluteCursorPosition(0, 0);
2144 for (var row = 0; row < this.screenSize.height; row++) {
2145 for (var col = 0; col < this.screenSize.width; col++) {
2146 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002147 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002148 }
2149 }
2150
2151 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002152};
2153
2154/**
rginda9ea433c2012-03-16 11:57:00 -07002155 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002156 *
rginda9ea433c2012-03-16 11:57:00 -07002157 * This does not respect the scroll region.
2158 *
2159 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2160 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002161 */
rginda9ea433c2012-03-16 11:57:00 -07002162hterm.Terminal.prototype.clearHome = function(opt_screen) {
2163 var screen = opt_screen || this.screen_;
2164 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002165
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002166 this.accessibilityReader_.clear();
2167
rginda11057d52012-04-25 12:29:56 -07002168 if (bottom == 0) {
2169 // Empty screen, nothing to do.
2170 return;
2171 }
2172
rgindae4d29232012-01-19 10:47:13 -08002173 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002174 screen.setCursorPosition(i, 0);
2175 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002176 }
2177
rginda9ea433c2012-03-16 11:57:00 -07002178 screen.setCursorPosition(0, 0);
2179};
2180
2181/**
2182 * Erase the entire display without changing the cursor position.
2183 *
2184 * The cursor position is unchanged. This does not respect the scroll
2185 * region.
2186 *
2187 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2188 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002189 */
2190hterm.Terminal.prototype.clear = function(opt_screen) {
2191 var screen = opt_screen || this.screen_;
2192 var cursor = screen.cursorPosition.clone();
2193 this.clearHome(screen);
2194 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002195};
2196
2197/**
2198 * VT command to insert lines at the current cursor row.
2199 *
2200 * This respects the current scroll region. Rows pushed off the bottom are
2201 * lost (they won't show up in the scrollback buffer).
2202 *
rginda8ba33642011-12-14 12:31:31 -08002203 * @param {integer} count The number of lines to insert.
2204 */
2205hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07002206 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002207
2208 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002209 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002210
Robert Ginda579186b2012-09-26 11:40:04 -07002211 // The moveCount is the number of rows we need to relocate to make room for
2212 // the new row(s). The count is the distance to move them.
2213 var moveCount = bottom - cursorRow - count + 1;
2214 if (moveCount)
2215 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08002216
Robert Ginda579186b2012-09-26 11:40:04 -07002217 for (var i = count - 1; i >= 0; i--) {
2218 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002219 this.screen_.clearCursorRow();
2220 }
rginda8ba33642011-12-14 12:31:31 -08002221};
2222
2223/**
2224 * VT command to delete lines at the current cursor row.
2225 *
2226 * New rows are added to the bottom of scroll region to take their place. New
2227 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002228 *
2229 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002230 */
2231hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08002232 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002233
rginda87b86462011-12-14 13:48:03 -08002234 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08002235 var bottom = this.getVTScrollBottom();
2236
rginda87b86462011-12-14 13:48:03 -08002237 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002238 count = Math.min(count, maxCount);
2239
rginda87b86462011-12-14 13:48:03 -08002240 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08002241 if (count != maxCount)
2242 this.moveRows_(top, count, moveStart);
2243
2244 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002245 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002246 this.screen_.clearCursorRow();
2247 }
2248
rginda87b86462011-12-14 13:48:03 -08002249 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002250 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002251};
2252
2253/**
2254 * Inserts the given number of spaces at the current cursor position.
2255 *
rginda87b86462011-12-14 13:48:03 -08002256 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002257 *
2258 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002259 */
2260hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08002261 var cursor = this.saveCursor();
2262
rgindacbbd7482012-06-13 15:06:16 -07002263 var ws = lib.f.getWhitespace(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002264 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002265 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002266
2267 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002268 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002269};
2270
2271/**
2272 * Forward-delete the specified number of characters starting at the cursor
2273 * position.
2274 *
2275 * @param {integer} count The number of characters to delete.
2276 */
2277hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002278 var deleted = this.screen_.deleteChars(count);
2279 if (deleted && !this.screen_.textAttributes.isDefault()) {
2280 var cursor = this.saveCursor();
2281 this.setCursorColumn(this.screenSize.width - deleted);
2282 this.screen_.insertString(lib.f.getWhitespace(deleted));
2283 this.restoreCursor(cursor);
2284 }
2285
David Benjamin54e8bf62012-06-01 22:31:40 -04002286 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002287};
2288
2289/**
2290 * Shift rows in the scroll region upwards by a given number of lines.
2291 *
2292 * New rows are inserted at the bottom of the scroll region to fill the
2293 * vacated rows. The new rows not filled out with the current text attributes.
2294 *
2295 * This function does not affect the scrollback rows at all. Rows shifted
2296 * off the top are lost.
2297 *
rginda87b86462011-12-14 13:48:03 -08002298 * The cursor position is not altered.
2299 *
rginda8ba33642011-12-14 12:31:31 -08002300 * @param {integer} count The number of rows to scroll.
2301 */
2302hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002303 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002304
rginda87b86462011-12-14 13:48:03 -08002305 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002306 this.deleteLines(count);
2307
rginda87b86462011-12-14 13:48:03 -08002308 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002309};
2310
2311/**
2312 * Shift rows below the cursor down by a given number of lines.
2313 *
2314 * This function respects the current scroll region.
2315 *
2316 * New rows are inserted at the top of the scroll region to fill the
2317 * vacated rows. The new rows not filled out with the current text attributes.
2318 *
2319 * This function does not affect the scrollback rows at all. Rows shifted
2320 * off the bottom are lost.
2321 *
2322 * @param {integer} count The number of rows to scroll.
2323 */
2324hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08002325 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002326
rginda87b86462011-12-14 13:48:03 -08002327 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08002328 this.insertLines(opt_count);
2329
rginda87b86462011-12-14 13:48:03 -08002330 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002331};
2332
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002333/**
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002334 * Enable accessibility-friendly features that have a performance impact.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002335 *
2336 * This will generate additional DOM nodes in an aria-live region that will
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002337 * cause Assitive Technology to announce the output of the terminal. It also
2338 * enables other features that aid assistive technology. All the features gated
2339 * behind this flag have a performance impact on the terminal which is why they
2340 * are made optional.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002341 *
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002342 * @param {boolean} enabled Whether to enable accessibility-friendly features.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002343 */
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002344hterm.Terminal.prototype.setAccessibilityEnabled = function(enabled) {
Raymes Khoury177aec72018-06-26 10:58:53 +10002345 this.accessibilityReader_.setAccessibilityEnabled(enabled);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002346};
rginda87b86462011-12-14 13:48:03 -08002347
rginda8ba33642011-12-14 12:31:31 -08002348/**
2349 * Set the cursor position.
2350 *
2351 * The cursor row is relative to the scroll region if the terminal has
2352 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2353 *
2354 * @param {integer} row The new zero-based cursor row.
2355 * @param {integer} row The new zero-based cursor column.
2356 */
2357hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2358 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002359 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002360 } else {
rginda87b86462011-12-14 13:48:03 -08002361 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002362 }
rginda87b86462011-12-14 13:48:03 -08002363};
rginda8ba33642011-12-14 12:31:31 -08002364
Evan Jones2600d4f2016-12-06 09:29:36 -05002365/**
2366 * Move the cursor relative to its current position.
2367 *
2368 * @param {number} row
2369 * @param {number} column
2370 */
rginda87b86462011-12-14 13:48:03 -08002371hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2372 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002373 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2374 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002375 this.screen_.setCursorPosition(row, column);
2376};
2377
Evan Jones2600d4f2016-12-06 09:29:36 -05002378/**
2379 * Move the cursor to the specified position.
2380 *
2381 * @param {number} row
2382 * @param {number} column
2383 */
rginda87b86462011-12-14 13:48:03 -08002384hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002385 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2386 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002387 this.screen_.setCursorPosition(row, column);
2388};
2389
2390/**
2391 * Set the cursor column.
2392 *
2393 * @param {integer} column The new zero-based cursor column.
2394 */
2395hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002396 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002397};
2398
2399/**
2400 * Return the cursor column.
2401 *
2402 * @return {integer} The zero-based cursor column.
2403 */
2404hterm.Terminal.prototype.getCursorColumn = function() {
2405 return this.screen_.cursorPosition.column;
2406};
2407
2408/**
2409 * Set the cursor row.
2410 *
2411 * The cursor row is relative to the scroll region if the terminal has
2412 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2413 *
2414 * @param {integer} row The new cursor row.
2415 */
rginda87b86462011-12-14 13:48:03 -08002416hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2417 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002418};
2419
2420/**
2421 * Return the cursor row.
2422 *
2423 * @return {integer} The zero-based cursor row.
2424 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002425hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002426 return this.screen_.cursorPosition.row;
2427};
2428
2429/**
2430 * Request that the ScrollPort redraw itself soon.
2431 *
2432 * The redraw will happen asynchronously, soon after the call stack winds down.
2433 * Multiple calls will be coalesced into a single redraw.
2434 */
2435hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002436 if (this.timeouts_.redraw)
2437 return;
rginda8ba33642011-12-14 12:31:31 -08002438
2439 var self = this;
rginda87b86462011-12-14 13:48:03 -08002440 this.timeouts_.redraw = setTimeout(function() {
2441 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002442 self.scrollPort_.redraw_();
2443 }, 0);
2444};
2445
2446/**
2447 * Request that the ScrollPort be scrolled to the bottom.
2448 *
2449 * The scroll will happen asynchronously, soon after the call stack winds down.
2450 * Multiple calls will be coalesced into a single scroll.
2451 *
2452 * This affects the scrollbar position of the ScrollPort, and has nothing to
2453 * do with the VT scroll commands.
2454 */
2455hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2456 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002457 return;
rginda8ba33642011-12-14 12:31:31 -08002458
2459 var self = this;
2460 this.timeouts_.scrollDown = setTimeout(function() {
2461 delete self.timeouts_.scrollDown;
2462 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2463 }, 10);
2464};
2465
2466/**
2467 * Move the cursor up a specified number of rows.
2468 *
2469 * @param {integer} count The number of rows to move the cursor.
2470 */
2471hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002472 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002473};
2474
2475/**
2476 * Move the cursor down a specified number of rows.
2477 *
2478 * @param {integer} count The number of rows to move the cursor.
2479 */
2480hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002481 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002482 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2483 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2484 this.screenSize.height - 1);
2485
rgindacbbd7482012-06-13 15:06:16 -07002486 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002487 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002488 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002489};
2490
2491/**
2492 * Move the cursor left a specified number of columns.
2493 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002494 * If reverse wraparound mode is enabled and the previous row wrapped into
2495 * the current row then we back up through the wraparound as well.
2496 *
rginda8ba33642011-12-14 12:31:31 -08002497 * @param {integer} count The number of columns to move the cursor.
2498 */
2499hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002500 count = count || 1;
2501
2502 if (count < 1)
2503 return;
2504
2505 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002506 if (this.options_.reverseWraparound) {
2507 if (this.screen_.cursorPosition.overflow) {
2508 // If this cursor is in the right margin, consume one count to get it
2509 // back to the last column. This only applies when we're in reverse
2510 // wraparound mode.
2511 count--;
2512 this.clearCursorOverflow();
2513
2514 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002515 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002516 }
2517
Robert Gindabfb32622014-07-17 13:20:27 -07002518 var newRow = this.screen_.cursorPosition.row;
2519 var newColumn = currentColumn - count;
2520 if (newColumn < 0) {
2521 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2522 if (newRow < 0) {
2523 // xterm also wraps from row 0 to the last row.
2524 newRow = this.screenSize.height + newRow % this.screenSize.height;
2525 }
2526 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2527 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002528
Robert Gindabfb32622014-07-17 13:20:27 -07002529 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2530
2531 } else {
2532 var newColumn = Math.max(currentColumn - count, 0);
2533 this.setCursorColumn(newColumn);
2534 }
rginda8ba33642011-12-14 12:31:31 -08002535};
2536
2537/**
2538 * Move the cursor right a specified number of columns.
2539 *
2540 * @param {integer} count The number of columns to move the cursor.
2541 */
2542hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002543 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002544
2545 if (count < 1)
2546 return;
2547
rgindacbbd7482012-06-13 15:06:16 -07002548 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002549 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002550 this.setCursorColumn(column);
2551};
2552
2553/**
2554 * Reverse the foreground and background colors of the terminal.
2555 *
2556 * This only affects text that was drawn with no attributes.
2557 *
2558 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2559 * been drawn with attributes that happen to coincide with the default
2560 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002561 *
2562 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002563 */
2564hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002565 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002566 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002567 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2568 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002569 } else {
rginda9f5222b2012-03-05 11:53:28 -08002570 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2571 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002572 }
2573};
2574
2575/**
rginda87b86462011-12-14 13:48:03 -08002576 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002577 *
2578 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002579 */
2580hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002581 this.cursorNode_.style.backgroundColor =
2582 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002583
2584 var self = this;
2585 setTimeout(function() {
Matheus Fernandes2d733082017-09-11 06:43:01 -04002586 self.restyleCursor_();
rginda6d397402012-01-17 10:58:29 -08002587 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002588
Michael Kelly485ecd12014-06-09 11:41:56 -04002589 // bellSquelchTimeout_ affects both audio and notification bells.
2590 if (this.bellSquelchTimeout_)
2591 return;
2592
Robert Ginda92e18102013-03-14 13:56:37 -07002593 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002594 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002595 this.bellSequelchTimeout_ = setTimeout(function() {
2596 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002597 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002598 } else {
2599 delete this.bellSquelchTimeout_;
2600 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002601
2602 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingera5fb83c2017-06-22 14:48:35 -07002603 var n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002604 this.bellNotificationList_.push(n);
2605 // TODO: Should we try to raise the window here?
2606 n.onclick = function() { self.closeBellNotifications_(); };
2607 }
rginda87b86462011-12-14 13:48:03 -08002608};
2609
2610/**
rginda8ba33642011-12-14 12:31:31 -08002611 * Set the origin mode bit.
2612 *
2613 * If origin mode is on, certain VT cursor and scrolling commands measure their
2614 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2615 * to the top of the addressable screen.
2616 *
2617 * Defaults to off.
2618 *
2619 * @param {boolean} state True to set origin mode, false to unset.
2620 */
2621hterm.Terminal.prototype.setOriginMode = function(state) {
2622 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002623 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002624};
2625
2626/**
2627 * Set the insert mode bit.
2628 *
2629 * If insert mode is on, existing text beyond the cursor position will be
2630 * shifted right to make room for new text. Otherwise, new text overwrites
2631 * any existing text.
2632 *
2633 * Defaults to off.
2634 *
2635 * @param {boolean} state True to set insert mode, false to unset.
2636 */
2637hterm.Terminal.prototype.setInsertMode = function(state) {
2638 this.options_.insertMode = state;
2639};
2640
2641/**
rginda87b86462011-12-14 13:48:03 -08002642 * Set the auto carriage return bit.
2643 *
2644 * If auto carriage return is on then a formfeed character is interpreted
2645 * as a newline, otherwise it's the same as a linefeed. The difference boils
2646 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002647 *
2648 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002649 */
2650hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2651 this.options_.autoCarriageReturn = state;
2652};
2653
2654/**
rginda8ba33642011-12-14 12:31:31 -08002655 * Set the wraparound mode bit.
2656 *
2657 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2658 * to the start of the following row. Otherwise, the cursor is clamped to the
2659 * end of the screen and attempts to write past it are ignored.
2660 *
2661 * Defaults to on.
2662 *
2663 * @param {boolean} state True to set wraparound mode, false to unset.
2664 */
2665hterm.Terminal.prototype.setWraparound = function(state) {
2666 this.options_.wraparound = state;
2667};
2668
2669/**
2670 * Set the reverse-wraparound mode bit.
2671 *
2672 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2673 * to the end of the previous row. Otherwise, the cursor is clamped to column
2674 * 0.
2675 *
2676 * Defaults to off.
2677 *
2678 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2679 */
2680hterm.Terminal.prototype.setReverseWraparound = function(state) {
2681 this.options_.reverseWraparound = state;
2682};
2683
2684/**
2685 * Selects between the primary and alternate screens.
2686 *
2687 * If alternate mode is on, the alternate screen is active. Otherwise the
2688 * primary screen is active.
2689 *
2690 * Swapping screens has no effect on the scrollback buffer.
2691 *
2692 * Each screen maintains its own cursor position.
2693 *
2694 * Defaults to off.
2695 *
2696 * @param {boolean} state True to set alternate mode, false to unset.
2697 */
2698hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002699 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002700 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2701
rginda35c456b2012-02-09 17:29:05 -08002702 if (this.screen_.rowsArray.length &&
2703 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2704 // If the screen changed sizes while we were away, our rowIndexes may
2705 // be incorrect.
2706 var offset = this.scrollbackRows_.length;
2707 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002708 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002709 ary[i].rowIndex = offset + i;
2710 }
2711 }
rginda8ba33642011-12-14 12:31:31 -08002712
rginda35c456b2012-02-09 17:29:05 -08002713 this.realizeWidth_(this.screenSize.width);
2714 this.realizeHeight_(this.screenSize.height);
2715 this.scrollPort_.syncScrollHeight();
2716 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002717
rginda6d397402012-01-17 10:58:29 -08002718 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002719 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002720};
2721
2722/**
2723 * Set the cursor-blink mode bit.
2724 *
2725 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2726 * a visible cursor does not blink.
2727 *
2728 * You should make sure to turn blinking off if you're going to dispose of a
2729 * terminal, otherwise you'll leak a timeout.
2730 *
2731 * Defaults to on.
2732 *
2733 * @param {boolean} state True to set cursor-blink mode, false to unset.
2734 */
2735hterm.Terminal.prototype.setCursorBlink = function(state) {
2736 this.options_.cursorBlink = state;
2737
2738 if (!state && this.timeouts_.cursorBlink) {
2739 clearTimeout(this.timeouts_.cursorBlink);
2740 delete this.timeouts_.cursorBlink;
2741 }
2742
2743 if (this.options_.cursorVisible)
2744 this.setCursorVisible(true);
2745};
2746
2747/**
2748 * Set the cursor-visible mode bit.
2749 *
2750 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2751 *
2752 * Defaults to on.
2753 *
2754 * @param {boolean} state True to set cursor-visible mode, false to unset.
2755 */
2756hterm.Terminal.prototype.setCursorVisible = function(state) {
2757 this.options_.cursorVisible = state;
2758
2759 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002760 if (this.timeouts_.cursorBlink) {
2761 clearTimeout(this.timeouts_.cursorBlink);
2762 delete this.timeouts_.cursorBlink;
2763 }
rginda87b86462011-12-14 13:48:03 -08002764 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002765 return;
2766 }
2767
rginda87b86462011-12-14 13:48:03 -08002768 this.syncCursorPosition_();
2769
2770 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002771
2772 if (this.options_.cursorBlink) {
2773 if (this.timeouts_.cursorBlink)
2774 return;
2775
Robert Gindaea2183e2014-07-17 09:51:51 -07002776 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002777 } else {
2778 if (this.timeouts_.cursorBlink) {
2779 clearTimeout(this.timeouts_.cursorBlink);
2780 delete this.timeouts_.cursorBlink;
2781 }
2782 }
2783};
2784
2785/**
rginda87b86462011-12-14 13:48:03 -08002786 * Synchronizes the visible cursor and document selection with the current
2787 * cursor coordinates.
Raymes Khourye5d48982018-08-02 09:08:32 +10002788 *
2789 * @return {boolean} True if the cursor is onscreen and synced.
rginda8ba33642011-12-14 12:31:31 -08002790 */
2791hterm.Terminal.prototype.syncCursorPosition_ = function() {
2792 var topRowIndex = this.scrollPort_.getTopRowIndex();
2793 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2794 var cursorRowIndex = this.scrollbackRows_.length +
2795 this.screen_.cursorPosition.row;
2796
Raymes Khoury15697f42018-07-17 11:37:18 +10002797 let forceSyncSelection = false;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002798 if (this.accessibilityReader_.accessibilityEnabled) {
2799 // Report the new position of the cursor for accessibility purposes.
2800 const cursorColumnIndex = this.screen_.cursorPosition.column;
2801 const cursorLineText =
2802 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
Raymes Khoury15697f42018-07-17 11:37:18 +10002803 // This will force the selection to be sync'd to the cursor position if the
2804 // user has pressed a key. Generally we would only sync the cursor position
2805 // when selection is collapsed so that if the user has selected something
2806 // we don't clear the selection by moving the selection. However when a
2807 // screen reader is used, it's intuitive for entering a key to move the
2808 // selection to the cursor.
2809 forceSyncSelection = this.accessibilityReader_.hasUserGesture;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002810 this.accessibilityReader_.afterCursorChange(
2811 cursorLineText, cursorRowIndex, cursorColumnIndex);
2812 }
2813
rginda8ba33642011-12-14 12:31:31 -08002814 if (cursorRowIndex > bottomRowIndex) {
2815 // Cursor is scrolled off screen, move it outside of the visible area.
Mike Frysinger44c32202017-08-05 01:13:09 -04002816 this.setCssVar('cursor-offset-row', '-1');
Raymes Khourye5d48982018-08-02 09:08:32 +10002817 return false;
rginda8ba33642011-12-14 12:31:31 -08002818 }
2819
Robert Gindab837c052014-08-11 11:17:51 -07002820 if (this.options_.cursorVisible &&
2821 this.cursorNode_.style.display == 'none') {
2822 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2823 this.cursorNode_.style.display = '';
2824 }
2825
Mike Frysinger44c32202017-08-05 01:13:09 -04002826 // Position the cursor using CSS variable math. If we do the math in JS,
2827 // the float math will end up being more precise than the CSS which will
2828 // cause the cursor tracking to be off.
2829 this.setCssVar(
2830 'cursor-offset-row',
2831 `${cursorRowIndex - topRowIndex} + ` +
2832 `${this.scrollPort_.visibleRowTopMargin}px`);
2833 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08002834
2835 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04002836 '(' + this.screen_.cursorPosition.column +
2837 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08002838 ')');
2839
2840 // Update the caret for a11y purposes.
2841 var selection = this.document_.getSelection();
Raymes Khoury15697f42018-07-17 11:37:18 +10002842 if (selection && (selection.isCollapsed || forceSyncSelection)) {
rginda87b86462011-12-14 13:48:03 -08002843 this.screen_.syncSelectionCaret(selection);
Raymes Khoury15697f42018-07-17 11:37:18 +10002844 }
Raymes Khourye5d48982018-08-02 09:08:32 +10002845 return true;
rginda8ba33642011-12-14 12:31:31 -08002846};
2847
Robert Gindafb1be6a2013-12-11 11:56:22 -08002848/**
2849 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2850 * and character cell dimensions.
2851 */
Robert Ginda830583c2013-08-07 13:20:46 -07002852hterm.Terminal.prototype.restyleCursor_ = function() {
2853 var shape = this.cursorShape_;
2854
2855 if (this.cursorNode_.getAttribute('focus') == 'false') {
2856 // Always show a block cursor when unfocused.
2857 shape = hterm.Terminal.cursorShape.BLOCK;
2858 }
2859
2860 var style = this.cursorNode_.style;
2861
2862 switch (shape) {
2863 case hterm.Terminal.cursorShape.BEAM:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002864 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002865 style.backgroundColor = 'transparent';
2866 style.borderBottomStyle = null;
2867 style.borderLeftStyle = 'solid';
2868 break;
2869
2870 case hterm.Terminal.cursorShape.UNDERLINE:
2871 style.height = this.scrollPort_.characterSize.baseline + 'px';
2872 style.backgroundColor = 'transparent';
2873 style.borderBottomStyle = 'solid';
2874 // correct the size to put it exactly at the baseline
2875 style.borderLeftStyle = null;
2876 break;
2877
2878 default:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002879 style.height = 'var(--hterm-charsize-height)';
Mike Frysinger2fd079a2018-09-02 01:46:12 -04002880 style.backgroundColor = 'var(--hterm-cursor-color)';
Robert Ginda830583c2013-08-07 13:20:46 -07002881 style.borderBottomStyle = null;
2882 style.borderLeftStyle = null;
2883 break;
2884 }
2885};
2886
rginda8ba33642011-12-14 12:31:31 -08002887/**
2888 * Synchronizes the visible cursor with the current cursor coordinates.
2889 *
2890 * The sync will happen asynchronously, soon after the call stack winds down.
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002891 * Multiple calls will be coalesced into a single sync. This should be called
2892 * prior to the cursor actually changing position.
rginda8ba33642011-12-14 12:31:31 -08002893 */
2894hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2895 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002896 return;
rginda8ba33642011-12-14 12:31:31 -08002897
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002898 if (this.accessibilityReader_.accessibilityEnabled) {
2899 // Report the previous position of the cursor for accessibility purposes.
2900 const cursorRowIndex = this.scrollbackRows_.length +
2901 this.screen_.cursorPosition.row;
2902 const cursorColumnIndex = this.screen_.cursorPosition.column;
2903 const cursorLineText =
2904 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
2905 this.accessibilityReader_.beforeCursorChange(
2906 cursorLineText, cursorRowIndex, cursorColumnIndex);
2907 }
2908
rginda8ba33642011-12-14 12:31:31 -08002909 var self = this;
2910 this.timeouts_.syncCursor = setTimeout(function() {
2911 self.syncCursorPosition_();
2912 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002913 }, 0);
2914};
2915
rgindacc2996c2012-02-24 14:59:31 -08002916/**
rgindaf522ce02012-04-17 17:49:17 -07002917 * Show or hide the zoom warning.
2918 *
2919 * The zoom warning is a message warning the user that their browser zoom must
2920 * be set to 100% in order for hterm to function properly.
2921 *
2922 * @param {boolean} state True to show the message, false to hide it.
2923 */
2924hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2925 if (!this.zoomWarningNode_) {
2926 if (!state)
2927 return;
2928
2929 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002930 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07002931 this.zoomWarningNode_.style.cssText = (
2932 'color: black;' +
2933 'background-color: #ff2222;' +
2934 'font-size: large;' +
2935 'border-radius: 8px;' +
2936 'opacity: 0.75;' +
2937 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2938 'top: 0.5em;' +
2939 'right: 1.2em;' +
2940 'position: absolute;' +
2941 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002942 '-webkit-user-select: none;' +
2943 '-moz-text-size-adjust: none;' +
2944 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05002945
2946 this.zoomWarningNode_.addEventListener('click', function(e) {
2947 this.parentNode.removeChild(this);
2948 });
rgindaf522ce02012-04-17 17:49:17 -07002949 }
2950
Mike Frysingerb7289952019-03-23 16:05:38 -07002951 this.zoomWarningNode_.textContent = lib.i18n.replaceReferences(
Robert Gindab4839c22013-02-28 16:52:10 -08002952 hterm.zoomWarningMessage,
2953 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2954
rgindaf522ce02012-04-17 17:49:17 -07002955 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2956
2957 if (state) {
2958 if (!this.zoomWarningNode_.parentNode)
2959 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2960 } else if (this.zoomWarningNode_.parentNode) {
2961 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2962 }
2963};
2964
2965/**
rgindacc2996c2012-02-24 14:59:31 -08002966 * Show the terminal overlay for a given amount of time.
2967 *
2968 * The terminal overlay appears in inverse video in a large font, centered
2969 * over the terminal. You should probably keep the overlay message brief,
2970 * since it's in a large font and you probably aren't going to check the size
2971 * of the terminal first.
2972 *
2973 * @param {string} msg The text (not HTML) message to display in the overlay.
2974 * @param {number} opt_timeout The amount of time to wait before fading out
2975 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2976 * stay up forever (or until the next overlay).
2977 */
2978hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002979 if (!this.overlayNode_) {
2980 if (!this.div_)
2981 return;
2982
2983 this.overlayNode_ = this.document_.createElement('div');
2984 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002985 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002986 'font-size: xx-large;' +
2987 'opacity: 0.75;' +
2988 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2989 'position: absolute;' +
2990 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002991 '-webkit-transition: opacity 180ms ease-in;' +
2992 '-moz-user-select: none;' +
2993 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002994
2995 this.overlayNode_.addEventListener('mousedown', function(e) {
2996 e.preventDefault();
2997 e.stopPropagation();
2998 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002999 }
3000
rginda9f5222b2012-03-05 11:53:28 -08003001 this.overlayNode_.style.color = this.prefs_.get('background-color');
3002 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
3003 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
3004
rgindaf0090c92012-02-10 14:58:52 -08003005 this.overlayNode_.textContent = msg;
3006 this.overlayNode_.style.opacity = '0.75';
3007
3008 if (!this.overlayNode_.parentNode)
3009 this.div_.appendChild(this.overlayNode_);
3010
Robert Ginda97769282013-02-01 15:30:30 -08003011 var divSize = hterm.getClientSize(this.div_);
3012 var overlaySize = hterm.getClientSize(this.overlayNode_);
3013
Robert Ginda8a59f762014-07-23 11:29:55 -07003014 this.overlayNode_.style.top =
3015 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08003016 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07003017 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08003018
rgindaf0090c92012-02-10 14:58:52 -08003019 if (this.overlayTimeout_)
3020 clearTimeout(this.overlayTimeout_);
3021
Raymes Khouryc7a06382018-07-04 10:25:45 +10003022 this.accessibilityReader_.assertiveAnnounce(msg);
3023
rgindacc2996c2012-02-24 14:59:31 -08003024 if (opt_timeout === null)
3025 return;
3026
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003027 this.overlayTimeout_ = setTimeout(() => {
3028 this.overlayNode_.style.opacity = '0';
3029 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
3030 }, opt_timeout || 1500);
3031};
3032
3033/**
3034 * Hide the terminal overlay immediately.
3035 *
3036 * Useful when we show an overlay for an event with an unknown end time.
3037 */
3038hterm.Terminal.prototype.hideOverlay = function() {
3039 if (this.overlayTimeout_)
3040 clearTimeout(this.overlayTimeout_);
3041 this.overlayTimeout_ = null;
3042
3043 if (this.overlayNode_.parentNode)
3044 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
3045 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08003046};
3047
rginda4bba5e12012-06-20 16:15:30 -07003048/**
3049 * Paste from the system clipboard to the terminal.
3050 */
3051hterm.Terminal.prototype.paste = function() {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003052 return hterm.pasteFromClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07003053};
3054
3055/**
3056 * Copy a string to the system clipboard.
3057 *
3058 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05003059 *
3060 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07003061 */
3062hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08003063 if (this.prefs_.get('enable-clipboard-notice'))
3064 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
3065
Mike Frysinger96eacae2019-01-02 18:13:56 -05003066 hterm.copySelectionToClipboard(this.document_, str);
rginda4bba5e12012-06-20 16:15:30 -07003067};
3068
Evan Jones2600d4f2016-12-06 09:29:36 -05003069/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003070 * Display an image.
3071 *
Mike Frysinger2558ed52019-01-14 01:03:41 -05003072 * Either URI or buffer or blob fields must be specified.
3073 *
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003074 * @param {Object} options The image to display.
3075 * @param {string=} options.name A human readable string for the image.
3076 * @param {string|number=} options.size The size (in bytes).
3077 * @param {boolean=} options.preserveAspectRatio Whether to preserve aspect.
3078 * @param {boolean=} options.inline Whether to display the image inline.
3079 * @param {string|number=} options.width The width of the image.
3080 * @param {string|number=} options.height The height of the image.
3081 * @param {string=} options.align Direction to align the image.
Mike Frysinger2558ed52019-01-14 01:03:41 -05003082 * @param {string=} options.uri The source URI for the image.
3083 * @param {ArrayBuffer=} options.buffer The ArrayBuffer image data.
3084 * @param {Blob=} options.blob The Blob image data.
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003085 * @param {string=} options.type The MIME type of the image data.
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003086 * @param {function=} onLoad Callback when loading finishes.
3087 * @param {function(Event)=} onError Callback when loading fails.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003088 */
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003089hterm.Terminal.prototype.displayImage = function(options, onLoad, onError) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003090 // Make sure we're actually given a resource to display.
Mike Frysinger2558ed52019-01-14 01:03:41 -05003091 if (options.uri === undefined && options.buffer === undefined &&
3092 options.blob === undefined)
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003093 return;
3094
3095 // Set up the defaults to simplify code below.
3096 if (!options.name)
3097 options.name = '';
3098
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003099 // See if the mime type is available. If not, guess from the filename.
3100 // We don't list all possible mime types because the browser can usually
3101 // guess it correctly. So list the ones that need a bit more help.
3102 if (!options.type) {
3103 const ary = options.name.split('.');
3104 const ext = ary[ary.length - 1].trim();
3105 switch (ext) {
3106 case 'svg':
3107 case 'svgz':
3108 options.type = 'image/svg+xml';
3109 break;
3110 }
3111 }
3112
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003113 // Has the user approved image display yet?
3114 if (this.allowImagesInline !== true) {
3115 this.newLine();
3116 const row = this.getRowNode(this.scrollbackRows_.length +
3117 this.getCursorRow() - 1);
3118
3119 if (this.allowImagesInline === false) {
3120 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
3121 'Inline Images Disabled');
3122 return;
3123 }
3124
3125 // Show a prompt.
3126 let button;
3127 const span = this.document_.createElement('span');
3128 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
3129 span.style.fontWeight = 'bold';
3130 span.style.borderWidth = '1px';
3131 span.style.borderStyle = 'dashed';
3132 button = this.document_.createElement('span');
3133 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3134 button.style.marginLeft = '1em';
3135 button.style.borderWidth = '1px';
3136 button.style.borderStyle = 'solid';
3137 button.addEventListener('click', () => {
3138 this.prefs_.set('allow-images-inline', false);
3139 });
3140 span.appendChild(button);
3141 button = this.document_.createElement('span');
3142 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3143 'allow this session');
3144 button.style.marginLeft = '1em';
3145 button.style.borderWidth = '1px';
3146 button.style.borderStyle = 'solid';
3147 button.addEventListener('click', () => {
3148 this.allowImagesInline = true;
3149 });
3150 span.appendChild(button);
3151 button = this.document_.createElement('span');
3152 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3153 button.style.marginLeft = '1em';
3154 button.style.borderWidth = '1px';
3155 button.style.borderStyle = 'solid';
3156 button.addEventListener('click', () => {
3157 this.prefs_.set('allow-images-inline', true);
3158 });
3159 span.appendChild(button);
3160
3161 row.appendChild(span);
3162 return;
3163 }
3164
3165 // See if we should show this object directly, or download it.
3166 if (options.inline) {
3167 const io = this.io.push();
3168 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
3169 'Loading $1 ...'), null);
3170
3171 // While we're loading the image, eat all the user's input.
3172 io.onVTKeystroke = io.sendString = () => {};
3173
3174 // Initialize this new image.
Adrián Pérez-Orozco6a550322018-08-31 14:36:06 -07003175 const img =
3176 /** @type {!HTMLImageElement} */ (this.document_.createElement('img'));
Mike Frysinger2558ed52019-01-14 01:03:41 -05003177 if (options.uri !== undefined) {
3178 img.src = options.uri;
3179 } else if (options.buffer !== undefined) {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003180 const blob = new Blob([options.buffer], {type: options.type});
Mike Frysinger2558ed52019-01-14 01:03:41 -05003181 img.src = URL.createObjectURL(blob);
3182 } else {
Mike Frysingerb9eb8432019-01-20 19:33:24 -05003183 const blob = new Blob([options.blob], {type: options.type});
Mike Frysinger2558ed52019-01-14 01:03:41 -05003184 img.src = URL.createObjectURL(options.blob);
3185 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003186 img.title = img.alt = options.name;
3187
3188 // Attach the image to the page to let it load/render. It won't stay here.
3189 // This is needed so it's visible and the DOM can calculate the height. If
3190 // the image is hidden or not in the DOM, the height is always 0.
3191 this.document_.body.appendChild(img);
3192
3193 // Wait for the image to finish loading before we try moving it to the
3194 // right place in the terminal.
3195 img.onload = () => {
3196 // Now that we have the image dimensions, figure out how to show it.
3197 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
3198 img.style.maxWidth = `${this.document_.body.clientWidth}px`;
3199 img.style.maxHeight = `${this.document_.body.clientHeight}px`;
3200
3201 // Parse a width/height specification.
3202 const parseDim = (dim, maxDim, cssVar) => {
3203 if (!dim || dim == 'auto')
3204 return '';
3205
3206 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3207 if (ary) {
3208 if (ary[2] == '%')
3209 return maxDim * parseInt(ary[1]) / 100 + 'px';
3210 else if (ary[2] == 'px')
3211 return dim;
3212 else
3213 return `calc(${dim} * var(${cssVar}))`;
3214 }
3215
3216 return '';
3217 };
3218 img.style.width =
3219 parseDim(options.width, this.document_.body.clientWidth,
3220 '--hterm-charsize-width');
3221 img.style.height =
3222 parseDim(options.height, this.document_.body.clientHeight,
3223 '--hterm-charsize-height');
3224
3225 // Figure out how many rows the image occupies, then add that many.
3226 // XXX: This count will be inaccurate if the font size changes on us.
3227 const padRows = Math.ceil(img.clientHeight /
3228 this.scrollPort_.characterSize.height);
3229 for (let i = 0; i < padRows; ++i)
3230 this.newLine();
3231
3232 // Update the max height in case the user shrinks the character size.
3233 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3234
3235 // Move the image to the last row. This way when we scroll up, it doesn't
3236 // disappear when the first row gets clipped. It will disappear when we
3237 // scroll down and the last row is clipped ...
3238 this.document_.body.removeChild(img);
3239 // Create a wrapper node so we can do an absolute in a relative position.
3240 // This helps with rounding errors between JS & CSS counts.
3241 const div = this.document_.createElement('div');
3242 div.style.position = 'relative';
3243 div.style.textAlign = options.align;
3244 img.style.position = 'absolute';
3245 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3246 div.appendChild(img);
3247 const row = this.getRowNode(this.scrollbackRows_.length +
3248 this.getCursorRow() - 1);
3249 row.appendChild(div);
3250
Mike Frysinger2558ed52019-01-14 01:03:41 -05003251 // Now that the image has been read, we can revoke the source.
3252 if (options.uri === undefined) {
3253 URL.revokeObjectURL(img.src);
3254 }
3255
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003256 io.hideOverlay();
3257 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003258
3259 if (onLoad)
3260 onLoad();
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003261 };
3262
3263 // If we got a malformed image, give up.
3264 img.onerror = (e) => {
3265 this.document_.body.removeChild(img);
3266 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
Mike Frysingere14a8c42018-03-10 00:17:30 -08003267 'Loading $1 failed'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003268 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003269
3270 if (onError)
3271 onError(e);
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003272 };
3273 } else {
3274 // We can't use chrome.downloads.download as that requires "downloads"
3275 // permissions, and that works only in extensions, not apps.
3276 const a = this.document_.createElement('a');
Mike Frysinger2558ed52019-01-14 01:03:41 -05003277 if (options.uri !== undefined) {
3278 a.href = options.uri;
3279 } else if (options.buffer !== undefined) {
3280 const blob = new Blob([options.buffer]);
3281 a.href = URL.createObjectURL(blob);
3282 } else {
3283 a.href = URL.createObjectURL(options.blob);
3284 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003285 a.download = options.name;
3286 this.document_.body.appendChild(a);
3287 a.click();
3288 a.remove();
Mike Frysinger2558ed52019-01-14 01:03:41 -05003289 if (options.uri === undefined) {
3290 URL.revokeObjectURL(a.href);
3291 }
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003292 }
3293};
3294
3295/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003296 * Returns the selected text, or null if no text is selected.
3297 *
3298 * @return {string|null}
3299 */
rgindaa09e7332012-08-17 12:49:51 -07003300hterm.Terminal.prototype.getSelectionText = function() {
3301 var selection = this.scrollPort_.selection;
3302 selection.sync();
3303
3304 if (selection.isCollapsed)
3305 return null;
3306
rgindaa09e7332012-08-17 12:49:51 -07003307 // Start offset measures from the beginning of the line.
3308 var startOffset = selection.startOffset;
3309 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003310
Raymes Khoury334625a2018-06-25 10:29:40 +10003311 // If an x-row isn't selected, |node| will be null.
3312 if (!node)
3313 return null;
3314
Robert Gindafdbb3f22012-09-06 20:23:06 -07003315 if (node.nodeName != 'X-ROW') {
3316 // If the selection doesn't start on an x-row node, then it must be
3317 // somewhere inside the x-row. Add any characters from previous siblings
3318 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003319
3320 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3321 // If node is the text node in a styled span, move up to the span node.
3322 node = node.parentNode;
3323 }
3324
Robert Gindafdbb3f22012-09-06 20:23:06 -07003325 while (node.previousSibling) {
3326 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003327 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003328 }
rgindaa09e7332012-08-17 12:49:51 -07003329 }
3330
3331 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08003332 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
3333 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003334 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003335
Robert Gindafdbb3f22012-09-06 20:23:06 -07003336 if (node.nodeName != 'X-ROW') {
3337 // If the selection doesn't end on an x-row node, then it must be
3338 // somewhere inside the x-row. Add any characters from following siblings
3339 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003340
3341 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3342 // If node is the text node in a styled span, move up to the span node.
3343 node = node.parentNode;
3344 }
3345
Robert Gindafdbb3f22012-09-06 20:23:06 -07003346 while (node.nextSibling) {
3347 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003348 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003349 }
rgindaa09e7332012-08-17 12:49:51 -07003350 }
3351
3352 var rv = this.getRowsText(selection.startRow.rowIndex,
3353 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003354 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003355};
3356
rginda4bba5e12012-06-20 16:15:30 -07003357/**
3358 * Copy the current selection to the system clipboard, then clear it after a
3359 * short delay.
3360 */
3361hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07003362 var text = this.getSelectionText();
3363 if (text != null)
3364 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07003365};
3366
rgindaf0090c92012-02-10 14:58:52 -08003367hterm.Terminal.prototype.overlaySize = function() {
3368 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
3369};
3370
rginda87b86462011-12-14 13:48:03 -08003371/**
3372 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3373 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003374 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003375 */
3376hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08003377 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08003378 this.scrollPort_.scrollRowToBottom(this.getRowCount());
3379
Mike Frysinger79669762018-12-30 20:51:10 -05003380 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08003381};
3382
3383/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003384 * Open the selected url.
3385 */
3386hterm.Terminal.prototype.openSelectedUrl_ = function() {
3387 var str = this.getSelectionText();
3388
3389 // If there is no selection, try and expand wherever they clicked.
3390 if (str == null) {
John Lincae9b732018-03-08 13:56:35 +08003391 this.screen_.expandSelectionForUrl(this.document_.getSelection());
Mike Frysinger70b94692017-01-26 18:57:50 -10003392 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003393
3394 // If clicking in empty space, return.
3395 if (str == null)
3396 return;
Mike Frysinger70b94692017-01-26 18:57:50 -10003397 }
3398
3399 // Make sure URL is valid before opening.
3400 if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0)
3401 return;
Mike Frysinger43472622017-06-26 18:11:07 -04003402
3403 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003404 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003405 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3406 // We have to whitelist a few protocols that lack authorities and thus
3407 // never use the //. Like mailto.
3408 switch (str.split(':', 1)[0]) {
3409 case 'mailto':
3410 break;
3411 default:
3412 str = 'http://' + str;
3413 break;
3414 }
3415 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003416
Mike Frysinger720fa832017-10-23 01:15:52 -04003417 hterm.openUrl(str);
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003418};
Mike Frysinger70b94692017-01-26 18:57:50 -10003419
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003420/**
3421 * Manage the automatic mouse hiding behavior while typing.
3422 *
3423 * @param {boolean=} v Whether to enable automatic hiding.
3424 */
3425hterm.Terminal.prototype.setAutomaticMouseHiding = function(v=null) {
3426 // Since Chrome OS & macOS do this by default everywhere, we don't need to.
3427 // Linux & Windows seem to leave this to specific applications to manage.
3428 if (v === null)
3429 v = (hterm.os != 'cros' && hterm.os != 'mac');
3430
3431 this.mouseHideWhileTyping_ = !!v;
3432};
3433
3434/**
3435 * Handler for monitoring user keyboard activity.
3436 *
3437 * This isn't for processing the keystrokes directly, but for updating any
3438 * state that might toggle based on the user using the keyboard at all.
3439 *
3440 * @param {KeyboardEvent} e The keyboard event that triggered us.
3441 */
3442hterm.Terminal.prototype.onKeyboardActivity_ = function(e) {
3443 // When the user starts typing, hide the mouse cursor.
3444 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_)
3445 this.setCssVar('mouse-cursor-style', 'none');
3446};
Mike Frysinger70b94692017-01-26 18:57:50 -10003447
3448/**
rgindad5613292012-06-19 15:40:37 -07003449 * Add the terminalRow and terminalColumn properties to mouse events and
3450 * then forward on to onMouse().
3451 *
3452 * The terminalRow and terminalColumn properties contain the (row, column)
3453 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003454 *
3455 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003456 */
3457hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003458 if (e.processedByTerminalHandler_) {
3459 // We register our event handlers on the document, as well as the cursor
3460 // and the scroll blocker. Mouse events that occur on the cursor or
3461 // scroll blocker will also appear on the document, but we don't want to
3462 // process them twice.
3463 //
3464 // We can't just prevent bubbling because that has other side effects, so
3465 // we decorate the event object with this property instead.
3466 return;
3467 }
3468
Mike Frysinger468966c2018-08-28 13:48:51 -04003469 // Consume navigation events. Button 3 is usually "browser back" and
3470 // button 4 is "browser forward" which we don't want to happen.
3471 if (e.button > 2) {
3472 e.preventDefault();
3473 // We don't return so click events can be passed to the remote below.
3474 }
3475
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003476 var reportMouseEvents = (!this.defeatMouseReports_ &&
3477 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3478
rgindafaa74742012-08-21 13:34:03 -07003479 e.processedByTerminalHandler_ = true;
3480
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003481 // Handle auto hiding of mouse cursor while typing.
3482 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
3483 // Make sure the mouse cursor is visible.
3484 this.syncMouseStyle();
3485 // This debounce isn't perfect, but should work well enough for such a
3486 // simple implementation. If the user moved the mouse, we enabled this
3487 // debounce, and then moved the mouse just before the timeout, we wouldn't
3488 // debounce that later movement.
3489 this.mouseHideDelay_ = setTimeout(() => this.mouseHideDelay_ = null, 1000);
3490 }
3491
Robert Gindaeda48db2014-07-17 09:25:30 -07003492 // One based row/column stored on the mouse event.
3493 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
3494 this.scrollPort_.characterSize.height) + 1;
3495 e.terminalColumn = parseInt(e.clientX /
3496 this.scrollPort_.characterSize.width) + 1;
3497
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003498 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
3499 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07003500 return;
3501 }
3502
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003503 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07003504 // If the cursor is visible and we're not sending mouse events to the
3505 // host app, then we want to hide the terminal cursor when the mouse
3506 // cursor is over top. This keeps the terminal cursor from interfering
3507 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003508 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3509 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3510 this.cursorNode_.style.display = 'none';
3511 } else if (this.cursorNode_.style.display == 'none') {
3512 this.cursorNode_.style.display = '';
3513 }
3514 }
rgindad5613292012-06-19 15:40:37 -07003515
Robert Ginda928cf632014-03-05 15:07:41 -08003516 if (e.type == 'mousedown') {
Mike Frysingercc114512017-09-11 21:39:17 -04003517 this.contextMenu.hide(e);
3518
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003519 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003520 // If VT mouse reporting is disabled, or has been defeated with
3521 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003522 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003523 this.setSelectionEnabled(true);
3524 } else {
3525 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003526 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003527 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003528 this.setSelectionEnabled(false);
3529 e.preventDefault();
3530 }
3531 }
3532
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003533 if (!reportMouseEvents) {
John Lin2aad22e2018-03-16 13:58:11 +08003534 if (e.type == 'dblclick') {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003535 this.screen_.expandSelection(this.document_.getSelection());
John Lin2aad22e2018-03-16 13:58:11 +08003536 if (this.copyOnSelect)
Mike Frysinger406c76c2019-01-02 17:53:51 -05003537 this.copySelectionToClipboard();
rgindad5613292012-06-19 15:40:37 -07003538 }
3539
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003540 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003541 // Debounce this event with the dblclick event. If you try to doubleclick
3542 // a URL to open it, Chrome will fire click then dblclick, but we won't
3543 // have expanded the selection text at the first click event.
3544 clearTimeout(this.timeouts_.openUrl);
3545 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3546 500);
3547 return;
3548 }
3549
Mike Frysinger847577f2017-05-23 23:25:57 -04003550 if (e.type == 'mousedown') {
Mike Frysingercc114512017-09-11 21:39:17 -04003551 if (e.ctrlKey && e.button == 2 /* right button */) {
3552 e.preventDefault();
3553 this.contextMenu.show(e, this);
3554 } else if (e.button == this.mousePasteButton ||
3555 (this.mouseRightClickPaste && e.button == 2 /* right button */)) {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003556 if (!this.paste())
Mike Frysinger05a57f02017-08-27 17:48:55 -04003557 console.warn('Could not paste manually due to web restrictions');
Mike Frysinger847577f2017-05-23 23:25:57 -04003558 }
3559 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003560
Mike Frysinger2edd3612017-05-24 00:54:39 -04003561 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003562 !this.document_.getSelection().isCollapsed) {
Mike Frysinger406c76c2019-01-02 17:53:51 -05003563 this.copySelectionToClipboard();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003564 }
3565
3566 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3567 this.scrollBlockerNode_.engaged) {
3568 // Disengage the scroll-blocker after one of these events.
3569 this.scrollBlockerNode_.engaged = false;
3570 this.scrollBlockerNode_.style.top = '-99px';
3571 }
3572
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003573 // Emulate arrow key presses via scroll wheel events.
3574 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3575 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003576 if (e.type == 'wheel') {
Mike Frysinger321063c2018-08-29 15:33:14 -04003577 const delta = this.scrollPort_.scrollWheelDelta(e);
Mike Frysingerc3030a82017-05-29 14:16:11 -04003578
Mike Frysinger321063c2018-08-29 15:33:14 -04003579 // Helper to turn a wheel event delta into a series of key presses.
3580 const deltaToArrows = (distance, charSize, arrowPos, arrowNeg) => {
3581 if (distance == 0) {
3582 return '';
3583 }
3584
3585 // Convert the scroll distance into a number of rows/cols.
3586 const cells = lib.f.smartFloorDivide(Math.abs(distance), charSize);
3587 const data = '\x1bO' + (distance < 0 ? arrowNeg : arrowPos);
3588 return data.repeat(cells);
3589 };
3590
3591 // The order between up/down and left/right doesn't really matter.
3592 this.io.sendString(
3593 // Up/down arrow keys.
3594 deltaToArrows(delta.y, this.scrollPort_.characterSize.height,
3595 'A', 'B') +
3596 // Left/right arrow keys.
3597 deltaToArrows(delta.x, this.scrollPort_.characterSize.width,
3598 'C', 'D')
3599 );
Mike Frysingerc3030a82017-05-29 14:16:11 -04003600
3601 e.preventDefault();
3602 }
3603 }
Robert Ginda928cf632014-03-05 15:07:41 -08003604 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003605 if (!this.scrollBlockerNode_.engaged) {
3606 if (e.type == 'mousedown') {
3607 // Move the scroll-blocker into place if we want to keep the scrollport
3608 // from scrolling.
3609 this.scrollBlockerNode_.engaged = true;
3610 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3611 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3612 } else if (e.type == 'mousemove') {
3613 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3614 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003615 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003616 e.preventDefault();
3617 }
3618 }
Robert Ginda928cf632014-03-05 15:07:41 -08003619
3620 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003621 }
3622
Robert Ginda928cf632014-03-05 15:07:41 -08003623 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3624 // Restore this on mouseup in case it was temporarily defeated with a
3625 // alt-mousedown. Only do this when the selection is empty so that
3626 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003627 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003628 }
rgindad5613292012-06-19 15:40:37 -07003629};
3630
3631/**
3632 * Clients should override this if they care to know about mouse events.
3633 *
3634 * The event parameter will be a normal DOM mouse click event with additional
3635 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003636 *
3637 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003638 */
3639hterm.Terminal.prototype.onMouse = function(e) { };
3640
3641/**
rginda8e92a692012-05-20 19:37:20 -07003642 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003643 *
3644 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003645 */
Rob Spies06533ba2014-04-24 11:20:37 -07003646hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3647 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003648 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003649
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003650 if (this.reportFocus)
3651 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O');
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003652
Michael Kelly485ecd12014-06-09 11:41:56 -04003653 if (focused === true)
3654 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07003655};
3656
3657/**
rginda8ba33642011-12-14 12:31:31 -08003658 * React when the ScrollPort is scrolled.
3659 */
3660hterm.Terminal.prototype.onScroll_ = function() {
3661 this.scheduleSyncCursorPosition_();
3662};
3663
3664/**
rginda9846e2f2012-01-27 13:53:33 -08003665 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003666 *
3667 * @param {Event} e The DOM paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08003668 */
3669hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaa063b202014-07-21 11:08:25 -07003670 var data = e.text.replace(/\n/mg, '\r');
Mike Frysingere8c32c82018-03-11 14:57:28 -07003671 if (this.options_.bracketedPaste) {
3672 // We strip out most escape sequences as they can cause issues (like
3673 // inserting an \x1b[201~ midstream). We pass through whitespace
3674 // though: 0x08:\b 0x09:\t 0x0a:\n 0x0d:\r.
3675 // This matches xterm behavior.
3676 const filter = (data) => data.replace(/[\x00-\x07\x0b-\x0c\x0e-\x1f]/g, '');
3677 data = '\x1b[200~' + filter(data) + '\x1b[201~';
3678 }
Robert Gindaa063b202014-07-21 11:08:25 -07003679
3680 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003681};
3682
3683/**
rgindaa09e7332012-08-17 12:49:51 -07003684 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003685 *
3686 * @param {Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07003687 */
3688hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07003689 if (!this.useDefaultWindowCopy) {
3690 e.preventDefault();
3691 setTimeout(this.copySelectionToClipboard.bind(this), 0);
3692 }
rgindaa09e7332012-08-17 12:49:51 -07003693};
3694
3695/**
rginda8ba33642011-12-14 12:31:31 -08003696 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08003697 *
3698 * Note: This function should not directly contain code that alters the internal
3699 * state of the terminal. That kind of code belongs in realizeWidth or
3700 * realizeHeight, so that it can be executed synchronously in the case of a
3701 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08003702 */
3703hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08003704 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07003705 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08003706 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07003707 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08003708
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003709 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08003710 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003711 // gets removed from the document or during the initial load, and we can't
3712 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07003713 // This can also happen if called before the scrollPort calculates the
3714 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08003715 return;
3716 }
3717
rgindaa8ba17d2012-08-15 14:41:10 -07003718 var isNewSize = (columnCount != this.screenSize.width ||
3719 rowCount != this.screenSize.height);
3720
3721 // We do this even if the size didn't change, just to be sure everything is
3722 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04003723 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07003724 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07003725
3726 if (isNewSize)
3727 this.overlaySize();
3728
Robert Gindafb1be6a2013-12-11 11:56:22 -08003729 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07003730 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08003731};
3732
3733/**
3734 * Service the cursor blink timeout.
3735 */
3736hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07003737 if (!this.options_.cursorBlink) {
3738 delete this.timeouts_.cursorBlink;
3739 return;
3740 }
3741
Robert Ginda830583c2013-08-07 13:20:46 -07003742 if (this.cursorNode_.getAttribute('focus') == 'false' ||
3743 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08003744 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07003745 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3746 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08003747 } else {
rginda87b86462011-12-14 13:48:03 -08003748 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07003749 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3750 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08003751 }
3752};
David Reveman8f552492012-03-28 12:18:41 -04003753
3754/**
3755 * Set the scrollbar-visible mode bit.
3756 *
3757 * If scrollbar-visible is on, the vertical scrollbar will be visible.
3758 * Otherwise it will not.
3759 *
3760 * Defaults to on.
3761 *
3762 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
3763 */
3764hterm.Terminal.prototype.setScrollbarVisible = function(state) {
3765 this.scrollPort_.setScrollbarVisible(state);
3766};
Michael Kelly485ecd12014-06-09 11:41:56 -04003767
3768/**
Rob Spies49039e52014-12-17 13:40:04 -08003769 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04003770 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08003771 *
3772 * Defaults to 1.
3773 *
Evan Jones2600d4f2016-12-06 09:29:36 -05003774 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08003775 */
3776hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3777 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3778};
3779
3780/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003781 * Close all web notifications created by terminal bells.
3782 */
3783hterm.Terminal.prototype.closeBellNotifications_ = function() {
3784 this.bellNotificationList_.forEach(function(n) {
3785 n.close();
3786 });
3787 this.bellNotificationList_.length = 0;
3788};
Raymes Khourye5d48982018-08-02 09:08:32 +10003789
3790/**
3791 * Syncs the cursor position when the scrollport gains focus.
3792 */
3793hterm.Terminal.prototype.onScrollportFocus_ = function() {
3794 // If the cursor is offscreen we set selection to the last row on the screen.
3795 const topRowIndex = this.scrollPort_.getTopRowIndex();
3796 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
3797 const selection = this.document_.getSelection();
3798 if (!this.syncCursorPosition_() && selection) {
3799 selection.collapse(this.getRowNode(bottomRowIndex));
3800 }
3801};