blob: 30c3adca4a4260533fa8b9a109b90b2fde2eb27d [file] [log] [blame]
rginda87b86462011-12-14 13:48:03 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rginda8ba33642011-12-14 12:31:31 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rgindacbbd7482012-06-13 15:06:16 -07005'use strict';
6
Masaya Suzuki273aa982014-05-31 07:25:55 +09007lib.rtdep('lib.colors', 'lib.PreferenceManager', 'lib.resource', 'lib.wc',
Raymes Khoury3e44bc92018-05-17 10:54:23 +10008 'lib.f', 'hterm.AccessibilityReader', 'hterm.Keyboard',
9 'hterm.Options', 'hterm.PreferenceManager', 'hterm.Screen',
10 'hterm.ScrollPort', 'hterm.Size', 'hterm.TextAttributes', 'hterm.VT');
rgindacbbd7482012-06-13 15:06:16 -070011
rginda8ba33642011-12-14 12:31:31 -080012/**
13 * Constructor for the Terminal class.
14 *
15 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
16 * classes to provide the complete terminal functionality.
17 *
18 * There are a number of lower-level Terminal methods that can be called
19 * directly to manipulate the cursor, text, scroll region, and other terminal
20 * attributes. However, the primary method is interpret(), which parses VT
21 * escape sequences and invokes the appropriate Terminal methods.
22 *
23 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
24 *
25 * TODO(rginda): Eventually we're going to need to support characters which are
26 * displayed twice as wide as standard latin characters. This is to support
27 * CJK (and possibly other character sets).
rginda9f5222b2012-03-05 11:53:28 -080028 *
Robert Ginda57f03b42012-09-13 11:02:48 -070029 * @param {string} opt_profileId Optional preference profile name. If not
rginda9f5222b2012-03-05 11:53:28 -080030 * provided, defaults to 'default'.
rginda8ba33642011-12-14 12:31:31 -080031 */
Robert Ginda57f03b42012-09-13 11:02:48 -070032hterm.Terminal = function(opt_profileId) {
33 this.profileId_ = null;
rginda9f5222b2012-03-05 11:53:28 -080034
rginda8ba33642011-12-14 12:31:31 -080035 // Two screen instances.
36 this.primaryScreen_ = new hterm.Screen();
37 this.alternateScreen_ = new hterm.Screen();
38
39 // The "current" screen.
40 this.screen_ = this.primaryScreen_;
41
rginda8ba33642011-12-14 12:31:31 -080042 // The local notion of the screen size. ScreenBuffers also have a size which
43 // indicates their present size. During size changes, the two may disagree.
44 // Also, the inactive screen's size is not altered until it is made the active
45 // screen.
46 this.screenSize = new hterm.Size(0, 0);
47
rginda8ba33642011-12-14 12:31:31 -080048 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080049 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080050 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
51 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080052 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
Raymes Khourye5d48982018-08-02 09:08:32 +100053 this.scrollPort_.subscribe('focus', this.onScrollportFocus_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070054 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080055
rginda87b86462011-12-14 13:48:03 -080056 // The div that contains this terminal.
57 this.div_ = null;
58
rgindac9bc5502012-01-18 11:48:44 -080059 // The document that contains the scrollPort. Defaulted to the global
60 // document here so that the terminal is functional even if it hasn't been
61 // inserted into a document yet, but re-set in decorate().
62 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080063
rginda8ba33642011-12-14 12:31:31 -080064 // The rows that have scrolled off screen and are no longer addressable.
65 this.scrollbackRows_ = [];
66
rgindac9bc5502012-01-18 11:48:44 -080067 // Saved tab stops.
68 this.tabStops_ = [];
69
David Benjamin66e954d2012-05-05 21:08:12 -040070 // Keep track of whether default tab stops have been erased; after a TBC
71 // clears all tab stops, defaults aren't restored on resize until a reset.
72 this.defaultTabStops = true;
73
rginda8ba33642011-12-14 12:31:31 -080074 // The VT's notion of the top and bottom rows. Used during some VT
75 // cursor positioning and scrolling commands.
76 this.vtScrollTop_ = null;
77 this.vtScrollBottom_ = null;
78
79 // The DIV element for the visible cursor.
80 this.cursorNode_ = null;
81
Robert Ginda830583c2013-08-07 13:20:46 -070082 // The current cursor shape of the terminal.
83 this.cursorShape_ = hterm.Terminal.cursorShape.BLOCK;
84
Robert Gindaea2183e2014-07-17 09:51:51 -070085 // Cursor blink on/off cycle in ms, overwritten by prefs once they're loaded.
86 this.cursorBlinkCycle_ = [100, 100];
87
88 // Pre-bound onCursorBlink_ handler, so we don't have to do this for each
89 // cursor on/off servicing.
90 this.myOnCursorBlink_ = this.onCursorBlink_.bind(this);
91
rginda9f5222b2012-03-05 11:53:28 -080092 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -070093 // each output and keystroke. They are initialized by the preference manager.
Robert Ginda8cb7d902013-06-20 14:37:18 -070094 this.backgroundColor_ = null;
95 this.foregroundColor_ = null;
Robert Ginda57f03b42012-09-13 11:02:48 -070096 this.scrollOnOutput_ = null;
97 this.scrollOnKeystroke_ = null;
Mike Frysinger3c9fa072017-07-13 10:21:13 -040098 this.scrollWheelArrowKeys_ = null;
rginda9f5222b2012-03-05 11:53:28 -080099
Robert Ginda6aec7eb2015-06-16 10:31:30 -0700100 // True if we should override mouse event reporting to allow local selection.
101 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -0800102
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400103 // Whether to auto hide the mouse cursor when typing.
104 this.setAutomaticMouseHiding();
105 // Timer to keep mouse visible while it's being used.
106 this.mouseHideDelay_ = null;
107
rgindaf0090c92012-02-10 14:58:52 -0800108 // Terminal bell sound.
109 this.bellAudio_ = this.document_.createElement('audio');
Mike Frysingerd826f1a2017-07-06 16:20:06 -0400110 this.bellAudio_.id = 'hterm:bell-audio';
rgindaf0090c92012-02-10 14:58:52 -0800111 this.bellAudio_.setAttribute('preload', 'auto');
112
Raymes Khoury3e44bc92018-05-17 10:54:23 +1000113 // The AccessibilityReader object for announcing command output.
114 this.accessibilityReader_ = null;
115
Mike Frysingercc114512017-09-11 21:39:17 -0400116 // The context menu object.
117 this.contextMenu = new hterm.ContextMenu();
118
Michael Kelly485ecd12014-06-09 11:41:56 -0400119 // All terminal bell notifications that have been generated (not necessarily
120 // shown).
121 this.bellNotificationList_ = [];
122
123 // Whether we have permission to display notifications.
124 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400125
rginda6d397402012-01-17 10:58:29 -0800126 // Cursor position and attributes saved with DECSC.
127 this.savedOptions_ = {};
128
rginda8ba33642011-12-14 12:31:31 -0800129 // The current mode bits for the terminal.
130 this.options_ = new hterm.Options();
131
132 // Timeouts we might need to clear.
133 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800134
135 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800136 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800137
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800138 this.saveCursorAndState(true);
139
Zhu Qunying30d40712017-03-14 16:27:00 -0700140 // The keyboard handler.
rgindafeaf3142012-01-31 15:14:20 -0800141 this.keyboard = new hterm.Keyboard(this);
142
rginda87b86462011-12-14 13:48:03 -0800143 // General IO interface that can be given to third parties without exposing
144 // the entire terminal object.
145 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800146
rgindad5613292012-06-19 15:40:37 -0700147 // True if mouse-click-drag should scroll the terminal.
148 this.enableMouseDragScroll = true;
149
Robert Ginda57f03b42012-09-13 11:02:48 -0700150 this.copyOnSelect = null;
Mike Frysinger847577f2017-05-23 23:25:57 -0400151 this.mouseRightClickPaste = null;
rginda4bba5e12012-06-20 16:15:30 -0700152 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700153
Zhu Qunying30d40712017-03-14 16:27:00 -0700154 // Whether to use the default window copy behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700155 this.useDefaultWindowCopy = false;
156
157 this.clearSelectionAfterCopy = true;
158
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400159 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800160 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700161
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400162 // Whether we allow images to be shown.
163 this.allowImagesInline = null;
164
Gabriel Holodake8a09be2017-10-10 01:07:11 -0400165 this.reportFocus = false;
166
Robert Ginda57f03b42012-09-13 11:02:48 -0700167 this.setProfile(opt_profileId || 'default',
Evan Jones5f9df812016-12-06 09:38:58 -0500168 function() { this.onTerminalReady(); }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800169};
170
171/**
Robert Ginda830583c2013-08-07 13:20:46 -0700172 * Possible cursor shapes.
173 */
174hterm.Terminal.cursorShape = {
175 BLOCK: 'BLOCK',
176 BEAM: 'BEAM',
177 UNDERLINE: 'UNDERLINE'
178};
179
180/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700181 * Clients should override this to be notified when the terminal is ready
182 * for use.
183 *
184 * The terminal initialization is asynchronous, and shouldn't be used before
185 * this method is called.
186 */
187hterm.Terminal.prototype.onTerminalReady = function() { };
188
189/**
rginda35c456b2012-02-09 17:29:05 -0800190 * Default tab with of 8 to match xterm.
191 */
192hterm.Terminal.prototype.tabWidth = 8;
193
194/**
rginda9f5222b2012-03-05 11:53:28 -0800195 * Select a preference profile.
196 *
197 * This will load the terminal preferences for the given profile name and
198 * associate subsequent preference changes with the new preference profile.
199 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500200 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800201 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700202 * @param {function} opt_callback Optional callback to invoke when the profile
203 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800204 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700205hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
206 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800207
Robert Ginda57f03b42012-09-13 11:02:48 -0700208 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800209
Robert Ginda57f03b42012-09-13 11:02:48 -0700210 if (this.prefs_)
211 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800212
Robert Ginda57f03b42012-09-13 11:02:48 -0700213 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
214 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800215 'alt-gr-mode': function(v) {
216 if (v == null) {
217 if (navigator.language.toLowerCase() == 'en-us') {
218 v = 'none';
219 } else {
220 v = 'right-alt';
221 }
222 } else if (typeof v == 'string') {
223 v = v.toLowerCase();
224 } else {
225 v = 'none';
226 }
227
228 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v))
229 v = 'none';
230
231 terminal.keyboard.altGrMode = v;
232 },
233
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700234 'alt-backspace-is-meta-backspace': function(v) {
235 terminal.keyboard.altBackspaceIsMetaBackspace = v;
236 },
237
Robert Ginda57f03b42012-09-13 11:02:48 -0700238 'alt-is-meta': function(v) {
239 terminal.keyboard.altIsMeta = v;
240 },
241
242 'alt-sends-what': function(v) {
243 if (!/^(escape|8-bit|browser-key)$/.test(v))
244 v = 'escape';
245
246 terminal.keyboard.altSendsWhat = v;
247 },
248
249 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800250 var ary = v.match(/^lib-resource:(\S+)/);
251 if (ary) {
252 terminal.bellAudio_.setAttribute('src',
253 lib.resource.getDataUrl(ary[1]));
254 } else {
255 terminal.bellAudio_.setAttribute('src', v);
256 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700257 },
258
Michael Kelly485ecd12014-06-09 11:41:56 -0400259 'desktop-notification-bell': function(v) {
260 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700261 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400262 Notification.permission === 'granted';
263 if (!terminal.desktopNotificationBell_) {
264 // Note: We don't call Notification.requestPermission here because
265 // Chrome requires the call be the result of a user action (such as an
266 // onclick handler), and pref listeners are run asynchronously.
267 //
268 // A way of working around this would be to display a dialog in the
269 // terminal with a "click-to-request-permission" button.
270 console.warn('desktop-notification-bell is true but we do not have ' +
271 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400272 }
273 } else {
274 terminal.desktopNotificationBell_ = false;
275 }
276 },
277
Robert Ginda57f03b42012-09-13 11:02:48 -0700278 'background-color': function(v) {
279 terminal.setBackgroundColor(v);
280 },
281
282 'background-image': function(v) {
283 terminal.scrollPort_.setBackgroundImage(v);
284 },
285
286 'background-size': function(v) {
287 terminal.scrollPort_.setBackgroundSize(v);
288 },
289
290 'background-position': function(v) {
291 terminal.scrollPort_.setBackgroundPosition(v);
292 },
293
294 'backspace-sends-backspace': function(v) {
295 terminal.keyboard.backspaceSendsBackspace = v;
296 },
297
Brad Town18654b62015-03-12 00:27:45 -0700298 'character-map-overrides': function(v) {
299 if (!(v == null || v instanceof Object)) {
300 console.warn('Preference character-map-modifications is not an ' +
301 'object: ' + v);
302 return;
303 }
304
Mike Frysinger095d4062017-06-14 00:29:48 -0700305 terminal.vt.characterMaps.reset();
306 terminal.vt.characterMaps.setOverrides(v);
Brad Town18654b62015-03-12 00:27:45 -0700307 },
308
Robert Ginda57f03b42012-09-13 11:02:48 -0700309 'cursor-blink': function(v) {
310 terminal.setCursorBlink(!!v);
311 },
312
Robert Gindaea2183e2014-07-17 09:51:51 -0700313 'cursor-blink-cycle': function(v) {
314 if (v instanceof Array &&
315 typeof v[0] == 'number' &&
316 typeof v[1] == 'number') {
317 terminal.cursorBlinkCycle_ = v;
318 } else if (typeof v == 'number') {
319 terminal.cursorBlinkCycle_ = [v, v];
320 } else {
321 // Fast blink indicates an error.
322 terminal.cursorBlinkCycle_ = [100, 100];
323 }
324 },
325
Robert Ginda57f03b42012-09-13 11:02:48 -0700326 'cursor-color': function(v) {
327 terminal.setCursorColor(v);
328 },
329
330 'color-palette-overrides': function(v) {
331 if (!(v == null || v instanceof Object || v instanceof Array)) {
332 console.warn('Preference color-palette-overrides is not an array or ' +
333 'object: ' + v);
334 return;
rginda9f5222b2012-03-05 11:53:28 -0800335 }
rginda9f5222b2012-03-05 11:53:28 -0800336
Robert Ginda57f03b42012-09-13 11:02:48 -0700337 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700338
Robert Ginda57f03b42012-09-13 11:02:48 -0700339 if (v) {
340 for (var key in v) {
341 var i = parseInt(key);
342 if (isNaN(i) || i < 0 || i > 255) {
343 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
344 continue;
345 }
346
347 if (v[i]) {
348 var rgb = lib.colors.normalizeCSS(v[i]);
349 if (rgb)
350 lib.colors.colorPalette[i] = rgb;
351 }
352 }
rginda30f20f62012-04-05 16:36:19 -0700353 }
rginda30f20f62012-04-05 16:36:19 -0700354
Evan Jones5f9df812016-12-06 09:38:58 -0500355 terminal.primaryScreen_.textAttributes.resetColorPalette();
Robert Ginda57f03b42012-09-13 11:02:48 -0700356 terminal.alternateScreen_.textAttributes.resetColorPalette();
357 },
rginda30f20f62012-04-05 16:36:19 -0700358
Robert Ginda57f03b42012-09-13 11:02:48 -0700359 'copy-on-select': function(v) {
360 terminal.copyOnSelect = !!v;
361 },
rginda9f5222b2012-03-05 11:53:28 -0800362
Rob Spies0bec09b2014-06-06 15:58:09 -0700363 'use-default-window-copy': function(v) {
364 terminal.useDefaultWindowCopy = !!v;
365 },
366
367 'clear-selection-after-copy': function(v) {
368 terminal.clearSelectionAfterCopy = !!v;
369 },
370
Robert Ginda7e5e9522014-03-14 12:23:58 -0700371 'ctrl-plus-minus-zero-zoom': function(v) {
372 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
373 },
374
Robert Gindafb5a3f92014-05-13 14:12:00 -0700375 'ctrl-c-copy': function(v) {
376 terminal.keyboard.ctrlCCopy = v;
377 },
378
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100379 'ctrl-v-paste': function(v) {
380 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700381 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100382 },
383
Masaya Suzuki273aa982014-05-31 07:25:55 +0900384 'east-asian-ambiguous-as-two-column': function(v) {
385 lib.wc.regardCjkAmbiguous = v;
386 },
387
Robert Ginda57f03b42012-09-13 11:02:48 -0700388 'enable-8-bit-control': function(v) {
389 terminal.vt.enable8BitControl = !!v;
390 },
rginda30f20f62012-04-05 16:36:19 -0700391
Robert Ginda57f03b42012-09-13 11:02:48 -0700392 'enable-bold': function(v) {
393 terminal.syncBoldSafeState();
394 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400395
Robert Ginda3e278d72014-03-25 13:18:51 -0700396 'enable-bold-as-bright': function(v) {
397 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
398 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
399 },
400
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400401 'enable-blink': function(v) {
Mike Frysinger261597c2017-12-28 01:14:21 -0500402 terminal.setTextBlink(!!v);
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400403 },
404
Robert Ginda57f03b42012-09-13 11:02:48 -0700405 'enable-clipboard-write': function(v) {
406 terminal.vt.enableClipboardWrite = !!v;
407 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400408
Robert Ginda3755e752013-05-31 13:34:09 -0700409 'enable-dec12': function(v) {
410 terminal.vt.enableDec12 = !!v;
411 },
412
Mike Frysinger38f267d2018-09-07 02:50:59 -0400413 'enable-csi-j-3': function(v) {
414 terminal.vt.enableCsiJ3 = !!v;
415 },
416
Robert Ginda57f03b42012-09-13 11:02:48 -0700417 'font-family': function(v) {
418 terminal.syncFontFamily();
419 },
rginda30f20f62012-04-05 16:36:19 -0700420
Robert Ginda57f03b42012-09-13 11:02:48 -0700421 'font-size': function(v) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500422 v = parseInt(v);
423 if (v <= 0) {
424 console.error(`Invalid font size: ${v}`);
425 return;
426 }
427
Robert Ginda57f03b42012-09-13 11:02:48 -0700428 terminal.setFontSize(v);
429 },
rginda9875d902012-08-20 16:21:57 -0700430
Robert Ginda57f03b42012-09-13 11:02:48 -0700431 'font-smoothing': function(v) {
432 terminal.syncFontFamily();
433 },
rgindade84e382012-04-20 15:39:31 -0700434
Robert Ginda57f03b42012-09-13 11:02:48 -0700435 'foreground-color': function(v) {
436 terminal.setForegroundColor(v);
437 },
rginda30f20f62012-04-05 16:36:19 -0700438
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400439 'hide-mouse-while-typing': function(v) {
440 terminal.setAutomaticMouseHiding(v);
441 },
442
Robert Ginda57f03b42012-09-13 11:02:48 -0700443 'home-keys-scroll': function(v) {
444 terminal.keyboard.homeKeysScroll = v;
445 },
rginda4bba5e12012-06-20 16:15:30 -0700446
Robert Gindaa8165692015-06-15 14:46:31 -0700447 'keybindings': function(v) {
448 terminal.keyboard.bindings.clear();
449
450 if (!v)
451 return;
452
453 if (!(v instanceof Object)) {
454 console.error('Error in keybindings preference: Expected object');
455 return;
456 }
457
458 try {
459 terminal.keyboard.bindings.addBindings(v);
460 } catch (ex) {
461 console.error('Error in keybindings preference: ' + ex);
462 }
463 },
464
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700465 'media-keys-are-fkeys': function(v) {
466 terminal.keyboard.mediaKeysAreFKeys = v;
467 },
468
Robert Ginda57f03b42012-09-13 11:02:48 -0700469 'meta-sends-escape': function(v) {
470 terminal.keyboard.metaSendsEscape = v;
471 },
rginda30f20f62012-04-05 16:36:19 -0700472
Mike Frysinger847577f2017-05-23 23:25:57 -0400473 'mouse-right-click-paste': function(v) {
474 terminal.mouseRightClickPaste = v;
475 },
476
Robert Ginda57f03b42012-09-13 11:02:48 -0700477 'mouse-paste-button': function(v) {
478 terminal.syncMousePasteButton();
479 },
rgindaa8ba17d2012-08-15 14:41:10 -0700480
Robert Gindae76aa9f2014-03-14 12:29:12 -0700481 'page-keys-scroll': function(v) {
482 terminal.keyboard.pageKeysScroll = v;
483 },
484
Robert Ginda40932892012-12-10 17:26:40 -0800485 'pass-alt-number': function(v) {
486 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800487 // Let Alt-1..9 pass to the browser (to control tab switching) on
488 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500489 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800490 }
491
492 terminal.passAltNumber = v;
493 },
494
495 'pass-ctrl-number': function(v) {
496 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800497 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
498 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500499 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800500 }
501
502 terminal.passCtrlNumber = v;
503 },
504
505 'pass-meta-number': function(v) {
506 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800507 // Let Meta-1..9 pass to the browser (to control tab switching) on
508 // OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500509 v = (hterm.os == 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800510 }
511
512 terminal.passMetaNumber = v;
513 },
514
Marius Schilder77857b32014-05-14 16:21:26 -0700515 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700516 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700517 },
518
Robert Ginda8cb7d902013-06-20 14:37:18 -0700519 'receive-encoding': function(v) {
520 if (!(/^(utf-8|raw)$/).test(v)) {
521 console.warn('Invalid value for "receive-encoding": ' + v);
522 v = 'utf-8';
523 }
524
525 terminal.vt.characterEncoding = v;
526 },
527
Robert Ginda57f03b42012-09-13 11:02:48 -0700528 'scroll-on-keystroke': function(v) {
529 terminal.scrollOnKeystroke_ = v;
530 },
rginda9f5222b2012-03-05 11:53:28 -0800531
Robert Ginda57f03b42012-09-13 11:02:48 -0700532 'scroll-on-output': function(v) {
533 terminal.scrollOnOutput_ = v;
534 },
rginda30f20f62012-04-05 16:36:19 -0700535
Robert Ginda57f03b42012-09-13 11:02:48 -0700536 'scrollbar-visible': function(v) {
537 terminal.setScrollbarVisible(v);
538 },
rginda9f5222b2012-03-05 11:53:28 -0800539
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400540 'scroll-wheel-may-send-arrow-keys': function(v) {
541 terminal.scrollWheelArrowKeys_ = v;
542 },
543
Rob Spies49039e52014-12-17 13:40:04 -0800544 'scroll-wheel-move-multiplier': function(v) {
545 terminal.setScrollWheelMoveMultipler(v);
546 },
547
Robert Ginda8cb7d902013-06-20 14:37:18 -0700548 'send-encoding': function(v) {
549 if (!(/^(utf-8|raw)$/).test(v)) {
550 console.warn('Invalid value for "send-encoding": ' + v);
551 v = 'utf-8';
552 }
553
554 terminal.keyboard.characterEncoding = v;
555 },
556
Robert Ginda57f03b42012-09-13 11:02:48 -0700557 'shift-insert-paste': function(v) {
558 terminal.keyboard.shiftInsertPaste = v;
559 },
rginda9f5222b2012-03-05 11:53:28 -0800560
Mike Frysingera7768922017-07-28 15:00:12 -0400561 'terminal-encoding': function(v) {
Mike Frysingera1371e12017-08-17 01:37:17 -0400562 terminal.vt.setEncoding(v);
Mike Frysingera7768922017-07-28 15:00:12 -0400563 },
564
Robert Gindae76aa9f2014-03-14 12:29:12 -0700565 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400566 terminal.scrollPort_.setUserCssUrl(v);
567 },
568
569 'user-css-text': function(v) {
570 terminal.scrollPort_.setUserCssText(v);
571 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400572
573 'word-break-match-left': function(v) {
574 terminal.primaryScreen_.wordBreakMatchLeft = v;
575 terminal.alternateScreen_.wordBreakMatchLeft = v;
576 },
577
578 'word-break-match-right': function(v) {
579 terminal.primaryScreen_.wordBreakMatchRight = v;
580 terminal.alternateScreen_.wordBreakMatchRight = v;
581 },
582
583 'word-break-match-middle': function(v) {
584 terminal.primaryScreen_.wordBreakMatchMiddle = v;
585 terminal.alternateScreen_.wordBreakMatchMiddle = v;
586 },
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400587
588 'allow-images-inline': function(v) {
589 terminal.allowImagesInline = v;
590 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700591 });
rginda30f20f62012-04-05 16:36:19 -0700592
Robert Ginda57f03b42012-09-13 11:02:48 -0700593 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800594 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700595
596 if (opt_callback)
597 opt_callback();
598 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800599};
600
Rob Spies56953412014-04-28 14:09:47 -0700601
602/**
603 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500604 *
605 * @return {hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700606 */
607hterm.Terminal.prototype.getPrefs = function() {
608 return this.prefs_;
609};
610
Robert Gindaa063b202014-07-21 11:08:25 -0700611/**
612 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500613 *
614 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700615 */
616hterm.Terminal.prototype.setBracketedPaste = function(state) {
617 this.options_.bracketedPaste = state;
618};
Rob Spies56953412014-04-28 14:09:47 -0700619
rginda8e92a692012-05-20 19:37:20 -0700620/**
621 * Set the color for the cursor.
622 *
623 * If you want this setting to persist, set it through prefs_, rather than
624 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500625 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500626 * @param {string=} color The color to set. If not defined, we reset to the
627 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700628 */
629hterm.Terminal.prototype.setCursorColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500630 if (color === undefined)
631 color = this.prefs_.get('cursor-color');
632
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400633 this.setCssVar('cursor-color', color);
rginda8e92a692012-05-20 19:37:20 -0700634};
635
636/**
637 * Return the current cursor color as a string.
Evan Jones2600d4f2016-12-06 09:29:36 -0500638 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700639 */
640hterm.Terminal.prototype.getCursorColor = function() {
Mike Frysinger2fd079a2018-09-02 01:46:12 -0400641 return this.getCssVar('cursor-color');
rginda8e92a692012-05-20 19:37:20 -0700642};
643
644/**
rgindad5613292012-06-19 15:40:37 -0700645 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500646 *
647 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700648 */
649hterm.Terminal.prototype.setSelectionEnabled = function(state) {
650 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700651};
652
653/**
rginda8e92a692012-05-20 19:37:20 -0700654 * Set the background color.
655 *
656 * If you want this setting to persist, set it through prefs_, rather than
657 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500658 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500659 * @param {string=} color The color to set. If not defined, we reset to the
660 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700661 */
662hterm.Terminal.prototype.setBackgroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500663 if (color === undefined)
664 color = this.prefs_.get('background-color');
665
rgindacbbd7482012-06-13 15:06:16 -0700666 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700667 this.primaryScreen_.textAttributes.setDefaults(
668 this.foregroundColor_, this.backgroundColor_);
669 this.alternateScreen_.textAttributes.setDefaults(
670 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700671 this.scrollPort_.setBackgroundColor(color);
672};
673
rginda9f5222b2012-03-05 11:53:28 -0800674/**
675 * Return the current terminal background color.
676 *
677 * Intended for use by other classes, so we don't have to expose the entire
678 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500679 *
680 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800681 */
682hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700683 return this.backgroundColor_;
684};
685
686/**
687 * Set the foreground color.
688 *
689 * If you want this setting to persist, set it through prefs_, rather than
690 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500691 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500692 * @param {string=} color The color to set. If not defined, we reset to the
693 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700694 */
695hterm.Terminal.prototype.setForegroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500696 if (color === undefined)
697 color = this.prefs_.get('foreground-color');
698
rgindacbbd7482012-06-13 15:06:16 -0700699 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700700 this.primaryScreen_.textAttributes.setDefaults(
701 this.foregroundColor_, this.backgroundColor_);
702 this.alternateScreen_.textAttributes.setDefaults(
703 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700704 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800705};
706
707/**
708 * Return the current terminal foreground color.
709 *
710 * Intended for use by other classes, so we don't have to expose the entire
711 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500712 *
713 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800714 */
715hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700716 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800717};
718
719/**
rginda87b86462011-12-14 13:48:03 -0800720 * Create a new instance of a terminal command and run it with a given
721 * argument string.
722 *
723 * @param {function} commandClass The constructor for a terminal command.
724 * @param {string} argString The argument string to pass to the command.
725 */
726hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700727 var environment = this.prefs_.get('environment');
728 if (typeof environment != 'object' || environment == null)
729 environment = {};
730
rginda87b86462011-12-14 13:48:03 -0800731 var self = this;
732 this.command = new commandClass(
733 { argString: argString || '',
734 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700735 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800736 onExit: function(code) {
737 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800738 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700739 if (self.prefs_.get('close-on-exit'))
740 window.close();
rginda87b86462011-12-14 13:48:03 -0800741 }
742 });
743
rgindafeaf3142012-01-31 15:14:20 -0800744 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800745 this.command.run();
746};
747
748/**
rgindafeaf3142012-01-31 15:14:20 -0800749 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500750 *
751 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800752 */
753hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700754 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800755};
756
757/**
758 * Install the keyboard handler for this terminal.
759 *
760 * This will prevent the browser from seeing any keystrokes sent to the
761 * terminal.
762 */
763hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700764 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400765};
rgindafeaf3142012-01-31 15:14:20 -0800766
767/**
768 * Uninstall the keyboard handler for this terminal.
769 */
770hterm.Terminal.prototype.uninstallKeyboard = function() {
771 this.keyboard.installKeyboard(null);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400772};
rgindafeaf3142012-01-31 15:14:20 -0800773
774/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400775 * Set a CSS variable.
776 *
777 * Normally this is used to set variables in the hterm namespace.
778 *
779 * @param {string} name The variable to set.
780 * @param {string} value The value to assign to the variable.
781 * @param {string?} opt_prefix The variable namespace/prefix to use.
782 */
783hterm.Terminal.prototype.setCssVar = function(name, value,
784 opt_prefix='--hterm-') {
785 this.document_.documentElement.style.setProperty(
786 `${opt_prefix}${name}`, value);
787};
788
789/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500790 * Get a CSS variable.
791 *
792 * Normally this is used to get variables in the hterm namespace.
793 *
794 * @param {string} name The variable to read.
795 * @param {string?} opt_prefix The variable namespace/prefix to use.
796 * @return {string} The current setting for this variable.
797 */
798hterm.Terminal.prototype.getCssVar = function(name, opt_prefix='--hterm-') {
799 return this.document_.documentElement.style.getPropertyValue(
800 `${opt_prefix}${name}`);
801};
802
803/**
rginda35c456b2012-02-09 17:29:05 -0800804 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800805 *
806 * Call setFontSize(0) to reset to the default font size.
807 *
808 * This function does not modify the font-size preference.
809 *
810 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800811 */
812hterm.Terminal.prototype.setFontSize = function(px) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500813 if (px <= 0)
rginda9f5222b2012-03-05 11:53:28 -0800814 px = this.prefs_.get('font-size');
815
rginda35c456b2012-02-09 17:29:05 -0800816 this.scrollPort_.setFontSize(px);
Mike Frysingercce97c42017-08-05 01:11:22 -0400817 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
818 this.setCssVar('charsize-height',
819 this.scrollPort_.characterSize.height + 'px');
rginda35c456b2012-02-09 17:29:05 -0800820};
821
822/**
823 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500824 *
825 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800826 */
827hterm.Terminal.prototype.getFontSize = function() {
828 return this.scrollPort_.getFontSize();
829};
830
831/**
rginda8e92a692012-05-20 19:37:20 -0700832 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500833 *
834 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700835 */
836hterm.Terminal.prototype.getFontFamily = function() {
837 return this.scrollPort_.getFontFamily();
838};
839
840/**
rginda35c456b2012-02-09 17:29:05 -0800841 * Set the CSS "font-family" for this terminal.
842 */
rginda9f5222b2012-03-05 11:53:28 -0800843hterm.Terminal.prototype.syncFontFamily = function() {
844 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
845 this.prefs_.get('font-smoothing'));
846 this.syncBoldSafeState();
847};
848
rginda4bba5e12012-06-20 16:15:30 -0700849/**
850 * Set this.mousePasteButton based on the mouse-paste-button pref,
851 * autodetecting if necessary.
852 */
853hterm.Terminal.prototype.syncMousePasteButton = function() {
854 var button = this.prefs_.get('mouse-paste-button');
855 if (typeof button == 'number') {
856 this.mousePasteButton = button;
857 return;
858 }
859
Mike Frysingeree81a002017-12-12 16:14:53 -0500860 if (hterm.os != 'linux') {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400861 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700862 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400863 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700864 }
865};
866
867/**
868 * Enable or disable bold based on the enable-bold pref, autodetecting if
869 * necessary.
870 */
rginda9f5222b2012-03-05 11:53:28 -0800871hterm.Terminal.prototype.syncBoldSafeState = function() {
872 var enableBold = this.prefs_.get('enable-bold');
873 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700874 this.primaryScreen_.textAttributes.enableBold = enableBold;
875 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800876 return;
877 }
878
rgindaf7521392012-02-28 17:20:34 -0800879 var normalSize = this.scrollPort_.measureCharacterSize();
880 var boldSize = this.scrollPort_.measureCharacterSize('bold');
881
882 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800883 if (!isBoldSafe) {
884 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700885 'from normal. Font family is: ' +
886 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800887 }
rginda9f5222b2012-03-05 11:53:28 -0800888
Robert Gindaed016262012-10-26 16:27:09 -0700889 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
890 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800891};
892
893/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500894 * Control text blinking behavior.
895 *
896 * @param {boolean=} state Whether to enable support for blinking text.
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400897 */
Mike Frysinger261597c2017-12-28 01:14:21 -0500898hterm.Terminal.prototype.setTextBlink = function(state) {
899 if (state === undefined)
900 state = this.prefs_.get('enable-blink');
901 this.setCssVar('blink-node-duration', state ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400902};
903
904/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400905 * Set the mouse cursor style based on the current terminal mode.
906 */
907hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -0400908 this.setCssVar('mouse-cursor-style',
909 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
910 'var(--hterm-mouse-cursor-text)' :
911 'var(--hterm-mouse-cursor-pointer)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400912};
913
914/**
rginda87b86462011-12-14 13:48:03 -0800915 * Return a copy of the current cursor position.
916 *
917 * @return {hterm.RowCol} The RowCol object representing the current position.
918 */
919hterm.Terminal.prototype.saveCursor = function() {
920 return this.screen_.cursorPosition.clone();
921};
922
Evan Jones2600d4f2016-12-06 09:29:36 -0500923/**
924 * Return the current text attributes.
925 *
926 * @return {string}
927 */
rgindaa19afe22012-01-25 15:40:22 -0800928hterm.Terminal.prototype.getTextAttributes = function() {
929 return this.screen_.textAttributes;
930};
931
Evan Jones2600d4f2016-12-06 09:29:36 -0500932/**
933 * Set the text attributes.
934 *
935 * @param {string} textAttributes The attributes to set.
936 */
rginda1a09aa02012-06-18 21:11:25 -0700937hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
938 this.screen_.textAttributes = textAttributes;
939};
940
rginda87b86462011-12-14 13:48:03 -0800941/**
rgindaf522ce02012-04-17 17:49:17 -0700942 * Return the current browser zoom factor applied to the terminal.
943 *
944 * @return {number} The current browser zoom factor.
945 */
946hterm.Terminal.prototype.getZoomFactor = function() {
947 return this.scrollPort_.characterSize.zoomFactor;
948};
949
950/**
rginda9846e2f2012-01-27 13:53:33 -0800951 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -0500952 *
953 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -0800954 */
955hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800956 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800957};
958
959/**
rginda87b86462011-12-14 13:48:03 -0800960 * Restore a previously saved cursor position.
961 *
962 * @param {hterm.RowCol} cursor The position to restore.
963 */
964hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700965 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
966 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800967 this.screen_.setCursorPosition(row, column);
968 if (cursor.column > column ||
969 cursor.column == column && cursor.overflow) {
970 this.screen_.cursorPosition.overflow = true;
971 }
rginda87b86462011-12-14 13:48:03 -0800972};
973
974/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400975 * Clear the cursor's overflow flag.
976 */
977hterm.Terminal.prototype.clearCursorOverflow = function() {
978 this.screen_.cursorPosition.overflow = false;
979};
980
981/**
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800982 * Save the current cursor state to the corresponding screens.
983 *
984 * See the hterm.Screen.CursorState class for more details.
985 *
986 * @param {boolean=} both If true, update both screens, else only update the
987 * current screen.
988 */
989hterm.Terminal.prototype.saveCursorAndState = function(both) {
990 if (both) {
991 this.primaryScreen_.saveCursorAndState(this.vt);
992 this.alternateScreen_.saveCursorAndState(this.vt);
993 } else
994 this.screen_.saveCursorAndState(this.vt);
995};
996
997/**
998 * Restore the saved cursor state in the corresponding screens.
999 *
1000 * See the hterm.Screen.CursorState class for more details.
1001 *
1002 * @param {boolean=} both If true, update both screens, else only update the
1003 * current screen.
1004 */
1005hterm.Terminal.prototype.restoreCursorAndState = function(both) {
1006 if (both) {
1007 this.primaryScreen_.restoreCursorAndState(this.vt);
1008 this.alternateScreen_.restoreCursorAndState(this.vt);
1009 } else
1010 this.screen_.restoreCursorAndState(this.vt);
1011};
1012
1013/**
Robert Ginda830583c2013-08-07 13:20:46 -07001014 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001015 *
1016 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -07001017 */
1018hterm.Terminal.prototype.setCursorShape = function(shape) {
1019 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001020 this.restyleCursor_();
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001021};
Robert Ginda830583c2013-08-07 13:20:46 -07001022
1023/**
1024 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001025 *
1026 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -07001027 */
1028hterm.Terminal.prototype.getCursorShape = function() {
1029 return this.cursorShape_;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001030};
Robert Ginda830583c2013-08-07 13:20:46 -07001031
1032/**
rginda87b86462011-12-14 13:48:03 -08001033 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001034 *
1035 * @param {number} columnCount
rginda87b86462011-12-14 13:48:03 -08001036 */
1037hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -08001038 if (columnCount == null) {
1039 this.div_.style.width = '100%';
1040 return;
1041 }
1042
Robert Ginda26806d12014-07-24 13:44:07 -07001043 this.div_.style.width = Math.ceil(
1044 this.scrollPort_.characterSize.width *
1045 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001046 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -08001047 this.scheduleSyncCursorPosition_();
1048};
rginda87b86462011-12-14 13:48:03 -08001049
rgindac9bc5502012-01-18 11:48:44 -08001050/**
rginda35c456b2012-02-09 17:29:05 -08001051 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001052 *
1053 * @param {number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -08001054 */
1055hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -08001056 if (rowCount == null) {
1057 this.div_.style.height = '100%';
1058 return;
1059 }
1060
rginda35c456b2012-02-09 17:29:05 -08001061 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -07001062 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -08001063 this.realizeSize_(this.screenSize.width, rowCount);
1064 this.scheduleSyncCursorPosition_();
1065};
1066
1067/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001068 * Deal with terminal size changes.
1069 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001070 * @param {number} columnCount The number of columns.
1071 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001072 */
1073hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
1074 if (columnCount != this.screenSize.width)
1075 this.realizeWidth_(columnCount);
1076
1077 if (rowCount != this.screenSize.height)
1078 this.realizeHeight_(rowCount);
1079
1080 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -07001081 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001082};
1083
1084/**
rgindac9bc5502012-01-18 11:48:44 -08001085 * Deal with terminal width changes.
1086 *
1087 * This function does what needs to be done when the terminal width changes
1088 * out from under us. It happens here rather than in onResize_() because this
1089 * code may need to run synchronously to handle programmatic changes of
1090 * terminal width.
1091 *
1092 * Relying on the browser to send us an async resize event means we may not be
1093 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001094 *
1095 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001096 */
1097hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001098 if (columnCount <= 0)
1099 throw new Error('Attempt to realize bad width: ' + columnCount);
1100
rgindac9bc5502012-01-18 11:48:44 -08001101 var deltaColumns = columnCount - this.screen_.getWidth();
1102
rginda87b86462011-12-14 13:48:03 -08001103 this.screenSize.width = columnCount;
1104 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001105
1106 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -04001107 if (this.defaultTabStops)
1108 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -08001109 } else {
1110 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -04001111 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -08001112 break;
1113
1114 this.tabStops_.pop();
1115 }
1116 }
1117
1118 this.screen_.setColumnCount(this.screenSize.width);
1119};
1120
1121/**
1122 * Deal with terminal height changes.
1123 *
1124 * This function does what needs to be done when the terminal height changes
1125 * out from under us. It happens here rather than in onResize_() because this
1126 * code may need to run synchronously to handle programmatic changes of
1127 * terminal height.
1128 *
1129 * Relying on the browser to send us an async resize event means we may not be
1130 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001131 *
1132 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001133 */
1134hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001135 if (rowCount <= 0)
1136 throw new Error('Attempt to realize bad height: ' + rowCount);
1137
rgindac9bc5502012-01-18 11:48:44 -08001138 var deltaRows = rowCount - this.screen_.getHeight();
1139
1140 this.screenSize.height = rowCount;
1141
1142 var cursor = this.saveCursor();
1143
1144 if (deltaRows < 0) {
1145 // Screen got smaller.
1146 deltaRows *= -1;
1147 while (deltaRows) {
1148 var lastRow = this.getRowCount() - 1;
1149 if (lastRow - this.scrollbackRows_.length == cursor.row)
1150 break;
1151
1152 if (this.getRowText(lastRow))
1153 break;
1154
1155 this.screen_.popRow();
1156 deltaRows--;
1157 }
1158
1159 var ary = this.screen_.shiftRows(deltaRows);
1160 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1161
1162 // We just removed rows from the top of the screen, we need to update
1163 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001164 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001165 } else if (deltaRows > 0) {
1166 // Screen got larger.
1167
1168 if (deltaRows <= this.scrollbackRows_.length) {
1169 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1170 var rows = this.scrollbackRows_.splice(
1171 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1172 this.screen_.unshiftRows(rows);
1173 deltaRows -= scrollbackCount;
1174 cursor.row += scrollbackCount;
1175 }
1176
1177 if (deltaRows)
1178 this.appendRows_(deltaRows);
1179 }
1180
rginda35c456b2012-02-09 17:29:05 -08001181 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001182 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001183};
1184
1185/**
1186 * Scroll the terminal to the top of the scrollback buffer.
1187 */
1188hterm.Terminal.prototype.scrollHome = function() {
1189 this.scrollPort_.scrollRowToTop(0);
1190};
1191
1192/**
1193 * Scroll the terminal to the end.
1194 */
1195hterm.Terminal.prototype.scrollEnd = function() {
1196 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1197};
1198
1199/**
1200 * Scroll the terminal one page up (minus one line) relative to the current
1201 * position.
1202 */
1203hterm.Terminal.prototype.scrollPageUp = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001204 this.scrollPort_.scrollPageUp();
rginda87b86462011-12-14 13:48:03 -08001205};
1206
1207/**
1208 * Scroll the terminal one page down (minus one line) relative to the current
1209 * position.
1210 */
1211hterm.Terminal.prototype.scrollPageDown = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001212 this.scrollPort_.scrollPageDown();
rginda8ba33642011-12-14 12:31:31 -08001213};
1214
rgindac9bc5502012-01-18 11:48:44 -08001215/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001216 * Scroll the terminal one line up relative to the current position.
1217 */
1218hterm.Terminal.prototype.scrollLineUp = function() {
1219 var i = this.scrollPort_.getTopRowIndex();
1220 this.scrollPort_.scrollRowToTop(i - 1);
1221};
1222
1223/**
1224 * Scroll the terminal one line down relative to the current position.
1225 */
1226hterm.Terminal.prototype.scrollLineDown = function() {
1227 var i = this.scrollPort_.getTopRowIndex();
1228 this.scrollPort_.scrollRowToTop(i + 1);
1229};
1230
1231/**
Robert Ginda40932892012-12-10 17:26:40 -08001232 * Clear primary screen, secondary screen, and the scrollback buffer.
1233 */
1234hterm.Terminal.prototype.wipeContents = function() {
Mike Frysinger9c482b82018-09-07 02:49:36 -04001235 this.clearHome(this.primaryScreen_);
1236 this.clearHome(this.alternateScreen_);
1237
1238 this.clearScrollback();
1239};
1240
1241/**
1242 * Clear scrollback buffer.
1243 */
1244hterm.Terminal.prototype.clearScrollback = function() {
1245 // Move to the end of the buffer in case the screen was scrolled back.
1246 // We're going to throw it away which would leave the display invalid.
1247 this.scrollEnd();
1248
Robert Ginda40932892012-12-10 17:26:40 -08001249 this.scrollbackRows_.length = 0;
1250 this.scrollPort_.resetCache();
1251
Mike Frysinger9c482b82018-09-07 02:49:36 -04001252 [this.primaryScreen_, this.alternateScreen_].forEach((screen) => {
1253 const bottom = screen.getHeight();
1254 this.renumberRows_(0, bottom, screen);
1255 });
Robert Ginda40932892012-12-10 17:26:40 -08001256
1257 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001258 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001259};
1260
1261/**
rgindac9bc5502012-01-18 11:48:44 -08001262 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001263 *
1264 * Perform a full reset to the default values listed in
1265 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001266 */
rginda87b86462011-12-14 13:48:03 -08001267hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001268 this.vt.reset();
1269
rgindac9bc5502012-01-18 11:48:44 -08001270 this.clearAllTabStops();
1271 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001272
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001273 const resetScreen = (screen) => {
1274 // We want to make sure to reset the attributes before we clear the screen.
1275 // The attributes might be used to initialize default/empty rows.
1276 screen.textAttributes.reset();
1277 screen.textAttributes.resetColorPalette();
1278 this.clearHome(screen);
1279 screen.saveCursorAndState(this.vt);
1280 };
1281 resetScreen(this.primaryScreen_);
1282 resetScreen(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001283
Mike Frysinger84301d02017-11-29 13:28:46 -08001284 // Reset terminal options to their default values.
1285 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001286 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1287
Mike Frysinger84301d02017-11-29 13:28:46 -08001288 this.setVTScrollRegion(null, null);
1289
1290 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001291};
1292
rgindac9bc5502012-01-18 11:48:44 -08001293/**
1294 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001295 *
1296 * Perform a soft reset to the default values listed in
1297 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001298 */
rginda0f5c0292012-01-13 11:00:13 -08001299hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001300 this.vt.reset();
1301
rgindab8bc8932012-04-27 12:45:03 -07001302 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001303 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001304
Brad Townb62dfdc2015-03-16 19:07:15 -07001305 // We show the cursor on soft reset but do not alter the blink state.
1306 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1307
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001308 const resetScreen = (screen) => {
1309 // Xterm also resets the color palette on soft reset, even though it doesn't
1310 // seem to be documented anywhere.
1311 screen.textAttributes.reset();
1312 screen.textAttributes.resetColorPalette();
1313 screen.saveCursorAndState(this.vt);
1314 };
1315 resetScreen(this.primaryScreen_);
1316 resetScreen(this.alternateScreen_);
rgindaf522ce02012-04-17 17:49:17 -07001317
rgindab8bc8932012-04-27 12:45:03 -07001318 // The xterm man page explicitly says this will happen on soft reset.
1319 this.setVTScrollRegion(null, null);
1320
1321 // Xterm also shows the cursor on soft reset, but does not alter the blink
1322 // state.
rgindaa19afe22012-01-25 15:40:22 -08001323 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001324};
1325
rgindac9bc5502012-01-18 11:48:44 -08001326/**
1327 * Move the cursor forward to the next tab stop, or to the last column
1328 * if no more tab stops are set.
1329 */
1330hterm.Terminal.prototype.forwardTabStop = function() {
1331 var column = this.screen_.cursorPosition.column;
1332
1333 for (var i = 0; i < this.tabStops_.length; i++) {
1334 if (this.tabStops_[i] > column) {
1335 this.setCursorColumn(this.tabStops_[i]);
1336 return;
1337 }
1338 }
1339
David Benjamin66e954d2012-05-05 21:08:12 -04001340 // xterm does not clear the overflow flag on HT or CHT.
1341 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001342 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001343 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001344};
1345
rgindac9bc5502012-01-18 11:48:44 -08001346/**
1347 * Move the cursor backward to the previous tab stop, or to the first column
1348 * if no previous tab stops are set.
1349 */
1350hterm.Terminal.prototype.backwardTabStop = function() {
1351 var column = this.screen_.cursorPosition.column;
1352
1353 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1354 if (this.tabStops_[i] < column) {
1355 this.setCursorColumn(this.tabStops_[i]);
1356 return;
1357 }
1358 }
1359
1360 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001361};
1362
rgindac9bc5502012-01-18 11:48:44 -08001363/**
1364 * Set a tab stop at the given column.
1365 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001366 * @param {integer} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001367 */
1368hterm.Terminal.prototype.setTabStop = function(column) {
1369 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1370 if (this.tabStops_[i] == column)
1371 return;
1372
1373 if (this.tabStops_[i] < column) {
1374 this.tabStops_.splice(i + 1, 0, column);
1375 return;
1376 }
1377 }
1378
1379 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001380};
1381
rgindac9bc5502012-01-18 11:48:44 -08001382/**
1383 * Clear the tab stop at the current cursor position.
1384 *
1385 * No effect if there is no tab stop at the current cursor position.
1386 */
1387hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1388 var column = this.screen_.cursorPosition.column;
1389
1390 var i = this.tabStops_.indexOf(column);
1391 if (i == -1)
1392 return;
1393
1394 this.tabStops_.splice(i, 1);
1395};
1396
1397/**
1398 * Clear all tab stops.
1399 */
1400hterm.Terminal.prototype.clearAllTabStops = function() {
1401 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001402 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001403};
1404
1405/**
1406 * Set up the default tab stops, starting from a given column.
1407 *
1408 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001409 * from the specified column, or 0 if no column is provided. It also flags
1410 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001411 *
1412 * This does not clear the existing tab stops first, use clearAllTabStops
1413 * for that.
1414 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001415 * @param {integer} opt_start Optional starting zero based starting column, useful
rgindac9bc5502012-01-18 11:48:44 -08001416 * for filling out missing tab stops when the terminal is resized.
1417 */
1418hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1419 var start = opt_start || 0;
1420 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001421 // Round start up to a default tab stop.
1422 start = start - 1 - ((start - 1) % w) + w;
1423 for (var i = start; i < this.screenSize.width; i += w) {
1424 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001425 }
David Benjamin66e954d2012-05-05 21:08:12 -04001426
1427 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001428};
1429
rginda6d397402012-01-17 10:58:29 -08001430/**
rginda8ba33642011-12-14 12:31:31 -08001431 * Interpret a sequence of characters.
1432 *
1433 * Incomplete escape sequences are buffered until the next call.
1434 *
1435 * @param {string} str Sequence of characters to interpret or pass through.
1436 */
1437hterm.Terminal.prototype.interpret = function(str) {
rginda8ba33642011-12-14 12:31:31 -08001438 this.scheduleSyncCursorPosition_();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001439 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001440};
1441
1442/**
1443 * Take over the given DIV for use as the terminal display.
1444 *
1445 * @param {HTMLDivElement} div The div to use as the terminal display.
1446 */
1447hterm.Terminal.prototype.decorate = function(div) {
Mike Frysinger5768a9d2017-12-26 12:57:44 -05001448 const charset = div.ownerDocument.characterSet.toLowerCase();
1449 if (charset != 'utf-8') {
1450 console.warn(`Document encoding should be set to utf-8, not "${charset}";` +
1451 ` Add <meta charset='utf-8'/> to your HTML <head> to fix.`);
1452 }
1453
rginda87b86462011-12-14 13:48:03 -08001454 this.div_ = div;
1455
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001456 this.accessibilityReader_ = new hterm.AccessibilityReader(div);
1457
rginda8ba33642011-12-14 12:31:31 -08001458 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001459 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001460 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1461 this.scrollPort_.setBackgroundPosition(
1462 this.prefs_.get('background-position'));
Mike Frysinger08bad432017-04-24 00:50:54 -04001463 this.scrollPort_.setUserCssUrl(this.prefs_.get('user-css'));
1464 this.scrollPort_.setUserCssText(this.prefs_.get('user-css-text'));
Raymes Khoury177aec72018-06-26 10:58:53 +10001465 this.scrollPort_.setAccessibilityReader(this.accessibilityReader_);
rginda30f20f62012-04-05 16:36:19 -07001466
rginda0918b652012-04-04 11:26:24 -07001467 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001468
rginda9f5222b2012-03-05 11:53:28 -08001469 this.setFontSize(this.prefs_.get('font-size'));
1470 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001471
David Reveman8f552492012-03-28 12:18:41 -04001472 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001473 this.setScrollWheelMoveMultipler(
1474 this.prefs_.get('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001475
rginda8ba33642011-12-14 12:31:31 -08001476 this.document_ = this.scrollPort_.getDocument();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001477 this.accessibilityReader_.decorate(this.document_);
rginda8ba33642011-12-14 12:31:31 -08001478
Evan Jones5f9df812016-12-06 09:38:58 -05001479 this.document_.body.oncontextmenu = function() { return false; };
Mike Frysingercc114512017-09-11 21:39:17 -04001480 this.contextMenu.setDocument(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07001481
1482 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001483 var screenNode = this.scrollPort_.getScreenNode();
1484 screenNode.addEventListener('mousedown', onMouse);
1485 screenNode.addEventListener('mouseup', onMouse);
1486 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001487 this.scrollPort_.onScrollWheel = onMouse;
1488
Mike Frysinger02ded6d2018-06-21 14:25:20 -04001489 screenNode.addEventListener('keydown', this.onKeyboardActivity_.bind(this));
1490
Toni Barzic0bfa8922013-11-22 11:18:35 -08001491 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001492 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001493 // Listen for mousedown events on the screenNode as in FF the focus
1494 // events don't bubble.
1495 screenNode.addEventListener('mousedown', function() {
1496 setTimeout(this.onFocusChange_.bind(this, true));
1497 }.bind(this));
1498
Toni Barzic0bfa8922013-11-22 11:18:35 -08001499 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001500 'blur', this.onFocusChange_.bind(this, false));
1501
1502 var style = this.document_.createElement('style');
1503 style.textContent =
1504 ('.cursor-node[focus="false"] {' +
1505 ' box-sizing: border-box;' +
1506 ' background-color: transparent !important;' +
1507 ' border-width: 2px;' +
1508 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001509 '}' +
Mike Frysingercc114512017-09-11 21:39:17 -04001510 'menu {' +
1511 ' margin: 0;' +
1512 ' padding: 0;' +
1513 ' cursor: var(--hterm-mouse-cursor-pointer);' +
1514 '}' +
1515 'menuitem {' +
1516 ' white-space: nowrap;' +
1517 ' border-bottom: 1px dashed;' +
1518 ' display: block;' +
1519 ' padding: 0.3em 0.3em 0 0.3em;' +
1520 '}' +
1521 'menuitem.separator {' +
1522 ' border-bottom: none;' +
1523 ' height: 0.5em;' +
1524 ' padding: 0;' +
1525 '}' +
1526 'menuitem:hover {' +
1527 ' color: var(--hterm-cursor-color);' +
1528 '}' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001529 '.wc-node {' +
1530 ' display: inline-block;' +
1531 ' text-align: center;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001532 ' width: calc(var(--hterm-charsize-width) * 2);' +
Mike Frysinger4036f6e2017-05-31 14:02:55 -04001533 ' line-height: var(--hterm-charsize-height);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001534 '}' +
1535 ':root {' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001536 ' --hterm-charsize-width: ' + this.scrollPort_.characterSize.width + 'px;' +
1537 ' --hterm-charsize-height: ' + this.scrollPort_.characterSize.height + 'px;' +
Mike Frysingera27c0502017-08-23 22:37:10 -04001538 // Default position hides the cursor for when the window is initializing.
1539 ' --hterm-cursor-offset-col: -1;' +
1540 ' --hterm-cursor-offset-row: -1;' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001541 ' --hterm-blink-node-duration: 0.7s;' +
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001542 ' --hterm-mouse-cursor-text: text;' +
1543 ' --hterm-mouse-cursor-pointer: default;' +
1544 ' --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001545 '}' +
Mike Frysingerd3907202017-10-23 01:23:50 -04001546 '.uri-node:hover {' +
1547 ' text-decoration: underline;' +
Mike Frysingerb74a6472018-06-22 13:37:08 -04001548 ' cursor: var(--hterm-mouse-cursor-pointer), pointer;' +
Mike Frysingerd3907202017-10-23 01:23:50 -04001549 '}' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001550 '@keyframes blink {' +
1551 ' from { opacity: 1.0; }' +
1552 ' to { opacity: 0.0; }' +
1553 '}' +
1554 '.blink-node {' +
1555 ' animation-name: blink;' +
1556 ' animation-duration: var(--hterm-blink-node-duration);' +
1557 ' animation-iteration-count: infinite;' +
1558 ' animation-timing-function: ease-in-out;' +
1559 ' animation-direction: alternate;' +
rginda8e92a692012-05-20 19:37:20 -07001560 '}');
Mike Frysingerb74a6472018-06-22 13:37:08 -04001561 // Insert this stock style as the first node so that any user styles will
1562 // override w/out having to use !important everywhere. The rules above mix
1563 // runtime variables with default ones designed to be overridden by the user,
1564 // but we can wait for a concrete case from the users to determine the best
1565 // way to split the sheet up to before & after the user-css settings.
1566 this.document_.head.insertBefore(style, this.document_.head.firstChild);
rginda8e92a692012-05-20 19:37:20 -07001567
rginda8ba33642011-12-14 12:31:31 -08001568 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001569 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001570 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001571 this.cursorNode_.style.cssText =
1572 ('position: absolute;' +
Mike Frysinger44c32202017-08-05 01:13:09 -04001573 'left: calc(var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));' +
1574 'top: calc(var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));' +
Mike Frysingerd60f4c22018-03-15 21:25:30 -07001575 'display: ' + (this.options_.cursorVisible ? '' : 'none') + ';' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001576 'width: var(--hterm-charsize-width);' +
1577 'height: var(--hterm-charsize-height);' +
Mike Frysinger2fd079a2018-09-02 01:46:12 -04001578 'background-color: var(--hterm-cursor-color);' +
1579 'border-color: var(--hterm-cursor-color);' +
Rob Spies06533ba2014-04-24 11:20:37 -07001580 '-webkit-transition: opacity, background-color 100ms linear;' +
1581 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001582
Mike Frysingerf02a2cb2017-12-21 00:34:03 -05001583 this.setCursorColor();
Robert Gindafb1be6a2013-12-11 11:56:22 -08001584 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1585 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001586
rginda8ba33642011-12-14 12:31:31 -08001587 this.document_.body.appendChild(this.cursorNode_);
1588
rgindad5613292012-06-19 15:40:37 -07001589 // When 'enableMouseDragScroll' is off we reposition this element directly
1590 // under the mouse cursor after a click. This makes Chrome associate
1591 // subsequent mousemove events with the scroll-blocker. Since the
1592 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1593 // events do not cause the scrollport to scroll.
1594 //
1595 // It's a hack, but it's the cleanest way I could find.
1596 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001597 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
Raymes Khoury6dce2f82018-04-12 15:38:58 +10001598 this.scrollBlockerNode_.setAttribute('aria-hidden', 'true');
rgindad5613292012-06-19 15:40:37 -07001599 this.scrollBlockerNode_.style.cssText =
1600 ('position: absolute;' +
1601 'top: -99px;' +
1602 'display: block;' +
1603 'width: 10px;' +
1604 'height: 10px;');
1605 this.document_.body.appendChild(this.scrollBlockerNode_);
1606
rgindad5613292012-06-19 15:40:37 -07001607 this.scrollPort_.onScrollWheel = onMouse;
1608 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1609 ].forEach(function(event) {
1610 this.scrollBlockerNode_.addEventListener(event, onMouse);
1611 this.cursorNode_.addEventListener(event, onMouse);
1612 this.document_.addEventListener(event, onMouse);
1613 }.bind(this));
1614
1615 this.cursorNode_.addEventListener('mousedown', function() {
1616 setTimeout(this.focus.bind(this));
1617 }.bind(this));
1618
rginda8ba33642011-12-14 12:31:31 -08001619 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001620
rginda87b86462011-12-14 13:48:03 -08001621 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001622 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001623};
1624
rginda0918b652012-04-04 11:26:24 -07001625/**
1626 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001627 *
1628 * @return {HTMLDocument}
rginda0918b652012-04-04 11:26:24 -07001629 */
rginda87b86462011-12-14 13:48:03 -08001630hterm.Terminal.prototype.getDocument = function() {
1631 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001632};
1633
1634/**
rginda0918b652012-04-04 11:26:24 -07001635 * Focus the terminal.
1636 */
1637hterm.Terminal.prototype.focus = function() {
1638 this.scrollPort_.focus();
1639};
1640
1641/**
rginda8ba33642011-12-14 12:31:31 -08001642 * Return the HTML Element for a given row index.
1643 *
1644 * This is a method from the RowProvider interface. The ScrollPort uses
1645 * it to fetch rows on demand as they are scrolled into view.
1646 *
1647 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1648 * pairs to conserve memory.
1649 *
1650 * @param {integer} index The zero-based row index, measured relative to the
1651 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001652 * largest indices.
rginda8ba33642011-12-14 12:31:31 -08001653 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1654 */
1655hterm.Terminal.prototype.getRowNode = function(index) {
1656 if (index < this.scrollbackRows_.length)
1657 return this.scrollbackRows_[index];
1658
1659 var screenIndex = index - this.scrollbackRows_.length;
1660 return this.screen_.rowsArray[screenIndex];
1661};
1662
1663/**
1664 * Return the text content for a given range of rows.
1665 *
1666 * This is a method from the RowProvider interface. The ScrollPort uses
1667 * it to fetch text content on demand when the user attempts to copy their
1668 * selection to the clipboard.
1669 *
1670 * @param {integer} start The zero-based row index to start from, measured
1671 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001672 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001673 * @param {integer} end The zero-based row index to end on, measured
1674 * relative to the start of the scrollback buffer.
1675 * @return {string} A single string containing the text value of the range of
1676 * rows. Lines will be newline delimited, with no trailing newline.
1677 */
1678hterm.Terminal.prototype.getRowsText = function(start, end) {
1679 var ary = [];
1680 for (var i = start; i < end; i++) {
1681 var node = this.getRowNode(i);
1682 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001683 if (i < end - 1 && !node.getAttribute('line-overflow'))
1684 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001685 }
1686
rgindaa09e7332012-08-17 12:49:51 -07001687 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001688};
1689
1690/**
1691 * Return the text content for a given row.
1692 *
1693 * This is a method from the RowProvider interface. The ScrollPort uses
1694 * it to fetch text content on demand when the user attempts to copy their
1695 * selection to the clipboard.
1696 *
1697 * @param {integer} index The zero-based row index to return, measured
1698 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001699 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001700 * @return {string} A string containing the text value of the selected row.
1701 */
1702hterm.Terminal.prototype.getRowText = function(index) {
1703 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001704 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001705};
1706
1707/**
1708 * Return the total number of rows in the addressable screen and in the
1709 * scrollback buffer of this terminal.
1710 *
1711 * This is a method from the RowProvider interface. The ScrollPort uses
1712 * it to compute the size of the scrollbar.
1713 *
1714 * @return {integer} The number of rows in this terminal.
1715 */
1716hterm.Terminal.prototype.getRowCount = function() {
1717 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1718};
1719
1720/**
1721 * Create DOM nodes for new rows and append them to the end of the terminal.
1722 *
1723 * This is the only correct way to add a new DOM node for a row. Notice that
1724 * the new row is appended to the bottom of the list of rows, and does not
1725 * require renumbering (of the rowIndex property) of previous rows.
1726 *
1727 * If you think you want a new blank row somewhere in the middle of the
1728 * terminal, look into moveRows_().
1729 *
1730 * This method does not pay attention to vtScrollTop/Bottom, since you should
1731 * be using moveRows() in cases where they would matter.
1732 *
1733 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001734 *
1735 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001736 */
1737hterm.Terminal.prototype.appendRows_ = function(count) {
1738 var cursorRow = this.screen_.rowsArray.length;
1739 var offset = this.scrollbackRows_.length + cursorRow;
1740 for (var i = 0; i < count; i++) {
1741 var row = this.document_.createElement('x-row');
1742 row.appendChild(this.document_.createTextNode(''));
1743 row.rowIndex = offset + i;
1744 this.screen_.pushRow(row);
1745 }
1746
1747 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1748 if (extraRows > 0) {
1749 var ary = this.screen_.shiftRows(extraRows);
1750 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001751 if (this.scrollPort_.isScrolledEnd)
1752 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001753 }
1754
1755 if (cursorRow >= this.screen_.rowsArray.length)
1756 cursorRow = this.screen_.rowsArray.length - 1;
1757
rginda87b86462011-12-14 13:48:03 -08001758 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001759};
1760
1761/**
1762 * Relocate rows from one part of the addressable screen to another.
1763 *
1764 * This is used to recycle rows during VT scrolls (those which are driven
1765 * by VT commands, rather than by the user manipulating the scrollbar.)
1766 *
1767 * In this case, the blank lines scrolled into the scroll region are made of
1768 * the nodes we scrolled off. These have their rowIndex properties carefully
1769 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001770 *
1771 * @param {number} fromIndex The start index.
1772 * @param {number} count The number of rows to move.
1773 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001774 */
1775hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1776 var ary = this.screen_.removeRows(fromIndex, count);
1777 this.screen_.insertRows(toIndex, ary);
1778
1779 var start, end;
1780 if (fromIndex < toIndex) {
1781 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001782 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001783 } else {
1784 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001785 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001786 }
1787
1788 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001789 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001790};
1791
1792/**
1793 * Renumber the rowIndex property of the given range of rows.
1794 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001795 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08001796 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001797 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001798 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05001799 *
1800 * @param {number} start The start index.
1801 * @param {number} end The end index.
1802 * @param {hterm.Screen} opt_screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08001803 */
Robert Ginda40932892012-12-10 17:26:40 -08001804hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1805 var screen = opt_screen || this.screen_;
1806
rginda8ba33642011-12-14 12:31:31 -08001807 var offset = this.scrollbackRows_.length;
1808 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001809 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001810 }
1811};
1812
1813/**
1814 * Print a string to the terminal.
1815 *
1816 * This respects the current insert and wraparound modes. It will add new lines
1817 * to the end of the terminal, scrolling off the top into the scrollback buffer
1818 * if necessary.
1819 *
1820 * The string is *not* parsed for escape codes. Use the interpret() method if
1821 * that's what you're after.
1822 *
1823 * @param{string} str The string to print.
1824 */
1825hterm.Terminal.prototype.print = function(str) {
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001826 this.scheduleSyncCursorPosition_();
1827
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001828 // Basic accessibility output for the screen reader.
Raymes Khoury177aec72018-06-26 10:58:53 +10001829 this.accessibilityReader_.announce(str);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001830
rgindaa9abdd82012-08-06 18:05:09 -07001831 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001832
Ricky Liang48f05cb2013-12-31 23:35:29 +08001833 var strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04001834 // Fun edge case: If the string only contains zero width codepoints (like
1835 // combining characters), we make sure to iterate at least once below.
1836 if (strWidth == 0 && str)
1837 strWidth = 1;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001838
1839 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001840 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1841 this.screen_.commitLineOverflow();
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001842 this.newLine(true);
rgindaa09e7332012-08-17 12:49:51 -07001843 }
rgindaa19afe22012-01-25 15:40:22 -08001844
Ricky Liang48f05cb2013-12-31 23:35:29 +08001845 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001846 var didOverflow = false;
1847 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001848
rgindaa9abdd82012-08-06 18:05:09 -07001849 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1850 didOverflow = true;
1851 count = this.screenSize.width - this.screen_.cursorPosition.column;
1852 }
rgindaa19afe22012-01-25 15:40:22 -08001853
rgindaa9abdd82012-08-06 18:05:09 -07001854 if (didOverflow && !this.options_.wraparound) {
1855 // If the string overflowed the line but wraparound is off, then the
1856 // last printed character should be the last of the string.
1857 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001858 substr = lib.wc.substr(str, startOffset, count - 1) +
1859 lib.wc.substr(str, strWidth - 1);
1860 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001861 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001862 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001863 }
rgindaa19afe22012-01-25 15:40:22 -08001864
Ricky Liang48f05cb2013-12-31 23:35:29 +08001865 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1866 for (var i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001867 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
1868 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001869
1870 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001871 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001872 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001873 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001874 }
1875 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001876 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07001877 }
1878
1879 this.screen_.maybeClipCurrentRow();
1880 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001881 }
rginda8ba33642011-12-14 12:31:31 -08001882
rginda9f5222b2012-03-05 11:53:28 -08001883 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001884 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001885};
1886
1887/**
rginda87b86462011-12-14 13:48:03 -08001888 * Set the VT scroll region.
1889 *
rginda87b86462011-12-14 13:48:03 -08001890 * This also resets the cursor position to the absolute (0, 0) position, since
1891 * that's what xterm appears to do.
1892 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001893 * Setting the scroll region to the full height of the terminal will clear
1894 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1895 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1896 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1897 * continue to work as most users would expect.
1898 *
rginda87b86462011-12-14 13:48:03 -08001899 * @param {integer} scrollTop The zero-based top of the scroll region.
1900 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1901 * inclusive.
1902 */
1903hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001904 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001905 this.vtScrollTop_ = null;
1906 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001907 } else {
1908 this.vtScrollTop_ = scrollTop;
1909 this.vtScrollBottom_ = scrollBottom;
1910 }
rginda87b86462011-12-14 13:48:03 -08001911};
1912
1913/**
rginda8ba33642011-12-14 12:31:31 -08001914 * Return the top row index according to the VT.
1915 *
1916 * This will return 0 unless the terminal has been told to restrict scrolling
1917 * to some lower row. It is used for some VT cursor positioning and scrolling
1918 * commands.
1919 *
1920 * @return {integer} The topmost row in the terminal's scroll region.
1921 */
1922hterm.Terminal.prototype.getVTScrollTop = function() {
1923 if (this.vtScrollTop_ != null)
1924 return this.vtScrollTop_;
1925
1926 return 0;
rginda87b86462011-12-14 13:48:03 -08001927};
rginda8ba33642011-12-14 12:31:31 -08001928
1929/**
1930 * Return the bottom row index according to the VT.
1931 *
1932 * This will return the height of the terminal unless the it has been told to
1933 * restrict scrolling to some higher row. It is used for some VT cursor
1934 * positioning and scrolling commands.
1935 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001936 * @return {integer} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001937 */
1938hterm.Terminal.prototype.getVTScrollBottom = function() {
1939 if (this.vtScrollBottom_ != null)
1940 return this.vtScrollBottom_;
1941
rginda87b86462011-12-14 13:48:03 -08001942 return this.screenSize.height - 1;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001943};
rginda8ba33642011-12-14 12:31:31 -08001944
1945/**
1946 * Process a '\n' character.
1947 *
1948 * If the cursor is on the final row of the terminal this will append a new
1949 * blank row to the screen and scroll the topmost row into the scrollback
1950 * buffer.
1951 *
1952 * Otherwise, this moves the cursor to column zero of the next row.
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001953 *
1954 * @param {boolean=} dueToOverflow Whether the newline is due to wraparound of
1955 * the terminal.
rginda8ba33642011-12-14 12:31:31 -08001956 */
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001957hterm.Terminal.prototype.newLine = function(dueToOverflow = false) {
1958 if (!dueToOverflow)
1959 this.accessibilityReader_.newLine();
1960
Robert Ginda9937abc2013-07-25 16:09:23 -07001961 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1962 this.screen_.rowsArray.length - 1);
1963
1964 if (this.vtScrollBottom_ != null) {
1965 // A VT Scroll region is active, we never append new rows.
1966 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1967 // We're at the end of the VT Scroll Region, perform a VT scroll.
1968 this.vtScrollUp(1);
1969 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1970 } else if (cursorAtEndOfScreen) {
1971 // We're at the end of the screen, the only thing to do is put the
1972 // cursor to column 0.
1973 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1974 } else {
1975 // Anywhere else, advance the cursor row, and reset the column.
1976 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1977 }
1978 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001979 // We're at the end of the screen. Append a new row to the terminal,
1980 // shifting the top row into the scrollback.
1981 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001982 } else {
rginda87b86462011-12-14 13:48:03 -08001983 // Anywhere else in the screen just moves the cursor.
1984 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001985 }
1986};
1987
1988/**
1989 * Like newLine(), except maintain the cursor column.
1990 */
1991hterm.Terminal.prototype.lineFeed = function() {
1992 var column = this.screen_.cursorPosition.column;
1993 this.newLine();
1994 this.setCursorColumn(column);
1995};
1996
1997/**
rginda87b86462011-12-14 13:48:03 -08001998 * If autoCarriageReturn is set then newLine(), else lineFeed().
1999 */
2000hterm.Terminal.prototype.formFeed = function() {
2001 if (this.options_.autoCarriageReturn) {
2002 this.newLine();
2003 } else {
2004 this.lineFeed();
2005 }
2006};
2007
2008/**
2009 * Move the cursor up one row, possibly inserting a blank line.
2010 *
2011 * The cursor column is not changed.
2012 */
2013hterm.Terminal.prototype.reverseLineFeed = function() {
2014 var scrollTop = this.getVTScrollTop();
2015 var currentRow = this.screen_.cursorPosition.row;
2016
2017 if (currentRow == scrollTop) {
2018 this.insertLines(1);
2019 } else {
2020 this.setAbsoluteCursorRow(currentRow - 1);
2021 }
2022};
2023
2024/**
rginda8ba33642011-12-14 12:31:31 -08002025 * Replace all characters to the left of the current cursor with the space
2026 * character.
2027 *
2028 * TODO(rginda): This should probably *remove* the characters (not just replace
2029 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07002030 * position.
rginda8ba33642011-12-14 12:31:31 -08002031 */
2032hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08002033 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002034 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002035 const count = cursor.column + 1;
2036 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08002037 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002038};
2039
2040/**
David Benjamin684a9b72012-05-01 17:19:58 -04002041 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08002042 *
2043 * The cursor position is unchanged.
2044 *
Robert Gindaf2547f12012-10-25 20:36:21 -07002045 * If the current background color is not the default background color this
2046 * will insert spaces rather than delete. This is unfortunate because the
2047 * trailing space will affect text selection, but it's difficult to come up
2048 * with a way to style empty space that wouldn't trip up the hterm.Screen
2049 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07002050 *
2051 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
2052 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
2053 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05002054 *
2055 * @param {number} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08002056 */
2057hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07002058 if (this.screen_.cursorPosition.overflow)
2059 return;
2060
Robert Ginda7fd57082012-09-25 14:41:47 -07002061 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
2062 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07002063
2064 if (this.screen_.textAttributes.background ===
2065 this.screen_.textAttributes.DEFAULT_COLOR) {
2066 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08002067 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07002068 this.screen_.cursorPosition.column + count) {
2069 this.screen_.deleteChars(count);
2070 this.clearCursorOverflow();
2071 return;
2072 }
2073 }
2074
rginda87b86462011-12-14 13:48:03 -08002075 var cursor = this.saveCursor();
Mike Frysinger6380bed2017-08-24 18:46:39 -04002076 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08002077 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002078 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002079};
2080
2081/**
2082 * Erase the current line.
2083 *
2084 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002085 */
2086hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08002087 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002088 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08002089 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002090 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002091};
2092
2093/**
David Benjamina08d78f2012-05-05 00:28:49 -04002094 * Erase all characters from the start of the screen to the current cursor
2095 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002096 *
2097 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002098 */
2099hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08002100 var cursor = this.saveCursor();
2101
2102 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002103
David Benjamina08d78f2012-05-05 00:28:49 -04002104 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002105 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002106 this.screen_.clearCursorRow();
2107 }
2108
rginda87b86462011-12-14 13:48:03 -08002109 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002110 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002111};
2112
2113/**
2114 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002115 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002116 *
2117 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002118 */
2119hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08002120 var cursor = this.saveCursor();
2121
2122 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002123
David Benjamina08d78f2012-05-05 00:28:49 -04002124 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08002125 for (var i = cursor.row + 1; i <= bottom; i++) {
2126 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002127 this.screen_.clearCursorRow();
2128 }
2129
rginda87b86462011-12-14 13:48:03 -08002130 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002131 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002132};
2133
2134/**
2135 * Fill the terminal with a given character.
2136 *
2137 * This methods does not respect the VT scroll region.
2138 *
2139 * @param {string} ch The character to use for the fill.
2140 */
2141hterm.Terminal.prototype.fill = function(ch) {
2142 var cursor = this.saveCursor();
2143
2144 this.setAbsoluteCursorPosition(0, 0);
2145 for (var row = 0; row < this.screenSize.height; row++) {
2146 for (var col = 0; col < this.screenSize.width; col++) {
2147 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002148 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002149 }
2150 }
2151
2152 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002153};
2154
2155/**
rginda9ea433c2012-03-16 11:57:00 -07002156 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002157 *
rginda9ea433c2012-03-16 11:57:00 -07002158 * This does not respect the scroll region.
2159 *
2160 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2161 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002162 */
rginda9ea433c2012-03-16 11:57:00 -07002163hterm.Terminal.prototype.clearHome = function(opt_screen) {
2164 var screen = opt_screen || this.screen_;
2165 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002166
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002167 this.accessibilityReader_.clear();
2168
rginda11057d52012-04-25 12:29:56 -07002169 if (bottom == 0) {
2170 // Empty screen, nothing to do.
2171 return;
2172 }
2173
rgindae4d29232012-01-19 10:47:13 -08002174 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002175 screen.setCursorPosition(i, 0);
2176 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002177 }
2178
rginda9ea433c2012-03-16 11:57:00 -07002179 screen.setCursorPosition(0, 0);
2180};
2181
2182/**
2183 * Erase the entire display without changing the cursor position.
2184 *
2185 * The cursor position is unchanged. This does not respect the scroll
2186 * region.
2187 *
2188 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2189 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002190 */
2191hterm.Terminal.prototype.clear = function(opt_screen) {
2192 var screen = opt_screen || this.screen_;
2193 var cursor = screen.cursorPosition.clone();
2194 this.clearHome(screen);
2195 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002196};
2197
2198/**
2199 * VT command to insert lines at the current cursor row.
2200 *
2201 * This respects the current scroll region. Rows pushed off the bottom are
2202 * lost (they won't show up in the scrollback buffer).
2203 *
rginda8ba33642011-12-14 12:31:31 -08002204 * @param {integer} count The number of lines to insert.
2205 */
2206hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07002207 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002208
2209 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002210 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002211
Robert Ginda579186b2012-09-26 11:40:04 -07002212 // The moveCount is the number of rows we need to relocate to make room for
2213 // the new row(s). The count is the distance to move them.
2214 var moveCount = bottom - cursorRow - count + 1;
2215 if (moveCount)
2216 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08002217
Robert Ginda579186b2012-09-26 11:40:04 -07002218 for (var i = count - 1; i >= 0; i--) {
2219 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002220 this.screen_.clearCursorRow();
2221 }
rginda8ba33642011-12-14 12:31:31 -08002222};
2223
2224/**
2225 * VT command to delete lines at the current cursor row.
2226 *
2227 * New rows are added to the bottom of scroll region to take their place. New
2228 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002229 *
2230 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002231 */
2232hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08002233 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002234
rginda87b86462011-12-14 13:48:03 -08002235 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08002236 var bottom = this.getVTScrollBottom();
2237
rginda87b86462011-12-14 13:48:03 -08002238 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002239 count = Math.min(count, maxCount);
2240
rginda87b86462011-12-14 13:48:03 -08002241 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08002242 if (count != maxCount)
2243 this.moveRows_(top, count, moveStart);
2244
2245 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002246 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002247 this.screen_.clearCursorRow();
2248 }
2249
rginda87b86462011-12-14 13:48:03 -08002250 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002251 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002252};
2253
2254/**
2255 * Inserts the given number of spaces at the current cursor position.
2256 *
rginda87b86462011-12-14 13:48:03 -08002257 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002258 *
2259 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002260 */
2261hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08002262 var cursor = this.saveCursor();
2263
rgindacbbd7482012-06-13 15:06:16 -07002264 var ws = lib.f.getWhitespace(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002265 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002266 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002267
2268 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002269 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002270};
2271
2272/**
2273 * Forward-delete the specified number of characters starting at the cursor
2274 * position.
2275 *
2276 * @param {integer} count The number of characters to delete.
2277 */
2278hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002279 var deleted = this.screen_.deleteChars(count);
2280 if (deleted && !this.screen_.textAttributes.isDefault()) {
2281 var cursor = this.saveCursor();
2282 this.setCursorColumn(this.screenSize.width - deleted);
2283 this.screen_.insertString(lib.f.getWhitespace(deleted));
2284 this.restoreCursor(cursor);
2285 }
2286
David Benjamin54e8bf62012-06-01 22:31:40 -04002287 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002288};
2289
2290/**
2291 * Shift rows in the scroll region upwards by a given number of lines.
2292 *
2293 * New rows are inserted at the bottom of the scroll region to fill the
2294 * vacated rows. The new rows not filled out with the current text attributes.
2295 *
2296 * This function does not affect the scrollback rows at all. Rows shifted
2297 * off the top are lost.
2298 *
rginda87b86462011-12-14 13:48:03 -08002299 * The cursor position is not altered.
2300 *
rginda8ba33642011-12-14 12:31:31 -08002301 * @param {integer} count The number of rows to scroll.
2302 */
2303hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002304 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002305
rginda87b86462011-12-14 13:48:03 -08002306 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002307 this.deleteLines(count);
2308
rginda87b86462011-12-14 13:48:03 -08002309 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002310};
2311
2312/**
2313 * Shift rows below the cursor down by a given number of lines.
2314 *
2315 * This function respects the current scroll region.
2316 *
2317 * New rows are inserted at the top of the scroll region to fill the
2318 * vacated rows. The new rows not filled out with the current text attributes.
2319 *
2320 * This function does not affect the scrollback rows at all. Rows shifted
2321 * off the bottom are lost.
2322 *
2323 * @param {integer} count The number of rows to scroll.
2324 */
2325hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08002326 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002327
rginda87b86462011-12-14 13:48:03 -08002328 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08002329 this.insertLines(opt_count);
2330
rginda87b86462011-12-14 13:48:03 -08002331 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002332};
2333
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002334/**
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002335 * Enable accessibility-friendly features that have a performance impact.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002336 *
2337 * This will generate additional DOM nodes in an aria-live region that will
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002338 * cause Assitive Technology to announce the output of the terminal. It also
2339 * enables other features that aid assistive technology. All the features gated
2340 * behind this flag have a performance impact on the terminal which is why they
2341 * are made optional.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002342 *
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002343 * @param {boolean} enabled Whether to enable accessibility-friendly features.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002344 */
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002345hterm.Terminal.prototype.setAccessibilityEnabled = function(enabled) {
Raymes Khoury177aec72018-06-26 10:58:53 +10002346 this.accessibilityReader_.setAccessibilityEnabled(enabled);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002347};
rginda87b86462011-12-14 13:48:03 -08002348
rginda8ba33642011-12-14 12:31:31 -08002349/**
2350 * Set the cursor position.
2351 *
2352 * The cursor row is relative to the scroll region if the terminal has
2353 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2354 *
2355 * @param {integer} row The new zero-based cursor row.
2356 * @param {integer} row The new zero-based cursor column.
2357 */
2358hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2359 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002360 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002361 } else {
rginda87b86462011-12-14 13:48:03 -08002362 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002363 }
rginda87b86462011-12-14 13:48:03 -08002364};
rginda8ba33642011-12-14 12:31:31 -08002365
Evan Jones2600d4f2016-12-06 09:29:36 -05002366/**
2367 * Move the cursor relative to its current position.
2368 *
2369 * @param {number} row
2370 * @param {number} column
2371 */
rginda87b86462011-12-14 13:48:03 -08002372hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2373 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002374 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2375 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002376 this.screen_.setCursorPosition(row, column);
2377};
2378
Evan Jones2600d4f2016-12-06 09:29:36 -05002379/**
2380 * Move the cursor to the specified position.
2381 *
2382 * @param {number} row
2383 * @param {number} column
2384 */
rginda87b86462011-12-14 13:48:03 -08002385hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002386 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2387 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002388 this.screen_.setCursorPosition(row, column);
2389};
2390
2391/**
2392 * Set the cursor column.
2393 *
2394 * @param {integer} column The new zero-based cursor column.
2395 */
2396hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002397 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002398};
2399
2400/**
2401 * Return the cursor column.
2402 *
2403 * @return {integer} The zero-based cursor column.
2404 */
2405hterm.Terminal.prototype.getCursorColumn = function() {
2406 return this.screen_.cursorPosition.column;
2407};
2408
2409/**
2410 * Set the cursor row.
2411 *
2412 * The cursor row is relative to the scroll region if the terminal has
2413 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2414 *
2415 * @param {integer} row The new cursor row.
2416 */
rginda87b86462011-12-14 13:48:03 -08002417hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2418 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002419};
2420
2421/**
2422 * Return the cursor row.
2423 *
2424 * @return {integer} The zero-based cursor row.
2425 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002426hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002427 return this.screen_.cursorPosition.row;
2428};
2429
2430/**
2431 * Request that the ScrollPort redraw itself soon.
2432 *
2433 * The redraw will happen asynchronously, soon after the call stack winds down.
2434 * Multiple calls will be coalesced into a single redraw.
2435 */
2436hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002437 if (this.timeouts_.redraw)
2438 return;
rginda8ba33642011-12-14 12:31:31 -08002439
2440 var self = this;
rginda87b86462011-12-14 13:48:03 -08002441 this.timeouts_.redraw = setTimeout(function() {
2442 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002443 self.scrollPort_.redraw_();
2444 }, 0);
2445};
2446
2447/**
2448 * Request that the ScrollPort be scrolled to the bottom.
2449 *
2450 * The scroll will happen asynchronously, soon after the call stack winds down.
2451 * Multiple calls will be coalesced into a single scroll.
2452 *
2453 * This affects the scrollbar position of the ScrollPort, and has nothing to
2454 * do with the VT scroll commands.
2455 */
2456hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2457 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002458 return;
rginda8ba33642011-12-14 12:31:31 -08002459
2460 var self = this;
2461 this.timeouts_.scrollDown = setTimeout(function() {
2462 delete self.timeouts_.scrollDown;
2463 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2464 }, 10);
2465};
2466
2467/**
2468 * Move the cursor up a specified number of rows.
2469 *
2470 * @param {integer} count The number of rows to move the cursor.
2471 */
2472hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002473 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002474};
2475
2476/**
2477 * Move the cursor down a specified number of rows.
2478 *
2479 * @param {integer} count The number of rows to move the cursor.
2480 */
2481hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002482 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002483 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2484 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2485 this.screenSize.height - 1);
2486
rgindacbbd7482012-06-13 15:06:16 -07002487 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002488 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002489 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002490};
2491
2492/**
2493 * Move the cursor left a specified number of columns.
2494 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002495 * If reverse wraparound mode is enabled and the previous row wrapped into
2496 * the current row then we back up through the wraparound as well.
2497 *
rginda8ba33642011-12-14 12:31:31 -08002498 * @param {integer} count The number of columns to move the cursor.
2499 */
2500hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002501 count = count || 1;
2502
2503 if (count < 1)
2504 return;
2505
2506 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002507 if (this.options_.reverseWraparound) {
2508 if (this.screen_.cursorPosition.overflow) {
2509 // If this cursor is in the right margin, consume one count to get it
2510 // back to the last column. This only applies when we're in reverse
2511 // wraparound mode.
2512 count--;
2513 this.clearCursorOverflow();
2514
2515 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002516 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002517 }
2518
Robert Gindabfb32622014-07-17 13:20:27 -07002519 var newRow = this.screen_.cursorPosition.row;
2520 var newColumn = currentColumn - count;
2521 if (newColumn < 0) {
2522 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2523 if (newRow < 0) {
2524 // xterm also wraps from row 0 to the last row.
2525 newRow = this.screenSize.height + newRow % this.screenSize.height;
2526 }
2527 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2528 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002529
Robert Gindabfb32622014-07-17 13:20:27 -07002530 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2531
2532 } else {
2533 var newColumn = Math.max(currentColumn - count, 0);
2534 this.setCursorColumn(newColumn);
2535 }
rginda8ba33642011-12-14 12:31:31 -08002536};
2537
2538/**
2539 * Move the cursor right a specified number of columns.
2540 *
2541 * @param {integer} count The number of columns to move the cursor.
2542 */
2543hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002544 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002545
2546 if (count < 1)
2547 return;
2548
rgindacbbd7482012-06-13 15:06:16 -07002549 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002550 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002551 this.setCursorColumn(column);
2552};
2553
2554/**
2555 * Reverse the foreground and background colors of the terminal.
2556 *
2557 * This only affects text that was drawn with no attributes.
2558 *
2559 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2560 * been drawn with attributes that happen to coincide with the default
2561 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002562 *
2563 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002564 */
2565hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002566 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002567 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002568 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2569 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002570 } else {
rginda9f5222b2012-03-05 11:53:28 -08002571 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2572 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002573 }
2574};
2575
2576/**
rginda87b86462011-12-14 13:48:03 -08002577 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002578 *
2579 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002580 */
2581hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002582 this.cursorNode_.style.backgroundColor =
2583 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002584
2585 var self = this;
2586 setTimeout(function() {
Matheus Fernandes2d733082017-09-11 06:43:01 -04002587 self.restyleCursor_();
rginda6d397402012-01-17 10:58:29 -08002588 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002589
Michael Kelly485ecd12014-06-09 11:41:56 -04002590 // bellSquelchTimeout_ affects both audio and notification bells.
2591 if (this.bellSquelchTimeout_)
2592 return;
2593
Robert Ginda92e18102013-03-14 13:56:37 -07002594 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002595 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002596 this.bellSequelchTimeout_ = setTimeout(function() {
2597 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002598 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002599 } else {
2600 delete this.bellSquelchTimeout_;
2601 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002602
2603 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingera5fb83c2017-06-22 14:48:35 -07002604 var n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002605 this.bellNotificationList_.push(n);
2606 // TODO: Should we try to raise the window here?
2607 n.onclick = function() { self.closeBellNotifications_(); };
2608 }
rginda87b86462011-12-14 13:48:03 -08002609};
2610
2611/**
rginda8ba33642011-12-14 12:31:31 -08002612 * Set the origin mode bit.
2613 *
2614 * If origin mode is on, certain VT cursor and scrolling commands measure their
2615 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2616 * to the top of the addressable screen.
2617 *
2618 * Defaults to off.
2619 *
2620 * @param {boolean} state True to set origin mode, false to unset.
2621 */
2622hterm.Terminal.prototype.setOriginMode = function(state) {
2623 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002624 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002625};
2626
2627/**
2628 * Set the insert mode bit.
2629 *
2630 * If insert mode is on, existing text beyond the cursor position will be
2631 * shifted right to make room for new text. Otherwise, new text overwrites
2632 * any existing text.
2633 *
2634 * Defaults to off.
2635 *
2636 * @param {boolean} state True to set insert mode, false to unset.
2637 */
2638hterm.Terminal.prototype.setInsertMode = function(state) {
2639 this.options_.insertMode = state;
2640};
2641
2642/**
rginda87b86462011-12-14 13:48:03 -08002643 * Set the auto carriage return bit.
2644 *
2645 * If auto carriage return is on then a formfeed character is interpreted
2646 * as a newline, otherwise it's the same as a linefeed. The difference boils
2647 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002648 *
2649 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002650 */
2651hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2652 this.options_.autoCarriageReturn = state;
2653};
2654
2655/**
rginda8ba33642011-12-14 12:31:31 -08002656 * Set the wraparound mode bit.
2657 *
2658 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2659 * to the start of the following row. Otherwise, the cursor is clamped to the
2660 * end of the screen and attempts to write past it are ignored.
2661 *
2662 * Defaults to on.
2663 *
2664 * @param {boolean} state True to set wraparound mode, false to unset.
2665 */
2666hterm.Terminal.prototype.setWraparound = function(state) {
2667 this.options_.wraparound = state;
2668};
2669
2670/**
2671 * Set the reverse-wraparound mode bit.
2672 *
2673 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2674 * to the end of the previous row. Otherwise, the cursor is clamped to column
2675 * 0.
2676 *
2677 * Defaults to off.
2678 *
2679 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2680 */
2681hterm.Terminal.prototype.setReverseWraparound = function(state) {
2682 this.options_.reverseWraparound = state;
2683};
2684
2685/**
2686 * Selects between the primary and alternate screens.
2687 *
2688 * If alternate mode is on, the alternate screen is active. Otherwise the
2689 * primary screen is active.
2690 *
2691 * Swapping screens has no effect on the scrollback buffer.
2692 *
2693 * Each screen maintains its own cursor position.
2694 *
2695 * Defaults to off.
2696 *
2697 * @param {boolean} state True to set alternate mode, false to unset.
2698 */
2699hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002700 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002701 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2702
rginda35c456b2012-02-09 17:29:05 -08002703 if (this.screen_.rowsArray.length &&
2704 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2705 // If the screen changed sizes while we were away, our rowIndexes may
2706 // be incorrect.
2707 var offset = this.scrollbackRows_.length;
2708 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002709 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002710 ary[i].rowIndex = offset + i;
2711 }
2712 }
rginda8ba33642011-12-14 12:31:31 -08002713
rginda35c456b2012-02-09 17:29:05 -08002714 this.realizeWidth_(this.screenSize.width);
2715 this.realizeHeight_(this.screenSize.height);
2716 this.scrollPort_.syncScrollHeight();
2717 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002718
rginda6d397402012-01-17 10:58:29 -08002719 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002720 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002721};
2722
2723/**
2724 * Set the cursor-blink mode bit.
2725 *
2726 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2727 * a visible cursor does not blink.
2728 *
2729 * You should make sure to turn blinking off if you're going to dispose of a
2730 * terminal, otherwise you'll leak a timeout.
2731 *
2732 * Defaults to on.
2733 *
2734 * @param {boolean} state True to set cursor-blink mode, false to unset.
2735 */
2736hterm.Terminal.prototype.setCursorBlink = function(state) {
2737 this.options_.cursorBlink = state;
2738
2739 if (!state && this.timeouts_.cursorBlink) {
2740 clearTimeout(this.timeouts_.cursorBlink);
2741 delete this.timeouts_.cursorBlink;
2742 }
2743
2744 if (this.options_.cursorVisible)
2745 this.setCursorVisible(true);
2746};
2747
2748/**
2749 * Set the cursor-visible mode bit.
2750 *
2751 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2752 *
2753 * Defaults to on.
2754 *
2755 * @param {boolean} state True to set cursor-visible mode, false to unset.
2756 */
2757hterm.Terminal.prototype.setCursorVisible = function(state) {
2758 this.options_.cursorVisible = state;
2759
2760 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002761 if (this.timeouts_.cursorBlink) {
2762 clearTimeout(this.timeouts_.cursorBlink);
2763 delete this.timeouts_.cursorBlink;
2764 }
rginda87b86462011-12-14 13:48:03 -08002765 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002766 return;
2767 }
2768
rginda87b86462011-12-14 13:48:03 -08002769 this.syncCursorPosition_();
2770
2771 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002772
2773 if (this.options_.cursorBlink) {
2774 if (this.timeouts_.cursorBlink)
2775 return;
2776
Robert Gindaea2183e2014-07-17 09:51:51 -07002777 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002778 } else {
2779 if (this.timeouts_.cursorBlink) {
2780 clearTimeout(this.timeouts_.cursorBlink);
2781 delete this.timeouts_.cursorBlink;
2782 }
2783 }
2784};
2785
2786/**
rginda87b86462011-12-14 13:48:03 -08002787 * Synchronizes the visible cursor and document selection with the current
2788 * cursor coordinates.
Raymes Khourye5d48982018-08-02 09:08:32 +10002789 *
2790 * @return {boolean} True if the cursor is onscreen and synced.
rginda8ba33642011-12-14 12:31:31 -08002791 */
2792hterm.Terminal.prototype.syncCursorPosition_ = function() {
2793 var topRowIndex = this.scrollPort_.getTopRowIndex();
2794 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2795 var cursorRowIndex = this.scrollbackRows_.length +
2796 this.screen_.cursorPosition.row;
2797
Raymes Khoury15697f42018-07-17 11:37:18 +10002798 let forceSyncSelection = false;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002799 if (this.accessibilityReader_.accessibilityEnabled) {
2800 // Report the new position of the cursor for accessibility purposes.
2801 const cursorColumnIndex = this.screen_.cursorPosition.column;
2802 const cursorLineText =
2803 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
Raymes Khoury15697f42018-07-17 11:37:18 +10002804 // This will force the selection to be sync'd to the cursor position if the
2805 // user has pressed a key. Generally we would only sync the cursor position
2806 // when selection is collapsed so that if the user has selected something
2807 // we don't clear the selection by moving the selection. However when a
2808 // screen reader is used, it's intuitive for entering a key to move the
2809 // selection to the cursor.
2810 forceSyncSelection = this.accessibilityReader_.hasUserGesture;
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002811 this.accessibilityReader_.afterCursorChange(
2812 cursorLineText, cursorRowIndex, cursorColumnIndex);
2813 }
2814
rginda8ba33642011-12-14 12:31:31 -08002815 if (cursorRowIndex > bottomRowIndex) {
2816 // Cursor is scrolled off screen, move it outside of the visible area.
Mike Frysinger44c32202017-08-05 01:13:09 -04002817 this.setCssVar('cursor-offset-row', '-1');
Raymes Khourye5d48982018-08-02 09:08:32 +10002818 return false;
rginda8ba33642011-12-14 12:31:31 -08002819 }
2820
Robert Gindab837c052014-08-11 11:17:51 -07002821 if (this.options_.cursorVisible &&
2822 this.cursorNode_.style.display == 'none') {
2823 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2824 this.cursorNode_.style.display = '';
2825 }
2826
Mike Frysinger44c32202017-08-05 01:13:09 -04002827 // Position the cursor using CSS variable math. If we do the math in JS,
2828 // the float math will end up being more precise than the CSS which will
2829 // cause the cursor tracking to be off.
2830 this.setCssVar(
2831 'cursor-offset-row',
2832 `${cursorRowIndex - topRowIndex} + ` +
2833 `${this.scrollPort_.visibleRowTopMargin}px`);
2834 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08002835
2836 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04002837 '(' + this.screen_.cursorPosition.column +
2838 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08002839 ')');
2840
2841 // Update the caret for a11y purposes.
2842 var selection = this.document_.getSelection();
Raymes Khoury15697f42018-07-17 11:37:18 +10002843 if (selection && (selection.isCollapsed || forceSyncSelection)) {
rginda87b86462011-12-14 13:48:03 -08002844 this.screen_.syncSelectionCaret(selection);
Raymes Khoury15697f42018-07-17 11:37:18 +10002845 }
Raymes Khourye5d48982018-08-02 09:08:32 +10002846 return true;
rginda8ba33642011-12-14 12:31:31 -08002847};
2848
Robert Gindafb1be6a2013-12-11 11:56:22 -08002849/**
2850 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2851 * and character cell dimensions.
2852 */
Robert Ginda830583c2013-08-07 13:20:46 -07002853hterm.Terminal.prototype.restyleCursor_ = function() {
2854 var shape = this.cursorShape_;
2855
2856 if (this.cursorNode_.getAttribute('focus') == 'false') {
2857 // Always show a block cursor when unfocused.
2858 shape = hterm.Terminal.cursorShape.BLOCK;
2859 }
2860
2861 var style = this.cursorNode_.style;
2862
2863 switch (shape) {
2864 case hterm.Terminal.cursorShape.BEAM:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002865 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002866 style.backgroundColor = 'transparent';
2867 style.borderBottomStyle = null;
2868 style.borderLeftStyle = 'solid';
2869 break;
2870
2871 case hterm.Terminal.cursorShape.UNDERLINE:
2872 style.height = this.scrollPort_.characterSize.baseline + 'px';
2873 style.backgroundColor = 'transparent';
2874 style.borderBottomStyle = 'solid';
2875 // correct the size to put it exactly at the baseline
2876 style.borderLeftStyle = null;
2877 break;
2878
2879 default:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002880 style.height = 'var(--hterm-charsize-height)';
Mike Frysinger2fd079a2018-09-02 01:46:12 -04002881 style.backgroundColor = 'var(--hterm-cursor-color)';
Robert Ginda830583c2013-08-07 13:20:46 -07002882 style.borderBottomStyle = null;
2883 style.borderLeftStyle = null;
2884 break;
2885 }
2886};
2887
rginda8ba33642011-12-14 12:31:31 -08002888/**
2889 * Synchronizes the visible cursor with the current cursor coordinates.
2890 *
2891 * The sync will happen asynchronously, soon after the call stack winds down.
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002892 * Multiple calls will be coalesced into a single sync. This should be called
2893 * prior to the cursor actually changing position.
rginda8ba33642011-12-14 12:31:31 -08002894 */
2895hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2896 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002897 return;
rginda8ba33642011-12-14 12:31:31 -08002898
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002899 if (this.accessibilityReader_.accessibilityEnabled) {
2900 // Report the previous position of the cursor for accessibility purposes.
2901 const cursorRowIndex = this.scrollbackRows_.length +
2902 this.screen_.cursorPosition.row;
2903 const cursorColumnIndex = this.screen_.cursorPosition.column;
2904 const cursorLineText =
2905 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
2906 this.accessibilityReader_.beforeCursorChange(
2907 cursorLineText, cursorRowIndex, cursorColumnIndex);
2908 }
2909
rginda8ba33642011-12-14 12:31:31 -08002910 var self = this;
2911 this.timeouts_.syncCursor = setTimeout(function() {
2912 self.syncCursorPosition_();
2913 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002914 }, 0);
2915};
2916
rgindacc2996c2012-02-24 14:59:31 -08002917/**
rgindaf522ce02012-04-17 17:49:17 -07002918 * Show or hide the zoom warning.
2919 *
2920 * The zoom warning is a message warning the user that their browser zoom must
2921 * be set to 100% in order for hterm to function properly.
2922 *
2923 * @param {boolean} state True to show the message, false to hide it.
2924 */
2925hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2926 if (!this.zoomWarningNode_) {
2927 if (!state)
2928 return;
2929
2930 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002931 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07002932 this.zoomWarningNode_.style.cssText = (
2933 'color: black;' +
2934 'background-color: #ff2222;' +
2935 'font-size: large;' +
2936 'border-radius: 8px;' +
2937 'opacity: 0.75;' +
2938 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2939 'top: 0.5em;' +
2940 'right: 1.2em;' +
2941 'position: absolute;' +
2942 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002943 '-webkit-user-select: none;' +
2944 '-moz-text-size-adjust: none;' +
2945 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05002946
2947 this.zoomWarningNode_.addEventListener('click', function(e) {
2948 this.parentNode.removeChild(this);
2949 });
rgindaf522ce02012-04-17 17:49:17 -07002950 }
2951
Robert Gindab4839c22013-02-28 16:52:10 -08002952 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2953 hterm.zoomWarningMessage,
2954 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2955
rgindaf522ce02012-04-17 17:49:17 -07002956 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2957
2958 if (state) {
2959 if (!this.zoomWarningNode_.parentNode)
2960 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2961 } else if (this.zoomWarningNode_.parentNode) {
2962 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2963 }
2964};
2965
2966/**
rgindacc2996c2012-02-24 14:59:31 -08002967 * Show the terminal overlay for a given amount of time.
2968 *
2969 * The terminal overlay appears in inverse video in a large font, centered
2970 * over the terminal. You should probably keep the overlay message brief,
2971 * since it's in a large font and you probably aren't going to check the size
2972 * of the terminal first.
2973 *
2974 * @param {string} msg The text (not HTML) message to display in the overlay.
2975 * @param {number} opt_timeout The amount of time to wait before fading out
2976 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2977 * stay up forever (or until the next overlay).
2978 */
2979hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002980 if (!this.overlayNode_) {
2981 if (!this.div_)
2982 return;
2983
2984 this.overlayNode_ = this.document_.createElement('div');
2985 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002986 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002987 'font-size: xx-large;' +
2988 'opacity: 0.75;' +
2989 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2990 'position: absolute;' +
2991 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002992 '-webkit-transition: opacity 180ms ease-in;' +
2993 '-moz-user-select: none;' +
2994 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002995
2996 this.overlayNode_.addEventListener('mousedown', function(e) {
2997 e.preventDefault();
2998 e.stopPropagation();
2999 }, true);
rgindaf0090c92012-02-10 14:58:52 -08003000 }
3001
rginda9f5222b2012-03-05 11:53:28 -08003002 this.overlayNode_.style.color = this.prefs_.get('background-color');
3003 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
3004 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
3005
rgindaf0090c92012-02-10 14:58:52 -08003006 this.overlayNode_.textContent = msg;
3007 this.overlayNode_.style.opacity = '0.75';
3008
3009 if (!this.overlayNode_.parentNode)
3010 this.div_.appendChild(this.overlayNode_);
3011
Robert Ginda97769282013-02-01 15:30:30 -08003012 var divSize = hterm.getClientSize(this.div_);
3013 var overlaySize = hterm.getClientSize(this.overlayNode_);
3014
Robert Ginda8a59f762014-07-23 11:29:55 -07003015 this.overlayNode_.style.top =
3016 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08003017 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07003018 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08003019
rgindaf0090c92012-02-10 14:58:52 -08003020 if (this.overlayTimeout_)
3021 clearTimeout(this.overlayTimeout_);
3022
Raymes Khouryc7a06382018-07-04 10:25:45 +10003023 this.accessibilityReader_.assertiveAnnounce(msg);
3024
rgindacc2996c2012-02-24 14:59:31 -08003025 if (opt_timeout === null)
3026 return;
3027
Mike Frysingerb6cfded2017-09-18 00:39:31 -04003028 this.overlayTimeout_ = setTimeout(() => {
3029 this.overlayNode_.style.opacity = '0';
3030 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
3031 }, opt_timeout || 1500);
3032};
3033
3034/**
3035 * Hide the terminal overlay immediately.
3036 *
3037 * Useful when we show an overlay for an event with an unknown end time.
3038 */
3039hterm.Terminal.prototype.hideOverlay = function() {
3040 if (this.overlayTimeout_)
3041 clearTimeout(this.overlayTimeout_);
3042 this.overlayTimeout_ = null;
3043
3044 if (this.overlayNode_.parentNode)
3045 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
3046 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08003047};
3048
rginda4bba5e12012-06-20 16:15:30 -07003049/**
3050 * Paste from the system clipboard to the terminal.
3051 */
3052hterm.Terminal.prototype.paste = function() {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003053 return hterm.pasteFromClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07003054};
3055
3056/**
3057 * Copy a string to the system clipboard.
3058 *
3059 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05003060 *
3061 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07003062 */
3063hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08003064 if (this.prefs_.get('enable-clipboard-notice'))
3065 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
3066
rgindaa09e7332012-08-17 12:49:51 -07003067 var copySource = this.document_.createElement('pre');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04003068 copySource.id = 'hterm:copy-to-clipboard-source';
rginda4bba5e12012-06-20 16:15:30 -07003069 copySource.textContent = str;
3070 copySource.style.cssText = (
3071 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003072 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07003073 'position: absolute;' +
3074 'top: -99px');
3075
3076 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07003077
rginda4bba5e12012-06-20 16:15:30 -07003078 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07003079 var anchorNode = selection.anchorNode;
3080 var anchorOffset = selection.anchorOffset;
3081 var focusNode = selection.focusNode;
3082 var focusOffset = selection.focusOffset;
3083
rginda4bba5e12012-06-20 16:15:30 -07003084 selection.selectAllChildren(copySource);
3085
rgindaa09e7332012-08-17 12:49:51 -07003086 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07003087
Rob Spies56953412014-04-28 14:09:47 -07003088 // IE doesn't support selection.extend. This means that the selection
3089 // won't return on IE.
Robert Ginda6f4f0aa2014-07-28 10:21:39 -07003090 if (selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07003091 selection.collapse(anchorNode, anchorOffset);
3092 selection.extend(focusNode, focusOffset);
3093 }
rgindafaa74742012-08-21 13:34:03 -07003094
rginda4bba5e12012-06-20 16:15:30 -07003095 copySource.parentNode.removeChild(copySource);
3096};
3097
Evan Jones2600d4f2016-12-06 09:29:36 -05003098/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003099 * Display an image.
3100 *
3101 * @param {Object} options The image to display.
3102 * @param {string=} options.name A human readable string for the image.
3103 * @param {string|number=} options.size The size (in bytes).
3104 * @param {boolean=} options.preserveAspectRatio Whether to preserve aspect.
3105 * @param {boolean=} options.inline Whether to display the image inline.
3106 * @param {string|number=} options.width The width of the image.
3107 * @param {string|number=} options.height The height of the image.
3108 * @param {string=} options.align Direction to align the image.
3109 * @param {string} options.uri The source URI for the image.
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003110 * @param {function=} onLoad Callback when loading finishes.
3111 * @param {function(Event)=} onError Callback when loading fails.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003112 */
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003113hterm.Terminal.prototype.displayImage = function(options, onLoad, onError) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003114 // Make sure we're actually given a resource to display.
3115 if (options.uri === undefined)
3116 return;
3117
3118 // Set up the defaults to simplify code below.
3119 if (!options.name)
3120 options.name = '';
3121
3122 // Has the user approved image display yet?
3123 if (this.allowImagesInline !== true) {
3124 this.newLine();
3125 const row = this.getRowNode(this.scrollbackRows_.length +
3126 this.getCursorRow() - 1);
3127
3128 if (this.allowImagesInline === false) {
3129 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
3130 'Inline Images Disabled');
3131 return;
3132 }
3133
3134 // Show a prompt.
3135 let button;
3136 const span = this.document_.createElement('span');
3137 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
3138 span.style.fontWeight = 'bold';
3139 span.style.borderWidth = '1px';
3140 span.style.borderStyle = 'dashed';
3141 button = this.document_.createElement('span');
3142 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3143 button.style.marginLeft = '1em';
3144 button.style.borderWidth = '1px';
3145 button.style.borderStyle = 'solid';
3146 button.addEventListener('click', () => {
3147 this.prefs_.set('allow-images-inline', false);
3148 });
3149 span.appendChild(button);
3150 button = this.document_.createElement('span');
3151 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3152 'allow this session');
3153 button.style.marginLeft = '1em';
3154 button.style.borderWidth = '1px';
3155 button.style.borderStyle = 'solid';
3156 button.addEventListener('click', () => {
3157 this.allowImagesInline = true;
3158 });
3159 span.appendChild(button);
3160 button = this.document_.createElement('span');
3161 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3162 button.style.marginLeft = '1em';
3163 button.style.borderWidth = '1px';
3164 button.style.borderStyle = 'solid';
3165 button.addEventListener('click', () => {
3166 this.prefs_.set('allow-images-inline', true);
3167 });
3168 span.appendChild(button);
3169
3170 row.appendChild(span);
3171 return;
3172 }
3173
3174 // See if we should show this object directly, or download it.
3175 if (options.inline) {
3176 const io = this.io.push();
3177 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
3178 'Loading $1 ...'), null);
3179
3180 // While we're loading the image, eat all the user's input.
3181 io.onVTKeystroke = io.sendString = () => {};
3182
3183 // Initialize this new image.
3184 const img = this.document_.createElement('img');
3185 img.src = options.uri;
3186 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
3251 io.hideOverlay();
3252 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003253
3254 if (onLoad)
3255 onLoad();
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003256 };
3257
3258 // If we got a malformed image, give up.
3259 img.onerror = (e) => {
3260 this.document_.body.removeChild(img);
3261 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
Mike Frysingere14a8c42018-03-10 00:17:30 -08003262 'Loading $1 failed'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003263 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003264
3265 if (onError)
3266 onError(e);
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003267 };
3268 } else {
3269 // We can't use chrome.downloads.download as that requires "downloads"
3270 // permissions, and that works only in extensions, not apps.
3271 const a = this.document_.createElement('a');
3272 a.href = options.uri;
3273 a.download = options.name;
3274 this.document_.body.appendChild(a);
3275 a.click();
3276 a.remove();
3277 }
3278};
3279
3280/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003281 * Returns the selected text, or null if no text is selected.
3282 *
3283 * @return {string|null}
3284 */
rgindaa09e7332012-08-17 12:49:51 -07003285hterm.Terminal.prototype.getSelectionText = function() {
3286 var selection = this.scrollPort_.selection;
3287 selection.sync();
3288
3289 if (selection.isCollapsed)
3290 return null;
3291
rgindaa09e7332012-08-17 12:49:51 -07003292 // Start offset measures from the beginning of the line.
3293 var startOffset = selection.startOffset;
3294 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003295
Raymes Khoury334625a2018-06-25 10:29:40 +10003296 // If an x-row isn't selected, |node| will be null.
3297 if (!node)
3298 return null;
3299
Robert Gindafdbb3f22012-09-06 20:23:06 -07003300 if (node.nodeName != 'X-ROW') {
3301 // If the selection doesn't start on an x-row node, then it must be
3302 // somewhere inside the x-row. Add any characters from previous siblings
3303 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003304
3305 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3306 // If node is the text node in a styled span, move up to the span node.
3307 node = node.parentNode;
3308 }
3309
Robert Gindafdbb3f22012-09-06 20:23:06 -07003310 while (node.previousSibling) {
3311 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003312 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003313 }
rgindaa09e7332012-08-17 12:49:51 -07003314 }
3315
3316 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08003317 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
3318 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003319 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003320
Robert Gindafdbb3f22012-09-06 20:23:06 -07003321 if (node.nodeName != 'X-ROW') {
3322 // If the selection doesn't end on an x-row node, then it must be
3323 // somewhere inside the x-row. Add any characters from following siblings
3324 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003325
3326 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3327 // If node is the text node in a styled span, move up to the span node.
3328 node = node.parentNode;
3329 }
3330
Robert Gindafdbb3f22012-09-06 20:23:06 -07003331 while (node.nextSibling) {
3332 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003333 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003334 }
rgindaa09e7332012-08-17 12:49:51 -07003335 }
3336
3337 var rv = this.getRowsText(selection.startRow.rowIndex,
3338 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003339 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003340};
3341
rginda4bba5e12012-06-20 16:15:30 -07003342/**
3343 * Copy the current selection to the system clipboard, then clear it after a
3344 * short delay.
3345 */
3346hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07003347 var text = this.getSelectionText();
3348 if (text != null)
3349 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07003350};
3351
rgindaf0090c92012-02-10 14:58:52 -08003352hterm.Terminal.prototype.overlaySize = function() {
3353 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
3354};
3355
rginda87b86462011-12-14 13:48:03 -08003356/**
3357 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3358 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003359 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003360 */
3361hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08003362 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08003363 this.scrollPort_.scrollRowToBottom(this.getRowCount());
3364
Robert Ginda8cb7d902013-06-20 14:37:18 -07003365 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08003366};
3367
3368/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003369 * Open the selected url.
3370 */
3371hterm.Terminal.prototype.openSelectedUrl_ = function() {
3372 var str = this.getSelectionText();
3373
3374 // If there is no selection, try and expand wherever they clicked.
3375 if (str == null) {
John Lincae9b732018-03-08 13:56:35 +08003376 this.screen_.expandSelectionForUrl(this.document_.getSelection());
Mike Frysinger70b94692017-01-26 18:57:50 -10003377 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003378
3379 // If clicking in empty space, return.
3380 if (str == null)
3381 return;
Mike Frysinger70b94692017-01-26 18:57:50 -10003382 }
3383
3384 // Make sure URL is valid before opening.
3385 if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0)
3386 return;
Mike Frysinger43472622017-06-26 18:11:07 -04003387
3388 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003389 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003390 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3391 // We have to whitelist a few protocols that lack authorities and thus
3392 // never use the //. Like mailto.
3393 switch (str.split(':', 1)[0]) {
3394 case 'mailto':
3395 break;
3396 default:
3397 str = 'http://' + str;
3398 break;
3399 }
3400 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003401
Mike Frysinger720fa832017-10-23 01:15:52 -04003402 hterm.openUrl(str);
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003403};
Mike Frysinger70b94692017-01-26 18:57:50 -10003404
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003405/**
3406 * Manage the automatic mouse hiding behavior while typing.
3407 *
3408 * @param {boolean=} v Whether to enable automatic hiding.
3409 */
3410hterm.Terminal.prototype.setAutomaticMouseHiding = function(v=null) {
3411 // Since Chrome OS & macOS do this by default everywhere, we don't need to.
3412 // Linux & Windows seem to leave this to specific applications to manage.
3413 if (v === null)
3414 v = (hterm.os != 'cros' && hterm.os != 'mac');
3415
3416 this.mouseHideWhileTyping_ = !!v;
3417};
3418
3419/**
3420 * Handler for monitoring user keyboard activity.
3421 *
3422 * This isn't for processing the keystrokes directly, but for updating any
3423 * state that might toggle based on the user using the keyboard at all.
3424 *
3425 * @param {KeyboardEvent} e The keyboard event that triggered us.
3426 */
3427hterm.Terminal.prototype.onKeyboardActivity_ = function(e) {
3428 // When the user starts typing, hide the mouse cursor.
3429 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_)
3430 this.setCssVar('mouse-cursor-style', 'none');
3431};
Mike Frysinger70b94692017-01-26 18:57:50 -10003432
3433/**
rgindad5613292012-06-19 15:40:37 -07003434 * Add the terminalRow and terminalColumn properties to mouse events and
3435 * then forward on to onMouse().
3436 *
3437 * The terminalRow and terminalColumn properties contain the (row, column)
3438 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003439 *
3440 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003441 */
3442hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003443 if (e.processedByTerminalHandler_) {
3444 // We register our event handlers on the document, as well as the cursor
3445 // and the scroll blocker. Mouse events that occur on the cursor or
3446 // scroll blocker will also appear on the document, but we don't want to
3447 // process them twice.
3448 //
3449 // We can't just prevent bubbling because that has other side effects, so
3450 // we decorate the event object with this property instead.
3451 return;
3452 }
3453
Mike Frysinger468966c2018-08-28 13:48:51 -04003454 // Consume navigation events. Button 3 is usually "browser back" and
3455 // button 4 is "browser forward" which we don't want to happen.
3456 if (e.button > 2) {
3457 e.preventDefault();
3458 // We don't return so click events can be passed to the remote below.
3459 }
3460
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003461 var reportMouseEvents = (!this.defeatMouseReports_ &&
3462 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3463
rgindafaa74742012-08-21 13:34:03 -07003464 e.processedByTerminalHandler_ = true;
3465
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003466 // Handle auto hiding of mouse cursor while typing.
3467 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
3468 // Make sure the mouse cursor is visible.
3469 this.syncMouseStyle();
3470 // This debounce isn't perfect, but should work well enough for such a
3471 // simple implementation. If the user moved the mouse, we enabled this
3472 // debounce, and then moved the mouse just before the timeout, we wouldn't
3473 // debounce that later movement.
3474 this.mouseHideDelay_ = setTimeout(() => this.mouseHideDelay_ = null, 1000);
3475 }
3476
Robert Gindaeda48db2014-07-17 09:25:30 -07003477 // One based row/column stored on the mouse event.
3478 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
3479 this.scrollPort_.characterSize.height) + 1;
3480 e.terminalColumn = parseInt(e.clientX /
3481 this.scrollPort_.characterSize.width) + 1;
3482
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003483 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
3484 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07003485 return;
3486 }
3487
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003488 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07003489 // If the cursor is visible and we're not sending mouse events to the
3490 // host app, then we want to hide the terminal cursor when the mouse
3491 // cursor is over top. This keeps the terminal cursor from interfering
3492 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003493 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3494 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3495 this.cursorNode_.style.display = 'none';
3496 } else if (this.cursorNode_.style.display == 'none') {
3497 this.cursorNode_.style.display = '';
3498 }
3499 }
rgindad5613292012-06-19 15:40:37 -07003500
Robert Ginda928cf632014-03-05 15:07:41 -08003501 if (e.type == 'mousedown') {
Mike Frysingercc114512017-09-11 21:39:17 -04003502 this.contextMenu.hide(e);
3503
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003504 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003505 // If VT mouse reporting is disabled, or has been defeated with
3506 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003507 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003508 this.setSelectionEnabled(true);
3509 } else {
3510 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003511 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003512 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003513 this.setSelectionEnabled(false);
3514 e.preventDefault();
3515 }
3516 }
3517
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003518 if (!reportMouseEvents) {
John Lin2aad22e2018-03-16 13:58:11 +08003519 if (e.type == 'dblclick') {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003520 this.screen_.expandSelection(this.document_.getSelection());
John Lin2aad22e2018-03-16 13:58:11 +08003521 if (this.copyOnSelect)
3522 this.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07003523 }
3524
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003525 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003526 // Debounce this event with the dblclick event. If you try to doubleclick
3527 // a URL to open it, Chrome will fire click then dblclick, but we won't
3528 // have expanded the selection text at the first click event.
3529 clearTimeout(this.timeouts_.openUrl);
3530 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3531 500);
3532 return;
3533 }
3534
Mike Frysinger847577f2017-05-23 23:25:57 -04003535 if (e.type == 'mousedown') {
Mike Frysingercc114512017-09-11 21:39:17 -04003536 if (e.ctrlKey && e.button == 2 /* right button */) {
3537 e.preventDefault();
3538 this.contextMenu.show(e, this);
3539 } else if (e.button == this.mousePasteButton ||
3540 (this.mouseRightClickPaste && e.button == 2 /* right button */)) {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003541 if (!this.paste())
Mike Frysinger05a57f02017-08-27 17:48:55 -04003542 console.warn('Could not paste manually due to web restrictions');
Mike Frysinger847577f2017-05-23 23:25:57 -04003543 }
3544 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003545
Mike Frysinger2edd3612017-05-24 00:54:39 -04003546 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003547 !this.document_.getSelection().isCollapsed) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003548 this.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003549 }
3550
3551 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3552 this.scrollBlockerNode_.engaged) {
3553 // Disengage the scroll-blocker after one of these events.
3554 this.scrollBlockerNode_.engaged = false;
3555 this.scrollBlockerNode_.style.top = '-99px';
3556 }
3557
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003558 // Emulate arrow key presses via scroll wheel events.
3559 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3560 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003561 if (e.type == 'wheel') {
Mike Frysinger321063c2018-08-29 15:33:14 -04003562 const delta = this.scrollPort_.scrollWheelDelta(e);
Mike Frysingerc3030a82017-05-29 14:16:11 -04003563
Mike Frysinger321063c2018-08-29 15:33:14 -04003564 // Helper to turn a wheel event delta into a series of key presses.
3565 const deltaToArrows = (distance, charSize, arrowPos, arrowNeg) => {
3566 if (distance == 0) {
3567 return '';
3568 }
3569
3570 // Convert the scroll distance into a number of rows/cols.
3571 const cells = lib.f.smartFloorDivide(Math.abs(distance), charSize);
3572 const data = '\x1bO' + (distance < 0 ? arrowNeg : arrowPos);
3573 return data.repeat(cells);
3574 };
3575
3576 // The order between up/down and left/right doesn't really matter.
3577 this.io.sendString(
3578 // Up/down arrow keys.
3579 deltaToArrows(delta.y, this.scrollPort_.characterSize.height,
3580 'A', 'B') +
3581 // Left/right arrow keys.
3582 deltaToArrows(delta.x, this.scrollPort_.characterSize.width,
3583 'C', 'D')
3584 );
Mike Frysingerc3030a82017-05-29 14:16:11 -04003585
3586 e.preventDefault();
3587 }
3588 }
Robert Ginda928cf632014-03-05 15:07:41 -08003589 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003590 if (!this.scrollBlockerNode_.engaged) {
3591 if (e.type == 'mousedown') {
3592 // Move the scroll-blocker into place if we want to keep the scrollport
3593 // from scrolling.
3594 this.scrollBlockerNode_.engaged = true;
3595 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3596 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3597 } else if (e.type == 'mousemove') {
3598 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3599 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003600 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003601 e.preventDefault();
3602 }
3603 }
Robert Ginda928cf632014-03-05 15:07:41 -08003604
3605 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003606 }
3607
Robert Ginda928cf632014-03-05 15:07:41 -08003608 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3609 // Restore this on mouseup in case it was temporarily defeated with a
3610 // alt-mousedown. Only do this when the selection is empty so that
3611 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003612 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003613 }
rgindad5613292012-06-19 15:40:37 -07003614};
3615
3616/**
3617 * Clients should override this if they care to know about mouse events.
3618 *
3619 * The event parameter will be a normal DOM mouse click event with additional
3620 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003621 *
3622 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003623 */
3624hterm.Terminal.prototype.onMouse = function(e) { };
3625
3626/**
rginda8e92a692012-05-20 19:37:20 -07003627 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003628 *
3629 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003630 */
Rob Spies06533ba2014-04-24 11:20:37 -07003631hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3632 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003633 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003634
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003635 if (this.reportFocus)
3636 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O');
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003637
Michael Kelly485ecd12014-06-09 11:41:56 -04003638 if (focused === true)
3639 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07003640};
3641
3642/**
rginda8ba33642011-12-14 12:31:31 -08003643 * React when the ScrollPort is scrolled.
3644 */
3645hterm.Terminal.prototype.onScroll_ = function() {
3646 this.scheduleSyncCursorPosition_();
3647};
3648
3649/**
rginda9846e2f2012-01-27 13:53:33 -08003650 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003651 *
3652 * @param {Event} e The DOM paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08003653 */
3654hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaa063b202014-07-21 11:08:25 -07003655 var data = e.text.replace(/\n/mg, '\r');
Connor Hegartyf525ceb2014-09-03 13:36:03 -07003656 data = this.keyboard.encode(data);
Mike Frysingere8c32c82018-03-11 14:57:28 -07003657 if (this.options_.bracketedPaste) {
3658 // We strip out most escape sequences as they can cause issues (like
3659 // inserting an \x1b[201~ midstream). We pass through whitespace
3660 // though: 0x08:\b 0x09:\t 0x0a:\n 0x0d:\r.
3661 // This matches xterm behavior.
3662 const filter = (data) => data.replace(/[\x00-\x07\x0b-\x0c\x0e-\x1f]/g, '');
3663 data = '\x1b[200~' + filter(data) + '\x1b[201~';
3664 }
Robert Gindaa063b202014-07-21 11:08:25 -07003665
3666 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003667};
3668
3669/**
rgindaa09e7332012-08-17 12:49:51 -07003670 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003671 *
3672 * @param {Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07003673 */
3674hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07003675 if (!this.useDefaultWindowCopy) {
3676 e.preventDefault();
3677 setTimeout(this.copySelectionToClipboard.bind(this), 0);
3678 }
rgindaa09e7332012-08-17 12:49:51 -07003679};
3680
3681/**
rginda8ba33642011-12-14 12:31:31 -08003682 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08003683 *
3684 * Note: This function should not directly contain code that alters the internal
3685 * state of the terminal. That kind of code belongs in realizeWidth or
3686 * realizeHeight, so that it can be executed synchronously in the case of a
3687 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08003688 */
3689hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08003690 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07003691 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08003692 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07003693 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08003694
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003695 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08003696 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003697 // gets removed from the document or during the initial load, and we can't
3698 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07003699 // This can also happen if called before the scrollPort calculates the
3700 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08003701 return;
3702 }
3703
rgindaa8ba17d2012-08-15 14:41:10 -07003704 var isNewSize = (columnCount != this.screenSize.width ||
3705 rowCount != this.screenSize.height);
3706
3707 // We do this even if the size didn't change, just to be sure everything is
3708 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04003709 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07003710 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07003711
3712 if (isNewSize)
3713 this.overlaySize();
3714
Robert Gindafb1be6a2013-12-11 11:56:22 -08003715 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07003716 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08003717};
3718
3719/**
3720 * Service the cursor blink timeout.
3721 */
3722hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07003723 if (!this.options_.cursorBlink) {
3724 delete this.timeouts_.cursorBlink;
3725 return;
3726 }
3727
Robert Ginda830583c2013-08-07 13:20:46 -07003728 if (this.cursorNode_.getAttribute('focus') == 'false' ||
3729 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08003730 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07003731 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3732 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08003733 } else {
rginda87b86462011-12-14 13:48:03 -08003734 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07003735 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3736 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08003737 }
3738};
David Reveman8f552492012-03-28 12:18:41 -04003739
3740/**
3741 * Set the scrollbar-visible mode bit.
3742 *
3743 * If scrollbar-visible is on, the vertical scrollbar will be visible.
3744 * Otherwise it will not.
3745 *
3746 * Defaults to on.
3747 *
3748 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
3749 */
3750hterm.Terminal.prototype.setScrollbarVisible = function(state) {
3751 this.scrollPort_.setScrollbarVisible(state);
3752};
Michael Kelly485ecd12014-06-09 11:41:56 -04003753
3754/**
Rob Spies49039e52014-12-17 13:40:04 -08003755 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04003756 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08003757 *
3758 * Defaults to 1.
3759 *
Evan Jones2600d4f2016-12-06 09:29:36 -05003760 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08003761 */
3762hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3763 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3764};
3765
3766/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003767 * Close all web notifications created by terminal bells.
3768 */
3769hterm.Terminal.prototype.closeBellNotifications_ = function() {
3770 this.bellNotificationList_.forEach(function(n) {
3771 n.close();
3772 });
3773 this.bellNotificationList_.length = 0;
3774};
Raymes Khourye5d48982018-08-02 09:08:32 +10003775
3776/**
3777 * Syncs the cursor position when the scrollport gains focus.
3778 */
3779hterm.Terminal.prototype.onScrollportFocus_ = function() {
3780 // If the cursor is offscreen we set selection to the last row on the screen.
3781 const topRowIndex = this.scrollPort_.getTopRowIndex();
3782 const bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
3783 const selection = this.document_.getSelection();
3784 if (!this.syncCursorPosition_() && selection) {
3785 selection.collapse(this.getRowNode(bottomRowIndex));
3786 }
3787};